Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4 * Copyright (C) 2015-2016 Nobuo Iwata
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#include <linux/kthread.h>
  8#include <linux/file.h>
  9#include <linux/net.h>
 10#include <linux/platform_device.h>
 11#include <linux/slab.h>
 12
 13/* Hardening for Spectre-v1 */
 14#include <linux/nospec.h>
 15
 16#include "usbip_common.h"
 17#include "vhci.h"
 18
 19/* TODO: refine locking ?*/
 20
 21/*
 22 * output example:
 23 * hub port sta spd dev       sockfd local_busid
 24 * hs  0000 004 000 00000000  000003 1-2.3
 25 * ................................................
 26 * ss  0008 004 000 00000000  000004 2-3.4
 27 * ................................................
 28 *
 29 * Output includes socket fd instead of socket pointer address to avoid
 30 * leaking kernel memory address in:
 31 *	/sys/devices/platform/vhci_hcd.0/status and in debug output.
 32 * The socket pointer address is not used at the moment and it was made
 33 * visible as a convenient way to find IP address from socket pointer
 34 * address by looking up /proc/net/{tcp,tcp6}. As this opens a security
 35 * hole, the change is made to use sockfd instead.
 36 *
 37 */
 38static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev)
 39{
 40	if (hub == HUB_SPEED_HIGH)
 41		*out += sprintf(*out, "hs  %04u %03u ",
 42				      port, vdev->ud.status);
 43	else /* hub == HUB_SPEED_SUPER */
 44		*out += sprintf(*out, "ss  %04u %03u ",
 45				      port, vdev->ud.status);
 46
 47	if (vdev->ud.status == VDEV_ST_USED) {
 48		*out += sprintf(*out, "%03u %08x ",
 49				      vdev->speed, vdev->devid);
 50		*out += sprintf(*out, "%06u %s",
 51				      vdev->ud.sockfd,
 52				      dev_name(&vdev->udev->dev));
 53
 54	} else {
 55		*out += sprintf(*out, "000 00000000 ");
 56		*out += sprintf(*out, "000000 0-0");
 57	}
 58
 59	*out += sprintf(*out, "\n");
 60}
 61
 62/* Sysfs entry to show port status */
 63static ssize_t status_show_vhci(int pdev_nr, char *out)
 64{
 65	struct platform_device *pdev = vhcis[pdev_nr].pdev;
 66	struct vhci *vhci;
 67	struct usb_hcd *hcd;
 68	struct vhci_hcd *vhci_hcd;
 69	char *s = out;
 70	int i;
 71	unsigned long flags;
 72
 73	if (!pdev || !out) {
 74		usbip_dbg_vhci_sysfs("show status error\n");
 75		return 0;
 76	}
 77
 78	hcd = platform_get_drvdata(pdev);
 79	vhci_hcd = hcd_to_vhci_hcd(hcd);
 80	vhci = vhci_hcd->vhci;
 81
 82	spin_lock_irqsave(&vhci->lock, flags);
 83
 
 
 
 
 
 
 
 
 
 
 84	for (i = 0; i < VHCI_HC_PORTS; i++) {
 85		struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i];
 86
 87		spin_lock(&vdev->ud.lock);
 88		port_show_vhci(&out, HUB_SPEED_HIGH,
 89			       pdev_nr * VHCI_PORTS + i, vdev);
 90		spin_unlock(&vdev->ud.lock);
 91	}
 92
 93	for (i = 0; i < VHCI_HC_PORTS; i++) {
 94		struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i];
 
 
 
 
 
 
 
 
 95
 96		spin_lock(&vdev->ud.lock);
 97		port_show_vhci(&out, HUB_SPEED_SUPER,
 98			       pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev);
 99		spin_unlock(&vdev->ud.lock);
100	}
101
102	spin_unlock_irqrestore(&vhci->lock, flags);
103
104	return out - s;
105}
106
107static ssize_t status_show_not_ready(int pdev_nr, char *out)
108{
109	char *s = out;
110	int i = 0;
111
112	for (i = 0; i < VHCI_HC_PORTS; i++) {
113		out += sprintf(out, "hs  %04u %03u ",
114				    (pdev_nr * VHCI_PORTS) + i,
115				    VDEV_ST_NOTASSIGNED);
116		out += sprintf(out, "000 00000000 0000000000000000 0-0");
117		out += sprintf(out, "\n");
118	}
119
120	for (i = 0; i < VHCI_HC_PORTS; i++) {
121		out += sprintf(out, "ss  %04u %03u ",
122				    (pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i,
123				    VDEV_ST_NOTASSIGNED);
124		out += sprintf(out, "000 00000000 0000000000000000 0-0");
125		out += sprintf(out, "\n");
126	}
127	return out - s;
128}
129
130static int status_name_to_id(const char *name)
131{
132	char *c;
133	long val;
134	int ret;
135
136	c = strchr(name, '.');
137	if (c == NULL)
138		return 0;
139
140	ret = kstrtol(c+1, 10, &val);
141	if (ret < 0)
142		return ret;
143
144	return val;
145}
146
147static ssize_t status_show(struct device *dev,
148			   struct device_attribute *attr, char *out)
149{
150	char *s = out;
151	int pdev_nr;
152
153	out += sprintf(out,
154		       "hub port sta spd dev      sockfd local_busid\n");
155
156	pdev_nr = status_name_to_id(attr->attr.name);
157	if (pdev_nr < 0)
158		out += status_show_not_ready(pdev_nr, out);
159	else
160		out += status_show_vhci(pdev_nr, out);
161
162	return out - s;
163}
164
165static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
166			   char *out)
167{
168	char *s = out;
169
170	/*
171	 * Half the ports are for SPEED_HIGH and half for SPEED_SUPER,
172	 * thus the * 2.
173	 */
174	out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers);
175	return out - s;
176}
177static DEVICE_ATTR_RO(nports);
178
179/* Sysfs entry to shutdown a virtual connection */
180static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport)
181{
182	struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
183	struct vhci *vhci = vhci_hcd->vhci;
184	unsigned long flags;
185
186	usbip_dbg_vhci_sysfs("enter\n");
187
188	mutex_lock(&vdev->ud.sysfs_lock);
189
190	/* lock */
191	spin_lock_irqsave(&vhci->lock, flags);
192	spin_lock(&vdev->ud.lock);
193
194	if (vdev->ud.status == VDEV_ST_NULL) {
195		pr_err("not connected %d\n", vdev->ud.status);
196
197		/* unlock */
198		spin_unlock(&vdev->ud.lock);
199		spin_unlock_irqrestore(&vhci->lock, flags);
200		mutex_unlock(&vdev->ud.sysfs_lock);
201
202		return -EINVAL;
203	}
204
205	/* unlock */
206	spin_unlock(&vdev->ud.lock);
207	spin_unlock_irqrestore(&vhci->lock, flags);
208
209	usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
210
211	mutex_unlock(&vdev->ud.sysfs_lock);
212
213	return 0;
214}
215
216static int valid_port(__u32 *pdev_nr, __u32 *rhport)
217{
218	if (*pdev_nr >= vhci_num_controllers) {
219		pr_err("pdev %u\n", *pdev_nr);
220		return 0;
221	}
222	*pdev_nr = array_index_nospec(*pdev_nr, vhci_num_controllers);
223
224	if (*rhport >= VHCI_HC_PORTS) {
225		pr_err("rhport %u\n", *rhport);
226		return 0;
227	}
228	*rhport = array_index_nospec(*rhport, VHCI_HC_PORTS);
229
230	return 1;
231}
232
233static ssize_t detach_store(struct device *dev, struct device_attribute *attr,
234			    const char *buf, size_t count)
235{
236	__u32 port = 0, pdev_nr = 0, rhport = 0;
237	struct usb_hcd *hcd;
238	struct vhci_hcd *vhci_hcd;
239	int ret;
240
241	if (kstrtoint(buf, 10, &port) < 0)
242		return -EINVAL;
243
244	pdev_nr = port_to_pdev_nr(port);
245	rhport = port_to_rhport(port);
246
247	if (!valid_port(&pdev_nr, &rhport))
248		return -EINVAL;
249
250	hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
251	if (hcd == NULL) {
252		dev_err(dev, "port is not ready %u\n", port);
253		return -EAGAIN;
254	}
255
256	usbip_dbg_vhci_sysfs("rhport %d\n", rhport);
257
258	if ((port / VHCI_HC_PORTS) % 2)
259		vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss;
260	else
261		vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs;
262
263	ret = vhci_port_disconnect(vhci_hcd, rhport);
264	if (ret < 0)
265		return -EINVAL;
266
267	usbip_dbg_vhci_sysfs("Leave\n");
268
269	return count;
270}
271static DEVICE_ATTR_WO(detach);
272
273static int valid_args(__u32 *pdev_nr, __u32 *rhport,
274		      enum usb_device_speed speed)
275{
276	if (!valid_port(pdev_nr, rhport)) {
277		return 0;
278	}
279
280	switch (speed) {
281	case USB_SPEED_LOW:
282	case USB_SPEED_FULL:
283	case USB_SPEED_HIGH:
284	case USB_SPEED_WIRELESS:
285	case USB_SPEED_SUPER:
286		break;
287	default:
288		pr_err("Failed attach request for unsupported USB speed: %s\n",
289			usb_speed_string(speed));
290		return 0;
291	}
292
293	return 1;
294}
295
296/* Sysfs entry to establish a virtual connection */
297/*
298 * To start a new USB/IP attachment, a userland program needs to setup a TCP
299 * connection and then write its socket descriptor with remote device
300 * information into this sysfs file.
301 *
302 * A remote device is virtually attached to the root-hub port of @rhport with
303 * @speed. @devid is embedded into a request to specify the remote device in a
304 * server host.
305 *
306 * write() returns 0 on success, else negative errno.
307 */
308static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
309			    const char *buf, size_t count)
310{
311	struct socket *socket;
312	int sockfd = 0;
313	__u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
314	struct usb_hcd *hcd;
315	struct vhci_hcd *vhci_hcd;
316	struct vhci_device *vdev;
317	struct vhci *vhci;
318	int err;
319	unsigned long flags;
320	struct task_struct *tcp_rx = NULL;
321	struct task_struct *tcp_tx = NULL;
322
323	/*
324	 * @rhport: port number of vhci_hcd
325	 * @sockfd: socket descriptor of an established TCP connection
326	 * @devid: unique device identifier in a remote host
327	 * @speed: usb device speed in a remote host
328	 */
329	if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
330		return -EINVAL;
331	pdev_nr = port_to_pdev_nr(port);
332	rhport = port_to_rhport(port);
333
334	usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
335			     port, pdev_nr, rhport);
336	usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
337			     sockfd, devid, speed);
338
339	/* check received parameters */
340	if (!valid_args(&pdev_nr, &rhport, speed))
341		return -EINVAL;
342
343	hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
344	if (hcd == NULL) {
345		dev_err(dev, "port %d is not ready\n", port);
346		return -EAGAIN;
347	}
348
349	vhci_hcd = hcd_to_vhci_hcd(hcd);
350	vhci = vhci_hcd->vhci;
351
352	if (speed == USB_SPEED_SUPER)
353		vdev = &vhci->vhci_hcd_ss->vdev[rhport];
354	else
355		vdev = &vhci->vhci_hcd_hs->vdev[rhport];
356
357	mutex_lock(&vdev->ud.sysfs_lock);
358
359	/* Extract socket from fd. */
360	socket = sockfd_lookup(sockfd, &err);
361	if (!socket) {
362		dev_err(dev, "failed to lookup sock");
363		err = -EINVAL;
364		goto unlock_mutex;
365	}
366	if (socket->type != SOCK_STREAM) {
367		dev_err(dev, "Expecting SOCK_STREAM - found %d",
368			socket->type);
369		sockfd_put(socket);
370		err = -EINVAL;
371		goto unlock_mutex;
372	}
373
374	/* create threads before locking */
375	tcp_rx = kthread_create(vhci_rx_loop, &vdev->ud, "vhci_rx");
376	if (IS_ERR(tcp_rx)) {
377		sockfd_put(socket);
378		err = -EINVAL;
379		goto unlock_mutex;
380	}
381	tcp_tx = kthread_create(vhci_tx_loop, &vdev->ud, "vhci_tx");
382	if (IS_ERR(tcp_tx)) {
383		kthread_stop(tcp_rx);
384		sockfd_put(socket);
385		err = -EINVAL;
386		goto unlock_mutex;
387	}
388
389	/* get task structs now */
390	get_task_struct(tcp_rx);
391	get_task_struct(tcp_tx);
392
393	/* now begin lock until setting vdev status set */
394	spin_lock_irqsave(&vhci->lock, flags);
395	spin_lock(&vdev->ud.lock);
396
397	if (vdev->ud.status != VDEV_ST_NULL) {
398		/* end of the lock */
399		spin_unlock(&vdev->ud.lock);
400		spin_unlock_irqrestore(&vhci->lock, flags);
401
402		sockfd_put(socket);
403		kthread_stop_put(tcp_rx);
404		kthread_stop_put(tcp_tx);
405
406		dev_err(dev, "port %d already used\n", rhport);
407		/*
408		 * Will be retried from userspace
409		 * if there's another free port.
410		 */
411		err = -EBUSY;
412		goto unlock_mutex;
413	}
414
415	dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
416		 pdev_nr, rhport, sockfd);
417	dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
418		 devid, speed, usb_speed_string(speed));
419
420	vdev->devid         = devid;
421	vdev->speed         = speed;
422	vdev->ud.sockfd     = sockfd;
423	vdev->ud.tcp_socket = socket;
424	vdev->ud.tcp_rx     = tcp_rx;
425	vdev->ud.tcp_tx     = tcp_tx;
426	vdev->ud.status     = VDEV_ST_NOTASSIGNED;
427	usbip_kcov_handle_init(&vdev->ud);
428
429	spin_unlock(&vdev->ud.lock);
430	spin_unlock_irqrestore(&vhci->lock, flags);
431	/* end the lock */
432
433	wake_up_process(vdev->ud.tcp_rx);
434	wake_up_process(vdev->ud.tcp_tx);
435
436	rh_port_connect(vdev, speed);
437
438	dev_info(dev, "Device attached\n");
439
440	mutex_unlock(&vdev->ud.sysfs_lock);
441
442	return count;
443
444unlock_mutex:
445	mutex_unlock(&vdev->ud.sysfs_lock);
446	return err;
447}
448static DEVICE_ATTR_WO(attach);
449
450#define MAX_STATUS_NAME 16
451
452struct status_attr {
453	struct device_attribute attr;
454	char name[MAX_STATUS_NAME+1];
455};
456
457static struct status_attr *status_attrs;
458
459static void set_status_attr(int id)
460{
461	struct status_attr *status;
462
463	status = status_attrs + id;
464	if (id == 0)
465		strcpy(status->name, "status");
466	else
467		snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
468	status->attr.attr.name = status->name;
469	status->attr.attr.mode = S_IRUGO;
470	status->attr.show = status_show;
471	sysfs_attr_init(&status->attr.attr);
472}
473
474static int init_status_attrs(void)
475{
476	int id;
477
478	status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
479			       GFP_KERNEL);
480	if (status_attrs == NULL)
481		return -ENOMEM;
482
483	for (id = 0; id < vhci_num_controllers; id++)
484		set_status_attr(id);
485
486	return 0;
487}
488
489static void finish_status_attrs(void)
490{
491	kfree(status_attrs);
492}
493
494struct attribute_group vhci_attr_group = {
495	.attrs = NULL,
496};
497
498int vhci_init_attr_group(void)
499{
500	struct attribute **attrs;
501	int ret, i;
502
503	attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
504			GFP_KERNEL);
505	if (attrs == NULL)
506		return -ENOMEM;
507
508	ret = init_status_attrs();
509	if (ret) {
510		kfree(attrs);
511		return ret;
512	}
513	*attrs = &dev_attr_nports.attr;
514	*(attrs + 1) = &dev_attr_detach.attr;
515	*(attrs + 2) = &dev_attr_attach.attr;
516	*(attrs + 3) = &dev_attr_usbip_debug.attr;
517	for (i = 0; i < vhci_num_controllers; i++)
518		*(attrs + i + 4) = &((status_attrs + i)->attr.attr);
519	vhci_attr_group.attrs = attrs;
520	return 0;
521}
522
523void vhci_finish_attr_group(void)
524{
525	finish_status_attrs();
526	kfree(vhci_attr_group.attrs);
527}
v4.10.11
 
  1/*
  2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3 * Copyright (C) 2015-2016 Nobuo Iwata
  4 *
  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.
  9 *
 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.
 14 *
 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,
 18 * USA.
 19 */
 20
 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>
 26
 
 
 
 27#include "usbip_common.h"
 28#include "vhci.h"
 29
 30/* TODO: refine locking ?*/
 31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 32/* Sysfs entry to show port status */
 33static ssize_t status_show_vhci(int pdev_nr, char *out)
 34{
 35	struct platform_device *pdev = *(vhci_pdevs + pdev_nr);
 36	struct vhci_hcd *vhci;
 
 
 37	char *s = out;
 38	int i = 0;
 39	unsigned long flags;
 40
 41	if (!pdev || !out) {
 42		usbip_dbg_vhci_sysfs("show status error\n");
 43		return 0;
 44	}
 45
 46	vhci = hcd_to_vhci(platform_get_drvdata(pdev));
 
 
 47
 48	spin_lock_irqsave(&vhci->lock, flags);
 49
 50	/*
 51	 * output example:
 52	 * port sta spd dev      socket           local_busid
 53	 * 0000 004 000 00000000         c5a7bb80 1-2.3
 54	 * 0001 004 000 00000000         d8cee980 2-3.4
 55	 *
 56	 * IP address can be retrieved from a socket pointer address by looking
 57	 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
 58	 * port number and its peer IP address.
 59	 */
 60	for (i = 0; i < VHCI_HC_PORTS; i++) {
 61		struct vhci_device *vdev = &vhci->vdev[i];
 62
 63		spin_lock(&vdev->ud.lock);
 64		out += sprintf(out, "%04u %03u ",
 65				    (pdev_nr * VHCI_HC_PORTS) + i,
 66				    vdev->ud.status);
 67
 68		if (vdev->ud.status == VDEV_ST_USED) {
 69			out += sprintf(out, "%03u %08x ",
 70					    vdev->speed, vdev->devid);
 71			out += sprintf(out, "%16p %s",
 72					    vdev->ud.tcp_socket,
 73					    dev_name(&vdev->udev->dev));
 74
 75		} else {
 76			out += sprintf(out, "000 00000000 ");
 77			out += sprintf(out, "0000000000000000 0-0");
 78		}
 79
 80		out += sprintf(out, "\n");
 
 
 81		spin_unlock(&vdev->ud.lock);
 82	}
 83
 84	spin_unlock_irqrestore(&vhci->lock, flags);
 85
 86	return out - s;
 87}
 88
 89static ssize_t status_show_not_ready(int pdev_nr, char *out)
 90{
 91	char *s = out;
 92	int i = 0;
 93
 94	for (i = 0; i < VHCI_HC_PORTS; i++) {
 95		out += sprintf(out, "%04u %03u ",
 96				    (pdev_nr * VHCI_HC_PORTS) + i,
 
 
 
 
 
 
 
 
 97				    VDEV_ST_NOTASSIGNED);
 98		out += sprintf(out, "000 00000000 0000000000000000 0-0");
 99		out += sprintf(out, "\n");
100	}
101	return out - s;
102}
103
104static int status_name_to_id(const char *name)
105{
106	char *c;
107	long val;
108	int ret;
109
110	c = strchr(name, '.');
111	if (c == NULL)
112		return 0;
113
114	ret = kstrtol(c+1, 10, &val);
115	if (ret < 0)
116		return ret;
117
118	return val;
119}
120
121static ssize_t status_show(struct device *dev,
122			   struct device_attribute *attr, char *out)
123{
124	char *s = out;
125	int pdev_nr;
126
127	out += sprintf(out,
128		       "port sta spd dev      socket           local_busid\n");
129
130	pdev_nr = status_name_to_id(attr->attr.name);
131	if (pdev_nr < 0)
132		out += status_show_not_ready(pdev_nr, out);
133	else
134		out += status_show_vhci(pdev_nr, out);
135
136	return out - s;
137}
138
139static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
140			   char *out)
141{
142	char *s = out;
143
144	out += sprintf(out, "%d\n", VHCI_HC_PORTS * vhci_num_controllers);
 
 
 
 
145	return out - s;
146}
147static DEVICE_ATTR_RO(nports);
148
149/* Sysfs entry to shutdown a virtual connection */
150static int vhci_port_disconnect(struct vhci_hcd *vhci, __u32 rhport)
151{
152	struct vhci_device *vdev = &vhci->vdev[rhport];
 
153	unsigned long flags;
154
155	usbip_dbg_vhci_sysfs("enter\n");
156
 
 
157	/* lock */
158	spin_lock_irqsave(&vhci->lock, flags);
159	spin_lock(&vdev->ud.lock);
160
161	if (vdev->ud.status == VDEV_ST_NULL) {
162		pr_err("not connected %d\n", vdev->ud.status);
163
164		/* unlock */
165		spin_unlock(&vdev->ud.lock);
166		spin_unlock_irqrestore(&vhci->lock, flags);
 
167
168		return -EINVAL;
169	}
170
171	/* unlock */
172	spin_unlock(&vdev->ud.lock);
173	spin_unlock_irqrestore(&vhci->lock, flags);
174
175	usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
176
 
 
177	return 0;
178}
179
180static int valid_port(__u32 pdev_nr, __u32 rhport)
181{
182	if (pdev_nr >= vhci_num_controllers) {
183		pr_err("pdev %u\n", pdev_nr);
184		return 0;
185	}
186	if (rhport >= VHCI_HC_PORTS) {
187		pr_err("rhport %u\n", rhport);
 
 
188		return 0;
189	}
 
 
190	return 1;
191}
192
193static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
194			    const char *buf, size_t count)
195{
196	__u32 port = 0, pdev_nr = 0, rhport = 0;
197	struct usb_hcd *hcd;
 
198	int ret;
199
200	if (kstrtoint(buf, 10, &port) < 0)
201		return -EINVAL;
202
203	pdev_nr = port_to_pdev_nr(port);
204	rhport = port_to_rhport(port);
205
206	if (!valid_port(pdev_nr, rhport))
207		return -EINVAL;
208
209	hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr));
210	if (hcd == NULL) {
211		dev_err(dev, "port is not ready %u\n", port);
212		return -EAGAIN;
213	}
214
215	ret = vhci_port_disconnect(hcd_to_vhci(hcd), rhport);
 
 
 
 
 
 
 
216	if (ret < 0)
217		return -EINVAL;
218
219	usbip_dbg_vhci_sysfs("Leave\n");
220
221	return count;
222}
223static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
224
225static int valid_args(__u32 pdev_nr, __u32 rhport, enum usb_device_speed speed)
 
226{
227	if (!valid_port(pdev_nr, rhport)) {
228		return 0;
229	}
230
231	switch (speed) {
232	case USB_SPEED_LOW:
233	case USB_SPEED_FULL:
234	case USB_SPEED_HIGH:
235	case USB_SPEED_WIRELESS:
 
236		break;
237	default:
238		pr_err("Failed attach request for unsupported USB speed: %s\n",
239			usb_speed_string(speed));
240		return 0;
241	}
242
243	return 1;
244}
245
246/* Sysfs entry to establish a virtual connection */
247/*
248 * To start a new USB/IP attachment, a userland program needs to setup a TCP
249 * connection and then write its socket descriptor with remote device
250 * information into this sysfs file.
251 *
252 * A remote device is virtually attached to the root-hub port of @rhport with
253 * @speed. @devid is embedded into a request to specify the remote device in a
254 * server host.
255 *
256 * write() returns 0 on success, else negative errno.
257 */
258static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
259			    const char *buf, size_t count)
260{
261	struct socket *socket;
262	int sockfd = 0;
263	__u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
264	struct usb_hcd *hcd;
265	struct vhci_hcd *vhci;
266	struct vhci_device *vdev;
 
267	int err;
268	unsigned long flags;
 
 
269
270	/*
271	 * @rhport: port number of vhci_hcd
272	 * @sockfd: socket descriptor of an established TCP connection
273	 * @devid: unique device identifier in a remote host
274	 * @speed: usb device speed in a remote host
275	 */
276	if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
277		return -EINVAL;
278	pdev_nr = port_to_pdev_nr(port);
279	rhport = port_to_rhport(port);
280
281	usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
282			     port, pdev_nr, rhport);
283	usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
284			     sockfd, devid, speed);
285
286	/* check received parameters */
287	if (!valid_args(pdev_nr, rhport, speed))
288		return -EINVAL;
289
290	hcd = platform_get_drvdata(*(vhci_pdevs + pdev_nr));
291	if (hcd == NULL) {
292		dev_err(dev, "port %d is not ready\n", port);
293		return -EAGAIN;
294	}
295	vhci = hcd_to_vhci(hcd);
296	vdev = &vhci->vdev[rhport];
 
 
 
 
 
 
 
 
297
298	/* Extract socket from fd. */
299	socket = sockfd_lookup(sockfd, &err);
300	if (!socket)
301		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
303	/* now need lock until setting vdev status as used */
 
 
304
305	/* begin a lock */
306	spin_lock_irqsave(&vhci->lock, flags);
307	spin_lock(&vdev->ud.lock);
308
309	if (vdev->ud.status != VDEV_ST_NULL) {
310		/* end of the lock */
311		spin_unlock(&vdev->ud.lock);
312		spin_unlock_irqrestore(&vhci->lock, flags);
313
314		sockfd_put(socket);
 
 
315
316		dev_err(dev, "port %d already used\n", rhport);
317		return -EINVAL;
 
 
 
 
 
318	}
319
320	dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
321		 pdev_nr, rhport, sockfd);
322	dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
323		 devid, speed, usb_speed_string(speed));
324
325	vdev->devid         = devid;
326	vdev->speed         = speed;
 
327	vdev->ud.tcp_socket = socket;
 
 
328	vdev->ud.status     = VDEV_ST_NOTASSIGNED;
 
329
330	spin_unlock(&vdev->ud.lock);
331	spin_unlock_irqrestore(&vhci->lock, flags);
332	/* end the lock */
333
334	vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
335	vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
336
337	rh_port_connect(vdev, speed);
338
 
 
 
 
339	return count;
 
 
 
 
340}
341static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
342
343#define MAX_STATUS_NAME 16
344
345struct status_attr {
346	struct device_attribute attr;
347	char name[MAX_STATUS_NAME+1];
348};
349
350static struct status_attr *status_attrs;
351
352static void set_status_attr(int id)
353{
354	struct status_attr *status;
355
356	status = status_attrs + id;
357	if (id == 0)
358		strcpy(status->name, "status");
359	else
360		snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
361	status->attr.attr.name = status->name;
362	status->attr.attr.mode = S_IRUGO;
363	status->attr.show = status_show;
364	sysfs_attr_init(&status->attr.attr);
365}
366
367static int init_status_attrs(void)
368{
369	int id;
370
371	status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
372			       GFP_KERNEL);
373	if (status_attrs == NULL)
374		return -ENOMEM;
375
376	for (id = 0; id < vhci_num_controllers; id++)
377		set_status_attr(id);
378
379	return 0;
380}
381
382static void finish_status_attrs(void)
383{
384	kfree(status_attrs);
385}
386
387struct attribute_group vhci_attr_group = {
388	.attrs = NULL,
389};
390
391int vhci_init_attr_group(void)
392{
393	struct attribute **attrs;
394	int ret, i;
395
396	attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
397			GFP_KERNEL);
398	if (attrs == NULL)
399		return -ENOMEM;
400
401	ret = init_status_attrs();
402	if (ret) {
403		kfree(attrs);
404		return ret;
405	}
406	*attrs = &dev_attr_nports.attr;
407	*(attrs + 1) = &dev_attr_detach.attr;
408	*(attrs + 2) = &dev_attr_attach.attr;
409	*(attrs + 3) = &dev_attr_usbip_debug.attr;
410	for (i = 0; i < vhci_num_controllers; i++)
411		*(attrs + i + 4) = &((status_attrs + i)->attr.attr);
412	vhci_attr_group.attrs = attrs;
413	return 0;
414}
415
416void vhci_finish_attr_group(void)
417{
418	finish_status_attrs();
419	kfree(vhci_attr_group.attrs);
420}