2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
6 * Krzysztof Opasiak <k.opasiak@samsung.com>
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 rc
= 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
,
145 (!error
? ST_OK
: ST_NA
));
147 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT
);
152 dbg("import request busid %s: failed", req
.busid
);
156 memcpy(&pdu_udev
, &edev
->udev
, sizeof(pdu_udev
));
157 usbip_net_pack_usb_device(1, &pdu_udev
);
159 rc
= usbip_net_send(sockfd
, &pdu_udev
, sizeof(pdu_udev
));
161 dbg("usbip_net_send failed: devinfo");
165 dbg("import request busid %s: complete", req
.busid
);
170 static int send_reply_devlist(int connfd
)
172 struct usbip_exported_device
*edev
;
173 struct usbip_usb_device pdu_udev
;
174 struct usbip_usb_interface pdu_uinf
;
175 struct op_devlist_reply reply
;
180 /* number of exported devices */
181 list_for_each(j
, &driver
->edev_list
) {
184 info("exportable devices: %d", reply
.ndev
);
186 rc
= usbip_net_send_op_common(connfd
, OP_REP_DEVLIST
, ST_OK
);
188 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST
);
191 PACK_OP_DEVLIST_REPLY(1, &reply
);
193 rc
= usbip_net_send(connfd
, &reply
, sizeof(reply
));
195 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST
);
199 list_for_each(j
, &driver
->edev_list
) {
200 edev
= list_entry(j
, struct usbip_exported_device
, node
);
201 dump_usb_device(&edev
->udev
);
202 memcpy(&pdu_udev
, &edev
->udev
, sizeof(pdu_udev
));
203 usbip_net_pack_usb_device(1, &pdu_udev
);
205 rc
= usbip_net_send(connfd
, &pdu_udev
, sizeof(pdu_udev
));
207 dbg("usbip_net_send failed: pdu_udev");
211 for (i
= 0; i
< edev
->udev
.bNumInterfaces
; i
++) {
212 dump_usb_interface(&edev
->uinf
[i
]);
213 memcpy(&pdu_uinf
, &edev
->uinf
[i
], sizeof(pdu_uinf
));
214 usbip_net_pack_usb_interface(1, &pdu_uinf
);
216 rc
= usbip_net_send(connfd
, &pdu_uinf
,
219 err("usbip_net_send failed: pdu_uinf");
228 static int recv_request_devlist(int connfd
)
230 struct op_devlist_request req
;
233 memset(&req
, 0, sizeof(req
));
235 rc
= usbip_net_recv(connfd
, &req
, sizeof(req
));
237 dbg("usbip_net_recv failed: devlist request");
241 rc
= send_reply_devlist(connfd
);
243 dbg("send_reply_devlist failed");
250 static int recv_pdu(int connfd
)
252 uint16_t code
= OP_UNSPEC
;
255 ret
= usbip_net_recv_op_common(connfd
, &code
);
257 dbg("could not receive opcode: %#0x", code
);
261 ret
= usbip_refresh_device_list(driver
);
263 dbg("could not refresh device list: %d", ret
);
267 info("received request: %#0x(%d)", code
, connfd
);
270 ret
= recv_request_devlist(connfd
);
273 ret
= recv_request_import(connfd
);
278 err("received an unknown opcode: %#0x", code
);
283 info("request %#0x(%d): complete", code
, connfd
);
285 info("request %#0x(%d): failed", code
, connfd
);
291 static int tcpd_auth(int connfd
)
293 struct request_info request
;
296 request_init(&request
, RQ_DAEMON
, PROGNAME
, RQ_FILE
, connfd
, 0);
298 rc
= hosts_access(&request
);
306 static int do_accept(int listenfd
)
309 struct sockaddr_storage ss
;
310 socklen_t len
= sizeof(ss
);
311 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
314 memset(&ss
, 0, sizeof(ss
));
316 connfd
= accept(listenfd
, (struct sockaddr
*)&ss
, &len
);
318 err("failed to accept connection");
322 rc
= getnameinfo((struct sockaddr
*)&ss
, len
, host
, sizeof(host
),
323 port
, sizeof(port
), NI_NUMERICHOST
| NI_NUMERICSERV
);
325 err("getnameinfo: %s", gai_strerror(rc
));
328 rc
= tcpd_auth(connfd
);
330 info("denied access from %s", host
);
335 info("connection from %s:%s", host
, port
);
340 int process_request(int listenfd
)
345 connfd
= do_accept(listenfd
);
358 static void addrinfo_to_text(struct addrinfo
*ai
, char buf
[],
359 const size_t buf_size
)
361 char hbuf
[NI_MAXHOST
];
362 char sbuf
[NI_MAXSERV
];
367 rc
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, hbuf
, sizeof(hbuf
),
368 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
370 err("getnameinfo: %s", gai_strerror(rc
));
372 snprintf(buf
, buf_size
, "%s:%s", hbuf
, sbuf
);
375 static int listen_all_addrinfo(struct addrinfo
*ai_head
, int sockfdlist
[],
379 int ret
, nsockfd
= 0;
380 const size_t ai_buf_size
= NI_MAXHOST
+ NI_MAXSERV
+ 2;
381 char ai_buf
[ai_buf_size
];
383 for (ai
= ai_head
; ai
&& nsockfd
< maxsockfd
; ai
= ai
->ai_next
) {
386 addrinfo_to_text(ai
, ai_buf
, ai_buf_size
);
387 dbg("opening %s", ai_buf
);
388 sock
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
390 err("socket: %s: %d (%s)",
391 ai_buf
, errno
, strerror(errno
));
395 usbip_net_set_reuseaddr(sock
);
396 usbip_net_set_nodelay(sock
);
397 /* We use seperate sockets for IPv4 and IPv6
398 * (see do_standalone_mode()) */
399 usbip_net_set_v6only(sock
);
401 ret
= bind(sock
, ai
->ai_addr
, ai
->ai_addrlen
);
403 err("bind: %s: %d (%s)",
404 ai_buf
, errno
, strerror(errno
));
409 ret
= listen(sock
, SOMAXCONN
);
411 err("listen: %s: %d (%s)",
412 ai_buf
, errno
, strerror(errno
));
417 info("listening on %s", ai_buf
);
418 sockfdlist
[nsockfd
++] = sock
;
424 static struct addrinfo
*do_getaddrinfo(char *host
, int ai_family
)
426 struct addrinfo hints
, *ai_head
;
429 memset(&hints
, 0, sizeof(hints
));
430 hints
.ai_family
= ai_family
;
431 hints
.ai_socktype
= SOCK_STREAM
;
432 hints
.ai_flags
= AI_PASSIVE
;
434 rc
= getaddrinfo(host
, usbip_port_string
, &hints
, &ai_head
);
436 err("failed to get a network address %s: %s", usbip_port_string
,
444 static void signal_handler(int i
)
446 dbg("received '%s' signal", strsignal(i
));
449 static void set_signal(void)
451 struct sigaction act
;
453 memset(&act
, 0, sizeof(act
));
454 act
.sa_handler
= signal_handler
;
455 sigemptyset(&act
.sa_mask
);
456 sigaction(SIGTERM
, &act
, NULL
);
457 sigaction(SIGINT
, &act
, NULL
);
458 act
.sa_handler
= SIG_IGN
;
459 sigaction(SIGCLD
, &act
, NULL
);
462 static const char *pid_file
;
464 static void write_pid_file(void)
467 dbg("creating pid file %s", pid_file
);
470 fp
= fopen(pid_file
, "w");
472 err("pid_file: %s: %d (%s)",
473 pid_file
, errno
, strerror(errno
));
476 fprintf(fp
, "%d\n", getpid());
481 static void remove_pid_file(void)
484 dbg("removing pid file %s", pid_file
);
489 static int do_standalone_mode(int daemonize
, int ipv4
, int ipv6
)
491 struct addrinfo
*ai_head
;
492 int sockfdlist
[MAXSOCKFD
];
496 struct timespec timeout
;
499 if (usbip_driver_open(driver
))
503 if (daemon(0, 0) < 0) {
504 err("daemonizing failed: %s", strerror(errno
));
505 usbip_driver_close(driver
);
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_driver_close(driver
);
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_driver_close(driver
);
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_driver_close(driver
);
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 { "device", no_argument
, NULL
, 'e' },
593 { "pid", optional_argument
, NULL
, 'P' },
594 { "tcp-port", required_argument
, NULL
, 't' },
595 { "help", no_argument
, NULL
, 'h' },
596 { "version", no_argument
, NULL
, 'v' },
601 cmd_standalone_mode
= 1,
607 int ipv4
= 0, ipv6
= 0;
612 usbip_use_stderr
= 1;
613 usbip_use_syslog
= 0;
616 err("not running as root?");
618 cmd
= cmd_standalone_mode
;
619 driver
= &host_driver
;
621 opt
= getopt_long(argc
, argv
, "46DdeP::t:hv", longopts
, NULL
);
643 pid_file
= optarg
? optarg
: DEFAULT_PID_FILE
;
646 usbip_setup_port_number(optarg
);
652 driver
= &device_driver
;
665 case cmd_standalone_mode
:
666 rc
= do_standalone_mode(daemonize
, ipv4
, ipv6
);
670 printf(PROGNAME
" (%s)\n", usbip_version_string
);
683 return (rc
> -1 ? EXIT_SUCCESS
: EXIT_FAILURE
);