]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nft_ct.c
netfilter: nf_tables: fix potential oops when dumping sets
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nft_ct.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18#include <net/netfilter/nf_conntrack.h>
19#include <net/netfilter/nf_conntrack_tuple.h>
20#include <net/netfilter/nf_conntrack_helper.h>
c4ede3d3 21#include <net/netfilter/nf_conntrack_ecache.h>
96518518
PM
22
23struct nft_ct {
24 enum nft_ct_keys key:8;
25 enum ip_conntrack_dir dir:8;
c4ede3d3
KE
26 union{
27 enum nft_registers dreg:8;
28 enum nft_registers sreg:8;
29 };
96518518
PM
30 uint8_t family;
31};
32
c4ede3d3
KE
33static void nft_ct_get_eval(const struct nft_expr *expr,
34 struct nft_data data[NFT_REG_MAX + 1],
35 const struct nft_pktinfo *pkt)
96518518
PM
36{
37 const struct nft_ct *priv = nft_expr_priv(expr);
38 struct nft_data *dest = &data[priv->dreg];
39 enum ip_conntrack_info ctinfo;
40 const struct nf_conn *ct;
41 const struct nf_conn_help *help;
42 const struct nf_conntrack_tuple *tuple;
43 const struct nf_conntrack_helper *helper;
44 long diff;
45 unsigned int state;
46
47 ct = nf_ct_get(pkt->skb, &ctinfo);
48
49 switch (priv->key) {
50 case NFT_CT_STATE:
51 if (ct == NULL)
52 state = NF_CT_STATE_INVALID_BIT;
53 else if (nf_ct_is_untracked(ct))
54 state = NF_CT_STATE_UNTRACKED_BIT;
55 else
56 state = NF_CT_STATE_BIT(ctinfo);
57 dest->data[0] = state;
58 return;
59 }
60
61 if (ct == NULL)
62 goto err;
63
64 switch (priv->key) {
65 case NFT_CT_DIRECTION:
66 dest->data[0] = CTINFO2DIR(ctinfo);
67 return;
68 case NFT_CT_STATUS:
69 dest->data[0] = ct->status;
70 return;
71#ifdef CONFIG_NF_CONNTRACK_MARK
72 case NFT_CT_MARK:
73 dest->data[0] = ct->mark;
74 return;
75#endif
76#ifdef CONFIG_NF_CONNTRACK_SECMARK
77 case NFT_CT_SECMARK:
78 dest->data[0] = ct->secmark;
79 return;
80#endif
81 case NFT_CT_EXPIRATION:
82 diff = (long)jiffies - (long)ct->timeout.expires;
83 if (diff < 0)
84 diff = 0;
85 dest->data[0] = jiffies_to_msecs(diff);
86 return;
87 case NFT_CT_HELPER:
88 if (ct->master == NULL)
89 goto err;
90 help = nfct_help(ct->master);
91 if (help == NULL)
92 goto err;
93 helper = rcu_dereference(help->helper);
94 if (helper == NULL)
95 goto err;
96 if (strlen(helper->name) >= sizeof(dest->data))
97 goto err;
98 strncpy((char *)dest->data, helper->name, sizeof(dest->data));
99 return;
100 }
101
102 tuple = &ct->tuplehash[priv->dir].tuple;
103 switch (priv->key) {
104 case NFT_CT_L3PROTOCOL:
105 dest->data[0] = nf_ct_l3num(ct);
106 return;
107 case NFT_CT_SRC:
108 memcpy(dest->data, tuple->src.u3.all,
109 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
110 return;
111 case NFT_CT_DST:
112 memcpy(dest->data, tuple->dst.u3.all,
113 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
114 return;
115 case NFT_CT_PROTOCOL:
116 dest->data[0] = nf_ct_protonum(ct);
117 return;
118 case NFT_CT_PROTO_SRC:
119 dest->data[0] = (__force __u16)tuple->src.u.all;
120 return;
121 case NFT_CT_PROTO_DST:
122 dest->data[0] = (__force __u16)tuple->dst.u.all;
123 return;
124 }
125 return;
126err:
127 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
128}
129
c4ede3d3
KE
130static void nft_ct_set_eval(const struct nft_expr *expr,
131 struct nft_data data[NFT_REG_MAX + 1],
132 const struct nft_pktinfo *pkt)
133{
134 const struct nft_ct *priv = nft_expr_priv(expr);
135 struct sk_buff *skb = pkt->skb;
847c8e29 136#ifdef CONFIG_NF_CONNTRACK_MARK
c4ede3d3 137 u32 value = data[priv->sreg].data[0];
847c8e29 138#endif
c4ede3d3
KE
139 enum ip_conntrack_info ctinfo;
140 struct nf_conn *ct;
141
142 ct = nf_ct_get(skb, &ctinfo);
143 if (ct == NULL)
144 return;
145
146 switch (priv->key) {
147#ifdef CONFIG_NF_CONNTRACK_MARK
148 case NFT_CT_MARK:
149 if (ct->mark != value) {
150 ct->mark = value;
151 nf_conntrack_event_cache(IPCT_MARK, ct);
152 }
153 break;
154#endif
155 }
156}
157
96518518
PM
158static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
159 [NFTA_CT_DREG] = { .type = NLA_U32 },
160 [NFTA_CT_KEY] = { .type = NLA_U32 },
161 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
c4ede3d3 162 [NFTA_CT_SREG] = { .type = NLA_U32 },
96518518
PM
163};
164
9638f33e
PM
165static int nft_ct_l3proto_try_module_get(uint8_t family)
166{
167 int err;
168
169 if (family == NFPROTO_INET) {
170 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4);
171 if (err < 0)
172 goto err1;
173 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6);
174 if (err < 0)
175 goto err2;
176 } else {
177 err = nf_ct_l3proto_try_module_get(family);
178 if (err < 0)
179 goto err1;
180 }
181 return 0;
182
183err2:
184 nf_ct_l3proto_module_put(NFPROTO_IPV4);
185err1:
186 return err;
187}
188
189static void nft_ct_l3proto_module_put(uint8_t family)
190{
191 if (family == NFPROTO_INET) {
192 nf_ct_l3proto_module_put(NFPROTO_IPV4);
193 nf_ct_l3proto_module_put(NFPROTO_IPV6);
194 } else
195 nf_ct_l3proto_module_put(family);
196}
197
c4ede3d3
KE
198static int nft_ct_init_validate_get(const struct nft_expr *expr,
199 const struct nlattr * const tb[])
96518518
PM
200{
201 struct nft_ct *priv = nft_expr_priv(expr);
96518518 202
96518518
PM
203 if (tb[NFTA_CT_DIRECTION] != NULL) {
204 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
205 switch (priv->dir) {
206 case IP_CT_DIR_ORIGINAL:
207 case IP_CT_DIR_REPLY:
208 break;
209 default:
210 return -EINVAL;
211 }
212 }
213
214 switch (priv->key) {
215 case NFT_CT_STATE:
216 case NFT_CT_DIRECTION:
217 case NFT_CT_STATUS:
218#ifdef CONFIG_NF_CONNTRACK_MARK
219 case NFT_CT_MARK:
220#endif
221#ifdef CONFIG_NF_CONNTRACK_SECMARK
222 case NFT_CT_SECMARK:
223#endif
224 case NFT_CT_EXPIRATION:
225 case NFT_CT_HELPER:
226 if (tb[NFTA_CT_DIRECTION] != NULL)
227 return -EINVAL;
228 break;
229 case NFT_CT_PROTOCOL:
230 case NFT_CT_SRC:
231 case NFT_CT_DST:
232 case NFT_CT_PROTO_SRC:
233 case NFT_CT_PROTO_DST:
234 if (tb[NFTA_CT_DIRECTION] == NULL)
235 return -EINVAL;
236 break;
237 default:
238 return -EOPNOTSUPP;
239 }
240
c4ede3d3
KE
241 return 0;
242}
243
244static int nft_ct_init_validate_set(uint32_t key)
245{
246 switch (key) {
247 case NFT_CT_MARK:
248 break;
249 default:
250 return -EOPNOTSUPP;
251 }
252
253 return 0;
254}
255
256static int nft_ct_init(const struct nft_ctx *ctx,
257 const struct nft_expr *expr,
258 const struct nlattr * const tb[])
259{
260 struct nft_ct *priv = nft_expr_priv(expr);
261 int err;
262
263 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
264
265 if (tb[NFTA_CT_DREG]) {
266 err = nft_ct_init_validate_get(expr, tb);
267 if (err < 0)
268 return err;
269
270 priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
271 err = nft_validate_output_register(priv->dreg);
272 if (err < 0)
273 return err;
274
275 err = nft_validate_data_load(ctx, priv->dreg, NULL,
276 NFT_DATA_VALUE);
277 if (err < 0)
278 return err;
279 } else {
280 err = nft_ct_init_validate_set(priv->key);
281 if (err < 0)
282 return err;
283
284 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
285 err = nft_validate_input_register(priv->sreg);
286 if (err < 0)
287 return err;
288 }
289
9638f33e 290 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
96518518
PM
291 if (err < 0)
292 return err;
96518518 293
c4ede3d3 294 priv->family = ctx->afi->family;
96518518 295
96518518 296 return 0;
96518518
PM
297}
298
299static void nft_ct_destroy(const struct nft_expr *expr)
300{
301 struct nft_ct *priv = nft_expr_priv(expr);
302
9638f33e 303 nft_ct_l3proto_module_put(priv->family);
96518518
PM
304}
305
c4ede3d3 306static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
96518518
PM
307{
308 const struct nft_ct *priv = nft_expr_priv(expr);
309
310 if (nla_put_be32(skb, NFTA_CT_DREG, htonl(priv->dreg)))
311 goto nla_put_failure;
312 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
313 goto nla_put_failure;
2a53bfb3
AB
314
315 switch (priv->key) {
316 case NFT_CT_PROTOCOL:
317 case NFT_CT_SRC:
318 case NFT_CT_DST:
319 case NFT_CT_PROTO_SRC:
320 case NFT_CT_PROTO_DST:
321 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
322 goto nla_put_failure;
323 default:
324 break;
325 }
326
96518518
PM
327 return 0;
328
329nla_put_failure:
330 return -1;
331}
332
c4ede3d3
KE
333static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
334{
335 const struct nft_ct *priv = nft_expr_priv(expr);
336
337 if (nla_put_be32(skb, NFTA_CT_SREG, htonl(priv->sreg)))
338 goto nla_put_failure;
339 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
340 goto nla_put_failure;
341 return 0;
342
343nla_put_failure:
344 return -1;
345}
346
ef1f7df9 347static struct nft_expr_type nft_ct_type;
c4ede3d3 348static const struct nft_expr_ops nft_ct_get_ops = {
ef1f7df9 349 .type = &nft_ct_type,
96518518 350 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
c4ede3d3 351 .eval = nft_ct_get_eval,
96518518
PM
352 .init = nft_ct_init,
353 .destroy = nft_ct_destroy,
c4ede3d3 354 .dump = nft_ct_get_dump,
ef1f7df9
PM
355};
356
c4ede3d3
KE
357static const struct nft_expr_ops nft_ct_set_ops = {
358 .type = &nft_ct_type,
359 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
360 .eval = nft_ct_set_eval,
361 .init = nft_ct_init,
362 .destroy = nft_ct_destroy,
363 .dump = nft_ct_set_dump,
364};
365
366static const struct nft_expr_ops *
367nft_ct_select_ops(const struct nft_ctx *ctx,
368 const struct nlattr * const tb[])
369{
370 if (tb[NFTA_CT_KEY] == NULL)
371 return ERR_PTR(-EINVAL);
372
373 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
374 return ERR_PTR(-EINVAL);
375
376 if (tb[NFTA_CT_DREG])
377 return &nft_ct_get_ops;
378
379 if (tb[NFTA_CT_SREG])
380 return &nft_ct_set_ops;
381
382 return ERR_PTR(-EINVAL);
383}
384
ef1f7df9
PM
385static struct nft_expr_type nft_ct_type __read_mostly = {
386 .name = "ct",
c4ede3d3 387 .select_ops = &nft_ct_select_ops,
96518518
PM
388 .policy = nft_ct_policy,
389 .maxattr = NFTA_CT_MAX,
ef1f7df9 390 .owner = THIS_MODULE,
96518518
PM
391};
392
393static int __init nft_ct_module_init(void)
394{
ef1f7df9 395 return nft_register_expr(&nft_ct_type);
96518518
PM
396}
397
398static void __exit nft_ct_module_exit(void)
399{
ef1f7df9 400 nft_unregister_expr(&nft_ct_type);
96518518
PM
401}
402
403module_init(nft_ct_module_init);
404module_exit(nft_ct_module_exit);
405
406MODULE_LICENSE("GPL");
407MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
408MODULE_ALIAS_NFT_EXPR("ct");