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