netlink_dev.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:5k
源码类别:

嵌入式Linux

开发平台:

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 loff_t netlink_lseek(struct file * file, loff_t offset, int origin)
  88. {
  89. return -ESPIPE;
  90. }
  91. static int netlink_open(struct inode * inode, struct file * file)
  92. {
  93. unsigned int minor = MINOR(inode->i_rdev);
  94. struct socket *sock;
  95. struct sockaddr_nl nladdr;
  96. int err;
  97. if (minor>=MAX_LINKS)
  98. return -ENODEV;
  99. if (test_and_set_bit(minor, &open_map))
  100. return -EBUSY;
  101. err = sock_create(PF_NETLINK, SOCK_RAW, minor, &sock);
  102. if (err < 0)
  103. goto out;
  104. memset(&nladdr, 0, sizeof(nladdr));
  105. nladdr.nl_family = AF_NETLINK;
  106. nladdr.nl_groups = ~0;
  107. if ((err = sock->ops->bind(sock, (struct sockaddr*)&nladdr, sizeof(nladdr))) < 0) {
  108. sock_release(sock);
  109. goto out;
  110. }
  111. netlink_user[minor] = sock;
  112. return 0;
  113. out:
  114. clear_bit(minor, &open_map);
  115. return err;
  116. }
  117. static int netlink_release(struct inode * inode, struct file * file)
  118. {
  119. unsigned int minor = MINOR(inode->i_rdev);
  120. struct socket *sock;
  121. sock = netlink_user[minor];
  122. netlink_user[minor] = NULL;
  123. clear_bit(minor, &open_map);
  124. sock_release(sock);
  125. return 0;
  126. }
  127. static int netlink_ioctl(struct inode *inode, struct file *file,
  128.     unsigned int cmd, unsigned long arg)
  129. {
  130. unsigned int minor = MINOR(inode->i_rdev);
  131. int retval = 0;
  132. if (minor >= MAX_LINKS)
  133. return -ENODEV;
  134. switch ( cmd ) {
  135. default:
  136. retval = -EINVAL;
  137. }
  138. return retval;
  139. }
  140. static struct file_operations netlink_fops = {
  141. owner: THIS_MODULE,
  142. llseek: netlink_lseek,
  143. read: netlink_read,
  144. write: netlink_write,
  145. poll: netlink_poll,
  146. ioctl: netlink_ioctl,
  147. open: netlink_open,
  148. release: netlink_release,
  149. };
  150. static devfs_handle_t devfs_handle;
  151. static void __init make_devfs_entries (const char *name, int minor)
  152. {
  153. devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT,
  154. NETLINK_MAJOR, minor,
  155. S_IFCHR | S_IRUSR | S_IWUSR,
  156. &netlink_fops, NULL);
  157. }
  158. int __init init_netlink(void)
  159. {
  160. if (devfs_register_chrdev(NETLINK_MAJOR,"netlink", &netlink_fops)) {
  161. printk(KERN_ERR "netlink: unable to get major %dn", NETLINK_MAJOR);
  162. return -EIO;
  163. }
  164. devfs_handle = devfs_mk_dir (NULL, "netlink", NULL);
  165. /*  Someone tell me the official names for the uppercase ones  */
  166. make_devfs_entries ("route", 0);
  167. make_devfs_entries ("skip", 1);
  168. make_devfs_entries ("usersock", 2);
  169. make_devfs_entries ("fwmonitor", 3);
  170. make_devfs_entries ("tcpdiag", 4);
  171. make_devfs_entries ("arpd", 8);
  172. make_devfs_entries ("route6", 11);
  173. make_devfs_entries ("ip6_fw", 13);
  174. make_devfs_entries ("dnrtmsg", 13);
  175. devfs_register_series (devfs_handle, "tap%u", 16, DEVFS_FL_DEFAULT,
  176.        NETLINK_MAJOR, 16,
  177.        S_IFCHR | S_IRUSR | S_IWUSR,
  178.        &netlink_fops, NULL);
  179. return 0;
  180. }
  181. #ifdef MODULE
  182. int init_module(void)
  183. {
  184. printk(KERN_INFO "Network Kernel/User communications module 0.04n");
  185. return init_netlink();
  186. }
  187. void cleanup_module(void)
  188. {
  189. devfs_unregister (devfs_handle);
  190. devfs_unregister_chrdev(NETLINK_MAJOR, "netlink");
  191. }
  192. #endif