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