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)
88 spin_lock(&netlabel_mgmt_protocount_lock
);
89 netlabel_mgmt_protocount
++;
90 spin_unlock(&netlabel_mgmt_protocount_lock
);
94 * netlbl_mgmt_protocount_dec - Decrement the configured labeled protocol count
97 * Decrement the number of labeled protocol configurations in the current
98 * NetLabel configuration. Keep track of this for use in determining if
99 * NetLabel label enforcement should be active/enabled or not in the LSM.
102 void netlbl_mgmt_protocount_dec(void)
104 spin_lock(&netlabel_mgmt_protocount_lock
);
105 if (netlabel_mgmt_protocount
> 0)
106 netlabel_mgmt_protocount
--;
107 spin_unlock(&netlabel_mgmt_protocount_lock
);
111 * netlbl_mgmt_protocount_value - Return the number of configured protocols
114 * Return the number of labeled protocols in the current NetLabel
115 * configuration. This value is useful in determining if NetLabel label
116 * enforcement should be active/enabled or not in the LSM.
119 u32
netlbl_mgmt_protocount_value(void)
124 val
= netlabel_mgmt_protocount
;
131 * NetLabel Command Handlers
135 * netlbl_mgmt_add - Handle an ADD message
136 * @skb: the NETLINK buffer
137 * @info: the Generic NETLINK info block
140 * Process a user generated ADD message and add the domains from the message
141 * to the hash table. See netlabel.h for a description of the message format.
142 * Returns zero on success, negative values on failure.
145 static int netlbl_mgmt_add(struct sk_buff
*skb
, struct genl_info
*info
)
147 int ret_val
= -EINVAL
;
148 struct netlbl_dom_map
*entry
= NULL
;
151 struct netlbl_audit audit_info
;
153 if (!info
->attrs
[NLBL_MGMT_A_DOMAIN
] ||
154 !info
->attrs
[NLBL_MGMT_A_PROTOCOL
])
157 netlbl_netlink_auditinfo(skb
, &audit_info
);
159 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
164 tmp_size
= nla_len(info
->attrs
[NLBL_MGMT_A_DOMAIN
]);
165 entry
->domain
= kmalloc(tmp_size
, GFP_KERNEL
);
166 if (entry
->domain
== NULL
) {
170 entry
->type
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_PROTOCOL
]);
171 nla_strlcpy(entry
->domain
, info
->attrs
[NLBL_MGMT_A_DOMAIN
], tmp_size
);
173 switch (entry
->type
) {
174 case NETLBL_NLTYPE_UNLABELED
:
175 ret_val
= netlbl_domhsh_add(entry
, &audit_info
);
177 case NETLBL_NLTYPE_CIPSOV4
:
178 if (!info
->attrs
[NLBL_MGMT_A_CV4DOI
])
181 tmp_val
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_CV4DOI
]);
182 /* We should be holding a rcu_read_lock() here while we hold
183 * the result but since the entry will always be deleted when
184 * the CIPSO DOI is deleted we aren't going to keep the
187 entry
->type_def
.cipsov4
= cipso_v4_doi_getdef(tmp_val
);
188 if (entry
->type_def
.cipsov4
== NULL
) {
192 ret_val
= netlbl_domhsh_add(entry
, &audit_info
);
205 kfree(entry
->domain
);
211 * netlbl_mgmt_remove - Handle a REMOVE message
212 * @skb: the NETLINK buffer
213 * @info: the Generic NETLINK info block
216 * Process a user generated REMOVE message and remove the specified domain
217 * mappings. Returns zero on success, negative values on failure.
220 static int netlbl_mgmt_remove(struct sk_buff
*skb
, struct genl_info
*info
)
223 struct netlbl_audit audit_info
;
225 if (!info
->attrs
[NLBL_MGMT_A_DOMAIN
])
228 netlbl_netlink_auditinfo(skb
, &audit_info
);
230 domain
= nla_data(info
->attrs
[NLBL_MGMT_A_DOMAIN
]);
231 return netlbl_domhsh_remove(domain
, &audit_info
);
235 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
236 * @entry: the domain mapping hash table entry
237 * @arg: the netlbl_domhsh_walk_arg structure
240 * This function is designed to be used as a callback to the
241 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
242 * message. Returns the size of the message on success, negative values on
246 static int netlbl_mgmt_listall_cb(struct netlbl_dom_map
*entry
, void *arg
)
248 int ret_val
= -ENOMEM
;
249 struct netlbl_domhsh_walk_arg
*cb_arg
= arg
;
252 data
= genlmsg_put(cb_arg
->skb
, NETLINK_CB(cb_arg
->nl_cb
->skb
).pid
,
253 cb_arg
->seq
, &netlbl_mgmt_gnl_family
,
254 NLM_F_MULTI
, NLBL_MGMT_C_LISTALL
);
256 goto listall_cb_failure
;
258 ret_val
= nla_put_string(cb_arg
->skb
,
262 goto listall_cb_failure
;
263 ret_val
= nla_put_u32(cb_arg
->skb
, NLBL_MGMT_A_PROTOCOL
, entry
->type
);
265 goto listall_cb_failure
;
266 switch (entry
->type
) {
267 case NETLBL_NLTYPE_CIPSOV4
:
268 ret_val
= nla_put_u32(cb_arg
->skb
,
270 entry
->type_def
.cipsov4
->doi
);
272 goto listall_cb_failure
;
277 return genlmsg_end(cb_arg
->skb
, data
);
280 genlmsg_cancel(cb_arg
->skb
, data
);
285 * netlbl_mgmt_listall - Handle a LISTALL message
286 * @skb: the NETLINK buffer
287 * @cb: the NETLINK callback
290 * Process a user generated LISTALL message and dumps the domain hash table in
291 * a form suitable for use in a kernel generated LISTALL message. Returns zero
292 * on success, negative values on failure.
295 static int netlbl_mgmt_listall(struct sk_buff
*skb
,
296 struct netlink_callback
*cb
)
298 struct netlbl_domhsh_walk_arg cb_arg
;
299 u32 skip_bkt
= cb
->args
[0];
300 u32 skip_chain
= cb
->args
[1];
304 cb_arg
.seq
= cb
->nlh
->nlmsg_seq
;
306 netlbl_domhsh_walk(&skip_bkt
,
308 netlbl_mgmt_listall_cb
,
311 cb
->args
[0] = skip_bkt
;
312 cb
->args
[1] = skip_chain
;
317 * netlbl_mgmt_adddef - Handle an ADDDEF message
318 * @skb: the NETLINK buffer
319 * @info: the Generic NETLINK info block
322 * Process a user generated ADDDEF message and respond accordingly. Returns
323 * zero on success, negative values on failure.
326 static int netlbl_mgmt_adddef(struct sk_buff
*skb
, struct genl_info
*info
)
328 int ret_val
= -EINVAL
;
329 struct netlbl_dom_map
*entry
= NULL
;
331 struct netlbl_audit audit_info
;
333 if (!info
->attrs
[NLBL_MGMT_A_PROTOCOL
])
336 netlbl_netlink_auditinfo(skb
, &audit_info
);
338 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
343 entry
->type
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_PROTOCOL
]);
345 switch (entry
->type
) {
346 case NETLBL_NLTYPE_UNLABELED
:
347 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
349 case NETLBL_NLTYPE_CIPSOV4
:
350 if (!info
->attrs
[NLBL_MGMT_A_CV4DOI
])
353 tmp_val
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_CV4DOI
]);
354 /* We should be holding a rcu_read_lock() here while we hold
355 * the result but since the entry will always be deleted when
356 * the CIPSO DOI is deleted we aren't going to keep the
359 entry
->type_def
.cipsov4
= cipso_v4_doi_getdef(tmp_val
);
360 if (entry
->type_def
.cipsov4
== NULL
) {
364 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
381 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
382 * @skb: the NETLINK buffer
383 * @info: the Generic NETLINK info block
386 * Process a user generated REMOVEDEF message and remove the default domain
387 * mapping. Returns zero on success, negative values on failure.
390 static int netlbl_mgmt_removedef(struct sk_buff
*skb
, struct genl_info
*info
)
392 struct netlbl_audit audit_info
;
394 netlbl_netlink_auditinfo(skb
, &audit_info
);
396 return netlbl_domhsh_remove_default(&audit_info
);
400 * netlbl_mgmt_listdef - Handle a LISTDEF message
401 * @skb: the NETLINK buffer
402 * @info: the Generic NETLINK info block
405 * Process a user generated LISTDEF message and dumps the default domain
406 * mapping in a form suitable for use in a kernel generated LISTDEF message.
407 * Returns zero on success, negative values on failure.
410 static int netlbl_mgmt_listdef(struct sk_buff
*skb
, struct genl_info
*info
)
412 int ret_val
= -ENOMEM
;
413 struct sk_buff
*ans_skb
= NULL
;
415 struct netlbl_dom_map
*entry
;
417 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
420 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_mgmt_gnl_family
,
421 0, NLBL_MGMT_C_LISTDEF
);
423 goto listdef_failure
;
426 entry
= netlbl_domhsh_getentry(NULL
);
429 goto listdef_failure_lock
;
431 ret_val
= nla_put_u32(ans_skb
, NLBL_MGMT_A_PROTOCOL
, entry
->type
);
433 goto listdef_failure_lock
;
434 switch (entry
->type
) {
435 case NETLBL_NLTYPE_CIPSOV4
:
436 ret_val
= nla_put_u32(ans_skb
,
438 entry
->type_def
.cipsov4
->doi
);
440 goto listdef_failure_lock
;
445 genlmsg_end(ans_skb
, data
);
447 ret_val
= genlmsg_reply(ans_skb
, info
);
449 goto listdef_failure
;
452 listdef_failure_lock
:
460 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
461 * @skb: the skb to write to
462 * @seq: the NETLINK sequence number
463 * @cb: the NETLINK callback
464 * @protocol: the NetLabel protocol to use in the message
467 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
468 * answer a application's PROTOCOLS message. Returns the size of the message
469 * on success, negative values on failure.
472 static int netlbl_mgmt_protocols_cb(struct sk_buff
*skb
,
473 struct netlink_callback
*cb
,
476 int ret_val
= -ENOMEM
;
479 data
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
480 &netlbl_mgmt_gnl_family
, NLM_F_MULTI
,
481 NLBL_MGMT_C_PROTOCOLS
);
483 goto protocols_cb_failure
;
485 ret_val
= nla_put_u32(skb
, NLBL_MGMT_A_PROTOCOL
, protocol
);
487 goto protocols_cb_failure
;
489 return genlmsg_end(skb
, data
);
491 protocols_cb_failure
:
492 genlmsg_cancel(skb
, data
);
497 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
498 * @skb: the NETLINK buffer
499 * @cb: the NETLINK callback
502 * Process a user generated PROTOCOLS message and respond accordingly.
505 static int netlbl_mgmt_protocols(struct sk_buff
*skb
,
506 struct netlink_callback
*cb
)
508 u32 protos_sent
= cb
->args
[0];
510 if (protos_sent
== 0) {
511 if (netlbl_mgmt_protocols_cb(skb
,
513 NETLBL_NLTYPE_UNLABELED
) < 0)
514 goto protocols_return
;
517 if (protos_sent
== 1) {
518 if (netlbl_mgmt_protocols_cb(skb
,
520 NETLBL_NLTYPE_CIPSOV4
) < 0)
521 goto protocols_return
;
526 cb
->args
[0] = protos_sent
;
531 * netlbl_mgmt_version - Handle a VERSION message
532 * @skb: the NETLINK buffer
533 * @info: the Generic NETLINK info block
536 * Process a user generated VERSION message and respond accordingly. Returns
537 * zero on success, negative values on failure.
540 static int netlbl_mgmt_version(struct sk_buff
*skb
, struct genl_info
*info
)
542 int ret_val
= -ENOMEM
;
543 struct sk_buff
*ans_skb
= NULL
;
546 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
549 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_mgmt_gnl_family
,
550 0, NLBL_MGMT_C_VERSION
);
552 goto version_failure
;
554 ret_val
= nla_put_u32(ans_skb
,
556 NETLBL_PROTO_VERSION
);
558 goto version_failure
;
560 genlmsg_end(ans_skb
, data
);
562 ret_val
= genlmsg_reply(ans_skb
, info
);
564 goto version_failure
;
574 * NetLabel Generic NETLINK Command Definitions
577 static struct genl_ops netlbl_mgmt_genl_c_add
= {
578 .cmd
= NLBL_MGMT_C_ADD
,
579 .flags
= GENL_ADMIN_PERM
,
580 .policy
= netlbl_mgmt_genl_policy
,
581 .doit
= netlbl_mgmt_add
,
585 static struct genl_ops netlbl_mgmt_genl_c_remove
= {
586 .cmd
= NLBL_MGMT_C_REMOVE
,
587 .flags
= GENL_ADMIN_PERM
,
588 .policy
= netlbl_mgmt_genl_policy
,
589 .doit
= netlbl_mgmt_remove
,
593 static struct genl_ops netlbl_mgmt_genl_c_listall
= {
594 .cmd
= NLBL_MGMT_C_LISTALL
,
596 .policy
= netlbl_mgmt_genl_policy
,
598 .dumpit
= netlbl_mgmt_listall
,
601 static struct genl_ops netlbl_mgmt_genl_c_adddef
= {
602 .cmd
= NLBL_MGMT_C_ADDDEF
,
603 .flags
= GENL_ADMIN_PERM
,
604 .policy
= netlbl_mgmt_genl_policy
,
605 .doit
= netlbl_mgmt_adddef
,
609 static struct genl_ops netlbl_mgmt_genl_c_removedef
= {
610 .cmd
= NLBL_MGMT_C_REMOVEDEF
,
611 .flags
= GENL_ADMIN_PERM
,
612 .policy
= netlbl_mgmt_genl_policy
,
613 .doit
= netlbl_mgmt_removedef
,
617 static struct genl_ops netlbl_mgmt_genl_c_listdef
= {
618 .cmd
= NLBL_MGMT_C_LISTDEF
,
620 .policy
= netlbl_mgmt_genl_policy
,
621 .doit
= netlbl_mgmt_listdef
,
625 static struct genl_ops netlbl_mgmt_genl_c_protocols
= {
626 .cmd
= NLBL_MGMT_C_PROTOCOLS
,
628 .policy
= netlbl_mgmt_genl_policy
,
630 .dumpit
= netlbl_mgmt_protocols
,
633 static struct genl_ops netlbl_mgmt_genl_c_version
= {
634 .cmd
= NLBL_MGMT_C_VERSION
,
636 .policy
= netlbl_mgmt_genl_policy
,
637 .doit
= netlbl_mgmt_version
,
642 * NetLabel Generic NETLINK Protocol Functions
646 * netlbl_mgmt_genl_init - Register the NetLabel management component
649 * Register the NetLabel management component with the Generic NETLINK
650 * mechanism. Returns zero on success, negative values on failure.
653 int netlbl_mgmt_genl_init(void)
657 ret_val
= genl_register_family(&netlbl_mgmt_gnl_family
);
661 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
662 &netlbl_mgmt_genl_c_add
);
665 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
666 &netlbl_mgmt_genl_c_remove
);
669 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
670 &netlbl_mgmt_genl_c_listall
);
673 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
674 &netlbl_mgmt_genl_c_adddef
);
677 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
678 &netlbl_mgmt_genl_c_removedef
);
681 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
682 &netlbl_mgmt_genl_c_listdef
);
685 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
686 &netlbl_mgmt_genl_c_protocols
);
689 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
690 &netlbl_mgmt_genl_c_version
);