blob: 0ac314f18ad146296b713da535202b54563e36ca [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 */
62static struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
63 [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 Moored15c3452006-08-03 16:48:37 -070090
Paul Moorefd385852006-09-25 15:56:37 -070091 if (!info->attrs[NLBL_MGMT_A_DOMAIN] ||
92 !info->attrs[NLBL_MGMT_A_PROTOCOL])
93 goto add_failure;
94
95 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
96 if (entry == NULL) {
97 ret_val = -ENOMEM;
98 goto add_failure;
99 }
100 tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]);
101 entry->domain = kmalloc(tmp_size, GFP_KERNEL);
102 if (entry->domain == NULL) {
103 ret_val = -ENOMEM;
104 goto add_failure;
105 }
106 entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
107 nla_strlcpy(entry->domain, info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
108
109 switch (entry->type) {
110 case NETLBL_NLTYPE_UNLABELED:
Paul Moore32f50cd2006-09-28 14:51:47 -0700111 ret_val = netlbl_domhsh_add(entry, NETLINK_CB(skb).sid);
Paul Moorefd385852006-09-25 15:56:37 -0700112 break;
113 case NETLBL_NLTYPE_CIPSOV4:
114 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
115 goto add_failure;
116
117 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
118 /* We should be holding a rcu_read_lock() here while we hold
119 * the result but since the entry will always be deleted when
120 * the CIPSO DOI is deleted we aren't going to keep the
121 * lock. */
122 rcu_read_lock();
123 entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
124 if (entry->type_def.cipsov4 == NULL) {
125 rcu_read_unlock();
126 goto add_failure;
127 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700128 ret_val = netlbl_domhsh_add(entry, NETLINK_CB(skb).sid);
Paul Moorefd385852006-09-25 15:56:37 -0700129 rcu_read_unlock();
130 break;
131 default:
132 goto add_failure;
133 }
Paul Moored15c3452006-08-03 16:48:37 -0700134 if (ret_val != 0)
135 goto add_failure;
136
Paul Moored15c3452006-08-03 16:48:37 -0700137 return 0;
138
139add_failure:
140 if (entry)
141 kfree(entry->domain);
142 kfree(entry);
Paul Moored15c3452006-08-03 16:48:37 -0700143 return ret_val;
144}
145
146/**
147 * netlbl_mgmt_remove - Handle a REMOVE message
148 * @skb: the NETLINK buffer
149 * @info: the Generic NETLINK info block
150 *
151 * Description:
152 * Process a user generated REMOVE message and remove the specified domain
153 * mappings. Returns zero on success, negative values on failure.
154 *
155 */
156static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
157{
Paul Moorefd385852006-09-25 15:56:37 -0700158 char *domain;
Paul Moored15c3452006-08-03 16:48:37 -0700159
Paul Moorefd385852006-09-25 15:56:37 -0700160 if (!info->attrs[NLBL_MGMT_A_DOMAIN])
161 return -EINVAL;
162
163 domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
Paul Moore32f50cd2006-09-28 14:51:47 -0700164 return netlbl_domhsh_remove(domain, NETLINK_CB(skb).sid);
Paul Moorefd385852006-09-25 15:56:37 -0700165}
166
167/**
168 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
169 * @entry: the domain mapping hash table entry
170 * @arg: the netlbl_domhsh_walk_arg structure
171 *
172 * Description:
173 * This function is designed to be used as a callback to the
174 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
175 * message. Returns the size of the message on success, negative values on
176 * failure.
177 *
178 */
179static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
180{
181 int ret_val = -ENOMEM;
182 struct netlbl_domhsh_walk_arg *cb_arg = arg;
183 void *data;
184
185 data = netlbl_netlink_hdr_put(cb_arg->skb,
186 NETLINK_CB(cb_arg->nl_cb->skb).pid,
187 cb_arg->seq,
188 netlbl_mgmt_gnl_family.id,
189 NLM_F_MULTI,
190 NLBL_MGMT_C_LISTALL);
191 if (data == NULL)
192 goto listall_cb_failure;
193
194 ret_val = nla_put_string(cb_arg->skb,
195 NLBL_MGMT_A_DOMAIN,
196 entry->domain);
Paul Moored15c3452006-08-03 16:48:37 -0700197 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700198 goto listall_cb_failure;
199 ret_val = nla_put_u32(cb_arg->skb, NLBL_MGMT_A_PROTOCOL, entry->type);
200 if (ret_val != 0)
201 goto listall_cb_failure;
202 switch (entry->type) {
203 case NETLBL_NLTYPE_CIPSOV4:
204 ret_val = nla_put_u32(cb_arg->skb,
205 NLBL_MGMT_A_CV4DOI,
206 entry->type_def.cipsov4->doi);
Paul Moored15c3452006-08-03 16:48:37 -0700207 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700208 goto listall_cb_failure;
209 break;
Paul Moored15c3452006-08-03 16:48:37 -0700210 }
211
Paul Moorefd385852006-09-25 15:56:37 -0700212 cb_arg->seq++;
213 return genlmsg_end(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700214
Paul Moorefd385852006-09-25 15:56:37 -0700215listall_cb_failure:
216 genlmsg_cancel(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700217 return ret_val;
218}
219
220/**
Paul Moorefd385852006-09-25 15:56:37 -0700221 * netlbl_mgmt_listall - Handle a LISTALL message
Paul Moored15c3452006-08-03 16:48:37 -0700222 * @skb: the NETLINK buffer
Paul Moorefd385852006-09-25 15:56:37 -0700223 * @cb: the NETLINK callback
Paul Moored15c3452006-08-03 16:48:37 -0700224 *
225 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700226 * Process a user generated LISTALL message and dumps the domain hash table in
227 * a form suitable for use in a kernel generated LISTALL message. Returns zero
228 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700229 *
230 */
Paul Moorefd385852006-09-25 15:56:37 -0700231static int netlbl_mgmt_listall(struct sk_buff *skb,
232 struct netlink_callback *cb)
Paul Moored15c3452006-08-03 16:48:37 -0700233{
Paul Moorefd385852006-09-25 15:56:37 -0700234 struct netlbl_domhsh_walk_arg cb_arg;
235 u32 skip_bkt = cb->args[0];
236 u32 skip_chain = cb->args[1];
Paul Moored15c3452006-08-03 16:48:37 -0700237
Paul Moorefd385852006-09-25 15:56:37 -0700238 cb_arg.nl_cb = cb;
239 cb_arg.skb = skb;
240 cb_arg.seq = cb->nlh->nlmsg_seq;
Paul Moored15c3452006-08-03 16:48:37 -0700241
Paul Moorefd385852006-09-25 15:56:37 -0700242 netlbl_domhsh_walk(&skip_bkt,
243 &skip_chain,
244 netlbl_mgmt_listall_cb,
245 &cb_arg);
Paul Moored15c3452006-08-03 16:48:37 -0700246
Paul Moorefd385852006-09-25 15:56:37 -0700247 cb->args[0] = skip_bkt;
248 cb->args[1] = skip_chain;
249 return skb->len;
Paul Moored15c3452006-08-03 16:48:37 -0700250}
251
252/**
253 * netlbl_mgmt_adddef - Handle an ADDDEF message
254 * @skb: the NETLINK buffer
255 * @info: the Generic NETLINK info block
256 *
257 * Description:
258 * Process a user generated ADDDEF message and respond accordingly. Returns
259 * zero on success, negative values on failure.
260 *
261 */
262static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
263{
264 int ret_val = -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -0700265 struct netlbl_dom_map *entry = NULL;
266 u32 tmp_val;
267
Paul Moorefd385852006-09-25 15:56:37 -0700268 if (!info->attrs[NLBL_MGMT_A_PROTOCOL])
Paul Moored15c3452006-08-03 16:48:37 -0700269 goto adddef_failure;
270
Paul Moored15c3452006-08-03 16:48:37 -0700271 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
272 if (entry == NULL) {
273 ret_val = -ENOMEM;
274 goto adddef_failure;
275 }
Paul Moorefd385852006-09-25 15:56:37 -0700276 entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
Paul Moored15c3452006-08-03 16:48:37 -0700277
Paul Moored15c3452006-08-03 16:48:37 -0700278 switch (entry->type) {
279 case NETLBL_NLTYPE_UNLABELED:
Paul Moore32f50cd2006-09-28 14:51:47 -0700280 ret_val = netlbl_domhsh_add_default(entry,
281 NETLINK_CB(skb).sid);
Paul Moored15c3452006-08-03 16:48:37 -0700282 break;
283 case NETLBL_NLTYPE_CIPSOV4:
Paul Moorefd385852006-09-25 15:56:37 -0700284 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
Paul Moored15c3452006-08-03 16:48:37 -0700285 goto adddef_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700286
287 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
288 /* We should be holding a rcu_read_lock() here while we hold
289 * the result but since the entry will always be deleted when
290 * the CIPSO DOI is deleted we aren't going to keep the
291 * lock. */
Paul Moored15c3452006-08-03 16:48:37 -0700292 rcu_read_lock();
293 entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
294 if (entry->type_def.cipsov4 == NULL) {
295 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700296 goto adddef_failure;
297 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700298 ret_val = netlbl_domhsh_add_default(entry,
299 NETLINK_CB(skb).sid);
Paul Moored15c3452006-08-03 16:48:37 -0700300 rcu_read_unlock();
301 break;
302 default:
Paul Moorefd385852006-09-25 15:56:37 -0700303 goto adddef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700304 }
305 if (ret_val != 0)
306 goto adddef_failure;
307
Paul Moored15c3452006-08-03 16:48:37 -0700308 return 0;
309
310adddef_failure:
311 kfree(entry);
Paul Moored15c3452006-08-03 16:48:37 -0700312 return ret_val;
313}
314
315/**
316 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
317 * @skb: the NETLINK buffer
318 * @info: the Generic NETLINK info block
319 *
320 * Description:
321 * Process a user generated REMOVEDEF message and remove the default domain
322 * mapping. Returns zero on success, negative values on failure.
323 *
324 */
325static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
326{
Paul Moore32f50cd2006-09-28 14:51:47 -0700327 return netlbl_domhsh_remove_default(NETLINK_CB(skb).sid);
Paul Moored15c3452006-08-03 16:48:37 -0700328}
329
330/**
331 * netlbl_mgmt_listdef - Handle a LISTDEF message
332 * @skb: the NETLINK buffer
333 * @info: the Generic NETLINK info block
334 *
335 * Description:
336 * Process a user generated LISTDEF message and dumps the default domain
337 * mapping in a form suitable for use in a kernel generated LISTDEF message.
338 * Returns zero on success, negative values on failure.
339 *
340 */
341static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
342{
343 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700344 struct sk_buff *ans_skb = NULL;
345 void *data;
346 struct netlbl_dom_map *entry;
Paul Moored15c3452006-08-03 16:48:37 -0700347
Paul Moorefd385852006-09-25 15:56:37 -0700348 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700349 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700350 return -ENOMEM;
351 data = netlbl_netlink_hdr_put(ans_skb,
352 info->snd_pid,
353 info->snd_seq,
354 netlbl_mgmt_gnl_family.id,
355 0,
356 NLBL_MGMT_C_LISTDEF);
357 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700358 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700359
Paul Moorefd385852006-09-25 15:56:37 -0700360 rcu_read_lock();
361 entry = netlbl_domhsh_getentry(NULL);
362 if (entry == NULL) {
363 ret_val = -ENOENT;
364 goto listdef_failure_lock;
365 }
366 ret_val = nla_put_u32(ans_skb, NLBL_MGMT_A_PROTOCOL, entry->type);
367 if (ret_val != 0)
368 goto listdef_failure_lock;
369 switch (entry->type) {
370 case NETLBL_NLTYPE_CIPSOV4:
371 ret_val = nla_put_u32(ans_skb,
372 NLBL_MGMT_A_CV4DOI,
373 entry->type_def.cipsov4->doi);
374 if (ret_val != 0)
375 goto listdef_failure_lock;
376 break;
377 }
378 rcu_read_unlock();
379
380 genlmsg_end(ans_skb, data);
381
382 ret_val = genlmsg_unicast(ans_skb, info->snd_pid);
Paul Moored15c3452006-08-03 16:48:37 -0700383 if (ret_val != 0)
384 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700385 return 0;
386
Paul Moorefd385852006-09-25 15:56:37 -0700387listdef_failure_lock:
388 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700389listdef_failure:
Paul Moorefd385852006-09-25 15:56:37 -0700390 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700391 return ret_val;
392}
393
394/**
Paul Moorefd385852006-09-25 15:56:37 -0700395 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
396 * @skb: the skb to write to
397 * @seq: the NETLINK sequence number
398 * @cb: the NETLINK callback
399 * @protocol: the NetLabel protocol to use in the message
Paul Moored15c3452006-08-03 16:48:37 -0700400 *
401 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700402 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
403 * answer a application's PROTOCOLS message. Returns the size of the message
404 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700405 *
406 */
Paul Moorefd385852006-09-25 15:56:37 -0700407static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
408 struct netlink_callback *cb,
409 u32 protocol)
Paul Moored15c3452006-08-03 16:48:37 -0700410{
411 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700412 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700413
Paul Moorefd385852006-09-25 15:56:37 -0700414 data = netlbl_netlink_hdr_put(skb,
415 NETLINK_CB(cb->skb).pid,
416 cb->nlh->nlmsg_seq,
417 netlbl_mgmt_gnl_family.id,
418 NLM_F_MULTI,
419 NLBL_MGMT_C_PROTOCOLS);
420 if (data == NULL)
421 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700422
Paul Moorefd385852006-09-25 15:56:37 -0700423 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
Paul Moored15c3452006-08-03 16:48:37 -0700424 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700425 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700426
Paul Moorefd385852006-09-25 15:56:37 -0700427 return genlmsg_end(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700428
Paul Moorefd385852006-09-25 15:56:37 -0700429protocols_cb_failure:
430 genlmsg_cancel(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700431 return ret_val;
432}
433
434/**
Paul Moorefd385852006-09-25 15:56:37 -0700435 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
436 * @skb: the NETLINK buffer
437 * @cb: the NETLINK callback
438 *
439 * Description:
440 * Process a user generated PROTOCOLS message and respond accordingly.
441 *
442 */
443static int netlbl_mgmt_protocols(struct sk_buff *skb,
444 struct netlink_callback *cb)
445{
446 u32 protos_sent = cb->args[0];
447
448 if (protos_sent == 0) {
449 if (netlbl_mgmt_protocols_cb(skb,
450 cb,
451 NETLBL_NLTYPE_UNLABELED) < 0)
452 goto protocols_return;
453 protos_sent++;
454 }
455 if (protos_sent == 1) {
456 if (netlbl_mgmt_protocols_cb(skb,
457 cb,
458 NETLBL_NLTYPE_CIPSOV4) < 0)
459 goto protocols_return;
460 protos_sent++;
461 }
462
463protocols_return:
464 cb->args[0] = protos_sent;
465 return skb->len;
466}
467
468/**
Paul Moored15c3452006-08-03 16:48:37 -0700469 * netlbl_mgmt_version - Handle a VERSION message
470 * @skb: the NETLINK buffer
471 * @info: the Generic NETLINK info block
472 *
473 * Description:
474 * Process a user generated VERSION message and respond accordingly. Returns
475 * zero on success, negative values on failure.
476 *
477 */
478static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
479{
480 int ret_val = -ENOMEM;
481 struct sk_buff *ans_skb = NULL;
Paul Moorefd385852006-09-25 15:56:37 -0700482 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700483
Paul Moorefd385852006-09-25 15:56:37 -0700484 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700485 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700486 return -ENOMEM;
487 data = netlbl_netlink_hdr_put(ans_skb,
488 info->snd_pid,
489 info->snd_seq,
490 netlbl_mgmt_gnl_family.id,
491 0,
492 NLBL_MGMT_C_VERSION);
493 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700494 goto version_failure;
495
Paul Moorefd385852006-09-25 15:56:37 -0700496 ret_val = nla_put_u32(ans_skb,
497 NLBL_MGMT_A_VERSION,
498 NETLBL_PROTO_VERSION);
Paul Moored15c3452006-08-03 16:48:37 -0700499 if (ret_val != 0)
500 goto version_failure;
501
Paul Moorefd385852006-09-25 15:56:37 -0700502 genlmsg_end(ans_skb, data);
503
504 ret_val = genlmsg_unicast(ans_skb, info->snd_pid);
Paul Moored15c3452006-08-03 16:48:37 -0700505 if (ret_val != 0)
506 goto version_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700507 return 0;
508
509version_failure:
510 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700511 return ret_val;
512}
513
514
515/*
516 * NetLabel Generic NETLINK Command Definitions
517 */
518
519static struct genl_ops netlbl_mgmt_genl_c_add = {
520 .cmd = NLBL_MGMT_C_ADD,
Paul Moorefd385852006-09-25 15:56:37 -0700521 .flags = GENL_ADMIN_PERM,
522 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700523 .doit = netlbl_mgmt_add,
524 .dumpit = NULL,
525};
526
527static struct genl_ops netlbl_mgmt_genl_c_remove = {
528 .cmd = NLBL_MGMT_C_REMOVE,
Paul Moorefd385852006-09-25 15:56:37 -0700529 .flags = GENL_ADMIN_PERM,
530 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700531 .doit = netlbl_mgmt_remove,
532 .dumpit = NULL,
533};
534
Paul Moorefd385852006-09-25 15:56:37 -0700535static struct genl_ops netlbl_mgmt_genl_c_listall = {
536 .cmd = NLBL_MGMT_C_LISTALL,
Paul Moored15c3452006-08-03 16:48:37 -0700537 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700538 .policy = netlbl_mgmt_genl_policy,
539 .doit = NULL,
540 .dumpit = netlbl_mgmt_listall,
Paul Moored15c3452006-08-03 16:48:37 -0700541};
542
543static struct genl_ops netlbl_mgmt_genl_c_adddef = {
544 .cmd = NLBL_MGMT_C_ADDDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700545 .flags = GENL_ADMIN_PERM,
546 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700547 .doit = netlbl_mgmt_adddef,
548 .dumpit = NULL,
549};
550
551static struct genl_ops netlbl_mgmt_genl_c_removedef = {
552 .cmd = NLBL_MGMT_C_REMOVEDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700553 .flags = GENL_ADMIN_PERM,
554 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700555 .doit = netlbl_mgmt_removedef,
556 .dumpit = NULL,
557};
558
559static struct genl_ops netlbl_mgmt_genl_c_listdef = {
560 .cmd = NLBL_MGMT_C_LISTDEF,
561 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700562 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700563 .doit = netlbl_mgmt_listdef,
564 .dumpit = NULL,
565};
566
Paul Moorefd385852006-09-25 15:56:37 -0700567static struct genl_ops netlbl_mgmt_genl_c_protocols = {
568 .cmd = NLBL_MGMT_C_PROTOCOLS,
Paul Moored15c3452006-08-03 16:48:37 -0700569 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700570 .policy = netlbl_mgmt_genl_policy,
571 .doit = NULL,
572 .dumpit = netlbl_mgmt_protocols,
Paul Moored15c3452006-08-03 16:48:37 -0700573};
574
575static struct genl_ops netlbl_mgmt_genl_c_version = {
576 .cmd = NLBL_MGMT_C_VERSION,
577 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700578 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700579 .doit = netlbl_mgmt_version,
580 .dumpit = NULL,
581};
582
583/*
584 * NetLabel Generic NETLINK Protocol Functions
585 */
586
587/**
588 * netlbl_mgmt_genl_init - Register the NetLabel management component
589 *
590 * Description:
591 * Register the NetLabel management component with the Generic NETLINK
592 * mechanism. Returns zero on success, negative values on failure.
593 *
594 */
595int netlbl_mgmt_genl_init(void)
596{
597 int ret_val;
598
599 ret_val = genl_register_family(&netlbl_mgmt_gnl_family);
600 if (ret_val != 0)
601 return ret_val;
602
603 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
604 &netlbl_mgmt_genl_c_add);
605 if (ret_val != 0)
606 return ret_val;
607 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
608 &netlbl_mgmt_genl_c_remove);
609 if (ret_val != 0)
610 return ret_val;
611 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
Paul Moorefd385852006-09-25 15:56:37 -0700612 &netlbl_mgmt_genl_c_listall);
Paul Moored15c3452006-08-03 16:48:37 -0700613 if (ret_val != 0)
614 return ret_val;
615 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
616 &netlbl_mgmt_genl_c_adddef);
617 if (ret_val != 0)
618 return ret_val;
619 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
620 &netlbl_mgmt_genl_c_removedef);
621 if (ret_val != 0)
622 return ret_val;
623 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
624 &netlbl_mgmt_genl_c_listdef);
625 if (ret_val != 0)
626 return ret_val;
627 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
Paul Moorefd385852006-09-25 15:56:37 -0700628 &netlbl_mgmt_genl_c_protocols);
Paul Moored15c3452006-08-03 16:48:37 -0700629 if (ret_val != 0)
630 return ret_val;
631 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
632 &netlbl_mgmt_genl_c_version);
633 if (ret_val != 0)
634 return ret_val;
635
636 return 0;
637}