Skip to content

Commit 86ca02e

Browse files
joestringerdavem330
authored andcommitted
netfilter: connlabels: Export setting connlabel length
Add functions to change connlabel length into nf_conntrack_labels.c so they may be reused by other modules like OVS and nftables without needing to jump through xt_match_check() hoops. Suggested-by: Florian Westphal <fw@strlen.de> Signed-off-by: Joe Stringer <joestringer@nicira.com> Acked-by: Florian Westphal <fw@strlen.de> Acked-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 55e5713 commit 86ca02e

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

include/net/netfilter/nf_conntrack_labels.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ int nf_connlabels_replace(struct nf_conn *ct,
5454
#ifdef CONFIG_NF_CONNTRACK_LABELS
5555
int nf_conntrack_labels_init(void);
5656
void nf_conntrack_labels_fini(void);
57+
int nf_connlabels_get(struct net *net, unsigned int n_bits);
58+
void nf_connlabels_put(struct net *net);
5759
#else
5860
static inline int nf_conntrack_labels_init(void) { return 0; }
5961
static inline void nf_conntrack_labels_fini(void) {}
62+
static inline int nf_connlabels_get(struct net *net, unsigned int n_bits) { return 0; }
63+
static inline void nf_connlabels_put(struct net *net) {}
6064
#endif

net/netfilter/nf_conntrack_labels.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <net/netfilter/nf_conntrack_ecache.h>
1515
#include <net/netfilter/nf_conntrack_labels.h>
1616

17+
static spinlock_t nf_connlabels_lock;
18+
1719
static unsigned int label_bits(const struct nf_conn_labels *l)
1820
{
1921
unsigned int longs = l->words;
@@ -89,6 +91,35 @@ int nf_connlabels_replace(struct nf_conn *ct,
8991
}
9092
EXPORT_SYMBOL_GPL(nf_connlabels_replace);
9193

94+
int nf_connlabels_get(struct net *net, unsigned int n_bits)
95+
{
96+
size_t words;
97+
98+
if (n_bits > (NF_CT_LABELS_MAX_SIZE * BITS_PER_BYTE))
99+
return -ERANGE;
100+
101+
words = BITS_TO_LONGS(n_bits);
102+
103+
spin_lock(&nf_connlabels_lock);
104+
net->ct.labels_used++;
105+
if (words > net->ct.label_words)
106+
net->ct.label_words = words;
107+
spin_unlock(&nf_connlabels_lock);
108+
109+
return 0;
110+
}
111+
EXPORT_SYMBOL_GPL(nf_connlabels_get);
112+
113+
void nf_connlabels_put(struct net *net)
114+
{
115+
spin_lock(&nf_connlabels_lock);
116+
net->ct.labels_used--;
117+
if (net->ct.labels_used == 0)
118+
net->ct.label_words = 0;
119+
spin_unlock(&nf_connlabels_lock);
120+
}
121+
EXPORT_SYMBOL_GPL(nf_connlabels_put);
122+
92123
static struct nf_ct_ext_type labels_extend __read_mostly = {
93124
.len = sizeof(struct nf_conn_labels),
94125
.align = __alignof__(struct nf_conn_labels),
@@ -97,6 +128,7 @@ static struct nf_ct_ext_type labels_extend __read_mostly = {
97128

98129
int nf_conntrack_labels_init(void)
99130
{
131+
spin_lock_init(&nf_connlabels_lock);
100132
return nf_ct_extend_register(&labels_extend);
101133
}
102134

net/netfilter/xt_connlabel.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ static int connlabel_mt_check(const struct xt_mtchk_param *par)
4242
XT_CONNLABEL_OP_SET;
4343
struct xt_connlabel_mtinfo *info = par->matchinfo;
4444
int ret;
45-
size_t words;
46-
47-
if (info->bit > XT_CONNLABEL_MAXBIT)
48-
return -ERANGE;
4945

5046
if (info->options & ~options) {
5147
pr_err("Unknown options in mask %x\n", info->options);
@@ -59,19 +55,15 @@ static int connlabel_mt_check(const struct xt_mtchk_param *par)
5955
return ret;
6056
}
6157

62-
par->net->ct.labels_used++;
63-
words = BITS_TO_LONGS(info->bit+1);
64-
if (words > par->net->ct.label_words)
65-
par->net->ct.label_words = words;
66-
58+
ret = nf_connlabels_get(par->net, info->bit + 1);
59+
if (ret < 0)
60+
nf_ct_l3proto_module_put(par->family);
6761
return ret;
6862
}
6963

7064
static void connlabel_mt_destroy(const struct xt_mtdtor_param *par)
7165
{
72-
par->net->ct.labels_used--;
73-
if (par->net->ct.labels_used == 0)
74-
par->net->ct.label_words = 0;
66+
nf_connlabels_put(par->net);
7567
nf_ct_l3proto_module_put(par->family);
7668
}
7769

0 commit comments

Comments
 (0)