2 * Linux NET3: IP/IP protocol decoder modified to support
3 * virtual tunnel interface
6 * Saurabh Mohan (saurabh.mohan@vyatta.com) 05/07/2012
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.
16 This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
18 For comments look at net/ipv4/ip_gre.c --ANK
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>
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 #include <linux/icmpv6.h>
42 #include <net/ip_tunnels.h>
43 #include <net/inet_ecn.h>
45 #include <net/net_namespace.h>
46 #include <net/netns/generic.h>
48 static struct rtnl_link_ops vti_link_ops __read_mostly;
50 static int vti_net_id __read_mostly;
51 static int vti_tunnel_init(struct net_device *dev);
53 static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
54 int encap_type, bool update_skb_dev)
56 struct ip_tunnel *tunnel;
57 const struct iphdr *iph = ip_hdr(skb);
58 struct net *net = dev_net(skb->dev);
59 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
61 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
62 iph->saddr, iph->daddr, 0);
64 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
67 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
70 skb->dev = tunnel->dev;
72 return xfrm_input(skb, nexthdr, spi, encap_type);
81 static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
84 return vti_input(skb, nexthdr, spi, encap_type, false);
87 static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
89 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
90 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
92 return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
95 static int vti_rcv_proto(struct sk_buff *skb)
97 return vti_rcv(skb, 0, false);
100 static int vti_rcv_tunnel(struct sk_buff *skb)
102 struct ip_tunnel_net *itn = net_generic(dev_net(skb->dev), vti_net_id);
103 const struct iphdr *iph = ip_hdr(skb);
104 struct ip_tunnel *tunnel;
106 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
107 iph->saddr, iph->daddr, 0);
109 struct tnl_ptk_info tpi = {
110 .proto = htons(ETH_P_IP),
113 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
115 if (iptunnel_pull_header(skb, 0, tpi.proto))
117 return ip_tunnel_rcv(tunnel, skb, &tpi, NULL, false);
126 static int vti_rcv_cb(struct sk_buff *skb, int err)
128 unsigned short family;
129 struct net_device *dev;
130 struct pcpu_sw_netstats *tstats;
131 struct xfrm_state *x;
132 struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
133 u32 orig_mark = skb->mark;
142 dev->stats.rx_errors++;
143 dev->stats.rx_dropped++;
148 x = xfrm_input_state(skb);
149 family = x->inner_mode->afinfo->family;
151 skb->mark = be32_to_cpu(tunnel->parms.i_key);
152 ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
153 skb->mark = orig_mark;
158 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
161 tstats = this_cpu_ptr(dev->tstats);
163 u64_stats_update_begin(&tstats->syncp);
164 tstats->rx_packets++;
165 tstats->rx_bytes += skb->len;
166 u64_stats_update_end(&tstats->syncp);
171 static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
173 xfrm_address_t *daddr = (xfrm_address_t *)&dst;
174 xfrm_address_t *saddr = (xfrm_address_t *)&src;
176 /* if there is no transform then this tunnel is not functional.
177 * Or if the xfrm is not mode tunnel.
179 if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
180 x->props.family != AF_INET)
184 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
186 if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
192 static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
195 struct ip_tunnel *tunnel = netdev_priv(dev);
196 struct ip_tunnel_parm *parms = &tunnel->parms;
197 struct dst_entry *dst = skb_dst(skb);
198 struct net_device *tdev; /* Device to other host */
199 int pkt_len = skb->len;
203 switch (skb->protocol) {
204 case htons(ETH_P_IP): {
207 fl->u.ip4.flowi4_oif = dev->ifindex;
208 fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
209 rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
211 dev->stats.tx_carrier_errors++;
215 skb_dst_set(skb, dst);
218 #if IS_ENABLED(CONFIG_IPV6)
219 case htons(ETH_P_IPV6):
220 fl->u.ip6.flowi6_oif = dev->ifindex;
221 fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
222 dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
226 dev->stats.tx_carrier_errors++;
229 skb_dst_set(skb, dst);
233 dev->stats.tx_carrier_errors++;
239 dst = xfrm_lookup(tunnel->net, dst, fl, NULL, 0);
241 dev->stats.tx_carrier_errors++;
245 if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
246 dev->stats.tx_carrier_errors++;
255 dev->stats.collisions++;
259 if (tunnel->err_count > 0) {
260 if (time_before(jiffies,
261 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
263 dst_link_failure(skb);
265 tunnel->err_count = 0;
268 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
269 skb_dst_set(skb, dst);
270 skb->dev = skb_dst(skb)->dev;
272 err = dst_output(tunnel->net, skb->sk, skb);
273 if (net_xmit_eval(err) == 0)
275 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
279 dst_link_failure(skb);
281 dev->stats.tx_errors++;
286 /* This function assumes it is being called from dev_queue_xmit()
287 * and that skb is filled properly by that function.
289 static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
291 struct ip_tunnel *tunnel = netdev_priv(dev);
294 memset(&fl, 0, sizeof(fl));
296 switch (skb->protocol) {
297 case htons(ETH_P_IP):
298 xfrm_decode_session(skb, &fl, AF_INET);
299 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
301 case htons(ETH_P_IPV6):
302 xfrm_decode_session(skb, &fl, AF_INET6);
303 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
306 dev->stats.tx_errors++;
311 /* override mark with tunnel output key */
312 fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
314 return vti_xmit(skb, dev, &fl);
317 static int vti4_err(struct sk_buff *skb, u32 info)
321 struct xfrm_state *x;
322 struct ip_tunnel *tunnel;
323 struct ip_esp_hdr *esph;
324 struct ip_auth_hdr *ah ;
325 struct ip_comp_hdr *ipch;
326 struct net *net = dev_net(skb->dev);
327 const struct iphdr *iph = (const struct iphdr *)skb->data;
328 int protocol = iph->protocol;
329 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
331 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
332 iph->daddr, iph->saddr, 0);
336 mark = be32_to_cpu(tunnel->parms.o_key);
340 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
344 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
348 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
349 spi = htonl(ntohs(ipch->cpi));
355 switch (icmp_hdr(skb)->type) {
356 case ICMP_DEST_UNREACH:
357 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
365 x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
366 spi, protocol, AF_INET);
370 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
371 ipv4_update_pmtu(skb, net, info, 0, 0, protocol, 0);
373 ipv4_redirect(skb, net, 0, 0, protocol, 0);
380 vti_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
383 struct ip_tunnel_parm p;
385 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
388 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
389 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPIP ||
394 if (!(p.i_flags & GRE_KEY))
396 if (!(p.o_flags & GRE_KEY))
399 p.i_flags = VTI_ISVTI;
401 err = ip_tunnel_ioctl(dev, &p, cmd);
405 if (cmd != SIOCDELTUNNEL) {
406 p.i_flags |= GRE_KEY;
407 p.o_flags |= GRE_KEY;
410 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
415 static const struct net_device_ops vti_netdev_ops = {
416 .ndo_init = vti_tunnel_init,
417 .ndo_uninit = ip_tunnel_uninit,
418 .ndo_start_xmit = vti_tunnel_xmit,
419 .ndo_do_ioctl = vti_tunnel_ioctl,
420 .ndo_change_mtu = ip_tunnel_change_mtu,
421 .ndo_get_stats64 = ip_tunnel_get_stats64,
422 .ndo_get_iflink = ip_tunnel_get_iflink,
425 static void vti_tunnel_setup(struct net_device *dev)
427 dev->netdev_ops = &vti_netdev_ops;
428 dev->type = ARPHRD_TUNNEL;
429 ip_tunnel_setup(dev, vti_net_id);
432 static int vti_tunnel_init(struct net_device *dev)
434 struct ip_tunnel *tunnel = netdev_priv(dev);
435 struct iphdr *iph = &tunnel->parms.iph;
437 memcpy(dev->dev_addr, &iph->saddr, 4);
438 memcpy(dev->broadcast, &iph->daddr, 4);
440 dev->mtu = ETH_DATA_LEN;
441 dev->flags = IFF_NOARP;
443 dev->features |= NETIF_F_LLTX;
446 return ip_tunnel_init(dev);
449 static void __net_init vti_fb_tunnel_init(struct net_device *dev)
451 struct ip_tunnel *tunnel = netdev_priv(dev);
452 struct iphdr *iph = &tunnel->parms.iph;
455 iph->protocol = IPPROTO_IPIP;
459 static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
460 .handler = vti_rcv_proto,
461 .input_handler = vti_input_proto,
462 .cb_handler = vti_rcv_cb,
463 .err_handler = vti4_err,
467 static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
468 .handler = vti_rcv_proto,
469 .input_handler = vti_input_proto,
470 .cb_handler = vti_rcv_cb,
471 .err_handler = vti4_err,
475 static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
476 .handler = vti_rcv_proto,
477 .input_handler = vti_input_proto,
478 .cb_handler = vti_rcv_cb,
479 .err_handler = vti4_err,
483 static struct xfrm_tunnel ipip_handler __read_mostly = {
484 .handler = vti_rcv_tunnel,
485 .err_handler = vti4_err,
489 static int __net_init vti_init_net(struct net *net)
492 struct ip_tunnel_net *itn;
494 err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
497 itn = net_generic(net, vti_net_id);
498 vti_fb_tunnel_init(itn->fb_tunnel_dev);
502 static void __net_exit vti_exit_net(struct net *net)
504 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
505 ip_tunnel_delete_net(itn, &vti_link_ops);
508 static struct pernet_operations vti_net_ops = {
509 .init = vti_init_net,
510 .exit = vti_exit_net,
512 .size = sizeof(struct ip_tunnel_net),
515 static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
520 static void vti_netlink_parms(struct nlattr *data[],
521 struct ip_tunnel_parm *parms)
523 memset(parms, 0, sizeof(*parms));
525 parms->iph.protocol = IPPROTO_IPIP;
530 parms->i_flags = VTI_ISVTI;
532 if (data[IFLA_VTI_LINK])
533 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
535 if (data[IFLA_VTI_IKEY])
536 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
538 if (data[IFLA_VTI_OKEY])
539 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
541 if (data[IFLA_VTI_LOCAL])
542 parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
544 if (data[IFLA_VTI_REMOTE])
545 parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
549 static int vti_newlink(struct net *src_net, struct net_device *dev,
550 struct nlattr *tb[], struct nlattr *data[])
552 struct ip_tunnel_parm parms;
554 vti_netlink_parms(data, &parms);
555 return ip_tunnel_newlink(dev, tb, &parms);
558 static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
559 struct nlattr *data[])
561 struct ip_tunnel_parm p;
563 vti_netlink_parms(data, &p);
564 return ip_tunnel_changelink(dev, tb, &p);
567 static size_t vti_get_size(const struct net_device *dev)
578 /* IFLA_VTI_REMOTE */
583 static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
585 struct ip_tunnel *t = netdev_priv(dev);
586 struct ip_tunnel_parm *p = &t->parms;
588 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
589 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
590 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
591 nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr);
592 nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr);
597 static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
598 [IFLA_VTI_LINK] = { .type = NLA_U32 },
599 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
600 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
601 [IFLA_VTI_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
602 [IFLA_VTI_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
605 static struct rtnl_link_ops vti_link_ops __read_mostly = {
607 .maxtype = IFLA_VTI_MAX,
608 .policy = vti_policy,
609 .priv_size = sizeof(struct ip_tunnel),
610 .setup = vti_tunnel_setup,
611 .validate = vti_tunnel_validate,
612 .newlink = vti_newlink,
613 .changelink = vti_changelink,
614 .dellink = ip_tunnel_dellink,
615 .get_size = vti_get_size,
616 .fill_info = vti_fill_info,
617 .get_link_net = ip_tunnel_get_link_net,
620 static bool is_vti_tunnel(const struct net_device *dev)
622 return dev->netdev_ops == &vti_netdev_ops;
625 static int vti_device_event(struct notifier_block *unused,
626 unsigned long event, void *ptr)
628 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
629 struct ip_tunnel *tunnel = netdev_priv(dev);
631 if (!is_vti_tunnel(dev))
636 if (!net_eq(tunnel->net, dev_net(dev)))
637 xfrm_garbage_collect(tunnel->net);
643 static struct notifier_block vti_notifier_block __read_mostly = {
644 .notifier_call = vti_device_event,
647 static int __init vti_init(void)
652 pr_info("IPv4 over IPsec tunneling driver\n");
654 register_netdevice_notifier(&vti_notifier_block);
656 msg = "tunnel device";
657 err = register_pernet_device(&vti_net_ops);
659 goto pernet_dev_failed;
661 msg = "tunnel protocols";
662 err = xfrm4_protocol_register(&vti_esp4_protocol, IPPROTO_ESP);
664 goto xfrm_proto_esp_failed;
665 err = xfrm4_protocol_register(&vti_ah4_protocol, IPPROTO_AH);
667 goto xfrm_proto_ah_failed;
668 err = xfrm4_protocol_register(&vti_ipcomp4_protocol, IPPROTO_COMP);
670 goto xfrm_proto_comp_failed;
673 err = xfrm4_tunnel_register(&ipip_handler, AF_INET);
675 goto xfrm_tunnel_failed;
677 msg = "netlink interface";
678 err = rtnl_link_register(&vti_link_ops);
680 goto rtnl_link_failed;
685 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
687 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
688 xfrm_proto_comp_failed:
689 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
690 xfrm_proto_ah_failed:
691 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
692 xfrm_proto_esp_failed:
693 unregister_pernet_device(&vti_net_ops);
695 unregister_netdevice_notifier(&vti_notifier_block);
696 pr_err("vti init: failed to register %s\n", msg);
700 static void __exit vti_fini(void)
702 rtnl_link_unregister(&vti_link_ops);
703 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
704 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
705 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
706 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
707 unregister_pernet_device(&vti_net_ops);
708 unregister_netdevice_notifier(&vti_notifier_block);
711 module_init(vti_init);
712 module_exit(vti_fini);
713 MODULE_LICENSE("GPL");
714 MODULE_ALIAS_RTNL_LINK("vti");
715 MODULE_ALIAS_NETDEV("ip_vti0");