/* * crypto_conn.c * * Copyright (c) 2004 Evgeniy Polyakov * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include "acrypto.h" #include "crypto_lb.h" #include "../connector/connector.h" #include "crypto_conn.h" static struct cb_id crypto_conn_id = { 0xdead, 0x0000 }; static char crypto_conn_name[] = "crconn"; static void crypto_conn_callback(void *data) { struct cn_msg *msg, *reply; struct crypto_conn_data *d, *cmd; struct crypto_device *dev; u32 sessions; msg = (struct cn_msg *)data; d = (struct crypto_conn_data *)msg->data; if (msg->len < sizeof(*d)) { dprintk(KERN_ERR "Wrong message to crypto connector: msg->len=%u < %u.\n", msg->len, sizeof(*d)); return; } if (msg->len != sizeof(*d) + d->len) { dprintk(KERN_ERR "Wrong message to crypto connector: msg->len=%u != %u.\n", msg->len, sizeof(*d) + d->len); return; } dev = crypto_device_get_name(d->name); if (!dev) { dprintk(KERN_INFO "Crypto device %s was not found.\n", d->name); return; } switch (d->cmd) { case CRYPTO_CONN_READ_SESSIONS: reply = kmalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(sessions), GFP_ATOMIC); if (reply) { memcpy(reply, msg, sizeof(*reply)); reply->len = sizeof(*cmd) + sizeof(sessions); /* * See protocol description in connector.c */ reply->ack++; cmd = (struct crypto_conn_data *)(reply + 1); memcpy(cmd, d, sizeof(*cmd)); cmd->len = sizeof(sessions); sessions = atomic_read(&dev->refcnt); memcpy(cmd+1, &sessions, sizeof(sessions)); cn_netlink_send(reply, 0); kfree(reply); } else dprintk(KERN_ERR "Failed to allocate %d bytes in reply to comamnd 0x%x.\n", sizeof(*msg) + sizeof(*cmd), d->cmd); break; case CRYPTO_CONN_DUMP_QUEUE: reply = kmalloc(sizeof(*msg) + sizeof(*cmd) + 1024*sizeof(struct crypto_session_initializer), GFP_ATOMIC); if (reply) { struct crypto_session *s; struct crypto_session_initializer *ptr; memcpy(reply, msg, sizeof(*reply)); /* * See protocol description in connector.c */ reply->ack++; cmd = (struct crypto_conn_data *)(reply + 1); memcpy(cmd, d, sizeof(*cmd)); ptr = (struct crypto_session_initializer *)(cmd+1); sessions = 0; spin_lock_bh(&dev->session_lock); list_for_each_entry(s, &dev->session_list, dev_queue_entry) { memcpy(ptr, &s->ci, sizeof(*ptr)); sessions++; ptr++; if (sessions >= 1024) break; } spin_unlock_bh(&dev->session_lock); cmd->len = sizeof(*ptr)*sessions; reply->len = sizeof(*cmd) + cmd->len; cn_netlink_send(reply, 0); kfree(reply); } else dprintk(KERN_ERR "Failed to allocate %d bytes in reply to comamnd 0x%x.\n", sizeof(*msg) + sizeof(*cmd), d->cmd); break; case CRYPTO_GET_STAT: reply = kmalloc(sizeof(*msg) + sizeof(*cmd) + sizeof(struct crypto_device_stat), GFP_ATOMIC); if (reply) { struct crypto_device_stat *ptr; memcpy(reply, msg, sizeof(*reply)); reply->len = sizeof(*cmd) + sizeof(*ptr); /* * See protocol description in connector.c */ reply->ack++; cmd = (struct crypto_conn_data *)(reply + 1); memcpy(cmd, d, sizeof(*cmd)); cmd->len = sizeof(*ptr); ptr = (struct crypto_device_stat *)(cmd+1); memcpy(ptr, &dev->stat, sizeof(*ptr)); cn_netlink_send(reply, 0); kfree(reply); } else dprintk(KERN_ERR "Failed to allocate %d bytes in reply to comamnd 0x%x.\n", sizeof(*msg) + sizeof(*cmd), d->cmd); break; default: dprintk(KERN_ERR "Wrong operation 0x%04x for crypto connector.\n", d->cmd); return; } crypto_device_put(dev); } int crypto_conn_init(void) { int err; err = cn_add_callback(&crypto_conn_id, crypto_conn_name, crypto_conn_callback); if (err) return err; dprintk(KERN_INFO "Crypto connector callback is registered.\n"); return 0; } void crypto_conn_fini(void) { cn_del_callback(&crypto_conn_id); dprintk(KERN_INFO "Crypto connector callback is unregistered.\n"); }