Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * NetLabel Domain Hash Table
0004  *
0005  * This file manages the domain hash table that NetLabel uses to determine
0006  * which network labeling protocol to use for a given domain.  The NetLabel
0007  * system manages static and dynamic label mappings for network protocols such
0008  * as CIPSO and RIPSO.
0009  *
0010  * Author: Paul Moore <paul@paul-moore.com>
0011  */
0012 
0013 /*
0014  * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
0015  */
0016 
0017 #include <linux/types.h>
0018 #include <linux/rculist.h>
0019 #include <linux/skbuff.h>
0020 #include <linux/spinlock.h>
0021 #include <linux/string.h>
0022 #include <linux/audit.h>
0023 #include <linux/slab.h>
0024 #include <net/netlabel.h>
0025 #include <net/cipso_ipv4.h>
0026 #include <net/calipso.h>
0027 #include <asm/bug.h>
0028 
0029 #include "netlabel_mgmt.h"
0030 #include "netlabel_addrlist.h"
0031 #include "netlabel_calipso.h"
0032 #include "netlabel_domainhash.h"
0033 #include "netlabel_user.h"
0034 
0035 struct netlbl_domhsh_tbl {
0036     struct list_head *tbl;
0037     u32 size;
0038 };
0039 
0040 /* Domain hash table */
0041 /* updates should be so rare that having one spinlock for the entire hash table
0042  * should be okay */
0043 static DEFINE_SPINLOCK(netlbl_domhsh_lock);
0044 #define netlbl_domhsh_rcu_deref(p) \
0045     rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
0046 static struct netlbl_domhsh_tbl __rcu *netlbl_domhsh;
0047 static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv4;
0048 static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv6;
0049 
0050 /*
0051  * Domain Hash Table Helper Functions
0052  */
0053 
0054 /**
0055  * netlbl_domhsh_free_entry - Frees a domain hash table entry
0056  * @entry: the entry's RCU field
0057  *
0058  * Description:
0059  * This function is designed to be used as a callback to the call_rcu()
0060  * function so that the memory allocated to a hash table entry can be released
0061  * safely.
0062  *
0063  */
0064 static void netlbl_domhsh_free_entry(struct rcu_head *entry)
0065 {
0066     struct netlbl_dom_map *ptr;
0067     struct netlbl_af4list *iter4;
0068     struct netlbl_af4list *tmp4;
0069 #if IS_ENABLED(CONFIG_IPV6)
0070     struct netlbl_af6list *iter6;
0071     struct netlbl_af6list *tmp6;
0072 #endif /* IPv6 */
0073 
0074     ptr = container_of(entry, struct netlbl_dom_map, rcu);
0075     if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
0076         netlbl_af4list_foreach_safe(iter4, tmp4,
0077                         &ptr->def.addrsel->list4) {
0078             netlbl_af4list_remove_entry(iter4);
0079             kfree(netlbl_domhsh_addr4_entry(iter4));
0080         }
0081 #if IS_ENABLED(CONFIG_IPV6)
0082         netlbl_af6list_foreach_safe(iter6, tmp6,
0083                         &ptr->def.addrsel->list6) {
0084             netlbl_af6list_remove_entry(iter6);
0085             kfree(netlbl_domhsh_addr6_entry(iter6));
0086         }
0087 #endif /* IPv6 */
0088         kfree(ptr->def.addrsel);
0089     }
0090     kfree(ptr->domain);
0091     kfree(ptr);
0092 }
0093 
0094 /**
0095  * netlbl_domhsh_hash - Hashing function for the domain hash table
0096  * @key: the domain name to hash
0097  *
0098  * Description:
0099  * This is the hashing function for the domain hash table, it returns the
0100  * correct bucket number for the domain.  The caller is responsible for
0101  * ensuring that the hash table is protected with either a RCU read lock or the
0102  * hash table lock.
0103  *
0104  */
0105 static u32 netlbl_domhsh_hash(const char *key)
0106 {
0107     u32 iter;
0108     u32 val;
0109     u32 len;
0110 
0111     /* This is taken (with slight modification) from
0112      * security/selinux/ss/symtab.c:symhash() */
0113 
0114     for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
0115         val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
0116     return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
0117 }
0118 
0119 static bool netlbl_family_match(u16 f1, u16 f2)
0120 {
0121     return (f1 == f2) || (f1 == AF_UNSPEC) || (f2 == AF_UNSPEC);
0122 }
0123 
0124 /**
0125  * netlbl_domhsh_search - Search for a domain entry
0126  * @domain: the domain
0127  * @family: the address family
0128  *
0129  * Description:
0130  * Searches the domain hash table and returns a pointer to the hash table
0131  * entry if found, otherwise NULL is returned.  @family may be %AF_UNSPEC
0132  * which matches any address family entries.  The caller is responsible for
0133  * ensuring that the hash table is protected with either a RCU read lock or the
0134  * hash table lock.
0135  *
0136  */
0137 static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain,
0138                            u16 family)
0139 {
0140     u32 bkt;
0141     struct list_head *bkt_list;
0142     struct netlbl_dom_map *iter;
0143 
0144     if (domain != NULL) {
0145         bkt = netlbl_domhsh_hash(domain);
0146         bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
0147         list_for_each_entry_rcu(iter, bkt_list, list,
0148                     lockdep_is_held(&netlbl_domhsh_lock))
0149             if (iter->valid &&
0150                 netlbl_family_match(iter->family, family) &&
0151                 strcmp(iter->domain, domain) == 0)
0152                 return iter;
0153     }
0154 
0155     return NULL;
0156 }
0157 
0158 /**
0159  * netlbl_domhsh_search_def - Search for a domain entry
0160  * @domain: the domain
0161  * @family: the address family
0162  *
0163  * Description:
0164  * Searches the domain hash table and returns a pointer to the hash table
0165  * entry if an exact match is found, if an exact match is not present in the
0166  * hash table then the default entry is returned if valid otherwise NULL is
0167  * returned.  @family may be %AF_UNSPEC which matches any address family
0168  * entries.  The caller is responsible ensuring that the hash table is
0169  * protected with either a RCU read lock or the hash table lock.
0170  *
0171  */
0172 static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain,
0173                                u16 family)
0174 {
0175     struct netlbl_dom_map *entry;
0176 
0177     entry = netlbl_domhsh_search(domain, family);
0178     if (entry != NULL)
0179         return entry;
0180     if (family == AF_INET || family == AF_UNSPEC) {
0181         entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4);
0182         if (entry != NULL && entry->valid)
0183             return entry;
0184     }
0185     if (family == AF_INET6 || family == AF_UNSPEC) {
0186         entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6);
0187         if (entry != NULL && entry->valid)
0188             return entry;
0189     }
0190 
0191     return NULL;
0192 }
0193 
0194 /**
0195  * netlbl_domhsh_audit_add - Generate an audit entry for an add event
0196  * @entry: the entry being added
0197  * @addr4: the IPv4 address information
0198  * @addr6: the IPv6 address information
0199  * @result: the result code
0200  * @audit_info: NetLabel audit information
0201  *
0202  * Description:
0203  * Generate an audit record for adding a new NetLabel/LSM mapping entry with
0204  * the given information.  Caller is responsible for holding the necessary
0205  * locks.
0206  *
0207  */
0208 static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
0209                     struct netlbl_af4list *addr4,
0210                     struct netlbl_af6list *addr6,
0211                     int result,
0212                     struct netlbl_audit *audit_info)
0213 {
0214     struct audit_buffer *audit_buf;
0215     struct cipso_v4_doi *cipsov4 = NULL;
0216     struct calipso_doi *calipso = NULL;
0217     u32 type;
0218 
0219     audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
0220     if (audit_buf != NULL) {
0221         audit_log_format(audit_buf, " nlbl_domain=%s",
0222                  entry->domain ? entry->domain : "(default)");
0223         if (addr4 != NULL) {
0224             struct netlbl_domaddr4_map *map4;
0225             map4 = netlbl_domhsh_addr4_entry(addr4);
0226             type = map4->def.type;
0227             cipsov4 = map4->def.cipso;
0228             netlbl_af4list_audit_addr(audit_buf, 0, NULL,
0229                           addr4->addr, addr4->mask);
0230 #if IS_ENABLED(CONFIG_IPV6)
0231         } else if (addr6 != NULL) {
0232             struct netlbl_domaddr6_map *map6;
0233             map6 = netlbl_domhsh_addr6_entry(addr6);
0234             type = map6->def.type;
0235             calipso = map6->def.calipso;
0236             netlbl_af6list_audit_addr(audit_buf, 0, NULL,
0237                           &addr6->addr, &addr6->mask);
0238 #endif /* IPv6 */
0239         } else {
0240             type = entry->def.type;
0241             cipsov4 = entry->def.cipso;
0242             calipso = entry->def.calipso;
0243         }
0244         switch (type) {
0245         case NETLBL_NLTYPE_UNLABELED:
0246             audit_log_format(audit_buf, " nlbl_protocol=unlbl");
0247             break;
0248         case NETLBL_NLTYPE_CIPSOV4:
0249             BUG_ON(cipsov4 == NULL);
0250             audit_log_format(audit_buf,
0251                      " nlbl_protocol=cipsov4 cipso_doi=%u",
0252                      cipsov4->doi);
0253             break;
0254         case NETLBL_NLTYPE_CALIPSO:
0255             BUG_ON(calipso == NULL);
0256             audit_log_format(audit_buf,
0257                      " nlbl_protocol=calipso calipso_doi=%u",
0258                      calipso->doi);
0259             break;
0260         }
0261         audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
0262         audit_log_end(audit_buf);
0263     }
0264 }
0265 
0266 /**
0267  * netlbl_domhsh_validate - Validate a new domain mapping entry
0268  * @entry: the entry to validate
0269  *
0270  * This function validates the new domain mapping entry to ensure that it is
0271  * a valid entry.  Returns zero on success, negative values on failure.
0272  *
0273  */
0274 static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
0275 {
0276     struct netlbl_af4list *iter4;
0277     struct netlbl_domaddr4_map *map4;
0278 #if IS_ENABLED(CONFIG_IPV6)
0279     struct netlbl_af6list *iter6;
0280     struct netlbl_domaddr6_map *map6;
0281 #endif /* IPv6 */
0282 
0283     if (entry == NULL)
0284         return -EINVAL;
0285 
0286     if (entry->family != AF_INET && entry->family != AF_INET6 &&
0287         (entry->family != AF_UNSPEC ||
0288          entry->def.type != NETLBL_NLTYPE_UNLABELED))
0289         return -EINVAL;
0290 
0291     switch (entry->def.type) {
0292     case NETLBL_NLTYPE_UNLABELED:
0293         if (entry->def.cipso != NULL || entry->def.calipso != NULL ||
0294             entry->def.addrsel != NULL)
0295             return -EINVAL;
0296         break;
0297     case NETLBL_NLTYPE_CIPSOV4:
0298         if (entry->family != AF_INET ||
0299             entry->def.cipso == NULL)
0300             return -EINVAL;
0301         break;
0302     case NETLBL_NLTYPE_CALIPSO:
0303         if (entry->family != AF_INET6 ||
0304             entry->def.calipso == NULL)
0305             return -EINVAL;
0306         break;
0307     case NETLBL_NLTYPE_ADDRSELECT:
0308         netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
0309             map4 = netlbl_domhsh_addr4_entry(iter4);
0310             switch (map4->def.type) {
0311             case NETLBL_NLTYPE_UNLABELED:
0312                 if (map4->def.cipso != NULL)
0313                     return -EINVAL;
0314                 break;
0315             case NETLBL_NLTYPE_CIPSOV4:
0316                 if (map4->def.cipso == NULL)
0317                     return -EINVAL;
0318                 break;
0319             default:
0320                 return -EINVAL;
0321             }
0322         }
0323 #if IS_ENABLED(CONFIG_IPV6)
0324         netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
0325             map6 = netlbl_domhsh_addr6_entry(iter6);
0326             switch (map6->def.type) {
0327             case NETLBL_NLTYPE_UNLABELED:
0328                 if (map6->def.calipso != NULL)
0329                     return -EINVAL;
0330                 break;
0331             case NETLBL_NLTYPE_CALIPSO:
0332                 if (map6->def.calipso == NULL)
0333                     return -EINVAL;
0334                 break;
0335             default:
0336                 return -EINVAL;
0337             }
0338         }
0339 #endif /* IPv6 */
0340         break;
0341     default:
0342         return -EINVAL;
0343     }
0344 
0345     return 0;
0346 }
0347 
0348 /*
0349  * Domain Hash Table Functions
0350  */
0351 
0352 /**
0353  * netlbl_domhsh_init - Init for the domain hash
0354  * @size: the number of bits to use for the hash buckets
0355  *
0356  * Description:
0357  * Initializes the domain hash table, should be called only by
0358  * netlbl_user_init() during initialization.  Returns zero on success, non-zero
0359  * values on error.
0360  *
0361  */
0362 int __init netlbl_domhsh_init(u32 size)
0363 {
0364     u32 iter;
0365     struct netlbl_domhsh_tbl *hsh_tbl;
0366 
0367     if (size == 0)
0368         return -EINVAL;
0369 
0370     hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
0371     if (hsh_tbl == NULL)
0372         return -ENOMEM;
0373     hsh_tbl->size = 1 << size;
0374     hsh_tbl->tbl = kcalloc(hsh_tbl->size,
0375                    sizeof(struct list_head),
0376                    GFP_KERNEL);
0377     if (hsh_tbl->tbl == NULL) {
0378         kfree(hsh_tbl);
0379         return -ENOMEM;
0380     }
0381     for (iter = 0; iter < hsh_tbl->size; iter++)
0382         INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
0383 
0384     spin_lock(&netlbl_domhsh_lock);
0385     rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
0386     spin_unlock(&netlbl_domhsh_lock);
0387 
0388     return 0;
0389 }
0390 
0391 /**
0392  * netlbl_domhsh_add - Adds a entry to the domain hash table
0393  * @entry: the entry to add
0394  * @audit_info: NetLabel audit information
0395  *
0396  * Description:
0397  * Adds a new entry to the domain hash table and handles any updates to the
0398  * lower level protocol handler (i.e. CIPSO).  @entry->family may be set to
0399  * %AF_UNSPEC which will add an entry that matches all address families.  This
0400  * is only useful for the unlabelled type and will only succeed if there is no
0401  * existing entry for any address family with the same domain.  Returns zero
0402  * on success, negative on failure.
0403  *
0404  */
0405 int netlbl_domhsh_add(struct netlbl_dom_map *entry,
0406               struct netlbl_audit *audit_info)
0407 {
0408     int ret_val = 0;
0409     struct netlbl_dom_map *entry_old, *entry_b;
0410     struct netlbl_af4list *iter4;
0411     struct netlbl_af4list *tmp4;
0412 #if IS_ENABLED(CONFIG_IPV6)
0413     struct netlbl_af6list *iter6;
0414     struct netlbl_af6list *tmp6;
0415 #endif /* IPv6 */
0416 
0417     ret_val = netlbl_domhsh_validate(entry);
0418     if (ret_val != 0)
0419         return ret_val;
0420 
0421     /* XXX - we can remove this RCU read lock as the spinlock protects the
0422      *       entire function, but before we do we need to fixup the
0423      *       netlbl_af[4,6]list RCU functions to do "the right thing" with
0424      *       respect to rcu_dereference() when only a spinlock is held. */
0425     rcu_read_lock();
0426     spin_lock(&netlbl_domhsh_lock);
0427     if (entry->domain != NULL)
0428         entry_old = netlbl_domhsh_search(entry->domain, entry->family);
0429     else
0430         entry_old = netlbl_domhsh_search_def(entry->domain,
0431                              entry->family);
0432     if (entry_old == NULL) {
0433         entry->valid = 1;
0434 
0435         if (entry->domain != NULL) {
0436             u32 bkt = netlbl_domhsh_hash(entry->domain);
0437             list_add_tail_rcu(&entry->list,
0438                     &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
0439         } else {
0440             INIT_LIST_HEAD(&entry->list);
0441             switch (entry->family) {
0442             case AF_INET:
0443                 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
0444                            entry);
0445                 break;
0446             case AF_INET6:
0447                 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
0448                            entry);
0449                 break;
0450             case AF_UNSPEC:
0451                 if (entry->def.type !=
0452                     NETLBL_NLTYPE_UNLABELED) {
0453                     ret_val = -EINVAL;
0454                     goto add_return;
0455                 }
0456                 entry_b = kzalloc(sizeof(*entry_b), GFP_ATOMIC);
0457                 if (entry_b == NULL) {
0458                     ret_val = -ENOMEM;
0459                     goto add_return;
0460                 }
0461                 entry_b->family = AF_INET6;
0462                 entry_b->def.type = NETLBL_NLTYPE_UNLABELED;
0463                 entry_b->valid = 1;
0464                 entry->family = AF_INET;
0465                 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
0466                            entry);
0467                 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
0468                            entry_b);
0469                 break;
0470             default:
0471                 /* Already checked in
0472                  * netlbl_domhsh_validate(). */
0473                 ret_val = -EINVAL;
0474                 goto add_return;
0475             }
0476         }
0477 
0478         if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
0479             netlbl_af4list_foreach_rcu(iter4,
0480                            &entry->def.addrsel->list4)
0481                 netlbl_domhsh_audit_add(entry, iter4, NULL,
0482                             ret_val, audit_info);
0483 #if IS_ENABLED(CONFIG_IPV6)
0484             netlbl_af6list_foreach_rcu(iter6,
0485                            &entry->def.addrsel->list6)
0486                 netlbl_domhsh_audit_add(entry, NULL, iter6,
0487                             ret_val, audit_info);
0488 #endif /* IPv6 */
0489         } else
0490             netlbl_domhsh_audit_add(entry, NULL, NULL,
0491                         ret_val, audit_info);
0492     } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
0493            entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
0494         struct list_head *old_list4;
0495         struct list_head *old_list6;
0496 
0497         old_list4 = &entry_old->def.addrsel->list4;
0498         old_list6 = &entry_old->def.addrsel->list6;
0499 
0500         /* we only allow the addition of address selectors if all of
0501          * the selectors do not exist in the existing domain map */
0502         netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
0503             if (netlbl_af4list_search_exact(iter4->addr,
0504                             iter4->mask,
0505                             old_list4)) {
0506                 ret_val = -EEXIST;
0507                 goto add_return;
0508             }
0509 #if IS_ENABLED(CONFIG_IPV6)
0510         netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
0511             if (netlbl_af6list_search_exact(&iter6->addr,
0512                             &iter6->mask,
0513                             old_list6)) {
0514                 ret_val = -EEXIST;
0515                 goto add_return;
0516             }
0517 #endif /* IPv6 */
0518 
0519         netlbl_af4list_foreach_safe(iter4, tmp4,
0520                         &entry->def.addrsel->list4) {
0521             netlbl_af4list_remove_entry(iter4);
0522             iter4->valid = 1;
0523             ret_val = netlbl_af4list_add(iter4, old_list4);
0524             netlbl_domhsh_audit_add(entry_old, iter4, NULL,
0525                         ret_val, audit_info);
0526             if (ret_val != 0)
0527                 goto add_return;
0528         }
0529 #if IS_ENABLED(CONFIG_IPV6)
0530         netlbl_af6list_foreach_safe(iter6, tmp6,
0531                         &entry->def.addrsel->list6) {
0532             netlbl_af6list_remove_entry(iter6);
0533             iter6->valid = 1;
0534             ret_val = netlbl_af6list_add(iter6, old_list6);
0535             netlbl_domhsh_audit_add(entry_old, NULL, iter6,
0536                         ret_val, audit_info);
0537             if (ret_val != 0)
0538                 goto add_return;
0539         }
0540 #endif /* IPv6 */
0541         /* cleanup the new entry since we've moved everything over */
0542         netlbl_domhsh_free_entry(&entry->rcu);
0543     } else
0544         ret_val = -EINVAL;
0545 
0546 add_return:
0547     spin_unlock(&netlbl_domhsh_lock);
0548     rcu_read_unlock();
0549     return ret_val;
0550 }
0551 
0552 /**
0553  * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
0554  * @entry: the entry to add
0555  * @audit_info: NetLabel audit information
0556  *
0557  * Description:
0558  * Adds a new default entry to the domain hash table and handles any updates
0559  * to the lower level protocol handler (i.e. CIPSO).  Returns zero on success,
0560  * negative on failure.
0561  *
0562  */
0563 int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
0564                   struct netlbl_audit *audit_info)
0565 {
0566     return netlbl_domhsh_add(entry, audit_info);
0567 }
0568 
0569 /**
0570  * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
0571  * @entry: the entry to remove
0572  * @audit_info: NetLabel audit information
0573  *
0574  * Description:
0575  * Removes an entry from the domain hash table and handles any updates to the
0576  * lower level protocol handler (i.e. CIPSO).  Caller is responsible for
0577  * ensuring that the RCU read lock is held.  Returns zero on success, negative
0578  * on failure.
0579  *
0580  */
0581 int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
0582                    struct netlbl_audit *audit_info)
0583 {
0584     int ret_val = 0;
0585     struct audit_buffer *audit_buf;
0586     struct netlbl_af4list *iter4;
0587     struct netlbl_domaddr4_map *map4;
0588 #if IS_ENABLED(CONFIG_IPV6)
0589     struct netlbl_af6list *iter6;
0590     struct netlbl_domaddr6_map *map6;
0591 #endif /* IPv6 */
0592 
0593     if (entry == NULL)
0594         return -ENOENT;
0595 
0596     spin_lock(&netlbl_domhsh_lock);
0597     if (entry->valid) {
0598         entry->valid = 0;
0599         if (entry == rcu_dereference(netlbl_domhsh_def_ipv4))
0600             RCU_INIT_POINTER(netlbl_domhsh_def_ipv4, NULL);
0601         else if (entry == rcu_dereference(netlbl_domhsh_def_ipv6))
0602             RCU_INIT_POINTER(netlbl_domhsh_def_ipv6, NULL);
0603         else
0604             list_del_rcu(&entry->list);
0605     } else
0606         ret_val = -ENOENT;
0607     spin_unlock(&netlbl_domhsh_lock);
0608 
0609     if (ret_val)
0610         return ret_val;
0611 
0612     audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
0613     if (audit_buf != NULL) {
0614         audit_log_format(audit_buf,
0615                  " nlbl_domain=%s res=1",
0616                  entry->domain ? entry->domain : "(default)");
0617         audit_log_end(audit_buf);
0618     }
0619 
0620     switch (entry->def.type) {
0621     case NETLBL_NLTYPE_ADDRSELECT:
0622         netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
0623             map4 = netlbl_domhsh_addr4_entry(iter4);
0624             cipso_v4_doi_putdef(map4->def.cipso);
0625         }
0626 #if IS_ENABLED(CONFIG_IPV6)
0627         netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
0628             map6 = netlbl_domhsh_addr6_entry(iter6);
0629             calipso_doi_putdef(map6->def.calipso);
0630         }
0631 #endif /* IPv6 */
0632         break;
0633     case NETLBL_NLTYPE_CIPSOV4:
0634         cipso_v4_doi_putdef(entry->def.cipso);
0635         break;
0636 #if IS_ENABLED(CONFIG_IPV6)
0637     case NETLBL_NLTYPE_CALIPSO:
0638         calipso_doi_putdef(entry->def.calipso);
0639         break;
0640 #endif /* IPv6 */
0641     }
0642     call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
0643 
0644     return ret_val;
0645 }
0646 
0647 /**
0648  * netlbl_domhsh_remove_af4 - Removes an address selector entry
0649  * @domain: the domain
0650  * @addr: IPv4 address
0651  * @mask: IPv4 address mask
0652  * @audit_info: NetLabel audit information
0653  *
0654  * Description:
0655  * Removes an individual address selector from a domain mapping and potentially
0656  * the entire mapping if it is empty.  Returns zero on success, negative values
0657  * on failure.
0658  *
0659  */
0660 int netlbl_domhsh_remove_af4(const char *domain,
0661                  const struct in_addr *addr,
0662                  const struct in_addr *mask,
0663                  struct netlbl_audit *audit_info)
0664 {
0665     struct netlbl_dom_map *entry_map;
0666     struct netlbl_af4list *entry_addr;
0667     struct netlbl_af4list *iter4;
0668 #if IS_ENABLED(CONFIG_IPV6)
0669     struct netlbl_af6list *iter6;
0670 #endif /* IPv6 */
0671     struct netlbl_domaddr4_map *entry;
0672 
0673     rcu_read_lock();
0674 
0675     if (domain)
0676         entry_map = netlbl_domhsh_search(domain, AF_INET);
0677     else
0678         entry_map = netlbl_domhsh_search_def(domain, AF_INET);
0679     if (entry_map == NULL ||
0680         entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
0681         goto remove_af4_failure;
0682 
0683     spin_lock(&netlbl_domhsh_lock);
0684     entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
0685                        &entry_map->def.addrsel->list4);
0686     spin_unlock(&netlbl_domhsh_lock);
0687 
0688     if (entry_addr == NULL)
0689         goto remove_af4_failure;
0690     netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
0691         goto remove_af4_single_addr;
0692 #if IS_ENABLED(CONFIG_IPV6)
0693     netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
0694         goto remove_af4_single_addr;
0695 #endif /* IPv6 */
0696     /* the domain mapping is empty so remove it from the mapping table */
0697     netlbl_domhsh_remove_entry(entry_map, audit_info);
0698 
0699 remove_af4_single_addr:
0700     rcu_read_unlock();
0701     /* yick, we can't use call_rcu here because we don't have a rcu head
0702      * pointer but hopefully this should be a rare case so the pause
0703      * shouldn't be a problem */
0704     synchronize_rcu();
0705     entry = netlbl_domhsh_addr4_entry(entry_addr);
0706     cipso_v4_doi_putdef(entry->def.cipso);
0707     kfree(entry);
0708     return 0;
0709 
0710 remove_af4_failure:
0711     rcu_read_unlock();
0712     return -ENOENT;
0713 }
0714 
0715 #if IS_ENABLED(CONFIG_IPV6)
0716 /**
0717  * netlbl_domhsh_remove_af6 - Removes an address selector entry
0718  * @domain: the domain
0719  * @addr: IPv6 address
0720  * @mask: IPv6 address mask
0721  * @audit_info: NetLabel audit information
0722  *
0723  * Description:
0724  * Removes an individual address selector from a domain mapping and potentially
0725  * the entire mapping if it is empty.  Returns zero on success, negative values
0726  * on failure.
0727  *
0728  */
0729 int netlbl_domhsh_remove_af6(const char *domain,
0730                  const struct in6_addr *addr,
0731                  const struct in6_addr *mask,
0732                  struct netlbl_audit *audit_info)
0733 {
0734     struct netlbl_dom_map *entry_map;
0735     struct netlbl_af6list *entry_addr;
0736     struct netlbl_af4list *iter4;
0737     struct netlbl_af6list *iter6;
0738     struct netlbl_domaddr6_map *entry;
0739 
0740     rcu_read_lock();
0741 
0742     if (domain)
0743         entry_map = netlbl_domhsh_search(domain, AF_INET6);
0744     else
0745         entry_map = netlbl_domhsh_search_def(domain, AF_INET6);
0746     if (entry_map == NULL ||
0747         entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
0748         goto remove_af6_failure;
0749 
0750     spin_lock(&netlbl_domhsh_lock);
0751     entry_addr = netlbl_af6list_remove(addr, mask,
0752                        &entry_map->def.addrsel->list6);
0753     spin_unlock(&netlbl_domhsh_lock);
0754 
0755     if (entry_addr == NULL)
0756         goto remove_af6_failure;
0757     netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
0758         goto remove_af6_single_addr;
0759     netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
0760         goto remove_af6_single_addr;
0761     /* the domain mapping is empty so remove it from the mapping table */
0762     netlbl_domhsh_remove_entry(entry_map, audit_info);
0763 
0764 remove_af6_single_addr:
0765     rcu_read_unlock();
0766     /* yick, we can't use call_rcu here because we don't have a rcu head
0767      * pointer but hopefully this should be a rare case so the pause
0768      * shouldn't be a problem */
0769     synchronize_rcu();
0770     entry = netlbl_domhsh_addr6_entry(entry_addr);
0771     calipso_doi_putdef(entry->def.calipso);
0772     kfree(entry);
0773     return 0;
0774 
0775 remove_af6_failure:
0776     rcu_read_unlock();
0777     return -ENOENT;
0778 }
0779 #endif /* IPv6 */
0780 
0781 /**
0782  * netlbl_domhsh_remove - Removes an entry from the domain hash table
0783  * @domain: the domain to remove
0784  * @family: address family
0785  * @audit_info: NetLabel audit information
0786  *
0787  * Description:
0788  * Removes an entry from the domain hash table and handles any updates to the
0789  * lower level protocol handler (i.e. CIPSO).  @family may be %AF_UNSPEC which
0790  * removes all address family entries.  Returns zero on success, negative on
0791  * failure.
0792  *
0793  */
0794 int netlbl_domhsh_remove(const char *domain, u16 family,
0795              struct netlbl_audit *audit_info)
0796 {
0797     int ret_val = -EINVAL;
0798     struct netlbl_dom_map *entry;
0799 
0800     rcu_read_lock();
0801 
0802     if (family == AF_INET || family == AF_UNSPEC) {
0803         if (domain)
0804             entry = netlbl_domhsh_search(domain, AF_INET);
0805         else
0806             entry = netlbl_domhsh_search_def(domain, AF_INET);
0807         ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
0808         if (ret_val && ret_val != -ENOENT)
0809             goto done;
0810     }
0811     if (family == AF_INET6 || family == AF_UNSPEC) {
0812         int ret_val2;
0813 
0814         if (domain)
0815             entry = netlbl_domhsh_search(domain, AF_INET6);
0816         else
0817             entry = netlbl_domhsh_search_def(domain, AF_INET6);
0818         ret_val2 = netlbl_domhsh_remove_entry(entry, audit_info);
0819         if (ret_val2 != -ENOENT)
0820             ret_val = ret_val2;
0821     }
0822 done:
0823     rcu_read_unlock();
0824 
0825     return ret_val;
0826 }
0827 
0828 /**
0829  * netlbl_domhsh_remove_default - Removes the default entry from the table
0830  * @family: address family
0831  * @audit_info: NetLabel audit information
0832  *
0833  * Description:
0834  * Removes/resets the default entry corresponding to @family from the domain
0835  * hash table and handles any updates to the lower level protocol handler
0836  * (i.e. CIPSO).  @family may be %AF_UNSPEC which removes all address family
0837  * entries.  Returns zero on success, negative on failure.
0838  *
0839  */
0840 int netlbl_domhsh_remove_default(u16 family, struct netlbl_audit *audit_info)
0841 {
0842     return netlbl_domhsh_remove(NULL, family, audit_info);
0843 }
0844 
0845 /**
0846  * netlbl_domhsh_getentry - Get an entry from the domain hash table
0847  * @domain: the domain name to search for
0848  * @family: address family
0849  *
0850  * Description:
0851  * Look through the domain hash table searching for an entry to match @domain,
0852  * with address family @family, return a pointer to a copy of the entry or
0853  * NULL.  The caller is responsible for ensuring that rcu_read_[un]lock() is
0854  * called.
0855  *
0856  */
0857 struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain, u16 family)
0858 {
0859     if (family == AF_UNSPEC)
0860         return NULL;
0861     return netlbl_domhsh_search_def(domain, family);
0862 }
0863 
0864 /**
0865  * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
0866  * @domain: the domain name to search for
0867  * @addr: the IP address to search for
0868  *
0869  * Description:
0870  * Look through the domain hash table searching for an entry to match @domain
0871  * and @addr, return a pointer to a copy of the entry or NULL.  The caller is
0872  * responsible for ensuring that rcu_read_[un]lock() is called.
0873  *
0874  */
0875 struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
0876                              __be32 addr)
0877 {
0878     struct netlbl_dom_map *dom_iter;
0879     struct netlbl_af4list *addr_iter;
0880 
0881     dom_iter = netlbl_domhsh_search_def(domain, AF_INET);
0882     if (dom_iter == NULL)
0883         return NULL;
0884 
0885     if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
0886         return &dom_iter->def;
0887     addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
0888     if (addr_iter == NULL)
0889         return NULL;
0890     return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
0891 }
0892 
0893 #if IS_ENABLED(CONFIG_IPV6)
0894 /**
0895  * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
0896  * @domain: the domain name to search for
0897  * @addr: the IP address to search for
0898  *
0899  * Description:
0900  * Look through the domain hash table searching for an entry to match @domain
0901  * and @addr, return a pointer to a copy of the entry or NULL.  The caller is
0902  * responsible for ensuring that rcu_read_[un]lock() is called.
0903  *
0904  */
0905 struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
0906                            const struct in6_addr *addr)
0907 {
0908     struct netlbl_dom_map *dom_iter;
0909     struct netlbl_af6list *addr_iter;
0910 
0911     dom_iter = netlbl_domhsh_search_def(domain, AF_INET6);
0912     if (dom_iter == NULL)
0913         return NULL;
0914 
0915     if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
0916         return &dom_iter->def;
0917     addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
0918     if (addr_iter == NULL)
0919         return NULL;
0920     return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
0921 }
0922 #endif /* IPv6 */
0923 
0924 /**
0925  * netlbl_domhsh_walk - Iterate through the domain mapping hash table
0926  * @skip_bkt: the number of buckets to skip at the start
0927  * @skip_chain: the number of entries to skip in the first iterated bucket
0928  * @callback: callback for each entry
0929  * @cb_arg: argument for the callback function
0930  *
0931  * Description:
0932  * Iterate over the domain mapping hash table, skipping the first @skip_bkt
0933  * buckets and @skip_chain entries.  For each entry in the table call
0934  * @callback, if @callback returns a negative value stop 'walking' through the
0935  * table and return.  Updates the values in @skip_bkt and @skip_chain on
0936  * return.  Returns zero on success, negative values on failure.
0937  *
0938  */
0939 int netlbl_domhsh_walk(u32 *skip_bkt,
0940              u32 *skip_chain,
0941              int (*callback) (struct netlbl_dom_map *entry, void *arg),
0942              void *cb_arg)
0943 {
0944     int ret_val = -ENOENT;
0945     u32 iter_bkt;
0946     struct list_head *iter_list;
0947     struct netlbl_dom_map *iter_entry;
0948     u32 chain_cnt = 0;
0949 
0950     rcu_read_lock();
0951     for (iter_bkt = *skip_bkt;
0952          iter_bkt < rcu_dereference(netlbl_domhsh)->size;
0953          iter_bkt++, chain_cnt = 0) {
0954         iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
0955         list_for_each_entry_rcu(iter_entry, iter_list, list)
0956             if (iter_entry->valid) {
0957                 if (chain_cnt++ < *skip_chain)
0958                     continue;
0959                 ret_val = callback(iter_entry, cb_arg);
0960                 if (ret_val < 0) {
0961                     chain_cnt--;
0962                     goto walk_return;
0963                 }
0964             }
0965     }
0966 
0967 walk_return:
0968     rcu_read_unlock();
0969     *skip_bkt = iter_bkt;
0970     *skip_chain = chain_cnt;
0971     return ret_val;
0972 }