2 * hostapd / RADIUS Accounting
3 * Copyright (c) 2002-2009, 2012, 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 "radius/radius.h"
14 #include "radius/radius_client.h"
16 #include "ieee802_1x.h"
17 #include "ap_config.h"
19 #include "ap_drv_ops.h"
20 #include "accounting.h"
23 /* Default interval in seconds for polling TX/RX octets from the driver if
24 * STA is not using interim accounting. This detects wrap arounds for
25 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
26 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
28 static void accounting_sta_interim(struct hostapd_data *hapd,
29 struct sta_info *sta);
32 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
36 struct radius_msg *msg;
43 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
44 radius_client_get_id(hapd->radius));
46 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
51 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
53 os_snprintf(buf, sizeof(buf), "%08X-%08X",
54 sta->acct_session_id_hi, sta->acct_session_id_lo);
55 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
56 (u8 *) buf, os_strlen(buf))) {
57 wpa_printf(MSG_INFO, "Could not add Acct-Session-Id");
61 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
64 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
66 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
70 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
71 RADIUS_ATTR_ACCT_AUTHENTIC) &&
72 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
73 hapd->conf->ieee802_1x ?
74 RADIUS_ACCT_AUTHENTIC_RADIUS :
75 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
76 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
81 /* Use 802.1X identity if available */
82 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
84 /* Use RADIUS ACL identity if 802.1X provides no identity */
85 if (!val && sta->identity) {
86 val = (u8 *) sta->identity;
87 len = os_strlen(sta->identity);
90 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
93 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
99 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
101 wpa_printf(MSG_INFO, "Could not add User-Name");
106 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
112 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
117 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
119 wpa_printf(MSG_INFO, "Could not add Class");
124 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
126 !radius_msg_add_attr(msg,
127 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
128 wpabuf_head(b), wpabuf_len(b))) {
129 wpa_printf(MSG_ERROR, "Could not add CUI");
133 if (!b && sta->radius_cui &&
134 !radius_msg_add_attr(msg,
135 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
136 (u8 *) sta->radius_cui,
137 os_strlen(sta->radius_cui))) {
138 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
146 radius_msg_free(msg);
151 static int accounting_sta_update_stats(struct hostapd_data *hapd,
152 struct sta_info *sta,
153 struct hostap_sta_driver_data *data)
155 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
158 if (sta->last_rx_bytes > data->rx_bytes)
159 sta->acct_input_gigawords++;
160 if (sta->last_tx_bytes > data->tx_bytes)
161 sta->acct_output_gigawords++;
162 sta->last_rx_bytes = data->rx_bytes;
163 sta->last_tx_bytes = data->tx_bytes;
165 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
166 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
167 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
168 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
169 sta->last_rx_bytes, sta->acct_input_gigawords,
170 sta->last_tx_bytes, sta->acct_output_gigawords);
176 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
178 struct hostapd_data *hapd = eloop_ctx;
179 struct sta_info *sta = timeout_ctx;
182 if (sta->acct_interim_interval) {
183 accounting_sta_interim(hapd, sta);
184 interval = sta->acct_interim_interval;
186 struct hostap_sta_driver_data data;
187 accounting_sta_update_stats(hapd, sta, &data);
188 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
191 eloop_register_timeout(interval, 0, accounting_interim_update,
197 * accounting_sta_start - Start STA accounting
198 * @hapd: hostapd BSS data
201 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
203 struct radius_msg *msg;
206 if (sta->acct_session_started)
209 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
211 "starting accounting session %08X-%08X",
212 sta->acct_session_id_hi, sta->acct_session_id_lo);
214 os_get_reltime(&sta->acct_session_start);
215 sta->last_rx_bytes = sta->last_tx_bytes = 0;
216 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
217 hostapd_drv_sta_clear_stats(hapd, sta->addr);
219 if (!hapd->conf->radius->acct_server)
222 if (sta->acct_interim_interval)
223 interval = sta->acct_interim_interval;
225 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
226 eloop_register_timeout(interval, 0, accounting_interim_update,
229 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
231 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
232 radius_msg_free(msg);
234 sta->acct_session_started = 1;
238 static void accounting_sta_report(struct hostapd_data *hapd,
239 struct sta_info *sta, int stop)
241 struct radius_msg *msg;
242 int cause = sta->acct_terminate_cause;
243 struct hostap_sta_driver_data data;
244 struct os_reltime now_r, diff;
248 if (!hapd->conf->radius->acct_server)
251 msg = accounting_msg(hapd, sta,
252 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
253 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
255 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
259 os_get_reltime(&now_r);
261 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
262 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
264 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
268 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
269 if (!radius_msg_add_attr_int32(msg,
270 RADIUS_ATTR_ACCT_INPUT_PACKETS,
272 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
275 if (!radius_msg_add_attr_int32(msg,
276 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
278 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
281 if (!radius_msg_add_attr_int32(msg,
282 RADIUS_ATTR_ACCT_INPUT_OCTETS,
284 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
287 gigawords = sta->acct_input_gigawords;
289 gigawords += data.rx_bytes >> 32;
292 !radius_msg_add_attr_int32(
293 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
295 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
298 if (!radius_msg_add_attr_int32(msg,
299 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
301 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
304 gigawords = sta->acct_output_gigawords;
306 gigawords += data.tx_bytes >> 32;
309 !radius_msg_add_attr_int32(
310 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
312 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
317 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
319 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
323 if (eloop_terminated())
324 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
327 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
329 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
333 if (radius_client_send(hapd->radius, msg,
334 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
340 radius_msg_free(msg);
345 * accounting_sta_interim - Send a interim STA accounting report
346 * @hapd: hostapd BSS data
349 static void accounting_sta_interim(struct hostapd_data *hapd,
350 struct sta_info *sta)
352 if (sta->acct_session_started)
353 accounting_sta_report(hapd, sta, 0);
358 * accounting_sta_stop - Stop STA accounting
359 * @hapd: hostapd BSS data
362 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
364 if (sta->acct_session_started) {
365 accounting_sta_report(hapd, sta, 1);
366 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
367 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
369 "stopped accounting session %08X-%08X",
370 sta->acct_session_id_hi,
371 sta->acct_session_id_lo);
372 sta->acct_session_started = 0;
377 void accounting_sta_get_id(struct hostapd_data *hapd,
378 struct sta_info *sta)
380 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
381 if (hapd->acct_session_id_lo == 0) {
382 hapd->acct_session_id_hi++;
384 sta->acct_session_id_hi = hapd->acct_session_id_hi;
389 * accounting_receive - Process the RADIUS frames from Accounting Server
390 * @msg: RADIUS response message
391 * @req: RADIUS request message
392 * @shared_secret: RADIUS shared secret
393 * @shared_secret_len: Length of shared_secret in octets
394 * @data: Context data (struct hostapd_data *)
395 * Returns: Processing status
397 static RadiusRxResult
398 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
399 const u8 *shared_secret, size_t shared_secret_len,
402 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
403 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
404 return RADIUS_RX_UNKNOWN;
407 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
408 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
409 return RADIUS_RX_INVALID_AUTHENTICATOR;
412 return RADIUS_RX_PROCESSED;
416 static void accounting_report_state(struct hostapd_data *hapd, int on)
418 struct radius_msg *msg;
420 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
423 /* Inform RADIUS server that accounting will start/stop so that the
424 * server can close old accounting sessions. */
425 msg = accounting_msg(hapd, NULL,
426 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
427 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
431 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
432 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
434 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
435 radius_msg_free(msg);
439 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
440 radius_msg_free(msg);
445 * accounting_init: Initialize accounting
446 * @hapd: hostapd BSS data
447 * Returns: 0 on success, -1 on failure
449 int accounting_init(struct hostapd_data *hapd)
453 /* Acct-Session-Id should be unique over reboots. If reliable clock is
454 * not available, this could be replaced with reboot counter, etc. */
456 hapd->acct_session_id_hi = now.sec;
458 if (radius_client_register(hapd->radius, RADIUS_ACCT,
459 accounting_receive, hapd))
462 accounting_report_state(hapd, 1);
469 * accounting_deinit: Deinitilize accounting
470 * @hapd: hostapd BSS data
472 void accounting_deinit(struct hostapd_data *hapd)
474 accounting_report_state(hapd, 0);