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