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