1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux NET3: IP/IP protocol decoder modified to support
4 * virtual tunnel interface
11 This version of net/ipv4/ip_vti.c is cloned of net/ipv4/ipip.c
13 For comments look at net/ipv4/ip_gre.c --ANK
17 #include <linux/capability.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/uaccess.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/if_arp.h>
28 #include <linux/init.h>
29 #include <linux/netfilter_ipv4.h>
30 #include <linux/if_ether.h>
31 #include <linux/icmpv6.h>
36 #include <net/ip_tunnels.h>
37 #include <net/inet_ecn.h>
39 #include <net/net_namespace.h>
40 #include <net/netns/generic.h>
42 static struct rtnl_link_ops vti_link_ops __read_mostly;
44 static unsigned int vti_net_id __read_mostly;
45 static int vti_tunnel_init(struct net_device *dev);
47 static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
48 int encap_type, bool update_skb_dev)
50 struct ip_tunnel *tunnel;
51 const struct iphdr *iph = ip_hdr(skb);
52 struct net *net = dev_net(skb->dev);
53 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
55 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
56 iph->saddr, iph->daddr, 0);
58 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
61 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
64 skb->dev = tunnel->dev;
66 return xfrm_input(skb, nexthdr, spi, encap_type);
75 static int vti_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
78 return vti_input(skb, nexthdr, spi, encap_type, false);
81 static int vti_rcv(struct sk_buff *skb, __be32 spi, bool update_skb_dev)
83 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
84 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
86 return vti_input(skb, ip_hdr(skb)->protocol, spi, 0, update_skb_dev);
89 static int vti_rcv_proto(struct sk_buff *skb)
91 return vti_rcv(skb, 0, false);
94 static int vti_rcv_tunnel(struct sk_buff *skb)
96 struct ip_tunnel_net *itn = net_generic(dev_net(skb->dev), vti_net_id);
97 const struct iphdr *iph = ip_hdr(skb);
98 struct ip_tunnel *tunnel;
100 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
101 iph->saddr, iph->daddr, 0);
103 struct tnl_ptk_info tpi = {
104 .proto = htons(ETH_P_IP),
107 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
109 if (iptunnel_pull_header(skb, 0, tpi.proto, false))
111 return ip_tunnel_rcv(tunnel, skb, &tpi, NULL, false);
120 static int vti_rcv_cb(struct sk_buff *skb, int err)
122 unsigned short family;
123 struct net_device *dev;
124 struct pcpu_sw_netstats *tstats;
125 struct xfrm_state *x;
126 const struct xfrm_mode *inner_mode;
127 struct ip_tunnel *tunnel = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4;
128 u32 orig_mark = skb->mark;
137 dev->stats.rx_errors++;
138 dev->stats.rx_dropped++;
143 x = xfrm_input_state(skb);
145 inner_mode = &x->inner_mode;
147 if (x->sel.family == AF_UNSPEC) {
148 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
149 if (inner_mode == NULL) {
150 XFRM_INC_STATS(dev_net(skb->dev),
151 LINUX_MIB_XFRMINSTATEMODEERROR);
156 family = inner_mode->family;
158 skb->mark = be32_to_cpu(tunnel->parms.i_key);
159 ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
160 skb->mark = orig_mark;
165 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
168 tstats = this_cpu_ptr(dev->tstats);
170 u64_stats_update_begin(&tstats->syncp);
171 tstats->rx_packets++;
172 tstats->rx_bytes += skb->len;
173 u64_stats_update_end(&tstats->syncp);
178 static bool vti_state_check(const struct xfrm_state *x, __be32 dst, __be32 src)
180 xfrm_address_t *daddr = (xfrm_address_t *)&dst;
181 xfrm_address_t *saddr = (xfrm_address_t *)&src;
183 /* if there is no transform then this tunnel is not functional.
184 * Or if the xfrm is not mode tunnel.
186 if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
187 x->props.family != AF_INET)
191 return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET);
193 if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET))
199 static netdev_tx_t vti_xmit(struct sk_buff *skb, struct net_device *dev,
202 struct ip_tunnel *tunnel = netdev_priv(dev);
203 struct ip_tunnel_parm *parms = &tunnel->parms;
204 struct dst_entry *dst = skb_dst(skb);
205 struct net_device *tdev; /* Device to other host */
206 int pkt_len = skb->len;
211 switch (skb->protocol) {
212 case htons(ETH_P_IP): {
215 fl->u.ip4.flowi4_oif = dev->ifindex;
216 fl->u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
217 rt = __ip_route_output_key(dev_net(dev), &fl->u.ip4);
219 dev->stats.tx_carrier_errors++;
223 skb_dst_set(skb, dst);
226 #if IS_ENABLED(CONFIG_IPV6)
227 case htons(ETH_P_IPV6):
228 fl->u.ip6.flowi6_oif = dev->ifindex;
229 fl->u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
230 dst = ip6_route_output(dev_net(dev), NULL, &fl->u.ip6);
234 dev->stats.tx_carrier_errors++;
237 skb_dst_set(skb, dst);
241 dev->stats.tx_carrier_errors++;
247 dst = xfrm_lookup(tunnel->net, dst, fl, NULL, 0);
249 dev->stats.tx_carrier_errors++;
253 if (!vti_state_check(dst->xfrm, parms->iph.daddr, parms->iph.saddr)) {
254 dev->stats.tx_carrier_errors++;
263 dev->stats.collisions++;
268 if (skb->len > mtu) {
269 skb_dst_update_pmtu_no_confirm(skb, mtu);
270 if (skb->protocol == htons(ETH_P_IP)) {
271 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
274 if (mtu < IPV6_MIN_MTU)
277 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
284 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(dev)));
285 skb_dst_set(skb, dst);
286 skb->dev = skb_dst(skb)->dev;
288 err = dst_output(tunnel->net, skb->sk, skb);
289 if (net_xmit_eval(err) == 0)
291 iptunnel_xmit_stats(dev, err);
295 dst_link_failure(skb);
297 dev->stats.tx_errors++;
302 /* This function assumes it is being called from dev_queue_xmit()
303 * and that skb is filled properly by that function.
305 static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
307 struct ip_tunnel *tunnel = netdev_priv(dev);
310 if (!pskb_inet_may_pull(skb))
313 memset(&fl, 0, sizeof(fl));
315 switch (skb->protocol) {
316 case htons(ETH_P_IP):
317 xfrm_decode_session(skb, &fl, AF_INET);
318 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
320 case htons(ETH_P_IPV6):
321 xfrm_decode_session(skb, &fl, AF_INET6);
322 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
328 /* override mark with tunnel output key */
329 fl.flowi_mark = be32_to_cpu(tunnel->parms.o_key);
331 return vti_xmit(skb, dev, &fl);
334 dev->stats.tx_errors++;
339 static int vti4_err(struct sk_buff *skb, u32 info)
343 struct xfrm_state *x;
344 struct ip_tunnel *tunnel;
345 struct ip_esp_hdr *esph;
346 struct ip_auth_hdr *ah ;
347 struct ip_comp_hdr *ipch;
348 struct net *net = dev_net(skb->dev);
349 const struct iphdr *iph = (const struct iphdr *)skb->data;
350 int protocol = iph->protocol;
351 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
353 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
354 iph->daddr, iph->saddr, 0);
358 mark = be32_to_cpu(tunnel->parms.o_key);
362 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
366 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
370 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
371 spi = htonl(ntohs(ipch->cpi));
377 switch (icmp_hdr(skb)->type) {
378 case ICMP_DEST_UNREACH:
379 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
387 x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
388 spi, protocol, AF_INET);
392 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
393 ipv4_update_pmtu(skb, net, info, 0, protocol);
395 ipv4_redirect(skb, net, 0, protocol);
402 vti_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
406 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
407 if (p->iph.version != 4 || p->iph.protocol != IPPROTO_IPIP ||
412 if (!(p->i_flags & GRE_KEY))
414 if (!(p->o_flags & GRE_KEY))
417 p->i_flags = VTI_ISVTI;
419 err = ip_tunnel_ctl(dev, p, cmd);
423 if (cmd != SIOCDELTUNNEL) {
424 p->i_flags |= GRE_KEY;
425 p->o_flags |= GRE_KEY;
430 static const struct net_device_ops vti_netdev_ops = {
431 .ndo_init = vti_tunnel_init,
432 .ndo_uninit = ip_tunnel_uninit,
433 .ndo_start_xmit = vti_tunnel_xmit,
434 .ndo_do_ioctl = ip_tunnel_ioctl,
435 .ndo_change_mtu = ip_tunnel_change_mtu,
436 .ndo_get_stats64 = ip_tunnel_get_stats64,
437 .ndo_get_iflink = ip_tunnel_get_iflink,
438 .ndo_tunnel_ctl = vti_tunnel_ctl,
441 static void vti_tunnel_setup(struct net_device *dev)
443 dev->netdev_ops = &vti_netdev_ops;
444 dev->type = ARPHRD_TUNNEL;
445 ip_tunnel_setup(dev, vti_net_id);
448 static int vti_tunnel_init(struct net_device *dev)
450 struct ip_tunnel *tunnel = netdev_priv(dev);
451 struct iphdr *iph = &tunnel->parms.iph;
453 memcpy(dev->dev_addr, &iph->saddr, 4);
454 memcpy(dev->broadcast, &iph->daddr, 4);
456 dev->flags = IFF_NOARP;
458 dev->features |= NETIF_F_LLTX;
461 return ip_tunnel_init(dev);
464 static void __net_init vti_fb_tunnel_init(struct net_device *dev)
466 struct ip_tunnel *tunnel = netdev_priv(dev);
467 struct iphdr *iph = &tunnel->parms.iph;
470 iph->protocol = IPPROTO_IPIP;
474 static struct xfrm4_protocol vti_esp4_protocol __read_mostly = {
475 .handler = vti_rcv_proto,
476 .input_handler = vti_input_proto,
477 .cb_handler = vti_rcv_cb,
478 .err_handler = vti4_err,
482 static struct xfrm4_protocol vti_ah4_protocol __read_mostly = {
483 .handler = vti_rcv_proto,
484 .input_handler = vti_input_proto,
485 .cb_handler = vti_rcv_cb,
486 .err_handler = vti4_err,
490 static struct xfrm4_protocol vti_ipcomp4_protocol __read_mostly = {
491 .handler = vti_rcv_proto,
492 .input_handler = vti_input_proto,
493 .cb_handler = vti_rcv_cb,
494 .err_handler = vti4_err,
498 static struct xfrm_tunnel ipip_handler __read_mostly = {
499 .handler = vti_rcv_tunnel,
500 .err_handler = vti4_err,
504 static int __net_init vti_init_net(struct net *net)
507 struct ip_tunnel_net *itn;
509 err = ip_tunnel_init_net(net, vti_net_id, &vti_link_ops, "ip_vti0");
512 itn = net_generic(net, vti_net_id);
513 if (itn->fb_tunnel_dev)
514 vti_fb_tunnel_init(itn->fb_tunnel_dev);
518 static void __net_exit vti_exit_batch_net(struct list_head *list_net)
520 ip_tunnel_delete_nets(list_net, vti_net_id, &vti_link_ops);
523 static struct pernet_operations vti_net_ops = {
524 .init = vti_init_net,
525 .exit_batch = vti_exit_batch_net,
527 .size = sizeof(struct ip_tunnel_net),
530 static int vti_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
531 struct netlink_ext_ack *extack)
536 static void vti_netlink_parms(struct nlattr *data[],
537 struct ip_tunnel_parm *parms,
540 memset(parms, 0, sizeof(*parms));
542 parms->iph.protocol = IPPROTO_IPIP;
547 parms->i_flags = VTI_ISVTI;
549 if (data[IFLA_VTI_LINK])
550 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
552 if (data[IFLA_VTI_IKEY])
553 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
555 if (data[IFLA_VTI_OKEY])
556 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
558 if (data[IFLA_VTI_LOCAL])
559 parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
561 if (data[IFLA_VTI_REMOTE])
562 parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
564 if (data[IFLA_VTI_FWMARK])
565 *fwmark = nla_get_u32(data[IFLA_VTI_FWMARK]);
568 static int vti_newlink(struct net *src_net, struct net_device *dev,
569 struct nlattr *tb[], struct nlattr *data[],
570 struct netlink_ext_ack *extack)
572 struct ip_tunnel_parm parms;
575 vti_netlink_parms(data, &parms, &fwmark);
576 return ip_tunnel_newlink(dev, tb, &parms, fwmark);
579 static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
580 struct nlattr *data[],
581 struct netlink_ext_ack *extack)
583 struct ip_tunnel *t = netdev_priv(dev);
584 __u32 fwmark = t->fwmark;
585 struct ip_tunnel_parm p;
587 vti_netlink_parms(data, &p, &fwmark);
588 return ip_tunnel_changelink(dev, tb, &p, fwmark);
591 static size_t vti_get_size(const struct net_device *dev)
602 /* IFLA_VTI_REMOTE */
604 /* IFLA_VTI_FWMARK */
609 static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
611 struct ip_tunnel *t = netdev_priv(dev);
612 struct ip_tunnel_parm *p = &t->parms;
614 if (nla_put_u32(skb, IFLA_VTI_LINK, p->link) ||
615 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key) ||
616 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key) ||
617 nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr) ||
618 nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr) ||
619 nla_put_u32(skb, IFLA_VTI_FWMARK, t->fwmark))
625 static const struct nla_policy vti_policy[IFLA_VTI_MAX + 1] = {
626 [IFLA_VTI_LINK] = { .type = NLA_U32 },
627 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
628 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
629 [IFLA_VTI_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
630 [IFLA_VTI_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
631 [IFLA_VTI_FWMARK] = { .type = NLA_U32 },
634 static struct rtnl_link_ops vti_link_ops __read_mostly = {
636 .maxtype = IFLA_VTI_MAX,
637 .policy = vti_policy,
638 .priv_size = sizeof(struct ip_tunnel),
639 .setup = vti_tunnel_setup,
640 .validate = vti_tunnel_validate,
641 .newlink = vti_newlink,
642 .changelink = vti_changelink,
643 .dellink = ip_tunnel_dellink,
644 .get_size = vti_get_size,
645 .fill_info = vti_fill_info,
646 .get_link_net = ip_tunnel_get_link_net,
649 static int __init vti_init(void)
654 pr_info("IPv4 over IPsec tunneling driver\n");
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 pr_err("vti init: failed to register %s\n", msg);
699 static void __exit vti_fini(void)
701 rtnl_link_unregister(&vti_link_ops);
702 xfrm4_tunnel_deregister(&ipip_handler, AF_INET);
703 xfrm4_protocol_deregister(&vti_ipcomp4_protocol, IPPROTO_COMP);
704 xfrm4_protocol_deregister(&vti_ah4_protocol, IPPROTO_AH);
705 xfrm4_protocol_deregister(&vti_esp4_protocol, IPPROTO_ESP);
706 unregister_pernet_device(&vti_net_ops);
709 module_init(vti_init);
710 module_exit(vti_fini);
711 MODULE_LICENSE("GPL");
712 MODULE_ALIAS_RTNL_LINK("vti");
713 MODULE_ALIAS_NETDEV("ip_vti0");