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