netlabel_addrlist.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * NetLabel Network Address Lists
  3. *
  4. * This file contains network address list functions used to manage ordered
  5. * lists of network addresses for use by the NetLabel subsystem. The NetLabel
  6. * system manages static and dynamic label mappings for network protocols such
  7. * as CIPSO and RIPSO.
  8. *
  9. * Author: Paul Moore <paul@paul-moore.com>
  10. *
  11. */
  12. /*
  13. * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  23. * the GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. #ifndef _NETLABEL_ADDRLIST_H
  30. #define _NETLABEL_ADDRLIST_H
  31. #include <linux/types.h>
  32. #include <linux/rcupdate.h>
  33. #include <linux/list.h>
  34. #include <linux/in6.h>
  35. #include <linux/audit.h>
  36. /**
  37. * struct netlbl_af4list - NetLabel IPv4 address list
  38. * @addr: IPv4 address
  39. * @mask: IPv4 address mask
  40. * @valid: valid flag
  41. * @list: list structure, used internally
  42. */
  43. struct netlbl_af4list {
  44. __be32 addr;
  45. __be32 mask;
  46. u32 valid;
  47. struct list_head list;
  48. };
  49. /**
  50. * struct netlbl_af6list - NetLabel IPv6 address list
  51. * @addr: IPv6 address
  52. * @mask: IPv6 address mask
  53. * @valid: valid flag
  54. * @list: list structure, used internally
  55. */
  56. struct netlbl_af6list {
  57. struct in6_addr addr;
  58. struct in6_addr mask;
  59. u32 valid;
  60. struct list_head list;
  61. };
  62. #define __af4list_entry(ptr) container_of(ptr, struct netlbl_af4list, list)
  63. static inline struct netlbl_af4list *__af4list_valid(struct list_head *s,
  64. struct list_head *h)
  65. {
  66. struct list_head *i = s;
  67. struct netlbl_af4list *n = __af4list_entry(s);
  68. while (i != h && !n->valid) {
  69. i = i->next;
  70. n = __af4list_entry(i);
  71. }
  72. return n;
  73. }
  74. static inline struct netlbl_af4list *__af4list_valid_rcu(struct list_head *s,
  75. struct list_head *h)
  76. {
  77. struct list_head *i = s;
  78. struct netlbl_af4list *n = __af4list_entry(s);
  79. while (i != h && !n->valid) {
  80. i = rcu_dereference(i->next);
  81. n = __af4list_entry(i);
  82. }
  83. return n;
  84. }
  85. #define netlbl_af4list_foreach(iter, head) \
  86. for (iter = __af4list_valid((head)->next, head); \
  87. &iter->list != (head); \
  88. iter = __af4list_valid(iter->list.next, head))
  89. #define netlbl_af4list_foreach_rcu(iter, head) \
  90. for (iter = __af4list_valid_rcu((head)->next, head); \
  91. &iter->list != (head); \
  92. iter = __af4list_valid_rcu(iter->list.next, head))
  93. #define netlbl_af4list_foreach_safe(iter, tmp, head) \
  94. for (iter = __af4list_valid((head)->next, head), \
  95. tmp = __af4list_valid(iter->list.next, head); \
  96. &iter->list != (head); \
  97. iter = tmp, tmp = __af4list_valid(iter->list.next, head))
  98. int netlbl_af4list_add(struct netlbl_af4list *entry,
  99. struct list_head *head);
  100. struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
  101. struct list_head *head);
  102. void netlbl_af4list_remove_entry(struct netlbl_af4list *entry);
  103. struct netlbl_af4list *netlbl_af4list_search(__be32 addr,
  104. struct list_head *head);
  105. struct netlbl_af4list *netlbl_af4list_search_exact(__be32 addr,
  106. __be32 mask,
  107. struct list_head *head);
  108. #ifdef CONFIG_AUDIT
  109. void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  110. int src, const char *dev,
  111. __be32 addr, __be32 mask);
  112. #else
  113. static inline void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  114. int src, const char *dev,
  115. __be32 addr, __be32 mask)
  116. {
  117. }
  118. #endif
  119. #if IS_ENABLED(CONFIG_IPV6)
  120. #define __af6list_entry(ptr) container_of(ptr, struct netlbl_af6list, list)
  121. static inline struct netlbl_af6list *__af6list_valid(struct list_head *s,
  122. struct list_head *h)
  123. {
  124. struct list_head *i = s;
  125. struct netlbl_af6list *n = __af6list_entry(s);
  126. while (i != h && !n->valid) {
  127. i = i->next;
  128. n = __af6list_entry(i);
  129. }
  130. return n;
  131. }
  132. static inline struct netlbl_af6list *__af6list_valid_rcu(struct list_head *s,
  133. struct list_head *h)
  134. {
  135. struct list_head *i = s;
  136. struct netlbl_af6list *n = __af6list_entry(s);
  137. while (i != h && !n->valid) {
  138. i = rcu_dereference(i->next);
  139. n = __af6list_entry(i);
  140. }
  141. return n;
  142. }
  143. #define netlbl_af6list_foreach(iter, head) \
  144. for (iter = __af6list_valid((head)->next, head); \
  145. &iter->list != (head); \
  146. iter = __af6list_valid(iter->list.next, head))
  147. #define netlbl_af6list_foreach_rcu(iter, head) \
  148. for (iter = __af6list_valid_rcu((head)->next, head); \
  149. &iter->list != (head); \
  150. iter = __af6list_valid_rcu(iter->list.next, head))
  151. #define netlbl_af6list_foreach_safe(iter, tmp, head) \
  152. for (iter = __af6list_valid((head)->next, head), \
  153. tmp = __af6list_valid(iter->list.next, head); \
  154. &iter->list != (head); \
  155. iter = tmp, tmp = __af6list_valid(iter->list.next, head))
  156. int netlbl_af6list_add(struct netlbl_af6list *entry,
  157. struct list_head *head);
  158. struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
  159. const struct in6_addr *mask,
  160. struct list_head *head);
  161. void netlbl_af6list_remove_entry(struct netlbl_af6list *entry);
  162. struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
  163. struct list_head *head);
  164. struct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
  165. const struct in6_addr *mask,
  166. struct list_head *head);
  167. #ifdef CONFIG_AUDIT
  168. void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  169. int src,
  170. const char *dev,
  171. const struct in6_addr *addr,
  172. const struct in6_addr *mask);
  173. #else
  174. static inline void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  175. int src,
  176. const char *dev,
  177. const struct in6_addr *addr,
  178. const struct in6_addr *mask)
  179. {
  180. }
  181. #endif
  182. #endif /* IPV6 */
  183. #endif