2 * hostapd / RADIUS Accounting
3 * Copyright (c) 2002-2009, 2012-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "eapol_auth/eapol_auth_sm.h"
14 #include "eapol_auth/eapol_auth_sm_i.h"
15 #include "radius/radius.h"
16 #include "radius/radius_client.h"
18 #include "ieee802_1x.h"
19 #include "ap_config.h"
21 #include "ap_drv_ops.h"
22 #include "accounting.h"
25 /* Default interval in seconds for polling TX/RX octets from the driver if
26 * STA is not using interim accounting. This detects wrap arounds for
27 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
28 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
30 static void accounting_sta_interim(struct hostapd_data *hapd,
31 struct sta_info *sta);
34 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
38 struct radius_msg *msg;
46 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
47 radius_client_get_id(hapd->radius));
49 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
53 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
55 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
60 if (!hostapd_config_get_radius_attr(
61 hapd->conf->radius_acct_req_attr,
62 RADIUS_ATTR_ACCT_AUTHENTIC) &&
63 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
64 hapd->conf->ieee802_1x ?
65 RADIUS_ACCT_AUTHENTIC_RADIUS :
66 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
67 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
71 /* Use 802.1X identity if available */
72 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
74 /* Use RADIUS ACL identity if 802.1X provides no identity */
75 if (!val && sta->identity) {
76 val = (u8 *) sta->identity;
77 len = os_strlen(sta->identity);
80 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
83 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
89 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
91 wpa_printf(MSG_INFO, "Could not add User-Name");
96 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
102 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
107 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
109 wpa_printf(MSG_INFO, "Could not add Class");
114 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
116 !radius_msg_add_attr(msg,
117 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
118 wpabuf_head(b), wpabuf_len(b))) {
119 wpa_printf(MSG_ERROR, "Could not add CUI");
123 if (!b && sta->radius_cui &&
124 !radius_msg_add_attr(msg,
125 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
126 (u8 *) sta->radius_cui,
127 os_strlen(sta->radius_cui))) {
128 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
133 !radius_msg_add_attr_int32(msg,
134 RADIUS_ATTR_FRAMED_IP_ADDRESS,
135 be_to_host32(sta->ipaddr))) {
136 wpa_printf(MSG_ERROR,
137 "Could not add Framed-IP-Address");
143 if (now.sec > 1000000000 &&
144 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
146 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
151 * Add Acct-Delay-Time with zero value for the first transmission. This
152 * will be updated within radius_client.c when retransmitting the frame.
154 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_DELAY_TIME, 0)) {
155 wpa_printf(MSG_INFO, "Could not add Acct-Delay-Time");
162 radius_msg_free(msg);
167 static int accounting_sta_update_stats(struct hostapd_data *hapd,
168 struct sta_info *sta,
169 struct hostap_sta_driver_data *data)
171 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
174 if (!data->bytes_64bit) {
175 /* Extend 32-bit counters from the driver to 64-bit counters */
176 if (sta->last_rx_bytes_lo > data->rx_bytes)
177 sta->last_rx_bytes_hi++;
178 sta->last_rx_bytes_lo = data->rx_bytes;
180 if (sta->last_tx_bytes_lo > data->tx_bytes)
181 sta->last_tx_bytes_hi++;
182 sta->last_tx_bytes_lo = data->tx_bytes;
185 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
187 "updated TX/RX stats: rx_bytes=%llu [%u:%u] tx_bytes=%llu [%u:%u] bytes_64bit=%d",
188 data->rx_bytes, sta->last_rx_bytes_hi,
189 sta->last_rx_bytes_lo,
190 data->tx_bytes, sta->last_tx_bytes_hi,
191 sta->last_tx_bytes_lo,
198 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
200 struct hostapd_data *hapd = eloop_ctx;
201 struct sta_info *sta = timeout_ctx;
204 if (sta->acct_interim_interval) {
205 accounting_sta_interim(hapd, sta);
206 interval = sta->acct_interim_interval;
208 struct hostap_sta_driver_data data;
209 accounting_sta_update_stats(hapd, sta, &data);
210 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
213 eloop_register_timeout(interval, 0, accounting_interim_update,
219 * accounting_sta_start - Start STA accounting
220 * @hapd: hostapd BSS data
223 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
225 struct radius_msg *msg;
228 if (sta->acct_session_started)
231 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
233 "starting accounting session %016llX",
234 (unsigned long long) sta->acct_session_id);
236 os_get_reltime(&sta->acct_session_start);
237 sta->last_rx_bytes_hi = 0;
238 sta->last_rx_bytes_lo = 0;
239 sta->last_tx_bytes_hi = 0;
240 sta->last_tx_bytes_lo = 0;
241 hostapd_drv_sta_clear_stats(hapd, sta->addr);
243 if (!hapd->conf->radius->acct_server)
246 if (sta->acct_interim_interval)
247 interval = sta->acct_interim_interval;
249 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
250 eloop_register_timeout(interval, 0, accounting_interim_update,
253 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
255 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
256 radius_msg_free(msg);
258 sta->acct_session_started = 1;
262 static void accounting_sta_report(struct hostapd_data *hapd,
263 struct sta_info *sta, int stop)
265 struct radius_msg *msg;
266 int cause = sta->acct_terminate_cause;
267 struct hostap_sta_driver_data data;
268 struct os_reltime now_r, diff;
271 if (!hapd->conf->radius->acct_server)
274 msg = accounting_msg(hapd, sta,
275 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
276 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
278 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
282 os_get_reltime(&now_r);
283 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
284 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
286 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
290 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
291 if (!radius_msg_add_attr_int32(msg,
292 RADIUS_ATTR_ACCT_INPUT_PACKETS,
294 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
297 if (!radius_msg_add_attr_int32(msg,
298 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
300 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
303 if (data.bytes_64bit)
304 bytes = data.rx_bytes;
306 bytes = ((u64) sta->last_rx_bytes_hi << 32) |
307 sta->last_rx_bytes_lo;
308 if (!radius_msg_add_attr_int32(msg,
309 RADIUS_ATTR_ACCT_INPUT_OCTETS,
311 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
314 if (!radius_msg_add_attr_int32(msg,
315 RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
316 (u32) (bytes >> 32))) {
317 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
320 if (data.bytes_64bit)
321 bytes = data.tx_bytes;
323 bytes = ((u64) sta->last_tx_bytes_hi << 32) |
324 sta->last_tx_bytes_lo;
325 if (!radius_msg_add_attr_int32(msg,
326 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
328 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
331 if (!radius_msg_add_attr_int32(msg,
332 RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
333 (u32) (bytes >> 32))) {
334 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
339 if (eloop_terminated())
340 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
343 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
345 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
349 if (radius_client_send(hapd->radius, msg,
350 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
356 radius_msg_free(msg);
361 * accounting_sta_interim - Send a interim STA accounting report
362 * @hapd: hostapd BSS data
365 static void accounting_sta_interim(struct hostapd_data *hapd,
366 struct sta_info *sta)
368 if (sta->acct_session_started)
369 accounting_sta_report(hapd, sta, 0);
374 * accounting_sta_stop - Stop STA accounting
375 * @hapd: hostapd BSS data
378 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
380 if (sta->acct_session_started) {
381 accounting_sta_report(hapd, sta, 1);
382 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
383 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
385 "stopped accounting session %016llX",
386 (unsigned long long) sta->acct_session_id);
387 sta->acct_session_started = 0;
392 int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
394 return radius_gen_session_id((u8 *) &sta->acct_session_id,
395 sizeof(sta->acct_session_id));
400 * accounting_receive - Process the RADIUS frames from Accounting Server
401 * @msg: RADIUS response message
402 * @req: RADIUS request message
403 * @shared_secret: RADIUS shared secret
404 * @shared_secret_len: Length of shared_secret in octets
405 * @data: Context data (struct hostapd_data *)
406 * Returns: Processing status
408 static RadiusRxResult
409 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
410 const u8 *shared_secret, size_t shared_secret_len,
413 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
414 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
415 return RADIUS_RX_UNKNOWN;
418 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
419 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
420 return RADIUS_RX_INVALID_AUTHENTICATOR;
423 return RADIUS_RX_PROCESSED;
427 static void accounting_report_state(struct hostapd_data *hapd, int on)
429 struct radius_msg *msg;
431 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
434 /* Inform RADIUS server that accounting will start/stop so that the
435 * server can close old accounting sessions. */
436 msg = accounting_msg(hapd, NULL,
437 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
438 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
442 if (hapd->acct_session_id) {
445 os_snprintf(buf, sizeof(buf), "%016llX",
446 (unsigned long long) hapd->acct_session_id);
447 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
448 (u8 *) buf, os_strlen(buf)))
449 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
452 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
453 radius_msg_free(msg);
457 static void accounting_interim_error_cb(const u8 *addr, void *ctx)
459 struct hostapd_data *hapd = ctx;
460 struct sta_info *sta;
461 unsigned int i, wait_time;
464 sta = ap_get_sta(hapd, addr);
467 sta->acct_interim_errors++;
468 if (sta->acct_interim_errors > 10 /* RADIUS_CLIENT_MAX_RETRIES */) {
469 wpa_printf(MSG_DEBUG,
470 "Interim RADIUS accounting update failed for " MACSTR
471 " - too many errors, abandon this interim accounting update",
473 sta->acct_interim_errors = 0;
474 /* Next update will be tried after normal update interval */
479 * Use a shorter update interval as an improved retransmission mechanism
480 * for failed interim accounting updates. This allows the statistics to
481 * be updated for each retransmission.
483 * RADIUS client code has already waited RADIUS_CLIENT_FIRST_WAIT.
484 * Schedule the first retry attempt immediately and every following one
485 * with exponential backoff.
487 if (sta->acct_interim_errors == 1) {
490 wait_time = 3; /* RADIUS_CLIENT_FIRST_WAIT */
491 for (i = 1; i < sta->acct_interim_errors; i++)
494 res = eloop_deplete_timeout(wait_time, 0, accounting_interim_update,
497 wpa_printf(MSG_DEBUG,
498 "Interim RADIUS accounting update failed for " MACSTR
499 " (error count: %u) - schedule next update in %u seconds",
500 MAC2STR(addr), sta->acct_interim_errors, wait_time);
502 wpa_printf(MSG_DEBUG,
503 "Interim RADIUS accounting update failed for " MACSTR
504 " (error count: %u)", MAC2STR(addr),
505 sta->acct_interim_errors);
507 wpa_printf(MSG_DEBUG,
508 "Interim RADIUS accounting update failed for " MACSTR
509 " (error count: %u) - no timer found", MAC2STR(addr),
510 sta->acct_interim_errors);
515 * accounting_init: Initialize accounting
516 * @hapd: hostapd BSS data
517 * Returns: 0 on success, -1 on failure
519 int accounting_init(struct hostapd_data *hapd)
521 if (radius_gen_session_id((u8 *) &hapd->acct_session_id,
522 sizeof(hapd->acct_session_id)) < 0)
525 if (radius_client_register(hapd->radius, RADIUS_ACCT,
526 accounting_receive, hapd))
528 radius_client_set_interim_error_cb(hapd->radius,
529 accounting_interim_error_cb, hapd);
531 accounting_report_state(hapd, 1);
538 * accounting_deinit: Deinitialize accounting
539 * @hapd: hostapd BSS data
541 void accounting_deinit(struct hostapd_data *hapd)
543 accounting_report_state(hapd, 0);