]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv6/sit.c
[SK_BUFF]: Introduce skb_network_header_len
[net-next-2.6.git] / net / ipv6 / sit.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
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
1da177e4 21#include <linux/module.h>
4fc268d2 22#include <linux/capability.h>
1da177e4
LT
23#include <linux/errno.h>
24#include <linux/types.h>
25#include <linux/socket.h>
26#include <linux/sockios.h>
1da177e4
LT
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>
46f25dff 35#include <linux/if_ether.h>
1da177e4
LT
36
37#include <net/sock.h>
38#include <net/snmp.h>
39
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>
54
55/*
56 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
57
58 For comments look at net/ipv4/ip_gre.c --ANK
59 */
60
61#define HASH_SIZE 16
e69a4adc 62#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
1da177e4
LT
63
64static int ipip6_fb_tunnel_init(struct net_device *dev);
65static int ipip6_tunnel_init(struct net_device *dev);
66static void ipip6_tunnel_setup(struct net_device *dev);
67
68static struct net_device *ipip6_fb_tunnel_dev;
69
70static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
71static struct ip_tunnel *tunnels_r[HASH_SIZE];
72static struct ip_tunnel *tunnels_l[HASH_SIZE];
73static struct ip_tunnel *tunnels_wc[1];
74static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
75
76static DEFINE_RWLOCK(ipip6_lock);
77
e69a4adc 78static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
1da177e4
LT
79{
80 unsigned h0 = HASH(remote);
81 unsigned h1 = HASH(local);
82 struct ip_tunnel *t;
83
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;
88 }
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;
92 }
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;
96 }
97 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
98 return t;
99 return NULL;
100}
101
102static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
103{
e69a4adc
AV
104 __be32 remote = t->parms.iph.daddr;
105 __be32 local = t->parms.iph.saddr;
1da177e4
LT
106 unsigned h = 0;
107 int prio = 0;
108
109 if (remote) {
110 prio |= 2;
111 h ^= HASH(remote);
112 }
113 if (local) {
114 prio |= 1;
115 h ^= HASH(local);
116 }
117 return &tunnels[prio][h];
118}
119
120static void ipip6_tunnel_unlink(struct ip_tunnel *t)
121{
122 struct ip_tunnel **tp;
123
124 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
125 if (t == *tp) {
126 write_lock_bh(&ipip6_lock);
127 *tp = t->next;
128 write_unlock_bh(&ipip6_lock);
129 break;
130 }
131 }
132}
133
134static void ipip6_tunnel_link(struct ip_tunnel *t)
135{
136 struct ip_tunnel **tp = ipip6_bucket(t);
137
138 t->next = *tp;
139 write_lock_bh(&ipip6_lock);
140 *tp = t;
141 write_unlock_bh(&ipip6_lock);
142}
143
144static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
145{
e69a4adc
AV
146 __be32 remote = parms->iph.daddr;
147 __be32 local = parms->iph.saddr;
1da177e4
LT
148 struct ip_tunnel *t, **tp, *nt;
149 struct net_device *dev;
150 unsigned h = 0;
151 int prio = 0;
152 char name[IFNAMSIZ];
153
154 if (remote) {
155 prio |= 2;
156 h ^= HASH(remote);
157 }
158 if (local) {
159 prio |= 1;
160 h ^= HASH(local);
161 }
162 for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
163 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
164 return t;
165 }
166 if (!create)
167 goto failed;
168
169 if (parms->name[0])
170 strlcpy(name, parms->name, IFNAMSIZ);
171 else {
172 int i;
173 for (i=1; i<100; i++) {
174 sprintf(name, "sit%d", i);
175 if (__dev_get_by_name(name) == NULL)
176 break;
177 }
178 if (i==100)
179 goto failed;
180 }
181
182 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
183 if (dev == NULL)
184 return NULL;
185
2941a486 186 nt = netdev_priv(dev);
1da177e4
LT
187 dev->init = ipip6_tunnel_init;
188 nt->parms = *parms;
189
190 if (register_netdevice(dev) < 0) {
191 free_netdev(dev);
192 goto failed;
193 }
194
195 dev_hold(dev);
196
197 ipip6_tunnel_link(nt);
1da177e4
LT
198 return nt;
199
200failed:
201 return NULL;
202}
203
204static void ipip6_tunnel_uninit(struct net_device *dev)
205{
206 if (dev == ipip6_fb_tunnel_dev) {
207 write_lock_bh(&ipip6_lock);
208 tunnels_wc[0] = NULL;
209 write_unlock_bh(&ipip6_lock);
210 dev_put(dev);
211 } else {
2941a486 212 ipip6_tunnel_unlink(netdev_priv(dev));
1da177e4
LT
213 dev_put(dev);
214 }
215}
216
217
c73cb5a2 218static int ipip6_err(struct sk_buff *skb, u32 info)
1da177e4
LT
219{
220#ifndef I_WISH_WORLD_WERE_PERFECT
221
222/* It is not :-( All the routers (except for Linux) return only
223 8 bytes of packet payload. It means, that precise relaying of
224 ICMP in the real Internet is absolutely infeasible.
225 */
226 struct iphdr *iph = (struct iphdr*)skb->data;
88c7664f
ACM
227 const int type = icmp_hdr(skb)->type;
228 const int code = icmp_hdr(skb)->code;
1da177e4 229 struct ip_tunnel *t;
c73cb5a2 230 int err;
1da177e4
LT
231
232 switch (type) {
233 default:
234 case ICMP_PARAMETERPROB:
c73cb5a2 235 return 0;
1da177e4
LT
236
237 case ICMP_DEST_UNREACH:
238 switch (code) {
239 case ICMP_SR_FAILED:
240 case ICMP_PORT_UNREACH:
241 /* Impossible event. */
c73cb5a2 242 return 0;
1da177e4
LT
243 case ICMP_FRAG_NEEDED:
244 /* Soft state for pmtu is maintained by IP core. */
c73cb5a2 245 return 0;
1da177e4
LT
246 default:
247 /* All others are translated to HOST_UNREACH.
248 rfc2003 contains "deep thoughts" about NET_UNREACH,
249 I believe they are just ether pollution. --ANK
250 */
251 break;
252 }
253 break;
254 case ICMP_TIME_EXCEEDED:
255 if (code != ICMP_EXC_TTL)
c73cb5a2 256 return 0;
1da177e4
LT
257 break;
258 }
259
c73cb5a2
KM
260 err = -ENOENT;
261
1da177e4
LT
262 read_lock(&ipip6_lock);
263 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
264 if (t == NULL || t->parms.iph.daddr == 0)
265 goto out;
c73cb5a2
KM
266
267 err = 0;
1da177e4
LT
268 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
269 goto out;
270
271 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
272 t->err_count++;
273 else
274 t->err_count = 1;
275 t->err_time = jiffies;
276out:
277 read_unlock(&ipip6_lock);
c73cb5a2 278 return err;
1da177e4
LT
279#else
280 struct iphdr *iph = (struct iphdr*)dp;
281 int hlen = iph->ihl<<2;
282 struct ipv6hdr *iph6;
88c7664f
ACM
283 const int type = icmp_hdr(skb)->type;
284 const int code = icmp_hdr(skb)->code;
1da177e4
LT
285 int rel_type = 0;
286 int rel_code = 0;
287 int rel_info = 0;
288 struct sk_buff *skb2;
289 struct rt6_info *rt6i;
290
291 if (len < hlen + sizeof(struct ipv6hdr))
292 return;
293 iph6 = (struct ipv6hdr*)(dp + hlen);
294
295 switch (type) {
296 default:
297 return;
298 case ICMP_PARAMETERPROB:
88c7664f 299 if (icmp_hdr(skb)->un.gateway < hlen)
1da177e4
LT
300 return;
301
302 /* So... This guy found something strange INSIDE encapsulated
303 packet. Well, he is fool, but what can we do ?
304 */
305 rel_type = ICMPV6_PARAMPROB;
88c7664f 306 rel_info = icmp_hdr(skb)->un.gateway - hlen;
1da177e4
LT
307 break;
308
309 case ICMP_DEST_UNREACH:
310 switch (code) {
311 case ICMP_SR_FAILED:
312 case ICMP_PORT_UNREACH:
313 /* Impossible event. */
314 return;
315 case ICMP_FRAG_NEEDED:
316 /* Too complicated case ... */
317 return;
318 default:
319 /* All others are translated to HOST_UNREACH.
320 rfc2003 contains "deep thoughts" about NET_UNREACH,
321 I believe, it is just ether pollution. --ANK
322 */
323 rel_type = ICMPV6_DEST_UNREACH;
324 rel_code = ICMPV6_ADDR_UNREACH;
325 break;
326 }
327 break;
328 case ICMP_TIME_EXCEEDED:
329 if (code != ICMP_EXC_TTL)
330 return;
331 rel_type = ICMPV6_TIME_EXCEED;
332 rel_code = ICMPV6_EXC_HOPLIMIT;
333 break;
334 }
335
336 /* Prepare fake skb to feed it to icmpv6_send */
337 skb2 = skb_clone(skb, GFP_ATOMIC);
338 if (skb2 == NULL)
c73cb5a2 339 return 0;
1da177e4
LT
340 dst_release(skb2->dst);
341 skb2->dst = NULL;
342 skb_pull(skb2, skb->data - (u8*)iph6);
c1d2bbe1 343 skb_reset_network_header(skb2);
1da177e4
LT
344
345 /* Try to guess incoming interface */
346 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
347 if (rt6i && rt6i->rt6i_dev) {
348 skb2->dev = rt6i->rt6i_dev;
349
350 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
351
352 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
2941a486 353 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
1da177e4
LT
354 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
355 rel_type = ICMPV6_DEST_UNREACH;
356 rel_code = ICMPV6_ADDR_UNREACH;
357 }
358 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
359 }
360 }
361 kfree_skb(skb2);
c73cb5a2 362 return 0;
1da177e4
LT
363#endif
364}
365
366static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
367{
368 if (INET_ECN_is_ce(iph->tos))
0660e03f 369 IP6_ECN_set_ce(ipv6_hdr(skb));
1da177e4
LT
370}
371
372static int ipip6_rcv(struct sk_buff *skb)
373{
374 struct iphdr *iph;
375 struct ip_tunnel *tunnel;
376
377 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
378 goto out;
379
eddc9ec5 380 iph = ip_hdr(skb);
1da177e4
LT
381
382 read_lock(&ipip6_lock);
383 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
384 secpath_reset(skb);
385 skb->mac.raw = skb->nh.raw;
c1d2bbe1 386 skb_reset_network_header(skb);
8cdfab8a 387 IPCB(skb)->flags = 0;
1da177e4
LT
388 skb->protocol = htons(ETH_P_IPV6);
389 skb->pkt_type = PACKET_HOST;
390 tunnel->stat.rx_packets++;
391 tunnel->stat.rx_bytes += skb->len;
392 skb->dev = tunnel->dev;
393 dst_release(skb->dst);
394 skb->dst = NULL;
395 nf_reset(skb);
396 ipip6_ecn_decapsulate(iph, skb);
397 netif_rx(skb);
398 read_unlock(&ipip6_lock);
399 return 0;
400 }
401
45af08be 402 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1da177e4
LT
403 kfree_skb(skb);
404 read_unlock(&ipip6_lock);
405out:
406 return 0;
407}
408
409/* Returns the embedded IPv4 address if the IPv6 address
410 comes from 6to4 (RFC 3056) addr space */
411
e69a4adc 412static inline __be32 try_6to4(struct in6_addr *v6dst)
1da177e4 413{
e69a4adc 414 __be32 dst = 0;
1da177e4
LT
415
416 if (v6dst->s6_addr16[0] == htons(0x2002)) {
1ab1457c 417 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
1da177e4
LT
418 memcpy(&dst, &v6dst->s6_addr16[1], 4);
419 }
420 return dst;
421}
422
423/*
424 * This function assumes it is being called from dev_queue_xmit()
425 * and that skb is filled properly by that function.
426 */
427
428static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
429{
2941a486 430 struct ip_tunnel *tunnel = netdev_priv(dev);
1da177e4
LT
431 struct net_device_stats *stats = &tunnel->stat;
432 struct iphdr *tiph = &tunnel->parms.iph;
0660e03f 433 struct ipv6hdr *iph6 = ipv6_hdr(skb);
1da177e4
LT
434 u8 tos = tunnel->parms.iph.tos;
435 struct rtable *rt; /* Route to the other host */
436 struct net_device *tdev; /* Device to other host */
437 struct iphdr *iph; /* Our new IP header */
438 int max_headroom; /* The extra header space needed */
e69a4adc 439 __be32 dst = tiph->daddr;
1da177e4 440 int mtu;
1ab1457c 441 struct in6_addr *addr6;
1da177e4
LT
442 int addr_type;
443
444 if (tunnel->recursion++) {
445 tunnel->stat.collisions++;
446 goto tx_error;
447 }
448
449 if (skb->protocol != htons(ETH_P_IPV6))
450 goto tx_error;
451
452 if (!dst)
453 dst = try_6to4(&iph6->daddr);
454
455 if (!dst) {
456 struct neighbour *neigh = NULL;
457
458 if (skb->dst)
459 neigh = skb->dst->neighbour;
460
461 if (neigh == NULL) {
462 if (net_ratelimit())
463 printk(KERN_DEBUG "sit: nexthop == NULL\n");
464 goto tx_error;
465 }
466
467 addr6 = (struct in6_addr*)&neigh->primary_key;
468 addr_type = ipv6_addr_type(addr6);
469
470 if (addr_type == IPV6_ADDR_ANY) {
0660e03f 471 addr6 = &ipv6_hdr(skb)->daddr;
1da177e4
LT
472 addr_type = ipv6_addr_type(addr6);
473 }
474
475 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
476 goto tx_error_icmp;
477
478 dst = addr6->s6_addr32[3];
479 }
480
481 {
482 struct flowi fl = { .nl_u = { .ip4_u =
483 { .daddr = dst,
484 .saddr = tiph->saddr,
485 .tos = RT_TOS(tos) } },
486 .oif = tunnel->parms.link,
487 .proto = IPPROTO_IPV6 };
488 if (ip_route_output_key(&rt, &fl)) {
489 tunnel->stat.tx_carrier_errors++;
490 goto tx_error_icmp;
491 }
492 }
493 if (rt->rt_type != RTN_UNICAST) {
494 ip_rt_put(rt);
495 tunnel->stat.tx_carrier_errors++;
496 goto tx_error_icmp;
497 }
498 tdev = rt->u.dst.dev;
499
500 if (tdev == dev) {
501 ip_rt_put(rt);
502 tunnel->stat.collisions++;
503 goto tx_error;
504 }
505
506 if (tiph->frag_off)
507 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
508 else
509 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
510
511 if (mtu < 68) {
512 tunnel->stat.collisions++;
513 ip_rt_put(rt);
514 goto tx_error;
515 }
516 if (mtu < IPV6_MIN_MTU)
517 mtu = IPV6_MIN_MTU;
518 if (tunnel->parms.iph.daddr && skb->dst)
519 skb->dst->ops->update_pmtu(skb->dst, mtu);
520
521 if (skb->len > mtu) {
522 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
523 ip_rt_put(rt);
524 goto tx_error;
525 }
526
527 if (tunnel->err_count > 0) {
528 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
529 tunnel->err_count--;
530 dst_link_failure(skb);
531 } else
532 tunnel->err_count = 0;
533 }
534
535 /*
536 * Okay, now see if we can stuff it in the buffer as-is.
537 */
538 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
539
540 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
541 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
542 if (!new_skb) {
543 ip_rt_put(rt);
1ab1457c 544 stats->tx_dropped++;
1da177e4
LT
545 dev_kfree_skb(skb);
546 tunnel->recursion--;
547 return 0;
548 }
549 if (skb->sk)
550 skb_set_owner_w(new_skb, skb->sk);
551 dev_kfree_skb(skb);
552 skb = new_skb;
0660e03f 553 iph6 = ipv6_hdr(skb);
1da177e4
LT
554 }
555
556 skb->h.raw = skb->nh.raw;
e2d1bca7
ACM
557 skb_push(skb, sizeof(struct iphdr));
558 skb_reset_network_header(skb);
1da177e4 559 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
8cdfab8a 560 IPCB(skb)->flags = 0;
1da177e4
LT
561 dst_release(skb->dst);
562 skb->dst = &rt->u.dst;
563
564 /*
565 * Push down and install the IPIP header.
566 */
567
eddc9ec5 568 iph = ip_hdr(skb);
1da177e4
LT
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
590tx_error_icmp:
591 dst_link_failure(skb);
592tx_error:
593 stats->tx_errors++;
594 dev_kfree_skb(skb);
595 tunnel->recursion--;
596 return 0;
597}
598
599static int
600ipip6_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)
2941a486 617 t = netdev_priv(dev);
1da177e4
LT
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 }
2941a486 654 t = netdev_priv(dev);
1da177e4
LT
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;
2941a486 690 if (t == netdev_priv(ipip6_fb_tunnel_dev))
1da177e4
LT
691 goto done;
692 dev = t->dev;
693 }
22f8cde5
SH
694 unregister_netdevice(dev);
695 err = 0;
1da177e4
LT
696 break;
697
698 default:
699 err = -EINVAL;
700 }
701
702done:
703 return err;
704}
705
706static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
707{
2941a486 708 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
1da177e4
LT
709}
710
711static 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
719static 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);
46f25dff 731 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
1da177e4
LT
732 dev->flags = IFF_NOARP;
733 dev->iflink = 0;
734 dev->addr_len = 4;
735}
736
737static 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
2941a486 743 tunnel = netdev_priv(dev);
1da177e4
LT
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
20380731 781static int __init ipip6_fb_tunnel_init(struct net_device *dev)
1da177e4 782{
2941a486 783 struct ip_tunnel *tunnel = netdev_priv(dev);
1da177e4
LT
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
c73cb5a2 799static struct xfrm_tunnel sit_handler = {
1da177e4
LT
800 .handler = ipip6_rcv,
801 .err_handler = ipip6_err,
c73cb5a2 802 .priority = 1,
1da177e4
LT
803};
804
db44575f
AK
805static 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
89c89458 819static void __exit sit_cleanup(void)
1da177e4 820{
c73cb5a2 821 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
db44575f
AK
822
823 rtnl_lock();
824 sit_destroy_tunnels();
825 unregister_netdevice(ipip6_fb_tunnel_dev);
826 rtnl_unlock();
1da177e4
LT
827}
828
89c89458 829static int __init sit_init(void)
1da177e4
LT
830{
831 int err;
832
833 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
834
c73cb5a2 835 if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
1da177e4
LT
836 printk(KERN_INFO "sit init: Can't add protocol\n");
837 return -EAGAIN;
838 }
839
1ab1457c 840 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1da177e4
LT
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:
c73cb5a2 857 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1da177e4
LT
858 goto out;
859}
989e5b96
JR
860
861module_init(sit_init);
862module_exit(sit_cleanup);
39c85086 863MODULE_LICENSE("GPL");
daccff02 864MODULE_ALIAS("sit0");