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), hapd->conf->ssid.ssid);
140 if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
141 RADIUS_ATTR_CALLED_STATION_ID) &&
142 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
143 (u8 *) buf, os_strlen(buf))) {
144 printf("Could not add Called-Station-Id\n");
149 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
151 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
152 (u8 *) buf, os_strlen(buf))) {
153 printf("Could not add Calling-Station-Id\n");
157 if (!hostapd_config_get_radius_attr(
158 hapd->conf->radius_acct_req_attr,
159 RADIUS_ATTR_NAS_PORT_TYPE) &&
160 !radius_msg_add_attr_int32(
161 msg, RADIUS_ATTR_NAS_PORT_TYPE,
162 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
163 printf("Could not add NAS-Port-Type\n");
167 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
168 radius_sta_rate(hapd, sta) / 2,
169 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
170 radius_mode_txt(hapd));
171 if (!hostapd_config_get_radius_attr(
172 hapd->conf->radius_acct_req_attr,
173 RADIUS_ATTR_CONNECT_INFO) &&
174 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
175 (u8 *) buf, os_strlen(buf))) {
176 printf("Could not add Connect-Info\n");
181 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
186 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
188 printf("Could not add Class\n");
193 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
195 !radius_msg_add_attr(msg,
196 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
197 wpabuf_head(b), wpabuf_len(b))) {
198 wpa_printf(MSG_ERROR, "Could not add CUI");
203 for (attr = hapd->conf->radius_acct_req_attr; attr; attr = attr->next)
205 if (!radius_msg_add_attr(msg, attr->type,
206 wpabuf_head(attr->val),
207 wpabuf_len(attr->val))) {
208 wpa_printf(MSG_ERROR, "Could not add RADIUS "
217 radius_msg_free(msg);
222 static int accounting_sta_update_stats(struct hostapd_data *hapd,
223 struct sta_info *sta,
224 struct hostap_sta_driver_data *data)
226 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
229 if (sta->last_rx_bytes > data->rx_bytes)
230 sta->acct_input_gigawords++;
231 if (sta->last_tx_bytes > data->tx_bytes)
232 sta->acct_output_gigawords++;
233 sta->last_rx_bytes = data->rx_bytes;
234 sta->last_tx_bytes = data->tx_bytes;
236 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
237 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
238 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
239 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
240 sta->last_rx_bytes, sta->acct_input_gigawords,
241 sta->last_tx_bytes, sta->acct_output_gigawords);
247 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
249 struct hostapd_data *hapd = eloop_ctx;
250 struct sta_info *sta = timeout_ctx;
253 if (sta->acct_interim_interval) {
254 accounting_sta_interim(hapd, sta);
255 interval = sta->acct_interim_interval;
257 struct hostap_sta_driver_data data;
258 accounting_sta_update_stats(hapd, sta, &data);
259 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
262 eloop_register_timeout(interval, 0, accounting_interim_update,
268 * accounting_sta_start - Start STA accounting
269 * @hapd: hostapd BSS data
272 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
274 struct radius_msg *msg;
278 if (sta->acct_session_started)
281 accounting_sta_get_id(hapd, sta);
282 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
284 "starting accounting session %08X-%08X",
285 sta->acct_session_id_hi, sta->acct_session_id_lo);
288 sta->acct_session_start = t.sec;
289 sta->last_rx_bytes = sta->last_tx_bytes = 0;
290 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
291 hostapd_drv_sta_clear_stats(hapd, sta->addr);
293 if (!hapd->conf->radius->acct_server)
296 if (sta->acct_interim_interval)
297 interval = sta->acct_interim_interval;
299 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
300 eloop_register_timeout(interval, 0, accounting_interim_update,
303 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
305 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
306 radius_msg_free(msg);
308 sta->acct_session_started = 1;
312 static void accounting_sta_report(struct hostapd_data *hapd,
313 struct sta_info *sta, int stop)
315 struct radius_msg *msg;
316 int cause = sta->acct_terminate_cause;
317 struct hostap_sta_driver_data data;
321 if (!hapd->conf->radius->acct_server)
324 msg = accounting_msg(hapd, sta,
325 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
326 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
328 printf("Could not create RADIUS Accounting message\n");
333 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
334 now.sec - sta->acct_session_start)) {
335 printf("Could not add Acct-Session-Time\n");
339 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
340 if (!radius_msg_add_attr_int32(msg,
341 RADIUS_ATTR_ACCT_INPUT_PACKETS,
343 printf("Could not add Acct-Input-Packets\n");
346 if (!radius_msg_add_attr_int32(msg,
347 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
349 printf("Could not add Acct-Output-Packets\n");
352 if (!radius_msg_add_attr_int32(msg,
353 RADIUS_ATTR_ACCT_INPUT_OCTETS,
355 printf("Could not add Acct-Input-Octets\n");
358 gigawords = sta->acct_input_gigawords;
360 gigawords += data.rx_bytes >> 32;
363 !radius_msg_add_attr_int32(
364 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
366 printf("Could not add Acct-Input-Gigawords\n");
369 if (!radius_msg_add_attr_int32(msg,
370 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
372 printf("Could not add Acct-Output-Octets\n");
375 gigawords = sta->acct_output_gigawords;
377 gigawords += data.tx_bytes >> 32;
380 !radius_msg_add_attr_int32(
381 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
383 printf("Could not add Acct-Output-Gigawords\n");
388 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
390 printf("Could not add Event-Timestamp\n");
394 if (eloop_terminated())
395 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
398 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
400 printf("Could not add Acct-Terminate-Cause\n");
404 if (radius_client_send(hapd->radius, msg,
405 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
411 radius_msg_free(msg);
416 * accounting_sta_interim - Send a interim STA accounting report
417 * @hapd: hostapd BSS data
420 static void accounting_sta_interim(struct hostapd_data *hapd,
421 struct sta_info *sta)
423 if (sta->acct_session_started)
424 accounting_sta_report(hapd, sta, 0);
429 * accounting_sta_stop - Stop STA accounting
430 * @hapd: hostapd BSS data
433 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
435 if (sta->acct_session_started) {
436 accounting_sta_report(hapd, sta, 1);
437 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
438 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
440 "stopped accounting session %08X-%08X",
441 sta->acct_session_id_hi,
442 sta->acct_session_id_lo);
443 sta->acct_session_started = 0;
448 static void accounting_sta_get_id(struct hostapd_data *hapd,
449 struct sta_info *sta)
451 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
452 if (hapd->acct_session_id_lo == 0) {
453 hapd->acct_session_id_hi++;
455 sta->acct_session_id_hi = hapd->acct_session_id_hi;
460 * accounting_receive - Process the RADIUS frames from Accounting Server
461 * @msg: RADIUS response message
462 * @req: RADIUS request message
463 * @shared_secret: RADIUS shared secret
464 * @shared_secret_len: Length of shared_secret in octets
465 * @data: Context data (struct hostapd_data *)
466 * Returns: Processing status
468 static RadiusRxResult
469 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
470 const u8 *shared_secret, size_t shared_secret_len,
473 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
474 printf("Unknown RADIUS message code\n");
475 return RADIUS_RX_UNKNOWN;
478 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
479 printf("Incoming RADIUS packet did not have correct "
480 "Authenticator - dropped\n");
481 return RADIUS_RX_INVALID_AUTHENTICATOR;
484 return RADIUS_RX_PROCESSED;
488 static void accounting_report_state(struct hostapd_data *hapd, int on)
490 struct radius_msg *msg;
492 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
495 /* Inform RADIUS server that accounting will start/stop so that the
496 * server can close old accounting sessions. */
497 msg = accounting_msg(hapd, NULL,
498 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
499 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
503 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
504 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
506 printf("Could not add Acct-Terminate-Cause\n");
507 radius_msg_free(msg);
511 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
512 radius_msg_free(msg);
517 * accounting_init: Initialize accounting
518 * @hapd: hostapd BSS data
519 * Returns: 0 on success, -1 on failure
521 int accounting_init(struct hostapd_data *hapd)
525 /* Acct-Session-Id should be unique over reboots. If reliable clock is
526 * not available, this could be replaced with reboot counter, etc. */
528 hapd->acct_session_id_hi = now.sec;
530 if (radius_client_register(hapd->radius, RADIUS_ACCT,
531 accounting_receive, hapd))
534 accounting_report_state(hapd, 1);
541 * accounting_deinit: Deinitilize accounting
542 * @hapd: hostapd BSS data
544 void accounting_deinit(struct hostapd_data *hapd)
546 accounting_report_state(hapd, 0);