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 int replace_u32(u32
*address
, u32 mask
, u32
new)
20 tmp
= (old
& mask
) ^ new;
23 } while (cmpxchg(address
, old
, tmp
) != old
);
28 int nf_connlabels_replace(struct nf_conn
*ct
,
30 const u32
*mask
, unsigned int words32
)
32 struct nf_conn_labels
*labels
;
37 labels
= nf_ct_labels_find(ct
);
41 size
= sizeof(labels
->bits
);
42 if (size
< (words32
* sizeof(u32
)))
43 words32
= size
/ sizeof(u32
);
45 dst
= (u32
*) labels
->bits
;
46 for (i
= 0; i
< words32
; i
++)
47 changed
|= replace_u32(&dst
[i
], mask
? ~mask
[i
] : 0, data
[i
]);
50 for (i
= words32
; i
< size
; i
++) /* pad */
51 replace_u32(&dst
[i
], 0, 0);
54 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
57 EXPORT_SYMBOL_GPL(nf_connlabels_replace
);
59 int nf_connlabels_get(struct net
*net
, unsigned int bits
)
63 if (BIT_WORD(bits
) >= NF_CT_LABELS_MAX_SIZE
/ sizeof(long))
66 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
/ sizeof(long) >= U8_MAX
);
68 v
= atomic_inc_return_relaxed(&net
->ct
.labels_used
);
73 EXPORT_SYMBOL_GPL(nf_connlabels_get
);
75 void nf_connlabels_put(struct net
*net
)
77 int v
= atomic_dec_return_relaxed(&net
->ct
.labels_used
);
81 EXPORT_SYMBOL_GPL(nf_connlabels_put
);