blob: f5ba4d42b4ae73e713ed0f404e8e1c88de17e659 [file] [log] [blame]
Steffen Klasserted1efb22013-08-19 08:07:34 +02001/*
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>
Steffen Klasserted1efb22013-08-19 08:07:34 +020027#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
Steffen Klasserted1efb22013-08-19 08:07:34 +020077#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);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200281 dev_put(dev);
282}
283
284static int vti6_rcv(struct sk_buff *skb)
285{
286 struct ip6_tnl *t;
287 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
288
289 rcu_read_lock();
290
291 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
292 &ipv6h->daddr)) != NULL) {
Li RongQing8f849852014-01-04 13:57:59 +0800293 struct pcpu_sw_netstats *tstats;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200294
295 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
296 rcu_read_unlock();
297 goto discard;
298 }
299
300 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
301 rcu_read_unlock();
302 return 0;
303 }
304
305 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
306 t->dev->stats.rx_dropped++;
307 rcu_read_unlock();
308 goto discard;
309 }
310
311 tstats = this_cpu_ptr(t->dev->tstats);
Li RongQing469bdce2014-01-02 14:24:36 +0800312 u64_stats_update_begin(&tstats->syncp);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200313 tstats->rx_packets++;
314 tstats->rx_bytes += skb->len;
Li RongQing469bdce2014-01-02 14:24:36 +0800315 u64_stats_update_end(&tstats->syncp);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200316
317 skb->mark = 0;
318 secpath_reset(skb);
319 skb->dev = t->dev;
320
321 rcu_read_unlock();
322 return 0;
323 }
324 rcu_read_unlock();
325 return 1;
326
327discard:
328 kfree_skb(skb);
329 return 0;
330}
331
332/**
333 * vti6_addr_conflict - compare packet addresses to tunnel's own
334 * @t: the outgoing tunnel device
335 * @hdr: IPv6 header from the incoming packet
336 *
337 * Description:
338 * Avoid trivial tunneling loop by checking that tunnel exit-point
339 * doesn't match source of incoming packet.
340 *
341 * Return:
342 * 1 if conflict,
343 * 0 else
344 **/
345static inline bool
346vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
347{
348 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
349}
350
351/**
352 * vti6_xmit - send a packet
353 * @skb: the outgoing socket buffer
354 * @dev: the outgoing tunnel device
355 **/
356static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
357{
Steffen Klasserted1efb22013-08-19 08:07:34 +0200358 struct ip6_tnl *t = netdev_priv(dev);
359 struct net_device_stats *stats = &t->dev->stats;
Steffen Klassert7c852582014-03-14 07:28:08 +0100360 struct dst_entry *dst = skb_dst(skb);
361 struct flowi fl;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200362 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
363 struct net_device *tdev;
364 int err = -1;
365
366 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
367 !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
368 return err;
369
Steffen Klassert7c852582014-03-14 07:28:08 +0100370 memset(&fl, 0, sizeof(fl));
371 skb->mark = be32_to_cpu(t->parms.o_key);
372 xfrm_decode_session(skb, &fl, AF_INET6);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200373
Steffen Klassert7c852582014-03-14 07:28:08 +0100374 if (!dst)
375 goto tx_err_link_failure;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200376
Steffen Klassert7c852582014-03-14 07:28:08 +0100377 dst_hold(dst);
378 dst = xfrm_lookup(t->net, dst, &fl, NULL, 0);
379 if (IS_ERR(dst)) {
380 err = PTR_ERR(dst);
381 dst = NULL;
382 goto tx_err_link_failure;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200383 }
384
385 if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
386 goto tx_err_link_failure;
387
388 tdev = dst->dev;
389
390 if (tdev == dev) {
391 stats->collisions++;
392 net_warn_ratelimited("%s: Local routing loop detected!\n",
393 t->parms.name);
394 goto tx_err_dst_release;
395 }
396
397
Steffen Klassert7c852582014-03-14 07:28:08 +0100398 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
399 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
400 skb_dst_set(skb, dst);
401 skb->dev = skb_dst(skb)->dev;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200402
403 ip6tunnel_xmit(skb, dev);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200404
405 return 0;
406tx_err_link_failure:
407 stats->tx_carrier_errors++;
408 dst_link_failure(skb);
409tx_err_dst_release:
Steffen Klassert7c852582014-03-14 07:28:08 +0100410 dst_release(dst);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200411 return err;
412}
413
414static netdev_tx_t
415vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
416{
417 struct ip6_tnl *t = netdev_priv(dev);
418 struct net_device_stats *stats = &t->dev->stats;
419 int ret;
420
421 switch (skb->protocol) {
422 case htons(ETH_P_IPV6):
423 ret = vti6_xmit(skb, dev);
424 break;
425 default:
426 goto tx_err;
427 }
428
429 if (ret < 0)
430 goto tx_err;
431
432 return NETDEV_TX_OK;
433
434tx_err:
435 stats->tx_errors++;
436 stats->tx_dropped++;
437 kfree_skb(skb);
438 return NETDEV_TX_OK;
439}
440
441static void vti6_link_config(struct ip6_tnl *t)
442{
443 struct dst_entry *dst;
444 struct net_device *dev = t->dev;
445 struct __ip6_tnl_parm *p = &t->parms;
446 struct flowi6 *fl6 = &t->fl.u.ip6;
447
448 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
449 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
450
451 /* Set up flowi template */
452 fl6->saddr = p->laddr;
453 fl6->daddr = p->raddr;
454 fl6->flowi6_oif = p->link;
455 fl6->flowi6_mark = be32_to_cpu(p->i_key);
456 fl6->flowi6_proto = p->proto;
457 fl6->flowlabel = 0;
458
459 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
460 IP6_TNL_F_CAP_PER_PACKET);
461 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
462
463 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
464 dev->flags |= IFF_POINTOPOINT;
465 else
466 dev->flags &= ~IFF_POINTOPOINT;
467
468 dev->iflink = p->link;
469
470 if (p->flags & IP6_TNL_F_CAP_XMIT) {
471
472 dst = ip6_route_output(dev_net(dev), NULL, fl6);
473 if (dst->error)
474 return;
475
476 dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
477 NULL, 0);
478 if (IS_ERR(dst))
479 return;
480
481 if (dst->dev) {
482 dev->hard_header_len = dst->dev->hard_header_len;
483
484 dev->mtu = dst_mtu(dst);
485
486 if (dev->mtu < IPV6_MIN_MTU)
487 dev->mtu = IPV6_MIN_MTU;
488 }
489 dst_release(dst);
490 }
491}
492
493/**
494 * vti6_tnl_change - update the tunnel parameters
495 * @t: tunnel to be changed
496 * @p: tunnel configuration parameters
497 *
498 * Description:
499 * vti6_tnl_change() updates the tunnel parameters
500 **/
501static int
502vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
503{
504 t->parms.laddr = p->laddr;
505 t->parms.raddr = p->raddr;
506 t->parms.link = p->link;
507 t->parms.i_key = p->i_key;
508 t->parms.o_key = p->o_key;
509 t->parms.proto = p->proto;
510 ip6_tnl_dst_reset(t);
511 vti6_link_config(t);
512 return 0;
513}
514
515static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
516{
517 struct net *net = dev_net(t->dev);
518 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
519 int err;
520
521 vti6_tnl_unlink(ip6n, t);
522 synchronize_net();
523 err = vti6_tnl_change(t, p);
524 vti6_tnl_link(ip6n, t);
525 netdev_state_change(t->dev);
526 return err;
527}
528
529static void
530vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
531{
532 p->laddr = u->laddr;
533 p->raddr = u->raddr;
534 p->link = u->link;
535 p->i_key = u->i_key;
536 p->o_key = u->o_key;
537 p->proto = u->proto;
538
539 memcpy(p->name, u->name, sizeof(u->name));
540}
541
542static void
543vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
544{
545 u->laddr = p->laddr;
546 u->raddr = p->raddr;
547 u->link = p->link;
548 u->i_key = p->i_key;
549 u->o_key = p->o_key;
550 u->proto = p->proto;
551
552 memcpy(u->name, p->name, sizeof(u->name));
553}
554
555/**
556 * vti6_tnl_ioctl - configure vti6 tunnels from userspace
557 * @dev: virtual device associated with tunnel
558 * @ifr: parameters passed from userspace
559 * @cmd: command to be performed
560 *
561 * Description:
562 * vti6_ioctl() is used for managing vti6 tunnels
563 * from userspace.
564 *
565 * The possible commands are the following:
566 * %SIOCGETTUNNEL: get tunnel parameters for device
567 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
568 * %SIOCCHGTUNNEL: change tunnel parameters to those given
569 * %SIOCDELTUNNEL: delete tunnel
570 *
571 * The fallback device "ip6_vti0", created during module
572 * initialization, can be used for creating other tunnel devices.
573 *
574 * Return:
575 * 0 on success,
576 * %-EFAULT if unable to copy data to or from userspace,
577 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
578 * %-EINVAL if passed tunnel parameters are invalid,
579 * %-EEXIST if changing a tunnel's parameters would cause a conflict
580 * %-ENODEV if attempting to change or delete a nonexisting device
581 **/
582static int
583vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
584{
585 int err = 0;
586 struct ip6_tnl_parm2 p;
587 struct __ip6_tnl_parm p1;
588 struct ip6_tnl *t = NULL;
589 struct net *net = dev_net(dev);
590 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
591
592 switch (cmd) {
593 case SIOCGETTUNNEL:
594 if (dev == ip6n->fb_tnl_dev) {
595 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
596 err = -EFAULT;
597 break;
598 }
599 vti6_parm_from_user(&p1, &p);
600 t = vti6_locate(net, &p1, 0);
601 } else {
602 memset(&p, 0, sizeof(p));
603 }
604 if (t == NULL)
605 t = netdev_priv(dev);
606 vti6_parm_to_user(&p, &t->parms);
607 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
608 err = -EFAULT;
609 break;
610 case SIOCADDTUNNEL:
611 case SIOCCHGTUNNEL:
612 err = -EPERM;
613 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
614 break;
615 err = -EFAULT;
616 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
617 break;
618 err = -EINVAL;
619 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
620 break;
621 vti6_parm_from_user(&p1, &p);
622 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
623 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
624 if (t != NULL) {
625 if (t->dev != dev) {
626 err = -EEXIST;
627 break;
628 }
629 } else
630 t = netdev_priv(dev);
631
632 err = vti6_update(t, &p1);
633 }
634 if (t) {
635 err = 0;
636 vti6_parm_to_user(&p, &t->parms);
637 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
638 err = -EFAULT;
639
640 } else
641 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
642 break;
643 case SIOCDELTUNNEL:
644 err = -EPERM;
645 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
646 break;
647
648 if (dev == ip6n->fb_tnl_dev) {
649 err = -EFAULT;
650 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
651 break;
652 err = -ENOENT;
653 vti6_parm_from_user(&p1, &p);
654 t = vti6_locate(net, &p1, 0);
655 if (t == NULL)
656 break;
657 err = -EPERM;
658 if (t->dev == ip6n->fb_tnl_dev)
659 break;
660 dev = t->dev;
661 }
662 err = 0;
663 unregister_netdevice(dev);
664 break;
665 default:
666 err = -EINVAL;
667 }
668 return err;
669}
670
671/**
672 * vti6_tnl_change_mtu - change mtu manually for tunnel device
673 * @dev: virtual device associated with tunnel
674 * @new_mtu: the new mtu
675 *
676 * Return:
677 * 0 on success,
678 * %-EINVAL if mtu too small
679 **/
680static int vti6_change_mtu(struct net_device *dev, int new_mtu)
681{
682 if (new_mtu < IPV6_MIN_MTU)
683 return -EINVAL;
684
685 dev->mtu = new_mtu;
686 return 0;
687}
688
689static const struct net_device_ops vti6_netdev_ops = {
690 .ndo_uninit = vti6_dev_uninit,
691 .ndo_start_xmit = vti6_tnl_xmit,
692 .ndo_do_ioctl = vti6_ioctl,
693 .ndo_change_mtu = vti6_change_mtu,
Li RongQing469bdce2014-01-02 14:24:36 +0800694 .ndo_get_stats64 = ip_tunnel_get_stats64,
Steffen Klasserted1efb22013-08-19 08:07:34 +0200695};
696
697/**
698 * vti6_dev_setup - setup virtual tunnel device
699 * @dev: virtual device associated with tunnel
700 *
701 * Description:
702 * Initialize function pointers and device parameters
703 **/
704static void vti6_dev_setup(struct net_device *dev)
705{
706 struct ip6_tnl *t;
707
708 dev->netdev_ops = &vti6_netdev_ops;
709 dev->destructor = vti6_dev_free;
710
711 dev->type = ARPHRD_TUNNEL6;
712 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
713 dev->mtu = ETH_DATA_LEN;
714 t = netdev_priv(dev);
715 dev->flags |= IFF_NOARP;
716 dev->addr_len = sizeof(struct in6_addr);
717 dev->features |= NETIF_F_NETNS_LOCAL;
718 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
719}
720
721/**
722 * vti6_dev_init_gen - general initializer for all tunnel devices
723 * @dev: virtual device associated with tunnel
724 **/
725static inline int vti6_dev_init_gen(struct net_device *dev)
726{
727 struct ip6_tnl *t = netdev_priv(dev);
728
729 t->dev = dev;
730 t->net = dev_net(dev);
WANG Cong1c213bd2014-02-13 11:46:28 -0800731 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Steffen Klasserted1efb22013-08-19 08:07:34 +0200732 if (!dev->tstats)
733 return -ENOMEM;
Steffen Klasserted1efb22013-08-19 08:07:34 +0200734 return 0;
735}
736
737/**
738 * vti6_dev_init - initializer for all non fallback tunnel devices
739 * @dev: virtual device associated with tunnel
740 **/
741static int vti6_dev_init(struct net_device *dev)
742{
743 struct ip6_tnl *t = netdev_priv(dev);
744 int err = vti6_dev_init_gen(dev);
745
746 if (err)
747 return err;
748 vti6_link_config(t);
749 return 0;
750}
751
752/**
753 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
754 * @dev: fallback device
755 *
756 * Return: 0
757 **/
758static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
759{
760 struct ip6_tnl *t = netdev_priv(dev);
761 struct net *net = dev_net(dev);
762 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
763 int err = vti6_dev_init_gen(dev);
764
765 if (err)
766 return err;
767
768 t->parms.proto = IPPROTO_IPV6;
769 dev_hold(dev);
770
771 vti6_link_config(t);
772
773 rcu_assign_pointer(ip6n->tnls_wc[0], t);
774 return 0;
775}
776
777static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
778{
779 return 0;
780}
781
782static void vti6_netlink_parms(struct nlattr *data[],
783 struct __ip6_tnl_parm *parms)
784{
785 memset(parms, 0, sizeof(*parms));
786
787 if (!data)
788 return;
789
790 if (data[IFLA_VTI_LINK])
791 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
792
793 if (data[IFLA_VTI_LOCAL])
794 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
795 sizeof(struct in6_addr));
796
797 if (data[IFLA_VTI_REMOTE])
798 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
799 sizeof(struct in6_addr));
800
801 if (data[IFLA_VTI_IKEY])
802 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
803
804 if (data[IFLA_VTI_OKEY])
805 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
806}
807
808static int vti6_newlink(struct net *src_net, struct net_device *dev,
809 struct nlattr *tb[], struct nlattr *data[])
810{
811 struct net *net = dev_net(dev);
812 struct ip6_tnl *nt;
813
814 nt = netdev_priv(dev);
815 vti6_netlink_parms(data, &nt->parms);
816
817 nt->parms.proto = IPPROTO_IPV6;
818
819 if (vti6_locate(net, &nt->parms, 0))
820 return -EEXIST;
821
822 return vti6_tnl_create2(dev);
823}
824
825static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
826 struct nlattr *data[])
827{
828 struct ip6_tnl *t;
829 struct __ip6_tnl_parm p;
830 struct net *net = dev_net(dev);
831 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
832
833 if (dev == ip6n->fb_tnl_dev)
834 return -EINVAL;
835
836 vti6_netlink_parms(data, &p);
837
838 t = vti6_locate(net, &p, 0);
839
840 if (t) {
841 if (t->dev != dev)
842 return -EEXIST;
843 } else
844 t = netdev_priv(dev);
845
846 return vti6_update(t, &p);
847}
848
849static size_t vti6_get_size(const struct net_device *dev)
850{
851 return
852 /* IFLA_VTI_LINK */
853 nla_total_size(4) +
854 /* IFLA_VTI_LOCAL */
855 nla_total_size(sizeof(struct in6_addr)) +
856 /* IFLA_VTI_REMOTE */
857 nla_total_size(sizeof(struct in6_addr)) +
858 /* IFLA_VTI_IKEY */
859 nla_total_size(4) +
860 /* IFLA_VTI_OKEY */
861 nla_total_size(4) +
862 0;
863}
864
865static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
866{
867 struct ip6_tnl *tunnel = netdev_priv(dev);
868 struct __ip6_tnl_parm *parm = &tunnel->parms;
869
870 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
871 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
872 &parm->laddr) ||
873 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
874 &parm->raddr) ||
875 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
876 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
877 goto nla_put_failure;
878 return 0;
879
880nla_put_failure:
881 return -EMSGSIZE;
882}
883
884static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
885 [IFLA_VTI_LINK] = { .type = NLA_U32 },
886 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
887 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
888 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
889 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
890};
891
892static struct rtnl_link_ops vti6_link_ops __read_mostly = {
893 .kind = "vti6",
894 .maxtype = IFLA_VTI_MAX,
895 .policy = vti6_policy,
896 .priv_size = sizeof(struct ip6_tnl),
897 .setup = vti6_dev_setup,
898 .validate = vti6_validate,
899 .newlink = vti6_newlink,
900 .changelink = vti6_changelink,
901 .get_size = vti6_get_size,
902 .fill_info = vti6_fill_info,
903};
904
905static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
906 .handler = vti6_rcv,
907 .priority = 1,
908};
909
910static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
911{
912 int h;
913 struct ip6_tnl *t;
914 LIST_HEAD(list);
915
916 for (h = 0; h < HASH_SIZE; h++) {
917 t = rtnl_dereference(ip6n->tnls_r_l[h]);
918 while (t != NULL) {
919 unregister_netdevice_queue(t->dev, &list);
920 t = rtnl_dereference(t->next);
921 }
922 }
923
924 t = rtnl_dereference(ip6n->tnls_wc[0]);
925 unregister_netdevice_queue(t->dev, &list);
926 unregister_netdevice_many(&list);
927}
928
929static int __net_init vti6_init_net(struct net *net)
930{
931 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
932 struct ip6_tnl *t = NULL;
933 int err;
934
935 ip6n->tnls[0] = ip6n->tnls_wc;
936 ip6n->tnls[1] = ip6n->tnls_r_l;
937
938 err = -ENOMEM;
939 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
940 vti6_dev_setup);
941
942 if (!ip6n->fb_tnl_dev)
943 goto err_alloc_dev;
944 dev_net_set(ip6n->fb_tnl_dev, net);
945
946 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
947 if (err < 0)
948 goto err_register;
949
950 err = register_netdev(ip6n->fb_tnl_dev);
951 if (err < 0)
952 goto err_register;
953
954 t = netdev_priv(ip6n->fb_tnl_dev);
955
956 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
957 return 0;
958
959err_register:
960 vti6_dev_free(ip6n->fb_tnl_dev);
961err_alloc_dev:
962 return err;
963}
964
965static void __net_exit vti6_exit_net(struct net *net)
966{
967 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
968
969 rtnl_lock();
970 vti6_destroy_tunnels(ip6n);
971 rtnl_unlock();
972}
973
974static struct pernet_operations vti6_net_ops = {
975 .init = vti6_init_net,
976 .exit = vti6_exit_net,
977 .id = &vti6_net_id,
978 .size = sizeof(struct vti6_net),
979};
980
981/**
982 * vti6_tunnel_init - register protocol and reserve needed resources
983 *
984 * Return: 0 on success
985 **/
986static int __init vti6_tunnel_init(void)
987{
988 int err;
989
990 err = register_pernet_device(&vti6_net_ops);
991 if (err < 0)
992 goto out_pernet;
993
994 err = xfrm6_mode_tunnel_input_register(&vti6_handler);
995 if (err < 0) {
996 pr_err("%s: can't register vti6\n", __func__);
997 goto out;
998 }
999 err = rtnl_link_register(&vti6_link_ops);
1000 if (err < 0)
1001 goto rtnl_link_failed;
1002
1003 return 0;
1004
1005rtnl_link_failed:
1006 xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1007out:
1008 unregister_pernet_device(&vti6_net_ops);
1009out_pernet:
1010 return err;
1011}
1012
1013/**
1014 * vti6_tunnel_cleanup - free resources and unregister protocol
1015 **/
1016static void __exit vti6_tunnel_cleanup(void)
1017{
1018 rtnl_link_unregister(&vti6_link_ops);
1019 if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
1020 pr_info("%s: can't deregister vti6\n", __func__);
1021
1022 unregister_pernet_device(&vti6_net_ops);
1023}
1024
1025module_init(vti6_tunnel_init);
1026module_exit(vti6_tunnel_cleanup);
1027MODULE_LICENSE("GPL");
1028MODULE_ALIAS_RTNL_LINK("vti6");
1029MODULE_ALIAS_NETDEV("ip6_vti0");
1030MODULE_AUTHOR("Steffen Klassert");
1031MODULE_DESCRIPTION("IPv6 virtual tunnel interface");