ipv6: Allow sending packets through tunnels with wildcard endpoints
[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
c835a677 207 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
ed1efb2a
SK
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) &&
d814b847
SK
256 ipv6_addr_equal(remote, &t->parms.raddr)) {
257 if (create)
258 return NULL;
259
ed1efb2a 260 return t;
d814b847 261 }
ed1efb2a
SK
262 }
263 if (!create)
264 return NULL;
265 return vti6_tnl_create(net, p);
266}
267
268/**
269 * vti6_dev_uninit - tunnel device uninitializer
270 * @dev: the device to be destroyed
271 *
272 * Description:
273 * vti6_dev_uninit() removes tunnel from its list
274 **/
275static void vti6_dev_uninit(struct net_device *dev)
276{
277 struct ip6_tnl *t = netdev_priv(dev);
278 struct net *net = dev_net(dev);
279 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
280
281 if (dev == ip6n->fb_tnl_dev)
282 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
283 else
284 vti6_tnl_unlink(ip6n, t);
ed1efb2a
SK
285 dev_put(dev);
286}
287
288static int vti6_rcv(struct sk_buff *skb)
289{
290 struct ip6_tnl *t;
291 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
292
293 rcu_read_lock();
ed1efb2a
SK
294 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
295 &ipv6h->daddr)) != NULL) {
ed1efb2a
SK
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
fa9ad96d
SK
312 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
313 skb->mark = be32_to_cpu(t->parms.i_key);
ed1efb2a
SK
314
315 rcu_read_unlock();
fa9ad96d
SK
316
317 return xfrm6_rcv(skb);
ed1efb2a
SK
318 }
319 rcu_read_unlock();
fa9ad96d 320 return -EINVAL;
ed1efb2a
SK
321discard:
322 kfree_skb(skb);
323 return 0;
324}
325
fa9ad96d
SK
326static int vti6_rcv_cb(struct sk_buff *skb, int err)
327{
328 unsigned short family;
329 struct net_device *dev;
330 struct pcpu_sw_netstats *tstats;
331 struct xfrm_state *x;
332 struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
333
334 if (!t)
335 return 1;
336
337 dev = t->dev;
338
339 if (err) {
340 dev->stats.rx_errors++;
341 dev->stats.rx_dropped++;
342
343 return 0;
344 }
345
346 x = xfrm_input_state(skb);
347 family = x->inner_mode->afinfo->family;
348
349 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family))
350 return -EPERM;
351
352 skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
353 skb->dev = dev;
354
355 tstats = this_cpu_ptr(dev->tstats);
356 u64_stats_update_begin(&tstats->syncp);
357 tstats->rx_packets++;
358 tstats->rx_bytes += skb->len;
359 u64_stats_update_end(&tstats->syncp);
360
361 return 0;
362}
363
ed1efb2a
SK
364/**
365 * vti6_addr_conflict - compare packet addresses to tunnel's own
366 * @t: the outgoing tunnel device
367 * @hdr: IPv6 header from the incoming packet
368 *
369 * Description:
370 * Avoid trivial tunneling loop by checking that tunnel exit-point
371 * doesn't match source of incoming packet.
372 *
373 * Return:
374 * 1 if conflict,
375 * 0 else
376 **/
377static inline bool
378vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
379{
380 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
381}
382
26be8e2d
SK
383static bool vti6_state_check(const struct xfrm_state *x,
384 const struct in6_addr *dst,
385 const struct in6_addr *src)
386{
387 xfrm_address_t *daddr = (xfrm_address_t *)dst;
388 xfrm_address_t *saddr = (xfrm_address_t *)src;
389
390 /* if there is no transform then this tunnel is not functional.
391 * Or if the xfrm is not mode tunnel.
392 */
393 if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
394 x->props.family != AF_INET6)
395 return false;
396
397 if (ipv6_addr_any(dst))
398 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
399
400 if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
401 return false;
402
403 return true;
404}
405
ed1efb2a
SK
406/**
407 * vti6_xmit - send a packet
408 * @skb: the outgoing socket buffer
409 * @dev: the outgoing tunnel device
22e1b23d 410 * @fl: the flow informations for the xfrm_lookup
ed1efb2a 411 **/
22e1b23d
SK
412static int
413vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
ed1efb2a 414{
ed1efb2a
SK
415 struct ip6_tnl *t = netdev_priv(dev);
416 struct net_device_stats *stats = &t->dev->stats;
7c852581 417 struct dst_entry *dst = skb_dst(skb);
ed1efb2a 418 struct net_device *tdev;
d5005140 419 struct xfrm_state *x;
ed1efb2a
SK
420 int err = -1;
421
7c852581
SK
422 if (!dst)
423 goto tx_err_link_failure;
ed1efb2a 424
7c852581 425 dst_hold(dst);
22e1b23d 426 dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
7c852581
SK
427 if (IS_ERR(dst)) {
428 err = PTR_ERR(dst);
429 dst = NULL;
430 goto tx_err_link_failure;
ed1efb2a
SK
431 }
432
d5005140
SK
433 x = dst->xfrm;
434 if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
435 goto tx_err_link_failure;
436
437 if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
438 (const struct in6_addr *)&x->id.daddr))
ed1efb2a
SK
439 goto tx_err_link_failure;
440
441 tdev = dst->dev;
442
443 if (tdev == dev) {
444 stats->collisions++;
445 net_warn_ratelimited("%s: Local routing loop detected!\n",
446 t->parms.name);
447 goto tx_err_dst_release;
448 }
449
7c852581
SK
450 skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
451 skb_dst_set(skb, dst);
452 skb->dev = skb_dst(skb)->dev;
ed1efb2a 453
22e1b23d
SK
454 err = dst_output(skb);
455 if (net_xmit_eval(err) == 0) {
456 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
457
458 u64_stats_update_begin(&tstats->syncp);
459 tstats->tx_bytes += skb->len;
460 tstats->tx_packets++;
461 u64_stats_update_end(&tstats->syncp);
462 } else {
463 stats->tx_errors++;
464 stats->tx_aborted_errors++;
465 }
ed1efb2a
SK
466
467 return 0;
468tx_err_link_failure:
469 stats->tx_carrier_errors++;
470 dst_link_failure(skb);
471tx_err_dst_release:
7c852581 472 dst_release(dst);
ed1efb2a
SK
473 return err;
474}
475
476static netdev_tx_t
477vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
478{
479 struct ip6_tnl *t = netdev_priv(dev);
480 struct net_device_stats *stats = &t->dev->stats;
22e1b23d
SK
481 struct ipv6hdr *ipv6h;
482 struct flowi fl;
ed1efb2a
SK
483 int ret;
484
22e1b23d
SK
485 memset(&fl, 0, sizeof(fl));
486 skb->mark = be32_to_cpu(t->parms.o_key);
487
ed1efb2a
SK
488 switch (skb->protocol) {
489 case htons(ETH_P_IPV6):
22e1b23d
SK
490 ipv6h = ipv6_hdr(skb);
491
492 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
d5005140 493 vti6_addr_conflict(t, ipv6h))
22e1b23d
SK
494 goto tx_err;
495
496 xfrm_decode_session(skb, &fl, AF_INET6);
497 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
498 break;
499 case htons(ETH_P_IP):
500 xfrm_decode_session(skb, &fl, AF_INET);
501 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
ed1efb2a
SK
502 break;
503 default:
504 goto tx_err;
505 }
506
22e1b23d 507 ret = vti6_xmit(skb, dev, &fl);
ed1efb2a
SK
508 if (ret < 0)
509 goto tx_err;
510
511 return NETDEV_TX_OK;
512
513tx_err:
514 stats->tx_errors++;
515 stats->tx_dropped++;
516 kfree_skb(skb);
517 return NETDEV_TX_OK;
518}
519
fa9ad96d
SK
520static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
521 u8 type, u8 code, int offset, __be32 info)
522{
523 __be32 spi;
6d004d6c 524 __u32 mark;
fa9ad96d
SK
525 struct xfrm_state *x;
526 struct ip6_tnl *t;
527 struct ip_esp_hdr *esph;
528 struct ip_auth_hdr *ah;
529 struct ip_comp_hdr *ipch;
530 struct net *net = dev_net(skb->dev);
531 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
532 int protocol = iph->nexthdr;
533
534 t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
535 if (!t)
536 return -1;
537
6d004d6c
SK
538 mark = be32_to_cpu(t->parms.o_key);
539
fa9ad96d
SK
540 switch (protocol) {
541 case IPPROTO_ESP:
542 esph = (struct ip_esp_hdr *)(skb->data + offset);
543 spi = esph->spi;
544 break;
545 case IPPROTO_AH:
546 ah = (struct ip_auth_hdr *)(skb->data + offset);
547 spi = ah->spi;
548 break;
549 case IPPROTO_COMP:
550 ipch = (struct ip_comp_hdr *)(skb->data + offset);
551 spi = htonl(ntohs(ipch->cpi));
552 break;
553 default:
554 return 0;
555 }
556
557 if (type != ICMPV6_PKT_TOOBIG &&
558 type != NDISC_REDIRECT)
559 return 0;
560
6d004d6c 561 x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
fa9ad96d
SK
562 spi, protocol, AF_INET6);
563 if (!x)
564 return 0;
565
566 if (type == NDISC_REDIRECT)
567 ip6_redirect(skb, net, skb->dev->ifindex, 0);
568 else
569 ip6_update_pmtu(skb, net, info, 0, 0);
570 xfrm_state_put(x);
571
572 return 0;
573}
574
ed1efb2a
SK
575static void vti6_link_config(struct ip6_tnl *t)
576{
ed1efb2a
SK
577 struct net_device *dev = t->dev;
578 struct __ip6_tnl_parm *p = &t->parms;
ed1efb2a
SK
579
580 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
581 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
582
ed1efb2a
SK
583 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
584 IP6_TNL_F_CAP_PER_PACKET);
585 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
586
587 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
588 dev->flags |= IFF_POINTOPOINT;
589 else
590 dev->flags &= ~IFF_POINTOPOINT;
591
592 dev->iflink = p->link;
ed1efb2a
SK
593}
594
595/**
596 * vti6_tnl_change - update the tunnel parameters
597 * @t: tunnel to be changed
598 * @p: tunnel configuration parameters
599 *
600 * Description:
601 * vti6_tnl_change() updates the tunnel parameters
602 **/
603static int
604vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
605{
606 t->parms.laddr = p->laddr;
607 t->parms.raddr = p->raddr;
608 t->parms.link = p->link;
609 t->parms.i_key = p->i_key;
610 t->parms.o_key = p->o_key;
611 t->parms.proto = p->proto;
612 ip6_tnl_dst_reset(t);
613 vti6_link_config(t);
614 return 0;
615}
616
617static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
618{
619 struct net *net = dev_net(t->dev);
620 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
621 int err;
622
623 vti6_tnl_unlink(ip6n, t);
624 synchronize_net();
625 err = vti6_tnl_change(t, p);
626 vti6_tnl_link(ip6n, t);
627 netdev_state_change(t->dev);
628 return err;
629}
630
631static void
632vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
633{
634 p->laddr = u->laddr;
635 p->raddr = u->raddr;
636 p->link = u->link;
637 p->i_key = u->i_key;
638 p->o_key = u->o_key;
639 p->proto = u->proto;
640
641 memcpy(p->name, u->name, sizeof(u->name));
642}
643
644static void
645vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
646{
647 u->laddr = p->laddr;
648 u->raddr = p->raddr;
649 u->link = p->link;
650 u->i_key = p->i_key;
651 u->o_key = p->o_key;
652 u->proto = p->proto;
653
654 memcpy(u->name, p->name, sizeof(u->name));
655}
656
657/**
658 * vti6_tnl_ioctl - configure vti6 tunnels from userspace
659 * @dev: virtual device associated with tunnel
660 * @ifr: parameters passed from userspace
661 * @cmd: command to be performed
662 *
663 * Description:
664 * vti6_ioctl() is used for managing vti6 tunnels
665 * from userspace.
666 *
667 * The possible commands are the following:
668 * %SIOCGETTUNNEL: get tunnel parameters for device
669 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
670 * %SIOCCHGTUNNEL: change tunnel parameters to those given
671 * %SIOCDELTUNNEL: delete tunnel
672 *
673 * The fallback device "ip6_vti0", created during module
674 * initialization, can be used for creating other tunnel devices.
675 *
676 * Return:
677 * 0 on success,
678 * %-EFAULT if unable to copy data to or from userspace,
679 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
680 * %-EINVAL if passed tunnel parameters are invalid,
681 * %-EEXIST if changing a tunnel's parameters would cause a conflict
682 * %-ENODEV if attempting to change or delete a nonexisting device
683 **/
684static int
685vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
686{
687 int err = 0;
688 struct ip6_tnl_parm2 p;
689 struct __ip6_tnl_parm p1;
690 struct ip6_tnl *t = NULL;
691 struct net *net = dev_net(dev);
692 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
693
694 switch (cmd) {
695 case SIOCGETTUNNEL:
696 if (dev == ip6n->fb_tnl_dev) {
697 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
698 err = -EFAULT;
699 break;
700 }
701 vti6_parm_from_user(&p1, &p);
702 t = vti6_locate(net, &p1, 0);
703 } else {
704 memset(&p, 0, sizeof(p));
705 }
706 if (t == NULL)
707 t = netdev_priv(dev);
708 vti6_parm_to_user(&p, &t->parms);
709 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
710 err = -EFAULT;
711 break;
712 case SIOCADDTUNNEL:
713 case SIOCCHGTUNNEL:
714 err = -EPERM;
715 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
716 break;
717 err = -EFAULT;
718 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
719 break;
720 err = -EINVAL;
721 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
722 break;
723 vti6_parm_from_user(&p1, &p);
724 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
725 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
726 if (t != NULL) {
727 if (t->dev != dev) {
728 err = -EEXIST;
729 break;
730 }
731 } else
732 t = netdev_priv(dev);
733
734 err = vti6_update(t, &p1);
735 }
736 if (t) {
737 err = 0;
738 vti6_parm_to_user(&p, &t->parms);
739 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
740 err = -EFAULT;
741
742 } else
743 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
744 break;
745 case SIOCDELTUNNEL:
746 err = -EPERM;
747 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
748 break;
749
750 if (dev == ip6n->fb_tnl_dev) {
751 err = -EFAULT;
752 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
753 break;
754 err = -ENOENT;
755 vti6_parm_from_user(&p1, &p);
756 t = vti6_locate(net, &p1, 0);
757 if (t == NULL)
758 break;
759 err = -EPERM;
760 if (t->dev == ip6n->fb_tnl_dev)
761 break;
762 dev = t->dev;
763 }
764 err = 0;
765 unregister_netdevice(dev);
766 break;
767 default:
768 err = -EINVAL;
769 }
770 return err;
771}
772
773/**
774 * vti6_tnl_change_mtu - change mtu manually for tunnel device
775 * @dev: virtual device associated with tunnel
776 * @new_mtu: the new mtu
777 *
778 * Return:
779 * 0 on success,
780 * %-EINVAL if mtu too small
781 **/
782static int vti6_change_mtu(struct net_device *dev, int new_mtu)
783{
784 if (new_mtu < IPV6_MIN_MTU)
785 return -EINVAL;
786
787 dev->mtu = new_mtu;
788 return 0;
789}
790
791static const struct net_device_ops vti6_netdev_ops = {
792 .ndo_uninit = vti6_dev_uninit,
793 .ndo_start_xmit = vti6_tnl_xmit,
794 .ndo_do_ioctl = vti6_ioctl,
795 .ndo_change_mtu = vti6_change_mtu,
469bdcef 796 .ndo_get_stats64 = ip_tunnel_get_stats64,
ed1efb2a
SK
797};
798
799/**
800 * vti6_dev_setup - setup virtual tunnel device
801 * @dev: virtual device associated with tunnel
802 *
803 * Description:
804 * Initialize function pointers and device parameters
805 **/
806static void vti6_dev_setup(struct net_device *dev)
807{
ed1efb2a
SK
808 dev->netdev_ops = &vti6_netdev_ops;
809 dev->destructor = vti6_dev_free;
810
811 dev->type = ARPHRD_TUNNEL6;
812 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
813 dev->mtu = ETH_DATA_LEN;
ed1efb2a
SK
814 dev->flags |= IFF_NOARP;
815 dev->addr_len = sizeof(struct in6_addr);
02875878 816 netif_keep_dst(dev);
ed1efb2a
SK
817}
818
819/**
820 * vti6_dev_init_gen - general initializer for all tunnel devices
821 * @dev: virtual device associated with tunnel
822 **/
823static inline int vti6_dev_init_gen(struct net_device *dev)
824{
825 struct ip6_tnl *t = netdev_priv(dev);
826
827 t->dev = dev;
828 t->net = dev_net(dev);
1c213bd2 829 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
ed1efb2a
SK
830 if (!dev->tstats)
831 return -ENOMEM;
832 return 0;
833}
834
835/**
836 * vti6_dev_init - initializer for all non fallback tunnel devices
837 * @dev: virtual device associated with tunnel
838 **/
839static int vti6_dev_init(struct net_device *dev)
840{
841 struct ip6_tnl *t = netdev_priv(dev);
842 int err = vti6_dev_init_gen(dev);
843
844 if (err)
845 return err;
846 vti6_link_config(t);
847 return 0;
848}
849
850/**
851 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
852 * @dev: fallback device
853 *
854 * Return: 0
855 **/
856static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
857{
858 struct ip6_tnl *t = netdev_priv(dev);
859 struct net *net = dev_net(dev);
860 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
861 int err = vti6_dev_init_gen(dev);
862
863 if (err)
864 return err;
865
866 t->parms.proto = IPPROTO_IPV6;
867 dev_hold(dev);
868
869 vti6_link_config(t);
870
871 rcu_assign_pointer(ip6n->tnls_wc[0], t);
872 return 0;
873}
874
875static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
876{
877 return 0;
878}
879
880static void vti6_netlink_parms(struct nlattr *data[],
881 struct __ip6_tnl_parm *parms)
882{
883 memset(parms, 0, sizeof(*parms));
884
885 if (!data)
886 return;
887
888 if (data[IFLA_VTI_LINK])
889 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
890
891 if (data[IFLA_VTI_LOCAL])
892 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
893 sizeof(struct in6_addr));
894
895 if (data[IFLA_VTI_REMOTE])
896 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
897 sizeof(struct in6_addr));
898
899 if (data[IFLA_VTI_IKEY])
900 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
901
902 if (data[IFLA_VTI_OKEY])
903 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
904}
905
906static int vti6_newlink(struct net *src_net, struct net_device *dev,
907 struct nlattr *tb[], struct nlattr *data[])
908{
909 struct net *net = dev_net(dev);
910 struct ip6_tnl *nt;
911
912 nt = netdev_priv(dev);
913 vti6_netlink_parms(data, &nt->parms);
914
915 nt->parms.proto = IPPROTO_IPV6;
916
917 if (vti6_locate(net, &nt->parms, 0))
918 return -EEXIST;
919
920 return vti6_tnl_create2(dev);
921}
922
923static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
924 struct nlattr *data[])
925{
926 struct ip6_tnl *t;
927 struct __ip6_tnl_parm p;
928 struct net *net = dev_net(dev);
929 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
930
931 if (dev == ip6n->fb_tnl_dev)
932 return -EINVAL;
933
934 vti6_netlink_parms(data, &p);
935
936 t = vti6_locate(net, &p, 0);
937
938 if (t) {
939 if (t->dev != dev)
940 return -EEXIST;
941 } else
942 t = netdev_priv(dev);
943
944 return vti6_update(t, &p);
945}
946
947static size_t vti6_get_size(const struct net_device *dev)
948{
949 return
950 /* IFLA_VTI_LINK */
951 nla_total_size(4) +
952 /* IFLA_VTI_LOCAL */
953 nla_total_size(sizeof(struct in6_addr)) +
954 /* IFLA_VTI_REMOTE */
955 nla_total_size(sizeof(struct in6_addr)) +
956 /* IFLA_VTI_IKEY */
957 nla_total_size(4) +
958 /* IFLA_VTI_OKEY */
959 nla_total_size(4) +
960 0;
961}
962
963static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
964{
965 struct ip6_tnl *tunnel = netdev_priv(dev);
966 struct __ip6_tnl_parm *parm = &tunnel->parms;
967
968 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
969 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
970 &parm->laddr) ||
971 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
972 &parm->raddr) ||
973 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
974 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
975 goto nla_put_failure;
976 return 0;
977
978nla_put_failure:
979 return -EMSGSIZE;
980}
981
982static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
983 [IFLA_VTI_LINK] = { .type = NLA_U32 },
984 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
985 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
986 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
987 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
988};
989
990static struct rtnl_link_ops vti6_link_ops __read_mostly = {
991 .kind = "vti6",
992 .maxtype = IFLA_VTI_MAX,
993 .policy = vti6_policy,
994 .priv_size = sizeof(struct ip6_tnl),
995 .setup = vti6_dev_setup,
996 .validate = vti6_validate,
997 .newlink = vti6_newlink,
998 .changelink = vti6_changelink,
999 .get_size = vti6_get_size,
1000 .fill_info = vti6_fill_info,
1001};
1002
ed1efb2a
SK
1003static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
1004{
1005 int h;
1006 struct ip6_tnl *t;
1007 LIST_HEAD(list);
1008
1009 for (h = 0; h < HASH_SIZE; h++) {
1010 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1011 while (t != NULL) {
1012 unregister_netdevice_queue(t->dev, &list);
1013 t = rtnl_dereference(t->next);
1014 }
1015 }
1016
1017 t = rtnl_dereference(ip6n->tnls_wc[0]);
1018 unregister_netdevice_queue(t->dev, &list);
1019 unregister_netdevice_many(&list);
1020}
1021
1022static int __net_init vti6_init_net(struct net *net)
1023{
1024 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1025 struct ip6_tnl *t = NULL;
1026 int err;
1027
1028 ip6n->tnls[0] = ip6n->tnls_wc;
1029 ip6n->tnls[1] = ip6n->tnls_r_l;
1030
1031 err = -ENOMEM;
1032 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
c835a677 1033 NET_NAME_UNKNOWN, vti6_dev_setup);
ed1efb2a
SK
1034
1035 if (!ip6n->fb_tnl_dev)
1036 goto err_alloc_dev;
1037 dev_net_set(ip6n->fb_tnl_dev, net);
1038
1039 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1040 if (err < 0)
1041 goto err_register;
1042
1043 err = register_netdev(ip6n->fb_tnl_dev);
1044 if (err < 0)
1045 goto err_register;
1046
1047 t = netdev_priv(ip6n->fb_tnl_dev);
1048
1049 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
1050 return 0;
1051
1052err_register:
1053 vti6_dev_free(ip6n->fb_tnl_dev);
1054err_alloc_dev:
1055 return err;
1056}
1057
1058static void __net_exit vti6_exit_net(struct net *net)
1059{
1060 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
1061
1062 rtnl_lock();
1063 vti6_destroy_tunnels(ip6n);
1064 rtnl_unlock();
1065}
1066
1067static struct pernet_operations vti6_net_ops = {
1068 .init = vti6_init_net,
1069 .exit = vti6_exit_net,
1070 .id = &vti6_net_id,
1071 .size = sizeof(struct vti6_net),
1072};
1073
fa9ad96d
SK
1074static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
1075 .handler = vti6_rcv,
1076 .cb_handler = vti6_rcv_cb,
1077 .err_handler = vti6_err,
1078 .priority = 100,
1079};
1080
1081static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
1082 .handler = vti6_rcv,
1083 .cb_handler = vti6_rcv_cb,
1084 .err_handler = vti6_err,
1085 .priority = 100,
1086};
1087
1088static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
1089 .handler = vti6_rcv,
1090 .cb_handler = vti6_rcv_cb,
1091 .err_handler = vti6_err,
1092 .priority = 100,
1093};
1094
ed1efb2a
SK
1095/**
1096 * vti6_tunnel_init - register protocol and reserve needed resources
1097 *
1098 * Return: 0 on success
1099 **/
1100static int __init vti6_tunnel_init(void)
1101{
e59d82fd
MK
1102 const char *msg;
1103 int err;
ed1efb2a 1104
e59d82fd 1105 msg = "tunnel device";
ed1efb2a
SK
1106 err = register_pernet_device(&vti6_net_ops);
1107 if (err < 0)
e59d82fd 1108 goto pernet_dev_failed;
ed1efb2a 1109
e59d82fd 1110 msg = "tunnel protocols";
fa9ad96d 1111 err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
e59d82fd
MK
1112 if (err < 0)
1113 goto xfrm_proto_esp_failed;
fa9ad96d 1114 err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
e59d82fd
MK
1115 if (err < 0)
1116 goto xfrm_proto_ah_failed;
fa9ad96d 1117 err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
e59d82fd
MK
1118 if (err < 0)
1119 goto xfrm_proto_comp_failed;
fa9ad96d 1120
e59d82fd 1121 msg = "netlink interface";
ed1efb2a
SK
1122 err = rtnl_link_register(&vti6_link_ops);
1123 if (err < 0)
1124 goto rtnl_link_failed;
1125
1126 return 0;
1127
1128rtnl_link_failed:
fa9ad96d 1129 xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
e59d82fd 1130xfrm_proto_comp_failed:
fa9ad96d 1131 xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
e59d82fd 1132xfrm_proto_ah_failed:
fa9ad96d 1133 xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
e59d82fd 1134xfrm_proto_esp_failed:
ed1efb2a 1135 unregister_pernet_device(&vti6_net_ops);
e59d82fd
MK
1136pernet_dev_failed:
1137 pr_err("vti6 init: failed to register %s\n", msg);
ed1efb2a
SK
1138 return err;
1139}
1140
1141/**
1142 * vti6_tunnel_cleanup - free resources and unregister protocol
1143 **/
1144static void __exit vti6_tunnel_cleanup(void)
1145{
1146 rtnl_link_unregister(&vti6_link_ops);
e59d82fd
MK
1147 xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
1148 xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
1149 xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
ed1efb2a
SK
1150 unregister_pernet_device(&vti6_net_ops);
1151}
1152
1153module_init(vti6_tunnel_init);
1154module_exit(vti6_tunnel_cleanup);
1155MODULE_LICENSE("GPL");
1156MODULE_ALIAS_RTNL_LINK("vti6");
1157MODULE_ALIAS_NETDEV("ip6_vti0");
1158MODULE_AUTHOR("Steffen Klassert");
1159MODULE_DESCRIPTION("IPv6 virtual tunnel interface");
This page took 0.128018 seconds and 5 git commands to generate.