1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/types.h>
32#include <linux/rculist.h>
33#include <linux/skbuff.h>
34#include <linux/spinlock.h>
35#include <linux/string.h>
36#include <linux/audit.h>
37#include <linux/slab.h>
38#include <net/netlabel.h>
39#include <net/cipso_ipv4.h>
40#include <asm/bug.h>
41
42#include "netlabel_mgmt.h"
43#include "netlabel_addrlist.h"
44#include "netlabel_domainhash.h"
45#include "netlabel_user.h"
46
47struct netlbl_domhsh_tbl {
48 struct list_head *tbl;
49 u32 size;
50};
51
52
53
54
55static DEFINE_SPINLOCK(netlbl_domhsh_lock);
56#define netlbl_domhsh_rcu_deref(p) \
57 rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
58static struct netlbl_domhsh_tbl *netlbl_domhsh;
59static struct netlbl_dom_map *netlbl_domhsh_def;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75static void netlbl_domhsh_free_entry(struct rcu_head *entry)
76{
77 struct netlbl_dom_map *ptr;
78 struct netlbl_af4list *iter4;
79 struct netlbl_af4list *tmp4;
80#if IS_ENABLED(CONFIG_IPV6)
81 struct netlbl_af6list *iter6;
82 struct netlbl_af6list *tmp6;
83#endif
84
85 ptr = container_of(entry, struct netlbl_dom_map, rcu);
86 if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
87 netlbl_af4list_foreach_safe(iter4, tmp4,
88 &ptr->def.addrsel->list4) {
89 netlbl_af4list_remove_entry(iter4);
90 kfree(netlbl_domhsh_addr4_entry(iter4));
91 }
92#if IS_ENABLED(CONFIG_IPV6)
93 netlbl_af6list_foreach_safe(iter6, tmp6,
94 &ptr->def.addrsel->list6) {
95 netlbl_af6list_remove_entry(iter6);
96 kfree(netlbl_domhsh_addr6_entry(iter6));
97 }
98#endif
99 }
100 kfree(ptr->domain);
101 kfree(ptr);
102}
103
104
105
106
107
108
109
110
111
112
113
114
115static u32 netlbl_domhsh_hash(const char *key)
116{
117 u32 iter;
118 u32 val;
119 u32 len;
120
121
122
123
124 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
125 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
126 return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
127}
128
129
130
131
132
133
134
135
136
137
138
139
140static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
141{
142 u32 bkt;
143 struct list_head *bkt_list;
144 struct netlbl_dom_map *iter;
145
146 if (domain != NULL) {
147 bkt = netlbl_domhsh_hash(domain);
148 bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
149 list_for_each_entry_rcu(iter, bkt_list, list)
150 if (iter->valid && strcmp(iter->domain, domain) == 0)
151 return iter;
152 }
153
154 return NULL;
155}
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
171{
172 struct netlbl_dom_map *entry;
173
174 entry = netlbl_domhsh_search(domain);
175 if (entry == NULL) {
176 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def);
177 if (entry != NULL && !entry->valid)
178 entry = NULL;
179 }
180
181 return entry;
182}
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
199 struct netlbl_af4list *addr4,
200 struct netlbl_af6list *addr6,
201 int result,
202 struct netlbl_audit *audit_info)
203{
204 struct audit_buffer *audit_buf;
205 struct cipso_v4_doi *cipsov4 = NULL;
206 u32 type;
207
208 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
209 if (audit_buf != NULL) {
210 audit_log_format(audit_buf, " nlbl_domain=%s",
211 entry->domain ? entry->domain : "(default)");
212 if (addr4 != NULL) {
213 struct netlbl_domaddr4_map *map4;
214 map4 = netlbl_domhsh_addr4_entry(addr4);
215 type = map4->def.type;
216 cipsov4 = map4->def.cipso;
217 netlbl_af4list_audit_addr(audit_buf, 0, NULL,
218 addr4->addr, addr4->mask);
219#if IS_ENABLED(CONFIG_IPV6)
220 } else if (addr6 != NULL) {
221 struct netlbl_domaddr6_map *map6;
222 map6 = netlbl_domhsh_addr6_entry(addr6);
223 type = map6->def.type;
224 netlbl_af6list_audit_addr(audit_buf, 0, NULL,
225 &addr6->addr, &addr6->mask);
226#endif
227 } else {
228 type = entry->def.type;
229 cipsov4 = entry->def.cipso;
230 }
231 switch (type) {
232 case NETLBL_NLTYPE_UNLABELED:
233 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
234 break;
235 case NETLBL_NLTYPE_CIPSOV4:
236 BUG_ON(cipsov4 == NULL);
237 audit_log_format(audit_buf,
238 " nlbl_protocol=cipsov4 cipso_doi=%u",
239 cipsov4->doi);
240 break;
241 }
242 audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
243 audit_log_end(audit_buf);
244 }
245}
246
247
248
249
250
251
252
253
254
255static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
256{
257 struct netlbl_af4list *iter4;
258 struct netlbl_domaddr4_map *map4;
259#if IS_ENABLED(CONFIG_IPV6)
260 struct netlbl_af6list *iter6;
261 struct netlbl_domaddr6_map *map6;
262#endif
263
264 if (entry == NULL)
265 return -EINVAL;
266
267 switch (entry->def.type) {
268 case NETLBL_NLTYPE_UNLABELED:
269 if (entry->def.cipso != NULL || entry->def.addrsel != NULL)
270 return -EINVAL;
271 break;
272 case NETLBL_NLTYPE_CIPSOV4:
273 if (entry->def.cipso == NULL)
274 return -EINVAL;
275 break;
276 case NETLBL_NLTYPE_ADDRSELECT:
277 netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
278 map4 = netlbl_domhsh_addr4_entry(iter4);
279 switch (map4->def.type) {
280 case NETLBL_NLTYPE_UNLABELED:
281 if (map4->def.cipso != NULL)
282 return -EINVAL;
283 break;
284 case NETLBL_NLTYPE_CIPSOV4:
285 if (map4->def.cipso == NULL)
286 return -EINVAL;
287 break;
288 default:
289 return -EINVAL;
290 }
291 }
292#if IS_ENABLED(CONFIG_IPV6)
293 netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
294 map6 = netlbl_domhsh_addr6_entry(iter6);
295 switch (map6->def.type) {
296 case NETLBL_NLTYPE_UNLABELED:
297 break;
298 default:
299 return -EINVAL;
300 }
301 }
302#endif
303 break;
304 default:
305 return -EINVAL;
306 }
307
308 return 0;
309}
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325int __init netlbl_domhsh_init(u32 size)
326{
327 u32 iter;
328 struct netlbl_domhsh_tbl *hsh_tbl;
329
330 if (size == 0)
331 return -EINVAL;
332
333 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
334 if (hsh_tbl == NULL)
335 return -ENOMEM;
336 hsh_tbl->size = 1 << size;
337 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
338 sizeof(struct list_head),
339 GFP_KERNEL);
340 if (hsh_tbl->tbl == NULL) {
341 kfree(hsh_tbl);
342 return -ENOMEM;
343 }
344 for (iter = 0; iter < hsh_tbl->size; iter++)
345 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
346
347 spin_lock(&netlbl_domhsh_lock);
348 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
349 spin_unlock(&netlbl_domhsh_lock);
350
351 return 0;
352}
353
354
355
356
357
358
359
360
361
362
363
364
365int netlbl_domhsh_add(struct netlbl_dom_map *entry,
366 struct netlbl_audit *audit_info)
367{
368 int ret_val = 0;
369 struct netlbl_dom_map *entry_old;
370 struct netlbl_af4list *iter4;
371 struct netlbl_af4list *tmp4;
372#if IS_ENABLED(CONFIG_IPV6)
373 struct netlbl_af6list *iter6;
374 struct netlbl_af6list *tmp6;
375#endif
376
377 ret_val = netlbl_domhsh_validate(entry);
378 if (ret_val != 0)
379 return ret_val;
380
381
382
383
384
385 rcu_read_lock();
386 spin_lock(&netlbl_domhsh_lock);
387 if (entry->domain != NULL)
388 entry_old = netlbl_domhsh_search(entry->domain);
389 else
390 entry_old = netlbl_domhsh_search_def(entry->domain);
391 if (entry_old == NULL) {
392 entry->valid = 1;
393
394 if (entry->domain != NULL) {
395 u32 bkt = netlbl_domhsh_hash(entry->domain);
396 list_add_tail_rcu(&entry->list,
397 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
398 } else {
399 INIT_LIST_HEAD(&entry->list);
400 rcu_assign_pointer(netlbl_domhsh_def, entry);
401 }
402
403 if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
404 netlbl_af4list_foreach_rcu(iter4,
405 &entry->def.addrsel->list4)
406 netlbl_domhsh_audit_add(entry, iter4, NULL,
407 ret_val, audit_info);
408#if IS_ENABLED(CONFIG_IPV6)
409 netlbl_af6list_foreach_rcu(iter6,
410 &entry->def.addrsel->list6)
411 netlbl_domhsh_audit_add(entry, NULL, iter6,
412 ret_val, audit_info);
413#endif
414 } else
415 netlbl_domhsh_audit_add(entry, NULL, NULL,
416 ret_val, audit_info);
417 } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
418 entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
419 struct list_head *old_list4;
420 struct list_head *old_list6;
421
422 old_list4 = &entry_old->def.addrsel->list4;
423 old_list6 = &entry_old->def.addrsel->list6;
424
425
426
427 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
428 if (netlbl_af4list_search_exact(iter4->addr,
429 iter4->mask,
430 old_list4)) {
431 ret_val = -EEXIST;
432 goto add_return;
433 }
434#if IS_ENABLED(CONFIG_IPV6)
435 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
436 if (netlbl_af6list_search_exact(&iter6->addr,
437 &iter6->mask,
438 old_list6)) {
439 ret_val = -EEXIST;
440 goto add_return;
441 }
442#endif
443
444 netlbl_af4list_foreach_safe(iter4, tmp4,
445 &entry->def.addrsel->list4) {
446 netlbl_af4list_remove_entry(iter4);
447 iter4->valid = 1;
448 ret_val = netlbl_af4list_add(iter4, old_list4);
449 netlbl_domhsh_audit_add(entry_old, iter4, NULL,
450 ret_val, audit_info);
451 if (ret_val != 0)
452 goto add_return;
453 }
454#if IS_ENABLED(CONFIG_IPV6)
455 netlbl_af6list_foreach_safe(iter6, tmp6,
456 &entry->def.addrsel->list6) {
457 netlbl_af6list_remove_entry(iter6);
458 iter6->valid = 1;
459 ret_val = netlbl_af6list_add(iter6, old_list6);
460 netlbl_domhsh_audit_add(entry_old, NULL, iter6,
461 ret_val, audit_info);
462 if (ret_val != 0)
463 goto add_return;
464 }
465#endif
466 } else
467 ret_val = -EINVAL;
468
469add_return:
470 spin_unlock(&netlbl_domhsh_lock);
471 rcu_read_unlock();
472 return ret_val;
473}
474
475
476
477
478
479
480
481
482
483
484
485
486int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
487 struct netlbl_audit *audit_info)
488{
489 return netlbl_domhsh_add(entry, audit_info);
490}
491
492
493
494
495
496
497
498
499
500
501
502
503
504int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
505 struct netlbl_audit *audit_info)
506{
507 int ret_val = 0;
508 struct audit_buffer *audit_buf;
509
510 if (entry == NULL)
511 return -ENOENT;
512
513 spin_lock(&netlbl_domhsh_lock);
514 if (entry->valid) {
515 entry->valid = 0;
516 if (entry != rcu_dereference(netlbl_domhsh_def))
517 list_del_rcu(&entry->list);
518 else
519 RCU_INIT_POINTER(netlbl_domhsh_def, NULL);
520 } else
521 ret_val = -ENOENT;
522 spin_unlock(&netlbl_domhsh_lock);
523
524 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
525 if (audit_buf != NULL) {
526 audit_log_format(audit_buf,
527 " nlbl_domain=%s res=%u",
528 entry->domain ? entry->domain : "(default)",
529 ret_val == 0 ? 1 : 0);
530 audit_log_end(audit_buf);
531 }
532
533 if (ret_val == 0) {
534 struct netlbl_af4list *iter4;
535 struct netlbl_domaddr4_map *map4;
536
537 switch (entry->def.type) {
538 case NETLBL_NLTYPE_ADDRSELECT:
539 netlbl_af4list_foreach_rcu(iter4,
540 &entry->def.addrsel->list4) {
541 map4 = netlbl_domhsh_addr4_entry(iter4);
542 cipso_v4_doi_putdef(map4->def.cipso);
543 }
544
545
546 break;
547 case NETLBL_NLTYPE_CIPSOV4:
548 cipso_v4_doi_putdef(entry->def.cipso);
549 break;
550 }
551 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
552 }
553
554 return ret_val;
555}
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570int netlbl_domhsh_remove_af4(const char *domain,
571 const struct in_addr *addr,
572 const struct in_addr *mask,
573 struct netlbl_audit *audit_info)
574{
575 struct netlbl_dom_map *entry_map;
576 struct netlbl_af4list *entry_addr;
577 struct netlbl_af4list *iter4;
578#if IS_ENABLED(CONFIG_IPV6)
579 struct netlbl_af6list *iter6;
580#endif
581 struct netlbl_domaddr4_map *entry;
582
583 rcu_read_lock();
584
585 if (domain)
586 entry_map = netlbl_domhsh_search(domain);
587 else
588 entry_map = netlbl_domhsh_search_def(domain);
589 if (entry_map == NULL ||
590 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
591 goto remove_af4_failure;
592
593 spin_lock(&netlbl_domhsh_lock);
594 entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
595 &entry_map->def.addrsel->list4);
596 spin_unlock(&netlbl_domhsh_lock);
597
598 if (entry_addr == NULL)
599 goto remove_af4_failure;
600 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
601 goto remove_af4_single_addr;
602#if IS_ENABLED(CONFIG_IPV6)
603 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
604 goto remove_af4_single_addr;
605#endif
606
607 netlbl_domhsh_remove_entry(entry_map, audit_info);
608
609remove_af4_single_addr:
610 rcu_read_unlock();
611
612
613
614 synchronize_rcu();
615 entry = netlbl_domhsh_addr4_entry(entry_addr);
616 cipso_v4_doi_putdef(entry->def.cipso);
617 kfree(entry);
618 return 0;
619
620remove_af4_failure:
621 rcu_read_unlock();
622 return -ENOENT;
623}
624
625
626
627
628
629
630
631
632
633
634
635
636int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
637{
638 int ret_val;
639 struct netlbl_dom_map *entry;
640
641 rcu_read_lock();
642 if (domain)
643 entry = netlbl_domhsh_search(domain);
644 else
645 entry = netlbl_domhsh_search_def(domain);
646 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
647 rcu_read_unlock();
648
649 return ret_val;
650}
651
652
653
654
655
656
657
658
659
660
661
662int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
663{
664 return netlbl_domhsh_remove(NULL, audit_info);
665}
666
667
668
669
670
671
672
673
674
675
676
677struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
678{
679 return netlbl_domhsh_search_def(domain);
680}
681
682
683
684
685
686
687
688
689
690
691
692
693struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
694 __be32 addr)
695{
696 struct netlbl_dom_map *dom_iter;
697 struct netlbl_af4list *addr_iter;
698
699 dom_iter = netlbl_domhsh_search_def(domain);
700 if (dom_iter == NULL)
701 return NULL;
702
703 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
704 return &dom_iter->def;
705 addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
706 if (addr_iter == NULL)
707 return NULL;
708 return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
709}
710
711#if IS_ENABLED(CONFIG_IPV6)
712
713
714
715
716
717
718
719
720
721
722
723struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
724 const struct in6_addr *addr)
725{
726 struct netlbl_dom_map *dom_iter;
727 struct netlbl_af6list *addr_iter;
728
729 dom_iter = netlbl_domhsh_search_def(domain);
730 if (dom_iter == NULL)
731 return NULL;
732
733 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
734 return &dom_iter->def;
735 addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
736 if (addr_iter == NULL)
737 return NULL;
738 return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
739}
740#endif
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757int netlbl_domhsh_walk(u32 *skip_bkt,
758 u32 *skip_chain,
759 int (*callback) (struct netlbl_dom_map *entry, void *arg),
760 void *cb_arg)
761{
762 int ret_val = -ENOENT;
763 u32 iter_bkt;
764 struct list_head *iter_list;
765 struct netlbl_dom_map *iter_entry;
766 u32 chain_cnt = 0;
767
768 rcu_read_lock();
769 for (iter_bkt = *skip_bkt;
770 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
771 iter_bkt++, chain_cnt = 0) {
772 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
773 list_for_each_entry_rcu(iter_entry, iter_list, list)
774 if (iter_entry->valid) {
775 if (chain_cnt++ < *skip_chain)
776 continue;
777 ret_val = callback(iter_entry, cb_arg);
778 if (ret_val < 0) {
779 chain_cnt--;
780 goto walk_return;
781 }
782 }
783 }
784
785walk_return:
786 rcu_read_unlock();
787 *skip_bkt = iter_bkt;
788 *skip_chain = chain_cnt;
789 return ret_val;
790}
791