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