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-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
26 * These routines implement a reliability layer on top of UDP,
27 * so that SSL/TLS can be run over UDP.
32 #if defined(USE_CRYPTO) && defined(USE_SSL)
42 * verify that test - base < extent while allowing for base or test wraparound
45 reliable_pid_in_range1 (const packet_id_type test
,
46 const packet_id_type base
,
47 const unsigned int extent
)
51 if (test
- base
< extent
)
56 if ((test
+0x80000000u
) - (base
+0x80000000u
) < extent
)
64 * verify that test < base + extent while allowing for base or test wraparound
67 reliable_pid_in_range2 (const packet_id_type test
,
68 const packet_id_type base
,
69 const unsigned int extent
)
71 if (base
+ extent
>= base
)
73 if (test
< base
+ extent
)
78 if ((test
+0x80000000u
) < (base
+0x80000000u
) + extent
)
86 * verify that p1 < p2 while allowing for p1 or p2 wraparound
89 reliable_pid_min (const packet_id_type p1
,
90 const packet_id_type p2
)
92 return !reliable_pid_in_range1 (p1
, p2
, 0x80000000u
);
95 /* check if a particular packet_id is present in ack */
97 reliable_ack_packet_id_present (struct reliable_ack
*ack
, packet_id_type pid
)
100 for (i
= 0; i
< ack
->len
; ++i
)
101 if (ack
->packet_id
[i
] == pid
)
106 /* get a packet_id from buf */
108 reliable_ack_read_packet_id (struct buffer
*buf
, packet_id_type
*pid
)
110 packet_id_type net_pid
;
112 if (buf_read (buf
, &net_pid
, sizeof (net_pid
)))
114 *pid
= ntohpid (net_pid
);
115 dmsg (D_REL_DEBUG
, "ACK read ID " packet_id_format
" (buf->len=%d)",
116 (packet_id_print_type
)*pid
, buf
->len
);
120 dmsg (D_REL_LOW
, "ACK read ID FAILED (buf->len=%d)", buf
->len
);
124 /* acknowledge a packet_id by adding it to a struct reliable_ack */
126 reliable_ack_acknowledge_packet_id (struct reliable_ack
*ack
, packet_id_type pid
)
128 if (!reliable_ack_packet_id_present (ack
, pid
) && ack
->len
< RELIABLE_ACK_SIZE
)
130 ack
->packet_id
[ack
->len
++] = pid
;
131 dmsg (D_REL_DEBUG
, "ACK acknowledge ID " packet_id_format
" (ack->len=%d)",
132 (packet_id_print_type
)pid
, ack
->len
);
136 dmsg (D_REL_LOW
, "ACK acknowledge ID " packet_id_format
" FAILED (ack->len=%d)",
137 (packet_id_print_type
)pid
, ack
->len
);
141 /* read a packet ID acknowledgement record from buf into ack */
143 reliable_ack_read (struct reliable_ack
* ack
,
144 struct buffer
* buf
, const struct session_id
* sid
)
146 struct gc_arena gc
= gc_new ();
149 packet_id_type net_pid
;
151 struct session_id session_id_remote
;
153 if (!buf_read (buf
, &count
, sizeof (count
)))
155 for (i
= 0; i
< count
; ++i
)
157 if (!buf_read (buf
, &net_pid
, sizeof (net_pid
)))
159 if (ack
->len
>= RELIABLE_ACK_SIZE
)
161 pid
= ntohpid (net_pid
);
162 ack
->packet_id
[ack
->len
++] = pid
;
166 if (!session_id_read (&session_id_remote
, buf
))
168 if (!session_id_defined (&session_id_remote
) ||
169 !session_id_equal (&session_id_remote
, sid
))
172 "ACK read BAD SESSION-ID FROM REMOTE, local=%s, remote=%s",
173 session_id_print (sid
, &gc
), session_id_print (&session_id_remote
, &gc
));
185 #define ACK_SIZE(n) (sizeof (uint8_t) + ((n) ? SID_SIZE : 0) + sizeof (packet_id_type) * (n))
187 /* write a packet ID acknowledgement record to buf, */
188 /* removing all acknowledged entries from ack */
190 reliable_ack_write (struct reliable_ack
* ack
,
192 const struct session_id
* sid
, int max
, bool prepend
)
201 sub
= buf_sub (buf
, ACK_SIZE(n
), prepend
);
204 ASSERT (buf_write (&sub
, &n
, sizeof (n
)));
205 for (i
= 0; i
< n
; ++i
)
207 packet_id_type pid
= ack
->packet_id
[i
];
208 packet_id_type net_pid
= htonpid (pid
);
209 ASSERT (buf_write (&sub
, &net_pid
, sizeof (net_pid
)));
210 dmsg (D_REL_DEBUG
, "ACK write ID " packet_id_format
" (ack->len=%d, n=%d)", (packet_id_print_type
)pid
, ack
->len
, n
);
214 ASSERT (session_id_defined (sid
));
215 ASSERT (session_id_write (sid
, &sub
));
216 for (i
= 0, j
= n
; j
< ack
->len
;)
217 ack
->packet_id
[i
++] = ack
->packet_id
[j
++];
227 /* add to extra_frame the maximum number of bytes we will need for reliable_ack_write */
229 reliable_ack_adjust_frame_parameters (struct frame
* frame
, int max
)
231 frame_add_to_extra_frame (frame
, ACK_SIZE (max
));
234 /* print a reliable ACK record coming off the wire */
236 reliable_ack_print (struct buffer
*buf
, bool verbose
, struct gc_arena
*gc
)
240 struct session_id sid_ack
;
242 struct buffer out
= alloc_buf_gc (256, gc
);
244 buf_printf (&out
, "[");
245 if (!buf_read (buf
, &n_ack
, sizeof (n_ack
)))
247 for (i
= 0; i
< n_ack
; ++i
)
249 if (!buf_read (buf
, &pid
, sizeof (pid
)))
252 buf_printf (&out
, " " packet_id_format
, (packet_id_print_type
)pid
);
256 if (!session_id_read (&sid_ack
, buf
))
259 buf_printf (&out
, " sid=%s", session_id_print (&sid_ack
, gc
));
263 buf_printf (&out
, " ]");
268 * struct reliable member functions.
272 reliable_init (struct reliable
*rel
, int buf_size
, int offset
, int array_size
, bool hold
)
277 ASSERT (array_size
> 0 && array_size
<= RELIABLE_CAPACITY
);
279 rel
->size
= array_size
;
280 rel
->offset
= offset
;
281 for (i
= 0; i
< rel
->size
; ++i
)
283 struct reliable_entry
*e
= &rel
->array
[i
];
284 e
->buf
= alloc_buf (buf_size
);
285 ASSERT (buf_init (&e
->buf
, offset
));
290 reliable_free (struct reliable
*rel
)
293 for (i
= 0; i
< rel
->size
; ++i
)
295 struct reliable_entry
*e
= &rel
->array
[i
];
300 /* no active buffers? */
302 reliable_empty (const struct reliable
*rel
)
305 for (i
= 0; i
< rel
->size
; ++i
)
307 const struct reliable_entry
*e
= &rel
->array
[i
];
314 /* del acknowledged items from send buf */
316 reliable_send_purge (struct reliable
*rel
, struct reliable_ack
*ack
)
319 for (i
= 0; i
< ack
->len
; ++i
)
321 packet_id_type pid
= ack
->packet_id
[i
];
322 for (j
= 0; j
< rel
->size
; ++j
)
324 struct reliable_entry
*e
= &rel
->array
[j
];
325 if (e
->active
&& e
->packet_id
== pid
)
328 "ACK received for pid " packet_id_format
", deleting from send buffer",
329 (packet_id_print_type
)pid
);
331 /* DEBUGGING -- how close were we timing out on ACK failure and resending? */
335 const interval_t wake
= e
->next_try
- now
;
336 msg (M_INFO
, "ACK " packet_id_format
", wake=%d", pid
, wake
);
347 /* print the current sequence of active packet IDs */
349 reliable_print_ids (const struct reliable
*rel
, struct gc_arena
*gc
)
351 struct buffer out
= alloc_buf_gc (256, gc
);
354 buf_printf (&out
, "[" packet_id_format
"]", (packet_id_print_type
)rel
->packet_id
);
355 for (i
= 0; i
< rel
->size
; ++i
)
357 const struct reliable_entry
*e
= &rel
->array
[i
];
359 buf_printf (&out
, " " packet_id_format
, (packet_id_print_type
)e
->packet_id
);
364 /* true if at least one free buffer available */
366 reliable_can_get (const struct reliable
*rel
)
368 struct gc_arena gc
= gc_new ();
370 for (i
= 0; i
< rel
->size
; ++i
)
372 const struct reliable_entry
*e
= &rel
->array
[i
];
376 dmsg (D_REL_LOW
, "ACK no free receive buffer available: %s", reliable_print_ids (rel
, &gc
));
381 /* make sure that incoming packet ID isn't a replay */
383 reliable_not_replay (const struct reliable
*rel
, packet_id_type id
)
385 struct gc_arena gc
= gc_new ();
387 if (reliable_pid_min (id
, rel
->packet_id
))
389 for (i
= 0; i
< rel
->size
; ++i
)
391 const struct reliable_entry
*e
= &rel
->array
[i
];
392 if (e
->active
&& e
->packet_id
== id
)
399 dmsg (D_REL_DEBUG
, "ACK " packet_id_format
" is a replay: %s", (packet_id_print_type
)id
, reliable_print_ids (rel
, &gc
));
404 /* make sure that incoming packet ID won't deadlock the receive buffer */
406 reliable_wont_break_sequentiality (const struct reliable
*rel
, packet_id_type id
)
408 struct gc_arena gc
= gc_new ();
410 const int ret
= reliable_pid_in_range2 (id
, rel
->packet_id
, rel
->size
);
414 dmsg (D_REL_LOW
, "ACK " packet_id_format
" breaks sequentiality: %s",
415 (packet_id_print_type
)id
, reliable_print_ids (rel
, &gc
));
418 dmsg (D_REL_DEBUG
, "ACK RWBS rel->size=%d rel->packet_id=%08x id=%08x ret=%d\n", rel
->size
, rel
->packet_id
, id
, ret
);
424 /* grab a free buffer */
426 reliable_get_buf (struct reliable
*rel
)
429 for (i
= 0; i
< rel
->size
; ++i
)
431 struct reliable_entry
*e
= &rel
->array
[i
];
434 ASSERT (buf_init (&e
->buf
, rel
->offset
));
441 /* grab a free buffer, fail if buffer clogged by unacknowledged low packet IDs */
443 reliable_get_buf_output_sequenced (struct reliable
*rel
)
445 struct gc_arena gc
= gc_new ();
447 packet_id_type min_id
= 0;
448 bool min_id_defined
= false;
449 struct buffer
*ret
= NULL
;
451 /* find minimum active packet_id */
452 for (i
= 0; i
< rel
->size
; ++i
)
454 const struct reliable_entry
*e
= &rel
->array
[i
];
457 if (!min_id_defined
|| reliable_pid_min (e
->packet_id
, min_id
))
459 min_id_defined
= true;
460 min_id
= e
->packet_id
;
465 if (!min_id_defined
|| reliable_pid_in_range1 (rel
->packet_id
, min_id
, rel
->size
))
467 ret
= reliable_get_buf (rel
);
471 dmsg (D_REL_LOW
, "ACK output sequence broken: %s", reliable_print_ids (rel
, &gc
));
477 /* get active buffer for next sequentially increasing key ID */
479 reliable_get_buf_sequenced (struct reliable
*rel
)
482 for (i
= 0; i
< rel
->size
; ++i
)
484 struct reliable_entry
*e
= &rel
->array
[i
];
485 if (e
->active
&& e
->packet_id
== rel
->packet_id
)
493 /* return true if reliable_send would return a non-NULL result */
495 reliable_can_send (const struct reliable
*rel
)
497 struct gc_arena gc
= gc_new ();
499 int n_active
= 0, n_current
= 0;
500 for (i
= 0; i
< rel
->size
; ++i
)
502 const struct reliable_entry
*e
= &rel
->array
[i
];
506 if (now
>= e
->next_try
)
510 dmsg (D_REL_DEBUG
, "ACK reliable_can_send active=%d current=%d : %s",
513 reliable_print_ids (rel
, &gc
));
516 return n_current
> 0 && !rel
->hold
;
519 /* return a unique point-in-time to trigger retry */
521 reliable_unique_retry (struct reliable
*rel
, time_t retry
)
526 for (i
= 0; i
< rel
->size
; ++i
)
528 struct reliable_entry
*e
= &rel
->array
[i
];
529 if (e
->active
&& e
->next_try
== retry
)
539 /* return next buffer to send to remote */
541 reliable_send (struct reliable
*rel
, int *opcode
)
544 struct reliable_entry
*best
= NULL
;
545 const time_t local_now
= now
;
547 for (i
= 0; i
< rel
->size
; ++i
)
549 struct reliable_entry
*e
= &rel
->array
[i
];
550 if (e
->active
&& local_now
>= e
->next_try
)
552 if (!best
|| reliable_pid_min (e
->packet_id
, best
->packet_id
))
558 #ifdef EXPONENTIAL_BACKOFF
559 /* exponential backoff */
560 best
->next_try
= reliable_unique_retry (rel
, local_now
+ best
->timeout
);
563 /* constant timeout, no backoff */
564 best
->next_try
= local_now
+ best
->timeout
;
566 *opcode
= best
->opcode
;
567 dmsg (D_REL_DEBUG
, "ACK reliable_send ID " packet_id_format
" (size=%d to=%d)",
568 (packet_id_print_type
)best
->packet_id
, best
->buf
.len
,
569 (int)(best
->next_try
- local_now
));
575 /* schedule all pending packets for immediate retransmit */
577 reliable_schedule_now (struct reliable
*rel
)
580 dmsg (D_REL_DEBUG
, "ACK reliable_schedule_now");
582 for (i
= 0; i
< rel
->size
; ++i
)
584 struct reliable_entry
*e
= &rel
->array
[i
];
588 e
->timeout
= rel
->initial_timeout
;
593 /* in how many seconds should we wake up to check for timeout */
594 /* if we return BIG_TIMEOUT, nothing to wait for */
596 reliable_send_timeout (const struct reliable
*rel
)
598 struct gc_arena gc
= gc_new ();
599 interval_t ret
= BIG_TIMEOUT
;
601 const time_t local_now
= now
;
603 for (i
= 0; i
< rel
->size
; ++i
)
605 const struct reliable_entry
*e
= &rel
->array
[i
];
608 if (e
->next_try
<= local_now
)
615 ret
= min_int (ret
, e
->next_try
- local_now
);
620 dmsg (D_REL_DEBUG
, "ACK reliable_send_timeout %d %s",
622 reliable_print_ids (rel
, &gc
));
629 * Enable an incoming buffer previously returned by a get function as active.
633 reliable_mark_active_incoming (struct reliable
*rel
, struct buffer
*buf
,
634 packet_id_type pid
, int opcode
)
637 for (i
= 0; i
< rel
->size
; ++i
)
639 struct reliable_entry
*e
= &rel
->array
[i
];
644 /* packets may not arrive in sequential order */
647 /* check for replay */
648 ASSERT (!reliable_pid_min (pid
, rel
->packet_id
));
653 dmsg (D_REL_DEBUG
, "ACK mark active incoming ID " packet_id_format
, (packet_id_print_type
)e
->packet_id
);
657 ASSERT (0); /* buf not found in rel */
661 * Enable an outgoing buffer previously returned by a get function as active.
665 reliable_mark_active_outgoing (struct reliable
*rel
, struct buffer
*buf
, int opcode
)
668 for (i
= 0; i
< rel
->size
; ++i
)
670 struct reliable_entry
*e
= &rel
->array
[i
];
673 /* Write mode, increment packet_id (i.e. sequence number)
674 linearly and prepend id to packet */
675 packet_id_type net_pid
;
676 e
->packet_id
= rel
->packet_id
++;
677 net_pid
= htonpid (e
->packet_id
);
678 ASSERT (buf_write_prepend (buf
, &net_pid
, sizeof (net_pid
)));
682 e
->timeout
= rel
->initial_timeout
;
683 dmsg (D_REL_DEBUG
, "ACK mark active outgoing ID " packet_id_format
, (packet_id_print_type
)e
->packet_id
);
687 ASSERT (0); /* buf not found in rel */
690 /* delete a buffer previously activated by reliable_mark_active() */
692 reliable_mark_deleted (struct reliable
*rel
, struct buffer
*buf
, bool inc_pid
)
695 for (i
= 0; i
< rel
->size
; ++i
)
697 struct reliable_entry
*e
= &rel
->array
[i
];
702 rel
->packet_id
= e
->packet_id
+ 1;
712 reliable_ack_debug_print (const struct reliable_ack
*ack
, char *desc
)
716 printf ("********* struct reliable_ack %s\n", desc
);
717 for (i
= 0; i
< ack
->len
; ++i
)
719 printf (" %d: " packet_id_format
"\n", i
, (packet_id_print_type
) ack
->packet_id
[i
]);
724 reliable_debug_print (const struct reliable
*rel
, char *desc
)
729 printf ("********* struct reliable %s\n", desc
);
730 printf (" initial_timeout=%d\n", (int)rel
->initial_timeout
);
731 printf (" packet_id=" packet_id_format
"\n", rel
->packet_id
);
732 printf (" now=" time_format
"\n", now
);
733 for (i
= 0; i
< rel
->size
; ++i
)
735 const struct reliable_entry
*e
= &rel
->array
[i
];
738 printf (" %d: packet_id=" packet_id_format
" len=%d", i
, e
->packet_id
, e
->buf
.len
);
739 printf (" next_try=" time_format
, e
->next_try
);
748 static void dummy(void) {}
749 #endif /* USE_CRYPTO && USE_SSL*/