blob: 516188b0dc1e4842a43350c2987de0ece26464a9 [file] [log] [blame]
Saurabh11814122012-07-17 09:44:54 +00001/*
2 * Linux NET3: IP/IP protocol decoder modified to support
3 * virtual tunnel interface
4 *
5 * Authors:
6 * Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
14
15/*
16 This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
17
18 For comments look at net/ipv4/ip_gre.c --ANK
19 */
20
21
22#include <linux/capability.h>
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/uaccess.h>
27#include <linux/skbuff.h>
28#include <linux/netdevice.h>
29#include <linux/in.h>
30#include <linux/tcp.h>
31#include <linux/udp.h>
32#include <linux/if_arp.h>
33#include <linux/mroute.h>
34#include <linux/init.h>
35#include <linux/netfilter_ipv4.h>
36#include <linux/if_ether.h>
37
38#include <net/sock.h>
39#include <net/ip.h>
40#include <net/icmp.h>
41#include <net/ipip.h>
42#include <net/inet_ecn.h>
43#include <net/xfrm.h>
44#include <net/net_namespace.h>
45#include <net/netns/generic.h>
46
47#define HASH_SIZE 16
48#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&(HASH_SIZE-1))
49
50static struct rtnl_link_ops vti_link_ops __read_mostly;
51
52static int vti_net_id __read_mostly;
53struct vti_net {
54 struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
55 struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
56 struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
57 struct ip_tunnel __rcu *tunnels_wc[1];
Saurabhe7d4b182012-07-23 07:52:04 +000058 struct ip_tunnel __rcu **tunnels[4];
Saurabh11814122012-07-17 09:44:54 +000059
60 struct net_device *fb_tunnel_dev;
61};
62
63static int vti_fb_tunnel_init(struct net_device *dev);
64static int vti_tunnel_init(struct net_device *dev);
65static void vti_tunnel_setup(struct net_device *dev);
66static void vti_dev_free(struct net_device *dev);
67static int vti_tunnel_bind_dev(struct net_device *dev);
68
Saurabh11814122012-07-17 09:44:54 +000069#define VTI_XMIT(stats1, stats2) do { \
70 int err; \
71 int pkt_len = skb->len; \
72 err = dst_output(skb); \
73 if (net_xmit_eval(err) == 0) { \
74 u64_stats_update_begin(&(stats1)->syncp); \
75 (stats1)->tx_bytes += pkt_len; \
76 (stats1)->tx_packets++; \
77 u64_stats_update_end(&(stats1)->syncp); \
78 } else { \
79 (stats2)->tx_errors++; \
80 (stats2)->tx_aborted_errors++; \
81 } \
82} while (0)
83
84
85static struct rtnl_link_stats64 *vti_get_stats64(struct net_device *dev,
86 struct rtnl_link_stats64 *tot)
87{
88 int i;
89
90 for_each_possible_cpu(i) {
91 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
92 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
93 unsigned int start;
94
95 do {
96 start = u64_stats_fetch_begin_bh(&tstats->syncp);
97 rx_packets = tstats->rx_packets;
98 tx_packets = tstats->tx_packets;
99 rx_bytes = tstats->rx_bytes;
100 tx_bytes = tstats->tx_bytes;
101 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
102
103 tot->rx_packets += rx_packets;
104 tot->tx_packets += tx_packets;
105 tot->rx_bytes += rx_bytes;
106 tot->tx_bytes += tx_bytes;
107 }
108
109 tot->multicast = dev->stats.multicast;
110 tot->rx_crc_errors = dev->stats.rx_crc_errors;
111 tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
112 tot->rx_length_errors = dev->stats.rx_length_errors;
113 tot->rx_errors = dev->stats.rx_errors;
114 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
115 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
116 tot->tx_dropped = dev->stats.tx_dropped;
117 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
118 tot->tx_errors = dev->stats.tx_errors;
119
120 return tot;
121}
122
123static struct ip_tunnel *vti_tunnel_lookup(struct net *net,
124 __be32 remote, __be32 local)
125{
126 unsigned h0 = HASH(remote);
127 unsigned h1 = HASH(local);
128 struct ip_tunnel *t;
129 struct vti_net *ipn = net_generic(net, vti_net_id);
130
Amerigo Wange086cad2012-11-11 21:52:34 +0000131 for_each_ip_tunnel_rcu(t, ipn->tunnels_r_l[h0 ^ h1])
Saurabh11814122012-07-17 09:44:54 +0000132 if (local == t->parms.iph.saddr &&
133 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
134 return t;
Amerigo Wange086cad2012-11-11 21:52:34 +0000135 for_each_ip_tunnel_rcu(t, ipn->tunnels_r[h0])
Saurabh11814122012-07-17 09:44:54 +0000136 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
137 return t;
138
Amerigo Wange086cad2012-11-11 21:52:34 +0000139 for_each_ip_tunnel_rcu(t, ipn->tunnels_l[h1])
Saurabh11814122012-07-17 09:44:54 +0000140 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
141 return t;
142
Amerigo Wange086cad2012-11-11 21:52:34 +0000143 for_each_ip_tunnel_rcu(t, ipn->tunnels_wc[0])
Saurabh11814122012-07-17 09:44:54 +0000144 if (t && (t->dev->flags&IFF_UP))
145 return t;
146 return NULL;
147}
148
Saurabhe7d4b182012-07-23 07:52:04 +0000149static struct ip_tunnel __rcu **__vti_bucket(struct vti_net *ipn,
150 struct ip_tunnel_parm *parms)
Saurabh11814122012-07-17 09:44:54 +0000151{
152 __be32 remote = parms->iph.daddr;
153 __be32 local = parms->iph.saddr;
154 unsigned h = 0;
155 int prio = 0;
156
157 if (remote) {
158 prio |= 2;
159 h ^= HASH(remote);
160 }
161 if (local) {
162 prio |= 1;
163 h ^= HASH(local);
164 }
165 return &ipn->tunnels[prio][h];
166}
167
Saurabhe7d4b182012-07-23 07:52:04 +0000168static inline struct ip_tunnel __rcu **vti_bucket(struct vti_net *ipn,
169 struct ip_tunnel *t)
Saurabh11814122012-07-17 09:44:54 +0000170{
171 return __vti_bucket(ipn, &t->parms);
172}
173
174static void vti_tunnel_unlink(struct vti_net *ipn, struct ip_tunnel *t)
175{
176 struct ip_tunnel __rcu **tp;
177 struct ip_tunnel *iter;
178
179 for (tp = vti_bucket(ipn, t);
180 (iter = rtnl_dereference(*tp)) != NULL;
181 tp = &iter->next) {
182 if (t == iter) {
183 rcu_assign_pointer(*tp, t->next);
184 break;
185 }
186 }
187}
188
189static void vti_tunnel_link(struct vti_net *ipn, struct ip_tunnel *t)
190{
191 struct ip_tunnel __rcu **tp = vti_bucket(ipn, t);
192
193 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
194 rcu_assign_pointer(*tp, t);
195}
196
197static struct ip_tunnel *vti_tunnel_locate(struct net *net,
198 struct ip_tunnel_parm *parms,
199 int create)
200{
201 __be32 remote = parms->iph.daddr;
202 __be32 local = parms->iph.saddr;
203 struct ip_tunnel *t, *nt;
204 struct ip_tunnel __rcu **tp;
205 struct net_device *dev;
206 char name[IFNAMSIZ];
207 struct vti_net *ipn = net_generic(net, vti_net_id);
208
209 for (tp = __vti_bucket(ipn, parms);
210 (t = rtnl_dereference(*tp)) != NULL;
211 tp = &t->next) {
212 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
213 return t;
214 }
215 if (!create)
216 return NULL;
217
218 if (parms->name[0])
219 strlcpy(name, parms->name, IFNAMSIZ);
220 else
221 strcpy(name, "vti%d");
222
223 dev = alloc_netdev(sizeof(*t), name, vti_tunnel_setup);
224 if (dev == NULL)
225 return NULL;
226
227 dev_net_set(dev, net);
228
229 nt = netdev_priv(dev);
230 nt->parms = *parms;
231 dev->rtnl_link_ops = &vti_link_ops;
232
233 vti_tunnel_bind_dev(dev);
234
235 if (register_netdevice(dev) < 0)
236 goto failed_free;
237
238 dev_hold(dev);
239 vti_tunnel_link(ipn, nt);
240 return nt;
241
242failed_free:
243 free_netdev(dev);
244 return NULL;
245}
246
247static void vti_tunnel_uninit(struct net_device *dev)
248{
249 struct net *net = dev_net(dev);
250 struct vti_net *ipn = net_generic(net, vti_net_id);
251
252 vti_tunnel_unlink(ipn, netdev_priv(dev));
253 dev_put(dev);
254}
255
256static int vti_err(struct sk_buff *skb, u32 info)
257{
258
259 /* All the routers (except for Linux) return only
260 * 8 bytes of packet payload. It means, that precise relaying of
261 * ICMP in the real Internet is absolutely infeasible.
262 */
263 struct iphdr *iph = (struct iphdr *)skb->data;
264 const int type = icmp_hdr(skb)->type;
265 const int code = icmp_hdr(skb)->code;
266 struct ip_tunnel *t;
267 int err;
268
269 switch (type) {
270 default:
271 case ICMP_PARAMETERPROB:
272 return 0;
273
274 case ICMP_DEST_UNREACH:
275 switch (code) {
276 case ICMP_SR_FAILED:
277 case ICMP_PORT_UNREACH:
278 /* Impossible event. */
279 return 0;
280 default:
281 /* All others are translated to HOST_UNREACH. */
282 break;
283 }
284 break;
285 case ICMP_TIME_EXCEEDED:
286 if (code != ICMP_EXC_TTL)
287 return 0;
288 break;
289 }
290
291 err = -ENOENT;
292
Saurabh11814122012-07-17 09:44:54 +0000293 t = vti_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
294 if (t == NULL)
295 goto out;
296
297 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
298 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
299 t->parms.link, 0, IPPROTO_IPIP, 0);
300 err = 0;
301 goto out;
302 }
303
304 err = 0;
305 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
306 goto out;
307
308 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
309 t->err_count++;
310 else
311 t->err_count = 1;
312 t->err_time = jiffies;
313out:
Saurabh11814122012-07-17 09:44:54 +0000314 return err;
315}
316
317/* We dont digest the packet therefore let the packet pass */
318static int vti_rcv(struct sk_buff *skb)
319{
320 struct ip_tunnel *tunnel;
321 const struct iphdr *iph = ip_hdr(skb);
322
Saurabh11814122012-07-17 09:44:54 +0000323 tunnel = vti_tunnel_lookup(dev_net(skb->dev), iph->saddr, iph->daddr);
324 if (tunnel != NULL) {
325 struct pcpu_tstats *tstats;
326
327 tstats = this_cpu_ptr(tunnel->dev->tstats);
328 u64_stats_update_begin(&tstats->syncp);
329 tstats->rx_packets++;
330 tstats->rx_bytes += skb->len;
331 u64_stats_update_end(&tstats->syncp);
332
333 skb->dev = tunnel->dev;
Saurabh11814122012-07-17 09:44:54 +0000334 return 1;
335 }
Saurabh11814122012-07-17 09:44:54 +0000336
337 return -1;
338}
339
340/* This function assumes it is being called from dev_queue_xmit()
341 * and that skb is filled properly by that function.
342 */
343
344static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
345{
346 struct ip_tunnel *tunnel = netdev_priv(dev);
347 struct pcpu_tstats *tstats;
348 struct iphdr *tiph = &tunnel->parms.iph;
349 u8 tos;
350 struct rtable *rt; /* Route to the other host */
351 struct net_device *tdev; /* Device to other host */
352 struct iphdr *old_iph = ip_hdr(skb);
353 __be32 dst = tiph->daddr;
354 struct flowi4 fl4;
355
356 if (skb->protocol != htons(ETH_P_IP))
357 goto tx_error;
358
359 tos = old_iph->tos;
360
361 memset(&fl4, 0, sizeof(fl4));
362 flowi4_init_output(&fl4, tunnel->parms.link,
stephen hemminger8437e762012-10-11 12:51:28 +0000363 be32_to_cpu(tunnel->parms.i_key), RT_TOS(tos),
Saurabh11814122012-07-17 09:44:54 +0000364 RT_SCOPE_UNIVERSE,
365 IPPROTO_IPIP, 0,
366 dst, tiph->saddr, 0, 0);
367 rt = ip_route_output_key(dev_net(dev), &fl4);
368 if (IS_ERR(rt)) {
369 dev->stats.tx_carrier_errors++;
370 goto tx_error_icmp;
371 }
372 /* if there is no transform then this tunnel is not functional.
373 * Or if the xfrm is not mode tunnel.
374 */
375 if (!rt->dst.xfrm ||
376 rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
377 dev->stats.tx_carrier_errors++;
378 goto tx_error_icmp;
379 }
380 tdev = rt->dst.dev;
381
382 if (tdev == dev) {
383 ip_rt_put(rt);
384 dev->stats.collisions++;
385 goto tx_error;
386 }
387
388 if (tunnel->err_count > 0) {
389 if (time_before(jiffies,
390 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
391 tunnel->err_count--;
392 dst_link_failure(skb);
393 } else
394 tunnel->err_count = 0;
395 }
396
397 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
398 IPSKB_REROUTED);
399 skb_dst_drop(skb);
400 skb_dst_set(skb, &rt->dst);
401 nf_reset(skb);
402 skb->dev = skb_dst(skb)->dev;
403
404 tstats = this_cpu_ptr(dev->tstats);
405 VTI_XMIT(tstats, &dev->stats);
406 return NETDEV_TX_OK;
407
408tx_error_icmp:
409 dst_link_failure(skb);
410tx_error:
411 dev->stats.tx_errors++;
412 dev_kfree_skb(skb);
413 return NETDEV_TX_OK;
414}
415
416static int vti_tunnel_bind_dev(struct net_device *dev)
417{
418 struct net_device *tdev = NULL;
419 struct ip_tunnel *tunnel;
420 struct iphdr *iph;
421
422 tunnel = netdev_priv(dev);
423 iph = &tunnel->parms.iph;
424
425 if (iph->daddr) {
426 struct rtable *rt;
427 struct flowi4 fl4;
428 memset(&fl4, 0, sizeof(fl4));
429 flowi4_init_output(&fl4, tunnel->parms.link,
stephen hemminger8437e762012-10-11 12:51:28 +0000430 be32_to_cpu(tunnel->parms.i_key),
Saurabh11814122012-07-17 09:44:54 +0000431 RT_TOS(iph->tos), RT_SCOPE_UNIVERSE,
432 IPPROTO_IPIP, 0,
433 iph->daddr, iph->saddr, 0, 0);
434 rt = ip_route_output_key(dev_net(dev), &fl4);
435 if (!IS_ERR(rt)) {
436 tdev = rt->dst.dev;
437 ip_rt_put(rt);
438 }
439 dev->flags |= IFF_POINTOPOINT;
440 }
441
442 if (!tdev && tunnel->parms.link)
443 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
444
445 if (tdev) {
446 dev->hard_header_len = tdev->hard_header_len +
447 sizeof(struct iphdr);
448 dev->mtu = tdev->mtu;
449 }
450 dev->iflink = tunnel->parms.link;
451 return dev->mtu;
452}
453
454static int
455vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
456{
457 int err = 0;
458 struct ip_tunnel_parm p;
459 struct ip_tunnel *t;
460 struct net *net = dev_net(dev);
461 struct vti_net *ipn = net_generic(net, vti_net_id);
462
463 switch (cmd) {
464 case SIOCGETTUNNEL:
465 t = NULL;
466 if (dev == ipn->fb_tunnel_dev) {
467 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
468 sizeof(p))) {
469 err = -EFAULT;
470 break;
471 }
472 t = vti_tunnel_locate(net, &p, 0);
473 }
474 if (t == NULL)
475 t = netdev_priv(dev);
476 memcpy(&p, &t->parms, sizeof(p));
477 p.i_flags |= GRE_KEY | VTI_ISVTI;
478 p.o_flags |= GRE_KEY;
479 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
480 err = -EFAULT;
481 break;
482
483 case SIOCADDTUNNEL:
484 case SIOCCHGTUNNEL:
485 err = -EPERM;
486 if (!capable(CAP_NET_ADMIN))
487 goto done;
488
489 err = -EFAULT;
490 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
491 goto done;
492
493 err = -EINVAL;
494 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
495 p.iph.ihl != 5)
496 goto done;
497
498 t = vti_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
499
500 if (dev != ipn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
501 if (t != NULL) {
502 if (t->dev != dev) {
503 err = -EEXIST;
504 break;
505 }
506 } else {
507 if (((dev->flags&IFF_POINTOPOINT) &&
508 !p.iph.daddr) ||
509 (!(dev->flags&IFF_POINTOPOINT) &&
510 p.iph.daddr)) {
511 err = -EINVAL;
512 break;
513 }
514 t = netdev_priv(dev);
515 vti_tunnel_unlink(ipn, t);
516 synchronize_net();
517 t->parms.iph.saddr = p.iph.saddr;
518 t->parms.iph.daddr = p.iph.daddr;
519 t->parms.i_key = p.i_key;
520 t->parms.o_key = p.o_key;
521 t->parms.iph.protocol = IPPROTO_IPIP;
522 memcpy(dev->dev_addr, &p.iph.saddr, 4);
523 memcpy(dev->broadcast, &p.iph.daddr, 4);
524 vti_tunnel_link(ipn, t);
525 netdev_state_change(dev);
526 }
527 }
528
529 if (t) {
530 err = 0;
531 if (cmd == SIOCCHGTUNNEL) {
532 t->parms.i_key = p.i_key;
533 t->parms.o_key = p.o_key;
534 if (t->parms.link != p.link) {
535 t->parms.link = p.link;
536 vti_tunnel_bind_dev(dev);
537 netdev_state_change(dev);
538 }
539 }
540 p.i_flags |= GRE_KEY | VTI_ISVTI;
541 p.o_flags |= GRE_KEY;
542 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms,
543 sizeof(p)))
544 err = -EFAULT;
545 } else
546 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
547 break;
548
549 case SIOCDELTUNNEL:
550 err = -EPERM;
551 if (!capable(CAP_NET_ADMIN))
552 goto done;
553
554 if (dev == ipn->fb_tunnel_dev) {
555 err = -EFAULT;
556 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data,
557 sizeof(p)))
558 goto done;
559 err = -ENOENT;
560
561 t = vti_tunnel_locate(net, &p, 0);
562 if (t == NULL)
563 goto done;
564 err = -EPERM;
565 if (t->dev == ipn->fb_tunnel_dev)
566 goto done;
567 dev = t->dev;
568 }
569 unregister_netdevice(dev);
570 err = 0;
571 break;
572
573 default:
574 err = -EINVAL;
575 }
576
577done:
578 return err;
579}
580
581static int vti_tunnel_change_mtu(struct net_device *dev, int new_mtu)
582{
583 if (new_mtu < 68 || new_mtu > 0xFFF8)
584 return -EINVAL;
585 dev->mtu = new_mtu;
586 return 0;
587}
588
589static const struct net_device_ops vti_netdev_ops = {
590 .ndo_init = vti_tunnel_init,
591 .ndo_uninit = vti_tunnel_uninit,
592 .ndo_start_xmit = vti_tunnel_xmit,
593 .ndo_do_ioctl = vti_tunnel_ioctl,
594 .ndo_change_mtu = vti_tunnel_change_mtu,
595 .ndo_get_stats64 = vti_get_stats64,
596};
597
598static void vti_dev_free(struct net_device *dev)
599{
600 free_percpu(dev->tstats);
601 free_netdev(dev);
602}
603
604static void vti_tunnel_setup(struct net_device *dev)
605{
606 dev->netdev_ops = &vti_netdev_ops;
607 dev->destructor = vti_dev_free;
608
609 dev->type = ARPHRD_TUNNEL;
610 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
611 dev->mtu = ETH_DATA_LEN;
612 dev->flags = IFF_NOARP;
613 dev->iflink = 0;
614 dev->addr_len = 4;
615 dev->features |= NETIF_F_NETNS_LOCAL;
616 dev->features |= NETIF_F_LLTX;
617 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
618}
619
620static int vti_tunnel_init(struct net_device *dev)
621{
622 struct ip_tunnel *tunnel = netdev_priv(dev);
623
624 tunnel->dev = dev;
625 strcpy(tunnel->parms.name, dev->name);
626
627 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
628 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
629
630 dev->tstats = alloc_percpu(struct pcpu_tstats);
631 if (!dev->tstats)
632 return -ENOMEM;
633
634 return 0;
635}
636
637static int __net_init vti_fb_tunnel_init(struct net_device *dev)
638{
639 struct ip_tunnel *tunnel = netdev_priv(dev);
640 struct iphdr *iph = &tunnel->parms.iph;
641 struct vti_net *ipn = net_generic(dev_net(dev), vti_net_id);
642
643 tunnel->dev = dev;
644 strcpy(tunnel->parms.name, dev->name);
645
646 iph->version = 4;
647 iph->protocol = IPPROTO_IPIP;
648 iph->ihl = 5;
649
650 dev->tstats = alloc_percpu(struct pcpu_tstats);
651 if (!dev->tstats)
652 return -ENOMEM;
653
654 dev_hold(dev);
655 rcu_assign_pointer(ipn->tunnels_wc[0], tunnel);
656 return 0;
657}
658
659static struct xfrm_tunnel vti_handler __read_mostly = {
660 .handler = vti_rcv,
661 .err_handler = vti_err,
662 .priority = 1,
663};
664
665static void vti_destroy_tunnels(struct vti_net *ipn, struct list_head *head)
666{
667 int prio;
668
669 for (prio = 1; prio < 4; prio++) {
670 int h;
671 for (h = 0; h < HASH_SIZE; h++) {
672 struct ip_tunnel *t;
673
674 t = rtnl_dereference(ipn->tunnels[prio][h]);
675 while (t != NULL) {
676 unregister_netdevice_queue(t->dev, head);
677 t = rtnl_dereference(t->next);
678 }
679 }
680 }
681}
682
683static int __net_init vti_init_net(struct net *net)
684{
685 int err;
686 struct vti_net *ipn = net_generic(net, vti_net_id);
687
688 ipn->tunnels[0] = ipn->tunnels_wc;
689 ipn->tunnels[1] = ipn->tunnels_l;
690 ipn->tunnels[2] = ipn->tunnels_r;
691 ipn->tunnels[3] = ipn->tunnels_r_l;
692
693 ipn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel),
694 "ip_vti0",
695 vti_tunnel_setup);
696 if (!ipn->fb_tunnel_dev) {
697 err = -ENOMEM;
698 goto err_alloc_dev;
699 }
700 dev_net_set(ipn->fb_tunnel_dev, net);
701
702 err = vti_fb_tunnel_init(ipn->fb_tunnel_dev);
703 if (err)
704 goto err_reg_dev;
705 ipn->fb_tunnel_dev->rtnl_link_ops = &vti_link_ops;
706
707 err = register_netdev(ipn->fb_tunnel_dev);
708 if (err)
709 goto err_reg_dev;
710 return 0;
711
712err_reg_dev:
713 vti_dev_free(ipn->fb_tunnel_dev);
714err_alloc_dev:
715 /* nothing */
716 return err;
717}
718
719static void __net_exit vti_exit_net(struct net *net)
720{
721 struct vti_net *ipn = net_generic(net, vti_net_id);
722 LIST_HEAD(list);
723
724 rtnl_lock();
725 vti_destroy_tunnels(ipn, &list);
726 unregister_netdevice_many(&list);
727 rtnl_unlock();
728}
729
730static struct pernet_operations vti_net_ops = {
731 .init = vti_init_net,
732 .exit = vti_exit_net,
733 .id = &vti_net_id,
734 .size = sizeof(struct vti_net),
735};
736
737static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
738{
739 return 0;
740}
741
742static void vti_netlink_parms(struct nlattr *data[],
743 struct ip_tunnel_parm *parms)
744{
745 memset(parms, 0, sizeof(*parms));
746
747 parms->iph.protocol = IPPROTO_IPIP;
748
749 if (!data)
750 return;
751
752 if (data[IFLA_VTI_LINK])
753 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
754
755 if (data[IFLA_VTI_IKEY])
756 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
757
758 if (data[IFLA_VTI_OKEY])
759 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
760
761 if (data[IFLA_VTI_LOCAL])
762 parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]);
763
764 if (data[IFLA_VTI_REMOTE])
765 parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]);
766
767}
768
769static int vti_newlink(struct net *src_net, struct net_device *dev,
770 struct nlattr *tb[], struct nlattr *data[])
771{
772 struct ip_tunnel *nt;
773 struct net *net = dev_net(dev);
774 struct vti_net *ipn = net_generic(net, vti_net_id);
775 int mtu;
776 int err;
777
778 nt = netdev_priv(dev);
779 vti_netlink_parms(data, &nt->parms);
780
781 if (vti_tunnel_locate(net, &nt->parms, 0))
782 return -EEXIST;
783
784 mtu = vti_tunnel_bind_dev(dev);
785 if (!tb[IFLA_MTU])
786 dev->mtu = mtu;
787
788 err = register_netdevice(dev);
789 if (err)
790 goto out;
791
792 dev_hold(dev);
793 vti_tunnel_link(ipn, nt);
794
795out:
796 return err;
797}
798
799static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
800 struct nlattr *data[])
801{
802 struct ip_tunnel *t, *nt;
803 struct net *net = dev_net(dev);
804 struct vti_net *ipn = net_generic(net, vti_net_id);
805 struct ip_tunnel_parm p;
806 int mtu;
807
808 if (dev == ipn->fb_tunnel_dev)
809 return -EINVAL;
810
811 nt = netdev_priv(dev);
812 vti_netlink_parms(data, &p);
813
814 t = vti_tunnel_locate(net, &p, 0);
815
816 if (t) {
817 if (t->dev != dev)
818 return -EEXIST;
819 } else {
820 t = nt;
821
822 vti_tunnel_unlink(ipn, t);
823 t->parms.iph.saddr = p.iph.saddr;
824 t->parms.iph.daddr = p.iph.daddr;
825 t->parms.i_key = p.i_key;
826 t->parms.o_key = p.o_key;
827 if (dev->type != ARPHRD_ETHER) {
828 memcpy(dev->dev_addr, &p.iph.saddr, 4);
829 memcpy(dev->broadcast, &p.iph.daddr, 4);
830 }
831 vti_tunnel_link(ipn, t);
832 netdev_state_change(dev);
833 }
834
835 if (t->parms.link != p.link) {
836 t->parms.link = p.link;
837 mtu = vti_tunnel_bind_dev(dev);
838 if (!tb[IFLA_MTU])
839 dev->mtu = mtu;
840 netdev_state_change(dev);
841 }
842
843 return 0;
844}
845
846static size_t vti_get_size(const struct net_device *dev)
847{
848 return
849 /* IFLA_VTI_LINK */
850 nla_total_size(4) +
851 /* IFLA_VTI_IKEY */
852 nla_total_size(4) +
853 /* IFLA_VTI_OKEY */
854 nla_total_size(4) +
855 /* IFLA_VTI_LOCAL */
856 nla_total_size(4) +
857 /* IFLA_VTI_REMOTE */
858 nla_total_size(4) +
859 0;
860}
861
862static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
863{
864 struct ip_tunnel *t = netdev_priv(dev);
865 struct ip_tunnel_parm *p = &t->parms;
866
867 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
868 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
869 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
870 nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr);
871 nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr);
872
873 return 0;
874}
875
876static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
877 [IFLA_VTI_LINK] = { .type = NLA_U32 },
878 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
879 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
880 [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
881 [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
882};
883
884static struct rtnl_link_ops vti_link_ops __read_mostly = {
885 .kind = "vti",
886 .maxtype = IFLA_VTI_MAX,
887 .policy = vti_policy,
888 .priv_size = sizeof(struct ip_tunnel),
889 .setup = vti_tunnel_setup,
890 .validate = vti_tunnel_validate,
891 .newlink = vti_newlink,
892 .changelink = vti_changelink,
893 .get_size = vti_get_size,
894 .fill_info = vti_fill_info,
895};
896
897static int __init vti_init(void)
898{
899 int err;
900
901 pr_info("IPv4 over IPSec tunneling driver\n");
902
903 err = register_pernet_device(&vti_net_ops);
904 if (err < 0)
905 return err;
906 err = xfrm4_mode_tunnel_input_register(&vti_handler);
907 if (err < 0) {
908 unregister_pernet_device(&vti_net_ops);
909 pr_info(KERN_INFO "vti init: can't register tunnel\n");
910 }
911
912 err = rtnl_link_register(&vti_link_ops);
913 if (err < 0)
914 goto rtnl_link_failed;
915
916 return err;
917
918rtnl_link_failed:
919 xfrm4_mode_tunnel_input_deregister(&vti_handler);
920 unregister_pernet_device(&vti_net_ops);
921 return err;
922}
923
924static void __exit vti_fini(void)
925{
926 rtnl_link_unregister(&vti_link_ops);
927 if (xfrm4_mode_tunnel_input_deregister(&vti_handler))
928 pr_info("vti close: can't deregister tunnel\n");
929
930 unregister_pernet_device(&vti_net_ops);
931}
932
933module_init(vti_init);
934module_exit(vti_fini);
935MODULE_LICENSE("GPL");
936MODULE_ALIAS_RTNL_LINK("vti");
937MODULE_ALIAS_NETDEV("ip_vti0");