2 * hostapd / RADIUS Accounting
3 * Copyright (c) 2002-2008, 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.
18 #include "radius/radius.h"
19 #include "radius/radius_client.h"
21 #include "accounting.h"
22 #include "ieee802_1x.h"
27 /* Default interval in seconds for polling TX/RX octets from the driver if
28 * STA is not using interim accounting. This detects wrap arounds for
29 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
30 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
32 static void accounting_sta_get_id(struct hostapd_data *hapd,
33 struct sta_info *sta);
36 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
40 struct radius_msg *msg;
46 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
47 radius_client_get_id(hapd->radius));
49 printf("Could not create net RADIUS packet\n");
54 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
56 os_snprintf(buf, sizeof(buf), "%08X-%08X",
57 sta->acct_session_id_hi, sta->acct_session_id_lo);
58 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
59 (u8 *) buf, os_strlen(buf))) {
60 printf("Could not add Acct-Session-Id\n");
64 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
67 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
69 printf("Could not add Acct-Status-Type\n");
73 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
74 hapd->conf->ieee802_1x ?
75 RADIUS_ACCT_AUTHENTIC_RADIUS :
76 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
77 printf("Could not add Acct-Authentic\n");
82 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
84 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
90 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
92 printf("Could not add User-Name\n");
97 if (hapd->conf->own_ip_addr.af == AF_INET &&
98 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
99 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
100 printf("Could not add NAS-IP-Address\n");
105 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
106 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
107 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
108 printf("Could not add NAS-IPv6-Address\n");
111 #endif /* CONFIG_IPV6 */
113 if (hapd->conf->nas_identifier &&
114 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
115 (u8 *) hapd->conf->nas_identifier,
116 os_strlen(hapd->conf->nas_identifier))) {
117 printf("Could not add NAS-Identifier\n");
122 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
123 printf("Could not add NAS-Port\n");
127 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
128 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
129 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
130 (u8 *) buf, os_strlen(buf))) {
131 printf("Could not add Called-Station-Id\n");
136 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
138 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
139 (u8 *) buf, os_strlen(buf))) {
140 printf("Could not add Calling-Station-Id\n");
144 if (!radius_msg_add_attr_int32(
145 msg, RADIUS_ATTR_NAS_PORT_TYPE,
146 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
147 printf("Could not add NAS-Port-Type\n");
151 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
152 radius_sta_rate(hapd, sta) / 2,
153 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
154 radius_mode_txt(hapd));
155 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
156 (u8 *) buf, os_strlen(buf))) {
157 printf("Could not add Connect-Info\n");
162 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
167 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
169 printf("Could not add Class\n");
178 radius_msg_free(msg);
184 static int accounting_sta_update_stats(struct hostapd_data *hapd,
185 struct sta_info *sta,
186 struct hostap_sta_driver_data *data)
188 if (hostapd_read_sta_data(hapd, data, sta->addr))
191 if (sta->last_rx_bytes > data->rx_bytes)
192 sta->acct_input_gigawords++;
193 if (sta->last_tx_bytes > data->tx_bytes)
194 sta->acct_output_gigawords++;
195 sta->last_rx_bytes = data->rx_bytes;
196 sta->last_tx_bytes = data->tx_bytes;
198 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
199 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
200 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
201 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
202 sta->last_rx_bytes, sta->acct_input_gigawords,
203 sta->last_tx_bytes, sta->acct_output_gigawords);
209 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
211 struct hostapd_data *hapd = eloop_ctx;
212 struct sta_info *sta = timeout_ctx;
215 if (sta->acct_interim_interval) {
216 accounting_sta_interim(hapd, sta);
217 interval = sta->acct_interim_interval;
219 struct hostap_sta_driver_data data;
220 accounting_sta_update_stats(hapd, sta, &data);
221 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
224 eloop_register_timeout(interval, 0, accounting_interim_update,
230 * accounting_sta_start - Start STA accounting
231 * @hapd: hostapd BSS data
234 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
236 struct radius_msg *msg;
239 if (sta->acct_session_started)
242 accounting_sta_get_id(hapd, sta);
243 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
245 "starting accounting session %08X-%08X",
246 sta->acct_session_id_hi, sta->acct_session_id_lo);
248 time(&sta->acct_session_start);
249 sta->last_rx_bytes = sta->last_tx_bytes = 0;
250 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
251 hostapd_sta_clear_stats(hapd, sta->addr);
253 if (!hapd->conf->radius->acct_server)
256 if (sta->acct_interim_interval)
257 interval = sta->acct_interim_interval;
259 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
260 eloop_register_timeout(interval, 0, accounting_interim_update,
263 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
265 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
267 sta->acct_session_started = 1;
271 static void accounting_sta_report(struct hostapd_data *hapd,
272 struct sta_info *sta, int stop)
274 struct radius_msg *msg;
275 int cause = sta->acct_terminate_cause;
276 struct hostap_sta_driver_data data;
279 if (!hapd->conf->radius->acct_server)
282 msg = accounting_msg(hapd, sta,
283 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
284 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
286 printf("Could not create RADIUS Accounting message\n");
290 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
291 time(NULL) - sta->acct_session_start)) {
292 printf("Could not add Acct-Session-Time\n");
296 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
297 if (!radius_msg_add_attr_int32(msg,
298 RADIUS_ATTR_ACCT_INPUT_PACKETS,
300 printf("Could not add Acct-Input-Packets\n");
303 if (!radius_msg_add_attr_int32(msg,
304 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
306 printf("Could not add Acct-Output-Packets\n");
309 if (!radius_msg_add_attr_int32(msg,
310 RADIUS_ATTR_ACCT_INPUT_OCTETS,
312 printf("Could not add Acct-Input-Octets\n");
315 gigawords = sta->acct_input_gigawords;
317 gigawords += data.rx_bytes >> 32;
320 !radius_msg_add_attr_int32(
321 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
323 printf("Could not add Acct-Input-Gigawords\n");
326 if (!radius_msg_add_attr_int32(msg,
327 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
329 printf("Could not add Acct-Output-Octets\n");
332 gigawords = sta->acct_output_gigawords;
334 gigawords += data.tx_bytes >> 32;
337 !radius_msg_add_attr_int32(
338 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
340 printf("Could not add Acct-Output-Gigawords\n");
345 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
347 printf("Could not add Event-Timestamp\n");
351 if (eloop_terminated())
352 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
355 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
357 printf("Could not add Acct-Terminate-Cause\n");
361 radius_client_send(hapd->radius, msg,
362 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
367 radius_msg_free(msg);
373 * accounting_sta_interim - Send a interim STA accounting report
374 * @hapd: hostapd BSS data
377 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
379 if (sta->acct_session_started)
380 accounting_sta_report(hapd, sta, 0);
385 * accounting_sta_stop - Stop STA accounting
386 * @hapd: hostapd BSS data
389 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
391 if (sta->acct_session_started) {
392 accounting_sta_report(hapd, sta, 1);
393 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
394 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
396 "stopped accounting session %08X-%08X",
397 sta->acct_session_id_hi,
398 sta->acct_session_id_lo);
399 sta->acct_session_started = 0;
404 static void accounting_sta_get_id(struct hostapd_data *hapd,
405 struct sta_info *sta)
407 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
408 if (hapd->acct_session_id_lo == 0) {
409 hapd->acct_session_id_hi++;
411 sta->acct_session_id_hi = hapd->acct_session_id_hi;
416 * accounting_receive - Process the RADIUS frames from Accounting Server
417 * @msg: RADIUS response message
418 * @req: RADIUS request message
419 * @shared_secret: RADIUS shared secret
420 * @shared_secret_len: Length of shared_secret in octets
421 * @data: Context data (struct hostapd_data *)
422 * Returns: Processing status
424 static RadiusRxResult
425 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
426 const u8 *shared_secret, size_t shared_secret_len,
429 if (msg->hdr->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
430 printf("Unknown RADIUS message code\n");
431 return RADIUS_RX_UNKNOWN;
434 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
435 printf("Incoming RADIUS packet did not have correct "
436 "Authenticator - dropped\n");
437 return RADIUS_RX_INVALID_AUTHENTICATOR;
440 return RADIUS_RX_PROCESSED;
444 static void accounting_report_state(struct hostapd_data *hapd, int on)
446 struct radius_msg *msg;
448 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
451 /* Inform RADIUS server that accounting will start/stop so that the
452 * server can close old accounting sessions. */
453 msg = accounting_msg(hapd, NULL,
454 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
455 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
459 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
460 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
462 printf("Could not add Acct-Terminate-Cause\n");
463 radius_msg_free(msg);
468 radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
473 * accounting_init: Initialize accounting
474 * @hapd: hostapd BSS data
475 * Returns: 0 on success, -1 on failure
477 int accounting_init(struct hostapd_data *hapd)
479 /* Acct-Session-Id should be unique over reboots. If reliable clock is
480 * not available, this could be replaced with reboot counter, etc. */
481 hapd->acct_session_id_hi = time(NULL);
483 if (radius_client_register(hapd->radius, RADIUS_ACCT,
484 accounting_receive, hapd))
487 accounting_report_state(hapd, 1);
494 * accounting_deinit: Deinitilize accounting
495 * @hapd: hostapd BSS data
497 void accounting_deinit(struct hostapd_data *hapd)
499 accounting_report_state(hapd, 0);
503 int accounting_reconfig(struct hostapd_data *hapd,
504 struct hostapd_config *oldconf)
506 if (!hapd->radius_client_reconfigured)
509 accounting_deinit(hapd);
510 return accounting_init(hapd);