netlabel_domainhash.c 21 KB

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