2 * NetLabel CIPSO/IPv4 Support
4 * This file defines the CIPSO/IPv4 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>
35 #include <linux/audit.h>
37 #include <net/netlink.h>
38 #include <net/genetlink.h>
39 #include <net/netlabel.h>
40 #include <net/cipso_ipv4.h>
42 #include "netlabel_user.h"
43 #include "netlabel_cipso_v4.h"
45 /* Argument struct for cipso_v4_doi_walk() */
46 struct netlbl_cipsov4_doiwalk_arg {
47 struct netlink_callback *nl_cb;
52 /* NetLabel Generic NETLINK CIPSOv4 family */
53 static struct genl_family netlbl_cipsov4_gnl_family = {
54 .id = GENL_ID_GENERATE,
56 .name = NETLBL_NLTYPE_CIPSOV4_NAME,
57 .version = NETLBL_PROTO_VERSION,
58 .maxattr = NLBL_CIPSOV4_A_MAX,
61 /* NetLabel Netlink attribute policy */
62 static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
63 [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
64 [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
65 [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
66 [NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
67 [NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
68 [NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
69 [NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
70 [NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
71 [NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
72 [NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
73 [NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
74 [NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
82 * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition
83 * @entry: the entry's RCU field
86 * This function is designed to be used as a callback to the call_rcu()
87 * function so that the memory allocated to the DOI definition can be released
91 static void netlbl_cipsov4_doi_free(struct rcu_head *entry)
93 struct cipso_v4_doi *ptr;
95 ptr = container_of(entry, struct cipso_v4_doi, rcu);
97 case CIPSO_V4_MAP_STD:
98 kfree(ptr->map.std->lvl.cipso);
99 kfree(ptr->map.std->lvl.local);
100 kfree(ptr->map.std->cat.cipso);
101 kfree(ptr->map.std->cat.local);
108 * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
109 * @info: the Generic NETLINK info block
110 * @doi_def: the CIPSO V4 DOI definition
113 * Parse the common sections of a ADD message and fill in the related values
114 * in @doi_def. Returns zero on success, negative values on failure.
117 static int netlbl_cipsov4_add_common(struct genl_info *info,
118 struct cipso_v4_doi *doi_def)
124 doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
126 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
128 netlbl_cipsov4_genl_policy) != 0)
131 nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
132 if (nla->nla_type == NLBL_CIPSOV4_A_TAG) {
133 if (iter >= CIPSO_V4_TAG_MAXCNT)
135 doi_def->tags[iter++] = nla_get_u8(nla);
137 while (iter < CIPSO_V4_TAG_MAXCNT)
138 doi_def->tags[iter++] = CIPSO_V4_TAG_INVALID;
144 * NetLabel Command Handlers
148 * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
149 * @info: the Generic NETLINK info block
152 * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message
153 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
157 static int netlbl_cipsov4_add_std(struct genl_info *info)
159 int ret_val = -EINVAL;
160 struct cipso_v4_doi *doi_def = NULL;
161 struct nlattr *nla_a;
162 struct nlattr *nla_b;
167 if (!info->attrs[NLBL_CIPSOV4_A_TAGLST] ||
168 !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST])
171 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
173 netlbl_cipsov4_genl_policy) != 0)
176 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
179 doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
180 if (doi_def->map.std == NULL) {
182 goto add_std_failure;
184 doi_def->type = CIPSO_V4_MAP_STD;
186 ret_val = netlbl_cipsov4_add_common(info, doi_def);
188 goto add_std_failure;
191 nla_for_each_nested(nla_a,
192 info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
194 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) {
195 if (nla_validate_nested(nla_a,
197 netlbl_cipsov4_genl_policy) != 0)
198 goto add_std_failure;
199 nla_for_each_nested(nla_b, nla_a, nla_b_rem)
200 switch (nla_b->nla_type) {
201 case NLBL_CIPSOV4_A_MLSLVLLOC:
202 if (nla_get_u32(nla_b) >
203 CIPSO_V4_MAX_LOC_LVLS)
204 goto add_std_failure;
205 if (nla_get_u32(nla_b) >=
206 doi_def->map.std->lvl.local_size)
207 doi_def->map.std->lvl.local_size =
208 nla_get_u32(nla_b) + 1;
210 case NLBL_CIPSOV4_A_MLSLVLREM:
211 if (nla_get_u32(nla_b) >
212 CIPSO_V4_MAX_REM_LVLS)
213 goto add_std_failure;
214 if (nla_get_u32(nla_b) >=
215 doi_def->map.std->lvl.cipso_size)
216 doi_def->map.std->lvl.cipso_size =
217 nla_get_u32(nla_b) + 1;
221 doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
224 if (doi_def->map.std->lvl.local == NULL) {
226 goto add_std_failure;
228 doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
231 if (doi_def->map.std->lvl.cipso == NULL) {
233 goto add_std_failure;
235 for (iter = 0; iter < doi_def->map.std->lvl.local_size; iter++)
236 doi_def->map.std->lvl.local[iter] = CIPSO_V4_INV_LVL;
237 for (iter = 0; iter < doi_def->map.std->lvl.cipso_size; iter++)
238 doi_def->map.std->lvl.cipso[iter] = CIPSO_V4_INV_LVL;
239 nla_for_each_nested(nla_a,
240 info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
242 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) {
243 struct nlattr *lvl_loc;
244 struct nlattr *lvl_rem;
246 lvl_loc = nla_find_nested(nla_a,
247 NLBL_CIPSOV4_A_MLSLVLLOC);
248 lvl_rem = nla_find_nested(nla_a,
249 NLBL_CIPSOV4_A_MLSLVLREM);
250 if (lvl_loc == NULL || lvl_rem == NULL)
251 goto add_std_failure;
252 doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] =
253 nla_get_u32(lvl_rem);
254 doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] =
255 nla_get_u32(lvl_loc);
258 if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) {
259 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
261 netlbl_cipsov4_genl_policy) != 0)
262 goto add_std_failure;
264 nla_for_each_nested(nla_a,
265 info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
267 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) {
268 if (nla_validate_nested(nla_a,
270 netlbl_cipsov4_genl_policy) != 0)
271 goto add_std_failure;
272 nla_for_each_nested(nla_b, nla_a, nla_b_rem)
273 switch (nla_b->nla_type) {
274 case NLBL_CIPSOV4_A_MLSCATLOC:
275 if (nla_get_u32(nla_b) >
276 CIPSO_V4_MAX_LOC_CATS)
277 goto add_std_failure;
278 if (nla_get_u32(nla_b) >=
279 doi_def->map.std->cat.local_size)
280 doi_def->map.std->cat.local_size =
281 nla_get_u32(nla_b) + 1;
283 case NLBL_CIPSOV4_A_MLSCATREM:
284 if (nla_get_u32(nla_b) >
285 CIPSO_V4_MAX_REM_CATS)
286 goto add_std_failure;
287 if (nla_get_u32(nla_b) >=
288 doi_def->map.std->cat.cipso_size)
289 doi_def->map.std->cat.cipso_size =
290 nla_get_u32(nla_b) + 1;
294 doi_def->map.std->cat.local = kcalloc(
295 doi_def->map.std->cat.local_size,
298 if (doi_def->map.std->cat.local == NULL) {
300 goto add_std_failure;
302 doi_def->map.std->cat.cipso = kcalloc(
303 doi_def->map.std->cat.cipso_size,
306 if (doi_def->map.std->cat.cipso == NULL) {
308 goto add_std_failure;
310 for (iter = 0; iter < doi_def->map.std->cat.local_size; iter++)
311 doi_def->map.std->cat.local[iter] = CIPSO_V4_INV_CAT;
312 for (iter = 0; iter < doi_def->map.std->cat.cipso_size; iter++)
313 doi_def->map.std->cat.cipso[iter] = CIPSO_V4_INV_CAT;
314 nla_for_each_nested(nla_a,
315 info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
317 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) {
318 struct nlattr *cat_loc;
319 struct nlattr *cat_rem;
321 cat_loc = nla_find_nested(nla_a,
322 NLBL_CIPSOV4_A_MLSCATLOC);
323 cat_rem = nla_find_nested(nla_a,
324 NLBL_CIPSOV4_A_MLSCATREM);
325 if (cat_loc == NULL || cat_rem == NULL)
326 goto add_std_failure;
327 doi_def->map.std->cat.local[
328 nla_get_u32(cat_loc)] =
329 nla_get_u32(cat_rem);
330 doi_def->map.std->cat.cipso[
331 nla_get_u32(cat_rem)] =
332 nla_get_u32(cat_loc);
336 ret_val = cipso_v4_doi_add(doi_def);
338 goto add_std_failure;
343 netlbl_cipsov4_doi_free(&doi_def->rcu);
348 * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
349 * @info: the Generic NETLINK info block
352 * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
353 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
357 static int netlbl_cipsov4_add_pass(struct genl_info *info)
360 struct cipso_v4_doi *doi_def = NULL;
362 if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
365 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
368 doi_def->type = CIPSO_V4_MAP_PASS;
370 ret_val = netlbl_cipsov4_add_common(info, doi_def);
372 goto add_pass_failure;
374 ret_val = cipso_v4_doi_add(doi_def);
376 goto add_pass_failure;
380 netlbl_cipsov4_doi_free(&doi_def->rcu);
385 * netlbl_cipsov4_add - Handle an ADD message
386 * @skb: the NETLINK buffer
387 * @info: the Generic NETLINK info block
390 * Create a new DOI definition based on the given ADD message and add it to the
391 * CIPSO V4 engine. Returns zero on success, negative values on failure.
394 static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
397 int ret_val = -EINVAL;
400 const char *type_str = "(unknown)";
401 struct audit_buffer *audit_buf;
402 struct netlbl_audit audit_info;
404 if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
405 !info->attrs[NLBL_CIPSOV4_A_MTYPE])
408 doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
409 netlbl_netlink_auditinfo(skb, &audit_info);
411 type = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE]);
413 case CIPSO_V4_MAP_STD:
415 ret_val = netlbl_cipsov4_add_std(info);
417 case CIPSO_V4_MAP_PASS:
419 ret_val = netlbl_cipsov4_add_pass(info);
423 audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
425 if (audit_buf != NULL) {
426 audit_log_format(audit_buf,
427 " cipso_doi=%u cipso_type=%s res=%u",
430 ret_val == 0 ? 1 : 0);
431 audit_log_end(audit_buf);
438 * netlbl_cipsov4_list - Handle a LIST message
439 * @skb: the NETLINK buffer
440 * @info: the Generic NETLINK info block
443 * Process a user generated LIST message and respond accordingly. While the
444 * response message generated by the kernel is straightforward, determining
445 * before hand the size of the buffer to allocate is not (we have to generate
446 * the message to know the size). In order to keep this function sane what we
447 * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
448 * that size, if we fail then we restart with a larger buffer and try again.
449 * We continue in this manner until we hit a limit of failed attempts then we
450 * give up and just send an error message. Returns zero on success and
451 * negative values on error.
454 static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
457 struct sk_buff *ans_skb = NULL;
461 struct nlattr *nla_a;
462 struct nlattr *nla_b;
463 struct cipso_v4_doi *doi_def;
466 if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
472 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
473 if (ans_skb == NULL) {
477 data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
478 0, NLBL_CIPSOV4_C_LIST);
484 doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
487 doi_def = cipso_v4_doi_getdef(doi);
488 if (doi_def == NULL) {
493 ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
495 goto list_failure_lock;
497 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
500 goto list_failure_lock;
503 iter < CIPSO_V4_TAG_MAXCNT &&
504 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
506 ret_val = nla_put_u8(ans_skb,
508 doi_def->tags[iter]);
510 goto list_failure_lock;
512 nla_nest_end(ans_skb, nla_a);
514 switch (doi_def->type) {
515 case CIPSO_V4_MAP_STD:
516 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
519 goto list_failure_lock;
522 iter < doi_def->map.std->lvl.local_size;
524 if (doi_def->map.std->lvl.local[iter] ==
528 nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
533 ret_val = nla_put_u32(ans_skb,
534 NLBL_CIPSOV4_A_MLSLVLLOC,
538 ret_val = nla_put_u32(ans_skb,
539 NLBL_CIPSOV4_A_MLSLVLREM,
540 doi_def->map.std->lvl.local[iter]);
543 nla_nest_end(ans_skb, nla_b);
545 nla_nest_end(ans_skb, nla_a);
547 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
553 iter < doi_def->map.std->cat.local_size;
555 if (doi_def->map.std->cat.local[iter] ==
559 nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
564 ret_val = nla_put_u32(ans_skb,
565 NLBL_CIPSOV4_A_MLSCATLOC,
569 ret_val = nla_put_u32(ans_skb,
570 NLBL_CIPSOV4_A_MLSCATREM,
571 doi_def->map.std->cat.local[iter]);
574 nla_nest_end(ans_skb, nla_b);
576 nla_nest_end(ans_skb, nla_a);
582 genlmsg_end(ans_skb, data);
584 ret_val = genlmsg_reply(ans_skb, info);
591 /* XXX - this limit is a guesstimate */
592 if (nlsze_mult < 4) {
606 * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
607 * @doi_def: the CIPSOv4 DOI definition
608 * @arg: the netlbl_cipsov4_doiwalk_arg structure
611 * This function is designed to be used as a callback to the
612 * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
613 * message. Returns the size of the message on success, negative values on
617 static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
619 int ret_val = -ENOMEM;
620 struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
623 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
624 cb_arg->seq, &netlbl_cipsov4_gnl_family,
625 NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
627 goto listall_cb_failure;
629 ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
631 goto listall_cb_failure;
632 ret_val = nla_put_u32(cb_arg->skb,
633 NLBL_CIPSOV4_A_MTYPE,
636 goto listall_cb_failure;
638 return genlmsg_end(cb_arg->skb, data);
641 genlmsg_cancel(cb_arg->skb, data);
646 * netlbl_cipsov4_listall - Handle a LISTALL message
647 * @skb: the NETLINK buffer
648 * @cb: the NETLINK callback
651 * Process a user generated LISTALL message and respond accordingly. Returns
652 * zero on success and negative values on error.
655 static int netlbl_cipsov4_listall(struct sk_buff *skb,
656 struct netlink_callback *cb)
658 struct netlbl_cipsov4_doiwalk_arg cb_arg;
659 int doi_skip = cb->args[0];
663 cb_arg.seq = cb->nlh->nlmsg_seq;
665 cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
667 cb->args[0] = doi_skip;
672 * netlbl_cipsov4_remove - Handle a REMOVE message
673 * @skb: the NETLINK buffer
674 * @info: the Generic NETLINK info block
677 * Process a user generated REMOVE message and respond accordingly. Returns
678 * zero on success, negative values on failure.
681 static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
683 int ret_val = -EINVAL;
685 struct audit_buffer *audit_buf;
686 struct netlbl_audit audit_info;
688 if (!info->attrs[NLBL_CIPSOV4_A_DOI])
691 doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
692 netlbl_netlink_auditinfo(skb, &audit_info);
694 ret_val = cipso_v4_doi_remove(doi,
696 netlbl_cipsov4_doi_free);
698 audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_DEL,
700 if (audit_buf != NULL) {
701 audit_log_format(audit_buf,
702 " cipso_doi=%u res=%u",
704 ret_val == 0 ? 1 : 0);
705 audit_log_end(audit_buf);
712 * NetLabel Generic NETLINK Command Definitions
715 static struct genl_ops netlbl_cipsov4_genl_c_add = {
716 .cmd = NLBL_CIPSOV4_C_ADD,
717 .flags = GENL_ADMIN_PERM,
718 .policy = netlbl_cipsov4_genl_policy,
719 .doit = netlbl_cipsov4_add,
723 static struct genl_ops netlbl_cipsov4_genl_c_remove = {
724 .cmd = NLBL_CIPSOV4_C_REMOVE,
725 .flags = GENL_ADMIN_PERM,
726 .policy = netlbl_cipsov4_genl_policy,
727 .doit = netlbl_cipsov4_remove,
731 static struct genl_ops netlbl_cipsov4_genl_c_list = {
732 .cmd = NLBL_CIPSOV4_C_LIST,
734 .policy = netlbl_cipsov4_genl_policy,
735 .doit = netlbl_cipsov4_list,
739 static struct genl_ops netlbl_cipsov4_genl_c_listall = {
740 .cmd = NLBL_CIPSOV4_C_LISTALL,
742 .policy = netlbl_cipsov4_genl_policy,
744 .dumpit = netlbl_cipsov4_listall,
748 * NetLabel Generic NETLINK Protocol Functions
752 * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
755 * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
756 * mechanism. Returns zero on success, negative values on failure.
759 int netlbl_cipsov4_genl_init(void)
763 ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
767 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
768 &netlbl_cipsov4_genl_c_add);
771 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
772 &netlbl_cipsov4_genl_c_remove);
775 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
776 &netlbl_cipsov4_genl_c_list);
779 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
780 &netlbl_cipsov4_genl_c_listall);