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 spinlock_t 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
);
72 EXPORT_SYMBOL_GPL(nf_connlabels_get
);
74 void nf_connlabels_put(struct net
*net
)
76 spin_lock(&nf_connlabels_lock
);
77 net
->ct
.labels_used
--;
78 spin_unlock(&nf_connlabels_lock
);
80 EXPORT_SYMBOL_GPL(nf_connlabels_put
);
82 static const struct nf_ct_ext_type labels_extend
= {
83 .len
= sizeof(struct nf_conn_labels
),
84 .align
= __alignof__(struct nf_conn_labels
),
85 .id
= NF_CT_EXT_LABELS
,
88 int nf_conntrack_labels_init(void)
90 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
/ sizeof(long) >= U8_MAX
);
92 spin_lock_init(&nf_connlabels_lock
);
93 return nf_ct_extend_register(&labels_extend
);
96 void nf_conntrack_labels_fini(void)
98 nf_ct_extend_unregister(&labels_extend
);