Fix memory leaks on radius_client_send error paths
[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 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
32
33 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
34                                           struct sta_info *sta,
35                                           int status_type)
36 {
37         struct radius_msg *msg;
38         char buf[128];
39         u8 *val;
40         size_t len;
41         int i;
42
43         msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
44                              radius_client_get_id(hapd->radius));
45         if (msg == NULL) {
46                 printf("Could not create net RADIUS packet\n");
47                 return NULL;
48         }
49
50         if (sta) {
51                 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
52
53                 os_snprintf(buf, sizeof(buf), "%08X-%08X",
54                             sta->acct_session_id_hi, sta->acct_session_id_lo);
55                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
56                                          (u8 *) buf, os_strlen(buf))) {
57                         printf("Could not add Acct-Session-Id\n");
58                         goto fail;
59                 }
60         } else {
61                 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
62         }
63
64         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
65                                        status_type)) {
66                 printf("Could not add Acct-Status-Type\n");
67                 goto fail;
68         }
69
70         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
71                                        hapd->conf->ieee802_1x ?
72                                        RADIUS_ACCT_AUTHENTIC_RADIUS :
73                                        RADIUS_ACCT_AUTHENTIC_LOCAL)) {
74                 printf("Could not add Acct-Authentic\n");
75                 goto fail;
76         }
77
78         if (sta) {
79                 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
80                 if (!val) {
81                         os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
82                                     MAC2STR(sta->addr));
83                         val = (u8 *) buf;
84                         len = os_strlen(buf);
85                 }
86
87                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
88                                          len)) {
89                         printf("Could not add User-Name\n");
90                         goto fail;
91                 }
92         }
93
94         if (hapd->conf->own_ip_addr.af == AF_INET &&
95             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
96                                  (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
97                 printf("Could not add NAS-IP-Address\n");
98                 goto fail;
99         }
100
101 #ifdef CONFIG_IPV6
102         if (hapd->conf->own_ip_addr.af == AF_INET6 &&
103             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
104                                  (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
105                 printf("Could not add NAS-IPv6-Address\n");
106                 goto fail;
107         }
108 #endif /* CONFIG_IPV6 */
109
110         if (hapd->conf->nas_identifier &&
111             !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
112                                  (u8 *) hapd->conf->nas_identifier,
113                                  os_strlen(hapd->conf->nas_identifier))) {
114                 printf("Could not add NAS-Identifier\n");
115                 goto fail;
116         }
117
118         if (sta &&
119             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
120                 printf("Could not add NAS-Port\n");
121                 goto fail;
122         }
123
124         os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
125                     MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
126         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
127                                  (u8 *) buf, os_strlen(buf))) {
128                 printf("Could not add Called-Station-Id\n");
129                 goto fail;
130         }
131
132         if (sta) {
133                 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
134                             MAC2STR(sta->addr));
135                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
136                                          (u8 *) buf, os_strlen(buf))) {
137                         printf("Could not add Calling-Station-Id\n");
138                         goto fail;
139                 }
140
141                 if (!radius_msg_add_attr_int32(
142                             msg, RADIUS_ATTR_NAS_PORT_TYPE,
143                             RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
144                         printf("Could not add NAS-Port-Type\n");
145                         goto fail;
146                 }
147
148                 os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
149                             radius_sta_rate(hapd, sta) / 2,
150                             (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
151                             radius_mode_txt(hapd));
152                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
153                                          (u8 *) buf, os_strlen(buf))) {
154                         printf("Could not add Connect-Info\n");
155                         goto fail;
156                 }
157
158                 for (i = 0; ; i++) {
159                         val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
160                                                           i);
161                         if (val == NULL)
162                                 break;
163
164                         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
165                                                  val, len)) {
166                                 printf("Could not add Class\n");
167                                 goto fail;
168                         }
169                 }
170         }
171
172         return msg;
173
174  fail:
175         radius_msg_free(msg);
176         return NULL;
177 }
178
179
180 static int accounting_sta_update_stats(struct hostapd_data *hapd,
181                                        struct sta_info *sta,
182                                        struct hostap_sta_driver_data *data)
183 {
184         if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
185                 return -1;
186
187         if (sta->last_rx_bytes > data->rx_bytes)
188                 sta->acct_input_gigawords++;
189         if (sta->last_tx_bytes > data->tx_bytes)
190                 sta->acct_output_gigawords++;
191         sta->last_rx_bytes = data->rx_bytes;
192         sta->last_tx_bytes = data->tx_bytes;
193
194         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
195                        HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
196                        "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
197                        "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
198                        sta->last_rx_bytes, sta->acct_input_gigawords,
199                        sta->last_tx_bytes, sta->acct_output_gigawords);
200
201         return 0;
202 }
203
204
205 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
206 {
207         struct hostapd_data *hapd = eloop_ctx;
208         struct sta_info *sta = timeout_ctx;
209         int interval;
210
211         if (sta->acct_interim_interval) {
212                 accounting_sta_interim(hapd, sta);
213                 interval = sta->acct_interim_interval;
214         } else {
215                 struct hostap_sta_driver_data data;
216                 accounting_sta_update_stats(hapd, sta, &data);
217                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
218         }
219
220         eloop_register_timeout(interval, 0, accounting_interim_update,
221                                hapd, sta);
222 }
223
224
225 /**
226  * accounting_sta_start - Start STA accounting
227  * @hapd: hostapd BSS data
228  * @sta: The station
229  */
230 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
231 {
232         struct radius_msg *msg;
233         struct os_time t;
234         int interval;
235
236         if (sta->acct_session_started)
237                 return;
238
239         accounting_sta_get_id(hapd, sta);
240         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
241                        HOSTAPD_LEVEL_INFO,
242                        "starting accounting session %08X-%08X",
243                        sta->acct_session_id_hi, sta->acct_session_id_lo);
244
245         os_get_time(&t);
246         sta->acct_session_start = t.sec;
247         sta->last_rx_bytes = sta->last_tx_bytes = 0;
248         sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
249         hostapd_drv_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) < 0)
264                 radius_msg_free(msg);
265
266         sta->acct_session_started = 1;
267 }
268
269
270 static void accounting_sta_report(struct hostapd_data *hapd,
271                                   struct sta_info *sta, int stop)
272 {
273         struct radius_msg *msg;
274         int cause = sta->acct_terminate_cause;
275         struct hostap_sta_driver_data data;
276         struct os_time now;
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         os_get_time(&now);
291         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
292                                        now.sec - sta->acct_session_start)) {
293                 printf("Could not add Acct-Session-Time\n");
294                 goto fail;
295         }
296
297         if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
298                 if (!radius_msg_add_attr_int32(msg,
299                                                RADIUS_ATTR_ACCT_INPUT_PACKETS,
300                                                data.rx_packets)) {
301                         printf("Could not add Acct-Input-Packets\n");
302                         goto fail;
303                 }
304                 if (!radius_msg_add_attr_int32(msg,
305                                                RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
306                                                data.tx_packets)) {
307                         printf("Could not add Acct-Output-Packets\n");
308                         goto fail;
309                 }
310                 if (!radius_msg_add_attr_int32(msg,
311                                                RADIUS_ATTR_ACCT_INPUT_OCTETS,
312                                                data.rx_bytes)) {
313                         printf("Could not add Acct-Input-Octets\n");
314                         goto fail;
315                 }
316                 gigawords = sta->acct_input_gigawords;
317 #if __WORDSIZE == 64
318                 gigawords += data.rx_bytes >> 32;
319 #endif
320                 if (gigawords &&
321                     !radius_msg_add_attr_int32(
322                             msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
323                             gigawords)) {
324                         printf("Could not add Acct-Input-Gigawords\n");
325                         goto fail;
326                 }
327                 if (!radius_msg_add_attr_int32(msg,
328                                                RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
329                                                data.tx_bytes)) {
330                         printf("Could not add Acct-Output-Octets\n");
331                         goto fail;
332                 }
333                 gigawords = sta->acct_output_gigawords;
334 #if __WORDSIZE == 64
335                 gigawords += data.tx_bytes >> 32;
336 #endif
337                 if (gigawords &&
338                     !radius_msg_add_attr_int32(
339                             msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
340                             gigawords)) {
341                         printf("Could not add Acct-Output-Gigawords\n");
342                         goto fail;
343                 }
344         }
345
346         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
347                                        now.sec)) {
348                 printf("Could not add Event-Timestamp\n");
349                 goto fail;
350         }
351
352         if (eloop_terminated())
353                 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
354
355         if (stop && cause &&
356             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
357                                        cause)) {
358                 printf("Could not add Acct-Terminate-Cause\n");
359                 goto fail;
360         }
361
362         if (radius_client_send(hapd->radius, msg,
363                                stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
364                                sta->addr) < 0)
365                 goto fail;
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         if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
469                 radius_msg_free(msg);
470 }
471
472
473 /**
474  * accounting_init: Initialize accounting
475  * @hapd: hostapd BSS data
476  * Returns: 0 on success, -1 on failure
477  */
478 int accounting_init(struct hostapd_data *hapd)
479 {
480         struct os_time now;
481
482         /* Acct-Session-Id should be unique over reboots. If reliable clock is
483          * not available, this could be replaced with reboot counter, etc. */
484         os_get_time(&now);
485         hapd->acct_session_id_hi = now.sec;
486
487         if (radius_client_register(hapd->radius, RADIUS_ACCT,
488                                    accounting_receive, hapd))
489                 return -1;
490
491         accounting_report_state(hapd, 1);
492
493         return 0;
494 }
495
496
497 /**
498  * accounting_deinit: Deinitilize accounting
499  * @hapd: hostapd BSS data
500  */
501 void accounting_deinit(struct hostapd_data *hapd)
502 {
503         accounting_report_state(hapd, 0);
504 }