diff mbox series

[01/24] selftests/net: Refactor xfrm_fill_key() to use array of structs

Message ID 20221002081712.757515-2-steffen.klassert@secunet.com (mailing list archive)
State Accepted
Commit 93d7c52a6eb93e58e4569bd4de95ba3b19e3cf20
Delegated to: Netdev Maintainers
Headers show
Series [01/24] selftests/net: Refactor xfrm_fill_key() to use array of structs | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply

Commit Message

Steffen Klassert Oct. 2, 2022, 8:16 a.m. UTC
From: Gautam Menghani <gautammenghani201@gmail.com>

A TODO in net/ipsec.c asks to refactor the code in xfrm_fill_key() to
use set/map to avoid manually comparing each algorithm with the "name"
parameter passed to the function as an argument. This patch refactors
the code to create an array of structs where each struct contains the
algorithm name and its corresponding key length.

Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
---
 tools/testing/selftests/net/ipsec.c | 104 ++++++++++++----------------
 1 file changed, 45 insertions(+), 59 deletions(-)

Comments

patchwork-bot+netdevbpf@kernel.org Oct. 3, 2022, 7:10 a.m. UTC | #1
Hello:

This series was applied to netdev/net-next.git (master)
by Steffen Klassert <steffen.klassert@secunet.com>:

On Sun, 2 Oct 2022 10:16:49 +0200 you wrote:
> From: Gautam Menghani <gautammenghani201@gmail.com>
> 
> A TODO in net/ipsec.c asks to refactor the code in xfrm_fill_key() to
> use set/map to avoid manually comparing each algorithm with the "name"
> parameter passed to the function as an argument. This patch refactors
> the code to create an array of structs where each struct contains the
> algorithm name and its corresponding key length.
> 
> [...]

Here is the summary with links:
  - [01/24] selftests/net: Refactor xfrm_fill_key() to use array of structs
    https://git.kernel.org/netdev/net-next/c/93d7c52a6eb9
  - [02/24] xfrm: Drop unused argument
    https://git.kernel.org/netdev/net-next/c/0de1978852df
  - [03/24] net: allow storing xfrm interface metadata in metadata_dst
    https://git.kernel.org/netdev/net-next/c/5182a5d48c3d
  - [04/24] xfrm: interface: support collect metadata mode
    https://git.kernel.org/netdev/net-next/c/abc340b38ba2
  - [05/24] xfrm: lwtunnel: add lwtunnel support for xfrm interfaces in collect_md mode
    https://git.kernel.org/netdev/net-next/c/2c2493b9da91
  - [06/24] xfrm: propagate extack to all netlink doit handlers
    https://git.kernel.org/netdev/net-next/c/3bec6c3e83b5
  - [07/24] xfrm: add extack support to verify_newpolicy_info
    https://git.kernel.org/netdev/net-next/c/ec2b4f01536d
  - [08/24] xfrm: add extack to verify_policy_dir
    https://git.kernel.org/netdev/net-next/c/24fc544fb525
  - [09/24] xfrm: add extack to verify_policy_type
    https://git.kernel.org/netdev/net-next/c/fb7deaba40cf
  - [10/24] xfrm: add extack to validate_tmpl
    https://git.kernel.org/netdev/net-next/c/d37bed89f082
  - [11/24] xfrm: add extack to verify_sec_ctx_len
    https://git.kernel.org/netdev/net-next/c/08a717e48037
  - [12/24] xfrm: add extack support to verify_newsa_info
    https://git.kernel.org/netdev/net-next/c/6999aae17a7b
  - [13/24] xfrm: add extack to verify_replay
    https://git.kernel.org/netdev/net-next/c/785b87b22085
  - [14/24] xfrm: add extack to verify_one_alg, verify_auth_trunc, verify_aead
    https://git.kernel.org/netdev/net-next/c/1fc8fde55391
  - [15/24] xfrm: add extack support to xfrm_dev_state_add
    https://git.kernel.org/netdev/net-next/c/adb5c33e4d4c
  - [16/24] xfrm: add extack to attach_*
    https://git.kernel.org/netdev/net-next/c/2b9168266d15
  - [17/24] xfrm: add extack to __xfrm_init_state
    https://git.kernel.org/netdev/net-next/c/741f9a106498
  - [18/24] xfrm: add extack support to xfrm_init_replay
    https://git.kernel.org/netdev/net-next/c/1cf9a3ae3e2d
  - [19/24] xfrm: pass extack down to xfrm_type ->init_state
    https://git.kernel.org/netdev/net-next/c/e1e10b44cf28
  - [20/24] xfrm: ah: add extack to ah_init_state, ah6_init_state
    https://git.kernel.org/netdev/net-next/c/ef87a4f84b10
  - [21/24] xfrm: esp: add extack to esp_init_state, esp6_init_state
    https://git.kernel.org/netdev/net-next/c/67c44f93c951
  - [22/24] xfrm: tunnel: add extack to ipip_init_state, xfrm6_tunnel_init_state
    https://git.kernel.org/netdev/net-next/c/25ec92cd042a
  - [23/24] xfrm: ipcomp: add extack to ipcomp{4,6}_init_state
    https://git.kernel.org/netdev/net-next/c/6ee55320520e
  - [24/24] xfrm: mip6: add extack to mip6_destopt_init_state, mip6_rthdr_init_state
    https://git.kernel.org/netdev/net-next/c/28b5dbd5dcf7

You are awesome, thank you!
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/ipsec.c b/tools/testing/selftests/net/ipsec.c
index cc10c10c5ed9..9a8229abfa02 100644
--- a/tools/testing/selftests/net/ipsec.c
+++ b/tools/testing/selftests/net/ipsec.c
@@ -58,6 +58,8 @@ 
 #define VETH_FMT	"ktst-%d"
 #define VETH_LEN	12
 
+#define XFRM_ALGO_NR_KEYS 29
+
 static int nsfd_parent	= -1;
 static int nsfd_childa	= -1;
 static int nsfd_childb	= -1;
@@ -75,6 +77,43 @@  const unsigned int ping_timeout		= 300;
 const unsigned int ping_count		= 100;
 const unsigned int ping_success		= 80;
 
+struct xfrm_key_entry {
+	char algo_name[35];
+	int key_len;
+};
+
+struct xfrm_key_entry xfrm_key_entries[] = {
+	{"digest_null", 0},
+	{"ecb(cipher_null)", 0},
+	{"cbc(des)", 64},
+	{"hmac(md5)", 128},
+	{"cmac(aes)", 128},
+	{"xcbc(aes)", 128},
+	{"cbc(cast5)", 128},
+	{"cbc(serpent)", 128},
+	{"hmac(sha1)", 160},
+	{"hmac(rmd160)", 160},
+	{"cbc(des3_ede)", 192},
+	{"hmac(sha256)", 256},
+	{"cbc(aes)", 256},
+	{"cbc(camellia)", 256},
+	{"cbc(twofish)", 256},
+	{"rfc3686(ctr(aes))", 288},
+	{"hmac(sha384)", 384},
+	{"cbc(blowfish)", 448},
+	{"hmac(sha512)", 512},
+	{"rfc4106(gcm(aes))-128", 160},
+	{"rfc4543(gcm(aes))-128", 160},
+	{"rfc4309(ccm(aes))-128", 152},
+	{"rfc4106(gcm(aes))-192", 224},
+	{"rfc4543(gcm(aes))-192", 224},
+	{"rfc4309(ccm(aes))-192", 216},
+	{"rfc4106(gcm(aes))-256", 288},
+	{"rfc4543(gcm(aes))-256", 288},
+	{"rfc4309(ccm(aes))-256", 280},
+	{"rfc7539(chacha20,poly1305)-128", 0}
+};
+
 static void randomize_buffer(void *buf, size_t buflen)
 {
 	int *p = (int *)buf;
@@ -767,65 +806,12 @@  static int do_ping(int cmd_fd, char *buf, size_t buf_len, struct in_addr from,
 static int xfrm_fill_key(char *name, char *buf,
 		size_t buf_len, unsigned int *key_len)
 {
-	/* TODO: use set/map instead */
-	if (strncmp(name, "digest_null", ALGO_LEN) == 0)
-		*key_len = 0;
-	else if (strncmp(name, "ecb(cipher_null)", ALGO_LEN) == 0)
-		*key_len = 0;
-	else if (strncmp(name, "cbc(des)", ALGO_LEN) == 0)
-		*key_len = 64;
-	else if (strncmp(name, "hmac(md5)", ALGO_LEN) == 0)
-		*key_len = 128;
-	else if (strncmp(name, "cmac(aes)", ALGO_LEN) == 0)
-		*key_len = 128;
-	else if (strncmp(name, "xcbc(aes)", ALGO_LEN) == 0)
-		*key_len = 128;
-	else if (strncmp(name, "cbc(cast5)", ALGO_LEN) == 0)
-		*key_len = 128;
-	else if (strncmp(name, "cbc(serpent)", ALGO_LEN) == 0)
-		*key_len = 128;
-	else if (strncmp(name, "hmac(sha1)", ALGO_LEN) == 0)
-		*key_len = 160;
-	else if (strncmp(name, "hmac(rmd160)", ALGO_LEN) == 0)
-		*key_len = 160;
-	else if (strncmp(name, "cbc(des3_ede)", ALGO_LEN) == 0)
-		*key_len = 192;
-	else if (strncmp(name, "hmac(sha256)", ALGO_LEN) == 0)
-		*key_len = 256;
-	else if (strncmp(name, "cbc(aes)", ALGO_LEN) == 0)
-		*key_len = 256;
-	else if (strncmp(name, "cbc(camellia)", ALGO_LEN) == 0)
-		*key_len = 256;
-	else if (strncmp(name, "cbc(twofish)", ALGO_LEN) == 0)
-		*key_len = 256;
-	else if (strncmp(name, "rfc3686(ctr(aes))", ALGO_LEN) == 0)
-		*key_len = 288;
-	else if (strncmp(name, "hmac(sha384)", ALGO_LEN) == 0)
-		*key_len = 384;
-	else if (strncmp(name, "cbc(blowfish)", ALGO_LEN) == 0)
-		*key_len = 448;
-	else if (strncmp(name, "hmac(sha512)", ALGO_LEN) == 0)
-		*key_len = 512;
-	else if (strncmp(name, "rfc4106(gcm(aes))-128", ALGO_LEN) == 0)
-		*key_len = 160;
-	else if (strncmp(name, "rfc4543(gcm(aes))-128", ALGO_LEN) == 0)
-		*key_len = 160;
-	else if (strncmp(name, "rfc4309(ccm(aes))-128", ALGO_LEN) == 0)
-		*key_len = 152;
-	else if (strncmp(name, "rfc4106(gcm(aes))-192", ALGO_LEN) == 0)
-		*key_len = 224;
-	else if (strncmp(name, "rfc4543(gcm(aes))-192", ALGO_LEN) == 0)
-		*key_len = 224;
-	else if (strncmp(name, "rfc4309(ccm(aes))-192", ALGO_LEN) == 0)
-		*key_len = 216;
-	else if (strncmp(name, "rfc4106(gcm(aes))-256", ALGO_LEN) == 0)
-		*key_len = 288;
-	else if (strncmp(name, "rfc4543(gcm(aes))-256", ALGO_LEN) == 0)
-		*key_len = 288;
-	else if (strncmp(name, "rfc4309(ccm(aes))-256", ALGO_LEN) == 0)
-		*key_len = 280;
-	else if (strncmp(name, "rfc7539(chacha20,poly1305)-128", ALGO_LEN) == 0)
-		*key_len = 0;
+	int i;
+
+	for (i = 0; i < XFRM_ALGO_NR_KEYS; i++) {
+		if (strncmp(name, xfrm_key_entries[i].algo_name, ALGO_LEN) == 0)
+			*key_len = xfrm_key_entries[i].key_len;
+	}
 
 	if (*key_len > buf_len) {
 		printk("Can't pack a key - too big for buffer");