netlabel_domainhash.c 21 KB

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