xref: /openbmc/linux/net/netlabel/netlabel_cipso_v4.c (revision 6c2e8ac0953fccdd24dc6c4b9e08e8f1cd68cf07)
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>
3696cb8e33SPaul Moore #include <net/sock.h>
3796cb8e33SPaul Moore #include <net/netlink.h>
3896cb8e33SPaul Moore #include <net/genetlink.h>
3996cb8e33SPaul Moore #include <net/netlabel.h>
4096cb8e33SPaul Moore #include <net/cipso_ipv4.h>
41c783f1ceSPaul Moore #include <asm/atomic.h>
4296cb8e33SPaul Moore 
4396cb8e33SPaul Moore #include "netlabel_user.h"
4496cb8e33SPaul Moore #include "netlabel_cipso_v4.h"
4523bcdc1aSPaul Moore #include "netlabel_mgmt.h"
46b1edeb10SPaul Moore #include "netlabel_domainhash.h"
4796cb8e33SPaul Moore 
48fd385855SPaul Moore /* Argument struct for cipso_v4_doi_walk() */
49fd385855SPaul Moore struct netlbl_cipsov4_doiwalk_arg {
50fd385855SPaul Moore 	struct netlink_callback *nl_cb;
51fd385855SPaul Moore 	struct sk_buff *skb;
52fd385855SPaul Moore 	u32 seq;
53fd385855SPaul Moore };
54fd385855SPaul Moore 
55b1edeb10SPaul Moore /* Argument struct for netlbl_domhsh_walk() */
56b1edeb10SPaul Moore struct netlbl_domhsh_walk_arg {
57b1edeb10SPaul Moore 	struct netlbl_audit *audit_info;
58b1edeb10SPaul Moore 	u32 doi;
59b1edeb10SPaul Moore };
60b1edeb10SPaul Moore 
6196cb8e33SPaul Moore /* NetLabel Generic NETLINK CIPSOv4 family */
6296cb8e33SPaul Moore static struct genl_family netlbl_cipsov4_gnl_family = {
6396cb8e33SPaul Moore 	.id = GENL_ID_GENERATE,
6496cb8e33SPaul Moore 	.hdrsize = 0,
6596cb8e33SPaul Moore 	.name = NETLBL_NLTYPE_CIPSOV4_NAME,
6696cb8e33SPaul Moore 	.version = NETLBL_PROTO_VERSION,
67fd385855SPaul Moore 	.maxattr = NLBL_CIPSOV4_A_MAX,
6896cb8e33SPaul Moore };
6996cb8e33SPaul Moore 
70fd385855SPaul Moore /* NetLabel Netlink attribute policy */
71ef7c79edSPatrick McHardy static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
72fd385855SPaul Moore 	[NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
73fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
74fd385855SPaul Moore 	[NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
75fd385855SPaul Moore 	[NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
76fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
77fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
78fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
79fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
80fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
81fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
82fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
83fd385855SPaul Moore 	[NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
84fd385855SPaul Moore };
8596cb8e33SPaul Moore 
8696cb8e33SPaul Moore /*
8796cb8e33SPaul Moore  * Helper Functions
8896cb8e33SPaul Moore  */
8996cb8e33SPaul Moore 
9096cb8e33SPaul Moore /**
91fd385855SPaul Moore  * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
92fd385855SPaul Moore  * @info: the Generic NETLINK info block
93fd385855SPaul Moore  * @doi_def: the CIPSO V4 DOI definition
94fd385855SPaul Moore  *
95fd385855SPaul Moore  * Description:
96fd385855SPaul Moore  * Parse the common sections of a ADD message and fill in the related values
97fd385855SPaul Moore  * in @doi_def.  Returns zero on success, negative values on failure.
98fd385855SPaul Moore  *
99fd385855SPaul Moore  */
100fd385855SPaul Moore static int netlbl_cipsov4_add_common(struct genl_info *info,
101fd385855SPaul Moore 				     struct cipso_v4_doi *doi_def)
102fd385855SPaul Moore {
103fd385855SPaul Moore 	struct nlattr *nla;
104fd385855SPaul Moore 	int nla_rem;
105fd385855SPaul Moore 	u32 iter = 0;
106fd385855SPaul Moore 
107fd385855SPaul Moore 	doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
108fd385855SPaul Moore 
109fd385855SPaul Moore 	if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
110fd385855SPaul Moore 				NLBL_CIPSOV4_A_MAX,
111fd385855SPaul Moore 				netlbl_cipsov4_genl_policy) != 0)
112fd385855SPaul Moore 		return -EINVAL;
113fd385855SPaul Moore 
114fd385855SPaul Moore 	nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
1158f4c1f9bSThomas Graf 		if (nla_type(nla) == NLBL_CIPSOV4_A_TAG) {
1162a2f11c2SPaul Moore 			if (iter >= CIPSO_V4_TAG_MAXCNT)
117fd385855SPaul Moore 				return -EINVAL;
118fd385855SPaul Moore 			doi_def->tags[iter++] = nla_get_u8(nla);
119fd385855SPaul Moore 		}
1202a2f11c2SPaul Moore 	while (iter < CIPSO_V4_TAG_MAXCNT)
1212a2f11c2SPaul Moore 		doi_def->tags[iter++] = CIPSO_V4_TAG_INVALID;
122fd385855SPaul Moore 
123fd385855SPaul Moore 	return 0;
124fd385855SPaul Moore }
12596cb8e33SPaul Moore 
12696cb8e33SPaul Moore /*
12796cb8e33SPaul Moore  * NetLabel Command Handlers
12896cb8e33SPaul Moore  */
12996cb8e33SPaul Moore 
13096cb8e33SPaul Moore /**
13196cb8e33SPaul Moore  * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
132fd385855SPaul Moore  * @info: the Generic NETLINK info block
133*6c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
13496cb8e33SPaul Moore  *
13596cb8e33SPaul Moore  * Description:
13615c45f7bSPaul Moore  * Create a new CIPSO_V4_MAP_TRANS DOI definition based on the given ADD
13715c45f7bSPaul Moore  * message and add it to the CIPSO V4 engine.  Return zero on success and
13815c45f7bSPaul Moore  * non-zero on error.
13996cb8e33SPaul Moore  *
14096cb8e33SPaul Moore  */
141*6c2e8ac0SPaul Moore static int netlbl_cipsov4_add_std(struct genl_info *info,
142*6c2e8ac0SPaul Moore 				  struct netlbl_audit *audit_info)
14396cb8e33SPaul Moore {
14496cb8e33SPaul Moore 	int ret_val = -EINVAL;
14596cb8e33SPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
146fd385855SPaul Moore 	struct nlattr *nla_a;
147fd385855SPaul Moore 	struct nlattr *nla_b;
148fd385855SPaul Moore 	int nla_a_rem;
149fd385855SPaul Moore 	int nla_b_rem;
150caff5b6aSPaul Moore 	u32 iter;
15196cb8e33SPaul Moore 
15232f50cdeSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_TAGLST] ||
153fd385855SPaul Moore 	    !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST])
154fd385855SPaul Moore 		return -EINVAL;
155fd385855SPaul Moore 
156fd385855SPaul Moore 	if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
157fd385855SPaul Moore 				NLBL_CIPSOV4_A_MAX,
158fd385855SPaul Moore 				netlbl_cipsov4_genl_policy) != 0)
159fd385855SPaul Moore 		return -EINVAL;
16096cb8e33SPaul Moore 
16196cb8e33SPaul Moore 	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
162fd385855SPaul Moore 	if (doi_def == NULL)
163fd385855SPaul Moore 		return -ENOMEM;
16496cb8e33SPaul Moore 	doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
16596cb8e33SPaul Moore 	if (doi_def->map.std == NULL) {
16696cb8e33SPaul Moore 		ret_val = -ENOMEM;
16796cb8e33SPaul Moore 		goto add_std_failure;
16896cb8e33SPaul Moore 	}
16915c45f7bSPaul Moore 	doi_def->type = CIPSO_V4_MAP_TRANS;
17096cb8e33SPaul Moore 
171fd385855SPaul Moore 	ret_val = netlbl_cipsov4_add_common(info, doi_def);
172fd385855SPaul Moore 	if (ret_val != 0)
17396cb8e33SPaul Moore 		goto add_std_failure;
1741fd2a25bSPaul Moore 	ret_val = -EINVAL;
175fd385855SPaul Moore 
176fd385855SPaul Moore 	nla_for_each_nested(nla_a,
177fd385855SPaul Moore 			    info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
178fd385855SPaul Moore 			    nla_a_rem)
1798f4c1f9bSThomas Graf 		if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
1801fd2a25bSPaul Moore 			if (nla_validate_nested(nla_a,
1811fd2a25bSPaul Moore 					    NLBL_CIPSOV4_A_MAX,
1821fd2a25bSPaul Moore 					    netlbl_cipsov4_genl_policy) != 0)
1831fd2a25bSPaul Moore 					goto add_std_failure;
184fd385855SPaul Moore 			nla_for_each_nested(nla_b, nla_a, nla_b_rem)
1858f4c1f9bSThomas Graf 				switch (nla_type(nla_b)) {
186fd385855SPaul Moore 				case NLBL_CIPSOV4_A_MLSLVLLOC:
1871fd2a25bSPaul Moore 					if (nla_get_u32(nla_b) >
1881fd2a25bSPaul Moore 					    CIPSO_V4_MAX_LOC_LVLS)
1891fd2a25bSPaul Moore 						goto add_std_failure;
190fd385855SPaul Moore 					if (nla_get_u32(nla_b) >=
191fd385855SPaul Moore 					    doi_def->map.std->lvl.local_size)
192fd385855SPaul Moore 					     doi_def->map.std->lvl.local_size =
193fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
19496cb8e33SPaul Moore 					break;
195fd385855SPaul Moore 				case NLBL_CIPSOV4_A_MLSLVLREM:
1961fd2a25bSPaul Moore 					if (nla_get_u32(nla_b) >
1971fd2a25bSPaul Moore 					    CIPSO_V4_MAX_REM_LVLS)
1981fd2a25bSPaul Moore 						goto add_std_failure;
199fd385855SPaul Moore 					if (nla_get_u32(nla_b) >=
200fd385855SPaul Moore 					    doi_def->map.std->lvl.cipso_size)
201fd385855SPaul Moore 					     doi_def->map.std->lvl.cipso_size =
202fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
203fd385855SPaul Moore 					break;
20496cb8e33SPaul Moore 				}
20596cb8e33SPaul Moore 		}
20696cb8e33SPaul Moore 	doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
20796cb8e33SPaul Moore 					      sizeof(u32),
20896cb8e33SPaul Moore 					      GFP_KERNEL);
20996cb8e33SPaul Moore 	if (doi_def->map.std->lvl.local == NULL) {
21096cb8e33SPaul Moore 		ret_val = -ENOMEM;
21196cb8e33SPaul Moore 		goto add_std_failure;
21296cb8e33SPaul Moore 	}
21396cb8e33SPaul Moore 	doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
21496cb8e33SPaul Moore 					      sizeof(u32),
21596cb8e33SPaul Moore 					      GFP_KERNEL);
21696cb8e33SPaul Moore 	if (doi_def->map.std->lvl.cipso == NULL) {
21796cb8e33SPaul Moore 		ret_val = -ENOMEM;
21896cb8e33SPaul Moore 		goto add_std_failure;
21996cb8e33SPaul Moore 	}
220caff5b6aSPaul Moore 	for (iter = 0; iter < doi_def->map.std->lvl.local_size; iter++)
221caff5b6aSPaul Moore 		doi_def->map.std->lvl.local[iter] = CIPSO_V4_INV_LVL;
222caff5b6aSPaul Moore 	for (iter = 0; iter < doi_def->map.std->lvl.cipso_size; iter++)
223caff5b6aSPaul Moore 		doi_def->map.std->lvl.cipso[iter] = CIPSO_V4_INV_LVL;
224fd385855SPaul Moore 	nla_for_each_nested(nla_a,
225fd385855SPaul Moore 			    info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
226fd385855SPaul Moore 			    nla_a_rem)
2278f4c1f9bSThomas Graf 		if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
228fd385855SPaul Moore 			struct nlattr *lvl_loc;
229fd385855SPaul Moore 			struct nlattr *lvl_rem;
23096cb8e33SPaul Moore 
231fd385855SPaul Moore 			lvl_loc = nla_find_nested(nla_a,
232fd385855SPaul Moore 						  NLBL_CIPSOV4_A_MLSLVLLOC);
233fd385855SPaul Moore 			lvl_rem = nla_find_nested(nla_a,
234fd385855SPaul Moore 						  NLBL_CIPSOV4_A_MLSLVLREM);
235fd385855SPaul Moore 			if (lvl_loc == NULL || lvl_rem == NULL)
236fd385855SPaul Moore 				goto add_std_failure;
237fd385855SPaul Moore 			doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] =
238fd385855SPaul Moore 				nla_get_u32(lvl_rem);
239fd385855SPaul Moore 			doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] =
240fd385855SPaul Moore 				nla_get_u32(lvl_loc);
241fd385855SPaul Moore 		}
242fd385855SPaul Moore 
243fd385855SPaul Moore 	if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) {
244fd385855SPaul Moore 		if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
245fd385855SPaul Moore 					NLBL_CIPSOV4_A_MAX,
246fd385855SPaul Moore 					netlbl_cipsov4_genl_policy) != 0)
247fd385855SPaul Moore 			goto add_std_failure;
248fd385855SPaul Moore 
249fd385855SPaul Moore 		nla_for_each_nested(nla_a,
250fd385855SPaul Moore 				    info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
251fd385855SPaul Moore 				    nla_a_rem)
2528f4c1f9bSThomas Graf 			if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
253fd385855SPaul Moore 				if (nla_validate_nested(nla_a,
254fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MAX,
255fd385855SPaul Moore 					      netlbl_cipsov4_genl_policy) != 0)
256fd385855SPaul Moore 					goto add_std_failure;
257fd385855SPaul Moore 				nla_for_each_nested(nla_b, nla_a, nla_b_rem)
2588f4c1f9bSThomas Graf 					switch (nla_type(nla_b)) {
259fd385855SPaul Moore 					case NLBL_CIPSOV4_A_MLSCATLOC:
2601fd2a25bSPaul Moore 						if (nla_get_u32(nla_b) >
2611fd2a25bSPaul Moore 						    CIPSO_V4_MAX_LOC_CATS)
2621fd2a25bSPaul Moore 							goto add_std_failure;
263fd385855SPaul Moore 						if (nla_get_u32(nla_b) >=
264fd385855SPaul Moore 					      doi_def->map.std->cat.local_size)
265fd385855SPaul Moore 					     doi_def->map.std->cat.local_size =
266fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
267fd385855SPaul Moore 						break;
268fd385855SPaul Moore 					case NLBL_CIPSOV4_A_MLSCATREM:
2691fd2a25bSPaul Moore 						if (nla_get_u32(nla_b) >
2701fd2a25bSPaul Moore 						    CIPSO_V4_MAX_REM_CATS)
2711fd2a25bSPaul Moore 							goto add_std_failure;
272fd385855SPaul Moore 						if (nla_get_u32(nla_b) >=
273fd385855SPaul Moore 					      doi_def->map.std->cat.cipso_size)
274fd385855SPaul Moore 					     doi_def->map.std->cat.cipso_size =
275fd385855SPaul Moore 						     nla_get_u32(nla_b) + 1;
276fd385855SPaul Moore 						break;
277fd385855SPaul Moore 					}
278fd385855SPaul Moore 			}
279fd385855SPaul Moore 		doi_def->map.std->cat.local = kcalloc(
280fd385855SPaul Moore 					      doi_def->map.std->cat.local_size,
28196cb8e33SPaul Moore 					      sizeof(u32),
28296cb8e33SPaul Moore 					      GFP_KERNEL);
28396cb8e33SPaul Moore 		if (doi_def->map.std->cat.local == NULL) {
28496cb8e33SPaul Moore 			ret_val = -ENOMEM;
28596cb8e33SPaul Moore 			goto add_std_failure;
28696cb8e33SPaul Moore 		}
287fd385855SPaul Moore 		doi_def->map.std->cat.cipso = kcalloc(
288fd385855SPaul Moore 					      doi_def->map.std->cat.cipso_size,
28996cb8e33SPaul Moore 					      sizeof(u32),
29096cb8e33SPaul Moore 					      GFP_KERNEL);
29196cb8e33SPaul Moore 		if (doi_def->map.std->cat.cipso == NULL) {
29296cb8e33SPaul Moore 			ret_val = -ENOMEM;
29396cb8e33SPaul Moore 			goto add_std_failure;
29496cb8e33SPaul Moore 		}
295caff5b6aSPaul Moore 		for (iter = 0; iter < doi_def->map.std->cat.local_size; iter++)
296caff5b6aSPaul Moore 			doi_def->map.std->cat.local[iter] = CIPSO_V4_INV_CAT;
297caff5b6aSPaul Moore 		for (iter = 0; iter < doi_def->map.std->cat.cipso_size; iter++)
298caff5b6aSPaul Moore 			doi_def->map.std->cat.cipso[iter] = CIPSO_V4_INV_CAT;
299fd385855SPaul Moore 		nla_for_each_nested(nla_a,
300fd385855SPaul Moore 				    info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
301fd385855SPaul Moore 				    nla_a_rem)
3028f4c1f9bSThomas Graf 			if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
303fd385855SPaul Moore 				struct nlattr *cat_loc;
304fd385855SPaul Moore 				struct nlattr *cat_rem;
30596cb8e33SPaul Moore 
306fd385855SPaul Moore 				cat_loc = nla_find_nested(nla_a,
307fd385855SPaul Moore 						     NLBL_CIPSOV4_A_MLSCATLOC);
308fd385855SPaul Moore 				cat_rem = nla_find_nested(nla_a,
309fd385855SPaul Moore 						     NLBL_CIPSOV4_A_MLSCATREM);
310fd385855SPaul Moore 				if (cat_loc == NULL || cat_rem == NULL)
31196cb8e33SPaul Moore 					goto add_std_failure;
312fd385855SPaul Moore 				doi_def->map.std->cat.local[
313fd385855SPaul Moore 							nla_get_u32(cat_loc)] =
314fd385855SPaul Moore 					nla_get_u32(cat_rem);
315fd385855SPaul Moore 				doi_def->map.std->cat.cipso[
316fd385855SPaul Moore 							nla_get_u32(cat_rem)] =
317fd385855SPaul Moore 					nla_get_u32(cat_loc);
318fd385855SPaul Moore 			}
31996cb8e33SPaul Moore 	}
32096cb8e33SPaul Moore 
321*6c2e8ac0SPaul Moore 	ret_val = cipso_v4_doi_add(doi_def, audit_info);
32296cb8e33SPaul Moore 	if (ret_val != 0)
32396cb8e33SPaul Moore 		goto add_std_failure;
32496cb8e33SPaul Moore 	return 0;
32596cb8e33SPaul Moore 
32696cb8e33SPaul Moore add_std_failure:
32796cb8e33SPaul Moore 	if (doi_def)
328b1edeb10SPaul Moore 		cipso_v4_doi_free(doi_def);
32996cb8e33SPaul Moore 	return ret_val;
33096cb8e33SPaul Moore }
33196cb8e33SPaul Moore 
33296cb8e33SPaul Moore /**
33396cb8e33SPaul Moore  * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
334fd385855SPaul Moore  * @info: the Generic NETLINK info block
335*6c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
33696cb8e33SPaul Moore  *
33796cb8e33SPaul Moore  * Description:
33896cb8e33SPaul Moore  * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
33996cb8e33SPaul Moore  * and add it to the CIPSO V4 engine.  Return zero on success and non-zero on
34096cb8e33SPaul Moore  * error.
34196cb8e33SPaul Moore  *
34296cb8e33SPaul Moore  */
343*6c2e8ac0SPaul Moore static int netlbl_cipsov4_add_pass(struct genl_info *info,
344*6c2e8ac0SPaul Moore 				   struct netlbl_audit *audit_info)
34596cb8e33SPaul Moore {
346fd385855SPaul Moore 	int ret_val;
34796cb8e33SPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
34896cb8e33SPaul Moore 
34932f50cdeSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
350fd385855SPaul Moore 		return -EINVAL;
35196cb8e33SPaul Moore 
35296cb8e33SPaul Moore 	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
353fd385855SPaul Moore 	if (doi_def == NULL)
354fd385855SPaul Moore 		return -ENOMEM;
35596cb8e33SPaul Moore 	doi_def->type = CIPSO_V4_MAP_PASS;
35696cb8e33SPaul Moore 
357fd385855SPaul Moore 	ret_val = netlbl_cipsov4_add_common(info, doi_def);
358fd385855SPaul Moore 	if (ret_val != 0)
35996cb8e33SPaul Moore 		goto add_pass_failure;
36096cb8e33SPaul Moore 
361*6c2e8ac0SPaul Moore 	ret_val = cipso_v4_doi_add(doi_def, audit_info);
36296cb8e33SPaul Moore 	if (ret_val != 0)
36396cb8e33SPaul Moore 		goto add_pass_failure;
36496cb8e33SPaul Moore 	return 0;
36596cb8e33SPaul Moore 
36696cb8e33SPaul Moore add_pass_failure:
367b1edeb10SPaul Moore 	cipso_v4_doi_free(doi_def);
36896cb8e33SPaul Moore 	return ret_val;
36996cb8e33SPaul Moore }
37096cb8e33SPaul Moore 
37196cb8e33SPaul Moore /**
372d91d4079SPaul Moore  * netlbl_cipsov4_add_local - Adds a CIPSO V4 DOI definition
373d91d4079SPaul Moore  * @info: the Generic NETLINK info block
374*6c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
375d91d4079SPaul Moore  *
376d91d4079SPaul Moore  * Description:
377d91d4079SPaul Moore  * Create a new CIPSO_V4_MAP_LOCAL DOI definition based on the given ADD
378d91d4079SPaul Moore  * message and add it to the CIPSO V4 engine.  Return zero on success and
379d91d4079SPaul Moore  * non-zero on error.
380d91d4079SPaul Moore  *
381d91d4079SPaul Moore  */
382*6c2e8ac0SPaul Moore static int netlbl_cipsov4_add_local(struct genl_info *info,
383*6c2e8ac0SPaul Moore 				    struct netlbl_audit *audit_info)
384d91d4079SPaul Moore {
385d91d4079SPaul Moore 	int ret_val;
386d91d4079SPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
387d91d4079SPaul Moore 
388d91d4079SPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
389d91d4079SPaul Moore 		return -EINVAL;
390d91d4079SPaul Moore 
391d91d4079SPaul Moore 	doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
392d91d4079SPaul Moore 	if (doi_def == NULL)
393d91d4079SPaul Moore 		return -ENOMEM;
394d91d4079SPaul Moore 	doi_def->type = CIPSO_V4_MAP_LOCAL;
395d91d4079SPaul Moore 
396d91d4079SPaul Moore 	ret_val = netlbl_cipsov4_add_common(info, doi_def);
397d91d4079SPaul Moore 	if (ret_val != 0)
398d91d4079SPaul Moore 		goto add_local_failure;
399d91d4079SPaul Moore 
400*6c2e8ac0SPaul Moore 	ret_val = cipso_v4_doi_add(doi_def, audit_info);
401d91d4079SPaul Moore 	if (ret_val != 0)
402d91d4079SPaul Moore 		goto add_local_failure;
403d91d4079SPaul Moore 	return 0;
404d91d4079SPaul Moore 
405d91d4079SPaul Moore add_local_failure:
406d91d4079SPaul Moore 	cipso_v4_doi_free(doi_def);
407d91d4079SPaul Moore 	return ret_val;
408d91d4079SPaul Moore }
409d91d4079SPaul Moore 
410d91d4079SPaul Moore /**
41196cb8e33SPaul Moore  * netlbl_cipsov4_add - Handle an ADD message
41296cb8e33SPaul Moore  * @skb: the NETLINK buffer
41396cb8e33SPaul Moore  * @info: the Generic NETLINK info block
41496cb8e33SPaul Moore  *
41596cb8e33SPaul Moore  * Description:
41696cb8e33SPaul Moore  * Create a new DOI definition based on the given ADD message and add it to the
41796cb8e33SPaul Moore  * CIPSO V4 engine.  Returns zero on success, negative values on failure.
41896cb8e33SPaul Moore  *
41996cb8e33SPaul Moore  */
42096cb8e33SPaul Moore static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
42196cb8e33SPaul Moore 
42296cb8e33SPaul Moore {
42396cb8e33SPaul Moore 	int ret_val = -EINVAL;
42432f50cdeSPaul Moore 	const char *type_str = "(unknown)";
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);
432*6c2e8ac0SPaul Moore 	switch (nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE])) {
43315c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
43415c45f7bSPaul Moore 		type_str = "trans";
435*6c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_std(info, &audit_info);
43696cb8e33SPaul Moore 		break;
43796cb8e33SPaul Moore 	case CIPSO_V4_MAP_PASS:
43832f50cdeSPaul Moore 		type_str = "pass";
439*6c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_pass(info, &audit_info);
44096cb8e33SPaul Moore 		break;
441d91d4079SPaul Moore 	case CIPSO_V4_MAP_LOCAL:
442d91d4079SPaul Moore 		type_str = "local";
443*6c2e8ac0SPaul Moore 		ret_val = netlbl_cipsov4_add_local(info, &audit_info);
444d91d4079SPaul Moore 		break;
44596cb8e33SPaul Moore 	}
44623bcdc1aSPaul Moore 	if (ret_val == 0)
447c783f1ceSPaul Moore 		atomic_inc(&netlabel_mgmt_protocount);
44896cb8e33SPaul Moore 
44996cb8e33SPaul Moore 	return ret_val;
45096cb8e33SPaul Moore }
45196cb8e33SPaul Moore 
45296cb8e33SPaul Moore /**
45396cb8e33SPaul Moore  * netlbl_cipsov4_list - Handle a LIST message
45496cb8e33SPaul Moore  * @skb: the NETLINK buffer
45596cb8e33SPaul Moore  * @info: the Generic NETLINK info block
45696cb8e33SPaul Moore  *
45796cb8e33SPaul Moore  * Description:
458fd385855SPaul Moore  * Process a user generated LIST message and respond accordingly.  While the
459fd385855SPaul Moore  * response message generated by the kernel is straightforward, determining
460fd385855SPaul Moore  * before hand the size of the buffer to allocate is not (we have to generate
461fd385855SPaul Moore  * the message to know the size).  In order to keep this function sane what we
462fd385855SPaul Moore  * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
463fd385855SPaul Moore  * that size, if we fail then we restart with a larger buffer and try again.
464fd385855SPaul Moore  * We continue in this manner until we hit a limit of failed attempts then we
465fd385855SPaul Moore  * give up and just send an error message.  Returns zero on success and
466fd385855SPaul Moore  * negative values on error.
46796cb8e33SPaul Moore  *
46896cb8e33SPaul Moore  */
46996cb8e33SPaul Moore static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
47096cb8e33SPaul Moore {
471fd385855SPaul Moore 	int ret_val;
472fd385855SPaul Moore 	struct sk_buff *ans_skb = NULL;
473fd385855SPaul Moore 	u32 nlsze_mult = 1;
474fd385855SPaul Moore 	void *data;
47596cb8e33SPaul Moore 	u32 doi;
476fd385855SPaul Moore 	struct nlattr *nla_a;
477fd385855SPaul Moore 	struct nlattr *nla_b;
478fd385855SPaul Moore 	struct cipso_v4_doi *doi_def;
479fd385855SPaul Moore 	u32 iter;
48096cb8e33SPaul Moore 
481fd385855SPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
482fd385855SPaul Moore 		ret_val = -EINVAL;
48396cb8e33SPaul Moore 		goto list_failure;
484fd385855SPaul Moore 	}
48596cb8e33SPaul Moore 
486fd385855SPaul Moore list_start:
487339bf98fSThomas Graf 	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
48896cb8e33SPaul Moore 	if (ans_skb == NULL) {
48996cb8e33SPaul Moore 		ret_val = -ENOMEM;
49096cb8e33SPaul Moore 		goto list_failure;
49196cb8e33SPaul Moore 	}
49217c157c8SThomas Graf 	data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
49317c157c8SThomas Graf 				 0, NLBL_CIPSOV4_C_LIST);
494fd385855SPaul Moore 	if (data == NULL) {
495fd385855SPaul Moore 		ret_val = -ENOMEM;
496fd385855SPaul Moore 		goto list_failure;
497fd385855SPaul Moore 	}
49896cb8e33SPaul Moore 
499fd385855SPaul Moore 	doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
500fd385855SPaul Moore 
501fd385855SPaul Moore 	rcu_read_lock();
502fd385855SPaul Moore 	doi_def = cipso_v4_doi_getdef(doi);
503fd385855SPaul Moore 	if (doi_def == NULL) {
504fd385855SPaul Moore 		ret_val = -EINVAL;
50556196701SPaul Moore 		goto list_failure_lock;
506fd385855SPaul Moore 	}
507fd385855SPaul Moore 
508fd385855SPaul Moore 	ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
509fd385855SPaul Moore 	if (ret_val != 0)
510fd385855SPaul Moore 		goto list_failure_lock;
511fd385855SPaul Moore 
512fd385855SPaul Moore 	nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
513fd385855SPaul Moore 	if (nla_a == NULL) {
514fd385855SPaul Moore 		ret_val = -ENOMEM;
515fd385855SPaul Moore 		goto list_failure_lock;
516fd385855SPaul Moore 	}
517fd385855SPaul Moore 	for (iter = 0;
518fd385855SPaul Moore 	     iter < CIPSO_V4_TAG_MAXCNT &&
519fd385855SPaul Moore 	       doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
520fd385855SPaul Moore 	     iter++) {
521fd385855SPaul Moore 		ret_val = nla_put_u8(ans_skb,
522fd385855SPaul Moore 				     NLBL_CIPSOV4_A_TAG,
523fd385855SPaul Moore 				     doi_def->tags[iter]);
524fd385855SPaul Moore 		if (ret_val != 0)
525fd385855SPaul Moore 			goto list_failure_lock;
526fd385855SPaul Moore 	}
527fd385855SPaul Moore 	nla_nest_end(ans_skb, nla_a);
528fd385855SPaul Moore 
529fd385855SPaul Moore 	switch (doi_def->type) {
53015c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
531fd385855SPaul Moore 		nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
532fd385855SPaul Moore 		if (nla_a == NULL) {
533fd385855SPaul Moore 			ret_val = -ENOMEM;
534fd385855SPaul Moore 			goto list_failure_lock;
535fd385855SPaul Moore 		}
536fd385855SPaul Moore 		for (iter = 0;
537fd385855SPaul Moore 		     iter < doi_def->map.std->lvl.local_size;
538fd385855SPaul Moore 		     iter++) {
539fd385855SPaul Moore 			if (doi_def->map.std->lvl.local[iter] ==
540fd385855SPaul Moore 			    CIPSO_V4_INV_LVL)
541fd385855SPaul Moore 				continue;
542fd385855SPaul Moore 
543fd385855SPaul Moore 			nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
544fd385855SPaul Moore 			if (nla_b == NULL) {
545fd385855SPaul Moore 				ret_val = -ENOMEM;
546fd385855SPaul Moore 				goto list_retry;
547fd385855SPaul Moore 			}
548fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
549fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MLSLVLLOC,
550fd385855SPaul Moore 					      iter);
551fd385855SPaul Moore 			if (ret_val != 0)
552fd385855SPaul Moore 				goto list_retry;
553fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
554fd385855SPaul Moore 					    NLBL_CIPSOV4_A_MLSLVLREM,
555fd385855SPaul Moore 					    doi_def->map.std->lvl.local[iter]);
556fd385855SPaul Moore 			if (ret_val != 0)
557fd385855SPaul Moore 				goto list_retry;
558fd385855SPaul Moore 			nla_nest_end(ans_skb, nla_b);
559fd385855SPaul Moore 		}
560fd385855SPaul Moore 		nla_nest_end(ans_skb, nla_a);
561fd385855SPaul Moore 
562fd385855SPaul Moore 		nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
563fd385855SPaul Moore 		if (nla_a == NULL) {
564fd385855SPaul Moore 			ret_val = -ENOMEM;
565fd385855SPaul Moore 			goto list_retry;
566fd385855SPaul Moore 		}
567fd385855SPaul Moore 		for (iter = 0;
568fd385855SPaul Moore 		     iter < doi_def->map.std->cat.local_size;
569fd385855SPaul Moore 		     iter++) {
570fd385855SPaul Moore 			if (doi_def->map.std->cat.local[iter] ==
571fd385855SPaul Moore 			    CIPSO_V4_INV_CAT)
572fd385855SPaul Moore 				continue;
573fd385855SPaul Moore 
574fd385855SPaul Moore 			nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
575fd385855SPaul Moore 			if (nla_b == NULL) {
576fd385855SPaul Moore 				ret_val = -ENOMEM;
577fd385855SPaul Moore 				goto list_retry;
578fd385855SPaul Moore 			}
579fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
580fd385855SPaul Moore 					      NLBL_CIPSOV4_A_MLSCATLOC,
581fd385855SPaul Moore 					      iter);
582fd385855SPaul Moore 			if (ret_val != 0)
583fd385855SPaul Moore 				goto list_retry;
584fd385855SPaul Moore 			ret_val = nla_put_u32(ans_skb,
585fd385855SPaul Moore 					    NLBL_CIPSOV4_A_MLSCATREM,
586fd385855SPaul Moore 					    doi_def->map.std->cat.local[iter]);
587fd385855SPaul Moore 			if (ret_val != 0)
588fd385855SPaul Moore 				goto list_retry;
589fd385855SPaul Moore 			nla_nest_end(ans_skb, nla_b);
590fd385855SPaul Moore 		}
591fd385855SPaul Moore 		nla_nest_end(ans_skb, nla_a);
592fd385855SPaul Moore 
593fd385855SPaul Moore 		break;
594fd385855SPaul Moore 	}
595fd385855SPaul Moore 	rcu_read_unlock();
596fd385855SPaul Moore 
597fd385855SPaul Moore 	genlmsg_end(ans_skb, data);
598fe785beeSDenis V. Lunev 	return genlmsg_reply(ans_skb, info);
59996cb8e33SPaul Moore 
600fd385855SPaul Moore list_retry:
601fd385855SPaul Moore 	/* XXX - this limit is a guesstimate */
602fd385855SPaul Moore 	if (nlsze_mult < 4) {
603fd385855SPaul Moore 		rcu_read_unlock();
604fd385855SPaul Moore 		kfree_skb(ans_skb);
60583aa2e96SDenis V. Lunev 		nlsze_mult *= 2;
606fd385855SPaul Moore 		goto list_start;
607fd385855SPaul Moore 	}
608fd385855SPaul Moore list_failure_lock:
609fd385855SPaul Moore 	rcu_read_unlock();
61096cb8e33SPaul Moore list_failure:
611fd385855SPaul Moore 	kfree_skb(ans_skb);
612fd385855SPaul Moore 	return ret_val;
613fd385855SPaul Moore }
614fd385855SPaul Moore 
615fd385855SPaul Moore /**
616fd385855SPaul Moore  * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
617fd385855SPaul Moore  * @doi_def: the CIPSOv4 DOI definition
618fd385855SPaul Moore  * @arg: the netlbl_cipsov4_doiwalk_arg structure
619fd385855SPaul Moore  *
620fd385855SPaul Moore  * Description:
621fd385855SPaul Moore  * This function is designed to be used as a callback to the
622fd385855SPaul Moore  * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
623fd385855SPaul Moore  * message.  Returns the size of the message on success, negative values on
624fd385855SPaul Moore  * failure.
625fd385855SPaul Moore  *
626fd385855SPaul Moore  */
627fd385855SPaul Moore static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
628fd385855SPaul Moore {
629fd385855SPaul Moore 	int ret_val = -ENOMEM;
630fd385855SPaul Moore 	struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
631fd385855SPaul Moore 	void *data;
632fd385855SPaul Moore 
63317c157c8SThomas Graf 	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
63417c157c8SThomas Graf 			   cb_arg->seq, &netlbl_cipsov4_gnl_family,
63517c157c8SThomas Graf 			   NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
636fd385855SPaul Moore 	if (data == NULL)
637fd385855SPaul Moore 		goto listall_cb_failure;
638fd385855SPaul Moore 
639fd385855SPaul Moore 	ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
640fd385855SPaul Moore 	if (ret_val != 0)
641fd385855SPaul Moore 		goto listall_cb_failure;
642fd385855SPaul Moore 	ret_val = nla_put_u32(cb_arg->skb,
643fd385855SPaul Moore 			      NLBL_CIPSOV4_A_MTYPE,
644fd385855SPaul Moore 			      doi_def->type);
645fd385855SPaul Moore 	if (ret_val != 0)
646fd385855SPaul Moore 		goto listall_cb_failure;
647fd385855SPaul Moore 
648fd385855SPaul Moore 	return genlmsg_end(cb_arg->skb, data);
649fd385855SPaul Moore 
650fd385855SPaul Moore listall_cb_failure:
651fd385855SPaul Moore 	genlmsg_cancel(cb_arg->skb, data);
65296cb8e33SPaul Moore 	return ret_val;
65396cb8e33SPaul Moore }
65496cb8e33SPaul Moore 
65596cb8e33SPaul Moore /**
65696cb8e33SPaul Moore  * netlbl_cipsov4_listall - Handle a LISTALL message
65796cb8e33SPaul Moore  * @skb: the NETLINK buffer
658fd385855SPaul Moore  * @cb: the NETLINK callback
65996cb8e33SPaul Moore  *
66096cb8e33SPaul Moore  * Description:
66196cb8e33SPaul Moore  * Process a user generated LISTALL message and respond accordingly.  Returns
66296cb8e33SPaul Moore  * zero on success and negative values on error.
66396cb8e33SPaul Moore  *
66496cb8e33SPaul Moore  */
665fd385855SPaul Moore static int netlbl_cipsov4_listall(struct sk_buff *skb,
666fd385855SPaul Moore 				  struct netlink_callback *cb)
66796cb8e33SPaul Moore {
668fd385855SPaul Moore 	struct netlbl_cipsov4_doiwalk_arg cb_arg;
66956196701SPaul Moore 	u32 doi_skip = cb->args[0];
67096cb8e33SPaul Moore 
671fd385855SPaul Moore 	cb_arg.nl_cb = cb;
672fd385855SPaul Moore 	cb_arg.skb = skb;
673fd385855SPaul Moore 	cb_arg.seq = cb->nlh->nlmsg_seq;
67496cb8e33SPaul Moore 
675fd385855SPaul Moore 	cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
67696cb8e33SPaul Moore 
677fd385855SPaul Moore 	cb->args[0] = doi_skip;
678fd385855SPaul Moore 	return skb->len;
67996cb8e33SPaul Moore }
68096cb8e33SPaul Moore 
68196cb8e33SPaul Moore /**
682b1edeb10SPaul Moore  * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
683b1edeb10SPaul Moore  * @entry: LSM domain mapping entry
684b1edeb10SPaul Moore  * @arg: the netlbl_domhsh_walk_arg structure
685b1edeb10SPaul Moore  *
686b1edeb10SPaul Moore  * Description:
687b1edeb10SPaul Moore  * This function is intended for use by netlbl_cipsov4_remove() as the callback
688b1edeb10SPaul Moore  * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
689b1edeb10SPaul Moore  * which are associated with the CIPSO DOI specified in @arg.  Returns zero on
690b1edeb10SPaul Moore  * success, negative values on failure.
691b1edeb10SPaul Moore  *
692b1edeb10SPaul Moore  */
693b1edeb10SPaul Moore static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
694b1edeb10SPaul Moore {
695b1edeb10SPaul Moore 	struct netlbl_domhsh_walk_arg *cb_arg = arg;
696b1edeb10SPaul Moore 
697b1edeb10SPaul Moore 	if (entry->type == NETLBL_NLTYPE_CIPSOV4 &&
698b1edeb10SPaul Moore 	    entry->type_def.cipsov4->doi == cb_arg->doi)
699b1edeb10SPaul Moore 		return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
700b1edeb10SPaul Moore 
701b1edeb10SPaul Moore 	return 0;
702b1edeb10SPaul Moore }
703b1edeb10SPaul Moore 
704b1edeb10SPaul Moore /**
70596cb8e33SPaul Moore  * netlbl_cipsov4_remove - Handle a REMOVE message
70696cb8e33SPaul Moore  * @skb: the NETLINK buffer
70796cb8e33SPaul Moore  * @info: the Generic NETLINK info block
70896cb8e33SPaul Moore  *
70996cb8e33SPaul Moore  * Description:
71096cb8e33SPaul Moore  * Process a user generated REMOVE message and respond accordingly.  Returns
71196cb8e33SPaul Moore  * zero on success, negative values on failure.
71296cb8e33SPaul Moore  *
71396cb8e33SPaul Moore  */
71496cb8e33SPaul Moore static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
71596cb8e33SPaul Moore {
716fd385855SPaul Moore 	int ret_val = -EINVAL;
717b1edeb10SPaul Moore 	struct netlbl_domhsh_walk_arg cb_arg;
71895d4e6beSPaul Moore 	struct netlbl_audit audit_info;
719b1edeb10SPaul Moore 	u32 skip_bkt = 0;
720b1edeb10SPaul Moore 	u32 skip_chain = 0;
72196cb8e33SPaul Moore 
72295d4e6beSPaul Moore 	if (!info->attrs[NLBL_CIPSOV4_A_DOI])
72395d4e6beSPaul Moore 		return -EINVAL;
72495d4e6beSPaul Moore 
72595d4e6beSPaul Moore 	netlbl_netlink_auditinfo(skb, &audit_info);
726*6c2e8ac0SPaul Moore 	cb_arg.doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
727b1edeb10SPaul Moore 	cb_arg.audit_info = &audit_info;
728b1edeb10SPaul Moore 	ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
729b1edeb10SPaul Moore 				     netlbl_cipsov4_remove_cb, &cb_arg);
730b1edeb10SPaul Moore 	if (ret_val == 0 || ret_val == -ENOENT) {
731*6c2e8ac0SPaul Moore 		ret_val = cipso_v4_doi_remove(cb_arg.doi, &audit_info);
73223bcdc1aSPaul Moore 		if (ret_val == 0)
733c783f1ceSPaul Moore 			atomic_dec(&netlabel_mgmt_protocount);
734b1edeb10SPaul Moore 	}
73595d4e6beSPaul Moore 
73696cb8e33SPaul Moore 	return ret_val;
73796cb8e33SPaul Moore }
73896cb8e33SPaul Moore 
73996cb8e33SPaul Moore /*
74096cb8e33SPaul Moore  * NetLabel Generic NETLINK Command Definitions
74196cb8e33SPaul Moore  */
74296cb8e33SPaul Moore 
743227c43c3SPavel Emelyanov static struct genl_ops netlbl_cipsov4_ops[] = {
744227c43c3SPavel Emelyanov 	{
74596cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_ADD,
746fd385855SPaul Moore 	.flags = GENL_ADMIN_PERM,
747fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
74896cb8e33SPaul Moore 	.doit = netlbl_cipsov4_add,
74996cb8e33SPaul Moore 	.dumpit = NULL,
750227c43c3SPavel Emelyanov 	},
751227c43c3SPavel Emelyanov 	{
75296cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_REMOVE,
753fd385855SPaul Moore 	.flags = GENL_ADMIN_PERM,
754fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
75596cb8e33SPaul Moore 	.doit = netlbl_cipsov4_remove,
75696cb8e33SPaul Moore 	.dumpit = NULL,
757227c43c3SPavel Emelyanov 	},
758227c43c3SPavel Emelyanov 	{
75996cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_LIST,
76096cb8e33SPaul Moore 	.flags = 0,
761fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
76296cb8e33SPaul Moore 	.doit = netlbl_cipsov4_list,
76396cb8e33SPaul Moore 	.dumpit = NULL,
764227c43c3SPavel Emelyanov 	},
765227c43c3SPavel Emelyanov 	{
76696cb8e33SPaul Moore 	.cmd = NLBL_CIPSOV4_C_LISTALL,
76796cb8e33SPaul Moore 	.flags = 0,
768fd385855SPaul Moore 	.policy = netlbl_cipsov4_genl_policy,
769fd385855SPaul Moore 	.doit = NULL,
770fd385855SPaul Moore 	.dumpit = netlbl_cipsov4_listall,
771227c43c3SPavel Emelyanov 	},
77296cb8e33SPaul Moore };
77396cb8e33SPaul Moore 
77496cb8e33SPaul Moore /*
77596cb8e33SPaul Moore  * NetLabel Generic NETLINK Protocol Functions
77696cb8e33SPaul Moore  */
77796cb8e33SPaul Moore 
77896cb8e33SPaul Moore /**
77996cb8e33SPaul Moore  * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
78096cb8e33SPaul Moore  *
78196cb8e33SPaul Moore  * Description:
78296cb8e33SPaul Moore  * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
78396cb8e33SPaul Moore  * mechanism.  Returns zero on success, negative values on failure.
78496cb8e33SPaul Moore  *
78596cb8e33SPaul Moore  */
78605705e4eSPavel Emelyanov int __init netlbl_cipsov4_genl_init(void)
78796cb8e33SPaul Moore {
788227c43c3SPavel Emelyanov 	int ret_val, i;
78996cb8e33SPaul Moore 
79096cb8e33SPaul Moore 	ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
79196cb8e33SPaul Moore 	if (ret_val != 0)
79296cb8e33SPaul Moore 		return ret_val;
79396cb8e33SPaul Moore 
794227c43c3SPavel Emelyanov 	for (i = 0; i < ARRAY_SIZE(netlbl_cipsov4_ops); i++) {
79596cb8e33SPaul Moore 		ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
796227c43c3SPavel Emelyanov 				&netlbl_cipsov4_ops[i]);
79796cb8e33SPaul Moore 		if (ret_val != 0)
79896cb8e33SPaul Moore 			return ret_val;
799227c43c3SPavel Emelyanov 	}
80096cb8e33SPaul Moore 
80196cb8e33SPaul Moore 	return 0;
80296cb8e33SPaul Moore }
803