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"
26 /* Default interval in seconds for polling TX/RX octets from the driver if
27 * STA is not using interim accounting. This detects wrap arounds for
28 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
29 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
31 static void accounting_sta_get_id(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 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
46 radius_client_get_id(hapd->radius));
48 printf("Could not create net RADIUS packet\n");
53 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
55 os_snprintf(buf, sizeof(buf), "%08X-%08X",
56 sta->acct_session_id_hi, sta->acct_session_id_lo);
57 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
58 (u8 *) buf, os_strlen(buf))) {
59 printf("Could not add Acct-Session-Id\n");
63 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
66 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
68 printf("Could not add Acct-Status-Type\n");
72 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
73 hapd->conf->ieee802_1x ?
74 RADIUS_ACCT_AUTHENTIC_RADIUS :
75 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
76 printf("Could not add Acct-Authentic\n");
81 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
83 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
89 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
91 printf("Could not add User-Name\n");
96 if (hapd->conf->own_ip_addr.af == AF_INET &&
97 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
98 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
99 printf("Could not add NAS-IP-Address\n");
104 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
105 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
106 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
107 printf("Could not add NAS-IPv6-Address\n");
110 #endif /* CONFIG_IPV6 */
112 if (hapd->conf->nas_identifier &&
113 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
114 (u8 *) hapd->conf->nas_identifier,
115 os_strlen(hapd->conf->nas_identifier))) {
116 printf("Could not add NAS-Identifier\n");
121 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
122 printf("Could not add NAS-Port\n");
126 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
127 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
128 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
129 (u8 *) buf, os_strlen(buf))) {
130 printf("Could not add Called-Station-Id\n");
135 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
137 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
138 (u8 *) buf, os_strlen(buf))) {
139 printf("Could not add Calling-Station-Id\n");
143 if (!radius_msg_add_attr_int32(
144 msg, RADIUS_ATTR_NAS_PORT_TYPE,
145 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
146 printf("Could not add NAS-Port-Type\n");
150 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
151 radius_sta_rate(hapd, sta) / 2,
152 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
153 radius_mode_txt(hapd));
154 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
155 (u8 *) buf, os_strlen(buf))) {
156 printf("Could not add Connect-Info\n");
161 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
166 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
168 printf("Could not add Class\n");
177 radius_msg_free(msg);
183 static int accounting_sta_update_stats(struct hostapd_data *hapd,
184 struct sta_info *sta,
185 struct hostap_sta_driver_data *data)
187 if (hostapd_read_sta_data(hapd, data, sta->addr))
190 if (sta->last_rx_bytes > data->rx_bytes)
191 sta->acct_input_gigawords++;
192 if (sta->last_tx_bytes > data->tx_bytes)
193 sta->acct_output_gigawords++;
194 sta->last_rx_bytes = data->rx_bytes;
195 sta->last_tx_bytes = data->tx_bytes;
197 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
198 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
199 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
200 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
201 sta->last_rx_bytes, sta->acct_input_gigawords,
202 sta->last_tx_bytes, sta->acct_output_gigawords);
208 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
210 struct hostapd_data *hapd = eloop_ctx;
211 struct sta_info *sta = timeout_ctx;
214 if (sta->acct_interim_interval) {
215 accounting_sta_interim(hapd, sta);
216 interval = sta->acct_interim_interval;
218 struct hostap_sta_driver_data data;
219 accounting_sta_update_stats(hapd, sta, &data);
220 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
223 eloop_register_timeout(interval, 0, accounting_interim_update,
229 * accounting_sta_start - Start STA accounting
230 * @hapd: hostapd BSS data
233 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
235 struct radius_msg *msg;
238 if (sta->acct_session_started)
241 accounting_sta_get_id(hapd, sta);
242 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
244 "starting accounting session %08X-%08X",
245 sta->acct_session_id_hi, sta->acct_session_id_lo);
247 time(&sta->acct_session_start);
248 sta->last_rx_bytes = sta->last_tx_bytes = 0;
249 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
250 hostapd_sta_clear_stats(hapd, sta->addr);
252 if (!hapd->conf->radius->acct_server)
255 if (sta->acct_interim_interval)
256 interval = sta->acct_interim_interval;
258 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
259 eloop_register_timeout(interval, 0, accounting_interim_update,
262 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
264 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
266 sta->acct_session_started = 1;
270 static void accounting_sta_report(struct hostapd_data *hapd,
271 struct sta_info *sta, int stop)
273 struct radius_msg *msg;
274 int cause = sta->acct_terminate_cause;
275 struct hostap_sta_driver_data data;
278 if (!hapd->conf->radius->acct_server)
281 msg = accounting_msg(hapd, sta,
282 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
283 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
285 printf("Could not create RADIUS Accounting message\n");
289 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
290 time(NULL) - sta->acct_session_start)) {
291 printf("Could not add Acct-Session-Time\n");
295 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
296 if (!radius_msg_add_attr_int32(msg,
297 RADIUS_ATTR_ACCT_INPUT_PACKETS,
299 printf("Could not add Acct-Input-Packets\n");
302 if (!radius_msg_add_attr_int32(msg,
303 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
305 printf("Could not add Acct-Output-Packets\n");
308 if (!radius_msg_add_attr_int32(msg,
309 RADIUS_ATTR_ACCT_INPUT_OCTETS,
311 printf("Could not add Acct-Input-Octets\n");
314 gigawords = sta->acct_input_gigawords;
316 gigawords += data.rx_bytes >> 32;
319 !radius_msg_add_attr_int32(
320 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
322 printf("Could not add Acct-Input-Gigawords\n");
325 if (!radius_msg_add_attr_int32(msg,
326 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
328 printf("Could not add Acct-Output-Octets\n");
331 gigawords = sta->acct_output_gigawords;
333 gigawords += data.tx_bytes >> 32;
336 !radius_msg_add_attr_int32(
337 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
339 printf("Could not add Acct-Output-Gigawords\n");
344 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
346 printf("Could not add Event-Timestamp\n");
350 if (eloop_terminated())
351 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
354 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
356 printf("Could not add Acct-Terminate-Cause\n");
360 radius_client_send(hapd->radius, msg,
361 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
366 radius_msg_free(msg);
372 * accounting_sta_interim - Send a interim STA accounting report
373 * @hapd: hostapd BSS data
376 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
378 if (sta->acct_session_started)
379 accounting_sta_report(hapd, sta, 0);
384 * accounting_sta_stop - Stop STA accounting
385 * @hapd: hostapd BSS data
388 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
390 if (sta->acct_session_started) {
391 accounting_sta_report(hapd, sta, 1);
392 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
393 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
395 "stopped accounting session %08X-%08X",
396 sta->acct_session_id_hi,
397 sta->acct_session_id_lo);
398 sta->acct_session_started = 0;
403 static void accounting_sta_get_id(struct hostapd_data *hapd,
404 struct sta_info *sta)
406 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
407 if (hapd->acct_session_id_lo == 0) {
408 hapd->acct_session_id_hi++;
410 sta->acct_session_id_hi = hapd->acct_session_id_hi;
415 * accounting_receive - Process the RADIUS frames from Accounting Server
416 * @msg: RADIUS response message
417 * @req: RADIUS request message
418 * @shared_secret: RADIUS shared secret
419 * @shared_secret_len: Length of shared_secret in octets
420 * @data: Context data (struct hostapd_data *)
421 * Returns: Processing status
423 static RadiusRxResult
424 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
425 const u8 *shared_secret, size_t shared_secret_len,
428 if (msg->hdr->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
429 printf("Unknown RADIUS message code\n");
430 return RADIUS_RX_UNKNOWN;
433 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
434 printf("Incoming RADIUS packet did not have correct "
435 "Authenticator - dropped\n");
436 return RADIUS_RX_INVALID_AUTHENTICATOR;
439 return RADIUS_RX_PROCESSED;
443 static void accounting_report_state(struct hostapd_data *hapd, int on)
445 struct radius_msg *msg;
447 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
450 /* Inform RADIUS server that accounting will start/stop so that the
451 * server can close old accounting sessions. */
452 msg = accounting_msg(hapd, NULL,
453 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
454 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
458 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
459 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
461 printf("Could not add Acct-Terminate-Cause\n");
462 radius_msg_free(msg);
467 radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
472 * accounting_init: Initialize accounting
473 * @hapd: hostapd BSS data
474 * Returns: 0 on success, -1 on failure
476 int accounting_init(struct hostapd_data *hapd)
478 /* Acct-Session-Id should be unique over reboots. If reliable clock is
479 * not available, this could be replaced with reboot counter, etc. */
480 hapd->acct_session_id_hi = time(NULL);
482 if (radius_client_register(hapd->radius, RADIUS_ACCT,
483 accounting_receive, hapd))
486 accounting_report_state(hapd, 1);
493 * accounting_deinit: Deinitilize accounting
494 * @hapd: hostapd BSS data
496 void accounting_deinit(struct hostapd_data *hapd)
498 accounting_report_state(hapd, 0);
502 int accounting_reconfig(struct hostapd_data *hapd,
503 struct hostapd_config *oldconf)
505 if (!hapd->radius_client_reconfigured)
508 accounting_deinit(hapd);
509 return accounting_init(hapd);