accounting: Staticise accounting_sta_interim
[mech_eap.git] / src / ap / accounting.c
1 /*
2  * hostapd / RADIUS Accounting
3  * Copyright (c) 2002-2009, 2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "drivers/driver.h"
14 #include "radius/radius.h"
15 #include "radius/radius_client.h"
16 #include "hostapd.h"
17 #include "ieee802_1x.h"
18 #include "ap_config.h"
19 #include "sta_info.h"
20 #include "ap_drv_ops.h"
21 #include "accounting.h"
22
23
24 /* Default interval in seconds for polling TX/RX octets from the driver if
25  * STA is not using interim accounting. This detects wrap arounds for
26  * input/output octets and updates Acct-{Input,Output}-Gigawords. */
27 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
28
29 static void accounting_sta_get_id(struct hostapd_data *hapd,
30                                   struct sta_info *sta);
31 static void accounting_sta_interim(struct hostapd_data *hapd,
32                                    struct sta_info *sta);
33
34
35 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
36                                           struct sta_info *sta,
37                                           int status_type)
38 {
39         struct radius_msg *msg;
40         char buf[128];
41         u8 *val;
42         size_t len;
43         int i;
44         struct wpabuf *b;
45         struct hostapd_radius_attr *attr;
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 (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
75                                             RADIUS_ATTR_ACCT_AUTHENTIC) &&
76             !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 (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
101                                             RADIUS_ATTR_NAS_IP_ADDRESS) &&
102             hapd->conf->own_ip_addr.af == AF_INET &&
103             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
104                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
105                 printf("Could not add NAS-IP-Address\n");
106                 goto fail;
107         }
108
109 #ifdef CONFIG_IPV6
110         if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
111                                             RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
112             hapd->conf->own_ip_addr.af == AF_INET6 &&
113             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
114                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
115                 printf("Could not add NAS-IPv6-Address\n");
116                 goto fail;
117         }
118 #endif /* CONFIG_IPV6 */
119
120         if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
121                                             RADIUS_ATTR_NAS_IDENTIFIER) &&
122             hapd->conf->nas_identifier &&
123             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
124                                  (u8 *) hapd->conf->nas_identifier,
125                                  os_strlen(hapd->conf->nas_identifier))) {
126                 printf("Could not add NAS-Identifier\n");
127                 goto fail;
128         }
129
130         if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
131                                             RADIUS_ATTR_NAS_PORT) &&
132             sta &&
133             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
134                 printf("Could not add NAS-Port\n");
135                 goto fail;
136         }
137
138         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
139                     MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
140         if (!hostapd_config_get_radius_attr(hapd->conf->radius_acct_req_attr,
141                                             RADIUS_ATTR_CALLED_STATION_ID) &&
142             !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
143                                  (u8 *) buf, os_strlen(buf))) {
144                 printf("Could not add Called-Station-Id\n");
145                 goto fail;
146         }
147
148         if (sta) {
149                 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
150                             MAC2STR(sta->addr));
151                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
152                                          (u8 *) buf, os_strlen(buf))) {
153                         printf("Could not add Calling-Station-Id\n");
154                         goto fail;
155                 }
156
157                 if (!hostapd_config_get_radius_attr(
158                             hapd->conf->radius_acct_req_attr,
159                             RADIUS_ATTR_NAS_PORT_TYPE) &&
160                     !radius_msg_add_attr_int32(
161                             msg, RADIUS_ATTR_NAS_PORT_TYPE,
162                             RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
163                         printf("Could not add NAS-Port-Type\n");
164                         goto fail;
165                 }
166
167                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
168                             radius_sta_rate(hapd, sta) / 2,
169                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
170                             radius_mode_txt(hapd));
171                 if (!hostapd_config_get_radius_attr(
172                             hapd->conf->radius_acct_req_attr,
173                             RADIUS_ATTR_CONNECT_INFO) &&
174                     !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
175                                          (u8 *) buf, os_strlen(buf))) {
176                         printf("Could not add Connect-Info\n");
177                         goto fail;
178                 }
179
180                 for (i = 0; ; i++) {
181                         val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
182                                                           i);
183                         if (val == NULL)
184                                 break;
185
186                         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
187                                                  val, len)) {
188                                 printf("Could not add Class\n");
189                                 goto fail;
190                         }
191                 }
192
193                 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
194                 if (b &&
195                     !radius_msg_add_attr(msg,
196                                          RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
197                                          wpabuf_head(b), wpabuf_len(b))) {
198                         wpa_printf(MSG_ERROR, "Could not add CUI");
199                         goto fail;
200                 }
201         }
202
203         for (attr = hapd->conf->radius_acct_req_attr; attr; attr = attr->next)
204         {
205                 if (!radius_msg_add_attr(msg, attr->type,
206                                          wpabuf_head(attr->val),
207                                          wpabuf_len(attr->val))) {
208                         wpa_printf(MSG_ERROR, "Could not add RADIUS "
209                                    "attribute");
210                         goto fail;
211                 }
212         }
213
214         return msg;
215
216  fail:
217         radius_msg_free(msg);
218         return NULL;
219 }
220
221
222 static int accounting_sta_update_stats(struct hostapd_data *hapd,
223                                        struct sta_info *sta,
224                                        struct hostap_sta_driver_data *data)
225 {
226         if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
227                 return -1;
228
229         if (sta->last_rx_bytes > data->rx_bytes)
230                 sta->acct_input_gigawords++;
231         if (sta->last_tx_bytes > data->tx_bytes)
232                 sta->acct_output_gigawords++;
233         sta->last_rx_bytes = data->rx_bytes;
234         sta->last_tx_bytes = data->tx_bytes;
235
236         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
237                        HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
238                        "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
239                        "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
240                        sta->last_rx_bytes, sta->acct_input_gigawords,
241                        sta->last_tx_bytes, sta->acct_output_gigawords);
242
243         return 0;
244 }
245
246
247 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
248 {
249         struct hostapd_data *hapd = eloop_ctx;
250         struct sta_info *sta = timeout_ctx;
251         int interval;
252
253         if (sta->acct_interim_interval) {
254                 accounting_sta_interim(hapd, sta);
255                 interval = sta->acct_interim_interval;
256         } else {
257                 struct hostap_sta_driver_data data;
258                 accounting_sta_update_stats(hapd, sta, &data);
259                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
260         }
261
262         eloop_register_timeout(interval, 0, accounting_interim_update,
263                                hapd, sta);
264 }
265
266
267 /**
268  * accounting_sta_start - Start STA accounting
269  * @hapd: hostapd BSS data
270  * @sta: The station
271  */
272 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
273 {
274         struct radius_msg *msg;
275         struct os_time t;
276         int interval;
277
278         if (sta->acct_session_started)
279                 return;
280
281         accounting_sta_get_id(hapd, sta);
282         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
283                        HOSTAPD_LEVEL_INFO,
284                        "starting accounting session %08X-%08X",
285                        sta->acct_session_id_hi, sta->acct_session_id_lo);
286
287         os_get_time(&t);
288         sta->acct_session_start = t.sec;
289         sta->last_rx_bytes = sta->last_tx_bytes = 0;
290         sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
291         hostapd_drv_sta_clear_stats(hapd, sta->addr);
292
293         if (!hapd->conf->radius->acct_server)
294                 return;
295
296         if (sta->acct_interim_interval)
297                 interval = sta->acct_interim_interval;
298         else
299                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
300         eloop_register_timeout(interval, 0, accounting_interim_update,
301                                hapd, sta);
302
303         msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
304         if (msg &&
305             radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
306                 radius_msg_free(msg);
307
308         sta->acct_session_started = 1;
309 }
310
311
312 static void accounting_sta_report(struct hostapd_data *hapd,
313                                   struct sta_info *sta, int stop)
314 {
315         struct radius_msg *msg;
316         int cause = sta->acct_terminate_cause;
317         struct hostap_sta_driver_data data;
318         struct os_time now;
319         u32 gigawords;
320
321         if (!hapd->conf->radius->acct_server)
322                 return;
323
324         msg = accounting_msg(hapd, sta,
325                              stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
326                              RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
327         if (!msg) {
328                 printf("Could not create RADIUS Accounting message\n");
329                 return;
330         }
331
332         os_get_time(&now);
333         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
334                                        now.sec - sta->acct_session_start)) {
335                 printf("Could not add Acct-Session-Time\n");
336                 goto fail;
337         }
338
339         if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
340                 if (!radius_msg_add_attr_int32(msg,
341                                                RADIUS_ATTR_ACCT_INPUT_PACKETS,
342                                                data.rx_packets)) {
343                         printf("Could not add Acct-Input-Packets\n");
344                         goto fail;
345                 }
346                 if (!radius_msg_add_attr_int32(msg,
347                                                RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
348                                                data.tx_packets)) {
349                         printf("Could not add Acct-Output-Packets\n");
350                         goto fail;
351                 }
352                 if (!radius_msg_add_attr_int32(msg,
353                                                RADIUS_ATTR_ACCT_INPUT_OCTETS,
354                                                data.rx_bytes)) {
355                         printf("Could not add Acct-Input-Octets\n");
356                         goto fail;
357                 }
358                 gigawords = sta->acct_input_gigawords;
359 #if __WORDSIZE == 64
360                 gigawords += data.rx_bytes >> 32;
361 #endif
362                 if (gigawords &&
363                     !radius_msg_add_attr_int32(
364                             msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
365                             gigawords)) {
366                         printf("Could not add Acct-Input-Gigawords\n");
367                         goto fail;
368                 }
369                 if (!radius_msg_add_attr_int32(msg,
370                                                RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
371                                                data.tx_bytes)) {
372                         printf("Could not add Acct-Output-Octets\n");
373                         goto fail;
374                 }
375                 gigawords = sta->acct_output_gigawords;
376 #if __WORDSIZE == 64
377                 gigawords += data.tx_bytes >> 32;
378 #endif
379                 if (gigawords &&
380                     !radius_msg_add_attr_int32(
381                             msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
382                             gigawords)) {
383                         printf("Could not add Acct-Output-Gigawords\n");
384                         goto fail;
385                 }
386         }
387
388         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
389                                        now.sec)) {
390                 printf("Could not add Event-Timestamp\n");
391                 goto fail;
392         }
393
394         if (eloop_terminated())
395                 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
396
397         if (stop && cause &&
398             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
399                                        cause)) {
400                 printf("Could not add Acct-Terminate-Cause\n");
401                 goto fail;
402         }
403
404         if (radius_client_send(hapd->radius, msg,
405                                stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
406                                sta->addr) < 0)
407                 goto fail;
408         return;
409
410  fail:
411         radius_msg_free(msg);
412 }
413
414
415 /**
416  * accounting_sta_interim - Send a interim STA accounting report
417  * @hapd: hostapd BSS data
418  * @sta: The station
419  */
420 static void accounting_sta_interim(struct hostapd_data *hapd,
421                                    struct sta_info *sta)
422 {
423         if (sta->acct_session_started)
424                 accounting_sta_report(hapd, sta, 0);
425 }
426
427
428 /**
429  * accounting_sta_stop - Stop STA accounting
430  * @hapd: hostapd BSS data
431  * @sta: The station
432  */
433 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
434 {
435         if (sta->acct_session_started) {
436                 accounting_sta_report(hapd, sta, 1);
437                 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
438                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
439                                HOSTAPD_LEVEL_INFO,
440                                "stopped accounting session %08X-%08X",
441                                sta->acct_session_id_hi,
442                                sta->acct_session_id_lo);
443                 sta->acct_session_started = 0;
444         }
445 }
446
447
448 static void accounting_sta_get_id(struct hostapd_data *hapd,
449                                   struct sta_info *sta)
450 {
451         sta->acct_session_id_lo = hapd->acct_session_id_lo++;
452         if (hapd->acct_session_id_lo == 0) {
453                 hapd->acct_session_id_hi++;
454         }
455         sta->acct_session_id_hi = hapd->acct_session_id_hi;
456 }
457
458
459 /**
460  * accounting_receive - Process the RADIUS frames from Accounting Server
461  * @msg: RADIUS response message
462  * @req: RADIUS request message
463  * @shared_secret: RADIUS shared secret
464  * @shared_secret_len: Length of shared_secret in octets
465  * @data: Context data (struct hostapd_data *)
466  * Returns: Processing status
467  */
468 static RadiusRxResult
469 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
470                    const u8 *shared_secret, size_t shared_secret_len,
471                    void *data)
472 {
473         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
474                 printf("Unknown RADIUS message code\n");
475                 return RADIUS_RX_UNKNOWN;
476         }
477
478         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
479                 printf("Incoming RADIUS packet did not have correct "
480                        "Authenticator - dropped\n");
481                 return RADIUS_RX_INVALID_AUTHENTICATOR;
482         }
483
484         return RADIUS_RX_PROCESSED;
485 }
486
487
488 static void accounting_report_state(struct hostapd_data *hapd, int on)
489 {
490         struct radius_msg *msg;
491
492         if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
493                 return;
494
495         /* Inform RADIUS server that accounting will start/stop so that the
496          * server can close old accounting sessions. */
497         msg = accounting_msg(hapd, NULL,
498                              on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
499                              RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
500         if (!msg)
501                 return;
502
503         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
504                                        RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
505         {
506                 printf("Could not add Acct-Terminate-Cause\n");
507                 radius_msg_free(msg);
508                 return;
509         }
510
511         if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
512                 radius_msg_free(msg);
513 }
514
515
516 /**
517  * accounting_init: Initialize accounting
518  * @hapd: hostapd BSS data
519  * Returns: 0 on success, -1 on failure
520  */
521 int accounting_init(struct hostapd_data *hapd)
522 {
523         struct os_time now;
524
525         /* Acct-Session-Id should be unique over reboots. If reliable clock is
526          * not available, this could be replaced with reboot counter, etc. */
527         os_get_time(&now);
528         hapd->acct_session_id_hi = now.sec;
529
530         if (radius_client_register(hapd->radius, RADIUS_ACCT,
531                                    accounting_receive, hapd))
532                 return -1;
533
534         accounting_report_state(hapd, 1);
535
536         return 0;
537 }
538
539
540 /**
541  * accounting_deinit: Deinitilize accounting
542  * @hapd: hostapd BSS data
543  */
544 void accounting_deinit(struct hostapd_data *hapd)
545 {
546         accounting_report_state(hapd, 0);
547 }