Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * NetLabel CALIPSO/IPv6 Support
  4 *
  5 * This file defines the CALIPSO/IPv6 functions for the NetLabel system.  The
  6 * NetLabel system manages static and dynamic label mappings for network
  7 * protocols such as CIPSO and CALIPSO.
  8 *
  9 * Authors: Paul Moore <paul@paul-moore.com>
 10 *          Huw Davies <huw@codeweavers.com>
 11 */
 12
 13/* (c) Copyright Hewlett-Packard Development Company, L.P., 2006
 14 * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
 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/* Argument struct for calipso_doi_walk() */
 36struct netlbl_calipso_doiwalk_arg {
 37	struct netlink_callback *nl_cb;
 38	struct sk_buff *skb;
 39	u32 seq;
 40};
 41
 42/* Argument struct for netlbl_domhsh_walk() */
 43struct netlbl_domhsh_walk_arg {
 44	struct netlbl_audit *audit_info;
 45	u32 doi;
 46};
 47
 48/* NetLabel Generic NETLINK CALIPSO family */
 49static struct genl_family netlbl_calipso_gnl_family;
 50
 51/* NetLabel Netlink attribute policy */
 52static 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
 57static const struct netlbl_calipso_ops *calipso_ops;
 58
 59/**
 60 * netlbl_calipso_ops_register - Register the CALIPSO operations
 61 * @ops: ops to register
 62 *
 63 * Description:
 64 * Register the CALIPSO packet engine operations.
 65 *
 66 */
 67const struct netlbl_calipso_ops *
 68netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
 69{
 70	return xchg(&calipso_ops, ops);
 71}
 72EXPORT_SYMBOL(netlbl_calipso_ops_register);
 73
 74static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
 75{
 76	return READ_ONCE(calipso_ops);
 77}
 78
 79/* NetLabel Command Handlers
 80 */
 81/**
 82 * netlbl_calipso_add_pass - Adds a CALIPSO pass DOI definition
 83 * @info: the Generic NETLINK info block
 84 * @audit_info: NetLabel audit information
 85 *
 86 * Description:
 87 * Create a new CALIPSO_MAP_PASS DOI definition based on the given ADD message
 88 * and add it to the CALIPSO engine.  Return zero on success and non-zero on
 89 * error.
 90 *
 91 */
 92static int netlbl_calipso_add_pass(struct genl_info *info,
 93				   struct netlbl_audit *audit_info)
 94{
 95	int ret_val;
 96	struct calipso_doi *doi_def = NULL;
 97
 98	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
 99	if (!doi_def)
100		return -ENOMEM;
101	doi_def->type = CALIPSO_MAP_PASS;
102	doi_def->doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
103	ret_val = calipso_doi_add(doi_def, audit_info);
104	if (ret_val != 0)
105		calipso_doi_free(doi_def);
106
107	return ret_val;
108}
109
110/**
111 * netlbl_calipso_add - Handle an ADD message
112 * @skb: the NETLINK buffer
113 * @info: the Generic NETLINK info block
114 *
115 * Description:
116 * Create a new DOI definition based on the given ADD message and add it to the
117 * CALIPSO engine.  Returns zero on success, negative values on failure.
118 *
119 */
120static int netlbl_calipso_add(struct sk_buff *skb, struct genl_info *info)
 
121{
122	int ret_val = -EINVAL;
123	struct netlbl_audit audit_info;
124	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
125
126	if (!info->attrs[NLBL_CALIPSO_A_DOI] ||
127	    !info->attrs[NLBL_CALIPSO_A_MTYPE])
128		return -EINVAL;
129
130	if (!ops)
131		return -EOPNOTSUPP;
132
133	netlbl_netlink_auditinfo(&audit_info);
134	switch (nla_get_u32(info->attrs[NLBL_CALIPSO_A_MTYPE])) {
135	case CALIPSO_MAP_PASS:
136		ret_val = netlbl_calipso_add_pass(info, &audit_info);
137		break;
138	}
139	if (ret_val == 0)
140		atomic_inc(&netlabel_mgmt_protocount);
141
142	return ret_val;
143}
144
145/**
146 * netlbl_calipso_list - Handle a LIST message
147 * @skb: the NETLINK buffer
148 * @info: the Generic NETLINK info block
149 *
150 * Description:
151 * Process a user generated LIST message and respond accordingly.
152 * Returns zero on success and negative values on error.
153 *
154 */
155static int netlbl_calipso_list(struct sk_buff *skb, struct genl_info *info)
156{
157	int ret_val;
158	struct sk_buff *ans_skb = NULL;
159	void *data;
160	u32 doi;
161	struct calipso_doi *doi_def;
162
163	if (!info->attrs[NLBL_CALIPSO_A_DOI]) {
164		ret_val = -EINVAL;
165		goto list_failure;
166	}
167
168	doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
169
170	doi_def = calipso_doi_getdef(doi);
171	if (!doi_def) {
172		ret_val = -EINVAL;
173		goto list_failure;
174	}
175
176	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
177	if (!ans_skb) {
178		ret_val = -ENOMEM;
179		goto list_failure_put;
180	}
181	data = genlmsg_put_reply(ans_skb, info, &netlbl_calipso_gnl_family,
182				 0, NLBL_CALIPSO_C_LIST);
183	if (!data) {
184		ret_val = -ENOMEM;
185		goto list_failure_put;
186	}
187
188	ret_val = nla_put_u32(ans_skb, NLBL_CALIPSO_A_MTYPE, doi_def->type);
189	if (ret_val != 0)
190		goto list_failure_put;
191
192	calipso_doi_putdef(doi_def);
193
194	genlmsg_end(ans_skb, data);
195	return genlmsg_reply(ans_skb, info);
196
197list_failure_put:
198	calipso_doi_putdef(doi_def);
199list_failure:
200	kfree_skb(ans_skb);
201	return ret_val;
202}
203
204/**
205 * netlbl_calipso_listall_cb - calipso_doi_walk() callback for LISTALL
206 * @doi_def: the CALIPSO DOI definition
207 * @arg: the netlbl_calipso_doiwalk_arg structure
208 *
209 * Description:
210 * This function is designed to be used as a callback to the
211 * calipso_doi_walk() function for use in generating a response for a LISTALL
212 * message.  Returns the size of the message on success, negative values on
213 * failure.
214 *
215 */
216static int netlbl_calipso_listall_cb(struct calipso_doi *doi_def, void *arg)
217{
218	int ret_val = -ENOMEM;
219	struct netlbl_calipso_doiwalk_arg *cb_arg = arg;
220	void *data;
221
222	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
223			   cb_arg->seq, &netlbl_calipso_gnl_family,
224			   NLM_F_MULTI, NLBL_CALIPSO_C_LISTALL);
225	if (!data)
226		goto listall_cb_failure;
227
228	ret_val = nla_put_u32(cb_arg->skb, NLBL_CALIPSO_A_DOI, doi_def->doi);
229	if (ret_val != 0)
230		goto listall_cb_failure;
231	ret_val = nla_put_u32(cb_arg->skb,
232			      NLBL_CALIPSO_A_MTYPE,
233			      doi_def->type);
234	if (ret_val != 0)
235		goto listall_cb_failure;
236
237	genlmsg_end(cb_arg->skb, data);
238	return 0;
239
240listall_cb_failure:
241	genlmsg_cancel(cb_arg->skb, data);
242	return ret_val;
243}
244
245/**
246 * netlbl_calipso_listall - Handle a LISTALL message
247 * @skb: the NETLINK buffer
248 * @cb: the NETLINK callback
249 *
250 * Description:
251 * Process a user generated LISTALL message and respond accordingly.  Returns
252 * zero on success and negative values on error.
253 *
254 */
255static int netlbl_calipso_listall(struct sk_buff *skb,
256				  struct netlink_callback *cb)
257{
258	struct netlbl_calipso_doiwalk_arg cb_arg;
259	u32 doi_skip = cb->args[0];
260
261	cb_arg.nl_cb = cb;
262	cb_arg.skb = skb;
263	cb_arg.seq = cb->nlh->nlmsg_seq;
264
265	calipso_doi_walk(&doi_skip, netlbl_calipso_listall_cb, &cb_arg);
266
267	cb->args[0] = doi_skip;
268	return skb->len;
269}
270
271/**
272 * netlbl_calipso_remove_cb - netlbl_calipso_remove() callback for REMOVE
273 * @entry: LSM domain mapping entry
274 * @arg: the netlbl_domhsh_walk_arg structure
275 *
276 * Description:
277 * This function is intended for use by netlbl_calipso_remove() as the callback
278 * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
279 * which are associated with the CALIPSO DOI specified in @arg.  Returns zero on
280 * success, negative values on failure.
281 *
282 */
283static int netlbl_calipso_remove_cb(struct netlbl_dom_map *entry, void *arg)
284{
285	struct netlbl_domhsh_walk_arg *cb_arg = arg;
286
287	if (entry->def.type == NETLBL_NLTYPE_CALIPSO &&
288	    entry->def.calipso->doi == cb_arg->doi)
289		return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
290
291	return 0;
292}
293
294/**
295 * netlbl_calipso_remove - Handle a REMOVE message
296 * @skb: the NETLINK buffer
297 * @info: the Generic NETLINK info block
298 *
299 * Description:
300 * Process a user generated REMOVE message and respond accordingly.  Returns
301 * zero on success, negative values on failure.
302 *
303 */
304static int netlbl_calipso_remove(struct sk_buff *skb, struct genl_info *info)
305{
306	int ret_val = -EINVAL;
307	struct netlbl_domhsh_walk_arg cb_arg;
308	struct netlbl_audit audit_info;
309	u32 skip_bkt = 0;
310	u32 skip_chain = 0;
311
312	if (!info->attrs[NLBL_CALIPSO_A_DOI])
313		return -EINVAL;
314
315	netlbl_netlink_auditinfo(&audit_info);
316	cb_arg.doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
317	cb_arg.audit_info = &audit_info;
318	ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
319				     netlbl_calipso_remove_cb, &cb_arg);
320	if (ret_val == 0 || ret_val == -ENOENT) {
321		ret_val = calipso_doi_remove(cb_arg.doi, &audit_info);
322		if (ret_val == 0)
323			atomic_dec(&netlabel_mgmt_protocount);
324	}
325
326	return ret_val;
327}
328
329/* NetLabel Generic NETLINK Command Definitions
330 */
331
332static const struct genl_small_ops netlbl_calipso_ops[] = {
333	{
334	.cmd = NLBL_CALIPSO_C_ADD,
335	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
336	.flags = GENL_ADMIN_PERM,
337	.doit = netlbl_calipso_add,
338	.dumpit = NULL,
339	},
340	{
341	.cmd = NLBL_CALIPSO_C_REMOVE,
342	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
343	.flags = GENL_ADMIN_PERM,
344	.doit = netlbl_calipso_remove,
345	.dumpit = NULL,
346	},
347	{
348	.cmd = NLBL_CALIPSO_C_LIST,
349	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
350	.flags = 0,
351	.doit = netlbl_calipso_list,
352	.dumpit = NULL,
353	},
354	{
355	.cmd = NLBL_CALIPSO_C_LISTALL,
356	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
357	.flags = 0,
358	.doit = NULL,
359	.dumpit = netlbl_calipso_listall,
360	},
361};
362
363static struct genl_family netlbl_calipso_gnl_family __ro_after_init = {
364	.hdrsize = 0,
365	.name = NETLBL_NLTYPE_CALIPSO_NAME,
366	.version = NETLBL_PROTO_VERSION,
367	.maxattr = NLBL_CALIPSO_A_MAX,
368	.policy = calipso_genl_policy,
369	.module = THIS_MODULE,
370	.small_ops = netlbl_calipso_ops,
371	.n_small_ops = ARRAY_SIZE(netlbl_calipso_ops),
372	.resv_start_op = NLBL_CALIPSO_C_LISTALL + 1,
373};
374
375/* NetLabel Generic NETLINK Protocol Functions
376 */
377
378/**
379 * netlbl_calipso_genl_init - Register the CALIPSO NetLabel component
380 *
381 * Description:
382 * Register the CALIPSO packet NetLabel component with the Generic NETLINK
383 * mechanism.  Returns zero on success, negative values on failure.
384 *
385 */
386int __init netlbl_calipso_genl_init(void)
387{
388	return genl_register_family(&netlbl_calipso_gnl_family);
389}
390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391/**
392 * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
393 * @doi_def: the DOI structure
394 * @audit_info: NetLabel audit information
395 *
396 * Description:
397 * The caller defines a new DOI for use by the CALIPSO engine and calls this
398 * function to add it to the list of acceptable domains.  The caller must
399 * ensure that the mapping table specified in @doi_def->map meets all of the
400 * requirements of the mapping type (see calipso.h for details).  Returns
401 * zero on success and non-zero on failure.
402 *
403 */
404int calipso_doi_add(struct calipso_doi *doi_def,
405		    struct netlbl_audit *audit_info)
406{
407	int ret_val = -ENOMSG;
408	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
409
410	if (ops)
411		ret_val = ops->doi_add(doi_def, audit_info);
412	return ret_val;
413}
414
415/**
416 * calipso_doi_free - Frees a DOI definition
417 * @doi_def: the DOI definition
418 *
419 * Description:
420 * This function frees all of the memory associated with a DOI definition.
421 *
422 */
423void calipso_doi_free(struct calipso_doi *doi_def)
424{
425	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
426
427	if (ops)
428		ops->doi_free(doi_def);
429}
430
431/**
432 * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
433 * @doi: the DOI value
434 * @audit_info: NetLabel audit information
435 *
436 * Description:
437 * Removes a DOI definition from the CALIPSO engine.  The NetLabel routines will
438 * be called to release their own LSM domain mappings as well as our own
439 * domain list.  Returns zero on success and negative values on failure.
440 *
441 */
442int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
443{
444	int ret_val = -ENOMSG;
445	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
446
447	if (ops)
448		ret_val = ops->doi_remove(doi, audit_info);
449	return ret_val;
450}
451
452/**
453 * calipso_doi_getdef - Returns a reference to a valid DOI definition
454 * @doi: the DOI value
455 *
456 * Description:
457 * Searches for a valid DOI definition and if one is found it is returned to
458 * the caller.  Otherwise NULL is returned.  The caller must ensure that
459 * calipso_doi_putdef() is called when the caller is done.
460 *
461 */
462struct calipso_doi *calipso_doi_getdef(u32 doi)
463{
464	struct calipso_doi *ret_val = NULL;
465	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
466
467	if (ops)
468		ret_val = ops->doi_getdef(doi);
469	return ret_val;
470}
471
472/**
473 * calipso_doi_putdef - Releases a reference for the given DOI definition
474 * @doi_def: the DOI definition
475 *
476 * Description:
477 * Releases a DOI definition reference obtained from calipso_doi_getdef().
478 *
479 */
480void calipso_doi_putdef(struct calipso_doi *doi_def)
481{
482	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
483
484	if (ops)
485		ops->doi_putdef(doi_def);
486}
487
488/**
489 * calipso_doi_walk - Iterate through the DOI definitions
490 * @skip_cnt: skip past this number of DOI definitions, updated
491 * @callback: callback for each DOI definition
492 * @cb_arg: argument for the callback function
493 *
494 * Description:
495 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
496 * For each entry call @callback, if @callback returns a negative value stop
497 * 'walking' through the list and return.  Updates the value in @skip_cnt upon
498 * return.  Returns zero on success, negative values on failure.
499 *
500 */
501int calipso_doi_walk(u32 *skip_cnt,
502		     int (*callback)(struct calipso_doi *doi_def, void *arg),
503		     void *cb_arg)
504{
505	int ret_val = -ENOMSG;
506	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
507
508	if (ops)
509		ret_val = ops->doi_walk(skip_cnt, callback, cb_arg);
510	return ret_val;
511}
512
513/**
514 * calipso_sock_getattr - Get the security attributes from a sock
515 * @sk: the sock
516 * @secattr: the security attributes
517 *
518 * Description:
519 * Query @sk to see if there is a CALIPSO option attached to the sock and if
520 * there is return the CALIPSO security attributes in @secattr.  This function
521 * requires that @sk be locked, or privately held, but it does not do any
522 * locking itself.  Returns zero on success and negative values on failure.
523 *
524 */
525int calipso_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
526{
527	int ret_val = -ENOMSG;
528	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
529
530	if (ops)
531		ret_val = ops->sock_getattr(sk, secattr);
532	return ret_val;
533}
534
535/**
536 * calipso_sock_setattr - Add a CALIPSO option to a socket
537 * @sk: the socket
538 * @doi_def: the CALIPSO DOI to use
539 * @secattr: the specific security attributes of the socket
540 *
541 * Description:
542 * Set the CALIPSO option on the given socket using the DOI definition and
543 * security attributes passed to the function.  This function requires
544 * exclusive access to @sk, which means it either needs to be in the
545 * process of being created or locked.  Returns zero on success and negative
546 * values on failure.
547 *
548 */
549int calipso_sock_setattr(struct sock *sk,
550			 const struct calipso_doi *doi_def,
551			 const struct netlbl_lsm_secattr *secattr)
552{
553	int ret_val = -ENOMSG;
554	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
555
556	if (ops)
557		ret_val = ops->sock_setattr(sk, doi_def, secattr);
558	return ret_val;
559}
560
561/**
562 * calipso_sock_delattr - Delete the CALIPSO option from a socket
563 * @sk: the socket
564 *
565 * Description:
566 * Removes the CALIPSO option from a socket, if present.
567 *
568 */
569void calipso_sock_delattr(struct sock *sk)
570{
571	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
572
573	if (ops)
574		ops->sock_delattr(sk);
575}
576
577/**
578 * calipso_req_setattr - Add a CALIPSO option to a connection request socket
579 * @req: the connection request socket
580 * @doi_def: the CALIPSO DOI to use
581 * @secattr: the specific security attributes of the socket
582 *
583 * Description:
584 * Set the CALIPSO option on the given socket using the DOI definition and
585 * security attributes passed to the function.  Returns zero on success and
586 * negative values on failure.
587 *
588 */
589int calipso_req_setattr(struct request_sock *req,
590			const struct calipso_doi *doi_def,
591			const struct netlbl_lsm_secattr *secattr)
592{
593	int ret_val = -ENOMSG;
594	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
595
596	if (ops)
597		ret_val = ops->req_setattr(req, doi_def, secattr);
598	return ret_val;
599}
600
601/**
602 * calipso_req_delattr - Delete the CALIPSO option from a request socket
603 * @req: the request socket
604 *
605 * Description:
606 * Removes the CALIPSO option from a request socket, if present.
607 *
608 */
609void calipso_req_delattr(struct request_sock *req)
610{
611	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
612
613	if (ops)
614		ops->req_delattr(req);
615}
616
617/**
618 * calipso_optptr - Find the CALIPSO option in the packet
619 * @skb: the packet
620 *
621 * Description:
622 * Parse the packet's IP header looking for a CALIPSO option.  Returns a pointer
623 * to the start of the CALIPSO option on success, NULL if one if not found.
624 *
625 */
626unsigned char *calipso_optptr(const struct sk_buff *skb)
627{
628	unsigned char *ret_val = NULL;
629	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
630
631	if (ops)
632		ret_val = ops->skbuff_optptr(skb);
633	return ret_val;
634}
635
636/**
637 * calipso_getattr - Get the security attributes from a memory block.
638 * @calipso: the CALIPSO option
639 * @secattr: the security attributes
640 *
641 * Description:
642 * Inspect @calipso and return the security attributes in @secattr.
643 * Returns zero on success and negative values on failure.
644 *
645 */
646int calipso_getattr(const unsigned char *calipso,
647		    struct netlbl_lsm_secattr *secattr)
648{
649	int ret_val = -ENOMSG;
650	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
651
652	if (ops)
653		ret_val = ops->opt_getattr(calipso, secattr);
654	return ret_val;
655}
656
657/**
658 * calipso_skbuff_setattr - Set the CALIPSO option on a packet
659 * @skb: the packet
660 * @doi_def: the CALIPSO DOI to use
661 * @secattr: the security attributes
662 *
663 * Description:
664 * Set the CALIPSO option on the given packet based on the security attributes.
665 * Returns a pointer to the IP header on success and NULL on failure.
666 *
667 */
668int calipso_skbuff_setattr(struct sk_buff *skb,
669			   const struct calipso_doi *doi_def,
670			   const struct netlbl_lsm_secattr *secattr)
671{
672	int ret_val = -ENOMSG;
673	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
674
675	if (ops)
676		ret_val = ops->skbuff_setattr(skb, doi_def, secattr);
677	return ret_val;
678}
679
680/**
681 * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
682 * @skb: the packet
683 *
684 * Description:
685 * Removes any and all CALIPSO options from the given packet.  Returns zero on
686 * success, negative values on failure.
687 *
688 */
689int calipso_skbuff_delattr(struct sk_buff *skb)
690{
691	int ret_val = -ENOMSG;
692	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
693
694	if (ops)
695		ret_val = ops->skbuff_delattr(skb);
696	return ret_val;
697}
698
699/**
700 * calipso_cache_invalidate - Invalidates the current CALIPSO cache
701 *
702 * Description:
703 * Invalidates and frees any entries in the CALIPSO cache.  Returns zero on
704 * success and negative values on failure.
705 *
706 */
707void calipso_cache_invalidate(void)
708{
709	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
710
711	if (ops)
712		ops->cache_invalidate();
713}
714
715/**
716 * calipso_cache_add - Add an entry to the CALIPSO cache
717 * @calipso_ptr: the CALIPSO option
718 * @secattr: the packet's security attributes
719 *
720 * Description:
721 * Add a new entry into the CALIPSO label mapping cache.
722 * Returns zero on success, negative values on failure.
723 *
724 */
725int calipso_cache_add(const unsigned char *calipso_ptr,
726		      const struct netlbl_lsm_secattr *secattr)
727
728{
729	int ret_val = -ENOMSG;
730	const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
731
732	if (ops)
733		ret_val = ops->cache_add(calipso_ptr, secattr);
734	return ret_val;
735}
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * NetLabel CALIPSO/IPv6 Support
  4 *
  5 * This file defines the CALIPSO/IPv6 functions for the NetLabel system.  The
  6 * NetLabel system manages static and dynamic label mappings for network
  7 * protocols such as CIPSO and CALIPSO.
  8 *
  9 * Authors: Paul Moore <paul@paul-moore.com>
 10 *          Huw Davies <huw@codeweavers.com>
 11 */
 12
 13/* (c) Copyright Hewlett-Packard Development Company, L.P., 2006
 14 * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
 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/* Argument struct for calipso_doi_walk() */
 36struct netlbl_calipso_doiwalk_arg {
 37	struct netlink_callback *nl_cb;
 38	struct sk_buff *skb;
 39	u32 seq;
 40};
 41
 42/* Argument struct for netlbl_domhsh_walk() */
 43struct netlbl_domhsh_walk_arg {
 44	struct netlbl_audit *audit_info;
 45	u32 doi;
 46};
 47
 48/* NetLabel Generic NETLINK CALIPSO family */
 49static struct genl_family netlbl_calipso_gnl_family;
 50
 51/* NetLabel Netlink attribute policy */
 52static 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/* NetLabel Command Handlers
 58 */
 59/**
 60 * netlbl_calipso_add_pass - Adds a CALIPSO pass DOI definition
 61 * @info: the Generic NETLINK info block
 62 * @audit_info: NetLabel audit information
 63 *
 64 * Description:
 65 * Create a new CALIPSO_MAP_PASS DOI definition based on the given ADD message
 66 * and add it to the CALIPSO engine.  Return zero on success and non-zero on
 67 * error.
 68 *
 69 */
 70static 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 * netlbl_calipso_add - Handle an ADD message
 90 * @skb: the NETLINK buffer
 91 * @info: the Generic NETLINK info block
 92 *
 93 * Description:
 94 * Create a new DOI definition based on the given ADD message and add it to the
 95 * CALIPSO engine.  Returns zero on success, negative values on failure.
 96 *
 97 */
 98static 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 * netlbl_calipso_list - Handle a LIST message
122 * @skb: the NETLINK buffer
123 * @info: the Generic NETLINK info block
124 *
125 * Description:
126 * Process a user generated LIST message and respond accordingly.
127 * Returns zero on success and negative values on error.
128 *
129 */
130static 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
172list_failure_put:
173	calipso_doi_putdef(doi_def);
174list_failure:
175	kfree_skb(ans_skb);
176	return ret_val;
177}
178
179/**
180 * netlbl_calipso_listall_cb - calipso_doi_walk() callback for LISTALL
181 * @doi_def: the CALIPSO DOI definition
182 * @arg: the netlbl_calipso_doiwalk_arg structure
183 *
184 * Description:
185 * This function is designed to be used as a callback to the
186 * calipso_doi_walk() function for use in generating a response for a LISTALL
187 * message.  Returns the size of the message on success, negative values on
188 * failure.
189 *
190 */
191static 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
215listall_cb_failure:
216	genlmsg_cancel(cb_arg->skb, data);
217	return ret_val;
218}
219
220/**
221 * netlbl_calipso_listall - Handle a LISTALL message
222 * @skb: the NETLINK buffer
223 * @cb: the NETLINK callback
224 *
225 * Description:
226 * Process a user generated LISTALL message and respond accordingly.  Returns
227 * zero on success and negative values on error.
228 *
229 */
230static 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 * netlbl_calipso_remove_cb - netlbl_calipso_remove() callback for REMOVE
248 * @entry: LSM domain mapping entry
249 * @arg: the netlbl_domhsh_walk_arg structure
250 *
251 * Description:
252 * This function is intended for use by netlbl_calipso_remove() as the callback
253 * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
254 * which are associated with the CALIPSO DOI specified in @arg.  Returns zero on
255 * success, negative values on failure.
256 *
257 */
258static 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 * netlbl_calipso_remove - Handle a REMOVE message
271 * @skb: the NETLINK buffer
272 * @info: the Generic NETLINK info block
273 *
274 * Description:
275 * Process a user generated REMOVE message and respond accordingly.  Returns
276 * zero on success, negative values on failure.
277 *
278 */
279static 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/* NetLabel Generic NETLINK Command Definitions
305 */
306
307static 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
338static 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/* NetLabel Generic NETLINK Protocol Functions
350 */
351
352/**
353 * netlbl_calipso_genl_init - Register the CALIPSO NetLabel component
354 *
355 * Description:
356 * Register the CALIPSO packet NetLabel component with the Generic NETLINK
357 * mechanism.  Returns zero on success, negative values on failure.
358 *
359 */
360int __init netlbl_calipso_genl_init(void)
361{
362	return genl_register_family(&netlbl_calipso_gnl_family);
363}
364
365static const struct netlbl_calipso_ops *calipso_ops;
366
367/**
368 * netlbl_calipso_ops_register - Register the CALIPSO operations
369 *
370 * Description:
371 * Register the CALIPSO packet engine operations.
372 *
373 */
374const struct netlbl_calipso_ops *
375netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
376{
377	return xchg(&calipso_ops, ops);
378}
379EXPORT_SYMBOL(netlbl_calipso_ops_register);
380
381static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
382{
383	return READ_ONCE(calipso_ops);
384}
385
386/**
387 * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
388 * @doi_def: the DOI structure
389 * @audit_info: NetLabel audit information
390 *
391 * Description:
392 * The caller defines a new DOI for use by the CALIPSO engine and calls this
393 * function to add it to the list of acceptable domains.  The caller must
394 * ensure that the mapping table specified in @doi_def->map meets all of the
395 * requirements of the mapping type (see calipso.h for details).  Returns
396 * zero on success and non-zero on failure.
397 *
398 */
399int 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 * calipso_doi_free - Frees a DOI definition
412 * @doi_def: the DOI definition
413 *
414 * Description:
415 * This function frees all of the memory associated with a DOI definition.
416 *
417 */
418void 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 * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
428 * @doi: the DOI value
429 * @audit_secid: the LSM secid to use in the audit message
430 *
431 * Description:
432 * Removes a DOI definition from the CALIPSO engine.  The NetLabel routines will
433 * be called to release their own LSM domain mappings as well as our own
434 * domain list.  Returns zero on success and negative values on failure.
435 *
436 */
437int 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 * calipso_doi_getdef - Returns a reference to a valid DOI definition
449 * @doi: the DOI value
450 *
451 * Description:
452 * Searches for a valid DOI definition and if one is found it is returned to
453 * the caller.  Otherwise NULL is returned.  The caller must ensure that
454 * calipso_doi_putdef() is called when the caller is done.
455 *
456 */
457struct 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 * calipso_doi_putdef - Releases a reference for the given DOI definition
469 * @doi_def: the DOI definition
470 *
471 * Description:
472 * Releases a DOI definition reference obtained from calipso_doi_getdef().
473 *
474 */
475void 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 * calipso_doi_walk - Iterate through the DOI definitions
485 * @skip_cnt: skip past this number of DOI definitions, updated
486 * @callback: callback for each DOI definition
487 * @cb_arg: argument for the callback function
488 *
489 * Description:
490 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
491 * For each entry call @callback, if @callback returns a negative value stop
492 * 'walking' through the list and return.  Updates the value in @skip_cnt upon
493 * return.  Returns zero on success, negative values on failure.
494 *
495 */
496int 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 * calipso_sock_getattr - Get the security attributes from a sock
510 * @sk: the sock
511 * @secattr: the security attributes
512 *
513 * Description:
514 * Query @sk to see if there is a CALIPSO option attached to the sock and if
515 * there is return the CALIPSO security attributes in @secattr.  This function
516 * requires that @sk be locked, or privately held, but it does not do any
517 * locking itself.  Returns zero on success and negative values on failure.
518 *
519 */
520int 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 * calipso_sock_setattr - Add a CALIPSO option to a socket
532 * @sk: the socket
533 * @doi_def: the CALIPSO DOI to use
534 * @secattr: the specific security attributes of the socket
535 *
536 * Description:
537 * Set the CALIPSO option on the given socket using the DOI definition and
538 * security attributes passed to the function.  This function requires
539 * exclusive access to @sk, which means it either needs to be in the
540 * process of being created or locked.  Returns zero on success and negative
541 * values on failure.
542 *
543 */
544int 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 * calipso_sock_delattr - Delete the CALIPSO option from a socket
558 * @sk: the socket
559 *
560 * Description:
561 * Removes the CALIPSO option from a socket, if present.
562 *
563 */
564void 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 * calipso_req_setattr - Add a CALIPSO option to a connection request socket
574 * @req: the connection request socket
575 * @doi_def: the CALIPSO DOI to use
576 * @secattr: the specific security attributes of the socket
577 *
578 * Description:
579 * Set the CALIPSO option on the given socket using the DOI definition and
580 * security attributes passed to the function.  Returns zero on success and
581 * negative values on failure.
582 *
583 */
584int 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 * calipso_req_delattr - Delete the CALIPSO option from a request socket
598 * @reg: the request socket
599 *
600 * Description:
601 * Removes the CALIPSO option from a request socket, if present.
602 *
603 */
604void 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 * calipso_optptr - Find the CALIPSO option in the packet
614 * @skb: the packet
615 *
616 * Description:
617 * Parse the packet's IP header looking for a CALIPSO option.  Returns a pointer
618 * to the start of the CALIPSO option on success, NULL if one if not found.
619 *
620 */
621unsigned 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 * calipso_getattr - Get the security attributes from a memory block.
633 * @calipso: the CALIPSO option
634 * @secattr: the security attributes
635 *
636 * Description:
637 * Inspect @calipso and return the security attributes in @secattr.
638 * Returns zero on success and negative values on failure.
639 *
640 */
641int 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 * calipso_skbuff_setattr - Set the CALIPSO option on a packet
654 * @skb: the packet
655 * @doi_def: the CALIPSO DOI to use
656 * @secattr: the security attributes
657 *
658 * Description:
659 * Set the CALIPSO option on the given packet based on the security attributes.
660 * Returns a pointer to the IP header on success and NULL on failure.
661 *
662 */
663int 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 * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
677 * @skb: the packet
678 *
679 * Description:
680 * Removes any and all CALIPSO options from the given packet.  Returns zero on
681 * success, negative values on failure.
682 *
683 */
684int 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 * calipso_cache_invalidate - Invalidates the current CALIPSO cache
696 *
697 * Description:
698 * Invalidates and frees any entries in the CALIPSO cache.  Returns zero on
699 * success and negative values on failure.
700 *
701 */
702void 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 * calipso_cache_add - Add an entry to the CALIPSO cache
712 * @calipso_ptr: the CALIPSO option
713 * @secattr: the packet's security attributes
714 *
715 * Description:
716 * Add a new entry into the CALIPSO label mapping cache.
717 * Returns zero on success, negative values on failure.
718 *
719 */
720int 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}