OpenVPN
forward.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-2025 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "forward.h"
30#include "init.h"
31#include "push.h"
32#include "gremlin.h"
33#include "mss.h"
34#include "event.h"
35#include "occ.h"
36#include "ping.h"
37#include "ps.h"
38#include "dhcp.h"
39#include "common.h"
40#include "ssl_verify.h"
41#include "dco.h"
42#include "auth_token.h"
43#include "tun_afunix.h"
44
45#include "memdbg.h"
46
49
50/* show event wait debugging info */
51
52#ifdef ENABLE_DEBUG
53
54static const char *
55wait_status_string(struct context *c, struct gc_arena *gc)
56{
57 struct buffer out = alloc_buf_gc(64, gc);
58
59 buf_printf(&out, "I/O WAIT %s|%s| %s", tun_stat(c->c1.tuntap, EVENT_READ, gc),
60 tun_stat(c->c1.tuntap, EVENT_WRITE, gc), tv_string(&c->c2.timeval, gc));
61 for (int i = 0; i < c->c1.link_sockets_num; i++)
62 {
63 buf_printf(&out, "\n %s|%s", socket_stat(c->c2.link_sockets[i], EVENT_READ, gc),
64 socket_stat(c->c2.link_sockets[i], EVENT_WRITE, gc));
65 }
66 return BSTR(&out);
67}
68
69static void
71{
72 struct gc_arena gc = gc_new();
73 dmsg(D_EVENT_WAIT, "%s", wait_status_string(c, &gc));
74 gc_free(&gc);
75}
76
77#endif /* ifdef ENABLE_DEBUG */
78
79static void
81{
82 msg(D_STREAM_ERRORS, "Fatal TLS error (check_tls_errors_co), restarting");
83 register_signal(c->sig, c->c2.tls_exit_signal, "tls-error"); /* SOFT-SIGUSR1 -- TLS error */
84}
85
86static void
88{
89 register_signal(c->sig, c->c2.tls_exit_signal, "tls-error"); /* SOFT-SIGUSR1 -- TLS error */
90}
91
92/*
93 * TLS errors are fatal in TCP mode.
94 * Also check for --tls-exit trigger.
95 */
96static inline void
98{
99 if (c->c2.tls_multi && c->c2.tls_exit_signal)
100 {
102 {
103 if (c->c2.tls_multi->n_soft_errors)
104 {
106 }
107 }
108 else
109 {
110 if (c->c2.tls_multi->n_hard_errors)
111 {
113 }
114 }
115 }
116}
117
118/*
119 * Set our wakeup to 0 seconds, so we will be rescheduled
120 * immediately.
121 */
122static inline void
124{
125 c->c2.timeval.tv_sec = 0; /* ZERO-TIMEOUT */
126 c->c2.timeval.tv_usec = 0;
127}
128
129static inline void
131{
132 if (sec < 0)
133 {
134 sec = 0;
135 }
136 if (sec < c->c2.timeval.tv_sec)
137 {
138 c->c2.timeval.tv_sec = sec;
139 c->c2.timeval.tv_usec = 0;
140 }
141}
142
143void
145{
146 /* DCO context is not yet initialised or enabled */
147 if (!dco_enabled(&c->options))
148 {
149 return;
150 }
151
152 /* no active peer (p2p tls-server mode) */
153 if (c->c2.tls_multi->dco_peer_id == -1)
154 {
155 return;
156 }
157
158 if (!dco_update_keys(&c->c1.tuntap->dco, c->c2.tls_multi))
159 {
160 /* Something bad happened. Kill the connection to
161 * be able to recover. */
162 register_signal(c->sig, SIGUSR1, "dco update keys error");
163 }
164}
165
166/*
167 * In TLS mode, let TLS level respond to any control-channel
168 * packets which were received, or prepare any packets for
169 * transmission.
170 *
171 * tmp_int is purely an optimization that allows us to call
172 * tls_multi_process less frequently when there's not much
173 * traffic on the control-channel.
174 *
175 */
176static void
178{
179 interval_t wakeup = BIG_TIMEOUT;
180
181 if (interval_test(&c->c2.tmp_int))
182 {
183 const int tmp_status = tls_multi_process(
184 c->c2.tls_multi, &c->c2.to_link, &c->c2.to_link_addr, get_link_socket_info(c), &wakeup);
185
186 if (tmp_status == TLSMP_RECONNECT)
187 {
190 }
191
192 if (tmp_status == TLSMP_ACTIVE || tmp_status == TLSMP_RECONNECT)
193 {
194 update_time();
196 }
197 else if (tmp_status == TLSMP_KILL)
198 {
199 if (c->options.mode == MODE_SERVER)
200 {
202 }
203 else
204 {
205 register_signal(c->sig, SIGTERM, "auth-control-exit");
206 }
207 }
208
210 }
211
212 interval_schedule_wakeup(&c->c2.tmp_int, &wakeup);
213
214 /*
215 * Our current code has no good hooks in the TLS machinery to update
216 * DCO keys. So we check the key status after the whole TLS machinery
217 * has been completed and potentially update them
218 *
219 * We have a hidden state transition from secondary to primary key based
220 * on ks->auth_deferred_expire that DCO needs to check that the normal
221 * TLS state engine does not check. So we call the \c check_dco_key_status
222 * function even if tmp_status does not indicate that something has changed.
223 */
225
226 if (wakeup)
227 {
228 context_reschedule_sec(c, wakeup);
229 }
230}
231
232static void
234{
235 if (buf_string_match_head_str(buf, "AUTH_FAILED"))
236 {
237 receive_auth_failed(c, buf);
238 }
239 else if (buf_string_match_head_str(buf, "PUSH_"))
240 {
241 incoming_push_message(c, buf);
242 }
243 else if (buf_string_match_head_str(buf, "RESTART"))
244 {
245 server_pushed_signal(c, buf, true, 7);
246 }
247 else if (buf_string_match_head_str(buf, "HALT"))
248 {
249 server_pushed_signal(c, buf, false, 4);
250 }
251 else if (buf_string_match_head_str(buf, "INFO_PRE"))
252 {
253 server_pushed_info(buf, 8);
254 }
255 else if (buf_string_match_head_str(buf, "INFO"))
256 {
257 server_pushed_info(buf, 4);
258 }
259 else if (buf_string_match_head_str(buf, "CR_RESPONSE"))
260 {
261 receive_cr_response(c, buf);
262 }
263 else if (buf_string_match_head_str(buf, "AUTH_PENDING"))
264 {
265 receive_auth_pending(c, buf);
266 }
267 else if (buf_string_match_head_str(buf, "EXIT"))
268 {
270 }
271 else
272 {
273 msg(D_PUSH_ERRORS, "WARNING: Received unknown control message: %s", BSTR(buf));
274 }
275}
276
277/*
278 * Handle incoming configuration
279 * messages on the control channel.
280 */
281static void
283{
284 int len = tls_test_payload_len(c->c2.tls_multi);
285 /* We should only be called with len >0 */
286 ASSERT(len > 0);
287
288 struct gc_arena gc = gc_new();
289 struct buffer buf = alloc_buf_gc(len, &gc);
290 if (tls_rec_payload(c->c2.tls_multi, &buf))
291 {
292 while (BLEN(&buf) > 1)
293 {
294 struct buffer cmdbuf = extract_command_buffer(&buf, &gc);
295
296 if (cmdbuf.len > 0)
297 {
299 }
300 }
301 }
302 else
303 {
304 msg(D_PUSH_ERRORS, "WARNING: Receive control message failed");
305 }
306
307 gc_free(&gc);
308}
309
310/*
311 * Periodically resend PUSH_REQUEST until PUSH message received
312 */
313static void
315{
317
318 /* if no response to first push_request, retry at PUSH_REQUEST_INTERVAL second intervals */
319 event_timeout_modify_wakeup(&c->c2.push_request_interval, PUSH_REQUEST_INTERVAL);
320}
321
322/*
323 * Things that need to happen immediately after connection initiation should go here.
324 *
325 * Options like --up-delay need to be triggered by this function which
326 * checks for connection establishment.
327 *
328 * Note: The process_incoming_push_reply currently assumes that this function
329 * only sets up the pull request timer when pull is enabled.
330 */
331static void
333{
335 {
336 /* if --pull was specified, send a push request to server */
337 if (c->c2.tls_multi && c->options.pull)
338 {
339#ifdef ENABLE_MANAGEMENT
340 if (management)
341 {
343 NULL);
344 }
345#endif
346 /* fire up push request right away (already 1s delayed) */
347 /* We might receive a AUTH_PENDING request before we armed this
348 * timer. In that case we don't change the value */
349 if (c->c2.push_request_timeout < now)
350 {
351 c->c2.push_request_timeout = now + c->options.handshake_window;
352 }
353 event_timeout_init(&c->c2.push_request_interval, 0, now);
355 }
356 else
357 {
358 if (!do_up(c, false, 0))
359 {
360 register_signal(c->sig, SIGUSR1, "connection initialisation failed");
361 }
362 }
363
364 event_timeout_clear(&c->c2.wait_for_connect);
365 }
366}
367
368bool
370 msglvl_t msglevel)
371{
372 struct gc_arena gc = gc_new();
373 bool stat;
374
376 struct key_state *ks = &session->key[KS_PRIMARY];
377
378 /* buffered cleartext write onto TLS control channel */
379 stat = tls_send_payload(ks, (uint8_t *)str, strlen(str) + 1);
380
381 msg(msglevel, "SENT CONTROL [%s]: '%s' (status=%d)",
382 session->common_name ? session->common_name : "UNDEF", sanitize_control_message(str, &gc),
383 (int)stat);
384
385 gc_free(&gc);
386 return stat;
387}
388
389void
391{
393 context_immediate_reschedule(c); /* ZERO-TIMEOUT */
394}
395
396bool
397send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
398{
399 if (c->c2.tls_multi)
400 {
402 bool ret = send_control_channel_string_dowork(session, str, msglevel);
404
405 return ret;
406 }
407 return true;
408}
409/*
410 * Add routes.
411 */
412
413static void
414check_add_routes_action(struct context *c, const bool errors)
415{
416 bool route_status = do_route(&c->options, c->c1.route_list, c->c1.route_ipv6_list, c->c1.tuntap,
417 c->plugins, c->c2.es, &c->net_ctx);
418
419 int flags = (errors ? ISC_ERRORS : 0);
420 flags |= (!route_status ? ISC_ROUTE_ERRORS : 0);
421
422 update_time();
425 initialization_sequence_completed(c, flags); /* client/p2p --route-delay was defined */
426}
427
428static void
430{
431 if (test_routes(c->c1.route_list, c->c1.tuntap))
432 {
433 check_add_routes_action(c, false);
434 }
436 {
438 }
439 else
440 {
441 msg(D_ROUTE, "Route: Waiting for TUN/TAP interface to come up...");
442 if (c->c1.tuntap)
443 {
444 if (!tun_standby(c->c1.tuntap))
445 {
446 register_signal(c->sig, SIGHUP, "ip-fail");
448#ifdef _WIN32
451#endif
452 }
453 }
454 update_time();
455 if (c->c2.route_wakeup.n != 1)
456 {
458 }
460 }
461}
462
463/*
464 * Should we exit due to inactivity timeout?
465 *
466 * In the non-dco case, the timeout is reset via register_activity()
467 * whenever there is sufficient activity on tun or link, so this function
468 * is only ever called to raise the TERM signal.
469 *
470 * With DCO, OpenVPN does not see incoming or outgoing data packets anymore
471 * and the logic needs to change - we permit the event to trigger and check
472 * kernel DCO counters here, returning and rearming the timer if there was
473 * sufficient traffic.
474 *
475 * NOTE: FreeBSD DCO does not supply "tun bytes" (= decrypted payload) today,
476 * so "dco bytes" (encrypted bytes, including keepalives) is used instead
477 */
478static void
480{
481 if (dco_enabled(&c->options) && dco_get_peer_stats(c, true) == 0)
482 {
483#ifdef TARGET_FREEBSD
484 int64_t tot_bytes = c->c2.dco_read_bytes + c->c2.dco_write_bytes;
485#else
486 int64_t tot_bytes = c->c2.tun_read_bytes + c->c2.tun_write_bytes;
487#endif
488 int64_t new_bytes = tot_bytes - c->c2.inactivity_bytes;
489
490 if (new_bytes > c->options.inactivity_minimum_bytes)
491 {
492 c->c2.inactivity_bytes = tot_bytes;
494 return;
495 }
496 }
497
498 msg(M_INFO, "Inactivity timeout (--inactive), exiting");
499 register_signal(c->sig, SIGTERM, "inactive");
500}
501
502int
504{
505 update_time();
506 int remaining = event_timeout_remaining(server_poll_timeout);
507 return max_int(0, remaining);
508}
509
510static void
512{
514 ASSERT(c->c2.tls_multi);
516 {
517 msg(M_INFO, "Server poll timeout, restarting");
518 register_signal(c->sig, SIGUSR1, "server_poll");
520 }
521}
522
523/*
524 * Schedule a SIGTERM signal c->options.scheduled_exit_interval seconds from now.
525 */
526bool
528{
529 const int n_seconds = c->options.scheduled_exit_interval;
530 /* don't reschedule if already scheduled. */
532 {
533 return false;
534 }
536 update_time();
538 event_timeout_init(&c->c2.scheduled_exit, n_seconds, now);
539 c->c2.scheduled_exit_signal = SIGTERM;
540 msg(D_SCHED_EXIT, "Delayed exit in %d seconds", n_seconds);
541 return true;
542}
543
544/*
545 * Scheduled exit?
546 */
547static void
549{
550 register_signal(c->sig, c->c2.scheduled_exit_signal, "delayed-exit");
551}
552
553/*
554 * Should we write timer-triggered status file.
555 */
556static void
558{
559 if (c->c1.status_output)
560 {
562 }
563}
564
565#ifdef ENABLE_FRAGMENT
566/*
567 * Should we deliver a datagram fragment to remote?
568 * c is expected to be a single-link context (p2p or child)
569 */
570static void
572{
573 struct link_socket_info *lsi = get_link_socket_info(c);
574
575 /* OS MTU Hint? */
576 if (lsi->mtu_changed && lsi->lsa)
577 {
579 lsi->mtu_changed = false;
580 }
581
583 {
584 if (!c->c2.to_link.len)
585 {
586 /* encrypt a fragment for output to TCP/UDP port */
588 encrypt_sign(c, false);
589 }
590 }
591
593}
594#endif /* ifdef ENABLE_FRAGMENT */
595
596/*
597 * Buffer reallocation, for use with null encryption.
598 */
599static inline void
600buffer_turnover(const uint8_t *orig_buf, struct buffer *dest_stub, struct buffer *src_stub,
601 struct buffer *storage)
602{
603 if (orig_buf == src_stub->data && src_stub->data != storage->data)
604 {
605 buf_assign(storage, src_stub);
606 *dest_stub = *storage;
607 }
608 else
609 {
610 *dest_stub = *src_stub;
611 }
612}
613
614/*
615 * Compress, fragment, encrypt and HMAC-sign an outgoing packet.
616 * Input: c->c2.buf
617 * Output: c->c2.to_link
618 */
619void
620encrypt_sign(struct context *c, bool comp_frag)
621{
622 struct context_buffers *b = c->c2.buffers;
623 const uint8_t *orig_buf = c->c2.buf.data;
624 struct crypto_options *co = NULL;
625
626 if (dco_enabled(&c->options))
627 {
628 msg(M_WARN, "Attempting to send data packet while data channel offload is in use. "
629 "Dropping packet");
630 c->c2.buf.len = 0;
631 }
632
633 /*
634 * Drop non-TLS outgoing packet if client-connect script/plugin
635 * has not yet succeeded. In non-TLS tls_multi mode is not defined
636 * and we always pass packets.
637 */
639 {
640 c->c2.buf.len = 0;
641 }
642
643 if (comp_frag)
644 {
645#ifdef USE_COMP
646 /* Compress the packet. */
647 if (c->c2.comp_context)
648 {
649 (*c->c2.comp_context->alg.compress)(&c->c2.buf, b->compress_buf, c->c2.comp_context,
650 &c->c2.frame);
651 }
652#endif
653#ifdef ENABLE_FRAGMENT
654 if (c->c2.fragment)
655 {
657 }
658#endif
659 }
660
661 /* initialize work buffer with buf.headroom bytes of prepend capacity */
663
664 if (c->c2.tls_multi)
665 {
666 /* Get the key we will use to encrypt the packet. */
667 tls_pre_encrypt(c->c2.tls_multi, &c->c2.buf, &co);
668 /* If using P_DATA_V2, prepend the 1-byte opcode and 3-byte peer-id to the
669 * packet before openvpn_encrypt(), so we can authenticate the opcode too.
670 */
671 if (c->c2.buf.len > 0 && c->c2.tls_multi->use_peer_id)
672 {
674 }
675 }
676 else
677 {
678 co = &c->c2.crypto_options;
679 }
680
681 /* Encrypt and authenticate the packet */
682 openvpn_encrypt(&c->c2.buf, b->encrypt_buf, co);
683
684 /* Do packet administration */
685 if (c->c2.tls_multi)
686 {
687 if (c->c2.buf.len > 0 && !c->c2.tls_multi->use_peer_id)
688 {
690 }
692 }
693
694 /*
695 * Get the address we will be sending the packet to.
696 */
698
699 /* if null encryption, copy result to read_tun_buf */
700 buffer_turnover(orig_buf, &c->c2.to_link, &c->c2.buf, &b->read_tun_buf);
701}
702
703/*
704 * Should we exit due to session timeout?
705 */
706static void
708{
711 {
712 msg(M_INFO, "Session timeout, exiting");
713 register_signal(c->sig, SIGTERM, "session-timeout");
714 }
715}
716
717/*
718 * Coarse timers work to 1 second resolution.
719 */
720static void
722{
723 /* flush current packet-id to file once per 60
724 * seconds if --replay-persist was specified */
727 {
729 }
730
731 /* Should we write timer-triggered status file */
732 if (c->c1.status_output
734 {
736 }
737
738 /* process connection establishment items */
740 {
742 }
743
744 /* see if we should send a push_request (option --pull) */
746 {
748 }
749
750 /* process --route options */
752 {
754 }
755
756 /* check if we want to refresh the auth-token */
758 {
760 }
761
762 /* possibly exit due to --inactive */
765 {
767 }
768
769 if (c->sig->signal_received)
770 {
771 return;
772 }
773
774 /* kill session if time is over */
776 if (c->sig->signal_received)
777 {
778 return;
779 }
780
781 /* restart if ping not received */
783 if (c->sig->signal_received)
784 {
785 return;
786 }
787
788 if (c->c2.tls_multi)
789 {
792 {
794 }
795 if (c->sig->signal_received)
796 {
797 return;
798 }
800 {
802 }
803 if (c->sig->signal_received)
804 {
805 return;
806 }
807 }
808
809 /* Should we send an OCC_REQUEST message? */
811
812 /* Should we send an MTU load test? */
814
815 /* Should we send an OCC_EXIT message to remote? */
817 {
819 }
820
821 /* Should we ping the remote? */
823
824#ifdef ENABLE_MANAGEMENT
825 if (management)
826 {
828 }
829#endif /* ENABLE_MANAGEMENT */
830}
831
832#if defined(__GNUC__) || defined(__clang__)
833#pragma GCC diagnostic push
834#pragma GCC diagnostic ignored "-Wconversion"
835#endif
836
837static void
839{
840 if (now < c->c2.coarse_timer_wakeup)
841 {
843 return;
844 }
845
846 const struct timeval save = c->c2.timeval;
847 c->c2.timeval.tv_sec = BIG_TIMEOUT;
848 c->c2.timeval.tv_usec = 0;
850 c->c2.coarse_timer_wakeup = now + c->c2.timeval.tv_sec;
851
852 dmsg(D_INTERVAL, "TIMER: coarse timer wakeup %" PRIi64 " seconds",
853 (int64_t)c->c2.timeval.tv_sec);
854
855 /* Is the coarse timeout NOT the earliest one? */
856 if (c->c2.timeval.tv_sec > save.tv_sec)
857 {
858 c->c2.timeval = save;
859 }
860}
861
862static void
864{
865 const int update_interval = 10; /* seconds */
866 c->c2.update_timeout_random_component = now + update_interval;
867 c->c2.timeout_random_component.tv_usec = (time_t)get_random() & 0x0003FFFF;
868 c->c2.timeout_random_component.tv_sec = 0;
869
870 dmsg(D_INTERVAL, "RANDOM USEC=%ld", (long)c->c2.timeout_random_component.tv_usec);
871}
872
873static inline void
875{
877 {
879 }
880 if (c->c2.timeval.tv_sec >= 1)
881 {
883 }
884}
885
886/*
887 * Handle addition and removal of the 10-byte Socks5 header
888 * in UDP packets.
889 */
890
891static inline void
893{
894 if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
895 {
897 }
898}
899
900static inline void
902 struct link_socket_actual **to_addr, int *size_delta)
903{
904 if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
905 {
906 *size_delta += socks_process_outgoing_udp(&c->c2.to_link, c->c2.to_link_addr);
907 *to_addr = &sock->socks_relay;
908 }
909}
910
911/* undo effect of socks_preprocess_outgoing_link */
912static inline void
913link_socket_write_post_size_adjust(int *size, int size_delta, struct buffer *buf)
914{
915 if (size_delta > 0 && *size > size_delta)
916 {
917 *size -= size_delta;
918 if (!buf_advance(buf, size_delta))
919 {
920 *size = 0;
921 }
922 }
923}
924
925/*
926 * Output: c->c2.buf
927 */
928
929void
930read_incoming_link(struct context *c, struct link_socket *sock)
931{
932 /*
933 * Set up for recvfrom call to read datagram
934 * sent to our TCP/UDP port.
935 */
936 int status;
937
938 /*ASSERT (!c->c2.to_tun.len);*/
939
940 c->c2.buf = c->c2.buffers->read_link_buf;
942
943 status = link_socket_read(sock, &c->c2.buf, &c->c2.from);
944
946 {
947#if PORT_SHARE
948 if (port_share && socket_foreign_protocol_detected(sock))
949 {
950 const struct buffer *fbuf = socket_foreign_protocol_head(sock);
951 const int sd = socket_foreign_protocol_sd(sock);
953 register_signal(c->sig, SIGTERM, "port-share-redirect");
954 }
955 else
956#endif
957 {
958 /* received a disconnect from a connection-oriented protocol */
959 if (event_timeout_defined(&c->c2.explicit_exit_notification_interval))
960 {
962 "Connection reset during exit notification period, ignoring [%d]", status);
964 }
965 else
966 {
968 "connection-reset"); /* SOFT-SIGUSR1 -- TCP connection reset */
969 msg(D_STREAM_ERRORS, "Connection reset, restarting [%d]", status);
970 }
971 }
972 return;
973 }
974
975 /* check_status() call below resets last-error code */
977
978 /* check recvfrom status */
979 check_status(status, "read", sock, NULL);
980
981 if (dco_win_timeout)
982 {
984 }
985
986 /* Remove socks header if applicable */
988}
989
990bool
992{
993 struct gc_arena gc = gc_new();
994 bool decrypt_status = false;
995
996 if (c->c2.buf.len > 0)
997 {
998 c->c2.link_read_bytes += c->c2.buf.len;
1000 c->c2.original_recv_size = c->c2.buf.len;
1001 }
1002 else
1003 {
1004 c->c2.original_recv_size = 0;
1005 }
1006
1007#ifdef ENABLE_DEBUG
1008 /* take action to corrupt packet if we are in gremlin test mode */
1009 if (c->options.gremlin)
1010 {
1011 if (!ask_gremlin(c->options.gremlin))
1012 {
1013 c->c2.buf.len = 0;
1014 }
1015 corrupt_gremlin(&c->c2.buf, c->options.gremlin);
1016 }
1017#endif
1018
1019 /* log incoming packet */
1020#ifdef LOG_RW
1021 if (c->c2.log_rw && c->c2.buf.len > 0)
1022 {
1023 fprintf(stderr, "R");
1024 }
1025#endif
1026 msg(D_LINK_RW, "%s READ [%d] from %s: %s", proto2ascii(lsi->proto, lsi->af, true),
1027 BLEN(&c->c2.buf), print_link_socket_actual(&c->c2.from, &gc), PROTO_DUMP(&c->c2.buf, &gc));
1028
1029 /*
1030 * Good, non-zero length packet received.
1031 * Commence multi-stage processing of packet,
1032 * such as authenticate, decrypt, decompress.
1033 * If any stage fails, it sets buf.len to 0 or -1,
1034 * telling downstream stages to ignore the packet.
1035 */
1036 if (c->c2.buf.len > 0)
1037 {
1038 struct crypto_options *co = NULL;
1039 const uint8_t *ad_start = NULL;
1040 if (!link_socket_verify_incoming_addr(&c->c2.buf, lsi, &c->c2.from))
1041 {
1043 }
1044
1045 if (c->c2.tls_multi)
1046 {
1047 uint8_t opcode = *BPTR(&c->c2.buf) >> P_OPCODE_SHIFT;
1048
1049 /*
1050 * If DCO is enabled, the kernel drivers require that the
1051 * other end only sends P_DATA_V2 packets. V1 are unknown
1052 * to kernel and passed to userland, but we cannot handle them
1053 * either because crypto context is missing - so drop the packet.
1054 *
1055 * This can only happen with particular old (2.4.0-2.4.4) servers.
1056 */
1057 if ((opcode == P_DATA_V1) && dco_enabled(&c->options))
1058 {
1059 msg(D_LINK_ERRORS, "Data Channel Offload doesn't support DATA_V1 packets. "
1060 "Upgrade your server to 2.4.5 or newer.");
1061 c->c2.buf.len = 0;
1062 }
1063
1064 /*
1065 * If tls_pre_decrypt returns true, it means the incoming
1066 * packet was a good TLS control channel packet. If so, TLS code
1067 * will deal with the packet and set buf.len to 0 so downstream
1068 * stages ignore it.
1069 *
1070 * If the packet is a data channel packet, tls_pre_decrypt
1071 * will load crypto_options with the correct encryption key
1072 * and return false.
1073 */
1074 if (tls_pre_decrypt(c->c2.tls_multi, &c->c2.from, &c->c2.buf, &co, floated, &ad_start))
1075 {
1077
1078 /* reset packet received timer if TLS packet */
1080 {
1082 }
1083 }
1084 }
1085 else
1086 {
1087 co = &c->c2.crypto_options;
1088 }
1089
1090 /*
1091 * Drop non-TLS packet if client-connect script/plugin and cipher selection
1092 * has not yet succeeded. In non-TLS mode tls_multi is not defined
1093 * and we always pass packets.
1094 */
1096 {
1097 c->c2.buf.len = 0;
1098 }
1099
1100 /* authenticate and decrypt the incoming packet */
1101 decrypt_status =
1102 openvpn_decrypt(&c->c2.buf, c->c2.buffers->decrypt_buf, co, &c->c2.frame, ad_start);
1103
1104 if (!decrypt_status
1105 /* on the instance context we have only one socket, so just check the first one */
1107 {
1108 /* decryption errors are fatal in TCP mode */
1109 register_signal(c->sig, SIGUSR1,
1110 "decryption-error"); /* SOFT-SIGUSR1 -- decryption error in TCP mode */
1111 msg(D_STREAM_ERRORS, "Fatal decryption error (process_incoming_link), restarting");
1112 }
1113 }
1114 else
1115 {
1116 buf_reset(&c->c2.to_tun);
1117 }
1118 gc_free(&gc);
1119
1120 return decrypt_status;
1121}
1122
1123void
1125 const uint8_t *orig_buf)
1126{
1127 if (c->c2.buf.len > 0)
1128 {
1129#ifdef ENABLE_FRAGMENT
1130 if (c->c2.fragment)
1131 {
1133 }
1134#endif
1135
1136#ifdef USE_COMP
1137 /* decompress the incoming packet */
1138 if (c->c2.comp_context)
1139 {
1140 (*c->c2.comp_context->alg.decompress)(&c->c2.buf, c->c2.buffers->decompress_buf,
1141 c->c2.comp_context, &c->c2.frame);
1142 }
1143#endif
1144
1145#ifdef PACKET_TRUNCATION_CHECK
1146 /* if (c->c2.buf.len > 1) --c->c2.buf.len; */
1147 ipv4_packet_size_verify(BPTR(&c->c2.buf), BLEN(&c->c2.buf), TUNNEL_TYPE(c->c1.tuntap),
1148 "POST_DECRYPT", &c->c2.n_trunc_post_decrypt);
1149#endif
1150
1151 /*
1152 * Set our "official" outgoing address, since
1153 * if buf.len is non-zero, we know the packet
1154 * authenticated. In TLS mode we do nothing
1155 * because TLS mode takes care of source address
1156 * authentication.
1157 *
1158 * Also, update the persisted version of our packet-id.
1159 */
1160 if (!TLS_MODE(c) && c->c2.buf.len > 0)
1161 {
1162 link_socket_set_outgoing_addr(lsi, &c->c2.from, NULL, c->c2.es);
1163 }
1164
1165 /* reset packet received timer */
1166 if (c->options.ping_rec_timeout && c->c2.buf.len > 0)
1167 {
1169 }
1170
1171 /* increment authenticated receive byte count */
1172 if (c->c2.buf.len > 0)
1173 {
1174 c->c2.link_read_bytes_auth += c->c2.buf.len;
1177 }
1178
1179 /* Did we just receive an openvpn ping packet? */
1180 if (is_ping_msg(&c->c2.buf))
1181 {
1182 dmsg(D_PING, "RECEIVED PING PACKET");
1183 c->c2.buf.len = 0; /* drop packet */
1184 }
1185
1186 /* Did we just receive an OCC packet? */
1187 if (is_occ_msg(&c->c2.buf))
1188 {
1190 }
1191
1192 buffer_turnover(orig_buf, &c->c2.to_tun, &c->c2.buf, &c->c2.buffers->read_link_buf);
1193
1194 /* to_tun defined + unopened tuntap can cause deadlock */
1195 if (!tuntap_defined(c->c1.tuntap))
1196 {
1197 c->c2.to_tun.len = 0;
1198 }
1199 }
1200 else
1201 {
1202 buf_reset(&c->c2.to_tun);
1203 }
1204}
1205
1206static void
1208{
1209 struct link_socket_info *lsi = &sock->info;
1210 const uint8_t *orig_buf = c->c2.buf.data;
1211
1212 process_incoming_link_part1(c, lsi, false);
1213 process_incoming_link_part2(c, lsi, orig_buf);
1214}
1215
1216void
1217extract_dco_float_peer_addr(const sa_family_t socket_family, struct openvpn_sockaddr *out_osaddr,
1218 const struct sockaddr *float_sa)
1219{
1220 if (float_sa->sa_family == AF_INET)
1221 {
1222 struct sockaddr_in *float4 = (struct sockaddr_in *)float_sa;
1223 /* DCO treats IPv4-mapped IPv6 addresses as pure IPv4. However, on a
1224 * dual-stack socket, we need to preserve the mapping otherwise openvpn
1225 * will not be able to find the peer by its transport address.
1226 */
1227 if (socket_family == AF_INET6)
1228 {
1229 out_osaddr->addr.in6.sin6_family = AF_INET6;
1230 out_osaddr->addr.in6.sin6_port = float4->sin_port;
1231
1232 memset(&out_osaddr->addr.in6.sin6_addr.s6_addr, 0, 10);
1233 out_osaddr->addr.in6.sin6_addr.s6_addr[10] = 0xff;
1234 out_osaddr->addr.in6.sin6_addr.s6_addr[11] = 0xff;
1235 memcpy(&out_osaddr->addr.in6.sin6_addr.s6_addr[12], &float4->sin_addr.s_addr,
1236 sizeof(in_addr_t));
1237 }
1238 else
1239 {
1240 memcpy(&out_osaddr->addr.in4, float4, sizeof(struct sockaddr_in));
1241 }
1242 }
1243 else
1244 {
1245 struct sockaddr_in6 *float6 = (struct sockaddr_in6 *)float_sa;
1246 memcpy(&out_osaddr->addr.in6, float6, sizeof(struct sockaddr_in6));
1247 }
1248}
1249
1250static void
1252{
1253#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
1254 dco_context_t *dco = &c->c1.tuntap->dco;
1255
1256 dco_do_read(dco);
1257
1258 /* no message for us to handle - platform specific code has logged details */
1259 if (dco->dco_message_type == 0)
1260 {
1261 return;
1262 }
1263
1264 /* FreeBSD currently sends us removal notifcation with the old peer-id in
1265 * p2p mode with the ping timeout reason, so ignore that one to not shoot
1266 * ourselves in the foot and removing the just established session */
1267 if (dco->dco_message_peer_id != c->c2.tls_multi->dco_peer_id)
1268 {
1270 "%s: received message for mismatching peer-id %d, "
1271 "expected %d",
1272 __func__, dco->dco_message_peer_id, c->c2.tls_multi->dco_peer_id);
1273 return;
1274 }
1275
1276 switch (dco->dco_message_type)
1277 {
1278 case OVPN_CMD_DEL_PEER:
1279 /* peer is gone, unset ID to prevent more kernel calls */
1280 c->c2.tls_multi->dco_peer_id = -1;
1281 if (dco->dco_del_peer_reason == OVPN_DEL_PEER_REASON_EXPIRED)
1282 {
1284 "%s: received peer expired notification of for peer-id "
1285 "%d",
1286 __func__, dco->dco_message_peer_id);
1288 return;
1289 }
1290 break;
1291
1292 case OVPN_CMD_SWAP_KEYS:
1293 msg(D_DCO_DEBUG, "%s: received key rotation notification for peer-id %d", __func__,
1294 dco->dco_message_peer_id);
1296 break;
1297
1298 default:
1299 msg(D_DCO_DEBUG, "%s: received message of type %u - ignoring", __func__,
1300 dco->dco_message_type);
1301 return;
1302 }
1303
1304#endif /* if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD)) */
1305}
1306
1307/*
1308 * Output: c->c2.buf
1309 */
1310
1311void
1313{
1314 /*
1315 * Setup for read() call on TUN/TAP device.
1316 */
1317 /*ASSERT (!c->c2.to_link.len);*/
1318
1319 c->c2.buf = c->c2.buffers->read_tun_buf;
1320
1321#ifdef _WIN32
1322 /* we cannot end up here when using dco */
1323 ASSERT(!dco_enabled(&c->options));
1324
1325 sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand, .prepend_sa = false };
1326 sockethandle_finalize(sh, &c->c1.tuntap->reads, &c->c2.buf, NULL);
1327#else /* ifdef _WIN32 */
1331 {
1332 c->c2.buf.len =
1334 }
1335 else
1336 {
1337 c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), c->c2.frame.buf.payload_size);
1338 }
1339#endif /* ifdef _WIN32 */
1340
1341#ifdef PACKET_TRUNCATION_CHECK
1342 ipv4_packet_size_verify(BPTR(&c->c2.buf), BLEN(&c->c2.buf), TUNNEL_TYPE(c->c1.tuntap),
1343 "READ_TUN", &c->c2.n_trunc_tun_read);
1344#endif
1345
1346 /* Was TUN/TAP interface stopped? */
1347 if (tuntap_stop(c->c2.buf.len))
1348 {
1349 register_signal(c->sig, SIGTERM, "tun-stop");
1350 msg(M_INFO, "TUN/TAP interface has been stopped, exiting");
1351 return;
1352 }
1353
1354 /* Was TUN/TAP I/O operation aborted? */
1355 if (tuntap_abort(c->c2.buf.len))
1356 {
1357 register_signal(c->sig, SIGHUP, "tun-abort");
1359 msg(M_INFO, "TUN/TAP I/O operation aborted, restarting");
1360 return;
1361 }
1362
1363 /* Check the status return from read() */
1364 check_status(c->c2.buf.len, "read from TUN/TAP", NULL, c->c1.tuntap);
1365}
1366
1375static void
1377{
1378 if (c->c2.to_link_addr == NULL) /* no remote addr known */
1379 {
1380 return;
1381 }
1382
1383 struct openvpn_sockaddr *link_addr = &c->c2.to_link_addr->dest;
1384 struct link_socket_info *lsi = get_link_socket_info(c);
1385 uint16_t link_port = atoi(c->c2.link_sockets[0]->remote_port);
1386
1387 int ip_hdr_offset = 0;
1388 int tun_ip_ver = get_tun_ip_ver(TUNNEL_TYPE(c->c1.tuntap), &c->c2.buf, &ip_hdr_offset);
1389
1390 if (tun_ip_ver == 4)
1391 {
1392 /* make sure we got whole IP header and TCP/UDP src/dst ports */
1393 if (BLEN(buf) < ((int)sizeof(struct openvpn_iphdr) + ip_hdr_offset + sizeof(uint16_t) * 2))
1394 {
1395 return;
1396 }
1397
1398 /* skip ipv4 packets for ipv6 tun */
1399 if (link_addr->addr.sa.sa_family != AF_INET)
1400 {
1401 return;
1402 }
1403
1404 struct openvpn_iphdr *pip = (struct openvpn_iphdr *)(BPTR(buf) + ip_hdr_offset);
1405
1406 /* skip if tun protocol doesn't match link protocol */
1407 if ((lsi->proto == PROTO_TCP && pip->protocol != OPENVPN_IPPROTO_TCP)
1408 || (lsi->proto == PROTO_UDP && pip->protocol != OPENVPN_IPPROTO_UDP))
1409 {
1410 return;
1411 }
1412
1413
1414 /* drop packets with same dest addr and port as remote */
1415 uint8_t *l4_hdr = (uint8_t *)pip + sizeof(struct openvpn_iphdr);
1416
1417 /* TCP and UDP ports are at the same place in the header, and other protocols
1418 * can not happen here due to the lsi->proto check above */
1419 uint16_t src_port = ntohs(*(uint16_t *)l4_hdr);
1420 uint16_t dst_port = ntohs(*(uint16_t *)(l4_hdr + sizeof(uint16_t)));
1421 if ((memcmp(&link_addr->addr.in4.sin_addr.s_addr, &pip->daddr, sizeof(pip->daddr)) == 0) && (link_port == dst_port))
1422 {
1423 c->c2.buf.len = 0;
1424
1425 struct gc_arena gc = gc_new();
1426 msg(D_LOW, "Recursive routing detected, packet dropped %s:%" PRIu16 " -> %s",
1428 src_port,
1430 gc_free(&gc);
1431 }
1432 }
1433 else if (tun_ip_ver == 6)
1434 {
1435 /* make sure we got whole IPv6 header and TCP/UDP src/dst ports */
1436 if (BLEN(buf) < ((int)sizeof(struct openvpn_ipv6hdr) + ip_hdr_offset + sizeof(uint16_t) * 2))
1437 {
1438 return;
1439 }
1440
1441 /* skip ipv6 packets for ipv4 tun */
1442 if (link_addr->addr.sa.sa_family != AF_INET6)
1443 {
1444 return;
1445 }
1446
1447 struct openvpn_ipv6hdr *pip6 = (struct openvpn_ipv6hdr *)(BPTR(buf) + ip_hdr_offset);
1448
1449 /* skip if tun protocol doesn't match link protocol */
1450 if ((lsi->proto == PROTO_TCP && pip6->nexthdr != OPENVPN_IPPROTO_TCP)
1451 || (lsi->proto == PROTO_UDP && pip6->nexthdr != OPENVPN_IPPROTO_UDP))
1452 {
1453 return;
1454 }
1455
1456 /* drop packets with same dest addr and port as remote */
1457 uint8_t *l4_hdr = (uint8_t *)pip6 + sizeof(struct openvpn_ipv6hdr);
1458 uint16_t src_port = ntohs(*(uint16_t *)l4_hdr);
1459 uint16_t dst_port = ntohs(*(uint16_t *)(l4_hdr + sizeof(uint16_t)));
1460 if ((OPENVPN_IN6_ARE_ADDR_EQUAL(&link_addr->addr.in6.sin6_addr, &pip6->daddr)) && (link_port == dst_port))
1461 {
1462 c->c2.buf.len = 0;
1463
1464 struct gc_arena gc = gc_new();
1465 msg(D_LOW, "Recursive routing detected, packet dropped %s:%" PRIu16 " -> %s",
1467 src_port,
1469 gc_free(&gc);
1470 }
1471 }
1472}
1473
1474/*
1475 * Input: c->c2.buf
1476 * Output: c->c2.to_link
1477 */
1478
1479void
1480process_incoming_tun(struct context *c, struct link_socket *out_sock)
1481{
1482 struct gc_arena gc = gc_new();
1483
1484 if (c->c2.buf.len > 0)
1485 {
1486 c->c2.tun_read_bytes += c->c2.buf.len;
1487 }
1488
1489#ifdef LOG_RW
1490 if (c->c2.log_rw && c->c2.buf.len > 0)
1491 {
1492 fprintf(stderr, "r");
1493 }
1494#endif
1495
1496 /* Show packet content */
1497 dmsg(D_TUN_RW, "TUN READ [%d]", BLEN(&c->c2.buf));
1498
1499 if (c->c2.buf.len > 0)
1500 {
1502 {
1504 }
1505 /*
1506 * The --passtos and --mssfix options require
1507 * us to examine the IP header (IPv4 or IPv6).
1508 */
1509 unsigned int flags =
1511 process_ip_header(c, flags, &c->c2.buf, out_sock);
1512
1513#ifdef PACKET_TRUNCATION_CHECK
1514 /* if (c->c2.buf.len > 1) --c->c2.buf.len; */
1515 ipv4_packet_size_verify(BPTR(&c->c2.buf), BLEN(&c->c2.buf), TUNNEL_TYPE(c->c1.tuntap),
1516 "PRE_ENCRYPT", &c->c2.n_trunc_pre_encrypt);
1517#endif
1518 }
1519 if (c->c2.buf.len > 0)
1520 {
1521 encrypt_sign(c, true);
1522 }
1523 else
1524 {
1525 buf_reset(&c->c2.to_link);
1526 }
1527 gc_free(&gc);
1528}
1529
1540void
1541ipv6_send_icmp_unreachable(struct context *c, struct buffer *buf, bool client)
1542{
1543#define MAX_ICMPV6LEN 1280
1544 struct openvpn_icmp6hdr icmp6out;
1545 CLEAR(icmp6out);
1546
1547 /*
1548 * Get a buffer to the ip packet, is_ipv6 automatically forwards
1549 * the buffer to the ip packet
1550 */
1551 struct buffer inputipbuf = *buf;
1552
1553 is_ipv6(TUNNEL_TYPE(c->c1.tuntap), &inputipbuf);
1554
1555 if (BLEN(&inputipbuf) < (int)sizeof(struct openvpn_ipv6hdr))
1556 {
1557 return;
1558 }
1559
1560 const struct openvpn_ipv6hdr *pip6 = (struct openvpn_ipv6hdr *)BPTR(&inputipbuf);
1561
1562 /* Copy version, traffic class, flow label from input packet */
1563 struct openvpn_ipv6hdr pip6out = *pip6;
1564
1565 pip6out.version_prio = pip6->version_prio;
1566 pip6out.daddr = pip6->saddr;
1567
1568 /*
1569 * Use the IPv6 remote address if we have one, otherwise use a fake one
1570 * using the remote address is preferred since it makes debugging and
1571 * understanding where the ICMPv6 error originates easier
1572 */
1574 {
1575 inet_pton(AF_INET6, c->options.ifconfig_ipv6_remote, &pip6out.saddr);
1576 }
1577 else
1578 {
1579 inet_pton(AF_INET6, "fe80::7", &pip6out.saddr);
1580 }
1581
1583
1584 /*
1585 * The ICMPv6 unreachable code worked best in my (arne) tests with Windows,
1586 * Linux and Android. Windows did not like the administratively prohibited
1587 * return code (no fast fail)
1588 */
1591
1592 int icmpheader_len = sizeof(struct openvpn_ipv6hdr) + sizeof(struct openvpn_icmp6hdr);
1593 int totalheader_len = icmpheader_len;
1594
1595 if (TUNNEL_TYPE(c->c1.tuntap) == DEV_TYPE_TAP)
1596 {
1597 totalheader_len += sizeof(struct openvpn_ethhdr);
1598 }
1599
1600 /*
1601 * Calculate size for payload, defined in the standard that the resulting
1602 * frame should be <= 1280 and have as much as possible of the original
1603 * packet
1604 */
1605 int max_payload_size = min_int(MAX_ICMPV6LEN, c->c2.frame.tun_mtu - icmpheader_len);
1606 int payload_len = min_int(max_payload_size, BLEN(&inputipbuf));
1607
1608 pip6out.payload_len = htons(sizeof(struct openvpn_icmp6hdr) + payload_len);
1609
1610 /* Construct the packet as outgoing packet back to the client */
1611 struct buffer *outbuf;
1612 if (client)
1613 {
1614 c->c2.to_tun = c->c2.buffers->aux_buf;
1615 outbuf = &(c->c2.to_tun);
1616 }
1617 else
1618 {
1619 c->c2.to_link = c->c2.buffers->aux_buf;
1620 outbuf = &(c->c2.to_link);
1621 }
1623
1624 /* Fill the end of the buffer with original packet */
1625 ASSERT(buf_safe(outbuf, payload_len));
1626 ASSERT(buf_copy_n(outbuf, &inputipbuf, payload_len));
1627
1628 /* ICMP Header, copy into buffer to allow checksum calculation */
1630
1631 /* Calculate checksum over the packet and write to header */
1632
1636 ((struct openvpn_icmp6hdr *)BPTR(outbuf))->icmp6_cksum = htons(new_csum);
1637
1638
1639 /* IPv6 Header */
1640 ASSERT(buf_write_prepend(outbuf, &pip6out, sizeof(struct openvpn_ipv6hdr)));
1641
1642 /*
1643 * Tap mode, we also need to create an Ethernet header.
1644 */
1645 if (TUNNEL_TYPE(c->c1.tuntap) == DEV_TYPE_TAP)
1646 {
1647 if (BLEN(buf) < (int)sizeof(struct openvpn_ethhdr))
1648 {
1649 return;
1650 }
1651
1652 const struct openvpn_ethhdr *orig_ethhdr = (struct openvpn_ethhdr *)BPTR(buf);
1653
1654 /* Copy frametype and reverse source/destination for the response */
1655 struct openvpn_ethhdr ethhdr;
1656 memcpy(ethhdr.source, orig_ethhdr->dest, OPENVPN_ETH_ALEN);
1657 memcpy(ethhdr.dest, orig_ethhdr->source, OPENVPN_ETH_ALEN);
1658 ethhdr.proto = htons(OPENVPN_ETH_P_IPV6);
1659 ASSERT(buf_write_prepend(outbuf, &ethhdr, sizeof(struct openvpn_ethhdr)));
1660 }
1661#undef MAX_ICMPV6LEN
1662}
1663
1664void
1665process_ip_header(struct context *c, unsigned int flags, struct buffer *buf,
1666 struct link_socket *sock)
1667{
1668 if (!c->options.ce.mssfix)
1669 {
1670 flags &= ~PIP_MSSFIX;
1671 }
1672#if PASSTOS_CAPABILITY
1673 if (!c->options.passtos)
1674 {
1675 flags &= ~PIPV4_PASSTOS;
1676 }
1677#endif
1678 if (!c->options.client_nat)
1679 {
1680 flags &= ~PIPV4_CLIENT_NAT;
1681 }
1683 {
1684 flags &= ~PIPV4_EXTRACT_DHCP_ROUTER;
1685 }
1686 if (!c->options.block_ipv6)
1687 {
1689 }
1690
1691 if (buf->len > 0)
1692 {
1693 struct buffer ipbuf = *buf;
1694 if (is_ipv4(TUNNEL_TYPE(c->c1.tuntap), &ipbuf))
1695 {
1696#if PASSTOS_CAPABILITY
1697 /* extract TOS from IP header */
1698 if (flags & PIPV4_PASSTOS)
1699 {
1701 }
1702#endif
1703
1704 /* possibly alter the TCP MSS */
1705 if (flags & PIP_MSSFIX)
1706 {
1707 mss_fixup_ipv4(&ipbuf, c->c2.frame.mss_fix);
1708 }
1709
1710 /* possibly do NAT on packet */
1711 if ((flags & PIPV4_CLIENT_NAT) && c->options.client_nat)
1712 {
1713 const int direction = (flags & PIP_OUTGOING) ? CN_INCOMING : CN_OUTGOING;
1714 client_nat_transform(c->options.client_nat, &ipbuf, direction);
1715 }
1716 /* possibly extract a DHCP router message */
1717 if (flags & PIPV4_EXTRACT_DHCP_ROUTER)
1718 {
1720 if (dhcp_router)
1721 {
1722 route_list_add_vpn_gateway(c->c1.route_list, c->c2.es, dhcp_router);
1723 }
1724 }
1725 }
1726 else if (is_ipv6(TUNNEL_TYPE(c->c1.tuntap), &ipbuf))
1727 {
1728 /* possibly alter the TCP MSS */
1729 if (flags & PIP_MSSFIX)
1730 {
1731 mss_fixup_ipv6(&ipbuf, c->c2.frame.mss_fix);
1732 }
1733 if (!(flags & PIP_OUTGOING)
1735 {
1737 /* Drop the IPv6 packet */
1738 buf->len = 0;
1739 }
1740 }
1741 }
1742}
1743
1744/*
1745 * Input: c->c2.to_link
1746 */
1747
1748void
1750{
1751 struct gc_arena gc = gc_new();
1752 int error_code = 0;
1753
1754 if (c->c2.to_link.len > 0 && c->c2.to_link.len <= c->c2.frame.buf.payload_size)
1755 {
1756 /*
1757 * Setup for call to send/sendto which will send
1758 * packet to remote over the TCP/UDP port.
1759 */
1760 int size = 0;
1762
1763#ifdef ENABLE_DEBUG
1764 /* In gremlin-test mode, we may choose to drop this packet */
1765 if (!c->options.gremlin || ask_gremlin(c->options.gremlin))
1766#endif
1767 {
1768 /*
1769 * Let the traffic shaper know how many bytes
1770 * we wrote.
1771 */
1772 if (c->options.shaper)
1773 {
1774 int overhead =
1775 datagram_overhead(c->c2.to_link_addr->dest.addr.sa.sa_family, sock->info.proto);
1776 shaper_wrote_bytes(&c->c2.shaper, BLEN(&c->c2.to_link) + overhead);
1777 }
1778
1779 /*
1780 * Let the pinger know that we sent a packet.
1781 */
1783 {
1785 }
1786
1787#if PASSTOS_CAPABILITY
1788 /* Set TOS */
1789 link_socket_set_tos(sock);
1790#endif
1791
1792 /* Log packet send */
1793#ifdef LOG_RW
1794 if (c->c2.log_rw)
1795 {
1796 fprintf(stderr, "W");
1797 }
1798#endif
1799 msg(D_LINK_RW, "%s WRITE [%d] to %s: %s",
1800 proto2ascii(sock->info.proto, sock->info.af, true), BLEN(&c->c2.to_link),
1802
1803 /* Packet send complexified by possible Socks5 usage */
1804 {
1805 struct link_socket_actual *to_addr = c->c2.to_link_addr;
1806 int size_delta = 0;
1807
1808 /* If Socks5 over UDP, prepend header */
1809 socks_preprocess_outgoing_link(c, sock, &to_addr, &size_delta);
1810
1811 /* Send packet */
1812 size = (int)link_socket_write(sock, &c->c2.to_link, to_addr);
1813
1814 /* Undo effect of prepend */
1815 link_socket_write_post_size_adjust(&size, size_delta, &c->c2.to_link);
1816 }
1817
1818 if (size > 0)
1819 {
1821 c->c2.link_write_bytes += size;
1823 }
1824 }
1825
1826 /* Check return status */
1827 error_code = openvpn_errno();
1828 check_status(size, "write", sock, NULL);
1829
1830 if (size > 0)
1831 {
1832 /* Did we write a different size packet than we intended? */
1833 if (size != BLEN(&c->c2.to_link))
1834 {
1836 "TCP/UDP packet was truncated/expanded on write to %s (tried=%d,actual=%d)",
1838 }
1839 }
1840
1841 /* if not a ping/control message, indicate activity regarding --inactive parameter */
1842 if (c->c2.buf.len > 0)
1843 {
1844 register_activity(c, size);
1845 }
1846
1847 /* for unreachable network and "connecting" state switch to the next host */
1848
1849 bool unreachable = error_code ==
1850#ifdef _WIN32
1851 WSAENETUNREACH;
1852#else
1853 ENETUNREACH;
1854#endif
1855 if (size < 0 && unreachable && c->c2.tls_multi
1858 {
1859 msg(M_INFO, "Network unreachable, restarting");
1860 register_signal(c->sig, SIGUSR1, "network-unreachable");
1861 }
1862 }
1863 else
1864 {
1865 if (c->c2.to_link.len > 0)
1866 {
1867 msg(D_LINK_ERRORS, "TCP/UDP packet too large on write to %s (tried=%d,max=%d)",
1870 }
1871 }
1872
1873 buf_reset(&c->c2.to_link);
1874
1875 gc_free(&gc);
1876}
1877
1878/*
1879 * Input: c->c2.to_tun
1880 */
1881
1882void
1883process_outgoing_tun(struct context *c, struct link_socket *in_sock)
1884{
1885 /*
1886 * Set up for write() call to TUN/TAP
1887 * device.
1888 */
1889 if (c->c2.to_tun.len <= 0)
1890 {
1891 return;
1892 }
1893
1894 /*
1895 * The --mssfix option requires
1896 * us to examine the IP header (IPv4 or IPv6).
1897 */
1899 &c->c2.to_tun, in_sock);
1900
1901 if (c->c2.to_tun.len <= c->c2.frame.buf.payload_size)
1902 {
1903 /*
1904 * Write to TUN/TAP device.
1905 */
1906 int size;
1907
1908#ifdef LOG_RW
1909 if (c->c2.log_rw)
1910 {
1911 fprintf(stderr, "w");
1912 }
1913#endif
1914 dmsg(D_TUN_RW, "TUN WRITE [%d]", BLEN(&c->c2.to_tun));
1915
1916#ifdef PACKET_TRUNCATION_CHECK
1917 ipv4_packet_size_verify(BPTR(&c->c2.to_tun), BLEN(&c->c2.to_tun), TUNNEL_TYPE(c->c1.tuntap),
1918 "WRITE_TUN", &c->c2.n_trunc_tun_write);
1919#endif
1920
1921#ifdef _WIN32
1922 size = tun_write_win32(c->c1.tuntap, &c->c2.to_tun);
1923#else
1925 {
1926 size = write_tun_afunix(c->c1.tuntap, BPTR(&c->c2.to_tun), BLEN(&c->c2.to_tun));
1927 }
1928 else
1929 {
1930 size = write_tun(c->c1.tuntap, BPTR(&c->c2.to_tun), BLEN(&c->c2.to_tun));
1931 }
1932#endif
1933
1934 if (size > 0)
1935 {
1936 c->c2.tun_write_bytes += size;
1937 }
1938 check_status(size, "write to TUN/TAP", NULL, c->c1.tuntap);
1939
1940 /* check written packet size */
1941 if (size > 0)
1942 {
1943 /* Did we write a different size packet than we intended? */
1944 if (size != BLEN(&c->c2.to_tun))
1945 {
1947 "TUN/TAP packet was destructively fragmented on write to %s (tried=%d,actual=%d)",
1948 c->c1.tuntap->actual_name, BLEN(&c->c2.to_tun), size);
1949 }
1950
1951 /* indicate activity regarding --inactive parameter */
1952 register_activity(c, size);
1953 }
1954 }
1955 else
1956 {
1957 /*
1958 * This should never happen, probably indicates some kind
1959 * of MTU mismatch.
1960 */
1961 msg(D_LINK_ERRORS, "tun packet too large on write (tried=%d,max=%d)", c->c2.to_tun.len,
1963 }
1964
1965 buf_reset(&c->c2.to_tun);
1966}
1967
1968#if defined(__GNUC__) || defined(__clang__)
1969#pragma GCC diagnostic pop
1970#endif
1971
1972void
1974{
1975 /* make sure current time (now) is updated on function entry */
1976
1977 /*
1978 * Start with an effectively infinite timeout, then let it
1979 * reduce to a timeout that reflects the component which
1980 * needs the earliest service.
1981 */
1982 c->c2.timeval.tv_sec = BIG_TIMEOUT;
1983 c->c2.timeval.tv_usec = 0;
1984
1985#if defined(_WIN32)
1987 {
1988 c->c2.timeval.tv_sec = 1;
1989 if (tuntap_defined(c->c1.tuntap))
1990 {
1992 }
1993 }
1994#endif
1995
1996 /* check coarse timers? */
1998 if (c->sig->signal_received)
1999 {
2000 return;
2001 }
2002
2003 /* If tls is enabled, do tls control channel packet processing. */
2004 if (c->c2.tls_multi)
2005 {
2006 check_tls(c);
2007 }
2008
2009 /* In certain cases, TLS errors will require a restart */
2011 if (c->sig->signal_received)
2012 {
2013 return;
2014 }
2015
2016 /* check for incoming control messages on the control channel like
2017 * push request/reply/update, or authentication failure and 2FA messages */
2018 if (tls_test_payload_len(c->c2.tls_multi) > 0)
2019 {
2021 }
2022
2023 /* Should we send an OCC message? */
2025
2026#ifdef ENABLE_FRAGMENT
2027 /* Should we deliver a datagram fragment to remote? */
2028 if (c->c2.fragment)
2029 {
2030 check_fragment(c);
2031 }
2032#endif
2033
2034 /* Update random component of timeout */
2036}
2037
2038static void
2039multi_io_process_flags(struct context *c, struct event_set *es, const unsigned int flags,
2040 unsigned int *out_socket, unsigned int *out_tuntap)
2041{
2042 unsigned int socket = 0;
2043 unsigned int tuntap = 0;
2044 static uintptr_t tun_shift = TUN_SHIFT;
2045 static uintptr_t err_shift = ERR_SHIFT;
2046
2047 /*
2048 * Calculate the flags based on the provided 'flags' argument.
2049 */
2050 if ((c->options.mode != MODE_SERVER) && (flags & IOW_WAIT_SIGNAL))
2051 {
2052 wait_signal(es, (void *)err_shift);
2053 }
2054
2055 if (flags & IOW_TO_LINK)
2056 {
2057 if (flags & IOW_SHAPER)
2058 {
2059 /*
2060 * If sending this packet would put us over our traffic shaping
2061 * quota, don't send -- instead compute the delay we must wait
2062 * until it will be OK to send the packet.
2063 */
2064 int delay = 0;
2065
2066 /* set traffic shaping delay in microseconds */
2067 if (c->options.shaper)
2068 {
2069 delay = max_int(delay, shaper_delay(&c->c2.shaper));
2070 }
2071
2072 if (delay < 1000)
2073 {
2074 socket |= EVENT_WRITE;
2075 }
2076 else
2077 {
2078 shaper_soonest_event(&c->c2.timeval, delay);
2079 }
2080 }
2081 else
2082 {
2083 socket |= EVENT_WRITE;
2084 }
2085 }
2086 else if (!((flags & IOW_FRAG) && TO_LINK_FRAG(c)))
2087 {
2088 if (flags & IOW_READ_TUN)
2089 {
2090 tuntap |= EVENT_READ;
2091 }
2092 }
2093
2094 /*
2095 * If outgoing data (for TUN/TAP device) pending, wait for ready-to-send status
2096 * from device. Otherwise, wait for incoming data on TCP/UDP port.
2097 */
2098 if (flags & IOW_TO_TUN)
2099 {
2101 }
2102 else
2103 {
2104 if (flags & IOW_READ_LINK)
2105 {
2106 socket |= EVENT_READ;
2107 }
2108 }
2109
2110 /*
2111 * outgoing bcast buffer waiting to be sent?
2112 */
2113 if (flags & IOW_MBUF)
2114 {
2115 socket |= EVENT_WRITE;
2116 }
2117
2118 /*
2119 * Force wait on TUN input, even if also waiting on TCP/UDP output
2120 */
2121 if (flags & IOW_READ_TUN_FORCE)
2122 {
2123 tuntap |= EVENT_READ;
2124 }
2125
2126 /*
2127 * Configure event wait based on socket, tuntap flags.
2128 * (for TCP server sockets this happens in
2129 * socket_set_listen_persistent()).
2130 */
2131 for (int i = 0; i < c->c1.link_sockets_num; i++)
2132 {
2134 {
2135 socket_set(c->c2.link_sockets[i], es, socket, &c->c2.link_sockets[i]->ev_arg, NULL);
2136 }
2137 }
2138
2139 tun_set(c->c1.tuntap, es, tuntap, (void *)tun_shift, NULL);
2140
2141 if (out_socket)
2142 {
2143 *out_socket = socket;
2144 }
2145
2146 if (out_tuntap)
2147 {
2148 *out_tuntap = tuntap;
2149 }
2150}
2151
2152/*
2153 * Wait for I/O events. Used for both TCP & UDP sockets
2154 * in point-to-point mode and for UDP sockets in
2155 * point-to-multipoint mode.
2156 */
2157
2158void
2159get_io_flags_dowork_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags)
2160{
2161 unsigned int out_socket, out_tuntap;
2162
2163 multi_io_process_flags(c, multi_io->es, flags, &out_socket, &out_tuntap);
2164 multi_io->udp_flags = out_socket | out_tuntap;
2165}
2166
2167void
2168get_io_flags_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags)
2169{
2171 if (c->c2.fast_io && (flags & (IOW_TO_TUN | IOW_TO_LINK | IOW_MBUF)))
2172 {
2173 /* fast path -- only for TUN/TAP/UDP writes */
2174 unsigned int ret = 0;
2175 if (flags & IOW_TO_TUN)
2176 {
2177 ret |= TUN_WRITE;
2178 }
2179 if (flags & (IOW_TO_LINK | IOW_MBUF))
2180 {
2181 ret |= SOCKET_WRITE;
2182 }
2183 multi_io->udp_flags = ret;
2184 }
2185 else
2186 {
2187 /* slow path - delegate to io_wait_dowork_udp to calculate flags */
2189 }
2190}
2191
2192void
2193io_wait_dowork(struct context *c, const unsigned int flags)
2194{
2195 unsigned int out_socket;
2196 unsigned int out_tuntap;
2197 struct event_set_return esr[4];
2198
2199 /* These shifts all depend on EVENT_READ and EVENT_WRITE */
2200 static uintptr_t socket_shift = SOCKET_SHIFT; /* depends on SOCKET_READ and SOCKET_WRITE */
2201#ifdef ENABLE_MANAGEMENT
2202 static uintptr_t management_shift =
2203 MANAGEMENT_SHIFT; /* depends on MANAGEMENT_READ and MANAGEMENT_WRITE */
2204#endif
2205#ifdef ENABLE_ASYNC_PUSH
2206 static uintptr_t file_shift = FILE_SHIFT;
2207#endif
2208#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
2209 static uintptr_t dco_shift = DCO_SHIFT; /* Event from DCO linux kernel module */
2210#endif
2211
2212 /*
2213 * Decide what kind of events we want to wait for.
2214 */
2216
2217 multi_io_process_flags(c, c->c2.event_set, flags, &out_socket, &out_tuntap);
2218
2219#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
2220 if (out_socket & EVENT_READ && c->c2.did_open_tun)
2221 {
2222 dco_event_set(&c->c1.tuntap->dco, c->c2.event_set, (void *)dco_shift);
2223 }
2224#endif
2225
2226#ifdef ENABLE_MANAGEMENT
2227 if (management)
2228 {
2229 management_socket_set(management, c->c2.event_set, (void *)management_shift, NULL);
2230 }
2231#endif
2232
2233#ifdef ENABLE_ASYNC_PUSH
2234 /* arm inotify watcher */
2235 if (c->options.mode == MODE_SERVER)
2236 {
2237 event_ctl(c->c2.event_set, c->c2.inotify_fd, EVENT_READ, (void *)file_shift);
2238 }
2239#endif
2240
2241 /*
2242 * Possible scenarios:
2243 * (1) tcp/udp port has data available to read
2244 * (2) tcp/udp port is ready to accept more data to write
2245 * (3) tun dev has data available to read
2246 * (4) tun dev is ready to accept more data to write
2247 * (5) we received a signal (handler sets signal_received)
2248 * (6) timeout (tv) expired
2249 */
2250
2252
2253 if (!c->sig->signal_received)
2254 {
2255 if (!(flags & IOW_CHECK_RESIDUAL) || !sockets_read_residual(c))
2256 {
2257 int status;
2258
2259#ifdef ENABLE_DEBUG
2261 {
2262 show_wait_status(c);
2263 }
2264#endif
2265
2266 /*
2267 * Wait for something to happen.
2268 */
2269 status = event_wait(c->c2.event_set, &c->c2.timeval, esr, SIZE(esr));
2270
2271 check_status(status, "event_wait", NULL, NULL);
2272
2273 if (status > 0)
2274 {
2275 int i;
2276 c->c2.event_set_status = 0;
2277 for (i = 0; i < status; ++i)
2278 {
2279 const struct event_set_return *e = &esr[i];
2280 uintptr_t shift;
2281
2282 if (e->arg >= MULTI_N)
2283 {
2284 struct event_arg *ev_arg = (struct event_arg *)e->arg;
2285 if (ev_arg->type != EVENT_ARG_LINK_SOCKET)
2286 {
2288 msg(D_LINK_ERRORS, "io_work: non socket event delivered");
2289 return;
2290 }
2291
2292 shift = socket_shift;
2293 }
2294 else
2295 {
2296 shift = (uintptr_t)e->arg;
2297 }
2298
2299 c->c2.event_set_status |= ((e->rwflags & 3) << shift);
2300 }
2301 }
2302 else if (status == 0)
2303 {
2305 }
2306 }
2307 else
2308 {
2310 }
2311 }
2312
2313 /* 'now' should always be a reasonably up-to-date timestamp */
2314 update_time();
2315
2316 /* set signal_received if a signal was received */
2317 if (c->c2.event_set_status & ES_ERROR)
2318 {
2320 }
2321
2322 dmsg(D_EVENT_WAIT, "I/O WAIT status=0x%04x", c->c2.event_set_status);
2323}
2324
2325void
2327{
2328 const unsigned int status = c->c2.event_set_status;
2329
2330#ifdef ENABLE_MANAGEMENT
2332 {
2335 }
2336#endif
2337
2338 /* TCP/UDP port ready to accept write */
2339 if (status & SOCKET_WRITE)
2340 {
2342 }
2343 /* TUN device ready to accept write */
2344 else if (status & TUN_WRITE)
2345 {
2347 }
2348 /* Incoming data on TCP/UDP port */
2349 else if (status & SOCKET_READ)
2350 {
2352 if (!IS_SIG(c))
2353 {
2355 }
2356 }
2357 /* Incoming data on TUN device */
2358 else if (status & TUN_READ)
2359 {
2361 if (!IS_SIG(c))
2362 {
2364 }
2365 }
2366 else if (status & DCO_READ)
2367 {
2368 if (!IS_SIG(c))
2369 {
2371 }
2372 }
2373}
void check_send_auth_token(struct context *c)
Checks if the timer to resend the auth-token has expired and if a new auth-token should be send to th...
Definition auth_token.c:415
bool buf_printf(struct buffer *buf, const char *format,...)
Definition buffer.c:241
bool buf_assign(struct buffer *dest, const struct buffer *src)
Definition buffer.c:174
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition buffer.c:89
bool buf_string_match_head_str(const struct buffer *src, const char *match)
Definition buffer.c:778
#define BSTR(buf)
Definition buffer.h:128
#define BPTR(buf)
Definition buffer.h:123
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition buffer.h:672
static bool buf_copy_n(struct buffer *dest, struct buffer *src, int n)
Definition buffer.h:710
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:518
static bool buf_advance(struct buffer *buf, int size)
Definition buffer.h:616
#define BLEN(buf)
Definition buffer.h:126
static void gc_free(struct gc_arena *a)
Definition buffer.h:1025
#define buf_init(buf, offset)
Definition buffer.h:209
static struct gc_arena gc_new(void)
Definition buffer.h:1017
void client_nat_transform(const struct client_nat_option_list *list, struct buffer *ipbuf, const int direction)
Definition clinat.c:184
#define CN_INCOMING
Definition clinat.h:31
#define CN_OUTGOING
Definition clinat.h:30
uint64_t counter_type
Definition common.h:31
int interval_t
Definition common.h:37
#define PUSH_REQUEST_INTERVAL
Definition common.h:94
#define BIG_TIMEOUT
Definition common.h:42
long int get_random(void)
Definition crypto.c:1717
static int dco_do_read(dco_context_t *dco)
Definition dco.h:308
static void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
Definition dco.h:315
static bool dco_update_keys(dco_context_t *dco, struct tls_multi *multi)
Definition dco.h:327
void * dco_context_t
Definition dco.h:258
static int dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
Definition dco.h:374
in_addr_t dhcp_extract_router_msg(struct buffer *ipbuf)
Definition dhcp.c:148
#define D_TUN_RW
Definition errlevel.h:113
#define D_TAP_WIN_DEBUG
Definition errlevel.h:114
#define D_PING
Definition errlevel.h:143
#define D_EVENT_WAIT
Definition errlevel.h:161
#define D_INTERVAL
Definition errlevel.h:157
#define D_STREAM_ERRORS
Definition errlevel.h:62
#define D_DCO_DEBUG
Definition errlevel.h:117
#define D_PUSH_ERRORS
Definition errlevel.h:66
#define D_LOW
Definition errlevel.h:96
#define M_INFO
Definition errlevel.h:54
#define D_SCHED_EXIT
Definition errlevel.h:88
#define D_ROUTE
Definition errlevel.h:79
#define D_LINK_ERRORS
Definition errlevel.h:56
#define D_LINK_RW
Definition errlevel.h:112
#define DCO_READ
Definition event.h:74
#define MANAGEMENT_SHIFT
Definition event.h:68
#define SOCKET_SHIFT
Definition event.h:59
#define TUN_WRITE
Definition event.h:64
#define SOCKET_READ
Definition event.h:60
#define MANAGEMENT_READ
Definition event.h:69
#define MANAGEMENT_WRITE
Definition event.h:70
static int event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition event.h:188
#define ES_ERROR
Definition event.h:66
#define SOCKET_WRITE
Definition event.h:61
#define ES_TIMEOUT
Definition event.h:67
#define EVENT_WRITE
Definition event.h:38
#define MULTI_N
Definition event.h:87
#define TUN_SHIFT
Definition event.h:62
#define EVENT_READ
Definition event.h:37
#define FILE_SHIFT
Definition event.h:71
#define TUN_READ
Definition event.h:63
static void event_reset(struct event_set *es)
Definition event.h:170
static void wait_signal(struct event_set *es, void *arg)
Definition event.h:205
#define DCO_SHIFT
Definition event.h:73
#define ERR_SHIFT
Definition event.h:65
@ 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:182
counter_type link_write_bytes_global
Definition forward.c:48
static void check_inactivity_timeout(struct context *c)
Definition forward.c:479
void reschedule_multi_process(struct context *c)
Reschedule tls_multi_process.
Definition forward.c:390
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition forward.c:503
bool send_control_channel_string(struct context *c, const char *str, msglvl_t msglevel)
Definition forward.c:397
static void check_fragment(struct context *c)
Definition forward.c:571
static void check_tls_errors(struct context *c)
Definition forward.c:97
static void check_scheduled_exit(struct context *c)
Definition forward.c:548
#define MAX_ICMPV6LEN
static void check_session_timeout(struct context *c)
Definition forward.c:707
void process_io(struct context *c, struct link_socket *sock)
Definition forward.c:2326
static void context_reschedule_sec(struct context *c, int sec)
Definition forward.c:130
static void check_timeout_random_component(struct context *c)
Definition forward.c:874
bool schedule_exit(struct context *c)
Definition forward.c:527
static void process_coarse_timers(struct context *c)
Definition forward.c:721
static void check_connection_established(struct context *c)
Definition forward.c:332
static void check_timeout_random_component_dowork(struct context *c)
Definition forward.c:863
static void buffer_turnover(const uint8_t *orig_buf, struct buffer *dest_stub, struct buffer *src_stub, struct buffer *storage)
Definition forward.c:600
static void check_tls_errors_nco(struct context *c)
Definition forward.c:87
void ipv6_send_icmp_unreachable(struct context *c, struct buffer *buf, bool client)
Forges a IPv6 ICMP packet with a no route to host error code from the IPv6 packet in buf and sends it...
Definition forward.c:1541
bool send_control_channel_string_dowork(struct tls_session *session, const char *str, msglvl_t msglevel)
Definition forward.c:369
void get_io_flags_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags)
Definition forward.c:2168
static void multi_io_process_flags(struct context *c, struct event_set *es, const unsigned int flags, unsigned int *out_socket, unsigned int *out_tuntap)
Definition forward.c:2039
void pre_select(struct context *c)
Definition forward.c:1973
static void check_incoming_control_channel(struct context *c)
Definition forward.c:282
static void check_add_routes(struct context *c)
Definition forward.c:429
static void check_server_poll_timeout(struct context *c)
Definition forward.c:511
static void parse_incoming_control_channel_command(struct context *c, struct buffer *buf)
Definition forward.c:233
static void socks_postprocess_incoming_link(struct context *c, struct link_socket *sock)
Definition forward.c:892
static void check_push_request(struct context *c)
Definition forward.c:314
static void process_incoming_link(struct context *c, struct link_socket *sock)
Definition forward.c:1207
static void check_tls_errors_co(struct context *c)
Definition forward.c:80
static void socks_preprocess_outgoing_link(struct context *c, struct link_socket *sock, struct link_socket_actual **to_addr, int *size_delta)
Definition forward.c:901
static void link_socket_write_post_size_adjust(int *size, int size_delta, struct buffer *buf)
Definition forward.c:913
void get_io_flags_dowork_udp(struct context *c, struct multi_io *multi_io, const unsigned int flags)
Definition forward.c:2159
static void context_immediate_reschedule(struct context *c)
Definition forward.c:123
static void check_add_routes_action(struct context *c, const bool errors)
Definition forward.c:414
static void drop_if_recursive_routing(struct context *c, struct buffer *buf)
Drops UDP packets which OS decided to route via tun.
Definition forward.c:1376
counter_type link_read_bytes_global
Definition forward.c:47
static void check_coarse_timers(struct context *c)
Definition forward.c:838
void check_dco_key_status(struct context *c)
Definition forward.c:144
void io_wait_dowork(struct context *c, const unsigned int flags)
Definition forward.c:2193
static void process_incoming_dco(struct context *c)
Definition forward.c:1251
void process_ip_header(struct context *c, unsigned int flags, struct buffer *buf, struct link_socket *sock)
Definition forward.c:1665
void extract_dco_float_peer_addr(const sa_family_t socket_family, struct openvpn_sockaddr *out_osaddr, const struct sockaddr *float_sa)
Transfers float_sa data extracted from an incoming DCO PEER_FLOAT_NTF to out_osaddr for later process...
Definition forward.c:1217
static void check_tls(struct context *c)
Definition forward.c:177
static void check_status_file(struct context *c)
Definition forward.c:557
Interface functions to the internal and external multiplexers.
#define PIP_MSSFIX
Definition forward.h:319
#define PIPV4_CLIENT_NAT
Definition forward.h:322
#define IOW_WAIT_SIGNAL
Definition forward.h:63
#define IOW_READ_TUN
Definition forward.h:56
#define PIPV4_EXTRACT_DHCP_ROUTER
Definition forward.h:321
#define PIPV6_ICMP_NOHOST_SERVER
Definition forward.h:324
#define PIPV6_ICMP_NOHOST_CLIENT
Definition forward.h:323
#define IOW_SHAPER
Definition forward.h:58
#define IOW_FRAG
Definition forward.h:60
#define IOW_MBUF
Definition forward.h:61
static struct link_socket_info * get_link_socket_info(struct context *c)
Definition forward.h:333
#define IOW_READ_LINK
Definition forward.h:57
#define IOW_CHECK_RESIDUAL
Definition forward.h:59
#define TO_LINK_FRAG(c)
Definition forward.h:42
static bool connection_established(struct context *c)
Definition forward.h:408
static void register_activity(struct context *c, const int size)
Definition forward.h:346
#define IOW_TO_TUN
Definition forward.h:54
#define PIPV4_PASSTOS
Definition forward.h:318
#define IOW_READ_TUN_FORCE
Definition forward.h:62
#define PIP_OUTGOING
Definition forward.h:320
#define IOW_TO_LINK
Definition forward.h:55
#define KS_PRIMARY
Primary key state index.
Definition ssl_common.h:465
#define TM_ACTIVE
Active tls_session.
Definition ssl_common.h:545
void encrypt_sign(struct context *c, bool comp_frag)
Process a data channel packet that will be sent through a VPN tunnel.
Definition forward.c:620
void tls_post_encrypt(struct tls_multi *multi, struct buffer *buf)
Perform some accounting for the key state used.
Definition ssl.c:4005
void openvpn_encrypt(struct buffer *buf, struct buffer work, struct crypto_options *opt)
Encrypt and HMAC sign a packet so that it can be sent as a data channel VPN tunnel packet to a remote...
Definition crypto.c:322
void tls_prepend_opcode_v1(const struct tls_multi *multi, struct buffer *buf)
Prepend a one-byte OpenVPN data channel P_DATA_V1 opcode to the packet.
Definition ssl.c:3973
void tls_pre_encrypt(struct tls_multi *multi, struct buffer *buf, struct crypto_options **opt)
Choose the appropriate security parameters with which to process an outgoing packet.
Definition ssl.c:3941
void tls_prepend_opcode_v2(const struct tls_multi *multi, struct buffer *buf)
Prepend an OpenVPN data channel P_DATA_V2 header to the packet.
Definition ssl.c:3991
bool openvpn_decrypt(struct buffer *buf, struct buffer work, struct crypto_options *opt, const struct frame *frame, const uint8_t *ad_start)
HMAC verify and decrypt a data channel packet received from a remote OpenVPN peer.
Definition crypto.c:772
bool process_incoming_link_part1(struct context *c, struct link_socket_info *lsi, bool floated)
Starts processing a packet read from the external network interface.
Definition forward.c:991
void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi, const uint8_t *orig_buf)
Continues processing a packet read from the external network interface.
Definition forward.c:1124
void process_outgoing_link(struct context *c, struct link_socket *sock)
Write a packet to the external network interface.
Definition forward.c:1749
void read_incoming_link(struct context *c, struct link_socket *sock)
Read a packet from the external network interface.
Definition forward.c:930
bool tls_pre_decrypt(struct tls_multi *multi, const struct link_socket_actual *from, struct buffer *buf, struct crypto_options **opt, bool floated, const uint8_t **ad_start)
Determine whether an incoming packet is a data channel or control channel packet, and process accordi...
Definition ssl.c:3562
static void fragment_housekeeping(struct fragment_master *f, struct frame *frame, struct timeval *tv)
Perform housekeeping of a fragment_master structure.
Definition fragment.h:456
static bool fragment_outgoing_defined(struct fragment_master *f)
Check whether a fragment_master structure contains fragments ready to be sent.
Definition fragment.h:428
void fragment_outgoing(struct fragment_master *f, struct buffer *buf, const struct frame *frame)
Process an outgoing packet, which may or may not need to be fragmented.
Definition fragment.c:313
void fragment_incoming(struct fragment_master *f, struct buffer *buf, const struct frame *frame)
Process an incoming packet, which may or may not be fragmented.
Definition fragment.c:139
bool fragment_ready_to_send(struct fragment_master *f, struct buffer *buf, const struct frame *frame)
Check whether outgoing fragments are ready to be send, and if so make one available.
Definition fragment.c:363
void read_incoming_tun(struct context *c)
Read a packet from the virtual tun/tap network interface.
Definition forward.c:1312
void process_incoming_tun(struct context *c, struct link_socket *out_sock)
Process a packet read from the virtual tun/tap network interface.
Definition forward.c:1480
void process_outgoing_tun(struct context *c, struct link_socket *in_sock)
Write a packet to the virtual tun/tap network interface.
Definition forward.c:1883
void initialization_sequence_completed(struct context *c, const unsigned int flags)
Definition init.c:1528
void reset_coarse_timers(struct context *c)
Definition init.c:1296
bool do_up(struct context *c, bool pulled_options, unsigned int option_types_found)
Definition init.c:2332
bool do_route(const struct options *options, struct route_list *route_list, struct route_ipv6_list *route_ipv6_list, const struct tuntap *tt, const struct plugin_list *plugins, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition init.c:1649
#define ISC_ERRORS
Definition init.h:120
#define ISC_ROUTE_ERRORS
Definition init.h:122
static int min_int(int x, int y)
Definition integer.h:105
static int max_int(int x, int y)
Definition integer.h:92
static SERVICE_STATUS status
Definition interactive.c:51
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
This is the principal function for testing and triggering recurring timers.
Definition interval.c:47
#define ETT_DEFAULT
Definition interval.h:222
static void interval_future_trigger(struct interval *top, interval_t wakeup)
Definition interval.h:106
static void event_timeout_reset(struct event_timeout *et)
Resets a timer.
Definition interval.h:187
static void interval_action(struct interval *top)
Definition interval.h:122
static bool event_timeout_defined(const struct event_timeout *et)
Definition interval.h:142
static void event_timeout_init(struct event_timeout *et, interval_t n, const time_t last)
Initialises a timer struct.
Definition interval.h:172
static void event_timeout_clear(struct event_timeout *et)
Clears the timeout and reset all values to 0.
Definition interval.h:153
static void interval_schedule_wakeup(struct interval *top, interval_t *wakeup)
Definition interval.h:92
static void event_timeout_modify_wakeup(struct event_timeout *et, interval_t n)
Sets the interval n of a timeout.
Definition interval.h:204
static bool interval_test(struct interval *top)
Definition interval.h:65
static interval_t event_timeout_remaining(struct event_timeout *et)
Returns the time until the timeout should triggered, from now.
Definition interval.h:217
void management_socket_set(struct management *man, struct event_set *es, void *arg, unsigned int *persistent)
Definition manage.c:3152
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:2789
void management_io(struct management *man)
Definition manage.c:3190
void management_check_bytecount_client(struct context *c, struct management *man, struct timeval *timeval)
Definition manage.c:4164
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4140
#define OPENVPN_STATE_GET_CONFIG
Definition manage.h:459
const char * sanitize_control_message(const char *src, struct gc_arena *gc)
Definition misc.c:656
void mss_fixup_ipv6(struct buffer *buf, uint16_t maxmss)
Definition mss.c:83
void mss_fixup_ipv4(struct buffer *buf, uint16_t maxmss)
Definition mss.c:46
void frame_adjust_path_mtu(struct context *c)
Checks and adjusts the fragment and mssfix value according to the discovered path mtu value.
Definition mss.c:336
void process_received_occ_msg(struct context *c)
Definition occ.c:360
static void check_send_occ_msg(struct context *c)
Definition occ.h:138
static bool is_occ_msg(const struct buffer *buf)
Definition occ.h:83
static void check_send_occ_req(struct context *c)
Definition occ.h:110
static void check_send_occ_load_test(struct context *c)
Definition occ.h:124
#define CLEAR(x)
Definition basic.h:32
#define SIZE(x)
Definition basic.h:29
static bool check_debug_level(msglvl_t level)
Definition error.h:259
#define M_NOPREFIX
Definition error.h:98
#define dmsg(flags,...)
Definition error.h:172
#define openvpn_errno()
Definition error.h:71
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
static void check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
Definition error.h:314
#define TLS_MODE(c)
Definition openvpn.h:542
#define PROTO_DUMP(buf, gc)
Definition openvpn.h:544
#define MODE_POINT_TO_POINT
Definition options.h:263
#define MODE_SERVER
Definition options.h:264
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:995
time_t now
Definition otime.c:33
const char * tv_string(const struct timeval *tv, struct gc_arena *gc)
Definition otime.c:83
static void update_time(void)
Definition otime.h:81
static void tv_add(struct timeval *dest, const struct timeval *src)
Definition otime.h:136
@ OVPN_DEL_PEER_REASON_EXPIRED
@ OVPN_CMD_SWAP_KEYS
@ OVPN_CMD_DEL_PEER
void packet_id_persist_save(struct packet_id_persist *p)
Definition packet_id.c:508
static bool packet_id_persist_enabled(const struct packet_id_persist *p)
Definition packet_id.h:278
void trigger_ping_timeout_signal(struct context *c)
Trigger the correct signal on a –ping timeout depending if –ping-exit is set (SIGTERM) or not (SIGUSR...
Definition ping.c:45
static void check_ping_restart(struct context *c)
Definition ping.h:59
static bool is_ping_msg(const struct buffer *buf)
Definition ping.h:40
static void check_ping_send(struct context *c)
Definition ping.h:76
bool is_ipv4(int tunnel_type, struct buffer *buf)
Definition proto.c:108
bool is_ipv6(int tunnel_type, struct buffer *buf)
Definition proto.c:113
uint16_t ip_checksum(const sa_family_t af, const uint8_t *payload, const int len_payload, const uint8_t *src_addr, const uint8_t *dest_addr, const int proto)
Calculates an IP or IPv6 checksum with a pseudo header as required by TCP, UDP and ICMPv6.
Definition proto.c:120
static int get_tun_ip_ver(int tunnel_type, struct buffer *buf, int *ip_hdr_offset)
Definition proto.h:251
#define DEV_TYPE_TAP
Definition proto.h:36
#define OPENVPN_ETH_ALEN
Definition proto.h:52
#define OPENVPN_ETH_P_IPV6
Definition proto.h:59
#define OPENVPN_IPPROTO_ICMPV6
Definition proto.h:107
#define OPENVPN_IN6_ARE_ADDR_EQUAL(a, b)
Version of IN6_ARE_ADDR_EQUAL that is guaranteed to work for unaligned access.
Definition proto.h:87
#define OPENVPN_IPPROTO_UDP
Definition proto.h:106
#define OPENVPN_ICMP6_DESTINATION_UNREACHABLE
Definition proto.h:136
#define OPENVPN_IPPROTO_TCP
Definition proto.h:105
#define OPENVPN_ICMP6_DU_NOROUTE
Definition proto.h:144
void receive_auth_pending(struct context *c, const struct buffer *buffer)
Parses an AUTH_PENDING message and if in pull mode extends the timeout.
Definition push.c:336
void receive_auth_failed(struct context *c, const struct buffer *buffer)
Definition push.c:48
void server_pushed_signal(struct context *c, const struct buffer *buffer, const bool restart, const int adv)
Definition push.c:128
void receive_cr_response(struct context *c, const struct buffer *buffer)
Definition push.c:263
void send_auth_failed(struct context *c, const char *client_reason)
Definition push.c:393
void receive_exit_message(struct context *c)
Definition push.c:189
bool send_push_request(struct context *c)
Definition push.c:567
void incoming_push_message(struct context *c, const struct buffer *buffer)
Definition push.c:505
void server_pushed_info(const struct buffer *buffer, const int adv)
Definition push.c:225
bool test_routes(const struct route_list *rl, const struct tuntap *tt)
Definition route.c:2439
void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr)
Definition route.c:535
void show_routes(msglvl_t msglevel)
Definition route.c:3067
bool shaper_soonest_event(struct timeval *tv, int delay)
Definition shaper.c:36
static void shaper_wrote_bytes(struct shaper *s, int nbytes)
Definition shaper.h:121
static int shaper_delay(struct shaper *s)
Definition shaper.h:96
void print_status(struct context *c, struct status_output *so)
Definition sig.c:478
void process_explicit_exit_notification_timer_wakeup(struct context *c)
Definition sig.c:563
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:228
#define IS_SIG(c)
Definition sig.h:47
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition sig.h:109
const char * socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:2068
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:2862
unsigned int socket_set(struct link_socket *s, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:2955
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:1968
bool sockets_read_residual(const struct context *c)
Definition socket.c:45
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:706
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition socket.h:408
static bool socket_connection_reset(const struct link_socket *sock, int status)
Definition socket.h:443
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:466
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:513
static int link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:587
static void link_socket_get_outgoing_addr(struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
Definition socket.h:493
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
static bool link_socket_actual_defined(const struct link_socket_actual *act)
static int datagram_overhead(sa_family_t af, int proto)
@ PROTO_UDP
@ PROTO_TCP
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP)
#define IA_NET_ORDER
Definition socket_util.h:90
int socks_process_outgoing_udp(struct buffer *buf, const struct link_socket_actual *to)
Definition socks.c:481
void socks_process_incoming_udp(struct buffer *buf, struct link_socket_actual *from)
Definition socks.c:443
bool tls_send_payload(struct key_state *ks, const uint8_t *data, size_t size)
Definition ssl.c:4025
bool tls_rec_payload(struct tls_multi *multi, struct buffer *buf)
Definition ssl.c:4058
void tls_session_soft_reset(struct tls_multi *tls_multi)
Definition ssl.c:1769
int tls_multi_process(struct tls_multi *multi, struct buffer *to_link, struct link_socket_actual **to_link_addr, struct link_socket_info *to_link_socket_info, interval_t *wakeup)
Definition ssl.c:3205
#define TLSMP_RECONNECT
Definition ssl.h:232
#define TLSMP_ACTIVE
Definition ssl.h:230
static bool tls_initial_packet_received(const struct tls_multi *multi)
Definition ssl.h:490
static void tls_set_single_session(struct tls_multi *multi)
Definition ssl.h:510
#define TLSMP_KILL
Definition ssl.h:231
static int tls_test_payload_len(const struct tls_multi *multi)
Definition ssl.h:496
@ CAS_CONNECT_DONE
Definition ssl_common.h:594
struct buffer extract_command_buffer(struct buffer *buf, struct gc_arena *gc)
Extracts a control channel message from buf and adjusts the size of buf after the message has been ex...
Definition ssl_pkt.c:573
#define P_DATA_V1
Definition ssl_pkt.h:47
#define P_OPCODE_SHIFT
Definition ssl_pkt.h:39
Control Channel Verification Module.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:67
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
int mssfix
Definition options.h:144
int connect_timeout
Definition options.h:121
struct status_output * status_output
Definition openvpn.h:185
struct route_list * route_list
List of routing information.
Definition openvpn.h:177
int link_sockets_num
Definition openvpn.h:158
struct route_ipv6_list * route_ipv6_list
Definition openvpn.h:182
struct packet_id_persist pid_persist
Definition openvpn.h:170
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:172
counter_type link_read_bytes
Definition openvpn.h:266
counter_type link_write_bytes
Definition openvpn.h:269
struct event_timeout server_poll_interval
Definition openvpn.h:408
int max_recv_size_local
Definition openvpn.h:308
struct fragment_master * fragment
Definition openvpn.h:252
time_t update_timeout_random_component
Definition openvpn.h:403
unsigned int event_set_status
Definition openvpn.h:235
counter_type dco_read_bytes
Definition openvpn.h:267
struct event_timeout route_wakeup_expire
Definition openvpn.h:384
struct event_timeout ping_send_interval
Definition openvpn.h:283
int max_send_size_local
Definition openvpn.h:310
bool did_open_tun
Definition openvpn.h:387
struct timeval timeout_random_component
Definition openvpn.h:404
counter_type tun_read_bytes
Definition openvpn.h:264
counter_type dco_write_bytes
Definition openvpn.h:270
struct event_timeout scheduled_exit
Definition openvpn.h:447
struct env_set * es
Definition openvpn.h:420
struct interval tmp_int
Definition openvpn.h:344
struct event_timeout auth_token_renewal_interval
Definition openvpn.h:293
struct event_timeout wait_for_connect
Definition openvpn.h:282
struct shaper shaper
Definition openvpn.h:259
struct event_timeout push_request_interval
Definition openvpn.h:439
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:323
time_t coarse_timer_wakeup
Definition openvpn.h:399
int scheduled_exit_signal
Definition openvpn.h:448
struct frame frame
Definition openvpn.h:248
struct link_socket_actual from
Definition openvpn.h:245
struct frame frame_fragment
Definition openvpn.h:253
struct buffer to_link
Definition openvpn.h:377
int64_t inactivity_bytes
Definition openvpn.h:288
struct crypto_options crypto_options
Security parameters and crypto state used by the Data Channel Crypto module to process data channel p...
Definition openvpn.h:349
struct buffer to_tun
Definition openvpn.h:376
counter_type tun_write_bytes
Definition openvpn.h:265
struct link_socket ** link_sockets
Definition openvpn.h:237
counter_type link_read_bytes_auth
Definition openvpn.h:268
struct event_timeout packet_id_persist_interval
Definition openvpn.h:355
struct link_socket_actual * to_link_addr
Definition openvpn.h:244
struct event_timeout session_interval
Definition openvpn.h:290
int original_recv_size
Definition openvpn.h:307
struct buffer buf
Definition openvpn.h:375
struct timeval timeval
Time to next event of timers and similar.
Definition openvpn.h:396
time_t explicit_exit_notification_time_wait
Definition openvpn.h:416
bool log_rw
Definition openvpn.h:380
bool fast_io
Definition openvpn.h:424
struct event_set * event_set
Definition openvpn.h:230
struct context_buffers * buffers
Definition openvpn.h:367
struct event_timeout route_wakeup
Definition openvpn.h:383
int tls_exit_signal
Definition openvpn.h:347
struct event_timeout inactivity_interval
Definition openvpn.h:287
struct event_timeout ping_rec_interval
Definition openvpn.h:284
struct buffer read_link_buf
Definition openvpn.h:113
struct buffer encrypt_buf
Definition openvpn.h:100
struct buffer read_tun_buf
Definition openvpn.h:114
struct buffer decrypt_buf
Definition openvpn.h:101
int restart_sleep_seconds
Definition openvpn.h:122
Contains all state information for one tunnel.
Definition openvpn.h:474
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:503
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:501
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:505
struct context_2 c2
Level 2 context.
Definition openvpn.h:517
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:475
struct context_1 c1
Level 1 context.
Definition openvpn.h:516
struct context_persist persist
Persistent context.
Definition openvpn.h:513
Security parameter state for processing data channel packets.
Definition crypto.h:293
struct link_socket * sock
Definition event.h:147
event_arg_t type
Definition event.h:143
unsigned int rwflags
Definition event.h:125
interval_t n
periodic interval for periodic timeouts
Definition interval.h:137
int tun_mtu
the (user) configured tun-mtu.
Definition mtu.h:137
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:108
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:114
struct frame::@8 buf
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Security parameter state of one TLS and data channel key session.
Definition ssl_common.h:208
struct event_set * es
Definition multi_io.h:53
unsigned int udp_flags
Definition multi_io.h:58
uint8_t dest[OPENVPN_ETH_ALEN]
Definition proto.h:55
uint16_t proto
Definition proto.h:62
uint8_t source[OPENVPN_ETH_ALEN]
Definition proto.h:56
uint8_t icmp6_type
Definition proto.h:143
uint16_t icmp6_cksum
Definition proto.h:147
uint8_t icmp6_code
Definition proto.h:146
uint32_t saddr
Definition proto.h:111
uint32_t daddr
Definition proto.h:112
uint8_t protocol
Definition proto.h:108
uint8_t version_prio
Definition proto.h:121
struct in6_addr saddr
Definition proto.h:127
struct in6_addr daddr
Definition proto.h:128
uint8_t nexthdr
Definition proto.h:124
uint16_t payload_len
Definition proto.h:123
union openvpn_sockaddr::@27 addr
struct sockaddr sa
Definition socket_util.h:42
struct sockaddr_in in4
Definition socket_util.h:43
struct sockaddr_in6 in6
Definition socket_util.h:44
const char * ifconfig_ipv6_remote
Definition options.h:330
int scheduled_exit_interval
Definition options.h:565
int shaper
Definition options.h:333
bool allow_recursive_routing
Definition options.h:720
int64_t inactivity_minimum_bytes
Definition options.h:345
int inactivity_timeout
Definition options.h:344
struct connection_entry ce
Definition options.h:293
int mode
Definition options.h:265
bool block_ipv6
Definition options.h:438
int ping_rec_timeout
Definition options.h:350
int ping_send_timeout
Definition options.h:349
bool route_gateway_via_dhcp
Definition options.h:440
struct client_nat_option_list * client_nat
Definition options.h:442
int session_timeout
Definition options.h:347
volatile int signal_received
Definition sig.h:42
bool is_handle
Definition socket.h:261
struct event_timeout et
Definition status.h:62
int n_hard_errors
Definition ssl_common.h:638
enum multi_status multi_state
Definition ssl_common.h:633
struct tls_session session[TM_SIZE]
Array of tls_session objects representing control channel sessions with the remote peer.
Definition ssl_common.h:712
char * client_reason
An error message to send to client on AUTH_FAILED.
Definition ssl_common.h:667
bool use_peer_id
Definition ssl_common.h:701
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
Definition ssl_common.h:724
int n_soft_errors
Definition ssl_common.h:639
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:490
Definition tun.h:183
enum tun_driver_type backend_driver
The backend driver that used for this tun/tap device.
Definition tun.h:193
HANDLE hand
Definition tun.h:218
struct overlapped_io reads
Definition tun.h:221
dco_context_t dco
Definition tun.h:249
char * actual_name
Definition tun.h:207
unsigned short sa_family_t
Definition syshead.h:396
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:131
void tun_show_debug(struct tuntap *tt)
Definition tun.c:6283
const char * tun_stat(const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc)
Definition tun.c:731
int tun_write_win32(struct tuntap *tt, struct buffer *buf)
Definition tun.c:3615
bool tun_standby(struct tuntap *tt)
Definition tun.c:5538
void show_adapters(msglvl_t msglevel)
Definition tun.c:4879
static bool tuntap_abort(int status)
Definition tun.h:490
#define TUNNEL_TYPE(tt)
Definition tun.h:184
static bool tuntap_defined(const struct tuntap *tt)
Definition tun.h:254
@ DRIVER_AFUNIX
using an AF_UNIX socket to pass packets from/to an external program.
Definition tun.h:51
static void tun_set(struct tuntap *tt, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition tun.h:598
int read_tun(struct tuntap *tt, uint8_t *buf, int len)
static bool tuntap_is_dco_win_timeout(struct tuntap *tt, int status)
Definition tun.h:538
static bool tuntap_stop(int status)
Definition tun.h:476
int write_tun(struct tuntap *tt, uint8_t *buf, int len)
ssize_t read_tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
Reads a packet from a AF_UNIX based tun device.
Definition tun_afunix.c:158
ssize_t write_tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
Writes a packet to a AF_UNIX based tun device.
Definition tun_afunix.c:145