RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv6 / sit.c
blob2ea3b26be31723fac464455a13d50e6dc619eaee
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_parm *parms)
104 __be32 remote = parms->iph.daddr;
105 __be32 local = 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 inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
122 return __ipip6_bucket(&t->parms);
125 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
127 struct ip_tunnel **tp;
129 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
130 if (t == *tp) {
131 write_lock_bh(&ipip6_lock);
132 *tp = t->next;
133 write_unlock_bh(&ipip6_lock);
134 break;
139 static void ipip6_tunnel_link(struct ip_tunnel *t)
141 struct ip_tunnel **tp = ipip6_bucket(t);
143 t->next = *tp;
144 write_lock_bh(&ipip6_lock);
145 *tp = t;
146 write_unlock_bh(&ipip6_lock);
149 static void ipip6_tunnel_clone_6rd(struct net_device *dev)
151 #ifdef CONFIG_IPV6_SIT_6RD
152 struct ip_tunnel *t = netdev_priv(dev);
154 /* NB: not t->dev due it isn't inited yet */
155 if (dev == ipip6_fb_tunnel_dev) {
156 ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
157 t->ip6rd.relay_prefix = 0;
158 t->ip6rd.prefixlen = 16;
159 t->ip6rd.relay_prefixlen = 0;
160 } else {
161 struct ip_tunnel *t0 = netdev_priv(ipip6_fb_tunnel_dev);
162 memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
164 #endif
167 static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
169 __be32 remote = parms->iph.daddr;
170 __be32 local = parms->iph.saddr;
171 struct ip_tunnel *t, **tp, *nt;
172 struct net_device *dev;
173 char name[IFNAMSIZ];
175 for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
176 if (local == t->parms.iph.saddr &&
177 remote == t->parms.iph.daddr) {
178 if (create)
179 return NULL;
180 else
181 return t;
184 if (!create)
185 goto failed;
187 if (parms->name[0])
188 strlcpy(name, parms->name, IFNAMSIZ);
189 else {
190 int i;
191 for (i=1; i<100; i++) {
192 sprintf(name, "sit%d", i);
193 if (__dev_get_by_name(name) == NULL)
194 break;
196 if (i==100)
197 goto failed;
200 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
201 if (dev == NULL)
202 return NULL;
204 nt = netdev_priv(dev);
205 dev->init = ipip6_tunnel_init;
206 nt->parms = *parms;
207 ipip6_tunnel_clone_6rd(dev);
209 if (register_netdevice(dev) < 0) {
210 free_netdev(dev);
211 goto failed;
214 dev_hold(dev);
216 ipip6_tunnel_link(nt);
217 return nt;
219 failed:
220 return NULL;
223 static void ipip6_tunnel_uninit(struct net_device *dev)
225 if (dev == ipip6_fb_tunnel_dev) {
226 write_lock_bh(&ipip6_lock);
227 tunnels_wc[0] = NULL;
228 write_unlock_bh(&ipip6_lock);
229 dev_put(dev);
230 } else {
231 ipip6_tunnel_unlink(netdev_priv(dev));
232 dev_put(dev);
237 static int ipip6_err(struct sk_buff *skb, u32 info)
239 #ifndef I_WISH_WORLD_WERE_PERFECT
241 /* It is not :-( All the routers (except for Linux) return only
242 8 bytes of packet payload. It means, that precise relaying of
243 ICMP in the real Internet is absolutely infeasible.
245 struct iphdr *iph = (struct iphdr*)skb->data;
246 const int type = icmp_hdr(skb)->type;
247 const int code = icmp_hdr(skb)->code;
248 struct ip_tunnel *t;
249 int err;
251 switch (type) {
252 default:
253 case ICMP_PARAMETERPROB:
254 return 0;
256 case ICMP_DEST_UNREACH:
257 switch (code) {
258 case ICMP_SR_FAILED:
259 case ICMP_PORT_UNREACH:
260 /* Impossible event. */
261 return 0;
262 case ICMP_FRAG_NEEDED:
263 /* Soft state for pmtu is maintained by IP core. */
264 return 0;
265 default:
266 /* All others are translated to HOST_UNREACH.
267 rfc2003 contains "deep thoughts" about NET_UNREACH,
268 I believe they are just ether pollution. --ANK
270 break;
272 break;
273 case ICMP_TIME_EXCEEDED:
274 if (code != ICMP_EXC_TTL)
275 return 0;
276 break;
279 err = -ENOENT;
281 read_lock(&ipip6_lock);
282 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
283 if (t == NULL || t->parms.iph.daddr == 0)
284 goto out;
286 err = 0;
287 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
288 goto out;
290 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
291 t->err_count++;
292 else
293 t->err_count = 1;
294 t->err_time = jiffies;
295 out:
296 read_unlock(&ipip6_lock);
297 return err;
298 #else
299 struct iphdr *iph = (struct iphdr*)dp;
300 int hlen = iph->ihl<<2;
301 struct ipv6hdr *iph6;
302 const int type = icmp_hdr(skb)->type;
303 const int code = icmp_hdr(skb)->code;
304 int rel_type = 0;
305 int rel_code = 0;
306 int rel_info = 0;
307 struct sk_buff *skb2;
308 struct rt6_info *rt6i;
310 if (len < hlen + sizeof(struct ipv6hdr))
311 return;
312 iph6 = (struct ipv6hdr*)(dp + hlen);
314 switch (type) {
315 default:
316 return;
317 case ICMP_PARAMETERPROB:
318 if (icmp_hdr(skb)->un.gateway < hlen)
319 return;
321 /* So... This guy found something strange INSIDE encapsulated
322 packet. Well, he is fool, but what can we do ?
324 rel_type = ICMPV6_PARAMPROB;
325 rel_info = icmp_hdr(skb)->un.gateway - hlen;
326 break;
328 case ICMP_DEST_UNREACH:
329 switch (code) {
330 case ICMP_SR_FAILED:
331 case ICMP_PORT_UNREACH:
332 /* Impossible event. */
333 return;
334 case ICMP_FRAG_NEEDED:
335 /* Too complicated case ... */
336 return;
337 default:
338 /* All others are translated to HOST_UNREACH.
339 rfc2003 contains "deep thoughts" about NET_UNREACH,
340 I believe, it is just ether pollution. --ANK
342 rel_type = ICMPV6_DEST_UNREACH;
343 rel_code = ICMPV6_ADDR_UNREACH;
344 break;
346 break;
347 case ICMP_TIME_EXCEEDED:
348 if (code != ICMP_EXC_TTL)
349 return;
350 rel_type = ICMPV6_TIME_EXCEED;
351 rel_code = ICMPV6_EXC_HOPLIMIT;
352 break;
355 /* Prepare fake skb to feed it to icmpv6_send */
356 skb2 = skb_clone(skb, GFP_ATOMIC);
357 if (skb2 == NULL)
358 return 0;
359 dst_release(skb2->dst);
360 skb2->dst = NULL;
361 skb_pull(skb2, skb->data - (u8*)iph6);
362 skb_reset_network_header(skb2);
364 /* Try to guess incoming interface */
365 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
366 if (rt6i && rt6i->rt6i_dev) {
367 skb2->dev = rt6i->rt6i_dev;
369 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
371 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
372 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
373 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
374 rel_type = ICMPV6_DEST_UNREACH;
375 rel_code = ICMPV6_ADDR_UNREACH;
377 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
380 kfree_skb(skb2);
381 return 0;
382 #endif
385 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
387 if (INET_ECN_is_ce(iph->tos))
388 IP6_ECN_set_ce(ipv6_hdr(skb));
391 static int ipip6_rcv(struct sk_buff *skb)
393 struct iphdr *iph;
394 struct ip_tunnel *tunnel;
396 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
397 goto out;
399 iph = ip_hdr(skb);
401 read_lock(&ipip6_lock);
402 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
403 secpath_reset(skb);
404 skb->mac_header = skb->network_header;
405 skb_reset_network_header(skb);
406 IPCB(skb)->flags = 0;
407 skb->protocol = htons(ETH_P_IPV6);
408 skb->pkt_type = PACKET_HOST;
409 tunnel->stat.rx_packets++;
410 tunnel->stat.rx_bytes += skb->len;
411 skb->dev = tunnel->dev;
412 dst_release(skb->dst);
413 skb->dst = NULL;
414 nf_reset(skb);
415 ipip6_ecn_decapsulate(iph, skb);
416 netif_rx(skb);
417 read_unlock(&ipip6_lock);
418 return 0;
421 /* no tunnel matched, let upstream know, ipsec may handle it */
422 read_unlock(&ipip6_lock);
423 return 1;
424 out:
425 kfree_skb(skb);
426 return 0;
430 * Returns the embedded IPv4 address if the IPv6 address
431 * comes from 6rd / 6to4 (RFC 3056) addr space.
433 static inline
434 __be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel)
436 __be32 dst = 0;
438 #ifdef CONFIG_IPV6_SIT_6RD
439 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
440 tunnel->ip6rd.prefixlen)) {
441 unsigned pbw0, pbi0;
442 int pbi1;
443 u32 d;
445 pbw0 = tunnel->ip6rd.prefixlen >> 5;
446 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
448 d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
449 tunnel->ip6rd.relay_prefixlen;
451 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
452 if (pbi1 > 0)
453 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
454 (32 - pbi1);
456 dst = tunnel->ip6rd.relay_prefix | htonl(d);
458 #else
459 if (v6dst->s6_addr16[0] == htons(0x2002)) {
460 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
461 memcpy(&dst, &v6dst->s6_addr16[1], 4);
463 #endif
464 return dst;
468 * This function assumes it is being called from dev_queue_xmit()
469 * and that skb is filled properly by that function.
472 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
474 struct ip_tunnel *tunnel = netdev_priv(dev);
475 struct net_device_stats *stats = &tunnel->stat;
476 struct iphdr *tiph = &tunnel->parms.iph;
477 struct ipv6hdr *iph6 = ipv6_hdr(skb);
478 u8 tos = tunnel->parms.iph.tos;
479 __be16 df = tiph->frag_off;
480 struct rtable *rt; /* Route to the other host */
481 struct net_device *tdev; /* Device to other host */
482 struct iphdr *iph; /* Our new IP header */
483 int max_headroom; /* The extra header space needed */
484 __be32 dst = tiph->daddr;
485 int mtu;
486 struct in6_addr *addr6;
487 int addr_type;
489 if (tunnel->recursion++) {
490 tunnel->stat.collisions++;
491 goto tx_error;
494 if (skb->protocol != htons(ETH_P_IPV6))
495 goto tx_error;
497 if (!dst)
498 dst = try_6rd(&iph6->daddr, tunnel);
500 if (!dst) {
501 struct neighbour *neigh = NULL;
503 if (skb->dst)
504 neigh = skb->dst->neighbour;
506 if (neigh == NULL) {
507 if (net_ratelimit())
508 printk(KERN_DEBUG "sit: nexthop == NULL\n");
509 goto tx_error;
512 addr6 = (struct in6_addr*)&neigh->primary_key;
513 addr_type = ipv6_addr_type(addr6);
515 if (addr_type == IPV6_ADDR_ANY) {
516 addr6 = &ipv6_hdr(skb)->daddr;
517 addr_type = ipv6_addr_type(addr6);
520 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
521 goto tx_error_icmp;
523 dst = addr6->s6_addr32[3];
527 struct flowi fl = { .nl_u = { .ip4_u =
528 { .daddr = dst,
529 .saddr = tiph->saddr,
530 .tos = RT_TOS(tos) } },
531 .oif = tunnel->parms.link,
532 .proto = IPPROTO_IPV6 };
533 if (ip_route_output_key(&rt, &fl)) {
534 tunnel->stat.tx_carrier_errors++;
535 goto tx_error_icmp;
538 if (rt->rt_type != RTN_UNICAST) {
539 ip_rt_put(rt);
540 tunnel->stat.tx_carrier_errors++;
541 goto tx_error_icmp;
543 tdev = rt->u.dst.dev;
545 if (tdev == dev) {
546 ip_rt_put(rt);
547 tunnel->stat.collisions++;
548 goto tx_error;
551 if (df) {
552 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
554 if (mtu < 68) {
555 tunnel->stat.collisions++;
556 ip_rt_put(rt);
557 goto tx_error;
560 if (mtu < IPV6_MIN_MTU) {
561 mtu = IPV6_MIN_MTU;
562 df = 0;
565 if (tunnel->parms.iph.daddr && skb->dst)
566 skb->dst->ops->update_pmtu(skb->dst, mtu);
568 if (skb->len > mtu) {
569 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
570 ip_rt_put(rt);
571 goto tx_error;
575 if (tunnel->err_count > 0) {
576 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
577 tunnel->err_count--;
578 dst_link_failure(skb);
579 } else
580 tunnel->err_count = 0;
584 * Okay, now see if we can stuff it in the buffer as-is.
586 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
588 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
589 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
590 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
591 if (!new_skb) {
592 ip_rt_put(rt);
593 stats->tx_dropped++;
594 dev_kfree_skb(skb);
595 tunnel->recursion--;
596 return 0;
598 if (skb->sk)
599 skb_set_owner_w(new_skb, skb->sk);
600 dev_kfree_skb(skb);
601 skb = new_skb;
602 iph6 = ipv6_hdr(skb);
605 skb->transport_header = skb->network_header;
606 skb_push(skb, sizeof(struct iphdr));
607 skb_reset_network_header(skb);
608 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
609 IPCB(skb)->flags = 0;
610 dst_release(skb->dst);
611 skb->dst = &rt->u.dst;
614 * Push down and install the IPIP header.
617 iph = ip_hdr(skb);
618 iph->version = 4;
619 iph->ihl = sizeof(struct iphdr)>>2;
620 iph->frag_off = df;
621 iph->protocol = IPPROTO_IPV6;
622 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
623 iph->daddr = rt->rt_dst;
624 iph->saddr = rt->rt_src;
626 if ((iph->ttl = tiph->ttl) == 0)
627 iph->ttl = iph6->hop_limit;
629 nf_reset(skb);
631 IPTUNNEL_XMIT();
632 tunnel->recursion--;
633 return 0;
635 tx_error_icmp:
636 dst_link_failure(skb);
637 tx_error:
638 stats->tx_errors++;
639 dev_kfree_skb(skb);
640 tunnel->recursion--;
641 return 0;
644 static void ipip6_tunnel_bind_dev(struct net_device *dev)
646 struct net_device *tdev = NULL;
647 struct ip_tunnel *tunnel;
648 struct iphdr *iph;
650 tunnel = netdev_priv(dev);
651 iph = &tunnel->parms.iph;
653 if (iph->daddr) {
654 struct flowi fl = { .nl_u = { .ip4_u =
655 { .daddr = iph->daddr,
656 .saddr = iph->saddr,
657 .tos = RT_TOS(iph->tos) } },
658 .oif = tunnel->parms.link,
659 .proto = IPPROTO_IPV6 };
660 struct rtable *rt;
661 if (!ip_route_output_key(&rt, &fl)) {
662 tdev = rt->u.dst.dev;
663 ip_rt_put(rt);
665 dev->flags |= IFF_POINTOPOINT;
668 if (!tdev && tunnel->parms.link)
669 tdev = __dev_get_by_index(tunnel->parms.link);
671 if (tdev) {
672 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
673 dev->mtu = tdev->mtu - sizeof(struct iphdr);
674 if (dev->mtu < IPV6_MIN_MTU)
675 dev->mtu = IPV6_MIN_MTU;
677 dev->iflink = tunnel->parms.link;
680 static int
681 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
683 int err = 0;
684 struct ip_tunnel_parm p;
685 struct ip_tunnel *t;
686 #ifdef CONFIG_IPV6_SIT_6RD
687 struct ip_tunnel_6rd ip6rd;
688 #endif
690 switch (cmd) {
691 case SIOCGETTUNNEL:
692 #ifdef CONFIG_IPV6_SIT_6RD
693 case SIOCGET6RD:
694 #endif
695 t = NULL;
696 if (dev == ipip6_fb_tunnel_dev) {
697 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
698 err = -EFAULT;
699 break;
701 t = ipip6_tunnel_locate(&p, 0);
703 if (t == NULL)
704 t = netdev_priv(dev);
706 err = -EFAULT;
707 if (cmd == SIOCGETTUNNEL) {
708 memcpy(&p, &t->parms, sizeof(p));
709 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
710 sizeof(p)))
711 goto done;
712 #ifdef CONFIG_IPV6_SIT_6RD
713 } else {
714 ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix);
715 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
716 ip6rd.prefixlen = t->ip6rd.prefixlen;
717 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
718 if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
719 sizeof(ip6rd)))
720 goto done;
721 #endif
723 err = 0;
724 break;
726 case SIOCADDTUNNEL:
727 case SIOCCHGTUNNEL:
728 err = -EPERM;
729 if (!capable(CAP_NET_ADMIN))
730 goto done;
732 err = -EFAULT;
733 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
734 goto done;
736 err = -EINVAL;
737 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
738 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
739 goto done;
740 if (p.iph.ttl)
741 p.iph.frag_off |= htons(IP_DF);
743 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
745 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
746 if (t != NULL) {
747 if (t->dev != dev) {
748 err = -EEXIST;
749 break;
751 } else {
752 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
753 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
754 err = -EINVAL;
755 break;
757 t = netdev_priv(dev);
758 ipip6_tunnel_unlink(t);
759 t->parms.iph.saddr = p.iph.saddr;
760 t->parms.iph.daddr = p.iph.daddr;
761 memcpy(dev->dev_addr, &p.iph.saddr, 4);
762 memcpy(dev->broadcast, &p.iph.daddr, 4);
763 ipip6_tunnel_link(t);
764 netdev_state_change(dev);
768 if (t) {
769 err = 0;
770 if (cmd == SIOCCHGTUNNEL) {
771 t->parms.iph.ttl = p.iph.ttl;
772 t->parms.iph.tos = p.iph.tos;
773 if (t->parms.link != p.link) {
774 t->parms.link = p.link;
775 ipip6_tunnel_bind_dev(dev);
776 netdev_state_change(dev);
779 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
780 err = -EFAULT;
781 } else
782 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
783 break;
785 case SIOCDELTUNNEL:
786 err = -EPERM;
787 if (!capable(CAP_NET_ADMIN))
788 goto done;
790 if (dev == ipip6_fb_tunnel_dev) {
791 err = -EFAULT;
792 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
793 goto done;
794 err = -ENOENT;
795 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
796 goto done;
797 err = -EPERM;
798 if (t == netdev_priv(ipip6_fb_tunnel_dev))
799 goto done;
800 dev = t->dev;
802 unregister_netdevice(dev);
803 err = 0;
804 break;
806 #ifdef CONFIG_IPV6_SIT_6RD
807 case SIOCADD6RD:
808 case SIOCCHG6RD:
809 case SIOCDEL6RD:
810 err = -EPERM;
811 if (!capable(CAP_NET_ADMIN))
812 goto done;
814 err = -EFAULT;
815 if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
816 sizeof(ip6rd)))
817 goto done;
819 t = netdev_priv(dev);
821 if (cmd != SIOCDEL6RD) {
822 struct in6_addr prefix;
823 __be32 relay_prefix;
825 err = -EINVAL;
826 if (ip6rd.relay_prefixlen > 32 ||
827 ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
828 goto done;
830 ipv6_addr_prefix(&prefix, &ip6rd.prefix,
831 ip6rd.prefixlen);
832 if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
833 goto done;
834 if (ip6rd.relay_prefixlen)
835 relay_prefix = ip6rd.relay_prefix &
836 htonl(0xffffffffUL <<
837 (32 - ip6rd.relay_prefixlen));
838 else
839 relay_prefix = 0;
840 if (relay_prefix != ip6rd.relay_prefix)
841 goto done;
843 ipv6_addr_copy(&t->ip6rd.prefix, &prefix);
844 t->ip6rd.relay_prefix = relay_prefix;
845 t->ip6rd.prefixlen = ip6rd.prefixlen;
846 t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
847 } else
848 ipip6_tunnel_clone_6rd(dev);
850 err = 0;
851 break;
852 #endif
854 default:
855 err = -EINVAL;
858 done:
859 return err;
862 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
864 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
867 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
869 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
870 return -EINVAL;
871 dev->mtu = new_mtu;
872 return 0;
875 static void ipip6_tunnel_setup(struct net_device *dev)
877 SET_MODULE_OWNER(dev);
878 dev->uninit = ipip6_tunnel_uninit;
879 dev->destructor = free_netdev;
880 dev->hard_start_xmit = ipip6_tunnel_xmit;
881 dev->get_stats = ipip6_tunnel_get_stats;
882 dev->do_ioctl = ipip6_tunnel_ioctl;
883 dev->change_mtu = ipip6_tunnel_change_mtu;
885 dev->type = ARPHRD_SIT;
886 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
887 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
888 dev->flags = IFF_NOARP;
889 dev->iflink = 0;
890 dev->addr_len = 4;
893 static int ipip6_tunnel_init(struct net_device *dev)
895 struct ip_tunnel *tunnel;
897 tunnel = netdev_priv(dev);
899 tunnel->dev = dev;
900 strcpy(tunnel->parms.name, dev->name);
902 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
903 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
905 ipip6_tunnel_bind_dev(dev);
907 return 0;
910 static int __init ipip6_fb_tunnel_init(struct net_device *dev)
912 struct ip_tunnel *tunnel = netdev_priv(dev);
913 struct iphdr *iph = &tunnel->parms.iph;
915 tunnel->dev = dev;
916 strcpy(tunnel->parms.name, dev->name);
918 iph->version = 4;
919 iph->protocol = IPPROTO_IPV6;
920 iph->ihl = 5;
921 iph->ttl = 64;
923 dev_hold(dev);
924 tunnels_wc[0] = tunnel;
925 return 0;
928 static struct xfrm_tunnel sit_handler = {
929 .handler = ipip6_rcv,
930 .err_handler = ipip6_err,
931 .priority = 1,
934 static void __exit sit_destroy_tunnels(void)
936 int prio;
938 for (prio = 1; prio < 4; prio++) {
939 int h;
940 for (h = 0; h < HASH_SIZE; h++) {
941 struct ip_tunnel *t;
942 while ((t = tunnels[prio][h]) != NULL)
943 unregister_netdevice(t->dev);
948 static void __exit sit_cleanup(void)
950 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
952 rtnl_lock();
953 sit_destroy_tunnels();
954 unregister_netdevice(ipip6_fb_tunnel_dev);
955 rtnl_unlock();
958 static int __init sit_init(void)
960 int err;
962 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
964 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
965 printk(KERN_INFO "sit init: Can't add protocol\n");
966 return -EAGAIN;
969 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
970 ipip6_tunnel_setup);
971 if (!ipip6_fb_tunnel_dev) {
972 err = -ENOMEM;
973 goto err1;
976 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
977 ipip6_tunnel_clone_6rd(ipip6_fb_tunnel_dev);
979 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
980 goto err2;
982 out:
983 return err;
984 err2:
985 free_netdev(ipip6_fb_tunnel_dev);
986 err1:
987 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
988 goto out;
991 module_init(sit_init);
992 module_exit(sit_cleanup);
993 MODULE_LICENSE("GPL");
994 MODULE_ALIAS("sit0");