netlabel_domainhash.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NetLabel Domain Hash Table
  4. *
  5. * This file manages the domain hash table that NetLabel uses to determine
  6. * which network labeling protocol to use for a given domain. The NetLabel
  7. * system manages static and dynamic label mappings for network protocols such
  8. * as CIPSO and RIPSO.
  9. *
  10. * Author: Paul Moore <paul@paul-moore.com>
  11. */
  12. /*
  13. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  14. */
  15. #include <linux/types.h>
  16. #include <linux/rculist.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/string.h>
  20. #include <linux/audit.h>
  21. #include <linux/slab.h>
  22. #include <net/netlabel.h>
  23. #include <net/cipso_ipv4.h>
  24. #include <net/calipso.h>
  25. #include <asm/bug.h>
  26. #include "netlabel_mgmt.h"
  27. #include "netlabel_addrlist.h"
  28. #include "netlabel_calipso.h"
  29. #include "netlabel_domainhash.h"
  30. #include "netlabel_user.h"
  31. struct netlbl_domhsh_tbl {
  32. struct list_head *tbl;
  33. u32 size;
  34. };
  35. /* Domain hash table */
  36. /* updates should be so rare that having one spinlock for the entire hash table
  37. * should be okay */
  38. static DEFINE_SPINLOCK(netlbl_domhsh_lock);
  39. #define netlbl_domhsh_rcu_deref(p) \
  40. rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
  41. static struct netlbl_domhsh_tbl __rcu *netlbl_domhsh;
  42. static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv4;
  43. static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv6;
  44. /*
  45. * Domain Hash Table Helper Functions
  46. */
  47. /**
  48. * netlbl_domhsh_free_entry - Frees a domain hash table entry
  49. * @entry: the entry's RCU field
  50. *
  51. * Description:
  52. * This function is designed to be used as a callback to the call_rcu()
  53. * function so that the memory allocated to a hash table entry can be released
  54. * safely.
  55. *
  56. */
  57. static void netlbl_domhsh_free_entry(struct rcu_head *entry)
  58. {
  59. struct netlbl_dom_map *ptr;
  60. struct netlbl_af4list *iter4;
  61. struct netlbl_af4list *tmp4;
  62. #if IS_ENABLED(CONFIG_IPV6)
  63. struct netlbl_af6list *iter6;
  64. struct netlbl_af6list *tmp6;
  65. #endif /* IPv6 */
  66. ptr = container_of(entry, struct netlbl_dom_map, rcu);
  67. if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
  68. netlbl_af4list_foreach_safe(iter4, tmp4,
  69. &ptr->def.addrsel->list4) {
  70. netlbl_af4list_remove_entry(iter4);
  71. kfree(netlbl_domhsh_addr4_entry(iter4));
  72. }
  73. #if IS_ENABLED(CONFIG_IPV6)
  74. netlbl_af6list_foreach_safe(iter6, tmp6,
  75. &ptr->def.addrsel->list6) {
  76. netlbl_af6list_remove_entry(iter6);
  77. kfree(netlbl_domhsh_addr6_entry(iter6));
  78. }
  79. #endif /* IPv6 */
  80. kfree(ptr->def.addrsel);
  81. }
  82. kfree(ptr->domain);
  83. kfree(ptr);
  84. }
  85. /**
  86. * netlbl_domhsh_hash - Hashing function for the domain hash table
  87. * @key: the domain name to hash
  88. *
  89. * Description:
  90. * This is the hashing function for the domain hash table, it returns the
  91. * correct bucket number for the domain. The caller is responsible for
  92. * ensuring that the hash table is protected with either a RCU read lock or the
  93. * hash table lock.
  94. *
  95. */
  96. static u32 netlbl_domhsh_hash(const char *key)
  97. {
  98. u32 iter;
  99. u32 val;
  100. u32 len;
  101. /* This is taken (with slight modification) from
  102. * security/selinux/ss/symtab.c:symhash() */
  103. for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
  104. val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
  105. return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
  106. }
  107. static bool netlbl_family_match(u16 f1, u16 f2)
  108. {
  109. return (f1 == f2) || (f1 == AF_UNSPEC) || (f2 == AF_UNSPEC);
  110. }
  111. /**
  112. * netlbl_domhsh_search - Search for a domain entry
  113. * @domain: the domain
  114. * @family: the address family
  115. *
  116. * Description:
  117. * Searches the domain hash table and returns a pointer to the hash table
  118. * entry if found, otherwise NULL is returned. @family may be %AF_UNSPEC
  119. * which matches any address family entries. The caller is responsible for
  120. * ensuring that the hash table is protected with either a RCU read lock or the
  121. * hash table lock.
  122. *
  123. */
  124. static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain,
  125. u16 family)
  126. {
  127. u32 bkt;
  128. struct list_head *bkt_list;
  129. struct netlbl_dom_map *iter;
  130. if (domain != NULL) {
  131. bkt = netlbl_domhsh_hash(domain);
  132. bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
  133. list_for_each_entry_rcu(iter, bkt_list, list,
  134. lockdep_is_held(&netlbl_domhsh_lock))
  135. if (iter->valid &&
  136. netlbl_family_match(iter->family, family) &&
  137. 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. * @family: the address family
  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. @family may be %AF_UNSPEC which matches any address family
  152. * entries. 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. u16 family)
  158. {
  159. struct netlbl_dom_map *entry;
  160. entry = netlbl_domhsh_search(domain, family);
  161. if (entry != NULL)
  162. return entry;
  163. if (family == AF_INET || family == AF_UNSPEC) {
  164. entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4);
  165. if (entry != NULL && entry->valid)
  166. return entry;
  167. }
  168. if (family == AF_INET6 || family == AF_UNSPEC) {
  169. entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6);
  170. if (entry != NULL && entry->valid)
  171. return entry;
  172. }
  173. return NULL;
  174. }
  175. /**
  176. * netlbl_domhsh_audit_add - Generate an audit entry for an add event
  177. * @entry: the entry being added
  178. * @addr4: the IPv4 address information
  179. * @addr6: the IPv6 address information
  180. * @result: the result code
  181. * @audit_info: NetLabel audit information
  182. *
  183. * Description:
  184. * Generate an audit record for adding a new NetLabel/LSM mapping entry with
  185. * the given information. Caller is responsible for holding the necessary
  186. * locks.
  187. *
  188. */
  189. static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
  190. struct netlbl_af4list *addr4,
  191. struct netlbl_af6list *addr6,
  192. int result,
  193. struct netlbl_audit *audit_info)
  194. {
  195. struct audit_buffer *audit_buf;
  196. struct cipso_v4_doi *cipsov4 = NULL;
  197. struct calipso_doi *calipso = NULL;
  198. u32 type;
  199. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
  200. if (audit_buf != NULL) {
  201. audit_log_format(audit_buf, " nlbl_domain=%s",
  202. entry->domain ? entry->domain : "(default)");
  203. if (addr4 != NULL) {
  204. struct netlbl_domaddr4_map *map4;
  205. map4 = netlbl_domhsh_addr4_entry(addr4);
  206. type = map4->def.type;
  207. cipsov4 = map4->def.cipso;
  208. netlbl_af4list_audit_addr(audit_buf, 0, NULL,
  209. addr4->addr, addr4->mask);
  210. #if IS_ENABLED(CONFIG_IPV6)
  211. } else if (addr6 != NULL) {
  212. struct netlbl_domaddr6_map *map6;
  213. map6 = netlbl_domhsh_addr6_entry(addr6);
  214. type = map6->def.type;
  215. calipso = map6->def.calipso;
  216. netlbl_af6list_audit_addr(audit_buf, 0, NULL,
  217. &addr6->addr, &addr6->mask);
  218. #endif /* IPv6 */
  219. } else {
  220. type = entry->def.type;
  221. cipsov4 = entry->def.cipso;
  222. calipso = entry->def.calipso;
  223. }
  224. switch (type) {
  225. case NETLBL_NLTYPE_UNLABELED:
  226. audit_log_format(audit_buf, " nlbl_protocol=unlbl");
  227. break;
  228. case NETLBL_NLTYPE_CIPSOV4:
  229. BUG_ON(cipsov4 == NULL);
  230. audit_log_format(audit_buf,
  231. " nlbl_protocol=cipsov4 cipso_doi=%u",
  232. cipsov4->doi);
  233. break;
  234. case NETLBL_NLTYPE_CALIPSO:
  235. BUG_ON(calipso == NULL);
  236. audit_log_format(audit_buf,
  237. " nlbl_protocol=calipso calipso_doi=%u",
  238. calipso->doi);
  239. break;
  240. }
  241. audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
  242. audit_log_end(audit_buf);
  243. }
  244. }
  245. /**
  246. * netlbl_domhsh_validate - Validate a new domain mapping entry
  247. * @entry: the entry to validate
  248. *
  249. * This function validates the new domain mapping entry to ensure that it is
  250. * a valid entry. Returns zero on success, negative values on failure.
  251. *
  252. */
  253. static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
  254. {
  255. struct netlbl_af4list *iter4;
  256. struct netlbl_domaddr4_map *map4;
  257. #if IS_ENABLED(CONFIG_IPV6)
  258. struct netlbl_af6list *iter6;
  259. struct netlbl_domaddr6_map *map6;
  260. #endif /* IPv6 */
  261. if (entry == NULL)
  262. return -EINVAL;
  263. if (entry->family != AF_INET && entry->family != AF_INET6 &&
  264. (entry->family != AF_UNSPEC ||
  265. entry->def.type != NETLBL_NLTYPE_UNLABELED))
  266. return -EINVAL;
  267. switch (entry->def.type) {
  268. case NETLBL_NLTYPE_UNLABELED:
  269. if (entry->def.cipso != NULL || entry->def.calipso != NULL ||
  270. entry->def.addrsel != NULL)
  271. return -EINVAL;
  272. break;
  273. case NETLBL_NLTYPE_CIPSOV4:
  274. if (entry->family != AF_INET ||
  275. entry->def.cipso == NULL)
  276. return -EINVAL;
  277. break;
  278. case NETLBL_NLTYPE_CALIPSO:
  279. if (entry->family != AF_INET6 ||
  280. entry->def.calipso == NULL)
  281. return -EINVAL;
  282. break;
  283. case NETLBL_NLTYPE_ADDRSELECT:
  284. netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
  285. map4 = netlbl_domhsh_addr4_entry(iter4);
  286. switch (map4->def.type) {
  287. case NETLBL_NLTYPE_UNLABELED:
  288. if (map4->def.cipso != NULL)
  289. return -EINVAL;
  290. break;
  291. case NETLBL_NLTYPE_CIPSOV4:
  292. if (map4->def.cipso == NULL)
  293. return -EINVAL;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. }
  299. #if IS_ENABLED(CONFIG_IPV6)
  300. netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
  301. map6 = netlbl_domhsh_addr6_entry(iter6);
  302. switch (map6->def.type) {
  303. case NETLBL_NLTYPE_UNLABELED:
  304. if (map6->def.calipso != NULL)
  305. return -EINVAL;
  306. break;
  307. case NETLBL_NLTYPE_CALIPSO:
  308. if (map6->def.calipso == NULL)
  309. return -EINVAL;
  310. break;
  311. default:
  312. return -EINVAL;
  313. }
  314. }
  315. #endif /* IPv6 */
  316. break;
  317. default:
  318. return -EINVAL;
  319. }
  320. return 0;
  321. }
  322. /*
  323. * Domain Hash Table Functions
  324. */
  325. /**
  326. * netlbl_domhsh_init - Init for the domain hash
  327. * @size: the number of bits to use for the hash buckets
  328. *
  329. * Description:
  330. * Initializes the domain hash table, should be called only by
  331. * netlbl_user_init() during initialization. Returns zero on success, non-zero
  332. * values on error.
  333. *
  334. */
  335. int __init netlbl_domhsh_init(u32 size)
  336. {
  337. u32 iter;
  338. struct netlbl_domhsh_tbl *hsh_tbl;
  339. if (size == 0)
  340. return -EINVAL;
  341. hsh_tbl = kmalloc_obj(*hsh_tbl);
  342. if (hsh_tbl == NULL)
  343. return -ENOMEM;
  344. hsh_tbl->size = 1 << size;
  345. hsh_tbl->tbl = kzalloc_objs(struct list_head, hsh_tbl->size);
  346. if (hsh_tbl->tbl == NULL) {
  347. kfree(hsh_tbl);
  348. return -ENOMEM;
  349. }
  350. for (iter = 0; iter < hsh_tbl->size; iter++)
  351. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  352. spin_lock(&netlbl_domhsh_lock);
  353. rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
  354. spin_unlock(&netlbl_domhsh_lock);
  355. return 0;
  356. }
  357. /**
  358. * netlbl_domhsh_add - Adds a entry to the domain hash table
  359. * @entry: the entry to add
  360. * @audit_info: NetLabel audit information
  361. *
  362. * Description:
  363. * Adds a new entry to the domain hash table and handles any updates to the
  364. * lower level protocol handler (i.e. CIPSO). @entry->family may be set to
  365. * %AF_UNSPEC which will add an entry that matches all address families. This
  366. * is only useful for the unlabelled type and will only succeed if there is no
  367. * existing entry for any address family with the same domain. Returns zero
  368. * on success, negative on failure.
  369. *
  370. */
  371. int netlbl_domhsh_add(struct netlbl_dom_map *entry,
  372. struct netlbl_audit *audit_info)
  373. {
  374. int ret_val = 0;
  375. struct netlbl_dom_map *entry_old, *entry_b;
  376. struct netlbl_af4list *iter4;
  377. struct netlbl_af4list *tmp4;
  378. #if IS_ENABLED(CONFIG_IPV6)
  379. struct netlbl_af6list *iter6;
  380. struct netlbl_af6list *tmp6;
  381. #endif /* IPv6 */
  382. ret_val = netlbl_domhsh_validate(entry);
  383. if (ret_val != 0)
  384. return ret_val;
  385. /* XXX - we can remove this RCU read lock as the spinlock protects the
  386. * entire function, but before we do we need to fixup the
  387. * netlbl_af[4,6]list RCU functions to do "the right thing" with
  388. * respect to rcu_dereference() when only a spinlock is held. */
  389. rcu_read_lock();
  390. spin_lock(&netlbl_domhsh_lock);
  391. if (entry->domain != NULL)
  392. entry_old = netlbl_domhsh_search(entry->domain, entry->family);
  393. else
  394. entry_old = netlbl_domhsh_search_def(entry->domain,
  395. entry->family);
  396. if (entry_old == NULL) {
  397. entry->valid = 1;
  398. if (entry->domain != NULL) {
  399. u32 bkt = netlbl_domhsh_hash(entry->domain);
  400. list_add_tail_rcu(&entry->list,
  401. &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
  402. } else {
  403. INIT_LIST_HEAD(&entry->list);
  404. switch (entry->family) {
  405. case AF_INET:
  406. rcu_assign_pointer(netlbl_domhsh_def_ipv4,
  407. entry);
  408. break;
  409. case AF_INET6:
  410. rcu_assign_pointer(netlbl_domhsh_def_ipv6,
  411. entry);
  412. break;
  413. case AF_UNSPEC:
  414. if (entry->def.type !=
  415. NETLBL_NLTYPE_UNLABELED) {
  416. ret_val = -EINVAL;
  417. goto add_return;
  418. }
  419. entry_b = kzalloc_obj(*entry_b, GFP_ATOMIC);
  420. if (entry_b == NULL) {
  421. ret_val = -ENOMEM;
  422. goto add_return;
  423. }
  424. entry_b->family = AF_INET6;
  425. entry_b->def.type = NETLBL_NLTYPE_UNLABELED;
  426. entry_b->valid = 1;
  427. entry->family = AF_INET;
  428. rcu_assign_pointer(netlbl_domhsh_def_ipv4,
  429. entry);
  430. rcu_assign_pointer(netlbl_domhsh_def_ipv6,
  431. entry_b);
  432. break;
  433. default:
  434. /* Already checked in
  435. * netlbl_domhsh_validate(). */
  436. ret_val = -EINVAL;
  437. goto add_return;
  438. }
  439. }
  440. if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
  441. netlbl_af4list_foreach_rcu(iter4,
  442. &entry->def.addrsel->list4)
  443. netlbl_domhsh_audit_add(entry, iter4, NULL,
  444. ret_val, audit_info);
  445. #if IS_ENABLED(CONFIG_IPV6)
  446. netlbl_af6list_foreach_rcu(iter6,
  447. &entry->def.addrsel->list6)
  448. netlbl_domhsh_audit_add(entry, NULL, iter6,
  449. ret_val, audit_info);
  450. #endif /* IPv6 */
  451. } else
  452. netlbl_domhsh_audit_add(entry, NULL, NULL,
  453. ret_val, audit_info);
  454. } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
  455. entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
  456. struct list_head *old_list4;
  457. struct list_head *old_list6;
  458. old_list4 = &entry_old->def.addrsel->list4;
  459. old_list6 = &entry_old->def.addrsel->list6;
  460. /* we only allow the addition of address selectors if all of
  461. * the selectors do not exist in the existing domain map */
  462. netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
  463. if (netlbl_af4list_search_exact(iter4->addr,
  464. iter4->mask,
  465. old_list4)) {
  466. ret_val = -EEXIST;
  467. goto add_return;
  468. }
  469. #if IS_ENABLED(CONFIG_IPV6)
  470. netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
  471. if (netlbl_af6list_search_exact(&iter6->addr,
  472. &iter6->mask,
  473. old_list6)) {
  474. ret_val = -EEXIST;
  475. goto add_return;
  476. }
  477. #endif /* IPv6 */
  478. netlbl_af4list_foreach_safe(iter4, tmp4,
  479. &entry->def.addrsel->list4) {
  480. netlbl_af4list_remove_entry(iter4);
  481. iter4->valid = 1;
  482. ret_val = netlbl_af4list_add(iter4, old_list4);
  483. netlbl_domhsh_audit_add(entry_old, iter4, NULL,
  484. ret_val, audit_info);
  485. if (ret_val != 0)
  486. goto add_return;
  487. }
  488. #if IS_ENABLED(CONFIG_IPV6)
  489. netlbl_af6list_foreach_safe(iter6, tmp6,
  490. &entry->def.addrsel->list6) {
  491. netlbl_af6list_remove_entry(iter6);
  492. iter6->valid = 1;
  493. ret_val = netlbl_af6list_add(iter6, old_list6);
  494. netlbl_domhsh_audit_add(entry_old, NULL, iter6,
  495. ret_val, audit_info);
  496. if (ret_val != 0)
  497. goto add_return;
  498. }
  499. #endif /* IPv6 */
  500. /* cleanup the new entry since we've moved everything over */
  501. netlbl_domhsh_free_entry(&entry->rcu);
  502. } else
  503. ret_val = -EINVAL;
  504. add_return:
  505. spin_unlock(&netlbl_domhsh_lock);
  506. rcu_read_unlock();
  507. return ret_val;
  508. }
  509. /**
  510. * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
  511. * @entry: the entry to add
  512. * @audit_info: NetLabel audit information
  513. *
  514. * Description:
  515. * Adds a new default entry to the domain hash table and handles any updates
  516. * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
  517. * negative on failure.
  518. *
  519. */
  520. int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
  521. struct netlbl_audit *audit_info)
  522. {
  523. return netlbl_domhsh_add(entry, audit_info);
  524. }
  525. /**
  526. * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
  527. * @entry: the entry to remove
  528. * @audit_info: NetLabel audit information
  529. *
  530. * Description:
  531. * Removes an entry from the domain hash table and handles any updates to the
  532. * lower level protocol handler (i.e. CIPSO). Caller is responsible for
  533. * ensuring that the RCU read lock is held. Returns zero on success, negative
  534. * on failure.
  535. *
  536. */
  537. int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
  538. struct netlbl_audit *audit_info)
  539. {
  540. int ret_val = 0;
  541. struct audit_buffer *audit_buf;
  542. struct netlbl_af4list *iter4;
  543. struct netlbl_domaddr4_map *map4;
  544. #if IS_ENABLED(CONFIG_IPV6)
  545. struct netlbl_af6list *iter6;
  546. struct netlbl_domaddr6_map *map6;
  547. #endif /* IPv6 */
  548. if (entry == NULL)
  549. return -ENOENT;
  550. spin_lock(&netlbl_domhsh_lock);
  551. if (entry->valid) {
  552. entry->valid = 0;
  553. if (entry == rcu_dereference(netlbl_domhsh_def_ipv4))
  554. RCU_INIT_POINTER(netlbl_domhsh_def_ipv4, NULL);
  555. else if (entry == rcu_dereference(netlbl_domhsh_def_ipv6))
  556. RCU_INIT_POINTER(netlbl_domhsh_def_ipv6, NULL);
  557. else
  558. list_del_rcu(&entry->list);
  559. } else
  560. ret_val = -ENOENT;
  561. spin_unlock(&netlbl_domhsh_lock);
  562. if (ret_val)
  563. return ret_val;
  564. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
  565. if (audit_buf != NULL) {
  566. audit_log_format(audit_buf,
  567. " nlbl_domain=%s res=1",
  568. entry->domain ? entry->domain : "(default)");
  569. audit_log_end(audit_buf);
  570. }
  571. switch (entry->def.type) {
  572. case NETLBL_NLTYPE_ADDRSELECT:
  573. netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
  574. map4 = netlbl_domhsh_addr4_entry(iter4);
  575. cipso_v4_doi_putdef(map4->def.cipso);
  576. }
  577. #if IS_ENABLED(CONFIG_IPV6)
  578. netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
  579. map6 = netlbl_domhsh_addr6_entry(iter6);
  580. calipso_doi_putdef(map6->def.calipso);
  581. }
  582. #endif /* IPv6 */
  583. break;
  584. case NETLBL_NLTYPE_CIPSOV4:
  585. cipso_v4_doi_putdef(entry->def.cipso);
  586. break;
  587. #if IS_ENABLED(CONFIG_IPV6)
  588. case NETLBL_NLTYPE_CALIPSO:
  589. calipso_doi_putdef(entry->def.calipso);
  590. break;
  591. #endif /* IPv6 */
  592. }
  593. call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
  594. return ret_val;
  595. }
  596. /**
  597. * netlbl_domhsh_remove_af4 - Removes an address selector entry
  598. * @domain: the domain
  599. * @addr: IPv4 address
  600. * @mask: IPv4 address mask
  601. * @audit_info: NetLabel audit information
  602. *
  603. * Description:
  604. * Removes an individual address selector from a domain mapping and potentially
  605. * the entire mapping if it is empty. Returns zero on success, negative values
  606. * on failure.
  607. *
  608. */
  609. int netlbl_domhsh_remove_af4(const char *domain,
  610. const struct in_addr *addr,
  611. const struct in_addr *mask,
  612. struct netlbl_audit *audit_info)
  613. {
  614. struct netlbl_dom_map *entry_map;
  615. struct netlbl_af4list *entry_addr;
  616. struct netlbl_af4list *iter4;
  617. #if IS_ENABLED(CONFIG_IPV6)
  618. struct netlbl_af6list *iter6;
  619. #endif /* IPv6 */
  620. struct netlbl_domaddr4_map *entry;
  621. rcu_read_lock();
  622. if (domain)
  623. entry_map = netlbl_domhsh_search(domain, AF_INET);
  624. else
  625. entry_map = netlbl_domhsh_search_def(domain, AF_INET);
  626. if (entry_map == NULL ||
  627. entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
  628. goto remove_af4_failure;
  629. spin_lock(&netlbl_domhsh_lock);
  630. entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  631. &entry_map->def.addrsel->list4);
  632. spin_unlock(&netlbl_domhsh_lock);
  633. if (entry_addr == NULL)
  634. goto remove_af4_failure;
  635. netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
  636. goto remove_af4_single_addr;
  637. #if IS_ENABLED(CONFIG_IPV6)
  638. netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
  639. goto remove_af4_single_addr;
  640. #endif /* IPv6 */
  641. /* the domain mapping is empty so remove it from the mapping table */
  642. netlbl_domhsh_remove_entry(entry_map, audit_info);
  643. remove_af4_single_addr:
  644. rcu_read_unlock();
  645. /* yick, we can't use call_rcu here because we don't have a rcu head
  646. * pointer but hopefully this should be a rare case so the pause
  647. * shouldn't be a problem */
  648. synchronize_rcu();
  649. entry = netlbl_domhsh_addr4_entry(entry_addr);
  650. cipso_v4_doi_putdef(entry->def.cipso);
  651. kfree(entry);
  652. return 0;
  653. remove_af4_failure:
  654. rcu_read_unlock();
  655. return -ENOENT;
  656. }
  657. #if IS_ENABLED(CONFIG_IPV6)
  658. /**
  659. * netlbl_domhsh_remove_af6 - Removes an address selector entry
  660. * @domain: the domain
  661. * @addr: IPv6 address
  662. * @mask: IPv6 address mask
  663. * @audit_info: NetLabel audit information
  664. *
  665. * Description:
  666. * Removes an individual address selector from a domain mapping and potentially
  667. * the entire mapping if it is empty. Returns zero on success, negative values
  668. * on failure.
  669. *
  670. */
  671. int netlbl_domhsh_remove_af6(const char *domain,
  672. const struct in6_addr *addr,
  673. const struct in6_addr *mask,
  674. struct netlbl_audit *audit_info)
  675. {
  676. struct netlbl_dom_map *entry_map;
  677. struct netlbl_af6list *entry_addr;
  678. struct netlbl_af4list *iter4;
  679. struct netlbl_af6list *iter6;
  680. struct netlbl_domaddr6_map *entry;
  681. rcu_read_lock();
  682. if (domain)
  683. entry_map = netlbl_domhsh_search(domain, AF_INET6);
  684. else
  685. entry_map = netlbl_domhsh_search_def(domain, AF_INET6);
  686. if (entry_map == NULL ||
  687. entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
  688. goto remove_af6_failure;
  689. spin_lock(&netlbl_domhsh_lock);
  690. entry_addr = netlbl_af6list_remove(addr, mask,
  691. &entry_map->def.addrsel->list6);
  692. spin_unlock(&netlbl_domhsh_lock);
  693. if (entry_addr == NULL)
  694. goto remove_af6_failure;
  695. netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
  696. goto remove_af6_single_addr;
  697. netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
  698. goto remove_af6_single_addr;
  699. /* the domain mapping is empty so remove it from the mapping table */
  700. netlbl_domhsh_remove_entry(entry_map, audit_info);
  701. remove_af6_single_addr:
  702. rcu_read_unlock();
  703. /* yick, we can't use call_rcu here because we don't have a rcu head
  704. * pointer but hopefully this should be a rare case so the pause
  705. * shouldn't be a problem */
  706. synchronize_rcu();
  707. entry = netlbl_domhsh_addr6_entry(entry_addr);
  708. calipso_doi_putdef(entry->def.calipso);
  709. kfree(entry);
  710. return 0;
  711. remove_af6_failure:
  712. rcu_read_unlock();
  713. return -ENOENT;
  714. }
  715. #endif /* IPv6 */
  716. /**
  717. * netlbl_domhsh_remove - Removes an entry from the domain hash table
  718. * @domain: the domain to remove
  719. * @family: address family
  720. * @audit_info: NetLabel audit information
  721. *
  722. * Description:
  723. * Removes an entry from the domain hash table and handles any updates to the
  724. * lower level protocol handler (i.e. CIPSO). @family may be %AF_UNSPEC which
  725. * removes all address family entries. Returns zero on success, negative on
  726. * failure.
  727. *
  728. */
  729. int netlbl_domhsh_remove(const char *domain, u16 family,
  730. struct netlbl_audit *audit_info)
  731. {
  732. int ret_val = -EINVAL;
  733. struct netlbl_dom_map *entry;
  734. rcu_read_lock();
  735. if (family == AF_INET || family == AF_UNSPEC) {
  736. if (domain)
  737. entry = netlbl_domhsh_search(domain, AF_INET);
  738. else
  739. entry = netlbl_domhsh_search_def(domain, AF_INET);
  740. ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
  741. if (ret_val && ret_val != -ENOENT)
  742. goto done;
  743. }
  744. if (family == AF_INET6 || family == AF_UNSPEC) {
  745. int ret_val2;
  746. if (domain)
  747. entry = netlbl_domhsh_search(domain, AF_INET6);
  748. else
  749. entry = netlbl_domhsh_search_def(domain, AF_INET6);
  750. ret_val2 = netlbl_domhsh_remove_entry(entry, audit_info);
  751. if (ret_val2 != -ENOENT)
  752. ret_val = ret_val2;
  753. }
  754. done:
  755. rcu_read_unlock();
  756. return ret_val;
  757. }
  758. /**
  759. * netlbl_domhsh_remove_default - Removes the default entry from the table
  760. * @family: address family
  761. * @audit_info: NetLabel audit information
  762. *
  763. * Description:
  764. * Removes/resets the default entry corresponding to @family from the domain
  765. * hash table and handles any updates to the lower level protocol handler
  766. * (i.e. CIPSO). @family may be %AF_UNSPEC which removes all address family
  767. * entries. Returns zero on success, negative on failure.
  768. *
  769. */
  770. int netlbl_domhsh_remove_default(u16 family, struct netlbl_audit *audit_info)
  771. {
  772. return netlbl_domhsh_remove(NULL, family, audit_info);
  773. }
  774. /**
  775. * netlbl_domhsh_getentry - Get an entry from the domain hash table
  776. * @domain: the domain name to search for
  777. * @family: address family
  778. *
  779. * Description:
  780. * Look through the domain hash table searching for an entry to match @domain,
  781. * with address family @family, return a pointer to a copy of the entry or
  782. * NULL. The caller is responsible for ensuring that rcu_read_[un]lock() is
  783. * called.
  784. *
  785. */
  786. struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain, u16 family)
  787. {
  788. if (family == AF_UNSPEC)
  789. return NULL;
  790. return netlbl_domhsh_search_def(domain, family);
  791. }
  792. /**
  793. * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
  794. * @domain: the domain name to search for
  795. * @addr: the IP address to search for
  796. *
  797. * Description:
  798. * Look through the domain hash table searching for an entry to match @domain
  799. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  800. * responsible for ensuring that rcu_read_[un]lock() is called.
  801. *
  802. */
  803. struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
  804. __be32 addr)
  805. {
  806. struct netlbl_dom_map *dom_iter;
  807. struct netlbl_af4list *addr_iter;
  808. dom_iter = netlbl_domhsh_search_def(domain, AF_INET);
  809. if (dom_iter == NULL)
  810. return NULL;
  811. if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
  812. return &dom_iter->def;
  813. addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
  814. if (addr_iter == NULL)
  815. return NULL;
  816. return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
  817. }
  818. #if IS_ENABLED(CONFIG_IPV6)
  819. /**
  820. * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
  821. * @domain: the domain name to search for
  822. * @addr: the IP address to search for
  823. *
  824. * Description:
  825. * Look through the domain hash table searching for an entry to match @domain
  826. * and @addr, return a pointer to a copy of the entry or NULL. The caller is
  827. * responsible for ensuring that rcu_read_[un]lock() is called.
  828. *
  829. */
  830. struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
  831. const struct in6_addr *addr)
  832. {
  833. struct netlbl_dom_map *dom_iter;
  834. struct netlbl_af6list *addr_iter;
  835. dom_iter = netlbl_domhsh_search_def(domain, AF_INET6);
  836. if (dom_iter == NULL)
  837. return NULL;
  838. if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
  839. return &dom_iter->def;
  840. addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
  841. if (addr_iter == NULL)
  842. return NULL;
  843. return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
  844. }
  845. #endif /* IPv6 */
  846. /**
  847. * netlbl_domhsh_walk - Iterate through the domain mapping hash table
  848. * @skip_bkt: the number of buckets to skip at the start
  849. * @skip_chain: the number of entries to skip in the first iterated bucket
  850. * @callback: callback for each entry
  851. * @cb_arg: argument for the callback function
  852. *
  853. * Description:
  854. * Iterate over the domain mapping hash table, skipping the first @skip_bkt
  855. * buckets and @skip_chain entries. For each entry in the table call
  856. * @callback, if @callback returns a negative value stop 'walking' through the
  857. * table and return. Updates the values in @skip_bkt and @skip_chain on
  858. * return. Returns zero on success, negative values on failure.
  859. *
  860. */
  861. int netlbl_domhsh_walk(u32 *skip_bkt,
  862. u32 *skip_chain,
  863. int (*callback) (struct netlbl_dom_map *entry, void *arg),
  864. void *cb_arg)
  865. {
  866. int ret_val = -ENOENT;
  867. u32 iter_bkt;
  868. struct list_head *iter_list;
  869. struct netlbl_dom_map *iter_entry;
  870. u32 chain_cnt = 0;
  871. rcu_read_lock();
  872. for (iter_bkt = *skip_bkt;
  873. iter_bkt < rcu_dereference(netlbl_domhsh)->size;
  874. iter_bkt++, chain_cnt = 0) {
  875. iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
  876. list_for_each_entry_rcu(iter_entry, iter_list, list)
  877. if (iter_entry->valid) {
  878. if (chain_cnt++ < *skip_chain)
  879. continue;
  880. ret_val = callback(iter_entry, cb_arg);
  881. if (ret_val < 0) {
  882. chain_cnt--;
  883. goto walk_return;
  884. }
  885. }
  886. }
  887. walk_return:
  888. rcu_read_unlock();
  889. *skip_bkt = iter_bkt;
  890. *skip_chain = chain_cnt;
  891. return ret_val;
  892. }