netlabel_domainhash.c 21 KB

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