Patchwork [2/8] AP: use monotonic time for STA accounting

login
register
mail settings
Submitter Johannes Berg
Date Nov. 25, 2013, 8:56 p.m.
Message ID <1385412969-15373-3-git-send-email-johannes@sipsolutions.net>
Download mbox | patch
Permalink /patch/294091/
State New
Headers show

Comments

Johannes Berg - Nov. 25, 2013, 8:56 p.m.
From: Johannes Berg <johannes.berg@intel.com>

For type-safetype, make sta->acct_session_start a struct os_reltime
and then use monotonic time for accounting.

Signed-hostap: Johannes Berg <johannes.berg@intel.com>
---
 src/ap/accounting.c | 11 +++++------
 src/ap/ieee802_1x.c |  6 +++---
 src/ap/sta_info.h   |  2 +-
 3 files changed, 9 insertions(+), 10 deletions(-)
Jouni Malinen - Dec. 8, 2013, 2:21 a.m.
On Mon, Nov 25, 2013 at 09:56:03PM +0100, Johannes Berg wrote:
> For type-safetype, make sta->acct_session_start a struct os_reltime

type-safety? ;-)

> diff --git a/src/ap/accounting.c b/src/ap/accounting.c
> @@ -244,7 +242,7 @@ static void accounting_sta_report(struct hostapd_data *hapd,
> -	struct os_time now;
> +	struct os_reltime now, diff;
> @@ -258,9 +256,10 @@ static void accounting_sta_report(struct hostapd_data *hapd,
> -	os_get_time(&now);
> +	os_get_reltime(&now);
> +	os_reltime_sub(&now, &sta->acct_session_start, &diff);
>  	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
> -				       now.sec - sta->acct_session_start)) {
> +				       diff.sec)) {

While this part here looks fine, the following use of now in the same
function is problematic:

    if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
		       now.sec)) {

I'd expect that RADIUS Timestamp attribute to be in showing the real
timestamp of the current moment rather than the system-internal relative
timestamp.

Patch

diff --git a/src/ap/accounting.c b/src/ap/accounting.c
index a1f67f0..8d490f2 100644
--- a/src/ap/accounting.c
+++ b/src/ap/accounting.c
@@ -202,7 +202,6 @@  static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
 {
 	struct radius_msg *msg;
-	struct os_time t;
 	int interval;
 
 	if (sta->acct_session_started)
@@ -213,8 +212,7 @@  void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
 		       "starting accounting session %08X-%08X",
 		       sta->acct_session_id_hi, sta->acct_session_id_lo);
 
-	os_get_time(&t);
-	sta->acct_session_start = t.sec;
+	os_get_reltime(&sta->acct_session_start);
 	sta->last_rx_bytes = sta->last_tx_bytes = 0;
 	sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
 	hostapd_drv_sta_clear_stats(hapd, sta->addr);
@@ -244,7 +242,7 @@  static void accounting_sta_report(struct hostapd_data *hapd,
 	struct radius_msg *msg;
 	int cause = sta->acct_terminate_cause;
 	struct hostap_sta_driver_data data;
-	struct os_time now;
+	struct os_reltime now, diff;
 	u32 gigawords;
 
 	if (!hapd->conf->radius->acct_server)
@@ -258,9 +256,10 @@  static void accounting_sta_report(struct hostapd_data *hapd,
 		return;
 	}
 
-	os_get_time(&now);
+	os_get_reltime(&now);
+	os_reltime_sub(&now, &sta->acct_session_start, &diff);
 	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
-				       now.sec - sta->acct_session_start)) {
+				       diff.sec)) {
 		wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
 		goto fail;
 	}
diff --git a/src/ap/ieee802_1x.c b/src/ap/ieee802_1x.c
index 7874332..26685ef 100644
--- a/src/ap/ieee802_1x.c
+++ b/src/ap/ieee802_1x.c
@@ -1951,7 +1951,7 @@  int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
 {
 	int len = 0, ret;
 	struct eapol_state_machine *sm = sta->eapol_sm;
-	struct os_time t;
+	struct os_reltime diff;
 
 	if (sm == NULL)
 		return 0;
@@ -2066,7 +2066,7 @@  int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
 	len += ret;
 
 	/* dot1xAuthSessionStatsTable */
-	os_get_time(&t);
+	os_reltime_age(&sta->acct_session_start, &diff);
 	ret = os_snprintf(buf + len, buflen - len,
 			  /* TODO: dot1xAuthSessionOctetsRx */
 			  /* TODO: dot1xAuthSessionOctetsTx */
@@ -2081,7 +2081,7 @@  int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
 			  (wpa_key_mgmt_wpa_ieee8021x(
 				   wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
 			  1 : 2,
-			  (unsigned int) (t.sec - sta->acct_session_start),
+			  (unsigned int) diff.sec,
 			  sm->identity);
 	if (ret < 0 || (size_t) ret >= buflen - len)
 		return len;
diff --git a/src/ap/sta_info.h b/src/ap/sta_info.h
index dc74219..047b8f0 100644
--- a/src/ap/sta_info.h
+++ b/src/ap/sta_info.h
@@ -78,7 +78,7 @@  struct sta_info {
 
 	u32 acct_session_id_hi;
 	u32 acct_session_id_lo;
-	time_t acct_session_start;
+	struct os_reltime acct_session_start;
 	int acct_session_started;
 	int acct_terminate_cause; /* Acct-Terminate-Cause */
 	int acct_interim_interval; /* Acct-Interim-Interval */