iwlwifi: mvm: fix a few firmware capability checks
[cascardo/linux.git] / net / netfilter / nf_conntrack_labels.c
index 3ce5c31..252e6a7 100644 (file)
 
 static spinlock_t nf_connlabels_lock;
 
-static unsigned int label_bits(const struct nf_conn_labels *l)
-{
-       unsigned int longs = l->words;
-       return longs * BITS_PER_LONG;
-}
-
-bool nf_connlabel_match(const struct nf_conn *ct, u16 bit)
-{
-       struct nf_conn_labels *labels = nf_ct_labels_find(ct);
-
-       if (!labels)
-               return false;
-
-       return bit < label_bits(labels) && test_bit(bit, labels->bits);
-}
-EXPORT_SYMBOL_GPL(nf_connlabel_match);
-
 int nf_connlabel_set(struct nf_conn *ct, u16 bit)
 {
        struct nf_conn_labels *labels = nf_ct_labels_find(ct);
 
-       if (!labels || bit >= label_bits(labels))
+       if (!labels || BIT_WORD(bit) >= labels->words)
                return -ENOSPC;
 
        if (test_bit(bit, labels->bits))
@@ -50,14 +33,18 @@ int nf_connlabel_set(struct nf_conn *ct, u16 bit)
 }
 EXPORT_SYMBOL_GPL(nf_connlabel_set);
 
-static void replace_u32(u32 *address, u32 mask, u32 new)
+static int replace_u32(u32 *address, u32 mask, u32 new)
 {
        u32 old, tmp;
 
        do {
                old = *address;
                tmp = (old & mask) ^ new;
+               if (old == tmp)
+                       return 0;
        } while (cmpxchg(address, old, tmp) != old);
+
+       return 1;
 }
 
 int nf_connlabels_replace(struct nf_conn *ct,
@@ -66,6 +53,7 @@ int nf_connlabels_replace(struct nf_conn *ct,
 {
        struct nf_conn_labels *labels;
        unsigned int size, i;
+       int changed = 0;
        u32 *dst;
 
        labels = nf_ct_labels_find(ct);
@@ -77,29 +65,27 @@ int nf_connlabels_replace(struct nf_conn *ct,
                words32 = size / sizeof(u32);
 
        dst = (u32 *) labels->bits;
-       if (words32) {
-               for (i = 0; i < words32; i++)
-                       replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
-       }
+       for (i = 0; i < words32; i++)
+               changed |= replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
 
        size /= sizeof(u32);
        for (i = words32; i < size; i++) /* pad */
                replace_u32(&dst[i], 0, 0);
 
-       nf_conntrack_event_cache(IPCT_LABEL, ct);
+       if (changed)
+               nf_conntrack_event_cache(IPCT_LABEL, ct);
        return 0;
 }
 EXPORT_SYMBOL_GPL(nf_connlabels_replace);
 
-int nf_connlabels_get(struct net *net, unsigned int n_bits)
+int nf_connlabels_get(struct net *net, unsigned int bits)
 {
        size_t words;
 
-       if (n_bits > (NF_CT_LABELS_MAX_SIZE * BITS_PER_BYTE))
+       words = BIT_WORD(bits) + 1;
+       if (words > NF_CT_LABELS_MAX_SIZE / sizeof(long))
                return -ERANGE;
 
-       words = BITS_TO_LONGS(n_bits);
-
        spin_lock(&nf_connlabels_lock);
        net->ct.labels_used++;
        if (words > net->ct.label_words)
@@ -128,6 +114,8 @@ static struct nf_ct_ext_type labels_extend __read_mostly = {
 
 int nf_conntrack_labels_init(void)
 {
+       BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX);
+
        spin_lock_init(&nf_connlabels_lock);
        return nf_ct_extend_register(&labels_extend);
 }