OpenVPN
packet_id.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/*
24 * These routines are designed to catch replay attacks,
25 * where a man-in-the-middle captures packets and then
26 * attempts to replay them back later.
27 *
28 * We use the "sliding-window" algorithm, similar
29 * to IPSec.
30 */
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include "syshead.h"
37
38#include <stddef.h>
39
40#include "packet_id.h"
41#include "misc.h"
42#include "integer.h"
43
44#include "memdbg.h"
45
46/* #define PID_SIMULATE_BACKTRACK */
47
48/*
49 * Special time_t value that indicates that
50 * sequence number has expired.
51 */
52#define SEQ_UNSEEN ((time_t)0)
53#define SEQ_EXPIRED ((time_t)1)
54
55#ifndef ENABLE_SMALL
56static void packet_id_debug_print(msglvl_t msglevel, const struct packet_id_rec *p,
57 const struct packet_id_net *pin, const char *message,
59
60#endif
61
62static inline void
63packet_id_debug(msglvl_t msglevel, const struct packet_id_rec *p,
64 const struct packet_id_net *pin, const char *message, uint64_t value)
65{
66#ifndef ENABLE_SMALL
67 if (unlikely(check_debug_level(msglevel)))
68 {
69 packet_id_debug_print(msglevel, p, pin, message, value);
70 }
71#endif
72}
73
74#if defined(__GNUC__) || defined(__clang__)
75#pragma GCC diagnostic push
76#pragma GCC diagnostic ignored "-Wsign-conversion"
77#endif
78
79static void
80packet_id_init_recv(struct packet_id_rec *rec, int seq_backtrack, int time_backtrack,
81 const char *name, int unit)
82{
83 rec->name = name;
84 rec->unit = unit;
85 if (seq_backtrack)
86 {
87 ASSERT(MIN_SEQ_BACKTRACK <= seq_backtrack && seq_backtrack <= MAX_SEQ_BACKTRACK);
88 ASSERT(MIN_TIME_BACKTRACK <= time_backtrack && time_backtrack <= MAX_TIME_BACKTRACK);
89 CIRC_LIST_ALLOC(rec->seq_list, struct seq_list, seq_backtrack);
90 rec->seq_backtrack = seq_backtrack;
91 rec->time_backtrack = time_backtrack;
92 }
93 rec->initialized = true;
94}
95void
96packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name,
97 int unit)
98{
99 dmsg(D_PID_DEBUG, "PID packet_id_init seq_backtrack=%d time_backtrack=%d", seq_backtrack,
100 time_backtrack);
101
102 ASSERT(p);
103 CLEAR(*p);
104
105 packet_id_init_recv(&p->rec, seq_backtrack, time_backtrack, name, unit);
106}
107
108void
110{
111 ASSERT(src);
112 ASSERT(dest);
113 /* clear free any old data in rec list */
114 free(dest->seq_list);
115 CLEAR(*dest);
116
117 /* Copy data to dest */
118 *dest = *src;
119
120 /* Reinitalise the source */
121 CLEAR(*src);
122 packet_id_init_recv(src, (int)dest->seq_backtrack, dest->time_backtrack, dest->name, dest->unit);
123}
124
125void
127{
128 if (p)
129 {
130 dmsg(D_PID_DEBUG, "PID packet_id_free");
131 free(p->rec.seq_list);
132 CLEAR(*p);
133 }
134}
135
136void
137packet_id_add(struct packet_id_rec *p, const struct packet_id_net *pin)
138{
139 const time_t local_now = now;
140 if (p->seq_list)
141 {
142 int64_t diff;
143
144 /*
145 * If time value increases, start a new sequence list of number
146 * sequence for the new time point.
147 */
148 if (!CIRC_LIST_SIZE(p->seq_list) || pin->time > p->time
149 || (pin->id >= p->seq_backtrack && pin->id - p->seq_backtrack > p->id))
150 {
151 p->time = pin->time;
152 p->id = 0;
153 if (pin->id > p->seq_backtrack)
154 {
155 p->id = pin->id - p->seq_backtrack;
156 }
158 }
159
160 while (p->id < pin->id
161#ifdef PID_SIMULATE_BACKTRACK
162 || (get_random() % 64) < 31
163#endif
164 )
165 {
167 ++p->id;
168 }
169
170 diff = p->id - pin->id;
171 if (diff < CIRC_LIST_SIZE(p->seq_list) && local_now > SEQ_EXPIRED)
172 {
173 CIRC_LIST_ITEM(p->seq_list, (int)diff) = local_now;
174 }
175 }
176 else
177 {
178 p->time = pin->time;
179 p->id = pin->id;
180 }
181}
182
183#if defined(__GNUC__) || defined(__clang__)
184#pragma GCC diagnostic pop
185#endif
186
187/*
188 * Expire sequence numbers which can no longer
189 * be accepted because they would violate
190 * time_backtrack.
191 */
192void
194{
195 const time_t local_now = now;
196 if (p->time_backtrack)
197 {
198 bool expire = false;
199 for (int i = 0; i < CIRC_LIST_SIZE(p->seq_list); ++i)
200 {
201 const time_t t = CIRC_LIST_ITEM(p->seq_list, i);
202 if (t == SEQ_EXPIRED)
203 {
204 break;
205 }
206 if (!expire && t && t + p->time_backtrack < local_now)
207 {
208 expire = true;
209 }
210 if (expire)
211 {
213 }
214 }
215 }
216 p->last_reap = local_now;
217}
218
219/*
220 * Return true if packet id is ok, or false if
221 * it is a replay.
222 */
223bool
224packet_id_test(struct packet_id_rec *p, const struct packet_id_net *pin)
225{
226 packet_id_debug(D_PID_DEBUG, p, pin, "PID_TEST", 0);
227
229
230 if (!pin->id)
231 {
232 return false;
233 }
234
235 if (p->seq_backtrack)
236 {
237 /*
238 * In backtrack mode, we allow packet reordering subject
239 * to the seq_backtrack and time_backtrack constraints.
240 *
241 * This mode is used with UDP.
242 */
243 if (pin->time == p->time)
244 {
245 /* is packet-id greater than any one we've seen yet? */
246 if (pin->id > p->id)
247 {
248 return true;
249 }
250
251 /* check packet-id sliding window for original/replay status */
252 const uint64_t diff = p->id - pin->id;
253
254 /* keep track of maximum backtrack seen for debugging purposes */
255 if (diff > p->max_backtrack_stat)
256 {
257 p->max_backtrack_stat = diff;
258 packet_id_debug(D_PID_DEBUG_LOW, p, pin, "PID_ERR replay-window backtrack occurred",
260 }
261
262 if (diff >= (packet_id_type)CIRC_LIST_SIZE(p->seq_list))
263 {
264 packet_id_debug(D_PID_DEBUG_LOW, p, pin, "PID_ERR large diff", diff);
265 return false;
266 }
267
268 const time_t v = CIRC_LIST_ITEM(p->seq_list, (int)diff);
269 if (v == 0)
270 {
271 return true;
272 }
273 else
274 {
275 /* raised from D_PID_DEBUG_LOW to reduce verbosity */
276 packet_id_debug(D_PID_DEBUG_MEDIUM, p, pin, "PID_ERR replay", diff);
277 return false;
278 }
279 }
280 else if (pin->time < p->time) /* if time goes back, reject */
281 {
282 packet_id_debug(D_PID_DEBUG_LOW, p, pin, "PID_ERR time backtrack", 0);
283 return false;
284 }
285 else /* time moved forward */
286 {
287 return true;
288 }
289 }
290 else
291 {
292 /*
293 * In non-backtrack mode, all sequence number series must
294 * begin at some number n > 0 and must increment linearly without gaps.
295 *
296 * This mode is used with TCP.
297 */
298 if (pin->time == p->time)
299 {
300 return !p->id || pin->id == p->id + 1;
301 }
302 else if (pin->time < p->time) /* if time goes back, reject */
303 {
304 return false;
305 }
306 else /* time moved forward */
307 {
308 return pin->id == 1;
309 }
310 }
311}
312
313/*
314 * Read/write a packet ID to/from the buffer. Short form is sequence number
315 * only. Long form is sequence number and timestamp.
316 */
317
318bool
319packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool long_form)
320{
321 packet_id_type net_id;
322 net_time_t net_time;
323
324 pin->id = 0;
325 pin->time = 0;
326
327 if (!buf_read(buf, &net_id, sizeof(net_id)))
328 {
329 return false;
330 }
331 pin->id = ntohpid(net_id);
332 if (long_form)
333 {
334 if (!buf_read(buf, &net_time, sizeof(net_time)))
335 {
336 return false;
337 }
338 pin->time = ntohtime(net_time);
339 }
340 return true;
341}
342
343static bool
344packet_id_send_update(struct packet_id_send *p, bool long_form)
345{
346 if (!p->time)
347 {
348 p->time = now;
349 }
350 if (p->id == PACKET_ID_MAX)
351 {
352 /* Packet ID only allowed to roll over if using long form and time has
353 * moved forward since last roll over.
354 */
355 if (!long_form || now <= p->time)
356 {
357 return false;
358 }
359 p->time = now;
360 p->id = 0;
361 }
362 p->id++;
363 return true;
364}
365
366static bool
368{
369 if (!p->time)
370 {
371 p->time = now;
372 }
373 if (p->id == PACKET_ID_EPOCH_MAX)
374 {
375 return false;
376 }
377 p->id++;
378 return true;
379}
380
381bool
382packet_id_write(struct packet_id_send *p, struct buffer *buf, bool long_form, bool prepend)
383{
384 if (!packet_id_send_update(p, long_form))
385 {
386 return false;
387 }
388
389 const packet_id_type net_id = htonpid((packet_id_type)p->id);
390 const net_time_t net_time = htontime(p->time);
391 if (prepend)
392 {
393 if (long_form)
394 {
395 if (!buf_write_prepend(buf, &net_time, sizeof(net_time)))
396 {
397 return false;
398 }
399 }
400 if (!buf_write_prepend(buf, &net_id, sizeof(net_id)))
401 {
402 return false;
403 }
404 }
405 else
406 {
407 if (!buf_write(buf, &net_id, sizeof(net_id)))
408 {
409 return false;
410 }
411 if (long_form)
412 {
413 if (!buf_write(buf, &net_time, sizeof(net_time)))
414 {
415 return false;
416 }
417 }
418 }
419 return true;
420}
421
422const char *
423packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp, struct gc_arena *gc)
424{
425 struct buffer out = alloc_buf_gc(256, gc);
426
428 if (print_timestamp && pin->time)
429 {
430 buf_printf(&out, " / time = (" packet_id_format ") %s", (packet_id_print_type)pin->time,
431 time_string(pin->time, 0, false, gc));
432 }
433
434 buf_printf(&out, " ]");
435 return BSTR(&out);
436}
437
438#ifndef ENABLE_SMALL
439
440static void
442 const struct packet_id_net *pin,
443 const char *message, packet_id_print_type value)
444{
445 struct gc_arena gc = gc_new();
446 struct buffer out = alloc_buf_gc(256, &gc);
447 struct timeval tv;
448 const time_t prev_now = now;
449 const struct seq_list *sl = p->seq_list;
450 int i;
451
452 CLEAR(tv);
453 gettimeofday(&tv, NULL);
454
455 buf_printf(&out, "%s [" packet_id_format "]", message, value);
456 buf_printf(&out, " [%s-%d] [", p->name, p->unit);
457 for (i = 0; sl != NULL && i < sl->x_size; ++i)
458 {
459 char c;
460 time_t v;
461
462 v = CIRC_LIST_ITEM(sl, i);
463 if (v == SEQ_UNSEEN)
464 {
465 c = '_';
466 }
467 else if (v == SEQ_EXPIRED)
468 {
469 c = 'E';
470 }
471 else
472 {
473 const int diff = (int)(prev_now - v);
474 if (diff < 0)
475 {
476 c = 'N';
477 }
478 else if (diff < 10)
479 {
480 c = (char)('0' + diff);
481 }
482 else
483 {
484 c = '>';
485 }
486 }
487 buf_printf(&out, "%c", c);
488 }
489 buf_printf(&out, "] %" PRIi64 ":" packet_id_format, (int64_t)p->time, p->id);
490 if (pin)
491 {
492 buf_printf(&out, " %" PRIi64 ":" packet_id_format, (int64_t)pin->time, pin->id);
493 }
494
495 buf_printf(&out, " t=%" PRIi64 "[%d]", (int64_t)prev_now, (int)(prev_now - tv.tv_sec));
496
497 buf_printf(&out, " r=[%d,%" PRIu64 ",%d,%" PRIu64 ",%d]", (int)(p->last_reap - tv.tv_sec),
499 if (sl != NULL)
500 {
501 buf_printf(&out, " sl=[%d,%d,%d,%zu]", sl->x_head, sl->x_size, sl->x_cap, sl->x_sizeof);
502 }
503
504
505 msg(msglevel, "%s", BSTR(&out));
506 gc_free(&gc);
507}
508
509#endif
510
511uint16_t
513{
514 uint64_t packet_id;
515
516 if (!buf_read(buf, &packet_id, sizeof(packet_id)))
517 {
518 return 0;
519 }
520
521 uint64_t id = ntohll(packet_id);
522 /* top most 16 bits */
523 uint16_t epoch = (uint16_t)(id >> 48);
524
525 pin->id = id & PACKET_ID_MASK;
526 return epoch;
527}
528
529bool
530packet_id_write_epoch(struct packet_id_send *p, uint16_t epoch, struct buffer *buf)
531{
533 {
534 return false;
535 }
536
537 /* Highest 16 bits of packet id is the epoch.
538 *
539 * The lower 48 bits are the per-epoch packet id counter. */
540 uint64_t net_id = ((uint64_t)epoch) << 48 | p->id;
541
542 /* convert to network order. This ensures that the highest bytes
543 * also become the first ones on the wire*/
544 net_id = htonll(net_id);
545
546 return buf_write(buf, &net_id, sizeof(net_id));
547}
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
Definition buffer.h:1268
static bool buf_read(struct buffer *src, void *dest, int size)
Read bytes from the front of a buffer into a caller-supplied destination.
Definition buffer.h:1456
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
#define CIRC_LIST_ALLOC(dest, list_type, size)
Definition circ_list.h:61
#define CIRC_LIST_PUSH(obj, item)
Definition circ_list.h:40
#define CIRC_LIST_ITEM(obj, index)
Definition circ_list.h:53
#define CIRC_LIST_RESET(obj)
Definition circ_list.h:55
#define CIRC_LIST_SIZE(obj)
Definition circ_list.h:47
int64_t get_random(void)
an analogue to the random() function, but use prng_bytes and also int64_t instead of long to avoid LL...
Definition crypto.c:1703
#define D_PID_DEBUG_LOW
Definition errlevel.h:103
#define D_PID_DEBUG_MEDIUM
Definition errlevel.h:104
#define D_PID_DEBUG
Definition errlevel.h:145
#define ntohll(x)
Definition integer.h:36
#define htonll(x)
Definition integer.h:29
#define CLEAR(x)
Definition basic.h:32
static bool check_debug_level(msglvl_t level)
Definition error.h:253
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
const char * time_string(time_t t, tv_usec_t usec, bool show_usec, struct gc_arena *gc)
Definition otime.c:104
time_t now
Definition otime.c:33
static void packet_id_init_recv(struct packet_id_rec *rec, int seq_backtrack, int time_backtrack, const char *name, int unit)
Definition packet_id.c:80
uint16_t packet_id_read_epoch(struct packet_id_net *pin, struct buffer *buf)
Reads the packet ID containing both the epoch and the per-epoch counter from the buf.
Definition packet_id.c:512
void packet_id_init(struct packet_id *p, int seq_backtrack, int time_backtrack, const char *name, int unit)
Definition packet_id.c:96
#define SEQ_UNSEEN
Definition packet_id.c:52
static void packet_id_debug(msglvl_t msglevel, const struct packet_id_rec *p, const struct packet_id_net *pin, const char *message, uint64_t value)
Definition packet_id.c:63
bool packet_id_write_epoch(struct packet_id_send *p, uint16_t epoch, struct buffer *buf)
Writes the packet ID containing both the epoch and the packet id to the buffer specified by buf.
Definition packet_id.c:530
void packet_id_free(struct packet_id *p)
Definition packet_id.c:126
static bool packet_id_send_update_epoch(struct packet_id_send *p)
Definition packet_id.c:367
bool packet_id_test(struct packet_id_rec *p, const struct packet_id_net *pin)
Definition packet_id.c:224
bool packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool long_form)
Definition packet_id.c:319
static bool packet_id_send_update(struct packet_id_send *p, bool long_form)
Definition packet_id.c:344
void packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src)
Move the packet id recv structure from src to dest.
Definition packet_id.c:109
void packet_id_reap(struct packet_id_rec *p)
Definition packet_id.c:193
const char * packet_id_net_print(const struct packet_id_net *pin, bool print_timestamp, struct gc_arena *gc)
Definition packet_id.c:423
void packet_id_add(struct packet_id_rec *p, const struct packet_id_net *pin)
Definition packet_id.c:137
#define SEQ_EXPIRED
Definition packet_id.c:53
static void packet_id_debug_print(msglvl_t msglevel, const struct packet_id_rec *p, const struct packet_id_net *pin, const char *message, packet_id_print_type value)
Definition packet_id.c:441
bool packet_id_write(struct packet_id_send *p, struct buffer *buf, bool long_form, bool prepend)
Write a packet ID to buf, and update the packet ID state.
Definition packet_id.c:382
#define packet_id_format
Definition packet_id.h:76
#define htonpid(x)
Definition packet_id.h:61
uint64_t packet_id_print_type
Definition packet_id.h:77
#define MAX_SEQ_BACKTRACK
Definition packet_id.h:85
uint32_t net_time_t
Definition packet_id.h:51
uint32_t packet_id_type
Definition packet_id.h:45
#define PACKET_ID_MAX
Definition packet_id.h:46
#define ntohtime(x)
Definition packet_id.h:70
#define MIN_SEQ_BACKTRACK
Definition packet_id.h:84
#define MAX_TIME_BACKTRACK
Definition packet_id.h:94
#define ntohpid(x)
Definition packet_id.h:64
#define MIN_TIME_BACKTRACK
Definition packet_id.h:93
#define htontime(x)
Definition packet_id.h:67
#define PACKET_ID_EPOCH_MAX
Definition packet_id.h:47
#define PACKET_ID_MASK
Mask of the bits that contain the 48-bit of the per-epoch packet counter in the packet id.
Definition packet_id.h:50
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Data structure for describing the packet id that is received/send to the network.
Definition packet_id.h:171
uint64_t id
Definition packet_id.h:174
uint64_t max_backtrack_stat
Definition packet_id.h:120
uint64_t seq_backtrack
Definition packet_id.h:118
const char * name
Definition packet_id.h:123
uint64_t id
Definition packet_id.h:117
time_t last_reap
Definition packet_id.h:115
struct seq_list * seq_list
Definition packet_id.h:122
uint64_t id
Definition packet_id.h:133
struct packet_id_rec rec
Definition packet_id.h:181
#define unlikely(x)
Definition syshead.h:35
struct gc_arena gc
Definition test_ssl.c:138