summaryrefslogtreecommitdiffstats
path: root/net/ipv4/fib_trie.c
diff options
context:
space:
mode:
authorDavid Ahern <dsahern@gmail.com>2017-05-28 00:19:25 +0200
committerDavid S. Miller <davem@davemloft.net>2017-05-30 17:55:31 +0200
commitba277e8e05dbd4aa13f74f859e276d5d54467eab (patch)
tree6088f00fefb073abbc271ca759a29621617469e4 /net/ipv4/fib_trie.c
parentMerge branch 'nfp-pci-core-hwmon-live-mac-addr-change' (diff)
downloadlinux-ba277e8e05dbd4aa13f74f859e276d5d54467eab.tar.xz
linux-ba277e8e05dbd4aa13f74f859e276d5d54467eab.zip
net: ipv4: refactor key and length checks
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> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/ipv4/fib_trie.c')
-rw-r--r--net/ipv4/fib_trie.c25
1 files 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);