OpenVPN
socket.h
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#ifndef SOCKET_H
25#define SOCKET_H
26
27#include "buffer.h"
28#include "common.h"
29#include "error.h"
30#include "proto.h"
31#include "mtu.h"
32#include "win32.h"
33#include "event.h"
34#include "proxy.h"
35#include "socks.h"
36#include "misc.h"
37#include "tun.h"
38
39/*
40 * OpenVPN's default port number as assigned by IANA.
41 */
42#define OPENVPN_PORT "1194"
43
44/*
45 * Number of seconds that "resolv-retry infinite"
46 * represents.
47 */
48#define RESOLV_RETRY_INFINITE 1000000000
49
50/*
51 * packet_size_type is used to communicate packet size
52 * over the wire when stream oriented protocols are
53 * being used
54 */
55
56typedef uint16_t packet_size_type;
57
58/* convert a packet_size_type from host to network order */
59#define htonps(x) htons(x)
60
61/* convert a packet_size_type from network to host order */
62#define ntohps(x) ntohs(x)
63
64/* OpenVPN sockaddr struct */
66{
67 /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
68 union {
69 struct sockaddr sa;
70 struct sockaddr_in in4;
71 struct sockaddr_in6 in6;
73};
74
75/* struct to hold preresolved host names */
77 const char *hostname;
78 const char *servname;
80 int flags;
81 struct addrinfo *ai;
83};
84
85/* actual address of remote, based on source address of received packets */
87{
88 /*int dummy;*/ /* add offset to force a bug if dest not explicitly dereferenced */
89
91#if ENABLE_IP_PKTINFO
92 union {
93#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
94 struct in_pktinfo in4;
95#elif defined(IP_RECVDSTADDR)
96 struct in_addr in4;
97#endif
98 struct in6_pktinfo in6;
99 } pi;
100#endif
101};
102
103/* IP addresses which are persistent across SIGUSR1s */
105{
106 struct addrinfo *bind_local;
107 struct addrinfo *remote_list; /* complete remote list */
108 struct addrinfo *current_remote; /* remote used in the
109 * current connection attempt */
110 struct link_socket_actual actual; /* reply to this address */
111};
112
114{
117 const char *ipchange_command;
118 const struct plugin_list *plugins;
120 int proto; /* Protocol (PROTO_x defined below) */
121 sa_family_t af; /* Address family like AF_INET, AF_INET6 or AF_UNSPEC*/
123 int mtu_changed; /* Set to true when mtu value is changed */
124};
125
126/*
127 * Used to extract packets encapsulated in streams into a buffer,
128 * in this case IP packets embedded in a TCP stream.
129 */
131{
136
137 struct buffer buf;
138 struct buffer next;
139 int len; /* -1 if not yet known */
140
141 bool error; /* if true, fatal TCP error has occurred,
142 * requiring that connection be restarted */
143#if PORT_SHARE
144#define PS_DISABLED 0
145#define PS_ENABLED 1
146#define PS_FOREIGN 2
148#endif
149};
150
151/*
152 * Used to set socket buffer sizes
153 */
155{
158};
159
167void
169 const struct socket_buffer_size *sbs,
170 bool reduce_size);
171
172/*
173 * This is the main socket structure used by OpenVPN. The SOCKET_
174 * defines try to abstract away our implementation differences between
175 * using sockets on Posix vs. Win32.
176 */
178{
180
187 socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */
188
189#ifdef _WIN32
193 struct rw_handle listen_handle; /* For listening on TCP socket in server mode */
194#endif
195
196 /* used for printing status info only */
197 unsigned int rwflags_debug;
198
199 /* used for long-term queueing of pre-accepted socket listen */
201
202 const char *remote_host;
203 const char *remote_port;
204 const char *local_host;
205 const char *local_port;
208
209#define LS_MODE_DEFAULT 0
210#define LS_MODE_TCP_LISTEN 1
211#define LS_MODE_TCP_ACCEPT_FROM 2
212 int mode;
213
216
218
219 int mtu; /* OS discovered MTU, or 0 if unknown */
220
221#define SF_USE_IP_PKTINFO (1<<0)
222#define SF_TCP_NODELAY (1<<1)
223#define SF_PORT_SHARE (1<<2)
224#define SF_HOST_RANDOMIZE (1<<3)
225#define SF_GETADDRINFO_DGRAM (1<<4)
226#define SF_DCO_WIN (1<<5)
227#define SF_PREPEND_SA (1<<6)
228 unsigned int sockflags;
229 int mark;
230 const char *bind_dev;
231
232 /* for stream sockets */
236
237 /* HTTP proxy */
239
240 /* Socks proxy */
242 struct link_socket_actual socks_relay; /* Socks UDP relay address */
243
244 /* The OpenVPN server we will use the proxy to connect to */
245 const char *proxy_dest_host;
246 const char *proxy_dest_port;
247
248 /* Pointer to the server-poll to trigger the timeout in function which have
249 * their own loop instead of using the main oop */
251
252#if PASSTOS_CAPABILITY
253 /* used to get/set TOS. */
254#if defined(TARGET_LINUX)
255 uint8_t ptos;
256#else /* all the BSDs, Solaris, MacOS use plain "int" -> see "man ip" there */
257 int ptos;
258#endif
259 bool ptos_defined;
260#endif
261
262#ifdef ENABLE_DEBUG
263 int gremlin; /* --gremlin bits */
264#endif
265};
266
267/*
268 * Some Posix/Win32 differences.
269 */
270
271#ifndef MSG_NOSIGNAL
272#define MSG_NOSIGNAL 0
273#endif
274
275#ifdef _WIN32
276
277#define openvpn_close_socket(s) closesocket(s)
278
279int socket_recv_queue(struct link_socket *sock, int maxsize);
280
281int socket_send_queue(struct link_socket *sock,
282 struct buffer *buf,
283 const struct link_socket_actual *to);
284
285typedef struct {
286 union {
287 SOCKET s;
288 HANDLE h;
289 };
291 bool prepend_sa; /* are incoming packets prepended with sockaddr? */
293
295 struct overlapped_io *io,
296 struct buffer *buf,
297 struct link_socket_actual *from);
298
299static inline BOOL
301{
302 return sh.is_handle ?
303 GetOverlappedResult(sh.h, &io->overlapped, &io->size, FALSE) :
304 WSAGetOverlappedResult(sh.s, &io->overlapped, &io->size, FALSE, &io->flags);
305}
306
307static inline int
309{
310 return sh.is_handle ? (int)GetLastError() : WSAGetLastError();
311}
312
313inline static void
315{
316 sh.is_handle ? SetLastError(err) : WSASetLastError(err);
317}
318
319static inline void
321{
322 sh.is_handle ? SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL);
323}
324
325#else /* ifdef _WIN32 */
326
327#define openvpn_close_socket(s) close(s)
328
329#endif /* ifdef _WIN32 */
330
331struct link_socket *link_socket_new(void);
332
334 struct addrinfo *local,
335 int af_family,
336 const char *prefix,
337 bool ipv6only);
338
340 const struct sockaddr *remote,
341 int connect_timeout,
342 volatile int *signal_received);
343
344
345
346/*
347 * Initialize link_socket object.
348 */
349void
351 int sock_index,
352 int mode);
353
354void link_socket_init_phase2(struct context *c,
355 struct link_socket *sock);
356
357void do_preresolve(struct context *c);
358
359void link_socket_close(struct link_socket *sock);
360
362
363#define PS_SHOW_PORT_IF_DEFINED (1<<0)
364#define PS_SHOW_PORT (1<<1)
365#define PS_SHOW_PKTINFO (1<<2)
366#define PS_DONT_SHOW_ADDR (1<<3)
367#define PS_DONT_SHOW_FAMILY (1<<4)
368
369const char *print_sockaddr_ex(const struct sockaddr *addr,
370 const char *separator,
371 const unsigned int flags,
372 struct gc_arena *gc);
373
374static inline
375const char *
377 struct gc_arena *gc)
378{
379 return print_sockaddr_ex(&addr->addr.sa, ":", PS_SHOW_PORT, gc);
380}
381
382static inline
383const char *
384print_sockaddr(const struct sockaddr *addr,
385 struct gc_arena *gc)
386{
387 return print_sockaddr_ex(addr, ":", PS_SHOW_PORT, gc);
388}
389
390
391
392const char *print_link_socket_actual_ex(const struct link_socket_actual *act,
393 const char *separator,
394 const unsigned int flags,
395 struct gc_arena *gc);
396
397const char *print_link_socket_actual(const struct link_socket_actual *act,
398 struct gc_arena *gc);
399
400
401#define IA_EMPTY_IF_UNDEF (1<<0)
402#define IA_NET_ORDER (1<<1)
403const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc);
404
405const char *print_in6_addr(struct in6_addr addr6, unsigned int flags, struct gc_arena *gc);
406
407const char *print_in_port_t(in_port_t port, struct gc_arena *gc);
408
409struct in6_addr add_in6_addr( struct in6_addr base, uint32_t add );
410
411#define SA_IP_PORT (1<<0)
412#define SA_SET_IF_NONZERO (1<<1)
413void setenv_sockaddr(struct env_set *es,
414 const char *name_prefix,
415 const struct openvpn_sockaddr *addr,
416 const unsigned int flags);
417
418void setenv_in_addr_t(struct env_set *es,
419 const char *name_prefix,
420 in_addr_t addr,
421 const unsigned int flags);
422
423void setenv_in6_addr(struct env_set *es,
424 const char *name_prefix,
425 const struct in6_addr *addr,
426 const unsigned int flags);
427
429 const char *name_prefix,
430 const struct link_socket_actual *act,
431 const unsigned int flags);
432
433void bad_address_length(int actual, int expected);
434
435/* IPV4_INVALID_ADDR: returned by link_socket_current_remote()
436 * to ease redirect-gateway logic for ipv4 tunnels on ipv6 endpoints
437 */
438#define IPV4_INVALID_ADDR 0xffffffff
439in_addr_t link_socket_current_remote(const struct link_socket_info *info);
440
441const struct in6_addr *link_socket_current_remote_ipv6
442 (const struct link_socket_info *info);
443
445 const struct link_socket_actual *addr,
446 const char *common_name,
447 struct env_set *es);
448
450 const struct link_socket_info *info,
451 const struct link_socket_actual *from_addr);
452
453void set_actual_address(struct link_socket_actual *actual,
454 struct addrinfo *ai);
455
457
458void setenv_trusted(struct env_set *es, const struct link_socket_info *info);
459
460bool link_socket_update_flags(struct link_socket *sock, unsigned int sockflags);
461
462void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf);
463
464/*
465 * Low-level functions
466 */
467
468/* return values of openvpn_inet_aton */
469#define OIA_HOSTNAME 0
470#define OIA_IP 1
471#define OIA_ERROR -1
472int openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr);
473
474/* integrity validation on pulled options */
475bool ip_addr_dotted_quad_safe(const char *dotted_quad);
476
477bool ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn);
478
479bool mac_addr_safe(const char *mac_addr);
480
481bool ipv6_addr_safe(const char *ipv6_text_addr);
482
483socket_descriptor_t create_socket_tcp(struct addrinfo *);
484
486 struct link_socket_actual *act,
487 const bool nowait);
488
489#if UNIX_SOCK_SUPPORT
490
491socket_descriptor_t create_socket_unix(void);
492
493void socket_bind_unix(socket_descriptor_t sd,
494 struct sockaddr_un *local,
495 const char *prefix);
496
497socket_descriptor_t socket_accept_unix(socket_descriptor_t sd,
498 struct sockaddr_un *remote);
499
500int socket_connect_unix(socket_descriptor_t sd,
501 struct sockaddr_un *remote);
502
503void sockaddr_unix_init(struct sockaddr_un *local, const char *path);
504
505const char *sockaddr_unix_name(const struct sockaddr_un *local, const char *null);
506
507void socket_delete_unix(const struct sockaddr_un *local);
508
509bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, int *uid, int *gid);
510
511#endif /* if UNIX_SOCK_SUPPORT */
512
513/*
514 * DNS resolution
515 */
516
517#define GETADDR_RESOLVE (1<<0)
518#define GETADDR_FATAL (1<<1)
519#define GETADDR_HOST_ORDER (1<<2)
520#define GETADDR_MENTION_RESOLVE_RETRY (1<<3)
521#define GETADDR_FATAL_ON_SIGNAL (1<<4)
522#define GETADDR_WARN_ON_SIGNAL (1<<5)
523#define GETADDR_MSG_VIRT_OUT (1<<6)
524#define GETADDR_TRY_ONCE (1<<7)
525#define GETADDR_UPDATE_MANAGEMENT_STATE (1<<8)
526#define GETADDR_RANDOMIZE (1<<9)
527#define GETADDR_PASSIVE (1<<10)
528#define GETADDR_DATAGRAM (1<<11)
529
530#define GETADDR_CACHE_MASK (GETADDR_DATAGRAM|GETADDR_PASSIVE)
531
538in_addr_t getaddr(unsigned int flags,
539 const char *hostname,
540 int resolve_retry_seconds,
541 bool *succeeded,
542 struct signal_info *sig_info);
543
547bool get_ipv6_addr(const char *hostname, struct in6_addr *network,
548 unsigned int *netbits, int msglevel);
549
550int openvpn_getaddrinfo(unsigned int flags,
551 const char *hostname,
552 const char *servname,
553 int resolve_retry_seconds,
554 struct signal_info *sig_info,
555 int ai_family,
556 struct addrinfo **res);
557
558/*
559 * Transport protocol naming and other details.
560 */
561
562/*
563 * Use enum's instead of #define to allow for easier
564 * optional proto support
565 */
574
575static inline bool
576proto_is_net(int proto)
577{
578 ASSERT(proto >= 0 && proto < PROTO_N);
579 return proto != PROTO_NONE;
580}
581
585static inline bool
586proto_is_udp(int proto)
587{
588 ASSERT(proto >= 0 && proto < PROTO_N);
589 return proto == PROTO_UDP;
590}
591
596static inline bool
598{
599 return proto_is_udp(proto);
600}
601
605static inline bool
606proto_is_tcp(int proto)
607{
608 ASSERT(proto >= 0 && proto < PROTO_N);
609 return proto == PROTO_TCP_CLIENT || proto == PROTO_TCP_SERVER;
610}
611
612
613int ascii2proto(const char *proto_name);
614
615sa_family_t ascii2af(const char *proto_name);
616
617const char *proto2ascii(int proto, sa_family_t af, bool display_form);
618
619const char *proto2ascii_all(struct gc_arena *gc);
620
621const char *proto_remote(int proto, bool remote);
622
623const char *addr_family_name(int af);
624
625/*
626 * Overhead added to packets by various protocols.
627 */
628static inline int
630{
631 int overhead = 0;
632 overhead += (proto == PROTO_UDP) ? 8 : 20;
633 overhead += (af == AF_INET) ? 20 : 40;
634 return overhead;
635}
636
637/*
638 * Misc inline functions
639 */
640
641static inline bool
643{
644 return !proto_is_dgram(proto);
645}
646
647static inline bool
649{
650 if (sock)
651 {
653 }
654 else
655 {
656 return false;
657 }
658}
659
660static inline bool
662{
663 if (!addr)
664 {
665 return 0;
666 }
667 switch (addr->addr.sa.sa_family)
668 {
669 case AF_INET: return addr->addr.in4.sin_addr.s_addr != 0;
670
671 case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr);
672
673 default: return 0;
674 }
675}
676
677static inline bool
678addr_local(const struct sockaddr *addr)
679{
680 if (!addr)
681 {
682 return false;
683 }
684 switch (addr->sa_family)
685 {
686 case AF_INET:
687 return ((const struct sockaddr_in *)addr)->sin_addr.s_addr == htonl(INADDR_LOOPBACK);
688
689 case AF_INET6:
690 return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 *)addr)->sin6_addr);
691
692 default:
693 return false;
694 }
695}
696
697
698static inline bool
700{
701#if ENABLE_IP_PKTINFO
702 if (!lsa)
703 {
704 return 0;
705 }
706 switch (lsa->dest.addr.sa.sa_family)
707 {
708#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
709 case AF_INET: return lsa->pi.in4.ipi_spec_dst.s_addr != 0;
710
711#elif defined(IP_RECVDSTADDR)
712 case AF_INET: return lsa->pi.in4.s_addr != 0;
713
714#endif
715 case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
716
717 default: return 0;
718 }
719#else /* if ENABLE_IP_PKTINFO */
720 ASSERT(0);
721#endif
722 return false;
723}
724
725static inline bool
727{
728 return act && addr_defined(&act->dest);
729}
730
731static inline bool
732addr_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
733{
734 switch (a1->addr.sa.sa_family)
735 {
736 case AF_INET:
737 return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr;
738
739 case AF_INET6:
740 return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr);
741 }
742 ASSERT(0);
743 return false;
744}
745
746static inline bool
747addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *addrlist)
748{
749 const struct addrinfo *curele;
750 for (curele = addrlist; curele; curele = curele->ai_next)
751 {
752 switch (a1->addr.sa.sa_family)
753 {
754 case AF_INET:
755 if (a1->addr.in4.sin_addr.s_addr == ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr)
756 {
757 return true;
758 }
759 break;
760
761 case AF_INET6:
762 if (IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &((struct sockaddr_in6 *) curele->ai_addr)->sin6_addr))
763 {
764 return true;
765 }
766 break;
767
768 default:
769 ASSERT(0);
770 }
771 }
772 return false;
773}
774
775static inline bool
776addrlist_port_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a2)
777{
778 const struct addrinfo *curele;
779 for (curele = a2; curele; curele = curele->ai_next)
780 {
781 switch (a1->addr.sa.sa_family)
782 {
783 case AF_INET:
784 if (curele->ai_family == AF_INET
785 && a1->addr.in4.sin_addr.s_addr == ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr
786 && a1->addr.in4.sin_port == ((struct sockaddr_in *)curele->ai_addr)->sin_port)
787 {
788 return true;
789 }
790 break;
791
792 case AF_INET6:
793 if (curele->ai_family == AF_INET6
794 && IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &((struct sockaddr_in6 *) curele->ai_addr)->sin6_addr)
795 && a1->addr.in6.sin6_port == ((struct sockaddr_in6 *) curele->ai_addr)->sin6_port)
796 {
797 return true;
798 }
799 break;
800
801 default:
802 ASSERT(0);
803 }
804 }
805 return false;
806}
807
808
809
810static inline bool
811addr_port_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
812{
813 switch (a1->addr.sa.sa_family)
814 {
815 case AF_INET:
816 return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr
817 && a1->addr.in4.sin_port == a2->addr.in4.sin_port;
818
819 case AF_INET6:
820 return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr)
821 && a1->addr.in6.sin6_port == a2->addr.in6.sin6_port;
822 }
823 ASSERT(0);
824 return false;
825}
826
827static inline bool
829 const struct openvpn_sockaddr *a2,
830 const int proto)
831{
833 ? addr_match(a1, a2)
834 : addr_port_match(a1, a2);
835}
836
837
838static inline bool
840 struct addrinfo *addr_list,
841 const int proto)
842{
844 ? addrlist_match(a1, addr_list)
845 : addrlist_port_match(a1, addr_list);
846}
847
848static inline void
850{
851 switch (addr->addr.sa.sa_family)
852 {
853 case AF_INET:
854 addr->addr.in4.sin_addr.s_addr = 0;
855 break;
856
857 case AF_INET6:
858 memset(&addr->addr.in6.sin6_addr, 0, sizeof(struct in6_addr));
859 break;
860 }
861}
862
863static inline int
865{
866 switch (af)
867 {
868 case AF_INET: return sizeof(struct sockaddr_in);
869
870 case AF_INET6: return sizeof(struct sockaddr_in6);
871
872 default:
873#if 0
874 /* could be called from socket_do_accept() with empty addr */
875 msg(M_ERR, "Bad address family: %d\n", af);
876 ASSERT(0);
877#endif
878 return 0;
879 }
880}
881
882static inline bool
884{
885 return addr_port_match(&a1->dest, &a2->dest);
886}
887
888#if PORT_SHARE
889
890static inline bool
891socket_foreign_protocol_detected(const struct link_socket *sock)
892{
894 && sock->stream_buf.port_share_state == PS_FOREIGN;
895}
896
897static inline const struct buffer *
899{
900 return &sock->stream_buf.buf;
901}
902
903static inline int
904socket_foreign_protocol_sd(const struct link_socket *sock)
905{
906 return sock->sd;
907}
908
909#endif /* if PORT_SHARE */
910
911static inline bool
913{
915 {
916 if (sock->stream_reset || sock->stream_buf.error)
917 {
918 return true;
919 }
920 else if (status < 0)
921 {
922 const int err = openvpn_errno();
923#ifdef _WIN32
924 return err == WSAECONNRESET || err == WSAECONNABORTED
926#else
927 return err == ECONNRESET;
928#endif
929 }
930 }
931 return false;
932}
933
934static inline bool
936 const struct link_socket_info *info,
937 const struct link_socket_actual *from_addr)
938{
939 if (buf->len > 0)
940 {
941 switch (from_addr->dest.addr.sa.sa_family)
942 {
943 case AF_INET6:
944 case AF_INET:
946 {
947 return false;
948 }
949 if (info->remote_float || (!info->lsa->remote_list))
950 {
951 return true;
952 }
953 if (addrlist_match_proto(&from_addr->dest, info->lsa->remote_list, info->proto))
954 {
955 return true;
956 }
957 }
958 }
959 return false;
960}
961
962static inline void
964 const struct link_socket_info *info,
965 struct link_socket_actual **act)
966{
967 if (buf->len > 0)
968 {
969 struct link_socket_addr *lsa = info->lsa;
971 {
972 *act = &lsa->actual;
973 }
974 else
975 {
977 buf->len = 0;
978 *act = NULL;
979 }
980 }
981}
982
983static inline void
985 const struct link_socket_actual *act,
986 const char *common_name,
987 struct env_set *es)
988{
989 struct link_socket_addr *lsa = info->lsa;
990 if (
991 /* new or changed address? */
993 || !addr_match_proto(&act->dest, &lsa->actual.dest, info->proto)
994 )
995 &&
996 /* address undef or address == remote or --float */
997 (info->remote_float
998 || (!lsa->remote_list || addrlist_match_proto(&act->dest, lsa->remote_list, info->proto))
999 )
1000 )
1001 {
1002 link_socket_connection_initiated(info, act, common_name, es);
1003 }
1004}
1005
1006bool stream_buf_read_setup_dowork(struct link_socket *sock);
1007
1008static inline bool
1010{
1012 {
1013 return stream_buf_read_setup_dowork(sock);
1014 }
1015 else
1016 {
1017 return true;
1018 }
1019}
1020
1026static inline bool
1028{
1029 return s->sockflags & SF_DCO_WIN;
1030}
1031
1032/*
1033 * Socket Read Routines
1034 */
1035
1036int link_socket_read_tcp(struct link_socket *sock,
1037 struct buffer *buf);
1038
1039#ifdef _WIN32
1040
1041static inline int
1043 struct buffer *buf,
1044 struct link_socket_actual *from)
1045{
1046 sockethandle_t sh = { .s = sock->sd };
1047 if (socket_is_dco_win(sock))
1048 {
1049 *from = sock->info.lsa->actual;
1050 sh.is_handle = true;
1051 sh.prepend_sa = sock->sockflags & SF_PREPEND_SA;
1052 }
1053 return sockethandle_finalize(sh, &sock->reads, buf, from);
1054}
1055
1056#else /* ifdef _WIN32 */
1057
1058int link_socket_read_udp_posix(struct link_socket *sock,
1059 struct buffer *buf,
1060 struct link_socket_actual *from);
1061
1062#endif /* ifdef _WIN32 */
1063
1064/* read a TCP or UDP packet from link */
1065static inline int
1067 struct buffer *buf,
1068 struct link_socket_actual *from)
1069{
1070 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
1071 /* unified UDPv4 and UDPv6, for DCO-WIN the kernel
1072 * will strip the length header */
1073 {
1074 int res;
1075
1076#ifdef _WIN32
1077 res = link_socket_read_udp_win32(sock, buf, from);
1078#else
1079 res = link_socket_read_udp_posix(sock, buf, from);
1080#endif
1081 return res;
1082 }
1083 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
1084 {
1085 /* from address was returned by accept */
1086 from->dest = sock->info.lsa->actual.dest;
1087 return link_socket_read_tcp(sock, buf);
1088 }
1089 else
1090 {
1091 ASSERT(0);
1092 return -1; /* NOTREACHED */
1093 }
1094}
1095
1096/*
1097 * Socket Write routines
1098 */
1099
1100ssize_t link_socket_write_tcp(struct link_socket *sock,
1101 struct buffer *buf,
1102 struct link_socket_actual *to);
1103
1104#ifdef _WIN32
1105
1106static inline int
1108 struct buffer *buf,
1109 struct link_socket_actual *to)
1110{
1111 int err = 0;
1112 int status = 0;
1113 sockethandle_t sh = { .s = sock->sd, .is_handle = socket_is_dco_win(sock) };
1114 if (overlapped_io_active(&sock->writes))
1115 {
1116 status = sockethandle_finalize(sh, &sock->writes, NULL, NULL);
1117 if (status < 0)
1118 {
1119 err = SocketHandleGetLastError(sh);
1120 }
1121 }
1122
1123 /* dco-win mp requires control packets to be prepended with sockaddr */
1124 if (sock->sockflags & SF_PREPEND_SA)
1125 {
1126 if (to->dest.addr.sa.sa_family == AF_INET)
1127 {
1128 buf_write_prepend(buf, &to->dest.addr.in4, sizeof(struct sockaddr_in));
1129 }
1130 else
1131 {
1132 buf_write_prepend(buf, &to->dest.addr.in6, sizeof(struct sockaddr_in6));
1133 }
1134 }
1135
1136 socket_send_queue(sock, buf, to);
1137 if (status < 0)
1138 {
1139 SocketHandleSetLastError(sh, err);
1140 return status;
1141 }
1142 else
1143 {
1144 return BLEN(buf);
1145 }
1146}
1147
1148#else /* ifdef _WIN32 */
1149
1150ssize_t link_socket_write_udp_posix_sendmsg(struct link_socket *sock,
1151 struct buffer *buf,
1152 struct link_socket_actual *to);
1153
1154
1155static inline ssize_t
1156link_socket_write_udp_posix(struct link_socket *sock,
1157 struct buffer *buf,
1158 struct link_socket_actual *to)
1159{
1160#if ENABLE_IP_PKTINFO
1161 if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
1162 && addr_defined_ipi(to))
1163 {
1164 return link_socket_write_udp_posix_sendmsg(sock, buf, to);
1165 }
1166 else
1167#endif
1168 return sendto(sock->sd, BPTR(buf), BLEN(buf), 0,
1169 (struct sockaddr *) &to->dest.addr.sa,
1170 (socklen_t) af_addr_size(to->dest.addr.sa.sa_family));
1171}
1172
1173static inline ssize_t
1174link_socket_write_tcp_posix(struct link_socket *sock,
1175 struct buffer *buf)
1176{
1177 return send(sock->sd, BPTR(buf), BLEN(buf), MSG_NOSIGNAL);
1178}
1179
1180#endif /* ifdef _WIN32 */
1181
1182static inline ssize_t
1184 struct buffer *buf,
1185 struct link_socket_actual *to)
1186{
1187#ifdef _WIN32
1188 return link_socket_write_win32(sock, buf, to);
1189#else
1190 return link_socket_write_udp_posix(sock, buf, to);
1191#endif
1192}
1193
1194/* write a TCP or UDP packet to link */
1195static inline ssize_t
1197 struct buffer *buf,
1198 struct link_socket_actual *to)
1199{
1200 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
1201 {
1202 /* unified UDPv4, UDPv6 and DCO-WIN (driver adds length header) */
1203 return link_socket_write_udp(sock, buf, to);
1204 }
1205 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
1206 {
1207 return link_socket_write_tcp(sock, buf, to);
1208 }
1209 else
1210 {
1211 ASSERT(0);
1212 return -1; /* NOTREACHED */
1213 }
1214}
1215
1216#if PASSTOS_CAPABILITY
1217
1218/*
1219 * Extract TOS bits. Assumes that ipbuf is a valid IPv4 packet.
1220 */
1221static inline void
1222link_socket_extract_tos(struct link_socket *sock, const struct buffer *ipbuf)
1223{
1224 if (sock && ipbuf)
1225 {
1226 struct openvpn_iphdr *iph = (struct openvpn_iphdr *) BPTR(ipbuf);
1227 sock->ptos = iph->tos;
1228 sock->ptos_defined = true;
1229 }
1230}
1231
1232/*
1233 * Set socket properties to reflect TOS bits which were extracted
1234 * from tunnel packet.
1235 */
1236static inline void
1237link_socket_set_tos(struct link_socket *sock)
1238{
1239 if (sock && sock->ptos_defined)
1240 {
1241 setsockopt(sock->sd, IPPROTO_IP, IP_TOS, (const void *)&sock->ptos, sizeof(sock->ptos));
1242 }
1243}
1244
1245#endif /* if PASSTOS_CAPABILITY */
1246
1247/*
1248 * Socket I/O wait functions
1249 */
1250
1251/*
1252 * Extends the pre-existing read residual logic
1253 * to all initialized sockets, ensuring the complete
1254 * packet is read.
1255 */
1256bool sockets_read_residual(const struct context *c);
1257
1258static inline event_t
1260{
1261#ifdef _WIN32
1262 return &sock->rw_handle;
1263#else
1264 return sock->sd;
1265#endif
1266}
1267
1269
1270unsigned int
1271socket_set(struct link_socket *sock,
1272 struct event_set *es,
1273 unsigned int rwflags,
1274 void *arg,
1275 unsigned int *persistent);
1276
1277static inline void
1279 struct event_set *es,
1280 void *arg)
1281{
1282 if (sock && !sock->listen_persistent_queued)
1283 {
1285 sock->listen_persistent_queued = true;
1286 }
1287}
1288
1289static inline void
1291{
1292#ifdef _WIN32
1293 reset_net_event_win32(&sock->listen_handle, sock->sd);
1294#endif
1295}
1296
1297const char *socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc);
1298
1299#endif /* SOCKET_H */
#define BPTR(buf)
Definition buffer.h:124
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition buffer.h:680
#define BLEN(buf)
Definition buffer.h:127
#define EVENT_READ
Definition event.h:39
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:181
static SERVICE_STATUS status
Definition interactive.c:53
#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
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
Definition socket.c:1894
static bool addr_port_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
Definition socket.h:811
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:1259
const char * socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:2618
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
static bool link_socket_actual_defined(const struct link_socket_actual *act)
Definition socket.h:726
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:2267
int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
Definition socket.c:3720
static bool addr_match_proto(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2, const int proto)
Definition socket.h:828
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
Definition socket.c:3215
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
static const char * print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_arena *gc)
Definition socket.h:376
static int datagram_overhead(sa_family_t af, int proto)
Definition socket.h:629
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:3490
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
Definition socket.c:1037
event_t socket_listen_event_handle(struct link_socket *sock)
Definition socket.c:2817
static bool proto_is_net(int proto)
Definition socket.h:576
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:2585
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:1583
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:469
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
Definition socket.h:586
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:1196
static void socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
Definition socket.h:1278
#define SF_DCO_WIN
Definition socket.h:226
static bool link_socket_actual_match(const struct link_socket_actual *a1, const struct link_socket_actual *a2)
Definition socket.h:883
void bad_address_length(int actual, int expected)
Definition socket.c:3304
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
bool mac_addr_safe(const char *mac_addr)
Definition socket.c:840
static bool stream_buf_read_setup(struct link_socket *sock)
Definition socket.h:1009
#define PS_SHOW_PORT
Definition socket.h:364
void setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t addr, const unsigned int flags)
Definition socket.c:3120
proto_num
Definition socket.h:566
@ 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
void sd_close(socket_descriptor_t *sd)
Definition socket.c:4056
const char * print_in_port_t(in_port_t port, struct gc_arena *gc)
Definition socket.c:3033
const char * proto2ascii_all(struct gc_arena *gc)
Definition socket.c:3236
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:3149
static void socket_reset_listen_persistent(struct link_socket *sock)
Definition socket.h:1290
static int link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:1042
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
Definition socket.h:314
static bool socket_connection_reset(const struct link_socket *sock, int status)
Definition socket.h:912
void setenv_in6_addr(struct env_set *es, const char *name_prefix, const struct in6_addr *addr, const unsigned int flags)
Definition socket.c:3133
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
Definition socket.c:2916
socket_descriptor_t create_socket_tcp(struct addrinfo *)
Definition socket.c:1053
static int SocketHandleGetLastError(sockethandle_t sh)
Definition socket.h:308
static void SocketHandleSetInvalError(sockethandle_t sh)
Definition socket.h:320
bool stream_buf_read_setup_dowork(struct link_socket *sock)
Definition socket.c:2719
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
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:1268
static void addr_zero_host(struct openvpn_sockaddr *addr)
Definition socket.h:849
void setenv_sockaddr(struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const unsigned int flags)
Definition socket.c:3067
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:3603
void link_socket_close(struct link_socket *sock)
Definition socket.c:2390
static bool link_socket_verify_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
Definition socket.h:935
static void link_socket_set_outgoing_addr(struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
Definition socket.h:984
static bool addr_defined_ipi(const struct link_socket_actual *lsa)
Definition socket.h:699
static int link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:1066
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:2926
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:945
struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add)
Definition socket.c:3044
static void link_socket_get_outgoing_addr(struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
Definition socket.h:963
#define SF_USE_IP_PKTINFO
Definition socket.h:221
#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
sa_family_t ascii2af(const char *proto_name)
Definition socket.c:3202
uint16_t packet_size_type
Definition socket.h:56
void link_socket_bad_outgoing_addr(void)
Definition socket.c:2544
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
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:3924
static int af_addr_size(sa_family_t af)
Definition socket.h:864
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:2550
int openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr)
Definition socket.c:713
int link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
Definition socket.c:3316
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:1488
#define SF_PREPEND_SA
Definition socket.h:227
static bool addr_defined(const struct openvpn_sockaddr *addr)
Definition socket.h:661
const char * proto_remote(int proto, bool remote)
Definition socket.c:3276
bool ipv6_addr_safe(const char *ipv6_text_addr)
Definition socket.c:787
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:823
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:2515
int ascii2proto(const char *proto_name)
Definition socket.c:3189
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Definition socket.c:2997
unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:4019
static ssize_t link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:1183
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
Definition socket.c:1434
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:1107
static bool addrlist_port_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a2)
Definition socket.h:776
const char * print_sockaddr_ex(const struct sockaddr *addr, const char *separator, const unsigned int flags, struct gc_arena *gc)
Definition socket.c:2835
struct link_socket * link_socket_new(void)
Definition socket.c:1880
void link_socket_connection_initiated(struct link_socket_info *info, const struct link_socket_actual *addr, const char *common_name, struct env_set *es)
Definition socket.c:2464
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
static bool addr_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
Definition socket.h:732
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:2442
const char * addr_family_name(int af)
Definition socket.c:3252
const char * print_in6_addr(struct in6_addr addr6, unsigned int flags, struct gc_arena *gc)
Definition socket.c:3017
bool link_socket_update_flags(struct link_socket *sock, unsigned int sockflags)
Definition socket.c:1023
static bool addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *addrlist)
Definition socket.h:747
bool ip_addr_dotted_quad_safe(const char *dotted_quad)
Definition socket.c:737
static bool link_socket_proto_connection_oriented(int proto)
Definition socket.h:642
static bool addrlist_match_proto(const struct openvpn_sockaddr *a1, struct addrinfo *addr_list, const int proto)
Definition socket.h:839
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
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
Contains all state information for one tunnel.
Definition openvpn.h:474
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
uint8_t tos
Definition proto.h:97
union openvpn_sockaddr::@25 addr
struct sockaddr sa
Definition socket.h:69
struct sockaddr_in in4
Definition socket.h:70
struct sockaddr_in6 in6
Definition socket.h:71
DWORD flags
Definition win32.h:209
DWORD size
Definition win32.h:208
OVERLAPPED overlapped
Definition win32.h:207
bool is_handle
Definition socket.h:290
bool prepend_sa
Definition socket.h:291
struct buffer buf
Definition socket.h:137
bool error
Definition socket.h:141
struct buffer residual
Definition socket.h:133
bool residual_fully_formed
Definition socket.h:135
int maxlen
Definition socket.h:134
int len
Definition socket.h:139
struct buffer next
Definition socket.h:138
struct buffer buf_init
Definition socket.h:132
unsigned short sa_family_t
Definition syshead.h:395
SOCKET socket_descriptor_t
Definition syshead.h:439
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:155
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:262
static bool overlapped_io_active(struct overlapped_io *o)
Definition win32.h:228
#define IN6_ARE_ADDR_EQUAL(a, b)
Definition win32.h:52