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-2026 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "multi.h"
30#include "forward.h"
31#include "mtcp.h"
32#include "multi_io.h"
33
34#include "memdbg.h"
35
36#ifdef HAVE_SYS_INOTIFY_H
37#include <sys/inotify.h>
38#endif
39
40struct multi_instance *
42{
43 struct gc_arena gc = gc_new();
44 struct multi_instance *mi = NULL;
45
46 mi = multi_create_instance(m, NULL, sock);
47 if (mi)
48 {
49 mi->real.proto = sock->info.proto;
51 }
52
53#ifndef ENABLE_SMALL
54 if (mi)
55 {
56 dmsg(D_MULTI_DEBUG, "MULTI TCP: instance added: %s", mroute_addr_print(&mi->real, &gc));
57 }
58 else
59 {
60 dmsg(D_MULTI_DEBUG, "MULTI TCP: new client instance failed");
61 }
62#endif
63
64 gc_free(&gc);
65 ASSERT(!(mi && mi->halt));
66 return mi;
67}
68
69bool
71{
72 /* buffer for queued TCP socket output packets */
74
79 ASSERT(mi->context.c2.link_sockets[0]->info.lsa->actual.dest.addr.sa.sa_family == AF_INET
80 || mi->context.c2.link_sockets[0]->info.lsa->actual.dest.addr.sa.sa_family == AF_INET6);
83 &mi->real, &mi->context.c2.link_sockets[0]->info.lsa->actual.dest, true))
84 {
85 msg(D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined");
86 return false;
87 }
88 return true;
89}
90
91void
96
97void
99{
100 if (multi_io && multi_io->es)
101 {
102 event_del(multi_io->es, event);
103 }
104}
105
106void
108{
109 const struct link_socket *sock = mi->context.c2.link_sockets[0];
110 if (sock && mi->socket_set_called)
111 {
113 mi->socket_set_called = false;
114 }
115 multi_io->n_esr = 0;
116}
117
118bool
120 const unsigned int mpp_flags)
121{
122 struct mbuf_item item;
123 bool ret = true;
124 ASSERT(mi);
125
126 /* extract from queue */
127 if (mbuf_extract_item(mi->tcp_link_out_deferred, &item)) /* ciphertext IP packet */
128 {
129 dmsg(D_MULTI_TCP, "MULTI TCP: transmitting previously deferred packet");
130
131 ASSERT(mi == item.instance);
132 mi->context.c2.to_link = item.buffer->buf;
133 ret = multi_process_outgoing_link_dowork(m, mi, mpp_flags);
134 mbuf_free_buf(item.buffer);
135 }
136 return ret;
137}
138
139bool
140multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const unsigned int mpp_flags)
141{
143 bool ret = true;
144
145 if (mi)
146 {
147 if ((defer && !proto_is_dgram(mi->context.c2.link_sockets[0]->info.proto))
149 {
150 /* save to queue */
151 struct buffer *buf = &mi->context.c2.to_link;
152 if (BLEN(buf) > 0)
153 {
154 struct mbuf_buffer *mb = mbuf_alloc_buf(buf);
155 struct mbuf_item item;
156
157 set_prefix(mi);
158 dmsg(D_MULTI_TCP, "MULTI TCP: queuing deferred packet");
159 item.buffer = mb;
160 item.instance = mi;
162 mbuf_free_buf(mb);
163 buf_reset(buf);
164 ret = multi_process_post(m, mi, mpp_flags);
165 if (!ret)
166 {
167 mi = NULL;
168 }
169 clear_prefix();
170 }
171 }
172 else
173 {
174 ret = multi_process_outgoing_link_dowork(m, mi, mpp_flags);
175 if (!ret)
176 {
177 mi = NULL;
178 }
179 }
180 }
181 return ret;
182}
static void buf_reset(struct buffer *buf)
Reset a buffer to an undefined (unallocated) state.
Definition buffer.h:599
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1965
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1949
#define D_MULTI_ERRORS
Definition errlevel.h:64
#define D_MULTI_DEBUG
Definition errlevel.h:126
#define D_MULTI_TCP
Definition errlevel.h:162
static void event_del(struct event_set *es, event_t event)
Definition event.h:174
const struct rw_handle * event_t
Definition event.h:89
Interface functions to the internal and external multiplexers.
void mbuf_add_item(struct mbuf_set *ms, const struct mbuf_item *item)
Definition mbuf.c:104
struct mbuf_buffer * mbuf_alloc_buf(const struct buffer *buf)
Definition mbuf.c:65
void mbuf_free_buf(struct mbuf_buffer *mb)
Definition mbuf.c:76
bool mbuf_extract_item(struct mbuf_set *ms, struct mbuf_item *item)
Definition mbuf.c:126
void mbuf_free(struct mbuf_set *ms)
Definition mbuf.c:50
struct mbuf_set * mbuf_init(unsigned int size)
Definition mbuf.c:38
static bool mbuf_defined(const struct mbuf_set *ms)
Definition mbuf.h:81
bool mroute_extract_openvpn_sockaddr(struct mroute_addr *addr, const struct openvpn_sockaddr *osaddr, bool use_port)
Definition mroute.c:255
const char * mroute_addr_print(const struct mroute_addr *ma, struct gc_arena *gc)
Definition mroute.c:372
bool multi_tcp_instance_specific_init(const struct multi_context *m, struct multi_instance *mi)
Definition mtcp.c:70
void multi_tcp_instance_specific_free(struct multi_instance *mi)
Definition mtcp.c:92
struct multi_instance * multi_create_instance_tcp(struct multi_context *m, struct link_socket *sock)
Definition mtcp.c:41
void multi_tcp_delete_event(struct multi_io *multi_io, event_t event)
Definition mtcp.c:98
bool multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const unsigned int mpp_flags)
Definition mtcp.c:140
bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition mtcp.c:119
void multi_tcp_dereference_instance(struct multi_io *multi_io, struct multi_instance *mi)
Definition mtcp.c:107
struct multi_instance * multi_create_instance(struct multi_context *m, const struct mroute_addr *real, struct link_socket *sock)
Definition multi.c:722
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:3032
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:4198
Header file for server-mode related structures and functions.
static void clear_prefix(void)
Definition multi.h:544
static struct multi_instance * multi_process_outgoing_link_pre(struct multi_context *m)
Definition multi.h:422
static void set_prefix(const struct multi_instance *mi)
Definition multi.h:532
static bool multi_process_outgoing_link_dowork(struct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flags)
Definition multi.h:670
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:797
#define LS_MODE_TCP_ACCEPT_FROM
Definition socket.h:189
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP).
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
struct buffer to_link
Definition openvpn.h:377
struct link_socket ** link_sockets
Definition openvpn.h:238
struct context_2 c2
Level 2 context.
Definition openvpn.h:516
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct buffer buf
Definition mbuf.h:44
struct mbuf_buffer * buffer
Definition mbuf.h:53
struct multi_instance * instance
Definition mbuf.h:54
uint8_t proto
Definition mroute.h:84
Main OpenVPN server state structure.
Definition multi.h:170
struct context top
Storage structure for process-wide configuration.
Definition multi.h:215
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct mbuf_set * tcp_link_out_deferred
Definition multi.h:128
struct mroute_addr real
External network address of the remote peer.
Definition multi.h:121
bool socket_set_called
Definition multi.h:129
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:150
struct event_set * es
Definition multi_io.h:53
int n_esr
Definition multi_io.h:55
union openvpn_sockaddr::@051240265251124304325377241125360350250341115171 addr
struct sockaddr sa
Definition socket_util.h:42
int n_bcast_buf
Definition options.h:510
struct gc_arena gc
Definition test_ssl.c:122