Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / netlabel / netlabel_domainhash.c
blobf99c31fc491f25f90344df0754a29509c5fb1e83
1 /*
2 * NetLabel Domain Hash Table
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.
9 * Author: Paul Moore <paul.moore@hp.com>
14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
24 * the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <linux/types.h>
33 #include <linux/rcupdate.h>
34 #include <linux/list.h>
35 #include <linux/skbuff.h>
36 #include <linux/spinlock.h>
37 #include <linux/string.h>
38 #include <linux/audit.h>
39 #include <net/netlabel.h>
40 #include <net/cipso_ipv4.h>
41 #include <asm/bug.h>
43 #include "netlabel_mgmt.h"
44 #include "netlabel_domainhash.h"
45 #include "netlabel_user.h"
47 struct netlbl_domhsh_tbl {
48 struct list_head *tbl;
49 u32 size;
52 /* Domain hash table */
53 /* XXX - updates should be so rare that having one spinlock for the entire
54 * hash table should be okay */
55 static DEFINE_SPINLOCK(netlbl_domhsh_lock);
56 static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
57 static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
60 * Domain Hash Table Helper Functions
63 /**
64 * netlbl_domhsh_free_entry - Frees a domain hash table entry
65 * @entry: the entry's RCU field
67 * Description:
68 * This function is designed to be used as a callback to the call_rcu()
69 * function so that the memory allocated to a hash table entry can be released
70 * safely.
73 static void netlbl_domhsh_free_entry(struct rcu_head *entry)
75 struct netlbl_dom_map *ptr;
77 ptr = container_of(entry, struct netlbl_dom_map, rcu);
78 kfree(ptr->domain);
79 kfree(ptr);
82 /**
83 * netlbl_domhsh_hash - Hashing function for the domain hash table
84 * @domain: the domain name to hash
86 * Description:
87 * This is the hashing function for the domain hash table, it returns the
88 * correct bucket number for the domain. The caller is responsibile for
89 * calling the rcu_read_[un]lock() functions.
92 static u32 netlbl_domhsh_hash(const char *key)
94 u32 iter;
95 u32 val;
96 u32 len;
98 /* This is taken (with slight modification) from
99 * security/selinux/ss/symtab.c:symhash() */
101 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
102 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
103 return val & (rcu_dereference(netlbl_domhsh)->size - 1);
107 * netlbl_domhsh_search - Search for a domain entry
108 * @domain: the domain
110 * Description:
111 * Searches the domain hash table and returns a pointer to the hash table
112 * entry if found, otherwise NULL is returned. The caller is responsibile for
113 * the rcu hash table locks (i.e. the caller much call rcu_read_[un]lock()).
116 static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
118 u32 bkt;
119 struct netlbl_dom_map *iter;
121 if (domain != NULL) {
122 bkt = netlbl_domhsh_hash(domain);
123 list_for_each_entry_rcu(iter,
124 &rcu_dereference(netlbl_domhsh)->tbl[bkt],
125 list)
126 if (iter->valid && strcmp(iter->domain, domain) == 0)
127 return iter;
130 return NULL;
134 * netlbl_domhsh_search_def - Search for a domain entry
135 * @domain: the domain
136 * @def: return default if no match is found
138 * Description:
139 * Searches the domain hash table and returns a pointer to the hash table
140 * entry if an exact match is found, if an exact match is not present in the
141 * hash table then the default entry is returned if valid otherwise NULL is
142 * returned. The caller is responsibile for the rcu hash table locks
143 * (i.e. the caller much call rcu_read_[un]lock()).
146 static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
148 struct netlbl_dom_map *entry;
150 entry = netlbl_domhsh_search(domain);
151 if (entry == NULL) {
152 entry = rcu_dereference(netlbl_domhsh_def);
153 if (entry != NULL && !entry->valid)
154 entry = NULL;
157 return entry;
161 * Domain Hash Table Functions
165 * netlbl_domhsh_init - Init for the domain hash
166 * @size: the number of bits to use for the hash buckets
168 * Description:
169 * Initializes the domain hash table, should be called only by
170 * netlbl_user_init() during initialization. Returns zero on success, non-zero
171 * values on error.
174 <<<<<<< HEAD:net/netlabel/netlabel_domainhash.c
175 int netlbl_domhsh_init(u32 size)
176 =======
177 int __init netlbl_domhsh_init(u32 size)
178 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/netlabel/netlabel_domainhash.c
180 u32 iter;
181 struct netlbl_domhsh_tbl *hsh_tbl;
183 if (size == 0)
184 return -EINVAL;
186 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
187 if (hsh_tbl == NULL)
188 return -ENOMEM;
189 hsh_tbl->size = 1 << size;
190 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
191 sizeof(struct list_head),
192 GFP_KERNEL);
193 if (hsh_tbl->tbl == NULL) {
194 kfree(hsh_tbl);
195 return -ENOMEM;
197 for (iter = 0; iter < hsh_tbl->size; iter++)
198 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
200 spin_lock(&netlbl_domhsh_lock);
201 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
202 spin_unlock(&netlbl_domhsh_lock);
204 return 0;
208 * netlbl_domhsh_add - Adds a entry to the domain hash table
209 * @entry: the entry to add
210 * @audit_info: NetLabel audit information
212 * Description:
213 * Adds a new entry to the domain hash table and handles any updates to the
214 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
215 * negative on failure.
218 int netlbl_domhsh_add(struct netlbl_dom_map *entry,
219 struct netlbl_audit *audit_info)
221 int ret_val;
222 u32 bkt;
223 struct audit_buffer *audit_buf;
225 switch (entry->type) {
226 case NETLBL_NLTYPE_UNLABELED:
227 ret_val = 0;
228 break;
229 case NETLBL_NLTYPE_CIPSOV4:
230 ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4,
231 entry->domain);
232 break;
233 default:
234 return -EINVAL;
236 if (ret_val != 0)
237 return ret_val;
239 entry->valid = 1;
240 INIT_RCU_HEAD(&entry->rcu);
242 rcu_read_lock();
243 spin_lock(&netlbl_domhsh_lock);
244 if (entry->domain != NULL) {
245 bkt = netlbl_domhsh_hash(entry->domain);
246 if (netlbl_domhsh_search(entry->domain) == NULL)
247 list_add_tail_rcu(&entry->list,
248 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
249 else
250 ret_val = -EEXIST;
251 } else {
252 INIT_LIST_HEAD(&entry->list);
253 if (rcu_dereference(netlbl_domhsh_def) == NULL)
254 rcu_assign_pointer(netlbl_domhsh_def, entry);
255 else
256 ret_val = -EEXIST;
258 spin_unlock(&netlbl_domhsh_lock);
259 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
260 if (audit_buf != NULL) {
261 audit_log_format(audit_buf,
262 " nlbl_domain=%s",
263 entry->domain ? entry->domain : "(default)");
264 switch (entry->type) {
265 case NETLBL_NLTYPE_UNLABELED:
266 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
267 break;
268 case NETLBL_NLTYPE_CIPSOV4:
269 audit_log_format(audit_buf,
270 " nlbl_protocol=cipsov4 cipso_doi=%u",
271 entry->type_def.cipsov4->doi);
272 break;
274 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
275 audit_log_end(audit_buf);
277 rcu_read_unlock();
279 if (ret_val != 0) {
280 switch (entry->type) {
281 case NETLBL_NLTYPE_CIPSOV4:
282 if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
283 entry->domain) != 0)
284 BUG();
285 break;
289 return ret_val;
293 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
294 * @entry: the entry to add
295 * @audit_info: NetLabel audit information
297 * Description:
298 * Adds a new default entry to the domain hash table and handles any updates
299 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
300 * negative on failure.
303 int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
304 struct netlbl_audit *audit_info)
306 return netlbl_domhsh_add(entry, audit_info);
310 * netlbl_domhsh_remove - Removes an entry from the domain hash table
311 * @domain: the domain to remove
312 * @audit_info: NetLabel audit information
314 * Description:
315 * Removes an entry from the domain hash table and handles any updates to the
316 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
317 * negative on failure.
320 int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
322 int ret_val = -ENOENT;
323 struct netlbl_dom_map *entry;
324 struct audit_buffer *audit_buf;
326 rcu_read_lock();
327 if (domain)
328 entry = netlbl_domhsh_search(domain);
329 else
330 entry = netlbl_domhsh_search_def(domain);
331 if (entry == NULL)
332 goto remove_return;
333 switch (entry->type) {
334 case NETLBL_NLTYPE_CIPSOV4:
335 cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
336 entry->domain);
337 break;
339 spin_lock(&netlbl_domhsh_lock);
340 if (entry->valid) {
341 entry->valid = 0;
342 if (entry != rcu_dereference(netlbl_domhsh_def))
343 list_del_rcu(&entry->list);
344 else
345 rcu_assign_pointer(netlbl_domhsh_def, NULL);
346 ret_val = 0;
348 spin_unlock(&netlbl_domhsh_lock);
350 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
351 if (audit_buf != NULL) {
352 audit_log_format(audit_buf,
353 " nlbl_domain=%s res=%u",
354 entry->domain ? entry->domain : "(default)",
355 ret_val == 0 ? 1 : 0);
356 audit_log_end(audit_buf);
359 remove_return:
360 rcu_read_unlock();
361 if (ret_val == 0)
362 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
363 return ret_val;
367 * netlbl_domhsh_remove_default - Removes the default entry from the table
368 * @audit_info: NetLabel audit information
370 * Description:
371 * Removes/resets the default entry for the domain hash table and handles any
372 * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
373 * success, non-zero on failure.
376 int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
378 return netlbl_domhsh_remove(NULL, audit_info);
382 * netlbl_domhsh_getentry - Get an entry from the domain hash table
383 * @domain: the domain name to search for
385 * Description:
386 * Look through the domain hash table searching for an entry to match @domain,
387 * return a pointer to a copy of the entry or NULL. The caller is responsibile
388 * for ensuring that rcu_read_[un]lock() is called.
391 struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
393 return netlbl_domhsh_search_def(domain);
397 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
398 * @skip_bkt: the number of buckets to skip at the start
399 * @skip_chain: the number of entries to skip in the first iterated bucket
400 * @callback: callback for each entry
401 * @cb_arg: argument for the callback function
403 * Description:
404 * Interate over the domain mapping hash table, skipping the first @skip_bkt
405 * buckets and @skip_chain entries. For each entry in the table call
406 * @callback, if @callback returns a negative value stop 'walking' through the
407 * table and return. Updates the values in @skip_bkt and @skip_chain on
408 * return. Returns zero on succcess, negative values on failure.
411 int netlbl_domhsh_walk(u32 *skip_bkt,
412 u32 *skip_chain,
413 int (*callback) (struct netlbl_dom_map *entry, void *arg),
414 void *cb_arg)
416 int ret_val = -ENOENT;
417 u32 iter_bkt;
418 struct netlbl_dom_map *iter_entry;
419 u32 chain_cnt = 0;
421 rcu_read_lock();
422 for (iter_bkt = *skip_bkt;
423 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
424 iter_bkt++, chain_cnt = 0) {
425 list_for_each_entry_rcu(iter_entry,
426 &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt],
427 list)
428 if (iter_entry->valid) {
429 if (chain_cnt++ < *skip_chain)
430 continue;
431 ret_val = callback(iter_entry, cb_arg);
432 if (ret_val < 0) {
433 chain_cnt--;
434 goto walk_return;
439 walk_return:
440 rcu_read_unlock();
441 *skip_bkt = iter_bkt;
442 *skip_chain = chain_cnt;
443 return ret_val;