vhci_sysfs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <linux/kthread.h>
  20. #include <linux/file.h>
  21. #include <linux/net.h>
  22. #include "usbip_common.h"
  23. #include "vhci.h"
  24. /* TODO: refine locking ?*/
  25. /* Sysfs entry to show port status */
  26. static ssize_t status_show(struct device *dev, struct device_attribute *attr,
  27. char *out)
  28. {
  29. char *s = out;
  30. int i = 0;
  31. unsigned long flags;
  32. BUG_ON(!the_controller || !out);
  33. spin_lock_irqsave(&the_controller->lock, flags);
  34. /*
  35. * output example:
  36. * port sta spd dev sockfd local_busid
  37. * 0000 004 000 00000000 000003 1-2.3
  38. * 0001 004 000 00000000 000004 2-3.4
  39. *
  40. * Output includes socket fd instead of socket pointer address to
  41. * avoid leaking kernel memory address in:
  42. * /sys/devices/platform/vhci_hcd.0/status and in debug output.
  43. * The socket pointer address is not used at the moment and it was
  44. * made visible as a convenient way to find IP address from socket
  45. * pointer address by looking up /proc/net/{tcp,tcp6}. As this opens
  46. * a security hole, the change is made to use sockfd instead.
  47. */
  48. out += sprintf(out,
  49. "prt sta spd dev sockfd local_busid\n");
  50. for (i = 0; i < VHCI_NPORTS; i++) {
  51. struct vhci_device *vdev = port_to_vdev(i);
  52. spin_lock(&vdev->ud.lock);
  53. out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
  54. if (vdev->ud.status == VDEV_ST_USED) {
  55. out += sprintf(out, "%03u %08x ",
  56. vdev->speed, vdev->devid);
  57. out += sprintf(out, "%06u ", vdev->ud.sockfd);
  58. out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
  59. } else
  60. out += sprintf(out, "000 00000000 000000 0-0");
  61. out += sprintf(out, "\n");
  62. spin_unlock(&vdev->ud.lock);
  63. }
  64. spin_unlock_irqrestore(&the_controller->lock, flags);
  65. return out - s;
  66. }
  67. static DEVICE_ATTR_RO(status);
  68. /* Sysfs entry to shutdown a virtual connection */
  69. static int vhci_port_disconnect(__u32 rhport)
  70. {
  71. struct vhci_device *vdev;
  72. unsigned long flags;
  73. usbip_dbg_vhci_sysfs("enter\n");
  74. /* lock */
  75. spin_lock_irqsave(&the_controller->lock, flags);
  76. vdev = port_to_vdev(rhport);
  77. spin_lock(&vdev->ud.lock);
  78. if (vdev->ud.status == VDEV_ST_NULL) {
  79. pr_err("not connected %d\n", vdev->ud.status);
  80. /* unlock */
  81. spin_unlock(&vdev->ud.lock);
  82. spin_unlock_irqrestore(&the_controller->lock, flags);
  83. return -EINVAL;
  84. }
  85. /* unlock */
  86. spin_unlock(&vdev->ud.lock);
  87. spin_unlock_irqrestore(&the_controller->lock, flags);
  88. usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
  89. return 0;
  90. }
  91. static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
  92. const char *buf, size_t count)
  93. {
  94. int err;
  95. __u32 rhport = 0;
  96. if (sscanf(buf, "%u", &rhport) != 1)
  97. return -EINVAL;
  98. /* check rhport */
  99. if (rhport >= VHCI_NPORTS) {
  100. dev_err(dev, "invalid port %u\n", rhport);
  101. return -EINVAL;
  102. }
  103. err = vhci_port_disconnect(rhport);
  104. if (err < 0)
  105. return -EINVAL;
  106. usbip_dbg_vhci_sysfs("Leave\n");
  107. return count;
  108. }
  109. static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
  110. /* Sysfs entry to establish a virtual connection */
  111. static int valid_args(__u32 rhport, enum usb_device_speed speed)
  112. {
  113. /* check rhport */
  114. if (rhport >= VHCI_NPORTS) {
  115. pr_err("port %u\n", rhport);
  116. return -EINVAL;
  117. }
  118. /* check speed */
  119. switch (speed) {
  120. case USB_SPEED_LOW:
  121. case USB_SPEED_FULL:
  122. case USB_SPEED_HIGH:
  123. case USB_SPEED_WIRELESS:
  124. break;
  125. default:
  126. pr_err("Failed attach request for unsupported USB speed: %s\n",
  127. usb_speed_string(speed));
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. /*
  133. * To start a new USB/IP attachment, a userland program needs to setup a TCP
  134. * connection and then write its socket descriptor with remote device
  135. * information into this sysfs file.
  136. *
  137. * A remote device is virtually attached to the root-hub port of @rhport with
  138. * @speed. @devid is embedded into a request to specify the remote device in a
  139. * server host.
  140. *
  141. * write() returns 0 on success, else negative errno.
  142. */
  143. static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
  144. const char *buf, size_t count)
  145. {
  146. struct vhci_device *vdev;
  147. struct socket *socket;
  148. int sockfd = 0;
  149. __u32 rhport = 0, devid = 0, speed = 0;
  150. int err;
  151. unsigned long flags;
  152. /*
  153. * @rhport: port number of vhci_hcd
  154. * @sockfd: socket descriptor of an established TCP connection
  155. * @devid: unique device identifier in a remote host
  156. * @speed: usb device speed in a remote host
  157. */
  158. if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4)
  159. return -EINVAL;
  160. usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
  161. rhport, sockfd, devid, speed);
  162. /* check received parameters */
  163. if (valid_args(rhport, speed) < 0)
  164. return -EINVAL;
  165. /* Extract socket from fd. */
  166. socket = sockfd_lookup(sockfd, &err);
  167. if (!socket)
  168. return -EINVAL;
  169. /* now need lock until setting vdev status as used */
  170. /* begin a lock */
  171. spin_lock_irqsave(&the_controller->lock, flags);
  172. vdev = port_to_vdev(rhport);
  173. spin_lock(&vdev->ud.lock);
  174. if (vdev->ud.status != VDEV_ST_NULL) {
  175. /* end of the lock */
  176. spin_unlock(&vdev->ud.lock);
  177. spin_unlock_irqrestore(&the_controller->lock, flags);
  178. sockfd_put(socket);
  179. dev_err(dev, "port %d already used\n", rhport);
  180. return -EINVAL;
  181. }
  182. dev_info(dev,
  183. "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n",
  184. rhport, sockfd, devid, speed, usb_speed_string(speed));
  185. vdev->devid = devid;
  186. vdev->speed = speed;
  187. vdev->ud.sockfd = sockfd;
  188. vdev->ud.tcp_socket = socket;
  189. vdev->ud.status = VDEV_ST_NOTASSIGNED;
  190. spin_unlock(&vdev->ud.lock);
  191. spin_unlock_irqrestore(&the_controller->lock, flags);
  192. /* end the lock */
  193. vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
  194. vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
  195. rh_port_connect(rhport, speed);
  196. return count;
  197. }
  198. static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
  199. static struct attribute *dev_attrs[] = {
  200. &dev_attr_status.attr,
  201. &dev_attr_detach.attr,
  202. &dev_attr_attach.attr,
  203. &dev_attr_usbip_debug.attr,
  204. NULL,
  205. };
  206. const struct attribute_group dev_attr_group = {
  207. .attrs = dev_attrs,
  208. };