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;
45 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
46 radius_client_get_id(hapd->radius));
48 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
53 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
55 if ((hapd->conf->wpa & 2) &&
56 !hapd->conf->disable_pmksa_caching &&
57 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id_hi) {
58 os_snprintf(buf, sizeof(buf), "%08X+%08X",
59 sta->eapol_sm->acct_multi_session_id_hi,
60 sta->eapol_sm->acct_multi_session_id_lo);
61 if (!radius_msg_add_attr(
62 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
63 (u8 *) buf, os_strlen(buf))) {
65 "Could not add Acct-Multi-Session-Id");
70 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
73 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
75 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
80 if (!hostapd_config_get_radius_attr(
81 hapd->conf->radius_acct_req_attr,
82 RADIUS_ATTR_ACCT_AUTHENTIC) &&
83 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
84 hapd->conf->ieee802_1x ?
85 RADIUS_ACCT_AUTHENTIC_RADIUS :
86 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
87 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
91 /* Use 802.1X identity if available */
92 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
94 /* Use RADIUS ACL identity if 802.1X provides no identity */
95 if (!val && sta->identity) {
96 val = (u8 *) sta->identity;
97 len = os_strlen(sta->identity);
100 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
103 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
106 len = os_strlen(buf);
109 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
111 wpa_printf(MSG_INFO, "Could not add User-Name");
116 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
122 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
127 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
129 wpa_printf(MSG_INFO, "Could not add Class");
134 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
136 !radius_msg_add_attr(msg,
137 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
138 wpabuf_head(b), wpabuf_len(b))) {
139 wpa_printf(MSG_ERROR, "Could not add CUI");
143 if (!b && sta->radius_cui &&
144 !radius_msg_add_attr(msg,
145 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
146 (u8 *) sta->radius_cui,
147 os_strlen(sta->radius_cui))) {
148 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
153 !radius_msg_add_attr_int32(msg,
154 RADIUS_ATTR_FRAMED_IP_ADDRESS,
155 be_to_host32(sta->ipaddr))) {
156 wpa_printf(MSG_ERROR,
157 "Could not add Framed-IP-Address");
165 radius_msg_free(msg);
170 static int accounting_sta_update_stats(struct hostapd_data *hapd,
171 struct sta_info *sta,
172 struct hostap_sta_driver_data *data)
174 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
177 if (sta->last_rx_bytes > data->rx_bytes)
178 sta->acct_input_gigawords++;
179 if (sta->last_tx_bytes > data->tx_bytes)
180 sta->acct_output_gigawords++;
181 sta->last_rx_bytes = data->rx_bytes;
182 sta->last_tx_bytes = data->tx_bytes;
184 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
185 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
186 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
187 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
188 sta->last_rx_bytes, sta->acct_input_gigawords,
189 sta->last_tx_bytes, sta->acct_output_gigawords);
195 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
197 struct hostapd_data *hapd = eloop_ctx;
198 struct sta_info *sta = timeout_ctx;
201 if (sta->acct_interim_interval) {
202 accounting_sta_interim(hapd, sta);
203 interval = sta->acct_interim_interval;
205 struct hostap_sta_driver_data data;
206 accounting_sta_update_stats(hapd, sta, &data);
207 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
210 eloop_register_timeout(interval, 0, accounting_interim_update,
216 * accounting_sta_start - Start STA accounting
217 * @hapd: hostapd BSS data
220 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
222 struct radius_msg *msg;
225 if (sta->acct_session_started)
228 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
230 "starting accounting session %08X-%08X",
231 sta->acct_session_id_hi, sta->acct_session_id_lo);
233 os_get_reltime(&sta->acct_session_start);
234 sta->last_rx_bytes = sta->last_tx_bytes = 0;
235 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
236 hostapd_drv_sta_clear_stats(hapd, sta->addr);
238 if (!hapd->conf->radius->acct_server)
241 if (sta->acct_interim_interval)
242 interval = sta->acct_interim_interval;
244 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
245 eloop_register_timeout(interval, 0, accounting_interim_update,
248 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
250 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
251 radius_msg_free(msg);
253 sta->acct_session_started = 1;
257 static void accounting_sta_report(struct hostapd_data *hapd,
258 struct sta_info *sta, int stop)
260 struct radius_msg *msg;
261 int cause = sta->acct_terminate_cause;
262 struct hostap_sta_driver_data data;
263 struct os_reltime now_r, diff;
267 if (!hapd->conf->radius->acct_server)
270 msg = accounting_msg(hapd, sta,
271 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
272 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
274 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
278 os_get_reltime(&now_r);
280 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
281 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
283 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
287 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
288 if (!radius_msg_add_attr_int32(msg,
289 RADIUS_ATTR_ACCT_INPUT_PACKETS,
291 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
294 if (!radius_msg_add_attr_int32(msg,
295 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
297 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
300 if (!radius_msg_add_attr_int32(msg,
301 RADIUS_ATTR_ACCT_INPUT_OCTETS,
303 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
306 gigawords = sta->acct_input_gigawords;
308 gigawords += data.rx_bytes >> 32;
311 !radius_msg_add_attr_int32(
312 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
314 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
317 if (!radius_msg_add_attr_int32(msg,
318 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
320 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
323 gigawords = sta->acct_output_gigawords;
325 gigawords += data.tx_bytes >> 32;
328 !radius_msg_add_attr_int32(
329 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
331 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
336 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
338 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
342 if (eloop_terminated())
343 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
346 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
348 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
352 if (radius_client_send(hapd->radius, msg,
353 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
359 radius_msg_free(msg);
364 * accounting_sta_interim - Send a interim STA accounting report
365 * @hapd: hostapd BSS data
368 static void accounting_sta_interim(struct hostapd_data *hapd,
369 struct sta_info *sta)
371 if (sta->acct_session_started)
372 accounting_sta_report(hapd, sta, 0);
377 * accounting_sta_stop - Stop STA accounting
378 * @hapd: hostapd BSS data
381 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
383 if (sta->acct_session_started) {
384 accounting_sta_report(hapd, sta, 1);
385 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
386 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
388 "stopped accounting session %08X-%08X",
389 sta->acct_session_id_hi,
390 sta->acct_session_id_lo);
391 sta->acct_session_started = 0;
396 void accounting_sta_get_id(struct hostapd_data *hapd,
397 struct sta_info *sta)
399 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
400 if (hapd->acct_session_id_lo == 0) {
401 hapd->acct_session_id_hi++;
403 sta->acct_session_id_hi = hapd->acct_session_id_hi;
408 * accounting_receive - Process the RADIUS frames from Accounting Server
409 * @msg: RADIUS response message
410 * @req: RADIUS request message
411 * @shared_secret: RADIUS shared secret
412 * @shared_secret_len: Length of shared_secret in octets
413 * @data: Context data (struct hostapd_data *)
414 * Returns: Processing status
416 static RadiusRxResult
417 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
418 const u8 *shared_secret, size_t shared_secret_len,
421 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
422 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
423 return RADIUS_RX_UNKNOWN;
426 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
427 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
428 return RADIUS_RX_INVALID_AUTHENTICATOR;
431 return RADIUS_RX_PROCESSED;
435 static void accounting_report_state(struct hostapd_data *hapd, int on)
437 struct radius_msg *msg;
439 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
442 /* Inform RADIUS server that accounting will start/stop so that the
443 * server can close old accounting sessions. */
444 msg = accounting_msg(hapd, NULL,
445 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
446 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
450 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
451 radius_msg_free(msg);
456 * accounting_init: Initialize accounting
457 * @hapd: hostapd BSS data
458 * Returns: 0 on success, -1 on failure
460 int accounting_init(struct hostapd_data *hapd)
464 /* Acct-Session-Id should be unique over reboots. Using a random number
465 * is preferred. If that is not available, take the current time. Mix
466 * in microseconds to make this more likely to be unique. */
468 if (os_get_random((u8 *) &hapd->acct_session_id_hi,
469 sizeof(hapd->acct_session_id_hi)) < 0)
470 hapd->acct_session_id_hi = now.sec;
471 hapd->acct_session_id_hi ^= now.usec;
473 if (radius_client_register(hapd->radius, RADIUS_ACCT,
474 accounting_receive, hapd))
477 accounting_report_state(hapd, 1);
484 * accounting_deinit: Deinitialize accounting
485 * @hapd: hostapd BSS data
487 void accounting_deinit(struct hostapd_data *hapd)
489 accounting_report_state(hapd, 0);