1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
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, const struct nft_expr *expr)
78 const struct nft_masq *priv = nft_expr_priv(expr);
80 if (priv->flags != 0 &&
81 nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
84 if (priv->sreg_proto_min) {
85 if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN,
86 priv->sreg_proto_min) ||
87 nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX,
88 priv->sreg_proto_max))
98 static void nft_masq_ipv4_eval(const struct nft_expr *expr,
99 struct nft_regs *regs,
100 const struct nft_pktinfo *pkt)
102 struct nft_masq *priv = nft_expr_priv(expr);
103 struct nf_nat_range2 range;
105 memset(&range, 0, sizeof(range));
106 range.flags = priv->flags;
107 if (priv->sreg_proto_min) {
108 range.min_proto.all = (__force __be16)nft_reg_load16(
109 ®s->data[priv->sreg_proto_min]);
110 range.max_proto.all = (__force __be16)nft_reg_load16(
111 ®s->data[priv->sreg_proto_max]);
113 regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, nft_hook(pkt),
114 &range, nft_out(pkt));
118 nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
120 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
123 static struct nft_expr_type nft_masq_ipv4_type;
124 static const struct nft_expr_ops nft_masq_ipv4_ops = {
125 .type = &nft_masq_ipv4_type,
126 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
127 .eval = nft_masq_ipv4_eval,
128 .init = nft_masq_init,
129 .destroy = nft_masq_ipv4_destroy,
130 .dump = nft_masq_dump,
131 .validate = nft_masq_validate,
132 .reduce = NFT_REDUCE_READONLY,
135 static struct nft_expr_type nft_masq_ipv4_type __read_mostly = {
136 .family = NFPROTO_IPV4,
138 .ops = &nft_masq_ipv4_ops,
139 .policy = nft_masq_policy,
140 .maxattr = NFTA_MASQ_MAX,
141 .owner = THIS_MODULE,
144 #ifdef CONFIG_NF_TABLES_IPV6
145 static void nft_masq_ipv6_eval(const struct nft_expr *expr,
146 struct nft_regs *regs,
147 const struct nft_pktinfo *pkt)
149 struct nft_masq *priv = nft_expr_priv(expr);
150 struct nf_nat_range2 range;
152 memset(&range, 0, sizeof(range));
153 range.flags = priv->flags;
154 if (priv->sreg_proto_min) {
155 range.min_proto.all = (__force __be16)nft_reg_load16(
156 ®s->data[priv->sreg_proto_min]);
157 range.max_proto.all = (__force __be16)nft_reg_load16(
158 ®s->data[priv->sreg_proto_max]);
160 regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range,
165 nft_masq_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
167 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
170 static struct nft_expr_type nft_masq_ipv6_type;
171 static const struct nft_expr_ops nft_masq_ipv6_ops = {
172 .type = &nft_masq_ipv6_type,
173 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
174 .eval = nft_masq_ipv6_eval,
175 .init = nft_masq_init,
176 .destroy = nft_masq_ipv6_destroy,
177 .dump = nft_masq_dump,
178 .validate = nft_masq_validate,
179 .reduce = NFT_REDUCE_READONLY,
182 static struct nft_expr_type nft_masq_ipv6_type __read_mostly = {
183 .family = NFPROTO_IPV6,
185 .ops = &nft_masq_ipv6_ops,
186 .policy = nft_masq_policy,
187 .maxattr = NFTA_MASQ_MAX,
188 .owner = THIS_MODULE,
191 static int __init nft_masq_module_init_ipv6(void)
193 return nft_register_expr(&nft_masq_ipv6_type);
196 static void nft_masq_module_exit_ipv6(void)
198 nft_unregister_expr(&nft_masq_ipv6_type);
201 static inline int nft_masq_module_init_ipv6(void) { return 0; }
202 static inline void nft_masq_module_exit_ipv6(void) {}
205 #ifdef CONFIG_NF_TABLES_INET
206 static void nft_masq_inet_eval(const struct nft_expr *expr,
207 struct nft_regs *regs,
208 const struct nft_pktinfo *pkt)
210 switch (nft_pf(pkt)) {
212 return nft_masq_ipv4_eval(expr, regs, pkt);
214 return nft_masq_ipv6_eval(expr, regs, pkt);
221 nft_masq_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
223 nf_ct_netns_put(ctx->net, NFPROTO_INET);
226 static struct nft_expr_type nft_masq_inet_type;
227 static const struct nft_expr_ops nft_masq_inet_ops = {
228 .type = &nft_masq_inet_type,
229 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)),
230 .eval = nft_masq_inet_eval,
231 .init = nft_masq_init,
232 .destroy = nft_masq_inet_destroy,
233 .dump = nft_masq_dump,
234 .validate = nft_masq_validate,
235 .reduce = NFT_REDUCE_READONLY,
238 static struct nft_expr_type nft_masq_inet_type __read_mostly = {
239 .family = NFPROTO_INET,
241 .ops = &nft_masq_inet_ops,
242 .policy = nft_masq_policy,
243 .maxattr = NFTA_MASQ_MAX,
244 .owner = THIS_MODULE,
247 static int __init nft_masq_module_init_inet(void)
249 return nft_register_expr(&nft_masq_inet_type);
252 static void nft_masq_module_exit_inet(void)
254 nft_unregister_expr(&nft_masq_inet_type);
257 static inline int nft_masq_module_init_inet(void) { return 0; }
258 static inline void nft_masq_module_exit_inet(void) {}
261 static int __init nft_masq_module_init(void)
265 ret = nft_masq_module_init_ipv6();
269 ret = nft_masq_module_init_inet();
271 nft_masq_module_exit_ipv6();
275 ret = nft_register_expr(&nft_masq_ipv4_type);
277 nft_masq_module_exit_inet();
278 nft_masq_module_exit_ipv6();
282 ret = nf_nat_masquerade_inet_register_notifiers();
284 nft_masq_module_exit_ipv6();
285 nft_masq_module_exit_inet();
286 nft_unregister_expr(&nft_masq_ipv4_type);
293 static void __exit nft_masq_module_exit(void)
295 nft_masq_module_exit_ipv6();
296 nft_masq_module_exit_inet();
297 nft_unregister_expr(&nft_masq_ipv4_type);
298 nf_nat_masquerade_inet_unregister_notifiers();
301 module_init(nft_masq_module_init);
302 module_exit(nft_masq_module_exit);
304 MODULE_LICENSE("GPL");
305 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
306 MODULE_ALIAS_NFT_EXPR("masq");
307 MODULE_DESCRIPTION("Netfilter nftables masquerade expression support");