Import 2.1.118
[davej-history.git] / net / netlink / netlink_dev.c
blob288cfd9a9f251171bc27dac1b727b77518155a6f
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@cymru.net>
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 = -EINVAL;
119 if (net_families[PF_NETLINK]==NULL)
120 goto out;
122 err = -ENFILE;
123 if (!(sock = sock_alloc()))
124 goto out;
126 sock->type = SOCK_RAW;
128 if ((err = net_families[PF_NETLINK]->create(sock, minor)) < 0)
130 sock_release(sock);
131 goto out;
134 memset(&nladdr, 0, sizeof(nladdr));
135 nladdr.nl_family = AF_NETLINK;
136 nladdr.nl_groups = ~0;
137 if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
138 sock_release(sock);
139 goto out;
142 netlink_user[minor] = sock;
143 return 0;
145 out:
146 open_map &= ~(1<<minor);
147 return err;
150 static int netlink_release(struct inode * inode, struct file * file)
152 unsigned int minor = MINOR(inode->i_rdev);
153 struct socket *sock = netlink_user[minor];
155 netlink_user[minor] = NULL;
156 open_map &= ~(1<<minor);
157 sock_release(sock);
158 MOD_DEC_USE_COUNT;
159 return 0;
163 static int netlink_ioctl(struct inode *inode, struct file *file,
164 unsigned int cmd, unsigned long arg)
166 unsigned int minor = MINOR(inode->i_rdev);
167 int retval = 0;
169 if (minor >= MAX_LINKS)
170 return -ENODEV;
171 switch ( cmd ) {
172 default:
173 retval = -EINVAL;
175 return retval;
179 static struct file_operations netlink_fops = {
180 netlink_lseek,
181 netlink_read,
182 netlink_write,
183 NULL, /* netlink_readdir */
184 netlink_poll,
185 netlink_ioctl,
186 NULL, /* netlink_mmap */
187 netlink_open,
188 NULL, /* flush */
189 netlink_release
192 __initfunc(int init_netlink(void))
194 if (register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
195 printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
196 return -EIO;
198 return 0;
201 #ifdef MODULE
203 int init_module(void)
205 printk(KERN_INFO "Network Kernel/User communications module 0.04\n");
206 return init_netlink();
209 void cleanup_module(void)
211 unregister_chrdev(NET_MAJOR,"netlink");
214 #endif