Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 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> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 33 | #include <linux/rculist.h> |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 34 | #include <linux/skbuff.h> |
| 35 | #include <linux/spinlock.h> |
| 36 | #include <linux/string.h> |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 37 | #include <linux/audit.h> |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 38 | #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 Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 44 | #include "netlabel_user.h" |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 45 | |
| 46 | struct 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 Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 54 | static DEFINE_SPINLOCK(netlbl_domhsh_lock); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 55 | static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 56 | static 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 | */ |
| 72 | static 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 | */ |
| 91 | static 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 Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 108 | * |
| 109 | * Description: |
| 110 | * Searches the domain hash table and returns a pointer to the hash table |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 111 | * 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 Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 113 | * |
| 114 | */ |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 115 | static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 116 | { |
| 117 | u32 bkt; |
| 118 | struct netlbl_dom_map *iter; |
| 119 | |
| 120 | if (domain != NULL) { |
| 121 | bkt = netlbl_domhsh_hash(domain); |
Paul Moore | 3482fd9 | 2007-08-07 17:53:10 -0700 | [diff] [blame] | 122 | list_for_each_entry_rcu(iter, |
| 123 | &rcu_dereference(netlbl_domhsh)->tbl[bkt], |
| 124 | list) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 125 | if (iter->valid && strcmp(iter->domain, domain) == 0) |
| 126 | return iter; |
| 127 | } |
| 128 | |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 129 | 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 | */ |
| 145 | static 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 Emelyanov | 4c3a0a2 | 2008-02-12 22:15:14 -0800 | [diff] [blame] | 152 | if (entry != NULL && !entry->valid) |
| 153 | entry = NULL; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Pavel Emelyanov | 4c3a0a2 | 2008-02-12 22:15:14 -0800 | [diff] [blame] | 156 | return entry; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 157 | } |
| 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 Emelyanov | 05705e4 | 2008-02-17 22:33:57 -0800 | [diff] [blame] | 173 | int __init netlbl_domhsh_init(u32 size) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 174 | { |
| 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 Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 195 | spin_lock(&netlbl_domhsh_lock); |
| 196 | rcu_assign_pointer(netlbl_domhsh, hsh_tbl); |
| 197 | spin_unlock(&netlbl_domhsh_lock); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 198 | |
| 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 Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 205 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 206 | * |
| 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 Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 213 | int netlbl_domhsh_add(struct netlbl_dom_map *entry, |
| 214 | struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 215 | { |
| 216 | int ret_val; |
| 217 | u32 bkt; |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 218 | struct audit_buffer *audit_buf; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 219 | |
| 220 | switch (entry->type) { |
| 221 | case NETLBL_NLTYPE_UNLABELED: |
| 222 | ret_val = 0; |
| 223 | break; |
| 224 | case NETLBL_NLTYPE_CIPSOV4: |
| 225 | ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4, |
| 226 | entry->domain); |
| 227 | break; |
| 228 | default: |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | if (ret_val != 0) |
| 232 | return ret_val; |
| 233 | |
| 234 | entry->valid = 1; |
| 235 | INIT_RCU_HEAD(&entry->rcu); |
| 236 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 237 | rcu_read_lock(); |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 238 | spin_lock(&netlbl_domhsh_lock); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 239 | if (entry->domain != NULL) { |
| 240 | bkt = netlbl_domhsh_hash(entry->domain); |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 241 | if (netlbl_domhsh_search(entry->domain) == NULL) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 242 | list_add_tail_rcu(&entry->list, |
Paul Moore | 3482fd9 | 2007-08-07 17:53:10 -0700 | [diff] [blame] | 243 | &rcu_dereference(netlbl_domhsh)->tbl[bkt]); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 244 | else |
| 245 | ret_val = -EEXIST; |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 246 | } else { |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 247 | INIT_LIST_HEAD(&entry->list); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 248 | if (rcu_dereference(netlbl_domhsh_def) == NULL) |
| 249 | rcu_assign_pointer(netlbl_domhsh_def, entry); |
| 250 | else |
| 251 | ret_val = -EEXIST; |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 252 | } |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 253 | spin_unlock(&netlbl_domhsh_lock); |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 254 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info); |
Paul Moore | de64688 | 2006-11-17 17:38:55 -0500 | [diff] [blame] | 255 | if (audit_buf != NULL) { |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 256 | audit_log_format(audit_buf, |
Paul Moore | de64688 | 2006-11-17 17:38:55 -0500 | [diff] [blame] | 257 | " nlbl_domain=%s", |
| 258 | entry->domain ? entry->domain : "(default)"); |
| 259 | switch (entry->type) { |
| 260 | case NETLBL_NLTYPE_UNLABELED: |
| 261 | audit_log_format(audit_buf, " nlbl_protocol=unlbl"); |
| 262 | break; |
| 263 | case NETLBL_NLTYPE_CIPSOV4: |
| 264 | audit_log_format(audit_buf, |
| 265 | " nlbl_protocol=cipsov4 cipso_doi=%u", |
| 266 | entry->type_def.cipsov4->doi); |
| 267 | break; |
| 268 | } |
| 269 | audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0); |
| 270 | audit_log_end(audit_buf); |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 271 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 272 | rcu_read_unlock(); |
| 273 | |
| 274 | if (ret_val != 0) { |
| 275 | switch (entry->type) { |
| 276 | case NETLBL_NLTYPE_CIPSOV4: |
| 277 | if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4, |
| 278 | entry->domain) != 0) |
| 279 | BUG(); |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return ret_val; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * netlbl_domhsh_add_default - Adds the default entry to the domain hash table |
| 289 | * @entry: the entry to add |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 290 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 291 | * |
| 292 | * Description: |
| 293 | * Adds a new default entry to the domain hash table and handles any updates |
| 294 | * to the lower level protocol handler (i.e. CIPSO). Returns zero on success, |
| 295 | * negative on failure. |
| 296 | * |
| 297 | */ |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 298 | int netlbl_domhsh_add_default(struct netlbl_dom_map *entry, |
| 299 | struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 300 | { |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 301 | return netlbl_domhsh_add(entry, audit_info); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * netlbl_domhsh_remove - Removes an entry from the domain hash table |
| 306 | * @domain: the domain to remove |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 307 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 308 | * |
| 309 | * Description: |
| 310 | * Removes an entry from the domain hash table and handles any updates to the |
| 311 | * lower level protocol handler (i.e. CIPSO). Returns zero on success, |
| 312 | * negative on failure. |
| 313 | * |
| 314 | */ |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 315 | int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 316 | { |
| 317 | int ret_val = -ENOENT; |
| 318 | struct netlbl_dom_map *entry; |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 319 | struct audit_buffer *audit_buf; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 320 | |
| 321 | rcu_read_lock(); |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 322 | if (domain) |
| 323 | entry = netlbl_domhsh_search(domain); |
| 324 | else |
| 325 | entry = netlbl_domhsh_search_def(domain); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 326 | if (entry == NULL) |
| 327 | goto remove_return; |
| 328 | switch (entry->type) { |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 329 | case NETLBL_NLTYPE_CIPSOV4: |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 330 | cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4, |
| 331 | entry->domain); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 332 | break; |
| 333 | } |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 334 | spin_lock(&netlbl_domhsh_lock); |
| 335 | if (entry->valid) { |
| 336 | entry->valid = 0; |
| 337 | if (entry != rcu_dereference(netlbl_domhsh_def)) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 338 | list_del_rcu(&entry->list); |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 339 | else |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 340 | rcu_assign_pointer(netlbl_domhsh_def, NULL); |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 341 | ret_val = 0; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 342 | } |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 343 | spin_unlock(&netlbl_domhsh_lock); |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 344 | |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 345 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info); |
Paul Moore | de64688 | 2006-11-17 17:38:55 -0500 | [diff] [blame] | 346 | if (audit_buf != NULL) { |
| 347 | audit_log_format(audit_buf, |
| 348 | " nlbl_domain=%s res=%u", |
| 349 | entry->domain ? entry->domain : "(default)", |
| 350 | ret_val == 0 ? 1 : 0); |
| 351 | audit_log_end(audit_buf); |
| 352 | } |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 353 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 354 | remove_return: |
| 355 | rcu_read_unlock(); |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 356 | if (ret_val == 0) |
| 357 | call_rcu(&entry->rcu, netlbl_domhsh_free_entry); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 358 | return ret_val; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * netlbl_domhsh_remove_default - Removes the default entry from the table |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 363 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 364 | * |
| 365 | * Description: |
| 366 | * Removes/resets the default entry for the domain hash table and handles any |
| 367 | * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on |
| 368 | * success, non-zero on failure. |
| 369 | * |
| 370 | */ |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 371 | int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 372 | { |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 373 | return netlbl_domhsh_remove(NULL, audit_info); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * netlbl_domhsh_getentry - Get an entry from the domain hash table |
| 378 | * @domain: the domain name to search for |
| 379 | * |
| 380 | * Description: |
| 381 | * Look through the domain hash table searching for an entry to match @domain, |
| 382 | * return a pointer to a copy of the entry or NULL. The caller is responsibile |
| 383 | * for ensuring that rcu_read_[un]lock() is called. |
| 384 | * |
| 385 | */ |
| 386 | struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain) |
| 387 | { |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 388 | return netlbl_domhsh_search_def(domain); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 392 | * netlbl_domhsh_walk - Iterate through the domain mapping hash table |
| 393 | * @skip_bkt: the number of buckets to skip at the start |
| 394 | * @skip_chain: the number of entries to skip in the first iterated bucket |
| 395 | * @callback: callback for each entry |
| 396 | * @cb_arg: argument for the callback function |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 397 | * |
| 398 | * Description: |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 399 | * Interate over the domain mapping hash table, skipping the first @skip_bkt |
| 400 | * buckets and @skip_chain entries. For each entry in the table call |
| 401 | * @callback, if @callback returns a negative value stop 'walking' through the |
| 402 | * table and return. Updates the values in @skip_bkt and @skip_chain on |
| 403 | * return. Returns zero on succcess, negative values on failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 404 | * |
| 405 | */ |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 406 | int netlbl_domhsh_walk(u32 *skip_bkt, |
| 407 | u32 *skip_chain, |
| 408 | int (*callback) (struct netlbl_dom_map *entry, void *arg), |
| 409 | void *cb_arg) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 410 | { |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 411 | int ret_val = -ENOENT; |
| 412 | u32 iter_bkt; |
| 413 | struct netlbl_dom_map *iter_entry; |
| 414 | u32 chain_cnt = 0; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 415 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 416 | rcu_read_lock(); |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 417 | for (iter_bkt = *skip_bkt; |
| 418 | iter_bkt < rcu_dereference(netlbl_domhsh)->size; |
| 419 | iter_bkt++, chain_cnt = 0) { |
| 420 | list_for_each_entry_rcu(iter_entry, |
Paul Moore | 3482fd9 | 2007-08-07 17:53:10 -0700 | [diff] [blame] | 421 | &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt], |
| 422 | list) |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 423 | if (iter_entry->valid) { |
| 424 | if (chain_cnt++ < *skip_chain) |
| 425 | continue; |
| 426 | ret_val = callback(iter_entry, cb_arg); |
| 427 | if (ret_val < 0) { |
| 428 | chain_cnt--; |
| 429 | goto walk_return; |
| 430 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 431 | } |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 432 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 433 | |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 434 | walk_return: |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 435 | rcu_read_unlock(); |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 436 | *skip_bkt = iter_bkt; |
| 437 | *skip_chain = chain_cnt; |
| 438 | return ret_val; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 439 | } |