aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Dunwoodie <ncon@mail.noconroy.net>2019-10-09 13:50:56 +0100
committerMatt Dunwoodie <ncon@mail.noconroy.net>2019-10-09 14:29:30 +0100
commit9fdaa13295bda1fcdfcab35be622de04964ca325 (patch)
tree140aab9670c4d18c4115c58b2d08160764481da7
parentPermission checks on keys probably don't belong here (diff)
downloadwireguard-openbsd-9fdaa13295bda1fcdfcab35be622de04964ca325.tar.xz
wireguard-openbsd-9fdaa13295bda1fcdfcab35be622de04964ca325.zip
Remove wg_peer_attach_session
Put all the session initialisation into wg_peer_new_session
-rw-r--r--src/wireguard.c64
1 files changed, 27 insertions, 37 deletions
diff --git a/src/wireguard.c b/src/wireguard.c
index 53bfeba..b0e4be0 100644
--- a/src/wireguard.c
+++ b/src/wireguard.c
@@ -78,10 +78,8 @@ enum wg_error wg_msg_response_valid_mac2(struct wg_msg_response *, struct wg_coo
struct wg_session *wg_peer_hs_session(struct wg_peer *);
struct wg_session *wg_peer_ks_session(struct wg_peer *);
+struct wg_session *wg_peer_new_session(struct wg_peer *);
void wg_session_drop(struct wg_session *);
-void wg_peer_attach_session(struct wg_peer *, struct wg_session *,
- struct wg_handshake *, enum wg_state);
-struct wg_session *wg_device_new_session(struct wg_device *);
/* Some crappy API */
void
@@ -150,18 +148,29 @@ wg_device_new_peer(struct wg_device *dev, struct wg_pubkey *key, void *arg)
}
struct wg_session *
-wg_device_new_session(struct wg_device *dev)
+wg_peer_new_session(struct wg_peer *peer)
{
- struct wg_session *session;
+ struct wg_session *session, *old_session;
session = malloc(sizeof(*session), M_DEVBUF, M_WAITOK | M_ZERO);
getnanotime(&session->s_created);
- mtx_init(&session->s_mtx, dev->d_mtx.mtx_wantipl);
+ session->s_peer = peer;
+ session->s_state = WG_STATE_NEW;
+ mtx_init(&session->s_mtx, peer->p_mtx.mtx_wantipl);
+
+ mtx_enter(&peer->p_mtx);
+
+ old_session = peer->p_hs_session;
+ peer->p_hs_session = session;
+ mtx_leave(&peer->p_mtx);
mtx_enter(&session->s_mtx);
- session->s_local_id = fm_insert(&dev->d_sessions, session);
+ session->s_local_id = fm_insert(&peer->p_device->d_sessions, session);
mtx_leave(&session->s_mtx);
+ if (old_session)
+ wg_session_drop(old_session);
+
return session;
}
@@ -216,31 +225,6 @@ wg_session_drop(struct wg_session *session)
}
void
-wg_peer_attach_session(struct wg_peer *peer, struct wg_session *session,
- struct wg_handshake *hs, enum wg_state state)
-{
- struct wg_session *old_session;
-
- /* Assert the session is not attached to another peer.
- * Assert the session is newly created, by checking s_state. */
- mtx_enter(&session->s_mtx);
- KASSERT(session->s_peer == NULL);
- KASSERT(session->s_state == WG_STATE_NEW);
- session->s_peer = peer;
- session->s_state = state;
- session->s_handshake = *hs;
- mtx_leave(&session->s_mtx);
-
- mtx_enter(&peer->p_mtx);
- old_session = peer->p_hs_session;
- peer->p_hs_session = session;
- mtx_leave(&peer->p_mtx);
-
- if (old_session)
- wg_session_drop(old_session);
-}
-
-void
wg_peer_reset_attempts(struct wg_peer *peer)
{
mtx_enter(&peer->p_mtx);
@@ -434,10 +418,14 @@ wg_device_rx_initiation(struct wg_device *dev, struct wg_msg_initiation *init,
ret_error(WG_TIMESTAMP);
}
- session = wg_device_new_session(dev);
+ session = wg_peer_new_session(peer);
+
+ mtx_enter(&session->s_mtx);
session->s_remote_id = init->sender;
+ session->s_handshake = hs;
+ session->s_state = WG_STATE_RECV_INITIATION;
+ mtx_leave(&session->s_mtx);
- wg_peer_attach_session(peer, session, &hs, WG_STATE_RECV_INITIATION);
dev->d_outq(peer, WG_PKT_RESPONSE, session->s_local_id);
wg_peer_put(peer);
*s = session;
@@ -638,7 +626,7 @@ wg_device_tx_initiation(struct wg_device *dev, struct wg_msg_initiation *init,
/* We need to generate the session here first, so we can use s_local_id
* below. We also want to operate on a local handshake, so we don't
* have to lock the session. */
- session = wg_device_new_session(dev);
+ session = wg_peer_new_session(peer);
wg_device_getkey(dev, &kp);
wg_keypair_generate(&hs.h_local);
@@ -681,8 +669,10 @@ wg_device_tx_initiation(struct wg_device *dev, struct wg_msg_initiation *init,
sizeof(init->mac2), offsetof(struct wg_msg_initiation, mac2),
sizeof(peer->p_cookie.cookie));
- wg_peer_attach_session(peer, session, &hs,
- WG_STATE_MADE_INITIATION);
+ mtx_enter(&session->s_mtx);
+ session->s_handshake = hs;
+ session->s_state = WG_STATE_MADE_INITIATION;
+ mtx_leave(&session->s_mtx);
*s = session;
leave: