2 * Host AP (software wireless LAN access point) user space daemon for
3 * Host AP kernel driver / Accounting
4 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
19 #include <netinet/in.h>
21 #include <sys/ioctl.h>
26 #include <sys/socket.h>
31 #include "radius_client.h"
33 #include "accounting.h"
34 #include "ieee802_1x.h"
38 /* Default interval in seconds for polling TX/RX octets from the driver if
39 * STA is not using interim accounting. This detects wrap arounds for
40 * input/output octets and updates Acct-{Input,Output}-Gigawords. */
41 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
43 static struct radius_msg
* accounting_msg(hostapd
*hapd
, struct sta_info
*sta
,
46 struct radius_msg
*msg
;
52 msg
= radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST
,
53 radius_client_get_id(hapd
->radius
));
55 printf("Could not create net RADIUS packet\n");
60 radius_msg_make_authenticator(msg
, (u8
*) sta
, sizeof(*sta
));
62 snprintf(buf
, sizeof(buf
), "%08X-%08X",
63 sta
->acct_session_id_hi
, sta
->acct_session_id_lo
);
64 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_ACCT_SESSION_ID
,
65 (u8
*) buf
, strlen(buf
))) {
66 printf("Could not add Acct-Session-Id\n");
70 radius_msg_make_authenticator(msg
, (u8
*) hapd
, sizeof(*hapd
));
73 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_ACCT_STATUS_TYPE
,
75 printf("Could not add Acct-Status-Type\n");
79 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_ACCT_AUTHENTIC
,
80 hapd
->conf
->ieee802_1x
?
81 RADIUS_ACCT_AUTHENTIC_RADIUS
:
82 RADIUS_ACCT_AUTHENTIC_LOCAL
)) {
83 printf("Could not add Acct-Authentic\n");
88 val
= ieee802_1x_get_identity(sta
->eapol_sm
, &len
);
90 snprintf(buf
, sizeof(buf
), RADIUS_ADDR_FORMAT
,
96 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
, val
,
98 printf("Could not add User-Name\n");
103 if (hapd
->conf
->own_ip_addr
.af
== AF_INET
&&
104 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
105 (u8
*) &hapd
->conf
->own_ip_addr
.u
.v4
, 4)) {
106 printf("Could not add NAS-IP-Address\n");
111 if (hapd
->conf
->own_ip_addr
.af
== AF_INET6
&&
112 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IPV6_ADDRESS
,
113 (u8
*) &hapd
->conf
->own_ip_addr
.u
.v6
, 16)) {
114 printf("Could not add NAS-IPv6-Address\n");
117 #endif /* CONFIG_IPV6 */
119 if (hapd
->conf
->nas_identifier
&&
120 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IDENTIFIER
,
121 (u8
*) hapd
->conf
->nas_identifier
,
122 strlen(hapd
->conf
->nas_identifier
))) {
123 printf("Could not add NAS-Identifier\n");
128 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT
, sta
->aid
)) {
129 printf("Could not add NAS-Port\n");
133 snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
":%s",
134 MAC2STR(hapd
->own_addr
), hapd
->conf
->ssid
);
135 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLED_STATION_ID
,
136 (u8
*) buf
, strlen(buf
))) {
137 printf("Could not add Called-Station-Id\n");
142 snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
144 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
145 (u8
*) buf
, strlen(buf
))) {
146 printf("Could not add Calling-Station-Id\n");
150 if (!radius_msg_add_attr_int32(
151 msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
152 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
153 printf("Could not add NAS-Port-Type\n");
157 snprintf(buf
, sizeof(buf
), "CONNECT 11Mbps 802.11b");
158 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
159 (u8
*) buf
, strlen(buf
))) {
160 printf("Could not add Connect-Info\n");
165 val
= ieee802_1x_get_radius_class(sta
->eapol_sm
, &len
,
170 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CLASS
,
172 printf("Could not add Class\n");
181 radius_msg_free(msg
);
187 static int accounting_sta_update_stats(struct hostapd_data
*hapd
,
188 struct sta_info
*sta
,
189 struct hostap_sta_driver_data
*data
)
191 if (hostapd_read_sta_data(hapd
, data
, sta
->addr
))
194 if (sta
->last_rx_bytes
> data
->rx_bytes
)
195 sta
->acct_input_gigawords
++;
196 if (sta
->last_tx_bytes
> data
->tx_bytes
)
197 sta
->acct_output_gigawords
++;
198 sta
->last_rx_bytes
= data
->rx_bytes
;
199 sta
->last_tx_bytes
= data
->tx_bytes
;
201 hostapd_logger(hapd
, sta
->addr
, HOSTAPD_MODULE_RADIUS
,
202 HOSTAPD_LEVEL_DEBUG
, "updated TX/RX stats: "
203 "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
204 "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
205 sta
->last_rx_bytes
, sta
->acct_input_gigawords
,
206 sta
->last_tx_bytes
, sta
->acct_output_gigawords
);
212 static void accounting_interim_update(void *eloop_ctx
, void *timeout_ctx
)
214 hostapd
*hapd
= eloop_ctx
;
215 struct sta_info
*sta
= timeout_ctx
;
218 if (sta
->acct_interim_interval
) {
219 accounting_sta_interim(hapd
, sta
);
220 interval
= sta
->acct_interim_interval
;
222 struct hostap_sta_driver_data data
;
223 accounting_sta_update_stats(hapd
, sta
, &data
);
224 interval
= ACCT_DEFAULT_UPDATE_INTERVAL
;
227 eloop_register_timeout(interval
, 0, accounting_interim_update
,
232 void accounting_sta_start(hostapd
*hapd
, struct sta_info
*sta
)
234 struct radius_msg
*msg
;
237 if (sta
->acct_session_started
)
240 time(&sta
->acct_session_start
);
241 sta
->last_rx_bytes
= sta
->last_tx_bytes
= 0;
242 sta
->acct_input_gigawords
= sta
->acct_output_gigawords
= 0;
243 hostapd_sta_clear_stats(hapd
, sta
->addr
);
245 if (!hapd
->conf
->radius
->acct_server
)
248 if (sta
->acct_interim_interval
)
249 interval
= sta
->acct_interim_interval
;
251 interval
= ACCT_DEFAULT_UPDATE_INTERVAL
;
252 eloop_register_timeout(interval
, 0, accounting_interim_update
,
255 msg
= accounting_msg(hapd
, sta
, RADIUS_ACCT_STATUS_TYPE_START
);
257 radius_client_send(hapd
->radius
, msg
, RADIUS_ACCT
, sta
->addr
);
259 sta
->acct_session_started
= 1;
263 void accounting_sta_report(hostapd
*hapd
, struct sta_info
*sta
, int stop
)
265 struct radius_msg
*msg
;
266 int cause
= sta
->acct_terminate_cause
;
267 struct hostap_sta_driver_data data
;
270 if (!hapd
->conf
->radius
->acct_server
)
273 msg
= accounting_msg(hapd
, sta
,
274 stop
? RADIUS_ACCT_STATUS_TYPE_STOP
:
275 RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE
);
277 printf("Could not create RADIUS Accounting message\n");
281 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_ACCT_SESSION_TIME
,
282 time(NULL
) - sta
->acct_session_start
)) {
283 printf("Could not add Acct-Session-Time\n");
287 if (accounting_sta_update_stats(hapd
, sta
, &data
) == 0) {
288 if (!radius_msg_add_attr_int32(msg
,
289 RADIUS_ATTR_ACCT_INPUT_PACKETS
,
291 printf("Could not add Acct-Input-Packets\n");
294 if (!radius_msg_add_attr_int32(msg
,
295 RADIUS_ATTR_ACCT_OUTPUT_PACKETS
,
297 printf("Could not add Acct-Output-Packets\n");
300 if (!radius_msg_add_attr_int32(msg
,
301 RADIUS_ATTR_ACCT_INPUT_OCTETS
,
303 printf("Could not add Acct-Input-Octets\n");
306 gigawords
= sta
->acct_input_gigawords
;
308 gigawords
+= data
.rx_bytes
>> 32;
311 !radius_msg_add_attr_int32(
312 msg
, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS
,
314 printf("Could not add Acct-Input-Gigawords\n");
317 if (!radius_msg_add_attr_int32(msg
,
318 RADIUS_ATTR_ACCT_OUTPUT_OCTETS
,
320 printf("Could not add Acct-Output-Octets\n");
323 gigawords
= sta
->acct_output_gigawords
;
325 gigawords
+= data
.tx_bytes
>> 32;
328 !radius_msg_add_attr_int32(
329 msg
, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS
,
331 printf("Could not add Acct-Output-Gigawords\n");
336 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_EVENT_TIMESTAMP
,
338 printf("Could not add Event-Timestamp\n");
342 if (eloop_terminated())
343 cause
= RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT
;
346 !radius_msg_add_attr_int32(msg
, RADIUS_ATTR_ACCT_TERMINATE_CAUSE
,
348 printf("Could not add Acct-Terminate-Cause\n");
352 radius_client_send(hapd
->radius
, msg
,
353 stop
? RADIUS_ACCT
: RADIUS_ACCT_INTERIM
,
358 radius_msg_free(msg
);
363 void accounting_sta_interim(hostapd
*hapd
, struct sta_info
*sta
)
365 if (sta
->acct_session_started
)
366 accounting_sta_report(hapd
, sta
, 0);
370 void accounting_sta_stop(hostapd
*hapd
, struct sta_info
*sta
)
372 if (sta
->acct_session_started
) {
373 accounting_sta_report(hapd
, sta
, 1);
374 eloop_cancel_timeout(accounting_interim_update
, hapd
, sta
);
375 sta
->acct_session_started
= 0;
380 void accounting_sta_get_id(struct hostapd_data
*hapd
, struct sta_info
*sta
)
382 sta
->acct_session_id_lo
= hapd
->acct_session_id_lo
++;
383 if (hapd
->acct_session_id_lo
== 0) {
384 hapd
->acct_session_id_hi
++;
386 sta
->acct_session_id_hi
= hapd
->acct_session_id_hi
;
390 /* Process the RADIUS frames from Accounting Server */
391 static RadiusRxResult
392 accounting_receive(struct radius_msg
*msg
, struct radius_msg
*req
,
393 u8
*shared_secret
, size_t shared_secret_len
, void *data
)
395 if (msg
->hdr
->code
!= RADIUS_CODE_ACCOUNTING_RESPONSE
) {
396 printf("Unknown RADIUS message code\n");
397 return RADIUS_RX_UNKNOWN
;
400 if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
, req
, 0)) {
401 printf("Incoming RADIUS packet did not have correct "
402 "Authenticator - dropped\n");
403 return RADIUS_RX_INVALID_AUTHENTICATOR
;
406 return RADIUS_RX_PROCESSED
;
410 static void accounting_report_state(struct hostapd_data
*hapd
, int on
)
412 struct radius_msg
*msg
;
414 if (!hapd
->conf
->radius
->acct_server
|| hapd
->radius
== NULL
)
417 /* Inform RADIUS server that accounting will start/stop so that the
418 * server can close old accounting sessions. */
419 msg
= accounting_msg(hapd
, NULL
,
420 on
? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON
:
421 RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF
);
425 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_ACCT_TERMINATE_CAUSE
,
426 RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT
))
428 printf("Could not add Acct-Terminate-Cause\n");
429 radius_msg_free(msg
);
434 radius_client_send(hapd
->radius
, msg
, RADIUS_ACCT
, NULL
);
438 int accounting_init(hostapd
*hapd
)
440 /* Acct-Session-Id should be unique over reboots. If reliable clock is
441 * not available, this could be replaced with reboot counter, etc. */
442 hapd
->acct_session_id_hi
= time(NULL
);
444 if (radius_client_register(hapd
->radius
, RADIUS_ACCT
,
445 accounting_receive
, hapd
))
448 accounting_report_state(hapd
, 1);
454 void accounting_deinit(hostapd
*hapd
)
456 accounting_report_state(hapd
, 0);