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

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