netlabel_domainhash.c 12 KB

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