netlabel_addrlist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include <linux/types.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/list.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/in.h>
  35. #include <linux/in6.h>
  36. #include <linux/ip.h>
  37. #include <linux/ipv6.h>
  38. #include <net/ip.h>
  39. #include <net/ipv6.h>
  40. #include <linux/audit.h>
  41. #include "netlabel_addrlist.h"
  42. /*
  43. * Address List Functions
  44. */
  45. /**
  46. * netlbl_af4list_search - Search for a matching IPv4 address entry
  47. * @addr: IPv4 address
  48. * @head: the list head
  49. *
  50. * Description:
  51. * Searches the IPv4 address list given by @head. If a matching address entry
  52. * is found it is returned, otherwise NULL is returned. The caller is
  53. * responsible for calling the rcu_read_[un]lock() functions.
  54. *
  55. */
  56. struct netlbl_af4list *netlbl_af4list_search(__be32 addr,
  57. struct list_head *head)
  58. {
  59. struct netlbl_af4list *iter;
  60. list_for_each_entry_rcu(iter, head, list)
  61. if (iter->valid && (addr & iter->mask) == iter->addr)
  62. return iter;
  63. return NULL;
  64. }
  65. /**
  66. * netlbl_af4list_search_exact - Search for an exact IPv4 address entry
  67. * @addr: IPv4 address
  68. * @mask: IPv4 address mask
  69. * @head: the list head
  70. *
  71. * Description:
  72. * Searches the IPv4 address list given by @head. If an exact match if found
  73. * it is returned, otherwise NULL is returned. The caller is responsible for
  74. * calling the rcu_read_[un]lock() functions.
  75. *
  76. */
  77. struct netlbl_af4list *netlbl_af4list_search_exact(__be32 addr,
  78. __be32 mask,
  79. struct list_head *head)
  80. {
  81. struct netlbl_af4list *iter;
  82. list_for_each_entry_rcu(iter, head, list)
  83. if (iter->valid && iter->addr == addr && iter->mask == mask)
  84. return iter;
  85. return NULL;
  86. }
  87. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  88. /**
  89. * netlbl_af6list_search - Search for a matching IPv6 address entry
  90. * @addr: IPv6 address
  91. * @head: the list head
  92. *
  93. * Description:
  94. * Searches the IPv6 address list given by @head. If a matching address entry
  95. * is found it is returned, otherwise NULL is returned. The caller is
  96. * responsible for calling the rcu_read_[un]lock() functions.
  97. *
  98. */
  99. struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
  100. struct list_head *head)
  101. {
  102. struct netlbl_af6list *iter;
  103. list_for_each_entry_rcu(iter, head, list)
  104. if (iter->valid &&
  105. ipv6_masked_addr_cmp(&iter->addr, &iter->mask, addr) == 0)
  106. return iter;
  107. return NULL;
  108. }
  109. /**
  110. * netlbl_af6list_search_exact - Search for an exact IPv6 address entry
  111. * @addr: IPv6 address
  112. * @mask: IPv6 address mask
  113. * @head: the list head
  114. *
  115. * Description:
  116. * Searches the IPv6 address list given by @head. If an exact match if found
  117. * it is returned, otherwise NULL is returned. The caller is responsible for
  118. * calling the rcu_read_[un]lock() functions.
  119. *
  120. */
  121. struct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
  122. const struct in6_addr *mask,
  123. struct list_head *head)
  124. {
  125. struct netlbl_af6list *iter;
  126. list_for_each_entry_rcu(iter, head, list)
  127. if (iter->valid &&
  128. ipv6_addr_equal(&iter->addr, addr) &&
  129. ipv6_addr_equal(&iter->mask, mask))
  130. return iter;
  131. return NULL;
  132. }
  133. #endif /* IPv6 */
  134. /**
  135. * netlbl_af4list_add - Add a new IPv4 address entry to a list
  136. * @entry: address entry
  137. * @head: the list head
  138. *
  139. * Description:
  140. * Add a new address entry to the list pointed to by @head. On success zero is
  141. * returned, otherwise a negative value is returned. The caller is responsible
  142. * for calling the necessary locking functions.
  143. *
  144. */
  145. int netlbl_af4list_add(struct netlbl_af4list *entry, struct list_head *head)
  146. {
  147. struct netlbl_af4list *iter;
  148. iter = netlbl_af4list_search(entry->addr, head);
  149. if (iter != NULL &&
  150. iter->addr == entry->addr && iter->mask == entry->mask)
  151. return -EEXIST;
  152. /* in order to speed up address searches through the list (the common
  153. * case) we need to keep the list in order based on the size of the
  154. * address mask such that the entry with the widest mask (smallest
  155. * numerical value) appears first in the list */
  156. list_for_each_entry_rcu(iter, head, list)
  157. if (iter->valid &&
  158. ntohl(entry->mask) > ntohl(iter->mask)) {
  159. __list_add_rcu(&entry->list,
  160. iter->list.prev,
  161. &iter->list);
  162. return 0;
  163. }
  164. list_add_tail_rcu(&entry->list, head);
  165. return 0;
  166. }
  167. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  168. /**
  169. * netlbl_af6list_add - Add a new IPv6 address entry to a list
  170. * @entry: address entry
  171. * @head: the list head
  172. *
  173. * Description:
  174. * Add a new address entry to the list pointed to by @head. On success zero is
  175. * returned, otherwise a negative value is returned. The caller is responsible
  176. * for calling the necessary locking functions.
  177. *
  178. */
  179. int netlbl_af6list_add(struct netlbl_af6list *entry, struct list_head *head)
  180. {
  181. struct netlbl_af6list *iter;
  182. iter = netlbl_af6list_search(&entry->addr, head);
  183. if (iter != NULL &&
  184. ipv6_addr_equal(&iter->addr, &entry->addr) &&
  185. ipv6_addr_equal(&iter->mask, &entry->mask))
  186. return -EEXIST;
  187. /* in order to speed up address searches through the list (the common
  188. * case) we need to keep the list in order based on the size of the
  189. * address mask such that the entry with the widest mask (smallest
  190. * numerical value) appears first in the list */
  191. list_for_each_entry_rcu(iter, head, list)
  192. if (iter->valid &&
  193. ipv6_addr_cmp(&entry->mask, &iter->mask) > 0) {
  194. __list_add_rcu(&entry->list,
  195. iter->list.prev,
  196. &iter->list);
  197. return 0;
  198. }
  199. list_add_tail_rcu(&entry->list, head);
  200. return 0;
  201. }
  202. #endif /* IPv6 */
  203. /**
  204. * netlbl_af4list_remove_entry - Remove an IPv4 address entry
  205. * @entry: address entry
  206. *
  207. * Description:
  208. * Remove the specified IP address entry. The caller is responsible for
  209. * calling the necessary locking functions.
  210. *
  211. */
  212. void netlbl_af4list_remove_entry(struct netlbl_af4list *entry)
  213. {
  214. entry->valid = 0;
  215. list_del_rcu(&entry->list);
  216. }
  217. /**
  218. * netlbl_af4list_remove - Remove an IPv4 address entry
  219. * @addr: IP address
  220. * @mask: IP address mask
  221. * @head: the list head
  222. *
  223. * Description:
  224. * Remove an IP address entry from the list pointed to by @head. Returns the
  225. * entry on success, NULL on failure. The caller is responsible for calling
  226. * the necessary locking functions.
  227. *
  228. */
  229. struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
  230. struct list_head *head)
  231. {
  232. struct netlbl_af4list *entry;
  233. entry = netlbl_af4list_search(addr, head);
  234. if (entry != NULL && entry->addr == addr && entry->mask == mask) {
  235. netlbl_af4list_remove_entry(entry);
  236. return entry;
  237. }
  238. return NULL;
  239. }
  240. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  241. /**
  242. * netlbl_af6list_remove_entry - Remove an IPv6 address entry
  243. * @entry: address entry
  244. *
  245. * Description:
  246. * Remove the specified IP address entry. The caller is responsible for
  247. * calling the necessary locking functions.
  248. *
  249. */
  250. void netlbl_af6list_remove_entry(struct netlbl_af6list *entry)
  251. {
  252. entry->valid = 0;
  253. list_del_rcu(&entry->list);
  254. }
  255. /**
  256. * netlbl_af6list_remove - Remove an IPv6 address entry
  257. * @addr: IP address
  258. * @mask: IP address mask
  259. * @head: the list head
  260. *
  261. * Description:
  262. * Remove an IP address entry from the list pointed to by @head. Returns the
  263. * entry on success, NULL on failure. The caller is responsible for calling
  264. * the necessary locking functions.
  265. *
  266. */
  267. struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
  268. const struct in6_addr *mask,
  269. struct list_head *head)
  270. {
  271. struct netlbl_af6list *entry;
  272. entry = netlbl_af6list_search(addr, head);
  273. if (entry != NULL &&
  274. ipv6_addr_equal(&entry->addr, addr) &&
  275. ipv6_addr_equal(&entry->mask, mask)) {
  276. netlbl_af6list_remove_entry(entry);
  277. return entry;
  278. }
  279. return NULL;
  280. }
  281. #endif /* IPv6 */
  282. /*
  283. * Audit Helper Functions
  284. */
  285. #ifdef CONFIG_AUDIT
  286. /**
  287. * netlbl_af4list_audit_addr - Audit an IPv4 address
  288. * @audit_buf: audit buffer
  289. * @src: true if source address, false if destination
  290. * @dev: network interface
  291. * @addr: IP address
  292. * @mask: IP address mask
  293. *
  294. * Description:
  295. * Write the IPv4 address and address mask, if necessary, to @audit_buf.
  296. *
  297. */
  298. void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  299. int src, const char *dev,
  300. __be32 addr, __be32 mask)
  301. {
  302. u32 mask_val = ntohl(mask);
  303. char *dir = (src ? "src" : "dst");
  304. if (dev != NULL)
  305. audit_log_format(audit_buf, " netif=%s", dev);
  306. audit_log_format(audit_buf, " %s=" NIPQUAD_FMT, dir, NIPQUAD(addr));
  307. if (mask_val != 0xffffffff) {
  308. u32 mask_len = 0;
  309. while (mask_val > 0) {
  310. mask_val <<= 1;
  311. mask_len++;
  312. }
  313. audit_log_format(audit_buf, " %s_prefixlen=%d", dir, mask_len);
  314. }
  315. }
  316. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  317. /**
  318. * netlbl_af6list_audit_addr - Audit an IPv6 address
  319. * @audit_buf: audit buffer
  320. * @src: true if source address, false if destination
  321. * @dev: network interface
  322. * @addr: IP address
  323. * @mask: IP address mask
  324. *
  325. * Description:
  326. * Write the IPv6 address and address mask, if necessary, to @audit_buf.
  327. *
  328. */
  329. void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  330. int src,
  331. const char *dev,
  332. const struct in6_addr *addr,
  333. const struct in6_addr *mask)
  334. {
  335. char *dir = (src ? "src" : "dst");
  336. if (dev != NULL)
  337. audit_log_format(audit_buf, " netif=%s", dev);
  338. audit_log_format(audit_buf, " %s=" NIP6_FMT, dir, NIP6(*addr));
  339. if (ntohl(mask->s6_addr32[3]) != 0xffffffff) {
  340. u32 mask_len = 0;
  341. u32 mask_val;
  342. int iter = -1;
  343. while (ntohl(mask->s6_addr32[++iter]) == 0xffffffff)
  344. mask_len += 32;
  345. mask_val = ntohl(mask->s6_addr32[iter]);
  346. while (mask_val > 0) {
  347. mask_val <<= 1;
  348. mask_len++;
  349. }
  350. audit_log_format(audit_buf, " %s_prefixlen=%d", dir, mask_len);
  351. }
  352. }
  353. #endif /* IPv6 */
  354. #endif /* CONFIG_AUDIT */