1 | // SPDX-License-Identifier: GPL-2.0-only |
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 |
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> |
20 | #include <net/netfilter/nf_tables.h> |
21 | #include <net/ip.h> |
22 | |
23 | struct nft_nat { |
24 | u8 sreg_addr_min; |
25 | u8 sreg_addr_max; |
26 | u8 sreg_proto_min; |
27 | u8 sreg_proto_max; |
28 | enum nf_nat_manip_type type:8; |
29 | u8 family; |
30 | u16 flags; |
31 | }; |
32 | |
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(sreg: ®s->data[priv->sreg_proto_min]); |
59 | range->max_proto.all = (__force __be16) |
60 | nft_reg_load16(sreg: ®s->data[priv->sreg_proto_max]); |
61 | } |
62 | |
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 | |
103 | static void nft_nat_eval(const struct nft_expr *expr, |
104 | struct nft_regs *regs, |
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(skb: pkt->skb, ctinfo: &ctinfo); |
110 | struct nf_nat_range2 range; |
111 | |
112 | memset(&range, 0, sizeof(range)); |
113 | |
114 | if (priv->sreg_addr_min) { |
115 | nft_nat_setup_addr(range: &range, regs, priv); |
116 | if (priv->flags & NF_NAT_RANGE_NETMAP) |
117 | nft_nat_setup_netmap(range: &range, pkt, priv); |
118 | } |
119 | |
120 | if (priv->sreg_proto_min) |
121 | nft_nat_setup_proto(range: &range, regs, priv); |
122 | |
123 | range.flags = priv->flags; |
124 | |
125 | regs->verdict.code = nf_nat_setup_info(ct, range: &range, maniptype: priv->type); |
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 }, |
135 | [NFTA_NAT_FLAGS] = |
136 | NLA_POLICY_MASK(NLA_BE32, NF_NAT_RANGE_MASK), |
137 | }; |
138 | |
139 | static int nft_nat_validate(const struct nft_ctx *ctx, |
140 | const struct nft_expr *expr, |
141 | const struct nft_data **data) |
142 | { |
143 | struct nft_nat *priv = nft_expr_priv(expr); |
144 | int err; |
145 | |
146 | if (ctx->family != NFPROTO_IPV4 && |
147 | ctx->family != NFPROTO_IPV6 && |
148 | ctx->family != NFPROTO_INET) |
149 | return -EOPNOTSUPP; |
150 | |
151 | err = nft_chain_validate_dependency(chain: ctx->chain, type: NFT_CHAIN_T_NAT); |
152 | if (err < 0) |
153 | return err; |
154 | |
155 | switch (priv->type) { |
156 | case NFT_NAT_SNAT: |
157 | err = nft_chain_validate_hooks(chain: ctx->chain, |
158 | hook_flags: (1 << NF_INET_POST_ROUTING) | |
159 | (1 << NF_INET_LOCAL_IN)); |
160 | break; |
161 | case NFT_NAT_DNAT: |
162 | err = nft_chain_validate_hooks(chain: ctx->chain, |
163 | hook_flags: (1 << NF_INET_PRE_ROUTING) | |
164 | (1 << NF_INET_LOCAL_OUT)); |
165 | break; |
166 | } |
167 | |
168 | return err; |
169 | } |
170 | |
171 | static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr, |
172 | const struct nlattr * const tb[]) |
173 | { |
174 | struct nft_nat *priv = nft_expr_priv(expr); |
175 | unsigned int alen, plen; |
176 | u32 family; |
177 | int err; |
178 | |
179 | if (tb[NFTA_NAT_TYPE] == NULL || |
180 | (tb[NFTA_NAT_REG_ADDR_MIN] == NULL && |
181 | tb[NFTA_NAT_REG_PROTO_MIN] == NULL)) |
182 | return -EINVAL; |
183 | |
184 | switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) { |
185 | case NFT_NAT_SNAT: |
186 | priv->type = NF_NAT_MANIP_SRC; |
187 | break; |
188 | case NFT_NAT_DNAT: |
189 | priv->type = NF_NAT_MANIP_DST; |
190 | break; |
191 | default: |
192 | return -EOPNOTSUPP; |
193 | } |
194 | |
195 | if (tb[NFTA_NAT_FAMILY] == NULL) |
196 | return -EINVAL; |
197 | |
198 | family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY])); |
199 | if (ctx->family != NFPROTO_INET && ctx->family != family) |
200 | return -EOPNOTSUPP; |
201 | |
202 | switch (family) { |
203 | case NFPROTO_IPV4: |
204 | alen = sizeof_field(struct nf_nat_range, min_addr.ip); |
205 | break; |
206 | case NFPROTO_IPV6: |
207 | alen = sizeof_field(struct nf_nat_range, min_addr.ip6); |
208 | break; |
209 | default: |
210 | if (tb[NFTA_NAT_REG_ADDR_MIN]) |
211 | return -EAFNOSUPPORT; |
212 | break; |
213 | } |
214 | priv->family = family; |
215 | |
216 | if (tb[NFTA_NAT_REG_ADDR_MIN]) { |
217 | err = nft_parse_register_load(attr: tb[NFTA_NAT_REG_ADDR_MIN], |
218 | sreg: &priv->sreg_addr_min, len: alen); |
219 | if (err < 0) |
220 | return err; |
221 | |
222 | if (tb[NFTA_NAT_REG_ADDR_MAX]) { |
223 | err = nft_parse_register_load(attr: tb[NFTA_NAT_REG_ADDR_MAX], |
224 | sreg: &priv->sreg_addr_max, |
225 | len: alen); |
226 | if (err < 0) |
227 | return err; |
228 | } else { |
229 | priv->sreg_addr_max = priv->sreg_addr_min; |
230 | } |
231 | |
232 | priv->flags |= NF_NAT_RANGE_MAP_IPS; |
233 | } |
234 | |
235 | plen = sizeof_field(struct nf_nat_range, min_proto.all); |
236 | if (tb[NFTA_NAT_REG_PROTO_MIN]) { |
237 | err = nft_parse_register_load(attr: tb[NFTA_NAT_REG_PROTO_MIN], |
238 | sreg: &priv->sreg_proto_min, len: plen); |
239 | if (err < 0) |
240 | return err; |
241 | |
242 | if (tb[NFTA_NAT_REG_PROTO_MAX]) { |
243 | err = nft_parse_register_load(attr: tb[NFTA_NAT_REG_PROTO_MAX], |
244 | sreg: &priv->sreg_proto_max, |
245 | len: plen); |
246 | if (err < 0) |
247 | return err; |
248 | } else { |
249 | priv->sreg_proto_max = priv->sreg_proto_min; |
250 | } |
251 | |
252 | priv->flags |= NF_NAT_RANGE_PROTO_SPECIFIED; |
253 | } |
254 | |
255 | if (tb[NFTA_NAT_FLAGS]) |
256 | priv->flags |= ntohl(nla_get_be32(tb[NFTA_NAT_FLAGS])); |
257 | |
258 | return nf_ct_netns_get(net: ctx->net, nfproto: family); |
259 | } |
260 | |
261 | static int nft_nat_dump(struct sk_buff *skb, |
262 | const struct nft_expr *expr, bool reset) |
263 | { |
264 | const struct nft_nat *priv = nft_expr_priv(expr); |
265 | |
266 | switch (priv->type) { |
267 | case NF_NAT_MANIP_SRC: |
268 | if (nla_put_be32(skb, attrtype: NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT))) |
269 | goto nla_put_failure; |
270 | break; |
271 | case NF_NAT_MANIP_DST: |
272 | if (nla_put_be32(skb, attrtype: NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT))) |
273 | goto nla_put_failure; |
274 | break; |
275 | } |
276 | |
277 | if (nla_put_be32(skb, attrtype: NFTA_NAT_FAMILY, htonl(priv->family))) |
278 | goto nla_put_failure; |
279 | |
280 | if (priv->sreg_addr_min) { |
281 | if (nft_dump_register(skb, attr: NFTA_NAT_REG_ADDR_MIN, |
282 | reg: priv->sreg_addr_min) || |
283 | nft_dump_register(skb, attr: NFTA_NAT_REG_ADDR_MAX, |
284 | reg: priv->sreg_addr_max)) |
285 | goto nla_put_failure; |
286 | } |
287 | |
288 | if (priv->sreg_proto_min) { |
289 | if (nft_dump_register(skb, attr: NFTA_NAT_REG_PROTO_MIN, |
290 | reg: priv->sreg_proto_min) || |
291 | nft_dump_register(skb, attr: NFTA_NAT_REG_PROTO_MAX, |
292 | reg: priv->sreg_proto_max)) |
293 | goto nla_put_failure; |
294 | } |
295 | |
296 | if (priv->flags != 0) { |
297 | if (nla_put_be32(skb, attrtype: NFTA_NAT_FLAGS, htonl(priv->flags))) |
298 | goto nla_put_failure; |
299 | } |
300 | |
301 | return 0; |
302 | |
303 | nla_put_failure: |
304 | return -1; |
305 | } |
306 | |
307 | static void |
308 | nft_nat_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
309 | { |
310 | const struct nft_nat *priv = nft_expr_priv(expr); |
311 | |
312 | nf_ct_netns_put(net: ctx->net, nfproto: priv->family); |
313 | } |
314 | |
315 | static struct nft_expr_type nft_nat_type; |
316 | static const struct nft_expr_ops nft_nat_ops = { |
317 | .type = &nft_nat_type, |
318 | .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)), |
319 | .eval = nft_nat_eval, |
320 | .init = nft_nat_init, |
321 | .destroy = nft_nat_destroy, |
322 | .dump = nft_nat_dump, |
323 | .validate = nft_nat_validate, |
324 | .reduce = NFT_REDUCE_READONLY, |
325 | }; |
326 | |
327 | static struct nft_expr_type nft_nat_type __read_mostly = { |
328 | .name = "nat" , |
329 | .ops = &nft_nat_ops, |
330 | .policy = nft_nat_policy, |
331 | .maxattr = NFTA_NAT_MAX, |
332 | .owner = THIS_MODULE, |
333 | }; |
334 | |
335 | #ifdef CONFIG_NF_TABLES_INET |
336 | static void nft_nat_inet_eval(const struct nft_expr *expr, |
337 | struct nft_regs *regs, |
338 | const struct nft_pktinfo *pkt) |
339 | { |
340 | const struct nft_nat *priv = nft_expr_priv(expr); |
341 | |
342 | if (priv->family == nft_pf(pkt) || |
343 | priv->family == NFPROTO_INET) |
344 | nft_nat_eval(expr, regs, pkt); |
345 | } |
346 | |
347 | static const struct nft_expr_ops nft_nat_inet_ops = { |
348 | .type = &nft_nat_type, |
349 | .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)), |
350 | .eval = nft_nat_inet_eval, |
351 | .init = nft_nat_init, |
352 | .destroy = nft_nat_destroy, |
353 | .dump = nft_nat_dump, |
354 | .validate = nft_nat_validate, |
355 | .reduce = NFT_REDUCE_READONLY, |
356 | }; |
357 | |
358 | static struct nft_expr_type nft_inet_nat_type __read_mostly = { |
359 | .name = "nat" , |
360 | .family = NFPROTO_INET, |
361 | .ops = &nft_nat_inet_ops, |
362 | .policy = nft_nat_policy, |
363 | .maxattr = NFTA_NAT_MAX, |
364 | .owner = THIS_MODULE, |
365 | }; |
366 | |
367 | static int nft_nat_inet_module_init(void) |
368 | { |
369 | return nft_register_expr(&nft_inet_nat_type); |
370 | } |
371 | |
372 | static void nft_nat_inet_module_exit(void) |
373 | { |
374 | nft_unregister_expr(&nft_inet_nat_type); |
375 | } |
376 | #else |
377 | static int nft_nat_inet_module_init(void) { return 0; } |
378 | static void nft_nat_inet_module_exit(void) { } |
379 | #endif |
380 | |
381 | static int __init nft_nat_module_init(void) |
382 | { |
383 | int ret = nft_nat_inet_module_init(); |
384 | |
385 | if (ret) |
386 | return ret; |
387 | |
388 | ret = nft_register_expr(&nft_nat_type); |
389 | if (ret) |
390 | nft_nat_inet_module_exit(); |
391 | |
392 | return ret; |
393 | } |
394 | |
395 | static void __exit nft_nat_module_exit(void) |
396 | { |
397 | nft_nat_inet_module_exit(); |
398 | nft_unregister_expr(&nft_nat_type); |
399 | } |
400 | |
401 | module_init(nft_nat_module_init); |
402 | module_exit(nft_nat_module_exit); |
403 | |
404 | MODULE_LICENSE("GPL" ); |
405 | MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>" ); |
406 | MODULE_ALIAS_NFT_EXPR("nat" ); |
407 | MODULE_DESCRIPTION("Network Address Translation support" ); |
408 | |