1 // SPDX-License-Identifier: GPL-2.0-only
3 * test/set flag bits stored in conntrack extension area.
5 * (C) 2013 Astaro GmbH & Co KG
8 #include <linux/export.h>
9 #include <linux/types.h>
11 #include <net/netfilter/nf_conntrack_ecache.h>
12 #include <net/netfilter/nf_conntrack_labels.h>
14 static DEFINE_SPINLOCK(nf_connlabels_lock);
16 static int replace_u32(u32 *address, u32 mask, u32 new)
22 tmp = (old & mask) ^ new;
25 } while (cmpxchg(address, old, tmp) != old);
30 int nf_connlabels_replace(struct nf_conn *ct,
32 const u32 *mask, unsigned int words32)
34 struct nf_conn_labels *labels;
39 labels = nf_ct_labels_find(ct);
43 size = sizeof(labels->bits);
44 if (size < (words32 * sizeof(u32)))
45 words32 = size / sizeof(u32);
47 dst = (u32 *) labels->bits;
48 for (i = 0; i < words32; i++)
49 changed |= replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
52 for (i = words32; i < size; i++) /* pad */
53 replace_u32(&dst[i], 0, 0);
56 nf_conntrack_event_cache(IPCT_LABEL, ct);
59 EXPORT_SYMBOL_GPL(nf_connlabels_replace);
61 int nf_connlabels_get(struct net *net, unsigned int bits)
63 if (BIT_WORD(bits) >= NF_CT_LABELS_MAX_SIZE / sizeof(long))
66 spin_lock(&nf_connlabels_lock);
67 net->ct.labels_used++;
68 spin_unlock(&nf_connlabels_lock);
70 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX);
74 EXPORT_SYMBOL_GPL(nf_connlabels_get);
76 void nf_connlabels_put(struct net *net)
78 spin_lock(&nf_connlabels_lock);
79 net->ct.labels_used--;
80 spin_unlock(&nf_connlabels_lock);
82 EXPORT_SYMBOL_GPL(nf_connlabels_put);