netlabel_domainhash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * NetLabel Domain Hash Table
  3. *
  4. * This file manages the domain hash table that NetLabel uses to determine
  5. * which network labeling protocol to use for a given domain. The NetLabel
  6. * system manages static and dynamic label mappings for network protocols such
  7. * as CIPSO and RIPSO.
  8. *
  9. * Author: Paul Moore <paul.moore@hp.com>
  10. *
  11. */
  12. /*
  13. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  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/rcupdate.h>
  32. #include <linux/list.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/string.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_domainhash.h"
  41. struct netlbl_domhsh_tbl {
  42. struct list_head *tbl;
  43. u32 size;
  44. };
  45. /* Domain hash table */
  46. /* XXX - updates should be so rare that having one spinlock for the entire
  47. * hash table should be okay */
  48. static DEFINE_SPINLOCK(netlbl_domhsh_lock);
  49. static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
  50. /* Default domain mapping */
  51. static DEFINE_SPINLOCK(netlbl_domhsh_def_lock);
  52. static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
  53. /*
  54. * Domain Hash Table Helper Functions
  55. */
  56. /**
  57. * netlbl_domhsh_free_entry - Frees a domain hash table entry
  58. * @entry: the entry's RCU field
  59. *
  60. * Description:
  61. * This function is designed to be used as a callback to the call_rcu()
  62. * function so that the memory allocated to a hash table entry can be released
  63. * safely.
  64. *
  65. */
  66. static void netlbl_domhsh_free_entry(struct rcu_head *entry)
  67. {
  68. struct netlbl_dom_map *ptr;
  69. ptr = container_of(entry, struct netlbl_dom_map, rcu);
  70. kfree(ptr->domain);
  71. kfree(ptr);
  72. }
  73. /**
  74. * netlbl_domhsh_hash - Hashing function for the domain hash table
  75. * @domain: the domain name to hash
  76. *
  77. * Description:
  78. * This is the hashing function for the domain hash table, it returns the
  79. * correct bucket number for the domain. The caller is responsibile for
  80. * calling the rcu_read_[un]lock() functions.
  81. *
  82. */
  83. static u32 netlbl_domhsh_hash(const char *key)
  84. {
  85. u32 iter;
  86. u32 val;
  87. u32 len;
  88. /* This is taken (with slight modification) from
  89. * security/selinux/ss/symtab.c:symhash() */
  90. for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
  91. val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
  92. return val & (rcu_dereference(netlbl_domhsh)->size - 1);
  93. }
  94. /**
  95. * netlbl_domhsh_search - Search for a domain entry
  96. * @domain: the domain
  97. * @def: return default if no match is found
  98. *
  99. * Description:
  100. * Searches the domain hash table and returns a pointer to the hash table
  101. * entry if found, otherwise NULL is returned. If @def is non-zero and a
  102. * match is not found in the domain hash table the default mapping is returned
  103. * if it exists. The caller is responsibile for the rcu hash table locks
  104. * (i.e. the caller much call rcu_read_[un]lock()).
  105. *
  106. */
  107. static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain, u32 def)
  108. {
  109. u32 bkt;
  110. struct netlbl_dom_map *iter;
  111. if (domain != NULL) {
  112. bkt = netlbl_domhsh_hash(domain);
  113. list_for_each_entry_rcu(iter, &netlbl_domhsh->tbl[bkt], list)
  114. if (iter->valid && strcmp(iter->domain, domain) == 0)
  115. return iter;
  116. }
  117. if (def != 0) {
  118. iter = rcu_dereference(netlbl_domhsh_def);
  119. if (iter != NULL && iter->valid)
  120. return iter;
  121. }
  122. return NULL;
  123. }
  124. /*
  125. * Domain Hash Table Functions
  126. */
  127. /**
  128. * netlbl_domhsh_init - Init for the domain hash
  129. * @size: the number of bits to use for the hash buckets
  130. *
  131. * Description:
  132. * Initializes the domain hash table, should be called only by
  133. * netlbl_user_init() during initialization. Returns zero on success, non-zero
  134. * values on error.
  135. *
  136. */
  137. int netlbl_domhsh_init(u32 size)
  138. {
  139. u32 iter;
  140. struct netlbl_domhsh_tbl *hsh_tbl;
  141. if (size == 0)
  142. return -EINVAL;
  143. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  144. if (hsh_tbl == NULL)
  145. return -ENOMEM;
  146. hsh_tbl->size = 1 << size;
  147. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  148. sizeof(struct list_head),
  149. GFP_KERNEL);
  150. if (hsh_tbl->tbl == NULL) {
  151. kfree(hsh_tbl);
  152. return -ENOMEM;
  153. }
  154. for (iter = 0; iter < hsh_tbl->size; iter++)
  155. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  156. rcu_read_lock();
  157. spin_lock(&netlbl_domhsh_lock);
  158. rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
  159. spin_unlock(&netlbl_domhsh_lock);
  160. rcu_read_unlock();
  161. return 0;
  162. }
  163. /**
  164. * netlbl_domhsh_add - Adds a entry to the domain hash table
  165. * @entry: the entry to add
  166. *
  167. * Description:
  168. * Adds a new entry to the domain hash table and handles any updates to the
  169. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  170. * negative on failure.
  171. *
  172. */
  173. int netlbl_domhsh_add(struct netlbl_dom_map *entry)
  174. {
  175. int ret_val;
  176. u32 bkt;
  177. switch (entry->type) {
  178. case NETLBL_NLTYPE_UNLABELED:
  179. ret_val = 0;
  180. break;
  181. case NETLBL_NLTYPE_CIPSOV4:
  182. ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4,
  183. entry->domain);
  184. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. if (ret_val != 0)
  189. return ret_val;
  190. entry->valid = 1;
  191. INIT_RCU_HEAD(&entry->rcu);
  192. ret_val = 0;
  193. rcu_read_lock();
  194. if (entry->domain != NULL) {
  195. bkt = netlbl_domhsh_hash(entry->domain);
  196. spin_lock(&netlbl_domhsh_lock);
  197. if (netlbl_domhsh_search(entry->domain, 0) == NULL)
  198. list_add_tail_rcu(&entry->list,
  199. &netlbl_domhsh->tbl[bkt]);
  200. else
  201. ret_val = -EEXIST;
  202. spin_unlock(&netlbl_domhsh_lock);
  203. } else if (entry->domain == NULL) {
  204. INIT_LIST_HEAD(&entry->list);
  205. spin_lock(&netlbl_domhsh_def_lock);
  206. if (rcu_dereference(netlbl_domhsh_def) == NULL)
  207. rcu_assign_pointer(netlbl_domhsh_def, entry);
  208. else
  209. ret_val = -EEXIST;
  210. spin_unlock(&netlbl_domhsh_def_lock);
  211. } else
  212. ret_val = -EINVAL;
  213. rcu_read_unlock();
  214. if (ret_val != 0) {
  215. switch (entry->type) {
  216. case NETLBL_NLTYPE_CIPSOV4:
  217. if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
  218. entry->domain) != 0)
  219. BUG();
  220. break;
  221. }
  222. }
  223. return ret_val;
  224. }
  225. /**
  226. * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
  227. * @entry: the entry to add
  228. *
  229. * Description:
  230. * Adds a new default entry to the domain hash table and handles any updates
  231. * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
  232. * negative on failure.
  233. *
  234. */
  235. int netlbl_domhsh_add_default(struct netlbl_dom_map *entry)
  236. {
  237. return netlbl_domhsh_add(entry);
  238. }
  239. /**
  240. * netlbl_domhsh_remove - Removes an entry from the domain hash table
  241. * @domain: the domain to remove
  242. *
  243. * Description:
  244. * Removes an entry from the domain hash table and handles any updates to the
  245. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  246. * negative on failure.
  247. *
  248. */
  249. int netlbl_domhsh_remove(const char *domain)
  250. {
  251. int ret_val = -ENOENT;
  252. struct netlbl_dom_map *entry;
  253. rcu_read_lock();
  254. if (domain != NULL)
  255. entry = netlbl_domhsh_search(domain, 0);
  256. else
  257. entry = netlbl_domhsh_search(domain, 1);
  258. if (entry == NULL)
  259. goto remove_return;
  260. switch (entry->type) {
  261. case NETLBL_NLTYPE_UNLABELED:
  262. break;
  263. case NETLBL_NLTYPE_CIPSOV4:
  264. ret_val = cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
  265. entry->domain);
  266. if (ret_val != 0)
  267. goto remove_return;
  268. break;
  269. }
  270. ret_val = 0;
  271. if (entry != rcu_dereference(netlbl_domhsh_def)) {
  272. spin_lock(&netlbl_domhsh_lock);
  273. if (entry->valid) {
  274. entry->valid = 0;
  275. list_del_rcu(&entry->list);
  276. } else
  277. ret_val = -ENOENT;
  278. spin_unlock(&netlbl_domhsh_lock);
  279. } else {
  280. spin_lock(&netlbl_domhsh_def_lock);
  281. if (entry->valid) {
  282. entry->valid = 0;
  283. rcu_assign_pointer(netlbl_domhsh_def, NULL);
  284. } else
  285. ret_val = -ENOENT;
  286. spin_unlock(&netlbl_domhsh_def_lock);
  287. }
  288. if (ret_val == 0)
  289. call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
  290. remove_return:
  291. rcu_read_unlock();
  292. return ret_val;
  293. }
  294. /**
  295. * netlbl_domhsh_remove_default - Removes the default entry from the table
  296. *
  297. * Description:
  298. * Removes/resets the default entry for the domain hash table and handles any
  299. * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
  300. * success, non-zero on failure.
  301. *
  302. */
  303. int netlbl_domhsh_remove_default(void)
  304. {
  305. return netlbl_domhsh_remove(NULL);
  306. }
  307. /**
  308. * netlbl_domhsh_getentry - Get an entry from the domain hash table
  309. * @domain: the domain name to search for
  310. *
  311. * Description:
  312. * Look through the domain hash table searching for an entry to match @domain,
  313. * return a pointer to a copy of the entry or NULL. The caller is responsibile
  314. * for ensuring that rcu_read_[un]lock() is called.
  315. *
  316. */
  317. struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
  318. {
  319. return netlbl_domhsh_search(domain, 1);
  320. }
  321. /**
  322. * netlbl_domhsh_dump - Dump the domain hash table into a sk_buff
  323. *
  324. * Description:
  325. * Dump the domain hash table into a buffer suitable for returning to an
  326. * application in response to a NetLabel management DOMAIN message. This
  327. * function may fail if another process is growing the hash table at the same
  328. * time. The returned sk_buff has room at the front of the sk_buff for
  329. * @headroom bytes. See netlabel.h for the DOMAIN message format. Returns a
  330. * pointer to a sk_buff on success, NULL on error.
  331. *
  332. */
  333. struct sk_buff *netlbl_domhsh_dump(size_t headroom)
  334. {
  335. struct sk_buff *skb = NULL;
  336. ssize_t buf_len;
  337. u32 bkt_iter;
  338. u32 dom_cnt = 0;
  339. struct netlbl_domhsh_tbl *hsh_tbl;
  340. struct netlbl_dom_map *list_iter;
  341. ssize_t tmp_len;
  342. buf_len = NETLBL_LEN_U32;
  343. rcu_read_lock();
  344. hsh_tbl = rcu_dereference(netlbl_domhsh);
  345. for (bkt_iter = 0; bkt_iter < hsh_tbl->size; bkt_iter++)
  346. list_for_each_entry_rcu(list_iter,
  347. &hsh_tbl->tbl[bkt_iter], list) {
  348. buf_len += NETLBL_LEN_U32 +
  349. nla_total_size(strlen(list_iter->domain) + 1);
  350. switch (list_iter->type) {
  351. case NETLBL_NLTYPE_UNLABELED:
  352. break;
  353. case NETLBL_NLTYPE_CIPSOV4:
  354. buf_len += 2 * NETLBL_LEN_U32;
  355. break;
  356. }
  357. dom_cnt++;
  358. }
  359. skb = netlbl_netlink_alloc_skb(headroom, buf_len, GFP_ATOMIC);
  360. if (skb == NULL)
  361. goto dump_failure;
  362. if (nla_put_u32(skb, NLA_U32, dom_cnt) != 0)
  363. goto dump_failure;
  364. buf_len -= NETLBL_LEN_U32;
  365. hsh_tbl = rcu_dereference(netlbl_domhsh);
  366. for (bkt_iter = 0; bkt_iter < hsh_tbl->size; bkt_iter++)
  367. list_for_each_entry_rcu(list_iter,
  368. &hsh_tbl->tbl[bkt_iter], list) {
  369. tmp_len = nla_total_size(strlen(list_iter->domain) +
  370. 1);
  371. if (buf_len < NETLBL_LEN_U32 + tmp_len)
  372. goto dump_failure;
  373. if (nla_put_string(skb,
  374. NLA_STRING,
  375. list_iter->domain) != 0)
  376. goto dump_failure;
  377. if (nla_put_u32(skb, NLA_U32, list_iter->type) != 0)
  378. goto dump_failure;
  379. buf_len -= NETLBL_LEN_U32 + tmp_len;
  380. switch (list_iter->type) {
  381. case NETLBL_NLTYPE_UNLABELED:
  382. break;
  383. case NETLBL_NLTYPE_CIPSOV4:
  384. if (buf_len < 2 * NETLBL_LEN_U32)
  385. goto dump_failure;
  386. if (nla_put_u32(skb,
  387. NLA_U32,
  388. list_iter->type_def.cipsov4->type) != 0)
  389. goto dump_failure;
  390. if (nla_put_u32(skb,
  391. NLA_U32,
  392. list_iter->type_def.cipsov4->doi) != 0)
  393. goto dump_failure;
  394. buf_len -= 2 * NETLBL_LEN_U32;
  395. break;
  396. }
  397. }
  398. rcu_read_unlock();
  399. return skb;
  400. dump_failure:
  401. rcu_read_unlock();
  402. kfree_skb(skb);
  403. return NULL;
  404. }
  405. /**
  406. * netlbl_domhsh_dump_default - Dump the default domain mapping into a sk_buff
  407. *
  408. * Description:
  409. * Dump the default domain mapping into a buffer suitable for returning to an
  410. * application in response to a NetLabel management DEFDOMAIN message. This
  411. * function may fail if another process is changing the default domain mapping
  412. * at the same time. The returned sk_buff has room at the front of the
  413. * skb_buff for @headroom bytes. See netlabel.h for the DEFDOMAIN message
  414. * format. Returns a pointer to a sk_buff on success, NULL on error.
  415. *
  416. */
  417. struct sk_buff *netlbl_domhsh_dump_default(size_t headroom)
  418. {
  419. struct sk_buff *skb;
  420. ssize_t buf_len;
  421. struct netlbl_dom_map *entry;
  422. buf_len = NETLBL_LEN_U32;
  423. rcu_read_lock();
  424. entry = rcu_dereference(netlbl_domhsh_def);
  425. if (entry != NULL)
  426. switch (entry->type) {
  427. case NETLBL_NLTYPE_UNLABELED:
  428. break;
  429. case NETLBL_NLTYPE_CIPSOV4:
  430. buf_len += 2 * NETLBL_LEN_U32;
  431. break;
  432. }
  433. skb = netlbl_netlink_alloc_skb(headroom, buf_len, GFP_ATOMIC);
  434. if (skb == NULL)
  435. goto dump_default_failure;
  436. if (entry != rcu_dereference(netlbl_domhsh_def))
  437. goto dump_default_failure;
  438. if (entry != NULL) {
  439. if (nla_put_u32(skb, NLA_U32, entry->type) != 0)
  440. goto dump_default_failure;
  441. buf_len -= NETLBL_LEN_U32;
  442. switch (entry->type) {
  443. case NETLBL_NLTYPE_UNLABELED:
  444. break;
  445. case NETLBL_NLTYPE_CIPSOV4:
  446. if (buf_len < 2 * NETLBL_LEN_U32)
  447. goto dump_default_failure;
  448. if (nla_put_u32(skb,
  449. NLA_U32,
  450. entry->type_def.cipsov4->type) != 0)
  451. goto dump_default_failure;
  452. if (nla_put_u32(skb,
  453. NLA_U32,
  454. entry->type_def.cipsov4->doi) != 0)
  455. goto dump_default_failure;
  456. buf_len -= 2 * NETLBL_LEN_U32;
  457. break;
  458. }
  459. } else
  460. nla_put_u32(skb, NLA_U32, NETLBL_NLTYPE_NONE);
  461. rcu_read_unlock();
  462. return skb;
  463. dump_default_failure:
  464. rcu_read_unlock();
  465. kfree_skb(skb);
  466. return NULL;
  467. }