2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "../config.h"
29 #include <sys/types.h>
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
43 #include "usbip_host_driver.h"
44 #include "usbip_common.h"
45 #include "usbip_network.h"
49 #define PROGNAME "usbipd"
52 #define MAIN_LOOP_TIMEOUT 10
54 #define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
56 static const char usbip_version_string[] = PACKAGE_STRING;
58 static const char usbipd_help_string[] =
59 "usage: usbipd [options]\n"
62 " Bind to IPv4. Default is both.\n"
65 " Bind to IPv6. Default is both.\n"
68 " Run as a daemon process.\n"
71 " Print debugging information.\n"
73 " -PFILE, --pid FILE\n"
74 " Write process id to FILE.\n"
75 " If no FILE specified, use " DEFAULT_PID_FILE "\n"
77 " -tPORT, --tcp-port PORT\n"
78 " Listen on TCP/IP port PORT.\n"
86 static void usbipd_help(void)
88 printf("%s\n", usbipd_help_string);
91 static int recv_request_import(int sockfd)
93 struct op_import_request req;
94 struct op_common reply;
95 struct usbip_exported_device *edev;
96 struct usbip_usb_device pdu_udev;
102 memset(&req, 0, sizeof(req));
103 memset(&reply, 0, sizeof(reply));
105 rc = usbip_net_recv(sockfd, &req, sizeof(req));
107 dbg("usbip_net_recv failed: import request");
110 PACK_OP_IMPORT_REQUEST(0, &req);
112 list_for_each(i, &host_driver->edev_list) {
113 edev = list_entry(i, struct usbip_exported_device, node);
114 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
115 info("found requested device: %s", req.busid);
122 /* should set TCP_NODELAY for usbip */
123 usbip_net_set_nodelay(sockfd);
125 /* export device needs a TCP/IP socket descriptor */
126 rc = usbip_host_export_device(edev, sockfd);
130 info("requested device not found: %s", req.busid);
134 rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT,
135 (!error ? ST_OK : ST_NA));
137 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
142 dbg("import request busid %s: failed", req.busid);
146 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
147 usbip_net_pack_usb_device(1, &pdu_udev);
149 rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
151 dbg("usbip_net_send failed: devinfo");
155 dbg("import request busid %s: complete", req.busid);
160 static int send_reply_devlist(int connfd)
162 struct usbip_exported_device *edev;
163 struct usbip_usb_device pdu_udev;
164 struct usbip_usb_interface pdu_uinf;
165 struct op_devlist_reply reply;
170 /* number of exported devices */
171 list_for_each(j, &host_driver->edev_list) {
174 info("exportable devices: %d", reply.ndev);
176 rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
178 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
181 PACK_OP_DEVLIST_REPLY(1, &reply);
183 rc = usbip_net_send(connfd, &reply, sizeof(reply));
185 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
189 list_for_each(j, &host_driver->edev_list) {
190 edev = list_entry(j, struct usbip_exported_device, node);
191 dump_usb_device(&edev->udev);
192 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
193 usbip_net_pack_usb_device(1, &pdu_udev);
195 rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
197 dbg("usbip_net_send failed: pdu_udev");
201 for (i = 0; i < edev->udev.bNumInterfaces; i++) {
202 dump_usb_interface(&edev->uinf[i]);
203 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
204 usbip_net_pack_usb_interface(1, &pdu_uinf);
206 rc = usbip_net_send(connfd, &pdu_uinf,
209 err("usbip_net_send failed: pdu_uinf");
218 static int recv_request_devlist(int connfd)
220 struct op_devlist_request req;
223 memset(&req, 0, sizeof(req));
225 rc = usbip_net_recv(connfd, &req, sizeof(req));
227 dbg("usbip_net_recv failed: devlist request");
231 rc = send_reply_devlist(connfd);
233 dbg("send_reply_devlist failed");
240 static int recv_pdu(int connfd)
242 uint16_t code = OP_UNSPEC;
245 ret = usbip_net_recv_op_common(connfd, &code);
247 dbg("could not receive opcode: %#0x", code);
251 ret = usbip_host_refresh_device_list();
253 dbg("could not refresh device list: %d", ret);
257 info("received request: %#0x(%d)", code, connfd);
260 ret = recv_request_devlist(connfd);
263 ret = recv_request_import(connfd);
268 err("received an unknown opcode: %#0x", code);
273 info("request %#0x(%d): complete", code, connfd);
275 info("request %#0x(%d): failed", code, connfd);
281 static int tcpd_auth(int connfd)
283 struct request_info request;
286 request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
288 rc = hosts_access(&request);
296 static int do_accept(int listenfd)
299 struct sockaddr_storage ss;
300 socklen_t len = sizeof(ss);
301 char host[NI_MAXHOST], port[NI_MAXSERV];
304 memset(&ss, 0, sizeof(ss));
306 connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
308 err("failed to accept connection");
312 rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
313 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
315 err("getnameinfo: %s", gai_strerror(rc));
318 rc = tcpd_auth(connfd);
320 info("denied access from %s", host);
325 info("connection from %s:%s", host, port);
330 int process_request(int listenfd)
335 connfd = do_accept(listenfd);
348 static void addrinfo_to_text(struct addrinfo *ai, char buf[],
349 const size_t buf_size)
351 char hbuf[NI_MAXHOST];
352 char sbuf[NI_MAXSERV];
357 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
358 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
360 err("getnameinfo: %s", gai_strerror(rc));
362 snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
365 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[],
369 int ret, nsockfd = 0;
370 const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
371 char ai_buf[ai_buf_size];
373 for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) {
376 addrinfo_to_text(ai, ai_buf, ai_buf_size);
377 dbg("opening %s", ai_buf);
378 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
380 err("socket: %s: %d (%s)",
381 ai_buf, errno, strerror(errno));
385 usbip_net_set_reuseaddr(sock);
386 usbip_net_set_nodelay(sock);
387 /* We use seperate sockets for IPv4 and IPv6
388 * (see do_standalone_mode()) */
389 usbip_net_set_v6only(sock);
391 if (sock >= FD_SETSIZE) {
392 err("FD_SETSIZE: %s: sock=%d, max=%d",
393 ai_buf, sock, FD_SETSIZE);
398 ret = bind(sock, ai->ai_addr, ai->ai_addrlen);
400 err("bind: %s: %d (%s)",
401 ai_buf, errno, strerror(errno));
406 ret = listen(sock, SOMAXCONN);
408 err("listen: %s: %d (%s)",
409 ai_buf, errno, strerror(errno));
414 info("listening on %s", ai_buf);
415 sockfdlist[nsockfd++] = sock;
421 static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
423 struct addrinfo hints, *ai_head;
426 memset(&hints, 0, sizeof(hints));
427 hints.ai_family = ai_family;
428 hints.ai_socktype = SOCK_STREAM;
429 hints.ai_flags = AI_PASSIVE;
431 rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head);
433 err("failed to get a network address %s: %s", usbip_port_string,
441 static void signal_handler(int i)
443 dbg("received '%s' signal", strsignal(i));
446 static void set_signal(void)
448 struct sigaction act;
450 memset(&act, 0, sizeof(act));
451 act.sa_handler = signal_handler;
452 sigemptyset(&act.sa_mask);
453 sigaction(SIGTERM, &act, NULL);
454 sigaction(SIGINT, &act, NULL);
455 act.sa_handler = SIG_IGN;
456 sigaction(SIGCLD, &act, NULL);
459 static const char *pid_file;
461 static void write_pid_file(void)
464 dbg("creating pid file %s", pid_file);
467 fp = fopen(pid_file, "w");
469 err("pid_file: %s: %d (%s)",
470 pid_file, errno, strerror(errno));
473 fprintf(fp, "%d\n", getpid());
478 static void remove_pid_file(void)
481 dbg("removing pid file %s", pid_file);
486 static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
488 struct addrinfo *ai_head;
489 int sockfdlist[MAXSOCKFD];
493 struct timespec timeout;
496 if (usbip_host_driver_open()) {
497 err("please load " USBIP_CORE_MOD_NAME ".ko and "
498 USBIP_HOST_DRV_NAME ".ko!");
503 if (daemon(0, 0) < 0) {
504 err("daemonizing failed: %s", strerror(errno));
505 usbip_host_driver_close();
509 usbip_use_syslog = 1;
514 info("starting " PROGNAME " (%s)", usbip_version_string);
517 * To suppress warnings on systems with bindv6only disabled
518 * (default), we use seperate sockets for IPv6 and IPv4 and set
519 * IPV6_V6ONLY on the IPv6 sockets.
528 ai_head = do_getaddrinfo(NULL, family);
530 usbip_host_driver_close();
533 nsockfd = listen_all_addrinfo(ai_head, sockfdlist,
534 sizeof(sockfdlist) / sizeof(*sockfdlist));
535 freeaddrinfo(ai_head);
537 err("failed to open a listening socket");
538 usbip_host_driver_close();
542 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
544 fds = calloc(nsockfd, sizeof(struct pollfd));
545 for (i = 0; i < nsockfd; i++) {
546 fds[i].fd = sockfdlist[i];
547 fds[i].events = POLLIN;
549 timeout.tv_sec = MAIN_LOOP_TIMEOUT;
552 sigfillset(&sigmask);
553 sigdelset(&sigmask, SIGTERM);
554 sigdelset(&sigmask, SIGINT);
560 r = ppoll(fds, nsockfd, &timeout, &sigmask);
562 dbg("%s", strerror(errno));
565 for (i = 0; i < nsockfd; i++) {
566 if (fds[i].revents & POLLIN) {
567 dbg("read event on fd[%d]=%d",
569 process_request(sockfdlist[i]);
573 dbg("heartbeat timeout on ppoll()");
577 info("shutting down " PROGNAME);
579 usbip_host_driver_close();
584 int main(int argc, char *argv[])
586 static const struct option longopts[] = {
587 { "ipv4", no_argument, NULL, '4' },
588 { "ipv6", no_argument, NULL, '6' },
589 { "daemon", no_argument, NULL, 'D' },
590 { "daemon", no_argument, NULL, 'D' },
591 { "debug", no_argument, NULL, 'd' },
592 { "pid", optional_argument, NULL, 'P' },
593 { "tcp-port", required_argument, NULL, 't' },
594 { "help", no_argument, NULL, 'h' },
595 { "version", no_argument, NULL, 'v' },
600 cmd_standalone_mode = 1,
606 int ipv4 = 0, ipv6 = 0;
611 usbip_use_stderr = 1;
612 usbip_use_syslog = 0;
615 err("not running as root?");
617 cmd = cmd_standalone_mode;
619 opt = getopt_long(argc, argv, "46DdP::t:hv", longopts, NULL);
641 pid_file = optarg ? optarg : DEFAULT_PID_FILE;
644 usbip_setup_port_number(optarg);
660 case cmd_standalone_mode:
661 rc = do_standalone_mode(daemonize, ipv4, ipv6);
665 printf(PROGNAME " (%s)\n", usbip_version_string);
678 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);