1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * NetLabel Domain Hash Table
4 *
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
8 * as CIPSO and RIPSO.
9 *
10 * Author: Paul Moore <paul@paul-moore.com>
11 */
12
13 /*
14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
15 */
16
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>
27 #include <asm/bug.h>
28
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"
34
35 struct netlbl_domhsh_tbl {
36 struct list_head *tbl;
37 u32 size;
38 };
39
40 /* Domain hash table */
41 /* updates should be so rare that having one spinlock for the entire hash table
42 * should be okay */
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;
49
50 /*
51 * Domain Hash Table Helper Functions
52 */
53
54 /**
55 * netlbl_domhsh_free_entry - Frees a domain hash table entry
56 * @entry: the entry's RCU field
57 *
58 * Description:
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
61 * safely.
62 *
63 */
netlbl_domhsh_free_entry(struct rcu_head * entry)64 static void netlbl_domhsh_free_entry(struct rcu_head *entry)
65 {
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;
72 #endif /* IPv6 */
73
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));
80 }
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));
86 }
87 #endif /* IPv6 */
88 kfree(ptr->def.addrsel);
89 }
90 kfree(ptr->domain);
91 kfree(ptr);
92 }
93
94 /**
95 * netlbl_domhsh_hash - Hashing function for the domain hash table
96 * @key: the domain name to hash
97 *
98 * Description:
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
102 * hash table lock.
103 *
104 */
netlbl_domhsh_hash(const char * key)105 static u32 netlbl_domhsh_hash(const char *key)
106 {
107 u32 iter;
108 u32 val;
109 u32 len;
110
111 /* This is taken (with slight modification) from
112 * security/selinux/ss/symtab.c:symhash() */
113
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);
117 }
118
netlbl_family_match(u16 f1,u16 f2)119 static bool netlbl_family_match(u16 f1, u16 f2)
120 {
121 return (f1 == f2) || (f1 == AF_UNSPEC) || (f2 == AF_UNSPEC);
122 }
123
124 /**
125 * netlbl_domhsh_search - Search for a domain entry
126 * @domain: the domain
127 * @family: the address family
128 *
129 * Description:
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
134 * hash table lock.
135 *
136 */
netlbl_domhsh_search(const char * domain,u16 family)137 static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain,
138 u16 family)
139 {
140 u32 bkt;
141 struct list_head *bkt_list;
142 struct netlbl_dom_map *iter;
143
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))
149 if (iter->valid &&
150 netlbl_family_match(iter->family, family) &&
151 strcmp(iter->domain, domain) == 0)
152 return iter;
153 }
154
155 return NULL;
156 }
157
158 /**
159 * netlbl_domhsh_search_def - Search for a domain entry
160 * @domain: the domain
161 * @family: the address family
162 *
163 * Description:
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.
170 *
171 */
netlbl_domhsh_search_def(const char * domain,u16 family)172 static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain,
173 u16 family)
174 {
175 struct netlbl_dom_map *entry;
176
177 entry = netlbl_domhsh_search(domain, family);
178 if (entry != NULL)
179 return entry;
180 if (family == AF_INET || family == AF_UNSPEC) {
181 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4);
182 if (entry != NULL && entry->valid)
183 return entry;
184 }
185 if (family == AF_INET6 || family == AF_UNSPEC) {
186 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6);
187 if (entry != NULL && entry->valid)
188 return entry;
189 }
190
191 return NULL;
192 }
193
194 /**
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
201 *
202 * Description:
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
205 * locks.
206 *
207 */
netlbl_domhsh_audit_add(struct netlbl_dom_map * entry,struct netlbl_af4list * addr4,struct netlbl_af6list * addr6,int result,struct netlbl_audit * audit_info)208 static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
209 struct netlbl_af4list *addr4,
210 struct netlbl_af6list *addr6,
211 int result,
212 struct netlbl_audit *audit_info)
213 {
214 struct audit_buffer *audit_buf;
215 struct cipso_v4_doi *cipsov4 = NULL;
216 struct calipso_doi *calipso = NULL;
217 u32 type;
218
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)");
223 if (addr4 != NULL) {
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);
238 #endif /* IPv6 */
239 } else {
240 type = entry->def.type;
241 cipsov4 = entry->def.cipso;
242 calipso = entry->def.calipso;
243 }
244 switch (type) {
245 case NETLBL_NLTYPE_UNLABELED:
246 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
247 break;
248 case NETLBL_NLTYPE_CIPSOV4:
249 BUG_ON(cipsov4 == NULL);
250 audit_log_format(audit_buf,
251 " nlbl_protocol=cipsov4 cipso_doi=%u",
252 cipsov4->doi);
253 break;
254 case NETLBL_NLTYPE_CALIPSO:
255 BUG_ON(calipso == NULL);
256 audit_log_format(audit_buf,
257 " nlbl_protocol=calipso calipso_doi=%u",
258 calipso->doi);
259 break;
260 }
261 audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
262 audit_log_end(audit_buf);
263 }
264 }
265
266 /**
267 * netlbl_domhsh_validate - Validate a new domain mapping entry
268 * @entry: the entry to validate
269 *
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.
272 *
273 */
netlbl_domhsh_validate(const struct netlbl_dom_map * entry)274 static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
275 {
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;
281 #endif /* IPv6 */
282
283 if (entry == NULL)
284 return -EINVAL;
285
286 if (entry->family != AF_INET && entry->family != AF_INET6 &&
287 (entry->family != AF_UNSPEC ||
288 entry->def.type != NETLBL_NLTYPE_UNLABELED))
289 return -EINVAL;
290
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)
295 return -EINVAL;
296 break;
297 case NETLBL_NLTYPE_CIPSOV4:
298 if (entry->family != AF_INET ||
299 entry->def.cipso == NULL)
300 return -EINVAL;
301 break;
302 case NETLBL_NLTYPE_CALIPSO:
303 if (entry->family != AF_INET6 ||
304 entry->def.calipso == NULL)
305 return -EINVAL;
306 break;
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)
313 return -EINVAL;
314 break;
315 case NETLBL_NLTYPE_CIPSOV4:
316 if (map4->def.cipso == NULL)
317 return -EINVAL;
318 break;
319 default:
320 return -EINVAL;
321 }
322 }
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)
329 return -EINVAL;
330 break;
331 case NETLBL_NLTYPE_CALIPSO:
332 if (map6->def.calipso == NULL)
333 return -EINVAL;
334 break;
335 default:
336 return -EINVAL;
337 }
338 }
339 #endif /* IPv6 */
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 return 0;
346 }
347
348 /*
349 * Domain Hash Table Functions
350 */
351
352 /**
353 * netlbl_domhsh_init - Init for the domain hash
354 * @size: the number of bits to use for the hash buckets
355 *
356 * Description:
357 * Initializes the domain hash table, should be called only by
358 * netlbl_user_init() during initialization. Returns zero on success, non-zero
359 * values on error.
360 *
361 */
netlbl_domhsh_init(u32 size)362 int __init netlbl_domhsh_init(u32 size)
363 {
364 u32 iter;
365 struct netlbl_domhsh_tbl *hsh_tbl;
366
367 if (size == 0)
368 return -EINVAL;
369
370 hsh_tbl = kmalloc_obj(*hsh_tbl);
371 if (hsh_tbl == NULL)
372 return -ENOMEM;
373 hsh_tbl->size = 1 << size;
374 hsh_tbl->tbl = kzalloc_objs(struct list_head, hsh_tbl->size);
375 if (hsh_tbl->tbl == NULL) {
376 kfree(hsh_tbl);
377 return -ENOMEM;
378 }
379 for (iter = 0; iter < hsh_tbl->size; iter++)
380 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
381
382 spin_lock(&netlbl_domhsh_lock);
383 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
384 spin_unlock(&netlbl_domhsh_lock);
385
386 return 0;
387 }
388
389 /**
390 * netlbl_domhsh_add - Adds a entry to the domain hash table
391 * @entry: the entry to add
392 * @audit_info: NetLabel audit information
393 *
394 * Description:
395 * Adds a new entry to the domain hash table and handles any updates to the
396 * lower level protocol handler (i.e. CIPSO). @entry->family may be set to
397 * %AF_UNSPEC which will add an entry that matches all address families. This
398 * is only useful for the unlabelled type and will only succeed if there is no
399 * existing entry for any address family with the same domain. Returns zero
400 * on success, negative on failure.
401 *
402 */
netlbl_domhsh_add(struct netlbl_dom_map * entry,struct netlbl_audit * audit_info)403 int netlbl_domhsh_add(struct netlbl_dom_map *entry,
404 struct netlbl_audit *audit_info)
405 {
406 int ret_val = 0;
407 struct netlbl_dom_map *entry_old, *entry_b;
408 struct netlbl_af4list *iter4;
409 struct netlbl_af4list *tmp4;
410 #if IS_ENABLED(CONFIG_IPV6)
411 struct netlbl_af6list *iter6;
412 struct netlbl_af6list *tmp6;
413 #endif /* IPv6 */
414
415 ret_val = netlbl_domhsh_validate(entry);
416 if (ret_val != 0)
417 return ret_val;
418
419 /* XXX - we can remove this RCU read lock as the spinlock protects the
420 * entire function, but before we do we need to fixup the
421 * netlbl_af[4,6]list RCU functions to do "the right thing" with
422 * respect to rcu_dereference() when only a spinlock is held. */
423 rcu_read_lock();
424 spin_lock(&netlbl_domhsh_lock);
425 if (entry->domain != NULL)
426 entry_old = netlbl_domhsh_search(entry->domain, entry->family);
427 else
428 entry_old = netlbl_domhsh_search_def(entry->domain,
429 entry->family);
430 if (entry_old == NULL) {
431 entry->valid = 1;
432
433 if (entry->domain != NULL) {
434 u32 bkt = netlbl_domhsh_hash(entry->domain);
435 list_add_tail_rcu(&entry->list,
436 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
437 } else {
438 INIT_LIST_HEAD(&entry->list);
439 switch (entry->family) {
440 case AF_INET:
441 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
442 entry);
443 break;
444 case AF_INET6:
445 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
446 entry);
447 break;
448 case AF_UNSPEC:
449 if (entry->def.type !=
450 NETLBL_NLTYPE_UNLABELED) {
451 ret_val = -EINVAL;
452 goto add_return;
453 }
454 entry_b = kzalloc_obj(*entry_b, GFP_ATOMIC);
455 if (entry_b == NULL) {
456 ret_val = -ENOMEM;
457 goto add_return;
458 }
459 entry_b->family = AF_INET6;
460 entry_b->def.type = NETLBL_NLTYPE_UNLABELED;
461 entry_b->valid = 1;
462 entry->family = AF_INET;
463 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
464 entry);
465 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
466 entry_b);
467 break;
468 default:
469 /* Already checked in
470 * netlbl_domhsh_validate(). */
471 ret_val = -EINVAL;
472 goto add_return;
473 }
474 }
475
476 if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
477 netlbl_af4list_foreach_rcu(iter4,
478 &entry->def.addrsel->list4)
479 netlbl_domhsh_audit_add(entry, iter4, NULL,
480 ret_val, audit_info);
481 #if IS_ENABLED(CONFIG_IPV6)
482 netlbl_af6list_foreach_rcu(iter6,
483 &entry->def.addrsel->list6)
484 netlbl_domhsh_audit_add(entry, NULL, iter6,
485 ret_val, audit_info);
486 #endif /* IPv6 */
487 } else
488 netlbl_domhsh_audit_add(entry, NULL, NULL,
489 ret_val, audit_info);
490 } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
491 entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
492 struct list_head *old_list4;
493 struct list_head *old_list6;
494
495 old_list4 = &entry_old->def.addrsel->list4;
496 old_list6 = &entry_old->def.addrsel->list6;
497
498 /* we only allow the addition of address selectors if all of
499 * the selectors do not exist in the existing domain map */
500 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
501 if (netlbl_af4list_search_exact(iter4->addr,
502 iter4->mask,
503 old_list4)) {
504 ret_val = -EEXIST;
505 goto add_return;
506 }
507 #if IS_ENABLED(CONFIG_IPV6)
508 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
509 if (netlbl_af6list_search_exact(&iter6->addr,
510 &iter6->mask,
511 old_list6)) {
512 ret_val = -EEXIST;
513 goto add_return;
514 }
515 #endif /* IPv6 */
516
517 netlbl_af4list_foreach_safe(iter4, tmp4,
518 &entry->def.addrsel->list4) {
519 netlbl_af4list_remove_entry(iter4);
520 iter4->valid = 1;
521 ret_val = netlbl_af4list_add(iter4, old_list4);
522 netlbl_domhsh_audit_add(entry_old, iter4, NULL,
523 ret_val, audit_info);
524 if (ret_val != 0)
525 goto add_return;
526 }
527 #if IS_ENABLED(CONFIG_IPV6)
528 netlbl_af6list_foreach_safe(iter6, tmp6,
529 &entry->def.addrsel->list6) {
530 netlbl_af6list_remove_entry(iter6);
531 iter6->valid = 1;
532 ret_val = netlbl_af6list_add(iter6, old_list6);
533 netlbl_domhsh_audit_add(entry_old, NULL, iter6,
534 ret_val, audit_info);
535 if (ret_val != 0)
536 goto add_return;
537 }
538 #endif /* IPv6 */
539 /* cleanup the new entry since we've moved everything over */
540 netlbl_domhsh_free_entry(&entry->rcu);
541 } else
542 ret_val = -EINVAL;
543
544 add_return:
545 spin_unlock(&netlbl_domhsh_lock);
546 rcu_read_unlock();
547 return ret_val;
548 }
549
550 /**
551 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
552 * @entry: the entry to add
553 * @audit_info: NetLabel audit information
554 *
555 * Description:
556 * Adds a new default entry to the domain hash table and handles any updates
557 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
558 * negative on failure.
559 *
560 */
netlbl_domhsh_add_default(struct netlbl_dom_map * entry,struct netlbl_audit * audit_info)561 int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
562 struct netlbl_audit *audit_info)
563 {
564 return netlbl_domhsh_add(entry, audit_info);
565 }
566
567 /**
568 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
569 * @entry: the entry to remove
570 * @audit_info: NetLabel audit information
571 *
572 * Description:
573 * Removes an entry from the domain hash table and handles any updates to the
574 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
575 * ensuring that the RCU read lock is held. Returns zero on success, negative
576 * on failure.
577 *
578 */
netlbl_domhsh_remove_entry(struct netlbl_dom_map * entry,struct netlbl_audit * audit_info)579 int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
580 struct netlbl_audit *audit_info)
581 {
582 int ret_val = 0;
583 struct audit_buffer *audit_buf;
584 struct netlbl_af4list *iter4;
585 struct netlbl_domaddr4_map *map4;
586 #if IS_ENABLED(CONFIG_IPV6)
587 struct netlbl_af6list *iter6;
588 struct netlbl_domaddr6_map *map6;
589 #endif /* IPv6 */
590
591 if (entry == NULL)
592 return -ENOENT;
593
594 spin_lock(&netlbl_domhsh_lock);
595 if (entry->valid) {
596 entry->valid = 0;
597 if (entry == rcu_dereference(netlbl_domhsh_def_ipv4))
598 RCU_INIT_POINTER(netlbl_domhsh_def_ipv4, NULL);
599 else if (entry == rcu_dereference(netlbl_domhsh_def_ipv6))
600 RCU_INIT_POINTER(netlbl_domhsh_def_ipv6, NULL);
601 else
602 list_del_rcu(&entry->list);
603 } else
604 ret_val = -ENOENT;
605 spin_unlock(&netlbl_domhsh_lock);
606
607 if (ret_val)
608 return ret_val;
609
610 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
611 if (audit_buf != NULL) {
612 audit_log_format(audit_buf,
613 " nlbl_domain=%s res=1",
614 entry->domain ? entry->domain : "(default)");
615 audit_log_end(audit_buf);
616 }
617
618 switch (entry->def.type) {
619 case NETLBL_NLTYPE_ADDRSELECT:
620 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
621 map4 = netlbl_domhsh_addr4_entry(iter4);
622 cipso_v4_doi_putdef(map4->def.cipso);
623 }
624 #if IS_ENABLED(CONFIG_IPV6)
625 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
626 map6 = netlbl_domhsh_addr6_entry(iter6);
627 calipso_doi_putdef(map6->def.calipso);
628 }
629 #endif /* IPv6 */
630 break;
631 case NETLBL_NLTYPE_CIPSOV4:
632 cipso_v4_doi_putdef(entry->def.cipso);
633 break;
634 #if IS_ENABLED(CONFIG_IPV6)
635 case NETLBL_NLTYPE_CALIPSO:
636 calipso_doi_putdef(entry->def.calipso);
637 break;
638 #endif /* IPv6 */
639 }
640 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
641
642 return ret_val;
643 }
644
645 /**
646 * netlbl_domhsh_remove_af4 - Removes an address selector entry
647 * @domain: the domain
648 * @addr: IPv4 address
649 * @mask: IPv4 address mask
650 * @audit_info: NetLabel audit information
651 *
652 * Description:
653 * Removes an individual address selector from a domain mapping and potentially
654 * the entire mapping if it is empty. Returns zero on success, negative values
655 * on failure.
656 *
657 */
netlbl_domhsh_remove_af4(const char * domain,const struct in_addr * addr,const struct in_addr * mask,struct netlbl_audit * audit_info)658 int netlbl_domhsh_remove_af4(const char *domain,
659 const struct in_addr *addr,
660 const struct in_addr *mask,
661 struct netlbl_audit *audit_info)
662 {
663 struct netlbl_dom_map *entry_map;
664 struct netlbl_af4list *entry_addr;
665 struct netlbl_af4list *iter4;
666 #if IS_ENABLED(CONFIG_IPV6)
667 struct netlbl_af6list *iter6;
668 #endif /* IPv6 */
669 struct netlbl_domaddr4_map *entry;
670
671 rcu_read_lock();
672
673 if (domain)
674 entry_map = netlbl_domhsh_search(domain, AF_INET);
675 else
676 entry_map = netlbl_domhsh_search_def(domain, AF_INET);
677 if (entry_map == NULL ||
678 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
679 goto remove_af4_failure;
680
681 spin_lock(&netlbl_domhsh_lock);
682 entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
683 &entry_map->def.addrsel->list4);
684 spin_unlock(&netlbl_domhsh_lock);
685
686 if (entry_addr == NULL)
687 goto remove_af4_failure;
688 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
689 goto remove_af4_single_addr;
690 #if IS_ENABLED(CONFIG_IPV6)
691 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
692 goto remove_af4_single_addr;
693 #endif /* IPv6 */
694 /* the domain mapping is empty so remove it from the mapping table */
695 netlbl_domhsh_remove_entry(entry_map, audit_info);
696
697 remove_af4_single_addr:
698 rcu_read_unlock();
699 /* yick, we can't use call_rcu here because we don't have a rcu head
700 * pointer but hopefully this should be a rare case so the pause
701 * shouldn't be a problem */
702 synchronize_rcu();
703 entry = netlbl_domhsh_addr4_entry(entry_addr);
704 cipso_v4_doi_putdef(entry->def.cipso);
705 kfree(entry);
706 return 0;
707
708 remove_af4_failure:
709 rcu_read_unlock();
710 return -ENOENT;
711 }
712
713 #if IS_ENABLED(CONFIG_IPV6)
714 /**
715 * netlbl_domhsh_remove_af6 - Removes an address selector entry
716 * @domain: the domain
717 * @addr: IPv6 address
718 * @mask: IPv6 address mask
719 * @audit_info: NetLabel audit information
720 *
721 * Description:
722 * Removes an individual address selector from a domain mapping and potentially
723 * the entire mapping if it is empty. Returns zero on success, negative values
724 * on failure.
725 *
726 */
netlbl_domhsh_remove_af6(const char * domain,const struct in6_addr * addr,const struct in6_addr * mask,struct netlbl_audit * audit_info)727 int netlbl_domhsh_remove_af6(const char *domain,
728 const struct in6_addr *addr,
729 const struct in6_addr *mask,
730 struct netlbl_audit *audit_info)
731 {
732 struct netlbl_dom_map *entry_map;
733 struct netlbl_af6list *entry_addr;
734 struct netlbl_af4list *iter4;
735 struct netlbl_af6list *iter6;
736 struct netlbl_domaddr6_map *entry;
737
738 rcu_read_lock();
739
740 if (domain)
741 entry_map = netlbl_domhsh_search(domain, AF_INET6);
742 else
743 entry_map = netlbl_domhsh_search_def(domain, AF_INET6);
744 if (entry_map == NULL ||
745 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
746 goto remove_af6_failure;
747
748 spin_lock(&netlbl_domhsh_lock);
749 entry_addr = netlbl_af6list_remove(addr, mask,
750 &entry_map->def.addrsel->list6);
751 spin_unlock(&netlbl_domhsh_lock);
752
753 if (entry_addr == NULL)
754 goto remove_af6_failure;
755 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
756 goto remove_af6_single_addr;
757 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
758 goto remove_af6_single_addr;
759 /* the domain mapping is empty so remove it from the mapping table */
760 netlbl_domhsh_remove_entry(entry_map, audit_info);
761
762 remove_af6_single_addr:
763 rcu_read_unlock();
764 /* yick, we can't use call_rcu here because we don't have a rcu head
765 * pointer but hopefully this should be a rare case so the pause
766 * shouldn't be a problem */
767 synchronize_rcu();
768 entry = netlbl_domhsh_addr6_entry(entry_addr);
769 calipso_doi_putdef(entry->def.calipso);
770 kfree(entry);
771 return 0;
772
773 remove_af6_failure:
774 rcu_read_unlock();
775 return -ENOENT;
776 }
777 #endif /* IPv6 */
778
779 /**
780 * netlbl_domhsh_remove - Removes an entry from the domain hash table
781 * @domain: the domain to remove
782 * @family: address family
783 * @audit_info: NetLabel audit information
784 *
785 * Description:
786 * Removes an entry from the domain hash table and handles any updates to the
787 * lower level protocol handler (i.e. CIPSO). @family may be %AF_UNSPEC which
788 * removes all address family entries. Returns zero on success, negative on
789 * failure.
790 *
791 */
netlbl_domhsh_remove(const char * domain,u16 family,struct netlbl_audit * audit_info)792 int netlbl_domhsh_remove(const char *domain, u16 family,
793 struct netlbl_audit *audit_info)
794 {
795 int ret_val = -EINVAL;
796 struct netlbl_dom_map *entry;
797
798 rcu_read_lock();
799
800 if (family == AF_INET || family == AF_UNSPEC) {
801 if (domain)
802 entry = netlbl_domhsh_search(domain, AF_INET);
803 else
804 entry = netlbl_domhsh_search_def(domain, AF_INET);
805 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
806 if (ret_val && ret_val != -ENOENT)
807 goto done;
808 }
809 if (family == AF_INET6 || family == AF_UNSPEC) {
810 int ret_val2;
811
812 if (domain)
813 entry = netlbl_domhsh_search(domain, AF_INET6);
814 else
815 entry = netlbl_domhsh_search_def(domain, AF_INET6);
816 ret_val2 = netlbl_domhsh_remove_entry(entry, audit_info);
817 if (ret_val2 != -ENOENT)
818 ret_val = ret_val2;
819 }
820 done:
821 rcu_read_unlock();
822
823 return ret_val;
824 }
825
826 /**
827 * netlbl_domhsh_remove_default - Removes the default entry from the table
828 * @family: address family
829 * @audit_info: NetLabel audit information
830 *
831 * Description:
832 * Removes/resets the default entry corresponding to @family from the domain
833 * hash table and handles any updates to the lower level protocol handler
834 * (i.e. CIPSO). @family may be %AF_UNSPEC which removes all address family
835 * entries. Returns zero on success, negative on failure.
836 *
837 */
netlbl_domhsh_remove_default(u16 family,struct netlbl_audit * audit_info)838 int netlbl_domhsh_remove_default(u16 family, struct netlbl_audit *audit_info)
839 {
840 return netlbl_domhsh_remove(NULL, family, audit_info);
841 }
842
843 /**
844 * netlbl_domhsh_getentry - Get an entry from the domain hash table
845 * @domain: the domain name to search for
846 * @family: address family
847 *
848 * Description:
849 * Look through the domain hash table searching for an entry to match @domain,
850 * with address family @family, return a pointer to a copy of the entry or
851 * NULL. The caller is responsible for ensuring that rcu_read_[un]lock() is
852 * called.
853 *
854 */
netlbl_domhsh_getentry(const char * domain,u16 family)855 struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain, u16 family)
856 {
857 if (family == AF_UNSPEC)
858 return NULL;
859 return netlbl_domhsh_search_def(domain, family);
860 }
861
862 /**
863 * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
864 * @domain: the domain name to search for
865 * @addr: the IP address to search for
866 *
867 * Description:
868 * Look through the domain hash table searching for an entry to match @domain
869 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
870 * responsible for ensuring that rcu_read_[un]lock() is called.
871 *
872 */
netlbl_domhsh_getentry_af4(const char * domain,__be32 addr)873 struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
874 __be32 addr)
875 {
876 struct netlbl_dom_map *dom_iter;
877 struct netlbl_af4list *addr_iter;
878
879 dom_iter = netlbl_domhsh_search_def(domain, AF_INET);
880 if (dom_iter == NULL)
881 return NULL;
882
883 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
884 return &dom_iter->def;
885 addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
886 if (addr_iter == NULL)
887 return NULL;
888 return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
889 }
890
891 #if IS_ENABLED(CONFIG_IPV6)
892 /**
893 * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
894 * @domain: the domain name to search for
895 * @addr: the IP address to search for
896 *
897 * Description:
898 * Look through the domain hash table searching for an entry to match @domain
899 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
900 * responsible for ensuring that rcu_read_[un]lock() is called.
901 *
902 */
netlbl_domhsh_getentry_af6(const char * domain,const struct in6_addr * addr)903 struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
904 const struct in6_addr *addr)
905 {
906 struct netlbl_dom_map *dom_iter;
907 struct netlbl_af6list *addr_iter;
908
909 dom_iter = netlbl_domhsh_search_def(domain, AF_INET6);
910 if (dom_iter == NULL)
911 return NULL;
912
913 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
914 return &dom_iter->def;
915 addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
916 if (addr_iter == NULL)
917 return NULL;
918 return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
919 }
920 #endif /* IPv6 */
921
922 /**
923 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
924 * @skip_bkt: the number of buckets to skip at the start
925 * @skip_chain: the number of entries to skip in the first iterated bucket
926 * @callback: callback for each entry
927 * @cb_arg: argument for the callback function
928 *
929 * Description:
930 * Iterate over the domain mapping hash table, skipping the first @skip_bkt
931 * buckets and @skip_chain entries. For each entry in the table call
932 * @callback, if @callback returns a negative value stop 'walking' through the
933 * table and return. Updates the values in @skip_bkt and @skip_chain on
934 * return. Returns zero on success, negative values on failure.
935 *
936 */
netlbl_domhsh_walk(u32 * skip_bkt,u32 * skip_chain,int (* callback)(struct netlbl_dom_map * entry,void * arg),void * cb_arg)937 int netlbl_domhsh_walk(u32 *skip_bkt,
938 u32 *skip_chain,
939 int (*callback) (struct netlbl_dom_map *entry, void *arg),
940 void *cb_arg)
941 {
942 int ret_val = -ENOENT;
943 u32 iter_bkt;
944 struct list_head *iter_list;
945 struct netlbl_dom_map *iter_entry;
946 u32 chain_cnt = 0;
947
948 rcu_read_lock();
949 for (iter_bkt = *skip_bkt;
950 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
951 iter_bkt++, chain_cnt = 0) {
952 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
953 list_for_each_entry_rcu(iter_entry, iter_list, list)
954 if (iter_entry->valid) {
955 if (chain_cnt++ < *skip_chain)
956 continue;
957 ret_val = callback(iter_entry, cb_arg);
958 if (ret_val < 0) {
959 chain_cnt--;
960 goto walk_return;
961 }
962 }
963 }
964
965 walk_return:
966 rcu_read_unlock();
967 *skip_bkt = iter_bkt;
968 *skip_chain = chain_cnt;
969 return ret_val;
970 }
971