PM / devfreq: Add proper locking around list_del()
[linux/fpc-iii.git] / net / netlabel / netlabel_mgmt.c
blobf85d0e07af2dd9c7f4f4fb2f6b05fcf99b21c8eb
1 /*
2 * NetLabel Management Support
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.
8 * Author: Paul Moore <paul@paul-moore.com>
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
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.
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.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #include <linux/types.h>
31 #include <linux/socket.h>
32 #include <linux/string.h>
33 #include <linux/skbuff.h>
34 #include <linux/in.h>
35 #include <linux/in6.h>
36 #include <linux/slab.h>
37 #include <net/sock.h>
38 #include <net/netlink.h>
39 #include <net/genetlink.h>
40 #include <net/ip.h>
41 #include <net/ipv6.h>
42 #include <net/netlabel.h>
43 #include <net/cipso_ipv4.h>
44 #include <net/calipso.h>
45 #include <linux/atomic.h>
47 #include "netlabel_calipso.h"
48 #include "netlabel_domainhash.h"
49 #include "netlabel_user.h"
50 #include "netlabel_mgmt.h"
52 /* NetLabel configured protocol counter */
53 atomic_t netlabel_mgmt_protocount = ATOMIC_INIT(0);
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;
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,
68 .maxattr = NLBL_MGMT_A_MAX,
71 /* NetLabel Netlink attribute policy */
72 static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
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 },
77 [NLBL_MGMT_A_FAMILY] = { .type = NLA_U16 },
78 [NLBL_MGMT_A_CLPDOI] = { .type = NLA_U32 },
82 * Helper Functions
85 /**
86 * netlbl_mgmt_add - Handle an ADD message
87 * @info: the Generic NETLINK info block
88 * @audit_info: NetLabel audit information
90 * Description:
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.
96 static int netlbl_mgmt_add_common(struct genl_info *info,
97 struct netlbl_audit *audit_info)
99 int ret_val = -EINVAL;
100 struct netlbl_domaddr_map *addrmap = NULL;
101 struct cipso_v4_doi *cipsov4 = NULL;
102 #if IS_ENABLED(CONFIG_IPV6)
103 struct calipso_doi *calipso = NULL;
104 #endif
105 u32 tmp_val;
106 struct netlbl_dom_map *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
108 if (!entry)
109 return -ENOMEM;
110 entry->def.type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
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;
116 goto add_free_entry;
118 nla_strlcpy(entry->domain,
119 info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
122 /* NOTE: internally we allow/use a entry->def.type value of
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 */
127 switch (entry->def.type) {
128 case NETLBL_NLTYPE_UNLABELED:
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;
134 break;
135 case NETLBL_NLTYPE_CIPSOV4:
136 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
137 goto add_free_domain;
139 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
140 cipsov4 = cipso_v4_doi_getdef(tmp_val);
141 if (cipsov4 == NULL)
142 goto add_free_domain;
143 entry->family = AF_INET;
144 entry->def.cipso = cipsov4;
145 break;
146 #if IS_ENABLED(CONFIG_IPV6)
147 case NETLBL_NLTYPE_CALIPSO:
148 if (!info->attrs[NLBL_MGMT_A_CLPDOI])
149 goto add_free_domain;
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 */
159 default:
160 goto add_free_domain;
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;
167 if (info->attrs[NLBL_MGMT_A_IPV4ADDR]) {
168 struct in_addr *addr;
169 struct in_addr *mask;
170 struct netlbl_domaddr4_map *map;
172 addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
173 if (addrmap == NULL) {
174 ret_val = -ENOMEM;
175 goto add_doi_put_def;
177 INIT_LIST_HEAD(&addrmap->list4);
178 INIT_LIST_HEAD(&addrmap->list6);
180 if (nla_len(info->attrs[NLBL_MGMT_A_IPV4ADDR]) !=
181 sizeof(struct in_addr)) {
182 ret_val = -EINVAL;
183 goto add_free_addrmap;
185 if (nla_len(info->attrs[NLBL_MGMT_A_IPV4MASK]) !=
186 sizeof(struct in_addr)) {
187 ret_val = -EINVAL;
188 goto add_free_addrmap;
190 addr = nla_data(info->attrs[NLBL_MGMT_A_IPV4ADDR]);
191 mask = nla_data(info->attrs[NLBL_MGMT_A_IPV4MASK]);
193 map = kzalloc(sizeof(*map), GFP_KERNEL);
194 if (map == NULL) {
195 ret_val = -ENOMEM;
196 goto add_free_addrmap;
198 map->list.addr = addr->s_addr & mask->s_addr;
199 map->list.mask = mask->s_addr;
200 map->list.valid = 1;
201 map->def.type = entry->def.type;
202 if (cipsov4)
203 map->def.cipso = cipsov4;
205 ret_val = netlbl_af4list_add(&map->list, &addrmap->list4);
206 if (ret_val != 0) {
207 kfree(map);
208 goto add_free_addrmap;
211 entry->family = AF_INET;
212 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
213 entry->def.addrsel = addrmap;
214 #if IS_ENABLED(CONFIG_IPV6)
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;
220 addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
221 if (addrmap == NULL) {
222 ret_val = -ENOMEM;
223 goto add_doi_put_def;
225 INIT_LIST_HEAD(&addrmap->list4);
226 INIT_LIST_HEAD(&addrmap->list6);
228 if (nla_len(info->attrs[NLBL_MGMT_A_IPV6ADDR]) !=
229 sizeof(struct in6_addr)) {
230 ret_val = -EINVAL;
231 goto add_free_addrmap;
233 if (nla_len(info->attrs[NLBL_MGMT_A_IPV6MASK]) !=
234 sizeof(struct in6_addr)) {
235 ret_val = -EINVAL;
236 goto add_free_addrmap;
238 addr = nla_data(info->attrs[NLBL_MGMT_A_IPV6ADDR]);
239 mask = nla_data(info->attrs[NLBL_MGMT_A_IPV6MASK]);
241 map = kzalloc(sizeof(*map), GFP_KERNEL);
242 if (map == NULL) {
243 ret_val = -ENOMEM;
244 goto add_free_addrmap;
246 map->list.addr = *addr;
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];
251 map->list.mask = *mask;
252 map->list.valid = 1;
253 map->def.type = entry->def.type;
254 if (calipso)
255 map->def.calipso = calipso;
257 ret_val = netlbl_af6list_add(&map->list, &addrmap->list6);
258 if (ret_val != 0) {
259 kfree(map);
260 goto add_free_addrmap;
263 entry->family = AF_INET6;
264 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
265 entry->def.addrsel = addrmap;
266 #endif /* IPv6 */
269 ret_val = netlbl_domhsh_add(entry, audit_info);
270 if (ret_val != 0)
271 goto add_free_addrmap;
273 return 0;
275 add_free_addrmap:
276 kfree(addrmap);
277 add_doi_put_def:
278 cipso_v4_doi_putdef(cipsov4);
279 #if IS_ENABLED(CONFIG_IPV6)
280 calipso_doi_putdef(calipso);
281 #endif
282 add_free_domain:
283 kfree(entry->domain);
284 add_free_entry:
285 kfree(entry);
286 return ret_val;
290 * netlbl_mgmt_listentry - List a NetLabel/LSM domain map entry
291 * @skb: the NETLINK buffer
292 * @entry: the map entry
294 * Description:
295 * This function is a helper function used by the LISTALL and LISTDEF command
296 * handlers. The caller is responsible for ensuring that the RCU read lock
297 * is held. Returns zero on success, negative values on failure.
300 static int netlbl_mgmt_listentry(struct sk_buff *skb,
301 struct netlbl_dom_map *entry)
303 int ret_val = 0;
304 struct nlattr *nla_a;
305 struct nlattr *nla_b;
306 struct netlbl_af4list *iter4;
307 #if IS_ENABLED(CONFIG_IPV6)
308 struct netlbl_af6list *iter6;
309 #endif
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;
318 ret_val = nla_put_u16(skb, NLBL_MGMT_A_FAMILY, entry->family);
319 if (ret_val != 0)
320 return ret_val;
322 switch (entry->def.type) {
323 case NETLBL_NLTYPE_ADDRSELECT:
324 nla_a = nla_nest_start(skb, NLBL_MGMT_A_SELECTORLIST);
325 if (nla_a == NULL)
326 return -ENOMEM;
328 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
329 struct netlbl_domaddr4_map *map4;
330 struct in_addr addr_struct;
332 nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
333 if (nla_b == NULL)
334 return -ENOMEM;
336 addr_struct.s_addr = iter4->addr;
337 ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4ADDR,
338 addr_struct.s_addr);
339 if (ret_val != 0)
340 return ret_val;
341 addr_struct.s_addr = iter4->mask;
342 ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4MASK,
343 addr_struct.s_addr);
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,
348 map4->def.type);
349 if (ret_val != 0)
350 return ret_val;
351 switch (map4->def.type) {
352 case NETLBL_NLTYPE_CIPSOV4:
353 ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
354 map4->def.cipso->doi);
355 if (ret_val != 0)
356 return ret_val;
357 break;
360 nla_nest_end(skb, nla_b);
362 #if IS_ENABLED(CONFIG_IPV6)
363 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
364 struct netlbl_domaddr6_map *map6;
366 nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
367 if (nla_b == NULL)
368 return -ENOMEM;
370 ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6ADDR,
371 &iter6->addr);
372 if (ret_val != 0)
373 return ret_val;
374 ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6MASK,
375 &iter6->mask);
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,
380 map6->def.type);
381 if (ret_val != 0)
382 return ret_val;
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;
393 nla_nest_end(skb, nla_b);
395 #endif /* IPv6 */
397 nla_nest_end(skb, nla_a);
398 break;
399 case NETLBL_NLTYPE_UNLABELED:
400 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
401 entry->def.type);
402 break;
403 case NETLBL_NLTYPE_CIPSOV4:
404 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
405 entry->def.type);
406 if (ret_val != 0)
407 return ret_val;
408 ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
409 entry->def.cipso->doi);
410 break;
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;
421 return ret_val;
425 * NetLabel Command Handlers
429 * netlbl_mgmt_add - Handle an ADD message
430 * @skb: the NETLINK buffer
431 * @info: the Generic NETLINK info block
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.
439 static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
441 struct netlbl_audit audit_info;
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;
455 netlbl_netlink_auditinfo(skb, &audit_info);
457 return netlbl_mgmt_add_common(info, &audit_info);
461 * netlbl_mgmt_remove - Handle a REMOVE message
462 * @skb: the NETLINK buffer
463 * @info: the Generic NETLINK info block
465 * Description:
466 * Process a user generated REMOVE message and remove the specified domain
467 * mappings. Returns zero on success, negative values on failure.
470 static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
472 char *domain;
473 struct netlbl_audit audit_info;
475 if (!info->attrs[NLBL_MGMT_A_DOMAIN])
476 return -EINVAL;
478 netlbl_netlink_auditinfo(skb, &audit_info);
480 domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
481 return netlbl_domhsh_remove(domain, AF_UNSPEC, &audit_info);
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
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.
496 static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
498 int ret_val = -ENOMEM;
499 struct netlbl_domhsh_walk_arg *cb_arg = arg;
500 void *data;
502 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
503 cb_arg->seq, &netlbl_mgmt_gnl_family,
504 NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
505 if (data == NULL)
506 goto listall_cb_failure;
508 ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry);
509 if (ret_val != 0)
510 goto listall_cb_failure;
512 cb_arg->seq++;
513 genlmsg_end(cb_arg->skb, data);
514 return 0;
516 listall_cb_failure:
517 genlmsg_cancel(cb_arg->skb, data);
518 return ret_val;
522 * netlbl_mgmt_listall - Handle a LISTALL message
523 * @skb: the NETLINK buffer
524 * @cb: the NETLINK callback
526 * Description:
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.
532 static int netlbl_mgmt_listall(struct sk_buff *skb,
533 struct netlink_callback *cb)
535 struct netlbl_domhsh_walk_arg cb_arg;
536 u32 skip_bkt = cb->args[0];
537 u32 skip_chain = cb->args[1];
539 cb_arg.nl_cb = cb;
540 cb_arg.skb = skb;
541 cb_arg.seq = cb->nlh->nlmsg_seq;
543 netlbl_domhsh_walk(&skip_bkt,
544 &skip_chain,
545 netlbl_mgmt_listall_cb,
546 &cb_arg);
548 cb->args[0] = skip_bkt;
549 cb->args[1] = skip_chain;
550 return skb->len;
554 * netlbl_mgmt_adddef - Handle an ADDDEF message
555 * @skb: the NETLINK buffer
556 * @info: the Generic NETLINK info block
558 * Description:
559 * Process a user generated ADDDEF message and respond accordingly. Returns
560 * zero on success, negative values on failure.
563 static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
565 struct netlbl_audit audit_info;
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;
578 netlbl_netlink_auditinfo(skb, &audit_info);
580 return netlbl_mgmt_add_common(info, &audit_info);
584 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
585 * @skb: the NETLINK buffer
586 * @info: the Generic NETLINK info block
588 * Description:
589 * Process a user generated REMOVEDEF message and remove the default domain
590 * mapping. Returns zero on success, negative values on failure.
593 static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
595 struct netlbl_audit audit_info;
597 netlbl_netlink_auditinfo(skb, &audit_info);
599 return netlbl_domhsh_remove_default(AF_UNSPEC, &audit_info);
603 * netlbl_mgmt_listdef - Handle a LISTDEF message
604 * @skb: the NETLINK buffer
605 * @info: the Generic NETLINK info block
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.
613 static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
615 int ret_val = -ENOMEM;
616 struct sk_buff *ans_skb = NULL;
617 void *data;
618 struct netlbl_dom_map *entry;
619 u16 family;
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;
626 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
627 if (ans_skb == NULL)
628 return -ENOMEM;
629 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
630 0, NLBL_MGMT_C_LISTDEF);
631 if (data == NULL)
632 goto listdef_failure;
634 rcu_read_lock();
635 entry = netlbl_domhsh_getentry(NULL, family);
636 if (entry == NULL) {
637 ret_val = -ENOENT;
638 goto listdef_failure_lock;
640 ret_val = netlbl_mgmt_listentry(ans_skb, entry);
641 rcu_read_unlock();
642 if (ret_val != 0)
643 goto listdef_failure;
645 genlmsg_end(ans_skb, data);
646 return genlmsg_reply(ans_skb, info);
648 listdef_failure_lock:
649 rcu_read_unlock();
650 listdef_failure:
651 kfree_skb(ans_skb);
652 return ret_val;
656 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
657 * @skb: the skb to write to
658 * @cb: the NETLINK callback
659 * @protocol: the NetLabel protocol to use in the message
661 * Description:
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.
667 static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
668 struct netlink_callback *cb,
669 u32 protocol)
671 int ret_val = -ENOMEM;
672 void *data;
674 data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
675 &netlbl_mgmt_gnl_family, NLM_F_MULTI,
676 NLBL_MGMT_C_PROTOCOLS);
677 if (data == NULL)
678 goto protocols_cb_failure;
680 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
681 if (ret_val != 0)
682 goto protocols_cb_failure;
684 genlmsg_end(skb, data);
685 return 0;
687 protocols_cb_failure:
688 genlmsg_cancel(skb, data);
689 return ret_val;
693 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
694 * @skb: the NETLINK buffer
695 * @cb: the NETLINK callback
697 * Description:
698 * Process a user generated PROTOCOLS message and respond accordingly.
701 static int netlbl_mgmt_protocols(struct sk_buff *skb,
702 struct netlink_callback *cb)
704 u32 protos_sent = cb->args[0];
706 if (protos_sent == 0) {
707 if (netlbl_mgmt_protocols_cb(skb,
709 NETLBL_NLTYPE_UNLABELED) < 0)
710 goto protocols_return;
711 protos_sent++;
713 if (protos_sent == 1) {
714 if (netlbl_mgmt_protocols_cb(skb,
716 NETLBL_NLTYPE_CIPSOV4) < 0)
717 goto protocols_return;
718 protos_sent++;
720 #if IS_ENABLED(CONFIG_IPV6)
721 if (protos_sent == 2) {
722 if (netlbl_mgmt_protocols_cb(skb,
724 NETLBL_NLTYPE_CALIPSO) < 0)
725 goto protocols_return;
726 protos_sent++;
728 #endif
730 protocols_return:
731 cb->args[0] = protos_sent;
732 return skb->len;
736 * netlbl_mgmt_version - Handle a VERSION message
737 * @skb: the NETLINK buffer
738 * @info: the Generic NETLINK info block
740 * Description:
741 * Process a user generated VERSION message and respond accordingly. Returns
742 * zero on success, negative values on failure.
745 static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
747 int ret_val = -ENOMEM;
748 struct sk_buff *ans_skb = NULL;
749 void *data;
751 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
752 if (ans_skb == NULL)
753 return -ENOMEM;
754 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
755 0, NLBL_MGMT_C_VERSION);
756 if (data == NULL)
757 goto version_failure;
759 ret_val = nla_put_u32(ans_skb,
760 NLBL_MGMT_A_VERSION,
761 NETLBL_PROTO_VERSION);
762 if (ret_val != 0)
763 goto version_failure;
765 genlmsg_end(ans_skb, data);
766 return genlmsg_reply(ans_skb, info);
768 version_failure:
769 kfree_skb(ans_skb);
770 return ret_val;
775 * NetLabel Generic NETLINK Command Definitions
778 static const struct genl_ops netlbl_mgmt_genl_ops[] = {
780 .cmd = NLBL_MGMT_C_ADD,
781 .flags = GENL_ADMIN_PERM,
782 .policy = netlbl_mgmt_genl_policy,
783 .doit = netlbl_mgmt_add,
784 .dumpit = NULL,
787 .cmd = NLBL_MGMT_C_REMOVE,
788 .flags = GENL_ADMIN_PERM,
789 .policy = netlbl_mgmt_genl_policy,
790 .doit = netlbl_mgmt_remove,
791 .dumpit = NULL,
794 .cmd = NLBL_MGMT_C_LISTALL,
795 .flags = 0,
796 .policy = netlbl_mgmt_genl_policy,
797 .doit = NULL,
798 .dumpit = netlbl_mgmt_listall,
801 .cmd = NLBL_MGMT_C_ADDDEF,
802 .flags = GENL_ADMIN_PERM,
803 .policy = netlbl_mgmt_genl_policy,
804 .doit = netlbl_mgmt_adddef,
805 .dumpit = NULL,
808 .cmd = NLBL_MGMT_C_REMOVEDEF,
809 .flags = GENL_ADMIN_PERM,
810 .policy = netlbl_mgmt_genl_policy,
811 .doit = netlbl_mgmt_removedef,
812 .dumpit = NULL,
815 .cmd = NLBL_MGMT_C_LISTDEF,
816 .flags = 0,
817 .policy = netlbl_mgmt_genl_policy,
818 .doit = netlbl_mgmt_listdef,
819 .dumpit = NULL,
822 .cmd = NLBL_MGMT_C_PROTOCOLS,
823 .flags = 0,
824 .policy = netlbl_mgmt_genl_policy,
825 .doit = NULL,
826 .dumpit = netlbl_mgmt_protocols,
829 .cmd = NLBL_MGMT_C_VERSION,
830 .flags = 0,
831 .policy = netlbl_mgmt_genl_policy,
832 .doit = netlbl_mgmt_version,
833 .dumpit = NULL,
838 * NetLabel Generic NETLINK Protocol Functions
842 * netlbl_mgmt_genl_init - Register the NetLabel management component
844 * Description:
845 * Register the NetLabel management component with the Generic NETLINK
846 * mechanism. Returns zero on success, negative values on failure.
849 int __init netlbl_mgmt_genl_init(void)
851 return genl_register_family_with_ops(&netlbl_mgmt_gnl_family,
852 netlbl_mgmt_genl_ops);