netlabel_domainhash.c 22 KB

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