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
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 <net/netlabel.h>
39 #include <net/cipso_ipv4.h>
42 #include "netlabel_mgmt.h"
43 #include "netlabel_domainhash.h"
45 struct netlbl_domhsh_tbl {
46 struct list_head *tbl;
50 /* Domain hash table */
51 /* XXX - updates should be so rare that having one spinlock for the entire
52 * hash table should be okay */
53 static DEFINE_SPINLOCK(netlbl_domhsh_lock);
54 static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
56 /* Default domain mapping */
57 static DEFINE_SPINLOCK(netlbl_domhsh_def_lock);
58 static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
61 * Domain Hash Table Helper Functions
65 * netlbl_domhsh_free_entry - Frees a domain hash table entry
66 * @entry: the entry's RCU field
69 * This function is designed to be used as a callback to the call_rcu()
70 * function so that the memory allocated to a hash table entry can be released
74 static void netlbl_domhsh_free_entry(struct rcu_head *entry)
76 struct netlbl_dom_map *ptr;
78 ptr = container_of(entry, struct netlbl_dom_map, rcu);
84 * netlbl_domhsh_hash - Hashing function for the domain hash table
85 * @domain: the domain name to hash
88 * This is the hashing function for the domain hash table, it returns the
89 * correct bucket number for the domain. The caller is responsibile for
90 * calling the rcu_read_[un]lock() functions.
93 static u32 netlbl_domhsh_hash(const char *key)
99 /* This is taken (with slight modification) from
100 * security/selinux/ss/symtab.c:symhash() */
102 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
103 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
104 return val & (rcu_dereference(netlbl_domhsh)->size - 1);
108 * netlbl_domhsh_search - Search for a domain entry
109 * @domain: the domain
110 * @def: return default if no match is found
113 * Searches the domain hash table and returns a pointer to the hash table
114 * entry if found, otherwise NULL is returned. If @def is non-zero and a
115 * match is not found in the domain hash table the default mapping is returned
116 * if it exists. The caller is responsibile for the rcu hash table locks
117 * (i.e. the caller much call rcu_read_[un]lock()).
120 static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain, u32 def)
123 struct netlbl_dom_map *iter;
125 if (domain != NULL) {
126 bkt = netlbl_domhsh_hash(domain);
127 list_for_each_entry_rcu(iter, &netlbl_domhsh->tbl[bkt], list)
128 if (iter->valid && strcmp(iter->domain, domain) == 0)
133 iter = rcu_dereference(netlbl_domhsh_def);
134 if (iter != NULL && iter->valid)
142 * Domain Hash Table Functions
146 * netlbl_domhsh_init - Init for the domain hash
147 * @size: the number of bits to use for the hash buckets
150 * Initializes the domain hash table, should be called only by
151 * netlbl_user_init() during initialization. Returns zero on success, non-zero
155 int netlbl_domhsh_init(u32 size)
158 struct netlbl_domhsh_tbl *hsh_tbl;
163 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
166 hsh_tbl->size = 1 << size;
167 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
168 sizeof(struct list_head),
170 if (hsh_tbl->tbl == NULL) {
174 for (iter = 0; iter < hsh_tbl->size; iter++)
175 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
178 spin_lock(&netlbl_domhsh_lock);
179 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
180 spin_unlock(&netlbl_domhsh_lock);
187 * netlbl_domhsh_add - Adds a entry to the domain hash table
188 * @entry: the entry to add
191 * Adds a new entry to the domain hash table and handles any updates to the
192 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
193 * negative on failure.
196 int netlbl_domhsh_add(struct netlbl_dom_map *entry)
201 switch (entry->type) {
202 case NETLBL_NLTYPE_UNLABELED:
205 case NETLBL_NLTYPE_CIPSOV4:
206 ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4,
216 INIT_RCU_HEAD(&entry->rcu);
220 if (entry->domain != NULL) {
221 bkt = netlbl_domhsh_hash(entry->domain);
222 spin_lock(&netlbl_domhsh_lock);
223 if (netlbl_domhsh_search(entry->domain, 0) == NULL)
224 list_add_tail_rcu(&entry->list,
225 &netlbl_domhsh->tbl[bkt]);
228 spin_unlock(&netlbl_domhsh_lock);
229 } else if (entry->domain == NULL) {
230 INIT_LIST_HEAD(&entry->list);
231 spin_lock(&netlbl_domhsh_def_lock);
232 if (rcu_dereference(netlbl_domhsh_def) == NULL)
233 rcu_assign_pointer(netlbl_domhsh_def, entry);
236 spin_unlock(&netlbl_domhsh_def_lock);
242 switch (entry->type) {
243 case NETLBL_NLTYPE_CIPSOV4:
244 if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
255 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
256 * @entry: the entry to add
259 * Adds a new default entry to the domain hash table and handles any updates
260 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
261 * negative on failure.
264 int netlbl_domhsh_add_default(struct netlbl_dom_map *entry)
266 return netlbl_domhsh_add(entry);
270 * netlbl_domhsh_remove - Removes an entry from the domain hash table
271 * @domain: the domain to remove
274 * Removes an entry from the domain hash table and handles any updates to the
275 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
276 * negative on failure.
279 int netlbl_domhsh_remove(const char *domain)
281 int ret_val = -ENOENT;
282 struct netlbl_dom_map *entry;
286 entry = netlbl_domhsh_search(domain, 0);
288 entry = netlbl_domhsh_search(domain, 1);
291 switch (entry->type) {
292 case NETLBL_NLTYPE_UNLABELED:
294 case NETLBL_NLTYPE_CIPSOV4:
295 ret_val = cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
302 if (entry != rcu_dereference(netlbl_domhsh_def)) {
303 spin_lock(&netlbl_domhsh_lock);
306 list_del_rcu(&entry->list);
309 spin_unlock(&netlbl_domhsh_lock);
311 spin_lock(&netlbl_domhsh_def_lock);
314 rcu_assign_pointer(netlbl_domhsh_def, NULL);
317 spin_unlock(&netlbl_domhsh_def_lock);
320 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
328 * netlbl_domhsh_remove_default - Removes the default entry from the table
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.
336 int netlbl_domhsh_remove_default(void)
338 return netlbl_domhsh_remove(NULL);
342 * netlbl_domhsh_getentry - Get an entry from the domain hash table
343 * @domain: the domain name to search for
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.
351 struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
353 return netlbl_domhsh_search(domain, 1);
357 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
358 * @skip_bkt: the number of buckets to skip at the start
359 * @skip_chain: the number of entries to skip in the first iterated bucket
360 * @callback: callback for each entry
361 * @cb_arg: argument for the callback function
364 * Interate over the domain mapping hash table, skipping the first @skip_bkt
365 * buckets and @skip_chain entries. For each entry in the table call
366 * @callback, if @callback returns a negative value stop 'walking' through the
367 * table and return. Updates the values in @skip_bkt and @skip_chain on
368 * return. Returns zero on succcess, negative values on failure.
371 int netlbl_domhsh_walk(u32 *skip_bkt,
373 int (*callback) (struct netlbl_dom_map *entry, void *arg),
376 int ret_val = -ENOENT;
378 struct netlbl_dom_map *iter_entry;
382 for (iter_bkt = *skip_bkt;
383 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
384 iter_bkt++, chain_cnt = 0) {
385 list_for_each_entry_rcu(iter_entry,
386 &netlbl_domhsh->tbl[iter_bkt],
388 if (iter_entry->valid) {
389 if (chain_cnt++ < *skip_chain)
391 ret_val = callback(iter_entry, cb_arg);
401 *skip_bkt = iter_bkt;
402 *skip_chain = chain_cnt;