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