]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nft_ct.c
netfilter: add and use nf_ct_netns_get/put
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nft_ct.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
25443261 3 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
96518518
PM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19#include <net/netfilter/nf_conntrack.h>
48f66c90 20#include <net/netfilter/nf_conntrack_acct.h>
96518518
PM
21#include <net/netfilter/nf_conntrack_tuple.h>
22#include <net/netfilter/nf_conntrack_helper.h>
c4ede3d3 23#include <net/netfilter/nf_conntrack_ecache.h>
d2bf2f34 24#include <net/netfilter/nf_conntrack_labels.h>
96518518
PM
25
26struct nft_ct {
27 enum nft_ct_keys key:8;
28 enum ip_conntrack_dir dir:8;
d46f2cd2 29 union {
c4ede3d3
KE
30 enum nft_registers dreg:8;
31 enum nft_registers sreg:8;
32 };
96518518
PM
33};
34
48f66c90
FW
35static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
36 enum nft_ct_keys k,
37 enum ip_conntrack_dir d)
38{
39 if (d < IP_CT_DIR_MAX)
40 return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) :
41 atomic64_read(&c[d].packets);
42
43 return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) +
44 nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY);
45}
46
c4ede3d3 47static void nft_ct_get_eval(const struct nft_expr *expr,
a55e22e9 48 struct nft_regs *regs,
c4ede3d3 49 const struct nft_pktinfo *pkt)
96518518
PM
50{
51 const struct nft_ct *priv = nft_expr_priv(expr);
49499c3e 52 u32 *dest = &regs->data[priv->dreg];
96518518
PM
53 enum ip_conntrack_info ctinfo;
54 const struct nf_conn *ct;
55 const struct nf_conn_help *help;
56 const struct nf_conntrack_tuple *tuple;
57 const struct nf_conntrack_helper *helper;
96518518
PM
58 unsigned int state;
59
60 ct = nf_ct_get(pkt->skb, &ctinfo);
61
62 switch (priv->key) {
63 case NFT_CT_STATE:
64 if (ct == NULL)
65 state = NF_CT_STATE_INVALID_BIT;
66 else if (nf_ct_is_untracked(ct))
67 state = NF_CT_STATE_UNTRACKED_BIT;
68 else
69 state = NF_CT_STATE_BIT(ctinfo);
fad136ea 70 *dest = state;
96518518 71 return;
c1f86676
DM
72 default:
73 break;
96518518
PM
74 }
75
76 if (ct == NULL)
77 goto err;
78
79 switch (priv->key) {
80 case NFT_CT_DIRECTION:
fad136ea 81 *dest = CTINFO2DIR(ctinfo);
96518518
PM
82 return;
83 case NFT_CT_STATUS:
fad136ea 84 *dest = ct->status;
96518518
PM
85 return;
86#ifdef CONFIG_NF_CONNTRACK_MARK
87 case NFT_CT_MARK:
fad136ea 88 *dest = ct->mark;
96518518
PM
89 return;
90#endif
91#ifdef CONFIG_NF_CONNTRACK_SECMARK
92 case NFT_CT_SECMARK:
fad136ea 93 *dest = ct->secmark;
96518518
PM
94 return;
95#endif
96 case NFT_CT_EXPIRATION:
c8607e02 97 *dest = jiffies_to_msecs(nf_ct_expires(ct));
96518518
PM
98 return;
99 case NFT_CT_HELPER:
100 if (ct->master == NULL)
101 goto err;
102 help = nfct_help(ct->master);
103 if (help == NULL)
104 goto err;
105 helper = rcu_dereference(help->helper);
106 if (helper == NULL)
107 goto err;
fad136ea 108 strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
96518518 109 return;
d2bf2f34
FW
110#ifdef CONFIG_NF_CONNTRACK_LABELS
111 case NFT_CT_LABELS: {
112 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
d2bf2f34 113
23014011
FW
114 if (labels)
115 memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE);
116 else
fad136ea 117 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
d2bf2f34
FW
118 return;
119 }
efaea94a 120#endif
48f66c90
FW
121 case NFT_CT_BYTES: /* fallthrough */
122 case NFT_CT_PKTS: {
123 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
124 u64 count = 0;
125
126 if (acct)
127 count = nft_ct_get_eval_counter(acct->counter,
128 priv->key, priv->dir);
129 memcpy(dest, &count, sizeof(count));
130 return;
131 }
d767ff2c
LZ
132 case NFT_CT_L3PROTOCOL:
133 *dest = nf_ct_l3num(ct);
134 return;
135 case NFT_CT_PROTOCOL:
136 *dest = nf_ct_protonum(ct);
137 return;
c1f86676
DM
138 default:
139 break;
96518518
PM
140 }
141
142 tuple = &ct->tuplehash[priv->dir].tuple;
143 switch (priv->key) {
96518518 144 case NFT_CT_SRC:
fad136ea 145 memcpy(dest, tuple->src.u3.all,
96518518
PM
146 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
147 return;
148 case NFT_CT_DST:
fad136ea 149 memcpy(dest, tuple->dst.u3.all,
96518518
PM
150 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
151 return;
96518518 152 case NFT_CT_PROTO_SRC:
fad136ea 153 *dest = (__force __u16)tuple->src.u.all;
96518518
PM
154 return;
155 case NFT_CT_PROTO_DST:
fad136ea 156 *dest = (__force __u16)tuple->dst.u.all;
96518518 157 return;
c1f86676
DM
158 default:
159 break;
96518518
PM
160 }
161 return;
162err:
a55e22e9 163 regs->verdict.code = NFT_BREAK;
96518518
PM
164}
165
c4ede3d3 166static void nft_ct_set_eval(const struct nft_expr *expr,
a55e22e9 167 struct nft_regs *regs,
c4ede3d3
KE
168 const struct nft_pktinfo *pkt)
169{
170 const struct nft_ct *priv = nft_expr_priv(expr);
171 struct sk_buff *skb = pkt->skb;
847c8e29 172#ifdef CONFIG_NF_CONNTRACK_MARK
49499c3e 173 u32 value = regs->data[priv->sreg];
847c8e29 174#endif
c4ede3d3
KE
175 enum ip_conntrack_info ctinfo;
176 struct nf_conn *ct;
177
178 ct = nf_ct_get(skb, &ctinfo);
179 if (ct == NULL)
180 return;
181
182 switch (priv->key) {
183#ifdef CONFIG_NF_CONNTRACK_MARK
184 case NFT_CT_MARK:
185 if (ct->mark != value) {
186 ct->mark = value;
187 nf_conntrack_event_cache(IPCT_MARK, ct);
188 }
189 break;
1ad8f48d
FW
190#endif
191#ifdef CONFIG_NF_CONNTRACK_LABELS
192 case NFT_CT_LABELS:
193 nf_connlabels_replace(ct,
194 &regs->data[priv->sreg],
195 &regs->data[priv->sreg],
196 NF_CT_LABELS_MAX_SIZE / sizeof(u32));
197 break;
c4ede3d3 198#endif
c1f86676
DM
199 default:
200 break;
c4ede3d3
KE
201 }
202}
203
96518518
PM
204static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
205 [NFTA_CT_DREG] = { .type = NLA_U32 },
206 [NFTA_CT_KEY] = { .type = NLA_U32 },
207 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
c4ede3d3 208 [NFTA_CT_SREG] = { .type = NLA_U32 },
96518518
PM
209};
210
ecb2421b 211static int nft_ct_netns_get(struct net *net, uint8_t family)
9638f33e
PM
212{
213 int err;
214
215 if (family == NFPROTO_INET) {
ecb2421b 216 err = nf_ct_netns_get(net, NFPROTO_IPV4);
9638f33e
PM
217 if (err < 0)
218 goto err1;
ecb2421b 219 err = nf_ct_netns_get(net, NFPROTO_IPV6);
9638f33e
PM
220 if (err < 0)
221 goto err2;
222 } else {
ecb2421b 223 err = nf_ct_netns_get(net, family);
9638f33e
PM
224 if (err < 0)
225 goto err1;
226 }
227 return 0;
228
229err2:
ecb2421b 230 nf_ct_netns_put(net, NFPROTO_IPV4);
9638f33e
PM
231err1:
232 return err;
233}
234
ecb2421b 235static void nft_ct_netns_put(struct net *net, uint8_t family)
9638f33e
PM
236{
237 if (family == NFPROTO_INET) {
ecb2421b
FW
238 nf_ct_netns_put(net, NFPROTO_IPV4);
239 nf_ct_netns_put(net, NFPROTO_IPV6);
9638f33e 240 } else
ecb2421b 241 nf_ct_netns_put(net, family);
9638f33e
PM
242}
243
fe92ca45
PM
244static int nft_ct_get_init(const struct nft_ctx *ctx,
245 const struct nft_expr *expr,
246 const struct nlattr * const tb[])
96518518
PM
247{
248 struct nft_ct *priv = nft_expr_priv(expr);
45d9bcda 249 unsigned int len;
fe92ca45 250 int err;
96518518 251
fe92ca45 252 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
96518518 253 switch (priv->key) {
96518518 254 case NFT_CT_DIRECTION:
45d9bcda
PM
255 if (tb[NFTA_CT_DIRECTION] != NULL)
256 return -EINVAL;
257 len = sizeof(u8);
258 break;
259 case NFT_CT_STATE:
96518518
PM
260 case NFT_CT_STATUS:
261#ifdef CONFIG_NF_CONNTRACK_MARK
262 case NFT_CT_MARK:
263#endif
264#ifdef CONFIG_NF_CONNTRACK_SECMARK
265 case NFT_CT_SECMARK:
d2bf2f34 266#endif
45d9bcda
PM
267 case NFT_CT_EXPIRATION:
268 if (tb[NFTA_CT_DIRECTION] != NULL)
269 return -EINVAL;
270 len = sizeof(u32);
271 break;
d2bf2f34
FW
272#ifdef CONFIG_NF_CONNTRACK_LABELS
273 case NFT_CT_LABELS:
45d9bcda
PM
274 if (tb[NFTA_CT_DIRECTION] != NULL)
275 return -EINVAL;
276 len = NF_CT_LABELS_MAX_SIZE;
277 break;
96518518 278#endif
96518518
PM
279 case NFT_CT_HELPER:
280 if (tb[NFTA_CT_DIRECTION] != NULL)
281 return -EINVAL;
45d9bcda 282 len = NF_CT_HELPER_NAME_LEN;
96518518 283 break;
45d9bcda 284
51292c07 285 case NFT_CT_L3PROTOCOL:
96518518 286 case NFT_CT_PROTOCOL:
d767ff2c
LZ
287 /* For compatibility, do not report error if NFTA_CT_DIRECTION
288 * attribute is specified.
289 */
45d9bcda
PM
290 len = sizeof(u8);
291 break;
96518518
PM
292 case NFT_CT_SRC:
293 case NFT_CT_DST:
45d9bcda
PM
294 if (tb[NFTA_CT_DIRECTION] == NULL)
295 return -EINVAL;
296
297 switch (ctx->afi->family) {
298 case NFPROTO_IPV4:
299 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
300 src.u3.ip);
301 break;
302 case NFPROTO_IPV6:
303 case NFPROTO_INET:
304 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
305 src.u3.ip6);
306 break;
307 default:
308 return -EAFNOSUPPORT;
309 }
310 break;
96518518
PM
311 case NFT_CT_PROTO_SRC:
312 case NFT_CT_PROTO_DST:
313 if (tb[NFTA_CT_DIRECTION] == NULL)
314 return -EINVAL;
45d9bcda 315 len = FIELD_SIZEOF(struct nf_conntrack_tuple, src.u.all);
96518518 316 break;
48f66c90
FW
317 case NFT_CT_BYTES:
318 case NFT_CT_PKTS:
319 /* no direction? return sum of original + reply */
320 if (tb[NFTA_CT_DIRECTION] == NULL)
321 priv->dir = IP_CT_DIR_MAX;
322 len = sizeof(u64);
323 break;
96518518
PM
324 default:
325 return -EOPNOTSUPP;
326 }
327
fe92ca45
PM
328 if (tb[NFTA_CT_DIRECTION] != NULL) {
329 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
330 switch (priv->dir) {
331 case IP_CT_DIR_ORIGINAL:
332 case IP_CT_DIR_REPLY:
333 break;
334 default:
335 return -EINVAL;
336 }
337 }
338
b1c96ed3 339 priv->dreg = nft_parse_register(tb[NFTA_CT_DREG]);
1ec10212
PM
340 err = nft_validate_register_store(ctx, priv->dreg, NULL,
341 NFT_DATA_VALUE, len);
fe92ca45
PM
342 if (err < 0)
343 return err;
344
ecb2421b 345 err = nft_ct_netns_get(ctx->net, ctx->afi->family);
fe92ca45
PM
346 if (err < 0)
347 return err;
348
3f8b61b7
LZ
349 if (priv->key == NFT_CT_BYTES || priv->key == NFT_CT_PKTS)
350 nf_ct_set_acct(ctx->net, true);
351
c4ede3d3
KE
352 return 0;
353}
354
fe92ca45
PM
355static int nft_ct_set_init(const struct nft_ctx *ctx,
356 const struct nft_expr *expr,
357 const struct nlattr * const tb[])
c4ede3d3 358{
fe92ca45 359 struct nft_ct *priv = nft_expr_priv(expr);
590025a2 360 bool label_got = false;
d07db988 361 unsigned int len;
fe92ca45
PM
362 int err;
363
364 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
365 switch (priv->key) {
e88e514e 366#ifdef CONFIG_NF_CONNTRACK_MARK
c4ede3d3 367 case NFT_CT_MARK:
7bfdde70
LZ
368 if (tb[NFTA_CT_DIRECTION])
369 return -EINVAL;
d07db988 370 len = FIELD_SIZEOF(struct nf_conn, mark);
c4ede3d3 371 break;
1ad8f48d
FW
372#endif
373#ifdef CONFIG_NF_CONNTRACK_LABELS
374 case NFT_CT_LABELS:
375 if (tb[NFTA_CT_DIRECTION])
376 return -EINVAL;
377 len = NF_CT_LABELS_MAX_SIZE;
378 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
379 if (err)
380 return err;
590025a2 381 label_got = true;
1ad8f48d 382 break;
e88e514e 383#endif
c4ede3d3
KE
384 default:
385 return -EOPNOTSUPP;
386 }
387
b1c96ed3 388 priv->sreg = nft_parse_register(tb[NFTA_CT_SREG]);
d07db988 389 err = nft_validate_register_load(priv->sreg, len);
fe92ca45 390 if (err < 0)
590025a2 391 goto err1;
c4ede3d3 392
ecb2421b 393 err = nft_ct_netns_get(ctx->net, ctx->afi->family);
96518518 394 if (err < 0)
590025a2 395 goto err1;
96518518 396
96518518 397 return 0;
590025a2
LZ
398
399err1:
400 if (label_got)
401 nf_connlabels_put(ctx->net);
402 return err;
403}
404
405static void nft_ct_get_destroy(const struct nft_ctx *ctx,
406 const struct nft_expr *expr)
407{
ecb2421b 408 nf_ct_netns_put(ctx->net, ctx->afi->family);
96518518
PM
409}
410
590025a2
LZ
411static void nft_ct_set_destroy(const struct nft_ctx *ctx,
412 const struct nft_expr *expr)
96518518 413{
1ad8f48d
FW
414 struct nft_ct *priv = nft_expr_priv(expr);
415
416 switch (priv->key) {
417#ifdef CONFIG_NF_CONNTRACK_LABELS
418 case NFT_CT_LABELS:
419 nf_connlabels_put(ctx->net);
420 break;
421#endif
422 default:
423 break;
424 }
425
ecb2421b 426 nft_ct_netns_put(ctx->net, ctx->afi->family);
96518518
PM
427}
428
c4ede3d3 429static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
96518518
PM
430{
431 const struct nft_ct *priv = nft_expr_priv(expr);
432
b1c96ed3 433 if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg))
96518518
PM
434 goto nla_put_failure;
435 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
436 goto nla_put_failure;
2a53bfb3
AB
437
438 switch (priv->key) {
2a53bfb3
AB
439 case NFT_CT_SRC:
440 case NFT_CT_DST:
441 case NFT_CT_PROTO_SRC:
442 case NFT_CT_PROTO_DST:
443 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
444 goto nla_put_failure;
48f66c90
FW
445 break;
446 case NFT_CT_BYTES:
447 case NFT_CT_PKTS:
448 if (priv->dir < IP_CT_DIR_MAX &&
449 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
450 goto nla_put_failure;
451 break;
2a53bfb3
AB
452 default:
453 break;
454 }
455
96518518
PM
456 return 0;
457
458nla_put_failure:
459 return -1;
460}
461
c4ede3d3
KE
462static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
463{
464 const struct nft_ct *priv = nft_expr_priv(expr);
465
b1c96ed3 466 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
c4ede3d3
KE
467 goto nla_put_failure;
468 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
469 goto nla_put_failure;
470 return 0;
471
472nla_put_failure:
473 return -1;
474}
475
ef1f7df9 476static struct nft_expr_type nft_ct_type;
c4ede3d3 477static const struct nft_expr_ops nft_ct_get_ops = {
ef1f7df9 478 .type = &nft_ct_type,
96518518 479 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
c4ede3d3 480 .eval = nft_ct_get_eval,
fe92ca45 481 .init = nft_ct_get_init,
590025a2 482 .destroy = nft_ct_get_destroy,
c4ede3d3 483 .dump = nft_ct_get_dump,
ef1f7df9
PM
484};
485
c4ede3d3
KE
486static const struct nft_expr_ops nft_ct_set_ops = {
487 .type = &nft_ct_type,
488 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
489 .eval = nft_ct_set_eval,
fe92ca45 490 .init = nft_ct_set_init,
590025a2 491 .destroy = nft_ct_set_destroy,
c4ede3d3
KE
492 .dump = nft_ct_set_dump,
493};
494
495static const struct nft_expr_ops *
496nft_ct_select_ops(const struct nft_ctx *ctx,
497 const struct nlattr * const tb[])
498{
499 if (tb[NFTA_CT_KEY] == NULL)
500 return ERR_PTR(-EINVAL);
501
502 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
503 return ERR_PTR(-EINVAL);
504
505 if (tb[NFTA_CT_DREG])
506 return &nft_ct_get_ops;
507
508 if (tb[NFTA_CT_SREG])
509 return &nft_ct_set_ops;
510
511 return ERR_PTR(-EINVAL);
512}
513
ef1f7df9
PM
514static struct nft_expr_type nft_ct_type __read_mostly = {
515 .name = "ct",
c4ede3d3 516 .select_ops = &nft_ct_select_ops,
96518518
PM
517 .policy = nft_ct_policy,
518 .maxattr = NFTA_CT_MAX,
ef1f7df9 519 .owner = THIS_MODULE,
96518518
PM
520};
521
25443261
PNA
522static void nft_notrack_eval(const struct nft_expr *expr,
523 struct nft_regs *regs,
524 const struct nft_pktinfo *pkt)
525{
526 struct sk_buff *skb = pkt->skb;
527 enum ip_conntrack_info ctinfo;
528 struct nf_conn *ct;
529
530 ct = nf_ct_get(pkt->skb, &ctinfo);
531 /* Previously seen (loopback or untracked)? Ignore. */
532 if (ct)
533 return;
534
535 ct = nf_ct_untracked_get();
536 atomic_inc(&ct->ct_general.use);
537 skb->nfct = &ct->ct_general;
538 skb->nfctinfo = IP_CT_NEW;
539}
540
541static struct nft_expr_type nft_notrack_type;
542static const struct nft_expr_ops nft_notrack_ops = {
543 .type = &nft_notrack_type,
544 .size = NFT_EXPR_SIZE(0),
545 .eval = nft_notrack_eval,
546};
547
548static struct nft_expr_type nft_notrack_type __read_mostly = {
549 .name = "notrack",
550 .ops = &nft_notrack_ops,
551 .owner = THIS_MODULE,
552};
553
96518518
PM
554static int __init nft_ct_module_init(void)
555{
25443261
PNA
556 int err;
557
adff6c65
FW
558 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE);
559
25443261
PNA
560 err = nft_register_expr(&nft_ct_type);
561 if (err < 0)
562 return err;
563
564 err = nft_register_expr(&nft_notrack_type);
565 if (err < 0)
566 goto err1;
567
568 return 0;
569err1:
570 nft_unregister_expr(&nft_ct_type);
571 return err;
96518518
PM
572}
573
574static void __exit nft_ct_module_exit(void)
575{
25443261 576 nft_unregister_expr(&nft_notrack_type);
ef1f7df9 577 nft_unregister_expr(&nft_ct_type);
96518518
PM
578}
579
580module_init(nft_ct_module_init);
581module_exit(nft_ct_module_exit);
582
583MODULE_LICENSE("GPL");
584MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
585MODULE_ALIAS_NFT_EXPR("ct");
25443261 586MODULE_ALIAS_NFT_EXPR("notrack");