Commit | Line | Data |
---|---|---|
75a6faf6 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
eb31628e TB |
2 | /* |
3 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> | |
4 | * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org> | |
5 | * Copyright (c) 2012 Intel Corporation | |
eb31628e TB |
6 | */ |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/skbuff.h> | |
11 | #include <linux/ip.h> | |
12 | #include <linux/string.h> | |
13 | #include <linux/netlink.h> | |
14 | #include <linux/netfilter.h> | |
15 | #include <linux/netfilter_ipv4.h> | |
16 | #include <linux/netfilter/nfnetlink.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
18 | #include <net/netfilter/nf_conntrack.h> | |
19 | #include <net/netfilter/nf_nat.h> | |
eb31628e | 20 | #include <net/netfilter/nf_tables.h> |
eb31628e TB |
21 | #include <net/ip.h> |
22 | ||
23 | struct nft_nat { | |
24 | enum nft_registers sreg_addr_min:8; | |
25 | enum nft_registers sreg_addr_max:8; | |
26 | enum nft_registers sreg_proto_min:8; | |
27 | enum nft_registers sreg_proto_max:8; | |
a4c2e8be PM |
28 | enum nf_nat_manip_type type:8; |
29 | u8 family; | |
e42eff8a | 30 | u16 flags; |
eb31628e TB |
31 | }; |
32 | ||
33 | static void nft_nat_eval(const struct nft_expr *expr, | |
a55e22e9 | 34 | struct nft_regs *regs, |
eb31628e TB |
35 | const struct nft_pktinfo *pkt) |
36 | { | |
37 | const struct nft_nat *priv = nft_expr_priv(expr); | |
38 | enum ip_conntrack_info ctinfo; | |
39 | struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo); | |
2eb0f624 | 40 | struct nf_nat_range2 range; |
eb31628e TB |
41 | |
42 | memset(&range, 0, sizeof(range)); | |
43 | if (priv->sreg_addr_min) { | |
44 | if (priv->family == AF_INET) { | |
98c37b6b | 45 | range.min_addr.ip = (__force __be32) |
49499c3e | 46 | regs->data[priv->sreg_addr_min]; |
98c37b6b | 47 | range.max_addr.ip = (__force __be32) |
49499c3e | 48 | regs->data[priv->sreg_addr_max]; |
eb31628e TB |
49 | |
50 | } else { | |
51 | memcpy(range.min_addr.ip6, | |
49499c3e PM |
52 | ®s->data[priv->sreg_addr_min], |
53 | sizeof(range.min_addr.ip6)); | |
eb31628e | 54 | memcpy(range.max_addr.ip6, |
49499c3e PM |
55 | ®s->data[priv->sreg_addr_max], |
56 | sizeof(range.max_addr.ip6)); | |
eb31628e TB |
57 | } |
58 | range.flags |= NF_NAT_RANGE_MAP_IPS; | |
59 | } | |
60 | ||
61 | if (priv->sreg_proto_min) { | |
10596608 LZ |
62 | range.min_proto.all = (__force __be16)nft_reg_load16( |
63 | ®s->data[priv->sreg_proto_min]); | |
64 | range.max_proto.all = (__force __be16)nft_reg_load16( | |
65 | ®s->data[priv->sreg_proto_max]); | |
eb31628e TB |
66 | range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; |
67 | } | |
68 | ||
e42eff8a AB |
69 | range.flags |= priv->flags; |
70 | ||
a55e22e9 | 71 | regs->verdict.code = nf_nat_setup_info(ct, &range, priv->type); |
eb31628e TB |
72 | } |
73 | ||
74 | static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = { | |
75 | [NFTA_NAT_TYPE] = { .type = NLA_U32 }, | |
76 | [NFTA_NAT_FAMILY] = { .type = NLA_U32 }, | |
77 | [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 }, | |
78 | [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 }, | |
79 | [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 }, | |
80 | [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 }, | |
e42eff8a | 81 | [NFTA_NAT_FLAGS] = { .type = NLA_U32 }, |
eb31628e TB |
82 | }; |
83 | ||
75e8d06d PNA |
84 | static int nft_nat_validate(const struct nft_ctx *ctx, |
85 | const struct nft_expr *expr, | |
86 | const struct nft_data **data) | |
eb31628e TB |
87 | { |
88 | struct nft_nat *priv = nft_expr_priv(expr); | |
89 | int err; | |
90 | ||
7210e4e3 PNA |
91 | err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT); |
92 | if (err < 0) | |
93 | return err; | |
94 | ||
75e8d06d PNA |
95 | switch (priv->type) { |
96 | case NFT_NAT_SNAT: | |
97 | err = nft_chain_validate_hooks(ctx->chain, | |
98 | (1 << NF_INET_POST_ROUTING) | | |
99 | (1 << NF_INET_LOCAL_IN)); | |
100 | break; | |
101 | case NFT_NAT_DNAT: | |
102 | err = nft_chain_validate_hooks(ctx->chain, | |
103 | (1 << NF_INET_PRE_ROUTING) | | |
104 | (1 << NF_INET_LOCAL_OUT)); | |
105 | break; | |
106 | } | |
107 | ||
108 | return err; | |
109 | } | |
110 | ||
111 | static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
112 | const struct nlattr * const tb[]) | |
113 | { | |
114 | struct nft_nat *priv = nft_expr_priv(expr); | |
d07db988 | 115 | unsigned int alen, plen; |
75e8d06d PNA |
116 | u32 family; |
117 | int err; | |
118 | ||
5c819a39 PNA |
119 | if (tb[NFTA_NAT_TYPE] == NULL || |
120 | (tb[NFTA_NAT_REG_ADDR_MIN] == NULL && | |
121 | tb[NFTA_NAT_REG_PROTO_MIN] == NULL)) | |
eb31628e TB |
122 | return -EINVAL; |
123 | ||
124 | switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) { | |
125 | case NFT_NAT_SNAT: | |
126 | priv->type = NF_NAT_MANIP_SRC; | |
127 | break; | |
128 | case NFT_NAT_DNAT: | |
129 | priv->type = NF_NAT_MANIP_DST; | |
130 | break; | |
131 | default: | |
132 | return -EINVAL; | |
133 | } | |
134 | ||
135 | if (tb[NFTA_NAT_FAMILY] == NULL) | |
136 | return -EINVAL; | |
137 | ||
a4c2e8be | 138 | family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY])); |
d164385e | 139 | if (ctx->family != NFPROTO_INET && ctx->family != family) |
a4c2e8be | 140 | return -EOPNOTSUPP; |
d07db988 PM |
141 | |
142 | switch (family) { | |
143 | case NFPROTO_IPV4: | |
144 | alen = FIELD_SIZEOF(struct nf_nat_range, min_addr.ip); | |
145 | break; | |
146 | case NFPROTO_IPV6: | |
147 | alen = FIELD_SIZEOF(struct nf_nat_range, min_addr.ip6); | |
148 | break; | |
149 | default: | |
150 | return -EAFNOSUPPORT; | |
151 | } | |
a4c2e8be | 152 | priv->family = family; |
eb31628e TB |
153 | |
154 | if (tb[NFTA_NAT_REG_ADDR_MIN]) { | |
61cfac6b | 155 | priv->sreg_addr_min = |
b1c96ed3 | 156 | nft_parse_register(tb[NFTA_NAT_REG_ADDR_MIN]); |
d07db988 | 157 | err = nft_validate_register_load(priv->sreg_addr_min, alen); |
eb31628e TB |
158 | if (err < 0) |
159 | return err; | |
eb31628e | 160 | |
61cfac6b PNA |
161 | if (tb[NFTA_NAT_REG_ADDR_MAX]) { |
162 | priv->sreg_addr_max = | |
b1c96ed3 | 163 | nft_parse_register(tb[NFTA_NAT_REG_ADDR_MAX]); |
61cfac6b | 164 | |
d07db988 PM |
165 | err = nft_validate_register_load(priv->sreg_addr_max, |
166 | alen); | |
61cfac6b PNA |
167 | if (err < 0) |
168 | return err; | |
169 | } else { | |
170 | priv->sreg_addr_max = priv->sreg_addr_min; | |
171 | } | |
172 | } | |
eb31628e | 173 | |
d07db988 | 174 | plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all); |
eb31628e | 175 | if (tb[NFTA_NAT_REG_PROTO_MIN]) { |
61cfac6b | 176 | priv->sreg_proto_min = |
b1c96ed3 | 177 | nft_parse_register(tb[NFTA_NAT_REG_PROTO_MIN]); |
61cfac6b | 178 | |
d07db988 | 179 | err = nft_validate_register_load(priv->sreg_proto_min, plen); |
eb31628e TB |
180 | if (err < 0) |
181 | return err; | |
eb31628e | 182 | |
61cfac6b PNA |
183 | if (tb[NFTA_NAT_REG_PROTO_MAX]) { |
184 | priv->sreg_proto_max = | |
b1c96ed3 | 185 | nft_parse_register(tb[NFTA_NAT_REG_PROTO_MAX]); |
61cfac6b | 186 | |
d07db988 PM |
187 | err = nft_validate_register_load(priv->sreg_proto_max, |
188 | plen); | |
61cfac6b PNA |
189 | if (err < 0) |
190 | return err; | |
191 | } else { | |
192 | priv->sreg_proto_max = priv->sreg_proto_min; | |
193 | } | |
194 | } | |
eb31628e | 195 | |
e42eff8a AB |
196 | if (tb[NFTA_NAT_FLAGS]) { |
197 | priv->flags = ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS])); | |
198 | if (priv->flags & ~NF_NAT_RANGE_MASK) | |
199 | return -EINVAL; | |
200 | } | |
201 | ||
20afd423 | 202 | return nf_ct_netns_get(ctx->net, family); |
eb31628e TB |
203 | } |
204 | ||
205 | static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
206 | { | |
207 | const struct nft_nat *priv = nft_expr_priv(expr); | |
208 | ||
209 | switch (priv->type) { | |
210 | case NF_NAT_MANIP_SRC: | |
211 | if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT))) | |
212 | goto nla_put_failure; | |
213 | break; | |
214 | case NF_NAT_MANIP_DST: | |
215 | if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT))) | |
216 | goto nla_put_failure; | |
217 | break; | |
218 | } | |
219 | ||
220 | if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family))) | |
221 | goto nla_put_failure; | |
1e2d56a5 PNA |
222 | |
223 | if (priv->sreg_addr_min) { | |
b1c96ed3 PM |
224 | if (nft_dump_register(skb, NFTA_NAT_REG_ADDR_MIN, |
225 | priv->sreg_addr_min) || | |
226 | nft_dump_register(skb, NFTA_NAT_REG_ADDR_MAX, | |
227 | priv->sreg_addr_max)) | |
1e2d56a5 PNA |
228 | goto nla_put_failure; |
229 | } | |
230 | ||
91513606 | 231 | if (priv->sreg_proto_min) { |
b1c96ed3 PM |
232 | if (nft_dump_register(skb, NFTA_NAT_REG_PROTO_MIN, |
233 | priv->sreg_proto_min) || | |
234 | nft_dump_register(skb, NFTA_NAT_REG_PROTO_MAX, | |
235 | priv->sreg_proto_max)) | |
91513606 PNA |
236 | goto nla_put_failure; |
237 | } | |
e42eff8a AB |
238 | |
239 | if (priv->flags != 0) { | |
240 | if (nla_put_be32(skb, NFTA_NAT_FLAGS, htonl(priv->flags))) | |
241 | goto nla_put_failure; | |
242 | } | |
243 | ||
eb31628e TB |
244 | return 0; |
245 | ||
246 | nla_put_failure: | |
247 | return -1; | |
248 | } | |
249 | ||
20afd423 FW |
250 | static void |
251 | nft_nat_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) | |
252 | { | |
253 | const struct nft_nat *priv = nft_expr_priv(expr); | |
254 | ||
255 | nf_ct_netns_put(ctx->net, priv->family); | |
256 | } | |
257 | ||
eb31628e TB |
258 | static struct nft_expr_type nft_nat_type; |
259 | static const struct nft_expr_ops nft_nat_ops = { | |
260 | .type = &nft_nat_type, | |
261 | .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)), | |
262 | .eval = nft_nat_eval, | |
263 | .init = nft_nat_init, | |
20afd423 | 264 | .destroy = nft_nat_destroy, |
eb31628e | 265 | .dump = nft_nat_dump, |
7210e4e3 | 266 | .validate = nft_nat_validate, |
eb31628e TB |
267 | }; |
268 | ||
269 | static struct nft_expr_type nft_nat_type __read_mostly = { | |
270 | .name = "nat", | |
271 | .ops = &nft_nat_ops, | |
272 | .policy = nft_nat_policy, | |
273 | .maxattr = NFTA_NAT_MAX, | |
274 | .owner = THIS_MODULE, | |
275 | }; | |
276 | ||
d164385e FW |
277 | #ifdef CONFIG_NF_TABLES_INET |
278 | static void nft_nat_inet_eval(const struct nft_expr *expr, | |
279 | struct nft_regs *regs, | |
280 | const struct nft_pktinfo *pkt) | |
281 | { | |
282 | const struct nft_nat *priv = nft_expr_priv(expr); | |
283 | ||
284 | if (priv->family == nft_pf(pkt)) | |
285 | nft_nat_eval(expr, regs, pkt); | |
286 | } | |
287 | ||
288 | static const struct nft_expr_ops nft_nat_inet_ops = { | |
289 | .type = &nft_nat_type, | |
290 | .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)), | |
291 | .eval = nft_nat_inet_eval, | |
292 | .init = nft_nat_init, | |
293 | .destroy = nft_nat_destroy, | |
294 | .dump = nft_nat_dump, | |
295 | .validate = nft_nat_validate, | |
296 | }; | |
297 | ||
298 | static struct nft_expr_type nft_inet_nat_type __read_mostly = { | |
299 | .name = "nat", | |
300 | .family = NFPROTO_INET, | |
301 | .ops = &nft_nat_inet_ops, | |
302 | .policy = nft_nat_policy, | |
303 | .maxattr = NFTA_NAT_MAX, | |
304 | .owner = THIS_MODULE, | |
305 | }; | |
306 | ||
307 | static int nft_nat_inet_module_init(void) | |
308 | { | |
309 | return nft_register_expr(&nft_inet_nat_type); | |
310 | } | |
311 | ||
312 | static void nft_nat_inet_module_exit(void) | |
313 | { | |
314 | nft_unregister_expr(&nft_inet_nat_type); | |
315 | } | |
316 | #else | |
317 | static int nft_nat_inet_module_init(void) { return 0; } | |
318 | static void nft_nat_inet_module_exit(void) { } | |
319 | #endif | |
320 | ||
eb31628e TB |
321 | static int __init nft_nat_module_init(void) |
322 | { | |
d164385e FW |
323 | int ret = nft_nat_inet_module_init(); |
324 | ||
325 | if (ret) | |
326 | return ret; | |
327 | ||
328 | ret = nft_register_expr(&nft_nat_type); | |
329 | if (ret) | |
330 | nft_nat_inet_module_exit(); | |
331 | ||
332 | return ret; | |
eb31628e TB |
333 | } |
334 | ||
335 | static void __exit nft_nat_module_exit(void) | |
336 | { | |
d164385e | 337 | nft_nat_inet_module_exit(); |
eb31628e TB |
338 | nft_unregister_expr(&nft_nat_type); |
339 | } | |
340 | ||
341 | module_init(nft_nat_module_init); | |
342 | module_exit(nft_nat_module_exit); | |
343 | ||
344 | MODULE_LICENSE("GPL"); | |
345 | MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>"); | |
346 | MODULE_ALIAS_NFT_EXPR("nat"); |