netlabel_addrlist.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.moore@hp.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, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #ifndef _NETLABEL_ADDRLIST_H
  31. #define _NETLABEL_ADDRLIST_H
  32. #include <linux/types.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/list.h>
  35. #include <linux/in6.h>
  36. #include <linux/audit.h>
  37. /**
  38. * struct netlbl_af4list - NetLabel IPv4 address list
  39. * @addr: IPv4 address
  40. * @mask: IPv4 address mask
  41. * @valid: valid flag
  42. * @list: list structure, used internally
  43. */
  44. struct netlbl_af4list {
  45. __be32 addr;
  46. __be32 mask;
  47. u32 valid;
  48. struct list_head list;
  49. };
  50. /**
  51. * struct netlbl_af6list - NetLabel IPv6 address list
  52. * @addr: IPv6 address
  53. * @mask: IPv6 address mask
  54. * @valid: valid flag
  55. * @list: list structure, used internally
  56. */
  57. struct netlbl_af6list {
  58. struct in6_addr addr;
  59. struct in6_addr mask;
  60. u32 valid;
  61. struct list_head list;
  62. };
  63. #define __af4list_entry(ptr) container_of(ptr, struct netlbl_af4list, list)
  64. static inline struct netlbl_af4list *__af4list_valid(struct list_head *s,
  65. struct list_head *h)
  66. {
  67. struct list_head *i = s;
  68. struct netlbl_af4list *n = __af4list_entry(s);
  69. while (i != h && !n->valid) {
  70. i = i->next;
  71. n = __af4list_entry(i);
  72. }
  73. return n;
  74. }
  75. static inline struct netlbl_af4list *__af4list_valid_rcu(struct list_head *s,
  76. struct list_head *h)
  77. {
  78. struct list_head *i = s;
  79. struct netlbl_af4list *n = __af4list_entry(s);
  80. while (i != h && !n->valid) {
  81. i = rcu_dereference(i->next);
  82. n = __af4list_entry(i);
  83. }
  84. return n;
  85. }
  86. #define netlbl_af4list_foreach(iter, head) \
  87. for (iter = __af4list_valid((head)->next, head); \
  88. &iter->list != (head); \
  89. iter = __af4list_valid(iter->list.next, head))
  90. #define netlbl_af4list_foreach_rcu(iter, head) \
  91. for (iter = __af4list_valid_rcu((head)->next, head); \
  92. &iter->list != (head); \
  93. iter = __af4list_valid_rcu(iter->list.next, head))
  94. #define netlbl_af4list_foreach_safe(iter, tmp, head) \
  95. for (iter = __af4list_valid((head)->next, head), \
  96. tmp = __af4list_valid(iter->list.next, head); \
  97. &iter->list != (head); \
  98. iter = tmp, tmp = __af4list_valid(iter->list.next, head))
  99. int netlbl_af4list_add(struct netlbl_af4list *entry,
  100. struct list_head *head);
  101. struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
  102. struct list_head *head);
  103. void netlbl_af4list_remove_entry(struct netlbl_af4list *entry);
  104. struct netlbl_af4list *netlbl_af4list_search(__be32 addr,
  105. struct list_head *head);
  106. struct netlbl_af4list *netlbl_af4list_search_exact(__be32 addr,
  107. __be32 mask,
  108. struct list_head *head);
  109. #ifdef CONFIG_AUDIT
  110. void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  111. int src, const char *dev,
  112. __be32 addr, __be32 mask);
  113. #else
  114. static inline void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  115. int src, const char *dev,
  116. __be32 addr, __be32 mask)
  117. {
  118. }
  119. #endif
  120. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  121. #define __af6list_entry(ptr) container_of(ptr, struct netlbl_af6list, list)
  122. static inline struct netlbl_af6list *__af6list_valid(struct list_head *s,
  123. struct list_head *h)
  124. {
  125. struct list_head *i = s;
  126. struct netlbl_af6list *n = __af6list_entry(s);
  127. while (i != h && !n->valid) {
  128. i = i->next;
  129. n = __af6list_entry(i);
  130. }
  131. return n;
  132. }
  133. static inline struct netlbl_af6list *__af6list_valid_rcu(struct list_head *s,
  134. struct list_head *h)
  135. {
  136. struct list_head *i = s;
  137. struct netlbl_af6list *n = __af6list_entry(s);
  138. while (i != h && !n->valid) {
  139. i = rcu_dereference(i->next);
  140. n = __af6list_entry(i);
  141. }
  142. return n;
  143. }
  144. #define netlbl_af6list_foreach(iter, head) \
  145. for (iter = __af6list_valid((head)->next, head); \
  146. &iter->list != (head); \
  147. iter = __af6list_valid(iter->list.next, head))
  148. #define netlbl_af6list_foreach_rcu(iter, head) \
  149. for (iter = __af6list_valid_rcu((head)->next, head); \
  150. &iter->list != (head); \
  151. iter = __af6list_valid_rcu(iter->list.next, head))
  152. #define netlbl_af6list_foreach_safe(iter, tmp, head) \
  153. for (iter = __af6list_valid((head)->next, head), \
  154. tmp = __af6list_valid(iter->list.next, head); \
  155. &iter->list != (head); \
  156. iter = tmp, tmp = __af6list_valid(iter->list.next, head))
  157. int netlbl_af6list_add(struct netlbl_af6list *entry,
  158. struct list_head *head);
  159. struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
  160. const struct in6_addr *mask,
  161. struct list_head *head);
  162. void netlbl_af6list_remove_entry(struct netlbl_af6list *entry);
  163. struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
  164. struct list_head *head);
  165. struct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
  166. const struct in6_addr *mask,
  167. struct list_head *head);
  168. #ifdef CONFIG_AUDIT
  169. void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  170. int src,
  171. const char *dev,
  172. const struct in6_addr *addr,
  173. const struct in6_addr *mask);
  174. #else
  175. static inline void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  176. int src,
  177. const char *dev,
  178. const struct in6_addr *addr,
  179. const struct in6_addr *mask)
  180. {
  181. }
  182. #endif
  183. #endif /* IPV6 */
  184. #endif