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