Skip to contentSkip to conversation

Simplify netfilter and network namespaces

PATCH In review Eric W. Biederman 6 participants 90 messages Jun 15, 2015 – Jul 16, 2015 Page 2 of 2

Subsystems (13)
lore
Share & tools
lore
#57 lore raw

PATCH 33/43 netfilter: ebtables: adapt the filter and nat table to pernet hooks

In reply to Eric W. Biederman

From: Pablo Neira Ayuso <[email protected]>

This adapts the filter and nat tables to register the hooks for each netnamespace.

---
 net/bridge/netfilter/ebtable_filter.c | 25 +++++++++++++------------
 net/bridge/netfilter/ebtable_nat.c    | 24 ++++++++++++------------
 2 files changed, 25 insertions(+), 24 deletions(-)
net/bridge/netfilter/ebtable_filter.c +13 -12
index a3dc249945ec..514273f949c0 100644
@@ -96,12 +96,23 @@ static struct nf_hook_ops ebt_ops_filter[] __read_mostly = {
 
 static int __net_init frame_filter_net_init(struct net *net)
 {
+	int ret;
+
 	net->xt.frame_filter = ebt_register_table(net, &frame_filter);
-	return PTR_ERR_OR_ZERO(net->xt.frame_filter);
+	if (IS_ERR(net->xt.frame_filter))
+		return PTR_ERR(net->xt.frame_filter);
+
+	ret = nf_register_hooks(net, ebt_ops_filter,
+				ARRAY_SIZE(ebt_ops_filter));
+	if (ret < 0)
+		ebt_unregister_table(net, net->xt.frame_filter);
+
+	return ret;
 }
 
 static void __net_exit frame_filter_net_exit(struct net *net)
 {
+	nf_unregister_hooks(net, ebt_ops_filter, ARRAY_SIZE(ebt_ops_filter));
 	ebt_unregister_table(net, net->xt.frame_filter);
 }
 
@@ -112,21 +123,11 @@ static struct pernet_operations frame_filter_net_ops = {
 
 static int __init ebtable_filter_init(void)
 {
-	int ret;
-
-	ret = register_pernet_subsys(&frame_filter_net_ops);
-	if (ret < 0)
-		return ret;
-	ret = nf_register_hooks(&init_net, ebt_ops_filter,
-				ARRAY_SIZE(ebt_ops_filter));
-	if (ret < 0)
-		unregister_pernet_subsys(&frame_filter_net_ops);
-	return ret;
+	return register_pernet_subsys(&frame_filter_net_ops);
 }
 
 static void __exit ebtable_filter_fini(void)
 {
-	nf_unregister_hooks(&init_net, ebt_ops_filter, ARRAY_SIZE(ebt_ops_filter));
 	unregister_pernet_subsys(&frame_filter_net_ops);
 }
 
net/bridge/netfilter/ebtable_nat.c +12 -12
index 11bf447f8b46..2dcd19c7d078 100644
@@ -96,12 +96,22 @@ static struct nf_hook_ops ebt_ops_nat[] __read_mostly = {
 
 static int __net_init frame_nat_net_init(struct net *net)
 {
+	int ret;
+
 	net->xt.frame_nat = ebt_register_table(net, &frame_nat);
-	return PTR_ERR_OR_ZERO(net->xt.frame_nat);
+	if (IS_ERR(net->xt.frame_nat))
+		return PTR_ERR(net->xt.frame_nat);
+
+	ret = nf_register_hooks(net, ebt_ops_nat, ARRAY_SIZE(ebt_ops_nat));
+	if (ret < 0)
+		ebt_unregister_table(net, net->xt.frame_nat);
+
+	return ret;
 }
 
 static void __net_exit frame_nat_net_exit(struct net *net)
 {
+	nf_unregister_hooks(net, ebt_ops_nat, ARRAY_SIZE(ebt_ops_nat));
 	ebt_unregister_table(net, net->xt.frame_nat);
 }
 
@@ -112,21 +122,11 @@ static struct pernet_operations frame_nat_net_ops = {
 
 static int __init ebtable_nat_init(void)
 {
-	int ret;
-
-	ret = register_pernet_subsys(&frame_nat_net_ops);
-	if (ret < 0)
-		return ret;
-	ret = nf_register_hooks(&init_net, ebt_ops_nat,
-				ARRAY_SIZE(ebt_ops_nat));
-	if (ret < 0)
-		unregister_pernet_subsys(&frame_nat_net_ops);
-	return ret;
+	return register_pernet_subsys(&frame_nat_net_ops);
 }
 
 static void __exit ebtable_nat_fini(void)
 {
-	nf_unregister_hooks(&init_net, ebt_ops_nat, ARRAY_SIZE(ebt_ops_nat));
 	unregister_pernet_subsys(&frame_nat_net_ops);
 }
 
Signature · 1 line
2.2.1
#58 lore raw

PATCH 34/43 netfilter: bridge: adapt it to pernet hooks

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

Since pernet hooks, we need to register the hook for each netnamespace space.

Based on a patch by Pablo Neira Ayuso <[email protected]> that was buggy and did a lot more.

---
 net/bridge/br_netfilter.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
net/bridge/br_netfilter.c +16 -2
index 15d0f3b78145..c6acff3a94ec 100644
@@ -1246,11 +1246,26 @@ static struct ctl_table brnf_table[] = {
 };
 #endif
 
+static int __net_init br_nf_net_init(struct net *net)
+{
+	return nf_register_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
+}
+
+static void __net_exit br_nf_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
+}
+
+static struct pernet_operations br_nf_net_ops = {
+	.init = br_nf_net_init,
+	.exit = br_nf_net_exit,
+};
+
 static int __init br_netfilter_init(void)
 {
 	int ret;
 
-	ret = nf_register_hooks(&init_net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
+	ret = register_pernet_subsys(&br_nf_net_ops);
 	if (ret < 0)
 		return ret;
 
@@ -1271,7 +1286,6 @@ static int __init br_netfilter_init(void)
 static void __exit br_netfilter_fini(void)
 {
 	RCU_INIT_POINTER(nf_br_ops, NULL);
-	nf_unregister_hooks(&init_net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
 #ifdef CONFIG_SYSCTL
 	unregister_net_sysctl_table(brnf_sysctl_header);
 #endif
Signature · 1 line
2.2.1
#59 lore raw

PATCH 35/43 ipvs: Register netfilter hooks in all network namespaces

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/netfilter/ipvs/ip_vs_core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
net/netfilter/ipvs/ip_vs_core.c +8 -10
index 1ab42bfe6020..2ea140c9a81c 100644
@@ -2005,6 +2005,9 @@ static int __net_init __ip_vs_init(struct net *net)
 	if (ip_vs_sync_net_init(net) < 0)
 		goto sync_fail;
 
+	if (nf_register_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops)))
+		goto hooks_fail;
+
 	printk(KERN_INFO "IPVS: Creating netns size=%zu id=%d\n",
 			 sizeof(struct netns_ipvs), ipvs->gen);
 	return 0;
@@ -2012,6 +2015,9 @@ static int __net_init __ip_vs_init(struct net *net)
  * Error handling
  */
 
+hooks_fail:
+	pr_err("can't register hooks.\n");
+	ip_vs_sync_net_cleanup(net);
 sync_fail:
 	ip_vs_conn_net_cleanup(net);
 conn_fail:
@@ -2029,6 +2035,7 @@ estimator_fail:
 
 static void __net_exit __ip_vs_cleanup(struct net *net)
 {
+	nf_unregister_hooks(net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
 	ip_vs_service_net_cleanup(net);	/* ip_vs_flush() with locks */
 	ip_vs_conn_net_cleanup(net);
 	ip_vs_app_net_cleanup(net);
@@ -2088,24 +2095,16 @@ static int __init ip_vs_init(void)
 	if (ret < 0)
 		goto cleanup_sub;
 
-	ret = nf_register_hooks(&init_net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
-	if (ret < 0) {
-		pr_err("can't register hooks.\n");
-		goto cleanup_dev;
-	}
-
 	ret = ip_vs_register_nl_ioctl();
 	if (ret < 0) {
 		pr_err("can't register netlink/ioctl.\n");
-		goto cleanup_hooks;
+		goto cleanup_dev;
 	}
 
 	pr_info("ipvs loaded.\n");
 
 	return ret;
 
-cleanup_hooks:
-	nf_unregister_hooks(&init_net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
 cleanup_dev:
 	unregister_pernet_device(&ipvs_core_dev_ops);
 cleanup_sub:
@@ -2122,7 +2121,6 @@ exit:
 static void __exit ip_vs_cleanup(void)
 {
 	ip_vs_unregister_nl_ioctl();
-	nf_unregister_hooks(&init_net, ip_vs_ops, ARRAY_SIZE(ip_vs_ops));
 	unregister_pernet_device(&ipvs_core_dev_ops);
 	unregister_pernet_subsys(&ipvs_core_ops);	/* free ip_vs struct */
 	ip_vs_conn_cleanup();
Signature · 1 line
2.2.1
#60 lore raw

PATCH 36/43 netfilter: nf_conntract: Register netfilter hooks in all network namespaces

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 22 +++++++++++-----------
 net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 24 ++++++++++++------------
 2 files changed, 23 insertions(+), 23 deletions(-)
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +11 -11
index abf6f60e80fe..735a32dace73 100644
@@ -424,7 +424,15 @@ static int ipv4_net_init(struct net *net)
 		pr_err("nf_conntrack_ipv4: pernet registration failed\n");
 		goto out_ipv4;
 	}
+	ret = nf_register_hooks(net, ipv4_conntrack_ops,
+				ARRAY_SIZE(ipv4_conntrack_ops));
+	if (ret < 0) {
+		pr_err("nf_conntrack_ipv4: can't register hooks.\n");
+		goto out_hooks;
+	}
 	return 0;
+out_hooks:
+	nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv4);
 out_ipv4:
 	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
 out_icmp:
@@ -437,6 +445,8 @@ out_tcp:
 
 static void ipv4_net_exit(struct net *net)
 {
+	nf_unregister_hooks(net, ipv4_conntrack_ops,
+			    ARRAY_SIZE(ipv4_conntrack_ops));
 	nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv4);
 	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
 	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
@@ -467,17 +477,10 @@ static int __init nf_conntrack_l3proto_ipv4_init(void)
 		goto cleanup_sockopt;
 	}
 
-	ret = nf_register_hooks(&init_net, ipv4_conntrack_ops,
-				ARRAY_SIZE(ipv4_conntrack_ops));
-	if (ret < 0) {
-		pr_err("nf_conntrack_ipv4: can't register hooks.\n");
-		goto cleanup_pernet;
-	}
-
 	ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp4);
 	if (ret < 0) {
 		pr_err("nf_conntrack_ipv4: can't register tcp4 proto.\n");
-		goto cleanup_hooks;
+		goto cleanup_pernet;
 	}
 
 	ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp4);
@@ -514,8 +517,6 @@ static int __init nf_conntrack_l3proto_ipv4_init(void)
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
  cleanup_tcp4:
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
- cleanup_hooks:
-	nf_unregister_hooks(&init_net, ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
  cleanup_pernet:
 	unregister_pernet_subsys(&ipv4_net_ops);
  cleanup_sockopt:
@@ -533,7 +534,6 @@ static void __exit nf_conntrack_l3proto_ipv4_fini(void)
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
-	nf_unregister_hooks(&init_net, ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
 	unregister_pernet_subsys(&ipv4_net_ops);
 	nf_unregister_sockopt(&so_getorigdst);
 }
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +12 -12
index 710ed6607e66..5823956fa130 100644
@@ -366,7 +366,16 @@ static int ipv6_net_init(struct net *net)
 		pr_err("nf_conntrack_ipv6: pernet registration failed.\n");
 		goto cleanup_icmpv6;
 	}
+	ret = nf_register_hooks(net, ipv6_conntrack_ops,
+				ARRAY_SIZE(ipv6_conntrack_ops));
+	if (ret < 0) {
+		pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
+		       "hook.\n");
+		goto cleanup_hooks;
+	}
 	return 0;
+cleanup_hooks:
+	nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6);
  cleanup_icmpv6:
 	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
  cleanup_udp6:
@@ -379,6 +388,8 @@ static int ipv6_net_init(struct net *net)
 
 static void ipv6_net_exit(struct net *net)
 {
+	nf_unregister_hooks(net, ipv6_conntrack_ops,
+			    ARRAY_SIZE(ipv6_conntrack_ops));
 	nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6);
 	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
 	nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
@@ -407,18 +418,10 @@ static int __init nf_conntrack_l3proto_ipv6_init(void)
 	if (ret < 0)
 		goto cleanup_sockopt;
 
-	ret = nf_register_hooks(&init_net, ipv6_conntrack_ops,
-				ARRAY_SIZE(ipv6_conntrack_ops));
-	if (ret < 0) {
-		pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
-		       "hook.\n");
-		goto cleanup_pernet;
-	}
-
 	ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp6);
 	if (ret < 0) {
 		pr_err("nf_conntrack_ipv6: can't register tcp6 proto.\n");
-		goto cleanup_hooks;
+		goto cleanup_pernet;
 	}
 
 	ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp6);
@@ -446,8 +449,6 @@ static int __init nf_conntrack_l3proto_ipv6_init(void)
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
  cleanup_tcp6:
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
- cleanup_hooks:
-	nf_unregister_hooks(&init_net, ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
  cleanup_pernet:
 	unregister_pernet_subsys(&ipv6_net_ops);
  cleanup_sockopt:
@@ -462,7 +463,6 @@ static void __exit nf_conntrack_l3proto_ipv6_fini(void)
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
 	nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
-	nf_unregister_hooks(&init_net, ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
 	unregister_pernet_subsys(&ipv6_net_ops);
 	nf_unregister_sockopt(&so_getorigdst6);
 }
Signature · 1 line
2.2.1
#61 lore raw

PATCH 37/43 netfilter: nf_defrag: Register netfilter hooks in all network namespaces

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/ipv4/netfilter/nf_defrag_ipv4.c       | 21 ++++++++++++++++++---
 net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 21 ++++++++++++++++++---
 2 files changed, 36 insertions(+), 6 deletions(-)
net/ipv4/netfilter/nf_defrag_ipv4.c +18 -3
index e9f4088d4759..8d5a45ddf9b5 100644
@@ -109,15 +109,30 @@ static struct nf_hook_ops ipv4_defrag_ops[] = {
 	},
 };
 
-static int __init nf_defrag_init(void)
+static int __net_init nf_defrag_net_init(struct net *net)
 {
-	return nf_register_hooks(&init_net, ipv4_defrag_ops,
+	return nf_register_hooks(net, ipv4_defrag_ops,
 				 ARRAY_SIZE(ipv4_defrag_ops));
 }
 
+static void __net_exit nf_defrag_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
+}
+
+static struct pernet_operations nf_defrag_net_ops = {
+	.init = nf_defrag_net_init,
+	.exit = nf_defrag_net_exit,
+};
+
+static int __init nf_defrag_init(void)
+{
+	return register_pernet_subsys(&nf_defrag_net_ops);
+}
+
 static void __exit nf_defrag_fini(void)
 {
-	nf_unregister_hooks(&init_net, ipv4_defrag_ops, ARRAY_SIZE(ipv4_defrag_ops));
+	unregister_pernet_subsys(&nf_defrag_net_ops);
 }
 
 void nf_defrag_ipv4_enable(void)
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +18 -3
index 2bd7aa696382..a20d461fbd89 100644
@@ -100,6 +100,22 @@ static struct nf_hook_ops ipv6_defrag_ops[] = {
 	},
 };
 
+static int __net_init nf_defrag_net_init(struct net *net)
+{
+	return nf_register_hooks(net, ipv6_defrag_ops,
+				 ARRAY_SIZE(ipv6_defrag_ops));
+}
+
+static void __net_exit nf_defrag_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, ipv6_defrag_ops, ARRAY_SIZE(ipv6_defrag_ops));
+}
+
+static struct pernet_operations nf_defrag_net_ops = {
+	.init = nf_defrag_net_init,
+	.exit = nf_defrag_net_exit,
+};
+
 static int __init nf_defrag_init(void)
 {
 	int ret = 0;
@@ -109,8 +125,7 @@ static int __init nf_defrag_init(void)
 		pr_err("nf_defrag_ipv6: can't initialize frag6.\n");
 		return ret;
 	}
-	ret = nf_register_hooks(&init_net, ipv6_defrag_ops,
-				ARRAY_SIZE(ipv6_defrag_ops));
+	ret = register_pernet_subsys(&nf_defrag_net_ops);
 	if (ret < 0) {
 		pr_err("nf_defrag_ipv6: can't register hooks\n");
 		goto cleanup_frag6;
@@ -125,7 +140,7 @@ cleanup_frag6:
 
 static void __exit nf_defrag_fini(void)
 {
-	nf_unregister_hooks(&init_net, ipv6_defrag_ops, ARRAY_SIZE(ipv6_defrag_ops));
+	unregister_pernet_subsys(&nf_defrag_net_ops);
 	nf_ct_frag6_cleanup();
 }
 
Signature · 1 line
2.2.1
#62 lore raw

PATCH 38/43 netfilter: synproxy: Register netfilter hooks in all network namespaces

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/ipv4/netfilter/ipt_SYNPROXY.c  | 24 ++++++++++++++++++++----
 net/ipv6/netfilter/ip6t_SYNPROXY.c | 24 ++++++++++++++++++++----
 2 files changed, 40 insertions(+), 8 deletions(-)
net/ipv4/netfilter/ipt_SYNPROXY.c +20 -4
index 301bb886a289..cb84e4e24175 100644
@@ -446,12 +446,28 @@ static struct nf_hook_ops ipv4_synproxy_ops[] __read_mostly = {
 	},
 };
 
+static int synproxy_tg4_net_init(struct net *net)
+{
+	return nf_register_hooks(net, ipv4_synproxy_ops,
+				 ARRAY_SIZE(ipv4_synproxy_ops));
+}
+
+static void synproxy_tg4_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, ipv4_synproxy_ops,
+			    ARRAY_SIZE(ipv4_synproxy_ops));
+}
+
+static struct pernet_operations synproxy_tg4_net_ops = {
+	.init = synproxy_tg4_net_init,
+	.exit = synproxy_tg4_net_exit,
+};
+
 static int __init synproxy_tg4_init(void)
 {
 	int err;
 
-	err = nf_register_hooks(&init_net, ipv4_synproxy_ops,
-				ARRAY_SIZE(ipv4_synproxy_ops));
+	err = register_pernet_subsys(&synproxy_tg4_net_ops);
 	if (err < 0)
 		goto err1;
 
@@ -462,7 +478,7 @@ static int __init synproxy_tg4_init(void)
 	return 0;
 
 err2:
-	nf_unregister_hooks(&init_net, ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
+	unregister_pernet_subsys(&synproxy_tg4_net_ops);
 err1:
 	return err;
 }
@@ -470,7 +486,7 @@ err1:
 static void __exit synproxy_tg4_exit(void)
 {
 	xt_unregister_target(&synproxy_tg4_reg);
-	nf_unregister_hooks(&init_net, ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
+	unregister_pernet_subsys(&synproxy_tg4_net_ops);
 }
 
 module_init(synproxy_tg4_init);
net/ipv6/netfilter/ip6t_SYNPROXY.c +20 -4
index 320521086b5c..0acc786fd3f0 100644
@@ -469,12 +469,28 @@ static struct nf_hook_ops ipv6_synproxy_ops[] __read_mostly = {
 	},
 };
 
+static int synproxy_tg6_net_init(struct net *net)
+{
+	return nf_register_hooks(net, ipv6_synproxy_ops,
+				 ARRAY_SIZE(ipv6_synproxy_ops));
+}
+
+static void synproxy_tg6_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, ipv6_synproxy_ops,
+			    ARRAY_SIZE(ipv6_synproxy_ops));
+}
+
+static struct pernet_operations synproxy_tg6_net_ops = {
+	.init = synproxy_tg6_net_init,
+	.exit = synproxy_tg6_net_exit,
+};
+
 static int __init synproxy_tg6_init(void)
 {
 	int err;
 
-	err = nf_register_hooks(&init_net, ipv6_synproxy_ops,
-				ARRAY_SIZE(ipv6_synproxy_ops));
+	err = register_pernet_subsys(&synproxy_tg6_net_ops);
 	if (err < 0)
 		goto err1;
 
@@ -485,7 +501,7 @@ static int __init synproxy_tg6_init(void)
 	return 0;
 
 err2:
-	nf_unregister_hooks(&init_net, ipv6_synproxy_ops, ARRAY_SIZE(ipv6_synproxy_ops));
+	unregister_pernet_subsys(&synproxy_tg6_net_ops);
 err1:
 	return err;
 }
@@ -493,7 +509,7 @@ err1:
 static void __exit synproxy_tg6_exit(void)
 {
 	xt_unregister_target(&synproxy_tg6_reg);
-	nf_unregister_hooks(&init_net, ipv6_synproxy_ops, ARRAY_SIZE(ipv6_synproxy_ops));
+	unregister_pernet_subsys(&synproxy_tg6_net_ops);
 }
 
 module_init(synproxy_tg6_init);
Signature · 1 line
2.2.1
#63 lore raw

PATCH 39/43 selinux: adapt it to pernet hooks

In reply to Eric W. Biederman

From: Pablo Neira Ayuso <[email protected]>

Since pernet hooks, we need to register the hook for each netnamespace space.

---
 security/selinux/hooks.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
security/selinux/hooks.c +18 -3
index 21a5e07b6834..f8db1d2ce9c2 100644
@@ -6147,6 +6147,22 @@ static struct nf_hook_ops selinux_nf_ops[] = {
 #endif	/* IPV6 */
 };
 
+static int selinux_net_init(struct net *net)
+{
+	return nf_register_hooks(net, selinux_nf_ops,
+				 ARRAY_SIZE(selinux_nf_ops));
+}
+
+static void selinux_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
+}
+
+static struct pernet_operations selinux_net_ops = {
+	.init = selinux_net_init,
+	.exit = selinux_net_exit,
+};
+
 static int __init selinux_nf_ip_init(void)
 {
 	int err;
@@ -6156,8 +6172,7 @@ static int __init selinux_nf_ip_init(void)
 
 	printk(KERN_DEBUG "SELinux:  Registering netfilter hooks\n");
 
-	err = nf_register_hooks(&init_net, selinux_nf_ops,
-				ARRAY_SIZE(selinux_nf_ops));
+	err = register_pernet_subsys(&selinux_net_ops);
 	if (err)
 		panic("SELinux: nf_register_hooks: error %d\n", err);
 
@@ -6171,7 +6186,7 @@ static void selinux_nf_ip_exit(void)
 {
 	printk(KERN_DEBUG "SELinux:  Unregistering netfilter hooks\n");
 
-	nf_unregister_hooks(&init_net, selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
+	unregister_pernet_subsys(&selinux_net_ops);
 }
 #endif
 
Signature · 1 line
2.2.1
#64 lore raw

PATCH 40/43 smack: adapt it to pernet hooks

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

Since pernet hooks, we need to register the hook for each netnamespace space.

---
 security/smack/smack_netfilter.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
security/smack/smack_netfilter.c +16 -2
index 0646fe5eda77..fcb3172adb75 100644
@@ -73,6 +73,21 @@ static struct nf_hook_ops smack_nf_ops[] = {
 #endif	/* IPV6 */
 };
 
+static int smack_net_init(struct net *net)
+{
+	return nf_register_hooks(net, smack_nf_ops, ARRAY_SIZE(smack_nf_ops));
+}
+
+static void smack_net_exit(struct net *net)
+{
+	nf_unregister_hooks(net, smack_nf_ops, ARRAY_SIZE(smack_nf_ops));
+}
+
+static struct pernet_operations smack_net_ops = {
+	.init = smack_net_init,
+	.exit = smack_net_exit,
+};
+
 static int __init smack_nf_ip_init(void)
 {
 	int err;
@@ -82,8 +97,7 @@ static int __init smack_nf_ip_init(void)
 
 	printk(KERN_DEBUG "Smack: Registering netfilter hooks\n");
 
-	err = nf_register_hooks(&init_net, smack_nf_ops,
-				ARRAY_SIZE(smack_nf_ops));
+	err = register_pernet_subsys(&smack_net_ops);
 	if (err)
 		pr_info("Smack: nf_register_hooks: error %d\n", err);
 
Signature · 1 line
2.2.1
#65 lore raw

PATCH 41/43 netfilter: Remove the network namespace Kconfig conflict

In reply to Eric W. Biederman

Now that the nf_register_hook conversion is complete netfilter should work just fine with network namespaces remove the temporary Kconfig conflict.

---
 init/Kconfig | 1 -
 1 file changed, 1 deletion(-)
init/Kconfig +0 -1
index 166cfbfdc2df..dc24dec60232 100644
@@ -1207,7 +1207,6 @@ config PID_NS
 config NET_NS
 	bool "Network namespace"
 	depends on NET
-	depends on !NETFILTER
 	default y
 	help
 	  Allow user space to create what appear to be multiple instances
Signature · 1 line
2.2.1
#66 lore raw

PATCH 42/43 netfilter bridge: Make the sysctl knobs per network namespace

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

This patch replaces the global brnf_call_* variables to make them per network namespace.

Based on an early patch by Pablo Neira Ayuso <[email protected]>

---
 include/net/netns/netfilter.h |  11 +++
 net/bridge/br_netfilter.c     | 151 +++++++++++++++++++++++++-----------------
 2 files changed, 103 insertions(+), 59 deletions(-)
include/net/netns/netfilter.h +11 -0
index 38aa4983e2a9..181b13e1390f 100644
@@ -15,5 +15,16 @@ struct netns_nf {
 	struct ctl_table_header *nf_log_dir_header;
 #endif
 	struct list_head hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
+#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
+	int brnf_call_iptables;
+	int brnf_call_ip6tables;
+	int brnf_call_arptables;
+	int brnf_filter_vlan_tagged;
+	int brnf_filter_pppoe_tagged;
+	int brnf_pass_vlan_indev;
+#ifdef CONFIG_SYSCTL
+	struct ctl_table_header *brnf_sysctl_header;
+#endif
+#endif
 };
 #endif
net/bridge/br_netfilter.c +92 -59

291 lines not on this page · raw message

Signature · 1 line
2.2.1
#67 lore raw

PATCH 43/43 netfilter: Skip unnecessary calls to synchronize_net

In reply to Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/netfilter/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/core.c +3 -1
index 95456c09cf69..1b4eadc9c030 100644
@@ -134,7 +134,9 @@ void nf_unregister_hook(struct net *net, const struct nf_hook_ops *reg)
 #ifdef HAVE_JUMP_LABEL
 	static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
 #endif
-	synchronize_net();
+	/* Don't wait if there are no packets in flight */
+	if (net->loopback_dev)
+		synchronize_net();
 	kfree(elem);
 }
 EXPORT_SYMBOL(nf_unregister_hook);
Signature · 1 line
2.2.1
#68 lore raw

Re: [PATCH net-next 43/43] netfilter: Skip unnecessary calls to synchronize_net

Quoted 12 lines · Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/netfilter/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/core.c +0 -0
index 95456c09cf69..1b4eadc9c030 100644

net/netfilter/core.c void nf_unregister_hook(struct net *net, const struct nf_hook_ops *reg) · line 139

Quoted 7 lines
net/netfilter/core.c +3 -1
@@ -134,7 +134,9 @@ void nf_unregister_hook(struct net *net, const struct nf_hook_ops *reg)
 #ifdef HAVE_JUMP_LABEL
 	static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
 #endif
-	synchronize_net();
+	/* Don't wait if there are no packets in flight */
+	if (net->loopback_dev)
+		synchronize_net();

I don't get this, could you please explain why there wouldn't be any packets in flight if there is no loopback_dev?

Quoted 6 lines
net/netfilter/core.c +0 -0
 	kfree(elem);
 }
 EXPORT_SYMBOL(nf_unregister_hook);
Signature · 1 line
2.2.1
#69 lore raw

Re: [PATCH net-next 43/43] netfilter: Skip unnecessary calls to synchronize_net

Quoted 8 lines · Patrick McHardy
Quoted 7 lines · Eric W. Biederman

From: Eric W Biederman <[email protected]>

---
 net/netfilter/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

net/netfilter/core.c void nf_unregister_hook(struct net *net, const struct nf_hook_ops *reg) · line 139

Quoted 12 lines
net/netfilter/core.c +3 -1
index 95456c09cf69..1b4eadc9c030 100644
@@ -134,7 +134,9 @@ void nf_unregister_hook(struct net *net, const struct nf_hook_ops *reg)
 #ifdef HAVE_JUMP_LABEL
 	static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
 #endif
-	synchronize_net();
+	/* Don't wait if there are no packets in flight */
+	if (net->loopback_dev)
+		synchronize_net();

I don't get this, could you please explain why there wouldn't be any packets in flight if there is no loopback_dev?

The simplified version.

Typical netfilter module:

Quoted 16 lines
static struct nf_hook_ops mod_ops[] __read_mostly = {
	...
};
static int __net_init mod_init(struct net *net)
{
        return nf_register_hooks(net, mod_ops, ARRAY_SIZE(mod_ops));
}
static void __net_exit mod_net_exit(struct net *net)
{
	nf_unregister_hooks(net, mod_ops, ARRAY_SIZE(mod_ops));
}

static struct pernet_operations mod_net_ops = {
.init = mod_net_init,
.exit = mod_net_exit,
};
static int __init mod_init(void)

Quoted 7 lines
{
	return register_pernet_subsys(&mod_net_ops);
}
static void __exit mod_fini(void)
{
	unregister_pernet_subsys(&mod_net_ops);
}

Which means there are essentially two times when nf_unregister_hook is called:
- At some random time when the netfilter module is being removed.
- From unregister_pernet_subsys.

It is an invariant of subsys code called from the network namespace exit path that no packets are in flight. Without that invariant it is a nightmare to clean up data structures, etc.

The last thing that happens before the network namespace subsystem exit routines are called is the loopback device is freed.

That happens in default_device_exit_batch in the rtnl_unlock() call.

Another way to look at it is that: I know there are no user space sockets, and I know that there are no networking devices by the time the loopback device is freed as part of network namespace clean up. Which removes all sources of packets.

Additionally we have to perform all of the rcu waits before we can free any network device and with the loopback device being the last network device freed those waits have all been performed.

The network stack also goes kaboom in the most interesting ways when we goof up and violate the rule that guarantee that packets are not in flight when unregistering. I have not seen that in years.

And my older documentation in net_namespace.h says:

Quoted 15 lines

/*
* Use these carefully. If you implement a network device and it
* needs per network namespace operations use device pernet operations,
* otherwise use pernet subsys operations.
*
* Network interfaces need to be removed from a dying netns _before_
* subsys notifiers can be called, as most of the network code cleanup
* (which is done from subsys notifiers) runs with the assumption that
* dev_remove_pack has been called so no new packets will arrive during
* and after the cleanup functions have been called. dev_remove_pack
* is not per namespace so instead the guarantee of no more packets
* arriving in a network namespace is provided by ensuring that all
* network devices and all sockets have left the network namespace
* before the cleanup methods are called.
*

Quoted 8 lines

* For the longest time the ipv4 icmp code was registered as a pernet
* device which caused kernel oops, and panics during network
* namespace cleanup. So please don't get this wrong.
*/
int register_pernet_subsys(struct pernet_operations *); void unregister_pernet_subsys(struct pernet_operations *); int register_pernet_device(struct pernet_operations *); void unregister_pernet_device(struct pernet_operations *);

Another more explicit way of looking at this in the context of netfilter:
- The loopback device is free (which is the mast network device to be freed) so there are no more network devices.
- Therefore the netfilter tracepoints can no longer be called.
- Therefore the worst we are dealing with is someone accessing a network device via an rcu.
- The rcu_barrier() after NETDEV_UNREGISTER in netdev_run_todo() is there so that a network device can assume that it has no rcu references from packets or other things.
- We know the loopback device and all other network devices before it have passed that rcu_barrier in netdev_run_todo().

- Or in short: loopback_dev == NULL means we have no network devices and an rcu grace perioed has elapsed since the last network device was freed.

Therefore loopback_dev == NULL is a strong and reliable indication that we don't have any packets in flight in a network namespace.

Hmm. I wonder if we might want to implement a function:
void syncrhonize_net_namespace(struct net *net)

{
	if (net->loopback_dev)
		synchronize_net();
}

Just to make it easier to take advantage of this knowledge.

What I do know is that without taking advantage of the fact we have no packets in flight. We will get a ton of synrhonize_net calls in the network namespace destruction with this change which will slow the connection rates of vsftp to a crawl as it will try to create a new network namespace for a new connection and have to wait until all of the pending network namespace destructions have completed. Sigh.

So I put in this code to prevent a nasty performance regeression.

Eric

#70 lore raw

Re: [PATCH net-next 00/43] Simplify netfilter and network namespaces (take 2)

In reply to Eric W. Biederman

----- Original Message -----

Quoted 2 lines

From: "Eric W. Biederman" <[email protected]>
Subject: [PATCH net-next 00/43] Simplify netfilter and network namespaces (take 2)

After all the chains, including the basechains, are now per netns, it should be possible to remove pnet from basechain.

like this????

include/net/netfilter/nf_tables.h +4 -4
index 8a61d8c..91bfded 100644
@@ -788,7 +788,6 @@ struct nft_stats {
  *     struct nft_base_chain - nf_tables base chain
  *
  *     @ops: netfilter hook ops
- *     @pnet: net namespace that this chain belongs to
  *     @type: chain type
  *     @policy: default policy
  *     @stats: per-cpu chain stats
@@ -797,7 +796,6 @@ struct nft_stats {
  */
 struct nft_base_chain {
        struct nf_hook_ops              ops[NFT_HOOK_OPS_MAX];
-       possible_net_t                  pnet;
        const struct nf_chain_type      *type;
        u8                              policy;
        u8                              flags;
@@ -811,9 +809,11 @@ static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chai
        return container_of(chain, struct nft_base_chain, chain);
 }
 
-int nft_register_basechain(struct nft_base_chain *basechain,
+int nft_register_basechain(struct net *net,
+                          struct nft_base_chain *basechain,
                           unsigned int hook_nops);
-void nft_unregister_basechain(struct nft_base_chain *basechain,
+void nft_unregister_basechain(struct net *net,
+                             struct nft_base_chain *basechain,
                              unsigned int hook_nops);
 
 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
net/netfilter/nf_tables_api.c +19 -15
index ed9ef99..b0346be 100644
@@ -127,11 +127,10 @@ static void nft_trans_destroy(struct nft_trans *trans)
        kfree(trans);
 }
 
-int nft_register_basechain(struct nft_base_chain *basechain,
+int nft_register_basechain(struct net *net,
+                          struct nft_base_chain *basechain,
                           unsigned int hook_nops)
 {
-       struct net *net = read_pnet(&basechain->pnet);
-
        if (basechain->flags & NFT_BASECHAIN_DISABLED)
                return 0;
 
@@ -139,11 +138,10 @@ int nft_register_basechain(struct nft_base_chain *basechain,
 }
 EXPORT_SYMBOL_GPL(nft_register_basechain);
 
-void nft_unregister_basechain(struct nft_base_chain *basechain,
+void nft_unregister_basechain(struct net *net,
+                             struct nft_base_chain *basechain,
                              unsigned int hook_nops)
 {
-       struct net *net = read_pnet(&basechain->pnet);
-
        if (basechain->flags & NFT_BASECHAIN_DISABLED)
                return;
 
@@ -152,6 +150,7 @@ void nft_unregister_basechain(struct nft_base_chain *basechain,
 EXPORT_SYMBOL_GPL(nft_unregister_basechain);
 
 static int nf_tables_register_hooks(const struct nft_table *table,
+                                   struct net *net,
                                    struct nft_chain *chain,
                                    unsigned int hook_nops)
 {
@@ -159,10 +158,11 @@ static int nf_tables_register_hooks(const struct nft_table *table,
            !(chain->flags & NFT_BASE_CHAIN))
                return 0;
 
-       return nft_register_basechain(nft_base_chain(chain), hook_nops);
+       return nft_register_basechain(net, nft_base_chain(chain), hook_nops);
 }
 
 static void nf_tables_unregister_hooks(const struct nft_table *table,
+                                      struct net *net,
                                       struct nft_chain *chain,
                                       unsigned int hook_nops)
 {
@@ -170,7 +170,7 @@ static void nf_tables_unregister_hooks(const struct nft_table *table,
            !(chain->flags & NFT_BASE_CHAIN))
                return;
 
-       nft_unregister_basechain(nft_base_chain(chain), hook_nops);
+       nft_unregister_basechain(net, nft_base_chain(chain), hook_nops);
 }
 
 /* Internal table flags */
@@ -588,6 +588,7 @@ err:
 }
 
 static int nf_tables_table_enable(const struct nft_af_info *afi,
+                                 struct net *net,
                                  struct nft_table *table)
 {
        struct nft_chain *chain;
@@ -597,7 +598,7 @@ static int nf_tables_table_enable(const struct nft_af_info *afi,
                if (!(chain->flags & NFT_BASE_CHAIN))
                        continue;
 
-               err = nft_register_basechain(nft_base_chain(chain), afi->nops);
+               err = nft_register_basechain(net, nft_base_chain(chain), afi->nops);
                if (err < 0)
                        goto err;
 
@@ -612,19 +613,20 @@ err:
                if (i-- <= 0)
                        break;
 
-               nft_unregister_basechain(nft_base_chain(chain), afi->nops);
+               nft_unregister_basechain(net, nft_base_chain(chain), afi->nops);
        }
        return err;
 }
 
 static void nf_tables_table_disable(const struct nft_af_info *afi,
+                                   struct net *net,
                                    struct nft_table *table)
 {
        struct nft_chain *chain;
 
        list_for_each_entry(chain, &table->chains, list) {
                if (chain->flags & NFT_BASE_CHAIN)
-                       nft_unregister_basechain(nft_base_chain(chain),
+                       nft_unregister_basechain(net, nft_base_chain(chain),
                                                 afi->nops);
        }
 }
@@ -655,7 +657,7 @@ static int nf_tables_updtable(struct nft_ctx *ctx)
                nft_trans_table_enable(trans) = false;
        } else if (!(flags & NFT_TABLE_F_DORMANT) &&
                   ctx->table->flags & NFT_TABLE_F_DORMANT) {
-               ret = nf_tables_table_enable(ctx->afi, ctx->table);
+               ret = nf_tables_table_enable(ctx->afi, ctx->net, ctx->table);
                if (ret >= 0) {
                        ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
                        nft_trans_table_enable(trans) = true;
@@ -1426,7 +1428,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
                        rcu_assign_pointer(basechain->stats, stats);
                }
 
-               write_pnet(&basechain->pnet, net);
                basechain->type = type;
                chain = &basechain->chain;
 
@@ -1458,7 +1459,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
        chain->table = table;
        nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
 
-       err = nf_tables_register_hooks(table, chain, afi->nops);
+       err = nf_tables_register_hooks(table, net, chain, afi->nops);
        if (err < 0)
                goto err1;
 
@@ -1471,7 +1472,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
        list_add_tail_rcu(&chain->list, &table->chains);
        return 0;
 err2:
-       nf_tables_unregister_hooks(table, chain, afi->nops);
+       nf_tables_unregister_hooks(table, net, chain, afi->nops);
 err1:
        nf_tables_chain_destroy(chain);
        return err;
@@ -3911,6 +3912,7 @@ static int nf_tables_commit(struct sk_buff *skb)
                        if (nft_trans_table_update(trans)) {
                                if (!nft_trans_table_enable(trans)) {
                                        nf_tables_table_disable(trans->ctx.afi,
+                                                               net,
                                                                trans->ctx.table);
                                        trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
                                }
@@ -3935,6 +3937,7 @@ static int nf_tables_commit(struct sk_buff *skb)
                case NFT_MSG_DELCHAIN:
                        nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
                        nf_tables_unregister_hooks(trans->ctx.table,
                                                   trans->ctx.afi->nops);
                        break;
@@ -4037,6 +4040,7 @@ static int nf_tables_abort(struct sk_buff *skb)
                        if (nft_trans_table_update(trans)) {
                                if (nft_trans_table_enable(trans)) {
                                        nf_tables_table_disable(trans->ctx.afi,
+                                                               net,
                                                                trans->ctx.table);
                                        trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
                                }
@@ -4059,6 +4063,7 @@ static int nf_tables_abort(struct sk_buff *skb)
                                trans->ctx.table->use--;
                                list_del_rcu(&trans->ctx.chain->list);
                                nf_tables_unregister_hooks(trans->ctx.table,
+                                                          net,
                                                           trans->ctx.chain,
                                                           trans->ctx.afi->nops);
                        }
net/netfilter/nf_tables_core.c +1 -2
index 5f23b86..5928fa1 100644
@@ -112,7 +112,6 @@ unsigned int
 nft_do_chain(struct nft_pktinfo *pkt, void *priv)
 {
        const struct nft_chain *chain = priv, *basechain = chain;
-       const struct net *net = read_pnet(&nft_base_chain(basechain)->pnet);
        const struct nft_rule *rule;
        const struct nft_expr *expr, *last;
        struct nft_regs regs;
@@ -120,7 +119,7 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)
        struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
        struct nft_stats *stats;
        int rulenum;
-       unsigned int gencursor = nft_genmask_cur(net);
+       unsigned int gencursor = nft_genmask_cur(pkt->net);
 
 do_chain:
        rulenum = 0;
net/netfilter/nf_tables_netdev.c +2 -2
index 7b9c053..e72f119 100644
@@ -171,7 +171,7 @@ static void nft_netdev_event(unsigned long event, struct nft_af_info *afi,
                basechain->ops[0].dev = dev;
                basechain->flags &= ~NFT_BASECHAIN_DISABLED;
                if (!(table->flags & NFT_TABLE_F_DORMANT))
-                       nft_register_basechain(basechain, afi->nops);
+                       nft_register_basechain(dev_net(dev), basechain, afi->nops);
                break;
        case NETDEV_UNREGISTER:
                if (strcmp(basechain->dev_name, dev->name) != 0)
@@ -180,7 +180,7 @@ static void nft_netdev_event(unsigned long event, struct nft_af_info *afi,
                BUG_ON(basechain->flags & NFT_BASECHAIN_DISABLED);
 
                if (!(table->flags & NFT_TABLE_F_DORMANT))
-                       nft_unregister_basechain(basechain, afi->nops);
+                       nft_unregister_basechain(dev_net(dev), basechain, afi->nops);
 
                dev_put(basechain->ops[0].dev);
                basechain->ops[0].dev = NULL;

Andreas

#71 lore raw

Re: [PATCH net-next 00/43] Simplify netfilter and network namespaces (take 2)

In reply to Eric W. Biederman

On Wed, Jun 17, 2015 at 10:09:40AM -0500, Eric W. Biederman wrote:
[...]

Quoted 3 lines

There are a few extra cleanups in the first group of changes sprinkled in as I noticed a few other things as I was sorting out the network namespace computation logic.

This is a rather large patchset that address many pernet issues in the netfilter codebase, I would classify them in:

1) Patches to prepare the ground for easier pernet integration.

2) Get rid of the dev_net(dev) ? ... : ...; pattern all around the netfilter code.

3) Missing pernet sysctl support is some spots, eg. br_netfilter.

4) Pernet hooks, probably the largest changeset in this pile and the most important one IMO.

So given that it's quite evident that netfilter netns support is half-cooked and there's room for improvement in it, as we've been receiving patches to partially add support on things that people sporadically needed, could you please split this in several (smaller) batches in logical changes for easier review?

On a different front, nfnetlink_log and nfnetlink_queue also still lack of netns support so patches for that would be also appreciated in another different round.

I'm going to take as much of small preparation patches that I can to reduce your patchload:

1/43, 8/43, 16/43, 17/43, 18/43, 26/43

Thank you.

#72 lore raw

cover Per network namespace netfilter chains

In reply to Eric W. Biederman

By maintining a set of functions to register and unregister netfilter hooks both globally and per network namespace I have managed to write a compact patchset that maintain per network netfilter chains, and registers the nftables netfilter hooks per network namespace.

There are lots of other possible and desirable cleanups but this one is a core change needed to make the other changes independent small changes.

Eric W. Biederman (6):
netfilter: nf_queue: Don't recompute the hook_list head
netfilter: kill nf_hooks_active
netfilter: Simply the tests for enabling and disabling the ingress queue hook
netfilter: Factor out the hook list selection from nf_register_hook
netfilter: Per network namespace netfilter hooks.
netfilter: nftables: Only run the nftables chains in the proper netns

 include/linux/netfilter.h      |  23 +++--
 include/net/netns/netfilter.h  |   1 +
 net/netfilter/core.c           | 221 +++++++++++++++++++++++++++++++++--------
 net/netfilter/nf_queue.c       |   2 +-
 net/netfilter/nf_tables_api.c  |   6 +-
 net/netfilter/nf_tables_core.c |   5 -
 6 files changed, 200 insertions(+), 58 deletions(-)

Eric

#73 lore raw

PATCH 1/6 netfilter: nf_queue: Don't recompute the hook_list head

If someone sends packets from one of the netdevice ingress hooks to the a userspace queue, and then userspace later accepts the packet, the netfilter code can enter an infinite loop as the list head will never be found.

Pass in the saved list_head to avoid this.

---
 net/netfilter/nf_queue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_queue.c +1 -1
index cd60d397fe05..8a8b2abc35ff 100644
@@ -213,7 +213,7 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 
 	if (verdict == NF_ACCEPT) {
 	next_hook:
-		verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook],
+		verdict = nf_iterate(entry->state.hook_list,
 				     skb, &entry->state, &elem);
 	}
 
Signature · 1 line
2.2.1
#74 lore raw

PATCH 2/6 netfilter: kill nf_hooks_active

In reply to Eric W. Biederman

The function obscures what is going on in nf_hook_thresh and it's existence requires computing the hook list twice.

---
 include/linux/netfilter.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
include/linux/netfilter.h +3 -7
index 00050dfd9f23..d7826e59779e 100644
@@ -150,11 +150,6 @@ static inline bool nf_hook_list_active(struct list_head *nf_hook_list,
 }
 #endif
 
-static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
-{
-	return nf_hook_list_active(&nf_hooks[pf][hook], pf, hook);
-}
-
 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
 
 /**
@@ -172,10 +167,11 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
 				 int (*okfn)(struct sock *, struct sk_buff *),
 				 int thresh)
 {
-	if (nf_hooks_active(pf, hook)) {
+	struct list_head *nf_hook_list = &nf_hooks[pf][hook];
+	if (nf_hook_list_active(nf_hook_list, pf, hook)) {
 		struct nf_hook_state state;
 
-		nf_hook_state_init(&state, &nf_hooks[pf][hook], hook, thresh,
+		nf_hook_state_init(&state, nf_hook_list, hook, thresh,
 				   pf, indev, outdev, sk, okfn);
 		return nf_hook_slow(skb, &state);
 	}
Signature · 1 line
2.2.1
#75 lore raw

PATCH 3/6 netfilter: Simply the tests for enabling and disabling the ingress queue hook

In reply to Eric W. Biederman

Replace an overcomplicated switch statement with a simple if statement.

This also removes the ingress queue enable outside of nf_hook_mutex as the protection provided by the mutex is not necessary and the code is clearer having both of the static key increments together.

---
 net/netfilter/core.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
net/netfilter/core.c +6 -11
index a0e54974e2c9..7e3bc12f1c62 100644
@@ -74,7 +74,6 @@ int nf_register_hook(struct nf_hook_ops *reg)
 		if (reg->hooknum == NF_NETDEV_INGRESS) {
 			BUG_ON(reg->dev == NULL);
 			nf_hook_list = &reg->dev->nf_hooks_ingress;
-			net_inc_ingress_queue();
 			break;
 		}
 #endif
@@ -90,6 +89,10 @@ int nf_register_hook(struct nf_hook_ops *reg)
 	}
 	list_add_rcu(&reg->list, elem->list.prev);
 	mutex_unlock(&nf_hook_mutex);
+#ifdef CONFIG_NETFILTER_INGRESS
+	if ((reg->pf == NFPROTO_NETDEV) && (reg->hooknum == NF_NETDEV_INGRESS))
+		net_inc_ingress_queue();
+#endif
 #ifdef HAVE_JUMP_LABEL
 	static_key_slow_inc(&nf_hooks_needed[reg->pf][reg->hooknum]);
 #endif
@@ -102,18 +105,10 @@ void nf_unregister_hook(struct nf_hook_ops *reg)
 	mutex_lock(&nf_hook_mutex);
 	list_del_rcu(&reg->list);
 	mutex_unlock(&nf_hook_mutex);
-	switch (reg->pf) {
-	case NFPROTO_NETDEV:
 #ifdef CONFIG_NETFILTER_INGRESS
-		if (reg->hooknum == NF_NETDEV_INGRESS) {
-			net_dec_ingress_queue();
-			break;
-		}
-		break;
+	if ((reg->pf == NFPROTO_NETDEV) && (reg->hooknum == NF_NETDEV_INGRESS))
+		net_dec_ingress_queue();
 #endif
-	default:
-		break;
-	}
 #ifdef HAVE_JUMP_LABEL
 	static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
 #endif
Signature · 1 line
2.2.1
#76 lore raw

PATCH 4/6 netfilter: Factor out the hook list selection from nf_register_hook

In reply to Eric W. Biederman

- Add a new function find_nf_hook_list to select the nf_hook_list

- Fail nf_register_hook when asked for a per netdevice hook list when support for per netdevice hook lists is not built into the kernel.

- Move the hook list head selection outside of nf_hook_mutex as nothing in the selection requires the hook list, and error handling is simpler if a mutex is not held.

---
 net/netfilter/core.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
net/netfilter/core.c +18 -14
index 7e3bc12f1c62..8bbcdfcdbfbd 100644
@@ -62,27 +62,31 @@ EXPORT_SYMBOL(nf_hooks_needed);
 
 static DEFINE_MUTEX(nf_hook_mutex);
 
-int nf_register_hook(struct nf_hook_ops *reg)
+static struct list_head *find_nf_hook_list(const struct nf_hook_ops *reg)
 {
-	struct list_head *nf_hook_list;
-	struct nf_hook_ops *elem;
+	struct list_head *nf_hook_list = NULL;
 
-	mutex_lock(&nf_hook_mutex);
-	switch (reg->pf) {
-	case NFPROTO_NETDEV:
+	if (reg->pf != NFPROTO_NETDEV)
+		nf_hook_list = &nf_hooks[reg->pf][reg->hooknum];
+	else if (reg->hooknum == NF_NETDEV_INGRESS) {
 #ifdef CONFIG_NETFILTER_INGRESS
-		if (reg->hooknum == NF_NETDEV_INGRESS) {
-			BUG_ON(reg->dev == NULL);
+		if (reg->dev)
 			nf_hook_list = &reg->dev->nf_hooks_ingress;
-			break;
-		}
 #endif
-		/* Fall through. */
-	default:
-		nf_hook_list = &nf_hooks[reg->pf][reg->hooknum];
-		break;
 	}
+	return nf_hook_list;
+}
+
+int nf_register_hook(struct nf_hook_ops *reg)
+{
+	struct list_head *nf_hook_list;
+	struct nf_hook_ops *elem;
 
+	nf_hook_list = find_nf_hook_list(reg);
+	if (!nf_hook_list)
+		return -ENOENT;
+
+	mutex_lock(&nf_hook_mutex);
 	list_for_each_entry(elem, nf_hook_list, list) {
 		if (reg->priority < elem->priority)
 			break;
Signature · 1 line
2.2.1
#77 lore raw

PATCH 5/6 netfilter: Per network namespace netfilter hooks.

In reply to Eric W. Biederman

- Add a new set of functions for registering and unregistering per network namespace hooks.

- Modify the old global namespace hook functions to use the per network namespace hooks in their implementation, so their remains a single list that needs to be walked for any hook (this is important for keeping the hook priority working and for keeping the code walking the hooks simple).

- Only allow registering the per netdevice hooks in the network namespace where the network device lives.

- Dynamically allocate the structures in the per network namespace hook list in nf_register_net_hook, and unregister them in nf_unregister_net_hook.

Dynamic allocate is required somewhere as the number of network namespaces are not fixed so we might as well allocate them in the registration function.

The chain of registered hooks on any list is expected to be small so the cost of walking that list to find the entry we are unregistering should also be small.

Performing the management of the dynamically allocated list entries in the registration and unregistration functions keeps the complexity from spreading.

---
 include/linux/netfilter.h     |  15 +++-
 include/net/netns/netfilter.h |   1 +
 net/netfilter/core.c          | 184 +++++++++++++++++++++++++++++++++++++-----
 3 files changed, 175 insertions(+), 25 deletions(-)
include/linux/netfilter.h +11 -4
index d7826e59779e..4903e392a6ac 100644
@@ -11,6 +11,8 @@
 #include <linux/list.h>
 #include <linux/static_key.h>
 #include <linux/netfilter_defs.h>
+#include <linux/netdevice.h>
+#include <net/net_namespace.h>
 
 #ifdef CONFIG_NETFILTER
 static inline int NF_DROP_GETERR(int verdict)
@@ -118,6 +120,13 @@ struct nf_sockopt_ops {
 };
 
 /* Function to register/unregister hook points. */
+int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
+void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
+int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
+			  unsigned int n);
+void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
+			     unsigned int n);
+
 int nf_register_hook(struct nf_hook_ops *reg);
 void nf_unregister_hook(struct nf_hook_ops *reg);
 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
@@ -128,8 +137,6 @@ void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
 int nf_register_sockopt(struct nf_sockopt_ops *reg);
 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
 
-extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
-
 #ifdef HAVE_JUMP_LABEL
 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
 
@@ -167,10 +174,10 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
 				 int (*okfn)(struct sock *, struct sk_buff *),
 				 int thresh)
 {
-	struct list_head *nf_hook_list = &nf_hooks[pf][hook];
+	struct net *net = dev_net(indev?indev:outdev);
+	struct list_head *nf_hook_list = &net->nf.hooks[pf][hook];
 	if (nf_hook_list_active(nf_hook_list, pf, hook)) {
 		struct nf_hook_state state;
-
 		nf_hook_state_init(&state, nf_hook_list, hook, thresh,
 				   pf, indev, outdev, sk, okfn);
 		return nf_hook_slow(skb, &state);
include/net/netns/netfilter.h +1 -0
index 532e4ba64f49..38aa4983e2a9 100644
@@ -14,5 +14,6 @@ struct netns_nf {
 #ifdef CONFIG_SYSCTL
 	struct ctl_table_header *nf_log_dir_header;
 #endif
+	struct list_head hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
 };
 #endif
net/netfilter/core.c +163 -21

257 lines not on this page · raw message

Signature · 1 line
2.2.1
#78 lore raw

Re: [PATCH -next 5/6] netfilter: Per network namespace netfilter hooks.

Quoted 21 lines · Eric W. Biederman
net/netfilter/core.c +11 -3
@@ -102,13 +112,35 @@ int nf_register_hook(struct nf_hook_ops *reg)
 #endif
 	return 0;
 }
-EXPORT_SYMBOL(nf_register_hook);
+EXPORT_SYMBOL(nf_register_net_hook);
 
-void nf_unregister_hook(struct nf_hook_ops *reg)
+void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
 {
+	struct list_head *nf_hook_list;
+	struct nf_hook_ops *elem;
+
+	nf_hook_list = find_nf_hook_list(net, reg);
+	if (!nf_hook_list)
+		return;
+
 	mutex_lock(&nf_hook_mutex);
-	list_del_rcu(&reg->list);
+	list_for_each_entry(elem, nf_hook_list, list) {
+		if ((reg->hook     == elem->hook) &&

net/netfilter/core.c int nf_register_hook(struct nf_hook_ops *reg) · line 138

Quoted 10 lines
net/netfilter/core.c +10 -0
+		    (reg->dev      == elem->dev) &&
+		    (reg->owner    == elem->owner) &&
+		    (reg->priv     == elem->priv) &&
+		    (reg->pf       == elem->pf) &&
+		    (reg->hooknum  == elem->hooknum) &&
+		    (reg->priority == elem->priority)) {
+			list_del_rcu(&elem->list);
+			break;
+		}
+	}

I think I found a problem with this code above.

If we register two hooks from the same module using exactly the same tuple that identifies this, we delete the hook that we don't want, eg.

        nft add table filter
        nft add chain filter test { type filter hook input priority 0\; }
        nft add chain filter test2 { type filter hook input priority 0\; }

then, you delete 'test':

nft delete chain filter test

This will delete 'test2' hook instead of 'test' as it will find this in first place on the list.

I think we should add a cookie field that stores the address of the original hook object that is passed as parameter, so we are sure we kill the right hook.

#79 lore raw

Re: [PATCH -next 5/6] netfilter: Per network namespace netfilter hooks.

Quoted 42 lines · Pablo Neira Ayuso
Quoted 21 lines · Eric W. Biederman
net/netfilter/core.c +11 -3
@@ -102,13 +112,35 @@ int nf_register_hook(struct nf_hook_ops *reg)
 #endif
 	return 0;
 }
-EXPORT_SYMBOL(nf_register_hook);
+EXPORT_SYMBOL(nf_register_net_hook);
 
-void nf_unregister_hook(struct nf_hook_ops *reg)
+void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
 {
+	struct list_head *nf_hook_list;
+	struct nf_hook_ops *elem;
+
+	nf_hook_list = find_nf_hook_list(net, reg);
+	if (!nf_hook_list)
+		return;
+
 	mutex_lock(&nf_hook_mutex);
-	list_del_rcu(&reg->list);
+	list_for_each_entry(elem, nf_hook_list, list) {
+		if ((reg->hook     == elem->hook) &&

net/netfilter/core.c int nf_register_hook(struct nf_hook_ops *reg) · line 138

Quoted 10 lines
net/netfilter/core.c +10 -0
+		    (reg->dev      == elem->dev) &&
+		    (reg->owner    == elem->owner) &&
+		    (reg->priv     == elem->priv) &&
+		    (reg->pf       == elem->pf) &&
+		    (reg->hooknum  == elem->hooknum) &&
+		    (reg->priority == elem->priority)) {
+			list_del_rcu(&elem->list);
+			break;
+		}
+	}

I think I found a problem with this code above.

If we register two hooks from the same module using exactly the same tuple that identifies this, we delete the hook that we don't want, eg.

        nft add table filter
        nft add chain filter test { type filter hook input priority 0\; }
        nft add chain filter test2 { type filter hook input priority 0\; }
Quoted 6 lines

then, you delete 'test':

nft delete chain filter test

This will delete 'test2' hook instead of 'test' as it will find this in first place on the list.

So we have two adjacent entries on the same chain that perform the exact same action. We delete one of them.

I do not see how that is noticable. Registration order plays a small role but especially with the priority bit we don't strongly honor registration order.

In your example above we will distinguish between the two chains
as nf_hook_ops->priv will point the nf tables chain. So that specific
case is at least safe.

Quoted 3 lines

I think we should add a cookie field that stores the address of the original hook object that is passed as parameter, so we are sure we kill the right hook.

I don't believe that is necessary. To the best of my knowledge for a registration to be meaningful we must always change at least one of those fields I am comparing. Typically either priv or hook.

It is a real change from what we have been doing but there is a lot of freedom in not needing to keep the original structure around.

Eric

#80 lore raw

PATCH 6/6 netfilter: nftables: Only run the nftables chains in the proper netns

In reply to Eric W. Biederman

- Register the nftables chains in the network namespace that they need to run in.

- Remove the hacks that stopped chains running in the wrong network namespace.

---
 net/netfilter/nf_tables_api.c  | 6 ++++--
 net/netfilter/nf_tables_core.c | 5 -----
 2 files changed, 4 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +4 -2
index cfe636808541..12e65ec46bd4 100644
@@ -130,20 +130,22 @@ static void nft_trans_destroy(struct nft_trans *trans)
 int nft_register_basechain(struct nft_base_chain *basechain,
 			   unsigned int hook_nops)
 {
+	struct net *net = read_pnet(&basechain->pnet);
 	if (basechain->flags & NFT_BASECHAIN_DISABLED)
 		return 0;
 
-	return nf_register_hooks(basechain->ops, hook_nops);
+	return nf_register_net_hooks(net, basechain->ops, hook_nops);
 }
 EXPORT_SYMBOL_GPL(nft_register_basechain);
 
 void nft_unregister_basechain(struct nft_base_chain *basechain,
 			      unsigned int hook_nops)
 {
+	struct net *net = read_pnet(&basechain->pnet);
 	if (basechain->flags & NFT_BASECHAIN_DISABLED)
 		return;
 
-	nf_unregister_hooks(basechain->ops, hook_nops);
+	nf_unregister_net_hooks(net, basechain->ops, hook_nops);
 }
 EXPORT_SYMBOL_GPL(nft_unregister_basechain);
 
net/netfilter/nf_tables_core.c +0 -5
index f77bad46ac68..05d0b03530f6 100644
@@ -114,7 +114,6 @@ unsigned int
 nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)
 {
 	const struct nft_chain *chain = ops->priv, *basechain = chain;
-	const struct net *chain_net = read_pnet(&nft_base_chain(basechain)->pnet);
 	const struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);
 	const struct nft_rule *rule;
 	const struct nft_expr *expr, *last;
@@ -125,10 +124,6 @@ nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)
 	int rulenum;
 	unsigned int gencursor = nft_genmask_cur(net);
 
-	/* Ignore chains that are not for the current network namespace */
-	if (!net_eq(net, chain_net))
-		return NF_ACCEPT;
-
 do_chain:
 	rulenum = 0;
 	rule = list_entry(&chain->rules, struct nft_rule, list);
Signature · 1 line
2.2.1
#81 lore raw

Re: [PATCH -next 0/6] Per network namespace netfilter chains

In reply to Eric W. Biederman
Quoted 5 lines · Eric W. Biederman

By maintining a set of functions to register and unregister netfilter hooks both globally and per network namespace I have managed to write a compact patchset that maintain per network netfilter chains, and registers the nftables netfilter hooks per network namespace.

Nice, thank you.

It would be great to convert this to the for_each_net_rcu variant once we're sure this is safe.

Quoted 3 lines

There are lots of other possible and desirable cleanups but this one is a core change needed to make the other changes independent small changes.

The state->net field will kill that dev_net(...) ? x : y; all over the code, that would be nice.

Some comments on your patchset:

* 1/6 netfilter: nf_queue: Don't recompute the hook_list head

I already passed this to current nf as you insisted on getting this, and for the sake of correctness, so it's basically already in David's net tree.

* From 2 to 6, I have applied these series with small coding style cleanups.

- Add line break between variable declaration and body:

+ struct list_head *nf_hook_list = &nf_hooks[pf][hook];
-+
+ if (nf_hook_list_active(nf_hook_list, pf, hook)) {

and here:

int nft_register_basechain(struct nft_base_chain *basechain,
unsigned int hook_nops)
{
+ struct net *net = read_pnet(&basechain->pnet);
++

- Get rid of unnecessary parens:

-+ if ((reg->pf == NFPROTO_NETDEV) && (reg->hooknum == NF_NETDEV_INGRESS))
++ if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)

- Get rid of unnecesary brackets:

-+ for_each_net(net) {
++ for_each_net(net)
+ nf_unregister_net_hook(net, reg);
-+ }

and here:

-+ list_for_each_entry_continue_reverse(elem, &nf_hook_list, list) {
++ list_for_each_entry_continue_reverse(elem, &nf_hook_list, list)
+ nf_unregister_net_hook(net, elem);
-+ }

I have pushed this to:

http://git.kernel.org/cgit/linux/kernel/git/pablo/nf-next.git/log/?h=pending

in case you want to have a closer look. Thank you.

#82 lore raw

Re: [PATCH -next 0/6] Per network namespace netfilter chains

Quoted 11 lines · Pablo Neira Ayuso
Quoted 5 lines · Eric W. Biederman

By maintining a set of functions to register and unregister netfilter hooks both globally and per network namespace I have managed to write a compact patchset that maintain per network netfilter chains, and registers the nftables netfilter hooks per network namespace.

Nice, thank you.

It would be great to convert this to the for_each_net_rcu variant once we're sure this is safe.

That seems reasonable.

Quoted 6 lines
Quoted 3 lines

There are lots of other possible and desirable cleanups but this one is a core change needed to make the other changes independent small changes.

The state->net field will kill that dev_net(...) ? x : y; all over the code, that would be nice.

Yes it will. I intend to do that after this patchset settles so I am not dealing with more than one issue at a time. Otherwise there is too much work at once.

Quoted 7 lines

Some comments on your patchset:

* 1/6 netfilter: nf_queue: Don't recompute the hook_list head

I already passed this to current nf as you insisted on getting this, and for the sake of correctness, so it's basically already in David's net tree.

I would have expected this patch to be somewhere. I did not see this change in net-next when I wrote the patchset (which seemed like a good approximation of the latest thing available). If I overlooked and the patch has already made it to Dave then my apologies for being redundant.

I still don't see this patch in your pending branch.

Am I missing something?

Quoted 17 lines

* From 2 to 6, I have applied these series with small coding style cleanups.

- Add line break between variable declaration and body:

+ struct list_head *nf_hook_list = &nf_hooks[pf][hook];
-+
+ if (nf_hook_list_active(nf_hook_list, pf, hook)) {

and here:

int nft_register_basechain(struct nft_base_chain *basechain,
unsigned int hook_nops)
{
+ struct net *net = read_pnet(&basechain->pnet);
++

Quoted 4 lines

- Get rid of unnecessary parens:

-+ if ((reg->pf == NFPROTO_NETDEV) && (reg->hooknum == NF_NETDEV_INGRESS))
++ if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)

Fair enough. For me those parens are necessary to trust the compiler is doing the right thing. I can never remember the C operator precedence rules.

Quoted 14 lines

- Get rid of unnecesary brackets:

-+ for_each_net(net) {
++ for_each_net(net)
+ nf_unregister_net_hook(net, reg);
-+ }

and here:

-+ list_for_each_entry_continue_reverse(elem, &nf_hook_list, list) {
++ list_for_each_entry_continue_reverse(elem, &nf_hook_list, list)
+ nf_unregister_net_hook(net, elem);
-+ }

Quoted 5 lines

I have pushed this to:

http://git.kernel.org/cgit/linux/kernel/git/pablo/nf-next.git/log/?h=pending

in case you want to have a closer look. Thank you.

Thank you.

Eric

#83 lore raw

Re: [PATCH -next 0/6] Per network namespace netfilter chains

Quoted 1 line · Eric W. Biederman

Pablo Neira Ayuso <[email protected]> writes:

[...]

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

There are lots of other possible and desirable cleanups but this one is a core change needed to make the other changes independent small changes.

The state->net field will kill that dev_net(...) ? x : y; all over the code, that would be nice.

Yes it will. I intend to do that after this patchset settles so I am not dealing with more than one issue at a time. Otherwise there is too much work at once.

Sure, thanks.

Quoted 14 lines
Quoted 7 lines

Some comments on your patchset:

* 1/6 netfilter: nf_queue: Don't recompute the hook_list head

I already passed this to current nf as you insisted on getting this, and for the sake of correctness, so it's basically already in David's net tree.

I would have expected this patch to be somewhere. I did not see this change in net-next when I wrote the patchset (which seemed like a good approximation of the latest thing available). If I overlooked and the patch has already made it to Dave then my apologies for being redundant.

Quoted 3 lines

I still don't see this patch in your pending branch.

Am I missing something?

It doesn't show yet in net-next, but it's already in net:

http://git.kernel.org/cgit/linux/kernel/git/davem/net.git/commit/?id=f307170d6e591a48529425b1ed6ca835790995a9

David periodically pulls net into net-next, so it will show there too at some point.

maintainer
#84 lore raw
In reply to Eric W. Biederman

Hello,

Quoted 8 lines · Eric W. Biederman

This patshset roots out all of the very weird network namespace computation logic (except for the code in ipvs) and fixes it. I really don't like how the code has been essentially guessing which network namespace to use.

Probably the worst guessing is in ipvs in the function skb_net. I have some preliminary changes to fix ipvs but they are not quite ready yet. Cleaning up ipvs enough that I can kill skb_net is on my short list.

For IPVS skb_net is too complicated. One of
the first things we do in hook handler is to check skb_dst, so even now dev_net(skb_dst(skb)->dev) should work. sock_net is used for administration via netlink, skb_net is not used there (ip_vs_ctl.c).

As for removing/replacing skb_net, ip_vs_conn_net()
is ok when cp is present, we have also svc->net, otherwise we can store net into struct ip_vs_iphdr, it is filled by ip_vs_fill_iph_skb from small number of places when hook handler is entered.

Regards

--
Julian Anastasov <[email protected]>

#85 lore raw
Quoted 9 lines · Julian Anastasov

Hello,

Quoted 5 lines · Eric W. Biederman

This patshset roots out all of the very weird network namespace computation logic (except for the code in ipvs) and fixes it. I really don't like how the code has been essentially guessing which network namespace to use.

Quoted 8 lines
Quoted 3 lines

Probably the worst guessing is in ipvs in the function skb_net. I have some preliminary changes to fix ipvs but they are not quite ready yet. Cleaning up ipvs enough that I can kill skb_net is on my short list.

For IPVS skb_net is too complicated. One of
the first things we do in hook handler is to check skb_dst, so even now dev_net(skb_dst(skb)->dev) should work.

A couple of things:
- You don't check skb_dst() first thing in all hooks.

- All hooks have state->net after my changes so it gets a lot easier.

- I am trying to avoid dev_net(skb_dst(skb)->dev) if I can because of my ulterior motive of allowing a destination network device in another network namespace.

Quoted 2 lines

sock_net is used for administration via netlink,
skb_net is not used there (ip_vs_ctl.c).

Agreed. sock_net is the right thing for the netlink sockets, and while obscured by skb_sknet that works fine.

Quoted 2 lines

As for removing/replacing skb_net, ip_vs_conn_net()
is ok when cp is present,

Yes I have seen that.

Quoted 1 line

we have also svc->net,

And I have seen this.

Quoted 4 lines

otherwise
we can store net into struct ip_vs_iphdr, it is filled by ip_vs_fill_iph_skb from small number of places when hook handler is entered.

Interesting I had not thought of changing ip_vs_iphdr.

I actually have a patch that passes state->net down to where it is needed or uses ip_vs_conn_net() and it it looks ok, but the patch is so busy with small tweaks that I need to break it up.

Further I am staring at things and trying to see if there is a way to
make the mass of parameters more readable/obvious. Something like
the passing of struct nf_hook_state into the netfilter hooks.

Or in other words I want to make the change to ip_vs deadly boring, obviously correct, and trivial to maintain, and I am not quite there yet.

Eric

p.s. I do have my patch that I can toss in your direction if you are interested.

maintainer
#86 lore raw

Hello,

Quoted 2 lines · Eric W. Biederman

p.s. I do have my patch that I can toss in your direction if you are interested.

Of course... I'll be able to check it after 8 hours...

Regards

--
Julian Anastasov <[email protected]>

#87 lore raw

Cc' list trimmed as this is not longer about the original patch submission.

Quoted 8 lines · Julian Anastasov

Hello,

Quoted 2 lines · Eric W. Biederman

p.s. I do have my patch that I can toss in your direction if you are interested.

Of course... I'll be able to check it after 8 hours...

My incremental patch for ipvs on top of everything else I have pushed out looks like this:

From: "Eric W. Biederman" <[email protected]>
Date: Fri, 12 Jun 2015 18:34:12 -0500
Subject: [PATCH] ipvs: Pass struct net down to where it is needed and used

Pass struct net down to where it is used and stop guessing which network namespace should be used.

---
 include/net/ip_vs.h                     |  45 +++---------
 net/netfilter/ipvs/ip_vs_conn.c         |  11 ++-
 net/netfilter/ipvs/ip_vs_core.c         | 118 ++++++++++++++------------------
 net/netfilter/ipvs/ip_vs_ftp.c          |   8 +--
 net/netfilter/ipvs/ip_vs_proto_ah_esp.c |   9 ++-
 net/netfilter/ipvs/ip_vs_proto_sctp.c   |   5 +-
 net/netfilter/ipvs/ip_vs_proto_tcp.c    |   8 +--
 net/netfilter/ipvs/ip_vs_proto_udp.c    |   5 +-
 net/netfilter/ipvs/ip_vs_xmit.c         |  51 ++++++++------
 net/netfilter/xt_ipvs.c                 |   2 +-
 10 files changed, 108 insertions(+), 154 deletions(-)
include/net/ip_vs.h +8 -37
index 4e3731ee4eac..a556d14cff70 100644
@@ -35,37 +35,6 @@ static inline struct netns_ipvs *net_ipvs(struct net* net)
 	return net->ipvs;
 }
 
-/* Get net ptr from skb in traffic cases
- * use skb_sknet when call is from userland (ioctl or netlink)
- */
-static inline struct net *skb_net(const struct sk_buff *skb)
-{
-#ifdef CONFIG_NET_NS
-#ifdef CONFIG_IP_VS_DEBUG
-	/*
-	 * This is used for debug only.
-	 * Start with the most likely hit
-	 * End with BUG
-	 */
-	if (likely(skb->dev && dev_net(skb->dev)))
-		return dev_net(skb->dev);
-	if (skb_dst(skb) && skb_dst(skb)->dev)
-		return dev_net(skb_dst(skb)->dev);
-	WARN(skb->sk, "Maybe skb_sknet should be used in %s() at line:%d\n",
-		      __func__, __LINE__);
-	if (likely(skb->sk && sock_net(skb->sk)))
-		return sock_net(skb->sk);
-	pr_err("There is no net ptr to find in the skb in %s() line:%d\n",
-		__func__, __LINE__);
-	BUG();
-#else
-	return dev_net(skb->dev ? : skb_dst(skb)->dev);
-#endif
-#else
-	return &init_net;
-#endif
-}
-
 static inline struct net *skb_sknet(const struct sk_buff *skb)
 {
 #ifdef CONFIG_NET_NS
@@ -441,19 +410,19 @@ struct ip_vs_protocol {
 
 	void (*exit_netns)(struct net *net, struct ip_vs_proto_data *pd);
 
-	int (*conn_schedule)(int af, struct sk_buff *skb,
+	int (*conn_schedule)(struct net *net, int af, struct sk_buff *skb,
 			     struct ip_vs_proto_data *pd,
 			     int *verdict, struct ip_vs_conn **cpp,
 			     struct ip_vs_iphdr *iph);
 
 	struct ip_vs_conn *
-	(*conn_in_get)(int af,
+	(*conn_in_get)(struct net *net, int af,
 		       const struct sk_buff *skb,
 		       const struct ip_vs_iphdr *iph,
 		       int inverse);
 
 	struct ip_vs_conn *
-	(*conn_out_get)(int af,
+	(*conn_out_get)(struct net *net, int af,
 			const struct sk_buff *skb,
 			const struct ip_vs_iphdr *iph,
 			int inverse);
@@ -1179,13 +1148,15 @@ static inline void ip_vs_conn_fill_param(struct net *net, int af, int protocol,
 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
 
-struct ip_vs_conn * ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
+struct ip_vs_conn * ip_vs_conn_in_get_proto(struct net *net, int af,
+					    const struct sk_buff *skb,
 					    const struct ip_vs_iphdr *iph,
 					    int inverse);
 
 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
 
-struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
+struct ip_vs_conn * ip_vs_conn_out_get_proto(struct net *net, int af,
+					     const struct sk_buff *skb,
 					     const struct ip_vs_iphdr *iph,
 					     int inverse);
 
@@ -1215,7 +1186,7 @@ void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
 
 const char *ip_vs_state_name(__u16 proto, int state);
 
-void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp);
+void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
 int ip_vs_check_template(struct ip_vs_conn *ct);
 void ip_vs_random_dropentry(struct net *net);
 int ip_vs_conn_init(void);
net/netfilter/ipvs/ip_vs_conn.c +5 -6
index b0f7b626b56d..4deb4a0d2247 100644
@@ -314,12 +314,11 @@ struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
 }
 
 static int
-ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
+ip_vs_conn_fill_param_proto(struct net *net, int af, const struct sk_buff *skb,
 			    const struct ip_vs_iphdr *iph,
 			    int inverse, struct ip_vs_conn_param *p)
 {
 	__be16 _ports[2], *pptr;
-	struct net *net = skb_net(skb);
 
 	pptr = frag_safe_skb_hp(skb, iph->len, sizeof(_ports), _ports, iph);
 	if (pptr == NULL)
@@ -335,12 +334,12 @@ ip_vs_conn_fill_param_proto(int af, const struct sk_buff *skb,
 }
 
 struct ip_vs_conn *
-ip_vs_conn_in_get_proto(int af, const struct sk_buff *skb,
+ip_vs_conn_in_get_proto(struct net *net, int af, const struct sk_buff *skb,
 			const struct ip_vs_iphdr *iph, int inverse)
 {
 	struct ip_vs_conn_param p;
 
-	if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
+	if (ip_vs_conn_fill_param_proto(net, af, skb, iph, inverse, &p))
 		return NULL;
 
 	return ip_vs_conn_in_get(&p);
@@ -439,12 +438,12 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
 }
 
 struct ip_vs_conn *
-ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,
+ip_vs_conn_out_get_proto(struct net *net, int af, const struct sk_buff *skb,
 			 const struct ip_vs_iphdr *iph, int inverse)
 {
 	struct ip_vs_conn_param p;
 
-	if (ip_vs_conn_fill_param_proto(af, skb, iph, inverse, &p))
+	if (ip_vs_conn_fill_param_proto(net, af, skb, iph, inverse, &p))
 		return NULL;
 
 	return ip_vs_conn_out_get(&p);
net/netfilter/ipvs/ip_vs_core.c +50 -68

439 lines not on this page · raw message

net/netfilter/ipvs/ip_vs_ftp.c +2 -6
index 5d3daae98bf0..eed97263e733 100644
@@ -181,7 +181,6 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,
 	int ret = 0;
 	enum ip_conntrack_info ctinfo;
 	struct nf_conn *ct;
-	struct net *net;
 
 	*diff = 0;
 
@@ -289,9 +288,8 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,
 		 * would be adjusted twice.
 		 */
 
-		net = skb_net(skb);
 		cp->app_data = NULL;
-		ip_vs_tcp_conn_listen(net, n_cp);
+		ip_vs_tcp_conn_listen(n_cp);
 		ip_vs_conn_put(n_cp);
 		return ret;
 	}
@@ -320,7 +318,6 @@ static int ip_vs_ftp_in(struct ip_vs_app *app, struct ip_vs_conn *cp,
 	union nf_inet_addr to;
 	__be16 port;
 	struct ip_vs_conn *n_cp;
-	struct net *net;
 
 	/* no diff required for incoming packets */
 	*diff = 0;
@@ -413,8 +410,7 @@ static int ip_vs_ftp_in(struct ip_vs_app *app, struct ip_vs_conn *cp,
 	/*
 	 *	Move tunnel to listen state
 	 */
-	net = skb_net(skb);
-	ip_vs_tcp_conn_listen(net, n_cp);
+	ip_vs_tcp_conn_listen(n_cp);
 	ip_vs_conn_put(n_cp);
 
 	return 1;
net/netfilter/ipvs/ip_vs_proto_ah_esp.c +4 -5
index 5de3dd312c0f..437d96d43597 100644
@@ -56,13 +56,12 @@ ah_esp_conn_fill_param_proto(struct net *net, int af,
 }
 
 static struct ip_vs_conn *
-ah_esp_conn_in_get(int af, const struct sk_buff *skb,
+ah_esp_conn_in_get(struct net *net, int af, const struct sk_buff *skb,
 		   const struct ip_vs_iphdr *iph,
 		   int inverse)
 {
 	struct ip_vs_conn *cp;
 	struct ip_vs_conn_param p;
-	struct net *net = skb_net(skb);
 
 	ah_esp_conn_fill_param_proto(net, af, iph, inverse, &p);
 	cp = ip_vs_conn_in_get(&p);
@@ -84,12 +83,11 @@ ah_esp_conn_in_get(int af, const struct sk_buff *skb,
 
 
 static struct ip_vs_conn *
-ah_esp_conn_out_get(int af, const struct sk_buff *skb,
+ah_esp_conn_out_get(struct net *net, int af, const struct sk_buff *skb,
 		    const struct ip_vs_iphdr *iph, int inverse)
 {
 	struct ip_vs_conn *cp;
 	struct ip_vs_conn_param p;
-	struct net *net = skb_net(skb);
 
 	ah_esp_conn_fill_param_proto(net, af, iph, inverse, &p);
 	cp = ip_vs_conn_out_get(&p);
@@ -107,7 +105,8 @@ ah_esp_conn_out_get(int af, const struct sk_buff *skb,
 
 
 static int
-ah_esp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
+ah_esp_conn_schedule(struct net *net, int af, struct sk_buff *skb,
+		     struct ip_vs_proto_data *pd,
 		     int *verdict, struct ip_vs_conn **cpp,
 		     struct ip_vs_iphdr *iph)
 {
net/netfilter/ipvs/ip_vs_proto_sctp.c +2 -3
index 5b84c0b56642..8baf1eddeed8 100644
@@ -9,11 +9,11 @@
 #include <net/ip_vs.h>
 
 static int
-sctp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
+sctp_conn_schedule(struct net *net, int af, struct sk_buff *skb,
+		   struct ip_vs_proto_data *pd,
 		   int *verdict, struct ip_vs_conn **cpp,
 		   struct ip_vs_iphdr *iph)
 {
-	struct net *net;
 	struct ip_vs_service *svc;
 	struct netns_ipvs *ipvs;
 	sctp_chunkhdr_t _schunkh, *sch;
@@ -32,7 +32,6 @@ sctp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		return 0;
 	}
 
-	net = skb_net(skb);
 	ipvs = net_ipvs(net);
 	rcu_read_lock();
 	if ((sch->type == SCTP_CID_INIT || sysctl_sloppy_sctp(ipvs)) &&
net/netfilter/ipvs/ip_vs_proto_tcp.c +4 -4
index 8e92beb0cca9..7651ef2c9a87 100644
@@ -32,11 +32,11 @@
 #include <net/ip_vs.h>
 
 static int
-tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
+tcp_conn_schedule(struct net *net, int af, struct sk_buff *skb,
+		  struct ip_vs_proto_data *pd,
 		  int *verdict, struct ip_vs_conn **cpp,
 		  struct ip_vs_iphdr *iph)
 {
-	struct net *net;
 	struct ip_vs_service *svc;
 	struct tcphdr _tcph, *th;
 	struct netns_ipvs *ipvs;
@@ -46,7 +46,6 @@ tcp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		*verdict = NF_DROP;
 		return 0;
 	}
-	net = skb_net(skb);
 	ipvs = net_ipvs(net);
 	/* No !th->ack check to allow scheduling on SYN+ACK for Active FTP */
 	rcu_read_lock();
@@ -653,8 +652,9 @@ tcp_app_conn_bind(struct ip_vs_conn *cp)
 /*
  *	Set LISTEN timeout. (ip_vs_conn_put will setup timer)
  */
-void ip_vs_tcp_conn_listen(struct net *net, struct ip_vs_conn *cp)
+void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp)
 {
+	struct net *net = ip_vs_conn_net(cp);
 	struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_TCP);
 
 	spin_lock_bh(&cp->lock);
net/netfilter/ipvs/ip_vs_proto_udp.c +2 -3
index b62a3c0ff9bf..6eb0c3be3859 100644
@@ -29,11 +29,11 @@
 #include <net/ip6_checksum.h>
 
 static int
-udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
+udp_conn_schedule(struct net *net, int af, struct sk_buff *skb,
+		  struct ip_vs_proto_data *pd,
 		  int *verdict, struct ip_vs_conn **cpp,
 		  struct ip_vs_iphdr *iph)
 {
-	struct net *net;
 	struct ip_vs_service *svc;
 	struct udphdr _udph, *uh;
 
@@ -43,7 +43,6 @@ udp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
 		*verdict = NF_DROP;
 		return 0;
 	}
-	net = skb_net(skb);
 	rcu_read_lock();
 	svc = ip_vs_service_find(net, af, skb->mark, iph->protocol,
 				 &iph->daddr, uh->dest);
net/netfilter/ipvs/ip_vs_xmit.c +30 -21

164 lines not on this page · raw message

net/netfilter/xt_ipvs.c +1 -1
index 8d47c3780fda..16884904b70b 100644
@@ -85,7 +85,7 @@ ipvs_mt(const struct sk_buff *skb, struct xt_action_param *par)
 	/*
 	 * Check if the packet belongs to an existing entry
 	 */
-	cp = pp->conn_out_get(family, skb, &iph, 1 /* inverse */);
+	cp = pp->conn_out_get(par->net, family, skb, &iph, 1 /* inverse */);
 	if (unlikely(cp == NULL)) {
 		match = false;
 		goto out;
Signature · 1 line
2.2.1
maintainer
#88 lore raw

Hello,

Quoted 9 lines · Eric W. Biederman

My incremental patch for ipvs on top of everything else I have pushed out looks like this:

From: "Eric W. Biederman" <[email protected]>
Date: Fri, 12 Jun 2015 18:34:12 -0500
Subject: [PATCH] ipvs: Pass struct net down to where it is needed and used

Pass struct net down to where it is used and stop guessing which network namespace should be used.

At first look patch is ok. But I'm not sure
for the changes in ip_vs_xmit.c. Can you explain in 2-3 lines, when can we see different netns? Is it when packet is forwarded to output device and it is part from another netns?

I'm asking because these __ip_vs_get_out_rt*
calls in ip_vs_xmit.c can reroute packet to another device...

Also, skb_sknet is another candidate for removal.
But I can take care about it after your changes are pushed...

Regards

--
Julian Anastasov <[email protected]>

#89 lore raw
Quoted 20 lines · Julian Anastasov

Hello,

Quoted 9 lines · Eric W. Biederman

My incremental patch for ipvs on top of everything else I have pushed out looks like this:

From: "Eric W. Biederman" <[email protected]>
Date: Fri, 12 Jun 2015 18:34:12 -0500
Subject: [PATCH] ipvs: Pass struct net down to where it is needed and used

Pass struct net down to where it is used and stop guessing which network namespace should be used.

At first look patch is ok. But I'm not sure
for the changes in ip_vs_xmit.c. Can you explain in 2-3 lines, when can we see different netns? Is it when packet is forwarded to output device and it is part from another netns?

Quoted 3 lines

I'm asking because these __ip_vs_get_out_rt*
calls in ip_vs_xmit.c can reroute packet to another device...

The driver was ensure_mtu_is_adequate where in one half of the function we have skb_net another half we have
dev_net(dst_skb(skb)->dev). That is goofy.

That gets replaced by ip_vs_conn_net(cp).

In practice today I don't see that there are differences in network namespaces today.

Moving forward I hope to be able to route packets out to network devices in different network namespaces. It is a massive optimization that doesn't need much code to support. Once that optimization is in place deriving the network namespace from the output device will not work.

I believe using ip_vs_conn_net(cp) is a simple rule that is easy to understand and easy to implement correctly on the output path.

Quoted 3 lines

Also, skb_sknet is another candidate for removal.
But I can take care about it after your changes are pushed...

*nod*

Eric
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in