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/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
];
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
)
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
)];
60 iov
.iov_base
= (void*)buf
;
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
)];
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
)
103 static int netlink_open(struct inode
* inode
, struct file
* file
)
105 unsigned int minor
= MINOR(inode
->i_rdev
);
107 struct sockaddr_nl nladdr
;
110 if (minor
>=MAX_LINKS
)
112 if (open_map
&(1<<minor
))
115 open_map
|= (1<<minor
);
118 err
= sock_create(PF_NETLINK
, SOCK_RAW
, minor
, &sock
);
122 memset(&nladdr
, 0, sizeof(nladdr
));
123 nladdr
.nl_family
= AF_NETLINK
;
124 nladdr
.nl_groups
= ~0;
125 if ((err
= sock
->ops
->bind(sock
, (struct sockaddr
*)&nladdr
, sizeof(nladdr
))) < 0) {
130 netlink_user
[minor
] = sock
;
134 open_map
&= ~(1<<minor
);
139 static int netlink_release(struct inode
* inode
, struct file
* file
)
141 unsigned int minor
= MINOR(inode
->i_rdev
);
142 struct socket
*sock
= netlink_user
[minor
];
144 netlink_user
[minor
] = NULL
;
145 open_map
&= ~(1<<minor
);
152 static int netlink_ioctl(struct inode
*inode
, struct file
*file
,
153 unsigned int cmd
, unsigned long arg
)
155 unsigned int minor
= MINOR(inode
->i_rdev
);
158 if (minor
>= MAX_LINKS
)
168 static struct file_operations netlink_fops
= {
172 NULL
, /* netlink_readdir */
175 NULL
, /* netlink_mmap */
181 int __init
init_netlink(void)
183 if (register_chrdev(NETLINK_MAJOR
,"netlink", &netlink_fops
)) {
184 printk(KERN_ERR
"netlink: unable to get major %d\n", NETLINK_MAJOR
);
192 int init_module(void)
194 printk(KERN_INFO
"Network Kernel/User communications module 0.04\n");
195 return init_netlink();
198 void cleanup_module(void)
200 unregister_chrdev(NET_MAJOR
,"netlink");