blob: 2056051c0af0d0fa9b3dd3f5e0a659b250ee2b81 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Arturo Borreroe9105f12014-10-17 12:39:09 +02002/*
Arturo Borrero Gonzalezcd727512016-10-18 14:02:29 +02003 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
Arturo Borreroe9105f12014-10-17 12:39:09 +02004 */
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>
Florian Westphalc78efc992019-02-28 12:02:50 +010013#include <net/netfilter/nf_nat_redirect.h>
Arturo Borreroe9105f12014-10-17 12:39:09 +020014#include <net/netfilter/nf_tables.h>
Arturo Borreroe9105f12014-10-17 12:39:09 +020015
Florian Westphalc78efc992019-02-28 12:02:50 +010016struct nft_redir {
17 enum nft_registers sreg_proto_min:8;
18 enum nft_registers sreg_proto_max:8;
19 u16 flags;
20};
21
22static const struct nla_policy nft_redir_policy[NFTA_REDIR_MAX + 1] = {
Arturo Borreroe9105f12014-10-17 12:39:09 +020023 [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};
Arturo Borreroe9105f12014-10-17 12:39:09 +020027
Florian Westphalc78efc992019-02-28 12:02:50 +010028static int nft_redir_validate(const struct nft_ctx *ctx,
29 const struct nft_expr *expr,
30 const struct nft_data **data)
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010031{
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}
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +010042
Florian Westphalc78efc992019-02-28 12:02:50 +010043static int nft_redir_init(const struct nft_ctx *ctx,
44 const struct nft_expr *expr,
45 const struct nlattr * const tb[])
Arturo Borreroe9105f12014-10-17 12:39:09 +020046{
47 struct nft_redir *priv = nft_expr_priv(expr);
Patrick McHardyd07db982015-04-11 02:27:30 +010048 unsigned int plen;
Arturo Borreroe9105f12014-10-17 12:39:09 +020049 int err;
50
Pankaj Bharadiyac5936422019-12-09 10:31:43 -080051 plen = sizeof_field(struct nf_nat_range, min_addr.all);
Arturo Borreroe9105f12014-10-17 12:39:09 +020052 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010053 priv->sreg_proto_min =
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010054 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010055
Patrick McHardyd07db982015-04-11 02:27:30 +010056 err = nft_validate_register_load(priv->sreg_proto_min, plen);
Arturo Borreroe9105f12014-10-17 12:39:09 +020057 if (err < 0)
58 return err;
59
60 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010061 priv->sreg_proto_max =
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010062 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
Pablo Neira Ayusobaf47502014-11-12 11:56:47 +010063
Patrick McHardyd07db982015-04-11 02:27:30 +010064 err = nft_validate_register_load(priv->sreg_proto_max,
65 plen);
Arturo Borreroe9105f12014-10-17 12:39:09 +020066 if (err < 0)
67 return err;
68 } else {
69 priv->sreg_proto_max = priv->sreg_proto_min;
70 }
71 }
72
73 if (tb[NFTA_REDIR_FLAGS]) {
74 priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
75 if (priv->flags & ~NF_NAT_RANGE_MASK)
76 return -EINVAL;
77 }
78
Pablo Neira Ayuso36596da2018-01-09 02:38:03 +010079 return nf_ct_netns_get(ctx->net, ctx->family);
Arturo Borreroe9105f12014-10-17 12:39:09 +020080}
Arturo Borreroe9105f12014-10-17 12:39:09 +020081
YueHaibingb3dfee32019-03-19 22:40:21 +080082static int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
Arturo Borreroe9105f12014-10-17 12:39:09 +020083{
84 const struct nft_redir *priv = nft_expr_priv(expr);
85
86 if (priv->sreg_proto_min) {
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010087 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
88 priv->sreg_proto_min))
Arturo Borreroe9105f12014-10-17 12:39:09 +020089 goto nla_put_failure;
Patrick McHardyb1c96ed2015-04-11 02:27:36 +010090 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
91 priv->sreg_proto_max))
Arturo Borreroe9105f12014-10-17 12:39:09 +020092 goto nla_put_failure;
93 }
94
95 if (priv->flags != 0 &&
96 nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
97 goto nla_put_failure;
98
99 return 0;
100
101nla_put_failure:
102 return -1;
103}
Florian Westphalc78efc992019-02-28 12:02:50 +0100104
105static void nft_redir_ipv4_eval(const struct nft_expr *expr,
106 struct nft_regs *regs,
107 const struct nft_pktinfo *pkt)
108{
109 struct nft_redir *priv = nft_expr_priv(expr);
110 struct nf_nat_ipv4_multi_range_compat mr;
111
112 memset(&mr, 0, sizeof(mr));
113 if (priv->sreg_proto_min) {
114 mr.range[0].min.all = (__force __be16)nft_reg_load16(
115 &regs->data[priv->sreg_proto_min]);
116 mr.range[0].max.all = (__force __be16)nft_reg_load16(
117 &regs->data[priv->sreg_proto_max]);
118 mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
119 }
120
121 mr.range[0].flags |= priv->flags;
122
123 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &mr, nft_hook(pkt));
124}
125
126static void
127nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
128{
129 nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
130}
131
132static struct nft_expr_type nft_redir_ipv4_type;
133static const struct nft_expr_ops nft_redir_ipv4_ops = {
134 .type = &nft_redir_ipv4_type,
135 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
136 .eval = nft_redir_ipv4_eval,
137 .init = nft_redir_init,
138 .destroy = nft_redir_ipv4_destroy,
139 .dump = nft_redir_dump,
140 .validate = nft_redir_validate,
141};
142
143static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
144 .family = NFPROTO_IPV4,
145 .name = "redir",
146 .ops = &nft_redir_ipv4_ops,
147 .policy = nft_redir_policy,
148 .maxattr = NFTA_REDIR_MAX,
149 .owner = THIS_MODULE,
150};
151
152#ifdef CONFIG_NF_TABLES_IPV6
153static void nft_redir_ipv6_eval(const struct nft_expr *expr,
154 struct nft_regs *regs,
155 const struct nft_pktinfo *pkt)
156{
157 struct nft_redir *priv = nft_expr_priv(expr);
158 struct nf_nat_range2 range;
159
160 memset(&range, 0, sizeof(range));
161 if (priv->sreg_proto_min) {
162 range.min_proto.all = (__force __be16)nft_reg_load16(
163 &regs->data[priv->sreg_proto_min]);
164 range.max_proto.all = (__force __be16)nft_reg_load16(
165 &regs->data[priv->sreg_proto_max]);
166 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
167 }
168
169 range.flags |= priv->flags;
170
171 regs->verdict.code =
172 nf_nat_redirect_ipv6(pkt->skb, &range, nft_hook(pkt));
173}
174
175static void
176nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
177{
178 nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
179}
180
181static struct nft_expr_type nft_redir_ipv6_type;
182static const struct nft_expr_ops nft_redir_ipv6_ops = {
183 .type = &nft_redir_ipv6_type,
184 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
185 .eval = nft_redir_ipv6_eval,
186 .init = nft_redir_init,
187 .destroy = nft_redir_ipv6_destroy,
188 .dump = nft_redir_dump,
189 .validate = nft_redir_validate,
190};
191
192static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
193 .family = NFPROTO_IPV6,
194 .name = "redir",
195 .ops = &nft_redir_ipv6_ops,
196 .policy = nft_redir_policy,
197 .maxattr = NFTA_REDIR_MAX,
198 .owner = THIS_MODULE,
199};
200#endif
201
Florian Westphal63ce3942019-03-27 09:22:28 +0100202#ifdef CONFIG_NF_TABLES_INET
203static void nft_redir_inet_eval(const struct nft_expr *expr,
204 struct nft_regs *regs,
205 const struct nft_pktinfo *pkt)
206{
207 switch (nft_pf(pkt)) {
208 case NFPROTO_IPV4:
209 return nft_redir_ipv4_eval(expr, regs, pkt);
210 case NFPROTO_IPV6:
211 return nft_redir_ipv6_eval(expr, regs, pkt);
212 }
213
214 WARN_ON_ONCE(1);
215}
216
217static void
218nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
219{
220 nf_ct_netns_put(ctx->net, NFPROTO_INET);
221}
222
223static struct nft_expr_type nft_redir_inet_type;
224static const struct nft_expr_ops nft_redir_inet_ops = {
225 .type = &nft_redir_inet_type,
226 .size = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
227 .eval = nft_redir_inet_eval,
228 .init = nft_redir_init,
229 .destroy = nft_redir_inet_destroy,
230 .dump = nft_redir_dump,
231 .validate = nft_redir_validate,
232};
233
234static 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
243static int __init nft_redir_module_init_inet(void)
244{
245 return nft_register_expr(&nft_redir_inet_type);
246}
247#else
248static inline int nft_redir_module_init_inet(void) { return 0; }
249#endif
250
Florian Westphalc78efc992019-02-28 12:02:50 +0100251static 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
Florian Westphal63ce3942019-03-27 09:22:28 +0100266 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
Florian Westphalc78efc992019-02-28 12:02:50 +0100275 return ret;
276}
277
278static 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
Florian Westphal63ce3942019-03-27 09:22:28 +0100284#ifdef CONFIG_NF_TABLES_INET
285 nft_unregister_expr(&nft_redir_inet_type);
286#endif
Florian Westphalc78efc992019-02-28 12:02:50 +0100287}
288
289module_init(nft_redir_module_init);
290module_exit(nft_redir_module_exit);
Arturo Borreroe9105f12014-10-17 12:39:09 +0200291
Arturo Borreroe9105f12014-10-17 12:39:09 +0200292MODULE_LICENSE("GPL");
Arturo Borrero Gonzalezcd727512016-10-18 14:02:29 +0200293MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
Christian Hessef41828e2019-07-11 01:31:12 +0200294MODULE_ALIAS_NFT_EXPR("redir");
Rob Gill4cacc392020-06-21 05:27:36 +0000295MODULE_DESCRIPTION("Netfilter nftables redirect support");