1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#if !defined(_GNU_SOURCE)
26#define _GNU_SOURCE
27#endif
28#include "private-lib-core.h"
29
30#include <sys/ioctl.h>
31
32#if !defined(LWS_DETECTED_PLAT_IOS)
33#include <net/route.h>
34#endif
35
36#include <net/if.h>
37
38#include <pwd.h>
39#include <grp.h>
40
41#if defined(LWS_WITH_MBEDTLS)
42#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
43#include "mbedtls/net_sockets.h"
44#else
45#include "mbedtls/net.h"
46#endif
47#endif
48
49#include <netinet/ip.h>
50
51int
52lws_send_pipe_choked(struct lws *wsi)
53{
54	struct lws_pollfd fds;
55	struct lws *wsi_eff;
56
57#if !defined(LWS_WITHOUT_EXTENSIONS)
58	if (wsi->ws && wsi->ws->tx_draining_ext)
59		return 1;
60#endif
61
62#if defined(LWS_WITH_HTTP2)
63	wsi_eff = lws_get_network_wsi(wsi);
64#else
65	wsi_eff = wsi;
66#endif
67
68	/* the fact we checked implies we avoided back-to-back writes */
69	wsi_eff->could_have_pending = 0;
70
71	/* treat the fact we got a truncated send pending as if we're choked */
72	if (lws_has_buffered_out(wsi_eff)
73#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
74	    ||wsi->http.comp_ctx.buflist_comp ||
75	    wsi->http.comp_ctx.may_have_more
76#endif
77	    )
78		return 1;
79
80	fds.fd = wsi_eff->desc.sockfd;
81	fds.events = POLLOUT;
82	fds.revents = 0;
83
84	if (poll(&fds, 1, 0) != 1)
85		return 1;
86
87	if ((fds.revents & POLLOUT) == 0)
88		return 1;
89
90	/* okay to send another packet without blocking */
91
92	return 0;
93}
94
95int
96lws_plat_set_nonblocking(lws_sockfd_type fd)
97{
98	return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
99}
100
101int
102lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
103{
104#ifdef TCP_SYNCNT
105    int max_retry = 2;
106    if (setsockopt(fd, IPPROTO_TCP, TCP_SYNCNT, &max_retry, sizeof(max_retry)) < 0) {
107        return 1;
108    }
109#endif
110	int optval = 1;
111	socklen_t optlen = sizeof(optval);
112
113#if defined(__APPLE__) || \
114    defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
115    defined(__NetBSD__) || \
116    defined(__OpenBSD__) || \
117    defined(__HAIKU__)
118	struct protoent *tcp_proto;
119#endif
120
121	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
122
123	if (!unix_skt && vhost->ka_time) {
124		/* enable keepalive on this socket */
125		optval = 1;
126		if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
127			       (const void *)&optval, optlen) < 0)
128			return 1;
129
130#if defined(__APPLE__) || \
131    defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
132    defined(__NetBSD__) || \
133    defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
134    defined(__HAIKU__)
135
136		/*
137		 * didn't find a way to set these per-socket, need to
138		 * tune kernel systemwide values
139		 */
140#else
141		/* set the keepalive conditions we want on it too */
142
143#if defined(LWS_HAVE_TCP_USER_TIMEOUT)
144		optval = 1000 * (vhost->ka_time +
145				 (vhost->ka_interval * vhost->ka_probes));
146		if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
147			       (const void *)&optval, optlen) < 0)
148			return 1;
149#endif
150		optval = vhost->ka_time;
151		if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
152			       (const void *)&optval, optlen) < 0)
153			return 1;
154
155		optval = vhost->ka_interval;
156		if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
157			       (const void *)&optval, optlen) < 0)
158			return 1;
159
160		optval = vhost->ka_probes;
161		if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
162			       (const void *)&optval, optlen) < 0)
163			return 1;
164#endif
165	}
166
167#if defined(SO_BINDTODEVICE)
168	if (!unix_skt && vhost->bind_iface && vhost->iface) {
169		lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
170		if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
171				(socklen_t)strlen(vhost->iface)) < 0) {
172			lwsl_warn("Failed to bind to device %s\n", vhost->iface);
173			return 1;
174		}
175	}
176#endif
177
178	/* Disable Nagle */
179	optval = 1;
180#if defined (__sun) || defined(__QNX__)
181	if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
182		return 1;
183#elif !defined(__APPLE__) && \
184      !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
185      !defined(__NetBSD__) && \
186      !defined(__OpenBSD__) && \
187      !defined(__HAIKU__)
188	if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
189		return 1;
190#else
191	tcp_proto = getprotobyname("TCP");
192	if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
193		return 1;
194#endif
195
196	return lws_plat_set_nonblocking(fd);
197}
198
199static const int ip_opt_lws_flags[] = {
200	LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT,
201	LCCSCF_IP_HIGH_RELIABILITY
202#if !defined(__OpenBSD__)
203	, LCCSCF_IP_LOW_COST
204#endif
205}, ip_opt_val[] = {
206	IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY
207#if !defined(__OpenBSD__) && !defined(__sun) && !defined(__QNX__)
208	, IPTOS_MINCOST
209#endif
210};
211#if !defined(LWS_WITH_NO_LOGS)
212static const char *ip_opt_names[] = {
213	"LOWDELAY", "THROUGHPUT", "RELIABILITY"
214#if !defined(__OpenBSD__) && !defined(__sun)
215	, "MINCOST"
216#endif
217};
218#endif
219
220int
221lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
222{
223	int optval = (int)pri, ret = 0, n;
224	socklen_t optlen = sizeof(optval);
225#if (_LWS_ENABLED_LOGS & LLL_WARN)
226	int en;
227#endif
228
229#if 0
230#if defined(TCP_FASTOPEN_CONNECT)
231	optval = 1;
232	if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval,
233		       sizeof(optval)))
234		lwsl_warn("%s: FASTOPEN_CONNECT failed\n", __func__);
235	optval = (int)pri;
236#endif
237#endif
238
239#if !defined(__APPLE__) && \
240      !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
241      !defined(__NetBSD__) && \
242      !defined(__OpenBSD__) && \
243      !defined(__sun) && \
244      !defined(__HAIKU__) && \
245      !defined(__CYGWIN__) && \
246      !defined(__QNX__)
247
248	/* the BSDs don't have SO_PRIORITY */
249
250	if (pri) { /* 0 is the default already */
251		if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY,
252				(const void *)&optval, optlen) < 0) {
253#if (_LWS_ENABLED_LOGS & LLL_WARN)
254			en = errno;
255			lwsl_warn("%s: unable to set socket pri %d: errno %d\n",
256				  __func__, (int)pri, en);
257#endif
258			ret = 1;
259		} else
260			lwsl_notice("%s: set pri %u\n", __func__, pri);
261	}
262#endif
263
264	for (n = 0; n < 4; n++) {
265		if (!(lws_flags & ip_opt_lws_flags[n]))
266			continue;
267
268		optval = (int)ip_opt_val[n];
269		if (setsockopt(fd, IPPROTO_IP, IP_TOS, (const void *)&optval,
270			       optlen) < 0) {
271#if !defined(LWS_WITH_NO_LOGS)
272			en = errno;
273			lwsl_warn("%s: unable to set %s: errno %d\n", __func__,
274				  ip_opt_names[n], en);
275#endif
276			ret = 1;
277		} else
278			lwsl_notice("%s: set ip flag %s\n", __func__,
279				    ip_opt_names[n]);
280	}
281
282	return ret;
283}
284
285/* cast a struct sockaddr_in6 * into addr for ipv6 */
286
287enum {
288	IP_SCORE_NONE,
289	IP_SCORE_NONNATIVE,
290	IP_SCORE_IPV6_SCOPE_BASE,
291	/* ipv6 scopes */
292	IP_SCORE_GLOBAL_NATIVE = 18
293};
294
295int
296lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
297		    size_t addrlen)
298{
299	int rc = LWS_ITOSA_NOT_EXIST;
300
301	struct ifaddrs *ifr;
302	struct ifaddrs *ifc;
303#if defined(LWS_WITH_IPV6)
304	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
305	unsigned long sco = IP_SCORE_NONE;
306	unsigned long ts;
307	const uint8_t *p;
308#endif
309
310	if (getifaddrs(&ifr)) {
311		lwsl_err("%s: unable to getifaddrs: errno %d\n", __func__, errno);
312
313		return LWS_ITOSA_USABLE;
314	}
315	for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
316		if (!ifc->ifa_addr || !ifc->ifa_name)
317			continue;
318
319		lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
320			   ifc->ifa_name, ifname,
321			   ifc->ifa_addr->sa_family, ipv6);
322
323		if (strcmp(ifc->ifa_name, ifname))
324			continue;
325
326		switch (ifc->ifa_addr->sa_family) {
327#if defined(AF_PACKET)
328		case AF_PACKET:
329			/* interface exists but is not usable */
330			if (rc == LWS_ITOSA_NOT_EXIST)
331				rc = LWS_ITOSA_NOT_USABLE;
332			continue;
333#endif
334
335		case AF_INET:
336#if defined(LWS_WITH_IPV6)
337			if (ipv6) {
338				/* any existing solution is better than this */
339				if (sco != IP_SCORE_NONE)
340					break;
341				sco = IP_SCORE_NONNATIVE;
342				rc = LWS_ITOSA_USABLE;
343				/* map IPv4 to IPv6 */
344				memset((char *)&addr6->sin6_addr, 0,
345						sizeof(struct in6_addr));
346				addr6->sin6_addr.s6_addr[10] = 0xff;
347				addr6->sin6_addr.s6_addr[11] = 0xff;
348				memcpy(&addr6->sin6_addr.s6_addr[12],
349				       &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
350							sizeof(struct in_addr));
351				lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
352				break;
353			}
354
355			sco = IP_SCORE_GLOBAL_NATIVE;
356#endif
357			rc = LWS_ITOSA_USABLE;
358			memcpy(addr, (struct sockaddr_in *)ifc->ifa_addr,
359						    sizeof(struct sockaddr_in));
360			break;
361#if defined(LWS_WITH_IPV6)
362		case AF_INET6:
363			p = (const uint8_t *)
364				&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr;
365			ts = IP_SCORE_IPV6_SCOPE_BASE;
366			if (p[0] == 0xff)
367				ts = (unsigned long)(IP_SCORE_IPV6_SCOPE_BASE + (p[1] & 0xf));
368
369			if (sco >= ts)
370				break;
371
372			sco = ts;
373			rc = LWS_ITOSA_USABLE;
374
375			memcpy(&addr6->sin6_addr,
376			     &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
377						       sizeof(struct in6_addr));
378			break;
379#endif
380		default:
381			break;
382		}
383	}
384
385	freeifaddrs(ifr);
386
387	if (rc &&
388	    !lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
389		rc = LWS_ITOSA_USABLE;
390
391	return rc;
392}
393
394
395const char *
396lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
397{
398	return inet_ntop(af, src, dst, cnt);
399}
400
401int
402lws_plat_inet_pton(int af, const char *src, void *dst)
403{
404	return inet_pton(af, src, dst);
405}
406
407int
408lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
409{
410#if defined(__linux__)
411	struct ifreq i;
412
413	memset(&i, 0, sizeof(i));
414	lws_strncpy(i.ifr_name, ifname, sizeof(i.ifr_name));
415
416	if (ioctl(fd, SIOCGIFHWADDR, &i) < 0)
417		return -1;
418
419	memcpy(hwaddr, &i.ifr_hwaddr.sa_data, 6);
420
421	return 6;
422#else
423	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
424
425	return -1;
426#endif
427}
428
429int
430lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
431			  size_t n, int fd, const char *iface)
432{
433#if defined(__linux__)
434	struct sockaddr_ll sll;
435	uint16_t *p16 = (uint16_t *)p;
436	uint32_t ucs = 0;
437
438	memcpy(p, canned, canned_len);
439
440	p[2] = (uint8_t)(n >> 8);
441	p[3] = (uint8_t)(n);
442
443	while (p16 < (uint16_t *)(p + 20))
444		ucs += ntohs(*p16++);
445
446	ucs += ucs >> 16;
447	ucs ^= 0xffff;
448
449	p[10] = (uint8_t)(ucs >> 8);
450	p[11] = (uint8_t)(ucs);
451	p[24] = (uint8_t)((n - 20) >> 8);
452	p[25] = (uint8_t)((n - 20));
453
454	memset(&sll, 0, sizeof(sll));
455	sll.sll_family = AF_PACKET;
456	sll.sll_protocol = htons(0x800);
457	sll.sll_halen = 6;
458	sll.sll_ifindex = (int)if_nametoindex(iface);
459	memset(sll.sll_addr, 0xff, 6);
460
461	return (int)sendto(fd, p, n, 0, (struct sockaddr *)&sll, sizeof(sll));
462#else
463	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
464
465	return -1;
466#endif
467}
468
469int
470lws_plat_if_up(const char *ifname, int fd, int up)
471{
472#if defined(__linux__)
473	struct ifreq ifr;
474
475	memset(&ifr, 0, sizeof(ifr));
476	lws_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
477
478	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
479		lwsl_err("%s: SIOCGIFFLAGS fail\n", __func__);
480		return 1;
481	}
482
483	if (up)
484		ifr.ifr_flags |= IFF_UP;
485	else
486		ifr.ifr_flags &= ~IFF_UP;
487
488	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
489		lwsl_err("%s: SIOCSIFFLAGS fail\n", __func__);
490		return 1;
491	}
492
493	return 0;
494#else
495	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
496
497	return -1;
498#endif
499}
500
501int
502lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
503{
504#if defined(__linux__)
505	struct ifreq i;
506
507	memset(&i, 0, sizeof(i));
508	i.ifr_addr.sa_family = AF_INET;
509	lws_strncpy(i.ifr_ifrn.ifrn_name, ifname,
510		    sizeof(i.ifr_ifrn.ifrn_name));
511	if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &i, sizeof(i)) < 0) {
512		lwsl_notice("%s: failed %d\n", __func__, LWS_ERRNO);
513		return 1;
514	}
515
516	return 0;
517#else
518	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
519
520	return -1;
521#endif
522}
523
524int
525lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
526{
527#if defined(__linux__)
528	struct rtentry route;
529	struct ifreq ifr;
530
531	memset(&ifr, 0, sizeof(ifr));
532	memset(&route, 0, sizeof(route));
533
534	lws_strncpy(ifr.ifr_name, is->ifname, IFNAMSIZ);
535
536	lws_plat_if_up(is->ifname, fd, 0);
537
538	memcpy(&ifr.ifr_addr, &is->sa46[LWSDH_SA46_IP], sizeof(struct sockaddr));
539	if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
540		lwsl_err("%s: SIOCSIFADDR fail\n", __func__);
541		return 1;
542	}
543
544	if (is->sa46[LWSDH_SA46_IP].sa4.sin_family == AF_INET) {
545		struct sockaddr_in sin;
546
547		memset(&sin, 0, sizeof(sin));
548		sin.sin_family = AF_INET;
549		sin.sin_addr.s_addr = *(uint32_t *)&is->nums[LWSDH_IPV4_SUBNET_MASK];
550		memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
551		if (ioctl(fd, SIOCSIFNETMASK, &ifr) < 0) {
552			lwsl_err("%s: SIOCSIFNETMASK fail\n", __func__);
553			return 1;
554		}
555
556		lws_plat_if_up(is->ifname, fd, 1);
557
558		memcpy(&route.rt_gateway,
559		       &is->sa46[LWSDH_SA46_IPV4_ROUTER].sa4,
560		       sizeof(struct sockaddr));
561
562		sin.sin_addr.s_addr = 0;
563		memcpy(&route.rt_dst, &sin, sizeof(struct sockaddr));
564		memcpy(&route.rt_genmask, &sin, sizeof(struct sockaddr));
565
566		route.rt_flags = RTF_UP | RTF_GATEWAY;
567		route.rt_metric = 100;
568		route.rt_dev = (char *)is->ifname;
569
570		if (ioctl(fd, SIOCADDRT, &route) < 0) {
571			lwsl_err("%s: SIOCADDRT 0x%x fail: %d\n", __func__,
572				(unsigned int)htonl(*(uint32_t *)&is->
573					sa46[LWSDH_SA46_IPV4_ROUTER].
574						sa4.sin_addr.s_addr), LWS_ERRNO);
575			return 1;
576		}
577	} else
578		lws_plat_if_up(is->ifname, fd, 1);
579
580	return 0;
581#else
582	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
583
584	return -1;
585#endif
586}
587
588int
589lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
590{
591	return 0;
592}
593
594#if defined(LWS_WITH_MBEDTLS)
595int
596lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
597{
598	int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
599	int ret;
600
601	if (fd < 0)
602		return MBEDTLS_ERR_NET_INVALID_CONTEXT;
603
604	ret = (int)write(fd, buf, len);
605	if (ret >= 0)
606		return ret;
607
608	if (errno == EAGAIN || errno == EWOULDBLOCK)
609		return MBEDTLS_ERR_SSL_WANT_WRITE;
610
611	if (errno == EPIPE || errno == ECONNRESET)
612		return MBEDTLS_ERR_NET_CONN_RESET;
613
614	if( errno == EINTR )
615		return MBEDTLS_ERR_SSL_WANT_WRITE;
616
617	return MBEDTLS_ERR_NET_SEND_FAILED;
618}
619
620int
621lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
622{
623	int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE_V30_ONLY(fd);
624	int ret;
625
626	if (fd < 0)
627		return MBEDTLS_ERR_NET_INVALID_CONTEXT;
628
629	ret = (int)read(fd, buf, len);
630	if (ret >= 0)
631		return ret;
632
633	if (errno == EAGAIN || errno == EWOULDBLOCK)
634		return MBEDTLS_ERR_SSL_WANT_READ;
635
636	if (errno == EPIPE || errno == ECONNRESET)
637		return MBEDTLS_ERR_NET_CONN_RESET;
638
639	if (errno == EINTR)
640		return MBEDTLS_ERR_SSL_WANT_READ;
641
642	return MBEDTLS_ERR_NET_RECV_FAILED;
643}
644#endif
645