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>
31 #include <asm/bitops.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
36 static struct socket *netlink_user[MAX_LINKS];
42 static unsigned int netlink_poll(struct file *file, poll_table * wait)
44 struct socket *sock = netlink_user[MINOR(file->f_dentry->d_inode->i_rdev)];
46 if (sock->ops->poll==NULL)
48 return sock->ops->poll(file, sock, wait);
52 * Write a message to the kernel side of a communication link
55 static ssize_t netlink_write(struct file * file, const char * buf,
56 size_t count, loff_t *pos)
58 struct inode *inode = file->f_dentry->d_inode;
59 struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
63 iov.iov_base = (void*)buf;
72 return sock_sendmsg(sock, &msg, count);
76 * Read a message from the kernel side of the communication link
79 static ssize_t netlink_read(struct file * file, char * buf,
80 size_t count, loff_t *pos)
82 struct inode *inode = file->f_dentry->d_inode;
83 struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
95 if (file->f_flags&O_NONBLOCK)
96 msg.msg_flags=MSG_DONTWAIT;
98 return sock_recvmsg(sock, &msg, count, msg.msg_flags);
101 static int netlink_open(struct inode * inode, struct file * file)
103 unsigned int minor = MINOR(inode->i_rdev);
105 struct sockaddr_nl nladdr;
108 if (minor>=MAX_LINKS)
110 if (test_and_set_bit(minor, &open_map))
113 err = sock_create(PF_NETLINK, SOCK_RAW, minor, &sock);
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) {
125 netlink_user[minor] = sock;
129 clear_bit(minor, &open_map);
133 static int netlink_release(struct inode * inode, struct file * file)
135 unsigned int minor = MINOR(inode->i_rdev);
138 sock = netlink_user[minor];
139 netlink_user[minor] = NULL;
140 clear_bit(minor, &open_map);
146 static int netlink_ioctl(struct inode *inode, struct file *file,
147 unsigned int cmd, unsigned long arg)
149 unsigned int minor = MINOR(inode->i_rdev);
152 if (minor >= MAX_LINKS)
162 static struct file_operations netlink_fops = {
166 write: netlink_write,
168 ioctl: netlink_ioctl,
170 release: netlink_release,
173 static devfs_handle_t devfs_handle;
175 static void __init make_devfs_entries (const char *name, int minor)
177 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT,
178 NETLINK_MAJOR, minor,
179 S_IFCHR | S_IRUSR | S_IWUSR,
180 &netlink_fops, NULL);
183 int __init init_netlink(void)
185 if (devfs_register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
186 printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
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,
202 S_IFCHR | S_IRUSR | S_IWUSR,
203 &netlink_fops, NULL);
209 MODULE_LICENSE("GPL");
211 int init_module(void)
213 printk(KERN_INFO "Network Kernel/User communications module 0.04\n");
214 return init_netlink();
217 void cleanup_module(void)
219 devfs_unregister (devfs_handle);
220 devfs_unregister_chrdev(NETLINK_MAJOR, "netlink");