Repository navigation
Commit 031caf4
committed
Implement DTLSv1_get_timeout and DTLSv1_handle_timeout
wolfSSL_DTLSv1_get_timeout always returned 0 and reported the full
timeout instead of the time left, and wolfSSL_DTLSv1_handle_timeout was
a stub that did nothing. OpenSSL style event loops that wait for
get_timeout and then call handle_timeout never retransmitted a lost
flight.
Track two timers on the object. The retransmission timer restarts
whenever a handshake message is stored for retransmission (each DTLS 1.2
pool save and DTLS 1.3 retransmit record), whenever the flight is
resent, either on a timeout or because the peer retransmitted or acked
part of it, including a resend that hit WANT_WRITE, on every timeout
backoff, and when a valid handshake message, or in DTLS 1.3 an ACK that
leaves nothing to retransmit, resets a backed off timeout to its initial
value. Restarting on every stored message rather than only on an empty
list keeps a renegotiation or post-handshake flight from inheriting the
start time of an older flight that is still kept. A DTLS 1.2 flight
counts as sent only in the states DtlsMsgPoolSend accepts for a
retransmission, so no timer runs while a flight is still being written
or waits for an asynchronous operation. A DTLS 1.3 flight counts as
sent while records are kept for it. get_timeout and handle_timeout
report no retransmission timer while the handshake is suspended on
asynchronous crypto, non-blocking OCSP or a chain verify callback, so a
slow signature cannot trigger a retransmission of a partly sent flight.
The DTLS 1.3 ack timer runs for
a quarter of the timeout while received records wait for an ack that can
be sent. It starts when the first of those records is seen and whenever
the quick timeout flag is set again.
DtlsTimersRunning() reports which timer runs and replaces the inline
check in EmbedReceiveFrom, which keeps its behaviour. The timers use
TimeNowInMilliseconds, which is now also built for DTLS, so DTLS 1.2
builds without session tickets, PSK or TLS 1.3 no longer count whole
seconds. Whole seconds stay the fallback where TLS 1.3 tickets, PSK and
DTLS 1.3 do not require a millisecond clock. With USER_TICKS, or on a
platform that would land in the gettimeofday() default without
HAVE_GETTIMEOFDAY, the library defines no TimeNowInMilliseconds and the
timers count LowResTimer seconds instead. Ports with only a seconds
clock, such as Xilinx or Nucleus, keep building, and a
USER_TICKS application that supplies its own TimeNowInMilliseconds still
links. The DEOS clock now reads the tick counter through a const
volatile pointer like the DEOS LowResTimer, since DTLS 1.2 builds now
compile it. The Zephyr clock skips k_cpu_idle() on native_posix and
native_sim like the Zephyr LowResTimer, since idling there advances
simulated time and the DTLS timers now read this clock. CMake now
exports HAVE_GETTIMEOFDAY to config.h as autotools does. The ladder
marks its whole second branches and DtlsTimerLowRes reports that to the
timers, so internal.h needs no second copy of the ladder. With a whole
second clock the timers round elapsed time down by one tick so a timer
never expires early. The ack timer is not rounded down, since an early
ACK only makes the peer resend the records it is missing. A timeout of 0
means no timeout, so no timer runs. get_timeout reports at most
DTLS_TIMEOUT_MAX seconds, which also keeps the millisecond count from
overflowing. DtlsTimerNowMs and DtlsTimerLowRes are visible to the tests
and get WOLFSSL_API_PREFIX_MAP aliases.
Fix the DTLS 1.3 quick timeout. Dtls13RtxTimeout cleared the flag when
it sent the ACKs and then checked it, so a timeout taken after the
quarter wait also retransmitted the flight and doubled the timeout.
Dtls13RtxTimeout now takes the timer that expired. handle_timeout passes
the timer it found expired. The receive path and
wolfSSL_dtls_got_timeout do not know how long the caller waited, so with
the quick timeout flag set they send only the ACKs while the buffered
flight has waited less than half its timeout, and retransmit with
backoff once it waited longer. Without a clock a quick timeout only
sends the ACKs and the next full timeout retransmits. A quick wait stays
under half the timeout and a full one reaches it. A whole second clock
is rounded down as for the timers, so a full wait can read as a quick
one and retransmit one timeout later. With nothing buffered to
retransmit, a timeout with the flag set only sends the ACKs instead of
backing off. Flushing the seen records also clears the flag, so a flag
left over from the previous flight no longer turns the next full timeout
into an ACK-only one. A timeout within DTLS13_MIN_RTX_INTERVAL of the
last retransmission no longer doubles the timeout when
Dtls13RtxSendBuffered holds the retransmission back.
Dtls13RtxSendBuffered now uses Dtls13RtxWaitMs for that check, and a
clock that stepped back counts as elapsed, so it no longer holds the
retransmission back until the clock catches up. test_dtls_timeout clears
the last retransmission time before its second timeout, which falls
within that interval of the first one. test_dtls_drop_client_ack takes
one timeout instead of two, since the first one no longer finds a flag
left over from the handshake.
get_timeout now returns 1 with the time left while a timer runs and 0
otherwise, and no longer fills in dtls_timeout when no timer runs, so
callers must check the return value. The time left includes any wait
before DTLS 1.3 may retransmit again. No timer runs while the object's
own output is still pending, so handle_timeout cannot queue a second
copy of a blocked write. handle_timeout returns 0 when no timer expired.
For an expired retransmission timer it sends any pending ACKs and
retransmits with backoff, and returns 1. For an expired DTLS 1.3 ack
timer it only sends the ACKs, without retransmitting or backing off, and
returns 1. It returns -1 on error, including when the timeout reached
its maximum, the ACK hits WANT_WRITE or the timers cannot be read under
WOLFSSL_RW_THREADED, which also sets the error, while get_timeout
returns 0. At the maximum timeout DTLS 1.3 now sets SOCKET_ERROR_E, as
DTLS 1.2 does, instead of -1. Both return 0 when NO_ASN_TIME leaves no
clock.
Under WOLFSSL_RW_THREADED the DTLS 1.3 ack timer state now sits behind
the dtls13Rtx mutex. The read thread sets the ack timer start and the
quick timeout flag with it held, the timeout handler reads and clears
the flag and the pending ACK state with it held, and get_timeout reads
the start in the same locked section as the flag, so it never pairs a
set flag with a stale start.1 parent e59e855 commit 031caf4
8 files changed
Lines changed: 1315 additions & 103 deletions
File tree
- cmake
- src
- tests/api
- wolfssl
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
782 | 782 | | |
783 | 783 | | |
784 | 784 | | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
785 | 789 | | |
786 | 790 | | |
787 | 791 | | |
| |||
827 | 831 | | |
828 | 832 | | |
829 | 833 | | |
| 834 | + | |
830 | 835 | | |
831 | 836 | | |
832 | 837 | | |
| |||
1039 | 1044 | | |
1040 | 1045 | | |
1041 | 1046 | | |
1042 | | - | |
| 1047 | + | |
1043 | 1048 | | |
| 1049 | + | |
| 1050 | + | |
1044 | 1051 | | |
1045 | 1052 | | |
1046 | 1053 | | |
| |||
1628 | 1635 | | |
1629 | 1636 | | |
1630 | 1637 | | |
1631 | | - | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
1632 | 1647 | | |
| 1648 | + | |
| 1649 | + | |
| 1650 | + | |
| 1651 | + | |
1633 | 1652 | | |
1634 | 1653 | | |
1635 | 1654 | | |
| |||
1655 | 1674 | | |
1656 | 1675 | | |
1657 | 1676 | | |
1658 | | - | |
1659 | | - | |
1660 | | - | |
1661 | | - | |
1662 | | - | |
1663 | | - | |
1664 | | - | |
1665 | 1677 | | |
1666 | 1678 | | |
1667 | 1679 | | |
1668 | 1680 | | |
1669 | 1681 | | |
1670 | | - | |
1671 | | - | |
| 1682 | + | |
1672 | 1683 | | |
1673 | 1684 | | |
1674 | 1685 | | |
1675 | 1686 | | |
1676 | 1687 | | |
1677 | | - | |
| 1688 | + | |
1678 | 1689 | | |
1679 | 1690 | | |
1680 | 1691 | | |
| |||
1731 | 1742 | | |
1732 | 1743 | | |
1733 | 1744 | | |
| 1745 | + | |
1734 | 1746 | | |
1735 | 1747 | | |
1736 | 1748 | | |
1737 | 1749 | | |
1738 | 1750 | | |
1739 | 1751 | | |
1740 | 1752 | | |
| 1753 | + | |
1741 | 1754 | | |
1742 | 1755 | | |
1743 | 1756 | | |
| |||
3418 | 3431 | | |
3419 | 3432 | | |
3420 | 3433 | | |
| 3434 | + | |
3421 | 3435 | | |
3422 | 3436 | | |
3423 | 3437 | | |
| |||
3456 | 3470 | | |
3457 | 3471 | | |
3458 | 3472 | | |
3459 | | - | |
3460 | | - | |
3461 | | - | |
| 3473 | + | |
| 3474 | + | |
| 3475 | + | |
| 3476 | + | |
| 3477 | + | |
| 3478 | + | |
| 3479 | + | |
| 3480 | + | |
| 3481 | + | |
| 3482 | + | |
| 3483 | + | |
| 3484 | + | |
| 3485 | + | |
| 3486 | + | |
| 3487 | + | |
| 3488 | + | |
| 3489 | + | |
| 3490 | + | |
| 3491 | + | |
| 3492 | + | |
| 3493 | + | |
| 3494 | + | |
| 3495 | + | |
| 3496 | + | |
| 3497 | + | |
| 3498 | + | |
| 3499 | + | |
| 3500 | + | |
| 3501 | + | |
| 3502 | + | |
| 3503 | + | |
| 3504 | + | |
| 3505 | + | |
| 3506 | + | |
| 3507 | + | |
| 3508 | + | |
| 3509 | + | |
| 3510 | + | |
| 3511 | + | |
| 3512 | + | |
| 3513 | + | |
| 3514 | + | |
| 3515 | + | |
| 3516 | + | |
| 3517 | + | |
| 3518 | + | |
3462 | 3519 | | |
3463 | 3520 | | |
| 3521 | + | |
| 3522 | + | |
| 3523 | + | |
3464 | 3524 | | |
3465 | | - | |
3466 | | - | |
3467 | | - | |
3468 | | - | |
3469 | | - | |
3470 | | - | |
| 3525 | + | |
| 3526 | + | |
| 3527 | + | |
| 3528 | + | |
| 3529 | + | |
| 3530 | + | |
| 3531 | + | |
| 3532 | + | |
3471 | 3533 | | |
3472 | | - | |
3473 | | - | |
| 3534 | + | |
| 3535 | + | |
| 3536 | + | |
| 3537 | + | |
| 3538 | + | |
| 3539 | + | |
| 3540 | + | |
| 3541 | + | |
3474 | 3542 | | |
3475 | 3543 | | |
3476 | 3544 | | |
3477 | 3545 | | |
3478 | 3546 | | |
3479 | | - | |
3480 | | - | |
3481 | | - | |
3482 | | - | |
3483 | | - | |
3484 | | - | |
3485 | | - | |
| 3547 | + | |
| 3548 | + | |
| 3549 | + | |
| 3550 | + | |
3486 | 3551 | | |
3487 | | - | |
3488 | 3552 | | |
3489 | 3553 | | |
3490 | 3554 | | |
3491 | | - | |
| 3555 | + | |
3492 | 3556 | | |
3493 | 3557 | | |
3494 | 3558 | | |
| |||
3623 | 3687 | | |
3624 | 3688 | | |
3625 | 3689 | | |
| 3690 | + | |
3626 | 3691 | | |
3627 | 3692 | | |
3628 | 3693 | | |
| |||
3646 | 3711 | | |
3647 | 3712 | | |
3648 | 3713 | | |
| 3714 | + | |
3649 | 3715 | | |
3650 | 3716 | | |
3651 | 3717 | | |
| |||
3670 | 3736 | | |
3671 | 3737 | | |
3672 | 3738 | | |
| 3739 | + | |
| 3740 | + | |
| 3741 | + | |
3673 | 3742 | | |
3674 | 3743 | | |
3675 | 3744 | | |
| |||
0 commit comments