1 // SPDX-License-Identifier: GPL-2.0
3 * XFRM virtual interface
5 * Copyright (C) 2018 secunet Security Networks AG
8 * Steffen Klassert <steffen.klassert@secunet.com>
11 #include <linux/module.h>
12 #include <linux/capability.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/sockios.h>
16 #include <linux/icmp.h>
20 #include <linux/net.h>
21 #include <linux/in6.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_link.h>
24 #include <linux/if_arp.h>
25 #include <linux/icmpv6.h>
26 #include <linux/init.h>
27 #include <linux/route.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/netfilter_ipv6.h>
30 #include <linux/slab.h>
31 #include <linux/hash.h>
33 #include <linux/uaccess.h>
34 #include <linux/atomic.h>
39 #include <net/ip6_route.h>
40 #include <net/ip_tunnels.h>
41 #include <net/addrconf.h>
43 #include <net/net_namespace.h>
44 #include <net/dst_metadata.h>
45 #include <net/netns/generic.h>
46 #include <linux/etherdevice.h>
48 static int xfrmi_dev_init(struct net_device *dev);
49 static void xfrmi_dev_setup(struct net_device *dev);
50 static struct rtnl_link_ops xfrmi_link_ops __read_mostly;
51 static unsigned int xfrmi_net_id __read_mostly;
52 static const struct net_device_ops xfrmi_netdev_ops;
54 #define XFRMI_HASH_BITS 8
55 #define XFRMI_HASH_SIZE BIT(XFRMI_HASH_BITS)
58 /* lists for storing interfaces in use */
59 struct xfrm_if __rcu *xfrmi[XFRMI_HASH_SIZE];
60 struct xfrm_if __rcu *collect_md_xfrmi;
63 static const struct nla_policy xfrm_lwt_policy[LWT_XFRM_MAX + 1] = {
64 [LWT_XFRM_IF_ID] = NLA_POLICY_MIN(NLA_U32, 1),
65 [LWT_XFRM_LINK] = NLA_POLICY_MIN(NLA_U32, 1),
68 static void xfrmi_destroy_state(struct lwtunnel_state *lwt)
72 static int xfrmi_build_state(struct net *net, struct nlattr *nla,
73 unsigned int family, const void *cfg,
74 struct lwtunnel_state **ts,
75 struct netlink_ext_ack *extack)
77 struct nlattr *tb[LWT_XFRM_MAX + 1];
78 struct lwtunnel_state *new_state;
79 struct xfrm_md_info *info;
82 ret = nla_parse_nested(tb, LWT_XFRM_MAX, nla, xfrm_lwt_policy, extack);
86 if (!tb[LWT_XFRM_IF_ID]) {
87 NL_SET_ERR_MSG(extack, "if_id must be set");
91 new_state = lwtunnel_state_alloc(sizeof(*info));
93 NL_SET_ERR_MSG(extack, "failed to create encap info");
97 new_state->type = LWTUNNEL_ENCAP_XFRM;
99 info = lwt_xfrm_info(new_state);
101 info->if_id = nla_get_u32(tb[LWT_XFRM_IF_ID]);
103 if (tb[LWT_XFRM_LINK])
104 info->link = nla_get_u32(tb[LWT_XFRM_LINK]);
110 static int xfrmi_fill_encap_info(struct sk_buff *skb,
111 struct lwtunnel_state *lwt)
113 struct xfrm_md_info *info = lwt_xfrm_info(lwt);
115 if (nla_put_u32(skb, LWT_XFRM_IF_ID, info->if_id) ||
116 (info->link && nla_put_u32(skb, LWT_XFRM_LINK, info->link)))
122 static int xfrmi_encap_nlsize(struct lwtunnel_state *lwtstate)
124 return nla_total_size(sizeof(u32)) + /* LWT_XFRM_IF_ID */
125 nla_total_size(sizeof(u32)); /* LWT_XFRM_LINK */
128 static int xfrmi_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
130 struct xfrm_md_info *a_info = lwt_xfrm_info(a);
131 struct xfrm_md_info *b_info = lwt_xfrm_info(b);
133 return memcmp(a_info, b_info, sizeof(*a_info));
136 static const struct lwtunnel_encap_ops xfrmi_encap_ops = {
137 .build_state = xfrmi_build_state,
138 .destroy_state = xfrmi_destroy_state,
139 .fill_encap = xfrmi_fill_encap_info,
140 .get_encap_size = xfrmi_encap_nlsize,
141 .cmp_encap = xfrmi_encap_cmp,
142 .owner = THIS_MODULE,
145 #define for_each_xfrmi_rcu(start, xi) \
146 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
148 static u32 xfrmi_hash(u32 if_id)
150 return hash_32(if_id, XFRMI_HASH_BITS);
153 static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x)
155 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
158 for_each_xfrmi_rcu(xfrmn->xfrmi[xfrmi_hash(x->if_id)], xi) {
159 if (x->if_id == xi->p.if_id &&
160 (xi->dev->flags & IFF_UP))
164 xi = rcu_dereference(xfrmn->collect_md_xfrmi);
165 if (xi && (xi->dev->flags & IFF_UP))
171 static bool xfrmi_decode_session(struct sk_buff *skb,
172 unsigned short family,
173 struct xfrm_if_decode_session_result *res)
175 struct net_device *dev;
179 if (!secpath_exists(skb) || !skb->dev)
184 ifindex = inet6_sdif(skb);
187 ifindex = inet_sdif(skb);
192 struct net *net = xs_net(xfrm_input_state(skb));
194 dev = dev_get_by_index_rcu(net, ifindex);
199 if (!dev || !(dev->flags & IFF_UP))
201 if (dev->netdev_ops != &xfrmi_netdev_ops)
204 xi = netdev_priv(dev);
207 if (xi->p.collect_md)
208 res->if_id = xfrm_input_state(skb)->if_id;
210 res->if_id = xi->p.if_id;
214 static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
216 struct xfrm_if __rcu **xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)];
218 rcu_assign_pointer(xi->next , rtnl_dereference(*xip));
219 rcu_assign_pointer(*xip, xi);
222 static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
224 struct xfrm_if __rcu **xip;
225 struct xfrm_if *iter;
227 for (xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)];
228 (iter = rtnl_dereference(*xip)) != NULL;
231 rcu_assign_pointer(*xip, xi->next);
237 static void xfrmi_dev_free(struct net_device *dev)
239 struct xfrm_if *xi = netdev_priv(dev);
241 gro_cells_destroy(&xi->gro_cells);
242 free_percpu(dev->tstats);
245 static int xfrmi_create(struct net_device *dev)
247 struct xfrm_if *xi = netdev_priv(dev);
248 struct net *net = dev_net(dev);
249 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
252 dev->rtnl_link_ops = &xfrmi_link_ops;
253 err = register_netdevice(dev);
257 if (xi->p.collect_md)
258 rcu_assign_pointer(xfrmn->collect_md_xfrmi, xi);
260 xfrmi_link(xfrmn, xi);
268 static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
270 struct xfrm_if __rcu **xip;
272 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
274 for (xip = &xfrmn->xfrmi[xfrmi_hash(p->if_id)];
275 (xi = rtnl_dereference(*xip)) != NULL;
277 if (xi->p.if_id == p->if_id)
283 static void xfrmi_dev_uninit(struct net_device *dev)
285 struct xfrm_if *xi = netdev_priv(dev);
286 struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
288 if (xi->p.collect_md)
289 RCU_INIT_POINTER(xfrmn->collect_md_xfrmi, NULL);
291 xfrmi_unlink(xfrmn, xi);
294 static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
296 skb_clear_tstamp(skb);
297 skb->pkt_type = PACKET_HOST;
313 static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
315 const struct xfrm_mode *inner_mode;
316 struct net_device *dev;
317 struct xfrm_state *x;
322 if (err && !secpath_exists(skb))
325 x = xfrm_input_state(skb);
327 xi = xfrmi_lookup(xs_net(x), x);
331 link = skb->dev->ifindex;
336 dev->stats.rx_errors++;
337 dev->stats.rx_dropped++;
342 xnet = !net_eq(xi->net, dev_net(skb->dev));
345 inner_mode = &x->inner_mode;
347 if (x->sel.family == AF_UNSPEC) {
348 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
349 if (inner_mode == NULL) {
350 XFRM_INC_STATS(dev_net(skb->dev),
351 LINUX_MIB_XFRMINSTATEMODEERROR);
356 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
361 xfrmi_scrub_packet(skb, xnet);
362 if (xi->p.collect_md) {
363 struct metadata_dst *md_dst;
365 md_dst = metadata_dst_alloc(0, METADATA_XFRM, GFP_ATOMIC);
369 md_dst->u.xfrm_info.if_id = x->if_id;
370 md_dst->u.xfrm_info.link = link;
371 skb_dst_set(skb, (struct dst_entry *)md_dst);
373 dev_sw_netstats_rx_add(dev, skb->len);
379 xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
381 struct xfrm_if *xi = netdev_priv(dev);
382 struct net_device_stats *stats = &xi->dev->stats;
383 struct dst_entry *dst = skb_dst(skb);
384 unsigned int length = skb->len;
385 struct net_device *tdev;
386 struct xfrm_state *x;
391 if (xi->p.collect_md) {
392 struct xfrm_md_info *md_info = skb_xfrm_md_info(skb);
394 if (unlikely(!md_info))
397 if_id = md_info->if_id;
398 fl->flowi_oif = md_info->link;
404 dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, if_id);
408 goto tx_err_link_failure;
413 goto tx_err_link_failure;
415 if (x->if_id != if_id)
416 goto tx_err_link_failure;
422 net_warn_ratelimited("%s: Local routing loop detected!\n",
424 goto tx_err_dst_release;
428 if ((!skb_is_gso(skb) && skb->len > mtu) ||
429 (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu))) {
430 skb_dst_update_pmtu_no_confirm(skb, mtu);
432 if (skb->protocol == htons(ETH_P_IPV6)) {
433 if (mtu < IPV6_MIN_MTU)
437 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
441 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
443 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
452 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
453 skb_dst_set(skb, dst);
456 err = dst_output(xi->net, skb->sk, skb);
457 if (net_xmit_eval(err) == 0) {
458 dev_sw_netstats_tx_add(dev, 1, length);
461 stats->tx_aborted_errors++;
466 stats->tx_carrier_errors++;
467 dst_link_failure(skb);
473 static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
475 struct xfrm_if *xi = netdev_priv(dev);
476 struct net_device_stats *stats = &xi->dev->stats;
477 struct dst_entry *dst = skb_dst(skb);
481 memset(&fl, 0, sizeof(fl));
483 switch (skb->protocol) {
484 case htons(ETH_P_IPV6):
485 xfrm_decode_session(skb, &fl, AF_INET6);
486 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
488 fl.u.ip6.flowi6_oif = dev->ifindex;
489 fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
490 dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6);
493 stats->tx_carrier_errors++;
496 skb_dst_set(skb, dst);
499 case htons(ETH_P_IP):
500 xfrm_decode_session(skb, &fl, AF_INET);
501 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
505 fl.u.ip4.flowi4_oif = dev->ifindex;
506 fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
507 rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4);
509 stats->tx_carrier_errors++;
512 skb_dst_set(skb, &rt->dst);
519 fl.flowi_oif = xi->p.link;
521 ret = xfrmi_xmit2(skb, dev, &fl);
534 static int xfrmi4_err(struct sk_buff *skb, u32 info)
536 const struct iphdr *iph = (const struct iphdr *)skb->data;
537 struct net *net = dev_net(skb->dev);
538 int protocol = iph->protocol;
539 struct ip_comp_hdr *ipch;
540 struct ip_esp_hdr *esph;
541 struct ip_auth_hdr *ah ;
542 struct xfrm_state *x;
548 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
552 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
556 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
557 spi = htonl(ntohs(ipch->cpi));
563 switch (icmp_hdr(skb)->type) {
564 case ICMP_DEST_UNREACH:
565 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
574 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
575 spi, protocol, AF_INET);
579 xi = xfrmi_lookup(net, x);
585 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
586 ipv4_update_pmtu(skb, net, info, 0, protocol);
588 ipv4_redirect(skb, net, 0, protocol);
594 static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
595 u8 type, u8 code, int offset, __be32 info)
597 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
598 struct net *net = dev_net(skb->dev);
599 int protocol = iph->nexthdr;
600 struct ip_comp_hdr *ipch;
601 struct ip_esp_hdr *esph;
602 struct ip_auth_hdr *ah;
603 struct xfrm_state *x;
609 esph = (struct ip_esp_hdr *)(skb->data + offset);
613 ah = (struct ip_auth_hdr *)(skb->data + offset);
617 ipch = (struct ip_comp_hdr *)(skb->data + offset);
618 spi = htonl(ntohs(ipch->cpi));
624 if (type != ICMPV6_PKT_TOOBIG &&
625 type != NDISC_REDIRECT)
628 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
629 spi, protocol, AF_INET6);
633 xi = xfrmi_lookup(net, x);
639 if (type == NDISC_REDIRECT)
640 ip6_redirect(skb, net, skb->dev->ifindex, 0,
641 sock_net_uid(net, NULL));
643 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
649 static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
651 if (xi->p.link != p->link)
654 xi->p.if_id = p->if_id;
659 static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
661 struct net *net = xi->net;
662 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
665 xfrmi_unlink(xfrmn, xi);
667 err = xfrmi_change(xi, p);
668 xfrmi_link(xfrmn, xi);
669 netdev_state_change(xi->dev);
673 static int xfrmi_get_iflink(const struct net_device *dev)
675 struct xfrm_if *xi = netdev_priv(dev);
680 static const struct net_device_ops xfrmi_netdev_ops = {
681 .ndo_init = xfrmi_dev_init,
682 .ndo_uninit = xfrmi_dev_uninit,
683 .ndo_start_xmit = xfrmi_xmit,
684 .ndo_get_stats64 = dev_get_tstats64,
685 .ndo_get_iflink = xfrmi_get_iflink,
688 static void xfrmi_dev_setup(struct net_device *dev)
690 dev->netdev_ops = &xfrmi_netdev_ops;
691 dev->header_ops = &ip_tunnel_header_ops;
692 dev->type = ARPHRD_NONE;
693 dev->mtu = ETH_DATA_LEN;
694 dev->min_mtu = ETH_MIN_MTU;
695 dev->max_mtu = IP_MAX_MTU;
696 dev->flags = IFF_NOARP;
697 dev->needs_free_netdev = true;
698 dev->priv_destructor = xfrmi_dev_free;
701 eth_broadcast_addr(dev->broadcast);
704 #define XFRMI_FEATURES (NETIF_F_SG | \
706 NETIF_F_GSO_SOFTWARE | \
709 static int xfrmi_dev_init(struct net_device *dev)
711 struct xfrm_if *xi = netdev_priv(dev);
712 struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link);
715 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
719 err = gro_cells_init(&xi->gro_cells, dev);
721 free_percpu(dev->tstats);
725 dev->features |= NETIF_F_LLTX;
726 dev->features |= XFRMI_FEATURES;
727 dev->hw_features |= XFRMI_FEATURES;
730 dev->needed_headroom = phydev->needed_headroom;
731 dev->needed_tailroom = phydev->needed_tailroom;
733 if (is_zero_ether_addr(dev->dev_addr))
734 eth_hw_addr_inherit(dev, phydev);
735 if (is_zero_ether_addr(dev->broadcast))
736 memcpy(dev->broadcast, phydev->broadcast,
739 eth_hw_addr_random(dev);
740 eth_broadcast_addr(dev->broadcast);
746 static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
747 struct netlink_ext_ack *extack)
752 static void xfrmi_netlink_parms(struct nlattr *data[],
753 struct xfrm_if_parms *parms)
755 memset(parms, 0, sizeof(*parms));
760 if (data[IFLA_XFRM_LINK])
761 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
763 if (data[IFLA_XFRM_IF_ID])
764 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
766 if (data[IFLA_XFRM_COLLECT_METADATA])
767 parms->collect_md = true;
770 static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
771 struct nlattr *tb[], struct nlattr *data[],
772 struct netlink_ext_ack *extack)
774 struct net *net = dev_net(dev);
775 struct xfrm_if_parms p = {};
779 xfrmi_netlink_parms(data, &p);
781 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
783 if (p.link || p.if_id) {
784 NL_SET_ERR_MSG(extack, "link and if_id must be zero");
788 if (rtnl_dereference(xfrmn->collect_md_xfrmi))
793 NL_SET_ERR_MSG(extack, "if_id must be non zero");
797 xi = xfrmi_locate(net, &p);
802 xi = netdev_priv(dev);
807 err = xfrmi_create(dev);
811 static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
813 unregister_netdevice_queue(dev, head);
816 static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
817 struct nlattr *data[],
818 struct netlink_ext_ack *extack)
820 struct xfrm_if *xi = netdev_priv(dev);
821 struct net *net = xi->net;
822 struct xfrm_if_parms p = {};
824 xfrmi_netlink_parms(data, &p);
826 NL_SET_ERR_MSG(extack, "if_id must be non zero");
831 NL_SET_ERR_MSG(extack, "collect_md can't be changed");
835 xi = xfrmi_locate(net, &p);
837 xi = netdev_priv(dev);
841 if (xi->p.collect_md) {
842 NL_SET_ERR_MSG(extack,
843 "device can't be changed to collect_md");
848 return xfrmi_update(xi, &p);
851 static size_t xfrmi_get_size(const struct net_device *dev)
856 /* IFLA_XFRM_IF_ID */
858 /* IFLA_XFRM_COLLECT_METADATA */
863 static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
865 struct xfrm_if *xi = netdev_priv(dev);
866 struct xfrm_if_parms *parm = &xi->p;
868 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
869 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id) ||
870 (xi->p.collect_md && nla_put_flag(skb, IFLA_XFRM_COLLECT_METADATA)))
871 goto nla_put_failure;
878 static struct net *xfrmi_get_link_net(const struct net_device *dev)
880 struct xfrm_if *xi = netdev_priv(dev);
885 static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
886 [IFLA_XFRM_UNSPEC] = { .strict_start_type = IFLA_XFRM_COLLECT_METADATA },
887 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
888 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
889 [IFLA_XFRM_COLLECT_METADATA] = { .type = NLA_FLAG },
892 static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
894 .maxtype = IFLA_XFRM_MAX,
895 .policy = xfrmi_policy,
896 .priv_size = sizeof(struct xfrm_if),
897 .setup = xfrmi_dev_setup,
898 .validate = xfrmi_validate,
899 .newlink = xfrmi_newlink,
900 .dellink = xfrmi_dellink,
901 .changelink = xfrmi_changelink,
902 .get_size = xfrmi_get_size,
903 .fill_info = xfrmi_fill_info,
904 .get_link_net = xfrmi_get_link_net,
907 static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list)
913 list_for_each_entry(net, net_exit_list, exit_list) {
914 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
915 struct xfrm_if __rcu **xip;
919 for (i = 0; i < XFRMI_HASH_SIZE; i++) {
920 for (xip = &xfrmn->xfrmi[i];
921 (xi = rtnl_dereference(*xip)) != NULL;
923 unregister_netdevice_queue(xi->dev, &list);
925 xi = rtnl_dereference(xfrmn->collect_md_xfrmi);
927 unregister_netdevice_queue(xi->dev, &list);
929 unregister_netdevice_many(&list);
933 static struct pernet_operations xfrmi_net_ops = {
934 .exit_batch = xfrmi_exit_batch_net,
936 .size = sizeof(struct xfrmi_net),
939 static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
940 .handler = xfrm6_rcv,
941 .input_handler = xfrm_input,
942 .cb_handler = xfrmi_rcv_cb,
943 .err_handler = xfrmi6_err,
947 static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
948 .handler = xfrm6_rcv,
949 .input_handler = xfrm_input,
950 .cb_handler = xfrmi_rcv_cb,
951 .err_handler = xfrmi6_err,
955 static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
956 .handler = xfrm6_rcv,
957 .input_handler = xfrm_input,
958 .cb_handler = xfrmi_rcv_cb,
959 .err_handler = xfrmi6_err,
963 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
964 static int xfrmi6_rcv_tunnel(struct sk_buff *skb)
966 const xfrm_address_t *saddr;
969 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
970 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
972 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
975 static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = {
976 .handler = xfrmi6_rcv_tunnel,
977 .cb_handler = xfrmi_rcv_cb,
978 .err_handler = xfrmi6_err,
982 static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = {
983 .handler = xfrmi6_rcv_tunnel,
984 .cb_handler = xfrmi_rcv_cb,
985 .err_handler = xfrmi6_err,
990 static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
991 .handler = xfrm4_rcv,
992 .input_handler = xfrm_input,
993 .cb_handler = xfrmi_rcv_cb,
994 .err_handler = xfrmi4_err,
998 static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
999 .handler = xfrm4_rcv,
1000 .input_handler = xfrm_input,
1001 .cb_handler = xfrmi_rcv_cb,
1002 .err_handler = xfrmi4_err,
1006 static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
1007 .handler = xfrm4_rcv,
1008 .input_handler = xfrm_input,
1009 .cb_handler = xfrmi_rcv_cb,
1010 .err_handler = xfrmi4_err,
1014 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1015 static int xfrmi4_rcv_tunnel(struct sk_buff *skb)
1017 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
1020 static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly = {
1021 .handler = xfrmi4_rcv_tunnel,
1022 .cb_handler = xfrmi_rcv_cb,
1023 .err_handler = xfrmi4_err,
1027 static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly = {
1028 .handler = xfrmi4_rcv_tunnel,
1029 .cb_handler = xfrmi_rcv_cb,
1030 .err_handler = xfrmi4_err,
1035 static int __init xfrmi4_init(void)
1039 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
1041 goto xfrm_proto_esp_failed;
1042 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
1044 goto xfrm_proto_ah_failed;
1045 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
1047 goto xfrm_proto_comp_failed;
1048 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1049 err = xfrm4_tunnel_register(&xfrmi_ipip_handler, AF_INET);
1051 goto xfrm_tunnel_ipip_failed;
1052 err = xfrm4_tunnel_register(&xfrmi_ipip6_handler, AF_INET6);
1054 goto xfrm_tunnel_ipip6_failed;
1059 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1060 xfrm_tunnel_ipip6_failed:
1061 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
1062 xfrm_tunnel_ipip_failed:
1063 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
1065 xfrm_proto_comp_failed:
1066 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
1067 xfrm_proto_ah_failed:
1068 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
1069 xfrm_proto_esp_failed:
1073 static void xfrmi4_fini(void)
1075 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1076 xfrm4_tunnel_deregister(&xfrmi_ipip6_handler, AF_INET6);
1077 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
1079 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
1080 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
1081 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
1084 static int __init xfrmi6_init(void)
1088 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
1090 goto xfrm_proto_esp_failed;
1091 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
1093 goto xfrm_proto_ah_failed;
1094 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
1096 goto xfrm_proto_comp_failed;
1097 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1098 err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6);
1100 goto xfrm_tunnel_ipv6_failed;
1101 err = xfrm6_tunnel_register(&xfrmi_ip6ip_handler, AF_INET);
1103 goto xfrm_tunnel_ip6ip_failed;
1108 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1109 xfrm_tunnel_ip6ip_failed:
1110 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
1111 xfrm_tunnel_ipv6_failed:
1112 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
1114 xfrm_proto_comp_failed:
1115 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
1116 xfrm_proto_ah_failed:
1117 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
1118 xfrm_proto_esp_failed:
1122 static void xfrmi6_fini(void)
1124 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1125 xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler, AF_INET);
1126 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
1128 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
1129 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
1130 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
1133 static const struct xfrm_if_cb xfrm_if_cb = {
1134 .decode_session = xfrmi_decode_session,
1137 static int __init xfrmi_init(void)
1142 pr_info("IPsec XFRM device driver\n");
1144 msg = "tunnel device";
1145 err = register_pernet_device(&xfrmi_net_ops);
1147 goto pernet_dev_failed;
1149 msg = "xfrm4 protocols";
1150 err = xfrmi4_init();
1154 msg = "xfrm6 protocols";
1155 err = xfrmi6_init();
1160 msg = "netlink interface";
1161 err = rtnl_link_register(&xfrmi_link_ops);
1163 goto rtnl_link_failed;
1165 lwtunnel_encap_add_ops(&xfrmi_encap_ops, LWTUNNEL_ENCAP_XFRM);
1167 xfrm_if_register_cb(&xfrm_if_cb);
1176 unregister_pernet_device(&xfrmi_net_ops);
1178 pr_err("xfrmi init: failed to register %s\n", msg);
1182 static void __exit xfrmi_fini(void)
1184 xfrm_if_unregister_cb();
1185 lwtunnel_encap_del_ops(&xfrmi_encap_ops, LWTUNNEL_ENCAP_XFRM);
1186 rtnl_link_unregister(&xfrmi_link_ops);
1189 unregister_pernet_device(&xfrmi_net_ops);
1192 module_init(xfrmi_init);
1193 module_exit(xfrmi_fini);
1194 MODULE_LICENSE("GPL");
1195 MODULE_ALIAS_RTNL_LINK("xfrm");
1196 MODULE_ALIAS_NETDEV("xfrm0");
1197 MODULE_AUTHOR("Steffen Klassert");
1198 MODULE_DESCRIPTION("XFRM virtual interface");