powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / net / netfilter / nft_ct.c
blobe6baeaebe6537ea6337d001faa58631ebf6e41b2
1 /*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_acct.h>
21 #include <net/netfilter/nf_conntrack_tuple.h>
22 #include <net/netfilter/nf_conntrack_helper.h>
23 #include <net/netfilter/nf_conntrack_ecache.h>
24 #include <net/netfilter/nf_conntrack_labels.h>
26 struct nft_ct {
27 enum nft_ct_keys key:8;
28 enum ip_conntrack_dir dir:8;
29 union {
30 enum nft_registers dreg:8;
31 enum nft_registers sreg:8;
35 static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
36 enum nft_ct_keys k,
37 enum ip_conntrack_dir d)
39 if (d < IP_CT_DIR_MAX)
40 return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) :
41 atomic64_read(&c[d].packets);
43 return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) +
44 nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY);
47 static void nft_ct_get_eval(const struct nft_expr *expr,
48 struct nft_regs *regs,
49 const struct nft_pktinfo *pkt)
51 const struct nft_ct *priv = nft_expr_priv(expr);
52 u32 *dest = &regs->data[priv->dreg];
53 enum ip_conntrack_info ctinfo;
54 const struct nf_conn *ct;
55 const struct nf_conn_help *help;
56 const struct nf_conntrack_tuple *tuple;
57 const struct nf_conntrack_helper *helper;
58 unsigned int state;
60 ct = nf_ct_get(pkt->skb, &ctinfo);
62 switch (priv->key) {
63 case NFT_CT_STATE:
64 if (ct == NULL)
65 state = NF_CT_STATE_INVALID_BIT;
66 else if (nf_ct_is_untracked(ct))
67 state = NF_CT_STATE_UNTRACKED_BIT;
68 else
69 state = NF_CT_STATE_BIT(ctinfo);
70 *dest = state;
71 return;
72 default:
73 break;
76 if (ct == NULL)
77 goto err;
79 switch (priv->key) {
80 case NFT_CT_DIRECTION:
81 *dest = CTINFO2DIR(ctinfo);
82 return;
83 case NFT_CT_STATUS:
84 *dest = ct->status;
85 return;
86 #ifdef CONFIG_NF_CONNTRACK_MARK
87 case NFT_CT_MARK:
88 *dest = ct->mark;
89 return;
90 #endif
91 #ifdef CONFIG_NF_CONNTRACK_SECMARK
92 case NFT_CT_SECMARK:
93 *dest = ct->secmark;
94 return;
95 #endif
96 case NFT_CT_EXPIRATION:
97 *dest = jiffies_to_msecs(nf_ct_expires(ct));
98 return;
99 case NFT_CT_HELPER:
100 if (ct->master == NULL)
101 goto err;
102 help = nfct_help(ct->master);
103 if (help == NULL)
104 goto err;
105 helper = rcu_dereference(help->helper);
106 if (helper == NULL)
107 goto err;
108 strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
109 return;
110 #ifdef CONFIG_NF_CONNTRACK_LABELS
111 case NFT_CT_LABELS: {
112 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
114 if (labels)
115 memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE);
116 else
117 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
118 return;
120 #endif
121 case NFT_CT_BYTES: /* fallthrough */
122 case NFT_CT_PKTS: {
123 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
124 u64 count = 0;
126 if (acct)
127 count = nft_ct_get_eval_counter(acct->counter,
128 priv->key, priv->dir);
129 memcpy(dest, &count, sizeof(count));
130 return;
132 case NFT_CT_L3PROTOCOL:
133 *dest = nf_ct_l3num(ct);
134 return;
135 case NFT_CT_PROTOCOL:
136 *dest = nf_ct_protonum(ct);
137 return;
138 default:
139 break;
142 tuple = &ct->tuplehash[priv->dir].tuple;
143 switch (priv->key) {
144 case NFT_CT_SRC:
145 memcpy(dest, tuple->src.u3.all,
146 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
147 return;
148 case NFT_CT_DST:
149 memcpy(dest, tuple->dst.u3.all,
150 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
151 return;
152 case NFT_CT_PROTO_SRC:
153 *dest = (__force __u16)tuple->src.u.all;
154 return;
155 case NFT_CT_PROTO_DST:
156 *dest = (__force __u16)tuple->dst.u.all;
157 return;
158 default:
159 break;
161 return;
162 err:
163 regs->verdict.code = NFT_BREAK;
166 static void nft_ct_set_eval(const struct nft_expr *expr,
167 struct nft_regs *regs,
168 const struct nft_pktinfo *pkt)
170 const struct nft_ct *priv = nft_expr_priv(expr);
171 struct sk_buff *skb = pkt->skb;
172 #ifdef CONFIG_NF_CONNTRACK_MARK
173 u32 value = regs->data[priv->sreg];
174 #endif
175 enum ip_conntrack_info ctinfo;
176 struct nf_conn *ct;
178 ct = nf_ct_get(skb, &ctinfo);
179 if (ct == NULL)
180 return;
182 switch (priv->key) {
183 #ifdef CONFIG_NF_CONNTRACK_MARK
184 case NFT_CT_MARK:
185 if (ct->mark != value) {
186 ct->mark = value;
187 nf_conntrack_event_cache(IPCT_MARK, ct);
189 break;
190 #endif
191 #ifdef CONFIG_NF_CONNTRACK_LABELS
192 case NFT_CT_LABELS:
193 nf_connlabels_replace(ct,
194 &regs->data[priv->sreg],
195 &regs->data[priv->sreg],
196 NF_CT_LABELS_MAX_SIZE / sizeof(u32));
197 break;
198 #endif
199 default:
200 break;
204 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
205 [NFTA_CT_DREG] = { .type = NLA_U32 },
206 [NFTA_CT_KEY] = { .type = NLA_U32 },
207 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
208 [NFTA_CT_SREG] = { .type = NLA_U32 },
211 static int nft_ct_netns_get(struct net *net, uint8_t family)
213 int err;
215 if (family == NFPROTO_INET) {
216 err = nf_ct_netns_get(net, NFPROTO_IPV4);
217 if (err < 0)
218 goto err1;
219 err = nf_ct_netns_get(net, NFPROTO_IPV6);
220 if (err < 0)
221 goto err2;
222 } else {
223 err = nf_ct_netns_get(net, family);
224 if (err < 0)
225 goto err1;
227 return 0;
229 err2:
230 nf_ct_netns_put(net, NFPROTO_IPV4);
231 err1:
232 return err;
235 static void nft_ct_netns_put(struct net *net, uint8_t family)
237 if (family == NFPROTO_INET) {
238 nf_ct_netns_put(net, NFPROTO_IPV4);
239 nf_ct_netns_put(net, NFPROTO_IPV6);
240 } else
241 nf_ct_netns_put(net, family);
244 static int nft_ct_get_init(const struct nft_ctx *ctx,
245 const struct nft_expr *expr,
246 const struct nlattr * const tb[])
248 struct nft_ct *priv = nft_expr_priv(expr);
249 unsigned int len;
250 int err;
252 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
253 switch (priv->key) {
254 case NFT_CT_DIRECTION:
255 if (tb[NFTA_CT_DIRECTION] != NULL)
256 return -EINVAL;
257 len = sizeof(u8);
258 break;
259 case NFT_CT_STATE:
260 case NFT_CT_STATUS:
261 #ifdef CONFIG_NF_CONNTRACK_MARK
262 case NFT_CT_MARK:
263 #endif
264 #ifdef CONFIG_NF_CONNTRACK_SECMARK
265 case NFT_CT_SECMARK:
266 #endif
267 case NFT_CT_EXPIRATION:
268 if (tb[NFTA_CT_DIRECTION] != NULL)
269 return -EINVAL;
270 len = sizeof(u32);
271 break;
272 #ifdef CONFIG_NF_CONNTRACK_LABELS
273 case NFT_CT_LABELS:
274 if (tb[NFTA_CT_DIRECTION] != NULL)
275 return -EINVAL;
276 len = NF_CT_LABELS_MAX_SIZE;
277 break;
278 #endif
279 case NFT_CT_HELPER:
280 if (tb[NFTA_CT_DIRECTION] != NULL)
281 return -EINVAL;
282 len = NF_CT_HELPER_NAME_LEN;
283 break;
285 case NFT_CT_L3PROTOCOL:
286 case NFT_CT_PROTOCOL:
287 /* For compatibility, do not report error if NFTA_CT_DIRECTION
288 * attribute is specified.
290 len = sizeof(u8);
291 break;
292 case NFT_CT_SRC:
293 case NFT_CT_DST:
294 if (tb[NFTA_CT_DIRECTION] == NULL)
295 return -EINVAL;
297 switch (ctx->afi->family) {
298 case NFPROTO_IPV4:
299 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
300 src.u3.ip);
301 break;
302 case NFPROTO_IPV6:
303 case NFPROTO_INET:
304 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
305 src.u3.ip6);
306 break;
307 default:
308 return -EAFNOSUPPORT;
310 break;
311 case NFT_CT_PROTO_SRC:
312 case NFT_CT_PROTO_DST:
313 if (tb[NFTA_CT_DIRECTION] == NULL)
314 return -EINVAL;
315 len = FIELD_SIZEOF(struct nf_conntrack_tuple, src.u.all);
316 break;
317 case NFT_CT_BYTES:
318 case NFT_CT_PKTS:
319 /* no direction? return sum of original + reply */
320 if (tb[NFTA_CT_DIRECTION] == NULL)
321 priv->dir = IP_CT_DIR_MAX;
322 len = sizeof(u64);
323 break;
324 default:
325 return -EOPNOTSUPP;
328 if (tb[NFTA_CT_DIRECTION] != NULL) {
329 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
330 switch (priv->dir) {
331 case IP_CT_DIR_ORIGINAL:
332 case IP_CT_DIR_REPLY:
333 break;
334 default:
335 return -EINVAL;
339 priv->dreg = nft_parse_register(tb[NFTA_CT_DREG]);
340 err = nft_validate_register_store(ctx, priv->dreg, NULL,
341 NFT_DATA_VALUE, len);
342 if (err < 0)
343 return err;
345 err = nft_ct_netns_get(ctx->net, ctx->afi->family);
346 if (err < 0)
347 return err;
349 if (priv->key == NFT_CT_BYTES || priv->key == NFT_CT_PKTS)
350 nf_ct_set_acct(ctx->net, true);
352 return 0;
355 static int nft_ct_set_init(const struct nft_ctx *ctx,
356 const struct nft_expr *expr,
357 const struct nlattr * const tb[])
359 struct nft_ct *priv = nft_expr_priv(expr);
360 bool label_got = false;
361 unsigned int len;
362 int err;
364 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
365 switch (priv->key) {
366 #ifdef CONFIG_NF_CONNTRACK_MARK
367 case NFT_CT_MARK:
368 if (tb[NFTA_CT_DIRECTION])
369 return -EINVAL;
370 len = FIELD_SIZEOF(struct nf_conn, mark);
371 break;
372 #endif
373 #ifdef CONFIG_NF_CONNTRACK_LABELS
374 case NFT_CT_LABELS:
375 if (tb[NFTA_CT_DIRECTION])
376 return -EINVAL;
377 len = NF_CT_LABELS_MAX_SIZE;
378 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
379 if (err)
380 return err;
381 label_got = true;
382 break;
383 #endif
384 default:
385 return -EOPNOTSUPP;
388 priv->sreg = nft_parse_register(tb[NFTA_CT_SREG]);
389 err = nft_validate_register_load(priv->sreg, len);
390 if (err < 0)
391 goto err1;
393 err = nft_ct_netns_get(ctx->net, ctx->afi->family);
394 if (err < 0)
395 goto err1;
397 return 0;
399 err1:
400 if (label_got)
401 nf_connlabels_put(ctx->net);
402 return err;
405 static void nft_ct_get_destroy(const struct nft_ctx *ctx,
406 const struct nft_expr *expr)
408 nf_ct_netns_put(ctx->net, ctx->afi->family);
411 static void nft_ct_set_destroy(const struct nft_ctx *ctx,
412 const struct nft_expr *expr)
414 struct nft_ct *priv = nft_expr_priv(expr);
416 switch (priv->key) {
417 #ifdef CONFIG_NF_CONNTRACK_LABELS
418 case NFT_CT_LABELS:
419 nf_connlabels_put(ctx->net);
420 break;
421 #endif
422 default:
423 break;
426 nft_ct_netns_put(ctx->net, ctx->afi->family);
429 static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
431 const struct nft_ct *priv = nft_expr_priv(expr);
433 if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg))
434 goto nla_put_failure;
435 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
436 goto nla_put_failure;
438 switch (priv->key) {
439 case NFT_CT_SRC:
440 case NFT_CT_DST:
441 case NFT_CT_PROTO_SRC:
442 case NFT_CT_PROTO_DST:
443 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
444 goto nla_put_failure;
445 break;
446 case NFT_CT_BYTES:
447 case NFT_CT_PKTS:
448 if (priv->dir < IP_CT_DIR_MAX &&
449 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
450 goto nla_put_failure;
451 break;
452 default:
453 break;
456 return 0;
458 nla_put_failure:
459 return -1;
462 static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
464 const struct nft_ct *priv = nft_expr_priv(expr);
466 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
467 goto nla_put_failure;
468 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
469 goto nla_put_failure;
470 return 0;
472 nla_put_failure:
473 return -1;
476 static struct nft_expr_type nft_ct_type;
477 static const struct nft_expr_ops nft_ct_get_ops = {
478 .type = &nft_ct_type,
479 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
480 .eval = nft_ct_get_eval,
481 .init = nft_ct_get_init,
482 .destroy = nft_ct_get_destroy,
483 .dump = nft_ct_get_dump,
486 static const struct nft_expr_ops nft_ct_set_ops = {
487 .type = &nft_ct_type,
488 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
489 .eval = nft_ct_set_eval,
490 .init = nft_ct_set_init,
491 .destroy = nft_ct_set_destroy,
492 .dump = nft_ct_set_dump,
495 static const struct nft_expr_ops *
496 nft_ct_select_ops(const struct nft_ctx *ctx,
497 const struct nlattr * const tb[])
499 if (tb[NFTA_CT_KEY] == NULL)
500 return ERR_PTR(-EINVAL);
502 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
503 return ERR_PTR(-EINVAL);
505 if (tb[NFTA_CT_DREG])
506 return &nft_ct_get_ops;
508 if (tb[NFTA_CT_SREG])
509 return &nft_ct_set_ops;
511 return ERR_PTR(-EINVAL);
514 static struct nft_expr_type nft_ct_type __read_mostly = {
515 .name = "ct",
516 .select_ops = &nft_ct_select_ops,
517 .policy = nft_ct_policy,
518 .maxattr = NFTA_CT_MAX,
519 .owner = THIS_MODULE,
522 static void nft_notrack_eval(const struct nft_expr *expr,
523 struct nft_regs *regs,
524 const struct nft_pktinfo *pkt)
526 struct sk_buff *skb = pkt->skb;
527 enum ip_conntrack_info ctinfo;
528 struct nf_conn *ct;
530 ct = nf_ct_get(pkt->skb, &ctinfo);
531 /* Previously seen (loopback or untracked)? Ignore. */
532 if (ct)
533 return;
535 ct = nf_ct_untracked_get();
536 atomic_inc(&ct->ct_general.use);
537 skb->nfct = &ct->ct_general;
538 skb->nfctinfo = IP_CT_NEW;
541 static struct nft_expr_type nft_notrack_type;
542 static const struct nft_expr_ops nft_notrack_ops = {
543 .type = &nft_notrack_type,
544 .size = NFT_EXPR_SIZE(0),
545 .eval = nft_notrack_eval,
548 static struct nft_expr_type nft_notrack_type __read_mostly = {
549 .name = "notrack",
550 .ops = &nft_notrack_ops,
551 .owner = THIS_MODULE,
554 static int __init nft_ct_module_init(void)
556 int err;
558 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE);
560 err = nft_register_expr(&nft_ct_type);
561 if (err < 0)
562 return err;
564 err = nft_register_expr(&nft_notrack_type);
565 if (err < 0)
566 goto err1;
568 return 0;
569 err1:
570 nft_unregister_expr(&nft_ct_type);
571 return err;
574 static void __exit nft_ct_module_exit(void)
576 nft_unregister_expr(&nft_notrack_type);
577 nft_unregister_expr(&nft_ct_type);
580 module_init(nft_ct_module_init);
581 module_exit(nft_ct_module_exit);
583 MODULE_LICENSE("GPL");
584 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
585 MODULE_ALIAS_NFT_EXPR("ct");
586 MODULE_ALIAS_NFT_EXPR("notrack");