]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/pohmelfs/config.c
Staging: Use kmemdup
[net-next-2.6.git] / drivers / staging / pohmelfs / config.c
CommitLineData
9ce8b619
EP
1/*
2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/kernel.h>
17#include <linux/connector.h>
18#include <linux/crypto.h>
19#include <linux/list.h>
20#include <linux/mutex.h>
21#include <linux/string.h>
22#include <linux/in.h>
5a0e3ad6 23#include <linux/slab.h>
9ce8b619
EP
24
25#include "netfs.h"
26
27/*
28 * Global configuration list.
29 * Each client can be asked to get one of them.
30 *
31 * Allows to provide remote server address (ipv4/v6/whatever), port
32 * and so on via kernel connector.
33 */
34
35static struct cb_id pohmelfs_cn_id = {.idx = POHMELFS_CN_IDX, .val = POHMELFS_CN_VAL};
36static LIST_HEAD(pohmelfs_config_list);
37static DEFINE_MUTEX(pohmelfs_config_lock);
38
39static inline int pohmelfs_config_eql(struct pohmelfs_ctl *sc, struct pohmelfs_ctl *ctl)
40{
41 if (sc->idx == ctl->idx && sc->type == ctl->type &&
42 sc->proto == ctl->proto &&
43 sc->addrlen == ctl->addrlen &&
44 !memcmp(&sc->addr, &ctl->addr, ctl->addrlen))
45 return 1;
46
47 return 0;
48}
49
50static struct pohmelfs_config_group *pohmelfs_find_config_group(unsigned int idx)
51{
52 struct pohmelfs_config_group *g, *group = NULL;
53
54 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
55 if (g->idx == idx) {
56 group = g;
57 break;
58 }
59 }
60
61 return group;
62}
63
64static struct pohmelfs_config_group *pohmelfs_find_create_config_group(unsigned int idx)
65{
66 struct pohmelfs_config_group *g;
67
68 g = pohmelfs_find_config_group(idx);
69 if (g)
70 return g;
71
72 g = kzalloc(sizeof(struct pohmelfs_config_group), GFP_KERNEL);
73 if (!g)
74 return NULL;
75
76 INIT_LIST_HEAD(&g->config_list);
77 g->idx = idx;
78 g->num_entry = 0;
79
80 list_add_tail(&g->group_entry, &pohmelfs_config_list);
81
82 return g;
83}
84
e0ca8739
EP
85static inline void pohmelfs_insert_config_entry(struct pohmelfs_sb *psb, struct pohmelfs_config *dst)
86{
87 struct pohmelfs_config *tmp;
88
89 INIT_LIST_HEAD(&dst->config_entry);
90
91 list_for_each_entry(tmp, &psb->state_list, config_entry) {
92 if (dst->state.ctl.prio > tmp->state.ctl.prio)
93 list_add_tail(&dst->config_entry, &tmp->config_entry);
94 }
95 if (list_empty(&dst->config_entry))
96 list_add_tail(&dst->config_entry, &psb->state_list);
97}
98
99static int pohmelfs_move_config_entry(struct pohmelfs_sb *psb,
100 struct pohmelfs_config *dst, struct pohmelfs_config *new)
101{
102 if ((dst->state.ctl.prio == new->state.ctl.prio) &&
103 (dst->state.ctl.perm == new->state.ctl.perm))
104 return 0;
105
106 dprintk("%s: dst: prio: %d, perm: %x, new: prio: %d, perm: %d.\n",
107 __func__, dst->state.ctl.prio, dst->state.ctl.perm,
108 new->state.ctl.prio, new->state.ctl.perm);
109 dst->state.ctl.prio = new->state.ctl.prio;
110 dst->state.ctl.perm = new->state.ctl.perm;
111
112 list_del_init(&dst->config_entry);
113 pohmelfs_insert_config_entry(psb, dst);
114 return 0;
115}
116
117/*
118 * pohmelfs_copy_config() is used to copy new state configs from the
119 * config group (controlled by the netlink messages) into the superblock.
120 * This happens either at startup time where no transactions can access
121 * the list of the configs (and thus list of the network states), or at
122 * run-time, where it is protected by the psb->state_lock.
123 */
9ce8b619
EP
124int pohmelfs_copy_config(struct pohmelfs_sb *psb)
125{
126 struct pohmelfs_config_group *g;
127 struct pohmelfs_config *c, *dst;
128 int err = -ENODEV;
129
130 mutex_lock(&pohmelfs_config_lock);
131
132 g = pohmelfs_find_config_group(psb->idx);
133 if (!g)
134 goto out_unlock;
135
136 /*
137 * Run over all entries in given config group and try to crate and
138 * initialize those, which do not exist in superblock list.
139 * Skip all existing entries.
140 */
141
142 list_for_each_entry(c, &g->config_list, config_entry) {
143 err = 0;
144 list_for_each_entry(dst, &psb->state_list, config_entry) {
145 if (pohmelfs_config_eql(&dst->state.ctl, &c->state.ctl)) {
e0ca8739
EP
146 err = pohmelfs_move_config_entry(psb, dst, c);
147 if (!err)
148 err = -EEXIST;
9ce8b619
EP
149 break;
150 }
151 }
152
153 if (err)
154 continue;
155
156 dst = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
157 if (!dst) {
158 err = -ENOMEM;
159 break;
160 }
161
162 memcpy(&dst->state.ctl, &c->state.ctl, sizeof(struct pohmelfs_ctl));
163
e0ca8739 164 pohmelfs_insert_config_entry(psb, dst);
9ce8b619
EP
165
166 err = pohmelfs_state_init_one(psb, dst);
167 if (err) {
168 list_del(&dst->config_entry);
169 kfree(dst);
170 }
171
172 err = 0;
173 }
174
175out_unlock:
176 mutex_unlock(&pohmelfs_config_lock);
177
178 return err;
179}
180
181int pohmelfs_copy_crypto(struct pohmelfs_sb *psb)
182{
183 struct pohmelfs_config_group *g;
184 int err = -ENOENT;
185
186 mutex_lock(&pohmelfs_config_lock);
187 g = pohmelfs_find_config_group(psb->idx);
188 if (!g)
189 goto err_out_exit;
190
191 if (g->hash_string) {
192 err = -ENOMEM;
193 psb->hash_string = kstrdup(g->hash_string, GFP_KERNEL);
194 if (!psb->hash_string)
195 goto err_out_exit;
196 psb->hash_strlen = g->hash_strlen;
197 }
198
199 if (g->cipher_string) {
200 psb->cipher_string = kstrdup(g->cipher_string, GFP_KERNEL);
201 if (!psb->cipher_string)
202 goto err_out_free_hash_string;
203 psb->cipher_strlen = g->cipher_strlen;
204 }
205
206 if (g->hash_keysize) {
94002c07
JL
207 psb->hash_key = kmemdup(g->hash_key, g->hash_keysize,
208 GFP_KERNEL);
9ce8b619
EP
209 if (!psb->hash_key)
210 goto err_out_free_cipher_string;
9ce8b619
EP
211 psb->hash_keysize = g->hash_keysize;
212 }
213
214 if (g->cipher_keysize) {
94002c07
JL
215 psb->cipher_key = kmemdup(g->cipher_key, g->cipher_keysize,
216 GFP_KERNEL);
9ce8b619
EP
217 if (!psb->cipher_key)
218 goto err_out_free_hash;
9ce8b619
EP
219 psb->cipher_keysize = g->cipher_keysize;
220 }
221
222 mutex_unlock(&pohmelfs_config_lock);
223
224 return 0;
225
226err_out_free_hash:
227 kfree(psb->hash_key);
228err_out_free_cipher_string:
229 kfree(psb->cipher_string);
230err_out_free_hash_string:
231 kfree(psb->hash_string);
232err_out_exit:
233 mutex_unlock(&pohmelfs_config_lock);
234 return err;
235}
236
237static int pohmelfs_send_reply(int err, int msg_num, int action, struct cn_msg *msg, struct pohmelfs_ctl *ctl)
238{
239 struct pohmelfs_cn_ack *ack;
240
7a6cb0d5 241 ack = kzalloc(sizeof(struct pohmelfs_cn_ack), GFP_KERNEL);
9ce8b619
EP
242 if (!ack)
243 return -ENOMEM;
244
9ce8b619
EP
245 memcpy(&ack->msg, msg, sizeof(struct cn_msg));
246
247 if (action == POHMELFS_CTLINFO_ACK)
248 memcpy(&ack->ctl, ctl, sizeof(struct pohmelfs_ctl));
249
250 ack->msg.len = sizeof(struct pohmelfs_cn_ack) - sizeof(struct cn_msg);
251 ack->msg.ack = msg->ack + 1;
252 ack->error = err;
253 ack->msg_num = msg_num;
254
255 cn_netlink_send(&ack->msg, 0, GFP_KERNEL);
256 kfree(ack);
257 return 0;
258}
259
260static int pohmelfs_cn_disp(struct cn_msg *msg)
261{
262 struct pohmelfs_config_group *g;
263 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
264 struct pohmelfs_config *c, *tmp;
265 int err = 0, i = 1;
266
267 if (msg->len != sizeof(struct pohmelfs_ctl))
268 return -EBADMSG;
269
270 mutex_lock(&pohmelfs_config_lock);
271
272 g = pohmelfs_find_config_group(ctl->idx);
273 if (!g) {
274 pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL);
275 goto out_unlock;
276 }
277
278 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
279 struct pohmelfs_ctl *sc = &c->state.ctl;
280 if (pohmelfs_send_reply(err, g->num_entry - i, POHMELFS_CTLINFO_ACK, msg, sc)) {
281 err = -ENOMEM;
282 goto out_unlock;
283 }
284 i += 1;
285 }
286
2d7cf8ef
EP
287 out_unlock:
288 mutex_unlock(&pohmelfs_config_lock);
289 return err;
290}
291
292static int pohmelfs_cn_dump(struct cn_msg *msg)
293{
294 struct pohmelfs_config_group *g;
295 struct pohmelfs_config *c, *tmp;
296 int err = 0, i = 1;
297 int total_msg = 0;
298
299 if (msg->len != sizeof(struct pohmelfs_ctl))
300 return -EBADMSG;
301
302 mutex_lock(&pohmelfs_config_lock);
303
304 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
305 if (g)
306 total_msg += g->num_entry;
307 }
308 if (total_msg == 0) {
309 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
310 err = -ENOMEM;
311 goto out_unlock;
312 }
313
314 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
315 if (g) {
316 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
317 struct pohmelfs_ctl *sc = &c->state.ctl;
318 if (pohmelfs_send_reply(err, total_msg - i, POHMELFS_CTLINFO_ACK, msg, sc)) {
319 err = -ENOMEM;
320 goto out_unlock;
321 }
322 i += 1;
323 }
324 }
325 }
326
9ce8b619 327out_unlock:
2d7cf8ef
EP
328 mutex_unlock(&pohmelfs_config_lock);
329 return err;
330}
331
332static int pohmelfs_cn_flush(struct cn_msg *msg)
333{
334 struct pohmelfs_config_group *g;
335 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
336 struct pohmelfs_config *c, *tmp;
337 int err = 0;
338
339 if (msg->len != sizeof(struct pohmelfs_ctl))
340 return -EBADMSG;
341
342 mutex_lock(&pohmelfs_config_lock);
343
344 if (ctl->idx != POHMELFS_NULL_IDX) {
345 g = pohmelfs_find_config_group(ctl->idx);
346
347 if (!g)
348 goto out_unlock;
349
350 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
351 list_del(&c->config_entry);
352 g->num_entry--;
353 kfree(c);
354 }
355 } else {
356 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
357 if (g) {
358 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
359 list_del(&c->config_entry);
360 g->num_entry--;
361 kfree(c);
362 }
363 }
364 }
365 }
366
367out_unlock:
368 mutex_unlock(&pohmelfs_config_lock);
369 pohmelfs_cn_dump(msg);
370
371 return err;
9ce8b619
EP
372}
373
e0ca8739
EP
374static int pohmelfs_modify_config(struct pohmelfs_ctl *old, struct pohmelfs_ctl *new)
375{
376 old->perm = new->perm;
377 old->prio = new->prio;
378 return 0;
379}
380
9ce8b619
EP
381static int pohmelfs_cn_ctl(struct cn_msg *msg, int action)
382{
383 struct pohmelfs_config_group *g;
384 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
385 struct pohmelfs_config *c, *tmp;
386 int err = 0;
387
388 if (msg->len != sizeof(struct pohmelfs_ctl))
389 return -EBADMSG;
390
391 mutex_lock(&pohmelfs_config_lock);
392
393 g = pohmelfs_find_create_config_group(ctl->idx);
394 if (!g) {
395 err = -ENOMEM;
396 goto out_unlock;
397 }
398
399 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
400 struct pohmelfs_ctl *sc = &c->state.ctl;
401
402 if (pohmelfs_config_eql(sc, ctl)) {
403 if (action == POHMELFS_FLAGS_ADD) {
404 err = -EEXIST;
405 goto out_unlock;
406 } else if (action == POHMELFS_FLAGS_DEL) {
407 list_del(&c->config_entry);
408 g->num_entry--;
409 kfree(c);
410 goto out_unlock;
e0ca8739
EP
411 } else if (action == POHMELFS_FLAGS_MODIFY) {
412 err = pohmelfs_modify_config(sc, ctl);
413 goto out_unlock;
9ce8b619
EP
414 } else {
415 err = -EEXIST;
416 goto out_unlock;
417 }
418 }
419 }
420 if (action == POHMELFS_FLAGS_DEL) {
421 err = -EBADMSG;
422 goto out_unlock;
423 }
424
425 c = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
426 if (!c) {
427 err = -ENOMEM;
428 goto out_unlock;
429 }
430 memcpy(&c->state.ctl, ctl, sizeof(struct pohmelfs_ctl));
431 g->num_entry++;
e0ca8739 432
9ce8b619
EP
433 list_add_tail(&c->config_entry, &g->config_list);
434
2d7cf8ef 435 out_unlock:
9ce8b619
EP
436 mutex_unlock(&pohmelfs_config_lock);
437 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
438 err = -ENOMEM;
439
440 return err;
441}
442
443static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
444{
445 char *algo = (char *)c->data;
446 u8 *key = (u8 *)(algo + c->strlen);
447
448 if (g->hash_string)
449 return -EEXIST;
450
451 g->hash_string = kstrdup(algo, GFP_KERNEL);
452 if (!g->hash_string)
453 return -ENOMEM;
454 g->hash_strlen = c->strlen;
455 g->hash_keysize = c->keysize;
456
94002c07 457 g->hash_key = kmemdup(key, c->keysize, GFP_KERNEL);
9ce8b619
EP
458 if (!g->hash_key) {
459 kfree(g->hash_string);
460 return -ENOMEM;
461 }
462
9ce8b619
EP
463 return 0;
464}
465
466static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
467{
468 char *algo = (char *)c->data;
469 u8 *key = (u8 *)(algo + c->strlen);
470
471 if (g->cipher_string)
472 return -EEXIST;
473
474 g->cipher_string = kstrdup(algo, GFP_KERNEL);
475 if (!g->cipher_string)
476 return -ENOMEM;
477 g->cipher_strlen = c->strlen;
478 g->cipher_keysize = c->keysize;
479
94002c07 480 g->cipher_key = kmemdup(key, c->keysize, GFP_KERNEL);
9ce8b619
EP
481 if (!g->cipher_key) {
482 kfree(g->cipher_string);
483 return -ENOMEM;
484 }
485
9ce8b619
EP
486 return 0;
487}
488
9ce8b619
EP
489static int pohmelfs_cn_crypto(struct cn_msg *msg)
490{
491 struct pohmelfs_crypto *crypto = (struct pohmelfs_crypto *)msg->data;
492 struct pohmelfs_config_group *g;
493 int err = 0;
494
495 dprintk("%s: idx: %u, strlen: %u, type: %u, keysize: %u, algo: %s.\n",
496 __func__, crypto->idx, crypto->strlen, crypto->type,
497 crypto->keysize, (char *)crypto->data);
498
499 mutex_lock(&pohmelfs_config_lock);
500 g = pohmelfs_find_create_config_group(crypto->idx);
501 if (!g) {
502 err = -ENOMEM;
503 goto out_unlock;
504 }
505
506 switch (crypto->type) {
5ac7af89 507 case POHMELFS_CRYPTO_HASH:
9ce8b619
EP
508 err = pohmelfs_crypto_hash_init(g, crypto);
509 break;
5ac7af89 510 case POHMELFS_CRYPTO_CIPHER:
9ce8b619
EP
511 err = pohmelfs_crypto_cipher_init(g, crypto);
512 break;
5ac7af89 513 default:
9ce8b619
EP
514 err = -ENOTSUPP;
515 break;
516 }
517
518out_unlock:
519 mutex_unlock(&pohmelfs_config_lock);
520 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
521 err = -ENOMEM;
522
523 return err;
524}
525
7069331d 526static void pohmelfs_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
9ce8b619 527{
9ce8b619
EP
528 int err;
529
98a5783a
PR
530 if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN))
531 return;
532
9ce8b619 533 switch (msg->flags) {
5ac7af89
RP
534 case POHMELFS_FLAGS_ADD:
535 case POHMELFS_FLAGS_DEL:
536 case POHMELFS_FLAGS_MODIFY:
e0ca8739 537 err = pohmelfs_cn_ctl(msg, msg->flags);
9ce8b619 538 break;
5ac7af89 539 case POHMELFS_FLAGS_FLUSH:
2d7cf8ef
EP
540 err = pohmelfs_cn_flush(msg);
541 break;
5ac7af89 542 case POHMELFS_FLAGS_SHOW:
9ce8b619
EP
543 err = pohmelfs_cn_disp(msg);
544 break;
5ac7af89 545 case POHMELFS_FLAGS_DUMP:
2d7cf8ef
EP
546 err = pohmelfs_cn_dump(msg);
547 break;
5ac7af89 548 case POHMELFS_FLAGS_CRYPTO:
9ce8b619
EP
549 err = pohmelfs_cn_crypto(msg);
550 break;
5ac7af89 551 default:
9ce8b619
EP
552 err = -ENOSYS;
553 break;
554 }
555}
556
557int pohmelfs_config_check(struct pohmelfs_config *config, int idx)
558{
559 struct pohmelfs_ctl *ctl = &config->state.ctl;
560 struct pohmelfs_config *tmp;
561 int err = -ENOENT;
562 struct pohmelfs_ctl *sc;
563 struct pohmelfs_config_group *g;
564
565 mutex_lock(&pohmelfs_config_lock);
566
567 g = pohmelfs_find_config_group(ctl->idx);
568 if (g) {
569 list_for_each_entry(tmp, &g->config_list, config_entry) {
570 sc = &tmp->state.ctl;
571
572 if (pohmelfs_config_eql(sc, ctl)) {
573 err = 0;
574 break;
575 }
576 }
577 }
578
579 mutex_unlock(&pohmelfs_config_lock);
580
581 return err;
582}
583
584int __init pohmelfs_config_init(void)
585{
2d7cf8ef
EP
586 /* XXX remove (void *) cast when vanilla connector got synced */
587 return cn_add_callback(&pohmelfs_cn_id, "pohmelfs", (void *)pohmelfs_cn_callback);
9ce8b619
EP
588}
589
590void pohmelfs_config_exit(void)
591{
592 struct pohmelfs_config *c, *tmp;
593 struct pohmelfs_config_group *g, *gtmp;
594
595 cn_del_callback(&pohmelfs_cn_id);
596
597 mutex_lock(&pohmelfs_config_lock);
598 list_for_each_entry_safe(g, gtmp, &pohmelfs_config_list, group_entry) {
599 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
600 list_del(&c->config_entry);
601 kfree(c);
602 }
603
604 list_del(&g->group_entry);
605
606 if (g->hash_string)
607 kfree(g->hash_string);
608
609 if (g->cipher_string)
610 kfree(g->cipher_string);
611
612 kfree(g);
613 }
614 mutex_unlock(&pohmelfs_config_lock);
615}