Move more driver ops into struct hostapd_driver_ops
[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 "common.h"
18 #include "hostapd.h"
19 #include "radius/radius.h"
20 #include "radius/radius_client.h"
21 #include "eloop.h"
22 #include "accounting.h"
23 #include "ieee802_1x.h"
24 #include "driver_i.h"
25 #include "sta_info.h"
26
27
28 /* Default interval in seconds for polling TX/RX octets from the driver if
29  * STA is not using interim accounting. This detects wrap arounds for
30  * input/output octets and updates Acct-{Input,Output}-Gigawords. */
31 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
32
33 static void accounting_sta_get_id(struct hostapd_data *hapd,
34                                   struct sta_info *sta);
35
36
37 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
38                                           struct sta_info *sta,
39                                           int status_type)
40 {
41         struct radius_msg *msg;
42         char buf[128];
43         u8 *val;
44         size_t len;
45         int i;
46
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;
52         }
53
54         if (sta) {
55                 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
56
57                 os_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, os_strlen(buf))) {
61                         printf("Could not add Acct-Session-Id\n");
62                         goto fail;
63                 }
64         } else {
65                 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
66         }
67
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;
72         }
73
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;
80         }
81
82         if (sta) {
83                 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
84                 if (!val) {
85                         os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
86                                     MAC2STR(sta->addr));
87                         val = (u8 *) buf;
88                         len = os_strlen(buf);
89                 }
90
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;
95                 }
96         }
97
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;
103         }
104
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;
111         }
112 #endif /* CONFIG_IPV6 */
113
114         if (hapd->conf->nas_identifier &&
115             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
116                                  (u8 *) hapd->conf->nas_identifier,
117                                  os_strlen(hapd->conf->nas_identifier))) {
118                 printf("Could not add NAS-Identifier\n");
119                 goto fail;
120         }
121
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;
126         }
127
128         os_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, os_strlen(buf))) {
132                 printf("Could not add Called-Station-Id\n");
133                 goto fail;
134         }
135
136         if (sta) {
137                 os_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, os_strlen(buf))) {
141                         printf("Could not add Calling-Station-Id\n");
142                         goto fail;
143                 }
144
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;
150                 }
151
152                 os_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, os_strlen(buf))) {
158                         printf("Could not add Connect-Info\n");
159                         goto fail;
160                 }
161
162                 for (i = 0; ; i++) {
163                         val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
164                                                           i);
165                         if (val == NULL)
166                                 break;
167
168                         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
169                                                  val, len)) {
170                                 printf("Could not add Class\n");
171                                 goto fail;
172                         }
173                 }
174         }
175
176         return msg;
177
178  fail:
179         radius_msg_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 /**
230  * accounting_sta_start - Start STA accounting
231  * @hapd: hostapd BSS data
232  * @sta: The station
233  */
234 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
235 {
236         struct radius_msg *msg;
237         int interval;
238
239         if (sta->acct_session_started)
240                 return;
241
242         accounting_sta_get_id(hapd, sta);
243         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
244                        HOSTAPD_LEVEL_INFO,
245                        "starting accounting session %08X-%08X",
246                        sta->acct_session_id_hi, sta->acct_session_id_lo);
247
248         time(&sta->acct_session_start);
249         sta->last_rx_bytes = sta->last_tx_bytes = 0;
250         sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
251         hostapd_sta_clear_stats(hapd, sta->addr);
252
253         if (!hapd->conf->radius->acct_server)
254                 return;
255
256         if (sta->acct_interim_interval)
257                 interval = sta->acct_interim_interval;
258         else
259                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
260         eloop_register_timeout(interval, 0, accounting_interim_update,
261                                hapd, sta);
262
263         msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
264         if (msg)
265                 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
266
267         sta->acct_session_started = 1;
268 }
269
270
271 static void accounting_sta_report(struct hostapd_data *hapd,
272                                   struct sta_info *sta, int stop)
273 {
274         struct radius_msg *msg;
275         int cause = sta->acct_terminate_cause;
276         struct hostap_sta_driver_data data;
277         u32 gigawords;
278
279         if (!hapd->conf->radius->acct_server)
280                 return;
281
282         msg = accounting_msg(hapd, sta,
283                              stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
284                              RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
285         if (!msg) {
286                 printf("Could not create RADIUS Accounting message\n");
287                 return;
288         }
289
290         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
291                                        time(NULL) - sta->acct_session_start)) {
292                 printf("Could not add Acct-Session-Time\n");
293                 goto fail;
294         }
295
296         if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
297                 if (!radius_msg_add_attr_int32(msg,
298                                                RADIUS_ATTR_ACCT_INPUT_PACKETS,
299                                                data.rx_packets)) {
300                         printf("Could not add Acct-Input-Packets\n");
301                         goto fail;
302                 }
303                 if (!radius_msg_add_attr_int32(msg,
304                                                RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
305                                                data.tx_packets)) {
306                         printf("Could not add Acct-Output-Packets\n");
307                         goto fail;
308                 }
309                 if (!radius_msg_add_attr_int32(msg,
310                                                RADIUS_ATTR_ACCT_INPUT_OCTETS,
311                                                data.rx_bytes)) {
312                         printf("Could not add Acct-Input-Octets\n");
313                         goto fail;
314                 }
315                 gigawords = sta->acct_input_gigawords;
316 #if __WORDSIZE == 64
317                 gigawords += data.rx_bytes >> 32;
318 #endif
319                 if (gigawords &&
320                     !radius_msg_add_attr_int32(
321                             msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
322                             gigawords)) {
323                         printf("Could not add Acct-Input-Gigawords\n");
324                         goto fail;
325                 }
326                 if (!radius_msg_add_attr_int32(msg,
327                                                RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
328                                                data.tx_bytes)) {
329                         printf("Could not add Acct-Output-Octets\n");
330                         goto fail;
331                 }
332                 gigawords = sta->acct_output_gigawords;
333 #if __WORDSIZE == 64
334                 gigawords += data.tx_bytes >> 32;
335 #endif
336                 if (gigawords &&
337                     !radius_msg_add_attr_int32(
338                             msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
339                             gigawords)) {
340                         printf("Could not add Acct-Output-Gigawords\n");
341                         goto fail;
342                 }
343         }
344
345         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
346                                        time(NULL))) {
347                 printf("Could not add Event-Timestamp\n");
348                 goto fail;
349         }
350
351         if (eloop_terminated())
352                 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
353
354         if (stop && cause &&
355             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
356                                        cause)) {
357                 printf("Could not add Acct-Terminate-Cause\n");
358                 goto fail;
359         }
360
361         radius_client_send(hapd->radius, msg,
362                            stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
363                            sta->addr);
364         return;
365
366  fail:
367         radius_msg_free(msg);
368 }
369
370
371 /**
372  * accounting_sta_interim - Send a interim STA accounting report
373  * @hapd: hostapd BSS data
374  * @sta: The station
375  */
376 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
377 {
378         if (sta->acct_session_started)
379                 accounting_sta_report(hapd, sta, 0);
380 }
381
382
383 /**
384  * accounting_sta_stop - Stop STA accounting
385  * @hapd: hostapd BSS data
386  * @sta: The station
387  */
388 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
389 {
390         if (sta->acct_session_started) {
391                 accounting_sta_report(hapd, sta, 1);
392                 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
393                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
394                                HOSTAPD_LEVEL_INFO,
395                                "stopped accounting session %08X-%08X",
396                                sta->acct_session_id_hi,
397                                sta->acct_session_id_lo);
398                 sta->acct_session_started = 0;
399         }
400 }
401
402
403 static void accounting_sta_get_id(struct hostapd_data *hapd,
404                                   struct sta_info *sta)
405 {
406         sta->acct_session_id_lo = hapd->acct_session_id_lo++;
407         if (hapd->acct_session_id_lo == 0) {
408                 hapd->acct_session_id_hi++;
409         }
410         sta->acct_session_id_hi = hapd->acct_session_id_hi;
411 }
412
413
414 /**
415  * accounting_receive - Process the RADIUS frames from Accounting Server
416  * @msg: RADIUS response message
417  * @req: RADIUS request message
418  * @shared_secret: RADIUS shared secret
419  * @shared_secret_len: Length of shared_secret in octets
420  * @data: Context data (struct hostapd_data *)
421  * Returns: Processing status
422  */
423 static RadiusRxResult
424 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
425                    const u8 *shared_secret, size_t shared_secret_len,
426                    void *data)
427 {
428         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
429                 printf("Unknown RADIUS message code\n");
430                 return RADIUS_RX_UNKNOWN;
431         }
432
433         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
434                 printf("Incoming RADIUS packet did not have correct "
435                        "Authenticator - dropped\n");
436                 return RADIUS_RX_INVALID_AUTHENTICATOR;
437         }
438
439         return RADIUS_RX_PROCESSED;
440 }
441
442
443 static void accounting_report_state(struct hostapd_data *hapd, int on)
444 {
445         struct radius_msg *msg;
446
447         if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
448                 return;
449
450         /* Inform RADIUS server that accounting will start/stop so that the
451          * server can close old accounting sessions. */
452         msg = accounting_msg(hapd, NULL,
453                              on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
454                              RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
455         if (!msg)
456                 return;
457
458         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
459                                        RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
460         {
461                 printf("Could not add Acct-Terminate-Cause\n");
462                 radius_msg_free(msg);
463                 return;
464         }
465
466         radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
467 }
468
469
470 /**
471  * accounting_init: Initialize accounting
472  * @hapd: hostapd BSS data
473  * Returns: 0 on success, -1 on failure
474  */
475 int accounting_init(struct hostapd_data *hapd)
476 {
477         /* Acct-Session-Id should be unique over reboots. If reliable clock is
478          * not available, this could be replaced with reboot counter, etc. */
479         hapd->acct_session_id_hi = time(NULL);
480
481         if (radius_client_register(hapd->radius, RADIUS_ACCT,
482                                    accounting_receive, hapd))
483                 return -1;
484
485         accounting_report_state(hapd, 1);
486
487         return 0;
488 }
489
490
491 /**
492  * accounting_deinit: Deinitilize accounting
493  * @hapd: hostapd BSS data
494  */
495 void accounting_deinit(struct hostapd_data *hapd)
496 {
497         accounting_report_state(hapd, 0);
498 }