netlink_dev.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/major.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netlink.h>
  24. #include <linux/poll.h>
  25. #include <linux/init.h>
  26. #include <linux/devfs_fs_kernel.h>
  27. #include <linux/smp_lock.h>
  28. #include <asm/bitops.h>
  29. #include <asm/system.h>
  30. #include <asm/uaccess.h>
  31. static long open_map;
  32. static struct socket *netlink_user[MAX_LINKS];
  33. /*
  34.  * Device operations
  35.  */
  36.  
  37. static unsigned int netlink_poll(struct file *file, poll_table * wait)
  38. {
  39. struct socket *sock = netlink_user[MINOR(file->f_dentry->d_inode->i_rdev)];
  40. if (sock->ops->poll==NULL)
  41. return 0;
  42. return sock->ops->poll(file, sock, wait);
  43. }
  44. /*
  45.  * Write a message to the kernel side of a communication link
  46.  */
  47.  
  48. static ssize_t netlink_write(struct file * file, const char * buf,
  49.      size_t count, loff_t *pos)
  50. {
  51. struct inode *inode = file->f_dentry->d_inode;
  52. struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
  53. struct msghdr msg;
  54. struct iovec iov;
  55. iov.iov_base = (void*)buf;
  56. iov.iov_len = count;
  57. msg.msg_name=NULL;
  58. msg.msg_namelen=0;
  59. msg.msg_controllen=0;
  60. msg.msg_flags=0;
  61. msg.msg_iov=&iov;
  62. msg.msg_iovlen=1;
  63. return sock_sendmsg(sock, &msg, count);
  64. }
  65. /*
  66.  * Read a message from the kernel side of the communication link
  67.  */
  68. static ssize_t netlink_read(struct file * file, char * buf,
  69.     size_t count, loff_t *pos)
  70. {
  71. struct inode *inode = file->f_dentry->d_inode;
  72. struct socket *sock = netlink_user[MINOR(inode->i_rdev)];
  73. struct msghdr msg;
  74. struct iovec iov;
  75. iov.iov_base = buf;
  76. iov.iov_len = count;
  77. msg.msg_name=NULL;
  78. msg.msg_namelen=0;
  79. msg.msg_controllen=0;
  80. msg.msg_flags=0;
  81. msg.msg_iov=&iov;
  82. msg.msg_iovlen=1;
  83. if (file->f_flags&O_NONBLOCK)
  84. msg.msg_flags=MSG_DONTWAIT;
  85. return sock_recvmsg(sock, &msg, count, msg.msg_flags);
  86. }
  87. static int netlink_open(struct inode * inode, struct file * file)
  88. {
  89. unsigned int minor = MINOR(inode->i_rdev);
  90. struct socket *sock;
  91. struct sockaddr_nl nladdr;
  92. int err;
  93. if (minor>=MAX_LINKS)
  94. return -ENODEV;
  95. if (test_and_set_bit(minor, &open_map))
  96. return -EBUSY;
  97. err = sock_create(PF_NETLINK, SOCK_RAW, minor, &sock);
  98. if (err < 0)
  99. goto out;
  100. memset(&nladdr, 0, sizeof(nladdr));
  101. nladdr.nl_family = AF_NETLINK;
  102. nladdr.nl_groups = ~0;
  103. if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
  104. sock_release(sock);
  105. goto out;
  106. }
  107. netlink_user[minor] = sock;
  108. return 0;
  109. out:
  110. clear_bit(minor, &open_map);
  111. return err;
  112. }
  113. static int netlink_release(struct inode * inode, struct file * file)
  114. {
  115. unsigned int minor = MINOR(inode->i_rdev);
  116. struct socket *sock;
  117. sock = netlink_user[minor];
  118. netlink_user[minor] = NULL;
  119. clear_bit(minor, &open_map);
  120. sock_release(sock);
  121. return 0;
  122. }
  123. static int netlink_ioctl(struct inode *inode, struct file *file,
  124.     unsigned int cmd, unsigned long arg)
  125. {
  126. unsigned int minor = MINOR(inode->i_rdev);
  127. int retval = 0;
  128. if (minor >= MAX_LINKS)
  129. return -ENODEV;
  130. switch ( cmd ) {
  131. default:
  132. retval = -EINVAL;
  133. }
  134. return retval;
  135. }
  136. static struct file_operations netlink_fops = {
  137. owner: THIS_MODULE,
  138. llseek: no_llseek,
  139. read: netlink_read,
  140. write: netlink_write,
  141. poll: netlink_poll,
  142. ioctl: netlink_ioctl,
  143. open: netlink_open,
  144. release: netlink_release,
  145. };
  146. static devfs_handle_t devfs_handle;
  147. static void __init make_devfs_entries (const char *name, int minor)
  148. {
  149. devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT,
  150. NETLINK_MAJOR, minor,
  151. S_IFCHR | S_IRUSR | S_IWUSR,
  152. &netlink_fops, NULL);
  153. }
  154. int __init init_netlink(void)
  155. {
  156. if (devfs_register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
  157. printk(KERN_ERR "netlink: unable to get major %dn", NETLINK_MAJOR);
  158. return -EIO;
  159. }
  160. devfs_handle = devfs_mk_dir (NULL, "netlink", NULL);
  161. /*  Someone tell me the official names for the uppercase ones  */
  162. make_devfs_entries ("route", 0);
  163. make_devfs_entries ("skip", 1);
  164. make_devfs_entries ("usersock", 2);
  165. make_devfs_entries ("fwmonitor", 3);
  166. make_devfs_entries ("tcpdiag", 4);
  167. make_devfs_entries ("arpd", 8);
  168. make_devfs_entries ("route6", 11);
  169. make_devfs_entries ("ip6_fw", 13);
  170. make_devfs_entries ("dnrtmsg", 13);
  171. devfs_register_series (devfs_handle, "tap%u", 16, DEVFS_FL_DEFAULT,
  172.        NETLINK_MAJOR, 16,
  173.        S_IFCHR | S_IRUSR | S_IWUSR,
  174.        &netlink_fops, NULL);
  175. return 0;
  176. }
  177. #ifdef MODULE
  178. MODULE_LICENSE("GPL");
  179. int init_module(void)
  180. {
  181. printk(KERN_INFO "Network Kernel/User communications module 0.04n");
  182. return init_netlink();
  183. }
  184. void cleanup_module(void)
  185. {
  186. devfs_unregister (devfs_handle);
  187. devfs_unregister_chrdev(NETLINK_MAJOR, "netlink");
  188. }
  189. #endif