]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/ip6_vti.c
ipv6: fix the use of pcpu_tstats in ip6_vti.c
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / ip6_vti.c
CommitLineData
ed1efb2a
SK
1/*
2 * IPv6 virtual tunneling interface
3 *
4 * Copyright (C) 2013 secunet Security Networks AG
5 *
6 * Author:
7 * Steffen Klassert <steffen.klassert@secunet.com>
8 *
9 * Based on:
10 * net/ipv6/ip6_tunnel.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/capability.h>
20#include <linux/errno.h>
21#include <linux/types.h>
22#include <linux/sockios.h>
23#include <linux/icmp.h>
24#include <linux/if.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/if_tunnel.h>
28#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/icmpv6.h>
33#include <linux/init.h>
34#include <linux/route.h>
35#include <linux/rtnetlink.h>
36#include <linux/netfilter_ipv6.h>
37#include <linux/slab.h>
38#include <linux/hash.h>
39
40#include <linux/uaccess.h>
41#include <linux/atomic.h>
42
43#include <net/icmp.h>
44#include <net/ip.h>
45#include <net/ip_tunnels.h>
46#include <net/ipv6.h>
47#include <net/ip6_route.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
50#include <net/xfrm.h>
51#include <net/net_namespace.h>
52#include <net/netns/generic.h>
53
54#define HASH_SIZE_SHIFT 5
55#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
56
57static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
58{
59 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
60
61 return hash_32(hash, HASH_SIZE_SHIFT);
62}
63
64static int vti6_dev_init(struct net_device *dev);
65static void vti6_dev_setup(struct net_device *dev);
66static struct rtnl_link_ops vti6_link_ops __read_mostly;
67
68static int vti6_net_id __read_mostly;
69struct vti6_net {
70 /* the vti6 tunnel fallback device */
71 struct net_device *fb_tnl_dev;
72 /* lists for storing tunnels in use */
73 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
74 struct ip6_tnl __rcu *tnls_wc[1];
75 struct ip6_tnl __rcu **tnls[2];
76};
77
ed1efb2a
SK
78#define for_each_vti6_tunnel_rcu(start) \
79 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
80
81/**
82 * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
83 * @net: network namespace
84 * @remote: the address of the tunnel exit-point
85 * @local: the address of the tunnel entry-point
86 *
87 * Return:
88 * tunnel matching given end-points if found,
89 * else fallback tunnel if its device is up,
90 * else %NULL
91 **/
92static struct ip6_tnl *
93vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
94 const struct in6_addr *local)
95{
96 unsigned int hash = HASH(remote, local);
97 struct ip6_tnl *t;
98 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
99
100 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
101 if (ipv6_addr_equal(local, &t->parms.laddr) &&
102 ipv6_addr_equal(remote, &t->parms.raddr) &&
103 (t->dev->flags & IFF_UP))
104 return t;
105 }
106 t = rcu_dereference(ip6n->tnls_wc[0]);
107 if (t && (t->dev->flags & IFF_UP))
108 return t;
109
110 return NULL;
111}
112
113/**
114 * vti6_tnl_bucket - get head of list matching given tunnel parameters
115 * @p: parameters containing tunnel end-points
116 *
117 * Description:
118 * vti6_tnl_bucket() returns the head of the list matching the
119 * &struct in6_addr entries laddr and raddr in @p.
120 *
121 * Return: head of IPv6 tunnel list
122 **/
123static struct ip6_tnl __rcu **
124vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
125{
126 const struct in6_addr *remote = &p->raddr;
127 const struct in6_addr *local = &p->laddr;
128 unsigned int h = 0;
129 int prio = 0;
130
131 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
132 prio = 1;
133 h = HASH(remote, local);
134 }
135 return &ip6n->tnls[prio][h];
136}
137
138static void
139vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
140{
141 struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
142
143 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
144 rcu_assign_pointer(*tp, t);
145}
146
147static void
148vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
149{
150 struct ip6_tnl __rcu **tp;
151 struct ip6_tnl *iter;
152
153 for (tp = vti6_tnl_bucket(ip6n, &t->parms);
154 (iter = rtnl_dereference(*tp)) != NULL;
155 tp = &iter->next) {
156 if (t == iter) {
157 rcu_assign_pointer(*tp, t->next);
158 break;
159 }
160 }
161}
162
163static void vti6_dev_free(struct net_device *dev)
164{
165 free_percpu(dev->tstats);
166 free_netdev(dev);
167}
168
169static int vti6_tnl_create2(struct net_device *dev)
170{
171 struct ip6_tnl *t = netdev_priv(dev);
172 struct net *net = dev_net(dev);
173 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
174 int err;
175
176 err = vti6_dev_init(dev);
177 if (err < 0)
178 goto out;
179
180 err = register_netdevice(dev);
181 if (err < 0)
182 goto out;
183
184 strcpy(t->parms.name, dev->name);
185 dev->rtnl_link_ops = &vti6_link_ops;
186
187 dev_hold(dev);
188 vti6_tnl_link(ip6n, t);
189
190 return 0;
191
192out:
193 return err;
194}
195
196static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
197{
198 struct net_device *dev;
199 struct ip6_tnl *t;
200 char name[IFNAMSIZ];
201 int err;
202
203 if (p->name[0])
204 strlcpy(name, p->name, IFNAMSIZ);
205 else
206 sprintf(name, "ip6_vti%%d");
207
208 dev = alloc_netdev(sizeof(*t), name, vti6_dev_setup);
209 if (dev == NULL)
210 goto failed;
211
212 dev_net_set(dev, net);
213
214 t = netdev_priv(dev);
215 t->parms = *p;
216 t->net = dev_net(dev);
217
218 err = vti6_tnl_create2(dev);
219 if (err < 0)
220 goto failed_free;
221
222 return t;
223
224failed_free:
225 vti6_dev_free(dev);
226failed:
227 return NULL;
228}
229
230/**
231 * vti6_locate - find or create tunnel matching given parameters
232 * @net: network namespace
233 * @p: tunnel parameters
234 * @create: != 0 if allowed to create new tunnel if no match found
235 *
236 * Description:
237 * vti6_locate() first tries to locate an existing tunnel
238 * based on @parms. If this is unsuccessful, but @create is set a new
239 * tunnel device is created and registered for use.
240 *
241 * Return:
242 * matching tunnel or NULL
243 **/
244static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
245 int create)
246{
247 const struct in6_addr *remote = &p->raddr;
248 const struct in6_addr *local = &p->laddr;
249 struct ip6_tnl __rcu **tp;
250 struct ip6_tnl *t;
251 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
252
253 for (tp = vti6_tnl_bucket(ip6n, p);
254 (t = rtnl_dereference(*tp)) != NULL;
255 tp = &t->next) {
256 if (ipv6_addr_equal(local, &t->parms.laddr) &&
257 ipv6_addr_equal(remote, &t->parms.raddr))
258 return t;
259 }
260 if (!create)
261 return NULL;
262 return vti6_tnl_create(net, p);
263}
264
265/**
266 * vti6_dev_uninit - tunnel device uninitializer
267 * @dev: the device to be destroyed
268 *
269 * Description:
270 * vti6_dev_uninit() removes tunnel from its list
271 **/
272static void vti6_dev_uninit(struct net_device *dev)
273{
274 struct ip6_tnl *t = netdev_priv(dev);
275 struct net *net = dev_net(dev);
276 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
277
278 if (dev == ip6n->fb_tnl_dev)
279 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
280 else
281 vti6_tnl_unlink(ip6n, t);
282 ip6_tnl_dst_reset(t);
283 dev_put(dev);
284}
285
286static int vti6_rcv(struct sk_buff *skb)
287{
288 struct ip6_tnl *t;
289 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
290
291 rcu_read_lock();
292
293 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
294 &ipv6h->daddr)) != NULL) {
295 struct pcpu_tstats *tstats;
296
297 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
298 rcu_read_unlock();
299 goto discard;
300 }
301
302 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
303 rcu_read_unlock();
304 return 0;
305 }
306
307 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
308 t->dev->stats.rx_dropped++;
309 rcu_read_unlock();
310 goto discard;
311 }
312
313 tstats = this_cpu_ptr(t->dev->tstats);
469bdcef 314 u64_stats_update_begin(&tstats->syncp);
ed1efb2a
SK
315 tstats->rx_packets++;
316 tstats->rx_bytes += skb->len;
469bdcef 317 u64_stats_update_end(&tstats->syncp);
ed1efb2a
SK
318
319 skb->mark = 0;
320 secpath_reset(skb);
321 skb->dev = t->dev;
322
323 rcu_read_unlock();
324 return 0;
325 }
326 rcu_read_unlock();
327 return 1;
328
329discard:
330 kfree_skb(skb);
331 return 0;
332}
333
334/**
335 * vti6_addr_conflict - compare packet addresses to tunnel's own
336 * @t: the outgoing tunnel device
337 * @hdr: IPv6 header from the incoming packet
338 *
339 * Description:
340 * Avoid trivial tunneling loop by checking that tunnel exit-point
341 * doesn't match source of incoming packet.
342 *
343 * Return:
344 * 1 if conflict,
345 * 0 else
346 **/
347static inline bool
348vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
349{
350 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
351}
352
353/**
354 * vti6_xmit - send a packet
355 * @skb: the outgoing socket buffer
356 * @dev: the outgoing tunnel device
357 **/
358static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
359{
360 struct net *net = dev_net(dev);
361 struct ip6_tnl *t = netdev_priv(dev);
362 struct net_device_stats *stats = &t->dev->stats;
363 struct dst_entry *dst = NULL, *ndst = NULL;
364 struct flowi6 fl6;
365 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
366 struct net_device *tdev;
367 int err = -1;
368
369 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
370 !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
371 return err;
372
373 dst = ip6_tnl_dst_check(t);
374 if (!dst) {
375 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
376
377 ndst = ip6_route_output(net, NULL, &fl6);
378
379 if (ndst->error)
380 goto tx_err_link_failure;
381 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
382 if (IS_ERR(ndst)) {
383 err = PTR_ERR(ndst);
384 ndst = NULL;
385 goto tx_err_link_failure;
386 }
387 dst = ndst;
388 }
389
390 if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
391 goto tx_err_link_failure;
392
393 tdev = dst->dev;
394
395 if (tdev == dev) {
396 stats->collisions++;
397 net_warn_ratelimited("%s: Local routing loop detected!\n",
398 t->parms.name);
399 goto tx_err_dst_release;
400 }
401
402
403 skb_dst_drop(skb);
404 skb_dst_set_noref(skb, dst);
405
406 ip6tunnel_xmit(skb, dev);
407 if (ndst) {
408 dev->mtu = dst_mtu(ndst);
409 ip6_tnl_dst_store(t, ndst);
410 }
411
412 return 0;
413tx_err_link_failure:
414 stats->tx_carrier_errors++;
415 dst_link_failure(skb);
416tx_err_dst_release:
417 dst_release(ndst);
418 return err;
419}
420
421static netdev_tx_t
422vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
423{
424 struct ip6_tnl *t = netdev_priv(dev);
425 struct net_device_stats *stats = &t->dev->stats;
426 int ret;
427
428 switch (skb->protocol) {
429 case htons(ETH_P_IPV6):
430 ret = vti6_xmit(skb, dev);
431 break;
432 default:
433 goto tx_err;
434 }
435
436 if (ret < 0)
437 goto tx_err;
438
439 return NETDEV_TX_OK;
440
441tx_err:
442 stats->tx_errors++;
443 stats->tx_dropped++;
444 kfree_skb(skb);
445 return NETDEV_TX_OK;
446}
447
448static void vti6_link_config(struct ip6_tnl *t)
449{
450 struct dst_entry *dst;
451 struct net_device *dev = t->dev;
452 struct __ip6_tnl_parm *p = &t->parms;
453 struct flowi6 *fl6 = &t->fl.u.ip6;
454
455 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
456 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
457
458 /* Set up flowi template */
459 fl6->saddr = p->laddr;
460 fl6->daddr = p->raddr;
461 fl6->flowi6_oif = p->link;
462 fl6->flowi6_mark = be32_to_cpu(p->i_key);
463 fl6->flowi6_proto = p->proto;
464 fl6->flowlabel = 0;
465
466 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
467 IP6_TNL_F_CAP_PER_PACKET);
468 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
469
470 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
471 dev->flags |= IFF_POINTOPOINT;
472 else
473 dev->flags &= ~IFF_POINTOPOINT;
474
475 dev->iflink = p->link;
476
477 if (p->flags & IP6_TNL_F_CAP_XMIT) {
478
479 dst = ip6_route_output(dev_net(dev), NULL, fl6);
480 if (dst->error)
481 return;
482
483 dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
484 NULL, 0);
485 if (IS_ERR(dst))
486 return;
487
488 if (dst->dev) {
489 dev->hard_header_len = dst->dev->hard_header_len;
490
491 dev->mtu = dst_mtu(dst);
492
493 if (dev->mtu < IPV6_MIN_MTU)
494 dev->mtu = IPV6_MIN_MTU;
495 }
496 dst_release(dst);
497 }
498}
499
500/**
501 * vti6_tnl_change - update the tunnel parameters
502 * @t: tunnel to be changed
503 * @p: tunnel configuration parameters
504 *
505 * Description:
506 * vti6_tnl_change() updates the tunnel parameters
507 **/
508static int
509vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
510{
511 t->parms.laddr = p->laddr;
512 t->parms.raddr = p->raddr;
513 t->parms.link = p->link;
514 t->parms.i_key = p->i_key;
515 t->parms.o_key = p->o_key;
516 t->parms.proto = p->proto;
517 ip6_tnl_dst_reset(t);
518 vti6_link_config(t);
519 return 0;
520}
521
522static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
523{
524 struct net *net = dev_net(t->dev);
525 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
526 int err;
527
528 vti6_tnl_unlink(ip6n, t);
529 synchronize_net();
530 err = vti6_tnl_change(t, p);
531 vti6_tnl_link(ip6n, t);
532 netdev_state_change(t->dev);
533 return err;
534}
535
536static void
537vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
538{
539 p->laddr = u->laddr;
540 p->raddr = u->raddr;
541 p->link = u->link;
542 p->i_key = u->i_key;
543 p->o_key = u->o_key;
544 p->proto = u->proto;
545
546 memcpy(p->name, u->name, sizeof(u->name));
547}
548
549static void
550vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
551{
552 u->laddr = p->laddr;
553 u->raddr = p->raddr;
554 u->link = p->link;
555 u->i_key = p->i_key;
556 u->o_key = p->o_key;
557 u->proto = p->proto;
558
559 memcpy(u->name, p->name, sizeof(u->name));
560}
561
562/**
563 * vti6_tnl_ioctl - configure vti6 tunnels from userspace
564 * @dev: virtual device associated with tunnel
565 * @ifr: parameters passed from userspace
566 * @cmd: command to be performed
567 *
568 * Description:
569 * vti6_ioctl() is used for managing vti6 tunnels
570 * from userspace.
571 *
572 * The possible commands are the following:
573 * %SIOCGETTUNNEL: get tunnel parameters for device
574 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
575 * %SIOCCHGTUNNEL: change tunnel parameters to those given
576 * %SIOCDELTUNNEL: delete tunnel
577 *
578 * The fallback device "ip6_vti0", created during module
579 * initialization, can be used for creating other tunnel devices.
580 *
581 * Return:
582 * 0 on success,
583 * %-EFAULT if unable to copy data to or from userspace,
584 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
585 * %-EINVAL if passed tunnel parameters are invalid,
586 * %-EEXIST if changing a tunnel's parameters would cause a conflict
587 * %-ENODEV if attempting to change or delete a nonexisting device
588 **/
589static int
590vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
591{
592 int err = 0;
593 struct ip6_tnl_parm2 p;
594 struct __ip6_tnl_parm p1;
595 struct ip6_tnl *t = NULL;
596 struct net *net = dev_net(dev);
597 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
598
599 switch (cmd) {
600 case SIOCGETTUNNEL:
601 if (dev == ip6n->fb_tnl_dev) {
602 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
603 err = -EFAULT;
604 break;
605 }
606 vti6_parm_from_user(&p1, &p);
607 t = vti6_locate(net, &p1, 0);
608 } else {
609 memset(&p, 0, sizeof(p));
610 }
611 if (t == NULL)
612 t = netdev_priv(dev);
613 vti6_parm_to_user(&p, &t->parms);
614 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
615 err = -EFAULT;
616 break;
617 case SIOCADDTUNNEL:
618 case SIOCCHGTUNNEL:
619 err = -EPERM;
620 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
621 break;
622 err = -EFAULT;
623 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
624 break;
625 err = -EINVAL;
626 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
627 break;
628 vti6_parm_from_user(&p1, &p);
629 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
630 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
631 if (t != NULL) {
632 if (t->dev != dev) {
633 err = -EEXIST;
634 break;
635 }
636 } else
637 t = netdev_priv(dev);
638
639 err = vti6_update(t, &p1);
640 }
641 if (t) {
642 err = 0;
643 vti6_parm_to_user(&p, &t->parms);
644 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
645 err = -EFAULT;
646
647 } else
648 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
649 break;
650 case SIOCDELTUNNEL:
651 err = -EPERM;
652 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
653 break;
654
655 if (dev == ip6n->fb_tnl_dev) {
656 err = -EFAULT;
657 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
658 break;
659 err = -ENOENT;
660 vti6_parm_from_user(&p1, &p);
661 t = vti6_locate(net, &p1, 0);
662 if (t == NULL)
663 break;
664 err = -EPERM;
665 if (t->dev == ip6n->fb_tnl_dev)
666 break;
667 dev = t->dev;
668 }
669 err = 0;
670 unregister_netdevice(dev);
671 break;
672 default:
673 err = -EINVAL;
674 }
675 return err;
676}
677
678/**
679 * vti6_tnl_change_mtu - change mtu manually for tunnel device
680 * @dev: virtual device associated with tunnel
681 * @new_mtu: the new mtu
682 *
683 * Return:
684 * 0 on success,
685 * %-EINVAL if mtu too small
686 **/
687static int vti6_change_mtu(struct net_device *dev, int new_mtu)
688{
689 if (new_mtu < IPV6_MIN_MTU)
690 return -EINVAL;
691
692 dev->mtu = new_mtu;
693 return 0;
694}
695
696static const struct net_device_ops vti6_netdev_ops = {
697 .ndo_uninit = vti6_dev_uninit,
698 .ndo_start_xmit = vti6_tnl_xmit,
699 .ndo_do_ioctl = vti6_ioctl,
700 .ndo_change_mtu = vti6_change_mtu,
469bdcef 701 .ndo_get_stats64 = ip_tunnel_get_stats64,
ed1efb2a
SK
702};
703
704/**
705 * vti6_dev_setup - setup virtual tunnel device
706 * @dev: virtual device associated with tunnel
707 *
708 * Description:
709 * Initialize function pointers and device parameters
710 **/
711static void vti6_dev_setup(struct net_device *dev)
712{
713 struct ip6_tnl *t;
714
715 dev->netdev_ops = &vti6_netdev_ops;
716 dev->destructor = vti6_dev_free;
717
718 dev->type = ARPHRD_TUNNEL6;
719 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
720 dev->mtu = ETH_DATA_LEN;
721 t = netdev_priv(dev);
722 dev->flags |= IFF_NOARP;
723 dev->addr_len = sizeof(struct in6_addr);
724 dev->features |= NETIF_F_NETNS_LOCAL;
725 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
726}
727
728/**
729 * vti6_dev_init_gen - general initializer for all tunnel devices
730 * @dev: virtual device associated with tunnel
731 **/
732static inline int vti6_dev_init_gen(struct net_device *dev)
733{
734 struct ip6_tnl *t = netdev_priv(dev);
735
736 t->dev = dev;
737 t->net = dev_net(dev);
738 dev->tstats = alloc_percpu(struct pcpu_tstats);
739 if (!dev->tstats)
740 return -ENOMEM;
741 return 0;
742}
743
744/**
745 * vti6_dev_init - initializer for all non fallback tunnel devices
746 * @dev: virtual device associated with tunnel
747 **/
748static int vti6_dev_init(struct net_device *dev)
749{
750 struct ip6_tnl *t = netdev_priv(dev);
751 int err = vti6_dev_init_gen(dev);
752
753 if (err)
754 return err;
755 vti6_link_config(t);
756 return 0;
757}
758
759/**
760 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
761 * @dev: fallback device
762 *
763 * Return: 0
764 **/
765static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
766{
767 struct ip6_tnl *t = netdev_priv(dev);
768 struct net *net = dev_net(dev);
769 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
770 int err = vti6_dev_init_gen(dev);
771
772 if (err)
773 return err;
774
775 t->parms.proto = IPPROTO_IPV6;
776 dev_hold(dev);
777
778 vti6_link_config(t);
779
780 rcu_assign_pointer(ip6n->tnls_wc[0], t);
781 return 0;
782}
783
784static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
785{
786 return 0;
787}
788
789static void vti6_netlink_parms(struct nlattr *data[],
790 struct __ip6_tnl_parm *parms)
791{
792 memset(parms, 0, sizeof(*parms));
793
794 if (!data)
795 return;
796
797 if (data[IFLA_VTI_LINK])
798 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
799
800 if (data[IFLA_VTI_LOCAL])
801 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
802 sizeof(struct in6_addr));
803
804 if (data[IFLA_VTI_REMOTE])
805 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
806 sizeof(struct in6_addr));
807
808 if (data[IFLA_VTI_IKEY])
809 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
810
811 if (data[IFLA_VTI_OKEY])
812 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
813}
814
815static int vti6_newlink(struct net *src_net, struct net_device *dev,
816 struct nlattr *tb[], struct nlattr *data[])
817{
818 struct net *net = dev_net(dev);
819 struct ip6_tnl *nt;
820
821 nt = netdev_priv(dev);
822 vti6_netlink_parms(data, &nt->parms);
823
824 nt->parms.proto = IPPROTO_IPV6;
825
826 if (vti6_locate(net, &nt->parms, 0))
827 return -EEXIST;
828
829 return vti6_tnl_create2(dev);
830}
831
832static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
833 struct nlattr *data[])
834{
835 struct ip6_tnl *t;
836 struct __ip6_tnl_parm p;
837 struct net *net = dev_net(dev);
838 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
839
840 if (dev == ip6n->fb_tnl_dev)
841 return -EINVAL;
842
843 vti6_netlink_parms(data, &p);
844
845 t = vti6_locate(net, &p, 0);
846
847 if (t) {
848 if (t->dev != dev)
849 return -EEXIST;
850 } else
851 t = netdev_priv(dev);
852
853 return vti6_update(t, &p);
854}
855
856static size_t vti6_get_size(const struct net_device *dev)
857{
858 return
859 /* IFLA_VTI_LINK */
860 nla_total_size(4) +
861 /* IFLA_VTI_LOCAL */
862 nla_total_size(sizeof(struct in6_addr)) +
863 /* IFLA_VTI_REMOTE */
864 nla_total_size(sizeof(struct in6_addr)) +
865 /* IFLA_VTI_IKEY */
866 nla_total_size(4) +
867 /* IFLA_VTI_OKEY */
868 nla_total_size(4) +
869 0;
870}
871
872static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
873{
874 struct ip6_tnl *tunnel = netdev_priv(dev);
875 struct __ip6_tnl_parm *parm = &tunnel->parms;
876
877 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
878 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
879 &parm->laddr) ||
880 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
881 &parm->raddr) ||
882 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
883 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
884 goto nla_put_failure;
885 return 0;
886
887nla_put_failure:
888 return -EMSGSIZE;
889}
890
891static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
892 [IFLA_VTI_LINK] = { .type = NLA_U32 },
893 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
894 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
895 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
896 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
897};
898
899static struct rtnl_link_ops vti6_link_ops __read_mostly = {
900 .kind = "vti6",
901 .maxtype = IFLA_VTI_MAX,
902 .policy = vti6_policy,
903 .priv_size = sizeof(struct ip6_tnl),
904 .setup = vti6_dev_setup,
905 .validate = vti6_validate,
906 .newlink = vti6_newlink,
907 .changelink = vti6_changelink,
908 .get_size = vti6_get_size,
909 .fill_info = vti6_fill_info,
910};
911
912static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
913 .handler = vti6_rcv,
914 .priority = 1,
915};
916
917static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
918{
919 int h;
920 struct ip6_tnl *t;
921 LIST_HEAD(list);
922
923 for (h = 0; h < HASH_SIZE; h++) {
924 t = rtnl_dereference(ip6n->tnls_r_l[h]);
925 while (t != NULL) {
926 unregister_netdevice_queue(t->dev, &list);
927 t = rtnl_dereference(t->next);
928 }
929 }
930
931 t = rtnl_dereference(ip6n->tnls_wc[0]);
932 unregister_netdevice_queue(t->dev, &list);
933 unregister_netdevice_many(&list);
934}
935
936static int __net_init vti6_init_net(struct net *net)
937{
938 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
939 struct ip6_tnl *t = NULL;
940 int err;
941
942 ip6n->tnls[0] = ip6n->tnls_wc;
943 ip6n->tnls[1] = ip6n->tnls_r_l;
944
945 err = -ENOMEM;
946 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
947 vti6_dev_setup);
948
949 if (!ip6n->fb_tnl_dev)
950 goto err_alloc_dev;
951 dev_net_set(ip6n->fb_tnl_dev, net);
952
953 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
954 if (err < 0)
955 goto err_register;
956
957 err = register_netdev(ip6n->fb_tnl_dev);
958 if (err < 0)
959 goto err_register;
960
961 t = netdev_priv(ip6n->fb_tnl_dev);
962
963 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
964 return 0;
965
966err_register:
967 vti6_dev_free(ip6n->fb_tnl_dev);
968err_alloc_dev:
969 return err;
970}
971
972static void __net_exit vti6_exit_net(struct net *net)
973{
974 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
975
976 rtnl_lock();
977 vti6_destroy_tunnels(ip6n);
978 rtnl_unlock();
979}
980
981static struct pernet_operations vti6_net_ops = {
982 .init = vti6_init_net,
983 .exit = vti6_exit_net,
984 .id = &vti6_net_id,
985 .size = sizeof(struct vti6_net),
986};
987
988/**
989 * vti6_tunnel_init - register protocol and reserve needed resources
990 *
991 * Return: 0 on success
992 **/
993static int __init vti6_tunnel_init(void)
994{
995 int err;
996
997 err = register_pernet_device(&vti6_net_ops);
998 if (err < 0)
999 goto out_pernet;
1000
1001 err = xfrm6_mode_tunnel_input_register(&vti6_handler);
1002 if (err < 0) {
1003 pr_err("%s: can't register vti6\n", __func__);
1004 goto out;
1005 }
1006 err = rtnl_link_register(&vti6_link_ops);
1007 if (err < 0)
1008 goto rtnl_link_failed;
1009
1010 return 0;
1011
1012rtnl_link_failed:
1013 xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1014out:
1015 unregister_pernet_device(&vti6_net_ops);
1016out_pernet:
1017 return err;
1018}
1019
1020/**
1021 * vti6_tunnel_cleanup - free resources and unregister protocol
1022 **/
1023static void __exit vti6_tunnel_cleanup(void)
1024{
1025 rtnl_link_unregister(&vti6_link_ops);
1026 if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
1027 pr_info("%s: can't deregister vti6\n", __func__);
1028
1029 unregister_pernet_device(&vti6_net_ops);
1030}
1031
1032module_init(vti6_tunnel_init);
1033module_exit(vti6_tunnel_cleanup);
1034MODULE_LICENSE("GPL");
1035MODULE_ALIAS_RTNL_LINK("vti6");
1036MODULE_ALIAS_NETDEV("ip6_vti0");
1037MODULE_AUTHOR("Steffen Klassert");
1038MODULE_DESCRIPTION("IPv6 virtual tunnel interface");