[PATCH] net: fib: fix fib_new_table() logical issue

Subsystems: networking [general], networking [ipv4/ipv6], the rest

STALE3408d

3 messages, 3 authors, 2017-06-21 · open the first message on its own page

[PATCH] net: fib: fix fib_new_table() logical issue

From: yuan linyu <hidden>
Date: 2017-06-21 14:03:12

From: yuan linyu <redacted>

when CONFIG_IP_MULTIPLE_TABLES defined,
if id == RT_TABLE_LOCAL and !net->ipv4.fib_has_custom_rules,
fib_new_table() call itself to get RT_TABLE_MAIN table,
but if RT_TABLE_MAIN table not exist at this point,
this function will become deadloop.

the solution is move RT_TABLE_MAIN table create to per-net init.
and call fib_get_table() to get RT_TABLE_MAIN table.

Signed-off-by: yuan linyu <redacted>
---
 net/ipv4/fib_frontend.c | 12 ++----------
 net/ipv4/fib_rules.c    | 11 +++++++++++
 2 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index 4e678fa..4606b4a 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -87,22 +87,14 @@ struct fib_table *fib_new_table(struct net *net, u32 id)
 		return tb;
 
 	if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
-		alias = fib_new_table(net, RT_TABLE_MAIN);
+		alias = fib_get_table(net, RT_TABLE_MAIN);
 
 	tb = fib_trie_table(id, alias);
 	if (!tb)
 		return NULL;
 
-	switch (id) {
-	case RT_TABLE_MAIN:
-		rcu_assign_pointer(net->ipv4.fib_main, tb);
-		break;
-	case RT_TABLE_DEFAULT:
+	if (id == RT_TABLE_DEFAULT)
 		rcu_assign_pointer(net->ipv4.fib_default, tb);
-		break;
-	default:
-		break;
-	}
 
 	h = id & (FIB_TABLE_HASHSZ - 1);
 	hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
diff --git a/net/ipv4/fib_rules.c b/net/ipv4/fib_rules.c
index 778ecf9..a9796ac 100644
--- a/net/ipv4/fib_rules.c
+++ b/net/ipv4/fib_rules.c
@@ -402,8 +402,14 @@ static int fib_default_rules_init(struct fib_rules_ops *ops)
 int __net_init fib4_rules_init(struct net *net)
 {
 	int err;
+	unsigned int h;
+	struct fib_table *main_table;
 	struct fib_rules_ops *ops;
 
+	main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
+	if (!main_table)
+		return -ENOMEM;
+
 	ops = fib_rules_register(&fib4_rules_ops_template, net);
 	if (IS_ERR(ops))
 		return PTR_ERR(ops);
@@ -413,11 +419,16 @@ int __net_init fib4_rules_init(struct net *net)
 		goto fail;
 	net->ipv4.rules_ops = ops;
 	net->ipv4.fib_has_custom_rules = false;
+
+	rcu_assign_pointer(net->ipv4.fib_main, main_table);
+	h = RT_TABLE_MAIN & (FIB_TABLE_HASHSZ - 1);
+	hlist_add_head_rcu(&main_table->tb_hlist, &net->ipv4.fib_table_hash[h]);
 	return 0;
 
 fail:
 	/* also cleans all rules already added */
 	fib_rules_unregister(ops);
+	fib_free_table(main_table);
 	return err;
 }
 
-- 
2.7.4

Re: [PATCH] net: fib: fix fib_new_table() logical issue

From: Cong Wang <hidden>
Date: 2017-06-21 22:54:14

On Wed, Jun 21, 2017 at 7:02 AM, yuan linyu [off-list ref] wrote:
From: yuan linyu <redacted>

when CONFIG_IP_MULTIPLE_TABLES defined,
if id == RT_TABLE_LOCAL and !net->ipv4.fib_has_custom_rules,
fib_new_table() call itself to get RT_TABLE_MAIN table,
but if RT_TABLE_MAIN table not exist at this point,
this function will become deadloop.
Why deadloop here? If fib_new_table() is called recursively,
then main table is created by fib_trie_table() and we return.

RE: [PATCH] net: fib: fix fib_new_table() logical issue

From: YUAN Linyu <hidden>
Date: 2017-06-21 23:48:47

Sorry, it's not dead loop, I realized it but I tried to send to mail to note it on my phone, it failed. 
-----Original Message-----
From: Cong Wang [mailto:xiyou.wangcong@gmail.com]
Sent: Thursday, June 22, 2017 6:54 AM
To: yuan linyu
Cc: Linux Kernel Network Developers; David S . Miller; YUAN Linyu
Subject: Re: [PATCH] net: fib: fix fib_new_table() logical issue

On Wed, Jun 21, 2017 at 7:02 AM, yuan linyu [off-list ref] wrote:
quoted
From: yuan linyu <redacted>

when CONFIG_IP_MULTIPLE_TABLES defined,
if id == RT_TABLE_LOCAL and !net->ipv4.fib_has_custom_rules,
fib_new_table() call itself to get RT_TABLE_MAIN table,
but if RT_TABLE_MAIN table not exist at this point,
this function will become deadloop.
Why deadloop here? If fib_new_table() is called recursively,
then main table is created by fib_trie_table() and we return.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help