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