[IPV6] ADDRCONF: Convert ipv6_get_saddr() to ipv6_dev_get_saddr().
[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 * Fred L. Templin <fltemplin@acm.org>: isatap support
20 */
21
22 #include <linux/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.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_parm *parms)
104 {
105 __be32 remote = parms->iph.daddr;
106 __be32 local = 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 inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
122 {
123 return __ipip6_bucket(&t->parms);
124 }
125
126 static void ipip6_tunnel_unlink(struct ip_tunnel *t)
127 {
128 struct ip_tunnel **tp;
129
130 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
131 if (t == *tp) {
132 write_lock_bh(&ipip6_lock);
133 *tp = t->next;
134 write_unlock_bh(&ipip6_lock);
135 break;
136 }
137 }
138 }
139
140 static void ipip6_tunnel_link(struct ip_tunnel *t)
141 {
142 struct ip_tunnel **tp = ipip6_bucket(t);
143
144 t->next = *tp;
145 write_lock_bh(&ipip6_lock);
146 *tp = t;
147 write_unlock_bh(&ipip6_lock);
148 }
149
150 static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
151 {
152 __be32 remote = parms->iph.daddr;
153 __be32 local = parms->iph.saddr;
154 struct ip_tunnel *t, **tp, *nt;
155 struct net_device *dev;
156 char name[IFNAMSIZ];
157
158 for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
159 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
160 return t;
161 }
162 if (!create)
163 goto failed;
164
165 if (parms->name[0])
166 strlcpy(name, parms->name, IFNAMSIZ);
167 else
168 sprintf(name, "sit%%d");
169
170 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
171 if (dev == NULL)
172 return NULL;
173
174 if (strchr(name, '%')) {
175 if (dev_alloc_name(dev, name) < 0)
176 goto failed_free;
177 }
178
179 nt = netdev_priv(dev);
180 dev->init = ipip6_tunnel_init;
181 nt->parms = *parms;
182
183 if (parms->i_flags & SIT_ISATAP)
184 dev->priv_flags |= IFF_ISATAP;
185
186 if (register_netdevice(dev) < 0)
187 goto failed_free;
188
189 dev_hold(dev);
190
191 ipip6_tunnel_link(nt);
192 return nt;
193
194 failed_free:
195 free_netdev(dev);
196 failed:
197 return NULL;
198 }
199
200 static void ipip6_tunnel_uninit(struct net_device *dev)
201 {
202 if (dev == ipip6_fb_tunnel_dev) {
203 write_lock_bh(&ipip6_lock);
204 tunnels_wc[0] = NULL;
205 write_unlock_bh(&ipip6_lock);
206 dev_put(dev);
207 } else {
208 ipip6_tunnel_unlink(netdev_priv(dev));
209 dev_put(dev);
210 }
211 }
212
213
214 static int ipip6_err(struct sk_buff *skb, u32 info)
215 {
216 #ifndef I_WISH_WORLD_WERE_PERFECT
217
218 /* It is not :-( All the routers (except for Linux) return only
219 8 bytes of packet payload. It means, that precise relaying of
220 ICMP in the real Internet is absolutely infeasible.
221 */
222 struct iphdr *iph = (struct iphdr*)skb->data;
223 const int type = icmp_hdr(skb)->type;
224 const int code = icmp_hdr(skb)->code;
225 struct ip_tunnel *t;
226 int err;
227
228 switch (type) {
229 default:
230 case ICMP_PARAMETERPROB:
231 return 0;
232
233 case ICMP_DEST_UNREACH:
234 switch (code) {
235 case ICMP_SR_FAILED:
236 case ICMP_PORT_UNREACH:
237 /* Impossible event. */
238 return 0;
239 case ICMP_FRAG_NEEDED:
240 /* Soft state for pmtu is maintained by IP core. */
241 return 0;
242 default:
243 /* All others are translated to HOST_UNREACH.
244 rfc2003 contains "deep thoughts" about NET_UNREACH,
245 I believe they are just ether pollution. --ANK
246 */
247 break;
248 }
249 break;
250 case ICMP_TIME_EXCEEDED:
251 if (code != ICMP_EXC_TTL)
252 return 0;
253 break;
254 }
255
256 err = -ENOENT;
257
258 read_lock(&ipip6_lock);
259 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
260 if (t == NULL || t->parms.iph.daddr == 0)
261 goto out;
262
263 err = 0;
264 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
265 goto out;
266
267 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
268 t->err_count++;
269 else
270 t->err_count = 1;
271 t->err_time = jiffies;
272 out:
273 read_unlock(&ipip6_lock);
274 return err;
275 #else
276 struct iphdr *iph = (struct iphdr*)dp;
277 int hlen = iph->ihl<<2;
278 struct ipv6hdr *iph6;
279 const int type = icmp_hdr(skb)->type;
280 const int code = icmp_hdr(skb)->code;
281 int rel_type = 0;
282 int rel_code = 0;
283 int rel_info = 0;
284 struct sk_buff *skb2;
285 struct rt6_info *rt6i;
286
287 if (len < hlen + sizeof(struct ipv6hdr))
288 return;
289 iph6 = (struct ipv6hdr*)(dp + hlen);
290
291 switch (type) {
292 default:
293 return;
294 case ICMP_PARAMETERPROB:
295 if (icmp_hdr(skb)->un.gateway < hlen)
296 return;
297
298 /* So... This guy found something strange INSIDE encapsulated
299 packet. Well, he is fool, but what can we do ?
300 */
301 rel_type = ICMPV6_PARAMPROB;
302 rel_info = icmp_hdr(skb)->un.gateway - hlen;
303 break;
304
305 case ICMP_DEST_UNREACH:
306 switch (code) {
307 case ICMP_SR_FAILED:
308 case ICMP_PORT_UNREACH:
309 /* Impossible event. */
310 return;
311 case ICMP_FRAG_NEEDED:
312 /* Too complicated case ... */
313 return;
314 default:
315 /* All others are translated to HOST_UNREACH.
316 rfc2003 contains "deep thoughts" about NET_UNREACH,
317 I believe, it is just ether pollution. --ANK
318 */
319 rel_type = ICMPV6_DEST_UNREACH;
320 rel_code = ICMPV6_ADDR_UNREACH;
321 break;
322 }
323 break;
324 case ICMP_TIME_EXCEEDED:
325 if (code != ICMP_EXC_TTL)
326 return;
327 rel_type = ICMPV6_TIME_EXCEED;
328 rel_code = ICMPV6_EXC_HOPLIMIT;
329 break;
330 }
331
332 /* Prepare fake skb to feed it to icmpv6_send */
333 skb2 = skb_clone(skb, GFP_ATOMIC);
334 if (skb2 == NULL)
335 return 0;
336 dst_release(skb2->dst);
337 skb2->dst = NULL;
338 skb_pull(skb2, skb->data - (u8*)iph6);
339 skb_reset_network_header(skb2);
340
341 /* Try to guess incoming interface */
342 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
343 if (rt6i && rt6i->rt6i_dev) {
344 skb2->dev = rt6i->rt6i_dev;
345
346 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
347
348 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
349 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
350 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
351 rel_type = ICMPV6_DEST_UNREACH;
352 rel_code = ICMPV6_ADDR_UNREACH;
353 }
354 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
355 }
356 }
357 kfree_skb(skb2);
358 return 0;
359 #endif
360 }
361
362 static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
363 {
364 if (INET_ECN_is_ce(iph->tos))
365 IP6_ECN_set_ce(ipv6_hdr(skb));
366 }
367
368 /* ISATAP (RFC4214) - check source address */
369 static int
370 isatap_srcok(struct sk_buff *skb, struct iphdr *iph, struct net_device *dev)
371 {
372 struct neighbour *neigh;
373 struct dst_entry *dst;
374 struct rt6_info *rt;
375 struct flowi fl;
376 struct in6_addr *addr6;
377 struct in6_addr rtr;
378 struct ipv6hdr *iph6;
379 int ok = 0;
380
381 /* from onlink default router */
382 ipv6_addr_set(&rtr, htonl(0xFE800000), 0, 0, 0);
383 ipv6_isatap_eui64(rtr.s6_addr + 8, iph->saddr);
384 if ((rt = rt6_get_dflt_router(&rtr, dev))) {
385 dst_release(&rt->u.dst);
386 return 1;
387 }
388
389 iph6 = ipv6_hdr(skb);
390 memset(&fl, 0, sizeof(fl));
391 fl.proto = iph6->nexthdr;
392 ipv6_addr_copy(&fl.fl6_dst, &iph6->saddr);
393 fl.oif = dev->ifindex;
394 security_skb_classify_flow(skb, &fl);
395
396 dst = ip6_route_output(NULL, &fl);
397 if (!dst->error && (dst->dev == dev) && (neigh = dst->neighbour)) {
398
399 addr6 = (struct in6_addr*)&neigh->primary_key;
400
401 /* from correct previous hop */
402 if (ipv6_addr_is_isatap(addr6) &&
403 (addr6->s6_addr32[3] == iph->saddr))
404 ok = 1;
405 }
406 dst_release(dst);
407 return ok;
408 }
409
410 static int ipip6_rcv(struct sk_buff *skb)
411 {
412 struct iphdr *iph;
413 struct ip_tunnel *tunnel;
414
415 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
416 goto out;
417
418 iph = ip_hdr(skb);
419
420 read_lock(&ipip6_lock);
421 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
422 secpath_reset(skb);
423 skb->mac_header = skb->network_header;
424 skb_reset_network_header(skb);
425 IPCB(skb)->flags = 0;
426 skb->protocol = htons(ETH_P_IPV6);
427 skb->pkt_type = PACKET_HOST;
428
429 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
430 !isatap_srcok(skb, iph, tunnel->dev)) {
431 tunnel->stat.rx_errors++;
432 read_unlock(&ipip6_lock);
433 kfree_skb(skb);
434 return 0;
435 }
436 tunnel->stat.rx_packets++;
437 tunnel->stat.rx_bytes += skb->len;
438 skb->dev = tunnel->dev;
439 dst_release(skb->dst);
440 skb->dst = NULL;
441 nf_reset(skb);
442 ipip6_ecn_decapsulate(iph, skb);
443 netif_rx(skb);
444 read_unlock(&ipip6_lock);
445 return 0;
446 }
447
448 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
449 kfree_skb(skb);
450 read_unlock(&ipip6_lock);
451 out:
452 return 0;
453 }
454
455 /* Returns the embedded IPv4 address if the IPv6 address
456 comes from 6to4 (RFC 3056) addr space */
457
458 static inline __be32 try_6to4(struct in6_addr *v6dst)
459 {
460 __be32 dst = 0;
461
462 if (v6dst->s6_addr16[0] == htons(0x2002)) {
463 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
464 memcpy(&dst, &v6dst->s6_addr16[1], 4);
465 }
466 return dst;
467 }
468
469 /*
470 * This function assumes it is being called from dev_queue_xmit()
471 * and that skb is filled properly by that function.
472 */
473
474 static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
475 {
476 struct ip_tunnel *tunnel = netdev_priv(dev);
477 struct net_device_stats *stats = &tunnel->stat;
478 struct iphdr *tiph = &tunnel->parms.iph;
479 struct ipv6hdr *iph6 = ipv6_hdr(skb);
480 u8 tos = tunnel->parms.iph.tos;
481 struct rtable *rt; /* Route to the other host */
482 struct net_device *tdev; /* Device to other host */
483 struct iphdr *iph; /* Our new IP header */
484 unsigned int max_headroom; /* The extra header space needed */
485 __be32 dst = tiph->daddr;
486 int mtu;
487 struct in6_addr *addr6;
488 int addr_type;
489
490 if (tunnel->recursion++) {
491 tunnel->stat.collisions++;
492 goto tx_error;
493 }
494
495 if (skb->protocol != htons(ETH_P_IPV6))
496 goto tx_error;
497
498 /* ISATAP (RFC4214) - must come before 6to4 */
499 if (dev->priv_flags & IFF_ISATAP) {
500 struct neighbour *neigh = NULL;
501
502 if (skb->dst)
503 neigh = skb->dst->neighbour;
504
505 if (neigh == NULL) {
506 if (net_ratelimit())
507 printk(KERN_DEBUG "sit: nexthop == NULL\n");
508 goto tx_error;
509 }
510
511 addr6 = (struct in6_addr*)&neigh->primary_key;
512 addr_type = ipv6_addr_type(addr6);
513
514 if ((addr_type & IPV6_ADDR_UNICAST) &&
515 ipv6_addr_is_isatap(addr6))
516 dst = addr6->s6_addr32[3];
517 else
518 goto tx_error;
519 }
520
521 if (!dst)
522 dst = try_6to4(&iph6->daddr);
523
524 if (!dst) {
525 struct neighbour *neigh = NULL;
526
527 if (skb->dst)
528 neigh = skb->dst->neighbour;
529
530 if (neigh == NULL) {
531 if (net_ratelimit())
532 printk(KERN_DEBUG "sit: nexthop == NULL\n");
533 goto tx_error;
534 }
535
536 addr6 = (struct in6_addr*)&neigh->primary_key;
537 addr_type = ipv6_addr_type(addr6);
538
539 if (addr_type == IPV6_ADDR_ANY) {
540 addr6 = &ipv6_hdr(skb)->daddr;
541 addr_type = ipv6_addr_type(addr6);
542 }
543
544 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
545 goto tx_error_icmp;
546
547 dst = addr6->s6_addr32[3];
548 }
549
550 {
551 struct flowi fl = { .nl_u = { .ip4_u =
552 { .daddr = dst,
553 .saddr = tiph->saddr,
554 .tos = RT_TOS(tos) } },
555 .oif = tunnel->parms.link,
556 .proto = IPPROTO_IPV6 };
557 if (ip_route_output_key(&init_net, &rt, &fl)) {
558 tunnel->stat.tx_carrier_errors++;
559 goto tx_error_icmp;
560 }
561 }
562 if (rt->rt_type != RTN_UNICAST) {
563 ip_rt_put(rt);
564 tunnel->stat.tx_carrier_errors++;
565 goto tx_error_icmp;
566 }
567 tdev = rt->u.dst.dev;
568
569 if (tdev == dev) {
570 ip_rt_put(rt);
571 tunnel->stat.collisions++;
572 goto tx_error;
573 }
574
575 if (tiph->frag_off)
576 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
577 else
578 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
579
580 if (mtu < 68) {
581 tunnel->stat.collisions++;
582 ip_rt_put(rt);
583 goto tx_error;
584 }
585 if (mtu < IPV6_MIN_MTU)
586 mtu = IPV6_MIN_MTU;
587 if (tunnel->parms.iph.daddr && skb->dst)
588 skb->dst->ops->update_pmtu(skb->dst, mtu);
589
590 if (skb->len > mtu) {
591 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
592 ip_rt_put(rt);
593 goto tx_error;
594 }
595
596 if (tunnel->err_count > 0) {
597 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
598 tunnel->err_count--;
599 dst_link_failure(skb);
600 } else
601 tunnel->err_count = 0;
602 }
603
604 /*
605 * Okay, now see if we can stuff it in the buffer as-is.
606 */
607 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
608
609 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
610 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
611 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
612 if (!new_skb) {
613 ip_rt_put(rt);
614 stats->tx_dropped++;
615 dev_kfree_skb(skb);
616 tunnel->recursion--;
617 return 0;
618 }
619 if (skb->sk)
620 skb_set_owner_w(new_skb, skb->sk);
621 dev_kfree_skb(skb);
622 skb = new_skb;
623 iph6 = ipv6_hdr(skb);
624 }
625
626 skb->transport_header = skb->network_header;
627 skb_push(skb, sizeof(struct iphdr));
628 skb_reset_network_header(skb);
629 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
630 IPCB(skb)->flags = 0;
631 dst_release(skb->dst);
632 skb->dst = &rt->u.dst;
633
634 /*
635 * Push down and install the IPIP header.
636 */
637
638 iph = ip_hdr(skb);
639 iph->version = 4;
640 iph->ihl = sizeof(struct iphdr)>>2;
641 if (mtu > IPV6_MIN_MTU)
642 iph->frag_off = htons(IP_DF);
643 else
644 iph->frag_off = 0;
645
646 iph->protocol = IPPROTO_IPV6;
647 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
648 iph->daddr = rt->rt_dst;
649 iph->saddr = rt->rt_src;
650
651 if ((iph->ttl = tiph->ttl) == 0)
652 iph->ttl = iph6->hop_limit;
653
654 nf_reset(skb);
655
656 IPTUNNEL_XMIT();
657 tunnel->recursion--;
658 return 0;
659
660 tx_error_icmp:
661 dst_link_failure(skb);
662 tx_error:
663 stats->tx_errors++;
664 dev_kfree_skb(skb);
665 tunnel->recursion--;
666 return 0;
667 }
668
669 static void ipip6_tunnel_bind_dev(struct net_device *dev)
670 {
671 struct net_device *tdev = NULL;
672 struct ip_tunnel *tunnel;
673 struct iphdr *iph;
674
675 tunnel = netdev_priv(dev);
676 iph = &tunnel->parms.iph;
677
678 if (iph->daddr) {
679 struct flowi fl = { .nl_u = { .ip4_u =
680 { .daddr = iph->daddr,
681 .saddr = iph->saddr,
682 .tos = RT_TOS(iph->tos) } },
683 .oif = tunnel->parms.link,
684 .proto = IPPROTO_IPV6 };
685 struct rtable *rt;
686 if (!ip_route_output_key(&init_net, &rt, &fl)) {
687 tdev = rt->u.dst.dev;
688 ip_rt_put(rt);
689 }
690 dev->flags |= IFF_POINTOPOINT;
691 }
692
693 if (!tdev && tunnel->parms.link)
694 tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
695
696 if (tdev) {
697 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
698 dev->mtu = tdev->mtu - sizeof(struct iphdr);
699 if (dev->mtu < IPV6_MIN_MTU)
700 dev->mtu = IPV6_MIN_MTU;
701 }
702 dev->iflink = tunnel->parms.link;
703 }
704
705 static int
706 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
707 {
708 int err = 0;
709 struct ip_tunnel_parm p;
710 struct ip_tunnel *t;
711
712 switch (cmd) {
713 case SIOCGETTUNNEL:
714 t = NULL;
715 if (dev == ipip6_fb_tunnel_dev) {
716 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
717 err = -EFAULT;
718 break;
719 }
720 t = ipip6_tunnel_locate(&p, 0);
721 }
722 if (t == NULL)
723 t = netdev_priv(dev);
724 memcpy(&p, &t->parms, sizeof(p));
725 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
726 err = -EFAULT;
727 break;
728
729 case SIOCADDTUNNEL:
730 case SIOCCHGTUNNEL:
731 err = -EPERM;
732 if (!capable(CAP_NET_ADMIN))
733 goto done;
734
735 err = -EFAULT;
736 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
737 goto done;
738
739 err = -EINVAL;
740 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
741 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
742 goto done;
743 if (p.iph.ttl)
744 p.iph.frag_off |= htons(IP_DF);
745
746 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
747
748 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
749 if (t != NULL) {
750 if (t->dev != dev) {
751 err = -EEXIST;
752 break;
753 }
754 } else {
755 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
756 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
757 err = -EINVAL;
758 break;
759 }
760 t = netdev_priv(dev);
761 ipip6_tunnel_unlink(t);
762 t->parms.iph.saddr = p.iph.saddr;
763 t->parms.iph.daddr = p.iph.daddr;
764 memcpy(dev->dev_addr, &p.iph.saddr, 4);
765 memcpy(dev->broadcast, &p.iph.daddr, 4);
766 ipip6_tunnel_link(t);
767 netdev_state_change(dev);
768 }
769 }
770
771 if (t) {
772 err = 0;
773 if (cmd == SIOCCHGTUNNEL) {
774 t->parms.iph.ttl = p.iph.ttl;
775 t->parms.iph.tos = p.iph.tos;
776 if (t->parms.link != p.link) {
777 t->parms.link = p.link;
778 ipip6_tunnel_bind_dev(dev);
779 netdev_state_change(dev);
780 }
781 }
782 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
783 err = -EFAULT;
784 } else
785 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
786 break;
787
788 case SIOCDELTUNNEL:
789 err = -EPERM;
790 if (!capable(CAP_NET_ADMIN))
791 goto done;
792
793 if (dev == ipip6_fb_tunnel_dev) {
794 err = -EFAULT;
795 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
796 goto done;
797 err = -ENOENT;
798 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
799 goto done;
800 err = -EPERM;
801 if (t == netdev_priv(ipip6_fb_tunnel_dev))
802 goto done;
803 dev = t->dev;
804 }
805 unregister_netdevice(dev);
806 err = 0;
807 break;
808
809 default:
810 err = -EINVAL;
811 }
812
813 done:
814 return err;
815 }
816
817 static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
818 {
819 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
820 }
821
822 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
823 {
824 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
825 return -EINVAL;
826 dev->mtu = new_mtu;
827 return 0;
828 }
829
830 static void ipip6_tunnel_setup(struct net_device *dev)
831 {
832 dev->uninit = ipip6_tunnel_uninit;
833 dev->destructor = free_netdev;
834 dev->hard_start_xmit = ipip6_tunnel_xmit;
835 dev->get_stats = ipip6_tunnel_get_stats;
836 dev->do_ioctl = ipip6_tunnel_ioctl;
837 dev->change_mtu = ipip6_tunnel_change_mtu;
838
839 dev->type = ARPHRD_SIT;
840 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
841 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
842 dev->flags = IFF_NOARP;
843 dev->iflink = 0;
844 dev->addr_len = 4;
845 }
846
847 static int ipip6_tunnel_init(struct net_device *dev)
848 {
849 struct ip_tunnel *tunnel;
850
851 tunnel = netdev_priv(dev);
852
853 tunnel->dev = dev;
854 strcpy(tunnel->parms.name, dev->name);
855
856 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
857 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
858
859 ipip6_tunnel_bind_dev(dev);
860
861 return 0;
862 }
863
864 static int __init ipip6_fb_tunnel_init(struct net_device *dev)
865 {
866 struct ip_tunnel *tunnel = netdev_priv(dev);
867 struct iphdr *iph = &tunnel->parms.iph;
868
869 tunnel->dev = dev;
870 strcpy(tunnel->parms.name, dev->name);
871
872 iph->version = 4;
873 iph->protocol = IPPROTO_IPV6;
874 iph->ihl = 5;
875 iph->ttl = 64;
876
877 dev_hold(dev);
878 tunnels_wc[0] = tunnel;
879 return 0;
880 }
881
882 static struct xfrm_tunnel sit_handler = {
883 .handler = ipip6_rcv,
884 .err_handler = ipip6_err,
885 .priority = 1,
886 };
887
888 static void __exit sit_destroy_tunnels(void)
889 {
890 int prio;
891
892 for (prio = 1; prio < 4; prio++) {
893 int h;
894 for (h = 0; h < HASH_SIZE; h++) {
895 struct ip_tunnel *t;
896 while ((t = tunnels[prio][h]) != NULL)
897 unregister_netdevice(t->dev);
898 }
899 }
900 }
901
902 static void __exit sit_cleanup(void)
903 {
904 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
905
906 rtnl_lock();
907 sit_destroy_tunnels();
908 unregister_netdevice(ipip6_fb_tunnel_dev);
909 rtnl_unlock();
910 }
911
912 static int __init sit_init(void)
913 {
914 int err;
915
916 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
917
918 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
919 printk(KERN_INFO "sit init: Can't add protocol\n");
920 return -EAGAIN;
921 }
922
923 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
924 ipip6_tunnel_setup);
925 if (!ipip6_fb_tunnel_dev) {
926 err = -ENOMEM;
927 goto err1;
928 }
929
930 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
931
932 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
933 goto err2;
934
935 out:
936 return err;
937 err2:
938 free_netdev(ipip6_fb_tunnel_dev);
939 err1:
940 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
941 goto out;
942 }
943
944 module_init(sit_init);
945 module_exit(sit_cleanup);
946 MODULE_LICENSE("GPL");
947 MODULE_ALIAS("sit0");
This page took 0.08924 seconds and 5 git commands to generate.