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 <linux/audit.h>
39 #include <net/netlabel.h>
40 #include <net/cipso_ipv4.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
;
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
;
58 /* Default domain mapping */
59 static DEFINE_SPINLOCK(netlbl_domhsh_def_lock
);
60 static struct netlbl_dom_map
*netlbl_domhsh_def
= NULL
;
63 * Domain Hash Table Helper Functions
67 * netlbl_domhsh_free_entry - Frees a domain hash table entry
68 * @entry: the entry's RCU field
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
76 static void netlbl_domhsh_free_entry(struct rcu_head
*entry
)
78 struct netlbl_dom_map
*ptr
;
80 ptr
= container_of(entry
, struct netlbl_dom_map
, rcu
);
86 * netlbl_domhsh_hash - Hashing function for the domain hash table
87 * @domain: the domain name to hash
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.
95 static u32
netlbl_domhsh_hash(const char *key
)
101 /* This is taken (with slight modification) from
102 * security/selinux/ss/symtab.c:symhash() */
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);
110 * netlbl_domhsh_search - Search for a domain entry
111 * @domain: the domain
112 * @def: return default if no match is found
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()).
122 static struct netlbl_dom_map
*netlbl_domhsh_search(const char *domain
, u32 def
)
125 struct netlbl_dom_map
*iter
;
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)
135 iter
= rcu_dereference(netlbl_domhsh_def
);
136 if (iter
!= NULL
&& iter
->valid
)
144 * Domain Hash Table Functions
148 * netlbl_domhsh_init - Init for the domain hash
149 * @size: the number of bits to use for the hash buckets
152 * Initializes the domain hash table, should be called only by
153 * netlbl_user_init() during initialization. Returns zero on success, non-zero
157 int netlbl_domhsh_init(u32 size
)
160 struct netlbl_domhsh_tbl
*hsh_tbl
;
165 hsh_tbl
= kmalloc(sizeof(*hsh_tbl
), GFP_KERNEL
);
168 hsh_tbl
->size
= 1 << size
;
169 hsh_tbl
->tbl
= kcalloc(hsh_tbl
->size
,
170 sizeof(struct list_head
),
172 if (hsh_tbl
->tbl
== NULL
) {
176 for (iter
= 0; iter
< hsh_tbl
->size
; iter
++)
177 INIT_LIST_HEAD(&hsh_tbl
->tbl
[iter
]);
180 spin_lock(&netlbl_domhsh_lock
);
181 rcu_assign_pointer(netlbl_domhsh
, hsh_tbl
);
182 spin_unlock(&netlbl_domhsh_lock
);
189 * netlbl_domhsh_add - Adds a entry to the domain hash table
190 * @entry: the entry to add
191 * @audit_secid: the LSM secid to use in the audit message
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.
199 int netlbl_domhsh_add(struct netlbl_dom_map
*entry
, u32 audit_secid
)
203 struct audit_buffer
*audit_buf
;
206 switch (entry
->type
) {
207 case NETLBL_NLTYPE_UNLABELED
:
210 case NETLBL_NLTYPE_CIPSOV4
:
211 ret_val
= cipso_v4_doi_domhsh_add(entry
->type_def
.cipsov4
,
221 INIT_RCU_HEAD(&entry
->rcu
);
225 if (entry
->domain
!= NULL
) {
226 bkt
= netlbl_domhsh_hash(entry
->domain
);
227 spin_lock(&netlbl_domhsh_lock
);
228 if (netlbl_domhsh_search(entry
->domain
, 0) == NULL
)
229 list_add_tail_rcu(&entry
->list
,
230 &netlbl_domhsh
->tbl
[bkt
]);
233 spin_unlock(&netlbl_domhsh_lock
);
234 } else if (entry
->domain
== NULL
) {
235 INIT_LIST_HEAD(&entry
->list
);
236 spin_lock(&netlbl_domhsh_def_lock
);
237 if (rcu_dereference(netlbl_domhsh_def
) == NULL
)
238 rcu_assign_pointer(netlbl_domhsh_def
, entry
);
241 spin_unlock(&netlbl_domhsh_def_lock
);
245 if (entry
->domain
!= NULL
)
246 audit_domain
= entry
->domain
;
248 audit_domain
= "(default)";
249 audit_buf
= netlbl_audit_start_common(AUDIT_MAC_MAP_ADD
,
251 audit_log_format(audit_buf
, " domain=%s", audit_domain
);
252 switch (entry
->type
) {
253 case NETLBL_NLTYPE_UNLABELED
:
254 audit_log_format(audit_buf
, " protocol=unlbl");
256 case NETLBL_NLTYPE_CIPSOV4
:
257 audit_log_format(audit_buf
,
258 " protocol=cipsov4 doi=%u",
259 entry
->type_def
.cipsov4
->doi
);
262 audit_log_end(audit_buf
);
267 switch (entry
->type
) {
268 case NETLBL_NLTYPE_CIPSOV4
:
269 if (cipso_v4_doi_domhsh_remove(entry
->type_def
.cipsov4
,
280 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
281 * @entry: the entry to add
282 * @audit_secid: the LSM secid to use in the audit message
285 * Adds a new default entry to the domain hash table and handles any updates
286 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
287 * negative on failure.
290 int netlbl_domhsh_add_default(struct netlbl_dom_map
*entry
, u32 audit_secid
)
292 return netlbl_domhsh_add(entry
, audit_secid
);
296 * netlbl_domhsh_remove - Removes an entry from the domain hash table
297 * @domain: the domain to remove
298 * @audit_secid: the LSM secid to use in the audit message
301 * Removes an entry from the domain hash table and handles any updates to the
302 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
303 * negative on failure.
306 int netlbl_domhsh_remove(const char *domain
, u32 audit_secid
)
308 int ret_val
= -ENOENT
;
309 struct netlbl_dom_map
*entry
;
310 struct audit_buffer
*audit_buf
;
315 entry
= netlbl_domhsh_search(domain
, 0);
317 entry
= netlbl_domhsh_search(domain
, 1);
320 switch (entry
->type
) {
321 case NETLBL_NLTYPE_UNLABELED
:
323 case NETLBL_NLTYPE_CIPSOV4
:
324 ret_val
= cipso_v4_doi_domhsh_remove(entry
->type_def
.cipsov4
,
331 if (entry
!= rcu_dereference(netlbl_domhsh_def
)) {
332 spin_lock(&netlbl_domhsh_lock
);
335 list_del_rcu(&entry
->list
);
338 spin_unlock(&netlbl_domhsh_lock
);
340 spin_lock(&netlbl_domhsh_def_lock
);
343 rcu_assign_pointer(netlbl_domhsh_def
, NULL
);
346 spin_unlock(&netlbl_domhsh_def_lock
);
349 if (entry
->domain
!= NULL
)
350 audit_domain
= entry
->domain
;
352 audit_domain
= "(default)";
353 audit_buf
= netlbl_audit_start_common(AUDIT_MAC_MAP_DEL
,
355 audit_log_format(audit_buf
, " domain=%s", audit_domain
);
356 audit_log_end(audit_buf
);
358 call_rcu(&entry
->rcu
, netlbl_domhsh_free_entry
);
367 * netlbl_domhsh_remove_default - Removes the default entry from the table
368 * @audit_secid: the LSM secid to use in the audit message
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(u32 audit_secid
)
378 return netlbl_domhsh_remove(NULL
, audit_secid
);
382 * netlbl_domhsh_getentry - Get an entry from the domain hash table
383 * @domain: the domain name to search for
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(domain
, 1);
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
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
,
413 int (*callback
) (struct netlbl_dom_map
*entry
, void *arg
),
416 int ret_val
= -ENOENT
;
418 struct netlbl_dom_map
*iter_entry
;
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 &netlbl_domhsh
->tbl
[iter_bkt
],
428 if (iter_entry
->valid
) {
429 if (chain_cnt
++ < *skip_chain
)
431 ret_val
= callback(iter_entry
, cb_arg
);
441 *skip_bkt
= iter_bkt
;
442 *skip_chain
= chain_cnt
;
This page took 0.056651 seconds and 5 git commands to generate.