netlabel_addrlist.c 9.7 KB

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