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");
54 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
56 if ((hapd->conf->wpa & 2) &&
57 !hapd->conf->disable_pmksa_caching &&
58 sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
59 os_snprintf(buf, sizeof(buf), "%016lX",
61 sta->eapol_sm->acct_multi_session_id);
62 if (!radius_msg_add_attr(
63 msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
64 (u8 *) buf, os_strlen(buf))) {
66 "Could not add Acct-Multi-Session-Id");
71 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
74 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
76 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
81 if (!hostapd_config_get_radius_attr(
82 hapd->conf->radius_acct_req_attr,
83 RADIUS_ATTR_ACCT_AUTHENTIC) &&
84 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
85 hapd->conf->ieee802_1x ?
86 RADIUS_ACCT_AUTHENTIC_RADIUS :
87 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
88 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
92 /* Use 802.1X identity if available */
93 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
95 /* Use RADIUS ACL identity if 802.1X provides no identity */
96 if (!val && sta->identity) {
97 val = (u8 *) sta->identity;
98 len = os_strlen(sta->identity);
101 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
104 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
107 len = os_strlen(buf);
110 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
112 wpa_printf(MSG_INFO, "Could not add User-Name");
117 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
123 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
128 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
130 wpa_printf(MSG_INFO, "Could not add Class");
135 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
137 !radius_msg_add_attr(msg,
138 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
139 wpabuf_head(b), wpabuf_len(b))) {
140 wpa_printf(MSG_ERROR, "Could not add CUI");
144 if (!b && sta->radius_cui &&
145 !radius_msg_add_attr(msg,
146 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
147 (u8 *) sta->radius_cui,
148 os_strlen(sta->radius_cui))) {
149 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
154 !radius_msg_add_attr_int32(msg,
155 RADIUS_ATTR_FRAMED_IP_ADDRESS,
156 be_to_host32(sta->ipaddr))) {
157 wpa_printf(MSG_ERROR,
158 "Could not add Framed-IP-Address");
164 if (now.sec > 1000000000 &&
165 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
167 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
174 radius_msg_free(msg);
179 static int accounting_sta_update_stats(struct hostapd_data *hapd,
180 struct sta_info *sta,
181 struct hostap_sta_driver_data *data)
183 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
186 if (sta->last_rx_bytes > data->rx_bytes)
187 sta->acct_input_gigawords++;
188 if (sta->last_tx_bytes > data->tx_bytes)
189 sta->acct_output_gigawords++;
190 sta->last_rx_bytes = data->rx_bytes;
191 sta->last_tx_bytes = data->tx_bytes;
193 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
194 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
195 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
196 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
197 sta->last_rx_bytes, sta->acct_input_gigawords,
198 sta->last_tx_bytes, sta->acct_output_gigawords);
204 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
206 struct hostapd_data *hapd = eloop_ctx;
207 struct sta_info *sta = timeout_ctx;
210 if (sta->acct_interim_interval) {
211 accounting_sta_interim(hapd, sta);
212 interval = sta->acct_interim_interval;
214 struct hostap_sta_driver_data data;
215 accounting_sta_update_stats(hapd, sta, &data);
216 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
219 eloop_register_timeout(interval, 0, accounting_interim_update,
225 * accounting_sta_start - Start STA accounting
226 * @hapd: hostapd BSS data
229 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
231 struct radius_msg *msg;
234 if (sta->acct_session_started)
237 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
239 "starting accounting session %016lX",
240 (long unsigned int) sta->acct_session_id);
242 os_get_reltime(&sta->acct_session_start);
243 sta->last_rx_bytes = sta->last_tx_bytes = 0;
244 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
245 hostapd_drv_sta_clear_stats(hapd, sta->addr);
247 if (!hapd->conf->radius->acct_server)
250 if (sta->acct_interim_interval)
251 interval = sta->acct_interim_interval;
253 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
254 eloop_register_timeout(interval, 0, accounting_interim_update,
257 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
259 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
260 radius_msg_free(msg);
262 sta->acct_session_started = 1;
266 static void accounting_sta_report(struct hostapd_data *hapd,
267 struct sta_info *sta, int stop)
269 struct radius_msg *msg;
270 int cause = sta->acct_terminate_cause;
271 struct hostap_sta_driver_data data;
272 struct os_reltime now_r, diff;
275 if (!hapd->conf->radius->acct_server)
278 msg = accounting_msg(hapd, sta,
279 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
280 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
282 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
286 os_get_reltime(&now_r);
287 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
288 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
290 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
294 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
295 if (!radius_msg_add_attr_int32(msg,
296 RADIUS_ATTR_ACCT_INPUT_PACKETS,
298 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
301 if (!radius_msg_add_attr_int32(msg,
302 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
304 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
307 if (!radius_msg_add_attr_int32(msg,
308 RADIUS_ATTR_ACCT_INPUT_OCTETS,
310 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
313 gigawords = sta->acct_input_gigawords;
315 gigawords += data.rx_bytes >> 32;
318 !radius_msg_add_attr_int32(
319 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
321 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
324 if (!radius_msg_add_attr_int32(msg,
325 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
327 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
330 gigawords = sta->acct_output_gigawords;
332 gigawords += data.tx_bytes >> 32;
335 !radius_msg_add_attr_int32(
336 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
338 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
343 if (eloop_terminated())
344 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
347 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
349 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
353 if (radius_client_send(hapd->radius, msg,
354 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
360 radius_msg_free(msg);
365 * accounting_sta_interim - Send a interim STA accounting report
366 * @hapd: hostapd BSS data
369 static void accounting_sta_interim(struct hostapd_data *hapd,
370 struct sta_info *sta)
372 if (sta->acct_session_started)
373 accounting_sta_report(hapd, sta, 0);
378 * accounting_sta_stop - Stop STA accounting
379 * @hapd: hostapd BSS data
382 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
384 if (sta->acct_session_started) {
385 accounting_sta_report(hapd, sta, 1);
386 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
387 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
389 "stopped accounting session %016lX",
390 (long unsigned int) sta->acct_session_id);
391 sta->acct_session_started = 0;
396 int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
399 * Acct-Session-Id should be globally and temporarily unique.
400 * A high quality random number is required therefore.
401 * This could be be improved by switching to a GUID.
403 return os_get_random((u8 *) &sta->acct_session_id,
404 sizeof(sta->acct_session_id));
409 * accounting_receive - Process the RADIUS frames from Accounting Server
410 * @msg: RADIUS response message
411 * @req: RADIUS request message
412 * @shared_secret: RADIUS shared secret
413 * @shared_secret_len: Length of shared_secret in octets
414 * @data: Context data (struct hostapd_data *)
415 * Returns: Processing status
417 static RadiusRxResult
418 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
419 const u8 *shared_secret, size_t shared_secret_len,
422 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
423 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
424 return RADIUS_RX_UNKNOWN;
427 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
428 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
429 return RADIUS_RX_INVALID_AUTHENTICATOR;
432 return RADIUS_RX_PROCESSED;
436 static void accounting_report_state(struct hostapd_data *hapd, int on)
438 struct radius_msg *msg;
440 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
443 /* Inform RADIUS server that accounting will start/stop so that the
444 * server can close old accounting sessions. */
445 msg = accounting_msg(hapd, NULL,
446 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
447 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
451 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
452 radius_msg_free(msg);
457 * accounting_init: Initialize accounting
458 * @hapd: hostapd BSS data
459 * Returns: 0 on success, -1 on failure
461 int accounting_init(struct hostapd_data *hapd)
463 if (radius_client_register(hapd->radius, RADIUS_ACCT,
464 accounting_receive, hapd))
467 accounting_report_state(hapd, 1);
474 * accounting_deinit: Deinitialize accounting
475 * @hapd: hostapd BSS data
477 void accounting_deinit(struct hostapd_data *hapd)
479 accounting_report_state(hapd, 0);