1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_nat.h>
14 #include <net/netfilter/nf_nat_masquerade.h>
22 static const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
23 [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
24 [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 },
25 [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 },
28 static int nft_masq_validate(const struct nft_ctx *ctx,
29 const struct nft_expr *expr,
30 const struct nft_data **data)
34 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
38 return nft_chain_validate_hooks(ctx->chain,
39 (1 << NF_INET_POST_ROUTING));
42 static int nft_masq_init(const struct nft_ctx *ctx,
43 const struct nft_expr *expr,
44 const struct nlattr * const tb[])
46 u32 plen = sizeof_field(struct nf_nat_range, min_proto.all);
47 struct nft_masq *priv = nft_expr_priv(expr);
50 if (tb[NFTA_MASQ_FLAGS]) {
51 priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
52 if (priv->flags & ~NF_NAT_RANGE_MASK)
56 if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
57 err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MIN],
58 &priv->sreg_proto_min, plen);
62 if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
63 err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MAX],
64 &priv->sreg_proto_max,
69 priv->sreg_proto_max = priv->sreg_proto_min;
73 return nf_ct_netns_get(ctx->net, ctx->family);
76 static int nft_masq_dump(struct sk_buff *skb,
77 const struct nft_expr *expr, bool reset)
79 const struct nft_masq *priv = nft_expr_priv(expr);
81 if (priv->flags != 0 &&
82 nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
85 if (priv->sreg_proto_min) {
86 if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN,
87 priv->sreg_proto_min) ||
88 nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX,
89 priv->sreg_proto_max))
99 static void nft_masq_eval(const struct nft_expr *expr,
100 struct nft_regs *regs,
101 const struct nft_pktinfo *pkt)
103 const struct nft_masq *priv = nft_expr_priv(expr);
104 struct nf_nat_range2 range;
106 memset(&range, 0, sizeof(range));
107 range.flags = priv->flags;
108 if (priv->sreg_proto_min) {
109 range.min_proto.all = (__force __be16)
110 nft_reg_load16(®s->data[priv->sreg_proto_min]);
111 range.max_proto.all = (__force __be16)
112 nft_reg_load16(®s->data[priv->sreg_proto_max]);
115 switch (nft_pf(pkt)) {
117 regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb,
122 #ifdef CONFIG_NF_TABLES_IPV6
124 regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range,
135 nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
137 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
140 static struct nft_expr_type nft_masq_ipv4_type;
141 static const struct nft_expr_ops nft_masq_ipv4_ops = {
142 .type = &nft_masq_ipv4_type,
143 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
144 .eval = nft_masq_eval,
145 .init = nft_masq_init,
146 .destroy = nft_masq_ipv4_destroy,
147 .dump = nft_masq_dump,
148 .validate = nft_masq_validate,
149 .reduce = NFT_REDUCE_READONLY,
152 static struct nft_expr_type nft_masq_ipv4_type __read_mostly = {
153 .family = NFPROTO_IPV4,
155 .ops = &nft_masq_ipv4_ops,
156 .policy = nft_masq_policy,
157 .maxattr = NFTA_MASQ_MAX,
158 .owner = THIS_MODULE,
161 #ifdef CONFIG_NF_TABLES_IPV6
163 nft_masq_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
165 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
168 static struct nft_expr_type nft_masq_ipv6_type;
169 static const struct nft_expr_ops nft_masq_ipv6_ops = {
170 .type = &nft_masq_ipv6_type,
171 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
172 .eval = nft_masq_eval,
173 .init = nft_masq_init,
174 .destroy = nft_masq_ipv6_destroy,
175 .dump = nft_masq_dump,
176 .validate = nft_masq_validate,
177 .reduce = NFT_REDUCE_READONLY,
180 static struct nft_expr_type nft_masq_ipv6_type __read_mostly = {
181 .family = NFPROTO_IPV6,
183 .ops = &nft_masq_ipv6_ops,
184 .policy = nft_masq_policy,
185 .maxattr = NFTA_MASQ_MAX,
186 .owner = THIS_MODULE,
189 static int __init nft_masq_module_init_ipv6(void)
191 return nft_register_expr(&nft_masq_ipv6_type);
194 static void nft_masq_module_exit_ipv6(void)
196 nft_unregister_expr(&nft_masq_ipv6_type);
199 static inline int nft_masq_module_init_ipv6(void) { return 0; }
200 static inline void nft_masq_module_exit_ipv6(void) {}
203 #ifdef CONFIG_NF_TABLES_INET
205 nft_masq_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
207 nf_ct_netns_put(ctx->net, NFPROTO_INET);
210 static struct nft_expr_type nft_masq_inet_type;
211 static const struct nft_expr_ops nft_masq_inet_ops = {
212 .type = &nft_masq_inet_type,
213 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
214 .eval = nft_masq_eval,
215 .init = nft_masq_init,
216 .destroy = nft_masq_inet_destroy,
217 .dump = nft_masq_dump,
218 .validate = nft_masq_validate,
219 .reduce = NFT_REDUCE_READONLY,
222 static struct nft_expr_type nft_masq_inet_type __read_mostly = {
223 .family = NFPROTO_INET,
225 .ops = &nft_masq_inet_ops,
226 .policy = nft_masq_policy,
227 .maxattr = NFTA_MASQ_MAX,
228 .owner = THIS_MODULE,
231 static int __init nft_masq_module_init_inet(void)
233 return nft_register_expr(&nft_masq_inet_type);
236 static void nft_masq_module_exit_inet(void)
238 nft_unregister_expr(&nft_masq_inet_type);
241 static inline int nft_masq_module_init_inet(void) { return 0; }
242 static inline void nft_masq_module_exit_inet(void) {}
245 static int __init nft_masq_module_init(void)
249 ret = nft_masq_module_init_ipv6();
253 ret = nft_masq_module_init_inet();
255 nft_masq_module_exit_ipv6();
259 ret = nft_register_expr(&nft_masq_ipv4_type);
261 nft_masq_module_exit_inet();
262 nft_masq_module_exit_ipv6();
266 ret = nf_nat_masquerade_inet_register_notifiers();
268 nft_masq_module_exit_ipv6();
269 nft_masq_module_exit_inet();
270 nft_unregister_expr(&nft_masq_ipv4_type);
277 static void __exit nft_masq_module_exit(void)
279 nft_masq_module_exit_ipv6();
280 nft_masq_module_exit_inet();
281 nft_unregister_expr(&nft_masq_ipv4_type);
282 nf_nat_masquerade_inet_unregister_notifiers();
285 module_init(nft_masq_module_init);
286 module_exit(nft_masq_module_exit);
288 MODULE_LICENSE("GPL");
290 MODULE_ALIAS_NFT_EXPR("masq");
291 MODULE_DESCRIPTION("Netfilter nftables masquerade expression support");