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-2022 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  if (mi && mi->context.c2.link_socket->info.dco_installed)
406  {
407  /* If we got a socket that has been handed over to the kernel
408  * we must not call the normal socket function to figure out
409  * if it is readable or writable */
410  /* Assert that we only have the DCO exptected flags */
411  ASSERT(action & (TA_SOCKET_READ | TA_SOCKET_WRITE));
412 
413  /* We are always ready! */
414  return action;
415  }
416 
417  switch (action)
418  {
419  case TA_TUN_READ:
420  looking_for = TUN_READ;
421  tun_input_pending = NULL;
422  io_wait(c, IOW_READ_TUN);
423  break;
424 
425  case TA_SOCKET_READ:
426  looking_for = SOCKET_READ;
427  tun_input_pending = NULL;
429  break;
430 
431  case TA_TUN_WRITE:
432  looking_for = TUN_WRITE;
433  tun_input_pending = NULL;
434  c->c2.timeval.tv_sec = 1; /* For some reason, the Linux 2.2 TUN/TAP driver hits this timeout */
436  io_wait(c, IOW_TO_TUN);
437  perf_pop();
438  break;
439 
440  case TA_SOCKET_WRITE:
441  looking_for = SOCKET_WRITE;
443  break;
444 
445  default:
446  msg(M_FATAL, "MULTI TCP: multi_tcp_wait_lite, unhandled action=%d", action);
447  }
448 
449  if (tun_input_pending && (c->c2.event_set_status & TUN_READ))
450  {
451  *tun_input_pending = true;
452  }
453 
454  if (c->c2.event_set_status & looking_for)
455  {
456  return action;
457  }
458  else
459  {
460  switch (action)
461  {
462  /* TCP socket output buffer is full */
463  case TA_SOCKET_WRITE:
465 
466  /* TUN device timed out on accepting write */
467  case TA_TUN_WRITE:
468  return TA_TUN_WRITE_TIMEOUT;
469  }
470 
471  return TA_UNDEF;
472  }
473 }
474 
475 static struct multi_instance *
476 multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int action)
477 {
478  const unsigned int mpp_flags = MPP_PRE_SELECT|MPP_RECORD_TOUCH;
479  struct multi_instance *touched = mi;
480  m->mpp_touched = &touched;
481 
482  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_dispatch a=%s mi=" ptr_format,
483  pract(action),
484  (ptr_type)mi);
485 
486  switch (action)
487  {
488  case TA_TUN_READ:
489  read_incoming_tun(&m->top);
490  if (!IS_SIG(&m->top))
491  {
492  multi_process_incoming_tun(m, mpp_flags);
493  }
494  break;
495 
496  case TA_SOCKET_READ:
498  ASSERT(mi);
499  ASSERT(mi->context.c2.link_socket);
500  set_prefix(mi);
501  read_incoming_link(&mi->context);
502  clear_prefix();
503  if (!IS_SIG(&mi->context))
504  {
505  multi_process_incoming_link(m, mi, mpp_flags);
506  if (!IS_SIG(&mi->context))
507  {
508  stream_buf_read_setup(mi->context.c2.link_socket);
509  }
510  }
511  break;
512 
513  case TA_TIMEOUT:
514  multi_process_timeout(m, mpp_flags);
515  break;
516 
517  case TA_TUN_WRITE:
518  multi_process_outgoing_tun(m, mpp_flags);
519  break;
520 
522  multi_process_drop_outgoing_tun(m, mpp_flags);
523  break;
524 
526  ASSERT(mi);
527  multi_tcp_process_outgoing_link_ready(m, mi, mpp_flags);
528  break;
529 
530  case TA_SOCKET_WRITE:
531  multi_tcp_process_outgoing_link(m, false, mpp_flags);
532  break;
533 
535  multi_tcp_process_outgoing_link(m, true, mpp_flags);
536  break;
537 
538  case TA_INITIAL:
539  ASSERT(mi);
540  if (!mi->context.c2.link_socket->info.dco_installed)
541  {
543  }
544  multi_process_post(m, mi, mpp_flags);
545  break;
546 
547  default:
548  msg(M_FATAL, "MULTI TCP: multi_tcp_dispatch, unhandled action=%d", action);
549  }
550 
551  m->mpp_touched = NULL;
552  return touched;
553 }
554 
555 static int
556 multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const int action)
557 {
558  struct context *c = multi_tcp_context(m, mi);
559  int newaction = TA_UNDEF;
560 
561 #define MTP_NONE 0
562 #define MTP_TUN_OUT (1<<0)
563 #define MTP_LINK_OUT (1<<1)
564  unsigned int flags = MTP_NONE;
565 
566  if (TUN_OUT(c))
567  {
568  flags |= MTP_TUN_OUT;
569  }
570  if (LINK_OUT(c))
571  {
572  flags |= MTP_LINK_OUT;
573  }
574 
575  switch (flags)
576  {
578  case MTP_TUN_OUT:
579  newaction = TA_TUN_WRITE;
580  break;
581 
582  case MTP_LINK_OUT:
583  newaction = TA_SOCKET_WRITE;
584  break;
585 
586  case MTP_NONE:
587  if (mi && socket_read_residual(c->c2.link_socket))
588  {
589  newaction = TA_SOCKET_READ_RESIDUAL;
590  }
591  else
592  {
593  if (!c->c2.link_socket->info.dco_installed)
594  {
596  }
597  }
598  break;
599 
600  default:
601  {
602  struct gc_arena gc = gc_new();
603  msg(M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=%s flags=%d",
604  multi_instance_string(mi, false, &gc),
605  flags);
606  gc_free(&gc);
607  break;
608  }
609  }
610 
611  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_post %s -> %s",
612  pract(action),
613  pract(newaction));
614 
615  return newaction;
616 }
617 
618 static void
619 multi_tcp_action(struct multi_context *m, struct multi_instance *mi, int action, bool poll)
620 {
621  bool tun_input_pending = false;
622 
623  do
624  {
625  dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_action a=%s p=%d",
626  pract(action),
627  poll);
628 
629  /*
630  * If TA_SOCKET_READ_RESIDUAL, it means we still have pending
631  * input packets which were read by a prior TCP recv.
632  *
633  * Otherwise do a "lite" wait, which means we wait with 0 timeout
634  * on I/O events only related to the current instance, not
635  * the big list of events.
636  *
637  * On our first pass, poll will be false because we already know
638  * that input is available, and to call io_wait would be redundant.
639  */
640  if (poll && action != TA_SOCKET_READ_RESIDUAL)
641  {
642  const int orig_action = action;
643  action = multi_tcp_wait_lite(m, mi, action, &tun_input_pending);
644  if (action == TA_UNDEF)
645  {
646  msg(M_FATAL, "MULTI TCP: I/O wait required blocking in multi_tcp_action, action=%d", orig_action);
647  }
648  }
649 
650  /*
651  * Dispatch the action
652  */
653  struct multi_instance *touched = multi_tcp_dispatch(m, mi, action);
654 
655  /*
656  * Signal received or TCP connection
657  * reset by peer?
658  */
659  if (touched && IS_SIG(&touched->context))
660  {
661  if (mi == touched)
662  {
663  mi = NULL;
664  }
665  multi_close_instance_on_signal(m, touched);
666  }
667 
668 
669  /*
670  * If dispatch produced any pending output
671  * for a particular instance, point to
672  * that instance.
673  */
674  if (m->pending)
675  {
676  mi = m->pending;
677  }
678 
679  /*
680  * Based on the effects of the action,
681  * such as generating pending output,
682  * possibly transition to a new action state.
683  */
684  action = multi_tcp_post(m, mi, action);
685 
686  /*
687  * If we are finished processing the original action,
688  * check if we have any TUN input. If so, transition
689  * our action state to processing this input.
690  */
691  if (tun_input_pending && action == TA_UNDEF)
692  {
693  action = TA_TUN_READ;
694  mi = NULL;
695  tun_input_pending = false;
696  poll = false;
697  }
698  else
699  {
700  poll = true;
701  }
702 
703  } while (action != TA_UNDEF);
704 }
705 
706 static void
708 {
709  struct multi_tcp *mtcp = m->mtcp;
710  int i;
711 
712  for (i = 0; i < mtcp->n_esr; ++i)
713  {
714  struct event_set_return *e = &mtcp->esr[i];
715 
716  /* incoming data for instance? */
717  if (e->arg >= MTCP_N)
718  {
719  struct multi_instance *mi = (struct multi_instance *) e->arg;
720  if (mi)
721  {
722  if (e->rwflags & EVENT_WRITE)
723  {
725  }
726  else if (e->rwflags & EVENT_READ)
727  {
728  multi_tcp_action(m, mi, TA_SOCKET_READ, false);
729  }
730  }
731  }
732  else
733  {
734 #ifdef ENABLE_MANAGEMENT
735  if (e->arg == MTCP_MANAGEMENT)
736  {
739  }
740  else
741 #endif
742  /* incoming data on TUN? */
743  if (e->arg == MTCP_TUN)
744  {
745  if (e->rwflags & EVENT_WRITE)
746  {
747  multi_tcp_action(m, NULL, TA_TUN_WRITE, false);
748  }
749  else if (e->rwflags & EVENT_READ)
750  {
751  multi_tcp_action(m, NULL, TA_TUN_READ, false);
752  }
753  }
754  /* new incoming TCP client attempting to connect? */
755  else if (e->arg == MTCP_SOCKET)
756  {
757  struct multi_instance *mi;
758  ASSERT(m->top.c2.link_socket);
761  if (mi)
762  {
763  multi_tcp_action(m, mi, TA_INITIAL, false);
764  }
765  }
766 #if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
767  /* incoming data on DCO? */
768  else if (e->arg == MTCP_DCO)
769  {
771  }
772 #endif
773  /* signal received? */
774  else if (e->arg == MTCP_SIG)
775  {
777  }
778 #ifdef ENABLE_ASYNC_PUSH
779  else if (e->arg == MTCP_FILE_CLOSE_WRITE)
780  {
781  multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_TOUCH);
782  }
783 #endif
784  }
785  if (IS_SIG(&m->top))
786  {
787  break;
788  }
789  }
790  mtcp->n_esr = 0;
791 
792  /*
793  * Process queued mbuf packets destined for TCP socket
794  */
795  {
796  struct multi_instance *mi;
797  while (!IS_SIG(&m->top) && (mi = mbuf_peek(m->mbuf)) != NULL)
798  {
799  multi_tcp_action(m, mi, TA_SOCKET_WRITE, true);
800  }
801  }
802 }
803 
804 /*
805  * Top level event loop for single-threaded operation.
806  * TCP mode.
807  */
808 void
810 {
811  struct multi_context multi;
812  int status;
813 
814  top->mode = CM_TOP;
816 
817  /* initialize top-tunnel instance */
819  if (IS_SIG(top))
820  {
821  return;
822  }
823 
824  /* initialize global multi_context object */
825  multi_init(&multi, top, true);
826 
827  /* initialize our cloned top object */
828  multi_top_init(&multi, top);
829 
830  /* initialize management interface */
832 
833  /* finished with initialization */
834  initialization_sequence_completed(top, ISC_SERVER); /* --mode server --proto tcp-server */
835 
836 #ifdef ENABLE_ASYNC_PUSH
837  multi.top.c2.inotify_fd = inotify_init();
838  if (multi.top.c2.inotify_fd < 0)
839  {
840  msg(D_MULTI_ERRORS | M_ERRNO, "MULTI: inotify_init error");
841  }
842 #endif
843 
844  /* per-packet event loop */
845  while (true)
846  {
848 
849  /* wait on tun/socket list */
850  multi_get_timeout(&multi, &multi.top.c2.timeval);
851  status = multi_tcp_wait(&multi.top, multi.mtcp);
852  MULTI_CHECK_SIG(&multi);
853 
854  /* check on status of coarse timers */
856 
857  /* timeout? */
858  if (status > 0)
859  {
860  /* process the I/O which triggered select */
861  multi_tcp_process_io(&multi);
862  MULTI_CHECK_SIG(&multi);
863  }
864  else if (status == 0)
865  {
866  multi_tcp_action(&multi, NULL, TA_TIMEOUT, false);
867  }
868 
869  perf_pop();
870  }
871 
872 #ifdef ENABLE_ASYNC_PUSH
873  close(top->c2.inotify_fd);
874 #endif
875 
876  /* shut down management interface */
878 
879  /* save ifconfig-pool */
880  multi_ifconfig_pool_persist(&multi, true);
881 
882  /* tear down tunnel instance (unless --persist-tun) */
883  multi_uninit(&multi);
884  multi_top_free(&multi);
886 }
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:235
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:45
multi_instance
Server-mode state structure for one single VPN tunnel.
Definition: multi.h:100
multi_instance::halt
bool halt
Definition: multi.h:105
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:288
multi_instance::tcp_rwflags
unsigned int tcp_rwflags
Definition: multi.h:119
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:998
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:378
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:3025
TA_TUN_READ
#define TA_TUN_READ
Definition: mtcp.c:50
buf_reset
static void buf_reset(struct buffer *buf)
Definition: buffer.h:290
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:167
tuntap_is_wintun
static bool tuntap_is_wintun(struct tuntap *tt)
Definition: tun.h:245
MTP_LINK_OUT
#define MTP_LINK_OUT
context
Contains all state information for one tunnel.
Definition: openvpn.h:474
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:166
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:113
multi_instance::gc
struct gc_arena gc
Definition: multi.h:104
set_prefix
static void set_prefix(struct multi_instance *mi)
Definition: multi.h:535
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:693
MPP_PRE_SELECT
#define MPP_PRE_SELECT
Definition: multi.h:285
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:493
multi_close_instance_on_signal
void multi_close_instance_on_signal(struct multi_context *m, struct multi_instance *mi)
Definition: multi.c:3156
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:164
context::mode
int mode
Role of this context within the OpenVPN process.
Definition: openvpn.h:488
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:547
context::c2
struct context_2 c2
Level 2 context.
Definition: openvpn.h:515
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:1237
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:293
multi_context::top
struct context top
Storage structure for process-wide configuration.
Definition: multi.h:193
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:121
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:4060
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:809
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:3609
multi_close_instance
void multi_close_instance(struct multi_context *m, struct multi_instance *mi, bool shutdown)
Definition: multi.c:590
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:674
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:4497
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:195
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:476
multi_context::mpp_touched
struct multi_instance ** mpp_touched
Definition: multi.h:189
dco_event_set
void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
Definition: dco_win.c:403
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:1193
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:160
multi_context::hash
struct hash * hash
VPN tunnel instances indexed by real address of the remote peer.
Definition: multi.h:158
multi_process_per_second_timers
static void multi_process_per_second_timers(struct multi_context *m)
Definition: multi.h:589
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:687
tuntap_ring_empty
static bool tuntap_ring_empty(struct tuntap *tt)
Definition: tun.h:251
multi_context::pending
struct multi_instance * pending
Definition: multi.h:187
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:2982
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:740
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:118
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:556
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:397
context_2::link_socket
struct link_socket * link_socket
Definition: openvpn.h:237
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:3478
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:1268
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:501
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:4150
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:707
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:154
init_management_callback_multi
void init_management_callback_multi(struct multi_context *m)
Definition: multi.c:4035
multi_top_init
void multi_top_init(struct multi_context *m, struct context *top)
Definition: multi.c:3749
context::es
struct env_set * es
Set of environment variables.
Definition: openvpn.h:497
CC_HARD_USR1_TO_HUP
#define CC_HARD_USR1_TO_HUP
Definition: init.h:108
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:50
multi_tcp_dispatch
static struct multi_instance * multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, const int action)
Definition: mtcp.c:476
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:4165
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:3854
MTP_NONE
#define MTP_NONE
tuntap::dco
dco_context_t dco
Definition: tun.h:230
multi_tcp_action
static void multi_tcp_action(struct multi_context *m, struct multi_instance *mi, int action, bool poll)
Definition: mtcp.c:619
TA_TUN_WRITE_TIMEOUT
#define TA_TUN_WRITE_TIMEOUT
Definition: mtcp.c:54
CM_TOP
#define CM_TOP
Definition: openvpn.h:484
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:852
multi_process_drop_outgoing_tun
void multi_process_drop_outgoing_tun(struct multi_context *m, const unsigned int mpp_flags)
Definition: multi.c:3640
ptr_type
unsigned long ptr_type
Definition: common.h:58
management
Definition: manage.h:311
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:1006
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:464
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:1033
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:1256
config.h
socket_read_residual
static bool socket_read_residual(const struct link_socket *s)
Definition: socket.h:1231
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:3274
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:169
get_signal
static void get_signal(volatile int *sig)
Definition: sig.h:89
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:650
multi_instance::tcp_link_out_deferred
struct mbuf_set * tcp_link_out_deferred
Definition: multi.h:120
event_set_return::rwflags
unsigned int rwflags
Definition: event.h:120
memdbg.h
ALLOC_ARRAY
#define ALLOC_ARRAY(dptr, type, n)
Definition: buffer.h:1039
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:3756
tun_set
static void tun_set(struct tuntap *tt, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition: tun.h:737
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:126
perf_push
static void perf_push(int type)
Definition: perf.h:78
management_io
void management_io(struct management *man)
Definition: manage.c:3065
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:135
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:607
initialization_sequence_completed
void initialization_sequence_completed(struct context *c, const unsigned int flags)
Definition: init.c:1541
multi_process_outgoing_link_pre
static struct multi_instance * multi_process_outgoing_link_pre(struct multi_context *m)
Definition: multi.h:423
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:514