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_tuple.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <net/netfilter/nf_conntrack_ecache.h>
22 #include <net/netfilter/nf_conntrack_labels.h>
25 enum nft_ct_keys key:8;
26 enum ip_conntrack_dir dir:8;
28 enum nft_registers dreg:8;
29 enum nft_registers sreg:8;
33 static 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)
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;
47 ct = nf_ct_get(pkt->skb, &ctinfo);
52 state = NF_CT_STATE_INVALID_BIT;
53 else if (nf_ct_is_untracked(ct))
54 state = NF_CT_STATE_UNTRACKED_BIT;
56 state = NF_CT_STATE_BIT(ctinfo);
57 dest->data[0] = state;
65 case NFT_CT_DIRECTION:
66 dest->data[0] = CTINFO2DIR(ctinfo);
69 dest->data[0] = ct->status;
71 #ifdef CONFIG_NF_CONNTRACK_MARK
73 dest->data[0] = ct->mark;
76 #ifdef CONFIG_NF_CONNTRACK_SECMARK
78 dest->data[0] = ct->secmark;
81 case NFT_CT_EXPIRATION:
82 diff = (long)jiffies - (long)ct->timeout.expires;
85 dest->data[0] = jiffies_to_msecs(diff);
88 if (ct->master == NULL)
90 help = nfct_help(ct->master);
93 helper = rcu_dereference(help->helper);
96 if (strlen(helper->name) >= sizeof(dest->data))
98 strncpy((char *)dest->data, helper->name, sizeof(dest->data));
100 #ifdef CONFIG_NF_CONNTRACK_LABELS
101 case NFT_CT_LABELS: {
102 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
106 memset(dest->data, 0, sizeof(dest->data));
110 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > sizeof(dest->data));
111 size = labels->words * sizeof(long);
113 memcpy(dest->data, labels->bits, size);
114 if (size < sizeof(dest->data))
115 memset(((char *) dest->data) + size, 0,
116 sizeof(dest->data) - size);
122 tuple = &ct->tuplehash[priv->dir].tuple;
124 case NFT_CT_L3PROTOCOL:
125 dest->data[0] = nf_ct_l3num(ct);
128 memcpy(dest->data, tuple->src.u3.all,
129 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
132 memcpy(dest->data, tuple->dst.u3.all,
133 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
135 case NFT_CT_PROTOCOL:
136 dest->data[0] = nf_ct_protonum(ct);
138 case NFT_CT_PROTO_SRC:
139 dest->data[0] = (__force __u16)tuple->src.u.all;
141 case NFT_CT_PROTO_DST:
142 dest->data[0] = (__force __u16)tuple->dst.u.all;
147 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
150 static void nft_ct_set_eval(const struct nft_expr *expr,
151 struct nft_data data[NFT_REG_MAX + 1],
152 const struct nft_pktinfo *pkt)
154 const struct nft_ct *priv = nft_expr_priv(expr);
155 struct sk_buff *skb = pkt->skb;
156 #ifdef CONFIG_NF_CONNTRACK_MARK
157 u32 value = data[priv->sreg].data[0];
159 enum ip_conntrack_info ctinfo;
162 ct = nf_ct_get(skb, &ctinfo);
167 #ifdef CONFIG_NF_CONNTRACK_MARK
169 if (ct->mark != value) {
171 nf_conntrack_event_cache(IPCT_MARK, ct);
178 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
179 [NFTA_CT_DREG] = { .type = NLA_U32 },
180 [NFTA_CT_KEY] = { .type = NLA_U32 },
181 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
182 [NFTA_CT_SREG] = { .type = NLA_U32 },
185 static int nft_ct_l3proto_try_module_get(uint8_t family)
189 if (family == NFPROTO_INET) {
190 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4);
193 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6);
197 err = nf_ct_l3proto_try_module_get(family);
204 nf_ct_l3proto_module_put(NFPROTO_IPV4);
209 static void nft_ct_l3proto_module_put(uint8_t family)
211 if (family == NFPROTO_INET) {
212 nf_ct_l3proto_module_put(NFPROTO_IPV4);
213 nf_ct_l3proto_module_put(NFPROTO_IPV6);
215 nf_ct_l3proto_module_put(family);
218 static int nft_ct_get_init(const struct nft_ctx *ctx,
219 const struct nft_expr *expr,
220 const struct nlattr * const tb[])
222 struct nft_ct *priv = nft_expr_priv(expr);
225 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
228 case NFT_CT_DIRECTION:
230 #ifdef CONFIG_NF_CONNTRACK_MARK
233 #ifdef CONFIG_NF_CONNTRACK_SECMARK
236 #ifdef CONFIG_NF_CONNTRACK_LABELS
239 case NFT_CT_EXPIRATION:
241 if (tb[NFTA_CT_DIRECTION] != NULL)
244 case NFT_CT_L3PROTOCOL:
245 case NFT_CT_PROTOCOL:
248 case NFT_CT_PROTO_SRC:
249 case NFT_CT_PROTO_DST:
250 if (tb[NFTA_CT_DIRECTION] == NULL)
257 if (tb[NFTA_CT_DIRECTION] != NULL) {
258 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
260 case IP_CT_DIR_ORIGINAL:
261 case IP_CT_DIR_REPLY:
268 priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
269 err = nft_validate_output_register(priv->dreg);
273 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
277 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
284 static int nft_ct_set_init(const struct nft_ctx *ctx,
285 const struct nft_expr *expr,
286 const struct nlattr * const tb[])
288 struct nft_ct *priv = nft_expr_priv(expr);
291 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
293 #ifdef CONFIG_NF_CONNTRACK_MARK
301 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
302 err = nft_validate_input_register(priv->sreg);
306 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
313 static void nft_ct_destroy(const struct nft_ctx *ctx,
314 const struct nft_expr *expr)
316 nft_ct_l3proto_module_put(ctx->afi->family);
319 static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
321 const struct nft_ct *priv = nft_expr_priv(expr);
323 if (nla_put_be32(skb, NFTA_CT_DREG, htonl(priv->dreg)))
324 goto nla_put_failure;
325 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
326 goto nla_put_failure;
329 case NFT_CT_PROTOCOL:
332 case NFT_CT_PROTO_SRC:
333 case NFT_CT_PROTO_DST:
334 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
335 goto nla_put_failure;
346 static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
348 const struct nft_ct *priv = nft_expr_priv(expr);
350 if (nla_put_be32(skb, NFTA_CT_SREG, htonl(priv->sreg)))
351 goto nla_put_failure;
352 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
353 goto nla_put_failure;
360 static struct nft_expr_type nft_ct_type;
361 static const struct nft_expr_ops nft_ct_get_ops = {
362 .type = &nft_ct_type,
363 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
364 .eval = nft_ct_get_eval,
365 .init = nft_ct_get_init,
366 .destroy = nft_ct_destroy,
367 .dump = nft_ct_get_dump,
370 static const struct nft_expr_ops nft_ct_set_ops = {
371 .type = &nft_ct_type,
372 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
373 .eval = nft_ct_set_eval,
374 .init = nft_ct_set_init,
375 .destroy = nft_ct_destroy,
376 .dump = nft_ct_set_dump,
379 static const struct nft_expr_ops *
380 nft_ct_select_ops(const struct nft_ctx *ctx,
381 const struct nlattr * const tb[])
383 if (tb[NFTA_CT_KEY] == NULL)
384 return ERR_PTR(-EINVAL);
386 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
387 return ERR_PTR(-EINVAL);
389 if (tb[NFTA_CT_DREG])
390 return &nft_ct_get_ops;
392 if (tb[NFTA_CT_SREG])
393 return &nft_ct_set_ops;
395 return ERR_PTR(-EINVAL);
398 static struct nft_expr_type nft_ct_type __read_mostly = {
400 .select_ops = &nft_ct_select_ops,
401 .policy = nft_ct_policy,
402 .maxattr = NFTA_CT_MAX,
403 .owner = THIS_MODULE,
406 static int __init nft_ct_module_init(void)
408 return nft_register_expr(&nft_ct_type);
411 static void __exit nft_ct_module_exit(void)
413 nft_unregister_expr(&nft_ct_type);
416 module_init(nft_ct_module_init);
417 module_exit(nft_ct_module_exit);
419 MODULE_LICENSE("GPL");
420 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
421 MODULE_ALIAS_NFT_EXPR("ct");