~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/usb/usbip/src/usbipd.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/usb/usbip/src/usbipd.c (Version linux-6.12-rc7) and /tools/usb/usbip/src/usbipd.c (Version linux-4.4.302)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php