1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NetLabel Domain Hash Table
5 * This file manages the domain hash table that NetLabel uses to determine
6 * which network labeling protocol to use for a given domain. The NetLabel
7 * system manages static and dynamic label mappings for network protocols such
10 * Author: Paul Moore <paul@paul-moore.com>
14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
17 #include <linux/types.h>
18 #include <linux/rculist.h>
19 #include <linux/skbuff.h>
20 #include <linux/spinlock.h>
21 #include <linux/string.h>
22 #include <linux/audit.h>
23 #include <linux/slab.h>
24 #include <net/netlabel.h>
25 #include <net/cipso_ipv4.h>
26 #include <net/calipso.h>
29 #include "netlabel_mgmt.h"
30 #include "netlabel_addrlist.h"
31 #include "netlabel_calipso.h"
32 #include "netlabel_domainhash.h"
33 #include "netlabel_user.h"
35 struct netlbl_domhsh_tbl
{
36 struct list_head
*tbl
;
40 /* Domain hash table */
41 /* updates should be so rare that having one spinlock for the entire hash table
43 static DEFINE_SPINLOCK(netlbl_domhsh_lock
);
44 #define netlbl_domhsh_rcu_deref(p) \
45 rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
46 static struct netlbl_domhsh_tbl __rcu
*netlbl_domhsh
;
47 static struct netlbl_dom_map __rcu
*netlbl_domhsh_def_ipv4
;
48 static struct netlbl_dom_map __rcu
*netlbl_domhsh_def_ipv6
;
51 * Domain Hash Table Helper Functions
55 * netlbl_domhsh_free_entry - Frees a domain hash table entry
56 * @entry: the entry's RCU field
59 * This function is designed to be used as a callback to the call_rcu()
60 * function so that the memory allocated to a hash table entry can be released
64 static void netlbl_domhsh_free_entry(struct rcu_head
*entry
)
66 struct netlbl_dom_map
*ptr
;
67 struct netlbl_af4list
*iter4
;
68 struct netlbl_af4list
*tmp4
;
69 #if IS_ENABLED(CONFIG_IPV6)
70 struct netlbl_af6list
*iter6
;
71 struct netlbl_af6list
*tmp6
;
74 ptr
= container_of(entry
, struct netlbl_dom_map
, rcu
);
75 if (ptr
->def
.type
== NETLBL_NLTYPE_ADDRSELECT
) {
76 netlbl_af4list_foreach_safe(iter4
, tmp4
,
77 &ptr
->def
.addrsel
->list4
) {
78 netlbl_af4list_remove_entry(iter4
);
79 kfree(netlbl_domhsh_addr4_entry(iter4
));
81 #if IS_ENABLED(CONFIG_IPV6)
82 netlbl_af6list_foreach_safe(iter6
, tmp6
,
83 &ptr
->def
.addrsel
->list6
) {
84 netlbl_af6list_remove_entry(iter6
);
85 kfree(netlbl_domhsh_addr6_entry(iter6
));
88 kfree(ptr
->def
.addrsel
);
95 * netlbl_domhsh_hash - Hashing function for the domain hash table
96 * @key: the domain name to hash
99 * This is the hashing function for the domain hash table, it returns the
100 * correct bucket number for the domain. The caller is responsible for
101 * ensuring that the hash table is protected with either a RCU read lock or the
105 static u32
netlbl_domhsh_hash(const char *key
)
111 /* This is taken (with slight modification) from
112 * security/selinux/ss/symtab.c:symhash() */
114 for (iter
= 0, val
= 0, len
= strlen(key
); iter
< len
; iter
++)
115 val
= (val
<< 4 | (val
>> (8 * sizeof(u32
) - 4))) ^ key
[iter
];
116 return val
& (netlbl_domhsh_rcu_deref(netlbl_domhsh
)->size
- 1);
119 static bool netlbl_family_match(u16 f1
, u16 f2
)
121 return (f1
== f2
) || (f1
== AF_UNSPEC
) || (f2
== AF_UNSPEC
);
125 * netlbl_domhsh_search - Search for a domain entry
126 * @domain: the domain
127 * @family: the address family
130 * Searches the domain hash table and returns a pointer to the hash table
131 * entry if found, otherwise NULL is returned. @family may be %AF_UNSPEC
132 * which matches any address family entries. The caller is responsible for
133 * ensuring that the hash table is protected with either a RCU read lock or the
137 static struct netlbl_dom_map
*netlbl_domhsh_search(const char *domain
,
141 struct list_head
*bkt_list
;
142 struct netlbl_dom_map
*iter
;
144 if (domain
!= NULL
) {
145 bkt
= netlbl_domhsh_hash(domain
);
146 bkt_list
= &netlbl_domhsh_rcu_deref(netlbl_domhsh
)->tbl
[bkt
];
147 list_for_each_entry_rcu(iter
, bkt_list
, list
,
148 lockdep_is_held(&netlbl_domhsh_lock
))
150 netlbl_family_match(iter
->family
, family
) &&
151 strcmp(iter
->domain
, domain
) == 0)
159 * netlbl_domhsh_search_def - Search for a domain entry
160 * @domain: the domain
161 * @family: the address family
164 * Searches the domain hash table and returns a pointer to the hash table
165 * entry if an exact match is found, if an exact match is not present in the
166 * hash table then the default entry is returned if valid otherwise NULL is
167 * returned. @family may be %AF_UNSPEC which matches any address family
168 * entries. The caller is responsible ensuring that the hash table is
169 * protected with either a RCU read lock or the hash table lock.
172 static struct netlbl_dom_map
*netlbl_domhsh_search_def(const char *domain
,
175 struct netlbl_dom_map
*entry
;
177 entry
= netlbl_domhsh_search(domain
, family
);
180 if (family
== AF_INET
|| family
== AF_UNSPEC
) {
181 entry
= netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4
);
182 if (entry
!= NULL
&& entry
->valid
)
185 if (family
== AF_INET6
|| family
== AF_UNSPEC
) {
186 entry
= netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6
);
187 if (entry
!= NULL
&& entry
->valid
)
195 * netlbl_domhsh_audit_add - Generate an audit entry for an add event
196 * @entry: the entry being added
197 * @addr4: the IPv4 address information
198 * @addr6: the IPv6 address information
199 * @result: the result code
200 * @audit_info: NetLabel audit information
203 * Generate an audit record for adding a new NetLabel/LSM mapping entry with
204 * the given information. Caller is responsible for holding the necessary
208 static void netlbl_domhsh_audit_add(struct netlbl_dom_map
*entry
,
209 struct netlbl_af4list
*addr4
,
210 struct netlbl_af6list
*addr6
,
212 struct netlbl_audit
*audit_info
)
214 struct audit_buffer
*audit_buf
;
215 struct cipso_v4_doi
*cipsov4
= NULL
;
216 struct calipso_doi
*calipso
= NULL
;
219 audit_buf
= netlbl_audit_start_common(AUDIT_MAC_MAP_ADD
, audit_info
);
220 if (audit_buf
!= NULL
) {
221 audit_log_format(audit_buf
, " nlbl_domain=%s",
222 entry
->domain
? entry
->domain
: "(default)");
224 struct netlbl_domaddr4_map
*map4
;
225 map4
= netlbl_domhsh_addr4_entry(addr4
);
226 type
= map4
->def
.type
;
227 cipsov4
= map4
->def
.cipso
;
228 netlbl_af4list_audit_addr(audit_buf
, 0, NULL
,
229 addr4
->addr
, addr4
->mask
);
230 #if IS_ENABLED(CONFIG_IPV6)
231 } else if (addr6
!= NULL
) {
232 struct netlbl_domaddr6_map
*map6
;
233 map6
= netlbl_domhsh_addr6_entry(addr6
);
234 type
= map6
->def
.type
;
235 calipso
= map6
->def
.calipso
;
236 netlbl_af6list_audit_addr(audit_buf
, 0, NULL
,
237 &addr6
->addr
, &addr6
->mask
);
240 type
= entry
->def
.type
;
241 cipsov4
= entry
->def
.cipso
;
242 calipso
= entry
->def
.calipso
;
245 case NETLBL_NLTYPE_UNLABELED
:
246 audit_log_format(audit_buf
, " nlbl_protocol=unlbl");
248 case NETLBL_NLTYPE_CIPSOV4
:
249 BUG_ON(cipsov4
== NULL
);
250 audit_log_format(audit_buf
,
251 " nlbl_protocol=cipsov4 cipso_doi=%u",
254 case NETLBL_NLTYPE_CALIPSO
:
255 BUG_ON(calipso
== NULL
);
256 audit_log_format(audit_buf
,
257 " nlbl_protocol=calipso calipso_doi=%u",
261 audit_log_format(audit_buf
, " res=%u", result
== 0 ? 1 : 0);
262 audit_log_end(audit_buf
);
267 * netlbl_domhsh_validate - Validate a new domain mapping entry
268 * @entry: the entry to validate
270 * This function validates the new domain mapping entry to ensure that it is
271 * a valid entry. Returns zero on success, negative values on failure.
274 static int netlbl_domhsh_validate(const struct netlbl_dom_map
*entry
)
276 struct netlbl_af4list
*iter4
;
277 struct netlbl_domaddr4_map
*map4
;
278 #if IS_ENABLED(CONFIG_IPV6)
279 struct netlbl_af6list
*iter6
;
280 struct netlbl_domaddr6_map
*map6
;
286 if (entry
->family
!= AF_INET
&& entry
->family
!= AF_INET6
&&
287 (entry
->family
!= AF_UNSPEC
||
288 entry
->def
.type
!= NETLBL_NLTYPE_UNLABELED
))
291 switch (entry
->def
.type
) {
292 case NETLBL_NLTYPE_UNLABELED
:
293 if (entry
->def
.cipso
!= NULL
|| entry
->def
.calipso
!= NULL
||
294 entry
->def
.addrsel
!= NULL
)
297 case NETLBL_NLTYPE_CIPSOV4
:
298 if (entry
->family
!= AF_INET
||
299 entry
->def
.cipso
== NULL
)
302 case NETLBL_NLTYPE_CALIPSO
:
303 if (entry
->family
!= AF_INET6
||
304 entry
->def
.calipso
== NULL
)
307 case NETLBL_NLTYPE_ADDRSELECT
:
308 netlbl_af4list_foreach(iter4
, &entry
->def
.addrsel
->list4
) {
309 map4
= netlbl_domhsh_addr4_entry(iter4
);
310 switch (map4
->def
.type
) {
311 case NETLBL_NLTYPE_UNLABELED
:
312 if (map4
->def
.cipso
!= NULL
)
315 case NETLBL_NLTYPE_CIPSOV4
:
316 if (map4
->def
.cipso
== NULL
)
323 #if IS_ENABLED(CONFIG_IPV6)
324 netlbl_af6list_foreach(iter6
, &entry
->def
.addrsel
->list6
) {
325 map6
= netlbl_domhsh_addr6_entry(iter6
);
326 switch (map6
->def
.type
) {
327 case NETLBL_NLTYPE_UNLABELED
:
328 if (map6
->def
.calipso
!= NULL
)
331 case NETLBL_NLTYPE_CALIPSO
:
332 if (map6
->def
.calipso
== NULL
)
349 * Domain Hash Table Functions
353 * netlbl_domhsh_init - Init for the domain hash
354 * @size: the number of bits to use for the hash buckets
357 * Initializes the domain hash table, should be called only by
358 * netlbl_user_init() during initialization. Returns zero on success, non-zero
362 int __init
netlbl_domhsh_init(u32 size
)
365 struct netlbl_domhsh_tbl
*hsh_tbl
;
370 hsh_tbl
= kmalloc(sizeof(*hsh_tbl
), GFP_KERNEL
);
373 hsh_tbl
->size
= 1 << size
;
374 hsh_tbl
->tbl
= kcalloc(hsh_tbl
->size
,
375 sizeof(struct list_head
),
377 if (hsh_tbl
->tbl
== NULL
) {
381 for (iter
= 0; iter
< hsh_tbl
->size
; iter
++)
382 INIT_LIST_HEAD(&hsh_tbl
->tbl
[iter
]);
384 spin_lock(&netlbl_domhsh_lock
);
385 rcu_assign_pointer(netlbl_domhsh
, hsh_tbl
);
386 spin_unlock(&netlbl_domhsh_lock
);
392 * netlbl_domhsh_add - Adds a entry to the domain hash table
393 * @entry: the entry to add
394 * @audit_info: NetLabel audit information
397 * Adds a new entry to the domain hash table and handles any updates to the
398 * lower level protocol handler (i.e. CIPSO). @entry->family may be set to
399 * %AF_UNSPEC which will add an entry that matches all address families. This
400 * is only useful for the unlabelled type and will only succeed if there is no
401 * existing entry for any address family with the same domain. Returns zero
402 * on success, negative on failure.
405 int netlbl_domhsh_add(struct netlbl_dom_map
*entry
,
406 struct netlbl_audit
*audit_info
)
409 struct netlbl_dom_map
*entry_old
, *entry_b
;
410 struct netlbl_af4list
*iter4
;
411 struct netlbl_af4list
*tmp4
;
412 #if IS_ENABLED(CONFIG_IPV6)
413 struct netlbl_af6list
*iter6
;
414 struct netlbl_af6list
*tmp6
;
417 ret_val
= netlbl_domhsh_validate(entry
);
421 /* XXX - we can remove this RCU read lock as the spinlock protects the
422 * entire function, but before we do we need to fixup the
423 * netlbl_af[4,6]list RCU functions to do "the right thing" with
424 * respect to rcu_dereference() when only a spinlock is held. */
426 spin_lock(&netlbl_domhsh_lock
);
427 if (entry
->domain
!= NULL
)
428 entry_old
= netlbl_domhsh_search(entry
->domain
, entry
->family
);
430 entry_old
= netlbl_domhsh_search_def(entry
->domain
,
432 if (entry_old
== NULL
) {
435 if (entry
->domain
!= NULL
) {
436 u32 bkt
= netlbl_domhsh_hash(entry
->domain
);
437 list_add_tail_rcu(&entry
->list
,
438 &rcu_dereference(netlbl_domhsh
)->tbl
[bkt
]);
440 INIT_LIST_HEAD(&entry
->list
);
441 switch (entry
->family
) {
443 rcu_assign_pointer(netlbl_domhsh_def_ipv4
,
447 rcu_assign_pointer(netlbl_domhsh_def_ipv6
,
451 if (entry
->def
.type
!=
452 NETLBL_NLTYPE_UNLABELED
) {
456 entry_b
= kzalloc(sizeof(*entry_b
), GFP_ATOMIC
);
457 if (entry_b
== NULL
) {
461 entry_b
->family
= AF_INET6
;
462 entry_b
->def
.type
= NETLBL_NLTYPE_UNLABELED
;
464 entry
->family
= AF_INET
;
465 rcu_assign_pointer(netlbl_domhsh_def_ipv4
,
467 rcu_assign_pointer(netlbl_domhsh_def_ipv6
,
471 /* Already checked in
472 * netlbl_domhsh_validate(). */
478 if (entry
->def
.type
== NETLBL_NLTYPE_ADDRSELECT
) {
479 netlbl_af4list_foreach_rcu(iter4
,
480 &entry
->def
.addrsel
->list4
)
481 netlbl_domhsh_audit_add(entry
, iter4
, NULL
,
482 ret_val
, audit_info
);
483 #if IS_ENABLED(CONFIG_IPV6)
484 netlbl_af6list_foreach_rcu(iter6
,
485 &entry
->def
.addrsel
->list6
)
486 netlbl_domhsh_audit_add(entry
, NULL
, iter6
,
487 ret_val
, audit_info
);
490 netlbl_domhsh_audit_add(entry
, NULL
, NULL
,
491 ret_val
, audit_info
);
492 } else if (entry_old
->def
.type
== NETLBL_NLTYPE_ADDRSELECT
&&
493 entry
->def
.type
== NETLBL_NLTYPE_ADDRSELECT
) {
494 struct list_head
*old_list4
;
495 struct list_head
*old_list6
;
497 old_list4
= &entry_old
->def
.addrsel
->list4
;
498 old_list6
= &entry_old
->def
.addrsel
->list6
;
500 /* we only allow the addition of address selectors if all of
501 * the selectors do not exist in the existing domain map */
502 netlbl_af4list_foreach_rcu(iter4
, &entry
->def
.addrsel
->list4
)
503 if (netlbl_af4list_search_exact(iter4
->addr
,
509 #if IS_ENABLED(CONFIG_IPV6)
510 netlbl_af6list_foreach_rcu(iter6
, &entry
->def
.addrsel
->list6
)
511 if (netlbl_af6list_search_exact(&iter6
->addr
,
519 netlbl_af4list_foreach_safe(iter4
, tmp4
,
520 &entry
->def
.addrsel
->list4
) {
521 netlbl_af4list_remove_entry(iter4
);
523 ret_val
= netlbl_af4list_add(iter4
, old_list4
);
524 netlbl_domhsh_audit_add(entry_old
, iter4
, NULL
,
525 ret_val
, audit_info
);
529 #if IS_ENABLED(CONFIG_IPV6)
530 netlbl_af6list_foreach_safe(iter6
, tmp6
,
531 &entry
->def
.addrsel
->list6
) {
532 netlbl_af6list_remove_entry(iter6
);
534 ret_val
= netlbl_af6list_add(iter6
, old_list6
);
535 netlbl_domhsh_audit_add(entry_old
, NULL
, iter6
,
536 ret_val
, audit_info
);
541 /* cleanup the new entry since we've moved everything over */
542 netlbl_domhsh_free_entry(&entry
->rcu
);
547 spin_unlock(&netlbl_domhsh_lock
);
553 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
554 * @entry: the entry to add
555 * @audit_info: NetLabel audit information
558 * Adds a new default entry to the domain hash table and handles any updates
559 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
560 * negative on failure.
563 int netlbl_domhsh_add_default(struct netlbl_dom_map
*entry
,
564 struct netlbl_audit
*audit_info
)
566 return netlbl_domhsh_add(entry
, audit_info
);
570 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
571 * @entry: the entry to remove
572 * @audit_info: NetLabel audit information
575 * Removes an entry from the domain hash table and handles any updates to the
576 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
577 * ensuring that the RCU read lock is held. Returns zero on success, negative
581 int netlbl_domhsh_remove_entry(struct netlbl_dom_map
*entry
,
582 struct netlbl_audit
*audit_info
)
585 struct audit_buffer
*audit_buf
;
586 struct netlbl_af4list
*iter4
;
587 struct netlbl_domaddr4_map
*map4
;
588 #if IS_ENABLED(CONFIG_IPV6)
589 struct netlbl_af6list
*iter6
;
590 struct netlbl_domaddr6_map
*map6
;
596 spin_lock(&netlbl_domhsh_lock
);
599 if (entry
== rcu_dereference(netlbl_domhsh_def_ipv4
))
600 RCU_INIT_POINTER(netlbl_domhsh_def_ipv4
, NULL
);
601 else if (entry
== rcu_dereference(netlbl_domhsh_def_ipv6
))
602 RCU_INIT_POINTER(netlbl_domhsh_def_ipv6
, NULL
);
604 list_del_rcu(&entry
->list
);
607 spin_unlock(&netlbl_domhsh_lock
);
612 audit_buf
= netlbl_audit_start_common(AUDIT_MAC_MAP_DEL
, audit_info
);
613 if (audit_buf
!= NULL
) {
614 audit_log_format(audit_buf
,
615 " nlbl_domain=%s res=1",
616 entry
->domain
? entry
->domain
: "(default)");
617 audit_log_end(audit_buf
);
620 switch (entry
->def
.type
) {
621 case NETLBL_NLTYPE_ADDRSELECT
:
622 netlbl_af4list_foreach_rcu(iter4
, &entry
->def
.addrsel
->list4
) {
623 map4
= netlbl_domhsh_addr4_entry(iter4
);
624 cipso_v4_doi_putdef(map4
->def
.cipso
);
626 #if IS_ENABLED(CONFIG_IPV6)
627 netlbl_af6list_foreach_rcu(iter6
, &entry
->def
.addrsel
->list6
) {
628 map6
= netlbl_domhsh_addr6_entry(iter6
);
629 calipso_doi_putdef(map6
->def
.calipso
);
633 case NETLBL_NLTYPE_CIPSOV4
:
634 cipso_v4_doi_putdef(entry
->def
.cipso
);
636 #if IS_ENABLED(CONFIG_IPV6)
637 case NETLBL_NLTYPE_CALIPSO
:
638 calipso_doi_putdef(entry
->def
.calipso
);
642 call_rcu(&entry
->rcu
, netlbl_domhsh_free_entry
);
648 * netlbl_domhsh_remove_af4 - Removes an address selector entry
649 * @domain: the domain
650 * @addr: IPv4 address
651 * @mask: IPv4 address mask
652 * @audit_info: NetLabel audit information
655 * Removes an individual address selector from a domain mapping and potentially
656 * the entire mapping if it is empty. Returns zero on success, negative values
660 int netlbl_domhsh_remove_af4(const char *domain
,
661 const struct in_addr
*addr
,
662 const struct in_addr
*mask
,
663 struct netlbl_audit
*audit_info
)
665 struct netlbl_dom_map
*entry_map
;
666 struct netlbl_af4list
*entry_addr
;
667 struct netlbl_af4list
*iter4
;
668 #if IS_ENABLED(CONFIG_IPV6)
669 struct netlbl_af6list
*iter6
;
671 struct netlbl_domaddr4_map
*entry
;
676 entry_map
= netlbl_domhsh_search(domain
, AF_INET
);
678 entry_map
= netlbl_domhsh_search_def(domain
, AF_INET
);
679 if (entry_map
== NULL
||
680 entry_map
->def
.type
!= NETLBL_NLTYPE_ADDRSELECT
)
681 goto remove_af4_failure
;
683 spin_lock(&netlbl_domhsh_lock
);
684 entry_addr
= netlbl_af4list_remove(addr
->s_addr
, mask
->s_addr
,
685 &entry_map
->def
.addrsel
->list4
);
686 spin_unlock(&netlbl_domhsh_lock
);
688 if (entry_addr
== NULL
)
689 goto remove_af4_failure
;
690 netlbl_af4list_foreach_rcu(iter4
, &entry_map
->def
.addrsel
->list4
)
691 goto remove_af4_single_addr
;
692 #if IS_ENABLED(CONFIG_IPV6)
693 netlbl_af6list_foreach_rcu(iter6
, &entry_map
->def
.addrsel
->list6
)
694 goto remove_af4_single_addr
;
696 /* the domain mapping is empty so remove it from the mapping table */
697 netlbl_domhsh_remove_entry(entry_map
, audit_info
);
699 remove_af4_single_addr
:
701 /* yick, we can't use call_rcu here because we don't have a rcu head
702 * pointer but hopefully this should be a rare case so the pause
703 * shouldn't be a problem */
705 entry
= netlbl_domhsh_addr4_entry(entry_addr
);
706 cipso_v4_doi_putdef(entry
->def
.cipso
);
715 #if IS_ENABLED(CONFIG_IPV6)
717 * netlbl_domhsh_remove_af6 - Removes an address selector entry
718 * @domain: the domain
719 * @addr: IPv6 address
720 * @mask: IPv6 address mask
721 * @audit_info: NetLabel audit information
724 * Removes an individual address selector from a domain mapping and potentially
725 * the entire mapping if it is empty. Returns zero on success, negative values
729 int netlbl_domhsh_remove_af6(const char *domain
,
730 const struct in6_addr
*addr
,
731 const struct in6_addr
*mask
,
732 struct netlbl_audit
*audit_info
)
734 struct netlbl_dom_map
*entry_map
;
735 struct netlbl_af6list
*entry_addr
;
736 struct netlbl_af4list
*iter4
;
737 struct netlbl_af6list
*iter6
;
738 struct netlbl_domaddr6_map
*entry
;
743 entry_map
= netlbl_domhsh_search(domain
, AF_INET6
);
745 entry_map
= netlbl_domhsh_search_def(domain
, AF_INET6
);
746 if (entry_map
== NULL
||
747 entry_map
->def
.type
!= NETLBL_NLTYPE_ADDRSELECT
)
748 goto remove_af6_failure
;
750 spin_lock(&netlbl_domhsh_lock
);
751 entry_addr
= netlbl_af6list_remove(addr
, mask
,
752 &entry_map
->def
.addrsel
->list6
);
753 spin_unlock(&netlbl_domhsh_lock
);
755 if (entry_addr
== NULL
)
756 goto remove_af6_failure
;
757 netlbl_af4list_foreach_rcu(iter4
, &entry_map
->def
.addrsel
->list4
)
758 goto remove_af6_single_addr
;
759 netlbl_af6list_foreach_rcu(iter6
, &entry_map
->def
.addrsel
->list6
)
760 goto remove_af6_single_addr
;
761 /* the domain mapping is empty so remove it from the mapping table */
762 netlbl_domhsh_remove_entry(entry_map
, audit_info
);
764 remove_af6_single_addr
:
766 /* yick, we can't use call_rcu here because we don't have a rcu head
767 * pointer but hopefully this should be a rare case so the pause
768 * shouldn't be a problem */
770 entry
= netlbl_domhsh_addr6_entry(entry_addr
);
771 calipso_doi_putdef(entry
->def
.calipso
);
782 * netlbl_domhsh_remove - Removes an entry from the domain hash table
783 * @domain: the domain to remove
784 * @family: address family
785 * @audit_info: NetLabel audit information
788 * Removes an entry from the domain hash table and handles any updates to the
789 * lower level protocol handler (i.e. CIPSO). @family may be %AF_UNSPEC which
790 * removes all address family entries. Returns zero on success, negative on
794 int netlbl_domhsh_remove(const char *domain
, u16 family
,
795 struct netlbl_audit
*audit_info
)
797 int ret_val
= -EINVAL
;
798 struct netlbl_dom_map
*entry
;
802 if (family
== AF_INET
|| family
== AF_UNSPEC
) {
804 entry
= netlbl_domhsh_search(domain
, AF_INET
);
806 entry
= netlbl_domhsh_search_def(domain
, AF_INET
);
807 ret_val
= netlbl_domhsh_remove_entry(entry
, audit_info
);
808 if (ret_val
&& ret_val
!= -ENOENT
)
811 if (family
== AF_INET6
|| family
== AF_UNSPEC
) {
815 entry
= netlbl_domhsh_search(domain
, AF_INET6
);
817 entry
= netlbl_domhsh_search_def(domain
, AF_INET6
);
818 ret_val2
= netlbl_domhsh_remove_entry(entry
, audit_info
);
819 if (ret_val2
!= -ENOENT
)
829 * netlbl_domhsh_remove_default - Removes the default entry from the table
830 * @family: address family
831 * @audit_info: NetLabel audit information
834 * Removes/resets the default entry corresponding to @family from the domain
835 * hash table and handles any updates to the lower level protocol handler
836 * (i.e. CIPSO). @family may be %AF_UNSPEC which removes all address family
837 * entries. Returns zero on success, negative on failure.
840 int netlbl_domhsh_remove_default(u16 family
, struct netlbl_audit
*audit_info
)
842 return netlbl_domhsh_remove(NULL
, family
, audit_info
);
846 * netlbl_domhsh_getentry - Get an entry from the domain hash table
847 * @domain: the domain name to search for
848 * @family: address family
851 * Look through the domain hash table searching for an entry to match @domain,
852 * with address family @family, return a pointer to a copy of the entry or
853 * NULL. The caller is responsible for ensuring that rcu_read_[un]lock() is
857 struct netlbl_dom_map
*netlbl_domhsh_getentry(const char *domain
, u16 family
)
859 if (family
== AF_UNSPEC
)
861 return netlbl_domhsh_search_def(domain
, family
);
865 * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
866 * @domain: the domain name to search for
867 * @addr: the IP address to search for
870 * Look through the domain hash table searching for an entry to match @domain
871 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
872 * responsible for ensuring that rcu_read_[un]lock() is called.
875 struct netlbl_dommap_def
*netlbl_domhsh_getentry_af4(const char *domain
,
878 struct netlbl_dom_map
*dom_iter
;
879 struct netlbl_af4list
*addr_iter
;
881 dom_iter
= netlbl_domhsh_search_def(domain
, AF_INET
);
882 if (dom_iter
== NULL
)
885 if (dom_iter
->def
.type
!= NETLBL_NLTYPE_ADDRSELECT
)
886 return &dom_iter
->def
;
887 addr_iter
= netlbl_af4list_search(addr
, &dom_iter
->def
.addrsel
->list4
);
888 if (addr_iter
== NULL
)
890 return &(netlbl_domhsh_addr4_entry(addr_iter
)->def
);
893 #if IS_ENABLED(CONFIG_IPV6)
895 * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
896 * @domain: the domain name to search for
897 * @addr: the IP address to search for
900 * Look through the domain hash table searching for an entry to match @domain
901 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
902 * responsible for ensuring that rcu_read_[un]lock() is called.
905 struct netlbl_dommap_def
*netlbl_domhsh_getentry_af6(const char *domain
,
906 const struct in6_addr
*addr
)
908 struct netlbl_dom_map
*dom_iter
;
909 struct netlbl_af6list
*addr_iter
;
911 dom_iter
= netlbl_domhsh_search_def(domain
, AF_INET6
);
912 if (dom_iter
== NULL
)
915 if (dom_iter
->def
.type
!= NETLBL_NLTYPE_ADDRSELECT
)
916 return &dom_iter
->def
;
917 addr_iter
= netlbl_af6list_search(addr
, &dom_iter
->def
.addrsel
->list6
);
918 if (addr_iter
== NULL
)
920 return &(netlbl_domhsh_addr6_entry(addr_iter
)->def
);
925 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
926 * @skip_bkt: the number of buckets to skip at the start
927 * @skip_chain: the number of entries to skip in the first iterated bucket
928 * @callback: callback for each entry
929 * @cb_arg: argument for the callback function
932 * Interate over the domain mapping hash table, skipping the first @skip_bkt
933 * buckets and @skip_chain entries. For each entry in the table call
934 * @callback, if @callback returns a negative value stop 'walking' through the
935 * table and return. Updates the values in @skip_bkt and @skip_chain on
936 * return. Returns zero on success, negative values on failure.
939 int netlbl_domhsh_walk(u32
*skip_bkt
,
941 int (*callback
) (struct netlbl_dom_map
*entry
, void *arg
),
944 int ret_val
= -ENOENT
;
946 struct list_head
*iter_list
;
947 struct netlbl_dom_map
*iter_entry
;
951 for (iter_bkt
= *skip_bkt
;
952 iter_bkt
< rcu_dereference(netlbl_domhsh
)->size
;
953 iter_bkt
++, chain_cnt
= 0) {
954 iter_list
= &rcu_dereference(netlbl_domhsh
)->tbl
[iter_bkt
];
955 list_for_each_entry_rcu(iter_entry
, iter_list
, list
)
956 if (iter_entry
->valid
) {
957 if (chain_cnt
++ < *skip_chain
)
959 ret_val
= callback(iter_entry
, cb_arg
);
969 *skip_bkt
= iter_bkt
;
970 *skip_chain
= chain_cnt
;