File: /usr/src/linux/net/netlink/netlink_dev.c
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/system.h>
32 #include <asm/uaccess.h>
33
34 static unsigned open_map;
35 static struct socket *netlink_user[MAX_LINKS];
36
37 /*
38 * Device operations
39 */
40
41 static unsigned int netlink_poll(struct file *file, poll_table * wait)
42 {
43 struct socket *sock = netlink_user[MINOR(file->f_dentry->d_inode->i_rdev)];
44
45 if (sock->ops->poll==NULL)
46 return 0;
47 return sock->ops->poll(file, sock, wait);
48 }
49
50 /*
51 * Write a message to the kernel side of a communication link
52 */
53
54 static ssize_t netlink_write(struct file * file, const char * buf,
55 size_t count, loff_t *pos)
56 {
57 struct inode *inode = file->f_dentry->d_inode;
58 struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
59 struct msghdr msg;
60 struct iovec iov;
61
62 iov.iov_base = (void*)buf;
63 iov.iov_len = count;
64 msg.msg_name=NULL;
65 msg.msg_namelen=0;
66 msg.msg_controllen=0;
67 msg.msg_flags=0;
68 msg.msg_iov=&iov;
69 msg.msg_iovlen=1;
70
71 return sock_sendmsg(sock, &msg, count);
72 }
73
74 /*
75 * Read a message from the kernel side of the communication link
76 */
77
78 static ssize_t netlink_read(struct file * file, char * buf,
79 size_t count, loff_t *pos)
80 {
81 struct inode *inode = file->f_dentry->d_inode;
82 struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
83 struct msghdr msg;
84 struct iovec iov;
85
86 iov.iov_base = buf;
87 iov.iov_len = count;
88 msg.msg_name=NULL;
89 msg.msg_namelen=0;
90 msg.msg_controllen=0;
91 msg.msg_flags=0;
92 msg.msg_iov=&iov;
93 msg.msg_iovlen=1;
94 if (file->f_flags&O_NONBLOCK)
95 msg.msg_flags=MSG_DONTWAIT;
96
97 return sock_recvmsg(sock, &msg, count, msg.msg_flags);
98 }
99
100 static loff_t netlink_lseek(struct file * file, loff_t offset, int origin)
101 {
102 return -ESPIPE;
103 }
104
105 static int netlink_open(struct inode * inode, struct file * file)
106 {
107 unsigned int minor = MINOR(inode->i_rdev);
108 struct socket *sock;
109 struct sockaddr_nl nladdr;
110 int err;
111
112 if (minor>=MAX_LINKS)
113 return -ENODEV;
114 if (open_map&(1<<minor))
115 return -EBUSY;
116
117 open_map |= (1<<minor);
118
119 err = sock_create(PF_NETLINK, SOCK_RAW, minor, &sock);
120 if (err < 0)
121 goto out;
122
123 memset(&nladdr, 0, sizeof(nladdr));
124 nladdr.nl_family = AF_NETLINK;
125 nladdr.nl_groups = ~0;
126 if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
127 sock_release(sock);
128 goto out;
129 }
130
131 netlink_user[minor] = sock;
132 return 0;
133
134 out:
135 open_map &= ~(1<<minor);
136 return err;
137 }
138
139 static int netlink_release(struct inode * inode, struct file * file)
140 {
141 unsigned int minor = MINOR(inode->i_rdev);
142 struct socket *sock;
143
144 lock_kernel();
145 sock = netlink_user[minor];
146 netlink_user[minor] = NULL;
147 open_map &= ~(1<<minor);
148 unlock_kernel();
149 sock_release(sock);
150 return 0;
151 }
152
153
154 static int netlink_ioctl(struct inode *inode, struct file *file,
155 unsigned int cmd, unsigned long arg)
156 {
157 unsigned int minor = MINOR(inode->i_rdev);
158 int retval = 0;
159
160 if (minor >= MAX_LINKS)
161 return -ENODEV;
162 switch ( cmd ) {
163 default:
164 retval = -EINVAL;
165 }
166 return retval;
167 }
168
169
170 static struct file_operations netlink_fops = {
171 owner: THIS_MODULE,
172 llseek: netlink_lseek,
173 read: netlink_read,
174 write: netlink_write,
175 poll: netlink_poll,
176 ioctl: netlink_ioctl,
177 open: netlink_open,
178 release: netlink_release,
179 };
180
181 static devfs_handle_t devfs_handle;
182
183 static void __init make_devfs_entries (const char *name, int minor)
184 {
185 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT,
186 NETLINK_MAJOR, minor,
187 S_IFCHR | S_IRUSR | S_IWUSR,
188 &netlink_fops, NULL);
189 }
190
191 int __init init_netlink(void)
192 {
193 if (devfs_register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
194 printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
195 return -EIO;
196 }
197 devfs_handle = devfs_mk_dir (NULL, "netlink", NULL);
198 /* Someone tell me the official names for the uppercase ones */
199 make_devfs_entries ("route", 0);
200 make_devfs_entries ("skip", 1);
201 make_devfs_entries ("USERSOCK", 2);
202 make_devfs_entries ("fwmonitor", 3);
203 make_devfs_entries ("ARPD", 8);
204 make_devfs_entries ("ROUTE6", 11);
205 make_devfs_entries ("IP6_FW", 13);
206 devfs_register_series (devfs_handle, "tap%u", 16, DEVFS_FL_DEFAULT,
207 NETLINK_MAJOR, 16,
208 S_IFCHR | S_IRUSR | S_IWUSR,
209 &netlink_fops, NULL);
210 return 0;
211 }
212
213 #ifdef MODULE
214
215 int init_module(void)
216 {
217 printk(KERN_INFO "Network Kernel/User communications module 0.04\n");
218 return init_netlink();
219 }
220
221 void cleanup_module(void)
222 {
223 devfs_unregister (devfs_handle);
224 devfs_unregister_chrdev(NETLINK_MAJOR, "netlink");
225 }
226
227 #endif
228