Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/ap/ieee802_1x.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * hostapd / IEEE 802.1X-2004 Authenticator
3
 * Copyright (c) 2002-2019, 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
#ifdef CONFIG_SQLITE
11
#include <sqlite3.h>
12
#endif /* CONFIG_SQLITE */
13
14
#include "utils/common.h"
15
#include "utils/eloop.h"
16
#include "crypto/md5.h"
17
#include "crypto/crypto.h"
18
#include "crypto/random.h"
19
#include "common/ieee802_11_defs.h"
20
#include "radius/radius.h"
21
#include "radius/radius_client.h"
22
#include "eap_server/eap.h"
23
#include "eap_common/eap_wsc_common.h"
24
#include "eapol_auth/eapol_auth_sm.h"
25
#include "eapol_auth/eapol_auth_sm_i.h"
26
#include "p2p/p2p.h"
27
#include "hostapd.h"
28
#include "accounting.h"
29
#include "sta_info.h"
30
#include "wpa_auth.h"
31
#include "preauth_auth.h"
32
#include "pmksa_cache_auth.h"
33
#include "ap_config.h"
34
#include "ap_drv_ops.h"
35
#include "wps_hostapd.h"
36
#include "hs20.h"
37
/* FIX: Not really a good thing to require ieee802_11.h here.. (FILS) */
38
#include "ieee802_11.h"
39
#include "ieee802_1x.h"
40
#include "wpa_auth_kay.h"
41
42
43
#ifdef CONFIG_HS20
44
static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx);
45
#endif /* CONFIG_HS20 */
46
static bool ieee802_1x_finished(struct hostapd_data *hapd,
47
        struct sta_info *sta, int success,
48
        bool logoff);
49
50
51
static void ieee802_1x_send(struct hostapd_data *hapd, struct sta_info *sta,
52
          u8 type, const u8 *data, size_t datalen)
53
0
{
54
0
  u8 *buf;
55
0
  struct ieee802_1x_hdr *xhdr;
56
0
  size_t len;
57
0
  int encrypt = 0;
58
59
0
  len = sizeof(*xhdr) + datalen;
60
0
  buf = os_zalloc(len);
61
0
  if (!buf) {
62
0
    wpa_printf(MSG_ERROR, "malloc() failed for %s(len=%lu)",
63
0
         __func__, (unsigned long) len);
64
0
    return;
65
0
  }
66
67
0
  xhdr = (struct ieee802_1x_hdr *) buf;
68
0
  xhdr->version = hapd->conf->eapol_version;
69
#ifdef CONFIG_MACSEC
70
  if (xhdr->version > 2 && hapd->conf->macsec_policy == 0)
71
    xhdr->version = 2;
72
#endif /* CONFIG_MACSEC */
73
0
  xhdr->type = type;
74
0
  xhdr->length = host_to_be16(datalen);
75
76
0
  if (datalen > 0 && data != NULL)
77
0
    os_memcpy(xhdr + 1, data, datalen);
78
79
0
  if (wpa_auth_pairwise_set(sta->wpa_sm))
80
0
    encrypt = 1;
81
#ifdef CONFIG_TESTING_OPTIONS
82
  if (hapd->ext_eapol_frame_io) {
83
    size_t hex_len = 2 * len + 1;
84
    char *hex = os_malloc(hex_len);
85
86
    if (hex) {
87
      wpa_snprintf_hex(hex, hex_len, buf, len);
88
      wpa_msg(hapd->msg_ctx, MSG_INFO,
89
        "EAPOL-TX " MACSTR " %s",
90
        MAC2STR(sta->addr), hex);
91
      os_free(hex);
92
    }
93
  } else
94
#endif /* CONFIG_TESTING_OPTIONS */
95
0
  if (sta->flags & WLAN_STA_PREAUTH) {
96
0
    rsn_preauth_send(hapd, sta, buf, len);
97
0
  } else {
98
0
    int link_id = -1;
99
100
#ifdef CONFIG_IEEE80211BE
101
    link_id = hapd->conf->mld_ap ? hapd->mld_link_id : -1;
102
#endif /* CONFIG_IEEE80211BE */
103
0
    hostapd_drv_hapd_send_eapol(
104
0
      hapd, sta->addr, buf, len,
105
0
      encrypt, hostapd_sta_flags_to_drv(sta->flags), link_id);
106
0
  }
107
108
0
  os_free(buf);
109
0
}
110
111
112
static void ieee802_1x_set_authorized(struct hostapd_data *hapd,
113
              struct sta_info *sta,
114
              bool authorized, bool mld)
115
0
{
116
0
  int res;
117
0
  bool update;
118
119
0
  if (sta->flags & WLAN_STA_PREAUTH)
120
0
    return;
121
122
0
  update = ap_sta_set_authorized_flag(hapd, sta, authorized);
123
0
  res = hostapd_set_authorized(hapd, sta, authorized);
124
0
  if (update)
125
0
    ap_sta_set_authorized_event(hapd, sta, authorized);
126
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
127
0
           HOSTAPD_LEVEL_DEBUG, "%sauthorizing port",
128
0
           authorized ? "" : "un");
129
130
0
  if (!mld && res && errno != ENOENT) {
131
0
    wpa_printf(MSG_DEBUG, "Could not set station " MACSTR
132
0
         " flags for kernel driver (errno=%d).",
133
0
         MAC2STR(sta->addr), errno);
134
0
  } else if (mld && res) {
135
0
    wpa_printf(MSG_DEBUG,
136
0
         "MLD: Could not set station " MACSTR " flags",
137
0
         MAC2STR(sta->addr));
138
0
  }
139
140
0
  if (authorized) {
141
0
    os_get_reltime(&sta->connected_time);
142
0
    accounting_sta_start(hapd, sta);
143
0
  }
144
0
}
145
146
147
static void ieee802_1x_ml_set_sta_authorized(struct hostapd_data *hapd,
148
               struct sta_info *sta,
149
               bool authorized)
150
0
{
151
#ifdef CONFIG_IEEE80211BE
152
  unsigned int i;
153
154
  if (!hostapd_is_mld_ap(hapd))
155
    return;
156
157
  /*
158
   * Authorizing the station should be done only in the station
159
   * performing the association
160
   */
161
  if (authorized && hapd->mld_link_id != sta->mld_assoc_link_id)
162
    return;
163
164
  for (i = 0; i < hapd->iface->interfaces->count; i++) {
165
    struct sta_info *tmp_sta;
166
    struct mld_link_info *link;
167
    struct hostapd_data *tmp_hapd =
168
      hapd->iface->interfaces->iface[i]->bss[0];
169
170
    if (!hostapd_is_ml_partner(hapd, tmp_hapd))
171
      continue;
172
173
    link = &sta->mld_info.links[tmp_hapd->mld_link_id];
174
    if (!link->valid)
175
      continue;
176
177
    for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
178
         tmp_sta = tmp_sta->next) {
179
      if (tmp_sta == sta ||
180
          tmp_sta->mld_assoc_link_id !=
181
          sta->mld_assoc_link_id ||
182
          tmp_sta->aid != sta->aid)
183
        continue;
184
185
      ieee802_1x_set_authorized(tmp_hapd, tmp_sta,
186
              authorized, true);
187
      break;
188
    }
189
  }
190
#endif /* CONFIG_IEEE80211BE */
191
0
}
192
193
194
195
void ieee802_1x_set_sta_authorized(struct hostapd_data *hapd,
196
           struct sta_info *sta, int authorized)
197
0
{
198
0
  ieee802_1x_set_authorized(hapd, sta, authorized, false);
199
0
  ieee802_1x_ml_set_sta_authorized(hapd, sta, !!authorized);
200
0
}
201
202
203
#ifdef CONFIG_WEP
204
#ifndef CONFIG_FIPS
205
#ifndef CONFIG_NO_RC4
206
207
static void ieee802_1x_tx_key_one(struct hostapd_data *hapd,
208
          struct sta_info *sta,
209
          int idx, int broadcast,
210
          u8 *key_data, size_t key_len)
211
{
212
  u8 *buf, *ekey;
213
  struct ieee802_1x_hdr *hdr;
214
  struct ieee802_1x_eapol_key *key;
215
  size_t len, ekey_len;
216
  struct eapol_state_machine *sm = sta->eapol_sm;
217
218
  if (!sm)
219
    return;
220
221
  len = sizeof(*key) + key_len;
222
  buf = os_zalloc(sizeof(*hdr) + len);
223
  if (!buf)
224
    return;
225
226
  hdr = (struct ieee802_1x_hdr *) buf;
227
  key = (struct ieee802_1x_eapol_key *) (hdr + 1);
228
  key->type = EAPOL_KEY_TYPE_RC4;
229
  WPA_PUT_BE16(key->key_length, key_len);
230
  wpa_get_ntp_timestamp(key->replay_counter);
231
  if (os_memcmp(key->replay_counter,
232
          hapd->last_1x_eapol_key_replay_counter,
233
          IEEE8021X_REPLAY_COUNTER_LEN) <= 0) {
234
    /* NTP timestamp did not increment from last EAPOL-Key frame;
235
     * use previously used value + 1 instead. */
236
    inc_byte_array(hapd->last_1x_eapol_key_replay_counter,
237
             IEEE8021X_REPLAY_COUNTER_LEN);
238
    os_memcpy(key->replay_counter,
239
        hapd->last_1x_eapol_key_replay_counter,
240
        IEEE8021X_REPLAY_COUNTER_LEN);
241
  } else {
242
    os_memcpy(hapd->last_1x_eapol_key_replay_counter,
243
        key->replay_counter,
244
        IEEE8021X_REPLAY_COUNTER_LEN);
245
  }
246
247
  if (random_get_bytes(key->key_iv, sizeof(key->key_iv))) {
248
    wpa_printf(MSG_ERROR, "Could not get random numbers");
249
    os_free(buf);
250
    return;
251
  }
252
253
  key->key_index = idx | (broadcast ? 0 : BIT(7));
254
  if (hapd->conf->eapol_key_index_workaround) {
255
    /* According to some information, WinXP Supplicant seems to
256
     * interpret bit7 as an indication whether the key is to be
257
     * activated, so make it possible to enable workaround that
258
     * sets this bit for all keys. */
259
    key->key_index |= BIT(7);
260
  }
261
262
  /* Key is encrypted using "Key-IV + MSK[0..31]" as the RC4-key and
263
   * MSK[32..63] is used to sign the message. */
264
  if (!sm->eap_if->eapKeyData || sm->eap_if->eapKeyDataLen < 64) {
265
    wpa_printf(MSG_ERROR,
266
         "No eapKeyData available for encrypting and signing EAPOL-Key");
267
    os_free(buf);
268
    return;
269
  }
270
  os_memcpy((u8 *) (key + 1), key_data, key_len);
271
  ekey_len = sizeof(key->key_iv) + 32;
272
  ekey = os_malloc(ekey_len);
273
  if (!ekey) {
274
    wpa_printf(MSG_ERROR, "Could not encrypt key");
275
    os_free(buf);
276
    return;
277
  }
278
  os_memcpy(ekey, key->key_iv, sizeof(key->key_iv));
279
  os_memcpy(ekey + sizeof(key->key_iv), sm->eap_if->eapKeyData, 32);
280
  rc4_skip(ekey, ekey_len, 0, (u8 *) (key + 1), key_len);
281
  os_free(ekey);
282
283
  /* This header is needed here for HMAC-MD5, but it will be regenerated
284
   * in ieee802_1x_send() */
285
  hdr->version = hapd->conf->eapol_version;
286
#ifdef CONFIG_MACSEC
287
  if (hdr->version > 2)
288
    hdr->version = 2;
289
#endif /* CONFIG_MACSEC */
290
  hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
291
  hdr->length = host_to_be16(len);
292
  hmac_md5(sm->eap_if->eapKeyData + 32, 32, buf, sizeof(*hdr) + len,
293
     key->key_signature);
294
295
  wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key to " MACSTR
296
       " (%s index=%d)", MAC2STR(sm->addr),
297
       broadcast ? "broadcast" : "unicast", idx);
298
  ieee802_1x_send(hapd, sta, IEEE802_1X_TYPE_EAPOL_KEY, (u8 *) key, len);
299
  if (sta->eapol_sm)
300
    sta->eapol_sm->dot1xAuthEapolFramesTx++;
301
  os_free(buf);
302
}
303
304
305
static void ieee802_1x_tx_key(struct hostapd_data *hapd, struct sta_info *sta)
306
{
307
  struct eapol_authenticator *eapol = hapd->eapol_auth;
308
  struct eapol_state_machine *sm = sta->eapol_sm;
309
310
  if (!sm || !sm->eap_if->eapKeyData)
311
    return;
312
313
  wpa_printf(MSG_DEBUG, "IEEE 802.1X: Sending EAPOL-Key(s) to " MACSTR,
314
       MAC2STR(sta->addr));
315
316
#ifndef CONFIG_NO_VLAN
317
  if (sta->vlan_id > 0) {
318
    wpa_printf(MSG_ERROR, "Using WEP with vlans is not supported.");
319
    return;
320
  }
321
#endif /* CONFIG_NO_VLAN */
322
323
  if (eapol->default_wep_key) {
324
    ieee802_1x_tx_key_one(hapd, sta, eapol->default_wep_key_idx, 1,
325
              eapol->default_wep_key,
326
              hapd->conf->default_wep_key_len);
327
  }
328
329
  if (hapd->conf->individual_wep_key_len > 0) {
330
    u8 *ikey;
331
332
    ikey = os_malloc(hapd->conf->individual_wep_key_len);
333
    if (!ikey ||
334
        random_get_bytes(ikey, hapd->conf->individual_wep_key_len))
335
    {
336
      wpa_printf(MSG_ERROR,
337
           "Could not generate random individual WEP key");
338
      os_free(ikey);
339
      return;
340
    }
341
342
    wpa_hexdump_key(MSG_DEBUG, "Individual WEP key",
343
        ikey, hapd->conf->individual_wep_key_len);
344
345
    ieee802_1x_tx_key_one(hapd, sta, 0, 0, ikey,
346
              hapd->conf->individual_wep_key_len);
347
348
    /* TODO: set encryption in TX callback, i.e., only after STA
349
     * has ACKed EAPOL-Key frame */
350
    if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
351
          sta->addr, 0, 0, 1, NULL, 0, ikey,
352
          hapd->conf->individual_wep_key_len,
353
          KEY_FLAG_PAIRWISE_RX_TX)) {
354
      wpa_printf(MSG_ERROR,
355
           "Could not set individual WEP encryption");
356
    }
357
358
    os_free(ikey);
359
  }
360
}
361
362
#endif /* CONFIG_NO_RC4 */
363
#endif /* CONFIG_FIPS */
364
#endif /* CONFIG_WEP */
365
366
367
const char *radius_mode_txt(struct hostapd_data *hapd)
368
0
{
369
0
  switch (hapd->iface->conf->hw_mode) {
370
0
  case HOSTAPD_MODE_IEEE80211AD:
371
0
    return "802.11ad";
372
0
  case HOSTAPD_MODE_IEEE80211A:
373
0
    return "802.11a";
374
0
  case HOSTAPD_MODE_IEEE80211G:
375
0
    return "802.11g";
376
0
  case HOSTAPD_MODE_IEEE80211B:
377
0
  default:
378
0
    return "802.11b";
379
0
  }
380
0
}
381
382
383
int radius_sta_rate(struct hostapd_data *hapd, struct sta_info *sta)
384
0
{
385
0
  int i;
386
0
  u8 rate = 0;
387
388
0
  for (i = 0; i < sta->supported_rates_len; i++)
389
0
    if ((sta->supported_rates[i] & 0x7f) > rate)
390
0
      rate = sta->supported_rates[i] & 0x7f;
391
392
0
  return rate;
393
0
}
394
395
396
#ifndef CONFIG_NO_RADIUS
397
static void ieee802_1x_learn_identity(struct hostapd_data *hapd,
398
              struct eapol_state_machine *sm,
399
              const u8 *eap, size_t len)
400
0
{
401
0
  const u8 *identity;
402
0
  size_t identity_len;
403
0
  const struct eap_hdr *hdr = (const struct eap_hdr *) eap;
404
405
0
  if (len <= sizeof(struct eap_hdr) ||
406
0
      (hdr->code == EAP_CODE_RESPONSE &&
407
0
       eap[sizeof(struct eap_hdr)] != EAP_TYPE_IDENTITY) ||
408
0
      (hdr->code == EAP_CODE_INITIATE &&
409
0
       eap[sizeof(struct eap_hdr)] != EAP_ERP_TYPE_REAUTH) ||
410
0
      (hdr->code != EAP_CODE_RESPONSE &&
411
0
       hdr->code != EAP_CODE_INITIATE))
412
0
    return;
413
414
0
  eap_erp_update_identity(sm->eap, eap, len);
415
0
  identity = eap_get_identity(sm->eap, &identity_len);
416
0
  if (!identity)
417
0
    return;
418
419
  /* Save station identity for future RADIUS packets */
420
0
  os_free(sm->identity);
421
0
  sm->identity = (u8 *) dup_binstr(identity, identity_len);
422
0
  if (!sm->identity) {
423
0
    sm->identity_len = 0;
424
0
    return;
425
0
  }
426
427
0
  sm->identity_len = identity_len;
428
0
  hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
429
0
           HOSTAPD_LEVEL_DEBUG, "STA identity '%s'", sm->identity);
430
0
  sm->dot1xAuthEapolRespIdFramesRx++;
431
0
}
432
433
434
static int add_common_radius_sta_attr_rsn(struct hostapd_data *hapd,
435
            struct hostapd_radius_attr *req_attr,
436
            struct sta_info *sta,
437
            struct radius_msg *msg)
438
0
{
439
0
  u32 suite;
440
0
  int ver, val;
441
442
0
  ver = wpa_auth_sta_wpa_version(sta->wpa_sm);
443
0
  val = wpa_auth_get_pairwise(sta->wpa_sm);
444
0
  suite = wpa_cipher_to_suite(ver, val);
445
0
  if (val != -1 &&
446
0
      !hostapd_config_get_radius_attr(req_attr,
447
0
              RADIUS_ATTR_WLAN_PAIRWISE_CIPHER) &&
448
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_PAIRWISE_CIPHER,
449
0
               suite)) {
450
0
    wpa_printf(MSG_ERROR, "Could not add WLAN-Pairwise-Cipher");
451
0
    return -1;
452
0
  }
453
454
0
  suite = wpa_cipher_to_suite(((hapd->conf->wpa & 0x2)) ?
455
0
            WPA_PROTO_RSN : WPA_PROTO_WPA,
456
0
            hapd->conf->wpa_group);
457
0
  if (!hostapd_config_get_radius_attr(req_attr,
458
0
              RADIUS_ATTR_WLAN_GROUP_CIPHER) &&
459
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_GROUP_CIPHER,
460
0
               suite)) {
461
0
    wpa_printf(MSG_ERROR, "Could not add WLAN-Group-Cipher");
462
0
    return -1;
463
0
  }
464
465
0
  val = wpa_auth_sta_key_mgmt(sta->wpa_sm);
466
0
  suite = wpa_akm_to_suite(val);
467
0
  if (val != -1 &&
468
0
      !hostapd_config_get_radius_attr(req_attr,
469
0
              RADIUS_ATTR_WLAN_AKM_SUITE) &&
470
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_WLAN_AKM_SUITE,
471
0
               suite)) {
472
0
    wpa_printf(MSG_ERROR, "Could not add WLAN-AKM-Suite");
473
0
    return -1;
474
0
  }
475
476
0
  if (hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
477
0
    suite = wpa_cipher_to_suite(WPA_PROTO_RSN,
478
0
              hapd->conf->group_mgmt_cipher);
479
0
    if (!hostapd_config_get_radius_attr(
480
0
          req_attr, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER) &&
481
0
        !radius_msg_add_attr_int32(
482
0
          msg, RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER, suite)) {
483
0
      wpa_printf(MSG_ERROR,
484
0
           "Could not add WLAN-Group-Mgmt-Cipher");
485
0
      return -1;
486
0
    }
487
0
  }
488
489
0
  return 0;
490
0
}
491
492
493
static int add_common_radius_sta_attr(struct hostapd_data *hapd,
494
              struct hostapd_radius_attr *req_attr,
495
              struct sta_info *sta,
496
              struct radius_msg *msg)
497
0
{
498
0
  char buf[128];
499
500
0
  if (!hostapd_config_get_radius_attr(req_attr,
501
0
              RADIUS_ATTR_SERVICE_TYPE) &&
502
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
503
0
               RADIUS_SERVICE_TYPE_FRAMED)) {
504
0
    wpa_printf(MSG_ERROR, "Could not add Service-Type");
505
0
    return -1;
506
0
  }
507
508
0
  if (!hostapd_config_get_radius_attr(req_attr,
509
0
              RADIUS_ATTR_NAS_PORT) &&
510
0
      sta->aid > 0 &&
511
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
512
0
    wpa_printf(MSG_ERROR, "Could not add NAS-Port");
513
0
    return -1;
514
0
  }
515
516
0
  os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
517
0
        MAC2STR(sta->addr));
518
0
  buf[sizeof(buf) - 1] = '\0';
519
0
  if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
520
0
         (u8 *) buf, os_strlen(buf))) {
521
0
    wpa_printf(MSG_ERROR, "Could not add Calling-Station-Id");
522
0
    return -1;
523
0
  }
524
525
0
  if (sta->flags & WLAN_STA_PREAUTH) {
526
0
    os_strlcpy(buf, "IEEE 802.11i Pre-Authentication",
527
0
         sizeof(buf));
528
0
  } else {
529
0
    os_snprintf(buf, sizeof(buf), "CONNECT %d%sMbps %s",
530
0
          radius_sta_rate(hapd, sta) / 2,
531
0
          (radius_sta_rate(hapd, sta) & 1) ? ".5" : "",
532
0
          radius_mode_txt(hapd));
533
0
    buf[sizeof(buf) - 1] = '\0';
534
0
  }
535
0
  if (!hostapd_config_get_radius_attr(req_attr,
536
0
              RADIUS_ATTR_CONNECT_INFO) &&
537
0
      !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
538
0
         (u8 *) buf, os_strlen(buf))) {
539
0
    wpa_printf(MSG_ERROR, "Could not add Connect-Info");
540
0
    return -1;
541
0
  }
542
543
0
  if (sta->acct_session_id) {
544
0
    os_snprintf(buf, sizeof(buf), "%016llX",
545
0
          (unsigned long long) sta->acct_session_id);
546
0
    if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
547
0
           (u8 *) buf, os_strlen(buf))) {
548
0
      wpa_printf(MSG_ERROR, "Could not add Acct-Session-Id");
549
0
      return -1;
550
0
    }
551
0
  }
552
553
0
  if ((hapd->conf->wpa & 2) &&
554
0
      !hapd->conf->disable_pmksa_caching &&
555
0
      sta->eapol_sm && sta->eapol_sm->acct_multi_session_id) {
556
0
    os_snprintf(buf, sizeof(buf), "%016llX",
557
0
          (unsigned long long)
558
0
          sta->eapol_sm->acct_multi_session_id);
559
0
    if (!radius_msg_add_attr(
560
0
          msg, RADIUS_ATTR_ACCT_MULTI_SESSION_ID,
561
0
          (u8 *) buf, os_strlen(buf))) {
562
0
      wpa_printf(MSG_INFO,
563
0
           "Could not add Acct-Multi-Session-Id");
564
0
      return -1;
565
0
    }
566
0
  }
567
568
0
#ifdef CONFIG_IEEE80211R_AP
569
0
  if (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
570
0
      sta->wpa_sm &&
571
0
      (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) ||
572
0
       sta->auth_alg == WLAN_AUTH_FT) &&
573
0
      !hostapd_config_get_radius_attr(req_attr,
574
0
              RADIUS_ATTR_MOBILITY_DOMAIN_ID) &&
575
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_MOBILITY_DOMAIN_ID,
576
0
               WPA_GET_BE16(
577
0
                 hapd->conf->mobility_domain))) {
578
0
    wpa_printf(MSG_ERROR, "Could not add Mobility-Domain-Id");
579
0
    return -1;
580
0
  }
581
0
#endif /* CONFIG_IEEE80211R_AP */
582
583
0
  if (hapd->conf->wpa && sta->wpa_sm &&
584
0
      add_common_radius_sta_attr_rsn(hapd, req_attr, sta, msg) < 0)
585
0
    return -1;
586
587
0
  return 0;
588
0
}
589
590
591
int add_common_radius_attr(struct hostapd_data *hapd,
592
         struct hostapd_radius_attr *req_attr,
593
         struct sta_info *sta,
594
         struct radius_msg *msg)
595
0
{
596
0
  char buf[128];
597
0
  struct hostapd_radius_attr *attr;
598
0
  int len;
599
600
0
  if (!hostapd_config_get_radius_attr(req_attr,
601
0
              RADIUS_ATTR_NAS_IP_ADDRESS) &&
602
0
      hapd->conf->own_ip_addr.af == AF_INET &&
603
0
      !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
604
0
         (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
605
0
    wpa_printf(MSG_ERROR, "Could not add NAS-IP-Address");
606
0
    return -1;
607
0
  }
608
609
0
#ifdef CONFIG_IPV6
610
0
  if (!hostapd_config_get_radius_attr(req_attr,
611
0
              RADIUS_ATTR_NAS_IPV6_ADDRESS) &&
612
0
      hapd->conf->own_ip_addr.af == AF_INET6 &&
613
0
      !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
614
0
         (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
615
0
    wpa_printf(MSG_ERROR, "Could not add NAS-IPv6-Address");
616
0
    return -1;
617
0
  }
618
0
#endif /* CONFIG_IPV6 */
619
620
0
  if (!hostapd_config_get_radius_attr(req_attr,
621
0
              RADIUS_ATTR_NAS_IDENTIFIER) &&
622
0
      hapd->conf->nas_identifier &&
623
0
      !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
624
0
         (u8 *) hapd->conf->nas_identifier,
625
0
         os_strlen(hapd->conf->nas_identifier))) {
626
0
    wpa_printf(MSG_ERROR, "Could not add NAS-Identifier");
627
0
    return -1;
628
0
  }
629
630
0
  len = os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":",
631
0
        MAC2STR(hapd->own_addr));
632
0
  os_memcpy(&buf[len], hapd->conf->ssid.ssid,
633
0
      hapd->conf->ssid.ssid_len);
634
0
  len += hapd->conf->ssid.ssid_len;
635
0
  if (!hostapd_config_get_radius_attr(req_attr,
636
0
              RADIUS_ATTR_CALLED_STATION_ID) &&
637
0
      !radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
638
0
         (u8 *) buf, len)) {
639
0
    wpa_printf(MSG_ERROR, "Could not add Called-Station-Id");
640
0
    return -1;
641
0
  }
642
643
0
  if (!hostapd_config_get_radius_attr(req_attr,
644
0
              RADIUS_ATTR_NAS_PORT_TYPE) &&
645
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
646
0
               RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
647
0
    wpa_printf(MSG_ERROR, "Could not add NAS-Port-Type");
648
0
    return -1;
649
0
  }
650
651
0
#ifdef CONFIG_INTERWORKING
652
0
  if (hapd->conf->interworking &&
653
0
      !is_zero_ether_addr(hapd->conf->hessid)) {
654
0
    os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
655
0
          MAC2STR(hapd->conf->hessid));
656
0
    buf[sizeof(buf) - 1] = '\0';
657
0
    if (!hostapd_config_get_radius_attr(req_attr,
658
0
                RADIUS_ATTR_WLAN_HESSID) &&
659
0
        !radius_msg_add_attr(msg, RADIUS_ATTR_WLAN_HESSID,
660
0
           (u8 *) buf, os_strlen(buf))) {
661
0
      wpa_printf(MSG_ERROR, "Could not add WLAN-HESSID");
662
0
      return -1;
663
0
    }
664
0
  }
665
0
#endif /* CONFIG_INTERWORKING */
666
667
0
  if (sta && add_common_radius_sta_attr(hapd, req_attr, sta, msg) < 0)
668
0
    return -1;
669
670
0
  for (attr = req_attr; attr; attr = attr->next) {
671
0
    if (!radius_msg_add_attr(msg, attr->type,
672
0
           wpabuf_head(attr->val),
673
0
           wpabuf_len(attr->val))) {
674
0
      wpa_printf(MSG_ERROR, "Could not add RADIUS attribute");
675
0
      return -1;
676
0
    }
677
0
  }
678
679
0
  return 0;
680
0
}
681
682
683
int add_sqlite_radius_attr(struct hostapd_data *hapd, struct sta_info *sta,
684
         struct radius_msg *msg, int acct)
685
0
{
686
#ifdef CONFIG_SQLITE
687
  const char *attrtxt;
688
  char addrtxt[3 * ETH_ALEN];
689
  char *sql;
690
  sqlite3_stmt *stmt = NULL;
691
692
  if (!hapd->rad_attr_db)
693
    return 0;
694
695
  os_snprintf(addrtxt, sizeof(addrtxt), MACSTR, MAC2STR(sta->addr));
696
697
  sql = "SELECT attr FROM radius_attributes WHERE sta=? AND (reqtype=? OR reqtype IS NULL);";
698
  if (sqlite3_prepare_v2(hapd->rad_attr_db, sql, os_strlen(sql), &stmt,
699
             NULL) != SQLITE_OK) {
700
    wpa_printf(MSG_ERROR, "DB: Failed to prepare SQL statement: %s",
701
         sqlite3_errmsg(hapd->rad_attr_db));
702
    return -1;
703
  }
704
  sqlite3_bind_text(stmt, 1, addrtxt, os_strlen(addrtxt), SQLITE_STATIC);
705
  sqlite3_bind_text(stmt, 2, acct ? "acct" : "auth", 4, SQLITE_STATIC);
706
  while (sqlite3_step(stmt) == SQLITE_ROW) {
707
    struct hostapd_radius_attr *attr;
708
    struct radius_attr_hdr *hdr;
709
710
    attrtxt = (const char *) sqlite3_column_text(stmt, 0);
711
    attr = hostapd_parse_radius_attr(attrtxt);
712
    if (!attr) {
713
      wpa_printf(MSG_ERROR,
714
           "Skipping invalid attribute from SQL: %s",
715
           attrtxt);
716
      continue;
717
    }
718
    wpa_printf(MSG_DEBUG, "Adding RADIUS attribute from SQL: %s",
719
         attrtxt);
720
    hdr = radius_msg_add_attr(msg, attr->type,
721
            wpabuf_head(attr->val),
722
            wpabuf_len(attr->val));
723
    hostapd_config_free_radius_attr(attr);
724
    if (!hdr) {
725
      wpa_printf(MSG_ERROR,
726
           "Could not add RADIUS attribute from SQL");
727
      continue;
728
    }
729
  }
730
731
  sqlite3_reset(stmt);
732
  sqlite3_clear_bindings(stmt);
733
  sqlite3_finalize(stmt);
734
#endif /* CONFIG_SQLITE */
735
736
0
  return 0;
737
0
}
738
739
740
void ieee802_1x_encapsulate_radius(struct hostapd_data *hapd,
741
           struct sta_info *sta,
742
           const u8 *eap, size_t len)
743
0
{
744
0
  struct radius_msg *msg;
745
0
  struct eapol_state_machine *sm = sta->eapol_sm;
746
747
0
  if (!sm)
748
0
    return;
749
750
0
  ieee802_1x_learn_identity(hapd, sm, eap, len);
751
752
0
  wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS packet");
753
754
0
  sm->radius_identifier = radius_client_get_id(hapd->radius);
755
0
  msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
756
0
           sm->radius_identifier);
757
0
  if (!msg) {
758
0
    wpa_printf(MSG_INFO, "Could not create new RADIUS packet");
759
0
    return;
760
0
  }
761
762
0
  if (radius_msg_make_authenticator(msg) < 0) {
763
0
    wpa_printf(MSG_INFO, "Could not make Request Authenticator");
764
0
    goto fail;
765
0
  }
766
767
0
  if (!radius_msg_add_msg_auth(msg))
768
0
    goto fail;
769
770
0
  if (sm->identity &&
771
0
      !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
772
0
         sm->identity, sm->identity_len)) {
773
0
    wpa_printf(MSG_INFO, "Could not add User-Name");
774
0
    goto fail;
775
0
  }
776
777
0
  if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr, sta,
778
0
           msg) < 0)
779
0
    goto fail;
780
781
0
  if (sta && add_sqlite_radius_attr(hapd, sta, msg, 0) < 0)
782
0
    goto fail;
783
784
  /* TODO: should probably check MTU from driver config; 2304 is max for
785
   * IEEE 802.11, but use 1400 to avoid problems with too large packets
786
   */
787
0
  if (!hostapd_config_get_radius_attr(hapd->conf->radius_auth_req_attr,
788
0
              RADIUS_ATTR_FRAMED_MTU) &&
789
0
      !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
790
0
    wpa_printf(MSG_INFO, "Could not add Framed-MTU");
791
0
    goto fail;
792
0
  }
793
794
0
  if (!radius_msg_add_eap(msg, eap, len)) {
795
0
    wpa_printf(MSG_INFO, "Could not add EAP-Message");
796
0
    goto fail;
797
0
  }
798
799
  /* State attribute must be copied if and only if this packet is
800
   * Access-Request reply to the previous Access-Challenge */
801
0
  if (sm->last_recv_radius &&
802
0
      radius_msg_get_hdr(sm->last_recv_radius)->code ==
803
0
      RADIUS_CODE_ACCESS_CHALLENGE) {
804
0
    int res = radius_msg_copy_attr(msg, sm->last_recv_radius,
805
0
                 RADIUS_ATTR_STATE);
806
0
    if (res < 0) {
807
0
      wpa_printf(MSG_INFO,
808
0
           "Could not copy State attribute from previous Access-Challenge");
809
0
      goto fail;
810
0
    }
811
0
    if (res > 0)
812
0
      wpa_printf(MSG_DEBUG, "Copied RADIUS State Attribute");
813
0
  }
814
815
0
  if (hapd->conf->radius_request_cui) {
816
0
    const u8 *cui;
817
0
    size_t cui_len;
818
    /* Add previously learned CUI or nul CUI to request CUI */
819
0
    if (sm->radius_cui) {
820
0
      cui = wpabuf_head(sm->radius_cui);
821
0
      cui_len = wpabuf_len(sm->radius_cui);
822
0
    } else {
823
0
      cui = (const u8 *) "\0";
824
0
      cui_len = 1;
825
0
    }
826
0
    if (!radius_msg_add_attr(msg,
827
0
           RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
828
0
           cui, cui_len)) {
829
0
      wpa_printf(MSG_ERROR, "Could not add CUI");
830
0
      goto fail;
831
0
    }
832
0
  }
833
834
0
#ifdef CONFIG_HS20
835
0
  if (hapd->conf->hs20) {
836
0
    u8 ver = hapd->conf->hs20_release - 1;
837
838
0
    if (!radius_msg_add_wfa(
839
0
          msg, RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION,
840
0
          &ver, 1)) {
841
0
      wpa_printf(MSG_ERROR,
842
0
           "Could not add HS 2.0 AP version");
843
0
      goto fail;
844
0
    }
845
846
0
    if (sta->hs20_ie && wpabuf_len(sta->hs20_ie) > 0) {
847
0
      const u8 *pos;
848
0
      u8 buf[3];
849
0
      u16 id;
850
851
0
      pos = wpabuf_head_u8(sta->hs20_ie);
852
0
      buf[0] = (*pos) >> 4;
853
0
      if (((*pos) & HS20_PPS_MO_ID_PRESENT) &&
854
0
          wpabuf_len(sta->hs20_ie) >= 3)
855
0
        id = WPA_GET_LE16(pos + 1);
856
0
      else
857
0
        id = 0;
858
0
      WPA_PUT_BE16(buf + 1, id);
859
0
      if (!radius_msg_add_wfa(
860
0
            msg,
861
0
            RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION,
862
0
            buf, sizeof(buf))) {
863
0
        wpa_printf(MSG_ERROR,
864
0
             "Could not add HS 2.0 STA version");
865
0
        goto fail;
866
0
      }
867
0
    }
868
869
0
    if (sta->roaming_consortium &&
870
0
        !radius_msg_add_wfa(
871
0
          msg, RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM,
872
0
          wpabuf_head(sta->roaming_consortium),
873
0
          wpabuf_len(sta->roaming_consortium))) {
874
0
      wpa_printf(MSG_ERROR,
875
0
           "Could not add HS 2.0 Roaming Consortium");
876
0
      goto fail;
877
0
    }
878
879
0
    if (hapd->conf->t_c_filename) {
880
0
      be32 timestamp;
881
882
0
      if (!radius_msg_add_wfa(
883
0
            msg,
884
0
            RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME,
885
0
            (const u8 *) hapd->conf->t_c_filename,
886
0
            os_strlen(hapd->conf->t_c_filename))) {
887
0
        wpa_printf(MSG_ERROR,
888
0
             "Could not add HS 2.0 T&C Filename");
889
0
        goto fail;
890
0
      }
891
892
0
      timestamp = host_to_be32(hapd->conf->t_c_timestamp);
893
0
      if (!radius_msg_add_wfa(
894
0
            msg,
895
0
            RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP,
896
0
            (const u8 *) &timestamp,
897
0
            sizeof(timestamp))) {
898
0
        wpa_printf(MSG_ERROR,
899
0
             "Could not add HS 2.0 Timestamp");
900
0
        goto fail;
901
0
      }
902
0
    }
903
0
  }
904
0
#endif /* CONFIG_HS20 */
905
906
0
  if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, sta->addr) < 0)
907
0
    goto fail;
908
909
0
  return;
910
911
0
 fail:
912
0
  radius_msg_free(msg);
913
0
}
914
#endif /* CONFIG_NO_RADIUS */
915
916
917
static void handle_eap_response(struct hostapd_data *hapd,
918
        struct sta_info *sta, struct eap_hdr *eap,
919
        size_t len)
920
0
{
921
0
  u8 type, *data;
922
0
  struct eapol_state_machine *sm = sta->eapol_sm;
923
924
0
  if (!sm)
925
0
    return;
926
927
0
  data = (u8 *) (eap + 1);
928
929
0
  if (len < sizeof(*eap) + 1) {
930
0
    wpa_printf(MSG_INFO, "%s: too short response data", __func__);
931
0
    return;
932
0
  }
933
934
0
  sm->eap_type_supp = type = data[0];
935
936
0
  hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
937
0
           HOSTAPD_LEVEL_DEBUG, "received EAP packet (code=%d "
938
0
           "id=%d len=%d) from STA: EAP Response-%s (%d)",
939
0
           eap->code, eap->identifier, be_to_host16(eap->length),
940
0
           eap_server_get_name(0, type), type);
941
942
0
  sm->dot1xAuthEapolRespFramesRx++;
943
944
0
  wpabuf_free(sm->eap_if->eapRespData);
945
0
  sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
946
0
  sm->eapolEap = true;
947
0
}
948
949
950
static void handle_eap_initiate(struct hostapd_data *hapd,
951
        struct sta_info *sta, struct eap_hdr *eap,
952
        size_t len)
953
0
{
954
#ifdef CONFIG_ERP
955
  u8 type, *data;
956
  struct eapol_state_machine *sm = sta->eapol_sm;
957
958
  if (!sm)
959
    return;
960
961
  if (len < sizeof(*eap) + 1) {
962
    wpa_printf(MSG_INFO, "%s: too short response data", __func__);
963
    return;
964
  }
965
966
  data = (u8 *) (eap + 1);
967
  type = data[0];
968
969
  hostapd_logger(hapd, sm->addr, HOSTAPD_MODULE_IEEE8021X,
970
           HOSTAPD_LEVEL_DEBUG,
971
           "received EAP packet (code=%d id=%d len=%d) from STA: EAP Initiate type %u",
972
           eap->code, eap->identifier, be_to_host16(eap->length),
973
           type);
974
975
  wpabuf_free(sm->eap_if->eapRespData);
976
  sm->eap_if->eapRespData = wpabuf_alloc_copy(eap, len);
977
  sm->eapolEap = true;
978
#endif /* CONFIG_ERP */
979
0
}
980
981
982
#ifndef CONFIG_NO_STDOUT_DEBUG
983
static const char * eap_code_str(u8 code)
984
0
{
985
0
  switch (code) {
986
0
  case EAP_CODE_REQUEST:
987
0
    return "request";
988
0
  case EAP_CODE_RESPONSE:
989
0
    return "response";
990
0
  case EAP_CODE_SUCCESS:
991
0
    return "success";
992
0
  case EAP_CODE_FAILURE:
993
0
    return "failure";
994
0
  case EAP_CODE_INITIATE:
995
0
    return "initiate";
996
0
  case EAP_CODE_FINISH:
997
0
    return "finish";
998
0
  default:
999
0
    return "unknown";
1000
0
  }
1001
0
}
1002
#endif /* CONFIG_NO_STDOUT_DEBUG */
1003
1004
1005
/* Process incoming EAP packet from Supplicant */
1006
static void handle_eap(struct hostapd_data *hapd, struct sta_info *sta,
1007
           u8 *buf, size_t len)
1008
0
{
1009
0
  struct eap_hdr *eap;
1010
0
  u16 eap_len;
1011
1012
0
  if (len < sizeof(*eap)) {
1013
0
    wpa_printf(MSG_INFO, "   too short EAP packet");
1014
0
    return;
1015
0
  }
1016
1017
0
  eap = (struct eap_hdr *) buf;
1018
1019
0
  eap_len = be_to_host16(eap->length);
1020
0
  wpa_printf(MSG_DEBUG, "EAP: code=%d (%s) identifier=%d length=%d",
1021
0
       eap->code, eap_code_str(eap->code), eap->identifier,
1022
0
       eap_len);
1023
0
  if (eap_len < sizeof(*eap)) {
1024
0
    wpa_printf(MSG_DEBUG, "   Invalid EAP length");
1025
0
    return;
1026
0
  } else if (eap_len > len) {
1027
0
    wpa_printf(MSG_DEBUG,
1028
0
         "   Too short frame to contain this EAP packet");
1029
0
    return;
1030
0
  } else if (eap_len < len) {
1031
0
    wpa_printf(MSG_DEBUG,
1032
0
         "   Ignoring %lu extra bytes after EAP packet",
1033
0
         (unsigned long) len - eap_len);
1034
0
  }
1035
1036
0
  switch (eap->code) {
1037
0
  case EAP_CODE_RESPONSE:
1038
0
    handle_eap_response(hapd, sta, eap, eap_len);
1039
0
    break;
1040
0
  case EAP_CODE_INITIATE:
1041
0
    handle_eap_initiate(hapd, sta, eap, eap_len);
1042
0
    break;
1043
0
  }
1044
0
}
1045
1046
1047
struct eapol_state_machine *
1048
ieee802_1x_alloc_eapol_sm(struct hostapd_data *hapd, struct sta_info *sta)
1049
0
{
1050
0
  int flags = 0;
1051
1052
0
  if (sta->flags & WLAN_STA_PREAUTH)
1053
0
    flags |= EAPOL_SM_PREAUTH;
1054
0
  if (sta->wpa_sm) {
1055
0
    flags |= EAPOL_SM_USES_WPA;
1056
0
    if (wpa_auth_sta_get_pmksa(sta->wpa_sm))
1057
0
      flags |= EAPOL_SM_FROM_PMKSA_CACHE;
1058
0
  }
1059
0
  return eapol_auth_alloc(hapd->eapol_auth, sta->addr, flags,
1060
0
        sta->wps_ie, sta->p2p_ie, sta,
1061
0
        sta->identity, sta->radius_cui);
1062
0
}
1063
1064
1065
static void ieee802_1x_save_eapol(struct sta_info *sta, const u8 *buf,
1066
          size_t len, enum frame_encryption encrypted)
1067
0
{
1068
0
  if (sta->pending_eapol_rx) {
1069
0
    wpabuf_free(sta->pending_eapol_rx->buf);
1070
0
  } else {
1071
0
    sta->pending_eapol_rx =
1072
0
      os_malloc(sizeof(*sta->pending_eapol_rx));
1073
0
    if (!sta->pending_eapol_rx)
1074
0
      return;
1075
0
  }
1076
1077
0
  sta->pending_eapol_rx->buf = wpabuf_alloc_copy(buf, len);
1078
0
  if (!sta->pending_eapol_rx->buf) {
1079
0
    os_free(sta->pending_eapol_rx);
1080
0
    sta->pending_eapol_rx = NULL;
1081
0
    return;
1082
0
  }
1083
1084
0
  sta->pending_eapol_rx->encrypted = encrypted;
1085
0
  os_get_reltime(&sta->pending_eapol_rx->rx_time);
1086
0
}
1087
1088
1089
static bool ieee802_1x_check_encryption(struct sta_info *sta,
1090
          enum frame_encryption encrypted,
1091
          u8 type)
1092
0
{
1093
0
  if (encrypted != FRAME_NOT_ENCRYPTED)
1094
0
    return true;
1095
0
  if (type != IEEE802_1X_TYPE_EAP_PACKET &&
1096
0
      type != IEEE802_1X_TYPE_EAPOL_START &&
1097
0
      type != IEEE802_1X_TYPE_EAPOL_LOGOFF)
1098
0
    return true;
1099
0
  if (!(sta->flags & WLAN_STA_MFP))
1100
0
    return true;
1101
0
  return !wpa_auth_pairwise_set(sta->wpa_sm);
1102
0
}
1103
1104
1105
/**
1106
 * ieee802_1x_receive - Process the EAPOL frames from the Supplicant
1107
 * @hapd: hostapd BSS data
1108
 * @sa: Source address (sender of the EAPOL frame)
1109
 * @buf: EAPOL frame
1110
 * @len: Length of buf in octets
1111
 * @encrypted: Whether the frame was encrypted
1112
 *
1113
 * This function is called for each incoming EAPOL frame from the interface
1114
 */
1115
void ieee802_1x_receive(struct hostapd_data *hapd, const u8 *sa, const u8 *buf,
1116
      size_t len, enum frame_encryption encrypted)
1117
0
{
1118
0
  struct sta_info *sta;
1119
0
  struct ieee802_1x_hdr *hdr;
1120
0
  struct ieee802_1x_eapol_key *key;
1121
0
  u16 datalen;
1122
0
  struct rsn_pmksa_cache_entry *pmksa;
1123
0
  int key_mgmt;
1124
1125
0
  if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
1126
0
      !hapd->conf->wps_state)
1127
0
    return;
1128
1129
0
  wpa_printf(MSG_DEBUG, "IEEE 802.1X: %lu bytes from " MACSTR
1130
0
       " (encrypted=%d)",
1131
0
       (unsigned long) len, MAC2STR(sa), encrypted);
1132
0
  sta = ap_get_sta(hapd, sa);
1133
0
  if (!sta || (!(sta->flags & (WLAN_STA_ASSOC | WLAN_STA_PREAUTH)) &&
1134
0
         !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED))) {
1135
0
    wpa_printf(MSG_DEBUG,
1136
0
         "IEEE 802.1X data frame from not associated/Pre-authenticating STA");
1137
1138
0
    if (sta && (sta->flags & WLAN_STA_AUTH)) {
1139
0
      wpa_printf(MSG_DEBUG, "Saving EAPOL frame from " MACSTR
1140
0
           " for later use", MAC2STR(sta->addr));
1141
0
      ieee802_1x_save_eapol(sta, buf, len, encrypted);
1142
0
    }
1143
1144
0
    return;
1145
0
  }
1146
1147
0
  if (len < sizeof(*hdr)) {
1148
0
    wpa_printf(MSG_INFO, "   too short IEEE 802.1X packet");
1149
0
    return;
1150
0
  }
1151
1152
0
  hdr = (struct ieee802_1x_hdr *) buf;
1153
0
  datalen = be_to_host16(hdr->length);
1154
0
  wpa_printf(MSG_DEBUG, "   IEEE 802.1X: version=%d type=%d length=%d",
1155
0
       hdr->version, hdr->type, datalen);
1156
1157
0
  if (len - sizeof(*hdr) < datalen) {
1158
0
    wpa_printf(MSG_INFO,
1159
0
         "   frame too short for this IEEE 802.1X packet");
1160
0
    if (sta->eapol_sm)
1161
0
      sta->eapol_sm->dot1xAuthEapLengthErrorFramesRx++;
1162
0
    return;
1163
0
  }
1164
0
  if (len - sizeof(*hdr) > datalen) {
1165
0
    wpa_printf(MSG_DEBUG,
1166
0
         "   ignoring %lu extra octets after IEEE 802.1X packet",
1167
0
         (unsigned long) len - sizeof(*hdr) - datalen);
1168
0
  }
1169
1170
0
  if (sta->eapol_sm) {
1171
0
    sta->eapol_sm->dot1xAuthLastEapolFrameVersion = hdr->version;
1172
0
    sta->eapol_sm->dot1xAuthEapolFramesRx++;
1173
0
  }
1174
1175
0
  key = (struct ieee802_1x_eapol_key *) (hdr + 1);
1176
0
  if (datalen >= sizeof(struct ieee802_1x_eapol_key) &&
1177
0
      hdr->type == IEEE802_1X_TYPE_EAPOL_KEY &&
1178
0
      (key->type == EAPOL_KEY_TYPE_WPA ||
1179
0
       key->type == EAPOL_KEY_TYPE_RSN)) {
1180
0
    wpa_receive(hapd->wpa_auth, sta->wpa_sm, (u8 *) hdr,
1181
0
          sizeof(*hdr) + datalen);
1182
0
    return;
1183
0
  }
1184
1185
0
  if (!hapd->conf->ieee802_1x &&
1186
0
      !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS))) {
1187
0
    wpa_printf(MSG_DEBUG,
1188
0
         "IEEE 802.1X: Ignore EAPOL message - 802.1X not enabled and WPS not used");
1189
0
    return;
1190
0
  }
1191
1192
0
  key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1193
0
  if (key_mgmt != -1 &&
1194
0
      (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1195
0
       key_mgmt == WPA_KEY_MGMT_DPP)) {
1196
0
    wpa_printf(MSG_DEBUG,
1197
0
         "IEEE 802.1X: Ignore EAPOL message - STA is using PSK");
1198
0
    return;
1199
0
  }
1200
1201
0
  if (!ieee802_1x_check_encryption(sta, encrypted, hdr->type)) {
1202
0
    wpa_printf(MSG_DEBUG,
1203
0
         "IEEE 802.1X: Discard unencrypted EAPOL message - encryption was expected");
1204
0
    return;
1205
0
  }
1206
1207
0
  if (!sta->eapol_sm) {
1208
0
    sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1209
0
    if (!sta->eapol_sm)
1210
0
      return;
1211
1212
0
#ifdef CONFIG_WPS
1213
0
    if (!hapd->conf->ieee802_1x && hapd->conf->wps_state) {
1214
0
      u32 wflags = sta->flags & (WLAN_STA_WPS |
1215
0
               WLAN_STA_WPS2 |
1216
0
               WLAN_STA_MAYBE_WPS);
1217
0
      if (wflags == WLAN_STA_MAYBE_WPS ||
1218
0
          wflags == (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) {
1219
        /*
1220
         * Delay EAPOL frame transmission until a
1221
         * possible WPS STA initiates the handshake
1222
         * with EAPOL-Start. Only allow the wait to be
1223
         * skipped if the STA is known to support WPS
1224
         * 2.0.
1225
         */
1226
0
        wpa_printf(MSG_DEBUG,
1227
0
             "WPS: Do not start EAPOL until EAPOL-Start is received");
1228
0
        sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1229
0
      }
1230
0
    }
1231
0
#endif /* CONFIG_WPS */
1232
1233
0
    sta->eapol_sm->eap_if->portEnabled = true;
1234
0
  }
1235
1236
  /* since we support version 1, we can ignore version field and proceed
1237
   * as specified in version 1 standard [IEEE Std 802.1X-2001, 7.5.5] */
1238
  /* TODO: actually, we are not version 1 anymore.. However, Version 2
1239
   * does not change frame contents, so should be ok to process frames
1240
   * more or less identically. Some changes might be needed for
1241
   * verification of fields. */
1242
1243
0
  switch (hdr->type) {
1244
0
  case IEEE802_1X_TYPE_EAP_PACKET:
1245
0
    handle_eap(hapd, sta, (u8 *) (hdr + 1), datalen);
1246
0
    break;
1247
1248
0
  case IEEE802_1X_TYPE_EAPOL_START:
1249
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1250
0
             HOSTAPD_LEVEL_DEBUG,
1251
0
             "received EAPOL-Start from STA");
1252
0
#ifdef CONFIG_IEEE80211R_AP
1253
0
    if (hapd->conf->wpa &&
1254
0
        wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) && sta->wpa_sm &&
1255
0
        ((wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm)) &&
1256
0
          (sta->flags & WLAN_STA_AUTHORIZED)) ||
1257
0
         sta->auth_alg == WLAN_AUTH_FT)) {
1258
      /* When FT is used, reauthentication to generate a new
1259
       * PMK-R0 would be complicated since the current AP
1260
       * might not be the one with which the currently used
1261
       * PMK-R0 was generated. IEEE Std 802.11-2020, 13.4.2
1262
       * (FT initial mobility domain association in an RSN)
1263
       * mandates STA to perform a new FT initial mobility
1264
       * domain association whenever its Supplicant would
1265
       * trigger sending of an EAPOL-Start frame. As such,
1266
       * this EAPOL-Start frame should not have been sent.
1267
       * Discard it to avoid unexpected behavior. */
1268
0
      hostapd_logger(hapd, sta->addr,
1269
0
               HOSTAPD_MODULE_IEEE8021X,
1270
0
               HOSTAPD_LEVEL_DEBUG,
1271
0
               "discard unexpected EAPOL-Start from STA that uses FT");
1272
0
      break;
1273
0
    }
1274
0
#endif /* CONFIG_IEEE80211R_AP */
1275
0
    sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1276
0
    pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1277
0
    if (pmksa) {
1278
0
      hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1279
0
               HOSTAPD_LEVEL_DEBUG,
1280
0
               "cached PMKSA available - ignore it since STA sent EAPOL-Start");
1281
0
      wpa_auth_sta_clear_pmksa(sta->wpa_sm, pmksa);
1282
0
    }
1283
0
    sta->eapol_sm->eapolStart = true;
1284
0
    sta->eapol_sm->dot1xAuthEapolStartFramesRx++;
1285
0
    eap_server_clear_identity(sta->eapol_sm->eap);
1286
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
1287
0
    break;
1288
1289
0
  case IEEE802_1X_TYPE_EAPOL_LOGOFF:
1290
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1291
0
             HOSTAPD_LEVEL_DEBUG,
1292
0
             "received EAPOL-Logoff from STA");
1293
0
    sta->acct_terminate_cause =
1294
0
      RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1295
0
    accounting_sta_stop(hapd, sta);
1296
0
    sta->eapol_sm->eapolLogoff = true;
1297
0
    sta->eapol_sm->dot1xAuthEapolLogoffFramesRx++;
1298
0
    eap_server_clear_identity(sta->eapol_sm->eap);
1299
0
    break;
1300
1301
0
  case IEEE802_1X_TYPE_EAPOL_KEY:
1302
0
    wpa_printf(MSG_DEBUG, "   EAPOL-Key");
1303
0
    if (!ap_sta_is_authorized(sta)) {
1304
0
      wpa_printf(MSG_DEBUG,
1305
0
           "   Dropped key data from unauthorized Supplicant");
1306
0
      break;
1307
0
    }
1308
0
    break;
1309
1310
0
  case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
1311
0
    wpa_printf(MSG_DEBUG, "   EAPOL-Encapsulated-ASF-Alert");
1312
    /* TODO: implement support for this; show data */
1313
0
    break;
1314
1315
#ifdef CONFIG_MACSEC
1316
  case IEEE802_1X_TYPE_EAPOL_MKA:
1317
    wpa_printf(MSG_EXCESSIVE,
1318
         "EAPOL type %d will be handled by MKA", hdr->type);
1319
    break;
1320
#endif /* CONFIG_MACSEC */
1321
1322
0
  default:
1323
0
    wpa_printf(MSG_DEBUG, "   unknown IEEE 802.1X packet type");
1324
0
    sta->eapol_sm->dot1xAuthInvalidEapolFramesRx++;
1325
0
    break;
1326
0
  }
1327
1328
0
  eapol_auth_step(sta->eapol_sm);
1329
0
}
1330
1331
1332
/**
1333
 * ieee802_1x_new_station - Start IEEE 802.1X authentication
1334
 * @hapd: hostapd BSS data
1335
 * @sta: The station
1336
 *
1337
 * This function is called to start IEEE 802.1X authentication when a new
1338
 * station completes IEEE 802.11 association.
1339
 */
1340
void ieee802_1x_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1341
0
{
1342
0
  struct rsn_pmksa_cache_entry *pmksa;
1343
0
  int reassoc = 1;
1344
0
  int force_1x = 0;
1345
0
  int key_mgmt;
1346
1347
0
#ifdef CONFIG_WPS
1348
0
  if (hapd->conf->wps_state &&
1349
0
      ((hapd->conf->wpa && (sta->flags & WLAN_STA_MAYBE_WPS)) ||
1350
0
       (sta->flags & WLAN_STA_WPS))) {
1351
    /*
1352
     * Need to enable IEEE 802.1X/EAPOL state machines for possible
1353
     * WPS handshake even if IEEE 802.1X/EAPOL is not used for
1354
     * authentication in this BSS.
1355
     */
1356
0
    force_1x = 1;
1357
0
  }
1358
0
#endif /* CONFIG_WPS */
1359
1360
0
  if (!force_1x && !hapd->conf->ieee802_1x) {
1361
0
    wpa_printf(MSG_DEBUG,
1362
0
         "IEEE 802.1X: Ignore STA - 802.1X not enabled or forced for WPS");
1363
    /*
1364
     * Clear any possible EAPOL authenticator state to support
1365
     * reassociation change from WPS to PSK.
1366
     */
1367
0
    ieee802_1x_free_station(hapd, sta);
1368
0
    return;
1369
0
  }
1370
1371
0
  key_mgmt = wpa_auth_sta_key_mgmt(sta->wpa_sm);
1372
0
  if (key_mgmt != -1 &&
1373
0
      (wpa_key_mgmt_wpa_psk(key_mgmt) || key_mgmt == WPA_KEY_MGMT_OWE ||
1374
0
       key_mgmt == WPA_KEY_MGMT_DPP)) {
1375
0
    wpa_printf(MSG_DEBUG, "IEEE 802.1X: Ignore STA - using PSK");
1376
    /*
1377
     * Clear any possible EAPOL authenticator state to support
1378
     * reassociation change from WPA-EAP to PSK.
1379
     */
1380
0
    ieee802_1x_free_station(hapd, sta);
1381
0
    return;
1382
0
  }
1383
1384
0
  if (!sta->eapol_sm) {
1385
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1386
0
             HOSTAPD_LEVEL_DEBUG, "start authentication");
1387
0
    sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
1388
0
    if (!sta->eapol_sm) {
1389
0
      hostapd_logger(hapd, sta->addr,
1390
0
               HOSTAPD_MODULE_IEEE8021X,
1391
0
               HOSTAPD_LEVEL_INFO,
1392
0
               "failed to allocate state machine");
1393
0
      return;
1394
0
    }
1395
0
    reassoc = 0;
1396
0
  }
1397
1398
0
#ifdef CONFIG_WPS
1399
0
  sta->eapol_sm->flags &= ~EAPOL_SM_WAIT_START;
1400
0
  if (!hapd->conf->ieee802_1x && hapd->conf->wps_state &&
1401
0
      !(sta->flags & WLAN_STA_WPS2)) {
1402
    /*
1403
     * Delay EAPOL frame transmission until a possible WPS STA
1404
     * initiates the handshake with EAPOL-Start. Only allow the
1405
     * wait to be skipped if the STA is known to support WPS 2.0.
1406
     */
1407
0
    wpa_printf(MSG_DEBUG,
1408
0
         "WPS: Do not start EAPOL until EAPOL-Start is received");
1409
0
    sta->eapol_sm->flags |= EAPOL_SM_WAIT_START;
1410
0
  }
1411
0
#endif /* CONFIG_WPS */
1412
1413
0
  sta->eapol_sm->eap_if->portEnabled = true;
1414
1415
0
#ifdef CONFIG_IEEE80211R_AP
1416
0
  if (sta->auth_alg == WLAN_AUTH_FT) {
1417
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1418
0
             HOSTAPD_LEVEL_DEBUG,
1419
0
             "PMK from FT - skip IEEE 802.1X/EAP");
1420
    /* Setup EAPOL state machines to already authenticated state
1421
     * because of existing FT information from R0KH. */
1422
0
    sta->eapol_sm->keyRun = true;
1423
0
    sta->eapol_sm->eap_if->eapKeyAvailable = true;
1424
0
    sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1425
0
    sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1426
0
    sta->eapol_sm->authSuccess = true;
1427
0
    sta->eapol_sm->authFail = false;
1428
0
    sta->eapol_sm->portValid = true;
1429
0
    if (sta->eapol_sm->eap)
1430
0
      eap_sm_notify_cached(sta->eapol_sm->eap);
1431
0
    ap_sta_bind_vlan(hapd, sta);
1432
0
    return;
1433
0
  }
1434
0
#endif /* CONFIG_IEEE80211R_AP */
1435
1436
#ifdef CONFIG_FILS
1437
  if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
1438
      sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
1439
      sta->auth_alg == WLAN_AUTH_FILS_PK) {
1440
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1441
             HOSTAPD_LEVEL_DEBUG,
1442
             "PMK from FILS - skip IEEE 802.1X/EAP");
1443
    /* Setup EAPOL state machines to already authenticated state
1444
     * because of existing FILS information. */
1445
    sta->eapol_sm->keyRun = true;
1446
    sta->eapol_sm->eap_if->eapKeyAvailable = true;
1447
    sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1448
    sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1449
    sta->eapol_sm->authSuccess = true;
1450
    sta->eapol_sm->authFail = false;
1451
    sta->eapol_sm->portValid = true;
1452
    if (sta->eapol_sm->eap)
1453
      eap_sm_notify_cached(sta->eapol_sm->eap);
1454
    wpa_auth_set_ptk_rekey_timer(sta->wpa_sm);
1455
    return;
1456
  }
1457
#endif /* CONFIG_FILS */
1458
1459
0
  pmksa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1460
0
  if (pmksa) {
1461
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1462
0
             HOSTAPD_LEVEL_DEBUG,
1463
0
             "PMK from PMKSA cache - skip IEEE 802.1X/EAP");
1464
    /* Setup EAPOL state machines to already authenticated state
1465
     * because of existing PMKSA information in the cache. */
1466
0
    sta->eapol_sm->keyRun = true;
1467
0
    sta->eapol_sm->eap_if->eapKeyAvailable = true;
1468
0
    sta->eapol_sm->auth_pae_state = AUTH_PAE_AUTHENTICATING;
1469
0
    sta->eapol_sm->be_auth_state = BE_AUTH_SUCCESS;
1470
0
    sta->eapol_sm->authSuccess = true;
1471
0
    sta->eapol_sm->authFail = false;
1472
0
    if (sta->eapol_sm->eap)
1473
0
      eap_sm_notify_cached(sta->eapol_sm->eap);
1474
0
    pmksa_cache_to_eapol_data(hapd, pmksa, sta->eapol_sm);
1475
0
    ap_sta_bind_vlan(hapd, sta);
1476
0
  } else {
1477
0
    if (reassoc) {
1478
      /*
1479
       * Force EAPOL state machines to start
1480
       * re-authentication without having to wait for the
1481
       * Supplicant to send EAPOL-Start.
1482
       */
1483
0
      sta->eapol_sm->reAuthenticate = true;
1484
0
    }
1485
0
    eapol_auth_step(sta->eapol_sm);
1486
0
  }
1487
0
}
1488
1489
1490
void ieee802_1x_free_station(struct hostapd_data *hapd, struct sta_info *sta)
1491
2.19k
{
1492
2.19k
  struct eapol_state_machine *sm = sta->eapol_sm;
1493
1494
2.19k
  if (sta->pending_eapol_rx) {
1495
0
    wpabuf_free(sta->pending_eapol_rx->buf);
1496
0
    os_free(sta->pending_eapol_rx);
1497
0
    sta->pending_eapol_rx = NULL;
1498
0
  }
1499
1500
2.19k
  if (!sm)
1501
2.19k
    return;
1502
1503
0
  sta->eapol_sm = NULL;
1504
1505
0
#ifndef CONFIG_NO_RADIUS
1506
0
  radius_msg_free(sm->last_recv_radius);
1507
0
  radius_free_class(&sm->radius_class);
1508
0
#endif /* CONFIG_NO_RADIUS */
1509
1510
0
  eapol_auth_free(sm);
1511
0
}
1512
1513
1514
#ifndef CONFIG_NO_RADIUS
1515
static void ieee802_1x_decapsulate_radius(struct hostapd_data *hapd,
1516
            struct sta_info *sta)
1517
0
{
1518
0
  struct wpabuf *eap;
1519
0
  const struct eap_hdr *hdr;
1520
0
  int eap_type = -1;
1521
0
  char buf[64];
1522
0
  struct radius_msg *msg;
1523
0
  struct eapol_state_machine *sm = sta->eapol_sm;
1524
1525
0
  if (!sm || !sm->last_recv_radius) {
1526
0
    if (sm)
1527
0
      sm->eap_if->aaaEapNoReq = true;
1528
0
    return;
1529
0
  }
1530
1531
0
  msg = sm->last_recv_radius;
1532
1533
0
  eap = radius_msg_get_eap(msg);
1534
0
  if (!eap) {
1535
    /* RFC 3579, Chap. 2.6.3:
1536
     * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
1537
     * attribute */
1538
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1539
0
             HOSTAPD_LEVEL_WARNING,
1540
0
             "could not extract EAP-Message from RADIUS message");
1541
0
    sm->eap_if->aaaEapNoReq = true;
1542
0
    return;
1543
0
  }
1544
1545
0
  if (wpabuf_len(eap) < sizeof(*hdr)) {
1546
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1547
0
             HOSTAPD_LEVEL_WARNING,
1548
0
             "too short EAP packet received from authentication server");
1549
0
    wpabuf_free(eap);
1550
0
    sm->eap_if->aaaEapNoReq = true;
1551
0
    return;
1552
0
  }
1553
1554
0
  if (wpabuf_len(eap) > sizeof(*hdr))
1555
0
    eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
1556
1557
0
  hdr = wpabuf_head(eap);
1558
0
  switch (hdr->code) {
1559
0
  case EAP_CODE_REQUEST:
1560
0
    if (eap_type >= 0)
1561
0
      sm->eap_type_authsrv = eap_type;
1562
0
    os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
1563
0
          eap_server_get_name(0, eap_type), eap_type);
1564
0
    break;
1565
0
  case EAP_CODE_RESPONSE:
1566
0
    os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
1567
0
          eap_server_get_name(0, eap_type), eap_type);
1568
0
    break;
1569
0
  case EAP_CODE_SUCCESS:
1570
0
    os_strlcpy(buf, "EAP Success", sizeof(buf));
1571
0
    break;
1572
0
  case EAP_CODE_FAILURE:
1573
0
    os_strlcpy(buf, "EAP Failure", sizeof(buf));
1574
0
    break;
1575
0
  default:
1576
0
    os_strlcpy(buf, "unknown EAP code", sizeof(buf));
1577
0
    break;
1578
0
  }
1579
0
  buf[sizeof(buf) - 1] = '\0';
1580
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1581
0
           HOSTAPD_LEVEL_DEBUG,
1582
0
           "decapsulated EAP packet (code=%d id=%d len=%d) from RADIUS server: %s",
1583
0
           hdr->code, hdr->identifier, be_to_host16(hdr->length),
1584
0
           buf);
1585
0
  sm->eap_if->aaaEapReq = true;
1586
1587
0
  wpabuf_free(sm->eap_if->aaaEapReqData);
1588
0
  sm->eap_if->aaaEapReqData = eap;
1589
0
}
1590
1591
1592
static void ieee802_1x_get_keys(struct hostapd_data *hapd,
1593
        struct sta_info *sta, struct radius_msg *msg,
1594
        struct radius_msg *req,
1595
        const u8 *shared_secret,
1596
        size_t shared_secret_len)
1597
0
{
1598
0
  struct radius_ms_mppe_keys *keys;
1599
0
  u8 *buf;
1600
0
  size_t len;
1601
0
  struct eapol_state_machine *sm = sta->eapol_sm;
1602
1603
0
  if (!sm)
1604
0
    return;
1605
1606
0
  keys = radius_msg_get_ms_keys(msg, req, shared_secret,
1607
0
              shared_secret_len);
1608
1609
0
  if (keys && keys->send && keys->recv) {
1610
0
    len = keys->send_len + keys->recv_len;
1611
0
    wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
1612
0
        keys->send, keys->send_len);
1613
0
    wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
1614
0
        keys->recv, keys->recv_len);
1615
1616
0
    os_free(sm->eap_if->aaaEapKeyData);
1617
0
    sm->eap_if->aaaEapKeyData = os_malloc(len);
1618
0
    if (sm->eap_if->aaaEapKeyData) {
1619
0
      os_memcpy(sm->eap_if->aaaEapKeyData, keys->recv,
1620
0
          keys->recv_len);
1621
0
      os_memcpy(sm->eap_if->aaaEapKeyData + keys->recv_len,
1622
0
          keys->send, keys->send_len);
1623
0
      sm->eap_if->aaaEapKeyDataLen = len;
1624
0
      sm->eap_if->aaaEapKeyAvailable = true;
1625
0
    }
1626
0
  } else {
1627
0
    wpa_printf(MSG_DEBUG,
1628
0
         "MS-MPPE: 1x_get_keys, could not get keys: %p  send: %p  recv: %p",
1629
0
         keys, keys ? keys->send : NULL,
1630
0
         keys ? keys->recv : NULL);
1631
0
  }
1632
1633
0
  if (keys) {
1634
0
    os_free(keys->send);
1635
0
    os_free(keys->recv);
1636
0
    os_free(keys);
1637
0
  }
1638
1639
0
  if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
1640
0
            NULL) == 0) {
1641
0
    os_free(sm->eap_if->eapSessionId);
1642
0
    sm->eap_if->eapSessionId = os_memdup(buf, len);
1643
0
    if (sm->eap_if->eapSessionId) {
1644
0
      sm->eap_if->eapSessionIdLen = len;
1645
0
      wpa_hexdump(MSG_DEBUG, "EAP-Key Name",
1646
0
            sm->eap_if->eapSessionId,
1647
0
            sm->eap_if->eapSessionIdLen);
1648
0
    }
1649
0
  } else {
1650
0
    sm->eap_if->eapSessionIdLen = 0;
1651
0
  }
1652
0
}
1653
1654
1655
static void ieee802_1x_store_radius_class(struct hostapd_data *hapd,
1656
            struct sta_info *sta,
1657
            struct radius_msg *msg)
1658
0
{
1659
0
  u8 *attr_class;
1660
0
  size_t class_len;
1661
0
  struct eapol_state_machine *sm = sta->eapol_sm;
1662
0
  int count, i;
1663
0
  struct radius_attr_data *nclass;
1664
0
  size_t nclass_count;
1665
1666
0
  if (!hapd->conf->radius->acct_server || !hapd->radius || !sm)
1667
0
    return;
1668
1669
0
  radius_free_class(&sm->radius_class);
1670
0
  count = radius_msg_count_attr(msg, RADIUS_ATTR_CLASS, 1);
1671
0
  if (count <= 0)
1672
0
    return;
1673
1674
0
  nclass = os_calloc(count, sizeof(struct radius_attr_data));
1675
0
  if (!nclass)
1676
0
    return;
1677
1678
0
  nclass_count = 0;
1679
1680
0
  attr_class = NULL;
1681
0
  for (i = 0; i < count; i++) {
1682
0
    do {
1683
0
      if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CLASS,
1684
0
                &attr_class, &class_len,
1685
0
                attr_class) < 0) {
1686
0
        i = count;
1687
0
        break;
1688
0
      }
1689
0
    } while (class_len < 1);
1690
1691
0
    nclass[nclass_count].data = os_memdup(attr_class, class_len);
1692
0
    if (!nclass[nclass_count].data)
1693
0
      break;
1694
1695
0
    nclass[nclass_count].len = class_len;
1696
0
    nclass_count++;
1697
0
  }
1698
1699
0
  sm->radius_class.attr = nclass;
1700
0
  sm->radius_class.count = nclass_count;
1701
0
  wpa_printf(MSG_DEBUG,
1702
0
       "IEEE 802.1X: Stored %lu RADIUS Class attributes for "
1703
0
       MACSTR,
1704
0
       (unsigned long) sm->radius_class.count,
1705
0
       MAC2STR(sta->addr));
1706
0
}
1707
1708
1709
/* Update sta->identity based on User-Name attribute in Access-Accept */
1710
static void ieee802_1x_update_sta_identity(struct hostapd_data *hapd,
1711
             struct sta_info *sta,
1712
             struct radius_msg *msg)
1713
0
{
1714
0
  u8 *buf, *identity;
1715
0
  size_t len;
1716
0
  struct eapol_state_machine *sm = sta->eapol_sm;
1717
1718
0
  if (!sm)
1719
0
    return;
1720
1721
0
  if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &buf, &len,
1722
0
            NULL) < 0)
1723
0
    return;
1724
1725
0
  identity = (u8 *) dup_binstr(buf, len);
1726
0
  if (!identity)
1727
0
    return;
1728
1729
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1730
0
           HOSTAPD_LEVEL_DEBUG,
1731
0
           "old identity '%s' updated with User-Name from Access-Accept '%s'",
1732
0
           sm->identity ? (char *) sm->identity : "N/A",
1733
0
           (char *) identity);
1734
1735
0
  os_free(sm->identity);
1736
0
  sm->identity = identity;
1737
0
  sm->identity_len = len;
1738
0
}
1739
1740
1741
/* Update CUI based on Chargeable-User-Identity attribute in Access-Accept */
1742
static void ieee802_1x_update_sta_cui(struct hostapd_data *hapd,
1743
              struct sta_info *sta,
1744
              struct radius_msg *msg)
1745
0
{
1746
0
  struct eapol_state_machine *sm = sta->eapol_sm;
1747
0
  struct wpabuf *cui;
1748
0
  u8 *buf;
1749
0
  size_t len;
1750
1751
0
  if (!sm)
1752
0
    return;
1753
1754
0
  if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
1755
0
            &buf, &len, NULL) < 0)
1756
0
    return;
1757
1758
0
  cui = wpabuf_alloc_copy(buf, len);
1759
0
  if (!cui)
1760
0
    return;
1761
1762
0
  wpabuf_free(sm->radius_cui);
1763
0
  sm->radius_cui = cui;
1764
0
}
1765
1766
1767
#ifdef CONFIG_HS20
1768
1769
static void ieee802_1x_hs20_deauth_req(struct hostapd_data *hapd,
1770
               struct sta_info *sta, const u8 *pos,
1771
               size_t len)
1772
0
{
1773
0
  size_t url_len;
1774
0
  unsigned int timeout;
1775
1776
0
  if (len < 3)
1777
0
    return; /* Malformed information */
1778
0
  url_len = len - 3;
1779
0
  sta->hs20_deauth_requested = 1;
1780
0
  sta->hs20_deauth_on_ack = url_len == 0;
1781
0
  wpa_printf(MSG_DEBUG,
1782
0
       "HS 2.0: Deauthentication request - Code %u  Re-auth Delay %u  URL length %zu",
1783
0
       *pos, WPA_GET_LE16(pos + 1), url_len);
1784
0
  wpabuf_free(sta->hs20_deauth_req);
1785
0
  sta->hs20_deauth_req = wpabuf_alloc(len + 1);
1786
0
  if (sta->hs20_deauth_req) {
1787
0
    wpabuf_put_data(sta->hs20_deauth_req, pos, 3);
1788
0
    wpabuf_put_u8(sta->hs20_deauth_req, url_len);
1789
0
    wpabuf_put_data(sta->hs20_deauth_req, pos + 3, url_len);
1790
0
  }
1791
0
  timeout = hapd->conf->hs20_deauth_req_timeout;
1792
  /* If there is no URL, no need to provide time to fetch it. Use a short
1793
   * timeout here to allow maximum time for completing 4-way handshake and
1794
   * WNM-Notification delivery. Acknowledgement of the frame will result
1795
   * in cutting this wait further. */
1796
0
  if (!url_len && timeout > 2)
1797
0
    timeout = 2;
1798
0
  ap_sta_session_timeout(hapd, sta, timeout);
1799
0
}
1800
1801
1802
static void ieee802_1x_hs20_session_info(struct hostapd_data *hapd,
1803
           struct sta_info *sta, u8 *pos,
1804
           size_t len, int session_timeout)
1805
0
{
1806
0
  unsigned int swt;
1807
0
  int warning_time, beacon_int;
1808
1809
0
  if (len < 1)
1810
0
    return; /* Malformed information */
1811
0
  os_free(sta->hs20_session_info_url);
1812
0
  sta->hs20_session_info_url = os_malloc(len);
1813
0
  if (!sta->hs20_session_info_url)
1814
0
    return;
1815
0
  swt = pos[0];
1816
0
  os_memcpy(sta->hs20_session_info_url, pos + 1, len - 1);
1817
0
  sta->hs20_session_info_url[len - 1] = '\0';
1818
0
  wpa_printf(MSG_DEBUG,
1819
0
       "HS 2.0: Session Information URL='%s' SWT=%u (session_timeout=%d)",
1820
0
       sta->hs20_session_info_url, swt, session_timeout);
1821
0
  if (session_timeout < 0) {
1822
0
    wpa_printf(MSG_DEBUG,
1823
0
         "HS 2.0: No Session-Timeout set - ignore session info URL");
1824
0
    return;
1825
0
  }
1826
0
  if (swt == 255)
1827
0
    swt = 1; /* Use one minute as the AP selected value */
1828
1829
0
  if ((unsigned int) session_timeout < swt * 60)
1830
0
    warning_time = 0;
1831
0
  else
1832
0
    warning_time = session_timeout - swt * 60;
1833
1834
0
  beacon_int = hapd->iconf->beacon_int;
1835
0
  if (beacon_int < 1)
1836
0
    beacon_int = 100; /* best guess */
1837
0
  sta->hs20_disassoc_timer = swt * 60 * 1000 / beacon_int * 125 / 128;
1838
0
  if (sta->hs20_disassoc_timer > 65535)
1839
0
    sta->hs20_disassoc_timer = 65535;
1840
1841
0
  ap_sta_session_warning_timeout(hapd, sta, warning_time);
1842
0
}
1843
1844
1845
static void ieee802_1x_hs20_t_c_filtering(struct hostapd_data *hapd,
1846
            struct sta_info *sta, u8 *pos,
1847
            size_t len)
1848
0
{
1849
0
  if (len < 4)
1850
0
    return; /* Malformed information */
1851
0
  wpa_printf(MSG_DEBUG,
1852
0
       "HS 2.0: Terms and Conditions filtering %02x %02x %02x %02x",
1853
0
       pos[0], pos[1], pos[2], pos[3]);
1854
0
  hs20_t_c_filtering(hapd, sta, pos[0] & BIT(0));
1855
0
}
1856
1857
1858
static void ieee802_1x_hs20_t_c_url(struct hostapd_data *hapd,
1859
            struct sta_info *sta, u8 *pos, size_t len)
1860
0
{
1861
0
  os_free(sta->t_c_url);
1862
0
  sta->t_c_url = os_malloc(len + 1);
1863
0
  if (!sta->t_c_url)
1864
0
    return;
1865
0
  os_memcpy(sta->t_c_url, pos, len);
1866
0
  sta->t_c_url[len] = '\0';
1867
0
  wpa_printf(MSG_DEBUG,
1868
0
       "HS 2.0: Terms and Conditions URL %s", sta->t_c_url);
1869
0
}
1870
1871
#endif /* CONFIG_HS20 */
1872
1873
1874
static void ieee802_1x_check_hs20(struct hostapd_data *hapd,
1875
          struct sta_info *sta,
1876
          struct radius_msg *msg,
1877
          int session_timeout)
1878
0
{
1879
0
#ifdef CONFIG_HS20
1880
0
  u8 *buf, *pos, *end, type, sublen;
1881
0
  size_t len;
1882
1883
0
  buf = NULL;
1884
0
  sta->hs20_deauth_requested = 0;
1885
0
  sta->hs20_deauth_on_ack = 0;
1886
1887
0
  for (;;) {
1888
0
    if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_VENDOR_SPECIFIC,
1889
0
              &buf, &len, buf) < 0)
1890
0
      break;
1891
0
    if (len < 6)
1892
0
      continue;
1893
0
    pos = buf;
1894
0
    end = buf + len;
1895
0
    if (WPA_GET_BE32(pos) != RADIUS_VENDOR_ID_WFA)
1896
0
      continue;
1897
0
    pos += 4;
1898
1899
0
    type = *pos++;
1900
0
    sublen = *pos++;
1901
0
    if (sublen < 2)
1902
0
      continue; /* invalid length */
1903
0
    sublen -= 2; /* skip header */
1904
0
    if (pos + sublen > end)
1905
0
      continue; /* invalid WFA VSA */
1906
1907
0
    switch (type) {
1908
0
    case RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ:
1909
0
      ieee802_1x_hs20_deauth_req(hapd, sta, pos, sublen);
1910
0
      break;
1911
0
    case RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL:
1912
0
      ieee802_1x_hs20_session_info(hapd, sta, pos, sublen,
1913
0
                 session_timeout);
1914
0
      break;
1915
0
    case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING:
1916
0
      ieee802_1x_hs20_t_c_filtering(hapd, sta, pos, sublen);
1917
0
      break;
1918
0
    case RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL:
1919
0
      ieee802_1x_hs20_t_c_url(hapd, sta, pos, sublen);
1920
0
      break;
1921
0
    }
1922
0
  }
1923
0
#endif /* CONFIG_HS20 */
1924
0
}
1925
1926
1927
struct sta_id_search {
1928
  u8 identifier;
1929
  struct eapol_state_machine *sm;
1930
};
1931
1932
1933
static int ieee802_1x_select_radius_identifier(struct hostapd_data *hapd,
1934
                 struct sta_info *sta,
1935
                 void *ctx)
1936
0
{
1937
0
  struct sta_id_search *id_search = ctx;
1938
0
  struct eapol_state_machine *sm = sta->eapol_sm;
1939
1940
0
  if (sm && sm->radius_identifier >= 0 &&
1941
0
      sm->radius_identifier == id_search->identifier) {
1942
0
    id_search->sm = sm;
1943
0
    return 1;
1944
0
  }
1945
0
  return 0;
1946
0
}
1947
1948
1949
static struct eapol_state_machine *
1950
ieee802_1x_search_radius_identifier(struct hostapd_data *hapd, u8 identifier)
1951
0
{
1952
0
  struct sta_id_search id_search;
1953
1954
0
  id_search.identifier = identifier;
1955
0
  id_search.sm = NULL;
1956
0
  ap_for_each_sta(hapd, ieee802_1x_select_radius_identifier, &id_search);
1957
0
  return id_search.sm;
1958
0
}
1959
1960
1961
#ifndef CONFIG_NO_VLAN
1962
static int ieee802_1x_update_vlan(struct radius_msg *msg,
1963
          struct hostapd_data *hapd,
1964
          struct sta_info *sta)
1965
0
{
1966
0
  struct vlan_description vlan_desc;
1967
1968
0
  os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1969
0
  vlan_desc.notempty = !!radius_msg_get_vlanid(msg, &vlan_desc.untagged,
1970
0
                 MAX_NUM_TAGGED_VLAN,
1971
0
                 vlan_desc.tagged);
1972
1973
0
  if (vlan_desc.notempty &&
1974
0
      !hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1975
0
    sta->eapol_sm->authFail = true;
1976
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
1977
0
             HOSTAPD_LEVEL_INFO,
1978
0
             "Invalid VLAN %d%s received from RADIUS server",
1979
0
             vlan_desc.untagged,
1980
0
             vlan_desc.tagged[0] ? "+" : "");
1981
0
    os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1982
0
    ap_sta_set_vlan(hapd, sta, &vlan_desc);
1983
0
    return -1;
1984
0
  }
1985
1986
0
  if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
1987
0
      !vlan_desc.notempty) {
1988
0
    sta->eapol_sm->authFail = true;
1989
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
1990
0
             HOSTAPD_LEVEL_INFO,
1991
0
             "authentication server did not include required VLAN ID in Access-Accept");
1992
0
    return -1;
1993
0
  }
1994
1995
0
  return ap_sta_set_vlan(hapd, sta, &vlan_desc);
1996
0
}
1997
#endif /* CONFIG_NO_VLAN */
1998
1999
2000
/**
2001
 * ieee802_1x_receive_auth - Process RADIUS frames from Authentication Server
2002
 * @msg: RADIUS response message
2003
 * @req: RADIUS request message
2004
 * @shared_secret: RADIUS shared secret
2005
 * @shared_secret_len: Length of shared_secret in octets
2006
 * @data: Context data (struct hostapd_data *)
2007
 * Returns: Processing status
2008
 */
2009
static RadiusRxResult
2010
ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
2011
      const u8 *shared_secret, size_t shared_secret_len,
2012
      void *data)
2013
0
{
2014
0
  struct hostapd_data *hapd = data;
2015
0
  struct sta_info *sta;
2016
0
  u32 session_timeout = 0, termination_action, acct_interim_interval;
2017
0
  int session_timeout_set;
2018
0
  u32 reason_code;
2019
0
  struct eapol_state_machine *sm;
2020
0
  int override_eapReq = 0;
2021
0
  struct radius_hdr *hdr = radius_msg_get_hdr(msg);
2022
2023
0
  sm = ieee802_1x_search_radius_identifier(hapd, hdr->identifier);
2024
0
  if (!sm) {
2025
0
    wpa_printf(MSG_DEBUG,
2026
0
         "IEEE 802.1X: Could not find matching station for this RADIUS message");
2027
0
    return RADIUS_RX_UNKNOWN;
2028
0
  }
2029
0
  sta = sm->sta;
2030
2031
0
  if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 1)) {
2032
0
    wpa_printf(MSG_INFO,
2033
0
         "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
2034
0
    return RADIUS_RX_INVALID_AUTHENTICATOR;
2035
0
  }
2036
2037
0
  if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
2038
0
      hdr->code != RADIUS_CODE_ACCESS_REJECT &&
2039
0
      hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
2040
0
    wpa_printf(MSG_INFO, "Unknown RADIUS message code");
2041
0
    return RADIUS_RX_UNKNOWN;
2042
0
  }
2043
2044
0
  sm->radius_identifier = -1;
2045
0
  wpa_printf(MSG_DEBUG, "RADIUS packet matching with station " MACSTR,
2046
0
       MAC2STR(sta->addr));
2047
2048
0
  radius_msg_free(sm->last_recv_radius);
2049
0
  sm->last_recv_radius = msg;
2050
2051
0
  session_timeout_set =
2052
0
    !radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
2053
0
             &session_timeout);
2054
0
  if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_TERMINATION_ACTION,
2055
0
              &termination_action))
2056
0
    termination_action = RADIUS_TERMINATION_ACTION_DEFAULT;
2057
2058
0
  if (hapd->conf->acct_interim_interval == 0 &&
2059
0
      hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
2060
0
      radius_msg_get_attr_int32(msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
2061
0
              &acct_interim_interval) == 0) {
2062
0
    if (acct_interim_interval < 60) {
2063
0
      hostapd_logger(hapd, sta->addr,
2064
0
               HOSTAPD_MODULE_IEEE8021X,
2065
0
               HOSTAPD_LEVEL_INFO,
2066
0
               "ignored too small Acct-Interim-Interval %d",
2067
0
               acct_interim_interval);
2068
0
    } else
2069
0
      sta->acct_interim_interval = acct_interim_interval;
2070
0
  }
2071
2072
2073
0
  switch (hdr->code) {
2074
0
  case RADIUS_CODE_ACCESS_ACCEPT:
2075
0
#ifndef CONFIG_NO_VLAN
2076
0
    if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
2077
0
        ieee802_1x_update_vlan(msg, hapd, sta) < 0)
2078
0
      break;
2079
2080
0
    if (sta->vlan_id > 0) {
2081
0
      hostapd_logger(hapd, sta->addr,
2082
0
               HOSTAPD_MODULE_RADIUS,
2083
0
               HOSTAPD_LEVEL_INFO,
2084
0
               "VLAN ID %d", sta->vlan_id);
2085
0
    }
2086
2087
0
    if ((sta->flags & WLAN_STA_ASSOC) &&
2088
0
        ap_sta_bind_vlan(hapd, sta) < 0)
2089
0
      break;
2090
0
#endif /* CONFIG_NO_VLAN */
2091
2092
0
    sta->session_timeout_set = !!session_timeout_set;
2093
0
    os_get_reltime(&sta->session_timeout);
2094
0
    sta->session_timeout.sec += session_timeout;
2095
2096
    /* RFC 3580, Ch. 3.17 */
2097
0
    if (session_timeout_set && termination_action ==
2098
0
        RADIUS_TERMINATION_ACTION_RADIUS_REQUEST)
2099
0
      sm->reAuthPeriod = session_timeout;
2100
0
    else if (session_timeout_set)
2101
0
      ap_sta_session_timeout(hapd, sta, session_timeout);
2102
0
    else
2103
0
      ap_sta_no_session_timeout(hapd, sta);
2104
2105
0
    sm->eap_if->aaaSuccess = true;
2106
0
    override_eapReq = 1;
2107
0
    ieee802_1x_get_keys(hapd, sta, msg, req, shared_secret,
2108
0
            shared_secret_len);
2109
0
    ieee802_1x_store_radius_class(hapd, sta, msg);
2110
0
    ieee802_1x_update_sta_identity(hapd, sta, msg);
2111
0
    ieee802_1x_update_sta_cui(hapd, sta, msg);
2112
0
    ieee802_1x_check_hs20(hapd, sta, msg,
2113
0
              session_timeout_set ?
2114
0
              (int) session_timeout : -1);
2115
0
    break;
2116
0
  case RADIUS_CODE_ACCESS_REJECT:
2117
0
    sm->eap_if->aaaFail = true;
2118
0
    override_eapReq = 1;
2119
0
    if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_WLAN_REASON_CODE,
2120
0
                &reason_code) == 0) {
2121
0
      wpa_printf(MSG_DEBUG,
2122
0
           "RADIUS server indicated WLAN-Reason-Code %u in Access-Reject for "
2123
0
           MACSTR, reason_code, MAC2STR(sta->addr));
2124
0
      sta->disconnect_reason_code = reason_code;
2125
0
    }
2126
0
    break;
2127
0
  case RADIUS_CODE_ACCESS_CHALLENGE:
2128
0
    sm->eap_if->aaaEapReq = true;
2129
0
    if (session_timeout_set) {
2130
      /* RFC 2869, Ch. 2.3.2; RFC 3580, Ch. 3.17 */
2131
0
      sm->eap_if->aaaMethodTimeout = session_timeout;
2132
0
      hostapd_logger(hapd, sm->addr,
2133
0
               HOSTAPD_MODULE_IEEE8021X,
2134
0
               HOSTAPD_LEVEL_DEBUG,
2135
0
               "using EAP timeout of %d seconds (from RADIUS)",
2136
0
               sm->eap_if->aaaMethodTimeout);
2137
0
    } else {
2138
      /*
2139
       * Use dynamic retransmission behavior per EAP
2140
       * specification.
2141
       */
2142
0
      sm->eap_if->aaaMethodTimeout = 0;
2143
0
    }
2144
0
    break;
2145
0
  }
2146
2147
0
  ieee802_1x_decapsulate_radius(hapd, sta);
2148
0
  if (override_eapReq)
2149
0
    sm->eap_if->aaaEapReq = false;
2150
2151
#ifdef CONFIG_FILS
2152
#ifdef NEED_AP_MLME
2153
  if (sta->flags &
2154
      (WLAN_STA_PENDING_FILS_ERP | WLAN_STA_PENDING_PASN_FILS_ERP)) {
2155
    /* TODO: Add a PMKSA entry on success? */
2156
    ieee802_11_finish_fils_auth(
2157
      hapd, sta, hdr->code == RADIUS_CODE_ACCESS_ACCEPT,
2158
      sm->eap_if->aaaEapReqData,
2159
      sm->eap_if->aaaEapKeyData,
2160
      sm->eap_if->aaaEapKeyDataLen);
2161
  }
2162
#endif /* NEED_AP_MLME */
2163
#endif /* CONFIG_FILS */
2164
2165
0
  eapol_auth_step(sm);
2166
2167
0
  return RADIUS_RX_QUEUED;
2168
0
}
2169
#endif /* CONFIG_NO_RADIUS */
2170
2171
2172
void ieee802_1x_abort_auth(struct hostapd_data *hapd, struct sta_info *sta)
2173
0
{
2174
0
  struct eapol_state_machine *sm = sta->eapol_sm;
2175
2176
0
  if (!sm)
2177
0
    return;
2178
2179
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2180
0
           HOSTAPD_LEVEL_DEBUG, "aborting authentication");
2181
2182
0
#ifndef CONFIG_NO_RADIUS
2183
0
  radius_msg_free(sm->last_recv_radius);
2184
0
  sm->last_recv_radius = NULL;
2185
0
#endif /* CONFIG_NO_RADIUS */
2186
2187
0
  if (sm->eap_if->eapTimeout) {
2188
    /*
2189
     * Disconnect the STA since it did not reply to the last EAP
2190
     * request and we cannot continue EAP processing (EAP-Failure
2191
     * could only be sent if the EAP peer actually replied).
2192
     */
2193
0
    wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "EAP Timeout, STA " MACSTR,
2194
0
      MAC2STR(sta->addr));
2195
2196
0
    sm->eap_if->portEnabled = false;
2197
0
    ap_sta_disconnect(hapd, sta, sta->addr,
2198
0
          WLAN_REASON_PREV_AUTH_NOT_VALID);
2199
0
  }
2200
0
}
2201
2202
2203
#ifdef CONFIG_WEP
2204
2205
static int ieee802_1x_rekey_broadcast(struct hostapd_data *hapd)
2206
{
2207
  struct eapol_authenticator *eapol = hapd->eapol_auth;
2208
2209
  if (hapd->conf->default_wep_key_len < 1)
2210
    return 0;
2211
2212
  os_free(eapol->default_wep_key);
2213
  eapol->default_wep_key = os_malloc(hapd->conf->default_wep_key_len);
2214
  if (!eapol->default_wep_key ||
2215
      random_get_bytes(eapol->default_wep_key,
2216
           hapd->conf->default_wep_key_len)) {
2217
    wpa_printf(MSG_INFO, "Could not generate random WEP key");
2218
    os_free(eapol->default_wep_key);
2219
    eapol->default_wep_key = NULL;
2220
    return -1;
2221
  }
2222
2223
  wpa_hexdump_key(MSG_DEBUG, "IEEE 802.1X: New default WEP key",
2224
      eapol->default_wep_key,
2225
      hapd->conf->default_wep_key_len);
2226
2227
  return 0;
2228
}
2229
2230
2231
static int ieee802_1x_sta_key_available(struct hostapd_data *hapd,
2232
          struct sta_info *sta, void *ctx)
2233
{
2234
  if (sta->eapol_sm) {
2235
    sta->eapol_sm->eap_if->eapKeyAvailable = true;
2236
    eapol_auth_step(sta->eapol_sm);
2237
  }
2238
  return 0;
2239
}
2240
2241
2242
static void ieee802_1x_rekey(void *eloop_ctx, void *timeout_ctx)
2243
{
2244
  struct hostapd_data *hapd = eloop_ctx;
2245
  struct eapol_authenticator *eapol = hapd->eapol_auth;
2246
2247
  if (eapol->default_wep_key_idx >= 3)
2248
    eapol->default_wep_key_idx =
2249
      hapd->conf->individual_wep_key_len > 0 ? 1 : 0;
2250
  else
2251
    eapol->default_wep_key_idx++;
2252
2253
  wpa_printf(MSG_DEBUG, "IEEE 802.1X: New default WEP key index %d",
2254
       eapol->default_wep_key_idx);
2255
2256
  if (ieee802_1x_rekey_broadcast(hapd)) {
2257
    hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
2258
             HOSTAPD_LEVEL_WARNING,
2259
             "failed to generate a new broadcast key");
2260
    os_free(eapol->default_wep_key);
2261
    eapol->default_wep_key = NULL;
2262
    return;
2263
  }
2264
2265
  /* TODO: Could setup key for RX here, but change default TX keyid only
2266
   * after new broadcast key has been sent to all stations. */
2267
  if (hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_WEP,
2268
        broadcast_ether_addr,
2269
        eapol->default_wep_key_idx, 0, 1, NULL, 0,
2270
        eapol->default_wep_key,
2271
        hapd->conf->default_wep_key_len,
2272
        KEY_FLAG_GROUP_RX_TX_DEFAULT)) {
2273
    hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE8021X,
2274
             HOSTAPD_LEVEL_WARNING,
2275
             "failed to configure a new broadcast key");
2276
    os_free(eapol->default_wep_key);
2277
    eapol->default_wep_key = NULL;
2278
    return;
2279
  }
2280
2281
  ap_for_each_sta(hapd, ieee802_1x_sta_key_available, NULL);
2282
2283
  if (hapd->conf->wep_rekeying_period > 0) {
2284
    eloop_register_timeout(hapd->conf->wep_rekeying_period, 0,
2285
               ieee802_1x_rekey, hapd, NULL);
2286
  }
2287
}
2288
2289
#endif /* CONFIG_WEP */
2290
2291
2292
static void ieee802_1x_eapol_send(void *ctx, void *sta_ctx, u8 type,
2293
          const u8 *data, size_t datalen)
2294
0
{
2295
0
#ifdef CONFIG_WPS
2296
0
  struct sta_info *sta = sta_ctx;
2297
2298
0
  if ((sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)) ==
2299
0
      WLAN_STA_MAYBE_WPS) {
2300
0
    const u8 *identity;
2301
0
    size_t identity_len;
2302
0
    struct eapol_state_machine *sm = sta->eapol_sm;
2303
2304
0
    identity = eap_get_identity(sm->eap, &identity_len);
2305
0
    if (identity &&
2306
0
        ((identity_len == WSC_ID_ENROLLEE_LEN &&
2307
0
          os_memcmp(identity, WSC_ID_ENROLLEE,
2308
0
        WSC_ID_ENROLLEE_LEN) == 0) ||
2309
0
         (identity_len == WSC_ID_REGISTRAR_LEN &&
2310
0
          os_memcmp(identity, WSC_ID_REGISTRAR,
2311
0
        WSC_ID_REGISTRAR_LEN) == 0))) {
2312
0
      wpa_printf(MSG_DEBUG,
2313
0
           "WPS: WLAN_STA_MAYBE_WPS -> WLAN_STA_WPS");
2314
0
      sta->flags |= WLAN_STA_WPS;
2315
0
    }
2316
0
  }
2317
0
#endif /* CONFIG_WPS */
2318
2319
0
  ieee802_1x_send(ctx, sta_ctx, type, data, datalen);
2320
0
}
2321
2322
2323
static void ieee802_1x_aaa_send(void *ctx, void *sta_ctx,
2324
        const u8 *data, size_t datalen)
2325
0
{
2326
0
#ifndef CONFIG_NO_RADIUS
2327
0
  struct hostapd_data *hapd = ctx;
2328
0
  struct sta_info *sta = sta_ctx;
2329
2330
0
  ieee802_1x_encapsulate_radius(hapd, sta, data, datalen);
2331
0
#endif /* CONFIG_NO_RADIUS */
2332
0
}
2333
2334
2335
static bool _ieee802_1x_finished(void *ctx, void *sta_ctx, int success,
2336
         int preauth, bool logoff)
2337
0
{
2338
0
  struct hostapd_data *hapd = ctx;
2339
0
  struct sta_info *sta = sta_ctx;
2340
2341
0
  if (preauth) {
2342
0
    rsn_preauth_finished(hapd, sta, success);
2343
0
    return false;
2344
0
  }
2345
2346
0
  return ieee802_1x_finished(hapd, sta, success, logoff);
2347
0
}
2348
2349
2350
static int ieee802_1x_get_eap_user(void *ctx, const u8 *identity,
2351
           size_t identity_len, int phase2,
2352
           struct eap_user *user)
2353
0
{
2354
0
  struct hostapd_data *hapd = ctx;
2355
0
  const struct hostapd_eap_user *eap_user;
2356
0
  int i;
2357
0
  int rv = -1;
2358
2359
0
  eap_user = hostapd_get_eap_user(hapd, identity, identity_len, phase2);
2360
0
  if (!eap_user)
2361
0
    goto out;
2362
2363
0
  os_memset(user, 0, sizeof(*user));
2364
0
  user->phase2 = phase2;
2365
0
  for (i = 0; i < EAP_MAX_METHODS; i++) {
2366
0
    user->methods[i].vendor = eap_user->methods[i].vendor;
2367
0
    user->methods[i].method = eap_user->methods[i].method;
2368
0
  }
2369
2370
0
  if (eap_user->password) {
2371
0
    user->password = os_memdup(eap_user->password,
2372
0
             eap_user->password_len);
2373
0
    if (!user->password)
2374
0
      goto out;
2375
0
    user->password_len = eap_user->password_len;
2376
0
    user->password_hash = eap_user->password_hash;
2377
0
    if (eap_user->salt && eap_user->salt_len) {
2378
0
      user->salt = os_memdup(eap_user->salt,
2379
0
                 eap_user->salt_len);
2380
0
      if (!user->salt)
2381
0
        goto out;
2382
0
      user->salt_len = eap_user->salt_len;
2383
0
    }
2384
0
  }
2385
0
  user->force_version = eap_user->force_version;
2386
0
  user->macacl = eap_user->macacl;
2387
0
  user->ttls_auth = eap_user->ttls_auth;
2388
0
  rv = 0;
2389
2390
0
out:
2391
0
  if (rv)
2392
0
    wpa_printf(MSG_DEBUG, "%s: Failed to find user", __func__);
2393
2394
0
  return rv;
2395
0
}
2396
2397
2398
static int ieee802_1x_sta_entry_alive(void *ctx, const u8 *addr)
2399
0
{
2400
0
  struct hostapd_data *hapd = ctx;
2401
0
  struct sta_info *sta;
2402
2403
0
  sta = ap_get_sta(hapd, addr);
2404
0
  if (!sta || !sta->eapol_sm)
2405
0
    return 0;
2406
0
  return 1;
2407
0
}
2408
2409
2410
static void ieee802_1x_logger(void *ctx, const u8 *addr,
2411
            eapol_logger_level level, const char *txt)
2412
0
{
2413
0
#ifndef CONFIG_NO_HOSTAPD_LOGGER
2414
0
  struct hostapd_data *hapd = ctx;
2415
0
  int hlevel;
2416
2417
0
  switch (level) {
2418
0
  case EAPOL_LOGGER_WARNING:
2419
0
    hlevel = HOSTAPD_LEVEL_WARNING;
2420
0
    break;
2421
0
  case EAPOL_LOGGER_INFO:
2422
0
    hlevel = HOSTAPD_LEVEL_INFO;
2423
0
    break;
2424
0
  case EAPOL_LOGGER_DEBUG:
2425
0
  default:
2426
0
    hlevel = HOSTAPD_LEVEL_DEBUG;
2427
0
    break;
2428
0
  }
2429
2430
0
  hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE8021X, hlevel, "%s",
2431
0
           txt);
2432
0
#endif /* CONFIG_NO_HOSTAPD_LOGGER */
2433
0
}
2434
2435
2436
static void ieee802_1x_set_port_authorized(void *ctx, void *sta_ctx,
2437
             int authorized)
2438
0
{
2439
0
  struct hostapd_data *hapd = ctx;
2440
0
  struct sta_info *sta = sta_ctx;
2441
2442
0
  ieee802_1x_set_sta_authorized(hapd, sta, authorized);
2443
0
}
2444
2445
2446
static void _ieee802_1x_abort_auth(void *ctx, void *sta_ctx)
2447
0
{
2448
0
  struct hostapd_data *hapd = ctx;
2449
0
  struct sta_info *sta = sta_ctx;
2450
2451
0
  ieee802_1x_abort_auth(hapd, sta);
2452
0
}
2453
2454
2455
#ifdef CONFIG_WEP
2456
static void _ieee802_1x_tx_key(void *ctx, void *sta_ctx)
2457
{
2458
#ifndef CONFIG_FIPS
2459
#ifndef CONFIG_NO_RC4
2460
  struct hostapd_data *hapd = ctx;
2461
  struct sta_info *sta = sta_ctx;
2462
2463
  ieee802_1x_tx_key(hapd, sta);
2464
#endif /* CONFIG_NO_RC4 */
2465
#endif /* CONFIG_FIPS */
2466
}
2467
#endif /* CONFIG_WEP */
2468
2469
2470
static void ieee802_1x_eapol_event(void *ctx, void *sta_ctx,
2471
           enum eapol_event type)
2472
0
{
2473
  /* struct hostapd_data *hapd = ctx; */
2474
0
  struct sta_info *sta = sta_ctx;
2475
2476
0
  switch (type) {
2477
0
  case EAPOL_AUTH_SM_CHANGE:
2478
0
    wpa_auth_sm_notify(sta->wpa_sm);
2479
0
    break;
2480
0
  case EAPOL_AUTH_REAUTHENTICATE:
2481
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH_EAPOL);
2482
0
    break;
2483
0
  }
2484
0
}
2485
2486
2487
#ifdef CONFIG_ERP
2488
2489
static struct eap_server_erp_key *
2490
ieee802_1x_erp_get_key(void *ctx, const char *keyname)
2491
{
2492
  struct hostapd_data *hapd = ctx;
2493
  struct eap_server_erp_key *erp;
2494
2495
  dl_list_for_each(erp, &hapd->erp_keys, struct eap_server_erp_key,
2496
       list) {
2497
    if (os_strcmp(erp->keyname_nai, keyname) == 0)
2498
      return erp;
2499
  }
2500
2501
  return NULL;
2502
}
2503
2504
2505
static int ieee802_1x_erp_add_key(void *ctx, struct eap_server_erp_key *erp)
2506
{
2507
  struct hostapd_data *hapd = ctx;
2508
2509
  dl_list_add(&hapd->erp_keys, &erp->list);
2510
  return 0;
2511
}
2512
2513
#endif /* CONFIG_ERP */
2514
2515
2516
int ieee802_1x_init(struct hostapd_data *hapd)
2517
0
{
2518
0
  struct eapol_auth_config conf;
2519
0
  struct eapol_auth_cb cb;
2520
2521
#ifdef CONFIG_IEEE80211BE
2522
  if (!hostapd_mld_is_first_bss(hapd)) {
2523
    struct hostapd_data *first;
2524
2525
    first = hostapd_mld_get_first_bss(hapd);
2526
    if (!first)
2527
      return -1;
2528
2529
    if (!first->eapol_auth) {
2530
      wpa_printf(MSG_DEBUG,
2531
           "MLD: First BSS IEEE 802.1X state machine does not exist. Init on its behalf");
2532
2533
      if (ieee802_1x_init(first))
2534
        return -1;
2535
    }
2536
2537
    wpa_printf(MSG_DEBUG,
2538
         "MLD: Using IEEE 802.1X state machine of the first BSS");
2539
2540
    hapd->eapol_auth = first->eapol_auth;
2541
    return 0;
2542
  }
2543
#endif /* CONFIG_IEEE80211BE */
2544
2545
0
  os_memset(&conf, 0, sizeof(conf));
2546
0
  conf.eap_cfg = hapd->eap_cfg;
2547
0
  conf.ctx = hapd;
2548
0
  conf.eap_reauth_period = hapd->conf->eap_reauth_period;
2549
0
  conf.wpa = hapd->conf->wpa;
2550
#ifdef CONFIG_WEP
2551
  conf.individual_wep_key_len = hapd->conf->individual_wep_key_len;
2552
#endif /* CONFIG_WEP */
2553
0
  conf.eap_req_id_text = hapd->conf->eap_req_id_text;
2554
0
  conf.eap_req_id_text_len = hapd->conf->eap_req_id_text_len;
2555
0
  conf.erp_send_reauth_start = hapd->conf->erp_send_reauth_start;
2556
0
  conf.erp_domain = hapd->conf->erp_domain;
2557
#ifdef CONFIG_TESTING_OPTIONS
2558
  conf.eap_skip_prot_success = hapd->conf->eap_skip_prot_success;
2559
#endif /* CONFIG_TESTING_OPTIONS */
2560
2561
0
  os_memset(&cb, 0, sizeof(cb));
2562
0
  cb.eapol_send = ieee802_1x_eapol_send;
2563
0
  cb.aaa_send = ieee802_1x_aaa_send;
2564
0
  cb.finished = _ieee802_1x_finished;
2565
0
  cb.get_eap_user = ieee802_1x_get_eap_user;
2566
0
  cb.sta_entry_alive = ieee802_1x_sta_entry_alive;
2567
0
  cb.logger = ieee802_1x_logger;
2568
0
  cb.set_port_authorized = ieee802_1x_set_port_authorized;
2569
0
  cb.abort_auth = _ieee802_1x_abort_auth;
2570
#ifdef CONFIG_WEP
2571
  cb.tx_key = _ieee802_1x_tx_key;
2572
#endif /* CONFIG_WEP */
2573
0
  cb.eapol_event = ieee802_1x_eapol_event;
2574
#ifdef CONFIG_ERP
2575
  cb.erp_get_key = ieee802_1x_erp_get_key;
2576
  cb.erp_add_key = ieee802_1x_erp_add_key;
2577
#endif /* CONFIG_ERP */
2578
2579
0
  hapd->eapol_auth = eapol_auth_init(&conf, &cb);
2580
0
  if (!hapd->eapol_auth)
2581
0
    return -1;
2582
2583
0
  if ((hapd->conf->ieee802_1x || hapd->conf->wpa) &&
2584
0
      hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1))
2585
0
    return -1;
2586
2587
0
#ifndef CONFIG_NO_RADIUS
2588
0
  if (radius_client_register(hapd->radius, RADIUS_AUTH,
2589
0
           ieee802_1x_receive_auth, hapd))
2590
0
    return -1;
2591
0
#endif /* CONFIG_NO_RADIUS */
2592
2593
#ifdef CONFIG_WEP
2594
  if (hapd->conf->default_wep_key_len) {
2595
    int i;
2596
2597
    for (i = 0; i < 4; i++)
2598
      hostapd_drv_set_key(hapd->conf->iface, hapd,
2599
              WPA_ALG_NONE, NULL, i, 0, 0, NULL,
2600
              0, NULL, 0, KEY_FLAG_GROUP);
2601
2602
    ieee802_1x_rekey(hapd, NULL);
2603
2604
    if (!hapd->eapol_auth->default_wep_key)
2605
      return -1;
2606
  }
2607
#endif /* CONFIG_WEP */
2608
2609
0
  return 0;
2610
0
}
2611
2612
2613
void ieee802_1x_erp_flush(struct hostapd_data *hapd)
2614
0
{
2615
0
  struct eap_server_erp_key *erp;
2616
2617
0
  while ((erp = dl_list_first(&hapd->erp_keys, struct eap_server_erp_key,
2618
0
            list)) != NULL) {
2619
0
    dl_list_del(&erp->list);
2620
0
    bin_clear_free(erp, sizeof(*erp));
2621
0
  }
2622
0
}
2623
2624
2625
void ieee802_1x_deinit(struct hostapd_data *hapd)
2626
0
{
2627
#ifdef CONFIG_IEEE80211BE
2628
  if (!hostapd_mld_is_first_bss(hapd)) {
2629
    wpa_printf(MSG_DEBUG,
2630
         "MLD: Deinit IEEE 802.1X state machine of a non-first BSS");
2631
2632
    hapd->eapol_auth = NULL;
2633
    return;
2634
  }
2635
#endif /* CONFIG_IEEE80211BE */
2636
2637
#ifdef CONFIG_WEP
2638
  eloop_cancel_timeout(ieee802_1x_rekey, hapd, NULL);
2639
#endif /* CONFIG_WEP */
2640
2641
0
  if (hapd->driver && hapd->drv_priv &&
2642
0
      (hapd->conf->ieee802_1x || hapd->conf->wpa))
2643
0
    hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
2644
2645
0
  eapol_auth_deinit(hapd->eapol_auth);
2646
0
  hapd->eapol_auth = NULL;
2647
2648
0
  ieee802_1x_erp_flush(hapd);
2649
0
}
2650
2651
2652
int ieee802_1x_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2653
       const u8 *buf, size_t len, int ack)
2654
0
{
2655
0
  struct ieee80211_hdr *hdr;
2656
0
  u8 *pos;
2657
0
  const unsigned char rfc1042_hdr[ETH_ALEN] =
2658
0
    { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
2659
2660
0
  if (!sta)
2661
0
    return -1;
2662
0
  if (len < sizeof(*hdr) + sizeof(rfc1042_hdr) + 2)
2663
0
    return 0;
2664
2665
0
  hdr = (struct ieee80211_hdr *) buf;
2666
0
  pos = (u8 *) (hdr + 1);
2667
0
  if (os_memcmp(pos, rfc1042_hdr, sizeof(rfc1042_hdr)) != 0)
2668
0
    return 0;
2669
0
  pos += sizeof(rfc1042_hdr);
2670
0
  if (WPA_GET_BE16(pos) != ETH_P_PAE)
2671
0
    return 0;
2672
0
  pos += 2;
2673
2674
0
  return ieee802_1x_eapol_tx_status(hapd, sta, pos, buf + len - pos,
2675
0
            ack);
2676
0
}
2677
2678
2679
int ieee802_1x_eapol_tx_status(struct hostapd_data *hapd, struct sta_info *sta,
2680
             const u8 *buf, int len, int ack)
2681
0
{
2682
0
  const struct ieee802_1x_hdr *xhdr =
2683
0
    (const struct ieee802_1x_hdr *) buf;
2684
0
  const u8 *pos = buf + sizeof(*xhdr);
2685
0
  struct ieee802_1x_eapol_key *key;
2686
2687
0
  if (len < (int) sizeof(*xhdr))
2688
0
    return 0;
2689
0
  wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
2690
0
       " TX status - version=%d type=%d length=%d - ack=%d",
2691
0
       MAC2STR(sta->addr), xhdr->version, xhdr->type,
2692
0
       be_to_host16(xhdr->length), ack);
2693
2694
0
#ifdef CONFIG_WPS
2695
0
  if (xhdr->type == IEEE802_1X_TYPE_EAP_PACKET && ack &&
2696
0
      (sta->flags & WLAN_STA_WPS) &&
2697
0
      ap_sta_pending_delayed_1x_auth_fail_disconnect(hapd, sta)) {
2698
0
    wpa_printf(MSG_DEBUG,
2699
0
         "WPS: Indicate EAP completion on ACK for EAP-Failure");
2700
0
    hostapd_wps_eap_completed(hapd);
2701
0
  }
2702
0
#endif /* CONFIG_WPS */
2703
2704
0
  if (xhdr->type != IEEE802_1X_TYPE_EAPOL_KEY)
2705
0
    return 0;
2706
2707
0
  if (pos + sizeof(struct wpa_eapol_key) <= buf + len) {
2708
0
    const struct wpa_eapol_key *wpa;
2709
2710
0
    wpa = (const struct wpa_eapol_key *) pos;
2711
0
    if (wpa->type == EAPOL_KEY_TYPE_RSN ||
2712
0
        wpa->type == EAPOL_KEY_TYPE_WPA)
2713
0
      wpa_auth_eapol_key_tx_status(hapd->wpa_auth,
2714
0
                 sta->wpa_sm, ack);
2715
0
  }
2716
2717
  /* EAPOL EAP-Packet packets are eventually re-sent by either Supplicant
2718
   * or Authenticator state machines, but EAPOL-Key packets are not
2719
   * retransmitted in case of failure. Try to re-send failed EAPOL-Key
2720
   * packets couple of times because otherwise STA keys become
2721
   * unsynchronized with AP. */
2722
0
  if (!ack && pos + sizeof(*key) <= buf + len) {
2723
0
    key = (struct ieee802_1x_eapol_key *) pos;
2724
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
2725
0
             HOSTAPD_LEVEL_DEBUG,
2726
0
             "did not Ack EAPOL-Key frame (%scast index=%d)",
2727
0
             key->key_index & BIT(7) ? "uni" : "broad",
2728
0
             key->key_index & ~BIT(7));
2729
    /* TODO: re-send EAPOL-Key couple of times (with short delay
2730
     * between them?). If all attempt fail, report error and
2731
     * deauthenticate STA so that it will get new keys when
2732
     * authenticating again (e.g., after returning in range).
2733
     * Separate limit/transmit state needed both for unicast and
2734
     * broadcast keys(?) */
2735
0
  }
2736
  /* TODO: could move unicast key configuration from ieee802_1x_tx_key()
2737
   * to here and change the key only if the EAPOL-Key packet was Acked.
2738
   */
2739
2740
0
  return 1;
2741
0
}
2742
2743
2744
u8 * ieee802_1x_get_identity(struct eapol_state_machine *sm, size_t *len)
2745
0
{
2746
0
  if (!sm || !sm->identity)
2747
0
    return NULL;
2748
2749
0
  *len = sm->identity_len;
2750
0
  return sm->identity;
2751
0
}
2752
2753
2754
u8 * ieee802_1x_get_radius_class(struct eapol_state_machine *sm, size_t *len,
2755
         int idx)
2756
0
{
2757
0
  if (!sm || !sm->radius_class.attr ||
2758
0
      idx >= (int) sm->radius_class.count)
2759
0
    return NULL;
2760
2761
0
  *len = sm->radius_class.attr[idx].len;
2762
0
  return sm->radius_class.attr[idx].data;
2763
0
}
2764
2765
2766
struct wpabuf * ieee802_1x_get_radius_cui(struct eapol_state_machine *sm)
2767
0
{
2768
0
  if (!sm)
2769
0
    return NULL;
2770
0
  return sm->radius_cui;
2771
0
}
2772
2773
2774
const u8 * ieee802_1x_get_key(struct eapol_state_machine *sm, size_t *len)
2775
0
{
2776
0
  *len = 0;
2777
0
  if (!sm)
2778
0
    return NULL;
2779
2780
0
  *len = sm->eap_if->eapKeyDataLen;
2781
0
  return sm->eap_if->eapKeyData;
2782
0
}
2783
2784
2785
#ifdef CONFIG_MACSEC
2786
const u8 * ieee802_1x_get_session_id(struct eapol_state_machine *sm,
2787
             size_t *len)
2788
{
2789
  *len = 0;
2790
  if (!sm || !sm->eap_if)
2791
    return NULL;
2792
2793
  *len = sm->eap_if->eapSessionIdLen;
2794
  return sm->eap_if->eapSessionId;
2795
}
2796
#endif /* CONFIG_MACSEC */
2797
2798
2799
void ieee802_1x_notify_port_enabled(struct eapol_state_machine *sm,
2800
            bool enabled)
2801
0
{
2802
0
  if (!sm)
2803
0
    return;
2804
0
  sm->eap_if->portEnabled = enabled;
2805
0
  eapol_auth_step(sm);
2806
0
}
2807
2808
2809
void ieee802_1x_notify_port_valid(struct eapol_state_machine *sm, bool valid)
2810
0
{
2811
0
  if (!sm)
2812
0
    return;
2813
0
  sm->portValid = valid;
2814
0
  eapol_auth_step(sm);
2815
0
}
2816
2817
2818
void ieee802_1x_notify_pre_auth(struct eapol_state_machine *sm, bool pre_auth)
2819
0
{
2820
0
  if (!sm)
2821
0
    return;
2822
0
  if (pre_auth)
2823
0
    sm->flags |= EAPOL_SM_PREAUTH;
2824
0
  else
2825
0
    sm->flags &= ~EAPOL_SM_PREAUTH;
2826
0
}
2827
2828
2829
static const char * bool_txt(bool val)
2830
0
{
2831
0
  return val ? "TRUE" : "FALSE";
2832
0
}
2833
2834
2835
int ieee802_1x_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2836
0
{
2837
  /* TODO */
2838
0
  return 0;
2839
0
}
2840
2841
2842
int ieee802_1x_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2843
         char *buf, size_t buflen)
2844
0
{
2845
0
  int len = 0, ret;
2846
0
  struct eapol_state_machine *sm = sta->eapol_sm;
2847
0
  struct os_reltime diff;
2848
0
  const char *name1;
2849
0
  const char *name2;
2850
0
  char *identity_buf = NULL;
2851
2852
0
  if (!sm)
2853
0
    return 0;
2854
2855
0
  ret = os_snprintf(buf + len, buflen - len,
2856
0
        "dot1xPaePortNumber=%d\n"
2857
0
        "dot1xPaePortProtocolVersion=%d\n"
2858
0
        "dot1xPaePortCapabilities=1\n"
2859
0
        "dot1xPaePortInitialize=%d\n"
2860
0
        "dot1xPaePortReauthenticate=FALSE\n",
2861
0
        sta->aid,
2862
0
        EAPOL_VERSION,
2863
0
        sm->initialize);
2864
0
  if (os_snprintf_error(buflen - len, ret))
2865
0
    return len;
2866
0
  len += ret;
2867
2868
  /* dot1xAuthConfigTable */
2869
0
  ret = os_snprintf(buf + len, buflen - len,
2870
0
        "dot1xAuthPaeState=%d\n"
2871
0
        "dot1xAuthBackendAuthState=%d\n"
2872
0
        "dot1xAuthAdminControlledDirections=%d\n"
2873
0
        "dot1xAuthOperControlledDirections=%d\n"
2874
0
        "dot1xAuthAuthControlledPortStatus=%d\n"
2875
0
        "dot1xAuthAuthControlledPortControl=%d\n"
2876
0
        "dot1xAuthQuietPeriod=%u\n"
2877
0
        "dot1xAuthServerTimeout=%u\n"
2878
0
        "dot1xAuthReAuthPeriod=%u\n"
2879
0
        "dot1xAuthReAuthEnabled=%s\n"
2880
0
        "dot1xAuthKeyTxEnabled=%s\n",
2881
0
        sm->auth_pae_state + 1,
2882
0
        sm->be_auth_state + 1,
2883
0
        sm->adminControlledDirections,
2884
0
        sm->operControlledDirections,
2885
0
        sm->authPortStatus,
2886
0
        sm->portControl,
2887
0
        sm->quietPeriod,
2888
0
        sm->serverTimeout,
2889
0
        sm->reAuthPeriod,
2890
0
        bool_txt(sm->reAuthEnabled),
2891
0
        bool_txt(sm->keyTxEnabled));
2892
0
  if (os_snprintf_error(buflen - len, ret))
2893
0
    return len;
2894
0
  len += ret;
2895
2896
  /* dot1xAuthStatsTable */
2897
0
  ret = os_snprintf(buf + len, buflen - len,
2898
0
        "dot1xAuthEapolFramesRx=%u\n"
2899
0
        "dot1xAuthEapolFramesTx=%u\n"
2900
0
        "dot1xAuthEapolStartFramesRx=%u\n"
2901
0
        "dot1xAuthEapolLogoffFramesRx=%u\n"
2902
0
        "dot1xAuthEapolRespIdFramesRx=%u\n"
2903
0
        "dot1xAuthEapolRespFramesRx=%u\n"
2904
0
        "dot1xAuthEapolReqIdFramesTx=%u\n"
2905
0
        "dot1xAuthEapolReqFramesTx=%u\n"
2906
0
        "dot1xAuthInvalidEapolFramesRx=%u\n"
2907
0
        "dot1xAuthEapLengthErrorFramesRx=%u\n"
2908
0
        "dot1xAuthLastEapolFrameVersion=%u\n"
2909
0
        "dot1xAuthLastEapolFrameSource=" MACSTR "\n",
2910
0
        sm->dot1xAuthEapolFramesRx,
2911
0
        sm->dot1xAuthEapolFramesTx,
2912
0
        sm->dot1xAuthEapolStartFramesRx,
2913
0
        sm->dot1xAuthEapolLogoffFramesRx,
2914
0
        sm->dot1xAuthEapolRespIdFramesRx,
2915
0
        sm->dot1xAuthEapolRespFramesRx,
2916
0
        sm->dot1xAuthEapolReqIdFramesTx,
2917
0
        sm->dot1xAuthEapolReqFramesTx,
2918
0
        sm->dot1xAuthInvalidEapolFramesRx,
2919
0
        sm->dot1xAuthEapLengthErrorFramesRx,
2920
0
        sm->dot1xAuthLastEapolFrameVersion,
2921
0
        MAC2STR(sm->addr));
2922
0
  if (os_snprintf_error(buflen - len, ret))
2923
0
    return len;
2924
0
  len += ret;
2925
2926
  /* dot1xAuthDiagTable */
2927
0
  ret = os_snprintf(buf + len, buflen - len,
2928
0
        "dot1xAuthEntersConnecting=%u\n"
2929
0
        "dot1xAuthEapLogoffsWhileConnecting=%u\n"
2930
0
        "dot1xAuthEntersAuthenticating=%u\n"
2931
0
        "dot1xAuthAuthSuccessesWhileAuthenticating=%u\n"
2932
0
        "dot1xAuthAuthTimeoutsWhileAuthenticating=%u\n"
2933
0
        "dot1xAuthAuthFailWhileAuthenticating=%u\n"
2934
0
        "dot1xAuthAuthEapStartsWhileAuthenticating=%u\n"
2935
0
        "dot1xAuthAuthEapLogoffWhileAuthenticating=%u\n"
2936
0
        "dot1xAuthAuthReauthsWhileAuthenticated=%u\n"
2937
0
        "dot1xAuthAuthEapStartsWhileAuthenticated=%u\n"
2938
0
        "dot1xAuthAuthEapLogoffWhileAuthenticated=%u\n"
2939
0
        "dot1xAuthBackendResponses=%u\n"
2940
0
        "dot1xAuthBackendAccessChallenges=%u\n"
2941
0
        "dot1xAuthBackendOtherRequestsToSupplicant=%u\n"
2942
0
        "dot1xAuthBackendAuthSuccesses=%u\n"
2943
0
        "dot1xAuthBackendAuthFails=%u\n",
2944
0
        sm->authEntersConnecting,
2945
0
        sm->authEapLogoffsWhileConnecting,
2946
0
        sm->authEntersAuthenticating,
2947
0
        sm->authAuthSuccessesWhileAuthenticating,
2948
0
        sm->authAuthTimeoutsWhileAuthenticating,
2949
0
        sm->authAuthFailWhileAuthenticating,
2950
0
        sm->authAuthEapStartsWhileAuthenticating,
2951
0
        sm->authAuthEapLogoffWhileAuthenticating,
2952
0
        sm->authAuthReauthsWhileAuthenticated,
2953
0
        sm->authAuthEapStartsWhileAuthenticated,
2954
0
        sm->authAuthEapLogoffWhileAuthenticated,
2955
0
        sm->backendResponses,
2956
0
        sm->backendAccessChallenges,
2957
0
        sm->backendOtherRequestsToSupplicant,
2958
0
        sm->backendAuthSuccesses,
2959
0
        sm->backendAuthFails);
2960
0
  if (os_snprintf_error(buflen - len, ret))
2961
0
    return len;
2962
0
  len += ret;
2963
2964
  /* dot1xAuthSessionStatsTable */
2965
0
  os_reltime_age(&sta->acct_session_start, &diff);
2966
0
  if (sm->eap && !sm->identity) {
2967
0
    const u8 *id;
2968
0
    size_t id_len;
2969
2970
0
    id = eap_get_identity(sm->eap, &id_len);
2971
0
    if (id)
2972
0
      identity_buf = dup_binstr(id, id_len);
2973
0
  }
2974
0
  ret = os_snprintf(buf + len, buflen - len,
2975
        /* TODO: dot1xAuthSessionOctetsRx */
2976
        /* TODO: dot1xAuthSessionOctetsTx */
2977
        /* TODO: dot1xAuthSessionFramesRx */
2978
        /* TODO: dot1xAuthSessionFramesTx */
2979
0
        "dot1xAuthSessionId=%016llX\n"
2980
0
        "dot1xAuthSessionAuthenticMethod=%d\n"
2981
0
        "dot1xAuthSessionTime=%u\n"
2982
0
        "dot1xAuthSessionTerminateCause=999\n"
2983
0
        "dot1xAuthSessionUserName=%s\n",
2984
0
        (unsigned long long) sta->acct_session_id,
2985
0
        (wpa_key_mgmt_wpa_ieee8021x(
2986
0
           wpa_auth_sta_key_mgmt(sta->wpa_sm))) ?
2987
0
        1 : 2,
2988
0
        (unsigned int) diff.sec,
2989
0
        sm->identity ? (char *) sm->identity :
2990
0
           (identity_buf ? identity_buf : "N/A"));
2991
0
  os_free(identity_buf);
2992
0
  if (os_snprintf_error(buflen - len, ret))
2993
0
    return len;
2994
0
  len += ret;
2995
2996
0
  if (sm->acct_multi_session_id) {
2997
0
    ret = os_snprintf(buf + len, buflen - len,
2998
0
          "authMultiSessionId=%016llX\n",
2999
0
          (unsigned long long)
3000
0
          sm->acct_multi_session_id);
3001
0
    if (os_snprintf_error(buflen - len, ret))
3002
0
      return len;
3003
0
    len += ret;
3004
0
  }
3005
3006
0
  name1 = eap_server_get_name(0, sm->eap_type_authsrv);
3007
0
  name2 = eap_server_get_name(0, sm->eap_type_supp);
3008
0
  ret = os_snprintf(buf + len, buflen - len,
3009
0
        "last_eap_type_as=%d (%s)\n"
3010
0
        "last_eap_type_sta=%d (%s)\n",
3011
0
        sm->eap_type_authsrv, name1,
3012
0
        sm->eap_type_supp, name2);
3013
0
  if (os_snprintf_error(buflen - len, ret))
3014
0
    return len;
3015
0
  len += ret;
3016
3017
0
  return len;
3018
0
}
3019
3020
3021
#ifdef CONFIG_HS20
3022
static void ieee802_1x_wnm_notif_send(void *eloop_ctx, void *timeout_ctx)
3023
0
{
3024
0
  struct hostapd_data *hapd = eloop_ctx;
3025
0
  struct sta_info *sta = timeout_ctx;
3026
3027
0
  if (sta->hs20_deauth_req) {
3028
0
    wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3029
0
         MACSTR " to indicate imminent deauthentication",
3030
0
         MAC2STR(sta->addr));
3031
0
    hs20_send_wnm_notification_deauth_req(hapd, sta->addr,
3032
0
                  sta->hs20_deauth_req);
3033
0
  }
3034
3035
0
  if (sta->hs20_t_c_filtering) {
3036
0
    wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to "
3037
0
         MACSTR " to indicate Terms and Conditions filtering",
3038
0
         MAC2STR(sta->addr));
3039
0
    hs20_send_wnm_notification_t_c(hapd, sta->addr, sta->t_c_url);
3040
0
    os_free(sta->t_c_url);
3041
0
    sta->t_c_url = NULL;
3042
0
  }
3043
0
}
3044
#endif /* CONFIG_HS20 */
3045
3046
3047
static bool ieee802_1x_finished(struct hostapd_data *hapd,
3048
        struct sta_info *sta, int success,
3049
        bool logoff)
3050
0
{
3051
0
  const u8 *key;
3052
0
  size_t len;
3053
  /* TODO: get PMKLifetime from WPA parameters */
3054
0
  static const int dot11RSNAConfigPMKLifetime = 43200;
3055
0
  unsigned int session_timeout;
3056
0
  struct os_reltime now, remaining;
3057
3058
0
#ifdef CONFIG_HS20
3059
0
  if (success && (sta->hs20_deauth_req || sta->hs20_t_c_filtering)) {
3060
0
    wpa_printf(MSG_DEBUG, "HS 2.0: Schedule WNM-Notification to "
3061
0
         MACSTR " in 100 ms", MAC2STR(sta->addr));
3062
0
    eloop_cancel_timeout(ieee802_1x_wnm_notif_send, hapd, sta);
3063
0
    eloop_register_timeout(0, 100000, ieee802_1x_wnm_notif_send,
3064
0
               hapd, sta);
3065
0
  }
3066
0
#endif /* CONFIG_HS20 */
3067
3068
#ifdef CONFIG_MACSEC
3069
  ieee802_1x_notify_create_actor_hapd(hapd, sta);
3070
#endif /* CONFIG_MACSEC */
3071
3072
0
  key = ieee802_1x_get_key(sta->eapol_sm, &len);
3073
0
  if (sta->session_timeout_set) {
3074
0
    os_get_reltime(&now);
3075
0
    os_reltime_sub(&sta->session_timeout, &now, &remaining);
3076
0
    session_timeout = (remaining.sec > 0) ? remaining.sec : 1;
3077
0
  } else {
3078
0
    session_timeout = dot11RSNAConfigPMKLifetime;
3079
0
  }
3080
0
  if (success && key && len >= PMK_LEN &&
3081
0
      !sta->hs20_deauth_requested &&
3082
0
      wpa_auth_pmksa_add(sta->wpa_sm, key, len, session_timeout,
3083
0
             sta->eapol_sm) == 0) {
3084
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
3085
0
             HOSTAPD_LEVEL_DEBUG,
3086
0
             "Added PMKSA cache entry (IEEE 802.1X)");
3087
0
  }
3088
3089
0
  if (!success) {
3090
    /*
3091
     * Many devices require deauthentication after WPS provisioning
3092
     * and some may not be be able to do that themselves, so
3093
     * disconnect the client here. In addition, this may also
3094
     * benefit IEEE 802.1X/EAPOL authentication cases, too since
3095
     * the EAPOL PAE state machine would remain in HELD state for
3096
     * considerable amount of time and some EAP methods, like
3097
     * EAP-FAST with anonymous provisioning, may require another
3098
     * EAPOL authentication to be started to complete connection.
3099
     */
3100
0
    ap_sta_delayed_1x_auth_fail_disconnect(hapd, sta,
3101
0
                   logoff ? 0 : 10);
3102
0
    if (logoff && sta->wpa_sm)
3103
0
      return true;
3104
0
  }
3105
3106
0
  return false;
3107
0
}