/* * connector.c * * 2004 Copyright (c) Evgeniy Polyakov * All rights reserved. * * 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 "../connector/connector.h" #include "../connector/cn_queue.h" MODULE_LICENSE("GPL"); MODULE_AUTHOR("Evgeniy Polyakov "); MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector."); static int unit = NETLINK_NFLOG; module_param(unit, int, 0); struct cn_dev cdev; /* * msg->seq and msg->ack are used to determine message genealogy. * When someone sends message it puts there locally unique sequence * and random acknowledge numbers. * Sequence number may be copied into nlmsghdr->nlmsg_seq too. * * Sequence number is incremented with each message to be sent. * * If we expect reply to our message, * then sequence number in received message MUST be the same as in original message, * and acknowledge number MUST be the same + 1. * * If we receive message and it's sequence number is not equal to one we are expecting, * then it is new message. * If we receive message and it's sequence number is the same as one we are expecting, * but it's acknowledge is not equal acknowledge number in original message + 1, * then it is new message. * */ void cn_netlink_send(struct cn_msg *msg) { struct cn_callback_entry *n, *__cbq; unsigned int size; struct sk_buff *skb; struct nlmsghdr *nlh; struct cn_msg *data; struct cn_dev *dev = &cdev; u32 groups = 0; int found = 0; spin_lock(&dev->cbdev->queue_lock); list_for_each_entry_safe(__cbq, n, &dev->cbdev->queue_list, callback_entry) { if (cn_cb_equal(&__cbq->cb->id, &msg->id)) { found = 1; groups = __cbq->group; } } spin_unlock(&dev->cbdev->queue_lock); if (!found) { printk(KERN_ERR "Failed to find multicast netlink group for callback[0x%x.0x%x]. seq=%u\n", msg->id.idx, msg->id.val, msg->seq); return; } size = NLMSG_SPACE(sizeof(*msg) + msg->len); skb = alloc_skb(size, GFP_ATOMIC); if (!skb) { printk(KERN_ERR "Failed to allocate new skb with size=%u.\n", size); return; } nlh = NLMSG_PUT(skb, 0, msg->seq, NLMSG_DONE, size - sizeof(*nlh)); data = (struct cn_msg *)NLMSG_DATA(nlh); memcpy(data, msg, sizeof(*data) + msg->len); #if 0 printk("%s: len=%u, seq=%u, ack=%u, group=%u.\n", __func__, msg->len, msg->seq, msg->ack, groups); #endif NETLINK_CB(skb).dst_groups = groups; netlink_broadcast(dev->nls, skb, 0, groups, GFP_ATOMIC); return; nlmsg_failure: printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack); kfree_skb(skb); return; } static int cn_call_callback(struct cn_msg *msg, void (*destruct_data) (void *), void *data) { struct cn_callback_entry *n, *__cbq; struct cn_dev *dev = &cdev; int found = 0; spin_lock(&dev->cbdev->queue_lock); list_for_each_entry_safe(__cbq, n, &dev->cbdev->queue_list, callback_entry) { if (cn_cb_equal(&__cbq->cb->id, &msg->id)) { __cbq->cb->priv = msg; __cbq->ddata = data; __cbq->destruct_data = destruct_data; queue_work(dev->cbdev->cn_queue, &__cbq->work); found = 1; break; } } spin_unlock(&dev->cbdev->queue_lock); return found; } static int __cn_rx_skb(struct sk_buff *skb, struct nlmsghdr *nlh) { u32 pid, uid, seq, group; struct cn_msg *msg; pid = NETLINK_CREDS(skb)->pid; uid = NETLINK_CREDS(skb)->uid; seq = nlh->nlmsg_seq; group = NETLINK_CB((skb)).groups; msg = (struct cn_msg *)NLMSG_DATA(nlh); #if 0 printk(KERN_INFO "pid=%u, uid=%u, seq=%u, group=%u.\n", pid, uid, seq, group); #endif return cn_call_callback(msg, (void (*)(void *))kfree_skb, skb); } static void cn_rx_skb(struct sk_buff *__skb) { struct nlmsghdr *nlh; u32 len; int err; struct sk_buff *skb; skb = skb_get(__skb); if (!skb) { printk(KERN_ERR "Failed to reference an skb.\n"); return; } #if 0 printk(KERN_INFO "skb: len=%u, data_len=%u, truesize=%u, proto=%u, cloned=%d, shared=%d.\n", skb->len, skb->data_len, skb->truesize, skb->protocol, skb_cloned(skb), skb_shared(skb)); #endif while (skb->len >= NLMSG_SPACE(0)) { nlh = (struct nlmsghdr *)skb->data; if (nlh->nlmsg_len < sizeof(struct cn_msg) || skb->len < nlh->nlmsg_len || nlh->nlmsg_len > CONNECTOR_MAX_MSG_SIZE) { printk(KERN_INFO "nlmsg_len=%u, sizeof(*nlh)=%u\n", nlh->nlmsg_len, sizeof(*nlh)); break; } len = NLMSG_ALIGN(nlh->nlmsg_len); if (len > skb->len) len = skb->len; err = __cn_rx_skb(skb, nlh); if (err) { if (err < 0) netlink_ack(skb, nlh, -err); kfree_skb(skb); break; } else { if (nlh->nlmsg_flags & NLM_F_ACK) netlink_ack(skb, nlh, 0); kfree_skb(skb); break; } skb_pull(skb, len); } } static void cn_input(struct sock *sk, int len) { struct sk_buff *skb; while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) cn_rx_skb(skb); } int cn_add_callback(struct cb_id *id, char *name, void (*callback) (void *)) { int err; struct cn_dev *dev = &cdev; struct cn_callback *cb; cb = kmalloc(sizeof(*cb), GFP_KERNEL); if (!cb) { printk(KERN_INFO "%s: Failed to allocate new struct cn_callback.\n", dev->cbdev->name); return -ENOMEM; } memset(cb, 0, sizeof(*cb)); snprintf(cb->name, sizeof(cb->name), "%s", name); memcpy(&cb->id, id, sizeof(cb->id)); cb->callback = callback; atomic_set(&cb->refcnt, 0); err = cn_queue_add_callback(dev->cbdev, cb); if (err) { kfree(cb); return err; } return 0; } void cn_del_callback(struct cb_id *id) { struct cn_dev *dev = &cdev; struct cn_callback_entry *n, *__cbq; list_for_each_entry_safe(__cbq, n, &dev->cbdev->queue_list, callback_entry) { if (cn_cb_equal(&__cbq->cb->id, id)) { cn_queue_del_callback(dev->cbdev, __cbq->cb); break; } } } static int cn_init(void) { struct cn_dev *dev = &cdev; dev->input = cn_input; dev->nls = netlink_kernel_create(unit, dev->input); if (!dev->nls) { printk(KERN_ERR "Failed to create new netlink socket(%u).\n", unit); return -EIO; } dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls); if (!dev->cbdev) { if (dev->nls->sk_socket) sock_release(dev->nls->sk_socket); return -EINVAL; } return 0; } static void cn_fini(void) { struct cn_dev *dev = &cdev; cn_queue_free_dev(dev->cbdev); if (dev->nls->sk_socket) sock_release(dev->nls->sk_socket); } module_init(cn_init); module_exit(cn_fini); EXPORT_SYMBOL(cn_add_callback); EXPORT_SYMBOL(cn_del_callback); EXPORT_SYMBOL(cn_netlink_send);