RADIUS: Use more likely unique accounting Acct-{,Multi-}Session-Id
[mech_eap.git] / src / ap / accounting.c
1 /*
2  * hostapd / RADIUS Accounting
3  * Copyright (c) 2002-2009, 2012-2015, 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 "eapol_auth/eapol_auth_sm.h"
14 #include "eapol_auth/eapol_auth_sm_i.h"
15 #include "radius/radius.h"
16 #include "radius/radius_client.h"
17 #include "hostapd.h"
18 #include "ieee802_1x.h"
19 #include "ap_config.h"
20 #include "sta_info.h"
21 #include "ap_drv_ops.h"
22 #include "accounting.h"
23
24
25 /* Default interval in seconds for polling TX/RX octets from the driver if
26  * STA is not using interim accounting. This detects wrap arounds for
27  * input/output octets and updates Acct-{Input,Output}-Gigawords. */
28 #define ACCT_DEFAULT_UPDATE_INTERVAL 300
29
30 static void accounting_sta_interim(struct hostapd_data *hapd,
31                                    struct sta_info *sta);
32
33
34 static struct radius_msg * accounting_msg(struct hostapd_data *hapd,
35                                           struct sta_info *sta,
36                                           int status_type)
37 {
38         struct radius_msg *msg;
39         char buf[128];
40         u8 *val;
41         size_t len;
42         int i;
43         struct wpabuf *b;
44         struct os_time now;
45
46         msg = radius_msg_new(RADIUS_CODE_ACCOUNTING_REQUEST,
47                              radius_client_get_id(hapd->radius));
48         if (msg == NULL) {
49                 wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
50                 return NULL;
51         }
52
53         if (sta) {
54                 radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(*sta));
55
56                 if ((hapd->conf->wpa & 2) &&
57                     !hapd->conf->disable_pmksa_caching &&
58                     sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
59                         os_snprintf(buf, sizeof(buf), "%016lX",
60                                     (long unsigned int)
61                                     sta->eapol_sm->acct_multi_session_id);
62                         if (!radius_msg_add_attr(
63                                     msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
64                                     (u8 *) buf, os_strlen(buf))) {
65                                 wpa_printf(MSG_INFO,
66                                            "Could not add Acct-Multi-Session-Id");
67                                 goto fail;
68                         }
69                 }
70         } else {
71                 radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(*hapd));
72         }
73
74         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
75                                        status_type)) {
76                 wpa_printf(MSG_INFO, "Could not add Acct-Status-Type");
77                 goto fail;
78         }
79
80         if (sta) {
81                 if (!hostapd_config_get_radius_attr(
82                             hapd->conf->radius_acct_req_attr,
83                             RADIUS_ATTR_ACCT_AUTHENTIC) &&
84                     !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_AUTHENTIC,
85                                                hapd->conf->ieee802_1x ?
86                                                RADIUS_ACCT_AUTHENTIC_RADIUS :
87                                                RADIUS_ACCT_AUTHENTIC_LOCAL)) {
88                         wpa_printf(MSG_INFO, "Could not add Acct-Authentic");
89                         goto fail;
90                 }
91
92                 /* Use 802.1X identity if available */
93                 val = ieee802_1x_get_identity(sta->eapol_sm, &len);
94
95                 /* Use RADIUS ACL identity if 802.1X provides no identity */
96                 if (!val && sta->identity) {
97                         val = (u8 *) sta->identity;
98                         len = os_strlen(sta->identity);
99                 }
100
101                 /* Use STA MAC if neither 802.1X nor RADIUS ACL provided
102                  * identity */
103                 if (!val) {
104                         os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
105                                     MAC2STR(sta->addr));
106                         val = (u8 *) buf;
107                         len = os_strlen(buf);
108                 }
109
110                 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val,
111                                          len)) {
112                         wpa_printf(MSG_INFO, "Could not add User-Name");
113                         goto fail;
114                 }
115         }
116
117         if (add_common_radius_attr(hapd, hapd->conf->radius_acct_req_attr, sta,
118                                    msg) < 0)
119                 goto fail;
120
121         if (sta) {
122                 for (i = 0; ; i++) {
123                         val = ieee802_1x_get_radius_class(sta->eapol_sm, &len,
124                                                           i);
125                         if (val == NULL)
126                                 break;
127
128                         if (!radius_msg_add_attr(msg, RADIUS_ATTR_CLASS,
129                                                  val, len)) {
130                                 wpa_printf(MSG_INFO, "Could not add Class");
131                                 goto fail;
132                         }
133                 }
134
135                 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
136                 if (b &&
137                     !radius_msg_add_attr(msg,
138                                          RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
139                                          wpabuf_head(b), wpabuf_len(b))) {
140                         wpa_printf(MSG_ERROR, "Could not add CUI");
141                         goto fail;
142                 }
143
144                 if (!b && sta->radius_cui &&
145                     !radius_msg_add_attr(msg,
146                                          RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
147                                          (u8 *) sta->radius_cui,
148                                          os_strlen(sta->radius_cui))) {
149                         wpa_printf(MSG_ERROR, "Could not add CUI from ACL");
150                         goto fail;
151                 }
152
153                 if (sta->ipaddr &&
154                     !radius_msg_add_attr_int32(msg,
155                                                RADIUS_ATTR_FRAMED_IP_ADDRESS,
156                                                be_to_host32(sta->ipaddr))) {
157                         wpa_printf(MSG_ERROR,
158                                    "Could not add Framed-IP-Address");
159                         goto fail;
160                 }
161         }
162
163         os_get_time(&now);
164         if (now.sec > 1000000000 &&
165             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_EVENT_TIMESTAMP,
166                                        now.sec)) {
167                 wpa_printf(MSG_INFO, "Could not add Event-Timestamp");
168                 goto fail;
169         }
170
171         return msg;
172
173  fail:
174         radius_msg_free(msg);
175         return NULL;
176 }
177
178
179 static int accounting_sta_update_stats(struct hostapd_data *hapd,
180                                        struct sta_info *sta,
181                                        struct hostap_sta_driver_data *data)
182 {
183         if (hostapd_drv_read_sta_data(hapd, data, sta->addr))
184                 return -1;
185
186         if (sta->last_rx_bytes > data->rx_bytes)
187                 sta->acct_input_gigawords++;
188         if (sta->last_tx_bytes > data->tx_bytes)
189                 sta->acct_output_gigawords++;
190         sta->last_rx_bytes = data->rx_bytes;
191         sta->last_tx_bytes = data->tx_bytes;
192
193         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
194                        HOSTAPD_LEVEL_DEBUG, "updated TX/RX stats: "
195                        "Acct-Input-Octets=%lu Acct-Input-Gigawords=%u "
196                        "Acct-Output-Octets=%lu Acct-Output-Gigawords=%u",
197                        sta->last_rx_bytes, sta->acct_input_gigawords,
198                        sta->last_tx_bytes, sta->acct_output_gigawords);
199
200         return 0;
201 }
202
203
204 static void accounting_interim_update(void *eloop_ctx, void *timeout_ctx)
205 {
206         struct hostapd_data *hapd = eloop_ctx;
207         struct sta_info *sta = timeout_ctx;
208         int interval;
209
210         if (sta->acct_interim_interval) {
211                 accounting_sta_interim(hapd, sta);
212                 interval = sta->acct_interim_interval;
213         } else {
214                 struct hostap_sta_driver_data data;
215                 accounting_sta_update_stats(hapd, sta, &data);
216                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
217         }
218
219         eloop_register_timeout(interval, 0, accounting_interim_update,
220                                hapd, sta);
221 }
222
223
224 /**
225  * accounting_sta_start - Start STA accounting
226  * @hapd: hostapd BSS data
227  * @sta: The station
228  */
229 void accounting_sta_start(struct hostapd_data *hapd, struct sta_info *sta)
230 {
231         struct radius_msg *msg;
232         int interval;
233
234         if (sta->acct_session_started)
235                 return;
236
237         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
238                        HOSTAPD_LEVEL_INFO,
239                        "starting accounting session %016lX",
240                        (long unsigned int) sta->acct_session_id);
241
242         os_get_reltime(&sta->acct_session_start);
243         sta->last_rx_bytes = sta->last_tx_bytes = 0;
244         sta->acct_input_gigawords = sta->acct_output_gigawords = 0;
245         hostapd_drv_sta_clear_stats(hapd, sta->addr);
246
247         if (!hapd->conf->radius->acct_server)
248                 return;
249
250         if (sta->acct_interim_interval)
251                 interval = sta->acct_interim_interval;
252         else
253                 interval = ACCT_DEFAULT_UPDATE_INTERVAL;
254         eloop_register_timeout(interval, 0, accounting_interim_update,
255                                hapd, sta);
256
257         msg = accounting_msg(hapd, sta, RADIUS_ACCT_STATUS_TYPE_START);
258         if (msg &&
259             radius_client_send(hapd->radius, msg, RADIUS_ACCT, sta->addr) < 0)
260                 radius_msg_free(msg);
261
262         sta->acct_session_started = 1;
263 }
264
265
266 static void accounting_sta_report(struct hostapd_data *hapd,
267                                   struct sta_info *sta, int stop)
268 {
269         struct radius_msg *msg;
270         int cause = sta->acct_terminate_cause;
271         struct hostap_sta_driver_data data;
272         struct os_reltime now_r, diff;
273         u32 gigawords;
274
275         if (!hapd->conf->radius->acct_server)
276                 return;
277
278         msg = accounting_msg(hapd, sta,
279                              stop ? RADIUS_ACCT_STATUS_TYPE_STOP :
280                              RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE);
281         if (!msg) {
282                 wpa_printf(MSG_INFO, "Could not create RADIUS Accounting message");
283                 return;
284         }
285
286         os_get_reltime(&now_r);
287         os_reltime_sub(&now_r, &sta->acct_session_start, &diff);
288         if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_SESSION_TIME,
289                                        diff.sec)) {
290                 wpa_printf(MSG_INFO, "Could not add Acct-Session-Time");
291                 goto fail;
292         }
293
294         if (accounting_sta_update_stats(hapd, sta, &data) == 0) {
295                 if (!radius_msg_add_attr_int32(msg,
296                                                RADIUS_ATTR_ACCT_INPUT_PACKETS,
297                                                data.rx_packets)) {
298                         wpa_printf(MSG_INFO, "Could not add Acct-Input-Packets");
299                         goto fail;
300                 }
301                 if (!radius_msg_add_attr_int32(msg,
302                                                RADIUS_ATTR_ACCT_OUTPUT_PACKETS,
303                                                data.tx_packets)) {
304                         wpa_printf(MSG_INFO, "Could not add Acct-Output-Packets");
305                         goto fail;
306                 }
307                 if (!radius_msg_add_attr_int32(msg,
308                                                RADIUS_ATTR_ACCT_INPUT_OCTETS,
309                                                data.rx_bytes)) {
310                         wpa_printf(MSG_INFO, "Could not add Acct-Input-Octets");
311                         goto fail;
312                 }
313                 gigawords = sta->acct_input_gigawords;
314 #if __WORDSIZE == 64
315                 gigawords += data.rx_bytes >> 32;
316 #endif
317                 if (gigawords &&
318                     !radius_msg_add_attr_int32(
319                             msg, RADIUS_ATTR_ACCT_INPUT_GIGAWORDS,
320                             gigawords)) {
321                         wpa_printf(MSG_INFO, "Could not add Acct-Input-Gigawords");
322                         goto fail;
323                 }
324                 if (!radius_msg_add_attr_int32(msg,
325                                                RADIUS_ATTR_ACCT_OUTPUT_OCTETS,
326                                                data.tx_bytes)) {
327                         wpa_printf(MSG_INFO, "Could not add Acct-Output-Octets");
328                         goto fail;
329                 }
330                 gigawords = sta->acct_output_gigawords;
331 #if __WORDSIZE == 64
332                 gigawords += data.tx_bytes >> 32;
333 #endif
334                 if (gigawords &&
335                     !radius_msg_add_attr_int32(
336                             msg, RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS,
337                             gigawords)) {
338                         wpa_printf(MSG_INFO, "Could not add Acct-Output-Gigawords");
339                         goto fail;
340                 }
341         }
342
343         if (eloop_terminated())
344                 cause = RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT;
345
346         if (stop && cause &&
347             !radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
348                                        cause)) {
349                 wpa_printf(MSG_INFO, "Could not add Acct-Terminate-Cause");
350                 goto fail;
351         }
352
353         if (radius_client_send(hapd->radius, msg,
354                                stop ? RADIUS_ACCT : RADIUS_ACCT_INTERIM,
355                                sta->addr) < 0)
356                 goto fail;
357         return;
358
359  fail:
360         radius_msg_free(msg);
361 }
362
363
364 /**
365  * accounting_sta_interim - Send a interim STA accounting report
366  * @hapd: hostapd BSS data
367  * @sta: The station
368  */
369 static void accounting_sta_interim(struct hostapd_data *hapd,
370                                    struct sta_info *sta)
371 {
372         if (sta->acct_session_started)
373                 accounting_sta_report(hapd, sta, 0);
374 }
375
376
377 /**
378  * accounting_sta_stop - Stop STA accounting
379  * @hapd: hostapd BSS data
380  * @sta: The station
381  */
382 void accounting_sta_stop(struct hostapd_data *hapd, struct sta_info *sta)
383 {
384         if (sta->acct_session_started) {
385                 accounting_sta_report(hapd, sta, 1);
386                 eloop_cancel_timeout(accounting_interim_update, hapd, sta);
387                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
388                                HOSTAPD_LEVEL_INFO,
389                                "stopped accounting session %016lX",
390                                (long unsigned int) sta->acct_session_id);
391                 sta->acct_session_started = 0;
392         }
393 }
394
395
396 int accounting_sta_get_id(struct hostapd_data *hapd, struct sta_info *sta)
397 {
398         /*
399          * Acct-Session-Id should be globally and temporarily unique.
400          * A high quality random number is required therefore.
401          * This could be be improved by switching to a GUID.
402          */
403         return os_get_random((u8 *) &sta->acct_session_id,
404                              sizeof(sta->acct_session_id));
405 }
406
407
408 /**
409  * accounting_receive - Process the RADIUS frames from Accounting Server
410  * @msg: RADIUS response message
411  * @req: RADIUS request message
412  * @shared_secret: RADIUS shared secret
413  * @shared_secret_len: Length of shared_secret in octets
414  * @data: Context data (struct hostapd_data *)
415  * Returns: Processing status
416  */
417 static RadiusRxResult
418 accounting_receive(struct radius_msg *msg, struct radius_msg *req,
419                    const u8 *shared_secret, size_t shared_secret_len,
420                    void *data)
421 {
422         if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_RESPONSE) {
423                 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
424                 return RADIUS_RX_UNKNOWN;
425         }
426
427         if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
428                 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have correct Authenticator - dropped");
429                 return RADIUS_RX_INVALID_AUTHENTICATOR;
430         }
431
432         return RADIUS_RX_PROCESSED;
433 }
434
435
436 static void accounting_report_state(struct hostapd_data *hapd, int on)
437 {
438         struct radius_msg *msg;
439
440         if (!hapd->conf->radius->acct_server || hapd->radius == NULL)
441                 return;
442
443         /* Inform RADIUS server that accounting will start/stop so that the
444          * server can close old accounting sessions. */
445         msg = accounting_msg(hapd, NULL,
446                              on ? RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON :
447                              RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
448         if (!msg)
449                 return;
450
451         if (radius_client_send(hapd->radius, msg, RADIUS_ACCT, NULL) < 0)
452                 radius_msg_free(msg);
453 }
454
455
456 /**
457  * accounting_init: Initialize accounting
458  * @hapd: hostapd BSS data
459  * Returns: 0 on success, -1 on failure
460  */
461 int accounting_init(struct hostapd_data *hapd)
462 {
463         if (radius_client_register(hapd->radius, RADIUS_ACCT,
464                                    accounting_receive, hapd))
465                 return -1;
466
467         accounting_report_state(hapd, 1);
468
469         return 0;
470 }
471
472
473 /**
474  * accounting_deinit: Deinitialize accounting
475  * @hapd: hostapd BSS data
476  */
477 void accounting_deinit(struct hostapd_data *hapd)
478 {
479         accounting_report_state(hapd, 0);
480 }