File: /usr/src/linux/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.52 2001/09/01 00:31:50 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 nt->parms.name[IFNAMSIZ-1] = '\0';
186 strcpy(dev->name, nt->parms.name);
187 if (dev->name[0] == 0) {
188 int i;
189 for (i=1; i<100; i++) {
190 sprintf(dev->name, "sit%d", i);
191 if (__dev_get_by_name(dev->name) == NULL)
192 break;
193 }
194 if (i==100)
195 goto failed;
196 memcpy(nt->parms.name, dev->name, IFNAMSIZ);
197 }
198 if (register_netdevice(dev) < 0)
199 goto failed;
200
201 dev_hold(dev);
202 ipip6_tunnel_link(nt);
203 /* Do not decrement MOD_USE_COUNT here. */
204 return nt;
205
206 failed:
207 kfree(dev);
208 MOD_DEC_USE_COUNT;
209 return NULL;
210 }
211
212 static void ipip6_tunnel_destructor(struct net_device *dev)
213 {
214 if (dev != &ipip6_fb_tunnel_dev) {
215 MOD_DEC_USE_COUNT;
216 }
217 }
218
219 static void ipip6_tunnel_uninit(struct net_device *dev)
220 {
221 if (dev == &ipip6_fb_tunnel_dev) {
222 write_lock_bh(&ipip6_lock);
223 tunnels_wc[0] = NULL;
224 write_unlock_bh(&ipip6_lock);
225 dev_put(dev);
226 } else {
227 ipip6_tunnel_unlink((struct ip_tunnel*)dev->priv);
228 dev_put(dev);
229 }
230 }
231
232
233 void ipip6_err(struct sk_buff *skb, u32 info)
234 {
235 #ifndef I_WISH_WORLD_WERE_PERFECT
236
237 /* It is not :-( All the routers (except for Linux) return only
238 8 bytes of packet payload. It means, that precise relaying of
239 ICMP in the real Internet is absolutely infeasible.
240 */
241 struct iphdr *iph = (struct iphdr*)skb->data;
242 int type = skb->h.icmph->type;
243 int code = skb->h.icmph->code;
244 struct ip_tunnel *t;
245
246 switch (type) {
247 default:
248 case ICMP_PARAMETERPROB:
249 return;
250
251 case ICMP_DEST_UNREACH:
252 switch (code) {
253 case ICMP_SR_FAILED:
254 case ICMP_PORT_UNREACH:
255 /* Impossible event. */
256 return;
257 case ICMP_FRAG_NEEDED:
258 /* Soft state for pmtu is maintained by IP core. */
259 return;
260 default:
261 /* All others are translated to HOST_UNREACH.
262 rfc2003 contains "deep thoughts" about NET_UNREACH,
263 I believe they are just ether pollution. --ANK
264 */
265 break;
266 }
267 break;
268 case ICMP_TIME_EXCEEDED:
269 if (code != ICMP_EXC_TTL)
270 return;
271 break;
272 }
273
274 read_lock(&ipip6_lock);
275 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
276 if (t == NULL || t->parms.iph.daddr == 0)
277 goto out;
278 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
279 goto out;
280
281 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
282 t->err_count++;
283 else
284 t->err_count = 1;
285 t->err_time = jiffies;
286 out:
287 read_unlock(&ipip6_lock);
288 return;
289 #else
290 struct iphdr *iph = (struct iphdr*)dp;
291 int hlen = iph->ihl<<2;
292 struct ipv6hdr *iph6;
293 int type = skb->h.icmph->type;
294 int code = skb->h.icmph->code;
295 int rel_type = 0;
296 int rel_code = 0;
297 int rel_info = 0;
298 struct sk_buff *skb2;
299 struct rt6_info *rt6i;
300
301 if (len < hlen + sizeof(struct ipv6hdr))
302 return;
303 iph6 = (struct ipv6hdr*)(dp + hlen);
304
305 switch (type) {
306 default:
307 return;
308 case ICMP_PARAMETERPROB:
309 if (skb->h.icmph->un.gateway < hlen)
310 return;
311
312 /* So... This guy found something strange INSIDE encapsulated
313 packet. Well, he is fool, but what can we do ?
314 */
315 rel_type = ICMPV6_PARAMPROB;
316 rel_info = skb->h.icmph->un.gateway - hlen;
317 break;
318
319 case ICMP_DEST_UNREACH:
320 switch (code) {
321 case ICMP_SR_FAILED:
322 case ICMP_PORT_UNREACH:
323 /* Impossible event. */
324 return;
325 case ICMP_FRAG_NEEDED:
326 /* Too complicated case ... */
327 return;
328 default:
329 /* All others are translated to HOST_UNREACH.
330 rfc2003 contains "deep thoughts" about NET_UNREACH,
331 I believe, it is just ether pollution. --ANK
332 */
333 rel_type = ICMPV6_DEST_UNREACH;
334 rel_code = ICMPV6_ADDR_UNREACH;
335 break;
336 }
337 break;
338 case ICMP_TIME_EXCEEDED:
339 if (code != ICMP_EXC_TTL)
340 return;
341 rel_type = ICMPV6_TIME_EXCEED;
342 rel_code = ICMPV6_EXC_HOPLIMIT;
343 break;
344 }
345
346 /* Prepare fake skb to feed it to icmpv6_send */
347 skb2 = skb_clone(skb, GFP_ATOMIC);
348 if (skb2 == NULL)
349 return;
350 dst_release(skb2->dst);
351 skb2->dst = NULL;
352 skb_pull(skb2, skb->data - (u8*)iph6);
353 skb2->nh.raw = skb2->data;
354
355 /* Try to guess incoming interface */
356 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
357 if (rt6i && rt6i->rt6i_dev) {
358 skb2->dev = rt6i->rt6i_dev;
359
360 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
361
362 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
363 struct ip_tunnel * t = (struct ip_tunnel*)rt6i->rt6i_dev->priv;
364 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
365 rel_type = ICMPV6_DEST_UNREACH;
366 rel_code = ICMPV6_ADDR_UNREACH;
367 }
368 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
369 }
370 }
371 kfree_skb(skb2);
372 return;
373 #endif
374 }
375
376 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
377 {
378 if (INET_ECN_is_ce(iph->tos) &&
379 INET_ECN_is_not_ce(ip6_get_dsfield(skb->nh.ipv6h)))
380 IP6_ECN_set_ce(skb->nh.ipv6h);
381 }
382
383 int ipip6_rcv(struct sk_buff *skb)
384 {
385 struct iphdr *iph;
386 struct ip_tunnel *tunnel;
387
388 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
389 goto out;
390
391 iph = skb->nh.iph;
392
393 read_lock(&ipip6_lock);
394 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
395 skb->mac.raw = skb->nh.raw;
396 skb->nh.raw = skb->data;
397 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
398 skb->protocol = __constant_htons(ETH_P_IPV6);
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 out:
422 return 0;
423 }
424
425 /* Need this wrapper because NF_HOOK takes the function address */
426 static inline int do_ip_send(struct sk_buff *skb)
427 {
428 return ip_send(skb);
429 }
430
431
432 /* Returns the embedded IPv4 address if the IPv6 address
433 comes from 6to4 (draft-ietf-ngtrans-6to4-04) addr space */
434
435 static inline u32 try_6to4(struct in6_addr *v6dst)
436 {
437 u32 dst = 0;
438
439 if (v6dst->s6_addr16[0] == htons(0x2002)) {
440 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
441 memcpy(&dst, &v6dst->s6_addr16[1], 4);
442 }
443 return dst;
444 }
445
446 /*
447 * This function assumes it is being called from dev_queue_xmit()
448 * and that skb is filled properly by that function.
449 */
450
451 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
452 {
453 struct ip_tunnel *tunnel = (struct ip_tunnel*)dev->priv;
454 struct net_device_stats *stats = &tunnel->stat;
455 struct iphdr *tiph = &tunnel->parms.iph;
456 struct ipv6hdr *iph6 = skb->nh.ipv6h;
457 u8 tos = tunnel->parms.iph.tos;
458 struct rtable *rt; /* Route to the other host */
459 struct net_device *tdev; /* Device to other host */
460 struct iphdr *iph; /* Our new IP header */
461 int max_headroom; /* The extra header space needed */
462 u32 dst = tiph->daddr;
463 int mtu;
464 struct in6_addr *addr6;
465 int addr_type;
466
467 if (tunnel->recursion++) {
468 tunnel->stat.collisions++;
469 goto tx_error;
470 }
471
472 if (skb->protocol != __constant_htons(ETH_P_IPV6))
473 goto tx_error;
474
475 if (!dst)
476 dst = try_6to4(&iph6->daddr);
477
478 if (!dst) {
479 struct neighbour *neigh = NULL;
480
481 if (skb->dst)
482 neigh = skb->dst->neighbour;
483
484 if (neigh == NULL) {
485 if (net_ratelimit())
486 printk(KERN_DEBUG "sit: nexthop == NULL\n");
487 goto tx_error;
488 }
489
490 addr6 = (struct in6_addr*)&neigh->primary_key;
491 addr_type = ipv6_addr_type(addr6);
492
493 if (addr_type == IPV6_ADDR_ANY) {
494 addr6 = &skb->nh.ipv6h->daddr;
495 addr_type = ipv6_addr_type(addr6);
496 }
497
498 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
499 goto tx_error_icmp;
500
501 dst = addr6->s6_addr32[3];
502 }
503
504 if (ip_route_output(&rt, dst, tiph->saddr, RT_TOS(tos), tunnel->parms.link)) {
505 tunnel->stat.tx_carrier_errors++;
506 goto tx_error_icmp;
507 }
508 if (rt->rt_type != RTN_UNICAST) {
509 tunnel->stat.tx_carrier_errors++;
510 goto tx_error_icmp;
511 }
512 tdev = rt->u.dst.dev;
513
514 if (tdev == dev) {
515 ip_rt_put(rt);
516 tunnel->stat.collisions++;
517 goto tx_error;
518 }
519
520 mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
521 if (mtu < 68) {
522 tunnel->stat.collisions++;
523 ip_rt_put(rt);
524 goto tx_error;
525 }
526 if (mtu < IPV6_MIN_MTU)
527 mtu = IPV6_MIN_MTU;
528 if (skb->dst && mtu < skb->dst->pmtu) {
529 struct rt6_info *rt6 = (struct rt6_info*)skb->dst;
530 if (mtu < rt6->u.dst.pmtu) {
531 if (tunnel->parms.iph.daddr || rt6->rt6i_dst.plen == 128) {
532 rt6->rt6i_flags |= RTF_MODIFIED;
533 rt6->u.dst.pmtu = mtu;
534 }
535 }
536 }
537 if (skb->len > mtu) {
538 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
539 ip_rt_put(rt);
540 goto tx_error;
541 }
542
543 if (tunnel->err_count > 0) {
544 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
545 tunnel->err_count--;
546 dst_link_failure(skb);
547 } else
548 tunnel->err_count = 0;
549 }
550
551 skb->h.raw = skb->nh.raw;
552
553 /*
554 * Okay, now see if we can stuff it in the buffer as-is.
555 */
556 max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
557
558 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
559 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
560 if (!new_skb) {
561 ip_rt_put(rt);
562 stats->tx_dropped++;
563 dev_kfree_skb(skb);
564 tunnel->recursion--;
565 return 0;
566 }
567 if (skb->sk)
568 skb_set_owner_w(new_skb, skb->sk);
569 dev_kfree_skb(skb);
570 skb = new_skb;
571 }
572
573 skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
574 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
575 dst_release(skb->dst);
576 skb->dst = &rt->u.dst;
577
578 /*
579 * Push down and install the IPIP header.
580 */
581
582 iph = skb->nh.iph;
583 iph->version = 4;
584 iph->ihl = sizeof(struct iphdr)>>2;
585 if (mtu > IPV6_MIN_MTU)
586 iph->frag_off = __constant_htons(IP_DF);
587 else
588 iph->frag_off = 0;
589
590 iph->protocol = IPPROTO_IPV6;
591 iph->tos = INET_ECN_encapsulate(tos, ip6_get_dsfield(iph6));
592 iph->daddr = rt->rt_dst;
593 iph->saddr = rt->rt_src;
594
595 if ((iph->ttl = tiph->ttl) == 0)
596 iph->ttl = iph6->hop_limit;
597
598 #ifdef CONFIG_NETFILTER
599 nf_conntrack_put(skb->nfct);
600 skb->nfct = NULL;
601 #ifdef CONFIG_NETFILTER_DEBUG
602 skb->nf_debug = 0;
603 #endif
604 #endif
605
606 IPTUNNEL_XMIT();
607 tunnel->recursion--;
608 return 0;
609
610 tx_error_icmp:
611 dst_link_failure(skb);
612 tx_error:
613 stats->tx_errors++;
614 dev_kfree_skb(skb);
615 tunnel->recursion--;
616 return 0;
617 }
618
619 static int
620 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
621 {
622 int err = 0;
623 struct ip_tunnel_parm p;
624 struct ip_tunnel *t;
625
626 MOD_INC_USE_COUNT;
627
628 switch (cmd) {
629 case SIOCGETTUNNEL:
630 t = NULL;
631 if (dev == &ipip6_fb_tunnel_dev) {
632 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
633 err = -EFAULT;
634 break;
635 }
636 t = ipip6_tunnel_locate(&p, 0);
637 }
638 if (t == NULL)
639 t = (struct ip_tunnel*)dev->priv;
640 memcpy(&p, &t->parms, sizeof(p));
641 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
642 err = -EFAULT;
643 break;
644
645 case SIOCADDTUNNEL:
646 case SIOCCHGTUNNEL:
647 err = -EPERM;
648 if (!capable(CAP_NET_ADMIN))
649 goto done;
650
651 err = -EFAULT;
652 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
653 goto done;
654
655 err = -EINVAL;
656 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
657 p.iph.ihl != 5 || (p.iph.frag_off&__constant_htons(~IP_DF)))
658 goto done;
659 if (p.iph.ttl)
660 p.iph.frag_off |= __constant_htons(IP_DF);
661
662 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
663
664 if (dev != &ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL &&
665 t != &ipip6_fb_tunnel) {
666 if (t != NULL) {
667 if (t->dev != dev) {
668 err = -EEXIST;
669 break;
670 }
671 } else {
672 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
673 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
674 err = -EINVAL;
675 break;
676 }
677 t = (struct ip_tunnel*)dev->priv;
678 ipip6_tunnel_unlink(t);
679 t->parms.iph.saddr = p.iph.saddr;
680 t->parms.iph.daddr = p.iph.daddr;
681 memcpy(dev->dev_addr, &p.iph.saddr, 4);
682 memcpy(dev->broadcast, &p.iph.daddr, 4);
683 ipip6_tunnel_link(t);
684 netdev_state_change(dev);
685 }
686 }
687
688 if (t) {
689 err = 0;
690 if (cmd == SIOCCHGTUNNEL) {
691 t->parms.iph.ttl = p.iph.ttl;
692 t->parms.iph.tos = p.iph.tos;
693 }
694 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
695 err = -EFAULT;
696 } else
697 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
698 break;
699
700 case SIOCDELTUNNEL:
701 err = -EPERM;
702 if (!capable(CAP_NET_ADMIN))
703 goto done;
704
705 if (dev == &ipip6_fb_tunnel_dev) {
706 err = -EFAULT;
707 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
708 goto done;
709 err = -ENOENT;
710 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
711 goto done;
712 err = -EPERM;
713 if (t == &ipip6_fb_tunnel)
714 goto done;
715 }
716 err = unregister_netdevice(dev);
717 break;
718
719 default:
720 err = -EINVAL;
721 }
722
723 done:
724 MOD_DEC_USE_COUNT;
725 return err;
726 }
727
728 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
729 {
730 return &(((struct ip_tunnel*)dev->priv)->stat);
731 }
732
733 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
734 {
735 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
736 return -EINVAL;
737 dev->mtu = new_mtu;
738 return 0;
739 }
740
741 static void ipip6_tunnel_init_gen(struct net_device *dev)
742 {
743 struct ip_tunnel *t = (struct ip_tunnel*)dev->priv;
744
745 dev->destructor = ipip6_tunnel_destructor;
746 dev->uninit = ipip6_tunnel_uninit;
747 dev->hard_start_xmit = ipip6_tunnel_xmit;
748 dev->get_stats = ipip6_tunnel_get_stats;
749 dev->do_ioctl = ipip6_tunnel_ioctl;
750 dev->change_mtu = ipip6_tunnel_change_mtu;
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_netdev(&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 register_netdev(&ipip6_fb_tunnel_dev);
856 inet_add_protocol(&sit_protocol);
857 return 0;
858 }
859