netlabel_domainhash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. if (entry->domain != NULL) {
  291. u32 bkt = netlbl_domhsh_hash(entry->domain);
  292. list_add_tail_rcu(&entry->list,
  293. &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
  294. } else {
  295. INIT_LIST_HEAD(&entry->list);
  296. rcu_assign_pointer(netlbl_domhsh_def, entry);
  297. }
  298. if (entry->type == NETLBL_NLTYPE_ADDRSELECT) {
  299. netlbl_af4list_foreach_rcu(iter4,
  300. &entry->type_def.addrsel->list4)
  301. netlbl_domhsh_audit_add(entry, iter4, NULL,
  302. ret_val, audit_info);
  303. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  304. netlbl_af6list_foreach_rcu(iter6,
  305. &entry->type_def.addrsel->list6)
  306. netlbl_domhsh_audit_add(entry, NULL, iter6,
  307. ret_val, audit_info);
  308. #endif /* IPv6 */
  309. } else
  310. netlbl_domhsh_audit_add(entry, NULL, NULL,
  311. ret_val, audit_info);
  312. } else if (entry_old->type == NETLBL_NLTYPE_ADDRSELECT &&
  313. entry->type == NETLBL_NLTYPE_ADDRSELECT) {
  314. struct list_head *old_list4;
  315. struct list_head *old_list6;
  316. old_list4 = &entry_old->type_def.addrsel->list4;
  317. old_list6 = &entry_old->type_def.addrsel->list6;
  318. /* we only allow the addition of address selectors if all of
  319. * the selectors do not exist in the existing domain map */
  320. netlbl_af4list_foreach_rcu(iter4,
  321. &entry->type_def.addrsel->list4)
  322. if (netlbl_af4list_search_exact(iter4->addr,
  323. iter4->mask,
  324. old_list4)) {
  325. ret_val = -EEXIST;
  326. goto add_return;
  327. }
  328. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  329. netlbl_af6list_foreach_rcu(iter6,
  330. &entry->type_def.addrsel->list6)
  331. if (netlbl_af6list_search_exact(&iter6->addr,
  332. &iter6->mask,
  333. old_list6)) {
  334. ret_val = -EEXIST;
  335. goto add_return;
  336. }
  337. #endif /* IPv6 */
  338. netlbl_af4list_foreach_safe(iter4, tmp4,
  339. &entry->type_def.addrsel->list4) {
  340. netlbl_af4list_remove_entry(iter4);
  341. iter4->valid = 1;
  342. ret_val = netlbl_af4list_add(iter4, old_list4);
  343. netlbl_domhsh_audit_add(entry_old, iter4, NULL,
  344. ret_val, audit_info);
  345. if (ret_val != 0)
  346. goto add_return;
  347. }
  348. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  349. netlbl_af6list_foreach_safe(iter6, tmp6,
  350. &entry->type_def.addrsel->list6) {
  351. netlbl_af6list_remove_entry(iter6);
  352. iter6->valid = 1;
  353. ret_val = netlbl_af6list_add(iter6, old_list6);
  354. netlbl_domhsh_audit_add(entry_old, NULL, iter6,
  355. ret_val, audit_info);
  356. if (ret_val != 0)
  357. goto add_return;
  358. }
  359. #endif /* IPv6 */
  360. } else
  361. ret_val = -EINVAL;
  362. add_return:
  363. spin_unlock(&netlbl_domhsh_lock);
  364. rcu_read_unlock();
  365. return ret_val;
  366. }
  367. /**
  368. * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
  369. * @entry: the entry to add
  370. * @audit_info: NetLabel audit information
  371. *
  372. * Description:
  373. * Adds a new default entry to the domain hash table and handles any updates
  374. * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
  375. * negative on failure.
  376. *
  377. */
  378. int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
  379. struct netlbl_audit *audit_info)
  380. {
  381. return netlbl_domhsh_add(entry, audit_info);
  382. }
  383. /**
  384. * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
  385. * @entry: the entry to remove
  386. * @audit_info: NetLabel audit information
  387. *
  388. * Description:
  389. * Removes an entry from the domain hash table and handles any updates to the
  390. * lower level protocol handler (i.e. CIPSO). Caller is responsible for
  391. * ensuring that the RCU read lock is held. Returns zero on success, negative
  392. * on failure.
  393. *
  394. */
  395. int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
  396. struct netlbl_audit *audit_info)
  397. {
  398. int ret_val = 0;
  399. struct audit_buffer *audit_buf;
  400. if (entry == NULL)
  401. return -ENOENT;
  402. spin_lock(&netlbl_domhsh_lock);
  403. if (entry->valid) {
  404. entry->valid = 0;
  405. if (entry != rcu_dereference(netlbl_domhsh_def))
  406. list_del_rcu(&entry->list);
  407. else
  408. rcu_assign_pointer(netlbl_domhsh_def, NULL);
  409. } else
  410. ret_val = -ENOENT;
  411. spin_unlock(&netlbl_domhsh_lock);
  412. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
  413. if (audit_buf != NULL) {
  414. audit_log_format(audit_buf,
  415. " nlbl_domain=%s res=%u",
  416. entry->domain ? entry->domain : "(default)",
  417. ret_val == 0 ? 1 : 0);
  418. audit_log_end(audit_buf);
  419. }
  420. if (ret_val == 0) {
  421. struct netlbl_af4list *iter4;
  422. struct netlbl_domaddr4_map *map4;
  423. switch (entry->type) {
  424. case NETLBL_NLTYPE_ADDRSELECT:
  425. netlbl_af4list_foreach_rcu(iter4,
  426. &entry->type_def.addrsel->list4) {
  427. map4 = netlbl_domhsh_addr4_entry(iter4);
  428. cipso_v4_doi_putdef(map4->type_def.cipsov4);
  429. }
  430. /* no need to check the IPv6 list since we currently
  431. * support only unlabeled protocols for IPv6 */
  432. break;
  433. case NETLBL_NLTYPE_CIPSOV4:
  434. cipso_v4_doi_putdef(entry->type_def.cipsov4);
  435. break;
  436. }
  437. call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
  438. }
  439. return ret_val;
  440. }
  441. /**
  442. * netlbl_domhsh_remove_af4 - Removes an address selector entry
  443. * @domain: the domain
  444. * @addr: IPv4 address
  445. * @mask: IPv4 address mask
  446. * @audit_info: NetLabel audit information
  447. *
  448. * Description:
  449. * Removes an individual address selector from a domain mapping and potentially
  450. * the entire mapping if it is empty. Returns zero on success, negative values
  451. * on failure.
  452. *
  453. */
  454. int netlbl_domhsh_remove_af4(const char *domain,
  455. const struct in_addr *addr,
  456. const struct in_addr *mask,
  457. struct netlbl_audit *audit_info)
  458. {
  459. struct netlbl_dom_map *entry_map;
  460. struct netlbl_af4list *entry_addr;
  461. struct netlbl_af4list *iter4;
  462. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  463. struct netlbl_af6list *iter6;
  464. #endif /* IPv6 */
  465. struct netlbl_domaddr4_map *entry;
  466. rcu_read_lock();
  467. if (domain)
  468. entry_map = netlbl_domhsh_search(domain);
  469. else
  470. entry_map = netlbl_domhsh_search_def(domain);
  471. if (entry_map == NULL || entry_map->type != NETLBL_NLTYPE_ADDRSELECT)
  472. goto remove_af4_failure;
  473. spin_lock(&netlbl_domhsh_lock);
  474. entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  475. &entry_map->type_def.addrsel->list4);
  476. spin_unlock(&netlbl_domhsh_lock);
  477. if (entry_addr == NULL)
  478. goto remove_af4_failure;
  479. netlbl_af4list_foreach_rcu(iter4, &entry_map->type_def.addrsel->list4)
  480. goto remove_af4_single_addr;
  481. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  482. netlbl_af6list_foreach_rcu(iter6, &entry_map->type_def.addrsel->list6)
  483. goto remove_af4_single_addr;
  484. #endif /* IPv6 */
  485. /* the domain mapping is empty so remove it from the mapping table */
  486. netlbl_domhsh_remove_entry(entry_map, audit_info);
  487. remove_af4_single_addr:
  488. rcu_read_unlock();
  489. /* yick, we can't use call_rcu here because we don't have a rcu head
  490. * pointer but hopefully this should be a rare case so the pause
  491. * shouldn't be a problem */
  492. synchronize_rcu();
  493. entry = netlbl_domhsh_addr4_entry(entry_addr);
  494. cipso_v4_doi_putdef(entry->type_def.cipsov4);
  495. kfree(entry);
  496. return 0;
  497. remove_af4_failure:
  498. rcu_read_unlock();
  499. return -ENOENT;
  500. }
  501. /**
  502. * netlbl_domhsh_remove - Removes an entry from the domain hash table
  503. * @domain: the domain to remove
  504. * @audit_info: NetLabel audit information
  505. *
  506. * Description:
  507. * Removes an entry from the domain hash table and handles any updates to the
  508. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  509. * negative on failure.
  510. *
  511. */
  512. int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
  513. {
  514. int ret_val;
  515. struct netlbl_dom_map *entry;
  516. rcu_read_lock();
  517. if (domain)
  518. entry = netlbl_domhsh_search(domain);
  519. else
  520. entry = netlbl_domhsh_search_def(domain);
  521. ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
  522. rcu_read_unlock();
  523. return ret_val;
  524. }
  525. /**
  526. * netlbl_domhsh_remove_default - Removes the default entry from the table
  527. * @audit_info: NetLabel audit information
  528. *
  529. * Description:
  530. * Removes/resets the default entry for the domain hash table and handles any
  531. * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
  532. * success, non-zero on failure.
  533. *
  534. */
  535. int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
  536. {
  537. return netlbl_domhsh_remove(NULL, audit_info);
  538. }
  539. /**
  540. * netlbl_domhsh_getentry - Get an entry from the domain hash table
  541. * @domain: the domain name to search for
  542. *
  543. * Description:
  544. * Look through the domain hash table searching for an entry to match @domain,
  545. * return a pointer to a copy of the entry or NULL. The caller is responsibile
  546. * for ensuring that rcu_read_[un]lock() is called.
  547. *
  548. */
  549. struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
  550. {
  551. return netlbl_domhsh_search_def(domain);
  552. }
  553. /**
  554. * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
  555. * @domain: the domain name to search for
  556. * @addr: the IP address to search for
  557. *
  558. * Description:
  559. * Look through the domain hash table searching for an entry to match @domain
  560. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  561. * responsible for ensuring that rcu_read_[un]lock() is called.
  562. *
  563. */
  564. struct netlbl_domaddr4_map *netlbl_domhsh_getentry_af4(const char *domain,
  565. __be32 addr)
  566. {
  567. struct netlbl_dom_map *dom_iter;
  568. struct netlbl_af4list *addr_iter;
  569. dom_iter = netlbl_domhsh_search_def(domain);
  570. if (dom_iter == NULL)
  571. return NULL;
  572. if (dom_iter->type != NETLBL_NLTYPE_ADDRSELECT)
  573. return NULL;
  574. addr_iter = netlbl_af4list_search(addr,
  575. &dom_iter->type_def.addrsel->list4);
  576. if (addr_iter == NULL)
  577. return NULL;
  578. return netlbl_domhsh_addr4_entry(addr_iter);
  579. }
  580. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  581. /**
  582. * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
  583. * @domain: the domain name to search for
  584. * @addr: the IP address to search for
  585. *
  586. * Description:
  587. * Look through the domain hash table searching for an entry to match @domain
  588. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  589. * responsible for ensuring that rcu_read_[un]lock() is called.
  590. *
  591. */
  592. struct netlbl_domaddr6_map *netlbl_domhsh_getentry_af6(const char *domain,
  593. const struct in6_addr *addr)
  594. {
  595. struct netlbl_dom_map *dom_iter;
  596. struct netlbl_af6list *addr_iter;
  597. dom_iter = netlbl_domhsh_search_def(domain);
  598. if (dom_iter == NULL)
  599. return NULL;
  600. if (dom_iter->type != NETLBL_NLTYPE_ADDRSELECT)
  601. return NULL;
  602. addr_iter = netlbl_af6list_search(addr,
  603. &dom_iter->type_def.addrsel->list6);
  604. if (addr_iter == NULL)
  605. return NULL;
  606. return netlbl_domhsh_addr6_entry(addr_iter);
  607. }
  608. #endif /* IPv6 */
  609. /**
  610. * netlbl_domhsh_walk - Iterate through the domain mapping hash table
  611. * @skip_bkt: the number of buckets to skip at the start
  612. * @skip_chain: the number of entries to skip in the first iterated bucket
  613. * @callback: callback for each entry
  614. * @cb_arg: argument for the callback function
  615. *
  616. * Description:
  617. * Interate over the domain mapping hash table, skipping the first @skip_bkt
  618. * buckets and @skip_chain entries. For each entry in the table call
  619. * @callback, if @callback returns a negative value stop 'walking' through the
  620. * table and return. Updates the values in @skip_bkt and @skip_chain on
  621. * return. Returns zero on success, negative values on failure.
  622. *
  623. */
  624. int netlbl_domhsh_walk(u32 *skip_bkt,
  625. u32 *skip_chain,
  626. int (*callback) (struct netlbl_dom_map *entry, void *arg),
  627. void *cb_arg)
  628. {
  629. int ret_val = -ENOENT;
  630. u32 iter_bkt;
  631. struct list_head *iter_list;
  632. struct netlbl_dom_map *iter_entry;
  633. u32 chain_cnt = 0;
  634. rcu_read_lock();
  635. for (iter_bkt = *skip_bkt;
  636. iter_bkt < rcu_dereference(netlbl_domhsh)->size;
  637. iter_bkt++, chain_cnt = 0) {
  638. iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
  639. list_for_each_entry_rcu(iter_entry, iter_list, list)
  640. if (iter_entry->valid) {
  641. if (chain_cnt++ < *skip_chain)
  642. continue;
  643. ret_val = callback(iter_entry, cb_arg);
  644. if (ret_val < 0) {
  645. chain_cnt--;
  646. goto walk_return;
  647. }
  648. }
  649. }
  650. walk_return:
  651. rcu_read_unlock();
  652. *skip_bkt = iter_bkt;
  653. *skip_chain = chain_cnt;
  654. return ret_val;
  655. }