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