netlabel_addrlist.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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, 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 IS_ENABLED(CONFIG_IPV6)
  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 IS_ENABLED(CONFIG_IPV6)
  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_exact(addr, mask, head);
  234. if (entry == NULL)
  235. return NULL;
  236. netlbl_af4list_remove_entry(entry);
  237. return entry;
  238. }
  239. #if IS_ENABLED(CONFIG_IPV6)
  240. /**
  241. * netlbl_af6list_remove_entry - Remove an IPv6 address entry
  242. * @entry: address entry
  243. *
  244. * Description:
  245. * Remove the specified IP address entry. The caller is responsible for
  246. * calling the necessary locking functions.
  247. *
  248. */
  249. void netlbl_af6list_remove_entry(struct netlbl_af6list *entry)
  250. {
  251. entry->valid = 0;
  252. list_del_rcu(&entry->list);
  253. }
  254. /**
  255. * netlbl_af6list_remove - Remove an IPv6 address entry
  256. * @addr: IP address
  257. * @mask: IP address mask
  258. * @head: the list head
  259. *
  260. * Description:
  261. * Remove an IP address entry from the list pointed to by @head. Returns the
  262. * entry on success, NULL on failure. The caller is responsible for calling
  263. * the necessary locking functions.
  264. *
  265. */
  266. struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
  267. const struct in6_addr *mask,
  268. struct list_head *head)
  269. {
  270. struct netlbl_af6list *entry;
  271. entry = netlbl_af6list_search_exact(addr, mask, head);
  272. if (entry == NULL)
  273. return NULL;
  274. netlbl_af6list_remove_entry(entry);
  275. return entry;
  276. }
  277. #endif /* IPv6 */
  278. /*
  279. * Audit Helper Functions
  280. */
  281. #ifdef CONFIG_AUDIT
  282. /**
  283. * netlbl_af4list_audit_addr - Audit an IPv4 address
  284. * @audit_buf: audit buffer
  285. * @src: true if source address, false if destination
  286. * @dev: network interface
  287. * @addr: IP address
  288. * @mask: IP address mask
  289. *
  290. * Description:
  291. * Write the IPv4 address and address mask, if necessary, to @audit_buf.
  292. *
  293. */
  294. void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
  295. int src, const char *dev,
  296. __be32 addr, __be32 mask)
  297. {
  298. u32 mask_val = ntohl(mask);
  299. char *dir = (src ? "src" : "dst");
  300. if (dev != NULL)
  301. audit_log_format(audit_buf, " netif=%s", dev);
  302. audit_log_format(audit_buf, " %s=%pI4", dir, &addr);
  303. if (mask_val != 0xffffffff) {
  304. u32 mask_len = 0;
  305. while (mask_val > 0) {
  306. mask_val <<= 1;
  307. mask_len++;
  308. }
  309. audit_log_format(audit_buf, " %s_prefixlen=%d", dir, mask_len);
  310. }
  311. }
  312. #if IS_ENABLED(CONFIG_IPV6)
  313. /**
  314. * netlbl_af6list_audit_addr - Audit an IPv6 address
  315. * @audit_buf: audit buffer
  316. * @src: true if source address, false if destination
  317. * @dev: network interface
  318. * @addr: IP address
  319. * @mask: IP address mask
  320. *
  321. * Description:
  322. * Write the IPv6 address and address mask, if necessary, to @audit_buf.
  323. *
  324. */
  325. void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
  326. int src,
  327. const char *dev,
  328. const struct in6_addr *addr,
  329. const struct in6_addr *mask)
  330. {
  331. char *dir = (src ? "src" : "dst");
  332. if (dev != NULL)
  333. audit_log_format(audit_buf, " netif=%s", dev);
  334. audit_log_format(audit_buf, " %s=%pI6", dir, addr);
  335. if (ntohl(mask->s6_addr32[3]) != 0xffffffff) {
  336. u32 mask_len = 0;
  337. u32 mask_val;
  338. int iter = -1;
  339. while (ntohl(mask->s6_addr32[++iter]) == 0xffffffff)
  340. mask_len += 32;
  341. mask_val = ntohl(mask->s6_addr32[iter]);
  342. while (mask_val > 0) {
  343. mask_val <<= 1;
  344. mask_len++;
  345. }
  346. audit_log_format(audit_buf, " %s_prefixlen=%d", dir, mask_len);
  347. }
  348. }
  349. #endif /* IPv6 */
  350. #endif /* CONFIG_AUDIT */