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_make_authenticator(msg) < 0) {
54 wpa_printf(MSG_INFO, "Could not make Request Authenticator");
58 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
60 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
65 if (!hostapd_config_get_radius_attr(
66 hapd->conf->radius_acct_req_attr,
67 RADIUS_ATTR_ACCT_AUTHENTIC) &&
68 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
69 hapd->conf->ieee802_1x ?
70 RADIUS_ACCT_AUTHENTIC_RADIUS :
71 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
72 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
76 /* Use 802.1X identity if available */
77 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
79 /* Use RADIUS ACL identity if 802.1X provides no identity */
80 if (!val && sta->identity) {
81 val = (u8 *) sta->identity;
82 len = os_strlen(sta->identity);
85 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
88 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
94 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
96 wpa_printf(MSG_INFO, "Could not add User-Name");
101 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
107 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
112 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
114 wpa_printf(MSG_INFO, "Could not add Class");
119 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
121 !radius_msg_add_attr(msg,
122 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
123 wpabuf_head(b), wpabuf_len(b))) {
124 wpa_printf(MSG_ERROR, "Could not add CUI");
128 if (!b && sta->radius_cui &&
129 !radius_msg_add_attr(msg,
130 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
131 (u8 *) sta->radius_cui,
132 os_strlen(sta->radius_cui))) {
133 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
138 !radius_msg_add_attr_int32(msg,
139 RADIUS_ATTR_FRAMED_IP_ADDRESS,
140 be_to_host32(sta->ipaddr))) {
141 wpa_printf(MSG_ERROR,
142 "Could not add Framed-IP-Address");
148 if (now.sec > 1000000000 &&
149 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
151 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
158 radius_msg_free(msg);
163 static int accounting_sta_update_stats(struct hostapd_data *hapd,
164 struct sta_info *sta,
165 struct hostap_sta_driver_data *data)
167 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
170 if (sta->last_rx_bytes > data->rx_bytes)
171 sta->acct_input_gigawords++;
172 if (sta->last_tx_bytes > data->tx_bytes)
173 sta->acct_output_gigawords++;
174 sta->last_rx_bytes = data->rx_bytes;
175 sta->last_tx_bytes = data->tx_bytes;
177 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
178 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
179 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
180 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
181 sta->last_rx_bytes, sta->acct_input_gigawords,
182 sta->last_tx_bytes, sta->acct_output_gigawords);
188 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
190 struct hostapd_data *hapd = eloop_ctx;
191 struct sta_info *sta = timeout_ctx;
194 if (sta->acct_interim_interval) {
195 accounting_sta_interim(hapd, sta);
196 interval = sta->acct_interim_interval;
198 struct hostap_sta_driver_data data;
199 accounting_sta_update_stats(hapd, sta, &data);
200 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
203 eloop_register_timeout(interval, 0, accounting_interim_update,
209 * accounting_sta_start - Start STA accounting
210 * @hapd: hostapd BSS data
213 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
215 struct radius_msg *msg;
218 if (sta->acct_session_started)
221 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
223 "starting accounting session %016lX",
224 (long unsigned int) sta->acct_session_id);
226 os_get_reltime(&sta->acct_session_start);
227 sta->last_rx_bytes = sta->last_tx_bytes = 0;
228 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
229 hostapd_drv_sta_clear_stats(hapd, sta->addr);
231 if (!hapd->conf->radius->acct_server)
234 if (sta->acct_interim_interval)
235 interval = sta->acct_interim_interval;
237 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
238 eloop_register_timeout(interval, 0, accounting_interim_update,
241 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
243 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
244 radius_msg_free(msg);
246 sta->acct_session_started = 1;
250 static void accounting_sta_report(struct hostapd_data *hapd,
251 struct sta_info *sta, int stop)
253 struct radius_msg *msg;
254 int cause = sta->acct_terminate_cause;
255 struct hostap_sta_driver_data data;
256 struct os_reltime now_r, diff;
259 if (!hapd->conf->radius->acct_server)
262 msg = accounting_msg(hapd, sta,
263 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
264 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
266 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
270 os_get_reltime(&now_r);
271 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
272 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
274 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
278 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
279 if (!radius_msg_add_attr_int32(msg,
280 RADIUS_ATTR_ACCT_INPUT_PACKETS,
282 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
285 if (!radius_msg_add_attr_int32(msg,
286 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
288 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
291 if (!radius_msg_add_attr_int32(msg,
292 RADIUS_ATTR_ACCT_INPUT_OCTETS,
294 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
297 gigawords = sta->acct_input_gigawords;
299 gigawords += data.rx_bytes >> 32;
302 !radius_msg_add_attr_int32(
303 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
305 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
308 if (!radius_msg_add_attr_int32(msg,
309 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
311 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
314 gigawords = sta->acct_output_gigawords;
316 gigawords += data.tx_bytes >> 32;
319 !radius_msg_add_attr_int32(
320 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
322 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
327 if (eloop_terminated())
328 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
331 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
333 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
337 if (radius_client_send(hapd->radius, msg,
338 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
344 radius_msg_free(msg);
349 * accounting_sta_interim - Send a interim STA accounting report
350 * @hapd: hostapd BSS data
353 static void accounting_sta_interim(struct hostapd_data *hapd,
354 struct sta_info *sta)
356 if (sta->acct_session_started)
357 accounting_sta_report(hapd, sta, 0);
362 * accounting_sta_stop - Stop STA accounting
363 * @hapd: hostapd BSS data
366 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
368 if (sta->acct_session_started) {
369 accounting_sta_report(hapd, sta, 1);
370 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
371 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
373 "stopped accounting session %016lX",
374 (long unsigned int) sta->acct_session_id);
375 sta->acct_session_started = 0;
380 int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
383 * Acct-Session-Id should be globally and temporarily unique.
384 * A high quality random number is required therefore.
385 * This could be be improved by switching to a GUID.
387 return os_get_random((u8 *) &sta->acct_session_id,
388 sizeof(sta->acct_session_id));
393 * accounting_receive - Process the RADIUS frames from Accounting Server
394 * @msg: RADIUS response message
395 * @req: RADIUS request message
396 * @shared_secret: RADIUS shared secret
397 * @shared_secret_len: Length of shared_secret in octets
398 * @data: Context data (struct hostapd_data *)
399 * Returns: Processing status
401 static RadiusRxResult
402 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
403 const u8 *shared_secret, size_t shared_secret_len,
406 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
407 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
408 return RADIUS_RX_UNKNOWN;
411 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
412 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
413 return RADIUS_RX_INVALID_AUTHENTICATOR;
416 return RADIUS_RX_PROCESSED;
420 static void accounting_report_state(struct hostapd_data *hapd, int on)
422 struct radius_msg *msg;
424 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
427 /* Inform RADIUS server that accounting will start/stop so that the
428 * server can close old accounting sessions. */
429 msg = accounting_msg(hapd, NULL,
430 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
431 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
435 if (hapd->acct_session_id) {
438 os_snprintf(buf, sizeof(buf), "%016lX",
439 (long unsigned int) hapd->acct_session_id);
440 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
441 (u8 *) buf, os_strlen(buf)))
442 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
445 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
446 radius_msg_free(msg);
451 * accounting_init: Initialize accounting
452 * @hapd: hostapd BSS data
453 * Returns: 0 on success, -1 on failure
455 int accounting_init(struct hostapd_data *hapd)
458 * Acct-Session-Id should be globally and temporarily unique.
459 * A high quality random number is required therefore.
460 * This could be be improved by switching to a GUID.
462 if (os_get_random((u8 *) &hapd->acct_session_id,
463 sizeof(hapd->acct_session_id)) < 0)
466 if (radius_client_register(hapd->radius, RADIUS_ACCT,
467 accounting_receive, hapd))
470 accounting_report_state(hapd, 1);
477 * accounting_deinit: Deinitialize accounting
478 * @hapd: hostapd BSS data
480 void accounting_deinit(struct hostapd_data *hapd)
482 accounting_report_state(hapd, 0);