blob: 7f9cae4c49e7ec42ad54a90405791b5ab698fa04 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
4 * Linux INET6 implementation
5 *
6 * Authors:
7 * Pedro Roque <roque@di.fc.ul.pt>
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010 * Changes:
11 * Roger Venning <r.venning@telstra.com>: 6to4 support
12 * Nate Thompson <nate@thebog.net>: 6to4 support
13 * Fred Templin <fred.l.templin@boeing.com>: isatap support
14 */
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include <linux/module.h>
19#include <linux/capability.h>
20#include <linux/errno.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/sockios.h>
24#include <linux/net.h>
25#include <linux/in6.h>
26#include <linux/netdevice.h>
27#include <linux/if_arp.h>
28#include <linux/icmp.h>
29#include <linux/slab.h>
30#include <linux/uaccess.h>
31#include <linux/init.h>
32#include <linux/netfilter_ipv4.h>
33#include <linux/if_ether.h>
34
35#include <net/sock.h>
36#include <net/snmp.h>
37
38#include <net/ipv6.h>
39#include <net/protocol.h>
40#include <net/transp_v6.h>
41#include <net/ip6_fib.h>
42#include <net/ip6_route.h>
43#include <net/ndisc.h>
44#include <net/addrconf.h>
45#include <net/ip.h>
46#include <net/udp.h>
47#include <net/icmp.h>
48#include <net/ip_tunnels.h>
49#include <net/inet_ecn.h>
50#include <net/xfrm.h>
51#include <net/dsfield.h>
52#include <net/net_namespace.h>
53#include <net/netns/generic.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 IP6_SIT_HASH_SIZE 16
62#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
63
64static bool log_ecn_error = true;
65module_param(log_ecn_error, bool, 0644);
66MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
67
68static int ipip6_tunnel_init(struct net_device *dev);
69static void ipip6_tunnel_setup(struct net_device *dev);
70static void ipip6_dev_free(struct net_device *dev);
71static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
72 __be32 *v4dst);
73static struct rtnl_link_ops sit_link_ops __read_mostly;
74
75static unsigned int sit_net_id __read_mostly;
76struct sit_net {
77 struct ip_tunnel __rcu *tunnels_r_l[IP6_SIT_HASH_SIZE];
78 struct ip_tunnel __rcu *tunnels_r[IP6_SIT_HASH_SIZE];
79 struct ip_tunnel __rcu *tunnels_l[IP6_SIT_HASH_SIZE];
80 struct ip_tunnel __rcu *tunnels_wc[1];
81 struct ip_tunnel __rcu **tunnels[4];
82
83 struct net_device *fb_tunnel_dev;
84};
85
86/*
87 * Must be invoked with rcu_read_lock
88 */
89static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
90 struct net_device *dev,
91 __be32 remote, __be32 local,
92 int sifindex)
93{
94 unsigned int h0 = HASH(remote);
95 unsigned int h1 = HASH(local);
96 struct ip_tunnel *t;
97 struct sit_net *sitn = net_generic(net, sit_net_id);
98 int ifindex = dev ? dev->ifindex : 0;
99
100 for_each_ip_tunnel_rcu(t, sitn->tunnels_r_l[h0 ^ h1]) {
101 if (local == t->parms.iph.saddr &&
102 remote == t->parms.iph.daddr &&
103 (!dev || !t->parms.link || ifindex == t->parms.link ||
104 sifindex == t->parms.link) &&
105 (t->dev->flags & IFF_UP))
106 return t;
107 }
108 for_each_ip_tunnel_rcu(t, sitn->tunnels_r[h0]) {
109 if (remote == t->parms.iph.daddr &&
110 (!dev || !t->parms.link || ifindex == t->parms.link ||
111 sifindex == t->parms.link) &&
112 (t->dev->flags & IFF_UP))
113 return t;
114 }
115 for_each_ip_tunnel_rcu(t, sitn->tunnels_l[h1]) {
116 if (local == t->parms.iph.saddr &&
117 (!dev || !t->parms.link || ifindex == t->parms.link ||
118 sifindex == t->parms.link) &&
119 (t->dev->flags & IFF_UP))
120 return t;
121 }
122 t = rcu_dereference(sitn->tunnels_wc[0]);
123 if (t && (t->dev->flags & IFF_UP))
124 return t;
125 return NULL;
126}
127
128static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
129 struct ip_tunnel_parm *parms)
130{
131 __be32 remote = parms->iph.daddr;
132 __be32 local = parms->iph.saddr;
133 unsigned int h = 0;
134 int prio = 0;
135
136 if (remote) {
137 prio |= 2;
138 h ^= HASH(remote);
139 }
140 if (local) {
141 prio |= 1;
142 h ^= HASH(local);
143 }
144 return &sitn->tunnels[prio][h];
145}
146
147static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
148 struct ip_tunnel *t)
149{
150 return __ipip6_bucket(sitn, &t->parms);
151}
152
153static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
154{
155 struct ip_tunnel __rcu **tp;
156 struct ip_tunnel *iter;
157
158 for (tp = ipip6_bucket(sitn, t);
159 (iter = rtnl_dereference(*tp)) != NULL;
160 tp = &iter->next) {
161 if (t == iter) {
162 rcu_assign_pointer(*tp, t->next);
163 break;
164 }
165 }
166}
167
168static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
169{
170 struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
171
172 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
173 rcu_assign_pointer(*tp, t);
174}
175
176static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
177{
178#ifdef CONFIG_IPV6_SIT_6RD
179 struct ip_tunnel *t = netdev_priv(dev);
180
181 if (dev == sitn->fb_tunnel_dev || !sitn->fb_tunnel_dev) {
182 ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
183 t->ip6rd.relay_prefix = 0;
184 t->ip6rd.prefixlen = 16;
185 t->ip6rd.relay_prefixlen = 0;
186 } else {
187 struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
188 memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
189 }
190#endif
191}
192
193static int ipip6_tunnel_create(struct net_device *dev)
194{
195 struct ip_tunnel *t = netdev_priv(dev);
196 struct net *net = dev_net(dev);
197 struct sit_net *sitn = net_generic(net, sit_net_id);
198 int err;
199
200 memcpy(dev->dev_addr, &t->parms.iph.saddr, 4);
201 memcpy(dev->broadcast, &t->parms.iph.daddr, 4);
202
203 if ((__force u16)t->parms.i_flags & SIT_ISATAP)
204 dev->priv_flags |= IFF_ISATAP;
205
206 dev->rtnl_link_ops = &sit_link_ops;
207
208 err = register_netdevice(dev);
209 if (err < 0)
210 goto out;
211
212 ipip6_tunnel_clone_6rd(dev, sitn);
213
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214 ipip6_tunnel_link(sitn, t);
215 return 0;
216
217out:
218 return err;
219}
220
221static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
222 struct ip_tunnel_parm *parms, int create)
223{
224 __be32 remote = parms->iph.daddr;
225 __be32 local = parms->iph.saddr;
226 struct ip_tunnel *t, *nt;
227 struct ip_tunnel __rcu **tp;
228 struct net_device *dev;
229 char name[IFNAMSIZ];
230 struct sit_net *sitn = net_generic(net, sit_net_id);
231
232 for (tp = __ipip6_bucket(sitn, parms);
233 (t = rtnl_dereference(*tp)) != NULL;
234 tp = &t->next) {
235 if (local == t->parms.iph.saddr &&
236 remote == t->parms.iph.daddr &&
237 parms->link == t->parms.link) {
238 if (create)
239 return NULL;
240 else
241 return t;
242 }
243 }
244 if (!create)
245 goto failed;
246
247 if (parms->name[0]) {
248 if (!dev_valid_name(parms->name))
249 goto failed;
250 strlcpy(name, parms->name, IFNAMSIZ);
251 } else {
252 strcpy(name, "sit%d");
253 }
254 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
255 ipip6_tunnel_setup);
256 if (!dev)
257 return NULL;
258
259 dev_net_set(dev, net);
260
261 nt = netdev_priv(dev);
262
263 nt->parms = *parms;
264 if (ipip6_tunnel_create(dev) < 0)
265 goto failed_free;
266
267 return nt;
268
269failed_free:
270 free_netdev(dev);
271failed:
272 return NULL;
273}
274
275#define for_each_prl_rcu(start) \
276 for (prl = rcu_dereference(start); \
277 prl; \
278 prl = rcu_dereference(prl->next))
279
280static struct ip_tunnel_prl_entry *
281__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
282{
283 struct ip_tunnel_prl_entry *prl;
284
285 for_each_prl_rcu(t->prl)
286 if (prl->addr == addr)
287 break;
288 return prl;
289
290}
291
292static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
293 struct ip_tunnel_prl __user *a)
294{
295 struct ip_tunnel_prl kprl, *kp;
296 struct ip_tunnel_prl_entry *prl;
297 unsigned int cmax, c = 0, ca, len;
298 int ret = 0;
299
300 if (copy_from_user(&kprl, a, sizeof(kprl)))
301 return -EFAULT;
302 cmax = kprl.datalen / sizeof(kprl);
303 if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
304 cmax = 1;
305
306 /* For simple GET or for root users,
307 * we try harder to allocate.
308 */
309 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
310 kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
311 NULL;
312
313 rcu_read_lock();
314
315 ca = t->prl_count < cmax ? t->prl_count : cmax;
316
317 if (!kp) {
318 /* We don't try hard to allocate much memory for
319 * non-root users.
320 * For root users, retry allocating enough memory for
321 * the answer.
322 */
323 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
324 if (!kp) {
325 ret = -ENOMEM;
326 goto out;
327 }
328 }
329
330 c = 0;
331 for_each_prl_rcu(t->prl) {
332 if (c >= cmax)
333 break;
334 if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
335 continue;
336 kp[c].addr = prl->addr;
337 kp[c].flags = prl->flags;
338 c++;
339 if (kprl.addr != htonl(INADDR_ANY))
340 break;
341 }
342out:
343 rcu_read_unlock();
344
345 len = sizeof(*kp) * c;
346 ret = 0;
347 if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
348 ret = -EFAULT;
349
350 kfree(kp);
351
352 return ret;
353}
354
355static int
356ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
357{
358 struct ip_tunnel_prl_entry *p;
359 int err = 0;
360
361 if (a->addr == htonl(INADDR_ANY))
362 return -EINVAL;
363
364 ASSERT_RTNL();
365
366 for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
367 if (p->addr == a->addr) {
368 if (chg) {
369 p->flags = a->flags;
370 goto out;
371 }
372 err = -EEXIST;
373 goto out;
374 }
375 }
376
377 if (chg) {
378 err = -ENXIO;
379 goto out;
380 }
381
382 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
383 if (!p) {
384 err = -ENOBUFS;
385 goto out;
386 }
387
388 p->next = t->prl;
389 p->addr = a->addr;
390 p->flags = a->flags;
391 t->prl_count++;
392 rcu_assign_pointer(t->prl, p);
393out:
394 return err;
395}
396
397static void prl_list_destroy_rcu(struct rcu_head *head)
398{
399 struct ip_tunnel_prl_entry *p, *n;
400
401 p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
402 do {
403 n = rcu_dereference_protected(p->next, 1);
404 kfree(p);
405 p = n;
406 } while (p);
407}
408
409static int
410ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
411{
412 struct ip_tunnel_prl_entry *x;
413 struct ip_tunnel_prl_entry __rcu **p;
414 int err = 0;
415
416 ASSERT_RTNL();
417
418 if (a && a->addr != htonl(INADDR_ANY)) {
419 for (p = &t->prl;
420 (x = rtnl_dereference(*p)) != NULL;
421 p = &x->next) {
422 if (x->addr == a->addr) {
423 *p = x->next;
424 kfree_rcu(x, rcu_head);
425 t->prl_count--;
426 goto out;
427 }
428 }
429 err = -ENXIO;
430 } else {
431 x = rtnl_dereference(t->prl);
432 if (x) {
433 t->prl_count = 0;
434 call_rcu(&x->rcu_head, prl_list_destroy_rcu);
435 t->prl = NULL;
436 }
437 }
438out:
439 return err;
440}
441
442static int
443isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
444{
445 struct ip_tunnel_prl_entry *p;
446 int ok = 1;
447
448 rcu_read_lock();
449 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
450 if (p) {
451 if (p->flags & PRL_DEFAULT)
452 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
453 else
454 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
455 } else {
456 const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
457
458 if (ipv6_addr_is_isatap(addr6) &&
459 (addr6->s6_addr32[3] == iph->saddr) &&
460 ipv6_chk_prefix(addr6, t->dev))
461 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
462 else
463 ok = 0;
464 }
465 rcu_read_unlock();
466 return ok;
467}
468
469static void ipip6_tunnel_uninit(struct net_device *dev)
470{
471 struct ip_tunnel *tunnel = netdev_priv(dev);
472 struct sit_net *sitn = net_generic(tunnel->net, sit_net_id);
473
474 if (dev == sitn->fb_tunnel_dev) {
475 RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
476 } else {
477 ipip6_tunnel_unlink(sitn, tunnel);
478 ipip6_tunnel_del_prl(tunnel, NULL);
479 }
480 dst_cache_reset(&tunnel->dst_cache);
481 dev_put(dev);
482}
483
484static int ipip6_err(struct sk_buff *skb, u32 info)
485{
486 const struct iphdr *iph = (const struct iphdr *)skb->data;
487 const int type = icmp_hdr(skb)->type;
488 const int code = icmp_hdr(skb)->code;
489 unsigned int data_len = 0;
490 struct ip_tunnel *t;
491 int sifindex;
492 int err;
493
494 switch (type) {
495 default:
496 case ICMP_PARAMETERPROB:
497 return 0;
498
499 case ICMP_DEST_UNREACH:
500 switch (code) {
501 case ICMP_SR_FAILED:
502 /* Impossible event. */
503 return 0;
504 default:
505 /* All others are translated to HOST_UNREACH.
506 rfc2003 contains "deep thoughts" about NET_UNREACH,
507 I believe they are just ether pollution. --ANK
508 */
509 break;
510 }
511 break;
512 case ICMP_TIME_EXCEEDED:
513 if (code != ICMP_EXC_TTL)
514 return 0;
515 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
516 break;
517 case ICMP_REDIRECT:
518 break;
519 }
520
521 err = -ENOENT;
522
523 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
524 t = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
525 iph->daddr, iph->saddr, sifindex);
526 if (!t)
527 goto out;
528
529 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
530 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
David Brazdil0f672f62019-12-10 10:32:29 +0000531 t->parms.link, iph->protocol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000532 err = 0;
533 goto out;
534 }
535 if (type == ICMP_REDIRECT) {
David Brazdil0f672f62019-12-10 10:32:29 +0000536 ipv4_redirect(skb, dev_net(skb->dev), t->parms.link,
537 iph->protocol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538 err = 0;
539 goto out;
540 }
541
542 err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000543 if (__in6_dev_get(skb->dev) &&
544 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4, type, data_len))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545 goto out;
546
547 if (t->parms.iph.daddr == 0)
548 goto out;
549
550 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
551 goto out;
552
553 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
554 t->err_count++;
555 else
556 t->err_count = 1;
557 t->err_time = jiffies;
558out:
559 return err;
560}
561
562static inline bool is_spoofed_6rd(struct ip_tunnel *tunnel, const __be32 v4addr,
563 const struct in6_addr *v6addr)
564{
565 __be32 v4embed = 0;
566 if (check_6rd(tunnel, v6addr, &v4embed) && v4addr != v4embed)
567 return true;
568 return false;
569}
570
571/* Checks if an address matches an address on the tunnel interface.
572 * Used to detect the NAT of proto 41 packets and let them pass spoofing test.
573 * Long story:
574 * This function is called after we considered the packet as spoofed
575 * in is_spoofed_6rd.
576 * We may have a router that is doing NAT for proto 41 packets
577 * for an internal station. Destination a.a.a.a/PREFIX:bbbb:bbbb
578 * will be translated to n.n.n.n/PREFIX:bbbb:bbbb. And is_spoofed_6rd
579 * function will return true, dropping the packet.
580 * But, we can still check if is spoofed against the IP
581 * addresses associated with the interface.
582 */
583static bool only_dnatted(const struct ip_tunnel *tunnel,
584 const struct in6_addr *v6dst)
585{
586 int prefix_len;
587
588#ifdef CONFIG_IPV6_SIT_6RD
589 prefix_len = tunnel->ip6rd.prefixlen + 32
590 - tunnel->ip6rd.relay_prefixlen;
591#else
592 prefix_len = 48;
593#endif
594 return ipv6_chk_custom_prefix(v6dst, prefix_len, tunnel->dev);
595}
596
597/* Returns true if a packet is spoofed */
598static bool packet_is_spoofed(struct sk_buff *skb,
599 const struct iphdr *iph,
600 struct ip_tunnel *tunnel)
601{
602 const struct ipv6hdr *ipv6h;
603
604 if (tunnel->dev->priv_flags & IFF_ISATAP) {
605 if (!isatap_chksrc(skb, iph, tunnel))
606 return true;
607
608 return false;
609 }
610
611 if (tunnel->dev->flags & IFF_POINTOPOINT)
612 return false;
613
614 ipv6h = ipv6_hdr(skb);
615
616 if (unlikely(is_spoofed_6rd(tunnel, iph->saddr, &ipv6h->saddr))) {
617 net_warn_ratelimited("Src spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
618 &iph->saddr, &ipv6h->saddr,
619 &iph->daddr, &ipv6h->daddr);
620 return true;
621 }
622
623 if (likely(!is_spoofed_6rd(tunnel, iph->daddr, &ipv6h->daddr)))
624 return false;
625
626 if (only_dnatted(tunnel, &ipv6h->daddr))
627 return false;
628
629 net_warn_ratelimited("Dst spoofed %pI4/%pI6c -> %pI4/%pI6c\n",
630 &iph->saddr, &ipv6h->saddr,
631 &iph->daddr, &ipv6h->daddr);
632 return true;
633}
634
635static int ipip6_rcv(struct sk_buff *skb)
636{
637 const struct iphdr *iph = ip_hdr(skb);
638 struct ip_tunnel *tunnel;
639 int sifindex;
640 int err;
641
642 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
643 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
644 iph->saddr, iph->daddr, sifindex);
645 if (tunnel) {
646 struct pcpu_sw_netstats *tstats;
647
648 if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
649 tunnel->parms.iph.protocol != 0)
650 goto out;
651
652 skb->mac_header = skb->network_header;
653 skb_reset_network_header(skb);
654 IPCB(skb)->flags = 0;
655 skb->dev = tunnel->dev;
656
657 if (packet_is_spoofed(skb, iph, tunnel)) {
658 tunnel->dev->stats.rx_errors++;
659 goto out;
660 }
661
662 if (iptunnel_pull_header(skb, 0, htons(ETH_P_IPV6),
663 !net_eq(tunnel->net, dev_net(tunnel->dev))))
664 goto out;
665
David Brazdil0f672f62019-12-10 10:32:29 +0000666 /* skb can be uncloned in iptunnel_pull_header, so
667 * old iph is no longer valid
668 */
669 iph = (const struct iphdr *)skb_mac_header(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000670 err = IP_ECN_decapsulate(iph, skb);
671 if (unlikely(err)) {
672 if (log_ecn_error)
673 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
674 &iph->saddr, iph->tos);
675 if (err > 1) {
676 ++tunnel->dev->stats.rx_frame_errors;
677 ++tunnel->dev->stats.rx_errors;
678 goto out;
679 }
680 }
681
682 tstats = this_cpu_ptr(tunnel->dev->tstats);
683 u64_stats_update_begin(&tstats->syncp);
684 tstats->rx_packets++;
685 tstats->rx_bytes += skb->len;
686 u64_stats_update_end(&tstats->syncp);
687
688 netif_rx(skb);
689
690 return 0;
691 }
692
693 /* no tunnel matched, let upstream know, ipsec may handle it */
694 return 1;
695out:
696 kfree_skb(skb);
697 return 0;
698}
699
700static const struct tnl_ptk_info ipip_tpi = {
701 /* no tunnel info required for ipip. */
702 .proto = htons(ETH_P_IP),
703};
704
705#if IS_ENABLED(CONFIG_MPLS)
706static const struct tnl_ptk_info mplsip_tpi = {
707 /* no tunnel info required for mplsip. */
708 .proto = htons(ETH_P_MPLS_UC),
709};
710#endif
711
712static int sit_tunnel_rcv(struct sk_buff *skb, u8 ipproto)
713{
714 const struct iphdr *iph;
715 struct ip_tunnel *tunnel;
716 int sifindex;
717
718 sifindex = netif_is_l3_master(skb->dev) ? IPCB(skb)->iif : 0;
719
720 iph = ip_hdr(skb);
721 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
722 iph->saddr, iph->daddr, sifindex);
723 if (tunnel) {
724 const struct tnl_ptk_info *tpi;
725
726 if (tunnel->parms.iph.protocol != ipproto &&
727 tunnel->parms.iph.protocol != 0)
728 goto drop;
729
730 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
731 goto drop;
732#if IS_ENABLED(CONFIG_MPLS)
733 if (ipproto == IPPROTO_MPLS)
734 tpi = &mplsip_tpi;
735 else
736#endif
737 tpi = &ipip_tpi;
738 if (iptunnel_pull_header(skb, 0, tpi->proto, false))
739 goto drop;
740 return ip_tunnel_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
741 }
742
743 return 1;
744
745drop:
746 kfree_skb(skb);
747 return 0;
748}
749
750static int ipip_rcv(struct sk_buff *skb)
751{
752 return sit_tunnel_rcv(skb, IPPROTO_IPIP);
753}
754
755#if IS_ENABLED(CONFIG_MPLS)
756static int mplsip_rcv(struct sk_buff *skb)
757{
758 return sit_tunnel_rcv(skb, IPPROTO_MPLS);
759}
760#endif
761
762/*
763 * If the IPv6 address comes from 6rd / 6to4 (RFC 3056) addr space this function
764 * stores the embedded IPv4 address in v4dst and returns true.
765 */
766static bool check_6rd(struct ip_tunnel *tunnel, const struct in6_addr *v6dst,
767 __be32 *v4dst)
768{
769#ifdef CONFIG_IPV6_SIT_6RD
770 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
771 tunnel->ip6rd.prefixlen)) {
772 unsigned int pbw0, pbi0;
773 int pbi1;
774 u32 d;
775
776 pbw0 = tunnel->ip6rd.prefixlen >> 5;
777 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
778
David Brazdil0f672f62019-12-10 10:32:29 +0000779 d = tunnel->ip6rd.relay_prefixlen < 32 ?
780 (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
781 tunnel->ip6rd.relay_prefixlen : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782
783 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
784 if (pbi1 > 0)
785 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
786 (32 - pbi1);
787
788 *v4dst = tunnel->ip6rd.relay_prefix | htonl(d);
789 return true;
790 }
791#else
792 if (v6dst->s6_addr16[0] == htons(0x2002)) {
793 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
794 memcpy(v4dst, &v6dst->s6_addr16[1], 4);
795 return true;
796 }
797#endif
798 return false;
799}
800
801static inline __be32 try_6rd(struct ip_tunnel *tunnel,
802 const struct in6_addr *v6dst)
803{
804 __be32 dst = 0;
805 check_6rd(tunnel, v6dst, &dst);
806 return dst;
807}
808
809/*
810 * This function assumes it is being called from dev_queue_xmit()
811 * and that skb is filled properly by that function.
812 */
813
814static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
815 struct net_device *dev)
816{
817 struct ip_tunnel *tunnel = netdev_priv(dev);
818 const struct iphdr *tiph = &tunnel->parms.iph;
819 const struct ipv6hdr *iph6 = ipv6_hdr(skb);
820 u8 tos = tunnel->parms.iph.tos;
821 __be16 df = tiph->frag_off;
822 struct rtable *rt; /* Route to the other host */
823 struct net_device *tdev; /* Device to other host */
824 unsigned int max_headroom; /* The extra header space needed */
825 __be32 dst = tiph->daddr;
826 struct flowi4 fl4;
827 int mtu;
828 const struct in6_addr *addr6;
829 int addr_type;
830 u8 ttl;
831 u8 protocol = IPPROTO_IPV6;
832 int t_hlen = tunnel->hlen + sizeof(struct iphdr);
833
834 if (tos == 1)
835 tos = ipv6_get_dsfield(iph6);
836
837 /* ISATAP (RFC4214) - must come before 6to4 */
838 if (dev->priv_flags & IFF_ISATAP) {
839 struct neighbour *neigh = NULL;
840 bool do_tx_error = false;
841
842 if (skb_dst(skb))
843 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
844
845 if (!neigh) {
846 net_dbg_ratelimited("nexthop == NULL\n");
847 goto tx_error;
848 }
849
850 addr6 = (const struct in6_addr *)&neigh->primary_key;
851 addr_type = ipv6_addr_type(addr6);
852
853 if ((addr_type & IPV6_ADDR_UNICAST) &&
854 ipv6_addr_is_isatap(addr6))
855 dst = addr6->s6_addr32[3];
856 else
857 do_tx_error = true;
858
859 neigh_release(neigh);
860 if (do_tx_error)
861 goto tx_error;
862 }
863
864 if (!dst)
865 dst = try_6rd(tunnel, &iph6->daddr);
866
867 if (!dst) {
868 struct neighbour *neigh = NULL;
869 bool do_tx_error = false;
870
871 if (skb_dst(skb))
872 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
873
874 if (!neigh) {
875 net_dbg_ratelimited("nexthop == NULL\n");
876 goto tx_error;
877 }
878
879 addr6 = (const struct in6_addr *)&neigh->primary_key;
880 addr_type = ipv6_addr_type(addr6);
881
882 if (addr_type == IPV6_ADDR_ANY) {
883 addr6 = &ipv6_hdr(skb)->daddr;
884 addr_type = ipv6_addr_type(addr6);
885 }
886
887 if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
888 dst = addr6->s6_addr32[3];
889 else
890 do_tx_error = true;
891
892 neigh_release(neigh);
893 if (do_tx_error)
894 goto tx_error;
895 }
896
897 flowi4_init_output(&fl4, tunnel->parms.link, tunnel->fwmark,
898 RT_TOS(tos), RT_SCOPE_UNIVERSE, IPPROTO_IPV6,
899 0, dst, tiph->saddr, 0, 0,
900 sock_net_uid(tunnel->net, NULL));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901
David Brazdil0f672f62019-12-10 10:32:29 +0000902 rt = dst_cache_get_ip4(&tunnel->dst_cache, &fl4.saddr);
903 if (!rt) {
904 rt = ip_route_output_flow(tunnel->net, &fl4, NULL);
905 if (IS_ERR(rt)) {
906 dev->stats.tx_carrier_errors++;
907 goto tx_error_icmp;
908 }
909 dst_cache_set_ip4(&tunnel->dst_cache, &rt->dst, fl4.saddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000910 }
David Brazdil0f672f62019-12-10 10:32:29 +0000911
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000912 if (rt->rt_type != RTN_UNICAST) {
913 ip_rt_put(rt);
914 dev->stats.tx_carrier_errors++;
915 goto tx_error_icmp;
916 }
917 tdev = rt->dst.dev;
918
919 if (tdev == dev) {
920 ip_rt_put(rt);
921 dev->stats.collisions++;
922 goto tx_error;
923 }
924
925 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4)) {
926 ip_rt_put(rt);
927 goto tx_error;
928 }
929
930 if (df) {
931 mtu = dst_mtu(&rt->dst) - t_hlen;
932
933 if (mtu < 68) {
934 dev->stats.collisions++;
935 ip_rt_put(rt);
936 goto tx_error;
937 }
938
939 if (mtu < IPV6_MIN_MTU) {
940 mtu = IPV6_MIN_MTU;
941 df = 0;
942 }
943
944 if (tunnel->parms.iph.daddr)
Olivier Deprez0e641232021-09-23 10:07:05 +0200945 skb_dst_update_pmtu_no_confirm(skb, mtu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000946
947 if (skb->len > mtu && !skb_is_gso(skb)) {
948 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
949 ip_rt_put(rt);
950 goto tx_error;
951 }
952 }
953
954 if (tunnel->err_count > 0) {
955 if (time_before(jiffies,
956 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
957 tunnel->err_count--;
958 dst_link_failure(skb);
959 } else
960 tunnel->err_count = 0;
961 }
962
963 /*
964 * Okay, now see if we can stuff it in the buffer as-is.
965 */
966 max_headroom = LL_RESERVED_SPACE(tdev) + t_hlen;
967
968 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
969 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
970 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
971 if (!new_skb) {
972 ip_rt_put(rt);
973 dev->stats.tx_dropped++;
974 kfree_skb(skb);
975 return NETDEV_TX_OK;
976 }
977 if (skb->sk)
978 skb_set_owner_w(new_skb, skb->sk);
979 dev_kfree_skb(skb);
980 skb = new_skb;
981 iph6 = ipv6_hdr(skb);
982 }
983 ttl = tiph->ttl;
984 if (ttl == 0)
985 ttl = iph6->hop_limit;
986 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
987
988 if (ip_tunnel_encap(skb, tunnel, &protocol, &fl4) < 0) {
989 ip_rt_put(rt);
990 goto tx_error;
991 }
992
993 skb_set_inner_ipproto(skb, IPPROTO_IPV6);
994
995 iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol, tos, ttl,
996 df, !net_eq(tunnel->net, dev_net(dev)));
997 return NETDEV_TX_OK;
998
999tx_error_icmp:
1000 dst_link_failure(skb);
1001tx_error:
1002 kfree_skb(skb);
1003 dev->stats.tx_errors++;
1004 return NETDEV_TX_OK;
1005}
1006
1007static netdev_tx_t sit_tunnel_xmit__(struct sk_buff *skb,
1008 struct net_device *dev, u8 ipproto)
1009{
1010 struct ip_tunnel *tunnel = netdev_priv(dev);
1011 const struct iphdr *tiph = &tunnel->parms.iph;
1012
1013 if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP4))
1014 goto tx_error;
1015
1016 skb_set_inner_ipproto(skb, ipproto);
1017
1018 ip_tunnel_xmit(skb, dev, tiph, ipproto);
1019 return NETDEV_TX_OK;
1020tx_error:
1021 kfree_skb(skb);
1022 dev->stats.tx_errors++;
1023 return NETDEV_TX_OK;
1024}
1025
1026static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
1027 struct net_device *dev)
1028{
David Brazdil0f672f62019-12-10 10:32:29 +00001029 if (!pskb_inet_may_pull(skb))
1030 goto tx_err;
1031
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032 switch (skb->protocol) {
1033 case htons(ETH_P_IP):
1034 sit_tunnel_xmit__(skb, dev, IPPROTO_IPIP);
1035 break;
1036 case htons(ETH_P_IPV6):
1037 ipip6_tunnel_xmit(skb, dev);
1038 break;
1039#if IS_ENABLED(CONFIG_MPLS)
1040 case htons(ETH_P_MPLS_UC):
1041 sit_tunnel_xmit__(skb, dev, IPPROTO_MPLS);
1042 break;
1043#endif
1044 default:
1045 goto tx_err;
1046 }
1047
1048 return NETDEV_TX_OK;
1049
1050tx_err:
1051 dev->stats.tx_errors++;
1052 kfree_skb(skb);
1053 return NETDEV_TX_OK;
1054
1055}
1056
1057static void ipip6_tunnel_bind_dev(struct net_device *dev)
1058{
1059 struct net_device *tdev = NULL;
1060 struct ip_tunnel *tunnel;
1061 const struct iphdr *iph;
1062 struct flowi4 fl4;
1063
1064 tunnel = netdev_priv(dev);
1065 iph = &tunnel->parms.iph;
1066
1067 if (iph->daddr) {
1068 struct rtable *rt = ip_route_output_ports(tunnel->net, &fl4,
1069 NULL,
1070 iph->daddr, iph->saddr,
1071 0, 0,
1072 IPPROTO_IPV6,
1073 RT_TOS(iph->tos),
1074 tunnel->parms.link);
1075
1076 if (!IS_ERR(rt)) {
1077 tdev = rt->dst.dev;
1078 ip_rt_put(rt);
1079 }
1080 dev->flags |= IFF_POINTOPOINT;
1081 }
1082
1083 if (!tdev && tunnel->parms.link)
1084 tdev = __dev_get_by_index(tunnel->net, tunnel->parms.link);
1085
David Brazdil0f672f62019-12-10 10:32:29 +00001086 if (tdev && !netif_is_l3_master(tdev)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001087 int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1088
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089 dev->mtu = tdev->mtu - t_hlen;
1090 if (dev->mtu < IPV6_MIN_MTU)
1091 dev->mtu = IPV6_MIN_MTU;
1092 }
1093}
1094
1095static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p,
1096 __u32 fwmark)
1097{
1098 struct net *net = t->net;
1099 struct sit_net *sitn = net_generic(net, sit_net_id);
1100
1101 ipip6_tunnel_unlink(sitn, t);
1102 synchronize_net();
1103 t->parms.iph.saddr = p->iph.saddr;
1104 t->parms.iph.daddr = p->iph.daddr;
1105 memcpy(t->dev->dev_addr, &p->iph.saddr, 4);
1106 memcpy(t->dev->broadcast, &p->iph.daddr, 4);
1107 ipip6_tunnel_link(sitn, t);
1108 t->parms.iph.ttl = p->iph.ttl;
1109 t->parms.iph.tos = p->iph.tos;
1110 t->parms.iph.frag_off = p->iph.frag_off;
1111 if (t->parms.link != p->link || t->fwmark != fwmark) {
1112 t->parms.link = p->link;
1113 t->fwmark = fwmark;
1114 ipip6_tunnel_bind_dev(t->dev);
1115 }
1116 dst_cache_reset(&t->dst_cache);
1117 netdev_state_change(t->dev);
1118}
1119
1120#ifdef CONFIG_IPV6_SIT_6RD
1121static int ipip6_tunnel_update_6rd(struct ip_tunnel *t,
1122 struct ip_tunnel_6rd *ip6rd)
1123{
1124 struct in6_addr prefix;
1125 __be32 relay_prefix;
1126
1127 if (ip6rd->relay_prefixlen > 32 ||
1128 ip6rd->prefixlen + (32 - ip6rd->relay_prefixlen) > 64)
1129 return -EINVAL;
1130
1131 ipv6_addr_prefix(&prefix, &ip6rd->prefix, ip6rd->prefixlen);
1132 if (!ipv6_addr_equal(&prefix, &ip6rd->prefix))
1133 return -EINVAL;
1134 if (ip6rd->relay_prefixlen)
1135 relay_prefix = ip6rd->relay_prefix &
1136 htonl(0xffffffffUL <<
1137 (32 - ip6rd->relay_prefixlen));
1138 else
1139 relay_prefix = 0;
1140 if (relay_prefix != ip6rd->relay_prefix)
1141 return -EINVAL;
1142
1143 t->ip6rd.prefix = prefix;
1144 t->ip6rd.relay_prefix = relay_prefix;
1145 t->ip6rd.prefixlen = ip6rd->prefixlen;
1146 t->ip6rd.relay_prefixlen = ip6rd->relay_prefixlen;
1147 dst_cache_reset(&t->dst_cache);
1148 netdev_state_change(t->dev);
1149 return 0;
1150}
1151#endif
1152
1153static bool ipip6_valid_ip_proto(u8 ipproto)
1154{
1155 return ipproto == IPPROTO_IPV6 ||
1156 ipproto == IPPROTO_IPIP ||
1157#if IS_ENABLED(CONFIG_MPLS)
1158 ipproto == IPPROTO_MPLS ||
1159#endif
1160 ipproto == 0;
1161}
1162
1163static int
1164ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1165{
1166 int err = 0;
1167 struct ip_tunnel_parm p;
1168 struct ip_tunnel_prl prl;
1169 struct ip_tunnel *t = netdev_priv(dev);
1170 struct net *net = t->net;
1171 struct sit_net *sitn = net_generic(net, sit_net_id);
1172#ifdef CONFIG_IPV6_SIT_6RD
1173 struct ip_tunnel_6rd ip6rd;
1174#endif
1175
1176 switch (cmd) {
1177 case SIOCGETTUNNEL:
1178#ifdef CONFIG_IPV6_SIT_6RD
1179 case SIOCGET6RD:
1180#endif
1181 if (dev == sitn->fb_tunnel_dev) {
1182 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1183 err = -EFAULT;
1184 break;
1185 }
1186 t = ipip6_tunnel_locate(net, &p, 0);
1187 if (!t)
1188 t = netdev_priv(dev);
1189 }
1190
1191 err = -EFAULT;
1192 if (cmd == SIOCGETTUNNEL) {
1193 memcpy(&p, &t->parms, sizeof(p));
1194 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
1195 sizeof(p)))
1196 goto done;
1197#ifdef CONFIG_IPV6_SIT_6RD
1198 } else {
1199 ip6rd.prefix = t->ip6rd.prefix;
1200 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
1201 ip6rd.prefixlen = t->ip6rd.prefixlen;
1202 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
1203 if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
1204 sizeof(ip6rd)))
1205 goto done;
1206#endif
1207 }
1208 err = 0;
1209 break;
1210
1211 case SIOCADDTUNNEL:
1212 case SIOCCHGTUNNEL:
1213 err = -EPERM;
1214 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1215 goto done;
1216
1217 err = -EFAULT;
1218 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1219 goto done;
1220
1221 err = -EINVAL;
1222 if (!ipip6_valid_ip_proto(p.iph.protocol))
1223 goto done;
1224 if (p.iph.version != 4 ||
1225 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
1226 goto done;
1227 if (p.iph.ttl)
1228 p.iph.frag_off |= htons(IP_DF);
1229
1230 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
1231
1232 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1233 if (t) {
1234 if (t->dev != dev) {
1235 err = -EEXIST;
1236 break;
1237 }
1238 } else {
1239 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
1240 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
1241 err = -EINVAL;
1242 break;
1243 }
1244 t = netdev_priv(dev);
1245 }
1246
1247 ipip6_tunnel_update(t, &p, t->fwmark);
1248 }
1249
1250 if (t) {
1251 err = 0;
1252 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1253 err = -EFAULT;
1254 } else
1255 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1256 break;
1257
1258 case SIOCDELTUNNEL:
1259 err = -EPERM;
1260 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1261 goto done;
1262
1263 if (dev == sitn->fb_tunnel_dev) {
1264 err = -EFAULT;
1265 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1266 goto done;
1267 err = -ENOENT;
1268 t = ipip6_tunnel_locate(net, &p, 0);
1269 if (!t)
1270 goto done;
1271 err = -EPERM;
1272 if (t == netdev_priv(sitn->fb_tunnel_dev))
1273 goto done;
1274 dev = t->dev;
1275 }
1276 unregister_netdevice(dev);
1277 err = 0;
1278 break;
1279
1280 case SIOCGETPRL:
1281 err = -EINVAL;
1282 if (dev == sitn->fb_tunnel_dev)
1283 goto done;
1284 err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
1285 break;
1286
1287 case SIOCADDPRL:
1288 case SIOCDELPRL:
1289 case SIOCCHGPRL:
1290 err = -EPERM;
1291 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1292 goto done;
1293 err = -EINVAL;
1294 if (dev == sitn->fb_tunnel_dev)
1295 goto done;
1296 err = -EFAULT;
1297 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
1298 goto done;
1299
1300 switch (cmd) {
1301 case SIOCDELPRL:
1302 err = ipip6_tunnel_del_prl(t, &prl);
1303 break;
1304 case SIOCADDPRL:
1305 case SIOCCHGPRL:
1306 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
1307 break;
1308 }
1309 dst_cache_reset(&t->dst_cache);
1310 netdev_state_change(dev);
1311 break;
1312
1313#ifdef CONFIG_IPV6_SIT_6RD
1314 case SIOCADD6RD:
1315 case SIOCCHG6RD:
1316 case SIOCDEL6RD:
1317 err = -EPERM;
1318 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1319 goto done;
1320
1321 err = -EFAULT;
1322 if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
1323 sizeof(ip6rd)))
1324 goto done;
1325
1326 if (cmd != SIOCDEL6RD) {
1327 err = ipip6_tunnel_update_6rd(t, &ip6rd);
1328 if (err < 0)
1329 goto done;
1330 } else
1331 ipip6_tunnel_clone_6rd(dev, sitn);
1332
1333 err = 0;
1334 break;
1335#endif
1336
1337 default:
1338 err = -EINVAL;
1339 }
1340
1341done:
1342 return err;
1343}
1344
1345static const struct net_device_ops ipip6_netdev_ops = {
1346 .ndo_init = ipip6_tunnel_init,
1347 .ndo_uninit = ipip6_tunnel_uninit,
1348 .ndo_start_xmit = sit_tunnel_xmit,
1349 .ndo_do_ioctl = ipip6_tunnel_ioctl,
1350 .ndo_get_stats64 = ip_tunnel_get_stats64,
1351 .ndo_get_iflink = ip_tunnel_get_iflink,
1352};
1353
1354static void ipip6_dev_free(struct net_device *dev)
1355{
1356 struct ip_tunnel *tunnel = netdev_priv(dev);
1357
1358 dst_cache_destroy(&tunnel->dst_cache);
1359 free_percpu(dev->tstats);
1360}
1361
1362#define SIT_FEATURES (NETIF_F_SG | \
1363 NETIF_F_FRAGLIST | \
1364 NETIF_F_HIGHDMA | \
1365 NETIF_F_GSO_SOFTWARE | \
1366 NETIF_F_HW_CSUM)
1367
1368static void ipip6_tunnel_setup(struct net_device *dev)
1369{
1370 struct ip_tunnel *tunnel = netdev_priv(dev);
1371 int t_hlen = tunnel->hlen + sizeof(struct iphdr);
1372
1373 dev->netdev_ops = &ipip6_netdev_ops;
1374 dev->needs_free_netdev = true;
1375 dev->priv_destructor = ipip6_dev_free;
1376
1377 dev->type = ARPHRD_SIT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378 dev->mtu = ETH_DATA_LEN - t_hlen;
1379 dev->min_mtu = IPV6_MIN_MTU;
1380 dev->max_mtu = IP6_MAX_MTU - t_hlen;
1381 dev->flags = IFF_NOARP;
1382 netif_keep_dst(dev);
1383 dev->addr_len = 4;
1384 dev->features |= NETIF_F_LLTX;
1385 dev->features |= SIT_FEATURES;
1386 dev->hw_features |= SIT_FEATURES;
1387}
1388
1389static int ipip6_tunnel_init(struct net_device *dev)
1390{
1391 struct ip_tunnel *tunnel = netdev_priv(dev);
1392 int err;
1393
1394 tunnel->dev = dev;
1395 tunnel->net = dev_net(dev);
1396 strcpy(tunnel->parms.name, dev->name);
1397
1398 ipip6_tunnel_bind_dev(dev);
1399 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1400 if (!dev->tstats)
1401 return -ENOMEM;
1402
1403 err = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
1404 if (err) {
1405 free_percpu(dev->tstats);
1406 dev->tstats = NULL;
1407 return err;
1408 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001409 dev_hold(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001410 return 0;
1411}
1412
1413static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1414{
1415 struct ip_tunnel *tunnel = netdev_priv(dev);
1416 struct iphdr *iph = &tunnel->parms.iph;
1417 struct net *net = dev_net(dev);
1418 struct sit_net *sitn = net_generic(net, sit_net_id);
1419
1420 iph->version = 4;
1421 iph->protocol = IPPROTO_IPV6;
1422 iph->ihl = 5;
1423 iph->ttl = 64;
1424
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001425 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
1426}
1427
1428static int ipip6_validate(struct nlattr *tb[], struct nlattr *data[],
1429 struct netlink_ext_ack *extack)
1430{
1431 u8 proto;
1432
1433 if (!data || !data[IFLA_IPTUN_PROTO])
1434 return 0;
1435
1436 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1437 if (!ipip6_valid_ip_proto(proto))
1438 return -EINVAL;
1439
1440 return 0;
1441}
1442
1443static void ipip6_netlink_parms(struct nlattr *data[],
1444 struct ip_tunnel_parm *parms,
1445 __u32 *fwmark)
1446{
1447 memset(parms, 0, sizeof(*parms));
1448
1449 parms->iph.version = 4;
1450 parms->iph.protocol = IPPROTO_IPV6;
1451 parms->iph.ihl = 5;
1452 parms->iph.ttl = 64;
1453
1454 if (!data)
1455 return;
1456
1457 if (data[IFLA_IPTUN_LINK])
1458 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1459
1460 if (data[IFLA_IPTUN_LOCAL])
1461 parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
1462
1463 if (data[IFLA_IPTUN_REMOTE])
1464 parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
1465
1466 if (data[IFLA_IPTUN_TTL]) {
1467 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
1468 if (parms->iph.ttl)
1469 parms->iph.frag_off = htons(IP_DF);
1470 }
1471
1472 if (data[IFLA_IPTUN_TOS])
1473 parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
1474
1475 if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
1476 parms->iph.frag_off = htons(IP_DF);
1477
1478 if (data[IFLA_IPTUN_FLAGS])
1479 parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
1480
1481 if (data[IFLA_IPTUN_PROTO])
1482 parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1483
1484 if (data[IFLA_IPTUN_FWMARK])
1485 *fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]);
1486}
1487
1488/* This function returns true when ENCAP attributes are present in the nl msg */
1489static bool ipip6_netlink_encap_parms(struct nlattr *data[],
1490 struct ip_tunnel_encap *ipencap)
1491{
1492 bool ret = false;
1493
1494 memset(ipencap, 0, sizeof(*ipencap));
1495
1496 if (!data)
1497 return ret;
1498
1499 if (data[IFLA_IPTUN_ENCAP_TYPE]) {
1500 ret = true;
1501 ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
1502 }
1503
1504 if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
1505 ret = true;
1506 ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
1507 }
1508
1509 if (data[IFLA_IPTUN_ENCAP_SPORT]) {
1510 ret = true;
1511 ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
1512 }
1513
1514 if (data[IFLA_IPTUN_ENCAP_DPORT]) {
1515 ret = true;
1516 ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
1517 }
1518
1519 return ret;
1520}
1521
1522#ifdef CONFIG_IPV6_SIT_6RD
1523/* This function returns true when 6RD attributes are present in the nl msg */
1524static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
1525 struct ip_tunnel_6rd *ip6rd)
1526{
1527 bool ret = false;
1528 memset(ip6rd, 0, sizeof(*ip6rd));
1529
1530 if (!data)
1531 return ret;
1532
1533 if (data[IFLA_IPTUN_6RD_PREFIX]) {
1534 ret = true;
1535 ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
1536 }
1537
1538 if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
1539 ret = true;
1540 ip6rd->relay_prefix =
1541 nla_get_be32(data[IFLA_IPTUN_6RD_RELAY_PREFIX]);
1542 }
1543
1544 if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
1545 ret = true;
1546 ip6rd->prefixlen = nla_get_u16(data[IFLA_IPTUN_6RD_PREFIXLEN]);
1547 }
1548
1549 if (data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]) {
1550 ret = true;
1551 ip6rd->relay_prefixlen =
1552 nla_get_u16(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
1553 }
1554
1555 return ret;
1556}
1557#endif
1558
1559static int ipip6_newlink(struct net *src_net, struct net_device *dev,
1560 struct nlattr *tb[], struct nlattr *data[],
1561 struct netlink_ext_ack *extack)
1562{
1563 struct net *net = dev_net(dev);
1564 struct ip_tunnel *nt;
1565 struct ip_tunnel_encap ipencap;
1566#ifdef CONFIG_IPV6_SIT_6RD
1567 struct ip_tunnel_6rd ip6rd;
1568#endif
1569 int err;
1570
1571 nt = netdev_priv(dev);
1572
1573 if (ipip6_netlink_encap_parms(data, &ipencap)) {
1574 err = ip_tunnel_encap_setup(nt, &ipencap);
1575 if (err < 0)
1576 return err;
1577 }
1578
1579 ipip6_netlink_parms(data, &nt->parms, &nt->fwmark);
1580
1581 if (ipip6_tunnel_locate(net, &nt->parms, 0))
1582 return -EEXIST;
1583
1584 err = ipip6_tunnel_create(dev);
1585 if (err < 0)
1586 return err;
1587
1588 if (tb[IFLA_MTU]) {
1589 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
1590
1591 if (mtu >= IPV6_MIN_MTU &&
1592 mtu <= IP6_MAX_MTU - dev->hard_header_len)
1593 dev->mtu = mtu;
1594 }
1595
1596#ifdef CONFIG_IPV6_SIT_6RD
Olivier Deprez0e641232021-09-23 10:07:05 +02001597 if (ipip6_netlink_6rd_parms(data, &ip6rd)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001598 err = ipip6_tunnel_update_6rd(nt, &ip6rd);
Olivier Deprez0e641232021-09-23 10:07:05 +02001599 if (err < 0)
1600 unregister_netdevice_queue(dev, NULL);
1601 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001602#endif
1603
1604 return err;
1605}
1606
1607static int ipip6_changelink(struct net_device *dev, struct nlattr *tb[],
1608 struct nlattr *data[],
1609 struct netlink_ext_ack *extack)
1610{
1611 struct ip_tunnel *t = netdev_priv(dev);
1612 struct ip_tunnel_parm p;
1613 struct ip_tunnel_encap ipencap;
1614 struct net *net = t->net;
1615 struct sit_net *sitn = net_generic(net, sit_net_id);
1616#ifdef CONFIG_IPV6_SIT_6RD
1617 struct ip_tunnel_6rd ip6rd;
1618#endif
1619 __u32 fwmark = t->fwmark;
1620 int err;
1621
1622 if (dev == sitn->fb_tunnel_dev)
1623 return -EINVAL;
1624
1625 if (ipip6_netlink_encap_parms(data, &ipencap)) {
1626 err = ip_tunnel_encap_setup(t, &ipencap);
1627 if (err < 0)
1628 return err;
1629 }
1630
1631 ipip6_netlink_parms(data, &p, &fwmark);
1632
1633 if (((dev->flags & IFF_POINTOPOINT) && !p.iph.daddr) ||
1634 (!(dev->flags & IFF_POINTOPOINT) && p.iph.daddr))
1635 return -EINVAL;
1636
1637 t = ipip6_tunnel_locate(net, &p, 0);
1638
1639 if (t) {
1640 if (t->dev != dev)
1641 return -EEXIST;
1642 } else
1643 t = netdev_priv(dev);
1644
1645 ipip6_tunnel_update(t, &p, fwmark);
1646
1647#ifdef CONFIG_IPV6_SIT_6RD
1648 if (ipip6_netlink_6rd_parms(data, &ip6rd))
1649 return ipip6_tunnel_update_6rd(t, &ip6rd);
1650#endif
1651
1652 return 0;
1653}
1654
1655static size_t ipip6_get_size(const struct net_device *dev)
1656{
1657 return
1658 /* IFLA_IPTUN_LINK */
1659 nla_total_size(4) +
1660 /* IFLA_IPTUN_LOCAL */
1661 nla_total_size(4) +
1662 /* IFLA_IPTUN_REMOTE */
1663 nla_total_size(4) +
1664 /* IFLA_IPTUN_TTL */
1665 nla_total_size(1) +
1666 /* IFLA_IPTUN_TOS */
1667 nla_total_size(1) +
1668 /* IFLA_IPTUN_PMTUDISC */
1669 nla_total_size(1) +
1670 /* IFLA_IPTUN_FLAGS */
1671 nla_total_size(2) +
1672 /* IFLA_IPTUN_PROTO */
1673 nla_total_size(1) +
1674#ifdef CONFIG_IPV6_SIT_6RD
1675 /* IFLA_IPTUN_6RD_PREFIX */
1676 nla_total_size(sizeof(struct in6_addr)) +
1677 /* IFLA_IPTUN_6RD_RELAY_PREFIX */
1678 nla_total_size(4) +
1679 /* IFLA_IPTUN_6RD_PREFIXLEN */
1680 nla_total_size(2) +
1681 /* IFLA_IPTUN_6RD_RELAY_PREFIXLEN */
1682 nla_total_size(2) +
1683#endif
1684 /* IFLA_IPTUN_ENCAP_TYPE */
1685 nla_total_size(2) +
1686 /* IFLA_IPTUN_ENCAP_FLAGS */
1687 nla_total_size(2) +
1688 /* IFLA_IPTUN_ENCAP_SPORT */
1689 nla_total_size(2) +
1690 /* IFLA_IPTUN_ENCAP_DPORT */
1691 nla_total_size(2) +
1692 /* IFLA_IPTUN_FWMARK */
1693 nla_total_size(4) +
1694 0;
1695}
1696
1697static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1698{
1699 struct ip_tunnel *tunnel = netdev_priv(dev);
1700 struct ip_tunnel_parm *parm = &tunnel->parms;
1701
1702 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1703 nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
1704 nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
1705 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
1706 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
1707 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
1708 !!(parm->iph.frag_off & htons(IP_DF))) ||
1709 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->iph.protocol) ||
1710 nla_put_be16(skb, IFLA_IPTUN_FLAGS, parm->i_flags) ||
1711 nla_put_u32(skb, IFLA_IPTUN_FWMARK, tunnel->fwmark))
1712 goto nla_put_failure;
1713
1714#ifdef CONFIG_IPV6_SIT_6RD
1715 if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
1716 &tunnel->ip6rd.prefix) ||
1717 nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
1718 tunnel->ip6rd.relay_prefix) ||
1719 nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
1720 tunnel->ip6rd.prefixlen) ||
1721 nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
1722 tunnel->ip6rd.relay_prefixlen))
1723 goto nla_put_failure;
1724#endif
1725
1726 if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
1727 tunnel->encap.type) ||
1728 nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
1729 tunnel->encap.sport) ||
1730 nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
1731 tunnel->encap.dport) ||
1732 nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
1733 tunnel->encap.flags))
1734 goto nla_put_failure;
1735
1736 return 0;
1737
1738nla_put_failure:
1739 return -EMSGSIZE;
1740}
1741
1742static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
1743 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1744 [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
1745 [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
1746 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1747 [IFLA_IPTUN_TOS] = { .type = NLA_U8 },
1748 [IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
1749 [IFLA_IPTUN_FLAGS] = { .type = NLA_U16 },
1750 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1751#ifdef CONFIG_IPV6_SIT_6RD
1752 [IFLA_IPTUN_6RD_PREFIX] = { .len = sizeof(struct in6_addr) },
1753 [IFLA_IPTUN_6RD_RELAY_PREFIX] = { .type = NLA_U32 },
1754 [IFLA_IPTUN_6RD_PREFIXLEN] = { .type = NLA_U16 },
1755 [IFLA_IPTUN_6RD_RELAY_PREFIXLEN] = { .type = NLA_U16 },
1756#endif
1757 [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
1758 [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
1759 [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
1760 [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
1761 [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 },
1762};
1763
1764static void ipip6_dellink(struct net_device *dev, struct list_head *head)
1765{
1766 struct net *net = dev_net(dev);
1767 struct sit_net *sitn = net_generic(net, sit_net_id);
1768
1769 if (dev != sitn->fb_tunnel_dev)
1770 unregister_netdevice_queue(dev, head);
1771}
1772
1773static struct rtnl_link_ops sit_link_ops __read_mostly = {
1774 .kind = "sit",
1775 .maxtype = IFLA_IPTUN_MAX,
1776 .policy = ipip6_policy,
1777 .priv_size = sizeof(struct ip_tunnel),
1778 .setup = ipip6_tunnel_setup,
1779 .validate = ipip6_validate,
1780 .newlink = ipip6_newlink,
1781 .changelink = ipip6_changelink,
1782 .get_size = ipip6_get_size,
1783 .fill_info = ipip6_fill_info,
1784 .dellink = ipip6_dellink,
1785 .get_link_net = ip_tunnel_get_link_net,
1786};
1787
1788static struct xfrm_tunnel sit_handler __read_mostly = {
1789 .handler = ipip6_rcv,
1790 .err_handler = ipip6_err,
1791 .priority = 1,
1792};
1793
1794static struct xfrm_tunnel ipip_handler __read_mostly = {
1795 .handler = ipip_rcv,
1796 .err_handler = ipip6_err,
1797 .priority = 2,
1798};
1799
1800#if IS_ENABLED(CONFIG_MPLS)
1801static struct xfrm_tunnel mplsip_handler __read_mostly = {
1802 .handler = mplsip_rcv,
1803 .err_handler = ipip6_err,
1804 .priority = 2,
1805};
1806#endif
1807
1808static void __net_exit sit_destroy_tunnels(struct net *net,
1809 struct list_head *head)
1810{
1811 struct sit_net *sitn = net_generic(net, sit_net_id);
1812 struct net_device *dev, *aux;
1813 int prio;
1814
1815 for_each_netdev_safe(net, dev, aux)
1816 if (dev->rtnl_link_ops == &sit_link_ops)
1817 unregister_netdevice_queue(dev, head);
1818
Olivier Deprez0e641232021-09-23 10:07:05 +02001819 for (prio = 0; prio < 4; prio++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001820 int h;
Olivier Deprez0e641232021-09-23 10:07:05 +02001821 for (h = 0; h < (prio ? IP6_SIT_HASH_SIZE : 1); h++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001822 struct ip_tunnel *t;
1823
1824 t = rtnl_dereference(sitn->tunnels[prio][h]);
1825 while (t) {
1826 /* If dev is in the same netns, it has already
1827 * been added to the list by the previous loop.
1828 */
1829 if (!net_eq(dev_net(t->dev), net))
1830 unregister_netdevice_queue(t->dev,
1831 head);
1832 t = rtnl_dereference(t->next);
1833 }
1834 }
1835 }
1836}
1837
1838static int __net_init sit_init_net(struct net *net)
1839{
1840 struct sit_net *sitn = net_generic(net, sit_net_id);
1841 struct ip_tunnel *t;
1842 int err;
1843
1844 sitn->tunnels[0] = sitn->tunnels_wc;
1845 sitn->tunnels[1] = sitn->tunnels_l;
1846 sitn->tunnels[2] = sitn->tunnels_r;
1847 sitn->tunnels[3] = sitn->tunnels_r_l;
1848
1849 if (!net_has_fallback_tunnels(net))
1850 return 0;
1851
1852 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1853 NET_NAME_UNKNOWN,
1854 ipip6_tunnel_setup);
1855 if (!sitn->fb_tunnel_dev) {
1856 err = -ENOMEM;
1857 goto err_alloc_dev;
1858 }
1859 dev_net_set(sitn->fb_tunnel_dev, net);
1860 sitn->fb_tunnel_dev->rtnl_link_ops = &sit_link_ops;
1861 /* FB netdevice is special: we have one, and only one per netns.
1862 * Allowing to move it to another netns is clearly unsafe.
1863 */
1864 sitn->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1865
1866 err = register_netdev(sitn->fb_tunnel_dev);
1867 if (err)
1868 goto err_reg_dev;
1869
1870 ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
1871 ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
1872
1873 t = netdev_priv(sitn->fb_tunnel_dev);
1874
1875 strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
1876 return 0;
1877
1878err_reg_dev:
1879 ipip6_dev_free(sitn->fb_tunnel_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001880 free_netdev(sitn->fb_tunnel_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001881err_alloc_dev:
1882 return err;
1883}
1884
1885static void __net_exit sit_exit_batch_net(struct list_head *net_list)
1886{
1887 LIST_HEAD(list);
1888 struct net *net;
1889
1890 rtnl_lock();
1891 list_for_each_entry(net, net_list, exit_list)
1892 sit_destroy_tunnels(net, &list);
1893
1894 unregister_netdevice_many(&list);
1895 rtnl_unlock();
1896}
1897
1898static struct pernet_operations sit_net_ops = {
1899 .init = sit_init_net,
1900 .exit_batch = sit_exit_batch_net,
1901 .id = &sit_net_id,
1902 .size = sizeof(struct sit_net),
1903};
1904
1905static void __exit sit_cleanup(void)
1906{
1907 rtnl_link_unregister(&sit_link_ops);
1908 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1909 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
1910#if IS_ENABLED(CONFIG_MPLS)
1911 xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1912#endif
1913
1914 unregister_pernet_device(&sit_net_ops);
1915 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1916}
1917
1918static int __init sit_init(void)
1919{
1920 int err;
1921
1922 pr_info("IPv6, IPv4 and MPLS over IPv4 tunneling driver\n");
1923
1924 err = register_pernet_device(&sit_net_ops);
1925 if (err < 0)
1926 return err;
1927 err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
1928 if (err < 0) {
1929 pr_info("%s: can't register ip6ip4\n", __func__);
1930 goto xfrm_tunnel_failed;
1931 }
1932 err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
1933 if (err < 0) {
1934 pr_info("%s: can't register ip4ip4\n", __func__);
1935 goto xfrm_tunnel4_failed;
1936 }
1937#if IS_ENABLED(CONFIG_MPLS)
1938 err = xfrm4_tunnel_register(&mplsip_handler, AF_MPLS);
1939 if (err < 0) {
1940 pr_info("%s: can't register mplsip\n", __func__);
1941 goto xfrm_tunnel_mpls_failed;
1942 }
1943#endif
1944 err = rtnl_link_register(&sit_link_ops);
1945 if (err < 0)
1946 goto rtnl_link_failed;
1947
1948out:
1949 return err;
1950
1951rtnl_link_failed:
1952#if IS_ENABLED(CONFIG_MPLS)
1953 xfrm4_tunnel_deregister(&mplsip_handler, AF_MPLS);
1954xfrm_tunnel_mpls_failed:
1955#endif
1956 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
1957xfrm_tunnel4_failed:
1958 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1959xfrm_tunnel_failed:
1960 unregister_pernet_device(&sit_net_ops);
1961 goto out;
1962}
1963
1964module_init(sit_init);
1965module_exit(sit_cleanup);
1966MODULE_LICENSE("GPL");
1967MODULE_ALIAS_RTNL_LINK("sit");
1968MODULE_ALIAS_NETDEV("sit0");