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