netlabel_addrlist.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. prefetch(iter->list.next), &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. prefetch(iter->list.next), &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. return;
  119. }
  120. #endif
  121. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  122. #define __af6list_entry(ptr) container_of(ptr, struct netlbl_af6list, list)
  123. static inline struct netlbl_af6list *__af6list_valid(struct list_head *s,
  124. struct list_head *h)
  125. {
  126. struct list_head *i = s;
  127. struct netlbl_af6list *n = __af6list_entry(s);
  128. while (i != h && !n->valid) {
  129. i = i->next;
  130. n = __af6list_entry(i);
  131. }
  132. return n;
  133. }
  134. static inline struct netlbl_af6list *__af6list_valid_rcu(struct list_head *s,
  135. struct list_head *h)
  136. {
  137. struct list_head *i = s;
  138. struct netlbl_af6list *n = __af6list_entry(s);
  139. while (i != h && !n->valid) {
  140. i = rcu_dereference(i->next);
  141. n = __af6list_entry(i);
  142. }
  143. return n;
  144. }
  145. #define netlbl_af6list_foreach(iter, head) \
  146. for (iter = __af6list_valid((head)->next, head); \
  147. prefetch(iter->list.next), &iter->list != (head); \
  148. iter = __af6list_valid(iter->list.next, head))
  149. #define netlbl_af6list_foreach_rcu(iter, head) \
  150. for (iter = __af6list_valid_rcu((head)->next, head); \
  151. prefetch(iter->list.next), &iter->list != (head); \
  152. iter = __af6list_valid_rcu(iter->list.next, head))
  153. #define netlbl_af6list_foreach_safe(iter, tmp, head) \
  154. for (iter = __af6list_valid((head)->next, head), \
  155. tmp = __af6list_valid(iter->list.next, head); \
  156. &iter->list != (head); \
  157. iter = tmp, tmp = __af6list_valid(iter->list.next, head))
  158. int netlbl_af6list_add(struct netlbl_af6list *entry,
  159. struct list_head *head);
  160. struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
  161. const struct in6_addr *mask,
  162. struct list_head *head);
  163. void netlbl_af6list_remove_entry(struct netlbl_af6list *entry);
  164. struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
  165. struct list_head *head);
  166. struct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
  167. const struct in6_addr *mask,
  168. struct list_head *head);
  169. #ifdef CONFIG_AUDIT
  170. void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  171. int src,
  172. const char *dev,
  173. const struct in6_addr *addr,
  174. const struct in6_addr *mask);
  175. #else
  176. static inline void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  177. int src,
  178. const char *dev,
  179. const struct in6_addr *addr,
  180. const struct in6_addr *mask)
  181. {
  182. return;
  183. }
  184. #endif
  185. #endif /* IPV6 */
  186. #endif