usbipd.c 14 KB

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