GNU Linux-libre 5.4.207-gnu1
[releases.git] / net / ipv6 / ip6_vti.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      IPv6 virtual tunneling interface
4  *
5  *      Copyright (C) 2013 secunet Security Networks AG
6  *
7  *      Author:
8  *      Steffen Klassert <steffen.klassert@secunet.com>
9  *
10  *      Based on:
11  *      net/ipv6/ip6_tunnel.c
12  */
13
14 #include <linux/module.h>
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/sockios.h>
19 #include <linux/icmp.h>
20 #include <linux/if.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/net.h>
24 #include <linux/in6.h>
25 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <linux/icmpv6.h>
28 #include <linux/init.h>
29 #include <linux/route.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/netfilter_ipv6.h>
32 #include <linux/slab.h>
33 #include <linux/hash.h>
34
35 #include <linux/uaccess.h>
36 #include <linux/atomic.h>
37
38 #include <net/icmp.h>
39 #include <net/ip.h>
40 #include <net/ip_tunnels.h>
41 #include <net/ipv6.h>
42 #include <net/ip6_route.h>
43 #include <net/addrconf.h>
44 #include <net/ip6_tunnel.h>
45 #include <net/xfrm.h>
46 #include <net/net_namespace.h>
47 #include <net/netns/generic.h>
48 #include <linux/etherdevice.h>
49
50 #define IP6_VTI_HASH_SIZE_SHIFT  5
51 #define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
52
53 static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
54 {
55         u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
56
57         return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
58 }
59
60 static int vti6_dev_init(struct net_device *dev);
61 static void vti6_dev_setup(struct net_device *dev);
62 static struct rtnl_link_ops vti6_link_ops __read_mostly;
63
64 static unsigned int vti6_net_id __read_mostly;
65 struct vti6_net {
66         /* the vti6 tunnel fallback device */
67         struct net_device *fb_tnl_dev;
68         /* lists for storing tunnels in use */
69         struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
70         struct ip6_tnl __rcu *tnls_wc[1];
71         struct ip6_tnl __rcu **tnls[2];
72 };
73
74 #define for_each_vti6_tunnel_rcu(start) \
75         for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
76
77 /**
78  * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
79  *   @net: network namespace
80  *   @remote: the address of the tunnel exit-point
81  *   @local: the address of the tunnel entry-point
82  *
83  * Return:
84  *   tunnel matching given end-points if found,
85  *   else fallback tunnel if its device is up,
86  *   else %NULL
87  **/
88 static struct ip6_tnl *
89 vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
90                 const struct in6_addr *local)
91 {
92         unsigned int hash = HASH(remote, local);
93         struct ip6_tnl *t;
94         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
95         struct in6_addr any;
96
97         for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
98                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
99                     ipv6_addr_equal(remote, &t->parms.raddr) &&
100                     (t->dev->flags & IFF_UP))
101                         return t;
102         }
103
104         memset(&any, 0, sizeof(any));
105         hash = HASH(&any, local);
106         for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
107                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
108                     (t->dev->flags & IFF_UP))
109                         return t;
110         }
111
112         hash = HASH(remote, &any);
113         for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
114                 if (ipv6_addr_equal(remote, &t->parms.raddr) &&
115                     (t->dev->flags & IFF_UP))
116                         return t;
117         }
118
119         t = rcu_dereference(ip6n->tnls_wc[0]);
120         if (t && (t->dev->flags & IFF_UP))
121                 return t;
122
123         return NULL;
124 }
125
126 /**
127  * vti6_tnl_bucket - get head of list matching given tunnel parameters
128  *   @p: parameters containing tunnel end-points
129  *
130  * Description:
131  *   vti6_tnl_bucket() returns the head of the list matching the
132  *   &struct in6_addr entries laddr and raddr in @p.
133  *
134  * Return: head of IPv6 tunnel list
135  **/
136 static struct ip6_tnl __rcu **
137 vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
138 {
139         const struct in6_addr *remote = &p->raddr;
140         const struct in6_addr *local = &p->laddr;
141         unsigned int h = 0;
142         int prio = 0;
143
144         if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
145                 prio = 1;
146                 h = HASH(remote, local);
147         }
148         return &ip6n->tnls[prio][h];
149 }
150
151 static void
152 vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
153 {
154         struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
155
156         rcu_assign_pointer(t->next , rtnl_dereference(*tp));
157         rcu_assign_pointer(*tp, t);
158 }
159
160 static void
161 vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
162 {
163         struct ip6_tnl __rcu **tp;
164         struct ip6_tnl *iter;
165
166         for (tp = vti6_tnl_bucket(ip6n, &t->parms);
167              (iter = rtnl_dereference(*tp)) != NULL;
168              tp = &iter->next) {
169                 if (t == iter) {
170                         rcu_assign_pointer(*tp, t->next);
171                         break;
172                 }
173         }
174 }
175
176 static void vti6_dev_free(struct net_device *dev)
177 {
178         free_percpu(dev->tstats);
179 }
180
181 static int vti6_tnl_create2(struct net_device *dev)
182 {
183         struct ip6_tnl *t = netdev_priv(dev);
184         struct net *net = dev_net(dev);
185         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
186         int err;
187
188         dev->rtnl_link_ops = &vti6_link_ops;
189         err = register_netdevice(dev);
190         if (err < 0)
191                 goto out;
192
193         strcpy(t->parms.name, dev->name);
194
195         vti6_tnl_link(ip6n, t);
196
197         return 0;
198
199 out:
200         return err;
201 }
202
203 static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
204 {
205         struct net_device *dev;
206         struct ip6_tnl *t;
207         char name[IFNAMSIZ];
208         int err;
209
210         if (p->name[0]) {
211                 if (!dev_valid_name(p->name))
212                         goto failed;
213                 strlcpy(name, p->name, IFNAMSIZ);
214         } else {
215                 sprintf(name, "ip6_vti%%d");
216         }
217
218         dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
219         if (!dev)
220                 goto failed;
221
222         dev_net_set(dev, net);
223
224         t = netdev_priv(dev);
225         t->parms = *p;
226         t->net = dev_net(dev);
227
228         err = vti6_tnl_create2(dev);
229         if (err < 0)
230                 goto failed_free;
231
232         return t;
233
234 failed_free:
235         free_netdev(dev);
236 failed:
237         return NULL;
238 }
239
240 /**
241  * vti6_locate - find or create tunnel matching given parameters
242  *   @net: network namespace
243  *   @p: tunnel parameters
244  *   @create: != 0 if allowed to create new tunnel if no match found
245  *
246  * Description:
247  *   vti6_locate() first tries to locate an existing tunnel
248  *   based on @parms. If this is unsuccessful, but @create is set a new
249  *   tunnel device is created and registered for use.
250  *
251  * Return:
252  *   matching tunnel or NULL
253  **/
254 static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
255                                    int create)
256 {
257         const struct in6_addr *remote = &p->raddr;
258         const struct in6_addr *local = &p->laddr;
259         struct ip6_tnl __rcu **tp;
260         struct ip6_tnl *t;
261         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
262
263         for (tp = vti6_tnl_bucket(ip6n, p);
264              (t = rtnl_dereference(*tp)) != NULL;
265              tp = &t->next) {
266                 if (ipv6_addr_equal(local, &t->parms.laddr) &&
267                     ipv6_addr_equal(remote, &t->parms.raddr)) {
268                         if (create)
269                                 return NULL;
270
271                         return t;
272                 }
273         }
274         if (!create)
275                 return NULL;
276         return vti6_tnl_create(net, p);
277 }
278
279 /**
280  * vti6_dev_uninit - tunnel device uninitializer
281  *   @dev: the device to be destroyed
282  *
283  * Description:
284  *   vti6_dev_uninit() removes tunnel from its list
285  **/
286 static void vti6_dev_uninit(struct net_device *dev)
287 {
288         struct ip6_tnl *t = netdev_priv(dev);
289         struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
290
291         if (dev == ip6n->fb_tnl_dev)
292                 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
293         else
294                 vti6_tnl_unlink(ip6n, t);
295         dev_put(dev);
296 }
297
298 static int vti6_rcv(struct sk_buff *skb)
299 {
300         struct ip6_tnl *t;
301         const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
302
303         rcu_read_lock();
304         t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
305         if (t) {
306                 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
307                         rcu_read_unlock();
308                         goto discard;
309                 }
310
311                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
312                         rcu_read_unlock();
313                         goto discard;
314                 }
315
316                 ipv6h = ipv6_hdr(skb);
317                 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
318                         t->dev->stats.rx_dropped++;
319                         rcu_read_unlock();
320                         goto discard;
321                 }
322
323                 rcu_read_unlock();
324
325                 return xfrm6_rcv_tnl(skb, t);
326         }
327         rcu_read_unlock();
328         return -EINVAL;
329 discard:
330         kfree_skb(skb);
331         return 0;
332 }
333
334 static int vti6_rcv_cb(struct sk_buff *skb, int err)
335 {
336         unsigned short family;
337         struct net_device *dev;
338         struct pcpu_sw_netstats *tstats;
339         struct xfrm_state *x;
340         const struct xfrm_mode *inner_mode;
341         struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
342         u32 orig_mark = skb->mark;
343         int ret;
344
345         if (!t)
346                 return 1;
347
348         dev = t->dev;
349
350         if (err) {
351                 dev->stats.rx_errors++;
352                 dev->stats.rx_dropped++;
353
354                 return 0;
355         }
356
357         x = xfrm_input_state(skb);
358
359         inner_mode = &x->inner_mode;
360
361         if (x->sel.family == AF_UNSPEC) {
362                 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
363                 if (inner_mode == NULL) {
364                         XFRM_INC_STATS(dev_net(skb->dev),
365                                        LINUX_MIB_XFRMINSTATEMODEERROR);
366                         return -EINVAL;
367                 }
368         }
369
370         family = inner_mode->family;
371
372         skb->mark = be32_to_cpu(t->parms.i_key);
373         ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
374         skb->mark = orig_mark;
375
376         if (!ret)
377                 return -EPERM;
378
379         skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
380         skb->dev = dev;
381
382         tstats = this_cpu_ptr(dev->tstats);
383         u64_stats_update_begin(&tstats->syncp);
384         tstats->rx_packets++;
385         tstats->rx_bytes += skb->len;
386         u64_stats_update_end(&tstats->syncp);
387
388         return 0;
389 }
390
391 /**
392  * vti6_addr_conflict - compare packet addresses to tunnel's own
393  *   @t: the outgoing tunnel device
394  *   @hdr: IPv6 header from the incoming packet
395  *
396  * Description:
397  *   Avoid trivial tunneling loop by checking that tunnel exit-point
398  *   doesn't match source of incoming packet.
399  *
400  * Return:
401  *   1 if conflict,
402  *   0 else
403  **/
404 static inline bool
405 vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
406 {
407         return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
408 }
409
410 static bool vti6_state_check(const struct xfrm_state *x,
411                              const struct in6_addr *dst,
412                              const struct in6_addr *src)
413 {
414         xfrm_address_t *daddr = (xfrm_address_t *)dst;
415         xfrm_address_t *saddr = (xfrm_address_t *)src;
416
417         /* if there is no transform then this tunnel is not functional.
418          * Or if the xfrm is not mode tunnel.
419          */
420         if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
421             x->props.family != AF_INET6)
422                 return false;
423
424         if (ipv6_addr_any(dst))
425                 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
426
427         if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
428                 return false;
429
430         return true;
431 }
432
433 /**
434  * vti6_xmit - send a packet
435  *   @skb: the outgoing socket buffer
436  *   @dev: the outgoing tunnel device
437  *   @fl: the flow informations for the xfrm_lookup
438  **/
439 static int
440 vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
441 {
442         struct ip6_tnl *t = netdev_priv(dev);
443         struct net_device_stats *stats = &t->dev->stats;
444         struct dst_entry *dst = skb_dst(skb);
445         struct net_device *tdev;
446         struct xfrm_state *x;
447         int pkt_len = skb->len;
448         int err = -1;
449         int mtu;
450
451         if (!dst) {
452                 switch (skb->protocol) {
453                 case htons(ETH_P_IP): {
454                         struct rtable *rt;
455
456                         fl->u.ip4.flowi4_oif = dev->ifindex;
457                         fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
458                         rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
459                         if (IS_ERR(rt))
460                                 goto tx_err_link_failure;
461                         dst = &rt->dst;
462                         skb_dst_set(skb, dst);
463                         break;
464                 }
465                 case htons(ETH_P_IPV6):
466                         fl->u.ip6.flowi6_oif = dev->ifindex;
467                         fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
468                         dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
469                         if (dst->error) {
470                                 dst_release(dst);
471                                 dst = NULL;
472                                 goto tx_err_link_failure;
473                         }
474                         skb_dst_set(skb, dst);
475                         break;
476                 default:
477                         goto tx_err_link_failure;
478                 }
479         }
480
481         dst_hold(dst);
482         dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
483         if (IS_ERR(dst)) {
484                 err = PTR_ERR(dst);
485                 dst = NULL;
486                 goto tx_err_link_failure;
487         }
488
489         x = dst->xfrm;
490         if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
491                 goto tx_err_link_failure;
492
493         if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
494                               (const struct in6_addr *)&x->id.daddr))
495                 goto tx_err_link_failure;
496
497         tdev = dst->dev;
498
499         if (tdev == dev) {
500                 stats->collisions++;
501                 net_warn_ratelimited("%s: Local routing loop detected!\n",
502                                      t->parms.name);
503                 goto tx_err_dst_release;
504         }
505
506         mtu = dst_mtu(dst);
507         if (skb->len > mtu) {
508                 skb_dst_update_pmtu_no_confirm(skb, mtu);
509
510                 if (skb->protocol == htons(ETH_P_IPV6)) {
511                         if (mtu < IPV6_MIN_MTU)
512                                 mtu = IPV6_MIN_MTU;
513
514                         icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
515                 } else {
516                         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
517                                   htonl(mtu));
518                 }
519
520                 err = -EMSGSIZE;
521                 goto tx_err_dst_release;
522         }
523
524         skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
525         skb_dst_set(skb, dst);
526         skb->dev = skb_dst(skb)->dev;
527
528         err = dst_output(t->net, skb->sk, skb);
529         if (net_xmit_eval(err) == 0)
530                 err = pkt_len;
531         iptunnel_xmit_stats(dev, err);
532
533         return 0;
534 tx_err_link_failure:
535         stats->tx_carrier_errors++;
536         dst_link_failure(skb);
537 tx_err_dst_release:
538         dst_release(dst);
539         return err;
540 }
541
542 static netdev_tx_t
543 vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
544 {
545         struct ip6_tnl *t = netdev_priv(dev);
546         struct net_device_stats *stats = &t->dev->stats;
547         struct flowi fl;
548         int ret;
549
550         if (!pskb_inet_may_pull(skb))
551                 goto tx_err;
552
553         memset(&fl, 0, sizeof(fl));
554
555         switch (skb->protocol) {
556         case htons(ETH_P_IPV6):
557                 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
558                     vti6_addr_conflict(t, ipv6_hdr(skb)))
559                         goto tx_err;
560
561                 xfrm_decode_session(skb, &fl, AF_INET6);
562                 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
563                 break;
564         case htons(ETH_P_IP):
565                 xfrm_decode_session(skb, &fl, AF_INET);
566                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
567                 break;
568         default:
569                 goto tx_err;
570         }
571
572         /* override mark with tunnel output key */
573         fl.flowi_mark = be32_to_cpu(t->parms.o_key);
574
575         ret = vti6_xmit(skb, dev, &fl);
576         if (ret < 0)
577                 goto tx_err;
578
579         return NETDEV_TX_OK;
580
581 tx_err:
582         stats->tx_errors++;
583         stats->tx_dropped++;
584         kfree_skb(skb);
585         return NETDEV_TX_OK;
586 }
587
588 static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
589                     u8 type, u8 code, int offset, __be32 info)
590 {
591         __be32 spi;
592         __u32 mark;
593         struct xfrm_state *x;
594         struct ip6_tnl *t;
595         struct ip_esp_hdr *esph;
596         struct ip_auth_hdr *ah;
597         struct ip_comp_hdr *ipch;
598         struct net *net = dev_net(skb->dev);
599         const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
600         int protocol = iph->nexthdr;
601
602         t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
603         if (!t)
604                 return -1;
605
606         mark = be32_to_cpu(t->parms.o_key);
607
608         switch (protocol) {
609         case IPPROTO_ESP:
610                 esph = (struct ip_esp_hdr *)(skb->data + offset);
611                 spi = esph->spi;
612                 break;
613         case IPPROTO_AH:
614                 ah = (struct ip_auth_hdr *)(skb->data + offset);
615                 spi = ah->spi;
616                 break;
617         case IPPROTO_COMP:
618                 ipch = (struct ip_comp_hdr *)(skb->data + offset);
619                 spi = htonl(ntohs(ipch->cpi));
620                 break;
621         default:
622                 return 0;
623         }
624
625         if (type != ICMPV6_PKT_TOOBIG &&
626             type != NDISC_REDIRECT)
627                 return 0;
628
629         x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
630                               spi, protocol, AF_INET6);
631         if (!x)
632                 return 0;
633
634         if (type == NDISC_REDIRECT)
635                 ip6_redirect(skb, net, skb->dev->ifindex, 0,
636                              sock_net_uid(net, NULL));
637         else
638                 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
639         xfrm_state_put(x);
640
641         return 0;
642 }
643
644 static void vti6_link_config(struct ip6_tnl *t, bool keep_mtu)
645 {
646         struct net_device *dev = t->dev;
647         struct __ip6_tnl_parm *p = &t->parms;
648         struct net_device *tdev = NULL;
649         int mtu;
650
651         memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
652         memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
653
654         p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
655                       IP6_TNL_F_CAP_PER_PACKET);
656         p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
657
658         if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
659                 dev->flags |= IFF_POINTOPOINT;
660         else
661                 dev->flags &= ~IFF_POINTOPOINT;
662
663         if (keep_mtu && dev->mtu) {
664                 dev->mtu = clamp(dev->mtu, dev->min_mtu, dev->max_mtu);
665                 return;
666         }
667
668         if (p->flags & IP6_TNL_F_CAP_XMIT) {
669                 int strict = (ipv6_addr_type(&p->raddr) &
670                               (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
671                 struct rt6_info *rt = rt6_lookup(t->net,
672                                                  &p->raddr, &p->laddr,
673                                                  p->link, NULL, strict);
674
675                 if (rt)
676                         tdev = rt->dst.dev;
677                 ip6_rt_put(rt);
678         }
679
680         if (!tdev && p->link)
681                 tdev = __dev_get_by_index(t->net, p->link);
682
683         if (tdev)
684                 mtu = tdev->mtu - sizeof(struct ipv6hdr);
685         else
686                 mtu = ETH_DATA_LEN - LL_MAX_HEADER - sizeof(struct ipv6hdr);
687
688         dev->mtu = max_t(int, mtu, IPV4_MIN_MTU);
689 }
690
691 /**
692  * vti6_tnl_change - update the tunnel parameters
693  *   @t: tunnel to be changed
694  *   @p: tunnel configuration parameters
695  *   @keep_mtu: MTU was set from userspace, don't re-compute it
696  *
697  * Description:
698  *   vti6_tnl_change() updates the tunnel parameters
699  **/
700 static int
701 vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
702                 bool keep_mtu)
703 {
704         t->parms.laddr = p->laddr;
705         t->parms.raddr = p->raddr;
706         t->parms.link = p->link;
707         t->parms.i_key = p->i_key;
708         t->parms.o_key = p->o_key;
709         t->parms.proto = p->proto;
710         t->parms.fwmark = p->fwmark;
711         dst_cache_reset(&t->dst_cache);
712         vti6_link_config(t, keep_mtu);
713         return 0;
714 }
715
716 static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p,
717                        bool keep_mtu)
718 {
719         struct net *net = dev_net(t->dev);
720         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
721         int err;
722
723         vti6_tnl_unlink(ip6n, t);
724         synchronize_net();
725         err = vti6_tnl_change(t, p, keep_mtu);
726         vti6_tnl_link(ip6n, t);
727         netdev_state_change(t->dev);
728         return err;
729 }
730
731 static void
732 vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
733 {
734         p->laddr = u->laddr;
735         p->raddr = u->raddr;
736         p->link = u->link;
737         p->i_key = u->i_key;
738         p->o_key = u->o_key;
739         p->proto = u->proto;
740
741         memcpy(p->name, u->name, sizeof(u->name));
742 }
743
744 static void
745 vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
746 {
747         u->laddr = p->laddr;
748         u->raddr = p->raddr;
749         u->link = p->link;
750         u->i_key = p->i_key;
751         u->o_key = p->o_key;
752         if (u->i_key)
753                 u->i_flags |= GRE_KEY;
754         if (u->o_key)
755                 u->o_flags |= GRE_KEY;
756         u->proto = p->proto;
757
758         memcpy(u->name, p->name, sizeof(u->name));
759 }
760
761 /**
762  * vti6_ioctl - configure vti6 tunnels from userspace
763  *   @dev: virtual device associated with tunnel
764  *   @ifr: parameters passed from userspace
765  *   @cmd: command to be performed
766  *
767  * Description:
768  *   vti6_ioctl() is used for managing vti6 tunnels
769  *   from userspace.
770  *
771  *   The possible commands are the following:
772  *     %SIOCGETTUNNEL: get tunnel parameters for device
773  *     %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
774  *     %SIOCCHGTUNNEL: change tunnel parameters to those given
775  *     %SIOCDELTUNNEL: delete tunnel
776  *
777  *   The fallback device "ip6_vti0", created during module
778  *   initialization, can be used for creating other tunnel devices.
779  *
780  * Return:
781  *   0 on success,
782  *   %-EFAULT if unable to copy data to or from userspace,
783  *   %-EPERM if current process hasn't %CAP_NET_ADMIN set
784  *   %-EINVAL if passed tunnel parameters are invalid,
785  *   %-EEXIST if changing a tunnel's parameters would cause a conflict
786  *   %-ENODEV if attempting to change or delete a nonexisting device
787  **/
788 static int
789 vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
790 {
791         int err = 0;
792         struct ip6_tnl_parm2 p;
793         struct __ip6_tnl_parm p1;
794         struct ip6_tnl *t = NULL;
795         struct net *net = dev_net(dev);
796         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
797
798         memset(&p1, 0, sizeof(p1));
799
800         switch (cmd) {
801         case SIOCGETTUNNEL:
802                 if (dev == ip6n->fb_tnl_dev) {
803                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
804                                 err = -EFAULT;
805                                 break;
806                         }
807                         vti6_parm_from_user(&p1, &p);
808                         t = vti6_locate(net, &p1, 0);
809                 } else {
810                         memset(&p, 0, sizeof(p));
811                 }
812                 if (!t)
813                         t = netdev_priv(dev);
814                 vti6_parm_to_user(&p, &t->parms);
815                 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
816                         err = -EFAULT;
817                 break;
818         case SIOCADDTUNNEL:
819         case SIOCCHGTUNNEL:
820                 err = -EPERM;
821                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
822                         break;
823                 err = -EFAULT;
824                 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
825                         break;
826                 err = -EINVAL;
827                 if (p.proto != IPPROTO_IPV6  && p.proto != 0)
828                         break;
829                 vti6_parm_from_user(&p1, &p);
830                 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
831                 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
832                         if (t) {
833                                 if (t->dev != dev) {
834                                         err = -EEXIST;
835                                         break;
836                                 }
837                         } else
838                                 t = netdev_priv(dev);
839
840                         err = vti6_update(t, &p1, false);
841                 }
842                 if (t) {
843                         err = 0;
844                         vti6_parm_to_user(&p, &t->parms);
845                         if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
846                                 err = -EFAULT;
847
848                 } else
849                         err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
850                 break;
851         case SIOCDELTUNNEL:
852                 err = -EPERM;
853                 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
854                         break;
855
856                 if (dev == ip6n->fb_tnl_dev) {
857                         err = -EFAULT;
858                         if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
859                                 break;
860                         err = -ENOENT;
861                         vti6_parm_from_user(&p1, &p);
862                         t = vti6_locate(net, &p1, 0);
863                         if (!t)
864                                 break;
865                         err = -EPERM;
866                         if (t->dev == ip6n->fb_tnl_dev)
867                                 break;
868                         dev = t->dev;
869                 }
870                 err = 0;
871                 unregister_netdevice(dev);
872                 break;
873         default:
874                 err = -EINVAL;
875         }
876         return err;
877 }
878
879 static const struct net_device_ops vti6_netdev_ops = {
880         .ndo_init       = vti6_dev_init,
881         .ndo_uninit     = vti6_dev_uninit,
882         .ndo_start_xmit = vti6_tnl_xmit,
883         .ndo_do_ioctl   = vti6_ioctl,
884         .ndo_get_stats64 = ip_tunnel_get_stats64,
885         .ndo_get_iflink = ip6_tnl_get_iflink,
886 };
887
888 /**
889  * vti6_dev_setup - setup virtual tunnel device
890  *   @dev: virtual device associated with tunnel
891  *
892  * Description:
893  *   Initialize function pointers and device parameters
894  **/
895 static void vti6_dev_setup(struct net_device *dev)
896 {
897         dev->netdev_ops = &vti6_netdev_ops;
898         dev->needs_free_netdev = true;
899         dev->priv_destructor = vti6_dev_free;
900
901         dev->type = ARPHRD_TUNNEL6;
902         dev->min_mtu = IPV4_MIN_MTU;
903         dev->max_mtu = IP_MAX_MTU - sizeof(struct ipv6hdr);
904         dev->flags |= IFF_NOARP;
905         dev->addr_len = sizeof(struct in6_addr);
906         netif_keep_dst(dev);
907         /* This perm addr will be used as interface identifier by IPv6 */
908         dev->addr_assign_type = NET_ADDR_RANDOM;
909         eth_random_addr(dev->perm_addr);
910 }
911
912 /**
913  * vti6_dev_init_gen - general initializer for all tunnel devices
914  *   @dev: virtual device associated with tunnel
915  **/
916 static inline int vti6_dev_init_gen(struct net_device *dev)
917 {
918         struct ip6_tnl *t = netdev_priv(dev);
919
920         t->dev = dev;
921         t->net = dev_net(dev);
922         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
923         if (!dev->tstats)
924                 return -ENOMEM;
925         dev_hold(dev);
926         return 0;
927 }
928
929 /**
930  * vti6_dev_init - initializer for all non fallback tunnel devices
931  *   @dev: virtual device associated with tunnel
932  **/
933 static int vti6_dev_init(struct net_device *dev)
934 {
935         struct ip6_tnl *t = netdev_priv(dev);
936         int err = vti6_dev_init_gen(dev);
937
938         if (err)
939                 return err;
940         vti6_link_config(t, true);
941         return 0;
942 }
943
944 /**
945  * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
946  *   @dev: fallback device
947  *
948  * Return: 0
949  **/
950 static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
951 {
952         struct ip6_tnl *t = netdev_priv(dev);
953         struct net *net = dev_net(dev);
954         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
955
956         t->parms.proto = IPPROTO_IPV6;
957
958         rcu_assign_pointer(ip6n->tnls_wc[0], t);
959         return 0;
960 }
961
962 static int vti6_validate(struct nlattr *tb[], struct nlattr *data[],
963                          struct netlink_ext_ack *extack)
964 {
965         return 0;
966 }
967
968 static void vti6_netlink_parms(struct nlattr *data[],
969                                struct __ip6_tnl_parm *parms)
970 {
971         memset(parms, 0, sizeof(*parms));
972
973         if (!data)
974                 return;
975
976         if (data[IFLA_VTI_LINK])
977                 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
978
979         if (data[IFLA_VTI_LOCAL])
980                 parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
981
982         if (data[IFLA_VTI_REMOTE])
983                 parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
984
985         if (data[IFLA_VTI_IKEY])
986                 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
987
988         if (data[IFLA_VTI_OKEY])
989                 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
990
991         if (data[IFLA_VTI_FWMARK])
992                 parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
993 }
994
995 static int vti6_newlink(struct net *src_net, struct net_device *dev,
996                         struct nlattr *tb[], struct nlattr *data[],
997                         struct netlink_ext_ack *extack)
998 {
999         struct net *net = dev_net(dev);
1000         struct ip6_tnl *nt;
1001
1002         nt = netdev_priv(dev);
1003         vti6_netlink_parms(data, &nt->parms);
1004
1005         nt->parms.proto = IPPROTO_IPV6;
1006
1007         if (vti6_locate(net, &nt->parms, 0))
1008                 return -EEXIST;
1009
1010         return vti6_tnl_create2(dev);
1011 }
1012
1013 static void vti6_dellink(struct net_device *dev, struct list_head *head)
1014 {
1015         struct net *net = dev_net(dev);
1016         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1017
1018         if (dev != ip6n->fb_tnl_dev)
1019                 unregister_netdevice_queue(dev, head);
1020 }
1021
1022 static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
1023                            struct nlattr *data[],
1024                            struct netlink_ext_ack *extack)
1025 {
1026         struct ip6_tnl *t;
1027         struct __ip6_tnl_parm p;
1028         struct net *net = dev_net(dev);
1029         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1030
1031         if (dev == ip6n->fb_tnl_dev)
1032                 return -EINVAL;
1033
1034         vti6_netlink_parms(data, &p);
1035
1036         t = vti6_locate(net, &p, 0);
1037
1038         if (t) {
1039                 if (t->dev != dev)
1040                         return -EEXIST;
1041         } else
1042                 t = netdev_priv(dev);
1043
1044         return vti6_update(t, &p, tb && tb[IFLA_MTU]);
1045 }
1046
1047 static size_t vti6_get_size(const struct net_device *dev)
1048 {
1049         return
1050                 /* IFLA_VTI_LINK */
1051                 nla_total_size(4) +
1052                 /* IFLA_VTI_LOCAL */
1053                 nla_total_size(sizeof(struct in6_addr)) +
1054                 /* IFLA_VTI_REMOTE */
1055                 nla_total_size(sizeof(struct in6_addr)) +
1056                 /* IFLA_VTI_IKEY */
1057                 nla_total_size(4) +
1058                 /* IFLA_VTI_OKEY */
1059                 nla_total_size(4) +
1060                 /* IFLA_VTI_FWMARK */
1061                 nla_total_size(4) +
1062                 0;
1063 }
1064
1065 static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1066 {
1067         struct ip6_tnl *tunnel = netdev_priv(dev);
1068         struct __ip6_tnl_parm *parm = &tunnel->parms;
1069
1070         if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
1071             nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
1072             nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
1073             nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
1074             nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key) ||
1075             nla_put_u32(skb, IFLA_VTI_FWMARK, parm->fwmark))
1076                 goto nla_put_failure;
1077         return 0;
1078
1079 nla_put_failure:
1080         return -EMSGSIZE;
1081 }
1082
1083 static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
1084         [IFLA_VTI_LINK]         = { .type = NLA_U32 },
1085         [IFLA_VTI_LOCAL]        = { .len = sizeof(struct in6_addr) },
1086         [IFLA_VTI_REMOTE]       = { .len = sizeof(struct in6_addr) },
1087         [IFLA_VTI_IKEY]         = { .type = NLA_U32 },
1088         [IFLA_VTI_OKEY]         = { .type = NLA_U32 },
1089         [IFLA_VTI_FWMARK]       = { .type = NLA_U32 },
1090 };
1091
1092 static struct rtnl_link_ops vti6_link_ops __read_mostly = {
1093         .kind           = "vti6",
1094         .maxtype        = IFLA_VTI_MAX,
1095         .policy         = vti6_policy,
1096         .priv_size      = sizeof(struct ip6_tnl),
1097         .setup          = vti6_dev_setup,
1098         .validate       = vti6_validate,
1099         .newlink        = vti6_newlink,
1100         .dellink        = vti6_dellink,
1101         .changelink     = vti6_changelink,
1102         .get_size       = vti6_get_size,
1103         .fill_info      = vti6_fill_info,
1104         .get_link_net   = ip6_tnl_get_link_net,
1105 };
1106
1107 static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n,
1108                                             struct list_head *list)
1109 {
1110         int h;
1111         struct ip6_tnl *t;
1112
1113         for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
1114                 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1115                 while (t) {
1116                         unregister_netdevice_queue(t->dev, list);
1117                         t = rtnl_dereference(t->next);
1118                 }
1119         }
1120
1121         t = rtnl_dereference(ip6n->tnls_wc[0]);
1122         if (t)
1123                 unregister_netdevice_queue(t->dev, list);
1124 }
1125
1126 static int __net_init vti6_init_net(struct net *net)
1127 {
1128         struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1129         struct ip6_tnl *t = NULL;
1130         int err;
1131
1132         ip6n->tnls[0] = ip6n->tnls_wc;
1133         ip6n->tnls[1] = ip6n->tnls_r_l;
1134
1135         if (!net_has_fallback_tunnels(net))
1136                 return 0;
1137         err = -ENOMEM;
1138         ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
1139                                         NET_NAME_UNKNOWN, vti6_dev_setup);
1140
1141         if (!ip6n->fb_tnl_dev)
1142                 goto err_alloc_dev;
1143         dev_net_set(ip6n->fb_tnl_dev, net);
1144         ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
1145
1146         err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1147         if (err < 0)
1148                 goto err_register;
1149
1150         err = register_netdev(ip6n->fb_tnl_dev);
1151         if (err < 0)
1152                 goto err_register;
1153
1154         t = netdev_priv(ip6n->fb_tnl_dev);
1155
1156         strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1157         return 0;
1158
1159 err_register:
1160         free_netdev(ip6n->fb_tnl_dev);
1161 err_alloc_dev:
1162         return err;
1163 }
1164
1165 static void __net_exit vti6_exit_batch_net(struct list_head *net_list)
1166 {
1167         struct vti6_net *ip6n;
1168         struct net *net;
1169         LIST_HEAD(list);
1170
1171         rtnl_lock();
1172         list_for_each_entry(net, net_list, exit_list) {
1173                 ip6n = net_generic(net, vti6_net_id);
1174                 vti6_destroy_tunnels(ip6n, &list);
1175         }
1176         unregister_netdevice_many(&list);
1177         rtnl_unlock();
1178 }
1179
1180 static struct pernet_operations vti6_net_ops = {
1181         .init = vti6_init_net,
1182         .exit_batch = vti6_exit_batch_net,
1183         .id   = &vti6_net_id,
1184         .size = sizeof(struct vti6_net),
1185 };
1186
1187 static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
1188         .handler        =       vti6_rcv,
1189         .cb_handler     =       vti6_rcv_cb,
1190         .err_handler    =       vti6_err,
1191         .priority       =       100,
1192 };
1193
1194 static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
1195         .handler        =       vti6_rcv,
1196         .cb_handler     =       vti6_rcv_cb,
1197         .err_handler    =       vti6_err,
1198         .priority       =       100,
1199 };
1200
1201 static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
1202         .handler        =       vti6_rcv,
1203         .cb_handler     =       vti6_rcv_cb,
1204         .err_handler    =       vti6_err,
1205         .priority       =       100,
1206 };
1207
1208 /**
1209  * vti6_tunnel_init - register protocol and reserve needed resources
1210  *
1211  * Return: 0 on success
1212  **/
1213 static int __init vti6_tunnel_init(void)
1214 {
1215         const char *msg;
1216         int err;
1217
1218         msg = "tunnel device";
1219         err = register_pernet_device(&vti6_net_ops);
1220         if (err < 0)
1221                 goto pernet_dev_failed;
1222
1223         msg = "tunnel protocols";
1224         err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
1225         if (err < 0)
1226                 goto xfrm_proto_esp_failed;
1227         err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
1228         if (err < 0)
1229                 goto xfrm_proto_ah_failed;
1230         err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
1231         if (err < 0)
1232                 goto xfrm_proto_comp_failed;
1233
1234         msg = "netlink interface";
1235         err = rtnl_link_register(&vti6_link_ops);
1236         if (err < 0)
1237                 goto rtnl_link_failed;
1238
1239         return 0;
1240
1241 rtnl_link_failed:
1242         xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
1243 xfrm_proto_comp_failed:
1244         xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
1245 xfrm_proto_ah_failed:
1246         xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
1247 xfrm_proto_esp_failed:
1248         unregister_pernet_device(&vti6_net_ops);
1249 pernet_dev_failed:
1250         pr_err("vti6 init: failed to register %s\n", msg);
1251         return err;
1252 }
1253
1254 /**
1255  * vti6_tunnel_cleanup - free resources and unregister protocol
1256  **/
1257 static void __exit vti6_tunnel_cleanup(void)
1258 {
1259         rtnl_link_unregister(&vti6_link_ops);
1260         xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
1261         xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
1262         xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
1263         unregister_pernet_device(&vti6_net_ops);
1264 }
1265
1266 module_init(vti6_tunnel_init);
1267 module_exit(vti6_tunnel_cleanup);
1268 MODULE_LICENSE("GPL");
1269 MODULE_ALIAS_RTNL_LINK("vti6");
1270 MODULE_ALIAS_NETDEV("ip6_vti0");
1271 MODULE_AUTHOR("Steffen Klassert");
1272 MODULE_DESCRIPTION("IPv6 virtual tunnel interface");