Skip to contentSkip to conversation

persistent keepalive: start sending immediately

PATCH In review Jason A. Donenfeld 4 participants 5 messages Jul 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 persistent keepalive: start sending immediately

Rather than only start sending the persistent keepalive packets when the device first sends data, this changes it to send the packets immediately on ip link set up. This makes things generally seem more stateless, since the administrator does not have to manually ping the endpoint.

Of course, if you have a lot of peers and all of them have persistent keepalive enabled, this could cause a lot of unwanted immediate traffic. On the other hand, if all of those peers are at some point going to be sending packets, this would happen anyway. I suppose the moral of the story is that persistent keepalive is a feature really just for clients behind NAT, not for servers, and it should be used sparingly, which is why we've set it off by default in the first place.
---

Hi list,

I haven't merged this yet, and I'm debating whether it's something we want. I'm open to all opinions here, so feed back would be most welcome.

Thanks,
Jason

 src/config.c | 5 ++++-
 src/device.c | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)
src/config.c +4 -1
index 767d31a..910c31b 100644
@@ -106,8 +106,11 @@ static int set_peer(struct wireguard_device *wg, void __user *user_peer, size_t
 	if (in_peer.persistent_keepalive_interval != (uint16_t)-1) {
 		if (in_peer.persistent_keepalive_interval && (in_peer.persistent_keepalive_interval < 10 || in_peer.persistent_keepalive_interval > 3600))
 			ret = -EINVAL;
-		else
+		else {
+			if (!peer->persistent_keepalive_interval && in_peer.persistent_keepalive_interval && netdev_pub(wg)->flags & IFF_UP)
+				socket_send_buffer_to_peer(peer, NULL, 0, 0);
 			peer->persistent_keepalive_interval = in_peer.persistent_keepalive_interval;
+		}
 	}
 
 	if (netdev_pub(wg)->flags & IFF_UP)
src/device.c +2 -0
index 120f8c9..198368f 100644
@@ -40,6 +40,8 @@ static int open_peer(struct wireguard_peer *peer, void *data)
 	socket_set_peer_dst(peer);
 	timers_init_peer(peer);
 	packet_send_queue(peer);
+	if (peer->persistent_keepalive_interval)
+		socket_send_buffer_to_peer(peer, NULL, 0, 0);
 	return 0;
 }
 
Signature · 1 line
2.9.0
#1 lore raw
Quoted 14 lines · Jason A. Donenfeld

Rather than only start sending the persistent keepalive packets when the device first sends data, this changes it to send the packets immediately on ip link set up. This makes things generally seem more stateless, since the administrator does not have to manually ping the endpoint.

Of course, if you have a lot of peers and all of them have persistent keepalive enabled, this could cause a lot of unwanted immediate traffic. On the other hand, if all of those peers are at some point going to be sending packets, this would happen anyway. I suppose the moral of the story is that persistent keepalive is a feature really just for clients behind NAT, not for servers, and it should be used sparingly, which is why we've set it off by default in the first place.
---

Quoted 4 lines

Hi list,

I haven't merged this yet, and I'm debating whether it's something we want. I'm open to all opinions here, so feed back would be most welcome.

Hi all,

just to have a trace here of what I pointed out on IRC:
In the case of making a peer accessible through a NAT "I don't want to have to manually ping things from the client I'm trying to ssh to in the first place".

Quoted 46 lines

Thanks,
Jason

 src/config.c | 5 ++++-
 src/device.c | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)
src/config.c +4 -1
index 767d31a..910c31b 100644
@@ -106,8 +106,11 @@ static int set_peer(struct wireguard_device *wg, void __user *user_peer, size_t
 	if (in_peer.persistent_keepalive_interval != (uint16_t)-1) {
 		if (in_peer.persistent_keepalive_interval && (in_peer.persistent_keepalive_interval < 10 || in_peer.persistent_keepalive_interval > 3600))
 			ret = -EINVAL;
-		else
+		else {
+			if (!peer->persistent_keepalive_interval && in_peer.persistent_keepalive_interval && netdev_pub(wg)->flags & IFF_UP)
+				socket_send_buffer_to_peer(peer, NULL, 0, 0);
 			peer->persistent_keepalive_interval = in_peer.persistent_keepalive_interval;
+		}
 	}
 
 	if (netdev_pub(wg)->flags & IFF_UP)
src/device.c +2 -0
index 120f8c9..198368f 100644
@@ -40,6 +40,8 @@ static int open_peer(struct wireguard_peer *peer, void *data)
 	socket_set_peer_dst(peer);
 	timers_init_peer(peer);
 	packet_send_queue(peer);
+	if (peer->persistent_keepalive_interval)
+		socket_send_buffer_to_peer(peer, NULL, 0, 0);
 	return 0;
 }
 
Signature · 6 lines
2.9.0

_______________________________________________
WireGuard mailing list
[email protected]
http://lists.zx2c4.com/mailman/listinfo/wireguard
Signature · 1 line
William
#2 lore raw
Quoted 6 lines · William
Quoted 4 lines · Jason A. Donenfeld

Rather than only start sending the persistent keepalive packets when the device first sends data, this changes it to send the packets immediately on ip link set up. This makes things generally seem more stateless, since the administrator does not have to manually ping the endpoint.

Quoted 7 lines
Quoted 2 lines

I haven't merged this yet, and I'm debating whether it's something we want. I'm open to all opinions here, so feed back would be most welcome.

just to have a trace here of what I pointed out on IRC:
In the case of making a peer accessible through a NAT "I don't want to have to manually ping things from the client I'm trying to ssh to in the first place".

If you want to save people from sending data through the tunnel using pings (or whatever), you need to start holding the tunnel open when the device comes up. The whole point is to make the end point available to remotely initiated connections. Waiting for manually entered data does work. (Though you could just send one ping packet to start things rather than send them continuously as before this feature was added.)

#3 lore raw
In reply to Jason A. Donenfeld
Quoted 4 lines · Jason A. Donenfeld

Rather than only start sending the persistent keepalive packets when the device first sends data, this changes it to send the packets immediately on ip link set up. This makes things generally seem more stateless, since the administrator does not have to manually ping the endpoint.

Yes, this is good to have, for the reasons mentioned by others (immediate reachability in the reverse direction).

Does the first handshake occurs when the interface is up or when data is first sent on the interface?

Quoted 7 lines

Of course, if you have a lot of peers and all of them have persistent keepalive enabled, this could cause a lot of unwanted immediate traffic. On the other hand, if all of those peers are at some point going to be sending packets, this would happen anyway. I suppose the moral of the story is that persistent keepalive is a feature really just for clients behind NAT, not for servers, and it should be used sparingly, which is why we've set it off by default in the first place.

Sounds sensible.

If the large amount of "unwanted immediate traffic" when starting an interface is really a problem, you could just randomize the delay between keepalives (which might also help for obfuscation?). But as you said, keepalives don't really make sense for "servers", who would be the one with lots of peers.

Just a random thought, though: are keepalive packets in only one direction (client to server) enough to maintain a mapping in a NAT or stateful firewall? Aren't there edge cases where you would need keepalives in both directions?

Quoted 44 lines

Hi list,

I haven't merged this yet, and I'm debating whether it's something we want. I'm open to all opinions here, so feed back would be most welcome.

Thanks,
Jason

 src/config.c | 5 ++++-
 src/device.c | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)
src/config.c +4 -1
index 767d31a..910c31b 100644
@@ -106,8 +106,11 @@ static int set_peer(struct wireguard_device *wg, void __user *user_peer, size_t
 	if (in_peer.persistent_keepalive_interval != (uint16_t)-1) {
 		if (in_peer.persistent_keepalive_interval && (in_peer.persistent_keepalive_interval < 10 || in_peer.persistent_keepalive_interval > 3600))
 			ret = -EINVAL;
-		else
+		else {
+			if (!peer->persistent_keepalive_interval && in_peer.persistent_keepalive_interval && netdev_pub(wg)->flags & IFF_UP)
+				socket_send_buffer_to_peer(peer, NULL, 0, 0);
 			peer->persistent_keepalive_interval = in_peer.persistent_keepalive_interval;
+		}
 	}
 
 	if (netdev_pub(wg)->flags & IFF_UP)
src/device.c +2 -0
index 120f8c9..198368f 100644
@@ -40,6 +40,8 @@ static int open_peer(struct wireguard_peer *peer, void *data)
 	socket_set_peer_dst(peer);
 	timers_init_peer(peer);
 	packet_send_queue(peer);
+	if (peer->persistent_keepalive_interval)
+		socket_send_buffer_to_peer(peer, NULL, 0, 0);
 	return 0;
 }
 
#4 lore raw
Quoted 2 lines · Baptiste Jonglez

Yes, this is good to have, for the reasons mentioned by others (immediate reachability in the reverse direction).

Will merge this then.

Quoted 3 lines

Does the first handshake occurs when the interface is up or when data is first sent on the interface?

Only when data is sent. WireGuard is as silent as possible by default.

Quoted 5 lines

Just a random thought, though: are keepalive packets in only one direction (client to server) enough to maintain a mapping in a NAT or stateful firewall? Aren't there edge cases where you would need keepalives in both directions?

If you send an outgoing packet behind nat to a certain IP, that IP is expected to be able to reply to you right afterward. That's the characteristic we depend on.
If both sides are behind odd firewalls, then both sides could enable the feature. But I suspect in most cases only one side will use it.