GNU Linux-libre 6.1.90-gnu
[releases.git] / net / netfilter / nft_redir.c
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
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
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
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
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
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         .reduce         = NFT_REDUCE_READONLY,
152 };
153
154 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
155         .family         = NFPROTO_IPV4,
156         .name           = "redir",
157         .ops            = &nft_redir_ipv4_ops,
158         .policy         = nft_redir_policy,
159         .maxattr        = NFTA_REDIR_MAX,
160         .owner          = THIS_MODULE,
161 };
162
163 #ifdef CONFIG_NF_TABLES_IPV6
164 static void
165 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
166 {
167         nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
168 }
169
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,
180 };
181
182 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
183         .family         = NFPROTO_IPV6,
184         .name           = "redir",
185         .ops            = &nft_redir_ipv6_ops,
186         .policy         = nft_redir_policy,
187         .maxattr        = NFTA_REDIR_MAX,
188         .owner          = THIS_MODULE,
189 };
190 #endif
191
192 #ifdef CONFIG_NF_TABLES_INET
193 static void
194 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
195 {
196         nf_ct_netns_put(ctx->net, NFPROTO_INET);
197 }
198
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,
209 };
210
211 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
212         .family         = NFPROTO_INET,
213         .name           = "redir",
214         .ops            = &nft_redir_inet_ops,
215         .policy         = nft_redir_policy,
216         .maxattr        = NFTA_REDIR_MAX,
217         .owner          = THIS_MODULE,
218 };
219
220 static int __init nft_redir_module_init_inet(void)
221 {
222         return nft_register_expr(&nft_redir_inet_type);
223 }
224 #else
225 static inline int nft_redir_module_init_inet(void) { return 0; }
226 #endif
227
228 static int __init nft_redir_module_init(void)
229 {
230         int ret = nft_register_expr(&nft_redir_ipv4_type);
231
232         if (ret)
233                 return ret;
234
235 #ifdef CONFIG_NF_TABLES_IPV6
236         ret = nft_register_expr(&nft_redir_ipv6_type);
237         if (ret) {
238                 nft_unregister_expr(&nft_redir_ipv4_type);
239                 return ret;
240         }
241 #endif
242
243         ret = nft_redir_module_init_inet();
244         if (ret < 0) {
245                 nft_unregister_expr(&nft_redir_ipv4_type);
246 #ifdef CONFIG_NF_TABLES_IPV6
247                 nft_unregister_expr(&nft_redir_ipv6_type);
248 #endif
249                 return ret;
250         }
251
252         return ret;
253 }
254
255 static void __exit nft_redir_module_exit(void)
256 {
257         nft_unregister_expr(&nft_redir_ipv4_type);
258 #ifdef CONFIG_NF_TABLES_IPV6
259         nft_unregister_expr(&nft_redir_ipv6_type);
260 #endif
261 #ifdef CONFIG_NF_TABLES_INET
262         nft_unregister_expr(&nft_redir_inet_type);
263 #endif
264 }
265
266 module_init(nft_redir_module_init);
267 module_exit(nft_redir_module_exit);
268
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");