]>
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 { | |
4f16d25c PNA |
24 | u8 sreg_addr_min; |
25 | u8 sreg_addr_max; | |
26 | u8 sreg_proto_min; | |
27 | u8 sreg_proto_max; | |
a4c2e8be PM |
28 | enum nf_nat_manip_type type:8; |
29 | u8 family; | |
e42eff8a | 30 | u16 flags; |
eb31628e TB |
31 | }; |
32 | ||
acd766e3 PNA |
33 | static void nft_nat_setup_addr(struct nf_nat_range2 *range, |
34 | const struct nft_regs *regs, | |
35 | const struct nft_nat *priv) | |
36 | { | |
37 | switch (priv->family) { | |
38 | case AF_INET: | |
39 | range->min_addr.ip = (__force __be32) | |
40 | regs->data[priv->sreg_addr_min]; | |
41 | range->max_addr.ip = (__force __be32) | |
42 | regs->data[priv->sreg_addr_max]; | |
43 | break; | |
44 | case AF_INET6: | |
45 | memcpy(range->min_addr.ip6, ®s->data[priv->sreg_addr_min], | |
46 | sizeof(range->min_addr.ip6)); | |
47 | memcpy(range->max_addr.ip6, ®s->data[priv->sreg_addr_max], | |
48 | sizeof(range->max_addr.ip6)); | |
49 | break; | |
50 | } | |
51 | } | |
52 | ||
53 | static void nft_nat_setup_proto(struct nf_nat_range2 *range, | |
54 | const struct nft_regs *regs, | |
55 | const struct nft_nat *priv) | |
56 | { | |
57 | range->min_proto.all = (__force __be16) | |
58 | nft_reg_load16(®s->data[priv->sreg_proto_min]); | |
59 | range->max_proto.all = (__force __be16) | |
60 | nft_reg_load16(®s->data[priv->sreg_proto_max]); | |
61 | } | |
62 | ||
3ff7ddb1 PNA |
63 | static void nft_nat_setup_netmap(struct nf_nat_range2 *range, |
64 | const struct nft_pktinfo *pkt, | |
65 | const struct nft_nat *priv) | |
66 | { | |
67 | struct sk_buff *skb = pkt->skb; | |
68 | union nf_inet_addr new_addr; | |
69 | __be32 netmask; | |
70 | int i, len = 0; | |
71 | ||
72 | switch (priv->type) { | |
73 | case NFT_NAT_SNAT: | |
74 | if (nft_pf(pkt) == NFPROTO_IPV4) { | |
75 | new_addr.ip = ip_hdr(skb)->saddr; | |
76 | len = sizeof(struct in_addr); | |
77 | } else { | |
78 | new_addr.in6 = ipv6_hdr(skb)->saddr; | |
79 | len = sizeof(struct in6_addr); | |
80 | } | |
81 | break; | |
82 | case NFT_NAT_DNAT: | |
83 | if (nft_pf(pkt) == NFPROTO_IPV4) { | |
84 | new_addr.ip = ip_hdr(skb)->daddr; | |
85 | len = sizeof(struct in_addr); | |
86 | } else { | |
87 | new_addr.in6 = ipv6_hdr(skb)->daddr; | |
88 | len = sizeof(struct in6_addr); | |
89 | } | |
90 | break; | |
91 | } | |
92 | ||
93 | for (i = 0; i < len / sizeof(__be32); i++) { | |
94 | netmask = ~(range->min_addr.ip6[i] ^ range->max_addr.ip6[i]); | |
95 | new_addr.ip6[i] &= ~netmask; | |
96 | new_addr.ip6[i] |= range->min_addr.ip6[i] & netmask; | |
97 | } | |
98 | ||
99 | range->min_addr = new_addr; | |
100 | range->max_addr = new_addr; | |
101 | } | |
102 | ||
eb31628e | 103 | static void nft_nat_eval(const struct nft_expr *expr, |
a55e22e9 | 104 | struct nft_regs *regs, |
eb31628e TB |
105 | const struct nft_pktinfo *pkt) |
106 | { | |
107 | const struct nft_nat *priv = nft_expr_priv(expr); | |
108 | enum ip_conntrack_info ctinfo; | |
109 | struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo); | |
2eb0f624 | 110 | struct nf_nat_range2 range; |
eb31628e TB |
111 | |
112 | memset(&range, 0, sizeof(range)); | |
3ff7ddb1 PNA |
113 | |
114 | if (priv->sreg_addr_min) { | |
acd766e3 | 115 | nft_nat_setup_addr(&range, regs, priv); |
3ff7ddb1 PNA |
116 | if (priv->flags & NF_NAT_RANGE_NETMAP) |
117 | nft_nat_setup_netmap(&range, pkt, priv); | |
118 | } | |
eb31628e | 119 | |
acd766e3 PNA |
120 | if (priv->sreg_proto_min) |
121 | nft_nat_setup_proto(&range, regs, priv); | |
eb31628e | 122 | |
4566aa44 | 123 | range.flags = priv->flags; |
e42eff8a | 124 | |
a55e22e9 | 125 | regs->verdict.code = nf_nat_setup_info(ct, &range, priv->type); |
eb31628e TB |
126 | } |
127 | ||
128 | static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = { | |
129 | [NFTA_NAT_TYPE] = { .type = NLA_U32 }, | |
130 | [NFTA_NAT_FAMILY] = { .type = NLA_U32 }, | |
131 | [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 }, | |
132 | [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 }, | |
133 | [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 }, | |
134 | [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 }, | |
100a11b6 FW |
135 | [NFTA_NAT_FLAGS] = |
136 | NLA_POLICY_MASK(NLA_BE32, NF_NAT_RANGE_MASK), | |
eb31628e TB |
137 | }; |
138 | ||
75e8d06d PNA |
139 | static int nft_nat_validate(const struct nft_ctx *ctx, |
140 | const struct nft_expr *expr, | |
141 | const struct nft_data **data) | |
eb31628e TB |
142 | { |
143 | struct nft_nat *priv = nft_expr_priv(expr); | |
144 | int err; | |
145 | ||
7210e4e3 PNA |
146 | err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT); |
147 | if (err < 0) | |
148 | return err; | |
149 | ||
75e8d06d PNA |
150 | switch (priv->type) { |
151 | case NFT_NAT_SNAT: | |
152 | err = nft_chain_validate_hooks(ctx->chain, | |
153 | (1 << NF_INET_POST_ROUTING) | | |
154 | (1 << NF_INET_LOCAL_IN)); | |
155 | break; | |
156 | case NFT_NAT_DNAT: | |
157 | err = nft_chain_validate_hooks(ctx->chain, | |
158 | (1 << NF_INET_PRE_ROUTING) | | |
159 | (1 << NF_INET_LOCAL_OUT)); | |
160 | break; | |
161 | } | |
162 | ||
163 | return err; | |
164 | } | |
165 | ||
166 | static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, | |
167 | const struct nlattr * const tb[]) | |
168 | { | |
169 | struct nft_nat *priv = nft_expr_priv(expr); | |
d07db988 | 170 | unsigned int alen, plen; |
75e8d06d PNA |
171 | u32 family; |
172 | int err; | |
173 | ||
5c819a39 PNA |
174 | if (tb[NFTA_NAT_TYPE] == NULL || |
175 | (tb[NFTA_NAT_REG_ADDR_MIN] == NULL && | |
176 | tb[NFTA_NAT_REG_PROTO_MIN] == NULL)) | |
eb31628e TB |
177 | return -EINVAL; |
178 | ||
179 | switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) { | |
180 | case NFT_NAT_SNAT: | |
181 | priv->type = NF_NAT_MANIP_SRC; | |
182 | break; | |
183 | case NFT_NAT_DNAT: | |
184 | priv->type = NF_NAT_MANIP_DST; | |
185 | break; | |
186 | default: | |
0d7c8346 | 187 | return -EOPNOTSUPP; |
eb31628e TB |
188 | } |
189 | ||
190 | if (tb[NFTA_NAT_FAMILY] == NULL) | |
191 | return -EINVAL; | |
192 | ||
a4c2e8be | 193 | family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY])); |
d164385e | 194 | if (ctx->family != NFPROTO_INET && ctx->family != family) |
a4c2e8be | 195 | return -EOPNOTSUPP; |
d07db988 PM |
196 | |
197 | switch (family) { | |
198 | case NFPROTO_IPV4: | |
c593642c | 199 | alen = sizeof_field(struct nf_nat_range, min_addr.ip); |
d07db988 PM |
200 | break; |
201 | case NFPROTO_IPV6: | |
c593642c | 202 | alen = sizeof_field(struct nf_nat_range, min_addr.ip6); |
d07db988 PM |
203 | break; |
204 | default: | |
a33f387e PNA |
205 | if (tb[NFTA_NAT_REG_ADDR_MIN]) |
206 | return -EAFNOSUPPORT; | |
207 | break; | |
d07db988 | 208 | } |
a4c2e8be | 209 | priv->family = family; |
eb31628e TB |
210 | |
211 | if (tb[NFTA_NAT_REG_ADDR_MIN]) { | |
4f16d25c PNA |
212 | err = nft_parse_register_load(tb[NFTA_NAT_REG_ADDR_MIN], |
213 | &priv->sreg_addr_min, alen); | |
eb31628e TB |
214 | if (err < 0) |
215 | return err; | |
eb31628e | 216 | |
61cfac6b | 217 | if (tb[NFTA_NAT_REG_ADDR_MAX]) { |
4f16d25c PNA |
218 | err = nft_parse_register_load(tb[NFTA_NAT_REG_ADDR_MAX], |
219 | &priv->sreg_addr_max, | |
220 | alen); | |
61cfac6b PNA |
221 | if (err < 0) |
222 | return err; | |
223 | } else { | |
224 | priv->sreg_addr_max = priv->sreg_addr_min; | |
225 | } | |
4566aa44 PNA |
226 | |
227 | priv->flags |= NF_NAT_RANGE_MAP_IPS; | |
61cfac6b | 228 | } |
eb31628e | 229 | |
068d82e7 | 230 | plen = sizeof_field(struct nf_nat_range, min_proto.all); |
eb31628e | 231 | if (tb[NFTA_NAT_REG_PROTO_MIN]) { |
4f16d25c PNA |
232 | err = nft_parse_register_load(tb[NFTA_NAT_REG_PROTO_MIN], |
233 | &priv->sreg_proto_min, plen); | |
eb31628e TB |
234 | if (err < 0) |
235 | return err; | |
eb31628e | 236 | |
61cfac6b | 237 | if (tb[NFTA_NAT_REG_PROTO_MAX]) { |
4f16d25c PNA |
238 | err = nft_parse_register_load(tb[NFTA_NAT_REG_PROTO_MAX], |
239 | &priv->sreg_proto_max, | |
240 | plen); | |
61cfac6b PNA |
241 | if (err < 0) |
242 | return err; | |
243 | } else { | |
244 | priv->sreg_proto_max = priv->sreg_proto_min; | |
245 | } | |
4566aa44 PNA |
246 | |
247 | priv->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
61cfac6b | 248 | } |
eb31628e | 249 | |
100a11b6 | 250 | if (tb[NFTA_NAT_FLAGS]) |
4566aa44 | 251 | priv->flags |= ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS])); |
e42eff8a | 252 | |
20afd423 | 253 | return nf_ct_netns_get(ctx->net, family); |
eb31628e TB |
254 | } |
255 | ||
7d34aa3e PS |
256 | static int nft_nat_dump(struct sk_buff *skb, |
257 | const struct nft_expr *expr, bool reset) | |
eb31628e TB |
258 | { |
259 | const struct nft_nat *priv = nft_expr_priv(expr); | |
260 | ||
261 | switch (priv->type) { | |
262 | case NF_NAT_MANIP_SRC: | |
263 | if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT))) | |
264 | goto nla_put_failure; | |
265 | break; | |
266 | case NF_NAT_MANIP_DST: | |
267 | if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT))) | |
268 | goto nla_put_failure; | |
269 | break; | |
270 | } | |
271 | ||
272 | if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family))) | |
273 | goto nla_put_failure; | |
1e2d56a5 PNA |
274 | |
275 | if (priv->sreg_addr_min) { | |
b1c96ed3 PM |
276 | if (nft_dump_register(skb, NFTA_NAT_REG_ADDR_MIN, |
277 | priv->sreg_addr_min) || | |
278 | nft_dump_register(skb, NFTA_NAT_REG_ADDR_MAX, | |
279 | priv->sreg_addr_max)) | |
1e2d56a5 PNA |
280 | goto nla_put_failure; |
281 | } | |
282 | ||
91513606 | 283 | if (priv->sreg_proto_min) { |
b1c96ed3 PM |
284 | if (nft_dump_register(skb, NFTA_NAT_REG_PROTO_MIN, |
285 | priv->sreg_proto_min) || | |
286 | nft_dump_register(skb, NFTA_NAT_REG_PROTO_MAX, | |
287 | priv->sreg_proto_max)) | |
91513606 PNA |
288 | goto nla_put_failure; |
289 | } | |
e42eff8a AB |
290 | |
291 | if (priv->flags != 0) { | |
292 | if (nla_put_be32(skb, NFTA_NAT_FLAGS, htonl(priv->flags))) | |
293 | goto nla_put_failure; | |
294 | } | |
295 | ||
eb31628e TB |
296 | return 0; |
297 | ||
298 | nla_put_failure: | |
299 | return -1; | |
300 | } | |
301 | ||
20afd423 FW |
302 | static void |
303 | nft_nat_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) | |
304 | { | |
305 | const struct nft_nat *priv = nft_expr_priv(expr); | |
306 | ||
307 | nf_ct_netns_put(ctx->net, priv->family); | |
308 | } | |
309 | ||
eb31628e TB |
310 | static struct nft_expr_type nft_nat_type; |
311 | static const struct nft_expr_ops nft_nat_ops = { | |
312 | .type = &nft_nat_type, | |
313 | .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)), | |
314 | .eval = nft_nat_eval, | |
315 | .init = nft_nat_init, | |
20afd423 | 316 | .destroy = nft_nat_destroy, |
eb31628e | 317 | .dump = nft_nat_dump, |
7210e4e3 | 318 | .validate = nft_nat_validate, |
b2d30654 | 319 | .reduce = NFT_REDUCE_READONLY, |
eb31628e TB |
320 | }; |
321 | ||
322 | static struct nft_expr_type nft_nat_type __read_mostly = { | |
323 | .name = "nat", | |
324 | .ops = &nft_nat_ops, | |
325 | .policy = nft_nat_policy, | |
326 | .maxattr = NFTA_NAT_MAX, | |
327 | .owner = THIS_MODULE, | |
328 | }; | |
329 | ||
d164385e FW |
330 | #ifdef CONFIG_NF_TABLES_INET |
331 | static void nft_nat_inet_eval(const struct nft_expr *expr, | |
332 | struct nft_regs *regs, | |
333 | const struct nft_pktinfo *pkt) | |
334 | { | |
335 | const struct nft_nat *priv = nft_expr_priv(expr); | |
336 | ||
282e5f8f FW |
337 | if (priv->family == nft_pf(pkt) || |
338 | priv->family == NFPROTO_INET) | |
d164385e FW |
339 | nft_nat_eval(expr, regs, pkt); |
340 | } | |
341 | ||
342 | static const struct nft_expr_ops nft_nat_inet_ops = { | |
343 | .type = &nft_nat_type, | |
344 | .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)), | |
345 | .eval = nft_nat_inet_eval, | |
346 | .init = nft_nat_init, | |
347 | .destroy = nft_nat_destroy, | |
348 | .dump = nft_nat_dump, | |
349 | .validate = nft_nat_validate, | |
b2d30654 | 350 | .reduce = NFT_REDUCE_READONLY, |
d164385e FW |
351 | }; |
352 | ||
353 | static struct nft_expr_type nft_inet_nat_type __read_mostly = { | |
354 | .name = "nat", | |
355 | .family = NFPROTO_INET, | |
356 | .ops = &nft_nat_inet_ops, | |
357 | .policy = nft_nat_policy, | |
358 | .maxattr = NFTA_NAT_MAX, | |
359 | .owner = THIS_MODULE, | |
360 | }; | |
361 | ||
362 | static int nft_nat_inet_module_init(void) | |
363 | { | |
364 | return nft_register_expr(&nft_inet_nat_type); | |
365 | } | |
366 | ||
367 | static void nft_nat_inet_module_exit(void) | |
368 | { | |
369 | nft_unregister_expr(&nft_inet_nat_type); | |
370 | } | |
371 | #else | |
372 | static int nft_nat_inet_module_init(void) { return 0; } | |
373 | static void nft_nat_inet_module_exit(void) { } | |
374 | #endif | |
375 | ||
eb31628e TB |
376 | static int __init nft_nat_module_init(void) |
377 | { | |
d164385e FW |
378 | int ret = nft_nat_inet_module_init(); |
379 | ||
380 | if (ret) | |
381 | return ret; | |
382 | ||
383 | ret = nft_register_expr(&nft_nat_type); | |
384 | if (ret) | |
385 | nft_nat_inet_module_exit(); | |
386 | ||
387 | return ret; | |
eb31628e TB |
388 | } |
389 | ||
390 | static void __exit nft_nat_module_exit(void) | |
391 | { | |
d164385e | 392 | nft_nat_inet_module_exit(); |
eb31628e TB |
393 | nft_unregister_expr(&nft_nat_type); |
394 | } | |
395 | ||
396 | module_init(nft_nat_module_init); | |
397 | module_exit(nft_nat_module_exit); | |
398 | ||
399 | MODULE_LICENSE("GPL"); | |
400 | MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>"); | |
401 | MODULE_ALIAS_NFT_EXPR("nat"); | |
4cacc395 | 402 | MODULE_DESCRIPTION("Network Address Translation support"); |