^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * 2005-2007 Takahiro Hirofuchi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015-2016 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Igor Kotrasinski <i.kotrasinsk@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Krzysztof Opasiak <k.opasiak@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #ifdef HAVE_CONFIG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "../config.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <netdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <arpa/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <netinet/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifdef HAVE_LIBWRAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <tcpd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "usbip_host_driver.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "usbip_host_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "usbip_device_driver.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "usbip_network.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include "list.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #undef PROGNAME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define PROGNAME "usbipd"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define MAXSOCKFD 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define MAIN_LOOP_TIMEOUT 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static const char usbip_version_string[] = PACKAGE_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const char usbipd_help_string[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) "usage: usbipd [options]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) " -4, --ipv4\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) " Bind to IPv4. Default is both.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) " -6, --ipv6\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) " Bind to IPv6. Default is both.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) " -e, --device\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) " Run in device mode.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) " Rather than drive an attached device, create\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) " a virtual UDC to bind gadgets to.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) " -D, --daemon\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) " Run as a daemon process.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) " -d, --debug\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) " Print debugging information.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) " -PFILE, --pid FILE\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) " Write process id to FILE.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) " If no FILE specified, use " DEFAULT_PID_FILE "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) " -tPORT, --tcp-port PORT\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) " Listen on TCP/IP port PORT.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) " -h, --help\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) " Print this help.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) " -v, --version\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) " Show version.\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static struct usbip_host_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void usbipd_help(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) printf("%s\n", usbipd_help_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static int recv_request_import(int sockfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct op_import_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct usbip_exported_device *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct usbip_usb_device pdu_udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct list_head *i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int status = ST_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) memset(&req, 0, sizeof(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rc = usbip_net_recv(sockfd, &req, sizeof(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) dbg("usbip_net_recv failed: import request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) PACK_OP_IMPORT_REQUEST(0, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) list_for_each(i, &driver->edev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) edev = list_entry(i, struct usbip_exported_device, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) info("found requested device: %s", req.busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* should set TCP_NODELAY for usbip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) usbip_net_set_nodelay(sockfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* export device needs a TCP/IP socket descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) status = usbip_export_device(edev, sockfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) status = ST_NA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) info("requested device not found: %s", req.busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) status = ST_NODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) dbg("import request busid %s: failed", req.busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) usbip_net_pack_usb_device(1, &pdu_udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) dbg("usbip_net_send failed: devinfo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) dbg("import request busid %s: complete", req.busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int send_reply_devlist(int connfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct usbip_exported_device *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct usbip_usb_device pdu_udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct usbip_usb_interface pdu_uinf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct op_devlist_reply reply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct list_head *j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Exclude devices that are already exported to a client from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * the exportable device list to avoid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * - import requests for devices that are exported only to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * fail the request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * - revealing devices that are imported by a client to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * another client.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) reply.ndev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* number of exported devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) list_for_each(j, &driver->edev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) edev = list_entry(j, struct usbip_exported_device, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (edev->status != SDEV_ST_USED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) reply.ndev += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) info("exportable devices: %d", reply.ndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) PACK_OP_DEVLIST_REPLY(1, &reply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) rc = usbip_net_send(connfd, &reply, sizeof(reply));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) list_for_each(j, &driver->edev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) edev = list_entry(j, struct usbip_exported_device, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (edev->status == SDEV_ST_USED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dump_usb_device(&edev->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) usbip_net_pack_usb_device(1, &pdu_udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dbg("usbip_net_send failed: pdu_udev");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) for (i = 0; i < edev->udev.bNumInterfaces; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) dump_usb_interface(&edev->uinf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) usbip_net_pack_usb_interface(1, &pdu_uinf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) rc = usbip_net_send(connfd, &pdu_uinf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) sizeof(pdu_uinf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) err("usbip_net_send failed: pdu_uinf");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int recv_request_devlist(int connfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct op_devlist_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) memset(&req, 0, sizeof(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) rc = usbip_net_recv(connfd, &req, sizeof(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) dbg("usbip_net_recv failed: devlist request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) rc = send_reply_devlist(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) dbg("send_reply_devlist failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int recv_pdu(int connfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) uint16_t code = OP_UNSPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) ret = usbip_net_recv_op_common(connfd, &code, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dbg("could not receive opcode: %#0x", code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = usbip_refresh_device_list(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) dbg("could not refresh device list: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) info("received request: %#0x(%d)", code, connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case OP_REQ_DEVLIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) ret = recv_request_devlist(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) case OP_REQ_IMPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ret = recv_request_import(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case OP_REQ_DEVINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) case OP_REQ_CRYPKEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) err("received an unknown opcode: %#0x", code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) info("request %#0x(%d): complete", code, connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) info("request %#0x(%d): failed", code, connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #ifdef HAVE_LIBWRAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static int tcpd_auth(int connfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct request_info request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) fromhost(&request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) rc = hosts_access(&request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int do_accept(int listenfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int connfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct sockaddr_storage ss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) socklen_t len = sizeof(ss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) char host[NI_MAXHOST], port[NI_MAXSERV];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) memset(&ss, 0, sizeof(ss));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (connfd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) err("failed to accept connection");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) err("getnameinfo: %s", gai_strerror(rc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) #ifdef HAVE_LIBWRAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) rc = tcpd_auth(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) info("denied access from %s", host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) close(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) info("connection from %s:%s", host, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return connfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int process_request(int listenfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) pid_t childpid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int connfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) connfd = do_accept(listenfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (connfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) childpid = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (childpid == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) close(listenfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) recv_pdu(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) close(connfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static void addrinfo_to_text(struct addrinfo *ai, char buf[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) const size_t buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) char hbuf[NI_MAXHOST];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) char sbuf[NI_MAXSERV];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) err("getnameinfo: %s", gai_strerror(rc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int maxsockfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct addrinfo *ai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) int ret, nsockfd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) char ai_buf[ai_buf_size];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) addrinfo_to_text(ai, ai_buf, ai_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) dbg("opening %s", ai_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (sock < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) err("socket: %s: %d (%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ai_buf, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) usbip_net_set_reuseaddr(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) usbip_net_set_nodelay(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /* We use seperate sockets for IPv4 and IPv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * (see do_standalone_mode()) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) usbip_net_set_v6only(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ret = bind(sock, ai->ai_addr, ai->ai_addrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) err("bind: %s: %d (%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ai_buf, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) close(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ret = listen(sock, SOMAXCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) err("listen: %s: %d (%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) ai_buf, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) close(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) info("listening on %s", ai_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) sockfdlist[nsockfd++] = sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return nsockfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct addrinfo hints, *ai_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) memset(&hints, 0, sizeof(hints));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) hints.ai_family = ai_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) hints.ai_socktype = SOCK_STREAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) hints.ai_flags = AI_PASSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) err("failed to get a network address %s: %s", usbip_port_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) gai_strerror(rc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return ai_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void signal_handler(int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) dbg("received '%s' signal", strsignal(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static void set_signal(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) memset(&act, 0, sizeof(act));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) act.sa_handler = signal_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) sigemptyset(&act.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) sigaction(SIGTERM, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) sigaction(SIGINT, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) act.sa_handler = SIG_IGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) sigaction(SIGCHLD, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static const char *pid_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static void write_pid_file(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (pid_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) dbg("creating pid file %s", pid_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) fp = fopen(pid_file, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) err("pid_file: %s: %d (%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) pid_file, errno, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) fprintf(fp, "%d\n", getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static void remove_pid_file(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (pid_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) dbg("removing pid file %s", pid_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) unlink(pid_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct addrinfo *ai_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int sockfdlist[MAXSOCKFD];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) int nsockfd, family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) int i, terminate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct pollfd *fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct timespec timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) sigset_t sigmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (usbip_driver_open(driver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (daemonize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (daemon(0, 0) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) err("daemonizing failed: %s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) usbip_driver_close(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) umask(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) usbip_use_syslog = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) set_signal();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) write_pid_file();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) info("starting " PROGNAME " (%s)", usbip_version_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * To suppress warnings on systems with bindv6only disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * (default), we use seperate sockets for IPv6 and IPv4 and set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * IPV6_V6ONLY on the IPv6 sockets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (ipv4 && ipv6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) family = AF_UNSPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) else if (ipv4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) ai_head = do_getaddrinfo(NULL, family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (!ai_head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) usbip_driver_close(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) nsockfd = listen_all_addrinfo(ai_head, sockfdlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) sizeof(sockfdlist) / sizeof(*sockfdlist));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) freeaddrinfo(ai_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (nsockfd <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) err("failed to open a listening socket");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) usbip_driver_close(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) fds = calloc(nsockfd, sizeof(struct pollfd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) for (i = 0; i < nsockfd; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) fds[i].fd = sockfdlist[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) fds[i].events = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) timeout.tv_sec = MAIN_LOOP_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) timeout.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) sigfillset(&sigmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) sigdelset(&sigmask, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) sigdelset(&sigmask, SIGINT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) terminate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) while (!terminate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) r = ppoll(fds, nsockfd, &timeout, &sigmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (r < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) dbg("%s", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) terminate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) } else if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) for (i = 0; i < nsockfd; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (fds[i].revents & POLLIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) dbg("read event on fd[%d]=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) i, sockfdlist[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) process_request(sockfdlist[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) dbg("heartbeat timeout on ppoll()");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) info("shutting down " PROGNAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) free(fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) usbip_driver_close(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static const struct option longopts[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) { "ipv4", no_argument, NULL, '4' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) { "ipv6", no_argument, NULL, '6' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) { "daemon", no_argument, NULL, 'D' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) { "daemon", no_argument, NULL, 'D' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) { "debug", no_argument, NULL, 'd' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) { "device", no_argument, NULL, 'e' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) { "pid", optional_argument, NULL, 'P' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) { "tcp-port", required_argument, NULL, 't' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) { "help", no_argument, NULL, 'h' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) { "version", no_argument, NULL, 'v' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) { NULL, 0, NULL, 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) cmd_standalone_mode = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) cmd_help,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) cmd_version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) } cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) int daemonize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) int ipv4 = 0, ipv6 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int opt, rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) pid_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) usbip_use_stderr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) usbip_use_syslog = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (geteuid() != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) err("not running as root?");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) cmd = cmd_standalone_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) driver = &host_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) opt = getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (opt == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) case '4':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) ipv4 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) case '6':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) ipv6 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) daemonize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) usbip_use_debug = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) cmd = cmd_help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) case 'P':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) pid_file = optarg ? optarg : DEFAULT_PID_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) usbip_setup_port_number(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) case 'v':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) cmd = cmd_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) case 'e':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) driver = &device_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) usbipd_help();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) if (!ipv4 && !ipv6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) ipv4 = ipv6 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) case cmd_standalone_mode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) rc = do_standalone_mode(daemonize, ipv4, ipv6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) remove_pid_file();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) case cmd_version:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) printf(PROGNAME " (%s)\n", usbip_version_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) case cmd_help:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) usbipd_help();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) usbipd_help();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }