Configure XFRM encryption using direct netlink requests
v2 cover Configure XFRM encryption using direct netlink requests
Rewrite xfrm encryption setup using direct netlink calls instead of executing the external ip utility. This allows the tests to detect whether the necessary features are supported by the kernel and exit with TCONF if the support is missing. Some kernels lack XFRM support even though kernel config claims the necessary modules should be available.
Martin Doucha (6):
lapi/xfrm.h: Include system XFRM header
xfrm01: Use NETDEV_SET_STATE() to activate loopback
xfrm01: Configure xfrm using direct netlink request lapi/udp.h: Add fallback constant for ESP-in-TCP
xfrm02: Configure xfrm using direct netlink request
xfrm03: Configure xfrm using direct netlink request
include/lapi/udp.h | 4 ++ include/lapi/xfrm.h | 3 + testcases/network/sockets/xfrm01.c | 99 ++++++++++++++++++++---------- testcases/network/sockets/xfrm02.c | 91 +++++++++++++++++++-------- testcases/network/sockets/xfrm03.c | 91 ++++++++++++++++++--------- 5 files changed, 200 insertions(+), 88 deletions(-)
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 1/6 lapi/xfrm.h: Include system XFRM header
Also include <netinet/in.h> first to avoid redefinition issues with IPv6 data structures.
---
Changes since v1: Replaced guard macro #define with #include <netinet/in.h>
include/lapi/xfrm.h | 3 +++ 1 file changed, 3 insertions(+)
include/lapi/xfrm.h +3 -0
@@ -6,6 +6,9 @@#ifndef LAPI_XFRM_H__ #define LAPI_XFRM_H__ +#include <netinet/in.h> +#include <linux/xfrm.h> +#ifndef XFRMNLGRP_NONE # define XFRMNLGRP_NONE 0#endif
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] lapi/xfrm.h: Include system XFRM header
Hi Martin,
Quoted 1 line · Martin Doucha
lapi/xfrm.h: Include system XFRM header
--- [PATCH 2/6] ---
Quoted 3 lines
xfrm01: Use
NETDEV_SET_STATE()to activate loopbackSigned-off-by Martin Doucha <[email protected]>
The body is missing. The subject states what changed, but not why. What is the motivation for switching from tst_cmd() to NETDEV_SET_STATE() here? Patches 3-6 remove the "ip" command dependency entirely, so presumably this is a step toward that goal, but this commit stands alone and should explain the reason.
This patch needs revision.
Pre-existing issues noticed in the surrounding code (not introduced by this patch):
- testcases/network/sockets/xfrm02.c in try_corrupt() -- usleep(30000) used to wait for the espintcp strparser to process buffered data. Ground Rule 2 prohibits sleep-based synchronization. An exponential-backoff polling loop on the processed data would be more robust under load.
- testcases/network/sockets/xfrm03.c in try_corrupt() -- same usleep(30000) for the same reason.
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 2/6 xfrm01: Use NETDEV_SET_STATE() to activate loopback
In reply to Martin Doucha---
Changes since v1: None
testcases/network/sockets/xfrm01.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
testcases/network/sockets/xfrm01.c +2 -8
@@ -27,6 +27,7 @@#include "tst_test.h" #include "tst_net.h" +#include "tst_netdevice.h" #include "lapi/udp.h" #include "lapi/splice.h"
@@ -72,14 +73,7 @@ static void setup(void)int i, ret;tst_setup_netns (); - -const char *const lo_cmd[] = { -"ip" ,"link" ,"set" ,"lo" ,"up" ,NULL - }; - - ret =tst_cmd (lo_cmd,NULL ,NULL ,TST_CMD_PASS_RETVAL ); -if (ret) -tst_brk (TBROK ,"Failed to bring up loopback interface" ); +NETDEV_SET_STATE ("lo" ,1 ); keyhex[0 ] ='0' ; keyhex[1 ] ='x' ;
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 3/6 xfrm01: Configure xfrm using direct netlink request
In reply to Martin DouchaSome kernels may not support ESP-in-UDP even though the necessary modules are enabled in kernel config. Rewrite the xfrm setup using direct netlink request so that the test can check errno and exit with TCONF if the support is missing.
---
Changes since v1: None
testcases/network/sockets/xfrm01.c | 87 +++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 24 deletions(-)
testcases/network/sockets/xfrm01.c +63 -24
@@ -25,8 +25,10 @@#define _GNU_SOURCE +#include "lapi/xfrm.h" #include "tst_test.h" #include "tst_net.h" +#include "tst_netlink.h" #include "tst_netdevice.h" #include "lapi/udp.h" #include "lapi/splice.h"
@@ -44,13 +46,40 @@#define SALT_LEN 4#define KEYTOTAL (AES_KEYLEN + SALT_LEN) -#define XFRM_CMD \ - "ip xfrm state add" \ - " src 127.0.0.1 dst 127.0.0.1" \ - " proto esp spi 0x%08x" \ - " encap espinudp %d %d 0.0.0.0" \ - " aead 'rfc4106(gcm(aes))' %s 128" \ - " replay-window 32" +static struct xfrm_usersa_info xs_payload = { + .family =AF_INET , + .id.proto = (uint8_t )IPPROTO_ESP , + .replay_window =32 , + .reqid =1 , + .lft = { + .soft_byte_limit =XFRM_INF , + .hard_byte_limit =XFRM_INF , + .soft_packet_limit =XFRM_INF , + .hard_packet_limit =XFRM_INF , + }, +}; + +static struct xfrm_encap_tmpl esp_encap = { + .encap_type =UDP_ENCAP_ESPINUDP +}; + +static struct { +struct xfrm_algo_aead alg; +char buf[KEYTOTAL ]; +} aead_alg_info = { + .alg = { + .alg_name ="rfc4106(gcm(aes))" , + .alg_key_len =KEYTOTAL *8 , + .alg_icv_len =ICV_SIZE *8 + } +}; + +static const struct tst_netlink_attr_list alg_config[] = { + {XFRMA_ENCAP , &esp_encap,sizeof (esp_encap),NULL }, + {XFRMA_ALG_AEAD , &aead_alg_info,sizeof (aead_alg_info.alg) +KEYTOTAL , +NULL }, + {0 ,NULL ,-1 ,NULL } +};static const uint8_t original[DATA_SIZE ] = {'T' ,'E' ,'S' ,'T' };
@@ -68,23 +97,37 @@ static int pipefd[2] = { -1, -1 };static void setup (void ) { -char keyhex[KEYTOTAL *2 +3 ]; -char cmd[512 ]; -int i, ret; +int ret; +struct tst_netlink_context *ctx; +struct nlmsghdr header = { + .nlmsg_type =XFRM_MSG_NEWSA , + .nlmsg_flags =NLM_F_REQUEST |NLM_F_CREATE |NLM_F_EXCL | +NLM_F_ACK + };tst_setup_netns ();NETDEV_SET_STATE ("lo" ,1 ); - keyhex[0 ] ='0' ; - keyhex[1 ] ='x' ; -for (i =0 ; i <KEYTOTAL ; i++) -sprintf (keyhex +2 + i *2 ,"%02x" , aead_key[i]); - -snprintf (cmd,sizeof (cmd),XFRM_CMD ,SPI ,ENC_PORT ,ENC_PORT , keyhex); - - ret =tst_system (cmd); -if (ret) -tst_brk (TBROK ,"Failed to install xfrm ESP state" ); +memcpy (aead_alg_info.alg.alg_key, aead_key,KEYTOTAL ); + xs_payload.saddr.a4 =htonl (INADDR_LOOPBACK ); + xs_payload.id.daddr.a4 =htonl (INADDR_LOOPBACK ); + xs_payload.id.spi =htonl (SPI ); + esp_encap.encap_sport =htons (ENC_PORT ); + esp_encap.encap_dport =htons (ENC_PORT ); + + ctx =NETLINK_CREATE_CONTEXT (NETLINK_XFRM ); +NETLINK_ADD_MESSAGE (ctx, &header, &xs_payload,sizeof (xs_payload)); +NETLINK_ADD_ATTR_LIST (ctx, alg_config); + ret =NETLINK_SEND_VALIDATE (ctx); +TST_ERR = tst_netlink_errno; +NETLINK_DESTROY_CONTEXT (ctx); + +if (!ret) { +if (TST_ERR ==EPROTONOSUPPORT ) +tst_brk (TCONF ,"xfrm ESP is not supported by kernel" ); + +tst_brk (TBROK |TTERRNO ,"Failed to install xfrm ESP state" ); + } }static void try_corrupt (void )
@@ -227,10 +270,6 @@ static struct tst_test test = { {"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP}, {} }, - .needs_cmds = (struct tst_cmd[]) { - {.cmd ="ip" }, - {} - }, .tags = (const struct tst_tag[]) { {"linux-git" ,"f4c50a4034e6" }, {"CVE", "2026-43284"},
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 4/6 lapi/udp.h: Add fallback constant for ESP-in-TCP
In reply to Martin DouchaThe system constant is defined in linux/udp.h but not in netinet/udp.h. Define it in lapi/udp.h anyway.
---
Changes since v1: None
include/lapi/udp.h | 4 ++++ 1 file changed, 4 insertions(+)
include/lapi/udp.h +4 -0
@@ -23,4 +23,8 @@# define UDP_ENCAP_ESPINUDP 2#endif +#ifndef TCP_ENCAP_ESPINTCP +# define TCP_ENCAP_ESPINTCP 7 +#endif +#endif /* LAPI_UDP_H__ */
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 5/6 xfrm02: Configure xfrm using direct netlink request
In reply to Martin DouchaSome kernels may not support ESP-in-TCP even though the necessary modules are enabled in kernel config. Rewrite the xfrm setup using direct netlink request so that the test can check errno and exit with TCONF if the support is missing.
---
Changes since v1: Removed .needs_cmds
testcases/network/sockets/xfrm02.c | 91 +++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 27 deletions(-)
testcases/network/sockets/xfrm02.c +64 -27
@@ -26,9 +26,11 @@#define _GNU_SOURCE +#include "lapi/xfrm.h" #include "tst_test.h" #include "tst_net.h" #include "tst_netdevice.h" +#include "lapi/udp.h" #include "lapi/tcp.h" #include "lapi/splice.h"
@@ -46,6 +48,45 @@/* ESP-in-TCP frame prefix: 2-byte length + ESP header */ #define PREFIX_SIZE (2 + ESP_HDR_SIZE) +static struct xfrm_usersa_info xs_payload = { + .family =AF_INET6 , + .mode =XFRM_MODE_TRANSPORT , + .reqid =1 , + .saddr.in6 =IN6ADDR_LOOPBACK_INIT , + .id = { + .proto = (uint8_t )IPPROTO_ESP , + .daddr.in6 =IN6ADDR_LOOPBACK_INIT + }, + .lft = { + .soft_byte_limit =XFRM_INF , + .hard_byte_limit =XFRM_INF , + .soft_packet_limit =XFRM_INF , + .hard_packet_limit =XFRM_INF , + }, +}; + +static struct xfrm_encap_tmpl esp_encap = { + .encap_type =TCP_ENCAP_ESPINTCP +}; + +static struct { +struct xfrm_algo_aead alg; +char buf[KEYTOTAL ]; +} aead_alg_info = { + .alg = { + .alg_name ="rfc4106(gcm(aes))" , + .alg_key_len =KEYTOTAL *8 , + .alg_icv_len =128 + } +}; + +static const struct tst_netlink_attr_list alg_config[] = { + {XFRMA_ENCAP , &esp_encap,sizeof (esp_encap),NULL }, + {XFRMA_ALG_AEAD , &aead_alg_info,sizeof (aead_alg_info.alg) +KEYTOTAL , +NULL }, + {0 ,NULL ,-1 ,NULL } +}; +static const uint8_t aead_key[KEYTOTAL ] = {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff ,
@@ -58,35 +99,35 @@ static int srv_fd = -1;static void setup (void ) { -char keyhex[KEYTOTAL *2 +3 ]; -char spihex[16 ]; -char port_str[8 ];int i, ret; +struct tst_netlink_context *ctx; +struct nlmsghdr header = { + .nlmsg_type =XFRM_MSG_NEWSA , + .nlmsg_flags =NLM_F_REQUEST |NLM_F_CREATE |NLM_F_EXCL | +NLM_F_ACK + };tst_setup_netns ();NETDEV_SET_STATE ("lo" ,1 ); - keyhex[0 ] ='0' ; - keyhex[1 ] ='x' ; -for (i =0 ; i <KEYTOTAL ; i++) -sprintf (keyhex +2 + i *2 ,"%02x" , aead_key[i]); - -snprintf (spihex,sizeof (spihex),"0x%08x" ,SPI ); -snprintf (port_str,sizeof (port_str),"%d" ,TCP_PORT ); - -const char *const xfrm_cmd[] = { -"ip" ,"xfrm" ,"state" ,"add" , -"src" ,"::1" ,"dst" ,"::1" , -"proto" ,"esp" ,"spi" , spihex, -"encap" ,"espintcp" , port_str, port_str,"::" , -"aead" ,"rfc4106(gcm(aes))" , keyhex,"128" , -"mode" ,"transport" , -NULL - }; +memcpy (aead_alg_info.alg.alg_key, aead_key,KEYTOTAL ); + xs_payload.id.spi =htonl (SPI ); + esp_encap.encap_sport =htons (TCP_PORT ); + esp_encap.encap_dport =htons (TCP_PORT ); + + ctx =NETLINK_CREATE_CONTEXT (NETLINK_XFRM ); +NETLINK_ADD_MESSAGE (ctx, &header, &xs_payload,sizeof (xs_payload)); +NETLINK_ADD_ATTR_LIST (ctx, alg_config); + ret =NETLINK_SEND_VALIDATE (ctx); +TST_ERR = tst_netlink_errno; +NETLINK_DESTROY_CONTEXT (ctx); - ret =tst_cmd (xfrm_cmd,NULL ,NULL ,TST_CMD_PASS_RETVAL ); -if (ret) -tst_brk (TBROK ,"Failed to install xfrm ESP-in-TCP state" ); +if (!ret) { +if (TST_ERR ==EPROTONOSUPPORT ) +tst_brk (TCONF ,"xfrm ESP is not supported by kernel" ); + +tst_brk (TBROK |TTERRNO ,"Failed to install xfrm ESP state" ); + }for (i =0 ; i <DATA_SIZE ; i++) original[i] = (uint8_t )(i &0xff );
@@ -220,10 +261,6 @@ static struct tst_test test = { {"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP}, {} }, - .needs_cmds = (struct tst_cmd[]) { - {.cmd ="ip" }, - {} - }, .tags = (const struct tst_tag[]) { {"CVE" ,"2026-46300" }, {}
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 6/6 xfrm03: Configure xfrm using direct netlink request
In reply to Martin DouchaSome kernels may not support ESP-in-TCP even though the necessary modules are enabled in kernel config. Rewrite the xfrm setup using direct netlink request so that the test can check errno and exit with TCONF if the support is missing.
---
Changes since v1: Removed .needs_cmds
testcases/network/sockets/xfrm03.c | 91 +++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 28 deletions(-)
testcases/network/sockets/xfrm03.c +63 -28
@@ -36,9 +36,11 @@#include <linux/sockios.h> #include <net/if.h> +#include "lapi/xfrm.h" #include "tst_test.h" #include "tst_net.h" #include "tst_netdevice.h" +#include "lapi/udp.h" #include "lapi/tcp.h" #include "lapi/splice.h" #include "lapi/sched.h"
@@ -61,6 +63,41 @@#define MIDDLE_ADDR2 0x0a000201/* 10.0.2.1 */ #define RECEIVER_ADDR 0x0a000202/* 10.0.2.2 */ +static struct xfrm_usersa_info xs_payload = { + .family =AF_INET , + .id.proto = (uint8_t )IPPROTO_ESP , + .mode =XFRM_MODE_TRANSPORT , + .reqid =1 , + .lft = { + .soft_byte_limit =XFRM_INF , + .hard_byte_limit =XFRM_INF , + .soft_packet_limit =XFRM_INF , + .hard_packet_limit =XFRM_INF , + }, +}; + +static struct xfrm_encap_tmpl esp_encap = { + .encap_type =TCP_ENCAP_ESPINTCP +}; + +static struct { +struct xfrm_algo_aead alg; +char buf[KEYTOTAL ]; +} aead_alg_info = { + .alg = { + .alg_name ="rfc4106(gcm(aes))" , + .alg_key_len =KEYTOTAL *8 , + .alg_icv_len =128 + } +}; + +static const struct tst_netlink_attr_list alg_config[] = { + {XFRMA_ENCAP , &esp_encap,sizeof (esp_encap),NULL }, + {XFRMA_ALG_AEAD , &aead_alg_info,sizeof (aead_alg_info.alg) +KEYTOTAL , +NULL }, + {0 ,NULL ,-1 ,NULL } +}; +static const uint8_t aead_key[KEYTOTAL ] = {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff ,
@@ -100,10 +137,13 @@ static void disable_offloads(const char *ifname)static void setup (void ) { -char keyhex[KEYTOTAL *2 +3 ]; -char spihex[16 ]; -char port_str[8 ];int i, ret; +struct tst_netlink_context *ctx; +struct nlmsghdr header = { + .nlmsg_type =XFRM_MSG_NEWSA , + .nlmsg_flags =NLM_F_REQUEST |NLM_F_CREATE |NLM_F_EXCL | +NLM_F_ACK + };tst_setup_netns ();NETDEV_SET_STATE ("lo" ,1 );
@@ -144,27 +184,26 @@ static void setup(void)NETDEV_SET_STATE ("veth_r" ,1 );NETDEV_ADD_ROUTE_INET ("veth_r" ,0 ,0 ,0 ,0 ,htonl (MIDDLE_ADDR2 )); - keyhex[0 ] ='0' ; - keyhex[1 ] ='x' ; -for (i =0 ; i <KEYTOTAL ; i++) -sprintf (keyhex +2 + i *2 ,"%02x" , aead_key[i]); - -snprintf (spihex,sizeof (spihex),"0x%08x" ,SPI ); -snprintf (port_str,sizeof (port_str),"%d" ,TCP_PORT ); - -const char *const xfrm_cmd[] = { -"ip" ,"xfrm" ,"state" ,"add" , -"src" ,"10.0.2.2" ,"dst" ,"10.0.2.2" , -"proto" ,"esp" ,"spi" , spihex, -"encap" ,"espintcp" , port_str, port_str,"0.0.0.0" , -"aead" ,"rfc4106(gcm(aes))" , keyhex,"128" , -"mode" ,"transport" , -NULL - }; - - ret =tst_cmd (xfrm_cmd,NULL ,NULL ,TST_CMD_PASS_RETVAL ); -if (ret) -tst_brk (TCONF ,"Failed to install xfrm ESP-in-TCP state" ); +memcpy (aead_alg_info.alg.alg_key, aead_key,KEYTOTAL ); + xs_payload.id.spi =htonl (SPI ); + xs_payload.saddr.a4 =htonl (RECEIVER_ADDR ); + xs_payload.id.daddr.a4 =htonl (RECEIVER_ADDR ); + esp_encap.encap_sport =htons (TCP_PORT ); + esp_encap.encap_dport =htons (TCP_PORT ); + + ctx =NETLINK_CREATE_CONTEXT (NETLINK_XFRM ); +NETLINK_ADD_MESSAGE (ctx, &header, &xs_payload,sizeof (xs_payload)); +NETLINK_ADD_ATTR_LIST (ctx, alg_config); + ret =NETLINK_SEND_VALIDATE (ctx); +TST_ERR = tst_netlink_errno; +NETLINK_DESTROY_CONTEXT (ctx); + +if (!ret) { +if (TST_ERR ==EPROTONOSUPPORT ) +tst_brk (TCONF ,"xfrm ESP is not supported by kernel" ); + +tst_brk (TBROK |TTERRNO ,"Failed to install xfrm ESP state" ); + }SAFE_SETNS (middlens,CLONE_NEWNET );
@@ -314,10 +353,6 @@ static struct tst_test test = { {"/proc/sys/user/max_user_namespaces", "1024", TST_SR_SKIP}, {} }, - .needs_cmds = (struct tst_cmd[]) { - {.cmd ="ip" }, - {} - }, .tags = (const struct tst_tag[]) { {"CVE" ,"2026-46300" }, {}
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Patchset pushed, thanks.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp