I wanted to start an SSH session to my OpenWrt router, sleep (S3) the client host overnight, and return to the SSH session the next day. However, I found that OpenWrt (specifically Dropbear) had closed the session.
I set the Dropbear SSHKeepAlive interval to 21600 seconds (6 hours), and 3 times that should have given plenty of time to avoid the timeout, but it didn't. With double the setting (12 hours), the result was unchanged.
Testing by sleeping the client host immediately after starting the SSH session and examining the OpenWrt log, I found that the interval between these messages:
authpriv.info: dropbear[7850]: Child connection from xx.xx.xx.xx:nnnnn
authpriv.notice: dropbear[7850]: Password auth succeeded for 'root' from xx.xx.xx.xx:nnnnn
and this message:
authpriv.info: dropbear[7850]: Exit (root) from <xx.xx.xx.xx:nnnnn>: Error reading: Host is unreachable
was 8 hours plus about 15 minutes. There was no relation to the SSHKeepAlive interval I'd set.
Using the OpenSSH -v option on the client gave some more clues about what was going on. After a little over 8 hours:
root@openwrt:~# debug1: SSH2_MSG_KEXINIT received
debug1: SSH2_MSG_KEXINIT sent
debug1: kex: algorithm: sntrup761x25519-sha512
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: rekeying in progress
debug1: rekeying in progress
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:byuVT7M+p+mss4HewLoIFA61qD/8CzeKZGIMw8h3jTc
debug1: ssh_packet_send2_wrapped: resetting send seqnr 11
debug1: ssh_set_newkeys: rekeying out, input 5032 bytes 413 blocks, output 6244 bytes 426 blocks
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: ssh_packet_read_poll2: resetting read seqnr 15
debug1: SSH2_MSG_NEWKEYS received
debug1: ssh_set_newkeys: rekeying in, input 5052 bytes 415 blocks, output 6244 bytes 0 blocks
debug1: rekey in after 134217728 blocks
This shows that Dropbear has an undocumented feature, "rekeying" of the SSH session. The relevant code is in common-session.c:
if (!ses.kexstate.sentkexinit
&& (elapsed(now, ses.kexstate.lastkextime) >= KEX_REKEY_TIMEOUT
|| ses.kexstate.datarecv+ses.kexstate.datatrans >= KEX_REKEY_DATA
|| ses.kexstate.needrekey)) {
TRACE(("rekeying after timeout or max data reached"))
ses.kexstate.needrekey = 0;
send_msg_kexinit();
}
and sysoptions.h:
/* Spec recommends after one hour or 1 gigabyte of data. One hour
* is a bit too verbose, so we try 8 hours */
#ifndef KEX_REKEY_TIMEOUT
#define KEX_REKEY_TIMEOUT (3600 * 8)
#endif
#ifndef KEX_REKEY_DATA
#define KEX_REKEY_DATA (1<<30) /* 2^30 == 1GB, this value must be < INT_MAX */
#endif
The spec it refers to is RFC 4253, "The Secure Shell (SSH) Transport Layer Protocol", which uses the term "Key Re-Exchange" in section 9. The relevant part says:
It is RECOMMENDED that the keys be changed after each gigabyte of
transmitted data or after each hour of connection time, whichever
comes sooner. However, since the re-exchange is a public key
operation, it requires a fair amount of processing power and should
not be performed too often.
Perhaps less attention need be given to the second sentence of the paragraph now, 20 years after the RFC was published. Still, Dropbear changes the keys after 8 hours.
It's interesting to note that by default OpenSSH (server or client) doesn't use time-based rekeying.
So I'd solved the mystery (as it appeared to me) of why the SSH sessions were dropping out. What to do? I considered recompiling Dropbear with a modification to the timeout, or replacing Dropbear with OpenSSH. I decided to do neither, and just live with the problem.
This all took place on my own LAN, where although I'm aware of the need for security, I'm less concerned about it than I am about traffic on the Internet. SSH rekeying is a security feature, and if I wanted to use a prolonged SSH session on the Internet, I'd certainly consider enabling time-based rekeying in OpenSSH, or use dbclient, the Dropbear client.