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;
34 static void nft_ct_get_eval(const struct nft_expr
*expr
,
35 struct nft_data data
[NFT_REG_MAX
+ 1],
36 const struct nft_pktinfo
*pkt
)
38 const struct nft_ct
*priv
= nft_expr_priv(expr
);
39 struct nft_data
*dest
= &data
[priv
->dreg
];
40 enum ip_conntrack_info ctinfo
;
41 const struct nf_conn
*ct
;
42 const struct nf_conn_help
*help
;
43 const struct nf_conntrack_tuple
*tuple
;
44 const struct nf_conntrack_helper
*helper
;
48 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
53 state
= NF_CT_STATE_INVALID_BIT
;
54 else if (nf_ct_is_untracked(ct
))
55 state
= NF_CT_STATE_UNTRACKED_BIT
;
57 state
= NF_CT_STATE_BIT(ctinfo
);
58 dest
->data
[0] = state
;
66 case NFT_CT_DIRECTION
:
67 dest
->data
[0] = CTINFO2DIR(ctinfo
);
70 dest
->data
[0] = ct
->status
;
72 #ifdef CONFIG_NF_CONNTRACK_MARK
74 dest
->data
[0] = ct
->mark
;
77 #ifdef CONFIG_NF_CONNTRACK_SECMARK
79 dest
->data
[0] = ct
->secmark
;
82 case NFT_CT_EXPIRATION
:
83 diff
= (long)jiffies
- (long)ct
->timeout
.expires
;
86 dest
->data
[0] = jiffies_to_msecs(diff
);
89 if (ct
->master
== NULL
)
91 help
= nfct_help(ct
->master
);
94 helper
= rcu_dereference(help
->helper
);
97 if (strlen(helper
->name
) >= sizeof(dest
->data
))
99 strncpy((char *)dest
->data
, helper
->name
, sizeof(dest
->data
));
101 #ifdef CONFIG_NF_CONNTRACK_LABELS
102 case NFT_CT_LABELS
: {
103 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
107 memset(dest
->data
, 0, sizeof(dest
->data
));
111 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
> sizeof(dest
->data
));
112 size
= labels
->words
* sizeof(long);
114 memcpy(dest
->data
, labels
->bits
, size
);
115 if (size
< sizeof(dest
->data
))
116 memset(((char *) dest
->data
) + size
, 0,
117 sizeof(dest
->data
) - size
);
123 tuple
= &ct
->tuplehash
[priv
->dir
].tuple
;
125 case NFT_CT_L3PROTOCOL
:
126 dest
->data
[0] = nf_ct_l3num(ct
);
129 memcpy(dest
->data
, tuple
->src
.u3
.all
,
130 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
133 memcpy(dest
->data
, tuple
->dst
.u3
.all
,
134 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
136 case NFT_CT_PROTOCOL
:
137 dest
->data
[0] = nf_ct_protonum(ct
);
139 case NFT_CT_PROTO_SRC
:
140 dest
->data
[0] = (__force __u16
)tuple
->src
.u
.all
;
142 case NFT_CT_PROTO_DST
:
143 dest
->data
[0] = (__force __u16
)tuple
->dst
.u
.all
;
148 data
[NFT_REG_VERDICT
].verdict
= NFT_BREAK
;
151 static void nft_ct_set_eval(const struct nft_expr
*expr
,
152 struct nft_data data
[NFT_REG_MAX
+ 1],
153 const struct nft_pktinfo
*pkt
)
155 const struct nft_ct
*priv
= nft_expr_priv(expr
);
156 struct sk_buff
*skb
= pkt
->skb
;
157 #ifdef CONFIG_NF_CONNTRACK_MARK
158 u32 value
= data
[priv
->sreg
].data
[0];
160 enum ip_conntrack_info ctinfo
;
163 ct
= nf_ct_get(skb
, &ctinfo
);
168 #ifdef CONFIG_NF_CONNTRACK_MARK
170 if (ct
->mark
!= value
) {
172 nf_conntrack_event_cache(IPCT_MARK
, ct
);
179 static const struct nla_policy nft_ct_policy
[NFTA_CT_MAX
+ 1] = {
180 [NFTA_CT_DREG
] = { .type
= NLA_U32
},
181 [NFTA_CT_KEY
] = { .type
= NLA_U32
},
182 [NFTA_CT_DIRECTION
] = { .type
= NLA_U8
},
183 [NFTA_CT_SREG
] = { .type
= NLA_U32
},
186 static int nft_ct_l3proto_try_module_get(uint8_t family
)
190 if (family
== NFPROTO_INET
) {
191 err
= nf_ct_l3proto_try_module_get(NFPROTO_IPV4
);
194 err
= nf_ct_l3proto_try_module_get(NFPROTO_IPV6
);
198 err
= nf_ct_l3proto_try_module_get(family
);
205 nf_ct_l3proto_module_put(NFPROTO_IPV4
);
210 static void nft_ct_l3proto_module_put(uint8_t family
)
212 if (family
== NFPROTO_INET
) {
213 nf_ct_l3proto_module_put(NFPROTO_IPV4
);
214 nf_ct_l3proto_module_put(NFPROTO_IPV6
);
216 nf_ct_l3proto_module_put(family
);
219 static int nft_ct_init_validate_get(const struct nft_expr
*expr
,
220 const struct nlattr
* const tb
[])
222 struct nft_ct
*priv
= nft_expr_priv(expr
);
224 if (tb
[NFTA_CT_DIRECTION
] != NULL
) {
225 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
227 case IP_CT_DIR_ORIGINAL
:
228 case IP_CT_DIR_REPLY
:
237 case NFT_CT_DIRECTION
:
239 #ifdef CONFIG_NF_CONNTRACK_MARK
242 #ifdef CONFIG_NF_CONNTRACK_SECMARK
245 #ifdef CONFIG_NF_CONNTRACK_LABELS
248 case NFT_CT_EXPIRATION
:
250 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
253 case NFT_CT_L3PROTOCOL
:
254 case NFT_CT_PROTOCOL
:
257 case NFT_CT_PROTO_SRC
:
258 case NFT_CT_PROTO_DST
:
259 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
269 static int nft_ct_init_validate_set(uint32_t key
)
281 static int nft_ct_init(const struct nft_ctx
*ctx
,
282 const struct nft_expr
*expr
,
283 const struct nlattr
* const tb
[])
285 struct nft_ct
*priv
= nft_expr_priv(expr
);
288 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
290 if (tb
[NFTA_CT_DREG
]) {
291 err
= nft_ct_init_validate_get(expr
, tb
);
295 priv
->dreg
= ntohl(nla_get_be32(tb
[NFTA_CT_DREG
]));
296 err
= nft_validate_output_register(priv
->dreg
);
300 err
= nft_validate_data_load(ctx
, priv
->dreg
, NULL
,
305 err
= nft_ct_init_validate_set(priv
->key
);
309 priv
->sreg
= ntohl(nla_get_be32(tb
[NFTA_CT_SREG
]));
310 err
= nft_validate_input_register(priv
->sreg
);
315 err
= nft_ct_l3proto_try_module_get(ctx
->afi
->family
);
319 priv
->family
= ctx
->afi
->family
;
324 static void nft_ct_destroy(const struct nft_expr
*expr
)
326 struct nft_ct
*priv
= nft_expr_priv(expr
);
328 nft_ct_l3proto_module_put(priv
->family
);
331 static int nft_ct_get_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
333 const struct nft_ct
*priv
= nft_expr_priv(expr
);
335 if (nla_put_be32(skb
, NFTA_CT_DREG
, htonl(priv
->dreg
)))
336 goto nla_put_failure
;
337 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
338 goto nla_put_failure
;
341 case NFT_CT_PROTOCOL
:
344 case NFT_CT_PROTO_SRC
:
345 case NFT_CT_PROTO_DST
:
346 if (nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
347 goto nla_put_failure
;
358 static int nft_ct_set_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
360 const struct nft_ct
*priv
= nft_expr_priv(expr
);
362 if (nla_put_be32(skb
, NFTA_CT_SREG
, htonl(priv
->sreg
)))
363 goto nla_put_failure
;
364 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
365 goto nla_put_failure
;
372 static struct nft_expr_type nft_ct_type
;
373 static const struct nft_expr_ops nft_ct_get_ops
= {
374 .type
= &nft_ct_type
,
375 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
376 .eval
= nft_ct_get_eval
,
378 .destroy
= nft_ct_destroy
,
379 .dump
= nft_ct_get_dump
,
382 static const struct nft_expr_ops nft_ct_set_ops
= {
383 .type
= &nft_ct_type
,
384 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
385 .eval
= nft_ct_set_eval
,
387 .destroy
= nft_ct_destroy
,
388 .dump
= nft_ct_set_dump
,
391 static const struct nft_expr_ops
*
392 nft_ct_select_ops(const struct nft_ctx
*ctx
,
393 const struct nlattr
* const tb
[])
395 if (tb
[NFTA_CT_KEY
] == NULL
)
396 return ERR_PTR(-EINVAL
);
398 if (tb
[NFTA_CT_DREG
] && tb
[NFTA_CT_SREG
])
399 return ERR_PTR(-EINVAL
);
401 if (tb
[NFTA_CT_DREG
])
402 return &nft_ct_get_ops
;
404 if (tb
[NFTA_CT_SREG
])
405 return &nft_ct_set_ops
;
407 return ERR_PTR(-EINVAL
);
410 static struct nft_expr_type nft_ct_type __read_mostly
= {
412 .select_ops
= &nft_ct_select_ops
,
413 .policy
= nft_ct_policy
,
414 .maxattr
= NFTA_CT_MAX
,
415 .owner
= THIS_MODULE
,
418 static int __init
nft_ct_module_init(void)
420 return nft_register_expr(&nft_ct_type
);
423 static void __exit
nft_ct_module_exit(void)
425 nft_unregister_expr(&nft_ct_type
);
428 module_init(nft_ct_module_init
);
429 module_exit(nft_ct_module_exit
);
431 MODULE_LICENSE("GPL");
432 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
433 MODULE_ALIAS_NFT_EXPR("ct");