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>
24 enum nft_ct_keys key:8;
25 enum ip_conntrack_dir dir:8;
27 enum nft_registers dreg:8;
28 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));
102 tuple = &ct->tuplehash[priv->dir].tuple;
104 case NFT_CT_L3PROTOCOL:
105 dest->data[0] = nf_ct_l3num(ct);
108 memcpy(dest->data, tuple->src.u3.all,
109 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
112 memcpy(dest->data, tuple->dst.u3.all,
113 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
115 case NFT_CT_PROTOCOL:
116 dest->data[0] = nf_ct_protonum(ct);
118 case NFT_CT_PROTO_SRC:
119 dest->data[0] = (__force __u16)tuple->src.u.all;
121 case NFT_CT_PROTO_DST:
122 dest->data[0] = (__force __u16)tuple->dst.u.all;
127 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
130 static void nft_ct_set_eval(const struct nft_expr *expr,
131 struct nft_data data[NFT_REG_MAX + 1],
132 const struct nft_pktinfo *pkt)
134 const struct nft_ct *priv = nft_expr_priv(expr);
135 struct sk_buff *skb = pkt->skb;
136 #ifdef CONFIG_NF_CONNTRACK_MARK
137 u32 value = data[priv->sreg].data[0];
139 enum ip_conntrack_info ctinfo;
142 ct = nf_ct_get(skb, &ctinfo);
147 #ifdef CONFIG_NF_CONNTRACK_MARK
149 if (ct->mark != value) {
151 nf_conntrack_event_cache(IPCT_MARK, ct);
158 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
159 [NFTA_CT_DREG] = { .type = NLA_U32 },
160 [NFTA_CT_KEY] = { .type = NLA_U32 },
161 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
162 [NFTA_CT_SREG] = { .type = NLA_U32 },
165 static int nft_ct_l3proto_try_module_get(uint8_t family)
169 if (family == NFPROTO_INET) {
170 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV4);
173 err = nf_ct_l3proto_try_module_get(NFPROTO_IPV6);
177 err = nf_ct_l3proto_try_module_get(family);
184 nf_ct_l3proto_module_put(NFPROTO_IPV4);
189 static void nft_ct_l3proto_module_put(uint8_t family)
191 if (family == NFPROTO_INET) {
192 nf_ct_l3proto_module_put(NFPROTO_IPV4);
193 nf_ct_l3proto_module_put(NFPROTO_IPV6);
195 nf_ct_l3proto_module_put(family);
198 static int nft_ct_init_validate_get(const struct nft_expr *expr,
199 const struct nlattr * const tb[])
201 struct nft_ct *priv = nft_expr_priv(expr);
203 if (tb[NFTA_CT_DIRECTION] != NULL) {
204 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
206 case IP_CT_DIR_ORIGINAL:
207 case IP_CT_DIR_REPLY:
216 case NFT_CT_DIRECTION:
218 #ifdef CONFIG_NF_CONNTRACK_MARK
221 #ifdef CONFIG_NF_CONNTRACK_SECMARK
224 case NFT_CT_EXPIRATION:
226 if (tb[NFTA_CT_DIRECTION] != NULL)
229 case NFT_CT_PROTOCOL:
232 case NFT_CT_PROTO_SRC:
233 case NFT_CT_PROTO_DST:
234 if (tb[NFTA_CT_DIRECTION] == NULL)
244 static int nft_ct_init_validate_set(uint32_t key)
256 static int nft_ct_init(const struct nft_ctx *ctx,
257 const struct nft_expr *expr,
258 const struct nlattr * const tb[])
260 struct nft_ct *priv = nft_expr_priv(expr);
263 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
265 if (tb[NFTA_CT_DREG]) {
266 err = nft_ct_init_validate_get(expr, tb);
270 priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
271 err = nft_validate_output_register(priv->dreg);
275 err = nft_validate_data_load(ctx, priv->dreg, NULL,
280 err = nft_ct_init_validate_set(priv->key);
284 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG]));
285 err = nft_validate_input_register(priv->sreg);
290 err = nft_ct_l3proto_try_module_get(ctx->afi->family);
294 priv->family = ctx->afi->family;
299 static void nft_ct_destroy(const struct nft_expr *expr)
301 struct nft_ct *priv = nft_expr_priv(expr);
303 nft_ct_l3proto_module_put(priv->family);
306 static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
308 const struct nft_ct *priv = nft_expr_priv(expr);
310 if (nla_put_be32(skb, NFTA_CT_DREG, htonl(priv->dreg)))
311 goto nla_put_failure;
312 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
313 goto nla_put_failure;
314 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
315 goto nla_put_failure;
322 static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
324 const struct nft_ct *priv = nft_expr_priv(expr);
326 if (nla_put_be32(skb, NFTA_CT_SREG, htonl(priv->sreg)))
327 goto nla_put_failure;
328 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
329 goto nla_put_failure;
336 static struct nft_expr_type nft_ct_type;
337 static const struct nft_expr_ops nft_ct_get_ops = {
338 .type = &nft_ct_type,
339 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
340 .eval = nft_ct_get_eval,
342 .destroy = nft_ct_destroy,
343 .dump = nft_ct_get_dump,
346 static const struct nft_expr_ops nft_ct_set_ops = {
347 .type = &nft_ct_type,
348 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
349 .eval = nft_ct_set_eval,
351 .destroy = nft_ct_destroy,
352 .dump = nft_ct_set_dump,
355 static const struct nft_expr_ops *
356 nft_ct_select_ops(const struct nft_ctx *ctx,
357 const struct nlattr * const tb[])
359 if (tb[NFTA_CT_KEY] == NULL)
360 return ERR_PTR(-EINVAL);
362 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
363 return ERR_PTR(-EINVAL);
365 if (tb[NFTA_CT_DREG])
366 return &nft_ct_get_ops;
368 if (tb[NFTA_CT_SREG])
369 return &nft_ct_set_ops;
371 return ERR_PTR(-EINVAL);
374 static struct nft_expr_type nft_ct_type __read_mostly = {
376 .select_ops = &nft_ct_select_ops,
377 .policy = nft_ct_policy,
378 .maxattr = NFTA_CT_MAX,
379 .owner = THIS_MODULE,
382 static int __init nft_ct_module_init(void)
384 return nft_register_expr(&nft_ct_type);
387 static void __exit nft_ct_module_exit(void)
389 nft_unregister_expr(&nft_ct_type);
392 module_init(nft_ct_module_init);
393 module_exit(nft_ct_module_exit);
395 MODULE_LICENSE("GPL");
397 MODULE_ALIAS_NFT_EXPR("ct");