linux-old/net/netlink/netlink_dev.c
<<
>>
Prefs
   1/*
   2 * NETLINK      An implementation of a loadable kernel mode driver providing
   3 *              multiple kernel/user space bidirectional communications links.
   4 *
   5 *              Author:         Alan Cox <alan@redhat.com>
   6 *
   7 *              This program is free software; you can redistribute it and/or
   8 *              modify it under the terms of the GNU General Public License
   9 *              as published by the Free Software Foundation; either version
  10 *              2 of the License, or (at your option) any later version.
  11 * 
  12 *      Now netlink devices are emulated on the top of netlink sockets
  13 *      by compatibility reasons. Remove this file after a period. --ANK
  14 *
  15 */
  16
  17#include <linux/module.h>
  18
  19#include <linux/errno.h>
  20#include <linux/kernel.h>
  21#include <linux/major.h>
  22#include <linux/sched.h>
  23#include <linux/slab.h>
  24#include <linux/skbuff.h>
  25#include <linux/netlink.h>
  26#include <linux/poll.h>
  27#include <linux/init.h>
  28#include <linux/devfs_fs_kernel.h>
  29#include <linux/smp_lock.h>
  30
  31#include <asm/bitops.h>
  32#include <asm/system.h>
  33#include <asm/uaccess.h>
  34
  35static long open_map;
  36static struct socket *netlink_user[MAX_LINKS];
  37
  38/*
  39 *      Device operations
  40 */
  41 
  42static unsigned int netlink_poll(struct file *file, poll_table * wait)
  43{
  44        struct socket *sock = netlink_user[MINOR(file->f_dentry->d_inode->i_rdev)];
  45
  46        if (sock->ops->poll==NULL)
  47                return 0;
  48        return sock->ops->poll(file, sock, wait);
  49}
  50
  51/*
  52 *      Write a message to the kernel side of a communication link
  53 */
  54 
  55static ssize_t netlink_write(struct file * file, const char * buf,
  56                             size_t count, loff_t *pos)
  57{
  58        struct inode *inode = file->f_dentry->d_inode;
  59        struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
  60        struct msghdr msg;
  61        struct iovec iov;
  62
  63        iov.iov_base = (void*)buf;
  64        iov.iov_len = count;
  65        msg.msg_name=NULL;
  66        msg.msg_namelen=0;
  67        msg.msg_controllen=0;
  68        msg.msg_flags=0;
  69        msg.msg_iov=&iov;
  70        msg.msg_iovlen=1;
  71
  72        return sock_sendmsg(sock, &msg, count);
  73}
  74
  75/*
  76 *      Read a message from the kernel side of the communication link
  77 */
  78
  79static ssize_t netlink_read(struct file * file, char * buf,
  80                            size_t count, loff_t *pos)
  81{
  82        struct inode *inode = file->f_dentry->d_inode;
  83        struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
  84        struct msghdr msg;
  85        struct iovec iov;
  86
  87        iov.iov_base = buf;
  88        iov.iov_len = count;
  89        msg.msg_name=NULL;
  90        msg.msg_namelen=0;
  91        msg.msg_controllen=0;
  92        msg.msg_flags=0;
  93        msg.msg_iov=&iov;
  94        msg.msg_iovlen=1;
  95        if (file->f_flags&O_NONBLOCK)
  96                msg.msg_flags=MSG_DONTWAIT;
  97
  98        return sock_recvmsg(sock, &msg, count, msg.msg_flags);
  99}
 100
 101static int netlink_open(struct inode * inode, struct file * file)
 102{
 103        unsigned int minor = MINOR(inode->i_rdev);
 104        struct socket *sock;
 105        struct sockaddr_nl nladdr;
 106        int err;
 107
 108        if (minor>=MAX_LINKS)
 109                return -ENODEV;
 110        if (test_and_set_bit(minor, &open_map))
 111                return -EBUSY;
 112
 113        err = sock_create(PF_NETLINK, SOCK_RAW, minor, &sock);
 114        if (err < 0)
 115                goto out;
 116
 117        memset(&nladdr, 0, sizeof(nladdr));
 118        nladdr.nl_family = AF_NETLINK;
 119        nladdr.nl_groups = ~0;
 120        if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
 121                sock_release(sock);
 122                goto out;
 123        }
 124
 125        netlink_user[minor] = sock;
 126        return 0;
 127
 128out:
 129        clear_bit(minor, &open_map);
 130        return err;
 131}
 132
 133static int netlink_release(struct inode * inode, struct file * file)
 134{
 135        unsigned int minor = MINOR(inode->i_rdev);
 136        struct socket *sock;
 137
 138        sock = netlink_user[minor];
 139        netlink_user[minor] = NULL;
 140        clear_bit(minor, &open_map);
 141        sock_release(sock);
 142        return 0;
 143}
 144
 145
 146static int netlink_ioctl(struct inode *inode, struct file *file,
 147                    unsigned int cmd, unsigned long arg)
 148{
 149        unsigned int minor = MINOR(inode->i_rdev);
 150        int retval = 0;
 151
 152        if (minor >= MAX_LINKS)
 153                return -ENODEV;
 154        switch ( cmd ) {
 155                default:
 156                        retval = -EINVAL;
 157        }
 158        return retval;
 159}
 160
 161
 162static struct file_operations netlink_fops = {
 163        owner:          THIS_MODULE,
 164        llseek:         no_llseek,
 165        read:           netlink_read,
 166        write:          netlink_write,
 167        poll:           netlink_poll,
 168        ioctl:          netlink_ioctl,
 169        open:           netlink_open,
 170        release:        netlink_release,
 171};
 172
 173static devfs_handle_t devfs_handle;
 174
 175static void __init make_devfs_entries (const char *name, int minor)
 176{
 177        devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT,
 178                        NETLINK_MAJOR, minor,
 179                        S_IFCHR | S_IRUSR | S_IWUSR,
 180                        &netlink_fops, NULL);
 181}
 182
 183int __init init_netlink(void)
 184{
 185        if (devfs_register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
 186                printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
 187                return -EIO;
 188        }
 189        devfs_handle = devfs_mk_dir (NULL, "netlink", NULL);
 190        /*  Someone tell me the official names for the uppercase ones  */
 191        make_devfs_entries ("route", 0);
 192        make_devfs_entries ("skip", 1);
 193        make_devfs_entries ("usersock", 2);
 194        make_devfs_entries ("fwmonitor", 3);
 195        make_devfs_entries ("tcpdiag", 4);
 196        make_devfs_entries ("arpd", 8);
 197        make_devfs_entries ("route6", 11);
 198        make_devfs_entries ("ip6_fw", 13);
 199        make_devfs_entries ("dnrtmsg", 13);
 200        devfs_register_series (devfs_handle, "tap%u", 16, DEVFS_FL_DEFAULT,
 201                               NETLINK_MAJOR, 16,
 202                               S_IFCHR | S_IRUSR | S_IWUSR,
 203                               &netlink_fops, NULL);
 204        return 0;
 205}
 206
 207#ifdef MODULE
 208
 209MODULE_LICENSE("GPL");
 210
 211int init_module(void)
 212{
 213        printk(KERN_INFO "Network Kernel/User communications module 0.04\n");
 214        return init_netlink();
 215}
 216
 217void cleanup_module(void)
 218{
 219        devfs_unregister (devfs_handle);
 220        devfs_unregister_chrdev(NETLINK_MAJOR, "netlink");
 221}
 222
 223#endif
 224
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.