• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
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]		= { .type = NLA_U32 },
26 };
27 
nft_redir_validate(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nft_data ** data)28 static int nft_redir_validate(const struct nft_ctx *ctx,
29 			      const struct nft_expr *expr,
30 			      const struct nft_data **data)
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(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(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 		if (priv->flags & ~NF_NAT_RANGE_MASK)
74 			return -EINVAL;
75 	}
76 
77 	return nf_ct_netns_get(ctx->net, ctx->family);
78 }
79 
nft_redir_dump(struct sk_buff * skb,const struct nft_expr * expr)80 static int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
81 {
82 	const struct nft_redir *priv = nft_expr_priv(expr);
83 
84 	if (priv->sreg_proto_min) {
85 		if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
86 				      priv->sreg_proto_min))
87 			goto nla_put_failure;
88 		if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
89 				      priv->sreg_proto_max))
90 			goto nla_put_failure;
91 	}
92 
93 	if (priv->flags != 0 &&
94 	    nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
95 			goto nla_put_failure;
96 
97 	return 0;
98 
99 nla_put_failure:
100 	return -1;
101 }
102 
nft_redir_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)103 static void nft_redir_eval(const struct nft_expr *expr,
104 			   struct nft_regs *regs,
105 			   const struct nft_pktinfo *pkt)
106 {
107 	const struct nft_redir *priv = nft_expr_priv(expr);
108 	struct nf_nat_range2 range;
109 
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(&regs->data[priv->sreg_proto_min]);
115 		range.max_proto.all = (__force __be16)
116 			nft_reg_load16(&regs->data[priv->sreg_proto_max]);
117 	}
118 
119 	switch (nft_pf(pkt)) {
120 	case NFPROTO_IPV4:
121 		regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &range,
122 							  nft_hook(pkt));
123 		break;
124 #ifdef CONFIG_NF_TABLES_IPV6
125 	case NFPROTO_IPV6:
126 		regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range,
127 							  nft_hook(pkt));
128 		break;
129 #endif
130 	default:
131 		WARN_ON_ONCE(1);
132 		break;
133 	}
134 }
135 
136 static void
nft_redir_ipv4_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)137 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
138 {
139 	nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
140 }
141 
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 };
152 
153 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
154 	.family		= NFPROTO_IPV4,
155 	.name		= "redir",
156 	.ops		= &nft_redir_ipv4_ops,
157 	.policy		= nft_redir_policy,
158 	.maxattr	= NFTA_REDIR_MAX,
159 	.owner		= THIS_MODULE,
160 };
161 
162 #ifdef CONFIG_NF_TABLES_IPV6
163 static void
nft_redir_ipv6_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)164 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
165 {
166 	nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
167 }
168 
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 };
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 };
207 
208 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
209 	.family		= NFPROTO_INET,
210 	.name		= "redir",
211 	.ops		= &nft_redir_inet_ops,
212 	.policy		= nft_redir_policy,
213 	.maxattr	= NFTA_REDIR_MAX,
214 	.owner		= THIS_MODULE,
215 };
216 
nft_redir_module_init_inet(void)217 static int __init nft_redir_module_init_inet(void)
218 {
219 	return nft_register_expr(&nft_redir_inet_type);
220 }
221 #else
nft_redir_module_init_inet(void)222 static inline int nft_redir_module_init_inet(void) { return 0; }
223 #endif
224 
nft_redir_module_init(void)225 static int __init nft_redir_module_init(void)
226 {
227 	int ret = nft_register_expr(&nft_redir_ipv4_type);
228 
229 	if (ret)
230 		return ret;
231 
232 #ifdef CONFIG_NF_TABLES_IPV6
233 	ret = nft_register_expr(&nft_redir_ipv6_type);
234 	if (ret) {
235 		nft_unregister_expr(&nft_redir_ipv4_type);
236 		return ret;
237 	}
238 #endif
239 
240 	ret = nft_redir_module_init_inet();
241 	if (ret < 0) {
242 		nft_unregister_expr(&nft_redir_ipv4_type);
243 #ifdef CONFIG_NF_TABLES_IPV6
244 		nft_unregister_expr(&nft_redir_ipv6_type);
245 #endif
246 		return ret;
247 	}
248 
249 	return ret;
250 }
251 
nft_redir_module_exit(void)252 static void __exit nft_redir_module_exit(void)
253 {
254 	nft_unregister_expr(&nft_redir_ipv4_type);
255 #ifdef CONFIG_NF_TABLES_IPV6
256 	nft_unregister_expr(&nft_redir_ipv6_type);
257 #endif
258 #ifdef CONFIG_NF_TABLES_INET
259 	nft_unregister_expr(&nft_redir_inet_type);
260 #endif
261 }
262 
263 module_init(nft_redir_module_init);
264 module_exit(nft_redir_module_exit);
265 
266 MODULE_LICENSE("GPL");
267 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
268 MODULE_ALIAS_NFT_EXPR("redir");
269 MODULE_DESCRIPTION("Netfilter nftables redirect support");
270