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@paul-moore.com>
14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
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, see <http://www.gnu.org/licenses/>.
31 #include <linux/types.h>
32 #include <linux/rculist.h>
33 #include <linux/skbuff.h>
34 #include <linux/spinlock.h>
35 #include <linux/string.h>
36 #include <linux/audit.h>
37 #include <linux/slab.h>
38 #include <net/netlabel.h>
39 #include <net/cipso_ipv4.h>
40 #include <net/calipso.h>
43 #include "netlabel_mgmt.h"
44 #include "netlabel_addrlist.h"
45 #include "netlabel_calipso.h"
46 #include "netlabel_domainhash.h"
47 #include "netlabel_user.h"
49 struct netlbl_domhsh_tbl {
50 struct list_head *tbl;
54 /* Domain hash table */
55 /* updates should be so rare that having one spinlock for the entire hash table
57 static DEFINE_SPINLOCK(netlbl_domhsh_lock);
58 #define netlbl_domhsh_rcu_deref(p) \
59 rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
60 static struct netlbl_domhsh_tbl __rcu *netlbl_domhsh;
61 static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv4;
62 static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv6;
65 * Domain Hash Table Helper Functions
69 * netlbl_domhsh_free_entry - Frees a domain hash table entry
70 * @entry: the entry's RCU field
73 * This function is designed to be used as a callback to the call_rcu()
74 * function so that the memory allocated to a hash table entry can be released
78 static void netlbl_domhsh_free_entry(struct rcu_head *entry)
80 struct netlbl_dom_map *ptr;
81 struct netlbl_af4list *iter4;
82 struct netlbl_af4list *tmp4;
83 #if IS_ENABLED(CONFIG_IPV6)
84 struct netlbl_af6list *iter6;
85 struct netlbl_af6list *tmp6;
88 ptr = container_of(entry, struct netlbl_dom_map, rcu);
89 if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
90 netlbl_af4list_foreach_safe(iter4, tmp4,
91 &ptr->def.addrsel->list4) {
92 netlbl_af4list_remove_entry(iter4);
93 kfree(netlbl_domhsh_addr4_entry(iter4));
95 #if IS_ENABLED(CONFIG_IPV6)
96 netlbl_af6list_foreach_safe(iter6, tmp6,
97 &ptr->def.addrsel->list6) {
98 netlbl_af6list_remove_entry(iter6);
99 kfree(netlbl_domhsh_addr6_entry(iter6));
102 kfree(ptr->def.addrsel);
109 * netlbl_domhsh_hash - Hashing function for the domain hash table
110 * @domain: the domain name to hash
113 * This is the hashing function for the domain hash table, it returns the
114 * correct bucket number for the domain. The caller is responsible for
115 * ensuring that the hash table is protected with either a RCU read lock or the
119 static u32 netlbl_domhsh_hash(const char *key)
125 /* This is taken (with slight modification) from
126 * security/selinux/ss/symtab.c:symhash() */
128 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
129 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
130 return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
133 static bool netlbl_family_match(u16 f1, u16 f2)
135 return (f1 == f2) || (f1 == AF_UNSPEC) || (f2 == AF_UNSPEC);
139 * netlbl_domhsh_search - Search for a domain entry
140 * @domain: the domain
141 * @family: the address family
144 * Searches the domain hash table and returns a pointer to the hash table
145 * entry if found, otherwise NULL is returned. @family may be %AF_UNSPEC
146 * which matches any address family entries. The caller is responsible for
147 * ensuring that the hash table is protected with either a RCU read lock or the
151 static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain,
155 struct list_head *bkt_list;
156 struct netlbl_dom_map *iter;
158 if (domain != NULL) {
159 bkt = netlbl_domhsh_hash(domain);
160 bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
161 list_for_each_entry_rcu(iter, bkt_list, list)
163 netlbl_family_match(iter->family, family) &&
164 strcmp(iter->domain, domain) == 0)
172 * netlbl_domhsh_search_def - Search for a domain entry
173 * @domain: the domain
174 * @family: the address family
177 * Searches the domain hash table and returns a pointer to the hash table
178 * entry if an exact match is found, if an exact match is not present in the
179 * hash table then the default entry is returned if valid otherwise NULL is
180 * returned. @family may be %AF_UNSPEC which matches any address family
181 * entries. The caller is responsible ensuring that the hash table is
182 * protected with either a RCU read lock or the hash table lock.
185 static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain,
188 struct netlbl_dom_map *entry;
190 entry = netlbl_domhsh_search(domain, family);
193 if (family == AF_INET || family == AF_UNSPEC) {
194 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4);
195 if (entry != NULL && entry->valid)
198 if (family == AF_INET6 || family == AF_UNSPEC) {
199 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6);
200 if (entry != NULL && entry->valid)
208 * netlbl_domhsh_audit_add - Generate an audit entry for an add event
209 * @entry: the entry being added
210 * @addr4: the IPv4 address information
211 * @addr6: the IPv6 address information
212 * @result: the result code
213 * @audit_info: NetLabel audit information
216 * Generate an audit record for adding a new NetLabel/LSM mapping entry with
217 * the given information. Caller is responsible for holding the necessary
221 static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
222 struct netlbl_af4list *addr4,
223 struct netlbl_af6list *addr6,
225 struct netlbl_audit *audit_info)
227 struct audit_buffer *audit_buf;
228 struct cipso_v4_doi *cipsov4 = NULL;
229 struct calipso_doi *calipso = NULL;
232 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
233 if (audit_buf != NULL) {
234 audit_log_format(audit_buf, " nlbl_domain=%s",
235 entry->domain ? entry->domain : "(default)");
237 struct netlbl_domaddr4_map *map4;
238 map4 = netlbl_domhsh_addr4_entry(addr4);
239 type = map4->def.type;
240 cipsov4 = map4->def.cipso;
241 netlbl_af4list_audit_addr(audit_buf, 0, NULL,
242 addr4->addr, addr4->mask);
243 #if IS_ENABLED(CONFIG_IPV6)
244 } else if (addr6 != NULL) {
245 struct netlbl_domaddr6_map *map6;
246 map6 = netlbl_domhsh_addr6_entry(addr6);
247 type = map6->def.type;
248 calipso = map6->def.calipso;
249 netlbl_af6list_audit_addr(audit_buf, 0, NULL,
250 &addr6->addr, &addr6->mask);
253 type = entry->def.type;
254 cipsov4 = entry->def.cipso;
255 calipso = entry->def.calipso;
258 case NETLBL_NLTYPE_UNLABELED:
259 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
261 case NETLBL_NLTYPE_CIPSOV4:
262 BUG_ON(cipsov4 == NULL);
263 audit_log_format(audit_buf,
264 " nlbl_protocol=cipsov4 cipso_doi=%u",
267 case NETLBL_NLTYPE_CALIPSO:
268 BUG_ON(calipso == NULL);
269 audit_log_format(audit_buf,
270 " nlbl_protocol=calipso calipso_doi=%u",
274 audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
275 audit_log_end(audit_buf);
280 * netlbl_domhsh_validate - Validate a new domain mapping entry
281 * @entry: the entry to validate
283 * This function validates the new domain mapping entry to ensure that it is
284 * a valid entry. Returns zero on success, negative values on failure.
287 static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
289 struct netlbl_af4list *iter4;
290 struct netlbl_domaddr4_map *map4;
291 #if IS_ENABLED(CONFIG_IPV6)
292 struct netlbl_af6list *iter6;
293 struct netlbl_domaddr6_map *map6;
299 if (entry->family != AF_INET && entry->family != AF_INET6 &&
300 (entry->family != AF_UNSPEC ||
301 entry->def.type != NETLBL_NLTYPE_UNLABELED))
304 switch (entry->def.type) {
305 case NETLBL_NLTYPE_UNLABELED:
306 if (entry->def.cipso != NULL || entry->def.calipso != NULL ||
307 entry->def.addrsel != NULL)
310 case NETLBL_NLTYPE_CIPSOV4:
311 if (entry->family != AF_INET ||
312 entry->def.cipso == NULL)
315 case NETLBL_NLTYPE_CALIPSO:
316 if (entry->family != AF_INET6 ||
317 entry->def.calipso == NULL)
320 case NETLBL_NLTYPE_ADDRSELECT:
321 netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
322 map4 = netlbl_domhsh_addr4_entry(iter4);
323 switch (map4->def.type) {
324 case NETLBL_NLTYPE_UNLABELED:
325 if (map4->def.cipso != NULL)
328 case NETLBL_NLTYPE_CIPSOV4:
329 if (map4->def.cipso == NULL)
336 #if IS_ENABLED(CONFIG_IPV6)
337 netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
338 map6 = netlbl_domhsh_addr6_entry(iter6);
339 switch (map6->def.type) {
340 case NETLBL_NLTYPE_UNLABELED:
341 if (map6->def.calipso != NULL)
344 case NETLBL_NLTYPE_CALIPSO:
345 if (map6->def.calipso == NULL)
362 * Domain Hash Table Functions
366 * netlbl_domhsh_init - Init for the domain hash
367 * @size: the number of bits to use for the hash buckets
370 * Initializes the domain hash table, should be called only by
371 * netlbl_user_init() during initialization. Returns zero on success, non-zero
375 int __init netlbl_domhsh_init(u32 size)
378 struct netlbl_domhsh_tbl *hsh_tbl;
383 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
386 hsh_tbl->size = 1 << size;
387 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
388 sizeof(struct list_head),
390 if (hsh_tbl->tbl == NULL) {
394 for (iter = 0; iter < hsh_tbl->size; iter++)
395 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
397 spin_lock(&netlbl_domhsh_lock);
398 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
399 spin_unlock(&netlbl_domhsh_lock);
405 * netlbl_domhsh_add - Adds a entry to the domain hash table
406 * @entry: the entry to add
407 * @audit_info: NetLabel audit information
410 * Adds a new entry to the domain hash table and handles any updates to the
411 * lower level protocol handler (i.e. CIPSO). @entry->family may be set to
412 * %AF_UNSPEC which will add an entry that matches all address families. This
413 * is only useful for the unlabelled type and will only succeed if there is no
414 * existing entry for any address family with the same domain. Returns zero
415 * on success, negative on failure.
418 int netlbl_domhsh_add(struct netlbl_dom_map *entry,
419 struct netlbl_audit *audit_info)
422 struct netlbl_dom_map *entry_old, *entry_b;
423 struct netlbl_af4list *iter4;
424 struct netlbl_af4list *tmp4;
425 #if IS_ENABLED(CONFIG_IPV6)
426 struct netlbl_af6list *iter6;
427 struct netlbl_af6list *tmp6;
430 ret_val = netlbl_domhsh_validate(entry);
434 /* XXX - we can remove this RCU read lock as the spinlock protects the
435 * entire function, but before we do we need to fixup the
436 * netlbl_af[4,6]list RCU functions to do "the right thing" with
437 * respect to rcu_dereference() when only a spinlock is held. */
439 spin_lock(&netlbl_domhsh_lock);
440 if (entry->domain != NULL)
441 entry_old = netlbl_domhsh_search(entry->domain, entry->family);
443 entry_old = netlbl_domhsh_search_def(entry->domain,
445 if (entry_old == NULL) {
448 if (entry->domain != NULL) {
449 u32 bkt = netlbl_domhsh_hash(entry->domain);
450 list_add_tail_rcu(&entry->list,
451 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
453 INIT_LIST_HEAD(&entry->list);
454 switch (entry->family) {
456 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
460 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
464 if (entry->def.type !=
465 NETLBL_NLTYPE_UNLABELED) {
469 entry_b = kzalloc(sizeof(*entry_b), GFP_ATOMIC);
470 if (entry_b == NULL) {
474 entry_b->family = AF_INET6;
475 entry_b->def.type = NETLBL_NLTYPE_UNLABELED;
477 entry->family = AF_INET;
478 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
480 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
484 /* Already checked in
485 * netlbl_domhsh_validate(). */
491 if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
492 netlbl_af4list_foreach_rcu(iter4,
493 &entry->def.addrsel->list4)
494 netlbl_domhsh_audit_add(entry, iter4, NULL,
495 ret_val, audit_info);
496 #if IS_ENABLED(CONFIG_IPV6)
497 netlbl_af6list_foreach_rcu(iter6,
498 &entry->def.addrsel->list6)
499 netlbl_domhsh_audit_add(entry, NULL, iter6,
500 ret_val, audit_info);
503 netlbl_domhsh_audit_add(entry, NULL, NULL,
504 ret_val, audit_info);
505 } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
506 entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
507 struct list_head *old_list4;
508 struct list_head *old_list6;
510 old_list4 = &entry_old->def.addrsel->list4;
511 old_list6 = &entry_old->def.addrsel->list6;
513 /* we only allow the addition of address selectors if all of
514 * the selectors do not exist in the existing domain map */
515 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
516 if (netlbl_af4list_search_exact(iter4->addr,
522 #if IS_ENABLED(CONFIG_IPV6)
523 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
524 if (netlbl_af6list_search_exact(&iter6->addr,
532 netlbl_af4list_foreach_safe(iter4, tmp4,
533 &entry->def.addrsel->list4) {
534 netlbl_af4list_remove_entry(iter4);
536 ret_val = netlbl_af4list_add(iter4, old_list4);
537 netlbl_domhsh_audit_add(entry_old, iter4, NULL,
538 ret_val, audit_info);
542 #if IS_ENABLED(CONFIG_IPV6)
543 netlbl_af6list_foreach_safe(iter6, tmp6,
544 &entry->def.addrsel->list6) {
545 netlbl_af6list_remove_entry(iter6);
547 ret_val = netlbl_af6list_add(iter6, old_list6);
548 netlbl_domhsh_audit_add(entry_old, NULL, iter6,
549 ret_val, audit_info);
554 /* cleanup the new entry since we've moved everything over */
555 netlbl_domhsh_free_entry(&entry->rcu);
560 spin_unlock(&netlbl_domhsh_lock);
566 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
567 * @entry: the entry to add
568 * @audit_info: NetLabel audit information
571 * Adds a new default entry to the domain hash table and handles any updates
572 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
573 * negative on failure.
576 int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
577 struct netlbl_audit *audit_info)
579 return netlbl_domhsh_add(entry, audit_info);
583 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
584 * @entry: the entry to remove
585 * @audit_info: NetLabel audit information
588 * Removes an entry from the domain hash table and handles any updates to the
589 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
590 * ensuring that the RCU read lock is held. Returns zero on success, negative
594 int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
595 struct netlbl_audit *audit_info)
598 struct audit_buffer *audit_buf;
599 struct netlbl_af4list *iter4;
600 struct netlbl_domaddr4_map *map4;
601 #if IS_ENABLED(CONFIG_IPV6)
602 struct netlbl_af6list *iter6;
603 struct netlbl_domaddr6_map *map6;
609 spin_lock(&netlbl_domhsh_lock);
612 if (entry == rcu_dereference(netlbl_domhsh_def_ipv4))
613 RCU_INIT_POINTER(netlbl_domhsh_def_ipv4, NULL);
614 else if (entry == rcu_dereference(netlbl_domhsh_def_ipv6))
615 RCU_INIT_POINTER(netlbl_domhsh_def_ipv6, NULL);
617 list_del_rcu(&entry->list);
620 spin_unlock(&netlbl_domhsh_lock);
625 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
626 if (audit_buf != NULL) {
627 audit_log_format(audit_buf,
628 " nlbl_domain=%s res=%u",
629 entry->domain ? entry->domain : "(default)",
630 ret_val == 0 ? 1 : 0);
631 audit_log_end(audit_buf);
634 switch (entry->def.type) {
635 case NETLBL_NLTYPE_ADDRSELECT:
636 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
637 map4 = netlbl_domhsh_addr4_entry(iter4);
638 cipso_v4_doi_putdef(map4->def.cipso);
640 #if IS_ENABLED(CONFIG_IPV6)
641 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
642 map6 = netlbl_domhsh_addr6_entry(iter6);
643 calipso_doi_putdef(map6->def.calipso);
647 case NETLBL_NLTYPE_CIPSOV4:
648 cipso_v4_doi_putdef(entry->def.cipso);
650 #if IS_ENABLED(CONFIG_IPV6)
651 case NETLBL_NLTYPE_CALIPSO:
652 calipso_doi_putdef(entry->def.calipso);
656 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
662 * netlbl_domhsh_remove_af4 - Removes an address selector entry
663 * @domain: the domain
664 * @addr: IPv4 address
665 * @mask: IPv4 address mask
666 * @audit_info: NetLabel audit information
669 * Removes an individual address selector from a domain mapping and potentially
670 * the entire mapping if it is empty. Returns zero on success, negative values
674 int netlbl_domhsh_remove_af4(const char *domain,
675 const struct in_addr *addr,
676 const struct in_addr *mask,
677 struct netlbl_audit *audit_info)
679 struct netlbl_dom_map *entry_map;
680 struct netlbl_af4list *entry_addr;
681 struct netlbl_af4list *iter4;
682 #if IS_ENABLED(CONFIG_IPV6)
683 struct netlbl_af6list *iter6;
685 struct netlbl_domaddr4_map *entry;
690 entry_map = netlbl_domhsh_search(domain, AF_INET);
692 entry_map = netlbl_domhsh_search_def(domain, AF_INET);
693 if (entry_map == NULL ||
694 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
695 goto remove_af4_failure;
697 spin_lock(&netlbl_domhsh_lock);
698 entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
699 &entry_map->def.addrsel->list4);
700 spin_unlock(&netlbl_domhsh_lock);
702 if (entry_addr == NULL)
703 goto remove_af4_failure;
704 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
705 goto remove_af4_single_addr;
706 #if IS_ENABLED(CONFIG_IPV6)
707 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
708 goto remove_af4_single_addr;
710 /* the domain mapping is empty so remove it from the mapping table */
711 netlbl_domhsh_remove_entry(entry_map, audit_info);
713 remove_af4_single_addr:
715 /* yick, we can't use call_rcu here because we don't have a rcu head
716 * pointer but hopefully this should be a rare case so the pause
717 * shouldn't be a problem */
719 entry = netlbl_domhsh_addr4_entry(entry_addr);
720 cipso_v4_doi_putdef(entry->def.cipso);
729 #if IS_ENABLED(CONFIG_IPV6)
731 * netlbl_domhsh_remove_af6 - Removes an address selector entry
732 * @domain: the domain
733 * @addr: IPv6 address
734 * @mask: IPv6 address mask
735 * @audit_info: NetLabel audit information
738 * Removes an individual address selector from a domain mapping and potentially
739 * the entire mapping if it is empty. Returns zero on success, negative values
743 int netlbl_domhsh_remove_af6(const char *domain,
744 const struct in6_addr *addr,
745 const struct in6_addr *mask,
746 struct netlbl_audit *audit_info)
748 struct netlbl_dom_map *entry_map;
749 struct netlbl_af6list *entry_addr;
750 struct netlbl_af4list *iter4;
751 struct netlbl_af6list *iter6;
752 struct netlbl_domaddr6_map *entry;
757 entry_map = netlbl_domhsh_search(domain, AF_INET6);
759 entry_map = netlbl_domhsh_search_def(domain, AF_INET6);
760 if (entry_map == NULL ||
761 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
762 goto remove_af6_failure;
764 spin_lock(&netlbl_domhsh_lock);
765 entry_addr = netlbl_af6list_remove(addr, mask,
766 &entry_map->def.addrsel->list6);
767 spin_unlock(&netlbl_domhsh_lock);
769 if (entry_addr == NULL)
770 goto remove_af6_failure;
771 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
772 goto remove_af6_single_addr;
773 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
774 goto remove_af6_single_addr;
775 /* the domain mapping is empty so remove it from the mapping table */
776 netlbl_domhsh_remove_entry(entry_map, audit_info);
778 remove_af6_single_addr:
780 /* yick, we can't use call_rcu here because we don't have a rcu head
781 * pointer but hopefully this should be a rare case so the pause
782 * shouldn't be a problem */
784 entry = netlbl_domhsh_addr6_entry(entry_addr);
785 calipso_doi_putdef(entry->def.calipso);
796 * netlbl_domhsh_remove - Removes an entry from the domain hash table
797 * @domain: the domain to remove
798 * @family: address family
799 * @audit_info: NetLabel audit information
802 * Removes an entry from the domain hash table and handles any updates to the
803 * lower level protocol handler (i.e. CIPSO). @family may be %AF_UNSPEC which
804 * removes all address family entries. Returns zero on success, negative on
808 int netlbl_domhsh_remove(const char *domain, u16 family,
809 struct netlbl_audit *audit_info)
811 int ret_val = -EINVAL;
812 struct netlbl_dom_map *entry;
816 if (family == AF_INET || family == AF_UNSPEC) {
818 entry = netlbl_domhsh_search(domain, AF_INET);
820 entry = netlbl_domhsh_search_def(domain, AF_INET);
821 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
822 if (ret_val && ret_val != -ENOENT)
825 if (family == AF_INET6 || family == AF_UNSPEC) {
829 entry = netlbl_domhsh_search(domain, AF_INET6);
831 entry = netlbl_domhsh_search_def(domain, AF_INET6);
832 ret_val2 = netlbl_domhsh_remove_entry(entry, audit_info);
833 if (ret_val2 != -ENOENT)
843 * netlbl_domhsh_remove_default - Removes the default entry from the table
844 * @family: address family
845 * @audit_info: NetLabel audit information
848 * Removes/resets the default entry corresponding to @family from the domain
849 * hash table and handles any updates to the lower level protocol handler
850 * (i.e. CIPSO). @family may be %AF_UNSPEC which removes all address family
851 * entries. Returns zero on success, negative on failure.
854 int netlbl_domhsh_remove_default(u16 family, struct netlbl_audit *audit_info)
856 return netlbl_domhsh_remove(NULL, family, audit_info);
860 * netlbl_domhsh_getentry - Get an entry from the domain hash table
861 * @domain: the domain name to search for
862 * @family: address family
865 * Look through the domain hash table searching for an entry to match @domain,
866 * with address family @family, return a pointer to a copy of the entry or
867 * NULL. The caller is responsible for ensuring that rcu_read_[un]lock() is
871 struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain, u16 family)
873 if (family == AF_UNSPEC)
875 return netlbl_domhsh_search_def(domain, family);
879 * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
880 * @domain: the domain name to search for
881 * @addr: the IP address to search for
884 * Look through the domain hash table searching for an entry to match @domain
885 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
886 * responsible for ensuring that rcu_read_[un]lock() is called.
889 struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
892 struct netlbl_dom_map *dom_iter;
893 struct netlbl_af4list *addr_iter;
895 dom_iter = netlbl_domhsh_search_def(domain, AF_INET);
896 if (dom_iter == NULL)
899 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
900 return &dom_iter->def;
901 addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
902 if (addr_iter == NULL)
904 return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
907 #if IS_ENABLED(CONFIG_IPV6)
909 * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
910 * @domain: the domain name to search for
911 * @addr: the IP address to search for
914 * Look through the domain hash table searching for an entry to match @domain
915 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
916 * responsible for ensuring that rcu_read_[un]lock() is called.
919 struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
920 const struct in6_addr *addr)
922 struct netlbl_dom_map *dom_iter;
923 struct netlbl_af6list *addr_iter;
925 dom_iter = netlbl_domhsh_search_def(domain, AF_INET6);
926 if (dom_iter == NULL)
929 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
930 return &dom_iter->def;
931 addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
932 if (addr_iter == NULL)
934 return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
939 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
940 * @skip_bkt: the number of buckets to skip at the start
941 * @skip_chain: the number of entries to skip in the first iterated bucket
942 * @callback: callback for each entry
943 * @cb_arg: argument for the callback function
946 * Interate over the domain mapping hash table, skipping the first @skip_bkt
947 * buckets and @skip_chain entries. For each entry in the table call
948 * @callback, if @callback returns a negative value stop 'walking' through the
949 * table and return. Updates the values in @skip_bkt and @skip_chain on
950 * return. Returns zero on success, negative values on failure.
953 int netlbl_domhsh_walk(u32 *skip_bkt,
955 int (*callback) (struct netlbl_dom_map *entry, void *arg),
958 int ret_val = -ENOENT;
960 struct list_head *iter_list;
961 struct netlbl_dom_map *iter_entry;
965 for (iter_bkt = *skip_bkt;
966 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
967 iter_bkt++, chain_cnt = 0) {
968 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
969 list_for_each_entry_rcu(iter_entry, iter_list, list)
970 if (iter_entry->valid) {
971 if (chain_cnt++ < *skip_chain)
973 ret_val = callback(iter_entry, cb_arg);
983 *skip_bkt = iter_bkt;
984 *skip_chain = chain_cnt;