OpenVPN
mtcp.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-2023 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 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 
32 #include "multi.h"
33 #include "forward.h"
34 
35 #include "memdbg.h"
36 
37 #ifdef HAVE_SYS_INOTIFY_H
38 #include <sys/inotify.h>
39 #endif
40 
41 /*
42  * TCP States
43  */
44 #define TA_UNDEF 0
45 #define TA_SOCKET_READ 1
46 #define TA_SOCKET_READ_RESIDUAL 2
47 #define TA_SOCKET_WRITE 3
48 #define TA_SOCKET_WRITE_READY 4
49 #define TA_SOCKET_WRITE_DEFERRED 5
50 #define TA_TUN_READ 6
51 #define TA_TUN_WRITE 7
52 #define TA_INITIAL 8
53 #define TA_TIMEOUT 9
54 #define TA_TUN_WRITE_TIMEOUT 10
55 
56 /*
57  * Special tags passed to event.[ch] functions
58  */
59 #define MTCP_SOCKET ((void *)1)
60 #define MTCP_TUN ((void *)2)
61 #define MTCP_SIG ((void *)3) /* Only on Windows */
62 #define MTCP_MANAGEMENT ((void *)4)
63 #define MTCP_FILE_CLOSE_WRITE ((void *)5)
64 #define MTCP_DCO ((void *)6)
65 
66 #define MTCP_N ((void *)16) /* upper bound on MTCP_x */
67 
69 {
70  unsigned int flags;
71  unsigned int ret;
72  unsigned int tun;
73  unsigned int sock;
74 };
75 
76 #ifdef ENABLE_DEBUG
77 static const char *
78 pract(int action)
79 {
80  switch (action)
81  {
82  case TA_UNDEF:
83  return "TA_UNDEF";
84 
85  case TA_SOCKET_READ:
86  return "TA_SOCKET_READ";
87 
89  return "TA_SOCKET_READ_RESIDUAL";
90 
91  case TA_SOCKET_WRITE:
92  return "TA_SOCKET_WRITE";
93 
95  return "TA_SOCKET_WRITE_READY";
96 
98  return "TA_SOCKET_WRITE_DEFERRED";
99 
100  case TA_TUN_READ:
101  return "TA_TUN_READ";
102 
103  case TA_TUN_WRITE:
104  return "TA_TUN_WRITE";
105 
106  case TA_INITIAL:
107  return "TA_INITIAL";
108 
109  case TA_TIMEOUT:
110  return "TA_TIMEOUT";
111 
113  return "TA_TUN_WRITE_TIMEOUT";
114 
115  default:
116  return "?";
117  }
118 }
119 #endif /* ENABLE_DEBUG */
120 
121 static struct multi_instance *
123 {
124  struct gc_arena gc = gc_new();
125  struct multi_instance *mi = NULL;
126  struct hash *hash = m->hash;
127 
128  mi = multi_create_instance(m, NULL);
129  if (mi)
130  {
131  struct hash_element *he;
132  const uint32_t hv = hash_value(hash, &mi->real);
133  struct hash_bucket *bucket = hash_bucket(hash, hv);
134 
135  multi_assign_peer_id(m, mi);
136 
137  he = hash_lookup_fast(hash, bucket, &mi->real, hv);
138 
139  if (he)
140  {
141  struct multi_instance *oldmi = (struct multi_instance *) he->value;
142  msg(D_MULTI_LOW, "MULTI TCP: new incoming client address matches existing client address -- new client takes precedence");
143  oldmi->did_real_hash = false;
144  multi_close_instance(m, oldmi, false);
145  he->key = &mi->real;
146  he->value = mi;
147  }
148  else
149  {
150  hash_add_fast(hash, bucket, &mi->real, hv, mi);
151  }
152 
153  mi->did_real_hash = true;
154  }
155 
156 #ifdef ENABLE_DEBUG
157  if (mi)
158  {
159  dmsg(D_MULTI_DEBUG, "MULTI TCP: instance added: %s", mroute_addr_print(&mi->real, &gc));
160  }
161  else
162  {
163  dmsg(D_MULTI_DEBUG, "MULTI TCP: new client instance failed");
164  }
165 #endif
166 
167  gc_free(&gc);
168  ASSERT(!(mi && mi->halt));
169  return mi;
170 }
171 
172 bool
174 {
175  /* buffer for queued TCP socket output packets */
177 
181  ASSERT(mi->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_family == AF_INET
182  || mi->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_family == AF_INET6
183  );
185  {
186  msg(D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined");
187  return false;
188  }
189  return true;
190 }
191 
192 void
194 {
196 }
197 
198 struct multi_tcp *
199 multi_tcp_init(int maxevents, int *maxclients)
200 {
201  struct multi_tcp *mtcp;
202  const int extra_events = BASE_N_EVENTS;
203 
204  ASSERT(maxevents >= 1);
205  ASSERT(maxclients);
206 
207  ALLOC_OBJ_CLEAR(mtcp, struct multi_tcp);
208  mtcp->maxevents = maxevents + extra_events;
209  mtcp->es = event_set_init(&mtcp->maxevents, 0);
210  wait_signal(mtcp->es, MTCP_SIG);
211  ALLOC_ARRAY(mtcp->esr, struct event_set_return, mtcp->maxevents);
212  *maxclients = max_int(min_int(mtcp->maxevents - extra_events, *maxclients), 1);
213  msg(D_MULTI_LOW, "MULTI: TCP INIT maxclients=%d maxevents=%d", *maxclients, mtcp->maxevents);
214  return mtcp;
215 }
216 
217 void
219 {
220  if (mtcp && mtcp->es)
221  {
222  event_del(mtcp->es, event);
223  }
224 }
225 
226 void
228 {
229  if (mtcp)
230  {
231  event_free(mtcp->es);
232  free(mtcp->esr);
233  free(mtcp);
234  }
235 }
236 
237 void
239 {
240  struct link_socket *ls = mi->context.c2.link_socket;
241  if (ls && mi->socket_set_called)
242  {
243  event_del(mtcp->es, socket_event_handle(ls));
244  mi->socket_set_called = false;
245  }
246  mtcp->n_esr = 0;
247 }
248 
249 static inline void
251 {
252  if (mi)
253  {
254  mi->socket_set_called = true;
256  m->mtcp->es,
258  mi,
259  &mi->tcp_rwflags);
260  }
261 }
262 
263 static inline int
264 multi_tcp_wait(const struct context *c,
265  struct multi_tcp *mtcp)
266 {
267  int status;
268  unsigned int *persistent = &mtcp->tun_rwflags;
270 
271 #ifdef _WIN32
272  if (tuntap_is_wintun(c->c1.tuntap))
273  {
274  if (!tuntap_ring_empty(c->c1.tuntap))
275  {
276  /* there is data in wintun ring buffer, read it immediately */
277  mtcp->esr[0].arg = MTCP_TUN;
278  mtcp->esr[0].rwflags = EVENT_READ;
279  mtcp->n_esr = 1;
280  return 1;
281  }
282  persistent = NULL;
283  }
284 #endif
285  tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
286 #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
287  dco_event_set(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO);
288 #endif
289 
290 #ifdef ENABLE_MANAGEMENT
291  if (management)
292  {
294  }
295 #endif
296 
297 #ifdef ENABLE_ASYNC_PUSH
298  /* arm inotify watcher */
299  event_ctl(mtcp->es, c->c2.inotify_fd, EVENT_READ, MTCP_FILE_CLOSE_WRITE);
300 #endif
301 
302  status = event_wait(mtcp->es, &c->c2.timeval, mtcp->esr, mtcp->maxevents);
303  update_time();
304  mtcp->n_esr = 0;
305  if (status > 0)
306  {
307  mtcp->n_esr = status;
308  }
309  return status;
310 }
311 
312 static inline struct context *
314 {
315  if (mi)
316  {
317  return &mi->context;
318  }
319  else
320  {
321  return &m->top;
322  }
323 }
324 
325 static bool
326 multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
327 {
328  struct mbuf_item item;
329  bool ret = true;
330  ASSERT(mi);
331 
332  /* extract from queue */
333  if (mbuf_extract_item(mi->tcp_link_out_deferred, &item)) /* ciphertext IP packet */
334  {
335  dmsg(D_MULTI_TCP, "MULTI TCP: transmitting previously deferred packet");
336 
337  ASSERT(mi == item.instance);
338  mi->context.c2.to_link = item.buffer->buf;
339  ret = multi_process_outgoing_link_dowork(m, mi, mpp_flags);
340  if (!ret)
341  {
342  mi = NULL;
343  }
344  mbuf_free_buf(item.buffer);
345  }
346  return ret;
347 }
348 
349 static bool
350 multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const unsigned int mpp_flags)
351 {
353  bool ret = true;
354 
355  if (mi)
356  {
357  if (defer || mbuf_defined(mi->tcp_link_out_deferred))
358  {
359  /* save to queue */
360  struct buffer *buf = &mi->context.c2.to_link;
361  if (BLEN(buf) > 0)
362  {
363  struct mbuf_buffer *mb = mbuf_alloc_buf(buf);
364  struct mbuf_item item;
365 
366  set_prefix(mi);
367  dmsg(D_MULTI_TCP, "MULTI TCP: queuing deferred packet");
368  item.buffer = mb;
369  item.instance = mi;
371  mbuf_free_buf(mb);
372  buf_reset(buf);
373  ret = multi_process_post(m, mi, mpp_flags);
374  if (!ret)
375  {
376  mi = NULL;
377  }
378  clear_prefix();
379  }
380  }
381  else
382  {
383  ret = multi_process_outgoing_link_dowork(m, mi, mpp_flags);
384  if (!ret)
385  {
386  mi = NULL;
387  }
388  }
389  }
390  return ret;
391 }
392 
393 static int
394 multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, const int action, bool *tun_input_pending)
395 {
396  struct context *c = multi_tcp_context(m, mi);
397  unsigned int looking_for = 0;
398 
399  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_wait_lite a=%s mi=" ptr_format,
400  pract(action),
401  (ptr_type)mi);
402 
403  tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */
404 
405  switch (action)
406  {
407  case TA_TUN_READ:
408  looking_for = TUN_READ;
409  tun_input_pending = NULL;
410  io_wait(c, IOW_READ_TUN);
411  break;
412 
413  case TA_SOCKET_READ:
414  looking_for = SOCKET_READ;
415  tun_input_pending = NULL;
417  break;
418 
419  case TA_TUN_WRITE:
420  looking_for = TUN_WRITE;
421  tun_input_pending = NULL;
422  c->c2.timeval.tv_sec = 1; /* For some reason, the Linux 2.2 TUN/TAP driver hits this timeout */
424  io_wait(c, IOW_TO_TUN);
425  perf_pop();
426  break;
427 
428  case TA_SOCKET_WRITE:
429  looking_for = SOCKET_WRITE;
431  break;
432 
433  default:
434  msg(M_FATAL, "MULTI TCP: multi_tcp_wait_lite, unhandled action=%d", action);
435  }
436 
437  if (tun_input_pending && (c->c2.event_set_status & TUN_READ))
438  {
439  *tun_input_pending = true;
440  }
441 
442  if (c->c2.event_set_status & looking_for)
443  {
444  return action;
445  }
446  else
447  {
448  switch (action)
449  {
450  /* TCP socket output buffer is full */
451  case TA_SOCKET_WRITE:
453 
454  /* TUN device timed out on accepting write */
455  case TA_TUN_WRITE:
456  return TA_TUN_WRITE_TIMEOUT;
457  }
458 
459  return TA_UNDEF;
460  }
461 }
462 
463 static struct multi_instance *
464 multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int action)
465 {
466  const unsigned int mpp_flags = MPP_PRE_SELECT|MPP_RECORD_TOUCH;
467  struct multi_instance *touched = mi;
468  m->mpp_touched = &touched;
469 
470  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_dispatch a=%s mi=" ptr_format,
471  pract(action),
472  (ptr_type)mi);
473 
474  switch (action)
475  {
476  case TA_TUN_READ:
477  read_incoming_tun(&m->top);
478  if (!IS_SIG(&m->top))
479  {
480  multi_process_incoming_tun(m, mpp_flags);
481  }
482  break;
483 
484  case TA_SOCKET_READ:
486  ASSERT(mi);
487  ASSERT(mi->context.c2.link_socket);
488  set_prefix(mi);
489  read_incoming_link(&mi->context);
490  clear_prefix();
491  if (!IS_SIG(&mi->context))
492  {
493  multi_process_incoming_link(m, mi, mpp_flags);
494  if (!IS_SIG(&mi->context))
495  {
496  stream_buf_read_setup(mi->context.c2.link_socket);
497  }
498  }
499  break;
500 
501  case TA_TIMEOUT:
502  multi_process_timeout(m, mpp_flags);
503  break;
504 
505  case TA_TUN_WRITE:
506  multi_process_outgoing_tun(m, mpp_flags);
507  break;
508 
510  multi_process_drop_outgoing_tun(m, mpp_flags);
511  break;
512 
514  ASSERT(mi);
515  multi_tcp_process_outgoing_link_ready(m, mi, mpp_flags);
516  break;
517 
518  case TA_SOCKET_WRITE:
519  multi_tcp_process_outgoing_link(m, false, mpp_flags);
520  break;
521 
523  multi_tcp_process_outgoing_link(m, true, mpp_flags);
524  break;
525 
526  case TA_INITIAL:
527  ASSERT(mi);
529  multi_process_post(m, mi, mpp_flags);
530  break;
531 
532  default:
533  msg(M_FATAL, "MULTI TCP: multi_tcp_dispatch, unhandled action=%d", action);
534  }
535 
536  m->mpp_touched = NULL;
537  return touched;
538 }
539 
540 static int
541 multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int action)
542 {
543  struct context *c = multi_tcp_context(m, mi);
544  int newaction = TA_UNDEF;
545 
546 #define MTP_NONE 0
547 #define MTP_TUN_OUT (1<<0)
548 #define MTP_LINK_OUT (1<<1)
549  unsigned int flags = MTP_NONE;
550 
551  if (TUN_OUT(c))
552  {
553  flags |= MTP_TUN_OUT;
554  }
555  if (LINK_OUT(c))
556  {
557  flags |= MTP_LINK_OUT;
558  }
559 
560  switch (flags)
561  {
563  case MTP_TUN_OUT:
564  newaction = TA_TUN_WRITE;
565  break;
566 
567  case MTP_LINK_OUT:
568  newaction = TA_SOCKET_WRITE;
569  break;
570 
571  case MTP_NONE:
572  if (mi && socket_read_residual(c->c2.link_socket))
573  {
574  newaction = TA_SOCKET_READ_RESIDUAL;
575  }
576  else
577  {
579  }
580  break;
581 
582  default:
583  {
584  struct gc_arena gc = gc_new();
585  msg(M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=%s flags=%d",
586  multi_instance_string(mi, false, &gc),
587  flags);
588  gc_free(&gc);
589  break;
590  }
591  }
592 
593  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_post %s -> %s",
594  pract(action),
595  pract(newaction));
596 
597  return newaction;
598 }
599 
600 static void
601 multi_tcp_action(struct multi_context *m, struct multi_instance *mi, int action, bool poll)
602 {
603  bool tun_input_pending = false;
604 
605  do
606  {
607  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_action a=%s p=%d",
608  pract(action),
609  poll);
610 
611  /*
612  * If TA_SOCKET_READ_RESIDUAL, it means we still have pending
613  * input packets which were read by a prior TCP recv.
614  *
615  * Otherwise do a "lite" wait, which means we wait with 0 timeout
616  * on I/O events only related to the current instance, not
617  * the big list of events.
618  *
619  * On our first pass, poll will be false because we already know
620  * that input is available, and to call io_wait would be redundant.
621  */
622  if (poll && action != TA_SOCKET_READ_RESIDUAL)
623  {
624  const int orig_action = action;
625  action = multi_tcp_wait_lite(m, mi, action, &tun_input_pending);
626  if (action == TA_UNDEF)
627  {
628  msg(M_FATAL, "MULTI TCP: I/O wait required blocking in multi_tcp_action, action=%d", orig_action);
629  }
630  }
631 
632  /*
633  * Dispatch the action
634  */
635  struct multi_instance *touched = multi_tcp_dispatch(m, mi, action);
636 
637  /*
638  * Signal received or TCP connection
639  * reset by peer?
640  */
641  if (touched && IS_SIG(&touched->context))
642  {
643  if (mi == touched)
644  {
645  mi = NULL;
646  }
647  multi_close_instance_on_signal(m, touched);
648  }
649 
650 
651  /*
652  * If dispatch produced any pending output
653  * for a particular instance, point to
654  * that instance.
655  */
656  if (m->pending)
657  {
658  mi = m->pending;
659  }
660 
661  /*
662  * Based on the effects of the action,
663  * such as generating pending output,
664  * possibly transition to a new action state.
665  */
666  action = multi_tcp_post(m, mi, action);
667 
668  /*
669  * If we are finished processing the original action,
670  * check if we have any TUN input. If so, transition
671  * our action state to processing this input.
672  */
673  if (tun_input_pending && action == TA_UNDEF)
674  {
675  action = TA_TUN_READ;
676  mi = NULL;
677  tun_input_pending = false;
678  poll = false;
679  }
680  else
681  {
682  poll = true;
683  }
684 
685  } while (action != TA_UNDEF);
686 }
687 
688 static void
690 {
691  struct multi_tcp *mtcp = m->mtcp;
692  int i;
693 
694  for (i = 0; i < mtcp->n_esr; ++i)
695  {
696  struct event_set_return *e = &mtcp->esr[i];
697 
698  /* incoming data for instance? */
699  if (e->arg >= MTCP_N)
700  {
701  struct multi_instance *mi = (struct multi_instance *) e->arg;
702  if (mi)
703  {
704  if (e->rwflags & EVENT_WRITE)
705  {
707  }
708  else if (e->rwflags & EVENT_READ)
709  {
710  multi_tcp_action(m, mi, TA_SOCKET_READ, false);
711  }
712  }
713  }
714  else
715  {
716 #ifdef ENABLE_MANAGEMENT
717  if (e->arg == MTCP_MANAGEMENT)
718  {
721  }
722  else
723 #endif
724  /* incoming data on TUN? */
725  if (e->arg == MTCP_TUN)
726  {
727  if (e->rwflags & EVENT_WRITE)
728  {
729  multi_tcp_action(m, NULL, TA_TUN_WRITE, false);
730  }
731  else if (e->rwflags & EVENT_READ)
732  {
733  multi_tcp_action(m, NULL, TA_TUN_READ, false);
734  }
735  }
736  /* new incoming TCP client attempting to connect? */
737  else if (e->arg == MTCP_SOCKET)
738  {
739  struct multi_instance *mi;
740  ASSERT(m->top.c2.link_socket);
743  if (mi)
744  {
745  multi_tcp_action(m, mi, TA_INITIAL, false);
746  }
747  }
748 #if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
749  /* incoming data on DCO? */
750  else if (e->arg == MTCP_DCO)
751  {
753  }
754 #endif
755  /* signal received? */
756  else if (e->arg == MTCP_SIG)
757  {
759  }
760 #ifdef ENABLE_ASYNC_PUSH
761  else if (e->arg == MTCP_FILE_CLOSE_WRITE)
762  {
763  multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_TOUCH);
764  }
765 #endif
766  }
767  if (IS_SIG(&m->top))
768  {
769  break;
770  }
771  }
772  mtcp->n_esr = 0;
773 
774  /*
775  * Process queued mbuf packets destined for TCP socket
776  */
777  {
778  struct multi_instance *mi;
779  while (!IS_SIG(&m->top) && (mi = mbuf_peek(m->mbuf)) != NULL)
780  {
781  multi_tcp_action(m, mi, TA_SOCKET_WRITE, true);
782  }
783  }
784 }
785 
786 /*
787  * Top level event loop for single-threaded operation.
788  * TCP mode.
789  */
790 void
792 {
793  struct multi_context multi;
794  int status;
795 
796  top->mode = CM_TOP;
798 
799  /* initialize top-tunnel instance */
801  if (IS_SIG(top))
802  {
803  return;
804  }
805 
806  /* initialize global multi_context object */
807  multi_init(&multi, top, true);
808 
809  /* initialize our cloned top object */
810  multi_top_init(&multi, top);
811 
812  /* initialize management interface */
814 
815  /* finished with initialization */
816  initialization_sequence_completed(top, ISC_SERVER); /* --mode server --proto tcp-server */
817 
818 #ifdef ENABLE_ASYNC_PUSH
819  multi.top.c2.inotify_fd = inotify_init();
820  if (multi.top.c2.inotify_fd < 0)
821  {
822  msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error");
823  }
824 #endif
825 
826  /* per-packet event loop */
827  while (true)
828  {
830 
831  /* wait on tun/socket list */
832  multi_get_timeout(&multi, &multi.top.c2.timeval);
833  status = multi_tcp_wait(&multi.top, multi.mtcp);
834  MULTI_CHECK_SIG(&multi);
835 
836  /* check on status of coarse timers */
838 
839  /* timeout? */
840  if (status > 0)
841  {
842  /* process the I/O which triggered select */
843  multi_tcp_process_io(&multi);
844  MULTI_CHECK_SIG(&multi);
845  }
846  else if (status == 0)
847  {
848  multi_tcp_action(&multi, NULL, TA_TIMEOUT, false);
849  }
850 
851  perf_pop();
852  }
853 
854 #ifdef ENABLE_ASYNC_PUSH
855  close(top->c2.inotify_fd);
856 #endif
857 
858  /* shut down management interface */
860 
861  /* save ifconfig-pool */
862  multi_ifconfig_pool_persist(&multi, true);
863 
864  /* tear down tunnel instance (unless --persist-tun) */
865  multi_uninit(&multi);
866  multi_top_free(&multi);
868 }
multi_tcp_wait
static int multi_tcp_wait(const struct context *c, struct multi_tcp *mtcp)
Definition: mtcp.c:264
context_2::event_set_status
unsigned int event_set_status
Definition: openvpn.h:238
mbuf_extract_item
bool mbuf_extract_item(struct mbuf_set *ms, struct mbuf_item *item)
Definition: mbuf.c:113
multi_tcp::maxevents
int maxevents
Definition: mtcp.h:41
signal_info::signal_received
volatile int signal_received
Definition: sig.h:43
multi_instance
Server-mode state structure for one single VPN tunnel.
Definition: multi.h:101
multi_instance::halt
bool halt
Definition: multi.h:106
multi_process_incoming_dco
bool multi_process_incoming_dco(struct multi_context *m)
Process an incoming DCO message (from kernel space).
mbuf_defined
static bool mbuf_defined(const struct mbuf_set *ms)
Definition: mbuf.h:80
PERF_PROC_OUT_TUN_MTCP
#define PERF_PROC_OUT_TUN_MTCP
Definition: perf.h:57
MPP_RECORD_TOUCH
#define MPP_RECORD_TOUCH
Definition: multi.h:290
multi_instance::tcp_rwflags
unsigned int tcp_rwflags
Definition: multi.h:120
hash_bucket
static struct hash_bucket * hash_bucket(struct hash *hash, uint32_t hv)
Definition: list.h:141
TUN_READ
#define TUN_READ
Definition: event.h:65
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1011
M_ERRNO
#define M_ERRNO
Definition: error.h:100
multi_tcp_process_outgoing_link_ready
static bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition: mtcp.c:326
context_2::to_link
struct buffer to_link
Definition: openvpn.h:383
forward.h
MTCP_MANAGEMENT
#define MTCP_MANAGEMENT
Definition: mtcp.c:62
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
TA_TUN_READ
#define TA_TUN_READ
Definition: mtcp.c:50
buf_reset
static void buf_reset(struct buffer *buf)
Definition: buffer.h:303
multi_tcp_set_global_rw_flags
static void multi_tcp_set_global_rw_flags(struct multi_context *m, struct multi_instance *mi)
Definition: mtcp.c:250
M_FATAL
#define M_FATAL
Definition: error.h:95
TUN_OUT
#define TUN_OUT(c)
Definition: forward.h:38
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
MTP_LINK_OUT
#define MTP_LINK_OUT
context
Contains all state information for one tunnel.
Definition: openvpn.h:479
hash
Definition: list.h:58
TA_UNDEF
#define TA_UNDEF
Definition: mtcp.c:44
multi_context::mbuf
struct mbuf_set * mbuf
Set of buffers for passing data channel packets between VPN tunnel instances.
Definition: multi.h:167
MTCP_SIG
#define MTCP_SIG
Definition: mtcp.c:61
multi_tcp_wait_lite
static int multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, const int action, bool *tun_input_pending)
Definition: mtcp.c:394
multi_instance::real
struct mroute_addr real
External network address of the remote peer.
Definition: multi.h:114
multi_instance::gc
struct gc_arena gc
Definition: multi.h:105
set_prefix
static void set_prefix(struct multi_instance *mi)
Definition: multi.h:537
D_MULTI_ERRORS
#define D_MULTI_ERRORS
Definition: errlevel.h:65
mbuf_peek
static struct multi_instance * mbuf_peek(struct mbuf_set *ms)
Definition: mbuf.h:100
multi_tcp
Definition: mtcp.h:36
config-msvc.h
multi_tcp_dereference_instance
void multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct multi_instance *mi)
Definition: mtcp.c:238
TA_TUN_WRITE
#define TA_TUN_WRITE
Definition: mtcp.c:51
multi_uninit
void multi_uninit(struct multi_context *m)
Definition: multi.c:704
MPP_PRE_SELECT
#define MPP_PRE_SELECT
Definition: multi.h:287
MTP_TUN_OUT
#define MTP_TUN_OUT
MTCP_SOCKET
#define MTCP_SOCKET
Definition: mtcp.c:59
TA_SOCKET_WRITE_READY
#define TA_SOCKET_WRITE_READY
Definition: mtcp.c:48
multi_tcp_process_outgoing_link
static bool multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const unsigned int mpp_flags)
Definition: mtcp.c:350
MTCP_N
#define MTCP_N
Definition: mtcp.c:66
hash_element::value
void * value
Definition: list.h:47
TA_SOCKET_READ
#define TA_SOCKET_READ
Definition: mtcp.c:45
dmsg
#define dmsg(flags,...)
Definition: error.h:154
options::n_bcast_buf
int n_bcast_buf
Definition: options.h:497
multi_close_instance_on_signal
void multi_close_instance_on_signal(struct multi_context *m, struct multi_instance *mi)
Definition: multi.c:3186
EVENT_READ
#define EVENT_READ
Definition: event.h:39
multi_ifconfig_pool_persist
void multi_ifconfig_pool_persist(struct multi_context *m, bool force)
Definition: multi.c:165
context::mode
int mode
Role of this context within the OpenVPN process.
Definition: openvpn.h:493
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
clear_prefix
static void clear_prefix(void)
Definition: multi.h:549
context::c2
struct context_2 c2
Level 2 context.
Definition: openvpn.h:520
TA_TIMEOUT
#define TA_TIMEOUT
Definition: mtcp.c:53
event_del
static void event_del(struct event_set *es, event_t event)
Definition: event.h:154
socket_event_handle
static event_t socket_event_handle(const struct link_socket *s)
Definition: socket.h:1247
ASSERT
#define ASSERT(x)
Definition: error.h:201
multi_init
void multi_init(struct multi_context *m, struct context *t, bool tcp_mode)
Definition: multi.c:294
multi_context::top
struct context top
Storage structure for process-wide configuration.
Definition: multi.h:195
mbuf_add_item
void mbuf_add_item(struct mbuf_set *ms, const struct mbuf_item *item)
Definition: mbuf.c:91
multi_instance::socket_set_called
bool socket_set_called
Definition: multi.h:122
openvpn_sockaddr::sa
struct sockaddr sa
Definition: socket.h:69
BLEN
#define BLEN(buf)
Definition: buffer.h:127
hash_add_fast
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv, void *value)
Definition: list.h:165
multi_assign_peer_id
void multi_assign_peer_id(struct multi_context *m, struct multi_instance *mi)
Assigns a peer-id to a a client and adds the instance to the the instances array of the multi_context...
Definition: multi.c:4094
io_wait
static void io_wait(struct context *c, const unsigned int flags)
Definition: forward.h:363
tunnel_server_tcp
void tunnel_server_tcp(struct context *top)
Main event loop for OpenVPN in TCP server mode.
Definition: mtcp.c:791
event_set_return::arg
void * arg
Definition: event.h:121
IOW_TO_TUN
#define IOW_TO_TUN
Definition: forward.h:54
multi_process_timeout
bool multi_process_timeout(struct multi_context *m, const unsigned int mpp_flags)
Definition: multi.c:3643
multi_close_instance
void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
Definition: multi.c:601
hash_element::key
const void * key
Definition: list.h:48
update_time
static void update_time(void)
Definition: otime.h:77
multi_tcp_instance_specific_init
bool multi_tcp_instance_specific_init(struct multi_context *m, struct multi_instance *mi)
Definition: mtcp.c:173
multi_process_outgoing_link_dowork
static bool multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition: multi.h:676
mbuf_free
void mbuf_free(struct mbuf_set *ms)
Definition: mbuf.c:51
MTCP_TUN
#define MTCP_TUN
Definition: mtcp.c:60
close_instance
void close_instance(struct context *c)
Definition: init.c:4748
perf_pop
static void perf_pop(void)
Definition: perf.h:82
MTCP_FILE_CLOSE_WRITE
#define MTCP_FILE_CLOSE_WRITE
Definition: mtcp.c:63
LS_MODE_TCP_ACCEPT_FROM
#define LS_MODE_TCP_ACCEPT_FROM
Definition: socket.h:194
hash_lookup_fast
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition: list.c:85
context::options
struct options options
Options loaded from command line or configuration file.
Definition: openvpn.h:481
multi_context::mpp_touched
struct multi_instance ** mpp_touched
Definition: multi.h:191
dco_event_set
void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
Definition: dco_win.c:437
D_MULTI_LOW
#define D_MULTI_LOW
Definition: errlevel.h:86
read_incoming_tun
void read_incoming_tun(struct context *c)
Read a packet from the virtual tun/tap network interface.
Definition: forward.c:1231
SOCKET_WRITE
#define SOCKET_WRITE
Definition: event.h:63
TA_SOCKET_WRITE
#define TA_SOCKET_WRITE
Definition: mtcp.c:47
multi.h
D_MULTI_TCP
#define D_MULTI_TCP
Definition: errlevel.h:162
multi_context::hash
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition: multi.h:159
multi_process_per_second_timers
static void multi_process_per_second_timers(struct multi_context *m)
Definition: multi.h:591
mbuf_item::instance
struct multi_instance * instance
Definition: mbuf.h:53
multi_tcp::es
struct event_set * es
Definition: mtcp.h:38
MULTI_CHECK_SIG
#define MULTI_CHECK_SIG(m)
Definition: multi.h:689
tuntap_ring_empty
static bool tuntap_ring_empty(struct tuntap *tt)
Definition: tun.h:255
multi_context::pending
struct multi_instance * pending
Definition: multi.h:189
TA_INITIAL
#define TA_INITIAL
Definition: mtcp.c:52
multi_process_post
bool multi_process_post(struct multi_context *m, struct multi_instance *mi, const unsigned int flags)
Perform postprocessing of a VPN tunnel instance.
Definition: multi.c:3012
TUN_WRITE
#define TUN_WRITE
Definition: event.h:66
mbuf_buffer::buf
struct buffer buf
Definition: mbuf.h:43
mroute_extract_openvpn_sockaddr
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition: mroute.c:266
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
multi_create_instance
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real)
Definition: multi.c:752
multi_create_instance_tcp
static struct multi_instance * multi_create_instance_tcp(struct multi_context *m)
Definition: mtcp.c:122
ISC_SERVER
#define ISC_SERVER
Definition: init.h:115
IOW_READ_TUN_FORCE
#define IOW_READ_TUN_FORCE
Definition: forward.h:62
ta_iow_flags::flags
unsigned int flags
Definition: mtcp.c:70
multi_tcp_post
static int multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int action)
Definition: mtcp.c:541
mbuf_item
Definition: mbuf.h:50
multi_tcp_context
static struct context * multi_tcp_context(struct multi_context *m, struct multi_instance *mi)
Definition: mtcp.c:313
ta_iow_flags::ret
unsigned int ret
Definition: mtcp.c:71
context_2::timeval
struct timeval timeval
Time to next event of timers and similar.
Definition: openvpn.h:402
context_2::link_socket
struct link_socket * link_socket
Definition: openvpn.h:240
multi_process_incoming_tun
bool multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags)
Determine the destination VPN tunnel of a packet received over the virtual tun/tap network interface ...
Definition: multi.c:3514
syshead.h
multi_tcp::n_esr
int n_esr
Definition: mtcp.h:40
mbuf_alloc_buf
struct mbuf_buffer * mbuf_alloc_buf(const struct buffer *buf)
Definition: mbuf.c:67
socket_reset_listen_persistent
static void socket_reset_listen_persistent(struct link_socket *s)
Definition: socket.h:1278
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
multi_tcp::tun_rwflags
unsigned int tun_rwflags
Definition: mtcp.h:42
context::sig
struct signal_info * sig
Internal error signaling object.
Definition: openvpn.h:506
context_clear_2
void context_clear_2(struct context *c)
Definition: init.c:90
TA_SOCKET_WRITE_DEFERRED
#define TA_SOCKET_WRITE_DEFERRED
Definition: mtcp.c:49
mroute_addr_print
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition: mroute.c:378
mbuf_buffer
Definition: mbuf.h:41
BASE_N_EVENTS
#define BASE_N_EVENTS
Definition: init.h:33
uninit_management_callback
void uninit_management_callback(void)
Definition: init.c:4391
multi_tcp_free
void multi_tcp_free(struct multi_tcp *mtcp)
Definition: mtcp.c:227
multi_tcp_process_io
static void multi_tcp_process_io(struct multi_context *m)
Definition: mtcp.c:689
ta_iow_flags::tun
unsigned int tun
Definition: mtcp.c:72
LINK_OUT
#define LINK_OUT(c)
Definition: forward.h:39
multi_context
Main OpenVPN server state structure.
Definition: multi.h:155
init_management_callback_multi
void init_management_callback_multi(struct multi_context *m)
Definition: multi.c:4069
multi_top_init
void multi_top_init(struct multi_context *m, struct context *top)
Definition: multi.c:3783
context::es
struct env_set * es
Set of environment variables.
Definition: openvpn.h:502
CC_HARD_USR1_TO_HUP
#define CC_HARD_USR1_TO_HUP
Definition: init.h:105
ta_iow_flags
Definition: mtcp.c:68
event_free
static void event_free(struct event_set *es)
Definition: event.h:139
IS_SIG
#define IS_SIG(c)
Definition: sig.h:48
multi_tcp_dispatch
static struct multi_instance * multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int action)
Definition: mtcp.c:464
multi_tcp_init
struct multi_tcp * multi_tcp_init(int maxevents, int *maxclients)
Definition: mtcp.c:199
init_instance_handle_signals
void init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
Definition: init.c:4417
max_int
static int max_int(int x, int y)
Definition: integer.h:69
mbuf_free_buf
void mbuf_free_buf(struct mbuf_buffer *mb)
Definition: mbuf.c:78
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:3879
MTP_NONE
#define MTP_NONE
tuntap::dco
dco_context_t dco
Definition: tun.h:234
multi_tcp_action
static void multi_tcp_action(struct multi_context *m, struct multi_instance *mi, int action, bool poll)
Definition: mtcp.c:601
TA_TUN_WRITE_TIMEOUT
#define TA_TUN_WRITE_TIMEOUT
Definition: mtcp.c:54
CM_TOP
#define CM_TOP
Definition: openvpn.h:489
stream_buf_read_setup
static bool stream_buf_read_setup(struct link_socket *sock)
Definition: socket.h:1012
status
static SERVICE_STATUS status
Definition: interactive.c:56
MTCP_DCO
#define MTCP_DCO
Definition: mtcp.c:64
read_incoming_link
void read_incoming_link(struct context *c)
Read a packet from the external network interface.
Definition: forward.c:888
multi_process_drop_outgoing_tun
void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Definition: multi.c:3674
ptr_type
unsigned long ptr_type
Definition: common.h:58
management
Definition: manage.h:334
hash_element
Definition: list.h:45
min_int
static int min_int(int x, int y)
Definition: integer.h:82
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1019
event_set_init
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition: event.c:1188
event_set_return
Definition: event.h:118
rw_handle
Definition: win32.h:76
multi_instance_string
const char * multi_instance_string(const struct multi_instance *mi, bool null, struct gc_arena *gc)
Definition: multi.c:467
hash_bucket
Definition: list.h:53
IOW_READ_TUN
#define IOW_READ_TUN
Definition: forward.h:56
ALLOC_OBJ_CLEAR
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1046
wait_signal
static void wait_signal(struct event_set *es, void *arg)
Definition: event.h:185
socket_set_listen_persistent
static void socket_set_listen_persistent(struct link_socket *s, struct event_set *es, void *arg)
Definition: socket.h:1266
config.h
socket_read_residual
static bool socket_read_residual(const struct link_socket *s)
Definition: socket.h:1241
multi_process_incoming_link
bool multi_process_incoming_link(struct multi_context *m, struct multi_instance *instance, const unsigned int mpp_flags)
Demultiplex and process a packet received over the external network interface.
Definition: multi.c:3310
ta_iow_flags::sock
unsigned int sock
Definition: mtcp.c:73
PERF_EVENT_LOOP
#define PERF_EVENT_LOOP
Definition: perf.h:44
mbuf_item::buffer
struct mbuf_buffer * buffer
Definition: mbuf.h:52
EVENT_WRITE
#define EVENT_WRITE
Definition: event.h:40
TA_SOCKET_READ_RESIDUAL
#define TA_SOCKET_READ_RESIDUAL
Definition: mtcp.c:46
multi_context::mtcp
struct multi_tcp * mtcp
State specific to OpenVPN using TCP as external transport.
Definition: multi.h:170
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:105
multi_process_outgoing_tun
static bool multi_process_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Send a packet over the virtual tun/tap network interface to its locally reachable destination.
Definition: multi.h:652
multi_instance::tcp_link_out_deferred
struct mbuf_set * tcp_link_out_deferred
Definition: multi.h:121
event_set_return::rwflags
unsigned int rwflags
Definition: event.h:120
memdbg.h
ALLOC_ARRAY
#define ALLOC_ARRAY(dptr, type, n)
Definition: buffer.h:1052
mbuf_init
struct mbuf_set * mbuf_init(unsigned int size)
Definition: mbuf.c:41
multi_tcp::management_persist_flags
unsigned int management_persist_flags
Definition: mtcp.h:44
multi_top_free
void multi_top_free(struct multi_context *m)
Definition: multi.c:3790
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
SOCKET_READ
#define SOCKET_READ
Definition: event.h:62
multi_tcp_instance_specific_free
void multi_tcp_instance_specific_free(struct multi_instance *mi)
Definition: mtcp.c:193
multi_tcp_delete_event
void multi_tcp_delete_event(struct multi_tcp *mtcp, event_t event)
Definition: mtcp.c:218
msg
#define msg(flags,...)
Definition: error.h:150
tv_clear
static void tv_clear(struct timeval *tv)
Definition: otime.h:101
multi_instance::did_real_hash
bool did_real_hash
Definition: multi.h:127
perf_push
static void perf_push(int type)
Definition: perf.h:78
management_io
void management_io(struct management *man)
Definition: manage.c:3155
ptr_format
#define ptr_format
Definition: common.h:49
multi_instance::context
struct context context
The context structure storing state for this VPN tunnel.
Definition: multi.h:136
openvpn_sockaddr::addr
union openvpn_sockaddr::@13 addr
multi_tcp::esr
struct event_set_return * esr
Definition: mtcp.h:39
D_MULTI_DEBUG
#define D_MULTI_DEBUG
Definition: errlevel.h:126
multi_get_timeout
static void multi_get_timeout(struct multi_context *m, struct timeval *dest)
Definition: multi.h:609
initialization_sequence_completed
void initialization_sequence_completed(struct context *c, const unsigned int flags)
Definition: init.c:1603
multi_process_outgoing_link_pre
static struct multi_instance * multi_process_outgoing_link_pre(struct multi_context *m)
Definition: multi.h:425
hash_value
static uint32_t hash_value(const struct hash *hash, const void *key)
Definition: list.h:123
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
context::c1
struct context_1 c1
Level 1 context.
Definition: openvpn.h:519