esp-seqno.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include <inttypes.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #define DTLS_EMPTY_BITMAP (0xFFFFFFFFFFFFFFFFULL)
  24. /* Eventually we're going to have to have more than one incoming ESP
  25. context at a time, to allow for the overlap period during a rekey.
  26. So pass the 'esp' even though for now it's redundant. */
  27. int verify_packet_seqno(struct openconnect_info *vpninfo,
  28. struct esp *esp, uint32_t seq)
  29. {
  30. /*
  31. * For incoming, esp->seq is the next *expected* packet, being
  32. * the sequence number *after* the latest we have received.
  33. *
  34. * Since it must always be true that packet esp->seq-1 has been
  35. * received, so there's no need to explicitly record that.
  36. *
  37. * So the backlog bitmap covers the 64 packets prior to that,
  38. * with the LSB representing packet (esp->seq - 2), and the MSB
  39. * representing (esp->seq - 65). A received packet is represented
  40. * by a zero bit, and a missing packet is represented by a one.
  41. *
  42. * Thus we can allow out-of-order reception of packets that are
  43. * within a reasonable interval of the latest packet received.
  44. */
  45. if (seq == esp->seq) {
  46. /* The common case. This is the packet we expected next. */
  47. esp->seq_backlog <<= 1;
  48. /* This might reach a value higher than the 32-bit ESP sequence
  49. * numbers can actually reach. Which is fine. When that
  50. * happens, we'll do the right thing and just not accept any
  51. * newer packets. Someone needs to start a new epoch. */
  52. esp->seq++;
  53. vpn_progress(vpninfo, PRG_TRACE,
  54. _("Accepting expected ESP packet with seq %u\n"),
  55. seq);
  56. return 0;
  57. } else if (seq > esp->seq) {
  58. /* The packet we were expecting has gone missing; this one is newer.
  59. * We always advance the window to accommodate it. */
  60. uint32_t delta = seq - esp->seq;
  61. if (delta >= 64) {
  62. /* We jumped a long way into the future. We have not seen
  63. * any of the previous 32 packets so set the backlog bitmap
  64. * to all ones. */
  65. esp->seq_backlog = DTLS_EMPTY_BITMAP;
  66. } else if (delta == 63) {
  67. /* Avoid undefined behaviour that shifting by 64 would incur.
  68. * The (clear) top bit represents the packet which is currently
  69. * esp->seq - 1, which we know was already received. */
  70. esp->seq_backlog = DTLS_EMPTY_BITMAP >> 1;
  71. } else {
  72. /* We have missed (delta) packets. Shift the backlog by that
  73. * amount *plus* the one we would have shifted it anyway if
  74. * we'd received the packet we were expecting. The zero bit
  75. * representing the packet which is currently esp->seq - 1,
  76. * which we know has been received, ends up at bit position
  77. * (1<<delta). Then we set all the bits lower than that, which
  78. * represent the missing packets. */
  79. esp->seq_backlog <<= delta + 1;
  80. esp->seq_backlog |= (1ULL << delta) - 1;
  81. }
  82. vpn_progress(vpninfo, PRG_TRACE,
  83. _("Accepting later-than-expected ESP packet with seq %u (expected %" PRIu64 ")\n"),
  84. seq, esp->seq);
  85. esp->seq = (uint64_t)seq + 1;
  86. return 0;
  87. } else {
  88. /* This packet is older than the one we were expecting. By how much...? */
  89. uint32_t delta = esp->seq - seq;
  90. /* delta==0 is the overflow case where esp->seq is 0x100000000 and seq is 0 */
  91. if (delta > 65 || delta == 0) {
  92. /* Too old. We can't know if it's a replay. */
  93. if (vpninfo->esp_replay_protect) {
  94. vpn_progress(vpninfo, PRG_DEBUG,
  95. _("Discarding ancient ESP packet with seq %u (expected %" PRIu64 ")\n"),
  96. seq, esp->seq);
  97. return -EINVAL;
  98. } else {
  99. vpn_progress(vpninfo, PRG_DEBUG,
  100. _("Tolerating ancient ESP packet with seq %u (expected %" PRIu64 ")\n"),
  101. seq, esp->seq);
  102. return 0;
  103. }
  104. } else if (delta == 1) {
  105. /* Not in the bitmask since it is by definition already received. */
  106. replayed:
  107. if (vpninfo->esp_replay_protect) {
  108. vpn_progress(vpninfo, PRG_DEBUG,
  109. _("Discarding replayed ESP packet with seq %u\n"),
  110. seq);
  111. return -EINVAL;
  112. } else {
  113. vpn_progress(vpninfo, PRG_DEBUG,
  114. _("Tolerating replayed ESP packet with seq %u\n"),
  115. seq);
  116. return 0;
  117. }
  118. } else {
  119. /* Within the backlog window, so we remember whether we've seen it or not. */
  120. uint64_t mask = 1ULL << (delta - 2);
  121. if (!(esp->seq_backlog & mask))
  122. goto replayed;
  123. esp->seq_backlog &= ~mask;
  124. vpn_progress(vpninfo, PRG_TRACE,
  125. _("Accepting out-of-order ESP packet with seq %u (expected %" PRIu64 ")\n"),
  126. seq, esp->seq);
  127. return 0;
  128. }
  129. }
  130. }