7fda7bcec099bb3269f8a0eb5f0801cb94d15508
[libeap.git] / hostapd / accounting.c
1 /*
2  * hostapd / RADIUS Accounting
3  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "hostapd.h"
18 #include "radius/radius.h"
19 #include "radius/radius_client.h"
20 #include "eloop.h"
21 #include "accounting.h"
22 #include "ieee802_1x.h"
23 #include "driver.h"
24
25
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
30
31 /* from ieee802_1x.c */
32 const char *radius_mode_txt(struct hostapd_data *hapd);
33 int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta);
34
35
36 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
37                                           struct sta_info *sta,
38                                           int status_type)
39 {
40         struct radius_msg *msg;
41         char buf[128];
42         u8 *val;
43         size_t len;
44         int i;
45
46         msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
47                              radius_client_get_id(hapd->radius));
48         if (msg == NULL) {
49                 printf("Could not create net RADIUS packet\n");
50                 return NULL;
51         }
52
53         if (sta) {
54                 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
55
56                 os_snprintf(buf, sizeof(buf), "%08X-%08X",
57                             sta->acct_session_id_hi, sta->acct_session_id_lo);
58                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
59                                          (u8 *) buf, os_strlen(buf))) {
60                         printf("Could not add Acct-Session-Id\n");
61                         goto fail;
62                 }
63         } else {
64                 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
65         }
66
67         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
68                                        status_type)) {
69                 printf("Could not add Acct-Status-Type\n");
70                 goto fail;
71         }
72
73         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
74                                        hapd->conf->ieee802_1x ?
75                                        RADIUS_ACCT_AUTHENTIC_RADIUS :
76                                        RADIUS_ACCT_AUTHENTIC_LOCAL)) {
77                 printf("Could not add Acct-Authentic\n");
78                 goto fail;
79         }
80
81         if (sta) {
82                 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
83                 if (!val) {
84                         os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
85                                     MAC2STR(sta->addr));
86                         val = (u8 *) buf;
87                         len = os_strlen(buf);
88                 }
89
90                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
91                                          len)) {
92                         printf("Could not add User-Name\n");
93                         goto fail;
94                 }
95         }
96
97         if (hapd->conf->own_ip_addr.af == AF_INET &&
98             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
99                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
100                 printf("Could not add NAS-IP-Address\n");
101                 goto fail;
102         }
103
104 #ifdef CONFIG_IPV6
105         if (hapd->conf->own_ip_addr.af == AF_INET6 &&
106             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
107                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
108                 printf("Could not add NAS-IPv6-Address\n");
109                 goto fail;
110         }
111 #endif /* CONFIG_IPV6 */
112
113         if (hapd->conf->nas_identifier &&
114             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
115                                  (u8 *) hapd->conf->nas_identifier,
116                                  os_strlen(hapd->conf->nas_identifier))) {
117                 printf("Could not add NAS-Identifier\n");
118                 goto fail;
119         }
120
121         if (sta &&
122             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
123                 printf("Could not add NAS-Port\n");
124                 goto fail;
125         }
126
127         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
128                     MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
129         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
130                                  (u8 *) buf, os_strlen(buf))) {
131                 printf("Could not add Called-Station-Id\n");
132                 goto fail;
133         }
134
135         if (sta) {
136                 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
137                             MAC2STR(sta->addr));
138                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
139                                          (u8 *) buf, os_strlen(buf))) {
140                         printf("Could not add Calling-Station-Id\n");
141                         goto fail;
142                 }
143
144                 if (!radius_msg_add_attr_int32(
145                             msg, RADIUS_ATTR_NAS_PORT_TYPE,
146                             RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
147                         printf("Could not add NAS-Port-Type\n");
148                         goto fail;
149                 }
150
151                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
152                             radius_sta_rate(hapd, sta) / 2,
153                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
154                             radius_mode_txt(hapd));
155                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
156                                          (u8 *) buf, os_strlen(buf))) {
157                         printf("Could not add Connect-Info\n");
158                         goto fail;
159                 }
160
161                 for (i = 0; ; i++) {
162                         val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
163                                                           i);
164                         if (val == NULL)
165                                 break;
166
167                         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
168                                                  val, len)) {
169                                 printf("Could not add Class\n");
170                                 goto fail;
171                         }
172                 }
173         }
174
175         return msg;
176
177  fail:
178         radius_msg_free(msg);
179         os_free(msg);
180         return NULL;
181 }
182
183
184 static int accounting_sta_update_stats(struct hostapd_data *hapd,
185                                        struct sta_info *sta,
186                                        struct hostap_sta_driver_data *data)
187 {
188         if (hostapd_read_sta_data(hapd, data, sta->addr))
189                 return -1;
190
191         if (sta->last_rx_bytes > data->rx_bytes)
192                 sta->acct_input_gigawords++;
193         if (sta->last_tx_bytes > data->tx_bytes)
194                 sta->acct_output_gigawords++;
195         sta->last_rx_bytes = data->rx_bytes;
196         sta->last_tx_bytes = data->tx_bytes;
197
198         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
199                        HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
200                        "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
201                        "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
202                        sta->last_rx_bytes, sta->acct_input_gigawords,
203                        sta->last_tx_bytes, sta->acct_output_gigawords);
204
205         return 0;
206 }
207
208
209 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
210 {
211         struct hostapd_data *hapd = eloop_ctx;
212         struct sta_info *sta = timeout_ctx;
213         int interval;
214
215         if (sta->acct_interim_interval) {
216                 accounting_sta_interim(hapd, sta);
217                 interval = sta->acct_interim_interval;
218         } else {
219                 struct hostap_sta_driver_data data;
220                 accounting_sta_update_stats(hapd, sta, &data);
221                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
222         }
223
224         eloop_register_timeout(interval, 0, accounting_interim_update,
225                                hapd, sta);
226 }
227
228
229 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
230 {
231         struct radius_msg *msg;
232         int interval;
233
234         if (sta->acct_session_started)
235                 return;
236
237         time(&sta->acct_session_start);
238         sta->last_rx_bytes = sta->last_tx_bytes = 0;
239         sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
240         hostapd_sta_clear_stats(hapd, sta->addr);
241
242         if (!hapd->conf->radius->acct_server)
243                 return;
244
245         if (sta->acct_interim_interval)
246                 interval = sta->acct_interim_interval;
247         else
248                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
249         eloop_register_timeout(interval, 0, accounting_interim_update,
250                                hapd, sta);
251
252         msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
253         if (msg)
254                 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
255
256         sta->acct_session_started = 1;
257 }
258
259
260 void accounting_sta_report(struct hostapd_data *hapd, struct sta_info *sta,
261                            int stop)
262 {
263         struct radius_msg *msg;
264         int cause = sta->acct_terminate_cause;
265         struct hostap_sta_driver_data data;
266         u32 gigawords;
267
268         if (!hapd->conf->radius->acct_server)
269                 return;
270
271         msg = accounting_msg(hapd, sta,
272                              stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
273                              RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
274         if (!msg) {
275                 printf("Could not create RADIUS Accounting message\n");
276                 return;
277         }
278
279         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
280                                        time(NULL) - sta->acct_session_start)) {
281                 printf("Could not add Acct-Session-Time\n");
282                 goto fail;
283         }
284
285         if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
286                 if (!radius_msg_add_attr_int32(msg,
287                                                RADIUS_ATTR_ACCT_INPUT_PACKETS,
288                                                data.rx_packets)) {
289                         printf("Could not add Acct-Input-Packets\n");
290                         goto fail;
291                 }
292                 if (!radius_msg_add_attr_int32(msg,
293                                                RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
294                                                data.tx_packets)) {
295                         printf("Could not add Acct-Output-Packets\n");
296                         goto fail;
297                 }
298                 if (!radius_msg_add_attr_int32(msg,
299                                                RADIUS_ATTR_ACCT_INPUT_OCTETS,
300                                                data.rx_bytes)) {
301                         printf("Could not add Acct-Input-Octets\n");
302                         goto fail;
303                 }
304                 gigawords = sta->acct_input_gigawords;
305 #if __WORDSIZE == 64
306                 gigawords += data.rx_bytes >> 32;
307 #endif
308                 if (gigawords &&
309                     !radius_msg_add_attr_int32(
310                             msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
311                             gigawords)) {
312                         printf("Could not add Acct-Input-Gigawords\n");
313                         goto fail;
314                 }
315                 if (!radius_msg_add_attr_int32(msg,
316                                                RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
317                                                data.tx_bytes)) {
318                         printf("Could not add Acct-Output-Octets\n");
319                         goto fail;
320                 }
321                 gigawords = sta->acct_output_gigawords;
322 #if __WORDSIZE == 64
323                 gigawords += data.tx_bytes >> 32;
324 #endif
325                 if (gigawords &&
326                     !radius_msg_add_attr_int32(
327                             msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
328                             gigawords)) {
329                         printf("Could not add Acct-Output-Gigawords\n");
330                         goto fail;
331                 }
332         }
333
334         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
335                                        time(NULL))) {
336                 printf("Could not add Event-Timestamp\n");
337                 goto fail;
338         }
339
340         if (eloop_terminated())
341                 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
342
343         if (stop && cause &&
344             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
345                                        cause)) {
346                 printf("Could not add Acct-Terminate-Cause\n");
347                 goto fail;
348         }
349
350         radius_client_send(hapd->radius, msg,
351                            stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
352                            sta->addr);
353         return;
354
355  fail:
356         radius_msg_free(msg);
357         os_free(msg);
358 }
359
360
361 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
362 {
363         if (sta->acct_session_started)
364                 accounting_sta_report(hapd, sta, 0);
365 }
366
367
368 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
369 {
370         if (sta->acct_session_started) {
371                 accounting_sta_report(hapd, sta, 1);
372                 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
373                 sta->acct_session_started = 0;
374         }
375 }
376
377
378 void accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
379 {
380         sta->acct_session_id_lo = hapd->acct_session_id_lo++;
381         if (hapd->acct_session_id_lo == 0) {
382                 hapd->acct_session_id_hi++;
383         }
384         sta->acct_session_id_hi = hapd->acct_session_id_hi;
385 }
386
387
388 /* Process the RADIUS frames from Accounting Server */
389 static RadiusRxResult
390 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
391                    u8 *shared_secret, size_t shared_secret_len, void *data)
392 {
393         if (msg->hdr->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
394                 printf("Unknown RADIUS message code\n");
395                 return RADIUS_RX_UNKNOWN;
396         }
397
398         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
399                 printf("Incoming RADIUS packet did not have correct "
400                        "Authenticator - dropped\n");
401                 return RADIUS_RX_INVALID_AUTHENTICATOR;
402         }
403
404         return RADIUS_RX_PROCESSED;
405 }
406
407
408 static void accounting_report_state(struct hostapd_data *hapd, int on)
409 {
410         struct radius_msg *msg;
411
412         if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
413                 return;
414
415         /* Inform RADIUS server that accounting will start/stop so that the
416          * server can close old accounting sessions. */
417         msg = accounting_msg(hapd, NULL,
418                              on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
419                              RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
420         if (!msg)
421                 return;
422
423         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
424                                        RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
425         {
426                 printf("Could not add Acct-Terminate-Cause\n");
427                 radius_msg_free(msg);
428                 os_free(msg);
429                 return;
430         }
431
432         radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
433 }
434
435
436 int accounting_init(struct hostapd_data *hapd)
437 {
438         /* Acct-Session-Id should be unique over reboots. If reliable clock is
439          * not available, this could be replaced with reboot counter, etc. */
440         hapd->acct_session_id_hi = time(NULL);
441
442         if (radius_client_register(hapd->radius, RADIUS_ACCT,
443                                    accounting_receive, hapd))
444                 return -1;
445
446         accounting_report_state(hapd, 1);
447
448         return 0;
449 }
450
451
452 void accounting_deinit(struct hostapd_data *hapd)
453 {
454         accounting_report_state(hapd, 0);
455 }
456
457
458 int accounting_reconfig(struct hostapd_data *hapd,
459                         struct hostapd_config *oldconf)
460 {
461         if (!hapd->radius_client_reconfigured)
462                 return 0;
463
464         accounting_deinit(hapd);
465         return accounting_init(hapd);
466 }