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_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, const struct nft_expr *expr)
82 const struct nft_redir *priv = nft_expr_priv(expr);
84 if (priv->sreg_proto_min) {
85 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
86 priv->sreg_proto_min))
88 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
89 priv->sreg_proto_max))
93 if (priv->flags != 0 &&
94 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
103 static void nft_redir_eval(const struct nft_expr *expr,
104 struct nft_regs *regs,
105 const struct nft_pktinfo *pkt)
107 const struct nft_redir *priv = nft_expr_priv(expr);
108 struct nf_nat_range2 range;
110 memset(&range, 0, sizeof(range));
111 range.flags = priv->flags;
112 if (priv->sreg_proto_min) {
113 range.min_proto.all = (__force __be16)
114 nft_reg_load16(®s->data[priv->sreg_proto_min]);
115 range.max_proto.all = (__force __be16)
116 nft_reg_load16(®s->data[priv->sreg_proto_max]);
119 switch (nft_pf(pkt)) {
121 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &range,
124 #ifdef CONFIG_NF_TABLES_IPV6
126 regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range,
137 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
139 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
142 static struct nft_expr_type nft_redir_ipv4_type;
143 static const struct nft_expr_ops nft_redir_ipv4_ops = {
144 .type = &nft_redir_ipv4_type,
145 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
146 .eval = nft_redir_eval,
147 .init = nft_redir_init,
148 .destroy = nft_redir_ipv4_destroy,
149 .dump = nft_redir_dump,
150 .validate = nft_redir_validate,
151 .reduce = NFT_REDUCE_READONLY,
154 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
155 .family = NFPROTO_IPV4,
157 .ops = &nft_redir_ipv4_ops,
158 .policy = nft_redir_policy,
159 .maxattr = NFTA_REDIR_MAX,
160 .owner = THIS_MODULE,
163 #ifdef CONFIG_NF_TABLES_IPV6
165 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
167 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
170 static struct nft_expr_type nft_redir_ipv6_type;
171 static const struct nft_expr_ops nft_redir_ipv6_ops = {
172 .type = &nft_redir_ipv6_type,
173 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
174 .eval = nft_redir_eval,
175 .init = nft_redir_init,
176 .destroy = nft_redir_ipv6_destroy,
177 .dump = nft_redir_dump,
178 .validate = nft_redir_validate,
179 .reduce = NFT_REDUCE_READONLY,
182 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
183 .family = NFPROTO_IPV6,
185 .ops = &nft_redir_ipv6_ops,
186 .policy = nft_redir_policy,
187 .maxattr = NFTA_REDIR_MAX,
188 .owner = THIS_MODULE,
192 #ifdef CONFIG_NF_TABLES_INET
194 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
196 nf_ct_netns_put(ctx->net, NFPROTO_INET);
199 static struct nft_expr_type nft_redir_inet_type;
200 static const struct nft_expr_ops nft_redir_inet_ops = {
201 .type = &nft_redir_inet_type,
202 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
203 .eval = nft_redir_eval,
204 .init = nft_redir_init,
205 .destroy = nft_redir_inet_destroy,
206 .dump = nft_redir_dump,
207 .validate = nft_redir_validate,
208 .reduce = NFT_REDUCE_READONLY,
211 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
212 .family = NFPROTO_INET,
214 .ops = &nft_redir_inet_ops,
215 .policy = nft_redir_policy,
216 .maxattr = NFTA_REDIR_MAX,
217 .owner = THIS_MODULE,
220 static int __init nft_redir_module_init_inet(void)
222 return nft_register_expr(&nft_redir_inet_type);
225 static inline int nft_redir_module_init_inet(void) { return 0; }
228 static int __init nft_redir_module_init(void)
230 int ret = nft_register_expr(&nft_redir_ipv4_type);
235 #ifdef CONFIG_NF_TABLES_IPV6
236 ret = nft_register_expr(&nft_redir_ipv6_type);
238 nft_unregister_expr(&nft_redir_ipv4_type);
243 ret = nft_redir_module_init_inet();
245 nft_unregister_expr(&nft_redir_ipv4_type);
246 #ifdef CONFIG_NF_TABLES_IPV6
247 nft_unregister_expr(&nft_redir_ipv6_type);
255 static void __exit nft_redir_module_exit(void)
257 nft_unregister_expr(&nft_redir_ipv4_type);
258 #ifdef CONFIG_NF_TABLES_IPV6
259 nft_unregister_expr(&nft_redir_ipv6_type);
261 #ifdef CONFIG_NF_TABLES_INET
262 nft_unregister_expr(&nft_redir_inet_type);
266 module_init(nft_redir_module_init);
267 module_exit(nft_redir_module_exit);
269 MODULE_LICENSE("GPL");
270 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
271 MODULE_ALIAS_NFT_EXPR("redir");
272 MODULE_DESCRIPTION("Netfilter nftables redirect support");