netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH nf-next 1/3] netfilter: nf_nat_proto: add nf_nat_bridge_ops support
@ 2019-07-08  8:29 wenxu
  2019-07-08  8:29 ` [PATCH nf-next 2/3] netfilter: nft_chain_nat: add nft_chain_nat_bridge support wenxu
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: wenxu @ 2019-07-08  8:29 UTC (permalink / raw)
  To: pablo, fw; +Cc: netfilter-devel, netdev

From: wenxu <wenxu@ucloud.cn>

Add nf_nat_bridge_ops to do nat in the bridge family

Signed-off-by: wenxu <wenxu@ucloud.cn>
---
 include/net/netfilter/nf_nat.h |  3 ++
 net/netfilter/nf_nat_proto.c   | 63 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/include/net/netfilter/nf_nat.h b/include/net/netfilter/nf_nat.h
index 423cda2..0c2d326 100644
--- a/include/net/netfilter/nf_nat.h
+++ b/include/net/netfilter/nf_nat.h
@@ -101,6 +101,9 @@ int nf_nat_icmpv6_reply_translation(struct sk_buff *skb, struct nf_conn *ct,
 int nf_nat_inet_register_fn(struct net *net, const struct nf_hook_ops *ops);
 void nf_nat_inet_unregister_fn(struct net *net, const struct nf_hook_ops *ops);
 
+int nf_nat_bridge_register_fn(struct net *net, const struct nf_hook_ops *ops);
+void nf_nat_bridge_unregister_fn(struct net *net, const struct nf_hook_ops *ops);
+
 unsigned int
 nf_nat_inet_fn(void *priv, struct sk_buff *skb,
 	       const struct nf_hook_state *state);
diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
index 888292e..652a71e 100644
--- a/net/netfilter/nf_nat_proto.c
+++ b/net/netfilter/nf_nat_proto.c
@@ -1035,3 +1035,66 @@ void nf_nat_inet_unregister_fn(struct net *net, const struct nf_hook_ops *ops)
 }
 EXPORT_SYMBOL_GPL(nf_nat_inet_unregister_fn);
 #endif /* NFT INET NAT */
+
+#if defined(CONFIG_NF_TABLES_BRIDGE) && IS_ENABLED(CONFIG_NFT_NAT)
+static unsigned int
+nf_nat_bridge_in(void *priv, struct sk_buff *skb,
+		 const struct nf_hook_state *state)
+{
+	switch (skb->protocol) {
+	case htons(ETH_P_IP):
+		return nf_nat_ipv4_in(priv, skb, state);
+	case htons(ETH_P_IPV6):
+		return nf_nat_ipv6_in(priv, skb, state);
+	default:
+		return NF_ACCEPT;
+	}
+}
+
+static unsigned int
+nf_nat_bridge_out(void *priv, struct sk_buff *skb,
+		  const struct nf_hook_state *state)
+{
+	switch (skb->protocol) {
+	case htons(ETH_P_IP):
+		return nf_nat_ipv4_out(priv, skb, state);
+	case htons(ETH_P_IPV6):
+		return nf_nat_ipv6_out(priv, skb, state);
+	default:
+		return NF_ACCEPT;
+	}
+}
+
+const struct nf_hook_ops nf_nat_bridge_ops[] = {
+	/* Before packet filtering, change destination */
+	{
+		.hook		= nf_nat_bridge_in,
+		.pf		= NFPROTO_BRIDGE,
+		.hooknum	= NF_INET_PRE_ROUTING,
+		.priority	= NF_IP_PRI_NAT_DST,
+	},
+	/* After packet filtering, change source */
+	{
+		.hook		= nf_nat_bridge_out,
+		.pf		= NFPROTO_BRIDGE,
+		.hooknum	= NF_INET_POST_ROUTING,
+		.priority	= NF_IP_PRI_NAT_SRC,
+	},
+};
+
+int nf_nat_bridge_register_fn(struct net *net, const struct nf_hook_ops *ops)
+{
+	if (WARN_ON_ONCE(ops->pf != NFPROTO_BRIDGE))
+		return -EINVAL;
+
+	return nf_nat_register_fn(net, ops->pf, ops, nf_nat_bridge_ops,
+				  ARRAY_SIZE(nf_nat_bridge_ops));
+}
+EXPORT_SYMBOL_GPL(nf_nat_bridge_register_fn);
+
+void nf_nat_bridge_unregister_fn(struct net *net, const struct nf_hook_ops *ops)
+{
+	nf_nat_unregister_fn(net, ops->pf, ops, ARRAY_SIZE(nf_nat_bridge_ops));
+}
+EXPORT_SYMBOL_GPL(nf_nat_bridge_unregister_fn);
+#endif
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-07-09 13:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-08  8:29 [PATCH nf-next 1/3] netfilter: nf_nat_proto: add nf_nat_bridge_ops support wenxu
2019-07-08  8:29 ` [PATCH nf-next 2/3] netfilter: nft_chain_nat: add nft_chain_nat_bridge support wenxu
2019-07-08  8:29 ` [PATCH nf-next 3/3] netfilter: nft_nat: add nft_bridge_nat_type support wenxu
2019-07-08 14:17 ` [PATCH nf-next 1/3] netfilter: nf_nat_proto: add nf_nat_bridge_ops support Florian Westphal
2019-07-09  2:56   ` wenxu
2019-07-09 10:42     ` Florian Westphal
2019-07-09 13:38       ` wenxu
2019-07-09  7:04 ` kbuild test robot

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).