2 * hostapd / RADIUS Accounting
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "drivers/driver.h"
20 #include "radius/radius.h"
21 #include "radius/radius_client.h"
23 #include "ieee802_1x.h"
24 #include "ap_config.h"
26 #include "ap_drv_ops.h"
27 #include "accounting.h"
30 /* Default interval in seconds for polling TX/RX octets from the driver if
31 * STA is not using interim accounting. This detects wrap arounds for
32 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
33 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
35 static void accounting_sta_get_id(struct hostapd_data *hapd,
36 struct sta_info *sta);
39 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
43 struct radius_msg *msg;
49 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
50 radius_client_get_id(hapd->radius));
52 printf("Could not create net RADIUS packet\n");
57 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
59 os_snprintf(buf, sizeof(buf), "%08X-%08X",
60 sta->acct_session_id_hi, sta->acct_session_id_lo);
61 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
62 (u8 *) buf, os_strlen(buf))) {
63 printf("Could not add Acct-Session-Id\n");
67 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
70 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
72 printf("Could not add Acct-Status-Type\n");
76 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
77 hapd->conf->ieee802_1x ?
78 RADIUS_ACCT_AUTHENTIC_RADIUS :
79 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
80 printf("Could not add Acct-Authentic\n");
85 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
87 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
93 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
95 printf("Could not add User-Name\n");
100 if (hapd->conf->own_ip_addr.af == AF_INET &&
101 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
102 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
103 printf("Could not add NAS-IP-Address\n");
108 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
109 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
110 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
111 printf("Could not add NAS-IPv6-Address\n");
114 #endif /* CONFIG_IPV6 */
116 if (hapd->conf->nas_identifier &&
117 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
118 (u8 *) hapd->conf->nas_identifier,
119 os_strlen(hapd->conf->nas_identifier))) {
120 printf("Could not add NAS-Identifier\n");
125 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
126 printf("Could not add NAS-Port\n");
130 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
131 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
132 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
133 (u8 *) buf, os_strlen(buf))) {
134 printf("Could not add Called-Station-Id\n");
139 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
141 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
142 (u8 *) buf, os_strlen(buf))) {
143 printf("Could not add Calling-Station-Id\n");
147 if (!radius_msg_add_attr_int32(
148 msg, RADIUS_ATTR_NAS_PORT_TYPE,
149 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
150 printf("Could not add NAS-Port-Type\n");
154 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
155 radius_sta_rate(hapd, sta) / 2,
156 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
157 radius_mode_txt(hapd));
158 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
159 (u8 *) buf, os_strlen(buf))) {
160 printf("Could not add Connect-Info\n");
165 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
170 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
172 printf("Could not add Class\n");
181 radius_msg_free(msg);
186 static int accounting_sta_update_stats(struct hostapd_data *hapd,
187 struct sta_info *sta,
188 struct hostap_sta_driver_data *data)
190 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
193 if (sta->last_rx_bytes > data->rx_bytes)
194 sta->acct_input_gigawords++;
195 if (sta->last_tx_bytes > data->tx_bytes)
196 sta->acct_output_gigawords++;
197 sta->last_rx_bytes = data->rx_bytes;
198 sta->last_tx_bytes = data->tx_bytes;
200 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
201 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
202 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
203 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
204 sta->last_rx_bytes, sta->acct_input_gigawords,
205 sta->last_tx_bytes, sta->acct_output_gigawords);
211 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
213 struct hostapd_data *hapd = eloop_ctx;
214 struct sta_info *sta = timeout_ctx;
217 if (sta->acct_interim_interval) {
218 accounting_sta_interim(hapd, sta);
219 interval = sta->acct_interim_interval;
221 struct hostap_sta_driver_data data;
222 accounting_sta_update_stats(hapd, sta, &data);
223 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
226 eloop_register_timeout(interval, 0, accounting_interim_update,
232 * accounting_sta_start - Start STA accounting
233 * @hapd: hostapd BSS data
236 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
238 struct radius_msg *msg;
242 if (sta->acct_session_started)
245 accounting_sta_get_id(hapd, sta);
246 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
248 "starting accounting session %08X-%08X",
249 sta->acct_session_id_hi, sta->acct_session_id_lo);
252 sta->acct_session_start = t.sec;
253 sta->last_rx_bytes = sta->last_tx_bytes = 0;
254 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
255 hostapd_drv_sta_clear_stats(hapd, sta->addr);
257 if (!hapd->conf->radius->acct_server)
260 if (sta->acct_interim_interval)
261 interval = sta->acct_interim_interval;
263 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
264 eloop_register_timeout(interval, 0, accounting_interim_update,
267 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
269 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
271 sta->acct_session_started = 1;
275 static void accounting_sta_report(struct hostapd_data *hapd,
276 struct sta_info *sta, int stop)
278 struct radius_msg *msg;
279 int cause = sta->acct_terminate_cause;
280 struct hostap_sta_driver_data data;
284 if (!hapd->conf->radius->acct_server)
287 msg = accounting_msg(hapd, sta,
288 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
289 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
291 printf("Could not create RADIUS Accounting message\n");
296 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
297 now.sec - sta->acct_session_start)) {
298 printf("Could not add Acct-Session-Time\n");
302 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
303 if (!radius_msg_add_attr_int32(msg,
304 RADIUS_ATTR_ACCT_INPUT_PACKETS,
306 printf("Could not add Acct-Input-Packets\n");
309 if (!radius_msg_add_attr_int32(msg,
310 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
312 printf("Could not add Acct-Output-Packets\n");
315 if (!radius_msg_add_attr_int32(msg,
316 RADIUS_ATTR_ACCT_INPUT_OCTETS,
318 printf("Could not add Acct-Input-Octets\n");
321 gigawords = sta->acct_input_gigawords;
323 gigawords += data.rx_bytes >> 32;
326 !radius_msg_add_attr_int32(
327 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
329 printf("Could not add Acct-Input-Gigawords\n");
332 if (!radius_msg_add_attr_int32(msg,
333 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
335 printf("Could not add Acct-Output-Octets\n");
338 gigawords = sta->acct_output_gigawords;
340 gigawords += data.tx_bytes >> 32;
343 !radius_msg_add_attr_int32(
344 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
346 printf("Could not add Acct-Output-Gigawords\n");
351 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
353 printf("Could not add Event-Timestamp\n");
357 if (eloop_terminated())
358 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
361 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
363 printf("Could not add Acct-Terminate-Cause\n");
367 radius_client_send(hapd->radius, msg,
368 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
373 radius_msg_free(msg);
378 * accounting_sta_interim - Send a interim STA accounting report
379 * @hapd: hostapd BSS data
382 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
384 if (sta->acct_session_started)
385 accounting_sta_report(hapd, sta, 0);
390 * accounting_sta_stop - Stop STA accounting
391 * @hapd: hostapd BSS data
394 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
396 if (sta->acct_session_started) {
397 accounting_sta_report(hapd, sta, 1);
398 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
399 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
401 "stopped accounting session %08X-%08X",
402 sta->acct_session_id_hi,
403 sta->acct_session_id_lo);
404 sta->acct_session_started = 0;
409 static void accounting_sta_get_id(struct hostapd_data *hapd,
410 struct sta_info *sta)
412 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
413 if (hapd->acct_session_id_lo == 0) {
414 hapd->acct_session_id_hi++;
416 sta->acct_session_id_hi = hapd->acct_session_id_hi;
421 * accounting_receive - Process the RADIUS frames from Accounting Server
422 * @msg: RADIUS response message
423 * @req: RADIUS request message
424 * @shared_secret: RADIUS shared secret
425 * @shared_secret_len: Length of shared_secret in octets
426 * @data: Context data (struct hostapd_data *)
427 * Returns: Processing status
429 static RadiusRxResult
430 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
431 const u8 *shared_secret, size_t shared_secret_len,
434 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
435 printf("Unknown RADIUS message code\n");
436 return RADIUS_RX_UNKNOWN;
439 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
440 printf("Incoming RADIUS packet did not have correct "
441 "Authenticator - dropped\n");
442 return RADIUS_RX_INVALID_AUTHENTICATOR;
445 return RADIUS_RX_PROCESSED;
449 static void accounting_report_state(struct hostapd_data *hapd, int on)
451 struct radius_msg *msg;
453 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
456 /* Inform RADIUS server that accounting will start/stop so that the
457 * server can close old accounting sessions. */
458 msg = accounting_msg(hapd, NULL,
459 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
460 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
464 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
465 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
467 printf("Could not add Acct-Terminate-Cause\n");
468 radius_msg_free(msg);
472 radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
477 * accounting_init: Initialize accounting
478 * @hapd: hostapd BSS data
479 * Returns: 0 on success, -1 on failure
481 int accounting_init(struct hostapd_data *hapd)
485 /* Acct-Session-Id should be unique over reboots. If reliable clock is
486 * not available, this could be replaced with reboot counter, etc. */
488 hapd->acct_session_id_hi = now.sec;
490 if (radius_client_register(hapd->radius, RADIUS_ACCT,
491 accounting_receive, hapd))
494 accounting_report_state(hapd, 1);
501 * accounting_deinit: Deinitilize accounting
502 * @hapd: hostapd BSS data
504 void accounting_deinit(struct hostapd_data *hapd)
506 accounting_report_state(hapd, 0);