vhci_sysfs.c 6.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2003-2008 Takahiro Hirofuchi
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

20
#include <linux/kthread.h>
21
#include <linux/file.h>
22 23
#include <linux/net.h>

24 25 26 27 28 29
#include "usbip_common.h"
#include "vhci.h"

/* TODO: refine locking ?*/

/* Sysfs entry to show port status */
30
static ssize_t status_show(struct device *dev, struct device_attribute *attr,
31 32 33 34
			   char *out)
{
	char *s = out;
	int i = 0;
35
	unsigned long flags;
36

37
	BUG_ON(!the_controller || !out);
38

39
	spin_lock_irqsave(&the_controller->lock, flags);
40 41 42

	/*
	 * output example:
43 44 45
	 * port sta spd dev      sockfd local_busid
	 * 0000 004 000 00000000 000003 1-2.3
	 * 0001 004 000 00000000 000004 2-3.4
46
	 *
47 48 49 50 51 52 53
	 * Output includes socket fd instead of socket pointer address to
	 * avoid leaking kernel memory address in:
	 *	/sys/devices/platform/vhci_hcd.0/status and in debug output.
	 * The socket pointer address is not used at the moment and it was
	 * made visible as a convenient way to find IP address from socket
	 * pointer address by looking up /proc/net/{tcp,tcp6}. As this opens
	 * a security hole, the change is made to use sockfd instead.
54
	 */
55
	out += sprintf(out,
56
		       "prt sta spd dev      sockfd local_busid\n");
57 58 59 60 61 62 63 64 65

	for (i = 0; i < VHCI_NPORTS; i++) {
		struct vhci_device *vdev = port_to_vdev(i);

		spin_lock(&vdev->ud.lock);
		out += sprintf(out, "%03u %03u ", i, vdev->ud.status);

		if (vdev->ud.status == VDEV_ST_USED) {
			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));
69

70
		} else
71
			out += sprintf(out, "000 00000000 000000 0-0");
72 73 74 75 76

		out += sprintf(out, "\n");
		spin_unlock(&vdev->ud.lock);
	}

77
	spin_unlock_irqrestore(&the_controller->lock, flags);
78 79 80

	return out - s;
}
81
static DEVICE_ATTR_RO(status);
82 83 84 85 86

/* Sysfs entry to shutdown a virtual connection */
static int vhci_port_disconnect(__u32 rhport)
{
	struct vhci_device *vdev;
87
	unsigned long flags;
88

89
	usbip_dbg_vhci_sysfs("enter\n");
90 91

	/* lock */
92
	spin_lock_irqsave(&the_controller->lock, flags);
93 94 95 96 97

	vdev = port_to_vdev(rhport);

	spin_lock(&vdev->ud.lock);
	if (vdev->ud.status == VDEV_ST_NULL) {
98
		pr_err("not connected %d\n", vdev->ud.status);
99 100 101

		/* unlock */
		spin_unlock(&vdev->ud.lock);
102
		spin_unlock_irqrestore(&the_controller->lock, flags);
103 104 105 106 107 108

		return -EINVAL;
	}

	/* unlock */
	spin_unlock(&vdev->ud.lock);
109
	spin_unlock_irqrestore(&the_controller->lock, flags);
110 111 112 113 114 115 116 117 118 119 120 121

	usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);

	return 0;
}

static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	int err;
	__u32 rhport = 0;

122 123
	if (sscanf(buf, "%u", &rhport) != 1)
		return -EINVAL;
124 125 126

	/* check rhport */
	if (rhport >= VHCI_NPORTS) {
127
		dev_err(dev, "invalid port %u\n", rhport);
128 129 130 131 132 133 134
		return -EINVAL;
	}

	err = vhci_port_disconnect(rhport);
	if (err < 0)
		return -EINVAL;

135
	usbip_dbg_vhci_sysfs("Leave\n");
136

137 138 139 140 141 142 143 144
	return count;
}
static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);

/* Sysfs entry to establish a virtual connection */
static int valid_args(__u32 rhport, enum usb_device_speed speed)
{
	/* check rhport */
145
	if (rhport >= VHCI_NPORTS) {
146
		pr_err("port %u\n", rhport);
147 148 149 150 151 152 153 154
		return -EINVAL;
	}

	/* check speed */
	switch (speed) {
	case USB_SPEED_LOW:
	case USB_SPEED_FULL:
	case USB_SPEED_HIGH:
155
	case USB_SPEED_WIRELESS:
156 157
		break;
	default:
158 159
		pr_err("Failed attach request for unsupported USB speed: %s\n",
			usb_speed_string(speed));
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		return -EINVAL;
	}

	return 0;
}

/*
 * To start a new USB/IP attachment, a userland program needs to setup a TCP
 * connection and then write its socket descriptor with remote device
 * information into this sysfs file.
 *
 * A remote device is virtually attached to the root-hub port of @rhport with
 * @speed. @devid is embedded into a request to specify the remote device in a
 * server host.
 *
 * write() returns 0 on success, else negative errno.
 */
static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct vhci_device *vdev;
	struct socket *socket;
	int sockfd = 0;
	__u32 rhport = 0, devid = 0, speed = 0;
184
	int err;
185
	unsigned long flags;
186 187 188 189 190 191 192

	/*
	 * @rhport: port number of vhci_hcd
	 * @sockfd: socket descriptor of an established TCP connection
	 * @devid: unique device identifier in a remote host
	 * @speed: usb device speed in a remote host
	 */
193
	if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4)
194
		return -EINVAL;
195

196
	usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
197
			     rhport, sockfd, devid, speed);
198 199 200 201 202

	/* check received parameters */
	if (valid_args(rhport, speed) < 0)
		return -EINVAL;

203
	/* Extract socket from fd. */
204
	socket = sockfd_lookup(sockfd, &err);
205
	if (!socket)
206
		return -EINVAL;
207 208 209 210

	/* now need lock until setting vdev status as used */

	/* begin a lock */
211
	spin_lock_irqsave(&the_controller->lock, flags);
212 213 214 215 216 217
	vdev = port_to_vdev(rhport);
	spin_lock(&vdev->ud.lock);

	if (vdev->ud.status != VDEV_ST_NULL) {
		/* end of the lock */
		spin_unlock(&vdev->ud.lock);
218
		spin_unlock_irqrestore(&the_controller->lock, flags);
219

220
		sockfd_put(socket);
221

222
		dev_err(dev, "port %d already used\n", rhport);
223 224 225
		return -EINVAL;
	}

226 227 228
	dev_info(dev,
		 "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n",
		 rhport, sockfd, devid, speed, usb_speed_string(speed));
229 230 231

	vdev->devid         = devid;
	vdev->speed         = speed;
232
	vdev->ud.sockfd     = sockfd;
233 234 235 236
	vdev->ud.tcp_socket = socket;
	vdev->ud.status     = VDEV_ST_NOTASSIGNED;

	spin_unlock(&vdev->ud.lock);
237
	spin_unlock_irqrestore(&the_controller->lock, flags);
238 239
	/* end the lock */

240 241
	vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
	vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256
	rh_port_connect(rhport, speed);

	return count;
}
static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);

static struct attribute *dev_attrs[] = {
	&dev_attr_status.attr,
	&dev_attr_detach.attr,
	&dev_attr_attach.attr,
	&dev_attr_usbip_debug.attr,
	NULL,
};

257
const struct attribute_group dev_attr_group = {
258 259
	.attrs = dev_attrs,
};