blob: e00fc219c72b7c70cd6c581ccd474e0a8c3b2034 [file] [log] [blame]
Paul Moored15c3452006-08-03 16:48:37 -07001/*
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.moore@hp.com>
9 *
10 */
11
12/*
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
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, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/types.h>
32#include <linux/socket.h>
33#include <linux/string.h>
34#include <linux/skbuff.h>
35#include <net/sock.h>
36#include <net/netlink.h>
37#include <net/genetlink.h>
38#include <net/netlabel.h>
39#include <net/cipso_ipv4.h>
40
41#include "netlabel_domainhash.h"
42#include "netlabel_user.h"
43#include "netlabel_mgmt.h"
44
Paul Moorefd385852006-09-25 15:56:37 -070045/* Argument struct for netlbl_domhsh_walk() */
46struct netlbl_domhsh_walk_arg {
47 struct netlink_callback *nl_cb;
48 struct sk_buff *skb;
49 u32 seq;
50};
51
Paul Moored15c3452006-08-03 16:48:37 -070052/* NetLabel Generic NETLINK CIPSOv4 family */
53static struct genl_family netlbl_mgmt_gnl_family = {
54 .id = GENL_ID_GENERATE,
55 .hdrsize = 0,
56 .name = NETLBL_NLTYPE_MGMT_NAME,
57 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -070058 .maxattr = NLBL_MGMT_A_MAX,
Paul Moored15c3452006-08-03 16:48:37 -070059};
60
Paul Moorefd385852006-09-25 15:56:37 -070061/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -070062static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -070063 [NLBL_MGMT_A_DOMAIN] = { .type = NLA_NUL_STRING },
64 [NLBL_MGMT_A_PROTOCOL] = { .type = NLA_U32 },
65 [NLBL_MGMT_A_VERSION] = { .type = NLA_U32 },
66 [NLBL_MGMT_A_CV4DOI] = { .type = NLA_U32 },
67};
Paul Moored15c3452006-08-03 16:48:37 -070068
69/*
70 * NetLabel Command Handlers
71 */
72
73/**
74 * netlbl_mgmt_add - Handle an ADD message
75 * @skb: the NETLINK buffer
76 * @info: the Generic NETLINK info block
77 *
78 * Description:
79 * Process a user generated ADD message and add the domains from the message
80 * to the hash table. See netlabel.h for a description of the message format.
81 * Returns zero on success, negative values on failure.
82 *
83 */
84static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
85{
86 int ret_val = -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -070087 struct netlbl_dom_map *entry = NULL;
Paul Moorefd385852006-09-25 15:56:37 -070088 size_t tmp_size;
Paul Moored15c3452006-08-03 16:48:37 -070089 u32 tmp_val;
Paul Moore95d4e6b2006-09-29 17:05:05 -070090 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -070091
Paul Moorefd385852006-09-25 15:56:37 -070092 if (!info->attrs[NLBL_MGMT_A_DOMAIN] ||
93 !info->attrs[NLBL_MGMT_A_PROTOCOL])
94 goto add_failure;
95
Paul Moore95d4e6b2006-09-29 17:05:05 -070096 netlbl_netlink_auditinfo(skb, &audit_info);
97
Paul Moorefd385852006-09-25 15:56:37 -070098 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
99 if (entry == NULL) {
100 ret_val = -ENOMEM;
101 goto add_failure;
102 }
103 tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]);
104 entry->domain = kmalloc(tmp_size, GFP_KERNEL);
105 if (entry->domain == NULL) {
106 ret_val = -ENOMEM;
107 goto add_failure;
108 }
109 entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
110 nla_strlcpy(entry->domain, info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
111
112 switch (entry->type) {
113 case NETLBL_NLTYPE_UNLABELED:
Paul Moore95d4e6b2006-09-29 17:05:05 -0700114 ret_val = netlbl_domhsh_add(entry, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700115 break;
116 case NETLBL_NLTYPE_CIPSOV4:
117 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
118 goto add_failure;
119
120 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
121 /* We should be holding a rcu_read_lock() here while we hold
122 * the result but since the entry will always be deleted when
123 * the CIPSO DOI is deleted we aren't going to keep the
124 * lock. */
125 rcu_read_lock();
126 entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
127 if (entry->type_def.cipsov4 == NULL) {
128 rcu_read_unlock();
129 goto add_failure;
130 }
Paul Moore95d4e6b2006-09-29 17:05:05 -0700131 ret_val = netlbl_domhsh_add(entry, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700132 rcu_read_unlock();
133 break;
134 default:
135 goto add_failure;
136 }
Paul Moored15c3452006-08-03 16:48:37 -0700137 if (ret_val != 0)
138 goto add_failure;
139
Paul Moored15c3452006-08-03 16:48:37 -0700140 return 0;
141
142add_failure:
143 if (entry)
144 kfree(entry->domain);
145 kfree(entry);
Paul Moored15c3452006-08-03 16:48:37 -0700146 return ret_val;
147}
148
149/**
150 * netlbl_mgmt_remove - Handle a REMOVE message
151 * @skb: the NETLINK buffer
152 * @info: the Generic NETLINK info block
153 *
154 * Description:
155 * Process a user generated REMOVE message and remove the specified domain
156 * mappings. Returns zero on success, negative values on failure.
157 *
158 */
159static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
160{
Paul Moorefd385852006-09-25 15:56:37 -0700161 char *domain;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700162 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700163
Paul Moorefd385852006-09-25 15:56:37 -0700164 if (!info->attrs[NLBL_MGMT_A_DOMAIN])
165 return -EINVAL;
166
Paul Moore95d4e6b2006-09-29 17:05:05 -0700167 netlbl_netlink_auditinfo(skb, &audit_info);
168
Paul Moorefd385852006-09-25 15:56:37 -0700169 domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
Paul Moore95d4e6b2006-09-29 17:05:05 -0700170 return netlbl_domhsh_remove(domain, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700171}
172
173/**
174 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
175 * @entry: the domain mapping hash table entry
176 * @arg: the netlbl_domhsh_walk_arg structure
177 *
178 * Description:
179 * This function is designed to be used as a callback to the
180 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
181 * message. Returns the size of the message on success, negative values on
182 * failure.
183 *
184 */
185static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
186{
187 int ret_val = -ENOMEM;
188 struct netlbl_domhsh_walk_arg *cb_arg = arg;
189 void *data;
190
Thomas Graf17c157c2006-11-14 19:46:02 -0800191 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
192 cb_arg->seq, &netlbl_mgmt_gnl_family,
193 NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
Paul Moorefd385852006-09-25 15:56:37 -0700194 if (data == NULL)
195 goto listall_cb_failure;
196
197 ret_val = nla_put_string(cb_arg->skb,
198 NLBL_MGMT_A_DOMAIN,
199 entry->domain);
Paul Moored15c3452006-08-03 16:48:37 -0700200 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700201 goto listall_cb_failure;
202 ret_val = nla_put_u32(cb_arg->skb, NLBL_MGMT_A_PROTOCOL, entry->type);
203 if (ret_val != 0)
204 goto listall_cb_failure;
205 switch (entry->type) {
206 case NETLBL_NLTYPE_CIPSOV4:
207 ret_val = nla_put_u32(cb_arg->skb,
208 NLBL_MGMT_A_CV4DOI,
209 entry->type_def.cipsov4->doi);
Paul Moored15c3452006-08-03 16:48:37 -0700210 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700211 goto listall_cb_failure;
212 break;
Paul Moored15c3452006-08-03 16:48:37 -0700213 }
214
Paul Moorefd385852006-09-25 15:56:37 -0700215 cb_arg->seq++;
216 return genlmsg_end(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700217
Paul Moorefd385852006-09-25 15:56:37 -0700218listall_cb_failure:
219 genlmsg_cancel(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700220 return ret_val;
221}
222
223/**
Paul Moorefd385852006-09-25 15:56:37 -0700224 * netlbl_mgmt_listall - Handle a LISTALL message
Paul Moored15c3452006-08-03 16:48:37 -0700225 * @skb: the NETLINK buffer
Paul Moorefd385852006-09-25 15:56:37 -0700226 * @cb: the NETLINK callback
Paul Moored15c3452006-08-03 16:48:37 -0700227 *
228 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700229 * Process a user generated LISTALL message and dumps the domain hash table in
230 * a form suitable for use in a kernel generated LISTALL message. Returns zero
231 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700232 *
233 */
Paul Moorefd385852006-09-25 15:56:37 -0700234static int netlbl_mgmt_listall(struct sk_buff *skb,
235 struct netlink_callback *cb)
Paul Moored15c3452006-08-03 16:48:37 -0700236{
Paul Moorefd385852006-09-25 15:56:37 -0700237 struct netlbl_domhsh_walk_arg cb_arg;
238 u32 skip_bkt = cb->args[0];
239 u32 skip_chain = cb->args[1];
Paul Moored15c3452006-08-03 16:48:37 -0700240
Paul Moorefd385852006-09-25 15:56:37 -0700241 cb_arg.nl_cb = cb;
242 cb_arg.skb = skb;
243 cb_arg.seq = cb->nlh->nlmsg_seq;
Paul Moored15c3452006-08-03 16:48:37 -0700244
Paul Moorefd385852006-09-25 15:56:37 -0700245 netlbl_domhsh_walk(&skip_bkt,
246 &skip_chain,
247 netlbl_mgmt_listall_cb,
248 &cb_arg);
Paul Moored15c3452006-08-03 16:48:37 -0700249
Paul Moorefd385852006-09-25 15:56:37 -0700250 cb->args[0] = skip_bkt;
251 cb->args[1] = skip_chain;
252 return skb->len;
Paul Moored15c3452006-08-03 16:48:37 -0700253}
254
255/**
256 * netlbl_mgmt_adddef - Handle an ADDDEF message
257 * @skb: the NETLINK buffer
258 * @info: the Generic NETLINK info block
259 *
260 * Description:
261 * Process a user generated ADDDEF message and respond accordingly. Returns
262 * zero on success, negative values on failure.
263 *
264 */
265static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
266{
267 int ret_val = -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -0700268 struct netlbl_dom_map *entry = NULL;
269 u32 tmp_val;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700270 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700271
Paul Moorefd385852006-09-25 15:56:37 -0700272 if (!info->attrs[NLBL_MGMT_A_PROTOCOL])
Paul Moored15c3452006-08-03 16:48:37 -0700273 goto adddef_failure;
274
Paul Moore95d4e6b2006-09-29 17:05:05 -0700275 netlbl_netlink_auditinfo(skb, &audit_info);
276
Paul Moored15c3452006-08-03 16:48:37 -0700277 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
278 if (entry == NULL) {
279 ret_val = -ENOMEM;
280 goto adddef_failure;
281 }
Paul Moorefd385852006-09-25 15:56:37 -0700282 entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
Paul Moored15c3452006-08-03 16:48:37 -0700283
Paul Moored15c3452006-08-03 16:48:37 -0700284 switch (entry->type) {
285 case NETLBL_NLTYPE_UNLABELED:
Paul Moore95d4e6b2006-09-29 17:05:05 -0700286 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700287 break;
288 case NETLBL_NLTYPE_CIPSOV4:
Paul Moorefd385852006-09-25 15:56:37 -0700289 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
Paul Moored15c3452006-08-03 16:48:37 -0700290 goto adddef_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700291
292 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
293 /* We should be holding a rcu_read_lock() here while we hold
294 * the result but since the entry will always be deleted when
295 * the CIPSO DOI is deleted we aren't going to keep the
296 * lock. */
Paul Moored15c3452006-08-03 16:48:37 -0700297 rcu_read_lock();
298 entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
299 if (entry->type_def.cipsov4 == NULL) {
300 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700301 goto adddef_failure;
302 }
Paul Moore95d4e6b2006-09-29 17:05:05 -0700303 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700304 rcu_read_unlock();
305 break;
306 default:
Paul Moorefd385852006-09-25 15:56:37 -0700307 goto adddef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700308 }
309 if (ret_val != 0)
310 goto adddef_failure;
311
Paul Moored15c3452006-08-03 16:48:37 -0700312 return 0;
313
314adddef_failure:
315 kfree(entry);
Paul Moored15c3452006-08-03 16:48:37 -0700316 return ret_val;
317}
318
319/**
320 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
321 * @skb: the NETLINK buffer
322 * @info: the Generic NETLINK info block
323 *
324 * Description:
325 * Process a user generated REMOVEDEF message and remove the default domain
326 * mapping. Returns zero on success, negative values on failure.
327 *
328 */
329static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
330{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700331 struct netlbl_audit audit_info;
332
333 netlbl_netlink_auditinfo(skb, &audit_info);
334
335 return netlbl_domhsh_remove_default(&audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700336}
337
338/**
339 * netlbl_mgmt_listdef - Handle a LISTDEF message
340 * @skb: the NETLINK buffer
341 * @info: the Generic NETLINK info block
342 *
343 * Description:
344 * Process a user generated LISTDEF message and dumps the default domain
345 * mapping in a form suitable for use in a kernel generated LISTDEF message.
346 * Returns zero on success, negative values on failure.
347 *
348 */
349static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
350{
351 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700352 struct sk_buff *ans_skb = NULL;
353 void *data;
354 struct netlbl_dom_map *entry;
Paul Moored15c3452006-08-03 16:48:37 -0700355
Thomas Graf339bf982006-11-10 14:10:15 -0800356 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700357 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700358 return -ENOMEM;
Thomas Graf17c157c2006-11-14 19:46:02 -0800359 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
360 0, NLBL_MGMT_C_LISTDEF);
Paul Moorefd385852006-09-25 15:56:37 -0700361 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700362 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700363
Paul Moorefd385852006-09-25 15:56:37 -0700364 rcu_read_lock();
365 entry = netlbl_domhsh_getentry(NULL);
366 if (entry == NULL) {
367 ret_val = -ENOENT;
368 goto listdef_failure_lock;
369 }
370 ret_val = nla_put_u32(ans_skb, NLBL_MGMT_A_PROTOCOL, entry->type);
371 if (ret_val != 0)
372 goto listdef_failure_lock;
373 switch (entry->type) {
374 case NETLBL_NLTYPE_CIPSOV4:
375 ret_val = nla_put_u32(ans_skb,
376 NLBL_MGMT_A_CV4DOI,
377 entry->type_def.cipsov4->doi);
378 if (ret_val != 0)
379 goto listdef_failure_lock;
380 break;
381 }
382 rcu_read_unlock();
383
384 genlmsg_end(ans_skb, data);
385
Thomas Graf81878d22006-11-14 19:45:27 -0800386 ret_val = genlmsg_reply(ans_skb, info);
Paul Moored15c3452006-08-03 16:48:37 -0700387 if (ret_val != 0)
388 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700389 return 0;
390
Paul Moorefd385852006-09-25 15:56:37 -0700391listdef_failure_lock:
392 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700393listdef_failure:
Paul Moorefd385852006-09-25 15:56:37 -0700394 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700395 return ret_val;
396}
397
398/**
Paul Moorefd385852006-09-25 15:56:37 -0700399 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
400 * @skb: the skb to write to
401 * @seq: the NETLINK sequence number
402 * @cb: the NETLINK callback
403 * @protocol: the NetLabel protocol to use in the message
Paul Moored15c3452006-08-03 16:48:37 -0700404 *
405 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700406 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
407 * answer a application's PROTOCOLS message. Returns the size of the message
408 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700409 *
410 */
Paul Moorefd385852006-09-25 15:56:37 -0700411static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
412 struct netlink_callback *cb,
413 u32 protocol)
Paul Moored15c3452006-08-03 16:48:37 -0700414{
415 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700416 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700417
Thomas Graf17c157c2006-11-14 19:46:02 -0800418 data = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
419 &netlbl_mgmt_gnl_family, NLM_F_MULTI,
420 NLBL_MGMT_C_PROTOCOLS);
Paul Moorefd385852006-09-25 15:56:37 -0700421 if (data == NULL)
422 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700423
Paul Moorefd385852006-09-25 15:56:37 -0700424 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
Paul Moored15c3452006-08-03 16:48:37 -0700425 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700426 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700427
Paul Moorefd385852006-09-25 15:56:37 -0700428 return genlmsg_end(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700429
Paul Moorefd385852006-09-25 15:56:37 -0700430protocols_cb_failure:
431 genlmsg_cancel(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700432 return ret_val;
433}
434
435/**
Paul Moorefd385852006-09-25 15:56:37 -0700436 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
437 * @skb: the NETLINK buffer
438 * @cb: the NETLINK callback
439 *
440 * Description:
441 * Process a user generated PROTOCOLS message and respond accordingly.
442 *
443 */
444static int netlbl_mgmt_protocols(struct sk_buff *skb,
445 struct netlink_callback *cb)
446{
447 u32 protos_sent = cb->args[0];
448
449 if (protos_sent == 0) {
450 if (netlbl_mgmt_protocols_cb(skb,
451 cb,
452 NETLBL_NLTYPE_UNLABELED) < 0)
453 goto protocols_return;
454 protos_sent++;
455 }
456 if (protos_sent == 1) {
457 if (netlbl_mgmt_protocols_cb(skb,
458 cb,
459 NETLBL_NLTYPE_CIPSOV4) < 0)
460 goto protocols_return;
461 protos_sent++;
462 }
463
464protocols_return:
465 cb->args[0] = protos_sent;
466 return skb->len;
467}
468
469/**
Paul Moored15c3452006-08-03 16:48:37 -0700470 * netlbl_mgmt_version - Handle a VERSION message
471 * @skb: the NETLINK buffer
472 * @info: the Generic NETLINK info block
473 *
474 * Description:
475 * Process a user generated VERSION message and respond accordingly. Returns
476 * zero on success, negative values on failure.
477 *
478 */
479static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
480{
481 int ret_val = -ENOMEM;
482 struct sk_buff *ans_skb = NULL;
Paul Moorefd385852006-09-25 15:56:37 -0700483 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700484
Thomas Graf339bf982006-11-10 14:10:15 -0800485 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700486 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700487 return -ENOMEM;
Thomas Graf17c157c2006-11-14 19:46:02 -0800488 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
489 0, NLBL_MGMT_C_VERSION);
Paul Moorefd385852006-09-25 15:56:37 -0700490 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700491 goto version_failure;
492
Paul Moorefd385852006-09-25 15:56:37 -0700493 ret_val = nla_put_u32(ans_skb,
494 NLBL_MGMT_A_VERSION,
495 NETLBL_PROTO_VERSION);
Paul Moored15c3452006-08-03 16:48:37 -0700496 if (ret_val != 0)
497 goto version_failure;
498
Paul Moorefd385852006-09-25 15:56:37 -0700499 genlmsg_end(ans_skb, data);
500
Thomas Graf81878d22006-11-14 19:45:27 -0800501 ret_val = genlmsg_reply(ans_skb, info);
Paul Moored15c3452006-08-03 16:48:37 -0700502 if (ret_val != 0)
503 goto version_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700504 return 0;
505
506version_failure:
507 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700508 return ret_val;
509}
510
511
512/*
513 * NetLabel Generic NETLINK Command Definitions
514 */
515
516static struct genl_ops netlbl_mgmt_genl_c_add = {
517 .cmd = NLBL_MGMT_C_ADD,
Paul Moorefd385852006-09-25 15:56:37 -0700518 .flags = GENL_ADMIN_PERM,
519 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700520 .doit = netlbl_mgmt_add,
521 .dumpit = NULL,
522};
523
524static struct genl_ops netlbl_mgmt_genl_c_remove = {
525 .cmd = NLBL_MGMT_C_REMOVE,
Paul Moorefd385852006-09-25 15:56:37 -0700526 .flags = GENL_ADMIN_PERM,
527 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700528 .doit = netlbl_mgmt_remove,
529 .dumpit = NULL,
530};
531
Paul Moorefd385852006-09-25 15:56:37 -0700532static struct genl_ops netlbl_mgmt_genl_c_listall = {
533 .cmd = NLBL_MGMT_C_LISTALL,
Paul Moored15c3452006-08-03 16:48:37 -0700534 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700535 .policy = netlbl_mgmt_genl_policy,
536 .doit = NULL,
537 .dumpit = netlbl_mgmt_listall,
Paul Moored15c3452006-08-03 16:48:37 -0700538};
539
540static struct genl_ops netlbl_mgmt_genl_c_adddef = {
541 .cmd = NLBL_MGMT_C_ADDDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700542 .flags = GENL_ADMIN_PERM,
543 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700544 .doit = netlbl_mgmt_adddef,
545 .dumpit = NULL,
546};
547
548static struct genl_ops netlbl_mgmt_genl_c_removedef = {
549 .cmd = NLBL_MGMT_C_REMOVEDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700550 .flags = GENL_ADMIN_PERM,
551 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700552 .doit = netlbl_mgmt_removedef,
553 .dumpit = NULL,
554};
555
556static struct genl_ops netlbl_mgmt_genl_c_listdef = {
557 .cmd = NLBL_MGMT_C_LISTDEF,
558 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700559 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700560 .doit = netlbl_mgmt_listdef,
561 .dumpit = NULL,
562};
563
Paul Moorefd385852006-09-25 15:56:37 -0700564static struct genl_ops netlbl_mgmt_genl_c_protocols = {
565 .cmd = NLBL_MGMT_C_PROTOCOLS,
Paul Moored15c3452006-08-03 16:48:37 -0700566 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700567 .policy = netlbl_mgmt_genl_policy,
568 .doit = NULL,
569 .dumpit = netlbl_mgmt_protocols,
Paul Moored15c3452006-08-03 16:48:37 -0700570};
571
572static struct genl_ops netlbl_mgmt_genl_c_version = {
573 .cmd = NLBL_MGMT_C_VERSION,
574 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700575 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700576 .doit = netlbl_mgmt_version,
577 .dumpit = NULL,
578};
579
580/*
581 * NetLabel Generic NETLINK Protocol Functions
582 */
583
584/**
585 * netlbl_mgmt_genl_init - Register the NetLabel management component
586 *
587 * Description:
588 * Register the NetLabel management component with the Generic NETLINK
589 * mechanism. Returns zero on success, negative values on failure.
590 *
591 */
592int netlbl_mgmt_genl_init(void)
593{
594 int ret_val;
595
596 ret_val = genl_register_family(&netlbl_mgmt_gnl_family);
597 if (ret_val != 0)
598 return ret_val;
599
600 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
601 &netlbl_mgmt_genl_c_add);
602 if (ret_val != 0)
603 return ret_val;
604 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
605 &netlbl_mgmt_genl_c_remove);
606 if (ret_val != 0)
607 return ret_val;
608 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
Paul Moorefd385852006-09-25 15:56:37 -0700609 &netlbl_mgmt_genl_c_listall);
Paul Moored15c3452006-08-03 16:48:37 -0700610 if (ret_val != 0)
611 return ret_val;
612 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
613 &netlbl_mgmt_genl_c_adddef);
614 if (ret_val != 0)
615 return ret_val;
616 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
617 &netlbl_mgmt_genl_c_removedef);
618 if (ret_val != 0)
619 return ret_val;
620 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
621 &netlbl_mgmt_genl_c_listdef);
622 if (ret_val != 0)
623 return ret_val;
624 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
Paul Moorefd385852006-09-25 15:56:37 -0700625 &netlbl_mgmt_genl_c_protocols);
Paul Moored15c3452006-08-03 16:48:37 -0700626 if (ret_val != 0)
627 return ret_val;
628 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
629 &netlbl_mgmt_genl_c_version);
630 if (ret_val != 0)
631 return ret_val;
632
633 return 0;
634}