blob: 23aeeb46f99fc96f248b27694cc2438265545908 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steffen Klasserted1efb22013-08-19 08:07:34 +02002/*
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
Steffen Klasserted1efb22013-08-19 08:07:34 +020012 */
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>
Steffen Klasserted1efb22013-08-19 08:07:34 +020023#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>
Felix Jia45ce0fd2017-01-26 16:59:18 +130048#include <linux/etherdevice.h>
Steffen Klasserted1efb22013-08-19 08:07:34 +020049
Jiri Kosinae87a8f22016-08-10 11:03:35 +020050#define IP6_VTI_HASH_SIZE_SHIFT 5
51#define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
Steffen Klasserted1efb22013-08-19 08:07:34 +020052
53static 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
Jiri Kosinae87a8f22016-08-10 11:03:35 +020057 return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
Steffen Klasserted1efb22013-08-19 08:07:34 +020058}
59
60static int vti6_dev_init(struct net_device *dev);
61static void vti6_dev_setup(struct net_device *dev);
62static struct rtnl_link_ops vti6_link_ops __read_mostly;
63
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030064static unsigned int vti6_net_id __read_mostly;
Steffen Klasserted1efb22013-08-19 08:07:34 +020065struct vti6_net {
66 /* the vti6 tunnel fallback device */
67 struct net_device *fb_tnl_dev;
68 /* lists for storing tunnels in use */
Jiri Kosinae87a8f22016-08-10 11:03:35 +020069 struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
Steffen Klasserted1efb22013-08-19 08:07:34 +020070 struct ip6_tnl __rcu *tnls_wc[1];
71 struct ip6_tnl __rcu **tnls[2];
72};
73
Steffen Klasserted1efb22013-08-19 08:07:34 +020074#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 **/
88static struct ip6_tnl *
89vti6_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);
Steffen Klassertfbe68ee2014-11-20 10:01:49 +010095 struct in6_addr any;
Steffen Klasserted1efb22013-08-19 08:07:34 +020096
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 }
Steffen Klassertfbe68ee2014-11-20 10:01:49 +0100103
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
Steffen Klasserted1efb22013-08-19 08:07:34 +0200119 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 **/
136static struct ip6_tnl __rcu **
137vti6_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
151static void
152vti6_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
160static void
161vti6_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
176static void vti6_dev_free(struct net_device *dev)
177{
178 free_percpu(dev->tstats);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200179}
180
181static 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
David Forster93e246f2017-01-06 10:27:59 +0000188 dev->rtnl_link_ops = &vti6_link_ops;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200189 err = register_netdevice(dev);
190 if (err < 0)
191 goto out;
192
193 strcpy(t->parms.name, dev->name);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200194
Steffen Klasserted1efb22013-08-19 08:07:34 +0200195 vti6_tnl_link(ip6n, t);
196
197 return 0;
198
199out:
200 return err;
201}
202
203static 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
Eric Dumazet537b3612018-04-05 06:39:31 -0700210 if (p->name[0]) {
211 if (!dev_valid_name(p->name))
212 goto failed;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200213 strlcpy(name, p->name, IFNAMSIZ);
Eric Dumazet537b3612018-04-05 06:39:31 -0700214 } else {
Steffen Klasserted1efb22013-08-19 08:07:34 +0200215 sprintf(name, "ip6_vti%%d");
Eric Dumazet537b3612018-04-05 06:39:31 -0700216 }
Steffen Klasserted1efb22013-08-19 08:07:34 +0200217
Tom Gundersenc835a672014-07-14 16:37:24 +0200218 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
Ian Morris63159f22015-03-29 14:00:04 +0100219 if (!dev)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200220 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
234failed_free:
David S. Millercf124db2017-05-08 12:52:56 -0400235 free_netdev(dev);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200236failed:
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 **/
254static 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) &&
Steffen Klassertd814b842014-09-22 10:07:25 +0200267 ipv6_addr_equal(remote, &t->parms.raddr)) {
268 if (create)
269 return NULL;
270
Steffen Klasserted1efb22013-08-19 08:07:34 +0200271 return t;
Steffen Klassertd814b842014-09-22 10:07:25 +0200272 }
Steffen Klasserted1efb22013-08-19 08:07:34 +0200273 }
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 **/
286static void vti6_dev_uninit(struct net_device *dev)
287{
288 struct ip6_tnl *t = netdev_priv(dev);
Yao Xiwei092a29a2015-04-02 17:31:17 +0200289 struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200290
291 if (dev == ip6n->fb_tnl_dev)
292 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
293 else
294 vti6_tnl_unlink(ip6n, t);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200295 dev_put(dev);
296}
297
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200298static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
299 int encap_type)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200300{
301 struct ip6_tnl *t;
302 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
303
304 rcu_read_lock();
Ian Morrise5d08d72014-11-23 21:28:43 +0000305 t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
Ian Morris53b24b82015-03-29 14:00:05 +0100306 if (t) {
Steffen Klasserted1efb22013-08-19 08:07:34 +0200307 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
308 rcu_read_unlock();
309 goto discard;
310 }
311
312 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
313 rcu_read_unlock();
Torsten Hilbrich2a9de3a2020-03-11 11:19:06 +0100314 goto discard;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200315 }
316
Eric Dumazetcbb49692018-12-21 07:47:51 -0800317 ipv6h = ipv6_hdr(skb);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200318 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
319 t->dev->stats.rx_dropped++;
320 rcu_read_unlock();
321 goto discard;
322 }
323
Steffen Klasserted1efb22013-08-19 08:07:34 +0200324 rcu_read_unlock();
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100325
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200326 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
327 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
328 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
329 return xfrm_input(skb, nexthdr, spi, encap_type);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200330 }
331 rcu_read_unlock();
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100332 return -EINVAL;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200333discard:
334 kfree_skb(skb);
335 return 0;
336}
337
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200338static int vti6_rcv(struct sk_buff *skb)
339{
340 int nexthdr = skb_network_header(skb)[IP6CB(skb)->nhoff];
341
342 return vti6_input_proto(skb, nexthdr, 0, 0);
343}
344
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100345static int vti6_rcv_cb(struct sk_buff *skb, int err)
346{
347 unsigned short family;
348 struct net_device *dev;
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100349 struct xfrm_state *x;
Florian Westphal4c145dc2019-03-29 21:16:31 +0100350 const struct xfrm_mode *inner_mode;
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100351 struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
Alexander Duyckd55c6702015-05-27 07:16:54 -0700352 u32 orig_mark = skb->mark;
353 int ret;
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100354
355 if (!t)
356 return 1;
357
358 dev = t->dev;
359
360 if (err) {
361 dev->stats.rx_errors++;
362 dev->stats.rx_dropped++;
363
364 return 0;
365 }
366
367 x = xfrm_input_state(skb);
thomas.zeitlhofer+lkml@ze-it.at1fb81e02016-09-07 20:40:38 +0200368
Florian Westphalc9500d72019-03-29 21:16:32 +0100369 inner_mode = &x->inner_mode;
thomas.zeitlhofer+lkml@ze-it.at1fb81e02016-09-07 20:40:38 +0200370
371 if (x->sel.family == AF_UNSPEC) {
372 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
373 if (inner_mode == NULL) {
374 XFRM_INC_STATS(dev_net(skb->dev),
375 LINUX_MIB_XFRMINSTATEMODEERROR);
376 return -EINVAL;
377 }
378 }
379
Florian Westphalb45714b2019-03-29 21:16:23 +0100380 family = inner_mode->family;
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100381
Alexander Duyckd55c6702015-05-27 07:16:54 -0700382 skb->mark = be32_to_cpu(t->parms.i_key);
383 ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
384 skb->mark = orig_mark;
385
386 if (!ret)
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100387 return -EPERM;
388
389 skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
390 skb->dev = dev;
Fabian Frederick5711eb02020-10-05 22:36:19 +0200391 dev_sw_netstats_rx_add(dev, skb->len);
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100392
393 return 0;
394}
395
Steffen Klasserted1efb22013-08-19 08:07:34 +0200396/**
397 * vti6_addr_conflict - compare packet addresses to tunnel's own
398 * @t: the outgoing tunnel device
399 * @hdr: IPv6 header from the incoming packet
400 *
401 * Description:
402 * Avoid trivial tunneling loop by checking that tunnel exit-point
403 * doesn't match source of incoming packet.
404 *
405 * Return:
406 * 1 if conflict,
407 * 0 else
408 **/
409static inline bool
410vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
411{
412 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
413}
414
Steffen Klassert26be8e22014-03-14 07:28:09 +0100415static bool vti6_state_check(const struct xfrm_state *x,
416 const struct in6_addr *dst,
417 const struct in6_addr *src)
418{
419 xfrm_address_t *daddr = (xfrm_address_t *)dst;
420 xfrm_address_t *saddr = (xfrm_address_t *)src;
421
422 /* if there is no transform then this tunnel is not functional.
423 * Or if the xfrm is not mode tunnel.
424 */
425 if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
426 x->props.family != AF_INET6)
427 return false;
428
429 if (ipv6_addr_any(dst))
430 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
431
432 if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
433 return false;
434
435 return true;
436}
437
Steffen Klasserted1efb22013-08-19 08:07:34 +0200438/**
439 * vti6_xmit - send a packet
440 * @skb: the outgoing socket buffer
441 * @dev: the outgoing tunnel device
Steffen Klassert22e1b232014-03-14 07:28:08 +0100442 * @fl: the flow informations for the xfrm_lookup
Steffen Klasserted1efb22013-08-19 08:07:34 +0200443 **/
Steffen Klassert22e1b232014-03-14 07:28:08 +0100444static int
445vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200446{
Steffen Klasserted1efb22013-08-19 08:07:34 +0200447 struct ip6_tnl *t = netdev_priv(dev);
448 struct net_device_stats *stats = &t->dev->stats;
Steffen Klassert7c852582014-03-14 07:28:08 +0100449 struct dst_entry *dst = skb_dst(skb);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200450 struct net_device *tdev;
Steffen Klassertd5005142014-11-05 08:02:48 +0100451 struct xfrm_state *x;
Alexey Kodanev36f6ee22017-09-26 15:14:29 +0300452 int pkt_len = skb->len;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200453 int err = -1;
Steffen Klassertccd740c2015-05-29 11:28:26 -0700454 int mtu;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200455
Nicolas Dichtel95224162020-01-13 09:32:46 +0100456 if (!dst) {
Nicolas Dichtelf1ed1022020-02-04 17:00:27 +0100457 switch (skb->protocol) {
458 case htons(ETH_P_IP): {
459 struct rtable *rt;
460
461 fl->u.ip4.flowi4_oif = dev->ifindex;
462 fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
463 rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
464 if (IS_ERR(rt))
465 goto tx_err_link_failure;
466 dst = &rt->dst;
467 skb_dst_set(skb, dst);
468 break;
469 }
470 case htons(ETH_P_IPV6):
471 fl->u.ip6.flowi6_oif = dev->ifindex;
472 fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
473 dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
474 if (dst->error) {
475 dst_release(dst);
476 dst = NULL;
477 goto tx_err_link_failure;
478 }
479 skb_dst_set(skb, dst);
480 break;
481 default:
Nicolas Dichtel95224162020-01-13 09:32:46 +0100482 goto tx_err_link_failure;
483 }
Nicolas Dichtel95224162020-01-13 09:32:46 +0100484 }
Steffen Klasserted1efb22013-08-19 08:07:34 +0200485
Steffen Klassert7c852582014-03-14 07:28:08 +0100486 dst_hold(dst);
Steffen Klassertb328ecc2020-07-17 10:35:32 +0200487 dst = xfrm_lookup_route(t->net, dst, fl, NULL, 0);
Steffen Klassert7c852582014-03-14 07:28:08 +0100488 if (IS_ERR(dst)) {
489 err = PTR_ERR(dst);
490 dst = NULL;
491 goto tx_err_link_failure;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200492 }
493
Steffen Klassertb328ecc2020-07-17 10:35:32 +0200494 if (dst->flags & DST_XFRM_QUEUE)
495 goto queued;
496
Steffen Klassertd5005142014-11-05 08:02:48 +0100497 x = dst->xfrm;
498 if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
499 goto tx_err_link_failure;
500
501 if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
502 (const struct in6_addr *)&x->id.daddr))
Steffen Klasserted1efb22013-08-19 08:07:34 +0200503 goto tx_err_link_failure;
504
505 tdev = dst->dev;
506
507 if (tdev == dev) {
508 stats->collisions++;
509 net_warn_ratelimited("%s: Local routing loop detected!\n",
510 t->parms.name);
511 goto tx_err_dst_release;
512 }
513
Steffen Klassertccd740c2015-05-29 11:28:26 -0700514 mtu = dst_mtu(dst);
Alexey Kodanev9f289542018-08-23 19:49:54 +0300515 if (skb->len > mtu) {
Hangbin Liu8247a792019-12-22 10:51:14 +0800516 skb_dst_update_pmtu_no_confirm(skb, mtu);
Steffen Klassertccd740c2015-05-29 11:28:26 -0700517
Steffen Klasserte3dc8472017-02-15 11:38:58 +0100518 if (skb->protocol == htons(ETH_P_IPV6)) {
519 if (mtu < IPV6_MIN_MTU)
520 mtu = IPV6_MIN_MTU;
521
Jason A. Donenfeld91796b62021-02-27 01:40:19 +0100522 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Steffen Klasserte3dc8472017-02-15 11:38:58 +0100523 } else {
Jason A. Donenfeld91796b62021-02-27 01:40:19 +0100524 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
525 htonl(mtu));
Steffen Klasserte3dc8472017-02-15 11:38:58 +0100526 }
Steffen Klassertccd740c2015-05-29 11:28:26 -0700527
Eyal Birgerd6990972018-06-07 10:11:02 +0300528 err = -EMSGSIZE;
529 goto tx_err_dst_release;
Steffen Klassertccd740c2015-05-29 11:28:26 -0700530 }
531
Steffen Klassertb328ecc2020-07-17 10:35:32 +0200532queued:
Eyal Birgerd6990972018-06-07 10:11:02 +0300533 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
534 skb_dst_set(skb, dst);
535 skb->dev = skb_dst(skb)->dev;
536
Eric W. Biederman13206b62015-10-07 16:48:35 -0500537 err = dst_output(t->net, skb->sk, skb);
Haishuang Yanbb107452018-08-18 22:43:48 +0800538 if (net_xmit_eval(err) == 0)
539 err = pkt_len;
540 iptunnel_xmit_stats(dev, err);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200541
542 return 0;
543tx_err_link_failure:
544 stats->tx_carrier_errors++;
545 dst_link_failure(skb);
546tx_err_dst_release:
Steffen Klassert7c852582014-03-14 07:28:08 +0100547 dst_release(dst);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200548 return err;
549}
550
551static netdev_tx_t
552vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
553{
554 struct ip6_tnl *t = netdev_priv(dev);
555 struct net_device_stats *stats = &t->dev->stats;
Steffen Klassert22e1b232014-03-14 07:28:08 +0100556 struct flowi fl;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200557 int ret;
558
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500559 if (!pskb_inet_may_pull(skb))
560 goto tx_err;
561
Steffen Klassert22e1b232014-03-14 07:28:08 +0100562 memset(&fl, 0, sizeof(fl));
Steffen Klassert22e1b232014-03-14 07:28:08 +0100563
Steffen Klasserted1efb22013-08-19 08:07:34 +0200564 switch (skb->protocol) {
565 case htons(ETH_P_IPV6):
Steffen Klassert22e1b232014-03-14 07:28:08 +0100566 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500567 vti6_addr_conflict(t, ipv6_hdr(skb)))
Steffen Klassert22e1b232014-03-14 07:28:08 +0100568 goto tx_err;
569
570 xfrm_decode_session(skb, &fl, AF_INET6);
571 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
572 break;
573 case htons(ETH_P_IP):
574 xfrm_decode_session(skb, &fl, AF_INET);
575 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Steffen Klasserted1efb22013-08-19 08:07:34 +0200576 break;
577 default:
578 goto tx_err;
579 }
580
Alexander Duyckcd5279c2015-05-27 07:16:43 -0700581 /* override mark with tunnel output key */
582 fl.flowi_mark = be32_to_cpu(t->parms.o_key);
583
Steffen Klassert22e1b232014-03-14 07:28:08 +0100584 ret = vti6_xmit(skb, dev, &fl);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200585 if (ret < 0)
586 goto tx_err;
587
588 return NETDEV_TX_OK;
589
590tx_err:
591 stats->tx_errors++;
592 stats->tx_dropped++;
593 kfree_skb(skb);
594 return NETDEV_TX_OK;
595}
596
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100597static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
598 u8 type, u8 code, int offset, __be32 info)
599{
600 __be32 spi;
Steffen Klassert6d004d62014-05-12 09:09:26 +0200601 __u32 mark;
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100602 struct xfrm_state *x;
603 struct ip6_tnl *t;
604 struct ip_esp_hdr *esph;
605 struct ip_auth_hdr *ah;
606 struct ip_comp_hdr *ipch;
607 struct net *net = dev_net(skb->dev);
608 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
609 int protocol = iph->nexthdr;
610
611 t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
612 if (!t)
613 return -1;
614
Steffen Klassert6d004d62014-05-12 09:09:26 +0200615 mark = be32_to_cpu(t->parms.o_key);
616
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100617 switch (protocol) {
618 case IPPROTO_ESP:
619 esph = (struct ip_esp_hdr *)(skb->data + offset);
620 spi = esph->spi;
621 break;
622 case IPPROTO_AH:
623 ah = (struct ip_auth_hdr *)(skb->data + offset);
624 spi = ah->spi;
625 break;
626 case IPPROTO_COMP:
627 ipch = (struct ip_comp_hdr *)(skb->data + offset);
628 spi = htonl(ntohs(ipch->cpi));
629 break;
630 default:
631 return 0;
632 }
633
634 if (type != ICMPV6_PKT_TOOBIG &&
635 type != NDISC_REDIRECT)
636 return 0;
637
Steffen Klassert6d004d62014-05-12 09:09:26 +0200638 x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100639 spi, protocol, AF_INET6);
640 if (!x)
641 return 0;
642
643 if (type == NDISC_REDIRECT)
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900644 ip6_redirect(skb, net, skb->dev->ifindex, 0,
645 sock_net_uid(net, NULL));
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100646 else
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900647 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Steffen Klassertfa9ad962014-03-14 07:28:08 +0100648 xfrm_state_put(x);
649
650 return 0;
651}
652
Stefano Brivio7a67e692018-03-15 17:17:12 +0100653static void vti6_link_config(struct ip6_tnl *t, bool keep_mtu)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200654{
Steffen Klasserted1efb22013-08-19 08:07:34 +0200655 struct net_device *dev = t->dev;
656 struct __ip6_tnl_parm *p = &t->parms;
Alexey Kodanev53c81e92017-12-19 16:59:21 +0300657 struct net_device *tdev = NULL;
Stefano Brivioc6741fb2018-03-15 17:17:11 +0100658 int mtu;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200659
660 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
661 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
662
Steffen Klasserted1efb22013-08-19 08:07:34 +0200663 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
664 IP6_TNL_F_CAP_PER_PACKET);
665 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
666
667 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
668 dev->flags |= IFF_POINTOPOINT;
669 else
670 dev->flags &= ~IFF_POINTOPOINT;
Alexey Kodanev53c81e92017-12-19 16:59:21 +0300671
Stefano Brivio7a67e692018-03-15 17:17:12 +0100672 if (keep_mtu && dev->mtu) {
673 dev->mtu = clamp(dev->mtu, dev->min_mtu, dev->max_mtu);
674 return;
675 }
676
Alexey Kodanev53c81e92017-12-19 16:59:21 +0300677 if (p->flags & IP6_TNL_F_CAP_XMIT) {
678 int strict = (ipv6_addr_type(&p->raddr) &
679 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL));
680 struct rt6_info *rt = rt6_lookup(t->net,
681 &p->raddr, &p->laddr,
David Ahernb75cc8f2018-03-02 08:32:17 -0800682 p->link, NULL, strict);
Alexey Kodanev53c81e92017-12-19 16:59:21 +0300683
684 if (rt)
685 tdev = rt->dst.dev;
686 ip6_rt_put(rt);
687 }
688
689 if (!tdev && p->link)
690 tdev = __dev_get_by_index(t->net, p->link);
691
692 if (tdev)
Stefano Brivioc6741fb2018-03-15 17:17:11 +0100693 mtu = tdev->mtu - sizeof(struct ipv6hdr);
694 else
695 mtu = ETH_DATA_LEN - LL_MAX_HEADER - sizeof(struct ipv6hdr);
696
Stefano Briviob4331a62018-04-26 19:39:09 +0200697 dev->mtu = max_t(int, mtu, IPV4_MIN_MTU);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200698}
699
700/**
701 * vti6_tnl_change - update the tunnel parameters
702 * @t: tunnel to be changed
703 * @p: tunnel configuration parameters
Stefano Brivio7a67e692018-03-15 17:17:12 +0100704 * @keep_mtu: MTU was set from userspace, don't re-compute it
Steffen Klasserted1efb22013-08-19 08:07:34 +0200705 *
706 * Description:
707 * vti6_tnl_change() updates the tunnel parameters
708 **/
709static int
Stefano Brivio7a67e692018-03-15 17:17:12 +0100710vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p,
711 bool keep_mtu)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200712{
713 t->parms.laddr = p->laddr;
714 t->parms.raddr = p->raddr;
715 t->parms.link = p->link;
716 t->parms.i_key = p->i_key;
717 t->parms.o_key = p->o_key;
718 t->parms.proto = p->proto;
Craig Gallek0a473b82017-04-19 12:30:53 -0400719 t->parms.fwmark = p->fwmark;
Paolo Abeni607f7252016-02-12 15:43:54 +0100720 dst_cache_reset(&t->dst_cache);
Stefano Brivio7a67e692018-03-15 17:17:12 +0100721 vti6_link_config(t, keep_mtu);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200722 return 0;
723}
724
Stefano Brivio7a67e692018-03-15 17:17:12 +0100725static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p,
726 bool keep_mtu)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200727{
728 struct net *net = dev_net(t->dev);
729 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
730 int err;
731
732 vti6_tnl_unlink(ip6n, t);
733 synchronize_net();
Stefano Brivio7a67e692018-03-15 17:17:12 +0100734 err = vti6_tnl_change(t, p, keep_mtu);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200735 vti6_tnl_link(ip6n, t);
736 netdev_state_change(t->dev);
737 return err;
738}
739
740static void
741vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
742{
743 p->laddr = u->laddr;
744 p->raddr = u->raddr;
745 p->link = u->link;
746 p->i_key = u->i_key;
747 p->o_key = u->o_key;
748 p->proto = u->proto;
749
750 memcpy(p->name, u->name, sizeof(u->name));
751}
752
753static void
754vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
755{
756 u->laddr = p->laddr;
757 u->raddr = p->raddr;
758 u->link = p->link;
759 u->i_key = p->i_key;
760 u->o_key = p->o_key;
David Forster7dcdf942017-02-24 14:20:32 +0000761 if (u->i_key)
762 u->i_flags |= GRE_KEY;
763 if (u->o_key)
764 u->o_flags |= GRE_KEY;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200765 u->proto = p->proto;
766
767 memcpy(u->name, p->name, sizeof(u->name));
768}
769
770/**
Sun Lianwen154a8c42018-04-29 15:05:52 +0800771 * vti6_ioctl - configure vti6 tunnels from userspace
Steffen Klasserted1efb22013-08-19 08:07:34 +0200772 * @dev: virtual device associated with tunnel
773 * @ifr: parameters passed from userspace
774 * @cmd: command to be performed
775 *
776 * Description:
777 * vti6_ioctl() is used for managing vti6 tunnels
778 * from userspace.
779 *
780 * The possible commands are the following:
781 * %SIOCGETTUNNEL: get tunnel parameters for device
782 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
783 * %SIOCCHGTUNNEL: change tunnel parameters to those given
784 * %SIOCDELTUNNEL: delete tunnel
785 *
786 * The fallback device "ip6_vti0", created during module
787 * initialization, can be used for creating other tunnel devices.
788 *
789 * Return:
790 * 0 on success,
791 * %-EFAULT if unable to copy data to or from userspace,
792 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
793 * %-EINVAL if passed tunnel parameters are invalid,
794 * %-EEXIST if changing a tunnel's parameters would cause a conflict
795 * %-ENODEV if attempting to change or delete a nonexisting device
796 **/
797static int
798vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
799{
800 int err = 0;
801 struct ip6_tnl_parm2 p;
802 struct __ip6_tnl_parm p1;
803 struct ip6_tnl *t = NULL;
804 struct net *net = dev_net(dev);
805 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
806
807 switch (cmd) {
808 case SIOCGETTUNNEL:
809 if (dev == ip6n->fb_tnl_dev) {
810 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
811 err = -EFAULT;
812 break;
813 }
814 vti6_parm_from_user(&p1, &p);
815 t = vti6_locate(net, &p1, 0);
816 } else {
817 memset(&p, 0, sizeof(p));
818 }
Ian Morris63159f22015-03-29 14:00:04 +0100819 if (!t)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200820 t = netdev_priv(dev);
821 vti6_parm_to_user(&p, &t->parms);
822 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
823 err = -EFAULT;
824 break;
825 case SIOCADDTUNNEL:
826 case SIOCCHGTUNNEL:
827 err = -EPERM;
828 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
829 break;
830 err = -EFAULT;
831 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
832 break;
833 err = -EINVAL;
834 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
835 break;
836 vti6_parm_from_user(&p1, &p);
837 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
838 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +0100839 if (t) {
Steffen Klasserted1efb22013-08-19 08:07:34 +0200840 if (t->dev != dev) {
841 err = -EEXIST;
842 break;
843 }
844 } else
845 t = netdev_priv(dev);
846
Stefano Brivio7a67e692018-03-15 17:17:12 +0100847 err = vti6_update(t, &p1, false);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200848 }
849 if (t) {
850 err = 0;
851 vti6_parm_to_user(&p, &t->parms);
852 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
853 err = -EFAULT;
854
855 } else
856 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
857 break;
858 case SIOCDELTUNNEL:
859 err = -EPERM;
860 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
861 break;
862
863 if (dev == ip6n->fb_tnl_dev) {
864 err = -EFAULT;
865 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
866 break;
867 err = -ENOENT;
868 vti6_parm_from_user(&p1, &p);
869 t = vti6_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +0100870 if (!t)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200871 break;
872 err = -EPERM;
873 if (t->dev == ip6n->fb_tnl_dev)
874 break;
875 dev = t->dev;
876 }
877 err = 0;
878 unregister_netdevice(dev);
879 break;
880 default:
881 err = -EINVAL;
882 }
883 return err;
884}
885
Steffen Klasserted1efb22013-08-19 08:07:34 +0200886static const struct net_device_ops vti6_netdev_ops = {
Steffen Klassert16a02312014-11-03 09:19:28 +0100887 .ndo_init = vti6_dev_init,
Steffen Klasserted1efb22013-08-19 08:07:34 +0200888 .ndo_uninit = vti6_dev_uninit,
889 .ndo_start_xmit = vti6_tnl_xmit,
890 .ndo_do_ioctl = vti6_ioctl,
Li RongQing469bdce2014-01-02 14:24:36 +0800891 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +0200892 .ndo_get_iflink = ip6_tnl_get_iflink,
Steffen Klasserted1efb22013-08-19 08:07:34 +0200893};
894
895/**
896 * vti6_dev_setup - setup virtual tunnel device
897 * @dev: virtual device associated with tunnel
898 *
899 * Description:
900 * Initialize function pointers and device parameters
901 **/
902static void vti6_dev_setup(struct net_device *dev)
903{
Steffen Klasserted1efb22013-08-19 08:07:34 +0200904 dev->netdev_ops = &vti6_netdev_ops;
Jason A. Donenfeldab59d2b2020-06-29 19:06:23 -0600905 dev->header_ops = &ip_tunnel_header_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400906 dev->needs_free_netdev = true;
907 dev->priv_destructor = vti6_dev_free;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200908
909 dev->type = ARPHRD_TUNNEL6;
Stefano Briviob4331a62018-04-26 19:39:09 +0200910 dev->min_mtu = IPV4_MIN_MTU;
Stefano Briviof8a554b2018-03-15 17:17:13 +0100911 dev->max_mtu = IP_MAX_MTU - sizeof(struct ipv6hdr);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200912 dev->flags |= IFF_NOARP;
913 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -0700914 netif_keep_dst(dev);
Felix Jia45ce0fd2017-01-26 16:59:18 +1300915 /* This perm addr will be used as interface identifier by IPv6 */
916 dev->addr_assign_type = NET_ADDR_RANDOM;
917 eth_random_addr(dev->perm_addr);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200918}
919
920/**
921 * vti6_dev_init_gen - general initializer for all tunnel devices
922 * @dev: virtual device associated with tunnel
923 **/
924static inline int vti6_dev_init_gen(struct net_device *dev)
925{
926 struct ip6_tnl *t = netdev_priv(dev);
927
928 t->dev = dev;
929 t->net = dev_net(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -0800930 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200931 if (!dev->tstats)
932 return -ENOMEM;
Eric Dumazet7bd851a2021-03-29 12:12:54 -0700933 dev_hold(dev);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200934 return 0;
935}
936
937/**
938 * vti6_dev_init - initializer for all non fallback tunnel devices
939 * @dev: virtual device associated with tunnel
940 **/
941static int vti6_dev_init(struct net_device *dev)
942{
943 struct ip6_tnl *t = netdev_priv(dev);
944 int err = vti6_dev_init_gen(dev);
945
946 if (err)
947 return err;
Stefano Brivio7a67e692018-03-15 17:17:12 +0100948 vti6_link_config(t, true);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200949 return 0;
950}
951
952/**
953 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
954 * @dev: fallback device
955 *
956 * Return: 0
957 **/
958static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
959{
960 struct ip6_tnl *t = netdev_priv(dev);
961 struct net *net = dev_net(dev);
962 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200963
964 t->parms.proto = IPPROTO_IPV6;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200965
Steffen Klasserted1efb22013-08-19 08:07:34 +0200966 rcu_assign_pointer(ip6n->tnls_wc[0], t);
967 return 0;
968}
969
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200970static int vti6_validate(struct nlattr *tb[], struct nlattr *data[],
971 struct netlink_ext_ack *extack)
Steffen Klasserted1efb22013-08-19 08:07:34 +0200972{
973 return 0;
974}
975
976static void vti6_netlink_parms(struct nlattr *data[],
977 struct __ip6_tnl_parm *parms)
978{
979 memset(parms, 0, sizeof(*parms));
980
981 if (!data)
982 return;
983
984 if (data[IFLA_VTI_LINK])
985 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
986
987 if (data[IFLA_VTI_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +0200988 parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200989
990 if (data[IFLA_VTI_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +0200991 parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200992
993 if (data[IFLA_VTI_IKEY])
994 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
995
996 if (data[IFLA_VTI_OKEY])
997 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
Craig Gallek0a473b82017-04-19 12:30:53 -0400998
999 if (data[IFLA_VTI_FWMARK])
1000 parms->fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001001}
1002
1003static int vti6_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001004 struct nlattr *tb[], struct nlattr *data[],
1005 struct netlink_ext_ack *extack)
Steffen Klasserted1efb22013-08-19 08:07:34 +02001006{
1007 struct net *net = dev_net(dev);
1008 struct ip6_tnl *nt;
1009
1010 nt = netdev_priv(dev);
1011 vti6_netlink_parms(data, &nt->parms);
1012
1013 nt->parms.proto = IPPROTO_IPV6;
1014
1015 if (vti6_locate(net, &nt->parms, 0))
1016 return -EEXIST;
1017
1018 return vti6_tnl_create2(dev);
1019}
1020
lucien20ea60c2014-11-23 15:04:11 +08001021static void vti6_dellink(struct net_device *dev, struct list_head *head)
1022{
1023 struct net *net = dev_net(dev);
1024 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1025
1026 if (dev != ip6n->fb_tnl_dev)
1027 unregister_netdevice_queue(dev, head);
1028}
1029
Steffen Klasserted1efb22013-08-19 08:07:34 +02001030static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001031 struct nlattr *data[],
1032 struct netlink_ext_ack *extack)
Steffen Klasserted1efb22013-08-19 08:07:34 +02001033{
1034 struct ip6_tnl *t;
1035 struct __ip6_tnl_parm p;
1036 struct net *net = dev_net(dev);
1037 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1038
1039 if (dev == ip6n->fb_tnl_dev)
1040 return -EINVAL;
1041
1042 vti6_netlink_parms(data, &p);
1043
1044 t = vti6_locate(net, &p, 0);
1045
1046 if (t) {
1047 if (t->dev != dev)
1048 return -EEXIST;
1049 } else
1050 t = netdev_priv(dev);
1051
Stefano Brivio7a67e692018-03-15 17:17:12 +01001052 return vti6_update(t, &p, tb && tb[IFLA_MTU]);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001053}
1054
1055static size_t vti6_get_size(const struct net_device *dev)
1056{
1057 return
1058 /* IFLA_VTI_LINK */
1059 nla_total_size(4) +
1060 /* IFLA_VTI_LOCAL */
1061 nla_total_size(sizeof(struct in6_addr)) +
1062 /* IFLA_VTI_REMOTE */
1063 nla_total_size(sizeof(struct in6_addr)) +
1064 /* IFLA_VTI_IKEY */
1065 nla_total_size(4) +
1066 /* IFLA_VTI_OKEY */
1067 nla_total_size(4) +
Craig Gallek0a473b82017-04-19 12:30:53 -04001068 /* IFLA_VTI_FWMARK */
1069 nla_total_size(4) +
Steffen Klasserted1efb22013-08-19 08:07:34 +02001070 0;
1071}
1072
1073static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1074{
1075 struct ip6_tnl *tunnel = netdev_priv(dev);
1076 struct __ip6_tnl_parm *parm = &tunnel->parms;
1077
1078 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001079 nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
1080 nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
Steffen Klasserted1efb22013-08-19 08:07:34 +02001081 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
Craig Gallek0a473b82017-04-19 12:30:53 -04001082 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key) ||
1083 nla_put_u32(skb, IFLA_VTI_FWMARK, parm->fwmark))
Steffen Klasserted1efb22013-08-19 08:07:34 +02001084 goto nla_put_failure;
1085 return 0;
1086
1087nla_put_failure:
1088 return -EMSGSIZE;
1089}
1090
1091static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
1092 [IFLA_VTI_LINK] = { .type = NLA_U32 },
1093 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
1094 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
1095 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
1096 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
Craig Gallek0a473b82017-04-19 12:30:53 -04001097 [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
Steffen Klasserted1efb22013-08-19 08:07:34 +02001098};
1099
1100static struct rtnl_link_ops vti6_link_ops __read_mostly = {
1101 .kind = "vti6",
1102 .maxtype = IFLA_VTI_MAX,
1103 .policy = vti6_policy,
1104 .priv_size = sizeof(struct ip6_tnl),
1105 .setup = vti6_dev_setup,
1106 .validate = vti6_validate,
1107 .newlink = vti6_newlink,
lucien20ea60c2014-11-23 15:04:11 +08001108 .dellink = vti6_dellink,
Steffen Klasserted1efb22013-08-19 08:07:34 +02001109 .changelink = vti6_changelink,
1110 .get_size = vti6_get_size,
1111 .fill_info = vti6_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001112 .get_link_net = ip6_tnl_get_link_net,
Steffen Klasserted1efb22013-08-19 08:07:34 +02001113};
1114
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001115static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n,
1116 struct list_head *list)
Steffen Klasserted1efb22013-08-19 08:07:34 +02001117{
1118 int h;
1119 struct ip6_tnl *t;
Steffen Klasserted1efb22013-08-19 08:07:34 +02001120
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001121 for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
Steffen Klasserted1efb22013-08-19 08:07:34 +02001122 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Ian Morris53b24b82015-03-29 14:00:05 +01001123 while (t) {
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001124 unregister_netdevice_queue(t->dev, list);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001125 t = rtnl_dereference(t->next);
1126 }
1127 }
1128
1129 t = rtnl_dereference(ip6n->tnls_wc[0]);
Haishuang Yan9c863362018-08-20 10:51:05 +08001130 if (t)
1131 unregister_netdevice_queue(t->dev, list);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001132}
1133
1134static int __net_init vti6_init_net(struct net *net)
1135{
1136 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1137 struct ip6_tnl *t = NULL;
1138 int err;
1139
1140 ip6n->tnls[0] = ip6n->tnls_wc;
1141 ip6n->tnls[1] = ip6n->tnls_r_l;
1142
Haishuang Yane2948e52018-08-19 15:05:05 +08001143 if (!net_has_fallback_tunnels(net))
1144 return 0;
Steffen Klasserted1efb22013-08-19 08:07:34 +02001145 err = -ENOMEM;
1146 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001147 NET_NAME_UNKNOWN, vti6_dev_setup);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001148
1149 if (!ip6n->fb_tnl_dev)
1150 goto err_alloc_dev;
1151 dev_net_set(ip6n->fb_tnl_dev, net);
lucien20ea60c2014-11-23 15:04:11 +08001152 ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
Steffen Klasserted1efb22013-08-19 08:07:34 +02001153
1154 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1155 if (err < 0)
1156 goto err_register;
1157
1158 err = register_netdev(ip6n->fb_tnl_dev);
1159 if (err < 0)
1160 goto err_register;
1161
1162 t = netdev_priv(ip6n->fb_tnl_dev);
1163
1164 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1165 return 0;
1166
1167err_register:
David S. Millercf124db2017-05-08 12:52:56 -04001168 free_netdev(ip6n->fb_tnl_dev);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001169err_alloc_dev:
1170 return err;
1171}
1172
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001173static void __net_exit vti6_exit_batch_net(struct list_head *net_list)
Steffen Klasserted1efb22013-08-19 08:07:34 +02001174{
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001175 struct vti6_net *ip6n;
1176 struct net *net;
1177 LIST_HEAD(list);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001178
1179 rtnl_lock();
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001180 list_for_each_entry(net, net_list, exit_list) {
1181 ip6n = net_generic(net, vti6_net_id);
1182 vti6_destroy_tunnels(ip6n, &list);
1183 }
1184 unregister_netdevice_many(&list);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001185 rtnl_unlock();
1186}
1187
1188static struct pernet_operations vti6_net_ops = {
1189 .init = vti6_init_net,
Eric Dumazetbb401ca2017-09-19 16:27:08 -07001190 .exit_batch = vti6_exit_batch_net,
Steffen Klasserted1efb22013-08-19 08:07:34 +02001191 .id = &vti6_net_id,
1192 .size = sizeof(struct vti6_net),
1193};
1194
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001195static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
1196 .handler = vti6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +02001197 .input_handler = vti6_input_proto,
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001198 .cb_handler = vti6_rcv_cb,
1199 .err_handler = vti6_err,
1200 .priority = 100,
1201};
1202
1203static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
1204 .handler = vti6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +02001205 .input_handler = vti6_input_proto,
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001206 .cb_handler = vti6_rcv_cb,
1207 .err_handler = vti6_err,
1208 .priority = 100,
1209};
1210
1211static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
1212 .handler = vti6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +02001213 .input_handler = vti6_input_proto,
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001214 .cb_handler = vti6_rcv_cb,
1215 .err_handler = vti6_err,
1216 .priority = 100,
1217};
1218
Xin Long96a208292020-07-17 15:03:14 +08001219#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Long08622862020-07-06 20:01:34 +08001220static int vti6_rcv_tunnel(struct sk_buff *skb)
1221{
1222 const xfrm_address_t *saddr;
1223 __be32 spi;
1224
1225 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
1226 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
1227
1228 return vti6_input_proto(skb, IPPROTO_IPV6, spi, 0);
1229}
1230
1231static struct xfrm6_tunnel vti_ipv6_handler __read_mostly = {
1232 .handler = vti6_rcv_tunnel,
1233 .cb_handler = vti6_rcv_cb,
1234 .err_handler = vti6_err,
1235 .priority = 0,
1236};
Xin Longa8757142020-07-13 15:42:37 +08001237
1238static struct xfrm6_tunnel vti_ip6ip_handler __read_mostly = {
1239 .handler = vti6_rcv_tunnel,
1240 .cb_handler = vti6_rcv_cb,
1241 .err_handler = vti6_err,
1242 .priority = 0,
1243};
Xin Long08622862020-07-06 20:01:34 +08001244#endif
1245
Steffen Klasserted1efb22013-08-19 08:07:34 +02001246/**
1247 * vti6_tunnel_init - register protocol and reserve needed resources
1248 *
1249 * Return: 0 on success
1250 **/
1251static int __init vti6_tunnel_init(void)
1252{
Mathias Krausee59d82f2014-05-09 23:43:41 +02001253 const char *msg;
1254 int err;
Steffen Klasserted1efb22013-08-19 08:07:34 +02001255
Mathias Krausee59d82f2014-05-09 23:43:41 +02001256 msg = "tunnel device";
Steffen Klasserted1efb22013-08-19 08:07:34 +02001257 err = register_pernet_device(&vti6_net_ops);
1258 if (err < 0)
Mathias Krausee59d82f2014-05-09 23:43:41 +02001259 goto pernet_dev_failed;
Steffen Klasserted1efb22013-08-19 08:07:34 +02001260
Mathias Krausee59d82f2014-05-09 23:43:41 +02001261 msg = "tunnel protocols";
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001262 err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001263 if (err < 0)
1264 goto xfrm_proto_esp_failed;
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001265 err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001266 if (err < 0)
1267 goto xfrm_proto_ah_failed;
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001268 err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001269 if (err < 0)
1270 goto xfrm_proto_comp_failed;
Xin Long96a208292020-07-17 15:03:14 +08001271#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Long08622862020-07-06 20:01:34 +08001272 msg = "ipv6 tunnel";
1273 err = xfrm6_tunnel_register(&vti_ipv6_handler, AF_INET6);
1274 if (err < 0)
Xin Long2ab110c2020-07-06 20:01:35 +08001275 goto vti_tunnel_ipv6_failed;
Xin Longa8757142020-07-13 15:42:37 +08001276 err = xfrm6_tunnel_register(&vti_ip6ip_handler, AF_INET);
Xin Long2ab110c2020-07-06 20:01:35 +08001277 if (err < 0)
1278 goto vti_tunnel_ip6ip_failed;
Xin Long08622862020-07-06 20:01:34 +08001279#endif
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001280
Mathias Krausee59d82f2014-05-09 23:43:41 +02001281 msg = "netlink interface";
Steffen Klasserted1efb22013-08-19 08:07:34 +02001282 err = rtnl_link_register(&vti6_link_ops);
1283 if (err < 0)
1284 goto rtnl_link_failed;
1285
1286 return 0;
1287
1288rtnl_link_failed:
Xin Long96a208292020-07-17 15:03:14 +08001289#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longa8757142020-07-13 15:42:37 +08001290 err = xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
Xin Long2ab110c2020-07-06 20:01:35 +08001291vti_tunnel_ip6ip_failed:
Xin Long08622862020-07-06 20:01:34 +08001292 err = xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
Xin Long2ab110c2020-07-06 20:01:35 +08001293vti_tunnel_ipv6_failed:
Xin Long08622862020-07-06 20:01:34 +08001294#endif
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001295 xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001296xfrm_proto_comp_failed:
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001297 xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001298xfrm_proto_ah_failed:
Steffen Klassertfa9ad962014-03-14 07:28:08 +01001299 xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001300xfrm_proto_esp_failed:
Steffen Klasserted1efb22013-08-19 08:07:34 +02001301 unregister_pernet_device(&vti6_net_ops);
Mathias Krausee59d82f2014-05-09 23:43:41 +02001302pernet_dev_failed:
1303 pr_err("vti6 init: failed to register %s\n", msg);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001304 return err;
1305}
1306
1307/**
1308 * vti6_tunnel_cleanup - free resources and unregister protocol
1309 **/
1310static void __exit vti6_tunnel_cleanup(void)
1311{
1312 rtnl_link_unregister(&vti6_link_ops);
Xin Long96a208292020-07-17 15:03:14 +08001313#if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
Xin Longa8757142020-07-13 15:42:37 +08001314 xfrm6_tunnel_deregister(&vti_ip6ip_handler, AF_INET);
Xin Long08622862020-07-06 20:01:34 +08001315 xfrm6_tunnel_deregister(&vti_ipv6_handler, AF_INET6);
1316#endif
Mathias Krausee59d82f2014-05-09 23:43:41 +02001317 xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
1318 xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
1319 xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001320 unregister_pernet_device(&vti6_net_ops);
Steffen Klasserted1efb22013-08-19 08:07:34 +02001321}
1322
1323module_init(vti6_tunnel_init);
1324module_exit(vti6_tunnel_cleanup);
1325MODULE_LICENSE("GPL");
1326MODULE_ALIAS_RTNL_LINK("vti6");
1327MODULE_ALIAS_NETDEV("ip6_vti0");
1328MODULE_AUTHOR("Steffen Klassert");
1329MODULE_DESCRIPTION("IPv6 virtual tunnel interface");