1 // SPDX-License-Identifier: GPL-2.0-only
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
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/skbuff.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>
28 enum nf_nat_manip_type type
:8;
33 static void nft_nat_setup_addr(struct nf_nat_range2
*range
,
34 const struct nft_regs
*regs
,
35 const struct nft_nat
*priv
)
37 switch (priv
->family
) {
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
];
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
));
53 static void nft_nat_setup_proto(struct nf_nat_range2
*range
,
54 const struct nft_regs
*regs
,
55 const struct nft_nat
*priv
)
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
]);
63 static void nft_nat_setup_netmap(struct nf_nat_range2
*range
,
64 const struct nft_pktinfo
*pkt
,
65 const struct nft_nat
*priv
)
67 struct sk_buff
*skb
= pkt
->skb
;
68 union nf_inet_addr new_addr
;
74 if (nft_pf(pkt
) == NFPROTO_IPV4
) {
75 new_addr
.ip
= ip_hdr(skb
)->saddr
;
76 len
= sizeof(struct in_addr
);
78 new_addr
.in6
= ipv6_hdr(skb
)->saddr
;
79 len
= sizeof(struct in6_addr
);
83 if (nft_pf(pkt
) == NFPROTO_IPV4
) {
84 new_addr
.ip
= ip_hdr(skb
)->daddr
;
85 len
= sizeof(struct in_addr
);
87 new_addr
.in6
= ipv6_hdr(skb
)->daddr
;
88 len
= sizeof(struct in6_addr
);
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
;
99 range
->min_addr
= new_addr
;
100 range
->max_addr
= new_addr
;
103 static void nft_nat_eval(const struct nft_expr
*expr
,
104 struct nft_regs
*regs
,
105 const struct nft_pktinfo
*pkt
)
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
);
110 struct nf_nat_range2 range
;
112 memset(&range
, 0, sizeof(range
));
114 if (priv
->sreg_addr_min
) {
115 nft_nat_setup_addr(&range
, regs
, priv
);
116 if (priv
->flags
& NF_NAT_RANGE_NETMAP
)
117 nft_nat_setup_netmap(&range
, pkt
, priv
);
120 if (priv
->sreg_proto_min
)
121 nft_nat_setup_proto(&range
, regs
, priv
);
123 range
.flags
= priv
->flags
;
125 regs
->verdict
.code
= nf_nat_setup_info(ct
, &range
, priv
->type
);
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
},
136 NLA_POLICY_MASK(NLA_BE32
, NF_NAT_RANGE_MASK
),
139 static int nft_nat_validate(const struct nft_ctx
*ctx
,
140 const struct nft_expr
*expr
)
142 struct nft_nat
*priv
= nft_expr_priv(expr
);
145 if (ctx
->family
!= NFPROTO_IPV4
&&
146 ctx
->family
!= NFPROTO_IPV6
&&
147 ctx
->family
!= NFPROTO_INET
)
150 err
= nft_chain_validate_dependency(ctx
->chain
, NFT_CHAIN_T_NAT
);
154 switch (priv
->type
) {
156 err
= nft_chain_validate_hooks(ctx
->chain
,
157 (1 << NF_INET_POST_ROUTING
) |
158 (1 << NF_INET_LOCAL_IN
));
161 err
= nft_chain_validate_hooks(ctx
->chain
,
162 (1 << NF_INET_PRE_ROUTING
) |
163 (1 << NF_INET_LOCAL_OUT
));
170 static int nft_nat_init(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
,
171 const struct nlattr
* const tb
[])
173 struct nft_nat
*priv
= nft_expr_priv(expr
);
174 unsigned int alen
, plen
;
178 if (tb
[NFTA_NAT_TYPE
] == NULL
||
179 (tb
[NFTA_NAT_REG_ADDR_MIN
] == NULL
&&
180 tb
[NFTA_NAT_REG_PROTO_MIN
] == NULL
))
183 switch (ntohl(nla_get_be32(tb
[NFTA_NAT_TYPE
]))) {
185 priv
->type
= NF_NAT_MANIP_SRC
;
188 priv
->type
= NF_NAT_MANIP_DST
;
194 if (tb
[NFTA_NAT_FAMILY
] == NULL
)
197 family
= ntohl(nla_get_be32(tb
[NFTA_NAT_FAMILY
]));
198 if (ctx
->family
!= NFPROTO_INET
&& ctx
->family
!= family
)
203 alen
= sizeof_field(struct nf_nat_range
, min_addr
.ip
);
206 alen
= sizeof_field(struct nf_nat_range
, min_addr
.ip6
);
209 if (tb
[NFTA_NAT_REG_ADDR_MIN
])
210 return -EAFNOSUPPORT
;
213 priv
->family
= family
;
215 if (tb
[NFTA_NAT_REG_ADDR_MIN
]) {
216 err
= nft_parse_register_load(ctx
, tb
[NFTA_NAT_REG_ADDR_MIN
],
217 &priv
->sreg_addr_min
, alen
);
221 if (tb
[NFTA_NAT_REG_ADDR_MAX
]) {
222 err
= nft_parse_register_load(ctx
, tb
[NFTA_NAT_REG_ADDR_MAX
],
223 &priv
->sreg_addr_max
,
228 priv
->sreg_addr_max
= priv
->sreg_addr_min
;
231 priv
->flags
|= NF_NAT_RANGE_MAP_IPS
;
234 plen
= sizeof_field(struct nf_nat_range
, min_proto
.all
);
235 if (tb
[NFTA_NAT_REG_PROTO_MIN
]) {
236 err
= nft_parse_register_load(ctx
, tb
[NFTA_NAT_REG_PROTO_MIN
],
237 &priv
->sreg_proto_min
, plen
);
241 if (tb
[NFTA_NAT_REG_PROTO_MAX
]) {
242 err
= nft_parse_register_load(ctx
, tb
[NFTA_NAT_REG_PROTO_MAX
],
243 &priv
->sreg_proto_max
,
248 priv
->sreg_proto_max
= priv
->sreg_proto_min
;
251 priv
->flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
254 if (tb
[NFTA_NAT_FLAGS
])
255 priv
->flags
|= ntohl(nla_get_be32(tb
[NFTA_NAT_FLAGS
]));
257 return nf_ct_netns_get(ctx
->net
, family
);
260 static int nft_nat_dump(struct sk_buff
*skb
,
261 const struct nft_expr
*expr
, bool reset
)
263 const struct nft_nat
*priv
= nft_expr_priv(expr
);
265 switch (priv
->type
) {
266 case NF_NAT_MANIP_SRC
:
267 if (nla_put_be32(skb
, NFTA_NAT_TYPE
, htonl(NFT_NAT_SNAT
)))
268 goto nla_put_failure
;
270 case NF_NAT_MANIP_DST
:
271 if (nla_put_be32(skb
, NFTA_NAT_TYPE
, htonl(NFT_NAT_DNAT
)))
272 goto nla_put_failure
;
276 if (nla_put_be32(skb
, NFTA_NAT_FAMILY
, htonl(priv
->family
)))
277 goto nla_put_failure
;
279 if (priv
->sreg_addr_min
) {
280 if (nft_dump_register(skb
, NFTA_NAT_REG_ADDR_MIN
,
281 priv
->sreg_addr_min
) ||
282 nft_dump_register(skb
, NFTA_NAT_REG_ADDR_MAX
,
283 priv
->sreg_addr_max
))
284 goto nla_put_failure
;
287 if (priv
->sreg_proto_min
) {
288 if (nft_dump_register(skb
, NFTA_NAT_REG_PROTO_MIN
,
289 priv
->sreg_proto_min
) ||
290 nft_dump_register(skb
, NFTA_NAT_REG_PROTO_MAX
,
291 priv
->sreg_proto_max
))
292 goto nla_put_failure
;
295 if (priv
->flags
!= 0) {
296 if (nla_put_be32(skb
, NFTA_NAT_FLAGS
, htonl(priv
->flags
)))
297 goto nla_put_failure
;
307 nft_nat_destroy(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
309 const struct nft_nat
*priv
= nft_expr_priv(expr
);
311 nf_ct_netns_put(ctx
->net
, priv
->family
);
314 static struct nft_expr_type nft_nat_type
;
315 static const struct nft_expr_ops nft_nat_ops
= {
316 .type
= &nft_nat_type
,
317 .size
= NFT_EXPR_SIZE(sizeof(struct nft_nat
)),
318 .eval
= nft_nat_eval
,
319 .init
= nft_nat_init
,
320 .destroy
= nft_nat_destroy
,
321 .dump
= nft_nat_dump
,
322 .validate
= nft_nat_validate
,
323 .reduce
= NFT_REDUCE_READONLY
,
326 static struct nft_expr_type nft_nat_type __read_mostly
= {
329 .policy
= nft_nat_policy
,
330 .maxattr
= NFTA_NAT_MAX
,
331 .owner
= THIS_MODULE
,
334 #ifdef CONFIG_NF_TABLES_INET
335 static void nft_nat_inet_eval(const struct nft_expr
*expr
,
336 struct nft_regs
*regs
,
337 const struct nft_pktinfo
*pkt
)
339 const struct nft_nat
*priv
= nft_expr_priv(expr
);
341 if (priv
->family
== nft_pf(pkt
) ||
342 priv
->family
== NFPROTO_INET
)
343 nft_nat_eval(expr
, regs
, pkt
);
346 static const struct nft_expr_ops nft_nat_inet_ops
= {
347 .type
= &nft_nat_type
,
348 .size
= NFT_EXPR_SIZE(sizeof(struct nft_nat
)),
349 .eval
= nft_nat_inet_eval
,
350 .init
= nft_nat_init
,
351 .destroy
= nft_nat_destroy
,
352 .dump
= nft_nat_dump
,
353 .validate
= nft_nat_validate
,
354 .reduce
= NFT_REDUCE_READONLY
,
357 static struct nft_expr_type nft_inet_nat_type __read_mostly
= {
359 .family
= NFPROTO_INET
,
360 .ops
= &nft_nat_inet_ops
,
361 .policy
= nft_nat_policy
,
362 .maxattr
= NFTA_NAT_MAX
,
363 .owner
= THIS_MODULE
,
366 static int nft_nat_inet_module_init(void)
368 return nft_register_expr(&nft_inet_nat_type
);
371 static void nft_nat_inet_module_exit(void)
373 nft_unregister_expr(&nft_inet_nat_type
);
376 static int nft_nat_inet_module_init(void) { return 0; }
377 static void nft_nat_inet_module_exit(void) { }
380 static int __init
nft_nat_module_init(void)
382 int ret
= nft_nat_inet_module_init();
387 ret
= nft_register_expr(&nft_nat_type
);
389 nft_nat_inet_module_exit();
394 static void __exit
nft_nat_module_exit(void)
396 nft_nat_inet_module_exit();
397 nft_unregister_expr(&nft_nat_type
);
400 module_init(nft_nat_module_init
);
401 module_exit(nft_nat_module_exit
);
403 MODULE_LICENSE("GPL");
404 MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
405 MODULE_ALIAS_NFT_EXPR("nat");
406 MODULE_DESCRIPTION("Network Address Translation support");