From e0790d6de32ddc0a3a6ed1494960913eecd60cfb Mon Sep 17 00:00:00 2001 From: kn Date: Tue, 23 Nov 2021 19:13:45 +0000 Subject: [PATCH] Use system uptime not UTC time to calculate PPPoE session duration Systems without RTC are likely to boot with wrong time, but pppoe(4) used microtime(9) anyway to remember when a new session began. (In)adequately, ifconfig(8) used gettimeofday(2) and calculated the difference between two absoloute dates to infer the PPPoE session duration. This goes off the rails if the wall clock jumps in between, e.g. due to NTP kicking in. Use getmicrouptime(9) and clock_gettime(2)/CLOCK_BOOTTIME instead to rely on the monotonically increasing system uptime instead to fix this. Reported and tested by Peter J. Philipp on some octeon box without RTC. I've seen this on a Edgerouter 4 as well (2m uptime, 19d session). OK claudio --- sbin/ifconfig/ifconfig.c | 8 +++++--- sys/net/if_pppoe.c | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index ae58b9e65c9..5f901401965 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifconfig.c,v 1.450 2021/11/17 18:00:24 bket Exp $ */ +/* $OpenBSD: ifconfig.c,v 1.451 2021/11/23 19:13:45 kn Exp $ */ /* $NetBSD: ifconfig.c,v 1.40 1997/10/01 02:19:43 enami Exp $ */ /* @@ -5362,12 +5362,13 @@ pppoe_status(void) printf(" PADR retries: %d", state.padr_retry_no); if (state.state == PPPOE_STATE_SESSION) { - struct timeval temp_time; + struct timespec temp_time; time_t diff_time, day = 0; unsigned int hour = 0, min = 0, sec = 0; if (state.session_time.tv_sec != 0) { - gettimeofday(&temp_time, NULL); + if (clock_gettime(CLOCK_BOOTTIME, &temp_time) == -1) + goto notime; diff_time = temp_time.tv_sec - state.session_time.tv_sec; @@ -5387,6 +5388,7 @@ pppoe_status(void) printf("%lldd ", (long long)day); printf("%02u:%02u:%02u", hour, min, sec); } +notime: putchar('\n'); } diff --git a/sys/net/if_pppoe.c b/sys/net/if_pppoe.c index 36cd5a30689..a4322c20d54 100644 --- a/sys/net/if_pppoe.c +++ b/sys/net/if_pppoe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_pppoe.c,v 1.78 2021/07/19 19:00:58 stsp Exp $ */ +/* $OpenBSD: if_pppoe.c,v 1.79 2021/11/23 19:13:45 kn Exp $ */ /* $NetBSD: if_pppoe.c,v 1.51 2003/11/28 08:56:48 keihan Exp $ */ /* @@ -586,7 +586,7 @@ breakbreak: PPPOEDEBUG(("%s: session 0x%x connected\n", sc->sc_sppp.pp_if.if_xname, session)); sc->sc_state = PPPOE_STATE_SESSION; - microtime(&sc->sc_session_time); + getmicrouptime(&sc->sc_session_time); sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */ break; -- 2.20.1