1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
| | // SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
* 2005-2007 Takahiro Hirofuchi
* Copyright (C) 2015-2016 Samsung Electronics
* Igor Kotrasinski <i.kotrasinsk@samsung.com>
* Krzysztof Opasiak <k.opasiak@samsung.com>
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#define _GNU_SOURCE
#include <getopt.h>
#include "usbip_host_driver.h"
#include "usbip_device_driver.h"
#include "usbip_network.h"
#undef PROGNAME
#define PROGNAME "in.usbipd"
#define MAXSOCKFD 20
#define MAIN_LOOP_TIMEOUT 10
static const char usbip_version_string[] = PACKAGE_STRING;
static const char in_usbipd_help_string[] =
"usage: in.usbipd [options]\n"
"\n"
" -e, --device\n"
" Run in device mode.\n"
" Rather than drive an attached device, create\n"
" a virtual UDC to bind gadgets to.\n"
"\n"
" -d, --debug\n"
" Print debugging information.\n"
"\n"
" -h, --help\n"
" Print this help.\n"
"\n"
" -v, --version\n"
" Show version.\n";
static struct usbip_host_driver *driver;
static void in_usbipd_help(void)
{
printf("%s\n", in_usbipd_help_string);
}
static int recv_request_import(int sockfd)
{
struct op_import_request req;
struct usbip_exported_device *edev;
struct usbip_usb_device pdu_udev;
struct list_head *i;
int found = 0;
int status = ST_OK;
int rc;
memset(&req, 0, sizeof(req));
rc = usbip_net_recv(sockfd, &req, sizeof(req));
if (rc < 0) {
dbg("usbip_net_recv failed: import request");
return -1;
}
PACK_OP_IMPORT_REQUEST(0, &req);
list_for_each(i, &driver->edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
info("found requested device: %s", req.busid);
found = 1;
break;
}
}
if (found) {
/* should set TCP_NODELAY for usbip */
usbip_net_set_nodelay(sockfd);
/* export device needs a TCP/IP socket descriptor */
status = usbip_export_device(edev, sockfd);
if (status < 0)
status = ST_NA;
} else {
info("requested device not found: %s", req.busid);
status = ST_NODEV;
}
rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, status);
if (rc < 0) {
dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
return -1;
}
if (status) {
dbg("import request busid %s: failed", req.busid);
return -1;
}
memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
usbip_net_pack_usb_device(1, &pdu_udev);
rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
if (rc < 0) {
dbg("usbip_net_send failed: devinfo");
return -1;
}
dbg("import request busid %s: complete", req.busid);
return 0;
}
static int send_reply_devlist(int connfd)
{
struct usbip_exported_device *edev;
struct usbip_usb_device pdu_udev;
struct usbip_usb_interface pdu_uinf;
struct op_devlist_reply reply;
struct list_head *j;
int rc, i;
/*
* Exclude devices that are already exported to a client from
* the exportable device list to avoid:
* - import requests for devices that are exported only to
* fail the request.
* - revealing devices that are imported by a client to
* another client.
*/
reply.ndev = 0;
/* number of exported devices */
list_for_each(j, &driver->edev_list) {
edev = list_entry(j, struct usbip_exported_device, node);
if (edev->status != SDEV_ST_USED)
reply.ndev += 1;
}
info("exportable devices: %d", reply.ndev);
rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
if (rc < 0) {
dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
return -1;
}
PACK_OP_DEVLIST_REPLY(1, &reply);
rc = usbip_net_send(connfd, &reply, sizeof(reply));
if (rc < 0) {
dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
return -1;
}
list_for_each(j, &driver->edev_list) {
edev = list_entry(j, struct usbip_exported_device, node);
if (edev->status == SDEV_ST_USED)
continue;
dump_usb_device(&edev->udev);
memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
usbip_net_pack_usb_device(1, &pdu_udev);
rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
if (rc < 0) {
dbg("usbip_net_send failed: pdu_udev");
return -1;
}
for (i = 0; i < edev->udev.bNumInterfaces; i++) {
dump_usb_interface(&edev->uinf[i]);
memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
usbip_net_pack_usb_interface(1, &pdu_uinf);
rc = usbip_net_send(connfd, &pdu_uinf,
sizeof(pdu_uinf));
if (rc < 0) {
err("usbip_net_send failed: pdu_uinf");
return -1;
}
}
}
return 0;
}
static int recv_request_devlist(int connfd)
{
struct op_devlist_request req;
int rc;
memset(&req, 0, sizeof(req));
rc = usbip_net_recv(connfd, &req, sizeof(req));
if (rc < 0) {
dbg("usbip_net_recv failed: devlist request");
return -1;
}
rc = send_reply_devlist(connfd);
if (rc < 0) {
dbg("send_reply_devlist failed");
return -1;
}
return 0;
}
static int recv_pdu(int connfd)
{
uint16_t code = OP_UNSPEC;
int ret;
int status;
ret = usbip_net_recv_op_common(connfd, &code, &status);
if (ret < 0) {
dbg("could not receive opcode: %#0x", code);
return -1;
}
ret = usbip_refresh_device_list(driver);
if (ret < 0) {
dbg("could not refresh device list: %d", ret);
return -1;
}
info("received request: %#0x(%d)", code, connfd);
switch (code) {
case OP_REQ_DEVLIST:
ret = recv_request_devlist(connfd);
break;
case OP_REQ_IMPORT:
ret = recv_request_import(connfd);
break;
case OP_REQ_DEVINFO:
case OP_REQ_CRYPKEY:
default:
err("received an unknown opcode: %#0x", code);
ret = -1;
}
if (ret == 0)
info("request %#0x(%d): complete", code, connfd);
else
info("request %#0x(%d): failed", code, connfd);
return ret;
}
int main(int argc, char *argv[])
{
static const struct option longopts[] = {
{ "debug", no_argument, NULL, 'd' },
{ "device", no_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
enum {
cmd_standalone_mode = 1,
cmd_help,
cmd_version
} cmd;
int opt, rc = -1;
usbip_use_stderr = 1;
usbip_use_syslog = 0;
if (geteuid() != 0)
err("not running as root?");
cmd = cmd_standalone_mode;
driver = &host_driver;
for (;;) {
opt = getopt_long(argc, argv, "dehv", longopts, NULL);
if (opt == -1)
break;
switch (opt) {
case 'd':
usbip_use_debug = 1;
break;
case 'h':
cmd = cmd_help;
break;
case 'v':
cmd = cmd_version;
break;
case 'e':
driver = &device_driver;
break;
case '?':
in_usbipd_help();
default:
goto err_out;
}
}
switch (cmd) {
case cmd_standalone_mode:
rc = recv_pdu(0);
break;
case cmd_version:
printf(PROGNAME " (%s)\n", usbip_version_string);
rc = 0;
break;
case cmd_help:
in_usbipd_help();
rc = 0;
break;
default:
in_usbipd_help();
goto err_out;
}
err_out:
return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
}
|