2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@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
26 * These routines implement a reliability layer on top of UDP,
27 * so that SSL/TLS can be run over UDP.
31 #include "config-win32.h"
36 #if defined(USE_CRYPTO) && defined(USE_SSL)
47 /* check if a particular packet_id is present in ack */
49 reliable_ack_packet_id_present (struct reliable_ack
*ack
, packet_id_type pid
)
52 for (i
= 0; i
< ack
->len
; ++i
)
53 if (ack
->packet_id
[i
] == pid
)
58 /* get a packet_id from buf */
60 reliable_ack_read_packet_id (struct buffer
*buf
, packet_id_type
*pid
)
62 packet_id_type net_pid
;
64 if (buf_read (buf
, &net_pid
, sizeof (net_pid
)))
66 *pid
= ntohpid (net_pid
);
67 dmsg (D_REL_DEBUG
, "ACK read ID " packet_id_format
" (buf->len=%d)",
68 (packet_id_print_type
)*pid
, buf
->len
);
72 dmsg (D_REL_LOW
, "ACK read ID FAILED (buf->len=%d)", buf
->len
);
76 /* acknowledge a packet_id by adding it to a struct reliable_ack */
78 reliable_ack_acknowledge_packet_id (struct reliable_ack
*ack
, packet_id_type pid
)
80 if (!reliable_ack_packet_id_present (ack
, pid
) && ack
->len
< RELIABLE_ACK_SIZE
)
82 ack
->packet_id
[ack
->len
++] = pid
;
83 dmsg (D_REL_DEBUG
, "ACK acknowledge ID " packet_id_format
" (ack->len=%d)",
84 (packet_id_print_type
)pid
, ack
->len
);
88 dmsg (D_REL_LOW
, "ACK acknowledge ID " packet_id_format
" FAILED (ack->len=%d)",
89 (packet_id_print_type
)pid
, ack
->len
);
93 /* read a packet ID acknowledgement record from buf into ack */
95 reliable_ack_read (struct reliable_ack
* ack
,
96 struct buffer
* buf
, const struct session_id
* sid
)
98 struct gc_arena gc
= gc_new ();
101 packet_id_type net_pid
;
103 struct session_id session_id_remote
;
105 if (!buf_read (buf
, &count
, sizeof (count
)))
107 for (i
= 0; i
< count
; ++i
)
109 if (!buf_read (buf
, &net_pid
, sizeof (net_pid
)))
111 if (ack
->len
>= RELIABLE_ACK_SIZE
)
113 pid
= ntohpid (net_pid
);
114 ack
->packet_id
[ack
->len
++] = pid
;
118 if (!session_id_read (&session_id_remote
, buf
))
120 if (!session_id_defined (&session_id_remote
) ||
121 !session_id_equal (&session_id_remote
, sid
))
124 "ACK read BAD SESSION-ID FROM REMOTE, local=%s, remote=%s",
125 session_id_print (sid
, &gc
), session_id_print (&session_id_remote
, &gc
));
137 #define ACK_SIZE(n) (sizeof (uint8_t) + ((n) ? SID_SIZE : 0) + sizeof (packet_id_type) * (n))
139 /* write a packet ID acknowledgement record to buf, */
140 /* removing all acknowledged entries from ack */
142 reliable_ack_write (struct reliable_ack
* ack
,
144 const struct session_id
* sid
, int max
, bool prepend
)
153 sub
= buf_sub (buf
, ACK_SIZE(n
), prepend
);
156 ASSERT (buf_write (&sub
, &n
, sizeof (n
)));
157 for (i
= 0; i
< n
; ++i
)
159 packet_id_type pid
= ack
->packet_id
[i
];
160 packet_id_type net_pid
= htonpid (pid
);
161 ASSERT (buf_write (&sub
, &net_pid
, sizeof (net_pid
)));
162 dmsg (D_REL_DEBUG
, "ACK write ID " packet_id_format
" (ack->len=%d, n=%d)", (packet_id_print_type
)pid
, ack
->len
, n
);
166 ASSERT (session_id_defined (sid
));
167 ASSERT (session_id_write (sid
, &sub
));
168 for (i
= 0, j
= n
; j
< ack
->len
;)
169 ack
->packet_id
[i
++] = ack
->packet_id
[j
++];
179 /* add to extra_frame the maximum number of bytes we will need for reliable_ack_write */
181 reliable_ack_adjust_frame_parameters (struct frame
* frame
, int max
)
183 frame_add_to_extra_frame (frame
, ACK_SIZE (max
));
186 /* print a reliable ACK record coming off the wire */
188 reliable_ack_print (struct buffer
*buf
, bool verbose
, struct gc_arena
*gc
)
192 struct session_id sid_ack
;
194 struct buffer out
= alloc_buf_gc (256, gc
);
196 buf_printf (&out
, "[");
197 if (!buf_read (buf
, &n_ack
, sizeof (n_ack
)))
199 for (i
= 0; i
< n_ack
; ++i
)
201 if (!buf_read (buf
, &pid
, sizeof (pid
)))
204 buf_printf (&out
, " " packet_id_format
, (packet_id_print_type
)pid
);
208 if (!session_id_read (&sid_ack
, buf
))
211 buf_printf (&out
, " sid=%s", session_id_print (&sid_ack
, gc
));
215 buf_printf (&out
, " ]");
220 * struct reliable member functions.
224 reliable_init (struct reliable
*rel
, int buf_size
, int offset
, int array_size
)
229 ASSERT (array_size
> 0 && array_size
<= RELIABLE_CAPACITY
);
230 rel
->size
= array_size
;
231 rel
->offset
= offset
;
232 for (i
= 0; i
< rel
->size
; ++i
)
234 struct reliable_entry
*e
= &rel
->array
[i
];
235 e
->buf
= alloc_buf (buf_size
);
236 ASSERT (buf_init (&e
->buf
, offset
));
241 reliable_free (struct reliable
*rel
)
244 for (i
= 0; i
< rel
->size
; ++i
)
246 struct reliable_entry
*e
= &rel
->array
[i
];
251 /* no active buffers? */
253 reliable_empty (const struct reliable
*rel
)
256 for (i
= 0; i
< rel
->size
; ++i
)
258 const struct reliable_entry
*e
= &rel
->array
[i
];
265 /* del acknowledged items from send buf */
267 reliable_send_purge (struct reliable
*rel
, struct reliable_ack
*ack
)
270 for (i
= 0; i
< ack
->len
; ++i
)
272 packet_id_type pid
= ack
->packet_id
[i
];
273 for (j
= 0; j
< rel
->size
; ++j
)
275 struct reliable_entry
*e
= &rel
->array
[j
];
276 if (e
->active
&& e
->packet_id
== pid
)
279 "ACK received for pid " packet_id_format
", deleting from send buffer",
280 (packet_id_print_type
)pid
);
282 /* DEBUGGING -- how close were we timing out on ACK failure and resending? */
286 const interval_t wake
= e
->next_try
- now
;
287 msg (M_INFO
, "ACK " packet_id_format
", wake=%d", pid
, wake
);
298 /* print the current sequence of active packet IDs */
300 reliable_print_ids (const struct reliable
*rel
, struct gc_arena
*gc
)
302 struct buffer out
= alloc_buf_gc (256, gc
);
305 buf_printf (&out
, "[" packet_id_format
"]", (packet_id_print_type
)rel
->packet_id
);
306 for (i
= 0; i
< rel
->size
; ++i
)
308 const struct reliable_entry
*e
= &rel
->array
[i
];
310 buf_printf (&out
, " " packet_id_format
, (packet_id_print_type
)e
->packet_id
);
315 /* true if at least one free buffer available */
317 reliable_can_get (const struct reliable
*rel
)
319 struct gc_arena gc
= gc_new ();
321 for (i
= 0; i
< rel
->size
; ++i
)
323 const struct reliable_entry
*e
= &rel
->array
[i
];
327 dmsg (D_REL_LOW
, "ACK no free receive buffer available: %s", reliable_print_ids (rel
, &gc
));
332 /* make sure that incoming packet ID isn't a replay */
334 reliable_not_replay (const struct reliable
*rel
, packet_id_type id
)
336 struct gc_arena gc
= gc_new ();
338 if (id
< rel
->packet_id
)
340 for (i
= 0; i
< rel
->size
; ++i
)
342 const struct reliable_entry
*e
= &rel
->array
[i
];
343 if (e
->active
&& e
->packet_id
== id
)
350 dmsg (D_REL_DEBUG
, "ACK " packet_id_format
" is a replay: %s", (packet_id_print_type
)id
, reliable_print_ids (rel
, &gc
));
355 /* make sure that incoming packet ID won't deadlock the receive buffer */
357 reliable_wont_break_sequentiality (const struct reliable
*rel
, packet_id_type id
)
359 struct gc_arena gc
= gc_new ();
362 if ((int)id
< (int)rel
->packet_id
+ rel
->size
)
368 dmsg (D_REL_LOW
, "ACK " packet_id_format
" breaks sequentiality: %s",
369 (packet_id_print_type
)id
, reliable_print_ids (rel
, &gc
));
376 /* grab a free buffer */
378 reliable_get_buf (struct reliable
*rel
)
381 for (i
= 0; i
< rel
->size
; ++i
)
383 struct reliable_entry
*e
= &rel
->array
[i
];
386 ASSERT (buf_init (&e
->buf
, rel
->offset
));
393 /* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */
395 reliable_get_buf_output_sequenced (struct reliable
*rel
)
397 struct gc_arena gc
= gc_new ();
399 packet_id_type min_id
= 0;
400 bool min_id_defined
= false;
401 struct buffer
*ret
= NULL
;
403 /* find minimum active packet_id */
404 for (i
= 0; i
< rel
->size
; ++i
)
406 const struct reliable_entry
*e
= &rel
->array
[i
];
409 if (!min_id_defined
|| e
->packet_id
< min_id
)
411 min_id_defined
= true;
412 min_id
= e
->packet_id
;
417 if (!min_id_defined
|| (int)(rel
->packet_id
- min_id
) < rel
->size
)
419 ret
= reliable_get_buf (rel
);
423 dmsg (D_REL_LOW
, "ACK output sequence broken: %s", reliable_print_ids (rel
, &gc
));
429 /* get active buffer for next sequentially increasing key ID */
431 reliable_get_buf_sequenced (struct reliable
*rel
)
434 for (i
= 0; i
< rel
->size
; ++i
)
436 struct reliable_entry
*e
= &rel
->array
[i
];
437 if (e
->active
&& e
->packet_id
== rel
->packet_id
)
445 /* return true if reliable_send would return a non-NULL result */
447 reliable_can_send (const struct reliable
*rel
)
449 struct gc_arena gc
= gc_new ();
451 int n_active
= 0, n_current
= 0;
452 for (i
= 0; i
< rel
->size
; ++i
)
454 const struct reliable_entry
*e
= &rel
->array
[i
];
458 if (now
>= e
->next_try
)
462 dmsg (D_REL_DEBUG
, "ACK reliable_can_send active=%d current=%d : %s",
465 reliable_print_ids (rel
, &gc
));
468 return n_current
> 0;
471 /* return a unique point-in-time to trigger retry */
473 reliable_unique_retry (struct reliable
*rel
, time_t retry
)
478 for (i
= 0; i
< rel
->size
; ++i
)
480 struct reliable_entry
*e
= &rel
->array
[i
];
481 if (e
->active
&& e
->next_try
== retry
)
491 /* return next buffer to send to remote */
493 reliable_send (struct reliable
*rel
, int *opcode
)
496 struct reliable_entry
*best
= NULL
;
497 const time_t local_now
= now
;
499 for (i
= 0; i
< rel
->size
; ++i
)
501 struct reliable_entry
*e
= &rel
->array
[i
];
502 if (e
->active
&& local_now
>= e
->next_try
)
504 if (!best
|| e
->packet_id
< best
->packet_id
)
510 #ifdef EXPONENTIAL_BACKOFF
511 /* exponential backoff */
512 best
->next_try
= reliable_unique_retry (rel
, local_now
+ best
->timeout
);
515 /* constant timeout, no backoff */
516 best
->next_try
= local_now
+ best
->timeout
;
518 *opcode
= best
->opcode
;
519 dmsg (D_REL_DEBUG
, "ACK reliable_send ID " packet_id_format
" (size=%d to=%d)",
520 (packet_id_print_type
)best
->packet_id
, best
->buf
.len
,
521 (int)(best
->next_try
- local_now
));
527 /* schedule all pending packets for immediate retransmit */
529 reliable_schedule_now (struct reliable
*rel
)
532 dmsg (D_REL_DEBUG
, "ACK reliable_schedule_now");
533 for (i
= 0; i
< rel
->size
; ++i
)
535 struct reliable_entry
*e
= &rel
->array
[i
];
539 e
->timeout
= rel
->initial_timeout
;
544 /* in how many seconds should we wake up to check for timeout */
545 /* if we return BIG_TIMEOUT, nothing to wait for */
547 reliable_send_timeout (const struct reliable
*rel
)
549 struct gc_arena gc
= gc_new ();
550 interval_t ret
= BIG_TIMEOUT
;
552 const time_t local_now
= now
;
554 for (i
= 0; i
< rel
->size
; ++i
)
556 const struct reliable_entry
*e
= &rel
->array
[i
];
559 if (e
->next_try
<= local_now
)
566 ret
= min_int (ret
, e
->next_try
- local_now
);
571 dmsg (D_REL_DEBUG
, "ACK reliable_send_timeout %d %s",
573 reliable_print_ids (rel
, &gc
));
580 * Enable an incoming buffer previously returned by a get function as active.
584 reliable_mark_active_incoming (struct reliable
*rel
, struct buffer
*buf
,
585 packet_id_type pid
, int opcode
)
588 for (i
= 0; i
< rel
->size
; ++i
)
590 struct reliable_entry
*e
= &rel
->array
[i
];
595 /* packets may not arrive in sequential order */
598 /* check for replay */
599 ASSERT (pid
>= rel
->packet_id
);
604 dmsg (D_REL_DEBUG
, "ACK mark active incoming ID " packet_id_format
, (packet_id_print_type
)e
->packet_id
);
608 ASSERT (0); /* buf not found in rel */
612 * Enable an outgoing buffer previously returned by a get function as active.
616 reliable_mark_active_outgoing (struct reliable
*rel
, struct buffer
*buf
, int opcode
)
619 for (i
= 0; i
< rel
->size
; ++i
)
621 struct reliable_entry
*e
= &rel
->array
[i
];
624 /* Write mode, increment packet_id (i.e. sequence number)
625 linearly and prepend id to packet */
626 packet_id_type net_pid
;
627 e
->packet_id
= rel
->packet_id
++;
628 net_pid
= htonpid (e
->packet_id
);
629 ASSERT (buf_write_prepend (buf
, &net_pid
, sizeof (net_pid
)));
633 e
->timeout
= rel
->initial_timeout
;
634 dmsg (D_REL_DEBUG
, "ACK mark active outgoing ID " packet_id_format
, (packet_id_print_type
)e
->packet_id
);
638 ASSERT (0); /* buf not found in rel */
641 /* delete a buffer previously activated by reliable_mark_active() */
643 reliable_mark_deleted (struct reliable
*rel
, struct buffer
*buf
, bool inc_pid
)
646 for (i
= 0; i
< rel
->size
; ++i
)
648 struct reliable_entry
*e
= &rel
->array
[i
];
653 rel
->packet_id
= e
->packet_id
+ 1;
663 reliable_ack_debug_print (const struct reliable_ack
*ack
, char *desc
)
667 printf ("********* struct reliable_ack %s\n", desc
);
668 for (i
= 0; i
< ack
->len
; ++i
)
670 printf (" %d: " packet_id_format
"\n", i
, (packet_id_print_type
) ack
->packet_id
[i
]);
675 reliable_debug_print (const struct reliable
*rel
, char *desc
)
680 printf ("********* struct reliable %s\n", desc
);
681 printf (" initial_timeout=%d\n", (int)rel
->initial_timeout
);
682 printf (" packet_id=" packet_id_format
"\n", rel
->packet_id
);
683 printf (" now=" time_format
"\n", now
);
684 for (i
= 0; i
< rel
->size
; ++i
)
686 const struct reliable_entry
*e
= &rel
->array
[i
];
689 printf (" %d: packet_id=" packet_id_format
" len=%d", i
, e
->packet_id
, e
->buf
.len
);
690 printf (" next_try=" time_format
, e
->next_try
);
699 static void dummy(void) {}
700 #endif /* USE_CRYPTO && USE_SSL*/