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/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 #include <linux/device.h>
31 #include <linux/bitops.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
37 static struct socket *netlink_user[MAX_LINKS];
38 static struct class_simple *netlink_class;
44 static unsigned int netlink_poll(struct file *file, poll_table * wait)
46 struct socket *sock = netlink_user[iminor(file->f_dentry->d_inode)];
48 if (sock->ops->poll==NULL)
50 return sock->ops->poll(file, sock, wait);
54 * Write a message to the kernel side of a communication link
57 static ssize_t netlink_write(struct file * file, const char __user * buf,
58 size_t count, loff_t *pos)
60 struct inode *inode = file->f_dentry->d_inode;
61 struct socket *sock = netlink_user[iminor(inode)];
65 iov.iov_base = (void __user*)buf;
74 return sock_sendmsg(sock, &msg, count);
78 * Read a message from the kernel side of the communication link
81 static ssize_t netlink_read(struct file * file, char __user * buf,
82 size_t count, loff_t *pos)
84 struct inode *inode = file->f_dentry->d_inode;
85 struct socket *sock = netlink_user[iminor(inode)];
97 if (file->f_flags&O_NONBLOCK)
98 msg.msg_flags=MSG_DONTWAIT;
100 return sock_recvmsg(sock, &msg, count, msg.msg_flags);
103 static int netlink_open(struct inode * inode, struct file * file)
105 unsigned int minor = iminor(inode);
107 struct sockaddr_nl nladdr;
110 if (minor>=MAX_LINKS)
112 if (test_and_set_bit(minor, &open_map))
115 err = sock_create_kern(PF_NETLINK, SOCK_RAW, minor, &sock);
119 memset(&nladdr, 0, sizeof(nladdr));
120 nladdr.nl_family = AF_NETLINK;
121 nladdr.nl_groups = ~0;
122 if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
127 netlink_user[minor] = sock;
131 clear_bit(minor, &open_map);
135 static int netlink_release(struct inode * inode, struct file * file)
137 unsigned int minor = iminor(inode);
140 sock = netlink_user[minor];
141 netlink_user[minor] = NULL;
142 clear_bit(minor, &open_map);
148 static int netlink_ioctl(struct inode *inode, struct file *file,
149 unsigned int cmd, unsigned long arg)
151 unsigned int minor = iminor(inode);
154 if (minor >= MAX_LINKS)
164 static struct file_operations netlink_fops = {
165 .owner = THIS_MODULE,
167 .read = netlink_read,
168 .write = netlink_write,
169 .poll = netlink_poll,
170 .ioctl = netlink_ioctl,
171 .open = netlink_open,
172 .release = netlink_release,
181 .minor = NETLINK_ROUTE,
185 .minor = NETLINK_SKIP,
189 .minor = NETLINK_USERSOCK,
193 .minor = NETLINK_FIREWALL,
197 .minor = NETLINK_TCPDIAG,
201 .minor = NETLINK_NFLOG,
205 .minor = NETLINK_XFRM,
209 .minor = NETLINK_ARPD,
213 .minor = NETLINK_ROUTE6,
217 .minor = NETLINK_IP6_FW,
221 .minor = NETLINK_DNRTMSG,
225 static int __init init_netlink(void)
229 if (register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
230 printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
234 netlink_class = class_simple_create(THIS_MODULE, "netlink");
235 if (IS_ERR(netlink_class)) {
236 printk (KERN_ERR "Error creating netlink class.\n");
237 unregister_chrdev(NETLINK_MAJOR, "netlink");
238 return PTR_ERR(netlink_class);
241 devfs_mk_dir("netlink");
243 /* Someone tell me the official names for the uppercase ones */
244 for (i = 0; i < ARRAY_SIZE(entries); i++) {
245 devfs_mk_cdev(MKDEV(NETLINK_MAJOR, entries[i].minor),
246 S_IFCHR|S_IRUSR|S_IWUSR, "netlink/%s", entries[i].name);
247 class_simple_device_add(netlink_class, MKDEV(NETLINK_MAJOR, entries[i].minor), NULL, "%s", entries[i].name);
250 for (i = 0; i < 16; i++) {
251 devfs_mk_cdev(MKDEV(NETLINK_MAJOR, i + 16),
252 S_IFCHR|S_IRUSR|S_IWUSR, "netlink/tap%d", i);
253 class_simple_device_add(netlink_class, MKDEV(NETLINK_MAJOR, i + 16), NULL, "tap%d", i);
259 static void __exit cleanup_netlink(void)
263 for (i = 0; i < ARRAY_SIZE(entries); i++) {
264 devfs_remove("netlink/%s", entries[i].name);
265 class_simple_device_remove(MKDEV(NETLINK_MAJOR, entries[i].minor));
267 for (i = 0; i < 16; i++) {
268 devfs_remove("netlink/tap%d", i);
269 class_simple_device_remove(MKDEV(NETLINK_MAJOR, i + 16));
271 devfs_remove("netlink");
272 class_simple_destroy(netlink_class);
273 unregister_chrdev(NETLINK_MAJOR, "netlink");
276 MODULE_LICENSE("GPL");
277 module_init(init_netlink);
278 module_exit(cleanup_netlink);