~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/sit.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.14 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.57 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.138 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.193 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *      IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3  *      Linux INET6 implementation
  4  *
  5  *      Authors:
  6  *      Pedro Roque             <roque@di.fc.ul.pt>     
  7  *      Alexey Kuznetsov        <kuznet@ms2.inr.ac.ru>
  8  *
  9  *      $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
 10  *
 11  *      This program is free software; you can redistribute it and/or
 12  *      modify it under the terms of the GNU General Public License
 13  *      as published by the Free Software Foundation; either version
 14  *      2 of the License, or (at your option) any later version.
 15  *
 16  *      Changes:
 17  * Roger Venning <r.venning@telstra.com>:       6to4 support
 18  * Nate Thompson <nate@thebog.net>:             6to4 support
 19  */
 20 
 21 #include <linux/config.h>
 22 #include <linux/module.h>
 23 #include <linux/errno.h>
 24 #include <linux/types.h>
 25 #include <linux/socket.h>
 26 #include <linux/sockios.h>
 27 #include <linux/sched.h>
 28 #include <linux/net.h>
 29 #include <linux/in6.h>
 30 #include <linux/netdevice.h>
 31 #include <linux/if_arp.h>
 32 #include <linux/icmp.h>
 33 #include <asm/uaccess.h>
 34 #include <linux/init.h>
 35 #include <linux/netfilter_ipv4.h>
 36 
 37 #include <net/sock.h>
 38 #include <net/snmp.h>
 39 
 40 #include <net/ipv6.h>
 41 #include <net/protocol.h>
 42 #include <net/transp_v6.h>
 43 #include <net/ip6_fib.h>
 44 #include <net/ip6_route.h>
 45 #include <net/ndisc.h>
 46 #include <net/addrconf.h>
 47 #include <net/ip.h>
 48 #include <net/udp.h>
 49 #include <net/icmp.h>
 50 #include <net/ipip.h>
 51 #include <net/inet_ecn.h>
 52 #include <net/xfrm.h>
 53 
 54 /*
 55    This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
 56 
 57    For comments look at net/ipv4/ip_gre.c --ANK
 58  */
 59 
 60 #define HASH_SIZE  16
 61 #define HASH(addr) ((addr^(addr>>4))&0xF)
 62 
 63 static int ipip6_fb_tunnel_init(struct net_device *dev);
 64 static int ipip6_tunnel_init(struct net_device *dev);
 65 static void ipip6_tunnel_setup(struct net_device *dev);
 66 
 67 static struct net_device *ipip6_fb_tunnel_dev;
 68 
 69 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
 70 static struct ip_tunnel *tunnels_r[HASH_SIZE];
 71 static struct ip_tunnel *tunnels_l[HASH_SIZE];
 72 static struct ip_tunnel *tunnels_wc[1];
 73 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
 74 
 75 static rwlock_t ipip6_lock = RW_LOCK_UNLOCKED;
 76 
 77 static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local)
 78 {
 79         unsigned h0 = HASH(remote);
 80         unsigned h1 = HASH(local);
 81         struct ip_tunnel *t;
 82 
 83         for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
 84                 if (local == t->parms.iph.saddr &&
 85                     remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
 86                         return t;
 87         }
 88         for (t = tunnels_r[h0]; t; t = t->next) {
 89                 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
 90                         return t;
 91         }
 92         for (t = tunnels_l[h1]; t; t = t->next) {
 93                 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
 94                         return t;
 95         }
 96         if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
 97                 return t;
 98         return NULL;
 99 }
100 
101 static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
102 {
103         u32 remote = t->parms.iph.daddr;
104         u32 local = t->parms.iph.saddr;
105         unsigned h = 0;
106         int prio = 0;
107 
108         if (remote) {
109                 prio |= 2;
110                 h ^= HASH(remote);
111         }
112         if (local) {
113                 prio |= 1;
114                 h ^= HASH(local);
115         }
116         return &tunnels[prio][h];
117 }
118 
119 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
120 {
121         struct ip_tunnel **tp;
122 
123         for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
124                 if (t == *tp) {
125                         write_lock_bh(&ipip6_lock);
126                         *tp = t->next;
127                         write_unlock_bh(&ipip6_lock);
128                         break;
129                 }
130         }
131 }
132 
133 static void ipip6_tunnel_link(struct ip_tunnel *t)
134 {
135         struct ip_tunnel **tp = ipip6_bucket(t);
136 
137         write_lock_bh(&ipip6_lock);
138         t->next = *tp;
139         write_unlock_bh(&ipip6_lock);
140         *tp = t;
141 }
142 
143 static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
144 {
145         u32 remote = parms->iph.daddr;
146         u32 local = parms->iph.saddr;
147         struct ip_tunnel *t, **tp, *nt;
148         struct net_device *dev;
149         unsigned h = 0;
150         int prio = 0;
151         char name[IFNAMSIZ];
152 
153         if (remote) {
154                 prio |= 2;
155                 h ^= HASH(remote);
156         }
157         if (local) {
158                 prio |= 1;
159                 h ^= HASH(local);
160         }
161         for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
162                 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
163                         return t;
164         }
165         if (!create)
166                 goto failed;
167 
168         if (parms->name[0])
169                 strlcpy(name, parms->name, IFNAMSIZ);
170         else {
171                 int i;
172                 for (i=1; i<100; i++) {
173                         sprintf(name, "sit%d", i);
174                         if (__dev_get_by_name(name) == NULL)
175                                 break;
176                 }
177                 if (i==100)
178                         goto failed;
179         }
180 
181         dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
182         if (dev == NULL)
183                 return NULL;
184 
185         nt = dev->priv;
186         dev->init = ipip6_tunnel_init;
187         nt->parms = *parms;
188 
189         if (register_netdevice(dev) < 0) {
190                 kfree(dev);
191                 goto failed;
192         }
193 
194         dev_hold(dev);
195 
196         ipip6_tunnel_link(nt);
197         /* Do not decrement MOD_USE_COUNT here. */
198         return nt;
199 
200 failed:
201         return NULL;
202 }
203 
204 static void ipip6_tunnel_uninit(struct net_device *dev)
205 {
206         if (dev == ipip6_fb_tunnel_dev) {
207                 write_lock_bh(&ipip6_lock);
208                 tunnels_wc[0] = NULL;
209                 write_unlock_bh(&ipip6_lock);
210                 dev_put(dev);
211         } else {
212                 ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv);
213                 dev_put(dev);
214         }
215 }
216 
217 
218 static void ipip6_err(struct sk_buff *skb, u32 info)
219 {
220 #ifndef I_WISH_WORLD_WERE_PERFECT
221 
222 /* It is not :-( All the routers (except for Linux) return only
223    8 bytes of packet payload. It means, that precise relaying of
224    ICMP in the real Internet is absolutely infeasible.
225  */
226         struct iphdr *iph = (struct iphdr*)skb->data;
227         int type = skb->h.icmph->type;
228         int code = skb->h.icmph->code;
229         struct ip_tunnel *t;
230 
231         switch (type) {
232         default:
233         case ICMP_PARAMETERPROB:
234                 return;
235 
236         case ICMP_DEST_UNREACH:
237                 switch (code) {
238                 case ICMP_SR_FAILED:
239                 case ICMP_PORT_UNREACH:
240                         /* Impossible event. */
241                         return;
242                 case ICMP_FRAG_NEEDED:
243                         /* Soft state for pmtu is maintained by IP core. */
244                         return;
245                 default:
246                         /* All others are translated to HOST_UNREACH.
247                            rfc2003 contains "deep thoughts" about NET_UNREACH,
248                            I believe they are just ether pollution. --ANK
249                          */
250                         break;
251                 }
252                 break;
253         case ICMP_TIME_EXCEEDED:
254                 if (code != ICMP_EXC_TTL)
255                         return;
256                 break;
257         }
258 
259         read_lock(&ipip6_lock);
260         t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
261         if (t == NULL || t->parms.iph.daddr == 0)
262                 goto out;
263         if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
264                 goto out;
265 
266         if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
267                 t->err_count++;
268         else
269                 t->err_count = 1;
270         t->err_time = jiffies;
271 out:
272         read_unlock(&ipip6_lock);
273         return;
274 #else
275         struct iphdr *iph = (struct iphdr*)dp;
276         int hlen = iph->ihl<<2;
277         struct ipv6hdr *iph6;
278         int type = skb->h.icmph->type;
279         int code = skb->h.icmph->code;
280         int rel_type = 0;
281         int rel_code = 0;
282         int rel_info = 0;
283         struct sk_buff *skb2;
284         struct rt6_info *rt6i;
285 
286         if (len < hlen + sizeof(struct ipv6hdr))
287                 return;
288         iph6 = (struct ipv6hdr*)(dp + hlen);
289 
290         switch (type) {
291         default:
292                 return;
293         case ICMP_PARAMETERPROB:
294                 if (skb->h.icmph->un.gateway < hlen)
295                         return;
296 
297                 /* So... This guy found something strange INSIDE encapsulated
298                    packet. Well, he is fool, but what can we do ?
299                  */
300                 rel_type = ICMPV6_PARAMPROB;
301                 rel_info = skb->h.icmph->un.gateway - hlen;
302                 break;
303 
304         case ICMP_DEST_UNREACH:
305                 switch (code) {
306                 case ICMP_SR_FAILED:
307                 case ICMP_PORT_UNREACH:
308                         /* Impossible event. */
309                         return;
310                 case ICMP_FRAG_NEEDED:
311                         /* Too complicated case ... */
312                         return;
313                 default:
314                         /* All others are translated to HOST_UNREACH.
315                            rfc2003 contains "deep thoughts" about NET_UNREACH,
316                            I believe, it is just ether pollution. --ANK
317                          */
318                         rel_type = ICMPV6_DEST_UNREACH;
319                         rel_code = ICMPV6_ADDR_UNREACH;
320                         break;
321                 }
322                 break;
323         case ICMP_TIME_EXCEEDED:
324                 if (code != ICMP_EXC_TTL)
325                         return;
326                 rel_type = ICMPV6_TIME_EXCEED;
327                 rel_code = ICMPV6_EXC_HOPLIMIT;
328                 break;
329         }
330 
331         /* Prepare fake skb to feed it to icmpv6_send */
332         skb2 = skb_clone(skb, GFP_ATOMIC);
333         if (skb2 == NULL)
334                 return;
335         dst_release(skb2->dst);
336         skb2->dst = NULL;
337         skb_pull(skb2, skb->data - (u8*)iph6);
338         skb2->nh.raw = skb2->data;
339 
340         /* Try to guess incoming interface */
341         rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
342         if (rt6i && rt6i->rt6i_dev) {
343                 skb2->dev = rt6i->rt6i_dev;
344 
345                 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
346 
347                 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
348                         struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv;
349                         if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
350                                 rel_type = ICMPV6_DEST_UNREACH;
351                                 rel_code = ICMPV6_ADDR_UNREACH;
352                         }
353                         icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
354                 }
355         }
356         kfree_skb(skb2);
357         return;
358 #endif
359 }
360 
361 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
362 {
363         if (INET_ECN_is_ce(iph->tos) &&
364             INET_ECN_is_not_ce(ip6_get_dsfield(skb->nh.ipv6h)))
365                 IP6_ECN_set_ce(skb->nh.ipv6h);
366 }
367 
368 static int ipip6_rcv(struct sk_buff *skb)
369 {
370         struct iphdr *iph;
371         struct ip_tunnel *tunnel;
372 
373         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
374                 goto out;
375 
376         iph = skb->nh.iph;
377 
378         read_lock(&ipip6_lock);
379         if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
380                 secpath_reset(skb);
381                 skb->mac.raw = skb->nh.raw;
382                 skb->nh.raw = skb->data;
383                 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
384                 skb->protocol = htons(ETH_P_IPV6);
385                 skb->pkt_type = PACKET_HOST;
386                 tunnel->stat.rx_packets++;
387                 tunnel->stat.rx_bytes += skb->len;
388                 skb->dev = tunnel->dev;
389                 dst_release(skb->dst);
390                 skb->dst = NULL;
391 #ifdef CONFIG_NETFILTER
392                 nf_conntrack_put(skb->nfct);
393                 skb->nfct = NULL;
394 #ifdef CONFIG_NETFILTER_DEBUG
395                 skb->nf_debug = 0;
396 #endif
397 #endif
398                 ipip6_ecn_decapsulate(iph, skb);
399                 netif_rx(skb);
400                 read_unlock(&ipip6_lock);
401                 return 0;
402         }
403 
404         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
405         kfree_skb(skb);
406         read_unlock(&ipip6_lock);
407 out:
408         return 0;
409 }
410 
411 /* Returns the embedded IPv4 address if the IPv6 address
412    comes from 6to4 (RFC 3056) addr space */
413 
414 static inline u32 try_6to4(struct in6_addr *v6dst)
415 {
416         u32 dst = 0;
417 
418         if (v6dst->s6_addr16[0] == htons(0x2002)) {
419                 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
420                 memcpy(&dst, &v6dst->s6_addr16[1], 4);
421         }
422         return dst;
423 }
424 
425 /*
426  *      This function assumes it is being called from dev_queue_xmit()
427  *      and that skb is filled properly by that function.
428  */
429 
430 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
431 {
432         struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
433         struct net_device_stats *stats = &tunnel->stat;
434         struct iphdr  *tiph = &tunnel->parms.iph;
435         struct ipv6hdr *iph6 = skb->nh.ipv6h;
436         u8     tos = tunnel->parms.iph.tos;
437         struct rtable *rt;                      /* Route to the other host */
438         struct net_device *tdev;                        /* Device to other host */
439         struct iphdr  *iph;                     /* Our new IP header */
440         int    max_headroom;                    /* The extra header space needed */
441         u32    dst = tiph->daddr;
442         int    mtu;
443         struct in6_addr *addr6; 
444         int addr_type;
445 
446         if (tunnel->recursion++) {
447                 tunnel->stat.collisions++;
448                 goto tx_error;
449         }
450 
451         if (skb->protocol != htons(ETH_P_IPV6))
452                 goto tx_error;
453 
454         if (!dst)
455                 dst = try_6to4(&iph6->daddr);
456 
457         if (!dst) {
458                 struct neighbour *neigh = NULL;
459 
460                 if (skb->dst)
461                         neigh = skb->dst->neighbour;
462 
463                 if (neigh == NULL) {
464                         if (net_ratelimit())
465                                 printk(KERN_DEBUG "sit: nexthop == NULL\n");
466                         goto tx_error;
467                 }
468 
469                 addr6 = (struct in6_addr*)&neigh->primary_key;
470                 addr_type = ipv6_addr_type(addr6);
471 
472                 if (addr_type == IPV6_ADDR_ANY) {
473                         addr6 = &skb->nh.ipv6h->daddr;
474                         addr_type = ipv6_addr_type(addr6);
475                 }
476 
477                 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
478                         goto tx_error_icmp;
479 
480                 dst = addr6->s6_addr32[3];
481         }
482 
483         {
484                 struct flowi fl = { .nl_u = { .ip4_u =
485                                               { .daddr = dst,
486                                                 .saddr = tiph->saddr,
487                                                 .tos = RT_TOS(tos) } },
488                                     .oif = tunnel->parms.link };
489                 if (ip_route_output_key(&rt, &fl)) {
490                         tunnel->stat.tx_carrier_errors++;
491                         goto tx_error_icmp;
492                 }
493         }
494         if (rt->rt_type != RTN_UNICAST) {
495                 tunnel->stat.tx_carrier_errors++;
496                 goto tx_error_icmp;
497         }
498         tdev = rt->u.dst.dev;
499 
500         if (tdev == dev) {
501                 ip_rt_put(rt);
502                 tunnel->stat.collisions++;
503                 goto tx_error;
504         }
505 
506         if (tiph->frag_off)
507                 mtu = dst_pmtu(&rt->u.dst) - sizeof(struct iphdr);
508         else
509                 mtu = skb->dst ? dst_pmtu(skb->dst) : dev->mtu;
510 
511         if (mtu < 68) {
512                 tunnel->stat.collisions++;
513                 ip_rt_put(rt);
514                 goto tx_error;
515         }
516         if (mtu < IPV6_MIN_MTU)
517                 mtu = IPV6_MIN_MTU;
518         if (tunnel->parms.iph.daddr && skb->dst)
519                 skb->dst->ops->update_pmtu(skb->dst, mtu);
520 
521         if (skb->len > mtu) {
522                 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
523                 ip_rt_put(rt);
524                 goto tx_error;
525         }
526 
527         if (tunnel->err_count > 0) {
528                 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
529                         tunnel->err_count--;
530                         dst_link_failure(skb);
531                 } else
532                         tunnel->err_count = 0;
533         }
534 
535         /*
536          * Okay, now see if we can stuff it in the buffer as-is.
537          */
538         max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
539 
540         if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
541                 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
542                 if (!new_skb) {
543                         ip_rt_put(rt);
544                         stats->tx_dropped++;
545                         dev_kfree_skb(skb);
546                         tunnel->recursion--;
547                         return 0;
548                 }
549                 if (skb->sk)
550                         skb_set_owner_w(new_skb, skb->sk);
551                 dev_kfree_skb(skb);
552                 skb = new_skb;
553                 iph6 = skb->nh.ipv6h;
554         }
555 
556         skb->h.raw = skb->nh.raw;
557         skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
558         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
559         dst_release(skb->dst);
560         skb->dst = &rt->u.dst;
561 
562         /*
563          *      Push down and install the IPIP header.
564          */
565 
566         iph                     =       skb->nh.iph;
567         iph->version            =       4;
568         iph->ihl                =       sizeof(struct iphdr)>>2;
569         if (mtu > IPV6_MIN_MTU)
570                 iph->frag_off   =       htons(IP_DF);
571         else
572                 iph->frag_off   =       0;
573 
574         iph->protocol           =       IPPROTO_IPV6;
575         iph->tos                =       INET_ECN_encapsulate(tos, ip6_get_dsfield(iph6));
576         iph->daddr              =       rt->rt_dst;
577         iph->saddr              =       rt->rt_src;
578 
579         if ((iph->ttl = tiph->ttl) == 0)
580                 iph->ttl        =       iph6->hop_limit;
581 
582 #ifdef CONFIG_NETFILTER
583         nf_conntrack_put(skb->nfct);
584         skb->nfct = NULL;
585 #ifdef CONFIG_NETFILTER_DEBUG
586         skb->nf_debug = 0;
587 #endif
588 #endif
589 
590         IPTUNNEL_XMIT();
591         tunnel->recursion--;
592         return 0;
593 
594 tx_error_icmp:
595         dst_link_failure(skb);
596 tx_error:
597         stats->tx_errors++;
598         dev_kfree_skb(skb);
599         tunnel->recursion--;
600         return 0;
601 }
602 
603 static int
604 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
605 {
606         int err = 0;
607         struct ip_tunnel_parm p;
608         struct ip_tunnel *t;
609 
610         switch (cmd) {
611         case SIOCGETTUNNEL:
612                 t = NULL;
613                 if (dev == ipip6_fb_tunnel_dev) {
614                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
615                                 err = -EFAULT;
616                                 break;
617                         }
618                         t = ipip6_tunnel_locate(&p, 0);
619                 }
620                 if (t == NULL)
621                         t = (struct ip_tunnel*)dev->priv;
622                 memcpy(&p, &t->parms, sizeof(p));
623                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
624                         err = -EFAULT;
625                 break;
626 
627         case SIOCADDTUNNEL:
628         case SIOCCHGTUNNEL:
629                 err = -EPERM;
630                 if (!capable(CAP_NET_ADMIN))
631                         goto done;
632 
633                 err = -EFAULT;
634                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
635                         goto done;
636 
637                 err = -EINVAL;
638                 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
639                     p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
640                         goto done;
641                 if (p.iph.ttl)
642                         p.iph.frag_off |= htons(IP_DF);
643 
644                 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
645 
646                 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
647                         if (t != NULL) {
648                                 if (t->dev != dev) {
649                                         err = -EEXIST;
650                                         break;
651                                 }
652                         } else {
653                                 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
654                                     (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
655                                         err = -EINVAL;
656                                         break;
657                                 }
658                                 t = (struct ip_tunnel*)dev->priv;
659                                 ipip6_tunnel_unlink(t);
660                                 t->parms.iph.saddr = p.iph.saddr;
661                                 t->parms.iph.daddr = p.iph.daddr;
662                                 memcpy(dev->dev_addr, &p.iph.saddr, 4);
663                                 memcpy(dev->broadcast, &p.iph.daddr, 4);
664                                 ipip6_tunnel_link(t);
665                                 netdev_state_change(dev);
666                         }
667                 }
668 
669                 if (t) {
670                         err = 0;
671                         if (cmd == SIOCCHGTUNNEL) {
672                                 t->parms.iph.ttl = p.iph.ttl;
673                                 t->parms.iph.tos = p.iph.tos;
674                         }
675                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
676                                 err = -EFAULT;
677                 } else
678                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
679                 break;
680 
681         case SIOCDELTUNNEL:
682                 err = -EPERM;
683                 if (!capable(CAP_NET_ADMIN))
684                         goto done;
685 
686                 if (dev == ipip6_fb_tunnel_dev) {
687                         err = -EFAULT;
688                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
689                                 goto done;
690                         err = -ENOENT;
691                         if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
692                                 goto done;
693                         err = -EPERM;
694                         if (t == ipip6_fb_tunnel_dev->priv)
695                                 goto done;
696                         dev = t->dev;
697                 }
698                 err = unregister_netdevice(dev);
699                 break;
700 
701         default:
702                 err = -EINVAL;
703         }
704 
705 done:
706         return err;
707 }
708 
709 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
710 {
711         return &(((struct ip_tunnel*)dev->priv)->stat);
712 }
713 
714 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
715 {
716         if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
717                 return -EINVAL;
718         dev->mtu = new_mtu;
719         return 0;
720 }
721 
722 static void ipip6_tunnel_setup(struct net_device *dev)
723 {
724         SET_MODULE_OWNER(dev);
725         dev->uninit             = ipip6_tunnel_uninit;
726         dev->destructor         = free_netdev;
727         dev->hard_start_xmit    = ipip6_tunnel_xmit;
728         dev->get_stats          = ipip6_tunnel_get_stats;
729         dev->do_ioctl           = ipip6_tunnel_ioctl;
730         dev->change_mtu         = ipip6_tunnel_change_mtu;
731 
732         dev->type               = ARPHRD_SIT;
733         dev->hard_header_len    = LL_MAX_HEADER + sizeof(struct iphdr);
734         dev->mtu                = 1500 - sizeof(struct iphdr);
735         dev->flags              = IFF_NOARP;
736         dev->iflink             = 0;
737         dev->addr_len           = 4;
738 }
739 
740 static int ipip6_tunnel_init(struct net_device *dev)
741 {
742         struct net_device *tdev = NULL;
743         struct ip_tunnel *tunnel;
744         struct iphdr *iph;
745 
746         tunnel = (struct ip_tunnel*)dev->priv;
747         iph = &tunnel->parms.iph;
748 
749         tunnel->dev = dev;
750         strcpy(tunnel->parms.name, dev->name);
751 
752         memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
753         memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
754 
755         if (iph->daddr) {
756                 struct flowi fl = { .nl_u = { .ip4_u =
757                                               { .daddr = iph->daddr,
758                                                 .saddr = iph->saddr,
759                                                 .tos = RT_TOS(iph->tos) } },
760                                     .oif = tunnel->parms.link };
761                 struct rtable *rt;
762                 if (!ip_route_output_key(&rt, &fl)) {
763                         tdev = rt->u.dst.dev;
764                         ip_rt_put(rt);
765                 }
766                 dev->flags |= IFF_POINTOPOINT;
767         }
768 
769         if (!tdev && tunnel->parms.link)
770                 tdev = __dev_get_by_index(tunnel->parms.link);
771 
772         if (tdev) {
773                 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
774                 dev->mtu = tdev->mtu - sizeof(struct iphdr);
775                 if (dev->mtu < IPV6_MIN_MTU)
776                         dev->mtu = IPV6_MIN_MTU;
777         }
778         dev->iflink = tunnel->parms.link;
779 
780         return 0;
781 }
782 
783 int __init ipip6_fb_tunnel_init(struct net_device *dev)
784 {
785         struct ip_tunnel *tunnel = dev->priv;
786         struct iphdr *iph = &tunnel->parms.iph;
787 
788         tunnel->dev = dev;
789         strcpy(tunnel->parms.name, dev->name);
790 
791         iph->version            = 4;
792         iph->protocol           = IPPROTO_IPV6;
793         iph->ihl                = 5;
794         iph->ttl                = 64;
795 
796         dev_hold(dev);
797         tunnels_wc[0]           = tunnel;
798         return 0;
799 }
800 
801 static struct inet_protocol sit_protocol = {
802         .handler        =       ipip6_rcv,
803         .err_handler    =       ipip6_err,
804 };
805 
806 #ifdef MODULE
807 void sit_cleanup(void)
808 {
809         inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
810         unregister_netdev(ipip6_fb_tunnel_dev);
811 }
812 #endif
813 
814 int __init sit_init(void)
815 {
816         int err;
817 
818         printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
819 
820         if (inet_add_protocol(&sit_protocol, IPPROTO_IPV6) < 0) {
821                 printk(KERN_INFO "sit init: Can't add protocol\n");
822                 return -EAGAIN;
823         }
824 
825         ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0", 
826                                            ipip6_tunnel_setup);
827         if (!ipip6_fb_tunnel_dev) {
828                 err = -ENOMEM;
829                 goto fail;
830         }
831 
832         ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
833 
834         if ((err =  register_netdev(ipip6_fb_tunnel_dev)))
835                 goto fail;
836 
837  out:
838         return err;
839  fail:
840         inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
841         kfree(ipip6_fb_tunnel_dev);
842         goto out;
843 }
844 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp