1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 Arturo Borrero Gonzalez <[email protected]>
4  */
5 
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>
15 
16 struct nft_redir {
17 	u8			sreg_proto_min;
18 	u8			sreg_proto_max;
19 	u16			flags;
20 };
21 
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]		=
26 		NLA_POLICY_MASK(NLA_BE32, NF_NAT_RANGE_MASK),
27 };
28 
nft_redir_validate(const struct nft_ctx * ctx,const struct nft_expr * expr)29 static int nft_redir_validate(const struct nft_ctx *ctx,
30 			      const struct nft_expr *expr)
31 {
32 	int err;
33 
34 	err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
35 	if (err < 0)
36 		return err;
37 
38 	return nft_chain_validate_hooks(ctx->chain,
39 					(1 << NF_INET_PRE_ROUTING) |
40 					(1 << NF_INET_LOCAL_OUT));
41 }
42 
nft_redir_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])43 static int nft_redir_init(const struct nft_ctx *ctx,
44 			  const struct nft_expr *expr,
45 			  const struct nlattr * const tb[])
46 {
47 	struct nft_redir *priv = nft_expr_priv(expr);
48 	unsigned int plen;
49 	int err;
50 
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(ctx, tb[NFTA_REDIR_REG_PROTO_MIN],
54 					      &priv->sreg_proto_min, plen);
55 		if (err < 0)
56 			return err;
57 
58 		if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
59 			err = nft_parse_register_load(ctx, tb[NFTA_REDIR_REG_PROTO_MAX],
60 						      &priv->sreg_proto_max,
61 						      plen);
62 			if (err < 0)
63 				return err;
64 		} else {
65 			priv->sreg_proto_max = priv->sreg_proto_min;
66 		}
67 
68 		priv->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
69 	}
70 
71 	if (tb[NFTA_REDIR_FLAGS])
72 		priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
73 
74 	return nf_ct_netns_get(ctx->net, ctx->family);
75 }
76 
nft_redir_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)77 static int nft_redir_dump(struct sk_buff *skb,
78 			  const struct nft_expr *expr, bool reset)
79 {
80 	const struct nft_redir *priv = nft_expr_priv(expr);
81 
82 	if (priv->sreg_proto_min) {
83 		if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
84 				      priv->sreg_proto_min))
85 			goto nla_put_failure;
86 		if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
87 				      priv->sreg_proto_max))
88 			goto nla_put_failure;
89 	}
90 
91 	if (priv->flags != 0 &&
92 	    nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
93 			goto nla_put_failure;
94 
95 	return 0;
96 
97 nla_put_failure:
98 	return -1;
99 }
100 
nft_redir_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)101 static void nft_redir_eval(const struct nft_expr *expr,
102 			   struct nft_regs *regs,
103 			   const struct nft_pktinfo *pkt)
104 {
105 	const struct nft_redir *priv = nft_expr_priv(expr);
106 	struct nf_nat_range2 range;
107 
108 	memset(&range, 0, sizeof(range));
109 	range.flags = priv->flags;
110 	if (priv->sreg_proto_min) {
111 		range.min_proto.all = (__force __be16)
112 			nft_reg_load16(&regs->data[priv->sreg_proto_min]);
113 		range.max_proto.all = (__force __be16)
114 			nft_reg_load16(&regs->data[priv->sreg_proto_max]);
115 	}
116 
117 	switch (nft_pf(pkt)) {
118 	case NFPROTO_IPV4:
119 		regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &range,
120 							  nft_hook(pkt));
121 		break;
122 #ifdef CONFIG_NF_TABLES_IPV6
123 	case NFPROTO_IPV6:
124 		regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range,
125 							  nft_hook(pkt));
126 		break;
127 #endif
128 	default:
129 		WARN_ON_ONCE(1);
130 		break;
131 	}
132 }
133 
134 static void
nft_redir_ipv4_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)135 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
136 {
137 	nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
138 }
139 
140 static struct nft_expr_type nft_redir_ipv4_type;
141 static const struct nft_expr_ops nft_redir_ipv4_ops = {
142 	.type		= &nft_redir_ipv4_type,
143 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_redir)),
144 	.eval		= nft_redir_eval,
145 	.init		= nft_redir_init,
146 	.destroy	= nft_redir_ipv4_destroy,
147 	.dump		= nft_redir_dump,
148 	.validate	= nft_redir_validate,
149 	.reduce		= NFT_REDUCE_READONLY,
150 };
151 
152 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
153 	.family		= NFPROTO_IPV4,
154 	.name		= "redir",
155 	.ops		= &nft_redir_ipv4_ops,
156 	.policy		= nft_redir_policy,
157 	.maxattr	= NFTA_REDIR_MAX,
158 	.owner		= THIS_MODULE,
159 };
160 
161 #ifdef CONFIG_NF_TABLES_IPV6
162 static void
nft_redir_ipv6_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)163 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
164 {
165 	nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
166 }
167 
168 static struct nft_expr_type nft_redir_ipv6_type;
169 static const struct nft_expr_ops nft_redir_ipv6_ops = {
170 	.type		= &nft_redir_ipv6_type,
171 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_redir)),
172 	.eval		= nft_redir_eval,
173 	.init		= nft_redir_init,
174 	.destroy	= nft_redir_ipv6_destroy,
175 	.dump		= nft_redir_dump,
176 	.validate	= nft_redir_validate,
177 	.reduce		= NFT_REDUCE_READONLY,
178 };
179 
180 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
181 	.family		= NFPROTO_IPV6,
182 	.name		= "redir",
183 	.ops		= &nft_redir_ipv6_ops,
184 	.policy		= nft_redir_policy,
185 	.maxattr	= NFTA_REDIR_MAX,
186 	.owner		= THIS_MODULE,
187 };
188 #endif
189 
190 #ifdef CONFIG_NF_TABLES_INET
191 static void
nft_redir_inet_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)192 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
193 {
194 	nf_ct_netns_put(ctx->net, NFPROTO_INET);
195 }
196 
197 static struct nft_expr_type nft_redir_inet_type;
198 static const struct nft_expr_ops nft_redir_inet_ops = {
199 	.type		= &nft_redir_inet_type,
200 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_redir)),
201 	.eval		= nft_redir_eval,
202 	.init		= nft_redir_init,
203 	.destroy	= nft_redir_inet_destroy,
204 	.dump		= nft_redir_dump,
205 	.validate	= nft_redir_validate,
206 	.reduce		= NFT_REDUCE_READONLY,
207 };
208 
209 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
210 	.family		= NFPROTO_INET,
211 	.name		= "redir",
212 	.ops		= &nft_redir_inet_ops,
213 	.policy		= nft_redir_policy,
214 	.maxattr	= NFTA_REDIR_MAX,
215 	.owner		= THIS_MODULE,
216 };
217 
nft_redir_module_init_inet(void)218 static int __init nft_redir_module_init_inet(void)
219 {
220 	return nft_register_expr(&nft_redir_inet_type);
221 }
222 #else
nft_redir_module_init_inet(void)223 static inline int nft_redir_module_init_inet(void) { return 0; }
224 #endif
225 
nft_redir_module_init(void)226 static int __init nft_redir_module_init(void)
227 {
228 	int ret = nft_register_expr(&nft_redir_ipv4_type);
229 
230 	if (ret)
231 		return ret;
232 
233 #ifdef CONFIG_NF_TABLES_IPV6
234 	ret = nft_register_expr(&nft_redir_ipv6_type);
235 	if (ret) {
236 		nft_unregister_expr(&nft_redir_ipv4_type);
237 		return ret;
238 	}
239 #endif
240 
241 	ret = nft_redir_module_init_inet();
242 	if (ret < 0) {
243 		nft_unregister_expr(&nft_redir_ipv4_type);
244 #ifdef CONFIG_NF_TABLES_IPV6
245 		nft_unregister_expr(&nft_redir_ipv6_type);
246 #endif
247 		return ret;
248 	}
249 
250 	return ret;
251 }
252 
nft_redir_module_exit(void)253 static void __exit nft_redir_module_exit(void)
254 {
255 	nft_unregister_expr(&nft_redir_ipv4_type);
256 #ifdef CONFIG_NF_TABLES_IPV6
257 	nft_unregister_expr(&nft_redir_ipv6_type);
258 #endif
259 #ifdef CONFIG_NF_TABLES_INET
260 	nft_unregister_expr(&nft_redir_inet_type);
261 #endif
262 }
263 
264 module_init(nft_redir_module_init);
265 module_exit(nft_redir_module_exit);
266 
267 MODULE_LICENSE("GPL");
268 MODULE_AUTHOR("Arturo Borrero Gonzalez <[email protected]>");
269 MODULE_ALIAS_NFT_EXPR("redir");
270 MODULE_DESCRIPTION("Netfilter nftables redirect support");
271