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