nf_conntrack_labels.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  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. #include <linux/export.h>
  11. #include <linux/types.h>
  12. #include <net/netfilter/nf_conntrack_ecache.h>
  13. #include <net/netfilter/nf_conntrack_labels.h>
  14. static spinlock_t nf_connlabels_lock;
  15. static unsigned int label_bits(const struct nf_conn_labels *l)
  16. {
  17. unsigned int longs = l->words;
  18. return longs * BITS_PER_LONG;
  19. }
  20. bool nf_connlabel_match(const struct nf_conn *ct, u16 bit)
  21. {
  22. struct nf_conn_labels *labels = nf_ct_labels_find(ct);
  23. if (!labels)
  24. return false;
  25. return bit < label_bits(labels) && test_bit(bit, labels->bits);
  26. }
  27. EXPORT_SYMBOL_GPL(nf_connlabel_match);
  28. int nf_connlabel_set(struct nf_conn *ct, u16 bit)
  29. {
  30. struct nf_conn_labels *labels = nf_ct_labels_find(ct);
  31. if (!labels || bit >= label_bits(labels))
  32. return -ENOSPC;
  33. if (test_bit(bit, labels->bits))
  34. return 0;
  35. if (!test_and_set_bit(bit, labels->bits))
  36. nf_conntrack_event_cache(IPCT_LABEL, ct);
  37. return 0;
  38. }
  39. EXPORT_SYMBOL_GPL(nf_connlabel_set);
  40. static void replace_u32(u32 *address, u32 mask, u32 new)
  41. {
  42. u32 old, tmp;
  43. do {
  44. old = *address;
  45. tmp = (old & mask) ^ new;
  46. } while (cmpxchg(address, old, tmp) != old);
  47. }
  48. int nf_connlabels_replace(struct nf_conn *ct,
  49. const u32 *data,
  50. const u32 *mask, unsigned int words32)
  51. {
  52. struct nf_conn_labels *labels;
  53. unsigned int size, i;
  54. u32 *dst;
  55. labels = nf_ct_labels_find(ct);
  56. if (!labels)
  57. return -ENOSPC;
  58. size = labels->words * sizeof(long);
  59. if (size < (words32 * sizeof(u32)))
  60. words32 = size / sizeof(u32);
  61. dst = (u32 *) labels->bits;
  62. if (words32) {
  63. for (i = 0; i < words32; i++)
  64. replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]);
  65. }
  66. size /= sizeof(u32);
  67. for (i = words32; i < size; i++) /* pad */
  68. replace_u32(&dst[i], 0, 0);
  69. nf_conntrack_event_cache(IPCT_LABEL, ct);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(nf_connlabels_replace);
  73. int nf_connlabels_get(struct net *net, unsigned int n_bits)
  74. {
  75. size_t words;
  76. if (n_bits > (NF_CT_LABELS_MAX_SIZE * BITS_PER_BYTE))
  77. return -ERANGE;
  78. words = BITS_TO_LONGS(n_bits);
  79. spin_lock(&nf_connlabels_lock);
  80. net->ct.labels_used++;
  81. if (words > net->ct.label_words)
  82. net->ct.label_words = words;
  83. spin_unlock(&nf_connlabels_lock);
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_GPL(nf_connlabels_get);
  87. void nf_connlabels_put(struct net *net)
  88. {
  89. spin_lock(&nf_connlabels_lock);
  90. net->ct.labels_used--;
  91. if (net->ct.labels_used == 0)
  92. net->ct.label_words = 0;
  93. spin_unlock(&nf_connlabels_lock);
  94. }
  95. EXPORT_SYMBOL_GPL(nf_connlabels_put);
  96. static struct nf_ct_ext_type labels_extend __read_mostly = {
  97. .len = sizeof(struct nf_conn_labels),
  98. .align = __alignof__(struct nf_conn_labels),
  99. .id = NF_CT_EXT_LABELS,
  100. };
  101. int nf_conntrack_labels_init(void)
  102. {
  103. spin_lock_init(&nf_connlabels_lock);
  104. return nf_ct_extend_register(&labels_extend);
  105. }
  106. void nf_conntrack_labels_fini(void)
  107. {
  108. nf_ct_extend_unregister(&labels_extend);
  109. }