include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.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) {
207 psb->hash_key = kmalloc(g->hash_keysize, GFP_KERNEL);
208 if (!psb->hash_key)
209 goto err_out_free_cipher_string;
210 memcpy(psb->hash_key, g->hash_key, g->hash_keysize);
211 psb->hash_keysize = g->hash_keysize;
212 }
213
214 if (g->cipher_keysize) {
215 psb->cipher_key = kmalloc(g->cipher_keysize, GFP_KERNEL);
216 if (!psb->cipher_key)
217 goto err_out_free_hash;
218 memcpy(psb->cipher_key, g->cipher_key, g->cipher_keysize);
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
241 ack = kmalloc(sizeof(struct pohmelfs_cn_ack), GFP_KERNEL);
242 if (!ack)
243 return -ENOMEM;
244
245 memset(ack, 0, sizeof(struct pohmelfs_cn_ack));
246 memcpy(&ack->msg, msg, sizeof(struct cn_msg));
247
248 if (action == POHMELFS_CTLINFO_ACK)
249 memcpy(&ack->ctl, ctl, sizeof(struct pohmelfs_ctl));
250
251 ack->msg.len = sizeof(struct pohmelfs_cn_ack) - sizeof(struct cn_msg);
252 ack->msg.ack = msg->ack + 1;
253 ack->error = err;
254 ack->msg_num = msg_num;
255
256 cn_netlink_send(&ack->msg, 0, GFP_KERNEL);
257 kfree(ack);
258 return 0;
259}
260
261static int pohmelfs_cn_disp(struct cn_msg *msg)
262{
263 struct pohmelfs_config_group *g;
264 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
265 struct pohmelfs_config *c, *tmp;
266 int err = 0, i = 1;
267
268 if (msg->len != sizeof(struct pohmelfs_ctl))
269 return -EBADMSG;
270
271 mutex_lock(&pohmelfs_config_lock);
272
273 g = pohmelfs_find_config_group(ctl->idx);
274 if (!g) {
275 pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL);
276 goto out_unlock;
277 }
278
279 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
280 struct pohmelfs_ctl *sc = &c->state.ctl;
281 if (pohmelfs_send_reply(err, g->num_entry - i, POHMELFS_CTLINFO_ACK, msg, sc)) {
282 err = -ENOMEM;
283 goto out_unlock;
284 }
285 i += 1;
286 }
287
2d7cf8ef
EP
288 out_unlock:
289 mutex_unlock(&pohmelfs_config_lock);
290 return err;
291}
292
293static int pohmelfs_cn_dump(struct cn_msg *msg)
294{
295 struct pohmelfs_config_group *g;
296 struct pohmelfs_config *c, *tmp;
297 int err = 0, i = 1;
298 int total_msg = 0;
299
300 if (msg->len != sizeof(struct pohmelfs_ctl))
301 return -EBADMSG;
302
303 mutex_lock(&pohmelfs_config_lock);
304
305 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
306 if (g)
307 total_msg += g->num_entry;
308 }
309 if (total_msg == 0) {
310 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
311 err = -ENOMEM;
312 goto out_unlock;
313 }
314
315 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
316 if (g) {
317 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
318 struct pohmelfs_ctl *sc = &c->state.ctl;
319 if (pohmelfs_send_reply(err, total_msg - i, POHMELFS_CTLINFO_ACK, msg, sc)) {
320 err = -ENOMEM;
321 goto out_unlock;
322 }
323 i += 1;
324 }
325 }
326 }
327
9ce8b619 328out_unlock:
2d7cf8ef
EP
329 mutex_unlock(&pohmelfs_config_lock);
330 return err;
331}
332
333static int pohmelfs_cn_flush(struct cn_msg *msg)
334{
335 struct pohmelfs_config_group *g;
336 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
337 struct pohmelfs_config *c, *tmp;
338 int err = 0;
339
340 if (msg->len != sizeof(struct pohmelfs_ctl))
341 return -EBADMSG;
342
343 mutex_lock(&pohmelfs_config_lock);
344
345 if (ctl->idx != POHMELFS_NULL_IDX) {
346 g = pohmelfs_find_config_group(ctl->idx);
347
348 if (!g)
349 goto out_unlock;
350
351 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
352 list_del(&c->config_entry);
353 g->num_entry--;
354 kfree(c);
355 }
356 } else {
357 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
358 if (g) {
359 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
360 list_del(&c->config_entry);
361 g->num_entry--;
362 kfree(c);
363 }
364 }
365 }
366 }
367
368out_unlock:
369 mutex_unlock(&pohmelfs_config_lock);
370 pohmelfs_cn_dump(msg);
371
372 return err;
9ce8b619
EP
373}
374
e0ca8739
EP
375static int pohmelfs_modify_config(struct pohmelfs_ctl *old, struct pohmelfs_ctl *new)
376{
377 old->perm = new->perm;
378 old->prio = new->prio;
379 return 0;
380}
381
9ce8b619
EP
382static int pohmelfs_cn_ctl(struct cn_msg *msg, int action)
383{
384 struct pohmelfs_config_group *g;
385 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
386 struct pohmelfs_config *c, *tmp;
387 int err = 0;
388
389 if (msg->len != sizeof(struct pohmelfs_ctl))
390 return -EBADMSG;
391
392 mutex_lock(&pohmelfs_config_lock);
393
394 g = pohmelfs_find_create_config_group(ctl->idx);
395 if (!g) {
396 err = -ENOMEM;
397 goto out_unlock;
398 }
399
400 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
401 struct pohmelfs_ctl *sc = &c->state.ctl;
402
403 if (pohmelfs_config_eql(sc, ctl)) {
404 if (action == POHMELFS_FLAGS_ADD) {
405 err = -EEXIST;
406 goto out_unlock;
407 } else if (action == POHMELFS_FLAGS_DEL) {
408 list_del(&c->config_entry);
409 g->num_entry--;
410 kfree(c);
411 goto out_unlock;
e0ca8739
EP
412 } else if (action == POHMELFS_FLAGS_MODIFY) {
413 err = pohmelfs_modify_config(sc, ctl);
414 goto out_unlock;
9ce8b619
EP
415 } else {
416 err = -EEXIST;
417 goto out_unlock;
418 }
419 }
420 }
421 if (action == POHMELFS_FLAGS_DEL) {
422 err = -EBADMSG;
423 goto out_unlock;
424 }
425
426 c = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
427 if (!c) {
428 err = -ENOMEM;
429 goto out_unlock;
430 }
431 memcpy(&c->state.ctl, ctl, sizeof(struct pohmelfs_ctl));
432 g->num_entry++;
e0ca8739 433
9ce8b619
EP
434 list_add_tail(&c->config_entry, &g->config_list);
435
2d7cf8ef 436 out_unlock:
9ce8b619
EP
437 mutex_unlock(&pohmelfs_config_lock);
438 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
439 err = -ENOMEM;
440
441 return err;
442}
443
444static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
445{
446 char *algo = (char *)c->data;
447 u8 *key = (u8 *)(algo + c->strlen);
448
449 if (g->hash_string)
450 return -EEXIST;
451
452 g->hash_string = kstrdup(algo, GFP_KERNEL);
453 if (!g->hash_string)
454 return -ENOMEM;
455 g->hash_strlen = c->strlen;
456 g->hash_keysize = c->keysize;
457
458 g->hash_key = kmalloc(c->keysize, GFP_KERNEL);
459 if (!g->hash_key) {
460 kfree(g->hash_string);
461 return -ENOMEM;
462 }
463
464 memcpy(g->hash_key, key, c->keysize);
465
466 return 0;
467}
468
469static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
470{
471 char *algo = (char *)c->data;
472 u8 *key = (u8 *)(algo + c->strlen);
473
474 if (g->cipher_string)
475 return -EEXIST;
476
477 g->cipher_string = kstrdup(algo, GFP_KERNEL);
478 if (!g->cipher_string)
479 return -ENOMEM;
480 g->cipher_strlen = c->strlen;
481 g->cipher_keysize = c->keysize;
482
483 g->cipher_key = kmalloc(c->keysize, GFP_KERNEL);
484 if (!g->cipher_key) {
485 kfree(g->cipher_string);
486 return -ENOMEM;
487 }
488
489 memcpy(g->cipher_key, key, c->keysize);
490
491 return 0;
492}
493
9ce8b619
EP
494static int pohmelfs_cn_crypto(struct cn_msg *msg)
495{
496 struct pohmelfs_crypto *crypto = (struct pohmelfs_crypto *)msg->data;
497 struct pohmelfs_config_group *g;
498 int err = 0;
499
500 dprintk("%s: idx: %u, strlen: %u, type: %u, keysize: %u, algo: %s.\n",
501 __func__, crypto->idx, crypto->strlen, crypto->type,
502 crypto->keysize, (char *)crypto->data);
503
504 mutex_lock(&pohmelfs_config_lock);
505 g = pohmelfs_find_create_config_group(crypto->idx);
506 if (!g) {
507 err = -ENOMEM;
508 goto out_unlock;
509 }
510
511 switch (crypto->type) {
512 case POHMELFS_CRYPTO_HASH:
513 err = pohmelfs_crypto_hash_init(g, crypto);
514 break;
515 case POHMELFS_CRYPTO_CIPHER:
516 err = pohmelfs_crypto_cipher_init(g, crypto);
517 break;
518 default:
519 err = -ENOTSUPP;
520 break;
521 }
522
523out_unlock:
524 mutex_unlock(&pohmelfs_config_lock);
525 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
526 err = -ENOMEM;
527
528 return err;
529}
530
7069331d 531static void pohmelfs_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
9ce8b619 532{
9ce8b619
EP
533 int err;
534
98a5783a
PR
535 if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN))
536 return;
537
9ce8b619
EP
538 switch (msg->flags) {
539 case POHMELFS_FLAGS_ADD:
9ce8b619 540 case POHMELFS_FLAGS_DEL:
e0ca8739
EP
541 case POHMELFS_FLAGS_MODIFY:
542 err = pohmelfs_cn_ctl(msg, msg->flags);
9ce8b619 543 break;
2d7cf8ef
EP
544 case POHMELFS_FLAGS_FLUSH:
545 err = pohmelfs_cn_flush(msg);
546 break;
9ce8b619
EP
547 case POHMELFS_FLAGS_SHOW:
548 err = pohmelfs_cn_disp(msg);
549 break;
2d7cf8ef
EP
550 case POHMELFS_FLAGS_DUMP:
551 err = pohmelfs_cn_dump(msg);
552 break;
9ce8b619
EP
553 case POHMELFS_FLAGS_CRYPTO:
554 err = pohmelfs_cn_crypto(msg);
555 break;
556 default:
557 err = -ENOSYS;
558 break;
559 }
560}
561
562int pohmelfs_config_check(struct pohmelfs_config *config, int idx)
563{
564 struct pohmelfs_ctl *ctl = &config->state.ctl;
565 struct pohmelfs_config *tmp;
566 int err = -ENOENT;
567 struct pohmelfs_ctl *sc;
568 struct pohmelfs_config_group *g;
569
570 mutex_lock(&pohmelfs_config_lock);
571
572 g = pohmelfs_find_config_group(ctl->idx);
573 if (g) {
574 list_for_each_entry(tmp, &g->config_list, config_entry) {
575 sc = &tmp->state.ctl;
576
577 if (pohmelfs_config_eql(sc, ctl)) {
578 err = 0;
579 break;
580 }
581 }
582 }
583
584 mutex_unlock(&pohmelfs_config_lock);
585
586 return err;
587}
588
589int __init pohmelfs_config_init(void)
590{
2d7cf8ef
EP
591 /* XXX remove (void *) cast when vanilla connector got synced */
592 return cn_add_callback(&pohmelfs_cn_id, "pohmelfs", (void *)pohmelfs_cn_callback);
9ce8b619
EP
593}
594
595void pohmelfs_config_exit(void)
596{
597 struct pohmelfs_config *c, *tmp;
598 struct pohmelfs_config_group *g, *gtmp;
599
600 cn_del_callback(&pohmelfs_cn_id);
601
602 mutex_lock(&pohmelfs_config_lock);
603 list_for_each_entry_safe(g, gtmp, &pohmelfs_config_list, group_entry) {
604 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
605 list_del(&c->config_entry);
606 kfree(c);
607 }
608
609 list_del(&g->group_entry);
610
611 if (g->hash_string)
612 kfree(g->hash_string);
613
614 if (g->cipher_string)
615 kfree(g->cipher_string);
616
617 kfree(g);
618 }
619 mutex_unlock(&pohmelfs_config_lock);
620}