~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_conntrack_labels.c

Version: ~ [ linux-5.18 ] ~ [ linux-5.17.9 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.41 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.117 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.195 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.244 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.280 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.315 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * test/set flag bits stored in conntrack extension area.
  4  *
  5  * (C) 2013 Astaro GmbH & Co KG
  6  */
  7 
  8 #include <linux/export.h>
  9 #include <linux/types.h>
 10 
 11 #include <net/netfilter/nf_conntrack_ecache.h>
 12 #include <net/netfilter/nf_conntrack_labels.h>
 13 
 14 static spinlock_t nf_connlabels_lock;
 15 
 16 static int replace_u32(u32 *address, u32 mask, u32 new)
 17 {
 18         u32 old, tmp;
 19 
 20         do {
 21                 old = *address;
 22                 tmp = (old & mask) ^ new;
 23                 if (old == tmp)
 24                         return 0;
 25         } while (cmpxchg(address, old, tmp) != old);
 26 
 27         return 1;
 28 }
 29 
 30 int nf_connlabels_replace(struct nf_conn *ct,
 31                           const u32 *data,
 32                           const u32 *mask, unsigned int words32)
 33 {
 34         struct nf_conn_labels *labels;
 35         unsigned int size, i;
 36         int changed = 0;
 37         u32 *dst;
 38 
 39         labels = nf_ct_labels_find(ct);
 40         if (!labels)
 41                 return -ENOSPC;
 42 
 43         size = sizeof(labels->bits);
 44         if (size < (words32 * sizeof(u32)))
 45                 words32 = size / sizeof(u32);
 46 
 47         dst = (u32 *) labels->bits;
 48         for (i = 0; i < words32; i++)
 49                 changed |= replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
 50 
 51         size /= sizeof(u32);
 52         for (i = words32; i < size; i++) /* pad */
 53                 replace_u32(&dst[i], 0, 0);
 54 
 55         if (changed)
 56                 nf_conntrack_event_cache(IPCT_LABEL, ct);
 57         return 0;
 58 }
 59 EXPORT_SYMBOL_GPL(nf_connlabels_replace);
 60 
 61 int nf_connlabels_get(struct net *net, unsigned int bits)
 62 {
 63         if (BIT_WORD(bits) >= NF_CT_LABELS_MAX_SIZE / sizeof(long))
 64                 return -ERANGE;
 65 
 66         spin_lock(&nf_connlabels_lock);
 67         net->ct.labels_used++;
 68         spin_unlock(&nf_connlabels_lock);
 69 
 70         return 0;
 71 }
 72 EXPORT_SYMBOL_GPL(nf_connlabels_get);
 73 
 74 void nf_connlabels_put(struct net *net)
 75 {
 76         spin_lock(&nf_connlabels_lock);
 77         net->ct.labels_used--;
 78         spin_unlock(&nf_connlabels_lock);
 79 }
 80 EXPORT_SYMBOL_GPL(nf_connlabels_put);
 81 
 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,
 86 };
 87 
 88 int nf_conntrack_labels_init(void)
 89 {
 90         BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX);
 91 
 92         spin_lock_init(&nf_connlabels_lock);
 93         return nf_ct_extend_register(&labels_extend);
 94 }
 95 
 96 void nf_conntrack_labels_fini(void)
 97 {
 98         nf_ct_extend_unregister(&labels_extend);
 99 }
100 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp