2008-02-28 02:34:43 +01:00
|
|
|
/*
|
|
|
|
* hostapd / RADIUS Accounting
|
2015-10-17 18:53:29 +02:00
|
|
|
* Copyright (c) 2002-2009, 2012-2015, Jouni Malinen <j@w1.fi>
|
2008-02-28 02:34:43 +01:00
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2008-02-28 02:34:43 +01:00
|
|
|
*/
|
|
|
|
|
2009-12-25 23:05:40 +01:00
|
|
|
#include "utils/includes.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
|
2009-12-25 23:05:40 +01:00
|
|
|
#include "utils/common.h"
|
|
|
|
#include "utils/eloop.h"
|
2014-10-18 09:20:24 +02:00
|
|
|
#include "eapol_auth/eapol_auth_sm.h"
|
|
|
|
#include "eapol_auth/eapol_auth_sm_i.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
#include "radius/radius.h"
|
|
|
|
#include "radius/radius_client.h"
|
2009-12-25 23:05:40 +01:00
|
|
|
#include "hostapd.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
#include "ieee802_1x.h"
|
2009-12-25 23:05:40 +01:00
|
|
|
#include "ap_config.h"
|
2009-03-25 15:13:35 +01:00
|
|
|
#include "sta_info.h"
|
2010-11-24 15:34:49 +01:00
|
|
|
#include "ap_drv_ops.h"
|
2009-12-25 23:05:40 +01:00
|
|
|
#include "accounting.h"
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Default interval in seconds for polling TX/RX octets from the driver if
|
|
|
|
* STA is not using interim accounting. This detects wrap arounds for
|
|
|
|
* input/output octets and updates Acct-{Input,Output}-Gigawords. */
|
|
|
|
#define ACCT_DEFAULT_UPDATE_INTERVAL 300
|
|
|
|
|
2012-08-05 18:40:49 +02:00
|
|
|
static void accounting_sta_interim(struct hostapd_data *hapd,
|
|
|
|
struct sta_info *sta);
|
2008-11-23 10:12:17 +01:00
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
|
|
|
|
struct sta_info *sta,
|
|
|
|
int status_type)
|
|
|
|
{
|
|
|
|
struct radius_msg *msg;
|
|
|
|
char buf[128];
|
|
|
|
u8 *val;
|
|
|
|
size_t len;
|
|
|
|
int i;
|
2012-05-05 17:05:09 +02:00
|
|
|
struct wpabuf *b;
|
2016-01-24 12:38:40 +01:00
|
|
|
struct os_time now;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
|
|
|
|
radius_client_get_id(hapd->radius));
|
|
|
|
if (msg == NULL) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
|
2008-02-28 02:34:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
|
|
|
|
status_type)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sta) {
|
2016-01-25 11:53:28 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-19 13:27:30 +02:00
|
|
|
/* Use 802.1X identity if available */
|
2008-02-28 02:34:43 +01:00
|
|
|
val = ieee802_1x_get_identity(sta->eapol_sm, &len);
|
2012-08-19 13:27:30 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2008-02-28 02:34:43 +01:00
|
|
|
if (!val) {
|
|
|
|
os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
|
|
|
|
MAC2STR(sta->addr));
|
|
|
|
val = (u8 *) buf;
|
|
|
|
len = os_strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
|
|
|
|
len)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add User-Name");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:13:15 +02:00
|
|
|
if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
|
|
|
|
msg) < 0)
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (sta) {
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
|
|
|
|
i);
|
|
|
|
if (val == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
|
|
|
|
val, len)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Class");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2012-05-05 17:05:09 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-08-19 13:27:30 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-10-17 18:53:29 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 12:38:40 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:40:23 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
return msg;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
radius_msg_free(msg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int accounting_sta_update_stats(struct hostapd_data *hapd,
|
|
|
|
struct sta_info *sta,
|
|
|
|
struct hostap_sta_driver_data *data)
|
|
|
|
{
|
2010-11-24 15:34:49 +01:00
|
|
|
if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
|
2008-02-28 02:34:43 +01:00
|
|
|
return -1;
|
|
|
|
|
2016-02-19 16:22:25 +01:00
|
|
|
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;
|
|
|
|
}
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
|
2016-02-19 16:22:25 +01:00
|
|
|
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);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
|
|
|
|
{
|
|
|
|
struct hostapd_data *hapd = eloop_ctx;
|
|
|
|
struct sta_info *sta = timeout_ctx;
|
|
|
|
int interval;
|
|
|
|
|
|
|
|
if (sta->acct_interim_interval) {
|
|
|
|
accounting_sta_interim(hapd, sta);
|
|
|
|
interval = sta->acct_interim_interval;
|
|
|
|
} else {
|
|
|
|
struct hostap_sta_driver_data data;
|
|
|
|
accounting_sta_update_stats(hapd, sta, &data);
|
|
|
|
interval = ACCT_DEFAULT_UPDATE_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
eloop_register_timeout(interval, 0, accounting_interim_update,
|
|
|
|
hapd, sta);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 15:33:00 +01:00
|
|
|
/**
|
|
|
|
* accounting_sta_start - Start STA accounting
|
|
|
|
* @hapd: hostapd BSS data
|
|
|
|
* @sta: The station
|
|
|
|
*/
|
2008-02-28 02:34:43 +01:00
|
|
|
void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
|
|
|
|
{
|
|
|
|
struct radius_msg *msg;
|
|
|
|
int interval;
|
|
|
|
|
|
|
|
if (sta->acct_session_started)
|
|
|
|
return;
|
|
|
|
|
2008-11-23 10:12:17 +01:00
|
|
|
hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
|
|
|
|
HOSTAPD_LEVEL_INFO,
|
2016-02-13 21:29:47 +01:00
|
|
|
"starting accounting session %016llX",
|
|
|
|
(unsigned long long) sta->acct_session_id);
|
2008-11-23 10:12:17 +01:00
|
|
|
|
2013-12-16 21:08:22 +01:00
|
|
|
os_get_reltime(&sta->acct_session_start);
|
2016-02-19 16:22:25 +01:00
|
|
|
sta->last_rx_bytes_hi = 0;
|
|
|
|
sta->last_rx_bytes_lo = 0;
|
|
|
|
sta->last_tx_bytes_hi = 0;
|
|
|
|
sta->last_tx_bytes_lo = 0;
|
2010-11-24 15:34:49 +01:00
|
|
|
hostapd_drv_sta_clear_stats(hapd, sta->addr);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (!hapd->conf->radius->acct_server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (sta->acct_interim_interval)
|
|
|
|
interval = sta->acct_interim_interval;
|
|
|
|
else
|
|
|
|
interval = ACCT_DEFAULT_UPDATE_INTERVAL;
|
|
|
|
eloop_register_timeout(interval, 0, accounting_interim_update,
|
|
|
|
hapd, sta);
|
|
|
|
|
|
|
|
msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
|
2012-04-01 16:55:20 +02:00
|
|
|
if (msg &&
|
|
|
|
radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
|
|
|
|
radius_msg_free(msg);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
sta->acct_session_started = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-03 19:38:42 +01:00
|
|
|
static void accounting_sta_report(struct hostapd_data *hapd,
|
|
|
|
struct sta_info *sta, int stop)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
struct radius_msg *msg;
|
|
|
|
int cause = sta->acct_terminate_cause;
|
|
|
|
struct hostap_sta_driver_data data;
|
2013-12-16 21:08:22 +01:00
|
|
|
struct os_reltime now_r, diff;
|
2016-02-19 16:22:25 +01:00
|
|
|
u64 bytes;
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
if (!hapd->conf->radius->acct_server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg = accounting_msg(hapd, sta,
|
|
|
|
stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
|
|
|
|
RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
|
|
|
|
if (!msg) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:08:22 +01:00
|
|
|
os_get_reltime(&now_r);
|
|
|
|
os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
|
2008-02-28 02:34:43 +01:00
|
|
|
if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
|
2013-12-16 21:08:22 +01:00
|
|
|
diff.sec)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
|
|
|
|
if (!radius_msg_add_attr_int32(msg,
|
|
|
|
RADIUS_ATTR_ACCT_INPUT_PACKETS,
|
|
|
|
data.rx_packets)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!radius_msg_add_attr_int32(msg,
|
|
|
|
RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
|
|
|
|
data.tx_packets)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-02-19 16:22:25 +01:00
|
|
|
if (data.bytes_64bit)
|
|
|
|
bytes = data.rx_bytes;
|
|
|
|
else
|
|
|
|
bytes = ((u64) sta->last_rx_bytes_hi << 32) |
|
|
|
|
sta->last_rx_bytes_lo;
|
2008-02-28 02:34:43 +01:00
|
|
|
if (!radius_msg_add_attr_int32(msg,
|
|
|
|
RADIUS_ATTR_ACCT_INPUT_OCTETS,
|
2016-02-19 16:22:25 +01:00
|
|
|
(u32) bytes)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-02-19 16:22:25 +01:00
|
|
|
if (!radius_msg_add_attr_int32(msg,
|
|
|
|
RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
|
|
|
|
(u32) (bytes >> 32))) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-02-19 16:22:25 +01:00
|
|
|
if (data.bytes_64bit)
|
|
|
|
bytes = data.tx_bytes;
|
|
|
|
else
|
|
|
|
bytes = ((u64) sta->last_tx_bytes_hi << 32) |
|
|
|
|
sta->last_tx_bytes_lo;
|
2008-02-28 02:34:43 +01:00
|
|
|
if (!radius_msg_add_attr_int32(msg,
|
|
|
|
RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
|
2016-02-19 16:22:25 +01:00
|
|
|
(u32) bytes)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-02-19 16:22:25 +01:00
|
|
|
if (!radius_msg_add_attr_int32(msg,
|
|
|
|
RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
|
|
|
|
(u32) (bytes >> 32))) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
|
2008-02-28 02:34:43 +01:00
|
|
|
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)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
|
2008-02-28 02:34:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-04-01 16:55:20 +02:00
|
|
|
if (radius_client_send(hapd->radius, msg,
|
|
|
|
stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
|
|
|
|
sta->addr) < 0)
|
|
|
|
goto fail;
|
2008-02-28 02:34:43 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
radius_msg_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 15:33:00 +01:00
|
|
|
/**
|
|
|
|
* accounting_sta_interim - Send a interim STA accounting report
|
|
|
|
* @hapd: hostapd BSS data
|
|
|
|
* @sta: The station
|
|
|
|
*/
|
2012-08-05 18:40:49 +02:00
|
|
|
static void accounting_sta_interim(struct hostapd_data *hapd,
|
|
|
|
struct sta_info *sta)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
|
|
|
if (sta->acct_session_started)
|
|
|
|
accounting_sta_report(hapd, sta, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 15:33:00 +01:00
|
|
|
/**
|
|
|
|
* accounting_sta_stop - Stop STA accounting
|
|
|
|
* @hapd: hostapd BSS data
|
|
|
|
* @sta: The station
|
|
|
|
*/
|
2008-02-28 02:34:43 +01:00
|
|
|
void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
|
|
|
|
{
|
|
|
|
if (sta->acct_session_started) {
|
|
|
|
accounting_sta_report(hapd, sta, 1);
|
|
|
|
eloop_cancel_timeout(accounting_interim_update, hapd, sta);
|
2008-11-23 10:12:17 +01:00
|
|
|
hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
|
|
|
|
HOSTAPD_LEVEL_INFO,
|
2016-02-13 21:29:47 +01:00
|
|
|
"stopped accounting session %016llX",
|
|
|
|
(unsigned long long) sta->acct_session_id);
|
2008-02-28 02:34:43 +01:00
|
|
|
sta->acct_session_started = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-24 12:37:46 +01:00
|
|
|
int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
2016-02-06 15:27:52 +01:00
|
|
|
return radius_gen_session_id((u8 *) &sta->acct_session_id,
|
|
|
|
sizeof(sta->acct_session_id));
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 15:33:00 +01:00
|
|
|
/**
|
|
|
|
* accounting_receive - Process the RADIUS frames from Accounting Server
|
|
|
|
* @msg: RADIUS response message
|
|
|
|
* @req: RADIUS request message
|
|
|
|
* @shared_secret: RADIUS shared secret
|
|
|
|
* @shared_secret_len: Length of shared_secret in octets
|
|
|
|
* @data: Context data (struct hostapd_data *)
|
|
|
|
* Returns: Processing status
|
|
|
|
*/
|
2008-02-28 02:34:43 +01:00
|
|
|
static RadiusRxResult
|
|
|
|
accounting_receive(struct radius_msg *msg, struct radius_msg *req,
|
2009-01-08 15:41:47 +01:00
|
|
|
const u8 *shared_secret, size_t shared_secret_len,
|
|
|
|
void *data)
|
2008-02-28 02:34:43 +01:00
|
|
|
{
|
2009-12-19 16:26:57 +01:00
|
|
|
if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Unknown RADIUS message code");
|
2008-02-28 02:34:43 +01:00
|
|
|
return RADIUS_RX_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
|
2013-11-02 11:51:30 +01:00
|
|
|
wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
|
2008-02-28 02:34:43 +01:00
|
|
|
return RADIUS_RX_INVALID_AUTHENTICATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RADIUS_RX_PROCESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void accounting_report_state(struct hostapd_data *hapd, int on)
|
|
|
|
{
|
|
|
|
struct radius_msg *msg;
|
|
|
|
|
|
|
|
if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Inform RADIUS server that accounting will start/stop so that the
|
|
|
|
* server can close old accounting sessions. */
|
|
|
|
msg = accounting_msg(hapd, NULL,
|
|
|
|
on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
|
|
|
|
RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
|
2016-01-25 11:43:33 +01:00
|
|
|
if (hapd->acct_session_id) {
|
|
|
|
char buf[20];
|
|
|
|
|
2016-02-13 21:29:47 +01:00
|
|
|
os_snprintf(buf, sizeof(buf), "%016llX",
|
|
|
|
(unsigned long long) hapd->acct_session_id);
|
2016-01-25 11:43:33 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2012-04-01 16:55:20 +02:00
|
|
|
if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
|
|
|
|
radius_msg_free(msg);
|
2008-02-28 02:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-29 10:44:43 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 15:33:00 +01:00
|
|
|
/**
|
|
|
|
* accounting_init: Initialize accounting
|
|
|
|
* @hapd: hostapd BSS data
|
|
|
|
* Returns: 0 on success, -1 on failure
|
|
|
|
*/
|
2008-02-28 02:34:43 +01:00
|
|
|
int accounting_init(struct hostapd_data *hapd)
|
|
|
|
{
|
2016-02-06 15:27:52 +01:00
|
|
|
if (radius_gen_session_id((u8 *) &hapd->acct_session_id,
|
|
|
|
sizeof(hapd->acct_session_id)) < 0)
|
2016-01-25 11:43:33 +01:00
|
|
|
return -1;
|
|
|
|
|
2008-02-28 02:34:43 +01:00
|
|
|
if (radius_client_register(hapd->radius, RADIUS_ACCT,
|
|
|
|
accounting_receive, hapd))
|
|
|
|
return -1;
|
2016-02-29 10:44:43 +01:00
|
|
|
radius_client_set_interim_error_cb(hapd->radius,
|
|
|
|
accounting_interim_error_cb, hapd);
|
2008-02-28 02:34:43 +01:00
|
|
|
|
|
|
|
accounting_report_state(hapd, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-08 15:33:00 +01:00
|
|
|
/**
|
2015-06-09 21:03:53 +02:00
|
|
|
* accounting_deinit: Deinitialize accounting
|
2009-01-08 15:33:00 +01:00
|
|
|
* @hapd: hostapd BSS data
|
|
|
|
*/
|
2008-02-28 02:34:43 +01:00
|
|
|
void accounting_deinit(struct hostapd_data *hapd)
|
|
|
|
{
|
|
|
|
accounting_report_state(hapd, 0);
|
|
|
|
}
|