2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
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.
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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_acct.h>
20 #include <net/netfilter/nf_conntrack_tuple.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <net/netfilter/nf_conntrack_ecache.h>
23 #include <net/netfilter/nf_conntrack_labels.h>
26 enum nft_ct_keys key:8;
27 enum ip_conntrack_dir dir:8;
29 enum nft_registers dreg:8;
30 enum nft_registers sreg:8;
34 static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
36 enum ip_conntrack_dir d)
38 if (d < IP_CT_DIR_MAX)
39 return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) :
40 atomic64_read(&c[d].packets);
42 return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) +
43 nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY);
46 static void nft_ct_get_eval(const struct nft_expr *expr,
47 struct nft_regs *regs,
48 const struct nft_pktinfo *pkt)
50 const struct nft_ct *priv = nft_expr_priv(expr);
51 u32 *dest = ®s->data[priv->dreg];
52 enum ip_conntrack_info ctinfo;
53 const struct nf_conn *ct;
54 const struct nf_conn_help *help;
55 const struct nf_conntrack_tuple *tuple;
56 const struct nf_conntrack_helper *helper;
60 ct = nf_ct_get(pkt->skb, &ctinfo);
65 state = NF_CT_STATE_INVALID_BIT;
66 else if (nf_ct_is_untracked(ct))
67 state = NF_CT_STATE_UNTRACKED_BIT;
69 state = NF_CT_STATE_BIT(ctinfo);
80 case NFT_CT_DIRECTION:
81 *dest = CTINFO2DIR(ctinfo);
86 #ifdef CONFIG_NF_CONNTRACK_MARK
91 #ifdef CONFIG_NF_CONNTRACK_SECMARK
96 case NFT_CT_EXPIRATION:
97 diff = (long)jiffies - (long)ct->timeout.expires;
100 *dest = jiffies_to_msecs(diff);
103 if (ct->master == NULL)
105 help = nfct_help(ct->master);
108 helper = rcu_dereference(help->helper);
111 strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
113 #ifdef CONFIG_NF_CONNTRACK_LABELS
114 case NFT_CT_LABELS: {
115 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
118 memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE);
120 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
124 case NFT_CT_BYTES: /* fallthrough */
126 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
130 count = nft_ct_get_eval_counter(acct->counter,
131 priv->key, priv->dir);
132 memcpy(dest, &count, sizeof(count));
139 tuple = &ct->tuplehash[priv->dir].tuple;
141 case NFT_CT_L3PROTOCOL:
142 *dest = nf_ct_l3num(ct);
145 memcpy(dest, tuple->src.u3.all,
146 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
149 memcpy(dest, tuple->dst.u3.all,
150 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
152 case NFT_CT_PROTOCOL:
153 *dest = nf_ct_protonum(ct);
155 case NFT_CT_PROTO_SRC:
156 *dest = (__force __u16)tuple->src.u.all;
158 case NFT_CT_PROTO_DST:
159 *dest = (__force __u16)tuple->dst.u.all;
166 regs->verdict.code = NFT_BREAK;
169 static void nft_ct_set_eval(const struct nft_expr *expr,
170 struct nft_regs *regs,
171 const struct nft_pktinfo *pkt)
173 const struct nft_ct *priv = nft_expr_priv(expr);
174 struct sk_buff *skb = pkt->skb;
175 #ifdef CONFIG_NF_CONNTRACK_MARK
176 u32 value = regs->data[priv->sreg];
178 enum ip_conntrack_info ctinfo;
181 ct = nf_ct_get(skb, &ctinfo);
186 #ifdef CONFIG_NF_CONNTRACK_MARK
188 if (ct->mark != value) {
190 nf_conntrack_event_cache(IPCT_MARK, ct);
194 #ifdef CONFIG_NF_CONNTRACK_LABELS
196 nf_connlabels_replace(ct,
197 ®s->data[priv->sreg],
198 ®s->data[priv->sreg],
199 NF_CT_LABELS_MAX_SIZE / sizeof(u32));
207 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
208 [NFTA_CT_DREG] = { .type = NLA_U32 },
209 [NFTA_CT_KEY] = { .type = NLA_U32 },
210 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
211 [NFTA_CT_SREG] = { .type = NLA_U32 },
214 static int nft_ct_l3proto_try_module_get(uint8_t family)
218 if (family == NFPROTO_INET) {
219 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4);
222 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6);
226 err = nf_ct_l3proto_try_module_get(family);
233 nf_ct_l3proto_module_put(NFPROTO_IPV4);
238 static void nft_ct_l3proto_module_put(uint8_t family)
240 if (family == NFPROTO_INET) {
241 nf_ct_l3proto_module_put(NFPROTO_IPV4);
242 nf_ct_l3proto_module_put(NFPROTO_IPV6);
244 nf_ct_l3proto_module_put(family);
247 static int nft_ct_get_init(const struct nft_ctx *ctx,
248 const struct nft_expr *expr,
249 const struct nlattr * const tb[])
251 struct nft_ct *priv = nft_expr_priv(expr);
255 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
257 case NFT_CT_DIRECTION:
258 if (tb[NFTA_CT_DIRECTION] != NULL)
264 #ifdef CONFIG_NF_CONNTRACK_MARK
267 #ifdef CONFIG_NF_CONNTRACK_SECMARK
270 case NFT_CT_EXPIRATION:
271 if (tb[NFTA_CT_DIRECTION] != NULL)
275 #ifdef CONFIG_NF_CONNTRACK_LABELS
277 if (tb[NFTA_CT_DIRECTION] != NULL)
279 len = NF_CT_LABELS_MAX_SIZE;
283 if (tb[NFTA_CT_DIRECTION] != NULL)
285 len = NF_CT_HELPER_NAME_LEN;
288 case NFT_CT_L3PROTOCOL:
289 case NFT_CT_PROTOCOL:
290 if (tb[NFTA_CT_DIRECTION] == NULL)
296 if (tb[NFTA_CT_DIRECTION] == NULL)
299 switch (ctx->afi->family) {
301 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
306 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
310 return -EAFNOSUPPORT;
313 case NFT_CT_PROTO_SRC:
314 case NFT_CT_PROTO_DST:
315 if (tb[NFTA_CT_DIRECTION] == NULL)
317 len = FIELD_SIZEOF(struct nf_conntrack_tuple, src.u.all);
321 /* no direction? return sum of original + reply */
322 if (tb[NFTA_CT_DIRECTION] == NULL)
323 priv->dir = IP_CT_DIR_MAX;
330 if (tb[NFTA_CT_DIRECTION] != NULL) {
331 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
333 case IP_CT_DIR_ORIGINAL:
334 case IP_CT_DIR_REPLY:
341 priv->dreg = nft_parse_register(tb[NFTA_CT_DREG]);
342 err = nft_validate_register_store(ctx, priv->dreg, NULL,
343 NFT_DATA_VALUE, len);
347 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
351 if (priv->key == NFT_CT_BYTES || priv->key == NFT_CT_PKTS)
352 nf_ct_set_acct(ctx->net, true);
357 static int nft_ct_set_init(const struct nft_ctx *ctx,
358 const struct nft_expr *expr,
359 const struct nlattr * const tb[])
361 struct nft_ct *priv = nft_expr_priv(expr);
362 bool label_got = false;
366 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
368 #ifdef CONFIG_NF_CONNTRACK_MARK
370 len = FIELD_SIZEOF(struct nf_conn, mark);
373 #ifdef CONFIG_NF_CONNTRACK_LABELS
375 if (tb[NFTA_CT_DIRECTION])
377 len = NF_CT_LABELS_MAX_SIZE;
378 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
388 priv->sreg = nft_parse_register(tb[NFTA_CT_SREG]);
389 err = nft_validate_register_load(priv->sreg, len);
393 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
401 nf_connlabels_put(ctx->net);
405 static void nft_ct_get_destroy(const struct nft_ctx *ctx,
406 const struct nft_expr *expr)
408 nft_ct_l3proto_module_put(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);
417 #ifdef CONFIG_NF_CONNTRACK_LABELS
419 nf_connlabels_put(ctx->net);
426 nft_ct_l3proto_module_put(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;
439 case NFT_CT_L3PROTOCOL:
440 case NFT_CT_PROTOCOL:
443 case NFT_CT_PROTO_SRC:
444 case NFT_CT_PROTO_DST:
445 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
446 goto nla_put_failure;
450 if (priv->dir < IP_CT_DIR_MAX &&
451 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
452 goto nla_put_failure;
464 static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
466 const struct nft_ct *priv = nft_expr_priv(expr);
468 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
469 goto nla_put_failure;
470 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
471 goto nla_put_failure;
478 static struct nft_expr_type nft_ct_type;
479 static const struct nft_expr_ops nft_ct_get_ops = {
480 .type = &nft_ct_type,
481 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
482 .eval = nft_ct_get_eval,
483 .init = nft_ct_get_init,
484 .destroy = nft_ct_get_destroy,
485 .dump = nft_ct_get_dump,
488 static const struct nft_expr_ops nft_ct_set_ops = {
489 .type = &nft_ct_type,
490 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
491 .eval = nft_ct_set_eval,
492 .init = nft_ct_set_init,
493 .destroy = nft_ct_set_destroy,
494 .dump = nft_ct_set_dump,
497 static const struct nft_expr_ops *
498 nft_ct_select_ops(const struct nft_ctx *ctx,
499 const struct nlattr * const tb[])
501 if (tb[NFTA_CT_KEY] == NULL)
502 return ERR_PTR(-EINVAL);
504 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
505 return ERR_PTR(-EINVAL);
507 if (tb[NFTA_CT_DREG])
508 return &nft_ct_get_ops;
510 if (tb[NFTA_CT_SREG])
511 return &nft_ct_set_ops;
513 return ERR_PTR(-EINVAL);
516 static struct nft_expr_type nft_ct_type __read_mostly = {
518 .select_ops = &nft_ct_select_ops,
519 .policy = nft_ct_policy,
520 .maxattr = NFTA_CT_MAX,
521 .owner = THIS_MODULE,
524 static int __init nft_ct_module_init(void)
526 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE);
528 return nft_register_expr(&nft_ct_type);
531 static void __exit nft_ct_module_exit(void)
533 nft_unregister_expr(&nft_ct_type);
536 module_init(nft_ct_module_init);
537 module_exit(nft_ct_module_exit);
539 MODULE_LICENSE("GPL");
540 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
541 MODULE_ALIAS_NFT_EXPR("ct");