Commit | Line | Data |
---|---|---|
96cb8e33 PM |
1 | /* |
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 | * | |
8 | * Author: Paul Moore <paul.moore@hp.com> | |
9 | * | |
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 | |
26 | * along with this program; if not, write to the Free Software | |
27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
28 | * | |
29 | */ | |
30 | ||
31 | #include <linux/types.h> | |
32 | #include <linux/socket.h> | |
33 | #include <linux/string.h> | |
34 | #include <linux/skbuff.h> | |
32f50cde | 35 | #include <linux/audit.h> |
96cb8e33 PM |
36 | #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> | |
41 | ||
42 | #include "netlabel_user.h" | |
43 | #include "netlabel_cipso_v4.h" | |
44 | ||
fd385855 PM |
45 | /* Argument struct for cipso_v4_doi_walk() */ |
46 | struct netlbl_cipsov4_doiwalk_arg { | |
47 | struct netlink_callback *nl_cb; | |
48 | struct sk_buff *skb; | |
49 | u32 seq; | |
50 | }; | |
51 | ||
96cb8e33 PM |
52 | /* NetLabel Generic NETLINK CIPSOv4 family */ |
53 | static struct genl_family netlbl_cipsov4_gnl_family = { | |
54 | .id = GENL_ID_GENERATE, | |
55 | .hdrsize = 0, | |
56 | .name = NETLBL_NLTYPE_CIPSOV4_NAME, | |
57 | .version = NETLBL_PROTO_VERSION, | |
fd385855 | 58 | .maxattr = NLBL_CIPSOV4_A_MAX, |
96cb8e33 PM |
59 | }; |
60 | ||
fd385855 PM |
61 | /* NetLabel Netlink attribute policy */ |
62 | static struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = { | |
63 | [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 }, | |
64 | [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 }, | |
65 | [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 }, | |
66 | [NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED }, | |
67 | [NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 }, | |
68 | [NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 }, | |
69 | [NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED }, | |
70 | [NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED }, | |
71 | [NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 }, | |
72 | [NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 }, | |
73 | [NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED }, | |
74 | [NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED }, | |
75 | }; | |
96cb8e33 PM |
76 | |
77 | /* | |
78 | * Helper Functions | |
79 | */ | |
80 | ||
81 | /** | |
82 | * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition | |
83 | * @entry: the entry's RCU field | |
84 | * | |
85 | * Description: | |
86 | * This function is designed to be used as a callback to the call_rcu() | |
87 | * function so that the memory allocated to the DOI definition can be released | |
88 | * safely. | |
89 | * | |
90 | */ | |
91 | static void netlbl_cipsov4_doi_free(struct rcu_head *entry) | |
92 | { | |
93 | struct cipso_v4_doi *ptr; | |
94 | ||
95 | ptr = container_of(entry, struct cipso_v4_doi, rcu); | |
96 | switch (ptr->type) { | |
97 | case CIPSO_V4_MAP_STD: | |
98 | kfree(ptr->map.std->lvl.cipso); | |
99 | kfree(ptr->map.std->lvl.local); | |
100 | kfree(ptr->map.std->cat.cipso); | |
101 | kfree(ptr->map.std->cat.local); | |
102 | break; | |
103 | } | |
104 | kfree(ptr); | |
105 | } | |
106 | ||
fd385855 PM |
107 | /** |
108 | * netlbl_cipsov4_add_common - Parse the common sections of a ADD message | |
109 | * @info: the Generic NETLINK info block | |
110 | * @doi_def: the CIPSO V4 DOI definition | |
111 | * | |
112 | * Description: | |
113 | * Parse the common sections of a ADD message and fill in the related values | |
114 | * in @doi_def. Returns zero on success, negative values on failure. | |
115 | * | |
116 | */ | |
117 | static int netlbl_cipsov4_add_common(struct genl_info *info, | |
118 | struct cipso_v4_doi *doi_def) | |
119 | { | |
120 | struct nlattr *nla; | |
121 | int nla_rem; | |
122 | u32 iter = 0; | |
123 | ||
124 | doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]); | |
125 | ||
126 | if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST], | |
127 | NLBL_CIPSOV4_A_MAX, | |
128 | netlbl_cipsov4_genl_policy) != 0) | |
129 | return -EINVAL; | |
130 | ||
131 | nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem) | |
132 | if (nla->nla_type == NLBL_CIPSOV4_A_TAG) { | |
133 | if (iter > CIPSO_V4_TAG_MAXCNT) | |
134 | return -EINVAL; | |
135 | doi_def->tags[iter++] = nla_get_u8(nla); | |
136 | } | |
137 | if (iter < CIPSO_V4_TAG_MAXCNT) | |
138 | doi_def->tags[iter] = CIPSO_V4_TAG_INVALID; | |
139 | ||
140 | return 0; | |
141 | } | |
96cb8e33 PM |
142 | |
143 | /* | |
144 | * NetLabel Command Handlers | |
145 | */ | |
146 | ||
147 | /** | |
148 | * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition | |
fd385855 | 149 | * @info: the Generic NETLINK info block |
96cb8e33 PM |
150 | * |
151 | * Description: | |
152 | * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message | |
153 | * and add it to the CIPSO V4 engine. Return zero on success and non-zero on | |
154 | * error. | |
155 | * | |
156 | */ | |
fd385855 | 157 | static int netlbl_cipsov4_add_std(struct genl_info *info) |
96cb8e33 PM |
158 | { |
159 | int ret_val = -EINVAL; | |
96cb8e33 | 160 | struct cipso_v4_doi *doi_def = NULL; |
fd385855 PM |
161 | struct nlattr *nla_a; |
162 | struct nlattr *nla_b; | |
163 | int nla_a_rem; | |
164 | int nla_b_rem; | |
96cb8e33 | 165 | |
32f50cde | 166 | if (!info->attrs[NLBL_CIPSOV4_A_TAGLST] || |
fd385855 PM |
167 | !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST]) |
168 | return -EINVAL; | |
169 | ||
170 | if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST], | |
171 | NLBL_CIPSOV4_A_MAX, | |
172 | netlbl_cipsov4_genl_policy) != 0) | |
173 | return -EINVAL; | |
96cb8e33 PM |
174 | |
175 | doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL); | |
fd385855 PM |
176 | if (doi_def == NULL) |
177 | return -ENOMEM; | |
96cb8e33 PM |
178 | doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL); |
179 | if (doi_def->map.std == NULL) { | |
180 | ret_val = -ENOMEM; | |
181 | goto add_std_failure; | |
182 | } | |
183 | doi_def->type = CIPSO_V4_MAP_STD; | |
184 | ||
fd385855 PM |
185 | ret_val = netlbl_cipsov4_add_common(info, doi_def); |
186 | if (ret_val != 0) | |
96cb8e33 | 187 | goto add_std_failure; |
1fd2a25b | 188 | ret_val = -EINVAL; |
96cb8e33 | 189 | |
fd385855 PM |
190 | nla_for_each_nested(nla_a, |
191 | info->attrs[NLBL_CIPSOV4_A_MLSLVLLST], | |
192 | nla_a_rem) | |
193 | if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) { | |
1fd2a25b PM |
194 | if (nla_validate_nested(nla_a, |
195 | NLBL_CIPSOV4_A_MAX, | |
196 | netlbl_cipsov4_genl_policy) != 0) | |
197 | goto add_std_failure; | |
fd385855 PM |
198 | nla_for_each_nested(nla_b, nla_a, nla_b_rem) |
199 | switch (nla_b->nla_type) { | |
200 | case NLBL_CIPSOV4_A_MLSLVLLOC: | |
1fd2a25b PM |
201 | if (nla_get_u32(nla_b) > |
202 | CIPSO_V4_MAX_LOC_LVLS) | |
203 | goto add_std_failure; | |
fd385855 PM |
204 | if (nla_get_u32(nla_b) >= |
205 | doi_def->map.std->lvl.local_size) | |
206 | doi_def->map.std->lvl.local_size = | |
207 | nla_get_u32(nla_b) + 1; | |
208 | break; | |
209 | case NLBL_CIPSOV4_A_MLSLVLREM: | |
1fd2a25b PM |
210 | if (nla_get_u32(nla_b) > |
211 | CIPSO_V4_MAX_REM_LVLS) | |
212 | goto add_std_failure; | |
fd385855 PM |
213 | if (nla_get_u32(nla_b) >= |
214 | doi_def->map.std->lvl.cipso_size) | |
215 | doi_def->map.std->lvl.cipso_size = | |
216 | nla_get_u32(nla_b) + 1; | |
217 | break; | |
218 | } | |
219 | } | |
96cb8e33 PM |
220 | doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size, |
221 | sizeof(u32), | |
222 | GFP_KERNEL); | |
223 | if (doi_def->map.std->lvl.local == NULL) { | |
224 | ret_val = -ENOMEM; | |
225 | goto add_std_failure; | |
226 | } | |
96cb8e33 PM |
227 | doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size, |
228 | sizeof(u32), | |
229 | GFP_KERNEL); | |
230 | if (doi_def->map.std->lvl.cipso == NULL) { | |
231 | ret_val = -ENOMEM; | |
232 | goto add_std_failure; | |
233 | } | |
fd385855 PM |
234 | nla_for_each_nested(nla_a, |
235 | info->attrs[NLBL_CIPSOV4_A_MLSLVLLST], | |
236 | nla_a_rem) | |
237 | if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) { | |
238 | struct nlattr *lvl_loc; | |
239 | struct nlattr *lvl_rem; | |
240 | ||
fd385855 PM |
241 | lvl_loc = nla_find_nested(nla_a, |
242 | NLBL_CIPSOV4_A_MLSLVLLOC); | |
243 | lvl_rem = nla_find_nested(nla_a, | |
244 | NLBL_CIPSOV4_A_MLSLVLREM); | |
245 | if (lvl_loc == NULL || lvl_rem == NULL) | |
246 | goto add_std_failure; | |
247 | doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] = | |
248 | nla_get_u32(lvl_rem); | |
249 | doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] = | |
250 | nla_get_u32(lvl_loc); | |
251 | } | |
96cb8e33 | 252 | |
fd385855 PM |
253 | if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) { |
254 | if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST], | |
255 | NLBL_CIPSOV4_A_MAX, | |
256 | netlbl_cipsov4_genl_policy) != 0) | |
257 | goto add_std_failure; | |
258 | ||
259 | nla_for_each_nested(nla_a, | |
260 | info->attrs[NLBL_CIPSOV4_A_MLSCATLST], | |
261 | nla_a_rem) | |
262 | if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) { | |
263 | if (nla_validate_nested(nla_a, | |
264 | NLBL_CIPSOV4_A_MAX, | |
265 | netlbl_cipsov4_genl_policy) != 0) | |
266 | goto add_std_failure; | |
267 | nla_for_each_nested(nla_b, nla_a, nla_b_rem) | |
268 | switch (nla_b->nla_type) { | |
269 | case NLBL_CIPSOV4_A_MLSCATLOC: | |
1fd2a25b PM |
270 | if (nla_get_u32(nla_b) > |
271 | CIPSO_V4_MAX_LOC_CATS) | |
272 | goto add_std_failure; | |
fd385855 PM |
273 | if (nla_get_u32(nla_b) >= |
274 | doi_def->map.std->cat.local_size) | |
275 | doi_def->map.std->cat.local_size = | |
276 | nla_get_u32(nla_b) + 1; | |
277 | break; | |
278 | case NLBL_CIPSOV4_A_MLSCATREM: | |
1fd2a25b PM |
279 | if (nla_get_u32(nla_b) > |
280 | CIPSO_V4_MAX_REM_CATS) | |
281 | goto add_std_failure; | |
fd385855 PM |
282 | if (nla_get_u32(nla_b) >= |
283 | doi_def->map.std->cat.cipso_size) | |
284 | doi_def->map.std->cat.cipso_size = | |
285 | nla_get_u32(nla_b) + 1; | |
286 | break; | |
287 | } | |
288 | } | |
fd385855 PM |
289 | doi_def->map.std->cat.local = kcalloc( |
290 | doi_def->map.std->cat.local_size, | |
96cb8e33 PM |
291 | sizeof(u32), |
292 | GFP_KERNEL); | |
fd385855 PM |
293 | if (doi_def->map.std->cat.local == NULL) { |
294 | ret_val = -ENOMEM; | |
295 | goto add_std_failure; | |
296 | } | |
297 | doi_def->map.std->cat.cipso = kcalloc( | |
298 | doi_def->map.std->cat.cipso_size, | |
96cb8e33 PM |
299 | sizeof(u32), |
300 | GFP_KERNEL); | |
fd385855 PM |
301 | if (doi_def->map.std->cat.cipso == NULL) { |
302 | ret_val = -ENOMEM; | |
96cb8e33 | 303 | goto add_std_failure; |
fd385855 PM |
304 | } |
305 | nla_for_each_nested(nla_a, | |
306 | info->attrs[NLBL_CIPSOV4_A_MLSCATLST], | |
307 | nla_a_rem) | |
308 | if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) { | |
309 | struct nlattr *cat_loc; | |
310 | struct nlattr *cat_rem; | |
311 | ||
312 | cat_loc = nla_find_nested(nla_a, | |
313 | NLBL_CIPSOV4_A_MLSCATLOC); | |
314 | cat_rem = nla_find_nested(nla_a, | |
315 | NLBL_CIPSOV4_A_MLSCATREM); | |
316 | if (cat_loc == NULL || cat_rem == NULL) | |
317 | goto add_std_failure; | |
318 | doi_def->map.std->cat.local[ | |
319 | nla_get_u32(cat_loc)] = | |
320 | nla_get_u32(cat_rem); | |
321 | doi_def->map.std->cat.cipso[ | |
322 | nla_get_u32(cat_rem)] = | |
323 | nla_get_u32(cat_loc); | |
324 | } | |
96cb8e33 PM |
325 | } |
326 | ||
96cb8e33 PM |
327 | ret_val = cipso_v4_doi_add(doi_def); |
328 | if (ret_val != 0) | |
329 | goto add_std_failure; | |
330 | return 0; | |
331 | ||
332 | add_std_failure: | |
333 | if (doi_def) | |
334 | netlbl_cipsov4_doi_free(&doi_def->rcu); | |
335 | return ret_val; | |
336 | } | |
337 | ||
338 | /** | |
339 | * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition | |
fd385855 | 340 | * @info: the Generic NETLINK info block |
96cb8e33 PM |
341 | * |
342 | * Description: | |
343 | * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message | |
344 | * and add it to the CIPSO V4 engine. Return zero on success and non-zero on | |
345 | * error. | |
346 | * | |
347 | */ | |
fd385855 | 348 | static int netlbl_cipsov4_add_pass(struct genl_info *info) |
96cb8e33 | 349 | { |
fd385855 | 350 | int ret_val; |
96cb8e33 | 351 | struct cipso_v4_doi *doi_def = NULL; |
96cb8e33 | 352 | |
32f50cde | 353 | if (!info->attrs[NLBL_CIPSOV4_A_TAGLST]) |
fd385855 | 354 | return -EINVAL; |
96cb8e33 PM |
355 | |
356 | doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL); | |
fd385855 PM |
357 | if (doi_def == NULL) |
358 | return -ENOMEM; | |
96cb8e33 PM |
359 | doi_def->type = CIPSO_V4_MAP_PASS; |
360 | ||
fd385855 PM |
361 | ret_val = netlbl_cipsov4_add_common(info, doi_def); |
362 | if (ret_val != 0) | |
363 | goto add_pass_failure; | |
96cb8e33 | 364 | |
96cb8e33 PM |
365 | ret_val = cipso_v4_doi_add(doi_def); |
366 | if (ret_val != 0) | |
367 | goto add_pass_failure; | |
368 | return 0; | |
369 | ||
370 | add_pass_failure: | |
fd385855 | 371 | netlbl_cipsov4_doi_free(&doi_def->rcu); |
96cb8e33 PM |
372 | return ret_val; |
373 | } | |
374 | ||
375 | /** | |
376 | * netlbl_cipsov4_add - Handle an ADD message | |
377 | * @skb: the NETLINK buffer | |
378 | * @info: the Generic NETLINK info block | |
379 | * | |
380 | * Description: | |
381 | * Create a new DOI definition based on the given ADD message and add it to the | |
382 | * CIPSO V4 engine. Returns zero on success, negative values on failure. | |
383 | * | |
384 | */ | |
385 | static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info) | |
386 | ||
387 | { | |
388 | int ret_val = -EINVAL; | |
32f50cde PM |
389 | u32 type; |
390 | u32 doi; | |
391 | const char *type_str = "(unknown)"; | |
392 | struct audit_buffer *audit_buf; | |
95d4e6be | 393 | struct netlbl_audit audit_info; |
96cb8e33 | 394 | |
32f50cde PM |
395 | if (!info->attrs[NLBL_CIPSOV4_A_DOI] || |
396 | !info->attrs[NLBL_CIPSOV4_A_MTYPE]) | |
fd385855 | 397 | return -EINVAL; |
96cb8e33 | 398 | |
95d4e6be PM |
399 | doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]); |
400 | netlbl_netlink_auditinfo(skb, &audit_info); | |
401 | ||
32f50cde PM |
402 | type = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE]); |
403 | switch (type) { | |
96cb8e33 | 404 | case CIPSO_V4_MAP_STD: |
32f50cde | 405 | type_str = "std"; |
fd385855 | 406 | ret_val = netlbl_cipsov4_add_std(info); |
96cb8e33 PM |
407 | break; |
408 | case CIPSO_V4_MAP_PASS: | |
32f50cde | 409 | type_str = "pass"; |
fd385855 | 410 | ret_val = netlbl_cipsov4_add_pass(info); |
96cb8e33 PM |
411 | break; |
412 | } | |
413 | ||
95d4e6be PM |
414 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD, |
415 | &audit_info); | |
de64688f PM |
416 | if (audit_buf != NULL) { |
417 | audit_log_format(audit_buf, | |
418 | " cipso_doi=%u cipso_type=%s res=%u", | |
419 | doi, | |
420 | type_str, | |
421 | ret_val == 0 ? 1 : 0); | |
422 | audit_log_end(audit_buf); | |
423 | } | |
32f50cde | 424 | |
96cb8e33 PM |
425 | return ret_val; |
426 | } | |
427 | ||
428 | /** | |
429 | * netlbl_cipsov4_list - Handle a LIST message | |
430 | * @skb: the NETLINK buffer | |
431 | * @info: the Generic NETLINK info block | |
432 | * | |
433 | * Description: | |
fd385855 PM |
434 | * Process a user generated LIST message and respond accordingly. While the |
435 | * response message generated by the kernel is straightforward, determining | |
436 | * before hand the size of the buffer to allocate is not (we have to generate | |
437 | * the message to know the size). In order to keep this function sane what we | |
438 | * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in | |
439 | * that size, if we fail then we restart with a larger buffer and try again. | |
440 | * We continue in this manner until we hit a limit of failed attempts then we | |
441 | * give up and just send an error message. Returns zero on success and | |
442 | * negative values on error. | |
96cb8e33 PM |
443 | * |
444 | */ | |
445 | static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info) | |
446 | { | |
fd385855 PM |
447 | int ret_val; |
448 | struct sk_buff *ans_skb = NULL; | |
449 | u32 nlsze_mult = 1; | |
450 | void *data; | |
96cb8e33 | 451 | u32 doi; |
fd385855 PM |
452 | struct nlattr *nla_a; |
453 | struct nlattr *nla_b; | |
454 | struct cipso_v4_doi *doi_def; | |
455 | u32 iter; | |
96cb8e33 | 456 | |
fd385855 PM |
457 | if (!info->attrs[NLBL_CIPSOV4_A_DOI]) { |
458 | ret_val = -EINVAL; | |
96cb8e33 | 459 | goto list_failure; |
fd385855 | 460 | } |
96cb8e33 | 461 | |
fd385855 | 462 | list_start: |
339bf98f | 463 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL); |
96cb8e33 PM |
464 | if (ans_skb == NULL) { |
465 | ret_val = -ENOMEM; | |
466 | goto list_failure; | |
467 | } | |
17c157c8 TG |
468 | data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family, |
469 | 0, NLBL_CIPSOV4_C_LIST); | |
fd385855 PM |
470 | if (data == NULL) { |
471 | ret_val = -ENOMEM; | |
472 | goto list_failure; | |
473 | } | |
474 | ||
475 | doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]); | |
476 | ||
477 | rcu_read_lock(); | |
478 | doi_def = cipso_v4_doi_getdef(doi); | |
479 | if (doi_def == NULL) { | |
480 | ret_val = -EINVAL; | |
481 | goto list_failure; | |
482 | } | |
483 | ||
484 | ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type); | |
485 | if (ret_val != 0) | |
486 | goto list_failure_lock; | |
487 | ||
488 | nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST); | |
489 | if (nla_a == NULL) { | |
490 | ret_val = -ENOMEM; | |
491 | goto list_failure_lock; | |
492 | } | |
493 | for (iter = 0; | |
494 | iter < CIPSO_V4_TAG_MAXCNT && | |
495 | doi_def->tags[iter] != CIPSO_V4_TAG_INVALID; | |
496 | iter++) { | |
497 | ret_val = nla_put_u8(ans_skb, | |
498 | NLBL_CIPSOV4_A_TAG, | |
499 | doi_def->tags[iter]); | |
500 | if (ret_val != 0) | |
501 | goto list_failure_lock; | |
502 | } | |
503 | nla_nest_end(ans_skb, nla_a); | |
504 | ||
505 | switch (doi_def->type) { | |
506 | case CIPSO_V4_MAP_STD: | |
507 | nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST); | |
508 | if (nla_a == NULL) { | |
509 | ret_val = -ENOMEM; | |
510 | goto list_failure_lock; | |
511 | } | |
512 | for (iter = 0; | |
513 | iter < doi_def->map.std->lvl.local_size; | |
514 | iter++) { | |
515 | if (doi_def->map.std->lvl.local[iter] == | |
516 | CIPSO_V4_INV_LVL) | |
517 | continue; | |
518 | ||
519 | nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL); | |
520 | if (nla_b == NULL) { | |
521 | ret_val = -ENOMEM; | |
522 | goto list_retry; | |
523 | } | |
524 | ret_val = nla_put_u32(ans_skb, | |
525 | NLBL_CIPSOV4_A_MLSLVLLOC, | |
526 | iter); | |
527 | if (ret_val != 0) | |
528 | goto list_retry; | |
529 | ret_val = nla_put_u32(ans_skb, | |
530 | NLBL_CIPSOV4_A_MLSLVLREM, | |
531 | doi_def->map.std->lvl.local[iter]); | |
532 | if (ret_val != 0) | |
533 | goto list_retry; | |
534 | nla_nest_end(ans_skb, nla_b); | |
535 | } | |
536 | nla_nest_end(ans_skb, nla_a); | |
537 | ||
538 | nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST); | |
539 | if (nla_a == NULL) { | |
540 | ret_val = -ENOMEM; | |
541 | goto list_retry; | |
542 | } | |
543 | for (iter = 0; | |
544 | iter < doi_def->map.std->cat.local_size; | |
545 | iter++) { | |
546 | if (doi_def->map.std->cat.local[iter] == | |
547 | CIPSO_V4_INV_CAT) | |
548 | continue; | |
549 | ||
550 | nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT); | |
551 | if (nla_b == NULL) { | |
552 | ret_val = -ENOMEM; | |
553 | goto list_retry; | |
554 | } | |
555 | ret_val = nla_put_u32(ans_skb, | |
556 | NLBL_CIPSOV4_A_MLSCATLOC, | |
557 | iter); | |
558 | if (ret_val != 0) | |
559 | goto list_retry; | |
560 | ret_val = nla_put_u32(ans_skb, | |
561 | NLBL_CIPSOV4_A_MLSCATREM, | |
562 | doi_def->map.std->cat.local[iter]); | |
563 | if (ret_val != 0) | |
564 | goto list_retry; | |
565 | nla_nest_end(ans_skb, nla_b); | |
566 | } | |
567 | nla_nest_end(ans_skb, nla_a); | |
568 | ||
569 | break; | |
570 | } | |
571 | rcu_read_unlock(); | |
96cb8e33 | 572 | |
fd385855 PM |
573 | genlmsg_end(ans_skb, data); |
574 | ||
81878d27 | 575 | ret_val = genlmsg_reply(ans_skb, info); |
96cb8e33 PM |
576 | if (ret_val != 0) |
577 | goto list_failure; | |
578 | ||
579 | return 0; | |
580 | ||
fd385855 PM |
581 | list_retry: |
582 | /* XXX - this limit is a guesstimate */ | |
583 | if (nlsze_mult < 4) { | |
584 | rcu_read_unlock(); | |
585 | kfree_skb(ans_skb); | |
586 | nlsze_mult++; | |
587 | goto list_start; | |
588 | } | |
589 | list_failure_lock: | |
590 | rcu_read_unlock(); | |
96cb8e33 | 591 | list_failure: |
fd385855 PM |
592 | kfree_skb(ans_skb); |
593 | return ret_val; | |
594 | } | |
595 | ||
596 | /** | |
597 | * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL | |
598 | * @doi_def: the CIPSOv4 DOI definition | |
599 | * @arg: the netlbl_cipsov4_doiwalk_arg structure | |
600 | * | |
601 | * Description: | |
602 | * This function is designed to be used as a callback to the | |
603 | * cipso_v4_doi_walk() function for use in generating a response for a LISTALL | |
604 | * message. Returns the size of the message on success, negative values on | |
605 | * failure. | |
606 | * | |
607 | */ | |
608 | static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg) | |
609 | { | |
610 | int ret_val = -ENOMEM; | |
611 | struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg; | |
612 | void *data; | |
613 | ||
17c157c8 TG |
614 | data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid, |
615 | cb_arg->seq, &netlbl_cipsov4_gnl_family, | |
616 | NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL); | |
fd385855 PM |
617 | if (data == NULL) |
618 | goto listall_cb_failure; | |
619 | ||
620 | ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi); | |
621 | if (ret_val != 0) | |
622 | goto listall_cb_failure; | |
623 | ret_val = nla_put_u32(cb_arg->skb, | |
624 | NLBL_CIPSOV4_A_MTYPE, | |
625 | doi_def->type); | |
626 | if (ret_val != 0) | |
627 | goto listall_cb_failure; | |
628 | ||
629 | return genlmsg_end(cb_arg->skb, data); | |
630 | ||
631 | listall_cb_failure: | |
632 | genlmsg_cancel(cb_arg->skb, data); | |
96cb8e33 PM |
633 | return ret_val; |
634 | } | |
635 | ||
636 | /** | |
637 | * netlbl_cipsov4_listall - Handle a LISTALL message | |
638 | * @skb: the NETLINK buffer | |
fd385855 | 639 | * @cb: the NETLINK callback |
96cb8e33 PM |
640 | * |
641 | * Description: | |
642 | * Process a user generated LISTALL message and respond accordingly. Returns | |
643 | * zero on success and negative values on error. | |
644 | * | |
645 | */ | |
fd385855 PM |
646 | static int netlbl_cipsov4_listall(struct sk_buff *skb, |
647 | struct netlink_callback *cb) | |
96cb8e33 | 648 | { |
fd385855 PM |
649 | struct netlbl_cipsov4_doiwalk_arg cb_arg; |
650 | int doi_skip = cb->args[0]; | |
96cb8e33 | 651 | |
fd385855 PM |
652 | cb_arg.nl_cb = cb; |
653 | cb_arg.skb = skb; | |
654 | cb_arg.seq = cb->nlh->nlmsg_seq; | |
96cb8e33 | 655 | |
fd385855 | 656 | cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg); |
96cb8e33 | 657 | |
fd385855 PM |
658 | cb->args[0] = doi_skip; |
659 | return skb->len; | |
96cb8e33 PM |
660 | } |
661 | ||
662 | /** | |
663 | * netlbl_cipsov4_remove - Handle a REMOVE message | |
664 | * @skb: the NETLINK buffer | |
665 | * @info: the Generic NETLINK info block | |
666 | * | |
667 | * Description: | |
668 | * Process a user generated REMOVE message and respond accordingly. Returns | |
669 | * zero on success, negative values on failure. | |
670 | * | |
671 | */ | |
672 | static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info) | |
673 | { | |
fd385855 | 674 | int ret_val = -EINVAL; |
32f50cde PM |
675 | u32 doi = 0; |
676 | struct audit_buffer *audit_buf; | |
95d4e6be | 677 | struct netlbl_audit audit_info; |
96cb8e33 | 678 | |
95d4e6be PM |
679 | if (!info->attrs[NLBL_CIPSOV4_A_DOI]) |
680 | return -EINVAL; | |
32f50cde | 681 | |
95d4e6be PM |
682 | doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]); |
683 | netlbl_netlink_auditinfo(skb, &audit_info); | |
684 | ||
685 | ret_val = cipso_v4_doi_remove(doi, | |
686 | &audit_info, | |
687 | netlbl_cipsov4_doi_free); | |
688 | ||
689 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_DEL, | |
690 | &audit_info); | |
de64688f PM |
691 | if (audit_buf != NULL) { |
692 | audit_log_format(audit_buf, | |
693 | " cipso_doi=%u res=%u", | |
694 | doi, | |
695 | ret_val == 0 ? 1 : 0); | |
696 | audit_log_end(audit_buf); | |
697 | } | |
96cb8e33 | 698 | |
96cb8e33 PM |
699 | return ret_val; |
700 | } | |
701 | ||
702 | /* | |
703 | * NetLabel Generic NETLINK Command Definitions | |
704 | */ | |
705 | ||
706 | static struct genl_ops netlbl_cipsov4_genl_c_add = { | |
707 | .cmd = NLBL_CIPSOV4_C_ADD, | |
fd385855 PM |
708 | .flags = GENL_ADMIN_PERM, |
709 | .policy = netlbl_cipsov4_genl_policy, | |
96cb8e33 PM |
710 | .doit = netlbl_cipsov4_add, |
711 | .dumpit = NULL, | |
712 | }; | |
713 | ||
714 | static struct genl_ops netlbl_cipsov4_genl_c_remove = { | |
715 | .cmd = NLBL_CIPSOV4_C_REMOVE, | |
fd385855 PM |
716 | .flags = GENL_ADMIN_PERM, |
717 | .policy = netlbl_cipsov4_genl_policy, | |
96cb8e33 PM |
718 | .doit = netlbl_cipsov4_remove, |
719 | .dumpit = NULL, | |
720 | }; | |
721 | ||
722 | static struct genl_ops netlbl_cipsov4_genl_c_list = { | |
723 | .cmd = NLBL_CIPSOV4_C_LIST, | |
724 | .flags = 0, | |
fd385855 | 725 | .policy = netlbl_cipsov4_genl_policy, |
96cb8e33 PM |
726 | .doit = netlbl_cipsov4_list, |
727 | .dumpit = NULL, | |
728 | }; | |
729 | ||
730 | static struct genl_ops netlbl_cipsov4_genl_c_listall = { | |
731 | .cmd = NLBL_CIPSOV4_C_LISTALL, | |
732 | .flags = 0, | |
fd385855 PM |
733 | .policy = netlbl_cipsov4_genl_policy, |
734 | .doit = NULL, | |
735 | .dumpit = netlbl_cipsov4_listall, | |
96cb8e33 PM |
736 | }; |
737 | ||
738 | /* | |
739 | * NetLabel Generic NETLINK Protocol Functions | |
740 | */ | |
741 | ||
742 | /** | |
743 | * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component | |
744 | * | |
745 | * Description: | |
746 | * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK | |
747 | * mechanism. Returns zero on success, negative values on failure. | |
748 | * | |
749 | */ | |
750 | int netlbl_cipsov4_genl_init(void) | |
751 | { | |
752 | int ret_val; | |
753 | ||
754 | ret_val = genl_register_family(&netlbl_cipsov4_gnl_family); | |
755 | if (ret_val != 0) | |
756 | return ret_val; | |
757 | ||
758 | ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family, | |
759 | &netlbl_cipsov4_genl_c_add); | |
760 | if (ret_val != 0) | |
761 | return ret_val; | |
762 | ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family, | |
763 | &netlbl_cipsov4_genl_c_remove); | |
764 | if (ret_val != 0) | |
765 | return ret_val; | |
766 | ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family, | |
767 | &netlbl_cipsov4_genl_c_list); | |
768 | if (ret_val != 0) | |
769 | return ret_val; | |
770 | ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family, | |
771 | &netlbl_cipsov4_genl_c_listall); | |
772 | if (ret_val != 0) | |
773 | return ret_val; | |
774 | ||
775 | return 0; | |
776 | } |