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
);
119 memset(dest
, 0, NF_CT_LABELS_MAX_SIZE
);
123 size
= labels
->words
* sizeof(long);
124 memcpy(dest
, labels
->bits
, size
);
125 if (size
< NF_CT_LABELS_MAX_SIZE
)
126 memset(((char *) dest
) + size
, 0,
127 NF_CT_LABELS_MAX_SIZE
- size
);
131 case NFT_CT_BYTES
: /* fallthrough */
133 const struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
137 count
= nft_ct_get_eval_counter(acct
->counter
,
138 priv
->key
, priv
->dir
);
139 memcpy(dest
, &count
, sizeof(count
));
146 tuple
= &ct
->tuplehash
[priv
->dir
].tuple
;
148 case NFT_CT_L3PROTOCOL
:
149 *dest
= nf_ct_l3num(ct
);
152 memcpy(dest
, tuple
->src
.u3
.all
,
153 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
156 memcpy(dest
, tuple
->dst
.u3
.all
,
157 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
159 case NFT_CT_PROTOCOL
:
160 *dest
= nf_ct_protonum(ct
);
162 case NFT_CT_PROTO_SRC
:
163 *dest
= (__force __u16
)tuple
->src
.u
.all
;
165 case NFT_CT_PROTO_DST
:
166 *dest
= (__force __u16
)tuple
->dst
.u
.all
;
173 regs
->verdict
.code
= NFT_BREAK
;
176 static void nft_ct_set_eval(const struct nft_expr
*expr
,
177 struct nft_regs
*regs
,
178 const struct nft_pktinfo
*pkt
)
180 const struct nft_ct
*priv
= nft_expr_priv(expr
);
181 struct sk_buff
*skb
= pkt
->skb
;
182 #ifdef CONFIG_NF_CONNTRACK_MARK
183 u32 value
= regs
->data
[priv
->sreg
];
185 enum ip_conntrack_info ctinfo
;
188 ct
= nf_ct_get(skb
, &ctinfo
);
193 #ifdef CONFIG_NF_CONNTRACK_MARK
195 if (ct
->mark
!= value
) {
197 nf_conntrack_event_cache(IPCT_MARK
, ct
);
206 static const struct nla_policy nft_ct_policy
[NFTA_CT_MAX
+ 1] = {
207 [NFTA_CT_DREG
] = { .type
= NLA_U32
},
208 [NFTA_CT_KEY
] = { .type
= NLA_U32
},
209 [NFTA_CT_DIRECTION
] = { .type
= NLA_U8
},
210 [NFTA_CT_SREG
] = { .type
= NLA_U32
},
213 static int nft_ct_l3proto_try_module_get(uint8_t family
)
217 if (family
== NFPROTO_INET
) {
218 err
= nf_ct_l3proto_try_module_get(NFPROTO_IPV4
);
221 err
= nf_ct_l3proto_try_module_get(NFPROTO_IPV6
);
225 err
= nf_ct_l3proto_try_module_get(family
);
232 nf_ct_l3proto_module_put(NFPROTO_IPV4
);
237 static void nft_ct_l3proto_module_put(uint8_t family
)
239 if (family
== NFPROTO_INET
) {
240 nf_ct_l3proto_module_put(NFPROTO_IPV4
);
241 nf_ct_l3proto_module_put(NFPROTO_IPV6
);
243 nf_ct_l3proto_module_put(family
);
246 static int nft_ct_get_init(const struct nft_ctx
*ctx
,
247 const struct nft_expr
*expr
,
248 const struct nlattr
* const tb
[])
250 struct nft_ct
*priv
= nft_expr_priv(expr
);
254 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
256 case NFT_CT_DIRECTION
:
257 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
263 #ifdef CONFIG_NF_CONNTRACK_MARK
266 #ifdef CONFIG_NF_CONNTRACK_SECMARK
269 case NFT_CT_EXPIRATION
:
270 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
274 #ifdef CONFIG_NF_CONNTRACK_LABELS
276 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
278 len
= NF_CT_LABELS_MAX_SIZE
;
282 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
284 len
= NF_CT_HELPER_NAME_LEN
;
287 case NFT_CT_L3PROTOCOL
:
288 case NFT_CT_PROTOCOL
:
289 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
295 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
298 switch (ctx
->afi
->family
) {
300 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
,
305 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
,
309 return -EAFNOSUPPORT
;
312 case NFT_CT_PROTO_SRC
:
313 case NFT_CT_PROTO_DST
:
314 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
316 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
, src
.u
.all
);
320 /* no direction? return sum of original + reply */
321 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
322 priv
->dir
= IP_CT_DIR_MAX
;
329 if (tb
[NFTA_CT_DIRECTION
] != NULL
) {
330 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
332 case IP_CT_DIR_ORIGINAL
:
333 case IP_CT_DIR_REPLY
:
340 priv
->dreg
= nft_parse_register(tb
[NFTA_CT_DREG
]);
341 err
= nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
342 NFT_DATA_VALUE
, len
);
346 err
= nft_ct_l3proto_try_module_get(ctx
->afi
->family
);
353 static int nft_ct_set_init(const struct nft_ctx
*ctx
,
354 const struct nft_expr
*expr
,
355 const struct nlattr
* const tb
[])
357 struct nft_ct
*priv
= nft_expr_priv(expr
);
361 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
363 #ifdef CONFIG_NF_CONNTRACK_MARK
365 len
= FIELD_SIZEOF(struct nf_conn
, mark
);
372 priv
->sreg
= nft_parse_register(tb
[NFTA_CT_SREG
]);
373 err
= nft_validate_register_load(priv
->sreg
, len
);
377 err
= nft_ct_l3proto_try_module_get(ctx
->afi
->family
);
384 static void nft_ct_destroy(const struct nft_ctx
*ctx
,
385 const struct nft_expr
*expr
)
387 nft_ct_l3proto_module_put(ctx
->afi
->family
);
390 static int nft_ct_get_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
392 const struct nft_ct
*priv
= nft_expr_priv(expr
);
394 if (nft_dump_register(skb
, NFTA_CT_DREG
, priv
->dreg
))
395 goto nla_put_failure
;
396 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
397 goto nla_put_failure
;
400 case NFT_CT_L3PROTOCOL
:
401 case NFT_CT_PROTOCOL
:
404 case NFT_CT_PROTO_SRC
:
405 case NFT_CT_PROTO_DST
:
406 if (nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
407 goto nla_put_failure
;
411 if (priv
->dir
< IP_CT_DIR_MAX
&&
412 nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
413 goto nla_put_failure
;
425 static int nft_ct_set_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
427 const struct nft_ct
*priv
= nft_expr_priv(expr
);
429 if (nft_dump_register(skb
, NFTA_CT_SREG
, priv
->sreg
))
430 goto nla_put_failure
;
431 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
432 goto nla_put_failure
;
439 static struct nft_expr_type nft_ct_type
;
440 static const struct nft_expr_ops nft_ct_get_ops
= {
441 .type
= &nft_ct_type
,
442 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
443 .eval
= nft_ct_get_eval
,
444 .init
= nft_ct_get_init
,
445 .destroy
= nft_ct_destroy
,
446 .dump
= nft_ct_get_dump
,
449 static const struct nft_expr_ops nft_ct_set_ops
= {
450 .type
= &nft_ct_type
,
451 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
452 .eval
= nft_ct_set_eval
,
453 .init
= nft_ct_set_init
,
454 .destroy
= nft_ct_destroy
,
455 .dump
= nft_ct_set_dump
,
458 static const struct nft_expr_ops
*
459 nft_ct_select_ops(const struct nft_ctx
*ctx
,
460 const struct nlattr
* const tb
[])
462 if (tb
[NFTA_CT_KEY
] == NULL
)
463 return ERR_PTR(-EINVAL
);
465 if (tb
[NFTA_CT_DREG
] && tb
[NFTA_CT_SREG
])
466 return ERR_PTR(-EINVAL
);
468 if (tb
[NFTA_CT_DREG
])
469 return &nft_ct_get_ops
;
471 if (tb
[NFTA_CT_SREG
])
472 return &nft_ct_set_ops
;
474 return ERR_PTR(-EINVAL
);
477 static struct nft_expr_type nft_ct_type __read_mostly
= {
479 .select_ops
= &nft_ct_select_ops
,
480 .policy
= nft_ct_policy
,
481 .maxattr
= NFTA_CT_MAX
,
482 .owner
= THIS_MODULE
,
485 static int __init
nft_ct_module_init(void)
487 return nft_register_expr(&nft_ct_type
);
490 static void __exit
nft_ct_module_exit(void)
492 nft_unregister_expr(&nft_ct_type
);
495 module_init(nft_ct_module_init
);
496 module_exit(nft_ct_module_exit
);
498 MODULE_LICENSE("GPL");
499 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
500 MODULE_ALIAS_NFT_EXPR("ct");