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>
36 #include <net/netlink.h>
37 #include <net/genetlink.h>
38 #include <net/netlabel.h>
39 #include <net/cipso_ipv4.h>
41 #include "netlabel_user.h"
42 #include "netlabel_cipso_v4.h"
44 /* Argument struct for cipso_v4_doi_walk() */
45 struct netlbl_cipsov4_doiwalk_arg {
46 struct netlink_callback *nl_cb;
51 /* NetLabel Generic NETLINK CIPSOv4 family */
52 static struct genl_family netlbl_cipsov4_gnl_family = {
53 .id = GENL_ID_GENERATE,
55 .name = NETLBL_NLTYPE_CIPSOV4_NAME,
56 .version = NETLBL_PROTO_VERSION,
57 .maxattr = NLBL_CIPSOV4_A_MAX,
60 /* NetLabel Netlink attribute policy */
61 static struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
62 [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
63 [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
64 [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
65 [NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
66 [NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
67 [NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
68 [NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
69 [NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
70 [NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
71 [NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
72 [NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
73 [NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
81 * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition
82 * @entry: the entry's RCU field
85 * This function is designed to be used as a callback to the call_rcu()
86 * function so that the memory allocated to the DOI definition can be released
90 static void netlbl_cipsov4_doi_free(struct rcu_head *entry)
92 struct cipso_v4_doi *ptr;
94 ptr = container_of(entry, struct cipso_v4_doi, rcu);
96 case CIPSO_V4_MAP_STD:
97 kfree(ptr->map.std->lvl.cipso);
98 kfree(ptr->map.std->lvl.local);
99 kfree(ptr->map.std->cat.cipso);
100 kfree(ptr->map.std->cat.local);
107 * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
108 * @info: the Generic NETLINK info block
109 * @doi_def: the CIPSO V4 DOI definition
112 * Parse the common sections of a ADD message and fill in the related values
113 * in @doi_def. Returns zero on success, negative values on failure.
116 static int netlbl_cipsov4_add_common(struct genl_info *info,
117 struct cipso_v4_doi *doi_def)
123 doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
125 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
127 netlbl_cipsov4_genl_policy) != 0)
130 nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
131 if (nla->nla_type == NLBL_CIPSOV4_A_TAG) {
132 if (iter > CIPSO_V4_TAG_MAXCNT)
134 doi_def->tags[iter++] = nla_get_u8(nla);
136 if (iter < CIPSO_V4_TAG_MAXCNT)
137 doi_def->tags[iter] = CIPSO_V4_TAG_INVALID;
143 * NetLabel Command Handlers
147 * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
148 * @info: the Generic NETLINK info block
151 * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message
152 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
156 static int netlbl_cipsov4_add_std(struct genl_info *info)
158 int ret_val = -EINVAL;
159 struct cipso_v4_doi *doi_def = NULL;
160 struct nlattr *nla_a;
161 struct nlattr *nla_b;
165 if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
166 !info->attrs[NLBL_CIPSOV4_A_TAGLST] ||
167 !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST])
170 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
172 netlbl_cipsov4_genl_policy) != 0)
175 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
178 doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
179 if (doi_def->map.std == NULL) {
181 goto add_std_failure;
183 doi_def->type = CIPSO_V4_MAP_STD;
185 ret_val = netlbl_cipsov4_add_common(info, doi_def);
187 goto add_std_failure;
189 nla_for_each_nested(nla_a,
190 info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
192 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) {
193 nla_for_each_nested(nla_b, nla_a, nla_b_rem)
194 switch (nla_b->nla_type) {
195 case NLBL_CIPSOV4_A_MLSLVLLOC:
196 if (nla_get_u32(nla_b) >=
197 doi_def->map.std->lvl.local_size)
198 doi_def->map.std->lvl.local_size =
199 nla_get_u32(nla_b) + 1;
201 case NLBL_CIPSOV4_A_MLSLVLREM:
202 if (nla_get_u32(nla_b) >=
203 doi_def->map.std->lvl.cipso_size)
204 doi_def->map.std->lvl.cipso_size =
205 nla_get_u32(nla_b) + 1;
209 if (doi_def->map.std->lvl.local_size > CIPSO_V4_MAX_LOC_LVLS ||
210 doi_def->map.std->lvl.cipso_size > CIPSO_V4_MAX_REM_LVLS)
211 goto add_std_failure;
212 doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
215 if (doi_def->map.std->lvl.local == NULL) {
217 goto add_std_failure;
219 doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
222 if (doi_def->map.std->lvl.cipso == NULL) {
224 goto add_std_failure;
226 nla_for_each_nested(nla_a,
227 info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
229 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) {
230 struct nlattr *lvl_loc;
231 struct nlattr *lvl_rem;
233 if (nla_validate_nested(nla_a,
235 netlbl_cipsov4_genl_policy) != 0)
236 goto add_std_failure;
238 lvl_loc = nla_find_nested(nla_a,
239 NLBL_CIPSOV4_A_MLSLVLLOC);
240 lvl_rem = nla_find_nested(nla_a,
241 NLBL_CIPSOV4_A_MLSLVLREM);
242 if (lvl_loc == NULL || lvl_rem == NULL)
243 goto add_std_failure;
244 doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] =
245 nla_get_u32(lvl_rem);
246 doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] =
247 nla_get_u32(lvl_loc);
250 if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) {
251 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
253 netlbl_cipsov4_genl_policy) != 0)
254 goto add_std_failure;
256 nla_for_each_nested(nla_a,
257 info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
259 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) {
260 if (nla_validate_nested(nla_a,
262 netlbl_cipsov4_genl_policy) != 0)
263 goto add_std_failure;
264 nla_for_each_nested(nla_b, nla_a, nla_b_rem)
265 switch (nla_b->nla_type) {
266 case NLBL_CIPSOV4_A_MLSCATLOC:
267 if (nla_get_u32(nla_b) >=
268 doi_def->map.std->cat.local_size)
269 doi_def->map.std->cat.local_size =
270 nla_get_u32(nla_b) + 1;
272 case NLBL_CIPSOV4_A_MLSCATREM:
273 if (nla_get_u32(nla_b) >=
274 doi_def->map.std->cat.cipso_size)
275 doi_def->map.std->cat.cipso_size =
276 nla_get_u32(nla_b) + 1;
280 if (doi_def->map.std->cat.local_size > CIPSO_V4_MAX_LOC_CATS ||
281 doi_def->map.std->cat.cipso_size > CIPSO_V4_MAX_REM_CATS)
282 goto add_std_failure;
283 doi_def->map.std->cat.local = kcalloc(
284 doi_def->map.std->cat.local_size,
287 if (doi_def->map.std->cat.local == NULL) {
289 goto add_std_failure;
291 doi_def->map.std->cat.cipso = kcalloc(
292 doi_def->map.std->cat.cipso_size,
295 if (doi_def->map.std->cat.cipso == NULL) {
297 goto add_std_failure;
299 nla_for_each_nested(nla_a,
300 info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
302 if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) {
303 struct nlattr *cat_loc;
304 struct nlattr *cat_rem;
306 cat_loc = nla_find_nested(nla_a,
307 NLBL_CIPSOV4_A_MLSCATLOC);
308 cat_rem = nla_find_nested(nla_a,
309 NLBL_CIPSOV4_A_MLSCATREM);
310 if (cat_loc == NULL || cat_rem == NULL)
311 goto add_std_failure;
312 doi_def->map.std->cat.local[
313 nla_get_u32(cat_loc)] =
314 nla_get_u32(cat_rem);
315 doi_def->map.std->cat.cipso[
316 nla_get_u32(cat_rem)] =
317 nla_get_u32(cat_loc);
321 ret_val = cipso_v4_doi_add(doi_def);
323 goto add_std_failure;
328 netlbl_cipsov4_doi_free(&doi_def->rcu);
333 * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
334 * @info: the Generic NETLINK info block
337 * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
338 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
342 static int netlbl_cipsov4_add_pass(struct genl_info *info)
345 struct cipso_v4_doi *doi_def = NULL;
347 if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
348 !info->attrs[NLBL_CIPSOV4_A_TAGLST])
351 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
354 doi_def->type = CIPSO_V4_MAP_PASS;
356 ret_val = netlbl_cipsov4_add_common(info, doi_def);
358 goto add_pass_failure;
360 ret_val = cipso_v4_doi_add(doi_def);
362 goto add_pass_failure;
366 netlbl_cipsov4_doi_free(&doi_def->rcu);
371 * netlbl_cipsov4_add - Handle an ADD message
372 * @skb: the NETLINK buffer
373 * @info: the Generic NETLINK info block
376 * Create a new DOI definition based on the given ADD message and add it to the
377 * CIPSO V4 engine. Returns zero on success, negative values on failure.
380 static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
383 int ret_val = -EINVAL;
386 if (!info->attrs[NLBL_CIPSOV4_A_MTYPE])
389 map_type = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE]);
391 case CIPSO_V4_MAP_STD:
392 ret_val = netlbl_cipsov4_add_std(info);
394 case CIPSO_V4_MAP_PASS:
395 ret_val = netlbl_cipsov4_add_pass(info);
403 * netlbl_cipsov4_list - Handle a LIST message
404 * @skb: the NETLINK buffer
405 * @info: the Generic NETLINK info block
408 * Process a user generated LIST message and respond accordingly. While the
409 * response message generated by the kernel is straightforward, determining
410 * before hand the size of the buffer to allocate is not (we have to generate
411 * the message to know the size). In order to keep this function sane what we
412 * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
413 * that size, if we fail then we restart with a larger buffer and try again.
414 * We continue in this manner until we hit a limit of failed attempts then we
415 * give up and just send an error message. Returns zero on success and
416 * negative values on error.
419 static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
422 struct sk_buff *ans_skb = NULL;
426 struct nlattr *nla_a;
427 struct nlattr *nla_b;
428 struct cipso_v4_doi *doi_def;
431 if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
437 ans_skb = nlmsg_new(NLMSG_GOODSIZE * nlsze_mult, GFP_KERNEL);
438 if (ans_skb == NULL) {
442 data = netlbl_netlink_hdr_put(ans_skb,
445 netlbl_cipsov4_gnl_family.id,
447 NLBL_CIPSOV4_C_LIST);
453 doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
456 doi_def = cipso_v4_doi_getdef(doi);
457 if (doi_def == NULL) {
462 ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
464 goto list_failure_lock;
466 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
469 goto list_failure_lock;
472 iter < CIPSO_V4_TAG_MAXCNT &&
473 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
475 ret_val = nla_put_u8(ans_skb,
477 doi_def->tags[iter]);
479 goto list_failure_lock;
481 nla_nest_end(ans_skb, nla_a);
483 switch (doi_def->type) {
484 case CIPSO_V4_MAP_STD:
485 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
488 goto list_failure_lock;
491 iter < doi_def->map.std->lvl.local_size;
493 if (doi_def->map.std->lvl.local[iter] ==
497 nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
502 ret_val = nla_put_u32(ans_skb,
503 NLBL_CIPSOV4_A_MLSLVLLOC,
507 ret_val = nla_put_u32(ans_skb,
508 NLBL_CIPSOV4_A_MLSLVLREM,
509 doi_def->map.std->lvl.local[iter]);
512 nla_nest_end(ans_skb, nla_b);
514 nla_nest_end(ans_skb, nla_a);
516 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
522 iter < doi_def->map.std->cat.local_size;
524 if (doi_def->map.std->cat.local[iter] ==
528 nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
533 ret_val = nla_put_u32(ans_skb,
534 NLBL_CIPSOV4_A_MLSCATLOC,
538 ret_val = nla_put_u32(ans_skb,
539 NLBL_CIPSOV4_A_MLSCATREM,
540 doi_def->map.std->cat.local[iter]);
543 nla_nest_end(ans_skb, nla_b);
545 nla_nest_end(ans_skb, nla_a);
551 genlmsg_end(ans_skb, data);
553 ret_val = genlmsg_unicast(ans_skb, info->snd_pid);
560 /* XXX - this limit is a guesstimate */
561 if (nlsze_mult < 4) {
575 * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
576 * @doi_def: the CIPSOv4 DOI definition
577 * @arg: the netlbl_cipsov4_doiwalk_arg structure
580 * This function is designed to be used as a callback to the
581 * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
582 * message. Returns the size of the message on success, negative values on
586 static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
588 int ret_val = -ENOMEM;
589 struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
592 data = netlbl_netlink_hdr_put(cb_arg->skb,
593 NETLINK_CB(cb_arg->nl_cb->skb).pid,
595 netlbl_cipsov4_gnl_family.id,
597 NLBL_CIPSOV4_C_LISTALL);
599 goto listall_cb_failure;
601 ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
603 goto listall_cb_failure;
604 ret_val = nla_put_u32(cb_arg->skb,
605 NLBL_CIPSOV4_A_MTYPE,
608 goto listall_cb_failure;
610 return genlmsg_end(cb_arg->skb, data);
613 genlmsg_cancel(cb_arg->skb, data);
618 * netlbl_cipsov4_listall - Handle a LISTALL message
619 * @skb: the NETLINK buffer
620 * @cb: the NETLINK callback
623 * Process a user generated LISTALL message and respond accordingly. Returns
624 * zero on success and negative values on error.
627 static int netlbl_cipsov4_listall(struct sk_buff *skb,
628 struct netlink_callback *cb)
630 struct netlbl_cipsov4_doiwalk_arg cb_arg;
631 int doi_skip = cb->args[0];
635 cb_arg.seq = cb->nlh->nlmsg_seq;
637 cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
639 cb->args[0] = doi_skip;
644 * netlbl_cipsov4_remove - Handle a REMOVE message
645 * @skb: the NETLINK buffer
646 * @info: the Generic NETLINK info block
649 * Process a user generated REMOVE message and respond accordingly. Returns
650 * zero on success, negative values on failure.
653 static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
655 int ret_val = -EINVAL;
658 if (info->attrs[NLBL_CIPSOV4_A_DOI]) {
659 doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
660 ret_val = cipso_v4_doi_remove(doi, netlbl_cipsov4_doi_free);
667 * NetLabel Generic NETLINK Command Definitions
670 static struct genl_ops netlbl_cipsov4_genl_c_add = {
671 .cmd = NLBL_CIPSOV4_C_ADD,
672 .flags = GENL_ADMIN_PERM,
673 .policy = netlbl_cipsov4_genl_policy,
674 .doit = netlbl_cipsov4_add,
678 static struct genl_ops netlbl_cipsov4_genl_c_remove = {
679 .cmd = NLBL_CIPSOV4_C_REMOVE,
680 .flags = GENL_ADMIN_PERM,
681 .policy = netlbl_cipsov4_genl_policy,
682 .doit = netlbl_cipsov4_remove,
686 static struct genl_ops netlbl_cipsov4_genl_c_list = {
687 .cmd = NLBL_CIPSOV4_C_LIST,
689 .policy = netlbl_cipsov4_genl_policy,
690 .doit = netlbl_cipsov4_list,
694 static struct genl_ops netlbl_cipsov4_genl_c_listall = {
695 .cmd = NLBL_CIPSOV4_C_LISTALL,
697 .policy = netlbl_cipsov4_genl_policy,
699 .dumpit = netlbl_cipsov4_listall,
703 * NetLabel Generic NETLINK Protocol Functions
707 * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
710 * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
711 * mechanism. Returns zero on success, negative values on failure.
714 int netlbl_cipsov4_genl_init(void)
718 ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
722 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
723 &netlbl_cipsov4_genl_c_add);
726 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
727 &netlbl_cipsov4_genl_c_remove);
730 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
731 &netlbl_cipsov4_genl_c_list);
734 ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
735 &netlbl_cipsov4_genl_c_listall);