From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on atuin.qyliss.net X-Spam-Level: X-Spam-Status: No, score=-4.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: by atuin.qyliss.net (Postfix, from userid 496) id A294E81DD8; Wed, 13 Oct 2021 13:53:53 +0000 (UTC) Received: from atuin.qyliss.net (localhost [IPv6:::1]) by atuin.qyliss.net (Postfix) with ESMTP id 2FBE781D40; Wed, 13 Oct 2021 13:53:35 +0000 (UTC) Received: by atuin.qyliss.net (Postfix, from userid 496) id DDEB681D28; Wed, 13 Oct 2021 13:53:31 +0000 (UTC) Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by atuin.qyliss.net (Postfix) with ESMTPS id 44D0D81D25 for ; Wed, 13 Oct 2021 13:53:27 +0000 (UTC) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 5ABA05C01B5 for ; Wed, 13 Oct 2021 09:53:25 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Wed, 13 Oct 2021 09:53:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alyssa.is; h= from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=mmBHRt/qDteVO 6U4N6LfNEl4PpzcYtw64zeO4rEK/9M=; b=rYPC4HHdlzuUz0TuT0PxCZ8LBd+9n BtfHvCD0Ksk1aJIHzTKWWTB/0N+5e6mUmZKMO+mBM9sqUS1WVeERjqc9cTmFPrj9 CU+5M0Fzr1tzxMjc5heBiTbB6LU99QVIztkAr0DqF8s5zUpt4DNr8aMMSOdtmYMv BEPoq5yOWH5tM1FOJU3IIOMNifDVCUZ00YPeK6V/4zWwSxkiUxOPJ8VrYx4fW/Zp vPCA9yo7wRi+BoGsTYPRD2NFED6nGk4HPcaxklhEnYyoNEv9FEj1wN/NccohZC6H 1ozFPQZR+LG6tfmrrZlW/8xY1yP7E2B2KajsGze2eh71WFmSwGv7srSmg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=mmBHRt/qDteVO6U4N6LfNEl4PpzcYtw64zeO4rEK/9M=; b=lXIJVpKP EGDSCjOPlbWaSQnZcbHhS8HHkn8GWlivmcJOpTb3Oii/G5pv3qRg9nVwNqn+QFAU XC2xbSTS5F0do6u0kTPLtKSXBWIKi8OaeTJZpyjiuObCUhAmBJlB3+elXKYpReLX sHr9TvGnbF8jbspgUl3JJqwu7c6Z2C810BFGJ1rnGp3bs0iVgReYCcrGvH8V5GYF spN9cpOPK4uFpLlUcXP50MHy4xme+eTgJN2UpAh42m7rWwP7cckSXIiXpmy/f3Ri eI82v92rBSVbGsomasqUi1Qyx49ff/YlVKgrWH1OFlry0JXiVTqqSWjfHlR4eoGL sB9x6a9Ugs6wqw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrvddutddgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgjfhgggfestdekre dtredttdenucfhrhhomheptehlhihsshgrucftohhsshcuoehhihesrghlhihsshgrrdhi sheqnecuggftrfgrthhtvghrnhepgfefudekvdelieelledufeevheeglefggedvudejvd dtffeuueevffehleejkedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomhepqhihlhhishhsseigvddvtddrqhihlhhishhsrdhnvght X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 13 Oct 2021 09:53:25 -0400 (EDT) Received: by x220.qyliss.net (Postfix, from userid 1000) id DB72716BB; Wed, 13 Oct 2021 13:53:22 +0000 (UTC) From: Alyssa Ross To: devel@spectrum-os.org Subject: [DEMO PATCH linux 1/3] usbip: tools: in.usbipd: add Date: Wed, 13 Oct 2021 13:53:11 +0000 Message-Id: <20211013135313.2494599-2-hi@alyssa.is> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211013135313.2494599-1-hi@alyssa.is> References: <20211013135313.2494599-1-hi@alyssa.is> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: FFSCA62D6CVGVDEYKKXRWH2SD4AS754L X-Message-ID-Hash: FFSCA62D6CVGVDEYKKXRWH2SD4AS754L X-MailFrom: qyliss@x220.qyliss.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.4 Precedence: list List-Id: Patches and low-level development discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This is an inetd-style version of usbipd, which allows it to be easily socket-activated, and work with other types of socket. I implemented this by just copying usbipd.c, and then deleting all the code that created, bound, and listened on a socket. In future, this could be made more maintainable by having it share the bulk of its code with usbipd. Signed-off-by: Alyssa Ross --- tools/usb/usbip/src/Makefile.am | 4 +- tools/usb/usbip/src/in.usbipd.c | 326 ++++++++++++++++++++++++++++++++ 2 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 tools/usb/usbip/src/in.usbipd.c diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am index e26f39e0579d..d64613733fd5 100644 --- a/tools/usb/usbip/src/Makefile.am +++ b/tools/usb/usbip/src/Makefile.am @@ -3,10 +3,12 @@ AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"' AM_CFLAGS = @EXTRA_CFLAGS@ LDADD = $(top_builddir)/libsrc/libusbip.la -sbin_PROGRAMS := usbip usbipd +sbin_PROGRAMS := usbip usbipd in.usbipd usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \ usbip_attach.c usbip_detach.c usbip_list.c \ usbip_bind.c usbip_unbind.c usbip_port.c usbipd_SOURCES := usbip_network.h usbipd.c usbip_network.c + +in_usbipd_SOURCES := usbip_network.h in.usbipd.c usbip_network.c diff --git a/tools/usb/usbip/src/in.usbipd.c b/tools/usb/usbip/src/in.usbipd.c new file mode 100644 index 000000000000..fc60f485fb4c --- /dev/null +++ b/tools/usb/usbip/src/in.usbipd.c @@ -0,0 +1,326 @@ +// 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); +} -- 2.32.0