xref: /openbmc/linux/net/netlabel/netlabel_cipso_v4.c (revision 60063497a95e716c9a689af3be2687d261f115b4)
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>
365a0e3ad6STejun 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>
42*60063497SArun Sharma #include <linux/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;
42595d4e6beSPaul Moore 	struct netlbl_audit audit_info;
42696cb8e33SPaul Moore 
42732f50cdeSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
42832f50cdeSPaul Moore 	    !info->attrs[NLBL_CIPSOV4_A_MTYPE])
429fd385855SPaul Moore 		return -EINVAL;
43096cb8e33SPaul Moore 
43195d4e6beSPaul Moore 	netlbl_netlink_auditinfo(skb, &audit_info);
4326c2e8ac0SPaul Moore 	switch (nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE])) {
43315c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
4346c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_std(info, &audit_info);
43596cb8e33SPaul Moore 		break;
43696cb8e33SPaul Moore 	case CIPSO_V4_MAP_PASS:
4376c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_pass(info, &audit_info);
43896cb8e33SPaul Moore 		break;
439d91d4079SPaul Moore 	case CIPSO_V4_MAP_LOCAL:
4406c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_local(info, &audit_info);
441d91d4079SPaul Moore 		break;
44296cb8e33SPaul Moore 	}
44323bcdc1aSPaul Moore 	if (ret_val == 0)
444c783f1ceSPaul Moore 		atomic_inc(&netlabel_mgmt_protocount);
44596cb8e33SPaul Moore 
44696cb8e33SPaul Moore 	return ret_val;
44796cb8e33SPaul Moore }
44896cb8e33SPaul Moore 
44996cb8e33SPaul Moore /**
45096cb8e33SPaul Moore  * netlbl_cipsov4_list - Handle a LIST message
45196cb8e33SPaul Moore  * @skb: the NETLINK buffer
45296cb8e33SPaul Moore  * @info: the Generic NETLINK info block
45396cb8e33SPaul Moore  *
45496cb8e33SPaul Moore  * Description:
455fd385855SPaul Moore  * Process a user generated LIST message and respond accordingly.  While the
456fd385855SPaul Moore  * response message generated by the kernel is straightforward, determining
457fd385855SPaul Moore  * before hand the size of the buffer to allocate is not (we have to generate
458fd385855SPaul Moore  * the message to know the size).  In order to keep this function sane what we
459fd385855SPaul Moore  * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
460fd385855SPaul Moore  * that size, if we fail then we restart with a larger buffer and try again.
461fd385855SPaul Moore  * We continue in this manner until we hit a limit of failed attempts then we
462fd385855SPaul Moore  * give up and just send an error message.  Returns zero on success and
463fd385855SPaul Moore  * negative values on error.
46496cb8e33SPaul Moore  *
46596cb8e33SPaul Moore  */
46696cb8e33SPaul Moore static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
46796cb8e33SPaul Moore {
468fd385855SPaul Moore 	int ret_val;
469fd385855SPaul Moore 	struct sk_buff *ans_skb = NULL;
470fd385855SPaul Moore 	u32 nlsze_mult = 1;
471fd385855SPaul Moore 	void *data;
47296cb8e33SPaul Moore 	u32 doi;
473fd385855SPaul Moore 	struct nlattr *nla_a;
474fd385855SPaul Moore 	struct nlattr *nla_b;
475fd385855SPaul Moore 	struct cipso_v4_doi *doi_def;
476fd385855SPaul Moore 	u32 iter;
47796cb8e33SPaul Moore 
478fd385855SPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
479fd385855SPaul Moore 		ret_val = -EINVAL;
48096cb8e33SPaul Moore 		goto list_failure;
481fd385855SPaul Moore 	}
48296cb8e33SPaul Moore 
483fd385855SPaul Moore list_start:
484339bf98fSThomas Graf 	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
48596cb8e33SPaul Moore 	if (ans_skb == NULL) {
48696cb8e33SPaul Moore 		ret_val = -ENOMEM;
48796cb8e33SPaul Moore 		goto list_failure;
48896cb8e33SPaul Moore 	}
48917c157c8SThomas Graf 	data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
49017c157c8SThomas Graf 				 0, NLBL_CIPSOV4_C_LIST);
491fd385855SPaul Moore 	if (data == NULL) {
492fd385855SPaul Moore 		ret_val = -ENOMEM;
493fd385855SPaul Moore 		goto list_failure;
494fd385855SPaul Moore 	}
49596cb8e33SPaul Moore 
496fd385855SPaul Moore 	doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
497fd385855SPaul Moore 
498fd385855SPaul Moore 	rcu_read_lock();
499fd385855SPaul Moore 	doi_def = cipso_v4_doi_getdef(doi);
500fd385855SPaul Moore 	if (doi_def == NULL) {
501fd385855SPaul Moore 		ret_val = -EINVAL;
50256196701SPaul Moore 		goto list_failure_lock;
503fd385855SPaul Moore 	}
504fd385855SPaul Moore 
505fd385855SPaul Moore 	ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
506fd385855SPaul Moore 	if (ret_val != 0)
507fd385855SPaul Moore 		goto list_failure_lock;
508fd385855SPaul Moore 
509fd385855SPaul Moore 	nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
510fd385855SPaul Moore 	if (nla_a == NULL) {
511fd385855SPaul Moore 		ret_val = -ENOMEM;
512fd385855SPaul Moore 		goto list_failure_lock;
513fd385855SPaul Moore 	}
514fd385855SPaul Moore 	for (iter = 0;
515fd385855SPaul Moore 	     iter < CIPSO_V4_TAG_MAXCNT &&
516fd385855SPaul Moore 	       doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
517fd385855SPaul Moore 	     iter++) {
518fd385855SPaul Moore 		ret_val = nla_put_u8(ans_skb,
519fd385855SPaul Moore 				     NLBL_CIPSOV4_A_TAG,
520fd385855SPaul Moore 				     doi_def->tags[iter]);
521fd385855SPaul Moore 		if (ret_val != 0)
522fd385855SPaul Moore 			goto list_failure_lock;
523fd385855SPaul Moore 	}
524fd385855SPaul Moore 	nla_nest_end(ans_skb, nla_a);
525fd385855SPaul Moore 
526fd385855SPaul Moore 	switch (doi_def->type) {
52715c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
528fd385855SPaul Moore 		nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
529fd385855SPaul Moore 		if (nla_a == NULL) {
530fd385855SPaul Moore 			ret_val = -ENOMEM;
531fd385855SPaul Moore 			goto list_failure_lock;
532fd385855SPaul Moore 		}
533fd385855SPaul Moore 		for (iter = 0;
534fd385855SPaul Moore 		     iter < doi_def->map.std->lvl.local_size;
535fd385855SPaul Moore 		     iter++) {
536fd385855SPaul Moore 			if (doi_def->map.std->lvl.local[iter] ==
537fd385855SPaul Moore 			    CIPSO_V4_INV_LVL)
538fd385855SPaul Moore 				continue;
539fd385855SPaul Moore 
540fd385855SPaul Moore 			nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
541fd385855SPaul Moore 			if (nla_b == NULL) {
542fd385855SPaul Moore 				ret_val = -ENOMEM;
543fd385855SPaul Moore 				goto list_retry;
544fd385855SPaul Moore 			}
545fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
546fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MLSLVLLOC,
547fd385855SPaul Moore 					      iter);
548fd385855SPaul Moore 			if (ret_val != 0)
549fd385855SPaul Moore 				goto list_retry;
550fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
551fd385855SPaul Moore 					    NLBL_CIPSOV4_A_MLSLVLREM,
552fd385855SPaul Moore 					    doi_def->map.std->lvl.local[iter]);
553fd385855SPaul Moore 			if (ret_val != 0)
554fd385855SPaul Moore 				goto list_retry;
555fd385855SPaul Moore 			nla_nest_end(ans_skb, nla_b);
556fd385855SPaul Moore 		}
557fd385855SPaul Moore 		nla_nest_end(ans_skb, nla_a);
558fd385855SPaul Moore 
559fd385855SPaul Moore 		nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
560fd385855SPaul Moore 		if (nla_a == NULL) {
561fd385855SPaul Moore 			ret_val = -ENOMEM;
562fd385855SPaul Moore 			goto list_retry;
563fd385855SPaul Moore 		}
564fd385855SPaul Moore 		for (iter = 0;
565fd385855SPaul Moore 		     iter < doi_def->map.std->cat.local_size;
566fd385855SPaul Moore 		     iter++) {
567fd385855SPaul Moore 			if (doi_def->map.std->cat.local[iter] ==
568fd385855SPaul Moore 			    CIPSO_V4_INV_CAT)
569fd385855SPaul Moore 				continue;
570fd385855SPaul Moore 
571fd385855SPaul Moore 			nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
572fd385855SPaul Moore 			if (nla_b == NULL) {
573fd385855SPaul Moore 				ret_val = -ENOMEM;
574fd385855SPaul Moore 				goto list_retry;
575fd385855SPaul Moore 			}
576fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
577fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MLSCATLOC,
578fd385855SPaul Moore 					      iter);
579fd385855SPaul Moore 			if (ret_val != 0)
580fd385855SPaul Moore 				goto list_retry;
581fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
582fd385855SPaul Moore 					    NLBL_CIPSOV4_A_MLSCATREM,
583fd385855SPaul Moore 					    doi_def->map.std->cat.local[iter]);
584fd385855SPaul Moore 			if (ret_val != 0)
585fd385855SPaul Moore 				goto list_retry;
586fd385855SPaul Moore 			nla_nest_end(ans_skb, nla_b);
587fd385855SPaul Moore 		}
588fd385855SPaul Moore 		nla_nest_end(ans_skb, nla_a);
589fd385855SPaul Moore 
590fd385855SPaul Moore 		break;
591fd385855SPaul Moore 	}
592fd385855SPaul Moore 	rcu_read_unlock();
593fd385855SPaul Moore 
594fd385855SPaul Moore 	genlmsg_end(ans_skb, data);
595fe785beeSDenis V. Lunev 	return genlmsg_reply(ans_skb, info);
59696cb8e33SPaul Moore 
597fd385855SPaul Moore list_retry:
598fd385855SPaul Moore 	/* XXX - this limit is a guesstimate */
599fd385855SPaul Moore 	if (nlsze_mult < 4) {
600fd385855SPaul Moore 		rcu_read_unlock();
601fd385855SPaul Moore 		kfree_skb(ans_skb);
60283aa2e96SDenis V. Lunev 		nlsze_mult *= 2;
603fd385855SPaul Moore 		goto list_start;
604fd385855SPaul Moore 	}
605fd385855SPaul Moore list_failure_lock:
606fd385855SPaul Moore 	rcu_read_unlock();
60796cb8e33SPaul Moore list_failure:
608fd385855SPaul Moore 	kfree_skb(ans_skb);
609fd385855SPaul Moore 	return ret_val;
610fd385855SPaul Moore }
611fd385855SPaul Moore 
612fd385855SPaul Moore /**
613fd385855SPaul Moore  * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
614fd385855SPaul Moore  * @doi_def: the CIPSOv4 DOI definition
615fd385855SPaul Moore  * @arg: the netlbl_cipsov4_doiwalk_arg structure
616fd385855SPaul Moore  *
617fd385855SPaul Moore  * Description:
618fd385855SPaul Moore  * This function is designed to be used as a callback to the
619fd385855SPaul Moore  * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
620fd385855SPaul Moore  * message.  Returns the size of the message on success, negative values on
621fd385855SPaul Moore  * failure.
622fd385855SPaul Moore  *
623fd385855SPaul Moore  */
624fd385855SPaul Moore static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
625fd385855SPaul Moore {
626fd385855SPaul Moore 	int ret_val = -ENOMEM;
627fd385855SPaul Moore 	struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
628fd385855SPaul Moore 	void *data;
629fd385855SPaul Moore 
63017c157c8SThomas Graf 	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
63117c157c8SThomas Graf 			   cb_arg->seq, &netlbl_cipsov4_gnl_family,
63217c157c8SThomas Graf 			   NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
633fd385855SPaul Moore 	if (data == NULL)
634fd385855SPaul Moore 		goto listall_cb_failure;
635fd385855SPaul Moore 
636fd385855SPaul Moore 	ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
637fd385855SPaul Moore 	if (ret_val != 0)
638fd385855SPaul Moore 		goto listall_cb_failure;
639fd385855SPaul Moore 	ret_val = nla_put_u32(cb_arg->skb,
640fd385855SPaul Moore 			      NLBL_CIPSOV4_A_MTYPE,
641fd385855SPaul Moore 			      doi_def->type);
642fd385855SPaul Moore 	if (ret_val != 0)
643fd385855SPaul Moore 		goto listall_cb_failure;
644fd385855SPaul Moore 
645fd385855SPaul Moore 	return genlmsg_end(cb_arg->skb, data);
646fd385855SPaul Moore 
647fd385855SPaul Moore listall_cb_failure:
648fd385855SPaul Moore 	genlmsg_cancel(cb_arg->skb, data);
64996cb8e33SPaul Moore 	return ret_val;
65096cb8e33SPaul Moore }
65196cb8e33SPaul Moore 
65296cb8e33SPaul Moore /**
65396cb8e33SPaul Moore  * netlbl_cipsov4_listall - Handle a LISTALL message
65496cb8e33SPaul Moore  * @skb: the NETLINK buffer
655fd385855SPaul Moore  * @cb: the NETLINK callback
65696cb8e33SPaul Moore  *
65796cb8e33SPaul Moore  * Description:
65896cb8e33SPaul Moore  * Process a user generated LISTALL message and respond accordingly.  Returns
65996cb8e33SPaul Moore  * zero on success and negative values on error.
66096cb8e33SPaul Moore  *
66196cb8e33SPaul Moore  */
662fd385855SPaul Moore static int netlbl_cipsov4_listall(struct sk_buff *skb,
663fd385855SPaul Moore 				  struct netlink_callback *cb)
66496cb8e33SPaul Moore {
665fd385855SPaul Moore 	struct netlbl_cipsov4_doiwalk_arg cb_arg;
66656196701SPaul Moore 	u32 doi_skip = cb->args[0];
66796cb8e33SPaul Moore 
668fd385855SPaul Moore 	cb_arg.nl_cb = cb;
669fd385855SPaul Moore 	cb_arg.skb = skb;
670fd385855SPaul Moore 	cb_arg.seq = cb->nlh->nlmsg_seq;
67196cb8e33SPaul Moore 
672fd385855SPaul Moore 	cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
67396cb8e33SPaul Moore 
674fd385855SPaul Moore 	cb->args[0] = doi_skip;
675fd385855SPaul Moore 	return skb->len;
67696cb8e33SPaul Moore }
67796cb8e33SPaul Moore 
67896cb8e33SPaul Moore /**
679b1edeb10SPaul Moore  * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
680b1edeb10SPaul Moore  * @entry: LSM domain mapping entry
681b1edeb10SPaul Moore  * @arg: the netlbl_domhsh_walk_arg structure
682b1edeb10SPaul Moore  *
683b1edeb10SPaul Moore  * Description:
684b1edeb10SPaul Moore  * This function is intended for use by netlbl_cipsov4_remove() as the callback
685b1edeb10SPaul Moore  * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
686b1edeb10SPaul Moore  * which are associated with the CIPSO DOI specified in @arg.  Returns zero on
687b1edeb10SPaul Moore  * success, negative values on failure.
688b1edeb10SPaul Moore  *
689b1edeb10SPaul Moore  */
690b1edeb10SPaul Moore static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
691b1edeb10SPaul Moore {
692b1edeb10SPaul Moore 	struct netlbl_domhsh_walk_arg *cb_arg = arg;
693b1edeb10SPaul Moore 
694b1edeb10SPaul Moore 	if (entry->type == NETLBL_NLTYPE_CIPSOV4 &&
695b1edeb10SPaul Moore 	    entry->type_def.cipsov4->doi == cb_arg->doi)
696b1edeb10SPaul Moore 		return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
697b1edeb10SPaul Moore 
698b1edeb10SPaul Moore 	return 0;
699b1edeb10SPaul Moore }
700b1edeb10SPaul Moore 
701b1edeb10SPaul Moore /**
70296cb8e33SPaul Moore  * netlbl_cipsov4_remove - Handle a REMOVE message
70396cb8e33SPaul Moore  * @skb: the NETLINK buffer
70496cb8e33SPaul Moore  * @info: the Generic NETLINK info block
70596cb8e33SPaul Moore  *
70696cb8e33SPaul Moore  * Description:
70796cb8e33SPaul Moore  * Process a user generated REMOVE message and respond accordingly.  Returns
70896cb8e33SPaul Moore  * zero on success, negative values on failure.
70996cb8e33SPaul Moore  *
71096cb8e33SPaul Moore  */
71196cb8e33SPaul Moore static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
71296cb8e33SPaul Moore {
713fd385855SPaul Moore 	int ret_val = -EINVAL;
714b1edeb10SPaul Moore 	struct netlbl_domhsh_walk_arg cb_arg;
71595d4e6beSPaul Moore 	struct netlbl_audit audit_info;
716b1edeb10SPaul Moore 	u32 skip_bkt = 0;
717b1edeb10SPaul Moore 	u32 skip_chain = 0;
71896cb8e33SPaul Moore 
71995d4e6beSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI])
72095d4e6beSPaul Moore 		return -EINVAL;
72195d4e6beSPaul Moore 
72295d4e6beSPaul Moore 	netlbl_netlink_auditinfo(skb, &audit_info);
7236c2e8ac0SPaul Moore 	cb_arg.doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
724b1edeb10SPaul Moore 	cb_arg.audit_info = &audit_info;
725b1edeb10SPaul Moore 	ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
726b1edeb10SPaul Moore 				     netlbl_cipsov4_remove_cb, &cb_arg);
727b1edeb10SPaul Moore 	if (ret_val == 0 || ret_val == -ENOENT) {
7286c2e8ac0SPaul Moore 		ret_val = cipso_v4_doi_remove(cb_arg.doi, &audit_info);
72923bcdc1aSPaul Moore 		if (ret_val == 0)
730c783f1ceSPaul Moore 			atomic_dec(&netlabel_mgmt_protocount);
731b1edeb10SPaul Moore 	}
73295d4e6beSPaul Moore 
73396cb8e33SPaul Moore 	return ret_val;
73496cb8e33SPaul Moore }
73596cb8e33SPaul Moore 
73696cb8e33SPaul Moore /*
73796cb8e33SPaul Moore  * NetLabel Generic NETLINK Command Definitions
73896cb8e33SPaul Moore  */
73996cb8e33SPaul Moore 
740227c43c3SPavel Emelyanov static struct genl_ops netlbl_cipsov4_ops[] = {
741227c43c3SPavel Emelyanov 	{
74296cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_ADD,
743fd385855SPaul Moore 	.flags = GENL_ADMIN_PERM,
744fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
74596cb8e33SPaul Moore 	.doit = netlbl_cipsov4_add,
74696cb8e33SPaul Moore 	.dumpit = NULL,
747227c43c3SPavel Emelyanov 	},
748227c43c3SPavel Emelyanov 	{
74996cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_REMOVE,
750fd385855SPaul Moore 	.flags = GENL_ADMIN_PERM,
751fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
75296cb8e33SPaul Moore 	.doit = netlbl_cipsov4_remove,
75396cb8e33SPaul Moore 	.dumpit = NULL,
754227c43c3SPavel Emelyanov 	},
755227c43c3SPavel Emelyanov 	{
75696cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_LIST,
75796cb8e33SPaul Moore 	.flags = 0,
758fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
75996cb8e33SPaul Moore 	.doit = netlbl_cipsov4_list,
76096cb8e33SPaul Moore 	.dumpit = NULL,
761227c43c3SPavel Emelyanov 	},
762227c43c3SPavel Emelyanov 	{
76396cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_LISTALL,
76496cb8e33SPaul Moore 	.flags = 0,
765fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
766fd385855SPaul Moore 	.doit = NULL,
767fd385855SPaul Moore 	.dumpit = netlbl_cipsov4_listall,
768227c43c3SPavel Emelyanov 	},
76996cb8e33SPaul Moore };
77096cb8e33SPaul Moore 
77196cb8e33SPaul Moore /*
77296cb8e33SPaul Moore  * NetLabel Generic NETLINK Protocol Functions
77396cb8e33SPaul Moore  */
77496cb8e33SPaul Moore 
77596cb8e33SPaul Moore /**
77696cb8e33SPaul Moore  * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
77796cb8e33SPaul Moore  *
77896cb8e33SPaul Moore  * Description:
77996cb8e33SPaul Moore  * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
78096cb8e33SPaul Moore  * mechanism.  Returns zero on success, negative values on failure.
78196cb8e33SPaul Moore  *
78296cb8e33SPaul Moore  */
78305705e4eSPavel Emelyanov int __init netlbl_cipsov4_genl_init(void)
78496cb8e33SPaul Moore {
7857ae740dfSMichał Mirosław 	return genl_register_family_with_ops(&netlbl_cipsov4_gnl_family,
7867ae740dfSMichał Mirosław 		netlbl_cipsov4_ops, ARRAY_SIZE(netlbl_cipsov4_ops));
78796cb8e33SPaul Moore }
788