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>
27 enum nft_ct_keys key
:8;
28 enum ip_conntrack_dir dir
:8;
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
,
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
= ®s
->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
;
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 *dest
= jiffies_to_msecs(nf_ct_expires(ct
));
100 if (ct
->master
== NULL
)
102 help
= nfct_help(ct
->master
);
105 helper
= rcu_dereference(help
->helper
);
108 strncpy((char *)dest
, helper
->name
, NF_CT_HELPER_NAME_LEN
);
110 #ifdef CONFIG_NF_CONNTRACK_LABELS
111 case NFT_CT_LABELS
: {
112 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
115 memcpy(dest
, labels
->bits
, NF_CT_LABELS_MAX_SIZE
);
117 memset(dest
, 0, NF_CT_LABELS_MAX_SIZE
);
121 case NFT_CT_BYTES
: /* fallthrough */
123 const struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
127 count
= nft_ct_get_eval_counter(acct
->counter
,
128 priv
->key
, priv
->dir
);
129 memcpy(dest
, &count
, sizeof(count
));
132 case NFT_CT_L3PROTOCOL
:
133 *dest
= nf_ct_l3num(ct
);
135 case NFT_CT_PROTOCOL
:
136 *dest
= nf_ct_protonum(ct
);
142 tuple
= &ct
->tuplehash
[priv
->dir
].tuple
;
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_PROTO_SRC
:
153 *dest
= (__force __u16
)tuple
->src
.u
.all
;
155 case NFT_CT_PROTO_DST
:
156 *dest
= (__force __u16
)tuple
->dst
.u
.all
;
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
];
175 enum ip_conntrack_info ctinfo
;
178 ct
= nf_ct_get(skb
, &ctinfo
);
183 #ifdef CONFIG_NF_CONNTRACK_MARK
185 if (ct
->mark
!= value
) {
187 nf_conntrack_event_cache(IPCT_MARK
, ct
);
191 #ifdef CONFIG_NF_CONNTRACK_LABELS
193 nf_connlabels_replace(ct
,
194 ®s
->data
[priv
->sreg
],
195 ®s
->data
[priv
->sreg
],
196 NF_CT_LABELS_MAX_SIZE
/ sizeof(u32
));
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
)
215 if (family
== NFPROTO_INET
) {
216 err
= nf_ct_netns_get(net
, NFPROTO_IPV4
);
219 err
= nf_ct_netns_get(net
, NFPROTO_IPV6
);
223 err
= nf_ct_netns_get(net
, family
);
230 nf_ct_netns_put(net
, NFPROTO_IPV4
);
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
);
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
);
252 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
254 case NFT_CT_DIRECTION
:
255 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
261 #ifdef CONFIG_NF_CONNTRACK_MARK
264 #ifdef CONFIG_NF_CONNTRACK_SECMARK
267 case NFT_CT_EXPIRATION
:
268 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
272 #ifdef CONFIG_NF_CONNTRACK_LABELS
274 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
276 len
= NF_CT_LABELS_MAX_SIZE
;
280 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
282 len
= NF_CT_HELPER_NAME_LEN
;
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.
294 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
297 switch (ctx
->afi
->family
) {
299 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
,
304 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
,
308 return -EAFNOSUPPORT
;
311 case NFT_CT_PROTO_SRC
:
312 case NFT_CT_PROTO_DST
:
313 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
315 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
, src
.u
.all
);
319 /* no direction? return sum of original + reply */
320 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
321 priv
->dir
= IP_CT_DIR_MAX
;
328 if (tb
[NFTA_CT_DIRECTION
] != NULL
) {
329 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
331 case IP_CT_DIR_ORIGINAL
:
332 case IP_CT_DIR_REPLY
:
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
);
345 err
= nft_ct_netns_get(ctx
->net
, ctx
->afi
->family
);
349 if (priv
->key
== NFT_CT_BYTES
|| priv
->key
== NFT_CT_PKTS
)
350 nf_ct_set_acct(ctx
->net
, true);
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;
364 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
366 #ifdef CONFIG_NF_CONNTRACK_MARK
368 if (tb
[NFTA_CT_DIRECTION
])
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_netns_get(ctx
->net
, 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 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
);
417 #ifdef CONFIG_NF_CONNTRACK_LABELS
419 nf_connlabels_put(ctx
->net
);
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
;
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
;
448 if (priv
->dir
< IP_CT_DIR_MAX
&&
449 nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
450 goto nla_put_failure
;
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
;
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
= {
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
;
530 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
531 /* Previously seen (loopback or untracked)? Ignore. */
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
= {
550 .ops
= &nft_notrack_ops
,
551 .owner
= THIS_MODULE
,
554 static int __init
nft_ct_module_init(void)
558 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
> NFT_REG_SIZE
);
560 err
= nft_register_expr(&nft_ct_type
);
564 err
= nft_register_expr(&nft_notrack_type
);
570 nft_unregister_expr(&nft_ct_type
);
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");