pull request (net-next): ipsec-next 2024-07-13
Subsystems (4)
Steffen Klassert
maintainercover pull request (net-next): ipsec-next 2024-07-13
1) Support sending NAT keepalives in ESP in UDP states. Userspace IKE daemon had to do this before, but the kernel can better keep track of it.
From Eyal Birger.
2) Support IPsec crypto offload for IPv6 ESP and IPv4 UDP-encapsulated ESP data paths. Currently, IPsec crypto offload is enabled for GRO code path only. This patchset support UDP encapsulation for the non GRO path. From Mike Yu.
Please pull or let me know if there are problems.
Thanks!
The following changes since commit 5233a55a5254ea38dcdd8d836a0f9ee886c3df51:
mISDN: remove unused struct 'bf_ctx' (2024-05-27 16:48:00 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git tags/ipsec-next-2024-07-13
for you to fetch changes up to d5b60c6517d227b044674718a993caae19080f7b:
Merge branch 'Support IPsec crypto offload for IPv6 ESP and IPv4 UDP-encapsulated ESP data paths' (2024-07-13 11:14:04 +0200)
----------------------------------------------------------------
ipsec-next-2024-07-13
----------------------------------------------------------------
Eyal Birger (1):
xfrm: support sending NAT keepalives in ESP in UDP states
Mike Yu (4):
xfrm: Support crypto offload for inbound IPv6 ESP packets not in GRO path
xfrm: Allow UDP encapsulation in crypto offload control path
xfrm: Support crypto offload for inbound IPv4 UDP-encapsulated ESP packet
xfrm: Support crypto offload for outbound IPv4 UDP-encapsulated ESP packet
Steffen Klassert (1):
Merge branch 'Support IPsec crypto offload for IPv6 ESP and IPv4 UDP-encapsulated ESP data paths'
include/net/ipv6_stubs.h | 3 + include/net/netns/xfrm.h | 1 + include/net/xfrm.h | 10 ++ include/uapi/linux/xfrm.h | 1 + net/ipv4/esp4.c | 8 +- net/ipv4/esp4_offload.c | 17 ++- net/ipv6/af_inet6.c | 1 + net/ipv6/xfrm6_policy.c | 7 + net/xfrm/Makefile | 3 +- net/xfrm/xfrm_compat.c | 6 +- net/xfrm/xfrm_device.c | 6 +- net/xfrm/xfrm_input.c | 3 +- net/xfrm/xfrm_nat_keepalive.c | 292 ++++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_policy.c | 13 +- net/xfrm/xfrm_state.c | 17 +++ net/xfrm/xfrm_user.c | 15 +++ 16 files changed, 393 insertions(+), 10 deletions(-) create mode 100644 net/xfrm/xfrm_nat_keepalive.c
Steffen Klassert
maintainerPATCH 1/5 xfrm: support sending NAT keepalives in ESP in UDP states
From: Eyal Birger <[email protected]>
Add the ability to send out RFC-3948 NAT keepalives from the xfrm stack.
To use, Userspace sets an XFRM_NAT_KEEPALIVE_INTERVAL integer property when creating XFRM outbound states which denotes the number of seconds between keepalive messages.
Keepalive messages are sent from a per net delayed work which iterates over the xfrm states. The logic is guarded by the xfrm state spinlock due to the xfrm state walk iterator.
Possible future enhancements:
- Adding counters to keep track of sent keepalives.
- deduplicate NAT keepalives between states sharing the same nat keepalive parameters.
- provisioning hardware offloads for devices capable of implementing this.
- revise xfrm state list to use an rcu list in order to avoid running this under spinlock.
--- include/net/ipv6_stubs.h | 3 + include/net/netns/xfrm.h | 1 + include/net/xfrm.h | 10 ++ include/uapi/linux/xfrm.h | 1 + net/ipv6/af_inet6.c | 1 + net/ipv6/xfrm6_policy.c | 7 + net/xfrm/Makefile | 3 +- net/xfrm/xfrm_compat.c | 6 +- net/xfrm/xfrm_nat_keepalive.c | 292 ++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_policy.c | 8 + net/xfrm/xfrm_state.c | 17 ++ net/xfrm/xfrm_user.c | 15 ++ 12 files changed, 361 insertions(+), 3 deletions(-) create mode 100644 net/xfrm/xfrm_nat_keepalive.c
include/net/ipv6_stubs.h +3 -0
@@ -9,6 +9,7 @@#include <net/flow.h> #include <net/neighbour.h> #include <net/sock.h> +#include <net/ipv6.h> /* structs from net/ip6_fib.h */ struct fib6_info ;
@@ -72,6 +73,8 @@ struct ipv6_stub { int (*output)(struct net *, struct sock *, struct sk_buff *));struct net_device *(*ipv6_dev_find)(struct net *net,const struct in6_addr *addr,struct net_device *dev); +int (*ip6_xmit)(const struct sock *sk,struct sk_buff *skb,struct flowi6 *fl6, +__u32 mark,struct ipv6_txoptions *opt,int tclass,u32 priority); };extern const struct ipv6_stub *ipv6_stub__read_mostly ;
include/net/netns/xfrm.h +1 -0
@@ -83,6 +83,7 @@ struct netns_xfrm {spinlock_t xfrm_policy_lock;struct mutex xfrm_cfg_mutex; + struct delayed_work nat_keepalive_work; };#endif
include/net/xfrm.h +10 -0
@@ -229,6 +229,10 @@ struct xfrm_state {struct xfrm_encap_tmpl *encap;struct sock __rcu *encap_sk; +/* NAT keepalive */ +u32 nat_keepalive_interval;/* seconds */ +time64_t nat_keepalive_expiration; +/* Data for care-of address */ xfrm_address_t *coaddr;
@@ -2203,4 +2207,10 @@ static inline int register_xfrm_state_bpf(void) }#endif +int xfrm_nat_keepalive_init (unsigned short family); +void xfrm_nat_keepalive_fini (unsigned short family); +int xfrm_nat_keepalive_net_init (struct net *net); +int xfrm_nat_keepalive_net_fini (struct net *net); +void xfrm_nat_keepalive_state_updated (struct xfrm_state *x); +#endif /* _NET_XFRM_H */
include/uapi/linux/xfrm.h +1 -0
@@ -321,6 +321,7 @@ enum xfrm_attr_type_t {XFRMA_IF_ID ,/* __u32 */ XFRMA_MTIMER_THRESH ,/* __u32 in seconds for input SA */ XFRMA_SA_DIR ,/* __u8 */ +XFRMA_NAT_KEEPALIVE_INTERVAL ,/* __u32 in seconds for NAT keepalive */ __XFRMA_MAX#define XFRMA_OUTPUT_MARK XFRMA_SET_MARK/* Compatibility */
net/ipv6/af_inet6.c +1 -0
@@ -1060,6 +1060,7 @@ static const struct ipv6_stub ipv6_stub_impl = { .nd_tbl = &nd_tbl, .ipv6_fragment = ip6_fragment, .ipv6_dev_find = ipv6_dev_find, + .ip6_xmit = ip6_xmit, };static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
net/ipv6/xfrm6_policy.c +7 -0
@@ -284,8 +284,14 @@ int __init xfrm6_init(void) ret =register_pernet_subsys (&xfrm6_net_ops);if (ret) goto out_protocol; + + ret =xfrm_nat_keepalive_init (AF_INET6 ); +if (ret) + goto out_nat_keepalive; out:return ret; +out_nat_keepalive: +unregister_pernet_subsys (&xfrm6_net_ops); out_protocol:xfrm6_protocol_fini (); out_state:
@@ -297,6 +303,7 @@ int __init xfrm6_init(void)void xfrm6_fini (void ) { +xfrm_nat_keepalive_fini (AF_INET6 );unregister_pernet_subsys (&xfrm6_net_ops);xfrm6_protocol_fini ();xfrm6_policy_fini ();
net/xfrm/Makefile +2 -1
@@ -13,7 +13,8 @@ endif obj-$(CONFIG_XFRM) := xfrm_policy.o xfrm_state.o xfrm_hash.o \ xfrm_input.o xfrm_output.o \ - xfrm_sysctl.o xfrm_replay.o xfrm_device.o + xfrm_sysctl.o xfrm_replay.o xfrm_device.o \ + xfrm_nat_keepalive.o obj-$(CONFIG_XFRM_STATISTICS) += xfrm_proc.o obj-$(CONFIG_XFRM_ALGO) += xfrm_algo.o obj-$(CONFIG_XFRM_USER) += xfrm_user.o
net/xfrm/xfrm_compat.c +4 -2
@@ -131,6 +131,7 @@ static const struct nla_policy compat_policy[XFRMA_MAX+1] = { [XFRMA_IF_ID] = { .type = NLA_U32 }, [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 }, [XFRMA_SA_DIR] = NLA_POLICY_RANGE(NLA_U8, XFRM_SA_DIR_IN, XFRM_SA_DIR_OUT), + [XFRMA_NAT_KEEPALIVE_INTERVAL] = { .type = NLA_U32 }, }; static struct nlmsghdr *xfrm_nlmsg_put_compat(struct sk_buff *skb,
@@ -280,9 +281,10 @@ static int xfrm_xlate64_attr(struct sk_buff *dst, const struct nlattr *src)case XFRMA_IF_ID :case XFRMA_MTIMER_THRESH :case XFRMA_SA_DIR : +case XFRMA_NAT_KEEPALIVE_INTERVAL :return xfrm_nla_cpy (dst, src,nla_len (src));default : -BUILD_BUG_ON (XFRMA_MAX !=XFRMA_SA_DIR ); +BUILD_BUG_ON (XFRMA_MAX !=XFRMA_NAT_KEEPALIVE_INTERVAL );pr_warn_once ("unsupported nla_type %d\n" , src->nla_type);return -EOPNOTSUPP ; }
@@ -437,7 +439,7 @@ static int xfrm_xlate32_attr(void *dst, const struct nlattr *nla,int err;if (type >XFRMA_MAX ) { -BUILD_BUG_ON (XFRMA_MAX !=XFRMA_SA_DIR ); +BUILD_BUG_ON (XFRMA_MAX !=XFRMA_NAT_KEEPALIVE_INTERVAL );NL_SET_ERR_MSG (extack,"Bad attribute" );return -EOPNOTSUPP ; }
net/xfrm/xfrm_nat_keepalive.c new file +292 -0
292 lines not on this page · raw message
net/xfrm/xfrm_policy.c +8 -0
@@ -4289,8 +4289,14 @@ static int __net_init xfrm_net_init(struct net *net)if (rv <0 ) goto out_sysctl; + rv =xfrm_nat_keepalive_net_init (net); +if (rv <0 ) + goto out_nat_keepalive; +return 0 ; +out_nat_keepalive: +xfrm_sysctl_fini (net); out_sysctl:xfrm_policy_fini (net); out_policy:
@@ -4303,6 +4309,7 @@ static int __net_init xfrm_net_init(struct net *net) static void __net_exit xfrm_net_exit(struct net *net) { +xfrm_nat_keepalive_net_fini (net);xfrm_sysctl_fini (net);xfrm_policy_fini (net);xfrm_state_fini (net);
@@ -4364,6 +4371,7 @@ void __init xfrm_init(void)#endif register_xfrm_state_bpf (); +xfrm_nat_keepalive_init (AF_INET ); }#ifdef CONFIG_AUDITSYSCALL
net/xfrm/xfrm_state.c +17 -0
@@ -715,6 +715,7 @@ int __xfrm_state_delete(struct xfrm_state *x)if (x->id.spi)hlist_del_rcu (&x->byspi); net->xfrm.state_num--; +xfrm_nat_keepalive_state_updated (x);spin_unlock (&net->xfrm.xfrm_state_lock);if (x->encap_sk)
@@ -1453,6 +1454,7 @@ static void __xfrm_state_insert(struct xfrm_state *x) net->xfrm.state_num++;xfrm_hash_grow_check (net, x->bydst.next !=NULL ); +xfrm_nat_keepalive_state_updated (x); }/* net->xfrm.xfrm_state_lock is held */
@@ -2871,6 +2873,21 @@ int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload, goto error; } +if (x->nat_keepalive_interval) { +if (x->dir !=XFRM_SA_DIR_OUT ) { +NL_SET_ERR_MSG (extack,"NAT keepalive is only supported for outbound SAs" ); + err = -EINVAL ; + goto error; + } + +if (!x->encap || x->encap->encap_type !=UDP_ENCAP_ESPINUDP ) { +NL_SET_ERR_MSG (extack, +"NAT keepalive is only supported for UDP encapsulation" ); + err = -EINVAL ; + goto error; + } + } + error:return err; }
net/xfrm/xfrm_user.c +15 -0
@@ -833,6 +833,10 @@ static struct xfrm_state *xfrm_state_construct(struct net *net,if (attrs[XFRMA_SA_DIR ]) x->dir =nla_get_u8 (attrs[XFRMA_SA_DIR ]); +if (attrs[XFRMA_NAT_KEEPALIVE_INTERVAL ]) + x->nat_keepalive_interval = +nla_get_u32 (attrs[XFRMA_NAT_KEEPALIVE_INTERVAL ]); + err =__xfrm_init_state (x, false, attrs[XFRMA_OFFLOAD_DEV ], extack);if (err) goto error;
@@ -1288,6 +1292,13 @@ static int copy_to_user_state_extra(struct xfrm_state *x, }if (x->dir) ret =nla_put_u8 (skb,XFRMA_SA_DIR , x->dir); + +if (x->nat_keepalive_interval) { + ret =nla_put_u32 (skb,XFRMA_NAT_KEEPALIVE_INTERVAL , + x->nat_keepalive_interval); +if (ret) + goto out; + } out:return ret; }
@@ -3165,6 +3176,7 @@ const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { [XFRMA_IF_ID] = { .type = NLA_U32 }, [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 }, [XFRMA_SA_DIR] = NLA_POLICY_RANGE(NLA_U8, XFRM_SA_DIR_IN, XFRM_SA_DIR_OUT), + [XFRMA_NAT_KEEPALIVE_INTERVAL ] = { .type =NLA_U32 }, };EXPORT_SYMBOL_GPL (xfrma_policy );
@@ -3474,6 +3486,9 @@ static inline unsigned int xfrm_sa_len(struct xfrm_state *x)if (x->dir) l +=nla_total_size (sizeof (x->dir)); +if (x->nat_keepalive_interval) + l +=nla_total_size (sizeof (x->nat_keepalive_interval)); +return l; }
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 2/5 xfrm: Support crypto offload for inbound IPv6 ESP packets not in GRO path
In reply to Steffen KlassertFrom: Mike Yu <[email protected]>
IPsec crypt offload supports outbound IPv6 ESP packets, but it doesn't support inbound IPv6 ESP packets.
This change enables the crypto offload for inbound IPv6 ESP packets that are not handled through GRO code path. If HW drivers add the offload information to the skb, the packet will be handled in the crypto offload rx code path.
Apart from the change in crypto offload rx code path, the change in xfrm_policy_check is also needed.
Exampe of RX data path:
+-----------+ +-------+
| HW Driver |-->| wlan0 |--------+
+-----------+ +-------+ |
v
+---------------+ +------+
+------>| Network Stack |-->| Apps |
| +---------------+ +------+
| |
| v
+--------+ +------------+
| ipsec1 |<--| XFRM Stack |
+--------+ +------------+Test: Enabled both in/out IPsec crypto offload, and verified IPv6
ESP packets on Android device on both wifi/cellular network
--- net/xfrm/xfrm_input.c | 2 +- net/xfrm/xfrm_policy.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-)
net/xfrm/xfrm_input.c +1 -1
@@ -471,7 +471,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)struct xfrm_offload *xo =xfrm_offload (skb);struct sec_path *sp; -if (encap_type <0 || (xo && xo->flags &XFRM_GRO )) { +if (encap_type <0 || (xo && (xo->flags &XFRM_GRO || encap_type ==0 ))) { x =xfrm_input_state (skb);if (unlikely (x->dir && x->dir !=XFRM_SA_DIR_IN )) {
net/xfrm/xfrm_policy.c +4 -1
@@ -3718,12 +3718,15 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, pol =xfrm_in_fwd_icmp (skb, &fl, family, if_id);if (!pol) { +const bool is_crypto_offload = sp && + (xfrm_input_state (skb)->xso.type ==XFRM_DEV_OFFLOAD_CRYPTO ); +if (net->xfrm.policy_default[dir] ==XFRM_USERPOLICY_BLOCK ) {XFRM_INC_STATS (net,LINUX_MIB_XFRMINNOPOLS );return 0 ; } -if (sp &&secpath_has_nontransport (sp,0 , &xerr_idx)) { +if (sp &&secpath_has_nontransport (sp,0 , &xerr_idx) && !is_crypto_offload) {xfrm_secpath_reject (xerr_idx, skb, &fl);XFRM_INC_STATS (net,LINUX_MIB_XFRMINNOPOLS );return 0 ;
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 3/5 xfrm: Allow UDP encapsulation in crypto offload control path
In reply to Steffen KlassertFrom: Mike Yu <[email protected]>
Unblock this limitation so that SAs with encapsulation specified can be passed to HW drivers. HW drivers can still reject the SA in their implementation of xdo_dev_state_add if the encapsulation is not supported.
Test: Verified on Android device
--- net/xfrm/xfrm_device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/xfrm/xfrm_device.c +3 -3
@@ -261,9 +261,9 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, is_packet_offload = xuo->flags &XFRM_OFFLOAD_PACKET ; -/* We don't yet support UDP encapsulation and TFC padding. */ -if ((!is_packet_offload && x->encap) || x->tfcpad) { -NL_SET_ERR_MSG (extack,"Encapsulation and TFC padding can't be offloaded" ); +/* We don't yet support TFC padding. */ +if (x->tfcpad) { +NL_SET_ERR_MSG (extack,"TFC padding can't be offloaded" );return -EINVAL ; }
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 4/5 xfrm: Support crypto offload for inbound IPv4 UDP-encapsulated ESP packet
In reply to Steffen KlassertFrom: Mike Yu <[email protected]>
If xfrm_input() is called with UDP_ENCAP_ESPINUDP, the packet is already processed in UDP layer that removes the UDP header. Therefore, there should be no much difference to treat it as an ESP packet in the XFRM stack.
Test: Enabled dir=in IPsec crypto offload, and verified IPv4
UDP-encapsulated ESP packets on both wifi/cellular network
--- net/xfrm/xfrm_input.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/xfrm/xfrm_input.c +2 -1
@@ -471,7 +471,8 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)struct xfrm_offload *xo =xfrm_offload (skb);struct sec_path *sp; -if (encap_type <0 || (xo && (xo->flags &XFRM_GRO || encap_type ==0 ))) { +if (encap_type <0 || (xo && (xo->flags &XFRM_GRO || encap_type ==0 || + encap_type ==UDP_ENCAP_ESPINUDP ))) { x =xfrm_input_state (skb);if (unlikely (x->dir && x->dir !=XFRM_SA_DIR_IN )) {
Signature · 1 line
2.34.1
Steffen Klassert
maintainerPATCH 5/5 xfrm: Support crypto offload for outbound IPv4 UDP-encapsulated ESP packet
In reply to Steffen KlassertFrom: Mike Yu <[email protected]>
esp_xmit() is already able to handle UDP encapsulation through the call to esp_output_head(). However, the ESP header and the outer IP header are not correct and need to be corrected.
Test: Enabled both dir=in/out IPsec crypto offload, and verified IPv4
UDP-encapsulated ESP packets on both wifi/cellular network
--- net/ipv4/esp4.c | 8 +++++++- net/ipv4/esp4_offload.c | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-)
net/ipv4/esp4.c +7 -1
@@ -349,6 +349,7 @@ static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb, {struct udphdr *uh;unsigned int len; +struct xfrm_offload *xo =xfrm_offload (skb); len = skb->len + esp->tailen -skb_transport_offset (skb);if (len +sizeof (struct iphdr ) >IP_MAX_MTU )
@@ -360,7 +361,12 @@ static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb, uh->len =htons (len); uh->check =0 ; - *skb_mac_header (skb) =IPPROTO_UDP ; +/* For IPv4 ESP with UDP encapsulation, if xo is not null, the skb is in the crypto offload +* data path, which means that esp_output_udp_encap is called outside of the XFRM stack. +* In this case, the mac header doesn't point to the IPv4 protocol field, so don't set it. +*/ +if (!xo || encap_type !=UDP_ENCAP_ESPINUDP ) + *skb_mac_header (skb) =IPPROTO_UDP ;return (struct ip_esp_hdr *)(uh +1 ); }
net/ipv4/esp4_offload.c +16 -1
@@ -264,6 +264,7 @@ static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_struct esp_info esp;bool hw_offload = true;__u32 seq; +int encap_type =0 ; esp.inplace = true;
@@ -296,8 +297,10 @@ static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_ esp.esph =ip_esp_hdr (skb); +if (x->encap) + encap_type = x->encap->encap_type; -if (!hw_offload || !skb_is_gso (skb)) { +if (!hw_offload || !skb_is_gso (skb) || (hw_offload && encap_type ==UDP_ENCAP_ESPINUDP )) { esp.nfrags =esp_output_head (x, skb, &esp);if (esp.nfrags <0 )return esp.nfrags;
@@ -324,6 +327,18 @@ static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_ esp.seqno =cpu_to_be64 (seq + ((u64 )xo->seq.hi <<32 )); +if (hw_offload && encap_type ==UDP_ENCAP_ESPINUDP ) { +/* In the XFRM stack, the encapsulation protocol is set to iphdr->protocol by +* setting *skb_mac_header(skb) (see esp_output_udp_encap()) where skb->mac_header +* points to iphdr->protocol (see xfrm4_tunnel_encap_add()). +* However, in esp_xmit(), skb->mac_header doesn't point to iphdr->protocol. +* Therefore, the protocol field needs to be corrected. +*/ +ip_hdr (skb)->protocol =IPPROTO_UDP ; + + esph->seq_no =htonl (seq); + } +ip_hdr (skb)->tot_len =htons (skb->len);ip_send_check (ip_hdr (skb));
Signature · 1 line
2.34.1
Jakub Kicinski
maintainerQuoted 1 line · On Sat, 13 Jul 2024 12:24:11 +0200 Steffen Klassert wrote:
ipsec-next-2024-07-13
...this too. And I meant to say "pulled" rather than "applied" in the previous email. Thanks!