Commit | Line | Data |
---|---|---|
d15c345f PM |
1 | /* |
2 | * NetLabel Management Support | |
3 | * | |
4 | * This file defines the management 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 | * | |
82c21bfa | 8 | * Author: Paul Moore <paul@paul-moore.com> |
d15c345f PM |
9 | * |
10 | */ | |
11 | ||
12 | /* | |
63c41688 | 13 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 |
d15c345f PM |
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 | |
d484ff15 | 26 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
d15c345f PM |
27 | * |
28 | */ | |
29 | ||
30 | #include <linux/types.h> | |
31 | #include <linux/socket.h> | |
32 | #include <linux/string.h> | |
33 | #include <linux/skbuff.h> | |
63c41688 PM |
34 | #include <linux/in.h> |
35 | #include <linux/in6.h> | |
5a0e3ad6 | 36 | #include <linux/slab.h> |
d15c345f PM |
37 | #include <net/sock.h> |
38 | #include <net/netlink.h> | |
39 | #include <net/genetlink.h> | |
63c41688 PM |
40 | #include <net/ip.h> |
41 | #include <net/ipv6.h> | |
d15c345f PM |
42 | #include <net/netlabel.h> |
43 | #include <net/cipso_ipv4.h> | |
dc7de73f | 44 | #include <net/calipso.h> |
60063497 | 45 | #include <linux/atomic.h> |
d15c345f | 46 | |
dc7de73f | 47 | #include "netlabel_calipso.h" |
d15c345f PM |
48 | #include "netlabel_domainhash.h" |
49 | #include "netlabel_user.h" | |
50 | #include "netlabel_mgmt.h" | |
51 | ||
c783f1ce PM |
52 | /* NetLabel configured protocol counter */ |
53 | atomic_t netlabel_mgmt_protocount = ATOMIC_INIT(0); | |
23bcdc1a | 54 | |
fd385855 PM |
55 | /* Argument struct for netlbl_domhsh_walk() */ |
56 | struct netlbl_domhsh_walk_arg { | |
57 | struct netlink_callback *nl_cb; | |
58 | struct sk_buff *skb; | |
59 | u32 seq; | |
60 | }; | |
61 | ||
d15c345f PM |
62 | /* NetLabel Generic NETLINK CIPSOv4 family */ |
63 | static struct genl_family netlbl_mgmt_gnl_family = { | |
64 | .id = GENL_ID_GENERATE, | |
65 | .hdrsize = 0, | |
66 | .name = NETLBL_NLTYPE_MGMT_NAME, | |
67 | .version = NETLBL_PROTO_VERSION, | |
fd385855 | 68 | .maxattr = NLBL_MGMT_A_MAX, |
d15c345f PM |
69 | }; |
70 | ||
fd385855 | 71 | /* NetLabel Netlink attribute policy */ |
ef7c79ed | 72 | static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = { |
fd385855 PM |
73 | [NLBL_MGMT_A_DOMAIN] = { .type = NLA_NUL_STRING }, |
74 | [NLBL_MGMT_A_PROTOCOL] = { .type = NLA_U32 }, | |
75 | [NLBL_MGMT_A_VERSION] = { .type = NLA_U32 }, | |
76 | [NLBL_MGMT_A_CV4DOI] = { .type = NLA_U32 }, | |
8f18e675 | 77 | [NLBL_MGMT_A_FAMILY] = { .type = NLA_U16 }, |
dc7de73f | 78 | [NLBL_MGMT_A_CLPDOI] = { .type = NLA_U32 }, |
fd385855 | 79 | }; |
d15c345f PM |
80 | |
81 | /* | |
63c41688 | 82 | * Helper Functions |
d15c345f PM |
83 | */ |
84 | ||
85 | /** | |
86 | * netlbl_mgmt_add - Handle an ADD message | |
d15c345f | 87 | * @info: the Generic NETLINK info block |
63c41688 | 88 | * @audit_info: NetLabel audit information |
d15c345f PM |
89 | * |
90 | * Description: | |
63c41688 PM |
91 | * Helper function for the ADD and ADDDEF messages to add the domain mappings |
92 | * from the message to the hash table. See netlabel.h for a description of the | |
93 | * message format. Returns zero on success, negative values on failure. | |
d15c345f PM |
94 | * |
95 | */ | |
63c41688 PM |
96 | static int netlbl_mgmt_add_common(struct genl_info *info, |
97 | struct netlbl_audit *audit_info) | |
d15c345f PM |
98 | { |
99 | int ret_val = -EINVAL; | |
63c41688 PM |
100 | struct netlbl_domaddr_map *addrmap = NULL; |
101 | struct cipso_v4_doi *cipsov4 = NULL; | |
dc7de73f HD |
102 | #if IS_ENABLED(CONFIG_IPV6) |
103 | struct calipso_doi *calipso = NULL; | |
104 | #endif | |
d15c345f | 105 | u32 tmp_val; |
4de46d5e | 106 | struct netlbl_dom_map *entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
95d4e6be | 107 | |
4de46d5e ME |
108 | if (!entry) |
109 | return -ENOMEM; | |
6a8b7f0c | 110 | entry->def.type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]); |
63c41688 PM |
111 | if (info->attrs[NLBL_MGMT_A_DOMAIN]) { |
112 | size_t tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]); | |
113 | entry->domain = kmalloc(tmp_size, GFP_KERNEL); | |
114 | if (entry->domain == NULL) { | |
115 | ret_val = -ENOMEM; | |
4de46d5e | 116 | goto add_free_entry; |
63c41688 PM |
117 | } |
118 | nla_strlcpy(entry->domain, | |
119 | info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size); | |
120 | } | |
121 | ||
6a8b7f0c | 122 | /* NOTE: internally we allow/use a entry->def.type value of |
63c41688 PM |
123 | * NETLBL_NLTYPE_ADDRSELECT but we don't currently allow users |
124 | * to pass that as a protocol value because we need to know the | |
125 | * "real" protocol */ | |
d15c345f | 126 | |
6a8b7f0c | 127 | switch (entry->def.type) { |
fd385855 | 128 | case NETLBL_NLTYPE_UNLABELED: |
8f18e675 HD |
129 | if (info->attrs[NLBL_MGMT_A_FAMILY]) |
130 | entry->family = | |
131 | nla_get_u16(info->attrs[NLBL_MGMT_A_FAMILY]); | |
132 | else | |
133 | entry->family = AF_UNSPEC; | |
fd385855 PM |
134 | break; |
135 | case NETLBL_NLTYPE_CIPSOV4: | |
136 | if (!info->attrs[NLBL_MGMT_A_CV4DOI]) | |
4de46d5e | 137 | goto add_free_domain; |
d15c345f | 138 | |
fd385855 | 139 | tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]); |
63c41688 PM |
140 | cipsov4 = cipso_v4_doi_getdef(tmp_val); |
141 | if (cipsov4 == NULL) | |
4de46d5e | 142 | goto add_free_domain; |
8f18e675 | 143 | entry->family = AF_INET; |
6a8b7f0c | 144 | entry->def.cipso = cipsov4; |
fd385855 | 145 | break; |
dc7de73f HD |
146 | #if IS_ENABLED(CONFIG_IPV6) |
147 | case NETLBL_NLTYPE_CALIPSO: | |
148 | if (!info->attrs[NLBL_MGMT_A_CLPDOI]) | |
149 | goto add_free_domain; | |
150 | ||
151 | tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CLPDOI]); | |
152 | calipso = calipso_doi_getdef(tmp_val); | |
153 | if (calipso == NULL) | |
154 | goto add_free_domain; | |
155 | entry->family = AF_INET6; | |
156 | entry->def.calipso = calipso; | |
157 | break; | |
158 | #endif /* IPv6 */ | |
fd385855 | 159 | default: |
4de46d5e | 160 | goto add_free_domain; |
d15c345f | 161 | } |
63c41688 | 162 | |
8f18e675 HD |
163 | if ((entry->family == AF_INET && info->attrs[NLBL_MGMT_A_IPV6ADDR]) || |
164 | (entry->family == AF_INET6 && info->attrs[NLBL_MGMT_A_IPV4ADDR])) | |
165 | goto add_doi_put_def; | |
166 | ||
63c41688 PM |
167 | if (info->attrs[NLBL_MGMT_A_IPV4ADDR]) { |
168 | struct in_addr *addr; | |
169 | struct in_addr *mask; | |
170 | struct netlbl_domaddr4_map *map; | |
171 | ||
172 | addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL); | |
173 | if (addrmap == NULL) { | |
174 | ret_val = -ENOMEM; | |
4de46d5e | 175 | goto add_doi_put_def; |
63c41688 PM |
176 | } |
177 | INIT_LIST_HEAD(&addrmap->list4); | |
178 | INIT_LIST_HEAD(&addrmap->list6); | |
179 | ||
180 | if (nla_len(info->attrs[NLBL_MGMT_A_IPV4ADDR]) != | |
181 | sizeof(struct in_addr)) { | |
182 | ret_val = -EINVAL; | |
4de46d5e | 183 | goto add_free_addrmap; |
63c41688 PM |
184 | } |
185 | if (nla_len(info->attrs[NLBL_MGMT_A_IPV4MASK]) != | |
186 | sizeof(struct in_addr)) { | |
187 | ret_val = -EINVAL; | |
4de46d5e | 188 | goto add_free_addrmap; |
63c41688 PM |
189 | } |
190 | addr = nla_data(info->attrs[NLBL_MGMT_A_IPV4ADDR]); | |
191 | mask = nla_data(info->attrs[NLBL_MGMT_A_IPV4MASK]); | |
192 | ||
193 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
194 | if (map == NULL) { | |
195 | ret_val = -ENOMEM; | |
4de46d5e | 196 | goto add_free_addrmap; |
63c41688 PM |
197 | } |
198 | map->list.addr = addr->s_addr & mask->s_addr; | |
199 | map->list.mask = mask->s_addr; | |
200 | map->list.valid = 1; | |
6a8b7f0c | 201 | map->def.type = entry->def.type; |
63c41688 | 202 | if (cipsov4) |
6a8b7f0c | 203 | map->def.cipso = cipsov4; |
63c41688 PM |
204 | |
205 | ret_val = netlbl_af4list_add(&map->list, &addrmap->list4); | |
206 | if (ret_val != 0) { | |
207 | kfree(map); | |
4de46d5e | 208 | goto add_free_addrmap; |
63c41688 PM |
209 | } |
210 | ||
8f18e675 | 211 | entry->family = AF_INET; |
6a8b7f0c PM |
212 | entry->def.type = NETLBL_NLTYPE_ADDRSELECT; |
213 | entry->def.addrsel = addrmap; | |
dfd56b8b | 214 | #if IS_ENABLED(CONFIG_IPV6) |
63c41688 PM |
215 | } else if (info->attrs[NLBL_MGMT_A_IPV6ADDR]) { |
216 | struct in6_addr *addr; | |
217 | struct in6_addr *mask; | |
218 | struct netlbl_domaddr6_map *map; | |
219 | ||
220 | addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL); | |
221 | if (addrmap == NULL) { | |
222 | ret_val = -ENOMEM; | |
4de46d5e | 223 | goto add_doi_put_def; |
63c41688 PM |
224 | } |
225 | INIT_LIST_HEAD(&addrmap->list4); | |
226 | INIT_LIST_HEAD(&addrmap->list6); | |
227 | ||
228 | if (nla_len(info->attrs[NLBL_MGMT_A_IPV6ADDR]) != | |
229 | sizeof(struct in6_addr)) { | |
230 | ret_val = -EINVAL; | |
4de46d5e | 231 | goto add_free_addrmap; |
63c41688 PM |
232 | } |
233 | if (nla_len(info->attrs[NLBL_MGMT_A_IPV6MASK]) != | |
234 | sizeof(struct in6_addr)) { | |
235 | ret_val = -EINVAL; | |
4de46d5e | 236 | goto add_free_addrmap; |
63c41688 PM |
237 | } |
238 | addr = nla_data(info->attrs[NLBL_MGMT_A_IPV6ADDR]); | |
239 | mask = nla_data(info->attrs[NLBL_MGMT_A_IPV6MASK]); | |
240 | ||
241 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
242 | if (map == NULL) { | |
243 | ret_val = -ENOMEM; | |
4de46d5e | 244 | goto add_free_addrmap; |
63c41688 | 245 | } |
4e3fd7a0 | 246 | map->list.addr = *addr; |
63c41688 PM |
247 | map->list.addr.s6_addr32[0] &= mask->s6_addr32[0]; |
248 | map->list.addr.s6_addr32[1] &= mask->s6_addr32[1]; | |
249 | map->list.addr.s6_addr32[2] &= mask->s6_addr32[2]; | |
250 | map->list.addr.s6_addr32[3] &= mask->s6_addr32[3]; | |
4e3fd7a0 | 251 | map->list.mask = *mask; |
63c41688 | 252 | map->list.valid = 1; |
6a8b7f0c | 253 | map->def.type = entry->def.type; |
dc7de73f HD |
254 | if (calipso) |
255 | map->def.calipso = calipso; | |
63c41688 PM |
256 | |
257 | ret_val = netlbl_af6list_add(&map->list, &addrmap->list6); | |
258 | if (ret_val != 0) { | |
259 | kfree(map); | |
4de46d5e | 260 | goto add_free_addrmap; |
63c41688 PM |
261 | } |
262 | ||
8f18e675 | 263 | entry->family = AF_INET6; |
6a8b7f0c PM |
264 | entry->def.type = NETLBL_NLTYPE_ADDRSELECT; |
265 | entry->def.addrsel = addrmap; | |
63c41688 PM |
266 | #endif /* IPv6 */ |
267 | } | |
268 | ||
269 | ret_val = netlbl_domhsh_add(entry, audit_info); | |
fd385855 | 270 | if (ret_val != 0) |
4de46d5e | 271 | goto add_free_addrmap; |
d15c345f | 272 | |
d15c345f PM |
273 | return 0; |
274 | ||
4de46d5e | 275 | add_free_addrmap: |
63c41688 | 276 | kfree(addrmap); |
4de46d5e ME |
277 | add_doi_put_def: |
278 | cipso_v4_doi_putdef(cipsov4); | |
dc7de73f HD |
279 | #if IS_ENABLED(CONFIG_IPV6) |
280 | calipso_doi_putdef(calipso); | |
281 | #endif | |
4de46d5e ME |
282 | add_free_domain: |
283 | kfree(entry->domain); | |
284 | add_free_entry: | |
d15c345f | 285 | kfree(entry); |
d15c345f PM |
286 | return ret_val; |
287 | } | |
288 | ||
63c41688 PM |
289 | /** |
290 | * netlbl_mgmt_listentry - List a NetLabel/LSM domain map entry | |
291 | * @skb: the NETLINK buffer | |
292 | * @entry: the map entry | |
293 | * | |
294 | * Description: | |
295 | * This function is a helper function used by the LISTALL and LISTDEF command | |
25985edc | 296 | * handlers. The caller is responsible for ensuring that the RCU read lock |
63c41688 PM |
297 | * is held. Returns zero on success, negative values on failure. |
298 | * | |
299 | */ | |
300 | static int netlbl_mgmt_listentry(struct sk_buff *skb, | |
301 | struct netlbl_dom_map *entry) | |
302 | { | |
f8a02479 | 303 | int ret_val = 0; |
63c41688 PM |
304 | struct nlattr *nla_a; |
305 | struct nlattr *nla_b; | |
306 | struct netlbl_af4list *iter4; | |
dfd56b8b | 307 | #if IS_ENABLED(CONFIG_IPV6) |
63c41688 PM |
308 | struct netlbl_af6list *iter6; |
309 | #endif | |
310 | ||
311 | if (entry->domain != NULL) { | |
312 | ret_val = nla_put_string(skb, | |
313 | NLBL_MGMT_A_DOMAIN, entry->domain); | |
314 | if (ret_val != 0) | |
315 | return ret_val; | |
316 | } | |
317 | ||
8f18e675 HD |
318 | ret_val = nla_put_u16(skb, NLBL_MGMT_A_FAMILY, entry->family); |
319 | if (ret_val != 0) | |
320 | return ret_val; | |
321 | ||
6a8b7f0c | 322 | switch (entry->def.type) { |
63c41688 PM |
323 | case NETLBL_NLTYPE_ADDRSELECT: |
324 | nla_a = nla_nest_start(skb, NLBL_MGMT_A_SELECTORLIST); | |
325 | if (nla_a == NULL) | |
326 | return -ENOMEM; | |
327 | ||
6a8b7f0c | 328 | netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) { |
63c41688 PM |
329 | struct netlbl_domaddr4_map *map4; |
330 | struct in_addr addr_struct; | |
331 | ||
332 | nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR); | |
333 | if (nla_b == NULL) | |
334 | return -ENOMEM; | |
335 | ||
336 | addr_struct.s_addr = iter4->addr; | |
930345ea JB |
337 | ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4ADDR, |
338 | addr_struct.s_addr); | |
63c41688 PM |
339 | if (ret_val != 0) |
340 | return ret_val; | |
341 | addr_struct.s_addr = iter4->mask; | |
930345ea JB |
342 | ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4MASK, |
343 | addr_struct.s_addr); | |
63c41688 PM |
344 | if (ret_val != 0) |
345 | return ret_val; | |
346 | map4 = netlbl_domhsh_addr4_entry(iter4); | |
347 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, | |
6a8b7f0c | 348 | map4->def.type); |
63c41688 PM |
349 | if (ret_val != 0) |
350 | return ret_val; | |
6a8b7f0c | 351 | switch (map4->def.type) { |
63c41688 PM |
352 | case NETLBL_NLTYPE_CIPSOV4: |
353 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI, | |
6a8b7f0c | 354 | map4->def.cipso->doi); |
63c41688 PM |
355 | if (ret_val != 0) |
356 | return ret_val; | |
357 | break; | |
358 | } | |
359 | ||
360 | nla_nest_end(skb, nla_b); | |
361 | } | |
dfd56b8b | 362 | #if IS_ENABLED(CONFIG_IPV6) |
6a8b7f0c | 363 | netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) { |
63c41688 PM |
364 | struct netlbl_domaddr6_map *map6; |
365 | ||
366 | nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR); | |
367 | if (nla_b == NULL) | |
368 | return -ENOMEM; | |
369 | ||
930345ea JB |
370 | ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6ADDR, |
371 | &iter6->addr); | |
63c41688 PM |
372 | if (ret_val != 0) |
373 | return ret_val; | |
930345ea JB |
374 | ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6MASK, |
375 | &iter6->mask); | |
63c41688 PM |
376 | if (ret_val != 0) |
377 | return ret_val; | |
378 | map6 = netlbl_domhsh_addr6_entry(iter6); | |
379 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, | |
6a8b7f0c | 380 | map6->def.type); |
63c41688 PM |
381 | if (ret_val != 0) |
382 | return ret_val; | |
383 | ||
dc7de73f HD |
384 | switch (map6->def.type) { |
385 | case NETLBL_NLTYPE_CALIPSO: | |
386 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_CLPDOI, | |
387 | map6->def.calipso->doi); | |
388 | if (ret_val != 0) | |
389 | return ret_val; | |
390 | break; | |
391 | } | |
392 | ||
63c41688 PM |
393 | nla_nest_end(skb, nla_b); |
394 | } | |
395 | #endif /* IPv6 */ | |
396 | ||
397 | nla_nest_end(skb, nla_a); | |
398 | break; | |
399 | case NETLBL_NLTYPE_UNLABELED: | |
dc7de73f HD |
400 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, |
401 | entry->def.type); | |
63c41688 PM |
402 | break; |
403 | case NETLBL_NLTYPE_CIPSOV4: | |
dc7de73f HD |
404 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, |
405 | entry->def.type); | |
63c41688 PM |
406 | if (ret_val != 0) |
407 | return ret_val; | |
408 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI, | |
6a8b7f0c | 409 | entry->def.cipso->doi); |
63c41688 | 410 | break; |
dc7de73f HD |
411 | case NETLBL_NLTYPE_CALIPSO: |
412 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, | |
413 | entry->def.type); | |
414 | if (ret_val != 0) | |
415 | return ret_val; | |
416 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_CLPDOI, | |
417 | entry->def.calipso->doi); | |
418 | break; | |
63c41688 PM |
419 | } |
420 | ||
421 | return ret_val; | |
422 | } | |
423 | ||
424 | /* | |
425 | * NetLabel Command Handlers | |
426 | */ | |
427 | ||
428 | /** | |
429 | * netlbl_mgmt_add - Handle an ADD message | |
430 | * @skb: the NETLINK buffer | |
431 | * @info: the Generic NETLINK info block | |
432 | * | |
433 | * Description: | |
434 | * Process a user generated ADD message and add the domains from the message | |
435 | * to the hash table. See netlabel.h for a description of the message format. | |
436 | * Returns zero on success, negative values on failure. | |
437 | * | |
438 | */ | |
439 | static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) | |
440 | { | |
441 | struct netlbl_audit audit_info; | |
442 | ||
443 | if ((!info->attrs[NLBL_MGMT_A_DOMAIN]) || | |
444 | (!info->attrs[NLBL_MGMT_A_PROTOCOL]) || | |
445 | (info->attrs[NLBL_MGMT_A_IPV4ADDR] && | |
446 | info->attrs[NLBL_MGMT_A_IPV6ADDR]) || | |
447 | (info->attrs[NLBL_MGMT_A_IPV4MASK] && | |
448 | info->attrs[NLBL_MGMT_A_IPV6MASK]) || | |
449 | ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^ | |
450 | (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) || | |
451 | ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^ | |
452 | (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL))) | |
453 | return -EINVAL; | |
454 | ||
455 | netlbl_netlink_auditinfo(skb, &audit_info); | |
456 | ||
457 | return netlbl_mgmt_add_common(info, &audit_info); | |
458 | } | |
459 | ||
d15c345f PM |
460 | /** |
461 | * netlbl_mgmt_remove - Handle a REMOVE message | |
462 | * @skb: the NETLINK buffer | |
463 | * @info: the Generic NETLINK info block | |
464 | * | |
465 | * Description: | |
466 | * Process a user generated REMOVE message and remove the specified domain | |
467 | * mappings. Returns zero on success, negative values on failure. | |
468 | * | |
469 | */ | |
470 | static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info) | |
471 | { | |
fd385855 | 472 | char *domain; |
95d4e6be | 473 | struct netlbl_audit audit_info; |
d15c345f | 474 | |
fd385855 PM |
475 | if (!info->attrs[NLBL_MGMT_A_DOMAIN]) |
476 | return -EINVAL; | |
d15c345f | 477 | |
95d4e6be PM |
478 | netlbl_netlink_auditinfo(skb, &audit_info); |
479 | ||
fd385855 | 480 | domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]); |
8f18e675 | 481 | return netlbl_domhsh_remove(domain, AF_UNSPEC, &audit_info); |
fd385855 PM |
482 | } |
483 | ||
484 | /** | |
485 | * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL | |
486 | * @entry: the domain mapping hash table entry | |
487 | * @arg: the netlbl_domhsh_walk_arg structure | |
488 | * | |
489 | * Description: | |
490 | * This function is designed to be used as a callback to the | |
491 | * netlbl_domhsh_walk() function for use in generating a response for a LISTALL | |
492 | * message. Returns the size of the message on success, negative values on | |
493 | * failure. | |
494 | * | |
495 | */ | |
496 | static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg) | |
497 | { | |
498 | int ret_val = -ENOMEM; | |
499 | struct netlbl_domhsh_walk_arg *cb_arg = arg; | |
500 | void *data; | |
501 | ||
15e47304 | 502 | data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid, |
17c157c8 TG |
503 | cb_arg->seq, &netlbl_mgmt_gnl_family, |
504 | NLM_F_MULTI, NLBL_MGMT_C_LISTALL); | |
fd385855 PM |
505 | if (data == NULL) |
506 | goto listall_cb_failure; | |
507 | ||
63c41688 | 508 | ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry); |
fd385855 PM |
509 | if (ret_val != 0) |
510 | goto listall_cb_failure; | |
d15c345f | 511 | |
fd385855 | 512 | cb_arg->seq++; |
053c095a JB |
513 | genlmsg_end(cb_arg->skb, data); |
514 | return 0; | |
d15c345f | 515 | |
fd385855 PM |
516 | listall_cb_failure: |
517 | genlmsg_cancel(cb_arg->skb, data); | |
d15c345f PM |
518 | return ret_val; |
519 | } | |
520 | ||
521 | /** | |
fd385855 | 522 | * netlbl_mgmt_listall - Handle a LISTALL message |
d15c345f | 523 | * @skb: the NETLINK buffer |
fd385855 | 524 | * @cb: the NETLINK callback |
d15c345f PM |
525 | * |
526 | * Description: | |
fd385855 PM |
527 | * Process a user generated LISTALL message and dumps the domain hash table in |
528 | * a form suitable for use in a kernel generated LISTALL message. Returns zero | |
529 | * on success, negative values on failure. | |
d15c345f PM |
530 | * |
531 | */ | |
fd385855 PM |
532 | static int netlbl_mgmt_listall(struct sk_buff *skb, |
533 | struct netlink_callback *cb) | |
d15c345f | 534 | { |
fd385855 PM |
535 | struct netlbl_domhsh_walk_arg cb_arg; |
536 | u32 skip_bkt = cb->args[0]; | |
537 | u32 skip_chain = cb->args[1]; | |
538 | ||
539 | cb_arg.nl_cb = cb; | |
540 | cb_arg.skb = skb; | |
541 | cb_arg.seq = cb->nlh->nlmsg_seq; | |
542 | ||
543 | netlbl_domhsh_walk(&skip_bkt, | |
544 | &skip_chain, | |
545 | netlbl_mgmt_listall_cb, | |
546 | &cb_arg); | |
547 | ||
548 | cb->args[0] = skip_bkt; | |
549 | cb->args[1] = skip_chain; | |
550 | return skb->len; | |
d15c345f PM |
551 | } |
552 | ||
553 | /** | |
554 | * netlbl_mgmt_adddef - Handle an ADDDEF message | |
555 | * @skb: the NETLINK buffer | |
556 | * @info: the Generic NETLINK info block | |
557 | * | |
558 | * Description: | |
559 | * Process a user generated ADDDEF message and respond accordingly. Returns | |
560 | * zero on success, negative values on failure. | |
561 | * | |
562 | */ | |
563 | static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) | |
564 | { | |
95d4e6be | 565 | struct netlbl_audit audit_info; |
d15c345f | 566 | |
63c41688 PM |
567 | if ((!info->attrs[NLBL_MGMT_A_PROTOCOL]) || |
568 | (info->attrs[NLBL_MGMT_A_IPV4ADDR] && | |
569 | info->attrs[NLBL_MGMT_A_IPV6ADDR]) || | |
570 | (info->attrs[NLBL_MGMT_A_IPV4MASK] && | |
571 | info->attrs[NLBL_MGMT_A_IPV6MASK]) || | |
572 | ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^ | |
573 | (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) || | |
574 | ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^ | |
575 | (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL))) | |
576 | return -EINVAL; | |
d15c345f | 577 | |
95d4e6be PM |
578 | netlbl_netlink_auditinfo(skb, &audit_info); |
579 | ||
63c41688 | 580 | return netlbl_mgmt_add_common(info, &audit_info); |
d15c345f PM |
581 | } |
582 | ||
583 | /** | |
584 | * netlbl_mgmt_removedef - Handle a REMOVEDEF message | |
585 | * @skb: the NETLINK buffer | |
586 | * @info: the Generic NETLINK info block | |
587 | * | |
588 | * Description: | |
589 | * Process a user generated REMOVEDEF message and remove the default domain | |
590 | * mapping. Returns zero on success, negative values on failure. | |
591 | * | |
592 | */ | |
593 | static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info) | |
594 | { | |
95d4e6be PM |
595 | struct netlbl_audit audit_info; |
596 | ||
597 | netlbl_netlink_auditinfo(skb, &audit_info); | |
598 | ||
8f18e675 | 599 | return netlbl_domhsh_remove_default(AF_UNSPEC, &audit_info); |
d15c345f PM |
600 | } |
601 | ||
602 | /** | |
603 | * netlbl_mgmt_listdef - Handle a LISTDEF message | |
604 | * @skb: the NETLINK buffer | |
605 | * @info: the Generic NETLINK info block | |
606 | * | |
607 | * Description: | |
608 | * Process a user generated LISTDEF message and dumps the default domain | |
609 | * mapping in a form suitable for use in a kernel generated LISTDEF message. | |
610 | * Returns zero on success, negative values on failure. | |
611 | * | |
612 | */ | |
613 | static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info) | |
614 | { | |
615 | int ret_val = -ENOMEM; | |
fd385855 PM |
616 | struct sk_buff *ans_skb = NULL; |
617 | void *data; | |
618 | struct netlbl_dom_map *entry; | |
8f18e675 HD |
619 | u16 family; |
620 | ||
621 | if (info->attrs[NLBL_MGMT_A_FAMILY]) | |
622 | family = nla_get_u16(info->attrs[NLBL_MGMT_A_FAMILY]); | |
623 | else | |
624 | family = AF_INET; | |
d15c345f | 625 | |
339bf98f | 626 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
d15c345f | 627 | if (ans_skb == NULL) |
fd385855 | 628 | return -ENOMEM; |
17c157c8 TG |
629 | data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family, |
630 | 0, NLBL_MGMT_C_LISTDEF); | |
fd385855 | 631 | if (data == NULL) |
d15c345f | 632 | goto listdef_failure; |
d15c345f | 633 | |
fd385855 | 634 | rcu_read_lock(); |
8f18e675 | 635 | entry = netlbl_domhsh_getentry(NULL, family); |
fd385855 PM |
636 | if (entry == NULL) { |
637 | ret_val = -ENOENT; | |
638 | goto listdef_failure_lock; | |
639 | } | |
63c41688 | 640 | ret_val = netlbl_mgmt_listentry(ans_skb, entry); |
fd385855 | 641 | rcu_read_unlock(); |
63c41688 PM |
642 | if (ret_val != 0) |
643 | goto listdef_failure; | |
d15c345f | 644 | |
fd385855 | 645 | genlmsg_end(ans_skb, data); |
fe785bee | 646 | return genlmsg_reply(ans_skb, info); |
d15c345f | 647 | |
fd385855 PM |
648 | listdef_failure_lock: |
649 | rcu_read_unlock(); | |
d15c345f | 650 | listdef_failure: |
fd385855 | 651 | kfree_skb(ans_skb); |
d15c345f PM |
652 | return ret_val; |
653 | } | |
654 | ||
655 | /** | |
fd385855 PM |
656 | * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response |
657 | * @skb: the skb to write to | |
fd385855 PM |
658 | * @cb: the NETLINK callback |
659 | * @protocol: the NetLabel protocol to use in the message | |
d15c345f PM |
660 | * |
661 | * Description: | |
fd385855 PM |
662 | * This function is to be used in conjunction with netlbl_mgmt_protocols() to |
663 | * answer a application's PROTOCOLS message. Returns the size of the message | |
664 | * on success, negative values on failure. | |
d15c345f PM |
665 | * |
666 | */ | |
fd385855 PM |
667 | static int netlbl_mgmt_protocols_cb(struct sk_buff *skb, |
668 | struct netlink_callback *cb, | |
669 | u32 protocol) | |
d15c345f PM |
670 | { |
671 | int ret_val = -ENOMEM; | |
fd385855 PM |
672 | void *data; |
673 | ||
15e47304 | 674 | data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
17c157c8 TG |
675 | &netlbl_mgmt_gnl_family, NLM_F_MULTI, |
676 | NLBL_MGMT_C_PROTOCOLS); | |
fd385855 PM |
677 | if (data == NULL) |
678 | goto protocols_cb_failure; | |
679 | ||
680 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol); | |
d15c345f | 681 | if (ret_val != 0) |
fd385855 | 682 | goto protocols_cb_failure; |
d15c345f | 683 | |
053c095a JB |
684 | genlmsg_end(skb, data); |
685 | return 0; | |
d15c345f | 686 | |
fd385855 PM |
687 | protocols_cb_failure: |
688 | genlmsg_cancel(skb, data); | |
d15c345f PM |
689 | return ret_val; |
690 | } | |
691 | ||
fd385855 PM |
692 | /** |
693 | * netlbl_mgmt_protocols - Handle a PROTOCOLS message | |
694 | * @skb: the NETLINK buffer | |
695 | * @cb: the NETLINK callback | |
696 | * | |
697 | * Description: | |
698 | * Process a user generated PROTOCOLS message and respond accordingly. | |
699 | * | |
700 | */ | |
701 | static int netlbl_mgmt_protocols(struct sk_buff *skb, | |
702 | struct netlink_callback *cb) | |
703 | { | |
704 | u32 protos_sent = cb->args[0]; | |
705 | ||
706 | if (protos_sent == 0) { | |
707 | if (netlbl_mgmt_protocols_cb(skb, | |
708 | cb, | |
709 | NETLBL_NLTYPE_UNLABELED) < 0) | |
710 | goto protocols_return; | |
711 | protos_sent++; | |
712 | } | |
713 | if (protos_sent == 1) { | |
714 | if (netlbl_mgmt_protocols_cb(skb, | |
715 | cb, | |
716 | NETLBL_NLTYPE_CIPSOV4) < 0) | |
717 | goto protocols_return; | |
718 | protos_sent++; | |
719 | } | |
cb72d382 HD |
720 | #if IS_ENABLED(CONFIG_IPV6) |
721 | if (protos_sent == 2) { | |
722 | if (netlbl_mgmt_protocols_cb(skb, | |
723 | cb, | |
724 | NETLBL_NLTYPE_CALIPSO) < 0) | |
725 | goto protocols_return; | |
726 | protos_sent++; | |
727 | } | |
728 | #endif | |
fd385855 PM |
729 | |
730 | protocols_return: | |
731 | cb->args[0] = protos_sent; | |
732 | return skb->len; | |
733 | } | |
734 | ||
d15c345f PM |
735 | /** |
736 | * netlbl_mgmt_version - Handle a VERSION message | |
737 | * @skb: the NETLINK buffer | |
738 | * @info: the Generic NETLINK info block | |
739 | * | |
740 | * Description: | |
741 | * Process a user generated VERSION message and respond accordingly. Returns | |
742 | * zero on success, negative values on failure. | |
743 | * | |
744 | */ | |
745 | static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info) | |
746 | { | |
747 | int ret_val = -ENOMEM; | |
748 | struct sk_buff *ans_skb = NULL; | |
fd385855 | 749 | void *data; |
d15c345f | 750 | |
339bf98f | 751 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
d15c345f | 752 | if (ans_skb == NULL) |
fd385855 | 753 | return -ENOMEM; |
17c157c8 TG |
754 | data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family, |
755 | 0, NLBL_MGMT_C_VERSION); | |
fd385855 | 756 | if (data == NULL) |
d15c345f PM |
757 | goto version_failure; |
758 | ||
fd385855 PM |
759 | ret_val = nla_put_u32(ans_skb, |
760 | NLBL_MGMT_A_VERSION, | |
761 | NETLBL_PROTO_VERSION); | |
d15c345f PM |
762 | if (ret_val != 0) |
763 | goto version_failure; | |
764 | ||
fd385855 | 765 | genlmsg_end(ans_skb, data); |
fe785bee | 766 | return genlmsg_reply(ans_skb, info); |
d15c345f PM |
767 | |
768 | version_failure: | |
769 | kfree_skb(ans_skb); | |
d15c345f PM |
770 | return ret_val; |
771 | } | |
772 | ||
773 | ||
774 | /* | |
775 | * NetLabel Generic NETLINK Command Definitions | |
776 | */ | |
777 | ||
4534de83 | 778 | static const struct genl_ops netlbl_mgmt_genl_ops[] = { |
227c43c3 | 779 | { |
d15c345f | 780 | .cmd = NLBL_MGMT_C_ADD, |
fd385855 PM |
781 | .flags = GENL_ADMIN_PERM, |
782 | .policy = netlbl_mgmt_genl_policy, | |
d15c345f PM |
783 | .doit = netlbl_mgmt_add, |
784 | .dumpit = NULL, | |
227c43c3 PE |
785 | }, |
786 | { | |
d15c345f | 787 | .cmd = NLBL_MGMT_C_REMOVE, |
fd385855 PM |
788 | .flags = GENL_ADMIN_PERM, |
789 | .policy = netlbl_mgmt_genl_policy, | |
d15c345f PM |
790 | .doit = netlbl_mgmt_remove, |
791 | .dumpit = NULL, | |
227c43c3 PE |
792 | }, |
793 | { | |
fd385855 | 794 | .cmd = NLBL_MGMT_C_LISTALL, |
d15c345f | 795 | .flags = 0, |
fd385855 PM |
796 | .policy = netlbl_mgmt_genl_policy, |
797 | .doit = NULL, | |
798 | .dumpit = netlbl_mgmt_listall, | |
227c43c3 PE |
799 | }, |
800 | { | |
d15c345f | 801 | .cmd = NLBL_MGMT_C_ADDDEF, |
fd385855 PM |
802 | .flags = GENL_ADMIN_PERM, |
803 | .policy = netlbl_mgmt_genl_policy, | |
d15c345f PM |
804 | .doit = netlbl_mgmt_adddef, |
805 | .dumpit = NULL, | |
227c43c3 PE |
806 | }, |
807 | { | |
d15c345f | 808 | .cmd = NLBL_MGMT_C_REMOVEDEF, |
fd385855 PM |
809 | .flags = GENL_ADMIN_PERM, |
810 | .policy = netlbl_mgmt_genl_policy, | |
d15c345f PM |
811 | .doit = netlbl_mgmt_removedef, |
812 | .dumpit = NULL, | |
227c43c3 PE |
813 | }, |
814 | { | |
d15c345f PM |
815 | .cmd = NLBL_MGMT_C_LISTDEF, |
816 | .flags = 0, | |
fd385855 | 817 | .policy = netlbl_mgmt_genl_policy, |
d15c345f PM |
818 | .doit = netlbl_mgmt_listdef, |
819 | .dumpit = NULL, | |
227c43c3 PE |
820 | }, |
821 | { | |
fd385855 | 822 | .cmd = NLBL_MGMT_C_PROTOCOLS, |
d15c345f | 823 | .flags = 0, |
fd385855 PM |
824 | .policy = netlbl_mgmt_genl_policy, |
825 | .doit = NULL, | |
826 | .dumpit = netlbl_mgmt_protocols, | |
227c43c3 PE |
827 | }, |
828 | { | |
d15c345f PM |
829 | .cmd = NLBL_MGMT_C_VERSION, |
830 | .flags = 0, | |
fd385855 | 831 | .policy = netlbl_mgmt_genl_policy, |
d15c345f PM |
832 | .doit = netlbl_mgmt_version, |
833 | .dumpit = NULL, | |
227c43c3 | 834 | }, |
d15c345f PM |
835 | }; |
836 | ||
837 | /* | |
838 | * NetLabel Generic NETLINK Protocol Functions | |
839 | */ | |
840 | ||
841 | /** | |
842 | * netlbl_mgmt_genl_init - Register the NetLabel management component | |
843 | * | |
844 | * Description: | |
845 | * Register the NetLabel management component with the Generic NETLINK | |
846 | * mechanism. Returns zero on success, negative values on failure. | |
847 | * | |
848 | */ | |
05705e4e | 849 | int __init netlbl_mgmt_genl_init(void) |
d15c345f | 850 | { |
7ae740df | 851 | return genl_register_family_with_ops(&netlbl_mgmt_gnl_family, |
c53ed742 | 852 | netlbl_mgmt_genl_ops); |
d15c345f | 853 | } |