netlabel_domainhash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * NetLabel Domain Hash Table
  3. *
  4. * This file manages the domain hash table that NetLabel uses to determine
  5. * which network labeling protocol to use for a given domain. The NetLabel
  6. * system manages static and dynamic label mappings for network protocols such
  7. * as CIPSO and RIPSO.
  8. *
  9. * Author: Paul Moore <paul@paul-moore.com>
  10. *
  11. */
  12. /*
  13. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  23. * the GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <linux/types.h>
  31. #include <linux/rculist.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/string.h>
  35. #include <linux/audit.h>
  36. #include <linux/slab.h>
  37. #include <net/netlabel.h>
  38. #include <net/cipso_ipv4.h>
  39. #include <asm/bug.h>
  40. #include "netlabel_mgmt.h"
  41. #include "netlabel_addrlist.h"
  42. #include "netlabel_domainhash.h"
  43. #include "netlabel_user.h"
  44. struct netlbl_domhsh_tbl {
  45. struct list_head *tbl;
  46. u32 size;
  47. };
  48. /* Domain hash table */
  49. /* updates should be so rare that having one spinlock for the entire hash table
  50. * should be okay */
  51. static DEFINE_SPINLOCK(netlbl_domhsh_lock);
  52. #define netlbl_domhsh_rcu_deref(p) \
  53. rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
  54. static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
  55. static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
  56. /*
  57. * Domain Hash Table Helper Functions
  58. */
  59. /**
  60. * netlbl_domhsh_free_entry - Frees a domain hash table entry
  61. * @entry: the entry's RCU field
  62. *
  63. * Description:
  64. * This function is designed to be used as a callback to the call_rcu()
  65. * function so that the memory allocated to a hash table entry can be released
  66. * safely.
  67. *
  68. */
  69. static void netlbl_domhsh_free_entry(struct rcu_head *entry)
  70. {
  71. struct netlbl_dom_map *ptr;
  72. struct netlbl_af4list *iter4;
  73. struct netlbl_af4list *tmp4;
  74. #if IS_ENABLED(CONFIG_IPV6)
  75. struct netlbl_af6list *iter6;
  76. struct netlbl_af6list *tmp6;
  77. #endif /* IPv6 */
  78. ptr = container_of(entry, struct netlbl_dom_map, rcu);
  79. if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
  80. netlbl_af4list_foreach_safe(iter4, tmp4,
  81. &ptr->def.addrsel->list4) {
  82. netlbl_af4list_remove_entry(iter4);
  83. kfree(netlbl_domhsh_addr4_entry(iter4));
  84. }
  85. #if IS_ENABLED(CONFIG_IPV6)
  86. netlbl_af6list_foreach_safe(iter6, tmp6,
  87. &ptr->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->def.type;
  198. cipsov4 = map4->def.cipso;
  199. netlbl_af4list_audit_addr(audit_buf, 0, NULL,
  200. addr4->addr, addr4->mask);
  201. #if IS_ENABLED(CONFIG_IPV6)
  202. } else if (addr6 != NULL) {
  203. struct netlbl_domaddr6_map *map6;
  204. map6 = netlbl_domhsh_addr6_entry(addr6);
  205. type = map6->def.type;
  206. netlbl_af6list_audit_addr(audit_buf, 0, NULL,
  207. &addr6->addr, &addr6->mask);
  208. #endif /* IPv6 */
  209. } else {
  210. type = entry->def.type;
  211. cipsov4 = entry->def.cipso;
  212. }
  213. switch (type) {
  214. case NETLBL_NLTYPE_UNLABELED:
  215. audit_log_format(audit_buf, " nlbl_protocol=unlbl");
  216. break;
  217. case NETLBL_NLTYPE_CIPSOV4:
  218. BUG_ON(cipsov4 == NULL);
  219. audit_log_format(audit_buf,
  220. " nlbl_protocol=cipsov4 cipso_doi=%u",
  221. cipsov4->doi);
  222. break;
  223. }
  224. audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
  225. audit_log_end(audit_buf);
  226. }
  227. }
  228. /**
  229. * netlbl_domhsh_validate - Validate a new domain mapping entry
  230. * @entry: the entry to validate
  231. *
  232. * This function validates the new domain mapping entry to ensure that it is
  233. * a valid entry. Returns zero on success, negative values on failure.
  234. *
  235. */
  236. static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
  237. {
  238. struct netlbl_af4list *iter4;
  239. struct netlbl_domaddr4_map *map4;
  240. #if IS_ENABLED(CONFIG_IPV6)
  241. struct netlbl_af6list *iter6;
  242. struct netlbl_domaddr6_map *map6;
  243. #endif /* IPv6 */
  244. if (entry == NULL)
  245. return -EINVAL;
  246. switch (entry->def.type) {
  247. case NETLBL_NLTYPE_UNLABELED:
  248. if (entry->def.cipso != NULL || entry->def.addrsel != NULL)
  249. return -EINVAL;
  250. break;
  251. case NETLBL_NLTYPE_CIPSOV4:
  252. if (entry->def.cipso == NULL)
  253. return -EINVAL;
  254. break;
  255. case NETLBL_NLTYPE_ADDRSELECT:
  256. netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
  257. map4 = netlbl_domhsh_addr4_entry(iter4);
  258. switch (map4->def.type) {
  259. case NETLBL_NLTYPE_UNLABELED:
  260. if (map4->def.cipso != NULL)
  261. return -EINVAL;
  262. break;
  263. case NETLBL_NLTYPE_CIPSOV4:
  264. if (map4->def.cipso == NULL)
  265. return -EINVAL;
  266. break;
  267. default:
  268. return -EINVAL;
  269. }
  270. }
  271. #if IS_ENABLED(CONFIG_IPV6)
  272. netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
  273. map6 = netlbl_domhsh_addr6_entry(iter6);
  274. switch (map6->def.type) {
  275. case NETLBL_NLTYPE_UNLABELED:
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. }
  281. #endif /* IPv6 */
  282. break;
  283. default:
  284. return -EINVAL;
  285. }
  286. return 0;
  287. }
  288. /*
  289. * Domain Hash Table Functions
  290. */
  291. /**
  292. * netlbl_domhsh_init - Init for the domain hash
  293. * @size: the number of bits to use for the hash buckets
  294. *
  295. * Description:
  296. * Initializes the domain hash table, should be called only by
  297. * netlbl_user_init() during initialization. Returns zero on success, non-zero
  298. * values on error.
  299. *
  300. */
  301. int __init netlbl_domhsh_init(u32 size)
  302. {
  303. u32 iter;
  304. struct netlbl_domhsh_tbl *hsh_tbl;
  305. if (size == 0)
  306. return -EINVAL;
  307. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  308. if (hsh_tbl == NULL)
  309. return -ENOMEM;
  310. hsh_tbl->size = 1 << size;
  311. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  312. sizeof(struct list_head),
  313. GFP_KERNEL);
  314. if (hsh_tbl->tbl == NULL) {
  315. kfree(hsh_tbl);
  316. return -ENOMEM;
  317. }
  318. for (iter = 0; iter < hsh_tbl->size; iter++)
  319. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  320. spin_lock(&netlbl_domhsh_lock);
  321. rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
  322. spin_unlock(&netlbl_domhsh_lock);
  323. return 0;
  324. }
  325. /**
  326. * netlbl_domhsh_add - Adds a entry to the domain hash table
  327. * @entry: the entry to add
  328. * @audit_info: NetLabel audit information
  329. *
  330. * Description:
  331. * Adds a new entry to the domain hash table and handles any updates to the
  332. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  333. * negative on failure.
  334. *
  335. */
  336. int netlbl_domhsh_add(struct netlbl_dom_map *entry,
  337. struct netlbl_audit *audit_info)
  338. {
  339. int ret_val = 0;
  340. struct netlbl_dom_map *entry_old;
  341. struct netlbl_af4list *iter4;
  342. struct netlbl_af4list *tmp4;
  343. #if IS_ENABLED(CONFIG_IPV6)
  344. struct netlbl_af6list *iter6;
  345. struct netlbl_af6list *tmp6;
  346. #endif /* IPv6 */
  347. ret_val = netlbl_domhsh_validate(entry);
  348. if (ret_val != 0)
  349. return ret_val;
  350. /* XXX - we can remove this RCU read lock as the spinlock protects the
  351. * entire function, but before we do we need to fixup the
  352. * netlbl_af[4,6]list RCU functions to do "the right thing" with
  353. * respect to rcu_dereference() when only a spinlock is held. */
  354. rcu_read_lock();
  355. spin_lock(&netlbl_domhsh_lock);
  356. if (entry->domain != NULL)
  357. entry_old = netlbl_domhsh_search(entry->domain);
  358. else
  359. entry_old = netlbl_domhsh_search_def(entry->domain);
  360. if (entry_old == NULL) {
  361. entry->valid = 1;
  362. if (entry->domain != NULL) {
  363. u32 bkt = netlbl_domhsh_hash(entry->domain);
  364. list_add_tail_rcu(&entry->list,
  365. &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
  366. } else {
  367. INIT_LIST_HEAD(&entry->list);
  368. rcu_assign_pointer(netlbl_domhsh_def, entry);
  369. }
  370. if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
  371. netlbl_af4list_foreach_rcu(iter4,
  372. &entry->def.addrsel->list4)
  373. netlbl_domhsh_audit_add(entry, iter4, NULL,
  374. ret_val, audit_info);
  375. #if IS_ENABLED(CONFIG_IPV6)
  376. netlbl_af6list_foreach_rcu(iter6,
  377. &entry->def.addrsel->list6)
  378. netlbl_domhsh_audit_add(entry, NULL, iter6,
  379. ret_val, audit_info);
  380. #endif /* IPv6 */
  381. } else
  382. netlbl_domhsh_audit_add(entry, NULL, NULL,
  383. ret_val, audit_info);
  384. } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
  385. entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
  386. struct list_head *old_list4;
  387. struct list_head *old_list6;
  388. old_list4 = &entry_old->def.addrsel->list4;
  389. old_list6 = &entry_old->def.addrsel->list6;
  390. /* we only allow the addition of address selectors if all of
  391. * the selectors do not exist in the existing domain map */
  392. netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
  393. if (netlbl_af4list_search_exact(iter4->addr,
  394. iter4->mask,
  395. old_list4)) {
  396. ret_val = -EEXIST;
  397. goto add_return;
  398. }
  399. #if IS_ENABLED(CONFIG_IPV6)
  400. netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
  401. if (netlbl_af6list_search_exact(&iter6->addr,
  402. &iter6->mask,
  403. old_list6)) {
  404. ret_val = -EEXIST;
  405. goto add_return;
  406. }
  407. #endif /* IPv6 */
  408. netlbl_af4list_foreach_safe(iter4, tmp4,
  409. &entry->def.addrsel->list4) {
  410. netlbl_af4list_remove_entry(iter4);
  411. iter4->valid = 1;
  412. ret_val = netlbl_af4list_add(iter4, old_list4);
  413. netlbl_domhsh_audit_add(entry_old, iter4, NULL,
  414. ret_val, audit_info);
  415. if (ret_val != 0)
  416. goto add_return;
  417. }
  418. #if IS_ENABLED(CONFIG_IPV6)
  419. netlbl_af6list_foreach_safe(iter6, tmp6,
  420. &entry->def.addrsel->list6) {
  421. netlbl_af6list_remove_entry(iter6);
  422. iter6->valid = 1;
  423. ret_val = netlbl_af6list_add(iter6, old_list6);
  424. netlbl_domhsh_audit_add(entry_old, NULL, iter6,
  425. ret_val, audit_info);
  426. if (ret_val != 0)
  427. goto add_return;
  428. }
  429. #endif /* IPv6 */
  430. } else
  431. ret_val = -EINVAL;
  432. add_return:
  433. spin_unlock(&netlbl_domhsh_lock);
  434. rcu_read_unlock();
  435. return ret_val;
  436. }
  437. /**
  438. * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
  439. * @entry: the entry to add
  440. * @audit_info: NetLabel audit information
  441. *
  442. * Description:
  443. * Adds a new default entry to the domain hash table and handles any updates
  444. * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
  445. * negative on failure.
  446. *
  447. */
  448. int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
  449. struct netlbl_audit *audit_info)
  450. {
  451. return netlbl_domhsh_add(entry, audit_info);
  452. }
  453. /**
  454. * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
  455. * @entry: the entry to remove
  456. * @audit_info: NetLabel audit information
  457. *
  458. * Description:
  459. * Removes an entry from the domain hash table and handles any updates to the
  460. * lower level protocol handler (i.e. CIPSO). Caller is responsible for
  461. * ensuring that the RCU read lock is held. Returns zero on success, negative
  462. * on failure.
  463. *
  464. */
  465. int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
  466. struct netlbl_audit *audit_info)
  467. {
  468. int ret_val = 0;
  469. struct audit_buffer *audit_buf;
  470. if (entry == NULL)
  471. return -ENOENT;
  472. spin_lock(&netlbl_domhsh_lock);
  473. if (entry->valid) {
  474. entry->valid = 0;
  475. if (entry != rcu_dereference(netlbl_domhsh_def))
  476. list_del_rcu(&entry->list);
  477. else
  478. RCU_INIT_POINTER(netlbl_domhsh_def, NULL);
  479. } else
  480. ret_val = -ENOENT;
  481. spin_unlock(&netlbl_domhsh_lock);
  482. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
  483. if (audit_buf != NULL) {
  484. audit_log_format(audit_buf,
  485. " nlbl_domain=%s res=%u",
  486. entry->domain ? entry->domain : "(default)",
  487. ret_val == 0 ? 1 : 0);
  488. audit_log_end(audit_buf);
  489. }
  490. if (ret_val == 0) {
  491. struct netlbl_af4list *iter4;
  492. struct netlbl_domaddr4_map *map4;
  493. switch (entry->def.type) {
  494. case NETLBL_NLTYPE_ADDRSELECT:
  495. netlbl_af4list_foreach_rcu(iter4,
  496. &entry->def.addrsel->list4) {
  497. map4 = netlbl_domhsh_addr4_entry(iter4);
  498. cipso_v4_doi_putdef(map4->def.cipso);
  499. }
  500. /* no need to check the IPv6 list since we currently
  501. * support only unlabeled protocols for IPv6 */
  502. break;
  503. case NETLBL_NLTYPE_CIPSOV4:
  504. cipso_v4_doi_putdef(entry->def.cipso);
  505. break;
  506. }
  507. call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
  508. }
  509. return ret_val;
  510. }
  511. /**
  512. * netlbl_domhsh_remove_af4 - Removes an address selector entry
  513. * @domain: the domain
  514. * @addr: IPv4 address
  515. * @mask: IPv4 address mask
  516. * @audit_info: NetLabel audit information
  517. *
  518. * Description:
  519. * Removes an individual address selector from a domain mapping and potentially
  520. * the entire mapping if it is empty. Returns zero on success, negative values
  521. * on failure.
  522. *
  523. */
  524. int netlbl_domhsh_remove_af4(const char *domain,
  525. const struct in_addr *addr,
  526. const struct in_addr *mask,
  527. struct netlbl_audit *audit_info)
  528. {
  529. struct netlbl_dom_map *entry_map;
  530. struct netlbl_af4list *entry_addr;
  531. struct netlbl_af4list *iter4;
  532. #if IS_ENABLED(CONFIG_IPV6)
  533. struct netlbl_af6list *iter6;
  534. #endif /* IPv6 */
  535. struct netlbl_domaddr4_map *entry;
  536. rcu_read_lock();
  537. if (domain)
  538. entry_map = netlbl_domhsh_search(domain);
  539. else
  540. entry_map = netlbl_domhsh_search_def(domain);
  541. if (entry_map == NULL ||
  542. entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
  543. goto remove_af4_failure;
  544. spin_lock(&netlbl_domhsh_lock);
  545. entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  546. &entry_map->def.addrsel->list4);
  547. spin_unlock(&netlbl_domhsh_lock);
  548. if (entry_addr == NULL)
  549. goto remove_af4_failure;
  550. netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
  551. goto remove_af4_single_addr;
  552. #if IS_ENABLED(CONFIG_IPV6)
  553. netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
  554. goto remove_af4_single_addr;
  555. #endif /* IPv6 */
  556. /* the domain mapping is empty so remove it from the mapping table */
  557. netlbl_domhsh_remove_entry(entry_map, audit_info);
  558. remove_af4_single_addr:
  559. rcu_read_unlock();
  560. /* yick, we can't use call_rcu here because we don't have a rcu head
  561. * pointer but hopefully this should be a rare case so the pause
  562. * shouldn't be a problem */
  563. synchronize_rcu();
  564. entry = netlbl_domhsh_addr4_entry(entry_addr);
  565. cipso_v4_doi_putdef(entry->def.cipso);
  566. kfree(entry);
  567. return 0;
  568. remove_af4_failure:
  569. rcu_read_unlock();
  570. return -ENOENT;
  571. }
  572. /**
  573. * netlbl_domhsh_remove - Removes an entry from the domain hash table
  574. * @domain: the domain to remove
  575. * @audit_info: NetLabel audit information
  576. *
  577. * Description:
  578. * Removes an entry from the domain hash table and handles any updates to the
  579. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  580. * negative on failure.
  581. *
  582. */
  583. int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
  584. {
  585. int ret_val;
  586. struct netlbl_dom_map *entry;
  587. rcu_read_lock();
  588. if (domain)
  589. entry = netlbl_domhsh_search(domain);
  590. else
  591. entry = netlbl_domhsh_search_def(domain);
  592. ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
  593. rcu_read_unlock();
  594. return ret_val;
  595. }
  596. /**
  597. * netlbl_domhsh_remove_default - Removes the default entry from the table
  598. * @audit_info: NetLabel audit information
  599. *
  600. * Description:
  601. * Removes/resets the default entry for the domain hash table and handles any
  602. * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
  603. * success, non-zero on failure.
  604. *
  605. */
  606. int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
  607. {
  608. return netlbl_domhsh_remove(NULL, audit_info);
  609. }
  610. /**
  611. * netlbl_domhsh_getentry - Get an entry from the domain hash table
  612. * @domain: the domain name to search for
  613. *
  614. * Description:
  615. * Look through the domain hash table searching for an entry to match @domain,
  616. * return a pointer to a copy of the entry or NULL. The caller is responsible
  617. * for ensuring that rcu_read_[un]lock() is called.
  618. *
  619. */
  620. struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
  621. {
  622. return netlbl_domhsh_search_def(domain);
  623. }
  624. /**
  625. * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
  626. * @domain: the domain name to search for
  627. * @addr: the IP address to search for
  628. *
  629. * Description:
  630. * Look through the domain hash table searching for an entry to match @domain
  631. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  632. * responsible for ensuring that rcu_read_[un]lock() is called.
  633. *
  634. */
  635. struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
  636. __be32 addr)
  637. {
  638. struct netlbl_dom_map *dom_iter;
  639. struct netlbl_af4list *addr_iter;
  640. dom_iter = netlbl_domhsh_search_def(domain);
  641. if (dom_iter == NULL)
  642. return NULL;
  643. if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
  644. return &dom_iter->def;
  645. addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
  646. if (addr_iter == NULL)
  647. return NULL;
  648. return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
  649. }
  650. #if IS_ENABLED(CONFIG_IPV6)
  651. /**
  652. * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
  653. * @domain: the domain name to search for
  654. * @addr: the IP address to search for
  655. *
  656. * Description:
  657. * Look through the domain hash table searching for an entry to match @domain
  658. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  659. * responsible for ensuring that rcu_read_[un]lock() is called.
  660. *
  661. */
  662. struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
  663. const struct in6_addr *addr)
  664. {
  665. struct netlbl_dom_map *dom_iter;
  666. struct netlbl_af6list *addr_iter;
  667. dom_iter = netlbl_domhsh_search_def(domain);
  668. if (dom_iter == NULL)
  669. return NULL;
  670. if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
  671. return &dom_iter->def;
  672. addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
  673. if (addr_iter == NULL)
  674. return NULL;
  675. return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
  676. }
  677. #endif /* IPv6 */
  678. /**
  679. * netlbl_domhsh_walk - Iterate through the domain mapping hash table
  680. * @skip_bkt: the number of buckets to skip at the start
  681. * @skip_chain: the number of entries to skip in the first iterated bucket
  682. * @callback: callback for each entry
  683. * @cb_arg: argument for the callback function
  684. *
  685. * Description:
  686. * Interate over the domain mapping hash table, skipping the first @skip_bkt
  687. * buckets and @skip_chain entries. For each entry in the table call
  688. * @callback, if @callback returns a negative value stop 'walking' through the
  689. * table and return. Updates the values in @skip_bkt and @skip_chain on
  690. * return. Returns zero on success, negative values on failure.
  691. *
  692. */
  693. int netlbl_domhsh_walk(u32 *skip_bkt,
  694. u32 *skip_chain,
  695. int (*callback) (struct netlbl_dom_map *entry, void *arg),
  696. void *cb_arg)
  697. {
  698. int ret_val = -ENOENT;
  699. u32 iter_bkt;
  700. struct list_head *iter_list;
  701. struct netlbl_dom_map *iter_entry;
  702. u32 chain_cnt = 0;
  703. rcu_read_lock();
  704. for (iter_bkt = *skip_bkt;
  705. iter_bkt < rcu_dereference(netlbl_domhsh)->size;
  706. iter_bkt++, chain_cnt = 0) {
  707. iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
  708. list_for_each_entry_rcu(iter_entry, iter_list, list)
  709. if (iter_entry->valid) {
  710. if (chain_cnt++ < *skip_chain)
  711. continue;
  712. ret_val = callback(iter_entry, cb_arg);
  713. if (ret_val < 0) {
  714. chain_cnt--;
  715. goto walk_return;
  716. }
  717. }
  718. }
  719. walk_return:
  720. rcu_read_unlock();
  721. *skip_bkt = iter_bkt;
  722. *skip_chain = chain_cnt;
  723. return ret_val;
  724. }