xref: /linux/tools/usb/usbip/src/usbip_network.c (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2588b48caSValentina Manea /*
3588b48caSValentina Manea  * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
4588b48caSValentina Manea  *               2005-2007 Takahiro Hirofuchi
5588b48caSValentina Manea  */
6588b48caSValentina Manea 
7588b48caSValentina Manea #include <sys/socket.h>
8588b48caSValentina Manea 
9588b48caSValentina Manea #include <string.h>
10588b48caSValentina Manea 
11588b48caSValentina Manea #include <arpa/inet.h>
12588b48caSValentina Manea #include <netdb.h>
13588b48caSValentina Manea #include <netinet/tcp.h>
14588b48caSValentina Manea #include <unistd.h>
15588b48caSValentina Manea 
16588b48caSValentina Manea #ifdef HAVE_LIBWRAP
17588b48caSValentina Manea #include <tcpd.h>
18588b48caSValentina Manea #endif
19588b48caSValentina Manea 
20588b48caSValentina Manea #include "usbip_common.h"
21588b48caSValentina Manea #include "usbip_network.h"
22588b48caSValentina Manea 
23588b48caSValentina Manea int usbip_port = 3240;
24588b48caSValentina Manea char *usbip_port_string = "3240";
25588b48caSValentina Manea 
usbip_setup_port_number(char * arg)26588b48caSValentina Manea void usbip_setup_port_number(char *arg)
27588b48caSValentina Manea {
28588b48caSValentina Manea 	dbg("parsing port arg '%s'", arg);
29588b48caSValentina Manea 	char *end;
30588b48caSValentina Manea 	unsigned long int port = strtoul(arg, &end, 10);
31588b48caSValentina Manea 
32588b48caSValentina Manea 	if (end == arg) {
33588b48caSValentina Manea 		err("port: could not parse '%s' as a decimal integer", arg);
34588b48caSValentina Manea 		return;
35588b48caSValentina Manea 	}
36588b48caSValentina Manea 
37588b48caSValentina Manea 	if (*end != '\0') {
38588b48caSValentina Manea 		err("port: garbage at end of '%s'", arg);
39588b48caSValentina Manea 		return;
40588b48caSValentina Manea 	}
41588b48caSValentina Manea 
42588b48caSValentina Manea 	if (port > UINT16_MAX) {
43588b48caSValentina Manea 		err("port: %s too high (max=%d)",
44588b48caSValentina Manea 		    arg, UINT16_MAX);
45588b48caSValentina Manea 		return;
46588b48caSValentina Manea 	}
47588b48caSValentina Manea 
48588b48caSValentina Manea 	usbip_port = port;
49588b48caSValentina Manea 	usbip_port_string = arg;
50588b48caSValentina Manea 	info("using port %d (\"%s\")", usbip_port, usbip_port_string);
51588b48caSValentina Manea }
52588b48caSValentina Manea 
usbip_net_pack_uint32_t(int pack,uint32_t num)53*585c91f4SShuah Khan uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num)
54588b48caSValentina Manea {
55588b48caSValentina Manea 	uint32_t i;
56588b48caSValentina Manea 
57588b48caSValentina Manea 	if (pack)
58*585c91f4SShuah Khan 		i = htonl(num);
59588b48caSValentina Manea 	else
60*585c91f4SShuah Khan 		i = ntohl(num);
61588b48caSValentina Manea 
62*585c91f4SShuah Khan 	return i;
63588b48caSValentina Manea }
64588b48caSValentina Manea 
usbip_net_pack_uint16_t(int pack,uint16_t num)65*585c91f4SShuah Khan uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num)
66588b48caSValentina Manea {
67588b48caSValentina Manea 	uint16_t i;
68588b48caSValentina Manea 
69588b48caSValentina Manea 	if (pack)
70*585c91f4SShuah Khan 		i = htons(num);
71588b48caSValentina Manea 	else
72*585c91f4SShuah Khan 		i = ntohs(num);
73588b48caSValentina Manea 
74*585c91f4SShuah Khan 	return i;
75588b48caSValentina Manea }
76588b48caSValentina Manea 
usbip_net_pack_usb_device(int pack,struct usbip_usb_device * udev)77588b48caSValentina Manea void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev)
78588b48caSValentina Manea {
79*585c91f4SShuah Khan 	udev->busnum = usbip_net_pack_uint32_t(pack, udev->busnum);
80*585c91f4SShuah Khan 	udev->devnum = usbip_net_pack_uint32_t(pack, udev->devnum);
81*585c91f4SShuah Khan 	udev->speed = usbip_net_pack_uint32_t(pack, udev->speed);
82588b48caSValentina Manea 
83*585c91f4SShuah Khan 	udev->idVendor = usbip_net_pack_uint16_t(pack, udev->idVendor);
84*585c91f4SShuah Khan 	udev->idProduct = usbip_net_pack_uint16_t(pack, udev->idProduct);
85*585c91f4SShuah Khan 	udev->bcdDevice = usbip_net_pack_uint16_t(pack, udev->bcdDevice);
86588b48caSValentina Manea }
87588b48caSValentina Manea 
usbip_net_pack_usb_interface(int pack,struct usbip_usb_interface * udev)88588b48caSValentina Manea void usbip_net_pack_usb_interface(int pack __attribute__((unused)),
89588b48caSValentina Manea 				  struct usbip_usb_interface *udev
90588b48caSValentina Manea 				  __attribute__((unused)))
91588b48caSValentina Manea {
92588b48caSValentina Manea 	/* uint8_t members need nothing */
93588b48caSValentina Manea }
94588b48caSValentina Manea 
usbip_net_xmit(int sockfd,void * buff,size_t bufflen,int sending)95588b48caSValentina Manea static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen,
96588b48caSValentina Manea 			      int sending)
97588b48caSValentina Manea {
98588b48caSValentina Manea 	ssize_t nbytes;
99588b48caSValentina Manea 	ssize_t total = 0;
100588b48caSValentina Manea 
101588b48caSValentina Manea 	if (!bufflen)
102588b48caSValentina Manea 		return 0;
103588b48caSValentina Manea 
104588b48caSValentina Manea 	do {
105588b48caSValentina Manea 		if (sending)
106588b48caSValentina Manea 			nbytes = send(sockfd, buff, bufflen, 0);
107588b48caSValentina Manea 		else
108588b48caSValentina Manea 			nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL);
109588b48caSValentina Manea 
110588b48caSValentina Manea 		if (nbytes <= 0)
111588b48caSValentina Manea 			return -1;
112588b48caSValentina Manea 
113588b48caSValentina Manea 		buff	 = (void *)((intptr_t) buff + nbytes);
114588b48caSValentina Manea 		bufflen	-= nbytes;
115588b48caSValentina Manea 		total	+= nbytes;
116588b48caSValentina Manea 
117588b48caSValentina Manea 	} while (bufflen > 0);
118588b48caSValentina Manea 
119588b48caSValentina Manea 	return total;
120588b48caSValentina Manea }
121588b48caSValentina Manea 
usbip_net_recv(int sockfd,void * buff,size_t bufflen)122588b48caSValentina Manea ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen)
123588b48caSValentina Manea {
124588b48caSValentina Manea 	return usbip_net_xmit(sockfd, buff, bufflen, 0);
125588b48caSValentina Manea }
126588b48caSValentina Manea 
usbip_net_send(int sockfd,void * buff,size_t bufflen)127588b48caSValentina Manea ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen)
128588b48caSValentina Manea {
129588b48caSValentina Manea 	return usbip_net_xmit(sockfd, buff, bufflen, 1);
130588b48caSValentina Manea }
131588b48caSValentina Manea 
usbip_net_pack_op_common(int pack,struct op_common * op_common)132*585c91f4SShuah Khan static inline void usbip_net_pack_op_common(int pack,
133*585c91f4SShuah Khan 					    struct op_common *op_common)
134*585c91f4SShuah Khan {
135*585c91f4SShuah Khan 	op_common->version = usbip_net_pack_uint16_t(pack, op_common->version);
136*585c91f4SShuah Khan 	op_common->code = usbip_net_pack_uint16_t(pack, op_common->code);
137*585c91f4SShuah Khan 	op_common->status = usbip_net_pack_uint32_t(pack, op_common->status);
138*585c91f4SShuah Khan }
139*585c91f4SShuah Khan 
usbip_net_send_op_common(int sockfd,uint32_t code,uint32_t status)140588b48caSValentina Manea int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status)
141588b48caSValentina Manea {
142588b48caSValentina Manea 	struct op_common op_common;
143588b48caSValentina Manea 	int rc;
144588b48caSValentina Manea 
145588b48caSValentina Manea 	memset(&op_common, 0, sizeof(op_common));
146588b48caSValentina Manea 
147588b48caSValentina Manea 	op_common.version = USBIP_VERSION;
148588b48caSValentina Manea 	op_common.code    = code;
149588b48caSValentina Manea 	op_common.status  = status;
150588b48caSValentina Manea 
151*585c91f4SShuah Khan 	usbip_net_pack_op_common(1, &op_common);
152588b48caSValentina Manea 
153588b48caSValentina Manea 	rc = usbip_net_send(sockfd, &op_common, sizeof(op_common));
154588b48caSValentina Manea 	if (rc < 0) {
155588b48caSValentina Manea 		dbg("usbip_net_send failed: %d", rc);
156588b48caSValentina Manea 		return -1;
157588b48caSValentina Manea 	}
158588b48caSValentina Manea 
159588b48caSValentina Manea 	return 0;
160588b48caSValentina Manea }
161588b48caSValentina Manea 
usbip_net_recv_op_common(int sockfd,uint16_t * code,int * status)162ad81b15dSShuah Khan int usbip_net_recv_op_common(int sockfd, uint16_t *code, int *status)
163588b48caSValentina Manea {
164588b48caSValentina Manea 	struct op_common op_common;
165588b48caSValentina Manea 	int rc;
166588b48caSValentina Manea 
167588b48caSValentina Manea 	memset(&op_common, 0, sizeof(op_common));
168588b48caSValentina Manea 
169588b48caSValentina Manea 	rc = usbip_net_recv(sockfd, &op_common, sizeof(op_common));
170588b48caSValentina Manea 	if (rc < 0) {
171588b48caSValentina Manea 		dbg("usbip_net_recv failed: %d", rc);
172588b48caSValentina Manea 		goto err;
173588b48caSValentina Manea 	}
174588b48caSValentina Manea 
175*585c91f4SShuah Khan 	usbip_net_pack_op_common(0, &op_common);
176588b48caSValentina Manea 
177588b48caSValentina Manea 	if (op_common.version != USBIP_VERSION) {
1788fe8f582SShuah Khan 		err("USBIP Kernel and tool version mismatch: %d %d:",
1798fe8f582SShuah Khan 		    op_common.version, USBIP_VERSION);
180588b48caSValentina Manea 		goto err;
181588b48caSValentina Manea 	}
182588b48caSValentina Manea 
183588b48caSValentina Manea 	switch (*code) {
184588b48caSValentina Manea 	case OP_UNSPEC:
185588b48caSValentina Manea 		break;
186588b48caSValentina Manea 	default:
187588b48caSValentina Manea 		if (op_common.code != *code) {
188588b48caSValentina Manea 			dbg("unexpected pdu %#0x for %#0x", op_common.code,
189588b48caSValentina Manea 			    *code);
190ad81b15dSShuah Khan 			/* return error status */
191ad81b15dSShuah Khan 			*status = ST_ERROR;
192588b48caSValentina Manea 			goto err;
193588b48caSValentina Manea 		}
194588b48caSValentina Manea 	}
195588b48caSValentina Manea 
196ad81b15dSShuah Khan 	*status = op_common.status;
197ad81b15dSShuah Khan 
198588b48caSValentina Manea 	if (op_common.status != ST_OK) {
199588b48caSValentina Manea 		dbg("request failed at peer: %d", op_common.status);
200588b48caSValentina Manea 		goto err;
201588b48caSValentina Manea 	}
202588b48caSValentina Manea 
203588b48caSValentina Manea 	*code = op_common.code;
204588b48caSValentina Manea 
205588b48caSValentina Manea 	return 0;
206588b48caSValentina Manea err:
207588b48caSValentina Manea 	return -1;
208588b48caSValentina Manea }
209588b48caSValentina Manea 
usbip_net_set_reuseaddr(int sockfd)210588b48caSValentina Manea int usbip_net_set_reuseaddr(int sockfd)
211588b48caSValentina Manea {
212588b48caSValentina Manea 	const int val = 1;
213588b48caSValentina Manea 	int ret;
214588b48caSValentina Manea 
215588b48caSValentina Manea 	ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
216588b48caSValentina Manea 	if (ret < 0)
217588b48caSValentina Manea 		dbg("setsockopt: SO_REUSEADDR");
218588b48caSValentina Manea 
219588b48caSValentina Manea 	return ret;
220588b48caSValentina Manea }
221588b48caSValentina Manea 
usbip_net_set_nodelay(int sockfd)222588b48caSValentina Manea int usbip_net_set_nodelay(int sockfd)
223588b48caSValentina Manea {
224588b48caSValentina Manea 	const int val = 1;
225588b48caSValentina Manea 	int ret;
226588b48caSValentina Manea 
227588b48caSValentina Manea 	ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
228588b48caSValentina Manea 	if (ret < 0)
229588b48caSValentina Manea 		dbg("setsockopt: TCP_NODELAY");
230588b48caSValentina Manea 
231588b48caSValentina Manea 	return ret;
232588b48caSValentina Manea }
233588b48caSValentina Manea 
usbip_net_set_keepalive(int sockfd)234588b48caSValentina Manea int usbip_net_set_keepalive(int sockfd)
235588b48caSValentina Manea {
236588b48caSValentina Manea 	const int val = 1;
237588b48caSValentina Manea 	int ret;
238588b48caSValentina Manea 
239588b48caSValentina Manea 	ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
240588b48caSValentina Manea 	if (ret < 0)
241588b48caSValentina Manea 		dbg("setsockopt: SO_KEEPALIVE");
242588b48caSValentina Manea 
243588b48caSValentina Manea 	return ret;
244588b48caSValentina Manea }
245588b48caSValentina Manea 
usbip_net_set_v6only(int sockfd)246588b48caSValentina Manea int usbip_net_set_v6only(int sockfd)
247588b48caSValentina Manea {
248588b48caSValentina Manea 	const int val = 1;
249588b48caSValentina Manea 	int ret;
250588b48caSValentina Manea 
251588b48caSValentina Manea 	ret = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
252588b48caSValentina Manea 	if (ret < 0)
253588b48caSValentina Manea 		dbg("setsockopt: IPV6_V6ONLY");
254588b48caSValentina Manea 
255588b48caSValentina Manea 	return ret;
256588b48caSValentina Manea }
257588b48caSValentina Manea 
258588b48caSValentina Manea /*
259588b48caSValentina Manea  * IPv6 Ready
260588b48caSValentina Manea  */
usbip_net_tcp_connect(char * hostname,char * service)261588b48caSValentina Manea int usbip_net_tcp_connect(char *hostname, char *service)
262588b48caSValentina Manea {
263588b48caSValentina Manea 	struct addrinfo hints, *res, *rp;
264588b48caSValentina Manea 	int sockfd;
265588b48caSValentina Manea 	int ret;
266588b48caSValentina Manea 
267588b48caSValentina Manea 	memset(&hints, 0, sizeof(hints));
268588b48caSValentina Manea 	hints.ai_family = AF_UNSPEC;
269588b48caSValentina Manea 	hints.ai_socktype = SOCK_STREAM;
270588b48caSValentina Manea 
271588b48caSValentina Manea 	/* get all possible addresses */
272588b48caSValentina Manea 	ret = getaddrinfo(hostname, service, &hints, &res);
273588b48caSValentina Manea 	if (ret < 0) {
274588b48caSValentina Manea 		dbg("getaddrinfo: %s service %s: %s", hostname, service,
275588b48caSValentina Manea 		    gai_strerror(ret));
276588b48caSValentina Manea 		return ret;
277588b48caSValentina Manea 	}
278588b48caSValentina Manea 
279588b48caSValentina Manea 	/* try the addresses */
280588b48caSValentina Manea 	for (rp = res; rp; rp = rp->ai_next) {
281588b48caSValentina Manea 		sockfd = socket(rp->ai_family, rp->ai_socktype,
282588b48caSValentina Manea 				rp->ai_protocol);
283588b48caSValentina Manea 		if (sockfd < 0)
284588b48caSValentina Manea 			continue;
285588b48caSValentina Manea 
286588b48caSValentina Manea 		/* should set TCP_NODELAY for usbip */
287588b48caSValentina Manea 		usbip_net_set_nodelay(sockfd);
288588b48caSValentina Manea 		/* TODO: write code for heartbeat */
289588b48caSValentina Manea 		usbip_net_set_keepalive(sockfd);
290588b48caSValentina Manea 
291588b48caSValentina Manea 		if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)
292588b48caSValentina Manea 			break;
293588b48caSValentina Manea 
294588b48caSValentina Manea 		close(sockfd);
295588b48caSValentina Manea 	}
296588b48caSValentina Manea 
297588b48caSValentina Manea 	freeaddrinfo(res);
298588b48caSValentina Manea 
299588b48caSValentina Manea 	if (!rp)
300588b48caSValentina Manea 		return EAI_SYSTEM;
301588b48caSValentina Manea 
302588b48caSValentina Manea 	return sockfd;
303588b48caSValentina Manea }
304