Merged the hostap_2.6 updates, and the Leap of Faith work, from the hostap_update...
[mech_eap.git] / libeap / src / ap / accounting.c
index 7939c68..0aacc3c 100644 (file)
@@ -1,28 +1,24 @@
 /*
  * hostapd / RADIUS Accounting
- * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2002-2009, 2012-2015, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "utils/includes.h"
 
 #include "utils/common.h"
 #include "utils/eloop.h"
-#include "drivers/driver.h"
+#include "eapol_auth/eapol_auth_sm.h"
+#include "eapol_auth/eapol_auth_sm_i.h"
 #include "radius/radius.h"
 #include "radius/radius_client.h"
 #include "hostapd.h"
 #include "ieee802_1x.h"
 #include "ap_config.h"
 #include "sta_info.h"
+#include "ap_drv_ops.h"
 #include "accounting.h"
 
 
@@ -31,8 +27,8 @@
  * input/output octets and updates Acct-{Input,Output}-Gigawords. */
 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
 
-static void accounting_sta_get_id(struct hostapd_data *hapd,
-                                 struct sta_info *sta);
+static void accounting_sta_interim(struct hostapd_data *hapd,
+                                  struct sta_info *sta);
 
 
 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
@@ -44,44 +40,45 @@ static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
        u8 *val;
        size_t len;
        int i;
+       struct wpabuf *b;
+       struct os_time now;
 
        msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
                             radius_client_get_id(hapd->radius));
        if (msg == NULL) {
-               printf("Could not create net RADIUS packet\n");
+               wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
                return NULL;
        }
 
-       if (sta) {
-               radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
-
-               os_snprintf(buf, sizeof(buf), "%08X-%08X",
-                           sta->acct_session_id_hi, sta->acct_session_id_lo);
-               if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
-                                        (u8 *) buf, os_strlen(buf))) {
-                       printf("Could not add Acct-Session-Id\n");
-                       goto fail;
-               }
-       } else {
-               radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
-       }
-
        if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
                                       status_type)) {
-               printf("Could not add Acct-Status-Type\n");
-               goto fail;
-       }
-
-       if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
-                                      hapd->conf->ieee802_1x ?
-                                      RADIUS_ACCT_AUTHENTIC_RADIUS :
-                                      RADIUS_ACCT_AUTHENTIC_LOCAL)) {
-               printf("Could not add Acct-Authentic\n");
+               wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
                goto fail;
        }
 
        if (sta) {
+               if (!hostapd_config_get_radius_attr(
+                           hapd->conf->radius_acct_req_attr,
+                           RADIUS_ATTR_ACCT_AUTHENTIC) &&
+                   !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
+                                              hapd->conf->ieee802_1x ?
+                                              RADIUS_ACCT_AUTHENTIC_RADIUS :
+                                              RADIUS_ACCT_AUTHENTIC_LOCAL)) {
+                       wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
+                       goto fail;
+               }
+
+               /* Use 802.1X identity if available */
                val = ieee802_1x_get_identity(sta->eapol_sm, &len);
+
+               /* Use RADIUS ACL identity if 802.1X provides no identity */
+               if (!val && sta->identity) {
+                       val = (u8 *) sta->identity;
+                       len = os_strlen(sta->identity);
+               }
+
+               /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
+                * identity */
                if (!val) {
                        os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
                                    MAC2STR(sta->addr));
@@ -91,75 +88,16 @@ static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
 
                if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
                                         len)) {
-                       printf("Could not add User-Name\n");
+                       wpa_printf(MSG_INFO, "Could not add User-Name");
                        goto fail;
                }
        }
 
-       if (hapd->conf->own_ip_addr.af == AF_INET &&
-           !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
-                                (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
-               printf("Could not add NAS-IP-Address\n");
-               goto fail;
-       }
-
-#ifdef CONFIG_IPV6
-       if (hapd->conf->own_ip_addr.af == AF_INET6 &&
-           !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
-                                (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
-               printf("Could not add NAS-IPv6-Address\n");
-               goto fail;
-       }
-#endif /* CONFIG_IPV6 */
-
-       if (hapd->conf->nas_identifier &&
-           !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
-                                (u8 *) hapd->conf->nas_identifier,
-                                os_strlen(hapd->conf->nas_identifier))) {
-               printf("Could not add NAS-Identifier\n");
-               goto fail;
-       }
-
-       if (sta &&
-           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
-               printf("Could not add NAS-Port\n");
-               goto fail;
-       }
-
-       os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
-                   MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
-       if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
-                                (u8 *) buf, os_strlen(buf))) {
-               printf("Could not add Called-Station-Id\n");
+       if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
+                                  msg) < 0)
                goto fail;
-       }
 
        if (sta) {
-               os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
-                           MAC2STR(sta->addr));
-               if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
-                                        (u8 *) buf, os_strlen(buf))) {
-                       printf("Could not add Calling-Station-Id\n");
-                       goto fail;
-               }
-
-               if (!radius_msg_add_attr_int32(
-                           msg, RADIUS_ATTR_NAS_PORT_TYPE,
-                           RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
-                       printf("Could not add NAS-Port-Type\n");
-                       goto fail;
-               }
-
-               os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
-                           radius_sta_rate(hapd, sta) / 2,
-                           (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
-                           radius_mode_txt(hapd));
-               if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
-                                        (u8 *) buf, os_strlen(buf))) {
-                       printf("Could not add Connect-Info\n");
-                       goto fail;
-               }
-
                for (i = 0; ; i++) {
                        val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
                                                          i);
@@ -168,10 +106,54 @@ static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
 
                        if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
                                                 val, len)) {
-                               printf("Could not add Class\n");
+                               wpa_printf(MSG_INFO, "Could not add Class");
                                goto fail;
                        }
                }
+
+               b = ieee802_1x_get_radius_cui(sta->eapol_sm);
+               if (b &&
+                   !radius_msg_add_attr(msg,
+                                        RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
+                                        wpabuf_head(b), wpabuf_len(b))) {
+                       wpa_printf(MSG_ERROR, "Could not add CUI");
+                       goto fail;
+               }
+
+               if (!b && sta->radius_cui &&
+                   !radius_msg_add_attr(msg,
+                                        RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
+                                        (u8 *) sta->radius_cui,
+                                        os_strlen(sta->radius_cui))) {
+                       wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
+                       goto fail;
+               }
+
+               if (sta->ipaddr &&
+                   !radius_msg_add_attr_int32(msg,
+                                              RADIUS_ATTR_FRAMED_IP_ADDRESS,
+                                              be_to_host32(sta->ipaddr))) {
+                       wpa_printf(MSG_ERROR,
+                                  "Could not add Framed-IP-Address");
+                       goto fail;
+               }
+       }
+
+       os_get_time(&now);
+       if (now.sec > 1000000000 &&
+           !radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
+                                      now.sec)) {
+               wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
+               goto fail;
+       }
+
+       /*
+        * Add Acct-Delay-Time with zero value for the first transmission. This
+        * will be updated within radius_client.c when retransmitting the frame.
+        */
+       if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_DELAY_TIME, 0)) {
+               wpa_printf(MSG_INFO, "Could not add Acct-Delay-Time");
+               goto fail;
        }
 
        return msg;
@@ -186,22 +168,28 @@ static int accounting_sta_update_stats(struct hostapd_data *hapd,
                                       struct sta_info *sta,
                                       struct hostap_sta_driver_data *data)
 {
-       if (hapd->drv.read_sta_data(hapd, data, sta->addr))
+       if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
                return -1;
 
-       if (sta->last_rx_bytes > data->rx_bytes)
-               sta->acct_input_gigawords++;
-       if (sta->last_tx_bytes > data->tx_bytes)
-               sta->acct_output_gigawords++;
-       sta->last_rx_bytes = data->rx_bytes;
-       sta->last_tx_bytes = data->tx_bytes;
+       if (!data->bytes_64bit) {
+               /* Extend 32-bit counters from the driver to 64-bit counters */
+               if (sta->last_rx_bytes_lo > data->rx_bytes)
+                       sta->last_rx_bytes_hi++;
+               sta->last_rx_bytes_lo = data->rx_bytes;
+
+               if (sta->last_tx_bytes_lo > data->tx_bytes)
+                       sta->last_tx_bytes_hi++;
+               sta->last_tx_bytes_lo = data->tx_bytes;
+       }
 
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
-                      HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
-                      "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
-                      "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
-                      sta->last_rx_bytes, sta->acct_input_gigawords,
-                      sta->last_tx_bytes, sta->acct_output_gigawords);
+                      HOSTAPD_LEVEL_DEBUG,
+                      "updated TX/RX stats: rx_bytes=%llu [%u:%u] tx_bytes=%llu [%u:%u] bytes_64bit=%d",
+                      data->rx_bytes, sta->last_rx_bytes_hi,
+                      sta->last_rx_bytes_lo,
+                      data->tx_bytes, sta->last_tx_bytes_hi,
+                      sta->last_tx_bytes_lo,
+                      data->bytes_64bit);
 
        return 0;
 }
@@ -240,16 +228,17 @@ void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
        if (sta->acct_session_started)
                return;
 
-       accounting_sta_get_id(hapd, sta);
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
                       HOSTAPD_LEVEL_INFO,
-                      "starting accounting session %08X-%08X",
-                      sta->acct_session_id_hi, sta->acct_session_id_lo);
+                      "starting accounting session %016llX",
+                      (unsigned long long) sta->acct_session_id);
 
-       time(&sta->acct_session_start);
-       sta->last_rx_bytes = sta->last_tx_bytes = 0;
-       sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
-       hapd->drv.sta_clear_stats(hapd, sta->addr);
+       os_get_reltime(&sta->acct_session_start);
+       sta->last_rx_bytes_hi = 0;
+       sta->last_rx_bytes_lo = 0;
+       sta->last_tx_bytes_hi = 0;
+       sta->last_tx_bytes_lo = 0;
+       hostapd_drv_sta_clear_stats(hapd, sta->addr);
 
        if (!hapd->conf->radius->acct_server)
                return;
@@ -262,8 +251,9 @@ void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
                               hapd, sta);
 
        msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
-       if (msg)
-               radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
+       if (msg &&
+           radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
+               radius_msg_free(msg);
 
        sta->acct_session_started = 1;
 }
@@ -275,7 +265,8 @@ 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;
-       u32 gigawords;
+       struct os_reltime now_r, diff;
+       u64 bytes;
 
        if (!hapd->conf->radius->acct_server)
                return;
@@ -284,13 +275,15 @@ static void accounting_sta_report(struct hostapd_data *hapd,
                             stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
                             RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
        if (!msg) {
-               printf("Could not create RADIUS Accounting message\n");
+               wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
                return;
        }
 
+       os_get_reltime(&now_r);
+       os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
        if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
-                                      time(NULL) - sta->acct_session_start)) {
-               printf("Could not add Acct-Session-Time\n");
+                                      diff.sec)) {
+               wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
                goto fail;
        }
 
@@ -298,70 +291,65 @@ static void accounting_sta_report(struct hostapd_data *hapd,
                if (!radius_msg_add_attr_int32(msg,
                                               RADIUS_ATTR_ACCT_INPUT_PACKETS,
                                               data.rx_packets)) {
-                       printf("Could not add Acct-Input-Packets\n");
+                       wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
                        goto fail;
                }
                if (!radius_msg_add_attr_int32(msg,
                                               RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
                                               data.tx_packets)) {
-                       printf("Could not add Acct-Output-Packets\n");
+                       wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
                        goto fail;
                }
+               if (data.bytes_64bit)
+                       bytes = data.rx_bytes;
+               else
+                       bytes = ((u64) sta->last_rx_bytes_hi << 32) |
+                               sta->last_rx_bytes_lo;
                if (!radius_msg_add_attr_int32(msg,
                                               RADIUS_ATTR_ACCT_INPUT_OCTETS,
-                                              data.rx_bytes)) {
-                       printf("Could not add Acct-Input-Octets\n");
+                                              (u32) bytes)) {
+                       wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
                        goto fail;
                }
-               gigawords = sta->acct_input_gigawords;
-#if __WORDSIZE == 64
-               gigawords += data.rx_bytes >> 32;
-#endif
-               if (gigawords &&
-                   !radius_msg_add_attr_int32(
-                           msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
-                           gigawords)) {
-                       printf("Could not add Acct-Input-Gigawords\n");
+               if (!radius_msg_add_attr_int32(msg,
+                                              RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
+                                              (u32) (bytes >> 32))) {
+                       wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
                        goto fail;
                }
+               if (data.bytes_64bit)
+                       bytes = data.tx_bytes;
+               else
+                       bytes = ((u64) sta->last_tx_bytes_hi << 32) |
+                               sta->last_tx_bytes_lo;
                if (!radius_msg_add_attr_int32(msg,
                                               RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
-                                              data.tx_bytes)) {
-                       printf("Could not add Acct-Output-Octets\n");
+                                              (u32) bytes)) {
+                       wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
                        goto fail;
                }
-               gigawords = sta->acct_output_gigawords;
-#if __WORDSIZE == 64
-               gigawords += data.tx_bytes >> 32;
-#endif
-               if (gigawords &&
-                   !radius_msg_add_attr_int32(
-                           msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
-                           gigawords)) {
-                       printf("Could not add Acct-Output-Gigawords\n");
+               if (!radius_msg_add_attr_int32(msg,
+                                              RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
+                                              (u32) (bytes >> 32))) {
+                       wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
                        goto fail;
                }
        }
 
-       if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
-                                      time(NULL))) {
-               printf("Could not add Event-Timestamp\n");
-               goto fail;
-       }
-
        if (eloop_terminated())
                cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
 
        if (stop && cause &&
            !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
                                       cause)) {
-               printf("Could not add Acct-Terminate-Cause\n");
+               wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
                goto fail;
        }
 
-       radius_client_send(hapd->radius, msg,
-                          stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
-                          sta->addr);
+       if (radius_client_send(hapd->radius, msg,
+                              stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
+                              sta->addr) < 0)
+               goto fail;
        return;
 
  fail:
@@ -374,7 +362,8 @@ static void accounting_sta_report(struct hostapd_data *hapd,
  * @hapd: hostapd BSS data
  * @sta: The station
  */
-void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
+static void accounting_sta_interim(struct hostapd_data *hapd,
+                                  struct sta_info *sta)
 {
        if (sta->acct_session_started)
                accounting_sta_report(hapd, sta, 0);
@@ -393,22 +382,17 @@ void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
                eloop_cancel_timeout(accounting_interim_update, hapd, sta);
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
                               HOSTAPD_LEVEL_INFO,
-                              "stopped accounting session %08X-%08X",
-                              sta->acct_session_id_hi,
-                              sta->acct_session_id_lo);
+                              "stopped accounting session %016llX",
+                              (unsigned long long) sta->acct_session_id);
                sta->acct_session_started = 0;
        }
 }
 
 
-static void accounting_sta_get_id(struct hostapd_data *hapd,
-                                 struct sta_info *sta)
+int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
 {
-       sta->acct_session_id_lo = hapd->acct_session_id_lo++;
-       if (hapd->acct_session_id_lo == 0) {
-               hapd->acct_session_id_hi++;
-       }
-       sta->acct_session_id_hi = hapd->acct_session_id_hi;
+       return radius_gen_session_id((u8 *) &sta->acct_session_id,
+                                    sizeof(sta->acct_session_id));
 }
 
 
@@ -427,13 +411,12 @@ accounting_receive(struct radius_msg *msg, struct radius_msg *req,
                   void *data)
 {
        if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
-               printf("Unknown RADIUS message code\n");
+               wpa_printf(MSG_INFO, "Unknown RADIUS message code");
                return RADIUS_RX_UNKNOWN;
        }
 
        if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
-               printf("Incoming RADIUS packet did not have correct "
-                      "Authenticator - dropped\n");
+               wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
                return RADIUS_RX_INVALID_AUTHENTICATOR;
        }
 
@@ -456,15 +439,75 @@ static void accounting_report_state(struct hostapd_data *hapd, int on)
        if (!msg)
                return;
 
-       if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
-                                      RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
-       {
-               printf("Could not add Acct-Terminate-Cause\n");
+       if (hapd->acct_session_id) {
+               char buf[20];
+
+               os_snprintf(buf, sizeof(buf), "%016llX",
+                           (unsigned long long) hapd->acct_session_id);
+               if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
+                                        (u8 *) buf, os_strlen(buf)))
+                       wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
+       }
+
+       if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
                radius_msg_free(msg);
+}
+
+
+static void accounting_interim_error_cb(const u8 *addr, void *ctx)
+{
+       struct hostapd_data *hapd = ctx;
+       struct sta_info *sta;
+       unsigned int i, wait_time;
+       int res;
+
+       sta = ap_get_sta(hapd, addr);
+       if (!sta)
+               return;
+       sta->acct_interim_errors++;
+       if (sta->acct_interim_errors > 10 /* RADIUS_CLIENT_MAX_RETRIES */) {
+               wpa_printf(MSG_DEBUG,
+                          "Interim RADIUS accounting update failed for " MACSTR
+                          " - too many errors, abandon this interim accounting update",
+                          MAC2STR(addr));
+               sta->acct_interim_errors = 0;
+               /* Next update will be tried after normal update interval */
                return;
        }
 
-       radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
+       /*
+        * Use a shorter update interval as an improved retransmission mechanism
+        * for failed interim accounting updates. This allows the statistics to
+        * be updated for each retransmission.
+        *
+        * RADIUS client code has already waited RADIUS_CLIENT_FIRST_WAIT.
+        * Schedule the first retry attempt immediately and every following one
+        * with exponential backoff.
+        */
+       if (sta->acct_interim_errors == 1) {
+               wait_time = 0;
+       } else {
+               wait_time = 3; /* RADIUS_CLIENT_FIRST_WAIT */
+               for (i = 1; i < sta->acct_interim_errors; i++)
+                       wait_time *= 2;
+       }
+       res = eloop_deplete_timeout(wait_time, 0, accounting_interim_update,
+                                   hapd, sta);
+       if (res == 1)
+               wpa_printf(MSG_DEBUG,
+                          "Interim RADIUS accounting update failed for " MACSTR
+                          " (error count: %u) - schedule next update in %u seconds",
+                          MAC2STR(addr), sta->acct_interim_errors, wait_time);
+       else if (res == 0)
+               wpa_printf(MSG_DEBUG,
+                          "Interim RADIUS accounting update failed for " MACSTR
+                          " (error count: %u)", MAC2STR(addr),
+                          sta->acct_interim_errors);
+       else
+               wpa_printf(MSG_DEBUG,
+                          "Interim RADIUS accounting update failed for " MACSTR
+                          " (error count: %u) - no timer found", MAC2STR(addr),
+                          sta->acct_interim_errors);
 }
 
 
@@ -475,13 +518,15 @@ static void accounting_report_state(struct hostapd_data *hapd, int on)
  */
 int accounting_init(struct hostapd_data *hapd)
 {
-       /* Acct-Session-Id should be unique over reboots. If reliable clock is
-        * not available, this could be replaced with reboot counter, etc. */
-       hapd->acct_session_id_hi = time(NULL);
+       if (radius_gen_session_id((u8 *) &hapd->acct_session_id,
+                                 sizeof(hapd->acct_session_id)) < 0)
+               return -1;
 
        if (radius_client_register(hapd->radius, RADIUS_ACCT,
                                   accounting_receive, hapd))
                return -1;
+       radius_client_set_interim_error_cb(hapd->radius,
+                                          accounting_interim_error_cb, hapd);
 
        accounting_report_state(hapd, 1);
 
@@ -490,7 +535,7 @@ int accounting_init(struct hostapd_data *hapd)
 
 
 /**
- * accounting_deinit: Deinitilize accounting
+ * accounting_deinit: Deinitialize accounting
  * @hapd: hostapd BSS data
  */
 void accounting_deinit(struct hostapd_data *hapd)