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