Blame SOURCES/0006-prefix-list-duplication.patch

d6cd25
From 667dcc277c15c0bddc785f9b949d658f8d815818 Mon Sep 17 00:00:00 2001
d6cd25
From: Igor Ryzhov <iryzhov@nfware.com>
d6cd25
Date: Tue, 10 Aug 2021 21:46:37 +0300
d6cd25
Subject: [PATCH] lib: fix prefix-list duplication check
d6cd25
d6cd25
Currently, when we check the new prefix-list entry for duplication, we
d6cd25
only take filled in fields into account and ignore optional fields.
d6cd25
For example, if we already have `ip prefix-list A 0.0.0.0/0 le 32` and
d6cd25
we try to add `ip prefix-list A 0.0.0.0/0`, it is treated as duplicate.
d6cd25
We should always compare all prefix-list fields when doing the check.
d6cd25
d6cd25
Fixes #9355.
d6cd25
d6cd25
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
d6cd25
---
d6cd25
 lib/filter.h     |   9 ++---
d6cd25
 lib/filter_cli.c | 102 ++++++++++-------------------------------------
d6cd25
 lib/filter_nb.c  |  85 ++++++++++++++++++++++-----------------
d6cd25
 3 files changed, 74 insertions(+), 122 deletions(-)
d6cd25
d6cd25
diff --git a/lib/filter.h b/lib/filter.h
d6cd25
index 941fabd38b8..d1956ec019f 100644
d6cd25
--- a/lib/filter.h
d6cd25
+++ b/lib/filter.h
d6cd25
@@ -207,11 +207,10 @@ struct plist_dup_args {
d6cd25
 	/** Entry action. */
d6cd25
 	const char *pda_action;
d6cd25
 
d6cd25
-#define PDA_MAX_VALUES 4
d6cd25
-	/** Entry XPath for value. */
d6cd25
-	const char *pda_xpath[PDA_MAX_VALUES];
d6cd25
-	/** Entry value to match. */
d6cd25
-	const char *pda_value[PDA_MAX_VALUES];
d6cd25
+	bool any;
d6cd25
+	struct prefix prefix;
d6cd25
+	int ge;
d6cd25
+	int le;
d6cd25
 
d6cd25
 	/** Duplicated entry found in list? */
d6cd25
 	bool pda_found;
d6cd25
diff --git a/lib/filter_cli.c b/lib/filter_cli.c
d6cd25
index f030ce1b335..45c7544a3b4 100644
d6cd25
--- a/lib/filter_cli.c
d6cd25
+++ b/lib/filter_cli.c
d6cd25
@@ -1196,11 +1196,9 @@ static int plist_remove_if_empty(struct vty *vty, const char *iptype,
d6cd25
 
d6cd25
 static int plist_remove(struct vty *vty, const char *iptype, const char *name,
d6cd25
 			const char *seq, const char *action,
d6cd25
-			const char *prefix_str, const char *ge_str,
d6cd25
-			const char *le_str)
d6cd25
+			union prefixconstptr prefix, int ge, int le)
d6cd25
 {
d6cd25
 	int64_t sseq;
d6cd25
-	int arg_idx = 0;
d6cd25
 	struct plist_dup_args pda = {};
d6cd25
 	char xpath[XPATH_MAXLEN];
d6cd25
 	char xpath_entry[XPATH_MAXLEN + 32];
d6cd25
@@ -1225,43 +1223,13 @@ static int plist_remove(struct vty *vty, const char *iptype, const char *name,
d6cd25
 	pda.pda_type = iptype;
d6cd25
 	pda.pda_name = name;
d6cd25
 	pda.pda_action = action;
d6cd25
-	if (prefix_str) {
d6cd25
-		if (strmatch(iptype, "ipv4")) {
d6cd25
-			pda.pda_xpath[arg_idx] = "./ipv4-prefix";
d6cd25
-			pda.pda_value[arg_idx] = prefix_str;
d6cd25
-			arg_idx++;
d6cd25
-			if (ge_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv4-prefix-length-greater-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = ge_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
-			if (le_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv4-prefix-length-lesser-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = le_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
-		} else {
d6cd25
-			pda.pda_xpath[arg_idx] = "./ipv6-prefix";
d6cd25
-			pda.pda_value[arg_idx] = prefix_str;
d6cd25
-			arg_idx++;
d6cd25
-			if (ge_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv6-prefix-length-greater-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = ge_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
-			if (le_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv6-prefix-length-lesser-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = le_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
-		}
d6cd25
+	if (prefix.p) {
d6cd25
+		prefix_copy(&pda.prefix, prefix);
d6cd25
+		apply_mask(&pda.prefix);
d6cd25
+		pda.ge = ge;
d6cd25
+		pda.le = le;
d6cd25
 	} else {
d6cd25
-		pda.pda_xpath[0] = "./any";
d6cd25
-		pda.pda_value[0] = "";
d6cd25
+		pda.any = true;
d6cd25
 	}
d6cd25
 
d6cd25
 	if (plist_is_dup(vty->candidate_config->dnode, &pda))
d6cd25
@@ -1298,7 +1266,6 @@ DEFPY_YANG(
d6cd25
 	"Maximum prefix length\n")
d6cd25
 {
d6cd25
 	int64_t sseq;
d6cd25
-	int arg_idx = 0;
d6cd25
 	struct plist_dup_args pda = {};
d6cd25
 	char xpath[XPATH_MAXLEN];
d6cd25
 	char xpath_entry[XPATH_MAXLEN + 128];
d6cd25
@@ -1312,24 +1279,11 @@ DEFPY_YANG(
d6cd25
 		pda.pda_name = name;
d6cd25
 		pda.pda_action = action;
d6cd25
 		if (prefix_str) {
d6cd25
-			pda.pda_xpath[arg_idx] = "./ipv4-prefix";
d6cd25
-			pda.pda_value[arg_idx] = prefix_str;
d6cd25
-			arg_idx++;
d6cd25
-			if (ge_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv4-prefix-length-greater-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = ge_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
-			if (le_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv4-prefix-length-lesser-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = le_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
+			prefix_copy(&pda.prefix, prefix);
d6cd25
+			pda.ge = ge;
d6cd25
+			pda.le = le;
d6cd25
 		} else {
d6cd25
-			pda.pda_xpath[0] = "./any";
d6cd25
-			pda.pda_value[0] = "";
d6cd25
+			pda.any = true;
d6cd25
 		}
d6cd25
 
d6cd25
 		/* Duplicated entry without sequence, just quit. */
d6cd25
@@ -1408,8 +1362,8 @@ DEFPY_YANG(
d6cd25
 	"Maximum prefix length to be matched\n"
d6cd25
 	"Maximum prefix length\n")
d6cd25
 {
d6cd25
-	return plist_remove(vty, "ipv4", name, seq_str, action, prefix_str,
d6cd25
-			    ge_str, le_str);
d6cd25
+	return plist_remove(vty, "ipv4", name, seq_str, action,
d6cd25
+			    prefix_str ? prefix : NULL, ge, le);
d6cd25
 }
d6cd25
 
d6cd25
 DEFPY_YANG(
d6cd25
@@ -1421,7 +1375,7 @@ DEFPY_YANG(
d6cd25
 	PREFIX_LIST_NAME_STR
d6cd25
 	ACCESS_LIST_SEQ_STR)
d6cd25
 {
d6cd25
-	return plist_remove(vty, "ipv4", name, seq_str, NULL, NULL, NULL, NULL);
d6cd25
+	return plist_remove(vty, "ipv4", name, seq_str, NULL, NULL, 0, 0);
d6cd25
 }
d6cd25
 
d6cd25
 DEFPY_YANG(
d6cd25
@@ -1516,7 +1470,6 @@ DEFPY_YANG(
d6cd25
 	"Minimum prefix length\n")
d6cd25
 {
d6cd25
 	int64_t sseq;
d6cd25
-	int arg_idx = 0;
d6cd25
 	struct plist_dup_args pda = {};
d6cd25
 	char xpath[XPATH_MAXLEN];
d6cd25
 	char xpath_entry[XPATH_MAXLEN + 128];
d6cd25
@@ -1530,24 +1483,11 @@ DEFPY_YANG(
d6cd25
 		pda.pda_name = name;
d6cd25
 		pda.pda_action = action;
d6cd25
 		if (prefix_str) {
d6cd25
-			pda.pda_xpath[arg_idx] = "./ipv6-prefix";
d6cd25
-			pda.pda_value[arg_idx] = prefix_str;
d6cd25
-			arg_idx++;
d6cd25
-			if (ge_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv6-prefix-length-greater-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = ge_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
-			if (le_str) {
d6cd25
-				pda.pda_xpath[arg_idx] =
d6cd25
-					"./ipv6-prefix-length-lesser-or-equal";
d6cd25
-				pda.pda_value[arg_idx] = le_str;
d6cd25
-				arg_idx++;
d6cd25
-			}
d6cd25
+			prefix_copy(&pda.prefix, prefix);
d6cd25
+			pda.ge = ge;
d6cd25
+			pda.le = le;
d6cd25
 		} else {
d6cd25
-			pda.pda_xpath[0] = "./any";
d6cd25
-			pda.pda_value[0] = "";
d6cd25
+			pda.any = true;
d6cd25
 		}
d6cd25
 
d6cd25
 		/* Duplicated entry without sequence, just quit. */
d6cd25
@@ -1626,8 +1566,8 @@ DEFPY_YANG(
d6cd25
 	"Minimum prefix length to be matched\n"
d6cd25
 	"Minimum prefix length\n")
d6cd25
 {
d6cd25
-	return plist_remove(vty, "ipv6", name, seq_str, action, prefix_str,
d6cd25
-			    ge_str, le_str);
d6cd25
+	return plist_remove(vty, "ipv6", name, seq_str, action,
d6cd25
+			    prefix_str ? prefix : NULL, ge, le);
d6cd25
 }
d6cd25
 
d6cd25
 DEFPY_YANG(
d6cd25
@@ -1639,7 +1579,7 @@ DEFPY_YANG(
d6cd25
 	PREFIX_LIST_NAME_STR
d6cd25
 	ACCESS_LIST_SEQ_STR)
d6cd25
 {
d6cd25
-	return plist_remove(vty, "ipv6", name, seq_str, NULL, NULL, NULL, NULL);
d6cd25
+	return plist_remove(vty, "ipv6", name, seq_str, NULL, NULL, 0, 0);
d6cd25
 }
d6cd25
 
d6cd25
 DEFPY_YANG(
d6cd25
diff --git a/lib/filter_nb.c b/lib/filter_nb.c
d6cd25
index 85805ffa47c..80ea7a57cb2 100644
d6cd25
--- a/lib/filter_nb.c
d6cd25
+++ b/lib/filter_nb.c
d6cd25
@@ -387,10 +387,50 @@ static bool acl_zebra_is_dup(const struct lyd_node *dnode,
d6cd25
 	return acl_is_dup(entry_dnode, &ada);
d6cd25
 }
d6cd25
 
d6cd25
+static void plist_dnode_to_prefix(const struct lyd_node *dnode, bool *any,
d6cd25
+				  struct prefix *p, int *ge, int *le)
d6cd25
+{
d6cd25
+	*any = false;
d6cd25
+	*ge = 0;
d6cd25
+	*le = 0;
d6cd25
+
d6cd25
+	if (yang_dnode_exists(dnode, "./any")) {
d6cd25
+		*any = true;
d6cd25
+		return;
d6cd25
+	}
d6cd25
+
d6cd25
+	switch (yang_dnode_get_enum(dnode, "../type")) {
d6cd25
+	case YPLT_IPV4:
d6cd25
+		yang_dnode_get_prefix(p, dnode, "./ipv4-prefix");
d6cd25
+		if (yang_dnode_exists(dnode,
d6cd25
+				      "./ipv4-prefix-length-greater-or-equal"))
d6cd25
+			*ge = yang_dnode_get_uint8(
d6cd25
+				dnode, "./ipv4-prefix-length-greater-or-equal");
d6cd25
+		if (yang_dnode_exists(dnode,
d6cd25
+				      "./ipv4-prefix-length-lesser-or-equal"))
d6cd25
+			*le = yang_dnode_get_uint8(
d6cd25
+				dnode, "./ipv4-prefix-length-lesser-or-equal");
d6cd25
+		break;
d6cd25
+	case YPLT_IPV6:
d6cd25
+		yang_dnode_get_prefix(p, dnode, "./ipv6-prefix");
d6cd25
+		if (yang_dnode_exists(dnode,
d6cd25
+				      "./ipv6-prefix-length-greater-or-equal"))
d6cd25
+			*ge = yang_dnode_get_uint8(
d6cd25
+				dnode, "./ipv6-prefix-length-greater-or-equal");
d6cd25
+		if (yang_dnode_exists(dnode,
d6cd25
+				      "./ipv6-prefix-length-lesser-or-equal"))
d6cd25
+			*le = yang_dnode_get_uint8(
d6cd25
+				dnode, "./ipv6-prefix-length-lesser-or-equal");
d6cd25
+		break;
d6cd25
+	}
d6cd25
+}
d6cd25
+
d6cd25
 static int _plist_is_dup(const struct lyd_node *dnode, void *arg)
d6cd25
 {
d6cd25
 	struct plist_dup_args *pda = arg;
d6cd25
-	int idx;
d6cd25
+	struct prefix p;
d6cd25
+	int ge, le;
d6cd25
+	bool any;
d6cd25
 
d6cd25
 	/* This entry is the caller, so skip it. */
d6cd25
 	if (pda->pda_entry_dnode
d6cd25
@@ -400,19 +440,14 @@ static int _plist_is_dup(const struct lyd_node *dnode, void *arg)
d6cd25
 	if (strcmp(yang_dnode_get_string(dnode, "action"), pda->pda_action))
d6cd25
 		return YANG_ITER_CONTINUE;
d6cd25
 
d6cd25
-	/* Check if all values match. */
d6cd25
-	for (idx = 0; idx < PDA_MAX_VALUES; idx++) {
d6cd25
-		/* No more values. */
d6cd25
-		if (pda->pda_xpath[idx] == NULL)
d6cd25
-			break;
d6cd25
+	plist_dnode_to_prefix(dnode, &any, &p, &ge, &le);
d6cd25
 
d6cd25
-		/* Not same type, just skip it. */
d6cd25
-		if (!yang_dnode_exists(dnode, pda->pda_xpath[idx]))
d6cd25
+	if (pda->any) {
d6cd25
+		if (!any)
d6cd25
 			return YANG_ITER_CONTINUE;
d6cd25
-
d6cd25
-		/* Check if different value. */
d6cd25
-		if (strcmp(yang_dnode_get_string(dnode, pda->pda_xpath[idx]),
d6cd25
-			   pda->pda_value[idx]))
d6cd25
+	} else {
d6cd25
+		if (!prefix_same(&pda->prefix, &p) || pda->ge != ge
d6cd25
+		    || pda->le != le)
d6cd25
 			return YANG_ITER_CONTINUE;
d6cd25
 	}
d6cd25
 
d6cd25
@@ -439,17 +474,6 @@ static bool plist_is_dup_nb(const struct lyd_node *dnode)
d6cd25
 	const struct lyd_node *entry_dnode =
d6cd25
 		yang_dnode_get_parent(dnode, "entry");
d6cd25
 	struct plist_dup_args pda = {};
d6cd25
-	int idx = 0, arg_idx = 0;
d6cd25
-	static const char *entries[] = {
d6cd25
-		"./ipv4-prefix",
d6cd25
-		"./ipv4-prefix-length-greater-or-equal",
d6cd25
-		"./ipv4-prefix-length-lesser-or-equal",
d6cd25
-		"./ipv6-prefix",
d6cd25
-		"./ipv6-prefix-length-greater-or-equal",
d6cd25
-		"./ipv6-prefix-length-lesser-or-equal",
d6cd25
-		"./any",
d6cd25
-		NULL
d6cd25
-	};
d6cd25
 
d6cd25
 	/* Initialize. */
d6cd25
 	pda.pda_type = yang_dnode_get_string(entry_dnode, "../type");
d6cd25
@@ -457,19 +481,8 @@ static bool plist_is_dup_nb(const struct lyd_node *dnode)
d6cd25
 	pda.pda_action = yang_dnode_get_string(entry_dnode, "action");
d6cd25
 	pda.pda_entry_dnode = entry_dnode;
d6cd25
 
d6cd25
-	/* Load all values/XPaths. */
d6cd25
-	while (entries[idx] != NULL) {
d6cd25
-		if (!yang_dnode_exists(entry_dnode, entries[idx])) {
d6cd25
-			idx++;
d6cd25
-			continue;
d6cd25
-		}
d6cd25
-
d6cd25
-		pda.pda_xpath[arg_idx] = entries[idx];
d6cd25
-		pda.pda_value[arg_idx] =
d6cd25
-			yang_dnode_get_string(entry_dnode, entries[idx]);
d6cd25
-		arg_idx++;
d6cd25
-		idx++;
d6cd25
-	}
d6cd25
+	plist_dnode_to_prefix(entry_dnode, &pda.any, &pda.prefix, &pda.ge,
d6cd25
+			      &pda.le);
d6cd25
 
d6cd25
 	return plist_is_dup(entry_dnode, &pda);
d6cd25
 }