diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-04-20 11:14:32 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-04-20 11:16:27 -0600 |
commit | e9fd156c23c276176085528af9ff4181da329b6a (patch) | |
tree | fa3e85cd085c80514eb3e6ddae8d83a66342865b | |
parent | wg_noise: ensure we check peer count on hashtable insert (diff) | |
download | wireguard-freebsd-e9fd156c23c276176085528af9ff4181da329b6a.tar.xz wireguard-freebsd-e9fd156c23c276176085528af9ff4181da329b6a.zip |
global: use proper boolean types
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | src/if_wg.c | 39 | ||||
-rw-r--r-- | src/wg_cookie.c | 6 | ||||
-rw-r--r-- | src/wg_cookie.h | 2 | ||||
-rw-r--r-- | src/wg_noise.c | 48 |
4 files changed, 49 insertions, 46 deletions
diff --git a/src/if_wg.c b/src/if_wg.c index 5b5e200..0714082 100644 --- a/src/if_wg.c +++ b/src/if_wg.c @@ -198,8 +198,8 @@ struct wg_peer { struct wg_queue p_encrypt_serial; struct wg_queue p_decrypt_serial; - int p_enabled; - int p_need_another_keepalive; + bool p_enabled; + bool p_need_another_keepalive; uint16_t p_persistent_keepalive_interval; struct callout p_new_handshake; struct callout p_send_keepalive; @@ -410,8 +410,8 @@ wg_peer_alloc(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE]) wg_queue_init(&peer->p_encrypt_serial, "txq"); wg_queue_init(&peer->p_decrypt_serial, "rxq"); - peer->p_enabled = 0; - peer->p_need_another_keepalive = 0; + peer->p_enabled = false; + peer->p_need_another_keepalive = false; peer->p_persistent_keepalive_interval = 0; callout_init(&peer->p_new_handshake, true); @@ -519,7 +519,8 @@ wg_aip_add(struct wg_softc *sc, struct wg_peer *peer, sa_family_t af, const void struct wg_aip *aip; struct sockaddr_in *sin_addr, *sin_mask; struct sockaddr_in6 *sin6_addr, *sin6_mask; - int i, need_free = 0, ret = 0; + bool need_free = false; + int i, ret = 0; if ((aip = malloc(sizeof(*aip), M_WG, M_NOWAIT | M_ZERO)) == NULL) return (ENOBUFS); @@ -572,7 +573,7 @@ wg_aip_add(struct wg_softc *sc, struct wg_peer *peer, sa_family_t af, const void LIST_INSERT_HEAD(&peer->p_aips, aip, a_entry); peer->p_aips_num++; } else { - need_free = 1; + need_free = true; aip = (struct wg_aip *) node; if (aip->a_peer != peer) { LIST_REMOVE(aip, a_entry); @@ -930,16 +931,16 @@ retry: static void wg_timers_enable(struct wg_peer *peer) { - WRITE_ONCE(peer->p_enabled, 1); + WRITE_ONCE(peer->p_enabled, true); wg_timers_run_persistent_keepalive(peer); } static void wg_timers_disable(struct wg_peer *peer) { - WRITE_ONCE(peer->p_enabled, 0); + WRITE_ONCE(peer->p_enabled, false); NET_EPOCH_WAIT(); - WRITE_ONCE(peer->p_need_another_keepalive, 0); + WRITE_ONCE(peer->p_need_another_keepalive, false); callout_stop(&peer->p_new_handshake); callout_stop(&peer->p_send_keepalive); @@ -994,7 +995,7 @@ wg_timers_event_data_received(struct wg_peer *peer) MSEC_2_TICKS(KEEPALIVE_TIMEOUT * 1000), wg_timers_run_send_keepalive, peer); else - WRITE_ONCE(peer->p_need_another_keepalive, 1); + WRITE_ONCE(peer->p_need_another_keepalive, true); } NET_EPOCH_EXIT(et); } @@ -1123,7 +1124,7 @@ wg_timers_run_send_keepalive(void *_peer) wg_send_keepalive(peer); if (READ_ONCE(peer->p_need_another_keepalive)) { - WRITE_ONCE(peer->p_need_another_keepalive, 0); + WRITE_ONCE(peer->p_need_another_keepalive, false); callout_reset(&peer->p_send_keepalive, MSEC_2_TICKS(KEEPALIVE_TIMEOUT * 1000), wg_timers_run_send_keepalive, peer); @@ -1266,16 +1267,17 @@ wg_handshake(struct wg_softc *sc, struct wg_packet *pkt) struct wg_peer *peer; struct mbuf *m; struct noise_remote *remote = NULL; - int res, underload = 0; + int res; + bool underload = false; static struct timeval wg_last_underload; /* microuptime */ static const struct timeval underload_interval = { UNDERLOAD_TIMEOUT, 0 }; if (wg_queue_len(&sc->sc_handshake_queue) >= MAX_QUEUED_HANDSHAKES/8) { getmicrouptime(&wg_last_underload); - underload = 1; + underload = true; } else if (wg_last_underload.tv_sec != 0) { if (!ratecheck(&wg_last_underload, &underload_interval)) - underload = 1; + underload = true; else bzero(&wg_last_underload, sizeof(wg_last_underload)); } @@ -1574,7 +1576,8 @@ wg_deliver_out(struct wg_peer *peer) struct wg_softc *sc = peer->p_sc; struct wg_packet *pkt; struct mbuf *m; - int rc, len, data_sent = 0; + int rc, len; + bool data_sent = false; wg_peer_get_endpoint(peer, &endpoint); @@ -1590,7 +1593,7 @@ wg_deliver_out(struct wg_peer *peer) wg_timers_event_any_authenticated_packet_traversal(peer); wg_timers_event_any_authenticated_packet_sent(peer); if (len > (sizeof(struct wg_pkt_data)+NOISE_AUTHTAG_LEN)) - data_sent = 1; + data_sent = true; counter_u64_add(peer->p_tx_bytes, len); } else if (rc == EADDRNOTAVAIL) { wg_peer_clear_src(peer); @@ -1620,7 +1623,7 @@ wg_deliver_in(struct wg_peer *peer) struct wg_packet *pkt; struct mbuf *m; uint32_t af; - int data_recv = 0; + bool data_recv = false; while ((pkt = wg_queue_dequeue_serial(&peer->p_decrypt_serial)) != NULL) { if (pkt->p_state == WG_PACKET_CRYPTED) { @@ -1646,7 +1649,7 @@ wg_deliver_in(struct wg_peer *peer) MPASS(pkt->p_af == AF_INET || pkt->p_af == AF_INET6); pkt->p_mbuf = NULL; - data_recv = 1; + data_recv = true; m->m_flags &= ~(M_MCAST | M_BCAST); m->m_pkthdr.rcvif = ifp; diff --git a/src/wg_cookie.c b/src/wg_cookie.c index ab35ad4..26673c1 100644 --- a/src/wg_cookie.c +++ b/src/wg_cookie.c @@ -110,7 +110,7 @@ cookie_maker_consume_payload(struct cookie_maker *cp, rw_enter_write(&cp->cp_lock); - if (cp->cp_mac1_valid == 0) { + if (!cp->cp_mac1_valid) { ret = ETIMEDOUT; goto error; } @@ -123,7 +123,7 @@ cookie_maker_consume_payload(struct cookie_maker *cp, memcpy(cp->cp_cookie, cookie, COOKIE_COOKIE_SIZE); getnanouptime(&cp->cp_birthdate); - cp->cp_mac1_valid = 0; + cp->cp_mac1_valid = false; error: rw_exit_write(&cp->cp_lock); @@ -139,7 +139,7 @@ cookie_maker_mac(struct cookie_maker *cp, struct cookie_macs *cm, void *buf, cookie_macs_mac1(cm, buf, len, cp->cp_mac1_key); memcpy(cp->cp_mac1_last, cm->mac1, COOKIE_MAC_SIZE); - cp->cp_mac1_valid = 1; + cp->cp_mac1_valid = true; if (!cookie_timer_expired(&cp->cp_birthdate, COOKIE_SECRET_MAX_AGE - COOKIE_SECRET_LATENCY, 0)) diff --git a/src/wg_cookie.h b/src/wg_cookie.h index c7338d8..d24223f 100644 --- a/src/wg_cookie.h +++ b/src/wg_cookie.h @@ -77,7 +77,7 @@ struct cookie_maker { struct rwlock cp_lock; uint8_t cp_cookie[COOKIE_COOKIE_SIZE]; struct timespec cp_birthdate; /* nanouptime */ - int cp_mac1_valid; + bool cp_mac1_valid; uint8_t cp_mac1_last[COOKIE_MAC_SIZE]; }; diff --git a/src/wg_noise.c b/src/wg_noise.c index c11315f..42c77c8 100644 --- a/src/wg_noise.c +++ b/src/wg_noise.c @@ -54,8 +54,8 @@ struct noise_index { struct noise_keypair { struct noise_index kp_index; u_int kp_refcnt; - int kp_can_send; - int kp_is_initiator; + bool kp_can_send; + bool kp_is_initiator; sbintime_t kp_birthdate; /* sbinuptime */ struct noise_remote *kp_remote; @@ -81,13 +81,13 @@ struct noise_remote { struct noise_index r_index; CK_LIST_ENTRY(noise_remote) r_entry; - int r_entry_inserted; + bool r_entry_inserted; uint8_t r_public[NOISE_PUBLIC_KEY_LEN]; struct rwlock r_handshake_lock; struct noise_handshake r_handshake; - int r_handshake_alive; - int r_handshake_initiator; + bool r_handshake_alive; + bool r_handshake_initiator; sbintime_t r_last_sent; /* sbinuptime */ sbintime_t r_last_init_recv; /* sbinuptime */ uint8_t r_timestamp[NOISE_TIMESTAMP_LEN]; @@ -107,7 +107,7 @@ struct noise_remote { struct noise_local { struct rwlock l_identity_lock; - int l_has_identity; + bool l_has_identity; uint8_t l_public[NOISE_PUBLIC_KEY_LEN]; uint8_t l_private[NOISE_PUBLIC_KEY_LEN]; @@ -128,7 +128,7 @@ static void noise_precompute_ss(struct noise_local *, struct noise_remote *); static void noise_remote_index_insert(struct noise_local *, struct noise_remote *); static struct noise_remote * - noise_remote_index_lookup(struct noise_local *, uint32_t, int); + noise_remote_index_lookup(struct noise_local *, uint32_t, bool); static int noise_remote_index_remove(struct noise_local *, struct noise_remote *); static void noise_remote_expire_current(struct noise_remote *); @@ -176,7 +176,7 @@ noise_local_alloc(void *arg) return (NULL); rw_init(&l->l_identity_lock, "noise_identity"); - l->l_has_identity = 0; + l->l_has_identity = false; bzero(l->l_public, NOISE_PUBLIC_KEY_LEN); bzero(l->l_private, NOISE_PUBLIC_KEY_LEN); @@ -287,15 +287,15 @@ noise_remote_alloc(struct noise_local *l, void *arg, if ((r = malloc(sizeof(*r), M_NOISE, M_NOWAIT)) == NULL) return (NULL); - r->r_index.i_is_keypair = 0; - r->r_entry_inserted = 0; + r->r_index.i_is_keypair = false; + r->r_entry_inserted = false; memcpy(r->r_public, public, NOISE_PUBLIC_KEY_LEN); rw_init(&r->r_handshake_lock, "noise_handshake"); bzero(&r->r_handshake, sizeof(r->r_handshake)); - r->r_handshake_alive = 0; - r->r_handshake_initiator = 0; + r->r_handshake_alive = false; + r->r_handshake_initiator = false; r->r_last_sent = TIMER_RESET; r->r_last_init_recv = TIMER_RESET; bzero(r->r_timestamp, NOISE_TIMESTAMP_LEN); @@ -325,9 +325,9 @@ noise_remote_enable(struct noise_remote *r) idx = siphash24(&l->l_hash_key, r->r_public, NOISE_PUBLIC_KEY_LEN) & HT_REMOTE_MASK; rw_wlock(&l->l_remote_lock); - if (!r->r_entry_inserted) + if (!r->r_entry_inserted) { if (l->l_remote_num < MAX_REMOTE_PER_LOCAL) { - r->r_entry_inserted = 1; + r->r_entry_inserted = true; l->l_remote_num++; CK_LIST_INSERT_HEAD(&l->l_remote_hash[idx], r, r_entry); } else { @@ -346,7 +346,7 @@ noise_remote_disable(struct noise_remote *r) /* remove from hashtable */ rw_wlock(&l->l_remote_lock); if (r->r_entry_inserted) { - r->r_entry_inserted = 0; + r->r_entry_inserted = false; CK_LIST_REMOVE(r, r_entry); l->l_remote_num--; }; @@ -403,11 +403,11 @@ assign_id: NET_EPOCH_EXIT(et); - r->r_handshake_alive = 1; + r->r_handshake_alive = true; } static struct noise_remote * -noise_remote_index_lookup(struct noise_local *l, uint32_t idx0, int lookup_keypair) +noise_remote_index_lookup(struct noise_local *l, uint32_t idx0, bool lookup_keypair) { struct epoch_tracker et; struct noise_index *i; @@ -437,7 +437,7 @@ noise_remote_index_lookup(struct noise_local *l, uint32_t idx0, int lookup_keypa struct noise_remote * noise_remote_index(struct noise_local *l, uint32_t idx) { - return noise_remote_index_lookup(l, idx, 1); + return noise_remote_index_lookup(l, idx, true); } static int @@ -448,7 +448,7 @@ noise_remote_index_remove(struct noise_local *l, struct noise_remote *r) rw_wlock(&l->l_index_lock); CK_LIST_REMOVE(&r->r_index, i_entry); rw_wunlock(&l->l_index_lock); - r->r_handshake_alive = 0; + r->r_handshake_alive = false; return (1); } return (0); @@ -627,7 +627,7 @@ noise_add_new_keypair(struct noise_local *l, struct noise_remote *r, /* Insert into index table */ rw_assert_wrlock(&r->r_handshake_lock); - kp->kp_index.i_is_keypair = 1; + kp->kp_index.i_is_keypair = true; kp->kp_index.i_local_index = r_i->i_local_index; kp->kp_index.i_remote_index = r_i->i_remote_index; @@ -650,7 +650,7 @@ noise_begin_session(struct noise_remote *r) return (ENOSPC); refcount_init(&kp->kp_refcnt, 1); - kp->kp_can_send = 1; + kp->kp_can_send = true; kp->kp_is_initiator = r->r_handshake_initiator; kp->kp_birthdate = getsbinuptime(); kp->kp_remote = noise_remote_ref(r); @@ -958,7 +958,7 @@ noise_create_initiation(struct noise_remote *r, noise_remote_index_insert(l, r); r->r_last_sent = getsbinuptime(); *s_idx = r->r_index.i_local_index; - r->r_handshake_initiator = 1; + r->r_handshake_initiator = true; ret = 0; error: rw_wunlock(&r->r_handshake_lock); @@ -1031,7 +1031,7 @@ noise_consume_initiation(struct noise_local *l, struct noise_remote **rp, /* Ok, we're happy to accept this initiation now */ noise_remote_index_insert(l, r); r->r_index.i_remote_index = s_idx; - r->r_handshake_initiator = 0; + r->r_handshake_initiator = false; r->r_handshake = hs; *rp = noise_remote_ref(r); ret = 0; @@ -1109,7 +1109,7 @@ noise_consume_response(struct noise_local *l, struct noise_remote **rp, struct noise_remote *r = NULL; int ret = EINVAL; - if ((r = noise_remote_index_lookup(l, r_idx, 0)) == NULL) + if ((r = noise_remote_index_lookup(l, r_idx, false)) == NULL) return (ret); rw_rlock(&l->l_identity_lock); |