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