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#include <linux/init.h>
31#include <linux/types.h>
32#include <linux/rcupdate.h>
33#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/string.h>
36#include <linux/jhash.h>
37#include <linux/audit.h>
38#include <linux/slab.h>
39#include <net/ip.h>
40#include <net/icmp.h>
41#include <net/tcp.h>
42#include <net/netlabel.h>
43#include <net/calipso.h>
44#include <linux/atomic.h>
45#include <linux/bug.h>
46#include <asm/unaligned.h>
47#include <linux/crc-ccitt.h>
48
49
50
51
52#define CALIPSO_OPT_LEN_MAX (2 + 252)
53
54
55
56
57#define CALIPSO_HDR_LEN (2 + 8)
58
59
60
61
62
63#define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
64
65
66
67
68
69#define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
70
71
72static DEFINE_SPINLOCK(calipso_doi_list_lock);
73static LIST_HEAD(calipso_doi_list);
74
75
76int calipso_cache_enabled = 1;
77int calipso_cache_bucketsize = 10;
78#define CALIPSO_CACHE_BUCKETBITS 7
79#define CALIPSO_CACHE_BUCKETS BIT(CALIPSO_CACHE_BUCKETBITS)
80#define CALIPSO_CACHE_REORDERLIMIT 10
81struct calipso_map_cache_bkt {
82 spinlock_t lock;
83 u32 size;
84 struct list_head list;
85};
86
87struct calipso_map_cache_entry {
88 u32 hash;
89 unsigned char *key;
90 size_t key_len;
91
92 struct netlbl_lsm_cache *lsm_data;
93
94 u32 activity;
95 struct list_head list;
96};
97
98static struct calipso_map_cache_bkt *calipso_cache;
99
100
101
102
103
104
105
106
107
108
109
110
111
112static void calipso_cache_entry_free(struct calipso_map_cache_entry *entry)
113{
114 if (entry->lsm_data)
115 netlbl_secattr_cache_free(entry->lsm_data);
116 kfree(entry->key);
117 kfree(entry);
118}
119
120
121
122
123
124
125
126
127
128
129static u32 calipso_map_cache_hash(const unsigned char *key, u32 key_len)
130{
131 return jhash(key, key_len, 0);
132}
133
134
135
136
137
138
139
140
141
142
143static int __init calipso_cache_init(void)
144{
145 u32 iter;
146
147 calipso_cache = kcalloc(CALIPSO_CACHE_BUCKETS,
148 sizeof(struct calipso_map_cache_bkt),
149 GFP_KERNEL);
150 if (!calipso_cache)
151 return -ENOMEM;
152
153 for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
154 spin_lock_init(&calipso_cache[iter].lock);
155 calipso_cache[iter].size = 0;
156 INIT_LIST_HEAD(&calipso_cache[iter].list);
157 }
158
159 return 0;
160}
161
162
163
164
165
166
167
168
169
170static void calipso_cache_invalidate(void)
171{
172 struct calipso_map_cache_entry *entry, *tmp_entry;
173 u32 iter;
174
175 for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
176 spin_lock_bh(&calipso_cache[iter].lock);
177 list_for_each_entry_safe(entry,
178 tmp_entry,
179 &calipso_cache[iter].list, list) {
180 list_del(&entry->list);
181 calipso_cache_entry_free(entry);
182 }
183 calipso_cache[iter].size = 0;
184 spin_unlock_bh(&calipso_cache[iter].lock);
185 }
186}
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210static int calipso_cache_check(const unsigned char *key,
211 u32 key_len,
212 struct netlbl_lsm_secattr *secattr)
213{
214 u32 bkt;
215 struct calipso_map_cache_entry *entry;
216 struct calipso_map_cache_entry *prev_entry = NULL;
217 u32 hash;
218
219 if (!calipso_cache_enabled)
220 return -ENOENT;
221
222 hash = calipso_map_cache_hash(key, key_len);
223 bkt = hash & (CALIPSO_CACHE_BUCKETS - 1);
224 spin_lock_bh(&calipso_cache[bkt].lock);
225 list_for_each_entry(entry, &calipso_cache[bkt].list, list) {
226 if (entry->hash == hash &&
227 entry->key_len == key_len &&
228 memcmp(entry->key, key, key_len) == 0) {
229 entry->activity += 1;
230 refcount_inc(&entry->lsm_data->refcount);
231 secattr->cache = entry->lsm_data;
232 secattr->flags |= NETLBL_SECATTR_CACHE;
233 secattr->type = NETLBL_NLTYPE_CALIPSO;
234 if (!prev_entry) {
235 spin_unlock_bh(&calipso_cache[bkt].lock);
236 return 0;
237 }
238
239 if (prev_entry->activity > 0)
240 prev_entry->activity -= 1;
241 if (entry->activity > prev_entry->activity &&
242 entry->activity - prev_entry->activity >
243 CALIPSO_CACHE_REORDERLIMIT) {
244 __list_del(entry->list.prev, entry->list.next);
245 __list_add(&entry->list,
246 prev_entry->list.prev,
247 &prev_entry->list);
248 }
249
250 spin_unlock_bh(&calipso_cache[bkt].lock);
251 return 0;
252 }
253 prev_entry = entry;
254 }
255 spin_unlock_bh(&calipso_cache[bkt].lock);
256
257 return -ENOENT;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275static int calipso_cache_add(const unsigned char *calipso_ptr,
276 const struct netlbl_lsm_secattr *secattr)
277{
278 int ret_val = -EPERM;
279 u32 bkt;
280 struct calipso_map_cache_entry *entry = NULL;
281 struct calipso_map_cache_entry *old_entry = NULL;
282 u32 calipso_ptr_len;
283
284 if (!calipso_cache_enabled || calipso_cache_bucketsize <= 0)
285 return 0;
286
287 calipso_ptr_len = calipso_ptr[1];
288
289 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
290 if (!entry)
291 return -ENOMEM;
292 entry->key = kmemdup(calipso_ptr + 2, calipso_ptr_len, GFP_ATOMIC);
293 if (!entry->key) {
294 ret_val = -ENOMEM;
295 goto cache_add_failure;
296 }
297 entry->key_len = calipso_ptr_len;
298 entry->hash = calipso_map_cache_hash(calipso_ptr, calipso_ptr_len);
299 refcount_inc(&secattr->cache->refcount);
300 entry->lsm_data = secattr->cache;
301
302 bkt = entry->hash & (CALIPSO_CACHE_BUCKETS - 1);
303 spin_lock_bh(&calipso_cache[bkt].lock);
304 if (calipso_cache[bkt].size < calipso_cache_bucketsize) {
305 list_add(&entry->list, &calipso_cache[bkt].list);
306 calipso_cache[bkt].size += 1;
307 } else {
308 old_entry = list_entry(calipso_cache[bkt].list.prev,
309 struct calipso_map_cache_entry, list);
310 list_del(&old_entry->list);
311 list_add(&entry->list, &calipso_cache[bkt].list);
312 calipso_cache_entry_free(old_entry);
313 }
314 spin_unlock_bh(&calipso_cache[bkt].lock);
315
316 return 0;
317
318cache_add_failure:
319 if (entry)
320 calipso_cache_entry_free(entry);
321 return ret_val;
322}
323
324
325
326
327
328
329
330
331
332
333
334
335
336static struct calipso_doi *calipso_doi_search(u32 doi)
337{
338 struct calipso_doi *iter;
339
340 list_for_each_entry_rcu(iter, &calipso_doi_list, list)
341 if (iter->doi == doi && refcount_read(&iter->refcount))
342 return iter;
343 return NULL;
344}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359static int calipso_doi_add(struct calipso_doi *doi_def,
360 struct netlbl_audit *audit_info)
361{
362 int ret_val = -EINVAL;
363 u32 doi;
364 u32 doi_type;
365 struct audit_buffer *audit_buf;
366
367 doi = doi_def->doi;
368 doi_type = doi_def->type;
369
370 if (doi_def->doi == CALIPSO_DOI_UNKNOWN)
371 goto doi_add_return;
372
373 refcount_set(&doi_def->refcount, 1);
374
375 spin_lock(&calipso_doi_list_lock);
376 if (calipso_doi_search(doi_def->doi)) {
377 spin_unlock(&calipso_doi_list_lock);
378 ret_val = -EEXIST;
379 goto doi_add_return;
380 }
381 list_add_tail_rcu(&doi_def->list, &calipso_doi_list);
382 spin_unlock(&calipso_doi_list_lock);
383 ret_val = 0;
384
385doi_add_return:
386 audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD, audit_info);
387 if (audit_buf) {
388 const char *type_str;
389
390 switch (doi_type) {
391 case CALIPSO_MAP_PASS:
392 type_str = "pass";
393 break;
394 default:
395 type_str = "(unknown)";
396 }
397 audit_log_format(audit_buf,
398 " calipso_doi=%u calipso_type=%s res=%u",
399 doi, type_str, ret_val == 0 ? 1 : 0);
400 audit_log_end(audit_buf);
401 }
402
403 return ret_val;
404}
405
406
407
408
409
410
411
412
413
414static void calipso_doi_free(struct calipso_doi *doi_def)
415{
416 kfree(doi_def);
417}
418
419
420
421
422
423
424
425
426
427
428
429static void calipso_doi_free_rcu(struct rcu_head *entry)
430{
431 struct calipso_doi *doi_def;
432
433 doi_def = container_of(entry, struct calipso_doi, rcu);
434 calipso_doi_free(doi_def);
435}
436
437
438
439
440
441
442
443
444
445
446
447
448static int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
449{
450 int ret_val;
451 struct calipso_doi *doi_def;
452 struct audit_buffer *audit_buf;
453
454 spin_lock(&calipso_doi_list_lock);
455 doi_def = calipso_doi_search(doi);
456 if (!doi_def) {
457 spin_unlock(&calipso_doi_list_lock);
458 ret_val = -ENOENT;
459 goto doi_remove_return;
460 }
461 if (!refcount_dec_and_test(&doi_def->refcount)) {
462 spin_unlock(&calipso_doi_list_lock);
463 ret_val = -EBUSY;
464 goto doi_remove_return;
465 }
466 list_del_rcu(&doi_def->list);
467 spin_unlock(&calipso_doi_list_lock);
468
469 call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
470 ret_val = 0;
471
472doi_remove_return:
473 audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_DEL, audit_info);
474 if (audit_buf) {
475 audit_log_format(audit_buf,
476 " calipso_doi=%u res=%u",
477 doi, ret_val == 0 ? 1 : 0);
478 audit_log_end(audit_buf);
479 }
480
481 return ret_val;
482}
483
484
485
486
487
488
489
490
491
492
493
494static struct calipso_doi *calipso_doi_getdef(u32 doi)
495{
496 struct calipso_doi *doi_def;
497
498 rcu_read_lock();
499 doi_def = calipso_doi_search(doi);
500 if (!doi_def)
501 goto doi_getdef_return;
502 if (!refcount_inc_not_zero(&doi_def->refcount))
503 doi_def = NULL;
504
505doi_getdef_return:
506 rcu_read_unlock();
507 return doi_def;
508}
509
510
511
512
513
514
515
516
517
518static void calipso_doi_putdef(struct calipso_doi *doi_def)
519{
520 if (!doi_def)
521 return;
522
523 if (!refcount_dec_and_test(&doi_def->refcount))
524 return;
525 spin_lock(&calipso_doi_list_lock);
526 list_del_rcu(&doi_def->list);
527 spin_unlock(&calipso_doi_list_lock);
528
529 call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
530}
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545static int calipso_doi_walk(u32 *skip_cnt,
546 int (*callback)(struct calipso_doi *doi_def,
547 void *arg),
548 void *cb_arg)
549{
550 int ret_val = -ENOENT;
551 u32 doi_cnt = 0;
552 struct calipso_doi *iter_doi;
553
554 rcu_read_lock();
555 list_for_each_entry_rcu(iter_doi, &calipso_doi_list, list)
556 if (refcount_read(&iter_doi->refcount) > 0) {
557 if (doi_cnt++ < *skip_cnt)
558 continue;
559 ret_val = callback(iter_doi, cb_arg);
560 if (ret_val < 0) {
561 doi_cnt--;
562 goto doi_walk_return;
563 }
564 }
565
566doi_walk_return:
567 rcu_read_unlock();
568 *skip_cnt = doi_cnt;
569 return ret_val;
570}
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588bool calipso_validate(const struct sk_buff *skb, const unsigned char *option)
589{
590 struct calipso_doi *doi_def;
591 bool ret_val;
592 u16 crc, len = option[1] + 2;
593 static const u8 zero[2];
594
595
596
597 crc = crc_ccitt(0xffff, option, 8);
598 crc = crc_ccitt(crc, zero, sizeof(zero));
599 if (len > 10)
600 crc = crc_ccitt(crc, option + 10, len - 10);
601 crc = ~crc;
602 if (option[8] != (crc & 0xff) || option[9] != ((crc >> 8) & 0xff))
603 return false;
604
605 rcu_read_lock();
606 doi_def = calipso_doi_search(get_unaligned_be32(option + 2));
607 ret_val = !!doi_def;
608 rcu_read_unlock();
609
610 return ret_val;
611}
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626static int calipso_map_cat_hton(const struct calipso_doi *doi_def,
627 const struct netlbl_lsm_secattr *secattr,
628 unsigned char *net_cat,
629 u32 net_cat_len)
630{
631 int spot = -1;
632 u32 net_spot_max = 0;
633 u32 net_clen_bits = net_cat_len * 8;
634
635 for (;;) {
636 spot = netlbl_catmap_walk(secattr->attr.mls.cat,
637 spot + 1);
638 if (spot < 0)
639 break;
640 if (spot >= net_clen_bits)
641 return -ENOSPC;
642 netlbl_bitmap_setbit(net_cat, spot, 1);
643
644 if (spot > net_spot_max)
645 net_spot_max = spot;
646 }
647
648 return (net_spot_max / 32 + 1) * 4;
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664static int calipso_map_cat_ntoh(const struct calipso_doi *doi_def,
665 const unsigned char *net_cat,
666 u32 net_cat_len,
667 struct netlbl_lsm_secattr *secattr)
668{
669 int ret_val;
670 int spot = -1;
671 u32 net_clen_bits = net_cat_len * 8;
672
673 for (;;) {
674 spot = netlbl_bitmap_walk(net_cat,
675 net_clen_bits,
676 spot + 1,
677 1);
678 if (spot < 0) {
679 if (spot == -2)
680 return -EFAULT;
681 return 0;
682 }
683
684 ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
685 spot,
686 GFP_ATOMIC);
687 if (ret_val != 0)
688 return ret_val;
689 }
690
691 return -EINVAL;
692}
693
694
695
696
697
698
699
700
701
702
703
704
705static int calipso_pad_write(unsigned char *buf, unsigned int offset,
706 unsigned int count)
707{
708 if (WARN_ON_ONCE(count >= 8))
709 return -EINVAL;
710
711 switch (count) {
712 case 0:
713 break;
714 case 1:
715 buf[offset] = IPV6_TLV_PAD1;
716 break;
717 default:
718 buf[offset] = IPV6_TLV_PADN;
719 buf[offset + 1] = count - 2;
720 if (count > 2)
721 memset(buf + offset + 2, 0, count - 2);
722 break;
723 }
724 return 0;
725}
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741static int calipso_genopt(unsigned char *buf, u32 start, u32 buf_len,
742 const struct calipso_doi *doi_def,
743 const struct netlbl_lsm_secattr *secattr)
744{
745 int ret_val;
746 u32 len, pad;
747 u16 crc;
748 static const unsigned char padding[4] = {2, 1, 0, 3};
749 unsigned char *calipso;
750
751
752 pad = padding[start & 3];
753 if (buf_len <= start + pad + CALIPSO_HDR_LEN)
754 return -ENOSPC;
755
756 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
757 return -EPERM;
758
759 len = CALIPSO_HDR_LEN;
760
761 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
762 ret_val = calipso_map_cat_hton(doi_def,
763 secattr,
764 buf + start + pad + len,
765 buf_len - start - pad - len);
766 if (ret_val < 0)
767 return ret_val;
768 len += ret_val;
769 }
770
771 calipso_pad_write(buf, start, pad);
772 calipso = buf + start + pad;
773
774 calipso[0] = IPV6_TLV_CALIPSO;
775 calipso[1] = len - 2;
776 *(__be32 *)(calipso + 2) = htonl(doi_def->doi);
777 calipso[6] = (len - CALIPSO_HDR_LEN) / 4;
778 calipso[7] = secattr->attr.mls.lvl,
779 crc = ~crc_ccitt(0xffff, calipso, len);
780 calipso[8] = crc & 0xff;
781 calipso[9] = (crc >> 8) & 0xff;
782 return pad + len;
783}
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798static int calipso_opt_update(struct sock *sk, struct ipv6_opt_hdr *hop)
799{
800 struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)), *txopts;
801
802 txopts = ipv6_renew_options(sk, old, IPV6_HOPOPTS, hop);
803 txopt_put(old);
804 if (IS_ERR(txopts))
805 return PTR_ERR(txopts);
806
807 txopts = ipv6_update_options(sk, txopts);
808 if (txopts) {
809 atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
810 txopt_put(txopts);
811 }
812
813 return 0;
814}
815
816
817
818
819
820
821
822
823
824
825
826static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
827{
828 unsigned char *tlv = (unsigned char *)opt;
829 unsigned int opt_len = ipv6_optlen(opt), tlv_len;
830
831 if (offset < sizeof(*opt) || offset >= opt_len)
832 return -EINVAL;
833 if (tlv[offset] == IPV6_TLV_PAD1)
834 return 1;
835 if (offset + 1 >= opt_len)
836 return -EINVAL;
837 tlv_len = tlv[offset + 1] + 2;
838 if (offset + tlv_len > opt_len)
839 return -EINVAL;
840 return tlv_len;
841}
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864static int calipso_opt_find(struct ipv6_opt_hdr *hop, unsigned int *start,
865 unsigned int *end)
866{
867 int ret_val = -ENOENT, tlv_len;
868 unsigned int opt_len, offset, offset_s = 0, offset_e = 0;
869 unsigned char *opt = (unsigned char *)hop;
870
871 opt_len = ipv6_optlen(hop);
872 offset = sizeof(*hop);
873
874 while (offset < opt_len) {
875 tlv_len = calipso_tlv_len(hop, offset);
876 if (tlv_len < 0)
877 return tlv_len;
878
879 switch (opt[offset]) {
880 case IPV6_TLV_PAD1:
881 case IPV6_TLV_PADN:
882 if (offset_e)
883 offset_e = offset;
884 break;
885 case IPV6_TLV_CALIPSO:
886 ret_val = 0;
887 offset_e = offset;
888 break;
889 default:
890 if (offset_e == 0)
891 offset_s = offset;
892 else
893 goto out;
894 }
895 offset += tlv_len;
896 }
897
898out:
899 if (offset_s)
900 *start = offset_s + calipso_tlv_len(hop, offset_s);
901 else
902 *start = sizeof(*hop);
903 if (offset_e)
904 *end = offset_e + calipso_tlv_len(hop, offset_e);
905 else
906 *end = opt_len;
907
908 return ret_val;
909}
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925static struct ipv6_opt_hdr *
926calipso_opt_insert(struct ipv6_opt_hdr *hop,
927 const struct calipso_doi *doi_def,
928 const struct netlbl_lsm_secattr *secattr)
929{
930 unsigned int start, end, buf_len, pad, hop_len;
931 struct ipv6_opt_hdr *new;
932 int ret_val;
933
934 if (hop) {
935 hop_len = ipv6_optlen(hop);
936 ret_val = calipso_opt_find(hop, &start, &end);
937 if (ret_val && ret_val != -ENOENT)
938 return ERR_PTR(ret_val);
939 } else {
940 hop_len = 0;
941 start = sizeof(*hop);
942 end = 0;
943 }
944
945 buf_len = hop_len + start - end + CALIPSO_OPT_LEN_MAX_WITH_PAD;
946 new = kzalloc(buf_len, GFP_ATOMIC);
947 if (!new)
948 return ERR_PTR(-ENOMEM);
949
950 if (start > sizeof(*hop))
951 memcpy(new, hop, start);
952 ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
953 secattr);
954 if (ret_val < 0) {
955 kfree(new);
956 return ERR_PTR(ret_val);
957 }
958
959 buf_len = start + ret_val;
960
961 pad = ((buf_len & 4) + (end & 7)) & 7;
962 calipso_pad_write((unsigned char *)new, buf_len, pad);
963 buf_len += pad;
964
965 if (end != hop_len) {
966 memcpy((char *)new + buf_len, (char *)hop + end, hop_len - end);
967 buf_len += hop_len - end;
968 }
969 new->nexthdr = 0;
970 new->hdrlen = buf_len / 8 - 1;
971
972 return new;
973}
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989static int calipso_opt_del(struct ipv6_opt_hdr *hop,
990 struct ipv6_opt_hdr **new)
991{
992 int ret_val;
993 unsigned int start, end, delta, pad, hop_len;
994
995 ret_val = calipso_opt_find(hop, &start, &end);
996 if (ret_val)
997 return ret_val;
998
999 hop_len = ipv6_optlen(hop);
1000 if (start == sizeof(*hop) && end == hop_len) {
1001
1002 *new = NULL;
1003 return 0;
1004 }
1005
1006 delta = (end - start) & ~7;
1007 *new = kzalloc(hop_len - delta, GFP_ATOMIC);
1008 if (!*new)
1009 return -ENOMEM;
1010
1011 memcpy(*new, hop, start);
1012 (*new)->hdrlen -= delta / 8;
1013 pad = (end - start) & 7;
1014 calipso_pad_write((unsigned char *)*new, start, pad);
1015 if (end != hop_len)
1016 memcpy((char *)*new + start + pad, (char *)hop + end,
1017 hop_len - end);
1018
1019 return 0;
1020}
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032static int calipso_opt_getattr(const unsigned char *calipso,
1033 struct netlbl_lsm_secattr *secattr)
1034{
1035 int ret_val = -ENOMSG;
1036 u32 doi, len = calipso[1], cat_len = calipso[6] * 4;
1037 struct calipso_doi *doi_def;
1038
1039 if (cat_len + 8 > len)
1040 return -EINVAL;
1041
1042 if (calipso_cache_check(calipso + 2, calipso[1], secattr) == 0)
1043 return 0;
1044
1045 doi = get_unaligned_be32(calipso + 2);
1046 rcu_read_lock();
1047 doi_def = calipso_doi_search(doi);
1048 if (!doi_def)
1049 goto getattr_return;
1050
1051 secattr->attr.mls.lvl = calipso[7];
1052 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1053
1054 if (cat_len) {
1055 ret_val = calipso_map_cat_ntoh(doi_def,
1056 calipso + 10,
1057 cat_len,
1058 secattr);
1059 if (ret_val != 0) {
1060 netlbl_catmap_free(secattr->attr.mls.cat);
1061 goto getattr_return;
1062 }
1063
1064 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1065 }
1066
1067 secattr->type = NETLBL_NLTYPE_CALIPSO;
1068
1069getattr_return:
1070 rcu_read_unlock();
1071 return ret_val;
1072}
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089static int calipso_sock_getattr(struct sock *sk,
1090 struct netlbl_lsm_secattr *secattr)
1091{
1092 struct ipv6_opt_hdr *hop;
1093 int opt_len, len, ret_val = -ENOMSG, offset;
1094 unsigned char *opt;
1095 struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
1096
1097 if (!txopts || !txopts->hopopt)
1098 goto done;
1099
1100 hop = txopts->hopopt;
1101 opt = (unsigned char *)hop;
1102 opt_len = ipv6_optlen(hop);
1103 offset = sizeof(*hop);
1104 while (offset < opt_len) {
1105 len = calipso_tlv_len(hop, offset);
1106 if (len < 0) {
1107 ret_val = len;
1108 goto done;
1109 }
1110 switch (opt[offset]) {
1111 case IPV6_TLV_CALIPSO:
1112 if (len < CALIPSO_HDR_LEN)
1113 ret_val = -EINVAL;
1114 else
1115 ret_val = calipso_opt_getattr(&opt[offset],
1116 secattr);
1117 goto done;
1118 default:
1119 offset += len;
1120 break;
1121 }
1122 }
1123done:
1124 txopt_put(txopts);
1125 return ret_val;
1126}
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142static int calipso_sock_setattr(struct sock *sk,
1143 const struct calipso_doi *doi_def,
1144 const struct netlbl_lsm_secattr *secattr)
1145{
1146 int ret_val;
1147 struct ipv6_opt_hdr *old, *new;
1148 struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
1149
1150 old = NULL;
1151 if (txopts)
1152 old = txopts->hopopt;
1153
1154 new = calipso_opt_insert(old, doi_def, secattr);
1155 txopt_put(txopts);
1156 if (IS_ERR(new))
1157 return PTR_ERR(new);
1158
1159 ret_val = calipso_opt_update(sk, new);
1160
1161 kfree(new);
1162 return ret_val;
1163}
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173static void calipso_sock_delattr(struct sock *sk)
1174{
1175 struct ipv6_opt_hdr *new_hop;
1176 struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
1177
1178 if (!txopts || !txopts->hopopt)
1179 goto done;
1180
1181 if (calipso_opt_del(txopts->hopopt, &new_hop))
1182 goto done;
1183
1184 calipso_opt_update(sk, new_hop);
1185 kfree(new_hop);
1186
1187done:
1188 txopt_put(txopts);
1189}
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206static int calipso_req_setattr(struct request_sock *req,
1207 const struct calipso_doi *doi_def,
1208 const struct netlbl_lsm_secattr *secattr)
1209{
1210 struct ipv6_txoptions *txopts;
1211 struct inet_request_sock *req_inet = inet_rsk(req);
1212 struct ipv6_opt_hdr *old, *new;
1213 struct sock *sk = sk_to_full_sk(req_to_sk(req));
1214
1215 if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
1216 old = req_inet->ipv6_opt->hopopt;
1217 else
1218 old = NULL;
1219
1220 new = calipso_opt_insert(old, doi_def, secattr);
1221 if (IS_ERR(new))
1222 return PTR_ERR(new);
1223
1224 txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
1225
1226 kfree(new);
1227
1228 if (IS_ERR(txopts))
1229 return PTR_ERR(txopts);
1230
1231 txopts = xchg(&req_inet->ipv6_opt, txopts);
1232 if (txopts) {
1233 atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
1234 txopt_put(txopts);
1235 }
1236
1237 return 0;
1238}
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248static void calipso_req_delattr(struct request_sock *req)
1249{
1250 struct inet_request_sock *req_inet = inet_rsk(req);
1251 struct ipv6_opt_hdr *new;
1252 struct ipv6_txoptions *txopts;
1253 struct sock *sk = sk_to_full_sk(req_to_sk(req));
1254
1255 if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
1256 return;
1257
1258 if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
1259 return;
1260
1261 txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
1262
1263 if (!IS_ERR(txopts)) {
1264 txopts = xchg(&req_inet->ipv6_opt, txopts);
1265 if (txopts) {
1266 atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
1267 txopt_put(txopts);
1268 }
1269 }
1270 kfree(new);
1271}
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
1286{
1287 const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
1288 int offset;
1289
1290 if (ip6_hdr->nexthdr != NEXTHDR_HOP)
1291 return NULL;
1292
1293 offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
1294 if (offset >= 0)
1295 return (unsigned char *)ip6_hdr + offset;
1296
1297 return NULL;
1298}
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311static int calipso_skbuff_setattr(struct sk_buff *skb,
1312 const struct calipso_doi *doi_def,
1313 const struct netlbl_lsm_secattr *secattr)
1314{
1315 int ret_val;
1316 struct ipv6hdr *ip6_hdr;
1317 struct ipv6_opt_hdr *hop;
1318 unsigned char buf[CALIPSO_MAX_BUFFER];
1319 int len_delta, new_end, pad, payload;
1320 unsigned int start, end;
1321
1322 ip6_hdr = ipv6_hdr(skb);
1323 if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
1324 hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
1325 ret_val = calipso_opt_find(hop, &start, &end);
1326 if (ret_val && ret_val != -ENOENT)
1327 return ret_val;
1328 } else {
1329 start = 0;
1330 end = 0;
1331 }
1332
1333 memset(buf, 0, sizeof(buf));
1334 ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
1335 if (ret_val < 0)
1336 return ret_val;
1337
1338 new_end = start + ret_val;
1339
1340 pad = ((new_end & 4) + (end & 7)) & 7;
1341 len_delta = new_end - (int)end + pad;
1342 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
1343 if (ret_val < 0)
1344 return ret_val;
1345
1346 ip6_hdr = ipv6_hdr(skb);
1347
1348 if (len_delta) {
1349 if (len_delta > 0)
1350 skb_push(skb, len_delta);
1351 else
1352 skb_pull(skb, -len_delta);
1353 memmove((char *)ip6_hdr - len_delta, ip6_hdr,
1354 sizeof(*ip6_hdr) + start);
1355 skb_reset_network_header(skb);
1356 ip6_hdr = ipv6_hdr(skb);
1357 payload = ntohs(ip6_hdr->payload_len);
1358 ip6_hdr->payload_len = htons(payload + len_delta);
1359 }
1360
1361 hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
1362 if (start == 0) {
1363 struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
1364
1365 new_hop->nexthdr = ip6_hdr->nexthdr;
1366 new_hop->hdrlen = len_delta / 8 - 1;
1367 ip6_hdr->nexthdr = NEXTHDR_HOP;
1368 } else {
1369 hop->hdrlen += len_delta / 8;
1370 }
1371 memcpy((char *)hop + start, buf + (start & 3), new_end - start);
1372 calipso_pad_write((unsigned char *)hop, new_end, pad);
1373
1374 return 0;
1375}
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386static int calipso_skbuff_delattr(struct sk_buff *skb)
1387{
1388 int ret_val;
1389 struct ipv6hdr *ip6_hdr;
1390 struct ipv6_opt_hdr *old_hop;
1391 u32 old_hop_len, start = 0, end = 0, delta, size, pad;
1392
1393 if (!calipso_skbuff_optptr(skb))
1394 return 0;
1395
1396
1397 ret_val = skb_cow(skb, skb_headroom(skb));
1398 if (ret_val < 0)
1399 return ret_val;
1400
1401 ip6_hdr = ipv6_hdr(skb);
1402 old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
1403 old_hop_len = ipv6_optlen(old_hop);
1404
1405 ret_val = calipso_opt_find(old_hop, &start, &end);
1406 if (ret_val)
1407 return ret_val;
1408
1409 if (start == sizeof(*old_hop) && end == old_hop_len) {
1410
1411
1412 delta = old_hop_len;
1413 size = sizeof(*ip6_hdr);
1414 ip6_hdr->nexthdr = old_hop->nexthdr;
1415 } else {
1416 delta = (end - start) & ~7;
1417 if (delta)
1418 old_hop->hdrlen -= delta / 8;
1419 pad = (end - start) & 7;
1420 size = sizeof(*ip6_hdr) + start + pad;
1421 calipso_pad_write((unsigned char *)old_hop, start, pad);
1422 }
1423
1424 if (delta) {
1425 skb_pull(skb, delta);
1426 memmove((char *)ip6_hdr + delta, ip6_hdr, size);
1427 skb_reset_network_header(skb);
1428 }
1429
1430 return 0;
1431}
1432
1433static const struct netlbl_calipso_ops ops = {
1434 .doi_add = calipso_doi_add,
1435 .doi_free = calipso_doi_free,
1436 .doi_remove = calipso_doi_remove,
1437 .doi_getdef = calipso_doi_getdef,
1438 .doi_putdef = calipso_doi_putdef,
1439 .doi_walk = calipso_doi_walk,
1440 .sock_getattr = calipso_sock_getattr,
1441 .sock_setattr = calipso_sock_setattr,
1442 .sock_delattr = calipso_sock_delattr,
1443 .req_setattr = calipso_req_setattr,
1444 .req_delattr = calipso_req_delattr,
1445 .opt_getattr = calipso_opt_getattr,
1446 .skbuff_optptr = calipso_skbuff_optptr,
1447 .skbuff_setattr = calipso_skbuff_setattr,
1448 .skbuff_delattr = calipso_skbuff_delattr,
1449 .cache_invalidate = calipso_cache_invalidate,
1450 .cache_add = calipso_cache_add
1451};
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461int __init calipso_init(void)
1462{
1463 int ret_val;
1464
1465 ret_val = calipso_cache_init();
1466 if (!ret_val)
1467 netlbl_calipso_ops_register(&ops);
1468 return ret_val;
1469}
1470
1471void calipso_exit(void)
1472{
1473 netlbl_calipso_ops_register(NULL);
1474 calipso_cache_invalidate();
1475 kfree(calipso_cache);
1476}
1477