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 },
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_proto.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;
68 priv->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
71 if (tb[NFTA_REDIR_FLAGS]) {
72 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
73 if (priv->flags & ~NF_NAT_RANGE_MASK)
77 return nf_ct_netns_get(ctx->net, ctx->family);
80 static int nft_redir_dump(struct sk_buff *skb,
81 const struct nft_expr *expr, bool reset)
83 const struct nft_redir *priv = nft_expr_priv(expr);
85 if (priv->sreg_proto_min) {
86 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
87 priv->sreg_proto_min))
89 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
90 priv->sreg_proto_max))
94 if (priv->flags != 0 &&
95 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
104 static void nft_redir_eval(const struct nft_expr *expr,
105 struct nft_regs *regs,
106 const struct nft_pktinfo *pkt)
108 const struct nft_redir *priv = nft_expr_priv(expr);
109 struct nf_nat_range2 range;
111 memset(&range, 0, sizeof(range));
112 range.flags = priv->flags;
113 if (priv->sreg_proto_min) {
114 range.min_proto.all = (__force __be16)
115 nft_reg_load16(®s->data[priv->sreg_proto_min]);
116 range.max_proto.all = (__force __be16)
117 nft_reg_load16(®s->data[priv->sreg_proto_max]);
120 switch (nft_pf(pkt)) {
122 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &range,
125 #ifdef CONFIG_NF_TABLES_IPV6
127 regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range,
138 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
140 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
143 static struct nft_expr_type nft_redir_ipv4_type;
144 static const struct nft_expr_ops nft_redir_ipv4_ops = {
145 .type = &nft_redir_ipv4_type,
146 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
147 .eval = nft_redir_eval,
148 .init = nft_redir_init,
149 .destroy = nft_redir_ipv4_destroy,
150 .dump = nft_redir_dump,
151 .validate = nft_redir_validate,
152 .reduce = NFT_REDUCE_READONLY,
155 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
156 .family = NFPROTO_IPV4,
158 .ops = &nft_redir_ipv4_ops,
159 .policy = nft_redir_policy,
160 .maxattr = NFTA_REDIR_MAX,
161 .owner = THIS_MODULE,
164 #ifdef CONFIG_NF_TABLES_IPV6
166 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
168 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
171 static struct nft_expr_type nft_redir_ipv6_type;
172 static const struct nft_expr_ops nft_redir_ipv6_ops = {
173 .type = &nft_redir_ipv6_type,
174 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
175 .eval = nft_redir_eval,
176 .init = nft_redir_init,
177 .destroy = nft_redir_ipv6_destroy,
178 .dump = nft_redir_dump,
179 .validate = nft_redir_validate,
180 .reduce = NFT_REDUCE_READONLY,
183 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
184 .family = NFPROTO_IPV6,
186 .ops = &nft_redir_ipv6_ops,
187 .policy = nft_redir_policy,
188 .maxattr = NFTA_REDIR_MAX,
189 .owner = THIS_MODULE,
193 #ifdef CONFIG_NF_TABLES_INET
195 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
197 nf_ct_netns_put(ctx->net, NFPROTO_INET);
200 static struct nft_expr_type nft_redir_inet_type;
201 static const struct nft_expr_ops nft_redir_inet_ops = {
202 .type = &nft_redir_inet_type,
203 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
204 .eval = nft_redir_eval,
205 .init = nft_redir_init,
206 .destroy = nft_redir_inet_destroy,
207 .dump = nft_redir_dump,
208 .validate = nft_redir_validate,
209 .reduce = NFT_REDUCE_READONLY,
212 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
213 .family = NFPROTO_INET,
215 .ops = &nft_redir_inet_ops,
216 .policy = nft_redir_policy,
217 .maxattr = NFTA_REDIR_MAX,
218 .owner = THIS_MODULE,
221 static int __init nft_redir_module_init_inet(void)
223 return nft_register_expr(&nft_redir_inet_type);
226 static inline int nft_redir_module_init_inet(void) { return 0; }
229 static int __init nft_redir_module_init(void)
231 int ret = nft_register_expr(&nft_redir_ipv4_type);
236 #ifdef CONFIG_NF_TABLES_IPV6
237 ret = nft_register_expr(&nft_redir_ipv6_type);
239 nft_unregister_expr(&nft_redir_ipv4_type);
244 ret = nft_redir_module_init_inet();
246 nft_unregister_expr(&nft_redir_ipv4_type);
247 #ifdef CONFIG_NF_TABLES_IPV6
248 nft_unregister_expr(&nft_redir_ipv6_type);
256 static void __exit nft_redir_module_exit(void)
258 nft_unregister_expr(&nft_redir_ipv4_type);
259 #ifdef CONFIG_NF_TABLES_IPV6
260 nft_unregister_expr(&nft_redir_ipv6_type);
262 #ifdef CONFIG_NF_TABLES_INET
263 nft_unregister_expr(&nft_redir_inet_type);
267 module_init(nft_redir_module_init);
268 module_exit(nft_redir_module_exit);
270 MODULE_LICENSE("GPL");
272 MODULE_ALIAS_NFT_EXPR("redir");
273 MODULE_DESCRIPTION("Netfilter nftables redirect support");