From 49d9b81aabce4c7d8491262286a66a55e3698989 Mon Sep 17 00:00:00 2001 From: Matt Dunwoodie Date: Sun, 4 Apr 2021 21:49:30 +1000 Subject: Run all timeouts in process context So the reason timeouts were running in interrupt context was because it was quicker. Running in process context required a `task` to be added, which we ended up doing anyway. So we might as well rely on timeout API to do it for us. --- sys/net/if_wg.c | 52 ++++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/sys/net/if_wg.c b/sys/net/if_wg.c index 0f09d8170bf..d29d98218dc 100644 --- a/sys/net/if_wg.c +++ b/sys/net/if_wg.c @@ -195,9 +195,6 @@ struct wg_peer { struct mutex p_endpoint_mtx; struct wg_endpoint p_endpoint; - struct task p_send_initiation; - struct task p_send_keepalive; - struct task p_clear_secrets; struct task p_deliver_out; struct task p_deliver_in; @@ -299,12 +296,11 @@ void wg_timers_run_zero_key_material(void *); void wg_timers_run_persistent_keepalive(void *); void wg_peer_send_buf(struct wg_peer *, uint8_t *, size_t); -void wg_send_initiation(void *); +void wg_send_initiation(struct wg_peer *); void wg_send_response(struct wg_peer *); void wg_send_cookie(struct wg_softc *, struct cookie_macs *, uint32_t, struct wg_endpoint *e); -void wg_send_keepalive(void *); -void wg_peer_clear_secrets(void *); +void wg_send_keepalive(struct wg_peer *); void wg_handshake(struct wg_softc *, struct wg_packet *); void wg_handshake_worker(void *); @@ -376,9 +372,6 @@ wg_peer_create(struct wg_softc *sc, uint8_t public[WG_KEY_SIZE], mtx_init(&peer->p_endpoint_mtx, IPL_NET); bzero(&peer->p_endpoint, sizeof(peer->p_endpoint)); - task_set(&peer->p_send_initiation, wg_send_initiation, peer); - task_set(&peer->p_send_keepalive, wg_send_keepalive, peer); - task_set(&peer->p_clear_secrets, wg_peer_clear_secrets, peer); task_set(&peer->p_deliver_out, wg_deliver_out, peer); task_set(&peer->p_deliver_in, wg_deliver_in, peer); @@ -825,13 +818,16 @@ wg_timers_init(struct wg_timers *t) rw_init(&t->t_lock, "wg_timers"); mtx_init(&t->t_handshake_mtx, IPL_NET); - timeout_set(&t->t_new_handshake, wg_timers_run_new_handshake, t); - timeout_set(&t->t_send_keepalive, wg_timers_run_send_keepalive, t); - timeout_set(&t->t_retry_handshake, wg_timers_run_retry_handshake, t); - timeout_set(&t->t_persistent_keepalive, - wg_timers_run_persistent_keepalive, t); - timeout_set(&t->t_zero_key_material, - wg_timers_run_zero_key_material, t); + timeout_set_flags(&t->t_new_handshake, + wg_timers_run_new_handshake, t, TIMEOUT_PROC); + timeout_set_flags(&t->t_send_keepalive, + wg_timers_run_send_keepalive, t, TIMEOUT_PROC); + timeout_set_flags(&t->t_retry_handshake, + wg_timers_run_retry_handshake, t, TIMEOUT_PROC); + timeout_set_flags(&t->t_persistent_keepalive, + wg_timers_run_persistent_keepalive, t, TIMEOUT_PROC); + timeout_set_flags(&t->t_zero_key_material, + wg_timers_run_zero_key_material, t, TIMEOUT_PROC); } void @@ -986,7 +982,7 @@ wg_timers_run_send_initiation(void *_t, int is_retry) if (!is_retry) t->t_handshake_retries = 0; if (noise_remote_initiation_expired(peer->p_remote) == ETIMEDOUT) - task_add(wg_handshake_taskq, &peer->p_send_initiation); + wg_send_initiation(peer); } void @@ -1026,7 +1022,7 @@ wg_timers_run_send_keepalive(void *_t) struct wg_timers *t = _t; struct wg_peer *peer = CONTAINER_OF(t, struct wg_peer, p_timers); - task_add(wg_crypt_taskq, &peer->p_send_keepalive); + wg_send_keepalive(peer); if (t->t_need_another_keepalive) { t->t_need_another_keepalive = 0; timeout_add_sec(&t->t_send_keepalive, KEEPALIVE_TIMEOUT); @@ -1042,8 +1038,8 @@ wg_timers_run_new_handshake(void *_t) DPRINTF(peer->p_sc, "Retrying handshake with peer %llu because we " "stopped hearing back after %d seconds\n", peer->p_id, NEW_HANDSHAKE_TIMEOUT); - wg_peer_clear_src(peer); + wg_peer_clear_src(peer); wg_timers_run_send_initiation(t, 0); } @@ -1056,7 +1052,7 @@ wg_timers_run_zero_key_material(void *_t) DPRINTF(peer->p_sc, "Zeroing out keys for peer %llu, since we " "haven't received a new one in %d seconds\n", peer->p_id, REJECT_AFTER_TIME * 3); - task_add(wg_handshake_taskq, &peer->p_clear_secrets); + noise_remote_keypairs_clear(peer->p_remote); } void @@ -1064,8 +1060,9 @@ wg_timers_run_persistent_keepalive(void *_t) { struct wg_timers *t = _t; struct wg_peer *peer = CONTAINER_OF(t, struct wg_peer, p_timers); + if (t->t_persistent_keepalive_interval > 0) - task_add(wg_crypt_taskq, &peer->p_send_keepalive); + wg_send_keepalive(peer); } /* The following functions handle handshakes */ @@ -1082,9 +1079,8 @@ wg_peer_send_buf(struct wg_peer *peer, uint8_t *buf, size_t len) } void -wg_send_initiation(void *_peer) +wg_send_initiation(struct wg_peer *peer) { - struct wg_peer *peer = _peer; struct wg_pkt_initiation pkt; if (noise_create_initiation(peer->p_remote, &pkt.s_idx, pkt.ue, pkt.es, @@ -1138,9 +1134,8 @@ wg_send_cookie(struct wg_softc *sc, struct cookie_macs *cm, uint32_t idx, } void -wg_send_keepalive(void *_peer) +wg_send_keepalive(struct wg_peer *peer) { - struct wg_peer *peer = _peer; struct wg_softc *sc = peer->p_sc; struct wg_packet *pkt; struct mbuf *m; @@ -1169,13 +1164,6 @@ send: wg_queue_out(peer->p_sc, peer); } -void -wg_peer_clear_secrets(void *_peer) -{ - struct wg_peer *peer = _peer; - noise_remote_keypairs_clear(peer->p_remote); -} - void wg_handshake(struct wg_softc *sc, struct wg_packet *pkt) { -- cgit v1.2.3-59-g8ed1b