OpenVPN
socket.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "syshead.h"
29
30#include "socket.h"
31#include "fdmisc.h"
32#include "misc.h"
33#include "gremlin.h"
34#include "plugin.h"
35#include "ps.h"
36#include "run_command.h"
37#include "manage.h"
38#include "misc.h"
39#include "manage.h"
40#include "openvpn.h"
41#include "forward.h"
42
43#include "memdbg.h"
44
45bool
47{
48 int i;
49
50 for (i = 0; i < c->c1.link_sockets_num; i++)
51 {
53 {
54 return true;
55 }
56 }
57 return false;
58}
59
60/*
61 * Convert sockflags/getaddr_flags into getaddr_flags
62 */
63static unsigned int
64sf2gaf(const unsigned int getaddr_flags,
65 const unsigned int sockflags)
66{
67 if (sockflags & SF_HOST_RANDOMIZE)
68 {
69 return getaddr_flags | GETADDR_RANDOMIZE;
70 }
71 else
72 {
73 return getaddr_flags;
74 }
75}
76
77/*
78 * Functions related to the translation of DNS names to IP addresses.
79 */
80static int
81get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname,
82 void *network, unsigned int *netbits,
83 int resolve_retry_seconds, struct signal_info *sig_info,
84 int msglevel)
85{
86 char *endp, *sep, *var_host = NULL;
87 struct addrinfo *ai = NULL;
88 unsigned long bits;
89 uint8_t max_bits;
90 int ret = -1;
91
92 if (!hostname)
93 {
94 msg(M_NONFATAL, "Can't resolve null hostname!");
95 goto out;
96 }
97
98 /* assign family specific default values */
99 switch (af)
100 {
101 case AF_INET:
102 bits = 0;
103 max_bits = sizeof(in_addr_t) * 8;
104 break;
105
106 case AF_INET6:
107 bits = 64;
108 max_bits = sizeof(struct in6_addr) * 8;
109 break;
110
111 default:
112 msg(M_WARN,
113 "Unsupported AF family passed to getaddrinfo for %s (%d)",
114 hostname, af);
115 goto out;
116 }
117
118 /* we need to modify the hostname received as input, but we don't want to
119 * touch it directly as it might be a constant string.
120 *
121 * Therefore, we clone the string here and free it at the end of the
122 * function */
123 var_host = strdup(hostname);
124 if (!var_host)
125 {
127 "Can't allocate hostname buffer for getaddrinfo");
128 goto out;
129 }
130
131 /* check if this hostname has a /bits suffix */
132 sep = strchr(var_host, '/');
133 if (sep)
134 {
135 bits = strtoul(sep + 1, &endp, 10);
136 if ((*endp != '\0') || (bits > max_bits))
137 {
138 msg(msglevel, "IP prefix '%s': invalid '/bits' spec (%s)", hostname,
139 sep + 1);
140 goto out;
141 }
142 *sep = '\0';
143 }
144
145 ret = openvpn_getaddrinfo(flags & ~GETADDR_HOST_ORDER, var_host, NULL,
146 resolve_retry_seconds, sig_info, af, &ai);
147 if ((ret == 0) && network)
148 {
149 struct in6_addr *ip6;
150 in_addr_t *ip4;
151
152 switch (af)
153 {
154 case AF_INET:
155 ip4 = network;
156 *ip4 = ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
157
158 if (flags & GETADDR_HOST_ORDER)
159 {
160 *ip4 = ntohl(*ip4);
161 }
162 break;
163
164 case AF_INET6:
165 ip6 = network;
166 *ip6 = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
167 break;
168
169 default:
170 /* can't get here because 'af' was previously checked */
171 msg(M_WARN,
172 "Unsupported AF family for %s (%d)", var_host, af);
173 goto out;
174 }
175 }
176
177 if (netbits)
178 {
179 *netbits = bits;
180 }
181
182 /* restore '/' separator, if any */
183 if (sep)
184 {
185 *sep = '/';
186 }
187out:
188 freeaddrinfo(ai);
189 free(var_host);
190
191 return ret;
192}
193
194in_addr_t
195getaddr(unsigned int flags,
196 const char *hostname,
197 int resolve_retry_seconds,
198 bool *succeeded,
199 struct signal_info *sig_info)
200{
201 in_addr_t addr;
202 int status;
203
204 status = get_addr_generic(AF_INET, flags, hostname, &addr, NULL,
205 resolve_retry_seconds, sig_info,
206 M_WARN);
207 if (status==0)
208 {
209 if (succeeded)
210 {
211 *succeeded = true;
212 }
213 return addr;
214 }
215 else
216 {
217 if (succeeded)
218 {
219 *succeeded = false;
220 }
221 return 0;
222 }
223}
224
225bool
226get_ipv6_addr(const char *hostname, struct in6_addr *network,
227 unsigned int *netbits, int msglevel)
228{
229 if (get_addr_generic(AF_INET6, GETADDR_RESOLVE, hostname, network, netbits,
230 0, NULL, msglevel) < 0)
231 {
232 return false;
233 }
234
235 return true; /* parsing OK, values set */
236}
237
238static inline bool
239streqnull(const char *a, const char *b)
240{
241 if (a == NULL && b == NULL)
242 {
243 return true;
244 }
245 else if (a == NULL || b == NULL)
246 {
247 return false;
248 }
249 else
250 {
251 return streq(a, b);
252 }
253}
254
255/*
256 * get_cached_dns_entry return 0 on success and -1
257 * otherwise. (like getaddrinfo)
258 */
259static int
261 const char *hostname,
262 const char *servname,
263 int ai_family,
264 int resolve_flags,
265 struct addrinfo **ai)
266{
267 struct cached_dns_entry *ph;
268 int flags;
269
270 /* Only use flags that are relevant for the structure */
271 flags = resolve_flags & GETADDR_CACHE_MASK;
272
273 for (ph = dns_cache; ph; ph = ph->next)
274 {
275 if (streqnull(ph->hostname, hostname)
277 && ph->ai_family == ai_family
278 && ph->flags == flags)
279 {
280 *ai = ph->ai;
281 return 0;
282 }
283 }
284 return -1;
285}
286
287
288static int
290 const char *hostname,
291 const char *servname,
292 const int af,
293 const int flags)
294{
295 struct addrinfo *ai;
296 int status;
297
299 hostname,
300 servname,
301 af,
302 flags,
303 &ai) == 0)
304 {
305 /* entry already cached, return success */
306 return 0;
307 }
308
309 status = openvpn_getaddrinfo(flags, hostname, servname,
311 af, &ai);
312 if (status == 0)
313 {
314 struct cached_dns_entry *ph;
315
316 ALLOC_OBJ_CLEAR_GC(ph, struct cached_dns_entry, &c->gc);
317 ph->ai = ai;
318 ph->hostname = hostname;
319 ph->servname = servname;
321
322 if (!c->c1.dns_cache)
323 {
324 c->c1.dns_cache = ph;
325 }
326 else
327 {
328 struct cached_dns_entry *prev = c->c1.dns_cache;
329 while (prev->next)
330 {
331 prev = prev->next;
332 }
333 prev->next = ph;
334 }
335
337
338 }
339 return status;
340}
341
342void
344{
345 struct connection_list *l = c->options.connection_list;
346 const unsigned int preresolve_flags = GETADDR_RESOLVE
350
351
352 for (int i = 0; i < l->len; ++i)
353 {
354 int status;
355 const char *remote;
356 int flags = preresolve_flags;
357
358 struct connection_entry *ce = l->array[i];
359
360 if (proto_is_dgram(ce->proto))
361 {
363 }
364
366 {
368 }
369
370 if (c->options.ip_remote_hint)
371 {
373 }
374 else
375 {
376 remote = ce->remote;
377 }
378
379 /* HTTP remote hostname does not need to be resolved */
380 if (!ce->http_proxy_options)
381 {
383 ce->af, flags);
384 if (status != 0)
385 {
386 goto err;
387 }
388 }
389
390 /* Preresolve proxy */
391 if (ce->http_proxy_options)
392 {
396 ce->af,
397 preresolve_flags);
398
399 if (status != 0)
400 {
401 goto err;
402 }
403 }
404
405 if (ce->socks_proxy_server)
406 {
410 ce->af,
411 flags);
412 if (status != 0)
413 {
414 goto err;
415 }
416 }
417
418 if (ce->bind_local)
419 {
421 flags &= ~GETADDR_RANDOMIZE;
422
423 for (int j = 0; j < ce->local_list->len; j++)
424 {
425 struct local_entry *le = ce->local_list->array[j];
426
427 if (!le->local)
428 {
429 continue;
430 }
431
432 status = do_preresolve_host(c, le->local, le->port, ce->af, flags);
433 if (status != 0)
434 {
435 goto err;
436 }
437
438 }
439 }
440
441 }
442 return;
443
444err:
445 throw_signal_soft(SIGHUP, "Preresolving failed");
446}
447
448/*
449 * Translate IPv4/IPv6 addr or hostname into struct addrinfo
450 * If resolve error, try again for resolve_retry_seconds seconds.
451 */
452int
453openvpn_getaddrinfo(unsigned int flags,
454 const char *hostname,
455 const char *servname,
456 int resolve_retry_seconds,
457 struct signal_info *sig_info,
458 int ai_family,
459 struct addrinfo **res)
460{
461 struct addrinfo hints;
462 int status;
463 struct signal_info sigrec = {0};
464 int msglevel = (flags & GETADDR_FATAL) ? M_FATAL : D_RESOLVE_ERRORS;
465 struct gc_arena gc = gc_new();
466 const char *print_hostname;
467 const char *print_servname;
468
469 ASSERT(res);
470
471 ASSERT(hostname || servname);
472 ASSERT(!(flags & GETADDR_HOST_ORDER));
473
474 if (servname)
475 {
476 print_servname = servname;
477 }
478 else
479 {
480 print_servname = "";
481 }
482
483 if (flags & GETADDR_MSG_VIRT_OUT)
484 {
485 msglevel |= M_MSG_VIRT_OUT;
486 }
487
489 && !sig_info)
490 {
491 sig_info = &sigrec;
492 }
493
494 /* try numeric ip addr first */
495 CLEAR(hints);
496 hints.ai_flags = AI_NUMERICHOST;
497
498 if (flags & GETADDR_PASSIVE)
499 {
500 hints.ai_flags |= AI_PASSIVE;
501 }
502
503 if (flags & GETADDR_DATAGRAM)
504 {
505 hints.ai_socktype = SOCK_DGRAM;
506 }
507 else
508 {
509 hints.ai_socktype = SOCK_STREAM;
510 }
511
512 /* if hostname is not set, we want to bind to 'ANY', with
513 * the correct address family - v4-only or v6/v6-dual-stack */
514 if (!hostname)
515 {
516 hints.ai_family = ai_family;
517 }
518
519 status = getaddrinfo(hostname, servname, &hints, res);
520
521 if (status != 0) /* parse as numeric address failed? */
522 {
523 const int fail_wait_interval = 5; /* seconds */
524 /* Add +4 to cause integer division rounding up (1 + 4) = 5, (0+4)/5=0 */
525 int resolve_retries = (flags & GETADDR_TRY_ONCE) ? 1 :
526 ((resolve_retry_seconds + 4)/ fail_wait_interval);
527 const char *fmt;
528 int level = 0;
529
530 /* this is not a numeric IP, therefore force resolution using the
531 * provided ai_family */
532 hints.ai_family = ai_family;
533
534 if (hostname && (flags & GETADDR_RANDOMIZE))
535 {
536 hostname = hostname_randomize(hostname, &gc);
537 }
538
539 if (hostname)
540 {
541 print_hostname = hostname;
542 }
543 else
544 {
545 print_hostname = "undefined";
546 }
547
548 fmt = "RESOLVE: Cannot resolve host address: %s:%s (%s)";
550 && !resolve_retry_seconds)
551 {
552 fmt = "RESOLVE: Cannot resolve host address: %s:%s (%s) "
553 "(I would have retried this name query if you had "
554 "specified the --resolv-retry option.)";
555 }
556
557 if (!(flags & GETADDR_RESOLVE) || status == EAI_FAIL)
558 {
559 msg(msglevel, "RESOLVE: Cannot parse IP address: %s:%s (%s)",
560 print_hostname, print_servname, gai_strerror(status));
561 goto done;
562 }
563
564#ifdef ENABLE_MANAGEMENT
566 {
567 if (management)
568 {
571 NULL,
572 NULL,
573 NULL,
574 NULL,
575 NULL);
576 }
577 }
578#endif
579
580 /*
581 * Resolve hostname
582 */
583 while (true)
584 {
585#ifndef _WIN32
586 /* force resolv.conf reload */
587 res_init();
588#endif
589 /* try hostname lookup */
590 hints.ai_flags &= ~AI_NUMERICHOST;
592 "GETADDRINFO flags=0x%04x ai_family=%d ai_socktype=%d",
593 flags, hints.ai_family, hints.ai_socktype);
594 status = getaddrinfo(hostname, servname, &hints, res);
595
596 if (sig_info)
597 {
598 get_signal(&sig_info->signal_received);
599 if (sig_info->signal_received) /* were we interrupted by a signal? */
600 {
601 /* why are we overwriting SIGUSR1 ? */
602 if (signal_reset(sig_info, SIGUSR1) == SIGUSR1) /* ignore SIGUSR1 */
603 {
604 msg(level,
605 "RESOLVE: Ignored SIGUSR1 signal received during "
606 "DNS resolution attempt");
607 }
608 else
609 {
610 /* turn success into failure (interrupted syscall) */
611 if (0 == status)
612 {
613 ASSERT(res);
614 freeaddrinfo(*res);
615 *res = NULL;
616 status = EAI_AGAIN; /* = temporary failure */
617 errno = EINTR;
618 }
619 goto done;
620 }
621 }
622 }
623
624 /* success? */
625 if (0 == status)
626 {
627 break;
628 }
629
630 /* resolve lookup failed, should we
631 * continue or fail? */
632 level = msglevel;
633 if (resolve_retries > 0)
634 {
635 level = D_RESOLVE_ERRORS;
636 }
637
638 msg(level,
639 fmt,
640 print_hostname,
641 print_servname,
642 gai_strerror(status));
643
644 if (--resolve_retries <= 0)
645 {
646 goto done;
647 }
648
649 management_sleep(fail_wait_interval);
650 }
651
652 ASSERT(res);
653
654 /* hostname resolve succeeded */
655
656 /*
657 * Do not choose an IP Addresse by random or change the order *
658 * of IP addresses, doing so will break RFC 3484 address selection *
659 */
660 }
661 else
662 {
663 /* IP address parse succeeded */
664 if (flags & GETADDR_RANDOMIZE)
665 {
666 msg(M_WARN,
667 "WARNING: ignoring --remote-random-hostname because the "
668 "hostname is an IP address");
669 }
670 }
671
672done:
673 if (sig_info && sig_info->signal_received)
674 {
675 int level = 0;
676 if (flags & GETADDR_FATAL_ON_SIGNAL)
677 {
678 level = M_FATAL;
679 }
680 else if (flags & GETADDR_WARN_ON_SIGNAL)
681 {
682 level = M_WARN;
683 }
684 msg(level, "RESOLVE: signal received during DNS resolution attempt");
685 }
686
687 gc_free(&gc);
688 return status;
689}
690
691/*
692 * We do our own inet_aton because the glibc function
693 * isn't very good about error checking.
694 */
695int
696openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr)
697{
698 unsigned int a, b, c, d;
699
700 CLEAR(*addr);
701 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) == 4)
702 {
703 if (a < 256 && b < 256 && c < 256 && d < 256)
704 {
705 addr->s_addr = htonl(a<<24 | b<<16 | c<<8 | d);
706 return OIA_IP; /* good dotted quad */
707 }
708 }
709 if (string_class(dotted_quad, CC_DIGIT|CC_DOT, 0))
710 {
711 return OIA_ERROR; /* probably a badly formatted dotted quad */
712 }
713 else
714 {
715 return OIA_HOSTNAME; /* probably a hostname */
716 }
717}
718
719bool
720ip_addr_dotted_quad_safe(const char *dotted_quad)
721{
722 /* verify non-NULL */
723 if (!dotted_quad)
724 {
725 return false;
726 }
727
728 /* verify length is within limits */
729 if (strlen(dotted_quad) > 15)
730 {
731 return false;
732 }
733
734 /* verify that all chars are either numeric or '.' and that no numeric
735 * substring is greater than 3 chars */
736 {
737 int nnum = 0;
738 const char *p = dotted_quad;
739 int c;
740
741 while ((c = *p++))
742 {
743 if (c >= '0' && c <= '9')
744 {
745 ++nnum;
746 if (nnum > 3)
747 {
748 return false;
749 }
750 }
751 else if (c == '.')
752 {
753 nnum = 0;
754 }
755 else
756 {
757 return false;
758 }
759 }
760 }
761
762 /* verify that string will convert to IP address */
763 {
764 struct in_addr a;
765 return openvpn_inet_aton(dotted_quad, &a) == OIA_IP;
766 }
767}
768
769bool
770ipv6_addr_safe(const char *ipv6_text_addr)
771{
772 /* verify non-NULL */
773 if (!ipv6_text_addr)
774 {
775 return false;
776 }
777
778 /* verify length is within limits */
779 if (strlen(ipv6_text_addr) > INET6_ADDRSTRLEN)
780 {
781 return false;
782 }
783
784 /* verify that string will convert to IPv6 address */
785 {
786 struct in6_addr a6;
787 return inet_pton( AF_INET6, ipv6_text_addr, &a6 ) == 1;
788 }
789}
790
791static bool
792dns_addr_safe(const char *addr)
793{
794 if (addr)
795 {
796 const size_t len = strlen(addr);
797 return len > 0 && len <= 255 && string_class(addr, CC_ALNUM|CC_DASH|CC_DOT, 0);
798 }
799 else
800 {
801 return false;
802 }
803}
804
805bool
806ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn)
807{
808 if (ip_addr_dotted_quad_safe(addr))
809 {
810 return true;
811 }
812 else if (allow_fqdn)
813 {
814 return dns_addr_safe(addr);
815 }
816 else
817 {
818 return false;
819 }
820}
821
822bool
823mac_addr_safe(const char *mac_addr)
824{
825 /* verify non-NULL */
826 if (!mac_addr)
827 {
828 return false;
829 }
830
831 /* verify length is within limits */
832 if (strlen(mac_addr) > 17)
833 {
834 return false;
835 }
836
837 /* verify that all chars are either alphanumeric or ':' and that no
838 * alphanumeric substring is greater than 2 chars */
839 {
840 int nnum = 0;
841 const char *p = mac_addr;
842 int c;
843
844 while ((c = *p++))
845 {
846 if ( (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') )
847 {
848 ++nnum;
849 if (nnum > 2)
850 {
851 return false;
852 }
853 }
854 else if (c == ':')
855 {
856 nnum = 0;
857 }
858 else
859 {
860 return false;
861 }
862 }
863 }
864
865 /* error-checking is left to script invoked in lladdr.c */
866 return true;
867}
868
869static int
871{
872#if defined(SOL_SOCKET) && defined(SO_SNDBUF)
873 int val;
874 socklen_t len;
875
876 len = sizeof(val);
877 if (getsockopt(sd, SOL_SOCKET, SO_SNDBUF, (void *) &val, &len) == 0
878 && len == sizeof(val))
879 {
880 return val;
881 }
882#endif
883 return 0;
884}
885
886static void
888{
889#if defined(SOL_SOCKET) && defined(SO_SNDBUF)
890 if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (void *) &size, sizeof(size)) != 0)
891 {
892 msg(M_WARN, "NOTE: setsockopt SO_SNDBUF=%d failed", size);
893 }
894#endif
895}
896
897static int
899{
900#if defined(SOL_SOCKET) && defined(SO_RCVBUF)
901 int val;
902 socklen_t len;
903
904 len = sizeof(val);
905 if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, (void *) &val, &len) == 0
906 && len == sizeof(val))
907 {
908 return val;
909 }
910#endif
911 return 0;
912}
913
914static bool
916{
917#if defined(SOL_SOCKET) && defined(SO_RCVBUF)
918 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (void *) &size, sizeof(size)) != 0)
919 {
920 msg(M_WARN, "NOTE: setsockopt SO_RCVBUF=%d failed", size);
921 return false;
922 }
923 return true;
924#endif
925}
926
927void
929 bool reduce_size)
930{
931 if (sbs)
932 {
933 const int sndbuf_old = socket_get_sndbuf(fd);
934 const int rcvbuf_old = socket_get_rcvbuf(fd);
935
936 if (sbs->sndbuf
937 && (reduce_size || sndbuf_old < sbs->sndbuf))
938 {
939 socket_set_sndbuf(fd, sbs->sndbuf);
940 }
941
942 if (sbs->rcvbuf
943 && (reduce_size || rcvbuf_old < sbs->rcvbuf))
944 {
945 socket_set_rcvbuf(fd, sbs->rcvbuf);
946 }
947
948 msg(D_OSBUF, "Socket Buffers: R=[%d->%d] S=[%d->%d]",
949 rcvbuf_old,
951 sndbuf_old,
953 }
954}
955
956/*
957 * Set other socket options
958 */
959
960static bool
962{
963#if defined(_WIN32) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
964 if (setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (void *) &state, sizeof(state)) != 0)
965 {
966 msg(M_WARN, "NOTE: setsockopt TCP_NODELAY=%d failed", state);
967 return false;
968 }
969 else
970 {
971 dmsg(D_OSBUF, "Socket flags: TCP_NODELAY=%d succeeded", state);
972 return true;
973 }
974#else /* if defined(_WIN32) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY)) */
975 msg(M_WARN, "NOTE: setsockopt TCP_NODELAY=%d failed (No kernel support)", state);
976 return false;
977#endif
978}
979
980static inline void
982{
983#if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
984 if (mark && setsockopt(sd, SOL_SOCKET, SO_MARK, (void *) &mark, sizeof(mark)) != 0)
985 {
986 msg(M_WARN, "NOTE: setsockopt SO_MARK=%d failed", mark);
987 }
988#endif
989}
990
991static bool
992socket_set_flags(socket_descriptor_t sd, unsigned int sockflags)
993{
994 /* SF_TCP_NODELAY doesn't make sense for dco-win */
995 if ((sockflags & SF_TCP_NODELAY) && (!(sockflags & SF_DCO_WIN)))
996 {
997 return socket_set_tcp_nodelay(sd, 1);
998 }
999 else
1000 {
1001 return true;
1002 }
1003}
1004
1005bool
1006link_socket_update_flags(struct link_socket *sock, unsigned int sockflags)
1007{
1008 if (sock && socket_defined(sock->sd))
1009 {
1010 sock->sockflags |= sockflags;
1011 return socket_set_flags(sock->sd, sock->sockflags);
1012 }
1013 else
1014 {
1015 return false;
1016 }
1017}
1018
1019void
1020link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
1021{
1022 if (sock && socket_defined(sock->sd))
1023 {
1024 sock->socket_buffer_sizes.sndbuf = sndbuf;
1025 sock->socket_buffer_sizes.rcvbuf = rcvbuf;
1026 socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);
1027 }
1028}
1029
1030/*
1031 * SOCKET INITIALIZATION CODE.
1032 * Create a TCP/UDP socket
1033 */
1034
1036create_socket_tcp(struct addrinfo *addrinfo)
1037{
1039
1040 ASSERT(addrinfo);
1041 ASSERT(addrinfo->ai_socktype == SOCK_STREAM);
1042
1043 if ((sd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol)) < 0)
1044 {
1045 msg(M_ERR, "Cannot create TCP socket");
1046 }
1047
1048#ifndef _WIN32 /* using SO_REUSEADDR on Windows will cause bind to succeed on port conflicts! */
1049 /* set SO_REUSEADDR on socket */
1050 {
1051 int on = 1;
1052 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
1053 (void *) &on, sizeof(on)) < 0)
1054 {
1055 msg(M_ERR, "TCP: Cannot setsockopt SO_REUSEADDR on TCP socket");
1056 }
1057 }
1058#endif
1059
1060 /* set socket file descriptor to not pass across execs, so that
1061 * scripts don't have access to it */
1062 set_cloexec(sd);
1063
1064 return sd;
1065}
1066
1068create_socket_udp(struct addrinfo *addrinfo, const unsigned int flags)
1069{
1071
1072 ASSERT(addrinfo);
1073 ASSERT(addrinfo->ai_socktype == SOCK_DGRAM);
1074
1075 if ((sd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol)) < 0)
1076 {
1077 msg(M_ERR, "UDP: Cannot create UDP/UDP6 socket");
1078 }
1079#if ENABLE_IP_PKTINFO
1080 else if (flags & SF_USE_IP_PKTINFO)
1081 {
1082 int pad = 1;
1083 if (addrinfo->ai_family == AF_INET)
1084 {
1085#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
1086 if (setsockopt(sd, SOL_IP, IP_PKTINFO,
1087 (void *)&pad, sizeof(pad)) < 0)
1088 {
1089 msg(M_ERR, "UDP: failed setsockopt for IP_PKTINFO");
1090 }
1091#elif defined(IP_RECVDSTADDR)
1092 if (setsockopt(sd, IPPROTO_IP, IP_RECVDSTADDR,
1093 (void *)&pad, sizeof(pad)) < 0)
1094 {
1095 msg(M_ERR, "UDP: failed setsockopt for IP_RECVDSTADDR");
1096 }
1097#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
1098#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
1099#endif
1100 }
1101 else if (addrinfo->ai_family == AF_INET6)
1102 {
1103#ifndef IPV6_RECVPKTINFO /* Some older Darwin platforms require this */
1104 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PKTINFO,
1105 (void *)&pad, sizeof(pad)) < 0)
1106#else
1107 if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1108 (void *)&pad, sizeof(pad)) < 0)
1109#endif
1110 { msg(M_ERR, "UDP: failed setsockopt for IPV6_RECVPKTINFO");}
1111 }
1112 }
1113#endif /* if ENABLE_IP_PKTINFO */
1114
1115 /* set socket file descriptor to not pass across execs, so that
1116 * scripts don't have access to it */
1117 set_cloexec(sd);
1118
1119 return sd;
1120}
1121
1122static void
1123bind_local(struct link_socket *sock, const sa_family_t ai_family)
1124{
1125 /* bind to local address/port */
1126 if (sock->bind_local)
1127 {
1128 if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
1129 {
1130 socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local,
1131 ai_family, "SOCKS", false);
1132 }
1133 else
1134 {
1135 socket_bind(sock->sd, sock->info.lsa->bind_local,
1136 ai_family,
1137 "TCP/UDP", sock->info.bind_ipv6_only);
1138 }
1139 }
1140}
1141
1142static void
1143create_socket(struct link_socket *sock, struct addrinfo *addr)
1144{
1145 if (addr->ai_protocol == IPPROTO_UDP || addr->ai_socktype == SOCK_DGRAM)
1146 {
1147 sock->sd = create_socket_udp(addr, sock->sockflags);
1149
1150 /* Assume that control socket and data socket to the socks proxy
1151 * are using the same IP family */
1152 if (sock->socks_proxy)
1153 {
1154 /* Construct a temporary addrinfo to create the socket,
1155 * currently resolve two remote addresses is not supported,
1156 * TODO: Rewrite the whole resolve_remote */
1157 struct addrinfo addrinfo_tmp = *addr;
1158 addrinfo_tmp.ai_socktype = SOCK_STREAM;
1159 addrinfo_tmp.ai_protocol = IPPROTO_TCP;
1160 sock->ctrl_sd = create_socket_tcp(&addrinfo_tmp);
1161 }
1162 }
1163 else if (addr->ai_protocol == IPPROTO_TCP || addr->ai_socktype == SOCK_STREAM)
1164 {
1165 sock->sd = create_socket_tcp(addr);
1166 }
1167 else
1168 {
1169 ASSERT(0);
1170 }
1171 /* Set af field of sock->info, so it always reflects the address family
1172 * of the created socket */
1173 sock->info.af = addr->ai_family;
1174
1175 /* set socket buffers based on --sndbuf and --rcvbuf options */
1176 socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);
1177
1178 /* set socket to --mark packets with given value */
1179 socket_set_mark(sock->sd, sock->mark);
1180
1181#if defined(TARGET_LINUX)
1182 if (sock->bind_dev)
1183 {
1184 msg(M_INFO, "Using bind-dev %s", sock->bind_dev);
1185 if (setsockopt(sock->sd, SOL_SOCKET, SO_BINDTODEVICE, sock->bind_dev, strlen(sock->bind_dev) + 1) != 0)
1186 {
1187 msg(M_WARN|M_ERRNO, "WARN: setsockopt SO_BINDTODEVICE=%s failed", sock->bind_dev);
1188 }
1189
1190 }
1191#endif
1192
1193 bind_local(sock, addr->ai_family);
1194}
1195
1196#ifdef TARGET_ANDROID
1197static void
1198protect_fd_nonlocal(int fd, const struct sockaddr *addr)
1199{
1200 if (!management)
1201 {
1202 msg(M_FATAL, "Required management interface not available.");
1203 }
1204
1205 /* pass socket FD to management interface to pass on to VPNService API
1206 * as "protected socket" (exempt from being routed into tunnel)
1207 */
1208 if (addr_local(addr))
1209 {
1210 msg(D_SOCKET_DEBUG, "Address is local, not protecting socket fd %d", fd);
1211 return;
1212 }
1213
1214 msg(D_SOCKET_DEBUG, "Protecting socket fd %d", fd);
1215 management->connection.fdtosend = fd;
1216 management_android_control(management, "PROTECTFD", __func__);
1217}
1218#endif
1219
1220/*
1221 * Functions used for establishing a TCP stream connection.
1222 */
1223static void
1225 const struct addrinfo *local,
1226 bool do_listen,
1227 bool do_set_nonblock)
1228{
1229 struct gc_arena gc = gc_new();
1230 if (do_listen)
1231 {
1232 ASSERT(local);
1233 msg(M_INFO, "Listening for incoming TCP connection on %s",
1234 print_sockaddr(local->ai_addr, &gc));
1235 if (listen(sd, 32))
1236 {
1237 msg(M_ERR, "TCP: listen() failed");
1238 }
1239 }
1240
1241 /* set socket to non-blocking mode */
1242 if (do_set_nonblock)
1243 {
1244 set_nonblock(sd);
1245 }
1246
1247 gc_free(&gc);
1248}
1249
1252 struct link_socket_actual *act,
1253 const bool nowait)
1254{
1255 /* af_addr_size WILL return 0 in this case if AFs other than AF_INET
1256 * are compiled because act is empty here.
1257 * could use getsockname() to support later remote_len check
1258 */
1259 socklen_t remote_len_af = af_addr_size(act->dest.addr.sa.sa_family);
1260 socklen_t remote_len = sizeof(act->dest.addr);
1262
1263 CLEAR(*act);
1264
1265 if (nowait)
1266 {
1267 new_sd = getpeername(sd, &act->dest.addr.sa, &remote_len);
1268
1269 if (!socket_defined(new_sd))
1270 {
1271 msg(D_LINK_ERRORS | M_ERRNO, "TCP: getpeername() failed");
1272 }
1273 else
1274 {
1275 new_sd = sd;
1276 }
1277 }
1278 else
1279 {
1280 new_sd = accept(sd, &act->dest.addr.sa, &remote_len);
1281 }
1282
1283#if 0 /* For debugging only, test the effect of accept() failures */
1284 {
1285 static int foo = 0;
1286 ++foo;
1287 if (foo & 1)
1288 {
1289 new_sd = -1;
1290 }
1291 }
1292#endif
1293
1294 if (!socket_defined(new_sd))
1295 {
1296 msg(D_LINK_ERRORS | M_ERRNO, "TCP: accept(%d) failed", (int)sd);
1297 }
1298 /* only valid if we have remote_len_af!=0 */
1299 else if (remote_len_af && remote_len != remote_len_af)
1300 {
1301 msg(D_LINK_ERRORS, "TCP: Received strange incoming connection with unknown address length=%d", remote_len);
1302 openvpn_close_socket(new_sd);
1303 new_sd = SOCKET_UNDEFINED;
1304 }
1305 else
1306 {
1307 /* set socket file descriptor to not pass across execs, so that
1308 * scripts don't have access to it */
1309 set_cloexec(sd);
1310 }
1311 return new_sd;
1312}
1313
1314static void
1316{
1317 struct gc_arena gc = gc_new();
1318 msg(M_INFO, "TCP connection established with %s",
1320 gc_free(&gc);
1321}
1322
1325 struct link_socket_actual *act,
1326 const char *remote_dynamic,
1327 const struct addrinfo *local,
1328 bool do_listen,
1329 bool nowait,
1330 volatile int *signal_received)
1331{
1332 struct gc_arena gc = gc_new();
1333 /* struct openvpn_sockaddr *remote = &act->dest; */
1334 struct openvpn_sockaddr remote_verify = act->dest;
1336
1337 CLEAR(*act);
1338 socket_do_listen(sd, local, do_listen, true);
1339
1340 while (true)
1341 {
1342 int status;
1343 fd_set reads;
1344 struct timeval tv;
1345
1346 FD_ZERO(&reads);
1347 openvpn_fd_set(sd, &reads);
1348 tv.tv_sec = 0;
1349 tv.tv_usec = 0;
1350
1351 status = select(sd + 1, &reads, NULL, NULL, &tv);
1352
1353 get_signal(signal_received);
1354 if (*signal_received)
1355 {
1356 gc_free(&gc);
1357 return sd;
1358 }
1359
1360 if (status < 0)
1361 {
1362 msg(D_LINK_ERRORS | M_ERRNO, "TCP: select() failed");
1363 }
1364
1365 if (status <= 0)
1366 {
1368 continue;
1369 }
1370
1371 new_sd = socket_do_accept(sd, act, nowait);
1372
1373 if (socket_defined(new_sd))
1374 {
1375 struct addrinfo *ai = NULL;
1376 if (remote_dynamic)
1377 {
1378 openvpn_getaddrinfo(0, remote_dynamic, NULL, 1, NULL,
1379 remote_verify.addr.sa.sa_family, &ai);
1380 }
1381
1382 if (ai && !addrlist_match(&remote_verify, ai))
1383 {
1384 msg(M_WARN,
1385 "TCP NOTE: Rejected connection attempt from %s due to --remote setting",
1387 if (openvpn_close_socket(new_sd))
1388 {
1389 msg(M_ERR, "TCP: close socket failed (new_sd)");
1390 }
1391 freeaddrinfo(ai);
1392 }
1393 else
1394 {
1395 if (ai)
1396 {
1397 freeaddrinfo(ai);
1398 }
1399 break;
1400 }
1401 }
1403 }
1404
1405 if (!nowait && openvpn_close_socket(sd))
1406 {
1407 msg(M_ERR, "TCP: close socket failed (sd)");
1408 }
1409
1411
1412 gc_free(&gc);
1413 return new_sd;
1414}
1415
1416void
1418 struct addrinfo *local,
1419 int ai_family,
1420 const char *prefix,
1421 bool ipv6only)
1422{
1423 struct gc_arena gc = gc_new();
1424
1425 /* FIXME (schwabe)
1426 * getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
1427 * entries for the requested protocol.
1428 * For example if an address has multiple A records
1429 * What is the correct way to deal with it?
1430 */
1431
1432 struct addrinfo *cur;
1433
1434 ASSERT(local);
1435
1436
1437 /* find the first addrinfo with correct ai_family */
1438 for (cur = local; cur; cur = cur->ai_next)
1439 {
1440 if (cur->ai_family == ai_family)
1441 {
1442 break;
1443 }
1444 }
1445 if (!cur)
1446 {
1447 msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record",
1448 prefix, addr_family_name(ai_family));
1449 }
1450
1451 if (ai_family == AF_INET6)
1452 {
1453 int v6only = ipv6only ? 1 : 0; /* setsockopt must have an "int" */
1454
1455 msg(M_INFO, "setsockopt(IPV6_V6ONLY=%d)", v6only);
1456 if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) &v6only, sizeof(v6only)))
1457 {
1458 msg(M_NONFATAL|M_ERRNO, "Setting IPV6_V6ONLY=%d failed", v6only);
1459 }
1460 }
1461 if (bind(sd, cur->ai_addr, cur->ai_addrlen))
1462 {
1463 msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s",
1464 prefix,
1465 print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
1466 }
1467 gc_free(&gc);
1468}
1469
1470int
1472 const struct sockaddr *remote,
1473 int connect_timeout,
1474 volatile int *signal_received)
1475{
1476 int status = 0;
1477
1478#ifdef TARGET_ANDROID
1479 protect_fd_nonlocal(sd, remote);
1480#endif
1481 set_nonblock(sd);
1482 status = connect(sd, remote, af_addr_size(remote->sa_family));
1483 if (status)
1484 {
1486 }
1487 if (
1488#ifdef _WIN32
1489 status == WSAEWOULDBLOCK
1490#else
1491 status == EINPROGRESS
1492#endif
1493 )
1494 {
1495 while (true)
1496 {
1497#if POLL
1498 struct pollfd fds[1];
1499 fds[0].fd = sd;
1500 fds[0].events = POLLOUT;
1501 status = poll(fds, 1, (connect_timeout > 0) ? 1000 : 0);
1502#else
1503 fd_set writes;
1504 struct timeval tv;
1505
1506 FD_ZERO(&writes);
1507 openvpn_fd_set(sd, &writes);
1508 tv.tv_sec = (connect_timeout > 0) ? 1 : 0;
1509 tv.tv_usec = 0;
1510
1511 status = select(sd + 1, NULL, &writes, NULL, &tv);
1512#endif
1513 if (signal_received)
1514 {
1515 get_signal(signal_received);
1516 if (*signal_received)
1517 {
1518 status = 0;
1519 break;
1520 }
1521 }
1522 if (status < 0)
1523 {
1525 break;
1526 }
1527 if (status <= 0)
1528 {
1529 if (--connect_timeout < 0)
1530 {
1531#ifdef _WIN32
1532 status = WSAETIMEDOUT;
1533#else
1534 status = ETIMEDOUT;
1535#endif
1536 break;
1537 }
1539 continue;
1540 }
1541
1542 /* got it */
1543 {
1544 int val = 0;
1545 socklen_t len;
1546
1547 len = sizeof(val);
1548 if (getsockopt(sd, SOL_SOCKET, SO_ERROR, (void *) &val, &len) == 0
1549 && len == sizeof(val))
1550 {
1551 status = val;
1552 }
1553 else
1554 {
1556 }
1557 break;
1558 }
1559 }
1560 }
1561
1562 return status;
1563}
1564
1565void
1566set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
1567{
1568 CLEAR(*actual);
1569 ASSERT(ai);
1570
1571 if (ai->ai_family == AF_INET)
1572 {
1573 actual->dest.addr.in4 =
1574 *((struct sockaddr_in *) ai->ai_addr);
1575 }
1576 else if (ai->ai_family == AF_INET6)
1577 {
1578 actual->dest.addr.in6 =
1579 *((struct sockaddr_in6 *) ai->ai_addr);
1580 }
1581 else
1582 {
1583 ASSERT(0);
1584 }
1585
1586}
1587
1588static void
1590 const struct sockaddr *dest,
1591 const int connect_timeout,
1592 struct signal_info *sig_info)
1593{
1594 struct gc_arena gc = gc_new();
1595 int status;
1596
1597 msg(M_INFO, "Attempting to establish TCP connection with %s",
1598 print_sockaddr(dest, &gc));
1599
1600#ifdef ENABLE_MANAGEMENT
1601 if (management)
1602 {
1605 NULL,
1606 NULL,
1607 NULL,
1608 NULL,
1609 NULL);
1610 }
1611#endif
1612
1613 /* Set the actual address */
1614 status = openvpn_connect(*sd, dest, connect_timeout, &sig_info->signal_received);
1615
1616 get_signal(&sig_info->signal_received);
1617 if (sig_info->signal_received)
1618 {
1619 goto done;
1620 }
1621
1622 if (status)
1623 {
1624
1625 msg(D_LINK_ERRORS, "TCP: connect to %s failed: %s",
1626 print_sockaddr(dest, &gc), strerror(status));
1627
1629 *sd = SOCKET_UNDEFINED;
1630 register_signal(sig_info, SIGUSR1, "connection-failed");
1631 }
1632 else
1633 {
1634 msg(M_INFO, "TCP connection established with %s",
1635 print_sockaddr(dest, &gc));
1636 }
1637
1638done:
1639 gc_free(&gc);
1640}
1641
1642/*
1643 * Stream buffer handling prototypes -- stream_buf is a helper class
1644 * to assist in the packetization of stream transport protocols
1645 * such as TCP.
1646 */
1647
1648static void
1649stream_buf_init(struct stream_buf *sb, struct buffer *buf,
1650 const unsigned int sockflags, const int proto);
1651
1652static void
1653stream_buf_close(struct stream_buf *sb);
1654
1655static bool
1656stream_buf_added(struct stream_buf *sb, int length_added);
1657
1658/* For stream protocols, allocate a buffer to build up packet.
1659 * Called after frame has been finalized. */
1660
1661static void
1662socket_frame_init(const struct frame *frame, struct link_socket *sock)
1663{
1664#ifdef _WIN32
1665 overlapped_io_init(&sock->reads, frame, FALSE);
1666 overlapped_io_init(&sock->writes, frame, TRUE);
1667 sock->rw_handle.read = sock->reads.overlapped.hEvent;
1668 sock->rw_handle.write = sock->writes.overlapped.hEvent;
1669#endif
1670
1672 {
1673#ifdef _WIN32
1675 &sock->reads.buf_init,
1676 sock->sockflags,
1677 sock->info.proto);
1678#else
1680
1682 &sock->stream_buf_data,
1683 sock->sockflags,
1684 sock->info.proto);
1685#endif
1686 }
1687}
1688
1689static void
1691{
1692 struct gc_arena gc = gc_new();
1693
1694 /* resolve local address if undefined */
1695 if (!sock->info.lsa->bind_local)
1696 {
1699 int status;
1700
1701 if (proto_is_dgram(sock->info.proto))
1702 {
1703 flags |= GETADDR_DATAGRAM;
1704 }
1705
1706 /* will return AF_{INET|INET6}from local_host */
1708 sock->local_host,
1709 sock->local_port,
1710 af,
1711 flags,
1712 &sock->info.lsa->bind_local);
1713
1714 if (status)
1715 {
1716 status = openvpn_getaddrinfo(flags, sock->local_host, sock->local_port, 0,
1717 NULL, af, &sock->info.lsa->bind_local);
1718 }
1719
1720 if (status !=0)
1721 {
1722 msg(M_FATAL, "getaddrinfo() failed for local \"%s:%s\": %s",
1723 sock->local_host, sock->local_port,
1724 gai_strerror(status));
1725 }
1726
1727 /* the resolved 'local entry' might have a different family than what
1728 * was globally configured */
1729 sock->info.af = sock->info.lsa->bind_local->ai_family;
1730 }
1731
1732 gc_free(&gc);
1733}
1734
1735static void
1737 int phase,
1738 const char **remote_dynamic,
1739 struct signal_info *sig_info)
1740{
1741 volatile int *signal_received = sig_info ? &sig_info->signal_received : NULL;
1742 struct gc_arena gc = gc_new();
1743
1744 /* resolve remote address if undefined */
1745 if (!sock->info.lsa->remote_list)
1746 {
1747 if (sock->remote_host)
1748 {
1750 int retry = 0;
1751 int status = -1;
1752 struct addrinfo *ai;
1753 if (proto_is_dgram(sock->info.proto))
1754 {
1755 flags |= GETADDR_DATAGRAM;
1756 }
1757
1759 {
1760 if (phase == 2)
1761 {
1762 flags |= (GETADDR_TRY_ONCE | GETADDR_FATAL);
1763 }
1764 retry = 0;
1765 }
1766 else if (phase == 1)
1767 {
1768 if (sock->resolve_retry_seconds)
1769 {
1770 retry = 0;
1771 }
1772 else
1773 {
1775 retry = 0;
1776 }
1777 }
1778 else if (phase == 2)
1779 {
1780 if (sock->resolve_retry_seconds)
1781 {
1782 flags |= GETADDR_FATAL;
1783 retry = sock->resolve_retry_seconds;
1784 }
1785 else
1786 {
1787 ASSERT(0);
1788 }
1789 }
1790 else
1791 {
1792 ASSERT(0);
1793 }
1794
1795
1797 sock->remote_host,
1798 sock->remote_port,
1799 sock->info.af,
1800 flags, &ai);
1801 if (status)
1802 {
1803 status = openvpn_getaddrinfo(flags, sock->remote_host, sock->remote_port,
1804 retry, sig_info, sock->info.af, &ai);
1805 }
1806
1807 if (status == 0)
1808 {
1809 sock->info.lsa->remote_list = ai;
1810 sock->info.lsa->current_remote = ai;
1811
1813 "RESOLVE_REMOTE flags=0x%04x phase=%d rrs=%d sig=%d status=%d",
1814 flags,
1815 phase,
1816 retry,
1817 signal_received ? *signal_received : -1,
1818 status);
1819 }
1820 if (signal_received && *signal_received)
1821 {
1822 goto done;
1823 }
1824 if (status!=0)
1825 {
1826 if (signal_received)
1827 {
1828 /* potential overwrite of signal */
1829 register_signal(sig_info, SIGUSR1, "socks-resolve-failure");
1830 }
1831 goto done;
1832 }
1833 }
1834 }
1835
1836 /* should we re-use previous active remote address? */
1838 {
1839 msg(M_INFO, "TCP/UDP: Preserving recently used remote address: %s",
1841 if (remote_dynamic)
1842 {
1843 *remote_dynamic = NULL;
1844 }
1845 }
1846 else
1847 {
1848 CLEAR(sock->info.lsa->actual);
1849 if (sock->info.lsa->current_remote)
1850 {
1852 sock->info.lsa->current_remote);
1853 }
1854 }
1855
1856done:
1857 gc_free(&gc);
1858}
1859
1860
1861
1862struct link_socket *
1864{
1865 struct link_socket *sock;
1866
1867 ALLOC_OBJ_CLEAR(sock, struct link_socket);
1868 sock->sd = SOCKET_UNDEFINED;
1869 sock->ctrl_sd = SOCKET_UNDEFINED;
1871 sock->ev_arg.u.sock = sock;
1872
1873 return sock;
1874}
1875
1876void
1877link_socket_init_phase1(struct context *c, int sock_index, int mode)
1878{
1879 struct link_socket *sock = c->c2.link_sockets[sock_index];
1880 struct options *o = &c->options;
1881 ASSERT(sock);
1882
1883 const char *host = o->ce.local_list->array[sock_index]->local;
1884 const char *port = o->ce.local_list->array[sock_index]->port;
1885 int proto = o->ce.local_list->array[sock_index]->proto;
1886 const char *remote_host = o->ce.remote;
1887 const char *remote_port = o->ce.remote_port;
1888
1889 if (c->mode == CM_CHILD_TCP || c->mode == CM_CHILD_UDP)
1890 {
1891 struct link_socket *tmp_sock = NULL;
1892 if (c->mode == CM_CHILD_TCP)
1893 {
1894 tmp_sock = (struct link_socket *)c->c2.accept_from;
1895 }
1896 else if (c->mode == CM_CHILD_UDP)
1897 {
1898 tmp_sock = c->c2.link_sockets[0];
1899 }
1900
1901 host = tmp_sock->local_host;
1902 port = tmp_sock->local_port;
1903 proto = tmp_sock->info.proto;
1904 }
1905
1906 sock->local_host = host;
1907 sock->local_port = port;
1908 sock->remote_host = remote_host;
1909 sock->remote_port = remote_port;
1910 sock->dns_cache = c->c1.dns_cache;
1911 sock->http_proxy = c->c1.http_proxy;
1912 sock->socks_proxy = c->c1.socks_proxy;
1913 sock->bind_local = o->ce.bind_local;
1916
1917#ifdef ENABLE_DEBUG
1918 sock->gremlin = o->gremlin;
1919#endif
1920
1923
1924 sock->sockflags = o->sockflags;
1925
1926#if PORT_SHARE
1927 if (o->port_share_host && o->port_share_port)
1928 {
1929 sock->sockflags |= SF_PORT_SHARE;
1930 }
1931#endif
1932
1933 sock->mark = o->mark;
1934 sock->bind_dev = o->bind_dev;
1935 sock->info.proto = proto;
1936 sock->info.af = o->ce.af;
1937 sock->info.remote_float = o->ce.remote_float;
1938 sock->info.lsa = &c->c1.link_socket_addrs[sock_index];
1940 sock->info.ipchange_command = o->ipchange;
1941 sock->info.plugins = c->plugins;
1943
1944 sock->mode = mode;
1946 {
1947 ASSERT(c->c2.accept_from);
1949 sock->sd = c->c2.accept_from->sd;
1950 /* inherit (possibly guessed) info AF from parent context */
1951 sock->info.af = c->c2.accept_from->info.af;
1952 }
1953
1954 /* are we running in HTTP proxy mode? */
1955 if (sock->http_proxy)
1956 {
1958
1959 /* the proxy server */
1961 sock->remote_port = c->c1.http_proxy->options.port;
1962
1963 /* the OpenVPN server we will use the proxy to connect to */
1966 }
1967 /* or in Socks proxy mode? */
1968 else if (sock->socks_proxy)
1969 {
1970 /* the proxy server */
1971 sock->remote_host = c->c1.socks_proxy->server;
1972 sock->remote_port = c->c1.socks_proxy->port;
1973
1974 /* the OpenVPN server we will use the proxy to connect to */
1977 }
1978 else
1979 {
1980 sock->remote_host = remote_host;
1981 sock->remote_port = remote_port;
1982 }
1983
1984 /* bind behavior for TCP server vs. client */
1985 if (sock->info.proto == PROTO_TCP_SERVER)
1986 {
1987 if (sock->mode == LS_MODE_TCP_ACCEPT_FROM)
1988 {
1989 sock->bind_local = false;
1990 }
1991 else
1992 {
1993 sock->bind_local = true;
1994 }
1995 }
1996
1998 {
1999 if (sock->bind_local)
2000 {
2001 resolve_bind_local(sock, sock->info.af);
2002 }
2003 resolve_remote(sock, 1, NULL, NULL);
2004 }
2005}
2006
2007static void
2009{
2010 /* set misc socket parameters */
2011 socket_set_flags(sock->sd, sock->sockflags);
2012
2013 /* set socket to non-blocking mode */
2014 set_nonblock(sock->sd);
2015
2016 /* set Path MTU discovery options on the socket */
2017 set_mtu_discover_type(sock->sd, sock->mtu_discover_type, sock->info.af);
2018
2019#if EXTENDED_SOCKET_ERROR_CAPABILITY
2020 /* if the OS supports it, enable extended error passing on the socket */
2021 set_sock_extended_error_passing(sock->sd, sock->info.af);
2022#endif
2023}
2024
2025
2026static void
2028{
2029 struct gc_arena gc = gc_new();
2030 const int msglevel = (sock->mode == LS_MODE_TCP_ACCEPT_FROM) ? D_INIT_MEDIUM : M_INFO;
2031
2032 /* print local address */
2033 if (sock->bind_local)
2034 {
2035 sa_family_t ai_family = sock->info.lsa->actual.dest.addr.sa.sa_family;
2036 /* Socket is always bound on the first matching address,
2037 * For bound sockets with no remote addr this is the element of
2038 * the list */
2039 struct addrinfo *cur;
2040 for (cur = sock->info.lsa->bind_local; cur; cur = cur->ai_next)
2041 {
2042 if (!ai_family || ai_family == cur->ai_family)
2043 {
2044 break;
2045 }
2046 }
2047 ASSERT(cur);
2048 msg(msglevel, "%s link local (bound): %s",
2049 proto2ascii(sock->info.proto, sock->info.af, true),
2050 print_sockaddr(cur->ai_addr, &gc));
2051 }
2052 else
2053 {
2054 msg(msglevel, "%s link local: (not bound)",
2055 proto2ascii(sock->info.proto, sock->info.af, true));
2056 }
2057
2058 /* print active remote address */
2059 msg(msglevel, "%s link remote: %s",
2060 proto2ascii(sock->info.proto, sock->info.af, true),
2062 ":",
2064 &gc));
2065 gc_free(&gc);
2066}
2067
2068static void
2069phase2_tcp_server(struct link_socket *sock, const char *remote_dynamic,
2070 struct signal_info *sig_info)
2071{
2072 ASSERT(sig_info);
2073 volatile int *signal_received = &sig_info->signal_received;
2074 switch (sock->mode)
2075 {
2076 case LS_MODE_DEFAULT:
2077 sock->sd = socket_listen_accept(sock->sd,
2078 &sock->info.lsa->actual,
2079 remote_dynamic,
2080 sock->info.lsa->bind_local,
2081 true,
2082 false,
2083 signal_received);
2084 break;
2085
2086 case LS_MODE_TCP_LISTEN:
2087 socket_do_listen(sock->sd,
2088 sock->info.lsa->bind_local,
2089 true,
2090 false);
2091 break;
2092
2094 sock->sd = socket_do_accept(sock->sd,
2095 &sock->info.lsa->actual,
2096 false);
2097 if (!socket_defined(sock->sd))
2098 {
2099 register_signal(sig_info, SIGTERM, "socket-undefined");
2100 return;
2101 }
2103 break;
2104
2105 default:
2106 ASSERT(0);
2107 }
2108}
2109
2110
2111static void
2112phase2_tcp_client(struct link_socket *sock, struct signal_info *sig_info)
2113{
2114 bool proxy_retry = false;
2115 do
2116 {
2117 socket_connect(&sock->sd,
2118 sock->info.lsa->current_remote->ai_addr,
2120 sig_info);
2121
2122 if (sig_info->signal_received)
2123 {
2124 return;
2125 }
2126
2127 if (sock->http_proxy)
2128 {
2129 proxy_retry = establish_http_proxy_passthru(sock->http_proxy,
2130 sock->sd,
2131 sock->proxy_dest_host,
2132 sock->proxy_dest_port,
2133 sock->server_poll_timeout,
2134 &sock->stream_buf.residual,
2135 sig_info);
2136 }
2137 else if (sock->socks_proxy)
2138 {
2140 sock->sd,
2141 sock->proxy_dest_host,
2142 sock->proxy_dest_port,
2143 sock->server_poll_timeout,
2144 sig_info);
2145 }
2146 if (proxy_retry)
2147 {
2148 openvpn_close_socket(sock->sd);
2149 sock->sd = create_socket_tcp(sock->info.lsa->current_remote);
2150 }
2151
2152 } while (proxy_retry);
2153
2154}
2155
2156static void
2157phase2_socks_client(struct link_socket *sock, struct signal_info *sig_info)
2158{
2159 socket_connect(&sock->ctrl_sd,
2160 sock->info.lsa->current_remote->ai_addr,
2162 sig_info);
2163
2164 if (sig_info->signal_received)
2165 {
2166 return;
2167 }
2168
2170 sock->ctrl_sd,
2171 &sock->socks_relay.dest,
2172 sock->server_poll_timeout,
2173 sig_info);
2174
2175 if (sig_info->signal_received)
2176 {
2177 return;
2178 }
2179
2180 sock->remote_host = sock->proxy_dest_host;
2181 sock->remote_port = sock->proxy_dest_port;
2182
2184 if (sock->info.lsa->remote_list)
2185 {
2186 freeaddrinfo(sock->info.lsa->remote_list);
2187 sock->info.lsa->current_remote = NULL;
2188 sock->info.lsa->remote_list = NULL;
2189 }
2190
2191 resolve_remote(sock, 1, NULL, sig_info);
2192}
2193
2194#if defined(_WIN32)
2195static void
2197 struct signal_info *sig_info)
2198{
2199 /* in P2P mode we must have remote resolved at this point */
2200 struct addrinfo *remoteaddr = sock->info.lsa->current_remote;
2201 if ((c->options.mode == MODE_POINT_TO_POINT) && (!remoteaddr))
2202 {
2203 return;
2204 }
2205
2206 if (!c->c1.tuntap)
2207 {
2208 struct tuntap *tt;
2209 ALLOC_OBJ_CLEAR(tt, struct tuntap);
2210
2212
2213 const char *device_guid = NULL; /* not used */
2214 tun_open_device(tt, c->options.dev_node, &device_guid, &c->gc);
2215
2216 /* Ensure we can "safely" cast the handle to a socket */
2217 static_assert(sizeof(sock->sd) == sizeof(tt->hand), "HANDLE and SOCKET size differs");
2218
2219 c->c1.tuntap = tt;
2220 }
2221
2222 if (c->options.mode == MODE_SERVER)
2223 {
2224 dco_mp_start_vpn(c->c1.tuntap->hand, sock);
2225 }
2226 else
2227 {
2228 dco_p2p_new_peer(c->c1.tuntap->hand, sock, sig_info);
2229 }
2230 sock->sockflags |= SF_DCO_WIN;
2231
2232 if (sig_info->signal_received)
2233 {
2234 return;
2235 }
2236
2237 sock->sd = (SOCKET)c->c1.tuntap->hand;
2238 linksock_print_addr(sock);
2239}
2240#endif /* if defined(_WIN32) */
2241
2242/* finalize socket initialization */
2243void
2245 struct link_socket *sock)
2246{
2247 const struct frame *frame = &c->c2.frame;
2248 struct signal_info *sig_info = c->sig;
2249
2250 const char *remote_dynamic = NULL;
2251 struct signal_info sig_save = {0};
2252
2253 ASSERT(sock);
2254 ASSERT(sig_info);
2255
2256 if (sig_info->signal_received)
2257 {
2258 sig_save = *sig_info;
2259 sig_save.signal_received = signal_reset(sig_info, 0);
2260 }
2261
2262 /* initialize buffers */
2263 socket_frame_init(frame, sock);
2264
2265 /*
2266 * Pass a remote name to connect/accept so that
2267 * they can test for dynamic IP address changes
2268 * and throw a SIGUSR1 if appropriate.
2269 */
2270 if (sock->resolve_retry_seconds)
2271 {
2272 remote_dynamic = sock->remote_host;
2273 }
2274
2275 /* Second chance to resolv/create socket */
2276 resolve_remote(sock, 2, &remote_dynamic, sig_info);
2277
2278 /* If a valid remote has been found, create the socket with its addrinfo */
2279#if defined(_WIN32)
2280 if (dco_enabled(&c->options))
2281 {
2282 create_socket_dco_win(c, sock, sig_info);
2283 goto done;
2284 }
2285#endif
2286 if (sock->info.lsa->current_remote)
2287 {
2288 create_socket(sock, sock->info.lsa->current_remote);
2289 }
2290
2291 /* If socket has not already been created create it now */
2292 if (sock->sd == SOCKET_UNDEFINED)
2293 {
2294 /* If we have no --remote and have still not figured out the
2295 * protocol family to use we will use the first of the bind */
2296
2297 if (sock->bind_local && !sock->remote_host && sock->info.lsa->bind_local)
2298 {
2299 /* Warn if this is because neither v4 or v6 was specified
2300 * and we should not connect a remote */
2301 if (sock->info.af == AF_UNSPEC)
2302 {
2303 msg(M_WARN, "Could not determine IPv4/IPv6 protocol. Using %s",
2304 addr_family_name(sock->info.lsa->bind_local->ai_family));
2305 sock->info.af = sock->info.lsa->bind_local->ai_family;
2306 }
2307 create_socket(sock, sock->info.lsa->bind_local);
2308 }
2309 }
2310
2311 /* Socket still undefined, give a warning and abort connection */
2312 if (sock->sd == SOCKET_UNDEFINED)
2313 {
2314 msg(M_WARN, "Could not determine IPv4/IPv6 protocol");
2315 register_signal(sig_info, SIGUSR1, "Could not determine IPv4/IPv6 protocol");
2316 goto done;
2317 }
2318
2319 if (sig_info->signal_received)
2320 {
2321 goto done;
2322 }
2323
2324 if (sock->info.proto == PROTO_TCP_SERVER)
2325 {
2326 phase2_tcp_server(sock, remote_dynamic, sig_info);
2327 }
2328 else if (sock->info.proto == PROTO_TCP_CLIENT)
2329 {
2330 phase2_tcp_client(sock, sig_info);
2331
2332 }
2333 else if (sock->info.proto == PROTO_UDP && sock->socks_proxy)
2334 {
2335 phase2_socks_client(sock, sig_info);
2336 }
2337#ifdef TARGET_ANDROID
2338 if (sock->sd != -1)
2339 {
2340 protect_fd_nonlocal(sock->sd, &sock->info.lsa->actual.dest.addr.sa);
2341 }
2342#endif
2343 if (sig_info->signal_received)
2344 {
2345 goto done;
2346 }
2347
2349 linksock_print_addr(sock);
2350
2351done:
2352 if (sig_save.signal_received)
2353 {
2354 /* Always restore the saved signal -- register/throw_signal will handle priority */
2355 if (sig_save.source == SIG_SOURCE_HARD && sig_info == &siginfo_static)
2356 {
2357 throw_signal(sig_save.signal_received);
2358 }
2359 else
2360 {
2361 register_signal(sig_info, sig_save.signal_received, sig_save.signal_text);
2362 }
2363 }
2364}
2365
2366void
2368{
2369 if (sock)
2370 {
2371#ifdef ENABLE_DEBUG
2372 const int gremlin = GREMLIN_CONNECTION_FLOOD_LEVEL(sock->gremlin);
2373#else
2374 const int gremlin = 0;
2375#endif
2376
2377 if (socket_defined(sock->sd))
2378 {
2379#ifdef _WIN32
2380 close_net_event_win32(&sock->listen_handle, sock->sd, 0);
2381#endif
2382 if (!gremlin)
2383 {
2384 msg(D_LOW, "TCP/UDP: Closing socket");
2385 if (openvpn_close_socket(sock->sd))
2386 {
2387 msg(M_WARN | M_ERRNO, "TCP/UDP: Close Socket failed");
2388 }
2389 }
2390 sock->sd = SOCKET_UNDEFINED;
2391#ifdef _WIN32
2392 if (!gremlin)
2393 {
2394 overlapped_io_close(&sock->reads);
2396 }
2397#endif
2398 }
2399
2400 if (socket_defined(sock->ctrl_sd))
2401 {
2402 if (openvpn_close_socket(sock->ctrl_sd))
2403 {
2404 msg(M_WARN | M_ERRNO, "TCP/UDP: Close Socket (ctrl_sd) failed");
2405 }
2406 sock->ctrl_sd = SOCKET_UNDEFINED;
2407 }
2408
2410 free_buf(&sock->stream_buf_data);
2411 if (!gremlin)
2412 {
2413 free(sock);
2414 }
2415 }
2416}
2417
2418void
2419setenv_trusted(struct env_set *es, const struct link_socket_info *info)
2420{
2421 setenv_link_socket_actual(es, "trusted", &info->lsa->actual, SA_IP_PORT);
2422}
2423
2424static void
2425ipchange_fmt(const bool include_cmd, struct argv *argv, const struct link_socket_info *info, struct gc_arena *gc)
2426{
2427 const char *host = print_sockaddr_ex(&info->lsa->actual.dest.addr.sa, " ", PS_SHOW_PORT, gc);
2428 if (include_cmd)
2429 {
2431 argv_printf_cat(argv, "%s", host);
2432 }
2433 else
2434 {
2435 argv_printf(argv, "%s", host);
2436 }
2437
2438}
2439
2440void
2442 const struct link_socket_actual *act,
2443 const char *common_name,
2444 struct env_set *es)
2445{
2446 struct gc_arena gc = gc_new();
2447
2448 info->lsa->actual = *act; /* Note: skip this line for --force-dest */
2449 setenv_trusted(es, info);
2450 info->connection_established = true;
2451
2452 /* Print connection initiated message, with common name if available */
2453 {
2454 struct buffer out = alloc_buf_gc(256, &gc);
2455 if (common_name)
2456 {
2457 buf_printf(&out, "[%s] ", common_name);
2458 }
2459 buf_printf(&out, "Peer Connection Initiated with %s", print_link_socket_actual(&info->lsa->actual, &gc));
2460 msg(M_INFO, "%s", BSTR(&out));
2461 }
2462
2463 /* set environmental vars */
2464 setenv_str(es, "common_name", common_name);
2465
2466 /* Process --ipchange plugin */
2468 {
2469 struct argv argv = argv_new();
2470 ipchange_fmt(false, &argv, info, &gc);
2471 if (plugin_call(info->plugins, OPENVPN_PLUGIN_IPCHANGE, &argv, NULL, es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
2472 {
2473 msg(M_WARN, "WARNING: ipchange plugin call failed");
2474 }
2475 argv_free(&argv);
2476 }
2477
2478 /* Process --ipchange option */
2479 if (info->ipchange_command)
2480 {
2481 struct argv argv = argv_new();
2482 setenv_str(es, "script_type", "ipchange");
2483 ipchange_fmt(true, &argv, info, &gc);
2484 openvpn_run_script(&argv, es, 0, "--ipchange");
2485 argv_free(&argv);
2486 }
2487
2488 gc_free(&gc);
2489}
2490
2491void
2493 const struct link_socket_info *info,
2494 const struct link_socket_actual *from_addr)
2495{
2496 struct gc_arena gc = gc_new();
2497 struct addrinfo *ai;
2498
2499 switch (from_addr->dest.addr.sa.sa_family)
2500 {
2501 case AF_INET:
2502 case AF_INET6:
2504 "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
2505 print_link_socket_actual(from_addr, &gc),
2506 (int)from_addr->dest.addr.sa.sa_family,
2507 print_sockaddr_ex(info->lsa->remote_list->ai_addr, ":", PS_SHOW_PORT, &gc));
2508 /* print additional remote addresses */
2509 for (ai = info->lsa->remote_list->ai_next; ai; ai = ai->ai_next)
2510 {
2511 msg(D_LINK_ERRORS, "or from peer address: %s",
2512 print_sockaddr_ex(ai->ai_addr, ":", PS_SHOW_PORT, &gc));
2513 }
2514 break;
2515 }
2516 buf->len = 0;
2517 gc_free(&gc);
2518}
2519
2520void
2522{
2523 dmsg(D_READ_WRITE, "TCP/UDP: No outgoing address to send packet");
2524}
2525
2526in_addr_t
2528{
2529 const struct link_socket_addr *lsa = info->lsa;
2530
2531/*
2532 * This logic supports "redirect-gateway" semantic, which
2533 * makes sense only for PF_INET routes over PF_INET endpoints
2534 *
2535 * Maybe in the future consider PF_INET6 endpoints also ...
2536 * by now just ignore it
2537 *
2538 * For --remote entries with multiple addresses this
2539 * only return the actual endpoint we have successfully connected to
2540 */
2541 if (lsa->actual.dest.addr.sa.sa_family != AF_INET)
2542 {
2543 return IPV4_INVALID_ADDR;
2544 }
2545
2547 {
2548 return ntohl(lsa->actual.dest.addr.in4.sin_addr.s_addr);
2549 }
2550 else if (lsa->current_remote)
2551 {
2552 return ntohl(((struct sockaddr_in *)lsa->current_remote->ai_addr)
2553 ->sin_addr.s_addr);
2554 }
2555 else
2556 {
2557 return 0;
2558 }
2559}
2560
2561const struct in6_addr *
2563{
2564 const struct link_socket_addr *lsa = info->lsa;
2565
2566/* This logic supports "redirect-gateway" semantic,
2567 * for PF_INET6 routes over PF_INET6 endpoints
2568 *
2569 * For --remote entries with multiple addresses this
2570 * only return the actual endpoint we have successfully connected to
2571 */
2572 if (lsa->actual.dest.addr.sa.sa_family != AF_INET6)
2573 {
2574 return NULL;
2575 }
2576
2578 {
2579 return &(lsa->actual.dest.addr.in6.sin6_addr);
2580 }
2581 else if (lsa->current_remote)
2582 {
2583 return &(((struct sockaddr_in6 *)lsa->current_remote->ai_addr)->sin6_addr);
2584 }
2585 else
2586 {
2587 return NULL;
2588 }
2589}
2590
2591/*
2592 * Return a status string describing socket state.
2593 */
2594const char *
2595socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
2596{
2597 struct buffer out = alloc_buf_gc(64, gc);
2598 if (s)
2599 {
2600 if (rwflags & EVENT_READ)
2601 {
2602 buf_printf(&out, "S%s",
2603 (s->rwflags_debug & EVENT_READ) ? "R" : "r");
2604#ifdef _WIN32
2605 buf_printf(&out, "%s",
2607#endif
2608 }
2609 if (rwflags & EVENT_WRITE)
2610 {
2611 buf_printf(&out, "S%s",
2612 (s->rwflags_debug & EVENT_WRITE) ? "W" : "w");
2613#ifdef _WIN32
2614 buf_printf(&out, "%s",
2616#endif
2617 }
2618 }
2619 else
2620 {
2621 buf_printf(&out, "S?");
2622 }
2623 return BSTR(&out);
2624}
2625
2626/*
2627 * Stream buffer functions, used to packetize a TCP
2628 * stream connection.
2629 */
2630
2631static inline void
2633{
2634 dmsg(D_STREAM_DEBUG, "STREAM: RESET");
2635 sb->residual_fully_formed = false;
2636 sb->buf = sb->buf_init;
2637 buf_reset(&sb->next);
2638 sb->len = -1;
2639}
2640
2641static void
2643 struct buffer *buf,
2644 const unsigned int sockflags,
2645 const int proto)
2646{
2647 sb->buf_init = *buf;
2648 sb->maxlen = sb->buf_init.len;
2649 sb->buf_init.len = 0;
2650 sb->residual = alloc_buf(sb->maxlen);
2651 sb->error = false;
2652#if PORT_SHARE
2653 sb->port_share_state = ((sockflags & SF_PORT_SHARE) && (proto == PROTO_TCP_SERVER))
2654 ? PS_ENABLED
2655 : PS_DISABLED;
2656#endif
2658
2659 dmsg(D_STREAM_DEBUG, "STREAM: INIT maxlen=%d", sb->maxlen);
2660}
2661
2662static inline void
2664{
2665 /* set up 'next' for next i/o read */
2666 sb->next = sb->buf;
2667 sb->next.offset = sb->buf.offset + sb->buf.len;
2668 sb->next.len = (sb->len >= 0 ? sb->len : sb->maxlen) - sb->buf.len;
2669 dmsg(D_STREAM_DEBUG, "STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d",
2670 sb->buf.offset, sb->buf.len,
2671 sb->next.offset, sb->next.len,
2672 sb->len, sb->maxlen);
2673 ASSERT(sb->next.len > 0);
2674 ASSERT(buf_safe(&sb->buf, sb->next.len));
2675}
2676
2677static inline void
2679{
2680 dmsg(D_STREAM_DEBUG, "STREAM: GET FINAL len=%d",
2681 buf_defined(&sb->buf) ? sb->buf.len : -1);
2682 ASSERT(buf_defined(&sb->buf));
2683 *buf = sb->buf;
2684}
2685
2686static inline void
2688{
2689 dmsg(D_STREAM_DEBUG, "STREAM: GET NEXT len=%d",
2690 buf_defined(&sb->next) ? sb->next.len : -1);
2691 ASSERT(buf_defined(&sb->next));
2692 *buf = sb->next;
2693}
2694
2695bool
2697{
2699 {
2701 ASSERT(buf_init(&sock->stream_buf.residual, 0));
2703 dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=%d",
2704 sock->stream_buf.residual_fully_formed ? "YES" : "NO",
2705 sock->stream_buf.residual.len);
2706 }
2707
2709 {
2711 }
2712 return !sock->stream_buf.residual_fully_formed;
2713}
2714
2715static bool
2717 int length_added)
2718{
2719 dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=%d", length_added);
2720 if (length_added > 0)
2721 {
2722 sb->buf.len += length_added;
2723 }
2724
2725 /* if length unknown, see if we can get the length prefix from
2726 * the head of the buffer */
2727 if (sb->len < 0 && sb->buf.len >= (int) sizeof(packet_size_type))
2728 {
2730
2731#if PORT_SHARE
2732 if (sb->port_share_state == PS_ENABLED)
2733 {
2734 if (!is_openvpn_protocol(&sb->buf))
2735 {
2736 msg(D_STREAM_ERRORS, "Non-OpenVPN client protocol detected");
2737 sb->port_share_state = PS_FOREIGN;
2738 sb->error = true;
2739 return false;
2740 }
2741 else
2742 {
2743 sb->port_share_state = PS_DISABLED;
2744 }
2745 }
2746#endif
2747
2748 ASSERT(buf_read(&sb->buf, &net_size, sizeof(net_size)));
2749 sb->len = ntohps(net_size);
2750
2751 if (sb->len < 1 || sb->len > sb->maxlen)
2752 {
2753 msg(M_WARN, "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]", sb->len, sb->maxlen);
2755 sb->error = true;
2756 return false;
2757 }
2758 }
2759
2760 /* is our incoming packet fully read? */
2761 if (sb->len > 0 && sb->buf.len >= sb->len)
2762 {
2763 /* save any residual data that's part of the next packet */
2764 ASSERT(buf_init(&sb->residual, 0));
2765 if (sb->buf.len > sb->len)
2766 {
2767 ASSERT(buf_copy_excess(&sb->residual, &sb->buf, sb->len));
2768 }
2769 dmsg(D_STREAM_DEBUG, "STREAM: ADD returned TRUE, buf_len=%d, residual_len=%d",
2770 BLEN(&sb->buf),
2771 BLEN(&sb->residual));
2772 return true;
2773 }
2774 else
2775 {
2776 dmsg(D_STREAM_DEBUG, "STREAM: ADD returned FALSE (have=%d need=%d)", sb->buf.len, sb->len);
2778 return false;
2779 }
2780}
2781
2782static void
2784{
2785 free_buf(&sb->residual);
2786}
2787
2788/*
2789 * The listen event is a special event whose sole purpose is
2790 * to tell us that there's a new incoming connection on a
2791 * TCP socket, for use in server mode.
2792 */
2793event_t
2795{
2796#ifdef _WIN32
2798 {
2800 }
2801 return &s->listen_handle;
2802#else /* ifdef _WIN32 */
2803 return s->sd;
2804#endif
2805}
2806
2807/*
2808 * Format IP addresses in ascii
2809 */
2810
2811const char *
2813 const char *separator,
2814 const unsigned int flags,
2815 struct gc_arena *gc)
2816{
2817 struct buffer out = alloc_buf_gc(128, gc);
2818 bool addr_is_defined = false;
2819 char hostaddr[NI_MAXHOST] = "";
2820 char servname[NI_MAXSERV] = "";
2821 int status;
2822
2823 socklen_t salen = 0;
2824 switch (sa->sa_family)
2825 {
2826 case AF_INET:
2827 if (!(flags & PS_DONT_SHOW_FAMILY))
2828 {
2829 buf_puts(&out, "[AF_INET]");
2830 }
2831 salen = sizeof(struct sockaddr_in);
2832 addr_is_defined = ((struct sockaddr_in *) sa)->sin_addr.s_addr != 0;
2833 break;
2834
2835 case AF_INET6:
2836 if (!(flags & PS_DONT_SHOW_FAMILY))
2837 {
2838 buf_puts(&out, "[AF_INET6]");
2839 }
2840 salen = sizeof(struct sockaddr_in6);
2841 addr_is_defined = !IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) sa)->sin6_addr);
2842 break;
2843
2844 case AF_UNSPEC:
2845 if (!(flags & PS_DONT_SHOW_FAMILY))
2846 {
2847 return "[AF_UNSPEC]";
2848 }
2849 else
2850 {
2851 return "";
2852 }
2853
2854 default:
2855 ASSERT(0);
2856 }
2857
2858 status = getnameinfo(sa, salen, hostaddr, sizeof(hostaddr),
2859 servname, sizeof(servname), NI_NUMERICHOST | NI_NUMERICSERV);
2860
2861 if (status!=0)
2862 {
2863 buf_printf(&out, "[nameinfo() err: %s]", gai_strerror(status));
2864 return BSTR(&out);
2865 }
2866
2867 if (!(flags & PS_DONT_SHOW_ADDR))
2868 {
2869 if (addr_is_defined)
2870 {
2871 buf_puts(&out, hostaddr);
2872 }
2873 else
2874 {
2875 buf_puts(&out, "[undef]");
2876 }
2877 }
2878
2879 if ((flags & PS_SHOW_PORT) || (flags & PS_SHOW_PORT_IF_DEFINED))
2880 {
2881 if (separator)
2882 {
2883 buf_puts(&out, separator);
2884 }
2885
2886 buf_puts(&out, servname);
2887 }
2888
2889 return BSTR(&out);
2890}
2891
2892const char *
2897
2898#ifndef IF_NAMESIZE
2899#define IF_NAMESIZE 16
2900#endif
2901
2902const char *
2904 const char *separator,
2905 const unsigned int flags,
2906 struct gc_arena *gc)
2907{
2908 if (act)
2909 {
2910 struct buffer out = alloc_buf_gc(128, gc);
2911 buf_printf(&out, "%s", print_sockaddr_ex(&act->dest.addr.sa, separator, flags, gc));
2912#if ENABLE_IP_PKTINFO
2913 char ifname[IF_NAMESIZE] = "[undef]";
2914
2915 if ((flags & PS_SHOW_PKTINFO) && addr_defined_ipi(act))
2916 {
2917 switch (act->dest.addr.sa.sa_family)
2918 {
2919 case AF_INET:
2920 {
2921 struct openvpn_sockaddr sa;
2922 CLEAR(sa);
2923 sa.addr.in4.sin_family = AF_INET;
2924#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2925 sa.addr.in4.sin_addr = act->pi.in4.ipi_spec_dst;
2926 if_indextoname(act->pi.in4.ipi_ifindex, ifname);
2927#elif defined(IP_RECVDSTADDR)
2928 sa.addr.in4.sin_addr = act->pi.in4;
2929 ifname[0] = 0;
2930#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
2931#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2932#endif
2933 buf_printf(&out, " (via %s%%%s)",
2934 print_sockaddr_ex(&sa.addr.sa, separator, 0, gc),
2935 ifname);
2936 }
2937 break;
2938
2939 case AF_INET6:
2940 {
2941 struct sockaddr_in6 sin6;
2942 char buf[INET6_ADDRSTRLEN] = "[undef]";
2943 CLEAR(sin6);
2944 sin6.sin6_family = AF_INET6;
2945 sin6.sin6_addr = act->pi.in6.ipi6_addr;
2946 if_indextoname(act->pi.in6.ipi6_ifindex, ifname);
2947 if (getnameinfo((struct sockaddr *)&sin6, sizeof(struct sockaddr_in6),
2948 buf, sizeof(buf), NULL, 0, NI_NUMERICHOST) == 0)
2949 {
2950 buf_printf(&out, " (via %s%%%s)", buf, ifname);
2951 }
2952 else
2953 {
2954 buf_printf(&out, " (via [getnameinfo() err]%%%s)", ifname);
2955 }
2956 }
2957 break;
2958 }
2959 }
2960#endif /* if ENABLE_IP_PKTINFO */
2961 return BSTR(&out);
2962 }
2963 else
2964 {
2965 return "[NULL]";
2966 }
2967}
2968
2969/*
2970 * Convert an in_addr_t in host byte order
2971 * to an ascii dotted quad.
2972 */
2973const char *
2974print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
2975{
2976 struct in_addr ia;
2977 char *out = gc_malloc(INET_ADDRSTRLEN, true, gc);
2978
2979 if (addr || !(flags & IA_EMPTY_IF_UNDEF))
2980 {
2981 CLEAR(ia);
2982 ia.s_addr = (flags & IA_NET_ORDER) ? addr : htonl(addr);
2983
2984 inet_ntop(AF_INET, &ia, out, INET_ADDRSTRLEN);
2985 }
2986 return out;
2987}
2988
2989/*
2990 * Convert an in6_addr in host byte order
2991 * to an ascii representation of an IPv6 address
2992 */
2993const char *
2994print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
2995{
2996 char *out = gc_malloc(INET6_ADDRSTRLEN, true, gc);
2997
2998 if (memcmp(&a6, &in6addr_any, sizeof(a6)) != 0
2999 || !(flags & IA_EMPTY_IF_UNDEF))
3000 {
3001 inet_ntop(AF_INET6, &a6, out, INET6_ADDRSTRLEN);
3002 }
3003 return out;
3004}
3005
3006/*
3007 * Convert an in_port_t in host byte order to a string
3008 */
3009const char *
3010print_in_port_t(in_port_t port, struct gc_arena *gc)
3011{
3012 struct buffer buffer = alloc_buf_gc(8, gc);
3013 buf_printf(&buffer, "%hu", port);
3014 return BSTR(&buffer);
3015}
3016
3017#ifndef UINT8_MAX
3018#define UINT8_MAX 0xff
3019#endif
3020
3021/* add some offset to an ipv6 address
3022 * (add in steps of 8 bits, taking overflow into next round)
3023 */
3024struct in6_addr
3025add_in6_addr( struct in6_addr base, uint32_t add )
3026{
3027 int i;
3028
3029 for (i = 15; i>=0 && add > 0; i--)
3030 {
3031 register int carry;
3032 register uint32_t h;
3033
3034 h = (unsigned char) base.s6_addr[i];
3035 base.s6_addr[i] = (h+add) & UINT8_MAX;
3036
3037 /* using explicit carry for the 8-bit additions will catch
3038 * 8-bit and(!) 32-bit overruns nicely
3039 */
3040 carry = ((h & 0xff) + (add & 0xff)) >> 8;
3041 add = (add>>8) + carry;
3042 }
3043 return base;
3044}
3045
3046/* set environmental variables for ip/port in *addr */
3047void
3048setenv_sockaddr(struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const unsigned int flags)
3049{
3050 char name_buf[256];
3051
3052 char buf[INET6_ADDRSTRLEN];
3053 switch (addr->addr.sa.sa_family)
3054 {
3055 case AF_INET:
3056 if (flags & SA_IP_PORT)
3057 {
3058 snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix);
3059 }
3060 else
3061 {
3062 snprintf(name_buf, sizeof(name_buf), "%s", name_prefix);
3063 }
3064
3065 inet_ntop(AF_INET, &addr->addr.in4.sin_addr, buf, sizeof(buf));
3066 setenv_str(es, name_buf, buf);
3067
3068 if ((flags & SA_IP_PORT) && addr->addr.in4.sin_port)
3069 {
3070 snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefix);
3071 setenv_int(es, name_buf, ntohs(addr->addr.in4.sin_port));
3072 }
3073 break;
3074
3075 case AF_INET6:
3076 if (IN6_IS_ADDR_V4MAPPED( &addr->addr.in6.sin6_addr ))
3077 {
3078 struct in_addr ia;
3079 memcpy(&ia.s_addr, &addr->addr.in6.sin6_addr.s6_addr[12],
3080 sizeof(ia.s_addr));
3081 snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix);
3082 inet_ntop(AF_INET, &ia, buf, sizeof(buf));
3083 }
3084 else
3085 {
3086 snprintf(name_buf, sizeof(name_buf), "%s_ip6", name_prefix);
3087 inet_ntop(AF_INET6, &addr->addr.in6.sin6_addr, buf, sizeof(buf));
3088 }
3089 setenv_str(es, name_buf, buf);
3090
3091 if ((flags & SA_IP_PORT) && addr->addr.in6.sin6_port)
3092 {
3093 snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefix);
3094 setenv_int(es, name_buf, ntohs(addr->addr.in6.sin6_port));
3095 }
3096 break;
3097 }
3098}
3099
3100void
3101setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t addr, const unsigned int flags)
3102{
3103 if (addr || !(flags & SA_SET_IF_NONZERO))
3104 {
3105 struct openvpn_sockaddr si;
3106 CLEAR(si);
3107 si.addr.in4.sin_family = AF_INET;
3108 si.addr.in4.sin_addr.s_addr = htonl(addr);
3109 setenv_sockaddr(es, name_prefix, &si, flags);
3110 }
3111}
3112
3113void
3115 const char *name_prefix,
3116 const struct in6_addr *addr,
3117 const unsigned int flags)
3118{
3119 if (!IN6_IS_ADDR_UNSPECIFIED(addr) || !(flags & SA_SET_IF_NONZERO))
3120 {
3121 struct openvpn_sockaddr si;
3122 CLEAR(si);
3123 si.addr.in6.sin6_family = AF_INET6;
3124 si.addr.in6.sin6_addr = *addr;
3125 setenv_sockaddr(es, name_prefix, &si, flags);
3126 }
3127}
3128
3129void
3131 const char *name_prefix,
3132 const struct link_socket_actual *act,
3133 const unsigned int flags)
3134{
3135 setenv_sockaddr(es, name_prefix, &act->dest, flags);
3136}
3137
3138/*
3139 * Convert protocol names between index and ascii form.
3140 */
3141
3148
3149/* Indexed by PROTO_x */
3150static const struct proto_names proto_names[] = {
3151 {"proto-uninitialized", "proto-NONE", AF_UNSPEC, PROTO_NONE},
3152 /* try IPv4 and IPv6 (client), bind dual-stack (server) */
3153 {"udp", "UDP", AF_UNSPEC, PROTO_UDP},
3154 {"tcp-server", "TCP_SERVER", AF_UNSPEC, PROTO_TCP_SERVER},
3155 {"tcp-client", "TCP_CLIENT", AF_UNSPEC, PROTO_TCP_CLIENT},
3156 {"tcp", "TCP", AF_UNSPEC, PROTO_TCP},
3157 /* force IPv4 */
3158 {"udp4", "UDPv4", AF_INET, PROTO_UDP},
3159 {"tcp4-server", "TCPv4_SERVER", AF_INET, PROTO_TCP_SERVER},
3160 {"tcp4-client", "TCPv4_CLIENT", AF_INET, PROTO_TCP_CLIENT},
3161 {"tcp4", "TCPv4", AF_INET, PROTO_TCP},
3162 /* force IPv6 */
3163 {"udp6", "UDPv6", AF_INET6, PROTO_UDP},
3164 {"tcp6-server", "TCPv6_SERVER", AF_INET6, PROTO_TCP_SERVER},
3165 {"tcp6-client", "TCPv6_CLIENT", AF_INET6, PROTO_TCP_CLIENT},
3166 {"tcp6", "TCPv6", AF_INET6, PROTO_TCP},
3167};
3168
3169int
3170ascii2proto(const char *proto_name)
3171{
3172 for (size_t i = 0; i < SIZE(proto_names); ++i)
3173 {
3174 if (!strcmp(proto_name, proto_names[i].short_form))
3175 {
3176 return proto_names[i].proto;
3177 }
3178 }
3179 return -1;
3180}
3181
3183ascii2af(const char *proto_name)
3184{
3185 for (size_t i = 0; i < SIZE(proto_names); ++i)
3186 {
3187 if (!strcmp(proto_name, proto_names[i].short_form))
3188 {
3189 return proto_names[i].proto_af;
3190 }
3191 }
3192 return 0;
3193}
3194
3195const char *
3197{
3198 for (size_t i = 0; i < SIZE(proto_names); ++i)
3199 {
3200 if (proto_names[i].proto_af == af && proto_names[i].proto == proto)
3201 {
3202 if (display_form)
3203 {
3204 return proto_names[i].display_form;
3205 }
3206 else
3207 {
3208 return proto_names[i].short_form;
3209 }
3210 }
3211 }
3212
3213 return "[unknown protocol]";
3214}
3215
3216const char *
3218{
3219 struct buffer out = alloc_buf_gc(256, gc);
3220
3221 for (size_t i = 0; i < SIZE(proto_names); ++i)
3222 {
3223 if (i)
3224 {
3225 buf_printf(&out, " ");
3226 }
3227 buf_printf(&out, "[%s]", proto_names[i].short_form);
3228 }
3229 return BSTR(&out);
3230}
3231
3232const char *
3234{
3235 switch (af)
3236 {
3237 case AF_INET: return "AF_INET";
3238
3239 case AF_INET6: return "AF_INET6";
3240 }
3241 return "AF_UNSPEC";
3242}
3243
3244/*
3245 * Given a local proto, return local proto
3246 * if !remote, or compatible remote proto
3247 * if remote.
3248 *
3249 * This is used for options compatibility
3250 * checking.
3251 *
3252 * IPv6 and IPv4 protocols are comptabile but OpenVPN
3253 * has always sent UDPv4, TCPv4 over the wire. Keep these
3254 * strings for backward compatibility
3255 */
3256const char *
3257proto_remote(int proto, bool remote)
3258{
3259 ASSERT(proto >= 0 && proto < PROTO_N);
3260 if (proto == PROTO_UDP)
3261 {
3262 return "UDPv4";
3263 }
3264
3265 if ( (remote && proto == PROTO_TCP_CLIENT)
3266 || (!remote && proto == PROTO_TCP_SERVER))
3267 {
3268 return "TCPv4_SERVER";
3269 }
3270 if ( (remote && proto == PROTO_TCP_SERVER)
3271 || (!remote && proto == PROTO_TCP_CLIENT))
3272 {
3273 return "TCPv4_CLIENT";
3274 }
3275
3276 ASSERT(0);
3277 return ""; /* Make the compiler happy */
3278}
3279
3280/*
3281 * Bad incoming address lengths that differ from what
3282 * we expect are considered to be fatal errors.
3283 */
3284void
3286{
3287 msg(M_FATAL, "ERROR: received strange incoming packet with an address length of %d -- we only accept address lengths of %d.",
3288 actual,
3289 expected);
3290}
3291
3292/*
3293 * Socket Read Routines
3294 */
3295
3296int
3298 struct buffer *buf)
3299{
3300 int len = 0;
3301
3303 {
3304 /* with Linux-DCO, we sometimes try to access a socket that is
3305 * already installed in the kernel and has no valid file descriptor
3306 * anymore. This is a bug.
3307 * Handle by resetting client instance instead of crashing.
3308 */
3309 if (sock->sd == SOCKET_UNDEFINED)
3310 {
3311 msg(M_INFO, "BUG: link_socket_read_tcp(): sock->sd==-1, reset client instance" );
3312 sock->stream_reset = true; /* reset client instance */
3313 return buf->len = 0; /* nothing to read */
3314 }
3315
3316#ifdef _WIN32
3317 sockethandle_t sh = { .s = sock->sd };
3318 len = sockethandle_finalize(sh, &sock->reads, buf, NULL);
3319#else
3320 struct buffer frag;
3322 len = recv(sock->sd, BPTR(&frag), BLEN(&frag), MSG_NOSIGNAL);
3323#endif
3324
3325 if (!len)
3326 {
3327 sock->stream_reset = true;
3328 }
3329 if (len <= 0)
3330 {
3331 return buf->len = len;
3332 }
3333 }
3334
3336 || stream_buf_added(&sock->stream_buf, len)) /* packet complete? */
3337 {
3338 stream_buf_get_final(&sock->stream_buf, buf);
3340 return buf->len;
3341 }
3342 else
3343 {
3344 return buf->len = 0; /* no error, but packet is still incomplete */
3345 }
3346}
3347
3348#ifndef _WIN32
3349
3350#if ENABLE_IP_PKTINFO
3351
3352/* make the buffer large enough to handle ancillary socket data for
3353 * both IPv4 and IPv6 destination addresses, plus padding (see RFC 2292)
3354 */
3355#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
3356#define PKTINFO_BUF_SIZE max_int( CMSG_SPACE(sizeof(struct in6_pktinfo)), \
3357 CMSG_SPACE(sizeof(struct in_pktinfo)) )
3358#else
3359#define PKTINFO_BUF_SIZE max_int( CMSG_SPACE(sizeof(struct in6_pktinfo)), \
3360 CMSG_SPACE(sizeof(struct in_addr)) )
3361#endif
3362
3363static socklen_t
3365 struct buffer *buf,
3366 struct link_socket_actual *from)
3367{
3368 struct iovec iov;
3369 uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
3370 struct msghdr mesg = {0};
3371 socklen_t fromlen = sizeof(from->dest.addr);
3372
3373 ASSERT(sock->sd >= 0); /* can't happen */
3374
3375 iov.iov_base = BPTR(buf);
3376 iov.iov_len = buf_forward_capacity_total(buf);
3377 mesg.msg_iov = &iov;
3378 mesg.msg_iovlen = 1;
3379 mesg.msg_name = &from->dest.addr;
3380 mesg.msg_namelen = fromlen;
3381 mesg.msg_control = pktinfo_buf;
3382 mesg.msg_controllen = sizeof pktinfo_buf;
3383 buf->len = recvmsg(sock->sd, &mesg, 0);
3384 if (buf->len >= 0)
3385 {
3386 struct cmsghdr *cmsg;
3387 fromlen = mesg.msg_namelen;
3388 cmsg = CMSG_FIRSTHDR(&mesg);
3389 if (cmsg != NULL
3390 && CMSG_NXTHDR(&mesg, cmsg) == NULL
3391#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
3392 && cmsg->cmsg_level == SOL_IP
3393 && cmsg->cmsg_type == IP_PKTINFO
3394 && cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in_pktinfo)) )
3395#elif defined(IP_RECVDSTADDR)
3396 && cmsg->cmsg_level == IPPROTO_IP
3397 && cmsg->cmsg_type == IP_RECVDSTADDR
3398 && cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in_addr)) )
3399#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
3400#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
3401#endif
3402 {
3403#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
3404 struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
3405 from->pi.in4.ipi_ifindex = pkti->ipi_ifindex;
3406 from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
3407#elif defined(IP_RECVDSTADDR)
3408 from->pi.in4 = *(struct in_addr *) CMSG_DATA(cmsg);
3409#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
3410#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
3411#endif
3412 }
3413 else if (cmsg != NULL
3414 && CMSG_NXTHDR(&mesg, cmsg) == NULL
3415 && cmsg->cmsg_level == IPPROTO_IPV6
3416 && cmsg->cmsg_type == IPV6_PKTINFO
3417 && cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in6_pktinfo)) )
3418 {
3419 struct in6_pktinfo *pkti6 = (struct in6_pktinfo *) CMSG_DATA(cmsg);
3420 from->pi.in6.ipi6_ifindex = pkti6->ipi6_ifindex;
3421 from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
3422 }
3423 else if (cmsg != NULL)
3424 {
3425 msg(M_WARN, "CMSG received that cannot be parsed (cmsg_level=%d, cmsg_type=%d, cmsg=len=%d)", (int)cmsg->cmsg_level, (int)cmsg->cmsg_type, (int)cmsg->cmsg_len );
3426 }
3427 }
3428
3429 return fromlen;
3430}
3431#endif /* if ENABLE_IP_PKTINFO */
3432
3433int
3434link_socket_read_udp_posix(struct link_socket *sock,
3435 struct buffer *buf,
3436 struct link_socket_actual *from)
3437{
3438 socklen_t fromlen = sizeof(from->dest.addr);
3439 socklen_t expectedlen = af_addr_size(sock->info.af);
3440 addr_zero_host(&from->dest);
3441
3442 ASSERT(sock->sd >= 0); /* can't happen */
3443
3444#if ENABLE_IP_PKTINFO
3445 /* Both PROTO_UDPv4 and PROTO_UDPv6 */
3446 if (sock->info.proto == PROTO_UDP && sock->sockflags & SF_USE_IP_PKTINFO)
3447 {
3448 fromlen = link_socket_read_udp_posix_recvmsg(sock, buf, from);
3449 }
3450 else
3451#endif
3452 {
3453 buf->len = recvfrom(sock->sd, BPTR(buf), buf_forward_capacity(buf), 0,
3454 &from->dest.addr.sa, &fromlen);
3455 }
3456 /* FIXME: won't do anything when sock->info.af == AF_UNSPEC */
3457 if (buf->len >= 0 && expectedlen && fromlen != expectedlen)
3458 {
3459 bad_address_length(fromlen, expectedlen);
3460 }
3461 return buf->len;
3462}
3463
3464#endif /* ifndef _WIN32 */
3465
3466/*
3467 * Socket Write Routines
3468 */
3469
3470ssize_t
3472 struct buffer *buf,
3473 struct link_socket_actual *to)
3474{
3475 packet_size_type len = BLEN(buf);
3476 dmsg(D_STREAM_DEBUG, "STREAM: WRITE %d offset=%d", (int)len, buf->offset);
3477 ASSERT(len <= sock->stream_buf.maxlen);
3478 len = htonps(len);
3479 ASSERT(buf_write_prepend(buf, &len, sizeof(len)));
3480#ifdef _WIN32
3481 return link_socket_write_win32(sock, buf, to);
3482#else
3483 return link_socket_write_tcp_posix(sock, buf);
3484#endif
3485}
3486
3487#if ENABLE_IP_PKTINFO
3488
3489ssize_t
3490link_socket_write_udp_posix_sendmsg(struct link_socket *sock,
3491 struct buffer *buf,
3492 struct link_socket_actual *to)
3493{
3494 struct iovec iov;
3495 struct msghdr mesg;
3496 struct cmsghdr *cmsg;
3497 uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
3498
3499 iov.iov_base = BPTR(buf);
3500 iov.iov_len = BLEN(buf);
3501 mesg.msg_iov = &iov;
3502 mesg.msg_iovlen = 1;
3503 switch (to->dest.addr.sa.sa_family)
3504 {
3505 case AF_INET:
3506 {
3507 mesg.msg_name = &to->dest.addr.sa;
3508 mesg.msg_namelen = sizeof(struct sockaddr_in);
3509 mesg.msg_control = pktinfo_buf;
3510 mesg.msg_flags = 0;
3511#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
3512 mesg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
3513 cmsg = CMSG_FIRSTHDR(&mesg);
3514 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
3515 cmsg->cmsg_level = SOL_IP;
3516 cmsg->cmsg_type = IP_PKTINFO;
3517 {
3518 struct in_pktinfo *pkti;
3519 pkti = (struct in_pktinfo *) CMSG_DATA(cmsg);
3520 pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
3521 pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
3522 pkti->ipi_addr.s_addr = 0;
3523 }
3524#elif defined(IP_RECVDSTADDR)
3525 ASSERT( CMSG_SPACE(sizeof(struct in_addr)) <= sizeof(pktinfo_buf) );
3526 mesg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
3527 cmsg = CMSG_FIRSTHDR(&mesg);
3528 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
3529 cmsg->cmsg_level = IPPROTO_IP;
3530 cmsg->cmsg_type = IP_RECVDSTADDR;
3531 *(struct in_addr *) CMSG_DATA(cmsg) = to->pi.in4;
3532#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
3533#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
3534#endif /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
3535 break;
3536 }
3537
3538 case AF_INET6:
3539 {
3540 struct in6_pktinfo *pkti6;
3541 mesg.msg_name = &to->dest.addr.sa;
3542 mesg.msg_namelen = sizeof(struct sockaddr_in6);
3543
3544 ASSERT( CMSG_SPACE(sizeof(struct in6_pktinfo)) <= sizeof(pktinfo_buf) );
3545 mesg.msg_control = pktinfo_buf;
3546 mesg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
3547 mesg.msg_flags = 0;
3548 cmsg = CMSG_FIRSTHDR(&mesg);
3549 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
3550 cmsg->cmsg_level = IPPROTO_IPV6;
3551 cmsg->cmsg_type = IPV6_PKTINFO;
3552
3553 pkti6 = (struct in6_pktinfo *) CMSG_DATA(cmsg);
3554 pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
3555 pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
3556 break;
3557 }
3558
3559 default: ASSERT(0);
3560 }
3561 return sendmsg(sock->sd, &mesg, 0);
3562}
3563
3564#endif /* if ENABLE_IP_PKTINFO */
3565
3566/*
3567 * Win32 overlapped socket I/O functions.
3568 */
3569
3570#ifdef _WIN32
3571
3572static int
3574{
3575 if (socket_is_dco_win(sock))
3576 {
3577 return GetLastError();
3578 }
3579
3580 return WSAGetLastError();
3581}
3582
3583int
3584socket_recv_queue(struct link_socket *sock, int maxsize)
3585{
3586 if (sock->reads.iostate == IOSTATE_INITIAL)
3587 {
3588 WSABUF wsabuf[1];
3589 int status;
3590
3591 /* reset buf to its initial state */
3592 if (proto_is_udp(sock->info.proto))
3593 {
3594 sock->reads.buf = sock->reads.buf_init;
3595 }
3596 else if (proto_is_tcp(sock->info.proto))
3597 {
3598 stream_buf_get_next(&sock->stream_buf, &sock->reads.buf);
3599 }
3600 else
3601 {
3602 ASSERT(0);
3603 }
3604
3605 /* Win32 docs say it's okay to allocate the wsabuf on the stack */
3606 wsabuf[0].buf = BSTR(&sock->reads.buf);
3607 wsabuf[0].len = maxsize ? maxsize : BLEN(&sock->reads.buf);
3608
3609 /* check for buffer overflow */
3610 ASSERT(wsabuf[0].len <= BLEN(&sock->reads.buf));
3611
3612 /* the overlapped read will signal this event on I/O completion */
3613 ASSERT(ResetEvent(sock->reads.overlapped.hEvent));
3614 sock->reads.flags = 0;
3615
3616 if (socket_is_dco_win(sock))
3617 {
3618 status = ReadFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len,
3619 &sock->reads.size, &sock->reads.overlapped);
3620 /* Readfile status is inverted from WSARecv */
3621 status = !status;
3622 }
3623 else if (proto_is_udp(sock->info.proto))
3624 {
3625 sock->reads.addr_defined = true;
3626 sock->reads.addrlen = sizeof(sock->reads.addr6);
3627 status = WSARecvFrom(
3628 sock->sd,
3629 wsabuf,
3630 1,
3631 &sock->reads.size,
3632 &sock->reads.flags,
3633 (struct sockaddr *) &sock->reads.addr,
3634 &sock->reads.addrlen,
3635 &sock->reads.overlapped,
3636 NULL);
3637 }
3638 else if (proto_is_tcp(sock->info.proto))
3639 {
3640 sock->reads.addr_defined = false;
3641 status = WSARecv(
3642 sock->sd,
3643 wsabuf,
3644 1,
3645 &sock->reads.size,
3646 &sock->reads.flags,
3647 &sock->reads.overlapped,
3648 NULL);
3649 }
3650 else
3651 {
3652 status = 0;
3653 ASSERT(0);
3654 }
3655
3656 if (!status) /* operation completed immediately? */
3657 {
3658 /* FIXME: won't do anything when sock->info.af == AF_UNSPEC */
3659 int af_len = af_addr_size(sock->info.af);
3660 if (sock->reads.addr_defined && af_len && sock->reads.addrlen != af_len)
3661 {
3662 bad_address_length(sock->reads.addrlen, af_len);
3663 }
3665
3666 /* since we got an immediate return, we must signal the event object ourselves */
3667 ASSERT(SetEvent(sock->reads.overlapped.hEvent));
3668 sock->reads.status = 0;
3669
3670 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Receive immediate return [%d,%d]",
3671 (int) wsabuf[0].len,
3672 (int) sock->reads.size);
3673 }
3674 else
3675 {
3677 if (status == WSA_IO_PENDING) /* operation queued? */
3678 {
3680 sock->reads.status = status;
3681 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Receive queued [%d]",
3682 (int) wsabuf[0].len);
3683 }
3684 else /* error occurred */
3685 {
3686 struct gc_arena gc = gc_new();
3687 ASSERT(SetEvent(sock->reads.overlapped.hEvent));
3689 sock->reads.status = status;
3690 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Receive error [%d]: %s",
3691 (int) wsabuf[0].len,
3693 gc_free(&gc);
3694 }
3695 }
3696 }
3697 return sock->reads.iostate;
3698}
3699
3700int
3701socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
3702{
3703 if (sock->writes.iostate == IOSTATE_INITIAL)
3704 {
3705 WSABUF wsabuf[1];
3706 int status;
3707
3708 /* make a private copy of buf */
3709 sock->writes.buf = sock->writes.buf_init;
3710 sock->writes.buf.len = 0;
3711 ASSERT(buf_copy(&sock->writes.buf, buf));
3712
3713 /* Win32 docs say it's okay to allocate the wsabuf on the stack */
3714 wsabuf[0].buf = BSTR(&sock->writes.buf);
3715 wsabuf[0].len = BLEN(&sock->writes.buf);
3716
3717 /* the overlapped write will signal this event on I/O completion */
3718 ASSERT(ResetEvent(sock->writes.overlapped.hEvent));
3719 sock->writes.flags = 0;
3720
3721 if (socket_is_dco_win(sock))
3722 {
3723 status = WriteFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len,
3724 &sock->writes.size, &sock->writes.overlapped);
3725
3726 /* WriteFile status is inverted from WSASendTo */
3727 status = !status;
3728
3729 }
3730 else if (proto_is_udp(sock->info.proto))
3731 {
3732 /* set destination address for UDP writes */
3733 sock->writes.addr_defined = true;
3734 if (to->dest.addr.sa.sa_family == AF_INET6)
3735 {
3736 sock->writes.addr6 = to->dest.addr.in6;
3737 sock->writes.addrlen = sizeof(sock->writes.addr6);
3738 }
3739 else
3740 {
3741 sock->writes.addr = to->dest.addr.in4;
3742 sock->writes.addrlen = sizeof(sock->writes.addr);
3743 }
3744
3745 status = WSASendTo(
3746 sock->sd,
3747 wsabuf,
3748 1,
3749 &sock->writes.size,
3750 sock->writes.flags,
3751 (struct sockaddr *) &sock->writes.addr,
3752 sock->writes.addrlen,
3753 &sock->writes.overlapped,
3754 NULL);
3755 }
3756 else if (proto_is_tcp(sock->info.proto))
3757 {
3758 /* destination address for TCP writes was established on connection initiation */
3759 sock->writes.addr_defined = false;
3760
3761 status = WSASend(
3762 sock->sd,
3763 wsabuf,
3764 1,
3765 &sock->writes.size,
3766 sock->writes.flags,
3767 &sock->writes.overlapped,
3768 NULL);
3769 }
3770 else
3771 {
3772 status = 0;
3773 ASSERT(0);
3774 }
3775
3776 if (!status) /* operation completed immediately? */
3777 {
3779
3780 /* since we got an immediate return, we must signal the event object ourselves */
3781 ASSERT(SetEvent(sock->writes.overlapped.hEvent));
3782
3783 sock->writes.status = 0;
3784
3785 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Send immediate return [%d,%d]",
3786 (int) wsabuf[0].len,
3787 (int) sock->writes.size);
3788 }
3789 else
3790 {
3792 /* both status code have the identical value */
3793 if (status == WSA_IO_PENDING || status == ERROR_IO_PENDING) /* operation queued? */
3794 {
3796 sock->writes.status = status;
3797 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Send queued [%d]",
3798 (int) wsabuf[0].len);
3799 }
3800 else /* error occurred */
3801 {
3802 struct gc_arena gc = gc_new();
3803 ASSERT(SetEvent(sock->writes.overlapped.hEvent));
3805 sock->writes.status = status;
3806
3807 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Send error [%d]: %s",
3808 (int) wsabuf[0].len,
3810
3811 gc_free(&gc);
3812 }
3813 }
3814 }
3815 return sock->writes.iostate;
3816}
3817
3818void
3819read_sockaddr_from_overlapped(struct overlapped_io *io, struct sockaddr *dst, int overlapped_ret)
3820{
3821 if (overlapped_ret >= 0 && io->addr_defined)
3822 {
3823 /* TODO(jjo): streamline this mess */
3824 /* in this func we don't have relevant info about the PF_ of this
3825 * endpoint, as link_socket_actual will be zero for the 1st received packet
3826 *
3827 * Test for inets PF_ possible sizes
3828 */
3829 switch (io->addrlen)
3830 {
3831 case sizeof(struct sockaddr_in):
3832 case sizeof(struct sockaddr_in6):
3833 /* TODO(jjo): for some reason (?) I'm getting 24,28 for AF_INET6
3834 * under _WIN32*/
3835 case sizeof(struct sockaddr_in6) - 4:
3836 break;
3837
3838 default:
3839 bad_address_length(io->addrlen, af_addr_size(io->addr.sin_family));
3840 }
3841
3842 switch (io->addr.sin_family)
3843 {
3844 case AF_INET:
3845 memcpy(dst, &io->addr, sizeof(struct sockaddr_in));
3846 break;
3847
3848 case AF_INET6:
3849 memcpy(dst, &io->addr6, sizeof(struct sockaddr_in6));
3850 break;
3851 }
3852 }
3853 else
3854 {
3855 CLEAR(*dst);
3856 }
3857}
3858
3868static int
3869read_sockaddr_from_packet(struct buffer *buf, struct sockaddr *dst)
3870{
3871 int sa_len = 0;
3872
3873 const struct sockaddr *sa = (const struct sockaddr *)BPTR(buf);
3874 switch (sa->sa_family)
3875 {
3876 case AF_INET:
3877 sa_len = sizeof(struct sockaddr_in);
3878 if (buf_len(buf) < sa_len)
3879 {
3880 msg(M_FATAL, "ERROR: received incoming packet with too short length of %d -- must be at least %d.", buf_len(buf), sa_len);
3881 }
3882 memcpy(dst, sa, sa_len);
3883 buf_advance(buf, sa_len);
3884 break;
3885
3886 case AF_INET6:
3887 sa_len = sizeof(struct sockaddr_in6);
3888 if (buf_len(buf) < sa_len)
3889 {
3890 msg(M_FATAL, "ERROR: received incoming packet with too short length of %d -- must be at least %d.", buf_len(buf), sa_len);
3891 }
3892 memcpy(dst, sa, sa_len);
3893 buf_advance(buf, sa_len);
3894 break;
3895
3896 default:
3897 msg(M_FATAL, "ERROR: received incoming packet with invalid address family %d.", sa->sa_family);
3898 }
3899
3900 return sa_len;
3901}
3902
3903/* Returns the number of bytes successfully read */
3904int
3906 struct overlapped_io *io,
3907 struct buffer *buf,
3908 struct link_socket_actual *from)
3909{
3910 int ret = -1;
3911 BOOL status;
3912
3913 switch (io->iostate)
3914 {
3915 case IOSTATE_QUEUED:
3917 if (status)
3918 {
3919 /* successful return for a queued operation */
3920 if (buf)
3921 {
3922 *buf = io->buf;
3923 }
3924 ret = io->size;
3926 ASSERT(ResetEvent(io->overlapped.hEvent));
3927
3928 dmsg(D_WIN32_IO, "WIN32 I/O: Completion success [%d]", ret);
3929 }
3930 else
3931 {
3932 /* error during a queued operation */
3933 ret = -1;
3934 if (SocketHandleGetLastError(sh) != ERROR_IO_INCOMPLETE)
3935 {
3936 /* if no error (i.e. just not finished yet), then DON'T execute this code */
3938 ASSERT(ResetEvent(io->overlapped.hEvent));
3939 msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion error");
3940 }
3941 }
3942 break;
3943
3946 ASSERT(ResetEvent(io->overlapped.hEvent));
3947 if (io->status)
3948 {
3949 /* error return for a non-queued operation */
3951 ret = -1;
3952 msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion non-queued error");
3953 }
3954 else
3955 {
3956 /* successful return for a non-queued operation */
3957 if (buf)
3958 {
3959 *buf = io->buf;
3960 }
3961 ret = io->size;
3962 dmsg(D_WIN32_IO, "WIN32 I/O: Completion non-queued success [%d]", ret);
3963 }
3964 break;
3965
3966 case IOSTATE_INITIAL: /* were we called without proper queueing? */
3968 ret = -1;
3969 dmsg(D_WIN32_IO, "WIN32 I/O: Completion BAD STATE");
3970 break;
3971
3972 default:
3973 ASSERT(0);
3974 }
3975
3976 if (from && ret > 0 && sh.is_handle && sh.prepend_sa)
3977 {
3978 ret -= read_sockaddr_from_packet(buf, &from->dest.addr.sa);
3979 }
3980
3981 if (!sh.is_handle && from)
3982 {
3983 read_sockaddr_from_overlapped(io, &from->dest.addr.sa, ret);
3984 }
3985
3986 if (buf)
3987 {
3988 buf->len = ret;
3989 }
3990 return ret;
3991}
3992
3993#endif /* _WIN32 */
3994
3995/*
3996 * Socket event notification
3997 */
3998
3999unsigned int
4001 struct event_set *es,
4002 unsigned int rwflags,
4003 void *arg,
4004 unsigned int *persistent)
4005{
4006 if (s)
4007 {
4008 if ((rwflags & EVENT_READ) && !stream_buf_read_setup(s))
4009 {
4010 ASSERT(!persistent);
4011 rwflags &= ~EVENT_READ;
4012 }
4013
4014#ifdef _WIN32
4015 if (rwflags & EVENT_READ)
4016 {
4017 socket_recv_queue(s, 0);
4018 }
4019#endif
4020
4021 /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
4022 if (!persistent || *persistent != rwflags)
4023 {
4024 event_ctl(es, socket_event_handle(s), rwflags, arg);
4025 if (persistent)
4026 {
4027 *persistent = rwflags;
4028 }
4029 }
4030
4031 s->rwflags_debug = rwflags;
4032 }
4033 return rwflags;
4034}
4035
4036void
4038{
4039 if (sd && socket_defined(*sd))
4040 {
4042 *sd = SOCKET_UNDEFINED;
4043 }
4044}
4045
4046#if UNIX_SOCK_SUPPORT
4047
4048/*
4049 * code for unix domain sockets
4050 */
4051
4052const char *
4053sockaddr_unix_name(const struct sockaddr_un *local, const char *null)
4054{
4055 if (local && local->sun_family == PF_UNIX)
4056 {
4057 return local->sun_path;
4058 }
4059 else
4060 {
4061 return null;
4062 }
4063}
4064
4066create_socket_unix(void)
4067{
4069
4070 if ((sd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
4071 {
4072 msg(M_ERR, "Cannot create unix domain socket");
4073 }
4074
4075 /* set socket file descriptor to not pass across execs, so that
4076 * scripts don't have access to it */
4077 set_cloexec(sd);
4078
4079 return sd;
4080}
4081
4082void
4083socket_bind_unix(socket_descriptor_t sd,
4084 struct sockaddr_un *local,
4085 const char *prefix)
4086{
4087 struct gc_arena gc = gc_new();
4088 const mode_t orig_umask = umask(0);
4089
4090 if (bind(sd, (struct sockaddr *) local, sizeof(struct sockaddr_un)))
4091 {
4093 "%s: Socket bind[%d] failed on unix domain socket %s",
4094 prefix,
4095 (int)sd,
4096 sockaddr_unix_name(local, "NULL"));
4097 }
4098
4099 umask(orig_umask);
4100 gc_free(&gc);
4101}
4102
4104socket_accept_unix(socket_descriptor_t sd,
4105 struct sockaddr_un *remote)
4106{
4107 socklen_t remote_len = sizeof(struct sockaddr_un);
4109
4110 CLEAR(*remote);
4111 ret = accept(sd, (struct sockaddr *) remote, &remote_len);
4112 if (ret >= 0)
4113 {
4114 /* set socket file descriptor to not pass across execs, so that
4115 * scripts don't have access to it */
4116 set_cloexec(ret);
4117 }
4118 return ret;
4119}
4120
4121int
4122socket_connect_unix(socket_descriptor_t sd,
4123 struct sockaddr_un *remote)
4124{
4125 int status = connect(sd, (struct sockaddr *) remote, sizeof(struct sockaddr_un));
4126 if (status)
4127 {
4129 }
4130 return status;
4131}
4132
4133void
4134sockaddr_unix_init(struct sockaddr_un *local, const char *path)
4135{
4136 local->sun_family = PF_UNIX;
4137 strncpynt(local->sun_path, path, sizeof(local->sun_path));
4138}
4139
4140void
4141socket_delete_unix(const struct sockaddr_un *local)
4142{
4143 const char *name = sockaddr_unix_name(local, NULL);
4144 if (name && strlen(name))
4145 {
4146 unlink(name);
4147 }
4148}
4149
4150bool
4151unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, int *uid, int *gid)
4152{
4153#ifdef HAVE_GETPEEREID
4154 uid_t u;
4155 gid_t g;
4156 if (getpeereid(sd, &u, &g) == -1)
4157 {
4158 return false;
4159 }
4160 if (uid)
4161 {
4162 *uid = u;
4163 }
4164 if (gid)
4165 {
4166 *gid = g;
4167 }
4168 return true;
4169#elif defined(SO_PEERCRED)
4170 struct ucred peercred;
4171 socklen_t so_len = sizeof(peercred);
4172 if (getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) == -1)
4173 {
4174 return false;
4175 }
4176 if (uid)
4177 {
4178 *uid = peercred.uid;
4179 }
4180 if (gid)
4181 {
4182 *gid = peercred.gid;
4183 }
4184 return true;
4185#else /* ifdef HAVE_GETPEEREID */
4186 return false;
4187#endif /* ifdef HAVE_GETPEEREID */
4188}
4189
4190#endif /* if UNIX_SOCK_SUPPORT */
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
Definition argv.c:483
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:102
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:440
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:464
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:88
void free_buf(struct buffer *buf)
Definition buffer.c:183
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:240
bool buf_puts(struct buffer *buf, const char *str)
Definition buffer.c:267
bool string_class(const char *str, const unsigned int inclusive, const unsigned int exclusive)
Definition buffer.c:1022
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:336
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:88
struct buffer alloc_buf(size_t size)
Definition buffer.c:62
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Definition buffer.c:438
#define CC_DASH
dash
Definition buffer.h:902
#define BSTR(buf)
Definition buffer.h:129
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Definition buffer.h:712
#define BPTR(buf)
Definition buffer.h:124
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Definition buffer.h:753
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition buffer.h:680
#define CC_DIGIT
digit isdigit()
Definition buffer.h:890
#define CC_DOT
dot
Definition buffer.h:903
static void buf_reset(struct buffer *buf)
Definition buffer.h:303
static bool buf_safe(const struct buffer *buf, size_t len)
Definition buffer.h:520
static bool buf_read(struct buffer *src, void *dest, int size)
Definition buffer.h:778
static bool buf_advance(struct buffer *buf, int size)
Definition buffer.h:618
static int buf_len(const struct buffer *buf)
Definition buffer.h:253
static int buf_forward_capacity(const struct buffer *buf)
Definition buffer.h:541
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Definition buffer.h:1097
#define BLEN(buf)
Definition buffer.h:127
static void strncpynt(char *dest, const char *src, size_t maxlen)
Definition buffer.h:361
static void gc_free(struct gc_arena *a)
Definition buffer.h:1033
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1060
static bool buf_defined(const struct buffer *buf)
Definition buffer.h:228
#define CC_ALNUM
alphanumeric isalnum()
Definition buffer.h:886
#define buf_init(buf, offset)
Definition buffer.h:209
static void gc_freeaddrinfo_callback(void *addr)
Definition buffer.h:215
static struct gc_arena gc_new(void)
Definition buffer.h:1025
static int buf_forward_capacity_total(const struct buffer *buf)
Definition buffer.h:559
void dco_mp_start_vpn(HANDLE handle, struct link_socket *sock)
Initializes and binds the kernel UDP transport socket for multipeer mode.
Definition dco_win.c:280
void dco_p2p_new_peer(HANDLE handle, struct link_socket *sock, struct signal_info *sig_info)
Definition dco_win.c:324
void setenv_int(struct env_set *es, const char *name, int value)
Definition env_set.c:267
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:283
#define D_WIN32_IO
Definition errlevel.h:173
#define D_STREAM_ERRORS
Definition errlevel.h:63
#define D_SOCKET_DEBUG
Definition errlevel.h:140
#define D_STREAM_DEBUG
Definition errlevel.h:172
#define D_INIT_MEDIUM
Definition errlevel.h:104
#define D_READ_WRITE
Definition errlevel.h:167
#define D_OSBUF
Definition errlevel.h:91
#define D_RESOLVE_ERRORS
Definition errlevel.h:60
#define D_LOW
Definition errlevel.h:97
#define M_INFO
Definition errlevel.h:55
#define D_LINK_ERRORS
Definition errlevel.h:57
#define EVENT_WRITE
Definition event.h:40
#define EVENT_READ
Definition event.h:39
@ EVENT_ARG_LINK_SOCKET
Definition event.h:137
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:181
void set_nonblock(socket_descriptor_t fd)
Definition fdmisc.c:69
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:79
static void openvpn_fd_set(socket_descriptor_t fd, fd_set *setp)
Definition fdmisc.h:40
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition forward.c:509
Interface functions to the internal and external multiplexers.
static SERVICE_STATUS status
Definition interactive.c:53
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
Definition manage.c:2749
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4117
#define OPENVPN_STATE_RESOLVE
Definition manage.h:481
#define OPENVPN_STATE_TCP_CONNECT
Definition manage.h:482
const char * hostname_randomize(const char *hostname, struct gc_arena *gc)
Definition misc.c:82
void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
Definition mtu.c:42
void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af)
Definition mtu.c:225
#define CLEAR(x)
Definition basic.h:33
#define SIZE(x)
Definition basic.h:30
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition error.c:812
#define M_FATAL
Definition error.h:89
#define M_NONFATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:148
#define M_ERR
Definition error.h:105
#define openvpn_errno()
Definition error.h:72
#define msg(flags,...)
Definition error.h:144
#define ASSERT(x)
Definition error.h:195
#define M_WARN
Definition error.h:91
#define M_MSG_VIRT_OUT
Definition error.h:99
#define M_ERRNO
Definition error.h:94
#define CM_CHILD_TCP
Definition openvpn.h:486
#define CM_CHILD_UDP
Definition openvpn.h:485
#define MODE_POINT_TO_POINT
Definition options.h:258
#define MODE_SERVER
Definition options.h:259
#define streq(x, y)
Definition options.h:725
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:929
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition plugin.c:932
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
Definition plugin.h:202
bool establish_http_proxy_passthru(struct http_proxy_info *p, socket_descriptor_t sd, const char *host, const char *port, struct event_timeout *server_poll_timeout, struct buffer *lookahead, struct signal_info *sig_info)
Definition proxy.c:644
static int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook)
Will run a script and return the exit code of the script if between 0 and 255, -1 otherwise.
Definition run_command.h:87
void throw_signal_soft(const int signum, const char *signal_text)
Throw a soft global signal.
Definition sig.c:206
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition sig.c:266
void throw_signal(const int signum)
Throw a hard signal.
Definition sig.c:177
struct signal_info siginfo_static
Definition sig.c:45
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
Definition sig.c:231
#define SIG_SOURCE_HARD
Definition sig.h:31
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition sig.h:110
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
Definition socket.c:1877
static void resolve_bind_local(struct link_socket *sock, const sa_family_t af)
Definition socket.c:1690
static int socket_get_sndbuf(socket_descriptor_t sd)
Definition socket.c:870
static void socket_set_sndbuf(socket_descriptor_t sd, int size)
Definition socket.c:887
static socket_descriptor_t socket_listen_accept(socket_descriptor_t sd, struct link_socket_actual *act, const char *remote_dynamic, const struct addrinfo *local, bool do_listen, bool nowait, volatile int *signal_received)
Definition socket.c:1324
static bool dns_addr_safe(const char *addr)
Definition socket.c:792
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:2244
int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
Definition socket.c:3701
static void ipchange_fmt(const bool include_cmd, struct argv *argv, const struct link_socket_info *info, struct gc_arena *gc)
Definition socket.c:2425
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
Definition socket.c:3196
static int socket_get_last_error(const struct link_socket *sock)
Definition socket.c:3573
bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int *netbits, int msglevel)
Translate an IPv6 addr or hostname from string form to in6_addr.
Definition socket.c:226
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:3471
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
Definition socket.c:1020
static socket_descriptor_t create_socket_udp(struct addrinfo *addrinfo, const unsigned int flags)
Definition socket.c:1068
static void phase2_tcp_server(struct link_socket *sock, const char *remote_dynamic, struct signal_info *sig_info)
Definition socket.c:2069
static void create_socket(struct link_socket *sock, struct addrinfo *addr)
Definition socket.c:1143
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:2562
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:1566
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
Definition socket.c:453
static bool socket_set_rcvbuf(socket_descriptor_t sd, int size)
Definition socket.c:915
static void stream_buf_set_next(struct stream_buf *sb)
Definition socket.c:2663
const char * socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:2595
void bad_address_length(int actual, int expected)
Definition socket.c:3285
bool mac_addr_safe(const char *mac_addr)
Definition socket.c:823
void setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t addr, const unsigned int flags)
Definition socket.c:3101
const char * print_sockaddr_ex(const struct sockaddr *sa, const char *separator, const unsigned int flags, struct gc_arena *gc)
Definition socket.c:2812
static bool stream_buf_added(struct stream_buf *sb, int length_added)
Definition socket.c:2716
event_t socket_listen_event_handle(struct link_socket *s)
Definition socket.c:2794
void sd_close(socket_descriptor_t *sd)
Definition socket.c:4037
const char * print_in_port_t(in_port_t port, struct gc_arena *gc)
Definition socket.c:3010
static int get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname, void *network, unsigned int *netbits, int resolve_retry_seconds, struct signal_info *sig_info, int msglevel)
Definition socket.c:81
static void linksock_print_addr(struct link_socket *sock)
Definition socket.c:2027
const char * proto2ascii_all(struct gc_arena *gc)
Definition socket.c:3217
void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, const struct link_socket_actual *act, const unsigned int flags)
Definition socket.c:3130
static void socket_set_mark(socket_descriptor_t sd, int mark)
Definition socket.c:981
void setenv_in6_addr(struct env_set *es, const char *name_prefix, const struct in6_addr *addr, const unsigned int flags)
Definition socket.c:3114
static void stream_buf_close(struct stream_buf *sb)
Definition socket.c:2783
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
Definition socket.c:2893
static void stream_buf_get_final(struct stream_buf *sb, struct buffer *buf)
Definition socket.c:2678
static void socket_connect(socket_descriptor_t *sd, const struct sockaddr *dest, const int connect_timeout, struct signal_info *sig_info)
Definition socket.c:1589
static void bind_local(struct link_socket *sock, const sa_family_t ai_family)
Definition socket.c:1123
bool stream_buf_read_setup_dowork(struct link_socket *sock)
Definition socket.c:2696
static void phase2_socks_client(struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:2157
static bool socket_set_tcp_nodelay(socket_descriptor_t sd, int state)
Definition socket.c:961
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:1251
static void socket_do_listen(socket_descriptor_t sd, const struct addrinfo *local, bool do_listen, bool do_set_nonblock)
Definition socket.c:1224
void setenv_sockaddr(struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const unsigned int flags)
Definition socket.c:3048
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:3584
void link_socket_close(struct link_socket *sock)
Definition socket.c:2367
void link_socket_connection_initiated(struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
Definition socket.c:2441
const char * print_link_socket_actual_ex(const struct link_socket_actual *act, const char *separator, const unsigned int flags, struct gc_arena *gc)
Definition socket.c:2903
void socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs, bool reduce_size)
Sets the receive and send buffer sizes of a socket descriptor.
Definition socket.c:928
struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add)
Definition socket.c:3025
static bool streqnull(const char *a, const char *b)
Definition socket.c:239
static void phase2_set_socket_flags(struct link_socket *sock)
Definition socket.c:2008
static void resolve_remote(struct link_socket *sock, int phase, const char **remote_dynamic, struct signal_info *sig_info)
Definition socket.c:1736
sa_family_t ascii2af(const char *proto_name)
Definition socket.c:3183
static int do_preresolve_host(struct context *c, const char *hostname, const char *servname, const int af, const int flags)
Definition socket.c:289
void link_socket_bad_outgoing_addr(void)
Definition socket.c:2521
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:3905
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:2527
int openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr)
Definition socket.c:696
static int socket_get_rcvbuf(socket_descriptor_t sd)
Definition socket.c:898
int link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
Definition socket.c:3297
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:1471
unsigned int socket_set(struct link_socket *s, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:4000
const char * proto_remote(int proto, bool remote)
Definition socket.c:3257
static unsigned int sf2gaf(const unsigned int getaddr_flags, const unsigned int sockflags)
Definition socket.c:64
static int get_cached_dns_entry(struct cached_dns_entry *dns_cache, const char *hostname, const char *servname, int ai_family, int resolve_flags, struct addrinfo **ai)
Definition socket.c:260
bool ipv6_addr_safe(const char *ipv6_text_addr)
Definition socket.c:770
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
Definition socket.c:2994
void do_preresolve(struct context *c)
Definition socket.c:343
bool ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn)
Definition socket.c:806
void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
Definition socket.c:2492
static void phase2_tcp_client(struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:2112
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix, bool ipv6only)
Definition socket.c:1417
int ascii2proto(const char *proto_name)
Definition socket.c:3170
socket_descriptor_t create_socket_tcp(struct addrinfo *addrinfo)
Definition socket.c:1036
static void socket_frame_init(const struct frame *frame, struct link_socket *sock)
Definition socket.c:1662
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Definition socket.c:2974
static void stream_buf_reset(struct stream_buf *sb)
Definition socket.c:2632
static void stream_buf_get_next(struct stream_buf *sb, struct buffer *buf)
Definition socket.c:2687
static void create_socket_dco_win(struct context *c, struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:2196
static void tcp_connection_established(const struct link_socket_actual *act)
Definition socket.c:1315
static bool socket_set_flags(socket_descriptor_t sd, unsigned int sockflags)
Definition socket.c:992
struct link_socket * link_socket_new(void)
Definition socket.c:1863
static int read_sockaddr_from_packet(struct buffer *buf, struct sockaddr *dst)
Extracts a sockaddr from a packet payload.
Definition socket.c:3869
bool sockets_read_residual(const struct context *c)
Definition socket.c:46
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
Definition socket.c:195
#define UINT8_MAX
Definition socket.c:3018
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:2419
#define IF_NAMESIZE
Definition socket.c:2899
const char * addr_family_name(int af)
Definition socket.c:3233
void read_sockaddr_from_overlapped(struct overlapped_io *io, struct sockaddr *dst, int overlapped_ret)
Definition socket.c:3819
bool link_socket_update_flags(struct link_socket *sock, unsigned int sockflags)
Definition socket.c:1006
bool ip_addr_dotted_quad_safe(const char *dotted_quad)
Definition socket.c:720
static void stream_buf_init(struct stream_buf *sb, struct buffer *buf, const unsigned int sockflags, const int proto)
Definition socket.c:2642
#define IA_EMPTY_IF_UNDEF
Definition socket.h:401
#define OIA_HOSTNAME
Definition socket.h:469
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:1259
#define IPV4_INVALID_ADDR
Definition socket.h:438
static const char * print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc)
Definition socket.h:384
static BOOL SocketHandleGetOverlappedResult(sockethandle_t sh, struct overlapped_io *io)
Definition socket.h:300
#define GETADDR_CACHE_MASK
Definition socket.h:530
static bool link_socket_actual_defined(const struct link_socket_actual *act)
Definition socket.h:726
#define LS_MODE_TCP_ACCEPT_FROM
Definition socket.h:211
#define GETADDR_MSG_VIRT_OUT
Definition socket.h:523
#define GETADDR_TRY_ONCE
Definition socket.h:524
#define SA_IP_PORT
Definition socket.h:411
#define GETADDR_PASSIVE
Definition socket.h:527
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
Definition socket.h:586
#define GETADDR_FATAL
Definition socket.h:518
#define GETADDR_UPDATE_MANAGEMENT_STATE
Definition socket.h:525
#define SF_DCO_WIN
Definition socket.h:226
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition socket.h:648
static bool addr_local(const struct sockaddr *addr)
Definition socket.h:678
static bool stream_buf_read_setup(struct link_socket *sock)
Definition socket.h:1009
#define PS_SHOW_PORT
Definition socket.h:364
@ PROTO_NONE
Definition socket.h:567
@ PROTO_UDP
Definition socket.h:568
@ PROTO_TCP
Definition socket.h:569
@ PROTO_TCP_CLIENT
Definition socket.h:571
@ PROTO_N
Definition socket.h:572
@ PROTO_TCP_SERVER
Definition socket.h:570
#define PS_DONT_SHOW_ADDR
Definition socket.h:366
#define GETADDR_HOST_ORDER
Definition socket.h:519
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
Definition socket.h:314
#define OIA_ERROR
Definition socket.h:471
static int SocketHandleGetLastError(sockethandle_t sh)
Definition socket.h:308
static void SocketHandleSetInvalError(sockethandle_t sh)
Definition socket.h:320
#define PS_SHOW_PORT_IF_DEFINED
Definition socket.h:363
#define SF_TCP_NODELAY
Definition socket.h:222
#define GETADDR_RANDOMIZE
Definition socket.h:526
#define RESOLV_RETRY_INFINITE
Definition socket.h:48
#define GETADDR_DATAGRAM
Definition socket.h:528
static bool proto_is_tcp(int proto)
returns if the proto is a TCP variant (tcp-server, tcp-client or tcp)
Definition socket.h:606
#define GETADDR_FATAL_ON_SIGNAL
Definition socket.h:521
static void addr_zero_host(struct openvpn_sockaddr *addr)
Definition socket.h:849
static bool addr_defined_ipi(const struct link_socket_actual *lsa)
Definition socket.h:699
#define SF_USE_IP_PKTINFO
Definition socket.h:221
#define LS_MODE_DEFAULT
Definition socket.h:209
#define OIA_IP
Definition socket.h:470
#define MSG_NOSIGNAL
Definition socket.h:272
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP)
Definition socket.h:597
uint16_t packet_size_type
Definition socket.h:56
static bool socket_is_dco_win(const struct link_socket *s)
Returns true if we are on Windows and this link is running on DCO-WIN.
Definition socket.h:1027
static int af_addr_size(sa_family_t af)
Definition socket.h:864
#define SF_HOST_RANDOMIZE
Definition socket.h:224
#define SF_GETADDRINFO_DGRAM
Definition socket.h:225
#define LS_MODE_TCP_LISTEN
Definition socket.h:210
#define SF_PORT_SHARE
Definition socket.h:223
#define GETADDR_RESOLVE
Definition socket.h:517
#define ntohps(x)
Definition socket.h:62
#define PS_DONT_SHOW_FAMILY
Definition socket.h:367
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:1107
#define IA_NET_ORDER
Definition socket.h:402
#define SA_SET_IF_NONZERO
Definition socket.h:412
#define openvpn_close_socket(s)
Definition socket.h:277
#define GETADDR_MENTION_RESOLVE_RETRY
Definition socket.h:520
#define htonps(x)
Definition socket.h:59
#define GETADDR_WARN_ON_SIGNAL
Definition socket.h:522
static bool addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *addrlist)
Definition socket.h:747
#define PS_SHOW_PKTINFO
Definition socket.h:365
void establish_socks_proxy_passthru(struct socks_proxy_info *p, socket_descriptor_t sd, const char *host, const char *servname, struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Definition socks.c:455
void establish_socks_proxy_udpassoc(struct socks_proxy_info *p, socket_descriptor_t ctrl_sd, struct openvpn_sockaddr *relay_addr, struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Definition socks.c:517
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:64
Definition socket.h:76
const char * hostname
Definition socket.h:77
int ai_family
Definition socket.h:79
const char * servname
Definition socket.h:78
int flags
Definition socket.h:80
struct addrinfo * ai
Definition socket.h:81
struct cached_dns_entry * next
Definition socket.h:82
Definition options.h:105
struct local_list * local_list
Definition options.h:106
bool bind_local
Definition options.h:116
const char * remote
Definition options.h:112
const char * socks_proxy_port
Definition options.h:122
struct http_proxy_options * http_proxy_options
Definition options.h:120
bool bind_ipv6_only
Definition options.h:115
bool remote_float
Definition options.h:113
const char * remote_port
Definition options.h:111
const char * socks_proxy_server
Definition options.h:121
int mtu_discover_type
Definition options.h:137
int proto
Definition options.h:107
sa_family_t af
Definition options.h:108
unsigned int flags
Definition options.h:159
struct connection_entry ** array
Definition options.h:200
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
Definition openvpn.h:158
int link_sockets_num
Definition openvpn.h:157
struct http_proxy_info * http_proxy
Definition openvpn.h:188
struct socks_proxy_info * socks_proxy
Definition openvpn.h:192
struct cached_dns_entry * dns_cache
Definition openvpn.h:166
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:171
struct event_timeout server_poll_interval
Definition openvpn.h:408
const struct link_socket * accept_from
Definition openvpn.h:242
struct frame frame
Definition openvpn.h:248
struct link_socket ** link_sockets
Definition openvpn.h:237
Contains all state information for one tunnel.
Definition openvpn.h:474
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:487
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:500
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:502
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:475
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:492
struct context_1 c1
Level 1 context.
Definition openvpn.h:513
struct link_socket * sock
Definition event.h:146
union event_arg::@1 u
event_arg_t type
Definition event.h:143
Packet geometry parameters.
Definition mtu.h:98
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
struct http_proxy_options options
Definition proxy.h:67
const char * port
Definition proxy.h:46
const char * server
Definition proxy.h:45
Definition options.h:98
const char * port
Definition options.h:100
int proto
Definition options.h:101
const char * local
Definition options.h:99
struct local_entry * array[CONNECTION_LIST_SIZE]
Definition options.h:192
struct man_connection connection
Definition manage.h:339
union openvpn_sockaddr::@20 addr
struct sockaddr sa
Definition socket.h:69
struct sockaddr_in in4
Definition socket.h:70
struct sockaddr_in6 in6
Definition socket.h:71
int resolve_retry_seconds
Definition options.h:365
int rcvbuf
Definition options.h:414
const char * ip_remote_hint
Definition options.h:367
struct connection_entry ce
Definition options.h:288
const char * ipchange
Definition options.h:315
int mode
Definition options.h:260
char * bind_dev
Definition options.h:419
int sndbuf
Definition options.h:415
int mark
Definition options.h:418
unsigned int sockflags
Definition options.h:422
const char * dev_node
Definition options.h:318
DWORD flags
Definition win32.h:209
struct buffer buf
Definition win32.h:218
DWORD size
Definition win32.h:208
OVERLAPPED overlapped
Definition win32.h:207
struct buffer buf_init
Definition win32.h:217
int addrlen
Definition win32.h:216
bool addr_defined
Definition win32.h:211
int iostate
Definition win32.h:206
struct sockaddr_in6 addr6
Definition win32.h:214
struct sockaddr_in addr
Definition win32.h:213
const char * short_form
Definition socket.c:3143
const char * display_form
Definition socket.c:3144
sa_family_t proto_af
Definition socket.c:3145
HANDLE write
Definition win32.h:81
HANDLE read
Definition win32.h:80
const char * signal_text
Definition sig.h:45
volatile int signal_received
Definition sig.h:43
volatile int source
Definition sig.h:44
bool is_handle
Definition socket.h:290
bool prepend_sa
Definition socket.h:291
char server[128]
Definition socks.h:40
const char * port
Definition socks.h:41
struct buffer buf
Definition socket.h:137
struct buffer residual
Definition socket.h:133
bool residual_fully_formed
Definition socket.h:135
int maxlen
Definition socket.h:134
Definition tun.h:181
enum tun_driver_type backend_driver
The backend driver that used for this tun/tap device.
Definition tun.h:191
HANDLE hand
Definition tun.h:216
unsigned short sa_family_t
Definition syshead.h:395
#define SOCKET_UNDEFINED
Definition syshead.h:437
#define SOL_IP
Definition syshead.h:388
SOCKET socket_descriptor_t
Definition syshead.h:439
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:447
#define ENABLE_IP_PKTINFO
Definition syshead.h:380
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:155
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
Definition tun.c:6565
@ DRIVER_DCO
Definition tun.h:55
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:223
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition win32.c:171
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:277
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition win32.c:202
void overlapped_io_close(struct overlapped_io *o)
Definition win32.c:189
static bool defined_net_event_win32(const struct rw_handle *event)
Definition win32.h:92
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:205
#define IOSTATE_INITIAL
Definition win32.h:203
#define IOSTATE_QUEUED
Definition win32.h:204