2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Nobuo Iwata
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 #include <linux/kthread.h>
22 #include <linux/file.h>
23 #include <linux/net.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 /* Hardening for Spectre-v1 */
28 #include <linux/nospec.h>
30 #include "usbip_common.h"
33 /* TODO: refine locking ?*/
35 /* Sysfs entry to show port status */
36 static ssize_t
status_show_vhci(int pdev_nr
, char *out
)
38 struct platform_device
*pdev
= *(vhci_pdevs
+ pdev_nr
);
39 struct vhci_hcd
*vhci
;
45 usbip_dbg_vhci_sysfs("show status error\n");
49 vhci
= hcd_to_vhci(platform_get_drvdata(pdev
));
51 spin_lock_irqsave(&vhci
->lock
, flags
);
55 * port sta spd dev sockfd local_busid
56 * 0000 004 000 00000000 000003 1-2.3
57 * 0001 004 000 00000000 000004 2-3.4
59 * Output includes socket fd instead of socket pointer address to
60 * avoid leaking kernel memory address in:
61 * /sys/devices/platform/vhci_hcd.0/status and in debug output.
62 * The socket pointer address is not used at the moment and it was
63 * made visible as a convenient way to find IP address from socket
64 * pointer address by looking up /proc/net/{tcp,tcp6}. As this opens
65 * a security hole, the change is made to use sockfd instead.
67 for (i
= 0; i
< VHCI_HC_PORTS
; i
++) {
68 struct vhci_device
*vdev
= &vhci
->vdev
[i
];
70 spin_lock(&vdev
->ud
.lock
);
71 out
+= sprintf(out
, "%04u %03u ",
72 (pdev_nr
* VHCI_HC_PORTS
) + i
,
75 if (vdev
->ud
.status
== VDEV_ST_USED
) {
76 out
+= sprintf(out
, "%03u %08x ",
77 vdev
->speed
, vdev
->devid
);
78 out
+= sprintf(out
, "%06u %s",
80 dev_name(&vdev
->udev
->dev
));
83 out
+= sprintf(out
, "000 00000000 ");
84 out
+= sprintf(out
, "000000 0-0");
87 out
+= sprintf(out
, "\n");
88 spin_unlock(&vdev
->ud
.lock
);
91 spin_unlock_irqrestore(&vhci
->lock
, flags
);
96 static ssize_t
status_show_not_ready(int pdev_nr
, char *out
)
101 for (i
= 0; i
< VHCI_HC_PORTS
; i
++) {
102 out
+= sprintf(out
, "%04u %03u ",
103 (pdev_nr
* VHCI_HC_PORTS
) + i
,
104 VDEV_ST_NOTASSIGNED
);
105 out
+= sprintf(out
, "000 00000000 0000000000000000 0-0");
106 out
+= sprintf(out
, "\n");
111 static int status_name_to_id(const char *name
)
117 c
= strchr(name
, '.');
121 ret
= kstrtol(c
+1, 10, &val
);
128 static ssize_t
status_show(struct device
*dev
,
129 struct device_attribute
*attr
, char *out
)
135 "port sta spd dev sockfd local_busid\n");
137 pdev_nr
= status_name_to_id(attr
->attr
.name
);
139 out
+= status_show_not_ready(pdev_nr
, out
);
141 out
+= status_show_vhci(pdev_nr
, out
);
146 static ssize_t
nports_show(struct device
*dev
, struct device_attribute
*attr
,
151 out
+= sprintf(out
, "%d\n", VHCI_HC_PORTS
* vhci_num_controllers
);
154 static DEVICE_ATTR_RO(nports
);
156 /* Sysfs entry to shutdown a virtual connection */
157 static int vhci_port_disconnect(struct vhci_hcd
*vhci
, __u32 rhport
)
159 struct vhci_device
*vdev
= &vhci
->vdev
[rhport
];
162 usbip_dbg_vhci_sysfs("enter\n");
165 spin_lock_irqsave(&vhci
->lock
, flags
);
166 spin_lock(&vdev
->ud
.lock
);
168 if (vdev
->ud
.status
== VDEV_ST_NULL
) {
169 pr_err("not connected %d\n", vdev
->ud
.status
);
172 spin_unlock(&vdev
->ud
.lock
);
173 spin_unlock_irqrestore(&vhci
->lock
, flags
);
179 spin_unlock(&vdev
->ud
.lock
);
180 spin_unlock_irqrestore(&vhci
->lock
, flags
);
182 usbip_event_add(&vdev
->ud
, VDEV_EVENT_DOWN
);
187 static int valid_port(__u32
*pdev_nr
, __u32
*rhport
)
189 if (*pdev_nr
>= vhci_num_controllers
) {
190 pr_err("pdev %u\n", *pdev_nr
);
193 *pdev_nr
= array_index_nospec(*pdev_nr
, vhci_num_controllers
);
195 if (*rhport
>= VHCI_HC_PORTS
) {
196 pr_err("rhport %u\n", *rhport
);
199 *rhport
= array_index_nospec(*rhport
, VHCI_HC_PORTS
);
204 static ssize_t
store_detach(struct device
*dev
, struct device_attribute
*attr
,
205 const char *buf
, size_t count
)
207 __u32 port
= 0, pdev_nr
= 0, rhport
= 0;
211 if (kstrtoint(buf
, 10, &port
) < 0)
214 pdev_nr
= port_to_pdev_nr(port
);
215 rhport
= port_to_rhport(port
);
217 if (!valid_port(&pdev_nr
, &rhport
))
220 hcd
= platform_get_drvdata(*(vhci_pdevs
+ pdev_nr
));
222 dev_err(dev
, "port is not ready %u\n", port
);
226 ret
= vhci_port_disconnect(hcd_to_vhci(hcd
), rhport
);
230 usbip_dbg_vhci_sysfs("Leave\n");
234 static DEVICE_ATTR(detach
, S_IWUSR
, NULL
, store_detach
);
236 static int valid_args(__u32
*pdev_nr
, __u32
*rhport
,
237 enum usb_device_speed speed
)
239 if (!valid_port(pdev_nr
, rhport
)) {
247 case USB_SPEED_WIRELESS
:
250 pr_err("Failed attach request for unsupported USB speed: %s\n",
251 usb_speed_string(speed
));
258 /* Sysfs entry to establish a virtual connection */
260 * To start a new USB/IP attachment, a userland program needs to setup a TCP
261 * connection and then write its socket descriptor with remote device
262 * information into this sysfs file.
264 * A remote device is virtually attached to the root-hub port of @rhport with
265 * @speed. @devid is embedded into a request to specify the remote device in a
268 * write() returns 0 on success, else negative errno.
270 static ssize_t
store_attach(struct device
*dev
, struct device_attribute
*attr
,
271 const char *buf
, size_t count
)
273 struct socket
*socket
;
275 __u32 port
= 0, pdev_nr
= 0, rhport
= 0, devid
= 0, speed
= 0;
277 struct vhci_hcd
*vhci
;
278 struct vhci_device
*vdev
;
283 * @rhport: port number of vhci_hcd
284 * @sockfd: socket descriptor of an established TCP connection
285 * @devid: unique device identifier in a remote host
286 * @speed: usb device speed in a remote host
288 if (sscanf(buf
, "%u %u %u %u", &port
, &sockfd
, &devid
, &speed
) != 4)
290 pdev_nr
= port_to_pdev_nr(port
);
291 rhport
= port_to_rhport(port
);
293 usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
294 port
, pdev_nr
, rhport
);
295 usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
296 sockfd
, devid
, speed
);
298 /* check received parameters */
299 if (!valid_args(&pdev_nr
, &rhport
, speed
))
302 hcd
= platform_get_drvdata(*(vhci_pdevs
+ pdev_nr
));
304 dev_err(dev
, "port %d is not ready\n", port
);
307 vhci
= hcd_to_vhci(hcd
);
308 vdev
= &vhci
->vdev
[rhport
];
310 /* Extract socket from fd. */
311 socket
= sockfd_lookup(sockfd
, &err
);
315 /* now need lock until setting vdev status as used */
318 spin_lock_irqsave(&vhci
->lock
, flags
);
319 spin_lock(&vdev
->ud
.lock
);
321 if (vdev
->ud
.status
!= VDEV_ST_NULL
) {
322 /* end of the lock */
323 spin_unlock(&vdev
->ud
.lock
);
324 spin_unlock_irqrestore(&vhci
->lock
, flags
);
328 dev_err(dev
, "port %d already used\n", rhport
);
332 dev_info(dev
, "pdev(%u) rhport(%u) sockfd(%d)\n",
333 pdev_nr
, rhport
, sockfd
);
334 dev_info(dev
, "devid(%u) speed(%u) speed_str(%s)\n",
335 devid
, speed
, usb_speed_string(speed
));
339 vdev
->ud
.sockfd
= sockfd
;
340 vdev
->ud
.tcp_socket
= socket
;
341 vdev
->ud
.status
= VDEV_ST_NOTASSIGNED
;
343 spin_unlock(&vdev
->ud
.lock
);
344 spin_unlock_irqrestore(&vhci
->lock
, flags
);
347 vdev
->ud
.tcp_rx
= kthread_get_run(vhci_rx_loop
, &vdev
->ud
, "vhci_rx");
348 vdev
->ud
.tcp_tx
= kthread_get_run(vhci_tx_loop
, &vdev
->ud
, "vhci_tx");
350 rh_port_connect(vdev
, speed
);
354 static DEVICE_ATTR(attach
, S_IWUSR
, NULL
, store_attach
);
356 #define MAX_STATUS_NAME 16
359 struct device_attribute attr
;
360 char name
[MAX_STATUS_NAME
+1];
363 static struct status_attr
*status_attrs
;
365 static void set_status_attr(int id
)
367 struct status_attr
*status
;
369 status
= status_attrs
+ id
;
371 strcpy(status
->name
, "status");
373 snprintf(status
->name
, MAX_STATUS_NAME
+1, "status.%d", id
);
374 status
->attr
.attr
.name
= status
->name
;
375 status
->attr
.attr
.mode
= S_IRUGO
;
376 status
->attr
.show
= status_show
;
377 sysfs_attr_init(&status
->attr
.attr
);
380 static int init_status_attrs(void)
384 status_attrs
= kcalloc(vhci_num_controllers
, sizeof(struct status_attr
),
386 if (status_attrs
== NULL
)
389 for (id
= 0; id
< vhci_num_controllers
; id
++)
395 static void finish_status_attrs(void)
400 struct attribute_group vhci_attr_group
= {
404 int vhci_init_attr_group(void)
406 struct attribute
**attrs
;
409 attrs
= kcalloc((vhci_num_controllers
+ 5), sizeof(struct attribute
*),
414 ret
= init_status_attrs();
419 *attrs
= &dev_attr_nports
.attr
;
420 *(attrs
+ 1) = &dev_attr_detach
.attr
;
421 *(attrs
+ 2) = &dev_attr_attach
.attr
;
422 *(attrs
+ 3) = &dev_attr_usbip_debug
.attr
;
423 for (i
= 0; i
< vhci_num_controllers
; i
++)
424 *(attrs
+ i
+ 4) = &((status_attrs
+ i
)->attr
.attr
);
425 vhci_attr_group
.attrs
= attrs
;
429 void vhci_finish_attr_group(void)
431 finish_status_attrs();
432 kfree(vhci_attr_group
.attrs
);