From: wenxu <wenxu@xxxxxxxxx> Add nf_nat_bridge_ops to do nat in the bridge family Signed-off-by: wenxu <wenxu@xxxxxxxxx> --- 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