2 * NetLabel Management Support
4 * This file defines the management functions for the NetLabel system. The
5 * NetLabel system manages static and dynamic label mappings for network
6 * protocols such as CIPSO and RIPSO.
8 * Author: Paul Moore <paul.moore@hp.com>
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/string.h>
34 #include <linux/skbuff.h>
36 #include <net/netlink.h>
37 #include <net/genetlink.h>
38 #include <net/netlabel.h>
39 #include <net/cipso_ipv4.h>
41 #include "netlabel_domainhash.h"
42 #include "netlabel_user.h"
43 #include "netlabel_mgmt.h"
45 /* NetLabel configured protocol count */
46 static DEFINE_SPINLOCK(netlabel_mgmt_protocount_lock
);
47 static u32 netlabel_mgmt_protocount
= 0;
49 /* Argument struct for netlbl_domhsh_walk() */
50 struct netlbl_domhsh_walk_arg
{
51 struct netlink_callback
*nl_cb
;
56 /* NetLabel Generic NETLINK CIPSOv4 family */
57 static struct genl_family netlbl_mgmt_gnl_family
= {
58 .id
= GENL_ID_GENERATE
,
60 .name
= NETLBL_NLTYPE_MGMT_NAME
,
61 .version
= NETLBL_PROTO_VERSION
,
62 .maxattr
= NLBL_MGMT_A_MAX
,
65 /* NetLabel Netlink attribute policy */
66 static const struct nla_policy netlbl_mgmt_genl_policy
[NLBL_MGMT_A_MAX
+ 1] = {
67 [NLBL_MGMT_A_DOMAIN
] = { .type
= NLA_NUL_STRING
},
68 [NLBL_MGMT_A_PROTOCOL
] = { .type
= NLA_U32
},
69 [NLBL_MGMT_A_VERSION
] = { .type
= NLA_U32
},
70 [NLBL_MGMT_A_CV4DOI
] = { .type
= NLA_U32
},
74 * NetLabel Misc Managment Functions
78 * netlbl_mgmt_protocount_inc - Increment the configured labeled protocol count
81 * Increment the number of labeled protocol configurations in the current
82 * NetLabel configuration. Keep track of this for use in determining if
83 * NetLabel label enforcement should be active/enabled or not in the LSM.
86 void netlbl_mgmt_protocount_inc(void)
89 spin_lock(&netlabel_mgmt_protocount_lock
);
90 netlabel_mgmt_protocount
++;
91 spin_unlock(&netlabel_mgmt_protocount_lock
);
96 * netlbl_mgmt_protocount_dec - Decrement the configured labeled protocol count
99 * Decrement the number of labeled protocol configurations in the current
100 * NetLabel configuration. Keep track of this for use in determining if
101 * NetLabel label enforcement should be active/enabled or not in the LSM.
104 void netlbl_mgmt_protocount_dec(void)
107 spin_lock(&netlabel_mgmt_protocount_lock
);
108 if (netlabel_mgmt_protocount
> 0)
109 netlabel_mgmt_protocount
--;
110 spin_unlock(&netlabel_mgmt_protocount_lock
);
115 * netlbl_mgmt_protocount_value - Return the number of configured protocols
118 * Return the number of labeled protocols in the current NetLabel
119 * configuration. This value is useful in determining if NetLabel label
120 * enforcement should be active/enabled or not in the LSM.
123 u32
netlbl_mgmt_protocount_value(void)
128 val
= netlabel_mgmt_protocount
;
135 * NetLabel Command Handlers
139 * netlbl_mgmt_add - Handle an ADD message
140 * @skb: the NETLINK buffer
141 * @info: the Generic NETLINK info block
144 * Process a user generated ADD message and add the domains from the message
145 * to the hash table. See netlabel.h for a description of the message format.
146 * Returns zero on success, negative values on failure.
149 static int netlbl_mgmt_add(struct sk_buff
*skb
, struct genl_info
*info
)
151 int ret_val
= -EINVAL
;
152 struct netlbl_dom_map
*entry
= NULL
;
155 struct netlbl_audit audit_info
;
157 if (!info
->attrs
[NLBL_MGMT_A_DOMAIN
] ||
158 !info
->attrs
[NLBL_MGMT_A_PROTOCOL
])
161 netlbl_netlink_auditinfo(skb
, &audit_info
);
163 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
168 tmp_size
= nla_len(info
->attrs
[NLBL_MGMT_A_DOMAIN
]);
169 entry
->domain
= kmalloc(tmp_size
, GFP_KERNEL
);
170 if (entry
->domain
== NULL
) {
174 entry
->type
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_PROTOCOL
]);
175 nla_strlcpy(entry
->domain
, info
->attrs
[NLBL_MGMT_A_DOMAIN
], tmp_size
);
177 switch (entry
->type
) {
178 case NETLBL_NLTYPE_UNLABELED
:
179 ret_val
= netlbl_domhsh_add(entry
, &audit_info
);
181 case NETLBL_NLTYPE_CIPSOV4
:
182 if (!info
->attrs
[NLBL_MGMT_A_CV4DOI
])
185 tmp_val
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_CV4DOI
]);
186 /* We should be holding a rcu_read_lock() here while we hold
187 * the result but since the entry will always be deleted when
188 * the CIPSO DOI is deleted we aren't going to keep the
191 entry
->type_def
.cipsov4
= cipso_v4_doi_getdef(tmp_val
);
192 if (entry
->type_def
.cipsov4
== NULL
) {
196 ret_val
= netlbl_domhsh_add(entry
, &audit_info
);
209 kfree(entry
->domain
);
215 * netlbl_mgmt_remove - Handle a REMOVE message
216 * @skb: the NETLINK buffer
217 * @info: the Generic NETLINK info block
220 * Process a user generated REMOVE message and remove the specified domain
221 * mappings. Returns zero on success, negative values on failure.
224 static int netlbl_mgmt_remove(struct sk_buff
*skb
, struct genl_info
*info
)
227 struct netlbl_audit audit_info
;
229 if (!info
->attrs
[NLBL_MGMT_A_DOMAIN
])
232 netlbl_netlink_auditinfo(skb
, &audit_info
);
234 domain
= nla_data(info
->attrs
[NLBL_MGMT_A_DOMAIN
]);
235 return netlbl_domhsh_remove(domain
, &audit_info
);
239 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
240 * @entry: the domain mapping hash table entry
241 * @arg: the netlbl_domhsh_walk_arg structure
244 * This function is designed to be used as a callback to the
245 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
246 * message. Returns the size of the message on success, negative values on
250 static int netlbl_mgmt_listall_cb(struct netlbl_dom_map
*entry
, void *arg
)
252 int ret_val
= -ENOMEM
;
253 struct netlbl_domhsh_walk_arg
*cb_arg
= arg
;
256 data
= genlmsg_put(cb_arg
->skb
, NETLINK_CB(cb_arg
->nl_cb
->skb
).pid
,
257 cb_arg
->seq
, &netlbl_mgmt_gnl_family
,
258 NLM_F_MULTI
, NLBL_MGMT_C_LISTALL
);
260 goto listall_cb_failure
;
262 ret_val
= nla_put_string(cb_arg
->skb
,
266 goto listall_cb_failure
;
267 ret_val
= nla_put_u32(cb_arg
->skb
, NLBL_MGMT_A_PROTOCOL
, entry
->type
);
269 goto listall_cb_failure
;
270 switch (entry
->type
) {
271 case NETLBL_NLTYPE_CIPSOV4
:
272 ret_val
= nla_put_u32(cb_arg
->skb
,
274 entry
->type_def
.cipsov4
->doi
);
276 goto listall_cb_failure
;
281 return genlmsg_end(cb_arg
->skb
, data
);
284 genlmsg_cancel(cb_arg
->skb
, data
);
289 * netlbl_mgmt_listall - Handle a LISTALL message
290 * @skb: the NETLINK buffer
291 * @cb: the NETLINK callback
294 * Process a user generated LISTALL message and dumps the domain hash table in
295 * a form suitable for use in a kernel generated LISTALL message. Returns zero
296 * on success, negative values on failure.
299 static int netlbl_mgmt_listall(struct sk_buff
*skb
,
300 struct netlink_callback
*cb
)
302 struct netlbl_domhsh_walk_arg cb_arg
;
303 u32 skip_bkt
= cb
->args
[0];
304 u32 skip_chain
= cb
->args
[1];
308 cb_arg
.seq
= cb
->nlh
->nlmsg_seq
;
310 netlbl_domhsh_walk(&skip_bkt
,
312 netlbl_mgmt_listall_cb
,
315 cb
->args
[0] = skip_bkt
;
316 cb
->args
[1] = skip_chain
;
321 * netlbl_mgmt_adddef - Handle an ADDDEF message
322 * @skb: the NETLINK buffer
323 * @info: the Generic NETLINK info block
326 * Process a user generated ADDDEF message and respond accordingly. Returns
327 * zero on success, negative values on failure.
330 static int netlbl_mgmt_adddef(struct sk_buff
*skb
, struct genl_info
*info
)
332 int ret_val
= -EINVAL
;
333 struct netlbl_dom_map
*entry
= NULL
;
335 struct netlbl_audit audit_info
;
337 if (!info
->attrs
[NLBL_MGMT_A_PROTOCOL
])
340 netlbl_netlink_auditinfo(skb
, &audit_info
);
342 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
347 entry
->type
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_PROTOCOL
]);
349 switch (entry
->type
) {
350 case NETLBL_NLTYPE_UNLABELED
:
351 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
353 case NETLBL_NLTYPE_CIPSOV4
:
354 if (!info
->attrs
[NLBL_MGMT_A_CV4DOI
])
357 tmp_val
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_CV4DOI
]);
358 /* We should be holding a rcu_read_lock() here while we hold
359 * the result but since the entry will always be deleted when
360 * the CIPSO DOI is deleted we aren't going to keep the
363 entry
->type_def
.cipsov4
= cipso_v4_doi_getdef(tmp_val
);
364 if (entry
->type_def
.cipsov4
== NULL
) {
368 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
385 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
386 * @skb: the NETLINK buffer
387 * @info: the Generic NETLINK info block
390 * Process a user generated REMOVEDEF message and remove the default domain
391 * mapping. Returns zero on success, negative values on failure.
394 static int netlbl_mgmt_removedef(struct sk_buff
*skb
, struct genl_info
*info
)
396 struct netlbl_audit audit_info
;
398 netlbl_netlink_auditinfo(skb
, &audit_info
);
400 return netlbl_domhsh_remove_default(&audit_info
);
404 * netlbl_mgmt_listdef - Handle a LISTDEF message
405 * @skb: the NETLINK buffer
406 * @info: the Generic NETLINK info block
409 * Process a user generated LISTDEF message and dumps the default domain
410 * mapping in a form suitable for use in a kernel generated LISTDEF message.
411 * Returns zero on success, negative values on failure.
414 static int netlbl_mgmt_listdef(struct sk_buff
*skb
, struct genl_info
*info
)
416 int ret_val
= -ENOMEM
;
417 struct sk_buff
*ans_skb
= NULL
;
419 struct netlbl_dom_map
*entry
;
421 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
424 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_mgmt_gnl_family
,
425 0, NLBL_MGMT_C_LISTDEF
);
427 goto listdef_failure
;
430 entry
= netlbl_domhsh_getentry(NULL
);
433 goto listdef_failure_lock
;
435 ret_val
= nla_put_u32(ans_skb
, NLBL_MGMT_A_PROTOCOL
, entry
->type
);
437 goto listdef_failure_lock
;
438 switch (entry
->type
) {
439 case NETLBL_NLTYPE_CIPSOV4
:
440 ret_val
= nla_put_u32(ans_skb
,
442 entry
->type_def
.cipsov4
->doi
);
444 goto listdef_failure_lock
;
449 genlmsg_end(ans_skb
, data
);
451 ret_val
= genlmsg_reply(ans_skb
, info
);
453 goto listdef_failure
;
456 listdef_failure_lock
:
464 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
465 * @skb: the skb to write to
466 * @seq: the NETLINK sequence number
467 * @cb: the NETLINK callback
468 * @protocol: the NetLabel protocol to use in the message
471 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
472 * answer a application's PROTOCOLS message. Returns the size of the message
473 * on success, negative values on failure.
476 static int netlbl_mgmt_protocols_cb(struct sk_buff
*skb
,
477 struct netlink_callback
*cb
,
480 int ret_val
= -ENOMEM
;
483 data
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
484 &netlbl_mgmt_gnl_family
, NLM_F_MULTI
,
485 NLBL_MGMT_C_PROTOCOLS
);
487 goto protocols_cb_failure
;
489 ret_val
= nla_put_u32(skb
, NLBL_MGMT_A_PROTOCOL
, protocol
);
491 goto protocols_cb_failure
;
493 return genlmsg_end(skb
, data
);
495 protocols_cb_failure
:
496 genlmsg_cancel(skb
, data
);
501 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
502 * @skb: the NETLINK buffer
503 * @cb: the NETLINK callback
506 * Process a user generated PROTOCOLS message and respond accordingly.
509 static int netlbl_mgmt_protocols(struct sk_buff
*skb
,
510 struct netlink_callback
*cb
)
512 u32 protos_sent
= cb
->args
[0];
514 if (protos_sent
== 0) {
515 if (netlbl_mgmt_protocols_cb(skb
,
517 NETLBL_NLTYPE_UNLABELED
) < 0)
518 goto protocols_return
;
521 if (protos_sent
== 1) {
522 if (netlbl_mgmt_protocols_cb(skb
,
524 NETLBL_NLTYPE_CIPSOV4
) < 0)
525 goto protocols_return
;
530 cb
->args
[0] = protos_sent
;
535 * netlbl_mgmt_version - Handle a VERSION message
536 * @skb: the NETLINK buffer
537 * @info: the Generic NETLINK info block
540 * Process a user generated VERSION message and respond accordingly. Returns
541 * zero on success, negative values on failure.
544 static int netlbl_mgmt_version(struct sk_buff
*skb
, struct genl_info
*info
)
546 int ret_val
= -ENOMEM
;
547 struct sk_buff
*ans_skb
= NULL
;
550 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
553 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_mgmt_gnl_family
,
554 0, NLBL_MGMT_C_VERSION
);
556 goto version_failure
;
558 ret_val
= nla_put_u32(ans_skb
,
560 NETLBL_PROTO_VERSION
);
562 goto version_failure
;
564 genlmsg_end(ans_skb
, data
);
566 ret_val
= genlmsg_reply(ans_skb
, info
);
568 goto version_failure
;
578 * NetLabel Generic NETLINK Command Definitions
581 static struct genl_ops netlbl_mgmt_genl_c_add
= {
582 .cmd
= NLBL_MGMT_C_ADD
,
583 .flags
= GENL_ADMIN_PERM
,
584 .policy
= netlbl_mgmt_genl_policy
,
585 .doit
= netlbl_mgmt_add
,
589 static struct genl_ops netlbl_mgmt_genl_c_remove
= {
590 .cmd
= NLBL_MGMT_C_REMOVE
,
591 .flags
= GENL_ADMIN_PERM
,
592 .policy
= netlbl_mgmt_genl_policy
,
593 .doit
= netlbl_mgmt_remove
,
597 static struct genl_ops netlbl_mgmt_genl_c_listall
= {
598 .cmd
= NLBL_MGMT_C_LISTALL
,
600 .policy
= netlbl_mgmt_genl_policy
,
602 .dumpit
= netlbl_mgmt_listall
,
605 static struct genl_ops netlbl_mgmt_genl_c_adddef
= {
606 .cmd
= NLBL_MGMT_C_ADDDEF
,
607 .flags
= GENL_ADMIN_PERM
,
608 .policy
= netlbl_mgmt_genl_policy
,
609 .doit
= netlbl_mgmt_adddef
,
613 static struct genl_ops netlbl_mgmt_genl_c_removedef
= {
614 .cmd
= NLBL_MGMT_C_REMOVEDEF
,
615 .flags
= GENL_ADMIN_PERM
,
616 .policy
= netlbl_mgmt_genl_policy
,
617 .doit
= netlbl_mgmt_removedef
,
621 static struct genl_ops netlbl_mgmt_genl_c_listdef
= {
622 .cmd
= NLBL_MGMT_C_LISTDEF
,
624 .policy
= netlbl_mgmt_genl_policy
,
625 .doit
= netlbl_mgmt_listdef
,
629 static struct genl_ops netlbl_mgmt_genl_c_protocols
= {
630 .cmd
= NLBL_MGMT_C_PROTOCOLS
,
632 .policy
= netlbl_mgmt_genl_policy
,
634 .dumpit
= netlbl_mgmt_protocols
,
637 static struct genl_ops netlbl_mgmt_genl_c_version
= {
638 .cmd
= NLBL_MGMT_C_VERSION
,
640 .policy
= netlbl_mgmt_genl_policy
,
641 .doit
= netlbl_mgmt_version
,
646 * NetLabel Generic NETLINK Protocol Functions
650 * netlbl_mgmt_genl_init - Register the NetLabel management component
653 * Register the NetLabel management component with the Generic NETLINK
654 * mechanism. Returns zero on success, negative values on failure.
657 int netlbl_mgmt_genl_init(void)
661 ret_val
= genl_register_family(&netlbl_mgmt_gnl_family
);
665 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
666 &netlbl_mgmt_genl_c_add
);
669 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
670 &netlbl_mgmt_genl_c_remove
);
673 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
674 &netlbl_mgmt_genl_c_listall
);
677 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
678 &netlbl_mgmt_genl_c_adddef
);
681 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
682 &netlbl_mgmt_genl_c_removedef
);
685 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
686 &netlbl_mgmt_genl_c_listdef
);
689 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
690 &netlbl_mgmt_genl_c_protocols
);
693 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
694 &netlbl_mgmt_genl_c_version
);