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