1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
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_nat.h>
13 #include <net/netfilter/nf_nat_redirect.h>
14 #include <net/netfilter/nf_tables.h>
22 static const struct nla_policy nft_redir_policy[NFTA_REDIR_MAX + 1] = {
23 [NFTA_REDIR_REG_PROTO_MIN] = { .type = NLA_U32 },
24 [NFTA_REDIR_REG_PROTO_MAX] = { .type = NLA_U32 },
25 [NFTA_REDIR_FLAGS] = { .type = NLA_U32 },
28 static int nft_redir_validate(const struct nft_ctx *ctx,
29 const struct nft_expr *expr,
30 const struct nft_data **data)
34 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
38 return nft_chain_validate_hooks(ctx->chain,
39 (1 << NF_INET_PRE_ROUTING) |
40 (1 << NF_INET_LOCAL_OUT));
43 static int nft_redir_init(const struct nft_ctx *ctx,
44 const struct nft_expr *expr,
45 const struct nlattr * const tb[])
47 struct nft_redir *priv = nft_expr_priv(expr);
51 plen = sizeof_field(struct nf_nat_range, min_addr.all);
52 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
53 err = nft_parse_register_load(tb[NFTA_REDIR_REG_PROTO_MIN],
54 &priv->sreg_proto_min, plen);
58 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
59 err = nft_parse_register_load(tb[NFTA_REDIR_REG_PROTO_MAX],
60 &priv->sreg_proto_max,
65 priv->sreg_proto_max = priv->sreg_proto_min;
69 if (tb[NFTA_REDIR_FLAGS]) {
70 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
71 if (priv->flags & ~NF_NAT_RANGE_MASK)
75 return nf_ct_netns_get(ctx->net, ctx->family);
78 static int nft_redir_dump(struct sk_buff *skb,
79 const struct nft_expr *expr, bool reset)
81 const struct nft_redir *priv = nft_expr_priv(expr);
83 if (priv->sreg_proto_min) {
84 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
85 priv->sreg_proto_min))
87 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
88 priv->sreg_proto_max))
92 if (priv->flags != 0 &&
93 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
102 static void nft_redir_ipv4_eval(const struct nft_expr *expr,
103 struct nft_regs *regs,
104 const struct nft_pktinfo *pkt)
106 struct nft_redir *priv = nft_expr_priv(expr);
107 struct nf_nat_ipv4_multi_range_compat mr;
109 memset(&mr, 0, sizeof(mr));
110 if (priv->sreg_proto_min) {
111 mr.range[0].min.all = (__force __be16)nft_reg_load16(
112 ®s->data[priv->sreg_proto_min]);
113 mr.range[0].max.all = (__force __be16)nft_reg_load16(
114 ®s->data[priv->sreg_proto_max]);
115 mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
118 mr.range[0].flags |= priv->flags;
120 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &mr, nft_hook(pkt));
124 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
126 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
129 static struct nft_expr_type nft_redir_ipv4_type;
130 static const struct nft_expr_ops nft_redir_ipv4_ops = {
131 .type = &nft_redir_ipv4_type,
132 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
133 .eval = nft_redir_ipv4_eval,
134 .init = nft_redir_init,
135 .destroy = nft_redir_ipv4_destroy,
136 .dump = nft_redir_dump,
137 .validate = nft_redir_validate,
138 .reduce = NFT_REDUCE_READONLY,
141 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
142 .family = NFPROTO_IPV4,
144 .ops = &nft_redir_ipv4_ops,
145 .policy = nft_redir_policy,
146 .maxattr = NFTA_REDIR_MAX,
147 .owner = THIS_MODULE,
150 #ifdef CONFIG_NF_TABLES_IPV6
151 static void nft_redir_ipv6_eval(const struct nft_expr *expr,
152 struct nft_regs *regs,
153 const struct nft_pktinfo *pkt)
155 struct nft_redir *priv = nft_expr_priv(expr);
156 struct nf_nat_range2 range;
158 memset(&range, 0, sizeof(range));
159 if (priv->sreg_proto_min) {
160 range.min_proto.all = (__force __be16)nft_reg_load16(
161 ®s->data[priv->sreg_proto_min]);
162 range.max_proto.all = (__force __be16)nft_reg_load16(
163 ®s->data[priv->sreg_proto_max]);
164 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
167 range.flags |= priv->flags;
170 nf_nat_redirect_ipv6(pkt->skb, &range, nft_hook(pkt));
174 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
176 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
179 static struct nft_expr_type nft_redir_ipv6_type;
180 static const struct nft_expr_ops nft_redir_ipv6_ops = {
181 .type = &nft_redir_ipv6_type,
182 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
183 .eval = nft_redir_ipv6_eval,
184 .init = nft_redir_init,
185 .destroy = nft_redir_ipv6_destroy,
186 .dump = nft_redir_dump,
187 .validate = nft_redir_validate,
188 .reduce = NFT_REDUCE_READONLY,
191 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
192 .family = NFPROTO_IPV6,
194 .ops = &nft_redir_ipv6_ops,
195 .policy = nft_redir_policy,
196 .maxattr = NFTA_REDIR_MAX,
197 .owner = THIS_MODULE,
201 #ifdef CONFIG_NF_TABLES_INET
202 static void nft_redir_inet_eval(const struct nft_expr *expr,
203 struct nft_regs *regs,
204 const struct nft_pktinfo *pkt)
206 switch (nft_pf(pkt)) {
208 return nft_redir_ipv4_eval(expr, regs, pkt);
210 return nft_redir_ipv6_eval(expr, regs, pkt);
217 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
219 nf_ct_netns_put(ctx->net, NFPROTO_INET);
222 static struct nft_expr_type nft_redir_inet_type;
223 static const struct nft_expr_ops nft_redir_inet_ops = {
224 .type = &nft_redir_inet_type,
225 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
226 .eval = nft_redir_inet_eval,
227 .init = nft_redir_init,
228 .destroy = nft_redir_inet_destroy,
229 .dump = nft_redir_dump,
230 .validate = nft_redir_validate,
231 .reduce = NFT_REDUCE_READONLY,
234 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
235 .family = NFPROTO_INET,
237 .ops = &nft_redir_inet_ops,
238 .policy = nft_redir_policy,
239 .maxattr = NFTA_MASQ_MAX,
240 .owner = THIS_MODULE,
243 static int __init nft_redir_module_init_inet(void)
245 return nft_register_expr(&nft_redir_inet_type);
248 static inline int nft_redir_module_init_inet(void) { return 0; }
251 static int __init nft_redir_module_init(void)
253 int ret = nft_register_expr(&nft_redir_ipv4_type);
258 #ifdef CONFIG_NF_TABLES_IPV6
259 ret = nft_register_expr(&nft_redir_ipv6_type);
261 nft_unregister_expr(&nft_redir_ipv4_type);
266 ret = nft_redir_module_init_inet();
268 nft_unregister_expr(&nft_redir_ipv4_type);
269 #ifdef CONFIG_NF_TABLES_IPV6
270 nft_unregister_expr(&nft_redir_ipv6_type);
278 static void __exit nft_redir_module_exit(void)
280 nft_unregister_expr(&nft_redir_ipv4_type);
281 #ifdef CONFIG_NF_TABLES_IPV6
282 nft_unregister_expr(&nft_redir_ipv6_type);
284 #ifdef CONFIG_NF_TABLES_INET
285 nft_unregister_expr(&nft_redir_inet_type);
289 module_init(nft_redir_module_init);
290 module_exit(nft_redir_module_exit);
292 MODULE_LICENSE("GPL");
293 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
294 MODULE_ALIAS_NFT_EXPR("redir");
295 MODULE_DESCRIPTION("Netfilter nftables redirect support");