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