hostapd: remove version tag from directory
[dragonfly.git] / contrib / hostapd / accounting.c
blobb22347b2ac2e1956b4a4779d9dd0841f7107ef56
1 /*
2 * hostapd / RADIUS Accounting
3 * Copyright (c) 2002-2005, 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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <assert.h>
18 #include "hostapd.h"
19 #include "radius.h"
20 #include "radius_client.h"
21 #include "eloop.h"
22 #include "accounting.h"
23 #include "ieee802_1x.h"
24 #include "driver.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 /* from ieee802_1x.c */
33 const char *radius_mode_txt(struct hostapd_data *hapd);
34 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta);
37 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
38 struct sta_info *sta,
39 int status_type)
41 struct radius_msg *msg;
42 char buf[128];
43 u8 *val;
44 size_t len;
45 int i;
47 msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
48 radius_client_get_id(hapd->radius));
49 if (msg == NULL) {
50 printf("Could not create net RADIUS packet\n");
51 return NULL;
54 if (sta) {
55 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
57 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, strlen(buf))) {
61 printf("Could not add Acct-Session-Id\n");
62 goto fail;
64 } else {
65 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
68 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
69 status_type)) {
70 printf("Could not add Acct-Status-Type\n");
71 goto fail;
74 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
75 hapd->conf->ieee802_1x ?
76 RADIUS_ACCT_AUTHENTIC_RADIUS :
77 RADIUS_ACCT_AUTHENTIC_LOCAL)) {
78 printf("Could not add Acct-Authentic\n");
79 goto fail;
82 if (sta) {
83 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
84 if (!val) {
85 snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
86 MAC2STR(sta->addr));
87 val = (u8 *) buf;
88 len = strlen(buf);
91 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
92 len)) {
93 printf("Could not add User-Name\n");
94 goto fail;
98 if (hapd->conf->own_ip_addr.af == AF_INET &&
99 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
100 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
101 printf("Could not add NAS-IP-Address\n");
102 goto fail;
105 #ifdef CONFIG_IPV6
106 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
107 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
108 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
109 printf("Could not add NAS-IPv6-Address\n");
110 goto fail;
112 #endif /* CONFIG_IPV6 */
114 if (hapd->conf->nas_identifier &&
115 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
116 (u8 *) hapd->conf->nas_identifier,
117 strlen(hapd->conf->nas_identifier))) {
118 printf("Could not add NAS-Identifier\n");
119 goto fail;
122 if (sta &&
123 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
124 printf("Could not add NAS-Port\n");
125 goto fail;
128 snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
129 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
130 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
131 (u8 *) buf, strlen(buf))) {
132 printf("Could not add Called-Station-Id\n");
133 goto fail;
136 if (sta) {
137 snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
138 MAC2STR(sta->addr));
139 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
140 (u8 *) buf, strlen(buf))) {
141 printf("Could not add Calling-Station-Id\n");
142 goto fail;
145 if (!radius_msg_add_attr_int32(
146 msg, RADIUS_ATTR_NAS_PORT_TYPE,
147 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
148 printf("Could not add NAS-Port-Type\n");
149 goto fail;
152 snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
153 radius_sta_rate(hapd, sta) / 2,
154 (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
155 radius_mode_txt(hapd));
156 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
157 (u8 *) buf, strlen(buf))) {
158 printf("Could not add Connect-Info\n");
159 goto fail;
162 for (i = 0; ; i++) {
163 val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
165 if (val == NULL)
166 break;
168 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
169 val, len)) {
170 printf("Could not add Class\n");
171 goto fail;
176 return msg;
178 fail:
179 radius_msg_free(msg);
180 free(msg);
181 return NULL;
185 static int accounting_sta_update_stats(struct hostapd_data *hapd,
186 struct sta_info *sta,
187 struct hostap_sta_driver_data *data)
189 if (hostapd_read_sta_data(hapd, data, sta->addr))
190 return -1;
192 if (sta->last_rx_bytes > data->rx_bytes)
193 sta->acct_input_gigawords++;
194 if (sta->last_tx_bytes > data->tx_bytes)
195 sta->acct_output_gigawords++;
196 sta->last_rx_bytes = data->rx_bytes;
197 sta->last_tx_bytes = data->tx_bytes;
199 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
200 HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
201 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
202 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
203 sta->last_rx_bytes, sta->acct_input_gigawords,
204 sta->last_tx_bytes, sta->acct_output_gigawords);
206 return 0;
210 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
212 struct hostapd_data *hapd = eloop_ctx;
213 struct sta_info *sta = timeout_ctx;
214 int interval;
216 if (sta->acct_interim_interval) {
217 accounting_sta_interim(hapd, sta);
218 interval = sta->acct_interim_interval;
219 } else {
220 struct hostap_sta_driver_data data;
221 accounting_sta_update_stats(hapd, sta, &data);
222 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
225 eloop_register_timeout(interval, 0, accounting_interim_update,
226 hapd, sta);
230 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
232 struct radius_msg *msg;
233 int interval;
235 if (sta->acct_session_started)
236 return;
238 time(&sta->acct_session_start);
239 sta->last_rx_bytes = sta->last_tx_bytes = 0;
240 sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
241 hostapd_sta_clear_stats(hapd, sta->addr);
243 if (!hapd->conf->radius->acct_server)
244 return;
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);
253 msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
254 if (msg)
255 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
257 sta->acct_session_started = 1;
261 void accounting_sta_report(struct hostapd_data *hapd, struct sta_info *sta,
262 int stop)
264 struct radius_msg *msg;
265 int cause = sta->acct_terminate_cause;
266 struct hostap_sta_driver_data data;
267 u32 gigawords;
269 if (!hapd->conf->radius->acct_server)
270 return;
272 msg = accounting_msg(hapd, sta,
273 stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
274 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
275 if (!msg) {
276 printf("Could not create RADIUS Accounting message\n");
277 return;
280 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
281 time(NULL) - sta->acct_session_start)) {
282 printf("Could not add Acct-Session-Time\n");
283 goto fail;
286 if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
287 if (!radius_msg_add_attr_int32(msg,
288 RADIUS_ATTR_ACCT_INPUT_PACKETS,
289 data.rx_packets)) {
290 printf("Could not add Acct-Input-Packets\n");
291 goto fail;
293 if (!radius_msg_add_attr_int32(msg,
294 RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
295 data.tx_packets)) {
296 printf("Could not add Acct-Output-Packets\n");
297 goto fail;
299 if (!radius_msg_add_attr_int32(msg,
300 RADIUS_ATTR_ACCT_INPUT_OCTETS,
301 data.rx_bytes)) {
302 printf("Could not add Acct-Input-Octets\n");
303 goto fail;
305 gigawords = sta->acct_input_gigawords;
306 #if __WORDSIZE == 64
307 gigawords += data.rx_bytes >> 32;
308 #endif
309 if (gigawords &&
310 !radius_msg_add_attr_int32(
311 msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
312 gigawords)) {
313 printf("Could not add Acct-Input-Gigawords\n");
314 goto fail;
316 if (!radius_msg_add_attr_int32(msg,
317 RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
318 data.tx_bytes)) {
319 printf("Could not add Acct-Output-Octets\n");
320 goto fail;
322 gigawords = sta->acct_output_gigawords;
323 #if __WORDSIZE == 64
324 gigawords += data.tx_bytes >> 32;
325 #endif
326 if (gigawords &&
327 !radius_msg_add_attr_int32(
328 msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
329 gigawords)) {
330 printf("Could not add Acct-Output-Gigawords\n");
331 goto fail;
335 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
336 time(NULL))) {
337 printf("Could not add Event-Timestamp\n");
338 goto fail;
341 if (eloop_terminated())
342 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
344 if (stop && cause &&
345 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
346 cause)) {
347 printf("Could not add Acct-Terminate-Cause\n");
348 goto fail;
351 radius_client_send(hapd->radius, msg,
352 stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
353 sta->addr);
354 return;
356 fail:
357 radius_msg_free(msg);
358 free(msg);
362 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
364 if (sta->acct_session_started)
365 accounting_sta_report(hapd, sta, 0);
369 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
371 if (sta->acct_session_started) {
372 accounting_sta_report(hapd, sta, 1);
373 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
374 sta->acct_session_started = 0;
379 void accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
381 sta->acct_session_id_lo = hapd->acct_session_id_lo++;
382 if (hapd->acct_session_id_lo == 0) {
383 hapd->acct_session_id_hi++;
385 sta->acct_session_id_hi = hapd->acct_session_id_hi;
389 /* Process the RADIUS frames from Accounting Server */
390 static RadiusRxResult
391 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
392 u8 *shared_secret, size_t shared_secret_len, void *data)
394 if (msg->hdr->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
395 printf("Unknown RADIUS message code\n");
396 return RADIUS_RX_UNKNOWN;
399 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
400 printf("Incoming RADIUS packet did not have correct "
401 "Authenticator - dropped\n");
402 return RADIUS_RX_INVALID_AUTHENTICATOR;
405 return RADIUS_RX_PROCESSED;
409 static void accounting_report_state(struct hostapd_data *hapd, int on)
411 struct radius_msg *msg;
413 if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
414 return;
416 /* Inform RADIUS server that accounting will start/stop so that the
417 * server can close old accounting sessions. */
418 msg = accounting_msg(hapd, NULL,
419 on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
420 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
421 if (!msg)
422 return;
424 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
425 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
427 printf("Could not add Acct-Terminate-Cause\n");
428 radius_msg_free(msg);
429 free(msg);
430 return;
433 radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
437 int accounting_init(struct hostapd_data *hapd)
439 /* Acct-Session-Id should be unique over reboots. If reliable clock is
440 * not available, this could be replaced with reboot counter, etc. */
441 hapd->acct_session_id_hi = time(NULL);
443 if (radius_client_register(hapd->radius, RADIUS_ACCT,
444 accounting_receive, hapd))
445 return -1;
447 accounting_report_state(hapd, 1);
449 return 0;
453 void accounting_deinit(struct hostapd_data *hapd)
455 accounting_report_state(hapd, 0);
459 int accounting_reconfig(struct hostapd_data *hapd,
460 struct hostapd_config *oldconf)
462 if (!hapd->radius_client_reconfigured)
463 return 0;
465 accounting_deinit(hapd);
466 return accounting_init(hapd);