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