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>
40 #include <net/ip6_route.h>
41 #include <net/ip_tunnels.h>
42 #include <net/addrconf.h>
44 #include <net/net_namespace.h>
45 #include <net/dst_metadata.h>
46 #include <net/netns/generic.h>
47 #include <linux/etherdevice.h>
49 static int xfrmi_dev_init(struct net_device
*dev
);
50 static void xfrmi_dev_setup(struct net_device
*dev
);
51 static struct rtnl_link_ops xfrmi_link_ops __read_mostly
;
52 static unsigned int xfrmi_net_id __read_mostly
;
53 static const struct net_device_ops xfrmi_netdev_ops
;
55 #define XFRMI_HASH_BITS 8
56 #define XFRMI_HASH_SIZE BIT(XFRMI_HASH_BITS)
59 /* lists for storing interfaces in use */
60 struct xfrm_if __rcu
*xfrmi
[XFRMI_HASH_SIZE
];
61 struct xfrm_if __rcu
*collect_md_xfrmi
;
64 static const struct nla_policy xfrm_lwt_policy
[LWT_XFRM_MAX
+ 1] = {
65 [LWT_XFRM_IF_ID
] = NLA_POLICY_MIN(NLA_U32
, 1),
66 [LWT_XFRM_LINK
] = NLA_POLICY_MIN(NLA_U32
, 1),
69 static void xfrmi_destroy_state(struct lwtunnel_state
*lwt
)
73 static int xfrmi_build_state(struct net
*net
, struct nlattr
*nla
,
74 unsigned int family
, const void *cfg
,
75 struct lwtunnel_state
**ts
,
76 struct netlink_ext_ack
*extack
)
78 struct nlattr
*tb
[LWT_XFRM_MAX
+ 1];
79 struct lwtunnel_state
*new_state
;
80 struct xfrm_md_info
*info
;
83 ret
= nla_parse_nested(tb
, LWT_XFRM_MAX
, nla
, xfrm_lwt_policy
, extack
);
87 if (!tb
[LWT_XFRM_IF_ID
]) {
88 NL_SET_ERR_MSG(extack
, "if_id must be set");
92 new_state
= lwtunnel_state_alloc(sizeof(*info
));
94 NL_SET_ERR_MSG(extack
, "failed to create encap info");
98 new_state
->type
= LWTUNNEL_ENCAP_XFRM
;
100 info
= lwt_xfrm_info(new_state
);
102 info
->if_id
= nla_get_u32(tb
[LWT_XFRM_IF_ID
]);
104 if (tb
[LWT_XFRM_LINK
])
105 info
->link
= nla_get_u32(tb
[LWT_XFRM_LINK
]);
111 static int xfrmi_fill_encap_info(struct sk_buff
*skb
,
112 struct lwtunnel_state
*lwt
)
114 struct xfrm_md_info
*info
= lwt_xfrm_info(lwt
);
116 if (nla_put_u32(skb
, LWT_XFRM_IF_ID
, info
->if_id
) ||
117 (info
->link
&& nla_put_u32(skb
, LWT_XFRM_LINK
, info
->link
)))
123 static int xfrmi_encap_nlsize(struct lwtunnel_state
*lwtstate
)
125 return nla_total_size(sizeof(u32
)) + /* LWT_XFRM_IF_ID */
126 nla_total_size(sizeof(u32
)); /* LWT_XFRM_LINK */
129 static int xfrmi_encap_cmp(struct lwtunnel_state
*a
, struct lwtunnel_state
*b
)
131 struct xfrm_md_info
*a_info
= lwt_xfrm_info(a
);
132 struct xfrm_md_info
*b_info
= lwt_xfrm_info(b
);
134 return memcmp(a_info
, b_info
, sizeof(*a_info
));
137 static const struct lwtunnel_encap_ops xfrmi_encap_ops
= {
138 .build_state
= xfrmi_build_state
,
139 .destroy_state
= xfrmi_destroy_state
,
140 .fill_encap
= xfrmi_fill_encap_info
,
141 .get_encap_size
= xfrmi_encap_nlsize
,
142 .cmp_encap
= xfrmi_encap_cmp
,
143 .owner
= THIS_MODULE
,
146 #define for_each_xfrmi_rcu(start, xi) \
147 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
149 static u32
xfrmi_hash(u32 if_id
)
151 return hash_32(if_id
, XFRMI_HASH_BITS
);
154 static struct xfrm_if
*xfrmi_lookup(struct net
*net
, struct xfrm_state
*x
)
156 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
159 for_each_xfrmi_rcu(xfrmn
->xfrmi
[xfrmi_hash(x
->if_id
)], xi
) {
160 if (x
->if_id
== xi
->p
.if_id
&&
161 (xi
->dev
->flags
& IFF_UP
))
165 xi
= rcu_dereference(xfrmn
->collect_md_xfrmi
);
166 if (xi
&& (xi
->dev
->flags
& IFF_UP
))
172 static bool xfrmi_decode_session(struct sk_buff
*skb
,
173 unsigned short family
,
174 struct xfrm_if_decode_session_result
*res
)
176 struct net_device
*dev
;
180 if (!secpath_exists(skb
) || !skb
->dev
)
185 ifindex
= inet6_sdif(skb
);
188 ifindex
= inet_sdif(skb
);
193 struct net
*net
= xs_net(xfrm_input_state(skb
));
195 dev
= dev_get_by_index_rcu(net
, ifindex
);
200 if (!dev
|| !(dev
->flags
& IFF_UP
))
202 if (dev
->netdev_ops
!= &xfrmi_netdev_ops
)
205 xi
= netdev_priv(dev
);
208 if (xi
->p
.collect_md
)
209 res
->if_id
= xfrm_input_state(skb
)->if_id
;
211 res
->if_id
= xi
->p
.if_id
;
215 static void xfrmi_link(struct xfrmi_net
*xfrmn
, struct xfrm_if
*xi
)
217 struct xfrm_if __rcu
**xip
= &xfrmn
->xfrmi
[xfrmi_hash(xi
->p
.if_id
)];
219 rcu_assign_pointer(xi
->next
, rtnl_dereference(*xip
));
220 rcu_assign_pointer(*xip
, xi
);
223 static void xfrmi_unlink(struct xfrmi_net
*xfrmn
, struct xfrm_if
*xi
)
225 struct xfrm_if __rcu
**xip
;
226 struct xfrm_if
*iter
;
228 for (xip
= &xfrmn
->xfrmi
[xfrmi_hash(xi
->p
.if_id
)];
229 (iter
= rtnl_dereference(*xip
)) != NULL
;
232 rcu_assign_pointer(*xip
, xi
->next
);
238 static void xfrmi_dev_free(struct net_device
*dev
)
240 struct xfrm_if
*xi
= netdev_priv(dev
);
242 gro_cells_destroy(&xi
->gro_cells
);
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_input(struct sk_buff
*skb
, int nexthdr
, __be32 spi
,
314 int encap_type
, unsigned short family
)
318 sp
= skb_sec_path(skb
);
319 if (sp
&& (sp
->len
|| sp
->olen
) &&
320 !xfrm_policy_check(NULL
, XFRM_POLICY_IN
, skb
, family
))
323 XFRM_SPI_SKB_CB(skb
)->family
= family
;
324 if (family
== AF_INET
) {
325 XFRM_SPI_SKB_CB(skb
)->daddroff
= offsetof(struct iphdr
, daddr
);
326 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
328 XFRM_SPI_SKB_CB(skb
)->daddroff
= offsetof(struct ipv6hdr
, daddr
);
329 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip6
= NULL
;
332 return xfrm_input(skb
, nexthdr
, spi
, encap_type
);
338 static int xfrmi4_rcv(struct sk_buff
*skb
)
340 return xfrmi_input(skb
, ip_hdr(skb
)->protocol
, 0, 0, AF_INET
);
343 static int xfrmi6_rcv(struct sk_buff
*skb
)
345 return xfrmi_input(skb
, skb_network_header(skb
)[IP6CB(skb
)->nhoff
],
349 static int xfrmi4_input(struct sk_buff
*skb
, int nexthdr
, __be32 spi
, int encap_type
)
351 return xfrmi_input(skb
, nexthdr
, spi
, encap_type
, AF_INET
);
354 static int xfrmi6_input(struct sk_buff
*skb
, int nexthdr
, __be32 spi
, int encap_type
)
356 return xfrmi_input(skb
, nexthdr
, spi
, encap_type
, AF_INET6
);
359 static int xfrmi_rcv_cb(struct sk_buff
*skb
, int err
)
361 const struct xfrm_mode
*inner_mode
;
362 struct net_device
*dev
;
363 struct xfrm_state
*x
;
368 if (err
&& !secpath_exists(skb
))
371 x
= xfrm_input_state(skb
);
373 xi
= xfrmi_lookup(xs_net(x
), x
);
377 link
= skb
->dev
->ifindex
;
382 DEV_STATS_INC(dev
, rx_errors
);
383 DEV_STATS_INC(dev
, rx_dropped
);
388 xnet
= !net_eq(xi
->net
, dev_net(skb
->dev
));
391 inner_mode
= &x
->inner_mode
;
393 if (x
->sel
.family
== AF_UNSPEC
) {
394 inner_mode
= xfrm_ip2inner_mode(x
, XFRM_MODE_SKB_CB(skb
)->protocol
);
395 if (inner_mode
== NULL
) {
396 XFRM_INC_STATS(dev_net(skb
->dev
),
397 LINUX_MIB_XFRMINSTATEMODEERROR
);
402 if (!xfrm_policy_check(NULL
, XFRM_POLICY_IN
, skb
,
407 xfrmi_scrub_packet(skb
, xnet
);
408 if (xi
->p
.collect_md
) {
409 struct metadata_dst
*md_dst
;
411 md_dst
= metadata_dst_alloc(0, METADATA_XFRM
, GFP_ATOMIC
);
415 md_dst
->u
.xfrm_info
.if_id
= x
->if_id
;
416 md_dst
->u
.xfrm_info
.link
= link
;
417 skb_dst_set(skb
, (struct dst_entry
*)md_dst
);
419 dev_sw_netstats_rx_add(dev
, skb
->len
);
425 xfrmi_xmit2(struct sk_buff
*skb
, struct net_device
*dev
, struct flowi
*fl
)
427 struct xfrm_if
*xi
= netdev_priv(dev
);
428 struct dst_entry
*dst
= skb_dst(skb
);
429 unsigned int length
= skb
->len
;
430 struct net_device
*tdev
;
431 struct xfrm_state
*x
;
436 if (xi
->p
.collect_md
) {
437 struct xfrm_md_info
*md_info
= skb_xfrm_md_info(skb
);
439 if (unlikely(!md_info
))
442 if_id
= md_info
->if_id
;
443 fl
->flowi_oif
= md_info
->link
;
444 if (md_info
->dst_orig
) {
445 struct dst_entry
*tmp_dst
= dst
;
447 dst
= md_info
->dst_orig
;
448 skb_dst_set(skb
, dst
);
449 md_info
->dst_orig
= NULL
;
450 dst_release(tmp_dst
);
457 dst
= xfrm_lookup_with_ifid(xi
->net
, dst
, fl
, NULL
, 0, if_id
);
461 goto tx_err_link_failure
;
466 goto tx_err_link_failure
;
468 if (x
->if_id
!= if_id
)
469 goto tx_err_link_failure
;
474 DEV_STATS_INC(dev
, collisions
);
475 net_warn_ratelimited("%s: Local routing loop detected!\n",
477 goto tx_err_dst_release
;
481 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
482 (skb_is_gso(skb
) && !skb_gso_validate_network_len(skb
, mtu
))) {
483 skb_dst_update_pmtu_no_confirm(skb
, mtu
);
485 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
486 if (mtu
< IPV6_MIN_MTU
)
490 icmpv6_ndo_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
494 if (!(ip_hdr(skb
)->frag_off
& htons(IP_DF
)))
496 icmp_ndo_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
505 xfrmi_scrub_packet(skb
, !net_eq(xi
->net
, dev_net(dev
)));
506 skb_dst_set(skb
, dst
);
509 err
= dst_output(xi
->net
, skb
->sk
, skb
);
510 if (net_xmit_eval(err
) == 0) {
511 dev_sw_netstats_tx_add(dev
, 1, length
);
513 DEV_STATS_INC(dev
, tx_errors
);
514 DEV_STATS_INC(dev
, tx_aborted_errors
);
519 DEV_STATS_INC(dev
, tx_carrier_errors
);
520 dst_link_failure(skb
);
526 static netdev_tx_t
xfrmi_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
528 struct xfrm_if
*xi
= netdev_priv(dev
);
529 struct dst_entry
*dst
= skb_dst(skb
);
533 memset(&fl
, 0, sizeof(fl
));
535 switch (skb
->protocol
) {
536 case htons(ETH_P_IPV6
):
537 memset(IP6CB(skb
), 0, sizeof(*IP6CB(skb
)));
538 xfrm_decode_session(dev_net(dev
), skb
, &fl
, AF_INET6
);
540 fl
.u
.ip6
.flowi6_oif
= dev
->ifindex
;
541 fl
.u
.ip6
.flowi6_flags
|= FLOWI_FLAG_ANYSRC
;
542 dst
= ip6_route_output(dev_net(dev
), NULL
, &fl
.u
.ip6
);
545 DEV_STATS_INC(dev
, tx_carrier_errors
);
548 skb_dst_set(skb
, dst
);
551 case htons(ETH_P_IP
):
552 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
553 xfrm_decode_session(dev_net(dev
), skb
, &fl
, AF_INET
);
557 fl
.u
.ip4
.flowi4_oif
= dev
->ifindex
;
558 fl
.u
.ip4
.flowi4_flags
|= FLOWI_FLAG_ANYSRC
;
559 rt
= __ip_route_output_key(dev_net(dev
), &fl
.u
.ip4
);
561 DEV_STATS_INC(dev
, tx_carrier_errors
);
564 skb_dst_set(skb
, &rt
->dst
);
571 fl
.flowi_oif
= xi
->p
.link
;
573 ret
= xfrmi_xmit2(skb
, dev
, &fl
);
580 DEV_STATS_INC(dev
, tx_errors
);
581 DEV_STATS_INC(dev
, tx_dropped
);
586 static int xfrmi4_err(struct sk_buff
*skb
, u32 info
)
588 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
589 struct net
*net
= dev_net(skb
->dev
);
590 int protocol
= iph
->protocol
;
591 struct ip_comp_hdr
*ipch
;
592 struct ip_esp_hdr
*esph
;
593 struct ip_auth_hdr
*ah
;
594 struct xfrm_state
*x
;
600 esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
604 ah
= (struct ip_auth_hdr
*)(skb
->data
+(iph
->ihl
<<2));
608 ipch
= (struct ip_comp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
609 spi
= htonl(ntohs(ipch
->cpi
));
615 switch (icmp_hdr(skb
)->type
) {
616 case ICMP_DEST_UNREACH
:
617 if (icmp_hdr(skb
)->code
!= ICMP_FRAG_NEEDED
)
626 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
627 spi
, protocol
, AF_INET
);
631 xi
= xfrmi_lookup(net
, x
);
637 if (icmp_hdr(skb
)->type
== ICMP_DEST_UNREACH
)
638 ipv4_update_pmtu(skb
, net
, info
, 0, protocol
);
640 ipv4_redirect(skb
, net
, 0, protocol
);
646 static int xfrmi6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
647 u8 type
, u8 code
, int offset
, __be32 info
)
649 const struct ipv6hdr
*iph
= (const struct ipv6hdr
*)skb
->data
;
650 struct net
*net
= dev_net(skb
->dev
);
651 int protocol
= iph
->nexthdr
;
652 struct ip_comp_hdr
*ipch
;
653 struct ip_esp_hdr
*esph
;
654 struct ip_auth_hdr
*ah
;
655 struct xfrm_state
*x
;
661 esph
= (struct ip_esp_hdr
*)(skb
->data
+ offset
);
665 ah
= (struct ip_auth_hdr
*)(skb
->data
+ offset
);
669 ipch
= (struct ip_comp_hdr
*)(skb
->data
+ offset
);
670 spi
= htonl(ntohs(ipch
->cpi
));
676 if (type
!= ICMPV6_PKT_TOOBIG
&&
677 type
!= NDISC_REDIRECT
)
680 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
681 spi
, protocol
, AF_INET6
);
685 xi
= xfrmi_lookup(net
, x
);
691 if (type
== NDISC_REDIRECT
)
692 ip6_redirect(skb
, net
, skb
->dev
->ifindex
, 0,
693 sock_net_uid(net
, NULL
));
695 ip6_update_pmtu(skb
, net
, info
, 0, 0, sock_net_uid(net
, NULL
));
701 static int xfrmi_change(struct xfrm_if
*xi
, const struct xfrm_if_parms
*p
)
703 if (xi
->p
.link
!= p
->link
)
706 xi
->p
.if_id
= p
->if_id
;
711 static int xfrmi_update(struct xfrm_if
*xi
, struct xfrm_if_parms
*p
)
713 struct net
*net
= xi
->net
;
714 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
717 xfrmi_unlink(xfrmn
, xi
);
719 err
= xfrmi_change(xi
, p
);
720 xfrmi_link(xfrmn
, xi
);
721 netdev_state_change(xi
->dev
);
725 static int xfrmi_get_iflink(const struct net_device
*dev
)
727 struct xfrm_if
*xi
= netdev_priv(dev
);
729 return READ_ONCE(xi
->p
.link
);
732 static const struct net_device_ops xfrmi_netdev_ops
= {
733 .ndo_init
= xfrmi_dev_init
,
734 .ndo_uninit
= xfrmi_dev_uninit
,
735 .ndo_start_xmit
= xfrmi_xmit
,
736 .ndo_get_stats64
= dev_get_tstats64
,
737 .ndo_get_iflink
= xfrmi_get_iflink
,
740 static void xfrmi_dev_setup(struct net_device
*dev
)
742 dev
->netdev_ops
= &xfrmi_netdev_ops
;
743 dev
->header_ops
= &ip_tunnel_header_ops
;
744 dev
->type
= ARPHRD_NONE
;
745 dev
->mtu
= ETH_DATA_LEN
;
746 dev
->min_mtu
= ETH_MIN_MTU
;
747 dev
->max_mtu
= IP_MAX_MTU
;
748 dev
->flags
= IFF_NOARP
;
749 dev
->needs_free_netdev
= true;
750 dev
->priv_destructor
= xfrmi_dev_free
;
751 dev
->pcpu_stat_type
= NETDEV_PCPU_STAT_TSTATS
;
754 eth_broadcast_addr(dev
->broadcast
);
757 #define XFRMI_FEATURES (NETIF_F_SG | \
759 NETIF_F_GSO_SOFTWARE | \
762 static int xfrmi_dev_init(struct net_device
*dev
)
764 struct xfrm_if
*xi
= netdev_priv(dev
);
765 struct net_device
*phydev
= __dev_get_by_index(xi
->net
, xi
->p
.link
);
768 err
= gro_cells_init(&xi
->gro_cells
, dev
);
773 dev
->features
|= XFRMI_FEATURES
;
774 dev
->hw_features
|= XFRMI_FEATURES
;
777 dev
->needed_headroom
= phydev
->needed_headroom
;
778 dev
->needed_tailroom
= phydev
->needed_tailroom
;
780 if (is_zero_ether_addr(dev
->dev_addr
))
781 eth_hw_addr_inherit(dev
, phydev
);
782 if (is_zero_ether_addr(dev
->broadcast
))
783 memcpy(dev
->broadcast
, phydev
->broadcast
,
786 eth_hw_addr_random(dev
);
787 eth_broadcast_addr(dev
->broadcast
);
793 static int xfrmi_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
794 struct netlink_ext_ack
*extack
)
799 static void xfrmi_netlink_parms(struct nlattr
*data
[],
800 struct xfrm_if_parms
*parms
)
802 memset(parms
, 0, sizeof(*parms
));
807 if (data
[IFLA_XFRM_LINK
])
808 parms
->link
= nla_get_u32(data
[IFLA_XFRM_LINK
]);
810 if (data
[IFLA_XFRM_IF_ID
])
811 parms
->if_id
= nla_get_u32(data
[IFLA_XFRM_IF_ID
]);
813 if (data
[IFLA_XFRM_COLLECT_METADATA
])
814 parms
->collect_md
= true;
817 static int xfrmi_newlink(struct net
*src_net
, struct net_device
*dev
,
818 struct nlattr
*tb
[], struct nlattr
*data
[],
819 struct netlink_ext_ack
*extack
)
821 struct net
*net
= dev_net(dev
);
822 struct xfrm_if_parms p
= {};
826 xfrmi_netlink_parms(data
, &p
);
828 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
830 if (p
.link
|| p
.if_id
) {
831 NL_SET_ERR_MSG(extack
, "link and if_id must be zero");
835 if (rtnl_dereference(xfrmn
->collect_md_xfrmi
))
840 NL_SET_ERR_MSG(extack
, "if_id must be non zero");
844 xi
= xfrmi_locate(net
, &p
);
849 xi
= netdev_priv(dev
);
854 err
= xfrmi_create(dev
);
858 static void xfrmi_dellink(struct net_device
*dev
, struct list_head
*head
)
860 unregister_netdevice_queue(dev
, head
);
863 static int xfrmi_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
864 struct nlattr
*data
[],
865 struct netlink_ext_ack
*extack
)
867 struct xfrm_if
*xi
= netdev_priv(dev
);
868 struct net
*net
= xi
->net
;
869 struct xfrm_if_parms p
= {};
871 xfrmi_netlink_parms(data
, &p
);
873 NL_SET_ERR_MSG(extack
, "if_id must be non zero");
878 NL_SET_ERR_MSG(extack
, "collect_md can't be changed");
882 xi
= xfrmi_locate(net
, &p
);
884 xi
= netdev_priv(dev
);
888 if (xi
->p
.collect_md
) {
889 NL_SET_ERR_MSG(extack
,
890 "device can't be changed to collect_md");
895 return xfrmi_update(xi
, &p
);
898 static size_t xfrmi_get_size(const struct net_device
*dev
)
903 /* IFLA_XFRM_IF_ID */
905 /* IFLA_XFRM_COLLECT_METADATA */
910 static int xfrmi_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
912 struct xfrm_if
*xi
= netdev_priv(dev
);
913 struct xfrm_if_parms
*parm
= &xi
->p
;
915 if (nla_put_u32(skb
, IFLA_XFRM_LINK
, parm
->link
) ||
916 nla_put_u32(skb
, IFLA_XFRM_IF_ID
, parm
->if_id
) ||
917 (xi
->p
.collect_md
&& nla_put_flag(skb
, IFLA_XFRM_COLLECT_METADATA
)))
918 goto nla_put_failure
;
925 static struct net
*xfrmi_get_link_net(const struct net_device
*dev
)
927 struct xfrm_if
*xi
= netdev_priv(dev
);
929 return READ_ONCE(xi
->net
);
932 static const struct nla_policy xfrmi_policy
[IFLA_XFRM_MAX
+ 1] = {
933 [IFLA_XFRM_UNSPEC
] = { .strict_start_type
= IFLA_XFRM_COLLECT_METADATA
},
934 [IFLA_XFRM_LINK
] = { .type
= NLA_U32
},
935 [IFLA_XFRM_IF_ID
] = { .type
= NLA_U32
},
936 [IFLA_XFRM_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
939 static struct rtnl_link_ops xfrmi_link_ops __read_mostly
= {
941 .maxtype
= IFLA_XFRM_MAX
,
942 .policy
= xfrmi_policy
,
943 .priv_size
= sizeof(struct xfrm_if
),
944 .setup
= xfrmi_dev_setup
,
945 .validate
= xfrmi_validate
,
946 .newlink
= xfrmi_newlink
,
947 .dellink
= xfrmi_dellink
,
948 .changelink
= xfrmi_changelink
,
949 .get_size
= xfrmi_get_size
,
950 .fill_info
= xfrmi_fill_info
,
951 .get_link_net
= xfrmi_get_link_net
,
954 static void __net_exit
xfrmi_exit_batch_rtnl(struct list_head
*net_exit_list
,
955 struct list_head
*dev_to_kill
)
960 list_for_each_entry(net
, net_exit_list
, exit_list
) {
961 struct xfrmi_net
*xfrmn
= net_generic(net
, xfrmi_net_id
);
962 struct xfrm_if __rcu
**xip
;
966 for (i
= 0; i
< XFRMI_HASH_SIZE
; i
++) {
967 for (xip
= &xfrmn
->xfrmi
[i
];
968 (xi
= rtnl_dereference(*xip
)) != NULL
;
970 unregister_netdevice_queue(xi
->dev
, dev_to_kill
);
972 xi
= rtnl_dereference(xfrmn
->collect_md_xfrmi
);
974 unregister_netdevice_queue(xi
->dev
, dev_to_kill
);
978 static struct pernet_operations xfrmi_net_ops
= {
979 .exit_batch_rtnl
= xfrmi_exit_batch_rtnl
,
981 .size
= sizeof(struct xfrmi_net
),
984 static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly
= {
985 .handler
= xfrmi6_rcv
,
986 .input_handler
= xfrmi6_input
,
987 .cb_handler
= xfrmi_rcv_cb
,
988 .err_handler
= xfrmi6_err
,
992 static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly
= {
993 .handler
= xfrm6_rcv
,
994 .input_handler
= xfrm_input
,
995 .cb_handler
= xfrmi_rcv_cb
,
996 .err_handler
= xfrmi6_err
,
1000 static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly
= {
1001 .handler
= xfrm6_rcv
,
1002 .input_handler
= xfrm_input
,
1003 .cb_handler
= xfrmi_rcv_cb
,
1004 .err_handler
= xfrmi6_err
,
1008 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1009 static int xfrmi6_rcv_tunnel(struct sk_buff
*skb
)
1011 const xfrm_address_t
*saddr
;
1014 saddr
= (const xfrm_address_t
*)&ipv6_hdr(skb
)->saddr
;
1015 spi
= xfrm6_tunnel_spi_lookup(dev_net(skb
->dev
), saddr
);
1017 return xfrm6_rcv_spi(skb
, IPPROTO_IPV6
, spi
, NULL
);
1020 static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly
= {
1021 .handler
= xfrmi6_rcv_tunnel
,
1022 .cb_handler
= xfrmi_rcv_cb
,
1023 .err_handler
= xfrmi6_err
,
1027 static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly
= {
1028 .handler
= xfrmi6_rcv_tunnel
,
1029 .cb_handler
= xfrmi_rcv_cb
,
1030 .err_handler
= xfrmi6_err
,
1035 static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly
= {
1036 .handler
= xfrmi4_rcv
,
1037 .input_handler
= xfrmi4_input
,
1038 .cb_handler
= xfrmi_rcv_cb
,
1039 .err_handler
= xfrmi4_err
,
1043 static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly
= {
1044 .handler
= xfrm4_rcv
,
1045 .input_handler
= xfrm_input
,
1046 .cb_handler
= xfrmi_rcv_cb
,
1047 .err_handler
= xfrmi4_err
,
1051 static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly
= {
1052 .handler
= xfrm4_rcv
,
1053 .input_handler
= xfrm_input
,
1054 .cb_handler
= xfrmi_rcv_cb
,
1055 .err_handler
= xfrmi4_err
,
1059 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1060 static int xfrmi4_rcv_tunnel(struct sk_buff
*skb
)
1062 return xfrm4_rcv_spi(skb
, IPPROTO_IPIP
, ip_hdr(skb
)->saddr
);
1065 static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly
= {
1066 .handler
= xfrmi4_rcv_tunnel
,
1067 .cb_handler
= xfrmi_rcv_cb
,
1068 .err_handler
= xfrmi4_err
,
1072 static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly
= {
1073 .handler
= xfrmi4_rcv_tunnel
,
1074 .cb_handler
= xfrmi_rcv_cb
,
1075 .err_handler
= xfrmi4_err
,
1080 static int __init
xfrmi4_init(void)
1084 err
= xfrm4_protocol_register(&xfrmi_esp4_protocol
, IPPROTO_ESP
);
1086 goto xfrm_proto_esp_failed
;
1087 err
= xfrm4_protocol_register(&xfrmi_ah4_protocol
, IPPROTO_AH
);
1089 goto xfrm_proto_ah_failed
;
1090 err
= xfrm4_protocol_register(&xfrmi_ipcomp4_protocol
, IPPROTO_COMP
);
1092 goto xfrm_proto_comp_failed
;
1093 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1094 err
= xfrm4_tunnel_register(&xfrmi_ipip_handler
, AF_INET
);
1096 goto xfrm_tunnel_ipip_failed
;
1097 err
= xfrm4_tunnel_register(&xfrmi_ipip6_handler
, AF_INET6
);
1099 goto xfrm_tunnel_ipip6_failed
;
1104 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1105 xfrm_tunnel_ipip6_failed
:
1106 xfrm4_tunnel_deregister(&xfrmi_ipip_handler
, AF_INET
);
1107 xfrm_tunnel_ipip_failed
:
1108 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol
, IPPROTO_COMP
);
1110 xfrm_proto_comp_failed
:
1111 xfrm4_protocol_deregister(&xfrmi_ah4_protocol
, IPPROTO_AH
);
1112 xfrm_proto_ah_failed
:
1113 xfrm4_protocol_deregister(&xfrmi_esp4_protocol
, IPPROTO_ESP
);
1114 xfrm_proto_esp_failed
:
1118 static void xfrmi4_fini(void)
1120 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
1121 xfrm4_tunnel_deregister(&xfrmi_ipip6_handler
, AF_INET6
);
1122 xfrm4_tunnel_deregister(&xfrmi_ipip_handler
, AF_INET
);
1124 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol
, IPPROTO_COMP
);
1125 xfrm4_protocol_deregister(&xfrmi_ah4_protocol
, IPPROTO_AH
);
1126 xfrm4_protocol_deregister(&xfrmi_esp4_protocol
, IPPROTO_ESP
);
1129 static int __init
xfrmi6_init(void)
1133 err
= xfrm6_protocol_register(&xfrmi_esp6_protocol
, IPPROTO_ESP
);
1135 goto xfrm_proto_esp_failed
;
1136 err
= xfrm6_protocol_register(&xfrmi_ah6_protocol
, IPPROTO_AH
);
1138 goto xfrm_proto_ah_failed
;
1139 err
= xfrm6_protocol_register(&xfrmi_ipcomp6_protocol
, IPPROTO_COMP
);
1141 goto xfrm_proto_comp_failed
;
1142 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1143 err
= xfrm6_tunnel_register(&xfrmi_ipv6_handler
, AF_INET6
);
1145 goto xfrm_tunnel_ipv6_failed
;
1146 err
= xfrm6_tunnel_register(&xfrmi_ip6ip_handler
, AF_INET
);
1148 goto xfrm_tunnel_ip6ip_failed
;
1153 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1154 xfrm_tunnel_ip6ip_failed
:
1155 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler
, AF_INET6
);
1156 xfrm_tunnel_ipv6_failed
:
1157 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol
, IPPROTO_COMP
);
1159 xfrm_proto_comp_failed
:
1160 xfrm6_protocol_deregister(&xfrmi_ah6_protocol
, IPPROTO_AH
);
1161 xfrm_proto_ah_failed
:
1162 xfrm6_protocol_deregister(&xfrmi_esp6_protocol
, IPPROTO_ESP
);
1163 xfrm_proto_esp_failed
:
1167 static void xfrmi6_fini(void)
1169 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
1170 xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler
, AF_INET
);
1171 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler
, AF_INET6
);
1173 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol
, IPPROTO_COMP
);
1174 xfrm6_protocol_deregister(&xfrmi_ah6_protocol
, IPPROTO_AH
);
1175 xfrm6_protocol_deregister(&xfrmi_esp6_protocol
, IPPROTO_ESP
);
1178 static const struct xfrm_if_cb xfrm_if_cb
= {
1179 .decode_session
= xfrmi_decode_session
,
1182 static int __init
xfrmi_init(void)
1187 pr_info("IPsec XFRM device driver\n");
1189 msg
= "tunnel device";
1190 err
= register_pernet_device(&xfrmi_net_ops
);
1192 goto pernet_dev_failed
;
1194 msg
= "xfrm4 protocols";
1195 err
= xfrmi4_init();
1199 msg
= "xfrm6 protocols";
1200 err
= xfrmi6_init();
1205 msg
= "netlink interface";
1206 err
= rtnl_link_register(&xfrmi_link_ops
);
1208 goto rtnl_link_failed
;
1210 err
= register_xfrm_interface_bpf();
1214 lwtunnel_encap_add_ops(&xfrmi_encap_ops
, LWTUNNEL_ENCAP_XFRM
);
1216 xfrm_if_register_cb(&xfrm_if_cb
);
1221 rtnl_link_unregister(&xfrmi_link_ops
);
1227 unregister_pernet_device(&xfrmi_net_ops
);
1229 pr_err("xfrmi init: failed to register %s\n", msg
);
1233 static void __exit
xfrmi_fini(void)
1235 xfrm_if_unregister_cb();
1236 lwtunnel_encap_del_ops(&xfrmi_encap_ops
, LWTUNNEL_ENCAP_XFRM
);
1237 rtnl_link_unregister(&xfrmi_link_ops
);
1240 unregister_pernet_device(&xfrmi_net_ops
);
1243 module_init(xfrmi_init
);
1244 module_exit(xfrmi_fini
);
1245 MODULE_LICENSE("GPL");
1246 MODULE_ALIAS_RTNL_LINK("xfrm");
1247 MODULE_ALIAS_NETDEV("xfrm0");
1248 MODULE_AUTHOR("Steffen Klassert");
1249 MODULE_DESCRIPTION("XFRM virtual interface");