1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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/init.h>
34#include <linux/netfilter_ipv4.h>
35#include <linux/if_ether.h>
36#include <linux/icmpv6.h>
37
38#include <net/sock.h>
39#include <net/ip.h>
40#include <net/icmp.h>
41#include <net/ip_tunnels.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
47static struct rtnl_link_ops vti_link_ops __read_mostly;
48
49static int vti_net_id __read_mostly;
50static int vti_tunnel_init(struct net_device *dev);
51
52static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
53 int encap_type)
54{
55 struct ip_tunnel *tunnel;
56 const struct iphdr *iph = ip_hdr(skb);
57 struct net *net = dev_net(skb->dev);
58 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
59
60 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
61 iph->saddr, iph->daddr, 0);
62 if (tunnel != NULL) {
63 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
64 goto drop;
65
66 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
67
68 return xfrm_input(skb, nexthdr, spi, encap_type);
69 }
70
71 return -EINVAL;
72drop:
73 kfree_skb(skb);
74 return 0;
75}
76
77static int vti_rcv(struct sk_buff *skb)
78{
79 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
80 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
81
82 return vti_input(skb, ip_hdr(skb)->protocol, 0, 0);
83}
84
85static int vti_rcv_cb(struct sk_buff *skb, int err)
86{
87 unsigned short family;
88 struct net_device *dev;
89 struct pcpu_sw_netstats *tstats;
90 struct xfrm_state *x;
91 struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
92 u32 orig_mark = skb->mark;
93 int ret;
94
95 if (!tunnel)
96 return 1;
97
98 dev = tunnel->dev;
99
100 if (err) {
101 dev->stats.rx_errors++;
102 dev->stats.rx_dropped++;
103
104 return 0;
105 }
106
107 x = xfrm_input_state(skb);
108 family = x->inner_mode->afinfo->family;
109
110 skb->mark = be32_to_cpu(tunnel->parms.i_key);
111 ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
112 skb->mark = orig_mark;
113
114 if (!ret)
115 return -EPERM;
116
117 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
118 skb->dev = dev;
119
120 tstats = this_cpu_ptr(dev->tstats);
121
122 u64_stats_update_begin(&tstats->syncp);
123 tstats->rx_packets++;
124 tstats->rx_bytes += skb->len;
125 u64_stats_update_end(&tstats->syncp);
126
127 return 0;
128}
129
130static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
131{
132 xfrm_address_t *daddr = (xfrm_address_t *)&dst;
133 xfrm_address_t *saddr = (xfrm_address_t *)&src;
134
135
136
137
138 if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
139 x->props.family != AF_INET)
140 return false;
141
142 if (!dst)
143 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
144
145 if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
146 return false;
147
148 return true;
149}
150
151static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
152 struct flowi *fl)
153{
154 struct ip_tunnel *tunnel = netdev_priv(dev);
155 struct ip_tunnel_parm *parms = &tunnel->parms;
156 struct dst_entry *dst = skb_dst(skb);
157 struct net_device *tdev;
158 int pkt_len = skb->len;
159 int err;
160 int mtu;
161
162 if (!dst) {
163 dev->stats.tx_carrier_errors++;
164 goto tx_error_icmp;
165 }
166
167 dst_hold(dst);
168 dst = xfrm_lookup(tunnel->net, dst, fl, NULL, 0);
169 if (IS_ERR(dst)) {
170 dev->stats.tx_carrier_errors++;
171 goto tx_error_icmp;
172 }
173
174 if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
175 dev->stats.tx_carrier_errors++;
176 dst_release(dst);
177 goto tx_error_icmp;
178 }
179
180 tdev = dst->dev;
181
182 if (tdev == dev) {
183 dst_release(dst);
184 dev->stats.collisions++;
185 goto tx_error;
186 }
187
188 if (tunnel->err_count > 0) {
189 if (time_before(jiffies,
190 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
191 tunnel->err_count--;
192 dst_link_failure(skb);
193 } else
194 tunnel->err_count = 0;
195 }
196
197 mtu = dst_mtu(dst);
198 if (skb->len > mtu) {
199 skb_dst_update_pmtu(skb, mtu);
200 if (skb->protocol == htons(ETH_P_IP)) {
201 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
202 htonl(mtu));
203 } else {
204 if (mtu < IPV6_MIN_MTU)
205 mtu = IPV6_MIN_MTU;
206
207 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
208 }
209
210 dst_release(dst);
211 goto tx_error;
212 }
213
214 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
215 skb_dst_set(skb, dst);
216 skb->dev = skb_dst(skb)->dev;
217
218 err = dst_output(skb);
219 if (net_xmit_eval(err) == 0)
220 err = pkt_len;
221 iptunnel_xmit_stats(dev, err);
222 return NETDEV_TX_OK;
223
224tx_error_icmp:
225 dst_link_failure(skb);
226tx_error:
227 dev->stats.tx_errors++;
228 kfree_skb(skb);
229 return NETDEV_TX_OK;
230}
231
232
233
234
235static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
236{
237 struct ip_tunnel *tunnel = netdev_priv(dev);
238 struct flowi fl;
239
240 memset(&fl, 0, sizeof(fl));
241
242 switch (skb->protocol) {
243 case htons(ETH_P_IP):
244 xfrm_decode_session(skb, &fl, AF_INET);
245 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
246 break;
247 case htons(ETH_P_IPV6):
248 xfrm_decode_session(skb, &fl, AF_INET6);
249 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
250 break;
251 default:
252 dev->stats.tx_errors++;
253 dev_kfree_skb(skb);
254 return NETDEV_TX_OK;
255 }
256
257
258 fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
259
260 return vti_xmit(skb, dev, &fl);
261}
262
263static int vti4_err(struct sk_buff *skb, u32 info)
264{
265 __be32 spi;
266 __u32 mark;
267 struct xfrm_state *x;
268 struct ip_tunnel *tunnel;
269 struct ip_esp_hdr *esph;
270 struct ip_auth_hdr *ah ;
271 struct ip_comp_hdr *ipch;
272 struct net *net = dev_net(skb->dev);
273 const struct iphdr *iph = (const struct iphdr *)skb->data;
274 int protocol = iph->protocol;
275 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
276
277 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
278 iph->daddr, iph->saddr, 0);
279 if (!tunnel)
280 return -1;
281
282 mark = be32_to_cpu(tunnel->parms.o_key);
283
284 switch (protocol) {
285 case IPPROTO_ESP:
286 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
287 spi = esph->spi;
288 break;
289 case IPPROTO_AH:
290 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
291 spi = ah->spi;
292 break;
293 case IPPROTO_COMP:
294 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
295 spi = htonl(ntohs(ipch->cpi));
296 break;
297 default:
298 return 0;
299 }
300
301 switch (icmp_hdr(skb)->type) {
302 case ICMP_DEST_UNREACH:
303 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
304 return 0;
305 case ICMP_REDIRECT:
306 break;
307 default:
308 return 0;
309 }
310
311 x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
312 spi, protocol, AF_INET);
313 if (!x)
314 return 0;
315
316 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
317 ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
318 else
319 ipv4_redirect(skb, net, 0, 0, protocol, 0);
320 xfrm_state_put(x);
321
322 return 0;
323}
324
325static int
326vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
327{
328 int err = 0;
329 struct ip_tunnel_parm p;
330
331 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
332 return -EFAULT;
333
334 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
335 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
336 p.iph.ihl != 5)
337 return -EINVAL;
338 }
339
340 if (!(p.i_flags & GRE_KEY))
341 p.i_key = 0;
342 if (!(p.o_flags & GRE_KEY))
343 p.o_key = 0;
344
345 p.i_flags = VTI_ISVTI;
346
347 err = ip_tunnel_ioctl(dev, &p, cmd);
348 if (err)
349 return err;
350
351 if (cmd != SIOCDELTUNNEL) {
352 p.i_flags |= GRE_KEY;
353 p.o_flags |= GRE_KEY;
354 }
355
356 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
357 return -EFAULT;
358 return 0;
359}
360
361static const struct net_device_ops vti_netdev_ops = {
362 .ndo_init = vti_tunnel_init,
363 .ndo_uninit = ip_tunnel_uninit,
364 .ndo_start_xmit = vti_tunnel_xmit,
365 .ndo_do_ioctl = vti_tunnel_ioctl,
366 .ndo_change_mtu_rh74 = ip_tunnel_change_mtu,
367 .ndo_get_stats64 = ip_tunnel_get_stats64,
368 .ndo_get_iflink = ip_tunnel_get_iflink,
369};
370
371static void vti_tunnel_setup(struct net_device *dev)
372{
373 dev->netdev_ops = &vti_netdev_ops;
374 dev->type = ARPHRD_TUNNEL;
375 ip_tunnel_setup(dev, vti_net_id);
376}
377
378static int vti_tunnel_init(struct net_device *dev)
379{
380 struct ip_tunnel *tunnel = netdev_priv(dev);
381 struct iphdr *iph = &tunnel->parms.iph;
382
383 memcpy(dev->dev_addr, &iph->saddr, 4);
384 memcpy(dev->broadcast, &iph->daddr, 4);
385
386 dev->flags = IFF_NOARP;
387 dev->addr_len = 4;
388 dev->features |= NETIF_F_LLTX;
389 netif_keep_dst(dev);
390
391 return ip_tunnel_init(dev);
392}
393
394static void __net_init vti_fb_tunnel_init(struct net_device *dev)
395{
396 struct ip_tunnel *tunnel = netdev_priv(dev);
397 struct iphdr *iph = &tunnel->parms.iph;
398
399 iph->version = 4;
400 iph->protocol = IPPROTO_IPIP;
401 iph->ihl = 5;
402}
403
404static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
405 .handler = vti_rcv,
406 .input_handler = vti_input,
407 .cb_handler = vti_rcv_cb,
408 .err_handler = vti4_err,
409 .priority = 100,
410};
411
412static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
413 .handler = vti_rcv,
414 .input_handler = vti_input,
415 .cb_handler = vti_rcv_cb,
416 .err_handler = vti4_err,
417 .priority = 100,
418};
419
420static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
421 .handler = vti_rcv,
422 .input_handler = vti_input,
423 .cb_handler = vti_rcv_cb,
424 .err_handler = vti4_err,
425 .priority = 100,
426};
427
428static int __net_init vti_init_net(struct net *net)
429{
430 int err;
431 struct ip_tunnel_net *itn;
432
433 err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
434 if (err)
435 return err;
436 itn = net_generic(net, vti_net_id);
437 vti_fb_tunnel_init(itn->fb_tunnel_dev);
438 return 0;
439}
440
441static void __net_exit vti_exit_net(struct net *net)
442{
443 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
444 ip_tunnel_delete_net(itn, &vti_link_ops);
445}
446
447static struct pernet_operations vti_net_ops = {
448 .init = vti_init_net,
449 .exit = vti_exit_net,
450 .id = &vti_net_id,
451 .size = sizeof(struct ip_tunnel_net),
452};
453
454static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
455{
456 return 0;
457}
458
459static void vti_netlink_parms(struct nlattr *data[],
460 struct ip_tunnel_parm *parms)
461{
462 memset(parms, 0, sizeof(*parms));
463
464 parms->iph.protocol = IPPROTO_IPIP;
465
466 if (!data)
467 return;
468
469 parms->i_flags = VTI_ISVTI;
470
471 if (data[IFLA_VTI_LINK])
472 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
473
474 if (data[IFLA_VTI_IKEY])
475 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
476
477 if (data[IFLA_VTI_OKEY])
478 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
479
480 if (data[IFLA_VTI_LOCAL])
481 parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
482
483 if (data[IFLA_VTI_REMOTE])
484 parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
485
486}
487
488static int vti_newlink(struct net *src_net, struct net_device *dev,
489 struct nlattr *tb[], struct nlattr *data[])
490{
491 struct ip_tunnel_parm parms;
492
493 vti_netlink_parms(data, &parms);
494 return ip_tunnel_newlink(dev, tb, &parms);
495}
496
497static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
498 struct nlattr *data[])
499{
500 struct ip_tunnel_parm p;
501
502 vti_netlink_parms(data, &p);
503 return ip_tunnel_changelink(dev, tb, &p);
504}
505
506static size_t vti_get_size(const struct net_device *dev)
507{
508 return
509
510 nla_total_size(4) +
511
512 nla_total_size(4) +
513
514 nla_total_size(4) +
515
516 nla_total_size(4) +
517
518 nla_total_size(4) +
519 0;
520}
521
522static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
523{
524 struct ip_tunnel *t = netdev_priv(dev);
525 struct ip_tunnel_parm *p = &t->parms;
526
527 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
528 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
529 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
530 nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr);
531 nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr);
532
533 return 0;
534}
535
536static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
537 [IFLA_VTI_LINK] = { .type = NLA_U32 },
538 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
539 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
540 [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
541 [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
542};
543
544static struct rtnl_link_ops vti_link_ops __read_mostly = {
545 .kind = "vti",
546 .maxtype = IFLA_VTI_MAX,
547 .policy = vti_policy,
548 .priv_size = sizeof(struct ip_tunnel),
549 .setup = vti_tunnel_setup,
550 .validate = vti_tunnel_validate,
551 .newlink = vti_newlink,
552 .changelink = vti_changelink,
553 .dellink = ip_tunnel_dellink,
554 .get_size = vti_get_size,
555 .fill_info = vti_fill_info,
556 .get_link_net = ip_tunnel_get_link_net,
557};
558
559static bool is_vti_tunnel(const struct net_device *dev)
560{
561 return dev->netdev_ops == &vti_netdev_ops;
562}
563
564static int vti_device_event(struct notifier_block *unused,
565 unsigned long event, void *ptr)
566{
567 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
568 struct ip_tunnel *tunnel = netdev_priv(dev);
569
570 if (!is_vti_tunnel(dev))
571 return NOTIFY_DONE;
572
573 switch (event) {
574 case NETDEV_DOWN:
575 if (!net_eq(tunnel->net, dev_net(dev)))
576 xfrm_garbage_collect(tunnel->net);
577 break;
578 }
579 return NOTIFY_DONE;
580}
581
582static struct notifier_block vti_notifier_block __read_mostly = {
583 .notifier_call = vti_device_event,
584};
585
586static int __init vti_init(void)
587{
588 const char *msg;
589 int err;
590
591 pr_info("IPv4 over IPsec tunneling driver\n");
592
593 register_netdevice_notifier_rh(&vti_notifier_block);
594
595 msg = "tunnel device";
596 err = register_pernet_device(&vti_net_ops);
597 if (err < 0)
598 goto pernet_dev_failed;
599
600 msg = "tunnel protocols";
601 err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
602 if (err < 0)
603 goto xfrm_proto_esp_failed;
604 err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
605 if (err < 0)
606 goto xfrm_proto_ah_failed;
607 err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
608 if (err < 0)
609 goto xfrm_proto_comp_failed;
610
611 msg = "netlink interface";
612 err = rtnl_link_register(&vti_link_ops);
613 if (err < 0)
614 goto rtnl_link_failed;
615
616 return err;
617
618rtnl_link_failed:
619 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
620xfrm_proto_comp_failed:
621 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
622xfrm_proto_ah_failed:
623 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
624xfrm_proto_esp_failed:
625 unregister_pernet_device(&vti_net_ops);
626pernet_dev_failed:
627 unregister_netdevice_notifier_rh(&vti_notifier_block);
628 pr_err("vti init: failed to register %s\n", msg);
629 return err;
630}
631
632static void __exit vti_fini(void)
633{
634 rtnl_link_unregister(&vti_link_ops);
635 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
636 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
637 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
638 unregister_pernet_device(&vti_net_ops);
639 unregister_netdevice_notifier_rh(&vti_notifier_block);
640}
641
642module_init(vti_init);
643module_exit(vti_fini);
644MODULE_LICENSE("GPL");
645MODULE_ALIAS_RTNL_LINK("vti");
646MODULE_ALIAS_NETDEV("ip_vti0");
647