Skip to content

Commit bb7b2a5

Browse files
Move initiator condition into timer fn
1 parent 6b8ab71 commit bb7b2a5

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

boringtun/src/noise/timers.rs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ impl Timers {
9797
}
9898

9999
pub(crate) fn rekey_after_time_on_send(&self) -> Option<Instant> {
100+
if !self.is_initiator {
101+
// If we aren't the initiator of the current session, this timer does not matter.
102+
return None;
103+
}
104+
100105
let session_established = self[TimeSessionEstablished];
101106

102107
if session_established >= self[TimeLastDataPacketSent] {
@@ -108,6 +113,11 @@ impl Timers {
108113
}
109114

110115
pub(crate) fn reject_after_time_on_receive(&self) -> Option<Instant> {
116+
if !self.is_initiator {
117+
// If we aren't the initiator of the current session, this timer does not matter.
118+
return None;
119+
}
120+
111121
let session_established = self[TimeSessionEstablished];
112122

113123
if session_established >= self[TimeLastDataPacketReceived] {
@@ -325,35 +335,33 @@ impl Tunn {
325335
handshake_initiation_required = true;
326336
}
327337
} else {
328-
if self.timers.is_initiator() {
329-
// After sending a packet, if the sender was the original initiator
330-
// of the handshake and if the current session key is REKEY_AFTER_TIME
331-
// ms old, we initiate a new handshake. If the sender was the original
332-
// responder of the handshake, it does not re-initiate a new handshake
333-
// after REKEY_AFTER_TIME ms like the original initiator does.
334-
if self
335-
.timers
336-
.rekey_after_time_on_send()
337-
.is_some_and(|deadline| now >= deadline)
338-
{
339-
tracing::debug!("HANDSHAKE(REKEY_AFTER_TIME (on send))");
340-
handshake_initiation_required = true;
341-
}
338+
// After sending a packet, if the sender was the original initiator
339+
// of the handshake and if the current session key is REKEY_AFTER_TIME
340+
// ms old, we initiate a new handshake. If the sender was the original
341+
// responder of the handshake, it does not re-initiate a new handshake
342+
// after REKEY_AFTER_TIME ms like the original initiator does.
343+
if self
344+
.timers
345+
.rekey_after_time_on_send()
346+
.is_some_and(|deadline| now >= deadline)
347+
{
348+
tracing::debug!("HANDSHAKE(REKEY_AFTER_TIME (on send))");
349+
handshake_initiation_required = true;
350+
}
342351

343-
// After receiving a packet, if the receiver was the original initiator
344-
// of the handshake and if the current session key is REJECT_AFTER_TIME
345-
// - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT ms old, we initiate a new
346-
// handshake.
347-
if self
348-
.timers
349-
.reject_after_time_on_receive()
350-
.is_some_and(|deadline| now >= deadline)
351-
{
352-
tracing::debug!(
353-
"HANDSHAKE(REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT (on receive))"
354-
);
355-
handshake_initiation_required = true;
356-
}
352+
// After receiving a packet, if the receiver was the original initiator
353+
// of the handshake and if the current session key is REJECT_AFTER_TIME
354+
// - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT ms old, we initiate a new
355+
// handshake.
356+
if self
357+
.timers
358+
.reject_after_time_on_receive()
359+
.is_some_and(|deadline| now >= deadline)
360+
{
361+
tracing::debug!(
362+
"HANDSHAKE(REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT (on receive))"
363+
);
364+
handshake_initiation_required = true;
357365
}
358366

359367
// If we have sent a packet to a given peer but have not received a

0 commit comments

Comments
 (0)