]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netlabel/netlabel_domainhash.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / net / netlabel / netlabel_domainhash.c
CommitLineData
d15c345f
PM
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/*
63c41688 14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
d15c345f
PM
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>
82524746 33#include <linux/rculist.h>
d15c345f
PM
34#include <linux/skbuff.h>
35#include <linux/spinlock.h>
36#include <linux/string.h>
32f50cde 37#include <linux/audit.h>
5a0e3ad6 38#include <linux/slab.h>
d15c345f
PM
39#include <net/netlabel.h>
40#include <net/cipso_ipv4.h>
41#include <asm/bug.h>
42
43#include "netlabel_mgmt.h"
63c41688 44#include "netlabel_addrlist.h"
d15c345f 45#include "netlabel_domainhash.h"
32f50cde 46#include "netlabel_user.h"
d15c345f
PM
47
48struct netlbl_domhsh_tbl {
49 struct list_head *tbl;
50 u32 size;
51};
52
53/* Domain hash table */
54/* XXX - updates should be so rare that having one spinlock for the entire
55 * hash table should be okay */
8ce11e6a 56static DEFINE_SPINLOCK(netlbl_domhsh_lock);
d15c345f 57static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL;
d15c345f
PM
58static struct netlbl_dom_map *netlbl_domhsh_def = NULL;
59
60/*
61 * Domain Hash Table Helper Functions
62 */
63
64/**
65 * netlbl_domhsh_free_entry - Frees a domain hash table entry
66 * @entry: the entry's RCU field
67 *
68 * Description:
69 * This function is designed to be used as a callback to the call_rcu()
70 * function so that the memory allocated to a hash table entry can be released
71 * safely.
72 *
73 */
74static void netlbl_domhsh_free_entry(struct rcu_head *entry)
75{
76 struct netlbl_dom_map *ptr;
63c41688
PM
77 struct netlbl_af4list *iter4;
78 struct netlbl_af4list *tmp4;
79#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
80 struct netlbl_af6list *iter6;
81 struct netlbl_af6list *tmp6;
82#endif /* IPv6 */
d15c345f
PM
83
84 ptr = container_of(entry, struct netlbl_dom_map, rcu);
63c41688
PM
85 if (ptr->type == NETLBL_NLTYPE_ADDRSELECT) {
86 netlbl_af4list_foreach_safe(iter4, tmp4,
87 &ptr->type_def.addrsel->list4) {
88 netlbl_af4list_remove_entry(iter4);
89 kfree(netlbl_domhsh_addr4_entry(iter4));
90 }
91#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
92 netlbl_af6list_foreach_safe(iter6, tmp6,
93 &ptr->type_def.addrsel->list6) {
94 netlbl_af6list_remove_entry(iter6);
95 kfree(netlbl_domhsh_addr6_entry(iter6));
96 }
97#endif /* IPv6 */
98 }
d15c345f
PM
99 kfree(ptr->domain);
100 kfree(ptr);
101}
102
103/**
104 * netlbl_domhsh_hash - Hashing function for the domain hash table
105 * @domain: the domain name to hash
106 *
107 * Description:
108 * This is the hashing function for the domain hash table, it returns the
109 * correct bucket number for the domain. The caller is responsibile for
110 * calling the rcu_read_[un]lock() functions.
111 *
112 */
113static u32 netlbl_domhsh_hash(const char *key)
114{
115 u32 iter;
116 u32 val;
117 u32 len;
118
119 /* This is taken (with slight modification) from
120 * security/selinux/ss/symtab.c:symhash() */
121
122 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
123 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
124 return val & (rcu_dereference(netlbl_domhsh)->size - 1);
125}
126
127/**
128 * netlbl_domhsh_search - Search for a domain entry
129 * @domain: the domain
d15c345f
PM
130 *
131 * Description:
132 * Searches the domain hash table and returns a pointer to the hash table
b64397e0
PM
133 * entry if found, otherwise NULL is returned. The caller is responsibile for
134 * the rcu hash table locks (i.e. the caller much call rcu_read_[un]lock()).
d15c345f
PM
135 *
136 */
b64397e0 137static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
d15c345f
PM
138{
139 u32 bkt;
56196701 140 struct list_head *bkt_list;
d15c345f
PM
141 struct netlbl_dom_map *iter;
142
143 if (domain != NULL) {
144 bkt = netlbl_domhsh_hash(domain);
56196701
PM
145 bkt_list = &rcu_dereference(netlbl_domhsh)->tbl[bkt];
146 list_for_each_entry_rcu(iter, bkt_list, list)
d15c345f
PM
147 if (iter->valid && strcmp(iter->domain, domain) == 0)
148 return iter;
149 }
150
b64397e0
PM
151 return NULL;
152}
153
154/**
155 * netlbl_domhsh_search_def - Search for a domain entry
156 * @domain: the domain
157 * @def: return default if no match is found
158 *
159 * Description:
160 * Searches the domain hash table and returns a pointer to the hash table
161 * entry if an exact match is found, if an exact match is not present in the
162 * hash table then the default entry is returned if valid otherwise NULL is
163 * returned. The caller is responsibile for the rcu hash table locks
164 * (i.e. the caller much call rcu_read_[un]lock()).
165 *
166 */
167static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
168{
169 struct netlbl_dom_map *entry;
170
171 entry = netlbl_domhsh_search(domain);
172 if (entry == NULL) {
173 entry = rcu_dereference(netlbl_domhsh_def);
4c3a0a25
PE
174 if (entry != NULL && !entry->valid)
175 entry = NULL;
d15c345f
PM
176 }
177
4c3a0a25 178 return entry;
d15c345f
PM
179}
180
63c41688
PM
181/**
182 * netlbl_domhsh_audit_add - Generate an audit entry for an add event
183 * @entry: the entry being added
184 * @addr4: the IPv4 address information
185 * @addr6: the IPv6 address information
186 * @result: the result code
187 * @audit_info: NetLabel audit information
188 *
189 * Description:
190 * Generate an audit record for adding a new NetLabel/LSM mapping entry with
191 * the given information. Caller is responsibile for holding the necessary
192 * locks.
193 *
194 */
195static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
196 struct netlbl_af4list *addr4,
197 struct netlbl_af6list *addr6,
198 int result,
199 struct netlbl_audit *audit_info)
200{
201 struct audit_buffer *audit_buf;
202 struct cipso_v4_doi *cipsov4 = NULL;
203 u32 type;
204
205 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
206 if (audit_buf != NULL) {
207 audit_log_format(audit_buf, " nlbl_domain=%s",
208 entry->domain ? entry->domain : "(default)");
209 if (addr4 != NULL) {
210 struct netlbl_domaddr4_map *map4;
211 map4 = netlbl_domhsh_addr4_entry(addr4);
212 type = map4->type;
213 cipsov4 = map4->type_def.cipsov4;
214 netlbl_af4list_audit_addr(audit_buf, 0, NULL,
215 addr4->addr, addr4->mask);
216#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
217 } else if (addr6 != NULL) {
218 struct netlbl_domaddr6_map *map6;
219 map6 = netlbl_domhsh_addr6_entry(addr6);
220 type = map6->type;
221 netlbl_af6list_audit_addr(audit_buf, 0, NULL,
222 &addr6->addr, &addr6->mask);
223#endif /* IPv6 */
224 } else {
225 type = entry->type;
226 cipsov4 = entry->type_def.cipsov4;
227 }
228 switch (type) {
229 case NETLBL_NLTYPE_UNLABELED:
230 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
231 break;
232 case NETLBL_NLTYPE_CIPSOV4:
233 BUG_ON(cipsov4 == NULL);
234 audit_log_format(audit_buf,
235 " nlbl_protocol=cipsov4 cipso_doi=%u",
236 cipsov4->doi);
237 break;
238 }
239 audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
240 audit_log_end(audit_buf);
241 }
242}
243
d15c345f
PM
244/*
245 * Domain Hash Table Functions
246 */
247
248/**
249 * netlbl_domhsh_init - Init for the domain hash
250 * @size: the number of bits to use for the hash buckets
251 *
252 * Description:
253 * Initializes the domain hash table, should be called only by
254 * netlbl_user_init() during initialization. Returns zero on success, non-zero
255 * values on error.
256 *
257 */
05705e4e 258int __init netlbl_domhsh_init(u32 size)
d15c345f
PM
259{
260 u32 iter;
261 struct netlbl_domhsh_tbl *hsh_tbl;
262
263 if (size == 0)
264 return -EINVAL;
265
266 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
267 if (hsh_tbl == NULL)
268 return -ENOMEM;
269 hsh_tbl->size = 1 << size;
270 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
271 sizeof(struct list_head),
272 GFP_KERNEL);
273 if (hsh_tbl->tbl == NULL) {
274 kfree(hsh_tbl);
275 return -ENOMEM;
276 }
277 for (iter = 0; iter < hsh_tbl->size; iter++)
278 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
279
d15c345f
PM
280 spin_lock(&netlbl_domhsh_lock);
281 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
282 spin_unlock(&netlbl_domhsh_lock);
d15c345f
PM
283
284 return 0;
285}
286
287/**
288 * netlbl_domhsh_add - Adds a entry to the domain hash table
289 * @entry: the entry to add
95d4e6be 290 * @audit_info: NetLabel audit information
d15c345f
PM
291 *
292 * Description:
293 * Adds a new entry to the domain hash table and handles any updates to the
294 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
295 * negative on failure.
296 *
297 */
95d4e6be
PM
298int netlbl_domhsh_add(struct netlbl_dom_map *entry,
299 struct netlbl_audit *audit_info)
d15c345f 300{
63c41688
PM
301 int ret_val = 0;
302 struct netlbl_dom_map *entry_old;
303 struct netlbl_af4list *iter4;
304 struct netlbl_af4list *tmp4;
305#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
306 struct netlbl_af6list *iter6;
307 struct netlbl_af6list *tmp6;
308#endif /* IPv6 */
d15c345f 309
d15c345f 310 rcu_read_lock();
63c41688 311
1c3fad93 312 spin_lock(&netlbl_domhsh_lock);
63c41688
PM
313 if (entry->domain != NULL)
314 entry_old = netlbl_domhsh_search(entry->domain);
315 else
316 entry_old = netlbl_domhsh_search_def(entry->domain);
317 if (entry_old == NULL) {
318 entry->valid = 1;
63c41688
PM
319
320 if (entry->domain != NULL) {
321 u32 bkt = netlbl_domhsh_hash(entry->domain);
d15c345f 322 list_add_tail_rcu(&entry->list,
3482fd90 323 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
63c41688
PM
324 } else {
325 INIT_LIST_HEAD(&entry->list);
d15c345f 326 rcu_assign_pointer(netlbl_domhsh_def, entry);
de64688f 327 }
d15c345f 328
63c41688
PM
329 if (entry->type == NETLBL_NLTYPE_ADDRSELECT) {
330 netlbl_af4list_foreach_rcu(iter4,
331 &entry->type_def.addrsel->list4)
332 netlbl_domhsh_audit_add(entry, iter4, NULL,
333 ret_val, audit_info);
334#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
335 netlbl_af6list_foreach_rcu(iter6,
336 &entry->type_def.addrsel->list6)
337 netlbl_domhsh_audit_add(entry, NULL, iter6,
338 ret_val, audit_info);
339#endif /* IPv6 */
340 } else
341 netlbl_domhsh_audit_add(entry, NULL, NULL,
342 ret_val, audit_info);
343 } else if (entry_old->type == NETLBL_NLTYPE_ADDRSELECT &&
344 entry->type == NETLBL_NLTYPE_ADDRSELECT) {
345 struct list_head *old_list4;
346 struct list_head *old_list6;
347
348 old_list4 = &entry_old->type_def.addrsel->list4;
349 old_list6 = &entry_old->type_def.addrsel->list6;
350
351 /* we only allow the addition of address selectors if all of
352 * the selectors do not exist in the existing domain map */
353 netlbl_af4list_foreach_rcu(iter4,
354 &entry->type_def.addrsel->list4)
355 if (netlbl_af4list_search_exact(iter4->addr,
356 iter4->mask,
357 old_list4)) {
358 ret_val = -EEXIST;
359 goto add_return;
360 }
361#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
362 netlbl_af6list_foreach_rcu(iter6,
363 &entry->type_def.addrsel->list6)
364 if (netlbl_af6list_search_exact(&iter6->addr,
365 &iter6->mask,
366 old_list6)) {
367 ret_val = -EEXIST;
368 goto add_return;
369 }
370#endif /* IPv6 */
371
372 netlbl_af4list_foreach_safe(iter4, tmp4,
373 &entry->type_def.addrsel->list4) {
374 netlbl_af4list_remove_entry(iter4);
375 iter4->valid = 1;
376 ret_val = netlbl_af4list_add(iter4, old_list4);
377 netlbl_domhsh_audit_add(entry_old, iter4, NULL,
378 ret_val, audit_info);
379 if (ret_val != 0)
380 goto add_return;
381 }
382#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
383 netlbl_af6list_foreach_safe(iter6, tmp6,
384 &entry->type_def.addrsel->list6) {
385 netlbl_af6list_remove_entry(iter6);
386 iter6->valid = 1;
387 ret_val = netlbl_af6list_add(iter6, old_list6);
388 netlbl_domhsh_audit_add(entry_old, NULL, iter6,
389 ret_val, audit_info);
390 if (ret_val != 0)
391 goto add_return;
392 }
393#endif /* IPv6 */
394 } else
395 ret_val = -EINVAL;
396
397add_return:
398 spin_unlock(&netlbl_domhsh_lock);
399 rcu_read_unlock();
d15c345f
PM
400 return ret_val;
401}
402
403/**
404 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
405 * @entry: the entry to add
95d4e6be 406 * @audit_info: NetLabel audit information
d15c345f
PM
407 *
408 * Description:
409 * Adds a new default entry to the domain hash table and handles any updates
410 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
411 * negative on failure.
412 *
413 */
95d4e6be
PM
414int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
415 struct netlbl_audit *audit_info)
d15c345f 416{
95d4e6be 417 return netlbl_domhsh_add(entry, audit_info);
d15c345f
PM
418}
419
420/**
b1edeb10
PM
421 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
422 * @entry: the entry to remove
95d4e6be 423 * @audit_info: NetLabel audit information
d15c345f
PM
424 *
425 * Description:
426 * Removes an entry from the domain hash table and handles any updates to the
b1edeb10
PM
427 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
428 * ensuring that the RCU read lock is held. Returns zero on success, negative
429 * on failure.
d15c345f
PM
430 *
431 */
b1edeb10
PM
432int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
433 struct netlbl_audit *audit_info)
d15c345f 434{
b1edeb10 435 int ret_val = 0;
32f50cde 436 struct audit_buffer *audit_buf;
d15c345f 437
d15c345f 438 if (entry == NULL)
b1edeb10
PM
439 return -ENOENT;
440
1c3fad93
PM
441 spin_lock(&netlbl_domhsh_lock);
442 if (entry->valid) {
443 entry->valid = 0;
444 if (entry != rcu_dereference(netlbl_domhsh_def))
d15c345f 445 list_del_rcu(&entry->list);
1c3fad93 446 else
d15c345f 447 rcu_assign_pointer(netlbl_domhsh_def, NULL);
b1edeb10
PM
448 } else
449 ret_val = -ENOENT;
1c3fad93 450 spin_unlock(&netlbl_domhsh_lock);
32f50cde 451
95d4e6be 452 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
de64688f
PM
453 if (audit_buf != NULL) {
454 audit_log_format(audit_buf,
455 " nlbl_domain=%s res=%u",
456 entry->domain ? entry->domain : "(default)",
457 ret_val == 0 ? 1 : 0);
458 audit_log_end(audit_buf);
459 }
95d4e6be 460
b1edeb10 461 if (ret_val == 0) {
63c41688
PM
462 struct netlbl_af4list *iter4;
463 struct netlbl_domaddr4_map *map4;
464
b1edeb10 465 switch (entry->type) {
63c41688
PM
466 case NETLBL_NLTYPE_ADDRSELECT:
467 netlbl_af4list_foreach_rcu(iter4,
468 &entry->type_def.addrsel->list4) {
469 map4 = netlbl_domhsh_addr4_entry(iter4);
470 cipso_v4_doi_putdef(map4->type_def.cipsov4);
471 }
472 /* no need to check the IPv6 list since we currently
473 * support only unlabeled protocols for IPv6 */
474 break;
b1edeb10
PM
475 case NETLBL_NLTYPE_CIPSOV4:
476 cipso_v4_doi_putdef(entry->type_def.cipsov4);
477 break;
478 }
4be2700f 479 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
b1edeb10
PM
480 }
481
482 return ret_val;
483}
484
6c2e8ac0
PM
485/**
486 * netlbl_domhsh_remove_af4 - Removes an address selector entry
487 * @domain: the domain
488 * @addr: IPv4 address
489 * @mask: IPv4 address mask
490 * @audit_info: NetLabel audit information
491 *
492 * Description:
493 * Removes an individual address selector from a domain mapping and potentially
494 * the entire mapping if it is empty. Returns zero on success, negative values
495 * on failure.
496 *
497 */
498int netlbl_domhsh_remove_af4(const char *domain,
499 const struct in_addr *addr,
500 const struct in_addr *mask,
501 struct netlbl_audit *audit_info)
502{
503 struct netlbl_dom_map *entry_map;
504 struct netlbl_af4list *entry_addr;
505 struct netlbl_af4list *iter4;
506#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
507 struct netlbl_af6list *iter6;
508#endif /* IPv6 */
509 struct netlbl_domaddr4_map *entry;
510
511 rcu_read_lock();
512
513 if (domain)
514 entry_map = netlbl_domhsh_search(domain);
515 else
516 entry_map = netlbl_domhsh_search_def(domain);
517 if (entry_map == NULL || entry_map->type != NETLBL_NLTYPE_ADDRSELECT)
518 goto remove_af4_failure;
519
520 spin_lock(&netlbl_domhsh_lock);
521 entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
522 &entry_map->type_def.addrsel->list4);
523 spin_unlock(&netlbl_domhsh_lock);
524
525 if (entry_addr == NULL)
526 goto remove_af4_failure;
527 netlbl_af4list_foreach_rcu(iter4, &entry_map->type_def.addrsel->list4)
528 goto remove_af4_single_addr;
529#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
530 netlbl_af6list_foreach_rcu(iter6, &entry_map->type_def.addrsel->list6)
531 goto remove_af4_single_addr;
532#endif /* IPv6 */
533 /* the domain mapping is empty so remove it from the mapping table */
534 netlbl_domhsh_remove_entry(entry_map, audit_info);
535
536remove_af4_single_addr:
537 rcu_read_unlock();
538 /* yick, we can't use call_rcu here because we don't have a rcu head
539 * pointer but hopefully this should be a rare case so the pause
540 * shouldn't be a problem */
541 synchronize_rcu();
542 entry = netlbl_domhsh_addr4_entry(entry_addr);
543 cipso_v4_doi_putdef(entry->type_def.cipsov4);
544 kfree(entry);
545 return 0;
546
547remove_af4_failure:
548 rcu_read_unlock();
549 return -ENOENT;
550}
551
b1edeb10
PM
552/**
553 * netlbl_domhsh_remove - Removes an entry from the domain hash table
554 * @domain: the domain to remove
555 * @audit_info: NetLabel audit information
556 *
557 * Description:
558 * Removes an entry from the domain hash table and handles any updates to the
559 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
560 * negative on failure.
561 *
562 */
563int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
564{
565 int ret_val;
566 struct netlbl_dom_map *entry;
567
568 rcu_read_lock();
569 if (domain)
570 entry = netlbl_domhsh_search(domain);
571 else
572 entry = netlbl_domhsh_search_def(domain);
573 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
574 rcu_read_unlock();
575
d15c345f
PM
576 return ret_val;
577}
578
579/**
580 * netlbl_domhsh_remove_default - Removes the default entry from the table
95d4e6be 581 * @audit_info: NetLabel audit information
d15c345f
PM
582 *
583 * Description:
584 * Removes/resets the default entry for the domain hash table and handles any
585 * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
586 * success, non-zero on failure.
587 *
588 */
95d4e6be 589int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
d15c345f 590{
95d4e6be 591 return netlbl_domhsh_remove(NULL, audit_info);
d15c345f
PM
592}
593
594/**
595 * netlbl_domhsh_getentry - Get an entry from the domain hash table
596 * @domain: the domain name to search for
597 *
598 * Description:
599 * Look through the domain hash table searching for an entry to match @domain,
600 * return a pointer to a copy of the entry or NULL. The caller is responsibile
601 * for ensuring that rcu_read_[un]lock() is called.
602 *
603 */
604struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
605{
b64397e0 606 return netlbl_domhsh_search_def(domain);
d15c345f
PM
607}
608
63c41688
PM
609/**
610 * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
611 * @domain: the domain name to search for
612 * @addr: the IP address to search for
613 *
614 * Description:
615 * Look through the domain hash table searching for an entry to match @domain
616 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
617 * responsible for ensuring that rcu_read_[un]lock() is called.
618 *
619 */
620struct netlbl_domaddr4_map *netlbl_domhsh_getentry_af4(const char *domain,
621 __be32 addr)
622{
623 struct netlbl_dom_map *dom_iter;
624 struct netlbl_af4list *addr_iter;
625
626 dom_iter = netlbl_domhsh_search_def(domain);
627 if (dom_iter == NULL)
628 return NULL;
629 if (dom_iter->type != NETLBL_NLTYPE_ADDRSELECT)
630 return NULL;
631
632 addr_iter = netlbl_af4list_search(addr,
633 &dom_iter->type_def.addrsel->list4);
634 if (addr_iter == NULL)
635 return NULL;
636
637 return netlbl_domhsh_addr4_entry(addr_iter);
638}
639
640#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
641/**
642 * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
643 * @domain: the domain name to search for
644 * @addr: the IP address to search for
645 *
646 * Description:
647 * Look through the domain hash table searching for an entry to match @domain
648 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
649 * responsible for ensuring that rcu_read_[un]lock() is called.
650 *
651 */
652struct netlbl_domaddr6_map *netlbl_domhsh_getentry_af6(const char *domain,
653 const struct in6_addr *addr)
654{
655 struct netlbl_dom_map *dom_iter;
656 struct netlbl_af6list *addr_iter;
657
658 dom_iter = netlbl_domhsh_search_def(domain);
659 if (dom_iter == NULL)
660 return NULL;
661 if (dom_iter->type != NETLBL_NLTYPE_ADDRSELECT)
662 return NULL;
663
664 addr_iter = netlbl_af6list_search(addr,
665 &dom_iter->type_def.addrsel->list6);
666 if (addr_iter == NULL)
667 return NULL;
668
669 return netlbl_domhsh_addr6_entry(addr_iter);
670}
671#endif /* IPv6 */
672
d15c345f 673/**
fcd48280
PM
674 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
675 * @skip_bkt: the number of buckets to skip at the start
676 * @skip_chain: the number of entries to skip in the first iterated bucket
677 * @callback: callback for each entry
678 * @cb_arg: argument for the callback function
d15c345f
PM
679 *
680 * Description:
fcd48280
PM
681 * Interate over the domain mapping hash table, skipping the first @skip_bkt
682 * buckets and @skip_chain entries. For each entry in the table call
683 * @callback, if @callback returns a negative value stop 'walking' through the
684 * table and return. Updates the values in @skip_bkt and @skip_chain on
af901ca1 685 * return. Returns zero on success, negative values on failure.
d15c345f
PM
686 *
687 */
fcd48280
PM
688int netlbl_domhsh_walk(u32 *skip_bkt,
689 u32 *skip_chain,
690 int (*callback) (struct netlbl_dom_map *entry, void *arg),
691 void *cb_arg)
d15c345f 692{
fcd48280
PM
693 int ret_val = -ENOENT;
694 u32 iter_bkt;
56196701 695 struct list_head *iter_list;
fcd48280
PM
696 struct netlbl_dom_map *iter_entry;
697 u32 chain_cnt = 0;
d15c345f 698
d15c345f 699 rcu_read_lock();
fcd48280
PM
700 for (iter_bkt = *skip_bkt;
701 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
702 iter_bkt++, chain_cnt = 0) {
56196701
PM
703 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
704 list_for_each_entry_rcu(iter_entry, iter_list, list)
fcd48280
PM
705 if (iter_entry->valid) {
706 if (chain_cnt++ < *skip_chain)
707 continue;
708 ret_val = callback(iter_entry, cb_arg);
709 if (ret_val < 0) {
710 chain_cnt--;
711 goto walk_return;
712 }
d15c345f 713 }
fcd48280 714 }
d15c345f 715
fcd48280 716walk_return:
d15c345f 717 rcu_read_unlock();
fcd48280
PM
718 *skip_bkt = iter_bkt;
719 *skip_chain = chain_cnt;
720 return ret_val;
d15c345f 721}