2 * test/set flag bits stored in conntrack extension area.
4 * (C) 2013 Astaro GmbH & Co KG
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.
11 #include <linux/export.h>
12 #include <linux/types.h>
14 #include <net/netfilter/nf_conntrack_ecache.h>
15 #include <net/netfilter/nf_conntrack_labels.h>
17 static spinlock_t nf_connlabels_lock
;
19 static int replace_u32(u32
*address
, u32 mask
, u32
new)
25 tmp
= (old
& mask
) ^ new;
28 } while (cmpxchg(address
, old
, tmp
) != old
);
33 int nf_connlabels_replace(struct nf_conn
*ct
,
35 const u32
*mask
, unsigned int words32
)
37 struct nf_conn_labels
*labels
;
42 labels
= nf_ct_labels_find(ct
);
46 size
= sizeof(labels
->bits
);
47 if (size
< (words32
* sizeof(u32
)))
48 words32
= size
/ sizeof(u32
);
50 dst
= (u32
*) labels
->bits
;
51 for (i
= 0; i
< words32
; i
++)
52 changed
|= replace_u32(&dst
[i
], mask
? ~mask
[i
] : 0, data
[i
]);
55 for (i
= words32
; i
< size
; i
++) /* pad */
56 replace_u32(&dst
[i
], 0, 0);
59 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
62 EXPORT_SYMBOL_GPL(nf_connlabels_replace
);
64 int nf_connlabels_get(struct net
*net
, unsigned int bits
)
66 if (BIT_WORD(bits
) >= NF_CT_LABELS_MAX_SIZE
/ sizeof(long))
69 spin_lock(&nf_connlabels_lock
);
70 net
->ct
.labels_used
++;
71 spin_unlock(&nf_connlabels_lock
);
75 EXPORT_SYMBOL_GPL(nf_connlabels_get
);
77 void nf_connlabels_put(struct net
*net
)
79 spin_lock(&nf_connlabels_lock
);
80 net
->ct
.labels_used
--;
81 spin_unlock(&nf_connlabels_lock
);
83 EXPORT_SYMBOL_GPL(nf_connlabels_put
);
85 static const struct nf_ct_ext_type labels_extend
= {
86 .len
= sizeof(struct nf_conn_labels
),
87 .align
= __alignof__(struct nf_conn_labels
),
88 .id
= NF_CT_EXT_LABELS
,
91 int nf_conntrack_labels_init(void)
93 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
/ sizeof(long) >= U8_MAX
);
95 spin_lock_init(&nf_connlabels_lock
);
96 return nf_ct_extend_register(&labels_extend
);
99 void nf_conntrack_labels_fini(void)
101 nf_ct_extend_unregister(&labels_extend
);