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
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 * Walk (don't run) through the routing table,
43 * deleting old entries, and possibly multi_instance
44 * structs as well which have been marked for deletion.
54 * One multi_instance object per client instance.
56 struct multi_instance
{
57 struct schedule_entry se
; /* this must be the first element of the structure */
63 int route_count
; /* number of routes (including cached routes) owned by this instance */
65 struct timeval wakeup
; /* absolute time */
66 struct mroute_addr real
;
67 ifconfig_pool_handle vaddr_handle
;
68 const char *msg_prefix
;
70 /* queued outgoing data in Server/TCP mode */
71 unsigned int tcp_rwflags
;
72 struct mbuf_set
*tcp_link_out_deferred
;
73 bool socket_set_called
;
75 in_addr_t reporting_addr
; /* IP address shown in status listing */
77 bool did_open_context
;
80 #ifdef MANAGEMENT_DEF_AUTH
82 struct buffer_list
*cc_config
;
84 bool connection_established_flag
;
86 int n_clients_delta
; /* added to multi_context.n_clients when instance is closed */
88 struct context context
;
92 * One multi_context object per server daemon thread.
94 struct multi_context
{
96 # define MC_SINGLE_THREADED (1<<0)
97 # define MC_MULTI_THREADED_MASTER (1<<1)
98 # define MC_MULTI_THREADED_WORKER (1<<2)
99 # define MC_MULTI_THREADED_SCHEDULER (1<<3)
100 # define MC_WORK_THREAD (MC_MULTI_THREADED_WORKER|MC_MULTI_THREADED_SCHEDULER)
103 struct hash
*hash
; /* client instances indexed by real address */
104 struct hash
*vhash
; /* client instances indexed by virtual address */
105 struct hash
*iter
; /* like real address hash but optimized for iteration */
106 struct schedule
*schedule
;
107 struct mbuf_set
*mbuf
;
108 struct multi_tcp
*mtcp
;
109 struct ifconfig_pool
*ifconfig_pool
;
110 struct frequency_limit
*new_connection_limiter
;
111 struct mroute_helper
*route_helper
;
112 struct multi_reap
*reaper
;
113 struct mroute_addr local
;
117 int status_file_version
;
118 int n_clients
; /* current number of authenticated clients */
120 #ifdef MANAGEMENT_DEF_AUTH
121 struct hash
*cid_hash
;
122 unsigned long cid_counter
;
125 struct multi_instance
*pending
;
126 struct multi_instance
*earliest_wakeup
;
127 struct multi_instance
**mpp_touched
;
128 struct context_buffers
*context_buffers
;
129 time_t per_second_trigger
;
139 struct mroute_addr addr
;
140 struct multi_instance
*instance
;
142 # define MULTI_ROUTE_CACHE (1<<0)
143 # define MULTI_ROUTE_AGEABLE (1<<1)
146 unsigned int cache_generation
;
147 time_t last_reference
;
151 * top level function, called by openvpn.c
153 void tunnel_server (struct context
*top
);
155 const char *multi_instance_string (const struct multi_instance
*mi
, bool null
, struct gc_arena
*gc
);
158 * Called by mtcp.c, mudp.c, or other (to be written) protocol drivers
161 void multi_init (struct multi_context
*m
, struct context
*t
, bool tcp_mode
, int thread_mode
);
162 void multi_uninit (struct multi_context
*m
);
164 void multi_top_init (struct multi_context
*m
, const struct context
*top
, const bool alloc_buffers
);
165 void multi_top_free (struct multi_context
*m
);
167 struct multi_instance
*multi_create_instance (struct multi_context
*m
, const struct mroute_addr
*real
);
168 void multi_close_instance (struct multi_context
*m
, struct multi_instance
*mi
, bool shutdown
);
170 bool multi_process_timeout (struct multi_context
*m
, const unsigned int mpp_flags
);
172 #define MPP_PRE_SELECT (1<<0)
173 #define MPP_CONDITIONAL_PRE_SELECT (1<<1)
174 #define MPP_CLOSE_ON_SIGNAL (1<<2)
175 #define MPP_RECORD_TOUCH (1<<3)
176 bool multi_process_post (struct multi_context
*m
, struct multi_instance
*mi
, const unsigned int flags
);
178 bool multi_process_incoming_link (struct multi_context
*m
, struct multi_instance
*instance
, const unsigned int mpp_flags
);
179 bool multi_process_incoming_tun (struct multi_context
*m
, const unsigned int mpp_flags
);
181 void multi_process_drop_outgoing_tun (struct multi_context
*m
, const unsigned int mpp_flags
);
183 void multi_print_status (struct multi_context
*m
, struct status_output
*so
, const int version
);
185 struct multi_instance
*multi_get_queue (struct mbuf_set
*ms
);
187 void multi_add_mbuf (struct multi_context
*m
,
188 struct multi_instance
*mi
,
189 struct mbuf_buffer
*mb
);
191 void multi_ifconfig_pool_persist (struct multi_context
*m
, bool force
);
193 bool multi_process_signal (struct multi_context
*m
);
195 void multi_close_instance_on_signal (struct multi_context
*m
, struct multi_instance
*mi
);
197 void init_management_callback_multi (struct multi_context
*m
);
198 void uninit_management_callback_multi (struct multi_context
*m
);
201 * Return true if our output queue is not full
204 multi_output_queue_ready (const struct multi_context
*m
,
205 const struct multi_instance
*mi
)
207 if (mi
->tcp_link_out_deferred
)
208 return mbuf_len (mi
->tcp_link_out_deferred
) <= m
->tcp_queue_limit
;
214 * Determine which instance has pending output
215 * and prepare the output for sending in
216 * the to_link buffer.
218 static inline struct multi_instance
*
219 multi_process_outgoing_link_pre (struct multi_context
*m
)
221 struct multi_instance
*mi
= NULL
;
225 else if (mbuf_defined (m
->mbuf
))
226 mi
= multi_get_queue (m
->mbuf
);
231 * Per-client route quota management
234 void route_quota_exceeded (const struct multi_context
*m
, const struct multi_instance
*mi
);
237 route_quota_inc (struct multi_instance
*mi
)
243 route_quota_dec (struct multi_instance
*mi
)
248 /* can we add a new route? */
250 route_quota_test (const struct multi_context
*m
, const struct multi_instance
*mi
)
252 if (mi
->route_count
>= mi
->context
.options
.max_routes_per_client
)
254 route_quota_exceeded (m
, mi
);
262 * Instance reference counting
266 multi_instance_inc_refcount (struct multi_instance
*mi
)
272 multi_instance_dec_refcount (struct multi_instance
*mi
)
274 if (--mi
->refcount
<= 0)
277 mutex_destroy (&mi
->mutex
);
283 multi_route_del (struct multi_route
*route
)
285 struct multi_instance
*mi
= route
->instance
;
286 route_quota_dec (mi
);
287 multi_instance_dec_refcount (mi
);
292 multi_route_defined (const struct multi_context
*m
,
293 const struct multi_route
*r
)
295 if (r
->instance
->halt
)
297 else if ((r
->flags
& MULTI_ROUTE_CACHE
)
298 && r
->cache_generation
!= m
->route_helper
->cache_generation
)
300 else if ((r
->flags
& MULTI_ROUTE_AGEABLE
)
301 && r
->last_reference
+ m
->route_helper
->ageable_ttl_secs
< now
)
308 * Set a msg() function prefix with our current client instance ID.
312 set_prefix (struct multi_instance
*mi
)
314 #ifdef MULTI_DEBUG_EVENT_LOOP
316 printf ("[%s]\n", mi
->msg_prefix
);
318 msg_set_prefix (mi
->msg_prefix
);
324 #ifdef MULTI_DEBUG_EVENT_LOOP
327 msg_set_prefix (NULL
);
333 * Reaper constants. The reaper is the process where the virtual address
334 * and virtual route hash table is scanned for dead entries which are
335 * then removed. The hash table could potentially be quite large, so we
336 * don't want to reap in a single pass.
339 #define REAP_MAX_WAKEUP 10 /* Do reap pass at least once per n seconds */
340 #define REAP_DIVISOR 256 /* How many passes to cover whole hash table */
341 #define REAP_MIN 16 /* Minimum number of buckets per pass */
342 #define REAP_MAX 1024 /* Maximum number of buckets per pass */
345 * Mark a cached host route for deletion after this
346 * many seconds without any references.
348 #define MULTI_CACHE_ROUTE_TTL 60
351 multi_reap_process (const struct multi_context
*m
)
353 void multi_reap_process_dowork (const struct multi_context
*m
);
354 if (m
->reaper
->last_call
!= now
)
355 multi_reap_process_dowork (m
);
359 multi_process_per_second_timers (struct multi_context
*m
)
361 if (m
->per_second_trigger
!= now
)
363 void multi_process_per_second_timers_dowork (struct multi_context
*m
);
364 multi_process_per_second_timers_dowork (m
);
365 m
->per_second_trigger
= now
;
370 * Compute earliest timeout expiry from the set of
371 * all instances. Output:
373 * m->earliest_wakeup : instance needing the earliest service.
374 * dest : earliest timeout as a delta in relation
378 multi_get_timeout (struct multi_context
*m
, struct timeval
*dest
)
380 struct timeval tv
, current
;
383 m
->earliest_wakeup
= (struct multi_instance
*) schedule_get_earliest_wakeup (m
->schedule
, &tv
);
384 if (m
->earliest_wakeup
)
386 ASSERT (!openvpn_gettimeofday (¤t
, NULL
));
387 tv_delta (dest
, ¤t
, &tv
);
388 if (dest
->tv_sec
>= REAP_MAX_WAKEUP
)
390 m
->earliest_wakeup
= NULL
;
391 dest
->tv_sec
= REAP_MAX_WAKEUP
;
397 dest
->tv_sec
= REAP_MAX_WAKEUP
;
403 * Send a packet to TUN/TAP interface.
406 multi_process_outgoing_tun (struct multi_context
*m
, const unsigned int mpp_flags
)
408 struct multi_instance
*mi
= m
->pending
;
412 #ifdef MULTI_DEBUG_EVENT_LOOP
413 printf ("%s -> TUN len=%d\n",
415 mi
->context
.c2
.to_tun
.len
);
418 process_outgoing_tun (&mi
->context
);
419 ret
= multi_process_post (m
, mi
, mpp_flags
);
425 multi_process_outgoing_link_dowork (struct multi_context
*m
, struct multi_instance
*mi
, const unsigned int mpp_flags
)
429 process_outgoing_link (&mi
->context
);
430 ret
= multi_process_post (m
, mi
, mpp_flags
);
438 #define MULTI_CHECK_SIG(m) EVENT_LOOP_CHECK_SIGNAL (&(m)->top, multi_process_signal, (m))
441 multi_set_pending (struct multi_context
*m
, struct multi_instance
*mi
)
447 multi_release_io_lock (struct multi_context
*m
)
451 #endif /* P2MP_SERVER */