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