xref: /openbmc/linux/net/netlabel/netlabel_cipso_v4.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
196cb8e33SPaul Moore /*
296cb8e33SPaul Moore  * NetLabel CIPSO/IPv4 Support
396cb8e33SPaul Moore  *
496cb8e33SPaul Moore  * This file defines the CIPSO/IPv4 functions for the NetLabel system.  The
596cb8e33SPaul Moore  * NetLabel system manages static and dynamic label mappings for network
696cb8e33SPaul Moore  * protocols such as CIPSO and RIPSO.
796cb8e33SPaul Moore  *
896cb8e33SPaul Moore  * Author: Paul Moore <paul.moore@hp.com>
996cb8e33SPaul Moore  *
1096cb8e33SPaul Moore  */
1196cb8e33SPaul Moore 
1296cb8e33SPaul Moore /*
1396cb8e33SPaul Moore  * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
1496cb8e33SPaul Moore  *
1596cb8e33SPaul Moore  * This program is free software;  you can redistribute it and/or modify
1696cb8e33SPaul Moore  * it under the terms of the GNU General Public License as published by
1796cb8e33SPaul Moore  * the Free Software Foundation; either version 2 of the License, or
1896cb8e33SPaul Moore  * (at your option) any later version.
1996cb8e33SPaul Moore  *
2096cb8e33SPaul Moore  * This program is distributed in the hope that it will be useful,
2196cb8e33SPaul Moore  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
2296cb8e33SPaul Moore  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
2396cb8e33SPaul Moore  * the GNU General Public License for more details.
2496cb8e33SPaul Moore  *
2596cb8e33SPaul Moore  * You should have received a copy of the GNU General Public License
2696cb8e33SPaul Moore  * along with this program;  if not, write to the Free Software
2796cb8e33SPaul Moore  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2896cb8e33SPaul Moore  *
2996cb8e33SPaul Moore  */
3096cb8e33SPaul Moore 
3196cb8e33SPaul Moore #include <linux/types.h>
3296cb8e33SPaul Moore #include <linux/socket.h>
3396cb8e33SPaul Moore #include <linux/string.h>
3496cb8e33SPaul Moore #include <linux/skbuff.h>
3532f50cdeSPaul Moore #include <linux/audit.h>
36*5a0e3ad6STejun Heo #include <linux/slab.h>
3796cb8e33SPaul Moore #include <net/sock.h>
3896cb8e33SPaul Moore #include <net/netlink.h>
3996cb8e33SPaul Moore #include <net/genetlink.h>
4096cb8e33SPaul Moore #include <net/netlabel.h>
4196cb8e33SPaul Moore #include <net/cipso_ipv4.h>
42c783f1ceSPaul Moore #include <asm/atomic.h>
4396cb8e33SPaul Moore 
4496cb8e33SPaul Moore #include "netlabel_user.h"
4596cb8e33SPaul Moore #include "netlabel_cipso_v4.h"
4623bcdc1aSPaul Moore #include "netlabel_mgmt.h"
47b1edeb10SPaul Moore #include "netlabel_domainhash.h"
4896cb8e33SPaul Moore 
49fd385855SPaul Moore /* Argument struct for cipso_v4_doi_walk() */
50fd385855SPaul Moore struct netlbl_cipsov4_doiwalk_arg {
51fd385855SPaul Moore 	struct netlink_callback *nl_cb;
52fd385855SPaul Moore 	struct sk_buff *skb;
53fd385855SPaul Moore 	u32 seq;
54fd385855SPaul Moore };
55fd385855SPaul Moore 
56b1edeb10SPaul Moore /* Argument struct for netlbl_domhsh_walk() */
57b1edeb10SPaul Moore struct netlbl_domhsh_walk_arg {
58b1edeb10SPaul Moore 	struct netlbl_audit *audit_info;
59b1edeb10SPaul Moore 	u32 doi;
60b1edeb10SPaul Moore };
61b1edeb10SPaul Moore 
6296cb8e33SPaul Moore /* NetLabel Generic NETLINK CIPSOv4 family */
6396cb8e33SPaul Moore static struct genl_family netlbl_cipsov4_gnl_family = {
6496cb8e33SPaul Moore 	.id = GENL_ID_GENERATE,
6596cb8e33SPaul Moore 	.hdrsize = 0,
6696cb8e33SPaul Moore 	.name = NETLBL_NLTYPE_CIPSOV4_NAME,
6796cb8e33SPaul Moore 	.version = NETLBL_PROTO_VERSION,
68fd385855SPaul Moore 	.maxattr = NLBL_CIPSOV4_A_MAX,
6996cb8e33SPaul Moore };
7096cb8e33SPaul Moore 
71fd385855SPaul Moore /* NetLabel Netlink attribute policy */
72ef7c79edSPatrick McHardy static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
73fd385855SPaul Moore 	[NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
74fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
75fd385855SPaul Moore 	[NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
76fd385855SPaul Moore 	[NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
77fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
78fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
79fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
80fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
81fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
82fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
83fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
84fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
85fd385855SPaul Moore };
8696cb8e33SPaul Moore 
8796cb8e33SPaul Moore /*
8896cb8e33SPaul Moore  * Helper Functions
8996cb8e33SPaul Moore  */
9096cb8e33SPaul Moore 
9196cb8e33SPaul Moore /**
92fd385855SPaul Moore  * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
93fd385855SPaul Moore  * @info: the Generic NETLINK info block
94fd385855SPaul Moore  * @doi_def: the CIPSO V4 DOI definition
95fd385855SPaul Moore  *
96fd385855SPaul Moore  * Description:
97fd385855SPaul Moore  * Parse the common sections of a ADD message and fill in the related values
98fd385855SPaul Moore  * in @doi_def.  Returns zero on success, negative values on failure.
99fd385855SPaul Moore  *
100fd385855SPaul Moore  */
101fd385855SPaul Moore static int netlbl_cipsov4_add_common(struct genl_info *info,
102fd385855SPaul Moore 				     struct cipso_v4_doi *doi_def)
103fd385855SPaul Moore {
104fd385855SPaul Moore 	struct nlattr *nla;
105fd385855SPaul Moore 	int nla_rem;
106fd385855SPaul Moore 	u32 iter = 0;
107fd385855SPaul Moore 
108fd385855SPaul Moore 	doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
109fd385855SPaul Moore 
110fd385855SPaul Moore 	if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
111fd385855SPaul Moore 				NLBL_CIPSOV4_A_MAX,
112fd385855SPaul Moore 				netlbl_cipsov4_genl_policy) != 0)
113fd385855SPaul Moore 		return -EINVAL;
114fd385855SPaul Moore 
115fd385855SPaul Moore 	nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
1168f4c1f9bSThomas Graf 		if (nla_type(nla) == NLBL_CIPSOV4_A_TAG) {
1172a2f11c2SPaul Moore 			if (iter >= CIPSO_V4_TAG_MAXCNT)
118fd385855SPaul Moore 				return -EINVAL;
119fd385855SPaul Moore 			doi_def->tags[iter++] = nla_get_u8(nla);
120fd385855SPaul Moore 		}
1212a2f11c2SPaul Moore 	while (iter < CIPSO_V4_TAG_MAXCNT)
1222a2f11c2SPaul Moore 		doi_def->tags[iter++] = CIPSO_V4_TAG_INVALID;
123fd385855SPaul Moore 
124fd385855SPaul Moore 	return 0;
125fd385855SPaul Moore }
12696cb8e33SPaul Moore 
12796cb8e33SPaul Moore /*
12896cb8e33SPaul Moore  * NetLabel Command Handlers
12996cb8e33SPaul Moore  */
13096cb8e33SPaul Moore 
13196cb8e33SPaul Moore /**
13296cb8e33SPaul Moore  * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
133fd385855SPaul Moore  * @info: the Generic NETLINK info block
1346c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
13596cb8e33SPaul Moore  *
13696cb8e33SPaul Moore  * Description:
13715c45f7bSPaul Moore  * Create a new CIPSO_V4_MAP_TRANS DOI definition based on the given ADD
13815c45f7bSPaul Moore  * message and add it to the CIPSO V4 engine.  Return zero on success and
13915c45f7bSPaul Moore  * non-zero on error.
14096cb8e33SPaul Moore  *
14196cb8e33SPaul Moore  */
1426c2e8ac0SPaul Moore static int netlbl_cipsov4_add_std(struct genl_info *info,
1436c2e8ac0SPaul Moore 				  struct netlbl_audit *audit_info)
14496cb8e33SPaul Moore {
14596cb8e33SPaul Moore 	int ret_val = -EINVAL;
14696cb8e33SPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
147fd385855SPaul Moore 	struct nlattr *nla_a;
148fd385855SPaul Moore 	struct nlattr *nla_b;
149fd385855SPaul Moore 	int nla_a_rem;
150fd385855SPaul Moore 	int nla_b_rem;
151caff5b6aSPaul Moore 	u32 iter;
15296cb8e33SPaul Moore 
15332f50cdeSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_TAGLST] ||
154fd385855SPaul Moore 	    !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST])
155fd385855SPaul Moore 		return -EINVAL;
156fd385855SPaul Moore 
157fd385855SPaul Moore 	if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
158fd385855SPaul Moore 				NLBL_CIPSOV4_A_MAX,
159fd385855SPaul Moore 				netlbl_cipsov4_genl_policy) != 0)
160fd385855SPaul Moore 		return -EINVAL;
16196cb8e33SPaul Moore 
16296cb8e33SPaul Moore 	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
163fd385855SPaul Moore 	if (doi_def == NULL)
164fd385855SPaul Moore 		return -ENOMEM;
16596cb8e33SPaul Moore 	doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
16696cb8e33SPaul Moore 	if (doi_def->map.std == NULL) {
16796cb8e33SPaul Moore 		ret_val = -ENOMEM;
16896cb8e33SPaul Moore 		goto add_std_failure;
16996cb8e33SPaul Moore 	}
17015c45f7bSPaul Moore 	doi_def->type = CIPSO_V4_MAP_TRANS;
17196cb8e33SPaul Moore 
172fd385855SPaul Moore 	ret_val = netlbl_cipsov4_add_common(info, doi_def);
173fd385855SPaul Moore 	if (ret_val != 0)
17496cb8e33SPaul Moore 		goto add_std_failure;
1751fd2a25bSPaul Moore 	ret_val = -EINVAL;
176fd385855SPaul Moore 
177fd385855SPaul Moore 	nla_for_each_nested(nla_a,
178fd385855SPaul Moore 			    info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
179fd385855SPaul Moore 			    nla_a_rem)
1808f4c1f9bSThomas Graf 		if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
1811fd2a25bSPaul Moore 			if (nla_validate_nested(nla_a,
1821fd2a25bSPaul Moore 					    NLBL_CIPSOV4_A_MAX,
1831fd2a25bSPaul Moore 					    netlbl_cipsov4_genl_policy) != 0)
1841fd2a25bSPaul Moore 					goto add_std_failure;
185fd385855SPaul Moore 			nla_for_each_nested(nla_b, nla_a, nla_b_rem)
1868f4c1f9bSThomas Graf 				switch (nla_type(nla_b)) {
187fd385855SPaul Moore 				case NLBL_CIPSOV4_A_MLSLVLLOC:
1881fd2a25bSPaul Moore 					if (nla_get_u32(nla_b) >
1891fd2a25bSPaul Moore 					    CIPSO_V4_MAX_LOC_LVLS)
1901fd2a25bSPaul Moore 						goto add_std_failure;
191fd385855SPaul Moore 					if (nla_get_u32(nla_b) >=
192fd385855SPaul Moore 					    doi_def->map.std->lvl.local_size)
193fd385855SPaul Moore 					     doi_def->map.std->lvl.local_size =
194fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
19596cb8e33SPaul Moore 					break;
196fd385855SPaul Moore 				case NLBL_CIPSOV4_A_MLSLVLREM:
1971fd2a25bSPaul Moore 					if (nla_get_u32(nla_b) >
1981fd2a25bSPaul Moore 					    CIPSO_V4_MAX_REM_LVLS)
1991fd2a25bSPaul Moore 						goto add_std_failure;
200fd385855SPaul Moore 					if (nla_get_u32(nla_b) >=
201fd385855SPaul Moore 					    doi_def->map.std->lvl.cipso_size)
202fd385855SPaul Moore 					     doi_def->map.std->lvl.cipso_size =
203fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
204fd385855SPaul Moore 					break;
20596cb8e33SPaul Moore 				}
20696cb8e33SPaul Moore 		}
20796cb8e33SPaul Moore 	doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
20896cb8e33SPaul Moore 					      sizeof(u32),
20996cb8e33SPaul Moore 					      GFP_KERNEL);
21096cb8e33SPaul Moore 	if (doi_def->map.std->lvl.local == NULL) {
21196cb8e33SPaul Moore 		ret_val = -ENOMEM;
21296cb8e33SPaul Moore 		goto add_std_failure;
21396cb8e33SPaul Moore 	}
21496cb8e33SPaul Moore 	doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
21596cb8e33SPaul Moore 					      sizeof(u32),
21696cb8e33SPaul Moore 					      GFP_KERNEL);
21796cb8e33SPaul Moore 	if (doi_def->map.std->lvl.cipso == NULL) {
21896cb8e33SPaul Moore 		ret_val = -ENOMEM;
21996cb8e33SPaul Moore 		goto add_std_failure;
22096cb8e33SPaul Moore 	}
221caff5b6aSPaul Moore 	for (iter = 0; iter < doi_def->map.std->lvl.local_size; iter++)
222caff5b6aSPaul Moore 		doi_def->map.std->lvl.local[iter] = CIPSO_V4_INV_LVL;
223caff5b6aSPaul Moore 	for (iter = 0; iter < doi_def->map.std->lvl.cipso_size; iter++)
224caff5b6aSPaul Moore 		doi_def->map.std->lvl.cipso[iter] = CIPSO_V4_INV_LVL;
225fd385855SPaul Moore 	nla_for_each_nested(nla_a,
226fd385855SPaul Moore 			    info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
227fd385855SPaul Moore 			    nla_a_rem)
2288f4c1f9bSThomas Graf 		if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
229fd385855SPaul Moore 			struct nlattr *lvl_loc;
230fd385855SPaul Moore 			struct nlattr *lvl_rem;
23196cb8e33SPaul Moore 
232fd385855SPaul Moore 			lvl_loc = nla_find_nested(nla_a,
233fd385855SPaul Moore 						  NLBL_CIPSOV4_A_MLSLVLLOC);
234fd385855SPaul Moore 			lvl_rem = nla_find_nested(nla_a,
235fd385855SPaul Moore 						  NLBL_CIPSOV4_A_MLSLVLREM);
236fd385855SPaul Moore 			if (lvl_loc == NULL || lvl_rem == NULL)
237fd385855SPaul Moore 				goto add_std_failure;
238fd385855SPaul Moore 			doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] =
239fd385855SPaul Moore 				nla_get_u32(lvl_rem);
240fd385855SPaul Moore 			doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] =
241fd385855SPaul Moore 				nla_get_u32(lvl_loc);
242fd385855SPaul Moore 		}
243fd385855SPaul Moore 
244fd385855SPaul Moore 	if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) {
245fd385855SPaul Moore 		if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
246fd385855SPaul Moore 					NLBL_CIPSOV4_A_MAX,
247fd385855SPaul Moore 					netlbl_cipsov4_genl_policy) != 0)
248fd385855SPaul Moore 			goto add_std_failure;
249fd385855SPaul Moore 
250fd385855SPaul Moore 		nla_for_each_nested(nla_a,
251fd385855SPaul Moore 				    info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
252fd385855SPaul Moore 				    nla_a_rem)
2538f4c1f9bSThomas Graf 			if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
254fd385855SPaul Moore 				if (nla_validate_nested(nla_a,
255fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MAX,
256fd385855SPaul Moore 					      netlbl_cipsov4_genl_policy) != 0)
257fd385855SPaul Moore 					goto add_std_failure;
258fd385855SPaul Moore 				nla_for_each_nested(nla_b, nla_a, nla_b_rem)
2598f4c1f9bSThomas Graf 					switch (nla_type(nla_b)) {
260fd385855SPaul Moore 					case NLBL_CIPSOV4_A_MLSCATLOC:
2611fd2a25bSPaul Moore 						if (nla_get_u32(nla_b) >
2621fd2a25bSPaul Moore 						    CIPSO_V4_MAX_LOC_CATS)
2631fd2a25bSPaul Moore 							goto add_std_failure;
264fd385855SPaul Moore 						if (nla_get_u32(nla_b) >=
265fd385855SPaul Moore 					      doi_def->map.std->cat.local_size)
266fd385855SPaul Moore 					     doi_def->map.std->cat.local_size =
267fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
268fd385855SPaul Moore 						break;
269fd385855SPaul Moore 					case NLBL_CIPSOV4_A_MLSCATREM:
2701fd2a25bSPaul Moore 						if (nla_get_u32(nla_b) >
2711fd2a25bSPaul Moore 						    CIPSO_V4_MAX_REM_CATS)
2721fd2a25bSPaul Moore 							goto add_std_failure;
273fd385855SPaul Moore 						if (nla_get_u32(nla_b) >=
274fd385855SPaul Moore 					      doi_def->map.std->cat.cipso_size)
275fd385855SPaul Moore 					     doi_def->map.std->cat.cipso_size =
276fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
277fd385855SPaul Moore 						break;
278fd385855SPaul Moore 					}
279fd385855SPaul Moore 			}
280fd385855SPaul Moore 		doi_def->map.std->cat.local = kcalloc(
281fd385855SPaul Moore 					      doi_def->map.std->cat.local_size,
28296cb8e33SPaul Moore 					      sizeof(u32),
28396cb8e33SPaul Moore 					      GFP_KERNEL);
28496cb8e33SPaul Moore 		if (doi_def->map.std->cat.local == NULL) {
28596cb8e33SPaul Moore 			ret_val = -ENOMEM;
28696cb8e33SPaul Moore 			goto add_std_failure;
28796cb8e33SPaul Moore 		}
288fd385855SPaul Moore 		doi_def->map.std->cat.cipso = kcalloc(
289fd385855SPaul Moore 					      doi_def->map.std->cat.cipso_size,
29096cb8e33SPaul Moore 					      sizeof(u32),
29196cb8e33SPaul Moore 					      GFP_KERNEL);
29296cb8e33SPaul Moore 		if (doi_def->map.std->cat.cipso == NULL) {
29396cb8e33SPaul Moore 			ret_val = -ENOMEM;
29496cb8e33SPaul Moore 			goto add_std_failure;
29596cb8e33SPaul Moore 		}
296caff5b6aSPaul Moore 		for (iter = 0; iter < doi_def->map.std->cat.local_size; iter++)
297caff5b6aSPaul Moore 			doi_def->map.std->cat.local[iter] = CIPSO_V4_INV_CAT;
298caff5b6aSPaul Moore 		for (iter = 0; iter < doi_def->map.std->cat.cipso_size; iter++)
299caff5b6aSPaul Moore 			doi_def->map.std->cat.cipso[iter] = CIPSO_V4_INV_CAT;
300fd385855SPaul Moore 		nla_for_each_nested(nla_a,
301fd385855SPaul Moore 				    info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
302fd385855SPaul Moore 				    nla_a_rem)
3038f4c1f9bSThomas Graf 			if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
304fd385855SPaul Moore 				struct nlattr *cat_loc;
305fd385855SPaul Moore 				struct nlattr *cat_rem;
30696cb8e33SPaul Moore 
307fd385855SPaul Moore 				cat_loc = nla_find_nested(nla_a,
308fd385855SPaul Moore 						     NLBL_CIPSOV4_A_MLSCATLOC);
309fd385855SPaul Moore 				cat_rem = nla_find_nested(nla_a,
310fd385855SPaul Moore 						     NLBL_CIPSOV4_A_MLSCATREM);
311fd385855SPaul Moore 				if (cat_loc == NULL || cat_rem == NULL)
31296cb8e33SPaul Moore 					goto add_std_failure;
313fd385855SPaul Moore 				doi_def->map.std->cat.local[
314fd385855SPaul Moore 							nla_get_u32(cat_loc)] =
315fd385855SPaul Moore 					nla_get_u32(cat_rem);
316fd385855SPaul Moore 				doi_def->map.std->cat.cipso[
317fd385855SPaul Moore 							nla_get_u32(cat_rem)] =
318fd385855SPaul Moore 					nla_get_u32(cat_loc);
319fd385855SPaul Moore 			}
32096cb8e33SPaul Moore 	}
32196cb8e33SPaul Moore 
3226c2e8ac0SPaul Moore 	ret_val = cipso_v4_doi_add(doi_def, audit_info);
32396cb8e33SPaul Moore 	if (ret_val != 0)
32496cb8e33SPaul Moore 		goto add_std_failure;
32596cb8e33SPaul Moore 	return 0;
32696cb8e33SPaul Moore 
32796cb8e33SPaul Moore add_std_failure:
32896cb8e33SPaul Moore 	if (doi_def)
329b1edeb10SPaul Moore 		cipso_v4_doi_free(doi_def);
33096cb8e33SPaul Moore 	return ret_val;
33196cb8e33SPaul Moore }
33296cb8e33SPaul Moore 
33396cb8e33SPaul Moore /**
33496cb8e33SPaul Moore  * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
335fd385855SPaul Moore  * @info: the Generic NETLINK info block
3366c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
33796cb8e33SPaul Moore  *
33896cb8e33SPaul Moore  * Description:
33996cb8e33SPaul Moore  * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
34096cb8e33SPaul Moore  * and add it to the CIPSO V4 engine.  Return zero on success and non-zero on
34196cb8e33SPaul Moore  * error.
34296cb8e33SPaul Moore  *
34396cb8e33SPaul Moore  */
3446c2e8ac0SPaul Moore static int netlbl_cipsov4_add_pass(struct genl_info *info,
3456c2e8ac0SPaul Moore 				   struct netlbl_audit *audit_info)
34696cb8e33SPaul Moore {
347fd385855SPaul Moore 	int ret_val;
34896cb8e33SPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
34996cb8e33SPaul Moore 
35032f50cdeSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
351fd385855SPaul Moore 		return -EINVAL;
35296cb8e33SPaul Moore 
35396cb8e33SPaul Moore 	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
354fd385855SPaul Moore 	if (doi_def == NULL)
355fd385855SPaul Moore 		return -ENOMEM;
35696cb8e33SPaul Moore 	doi_def->type = CIPSO_V4_MAP_PASS;
35796cb8e33SPaul Moore 
358fd385855SPaul Moore 	ret_val = netlbl_cipsov4_add_common(info, doi_def);
359fd385855SPaul Moore 	if (ret_val != 0)
36096cb8e33SPaul Moore 		goto add_pass_failure;
36196cb8e33SPaul Moore 
3626c2e8ac0SPaul Moore 	ret_val = cipso_v4_doi_add(doi_def, audit_info);
36396cb8e33SPaul Moore 	if (ret_val != 0)
36496cb8e33SPaul Moore 		goto add_pass_failure;
36596cb8e33SPaul Moore 	return 0;
36696cb8e33SPaul Moore 
36796cb8e33SPaul Moore add_pass_failure:
368b1edeb10SPaul Moore 	cipso_v4_doi_free(doi_def);
36996cb8e33SPaul Moore 	return ret_val;
37096cb8e33SPaul Moore }
37196cb8e33SPaul Moore 
37296cb8e33SPaul Moore /**
373d91d4079SPaul Moore  * netlbl_cipsov4_add_local - Adds a CIPSO V4 DOI definition
374d91d4079SPaul Moore  * @info: the Generic NETLINK info block
3756c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
376d91d4079SPaul Moore  *
377d91d4079SPaul Moore  * Description:
378d91d4079SPaul Moore  * Create a new CIPSO_V4_MAP_LOCAL DOI definition based on the given ADD
379d91d4079SPaul Moore  * message and add it to the CIPSO V4 engine.  Return zero on success and
380d91d4079SPaul Moore  * non-zero on error.
381d91d4079SPaul Moore  *
382d91d4079SPaul Moore  */
3836c2e8ac0SPaul Moore static int netlbl_cipsov4_add_local(struct genl_info *info,
3846c2e8ac0SPaul Moore 				    struct netlbl_audit *audit_info)
385d91d4079SPaul Moore {
386d91d4079SPaul Moore 	int ret_val;
387d91d4079SPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
388d91d4079SPaul Moore 
389d91d4079SPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
390d91d4079SPaul Moore 		return -EINVAL;
391d91d4079SPaul Moore 
392d91d4079SPaul Moore 	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
393d91d4079SPaul Moore 	if (doi_def == NULL)
394d91d4079SPaul Moore 		return -ENOMEM;
395d91d4079SPaul Moore 	doi_def->type = CIPSO_V4_MAP_LOCAL;
396d91d4079SPaul Moore 
397d91d4079SPaul Moore 	ret_val = netlbl_cipsov4_add_common(info, doi_def);
398d91d4079SPaul Moore 	if (ret_val != 0)
399d91d4079SPaul Moore 		goto add_local_failure;
400d91d4079SPaul Moore 
4016c2e8ac0SPaul Moore 	ret_val = cipso_v4_doi_add(doi_def, audit_info);
402d91d4079SPaul Moore 	if (ret_val != 0)
403d91d4079SPaul Moore 		goto add_local_failure;
404d91d4079SPaul Moore 	return 0;
405d91d4079SPaul Moore 
406d91d4079SPaul Moore add_local_failure:
407d91d4079SPaul Moore 	cipso_v4_doi_free(doi_def);
408d91d4079SPaul Moore 	return ret_val;
409d91d4079SPaul Moore }
410d91d4079SPaul Moore 
411d91d4079SPaul Moore /**
41296cb8e33SPaul Moore  * netlbl_cipsov4_add - Handle an ADD message
41396cb8e33SPaul Moore  * @skb: the NETLINK buffer
41496cb8e33SPaul Moore  * @info: the Generic NETLINK info block
41596cb8e33SPaul Moore  *
41696cb8e33SPaul Moore  * Description:
41796cb8e33SPaul Moore  * Create a new DOI definition based on the given ADD message and add it to the
41896cb8e33SPaul Moore  * CIPSO V4 engine.  Returns zero on success, negative values on failure.
41996cb8e33SPaul Moore  *
42096cb8e33SPaul Moore  */
42196cb8e33SPaul Moore static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
42296cb8e33SPaul Moore 
42396cb8e33SPaul Moore {
42496cb8e33SPaul Moore 	int ret_val = -EINVAL;
42532f50cdeSPaul Moore 	const char *type_str = "(unknown)";
42695d4e6beSPaul Moore 	struct netlbl_audit audit_info;
42796cb8e33SPaul Moore 
42832f50cdeSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
42932f50cdeSPaul Moore 	    !info->attrs[NLBL_CIPSOV4_A_MTYPE])
430fd385855SPaul Moore 		return -EINVAL;
43196cb8e33SPaul Moore 
43295d4e6beSPaul Moore 	netlbl_netlink_auditinfo(skb, &audit_info);
4336c2e8ac0SPaul Moore 	switch (nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE])) {
43415c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
43515c45f7bSPaul Moore 		type_str = "trans";
4366c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_std(info, &audit_info);
43796cb8e33SPaul Moore 		break;
43896cb8e33SPaul Moore 	case CIPSO_V4_MAP_PASS:
43932f50cdeSPaul Moore 		type_str = "pass";
4406c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_pass(info, &audit_info);
44196cb8e33SPaul Moore 		break;
442d91d4079SPaul Moore 	case CIPSO_V4_MAP_LOCAL:
443d91d4079SPaul Moore 		type_str = "local";
4446c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_local(info, &audit_info);
445d91d4079SPaul Moore 		break;
44696cb8e33SPaul Moore 	}
44723bcdc1aSPaul Moore 	if (ret_val == 0)
448c783f1ceSPaul Moore 		atomic_inc(&netlabel_mgmt_protocount);
44996cb8e33SPaul Moore 
45096cb8e33SPaul Moore 	return ret_val;
45196cb8e33SPaul Moore }
45296cb8e33SPaul Moore 
45396cb8e33SPaul Moore /**
45496cb8e33SPaul Moore  * netlbl_cipsov4_list - Handle a LIST message
45596cb8e33SPaul Moore  * @skb: the NETLINK buffer
45696cb8e33SPaul Moore  * @info: the Generic NETLINK info block
45796cb8e33SPaul Moore  *
45896cb8e33SPaul Moore  * Description:
459fd385855SPaul Moore  * Process a user generated LIST message and respond accordingly.  While the
460fd385855SPaul Moore  * response message generated by the kernel is straightforward, determining
461fd385855SPaul Moore  * before hand the size of the buffer to allocate is not (we have to generate
462fd385855SPaul Moore  * the message to know the size).  In order to keep this function sane what we
463fd385855SPaul Moore  * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
464fd385855SPaul Moore  * that size, if we fail then we restart with a larger buffer and try again.
465fd385855SPaul Moore  * We continue in this manner until we hit a limit of failed attempts then we
466fd385855SPaul Moore  * give up and just send an error message.  Returns zero on success and
467fd385855SPaul Moore  * negative values on error.
46896cb8e33SPaul Moore  *
46996cb8e33SPaul Moore  */
47096cb8e33SPaul Moore static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
47196cb8e33SPaul Moore {
472fd385855SPaul Moore 	int ret_val;
473fd385855SPaul Moore 	struct sk_buff *ans_skb = NULL;
474fd385855SPaul Moore 	u32 nlsze_mult = 1;
475fd385855SPaul Moore 	void *data;
47696cb8e33SPaul Moore 	u32 doi;
477fd385855SPaul Moore 	struct nlattr *nla_a;
478fd385855SPaul Moore 	struct nlattr *nla_b;
479fd385855SPaul Moore 	struct cipso_v4_doi *doi_def;
480fd385855SPaul Moore 	u32 iter;
48196cb8e33SPaul Moore 
482fd385855SPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
483fd385855SPaul Moore 		ret_val = -EINVAL;
48496cb8e33SPaul Moore 		goto list_failure;
485fd385855SPaul Moore 	}
48696cb8e33SPaul Moore 
487fd385855SPaul Moore list_start:
488339bf98fSThomas Graf 	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
48996cb8e33SPaul Moore 	if (ans_skb == NULL) {
49096cb8e33SPaul Moore 		ret_val = -ENOMEM;
49196cb8e33SPaul Moore 		goto list_failure;
49296cb8e33SPaul Moore 	}
49317c157c8SThomas Graf 	data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
49417c157c8SThomas Graf 				 0, NLBL_CIPSOV4_C_LIST);
495fd385855SPaul Moore 	if (data == NULL) {
496fd385855SPaul Moore 		ret_val = -ENOMEM;
497fd385855SPaul Moore 		goto list_failure;
498fd385855SPaul Moore 	}
49996cb8e33SPaul Moore 
500fd385855SPaul Moore 	doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
501fd385855SPaul Moore 
502fd385855SPaul Moore 	rcu_read_lock();
503fd385855SPaul Moore 	doi_def = cipso_v4_doi_getdef(doi);
504fd385855SPaul Moore 	if (doi_def == NULL) {
505fd385855SPaul Moore 		ret_val = -EINVAL;
50656196701SPaul Moore 		goto list_failure_lock;
507fd385855SPaul Moore 	}
508fd385855SPaul Moore 
509fd385855SPaul Moore 	ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
510fd385855SPaul Moore 	if (ret_val != 0)
511fd385855SPaul Moore 		goto list_failure_lock;
512fd385855SPaul Moore 
513fd385855SPaul Moore 	nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
514fd385855SPaul Moore 	if (nla_a == NULL) {
515fd385855SPaul Moore 		ret_val = -ENOMEM;
516fd385855SPaul Moore 		goto list_failure_lock;
517fd385855SPaul Moore 	}
518fd385855SPaul Moore 	for (iter = 0;
519fd385855SPaul Moore 	     iter < CIPSO_V4_TAG_MAXCNT &&
520fd385855SPaul Moore 	       doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
521fd385855SPaul Moore 	     iter++) {
522fd385855SPaul Moore 		ret_val = nla_put_u8(ans_skb,
523fd385855SPaul Moore 				     NLBL_CIPSOV4_A_TAG,
524fd385855SPaul Moore 				     doi_def->tags[iter]);
525fd385855SPaul Moore 		if (ret_val != 0)
526fd385855SPaul Moore 			goto list_failure_lock;
527fd385855SPaul Moore 	}
528fd385855SPaul Moore 	nla_nest_end(ans_skb, nla_a);
529fd385855SPaul Moore 
530fd385855SPaul Moore 	switch (doi_def->type) {
53115c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
532fd385855SPaul Moore 		nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
533fd385855SPaul Moore 		if (nla_a == NULL) {
534fd385855SPaul Moore 			ret_val = -ENOMEM;
535fd385855SPaul Moore 			goto list_failure_lock;
536fd385855SPaul Moore 		}
537fd385855SPaul Moore 		for (iter = 0;
538fd385855SPaul Moore 		     iter < doi_def->map.std->lvl.local_size;
539fd385855SPaul Moore 		     iter++) {
540fd385855SPaul Moore 			if (doi_def->map.std->lvl.local[iter] ==
541fd385855SPaul Moore 			    CIPSO_V4_INV_LVL)
542fd385855SPaul Moore 				continue;
543fd385855SPaul Moore 
544fd385855SPaul Moore 			nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
545fd385855SPaul Moore 			if (nla_b == NULL) {
546fd385855SPaul Moore 				ret_val = -ENOMEM;
547fd385855SPaul Moore 				goto list_retry;
548fd385855SPaul Moore 			}
549fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
550fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MLSLVLLOC,
551fd385855SPaul Moore 					      iter);
552fd385855SPaul Moore 			if (ret_val != 0)
553fd385855SPaul Moore 				goto list_retry;
554fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
555fd385855SPaul Moore 					    NLBL_CIPSOV4_A_MLSLVLREM,
556fd385855SPaul Moore 					    doi_def->map.std->lvl.local[iter]);
557fd385855SPaul Moore 			if (ret_val != 0)
558fd385855SPaul Moore 				goto list_retry;
559fd385855SPaul Moore 			nla_nest_end(ans_skb, nla_b);
560fd385855SPaul Moore 		}
561fd385855SPaul Moore 		nla_nest_end(ans_skb, nla_a);
562fd385855SPaul Moore 
563fd385855SPaul Moore 		nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
564fd385855SPaul Moore 		if (nla_a == NULL) {
565fd385855SPaul Moore 			ret_val = -ENOMEM;
566fd385855SPaul Moore 			goto list_retry;
567fd385855SPaul Moore 		}
568fd385855SPaul Moore 		for (iter = 0;
569fd385855SPaul Moore 		     iter < doi_def->map.std->cat.local_size;
570fd385855SPaul Moore 		     iter++) {
571fd385855SPaul Moore 			if (doi_def->map.std->cat.local[iter] ==
572fd385855SPaul Moore 			    CIPSO_V4_INV_CAT)
573fd385855SPaul Moore 				continue;
574fd385855SPaul Moore 
575fd385855SPaul Moore 			nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
576fd385855SPaul Moore 			if (nla_b == NULL) {
577fd385855SPaul Moore 				ret_val = -ENOMEM;
578fd385855SPaul Moore 				goto list_retry;
579fd385855SPaul Moore 			}
580fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
581fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MLSCATLOC,
582fd385855SPaul Moore 					      iter);
583fd385855SPaul Moore 			if (ret_val != 0)
584fd385855SPaul Moore 				goto list_retry;
585fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
586fd385855SPaul Moore 					    NLBL_CIPSOV4_A_MLSCATREM,
587fd385855SPaul Moore 					    doi_def->map.std->cat.local[iter]);
588fd385855SPaul Moore 			if (ret_val != 0)
589fd385855SPaul Moore 				goto list_retry;
590fd385855SPaul Moore 			nla_nest_end(ans_skb, nla_b);
591fd385855SPaul Moore 		}
592fd385855SPaul Moore 		nla_nest_end(ans_skb, nla_a);
593fd385855SPaul Moore 
594fd385855SPaul Moore 		break;
595fd385855SPaul Moore 	}
596fd385855SPaul Moore 	rcu_read_unlock();
597fd385855SPaul Moore 
598fd385855SPaul Moore 	genlmsg_end(ans_skb, data);
599fe785beeSDenis V. Lunev 	return genlmsg_reply(ans_skb, info);
60096cb8e33SPaul Moore 
601fd385855SPaul Moore list_retry:
602fd385855SPaul Moore 	/* XXX - this limit is a guesstimate */
603fd385855SPaul Moore 	if (nlsze_mult < 4) {
604fd385855SPaul Moore 		rcu_read_unlock();
605fd385855SPaul Moore 		kfree_skb(ans_skb);
60683aa2e96SDenis V. Lunev 		nlsze_mult *= 2;
607fd385855SPaul Moore 		goto list_start;
608fd385855SPaul Moore 	}
609fd385855SPaul Moore list_failure_lock:
610fd385855SPaul Moore 	rcu_read_unlock();
61196cb8e33SPaul Moore list_failure:
612fd385855SPaul Moore 	kfree_skb(ans_skb);
613fd385855SPaul Moore 	return ret_val;
614fd385855SPaul Moore }
615fd385855SPaul Moore 
616fd385855SPaul Moore /**
617fd385855SPaul Moore  * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
618fd385855SPaul Moore  * @doi_def: the CIPSOv4 DOI definition
619fd385855SPaul Moore  * @arg: the netlbl_cipsov4_doiwalk_arg structure
620fd385855SPaul Moore  *
621fd385855SPaul Moore  * Description:
622fd385855SPaul Moore  * This function is designed to be used as a callback to the
623fd385855SPaul Moore  * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
624fd385855SPaul Moore  * message.  Returns the size of the message on success, negative values on
625fd385855SPaul Moore  * failure.
626fd385855SPaul Moore  *
627fd385855SPaul Moore  */
628fd385855SPaul Moore static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
629fd385855SPaul Moore {
630fd385855SPaul Moore 	int ret_val = -ENOMEM;
631fd385855SPaul Moore 	struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
632fd385855SPaul Moore 	void *data;
633fd385855SPaul Moore 
63417c157c8SThomas Graf 	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
63517c157c8SThomas Graf 			   cb_arg->seq, &netlbl_cipsov4_gnl_family,
63617c157c8SThomas Graf 			   NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
637fd385855SPaul Moore 	if (data == NULL)
638fd385855SPaul Moore 		goto listall_cb_failure;
639fd385855SPaul Moore 
640fd385855SPaul Moore 	ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
641fd385855SPaul Moore 	if (ret_val != 0)
642fd385855SPaul Moore 		goto listall_cb_failure;
643fd385855SPaul Moore 	ret_val = nla_put_u32(cb_arg->skb,
644fd385855SPaul Moore 			      NLBL_CIPSOV4_A_MTYPE,
645fd385855SPaul Moore 			      doi_def->type);
646fd385855SPaul Moore 	if (ret_val != 0)
647fd385855SPaul Moore 		goto listall_cb_failure;
648fd385855SPaul Moore 
649fd385855SPaul Moore 	return genlmsg_end(cb_arg->skb, data);
650fd385855SPaul Moore 
651fd385855SPaul Moore listall_cb_failure:
652fd385855SPaul Moore 	genlmsg_cancel(cb_arg->skb, data);
65396cb8e33SPaul Moore 	return ret_val;
65496cb8e33SPaul Moore }
65596cb8e33SPaul Moore 
65696cb8e33SPaul Moore /**
65796cb8e33SPaul Moore  * netlbl_cipsov4_listall - Handle a LISTALL message
65896cb8e33SPaul Moore  * @skb: the NETLINK buffer
659fd385855SPaul Moore  * @cb: the NETLINK callback
66096cb8e33SPaul Moore  *
66196cb8e33SPaul Moore  * Description:
66296cb8e33SPaul Moore  * Process a user generated LISTALL message and respond accordingly.  Returns
66396cb8e33SPaul Moore  * zero on success and negative values on error.
66496cb8e33SPaul Moore  *
66596cb8e33SPaul Moore  */
666fd385855SPaul Moore static int netlbl_cipsov4_listall(struct sk_buff *skb,
667fd385855SPaul Moore 				  struct netlink_callback *cb)
66896cb8e33SPaul Moore {
669fd385855SPaul Moore 	struct netlbl_cipsov4_doiwalk_arg cb_arg;
67056196701SPaul Moore 	u32 doi_skip = cb->args[0];
67196cb8e33SPaul Moore 
672fd385855SPaul Moore 	cb_arg.nl_cb = cb;
673fd385855SPaul Moore 	cb_arg.skb = skb;
674fd385855SPaul Moore 	cb_arg.seq = cb->nlh->nlmsg_seq;
67596cb8e33SPaul Moore 
676fd385855SPaul Moore 	cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
67796cb8e33SPaul Moore 
678fd385855SPaul Moore 	cb->args[0] = doi_skip;
679fd385855SPaul Moore 	return skb->len;
68096cb8e33SPaul Moore }
68196cb8e33SPaul Moore 
68296cb8e33SPaul Moore /**
683b1edeb10SPaul Moore  * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
684b1edeb10SPaul Moore  * @entry: LSM domain mapping entry
685b1edeb10SPaul Moore  * @arg: the netlbl_domhsh_walk_arg structure
686b1edeb10SPaul Moore  *
687b1edeb10SPaul Moore  * Description:
688b1edeb10SPaul Moore  * This function is intended for use by netlbl_cipsov4_remove() as the callback
689b1edeb10SPaul Moore  * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
690b1edeb10SPaul Moore  * which are associated with the CIPSO DOI specified in @arg.  Returns zero on
691b1edeb10SPaul Moore  * success, negative values on failure.
692b1edeb10SPaul Moore  *
693b1edeb10SPaul Moore  */
694b1edeb10SPaul Moore static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
695b1edeb10SPaul Moore {
696b1edeb10SPaul Moore 	struct netlbl_domhsh_walk_arg *cb_arg = arg;
697b1edeb10SPaul Moore 
698b1edeb10SPaul Moore 	if (entry->type == NETLBL_NLTYPE_CIPSOV4 &&
699b1edeb10SPaul Moore 	    entry->type_def.cipsov4->doi == cb_arg->doi)
700b1edeb10SPaul Moore 		return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
701b1edeb10SPaul Moore 
702b1edeb10SPaul Moore 	return 0;
703b1edeb10SPaul Moore }
704b1edeb10SPaul Moore 
705b1edeb10SPaul Moore /**
70696cb8e33SPaul Moore  * netlbl_cipsov4_remove - Handle a REMOVE message
70796cb8e33SPaul Moore  * @skb: the NETLINK buffer
70896cb8e33SPaul Moore  * @info: the Generic NETLINK info block
70996cb8e33SPaul Moore  *
71096cb8e33SPaul Moore  * Description:
71196cb8e33SPaul Moore  * Process a user generated REMOVE message and respond accordingly.  Returns
71296cb8e33SPaul Moore  * zero on success, negative values on failure.
71396cb8e33SPaul Moore  *
71496cb8e33SPaul Moore  */
71596cb8e33SPaul Moore static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
71696cb8e33SPaul Moore {
717fd385855SPaul Moore 	int ret_val = -EINVAL;
718b1edeb10SPaul Moore 	struct netlbl_domhsh_walk_arg cb_arg;
71995d4e6beSPaul Moore 	struct netlbl_audit audit_info;
720b1edeb10SPaul Moore 	u32 skip_bkt = 0;
721b1edeb10SPaul Moore 	u32 skip_chain = 0;
72296cb8e33SPaul Moore 
72395d4e6beSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI])
72495d4e6beSPaul Moore 		return -EINVAL;
72595d4e6beSPaul Moore 
72695d4e6beSPaul Moore 	netlbl_netlink_auditinfo(skb, &audit_info);
7276c2e8ac0SPaul Moore 	cb_arg.doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
728b1edeb10SPaul Moore 	cb_arg.audit_info = &audit_info;
729b1edeb10SPaul Moore 	ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
730b1edeb10SPaul Moore 				     netlbl_cipsov4_remove_cb, &cb_arg);
731b1edeb10SPaul Moore 	if (ret_val == 0 || ret_val == -ENOENT) {
7326c2e8ac0SPaul Moore 		ret_val = cipso_v4_doi_remove(cb_arg.doi, &audit_info);
73323bcdc1aSPaul Moore 		if (ret_val == 0)
734c783f1ceSPaul Moore 			atomic_dec(&netlabel_mgmt_protocount);
735b1edeb10SPaul Moore 	}
73695d4e6beSPaul Moore 
73796cb8e33SPaul Moore 	return ret_val;
73896cb8e33SPaul Moore }
73996cb8e33SPaul Moore 
74096cb8e33SPaul Moore /*
74196cb8e33SPaul Moore  * NetLabel Generic NETLINK Command Definitions
74296cb8e33SPaul Moore  */
74396cb8e33SPaul Moore 
744227c43c3SPavel Emelyanov static struct genl_ops netlbl_cipsov4_ops[] = {
745227c43c3SPavel Emelyanov 	{
74696cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_ADD,
747fd385855SPaul Moore 	.flags = GENL_ADMIN_PERM,
748fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
74996cb8e33SPaul Moore 	.doit = netlbl_cipsov4_add,
75096cb8e33SPaul Moore 	.dumpit = NULL,
751227c43c3SPavel Emelyanov 	},
752227c43c3SPavel Emelyanov 	{
75396cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_REMOVE,
754fd385855SPaul Moore 	.flags = GENL_ADMIN_PERM,
755fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
75696cb8e33SPaul Moore 	.doit = netlbl_cipsov4_remove,
75796cb8e33SPaul Moore 	.dumpit = NULL,
758227c43c3SPavel Emelyanov 	},
759227c43c3SPavel Emelyanov 	{
76096cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_LIST,
76196cb8e33SPaul Moore 	.flags = 0,
762fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
76396cb8e33SPaul Moore 	.doit = netlbl_cipsov4_list,
76496cb8e33SPaul Moore 	.dumpit = NULL,
765227c43c3SPavel Emelyanov 	},
766227c43c3SPavel Emelyanov 	{
76796cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_LISTALL,
76896cb8e33SPaul Moore 	.flags = 0,
769fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
770fd385855SPaul Moore 	.doit = NULL,
771fd385855SPaul Moore 	.dumpit = netlbl_cipsov4_listall,
772227c43c3SPavel Emelyanov 	},
77396cb8e33SPaul Moore };
77496cb8e33SPaul Moore 
77596cb8e33SPaul Moore /*
77696cb8e33SPaul Moore  * NetLabel Generic NETLINK Protocol Functions
77796cb8e33SPaul Moore  */
77896cb8e33SPaul Moore 
77996cb8e33SPaul Moore /**
78096cb8e33SPaul Moore  * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
78196cb8e33SPaul Moore  *
78296cb8e33SPaul Moore  * Description:
78396cb8e33SPaul Moore  * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
78496cb8e33SPaul Moore  * mechanism.  Returns zero on success, negative values on failure.
78596cb8e33SPaul Moore  *
78696cb8e33SPaul Moore  */
78705705e4eSPavel Emelyanov int __init netlbl_cipsov4_genl_init(void)
78896cb8e33SPaul Moore {
7897ae740dfSMichał Mirosław 	return genl_register_family_with_ops(&netlbl_cipsov4_gnl_family,
7907ae740dfSMichał Mirosław 		netlbl_cipsov4_ops, ARRAY_SIZE(netlbl_cipsov4_ops));
79196cb8e33SPaul Moore }
792