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_regs
*regs
,
35 const struct nft_pktinfo
*pkt
)
37 const struct nft_ct
*priv
= nft_expr_priv(expr
);
38 u32
*dest
= ®s
->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
);
67 case NFT_CT_DIRECTION
:
68 *dest
= CTINFO2DIR(ctinfo
);
73 #ifdef CONFIG_NF_CONNTRACK_MARK
78 #ifdef CONFIG_NF_CONNTRACK_SECMARK
83 case NFT_CT_EXPIRATION
:
84 diff
= (long)jiffies
- (long)ct
->timeout
.expires
;
87 *dest
= jiffies_to_msecs(diff
);
90 if (ct
->master
== NULL
)
92 help
= nfct_help(ct
->master
);
95 helper
= rcu_dereference(help
->helper
);
98 strncpy((char *)dest
, helper
->name
, NF_CT_HELPER_NAME_LEN
);
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
, 0, NF_CT_LABELS_MAX_SIZE
);
110 size
= labels
->words
* sizeof(long);
111 memcpy(dest
, labels
->bits
, size
);
112 if (size
< NF_CT_LABELS_MAX_SIZE
)
113 memset(((char *) dest
) + size
, 0,
114 NF_CT_LABELS_MAX_SIZE
- size
);
122 tuple
= &ct
->tuplehash
[priv
->dir
].tuple
;
124 case NFT_CT_L3PROTOCOL
:
125 *dest
= nf_ct_l3num(ct
);
128 memcpy(dest
, tuple
->src
.u3
.all
,
129 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
132 memcpy(dest
, tuple
->dst
.u3
.all
,
133 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
135 case NFT_CT_PROTOCOL
:
136 *dest
= nf_ct_protonum(ct
);
138 case NFT_CT_PROTO_SRC
:
139 *dest
= (__force __u16
)tuple
->src
.u
.all
;
141 case NFT_CT_PROTO_DST
:
142 *dest
= (__force __u16
)tuple
->dst
.u
.all
;
149 regs
->verdict
.code
= NFT_BREAK
;
152 static void nft_ct_set_eval(const struct nft_expr
*expr
,
153 struct nft_regs
*regs
,
154 const struct nft_pktinfo
*pkt
)
156 const struct nft_ct
*priv
= nft_expr_priv(expr
);
157 struct sk_buff
*skb
= pkt
->skb
;
158 #ifdef CONFIG_NF_CONNTRACK_MARK
159 u32 value
= regs
->data
[priv
->sreg
];
161 enum ip_conntrack_info ctinfo
;
164 ct
= nf_ct_get(skb
, &ctinfo
);
169 #ifdef CONFIG_NF_CONNTRACK_MARK
171 if (ct
->mark
!= value
) {
173 nf_conntrack_event_cache(IPCT_MARK
, ct
);
182 static const struct nla_policy nft_ct_policy
[NFTA_CT_MAX
+ 1] = {
183 [NFTA_CT_DREG
] = { .type
= NLA_U32
},
184 [NFTA_CT_KEY
] = { .type
= NLA_U32
},
185 [NFTA_CT_DIRECTION
] = { .type
= NLA_U8
},
186 [NFTA_CT_SREG
] = { .type
= NLA_U32
},
189 static int nft_ct_l3proto_try_module_get(uint8_t family
)
193 if (family
== NFPROTO_INET
) {
194 err
= nf_ct_l3proto_try_module_get(NFPROTO_IPV4
);
197 err
= nf_ct_l3proto_try_module_get(NFPROTO_IPV6
);
201 err
= nf_ct_l3proto_try_module_get(family
);
208 nf_ct_l3proto_module_put(NFPROTO_IPV4
);
213 static void nft_ct_l3proto_module_put(uint8_t family
)
215 if (family
== NFPROTO_INET
) {
216 nf_ct_l3proto_module_put(NFPROTO_IPV4
);
217 nf_ct_l3proto_module_put(NFPROTO_IPV6
);
219 nf_ct_l3proto_module_put(family
);
222 static int nft_ct_get_init(const struct nft_ctx
*ctx
,
223 const struct nft_expr
*expr
,
224 const struct nlattr
* const tb
[])
226 struct nft_ct
*priv
= nft_expr_priv(expr
);
230 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
232 case NFT_CT_DIRECTION
:
233 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
239 #ifdef CONFIG_NF_CONNTRACK_MARK
242 #ifdef CONFIG_NF_CONNTRACK_SECMARK
245 case NFT_CT_EXPIRATION
:
246 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
250 #ifdef CONFIG_NF_CONNTRACK_LABELS
252 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
254 len
= NF_CT_LABELS_MAX_SIZE
;
258 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
260 len
= NF_CT_HELPER_NAME_LEN
;
263 case NFT_CT_L3PROTOCOL
:
264 case NFT_CT_PROTOCOL
:
265 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
271 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
274 switch (ctx
->afi
->family
) {
276 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
,
281 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
,
285 return -EAFNOSUPPORT
;
288 case NFT_CT_PROTO_SRC
:
289 case NFT_CT_PROTO_DST
:
290 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
292 len
= FIELD_SIZEOF(struct nf_conntrack_tuple
, src
.u
.all
);
298 if (tb
[NFTA_CT_DIRECTION
] != NULL
) {
299 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
301 case IP_CT_DIR_ORIGINAL
:
302 case IP_CT_DIR_REPLY
:
309 priv
->dreg
= nft_parse_register(tb
[NFTA_CT_DREG
]);
310 err
= nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
311 NFT_DATA_VALUE
, len
);
315 err
= nft_ct_l3proto_try_module_get(ctx
->afi
->family
);
322 static int nft_ct_set_init(const struct nft_ctx
*ctx
,
323 const struct nft_expr
*expr
,
324 const struct nlattr
* const tb
[])
326 struct nft_ct
*priv
= nft_expr_priv(expr
);
330 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
332 #ifdef CONFIG_NF_CONNTRACK_MARK
334 len
= FIELD_SIZEOF(struct nf_conn
, mark
);
341 priv
->sreg
= nft_parse_register(tb
[NFTA_CT_SREG
]);
342 err
= nft_validate_register_load(priv
->sreg
, len
);
346 err
= nft_ct_l3proto_try_module_get(ctx
->afi
->family
);
353 static void nft_ct_destroy(const struct nft_ctx
*ctx
,
354 const struct nft_expr
*expr
)
356 nft_ct_l3proto_module_put(ctx
->afi
->family
);
359 static int nft_ct_get_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
361 const struct nft_ct
*priv
= nft_expr_priv(expr
);
363 if (nft_dump_register(skb
, NFTA_CT_DREG
, priv
->dreg
))
364 goto nla_put_failure
;
365 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
366 goto nla_put_failure
;
369 case NFT_CT_PROTOCOL
:
372 case NFT_CT_PROTO_SRC
:
373 case NFT_CT_PROTO_DST
:
374 if (nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
375 goto nla_put_failure
;
386 static int nft_ct_set_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
388 const struct nft_ct
*priv
= nft_expr_priv(expr
);
390 if (nft_dump_register(skb
, NFTA_CT_SREG
, priv
->sreg
))
391 goto nla_put_failure
;
392 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
393 goto nla_put_failure
;
400 static struct nft_expr_type nft_ct_type
;
401 static const struct nft_expr_ops nft_ct_get_ops
= {
402 .type
= &nft_ct_type
,
403 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
404 .eval
= nft_ct_get_eval
,
405 .init
= nft_ct_get_init
,
406 .destroy
= nft_ct_destroy
,
407 .dump
= nft_ct_get_dump
,
410 static const struct nft_expr_ops nft_ct_set_ops
= {
411 .type
= &nft_ct_type
,
412 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
413 .eval
= nft_ct_set_eval
,
414 .init
= nft_ct_set_init
,
415 .destroy
= nft_ct_destroy
,
416 .dump
= nft_ct_set_dump
,
419 static const struct nft_expr_ops
*
420 nft_ct_select_ops(const struct nft_ctx
*ctx
,
421 const struct nlattr
* const tb
[])
423 if (tb
[NFTA_CT_KEY
] == NULL
)
424 return ERR_PTR(-EINVAL
);
426 if (tb
[NFTA_CT_DREG
] && tb
[NFTA_CT_SREG
])
427 return ERR_PTR(-EINVAL
);
429 if (tb
[NFTA_CT_DREG
])
430 return &nft_ct_get_ops
;
432 if (tb
[NFTA_CT_SREG
])
433 return &nft_ct_set_ops
;
435 return ERR_PTR(-EINVAL
);
438 static struct nft_expr_type nft_ct_type __read_mostly
= {
440 .select_ops
= &nft_ct_select_ops
,
441 .policy
= nft_ct_policy
,
442 .maxattr
= NFTA_CT_MAX
,
443 .owner
= THIS_MODULE
,
446 static int __init
nft_ct_module_init(void)
448 return nft_register_expr(&nft_ct_type
);
451 static void __exit
nft_ct_module_exit(void)
453 nft_unregister_expr(&nft_ct_type
);
456 module_init(nft_ct_module_init
);
457 module_exit(nft_ct_module_exit
);
459 MODULE_LICENSE("GPL");
460 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
461 MODULE_ALIAS_NFT_EXPR("ct");