netlabel_domainhash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * NetLabel Domain Hash Table
  3. *
  4. * This file manages the domain hash table that NetLabel uses to determine
  5. * which network labeling protocol to use for a given domain. 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., 2006, 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/rculist.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/string.h>
  35. #include <linux/audit.h>
  36. #include <linux/slab.h>
  37. #include <net/netlabel.h>
  38. #include <net/cipso_ipv4.h>
  39. #include <asm/bug.h>
  40. #include "netlabel_mgmt.h"
  41. #include "netlabel_addrlist.h"
  42. #include "netlabel_domainhash.h"
  43. #include "netlabel_user.h"
  44. struct netlbl_domhsh_tbl {
  45. struct list_head *tbl;
  46. u32 size;
  47. };
  48. /* Domain hash table */
  49. /* updates should be so rare that having one spinlock for the entire hash table
  50. * should be okay */
  51. static DEFINE_SPINLOCK(netlbl_domhsh_lock);
  52. #define netlbl_domhsh_rcu_deref(p) \
  53. rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
  54. static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
  55. static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
  56. /*
  57. * Domain Hash Table Helper Functions
  58. */
  59. /**
  60. * netlbl_domhsh_free_entry - Frees a domain hash table entry
  61. * @entry: the entry's RCU field
  62. *
  63. * Description:
  64. * This function is designed to be used as a callback to the call_rcu()
  65. * function so that the memory allocated to a hash table entry can be released
  66. * safely.
  67. *
  68. */
  69. static void netlbl_domhsh_free_entry(struct rcu_head *entry)
  70. {
  71. struct netlbl_dom_map *ptr;
  72. struct netlbl_af4list *iter4;
  73. struct netlbl_af4list *tmp4;
  74. #if IS_ENABLED(CONFIG_IPV6)
  75. struct netlbl_af6list *iter6;
  76. struct netlbl_af6list *tmp6;
  77. #endif /* IPv6 */
  78. ptr = container_of(entry, struct netlbl_dom_map, rcu);
  79. if (ptr->type == NETLBL_NLTYPE_ADDRSELECT) {
  80. netlbl_af4list_foreach_safe(iter4, tmp4,
  81. &ptr->type_def.addrsel->list4) {
  82. netlbl_af4list_remove_entry(iter4);
  83. kfree(netlbl_domhsh_addr4_entry(iter4));
  84. }
  85. #if IS_ENABLED(CONFIG_IPV6)
  86. netlbl_af6list_foreach_safe(iter6, tmp6,
  87. &ptr->type_def.addrsel->list6) {
  88. netlbl_af6list_remove_entry(iter6);
  89. kfree(netlbl_domhsh_addr6_entry(iter6));
  90. }
  91. #endif /* IPv6 */
  92. }
  93. kfree(ptr->domain);
  94. kfree(ptr);
  95. }
  96. /**
  97. * netlbl_domhsh_hash - Hashing function for the domain hash table
  98. * @domain: the domain name to hash
  99. *
  100. * Description:
  101. * This is the hashing function for the domain hash table, it returns the
  102. * correct bucket number for the domain. The caller is responsible for
  103. * ensuring that the hash table is protected with either a RCU read lock or the
  104. * hash table lock.
  105. *
  106. */
  107. static u32 netlbl_domhsh_hash(const char *key)
  108. {
  109. u32 iter;
  110. u32 val;
  111. u32 len;
  112. /* This is taken (with slight modification) from
  113. * security/selinux/ss/symtab.c:symhash() */
  114. for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
  115. val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
  116. return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
  117. }
  118. /**
  119. * netlbl_domhsh_search - Search for a domain entry
  120. * @domain: the domain
  121. *
  122. * Description:
  123. * Searches the domain hash table and returns a pointer to the hash table
  124. * entry if found, otherwise NULL is returned. The caller is responsible for
  125. * ensuring that the hash table is protected with either a RCU read lock or the
  126. * hash table lock.
  127. *
  128. */
  129. static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
  130. {
  131. u32 bkt;
  132. struct list_head *bkt_list;
  133. struct netlbl_dom_map *iter;
  134. if (domain != NULL) {
  135. bkt = netlbl_domhsh_hash(domain);
  136. bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
  137. list_for_each_entry_rcu(iter, bkt_list, list)
  138. if (iter->valid && strcmp(iter->domain, domain) == 0)
  139. return iter;
  140. }
  141. return NULL;
  142. }
  143. /**
  144. * netlbl_domhsh_search_def - Search for a domain entry
  145. * @domain: the domain
  146. * @def: return default if no match is found
  147. *
  148. * Description:
  149. * Searches the domain hash table and returns a pointer to the hash table
  150. * entry if an exact match is found, if an exact match is not present in the
  151. * hash table then the default entry is returned if valid otherwise NULL is
  152. * returned. The caller is responsible ensuring that the hash table is
  153. * protected with either a RCU read lock or the hash table lock.
  154. *
  155. */
  156. static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
  157. {
  158. struct netlbl_dom_map *entry;
  159. entry = netlbl_domhsh_search(domain);
  160. if (entry == NULL) {
  161. entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def);
  162. if (entry != NULL && !entry->valid)
  163. entry = NULL;
  164. }
  165. return entry;
  166. }
  167. /**
  168. * netlbl_domhsh_audit_add - Generate an audit entry for an add event
  169. * @entry: the entry being added
  170. * @addr4: the IPv4 address information
  171. * @addr6: the IPv6 address information
  172. * @result: the result code
  173. * @audit_info: NetLabel audit information
  174. *
  175. * Description:
  176. * Generate an audit record for adding a new NetLabel/LSM mapping entry with
  177. * the given information. Caller is responsible for holding the necessary
  178. * locks.
  179. *
  180. */
  181. static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
  182. struct netlbl_af4list *addr4,
  183. struct netlbl_af6list *addr6,
  184. int result,
  185. struct netlbl_audit *audit_info)
  186. {
  187. struct audit_buffer *audit_buf;
  188. struct cipso_v4_doi *cipsov4 = NULL;
  189. u32 type;
  190. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
  191. if (audit_buf != NULL) {
  192. audit_log_format(audit_buf, " nlbl_domain=%s",
  193. entry->domain ? entry->domain : "(default)");
  194. if (addr4 != NULL) {
  195. struct netlbl_domaddr4_map *map4;
  196. map4 = netlbl_domhsh_addr4_entry(addr4);
  197. type = map4->type;
  198. cipsov4 = map4->type_def.cipsov4;
  199. netlbl_af4list_audit_addr(audit_buf, 0, NULL,
  200. addr4->addr, addr4->mask);
  201. #if IS_ENABLED(CONFIG_IPV6)
  202. } else if (addr6 != NULL) {
  203. struct netlbl_domaddr6_map *map6;
  204. map6 = netlbl_domhsh_addr6_entry(addr6);
  205. type = map6->type;
  206. netlbl_af6list_audit_addr(audit_buf, 0, NULL,
  207. &addr6->addr, &addr6->mask);
  208. #endif /* IPv6 */
  209. } else {
  210. type = entry->type;
  211. cipsov4 = entry->type_def.cipsov4;
  212. }
  213. switch (type) {
  214. case NETLBL_NLTYPE_UNLABELED:
  215. audit_log_format(audit_buf, " nlbl_protocol=unlbl");
  216. break;
  217. case NETLBL_NLTYPE_CIPSOV4:
  218. BUG_ON(cipsov4 == NULL);
  219. audit_log_format(audit_buf,
  220. " nlbl_protocol=cipsov4 cipso_doi=%u",
  221. cipsov4->doi);
  222. break;
  223. }
  224. audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
  225. audit_log_end(audit_buf);
  226. }
  227. }
  228. /**
  229. * netlbl_domhsh_validate - Validate a new domain mapping entry
  230. * @entry: the entry to validate
  231. *
  232. * This function validates the new domain mapping entry to ensure that it is
  233. * a valid entry. Returns zero on success, negative values on failure.
  234. *
  235. */
  236. static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
  237. {
  238. struct netlbl_af4list *iter4;
  239. struct netlbl_domaddr4_map *map4;
  240. #if IS_ENABLED(CONFIG_IPV6)
  241. struct netlbl_af6list *iter6;
  242. struct netlbl_domaddr6_map *map6;
  243. #endif /* IPv6 */
  244. if (entry == NULL)
  245. return -EINVAL;
  246. switch (entry->type) {
  247. case NETLBL_NLTYPE_UNLABELED:
  248. if (entry->type_def.cipsov4 != NULL ||
  249. entry->type_def.addrsel != NULL)
  250. return -EINVAL;
  251. break;
  252. case NETLBL_NLTYPE_CIPSOV4:
  253. if (entry->type_def.cipsov4 == NULL)
  254. return -EINVAL;
  255. break;
  256. case NETLBL_NLTYPE_ADDRSELECT:
  257. netlbl_af4list_foreach(iter4, &entry->type_def.addrsel->list4) {
  258. map4 = netlbl_domhsh_addr4_entry(iter4);
  259. switch (map4->type) {
  260. case NETLBL_NLTYPE_UNLABELED:
  261. if (map4->type_def.cipsov4 != NULL)
  262. return -EINVAL;
  263. break;
  264. case NETLBL_NLTYPE_CIPSOV4:
  265. if (map4->type_def.cipsov4 == NULL)
  266. return -EINVAL;
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. }
  272. #if IS_ENABLED(CONFIG_IPV6)
  273. netlbl_af6list_foreach(iter6, &entry->type_def.addrsel->list6) {
  274. map6 = netlbl_domhsh_addr6_entry(iter6);
  275. switch (map6->type) {
  276. case NETLBL_NLTYPE_UNLABELED:
  277. break;
  278. default:
  279. return -EINVAL;
  280. }
  281. }
  282. #endif /* IPv6 */
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. return 0;
  288. }
  289. /*
  290. * Domain Hash Table Functions
  291. */
  292. /**
  293. * netlbl_domhsh_init - Init for the domain hash
  294. * @size: the number of bits to use for the hash buckets
  295. *
  296. * Description:
  297. * Initializes the domain hash table, should be called only by
  298. * netlbl_user_init() during initialization. Returns zero on success, non-zero
  299. * values on error.
  300. *
  301. */
  302. int __init netlbl_domhsh_init(u32 size)
  303. {
  304. u32 iter;
  305. struct netlbl_domhsh_tbl *hsh_tbl;
  306. if (size == 0)
  307. return -EINVAL;
  308. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  309. if (hsh_tbl == NULL)
  310. return -ENOMEM;
  311. hsh_tbl->size = 1 << size;
  312. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  313. sizeof(struct list_head),
  314. GFP_KERNEL);
  315. if (hsh_tbl->tbl == NULL) {
  316. kfree(hsh_tbl);
  317. return -ENOMEM;
  318. }
  319. for (iter = 0; iter < hsh_tbl->size; iter++)
  320. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  321. spin_lock(&netlbl_domhsh_lock);
  322. rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
  323. spin_unlock(&netlbl_domhsh_lock);
  324. return 0;
  325. }
  326. /**
  327. * netlbl_domhsh_add - Adds a entry to the domain hash table
  328. * @entry: the entry to add
  329. * @audit_info: NetLabel audit information
  330. *
  331. * Description:
  332. * Adds a new entry to the domain hash table and handles any updates to the
  333. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  334. * negative on failure.
  335. *
  336. */
  337. int netlbl_domhsh_add(struct netlbl_dom_map *entry,
  338. struct netlbl_audit *audit_info)
  339. {
  340. int ret_val = 0;
  341. struct netlbl_dom_map *entry_old;
  342. struct netlbl_af4list *iter4;
  343. struct netlbl_af4list *tmp4;
  344. #if IS_ENABLED(CONFIG_IPV6)
  345. struct netlbl_af6list *iter6;
  346. struct netlbl_af6list *tmp6;
  347. #endif /* IPv6 */
  348. ret_val = netlbl_domhsh_validate(entry);
  349. if (ret_val != 0)
  350. return ret_val;
  351. /* XXX - we can remove this RCU read lock as the spinlock protects the
  352. * entire function, but before we do we need to fixup the
  353. * netlbl_af[4,6]list RCU functions to do "the right thing" with
  354. * respect to rcu_dereference() when only a spinlock is held. */
  355. rcu_read_lock();
  356. spin_lock(&netlbl_domhsh_lock);
  357. if (entry->domain != NULL)
  358. entry_old = netlbl_domhsh_search(entry->domain);
  359. else
  360. entry_old = netlbl_domhsh_search_def(entry->domain);
  361. if (entry_old == NULL) {
  362. entry->valid = 1;
  363. if (entry->domain != NULL) {
  364. u32 bkt = netlbl_domhsh_hash(entry->domain);
  365. list_add_tail_rcu(&entry->list,
  366. &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
  367. } else {
  368. INIT_LIST_HEAD(&entry->list);
  369. rcu_assign_pointer(netlbl_domhsh_def, entry);
  370. }
  371. if (entry->type == NETLBL_NLTYPE_ADDRSELECT) {
  372. netlbl_af4list_foreach_rcu(iter4,
  373. &entry->type_def.addrsel->list4)
  374. netlbl_domhsh_audit_add(entry, iter4, NULL,
  375. ret_val, audit_info);
  376. #if IS_ENABLED(CONFIG_IPV6)
  377. netlbl_af6list_foreach_rcu(iter6,
  378. &entry->type_def.addrsel->list6)
  379. netlbl_domhsh_audit_add(entry, NULL, iter6,
  380. ret_val, audit_info);
  381. #endif /* IPv6 */
  382. } else
  383. netlbl_domhsh_audit_add(entry, NULL, NULL,
  384. ret_val, audit_info);
  385. } else if (entry_old->type == NETLBL_NLTYPE_ADDRSELECT &&
  386. entry->type == NETLBL_NLTYPE_ADDRSELECT) {
  387. struct list_head *old_list4;
  388. struct list_head *old_list6;
  389. old_list4 = &entry_old->type_def.addrsel->list4;
  390. old_list6 = &entry_old->type_def.addrsel->list6;
  391. /* we only allow the addition of address selectors if all of
  392. * the selectors do not exist in the existing domain map */
  393. netlbl_af4list_foreach_rcu(iter4,
  394. &entry->type_def.addrsel->list4)
  395. if (netlbl_af4list_search_exact(iter4->addr,
  396. iter4->mask,
  397. old_list4)) {
  398. ret_val = -EEXIST;
  399. goto add_return;
  400. }
  401. #if IS_ENABLED(CONFIG_IPV6)
  402. netlbl_af6list_foreach_rcu(iter6,
  403. &entry->type_def.addrsel->list6)
  404. if (netlbl_af6list_search_exact(&iter6->addr,
  405. &iter6->mask,
  406. old_list6)) {
  407. ret_val = -EEXIST;
  408. goto add_return;
  409. }
  410. #endif /* IPv6 */
  411. netlbl_af4list_foreach_safe(iter4, tmp4,
  412. &entry->type_def.addrsel->list4) {
  413. netlbl_af4list_remove_entry(iter4);
  414. iter4->valid = 1;
  415. ret_val = netlbl_af4list_add(iter4, old_list4);
  416. netlbl_domhsh_audit_add(entry_old, iter4, NULL,
  417. ret_val, audit_info);
  418. if (ret_val != 0)
  419. goto add_return;
  420. }
  421. #if IS_ENABLED(CONFIG_IPV6)
  422. netlbl_af6list_foreach_safe(iter6, tmp6,
  423. &entry->type_def.addrsel->list6) {
  424. netlbl_af6list_remove_entry(iter6);
  425. iter6->valid = 1;
  426. ret_val = netlbl_af6list_add(iter6, old_list6);
  427. netlbl_domhsh_audit_add(entry_old, NULL, iter6,
  428. ret_val, audit_info);
  429. if (ret_val != 0)
  430. goto add_return;
  431. }
  432. #endif /* IPv6 */
  433. } else
  434. ret_val = -EINVAL;
  435. add_return:
  436. spin_unlock(&netlbl_domhsh_lock);
  437. rcu_read_unlock();
  438. return ret_val;
  439. }
  440. /**
  441. * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
  442. * @entry: the entry to add
  443. * @audit_info: NetLabel audit information
  444. *
  445. * Description:
  446. * Adds a new default entry to the domain hash table and handles any updates
  447. * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
  448. * negative on failure.
  449. *
  450. */
  451. int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
  452. struct netlbl_audit *audit_info)
  453. {
  454. return netlbl_domhsh_add(entry, audit_info);
  455. }
  456. /**
  457. * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
  458. * @entry: the entry to remove
  459. * @audit_info: NetLabel audit information
  460. *
  461. * Description:
  462. * Removes an entry from the domain hash table and handles any updates to the
  463. * lower level protocol handler (i.e. CIPSO). Caller is responsible for
  464. * ensuring that the RCU read lock is held. Returns zero on success, negative
  465. * on failure.
  466. *
  467. */
  468. int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
  469. struct netlbl_audit *audit_info)
  470. {
  471. int ret_val = 0;
  472. struct audit_buffer *audit_buf;
  473. if (entry == NULL)
  474. return -ENOENT;
  475. spin_lock(&netlbl_domhsh_lock);
  476. if (entry->valid) {
  477. entry->valid = 0;
  478. if (entry != rcu_dereference(netlbl_domhsh_def))
  479. list_del_rcu(&entry->list);
  480. else
  481. RCU_INIT_POINTER(netlbl_domhsh_def, NULL);
  482. } else
  483. ret_val = -ENOENT;
  484. spin_unlock(&netlbl_domhsh_lock);
  485. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
  486. if (audit_buf != NULL) {
  487. audit_log_format(audit_buf,
  488. " nlbl_domain=%s res=%u",
  489. entry->domain ? entry->domain : "(default)",
  490. ret_val == 0 ? 1 : 0);
  491. audit_log_end(audit_buf);
  492. }
  493. if (ret_val == 0) {
  494. struct netlbl_af4list *iter4;
  495. struct netlbl_domaddr4_map *map4;
  496. switch (entry->type) {
  497. case NETLBL_NLTYPE_ADDRSELECT:
  498. netlbl_af4list_foreach_rcu(iter4,
  499. &entry->type_def.addrsel->list4) {
  500. map4 = netlbl_domhsh_addr4_entry(iter4);
  501. cipso_v4_doi_putdef(map4->type_def.cipsov4);
  502. }
  503. /* no need to check the IPv6 list since we currently
  504. * support only unlabeled protocols for IPv6 */
  505. break;
  506. case NETLBL_NLTYPE_CIPSOV4:
  507. cipso_v4_doi_putdef(entry->type_def.cipsov4);
  508. break;
  509. }
  510. call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
  511. }
  512. return ret_val;
  513. }
  514. /**
  515. * netlbl_domhsh_remove_af4 - Removes an address selector entry
  516. * @domain: the domain
  517. * @addr: IPv4 address
  518. * @mask: IPv4 address mask
  519. * @audit_info: NetLabel audit information
  520. *
  521. * Description:
  522. * Removes an individual address selector from a domain mapping and potentially
  523. * the entire mapping if it is empty. Returns zero on success, negative values
  524. * on failure.
  525. *
  526. */
  527. int netlbl_domhsh_remove_af4(const char *domain,
  528. const struct in_addr *addr,
  529. const struct in_addr *mask,
  530. struct netlbl_audit *audit_info)
  531. {
  532. struct netlbl_dom_map *entry_map;
  533. struct netlbl_af4list *entry_addr;
  534. struct netlbl_af4list *iter4;
  535. #if IS_ENABLED(CONFIG_IPV6)
  536. struct netlbl_af6list *iter6;
  537. #endif /* IPv6 */
  538. struct netlbl_domaddr4_map *entry;
  539. rcu_read_lock();
  540. if (domain)
  541. entry_map = netlbl_domhsh_search(domain);
  542. else
  543. entry_map = netlbl_domhsh_search_def(domain);
  544. if (entry_map == NULL || entry_map->type != NETLBL_NLTYPE_ADDRSELECT)
  545. goto remove_af4_failure;
  546. spin_lock(&netlbl_domhsh_lock);
  547. entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  548. &entry_map->type_def.addrsel->list4);
  549. spin_unlock(&netlbl_domhsh_lock);
  550. if (entry_addr == NULL)
  551. goto remove_af4_failure;
  552. netlbl_af4list_foreach_rcu(iter4, &entry_map->type_def.addrsel->list4)
  553. goto remove_af4_single_addr;
  554. #if IS_ENABLED(CONFIG_IPV6)
  555. netlbl_af6list_foreach_rcu(iter6, &entry_map->type_def.addrsel->list6)
  556. goto remove_af4_single_addr;
  557. #endif /* IPv6 */
  558. /* the domain mapping is empty so remove it from the mapping table */
  559. netlbl_domhsh_remove_entry(entry_map, audit_info);
  560. remove_af4_single_addr:
  561. rcu_read_unlock();
  562. /* yick, we can't use call_rcu here because we don't have a rcu head
  563. * pointer but hopefully this should be a rare case so the pause
  564. * shouldn't be a problem */
  565. synchronize_rcu();
  566. entry = netlbl_domhsh_addr4_entry(entry_addr);
  567. cipso_v4_doi_putdef(entry->type_def.cipsov4);
  568. kfree(entry);
  569. return 0;
  570. remove_af4_failure:
  571. rcu_read_unlock();
  572. return -ENOENT;
  573. }
  574. /**
  575. * netlbl_domhsh_remove - Removes an entry from the domain hash table
  576. * @domain: the domain to remove
  577. * @audit_info: NetLabel audit information
  578. *
  579. * Description:
  580. * Removes an entry from the domain hash table and handles any updates to the
  581. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  582. * negative on failure.
  583. *
  584. */
  585. int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
  586. {
  587. int ret_val;
  588. struct netlbl_dom_map *entry;
  589. rcu_read_lock();
  590. if (domain)
  591. entry = netlbl_domhsh_search(domain);
  592. else
  593. entry = netlbl_domhsh_search_def(domain);
  594. ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
  595. rcu_read_unlock();
  596. return ret_val;
  597. }
  598. /**
  599. * netlbl_domhsh_remove_default - Removes the default entry from the table
  600. * @audit_info: NetLabel audit information
  601. *
  602. * Description:
  603. * Removes/resets the default entry for the domain hash table and handles any
  604. * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
  605. * success, non-zero on failure.
  606. *
  607. */
  608. int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
  609. {
  610. return netlbl_domhsh_remove(NULL, audit_info);
  611. }
  612. /**
  613. * netlbl_domhsh_getentry - Get an entry from the domain hash table
  614. * @domain: the domain name to search for
  615. *
  616. * Description:
  617. * Look through the domain hash table searching for an entry to match @domain,
  618. * return a pointer to a copy of the entry or NULL. The caller is responsible
  619. * for ensuring that rcu_read_[un]lock() is called.
  620. *
  621. */
  622. struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
  623. {
  624. return netlbl_domhsh_search_def(domain);
  625. }
  626. /**
  627. * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
  628. * @domain: the domain name to search for
  629. * @addr: the IP address to search for
  630. *
  631. * Description:
  632. * Look through the domain hash table searching for an entry to match @domain
  633. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  634. * responsible for ensuring that rcu_read_[un]lock() is called.
  635. *
  636. */
  637. struct netlbl_domaddr4_map *netlbl_domhsh_getentry_af4(const char *domain,
  638. __be32 addr)
  639. {
  640. struct netlbl_dom_map *dom_iter;
  641. struct netlbl_af4list *addr_iter;
  642. dom_iter = netlbl_domhsh_search_def(domain);
  643. if (dom_iter == NULL)
  644. return NULL;
  645. if (dom_iter->type != NETLBL_NLTYPE_ADDRSELECT)
  646. return NULL;
  647. addr_iter = netlbl_af4list_search(addr,
  648. &dom_iter->type_def.addrsel->list4);
  649. if (addr_iter == NULL)
  650. return NULL;
  651. return netlbl_domhsh_addr4_entry(addr_iter);
  652. }
  653. #if IS_ENABLED(CONFIG_IPV6)
  654. /**
  655. * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
  656. * @domain: the domain name to search for
  657. * @addr: the IP address to search for
  658. *
  659. * Description:
  660. * Look through the domain hash table searching for an entry to match @domain
  661. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  662. * responsible for ensuring that rcu_read_[un]lock() is called.
  663. *
  664. */
  665. struct netlbl_domaddr6_map *netlbl_domhsh_getentry_af6(const char *domain,
  666. const struct in6_addr *addr)
  667. {
  668. struct netlbl_dom_map *dom_iter;
  669. struct netlbl_af6list *addr_iter;
  670. dom_iter = netlbl_domhsh_search_def(domain);
  671. if (dom_iter == NULL)
  672. return NULL;
  673. if (dom_iter->type != NETLBL_NLTYPE_ADDRSELECT)
  674. return NULL;
  675. addr_iter = netlbl_af6list_search(addr,
  676. &dom_iter->type_def.addrsel->list6);
  677. if (addr_iter == NULL)
  678. return NULL;
  679. return netlbl_domhsh_addr6_entry(addr_iter);
  680. }
  681. #endif /* IPv6 */
  682. /**
  683. * netlbl_domhsh_walk - Iterate through the domain mapping hash table
  684. * @skip_bkt: the number of buckets to skip at the start
  685. * @skip_chain: the number of entries to skip in the first iterated bucket
  686. * @callback: callback for each entry
  687. * @cb_arg: argument for the callback function
  688. *
  689. * Description:
  690. * Interate over the domain mapping hash table, skipping the first @skip_bkt
  691. * buckets and @skip_chain entries. For each entry in the table call
  692. * @callback, if @callback returns a negative value stop 'walking' through the
  693. * table and return. Updates the values in @skip_bkt and @skip_chain on
  694. * return. Returns zero on success, negative values on failure.
  695. *
  696. */
  697. int netlbl_domhsh_walk(u32 *skip_bkt,
  698. u32 *skip_chain,
  699. int (*callback) (struct netlbl_dom_map *entry, void *arg),
  700. void *cb_arg)
  701. {
  702. int ret_val = -ENOENT;
  703. u32 iter_bkt;
  704. struct list_head *iter_list;
  705. struct netlbl_dom_map *iter_entry;
  706. u32 chain_cnt = 0;
  707. rcu_read_lock();
  708. for (iter_bkt = *skip_bkt;
  709. iter_bkt < rcu_dereference(netlbl_domhsh)->size;
  710. iter_bkt++, chain_cnt = 0) {
  711. iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
  712. list_for_each_entry_rcu(iter_entry, iter_list, list)
  713. if (iter_entry->valid) {
  714. if (chain_cnt++ < *skip_chain)
  715. continue;
  716. ret_val = callback(iter_entry, cb_arg);
  717. if (ret_val < 0) {
  718. chain_cnt--;
  719. goto walk_return;
  720. }
  721. }
  722. }
  723. walk_return:
  724. rcu_read_unlock();
  725. *skip_bkt = iter_bkt;
  726. *skip_chain = chain_cnt;
  727. return ret_val;
  728. }