netlabel_domainhash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/rculist.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/string.h>
  35. #include <linux/audit.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. #include "netlabel_user.h"
  42. struct netlbl_domhsh_tbl {
  43. struct list_head *tbl;
  44. u32 size;
  45. };
  46. /* Domain hash table */
  47. /* XXX - updates should be so rare that having one spinlock for the entire
  48. * hash table should be okay */
  49. static DEFINE_SPINLOCK(netlbl_domhsh_lock);
  50. static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
  51. static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
  52. /*
  53. * Domain Hash Table Helper Functions
  54. */
  55. /**
  56. * netlbl_domhsh_free_entry - Frees a domain hash table entry
  57. * @entry: the entry's RCU field
  58. *
  59. * Description:
  60. * This function is designed to be used as a callback to the call_rcu()
  61. * function so that the memory allocated to a hash table entry can be released
  62. * safely.
  63. *
  64. */
  65. static void netlbl_domhsh_free_entry(struct rcu_head *entry)
  66. {
  67. struct netlbl_dom_map *ptr;
  68. ptr = container_of(entry, struct netlbl_dom_map, rcu);
  69. kfree(ptr->domain);
  70. kfree(ptr);
  71. }
  72. /**
  73. * netlbl_domhsh_hash - Hashing function for the domain hash table
  74. * @domain: the domain name to hash
  75. *
  76. * Description:
  77. * This is the hashing function for the domain hash table, it returns the
  78. * correct bucket number for the domain. The caller is responsibile for
  79. * calling the rcu_read_[un]lock() functions.
  80. *
  81. */
  82. static u32 netlbl_domhsh_hash(const char *key)
  83. {
  84. u32 iter;
  85. u32 val;
  86. u32 len;
  87. /* This is taken (with slight modification) from
  88. * security/selinux/ss/symtab.c:symhash() */
  89. for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
  90. val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
  91. return val & (rcu_dereference(netlbl_domhsh)->size - 1);
  92. }
  93. /**
  94. * netlbl_domhsh_search - Search for a domain entry
  95. * @domain: the domain
  96. *
  97. * Description:
  98. * Searches the domain hash table and returns a pointer to the hash table
  99. * entry if found, otherwise NULL is returned. The caller is responsibile for
  100. * the rcu hash table locks (i.e. the caller much call rcu_read_[un]lock()).
  101. *
  102. */
  103. static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
  104. {
  105. u32 bkt;
  106. struct netlbl_dom_map *iter;
  107. if (domain != NULL) {
  108. bkt = netlbl_domhsh_hash(domain);
  109. list_for_each_entry_rcu(iter,
  110. &rcu_dereference(netlbl_domhsh)->tbl[bkt],
  111. list)
  112. if (iter->valid && strcmp(iter->domain, domain) == 0)
  113. return iter;
  114. }
  115. return NULL;
  116. }
  117. /**
  118. * netlbl_domhsh_search_def - Search for a domain entry
  119. * @domain: the domain
  120. * @def: return default if no match is found
  121. *
  122. * Description:
  123. * Searches the domain hash table and returns a pointer to the hash table
  124. * entry if an exact match is found, if an exact match is not present in the
  125. * hash table then the default entry is returned if valid otherwise NULL is
  126. * returned. The caller is responsibile for the rcu hash table locks
  127. * (i.e. the caller much call rcu_read_[un]lock()).
  128. *
  129. */
  130. static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
  131. {
  132. struct netlbl_dom_map *entry;
  133. entry = netlbl_domhsh_search(domain);
  134. if (entry == NULL) {
  135. entry = rcu_dereference(netlbl_domhsh_def);
  136. if (entry != NULL && !entry->valid)
  137. entry = NULL;
  138. }
  139. return entry;
  140. }
  141. /*
  142. * Domain Hash Table Functions
  143. */
  144. /**
  145. * netlbl_domhsh_init - Init for the domain hash
  146. * @size: the number of bits to use for the hash buckets
  147. *
  148. * Description:
  149. * Initializes the domain hash table, should be called only by
  150. * netlbl_user_init() during initialization. Returns zero on success, non-zero
  151. * values on error.
  152. *
  153. */
  154. int __init netlbl_domhsh_init(u32 size)
  155. {
  156. u32 iter;
  157. struct netlbl_domhsh_tbl *hsh_tbl;
  158. if (size == 0)
  159. return -EINVAL;
  160. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  161. if (hsh_tbl == NULL)
  162. return -ENOMEM;
  163. hsh_tbl->size = 1 << size;
  164. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  165. sizeof(struct list_head),
  166. GFP_KERNEL);
  167. if (hsh_tbl->tbl == NULL) {
  168. kfree(hsh_tbl);
  169. return -ENOMEM;
  170. }
  171. for (iter = 0; iter < hsh_tbl->size; iter++)
  172. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  173. spin_lock(&netlbl_domhsh_lock);
  174. rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
  175. spin_unlock(&netlbl_domhsh_lock);
  176. return 0;
  177. }
  178. /**
  179. * netlbl_domhsh_add - Adds a entry to the domain hash table
  180. * @entry: the entry to add
  181. * @audit_info: NetLabel audit information
  182. *
  183. * Description:
  184. * Adds a new entry to the domain hash table and handles any updates to the
  185. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  186. * negative on failure.
  187. *
  188. */
  189. int netlbl_domhsh_add(struct netlbl_dom_map *entry,
  190. struct netlbl_audit *audit_info)
  191. {
  192. int ret_val;
  193. u32 bkt;
  194. struct audit_buffer *audit_buf;
  195. switch (entry->type) {
  196. case NETLBL_NLTYPE_UNLABELED:
  197. ret_val = 0;
  198. break;
  199. case NETLBL_NLTYPE_CIPSOV4:
  200. ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4,
  201. entry->domain);
  202. break;
  203. default:
  204. return -EINVAL;
  205. }
  206. if (ret_val != 0)
  207. return ret_val;
  208. entry->valid = 1;
  209. INIT_RCU_HEAD(&entry->rcu);
  210. rcu_read_lock();
  211. spin_lock(&netlbl_domhsh_lock);
  212. if (entry->domain != NULL) {
  213. bkt = netlbl_domhsh_hash(entry->domain);
  214. if (netlbl_domhsh_search(entry->domain) == NULL)
  215. list_add_tail_rcu(&entry->list,
  216. &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
  217. else
  218. ret_val = -EEXIST;
  219. } else {
  220. INIT_LIST_HEAD(&entry->list);
  221. if (rcu_dereference(netlbl_domhsh_def) == NULL)
  222. rcu_assign_pointer(netlbl_domhsh_def, entry);
  223. else
  224. ret_val = -EEXIST;
  225. }
  226. spin_unlock(&netlbl_domhsh_lock);
  227. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
  228. if (audit_buf != NULL) {
  229. audit_log_format(audit_buf,
  230. " nlbl_domain=%s",
  231. entry->domain ? entry->domain : "(default)");
  232. switch (entry->type) {
  233. case NETLBL_NLTYPE_UNLABELED:
  234. audit_log_format(audit_buf, " nlbl_protocol=unlbl");
  235. break;
  236. case NETLBL_NLTYPE_CIPSOV4:
  237. audit_log_format(audit_buf,
  238. " nlbl_protocol=cipsov4 cipso_doi=%u",
  239. entry->type_def.cipsov4->doi);
  240. break;
  241. }
  242. audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
  243. audit_log_end(audit_buf);
  244. }
  245. rcu_read_unlock();
  246. if (ret_val != 0) {
  247. switch (entry->type) {
  248. case NETLBL_NLTYPE_CIPSOV4:
  249. if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
  250. entry->domain) != 0)
  251. BUG();
  252. break;
  253. }
  254. }
  255. return ret_val;
  256. }
  257. /**
  258. * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
  259. * @entry: the entry to add
  260. * @audit_info: NetLabel audit information
  261. *
  262. * Description:
  263. * Adds a new default entry to the domain hash table and handles any updates
  264. * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
  265. * negative on failure.
  266. *
  267. */
  268. int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
  269. struct netlbl_audit *audit_info)
  270. {
  271. return netlbl_domhsh_add(entry, audit_info);
  272. }
  273. /**
  274. * netlbl_domhsh_remove - Removes an entry from the domain hash table
  275. * @domain: the domain to remove
  276. * @audit_info: NetLabel audit information
  277. *
  278. * Description:
  279. * Removes an entry from the domain hash table and handles any updates to the
  280. * lower level protocol handler (i.e. CIPSO). Returns zero on success,
  281. * negative on failure.
  282. *
  283. */
  284. int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
  285. {
  286. int ret_val = -ENOENT;
  287. struct netlbl_dom_map *entry;
  288. struct audit_buffer *audit_buf;
  289. rcu_read_lock();
  290. if (domain)
  291. entry = netlbl_domhsh_search(domain);
  292. else
  293. entry = netlbl_domhsh_search_def(domain);
  294. if (entry == NULL)
  295. goto remove_return;
  296. switch (entry->type) {
  297. case NETLBL_NLTYPE_CIPSOV4:
  298. cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
  299. entry->domain);
  300. break;
  301. }
  302. spin_lock(&netlbl_domhsh_lock);
  303. if (entry->valid) {
  304. entry->valid = 0;
  305. if (entry != rcu_dereference(netlbl_domhsh_def))
  306. list_del_rcu(&entry->list);
  307. else
  308. rcu_assign_pointer(netlbl_domhsh_def, NULL);
  309. ret_val = 0;
  310. }
  311. spin_unlock(&netlbl_domhsh_lock);
  312. audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
  313. if (audit_buf != NULL) {
  314. audit_log_format(audit_buf,
  315. " nlbl_domain=%s res=%u",
  316. entry->domain ? entry->domain : "(default)",
  317. ret_val == 0 ? 1 : 0);
  318. audit_log_end(audit_buf);
  319. }
  320. remove_return:
  321. rcu_read_unlock();
  322. if (ret_val == 0)
  323. call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
  324. return ret_val;
  325. }
  326. /**
  327. * netlbl_domhsh_remove_default - Removes the default entry from the table
  328. * @audit_info: NetLabel audit information
  329. *
  330. * Description:
  331. * Removes/resets the default entry for the domain hash table and handles any
  332. * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
  333. * success, non-zero on failure.
  334. *
  335. */
  336. int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
  337. {
  338. return netlbl_domhsh_remove(NULL, audit_info);
  339. }
  340. /**
  341. * netlbl_domhsh_getentry - Get an entry from the domain hash table
  342. * @domain: the domain name to search for
  343. *
  344. * Description:
  345. * Look through the domain hash table searching for an entry to match @domain,
  346. * return a pointer to a copy of the entry or NULL. The caller is responsibile
  347. * for ensuring that rcu_read_[un]lock() is called.
  348. *
  349. */
  350. struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
  351. {
  352. return netlbl_domhsh_search_def(domain);
  353. }
  354. /**
  355. * netlbl_domhsh_walk - Iterate through the domain mapping hash table
  356. * @skip_bkt: the number of buckets to skip at the start
  357. * @skip_chain: the number of entries to skip in the first iterated bucket
  358. * @callback: callback for each entry
  359. * @cb_arg: argument for the callback function
  360. *
  361. * Description:
  362. * Interate over the domain mapping hash table, skipping the first @skip_bkt
  363. * buckets and @skip_chain entries. For each entry in the table call
  364. * @callback, if @callback returns a negative value stop 'walking' through the
  365. * table and return. Updates the values in @skip_bkt and @skip_chain on
  366. * return. Returns zero on succcess, negative values on failure.
  367. *
  368. */
  369. int netlbl_domhsh_walk(u32 *skip_bkt,
  370. u32 *skip_chain,
  371. int (*callback) (struct netlbl_dom_map *entry, void *arg),
  372. void *cb_arg)
  373. {
  374. int ret_val = -ENOENT;
  375. u32 iter_bkt;
  376. struct netlbl_dom_map *iter_entry;
  377. u32 chain_cnt = 0;
  378. rcu_read_lock();
  379. for (iter_bkt = *skip_bkt;
  380. iter_bkt < rcu_dereference(netlbl_domhsh)->size;
  381. iter_bkt++, chain_cnt = 0) {
  382. list_for_each_entry_rcu(iter_entry,
  383. &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt],
  384. list)
  385. if (iter_entry->valid) {
  386. if (chain_cnt++ < *skip_chain)
  387. continue;
  388. ret_val = callback(iter_entry, cb_arg);
  389. if (ret_val < 0) {
  390. chain_cnt--;
  391. goto walk_return;
  392. }
  393. }
  394. }
  395. walk_return:
  396. rcu_read_unlock();
  397. *skip_bkt = iter_bkt;
  398. *skip_chain = chain_cnt;
  399. return ret_val;
  400. }