summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/config.c10
-rw-r--r--src/device.c3
-rw-r--r--src/peer.c5
-rw-r--r--src/timers.c33
-rw-r--r--src/timers.h5
5 files changed, 24 insertions, 32 deletions
diff --git a/src/config.c b/src/config.c
index bf8557c..b5d0799 100644
--- a/src/config.c
+++ b/src/config.c
@@ -14,10 +14,10 @@ static int set_device_port(struct wireguard_device *wg, u16 port)
struct wireguard_peer *peer, *temp;
socket_uninit(wg);
wg->incoming_port = port;
- if (!(wg->dev->flags & IFF_UP))
- return 0;
peer_for_each (wg, peer, temp, false)
socket_clear_peer_endpoint_src(peer);
+ if (!netif_running(wg->dev))
+ return 0;
return socket_init(wg);
}
@@ -72,8 +72,6 @@ static int set_peer(struct wireguard_device *wg, void __user *user_peer, size_t
peer = peer_rcu_get(peer_create(wg, in_peer.public_key, in_peer.preshared_key));
if (!peer)
return -ENOMEM;
- if (wg->dev->flags & IFF_UP)
- timers_init_peer(peer);
}
if (in_peer.flags & WGPEER_REMOVE_ME) {
@@ -107,13 +105,13 @@ static int set_peer(struct wireguard_device *wg, void __user *user_peer, size_t
}
if (in_peer.persistent_keepalive_interval != (u16)-1) {
- const bool send_keepalive = !peer->persistent_keepalive_interval && in_peer.persistent_keepalive_interval && wg->dev->flags & IFF_UP;
+ const bool send_keepalive = !peer->persistent_keepalive_interval && in_peer.persistent_keepalive_interval && netif_running(wg->dev);
peer->persistent_keepalive_interval = (unsigned long)in_peer.persistent_keepalive_interval * HZ;
if (send_keepalive)
packet_send_keepalive(peer);
}
- if (wg->dev->flags & IFF_UP)
+ if (netif_running(wg->dev))
packet_send_staged_packets(peer);
peer_put(peer);
diff --git a/src/device.c b/src/device.c
index eb1d59c..1fa7784 100644
--- a/src/device.c
+++ b/src/device.c
@@ -56,7 +56,6 @@ static int open(struct net_device *dev)
if (ret < 0)
return ret;
peer_for_each (wg, peer, temp, true) {
- timers_init_peer(peer);
packet_send_staged_packets(peer);
if (peer->persistent_keepalive_interval)
packet_send_keepalive(peer);
@@ -96,7 +95,7 @@ static int stop(struct net_device *dev)
struct wireguard_peer *peer, *temp;
peer_for_each (wg, peer, temp, true) {
skb_queue_purge(&peer->staged_packet_queue);
- timers_uninit_peer(peer);
+ timers_stop(peer);
noise_handshake_clear(&peer->handshake);
noise_keypairs_clear(&peer->keypairs);
}
diff --git a/src/peer.c b/src/peer.c
index cebda70..01b12fa 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -25,6 +25,7 @@ struct wireguard_peer *peer_create(struct wireguard_device *wg, const u8 public_
peer = kzalloc(sizeof(struct wireguard_peer), GFP_KERNEL);
if (!peer)
return NULL;
+ peer->device = wg;
if (dst_cache_init(&peer->endpoint_cache, GFP_KERNEL)) {
kfree(peer);
@@ -33,7 +34,6 @@ struct wireguard_peer *peer_create(struct wireguard_device *wg, const u8 public_
peer->internal_id = atomic64_inc_return(&peer_counter);
peer->serial_work_cpu = nr_cpumask_bits;
- peer->device = wg;
cookie_init(&peer->latest_cookie);
if (!noise_handshake_init(&peer->handshake, &wg->static_identity, public_key, preshared_key, peer)) {
kfree(peer);
@@ -49,6 +49,7 @@ struct wireguard_peer *peer_create(struct wireguard_device *wg, const u8 public_
packet_queue_init(&peer->tx_queue, packet_tx_worker, false);
packet_queue_init(&peer->rx_queue, packet_rx_worker, false);
skb_queue_head_init(&peer->staged_packet_queue);
+ timers_init(peer);
pr_debug("%s: Peer %Lu created\n", wg->dev->name, peer->internal_id);
return peer;
}
@@ -80,7 +81,7 @@ void peer_remove(struct wireguard_peer *peer)
noise_handshake_clear(&peer->handshake);
noise_keypairs_clear(&peer->keypairs);
list_del(&peer->peer_list);
- timers_uninit_peer(peer);
+ timers_stop(peer);
routing_table_remove_by_peer(&peer->device->peer_routing_table, peer);
pubkey_hashtable_remove(&peer->device->peer_hashtable, peer);
skb_queue_purge(&peer->staged_packet_queue);
diff --git a/src/timers.c b/src/timers.c
index e7cdd11..ef30005 100644
--- a/src/timers.c
+++ b/src/timers.c
@@ -31,7 +31,7 @@ static void expired_retransmit_handshake(unsigned long 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(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
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 +39,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(peer->timers_enabled) && !timer_pending(&peer->timer_zero_key_material))
+ if (likely(netif_running(peer->device->dev)) && !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 +59,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(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
mod_timer(&peer->timer_send_keepalive, jiffies + KEEPALIVE_TIMEOUT);
}
peer_put(peer);
@@ -94,7 +94,7 @@ static void expired_send_persistent_keepalive(unsigned long ptr)
{
peer_get_from_ptr(ptr);
if (likely(peer->persistent_keepalive_interval)) {
- if (likely(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
del_timer(&peer->timer_send_keepalive);
packet_send_keepalive(peer);
}
@@ -104,17 +104,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(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
del_timer(&peer->timer_send_keepalive);
- if (likely(peer->timers_enabled) && !timer_pending(&peer->timer_new_handshake))
+ if (likely(netif_running(peer->device->dev)) && !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(peer->timers_enabled) && !timer_pending(&peer->timer_send_keepalive))
+ if (likely(netif_running(peer->device->dev)) && !timer_pending(&peer->timer_send_keepalive))
mod_timer(&peer->timer_send_keepalive, jiffies + KEEPALIVE_TIMEOUT);
else
peer->timer_need_another_keepalive = true;
@@ -123,14 +123,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(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
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(peer->timers_enabled)) {
+ if (likely(netif_running(peer->device->dev))) {
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 +139,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(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
del_timer(&peer->timer_retransmit_handshake);
peer->timer_handshake_attempts = 0;
peer->sent_lastminute_handshake = false;
@@ -149,20 +149,19 @@ 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(peer->timers_enabled))
+ if (likely(netif_running(peer->device->dev)))
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(peer->timers_enabled))
+ if (peer->persistent_keepalive_interval && likely(netif_running(peer->device->dev)))
mod_timer(&peer->timer_persistent_keepalive, slack_time(jiffies + peer->persistent_keepalive_interval));
}
-void timers_init_peer(struct wireguard_peer *peer)
+void timers_init(struct wireguard_peer *peer)
{
- peer->timers_enabled = true;
setup_timer(&peer->timer_retransmit_handshake, expired_retransmit_handshake, (unsigned long)peer);
setup_timer(&peer->timer_send_keepalive, expired_send_keepalive, (unsigned long)peer);
setup_timer(&peer->timer_new_handshake, expired_new_handshake, (unsigned long)peer);
@@ -171,12 +170,8 @@ void timers_init_peer(struct wireguard_peer *peer)
INIT_WORK(&peer->clear_peer_work, queued_expired_zero_key_material);
}
-void timers_uninit_peer(struct wireguard_peer *peer)
+void timers_stop(struct wireguard_peer *peer)
{
- if (!peer->timers_enabled)
- return;
- peer->timers_enabled = false;
- wmb();
del_timer_sync(&peer->timer_retransmit_handshake);
del_timer_sync(&peer->timer_send_keepalive);
del_timer_sync(&peer->timer_new_handshake);
diff --git a/src/timers.h b/src/timers.h
index 4bfc00e..f9b2144 100644
--- a/src/timers.h
+++ b/src/timers.h
@@ -5,9 +5,8 @@
struct wireguard_peer;
-void timers_init_peer(struct wireguard_peer *peer);
-void timers_uninit_peer(struct wireguard_peer *peer);
-
+void timers_init(struct wireguard_peer *peer);
+void timers_stop(struct wireguard_peer *peer);
void timers_data_sent(struct wireguard_peer *peer);
void timers_data_received(struct wireguard_peer *peer);
void timers_any_authenticated_packet_received(struct wireguard_peer *peer);