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