blob: 0aacc3c95b084cb976e1f43d2ddd9354916b7b01 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / RADIUS Accounting
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003 * Copyright (c) 2002-2009, 2012-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080013#include "eapol_auth/eapol_auth_sm.h"
14#include "eapol_auth/eapol_auth_sm_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "radius/radius.h"
16#include "radius/radius_client.h"
17#include "hostapd.h"
18#include "ieee802_1x.h"
19#include "ap_config.h"
20#include "sta_info.h"
21#include "ap_drv_ops.h"
22#include "accounting.h"
23
24
25/* Default interval in seconds for polling TX/RX octets from the driver if
26 * STA is not using interim accounting. This detects wrap arounds for
27 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
28#define ACCT_DEFAULT_UPDATE_INTERVAL 300
29
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070030static void accounting_sta_interim(struct hostapd_data *hapd,
31 struct sta_info *sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032
33
34static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
35 struct sta_info *sta,
36 int status_type)
37{
38 struct radius_msg *msg;
39 char buf[128];
40 u8 *val;
41 size_t len;
42 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -070043 struct wpabuf *b;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -080044 struct os_time now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045
46 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
47 radius_client_get_id(hapd->radius));
48 if (msg == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080049 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050 return NULL;
51 }
52
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
54 status_type)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080055 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056 goto fail;
57 }
58
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059 if (sta) {
Dmitry Shmidtb97e4282016-02-08 10:16:07 -080060 if (!hostapd_config_get_radius_attr(
61 hapd->conf->radius_acct_req_attr,
62 RADIUS_ATTR_ACCT_AUTHENTIC) &&
63 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
64 hapd->conf->ieee802_1x ?
65 RADIUS_ACCT_AUTHENTIC_RADIUS :
66 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
67 wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
68 goto fail;
69 }
70
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070071 /* Use 802.1X identity if available */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070073
74 /* Use RADIUS ACL identity if 802.1X provides no identity */
75 if (!val && sta->identity) {
76 val = (u8 *) sta->identity;
77 len = os_strlen(sta->identity);
78 }
79
80 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
81 * identity */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082 if (!val) {
83 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
84 MAC2STR(sta->addr));
85 val = (u8 *) buf;
86 len = os_strlen(buf);
87 }
88
89 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
90 len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -080091 wpa_printf(MSG_INFO, "Could not add User-Name");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 goto fail;
93 }
94 }
95
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070096 if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
97 msg) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099
100 if (sta) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 for (i = 0; ; i++) {
102 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
103 i);
104 if (val == NULL)
105 break;
106
107 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
108 val, len)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800109 wpa_printf(MSG_INFO, "Could not add Class");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 goto fail;
111 }
112 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700113
114 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
115 if (b &&
116 !radius_msg_add_attr(msg,
117 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
118 wpabuf_head(b), wpabuf_len(b))) {
119 wpa_printf(MSG_ERROR, "Could not add CUI");
120 goto fail;
121 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700123 if (!b && sta->radius_cui &&
124 !radius_msg_add_attr(msg,
125 RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
126 (u8 *) sta->radius_cui,
127 os_strlen(sta->radius_cui))) {
128 wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700129 goto fail;
130 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800131
132 if (sta->ipaddr &&
133 !radius_msg_add_attr_int32(msg,
134 RADIUS_ATTR_FRAMED_IP_ADDRESS,
135 be_to_host32(sta->ipaddr))) {
136 wpa_printf(MSG_ERROR,
137 "Could not add Framed-IP-Address");
138 goto fail;
139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140 }
141
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800142 os_get_time(&now);
143 if (now.sec > 1000000000 &&
144 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
145 now.sec)) {
146 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
147 goto fail;
148 }
149
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800150 /*
151 * Add Acct-Delay-Time with zero value for the first transmission. This
152 * will be updated within radius_client.c when retransmitting the frame.
153 */
154 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_DELAY_TIME, 0)) {
155 wpa_printf(MSG_INFO, "Could not add Acct-Delay-Time");
156 goto fail;
157 }
158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 return msg;
160
161 fail:
162 radius_msg_free(msg);
163 return NULL;
164}
165
166
167static int accounting_sta_update_stats(struct hostapd_data *hapd,
168 struct sta_info *sta,
169 struct hostap_sta_driver_data *data)
170{
171 if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
172 return -1;
173
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800174 if (!data->bytes_64bit) {
175 /* Extend 32-bit counters from the driver to 64-bit counters */
176 if (sta->last_rx_bytes_lo > data->rx_bytes)
177 sta->last_rx_bytes_hi++;
178 sta->last_rx_bytes_lo = data->rx_bytes;
179
180 if (sta->last_tx_bytes_lo > data->tx_bytes)
181 sta->last_tx_bytes_hi++;
182 sta->last_tx_bytes_lo = data->tx_bytes;
183 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
185 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800186 HOSTAPD_LEVEL_DEBUG,
187 "updated TX/RX stats: rx_bytes=%llu [%u:%u] tx_bytes=%llu [%u:%u] bytes_64bit=%d",
188 data->rx_bytes, sta->last_rx_bytes_hi,
189 sta->last_rx_bytes_lo,
190 data->tx_bytes, sta->last_tx_bytes_hi,
191 sta->last_tx_bytes_lo,
192 data->bytes_64bit);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193
194 return 0;
195}
196
197
198static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
199{
200 struct hostapd_data *hapd = eloop_ctx;
201 struct sta_info *sta = timeout_ctx;
202 int interval;
203
204 if (sta->acct_interim_interval) {
205 accounting_sta_interim(hapd, sta);
206 interval = sta->acct_interim_interval;
207 } else {
208 struct hostap_sta_driver_data data;
209 accounting_sta_update_stats(hapd, sta, &data);
210 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
211 }
212
213 eloop_register_timeout(interval, 0, accounting_interim_update,
214 hapd, sta);
215}
216
217
218/**
219 * accounting_sta_start - Start STA accounting
220 * @hapd: hostapd BSS data
221 * @sta: The station
222 */
223void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
224{
225 struct radius_msg *msg;
226 int interval;
227
228 if (sta->acct_session_started)
229 return;
230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
232 HOSTAPD_LEVEL_INFO,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800233 "starting accounting session %016llX",
234 (unsigned long long) sta->acct_session_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800236 os_get_reltime(&sta->acct_session_start);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800237 sta->last_rx_bytes_hi = 0;
238 sta->last_rx_bytes_lo = 0;
239 sta->last_tx_bytes_hi = 0;
240 sta->last_tx_bytes_lo = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 hostapd_drv_sta_clear_stats(hapd, sta->addr);
242
243 if (!hapd->conf->radius->acct_server)
244 return;
245
246 if (sta->acct_interim_interval)
247 interval = sta->acct_interim_interval;
248 else
249 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
250 eloop_register_timeout(interval, 0, accounting_interim_update,
251 hapd, sta);
252
253 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700254 if (msg &&
255 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
256 radius_msg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257
258 sta->acct_session_started = 1;
259}
260
261
262static void accounting_sta_report(struct hostapd_data *hapd,
263 struct sta_info *sta, int stop)
264{
265 struct radius_msg *msg;
266 int cause = sta->acct_terminate_cause;
267 struct hostap_sta_driver_data data;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800268 struct os_reltime now_r, diff;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800269 u64 bytes;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270
271 if (!hapd->conf->radius->acct_server)
272 return;
273
274 msg = accounting_msg(hapd, sta,
275 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
276 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
277 if (!msg) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800278 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 return;
280 }
281
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800282 os_get_reltime(&now_r);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800285 diff.sec)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800286 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 goto fail;
288 }
289
290 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
291 if (!radius_msg_add_attr_int32(msg,
292 RADIUS_ATTR_ACCT_INPUT_PACKETS,
293 data.rx_packets)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800294 wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 goto fail;
296 }
297 if (!radius_msg_add_attr_int32(msg,
298 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
299 data.tx_packets)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800300 wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 goto fail;
302 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800303 if (data.bytes_64bit)
304 bytes = data.rx_bytes;
305 else
306 bytes = ((u64) sta->last_rx_bytes_hi << 32) |
307 sta->last_rx_bytes_lo;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 if (!radius_msg_add_attr_int32(msg,
309 RADIUS_ATTR_ACCT_INPUT_OCTETS,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800310 (u32) bytes)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800311 wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 goto fail;
313 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800314 if (!radius_msg_add_attr_int32(msg,
315 RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
316 (u32) (bytes >> 32))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800317 wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 goto fail;
319 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800320 if (data.bytes_64bit)
321 bytes = data.tx_bytes;
322 else
323 bytes = ((u64) sta->last_tx_bytes_hi << 32) |
324 sta->last_tx_bytes_lo;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 if (!radius_msg_add_attr_int32(msg,
326 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800327 (u32) bytes)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800328 wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 goto fail;
330 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800331 if (!radius_msg_add_attr_int32(msg,
332 RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
333 (u32) (bytes >> 32))) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800334 wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 goto fail;
336 }
337 }
338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 if (eloop_terminated())
340 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
341
342 if (stop && cause &&
343 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
344 cause)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800345 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 goto fail;
347 }
348
Dmitry Shmidt04949592012-07-19 12:16:46 -0700349 if (radius_client_send(hapd->radius, msg,
350 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
351 sta->addr) < 0)
352 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 return;
354
355 fail:
356 radius_msg_free(msg);
357}
358
359
360/**
361 * accounting_sta_interim - Send a interim STA accounting report
362 * @hapd: hostapd BSS data
363 * @sta: The station
364 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700365static void accounting_sta_interim(struct hostapd_data *hapd,
366 struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367{
368 if (sta->acct_session_started)
369 accounting_sta_report(hapd, sta, 0);
370}
371
372
373/**
374 * accounting_sta_stop - Stop STA accounting
375 * @hapd: hostapd BSS data
376 * @sta: The station
377 */
378void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
379{
380 if (sta->acct_session_started) {
381 accounting_sta_report(hapd, sta, 1);
382 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
383 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
384 HOSTAPD_LEVEL_INFO,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800385 "stopped accounting session %016llX",
386 (unsigned long long) sta->acct_session_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 sta->acct_session_started = 0;
388 }
389}
390
391
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800392int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393{
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800394 return radius_gen_session_id((u8 *) &sta->acct_session_id,
395 sizeof(sta->acct_session_id));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396}
397
398
399/**
400 * accounting_receive - Process the RADIUS frames from Accounting Server
401 * @msg: RADIUS response message
402 * @req: RADIUS request message
403 * @shared_secret: RADIUS shared secret
404 * @shared_secret_len: Length of shared_secret in octets
405 * @data: Context data (struct hostapd_data *)
406 * Returns: Processing status
407 */
408static RadiusRxResult
409accounting_receive(struct radius_msg *msg, struct radius_msg *req,
410 const u8 *shared_secret, size_t shared_secret_len,
411 void *data)
412{
413 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800414 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 return RADIUS_RX_UNKNOWN;
416 }
417
418 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800419 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 return RADIUS_RX_INVALID_AUTHENTICATOR;
421 }
422
423 return RADIUS_RX_PROCESSED;
424}
425
426
427static void accounting_report_state(struct hostapd_data *hapd, int on)
428{
429 struct radius_msg *msg;
430
431 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
432 return;
433
434 /* Inform RADIUS server that accounting will start/stop so that the
435 * server can close old accounting sessions. */
436 msg = accounting_msg(hapd, NULL,
437 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
438 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
439 if (!msg)
440 return;
441
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800442 if (hapd->acct_session_id) {
443 char buf[20];
444
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800445 os_snprintf(buf, sizeof(buf), "%016llX",
446 (unsigned long long) hapd->acct_session_id);
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800447 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
448 (u8 *) buf, os_strlen(buf)))
449 wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 }
451
Dmitry Shmidt04949592012-07-19 12:16:46 -0700452 if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
453 radius_msg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454}
455
456
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800457static void accounting_interim_error_cb(const u8 *addr, void *ctx)
458{
459 struct hostapd_data *hapd = ctx;
460 struct sta_info *sta;
461 unsigned int i, wait_time;
462 int res;
463
464 sta = ap_get_sta(hapd, addr);
465 if (!sta)
466 return;
467 sta->acct_interim_errors++;
468 if (sta->acct_interim_errors > 10 /* RADIUS_CLIENT_MAX_RETRIES */) {
469 wpa_printf(MSG_DEBUG,
470 "Interim RADIUS accounting update failed for " MACSTR
471 " - too many errors, abandon this interim accounting update",
472 MAC2STR(addr));
473 sta->acct_interim_errors = 0;
474 /* Next update will be tried after normal update interval */
475 return;
476 }
477
478 /*
479 * Use a shorter update interval as an improved retransmission mechanism
480 * for failed interim accounting updates. This allows the statistics to
481 * be updated for each retransmission.
482 *
483 * RADIUS client code has already waited RADIUS_CLIENT_FIRST_WAIT.
484 * Schedule the first retry attempt immediately and every following one
485 * with exponential backoff.
486 */
487 if (sta->acct_interim_errors == 1) {
488 wait_time = 0;
489 } else {
490 wait_time = 3; /* RADIUS_CLIENT_FIRST_WAIT */
491 for (i = 1; i < sta->acct_interim_errors; i++)
492 wait_time *= 2;
493 }
494 res = eloop_deplete_timeout(wait_time, 0, accounting_interim_update,
495 hapd, sta);
496 if (res == 1)
497 wpa_printf(MSG_DEBUG,
498 "Interim RADIUS accounting update failed for " MACSTR
499 " (error count: %u) - schedule next update in %u seconds",
500 MAC2STR(addr), sta->acct_interim_errors, wait_time);
501 else if (res == 0)
502 wpa_printf(MSG_DEBUG,
503 "Interim RADIUS accounting update failed for " MACSTR
504 " (error count: %u)", MAC2STR(addr),
505 sta->acct_interim_errors);
506 else
507 wpa_printf(MSG_DEBUG,
508 "Interim RADIUS accounting update failed for " MACSTR
509 " (error count: %u) - no timer found", MAC2STR(addr),
510 sta->acct_interim_errors);
511}
512
513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514/**
515 * accounting_init: Initialize accounting
516 * @hapd: hostapd BSS data
517 * Returns: 0 on success, -1 on failure
518 */
519int accounting_init(struct hostapd_data *hapd)
520{
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800521 if (radius_gen_session_id((u8 *) &hapd->acct_session_id,
522 sizeof(hapd->acct_session_id)) < 0)
523 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524
525 if (radius_client_register(hapd->radius, RADIUS_ACCT,
526 accounting_receive, hapd))
527 return -1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800528 radius_client_set_interim_error_cb(hapd->radius,
529 accounting_interim_error_cb, hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530
531 accounting_report_state(hapd, 1);
532
533 return 0;
534}
535
536
537/**
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -0700538 * accounting_deinit: Deinitialize accounting
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 * @hapd: hostapd BSS data
540 */
541void accounting_deinit(struct hostapd_data *hapd)
542{
543 accounting_report_state(hapd, 0);
544}