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>
40 #include <asm/atomic.h>
42 #include "netlabel_domainhash.h"
43 #include "netlabel_user.h"
44 #include "netlabel_mgmt.h"
46 /* NetLabel configured protocol counter */
47 atomic_t netlabel_mgmt_protocount
= ATOMIC_INIT(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 Command Handlers
78 * netlbl_mgmt_add - Handle an ADD message
79 * @skb: the NETLINK buffer
80 * @info: the Generic NETLINK info block
83 * Process a user generated ADD message and add the domains from the message
84 * to the hash table. See netlabel.h for a description of the message format.
85 * Returns zero on success, negative values on failure.
88 static int netlbl_mgmt_add(struct sk_buff
*skb
, struct genl_info
*info
)
90 int ret_val
= -EINVAL
;
91 struct netlbl_dom_map
*entry
= NULL
;
94 struct netlbl_audit audit_info
;
96 if (!info
->attrs
[NLBL_MGMT_A_DOMAIN
] ||
97 !info
->attrs
[NLBL_MGMT_A_PROTOCOL
])
100 netlbl_netlink_auditinfo(skb
, &audit_info
);
102 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
107 tmp_size
= nla_len(info
->attrs
[NLBL_MGMT_A_DOMAIN
]);
108 entry
->domain
= kmalloc(tmp_size
, GFP_KERNEL
);
109 if (entry
->domain
== NULL
) {
113 entry
->type
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_PROTOCOL
]);
114 nla_strlcpy(entry
->domain
, info
->attrs
[NLBL_MGMT_A_DOMAIN
], tmp_size
);
116 switch (entry
->type
) {
117 case NETLBL_NLTYPE_UNLABELED
:
118 ret_val
= netlbl_domhsh_add(entry
, &audit_info
);
120 case NETLBL_NLTYPE_CIPSOV4
:
121 if (!info
->attrs
[NLBL_MGMT_A_CV4DOI
])
124 tmp_val
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_CV4DOI
]);
125 /* We should be holding a rcu_read_lock() here while we hold
126 * the result but since the entry will always be deleted when
127 * the CIPSO DOI is deleted we aren't going to keep the
130 entry
->type_def
.cipsov4
= cipso_v4_doi_getdef(tmp_val
);
131 if (entry
->type_def
.cipsov4
== NULL
) {
135 ret_val
= netlbl_domhsh_add(entry
, &audit_info
);
148 kfree(entry
->domain
);
154 * netlbl_mgmt_remove - Handle a REMOVE message
155 * @skb: the NETLINK buffer
156 * @info: the Generic NETLINK info block
159 * Process a user generated REMOVE message and remove the specified domain
160 * mappings. Returns zero on success, negative values on failure.
163 static int netlbl_mgmt_remove(struct sk_buff
*skb
, struct genl_info
*info
)
166 struct netlbl_audit audit_info
;
168 if (!info
->attrs
[NLBL_MGMT_A_DOMAIN
])
171 netlbl_netlink_auditinfo(skb
, &audit_info
);
173 domain
= nla_data(info
->attrs
[NLBL_MGMT_A_DOMAIN
]);
174 return netlbl_domhsh_remove(domain
, &audit_info
);
178 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
179 * @entry: the domain mapping hash table entry
180 * @arg: the netlbl_domhsh_walk_arg structure
183 * This function is designed to be used as a callback to the
184 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
185 * message. Returns the size of the message on success, negative values on
189 static int netlbl_mgmt_listall_cb(struct netlbl_dom_map
*entry
, void *arg
)
191 int ret_val
= -ENOMEM
;
192 struct netlbl_domhsh_walk_arg
*cb_arg
= arg
;
195 data
= genlmsg_put(cb_arg
->skb
, NETLINK_CB(cb_arg
->nl_cb
->skb
).pid
,
196 cb_arg
->seq
, &netlbl_mgmt_gnl_family
,
197 NLM_F_MULTI
, NLBL_MGMT_C_LISTALL
);
199 goto listall_cb_failure
;
201 ret_val
= nla_put_string(cb_arg
->skb
,
205 goto listall_cb_failure
;
206 ret_val
= nla_put_u32(cb_arg
->skb
, NLBL_MGMT_A_PROTOCOL
, entry
->type
);
208 goto listall_cb_failure
;
209 switch (entry
->type
) {
210 case NETLBL_NLTYPE_CIPSOV4
:
211 ret_val
= nla_put_u32(cb_arg
->skb
,
213 entry
->type_def
.cipsov4
->doi
);
215 goto listall_cb_failure
;
220 return genlmsg_end(cb_arg
->skb
, data
);
223 genlmsg_cancel(cb_arg
->skb
, data
);
228 * netlbl_mgmt_listall - Handle a LISTALL message
229 * @skb: the NETLINK buffer
230 * @cb: the NETLINK callback
233 * Process a user generated LISTALL message and dumps the domain hash table in
234 * a form suitable for use in a kernel generated LISTALL message. Returns zero
235 * on success, negative values on failure.
238 static int netlbl_mgmt_listall(struct sk_buff
*skb
,
239 struct netlink_callback
*cb
)
241 struct netlbl_domhsh_walk_arg cb_arg
;
242 u32 skip_bkt
= cb
->args
[0];
243 u32 skip_chain
= cb
->args
[1];
247 cb_arg
.seq
= cb
->nlh
->nlmsg_seq
;
249 netlbl_domhsh_walk(&skip_bkt
,
251 netlbl_mgmt_listall_cb
,
254 cb
->args
[0] = skip_bkt
;
255 cb
->args
[1] = skip_chain
;
260 * netlbl_mgmt_adddef - Handle an ADDDEF message
261 * @skb: the NETLINK buffer
262 * @info: the Generic NETLINK info block
265 * Process a user generated ADDDEF message and respond accordingly. Returns
266 * zero on success, negative values on failure.
269 static int netlbl_mgmt_adddef(struct sk_buff
*skb
, struct genl_info
*info
)
271 int ret_val
= -EINVAL
;
272 struct netlbl_dom_map
*entry
= NULL
;
274 struct netlbl_audit audit_info
;
276 if (!info
->attrs
[NLBL_MGMT_A_PROTOCOL
])
279 netlbl_netlink_auditinfo(skb
, &audit_info
);
281 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
286 entry
->type
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_PROTOCOL
]);
288 switch (entry
->type
) {
289 case NETLBL_NLTYPE_UNLABELED
:
290 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
292 case NETLBL_NLTYPE_CIPSOV4
:
293 if (!info
->attrs
[NLBL_MGMT_A_CV4DOI
])
296 tmp_val
= nla_get_u32(info
->attrs
[NLBL_MGMT_A_CV4DOI
]);
297 /* We should be holding a rcu_read_lock() here while we hold
298 * the result but since the entry will always be deleted when
299 * the CIPSO DOI is deleted we aren't going to keep the
302 entry
->type_def
.cipsov4
= cipso_v4_doi_getdef(tmp_val
);
303 if (entry
->type_def
.cipsov4
== NULL
) {
307 ret_val
= netlbl_domhsh_add_default(entry
, &audit_info
);
324 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
325 * @skb: the NETLINK buffer
326 * @info: the Generic NETLINK info block
329 * Process a user generated REMOVEDEF message and remove the default domain
330 * mapping. Returns zero on success, negative values on failure.
333 static int netlbl_mgmt_removedef(struct sk_buff
*skb
, struct genl_info
*info
)
335 struct netlbl_audit audit_info
;
337 netlbl_netlink_auditinfo(skb
, &audit_info
);
339 return netlbl_domhsh_remove_default(&audit_info
);
343 * netlbl_mgmt_listdef - Handle a LISTDEF message
344 * @skb: the NETLINK buffer
345 * @info: the Generic NETLINK info block
348 * Process a user generated LISTDEF message and dumps the default domain
349 * mapping in a form suitable for use in a kernel generated LISTDEF message.
350 * Returns zero on success, negative values on failure.
353 static int netlbl_mgmt_listdef(struct sk_buff
*skb
, struct genl_info
*info
)
355 int ret_val
= -ENOMEM
;
356 struct sk_buff
*ans_skb
= NULL
;
358 struct netlbl_dom_map
*entry
;
360 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
363 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_mgmt_gnl_family
,
364 0, NLBL_MGMT_C_LISTDEF
);
366 goto listdef_failure
;
369 entry
= netlbl_domhsh_getentry(NULL
);
372 goto listdef_failure_lock
;
374 ret_val
= nla_put_u32(ans_skb
, NLBL_MGMT_A_PROTOCOL
, entry
->type
);
376 goto listdef_failure_lock
;
377 switch (entry
->type
) {
378 case NETLBL_NLTYPE_CIPSOV4
:
379 ret_val
= nla_put_u32(ans_skb
,
381 entry
->type_def
.cipsov4
->doi
);
383 goto listdef_failure_lock
;
388 genlmsg_end(ans_skb
, data
);
390 ret_val
= genlmsg_reply(ans_skb
, info
);
392 goto listdef_failure
;
395 listdef_failure_lock
:
403 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
404 * @skb: the skb to write to
405 * @seq: the NETLINK sequence number
406 * @cb: the NETLINK callback
407 * @protocol: the NetLabel protocol to use in the message
410 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
411 * answer a application's PROTOCOLS message. Returns the size of the message
412 * on success, negative values on failure.
415 static int netlbl_mgmt_protocols_cb(struct sk_buff
*skb
,
416 struct netlink_callback
*cb
,
419 int ret_val
= -ENOMEM
;
422 data
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
,
423 &netlbl_mgmt_gnl_family
, NLM_F_MULTI
,
424 NLBL_MGMT_C_PROTOCOLS
);
426 goto protocols_cb_failure
;
428 ret_val
= nla_put_u32(skb
, NLBL_MGMT_A_PROTOCOL
, protocol
);
430 goto protocols_cb_failure
;
432 return genlmsg_end(skb
, data
);
434 protocols_cb_failure
:
435 genlmsg_cancel(skb
, data
);
440 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
441 * @skb: the NETLINK buffer
442 * @cb: the NETLINK callback
445 * Process a user generated PROTOCOLS message and respond accordingly.
448 static int netlbl_mgmt_protocols(struct sk_buff
*skb
,
449 struct netlink_callback
*cb
)
451 u32 protos_sent
= cb
->args
[0];
453 if (protos_sent
== 0) {
454 if (netlbl_mgmt_protocols_cb(skb
,
456 NETLBL_NLTYPE_UNLABELED
) < 0)
457 goto protocols_return
;
460 if (protos_sent
== 1) {
461 if (netlbl_mgmt_protocols_cb(skb
,
463 NETLBL_NLTYPE_CIPSOV4
) < 0)
464 goto protocols_return
;
469 cb
->args
[0] = protos_sent
;
474 * netlbl_mgmt_version - Handle a VERSION message
475 * @skb: the NETLINK buffer
476 * @info: the Generic NETLINK info block
479 * Process a user generated VERSION message and respond accordingly. Returns
480 * zero on success, negative values on failure.
483 static int netlbl_mgmt_version(struct sk_buff
*skb
, struct genl_info
*info
)
485 int ret_val
= -ENOMEM
;
486 struct sk_buff
*ans_skb
= NULL
;
489 ans_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
492 data
= genlmsg_put_reply(ans_skb
, info
, &netlbl_mgmt_gnl_family
,
493 0, NLBL_MGMT_C_VERSION
);
495 goto version_failure
;
497 ret_val
= nla_put_u32(ans_skb
,
499 NETLBL_PROTO_VERSION
);
501 goto version_failure
;
503 genlmsg_end(ans_skb
, data
);
505 ret_val
= genlmsg_reply(ans_skb
, info
);
507 goto version_failure
;
517 * NetLabel Generic NETLINK Command Definitions
520 static struct genl_ops netlbl_mgmt_genl_ops
[] = {
522 .cmd
= NLBL_MGMT_C_ADD
,
523 .flags
= GENL_ADMIN_PERM
,
524 .policy
= netlbl_mgmt_genl_policy
,
525 .doit
= netlbl_mgmt_add
,
529 .cmd
= NLBL_MGMT_C_REMOVE
,
530 .flags
= GENL_ADMIN_PERM
,
531 .policy
= netlbl_mgmt_genl_policy
,
532 .doit
= netlbl_mgmt_remove
,
536 .cmd
= NLBL_MGMT_C_LISTALL
,
538 .policy
= netlbl_mgmt_genl_policy
,
540 .dumpit
= netlbl_mgmt_listall
,
543 .cmd
= NLBL_MGMT_C_ADDDEF
,
544 .flags
= GENL_ADMIN_PERM
,
545 .policy
= netlbl_mgmt_genl_policy
,
546 .doit
= netlbl_mgmt_adddef
,
550 .cmd
= NLBL_MGMT_C_REMOVEDEF
,
551 .flags
= GENL_ADMIN_PERM
,
552 .policy
= netlbl_mgmt_genl_policy
,
553 .doit
= netlbl_mgmt_removedef
,
557 .cmd
= NLBL_MGMT_C_LISTDEF
,
559 .policy
= netlbl_mgmt_genl_policy
,
560 .doit
= netlbl_mgmt_listdef
,
564 .cmd
= NLBL_MGMT_C_PROTOCOLS
,
566 .policy
= netlbl_mgmt_genl_policy
,
568 .dumpit
= netlbl_mgmt_protocols
,
571 .cmd
= NLBL_MGMT_C_VERSION
,
573 .policy
= netlbl_mgmt_genl_policy
,
574 .doit
= netlbl_mgmt_version
,
580 * NetLabel Generic NETLINK Protocol Functions
584 * netlbl_mgmt_genl_init - Register the NetLabel management component
587 * Register the NetLabel management component with the Generic NETLINK
588 * mechanism. Returns zero on success, negative values on failure.
591 int __init
netlbl_mgmt_genl_init(void)
595 ret_val
= genl_register_family(&netlbl_mgmt_gnl_family
);
599 for (i
= 0; i
< ARRAY_SIZE(netlbl_mgmt_genl_ops
); i
++) {
600 ret_val
= genl_register_ops(&netlbl_mgmt_gnl_family
,
601 &netlbl_mgmt_genl_ops
[i
]);