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/socket.h>
33#include <linux/string.h>
34#include <linux/skbuff.h>
35#include <linux/audit.h>
36#include <linux/slab.h>
37#include <net/sock.h>
38#include <net/netlink.h>
39#include <net/genetlink.h>
40#include <net/netlabel.h>
41#include <net/calipso.h>
42#include <linux/atomic.h>
43
44#include "netlabel_user.h"
45#include "netlabel_calipso.h"
46#include "netlabel_mgmt.h"
47#include "netlabel_domainhash.h"
48
49
50struct netlbl_calipso_doiwalk_arg {
51 struct netlink_callback *nl_cb;
52 struct sk_buff *skb;
53 u32 seq;
54};
55
56
57struct netlbl_domhsh_walk_arg {
58 struct netlbl_audit *audit_info;
59 u32 doi;
60};
61
62
63static struct genl_family netlbl_calipso_gnl_family;
64
65
66static const struct nla_policy calipso_genl_policy[NLBL_CALIPSO_A_MAX + 1] = {
67 [NLBL_CALIPSO_A_DOI] = { .type = NLA_U32 },
68 [NLBL_CALIPSO_A_MTYPE] = { .type = NLA_U32 },
69};
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84static int netlbl_calipso_add_pass(struct genl_info *info,
85 struct netlbl_audit *audit_info)
86{
87 int ret_val;
88 struct calipso_doi *doi_def = NULL;
89
90 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
91 if (!doi_def)
92 return -ENOMEM;
93 doi_def->type = CALIPSO_MAP_PASS;
94 doi_def->doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
95 ret_val = calipso_doi_add(doi_def, audit_info);
96 if (ret_val != 0)
97 calipso_doi_free(doi_def);
98
99 return ret_val;
100}
101
102
103
104
105
106
107
108
109
110
111
112static int netlbl_calipso_add(struct sk_buff *skb, struct genl_info *info)
113
114{
115 int ret_val = -EINVAL;
116 struct netlbl_audit audit_info;
117
118 if (!info->attrs[NLBL_CALIPSO_A_DOI] ||
119 !info->attrs[NLBL_CALIPSO_A_MTYPE])
120 return -EINVAL;
121
122 netlbl_netlink_auditinfo(skb, &audit_info);
123 switch (nla_get_u32(info->attrs[NLBL_CALIPSO_A_MTYPE])) {
124 case CALIPSO_MAP_PASS:
125 ret_val = netlbl_calipso_add_pass(info, &audit_info);
126 break;
127 }
128 if (ret_val == 0)
129 atomic_inc(&netlabel_mgmt_protocount);
130
131 return ret_val;
132}
133
134
135
136
137
138
139
140
141
142
143
144static int netlbl_calipso_list(struct sk_buff *skb, struct genl_info *info)
145{
146 int ret_val;
147 struct sk_buff *ans_skb = NULL;
148 void *data;
149 u32 doi;
150 struct calipso_doi *doi_def;
151
152 if (!info->attrs[NLBL_CALIPSO_A_DOI]) {
153 ret_val = -EINVAL;
154 goto list_failure;
155 }
156
157 doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
158
159 doi_def = calipso_doi_getdef(doi);
160 if (!doi_def) {
161 ret_val = -EINVAL;
162 goto list_failure;
163 }
164
165 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
166 if (!ans_skb) {
167 ret_val = -ENOMEM;
168 goto list_failure_put;
169 }
170 data = genlmsg_put_reply(ans_skb, info, &netlbl_calipso_gnl_family,
171 0, NLBL_CALIPSO_C_LIST);
172 if (!data) {
173 ret_val = -ENOMEM;
174 goto list_failure_put;
175 }
176
177 ret_val = nla_put_u32(ans_skb, NLBL_CALIPSO_A_MTYPE, doi_def->type);
178 if (ret_val != 0)
179 goto list_failure_put;
180
181 calipso_doi_putdef(doi_def);
182
183 genlmsg_end(ans_skb, data);
184 return genlmsg_reply(ans_skb, info);
185
186list_failure_put:
187 calipso_doi_putdef(doi_def);
188list_failure:
189 kfree_skb(ans_skb);
190 return ret_val;
191}
192
193
194
195
196
197
198
199
200
201
202
203
204
205static int netlbl_calipso_listall_cb(struct calipso_doi *doi_def, void *arg)
206{
207 int ret_val = -ENOMEM;
208 struct netlbl_calipso_doiwalk_arg *cb_arg = arg;
209 void *data;
210
211 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
212 cb_arg->seq, &netlbl_calipso_gnl_family,
213 NLM_F_MULTI, NLBL_CALIPSO_C_LISTALL);
214 if (!data)
215 goto listall_cb_failure;
216
217 ret_val = nla_put_u32(cb_arg->skb, NLBL_CALIPSO_A_DOI, doi_def->doi);
218 if (ret_val != 0)
219 goto listall_cb_failure;
220 ret_val = nla_put_u32(cb_arg->skb,
221 NLBL_CALIPSO_A_MTYPE,
222 doi_def->type);
223 if (ret_val != 0)
224 goto listall_cb_failure;
225
226 genlmsg_end(cb_arg->skb, data);
227 return 0;
228
229listall_cb_failure:
230 genlmsg_cancel(cb_arg->skb, data);
231 return ret_val;
232}
233
234
235
236
237
238
239
240
241
242
243
244static int netlbl_calipso_listall(struct sk_buff *skb,
245 struct netlink_callback *cb)
246{
247 struct netlbl_calipso_doiwalk_arg cb_arg;
248 u32 doi_skip = cb->args[0];
249
250 cb_arg.nl_cb = cb;
251 cb_arg.skb = skb;
252 cb_arg.seq = cb->nlh->nlmsg_seq;
253
254 calipso_doi_walk(&doi_skip, netlbl_calipso_listall_cb, &cb_arg);
255
256 cb->args[0] = doi_skip;
257 return skb->len;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272static int netlbl_calipso_remove_cb(struct netlbl_dom_map *entry, void *arg)
273{
274 struct netlbl_domhsh_walk_arg *cb_arg = arg;
275
276 if (entry->def.type == NETLBL_NLTYPE_CALIPSO &&
277 entry->def.calipso->doi == cb_arg->doi)
278 return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
279
280 return 0;
281}
282
283
284
285
286
287
288
289
290
291
292
293static int netlbl_calipso_remove(struct sk_buff *skb, struct genl_info *info)
294{
295 int ret_val = -EINVAL;
296 struct netlbl_domhsh_walk_arg cb_arg;
297 struct netlbl_audit audit_info;
298 u32 skip_bkt = 0;
299 u32 skip_chain = 0;
300
301 if (!info->attrs[NLBL_CALIPSO_A_DOI])
302 return -EINVAL;
303
304 netlbl_netlink_auditinfo(skb, &audit_info);
305 cb_arg.doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
306 cb_arg.audit_info = &audit_info;
307 ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
308 netlbl_calipso_remove_cb, &cb_arg);
309 if (ret_val == 0 || ret_val == -ENOENT) {
310 ret_val = calipso_doi_remove(cb_arg.doi, &audit_info);
311 if (ret_val == 0)
312 atomic_dec(&netlabel_mgmt_protocount);
313 }
314
315 return ret_val;
316}
317
318
319
320
321static const struct genl_ops netlbl_calipso_ops[] = {
322 {
323 .cmd = NLBL_CALIPSO_C_ADD,
324 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
325 .flags = GENL_ADMIN_PERM,
326 .doit = netlbl_calipso_add,
327 .dumpit = NULL,
328 },
329 {
330 .cmd = NLBL_CALIPSO_C_REMOVE,
331 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
332 .flags = GENL_ADMIN_PERM,
333 .doit = netlbl_calipso_remove,
334 .dumpit = NULL,
335 },
336 {
337 .cmd = NLBL_CALIPSO_C_LIST,
338 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
339 .flags = 0,
340 .doit = netlbl_calipso_list,
341 .dumpit = NULL,
342 },
343 {
344 .cmd = NLBL_CALIPSO_C_LISTALL,
345 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
346 .flags = 0,
347 .doit = NULL,
348 .dumpit = netlbl_calipso_listall,
349 },
350};
351
352static struct genl_family netlbl_calipso_gnl_family __ro_after_init = {
353 .hdrsize = 0,
354 .name = NETLBL_NLTYPE_CALIPSO_NAME,
355 .version = NETLBL_PROTO_VERSION,
356 .maxattr = NLBL_CALIPSO_A_MAX,
357 .policy = calipso_genl_policy,
358 .module = THIS_MODULE,
359 .ops = netlbl_calipso_ops,
360 .n_ops = ARRAY_SIZE(netlbl_calipso_ops),
361};
362
363
364
365
366
367
368
369
370
371
372
373
374int __init netlbl_calipso_genl_init(void)
375{
376 return genl_register_family(&netlbl_calipso_gnl_family);
377}
378
379static const struct netlbl_calipso_ops *calipso_ops;
380
381
382
383
384
385
386
387
388const struct netlbl_calipso_ops *
389netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
390{
391 return xchg(&calipso_ops, ops);
392}
393EXPORT_SYMBOL(netlbl_calipso_ops_register);
394
395static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
396{
397 return READ_ONCE(calipso_ops);
398}
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413int calipso_doi_add(struct calipso_doi *doi_def,
414 struct netlbl_audit *audit_info)
415{
416 int ret_val = -ENOMSG;
417 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
418
419 if (ops)
420 ret_val = ops->doi_add(doi_def, audit_info);
421 return ret_val;
422}
423
424
425
426
427
428
429
430
431
432void calipso_doi_free(struct calipso_doi *doi_def)
433{
434 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
435
436 if (ops)
437 ops->doi_free(doi_def);
438}
439
440
441
442
443
444
445
446
447
448
449
450
451int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
452{
453 int ret_val = -ENOMSG;
454 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
455
456 if (ops)
457 ret_val = ops->doi_remove(doi, audit_info);
458 return ret_val;
459}
460
461
462
463
464
465
466
467
468
469
470
471struct calipso_doi *calipso_doi_getdef(u32 doi)
472{
473 struct calipso_doi *ret_val = NULL;
474 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
475
476 if (ops)
477 ret_val = ops->doi_getdef(doi);
478 return ret_val;
479}
480
481
482
483
484
485
486
487
488
489void calipso_doi_putdef(struct calipso_doi *doi_def)
490{
491 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
492
493 if (ops)
494 ops->doi_putdef(doi_def);
495}
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510int calipso_doi_walk(u32 *skip_cnt,
511 int (*callback)(struct calipso_doi *doi_def, void *arg),
512 void *cb_arg)
513{
514 int ret_val = -ENOMSG;
515 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
516
517 if (ops)
518 ret_val = ops->doi_walk(skip_cnt, callback, cb_arg);
519 return ret_val;
520}
521
522
523
524
525
526
527
528
529
530
531
532
533
534int calipso_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
535{
536 int ret_val = -ENOMSG;
537 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
538
539 if (ops)
540 ret_val = ops->sock_getattr(sk, secattr);
541 return ret_val;
542}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558int calipso_sock_setattr(struct sock *sk,
559 const struct calipso_doi *doi_def,
560 const struct netlbl_lsm_secattr *secattr)
561{
562 int ret_val = -ENOMSG;
563 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
564
565 if (ops)
566 ret_val = ops->sock_setattr(sk, doi_def, secattr);
567 return ret_val;
568}
569
570
571
572
573
574
575
576
577
578void calipso_sock_delattr(struct sock *sk)
579{
580 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
581
582 if (ops)
583 ops->sock_delattr(sk);
584}
585
586
587
588
589
590
591
592
593
594
595
596
597
598int calipso_req_setattr(struct request_sock *req,
599 const struct calipso_doi *doi_def,
600 const struct netlbl_lsm_secattr *secattr)
601{
602 int ret_val = -ENOMSG;
603 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
604
605 if (ops)
606 ret_val = ops->req_setattr(req, doi_def, secattr);
607 return ret_val;
608}
609
610
611
612
613
614
615
616
617
618void calipso_req_delattr(struct request_sock *req)
619{
620 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
621
622 if (ops)
623 ops->req_delattr(req);
624}
625
626
627
628
629
630
631
632
633
634
635unsigned char *calipso_optptr(const struct sk_buff *skb)
636{
637 unsigned char *ret_val = NULL;
638 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
639
640 if (ops)
641 ret_val = ops->skbuff_optptr(skb);
642 return ret_val;
643}
644
645
646
647
648
649
650
651
652
653
654
655int calipso_getattr(const unsigned char *calipso,
656 struct netlbl_lsm_secattr *secattr)
657{
658 int ret_val = -ENOMSG;
659 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
660
661 if (ops)
662 ret_val = ops->opt_getattr(calipso, secattr);
663 return ret_val;
664}
665
666
667
668
669
670
671
672
673
674
675
676
677int calipso_skbuff_setattr(struct sk_buff *skb,
678 const struct calipso_doi *doi_def,
679 const struct netlbl_lsm_secattr *secattr)
680{
681 int ret_val = -ENOMSG;
682 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
683
684 if (ops)
685 ret_val = ops->skbuff_setattr(skb, doi_def, secattr);
686 return ret_val;
687}
688
689
690
691
692
693
694
695
696
697
698int calipso_skbuff_delattr(struct sk_buff *skb)
699{
700 int ret_val = -ENOMSG;
701 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
702
703 if (ops)
704 ret_val = ops->skbuff_delattr(skb);
705 return ret_val;
706}
707
708
709
710
711
712
713
714
715
716void calipso_cache_invalidate(void)
717{
718 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
719
720 if (ops)
721 ops->cache_invalidate();
722}
723
724
725
726
727
728
729
730
731
732
733
734int calipso_cache_add(const unsigned char *calipso_ptr,
735 const struct netlbl_lsm_secattr *secattr)
736
737{
738 int ret_val = -ENOMSG;
739 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
740
741 if (ops)
742 ret_val = ops->cache_add(calipso_ptr, secattr);
743 return ret_val;
744}
745