2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include <linux/kthread.h>
21 #include <linux/file.h>
22 #include <linux/net.h>
24 #include "usbip_common.h"
27 /* TODO: refine locking ?*/
29 /* Sysfs entry to show port status */
30 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
37 BUG_ON(!the_controller || !out);
39 spin_lock_irqsave(&the_controller->lock, flags);
43 * port sta spd dev sockfd local_busid
44 * 0000 004 000 00000000 000003 1-2.3
45 * 0001 004 000 00000000 000004 2-3.4
47 * Output includes socket fd instead of socket pointer address to
48 * avoid leaking kernel memory address in:
49 * /sys/devices/platform/vhci_hcd.0/status and in debug output.
50 * The socket pointer address is not used at the moment and it was
51 * made visible as a convenient way to find IP address from socket
52 * pointer address by looking up /proc/net/{tcp,tcp6}. As this opens
53 * a security hole, the change is made to use sockfd instead.
56 "prt sta spd dev sockfd local_busid\n");
58 for (i = 0; i < VHCI_NPORTS; i++) {
59 struct vhci_device *vdev = port_to_vdev(i);
61 spin_lock(&vdev->ud.lock);
62 out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
64 if (vdev->ud.status == VDEV_ST_USED) {
65 out += sprintf(out, "%03u %08x ",
66 vdev->speed, vdev->devid);
67 out += sprintf(out, "%06u ", vdev->ud.sockfd);
68 out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
71 out += sprintf(out, "000 00000000 000000 0-0");
73 out += sprintf(out, "\n");
74 spin_unlock(&vdev->ud.lock);
77 spin_unlock_irqrestore(&the_controller->lock, flags);
81 static DEVICE_ATTR_RO(status);
83 /* Sysfs entry to shutdown a virtual connection */
84 static int vhci_port_disconnect(__u32 rhport)
86 struct vhci_device *vdev;
89 usbip_dbg_vhci_sysfs("enter\n");
92 spin_lock_irqsave(&the_controller->lock, flags);
94 vdev = port_to_vdev(rhport);
96 spin_lock(&vdev->ud.lock);
97 if (vdev->ud.status == VDEV_ST_NULL) {
98 pr_err("not connected %d\n", vdev->ud.status);
101 spin_unlock(&vdev->ud.lock);
102 spin_unlock_irqrestore(&the_controller->lock, flags);
108 spin_unlock(&vdev->ud.lock);
109 spin_unlock_irqrestore(&the_controller->lock, flags);
111 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
116 static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
117 const char *buf, size_t count)
122 if (sscanf(buf, "%u", &rhport) != 1)
126 if (rhport >= VHCI_NPORTS) {
127 dev_err(dev, "invalid port %u\n", rhport);
131 err = vhci_port_disconnect(rhport);
135 usbip_dbg_vhci_sysfs("Leave\n");
139 static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
141 /* Sysfs entry to establish a virtual connection */
142 static int valid_args(__u32 rhport, enum usb_device_speed speed)
145 if (rhport >= VHCI_NPORTS) {
146 pr_err("port %u\n", rhport);
155 case USB_SPEED_WIRELESS:
158 pr_err("Failed attach request for unsupported USB speed: %s\n",
159 usb_speed_string(speed));
167 * To start a new USB/IP attachment, a userland program needs to setup a TCP
168 * connection and then write its socket descriptor with remote device
169 * information into this sysfs file.
171 * A remote device is virtually attached to the root-hub port of @rhport with
172 * @speed. @devid is embedded into a request to specify the remote device in a
175 * write() returns 0 on success, else negative errno.
177 static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
178 const char *buf, size_t count)
180 struct vhci_device *vdev;
181 struct socket *socket;
183 __u32 rhport = 0, devid = 0, speed = 0;
188 * @rhport: port number of vhci_hcd
189 * @sockfd: socket descriptor of an established TCP connection
190 * @devid: unique device identifier in a remote host
191 * @speed: usb device speed in a remote host
193 if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4)
196 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
197 rhport, sockfd, devid, speed);
199 /* check received parameters */
200 if (valid_args(rhport, speed) < 0)
203 /* Extract socket from fd. */
204 socket = sockfd_lookup(sockfd, &err);
208 /* now need lock until setting vdev status as used */
211 spin_lock_irqsave(&the_controller->lock, flags);
212 vdev = port_to_vdev(rhport);
213 spin_lock(&vdev->ud.lock);
215 if (vdev->ud.status != VDEV_ST_NULL) {
216 /* end of the lock */
217 spin_unlock(&vdev->ud.lock);
218 spin_unlock_irqrestore(&the_controller->lock, flags);
222 dev_err(dev, "port %d already used\n", rhport);
227 "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n",
228 rhport, sockfd, devid, speed, usb_speed_string(speed));
232 vdev->ud.sockfd = sockfd;
233 vdev->ud.tcp_socket = socket;
234 vdev->ud.status = VDEV_ST_NOTASSIGNED;
236 spin_unlock(&vdev->ud.lock);
237 spin_unlock_irqrestore(&the_controller->lock, flags);
240 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
241 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
243 rh_port_connect(rhport, speed);
247 static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
249 static struct attribute *dev_attrs[] = {
250 &dev_attr_status.attr,
251 &dev_attr_detach.attr,
252 &dev_attr_attach.attr,
253 &dev_attr_usbip_debug.attr,
257 const struct attribute_group dev_attr_group = {