blob: 4149d3e6358976f093dbcb25dee79d10f6275231 [file] [log] [blame]
Paul Moore96cb8e32006-08-03 16:48:59 -07001/*
2 * NetLabel CIPSO/IPv4 Support
3 *
4 * This file defines the CIPSO/IPv4 functions for the NetLabel system. The
5 * NetLabel system manages static and dynamic label mappings for network
6 * protocols such as CIPSO and RIPSO.
7 *
Paul Moore82c21bf2011-08-01 11:10:33 +00008 * Author: Paul Moore <paul@paul-moore.com>
Paul Moore96cb8e32006-08-03 16:48:59 -07009 *
10 */
11
12/*
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
Jeff Kirsherd484ff12013-12-06 09:13:41 -080026 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Paul Moore96cb8e32006-08-03 16:48:59 -070027 *
28 */
29
30#include <linux/types.h>
31#include <linux/socket.h>
32#include <linux/string.h>
33#include <linux/skbuff.h>
Paul Moore32f50cd2006-09-28 14:51:47 -070034#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070036#include <net/sock.h>
37#include <net/netlink.h>
38#include <net/genetlink.h>
39#include <net/netlabel.h>
40#include <net/cipso_ipv4.h>
Arun Sharma600634972011-07-26 16:09:06 -070041#include <linux/atomic.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070042
43#include "netlabel_user.h"
44#include "netlabel_cipso_v4.h"
Paul Moore23bcdc12007-07-18 12:28:45 -040045#include "netlabel_mgmt.h"
Paul Mooreb1edeb12008-10-10 10:16:31 -040046#include "netlabel_domainhash.h"
Paul Moore96cb8e32006-08-03 16:48:59 -070047
Paul Moorefd385852006-09-25 15:56:37 -070048/* Argument struct for cipso_v4_doi_walk() */
49struct netlbl_cipsov4_doiwalk_arg {
50 struct netlink_callback *nl_cb;
51 struct sk_buff *skb;
52 u32 seq;
53};
54
Paul Mooreb1edeb12008-10-10 10:16:31 -040055/* Argument struct for netlbl_domhsh_walk() */
56struct netlbl_domhsh_walk_arg {
57 struct netlbl_audit *audit_info;
58 u32 doi;
59};
60
Paul Moore96cb8e32006-08-03 16:48:59 -070061/* NetLabel Generic NETLINK CIPSOv4 family */
Johannes Berg489111e2016-10-24 14:40:03 +020062static struct genl_family netlbl_cipsov4_gnl_family;
Paul Moorefd385852006-09-25 15:56:37 -070063/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -070064static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -070065 [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
66 [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
67 [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
68 [NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
69 [NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
70 [NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
71 [NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
72 [NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
73 [NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
74 [NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
75 [NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
76 [NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
77};
Paul Moore96cb8e32006-08-03 16:48:59 -070078
79/*
80 * Helper Functions
81 */
82
83/**
Paul Moorefd385852006-09-25 15:56:37 -070084 * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
85 * @info: the Generic NETLINK info block
86 * @doi_def: the CIPSO V4 DOI definition
87 *
88 * Description:
89 * Parse the common sections of a ADD message and fill in the related values
90 * in @doi_def. Returns zero on success, negative values on failure.
91 *
92 */
93static int netlbl_cipsov4_add_common(struct genl_info *info,
94 struct cipso_v4_doi *doi_def)
95{
96 struct nlattr *nla;
97 int nla_rem;
98 u32 iter = 0;
99
100 doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
101
102 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
103 NLBL_CIPSOV4_A_MAX,
104 netlbl_cipsov4_genl_policy) != 0)
105 return -EINVAL;
106
107 nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200108 if (nla_type(nla) == NLBL_CIPSOV4_A_TAG) {
Paul Moore2a2f11c2007-01-05 15:08:22 -0500109 if (iter >= CIPSO_V4_TAG_MAXCNT)
Paul Moorefd385852006-09-25 15:56:37 -0700110 return -EINVAL;
111 doi_def->tags[iter++] = nla_get_u8(nla);
112 }
Paul Moore2a2f11c2007-01-05 15:08:22 -0500113 while (iter < CIPSO_V4_TAG_MAXCNT)
114 doi_def->tags[iter++] = CIPSO_V4_TAG_INVALID;
Paul Moorefd385852006-09-25 15:56:37 -0700115
116 return 0;
117}
Paul Moore96cb8e32006-08-03 16:48:59 -0700118
119/*
120 * NetLabel Command Handlers
121 */
122
123/**
124 * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
Paul Moorefd385852006-09-25 15:56:37 -0700125 * @info: the Generic NETLINK info block
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500126 * @audit_info: NetLabel audit information
Paul Moore96cb8e32006-08-03 16:48:59 -0700127 *
128 * Description:
Paul Moore15c45f72008-10-10 10:16:34 -0400129 * Create a new CIPSO_V4_MAP_TRANS DOI definition based on the given ADD
130 * message and add it to the CIPSO V4 engine. Return zero on success and
131 * non-zero on error.
Paul Moore96cb8e32006-08-03 16:48:59 -0700132 *
133 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500134static int netlbl_cipsov4_add_std(struct genl_info *info,
135 struct netlbl_audit *audit_info)
Paul Moore96cb8e32006-08-03 16:48:59 -0700136{
137 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700138 struct cipso_v4_doi *doi_def = NULL;
Paul Moorefd385852006-09-25 15:56:37 -0700139 struct nlattr *nla_a;
140 struct nlattr *nla_b;
141 int nla_a_rem;
142 int nla_b_rem;
Paul Moorecaff5b6a2006-12-15 16:49:28 -0500143 u32 iter;
Paul Moore96cb8e32006-08-03 16:48:59 -0700144
Paul Moore32f50cd2006-09-28 14:51:47 -0700145 if (!info->attrs[NLBL_CIPSOV4_A_TAGLST] ||
Paul Moorefd385852006-09-25 15:56:37 -0700146 !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST])
147 return -EINVAL;
148
149 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
150 NLBL_CIPSOV4_A_MAX,
151 netlbl_cipsov4_genl_policy) != 0)
152 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700153
154 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
Paul Moorefd385852006-09-25 15:56:37 -0700155 if (doi_def == NULL)
156 return -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700157 doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
158 if (doi_def->map.std == NULL) {
159 ret_val = -ENOMEM;
160 goto add_std_failure;
161 }
Paul Moore15c45f72008-10-10 10:16:34 -0400162 doi_def->type = CIPSO_V4_MAP_TRANS;
Paul Moore96cb8e32006-08-03 16:48:59 -0700163
Paul Moorefd385852006-09-25 15:56:37 -0700164 ret_val = netlbl_cipsov4_add_common(info, doi_def);
165 if (ret_val != 0)
166 goto add_std_failure;
Paul Moore1fd2a252006-12-15 16:49:27 -0500167 ret_val = -EINVAL;
Paul Moorefd385852006-09-25 15:56:37 -0700168
169 nla_for_each_nested(nla_a,
170 info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
171 nla_a_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200172 if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
Paul Moore1fd2a252006-12-15 16:49:27 -0500173 if (nla_validate_nested(nla_a,
174 NLBL_CIPSOV4_A_MAX,
175 netlbl_cipsov4_genl_policy) != 0)
176 goto add_std_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700177 nla_for_each_nested(nla_b, nla_a, nla_b_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200178 switch (nla_type(nla_b)) {
Paul Moorefd385852006-09-25 15:56:37 -0700179 case NLBL_CIPSOV4_A_MLSLVLLOC:
Paul Moore1fd2a252006-12-15 16:49:27 -0500180 if (nla_get_u32(nla_b) >
181 CIPSO_V4_MAX_LOC_LVLS)
182 goto add_std_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700183 if (nla_get_u32(nla_b) >=
184 doi_def->map.std->lvl.local_size)
185 doi_def->map.std->lvl.local_size =
186 nla_get_u32(nla_b) + 1;
187 break;
188 case NLBL_CIPSOV4_A_MLSLVLREM:
Paul Moore1fd2a252006-12-15 16:49:27 -0500189 if (nla_get_u32(nla_b) >
190 CIPSO_V4_MAX_REM_LVLS)
191 goto add_std_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700192 if (nla_get_u32(nla_b) >=
193 doi_def->map.std->lvl.cipso_size)
194 doi_def->map.std->lvl.cipso_size =
195 nla_get_u32(nla_b) + 1;
196 break;
197 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700198 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700199 doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
200 sizeof(u32),
201 GFP_KERNEL);
202 if (doi_def->map.std->lvl.local == NULL) {
203 ret_val = -ENOMEM;
204 goto add_std_failure;
205 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700206 doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
207 sizeof(u32),
208 GFP_KERNEL);
209 if (doi_def->map.std->lvl.cipso == NULL) {
210 ret_val = -ENOMEM;
211 goto add_std_failure;
212 }
Paul Moorecaff5b6a2006-12-15 16:49:28 -0500213 for (iter = 0; iter < doi_def->map.std->lvl.local_size; iter++)
214 doi_def->map.std->lvl.local[iter] = CIPSO_V4_INV_LVL;
215 for (iter = 0; iter < doi_def->map.std->lvl.cipso_size; iter++)
216 doi_def->map.std->lvl.cipso[iter] = CIPSO_V4_INV_LVL;
Paul Moorefd385852006-09-25 15:56:37 -0700217 nla_for_each_nested(nla_a,
218 info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
219 nla_a_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200220 if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
Paul Moorefd385852006-09-25 15:56:37 -0700221 struct nlattr *lvl_loc;
222 struct nlattr *lvl_rem;
Paul Moore96cb8e32006-08-03 16:48:59 -0700223
Paul Moorefd385852006-09-25 15:56:37 -0700224 lvl_loc = nla_find_nested(nla_a,
225 NLBL_CIPSOV4_A_MLSLVLLOC);
226 lvl_rem = nla_find_nested(nla_a,
227 NLBL_CIPSOV4_A_MLSLVLREM);
228 if (lvl_loc == NULL || lvl_rem == NULL)
229 goto add_std_failure;
230 doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] =
231 nla_get_u32(lvl_rem);
232 doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] =
233 nla_get_u32(lvl_loc);
234 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700235
Paul Moorefd385852006-09-25 15:56:37 -0700236 if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) {
237 if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
238 NLBL_CIPSOV4_A_MAX,
239 netlbl_cipsov4_genl_policy) != 0)
Paul Moore96cb8e32006-08-03 16:48:59 -0700240 goto add_std_failure;
241
Paul Moorefd385852006-09-25 15:56:37 -0700242 nla_for_each_nested(nla_a,
243 info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
244 nla_a_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200245 if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
Paul Moorefd385852006-09-25 15:56:37 -0700246 if (nla_validate_nested(nla_a,
247 NLBL_CIPSOV4_A_MAX,
248 netlbl_cipsov4_genl_policy) != 0)
249 goto add_std_failure;
250 nla_for_each_nested(nla_b, nla_a, nla_b_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200251 switch (nla_type(nla_b)) {
Paul Moorefd385852006-09-25 15:56:37 -0700252 case NLBL_CIPSOV4_A_MLSCATLOC:
Paul Moore1fd2a252006-12-15 16:49:27 -0500253 if (nla_get_u32(nla_b) >
254 CIPSO_V4_MAX_LOC_CATS)
255 goto add_std_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700256 if (nla_get_u32(nla_b) >=
257 doi_def->map.std->cat.local_size)
258 doi_def->map.std->cat.local_size =
259 nla_get_u32(nla_b) + 1;
260 break;
261 case NLBL_CIPSOV4_A_MLSCATREM:
Paul Moore1fd2a252006-12-15 16:49:27 -0500262 if (nla_get_u32(nla_b) >
263 CIPSO_V4_MAX_REM_CATS)
264 goto add_std_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700265 if (nla_get_u32(nla_b) >=
266 doi_def->map.std->cat.cipso_size)
267 doi_def->map.std->cat.cipso_size =
268 nla_get_u32(nla_b) + 1;
269 break;
270 }
271 }
Paul Moorefd385852006-09-25 15:56:37 -0700272 doi_def->map.std->cat.local = kcalloc(
YOSHIFUJI Hideakie1a952652007-02-09 23:25:05 +0900273 doi_def->map.std->cat.local_size,
Paul Moorefd385852006-09-25 15:56:37 -0700274 sizeof(u32),
275 GFP_KERNEL);
276 if (doi_def->map.std->cat.local == NULL) {
277 ret_val = -ENOMEM;
278 goto add_std_failure;
279 }
280 doi_def->map.std->cat.cipso = kcalloc(
YOSHIFUJI Hideakie1a952652007-02-09 23:25:05 +0900281 doi_def->map.std->cat.cipso_size,
Paul Moorefd385852006-09-25 15:56:37 -0700282 sizeof(u32),
283 GFP_KERNEL);
284 if (doi_def->map.std->cat.cipso == NULL) {
285 ret_val = -ENOMEM;
286 goto add_std_failure;
287 }
Paul Moorecaff5b6a2006-12-15 16:49:28 -0500288 for (iter = 0; iter < doi_def->map.std->cat.local_size; iter++)
289 doi_def->map.std->cat.local[iter] = CIPSO_V4_INV_CAT;
290 for (iter = 0; iter < doi_def->map.std->cat.cipso_size; iter++)
291 doi_def->map.std->cat.cipso[iter] = CIPSO_V4_INV_CAT;
Paul Moorefd385852006-09-25 15:56:37 -0700292 nla_for_each_nested(nla_a,
293 info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
294 nla_a_rem)
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200295 if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
Paul Moorefd385852006-09-25 15:56:37 -0700296 struct nlattr *cat_loc;
297 struct nlattr *cat_rem;
Paul Moore96cb8e32006-08-03 16:48:59 -0700298
Paul Moorefd385852006-09-25 15:56:37 -0700299 cat_loc = nla_find_nested(nla_a,
300 NLBL_CIPSOV4_A_MLSCATLOC);
301 cat_rem = nla_find_nested(nla_a,
302 NLBL_CIPSOV4_A_MLSCATREM);
303 if (cat_loc == NULL || cat_rem == NULL)
304 goto add_std_failure;
305 doi_def->map.std->cat.local[
YOSHIFUJI Hideakie1a952652007-02-09 23:25:05 +0900306 nla_get_u32(cat_loc)] =
Paul Moorefd385852006-09-25 15:56:37 -0700307 nla_get_u32(cat_rem);
308 doi_def->map.std->cat.cipso[
YOSHIFUJI Hideakie1a952652007-02-09 23:25:05 +0900309 nla_get_u32(cat_rem)] =
Paul Moorefd385852006-09-25 15:56:37 -0700310 nla_get_u32(cat_loc);
311 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700312 }
313
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500314 ret_val = cipso_v4_doi_add(doi_def, audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700315 if (ret_val != 0)
316 goto add_std_failure;
317 return 0;
318
319add_std_failure:
Markus Elfring7a11b1d2015-02-02 10:40:30 +0100320 cipso_v4_doi_free(doi_def);
Paul Moore96cb8e32006-08-03 16:48:59 -0700321 return ret_val;
322}
323
324/**
325 * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
Paul Moorefd385852006-09-25 15:56:37 -0700326 * @info: the Generic NETLINK info block
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500327 * @audit_info: NetLabel audit information
Paul Moore96cb8e32006-08-03 16:48:59 -0700328 *
329 * Description:
330 * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
331 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
332 * error.
333 *
334 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500335static int netlbl_cipsov4_add_pass(struct genl_info *info,
336 struct netlbl_audit *audit_info)
Paul Moore96cb8e32006-08-03 16:48:59 -0700337{
Paul Moorefd385852006-09-25 15:56:37 -0700338 int ret_val;
Paul Moore96cb8e32006-08-03 16:48:59 -0700339 struct cipso_v4_doi *doi_def = NULL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700340
Paul Moore32f50cd2006-09-28 14:51:47 -0700341 if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
Paul Moorefd385852006-09-25 15:56:37 -0700342 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700343
344 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
Paul Moorefd385852006-09-25 15:56:37 -0700345 if (doi_def == NULL)
346 return -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700347 doi_def->type = CIPSO_V4_MAP_PASS;
348
Paul Moorefd385852006-09-25 15:56:37 -0700349 ret_val = netlbl_cipsov4_add_common(info, doi_def);
350 if (ret_val != 0)
351 goto add_pass_failure;
Paul Moore96cb8e32006-08-03 16:48:59 -0700352
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500353 ret_val = cipso_v4_doi_add(doi_def, audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700354 if (ret_val != 0)
355 goto add_pass_failure;
356 return 0;
357
358add_pass_failure:
Paul Mooreb1edeb12008-10-10 10:16:31 -0400359 cipso_v4_doi_free(doi_def);
Paul Moore96cb8e32006-08-03 16:48:59 -0700360 return ret_val;
361}
362
363/**
Paul Moored91d4072008-10-10 10:16:34 -0400364 * netlbl_cipsov4_add_local - Adds a CIPSO V4 DOI definition
365 * @info: the Generic NETLINK info block
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500366 * @audit_info: NetLabel audit information
Paul Moored91d4072008-10-10 10:16:34 -0400367 *
368 * Description:
369 * Create a new CIPSO_V4_MAP_LOCAL DOI definition based on the given ADD
370 * message and add it to the CIPSO V4 engine. Return zero on success and
371 * non-zero on error.
372 *
373 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500374static int netlbl_cipsov4_add_local(struct genl_info *info,
375 struct netlbl_audit *audit_info)
Paul Moored91d4072008-10-10 10:16:34 -0400376{
377 int ret_val;
378 struct cipso_v4_doi *doi_def = NULL;
379
380 if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
381 return -EINVAL;
382
383 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
384 if (doi_def == NULL)
385 return -ENOMEM;
386 doi_def->type = CIPSO_V4_MAP_LOCAL;
387
388 ret_val = netlbl_cipsov4_add_common(info, doi_def);
389 if (ret_val != 0)
390 goto add_local_failure;
391
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500392 ret_val = cipso_v4_doi_add(doi_def, audit_info);
Paul Moored91d4072008-10-10 10:16:34 -0400393 if (ret_val != 0)
394 goto add_local_failure;
395 return 0;
396
397add_local_failure:
398 cipso_v4_doi_free(doi_def);
399 return ret_val;
400}
401
402/**
Paul Moore96cb8e32006-08-03 16:48:59 -0700403 * netlbl_cipsov4_add - Handle an ADD message
404 * @skb: the NETLINK buffer
405 * @info: the Generic NETLINK info block
406 *
407 * Description:
408 * Create a new DOI definition based on the given ADD message and add it to the
409 * CIPSO V4 engine. Returns zero on success, negative values on failure.
410 *
411 */
412static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
413
414{
415 int ret_val = -EINVAL;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700416 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700417
Paul Moore32f50cd2006-09-28 14:51:47 -0700418 if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
419 !info->attrs[NLBL_CIPSOV4_A_MTYPE])
Paul Moorefd385852006-09-25 15:56:37 -0700420 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700421
Paul Moore95d4e6b2006-09-29 17:05:05 -0700422 netlbl_netlink_auditinfo(skb, &audit_info);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500423 switch (nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE])) {
Paul Moore15c45f72008-10-10 10:16:34 -0400424 case CIPSO_V4_MAP_TRANS:
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500425 ret_val = netlbl_cipsov4_add_std(info, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700426 break;
427 case CIPSO_V4_MAP_PASS:
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500428 ret_val = netlbl_cipsov4_add_pass(info, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700429 break;
Paul Moored91d4072008-10-10 10:16:34 -0400430 case CIPSO_V4_MAP_LOCAL:
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500431 ret_val = netlbl_cipsov4_add_local(info, &audit_info);
Paul Moored91d4072008-10-10 10:16:34 -0400432 break;
Paul Moore96cb8e32006-08-03 16:48:59 -0700433 }
Paul Moore23bcdc12007-07-18 12:28:45 -0400434 if (ret_val == 0)
Paul Moorec783f1c2008-01-29 08:37:52 -0500435 atomic_inc(&netlabel_mgmt_protocount);
Paul Moore96cb8e32006-08-03 16:48:59 -0700436
Paul Moore96cb8e32006-08-03 16:48:59 -0700437 return ret_val;
438}
439
440/**
441 * netlbl_cipsov4_list - Handle a LIST message
442 * @skb: the NETLINK buffer
443 * @info: the Generic NETLINK info block
444 *
445 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700446 * Process a user generated LIST message and respond accordingly. While the
447 * response message generated by the kernel is straightforward, determining
448 * before hand the size of the buffer to allocate is not (we have to generate
449 * the message to know the size). In order to keep this function sane what we
450 * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
451 * that size, if we fail then we restart with a larger buffer and try again.
452 * We continue in this manner until we hit a limit of failed attempts then we
453 * give up and just send an error message. Returns zero on success and
454 * negative values on error.
Paul Moore96cb8e32006-08-03 16:48:59 -0700455 *
456 */
457static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
458{
Paul Moorefd385852006-09-25 15:56:37 -0700459 int ret_val;
460 struct sk_buff *ans_skb = NULL;
461 u32 nlsze_mult = 1;
462 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -0700463 u32 doi;
Paul Moorefd385852006-09-25 15:56:37 -0700464 struct nlattr *nla_a;
465 struct nlattr *nla_b;
466 struct cipso_v4_doi *doi_def;
467 u32 iter;
Paul Moore96cb8e32006-08-03 16:48:59 -0700468
Paul Moorefd385852006-09-25 15:56:37 -0700469 if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
470 ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700471 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700472 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700473
Paul Moorefd385852006-09-25 15:56:37 -0700474list_start:
Thomas Graf339bf982006-11-10 14:10:15 -0800475 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -0700476 if (ans_skb == NULL) {
477 ret_val = -ENOMEM;
478 goto list_failure;
479 }
Thomas Graf17c157c2006-11-14 19:46:02 -0800480 data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
481 0, NLBL_CIPSOV4_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -0700482 if (data == NULL) {
483 ret_val = -ENOMEM;
484 goto list_failure;
485 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700486
Paul Moorefd385852006-09-25 15:56:37 -0700487 doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
488
489 rcu_read_lock();
490 doi_def = cipso_v4_doi_getdef(doi);
491 if (doi_def == NULL) {
492 ret_val = -EINVAL;
Paul Moore56196702008-10-10 10:16:29 -0400493 goto list_failure_lock;
Paul Moorefd385852006-09-25 15:56:37 -0700494 }
495
496 ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
497 if (ret_val != 0)
498 goto list_failure_lock;
499
500 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
501 if (nla_a == NULL) {
502 ret_val = -ENOMEM;
503 goto list_failure_lock;
504 }
505 for (iter = 0;
506 iter < CIPSO_V4_TAG_MAXCNT &&
507 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
508 iter++) {
509 ret_val = nla_put_u8(ans_skb,
510 NLBL_CIPSOV4_A_TAG,
511 doi_def->tags[iter]);
512 if (ret_val != 0)
513 goto list_failure_lock;
514 }
515 nla_nest_end(ans_skb, nla_a);
516
517 switch (doi_def->type) {
Paul Moore15c45f72008-10-10 10:16:34 -0400518 case CIPSO_V4_MAP_TRANS:
Paul Moorefd385852006-09-25 15:56:37 -0700519 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
520 if (nla_a == NULL) {
521 ret_val = -ENOMEM;
522 goto list_failure_lock;
523 }
524 for (iter = 0;
525 iter < doi_def->map.std->lvl.local_size;
526 iter++) {
527 if (doi_def->map.std->lvl.local[iter] ==
528 CIPSO_V4_INV_LVL)
529 continue;
530
531 nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
532 if (nla_b == NULL) {
533 ret_val = -ENOMEM;
534 goto list_retry;
535 }
536 ret_val = nla_put_u32(ans_skb,
537 NLBL_CIPSOV4_A_MLSLVLLOC,
538 iter);
539 if (ret_val != 0)
540 goto list_retry;
541 ret_val = nla_put_u32(ans_skb,
542 NLBL_CIPSOV4_A_MLSLVLREM,
543 doi_def->map.std->lvl.local[iter]);
544 if (ret_val != 0)
545 goto list_retry;
546 nla_nest_end(ans_skb, nla_b);
547 }
548 nla_nest_end(ans_skb, nla_a);
549
550 nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
551 if (nla_a == NULL) {
552 ret_val = -ENOMEM;
553 goto list_retry;
554 }
555 for (iter = 0;
556 iter < doi_def->map.std->cat.local_size;
557 iter++) {
558 if (doi_def->map.std->cat.local[iter] ==
559 CIPSO_V4_INV_CAT)
560 continue;
561
562 nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
563 if (nla_b == NULL) {
564 ret_val = -ENOMEM;
565 goto list_retry;
566 }
567 ret_val = nla_put_u32(ans_skb,
568 NLBL_CIPSOV4_A_MLSCATLOC,
569 iter);
570 if (ret_val != 0)
571 goto list_retry;
572 ret_val = nla_put_u32(ans_skb,
573 NLBL_CIPSOV4_A_MLSCATREM,
574 doi_def->map.std->cat.local[iter]);
575 if (ret_val != 0)
576 goto list_retry;
577 nla_nest_end(ans_skb, nla_b);
578 }
579 nla_nest_end(ans_skb, nla_a);
580
581 break;
582 }
583 rcu_read_unlock();
584
585 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700586 return genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700587
Paul Moorefd385852006-09-25 15:56:37 -0700588list_retry:
589 /* XXX - this limit is a guesstimate */
590 if (nlsze_mult < 4) {
591 rcu_read_unlock();
592 kfree_skb(ans_skb);
Denis V. Lunev83aa2e92008-07-14 22:28:25 -0700593 nlsze_mult *= 2;
Paul Moorefd385852006-09-25 15:56:37 -0700594 goto list_start;
595 }
596list_failure_lock:
597 rcu_read_unlock();
Paul Moore96cb8e32006-08-03 16:48:59 -0700598list_failure:
Paul Moorefd385852006-09-25 15:56:37 -0700599 kfree_skb(ans_skb);
600 return ret_val;
601}
602
603/**
604 * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
605 * @doi_def: the CIPSOv4 DOI definition
606 * @arg: the netlbl_cipsov4_doiwalk_arg structure
607 *
608 * Description:
609 * This function is designed to be used as a callback to the
610 * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
611 * message. Returns the size of the message on success, negative values on
612 * failure.
613 *
614 */
615static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
616{
617 int ret_val = -ENOMEM;
618 struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
619 void *data;
620
Eric W. Biederman15e47302012-09-07 20:12:54 +0000621 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
Thomas Graf17c157c2006-11-14 19:46:02 -0800622 cb_arg->seq, &netlbl_cipsov4_gnl_family,
623 NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
Paul Moorefd385852006-09-25 15:56:37 -0700624 if (data == NULL)
625 goto listall_cb_failure;
626
627 ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
628 if (ret_val != 0)
629 goto listall_cb_failure;
630 ret_val = nla_put_u32(cb_arg->skb,
631 NLBL_CIPSOV4_A_MTYPE,
632 doi_def->type);
633 if (ret_val != 0)
634 goto listall_cb_failure;
635
Johannes Berg053c0952015-01-16 22:09:00 +0100636 genlmsg_end(cb_arg->skb, data);
637 return 0;
Paul Moorefd385852006-09-25 15:56:37 -0700638
639listall_cb_failure:
640 genlmsg_cancel(cb_arg->skb, data);
Paul Moore96cb8e32006-08-03 16:48:59 -0700641 return ret_val;
642}
643
644/**
645 * netlbl_cipsov4_listall - Handle a LISTALL message
646 * @skb: the NETLINK buffer
Paul Moorefd385852006-09-25 15:56:37 -0700647 * @cb: the NETLINK callback
Paul Moore96cb8e32006-08-03 16:48:59 -0700648 *
649 * Description:
650 * Process a user generated LISTALL message and respond accordingly. Returns
651 * zero on success and negative values on error.
652 *
653 */
Paul Moorefd385852006-09-25 15:56:37 -0700654static int netlbl_cipsov4_listall(struct sk_buff *skb,
655 struct netlink_callback *cb)
Paul Moore96cb8e32006-08-03 16:48:59 -0700656{
Paul Moorefd385852006-09-25 15:56:37 -0700657 struct netlbl_cipsov4_doiwalk_arg cb_arg;
Paul Moore56196702008-10-10 10:16:29 -0400658 u32 doi_skip = cb->args[0];
Paul Moore96cb8e32006-08-03 16:48:59 -0700659
Paul Moorefd385852006-09-25 15:56:37 -0700660 cb_arg.nl_cb = cb;
661 cb_arg.skb = skb;
662 cb_arg.seq = cb->nlh->nlmsg_seq;
Paul Moore96cb8e32006-08-03 16:48:59 -0700663
Paul Moorefd385852006-09-25 15:56:37 -0700664 cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
Paul Moore96cb8e32006-08-03 16:48:59 -0700665
Paul Moorefd385852006-09-25 15:56:37 -0700666 cb->args[0] = doi_skip;
667 return skb->len;
Paul Moore96cb8e32006-08-03 16:48:59 -0700668}
669
670/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400671 * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
672 * @entry: LSM domain mapping entry
673 * @arg: the netlbl_domhsh_walk_arg structure
674 *
675 * Description:
676 * This function is intended for use by netlbl_cipsov4_remove() as the callback
677 * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
678 * which are associated with the CIPSO DOI specified in @arg. Returns zero on
679 * success, negative values on failure.
680 *
681 */
682static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
683{
684 struct netlbl_domhsh_walk_arg *cb_arg = arg;
685
Paul Moore6a8b7f02013-08-02 14:45:08 -0400686 if (entry->def.type == NETLBL_NLTYPE_CIPSOV4 &&
687 entry->def.cipso->doi == cb_arg->doi)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400688 return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
689
690 return 0;
691}
692
693/**
Paul Moore96cb8e32006-08-03 16:48:59 -0700694 * netlbl_cipsov4_remove - Handle a REMOVE message
695 * @skb: the NETLINK buffer
696 * @info: the Generic NETLINK info block
697 *
698 * Description:
699 * Process a user generated REMOVE message and respond accordingly. Returns
700 * zero on success, negative values on failure.
701 *
702 */
703static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
704{
Paul Moorefd385852006-09-25 15:56:37 -0700705 int ret_val = -EINVAL;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400706 struct netlbl_domhsh_walk_arg cb_arg;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700707 struct netlbl_audit audit_info;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400708 u32 skip_bkt = 0;
709 u32 skip_chain = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700710
Paul Moore95d4e6b2006-09-29 17:05:05 -0700711 if (!info->attrs[NLBL_CIPSOV4_A_DOI])
712 return -EINVAL;
Paul Moore32f50cd2006-09-28 14:51:47 -0700713
Paul Moore95d4e6b2006-09-29 17:05:05 -0700714 netlbl_netlink_auditinfo(skb, &audit_info);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500715 cb_arg.doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400716 cb_arg.audit_info = &audit_info;
717 ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
718 netlbl_cipsov4_remove_cb, &cb_arg);
719 if (ret_val == 0 || ret_val == -ENOENT) {
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500720 ret_val = cipso_v4_doi_remove(cb_arg.doi, &audit_info);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400721 if (ret_val == 0)
722 atomic_dec(&netlabel_mgmt_protocount);
723 }
Paul Moore95d4e6b2006-09-29 17:05:05 -0700724
Paul Moore96cb8e32006-08-03 16:48:59 -0700725 return ret_val;
726}
727
728/*
729 * NetLabel Generic NETLINK Command Definitions
730 */
731
Johannes Berg4534de82013-11-14 17:14:46 +0100732static const struct genl_ops netlbl_cipsov4_ops[] = {
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800733 {
Paul Moore96cb8e32006-08-03 16:48:59 -0700734 .cmd = NLBL_CIPSOV4_C_ADD,
Paul Moorefd385852006-09-25 15:56:37 -0700735 .flags = GENL_ADMIN_PERM,
736 .policy = netlbl_cipsov4_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -0700737 .doit = netlbl_cipsov4_add,
738 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800739 },
740 {
Paul Moore96cb8e32006-08-03 16:48:59 -0700741 .cmd = NLBL_CIPSOV4_C_REMOVE,
Paul Moorefd385852006-09-25 15:56:37 -0700742 .flags = GENL_ADMIN_PERM,
743 .policy = netlbl_cipsov4_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -0700744 .doit = netlbl_cipsov4_remove,
745 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800746 },
747 {
Paul Moore96cb8e32006-08-03 16:48:59 -0700748 .cmd = NLBL_CIPSOV4_C_LIST,
749 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700750 .policy = netlbl_cipsov4_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -0700751 .doit = netlbl_cipsov4_list,
752 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800753 },
754 {
Paul Moore96cb8e32006-08-03 16:48:59 -0700755 .cmd = NLBL_CIPSOV4_C_LISTALL,
756 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700757 .policy = netlbl_cipsov4_genl_policy,
758 .doit = NULL,
759 .dumpit = netlbl_cipsov4_listall,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800760 },
Paul Moore96cb8e32006-08-03 16:48:59 -0700761};
762
Johannes Berg56989f62016-10-24 14:40:05 +0200763static struct genl_family netlbl_cipsov4_gnl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200764 .hdrsize = 0,
765 .name = NETLBL_NLTYPE_CIPSOV4_NAME,
766 .version = NETLBL_PROTO_VERSION,
767 .maxattr = NLBL_CIPSOV4_A_MAX,
768 .module = THIS_MODULE,
769 .ops = netlbl_cipsov4_ops,
770 .n_ops = ARRAY_SIZE(netlbl_cipsov4_ops),
771};
772
Paul Moore96cb8e32006-08-03 16:48:59 -0700773/*
774 * NetLabel Generic NETLINK Protocol Functions
775 */
776
777/**
778 * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
779 *
780 * Description:
781 * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
782 * mechanism. Returns zero on success, negative values on failure.
783 *
784 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -0800785int __init netlbl_cipsov4_genl_init(void)
Paul Moore96cb8e32006-08-03 16:48:59 -0700786{
Johannes Berg489111e2016-10-24 14:40:03 +0200787 return genl_register_family(&netlbl_cipsov4_gnl_family);
Paul Moore96cb8e32006-08-03 16:48:59 -0700788}