blob: bcab8bde73128f9f9f8b4dba31a1c283b07f8eb1 [file] [log] [blame]
Florian Westphalc539f012013-01-11 06:30:44 +00001/*
2 * test/set flag bits stored in conntrack extension area.
3 *
4 * (C) 2013 Astaro GmbH & Co KG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Florian Westphalc539f012013-01-11 06:30:44 +000011#include <linux/export.h>
Florian Westphalc539f012013-01-11 06:30:44 +000012#include <linux/types.h>
Florian Westphalc539f012013-01-11 06:30:44 +000013
14#include <net/netfilter/nf_conntrack_ecache.h>
15#include <net/netfilter/nf_conntrack_labels.h>
16
Joe Stringer86ca02e2015-08-26 11:31:51 -070017static spinlock_t nf_connlabels_lock;
18
Florian Westphal5a8145f2016-04-12 18:14:24 +020019static int replace_u32(u32 *address, u32 mask, u32 new)
Florian Westphal9b21f6a2013-01-11 06:30:46 +000020{
21 u32 old, tmp;
22
23 do {
24 old = *address;
25 tmp = (old & mask) ^ new;
Florian Westphal5a8145f2016-04-12 18:14:24 +020026 if (old == tmp)
27 return 0;
Florian Westphal9b21f6a2013-01-11 06:30:46 +000028 } while (cmpxchg(address, old, tmp) != old);
Florian Westphal5a8145f2016-04-12 18:14:24 +020029
30 return 1;
Florian Westphal9b21f6a2013-01-11 06:30:46 +000031}
32
33int nf_connlabels_replace(struct nf_conn *ct,
34 const u32 *data,
35 const u32 *mask, unsigned int words32)
36{
37 struct nf_conn_labels *labels;
38 unsigned int size, i;
Florian Westphal5a8145f2016-04-12 18:14:24 +020039 int changed = 0;
Florian Westphal9b21f6a2013-01-11 06:30:46 +000040 u32 *dst;
41
42 labels = nf_ct_labels_find(ct);
43 if (!labels)
44 return -ENOSPC;
45
Florian Westphal23014012016-07-21 12:51:16 +020046 size = sizeof(labels->bits);
Florian Westphal9b21f6a2013-01-11 06:30:46 +000047 if (size < (words32 * sizeof(u32)))
48 words32 = size / sizeof(u32);
49
50 dst = (u32 *) labels->bits;
Florian Westphal5a8145f2016-04-12 18:14:24 +020051 for (i = 0; i < words32; i++)
52 changed |= replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
Florian Westphal9b21f6a2013-01-11 06:30:46 +000053
54 size /= sizeof(u32);
55 for (i = words32; i < size; i++) /* pad */
56 replace_u32(&dst[i], 0, 0);
57
Florian Westphal5a8145f2016-04-12 18:14:24 +020058 if (changed)
59 nf_conntrack_event_cache(IPCT_LABEL, ct);
Florian Westphal9b21f6a2013-01-11 06:30:46 +000060 return 0;
61}
62EXPORT_SYMBOL_GPL(nf_connlabels_replace);
Florian Westphal9b21f6a2013-01-11 06:30:46 +000063
Florian Westphaladff6c62016-04-12 18:14:25 +020064int nf_connlabels_get(struct net *net, unsigned int bits)
Joe Stringer86ca02e2015-08-26 11:31:51 -070065{
Florian Westphal23014012016-07-21 12:51:16 +020066 if (BIT_WORD(bits) >= NF_CT_LABELS_MAX_SIZE / sizeof(long))
Joe Stringer86ca02e2015-08-26 11:31:51 -070067 return -ERANGE;
68
Joe Stringer86ca02e2015-08-26 11:31:51 -070069 spin_lock(&nf_connlabels_lock);
70 net->ct.labels_used++;
Joe Stringer86ca02e2015-08-26 11:31:51 -070071 spin_unlock(&nf_connlabels_lock);
72
73 return 0;
74}
75EXPORT_SYMBOL_GPL(nf_connlabels_get);
76
77void nf_connlabels_put(struct net *net)
78{
79 spin_lock(&nf_connlabels_lock);
80 net->ct.labels_used--;
Joe Stringer86ca02e2015-08-26 11:31:51 -070081 spin_unlock(&nf_connlabels_lock);
82}
83EXPORT_SYMBOL_GPL(nf_connlabels_put);
84
Florian Westphalc539f012013-01-11 06:30:44 +000085static struct nf_ct_ext_type labels_extend __read_mostly = {
86 .len = sizeof(struct nf_conn_labels),
87 .align = __alignof__(struct nf_conn_labels),
88 .id = NF_CT_EXT_LABELS,
89};
90
Gao feng5f69b8f2013-01-21 22:10:31 +000091int nf_conntrack_labels_init(void)
Florian Westphalc539f012013-01-11 06:30:44 +000092{
Florian Westphaladff6c62016-04-12 18:14:25 +020093 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX);
94
Joe Stringer86ca02e2015-08-26 11:31:51 -070095 spin_lock_init(&nf_connlabels_lock);
Gao feng5f69b8f2013-01-21 22:10:31 +000096 return nf_ct_extend_register(&labels_extend);
Florian Westphalc539f012013-01-11 06:30:44 +000097}
98
Gao feng5f69b8f2013-01-21 22:10:31 +000099void nf_conntrack_labels_fini(void)
Florian Westphalc539f012013-01-11 06:30:44 +0000100{
Gao feng5f69b8f2013-01-21 22:10:31 +0000101 nf_ct_extend_unregister(&labels_extend);
Florian Westphalc539f012013-01-11 06:30:44 +0000102}