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_addr.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
69 if (tb[NFTA_REDIR_FLAGS]) {
70 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
71 if (priv->flags & ~NF_NAT_RANGE_MASK)
72 return -EINVAL;
73 }
74
75 return nf_ct_netns_get(ctx->net, ctx->family);
76 }
77
nft_redir_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)78 static int nft_redir_dump(struct sk_buff *skb,
79 const struct nft_expr *expr, bool reset)
80 {
81 const struct nft_redir *priv = nft_expr_priv(expr);
82
83 if (priv->sreg_proto_min) {
84 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
85 priv->sreg_proto_min))
86 goto nla_put_failure;
87 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
88 priv->sreg_proto_max))
89 goto nla_put_failure;
90 }
91
92 if (priv->flags != 0 &&
93 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
94 goto nla_put_failure;
95
96 return 0;
97
98 nla_put_failure:
99 return -1;
100 }
101
nft_redir_ipv4_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)102 static void nft_redir_ipv4_eval(const struct nft_expr *expr,
103 struct nft_regs *regs,
104 const struct nft_pktinfo *pkt)
105 {
106 struct nft_redir *priv = nft_expr_priv(expr);
107 struct nf_nat_ipv4_multi_range_compat mr;
108
109 memset(&mr, 0, sizeof(mr));
110 if (priv->sreg_proto_min) {
111 mr.range[0].min.all = (__force __be16)nft_reg_load16(
112 ®s->data[priv->sreg_proto_min]);
113 mr.range[0].max.all = (__force __be16)nft_reg_load16(
114 ®s->data[priv->sreg_proto_max]);
115 mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
116 }
117
118 mr.range[0].flags |= priv->flags;
119
120 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &mr, nft_hook(pkt));
121 }
122
123 static void
nft_redir_ipv4_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)124 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
125 {
126 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
127 }
128
129 static struct nft_expr_type nft_redir_ipv4_type;
130 static const struct nft_expr_ops nft_redir_ipv4_ops = {
131 .type = &nft_redir_ipv4_type,
132 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
133 .eval = nft_redir_ipv4_eval,
134 .init = nft_redir_init,
135 .destroy = nft_redir_ipv4_destroy,
136 .dump = nft_redir_dump,
137 .validate = nft_redir_validate,
138 .reduce = NFT_REDUCE_READONLY,
139 };
140
141 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
142 .family = NFPROTO_IPV4,
143 .name = "redir",
144 .ops = &nft_redir_ipv4_ops,
145 .policy = nft_redir_policy,
146 .maxattr = NFTA_REDIR_MAX,
147 .owner = THIS_MODULE,
148 };
149
150 #ifdef CONFIG_NF_TABLES_IPV6
nft_redir_ipv6_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)151 static void nft_redir_ipv6_eval(const struct nft_expr *expr,
152 struct nft_regs *regs,
153 const struct nft_pktinfo *pkt)
154 {
155 struct nft_redir *priv = nft_expr_priv(expr);
156 struct nf_nat_range2 range;
157
158 memset(&range, 0, sizeof(range));
159 if (priv->sreg_proto_min) {
160 range.min_proto.all = (__force __be16)nft_reg_load16(
161 ®s->data[priv->sreg_proto_min]);
162 range.max_proto.all = (__force __be16)nft_reg_load16(
163 ®s->data[priv->sreg_proto_max]);
164 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
165 }
166
167 range.flags |= priv->flags;
168
169 regs->verdict.code =
170 nf_nat_redirect_ipv6(pkt->skb, &range, nft_hook(pkt));
171 }
172
173 static void
nft_redir_ipv6_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)174 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
175 {
176 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
177 }
178
179 static struct nft_expr_type nft_redir_ipv6_type;
180 static const struct nft_expr_ops nft_redir_ipv6_ops = {
181 .type = &nft_redir_ipv6_type,
182 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
183 .eval = nft_redir_ipv6_eval,
184 .init = nft_redir_init,
185 .destroy = nft_redir_ipv6_destroy,
186 .dump = nft_redir_dump,
187 .validate = nft_redir_validate,
188 .reduce = NFT_REDUCE_READONLY,
189 };
190
191 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
192 .family = NFPROTO_IPV6,
193 .name = "redir",
194 .ops = &nft_redir_ipv6_ops,
195 .policy = nft_redir_policy,
196 .maxattr = NFTA_REDIR_MAX,
197 .owner = THIS_MODULE,
198 };
199 #endif
200
201 #ifdef CONFIG_NF_TABLES_INET
nft_redir_inet_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)202 static void nft_redir_inet_eval(const struct nft_expr *expr,
203 struct nft_regs *regs,
204 const struct nft_pktinfo *pkt)
205 {
206 switch (nft_pf(pkt)) {
207 case NFPROTO_IPV4:
208 return nft_redir_ipv4_eval(expr, regs, pkt);
209 case NFPROTO_IPV6:
210 return nft_redir_ipv6_eval(expr, regs, pkt);
211 }
212
213 WARN_ON_ONCE(1);
214 }
215
216 static void
nft_redir_inet_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)217 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
218 {
219 nf_ct_netns_put(ctx->net, NFPROTO_INET);
220 }
221
222 static struct nft_expr_type nft_redir_inet_type;
223 static const struct nft_expr_ops nft_redir_inet_ops = {
224 .type = &nft_redir_inet_type,
225 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
226 .eval = nft_redir_inet_eval,
227 .init = nft_redir_init,
228 .destroy = nft_redir_inet_destroy,
229 .dump = nft_redir_dump,
230 .validate = nft_redir_validate,
231 .reduce = NFT_REDUCE_READONLY,
232 };
233
234 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
235 .family = NFPROTO_INET,
236 .name = "redir",
237 .ops = &nft_redir_inet_ops,
238 .policy = nft_redir_policy,
239 .maxattr = NFTA_MASQ_MAX,
240 .owner = THIS_MODULE,
241 };
242
nft_redir_module_init_inet(void)243 static int __init nft_redir_module_init_inet(void)
244 {
245 return nft_register_expr(&nft_redir_inet_type);
246 }
247 #else
nft_redir_module_init_inet(void)248 static inline int nft_redir_module_init_inet(void) { return 0; }
249 #endif
250
nft_redir_module_init(void)251 static int __init nft_redir_module_init(void)
252 {
253 int ret = nft_register_expr(&nft_redir_ipv4_type);
254
255 if (ret)
256 return ret;
257
258 #ifdef CONFIG_NF_TABLES_IPV6
259 ret = nft_register_expr(&nft_redir_ipv6_type);
260 if (ret) {
261 nft_unregister_expr(&nft_redir_ipv4_type);
262 return ret;
263 }
264 #endif
265
266 ret = nft_redir_module_init_inet();
267 if (ret < 0) {
268 nft_unregister_expr(&nft_redir_ipv4_type);
269 #ifdef CONFIG_NF_TABLES_IPV6
270 nft_unregister_expr(&nft_redir_ipv6_type);
271 #endif
272 return ret;
273 }
274
275 return ret;
276 }
277
nft_redir_module_exit(void)278 static void __exit nft_redir_module_exit(void)
279 {
280 nft_unregister_expr(&nft_redir_ipv4_type);
281 #ifdef CONFIG_NF_TABLES_IPV6
282 nft_unregister_expr(&nft_redir_ipv6_type);
283 #endif
284 #ifdef CONFIG_NF_TABLES_INET
285 nft_unregister_expr(&nft_redir_inet_type);
286 #endif
287 }
288
289 module_init(nft_redir_module_init);
290 module_exit(nft_redir_module_exit);
291
292 MODULE_LICENSE("GPL");
293 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
294 MODULE_ALIAS_NFT_EXPR("redir");
295 MODULE_DESCRIPTION("Netfilter nftables redirect support");
296