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");
79 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
80 RADIUS_ATTR_ACCT_AUTHENTIC) &&
81 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
82 hapd->conf->ieee802_1x ?
83 RADIUS_ACCT_AUTHENTIC_RADIUS :
84 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
85 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
90 /* Use 802.1X identity if available */
91 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
93 /* Use RADIUS ACL identity if 802.1X provides no identity */
94 if (!val && sta->identity) {
95 val = (u8 *) sta->identity;
96 len = os_strlen(sta->identity);
99 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
102 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
105 len = os_strlen(buf);
108 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
110 wpa_printf(MSG_INFO, "Could not add User-Name");
115 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
121 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
126 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
128 wpa_printf(MSG_INFO, "Could not add Class");
133 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
135 !radius_msg_add_attr(msg,
136 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
137 wpabuf_head(b), wpabuf_len(b))) {
138 wpa_printf(MSG_ERROR, "Could not add CUI");
142 if (!b && sta->radius_cui &&
143 !radius_msg_add_attr(msg,
144 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
145 (u8 *) sta->radius_cui,
146 os_strlen(sta->radius_cui))) {
147 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
152 !radius_msg_add_attr_int32(msg,
153 RADIUS_ATTR_FRAMED_IP_ADDRESS,
154 be_to_host32(sta->ipaddr))) {
155 wpa_printf(MSG_ERROR,
156 "Could not add Framed-IP-Address");
164 radius_msg_free(msg);
169 static int accounting_sta_update_stats(struct hostapd_data *hapd,
170 struct sta_info *sta,
171 struct hostap_sta_driver_data *data)
173 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
176 if (sta->last_rx_bytes > data->rx_bytes)
177 sta->acct_input_gigawords++;
178 if (sta->last_tx_bytes > data->tx_bytes)
179 sta->acct_output_gigawords++;
180 sta->last_rx_bytes = data->rx_bytes;
181 sta->last_tx_bytes = data->tx_bytes;
183 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
184 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
185 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
186 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
187 sta->last_rx_bytes, sta->acct_input_gigawords,
188 sta->last_tx_bytes, sta->acct_output_gigawords);
194 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
196 struct hostapd_data *hapd = eloop_ctx;
197 struct sta_info *sta = timeout_ctx;
200 if (sta->acct_interim_interval) {
201 accounting_sta_interim(hapd, sta);
202 interval = sta->acct_interim_interval;
204 struct hostap_sta_driver_data data;
205 accounting_sta_update_stats(hapd, sta, &data);
206 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
209 eloop_register_timeout(interval, 0, accounting_interim_update,
215 * accounting_sta_start - Start STA accounting
216 * @hapd: hostapd BSS data
219 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
221 struct radius_msg *msg;
224 if (sta->acct_session_started)
227 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
229 "starting accounting session %08X-%08X",
230 sta->acct_session_id_hi, sta->acct_session_id_lo);
232 os_get_reltime(&sta->acct_session_start);
233 sta->last_rx_bytes = sta->last_tx_bytes = 0;
234 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
235 hostapd_drv_sta_clear_stats(hapd, sta->addr);
237 if (!hapd->conf->radius->acct_server)
240 if (sta->acct_interim_interval)
241 interval = sta->acct_interim_interval;
243 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
244 eloop_register_timeout(interval, 0, accounting_interim_update,
247 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
249 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
250 radius_msg_free(msg);
252 sta->acct_session_started = 1;
256 static void accounting_sta_report(struct hostapd_data *hapd,
257 struct sta_info *sta, int stop)
259 struct radius_msg *msg;
260 int cause = sta->acct_terminate_cause;
261 struct hostap_sta_driver_data data;
262 struct os_reltime now_r, diff;
266 if (!hapd->conf->radius->acct_server)
269 msg = accounting_msg(hapd, sta,
270 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
271 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
273 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
277 os_get_reltime(&now_r);
279 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
280 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
282 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
286 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
287 if (!radius_msg_add_attr_int32(msg,
288 RADIUS_ATTR_ACCT_INPUT_PACKETS,
290 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
293 if (!radius_msg_add_attr_int32(msg,
294 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
296 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
299 if (!radius_msg_add_attr_int32(msg,
300 RADIUS_ATTR_ACCT_INPUT_OCTETS,
302 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
305 gigawords = sta->acct_input_gigawords;
307 gigawords += data.rx_bytes >> 32;
310 !radius_msg_add_attr_int32(
311 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
313 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
316 if (!radius_msg_add_attr_int32(msg,
317 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
319 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
322 gigawords = sta->acct_output_gigawords;
324 gigawords += data.tx_bytes >> 32;
327 !radius_msg_add_attr_int32(
328 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
330 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
335 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
337 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
341 if (eloop_terminated())
342 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
345 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
347 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
351 if (radius_client_send(hapd->radius, msg,
352 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
358 radius_msg_free(msg);
363 * accounting_sta_interim - Send a interim STA accounting report
364 * @hapd: hostapd BSS data
367 static void accounting_sta_interim(struct hostapd_data *hapd,
368 struct sta_info *sta)
370 if (sta->acct_session_started)
371 accounting_sta_report(hapd, sta, 0);
376 * accounting_sta_stop - Stop STA accounting
377 * @hapd: hostapd BSS data
380 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
382 if (sta->acct_session_started) {
383 accounting_sta_report(hapd, sta, 1);
384 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
385 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
387 "stopped accounting session %08X-%08X",
388 sta->acct_session_id_hi,
389 sta->acct_session_id_lo);
390 sta->acct_session_started = 0;
395 void accounting_sta_get_id(struct hostapd_data *hapd,
396 struct sta_info *sta)
398 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
399 if (hapd->acct_session_id_lo == 0) {
400 hapd->acct_session_id_hi++;
402 sta->acct_session_id_hi = hapd->acct_session_id_hi;
407 * accounting_receive - Process the RADIUS frames from Accounting Server
408 * @msg: RADIUS response message
409 * @req: RADIUS request message
410 * @shared_secret: RADIUS shared secret
411 * @shared_secret_len: Length of shared_secret in octets
412 * @data: Context data (struct hostapd_data *)
413 * Returns: Processing status
415 static RadiusRxResult
416 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
417 const u8 *shared_secret, size_t shared_secret_len,
420 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
421 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
422 return RADIUS_RX_UNKNOWN;
425 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
426 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
427 return RADIUS_RX_INVALID_AUTHENTICATOR;
430 return RADIUS_RX_PROCESSED;
434 static void accounting_report_state(struct hostapd_data *hapd, int on)
436 struct radius_msg *msg;
438 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
441 /* Inform RADIUS server that accounting will start/stop so that the
442 * server can close old accounting sessions. */
443 msg = accounting_msg(hapd, NULL,
444 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
445 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
449 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
450 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
452 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
453 radius_msg_free(msg);
457 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
458 radius_msg_free(msg);
463 * accounting_init: Initialize accounting
464 * @hapd: hostapd BSS data
465 * Returns: 0 on success, -1 on failure
467 int accounting_init(struct hostapd_data *hapd)
471 /* Acct-Session-Id should be unique over reboots. Using a random number
472 * is preferred. If that is not available, take the current time. Mix
473 * in microseconds to make this more likely to be unique. */
475 if (os_get_random((u8 *) &hapd->acct_session_id_hi,
476 sizeof(hapd->acct_session_id_hi)) < 0)
477 hapd->acct_session_id_hi = now.sec;
478 hapd->acct_session_id_hi ^= now.usec;
480 if (radius_client_register(hapd->radius, RADIUS_ACCT,
481 accounting_receive, hapd))
484 accounting_report_state(hapd, 1);
491 * accounting_deinit: Deinitialize accounting
492 * @hapd: hostapd BSS data
494 void accounting_deinit(struct hostapd_data *hapd)
496 accounting_report_state(hapd, 0);