// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2011 matt mooney * 2005-2007 Takahiro Hirofuchi * Copyright (C) 2015-2016 Samsung Electronics * Igor Kotrasinski * Krzysztof Opasiak */ #ifdef HAVE_CONFIG_H #include "../config.h" #endif #define _GNU_SOURCE #include #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); }