upstream commit

fix ClientAliveInterval when a time-based RekeyLimit is
 set; previously keepalive packets were not being sent. bz#2252 report and
 analysis by Christian Wittenhorst and Garrett Lee feedback and ok dtucker@

Upstream-ID: d48f9deadd35fdacdd5106b41bb07630ddd4aa81
diff --git a/serverloop.c b/serverloop.c
index 80d1db5..e6a9247 100644
--- a/serverloop.c
+++ b/serverloop.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: serverloop.c,v 1.182 2016/02/08 10:57:07 djm Exp $ */
+/* $OpenBSD: serverloop.c,v 1.183 2016/03/04 03:35:44 djm Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -276,7 +276,7 @@
  */
 static void
 wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
-    u_int *nallocp, u_int64_t max_time_milliseconds)
+    u_int *nallocp, u_int64_t max_time_ms)
 {
 	struct timeval tv, *tvp;
 	int ret;
@@ -288,9 +288,9 @@
 	channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
 	    &minwait_secs, 0);
 
+	/* XXX need proper deadline system for rekey/client alive */
 	if (minwait_secs != 0)
-		max_time_milliseconds = MIN(max_time_milliseconds,
-		    (u_int)minwait_secs * 1000);
+		max_time_ms = MIN(max_time_ms, (u_int)minwait_secs * 1000);
 
 	/*
 	 * if using client_alive, set the max timeout accordingly,
@@ -300,11 +300,13 @@
 	 * this could be randomized somewhat to make traffic
 	 * analysis more difficult, but we're not doing it yet.
 	 */
-	if (compat20 &&
-	    max_time_milliseconds == 0 && options.client_alive_interval) {
+	if (compat20 && options.client_alive_interval) {
+		uint64_t keepalive_ms =
+		    (uint64_t)options.client_alive_interval * 1000;
+
 		client_alive_scheduled = 1;
-		max_time_milliseconds =
-		    (u_int64_t)options.client_alive_interval * 1000;
+		if (max_time_ms == 0 || max_time_ms > keepalive_ms)
+			max_time_ms = keepalive_ms;
 	}
 
 	if (compat20) {
@@ -353,14 +355,14 @@
 	 * from it, then read as much as is available and exit.
 	 */
 	if (child_terminated && packet_not_very_much_data_to_write())
-		if (max_time_milliseconds == 0 || client_alive_scheduled)
-			max_time_milliseconds = 100;
+		if (max_time_ms == 0 || client_alive_scheduled)
+			max_time_ms = 100;
 
-	if (max_time_milliseconds == 0)
+	if (max_time_ms == 0)
 		tvp = NULL;
 	else {
-		tv.tv_sec = max_time_milliseconds / 1000;
-		tv.tv_usec = 1000 * (max_time_milliseconds % 1000);
+		tv.tv_sec = max_time_ms / 1000;
+		tv.tv_usec = 1000 * (max_time_ms % 1000);
 		tvp = &tv;
 	}