diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-09-28 15:49:31 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-10-02 02:45:53 +0200 |
commit | 2f3c1fcb41fe878694e6cd32708a4e2032b93fc4 (patch) | |
tree | 01f5d6710aed3460eca4874d6e0317309c409cfd /src | |
parent | dcb9387fd61851ef4cb38bb2adb664815514c44f (diff) |
timers: ensure safe timer removal
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/timers.c | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/timers.c b/src/timers.c index ef30005..42e188c 100644 --- a/src/timers.c +++ b/src/timers.c @@ -25,13 +25,18 @@ static inline unsigned long slack_time(unsigned long time) if (unlikely(!peer)) \ return; +static inline bool timers_active(struct wireguard_peer *peer) +{ + return netif_running(peer->device->dev) && !list_empty(&peer->peer_list); +} + static void expired_retransmit_handshake(unsigned long ptr) { peer_get_from_ptr(ptr); if (peer->timer_handshake_attempts > MAX_TIMER_HANDSHAKES) { pr_debug("%s: Handshake for peer %Lu (%pISpfsc) did not complete after %d attempts, giving up\n", peer->device->dev->name, peer->internal_id, &peer->endpoint.addr, MAX_TIMER_HANDSHAKES + 2); - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) del_timer(&peer->timer_send_keepalive); /* We drop all packets without a keypair and don't try again, * if we try unsuccessfully for too long to make a handshake. */ @@ -39,7 +44,7 @@ static void expired_retransmit_handshake(unsigned long ptr) /* We set a timer for destroying any residue that might be left * of a partial exchange. */ - if (likely(netif_running(peer->device->dev)) && !timer_pending(&peer->timer_zero_key_material)) + if (likely(timers_active(peer)) && !timer_pending(&peer->timer_zero_key_material)) mod_timer(&peer->timer_zero_key_material, jiffies + (REJECT_AFTER_TIME * 3)); } else { ++peer->timer_handshake_attempts; @@ -59,7 +64,7 @@ static void expired_send_keepalive(unsigned long ptr) packet_send_keepalive(peer); if (peer->timer_need_another_keepalive) { peer->timer_need_another_keepalive = false; - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) mod_timer(&peer->timer_send_keepalive, jiffies + KEEPALIVE_TIMEOUT); } peer_put(peer); @@ -94,7 +99,7 @@ static void expired_send_persistent_keepalive(unsigned long ptr) { peer_get_from_ptr(ptr); if (likely(peer->persistent_keepalive_interval)) { - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) del_timer(&peer->timer_send_keepalive); packet_send_keepalive(peer); } @@ -104,17 +109,17 @@ static void expired_send_persistent_keepalive(unsigned long ptr) /* Should be called after an authenticated data packet is sent. */ void timers_data_sent(struct wireguard_peer *peer) { - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) del_timer(&peer->timer_send_keepalive); - if (likely(netif_running(peer->device->dev)) && !timer_pending(&peer->timer_new_handshake)) + if (likely(timers_active(peer)) && !timer_pending(&peer->timer_new_handshake)) mod_timer(&peer->timer_new_handshake, jiffies + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT); } /* Should be called after an authenticated data packet is received. */ void timers_data_received(struct wireguard_peer *peer) { - if (likely(netif_running(peer->device->dev)) && !timer_pending(&peer->timer_send_keepalive)) + if (likely(timers_active(peer)) && !timer_pending(&peer->timer_send_keepalive)) mod_timer(&peer->timer_send_keepalive, jiffies + KEEPALIVE_TIMEOUT); else peer->timer_need_another_keepalive = true; @@ -123,14 +128,14 @@ void timers_data_received(struct wireguard_peer *peer) /* Should be called after any type of authenticated packet is received -- keepalive or data. */ void timers_any_authenticated_packet_received(struct wireguard_peer *peer) { - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) del_timer(&peer->timer_new_handshake); } /* Should be called after a handshake initiation message is sent. */ void timers_handshake_initiated(struct wireguard_peer *peer) { - if (likely(netif_running(peer->device->dev))) { + if (likely(timers_active(peer))) { del_timer(&peer->timer_send_keepalive); mod_timer(&peer->timer_retransmit_handshake, slack_time(jiffies + REKEY_TIMEOUT + prandom_u32_max(REKEY_TIMEOUT_JITTER_MAX))); } @@ -139,7 +144,7 @@ void timers_handshake_initiated(struct wireguard_peer *peer) /* Should be called after a handshake response message is received and processed or when getting key confirmation via the first data message. */ void timers_handshake_complete(struct wireguard_peer *peer) { - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) del_timer(&peer->timer_retransmit_handshake); peer->timer_handshake_attempts = 0; peer->sent_lastminute_handshake = false; @@ -149,14 +154,14 @@ void timers_handshake_complete(struct wireguard_peer *peer) /* Should be called after an ephemeral key is created, which is before sending a handshake response or after receiving a handshake response. */ void timers_session_derived(struct wireguard_peer *peer) { - if (likely(netif_running(peer->device->dev))) + if (likely(timers_active(peer))) mod_timer(&peer->timer_zero_key_material, jiffies + (REJECT_AFTER_TIME * 3)); } /* Should be called before a packet with authentication -- data, keepalive, either handshake -- is sent, or after one is received. */ void timers_any_authenticated_packet_traversal(struct wireguard_peer *peer) { - if (peer->persistent_keepalive_interval && likely(netif_running(peer->device->dev))) + if (peer->persistent_keepalive_interval && likely(timers_active(peer))) mod_timer(&peer->timer_persistent_keepalive, slack_time(jiffies + peer->persistent_keepalive_interval)); } |