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
12 * See README and COPYING for more details.
20 #include "radius_client.h"
22 #include "accounting.h"
23 #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 /* 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
,
41 struct radius_msg
*msg
;
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 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");
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 (!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");
83 val
= ieee802_1x_get_identity(sta
->eapol_sm
, &len
);
85 snprintf(buf
, sizeof(buf
), RADIUS_ADDR_FORMAT
,
91 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
, val
,
93 printf("Could not add User-Name\n");
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");
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");
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");
123 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT
, sta
->aid
)) {
124 printf("Could not add NAS-Port\n");
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");
137 snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
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");
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");
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");
163 val
= ieee802_1x_get_radius_class(sta
->eapol_sm
, &len
,
168 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CLASS
,
170 printf("Could not add Class\n");
179 radius_msg_free(msg
);
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
))
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
);
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
;
216 if (sta
->acct_interim_interval
) {
217 accounting_sta_interim(hapd
, sta
);
218 interval
= sta
->acct_interim_interval
;
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
,
230 void accounting_sta_start(struct hostapd_data
*hapd
, struct sta_info
*sta
)
232 struct radius_msg
*msg
;
235 if (sta
->acct_session_started
)
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
)
246 if (sta
->acct_interim_interval
)
247 interval
= sta
->acct_interim_interval
;
249 interval
= ACCT_DEFAULT_UPDATE_INTERVAL
;
250 eloop_register_timeout(interval
, 0, accounting_interim_update
,
253 msg
= accounting_msg(hapd
, sta
, RADIUS_ACCT_STATUS_TYPE_START
);
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
,
264 struct radius_msg
*msg
;
265 int cause
= sta
->acct_terminate_cause
;
266 struct hostap_sta_driver_data data
;
269 if (!hapd
->conf
->radius
->acct_server
)
272 msg
= accounting_msg(hapd
, sta
,
273 stop
? RADIUS_ACCT_STATUS_TYPE_STOP
:
274 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE
);
276 printf("Could not create RADIUS Accounting message\n");
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");
286 if (accounting_sta_update_stats(hapd
, sta
, &data
) == 0) {
287 if (!radius_msg_add_attr_int32(msg
,
288 RADIUS_ATTR_ACCT_INPUT_PACKETS
,
290 printf("Could not add Acct-Input-Packets\n");
293 if (!radius_msg_add_attr_int32(msg
,
294 RADIUS_ATTR_ACCT_OUTPUT_PACKETS
,
296 printf("Could not add Acct-Output-Packets\n");
299 if (!radius_msg_add_attr_int32(msg
,
300 RADIUS_ATTR_ACCT_INPUT_OCTETS
,
302 printf("Could not add Acct-Input-Octets\n");
305 gigawords
= sta
->acct_input_gigawords
;
307 gigawords
+= data
.rx_bytes
>> 32;
310 !radius_msg_add_attr_int32(
311 msg
, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS
,
313 printf("Could not add Acct-Input-Gigawords\n");
316 if (!radius_msg_add_attr_int32(msg
,
317 RADIUS_ATTR_ACCT_OUTPUT_OCTETS
,
319 printf("Could not add Acct-Output-Octets\n");
322 gigawords
= sta
->acct_output_gigawords
;
324 gigawords
+= data
.tx_bytes
>> 32;
327 !radius_msg_add_attr_int32(
328 msg
, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS
,
330 printf("Could not add Acct-Output-Gigawords\n");
335 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_EVENT_TIMESTAMP
,
337 printf("Could not add Event-Timestamp\n");
341 if (eloop_terminated())
342 cause
= RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT
;
345 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_ACCT_TERMINATE_CAUSE
,
347 printf("Could not add Acct-Terminate-Cause\n");
351 radius_client_send(hapd
->radius
, msg
,
352 stop
? RADIUS_ACCT
: RADIUS_ACCT_INTERIM
,
357 radius_msg_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
)
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
);
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
);
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
))
447 accounting_report_state(hapd
, 1);
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
)
465 accounting_deinit(hapd
);
466 return accounting_init(hapd
);