[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220802061813.24082-2-ehakim@nvidia.com>
Date: Tue, 2 Aug 2022 09:18:12 +0300
From: <ehakim@...dia.com>
To: <dsahern@...nel.org>, <netdev@...r.kernel.org>
CC: <raeds@...dia.com>, <tariqt@...dia.com>, <sd@...asysnail.net>,
Emeel Hakim <ehakim@...dia.com>
Subject: [PATCH main v2 2/3] macsec: add Extended Packet Number support
From: Emeel Hakim <ehakim@...dia.com>
This patch adds support for extended packet number (XPN).
XPN can be configured by passing 'xpn' as flag in the ip
link add command using macsec type, in addition using
'xpn' keyword instead of the 'pn' and passing a 12 bytes salt
using the 'salt' keyword as part of the ip macsec command is
also required (see example).
XPN uses a 32 bit short secure channel id (ssci) instead of
the 64 bit secure channel id (sci) for the initialization vector
(IV) calculation, this patch also allows the user to optionally
pass his own ssci using the 'ssci' keyword in the ip macsec
command, in case of non provided ssci, the value 1 will be passed
as the default ssci value.
e.g:
create a MACsec device on link eth0 with enabled xpn
# ip link add link eth0 macsec0 type macsec port 11
encrypt on flag xpn
configure a secure association on the device (here ssci is default)
# ip macsec add macsec0 tx sa 0 xpn 1024
salt 838383838383838383838383 on
key 01 81818181818181818181818181818181
configure a secure association on the device with ssci = 5
# ip macsec add macsec0 tx sa 0 xpn 1024 ssci 5
salt 838383838383838383838383 on
key 01 81818181818181818181818181818181
Signed-off-by: Emeel Hakim <ehakim@...dia.com>
---
include/uapi/linux/if_macsec.h | 2 +
ip/ipmacsec.c | 117 +++++++++++++++++++++++++++++----
2 files changed, 108 insertions(+), 11 deletions(-)
diff --git a/include/uapi/linux/if_macsec.h b/include/uapi/linux/if_macsec.h
index eee31cec..6edfea0a 100644
--- a/include/uapi/linux/if_macsec.h
+++ b/include/uapi/linux/if_macsec.h
@@ -22,6 +22,8 @@
#define MACSEC_KEYID_LEN 16
+#define MACSEC_SALT_LEN 12
+
/* cipher IDs as per IEEE802.1AE-2018 (Table 14-1) */
#define MACSEC_CIPHER_ID_GCM_AES_128 0x0080C20001000001ULL
#define MACSEC_CIPHER_ID_GCM_AES_256 0x0080C20001000002ULL
diff --git a/ip/ipmacsec.c b/ip/ipmacsec.c
index 9aeaafcc..54ab5f39 100644
--- a/ip/ipmacsec.c
+++ b/ip/ipmacsec.c
@@ -41,13 +41,33 @@ struct sci {
char abuf[6];
};
+union __pn {
+ struct {
+# if __BYTE_ORDER == __LITTLE_ENDIAN
+ __u32 lower;
+ __u32 upper;
+#endif
+# if __BYTE_ORDER == __BIG_ENDIAN
+ __u32 upper;
+ __u32 lower;
+#endif
+# if __BYTE_ORDER != __BIG_ENDIAN && __BYTE_ORDER != __LITTLE_ENDIAN
+#error "Please fix byteorder defines"
+#endif
+ };
+ __u64 full64;
+};
+
struct sa_desc {
__u8 an;
- __u32 pn;
+ union __pn pn;
__u8 key_id[MACSEC_KEYID_LEN];
__u32 key_len;
__u8 key[MACSEC_MAX_KEY_LEN];
__u8 active;
+ __u8 salt[MACSEC_SALT_LEN];
+ __u32 ssci;
+ bool xpn;
};
struct cipher_args {
@@ -71,8 +91,14 @@ struct rxsc_desc {
__u8 active;
};
+/* masks to set a bit in flags, hence to set bit i in flags,
+ * we need to use and bitwise with the value 2^(i-1)
+ */
+#define MACSEC_FLAGS_XPN 1
+
#define MACSEC_BUFLEN 1024
+#define DEFAULT_SSCI 1
/* netlink socket */
static struct rtnl_handle genl_rth;
@@ -98,7 +124,7 @@ static void ipmacsec_usage(void)
" ip macsec show\n"
" ip macsec show DEV\n"
" ip macsec offload DEV [ off | phy | mac ]\n"
- "where OPTS := [ pn <u32> ] [ on | off ]\n"
+ "where OPTS := [ pn <u32> ] [ xpn <u64> ] [ salt <u96> ] [ ssci <u32> ] [ on | off ]\n"
" ID := 128-bit hex string\n"
" KEY := 128-bit or 256-bit hex string\n"
" SCI := { sci <u64> | port { 1..2^16-1 } address <lladdr> }\n");
@@ -174,14 +200,34 @@ static int parse_sa_args(int *argcp, char ***argvp, struct sa_desc *sa)
while (argc > 0) {
if (strcmp(*argv, "pn") == 0) {
- if (sa->pn != 0)
+ if (sa->pn.lower != 0)
duparg2("pn", "pn");
NEXT_ARG();
- ret = get_u32(&sa->pn, *argv, 0);
+ ret = get_u32(&sa->pn.lower, *argv, 0);
+ if (ret)
+ invarg("expected pn", *argv);
+ if (sa->pn.lower == 0)
+ invarg("expected pn != 0", *argv);
+ } else if (strcmp(*argv, "xpn") == 0) {
+ if (sa->pn.full64 != 0)
+ duparg2("xpn", "xpn");
+ NEXT_ARG();
+ ret = get_u64(&sa->pn.full64, *argv, 0);
if (ret)
invarg("expected pn", *argv);
- if (sa->pn == 0)
+ if (sa->pn.full64 == 0)
invarg("expected pn != 0", *argv);
+ sa->xpn = true;
+ } else if (strcmp(*argv, "salt") == 0) {
+ unsigned int len;
+
+ NEXT_ARG();
+ if (!hexstring_a2n(*argv, sa->salt, MACSEC_SALT_LEN,
+ &len))
+ invarg("expected salt", *argv);
+ } else if (strcmp(*argv, "ssci") == 0) {
+ NEXT_ARG();
+ ret = get_u32(&sa->ssci, *argv, 0);
} else if (strcmp(*argv, "key") == 0) {
unsigned int len;
@@ -392,9 +438,29 @@ static int do_modify_nl(enum cmd c, enum macsec_nl_commands cmd, int ifindex,
addattr8(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_AN, sa->an);
if (c != CMD_DEL) {
- if (sa->pn)
+ if (sa->xpn) {
+ if (sa->pn.full64)
+ addattr64(&req.n, MACSEC_BUFLEN,
+ MACSEC_SA_ATTR_PN, sa->pn.full64);
+ if (c == CMD_ADD) {
+ addattr_l(&req.n, MACSEC_BUFLEN,
+ MACSEC_SA_ATTR_SALT,
+ sa->salt, MACSEC_SALT_LEN);
+ if (sa->ssci != 0)
+ addattr32(&req.n, MACSEC_BUFLEN,
+ MACSEC_SA_ATTR_SSCI,
+ sa->ssci);
+ else
+ addattr32(&req.n, MACSEC_BUFLEN,
+ MACSEC_SA_ATTR_SSCI,
+ DEFAULT_SSCI);
+ }
+ }
+
+ if (sa->pn.lower && !sa->xpn) {
addattr32(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_PN,
- sa->pn);
+ sa->pn.lower);
+ }
if (sa->key_len) {
addattr_l(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_KEYID,
@@ -426,10 +492,17 @@ static bool check_sa_args(enum cmd c, struct sa_desc *sa)
return -1;
}
- if (sa->pn == 0) {
+ if (sa->pn.full64 == 0) {
fprintf(stderr, "must specify a packet number != 0\n");
return -1;
}
+
+ if (sa->xpn && sa->salt[0] == '\0') {
+ fprintf(stderr,
+ "xpn set, but no salt set.\n");
+ return -1;
+ }
+
} else if (c == CMD_UPD) {
if (sa->key_len) {
fprintf(stderr, "cannot change key on SA\n");
@@ -615,6 +688,9 @@ static void print_key(struct rtattr *key)
#define CIPHER_NAME_GCM_AES_128 "GCM-AES-128"
#define CIPHER_NAME_GCM_AES_256 "GCM-AES-256"
+#define CIPHER_NAME_GCM_AES_XPN_128 "GCM-AES-XPN-128"
+#define CIPHER_NAME_GCM_AES_XPN_256 "GCM-AES-XPN-256"
+
#define DEFAULT_CIPHER_NAME CIPHER_NAME_GCM_AES_128
static const char *cs_id_to_name(__u64 cid)
@@ -627,6 +703,10 @@ static const char *cs_id_to_name(__u64 cid)
return CIPHER_NAME_GCM_AES_128;
case MACSEC_CIPHER_ID_GCM_AES_256:
return CIPHER_NAME_GCM_AES_256;
+ case MACSEC_CIPHER_ID_GCM_AES_XPN_128:
+ return CIPHER_NAME_GCM_AES_XPN_128;
+ case MACSEC_CIPHER_ID_GCM_AES_XPN_256:
+ return CIPHER_NAME_GCM_AES_XPN_256;
default:
return "(unknown)";
}
@@ -1258,7 +1338,7 @@ static void usage(FILE *f)
" [ offload { mac | phy | off } ]\n"
" [ flag FLAG-LIST ]\n"
"FLAG-LIST := [ FLAG-LIST ] FLAG\n"
- "FLAG :=\n"
+ "FLAG := xpn\n"
);
}
@@ -1268,8 +1348,16 @@ static int macsec_flag_parse(__u8 *flags, int *argcp, char ***argvp)
char **argv = *argvp;
while (1) {
- /* parse flag list */
- break;
+ if (strcmp(*argv, "xpn") == 0) {
+ *flags |= MACSEC_FLAGS_XPN;
+ } else {
+ PREV_ARG(); /* back track */
+ break;
+ }
+
+ if (!NEXT_ARG_OK())
+ break;
+ NEXT_ARG();
}
*argcp = argc;
@@ -1438,6 +1526,13 @@ static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
return -1;
}
+ if (flags & MACSEC_FLAGS_XPN) {
+ if (cipher.id == MACSEC_CIPHER_ID_GCM_AES_256)
+ cipher.id = MACSEC_CIPHER_ID_GCM_AES_XPN_256;
+ else
+ cipher.id = MACSEC_CIPHER_ID_GCM_AES_XPN_128;
+ }
+
if (cipher.id)
addattr_l(n, MACSEC_BUFLEN, IFLA_MACSEC_CIPHER_SUITE,
&cipher.id, sizeof(cipher.id));
--
2.21.3
Powered by blists - more mailing lists