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 "drivers/driver.h"
14 #include "radius/radius.h"
15 #include "radius/radius_client.h"
17 #include "ieee802_1x.h"
18 #include "ap_config.h"
20 #include "ap_drv_ops.h"
21 #include "accounting.h"
24 /* Default interval in seconds for polling TX/RX octets from the driver if
25 * STA is not using interim accounting. This detects wrap arounds for
26 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
27 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
29 static void accounting_sta_get_id(struct hostapd_data *hapd,
30 struct sta_info *sta);
31 static void accounting_sta_interim(struct hostapd_data *hapd,
32 struct sta_info *sta);
35 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
39 struct radius_msg *msg;
45 struct hostapd_radius_attr *attr;
47 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
48 radius_client_get_id(hapd->radius));
50 printf("Could not create net RADIUS packet\n");
55 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
57 os_snprintf(buf, sizeof(buf), "%08X-%08X",
58 sta->acct_session_id_hi, sta->acct_session_id_lo);
59 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
60 (u8 *) buf, os_strlen(buf))) {
61 printf("Could not add Acct-Session-Id\n");
65 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
68 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
70 printf("Could not add Acct-Status-Type\n");
74 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
75 RADIUS_ATTR_ACCT_AUTHENTIC) &&
76 !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 (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
101 RADIUS_ATTR_NAS_IP_ADDRESS) &&
102 hapd->conf->own_ip_addr.af == AF_INET &&
103 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
104 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
105 printf("Could not add NAS-IP-Address\n");
110 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
111 RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
112 hapd->conf->own_ip_addr.af == AF_INET6 &&
113 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
114 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
115 printf("Could not add NAS-IPv6-Address\n");
118 #endif /* CONFIG_IPV6 */
120 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
121 RADIUS_ATTR_NAS_IDENTIFIER) &&
122 hapd->conf->nas_identifier &&
123 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
124 (u8 *) hapd->conf->nas_identifier,
125 os_strlen(hapd->conf->nas_identifier))) {
126 printf("Could not add NAS-Identifier\n");
130 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
131 RADIUS_ATTR_NAS_PORT) &&
133 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
134 printf("Could not add NAS-Port\n");
138 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
139 MAC2STR(hapd->own_addr),
140 wpa_ssid_txt(hapd->conf->ssid.ssid,
141 hapd->conf->ssid.ssid_len));
142 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
143 RADIUS_ATTR_CALLED_STATION_ID) &&
144 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
145 (u8 *) buf, os_strlen(buf))) {
146 printf("Could not add Called-Station-Id\n");
151 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
153 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
154 (u8 *) buf, os_strlen(buf))) {
155 printf("Could not add Calling-Station-Id\n");
159 if (!hostapd_config_get_radius_attr(
160 hapd->conf->radius_acct_req_attr,
161 RADIUS_ATTR_NAS_PORT_TYPE) &&
162 !radius_msg_add_attr_int32(
163 msg, RADIUS_ATTR_NAS_PORT_TYPE,
164 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
165 printf("Could not add NAS-Port-Type\n");
169 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
170 radius_sta_rate(hapd, sta) / 2,
171 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
172 radius_mode_txt(hapd));
173 if (!hostapd_config_get_radius_attr(
174 hapd->conf->radius_acct_req_attr,
175 RADIUS_ATTR_CONNECT_INFO) &&
176 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
177 (u8 *) buf, os_strlen(buf))) {
178 printf("Could not add Connect-Info\n");
183 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
188 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
190 printf("Could not add Class\n");
195 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
197 !radius_msg_add_attr(msg,
198 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
199 wpabuf_head(b), wpabuf_len(b))) {
200 wpa_printf(MSG_ERROR, "Could not add CUI");
205 for (attr = hapd->conf->radius_acct_req_attr; attr; attr = attr->next)
207 if (!radius_msg_add_attr(msg, attr->type,
208 wpabuf_head(attr->val),
209 wpabuf_len(attr->val))) {
210 wpa_printf(MSG_ERROR, "Could not add RADIUS "
219 radius_msg_free(msg);
224 static int accounting_sta_update_stats(struct hostapd_data *hapd,
225 struct sta_info *sta,
226 struct hostap_sta_driver_data *data)
228 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
231 if (sta->last_rx_bytes > data->rx_bytes)
232 sta->acct_input_gigawords++;
233 if (sta->last_tx_bytes > data->tx_bytes)
234 sta->acct_output_gigawords++;
235 sta->last_rx_bytes = data->rx_bytes;
236 sta->last_tx_bytes = data->tx_bytes;
238 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
239 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
240 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
241 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
242 sta->last_rx_bytes, sta->acct_input_gigawords,
243 sta->last_tx_bytes, sta->acct_output_gigawords);
249 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
251 struct hostapd_data *hapd = eloop_ctx;
252 struct sta_info *sta = timeout_ctx;
255 if (sta->acct_interim_interval) {
256 accounting_sta_interim(hapd, sta);
257 interval = sta->acct_interim_interval;
259 struct hostap_sta_driver_data data;
260 accounting_sta_update_stats(hapd, sta, &data);
261 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
264 eloop_register_timeout(interval, 0, accounting_interim_update,
270 * accounting_sta_start - Start STA accounting
271 * @hapd: hostapd BSS data
274 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
276 struct radius_msg *msg;
280 if (sta->acct_session_started)
283 accounting_sta_get_id(hapd, sta);
284 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
286 "starting accounting session %08X-%08X",
287 sta->acct_session_id_hi, sta->acct_session_id_lo);
290 sta->acct_session_start = t.sec;
291 sta->last_rx_bytes = sta->last_tx_bytes = 0;
292 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
293 hostapd_drv_sta_clear_stats(hapd, sta->addr);
295 if (!hapd->conf->radius->acct_server)
298 if (sta->acct_interim_interval)
299 interval = sta->acct_interim_interval;
301 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
302 eloop_register_timeout(interval, 0, accounting_interim_update,
305 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
307 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
308 radius_msg_free(msg);
310 sta->acct_session_started = 1;
314 static void accounting_sta_report(struct hostapd_data *hapd,
315 struct sta_info *sta, int stop)
317 struct radius_msg *msg;
318 int cause = sta->acct_terminate_cause;
319 struct hostap_sta_driver_data data;
323 if (!hapd->conf->radius->acct_server)
326 msg = accounting_msg(hapd, sta,
327 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
328 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
330 printf("Could not create RADIUS Accounting message\n");
335 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
336 now.sec - sta->acct_session_start)) {
337 printf("Could not add Acct-Session-Time\n");
341 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
342 if (!radius_msg_add_attr_int32(msg,
343 RADIUS_ATTR_ACCT_INPUT_PACKETS,
345 printf("Could not add Acct-Input-Packets\n");
348 if (!radius_msg_add_attr_int32(msg,
349 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
351 printf("Could not add Acct-Output-Packets\n");
354 if (!radius_msg_add_attr_int32(msg,
355 RADIUS_ATTR_ACCT_INPUT_OCTETS,
357 printf("Could not add Acct-Input-Octets\n");
360 gigawords = sta->acct_input_gigawords;
362 gigawords += data.rx_bytes >> 32;
365 !radius_msg_add_attr_int32(
366 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
368 printf("Could not add Acct-Input-Gigawords\n");
371 if (!radius_msg_add_attr_int32(msg,
372 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
374 printf("Could not add Acct-Output-Octets\n");
377 gigawords = sta->acct_output_gigawords;
379 gigawords += data.tx_bytes >> 32;
382 !radius_msg_add_attr_int32(
383 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
385 printf("Could not add Acct-Output-Gigawords\n");
390 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
392 printf("Could not add Event-Timestamp\n");
396 if (eloop_terminated())
397 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
400 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
402 printf("Could not add Acct-Terminate-Cause\n");
406 if (radius_client_send(hapd->radius, msg,
407 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
413 radius_msg_free(msg);
418 * accounting_sta_interim - Send a interim STA accounting report
419 * @hapd: hostapd BSS data
422 static void accounting_sta_interim(struct hostapd_data *hapd,
423 struct sta_info *sta)
425 if (sta->acct_session_started)
426 accounting_sta_report(hapd, sta, 0);
431 * accounting_sta_stop - Stop STA accounting
432 * @hapd: hostapd BSS data
435 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
437 if (sta->acct_session_started) {
438 accounting_sta_report(hapd, sta, 1);
439 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
440 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
442 "stopped accounting session %08X-%08X",
443 sta->acct_session_id_hi,
444 sta->acct_session_id_lo);
445 sta->acct_session_started = 0;
450 static void accounting_sta_get_id(struct hostapd_data *hapd,
451 struct sta_info *sta)
453 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
454 if (hapd->acct_session_id_lo == 0) {
455 hapd->acct_session_id_hi++;
457 sta->acct_session_id_hi = hapd->acct_session_id_hi;
462 * accounting_receive - Process the RADIUS frames from Accounting Server
463 * @msg: RADIUS response message
464 * @req: RADIUS request message
465 * @shared_secret: RADIUS shared secret
466 * @shared_secret_len: Length of shared_secret in octets
467 * @data: Context data (struct hostapd_data *)
468 * Returns: Processing status
470 static RadiusRxResult
471 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
472 const u8 *shared_secret, size_t shared_secret_len,
475 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
476 printf("Unknown RADIUS message code\n");
477 return RADIUS_RX_UNKNOWN;
480 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
481 printf("Incoming RADIUS packet did not have correct "
482 "Authenticator - dropped\n");
483 return RADIUS_RX_INVALID_AUTHENTICATOR;
486 return RADIUS_RX_PROCESSED;
490 static void accounting_report_state(struct hostapd_data *hapd, int on)
492 struct radius_msg *msg;
494 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
497 /* Inform RADIUS server that accounting will start/stop so that the
498 * server can close old accounting sessions. */
499 msg = accounting_msg(hapd, NULL,
500 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
501 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
505 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
506 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
508 printf("Could not add Acct-Terminate-Cause\n");
509 radius_msg_free(msg);
513 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
514 radius_msg_free(msg);
519 * accounting_init: Initialize accounting
520 * @hapd: hostapd BSS data
521 * Returns: 0 on success, -1 on failure
523 int accounting_init(struct hostapd_data *hapd)
527 /* Acct-Session-Id should be unique over reboots. If reliable clock is
528 * not available, this could be replaced with reboot counter, etc. */
530 hapd->acct_session_id_hi = now.sec;
532 if (radius_client_register(hapd->radius, RADIUS_ACCT,
533 accounting_receive, hapd))
536 accounting_report_state(hapd, 1);
543 * accounting_deinit: Deinitilize accounting
544 * @hapd: hostapd BSS data
546 void accounting_deinit(struct hostapd_data *hapd)
548 accounting_report_state(hapd, 0);