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