blob: 2a0adc497bbb4dc5aa399e1b124ca5f677c6bceb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Arturo Borrero9ba1f7262014-09-08 13:45:00 +02002/*
Arturo Borrero Gonzalezcd727512016-10-18 14:02:29 +02003 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
Arturo Borrero9ba1f7262014-09-08 13:45:00 +02004 */
5
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>
Florian Westphalbf8981a2019-04-09 10:44:06 +020014#include <net/netfilter/nf_nat_masquerade.h>
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020015
Florian Westphala9ce8492019-02-28 12:02:51 +010016struct nft_masq {
17 u32 flags;
Pablo Neira Ayuso4f16d252021-01-25 17:28:18 +010018 u8 sreg_proto_min;
19 u8 sreg_proto_max;
Florian Westphala9ce8492019-02-28 12:02:51 +010020};
21
22static const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010023 [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
24 [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 },
25 [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 },
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020026};
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020027
Florian Westphala9ce8492019-02-28 12:02:51 +010028static int nft_masq_validate(const struct nft_ctx *ctx,
29 const struct nft_expr *expr,
30 const struct nft_data **data)
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010031{
32 int err;
33
34 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
35 if (err < 0)
36 return err;
37
38 return nft_chain_validate_hooks(ctx->chain,
39 (1 << NF_INET_POST_ROUTING));
40}
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010041
Florian Westphala9ce8492019-02-28 12:02:51 +010042static int nft_masq_init(const struct nft_ctx *ctx,
43 const struct nft_expr *expr,
44 const struct nlattr * const tb[])
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020045{
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080046 u32 plen = sizeof_field(struct nf_nat_range, min_addr.all);
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020047 struct nft_masq *priv = nft_expr_priv(expr);
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +020048 int err;
49
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010050 if (tb[NFTA_MASQ_FLAGS]) {
51 priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
52 if (priv->flags & ~NF_NAT_RANGE_MASK)
53 return -EINVAL;
54 }
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020055
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010056 if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
Pablo Neira Ayuso4f16d252021-01-25 17:28:18 +010057 err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MIN],
58 &priv->sreg_proto_min, plen);
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010059 if (err < 0)
60 return err;
61
62 if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
Pablo Neira Ayuso4f16d252021-01-25 17:28:18 +010063 err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MAX],
64 &priv->sreg_proto_max,
65 plen);
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010066 if (err < 0)
67 return err;
68 } else {
69 priv->sreg_proto_max = priv->sreg_proto_min;
70 }
71 }
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020072
Pablo Neira Ayuso36596da2018-01-09 02:38:03 +010073 return nf_ct_netns_get(ctx->net, ctx->family);
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020074}
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020075
Florian Westphala9ce8492019-02-28 12:02:51 +010076static int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr)
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020077{
78 const struct nft_masq *priv = nft_expr_priv(expr);
79
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010080 if (priv->flags != 0 &&
81 nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020082 goto nla_put_failure;
83
Pablo Neira Ayuso8a6bf5d2016-03-01 19:55:14 +010084 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))
89 goto nla_put_failure;
90 }
91
Arturo Borrero9ba1f7262014-09-08 13:45:00 +020092 return 0;
93
94nla_put_failure:
95 return -1;
96}
Florian Westphala9ce8492019-02-28 12:02:51 +010097
98static void nft_masq_ipv4_eval(const struct nft_expr *expr,
99 struct nft_regs *regs,
100 const struct nft_pktinfo *pkt)
101{
102 struct nft_masq *priv = nft_expr_priv(expr);
103 struct nf_nat_range2 range;
104
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 &regs->data[priv->sreg_proto_min]);
110 range.max_proto.all = (__force __be16)nft_reg_load16(
111 &regs->data[priv->sreg_proto_max]);
112 }
113 regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, nft_hook(pkt),
114 &range, nft_out(pkt));
115}
116
117static void
118nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
119{
120 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
121}
122
123static struct nft_expr_type nft_masq_ipv4_type;
124static 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,
Pablo Neira Ayusob2d30652022-03-14 18:23:00 +0100132 .reduce = NFT_REDUCE_READONLY,
Florian Westphala9ce8492019-02-28 12:02:51 +0100133};
134
135static struct nft_expr_type nft_masq_ipv4_type __read_mostly = {
136 .family = NFPROTO_IPV4,
137 .name = "masq",
138 .ops = &nft_masq_ipv4_ops,
139 .policy = nft_masq_policy,
140 .maxattr = NFTA_MASQ_MAX,
141 .owner = THIS_MODULE,
142};
143
144#ifdef CONFIG_NF_TABLES_IPV6
145static void nft_masq_ipv6_eval(const struct nft_expr *expr,
146 struct nft_regs *regs,
147 const struct nft_pktinfo *pkt)
148{
149 struct nft_masq *priv = nft_expr_priv(expr);
150 struct nf_nat_range2 range;
151
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 &regs->data[priv->sreg_proto_min]);
157 range.max_proto.all = (__force __be16)nft_reg_load16(
158 &regs->data[priv->sreg_proto_max]);
159 }
160 regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range,
161 nft_out(pkt));
162}
163
164static void
165nft_masq_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
166{
167 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
168}
169
170static struct nft_expr_type nft_masq_ipv6_type;
171static 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,
Pablo Neira Ayusob2d30652022-03-14 18:23:00 +0100179 .reduce = NFT_REDUCE_READONLY,
Florian Westphala9ce8492019-02-28 12:02:51 +0100180};
181
182static struct nft_expr_type nft_masq_ipv6_type __read_mostly = {
183 .family = NFPROTO_IPV6,
184 .name = "masq",
185 .ops = &nft_masq_ipv6_ops,
186 .policy = nft_masq_policy,
187 .maxattr = NFTA_MASQ_MAX,
188 .owner = THIS_MODULE,
189};
190
191static int __init nft_masq_module_init_ipv6(void)
192{
Florian Westphal610a4312019-04-09 10:44:08 +0200193 return nft_register_expr(&nft_masq_ipv6_type);
Florian Westphala9ce8492019-02-28 12:02:51 +0100194}
195
196static void nft_masq_module_exit_ipv6(void)
197{
198 nft_unregister_expr(&nft_masq_ipv6_type);
Florian Westphala9ce8492019-02-28 12:02:51 +0100199}
200#else
201static inline int nft_masq_module_init_ipv6(void) { return 0; }
202static inline void nft_masq_module_exit_ipv6(void) {}
203#endif
204
Florian Westphal071657d2019-03-27 09:22:27 +0100205#ifdef CONFIG_NF_TABLES_INET
206static void nft_masq_inet_eval(const struct nft_expr *expr,
207 struct nft_regs *regs,
208 const struct nft_pktinfo *pkt)
209{
210 switch (nft_pf(pkt)) {
211 case NFPROTO_IPV4:
212 return nft_masq_ipv4_eval(expr, regs, pkt);
213 case NFPROTO_IPV6:
214 return nft_masq_ipv6_eval(expr, regs, pkt);
215 }
216
217 WARN_ON_ONCE(1);
218}
219
220static void
221nft_masq_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
222{
223 nf_ct_netns_put(ctx->net, NFPROTO_INET);
224}
225
226static struct nft_expr_type nft_masq_inet_type;
227static 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,
Pablo Neira Ayusob2d30652022-03-14 18:23:00 +0100235 .reduce = NFT_REDUCE_READONLY,
Florian Westphal071657d2019-03-27 09:22:27 +0100236};
237
238static struct nft_expr_type nft_masq_inet_type __read_mostly = {
239 .family = NFPROTO_INET,
240 .name = "masq",
241 .ops = &nft_masq_inet_ops,
242 .policy = nft_masq_policy,
243 .maxattr = NFTA_MASQ_MAX,
244 .owner = THIS_MODULE,
245};
246
247static int __init nft_masq_module_init_inet(void)
248{
249 return nft_register_expr(&nft_masq_inet_type);
250}
251
252static void nft_masq_module_exit_inet(void)
253{
254 nft_unregister_expr(&nft_masq_inet_type);
255}
256#else
257static inline int nft_masq_module_init_inet(void) { return 0; }
258static inline void nft_masq_module_exit_inet(void) {}
259#endif
260
Florian Westphala9ce8492019-02-28 12:02:51 +0100261static int __init nft_masq_module_init(void)
262{
263 int ret;
264
265 ret = nft_masq_module_init_ipv6();
266 if (ret < 0)
267 return ret;
268
Florian Westphal071657d2019-03-27 09:22:27 +0100269 ret = nft_masq_module_init_inet();
270 if (ret < 0) {
271 nft_masq_module_exit_ipv6();
272 return ret;
273 }
274
Florian Westphala9ce8492019-02-28 12:02:51 +0100275 ret = nft_register_expr(&nft_masq_ipv4_type);
276 if (ret < 0) {
Florian Westphal071657d2019-03-27 09:22:27 +0100277 nft_masq_module_exit_inet();
Florian Westphala9ce8492019-02-28 12:02:51 +0100278 nft_masq_module_exit_ipv6();
279 return ret;
280 }
281
Florian Westphal610a4312019-04-09 10:44:08 +0200282 ret = nf_nat_masquerade_inet_register_notifiers();
Florian Westphala9ce8492019-02-28 12:02:51 +0100283 if (ret < 0) {
284 nft_masq_module_exit_ipv6();
Florian Westphal071657d2019-03-27 09:22:27 +0100285 nft_masq_module_exit_inet();
Florian Westphala9ce8492019-02-28 12:02:51 +0100286 nft_unregister_expr(&nft_masq_ipv4_type);
287 return ret;
288 }
289
290 return ret;
291}
292
293static void __exit nft_masq_module_exit(void)
294{
295 nft_masq_module_exit_ipv6();
Florian Westphal071657d2019-03-27 09:22:27 +0100296 nft_masq_module_exit_inet();
Florian Westphala9ce8492019-02-28 12:02:51 +0100297 nft_unregister_expr(&nft_masq_ipv4_type);
Florian Westphal610a4312019-04-09 10:44:08 +0200298 nf_nat_masquerade_inet_unregister_notifiers();
Florian Westphala9ce8492019-02-28 12:02:51 +0100299}
300
301module_init(nft_masq_module_init);
302module_exit(nft_masq_module_exit);
Arturo Borrero9ba1f7262014-09-08 13:45:00 +0200303
Arturo Borrero9ba1f7262014-09-08 13:45:00 +0200304MODULE_LICENSE("GPL");
Arturo Borrero Gonzalezcd727512016-10-18 14:02:29 +0200305MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
Pablo Neira Ayuso51429672019-05-24 22:26:34 +0200306MODULE_ALIAS_NFT_EXPR("masq");
Rob Gill4cacc392020-06-21 05:27:36 +0000307MODULE_DESCRIPTION("Netfilter nftables masquerade expression support");