netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Ahern <dsahern@gmail.com>
To: netdev@vger.kernel.org
Cc: roopa@cumulusnetworks.com, David Ahern <dsahern@gmail.com>
Subject: [PATCH v2 net-next 1/9] net: ipv4: refactor key and length checks
Date: Sat, 27 May 2017 16:19:25 -0600	[thread overview]
Message-ID: <20170527221933.57644-2-dsahern@gmail.com> (raw)
In-Reply-To: <20170527221933.57644-1-dsahern@gmail.com>

fib_table_insert and fib_table_delete have the same checks on the prefix
and length. Refactor into a helper. Avoids duplicate extack messages in
the next patch.

Signed-off-by: David Ahern <dsahern@gmail.com>
---
 net/ipv4/fib_trie.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 6e9df7d9bcc2..9bd46e1e1037 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -1099,6 +1099,17 @@ static int fib_insert_alias(struct trie *t, struct key_vector *tp,
 	return 0;
 }
 
+static bool fib_valid_key_len(u32 key, u8 plen)
+{
+	if (plen > KEYLENGTH)
+		return false;
+
+	if ((plen < KEYLENGTH) && (key << plen))
+		return false;
+
+	return true;
+}
+
 /* Caller must hold RTNL. */
 int fib_table_insert(struct net *net, struct fib_table *tb,
 		     struct fib_config *cfg, struct netlink_ext_ack *extack)
@@ -1115,16 +1126,13 @@ int fib_table_insert(struct net *net, struct fib_table *tb,
 	u32 key;
 	int err;
 
-	if (plen > KEYLENGTH)
-		return -EINVAL;
-
 	key = ntohl(cfg->fc_dst);
 
-	pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
-
-	if ((plen < KEYLENGTH) && (key << plen))
+	if (!fib_valid_key_len(key, plen))
 		return -EINVAL;
 
+	pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
+
 	fi = fib_create_info(cfg, extack);
 	if (IS_ERR(fi)) {
 		err = PTR_ERR(fi);
@@ -1518,12 +1526,9 @@ int fib_table_delete(struct net *net, struct fib_table *tb,
 	u8 tos = cfg->fc_tos;
 	u32 key;
 
-	if (plen > KEYLENGTH)
-		return -EINVAL;
-
 	key = ntohl(cfg->fc_dst);
 
-	if ((plen < KEYLENGTH) && (key << plen))
+	if (!fib_valid_key_len(key, plen))
 		return -EINVAL;
 
 	l = fib_find_node(t, &tp, key);
-- 
2.11.0 (Apple Git-81)

  reply	other threads:[~2017-05-27 22:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-27 22:19 [PATCH v2 net-next 0/9] net: another round of extack handling for routing David Ahern
2017-05-27 22:19 ` David Ahern [this message]
2017-05-27 22:19 ` [PATCH v2 net-next 2/9] net: ipv4: Add extack message for invalid prefix or length David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 3/9] net: lwtunnel: Add extack to encap attr validation David Ahern
2017-05-28  1:02   ` kbuild test robot
2017-05-28  2:44     ` David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 4/9] net: add extack arg to lwtunnel build state David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 5/9] net: Fill in extack for mpls lwt encap David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 6/9] net: mpls: Pull common label check into helper David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 7/9] net: mpls: Add extack messages for route add and delete failures David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 8/9] net: mpls: Make nla_get_via in af_mpls.c David Ahern
2017-05-27 22:19 ` [PATCH v2 net-next 9/9] net: mpls: remove unnecessary initialization of err David Ahern
2017-05-30 15:55 ` [PATCH v2 net-next 0/9] net: another round of extack handling for routing David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170527221933.57644-2-dsahern@gmail.com \
    --to=dsahern@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=roopa@cumulusnetworks.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).