1 // SPDX-License-Identifier: GPL-2.0-only
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 },
26 NLA_POLICY_MASK(NLA_BE32, NF_NAT_RANGE_MASK),
29 static int nft_redir_validate(const struct nft_ctx *ctx,
30 const struct nft_expr *expr,
31 const struct nft_data **data)
35 err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
39 return nft_chain_validate_hooks(ctx->chain,
40 (1 << NF_INET_PRE_ROUTING) |
41 (1 << NF_INET_LOCAL_OUT));
44 static int nft_redir_init(const struct nft_ctx *ctx,
45 const struct nft_expr *expr,
46 const struct nlattr * const tb[])
48 struct nft_redir *priv = nft_expr_priv(expr);
52 plen = sizeof_field(struct nf_nat_range, min_proto.all);
53 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
54 err = nft_parse_register_load(tb[NFTA_REDIR_REG_PROTO_MIN],
55 &priv->sreg_proto_min, plen);
59 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
60 err = nft_parse_register_load(tb[NFTA_REDIR_REG_PROTO_MAX],
61 &priv->sreg_proto_max,
66 priv->sreg_proto_max = priv->sreg_proto_min;
69 priv->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
72 if (tb[NFTA_REDIR_FLAGS])
73 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
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_eval(const struct nft_expr *expr,
103 struct nft_regs *regs,
104 const struct nft_pktinfo *pkt)
106 const struct nft_redir *priv = nft_expr_priv(expr);
107 struct nf_nat_range2 range;
109 memset(&range, 0, sizeof(range));
110 range.flags = priv->flags;
111 if (priv->sreg_proto_min) {
112 range.min_proto.all = (__force __be16)
113 nft_reg_load16(®s->data[priv->sreg_proto_min]);
114 range.max_proto.all = (__force __be16)
115 nft_reg_load16(®s->data[priv->sreg_proto_max]);
118 switch (nft_pf(pkt)) {
120 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &range,
123 #ifdef CONFIG_NF_TABLES_IPV6
125 regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range,
136 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
138 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
141 static struct nft_expr_type nft_redir_ipv4_type;
142 static const struct nft_expr_ops nft_redir_ipv4_ops = {
143 .type = &nft_redir_ipv4_type,
144 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
145 .eval = nft_redir_eval,
146 .init = nft_redir_init,
147 .destroy = nft_redir_ipv4_destroy,
148 .dump = nft_redir_dump,
149 .validate = nft_redir_validate,
150 .reduce = NFT_REDUCE_READONLY,
153 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
154 .family = NFPROTO_IPV4,
156 .ops = &nft_redir_ipv4_ops,
157 .policy = nft_redir_policy,
158 .maxattr = NFTA_REDIR_MAX,
159 .owner = THIS_MODULE,
162 #ifdef CONFIG_NF_TABLES_IPV6
164 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
166 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
169 static struct nft_expr_type nft_redir_ipv6_type;
170 static const struct nft_expr_ops nft_redir_ipv6_ops = {
171 .type = &nft_redir_ipv6_type,
172 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
173 .eval = nft_redir_eval,
174 .init = nft_redir_init,
175 .destroy = nft_redir_ipv6_destroy,
176 .dump = nft_redir_dump,
177 .validate = nft_redir_validate,
178 .reduce = NFT_REDUCE_READONLY,
181 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
182 .family = NFPROTO_IPV6,
184 .ops = &nft_redir_ipv6_ops,
185 .policy = nft_redir_policy,
186 .maxattr = NFTA_REDIR_MAX,
187 .owner = THIS_MODULE,
191 #ifdef CONFIG_NF_TABLES_INET
193 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
195 nf_ct_netns_put(ctx->net, NFPROTO_INET);
198 static struct nft_expr_type nft_redir_inet_type;
199 static const struct nft_expr_ops nft_redir_inet_ops = {
200 .type = &nft_redir_inet_type,
201 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
202 .eval = nft_redir_eval,
203 .init = nft_redir_init,
204 .destroy = nft_redir_inet_destroy,
205 .dump = nft_redir_dump,
206 .validate = nft_redir_validate,
207 .reduce = NFT_REDUCE_READONLY,
210 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
211 .family = NFPROTO_INET,
213 .ops = &nft_redir_inet_ops,
214 .policy = nft_redir_policy,
215 .maxattr = NFTA_REDIR_MAX,
216 .owner = THIS_MODULE,
219 static int __init nft_redir_module_init_inet(void)
221 return nft_register_expr(&nft_redir_inet_type);
224 static inline int nft_redir_module_init_inet(void) { return 0; }
227 static int __init nft_redir_module_init(void)
229 int ret = nft_register_expr(&nft_redir_ipv4_type);
234 #ifdef CONFIG_NF_TABLES_IPV6
235 ret = nft_register_expr(&nft_redir_ipv6_type);
237 nft_unregister_expr(&nft_redir_ipv4_type);
242 ret = nft_redir_module_init_inet();
244 nft_unregister_expr(&nft_redir_ipv4_type);
245 #ifdef CONFIG_NF_TABLES_IPV6
246 nft_unregister_expr(&nft_redir_ipv6_type);
254 static void __exit nft_redir_module_exit(void)
256 nft_unregister_expr(&nft_redir_ipv4_type);
257 #ifdef CONFIG_NF_TABLES_IPV6
258 nft_unregister_expr(&nft_redir_ipv6_type);
260 #ifdef CONFIG_NF_TABLES_INET
261 nft_unregister_expr(&nft_redir_inet_type);
265 module_init(nft_redir_module_init);
266 module_exit(nft_redir_module_exit);
268 MODULE_LICENSE("GPL");
270 MODULE_ALIAS_NFT_EXPR("redir");
271 MODULE_DESCRIPTION("Netfilter nftables redirect support");