blob: daa7c1383becc499e3170d5c72f1c86f232becf9 [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
17static unsigned int label_bits(const struct nf_conn_labels *l)
18{
19 unsigned int longs = l->words;
20 return longs * BITS_PER_LONG;
21}
22
23bool nf_connlabel_match(const struct nf_conn *ct, u16 bit)
24{
25 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
26
27 if (!labels)
28 return false;
29
30 return bit < label_bits(labels) && test_bit(bit, labels->bits);
31}
32EXPORT_SYMBOL_GPL(nf_connlabel_match);
33
34int nf_connlabel_set(struct nf_conn *ct, u16 bit)
35{
36 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
37
38 if (!labels || bit >= label_bits(labels))
39 return -ENOSPC;
40
41 if (test_bit(bit, labels->bits))
42 return 0;
43
Florian Westphal797a7d62013-06-21 16:51:30 +020044 if (!test_and_set_bit(bit, labels->bits))
Florian Westphal0ceabd82013-01-11 06:30:45 +000045 nf_conntrack_event_cache(IPCT_LABEL, ct);
Florian Westphalc539f012013-01-11 06:30:44 +000046
47 return 0;
48}
49EXPORT_SYMBOL_GPL(nf_connlabel_set);
50
Florian Westphal9b21f6a2013-01-11 06:30:46 +000051static void replace_u32(u32 *address, u32 mask, u32 new)
52{
53 u32 old, tmp;
54
55 do {
56 old = *address;
57 tmp = (old & mask) ^ new;
58 } while (cmpxchg(address, old, tmp) != old);
59}
60
61int nf_connlabels_replace(struct nf_conn *ct,
62 const u32 *data,
63 const u32 *mask, unsigned int words32)
64{
65 struct nf_conn_labels *labels;
66 unsigned int size, i;
67 u32 *dst;
68
69 labels = nf_ct_labels_find(ct);
70 if (!labels)
71 return -ENOSPC;
72
73 size = labels->words * sizeof(long);
74 if (size < (words32 * sizeof(u32)))
75 words32 = size / sizeof(u32);
76
77 dst = (u32 *) labels->bits;
78 if (words32) {
79 for (i = 0; i < words32; i++)
80 replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
81 }
82
83 size /= sizeof(u32);
84 for (i = words32; i < size; i++) /* pad */
85 replace_u32(&dst[i], 0, 0);
86
87 nf_conntrack_event_cache(IPCT_LABEL, ct);
88 return 0;
89}
90EXPORT_SYMBOL_GPL(nf_connlabels_replace);
Florian Westphal9b21f6a2013-01-11 06:30:46 +000091
Florian Westphalc539f012013-01-11 06:30:44 +000092static struct nf_ct_ext_type labels_extend __read_mostly = {
93 .len = sizeof(struct nf_conn_labels),
94 .align = __alignof__(struct nf_conn_labels),
95 .id = NF_CT_EXT_LABELS,
96};
97
Gao feng5f69b8f2013-01-21 22:10:31 +000098int nf_conntrack_labels_init(void)
Florian Westphalc539f012013-01-11 06:30:44 +000099{
Gao feng5f69b8f2013-01-21 22:10:31 +0000100 return nf_ct_extend_register(&labels_extend);
Florian Westphalc539f012013-01-11 06:30:44 +0000101}
102
Gao feng5f69b8f2013-01-21 22:10:31 +0000103void nf_conntrack_labels_fini(void)
Florian Westphalc539f012013-01-11 06:30:44 +0000104{
Gao feng5f69b8f2013-01-21 22:10:31 +0000105 nf_ct_extend_unregister(&labels_extend);
Florian Westphalc539f012013-01-11 06:30:44 +0000106}