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 static inline void pohmelfs_insert_config_entry(struct pohmelfs_sb *psb, struct pohmelfs_config *dst)
86 struct pohmelfs_config *tmp;
88 INIT_LIST_HEAD(&dst->config_entry);
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);
94 if (list_empty(&dst->config_entry))
95 list_add_tail(&dst->config_entry, &psb->state_list);
98 static int pohmelfs_move_config_entry(struct pohmelfs_sb *psb,
99 struct pohmelfs_config *dst, struct pohmelfs_config *new)
101 if ((dst->state.ctl.prio == new->state.ctl.prio) &&
102 (dst->state.ctl.perm == new->state.ctl.perm))
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;
111 list_del_init(&dst->config_entry);
112 pohmelfs_insert_config_entry(psb, dst);
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.
123 int pohmelfs_copy_config(struct pohmelfs_sb *psb)
125 struct pohmelfs_config_group *g;
126 struct pohmelfs_config *c, *dst;
129 mutex_lock(&pohmelfs_config_lock);
131 g = pohmelfs_find_config_group(psb->idx);
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.
141 list_for_each_entry(c, &g->config_list, config_entry) {
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);
155 dst = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
161 memcpy(&dst->state.ctl, &c->state.ctl, sizeof(struct pohmelfs_ctl));
163 pohmelfs_insert_config_entry(psb, dst);
165 err = pohmelfs_state_init_one(psb, dst);
167 list_del(&dst->config_entry);
175 mutex_unlock(&pohmelfs_config_lock);
180 int pohmelfs_copy_crypto(struct pohmelfs_sb *psb)
182 struct pohmelfs_config_group *g;
185 mutex_lock(&pohmelfs_config_lock);
186 g = pohmelfs_find_config_group(psb->idx);
190 if (g->hash_string) {
192 psb->hash_string = kstrdup(g->hash_string, GFP_KERNEL);
193 if (!psb->hash_string)
195 psb->hash_strlen = g->hash_strlen;
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;
205 if (g->hash_keysize) {
206 psb->hash_key = kmalloc(g->hash_keysize, GFP_KERNEL);
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;
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;
221 mutex_unlock(&pohmelfs_config_lock);
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);
232 mutex_unlock(&pohmelfs_config_lock);
236 static int pohmelfs_send_reply(int err, int msg_num, int action, struct cn_msg *msg, struct pohmelfs_ctl *ctl)
238 struct pohmelfs_cn_ack *ack;
240 ack = kmalloc(sizeof(struct pohmelfs_cn_ack), GFP_KERNEL);
244 memset(ack, 0, sizeof(struct pohmelfs_cn_ack));
245 memcpy(&ack->msg, msg, sizeof(struct cn_msg));
247 if (action == POHMELFS_CTLINFO_ACK)
248 memcpy(&ack->ctl, ctl, sizeof(struct pohmelfs_ctl));
250 ack->msg.len = sizeof(struct pohmelfs_cn_ack) - sizeof(struct cn_msg);
251 ack->msg.ack = msg->ack + 1;
253 ack->msg_num = msg_num;
255 cn_netlink_send(&ack->msg, 0, GFP_KERNEL);
260 static int pohmelfs_cn_disp(struct cn_msg *msg)
262 struct pohmelfs_config_group *g;
263 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
264 struct pohmelfs_config *c, *tmp;
267 if (msg->len != sizeof(struct pohmelfs_ctl))
270 mutex_lock(&pohmelfs_config_lock);
272 g = pohmelfs_find_config_group(ctl->idx);
274 pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL);
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)) {
288 mutex_unlock(&pohmelfs_config_lock);
292 static int pohmelfs_cn_dump(struct cn_msg *msg)
294 struct pohmelfs_config_group *g;
295 struct pohmelfs_config *c, *tmp;
299 if (msg->len != sizeof(struct pohmelfs_ctl))
302 mutex_lock(&pohmelfs_config_lock);
304 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
306 total_msg += g->num_entry;
308 if (total_msg == 0) {
309 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
314 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
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)) {
328 mutex_unlock(&pohmelfs_config_lock);
332 static int pohmelfs_cn_flush(struct cn_msg *msg)
334 struct pohmelfs_config_group *g;
335 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
336 struct pohmelfs_config *c, *tmp;
339 if (msg->len != sizeof(struct pohmelfs_ctl))
342 mutex_lock(&pohmelfs_config_lock);
344 if (ctl->idx != POHMELFS_NULL_IDX) {
345 g = pohmelfs_find_config_group(ctl->idx);
350 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
351 list_del(&c->config_entry);
356 list_for_each_entry(g, &pohmelfs_config_list, group_entry) {
358 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
359 list_del(&c->config_entry);
368 mutex_unlock(&pohmelfs_config_lock);
369 pohmelfs_cn_dump(msg);
374 static int pohmelfs_modify_config(struct pohmelfs_ctl *old, struct pohmelfs_ctl *new)
376 old->perm = new->perm;
377 old->prio = new->prio;
381 static int pohmelfs_cn_ctl(struct cn_msg *msg, int action)
383 struct pohmelfs_config_group *g;
384 struct pohmelfs_ctl *ctl = (struct pohmelfs_ctl *)msg->data;
385 struct pohmelfs_config *c, *tmp;
388 if (msg->len != sizeof(struct pohmelfs_ctl))
391 mutex_lock(&pohmelfs_config_lock);
393 g = pohmelfs_find_create_config_group(ctl->idx);
399 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
400 struct pohmelfs_ctl *sc = &c->state.ctl;
402 if (pohmelfs_config_eql(sc, ctl)) {
403 if (action == POHMELFS_FLAGS_ADD) {
406 } else if (action == POHMELFS_FLAGS_DEL) {
407 list_del(&c->config_entry);
411 } else if (action == POHMELFS_FLAGS_MODIFY) {
412 err = pohmelfs_modify_config(sc, ctl);
420 if (action == POHMELFS_FLAGS_DEL) {
425 c = kzalloc(sizeof(struct pohmelfs_config), GFP_KERNEL);
430 memcpy(&c->state.ctl, ctl, sizeof(struct pohmelfs_ctl));
433 list_add_tail(&c->config_entry, &g->config_list);
436 mutex_unlock(&pohmelfs_config_lock);
437 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
443 static int pohmelfs_crypto_hash_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
445 char *algo = (char *)c->data;
446 u8 *key = (u8 *)(algo + c->strlen);
451 g->hash_string = kstrdup(algo, GFP_KERNEL);
454 g->hash_strlen = c->strlen;
455 g->hash_keysize = c->keysize;
457 g->hash_key = kmalloc(c->keysize, GFP_KERNEL);
459 kfree(g->hash_string);
463 memcpy(g->hash_key, key, c->keysize);
468 static int pohmelfs_crypto_cipher_init(struct pohmelfs_config_group *g, struct pohmelfs_crypto *c)
470 char *algo = (char *)c->data;
471 u8 *key = (u8 *)(algo + c->strlen);
473 if (g->cipher_string)
476 g->cipher_string = kstrdup(algo, GFP_KERNEL);
477 if (!g->cipher_string)
479 g->cipher_strlen = c->strlen;
480 g->cipher_keysize = c->keysize;
482 g->cipher_key = kmalloc(c->keysize, GFP_KERNEL);
483 if (!g->cipher_key) {
484 kfree(g->cipher_string);
488 memcpy(g->cipher_key, key, c->keysize);
493 static int pohmelfs_cn_crypto(struct cn_msg *msg)
495 struct pohmelfs_crypto *crypto = (struct pohmelfs_crypto *)msg->data;
496 struct pohmelfs_config_group *g;
499 dprintk("%s: idx: %u, strlen: %u, type: %u, keysize: %u, algo: %s.\n",
500 __func__, crypto->idx, crypto->strlen, crypto->type,
501 crypto->keysize, (char *)crypto->data);
503 mutex_lock(&pohmelfs_config_lock);
504 g = pohmelfs_find_create_config_group(crypto->idx);
510 switch (crypto->type) {
511 case POHMELFS_CRYPTO_HASH:
512 err = pohmelfs_crypto_hash_init(g, crypto);
514 case POHMELFS_CRYPTO_CIPHER:
515 err = pohmelfs_crypto_cipher_init(g, crypto);
523 mutex_unlock(&pohmelfs_config_lock);
524 if (pohmelfs_send_reply(err, 0, POHMELFS_NOINFO_ACK, msg, NULL))
530 static void pohmelfs_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
534 if (!cap_raised(nsp->eff_cap, CAP_SYS_ADMIN))
537 switch (msg->flags) {
538 case POHMELFS_FLAGS_ADD:
539 case POHMELFS_FLAGS_DEL:
540 case POHMELFS_FLAGS_MODIFY:
541 err = pohmelfs_cn_ctl(msg, msg->flags);
543 case POHMELFS_FLAGS_FLUSH:
544 err = pohmelfs_cn_flush(msg);
546 case POHMELFS_FLAGS_SHOW:
547 err = pohmelfs_cn_disp(msg);
549 case POHMELFS_FLAGS_DUMP:
550 err = pohmelfs_cn_dump(msg);
552 case POHMELFS_FLAGS_CRYPTO:
553 err = pohmelfs_cn_crypto(msg);
561 int pohmelfs_config_check(struct pohmelfs_config *config, int idx)
563 struct pohmelfs_ctl *ctl = &config->state.ctl;
564 struct pohmelfs_config *tmp;
566 struct pohmelfs_ctl *sc;
567 struct pohmelfs_config_group *g;
569 mutex_lock(&pohmelfs_config_lock);
571 g = pohmelfs_find_config_group(ctl->idx);
573 list_for_each_entry(tmp, &g->config_list, config_entry) {
574 sc = &tmp->state.ctl;
576 if (pohmelfs_config_eql(sc, ctl)) {
583 mutex_unlock(&pohmelfs_config_lock);
588 int __init pohmelfs_config_init(void)
590 /* XXX remove (void *) cast when vanilla connector got synced */
591 return cn_add_callback(&pohmelfs_cn_id, "pohmelfs", (void *)pohmelfs_cn_callback);
594 void pohmelfs_config_exit(void)
596 struct pohmelfs_config *c, *tmp;
597 struct pohmelfs_config_group *g, *gtmp;
599 cn_del_callback(&pohmelfs_cn_id);
601 mutex_lock(&pohmelfs_config_lock);
602 list_for_each_entry_safe(g, gtmp, &pohmelfs_config_list, group_entry) {
603 list_for_each_entry_safe(c, tmp, &g->config_list, config_entry) {
604 list_del(&c->config_entry);
608 list_del(&g->group_entry);
611 kfree(g->hash_string);
613 if (g->cipher_string)
614 kfree(g->cipher_string);
618 mutex_unlock(&pohmelfs_config_lock);