blob: 0243f0c57b4157037b7d64d191b6652758472c7d [file] [log] [blame]
Paul Moored15c3452006-08-03 16:48:37 -07001/*
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>
Franck Bui-Huu82524742008-05-12 21:21:05 +020033#include <linux/rculist.h>
Paul Moored15c3452006-08-03 16:48:37 -070034#include <linux/skbuff.h>
35#include <linux/spinlock.h>
36#include <linux/string.h>
Paul Moore32f50cd2006-09-28 14:51:47 -070037#include <linux/audit.h>
Paul Moored15c3452006-08-03 16:48:37 -070038#include <net/netlabel.h>
39#include <net/cipso_ipv4.h>
40#include <asm/bug.h>
41
42#include "netlabel_mgmt.h"
43#include "netlabel_domainhash.h"
Paul Moore32f50cd2006-09-28 14:51:47 -070044#include "netlabel_user.h"
Paul Moored15c3452006-08-03 16:48:37 -070045
46struct netlbl_domhsh_tbl {
47 struct list_head *tbl;
48 u32 size;
49};
50
51/* Domain hash table */
52/* XXX - updates should be so rare that having one spinlock for the entire
53 * hash table should be okay */
Adrian Bunk8ce11e62006-08-07 21:50:48 -070054static DEFINE_SPINLOCK(netlbl_domhsh_lock);
Paul Moored15c3452006-08-03 16:48:37 -070055static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
Paul Moored15c3452006-08-03 16:48:37 -070056static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
57
58/*
59 * Domain Hash Table Helper Functions
60 */
61
62/**
63 * netlbl_domhsh_free_entry - Frees a domain hash table entry
64 * @entry: the entry's RCU field
65 *
66 * Description:
67 * This function is designed to be used as a callback to the call_rcu()
68 * function so that the memory allocated to a hash table entry can be released
69 * safely.
70 *
71 */
72static void netlbl_domhsh_free_entry(struct rcu_head *entry)
73{
74 struct netlbl_dom_map *ptr;
75
76 ptr = container_of(entry, struct netlbl_dom_map, rcu);
77 kfree(ptr->domain);
78 kfree(ptr);
79}
80
81/**
82 * netlbl_domhsh_hash - Hashing function for the domain hash table
83 * @domain: the domain name to hash
84 *
85 * Description:
86 * This is the hashing function for the domain hash table, it returns the
87 * correct bucket number for the domain. The caller is responsibile for
88 * calling the rcu_read_[un]lock() functions.
89 *
90 */
91static u32 netlbl_domhsh_hash(const char *key)
92{
93 u32 iter;
94 u32 val;
95 u32 len;
96
97 /* This is taken (with slight modification) from
98 * security/selinux/ss/symtab.c:symhash() */
99
100 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
101 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
102 return val & (rcu_dereference(netlbl_domhsh)->size - 1);
103}
104
105/**
106 * netlbl_domhsh_search - Search for a domain entry
107 * @domain: the domain
Paul Moored15c3452006-08-03 16:48:37 -0700108 *
109 * Description:
110 * Searches the domain hash table and returns a pointer to the hash table
Paul Mooreb64397e2008-01-29 08:37:54 -0500111 * entry if found, otherwise NULL is returned. The caller is responsibile for
112 * the rcu hash table locks (i.e. the caller much call rcu_read_[un]lock()).
Paul Moored15c3452006-08-03 16:48:37 -0700113 *
114 */
Paul Mooreb64397e2008-01-29 08:37:54 -0500115static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
Paul Moored15c3452006-08-03 16:48:37 -0700116{
117 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400118 struct list_head *bkt_list;
Paul Moored15c3452006-08-03 16:48:37 -0700119 struct netlbl_dom_map *iter;
120
121 if (domain != NULL) {
122 bkt = netlbl_domhsh_hash(domain);
Paul Moore56196702008-10-10 10:16:29 -0400123 bkt_list = &rcu_dereference(netlbl_domhsh)->tbl[bkt];
124 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moored15c3452006-08-03 16:48:37 -0700125 if (iter->valid && strcmp(iter->domain, domain) == 0)
126 return iter;
127 }
128
Paul Mooreb64397e2008-01-29 08:37:54 -0500129 return NULL;
130}
131
132/**
133 * netlbl_domhsh_search_def - Search for a domain entry
134 * @domain: the domain
135 * @def: return default if no match is found
136 *
137 * Description:
138 * Searches the domain hash table and returns a pointer to the hash table
139 * entry if an exact match is found, if an exact match is not present in the
140 * hash table then the default entry is returned if valid otherwise NULL is
141 * returned. The caller is responsibile for the rcu hash table locks
142 * (i.e. the caller much call rcu_read_[un]lock()).
143 *
144 */
145static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
146{
147 struct netlbl_dom_map *entry;
148
149 entry = netlbl_domhsh_search(domain);
150 if (entry == NULL) {
151 entry = rcu_dereference(netlbl_domhsh_def);
Pavel Emelyanov4c3a0a22008-02-12 22:15:14 -0800152 if (entry != NULL && !entry->valid)
153 entry = NULL;
Paul Moored15c3452006-08-03 16:48:37 -0700154 }
155
Pavel Emelyanov4c3a0a22008-02-12 22:15:14 -0800156 return entry;
Paul Moored15c3452006-08-03 16:48:37 -0700157}
158
159/*
160 * Domain Hash Table Functions
161 */
162
163/**
164 * netlbl_domhsh_init - Init for the domain hash
165 * @size: the number of bits to use for the hash buckets
166 *
167 * Description:
168 * Initializes the domain hash table, should be called only by
169 * netlbl_user_init() during initialization. Returns zero on success, non-zero
170 * values on error.
171 *
172 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -0800173int __init netlbl_domhsh_init(u32 size)
Paul Moored15c3452006-08-03 16:48:37 -0700174{
175 u32 iter;
176 struct netlbl_domhsh_tbl *hsh_tbl;
177
178 if (size == 0)
179 return -EINVAL;
180
181 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
182 if (hsh_tbl == NULL)
183 return -ENOMEM;
184 hsh_tbl->size = 1 << size;
185 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
186 sizeof(struct list_head),
187 GFP_KERNEL);
188 if (hsh_tbl->tbl == NULL) {
189 kfree(hsh_tbl);
190 return -ENOMEM;
191 }
192 for (iter = 0; iter < hsh_tbl->size; iter++)
193 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
194
Paul Moored15c3452006-08-03 16:48:37 -0700195 spin_lock(&netlbl_domhsh_lock);
196 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
197 spin_unlock(&netlbl_domhsh_lock);
Paul Moored15c3452006-08-03 16:48:37 -0700198
199 return 0;
200}
201
202/**
203 * netlbl_domhsh_add - Adds a entry to the domain hash table
204 * @entry: the entry to add
Paul Moore95d4e6b2006-09-29 17:05:05 -0700205 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700206 *
207 * Description:
208 * Adds a new entry to the domain hash table and handles any updates to the
209 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
210 * negative on failure.
211 *
212 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700213int netlbl_domhsh_add(struct netlbl_dom_map *entry,
214 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700215{
216 int ret_val;
217 u32 bkt;
Paul Moore32f50cd2006-09-28 14:51:47 -0700218 struct audit_buffer *audit_buf;
Paul Moored15c3452006-08-03 16:48:37 -0700219
Paul Moored15c3452006-08-03 16:48:37 -0700220 entry->valid = 1;
221 INIT_RCU_HEAD(&entry->rcu);
222
Paul Moored15c3452006-08-03 16:48:37 -0700223 rcu_read_lock();
Paul Moore1c3fad92008-01-29 08:37:57 -0500224 spin_lock(&netlbl_domhsh_lock);
Paul Moored15c3452006-08-03 16:48:37 -0700225 if (entry->domain != NULL) {
226 bkt = netlbl_domhsh_hash(entry->domain);
Paul Mooreb64397e2008-01-29 08:37:54 -0500227 if (netlbl_domhsh_search(entry->domain) == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700228 list_add_tail_rcu(&entry->list,
Paul Moore3482fd92007-08-07 17:53:10 -0700229 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
Paul Moored15c3452006-08-03 16:48:37 -0700230 else
231 ret_val = -EEXIST;
Paul Moore4be27002007-10-26 04:29:08 -0700232 } else {
Paul Moored15c3452006-08-03 16:48:37 -0700233 INIT_LIST_HEAD(&entry->list);
Paul Moored15c3452006-08-03 16:48:37 -0700234 if (rcu_dereference(netlbl_domhsh_def) == NULL)
235 rcu_assign_pointer(netlbl_domhsh_def, entry);
236 else
237 ret_val = -EEXIST;
Paul Moore4be27002007-10-26 04:29:08 -0700238 }
Paul Moore1c3fad92008-01-29 08:37:57 -0500239 spin_unlock(&netlbl_domhsh_lock);
Paul Moore95d4e6b2006-09-29 17:05:05 -0700240 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
Paul Moorede646882006-11-17 17:38:55 -0500241 if (audit_buf != NULL) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700242 audit_log_format(audit_buf,
Paul Moorede646882006-11-17 17:38:55 -0500243 " nlbl_domain=%s",
244 entry->domain ? entry->domain : "(default)");
245 switch (entry->type) {
246 case NETLBL_NLTYPE_UNLABELED:
247 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
248 break;
249 case NETLBL_NLTYPE_CIPSOV4:
250 audit_log_format(audit_buf,
251 " nlbl_protocol=cipsov4 cipso_doi=%u",
252 entry->type_def.cipsov4->doi);
253 break;
254 }
255 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
256 audit_log_end(audit_buf);
Paul Moore32f50cd2006-09-28 14:51:47 -0700257 }
Paul Moored15c3452006-08-03 16:48:37 -0700258 rcu_read_unlock();
259
Paul Moored15c3452006-08-03 16:48:37 -0700260 return ret_val;
261}
262
263/**
264 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
265 * @entry: the entry to add
Paul Moore95d4e6b2006-09-29 17:05:05 -0700266 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700267 *
268 * Description:
269 * Adds a new default entry to the domain hash table and handles any updates
270 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
271 * negative on failure.
272 *
273 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700274int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
275 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700276{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700277 return netlbl_domhsh_add(entry, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700278}
279
280/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400281 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
282 * @entry: the entry to remove
283 * @audit_info: NetLabel audit information
284 *
285 * Description:
286 * Removes an entry from the domain hash table and handles any updates to the
287 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
288 * ensuring that the RCU read lock is held. Returns zero on success, negative
289 * on failure.
290 *
291 */
292int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
293 struct netlbl_audit *audit_info)
294{
295 int ret_val = 0;
296 struct audit_buffer *audit_buf;
297
298 if (entry == NULL)
299 return -ENOENT;
300
301 spin_lock(&netlbl_domhsh_lock);
302 if (entry->valid) {
303 entry->valid = 0;
304 if (entry != rcu_dereference(netlbl_domhsh_def))
305 list_del_rcu(&entry->list);
306 else
307 rcu_assign_pointer(netlbl_domhsh_def, NULL);
308 } else
309 ret_val = -ENOENT;
310 spin_unlock(&netlbl_domhsh_lock);
311
312 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
313 if (audit_buf != NULL) {
314 audit_log_format(audit_buf,
315 " nlbl_domain=%s res=%u",
316 entry->domain ? entry->domain : "(default)",
317 ret_val == 0 ? 1 : 0);
318 audit_log_end(audit_buf);
319 }
320
321 if (ret_val == 0) {
322 switch (entry->type) {
323 case NETLBL_NLTYPE_CIPSOV4:
324 cipso_v4_doi_putdef(entry->type_def.cipsov4);
325 break;
326 }
327 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
328 }
329
330 return ret_val;
331}
332
333/**
Paul Moored15c3452006-08-03 16:48:37 -0700334 * netlbl_domhsh_remove - Removes an entry from the domain hash table
335 * @domain: the domain to remove
Paul Moore95d4e6b2006-09-29 17:05:05 -0700336 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700337 *
338 * Description:
339 * Removes an entry from the domain hash table and handles any updates to the
340 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
341 * negative on failure.
342 *
343 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700344int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700345{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400346 int ret_val;
Paul Moored15c3452006-08-03 16:48:37 -0700347 struct netlbl_dom_map *entry;
348
349 rcu_read_lock();
Paul Mooreb64397e2008-01-29 08:37:54 -0500350 if (domain)
351 entry = netlbl_domhsh_search(domain);
352 else
353 entry = netlbl_domhsh_search_def(domain);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400354 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700355 rcu_read_unlock();
Paul Mooreb1edeb12008-10-10 10:16:31 -0400356
Paul Moored15c3452006-08-03 16:48:37 -0700357 return ret_val;
358}
359
360/**
361 * netlbl_domhsh_remove_default - Removes the default entry from the table
Paul Moore95d4e6b2006-09-29 17:05:05 -0700362 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700363 *
364 * Description:
365 * Removes/resets the default entry for the domain hash table and handles any
366 * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
367 * success, non-zero on failure.
368 *
369 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700370int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700371{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700372 return netlbl_domhsh_remove(NULL, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700373}
374
375/**
376 * netlbl_domhsh_getentry - Get an entry from the domain hash table
377 * @domain: the domain name to search for
378 *
379 * Description:
380 * Look through the domain hash table searching for an entry to match @domain,
381 * return a pointer to a copy of the entry or NULL. The caller is responsibile
382 * for ensuring that rcu_read_[un]lock() is called.
383 *
384 */
385struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
386{
Paul Mooreb64397e2008-01-29 08:37:54 -0500387 return netlbl_domhsh_search_def(domain);
Paul Moored15c3452006-08-03 16:48:37 -0700388}
389
390/**
Paul Moorefcd48282006-09-25 15:56:09 -0700391 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
392 * @skip_bkt: the number of buckets to skip at the start
393 * @skip_chain: the number of entries to skip in the first iterated bucket
394 * @callback: callback for each entry
395 * @cb_arg: argument for the callback function
Paul Moored15c3452006-08-03 16:48:37 -0700396 *
397 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700398 * Interate over the domain mapping hash table, skipping the first @skip_bkt
399 * buckets and @skip_chain entries. For each entry in the table call
400 * @callback, if @callback returns a negative value stop 'walking' through the
401 * table and return. Updates the values in @skip_bkt and @skip_chain on
402 * return. Returns zero on succcess, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700403 *
404 */
Paul Moorefcd48282006-09-25 15:56:09 -0700405int netlbl_domhsh_walk(u32 *skip_bkt,
406 u32 *skip_chain,
407 int (*callback) (struct netlbl_dom_map *entry, void *arg),
408 void *cb_arg)
Paul Moored15c3452006-08-03 16:48:37 -0700409{
Paul Moorefcd48282006-09-25 15:56:09 -0700410 int ret_val = -ENOENT;
411 u32 iter_bkt;
Paul Moore56196702008-10-10 10:16:29 -0400412 struct list_head *iter_list;
Paul Moorefcd48282006-09-25 15:56:09 -0700413 struct netlbl_dom_map *iter_entry;
414 u32 chain_cnt = 0;
Paul Moored15c3452006-08-03 16:48:37 -0700415
Paul Moored15c3452006-08-03 16:48:37 -0700416 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700417 for (iter_bkt = *skip_bkt;
418 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
419 iter_bkt++, chain_cnt = 0) {
Paul Moore56196702008-10-10 10:16:29 -0400420 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
421 list_for_each_entry_rcu(iter_entry, iter_list, list)
Paul Moorefcd48282006-09-25 15:56:09 -0700422 if (iter_entry->valid) {
423 if (chain_cnt++ < *skip_chain)
424 continue;
425 ret_val = callback(iter_entry, cb_arg);
426 if (ret_val < 0) {
427 chain_cnt--;
428 goto walk_return;
429 }
Paul Moored15c3452006-08-03 16:48:37 -0700430 }
Paul Moorefcd48282006-09-25 15:56:09 -0700431 }
Paul Moored15c3452006-08-03 16:48:37 -0700432
Paul Moorefcd48282006-09-25 15:56:09 -0700433walk_return:
Paul Moored15c3452006-08-03 16:48:37 -0700434 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700435 *skip_bkt = iter_bkt;
436 *skip_chain = chain_cnt;
437 return ret_val;
Paul Moored15c3452006-08-03 16:48:37 -0700438}