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 #include <sys/socket.h>
20
21 #include <string.h>
22
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #include <netinet/tcp.h>
26 #include <unistd.h>
27
28 #ifdef HAVE_LIBWRAP
29 #include <tcpd.h>
30 #endif
31
32 #include "usbip_common.h"
33 #include "usbip_network.h"
34
35 int usbip_port = 3240;
36 char *usbip_port_string = "3240";
37
usbip_setup_port_number(char * arg)38 void usbip_setup_port_number(char *arg)
39 {
40 dbg("parsing port arg '%s'", arg);
41 char *end;
42 unsigned long int port = strtoul(arg, &end, 10);
43
44 if (end == arg) {
45 err("port: could not parse '%s' as a decimal integer", arg);
46 return;
47 }
48
49 if (*end != '\0') {
50 err("port: garbage at end of '%s'", arg);
51 return;
52 }
53
54 if (port > UINT16_MAX) {
55 err("port: %s too high (max=%d)",
56 arg, UINT16_MAX);
57 return;
58 }
59
60 usbip_port = port;
61 usbip_port_string = arg;
62 info("using port %d (\"%s\")", usbip_port, usbip_port_string);
63 }
64
usbip_net_pack_uint32_t(int pack,uint32_t num)65 uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num)
66 {
67 uint32_t i;
68
69 if (pack)
70 i = htonl(num);
71 else
72 i = ntohl(num);
73
74 return i;
75 }
76
usbip_net_pack_uint16_t(int pack,uint16_t num)77 uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num)
78 {
79 uint16_t i;
80
81 if (pack)
82 i = htons(num);
83 else
84 i = ntohs(num);
85
86 return i;
87 }
88
usbip_net_pack_usb_device(int pack,struct usbip_usb_device * udev)89 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev)
90 {
91 udev->busnum = usbip_net_pack_uint32_t(pack, udev->busnum);
92 udev->devnum = usbip_net_pack_uint32_t(pack, udev->devnum);
93 udev->speed = usbip_net_pack_uint32_t(pack, udev->speed);
94
95 udev->idVendor = usbip_net_pack_uint16_t(pack, udev->idVendor);
96 udev->idProduct = usbip_net_pack_uint16_t(pack, udev->idProduct);
97 udev->bcdDevice = usbip_net_pack_uint16_t(pack, udev->bcdDevice);
98 }
99
usbip_net_pack_usb_interface(int pack,struct usbip_usb_interface * udev)100 void usbip_net_pack_usb_interface(int pack __attribute__((unused)),
101 struct usbip_usb_interface *udev
102 __attribute__((unused)))
103 {
104 /* uint8_t members need nothing */
105 }
106
usbip_net_xmit(int sockfd,void * buff,size_t bufflen,int sending)107 static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen,
108 int sending)
109 {
110 ssize_t nbytes;
111 ssize_t total = 0;
112
113 if (!bufflen)
114 return 0;
115
116 do {
117 if (sending)
118 nbytes = send(sockfd, buff, bufflen, 0);
119 else
120 nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL);
121
122 if (nbytes <= 0)
123 return -1;
124
125 buff = (void *)((intptr_t) buff + nbytes);
126 bufflen -= nbytes;
127 total += nbytes;
128
129 } while (bufflen > 0);
130
131 return total;
132 }
133
usbip_net_recv(int sockfd,void * buff,size_t bufflen)134 ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen)
135 {
136 return usbip_net_xmit(sockfd, buff, bufflen, 0);
137 }
138
usbip_net_send(int sockfd,void * buff,size_t bufflen)139 ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen)
140 {
141 return usbip_net_xmit(sockfd, buff, bufflen, 1);
142 }
143
usbip_net_pack_op_common(int pack,struct op_common * op_common)144 static inline void usbip_net_pack_op_common(int pack,
145 struct op_common *op_common)
146 {
147 op_common->version = usbip_net_pack_uint16_t(pack, op_common->version);
148 op_common->code = usbip_net_pack_uint16_t(pack, op_common->code);
149 op_common->status = usbip_net_pack_uint32_t(pack, op_common->status);
150 }
151
usbip_net_send_op_common(int sockfd,uint32_t code,uint32_t status)152 int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status)
153 {
154 struct op_common op_common;
155 int rc;
156
157 memset(&op_common, 0, sizeof(op_common));
158
159 op_common.version = USBIP_VERSION;
160 op_common.code = code;
161 op_common.status = status;
162
163 usbip_net_pack_op_common(1, &op_common);
164
165 rc = usbip_net_send(sockfd, &op_common, sizeof(op_common));
166 if (rc < 0) {
167 dbg("usbip_net_send failed: %d", rc);
168 return -1;
169 }
170
171 return 0;
172 }
173
usbip_net_recv_op_common(int sockfd,uint16_t * code,int * status)174 int usbip_net_recv_op_common(int sockfd, uint16_t *code, int *status)
175 {
176 struct op_common op_common;
177 int rc;
178
179 memset(&op_common, 0, sizeof(op_common));
180
181 rc = usbip_net_recv(sockfd, &op_common, sizeof(op_common));
182 if (rc < 0) {
183 dbg("usbip_net_recv failed: %d", rc);
184 goto err;
185 }
186
187 usbip_net_pack_op_common(0, &op_common);
188
189 if (op_common.version != USBIP_VERSION) {
190 err("USBIP Kernel and tool version mismatch: %d %d:",
191 op_common.version, USBIP_VERSION);
192 goto err;
193 }
194
195 switch (*code) {
196 case OP_UNSPEC:
197 break;
198 default:
199 if (op_common.code != *code) {
200 dbg("unexpected pdu %#0x for %#0x", op_common.code,
201 *code);
202 /* return error status */
203 *status = ST_ERROR;
204 goto err;
205 }
206 }
207
208 *status = op_common.status;
209
210 if (op_common.status != ST_OK) {
211 dbg("request failed at peer: %d", op_common.status);
212 goto err;
213 }
214
215 *code = op_common.code;
216
217 return 0;
218 err:
219 return -1;
220 }
221
usbip_net_set_reuseaddr(int sockfd)222 int usbip_net_set_reuseaddr(int sockfd)
223 {
224 const int val = 1;
225 int ret;
226
227 ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
228 if (ret < 0)
229 dbg("setsockopt: SO_REUSEADDR");
230
231 return ret;
232 }
233
usbip_net_set_nodelay(int sockfd)234 int usbip_net_set_nodelay(int sockfd)
235 {
236 const int val = 1;
237 int ret;
238
239 ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
240 if (ret < 0)
241 dbg("setsockopt: TCP_NODELAY");
242
243 return ret;
244 }
245
usbip_net_set_keepalive(int sockfd)246 int usbip_net_set_keepalive(int sockfd)
247 {
248 const int val = 1;
249 int ret;
250
251 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
252 if (ret < 0)
253 dbg("setsockopt: SO_KEEPALIVE");
254
255 return ret;
256 }
257
usbip_net_set_v6only(int sockfd)258 int usbip_net_set_v6only(int sockfd)
259 {
260 const int val = 1;
261 int ret;
262
263 ret = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
264 if (ret < 0)
265 dbg("setsockopt: IPV6_V6ONLY");
266
267 return ret;
268 }
269
270 /*
271 * IPv6 Ready
272 */
usbip_net_tcp_connect(char * hostname,char * service)273 int usbip_net_tcp_connect(char *hostname, char *service)
274 {
275 struct addrinfo hints, *res, *rp;
276 int sockfd;
277 int ret;
278
279 memset(&hints, 0, sizeof(hints));
280 hints.ai_family = AF_UNSPEC;
281 hints.ai_socktype = SOCK_STREAM;
282
283 /* get all possible addresses */
284 ret = getaddrinfo(hostname, service, &hints, &res);
285 if (ret < 0) {
286 dbg("getaddrinfo: %s service %s: %s", hostname, service,
287 gai_strerror(ret));
288 return ret;
289 }
290
291 /* try the addresses */
292 for (rp = res; rp; rp = rp->ai_next) {
293 sockfd = socket(rp->ai_family, rp->ai_socktype,
294 rp->ai_protocol);
295 if (sockfd < 0)
296 continue;
297
298 /* should set TCP_NODELAY for usbip */
299 usbip_net_set_nodelay(sockfd);
300 /* TODO: write code for heartbeat */
301 usbip_net_set_keepalive(sockfd);
302
303 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)
304 break;
305
306 close(sockfd);
307 }
308
309 freeaddrinfo(res);
310
311 if (!rp)
312 return EAI_SYSTEM;
313
314 return sockfd;
315 }
316