netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: wenxu@ucloud.cn
To: pablo@netfilter.org, fw@strlen.de
Cc: netfilter-devel@vger.kernel.org, netdev@vger.kernel.org
Subject: [PATCH nf-next 1/3] netfilter: nf_nat_proto: add nf_nat_bridge_ops support
Date: Mon,  8 Jul 2019 16:29:25 +0800	[thread overview]
Message-ID: <1562574567-8293-1-git-send-email-wenxu@ucloud.cn> (raw)

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


             reply	other threads:[~2019-07-08  8:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-08  8:29 wenxu [this message]
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

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=1562574567-8293-1-git-send-email-wenxu@ucloud.cn \
    --to=wenxu@ucloud.cn \
    --cc=fw@strlen.de \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

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

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