Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / sit.c
blob08d6ed3396e42f6deb53c4422562eb9afb260765
1 /*
2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
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.
16 * Changes:
17 * Roger Venning <r.venning@telstra.com>: 6to4 support
18 * Nate Thompson <nate@thebog.net>: 6to4 support
21 #include <linux/module.h>
22 #include <linux/capability.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/icmp.h>
32 #include <asm/uaccess.h>
33 #include <linux/init.h>
34 #include <linux/netfilter_ipv4.h>
35 #include <linux/if_ether.h>
37 #include <net/sock.h>
38 #include <net/snmp.h>
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 #include <net/dsfield.h>
56 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
58 For comments look at net/ipv4/ip_gre.c --ANK
61 #define HASH_SIZE 16
62 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
64 static int ipip6_fb_tunnel_init(struct net_device *dev);
65 static int ipip6_tunnel_init(struct net_device *dev);
66 static void ipip6_tunnel_setup(struct net_device *dev);
68 static struct net_device *ipip6_fb_tunnel_dev;
70 static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
71 static struct ip_tunnel *tunnels_r[HASH_SIZE];
72 static struct ip_tunnel *tunnels_l[HASH_SIZE];
73 static struct ip_tunnel *tunnels_wc[1];
74 static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
76 static DEFINE_RWLOCK(ipip6_lock);
78 static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
80 unsigned h0 = HASH(remote);
81 unsigned h1 = HASH(local);
82 struct ip_tunnel *t;
84 for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
85 if (local == t->parms.iph.saddr &&
86 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
87 return t;
89 for (t = tunnels_r[h0]; t; t = t->next) {
90 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
91 return t;
93 for (t = tunnels_l[h1]; t; t = t->next) {
94 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
95 return t;
97 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
98 return t;
99 return NULL;
102 static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
104 __be32 remote = t->parms.iph.daddr;
105 __be32 local = t->parms.iph.saddr;
106 unsigned h = 0;
107 int prio = 0;
109 if (remote) {
110 prio |= 2;
111 h ^= HASH(remote);
113 if (local) {
114 prio |= 1;
115 h ^= HASH(local);
117 return &tunnels[prio][h];
120 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
122 struct ip_tunnel **tp;
124 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
125 if (t == *tp) {
126 write_lock_bh(&ipip6_lock);
127 *tp = t->next;
128 write_unlock_bh(&ipip6_lock);
129 break;
134 static void ipip6_tunnel_link(struct ip_tunnel *t)
136 struct ip_tunnel **tp = ipip6_bucket(t);
138 t->next = *tp;
139 write_lock_bh(&ipip6_lock);
140 *tp = t;
141 write_unlock_bh(&ipip6_lock);
144 static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
146 __be32 remote = parms->iph.daddr;
147 __be32 local = parms->iph.saddr;
148 struct ip_tunnel *t, **tp, *nt;
149 struct net_device *dev;
150 unsigned h = 0;
151 int prio = 0;
152 char name[IFNAMSIZ];
154 if (remote) {
155 prio |= 2;
156 h ^= HASH(remote);
158 if (local) {
159 prio |= 1;
160 h ^= HASH(local);
162 for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
163 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
164 return t;
166 if (!create)
167 goto failed;
169 if (parms->name[0])
170 strlcpy(name, parms->name, IFNAMSIZ);
171 else {
172 int i;
173 for (i=1; i<100; i++) {
174 sprintf(name, "sit%d", i);
175 if (__dev_get_by_name(name) == NULL)
176 break;
178 if (i==100)
179 goto failed;
182 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
183 if (dev == NULL)
184 return NULL;
186 nt = netdev_priv(dev);
187 dev->init = ipip6_tunnel_init;
188 nt->parms = *parms;
190 if (register_netdevice(dev) < 0) {
191 free_netdev(dev);
192 goto failed;
195 dev_hold(dev);
197 ipip6_tunnel_link(nt);
198 return nt;
200 failed:
201 return NULL;
204 static void ipip6_tunnel_uninit(struct net_device *dev)
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(netdev_priv(dev));
213 dev_put(dev);
218 static int ipip6_err(struct sk_buff *skb, u32 info)
220 #ifndef I_WISH_WORLD_WERE_PERFECT
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.
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 int err;
232 switch (type) {
233 default:
234 case ICMP_PARAMETERPROB:
235 return 0;
237 case ICMP_DEST_UNREACH:
238 switch (code) {
239 case ICMP_SR_FAILED:
240 case ICMP_PORT_UNREACH:
241 /* Impossible event. */
242 return 0;
243 case ICMP_FRAG_NEEDED:
244 /* Soft state for pmtu is maintained by IP core. */
245 return 0;
246 default:
247 /* All others are translated to HOST_UNREACH.
248 rfc2003 contains "deep thoughts" about NET_UNREACH,
249 I believe they are just ether pollution. --ANK
251 break;
253 break;
254 case ICMP_TIME_EXCEEDED:
255 if (code != ICMP_EXC_TTL)
256 return 0;
257 break;
260 err = -ENOENT;
262 read_lock(&ipip6_lock);
263 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
264 if (t == NULL || t->parms.iph.daddr == 0)
265 goto out;
267 err = 0;
268 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
269 goto out;
271 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
272 t->err_count++;
273 else
274 t->err_count = 1;
275 t->err_time = jiffies;
276 out:
277 read_unlock(&ipip6_lock);
278 return err;
279 #else
280 struct iphdr *iph = (struct iphdr*)dp;
281 int hlen = iph->ihl<<2;
282 struct ipv6hdr *iph6;
283 int type = skb->h.icmph->type;
284 int code = skb->h.icmph->code;
285 int rel_type = 0;
286 int rel_code = 0;
287 int rel_info = 0;
288 struct sk_buff *skb2;
289 struct rt6_info *rt6i;
291 if (len < hlen + sizeof(struct ipv6hdr))
292 return;
293 iph6 = (struct ipv6hdr*)(dp + hlen);
295 switch (type) {
296 default:
297 return;
298 case ICMP_PARAMETERPROB:
299 if (skb->h.icmph->un.gateway < hlen)
300 return;
302 /* So... This guy found something strange INSIDE encapsulated
303 packet. Well, he is fool, but what can we do ?
305 rel_type = ICMPV6_PARAMPROB;
306 rel_info = skb->h.icmph->un.gateway - hlen;
307 break;
309 case ICMP_DEST_UNREACH:
310 switch (code) {
311 case ICMP_SR_FAILED:
312 case ICMP_PORT_UNREACH:
313 /* Impossible event. */
314 return;
315 case ICMP_FRAG_NEEDED:
316 /* Too complicated case ... */
317 return;
318 default:
319 /* All others are translated to HOST_UNREACH.
320 rfc2003 contains "deep thoughts" about NET_UNREACH,
321 I believe, it is just ether pollution. --ANK
323 rel_type = ICMPV6_DEST_UNREACH;
324 rel_code = ICMPV6_ADDR_UNREACH;
325 break;
327 break;
328 case ICMP_TIME_EXCEEDED:
329 if (code != ICMP_EXC_TTL)
330 return;
331 rel_type = ICMPV6_TIME_EXCEED;
332 rel_code = ICMPV6_EXC_HOPLIMIT;
333 break;
336 /* Prepare fake skb to feed it to icmpv6_send */
337 skb2 = skb_clone(skb, GFP_ATOMIC);
338 if (skb2 == NULL)
339 return 0;
340 dst_release(skb2->dst);
341 skb2->dst = NULL;
342 skb_pull(skb2, skb->data - (u8*)iph6);
343 skb2->nh.raw = skb2->data;
345 /* Try to guess incoming interface */
346 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
347 if (rt6i && rt6i->rt6i_dev) {
348 skb2->dev = rt6i->rt6i_dev;
350 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
352 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
353 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
354 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
355 rel_type = ICMPV6_DEST_UNREACH;
356 rel_code = ICMPV6_ADDR_UNREACH;
358 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
361 kfree_skb(skb2);
362 return 0;
363 #endif
366 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
368 if (INET_ECN_is_ce(iph->tos))
369 IP6_ECN_set_ce(skb->nh.ipv6h);
372 static int ipip6_rcv(struct sk_buff *skb)
374 struct iphdr *iph;
375 struct ip_tunnel *tunnel;
377 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
378 goto out;
380 iph = skb->nh.iph;
382 read_lock(&ipip6_lock);
383 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
384 secpath_reset(skb);
385 skb->mac.raw = skb->nh.raw;
386 skb->nh.raw = skb->data;
387 IPCB(skb)->flags = 0;
388 skb->protocol = htons(ETH_P_IPV6);
389 skb->pkt_type = PACKET_HOST;
390 tunnel->stat.rx_packets++;
391 tunnel->stat.rx_bytes += skb->len;
392 skb->dev = tunnel->dev;
393 dst_release(skb->dst);
394 skb->dst = NULL;
395 nf_reset(skb);
396 ipip6_ecn_decapsulate(iph, skb);
397 netif_rx(skb);
398 read_unlock(&ipip6_lock);
399 return 0;
402 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
403 kfree_skb(skb);
404 read_unlock(&ipip6_lock);
405 out:
406 return 0;
409 /* Returns the embedded IPv4 address if the IPv6 address
410 comes from 6to4 (RFC 3056) addr space */
412 static inline __be32 try_6to4(struct in6_addr *v6dst)
414 __be32 dst = 0;
416 if (v6dst->s6_addr16[0] == htons(0x2002)) {
417 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
418 memcpy(&dst, &v6dst->s6_addr16[1], 4);
420 return dst;
424 * This function assumes it is being called from dev_queue_xmit()
425 * and that skb is filled properly by that function.
428 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
430 struct ip_tunnel *tunnel = netdev_priv(dev);
431 struct net_device_stats *stats = &tunnel->stat;
432 struct iphdr *tiph = &tunnel->parms.iph;
433 struct ipv6hdr *iph6 = skb->nh.ipv6h;
434 u8 tos = tunnel->parms.iph.tos;
435 struct rtable *rt; /* Route to the other host */
436 struct net_device *tdev; /* Device to other host */
437 struct iphdr *iph; /* Our new IP header */
438 int max_headroom; /* The extra header space needed */
439 __be32 dst = tiph->daddr;
440 int mtu;
441 struct in6_addr *addr6;
442 int addr_type;
444 if (tunnel->recursion++) {
445 tunnel->stat.collisions++;
446 goto tx_error;
449 if (skb->protocol != htons(ETH_P_IPV6))
450 goto tx_error;
452 if (!dst)
453 dst = try_6to4(&iph6->daddr);
455 if (!dst) {
456 struct neighbour *neigh = NULL;
458 if (skb->dst)
459 neigh = skb->dst->neighbour;
461 if (neigh == NULL) {
462 if (net_ratelimit())
463 printk(KERN_DEBUG "sit: nexthop == NULL\n");
464 goto tx_error;
467 addr6 = (struct in6_addr*)&neigh->primary_key;
468 addr_type = ipv6_addr_type(addr6);
470 if (addr_type == IPV6_ADDR_ANY) {
471 addr6 = &skb->nh.ipv6h->daddr;
472 addr_type = ipv6_addr_type(addr6);
475 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
476 goto tx_error_icmp;
478 dst = addr6->s6_addr32[3];
482 struct flowi fl = { .nl_u = { .ip4_u =
483 { .daddr = dst,
484 .saddr = tiph->saddr,
485 .tos = RT_TOS(tos) } },
486 .oif = tunnel->parms.link,
487 .proto = IPPROTO_IPV6 };
488 if (ip_route_output_key(&rt, &fl)) {
489 tunnel->stat.tx_carrier_errors++;
490 goto tx_error_icmp;
493 if (rt->rt_type != RTN_UNICAST) {
494 ip_rt_put(rt);
495 tunnel->stat.tx_carrier_errors++;
496 goto tx_error_icmp;
498 tdev = rt->u.dst.dev;
500 if (tdev == dev) {
501 ip_rt_put(rt);
502 tunnel->stat.collisions++;
503 goto tx_error;
506 if (tiph->frag_off)
507 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
508 else
509 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
511 if (mtu < 68) {
512 tunnel->stat.collisions++;
513 ip_rt_put(rt);
514 goto tx_error;
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);
521 if (skb->len > mtu) {
522 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
523 ip_rt_put(rt);
524 goto tx_error;
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;
536 * Okay, now see if we can stuff it in the buffer as-is.
538 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
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;
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;
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 IPCB(skb)->flags = 0;
560 dst_release(skb->dst);
561 skb->dst = &rt->u.dst;
564 * Push down and install the IPIP header.
567 iph = skb->nh.iph;
568 iph->version = 4;
569 iph->ihl = sizeof(struct iphdr)>>2;
570 if (mtu > IPV6_MIN_MTU)
571 iph->frag_off = htons(IP_DF);
572 else
573 iph->frag_off = 0;
575 iph->protocol = IPPROTO_IPV6;
576 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
577 iph->daddr = rt->rt_dst;
578 iph->saddr = rt->rt_src;
580 if ((iph->ttl = tiph->ttl) == 0)
581 iph->ttl = iph6->hop_limit;
583 nf_reset(skb);
585 IPTUNNEL_XMIT();
586 tunnel->recursion--;
587 return 0;
589 tx_error_icmp:
590 dst_link_failure(skb);
591 tx_error:
592 stats->tx_errors++;
593 dev_kfree_skb(skb);
594 tunnel->recursion--;
595 return 0;
598 static int
599 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
601 int err = 0;
602 struct ip_tunnel_parm p;
603 struct ip_tunnel *t;
605 switch (cmd) {
606 case SIOCGETTUNNEL:
607 t = NULL;
608 if (dev == ipip6_fb_tunnel_dev) {
609 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
610 err = -EFAULT;
611 break;
613 t = ipip6_tunnel_locate(&p, 0);
615 if (t == NULL)
616 t = netdev_priv(dev);
617 memcpy(&p, &t->parms, sizeof(p));
618 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
619 err = -EFAULT;
620 break;
622 case SIOCADDTUNNEL:
623 case SIOCCHGTUNNEL:
624 err = -EPERM;
625 if (!capable(CAP_NET_ADMIN))
626 goto done;
628 err = -EFAULT;
629 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
630 goto done;
632 err = -EINVAL;
633 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
634 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
635 goto done;
636 if (p.iph.ttl)
637 p.iph.frag_off |= htons(IP_DF);
639 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
641 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
642 if (t != NULL) {
643 if (t->dev != dev) {
644 err = -EEXIST;
645 break;
647 } else {
648 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
649 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
650 err = -EINVAL;
651 break;
653 t = netdev_priv(dev);
654 ipip6_tunnel_unlink(t);
655 t->parms.iph.saddr = p.iph.saddr;
656 t->parms.iph.daddr = p.iph.daddr;
657 memcpy(dev->dev_addr, &p.iph.saddr, 4);
658 memcpy(dev->broadcast, &p.iph.daddr, 4);
659 ipip6_tunnel_link(t);
660 netdev_state_change(dev);
664 if (t) {
665 err = 0;
666 if (cmd == SIOCCHGTUNNEL) {
667 t->parms.iph.ttl = p.iph.ttl;
668 t->parms.iph.tos = p.iph.tos;
670 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
671 err = -EFAULT;
672 } else
673 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
674 break;
676 case SIOCDELTUNNEL:
677 err = -EPERM;
678 if (!capable(CAP_NET_ADMIN))
679 goto done;
681 if (dev == ipip6_fb_tunnel_dev) {
682 err = -EFAULT;
683 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
684 goto done;
685 err = -ENOENT;
686 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
687 goto done;
688 err = -EPERM;
689 if (t == netdev_priv(ipip6_fb_tunnel_dev))
690 goto done;
691 dev = t->dev;
693 unregister_netdevice(dev);
694 err = 0;
695 break;
697 default:
698 err = -EINVAL;
701 done:
702 return err;
705 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
707 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
710 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
712 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
713 return -EINVAL;
714 dev->mtu = new_mtu;
715 return 0;
718 static void ipip6_tunnel_setup(struct net_device *dev)
720 SET_MODULE_OWNER(dev);
721 dev->uninit = ipip6_tunnel_uninit;
722 dev->destructor = free_netdev;
723 dev->hard_start_xmit = ipip6_tunnel_xmit;
724 dev->get_stats = ipip6_tunnel_get_stats;
725 dev->do_ioctl = ipip6_tunnel_ioctl;
726 dev->change_mtu = ipip6_tunnel_change_mtu;
728 dev->type = ARPHRD_SIT;
729 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
730 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
731 dev->flags = IFF_NOARP;
732 dev->iflink = 0;
733 dev->addr_len = 4;
736 static int ipip6_tunnel_init(struct net_device *dev)
738 struct net_device *tdev = NULL;
739 struct ip_tunnel *tunnel;
740 struct iphdr *iph;
742 tunnel = netdev_priv(dev);
743 iph = &tunnel->parms.iph;
745 tunnel->dev = dev;
746 strcpy(tunnel->parms.name, dev->name);
748 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
749 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
751 if (iph->daddr) {
752 struct flowi fl = { .nl_u = { .ip4_u =
753 { .daddr = iph->daddr,
754 .saddr = iph->saddr,
755 .tos = RT_TOS(iph->tos) } },
756 .oif = tunnel->parms.link,
757 .proto = IPPROTO_IPV6 };
758 struct rtable *rt;
759 if (!ip_route_output_key(&rt, &fl)) {
760 tdev = rt->u.dst.dev;
761 ip_rt_put(rt);
763 dev->flags |= IFF_POINTOPOINT;
766 if (!tdev && tunnel->parms.link)
767 tdev = __dev_get_by_index(tunnel->parms.link);
769 if (tdev) {
770 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
771 dev->mtu = tdev->mtu - sizeof(struct iphdr);
772 if (dev->mtu < IPV6_MIN_MTU)
773 dev->mtu = IPV6_MIN_MTU;
775 dev->iflink = tunnel->parms.link;
777 return 0;
780 static int __init ipip6_fb_tunnel_init(struct net_device *dev)
782 struct ip_tunnel *tunnel = netdev_priv(dev);
783 struct iphdr *iph = &tunnel->parms.iph;
785 tunnel->dev = dev;
786 strcpy(tunnel->parms.name, dev->name);
788 iph->version = 4;
789 iph->protocol = IPPROTO_IPV6;
790 iph->ihl = 5;
791 iph->ttl = 64;
793 dev_hold(dev);
794 tunnels_wc[0] = tunnel;
795 return 0;
798 static struct xfrm_tunnel sit_handler = {
799 .handler = ipip6_rcv,
800 .err_handler = ipip6_err,
801 .priority = 1,
804 static void __exit sit_destroy_tunnels(void)
806 int prio;
808 for (prio = 1; prio < 4; prio++) {
809 int h;
810 for (h = 0; h < HASH_SIZE; h++) {
811 struct ip_tunnel *t;
812 while ((t = tunnels[prio][h]) != NULL)
813 unregister_netdevice(t->dev);
818 static void __exit sit_cleanup(void)
820 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
822 rtnl_lock();
823 sit_destroy_tunnels();
824 unregister_netdevice(ipip6_fb_tunnel_dev);
825 rtnl_unlock();
828 static int __init sit_init(void)
830 int err;
832 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
834 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
835 printk(KERN_INFO "sit init: Can't add protocol\n");
836 return -EAGAIN;
839 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
840 ipip6_tunnel_setup);
841 if (!ipip6_fb_tunnel_dev) {
842 err = -ENOMEM;
843 goto err1;
846 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
848 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
849 goto err2;
851 out:
852 return err;
853 err2:
854 free_netdev(ipip6_fb_tunnel_dev);
855 err1:
856 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
857 goto out;
860 module_init(sit_init);
861 module_exit(sit_cleanup);
862 MODULE_LICENSE("GPL");
863 MODULE_ALIAS("sit0");