Staging: Pohmelfs: Added IO permissions and priorities.
[safe/jmp/linux-2.6] / drivers / staging / pohmelfs / config.c
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>
23
24 #include "netfs.h"
25
26 /*
27  * Global configuration list.
28  * Each client can be asked to get one of them.
29  *
30  * Allows to provide remote server address (ipv4/v6/whatever), port
31  * and so on via kernel connector.
32  */
33
34 static struct cb_id pohmelfs_cn_id = {.idx = POHMELFS_CN_IDX, .val = POHMELFS_CN_VAL};
35 static LIST_HEAD(pohmelfs_config_list);
36 static DEFINE_MUTEX(pohmelfs_config_lock);
37
38 static inline int pohmelfs_config_eql(struct pohmelfs_ctl *sc, struct pohmelfs_ctl *ctl)
39 {
40         if (sc->idx == ctl->idx && sc->type == ctl->type &&
41                         sc->proto == ctl->proto &&
42                         sc->addrlen == ctl->addrlen &&
43                         !memcmp(&sc->addr, &ctl->addr, ctl->addrlen))
44                 return 1;
45
46         return 0;
47 }
48
49 static struct pohmelfs_config_group *pohmelfs_find_config_group(unsigned int idx)
50 {
51         struct pohmelfs_config_group *g, *group = NULL;
52
53         list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
54                 if (g->idx == idx) {
55                         group = g;
56                         break;
57                 }
58         }
59
60         return group;
61 }
62
63 static struct pohmelfs_config_group *pohmelfs_find_create_config_group(unsigned int idx)
64 {
65         struct pohmelfs_config_group *g;
66
67         g = pohmelfs_find_config_group(idx);
68         if (g)
69                 return g;
70
71         g = kzalloc(sizeof(struct pohmelfs_config_group), GFP_KERNEL);
72         if (!g)
73                 return NULL;
74
75         INIT_LIST_HEAD(&g->config_list);
76         g->idx = idx;
77         g->num_entry = 0;
78
79         list_add_tail(&g->group_entry, &pohmelfs_config_list);
80
81         return g;
82 }
83
84 static inline void pohmelfs_insert_config_entry(struct pohmelfs_sb *psb, struct pohmelfs_config *dst)
85 {
86         struct pohmelfs_config *tmp;
87
88         INIT_LIST_HEAD(&dst->config_entry);
89
90         list_for_each_entry(tmp, &psb->state_list, config_entry) {
91                 if (dst->state.ctl.prio > tmp->state.ctl.prio)
92                         list_add_tail(&dst->config_entry, &tmp->config_entry);
93         }
94         if (list_empty(&dst->config_entry))
95                 list_add_tail(&dst->config_entry, &psb->state_list);
96 }
97
98 static int pohmelfs_move_config_entry(struct pohmelfs_sb *psb,
99                 struct pohmelfs_config *dst, struct pohmelfs_config *new)
100 {
101         if ((dst->state.ctl.prio == new->state.ctl.prio) &&
102                 (dst->state.ctl.perm == new->state.ctl.perm))
103                 return 0;
104
105         dprintk("%s: dst: prio: %d, perm: %x, new: prio: %d, perm: %d.\n",
106                         __func__, dst->state.ctl.prio, dst->state.ctl.perm,
107                         new->state.ctl.prio, new->state.ctl.perm);
108         dst->state.ctl.prio = new->state.ctl.prio;
109         dst->state.ctl.perm = new->state.ctl.perm;
110
111         list_del_init(&dst->config_entry);
112         pohmelfs_insert_config_entry(psb, dst);
113         return 0;
114 }
115
116 /*
117  * pohmelfs_copy_config() is used to copy new state configs from the
118  * config group (controlled by the netlink messages) into the superblock.
119  * This happens either at startup time where no transactions can access
120  * the list of the configs (and thus list of the network states), or at
121  * run-time, where it is protected by the psb->state_lock.
122  */
123 int pohmelfs_copy_config(struct pohmelfs_sb *psb)
124 {
125         struct pohmelfs_config_group *g;
126         struct pohmelfs_config *c, *dst;
127         int err = -ENODEV;
128
129         mutex_lock(&pohmelfs_config_lock);
130
131         g = pohmelfs_find_config_group(psb->idx);
132         if (!g)
133                 goto out_unlock;
134
135         /*
136          * Run over all entries in given config group and try to crate and
137          * initialize those, which do not exist in superblock list.
138          * Skip all existing entries.
139          */
140
141         list_for_each_entry(c, &g->config_list, config_entry) {
142                 err = 0;
143                 list_for_each_entry(dst, &psb->state_list, config_entry) {
144                         if (pohmelfs_config_eql(&dst->state.ctl, &c->state.ctl)) {
145                                 err = pohmelfs_move_config_entry(psb, dst, c);
146                                 if (!err)
147                                         err = -EEXIST;
148                                 break;
149                         }
150                 }
151
152                 if (err)
153                         continue;
154
155                 dst = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
156                 if (!dst) {
157                         err = -ENOMEM;
158                         break;
159                 }
160
161                 memcpy(&dst->state.ctl, &c->state.ctl, sizeof(struct pohmelfs_ctl));
162
163                 pohmelfs_insert_config_entry(psb, dst);
164
165                 err = pohmelfs_state_init_one(psb, dst);
166                 if (err) {
167                         list_del(&dst->config_entry);
168                         kfree(dst);
169                 }
170
171                 err = 0;
172         }
173
174 out_unlock:
175         mutex_unlock(&pohmelfs_config_lock);
176
177         return err;
178 }
179
180 int pohmelfs_copy_crypto(struct pohmelfs_sb *psb)
181 {
182         struct pohmelfs_config_group *g;
183         int err = -ENOENT;
184
185         mutex_lock(&pohmelfs_config_lock);
186         g = pohmelfs_find_config_group(psb->idx);
187         if (!g)
188                 goto err_out_exit;
189
190         if (g->hash_string) {
191                 err = -ENOMEM;
192                 psb->hash_string = kstrdup(g->hash_string, GFP_KERNEL);
193                 if (!psb->hash_string)
194                         goto err_out_exit;
195                 psb->hash_strlen = g->hash_strlen;
196         }
197
198         if (g->cipher_string) {
199                 psb->cipher_string = kstrdup(g->cipher_string, GFP_KERNEL);
200                 if (!psb->cipher_string)
201                         goto err_out_free_hash_string;
202                 psb->cipher_strlen = g->cipher_strlen;
203         }
204
205         if (g->hash_keysize) {
206                 psb->hash_key = kmalloc(g->hash_keysize, GFP_KERNEL);
207                 if (!psb->hash_key)
208                         goto err_out_free_cipher_string;
209                 memcpy(psb->hash_key, g->hash_key, g->hash_keysize);
210                 psb->hash_keysize = g->hash_keysize;
211         }
212
213         if (g->cipher_keysize) {
214                 psb->cipher_key = kmalloc(g->cipher_keysize, GFP_KERNEL);
215                 if (!psb->cipher_key)
216                         goto err_out_free_hash;
217                 memcpy(psb->cipher_key, g->cipher_key, g->cipher_keysize);
218                 psb->cipher_keysize = g->cipher_keysize;
219         }
220
221         mutex_unlock(&pohmelfs_config_lock);
222
223         return 0;
224
225 err_out_free_hash:
226         kfree(psb->hash_key);
227 err_out_free_cipher_string:
228         kfree(psb->cipher_string);
229 err_out_free_hash_string:
230         kfree(psb->hash_string);
231 err_out_exit:
232         mutex_unlock(&pohmelfs_config_lock);
233         return err;
234 }
235
236 static int pohmelfs_send_reply(int err, int msg_num, int action, struct cn_msg *msg, struct pohmelfs_ctl *ctl)
237 {
238         struct pohmelfs_cn_ack *ack;
239
240         ack = kmalloc(sizeof(struct pohmelfs_cn_ack), GFP_KERNEL);
241         if (!ack)
242                 return -ENOMEM;
243
244         memset(ack, 0, sizeof(struct pohmelfs_cn_ack));
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
260 static 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
287 out_unlock:
288         mutex_unlock(&pohmelfs_config_lock);
289         return err;
290 }
291
292 static int pohmelfs_modify_config(struct pohmelfs_ctl *old, struct pohmelfs_ctl *new)
293 {
294         old->perm = new->perm;
295         old->prio = new->prio;
296         return 0;
297 }
298
299 static int pohmelfs_cn_ctl(struct cn_msg *msg, int action)
300 {
301         struct pohmelfs_config_group *g;
302         struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
303         struct pohmelfs_config *c, *tmp;
304         int err = 0;
305
306         if (msg->len != sizeof(struct pohmelfs_ctl))
307                 return -EBADMSG;
308
309         mutex_lock(&pohmelfs_config_lock);
310
311         g = pohmelfs_find_create_config_group(ctl->idx);
312         if (!g) {
313                 err = -ENOMEM;
314                 goto out_unlock;
315         }
316
317         list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
318                 struct pohmelfs_ctl *sc = &c->state.ctl;
319
320                 if (pohmelfs_config_eql(sc, ctl)) {
321                         if (action == POHMELFS_FLAGS_ADD) {
322                                 err = -EEXIST;
323                                 goto out_unlock;
324                         } else if (action == POHMELFS_FLAGS_DEL) {
325                                 list_del(&c->config_entry);
326                                 g->num_entry--;
327                                 kfree(c);
328                                 goto out_unlock;
329                         } else if (action == POHMELFS_FLAGS_MODIFY) {
330                                 err = pohmelfs_modify_config(sc, ctl);
331                                 goto out_unlock;
332                         } else {
333                                 err = -EEXIST;
334                                 goto out_unlock;
335                         }
336                 }
337         }
338         if (action == POHMELFS_FLAGS_DEL) {
339                 err = -EBADMSG;
340                 goto out_unlock;
341         }
342
343         c = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
344         if (!c) {
345                 err = -ENOMEM;
346                 goto out_unlock;
347         }
348         memcpy(&c->state.ctl, ctl, sizeof(struct pohmelfs_ctl));
349         g->num_entry++;
350
351         list_add_tail(&c->config_entry, &g->config_list);
352
353 out_unlock:
354         mutex_unlock(&pohmelfs_config_lock);
355         if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
356                 err = -ENOMEM;
357
358         return err;
359 }
360
361 static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
362 {
363         char *algo = (char *)c->data;
364         u8 *key = (u8 *)(algo + c->strlen);
365
366         if (g->hash_string)
367                 return -EEXIST;
368
369         g->hash_string = kstrdup(algo, GFP_KERNEL);
370         if (!g->hash_string)
371                 return -ENOMEM;
372         g->hash_strlen = c->strlen;
373         g->hash_keysize = c->keysize;
374
375         g->hash_key = kmalloc(c->keysize, GFP_KERNEL);
376         if (!g->hash_key) {
377                 kfree(g->hash_string);
378                 return -ENOMEM;
379         }
380
381         memcpy(g->hash_key, key, c->keysize);
382
383         return 0;
384 }
385
386 static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
387 {
388         char *algo = (char *)c->data;
389         u8 *key = (u8 *)(algo + c->strlen);
390
391         if (g->cipher_string)
392                 return -EEXIST;
393
394         g->cipher_string = kstrdup(algo, GFP_KERNEL);
395         if (!g->cipher_string)
396                 return -ENOMEM;
397         g->cipher_strlen = c->strlen;
398         g->cipher_keysize = c->keysize;
399
400         g->cipher_key = kmalloc(c->keysize, GFP_KERNEL);
401         if (!g->cipher_key) {
402                 kfree(g->cipher_string);
403                 return -ENOMEM;
404         }
405
406         memcpy(g->cipher_key, key, c->keysize);
407
408         return 0;
409 }
410
411
412 static int pohmelfs_cn_crypto(struct cn_msg *msg)
413 {
414         struct pohmelfs_crypto *crypto = (struct pohmelfs_crypto *)msg->data;
415         struct pohmelfs_config_group *g;
416         int err = 0;
417
418         dprintk("%s: idx: %u, strlen: %u, type: %u, keysize: %u, algo: %s.\n",
419                         __func__, crypto->idx, crypto->strlen, crypto->type,
420                         crypto->keysize, (char *)crypto->data);
421
422         mutex_lock(&pohmelfs_config_lock);
423         g = pohmelfs_find_create_config_group(crypto->idx);
424         if (!g) {
425                 err = -ENOMEM;
426                 goto out_unlock;
427         }
428
429         switch (crypto->type) {
430                 case POHMELFS_CRYPTO_HASH:
431                         err = pohmelfs_crypto_hash_init(g, crypto);
432                         break;
433                 case POHMELFS_CRYPTO_CIPHER:
434                         err = pohmelfs_crypto_cipher_init(g, crypto);
435                         break;
436                 default:
437                         err = -ENOTSUPP;
438                         break;
439         }
440
441 out_unlock:
442         mutex_unlock(&pohmelfs_config_lock);
443         if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
444                 err = -ENOMEM;
445
446         return err;
447 }
448
449 static void pohmelfs_cn_callback(void *data)
450 {
451         struct cn_msg *msg = data;
452         int err;
453
454         switch (msg->flags) {
455                 case POHMELFS_FLAGS_ADD:
456                 case POHMELFS_FLAGS_DEL:
457                 case POHMELFS_FLAGS_MODIFY:
458                         err = pohmelfs_cn_ctl(msg, msg->flags);
459                         break;
460                 case POHMELFS_FLAGS_SHOW:
461                         err = pohmelfs_cn_disp(msg);
462                         break;
463                 case POHMELFS_FLAGS_CRYPTO:
464                         err = pohmelfs_cn_crypto(msg);
465                         break;
466                 default:
467                         err = -ENOSYS;
468                         break;
469         }
470 }
471
472 int pohmelfs_config_check(struct pohmelfs_config *config, int idx)
473 {
474         struct pohmelfs_ctl *ctl = &config->state.ctl;
475         struct pohmelfs_config *tmp;
476         int err = -ENOENT;
477         struct pohmelfs_ctl *sc;
478         struct pohmelfs_config_group *g;
479
480         mutex_lock(&pohmelfs_config_lock);
481
482         g = pohmelfs_find_config_group(ctl->idx);
483         if (g) {
484                 list_for_each_entry(tmp, &g->config_list, config_entry) {
485                         sc = &tmp->state.ctl;
486
487                         if (pohmelfs_config_eql(sc, ctl)) {
488                                 err = 0;
489                                 break;
490                         }
491                 }
492         }
493
494         mutex_unlock(&pohmelfs_config_lock);
495
496         return err;
497 }
498
499 int __init pohmelfs_config_init(void)
500 {
501         return cn_add_callback(&pohmelfs_cn_id, "pohmelfs", pohmelfs_cn_callback);
502 }
503
504 void pohmelfs_config_exit(void)
505 {
506         struct pohmelfs_config *c, *tmp;
507         struct pohmelfs_config_group *g, *gtmp;
508
509         cn_del_callback(&pohmelfs_cn_id);
510
511         mutex_lock(&pohmelfs_config_lock);
512         list_for_each_entry_safe(g, gtmp, &pohmelfs_config_list, group_entry) {
513                 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
514                         list_del(&c->config_entry);
515                         kfree(c);
516                 }
517
518                 list_del(&g->group_entry);
519
520                 if (g->hash_string)
521                         kfree(g->hash_string);
522
523                 if (g->cipher_string)
524                         kfree(g->cipher_string);
525
526                 kfree(g);
527         }
528         mutex_unlock(&pohmelfs_config_lock);
529 }