* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / netlink / netlink_dev.c
blobaa5bfc8860e180508769f704e9e72d5c62d39f51
1 /*
2 * NETLINK An implementation of a loadable kernel mode driver providing
3 * multiple kernel/user space bidirectional communications links.
5 * Author: Alan Cox <alan@redhat.com>
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.
12 * Now netlink devices are emulated on the top of netlink sockets
13 * by compatibility reasons. Remove this file after a period. --ANK
17 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/sched.h>
23 #include <linux/malloc.h>
24 #include <linux/skbuff.h>
25 #include <linux/netlink.h>
26 #include <linux/poll.h>
27 #include <linux/init.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
32 static unsigned open_map = 0;
33 static struct socket *netlink_user[MAX_LINKS];
36 * Device operations
39 static unsigned int netlink_poll(struct file *file, poll_table * wait)
41 struct socket *sock = netlink_user[MINOR(file->f_dentry->d_inode->i_rdev)];
43 if (sock->ops->poll==NULL)
44 return 0;
45 return sock->ops->poll(file, sock, wait);
49 * Write a message to the kernel side of a communication link
52 static ssize_t netlink_write(struct file * file, const char * buf,
53 size_t count, loff_t *pos)
55 struct inode *inode = file->f_dentry->d_inode;
56 struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
57 struct msghdr msg;
58 struct iovec iov;
60 iov.iov_base = (void*)buf;
61 iov.iov_len = count;
62 msg.msg_name=NULL;
63 msg.msg_namelen=0;
64 msg.msg_controllen=0;
65 msg.msg_flags=0;
66 msg.msg_iov=&iov;
67 msg.msg_iovlen=1;
69 return sock_sendmsg(sock, &msg, count);
73 * Read a message from the kernel side of the communication link
76 static ssize_t netlink_read(struct file * file, char * buf,
77 size_t count, loff_t *pos)
79 struct inode *inode = file->f_dentry->d_inode;
80 struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
81 struct msghdr msg;
82 struct iovec iov;
84 iov.iov_base = buf;
85 iov.iov_len = count;
86 msg.msg_name=NULL;
87 msg.msg_namelen=0;
88 msg.msg_controllen=0;
89 msg.msg_flags=0;
90 msg.msg_iov=&iov;
91 msg.msg_iovlen=1;
92 if (file->f_flags&O_NONBLOCK)
93 msg.msg_flags=MSG_DONTWAIT;
95 return sock_recvmsg(sock, &msg, count, msg.msg_flags);
98 static loff_t netlink_lseek(struct file * file, loff_t offset, int origin)
100 return -ESPIPE;
103 static int netlink_open(struct inode * inode, struct file * file)
105 unsigned int minor = MINOR(inode->i_rdev);
106 struct socket *sock;
107 struct sockaddr_nl nladdr;
108 int err;
110 if (minor>=MAX_LINKS)
111 return -ENODEV;
112 if (open_map&(1<<minor))
113 return -EBUSY;
115 open_map |= (1<<minor);
116 MOD_INC_USE_COUNT;
118 err = sock_create(PF_NETLINK, SOCK_RAW, minor, &sock);
119 if (err < 0)
120 goto out;
122 memset(&nladdr, 0, sizeof(nladdr));
123 nladdr.nl_family = AF_NETLINK;
124 nladdr.nl_groups = ~0;
125 if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
126 sock_release(sock);
127 goto out;
130 netlink_user[minor] = sock;
131 return 0;
133 out:
134 open_map &= ~(1<<minor);
135 MOD_DEC_USE_COUNT;
136 return err;
139 static int netlink_release(struct inode * inode, struct file * file)
141 unsigned int minor = MINOR(inode->i_rdev);
142 struct socket *sock = netlink_user[minor];
144 netlink_user[minor] = NULL;
145 open_map &= ~(1<<minor);
146 sock_release(sock);
147 MOD_DEC_USE_COUNT;
148 return 0;
152 static int netlink_ioctl(struct inode *inode, struct file *file,
153 unsigned int cmd, unsigned long arg)
155 unsigned int minor = MINOR(inode->i_rdev);
156 int retval = 0;
158 if (minor >= MAX_LINKS)
159 return -ENODEV;
160 switch ( cmd ) {
161 default:
162 retval = -EINVAL;
164 return retval;
168 static struct file_operations netlink_fops = {
169 netlink_lseek,
170 netlink_read,
171 netlink_write,
172 NULL, /* netlink_readdir */
173 netlink_poll,
174 netlink_ioctl,
175 NULL, /* netlink_mmap */
176 netlink_open,
177 NULL, /* flush */
178 netlink_release
181 int __init init_netlink(void)
183 if (register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
184 printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
185 return -EIO;
187 return 0;
190 #ifdef MODULE
192 int init_module(void)
194 printk(KERN_INFO "Network Kernel/User communications module 0.04\n");
195 return init_netlink();
198 void cleanup_module(void)
200 unregister_chrdev(NET_MAJOR,"netlink");
203 #endif