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