blob: 56483377997a0550a39e4ab56ea4cdb87910eea9 [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 Moore23bcdc12007-07-18 12:28:45 -040045/* NetLabel configured protocol count */
46static DEFINE_SPINLOCK(netlabel_mgmt_protocount_lock);
47static u32 netlabel_mgmt_protocount = 0;
48
Paul Moorefd385852006-09-25 15:56:37 -070049/* Argument struct for netlbl_domhsh_walk() */
50struct netlbl_domhsh_walk_arg {
51 struct netlink_callback *nl_cb;
52 struct sk_buff *skb;
53 u32 seq;
54};
55
Paul Moored15c3452006-08-03 16:48:37 -070056/* NetLabel Generic NETLINK CIPSOv4 family */
57static struct genl_family netlbl_mgmt_gnl_family = {
58 .id = GENL_ID_GENERATE,
59 .hdrsize = 0,
60 .name = NETLBL_NLTYPE_MGMT_NAME,
61 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -070062 .maxattr = NLBL_MGMT_A_MAX,
Paul Moored15c3452006-08-03 16:48:37 -070063};
64
Paul Moorefd385852006-09-25 15:56:37 -070065/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -070066static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -070067 [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};
Paul Moored15c3452006-08-03 16:48:37 -070072
73/*
Paul Moore23bcdc12007-07-18 12:28:45 -040074 * NetLabel Misc Managment Functions
75 */
76
77/**
78 * netlbl_mgmt_protocount_inc - Increment the configured labeled protocol count
79 *
80 * Description:
81 * Increment the number of labeled protocol configurations in the current
82 * NetLabel configuration. Keep track of this for use in determining if
83 * NetLabel label enforcement should be active/enabled or not in the LSM.
84 *
85 */
86void netlbl_mgmt_protocount_inc(void)
87{
Paul Moore23bcdc12007-07-18 12:28:45 -040088 spin_lock(&netlabel_mgmt_protocount_lock);
89 netlabel_mgmt_protocount++;
90 spin_unlock(&netlabel_mgmt_protocount_lock);
Paul Moore23bcdc12007-07-18 12:28:45 -040091}
92
93/**
94 * netlbl_mgmt_protocount_dec - Decrement the configured labeled protocol count
95 *
96 * Description:
97 * Decrement the number of labeled protocol configurations in the current
98 * NetLabel configuration. Keep track of this for use in determining if
99 * NetLabel label enforcement should be active/enabled or not in the LSM.
100 *
101 */
102void netlbl_mgmt_protocount_dec(void)
103{
Paul Moore23bcdc12007-07-18 12:28:45 -0400104 spin_lock(&netlabel_mgmt_protocount_lock);
105 if (netlabel_mgmt_protocount > 0)
106 netlabel_mgmt_protocount--;
107 spin_unlock(&netlabel_mgmt_protocount_lock);
Paul Moore23bcdc12007-07-18 12:28:45 -0400108}
109
110/**
111 * netlbl_mgmt_protocount_value - Return the number of configured protocols
112 *
113 * Description:
114 * Return the number of labeled protocols in the current NetLabel
115 * configuration. This value is useful in determining if NetLabel label
116 * enforcement should be active/enabled or not in the LSM.
117 *
118 */
119u32 netlbl_mgmt_protocount_value(void)
120{
121 u32 val;
122
123 rcu_read_lock();
124 val = netlabel_mgmt_protocount;
125 rcu_read_unlock();
126
127 return val;
128}
129
130/*
Paul Moored15c3452006-08-03 16:48:37 -0700131 * NetLabel Command Handlers
132 */
133
134/**
135 * netlbl_mgmt_add - Handle an ADD message
136 * @skb: the NETLINK buffer
137 * @info: the Generic NETLINK info block
138 *
139 * Description:
140 * Process a user generated ADD message and add the domains from the message
141 * to the hash table. See netlabel.h for a description of the message format.
142 * Returns zero on success, negative values on failure.
143 *
144 */
145static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
146{
147 int ret_val = -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -0700148 struct netlbl_dom_map *entry = NULL;
Paul Moorefd385852006-09-25 15:56:37 -0700149 size_t tmp_size;
Paul Moored15c3452006-08-03 16:48:37 -0700150 u32 tmp_val;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700151 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700152
Paul Moorefd385852006-09-25 15:56:37 -0700153 if (!info->attrs[NLBL_MGMT_A_DOMAIN] ||
154 !info->attrs[NLBL_MGMT_A_PROTOCOL])
155 goto add_failure;
156
Paul Moore95d4e6b2006-09-29 17:05:05 -0700157 netlbl_netlink_auditinfo(skb, &audit_info);
158
Paul Moorefd385852006-09-25 15:56:37 -0700159 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
160 if (entry == NULL) {
161 ret_val = -ENOMEM;
162 goto add_failure;
163 }
164 tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]);
165 entry->domain = kmalloc(tmp_size, GFP_KERNEL);
166 if (entry->domain == NULL) {
167 ret_val = -ENOMEM;
168 goto add_failure;
169 }
170 entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
171 nla_strlcpy(entry->domain, info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
172
173 switch (entry->type) {
174 case NETLBL_NLTYPE_UNLABELED:
Paul Moore95d4e6b2006-09-29 17:05:05 -0700175 ret_val = netlbl_domhsh_add(entry, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700176 break;
177 case NETLBL_NLTYPE_CIPSOV4:
178 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
179 goto add_failure;
180
181 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
182 /* We should be holding a rcu_read_lock() here while we hold
183 * the result but since the entry will always be deleted when
184 * the CIPSO DOI is deleted we aren't going to keep the
185 * lock. */
186 rcu_read_lock();
187 entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
188 if (entry->type_def.cipsov4 == NULL) {
189 rcu_read_unlock();
190 goto add_failure;
191 }
Paul Moore95d4e6b2006-09-29 17:05:05 -0700192 ret_val = netlbl_domhsh_add(entry, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700193 rcu_read_unlock();
194 break;
195 default:
196 goto add_failure;
197 }
Paul Moored15c3452006-08-03 16:48:37 -0700198 if (ret_val != 0)
199 goto add_failure;
200
Paul Moored15c3452006-08-03 16:48:37 -0700201 return 0;
202
203add_failure:
204 if (entry)
205 kfree(entry->domain);
206 kfree(entry);
Paul Moored15c3452006-08-03 16:48:37 -0700207 return ret_val;
208}
209
210/**
211 * netlbl_mgmt_remove - Handle a REMOVE message
212 * @skb: the NETLINK buffer
213 * @info: the Generic NETLINK info block
214 *
215 * Description:
216 * Process a user generated REMOVE message and remove the specified domain
217 * mappings. Returns zero on success, negative values on failure.
218 *
219 */
220static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
221{
Paul Moorefd385852006-09-25 15:56:37 -0700222 char *domain;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700223 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700224
Paul Moorefd385852006-09-25 15:56:37 -0700225 if (!info->attrs[NLBL_MGMT_A_DOMAIN])
226 return -EINVAL;
227
Paul Moore95d4e6b2006-09-29 17:05:05 -0700228 netlbl_netlink_auditinfo(skb, &audit_info);
229
Paul Moorefd385852006-09-25 15:56:37 -0700230 domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
Paul Moore95d4e6b2006-09-29 17:05:05 -0700231 return netlbl_domhsh_remove(domain, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700232}
233
234/**
235 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
236 * @entry: the domain mapping hash table entry
237 * @arg: the netlbl_domhsh_walk_arg structure
238 *
239 * Description:
240 * This function is designed to be used as a callback to the
241 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
242 * message. Returns the size of the message on success, negative values on
243 * failure.
244 *
245 */
246static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
247{
248 int ret_val = -ENOMEM;
249 struct netlbl_domhsh_walk_arg *cb_arg = arg;
250 void *data;
251
Thomas Graf17c157c2006-11-14 19:46:02 -0800252 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
253 cb_arg->seq, &netlbl_mgmt_gnl_family,
254 NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
Paul Moorefd385852006-09-25 15:56:37 -0700255 if (data == NULL)
256 goto listall_cb_failure;
257
258 ret_val = nla_put_string(cb_arg->skb,
259 NLBL_MGMT_A_DOMAIN,
260 entry->domain);
Paul Moored15c3452006-08-03 16:48:37 -0700261 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700262 goto listall_cb_failure;
263 ret_val = nla_put_u32(cb_arg->skb, NLBL_MGMT_A_PROTOCOL, entry->type);
264 if (ret_val != 0)
265 goto listall_cb_failure;
266 switch (entry->type) {
267 case NETLBL_NLTYPE_CIPSOV4:
268 ret_val = nla_put_u32(cb_arg->skb,
269 NLBL_MGMT_A_CV4DOI,
270 entry->type_def.cipsov4->doi);
Paul Moored15c3452006-08-03 16:48:37 -0700271 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700272 goto listall_cb_failure;
273 break;
Paul Moored15c3452006-08-03 16:48:37 -0700274 }
275
Paul Moorefd385852006-09-25 15:56:37 -0700276 cb_arg->seq++;
277 return genlmsg_end(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700278
Paul Moorefd385852006-09-25 15:56:37 -0700279listall_cb_failure:
280 genlmsg_cancel(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700281 return ret_val;
282}
283
284/**
Paul Moorefd385852006-09-25 15:56:37 -0700285 * netlbl_mgmt_listall - Handle a LISTALL message
Paul Moored15c3452006-08-03 16:48:37 -0700286 * @skb: the NETLINK buffer
Paul Moorefd385852006-09-25 15:56:37 -0700287 * @cb: the NETLINK callback
Paul Moored15c3452006-08-03 16:48:37 -0700288 *
289 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700290 * Process a user generated LISTALL message and dumps the domain hash table in
291 * a form suitable for use in a kernel generated LISTALL message. Returns zero
292 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700293 *
294 */
Paul Moorefd385852006-09-25 15:56:37 -0700295static int netlbl_mgmt_listall(struct sk_buff *skb,
296 struct netlink_callback *cb)
Paul Moored15c3452006-08-03 16:48:37 -0700297{
Paul Moorefd385852006-09-25 15:56:37 -0700298 struct netlbl_domhsh_walk_arg cb_arg;
299 u32 skip_bkt = cb->args[0];
300 u32 skip_chain = cb->args[1];
Paul Moored15c3452006-08-03 16:48:37 -0700301
Paul Moorefd385852006-09-25 15:56:37 -0700302 cb_arg.nl_cb = cb;
303 cb_arg.skb = skb;
304 cb_arg.seq = cb->nlh->nlmsg_seq;
Paul Moored15c3452006-08-03 16:48:37 -0700305
Paul Moorefd385852006-09-25 15:56:37 -0700306 netlbl_domhsh_walk(&skip_bkt,
307 &skip_chain,
308 netlbl_mgmt_listall_cb,
309 &cb_arg);
Paul Moored15c3452006-08-03 16:48:37 -0700310
Paul Moorefd385852006-09-25 15:56:37 -0700311 cb->args[0] = skip_bkt;
312 cb->args[1] = skip_chain;
313 return skb->len;
Paul Moored15c3452006-08-03 16:48:37 -0700314}
315
316/**
317 * netlbl_mgmt_adddef - Handle an ADDDEF message
318 * @skb: the NETLINK buffer
319 * @info: the Generic NETLINK info block
320 *
321 * Description:
322 * Process a user generated ADDDEF message and respond accordingly. Returns
323 * zero on success, negative values on failure.
324 *
325 */
326static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
327{
328 int ret_val = -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -0700329 struct netlbl_dom_map *entry = NULL;
330 u32 tmp_val;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700331 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700332
Paul Moorefd385852006-09-25 15:56:37 -0700333 if (!info->attrs[NLBL_MGMT_A_PROTOCOL])
Paul Moored15c3452006-08-03 16:48:37 -0700334 goto adddef_failure;
335
Paul Moore95d4e6b2006-09-29 17:05:05 -0700336 netlbl_netlink_auditinfo(skb, &audit_info);
337
Paul Moored15c3452006-08-03 16:48:37 -0700338 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
339 if (entry == NULL) {
340 ret_val = -ENOMEM;
341 goto adddef_failure;
342 }
Paul Moorefd385852006-09-25 15:56:37 -0700343 entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
Paul Moored15c3452006-08-03 16:48:37 -0700344
Paul Moored15c3452006-08-03 16:48:37 -0700345 switch (entry->type) {
346 case NETLBL_NLTYPE_UNLABELED:
Paul Moore95d4e6b2006-09-29 17:05:05 -0700347 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700348 break;
349 case NETLBL_NLTYPE_CIPSOV4:
Paul Moorefd385852006-09-25 15:56:37 -0700350 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
Paul Moored15c3452006-08-03 16:48:37 -0700351 goto adddef_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700352
353 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
354 /* We should be holding a rcu_read_lock() here while we hold
355 * the result but since the entry will always be deleted when
356 * the CIPSO DOI is deleted we aren't going to keep the
357 * lock. */
Paul Moored15c3452006-08-03 16:48:37 -0700358 rcu_read_lock();
359 entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
360 if (entry->type_def.cipsov4 == NULL) {
361 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700362 goto adddef_failure;
363 }
Paul Moore95d4e6b2006-09-29 17:05:05 -0700364 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700365 rcu_read_unlock();
366 break;
367 default:
Paul Moorefd385852006-09-25 15:56:37 -0700368 goto adddef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700369 }
370 if (ret_val != 0)
371 goto adddef_failure;
372
Paul Moored15c3452006-08-03 16:48:37 -0700373 return 0;
374
375adddef_failure:
376 kfree(entry);
Paul Moored15c3452006-08-03 16:48:37 -0700377 return ret_val;
378}
379
380/**
381 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
382 * @skb: the NETLINK buffer
383 * @info: the Generic NETLINK info block
384 *
385 * Description:
386 * Process a user generated REMOVEDEF message and remove the default domain
387 * mapping. Returns zero on success, negative values on failure.
388 *
389 */
390static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
391{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700392 struct netlbl_audit audit_info;
393
394 netlbl_netlink_auditinfo(skb, &audit_info);
395
396 return netlbl_domhsh_remove_default(&audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700397}
398
399/**
400 * netlbl_mgmt_listdef - Handle a LISTDEF message
401 * @skb: the NETLINK buffer
402 * @info: the Generic NETLINK info block
403 *
404 * Description:
405 * Process a user generated LISTDEF message and dumps the default domain
406 * mapping in a form suitable for use in a kernel generated LISTDEF message.
407 * Returns zero on success, negative values on failure.
408 *
409 */
410static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
411{
412 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700413 struct sk_buff *ans_skb = NULL;
414 void *data;
415 struct netlbl_dom_map *entry;
Paul Moored15c3452006-08-03 16:48:37 -0700416
Thomas Graf339bf982006-11-10 14:10:15 -0800417 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700418 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700419 return -ENOMEM;
Thomas Graf17c157c2006-11-14 19:46:02 -0800420 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
421 0, NLBL_MGMT_C_LISTDEF);
Paul Moorefd385852006-09-25 15:56:37 -0700422 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700423 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700424
Paul Moorefd385852006-09-25 15:56:37 -0700425 rcu_read_lock();
426 entry = netlbl_domhsh_getentry(NULL);
427 if (entry == NULL) {
428 ret_val = -ENOENT;
429 goto listdef_failure_lock;
430 }
431 ret_val = nla_put_u32(ans_skb, NLBL_MGMT_A_PROTOCOL, entry->type);
432 if (ret_val != 0)
433 goto listdef_failure_lock;
434 switch (entry->type) {
435 case NETLBL_NLTYPE_CIPSOV4:
436 ret_val = nla_put_u32(ans_skb,
437 NLBL_MGMT_A_CV4DOI,
438 entry->type_def.cipsov4->doi);
439 if (ret_val != 0)
440 goto listdef_failure_lock;
441 break;
442 }
443 rcu_read_unlock();
444
445 genlmsg_end(ans_skb, data);
446
Thomas Graf81878d22006-11-14 19:45:27 -0800447 ret_val = genlmsg_reply(ans_skb, info);
Paul Moored15c3452006-08-03 16:48:37 -0700448 if (ret_val != 0)
449 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700450 return 0;
451
Paul Moorefd385852006-09-25 15:56:37 -0700452listdef_failure_lock:
453 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700454listdef_failure:
Paul Moorefd385852006-09-25 15:56:37 -0700455 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700456 return ret_val;
457}
458
459/**
Paul Moorefd385852006-09-25 15:56:37 -0700460 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
461 * @skb: the skb to write to
462 * @seq: the NETLINK sequence number
463 * @cb: the NETLINK callback
464 * @protocol: the NetLabel protocol to use in the message
Paul Moored15c3452006-08-03 16:48:37 -0700465 *
466 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700467 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
468 * answer a application's PROTOCOLS message. Returns the size of the message
469 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700470 *
471 */
Paul Moorefd385852006-09-25 15:56:37 -0700472static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
473 struct netlink_callback *cb,
474 u32 protocol)
Paul Moored15c3452006-08-03 16:48:37 -0700475{
476 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700477 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700478
Thomas Graf17c157c2006-11-14 19:46:02 -0800479 data = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
480 &netlbl_mgmt_gnl_family, NLM_F_MULTI,
481 NLBL_MGMT_C_PROTOCOLS);
Paul Moorefd385852006-09-25 15:56:37 -0700482 if (data == NULL)
483 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700484
Paul Moorefd385852006-09-25 15:56:37 -0700485 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
Paul Moored15c3452006-08-03 16:48:37 -0700486 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700487 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700488
Paul Moorefd385852006-09-25 15:56:37 -0700489 return genlmsg_end(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700490
Paul Moorefd385852006-09-25 15:56:37 -0700491protocols_cb_failure:
492 genlmsg_cancel(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700493 return ret_val;
494}
495
496/**
Paul Moorefd385852006-09-25 15:56:37 -0700497 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
498 * @skb: the NETLINK buffer
499 * @cb: the NETLINK callback
500 *
501 * Description:
502 * Process a user generated PROTOCOLS message and respond accordingly.
503 *
504 */
505static int netlbl_mgmt_protocols(struct sk_buff *skb,
506 struct netlink_callback *cb)
507{
508 u32 protos_sent = cb->args[0];
509
510 if (protos_sent == 0) {
511 if (netlbl_mgmt_protocols_cb(skb,
512 cb,
513 NETLBL_NLTYPE_UNLABELED) < 0)
514 goto protocols_return;
515 protos_sent++;
516 }
517 if (protos_sent == 1) {
518 if (netlbl_mgmt_protocols_cb(skb,
519 cb,
520 NETLBL_NLTYPE_CIPSOV4) < 0)
521 goto protocols_return;
522 protos_sent++;
523 }
524
525protocols_return:
526 cb->args[0] = protos_sent;
527 return skb->len;
528}
529
530/**
Paul Moored15c3452006-08-03 16:48:37 -0700531 * netlbl_mgmt_version - Handle a VERSION message
532 * @skb: the NETLINK buffer
533 * @info: the Generic NETLINK info block
534 *
535 * Description:
536 * Process a user generated VERSION message and respond accordingly. Returns
537 * zero on success, negative values on failure.
538 *
539 */
540static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
541{
542 int ret_val = -ENOMEM;
543 struct sk_buff *ans_skb = NULL;
Paul Moorefd385852006-09-25 15:56:37 -0700544 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700545
Thomas Graf339bf982006-11-10 14:10:15 -0800546 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700547 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700548 return -ENOMEM;
Thomas Graf17c157c2006-11-14 19:46:02 -0800549 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
550 0, NLBL_MGMT_C_VERSION);
Paul Moorefd385852006-09-25 15:56:37 -0700551 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700552 goto version_failure;
553
Paul Moorefd385852006-09-25 15:56:37 -0700554 ret_val = nla_put_u32(ans_skb,
555 NLBL_MGMT_A_VERSION,
556 NETLBL_PROTO_VERSION);
Paul Moored15c3452006-08-03 16:48:37 -0700557 if (ret_val != 0)
558 goto version_failure;
559
Paul Moorefd385852006-09-25 15:56:37 -0700560 genlmsg_end(ans_skb, data);
561
Thomas Graf81878d22006-11-14 19:45:27 -0800562 ret_val = genlmsg_reply(ans_skb, info);
Paul Moored15c3452006-08-03 16:48:37 -0700563 if (ret_val != 0)
564 goto version_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700565 return 0;
566
567version_failure:
568 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700569 return ret_val;
570}
571
572
573/*
574 * NetLabel Generic NETLINK Command Definitions
575 */
576
577static struct genl_ops netlbl_mgmt_genl_c_add = {
578 .cmd = NLBL_MGMT_C_ADD,
Paul Moorefd385852006-09-25 15:56:37 -0700579 .flags = GENL_ADMIN_PERM,
580 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700581 .doit = netlbl_mgmt_add,
582 .dumpit = NULL,
583};
584
585static struct genl_ops netlbl_mgmt_genl_c_remove = {
586 .cmd = NLBL_MGMT_C_REMOVE,
Paul Moorefd385852006-09-25 15:56:37 -0700587 .flags = GENL_ADMIN_PERM,
588 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700589 .doit = netlbl_mgmt_remove,
590 .dumpit = NULL,
591};
592
Paul Moorefd385852006-09-25 15:56:37 -0700593static struct genl_ops netlbl_mgmt_genl_c_listall = {
594 .cmd = NLBL_MGMT_C_LISTALL,
Paul Moored15c3452006-08-03 16:48:37 -0700595 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700596 .policy = netlbl_mgmt_genl_policy,
597 .doit = NULL,
598 .dumpit = netlbl_mgmt_listall,
Paul Moored15c3452006-08-03 16:48:37 -0700599};
600
601static struct genl_ops netlbl_mgmt_genl_c_adddef = {
602 .cmd = NLBL_MGMT_C_ADDDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700603 .flags = GENL_ADMIN_PERM,
604 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700605 .doit = netlbl_mgmt_adddef,
606 .dumpit = NULL,
607};
608
609static struct genl_ops netlbl_mgmt_genl_c_removedef = {
610 .cmd = NLBL_MGMT_C_REMOVEDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700611 .flags = GENL_ADMIN_PERM,
612 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700613 .doit = netlbl_mgmt_removedef,
614 .dumpit = NULL,
615};
616
617static struct genl_ops netlbl_mgmt_genl_c_listdef = {
618 .cmd = NLBL_MGMT_C_LISTDEF,
619 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700620 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700621 .doit = netlbl_mgmt_listdef,
622 .dumpit = NULL,
623};
624
Paul Moorefd385852006-09-25 15:56:37 -0700625static struct genl_ops netlbl_mgmt_genl_c_protocols = {
626 .cmd = NLBL_MGMT_C_PROTOCOLS,
Paul Moored15c3452006-08-03 16:48:37 -0700627 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700628 .policy = netlbl_mgmt_genl_policy,
629 .doit = NULL,
630 .dumpit = netlbl_mgmt_protocols,
Paul Moored15c3452006-08-03 16:48:37 -0700631};
632
633static struct genl_ops netlbl_mgmt_genl_c_version = {
634 .cmd = NLBL_MGMT_C_VERSION,
635 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700636 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700637 .doit = netlbl_mgmt_version,
638 .dumpit = NULL,
639};
640
641/*
642 * NetLabel Generic NETLINK Protocol Functions
643 */
644
645/**
646 * netlbl_mgmt_genl_init - Register the NetLabel management component
647 *
648 * Description:
649 * Register the NetLabel management component with the Generic NETLINK
650 * mechanism. Returns zero on success, negative values on failure.
651 *
652 */
653int netlbl_mgmt_genl_init(void)
654{
655 int ret_val;
656
657 ret_val = genl_register_family(&netlbl_mgmt_gnl_family);
658 if (ret_val != 0)
659 return ret_val;
660
661 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
662 &netlbl_mgmt_genl_c_add);
663 if (ret_val != 0)
664 return ret_val;
665 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
666 &netlbl_mgmt_genl_c_remove);
667 if (ret_val != 0)
668 return ret_val;
669 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
Paul Moorefd385852006-09-25 15:56:37 -0700670 &netlbl_mgmt_genl_c_listall);
Paul Moored15c3452006-08-03 16:48:37 -0700671 if (ret_val != 0)
672 return ret_val;
673 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
674 &netlbl_mgmt_genl_c_adddef);
675 if (ret_val != 0)
676 return ret_val;
677 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
678 &netlbl_mgmt_genl_c_removedef);
679 if (ret_val != 0)
680 return ret_val;
681 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
682 &netlbl_mgmt_genl_c_listdef);
683 if (ret_val != 0)
684 return ret_val;
685 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
Paul Moorefd385852006-09-25 15:56:37 -0700686 &netlbl_mgmt_genl_c_protocols);
Paul Moored15c3452006-08-03 16:48:37 -0700687 if (ret_val != 0)
688 return ret_val;
689 ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
690 &netlbl_mgmt_genl_c_version);
691 if (ret_val != 0)
692 return ret_val;
693
694 return 0;
695}