2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
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.
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.
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>
27 * Global configuration list.
28 * Each client can be asked to get one of them.
30 * Allows to provide remote server address (ipv4/v6/whatever), port
31 * and so on via kernel connector.
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);
38 static inline int pohmelfs_config_eql(struct pohmelfs_ctl *sc, struct pohmelfs_ctl *ctl)
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))
49 static struct pohmelfs_config_group *pohmelfs_find_config_group(unsigned int idx)
51 struct pohmelfs_config_group *g, *group = NULL;
53 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
63 static struct pohmelfs_config_group *pohmelfs_find_create_config_group(unsigned int idx)
65 struct pohmelfs_config_group *g;
67 g = pohmelfs_find_config_group(idx);
71 g = kzalloc(sizeof(struct pohmelfs_config_group), GFP_KERNEL);
75 INIT_LIST_HEAD(&g->config_list);
79 list_add_tail(&g->group_entry, &pohmelfs_config_list);
84 int pohmelfs_copy_config(struct pohmelfs_sb *psb)
86 struct pohmelfs_config_group *g;
87 struct pohmelfs_config *c, *dst;
90 mutex_lock(&pohmelfs_config_lock);
92 g = pohmelfs_find_config_group(psb->idx);
97 * Run over all entries in given config group and try to crate and
98 * initialize those, which do not exist in superblock list.
99 * Skip all existing entries.
102 list_for_each_entry(c, &g->config_list, config_entry) {
104 list_for_each_entry(dst, &psb->state_list, config_entry) {
105 if (pohmelfs_config_eql(&dst->state.ctl, &c->state.ctl)) {
114 dst = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
120 memcpy(&dst->state.ctl, &c->state.ctl, sizeof(struct pohmelfs_ctl));
122 list_add_tail(&dst->config_entry, &psb->state_list);
124 err = pohmelfs_state_init_one(psb, dst);
126 list_del(&dst->config_entry);
134 mutex_unlock(&pohmelfs_config_lock);
139 int pohmelfs_copy_crypto(struct pohmelfs_sb *psb)
141 struct pohmelfs_config_group *g;
144 mutex_lock(&pohmelfs_config_lock);
145 g = pohmelfs_find_config_group(psb->idx);
149 if (g->hash_string) {
151 psb->hash_string = kstrdup(g->hash_string, GFP_KERNEL);
152 if (!psb->hash_string)
154 psb->hash_strlen = g->hash_strlen;
157 if (g->cipher_string) {
158 psb->cipher_string = kstrdup(g->cipher_string, GFP_KERNEL);
159 if (!psb->cipher_string)
160 goto err_out_free_hash_string;
161 psb->cipher_strlen = g->cipher_strlen;
164 if (g->hash_keysize) {
165 psb->hash_key = kmalloc(g->hash_keysize, GFP_KERNEL);
167 goto err_out_free_cipher_string;
168 memcpy(psb->hash_key, g->hash_key, g->hash_keysize);
169 psb->hash_keysize = g->hash_keysize;
172 if (g->cipher_keysize) {
173 psb->cipher_key = kmalloc(g->cipher_keysize, GFP_KERNEL);
174 if (!psb->cipher_key)
175 goto err_out_free_hash;
176 memcpy(psb->cipher_key, g->cipher_key, g->cipher_keysize);
177 psb->cipher_keysize = g->cipher_keysize;
180 mutex_unlock(&pohmelfs_config_lock);
185 kfree(psb->hash_key);
186 err_out_free_cipher_string:
187 kfree(psb->cipher_string);
188 err_out_free_hash_string:
189 kfree(psb->hash_string);
191 mutex_unlock(&pohmelfs_config_lock);
195 static int pohmelfs_send_reply(int err, int msg_num, int action, struct cn_msg *msg, struct pohmelfs_ctl *ctl)
197 struct pohmelfs_cn_ack *ack;
199 ack = kmalloc(sizeof(struct pohmelfs_cn_ack), GFP_KERNEL);
203 memset(ack, 0, sizeof(struct pohmelfs_cn_ack));
204 memcpy(&ack->msg, msg, sizeof(struct cn_msg));
206 if (action == POHMELFS_CTLINFO_ACK)
207 memcpy(&ack->ctl, ctl, sizeof(struct pohmelfs_ctl));
209 ack->msg.len = sizeof(struct pohmelfs_cn_ack) - sizeof(struct cn_msg);
210 ack->msg.ack = msg->ack + 1;
212 ack->msg_num = msg_num;
214 cn_netlink_send(&ack->msg, 0, GFP_KERNEL);
219 static int pohmelfs_cn_disp(struct cn_msg *msg)
221 struct pohmelfs_config_group *g;
222 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
223 struct pohmelfs_config *c, *tmp;
226 if (msg->len != sizeof(struct pohmelfs_ctl))
229 mutex_lock(&pohmelfs_config_lock);
231 g = pohmelfs_find_config_group(ctl->idx);
233 pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL);
237 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
238 struct pohmelfs_ctl *sc = &c->state.ctl;
239 if (pohmelfs_send_reply(err, g->num_entry - i, POHMELFS_CTLINFO_ACK, msg, sc)) {
247 mutex_unlock(&pohmelfs_config_lock);
251 static int pohmelfs_cn_ctl(struct cn_msg *msg, int action)
253 struct pohmelfs_config_group *g;
254 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
255 struct pohmelfs_config *c, *tmp;
258 if (msg->len != sizeof(struct pohmelfs_ctl))
261 mutex_lock(&pohmelfs_config_lock);
263 g = pohmelfs_find_create_config_group(ctl->idx);
269 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
270 struct pohmelfs_ctl *sc = &c->state.ctl;
272 if (pohmelfs_config_eql(sc, ctl)) {
273 if (action == POHMELFS_FLAGS_ADD) {
276 } else if (action == POHMELFS_FLAGS_DEL) {
277 list_del(&c->config_entry);
287 if (action == POHMELFS_FLAGS_DEL) {
292 c = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
297 memcpy(&c->state.ctl, ctl, sizeof(struct pohmelfs_ctl));
299 list_add_tail(&c->config_entry, &g->config_list);
302 mutex_unlock(&pohmelfs_config_lock);
303 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
309 static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
311 char *algo = (char *)c->data;
312 u8 *key = (u8 *)(algo + c->strlen);
317 g->hash_string = kstrdup(algo, GFP_KERNEL);
320 g->hash_strlen = c->strlen;
321 g->hash_keysize = c->keysize;
323 g->hash_key = kmalloc(c->keysize, GFP_KERNEL);
325 kfree(g->hash_string);
329 memcpy(g->hash_key, key, c->keysize);
334 static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
336 char *algo = (char *)c->data;
337 u8 *key = (u8 *)(algo + c->strlen);
339 if (g->cipher_string)
342 g->cipher_string = kstrdup(algo, GFP_KERNEL);
343 if (!g->cipher_string)
345 g->cipher_strlen = c->strlen;
346 g->cipher_keysize = c->keysize;
348 g->cipher_key = kmalloc(c->keysize, GFP_KERNEL);
349 if (!g->cipher_key) {
350 kfree(g->cipher_string);
354 memcpy(g->cipher_key, key, c->keysize);
360 static int pohmelfs_cn_crypto(struct cn_msg *msg)
362 struct pohmelfs_crypto *crypto = (struct pohmelfs_crypto *)msg->data;
363 struct pohmelfs_config_group *g;
366 dprintk("%s: idx: %u, strlen: %u, type: %u, keysize: %u, algo: %s.\n",
367 __func__, crypto->idx, crypto->strlen, crypto->type,
368 crypto->keysize, (char *)crypto->data);
370 mutex_lock(&pohmelfs_config_lock);
371 g = pohmelfs_find_create_config_group(crypto->idx);
377 switch (crypto->type) {
378 case POHMELFS_CRYPTO_HASH:
379 err = pohmelfs_crypto_hash_init(g, crypto);
381 case POHMELFS_CRYPTO_CIPHER:
382 err = pohmelfs_crypto_cipher_init(g, crypto);
390 mutex_unlock(&pohmelfs_config_lock);
391 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
397 static void pohmelfs_cn_callback(void *data)
399 struct cn_msg *msg = data;
402 switch (msg->flags) {
403 case POHMELFS_FLAGS_ADD:
404 err = pohmelfs_cn_ctl(msg, POHMELFS_FLAGS_ADD);
406 case POHMELFS_FLAGS_DEL:
407 err = pohmelfs_cn_ctl(msg, POHMELFS_FLAGS_DEL);
409 case POHMELFS_FLAGS_SHOW:
410 err = pohmelfs_cn_disp(msg);
412 case POHMELFS_FLAGS_CRYPTO:
413 err = pohmelfs_cn_crypto(msg);
421 int pohmelfs_config_check(struct pohmelfs_config *config, int idx)
423 struct pohmelfs_ctl *ctl = &config->state.ctl;
424 struct pohmelfs_config *tmp;
426 struct pohmelfs_ctl *sc;
427 struct pohmelfs_config_group *g;
429 mutex_lock(&pohmelfs_config_lock);
431 g = pohmelfs_find_config_group(ctl->idx);
433 list_for_each_entry(tmp, &g->config_list, config_entry) {
434 sc = &tmp->state.ctl;
436 if (pohmelfs_config_eql(sc, ctl)) {
443 mutex_unlock(&pohmelfs_config_lock);
448 int __init pohmelfs_config_init(void)
450 return cn_add_callback(&pohmelfs_cn_id, "pohmelfs", pohmelfs_cn_callback);
453 void pohmelfs_config_exit(void)
455 struct pohmelfs_config *c, *tmp;
456 struct pohmelfs_config_group *g, *gtmp;
458 cn_del_callback(&pohmelfs_cn_id);
460 mutex_lock(&pohmelfs_config_lock);
461 list_for_each_entry_safe(g, gtmp, &pohmelfs_config_list, group_entry) {
462 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
463 list_del(&c->config_entry);
467 list_del(&g->group_entry);
470 kfree(g->hash_string);
472 if (g->cipher_string)
473 kfree(g->cipher_string);
477 mutex_unlock(&pohmelfs_config_lock);