[NetLabel]: correct usage of RCU locking
[safe/jmp/linux-2.6] / 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,
130                                      &rcu_dereference(netlbl_domhsh)->tbl[bkt],
131                                      list)
132                         if (iter->valid && strcmp(iter->domain, domain) == 0)
133                                 return iter;
134         }
135
136         if (def != 0) {
137                 iter = rcu_dereference(netlbl_domhsh_def);
138                 if (iter != NULL && iter->valid)
139                         return iter;
140         }
141
142         return NULL;
143 }
144
145 /*
146  * Domain Hash Table Functions
147  */
148
149 /**
150  * netlbl_domhsh_init - Init for the domain hash
151  * @size: the number of bits to use for the hash buckets
152  *
153  * Description:
154  * Initializes the domain hash table, should be called only by
155  * netlbl_user_init() during initialization.  Returns zero on success, non-zero
156  * values on error.
157  *
158  */
159 int netlbl_domhsh_init(u32 size)
160 {
161         u32 iter;
162         struct netlbl_domhsh_tbl *hsh_tbl;
163
164         if (size == 0)
165                 return -EINVAL;
166
167         hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
168         if (hsh_tbl == NULL)
169                 return -ENOMEM;
170         hsh_tbl->size = 1 << size;
171         hsh_tbl->tbl = kcalloc(hsh_tbl->size,
172                                sizeof(struct list_head),
173                                GFP_KERNEL);
174         if (hsh_tbl->tbl == NULL) {
175                 kfree(hsh_tbl);
176                 return -ENOMEM;
177         }
178         for (iter = 0; iter < hsh_tbl->size; iter++)
179                 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
180
181         spin_lock(&netlbl_domhsh_lock);
182         rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
183         spin_unlock(&netlbl_domhsh_lock);
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
206         switch (entry->type) {
207         case NETLBL_NLTYPE_UNLABELED:
208                 ret_val = 0;
209                 break;
210         case NETLBL_NLTYPE_CIPSOV4:
211                 ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4,
212                                                   entry->domain);
213                 break;
214         default:
215                 return -EINVAL;
216         }
217         if (ret_val != 0)
218                 return ret_val;
219
220         entry->valid = 1;
221         INIT_RCU_HEAD(&entry->rcu);
222
223         rcu_read_lock();
224         if (entry->domain != NULL) {
225                 bkt = netlbl_domhsh_hash(entry->domain);
226                 spin_lock(&netlbl_domhsh_lock);
227                 if (netlbl_domhsh_search(entry->domain, 0) == NULL)
228                         list_add_tail_rcu(&entry->list,
229                                     &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
230                 else
231                         ret_val = -EEXIST;
232                 spin_unlock(&netlbl_domhsh_lock);
233         } else {
234                 INIT_LIST_HEAD(&entry->list);
235                 spin_lock(&netlbl_domhsh_def_lock);
236                 if (rcu_dereference(netlbl_domhsh_def) == NULL)
237                         rcu_assign_pointer(netlbl_domhsh_def, entry);
238                 else
239                         ret_val = -EEXIST;
240                 spin_unlock(&netlbl_domhsh_def_lock);
241         }
242         audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
243         if (audit_buf != NULL) {
244                 audit_log_format(audit_buf,
245                                  " nlbl_domain=%s",
246                                  entry->domain ? entry->domain : "(default)");
247                 switch (entry->type) {
248                 case NETLBL_NLTYPE_UNLABELED:
249                         audit_log_format(audit_buf, " nlbl_protocol=unlbl");
250                         break;
251                 case NETLBL_NLTYPE_CIPSOV4:
252                         audit_log_format(audit_buf,
253                                          " nlbl_protocol=cipsov4 cipso_doi=%u",
254                                          entry->type_def.cipsov4->doi);
255                         break;
256                 }
257                 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
258                 audit_log_end(audit_buf);
259         }
260         rcu_read_unlock();
261
262         if (ret_val != 0) {
263                 switch (entry->type) {
264                 case NETLBL_NLTYPE_CIPSOV4:
265                         if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
266                                                        entry->domain) != 0)
267                                 BUG();
268                         break;
269                 }
270         }
271
272         return ret_val;
273 }
274
275 /**
276  * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
277  * @entry: the entry to add
278  * @audit_info: NetLabel audit information
279  *
280  * Description:
281  * Adds a new default entry to the domain hash table and handles any updates
282  * to the lower level protocol handler (i.e. CIPSO).  Returns zero on success,
283  * negative on failure.
284  *
285  */
286 int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
287                               struct netlbl_audit *audit_info)
288 {
289         return netlbl_domhsh_add(entry, audit_info);
290 }
291
292 /**
293  * netlbl_domhsh_remove - Removes an entry from the domain hash table
294  * @domain: the domain to remove
295  * @audit_info: NetLabel audit information
296  *
297  * Description:
298  * Removes an entry from the domain hash table and handles any updates to the
299  * lower level protocol handler (i.e. CIPSO).  Returns zero on success,
300  * negative on failure.
301  *
302  */
303 int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
304 {
305         int ret_val = -ENOENT;
306         struct netlbl_dom_map *entry;
307         struct audit_buffer *audit_buf;
308
309         rcu_read_lock();
310         entry = netlbl_domhsh_search(domain, (domain != NULL ? 0 : 1));
311         if (entry == NULL)
312                 goto remove_return;
313         switch (entry->type) {
314         case NETLBL_NLTYPE_CIPSOV4:
315                 cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4,
316                                            entry->domain);
317                 break;
318         }
319         if (entry != rcu_dereference(netlbl_domhsh_def)) {
320                 spin_lock(&netlbl_domhsh_lock);
321                 if (entry->valid) {
322                         entry->valid = 0;
323                         list_del_rcu(&entry->list);
324                         ret_val = 0;
325                 }
326                 spin_unlock(&netlbl_domhsh_lock);
327         } else {
328                 spin_lock(&netlbl_domhsh_def_lock);
329                 if (entry->valid) {
330                         entry->valid = 0;
331                         rcu_assign_pointer(netlbl_domhsh_def, NULL);
332                         ret_val = 0;
333                 }
334                 spin_unlock(&netlbl_domhsh_def_lock);
335         }
336
337         audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
338         if (audit_buf != NULL) {
339                 audit_log_format(audit_buf,
340                                  " nlbl_domain=%s res=%u",
341                                  entry->domain ? entry->domain : "(default)",
342                                  ret_val == 0 ? 1 : 0);
343                 audit_log_end(audit_buf);
344         }
345
346 remove_return:
347         rcu_read_unlock();
348         if (ret_val == 0)
349                 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
350         return ret_val;
351 }
352
353 /**
354  * netlbl_domhsh_remove_default - Removes the default entry from the table
355  * @audit_info: NetLabel audit information
356  *
357  * Description:
358  * Removes/resets the default entry for the domain hash table and handles any
359  * updates to the lower level protocol handler (i.e. CIPSO).  Returns zero on
360  * success, non-zero on failure.
361  *
362  */
363 int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
364 {
365         return netlbl_domhsh_remove(NULL, audit_info);
366 }
367
368 /**
369  * netlbl_domhsh_getentry - Get an entry from the domain hash table
370  * @domain: the domain name to search for
371  *
372  * Description:
373  * Look through the domain hash table searching for an entry to match @domain,
374  * return a pointer to a copy of the entry or NULL.  The caller is responsibile
375  * for ensuring that rcu_read_[un]lock() is called.
376  *
377  */
378 struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
379 {
380         return netlbl_domhsh_search(domain, 1);
381 }
382
383 /**
384  * netlbl_domhsh_walk - Iterate through the domain mapping hash table
385  * @skip_bkt: the number of buckets to skip at the start
386  * @skip_chain: the number of entries to skip in the first iterated bucket
387  * @callback: callback for each entry
388  * @cb_arg: argument for the callback function
389  *
390  * Description:
391  * Interate over the domain mapping hash table, skipping the first @skip_bkt
392  * buckets and @skip_chain entries.  For each entry in the table call
393  * @callback, if @callback returns a negative value stop 'walking' through the
394  * table and return.  Updates the values in @skip_bkt and @skip_chain on
395  * return.  Returns zero on succcess, negative values on failure.
396  *
397  */
398 int netlbl_domhsh_walk(u32 *skip_bkt,
399                      u32 *skip_chain,
400                      int (*callback) (struct netlbl_dom_map *entry, void *arg),
401                      void *cb_arg)
402 {
403         int ret_val = -ENOENT;
404         u32 iter_bkt;
405         struct netlbl_dom_map *iter_entry;
406         u32 chain_cnt = 0;
407
408         rcu_read_lock();
409         for (iter_bkt = *skip_bkt;
410              iter_bkt < rcu_dereference(netlbl_domhsh)->size;
411              iter_bkt++, chain_cnt = 0) {
412                 list_for_each_entry_rcu(iter_entry,
413                                 &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt],
414                                 list)
415                         if (iter_entry->valid) {
416                                 if (chain_cnt++ < *skip_chain)
417                                         continue;
418                                 ret_val = callback(iter_entry, cb_arg);
419                                 if (ret_val < 0) {
420                                         chain_cnt--;
421                                         goto walk_return;
422                                 }
423                         }
424         }
425
426 walk_return:
427         rcu_read_unlock();
428         *skip_bkt = iter_bkt;
429         *skip_chain = chain_cnt;
430         return ret_val;
431 }