Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NetLabel Domain Hash Table |
| 3 | * |
| 4 | * This file manages the domain hash table that NetLabel uses to determine |
| 5 | * which network labeling protocol to use for a given domain. The NetLabel |
| 6 | * system manages static and dynamic label mappings for network protocols such |
| 7 | * as CIPSO and RIPSO. |
| 8 | * |
Paul Moore | 82c21bf | 2011-08-01 11:10:33 +0000 | [diff] [blame] | 9 | * Author: Paul Moore <paul@paul-moore.com> |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 10 | * |
| 11 | */ |
| 12 | |
| 13 | /* |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 14 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 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 |
Jeff Kirsher | d484ff1 | 2013-12-06 09:13:41 -0800 | [diff] [blame] | 27 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <linux/types.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 32 | #include <linux/rculist.h> |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 33 | #include <linux/skbuff.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/string.h> |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 36 | #include <linux/audit.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 38 | #include <net/netlabel.h> |
| 39 | #include <net/cipso_ipv4.h> |
| 40 | #include <asm/bug.h> |
| 41 | |
| 42 | #include "netlabel_mgmt.h" |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 43 | #include "netlabel_addrlist.h" |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 44 | #include "netlabel_domainhash.h" |
Paul Moore | 32f50cd | 2006-09-28 14:51:47 -0700 | [diff] [blame] | 45 | #include "netlabel_user.h" |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 46 | |
| 47 | struct netlbl_domhsh_tbl { |
| 48 | struct list_head *tbl; |
| 49 | u32 size; |
| 50 | }; |
| 51 | |
| 52 | /* Domain hash table */ |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 53 | /* updates should be so rare that having one spinlock for the entire hash table |
| 54 | * should be okay */ |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(netlbl_domhsh_lock); |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 56 | #define netlbl_domhsh_rcu_deref(p) \ |
Michal Hocko | d8bf4ca | 2011-07-08 14:39:41 +0200 | [diff] [blame] | 57 | rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock)) |
Huw Davies | 96a8f7f | 2016-06-27 15:02:45 -0400 | [diff] [blame] | 58 | static struct netlbl_domhsh_tbl __rcu *netlbl_domhsh; |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 59 | static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv4; |
| 60 | static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv6; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Domain Hash Table Helper Functions |
| 64 | */ |
| 65 | |
| 66 | /** |
| 67 | * netlbl_domhsh_free_entry - Frees a domain hash table entry |
| 68 | * @entry: the entry's RCU field |
| 69 | * |
| 70 | * Description: |
| 71 | * This function is designed to be used as a callback to the call_rcu() |
| 72 | * function so that the memory allocated to a hash table entry can be released |
| 73 | * safely. |
| 74 | * |
| 75 | */ |
| 76 | static void netlbl_domhsh_free_entry(struct rcu_head *entry) |
| 77 | { |
| 78 | struct netlbl_dom_map *ptr; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 79 | struct netlbl_af4list *iter4; |
| 80 | struct netlbl_af4list *tmp4; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 81 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 82 | struct netlbl_af6list *iter6; |
| 83 | struct netlbl_af6list *tmp6; |
| 84 | #endif /* IPv6 */ |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 85 | |
| 86 | ptr = container_of(entry, struct netlbl_dom_map, rcu); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 87 | if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 88 | netlbl_af4list_foreach_safe(iter4, tmp4, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 89 | &ptr->def.addrsel->list4) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 90 | netlbl_af4list_remove_entry(iter4); |
| 91 | kfree(netlbl_domhsh_addr4_entry(iter4)); |
| 92 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 93 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 94 | netlbl_af6list_foreach_safe(iter6, tmp6, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 95 | &ptr->def.addrsel->list6) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 96 | netlbl_af6list_remove_entry(iter6); |
| 97 | kfree(netlbl_domhsh_addr6_entry(iter6)); |
| 98 | } |
| 99 | #endif /* IPv6 */ |
| 100 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 101 | kfree(ptr->domain); |
| 102 | kfree(ptr); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * netlbl_domhsh_hash - Hashing function for the domain hash table |
| 107 | * @domain: the domain name to hash |
| 108 | * |
| 109 | * Description: |
| 110 | * This is the hashing function for the domain hash table, it returns the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 111 | * correct bucket number for the domain. The caller is responsible for |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 112 | * ensuring that the hash table is protected with either a RCU read lock or the |
| 113 | * hash table lock. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 114 | * |
| 115 | */ |
| 116 | static u32 netlbl_domhsh_hash(const char *key) |
| 117 | { |
| 118 | u32 iter; |
| 119 | u32 val; |
| 120 | u32 len; |
| 121 | |
| 122 | /* This is taken (with slight modification) from |
| 123 | * security/selinux/ss/symtab.c:symhash() */ |
| 124 | |
| 125 | for (iter = 0, val = 0, len = strlen(key); iter < len; iter++) |
| 126 | val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter]; |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 127 | return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 130 | static bool netlbl_family_match(u16 f1, u16 f2) |
| 131 | { |
| 132 | return (f1 == f2) || (f1 == AF_UNSPEC) || (f2 == AF_UNSPEC); |
| 133 | } |
| 134 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 135 | /** |
| 136 | * netlbl_domhsh_search - Search for a domain entry |
| 137 | * @domain: the domain |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 138 | * @family: the address family |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 139 | * |
| 140 | * Description: |
| 141 | * Searches the domain hash table and returns a pointer to the hash table |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 142 | * entry if found, otherwise NULL is returned. @family may be %AF_UNSPEC |
| 143 | * which matches any address family entries. The caller is responsible for |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 144 | * ensuring that the hash table is protected with either a RCU read lock or the |
| 145 | * hash table lock. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 146 | * |
| 147 | */ |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 148 | static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain, |
| 149 | u16 family) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 150 | { |
| 151 | u32 bkt; |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 152 | struct list_head *bkt_list; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 153 | struct netlbl_dom_map *iter; |
| 154 | |
| 155 | if (domain != NULL) { |
| 156 | bkt = netlbl_domhsh_hash(domain); |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 157 | bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt]; |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 158 | list_for_each_entry_rcu(iter, bkt_list, list) |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 159 | if (iter->valid && |
| 160 | netlbl_family_match(iter->family, family) && |
| 161 | strcmp(iter->domain, domain) == 0) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 162 | return iter; |
| 163 | } |
| 164 | |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * netlbl_domhsh_search_def - Search for a domain entry |
| 170 | * @domain: the domain |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 171 | * @family: the address family |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 172 | * |
| 173 | * Description: |
| 174 | * Searches the domain hash table and returns a pointer to the hash table |
| 175 | * entry if an exact match is found, if an exact match is not present in the |
| 176 | * hash table then the default entry is returned if valid otherwise NULL is |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 177 | * returned. @family may be %AF_UNSPEC which matches any address family |
| 178 | * entries. The caller is responsible ensuring that the hash table is |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 179 | * protected with either a RCU read lock or the hash table lock. |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 180 | * |
| 181 | */ |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 182 | static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain, |
| 183 | u16 family) |
Paul Moore | b64397e | 2008-01-29 08:37:54 -0500 | [diff] [blame] | 184 | { |
| 185 | struct netlbl_dom_map *entry; |
| 186 | |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 187 | entry = netlbl_domhsh_search(domain, family); |
| 188 | if (entry != NULL) |
| 189 | return entry; |
| 190 | if (family == AF_INET || family == AF_UNSPEC) { |
| 191 | entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4); |
| 192 | if (entry != NULL && entry->valid) |
| 193 | return entry; |
| 194 | } |
| 195 | if (family == AF_INET6 || family == AF_UNSPEC) { |
| 196 | entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6); |
| 197 | if (entry != NULL && entry->valid) |
| 198 | return entry; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 201 | return NULL; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 204 | /** |
| 205 | * netlbl_domhsh_audit_add - Generate an audit entry for an add event |
| 206 | * @entry: the entry being added |
| 207 | * @addr4: the IPv4 address information |
| 208 | * @addr6: the IPv6 address information |
| 209 | * @result: the result code |
| 210 | * @audit_info: NetLabel audit information |
| 211 | * |
| 212 | * Description: |
| 213 | * Generate an audit record for adding a new NetLabel/LSM mapping entry with |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 214 | * the given information. Caller is responsible for holding the necessary |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 215 | * locks. |
| 216 | * |
| 217 | */ |
| 218 | static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry, |
| 219 | struct netlbl_af4list *addr4, |
| 220 | struct netlbl_af6list *addr6, |
| 221 | int result, |
| 222 | struct netlbl_audit *audit_info) |
| 223 | { |
| 224 | struct audit_buffer *audit_buf; |
| 225 | struct cipso_v4_doi *cipsov4 = NULL; |
| 226 | u32 type; |
| 227 | |
| 228 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info); |
| 229 | if (audit_buf != NULL) { |
| 230 | audit_log_format(audit_buf, " nlbl_domain=%s", |
| 231 | entry->domain ? entry->domain : "(default)"); |
| 232 | if (addr4 != NULL) { |
| 233 | struct netlbl_domaddr4_map *map4; |
| 234 | map4 = netlbl_domhsh_addr4_entry(addr4); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 235 | type = map4->def.type; |
| 236 | cipsov4 = map4->def.cipso; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 237 | netlbl_af4list_audit_addr(audit_buf, 0, NULL, |
| 238 | addr4->addr, addr4->mask); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 239 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 240 | } else if (addr6 != NULL) { |
| 241 | struct netlbl_domaddr6_map *map6; |
| 242 | map6 = netlbl_domhsh_addr6_entry(addr6); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 243 | type = map6->def.type; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 244 | netlbl_af6list_audit_addr(audit_buf, 0, NULL, |
| 245 | &addr6->addr, &addr6->mask); |
| 246 | #endif /* IPv6 */ |
| 247 | } else { |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 248 | type = entry->def.type; |
| 249 | cipsov4 = entry->def.cipso; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 250 | } |
| 251 | switch (type) { |
| 252 | case NETLBL_NLTYPE_UNLABELED: |
| 253 | audit_log_format(audit_buf, " nlbl_protocol=unlbl"); |
| 254 | break; |
| 255 | case NETLBL_NLTYPE_CIPSOV4: |
| 256 | BUG_ON(cipsov4 == NULL); |
| 257 | audit_log_format(audit_buf, |
| 258 | " nlbl_protocol=cipsov4 cipso_doi=%u", |
| 259 | cipsov4->doi); |
| 260 | break; |
| 261 | } |
| 262 | audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0); |
| 263 | audit_log_end(audit_buf); |
| 264 | } |
| 265 | } |
| 266 | |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 267 | /** |
| 268 | * netlbl_domhsh_validate - Validate a new domain mapping entry |
| 269 | * @entry: the entry to validate |
| 270 | * |
| 271 | * This function validates the new domain mapping entry to ensure that it is |
| 272 | * a valid entry. Returns zero on success, negative values on failure. |
| 273 | * |
| 274 | */ |
| 275 | static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry) |
| 276 | { |
| 277 | struct netlbl_af4list *iter4; |
| 278 | struct netlbl_domaddr4_map *map4; |
| 279 | #if IS_ENABLED(CONFIG_IPV6) |
| 280 | struct netlbl_af6list *iter6; |
| 281 | struct netlbl_domaddr6_map *map6; |
| 282 | #endif /* IPv6 */ |
| 283 | |
| 284 | if (entry == NULL) |
| 285 | return -EINVAL; |
| 286 | |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 287 | if (entry->family != AF_INET && entry->family != AF_INET6 && |
| 288 | (entry->family != AF_UNSPEC || |
| 289 | entry->def.type != NETLBL_NLTYPE_UNLABELED)) |
| 290 | return -EINVAL; |
| 291 | |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 292 | switch (entry->def.type) { |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 293 | case NETLBL_NLTYPE_UNLABELED: |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 294 | if (entry->def.cipso != NULL || entry->def.addrsel != NULL) |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 295 | return -EINVAL; |
| 296 | break; |
| 297 | case NETLBL_NLTYPE_CIPSOV4: |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 298 | if (entry->family != AF_INET || |
| 299 | entry->def.cipso == NULL) |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | break; |
| 302 | case NETLBL_NLTYPE_ADDRSELECT: |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 303 | netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) { |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 304 | map4 = netlbl_domhsh_addr4_entry(iter4); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 305 | switch (map4->def.type) { |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 306 | case NETLBL_NLTYPE_UNLABELED: |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 307 | if (map4->def.cipso != NULL) |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 308 | return -EINVAL; |
| 309 | break; |
| 310 | case NETLBL_NLTYPE_CIPSOV4: |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 311 | if (map4->def.cipso == NULL) |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 312 | return -EINVAL; |
| 313 | break; |
| 314 | default: |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | } |
| 318 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 319 | netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) { |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 320 | map6 = netlbl_domhsh_addr6_entry(iter6); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 321 | switch (map6->def.type) { |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 322 | case NETLBL_NLTYPE_UNLABELED: |
| 323 | break; |
| 324 | default: |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | } |
| 328 | #endif /* IPv6 */ |
| 329 | break; |
| 330 | default: |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 337 | /* |
| 338 | * Domain Hash Table Functions |
| 339 | */ |
| 340 | |
| 341 | /** |
| 342 | * netlbl_domhsh_init - Init for the domain hash |
| 343 | * @size: the number of bits to use for the hash buckets |
| 344 | * |
| 345 | * Description: |
| 346 | * Initializes the domain hash table, should be called only by |
| 347 | * netlbl_user_init() during initialization. Returns zero on success, non-zero |
| 348 | * values on error. |
| 349 | * |
| 350 | */ |
Pavel Emelyanov | 05705e4 | 2008-02-17 22:33:57 -0800 | [diff] [blame] | 351 | int __init netlbl_domhsh_init(u32 size) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 352 | { |
| 353 | u32 iter; |
| 354 | struct netlbl_domhsh_tbl *hsh_tbl; |
| 355 | |
| 356 | if (size == 0) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL); |
| 360 | if (hsh_tbl == NULL) |
| 361 | return -ENOMEM; |
| 362 | hsh_tbl->size = 1 << size; |
| 363 | hsh_tbl->tbl = kcalloc(hsh_tbl->size, |
| 364 | sizeof(struct list_head), |
| 365 | GFP_KERNEL); |
| 366 | if (hsh_tbl->tbl == NULL) { |
| 367 | kfree(hsh_tbl); |
| 368 | return -ENOMEM; |
| 369 | } |
| 370 | for (iter = 0; iter < hsh_tbl->size; iter++) |
| 371 | INIT_LIST_HEAD(&hsh_tbl->tbl[iter]); |
| 372 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 373 | spin_lock(&netlbl_domhsh_lock); |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 374 | rcu_assign_pointer(netlbl_domhsh, hsh_tbl); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 375 | spin_unlock(&netlbl_domhsh_lock); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * netlbl_domhsh_add - Adds a entry to the domain hash table |
| 382 | * @entry: the entry to add |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 383 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 384 | * |
| 385 | * Description: |
| 386 | * Adds a new entry to the domain hash table and handles any updates to the |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 387 | * lower level protocol handler (i.e. CIPSO). @entry->family may be set to |
| 388 | * %AF_UNSPEC which will add an entry that matches all address families. This |
| 389 | * is only useful for the unlabelled type and will only succeed if there is no |
| 390 | * existing entry for any address family with the same domain. Returns zero |
| 391 | * on success, negative on failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 392 | * |
| 393 | */ |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 394 | int netlbl_domhsh_add(struct netlbl_dom_map *entry, |
| 395 | struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 396 | { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 397 | int ret_val = 0; |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 398 | struct netlbl_dom_map *entry_old, *entry_b; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 399 | struct netlbl_af4list *iter4; |
| 400 | struct netlbl_af4list *tmp4; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 401 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 402 | struct netlbl_af6list *iter6; |
| 403 | struct netlbl_af6list *tmp6; |
| 404 | #endif /* IPv6 */ |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 405 | |
Paul Moore | 6b21e1b | 2013-05-17 09:08:50 +0000 | [diff] [blame] | 406 | ret_val = netlbl_domhsh_validate(entry); |
| 407 | if (ret_val != 0) |
| 408 | return ret_val; |
| 409 | |
Paul Moore | b914f3a | 2010-04-01 10:43:57 +0000 | [diff] [blame] | 410 | /* XXX - we can remove this RCU read lock as the spinlock protects the |
| 411 | * entire function, but before we do we need to fixup the |
| 412 | * netlbl_af[4,6]list RCU functions to do "the right thing" with |
| 413 | * respect to rcu_dereference() when only a spinlock is held. */ |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 414 | rcu_read_lock(); |
Paul Moore | 1c3fad9 | 2008-01-29 08:37:57 -0500 | [diff] [blame] | 415 | spin_lock(&netlbl_domhsh_lock); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 416 | if (entry->domain != NULL) |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 417 | entry_old = netlbl_domhsh_search(entry->domain, entry->family); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 418 | else |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 419 | entry_old = netlbl_domhsh_search_def(entry->domain, |
| 420 | entry->family); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 421 | if (entry_old == NULL) { |
| 422 | entry->valid = 1; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 423 | |
| 424 | if (entry->domain != NULL) { |
| 425 | u32 bkt = netlbl_domhsh_hash(entry->domain); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 426 | list_add_tail_rcu(&entry->list, |
Paul Moore | 3482fd9 | 2007-08-07 17:53:10 -0700 | [diff] [blame] | 427 | &rcu_dereference(netlbl_domhsh)->tbl[bkt]); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 428 | } else { |
| 429 | INIT_LIST_HEAD(&entry->list); |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 430 | switch (entry->family) { |
| 431 | case AF_INET: |
| 432 | rcu_assign_pointer(netlbl_domhsh_def_ipv4, |
| 433 | entry); |
| 434 | break; |
| 435 | case AF_INET6: |
| 436 | rcu_assign_pointer(netlbl_domhsh_def_ipv6, |
| 437 | entry); |
| 438 | break; |
| 439 | case AF_UNSPEC: |
| 440 | if (entry->def.type != |
| 441 | NETLBL_NLTYPE_UNLABELED) { |
| 442 | ret_val = -EINVAL; |
| 443 | goto add_return; |
| 444 | } |
| 445 | entry_b = kzalloc(sizeof(*entry_b), GFP_ATOMIC); |
| 446 | if (entry_b == NULL) { |
| 447 | ret_val = -ENOMEM; |
| 448 | goto add_return; |
| 449 | } |
| 450 | entry_b->family = AF_INET6; |
| 451 | entry_b->def.type = NETLBL_NLTYPE_UNLABELED; |
| 452 | entry_b->valid = 1; |
| 453 | entry->family = AF_INET; |
| 454 | rcu_assign_pointer(netlbl_domhsh_def_ipv4, |
| 455 | entry); |
| 456 | rcu_assign_pointer(netlbl_domhsh_def_ipv6, |
| 457 | entry_b); |
| 458 | break; |
| 459 | default: |
| 460 | /* Already checked in |
| 461 | * netlbl_domhsh_validate(). */ |
| 462 | ret_val = -EINVAL; |
| 463 | goto add_return; |
| 464 | } |
Paul Moore | de64688 | 2006-11-17 17:38:55 -0500 | [diff] [blame] | 465 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 466 | |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 467 | if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 468 | netlbl_af4list_foreach_rcu(iter4, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 469 | &entry->def.addrsel->list4) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 470 | netlbl_domhsh_audit_add(entry, iter4, NULL, |
| 471 | ret_val, audit_info); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 472 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 473 | netlbl_af6list_foreach_rcu(iter6, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 474 | &entry->def.addrsel->list6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 475 | netlbl_domhsh_audit_add(entry, NULL, iter6, |
| 476 | ret_val, audit_info); |
| 477 | #endif /* IPv6 */ |
| 478 | } else |
| 479 | netlbl_domhsh_audit_add(entry, NULL, NULL, |
| 480 | ret_val, audit_info); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 481 | } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT && |
| 482 | entry->def.type == NETLBL_NLTYPE_ADDRSELECT) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 483 | struct list_head *old_list4; |
| 484 | struct list_head *old_list6; |
| 485 | |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 486 | old_list4 = &entry_old->def.addrsel->list4; |
| 487 | old_list6 = &entry_old->def.addrsel->list6; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 488 | |
| 489 | /* we only allow the addition of address selectors if all of |
| 490 | * the selectors do not exist in the existing domain map */ |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 491 | netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 492 | if (netlbl_af4list_search_exact(iter4->addr, |
| 493 | iter4->mask, |
| 494 | old_list4)) { |
| 495 | ret_val = -EEXIST; |
| 496 | goto add_return; |
| 497 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 498 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 499 | netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 500 | if (netlbl_af6list_search_exact(&iter6->addr, |
| 501 | &iter6->mask, |
| 502 | old_list6)) { |
| 503 | ret_val = -EEXIST; |
| 504 | goto add_return; |
| 505 | } |
| 506 | #endif /* IPv6 */ |
| 507 | |
| 508 | netlbl_af4list_foreach_safe(iter4, tmp4, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 509 | &entry->def.addrsel->list4) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 510 | netlbl_af4list_remove_entry(iter4); |
| 511 | iter4->valid = 1; |
| 512 | ret_val = netlbl_af4list_add(iter4, old_list4); |
| 513 | netlbl_domhsh_audit_add(entry_old, iter4, NULL, |
| 514 | ret_val, audit_info); |
| 515 | if (ret_val != 0) |
| 516 | goto add_return; |
| 517 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 518 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 519 | netlbl_af6list_foreach_safe(iter6, tmp6, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 520 | &entry->def.addrsel->list6) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 521 | netlbl_af6list_remove_entry(iter6); |
| 522 | iter6->valid = 1; |
| 523 | ret_val = netlbl_af6list_add(iter6, old_list6); |
| 524 | netlbl_domhsh_audit_add(entry_old, NULL, iter6, |
| 525 | ret_val, audit_info); |
| 526 | if (ret_val != 0) |
| 527 | goto add_return; |
| 528 | } |
| 529 | #endif /* IPv6 */ |
| 530 | } else |
| 531 | ret_val = -EINVAL; |
| 532 | |
| 533 | add_return: |
| 534 | spin_unlock(&netlbl_domhsh_lock); |
| 535 | rcu_read_unlock(); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 536 | return ret_val; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * netlbl_domhsh_add_default - Adds the default entry to the domain hash table |
| 541 | * @entry: the entry to add |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 542 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 543 | * |
| 544 | * Description: |
| 545 | * Adds a new default entry to the domain hash table and handles any updates |
| 546 | * to the lower level protocol handler (i.e. CIPSO). Returns zero on success, |
| 547 | * negative on failure. |
| 548 | * |
| 549 | */ |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 550 | int netlbl_domhsh_add_default(struct netlbl_dom_map *entry, |
| 551 | struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 552 | { |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 553 | return netlbl_domhsh_add(entry, audit_info); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /** |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 557 | * netlbl_domhsh_remove_entry - Removes a given entry from the domain table |
| 558 | * @entry: the entry to remove |
| 559 | * @audit_info: NetLabel audit information |
| 560 | * |
| 561 | * Description: |
| 562 | * Removes an entry from the domain hash table and handles any updates to the |
| 563 | * lower level protocol handler (i.e. CIPSO). Caller is responsible for |
| 564 | * ensuring that the RCU read lock is held. Returns zero on success, negative |
| 565 | * on failure. |
| 566 | * |
| 567 | */ |
| 568 | int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry, |
| 569 | struct netlbl_audit *audit_info) |
| 570 | { |
| 571 | int ret_val = 0; |
| 572 | struct audit_buffer *audit_buf; |
| 573 | |
| 574 | if (entry == NULL) |
| 575 | return -ENOENT; |
| 576 | |
| 577 | spin_lock(&netlbl_domhsh_lock); |
| 578 | if (entry->valid) { |
| 579 | entry->valid = 0; |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 580 | if (entry == rcu_dereference(netlbl_domhsh_def_ipv4)) |
| 581 | RCU_INIT_POINTER(netlbl_domhsh_def_ipv4, NULL); |
| 582 | else if (entry == rcu_dereference(netlbl_domhsh_def_ipv6)) |
| 583 | RCU_INIT_POINTER(netlbl_domhsh_def_ipv6, NULL); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 584 | else |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 585 | list_del_rcu(&entry->list); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 586 | } else |
| 587 | ret_val = -ENOENT; |
| 588 | spin_unlock(&netlbl_domhsh_lock); |
| 589 | |
| 590 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info); |
| 591 | if (audit_buf != NULL) { |
| 592 | audit_log_format(audit_buf, |
| 593 | " nlbl_domain=%s res=%u", |
| 594 | entry->domain ? entry->domain : "(default)", |
| 595 | ret_val == 0 ? 1 : 0); |
| 596 | audit_log_end(audit_buf); |
| 597 | } |
| 598 | |
| 599 | if (ret_val == 0) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 600 | struct netlbl_af4list *iter4; |
| 601 | struct netlbl_domaddr4_map *map4; |
| 602 | |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 603 | switch (entry->def.type) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 604 | case NETLBL_NLTYPE_ADDRSELECT: |
| 605 | netlbl_af4list_foreach_rcu(iter4, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 606 | &entry->def.addrsel->list4) { |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 607 | map4 = netlbl_domhsh_addr4_entry(iter4); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 608 | cipso_v4_doi_putdef(map4->def.cipso); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 609 | } |
| 610 | /* no need to check the IPv6 list since we currently |
| 611 | * support only unlabeled protocols for IPv6 */ |
| 612 | break; |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 613 | case NETLBL_NLTYPE_CIPSOV4: |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 614 | cipso_v4_doi_putdef(entry->def.cipso); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 615 | break; |
| 616 | } |
| 617 | call_rcu(&entry->rcu, netlbl_domhsh_free_entry); |
| 618 | } |
| 619 | |
| 620 | return ret_val; |
| 621 | } |
| 622 | |
| 623 | /** |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 624 | * netlbl_domhsh_remove_af4 - Removes an address selector entry |
| 625 | * @domain: the domain |
| 626 | * @addr: IPv4 address |
| 627 | * @mask: IPv4 address mask |
| 628 | * @audit_info: NetLabel audit information |
| 629 | * |
| 630 | * Description: |
| 631 | * Removes an individual address selector from a domain mapping and potentially |
| 632 | * the entire mapping if it is empty. Returns zero on success, negative values |
| 633 | * on failure. |
| 634 | * |
| 635 | */ |
| 636 | int netlbl_domhsh_remove_af4(const char *domain, |
| 637 | const struct in_addr *addr, |
| 638 | const struct in_addr *mask, |
| 639 | struct netlbl_audit *audit_info) |
| 640 | { |
| 641 | struct netlbl_dom_map *entry_map; |
| 642 | struct netlbl_af4list *entry_addr; |
| 643 | struct netlbl_af4list *iter4; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 644 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 645 | struct netlbl_af6list *iter6; |
| 646 | #endif /* IPv6 */ |
| 647 | struct netlbl_domaddr4_map *entry; |
| 648 | |
| 649 | rcu_read_lock(); |
| 650 | |
| 651 | if (domain) |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 652 | entry_map = netlbl_domhsh_search(domain, AF_INET); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 653 | else |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 654 | entry_map = netlbl_domhsh_search_def(domain, AF_INET); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 655 | if (entry_map == NULL || |
| 656 | entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT) |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 657 | goto remove_af4_failure; |
| 658 | |
| 659 | spin_lock(&netlbl_domhsh_lock); |
| 660 | entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr, |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 661 | &entry_map->def.addrsel->list4); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 662 | spin_unlock(&netlbl_domhsh_lock); |
| 663 | |
| 664 | if (entry_addr == NULL) |
| 665 | goto remove_af4_failure; |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 666 | netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4) |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 667 | goto remove_af4_single_addr; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 668 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 669 | netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6) |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 670 | goto remove_af4_single_addr; |
| 671 | #endif /* IPv6 */ |
| 672 | /* the domain mapping is empty so remove it from the mapping table */ |
| 673 | netlbl_domhsh_remove_entry(entry_map, audit_info); |
| 674 | |
| 675 | remove_af4_single_addr: |
| 676 | rcu_read_unlock(); |
| 677 | /* yick, we can't use call_rcu here because we don't have a rcu head |
| 678 | * pointer but hopefully this should be a rare case so the pause |
| 679 | * shouldn't be a problem */ |
| 680 | synchronize_rcu(); |
| 681 | entry = netlbl_domhsh_addr4_entry(entry_addr); |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 682 | cipso_v4_doi_putdef(entry->def.cipso); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 683 | kfree(entry); |
| 684 | return 0; |
| 685 | |
| 686 | remove_af4_failure: |
| 687 | rcu_read_unlock(); |
| 688 | return -ENOENT; |
| 689 | } |
| 690 | |
| 691 | /** |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 692 | * netlbl_domhsh_remove - Removes an entry from the domain hash table |
| 693 | * @domain: the domain to remove |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 694 | * @family: address family |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 695 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 696 | * |
| 697 | * Description: |
| 698 | * Removes an entry from the domain hash table and handles any updates to the |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 699 | * lower level protocol handler (i.e. CIPSO). @family may be %AF_UNSPEC which |
| 700 | * removes all address family entries. Returns zero on success, negative on |
| 701 | * failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 702 | * |
| 703 | */ |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 704 | int netlbl_domhsh_remove(const char *domain, u16 family, |
| 705 | struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 706 | { |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 707 | int ret_val = -EINVAL; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 708 | struct netlbl_dom_map *entry; |
| 709 | |
| 710 | rcu_read_lock(); |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 711 | |
| 712 | if (family == AF_INET || family == AF_UNSPEC) { |
| 713 | if (domain) |
| 714 | entry = netlbl_domhsh_search(domain, AF_INET); |
| 715 | else |
| 716 | entry = netlbl_domhsh_search_def(domain, AF_INET); |
| 717 | ret_val = netlbl_domhsh_remove_entry(entry, audit_info); |
| 718 | if (ret_val && ret_val != -ENOENT) |
| 719 | goto done; |
| 720 | } |
| 721 | if (family == AF_INET6 || family == AF_UNSPEC) { |
| 722 | int ret_val2; |
| 723 | |
| 724 | if (domain) |
| 725 | entry = netlbl_domhsh_search(domain, AF_INET6); |
| 726 | else |
| 727 | entry = netlbl_domhsh_search_def(domain, AF_INET6); |
| 728 | ret_val2 = netlbl_domhsh_remove_entry(entry, audit_info); |
| 729 | if (ret_val2 != -ENOENT) |
| 730 | ret_val = ret_val2; |
| 731 | } |
| 732 | done: |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 733 | rcu_read_unlock(); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 734 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 735 | return ret_val; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * netlbl_domhsh_remove_default - Removes the default entry from the table |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 740 | * @family: address family |
Paul Moore | 95d4e6b | 2006-09-29 17:05:05 -0700 | [diff] [blame] | 741 | * @audit_info: NetLabel audit information |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 742 | * |
| 743 | * Description: |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 744 | * Removes/resets the default entry corresponding to @family from the domain |
| 745 | * hash table and handles any updates to the lower level protocol handler |
| 746 | * (i.e. CIPSO). @family may be %AF_UNSPEC which removes all address family |
| 747 | * entries. Returns zero on success, negative on failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 748 | * |
| 749 | */ |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 750 | int netlbl_domhsh_remove_default(u16 family, struct netlbl_audit *audit_info) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 751 | { |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 752 | return netlbl_domhsh_remove(NULL, family, audit_info); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
| 756 | * netlbl_domhsh_getentry - Get an entry from the domain hash table |
| 757 | * @domain: the domain name to search for |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 758 | * @family: address family |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 759 | * |
| 760 | * Description: |
| 761 | * Look through the domain hash table searching for an entry to match @domain, |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 762 | * with address family @family, return a pointer to a copy of the entry or |
| 763 | * NULL. The caller is responsible for ensuring that rcu_read_[un]lock() is |
| 764 | * called. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 765 | * |
| 766 | */ |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 767 | struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain, u16 family) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 768 | { |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 769 | if (family == AF_UNSPEC) |
| 770 | return NULL; |
| 771 | return netlbl_domhsh_search_def(domain, family); |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 775 | * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table |
| 776 | * @domain: the domain name to search for |
| 777 | * @addr: the IP address to search for |
| 778 | * |
| 779 | * Description: |
| 780 | * Look through the domain hash table searching for an entry to match @domain |
| 781 | * and @addr, return a pointer to a copy of the entry or NULL. The caller is |
| 782 | * responsible for ensuring that rcu_read_[un]lock() is called. |
| 783 | * |
| 784 | */ |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 785 | struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain, |
| 786 | __be32 addr) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 787 | { |
| 788 | struct netlbl_dom_map *dom_iter; |
| 789 | struct netlbl_af4list *addr_iter; |
| 790 | |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 791 | dom_iter = netlbl_domhsh_search_def(domain, AF_INET); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 792 | if (dom_iter == NULL) |
| 793 | return NULL; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 794 | |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 795 | if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT) |
| 796 | return &dom_iter->def; |
| 797 | addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 798 | if (addr_iter == NULL) |
| 799 | return NULL; |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 800 | return &(netlbl_domhsh_addr4_entry(addr_iter)->def); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 801 | } |
| 802 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 803 | #if IS_ENABLED(CONFIG_IPV6) |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 804 | /** |
| 805 | * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table |
| 806 | * @domain: the domain name to search for |
| 807 | * @addr: the IP address to search for |
| 808 | * |
| 809 | * Description: |
| 810 | * Look through the domain hash table searching for an entry to match @domain |
| 811 | * and @addr, return a pointer to a copy of the entry or NULL. The caller is |
| 812 | * responsible for ensuring that rcu_read_[un]lock() is called. |
| 813 | * |
| 814 | */ |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 815 | struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain, |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 816 | const struct in6_addr *addr) |
| 817 | { |
| 818 | struct netlbl_dom_map *dom_iter; |
| 819 | struct netlbl_af6list *addr_iter; |
| 820 | |
Huw Davies | 8f18e67 | 2016-06-27 15:02:46 -0400 | [diff] [blame^] | 821 | dom_iter = netlbl_domhsh_search_def(domain, AF_INET6); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 822 | if (dom_iter == NULL) |
| 823 | return NULL; |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 824 | |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 825 | if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT) |
| 826 | return &dom_iter->def; |
| 827 | addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 828 | if (addr_iter == NULL) |
| 829 | return NULL; |
Paul Moore | 6a8b7f0 | 2013-08-02 14:45:08 -0400 | [diff] [blame] | 830 | return &(netlbl_domhsh_addr6_entry(addr_iter)->def); |
Paul Moore | 63c4168 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 831 | } |
| 832 | #endif /* IPv6 */ |
| 833 | |
| 834 | /** |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 835 | * netlbl_domhsh_walk - Iterate through the domain mapping hash table |
| 836 | * @skip_bkt: the number of buckets to skip at the start |
| 837 | * @skip_chain: the number of entries to skip in the first iterated bucket |
| 838 | * @callback: callback for each entry |
| 839 | * @cb_arg: argument for the callback function |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 840 | * |
| 841 | * Description: |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 842 | * Interate over the domain mapping hash table, skipping the first @skip_bkt |
| 843 | * buckets and @skip_chain entries. For each entry in the table call |
| 844 | * @callback, if @callback returns a negative value stop 'walking' through the |
| 845 | * table and return. Updates the values in @skip_bkt and @skip_chain on |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 846 | * return. Returns zero on success, negative values on failure. |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 847 | * |
| 848 | */ |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 849 | int netlbl_domhsh_walk(u32 *skip_bkt, |
| 850 | u32 *skip_chain, |
| 851 | int (*callback) (struct netlbl_dom_map *entry, void *arg), |
| 852 | void *cb_arg) |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 853 | { |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 854 | int ret_val = -ENOENT; |
| 855 | u32 iter_bkt; |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 856 | struct list_head *iter_list; |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 857 | struct netlbl_dom_map *iter_entry; |
| 858 | u32 chain_cnt = 0; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 859 | |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 860 | rcu_read_lock(); |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 861 | for (iter_bkt = *skip_bkt; |
| 862 | iter_bkt < rcu_dereference(netlbl_domhsh)->size; |
| 863 | iter_bkt++, chain_cnt = 0) { |
Paul Moore | 5619670 | 2008-10-10 10:16:29 -0400 | [diff] [blame] | 864 | iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt]; |
| 865 | list_for_each_entry_rcu(iter_entry, iter_list, list) |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 866 | if (iter_entry->valid) { |
| 867 | if (chain_cnt++ < *skip_chain) |
| 868 | continue; |
| 869 | ret_val = callback(iter_entry, cb_arg); |
| 870 | if (ret_val < 0) { |
| 871 | chain_cnt--; |
| 872 | goto walk_return; |
| 873 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 874 | } |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 875 | } |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 876 | |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 877 | walk_return: |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 878 | rcu_read_unlock(); |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 879 | *skip_bkt = iter_bkt; |
| 880 | *skip_chain = chain_cnt; |
| 881 | return ret_val; |
Paul Moore | d15c345 | 2006-08-03 16:48:37 -0700 | [diff] [blame] | 882 | } |