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 #include <linux/device.h>
31 #include <linux/bitops.h>
32
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35
36 static long open_map;
37 static struct socket *netlink_user[MAX_LINKS];
38 static struct class_simple *netlink_class;
39
40 /*
41 * Device operations
42 */
43
44 static unsigned int netlink_poll(struct file *file, poll_table * wait)
45 {
46 struct socket *sock = netlink_user[iminor(file->f_dentry->d_inode)];
47
48 if (sock->ops->poll==NULL)
49 return 0;
50 return sock->ops->poll(file, sock, wait);
51 }
52
53 /*
54 * Write a message to the kernel side of a communication link
55 */
56
57 static ssize_t netlink_write(struct file * file, const char __user * buf,
58 size_t count, loff_t *pos)
59 {
60 struct inode *inode = file->f_dentry->d_inode;
61 struct socket *sock = netlink_user[iminor(inode)];
62 struct msghdr msg;
63 struct iovec iov;
64
65 iov.iov_base = (void __user*)buf;
66 iov.iov_len = count;
67 msg.msg_name=NULL;
68 msg.msg_namelen=0;
69 msg.msg_controllen=0;
70 msg.msg_flags=0;
71 msg.msg_iov=&iov;
72 msg.msg_iovlen=1;
73
74 return sock_sendmsg(sock, &msg, count);
75 }
76
77 /*
78 * Read a message from the kernel side of the communication link
79 */
80
81 static ssize_t netlink_read(struct file * file, char __user * buf,
82 size_t count, loff_t *pos)
83 {
84 struct inode *inode = file->f_dentry->d_inode;
85 struct socket *sock = netlink_user[iminor(inode)];
86 struct msghdr msg;
87 struct iovec iov;
88
89 iov.iov_base = buf;
90 iov.iov_len = count;
91 msg.msg_name=NULL;
92 msg.msg_namelen=0;
93 msg.msg_controllen=0;
94 msg.msg_flags=0;
95 msg.msg_iov=&iov;
96 msg.msg_iovlen=1;
97 if (file->f_flags&O_NONBLOCK)
98 msg.msg_flags=MSG_DONTWAIT;
99
100 return sock_recvmsg(sock, &msg, count, msg.msg_flags);
101 }
102
103 static int netlink_open(struct inode * inode, struct file * file)
104 {
105 unsigned int minor = iminor(inode);
106 struct socket *sock;
107 struct sockaddr_nl nladdr;
108 int err;
109
110 if (minor>=MAX_LINKS)
111 return -ENODEV;
112 if (test_and_set_bit(minor, &open_map))
113 return -EBUSY;
114
115 err = sock_create_kern(PF_NETLINK, SOCK_RAW, minor, &sock);
116 if (err < 0)
117 goto out;
118
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) {
123 sock_release(sock);
124 goto out;
125 }
126
127 netlink_user[minor] = sock;
128 return 0;
129
130 out:
131 clear_bit(minor, &open_map);
132 return err;
133 }
134
135 static int netlink_release(struct inode * inode, struct file * file)
136 {
137 unsigned int minor = iminor(inode);
138 struct socket *sock;
139
140 sock = netlink_user[minor];
141 netlink_user[minor] = NULL;
142 clear_bit(minor, &open_map);
143 sock_release(sock);
144 return 0;
145 }
146
147
148 static int netlink_ioctl(struct inode *inode, struct file *file,
149 unsigned int cmd, unsigned long arg)
150 {
151 unsigned int minor = iminor(inode);
152 int retval = 0;
153
154 if (minor >= MAX_LINKS)
155 return -ENODEV;
156 switch ( cmd ) {
157 default:
158 retval = -EINVAL;
159 }
160 return retval;
161 }
162
163
164 static struct file_operations netlink_fops = {
165 .owner = THIS_MODULE,
166 .llseek = no_llseek,
167 .read = netlink_read,
168 .write = netlink_write,
169 .poll = netlink_poll,
170 .ioctl = netlink_ioctl,
171 .open = netlink_open,
172 .release = netlink_release,
173 };
174
175 static struct {
176 char *name;
177 int minor;
178 } entries[] = {
179 {
180 .name = "route",
181 .minor = NETLINK_ROUTE,
182 },
183 {
184 .name = "skip",
185 .minor = NETLINK_SKIP,
186 },
187 {
188 .name = "usersock",
189 .minor = NETLINK_USERSOCK,
190 },
191 {
192 .name = "fwmonitor",
193 .minor = NETLINK_FIREWALL,
194 },
195 {
196 .name = "tcpdiag",
197 .minor = NETLINK_TCPDIAG,
198 },
199 {
200 .name = "nflog",
201 .minor = NETLINK_NFLOG,
202 },
203 {
204 .name = "xfrm",
205 .minor = NETLINK_XFRM,
206 },
207 {
208 .name = "arpd",
209 .minor = NETLINK_ARPD,
210 },
211 {
212 .name = "route6",
213 .minor = NETLINK_ROUTE6,
214 },
215 {
216 .name = "ip6_fw",
217 .minor = NETLINK_IP6_FW,
218 },
219 {
220 .name = "dnrtmsg",
221 .minor = NETLINK_DNRTMSG,
222 },
223 };
224
225 static int __init init_netlink(void)
226 {
227 int i;
228
229 if (register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
230 printk(KERN_ERR "netlink: unable to get major %d\n", NETLINK_MAJOR);
231 return -EIO;
232 }
233
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);
239 }
240
241 devfs_mk_dir("netlink");
242
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);
248 }
249
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);
254 }
255
256 return 0;
257 }
258
259 static void __exit cleanup_netlink(void)
260 {
261 int i;
262
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));
266 }
267 for (i = 0; i < 16; i++) {
268 devfs_remove("netlink/tap%d", i);
269 class_simple_device_remove(MKDEV(NETLINK_MAJOR, i + 16));
270 }
271 devfs_remove("netlink");
272 class_simple_destroy(netlink_class);
273 unregister_chrdev(NETLINK_MAJOR, "netlink");
274 }
275
276 MODULE_LICENSE("GPL");
277 module_init(init_netlink);
278 module_exit(cleanup_netlink);
279
|
This page was automatically generated by the
LXR engine.
|