netlabel_addrlist.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * NetLabel Network Address Lists
  4. *
  5. * This file contains network address list functions used to manage ordered
  6. * lists of network addresses for use by the NetLabel subsystem. The NetLabel
  7. * system manages static and dynamic label mappings for network protocols such
  8. * as CIPSO and RIPSO.
  9. *
  10. * Author: Paul Moore <paul@paul-moore.com>
  11. */
  12. /*
  13. * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
  14. */
  15. #ifndef _NETLABEL_ADDRLIST_H
  16. #define _NETLABEL_ADDRLIST_H
  17. #include <linux/types.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/list.h>
  20. #include <linux/in6.h>
  21. #include <linux/audit.h>
  22. /**
  23. * struct netlbl_af4list - NetLabel IPv4 address list
  24. * @addr: IPv4 address
  25. * @mask: IPv4 address mask
  26. * @valid: valid flag
  27. * @list: list structure, used internally
  28. */
  29. struct netlbl_af4list {
  30. __be32 addr;
  31. __be32 mask;
  32. u32 valid;
  33. struct list_head list;
  34. };
  35. /**
  36. * struct netlbl_af6list - NetLabel IPv6 address list
  37. * @addr: IPv6 address
  38. * @mask: IPv6 address mask
  39. * @valid: valid flag
  40. * @list: list structure, used internally
  41. */
  42. struct netlbl_af6list {
  43. struct in6_addr addr;
  44. struct in6_addr mask;
  45. u32 valid;
  46. struct list_head list;
  47. };
  48. #define __af4list_entry(ptr) container_of(ptr, struct netlbl_af4list, list)
  49. static inline struct netlbl_af4list *__af4list_valid(struct list_head *s,
  50. struct list_head *h)
  51. {
  52. struct list_head *i = s;
  53. struct netlbl_af4list *n = __af4list_entry(s);
  54. while (i != h && !n->valid) {
  55. i = i->next;
  56. n = __af4list_entry(i);
  57. }
  58. return n;
  59. }
  60. static inline struct netlbl_af4list *__af4list_valid_rcu(struct list_head *s,
  61. struct list_head *h)
  62. {
  63. struct list_head *i = s;
  64. struct netlbl_af4list *n = __af4list_entry(s);
  65. while (i != h && !n->valid) {
  66. i = rcu_dereference(list_next_rcu(i));
  67. n = __af4list_entry(i);
  68. }
  69. return n;
  70. }
  71. #define netlbl_af4list_foreach(iter, head) \
  72. for (iter = __af4list_valid((head)->next, head); \
  73. &iter->list != (head); \
  74. iter = __af4list_valid(iter->list.next, head))
  75. #define netlbl_af4list_foreach_rcu(iter, head) \
  76. for (iter = __af4list_valid_rcu((head)->next, head); \
  77. &iter->list != (head); \
  78. iter = __af4list_valid_rcu(iter->list.next, head))
  79. #define netlbl_af4list_foreach_safe(iter, tmp, head) \
  80. for (iter = __af4list_valid((head)->next, head), \
  81. tmp = __af4list_valid(iter->list.next, head); \
  82. &iter->list != (head); \
  83. iter = tmp, tmp = __af4list_valid(iter->list.next, head))
  84. int netlbl_af4list_add(struct netlbl_af4list *entry,
  85. struct list_head *head);
  86. struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
  87. struct list_head *head);
  88. void netlbl_af4list_remove_entry(struct netlbl_af4list *entry);
  89. struct netlbl_af4list *netlbl_af4list_search(__be32 addr,
  90. struct list_head *head);
  91. struct netlbl_af4list *netlbl_af4list_search_exact(__be32 addr,
  92. __be32 mask,
  93. struct list_head *head);
  94. #ifdef CONFIG_AUDIT
  95. void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  96. int src, const char *dev,
  97. __be32 addr, __be32 mask);
  98. #else
  99. static inline void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  100. int src, const char *dev,
  101. __be32 addr, __be32 mask)
  102. {
  103. }
  104. #endif
  105. #if IS_ENABLED(CONFIG_IPV6)
  106. #define __af6list_entry(ptr) container_of(ptr, struct netlbl_af6list, list)
  107. static inline struct netlbl_af6list *__af6list_valid(struct list_head *s,
  108. struct list_head *h)
  109. {
  110. struct list_head *i = s;
  111. struct netlbl_af6list *n = __af6list_entry(s);
  112. while (i != h && !n->valid) {
  113. i = i->next;
  114. n = __af6list_entry(i);
  115. }
  116. return n;
  117. }
  118. static inline struct netlbl_af6list *__af6list_valid_rcu(struct list_head *s,
  119. struct list_head *h)
  120. {
  121. struct list_head *i = s;
  122. struct netlbl_af6list *n = __af6list_entry(s);
  123. while (i != h && !n->valid) {
  124. i = rcu_dereference(list_next_rcu(i));
  125. n = __af6list_entry(i);
  126. }
  127. return n;
  128. }
  129. #define netlbl_af6list_foreach(iter, head) \
  130. for (iter = __af6list_valid((head)->next, head); \
  131. &iter->list != (head); \
  132. iter = __af6list_valid(iter->list.next, head))
  133. #define netlbl_af6list_foreach_rcu(iter, head) \
  134. for (iter = __af6list_valid_rcu((head)->next, head); \
  135. &iter->list != (head); \
  136. iter = __af6list_valid_rcu(iter->list.next, head))
  137. #define netlbl_af6list_foreach_safe(iter, tmp, head) \
  138. for (iter = __af6list_valid((head)->next, head), \
  139. tmp = __af6list_valid(iter->list.next, head); \
  140. &iter->list != (head); \
  141. iter = tmp, tmp = __af6list_valid(iter->list.next, head))
  142. int netlbl_af6list_add(struct netlbl_af6list *entry,
  143. struct list_head *head);
  144. struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
  145. const struct in6_addr *mask,
  146. struct list_head *head);
  147. void netlbl_af6list_remove_entry(struct netlbl_af6list *entry);
  148. struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
  149. struct list_head *head);
  150. struct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
  151. const struct in6_addr *mask,
  152. struct list_head *head);
  153. #ifdef CONFIG_AUDIT
  154. void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  155. int src,
  156. const char *dev,
  157. const struct in6_addr *addr,
  158. const struct in6_addr *mask);
  159. #else
  160. static inline void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  161. int src,
  162. const char *dev,
  163. const struct in6_addr *addr,
  164. const struct in6_addr *mask)
  165. {
  166. }
  167. #endif
  168. #endif /* IPV6 */
  169. #endif