naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0146-xfrm-add-support-of-ESN-and-anti-replay-window.patch

4aca6e
From 438595db1cfbb23a85f68d2a573a9f35083bc3ee Mon Sep 17 00:00:00 2001
4aca6e
From: Phil Sutter <psutter@redhat.com>
4aca6e
Date: Mon, 27 Mar 2017 13:11:34 +0200
4aca6e
Subject: [PATCH] xfrm: add support of ESN and anti-replay window
4aca6e
4aca6e
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1425059
4aca6e
Upstream Status: iproute2.git commit 0151b56d10296
4aca6e
4aca6e
commit 0151b56d102961c1418aea3ee53428d4ca2669c9
4aca6e
Author: dingzhi <zhi.ding@6wind.com>
4aca6e
Date:   Mon Oct 20 11:23:04 2014 +0200
4aca6e
4aca6e
    xfrm: add support of ESN and anti-replay window
4aca6e
4aca6e
    This patch allows to configure ESN and anti-replay window.
4aca6e
4aca6e
    Signed-off-by: dingzhi <zhi.ding@6wind.com>
4aca6e
    Signed-off-by: Adrien Mazarguil <adrien.mazarguil@6wind.com>
4aca6e
    Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
4aca6e
---
4aca6e
 ip/ipxfrm.c     | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
4aca6e
 ip/xfrm_state.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
4aca6e
 2 files changed, 109 insertions(+), 7 deletions(-)
4aca6e
4aca6e
diff --git a/ip/ipxfrm.c b/ip/ipxfrm.c
4aca6e
index 899ec9d..fd3e645 100644
4aca6e
--- a/ip/ipxfrm.c
4aca6e
+++ b/ip/ipxfrm.c
4aca6e
@@ -795,6 +795,62 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
4aca6e
 		fprintf(fp, "%s", _SL_);
4aca6e
 	}
4aca6e
 
4aca6e
+	if (tb[XFRMA_REPLAY_VAL]) {
4aca6e
+		struct xfrm_replay_state *replay;
4aca6e
+
4aca6e
+		if (prefix)
4aca6e
+			fputs(prefix, fp);
4aca6e
+		fprintf(fp, "anti-replay context: ");
4aca6e
+
4aca6e
+		if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) {
4aca6e
+			fprintf(fp, "(ERROR truncated)");
4aca6e
+			fprintf(fp, "%s", _SL_);
4aca6e
+			return;
4aca6e
+		}
4aca6e
+
4aca6e
+		replay = (struct xfrm_replay_state *)RTA_DATA(tb[XFRMA_REPLAY_VAL]);
4aca6e
+		fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
4aca6e
+			replay->seq, replay->oseq, replay->bitmap);
4aca6e
+		fprintf(fp, "%s", _SL_);
4aca6e
+	}
4aca6e
+
4aca6e
+	if (tb[XFRMA_REPLAY_ESN_VAL]) {
4aca6e
+		struct xfrm_replay_state_esn *replay;
4aca6e
+		unsigned int i, j;
4aca6e
+
4aca6e
+		if (prefix)
4aca6e
+			fputs(prefix, fp);
4aca6e
+		fprintf(fp, "anti-replay esn context:");
4aca6e
+
4aca6e
+		if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) {
4aca6e
+			fprintf(fp, "(ERROR truncated)");
4aca6e
+			fprintf(fp, "%s", _SL_);
4aca6e
+			return;
4aca6e
+		}
4aca6e
+		fprintf(fp, "%s", _SL_);
4aca6e
+
4aca6e
+		replay = (struct xfrm_replay_state_esn *)RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]);
4aca6e
+		if (prefix)
4aca6e
+			fputs(prefix, fp);
4aca6e
+		fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
4aca6e
+			replay->seq_hi, replay->seq, replay->oseq_hi,
4aca6e
+			replay->oseq);
4aca6e
+		fprintf(fp, "%s", _SL_);
4aca6e
+		if (prefix)
4aca6e
+			fputs(prefix, fp);
4aca6e
+		fprintf(fp, " replay_window %u, bitmap-length %u",
4aca6e
+			replay->replay_window, replay->bmp_len);
4aca6e
+		for (i = replay->bmp_len, j = 0; i; i--) {
4aca6e
+			if (j++ % 8 == 0) {
4aca6e
+				fprintf(fp, "%s", _SL_);
4aca6e
+				if (prefix)
4aca6e
+					fputs(prefix, fp);
4aca6e
+				fprintf(fp, " ");
4aca6e
+			}
4aca6e
+			fprintf(fp, "%08x ", replay->bmp[i - 1]);
4aca6e
+		}
4aca6e
+		fprintf(fp, "%s", _SL_);
4aca6e
+	}
4aca6e
 }
4aca6e
 
4aca6e
 static int xfrm_selector_iszero(struct xfrm_selector *s)
4aca6e
@@ -838,6 +894,7 @@ void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
4aca6e
 		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
4aca6e
 		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
4aca6e
 		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
4aca6e
+		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn");
4aca6e
 		if (flags)
4aca6e
 			fprintf(fp, "%x", flags);
4aca6e
 	}
4aca6e
diff --git a/ip/xfrm_state.c b/ip/xfrm_state.c
4aca6e
index 4dfddbc..d01f8ad 100644
4aca6e
--- a/ip/xfrm_state.c
4aca6e
+++ b/ip/xfrm_state.c
4aca6e
@@ -59,6 +59,7 @@ static void usage(void)
4aca6e
 	fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
4aca6e
 	fprintf(stderr, "        [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n");
4aca6e
 	fprintf(stderr, "        [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n");
4aca6e
+	fprintf(stderr, "        [ replay-seq-hi SEQ ] [ replay-oseq-hi SEQ ]\n");
4aca6e
 	fprintf(stderr, "        [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n");
4aca6e
 	fprintf(stderr, "        [ coa ADDR[/PLEN] ] [ ctx CTX ] [ extra-flag EXTRA-FLAG-LIST ]\n");
4aca6e
 	fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n");
4aca6e
@@ -88,7 +89,7 @@ static void usage(void)
4aca6e
 	fprintf(stderr, " ALGO-NAME\n");
4aca6e
 	fprintf(stderr, "MODE := transport | tunnel | beet | ro | in_trigger\n");
4aca6e
 	fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
4aca6e
-	fprintf(stderr, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4\n");
4aca6e
+	fprintf(stderr, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4 | esn\n");
4aca6e
 	fprintf(stderr, "EXTRA-FLAG-LIST := [ EXTRA-FLAG-LIST ] EXTRA-FLAG\n");
4aca6e
 	fprintf(stderr, "EXTRA-FLAG := dont-encap-dscp\n");
4aca6e
 	fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
4aca6e
@@ -213,6 +214,8 @@ static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
4aca6e
 				*flags |= XFRM_STATE_AF_UNSPEC;
4aca6e
 			else if (strcmp(*argv, "align4") == 0)
4aca6e
 				*flags |= XFRM_STATE_ALIGN4;
4aca6e
+			else if (strcmp(*argv, "esn") == 0)
4aca6e
+				*flags |= XFRM_STATE_ESN;
4aca6e
 			else {
4aca6e
 				PREV_ARG(); /* back track */
4aca6e
 				break;
4aca6e
@@ -272,6 +275,9 @@ static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
4aca6e
 		char  			buf[RTA_BUF_SIZE];
4aca6e
 	} req;
4aca6e
 	struct xfrm_replay_state replay;
4aca6e
+	struct xfrm_replay_state_esn replay_esn;
4aca6e
+	__u32 replay_window = 0;
4aca6e
+	__u32 seq = 0, oseq = 0, seq_hi = 0, oseq_hi = 0;
4aca6e
 	char *idp = NULL;
4aca6e
 	char *aeadop = NULL;
4aca6e
 	char *ealgop = NULL;
4aca6e
@@ -288,6 +294,7 @@ static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
4aca6e
 
4aca6e
 	memset(&req, 0, sizeof(req));
4aca6e
 	memset(&replay, 0, sizeof(replay));
4aca6e
+	memset(&replay_esn, 0, sizeof(replay_esn));
4aca6e
 	memset(&ctx, 0, sizeof(ctx));
4aca6e
 
4aca6e
 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
4aca6e
@@ -314,16 +321,24 @@ static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
4aca6e
 			xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
4aca6e
 		} else if (strcmp(*argv, "replay-window") == 0) {
4aca6e
 			NEXT_ARG();
4aca6e
-			if (get_u8(&req.xsinfo.replay_window, *argv, 0))
4aca6e
+			if (get_u32(&replay_window, *argv, 0))
4aca6e
 				invarg("value after \"replay-window\" is invalid", *argv);
4aca6e
 		} else if (strcmp(*argv, "replay-seq") == 0) {
4aca6e
 			NEXT_ARG();
4aca6e
-			if (get_u32(&replay.seq, *argv, 0))
4aca6e
+			if (get_u32(&seq, *argv, 0))
4aca6e
 				invarg("value after \"replay-seq\" is invalid", *argv);
4aca6e
+		} else if (strcmp(*argv, "replay-seq-hi") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			if (get_u32(&seq_hi, *argv, 0))
4aca6e
+				invarg("value after \"replay-seq-hi\" is invalid", *argv);
4aca6e
 		} else if (strcmp(*argv, "replay-oseq") == 0) {
4aca6e
 			NEXT_ARG();
4aca6e
-			if (get_u32(&replay.oseq, *argv, 0))
4aca6e
+			if (get_u32(&oseq, *argv, 0))
4aca6e
 				invarg("value after \"replay-oseq\" is invalid", *argv);
4aca6e
+		} else if (strcmp(*argv, "replay-oseq-hi") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			if (get_u32(&oseq_hi, *argv, 0))
4aca6e
+				invarg("value after \"replay-oseq-hi\" is invalid", *argv);
4aca6e
 		} else if (strcmp(*argv, "flag") == 0) {
4aca6e
 			NEXT_ARG();
4aca6e
 			xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
4aca6e
@@ -511,9 +526,39 @@ static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
4aca6e
 		argc--; argv++;
4aca6e
 	}
4aca6e
 
4aca6e
-	if (replay.seq || replay.oseq)
4aca6e
-		addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
4aca6e
-			  (void *)&replay, sizeof(replay));
4aca6e
+	if (req.xsinfo.flags & XFRM_STATE_ESN &&
4aca6e
+	    replay_window == 0) {
4aca6e
+		fprintf(stderr, "Error: esn flag set without replay-window.\n");
4aca6e
+		exit(-1);
4aca6e
+	}
4aca6e
+
4aca6e
+	if (replay_window > XFRMA_REPLAY_ESN_MAX) {
4aca6e
+		fprintf(stderr,
4aca6e
+			"Error: replay-window (%u) > XFRMA_REPLAY_ESN_MAX (%u).\n",
4aca6e
+			replay_window, XFRMA_REPLAY_ESN_MAX);
4aca6e
+		exit(-1);
4aca6e
+	}
4aca6e
+
4aca6e
+	if (req.xsinfo.flags & XFRM_STATE_ESN ||
4aca6e
+	    replay_window > (sizeof(replay.bitmap) * 8)) {
4aca6e
+		replay_esn.seq = seq;
4aca6e
+		replay_esn.oseq = oseq;
4aca6e
+		replay_esn.seq_hi = seq_hi;
4aca6e
+		replay_esn.oseq_hi = oseq_hi;
4aca6e
+		replay_esn.replay_window = replay_window;
4aca6e
+		replay_esn.bmp_len = (replay_window + sizeof(__u32) * 8 - 1) /
4aca6e
+				     (sizeof(__u32) * 8);
4aca6e
+		addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_ESN_VAL,
4aca6e
+			  &replay_esn, sizeof(replay_esn));
4aca6e
+	} else {
4aca6e
+		if (seq || oseq) {
4aca6e
+			replay.seq = seq;
4aca6e
+			replay.oseq = oseq;
4aca6e
+			addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
4aca6e
+				  &replay, sizeof(replay));
4aca6e
+		}
4aca6e
+		req.xsinfo.replay_window = replay_window;
4aca6e
+	}
4aca6e
 
4aca6e
 	if (extra_flags)
4aca6e
 		addattr32(&req.n, sizeof(req.buf), XFRMA_SA_EXTRA_FLAGS,
4aca6e
-- 
4aca6e
1.8.3.1
4aca6e