]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv6/sit.c
netdev/phy: Make get_phy_id() static and quit EXPORTing it.
[mirror_ubuntu-artful-kernel.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 *
1da177e4
LT
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Changes:
15 * Roger Venning <r.venning@telstra.com>: 6to4 support
16 * Nate Thompson <nate@thebog.net>: 6to4 support
fadf6bf0 17 * Fred Templin <fred.l.templin@boeing.com>: isatap support
1da177e4
LT
18 */
19
1da177e4 20#include <linux/module.h>
4fc268d2 21#include <linux/capability.h>
1da177e4
LT
22#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/socket.h>
25#include <linux/sockios.h>
1da177e4
LT
26#include <linux/net.h>
27#include <linux/in6.h>
28#include <linux/netdevice.h>
29#include <linux/if_arp.h>
30#include <linux/icmp.h>
5a0e3ad6 31#include <linux/slab.h>
1da177e4
LT
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>
8190d900
PE
54#include <net/net_namespace.h>
55#include <net/netns/generic.h>
1da177e4
LT
56
57/*
58 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
59
60 For comments look at net/ipv4/ip_gre.c --ANK
61 */
62
63#define HASH_SIZE 16
e69a4adc 64#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
1da177e4 65
15fc1f70 66static int ipip6_tunnel_init(struct net_device *dev);
1da177e4 67static void ipip6_tunnel_setup(struct net_device *dev);
15fc1f70 68static void ipip6_dev_free(struct net_device *dev);
1da177e4 69
f99189b1 70static int sit_net_id __read_mostly;
8190d900 71struct sit_net {
3a43be3c
ED
72 struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
73 struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
74 struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
75 struct ip_tunnel __rcu *tunnels_wc[1];
76 struct ip_tunnel __rcu **tunnels[4];
29182176 77
cd3dbc19 78 struct net_device *fb_tunnel_dev;
8190d900
PE
79};
80
4543c10d 81/*
3a43be3c 82 * Locking : hash tables are protected by RCU and RTNL
4543c10d 83 */
4543c10d
ED
84
85#define for_each_ip_tunnel_rcu(start) \
86 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
1da177e4 87
15fc1f70
ED
88/* often modified stats are per cpu, other are shared (netdev->stats) */
89struct pcpu_tstats {
87b6d218 90 u64 rx_packets;
91 u64 rx_bytes;
92 u64 tx_packets;
93 u64 tx_bytes;
94 struct u64_stats_sync syncp;
95};
15fc1f70 96
87b6d218 97static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
98 struct rtnl_link_stats64 *tot)
15fc1f70 99{
15fc1f70
ED
100 int i;
101
102 for_each_possible_cpu(i) {
103 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
87b6d218 104 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
105 unsigned int start;
106
107 do {
108 start = u64_stats_fetch_begin_bh(&tstats->syncp);
109 rx_packets = tstats->rx_packets;
110 tx_packets = tstats->tx_packets;
111 rx_bytes = tstats->rx_bytes;
112 tx_bytes = tstats->tx_bytes;
113 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
114
115 tot->rx_packets += rx_packets;
116 tot->tx_packets += tx_packets;
117 tot->rx_bytes += rx_bytes;
118 tot->tx_bytes += tx_bytes;
15fc1f70 119 }
87b6d218 120
121 tot->rx_errors = dev->stats.rx_errors;
122 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
123 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
124 tot->tx_dropped = dev->stats.tx_dropped;
125 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
126 tot->tx_errors = dev->stats.tx_errors;
127
128 return tot;
15fc1f70 129}
87b6d218 130
4543c10d
ED
131/*
132 * Must be invoked with rcu_read_lock
133 */
3e866703 134static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
4fddbf5d 135 struct net_device *dev, __be32 remote, __be32 local)
1da177e4 136{
3a43be3c
ED
137 unsigned int h0 = HASH(remote);
138 unsigned int h1 = HASH(local);
1da177e4 139 struct ip_tunnel *t;
29182176 140 struct sit_net *sitn = net_generic(net, sit_net_id);
1da177e4 141
4543c10d 142 for_each_ip_tunnel_rcu(sitn->tunnels_r_l[h0 ^ h1]) {
1da177e4 143 if (local == t->parms.iph.saddr &&
4fddbf5d
SH
144 remote == t->parms.iph.daddr &&
145 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
146 (t->dev->flags & IFF_UP))
1da177e4
LT
147 return t;
148 }
4543c10d 149 for_each_ip_tunnel_rcu(sitn->tunnels_r[h0]) {
4fddbf5d
SH
150 if (remote == t->parms.iph.daddr &&
151 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
152 (t->dev->flags & IFF_UP))
1da177e4
LT
153 return t;
154 }
4543c10d 155 for_each_ip_tunnel_rcu(sitn->tunnels_l[h1]) {
4fddbf5d
SH
156 if (local == t->parms.iph.saddr &&
157 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
158 (t->dev->flags & IFF_UP))
1da177e4
LT
159 return t;
160 }
4543c10d 161 t = rcu_dereference(sitn->tunnels_wc[0]);
4fddbf5d 162 if ((t != NULL) && (t->dev->flags & IFF_UP))
1da177e4
LT
163 return t;
164 return NULL;
165}
166
3a43be3c 167static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
ca8def14 168 struct ip_tunnel_parm *parms)
1da177e4 169{
420fe234
YH
170 __be32 remote = parms->iph.daddr;
171 __be32 local = parms->iph.saddr;
3a43be3c 172 unsigned int h = 0;
1da177e4
LT
173 int prio = 0;
174
175 if (remote) {
176 prio |= 2;
177 h ^= HASH(remote);
178 }
179 if (local) {
180 prio |= 1;
181 h ^= HASH(local);
182 }
29182176 183 return &sitn->tunnels[prio][h];
1da177e4
LT
184}
185
3a43be3c 186static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
ca8def14 187 struct ip_tunnel *t)
420fe234 188{
ca8def14 189 return __ipip6_bucket(sitn, &t->parms);
420fe234
YH
190}
191
ca8def14 192static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
1da177e4 193{
3a43be3c
ED
194 struct ip_tunnel __rcu **tp;
195 struct ip_tunnel *iter;
196
197 for (tp = ipip6_bucket(sitn, t);
198 (iter = rtnl_dereference(*tp)) != NULL;
199 tp = &iter->next) {
200 if (t == iter) {
cf778b00 201 rcu_assign_pointer(*tp, t->next);
1da177e4
LT
202 break;
203 }
204 }
205}
206
ca8def14 207static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
1da177e4 208{
3a43be3c 209 struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
1da177e4 210
cf778b00
ED
211 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
212 rcu_assign_pointer(*tp, t);
1da177e4
LT
213}
214
e0c93948 215static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
fa857afc
YH
216{
217#ifdef CONFIG_IPV6_SIT_6RD
e0c93948
YH
218 struct ip_tunnel *t = netdev_priv(dev);
219
fa857afc
YH
220 if (t->dev == sitn->fb_tunnel_dev) {
221 ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
222 t->ip6rd.relay_prefix = 0;
223 t->ip6rd.prefixlen = 16;
224 t->ip6rd.relay_prefixlen = 0;
225 } else {
226 struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
227 memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
228 }
229#endif
230}
231
3a43be3c 232static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
ca8def14 233 struct ip_tunnel_parm *parms, int create)
1da177e4 234{
e69a4adc
AV
235 __be32 remote = parms->iph.daddr;
236 __be32 local = parms->iph.saddr;
3a43be3c
ED
237 struct ip_tunnel *t, *nt;
238 struct ip_tunnel __rcu **tp;
1da177e4 239 struct net_device *dev;
1da177e4 240 char name[IFNAMSIZ];
ca8def14 241 struct sit_net *sitn = net_generic(net, sit_net_id);
1da177e4 242
3a43be3c
ED
243 for (tp = __ipip6_bucket(sitn, parms);
244 (t = rtnl_dereference(*tp)) != NULL;
245 tp = &t->next) {
8db99e57 246 if (local == t->parms.iph.saddr &&
4fddbf5d
SH
247 remote == t->parms.iph.daddr &&
248 parms->link == t->parms.link) {
8db99e57
SH
249 if (create)
250 return NULL;
251 else
252 return t;
253 }
1da177e4
LT
254 }
255 if (!create)
256 goto failed;
257
258 if (parms->name[0])
259 strlcpy(name, parms->name, IFNAMSIZ);
34cc7ba6 260 else
15fc1f70 261 strcpy(name, "sit%d");
1da177e4
LT
262
263 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
264 if (dev == NULL)
265 return NULL;
266
7a97146c
PE
267 dev_net_set(dev, net);
268
2941a486 269 nt = netdev_priv(dev);
1326c3d5 270
1da177e4 271 nt->parms = *parms;
15fc1f70
ED
272 if (ipip6_tunnel_init(dev) < 0)
273 goto failed_free;
e0c93948 274 ipip6_tunnel_clone_6rd(dev, sitn);
1da177e4 275
c7dc89c0
FT
276 if (parms->i_flags & SIT_ISATAP)
277 dev->priv_flags |= IFF_ISATAP;
278
b37d428b
PE
279 if (register_netdevice(dev) < 0)
280 goto failed_free;
1da177e4 281
72b36015
TF
282 strcpy(nt->parms.name, dev->name);
283
1da177e4
LT
284 dev_hold(dev);
285
ca8def14 286 ipip6_tunnel_link(sitn, nt);
1da177e4
LT
287 return nt;
288
b37d428b 289failed_free:
15fc1f70 290 ipip6_dev_free(dev);
1da177e4
LT
291failed:
292 return NULL;
293}
294
ef9a9d11
ED
295#define for_each_prl_rcu(start) \
296 for (prl = rcu_dereference(start); \
297 prl; \
298 prl = rcu_dereference(prl->next))
299
fadf6bf0 300static struct ip_tunnel_prl_entry *
3fcfa129 301__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
fadf6bf0 302{
ef9a9d11 303 struct ip_tunnel_prl_entry *prl;
fadf6bf0 304
ef9a9d11
ED
305 for_each_prl_rcu(t->prl)
306 if (prl->addr == addr)
fadf6bf0 307 break;
ef9a9d11 308 return prl;
fadf6bf0
TF
309
310}
311
2b4743bd
YH
312static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
313 struct ip_tunnel_prl __user *a)
300aaeea 314{
2b4743bd 315 struct ip_tunnel_prl kprl, *kp;
300aaeea
YH
316 struct ip_tunnel_prl_entry *prl;
317 unsigned int cmax, c = 0, ca, len;
318 int ret = 0;
319
2b4743bd
YH
320 if (copy_from_user(&kprl, a, sizeof(kprl)))
321 return -EFAULT;
322 cmax = kprl.datalen / sizeof(kprl);
323 if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
300aaeea
YH
324 cmax = 1;
325
326 /* For simple GET or for root users,
327 * we try harder to allocate.
328 */
329 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
330 kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
331 NULL;
332
ef9a9d11 333 rcu_read_lock();
300aaeea
YH
334
335 ca = t->prl_count < cmax ? t->prl_count : cmax;
336
337 if (!kp) {
338 /* We don't try hard to allocate much memory for
339 * non-root users.
340 * For root users, retry allocating enough memory for
341 * the answer.
342 */
343 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
344 if (!kp) {
345 ret = -ENOMEM;
346 goto out;
347 }
348 }
349
350 c = 0;
ef9a9d11 351 for_each_prl_rcu(t->prl) {
298bf12d 352 if (c >= cmax)
300aaeea 353 break;
2b4743bd 354 if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
300aaeea
YH
355 continue;
356 kp[c].addr = prl->addr;
357 kp[c].flags = prl->flags;
358 c++;
2b4743bd 359 if (kprl.addr != htonl(INADDR_ANY))
300aaeea
YH
360 break;
361 }
362out:
ef9a9d11 363 rcu_read_unlock();
300aaeea
YH
364
365 len = sizeof(*kp) * c;
2b4743bd
YH
366 ret = 0;
367 if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
368 ret = -EFAULT;
300aaeea
YH
369
370 kfree(kp);
300aaeea 371
2b4743bd 372 return ret;
300aaeea
YH
373}
374
fadf6bf0
TF
375static int
376ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
377{
378 struct ip_tunnel_prl_entry *p;
3fcfa129
YH
379 int err = 0;
380
0009ae1f
YH
381 if (a->addr == htonl(INADDR_ANY))
382 return -EINVAL;
383
aac4dddc 384 ASSERT_RTNL();
fadf6bf0 385
3a43be3c 386 for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
300aaeea 387 if (p->addr == a->addr) {
ef9a9d11
ED
388 if (chg) {
389 p->flags = a->flags;
390 goto out;
391 }
3fcfa129
YH
392 err = -EEXIST;
393 goto out;
fadf6bf0
TF
394 }
395 }
396
3fcfa129
YH
397 if (chg) {
398 err = -ENXIO;
399 goto out;
400 }
fadf6bf0
TF
401
402 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
3fcfa129
YH
403 if (!p) {
404 err = -ENOBUFS;
405 goto out;
406 }
fadf6bf0 407
fadf6bf0 408 p->next = t->prl;
300aaeea
YH
409 p->addr = a->addr;
410 p->flags = a->flags;
ef9a9d11 411 t->prl_count++;
cf778b00 412 rcu_assign_pointer(t->prl, p);
3fcfa129 413out:
3fcfa129 414 return err;
fadf6bf0
TF
415}
416
ef9a9d11
ED
417static void prl_list_destroy_rcu(struct rcu_head *head)
418{
419 struct ip_tunnel_prl_entry *p, *n;
420
421 p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
422 do {
753ea8e9 423 n = rcu_dereference_protected(p->next, 1);
ef9a9d11
ED
424 kfree(p);
425 p = n;
426 } while (p);
427}
428
fadf6bf0
TF
429static int
430ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
431{
753ea8e9
ED
432 struct ip_tunnel_prl_entry *x;
433 struct ip_tunnel_prl_entry __rcu **p;
3fcfa129
YH
434 int err = 0;
435
aac4dddc 436 ASSERT_RTNL();
fadf6bf0 437
0009ae1f 438 if (a && a->addr != htonl(INADDR_ANY)) {
753ea8e9
ED
439 for (p = &t->prl;
440 (x = rtnl_dereference(*p)) != NULL;
441 p = &x->next) {
442 if (x->addr == a->addr) {
fadf6bf0 443 *p = x->next;
11c476f3 444 kfree_rcu(x, rcu_head);
300aaeea 445 t->prl_count--;
3fcfa129 446 goto out;
fadf6bf0
TF
447 }
448 }
3fcfa129 449 err = -ENXIO;
fadf6bf0 450 } else {
753ea8e9
ED
451 x = rtnl_dereference(t->prl);
452 if (x) {
ef9a9d11 453 t->prl_count = 0;
ef9a9d11
ED
454 call_rcu(&x->rcu_head, prl_list_destroy_rcu);
455 t->prl = NULL;
fadf6bf0
TF
456 }
457 }
3fcfa129 458out:
4b279601 459 return err;
fadf6bf0
TF
460}
461
fadf6bf0 462static int
b71d1d42 463isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
fadf6bf0 464{
3fcfa129 465 struct ip_tunnel_prl_entry *p;
fadf6bf0
TF
466 int ok = 1;
467
ef9a9d11 468 rcu_read_lock();
3fcfa129 469 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
fadf6bf0 470 if (p) {
300aaeea 471 if (p->flags & PRL_DEFAULT)
fadf6bf0
TF
472 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
473 else
474 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
475 } else {
b71d1d42
ED
476 const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
477
fadf6bf0
TF
478 if (ipv6_addr_is_isatap(addr6) &&
479 (addr6->s6_addr32[3] == iph->saddr) &&
52eeeb84 480 ipv6_chk_prefix(addr6, t->dev))
fadf6bf0
TF
481 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
482 else
483 ok = 0;
484 }
ef9a9d11 485 rcu_read_unlock();
fadf6bf0
TF
486 return ok;
487}
488
1da177e4
LT
489static void ipip6_tunnel_uninit(struct net_device *dev)
490{
ca8def14
PE
491 struct net *net = dev_net(dev);
492 struct sit_net *sitn = net_generic(net, sit_net_id);
493
cd3dbc19 494 if (dev == sitn->fb_tunnel_dev) {
a9b3cd7f 495 RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
1da177e4 496 } else {
ca8def14 497 ipip6_tunnel_unlink(sitn, netdev_priv(dev));
02e10b90 498 ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
1da177e4 499 }
3a43be3c 500 dev_put(dev);
1da177e4
LT
501}
502
503
c73cb5a2 504static int ipip6_err(struct sk_buff *skb, u32 info)
1da177e4 505{
1da177e4 506
071f92d0 507/* All the routers (except for Linux) return only
1da177e4
LT
508 8 bytes of packet payload. It means, that precise relaying of
509 ICMP in the real Internet is absolutely infeasible.
510 */
b71d1d42 511 const struct iphdr *iph = (const struct iphdr *)skb->data;
88c7664f
ACM
512 const int type = icmp_hdr(skb)->type;
513 const int code = icmp_hdr(skb)->code;
1da177e4 514 struct ip_tunnel *t;
c73cb5a2 515 int err;
1da177e4
LT
516
517 switch (type) {
518 default:
519 case ICMP_PARAMETERPROB:
c73cb5a2 520 return 0;
1da177e4
LT
521
522 case ICMP_DEST_UNREACH:
523 switch (code) {
524 case ICMP_SR_FAILED:
525 case ICMP_PORT_UNREACH:
526 /* Impossible event. */
c73cb5a2 527 return 0;
1da177e4
LT
528 case ICMP_FRAG_NEEDED:
529 /* Soft state for pmtu is maintained by IP core. */
c73cb5a2 530 return 0;
1da177e4
LT
531 default:
532 /* All others are translated to HOST_UNREACH.
533 rfc2003 contains "deep thoughts" about NET_UNREACH,
534 I believe they are just ether pollution. --ANK
535 */
536 break;
537 }
538 break;
539 case ICMP_TIME_EXCEEDED:
540 if (code != ICMP_EXC_TTL)
c73cb5a2 541 return 0;
1da177e4
LT
542 break;
543 }
544
c73cb5a2
KM
545 err = -ENOENT;
546
4543c10d 547 rcu_read_lock();
4fddbf5d
SH
548 t = ipip6_tunnel_lookup(dev_net(skb->dev),
549 skb->dev,
550 iph->daddr,
551 iph->saddr);
1da177e4
LT
552 if (t == NULL || t->parms.iph.daddr == 0)
553 goto out;
c73cb5a2
KM
554
555 err = 0;
1da177e4
LT
556 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
557 goto out;
558
bb80087a 559 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
1da177e4
LT
560 t->err_count++;
561 else
562 t->err_count = 1;
563 t->err_time = jiffies;
564out:
4543c10d 565 rcu_read_unlock();
c73cb5a2 566 return err;
1da177e4
LT
567}
568
b71d1d42 569static inline void ipip6_ecn_decapsulate(const struct iphdr *iph, struct sk_buff *skb)
1da177e4
LT
570{
571 if (INET_ECN_is_ce(iph->tos))
0660e03f 572 IP6_ECN_set_ce(ipv6_hdr(skb));
1da177e4
LT
573}
574
575static int ipip6_rcv(struct sk_buff *skb)
576{
b71d1d42 577 const struct iphdr *iph;
1da177e4
LT
578 struct ip_tunnel *tunnel;
579
580 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
581 goto out;
582
eddc9ec5 583 iph = ip_hdr(skb);
1da177e4 584
4543c10d 585 rcu_read_lock();
4fddbf5d
SH
586 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
587 iph->saddr, iph->daddr);
588 if (tunnel != NULL) {
15fc1f70
ED
589 struct pcpu_tstats *tstats;
590
1da177e4 591 secpath_reset(skb);
b0e380b1 592 skb->mac_header = skb->network_header;
c1d2bbe1 593 skb_reset_network_header(skb);
8cdfab8a 594 IPCB(skb)->flags = 0;
1da177e4
LT
595 skb->protocol = htons(ETH_P_IPV6);
596 skb->pkt_type = PACKET_HOST;
c7dc89c0
FT
597
598 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
fadf6bf0 599 !isatap_chksrc(skb, iph, tunnel)) {
4eecc107 600 tunnel->dev->stats.rx_errors++;
4543c10d 601 rcu_read_unlock();
c7dc89c0
FT
602 kfree_skb(skb);
603 return 0;
604 }
d19d56dd 605
15fc1f70
ED
606 tstats = this_cpu_ptr(tunnel->dev->tstats);
607 tstats->rx_packets++;
608 tstats->rx_bytes += skb->len;
609
610 __skb_tunnel_rx(skb, tunnel->dev);
d19d56dd 611
1da177e4 612 ipip6_ecn_decapsulate(iph, skb);
8990f468 613
caf586e5 614 netif_rx(skb);
8990f468 615
4543c10d 616 rcu_read_unlock();
1da177e4
LT
617 return 0;
618 }
619
6dcdd1b3 620 /* no tunnel matched, let upstream know, ipsec may handle it */
4543c10d 621 rcu_read_unlock();
6dcdd1b3 622 return 1;
1da177e4 623out:
36ca34cc 624 kfree_skb(skb);
1da177e4
LT
625 return 0;
626}
627
fa857afc
YH
628/*
629 * Returns the embedded IPv4 address if the IPv6 address
630 * comes from 6rd / 6to4 (RFC 3056) addr space.
631 */
632static inline
b71d1d42 633__be32 try_6rd(const struct in6_addr *v6dst, struct ip_tunnel *tunnel)
1da177e4 634{
e69a4adc 635 __be32 dst = 0;
1da177e4 636
fa857afc
YH
637#ifdef CONFIG_IPV6_SIT_6RD
638 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
639 tunnel->ip6rd.prefixlen)) {
3a43be3c 640 unsigned int pbw0, pbi0;
fa857afc
YH
641 int pbi1;
642 u32 d;
643
644 pbw0 = tunnel->ip6rd.prefixlen >> 5;
645 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
646
e7db38c3 647 d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
fa857afc
YH
648 tunnel->ip6rd.relay_prefixlen;
649
650 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
651 if (pbi1 > 0)
e7db38c3 652 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
fa857afc
YH
653 (32 - pbi1);
654
655 dst = tunnel->ip6rd.relay_prefix | htonl(d);
656 }
657#else
1da177e4 658 if (v6dst->s6_addr16[0] == htons(0x2002)) {
1ab1457c 659 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
1da177e4
LT
660 memcpy(&dst, &v6dst->s6_addr16[1], 4);
661 }
fa857afc 662#endif
1da177e4
LT
663 return dst;
664}
665
666/*
667 * This function assumes it is being called from dev_queue_xmit()
668 * and that skb is filled properly by that function.
669 */
670
6fef4c0c
SH
671static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
672 struct net_device *dev)
1da177e4 673{
2941a486 674 struct ip_tunnel *tunnel = netdev_priv(dev);
15fc1f70 675 struct pcpu_tstats *tstats;
b71d1d42
ED
676 const struct iphdr *tiph = &tunnel->parms.iph;
677 const struct ipv6hdr *iph6 = ipv6_hdr(skb);
1da177e4 678 u8 tos = tunnel->parms.iph.tos;
292f4f3c 679 __be16 df = tiph->frag_off;
1da177e4 680 struct rtable *rt; /* Route to the other host */
15fc1f70 681 struct net_device *tdev; /* Device to other host */
1da177e4 682 struct iphdr *iph; /* Our new IP header */
c2636b4d 683 unsigned int max_headroom; /* The extra header space needed */
e69a4adc 684 __be32 dst = tiph->daddr;
31e4543d 685 struct flowi4 fl4;
1da177e4 686 int mtu;
b71d1d42 687 const struct in6_addr *addr6;
1da177e4
LT
688 int addr_type;
689
1da177e4
LT
690 if (skb->protocol != htons(ETH_P_IPV6))
691 goto tx_error;
692
c2bceb3d
LEM
693 if (tos == 1)
694 tos = ipv6_get_dsfield(iph6);
695
c7dc89c0
FT
696 /* ISATAP (RFC4214) - must come before 6to4 */
697 if (dev->priv_flags & IFF_ISATAP) {
698 struct neighbour *neigh = NULL;
1e2927b0 699 bool do_tx_error = false;
c7dc89c0 700
adf30907 701 if (skb_dst(skb))
1e2927b0 702 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
c7dc89c0
FT
703
704 if (neigh == NULL) {
e87cc472 705 net_dbg_ratelimited("sit: nexthop == NULL\n");
c7dc89c0
FT
706 goto tx_error;
707 }
708
3e866703 709 addr6 = (const struct in6_addr *)&neigh->primary_key;
c7dc89c0
FT
710 addr_type = ipv6_addr_type(addr6);
711
712 if ((addr_type & IPV6_ADDR_UNICAST) &&
713 ipv6_addr_is_isatap(addr6))
714 dst = addr6->s6_addr32[3];
715 else
1e2927b0
DM
716 do_tx_error = true;
717
718 neigh_release(neigh);
719 if (do_tx_error)
c7dc89c0
FT
720 goto tx_error;
721 }
722
1da177e4 723 if (!dst)
fa857afc 724 dst = try_6rd(&iph6->daddr, tunnel);
1da177e4
LT
725
726 if (!dst) {
727 struct neighbour *neigh = NULL;
1e2927b0 728 bool do_tx_error = false;
1da177e4 729
adf30907 730 if (skb_dst(skb))
1e2927b0 731 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
1da177e4
LT
732
733 if (neigh == NULL) {
e87cc472 734 net_dbg_ratelimited("sit: nexthop == NULL\n");
1da177e4
LT
735 goto tx_error;
736 }
737
3e866703 738 addr6 = (const struct in6_addr *)&neigh->primary_key;
1da177e4
LT
739 addr_type = ipv6_addr_type(addr6);
740
741 if (addr_type == IPV6_ADDR_ANY) {
0660e03f 742 addr6 = &ipv6_hdr(skb)->daddr;
1da177e4
LT
743 addr_type = ipv6_addr_type(addr6);
744 }
745
1e2927b0
DM
746 if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
747 dst = addr6->s6_addr32[3];
748 else
749 do_tx_error = true;
1da177e4 750
1e2927b0
DM
751 neigh_release(neigh);
752 if (do_tx_error)
753 goto tx_error;
1da177e4
LT
754 }
755
31e4543d 756 rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
78fbfd8a
DM
757 dst, tiph->saddr,
758 0, 0,
759 IPPROTO_IPV6, RT_TOS(tos),
760 tunnel->parms.link);
761 if (IS_ERR(rt)) {
762 dev->stats.tx_carrier_errors++;
763 goto tx_error_icmp;
1da177e4
LT
764 }
765 if (rt->rt_type != RTN_UNICAST) {
766 ip_rt_put(rt);
15fc1f70 767 dev->stats.tx_carrier_errors++;
1da177e4
LT
768 goto tx_error_icmp;
769 }
d8d1f30b 770 tdev = rt->dst.dev;
1da177e4
LT
771
772 if (tdev == dev) {
773 ip_rt_put(rt);
15fc1f70 774 dev->stats.collisions++;
1da177e4
LT
775 goto tx_error;
776 }
777
292f4f3c 778 if (df) {
d8d1f30b 779 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
1da177e4 780
292f4f3c 781 if (mtu < 68) {
15fc1f70 782 dev->stats.collisions++;
292f4f3c
HX
783 ip_rt_put(rt);
784 goto tx_error;
785 }
1da177e4 786
292f4f3c
HX
787 if (mtu < IPV6_MIN_MTU) {
788 mtu = IPV6_MIN_MTU;
789 df = 0;
790 }
791
792 if (tunnel->parms.iph.daddr && skb_dst(skb))
793 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
794
795 if (skb->len > mtu) {
3ffe533c 796 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
292f4f3c
HX
797 ip_rt_put(rt);
798 goto tx_error;
799 }
1da177e4
LT
800 }
801
802 if (tunnel->err_count > 0) {
bb80087a
WY
803 if (time_before(jiffies,
804 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
1da177e4
LT
805 tunnel->err_count--;
806 dst_link_failure(skb);
807 } else
808 tunnel->err_count = 0;
809 }
810
811 /*
812 * Okay, now see if we can stuff it in the buffer as-is.
813 */
814 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
815
cfbba49d
PM
816 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
817 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1da177e4
LT
818 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
819 if (!new_skb) {
820 ip_rt_put(rt);
15fc1f70 821 dev->stats.tx_dropped++;
1da177e4 822 dev_kfree_skb(skb);
6ed10654 823 return NETDEV_TX_OK;
1da177e4
LT
824 }
825 if (skb->sk)
826 skb_set_owner_w(new_skb, skb->sk);
827 dev_kfree_skb(skb);
828 skb = new_skb;
0660e03f 829 iph6 = ipv6_hdr(skb);
1da177e4
LT
830 }
831
b0e380b1 832 skb->transport_header = skb->network_header;
e2d1bca7
ACM
833 skb_push(skb, sizeof(struct iphdr));
834 skb_reset_network_header(skb);
1da177e4 835 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
8cdfab8a 836 IPCB(skb)->flags = 0;
adf30907 837 skb_dst_drop(skb);
d8d1f30b 838 skb_dst_set(skb, &rt->dst);
1da177e4
LT
839
840 /*
841 * Push down and install the IPIP header.
842 */
843
eddc9ec5 844 iph = ip_hdr(skb);
1da177e4
LT
845 iph->version = 4;
846 iph->ihl = sizeof(struct iphdr)>>2;
292f4f3c 847 iph->frag_off = df;
1da177e4
LT
848 iph->protocol = IPPROTO_IPV6;
849 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
301102cc
DM
850 iph->daddr = fl4.daddr;
851 iph->saddr = fl4.saddr;
1da177e4
LT
852
853 if ((iph->ttl = tiph->ttl) == 0)
854 iph->ttl = iph6->hop_limit;
855
856 nf_reset(skb);
15fc1f70
ED
857 tstats = this_cpu_ptr(dev->tstats);
858 __IPTUNNEL_XMIT(tstats, &dev->stats);
6ed10654 859 return NETDEV_TX_OK;
1da177e4
LT
860
861tx_error_icmp:
862 dst_link_failure(skb);
863tx_error:
15fc1f70 864 dev->stats.tx_errors++;
1da177e4 865 dev_kfree_skb(skb);
6ed10654 866 return NETDEV_TX_OK;
1da177e4
LT
867}
868
8a4a50f9
MS
869static void ipip6_tunnel_bind_dev(struct net_device *dev)
870{
871 struct net_device *tdev = NULL;
872 struct ip_tunnel *tunnel;
b71d1d42 873 const struct iphdr *iph;
31e4543d 874 struct flowi4 fl4;
8a4a50f9
MS
875
876 tunnel = netdev_priv(dev);
877 iph = &tunnel->parms.iph;
878
879 if (iph->daddr) {
31e4543d 880 struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
78fbfd8a
DM
881 iph->daddr, iph->saddr,
882 0, 0,
883 IPPROTO_IPV6,
884 RT_TOS(iph->tos),
885 tunnel->parms.link);
b23dd4fe
DM
886
887 if (!IS_ERR(rt)) {
d8d1f30b 888 tdev = rt->dst.dev;
8a4a50f9
MS
889 ip_rt_put(rt);
890 }
891 dev->flags |= IFF_POINTOPOINT;
892 }
893
894 if (!tdev && tunnel->parms.link)
907a08c4 895 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
8a4a50f9
MS
896
897 if (tdev) {
898 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
899 dev->mtu = tdev->mtu - sizeof(struct iphdr);
900 if (dev->mtu < IPV6_MIN_MTU)
901 dev->mtu = IPV6_MIN_MTU;
902 }
903 dev->iflink = tunnel->parms.link;
904}
905
1da177e4
LT
906static int
907ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
908{
909 int err = 0;
910 struct ip_tunnel_parm p;
fadf6bf0 911 struct ip_tunnel_prl prl;
1da177e4 912 struct ip_tunnel *t;
ca8def14
PE
913 struct net *net = dev_net(dev);
914 struct sit_net *sitn = net_generic(net, sit_net_id);
fa857afc
YH
915#ifdef CONFIG_IPV6_SIT_6RD
916 struct ip_tunnel_6rd ip6rd;
917#endif
1da177e4
LT
918
919 switch (cmd) {
920 case SIOCGETTUNNEL:
fa857afc
YH
921#ifdef CONFIG_IPV6_SIT_6RD
922 case SIOCGET6RD:
923#endif
1da177e4 924 t = NULL;
cd3dbc19 925 if (dev == sitn->fb_tunnel_dev) {
1da177e4
LT
926 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
927 err = -EFAULT;
928 break;
929 }
ca8def14 930 t = ipip6_tunnel_locate(net, &p, 0);
1da177e4
LT
931 }
932 if (t == NULL)
2941a486 933 t = netdev_priv(dev);
fa857afc
YH
934
935 err = -EFAULT;
936 if (cmd == SIOCGETTUNNEL) {
937 memcpy(&p, &t->parms, sizeof(p));
938 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
939 sizeof(p)))
940 goto done;
941#ifdef CONFIG_IPV6_SIT_6RD
942 } else {
4e3fd7a0 943 ip6rd.prefix = t->ip6rd.prefix;
fa857afc
YH
944 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
945 ip6rd.prefixlen = t->ip6rd.prefixlen;
946 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
947 if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
948 sizeof(ip6rd)))
949 goto done;
950#endif
951 }
952 err = 0;
1da177e4
LT
953 break;
954
955 case SIOCADDTUNNEL:
956 case SIOCCHGTUNNEL:
957 err = -EPERM;
958 if (!capable(CAP_NET_ADMIN))
959 goto done;
960
961 err = -EFAULT;
962 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
963 goto done;
964
965 err = -EINVAL;
966 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
967 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
968 goto done;
969 if (p.iph.ttl)
970 p.iph.frag_off |= htons(IP_DF);
971
ca8def14 972 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
1da177e4 973
cd3dbc19 974 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1da177e4
LT
975 if (t != NULL) {
976 if (t->dev != dev) {
977 err = -EEXIST;
978 break;
979 }
980 } else {
981 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
982 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
983 err = -EINVAL;
984 break;
985 }
2941a486 986 t = netdev_priv(dev);
ca8def14 987 ipip6_tunnel_unlink(sitn, t);
74b0b85b 988 synchronize_net();
1da177e4
LT
989 t->parms.iph.saddr = p.iph.saddr;
990 t->parms.iph.daddr = p.iph.daddr;
991 memcpy(dev->dev_addr, &p.iph.saddr, 4);
992 memcpy(dev->broadcast, &p.iph.daddr, 4);
ca8def14 993 ipip6_tunnel_link(sitn, t);
1da177e4
LT
994 netdev_state_change(dev);
995 }
996 }
997
998 if (t) {
999 err = 0;
1000 if (cmd == SIOCCHGTUNNEL) {
1001 t->parms.iph.ttl = p.iph.ttl;
1002 t->parms.iph.tos = p.iph.tos;
8a4a50f9
MS
1003 if (t->parms.link != p.link) {
1004 t->parms.link = p.link;
1005 ipip6_tunnel_bind_dev(dev);
1006 netdev_state_change(dev);
1007 }
1da177e4
LT
1008 }
1009 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1010 err = -EFAULT;
1011 } else
1012 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1013 break;
1014
1015 case SIOCDELTUNNEL:
1016 err = -EPERM;
1017 if (!capable(CAP_NET_ADMIN))
1018 goto done;
1019
cd3dbc19 1020 if (dev == sitn->fb_tunnel_dev) {
1da177e4
LT
1021 err = -EFAULT;
1022 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1023 goto done;
1024 err = -ENOENT;
ca8def14 1025 if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
1da177e4
LT
1026 goto done;
1027 err = -EPERM;
cd3dbc19 1028 if (t == netdev_priv(sitn->fb_tunnel_dev))
1da177e4
LT
1029 goto done;
1030 dev = t->dev;
1031 }
22f8cde5
SH
1032 unregister_netdevice(dev);
1033 err = 0;
1da177e4
LT
1034 break;
1035
300aaeea 1036 case SIOCGETPRL:
2b4743bd
YH
1037 err = -EINVAL;
1038 if (dev == sitn->fb_tunnel_dev)
1039 goto done;
1040 err = -ENOENT;
1041 if (!(t = netdev_priv(dev)))
1042 goto done;
1043 err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
1044 break;
1045
fadf6bf0
TF
1046 case SIOCADDPRL:
1047 case SIOCDELPRL:
1048 case SIOCCHGPRL:
1049 err = -EPERM;
2b4743bd 1050 if (!capable(CAP_NET_ADMIN))
fadf6bf0
TF
1051 goto done;
1052 err = -EINVAL;
cd3dbc19 1053 if (dev == sitn->fb_tunnel_dev)
fadf6bf0
TF
1054 goto done;
1055 err = -EFAULT;
1056 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
1057 goto done;
1058 err = -ENOENT;
1059 if (!(t = netdev_priv(dev)))
1060 goto done;
1061
300aaeea 1062 switch (cmd) {
300aaeea 1063 case SIOCDELPRL:
fadf6bf0 1064 err = ipip6_tunnel_del_prl(t, &prl);
300aaeea
YH
1065 break;
1066 case SIOCADDPRL:
1067 case SIOCCHGPRL:
fadf6bf0 1068 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
300aaeea
YH
1069 break;
1070 }
2b4743bd 1071 netdev_state_change(dev);
fadf6bf0
TF
1072 break;
1073
fa857afc
YH
1074#ifdef CONFIG_IPV6_SIT_6RD
1075 case SIOCADD6RD:
1076 case SIOCCHG6RD:
1077 case SIOCDEL6RD:
1078 err = -EPERM;
1079 if (!capable(CAP_NET_ADMIN))
1080 goto done;
1081
1082 err = -EFAULT;
1083 if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
1084 sizeof(ip6rd)))
1085 goto done;
1086
1087 t = netdev_priv(dev);
1088
1089 if (cmd != SIOCDEL6RD) {
1090 struct in6_addr prefix;
1091 __be32 relay_prefix;
1092
1093 err = -EINVAL;
1094 if (ip6rd.relay_prefixlen > 32 ||
1095 ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
1096 goto done;
1097
1098 ipv6_addr_prefix(&prefix, &ip6rd.prefix,
1099 ip6rd.prefixlen);
1100 if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
1101 goto done;
91b2a3f9
YH
1102 if (ip6rd.relay_prefixlen)
1103 relay_prefix = ip6rd.relay_prefix &
1104 htonl(0xffffffffUL <<
1105 (32 - ip6rd.relay_prefixlen));
1106 else
1107 relay_prefix = 0;
fa857afc
YH
1108 if (relay_prefix != ip6rd.relay_prefix)
1109 goto done;
1110
4e3fd7a0 1111 t->ip6rd.prefix = prefix;
fa857afc
YH
1112 t->ip6rd.relay_prefix = relay_prefix;
1113 t->ip6rd.prefixlen = ip6rd.prefixlen;
1114 t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
1115 } else
e0c93948 1116 ipip6_tunnel_clone_6rd(dev, sitn);
fa857afc
YH
1117
1118 err = 0;
1119 break;
1120#endif
1121
1da177e4
LT
1122 default:
1123 err = -EINVAL;
1124 }
1125
1126done:
1127 return err;
1128}
1129
1da177e4
LT
1130static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1131{
1132 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
1133 return -EINVAL;
1134 dev->mtu = new_mtu;
1135 return 0;
1136}
1137
1326c3d5
SH
1138static const struct net_device_ops ipip6_netdev_ops = {
1139 .ndo_uninit = ipip6_tunnel_uninit,
1140 .ndo_start_xmit = ipip6_tunnel_xmit,
1141 .ndo_do_ioctl = ipip6_tunnel_ioctl,
1142 .ndo_change_mtu = ipip6_tunnel_change_mtu,
87b6d218 1143 .ndo_get_stats64= ipip6_get_stats64,
1326c3d5
SH
1144};
1145
15fc1f70
ED
1146static void ipip6_dev_free(struct net_device *dev)
1147{
1148 free_percpu(dev->tstats);
1149 free_netdev(dev);
1150}
1151
1da177e4
LT
1152static void ipip6_tunnel_setup(struct net_device *dev)
1153{
1326c3d5 1154 dev->netdev_ops = &ipip6_netdev_ops;
15fc1f70 1155 dev->destructor = ipip6_dev_free;
1da177e4
LT
1156
1157 dev->type = ARPHRD_SIT;
1158 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
46f25dff 1159 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
1da177e4 1160 dev->flags = IFF_NOARP;
f2ba025b 1161 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1da177e4
LT
1162 dev->iflink = 0;
1163 dev->addr_len = 4;
7a97146c 1164 dev->features |= NETIF_F_NETNS_LOCAL;
8df40d10 1165 dev->features |= NETIF_F_LLTX;
1da177e4
LT
1166}
1167
15fc1f70 1168static int ipip6_tunnel_init(struct net_device *dev)
1da177e4 1169{
1326c3d5 1170 struct ip_tunnel *tunnel = netdev_priv(dev);
1da177e4
LT
1171
1172 tunnel->dev = dev;
1da177e4
LT
1173
1174 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1175 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1176
8a4a50f9 1177 ipip6_tunnel_bind_dev(dev);
15fc1f70
ED
1178 dev->tstats = alloc_percpu(struct pcpu_tstats);
1179 if (!dev->tstats)
1180 return -ENOMEM;
1181
1182 return 0;
1da177e4
LT
1183}
1184
dd4080ee 1185static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1da177e4 1186{
2941a486 1187 struct ip_tunnel *tunnel = netdev_priv(dev);
1da177e4 1188 struct iphdr *iph = &tunnel->parms.iph;
29182176
PE
1189 struct net *net = dev_net(dev);
1190 struct sit_net *sitn = net_generic(net, sit_net_id);
1da177e4
LT
1191
1192 tunnel->dev = dev;
1193 strcpy(tunnel->parms.name, dev->name);
1194
1195 iph->version = 4;
1196 iph->protocol = IPPROTO_IPV6;
1197 iph->ihl = 5;
1198 iph->ttl = 64;
1199
dd4080ee
ED
1200 dev->tstats = alloc_percpu(struct pcpu_tstats);
1201 if (!dev->tstats)
1202 return -ENOMEM;
1da177e4 1203 dev_hold(dev);
cf778b00 1204 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
dd4080ee 1205 return 0;
1da177e4
LT
1206}
1207
6dcd814b 1208static struct xfrm_tunnel sit_handler __read_mostly = {
1da177e4
LT
1209 .handler = ipip6_rcv,
1210 .err_handler = ipip6_err,
c73cb5a2 1211 .priority = 1,
1da177e4
LT
1212};
1213
2c8c1e72 1214static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
db44575f
AK
1215{
1216 int prio;
1217
1218 for (prio = 1; prio < 4; prio++) {
1219 int h;
1220 for (h = 0; h < HASH_SIZE; h++) {
753ea8e9 1221 struct ip_tunnel *t;
62808f91 1222
753ea8e9 1223 t = rtnl_dereference(sitn->tunnels[prio][h]);
62808f91
ED
1224 while (t != NULL) {
1225 unregister_netdevice_queue(t->dev, head);
753ea8e9 1226 t = rtnl_dereference(t->next);
62808f91 1227 }
db44575f
AK
1228 }
1229 }
1230}
1231
2c8c1e72 1232static int __net_init sit_init_net(struct net *net)
8190d900 1233{
67101172 1234 struct sit_net *sitn = net_generic(net, sit_net_id);
72b36015 1235 struct ip_tunnel *t;
8190d900 1236 int err;
8190d900 1237
29182176
PE
1238 sitn->tunnels[0] = sitn->tunnels_wc;
1239 sitn->tunnels[1] = sitn->tunnels_l;
1240 sitn->tunnels[2] = sitn->tunnels_r;
1241 sitn->tunnels[3] = sitn->tunnels_r_l;
1242
cd3dbc19
PE
1243 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1244 ipip6_tunnel_setup);
1245 if (!sitn->fb_tunnel_dev) {
1246 err = -ENOMEM;
1247 goto err_alloc_dev;
1248 }
be77e593 1249 dev_net_set(sitn->fb_tunnel_dev, net);
cd3dbc19 1250
dd4080ee
ED
1251 err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
1252 if (err)
1253 goto err_dev_free;
1254
e0c93948 1255 ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
cd3dbc19
PE
1256
1257 if ((err = register_netdev(sitn->fb_tunnel_dev)))
1258 goto err_reg_dev;
1259
72b36015
TF
1260 t = netdev_priv(sitn->fb_tunnel_dev);
1261
1262 strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
8190d900
PE
1263 return 0;
1264
cd3dbc19 1265err_reg_dev:
1326c3d5 1266 dev_put(sitn->fb_tunnel_dev);
dd4080ee
ED
1267err_dev_free:
1268 ipip6_dev_free(sitn->fb_tunnel_dev);
cd3dbc19 1269err_alloc_dev:
8190d900
PE
1270 return err;
1271}
1272
2c8c1e72 1273static void __net_exit sit_exit_net(struct net *net)
8190d900 1274{
67101172 1275 struct sit_net *sitn = net_generic(net, sit_net_id);
62808f91 1276 LIST_HEAD(list);
8190d900 1277
cd3dbc19 1278 rtnl_lock();
62808f91
ED
1279 sit_destroy_tunnels(sitn, &list);
1280 unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
1281 unregister_netdevice_many(&list);
cd3dbc19 1282 rtnl_unlock();
8190d900
PE
1283}
1284
1285static struct pernet_operations sit_net_ops = {
1286 .init = sit_init_net,
1287 .exit = sit_exit_net,
67101172
EB
1288 .id = &sit_net_id,
1289 .size = sizeof(struct sit_net),
8190d900
PE
1290};
1291
89c89458 1292static void __exit sit_cleanup(void)
1da177e4 1293{
c73cb5a2 1294 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
db44575f 1295
67101172 1296 unregister_pernet_device(&sit_net_ops);
ef9a9d11 1297 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1da177e4
LT
1298}
1299
89c89458 1300static int __init sit_init(void)
1da177e4
LT
1301{
1302 int err;
1303
1304 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
1305
67101172 1306 err = register_pernet_device(&sit_net_ops);
8190d900 1307 if (err < 0)
d5aa407f
AD
1308 return err;
1309 err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
1310 if (err < 0) {
1311 unregister_pernet_device(&sit_net_ops);
1312 printk(KERN_INFO "sit init: Can't add protocol\n");
1313 }
1da177e4 1314 return err;
1da177e4 1315}
989e5b96
JR
1316
1317module_init(sit_init);
1318module_exit(sit_cleanup);
39c85086 1319MODULE_LICENSE("GPL");
8909c9ad 1320MODULE_ALIAS_NETDEV("sit0");