3 * 2005-2007 Takahiro Hirofuchi
4 * Copyright (C) 2015-2016 Samsung Electronics
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "../config.h"
32 #include <sys/types.h>
34 #include <arpa/inet.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
46 #include "usbip_host_driver.h"
47 #include "usbip_host_common.h"
48 #include "usbip_device_driver.h"
49 #include "usbip_common.h"
50 #include "usbip_network.h"
54 #define PROGNAME "usbipd"
57 #define MAIN_LOOP_TIMEOUT 10
59 #define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
61 static const char usbip_version_string[] = PACKAGE_STRING;
63 static const char usbipd_help_string[] =
64 "usage: usbipd [options]\n"
67 " Bind to IPv4. Default is both.\n"
70 " Bind to IPv6. Default is both.\n"
73 " Run in device mode.\n"
74 " Rather than drive an attached device, create\n"
75 " a virtual UDC to bind gadgets to.\n"
78 " Run as a daemon process.\n"
81 " Print debugging information.\n"
83 " -PFILE, --pid FILE\n"
84 " Write process id to FILE.\n"
85 " If no FILE specified, use " DEFAULT_PID_FILE "\n"
87 " -tPORT, --tcp-port PORT\n"
88 " Listen on TCP/IP port PORT.\n"
96 static struct usbip_host_driver *driver;
98 static void usbipd_help(void)
100 printf("%s\n", usbipd_help_string);
103 static int recv_request_import(int sockfd)
105 struct op_import_request req;
106 struct usbip_exported_device *edev;
107 struct usbip_usb_device pdu_udev;
113 memset(&req, 0, sizeof(req));
115 rc = usbip_net_recv(sockfd, &req, sizeof(req));
117 dbg("usbip_net_recv failed: import request");
120 PACK_OP_IMPORT_REQUEST(0, &req);
122 list_for_each(i, &driver->edev_list) {
123 edev = list_entry(i, struct usbip_exported_device, node);
124 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
125 info("found requested device: %s", req.busid);
132 /* should set TCP_NODELAY for usbip */
133 usbip_net_set_nodelay(sockfd);
135 /* export device needs a TCP/IP socket descriptor */
136 status = usbip_export_device(edev, sockfd);
140 info("requested device not found: %s", req.busid);
144 rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, status);
146 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
151 dbg("import request busid %s: failed", req.busid);
155 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
156 usbip_net_pack_usb_device(1, &pdu_udev);
158 rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
160 dbg("usbip_net_send failed: devinfo");
164 dbg("import request busid %s: complete", req.busid);
169 static int send_reply_devlist(int connfd)
171 struct usbip_exported_device *edev;
172 struct usbip_usb_device pdu_udev;
173 struct usbip_usb_interface pdu_uinf;
174 struct op_devlist_reply reply;
179 * Exclude devices that are already exported to a client from
180 * the exportable device list to avoid:
181 * - import requests for devices that are exported only to
183 * - revealing devices that are imported by a client to
188 /* number of exported devices */
189 list_for_each(j, &driver->edev_list) {
190 edev = list_entry(j, struct usbip_exported_device, node);
191 if (edev->status != SDEV_ST_USED)
194 info("exportable devices: %d", reply.ndev);
196 rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
198 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
201 PACK_OP_DEVLIST_REPLY(1, &reply);
203 rc = usbip_net_send(connfd, &reply, sizeof(reply));
205 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
209 list_for_each(j, &driver->edev_list) {
210 edev = list_entry(j, struct usbip_exported_device, node);
211 if (edev->status == SDEV_ST_USED)
214 dump_usb_device(&edev->udev);
215 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
216 usbip_net_pack_usb_device(1, &pdu_udev);
218 rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
220 dbg("usbip_net_send failed: pdu_udev");
224 for (i = 0; i < edev->udev.bNumInterfaces; i++) {
225 dump_usb_interface(&edev->uinf[i]);
226 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
227 usbip_net_pack_usb_interface(1, &pdu_uinf);
229 rc = usbip_net_send(connfd, &pdu_uinf,
232 err("usbip_net_send failed: pdu_uinf");
241 static int recv_request_devlist(int connfd)
243 struct op_devlist_request req;
246 memset(&req, 0, sizeof(req));
248 rc = usbip_net_recv(connfd, &req, sizeof(req));
250 dbg("usbip_net_recv failed: devlist request");
254 rc = send_reply_devlist(connfd);
256 dbg("send_reply_devlist failed");
263 static int recv_pdu(int connfd)
265 uint16_t code = OP_UNSPEC;
269 ret = usbip_net_recv_op_common(connfd, &code, &status);
271 dbg("could not receive opcode: %#0x", code);
275 ret = usbip_refresh_device_list(driver);
277 dbg("could not refresh device list: %d", ret);
281 info("received request: %#0x(%d)", code, connfd);
284 ret = recv_request_devlist(connfd);
287 ret = recv_request_import(connfd);
292 err("received an unknown opcode: %#0x", code);
297 info("request %#0x(%d): complete", code, connfd);
299 info("request %#0x(%d): failed", code, connfd);
305 static int tcpd_auth(int connfd)
307 struct request_info request;
310 request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
312 rc = hosts_access(&request);
320 static int do_accept(int listenfd)
323 struct sockaddr_storage ss;
324 socklen_t len = sizeof(ss);
325 char host[NI_MAXHOST], port[NI_MAXSERV];
328 memset(&ss, 0, sizeof(ss));
330 connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
332 err("failed to accept connection");
336 rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
337 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
339 err("getnameinfo: %s", gai_strerror(rc));
342 rc = tcpd_auth(connfd);
344 info("denied access from %s", host);
349 info("connection from %s:%s", host, port);
354 int process_request(int listenfd)
359 connfd = do_accept(listenfd);
372 static void addrinfo_to_text(struct addrinfo *ai, char buf[],
373 const size_t buf_size)
375 char hbuf[NI_MAXHOST];
376 char sbuf[NI_MAXSERV];
381 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
382 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
384 err("getnameinfo: %s", gai_strerror(rc));
386 snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
389 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[],
393 int ret, nsockfd = 0;
394 const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
395 char ai_buf[ai_buf_size];
397 for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) {
400 addrinfo_to_text(ai, ai_buf, ai_buf_size);
401 dbg("opening %s", ai_buf);
402 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
404 err("socket: %s: %d (%s)",
405 ai_buf, errno, strerror(errno));
409 usbip_net_set_reuseaddr(sock);
410 usbip_net_set_nodelay(sock);
411 /* We use seperate sockets for IPv4 and IPv6
412 * (see do_standalone_mode()) */
413 usbip_net_set_v6only(sock);
415 ret = bind(sock, ai->ai_addr, ai->ai_addrlen);
417 err("bind: %s: %d (%s)",
418 ai_buf, errno, strerror(errno));
423 ret = listen(sock, SOMAXCONN);
425 err("listen: %s: %d (%s)",
426 ai_buf, errno, strerror(errno));
431 info("listening on %s", ai_buf);
432 sockfdlist[nsockfd++] = sock;
438 static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
440 struct addrinfo hints, *ai_head;
443 memset(&hints, 0, sizeof(hints));
444 hints.ai_family = ai_family;
445 hints.ai_socktype = SOCK_STREAM;
446 hints.ai_flags = AI_PASSIVE;
448 rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head);
450 err("failed to get a network address %s: %s", usbip_port_string,
458 static void signal_handler(int i)
460 dbg("received '%s' signal", strsignal(i));
463 static void set_signal(void)
465 struct sigaction act;
467 memset(&act, 0, sizeof(act));
468 act.sa_handler = signal_handler;
469 sigemptyset(&act.sa_mask);
470 sigaction(SIGTERM, &act, NULL);
471 sigaction(SIGINT, &act, NULL);
472 act.sa_handler = SIG_IGN;
473 sigaction(SIGCHLD, &act, NULL);
476 static const char *pid_file;
478 static void write_pid_file(void)
481 dbg("creating pid file %s", pid_file);
484 fp = fopen(pid_file, "w");
486 err("pid_file: %s: %d (%s)",
487 pid_file, errno, strerror(errno));
490 fprintf(fp, "%d\n", getpid());
495 static void remove_pid_file(void)
498 dbg("removing pid file %s", pid_file);
503 static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
505 struct addrinfo *ai_head;
506 int sockfdlist[MAXSOCKFD];
510 struct timespec timeout;
513 if (usbip_driver_open(driver))
517 if (daemon(0, 0) < 0) {
518 err("daemonizing failed: %s", strerror(errno));
519 usbip_driver_close(driver);
523 usbip_use_syslog = 1;
528 info("starting " PROGNAME " (%s)", usbip_version_string);
531 * To suppress warnings on systems with bindv6only disabled
532 * (default), we use seperate sockets for IPv6 and IPv4 and set
533 * IPV6_V6ONLY on the IPv6 sockets.
542 ai_head = do_getaddrinfo(NULL, family);
544 usbip_driver_close(driver);
547 nsockfd = listen_all_addrinfo(ai_head, sockfdlist,
548 sizeof(sockfdlist) / sizeof(*sockfdlist));
549 freeaddrinfo(ai_head);
551 err("failed to open a listening socket");
552 usbip_driver_close(driver);
556 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
558 fds = calloc(nsockfd, sizeof(struct pollfd));
559 for (i = 0; i < nsockfd; i++) {
560 fds[i].fd = sockfdlist[i];
561 fds[i].events = POLLIN;
563 timeout.tv_sec = MAIN_LOOP_TIMEOUT;
566 sigfillset(&sigmask);
567 sigdelset(&sigmask, SIGTERM);
568 sigdelset(&sigmask, SIGINT);
574 r = ppoll(fds, nsockfd, &timeout, &sigmask);
576 dbg("%s", strerror(errno));
579 for (i = 0; i < nsockfd; i++) {
580 if (fds[i].revents & POLLIN) {
581 dbg("read event on fd[%d]=%d",
583 process_request(sockfdlist[i]);
587 dbg("heartbeat timeout on ppoll()");
591 info("shutting down " PROGNAME);
593 usbip_driver_close(driver);
598 int main(int argc, char *argv[])
600 static const struct option longopts[] = {
601 { "ipv4", no_argument, NULL, '4' },
602 { "ipv6", no_argument, NULL, '6' },
603 { "daemon", no_argument, NULL, 'D' },
604 { "daemon", no_argument, NULL, 'D' },
605 { "debug", no_argument, NULL, 'd' },
606 { "device", no_argument, NULL, 'e' },
607 { "pid", optional_argument, NULL, 'P' },
608 { "tcp-port", required_argument, NULL, 't' },
609 { "help", no_argument, NULL, 'h' },
610 { "version", no_argument, NULL, 'v' },
615 cmd_standalone_mode = 1,
621 int ipv4 = 0, ipv6 = 0;
626 usbip_use_stderr = 1;
627 usbip_use_syslog = 0;
630 err("not running as root?");
632 cmd = cmd_standalone_mode;
633 driver = &host_driver;
635 opt = getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL);
657 pid_file = optarg ? optarg : DEFAULT_PID_FILE;
660 usbip_setup_port_number(optarg);
666 driver = &device_driver;
679 case cmd_standalone_mode:
680 rc = do_standalone_mode(daemonize, ipv4, ipv6);
684 printf(PROGNAME " (%s)\n", usbip_version_string);
697 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);