hostapd_driver_ops reduction
[mech_eap.git] / src / ap / accounting.c
1 /*
2  * hostapd / RADIUS Accounting
3  * Copyright (c) 2002-2009, 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 "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "drivers/driver.h"
20 #include "radius/radius.h"
21 #include "radius/radius_client.h"
22 #include "hostapd.h"
23 #include "ieee802_1x.h"
24 #include "ap_config.h"
25 #include "sta_info.h"
26 #include "ap_drv_ops.h"
27 #include "accounting.h"
28
29
30 /* Default interval in seconds for polling TX/RX octets from the driver if
31  * STA is not using interim accounting. This detects wrap arounds for
32  * input/output octets and updates Acct-{Input,Output}-Gigawords. */
33 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
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         return NULL;
183 }
184
185
186 static int accounting_sta_update_stats(struct hostapd_data *hapd,
187                                        struct sta_info *sta,
188                                        struct hostap_sta_driver_data *data)
189 {
190         if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
191                 return -1;
192
193         if (sta->last_rx_bytes > data->rx_bytes)
194                 sta->acct_input_gigawords++;
195         if (sta->last_tx_bytes > data->tx_bytes)
196                 sta->acct_output_gigawords++;
197         sta->last_rx_bytes = data->rx_bytes;
198         sta->last_tx_bytes = data->tx_bytes;
199
200         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
201                        HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
202                        "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
203                        "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
204                        sta->last_rx_bytes, sta->acct_input_gigawords,
205                        sta->last_tx_bytes, sta->acct_output_gigawords);
206
207         return 0;
208 }
209
210
211 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
212 {
213         struct hostapd_data *hapd = eloop_ctx;
214         struct sta_info *sta = timeout_ctx;
215         int interval;
216
217         if (sta->acct_interim_interval) {
218                 accounting_sta_interim(hapd, sta);
219                 interval = sta->acct_interim_interval;
220         } else {
221                 struct hostap_sta_driver_data data;
222                 accounting_sta_update_stats(hapd, sta, &data);
223                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
224         }
225
226         eloop_register_timeout(interval, 0, accounting_interim_update,
227                                hapd, sta);
228 }
229
230
231 /**
232  * accounting_sta_start - Start STA accounting
233  * @hapd: hostapd BSS data
234  * @sta: The station
235  */
236 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
237 {
238         struct radius_msg *msg;
239         int interval;
240
241         if (sta->acct_session_started)
242                 return;
243
244         accounting_sta_get_id(hapd, sta);
245         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
246                        HOSTAPD_LEVEL_INFO,
247                        "starting accounting session %08X-%08X",
248                        sta->acct_session_id_hi, sta->acct_session_id_lo);
249
250         time(&sta->acct_session_start);
251         sta->last_rx_bytes = sta->last_tx_bytes = 0;
252         sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
253         hostapd_drv_sta_clear_stats(hapd, sta->addr);
254
255         if (!hapd->conf->radius->acct_server)
256                 return;
257
258         if (sta->acct_interim_interval)
259                 interval = sta->acct_interim_interval;
260         else
261                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
262         eloop_register_timeout(interval, 0, accounting_interim_update,
263                                hapd, sta);
264
265         msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
266         if (msg)
267                 radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr);
268
269         sta->acct_session_started = 1;
270 }
271
272
273 static void accounting_sta_report(struct hostapd_data *hapd,
274                                   struct sta_info *sta, int stop)
275 {
276         struct radius_msg *msg;
277         int cause = sta->acct_terminate_cause;
278         struct hostap_sta_driver_data data;
279         u32 gigawords;
280
281         if (!hapd->conf->radius->acct_server)
282                 return;
283
284         msg = accounting_msg(hapd, sta,
285                              stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
286                              RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
287         if (!msg) {
288                 printf("Could not create RADIUS Accounting message\n");
289                 return;
290         }
291
292         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
293                                        time(NULL) - sta->acct_session_start)) {
294                 printf("Could not add Acct-Session-Time\n");
295                 goto fail;
296         }
297
298         if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
299                 if (!radius_msg_add_attr_int32(msg,
300                                                RADIUS_ATTR_ACCT_INPUT_PACKETS,
301                                                data.rx_packets)) {
302                         printf("Could not add Acct-Input-Packets\n");
303                         goto fail;
304                 }
305                 if (!radius_msg_add_attr_int32(msg,
306                                                RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
307                                                data.tx_packets)) {
308                         printf("Could not add Acct-Output-Packets\n");
309                         goto fail;
310                 }
311                 if (!radius_msg_add_attr_int32(msg,
312                                                RADIUS_ATTR_ACCT_INPUT_OCTETS,
313                                                data.rx_bytes)) {
314                         printf("Could not add Acct-Input-Octets\n");
315                         goto fail;
316                 }
317                 gigawords = sta->acct_input_gigawords;
318 #if __WORDSIZE == 64
319                 gigawords += data.rx_bytes >> 32;
320 #endif
321                 if (gigawords &&
322                     !radius_msg_add_attr_int32(
323                             msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
324                             gigawords)) {
325                         printf("Could not add Acct-Input-Gigawords\n");
326                         goto fail;
327                 }
328                 if (!radius_msg_add_attr_int32(msg,
329                                                RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
330                                                data.tx_bytes)) {
331                         printf("Could not add Acct-Output-Octets\n");
332                         goto fail;
333                 }
334                 gigawords = sta->acct_output_gigawords;
335 #if __WORDSIZE == 64
336                 gigawords += data.tx_bytes >> 32;
337 #endif
338                 if (gigawords &&
339                     !radius_msg_add_attr_int32(
340                             msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
341                             gigawords)) {
342                         printf("Could not add Acct-Output-Gigawords\n");
343                         goto fail;
344                 }
345         }
346
347         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
348                                        time(NULL))) {
349                 printf("Could not add Event-Timestamp\n");
350                 goto fail;
351         }
352
353         if (eloop_terminated())
354                 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
355
356         if (stop && cause &&
357             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
358                                        cause)) {
359                 printf("Could not add Acct-Terminate-Cause\n");
360                 goto fail;
361         }
362
363         radius_client_send(hapd->radius, msg,
364                            stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
365                            sta->addr);
366         return;
367
368  fail:
369         radius_msg_free(msg);
370 }
371
372
373 /**
374  * accounting_sta_interim - Send a interim STA accounting report
375  * @hapd: hostapd BSS data
376  * @sta: The station
377  */
378 void accounting_sta_interim(struct hostapd_data *hapd, struct sta_info *sta)
379 {
380         if (sta->acct_session_started)
381                 accounting_sta_report(hapd, sta, 0);
382 }
383
384
385 /**
386  * accounting_sta_stop - Stop STA accounting
387  * @hapd: hostapd BSS data
388  * @sta: The station
389  */
390 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
391 {
392         if (sta->acct_session_started) {
393                 accounting_sta_report(hapd, sta, 1);
394                 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
395                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
396                                HOSTAPD_LEVEL_INFO,
397                                "stopped accounting session %08X-%08X",
398                                sta->acct_session_id_hi,
399                                sta->acct_session_id_lo);
400                 sta->acct_session_started = 0;
401         }
402 }
403
404
405 static void accounting_sta_get_id(struct hostapd_data *hapd,
406                                   struct sta_info *sta)
407 {
408         sta->acct_session_id_lo = hapd->acct_session_id_lo++;
409         if (hapd->acct_session_id_lo == 0) {
410                 hapd->acct_session_id_hi++;
411         }
412         sta->acct_session_id_hi = hapd->acct_session_id_hi;
413 }
414
415
416 /**
417  * accounting_receive - Process the RADIUS frames from Accounting Server
418  * @msg: RADIUS response message
419  * @req: RADIUS request message
420  * @shared_secret: RADIUS shared secret
421  * @shared_secret_len: Length of shared_secret in octets
422  * @data: Context data (struct hostapd_data *)
423  * Returns: Processing status
424  */
425 static RadiusRxResult
426 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
427                    const u8 *shared_secret, size_t shared_secret_len,
428                    void *data)
429 {
430         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
431                 printf("Unknown RADIUS message code\n");
432                 return RADIUS_RX_UNKNOWN;
433         }
434
435         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
436                 printf("Incoming RADIUS packet did not have correct "
437                        "Authenticator - dropped\n");
438                 return RADIUS_RX_INVALID_AUTHENTICATOR;
439         }
440
441         return RADIUS_RX_PROCESSED;
442 }
443
444
445 static void accounting_report_state(struct hostapd_data *hapd, int on)
446 {
447         struct radius_msg *msg;
448
449         if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
450                 return;
451
452         /* Inform RADIUS server that accounting will start/stop so that the
453          * server can close old accounting sessions. */
454         msg = accounting_msg(hapd, NULL,
455                              on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
456                              RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
457         if (!msg)
458                 return;
459
460         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
461                                        RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
462         {
463                 printf("Could not add Acct-Terminate-Cause\n");
464                 radius_msg_free(msg);
465                 return;
466         }
467
468         radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL);
469 }
470
471
472 /**
473  * accounting_init: Initialize accounting
474  * @hapd: hostapd BSS data
475  * Returns: 0 on success, -1 on failure
476  */
477 int accounting_init(struct hostapd_data *hapd)
478 {
479         /* Acct-Session-Id should be unique over reboots. If reliable clock is
480          * not available, this could be replaced with reboot counter, etc. */
481         hapd->acct_session_id_hi = time(NULL);
482
483         if (radius_client_register(hapd->radius, RADIUS_ACCT,
484                                    accounting_receive, hapd))
485                 return -1;
486
487         accounting_report_state(hapd, 1);
488
489         return 0;
490 }
491
492
493 /**
494  * accounting_deinit: Deinitilize accounting
495  * @hapd: hostapd BSS data
496  */
497 void accounting_deinit(struct hostapd_data *hapd)
498 {
499         accounting_report_state(hapd, 0);
500 }