Coverage Report

Created: 2026-09-03 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hostap/src/ap/ieee802_11.c
Line
Count
Source
1
/*
2
 * hostapd / IEEE 802.11 Management
3
 * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
4
 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5
 *
6
 * This software may be distributed under the terms of the BSD license.
7
 * See README for more details.
8
 */
9
10
#include "utils/includes.h"
11
12
#ifndef CONFIG_NATIVE_WINDOWS
13
14
#include "utils/common.h"
15
#include "utils/eloop.h"
16
#include "crypto/crypto.h"
17
#include "crypto/sha256.h"
18
#include "crypto/sha384.h"
19
#include "crypto/sha512.h"
20
#include "crypto/random.h"
21
#include "crypto/aes.h"
22
#include "crypto/aes_siv.h"
23
#include "common/ieee802_11_defs.h"
24
#include "common/ieee802_11_common.h"
25
#include "common/wpa_ctrl.h"
26
#include "common/sae.h"
27
#include "common/dpp.h"
28
#include "common/ocv.h"
29
#include "common/wpa_common.h"
30
#include "common/wpa_ctrl.h"
31
#include "common/ptksa_cache.h"
32
#include "common/nan_de.h"
33
#include "radius/radius.h"
34
#include "radius/radius_client.h"
35
#include "p2p/p2p.h"
36
#include "wps/wps.h"
37
#include "fst/fst.h"
38
#include "hostapd.h"
39
#include "beacon.h"
40
#include "ieee802_11_auth.h"
41
#include "sta_info.h"
42
#include "ieee802_1x.h"
43
#include "wpa_auth.h"
44
#include "pmksa_cache_auth.h"
45
#include "wmm.h"
46
#include "ap_list.h"
47
#include "accounting.h"
48
#include "ap_config.h"
49
#include "ap_mlme.h"
50
#include "p2p_hostapd.h"
51
#include "ap_drv_ops.h"
52
#include "wnm_ap.h"
53
#include "hw_features.h"
54
#include "ieee802_11.h"
55
#include "dfs.h"
56
#include "mbo_ap.h"
57
#include "rrm.h"
58
#include "taxonomy.h"
59
#include "fils_hlp.h"
60
#include "dpp_hostapd.h"
61
#include "gas_query_ap.h"
62
#include "comeback_token.h"
63
#include "nan_usd_ap.h"
64
#include "pasn/pasn_common.h"
65
#include "robust_av.h"
66
67
68
#ifdef CONFIG_FILS
69
static struct wpabuf *
70
prepare_auth_resp_fils(struct hostapd_data *hapd,
71
           struct sta_info *sta, u16 *resp,
72
           struct rsn_pmksa_cache_entry *pmksa,
73
           struct wpabuf *erp_resp,
74
           const u8 *msk, size_t msk_len,
75
           int *is_pub);
76
#endif /* CONFIG_FILS */
77
78
#ifdef CONFIG_PASN
79
#ifdef CONFIG_FILS
80
81
static void pasn_fils_auth_resp(struct hostapd_data *hapd,
82
        struct sta_info *sta, u16 status,
83
        struct wpabuf *erp_resp,
84
        const u8 *msk, size_t msk_len);
85
86
#endif /* CONFIG_FILS */
87
#endif /* CONFIG_PASN */
88
89
static void handle_auth(struct hostapd_data *hapd,
90
      const struct ieee80211_mgmt *mgmt, size_t len,
91
      int rssi, int from_queue);
92
static int add_associated_sta(struct hostapd_data *hapd,
93
            struct sta_info *sta, int reassoc);
94
#ifdef CONFIG_IEEE8021X_AUTH
95
static struct rsn_pmksa_cache_entry *
96
pmksa_cache_search(void *ctx, const u8 *spa, const u8 *pmkid, bool is_ml);
97
#endif /* CONFIG_IEEE8021X_AUTH */
98
99
100
static u8 * hostapd_eid_multi_ap(struct hostapd_data *hapd, u8 *eid, size_t len)
101
0
{
102
0
  struct multi_ap_params multi_ap = { 0 };
103
104
0
  if (!hapd->conf->multi_ap)
105
0
    return eid;
106
107
0
  if (hapd->conf->multi_ap & BACKHAUL_BSS)
108
0
    multi_ap.capability |= MULTI_AP_BACKHAUL_BSS;
109
0
  if (hapd->conf->multi_ap & FRONTHAUL_BSS)
110
0
    multi_ap.capability |= MULTI_AP_FRONTHAUL_BSS;
111
112
0
  if (hapd->conf->multi_ap_client_disallow &
113
0
      PROFILE1_CLIENT_ASSOC_DISALLOW)
114
0
    multi_ap.capability |=
115
0
      MULTI_AP_PROFILE1_BACKHAUL_STA_DISALLOWED;
116
0
  if (hapd->conf->multi_ap_client_disallow &
117
0
      PROFILE2_CLIENT_ASSOC_DISALLOW)
118
0
    multi_ap.capability |=
119
0
      MULTI_AP_PROFILE2_BACKHAUL_STA_DISALLOWED;
120
121
0
  multi_ap.profile = hapd->conf->multi_ap_profile;
122
0
  multi_ap.vlanid = hapd->conf->multi_ap_vlanid;
123
124
0
  return eid + add_multi_ap_ie(eid, len, &multi_ap);
125
0
}
126
127
128
static size_t hostapd_supp_rates(struct hostapd_data *hapd, u8 *buf)
129
0
{
130
0
  u8 *pos = buf;
131
0
  int i;
132
133
0
  if (!hapd->current_rates)
134
0
    return 0;
135
136
0
  for (i = 0; i < hapd->num_rates; i++) {
137
0
    *pos = hapd->current_rates[i].rate / 5;
138
0
    if (hapd->current_rates[i].flags & HOSTAPD_RATE_BASIC)
139
0
      *pos |= 0x80;
140
0
    pos++;
141
0
  }
142
143
0
  if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
144
0
    *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
145
146
0
  if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
147
0
    *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
148
149
0
#ifdef CONFIG_IEEE80211AX
150
0
  if (hapd->iconf->ieee80211ax && hapd->iconf->require_he)
151
0
    *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY;
152
0
#endif /* CONFIG_IEEE80211AX */
153
154
0
#ifdef CONFIG_IEEE80211BE
155
0
  if (hapd->iconf->ieee80211be && !hapd->conf->disable_11be &&
156
0
      (hapd->iconf->require_eht || hapd->conf->bss_require_eht))
157
0
    *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_EHT_PHY;
158
0
#endif /* CONFIG_IEEE80211BE */
159
160
0
#ifdef CONFIG_IEEE80211BN
161
0
  if (hapd->iconf->ieee80211bn && hapd->iconf->require_uhr)
162
0
    *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_UHR_PHY;
163
0
#endif /* CONFIG_IEEE80211BN */
164
165
#ifdef CONFIG_SAE
166
  if ((hapd->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
167
       hostapd_sae_pw_id_in_use(hapd->conf) == 2) &&
168
      hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK &&
169
      wpa_key_mgmt_only_sae(hapd->conf->wpa_key_mgmt))
170
    *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY;
171
#endif /* CONFIG_SAE */
172
173
0
  return pos - buf;
174
0
}
175
176
177
static int get_sta_profile_num(const u8 *sec_prof, size_t len)
178
0
{
179
0
  u8 indication, bitmap_len, num_vendor;
180
0
  const u8 *bitmap;
181
0
  unsigned int i;
182
0
  int num = -1;
183
184
0
  if (!sec_prof || len < 2)
185
0
    return -1;
186
187
  /* Parse Security Profile Indication field (1 octet):
188
   * Bits 0-3: bitmap_len (number of octets in the bitmap)
189
   * Bits 4-7: vendor profile count
190
   */
191
0
  indication = sec_prof[1];
192
0
  bitmap_len = indication & 0x0f;
193
0
  num_vendor = (indication & 0xf0) >> 4;
194
195
0
  if (num_vendor > 0) {
196
0
    wpa_printf(MSG_DEBUG,
197
0
         "STA Security Profile element indicated a not supported vendor profile");
198
0
    return -1;
199
0
  }
200
201
0
  if (len < (size_t) (2 + bitmap_len)) {
202
0
    wpa_printf(MSG_DEBUG,
203
0
         "Truncated Security Profile element bitmap");
204
0
    return -1;
205
0
  }
206
207
0
  if (bitmap_len == 0) {
208
0
    wpa_printf(MSG_DEBUG, "Empty Security Profile element bitmap");
209
0
    return -1;
210
0
  }
211
212
0
  bitmap = &sec_prof[2];
213
214
  /* The Security Profile element from a STA is allowed to have only a
215
   * single profile being indicated. Check that and determine the
216
   * indicated security profile number. Vendor specific security profiles
217
   * are not yet supported. */
218
0
  for (i = 0; i < bitmap_len * 8; i++) {
219
0
    if (bitmap[i / 8] & BIT(i % 8)) {
220
0
      if (num >= 0) {
221
0
        wpa_printf(MSG_DEBUG,
222
0
             "STA Security Profile element indicated more than a single profile");
223
0
        return -1;
224
0
      }
225
226
0
      num = i;
227
0
    }
228
0
  }
229
230
0
  if (num >= 0)
231
0
    wpa_printf(MSG_DEBUG, "STA security profile number: %u", num);
232
0
  else
233
0
    wpa_printf(MSG_DEBUG,
234
0
         "No security profile set in STA Security Profile element bitmap");
235
0
  return num;
236
0
}
237
238
239
static bool hostapd_sec_prof_enabled(const struct hostapd_bss_config *conf,
240
             int num)
241
0
{
242
0
  unsigned int i;
243
244
0
  for (i = 0; conf->security_profiles[i] >= 0; i++) {
245
0
    if (conf->security_profiles[i] == num)
246
0
      return true;
247
0
  }
248
249
0
  return false;
250
0
}
251
252
253
/**
254
 * find_and_validate_profile - Find and validate a matching security profile
255
 * @hapd: AP data
256
 * @addr: STA MAC address (for logging)
257
 * @sta_profile_num: Security profile number from STA's Security Profile element
258
 * @rsne_data: Parsed RSNE data from STA
259
 * @rsnxe: STA's RSNXE data
260
 * @rsnxe_len: STA's RSNXE data len
261
 * @profile_matched: On success, returns the matched profile entry
262
 * Returns: true if a matching security profile is found and validated, false
263
 * otherwise
264
 */
265
static bool find_and_validate_profile(
266
  struct hostapd_data *hapd, const u8 *addr,
267
  int sta_profile_num,
268
  const struct wpa_ie_data *rsne_data,
269
  const u8 *rsnxe, size_t rsnxe_len,
270
  const struct security_profile_entry **profile_matched)
271
0
{
272
0
  const struct security_profile_entry *entry;
273
274
  /* Vaidate that security profile is enabled */
275
0
  if (!hostapd_sec_prof_enabled(hapd->conf, sta_profile_num)) {
276
0
    wpa_printf(MSG_INFO,
277
0
         "Station " MACSTR
278
0
         " security profile number %d not enabled in AP's configuration",
279
0
         MAC2STR(addr), sta_profile_num);
280
0
    return false;
281
0
  }
282
283
  /* Validate that the profile number is known */
284
0
  entry = sec_prof_get(sta_profile_num);
285
0
  if (!entry) {
286
0
    wpa_printf(MSG_INFO, "Unsupported security profile %d",
287
0
         sta_profile_num);
288
0
    return false;
289
0
  }
290
291
  /* Validate RSNE fields */
292
0
  if (!(entry->key_mgmt & rsne_data->key_mgmt)) {
293
0
    wpa_printf(MSG_DEBUG,
294
0
         "Security profile number %d key_mgmt mismatch: required=0x%x got=0x%x",
295
0
         sta_profile_num, entry->key_mgmt,
296
0
         rsne_data->key_mgmt);
297
0
    return false;
298
0
  }
299
300
0
  if (!(entry->pairwise_cipher & rsne_data->pairwise_cipher)) {
301
0
    wpa_printf(MSG_DEBUG,
302
0
         "Securityy profile number %d pairwise cipher mismatch: required=0x%x got=0x%x",
303
0
         sta_profile_num, entry->pairwise_cipher,
304
0
         rsne_data->pairwise_cipher);
305
0
    return false;
306
0
  }
307
308
0
  if (!(rsne_data->capabilities & WPA_CAPABILITY_MFPC)) {
309
0
    wpa_printf(MSG_DEBUG,
310
0
         "RSN: Security profile use without MFP is not allowed");
311
0
    return false;
312
0
  }
313
314
  /* Validate RSNXE fields */
315
0
  if (entry->ieee8021x_auth_frame &&
316
0
      !ieee802_11_rsnx_capab_len(rsnxe, rsnxe_len,
317
0
               WLAN_RSNX_CAPAB_802_1X_IN_AUTH_FRAMES)) {
318
0
    wpa_printf(MSG_DEBUG,
319
0
         "Security profile number %d 8021x_auth_frame capab missing",
320
0
         sta_profile_num);
321
0
    return false;
322
0
  }
323
324
0
  if (entry->assoc_frame_encrypt &&
325
0
      !ieee802_11_rsnx_capab_len(rsnxe, rsnxe_len,
326
0
               WLAN_RSNX_CAPAB_ASSOC_FRAME_ENCRYPTION))
327
0
  {
328
0
    wpa_printf(MSG_DEBUG,
329
0
         "Security profile number %d assoc_frame_encrypt capab missing",
330
0
         sta_profile_num);
331
0
    return false;
332
0
  }
333
334
0
  if (entry->pmksa_caching_privacy &&
335
0
      !ieee802_11_rsnx_capab_len(rsnxe, rsnxe_len,
336
0
               WLAN_RSNX_CAPAB_PMKSA_CACHING_PRIVACY)) {
337
0
    wpa_printf(MSG_DEBUG,
338
0
         "Security profile number %d pmksa_caching_privacy capab missing",
339
0
         sta_profile_num);
340
0
    return false;
341
0
  }
342
343
0
  wpa_printf(MSG_DEBUG,
344
0
       "Station " MACSTR
345
0
       " indicated security profile number %d matches (key_mgmt=0x%x cipher=0x%x)",
346
0
       MAC2STR(addr), sta_profile_num,
347
0
       entry->key_mgmt, entry->pairwise_cipher);
348
0
  if (profile_matched)
349
0
    *profile_matched = entry;
350
0
  return true;
351
0
}
352
353
354
/**
355
 * validate_sta_security_profile - Validate STA's Security Profile element
356
 * @hapd: AP data
357
 * @addr: STA MAC address (for logging)
358
 * @wpa_ie: STA's RSNE (including EID and Length)
359
 * @wpa_ie_len: Length of wpa_ie
360
 * @rsnxe: STA's RSNXE (including EID and Length), or NULL
361
 * @rsnxe_len: Length of rsnxe (unused; rsnxe already includes EID+Len)
362
 * @sec_prof: Body of STA's Security Profile element (after EID_Ext byte)
363
 * @sec_prof_len: Length of sec_prof
364
 * @profile_matched: On success, returns the matched profile entry
365
 * Returns: Whether validation passes
366
 *
367
 * Validates that the STA's indicated security profile matches one of the
368
 * AP's advertised profiles and that the RSNE/RSNXE fields are consistent.
369
 */
370
static bool validate_sta_security_profile(
371
  struct hostapd_data *hapd, const u8 *addr,
372
  const u8 *rsne, size_t rsne_len,
373
  const u8 *rsnxe, size_t rsnxe_len,
374
  const u8 *sec_prof, size_t sec_prof_len,
375
  const struct security_profile_entry **profile_matched)
376
0
{
377
0
  struct wpa_ie_data data;
378
0
  int sta_profile_num;
379
380
0
  if (!hapd->conf->security_profiles)
381
0
    return true; /* No profiles configured, allow */
382
383
0
  wpa_printf(MSG_DEBUG, "Validating security profile for STA " MACSTR,
384
0
       MAC2STR(addr));
385
386
  /* Parse station's RSNE */
387
0
  if (!rsne || rsne_len < 2) {
388
0
    wpa_printf(MSG_INFO, "UHR: Station " MACSTR " missing RSNE",
389
0
         MAC2STR(addr));
390
0
    return false;
391
0
  }
392
393
0
  if (wpa_parse_wpa_ie_rsn(rsne - 2, rsne_len + 2, &data) < 0) {
394
0
    wpa_printf(MSG_INFO, "Station " MACSTR " used an invalid RSNE",
395
0
         MAC2STR(addr));
396
0
    return false;
397
0
  }
398
399
  /* Extract STA's profile number from Security Profile element */
400
0
  sta_profile_num = get_sta_profile_num(sec_prof, sec_prof_len);
401
0
  if (sta_profile_num < 0) {
402
0
    wpa_printf(MSG_INFO,
403
0
         "Station " MACSTR
404
0
         " did not indicate a valid security profile number",
405
0
         MAC2STR(addr));
406
0
    return false;
407
0
  }
408
409
0
  wpa_printf(MSG_DEBUG, "STA " MACSTR " profile number: %d",
410
0
       MAC2STR(addr), sta_profile_num);
411
412
  /* Use the helper function to find and validate the profile */
413
0
  return find_and_validate_profile(hapd, addr, sta_profile_num,
414
0
           &data, rsnxe, rsnxe_len,
415
0
           profile_matched);
416
0
}
417
418
419
/**
420
 * validate_security_profile - Security Profile validation
421
 * @hapd: hostapd BSS data structure
422
 * @sta_info: Station address.
423
 * @elems: Pre-parsed elements (caller is responsible for parsing)
424
 * @auth_context: Authentication context string (e.g., "SAE", "PASN", "802.1X")
425
 * @profile_matched: On success, returns the matched profile entry
426
 * Returns: WLAN_STATUS_SUCCESS on success or validation not needed,
427
 *      WLAN_STATUS_REJECTED_INVALID_SECURITY_PROFILE on validation failure
428
 *
429
 * This helper function consolidates the common Security Profile validation
430
 * logic used across different authentication methods (SAE, PASN, IEEE 802.1X).
431
 */
432
static u16 validate_security_profile(
433
  struct hostapd_data *hapd,
434
  struct sta_info *sta,
435
  struct ieee802_11_elems *elems,
436
  const char *auth_context,
437
  const struct security_profile_entry **profile_matched)
438
0
{
439
0
  u8 *addr;
440
441
0
  addr = sta->addr;
442
443
  /* Skip validation if no Security Profiles are configured */
444
0
  if (!hapd->conf->security_profiles)
445
0
    return WLAN_STATUS_SUCCESS;
446
447
0
  if (!elems->security_profile) {
448
    /* No Security Profile element present - validation not needed
449
     */
450
0
    return WLAN_STATUS_SUCCESS;
451
0
  }
452
453
0
  wpa_printf(MSG_DEBUG,
454
0
       "Validating Security Profile element from " MACSTR
455
0
       " in %s auth (body_len=%u)",
456
0
       MAC2STR(addr), auth_context, elems->security_profile_len);
457
458
  /* Perform validation */
459
0
  if (!validate_sta_security_profile(
460
0
        hapd, addr,
461
0
        elems->rsn_ie, elems->rsn_ie_len,
462
0
        elems->rsnxe, elems->rsnxe_len,
463
0
        elems->security_profile, elems->security_profile_len,
464
0
        profile_matched)) {
465
0
    wpa_printf(MSG_INFO,
466
0
         "Rejecting %s auth from " MACSTR
467
0
         " - Security Profile element mismatch",
468
0
         auth_context, MAC2STR(addr));
469
0
    return WLAN_STATUS_REJECTED_INVALID_SECURITY_PROFILE;
470
0
  }
471
472
0
  wpa_printf(MSG_DEBUG,
473
0
       "Security Profile element validated for " MACSTR
474
0
       " in %s auth",
475
0
       MAC2STR(addr), auth_context);
476
477
#ifdef CONFIG_PASN
478
  sta->sec_prof_used = true;
479
#endif /* CONFIG_PASN */
480
481
0
  return WLAN_STATUS_SUCCESS;
482
0
}
483
484
485
u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
486
0
{
487
0
  u8 *pos = eid;
488
0
  u8 buf[100];
489
0
  size_t len;
490
491
0
  len = hostapd_supp_rates(hapd, buf);
492
0
  if (len == 0)
493
0
    return eid;
494
  /* Only up to first eight values in this element */
495
0
  if (len > 8)
496
0
    len = 8;
497
498
0
  *pos++ = WLAN_EID_SUPP_RATES;
499
0
  *pos++ = len;
500
0
  os_memcpy(pos, buf, len);
501
0
  pos += len;
502
503
0
  return pos;
504
0
}
505
506
507
u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
508
0
{
509
0
  u8 *pos = eid;
510
0
  u8 buf[100];
511
0
  size_t len;
512
513
0
  len = hostapd_supp_rates(hapd, buf);
514
  /* Starting from the 9th value for this element */
515
0
  if (len <= 8)
516
0
    return eid;
517
518
0
  *pos++ = WLAN_EID_EXT_SUPP_RATES;
519
0
  *pos++ = len - 8;
520
0
  os_memcpy(pos, &buf[8], len - 8);
521
0
  pos += len - 8;
522
523
0
  return pos;
524
0
}
525
526
527
u8 * hostapd_eid_rm_enabled_capab(struct hostapd_data *hapd, u8 *eid,
528
          size_t len)
529
0
{
530
0
  size_t i;
531
532
0
  for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
533
0
    if (hapd->conf->radio_measurements[i])
534
0
      break;
535
0
  }
536
537
0
  if (i == RRM_CAPABILITIES_IE_LEN || len < 2 + RRM_CAPABILITIES_IE_LEN)
538
0
    return eid;
539
540
0
  *eid++ = WLAN_EID_RRM_ENABLED_CAPABILITIES;
541
0
  *eid++ = RRM_CAPABILITIES_IE_LEN;
542
0
  os_memcpy(eid, hapd->conf->radio_measurements, RRM_CAPABILITIES_IE_LEN);
543
544
0
  return eid + RRM_CAPABILITIES_IE_LEN;
545
0
}
546
547
548
u16 hostapd_own_capab_info(struct hostapd_data *hapd)
549
0
{
550
0
  int capab = WLAN_CAPABILITY_ESS;
551
0
  int privacy = 0;
552
0
  int dfs;
553
0
  int i;
554
555
  /* Check if any of configured channels require DFS */
556
0
  dfs = hostapd_is_dfs_required(hapd->iface);
557
0
  if (dfs < 0) {
558
0
    wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
559
0
         dfs);
560
0
    dfs = 0;
561
0
  }
562
563
0
  if (hapd->iface->num_sta_no_short_preamble == 0 &&
564
0
      hapd->iconf->preamble == SHORT_PREAMBLE)
565
0
    capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
566
567
#ifdef CONFIG_WEP
568
  privacy = hapd->conf->ssid.wep.keys_set;
569
570
  if (hapd->conf->ieee802_1x &&
571
      (hapd->conf->default_wep_key_len ||
572
       hapd->conf->individual_wep_key_len))
573
    privacy = 1;
574
#endif /* CONFIG_WEP */
575
576
0
  if (hapd->conf->wpa)
577
0
    privacy = 1;
578
579
0
  if (privacy)
580
0
    capab |= WLAN_CAPABILITY_PRIVACY;
581
582
0
  if (hapd->iface->current_mode &&
583
0
      hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
584
0
      hapd->iface->num_sta_no_short_slot_time == 0)
585
0
    capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
586
587
  /*
588
   * Currently, Spectrum Management capability bit is set when directly
589
   * requested in configuration by spectrum_mgmt_required or when AP is
590
   * running on DFS channel.
591
   * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
592
   */
593
0
  if (hapd->iface->current_mode &&
594
0
      hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
595
0
      (hapd->iconf->spectrum_mgmt_required || dfs))
596
0
    capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
597
598
0
  for (i = 0; i < RRM_CAPABILITIES_IE_LEN; i++) {
599
0
    if (hapd->conf->radio_measurements[i]) {
600
0
      capab |= IEEE80211_CAP_RRM;
601
0
      break;
602
0
    }
603
0
  }
604
605
0
  return capab;
606
0
}
607
608
609
#ifdef CONFIG_WEP
610
#ifndef CONFIG_NO_RC4
611
static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
612
         u16 auth_transaction, const u8 *challenge,
613
         int iswep)
614
{
615
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
616
           HOSTAPD_LEVEL_DEBUG,
617
           "authentication (shared key, transaction %d)",
618
           auth_transaction);
619
620
  if (auth_transaction == 1) {
621
    if (!sta->challenge) {
622
      /* Generate a pseudo-random challenge */
623
      u8 key[8];
624
625
      sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
626
      if (sta->challenge == NULL)
627
        return WLAN_STATUS_UNSPECIFIED_FAILURE;
628
629
      if (os_get_random(key, sizeof(key)) < 0) {
630
        os_free(sta->challenge);
631
        sta->challenge = NULL;
632
        return WLAN_STATUS_UNSPECIFIED_FAILURE;
633
      }
634
635
      rc4_skip(key, sizeof(key), 0,
636
         sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
637
    }
638
    return 0;
639
  }
640
641
  if (auth_transaction != 3)
642
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
643
644
  /* Transaction 3 */
645
  if (!iswep || !sta->challenge || !challenge ||
646
      os_memcmp_const(sta->challenge, challenge,
647
          WLAN_AUTH_CHALLENGE_LEN)) {
648
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
649
             HOSTAPD_LEVEL_INFO,
650
             "shared key authentication - invalid "
651
             "challenge-response");
652
    return WLAN_STATUS_CHALLENGE_FAIL;
653
  }
654
655
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
656
           HOSTAPD_LEVEL_DEBUG,
657
           "authentication OK (shared key)");
658
  sta->flags |= WLAN_STA_AUTH;
659
  wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
660
  os_free(sta->challenge);
661
  sta->challenge = NULL;
662
663
  return 0;
664
}
665
#endif /* CONFIG_NO_RC4 */
666
#endif /* CONFIG_WEP */
667
668
669
static int send_auth_reply(struct hostapd_data *hapd, struct sta_info *sta,
670
         const u8 *dst,
671
         u16 auth_alg, u16 auth_transaction, u16 resp,
672
         const u8 *ies, size_t ies_len, const char *dbg)
673
0
{
674
0
  struct ieee80211_mgmt *reply;
675
0
  u8 *buf;
676
0
  size_t rlen;
677
0
  int reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
678
0
  const u8 *sa = hapd->own_addr;
679
0
  struct wpabuf *ml_resp = NULL;
680
0
  size_t ml_resp_len = 0;
681
#ifdef CONFIG_IEEE8021X_AUTH
682
  size_t mic_len = 0;
683
#endif /* CONFIG_IEEE8021X_AUTH */
684
685
0
#ifdef CONFIG_IEEE80211BE
686
0
  if (ap_sta_is_mld(hapd, sta)) {
687
0
    ml_resp = hostapd_ml_auth_resp(hapd);
688
0
    if (!ml_resp)
689
0
      return -1;
690
0
    ml_resp_len = wpabuf_len(ml_resp);
691
0
  }
692
0
#endif /* CONFIG_IEEE80211BE */
693
694
0
  rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len + ml_resp_len;
695
#ifdef CONFIG_IEEE8021X_AUTH
696
  /* Add MIC element for an Authentication frame carrying an EAP-Success
697
   * message and for an Authentication frame with transaction sequence
698
   * frame 2, if PMKSA caching was used.
699
   */
700
  if (auth_alg == WLAN_AUTH_802_1X &&
701
      (resp == WLAN_STATUS_802_1_X_AUTH_SUCCESS ||
702
       (sta && sta->eap_auth_data.add_mic))) {
703
    mic_len = wpa_mic_len(sta->eap_auth_data.akm,
704
              sta->eap_auth_data.pmk_len,
705
              RSN_HASH_NOT_SPECIFIED);
706
    rlen += 2 + mic_len;
707
  }
708
#endif /* CONFIG_IEEE8021X_AUTH */
709
710
0
  buf = os_zalloc(rlen);
711
0
  if (!buf) {
712
0
    wpabuf_free(ml_resp);
713
0
    return -1;
714
0
  }
715
716
0
  reply = (struct ieee80211_mgmt *) buf;
717
0
  reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
718
0
              WLAN_FC_STYPE_AUTH);
719
0
  os_memcpy(reply->da, dst, ETH_ALEN);
720
0
  os_memcpy(reply->sa, sa, ETH_ALEN);
721
0
  os_memcpy(reply->bssid, sa, ETH_ALEN);
722
723
0
  reply->u.auth.auth_alg = host_to_le16(auth_alg);
724
0
  reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
725
0
  reply->u.auth.status_code = host_to_le16(resp);
726
727
0
  if (ies && ies_len)
728
0
    os_memcpy(reply->u.auth.variable, ies, ies_len);
729
730
0
#ifdef CONFIG_IEEE80211BE
731
0
  if (ml_resp)
732
0
    os_memcpy(reply->u.auth.variable + ies_len,
733
0
        wpabuf_head(ml_resp), wpabuf_len(ml_resp));
734
735
0
  wpabuf_free(ml_resp);
736
0
#endif /* CONFIG_IEEE80211BE */
737
738
0
  wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
739
0
       " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu) (dbg=%s)",
740
0
       MAC2STR(dst), auth_alg, auth_transaction,
741
0
       resp, (unsigned long) ies_len, dbg);
742
#ifdef CONFIG_TESTING_OPTIONS
743
#ifdef CONFIG_SAE
744
  if (hapd->conf->sae_confirm_immediate == 2 &&
745
      auth_alg == WLAN_AUTH_SAE) {
746
    if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT && sta &&
747
        (resp == WLAN_STATUS_SUCCESS ||
748
         resp == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
749
         resp == WLAN_STATUS_SAE_PK)) {
750
      wpa_printf(MSG_DEBUG,
751
           "TESTING: Postpone SAE Commit transmission until Confirm is ready");
752
      os_free(sta->sae_postponed_commit);
753
      sta->sae_postponed_commit = buf;
754
      sta->sae_postponed_commit_len = rlen;
755
      return WLAN_STATUS_SUCCESS;
756
    }
757
758
    if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM &&
759
        sta && sta->sae_postponed_commit) {
760
      wpa_printf(MSG_DEBUG,
761
           "TESTING: Send postponed SAE Commit first, immediately followed by SAE Confirm");
762
      if (hostapd_drv_send_mlme(hapd,
763
              sta->sae_postponed_commit,
764
              sta->sae_postponed_commit_len,
765
              0, NULL, 0, 0) < 0)
766
        wpa_printf(MSG_INFO, "send_auth_reply: send failed");
767
      os_free(sta->sae_postponed_commit);
768
      sta->sae_postponed_commit = NULL;
769
      sta->sae_postponed_commit_len = 0;
770
    }
771
  }
772
#endif /* CONFIG_SAE */
773
#endif /* CONFIG_TESTING_OPTIONS */
774
775
#ifdef CONFIG_IEEE8021X_AUTH
776
  if (auth_alg == WLAN_AUTH_802_1X &&
777
      (resp == WLAN_STATUS_802_1_X_AUTH_SUCCESS ||
778
       (sta && sta->eap_auth_data.add_mic))) {
779
    const u8 *frame, *data, *rsne, *rsnxe;
780
    u8 data_buf[500], mic[WPA_1X_MAX_MIC_LEN];
781
    size_t frame_len, data_len;
782
    const u8 *aa = sa;
783
    u8 *ptr = reply->u.auth.variable + ies_len + ml_resp_len;
784
785
#ifdef CONFIG_IEEE80211BE
786
    if (ap_sta_is_mld(hapd, sta))
787
      aa = hapd->mld->mld_addr;
788
#endif /* CONFIG_IEEE80211BE */
789
790
    rsne = hostapd_wpa_ie(hapd, WLAN_EID_RSN);
791
    if (!rsne) {
792
      wpa_printf(MSG_INFO, "No AP RSNE");
793
      return -1;
794
    }
795
    os_memcpy(data_buf, rsne, 2 + rsne[1]);
796
    data_len = 2 + rsne[1];
797
798
    rsnxe = hostapd_wpa_ie(hapd, WLAN_EID_RSNX);
799
    if (rsnxe) {
800
      wpa_printf(MSG_DEBUG, "Found AP RSNXE");
801
      os_memcpy(&data_buf[data_len], rsnxe, 2 + rsnxe[1]);
802
      data_len += 2 + rsnxe[1];
803
    } else {
804
      wpa_printf(MSG_DEBUG, "No AP RSNXE");
805
    }
806
    data = data_buf;
807
808
    /* MIC element */
809
    ptr[0] = WLAN_EID_MIC;
810
    ptr[1] = mic_len;
811
    os_memset(ptr + 2, 0, mic_len);
812
813
    frame = (const u8 *) &reply->u.auth.auth_alg;
814
    frame_len =  rlen - IEEE80211_HDRLEN;
815
    if (wpa_auth_8021x_mic(sta->eap_auth_data.akm,
816
               sta->eap_auth_data.ptk.kck,
817
               sta->eap_auth_data.ptk.kck_len,
818
               aa, sta->addr, data, data_len,
819
               frame, frame_len, mic)) {
820
      wpa_printf(MSG_INFO, "Failed to derive MIC");
821
      return -1;
822
    }
823
    os_memcpy(ptr + 2, mic, mic_len);
824
  }
825
#endif /* CONFIF_IEEE8021X_AUTH */
826
827
0
  if (hostapd_drv_send_mlme(hapd, reply, rlen, 0, NULL, 0, 0) < 0)
828
0
    wpa_printf(MSG_INFO, "send_auth_reply: send failed");
829
0
  else
830
0
    reply_res = WLAN_STATUS_SUCCESS;
831
832
0
  os_free(buf);
833
834
0
  return reply_res;
835
0
}
836
837
838
#ifdef CONFIG_IEEE8021X_AUTH
839
static void send_8021x_auth_reply(struct hostapd_data *hapd,
840
          struct sta_info *sta,
841
          u16 auth_transaction, u16 resp,
842
          struct wpabuf *ies)
843
{
844
  send_auth_reply(hapd, sta, sta->addr, WLAN_AUTH_802_1X,
845
      auth_transaction, resp, wpabuf_head(ies),
846
      wpabuf_len(ies), "send-8021x-auth-reply");
847
848
  if (sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS &&
849
           resp != WLAN_STATUS_802_1_X_AUTH_SUCCESS)) {
850
    hostapd_drv_sta_remove(hapd, sta->addr);
851
    sta->added_unassoc = 0;
852
  }
853
  wpabuf_free(ies);
854
}
855
#endif /* IEEE8021X_AUTH */
856
857
858
#ifdef CONFIG_IEEE80211R_AP
859
static void handle_auth_ft_finish(void *ctx, const u8 *dst,
860
          u16 auth_transaction, u16 status,
861
          const u8 *ies, size_t ies_len)
862
0
{
863
0
  struct hostapd_data *hapd = ctx;
864
0
  struct sta_info *sta;
865
0
  int reply_res;
866
867
0
  reply_res = send_auth_reply(hapd, NULL, dst, WLAN_AUTH_FT,
868
0
            auth_transaction, status, ies, ies_len,
869
0
            "auth-ft-finish");
870
871
0
  sta = ap_get_sta(hapd, dst);
872
0
  if (sta == NULL)
873
0
    return;
874
875
0
  if (sta->added_unassoc && (reply_res != WLAN_STATUS_SUCCESS ||
876
0
           status != WLAN_STATUS_SUCCESS)) {
877
0
    hostapd_drv_sta_remove(hapd, sta->addr);
878
0
    sta->added_unassoc = 0;
879
0
    return;
880
0
  }
881
882
0
  if (status != WLAN_STATUS_SUCCESS)
883
0
    return;
884
885
0
  hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
886
0
           HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
887
0
  sta->flags |= WLAN_STA_AUTH;
888
0
  mlme_authenticate_indication(hapd, sta);
889
0
}
890
#endif /* CONFIG_IEEE80211R_AP */
891
892
893
#ifdef CONFIG_SAE
894
895
static void sae_set_state(struct sta_info *sta, enum sae_state state,
896
        const char *reason)
897
{
898
  wpa_printf(MSG_DEBUG, "SAE: State %s -> %s for peer " MACSTR " (%s)",
899
       sae_state_txt(sta->sae->state), sae_state_txt(state),
900
       MAC2STR(sta->addr), reason);
901
  sta->sae->state = state;
902
}
903
904
905
static bool in_mac_addr_list(const u8 *list, unsigned int num, const u8 *addr)
906
{
907
  unsigned int i;
908
909
  for (i = 0; list && i < num; i++) {
910
    if (ether_addr_equal(&list[i * ETH_ALEN], addr))
911
      return true;
912
  }
913
914
  return false;
915
}
916
917
918
static struct sae_password_entry *
919
sae_password_find_pw(struct hostapd_data *hapd, struct sta_info *sta)
920
{
921
  struct sae_password_entry *pw = NULL;
922
923
  if (!sta->sae || !sta->sae->tmp || !sta->sae->tmp->used_pw)
924
    return NULL;
925
926
927
  for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
928
    if (pw == sta->sae->tmp->used_pw)
929
      return pw;
930
  }
931
932
  return NULL;
933
}
934
935
936
static bool is_other_sae_password(struct hostapd_data *hapd,
937
          struct sta_info *sta,
938
          struct sae_password_entry *used_pw)
939
{
940
  struct sae_password_entry *pw;
941
942
  for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
943
    if (pw == used_pw ||
944
        pw->identifier ||
945
        !is_broadcast_ether_addr(pw->peer_addr))
946
      continue;
947
948
    if (in_mac_addr_list(pw->success_mac,
949
             pw->num_success_mac,
950
             sta->addr))
951
      return true;
952
953
    if (!in_mac_addr_list(pw->fail_mac, pw->num_fail_mac,
954
              sta->addr))
955
      return true;
956
  }
957
958
  return false;
959
}
960
961
962
static bool has_sae_success_seen(struct hostapd_data *hapd,
963
         struct sta_info *sta)
964
{
965
  struct sae_password_entry *pw;
966
967
  for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
968
    if (pw->identifier ||
969
        !is_broadcast_ether_addr(pw->peer_addr))
970
      continue;
971
972
    if (in_mac_addr_list(pw->success_mac,
973
             pw->num_success_mac,
974
             sta->addr))
975
      return true;
976
  }
977
978
  return false;
979
}
980
981
982
static int sae_password_mark_success(struct hostapd_data *hapd,
983
              struct sae_password_entry *pw,
984
              const u8 *addr)
985
{
986
  if (in_mac_addr_list(pw->success_mac, pw->num_success_mac, addr))
987
    return 0;
988
989
  if (!pw->success_mac) {
990
    pw->success_mac = os_zalloc(hapd->conf->sae_track_password *
991
              ETH_ALEN);
992
    if (!pw->success_mac)
993
      return -1;
994
    pw->num_success_mac = hapd->conf->sae_track_password;
995
  }
996
997
  os_memcpy(&pw->success_mac[pw->next_success_mac * ETH_ALEN], addr,
998
      ETH_ALEN);
999
  pw->next_success_mac = (pw->next_success_mac + 1) % pw->num_success_mac;
1000
  return 0;
1001
}
1002
1003
1004
static void sae_password_track_success(struct hostapd_data *hapd,
1005
               struct sta_info *sta)
1006
{
1007
  struct sae_password_entry *pw;
1008
1009
  if (!hapd->conf->sae_track_password)
1010
    return;
1011
1012
  pw = sae_password_find_pw(hapd, sta);
1013
  if (!pw)
1014
    return;
1015
1016
  sae_password_mark_success(hapd, pw, sta->addr);
1017
}
1018
1019
1020
static bool sae_password_track_fail(struct hostapd_data *hapd,
1021
            struct sta_info *sta)
1022
{
1023
  struct sae_password_entry *pw;
1024
1025
  if (!hapd->conf->sae_track_password)
1026
    return false;
1027
1028
  pw = sae_password_find_pw(hapd, sta);
1029
  if (!pw)
1030
    return false;
1031
1032
  if (in_mac_addr_list(pw->fail_mac,
1033
           pw->num_fail_mac,
1034
           sta->addr))
1035
    return is_other_sae_password(hapd, sta, pw);
1036
1037
  if (!pw->fail_mac) {
1038
    pw->fail_mac = os_zalloc(hapd->conf->sae_track_password *
1039
           ETH_ALEN);
1040
    if (!pw->fail_mac)
1041
      return false;
1042
    pw->num_fail_mac = hapd->conf->sae_track_password;
1043
  }
1044
1045
  os_memcpy(&pw->fail_mac[pw->next_fail_mac * ETH_ALEN], sta->addr,
1046
      ETH_ALEN);
1047
  pw->next_fail_mac = (pw->next_fail_mac + 1) % pw->num_fail_mac;
1048
1049
  return is_other_sae_password(hapd, sta, pw);
1050
}
1051
1052
1053
int sae_password_bind(struct hostapd_data *hapd, const u8 *addr,
1054
          const char *password)
1055
{
1056
  struct sae_password_entry *pw;
1057
1058
  if (!hapd->conf->sae_track_password)
1059
    return -1;
1060
1061
  for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
1062
    if (pw->identifier ||
1063
        !is_broadcast_ether_addr(pw->peer_addr) ||
1064
        os_strcmp(password, pw->password) != 0)
1065
      continue;
1066
1067
    return sae_password_mark_success(hapd, pw, addr);
1068
  }
1069
1070
  return -1;
1071
}
1072
1073
1074
const char * sae_get_password(struct hostapd_data *hapd,
1075
            struct sta_info *sta,
1076
            const u8 *rx_id, size_t rx_id_len,
1077
            struct sae_password_entry **pw_entry,
1078
            struct sae_pt **s_pt,
1079
            const struct sae_pk **s_pk)
1080
{
1081
  const char *password = NULL;
1082
  struct sae_password_entry *pw;
1083
  struct sae_pt *pt = NULL;
1084
  const struct sae_pk *pk = NULL;
1085
  struct hostapd_sta_wpa_psk_short *psk = NULL;
1086
1087
  /* With sae_track_password functionality enabled, try to first find the
1088
   * next viable wildcard-address password if a password identifier was
1089
   * not used. Select an wildcard-addr entry if the STA is known to have
1090
   * used it successfully before. If no such entry exists, pick a
1091
   * wildcard-addr entry that does not have a failed entry tracked for the
1092
   * STA. */
1093
  if (!rx_id && sta && hapd->conf->sae_track_password) {
1094
    struct sae_password_entry *success = NULL, *no_fail = NULL;
1095
1096
    for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
1097
      if (pw->identifier ||
1098
          !is_broadcast_ether_addr(pw->peer_addr))
1099
        continue;
1100
      if (in_mac_addr_list(pw->success_mac,
1101
               pw->num_success_mac,
1102
               sta->addr)) {
1103
        success = pw;
1104
        break;
1105
      }
1106
1107
      if (!no_fail &&
1108
          !in_mac_addr_list(pw->fail_mac, pw->num_fail_mac,
1109
                sta->addr))
1110
        no_fail = pw;
1111
    }
1112
1113
    pw = success ? success : no_fail;
1114
    if (pw) {
1115
      password = pw->password;
1116
      pt = pw->pt;
1117
      if (!(hapd->conf->mesh & MESH_ENABLED))
1118
        pk = pw->pk;
1119
      goto found;
1120
    }
1121
  }
1122
1123
  /* If sae_track_password functionality is not enabled or no suitable
1124
   * password entry was found with it, pick the first entry that matches
1125
   * the STA MAC address and password identifier (if used). */
1126
  for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
1127
    if (!is_broadcast_ether_addr(pw->peer_addr) &&
1128
        (!sta ||
1129
         !ether_addr_equal(pw->peer_addr, sta->addr)))
1130
      continue;
1131
    if ((rx_id && !pw->identifier) || (!rx_id && pw->identifier))
1132
      continue;
1133
    if (rx_id && pw->identifier &&
1134
        (rx_id_len != os_strlen(pw->identifier) ||
1135
         os_memcmp(rx_id, pw->identifier, rx_id_len) != 0))
1136
      continue;
1137
    password = pw->password;
1138
    pt = pw->pt;
1139
    if (!(hapd->conf->mesh & MESH_ENABLED))
1140
      pk = pw->pk;
1141
    break;
1142
  }
1143
  if (!password && !rx_id && !hapd->conf->sae_password_psk) {
1144
    password = hapd->conf->ssid.wpa_passphrase;
1145
    pt = hapd->conf->ssid.pt;
1146
  }
1147
1148
  if (!password && sta && !rx_id) {
1149
    for (psk = sta->psk; psk; psk = psk->next) {
1150
      if (psk->is_passphrase) {
1151
        password = psk->passphrase;
1152
        break;
1153
      }
1154
    }
1155
  }
1156
1157
  /* Try to decrypt the received password identifier if no plaintext
1158
   * identifier match was found. */
1159
  if (!password && rx_id && rx_id_len > 4 + 4 + AES_BLOCK_SIZE &&
1160
      hapd->conf->sae_pw_id_key) {
1161
    u8 *plain, *pos, *counter;
1162
    size_t plain_len;
1163
    const u8 *id;
1164
    size_t id_len;
1165
1166
    plain = os_malloc(rx_id_len);
1167
    if (!plain)
1168
      goto fail;
1169
    if (aes_siv_decrypt(
1170
          wpabuf_head(hapd->conf->sae_pw_id_key),
1171
          wpabuf_len(hapd->conf->sae_pw_id_key),
1172
          rx_id, rx_id_len, 0, NULL, NULL, plain) < 0)
1173
      goto fail;
1174
    plain_len = rx_id_len - AES_BLOCK_SIZE;
1175
    wpa_hexdump_ascii(MSG_DEBUG,
1176
          "SAE: Decrypted password identifier info",
1177
          plain, plain_len);
1178
    /* 4 octet date | Password ID | <padding> | 4 octet counter */
1179
    counter = plain + plain_len - 4;
1180
    wpa_printf(MSG_DEBUG, "SAE: Generation time %u counter %u",
1181
         WPA_GET_BE32(plain), WPA_GET_BE32(counter));
1182
    id = pos = plain + 4;
1183
    while (pos < counter) {
1184
      if (*pos == 0x00)
1185
        break;
1186
      pos++;
1187
    }
1188
    id_len = pos - id;
1189
    wpa_hexdump_ascii(MSG_DEBUG,
1190
          "SAE: Decrypted password identifier",
1191
          id, id_len);
1192
    for (pw = hapd->conf->sae_passwords; pw; pw = pw->next) {
1193
      if (!is_broadcast_ether_addr(pw->peer_addr) &&
1194
          (!sta ||
1195
           !ether_addr_equal(pw->peer_addr, sta->addr)))
1196
        continue;
1197
      if (!pw->identifier ||
1198
          os_strlen(pw->identifier) != id_len ||
1199
          os_memcmp(id, pw->identifier, id_len) != 0)
1200
        continue;
1201
      password = pw->password;
1202
      if (!(hapd->conf->mesh & MESH_ENABLED))
1203
        pk = pw->pk;
1204
      if (sta && sta->sae && sta->sae->tmp) {
1205
        os_free(sta->sae->tmp->dec_pw_id);
1206
        sta->sae->tmp->dec_pw_id =
1207
          os_zalloc(id_len + 1);
1208
        if (sta->sae->tmp->dec_pw_id) {
1209
          os_memcpy(sta->sae->tmp->dec_pw_id,
1210
              id, id_len);
1211
          sta->sae->tmp->dec_pw_id_len = id_len;
1212
          sta->sae->tmp->pw_id_counter =
1213
            WPA_GET_BE32(counter);
1214
          wpa_printf(MSG_DEBUG,
1215
               "SAE: Bound decrypted password identifier to STA");
1216
        }
1217
      }
1218
      break;
1219
    }
1220
  fail:
1221
    os_free(plain);
1222
  }
1223
1224
found:
1225
  if (pw_entry)
1226
    *pw_entry = pw;
1227
  if (s_pt)
1228
    *s_pt = pt;
1229
  if (s_pk)
1230
    *s_pk = pk;
1231
1232
  return password;
1233
}
1234
1235
1236
static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
1237
               struct sta_info *sta, int update,
1238
               int status_code)
1239
{
1240
  struct wpabuf *buf;
1241
  const char *password = NULL;
1242
  struct sae_password_entry *pw;
1243
  const u8 *rx_id = NULL;
1244
  size_t rx_id_len = 0;
1245
  int use_pt = 0;
1246
  struct sae_pt *pt = NULL;
1247
  const struct sae_pk *pk = NULL;
1248
  const u8 *own_addr = hapd->own_addr;
1249
1250
#ifdef CONFIG_IEEE80211BE
1251
  if (ap_sta_is_mld(hapd, sta))
1252
    own_addr = hapd->mld->mld_addr;
1253
#endif /* CONFIG_IEEE80211BE */
1254
1255
  if (sta->sae->tmp) {
1256
    rx_id = sta->sae->tmp->parsed_pw_id ?
1257
      sta->sae->tmp->parsed_pw_id : sta->sae->tmp->pw_id;
1258
    rx_id_len = sta->sae->tmp->parsed_pw_id ?
1259
      sta->sae->tmp->parsed_pw_id_len :
1260
      sta->sae->tmp->pw_id_len;
1261
    use_pt = sta->sae->h2e;
1262
#ifdef CONFIG_SAE_PK
1263
    os_memcpy(sta->sae->tmp->own_addr, own_addr, ETH_ALEN);
1264
    os_memcpy(sta->sae->tmp->peer_addr, sta->addr, ETH_ALEN);
1265
#endif /* CONFIG_SAE_PK */
1266
  }
1267
1268
  if (rx_id && hapd->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1269
    use_pt = 1;
1270
  else if (status_code == WLAN_STATUS_SUCCESS)
1271
    use_pt = 0;
1272
  else if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1273
     status_code == WLAN_STATUS_SAE_PK)
1274
    use_pt = 1;
1275
1276
  password = sae_get_password(hapd, sta, rx_id, rx_id_len, &pw, &pt, &pk);
1277
  if (!password) {
1278
    wpa_printf(MSG_DEBUG, "SAE: No password available");
1279
    return NULL;
1280
  }
1281
1282
  if (use_pt) {
1283
    struct sae_pt *tmp_pt = NULL;
1284
    bool failed = false;
1285
1286
    if (!pt && pw) {
1287
      int groups[2] = { sta->sae->group, 0 };
1288
1289
      wpa_printf(MSG_DEBUG,
1290
           "SAE: Derive PT for encrypted PW ID");
1291
      tmp_pt = sae_derive_pt(groups, hapd->conf->ssid.ssid,
1292
                 hapd->conf->ssid.ssid_len,
1293
                 (const u8 *) pw->password,
1294
                 os_strlen(pw->password),
1295
                 rx_id, rx_id_len);
1296
      if (!tmp_pt) {
1297
        wpa_printf(MSG_DEBUG,
1298
             "SAE: Could not derive PT");
1299
        return NULL;
1300
      }
1301
      pt = tmp_pt;
1302
      update = 1;
1303
    }
1304
1305
    if (!pt) {
1306
      wpa_printf(MSG_DEBUG, "SAE: No PT available");
1307
      return NULL;
1308
    }
1309
1310
    if (update &&
1311
        sae_prepare_commit_pt(sta->sae, pt, own_addr, sta->addr,
1312
            NULL, pk) < 0)
1313
      failed = true;
1314
1315
    sae_deinit_pt(tmp_pt);
1316
    if (failed)
1317
      return NULL;
1318
  }
1319
1320
  if (update && !use_pt &&
1321
      sae_prepare_commit(own_addr, sta->addr,
1322
             (u8 *) password, os_strlen(password),
1323
             sta->sae) < 0) {
1324
    wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
1325
    return NULL;
1326
  }
1327
1328
  if (pw && sta->sae->tmp)
1329
    sta->sae->tmp->used_pw = pw;
1330
1331
  if (pw && pw->vlan_id) {
1332
    if (!sta->sae->tmp) {
1333
      wpa_printf(MSG_INFO,
1334
           "SAE: No temporary data allocated - cannot store VLAN ID");
1335
      return NULL;
1336
    }
1337
    sta->sae->tmp->vlan_id = pw->vlan_id;
1338
  }
1339
1340
  buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN +
1341
         (rx_id ? 3 + rx_id_len : 0));
1342
  if (buf &&
1343
      sae_write_commit(sta->sae, buf, sta->sae->tmp ?
1344
           sta->sae->tmp->anti_clogging_token : NULL,
1345
           rx_id, rx_id_len) < 0) {
1346
    wpabuf_free(buf);
1347
    buf = NULL;
1348
  }
1349
1350
  return buf;
1351
}
1352
1353
1354
static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
1355
                struct sta_info *sta)
1356
{
1357
  struct wpabuf *buf;
1358
1359
  buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
1360
  if (buf == NULL)
1361
    return NULL;
1362
1363
#ifdef CONFIG_SAE_PK
1364
#ifdef CONFIG_TESTING_OPTIONS
1365
  if (sta->sae->tmp)
1366
    sta->sae->tmp->omit_pk_elem = hapd->conf->sae_pk_omit;
1367
#endif /* CONFIG_TESTING_OPTIONS */
1368
#endif /* CONFIG_SAE_PK */
1369
1370
  if (sae_write_confirm(sta->sae, buf) < 0) {
1371
    wpabuf_free(buf);
1372
    return NULL;
1373
  }
1374
1375
  return buf;
1376
}
1377
1378
1379
static int auth_sae_send_commit(struct hostapd_data *hapd,
1380
        struct sta_info *sta,
1381
        int update, int status_code)
1382
{
1383
  struct wpabuf *data;
1384
  int reply_res;
1385
  u16 status;
1386
1387
  data = auth_build_sae_commit(hapd, sta, update, status_code);
1388
  if (!data && sta->sae->tmp &&
1389
      (sta->sae->tmp->pw_id || sta->sae->tmp->parsed_pw_id))
1390
    return WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER;
1391
  if (data == NULL)
1392
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
1393
1394
  if (sta->sae->tmp && sta->sae->pk)
1395
    status = WLAN_STATUS_SAE_PK;
1396
  else if (sta->sae->tmp && sta->sae->h2e)
1397
    status = WLAN_STATUS_SAE_HASH_TO_ELEMENT;
1398
  else
1399
    status = WLAN_STATUS_SUCCESS;
1400
#ifdef CONFIG_TESTING_OPTIONS
1401
  if (hapd->conf->sae_commit_status >= 0 &&
1402
      hapd->conf->sae_commit_status != status) {
1403
    wpa_printf(MSG_INFO,
1404
         "TESTING: Override SAE commit status code %u --> %d",
1405
         status, hapd->conf->sae_commit_status);
1406
    status = hapd->conf->sae_commit_status;
1407
  }
1408
#endif /* CONFIG_TESTING_OPTIONS */
1409
  reply_res = send_auth_reply(hapd, sta, sta->addr,
1410
            WLAN_AUTH_SAE, 1,
1411
            status, wpabuf_head(data),
1412
            wpabuf_len(data), "sae-send-commit");
1413
1414
  wpabuf_free(data);
1415
1416
  return reply_res;
1417
}
1418
1419
1420
static int auth_sae_send_confirm(struct hostapd_data *hapd,
1421
         struct sta_info *sta)
1422
{
1423
  struct wpabuf *data;
1424
  int reply_res;
1425
1426
  data = auth_build_sae_confirm(hapd, sta);
1427
  if (data == NULL)
1428
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
1429
1430
  reply_res = send_auth_reply(hapd, sta, sta->addr,
1431
            WLAN_AUTH_SAE, 2,
1432
            WLAN_STATUS_SUCCESS, wpabuf_head(data),
1433
            wpabuf_len(data), "sae-send-confirm");
1434
1435
  wpabuf_free(data);
1436
1437
  return reply_res;
1438
}
1439
1440
#endif /* CONFIG_SAE */
1441
1442
1443
#if defined(CONFIG_SAE) || defined(CONFIG_PASN)
1444
1445
static int use_anti_clogging(struct hostapd_data *hapd)
1446
{
1447
  struct sta_info *sta;
1448
  unsigned int open = 0;
1449
1450
  if (hapd->conf->anti_clogging_threshold == 0)
1451
    return 1;
1452
1453
  for (sta = hapd->sta_list; sta; sta = sta->next) {
1454
#ifdef CONFIG_SAE
1455
    if (sta->sae &&
1456
        (sta->sae->state == SAE_COMMITTED ||
1457
         sta->sae->state == SAE_CONFIRMED))
1458
      open++;
1459
#endif /* CONFIG_SAE */
1460
#ifdef CONFIG_PASN
1461
    if (sta->pasn && sta->pasn->ecdh)
1462
      open++;
1463
#endif /* CONFIG_PASN */
1464
    if (open >= hapd->conf->anti_clogging_threshold)
1465
      return 1;
1466
  }
1467
1468
#ifdef CONFIG_SAE
1469
  /* In addition to already existing open SAE sessions, check whether
1470
   * there are enough pending commit messages in the processing queue to
1471
   * potentially result in too many open sessions. */
1472
  if (open + dl_list_len(&hapd->sae_commit_queue) >=
1473
      hapd->conf->anti_clogging_threshold)
1474
    return 1;
1475
#endif /* CONFIG_SAE */
1476
1477
  return 0;
1478
}
1479
1480
#endif /* defined(CONFIG_SAE) || defined(CONFIG_PASN) */
1481
1482
1483
#ifdef CONFIG_SAE
1484
1485
static int sae_check_big_sync(struct hostapd_data *hapd, struct sta_info *sta)
1486
{
1487
  if (sta->sae->sync > hapd->conf->sae_sync) {
1488
    sae_set_state(sta, SAE_NOTHING, "Sync > dot11RSNASAESync");
1489
    sta->sae->sync = 0;
1490
    if (sta->sae->tmp) {
1491
      /* Disable this SAE instance for 10 seconds to avoid
1492
       * unnecessary flood of multiple SAE commits in
1493
       * unexpected mesh cases. */
1494
      if (os_get_reltime(&sta->sae->tmp->disabled_until) == 0)
1495
        sta->sae->tmp->disabled_until.sec += 10;
1496
    }
1497
    return -1;
1498
  }
1499
  return 0;
1500
}
1501
1502
1503
static bool sae_proto_instance_disabled(struct sta_info *sta)
1504
{
1505
  struct sae_temporary_data *tmp;
1506
1507
  if (!sta->sae)
1508
    return false;
1509
  tmp = sta->sae->tmp;
1510
  if (!tmp)
1511
    return false;
1512
1513
  if (os_reltime_initialized(&tmp->disabled_until)) {
1514
    struct os_reltime now;
1515
1516
    os_get_reltime(&now);
1517
    if (os_reltime_before(&now, &tmp->disabled_until))
1518
      return true;
1519
  }
1520
1521
  return false;
1522
}
1523
1524
1525
static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
1526
{
1527
  struct hostapd_data *hapd = eloop_ctx;
1528
  struct sta_info *sta = eloop_data;
1529
  int ret;
1530
1531
  if (sae_check_big_sync(hapd, sta))
1532
    return;
1533
  sta->sae->sync++;
1534
  wpa_printf(MSG_DEBUG, "SAE: Auth SAE retransmit timer for " MACSTR
1535
       " (sync=%d state=%s)",
1536
       MAC2STR(sta->addr), sta->sae->sync,
1537
       sae_state_txt(sta->sae->state));
1538
1539
  switch (sta->sae->state) {
1540
  case SAE_COMMITTED:
1541
    ret = auth_sae_send_commit(hapd, sta, 0, -1);
1542
    eloop_register_timeout(0,
1543
               hapd->dot11RSNASAERetransPeriod * 1000,
1544
               auth_sae_retransmit_timer, hapd, sta);
1545
    break;
1546
  case SAE_CONFIRMED:
1547
    ret = auth_sae_send_confirm(hapd, sta);
1548
    eloop_register_timeout(0,
1549
               hapd->dot11RSNASAERetransPeriod * 1000,
1550
               auth_sae_retransmit_timer, hapd, sta);
1551
    break;
1552
  default:
1553
    ret = -1;
1554
    break;
1555
  }
1556
1557
  if (ret != WLAN_STATUS_SUCCESS)
1558
    wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
1559
}
1560
1561
1562
void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
1563
{
1564
  eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
1565
}
1566
1567
1568
static void sae_set_retransmit_timer(struct hostapd_data *hapd,
1569
             struct sta_info *sta)
1570
{
1571
  if (!(hapd->conf->mesh & MESH_ENABLED))
1572
    return;
1573
1574
  eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
1575
  eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
1576
             auth_sae_retransmit_timer, hapd, sta);
1577
}
1578
1579
1580
static void sae_sme_send_external_auth_status(struct hostapd_data *hapd,
1581
                struct sta_info *sta, u16 status)
1582
{
1583
  struct external_auth params;
1584
1585
  os_memset(&params, 0, sizeof(params));
1586
  params.status = status;
1587
1588
#ifdef CONFIG_IEEE80211BE
1589
  if (ap_sta_is_mld(hapd, sta))
1590
    params.bssid =
1591
      sta->mld_info.links[sta->mld_assoc_link_id].peer_addr;
1592
#endif /* CONFIG_IEEE80211BE */
1593
  if (!params.bssid)
1594
    params.bssid = sta->addr;
1595
1596
  if (status == WLAN_STATUS_SUCCESS && sta->sae &&
1597
      !hapd->conf->disable_pmksa_caching)
1598
    params.pmkid = sta->sae->pmkid;
1599
1600
  hostapd_drv_send_external_auth_status(hapd, &params);
1601
}
1602
1603
1604
static int sae_assign_vlan(struct hostapd_data *hapd, struct sta_info *sta,
1605
         int vlan_id)
1606
{
1607
#ifndef CONFIG_NO_VLAN
1608
  struct vlan_description vlan_desc;
1609
1610
  if (vlan_id > 0) {
1611
    wpa_printf(MSG_DEBUG, "SAE: Assign STA " MACSTR
1612
         " to VLAN ID %d",
1613
         MAC2STR(sta->addr), vlan_id);
1614
1615
    if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
1616
      os_memset(&vlan_desc, 0, sizeof(vlan_desc));
1617
      vlan_desc.notempty = 1;
1618
      vlan_desc.untagged = vlan_id;
1619
      if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
1620
        wpa_printf(MSG_INFO,
1621
             "Invalid VLAN ID %d in sae_password",
1622
             vlan_id);
1623
        return -1;
1624
      }
1625
1626
      if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0 ||
1627
          ap_sta_bind_vlan(hapd, sta) < 0) {
1628
        wpa_printf(MSG_INFO,
1629
             "Failed to assign VLAN ID %d from sae_password to "
1630
             MACSTR, vlan_id,
1631
             MAC2STR(sta->addr));
1632
        return -1;
1633
      }
1634
    } else {
1635
      sta->vlan_id = vlan_id;
1636
    }
1637
  }
1638
#endif /* CONFIG_NO_VLAN */
1639
1640
  return 0;
1641
}
1642
1643
1644
void sae_accept_sta(struct hostapd_data *hapd, struct sta_info *sta)
1645
{
1646
  if (sta->sae->tmp &&
1647
      sae_assign_vlan(hapd, sta, sta->sae->tmp->vlan_id) < 0)
1648
    return;
1649
1650
  sta->flags |= WLAN_STA_AUTH;
1651
  sta->auth_alg = WLAN_AUTH_SAE;
1652
  mlme_authenticate_indication(hapd, sta);
1653
  wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1654
  sae_set_state(sta, SAE_ACCEPTED, "Accept Confirm");
1655
  crypto_bignum_deinit(sta->sae->peer_commit_scalar_accepted, 0);
1656
  sta->sae->peer_commit_scalar_accepted = sta->sae->peer_commit_scalar;
1657
  sta->sae->peer_commit_scalar = NULL;
1658
  wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
1659
             sta->sae->pmk, sta->sae->pmk_len,
1660
             sta->sae->pmkid, sta->sae->akmp,
1661
             ap_sta_is_mld(hapd, sta), sta->vlan_id);
1662
  sae_sme_send_external_auth_status(hapd, sta, WLAN_STATUS_SUCCESS);
1663
  if (sta->sae->tmp) {
1664
    struct sae_temporary_data *tmp = sta->sae->tmp;
1665
1666
    wpabuf_free(sta->sae_pw_id);
1667
    sta->sae_pw_id = NULL;
1668
    if (tmp->dec_pw_id) {
1669
      sta->sae_pw_id = wpabuf_alloc_copy(
1670
        tmp->dec_pw_id, tmp->dec_pw_id_len);
1671
      sta->sae_pw_id_counter = tmp->pw_id_counter;
1672
    } else if (tmp->pw_id) {
1673
      sta->sae_pw_id = wpabuf_alloc_copy(
1674
        tmp->pw_id, tmp->pw_id_len);
1675
    }
1676
  }
1677
}
1678
1679
1680
static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
1681
           u16 auth_transaction, u16 status_code,
1682
           int allow_reuse, int *sta_removed)
1683
{
1684
  int ret;
1685
1686
  *sta_removed = 0;
1687
1688
  if (auth_transaction != WLAN_AUTH_TR_SEQ_SAE_COMMIT &&
1689
      auth_transaction != WLAN_AUTH_TR_SEQ_SAE_CONFIRM)
1690
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
1691
1692
  wpa_printf(MSG_DEBUG, "SAE: Peer " MACSTR " state=%s auth_trans=%u",
1693
       MAC2STR(sta->addr), sae_state_txt(sta->sae->state),
1694
       auth_transaction);
1695
1696
  if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT &&
1697
      sae_proto_instance_disabled(sta)) {
1698
    wpa_printf(MSG_DEBUG,
1699
         "SAE: Protocol instance temporarily disabled - discard received SAE commit");
1700
    return WLAN_STATUS_SUCCESS;
1701
  }
1702
1703
  switch (sta->sae->state) {
1704
  case SAE_NOTHING:
1705
    if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1706
      struct sae_temporary_data *tmp = sta->sae->tmp;
1707
      bool immediate_confirm;
1708
1709
      if (tmp) {
1710
        sta->sae->h2e =
1711
          (status_code ==
1712
           WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1713
           status_code == WLAN_STATUS_SAE_PK);
1714
        sta->sae->pk =
1715
          status_code == WLAN_STATUS_SAE_PK;
1716
      }
1717
      ret = auth_sae_send_commit(hapd, sta,
1718
               !allow_reuse, status_code);
1719
      if (ret == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER)
1720
        wpa_msg(hapd->msg_ctx, MSG_INFO,
1721
          WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
1722
          MACSTR, MAC2STR(sta->addr));
1723
      if (ret)
1724
        return ret;
1725
1726
      if (tmp && tmp->parsed_pw_id && !tmp->pw_id) {
1727
        tmp->pw_id = tmp->parsed_pw_id;
1728
        tmp->pw_id_len = tmp->parsed_pw_id_len;
1729
        tmp->parsed_pw_id = NULL;
1730
        tmp->parsed_pw_id_len = 0;
1731
        wpa_hexdump_ascii(MSG_DEBUG,
1732
              "SAE: Known Password Identifier bound to this STA",
1733
              tmp->pw_id, tmp->pw_id_len);
1734
      }
1735
1736
      sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
1737
1738
      if (sae_process_commit(sta->sae) < 0)
1739
        return WLAN_STATUS_UNSPECIFIED_FAILURE;
1740
1741
      /*
1742
       * In mesh case, both Commit and Confirm are sent
1743
       * immediately. In infrastructure BSS, by default, only
1744
       * a single Authentication frame (Commit) is expected
1745
       * from the AP here and the second one (Confirm) will
1746
       * be sent once the STA has sent its second
1747
       * Authentication frame (Confirm). This behavior can be
1748
       * overridden with explicit configuration so that the
1749
       * infrastructure BSS case sends both frames together.
1750
       */
1751
      immediate_confirm = (hapd->conf->mesh & MESH_ENABLED) ||
1752
        hapd->conf->sae_confirm_immediate;
1753
1754
      /* If sae_track_password is enabled and the STA has not
1755
       * yet been tracked to having successfully completed
1756
       * SAE authentication with the password that the AP
1757
       * tries to use, do not send Confirm immediately to
1758
       * avoid an explicit indication on the STA side on
1759
       * password mismatch. */
1760
      if (immediate_confirm &&
1761
          hapd->conf->sae_track_password &&
1762
          (!sta->sae->tmp || !sta->sae->tmp->parsed_pw_id) &&
1763
          !has_sae_success_seen(hapd, sta))
1764
        immediate_confirm = false;
1765
1766
      if (immediate_confirm) {
1767
        /*
1768
         * Send both Commit and Confirm immediately
1769
         * based on SAE finite state machine
1770
         * Nothing -> Confirm transition.
1771
         */
1772
        ret = auth_sae_send_confirm(hapd, sta);
1773
        if (ret)
1774
          return ret;
1775
        sae_set_state(sta, SAE_CONFIRMED,
1776
                "Sent Confirm (mesh)");
1777
      } else {
1778
        /*
1779
         * For infrastructure BSS, send only the Commit
1780
         * message now to get alternating sequence of
1781
         * Authentication frames between the AP and STA.
1782
         * Confirm will be sent in
1783
         * Committed -> Confirmed/Accepted transition
1784
         * when receiving Confirm from STA.
1785
         */
1786
      }
1787
      sta->sae->sync = 0;
1788
      sae_set_retransmit_timer(hapd, sta);
1789
    } else {
1790
      hostapd_logger(hapd, sta->addr,
1791
               HOSTAPD_MODULE_IEEE80211,
1792
               HOSTAPD_LEVEL_DEBUG,
1793
               "SAE confirm before commit");
1794
    }
1795
    break;
1796
  case SAE_COMMITTED:
1797
    sae_clear_retransmit_timer(hapd, sta);
1798
    if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1799
      if (sae_process_commit(sta->sae) < 0)
1800
        return WLAN_STATUS_UNSPECIFIED_FAILURE;
1801
1802
      ret = auth_sae_send_confirm(hapd, sta);
1803
      if (ret)
1804
        return ret;
1805
      sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
1806
      sta->sae->sync = 0;
1807
      sae_set_retransmit_timer(hapd, sta);
1808
    } else if (hapd->conf->mesh & MESH_ENABLED) {
1809
      /*
1810
       * In mesh case, follow SAE finite state machine and
1811
       * send Commit now, if sync count allows.
1812
       */
1813
      if (sae_check_big_sync(hapd, sta))
1814
        return WLAN_STATUS_SUCCESS;
1815
      sta->sae->sync++;
1816
1817
      ret = auth_sae_send_commit(hapd, sta, 0, status_code);
1818
      if (ret)
1819
        return ret;
1820
1821
      sae_set_retransmit_timer(hapd, sta);
1822
    } else {
1823
      /*
1824
       * For instructure BSS, send the postponed Confirm from
1825
       * Nothing -> Confirmed transition that was reduced to
1826
       * Nothing -> Committed above.
1827
       */
1828
      ret = auth_sae_send_confirm(hapd, sta);
1829
      if (ret)
1830
        return ret;
1831
1832
      sae_set_state(sta, SAE_CONFIRMED, "Sent Confirm");
1833
1834
      /*
1835
       * Since this was triggered on Confirm RX, run another
1836
       * step to get to Accepted without waiting for
1837
       * additional events.
1838
       */
1839
      return sae_sm_step(hapd, sta, auth_transaction,
1840
             WLAN_STATUS_SUCCESS, 0, sta_removed);
1841
    }
1842
    break;
1843
  case SAE_CONFIRMED:
1844
    sae_clear_retransmit_timer(hapd, sta);
1845
    if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1846
      if (sae_check_big_sync(hapd, sta))
1847
        return WLAN_STATUS_SUCCESS;
1848
      sta->sae->sync++;
1849
1850
      ret = auth_sae_send_commit(hapd, sta, 1, status_code);
1851
      if (ret)
1852
        return ret;
1853
1854
      if (sae_process_commit(sta->sae) < 0)
1855
        return WLAN_STATUS_UNSPECIFIED_FAILURE;
1856
1857
      ret = auth_sae_send_confirm(hapd, sta);
1858
      if (ret)
1859
        return ret;
1860
1861
      sae_set_retransmit_timer(hapd, sta);
1862
    } else {
1863
      sta->sae->send_confirm = 0xffff;
1864
      sae_accept_sta(hapd, sta);
1865
    }
1866
    break;
1867
  case SAE_ACCEPTED:
1868
    if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT &&
1869
        (hapd->conf->mesh & MESH_ENABLED)) {
1870
      wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
1871
           ") doing reauthentication",
1872
           MAC2STR(sta->addr));
1873
      wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
1874
      ap_free_sta(hapd, sta);
1875
      *sta_removed = 1;
1876
    } else if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
1877
      wpa_printf(MSG_DEBUG, "SAE: Start reauthentication");
1878
      ret = auth_sae_send_commit(hapd, sta, 1, status_code);
1879
      if (ret)
1880
        return ret;
1881
      sae_set_state(sta, SAE_COMMITTED, "Sent Commit");
1882
1883
      if (sae_process_commit(sta->sae) < 0)
1884
        return WLAN_STATUS_UNSPECIFIED_FAILURE;
1885
      sta->sae->sync = 0;
1886
      sae_set_retransmit_timer(hapd, sta);
1887
    } else {
1888
      if (sae_check_big_sync(hapd, sta))
1889
        return WLAN_STATUS_SUCCESS;
1890
      sta->sae->sync++;
1891
1892
      ret = auth_sae_send_confirm(hapd, sta);
1893
      sae_clear_temp_data(sta->sae);
1894
      if (ret)
1895
        return ret;
1896
    }
1897
    break;
1898
  default:
1899
    wpa_printf(MSG_ERROR, "SAE: invalid state %d",
1900
         sta->sae->state);
1901
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
1902
  }
1903
  return WLAN_STATUS_SUCCESS;
1904
}
1905
1906
1907
static void sae_pick_next_group(struct hostapd_data *hapd, struct sta_info *sta)
1908
{
1909
  struct sae_data *sae = sta->sae;
1910
  struct hostapd_bss_config *conf = hapd->conf;
1911
  int i, *groups = conf->sae_groups;
1912
  int default_groups[] = { 19, 0, 0 };
1913
1914
  if (sae->state != SAE_COMMITTED)
1915
    return;
1916
1917
  wpa_printf(MSG_DEBUG, "SAE: Previously selected group: %d", sae->group);
1918
1919
  if (!groups) {
1920
    groups = default_groups;
1921
    if (hostapd_config_sae_ext_key(conf))
1922
      default_groups[1] = 20;
1923
  }
1924
1925
  for (i = 0; groups[i] > 0; i++) {
1926
    if (sae->group == groups[i])
1927
      break;
1928
  }
1929
1930
  if (groups[i] <= 0) {
1931
    wpa_printf(MSG_DEBUG,
1932
         "SAE: Previously selected group not found from the current configuration");
1933
    return;
1934
  }
1935
1936
  for (;;) {
1937
    i++;
1938
    if (groups[i] <= 0) {
1939
      wpa_printf(MSG_DEBUG,
1940
           "SAE: No alternative group enabled");
1941
      return;
1942
    }
1943
1944
    if (sae_set_group(sae, groups[i]) < 0)
1945
      continue;
1946
1947
    break;
1948
  }
1949
  wpa_printf(MSG_DEBUG, "SAE: Selected new group: %d", groups[i]);
1950
}
1951
1952
1953
static int sae_status_success(struct hostapd_data *hapd, u16 status_code)
1954
{
1955
  enum sae_pwe sae_pwe = hapd->conf->sae_pwe;
1956
  int id_in_use;
1957
  bool sae_pk = false;
1958
1959
  id_in_use = hostapd_sae_pw_id_in_use(hapd->conf);
1960
  if (id_in_use == 2 && sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK)
1961
    sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
1962
  else if (id_in_use == 1 && sae_pwe == SAE_PWE_HUNT_AND_PECK)
1963
    sae_pwe = SAE_PWE_BOTH;
1964
#ifdef CONFIG_SAE_PK
1965
  sae_pk = hostapd_sae_pk_in_use(hapd->conf);
1966
  if (sae_pwe == SAE_PWE_HUNT_AND_PECK && sae_pk)
1967
    sae_pwe = SAE_PWE_BOTH;
1968
#endif /* CONFIG_SAE_PK */
1969
  if (sae_pwe == SAE_PWE_HUNT_AND_PECK &&
1970
      (hapd->conf->wpa_key_mgmt &
1971
       (WPA_KEY_MGMT_SAE_EXT_KEY | WPA_KEY_MGMT_FT_SAE_EXT_KEY)))
1972
    sae_pwe = SAE_PWE_BOTH;
1973
1974
  return ((sae_pwe == SAE_PWE_HUNT_AND_PECK ||
1975
     sae_pwe == SAE_PWE_FORCE_HUNT_AND_PECK) &&
1976
    status_code == WLAN_STATUS_SUCCESS) ||
1977
    (sae_pwe == SAE_PWE_HASH_TO_ELEMENT &&
1978
     (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1979
      (sae_pk && status_code == WLAN_STATUS_SAE_PK))) ||
1980
    (sae_pwe == SAE_PWE_BOTH &&
1981
     (status_code == WLAN_STATUS_SUCCESS ||
1982
      status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
1983
      (sae_pk && status_code == WLAN_STATUS_SAE_PK)));
1984
}
1985
1986
1987
static int sae_is_group_enabled(struct hostapd_data *hapd, int group)
1988
{
1989
  struct hostapd_bss_config *conf = hapd->conf;
1990
  int *groups = conf->sae_groups;
1991
  int default_groups[] = { 19, 0, 0 };
1992
  int i;
1993
1994
  if (!groups) {
1995
    groups = default_groups;
1996
    if (hostapd_config_sae_ext_key(conf))
1997
      default_groups[1] = 20;
1998
  }
1999
2000
  for (i = 0; groups[i] > 0; i++) {
2001
    if (groups[i] == group)
2002
      return 1;
2003
  }
2004
2005
  return 0;
2006
}
2007
2008
2009
static int check_sae_rejected_groups(struct hostapd_data *hapd,
2010
             struct sae_data *sae)
2011
{
2012
  const struct wpabuf *groups;
2013
  size_t i, count, len;
2014
  const u8 *pos;
2015
2016
  if (!sae->tmp)
2017
    return 0;
2018
  groups = sae->tmp->peer_rejected_groups;
2019
  if (!groups)
2020
    return 0;
2021
2022
  pos = wpabuf_head(groups);
2023
  len = wpabuf_len(groups);
2024
  if (len & 1) {
2025
    wpa_printf(MSG_DEBUG,
2026
         "SAE: Invalid length of the Rejected Groups element payload: %zu",
2027
         len);
2028
    return 1;
2029
  }
2030
2031
  count = len / 2;
2032
  for (i = 0; i < count; i++) {
2033
    int enabled;
2034
    u16 group;
2035
2036
    group = WPA_GET_LE16(pos);
2037
    pos += 2;
2038
    enabled = sae_is_group_enabled(hapd, group);
2039
    wpa_printf(MSG_DEBUG, "SAE: Rejected group %u is %s",
2040
         group, enabled ? "enabled" : "disabled");
2041
    if (enabled)
2042
      return 1;
2043
  }
2044
2045
  return 0;
2046
}
2047
2048
2049
static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
2050
          const struct ieee80211_mgmt *mgmt, size_t len,
2051
          u16 auth_transaction, u16 status_code)
2052
{
2053
  int resp = WLAN_STATUS_SUCCESS;
2054
  struct wpabuf *data = NULL;
2055
  struct hostapd_bss_config *conf = hapd->conf;
2056
  int *groups = conf->sae_groups;
2057
  int default_groups[] = { 19, 0, 0 };
2058
  const u8 *pos, *end;
2059
  int sta_removed = 0;
2060
  bool success_status;
2061
2062
  if (!groups) {
2063
    groups = default_groups;
2064
    if (hostapd_config_sae_ext_key(conf))
2065
      default_groups[1] = 20;
2066
  }
2067
2068
#ifdef CONFIG_TESTING_OPTIONS
2069
  if (hapd->conf->sae_reflection_attack &&
2070
      auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
2071
    wpa_printf(MSG_DEBUG, "SAE: TESTING - reflection attack");
2072
    pos = mgmt->u.auth.variable;
2073
    end = ((const u8 *) mgmt) + len;
2074
    resp = status_code;
2075
    send_auth_reply(hapd, sta, sta->addr,
2076
        WLAN_AUTH_SAE,
2077
        auth_transaction, resp, pos, end - pos,
2078
        "auth-sae-reflection-attack");
2079
    goto remove_sta;
2080
  }
2081
2082
  if (hapd->conf->sae_commit_override &&
2083
      auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
2084
    wpa_printf(MSG_DEBUG, "SAE: TESTING - commit override");
2085
    send_auth_reply(hapd, sta, sta->addr,
2086
        WLAN_AUTH_SAE,
2087
        auth_transaction, resp,
2088
        wpabuf_head(hapd->conf->sae_commit_override),
2089
        wpabuf_len(hapd->conf->sae_commit_override),
2090
        "sae-commit-override");
2091
    goto remove_sta;
2092
  }
2093
#endif /* CONFIG_TESTING_OPTIONS */
2094
  if (!sta->sae) {
2095
    if (auth_transaction != WLAN_AUTH_TR_SEQ_SAE_COMMIT ||
2096
        !sae_status_success(hapd, status_code)) {
2097
      wpa_printf(MSG_DEBUG, "SAE: Unexpected Status Code %u",
2098
           status_code);
2099
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2100
      goto reply;
2101
    }
2102
    sta->sae = os_zalloc(sizeof(*sta->sae));
2103
    if (!sta->sae) {
2104
      resp = -1;
2105
      goto remove_sta;
2106
    }
2107
    if (!hostapd_sae_pw_id_in_use(hapd->conf))
2108
      sta->sae->no_pw_id = 1;
2109
    sae_set_state(sta, SAE_NOTHING, "Init");
2110
    sta->sae->sync = 0;
2111
  }
2112
2113
  if (sta->mesh_sae_pmksa_caching) {
2114
    wpa_printf(MSG_DEBUG,
2115
         "SAE: Cancel use of mesh PMKSA caching because peer starts SAE authentication");
2116
    wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
2117
    sta->mesh_sae_pmksa_caching = 0;
2118
  }
2119
2120
  if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT) {
2121
    const u8 *token = NULL;
2122
    size_t token_len = 0;
2123
    int allow_reuse = 0;
2124
    int ie_offset = 0;
2125
2126
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2127
             HOSTAPD_LEVEL_DEBUG,
2128
             "start SAE authentication (RX commit, status=%u (%s))",
2129
             status_code, status2str(status_code));
2130
2131
    if ((hapd->conf->mesh & MESH_ENABLED) &&
2132
        status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
2133
        sta->sae->tmp) {
2134
      pos = mgmt->u.auth.variable;
2135
      end = ((const u8 *) mgmt) + len;
2136
      if (pos + sizeof(le16) > end) {
2137
        wpa_printf(MSG_ERROR,
2138
             "SAE: Too short anti-clogging token request");
2139
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2140
        goto reply;
2141
      }
2142
      resp = sae_group_allowed(sta->sae, groups,
2143
             WPA_GET_LE16(pos));
2144
      if (resp != WLAN_STATUS_SUCCESS) {
2145
        wpa_printf(MSG_ERROR,
2146
             "SAE: Invalid group in anti-clogging token request");
2147
        goto reply;
2148
      }
2149
      pos += sizeof(le16);
2150
2151
      wpabuf_free(sta->sae->tmp->anti_clogging_token);
2152
      sta->sae->tmp->anti_clogging_token =
2153
        wpabuf_alloc_copy(pos, end - pos);
2154
      if (sta->sae->tmp->anti_clogging_token == NULL) {
2155
        wpa_printf(MSG_ERROR,
2156
             "SAE: Failed to alloc for anti-clogging token");
2157
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2158
        goto remove_sta;
2159
      }
2160
2161
      /*
2162
       * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
2163
       * is 76, a new Commit Message shall be constructed
2164
       * with the Anti-Clogging Token from the received
2165
       * Authentication frame, and the commit-scalar and
2166
       * COMMIT-ELEMENT previously sent.
2167
       */
2168
      resp = auth_sae_send_commit(hapd, sta, 0, status_code);
2169
      if (resp != WLAN_STATUS_SUCCESS) {
2170
        wpa_printf(MSG_ERROR,
2171
             "SAE: Failed to send commit message");
2172
        goto remove_sta;
2173
      }
2174
      sae_set_state(sta, SAE_COMMITTED,
2175
              "Sent Commit (anti-clogging token case in mesh)");
2176
      sta->sae->sync = 0;
2177
      sae_set_retransmit_timer(hapd, sta);
2178
      return;
2179
    }
2180
2181
    if ((hapd->conf->mesh & MESH_ENABLED) &&
2182
        status_code ==
2183
        WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
2184
        sta->sae->tmp) {
2185
      wpa_printf(MSG_DEBUG,
2186
           "SAE: Peer did not accept our SAE group");
2187
      sae_pick_next_group(hapd, sta);
2188
      goto remove_sta;
2189
    }
2190
2191
    if (!sae_status_success(hapd, status_code))
2192
      goto remove_sta;
2193
2194
    if (sae_proto_instance_disabled(sta)) {
2195
      wpa_printf(MSG_DEBUG,
2196
           "SAE: Protocol instance temporarily disabled - discard received SAE commit");
2197
      return;
2198
    }
2199
2200
    if (!(hapd->conf->mesh & MESH_ENABLED) &&
2201
        sta->sae->state == SAE_COMMITTED) {
2202
      /* This is needed in the infrastructure BSS case to
2203
       * address a sequence where a STA entry may remain in
2204
       * hostapd across two attempts to do SAE authentication
2205
       * by the same STA. The second attempt may end up trying
2206
       * to use a different group and that would not be
2207
       * allowed if we remain in Committed state with the
2208
       * previously set parameters. */
2209
      pos = mgmt->u.auth.variable;
2210
      end = ((const u8 *) mgmt) + len;
2211
      if ((!sta->sae->tmp ||
2212
           !sta->sae->tmp->try_other_password) &&
2213
          end - pos >= (int) sizeof(le16) &&
2214
          sae_group_allowed(sta->sae, groups,
2215
                WPA_GET_LE16(pos)) ==
2216
          WLAN_STATUS_SUCCESS) {
2217
        /* Do not waste resources deriving the same PWE
2218
         * again since the same group is reused. */
2219
        sae_set_state(sta, SAE_NOTHING,
2220
                "Allow previous PWE to be reused");
2221
        allow_reuse = 1;
2222
      } else {
2223
        sae_set_state(sta, SAE_NOTHING,
2224
                "Clear existing state to allow restart");
2225
        sae_clear_data(sta->sae);
2226
      }
2227
    }
2228
2229
    resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
2230
          ((const u8 *) mgmt) + len -
2231
          mgmt->u.auth.variable, &token,
2232
          &token_len, groups, status_code ==
2233
          WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
2234
          status_code == WLAN_STATUS_SAE_PK,
2235
          &ie_offset);
2236
    if (resp == SAE_SILENTLY_DISCARD) {
2237
      wpa_printf(MSG_DEBUG,
2238
           "SAE: Drop commit message from " MACSTR " due to reflection attack",
2239
           MAC2STR(sta->addr));
2240
      goto remove_sta;
2241
    }
2242
2243
    if (resp == WLAN_STATUS_UNKNOWN_PASSWORD_IDENTIFIER) {
2244
      wpa_msg(hapd->msg_ctx, MSG_INFO,
2245
        WPA_EVENT_SAE_UNKNOWN_PASSWORD_IDENTIFIER
2246
        MACSTR, MAC2STR(sta->addr));
2247
      sae_clear_retransmit_timer(hapd, sta);
2248
      sae_set_state(sta, SAE_NOTHING,
2249
              "Unknown Password Identifier");
2250
      if (sta->sae->state == SAE_NOTHING)
2251
        goto reply;
2252
      goto remove_sta;
2253
    }
2254
2255
    if (token &&
2256
        check_comeback_token(hapd->comeback_key,
2257
           hapd->comeback_pending_idx, sta->addr,
2258
           token, token_len)
2259
        < 0) {
2260
      wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
2261
           "incorrect token from " MACSTR,
2262
           MAC2STR(sta->addr));
2263
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2264
      goto remove_sta;
2265
    }
2266
2267
    if (resp != WLAN_STATUS_SUCCESS)
2268
      goto reply;
2269
2270
    if (check_sae_rejected_groups(hapd, sta->sae)) {
2271
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2272
      goto reply;
2273
    }
2274
2275
    if (hapd->conf->security_profiles) {
2276
      struct ieee802_11_elems elems;
2277
      const u8 *ies;
2278
      size_t ies_len;
2279
2280
      ies = mgmt->u.auth.variable + ie_offset;
2281
      ies_len = ((const u8 *) mgmt) + len -
2282
        mgmt->u.auth.variable - ie_offset;
2283
      if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) ==
2284
          ParseFailed) {
2285
        wpa_printf(MSG_DEBUG,
2286
             "Failed to parse elements");
2287
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2288
        goto reply;
2289
      }
2290
      resp = validate_security_profile(
2291
        hapd, sta, &elems, "SAE", NULL);
2292
      if (resp != WLAN_STATUS_SUCCESS)
2293
        goto reply;
2294
    }
2295
2296
    if (!token && use_anti_clogging(hapd) && !allow_reuse) {
2297
      int h2e = 0;
2298
2299
      wpa_printf(MSG_DEBUG,
2300
           "SAE: Request anti-clogging token from "
2301
           MACSTR, MAC2STR(sta->addr));
2302
      if (sta->sae->tmp)
2303
        h2e = sta->sae->h2e;
2304
      if (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
2305
          status_code == WLAN_STATUS_SAE_PK)
2306
        h2e = 1;
2307
      data = auth_build_token_req(
2308
        &hapd->last_comeback_key_update,
2309
        hapd->comeback_key,
2310
        hapd->comeback_idx,
2311
        hapd->comeback_pending_idx,
2312
        sizeof(hapd->comeback_pending_idx),
2313
        sta->sae->group,
2314
        sta->addr, h2e);
2315
      resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
2316
      if (hapd->conf->mesh & MESH_ENABLED)
2317
        sae_set_state(sta, SAE_NOTHING,
2318
                "Request anti-clogging token case in mesh");
2319
      goto reply;
2320
    }
2321
2322
    resp = sae_sm_step(hapd, sta, auth_transaction,
2323
           status_code, allow_reuse, &sta_removed);
2324
  } else if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM) {
2325
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2326
             HOSTAPD_LEVEL_DEBUG,
2327
             "SAE authentication (RX confirm, status=%u (%s))",
2328
             status_code, status2str(status_code));
2329
    if (status_code != WLAN_STATUS_SUCCESS)
2330
      goto remove_sta;
2331
    if (sta->sae->state >= SAE_CONFIRMED ||
2332
        !(hapd->conf->mesh & MESH_ENABLED)) {
2333
      const u8 *var;
2334
      size_t var_len;
2335
      u16 peer_send_confirm;
2336
2337
      var = mgmt->u.auth.variable;
2338
      var_len = ((u8 *) mgmt) + len - mgmt->u.auth.variable;
2339
      if (var_len < 2) {
2340
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2341
        goto reply;
2342
      }
2343
2344
      peer_send_confirm = WPA_GET_LE16(var);
2345
2346
      if (sta->sae->state == SAE_ACCEPTED &&
2347
          (peer_send_confirm <= sta->sae->rc ||
2348
           peer_send_confirm == 0xffff)) {
2349
        wpa_printf(MSG_DEBUG,
2350
             "SAE: Silently ignore unexpected Confirm from peer "
2351
             MACSTR
2352
             " (peer-send-confirm=%u Rc=%u)",
2353
             MAC2STR(sta->addr),
2354
             peer_send_confirm, sta->sae->rc);
2355
        return;
2356
      }
2357
2358
      if (sae_check_confirm(sta->sae, var, var_len,
2359
                NULL) < 0) {
2360
        if (sae_password_track_fail(hapd, sta)) {
2361
          wpa_printf(MSG_DEBUG,
2362
               "SAE: Reject mismatching Confirm so that another password can be attempted by "
2363
               MACSTR,
2364
               MAC2STR(sta->addr));
2365
          if (sta->sae->tmp)
2366
            sta->sae->tmp->
2367
              try_other_password = 1;
2368
          resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
2369
          goto reply;
2370
        }
2371
        resp = WLAN_STATUS_CHALLENGE_FAIL;
2372
        goto reply;
2373
      }
2374
      sae_password_track_success(hapd, sta);
2375
      sta->sae->rc = peer_send_confirm;
2376
    }
2377
    resp = sae_sm_step(hapd, sta, auth_transaction,
2378
           status_code, 0, &sta_removed);
2379
  } else {
2380
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2381
             HOSTAPD_LEVEL_DEBUG,
2382
             "unexpected SAE authentication transaction %u (status=%u (%s))",
2383
             auth_transaction, status_code,
2384
             status2str(status_code));
2385
    if (status_code != WLAN_STATUS_SUCCESS)
2386
      goto remove_sta;
2387
    resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
2388
  }
2389
2390
reply:
2391
  if (!sta_removed && resp != WLAN_STATUS_SUCCESS) {
2392
    pos = mgmt->u.auth.variable;
2393
    end = ((const u8 *) mgmt) + len;
2394
2395
    /* Copy the Finite Cyclic Group field from the request if we
2396
     * rejected it as unsupported group. */
2397
    if (resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
2398
        !data && end - pos >= 2)
2399
      data = wpabuf_alloc_copy(pos, 2);
2400
2401
    send_auth_reply(hapd, sta, sta->addr,
2402
        WLAN_AUTH_SAE,
2403
        auth_transaction, resp,
2404
        data ? wpabuf_head(data) : (u8 *) "",
2405
        data ? wpabuf_len(data) : 0, "auth-sae");
2406
    sae_sme_send_external_auth_status(hapd, sta, resp);
2407
  }
2408
2409
remove_sta:
2410
  if (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT)
2411
    success_status = sae_status_success(hapd, status_code);
2412
  else
2413
    success_status = status_code == WLAN_STATUS_SUCCESS;
2414
  if (!sta_removed && sta->added_unassoc &&
2415
      (resp != WLAN_STATUS_SUCCESS || !success_status)) {
2416
    hostapd_drv_sta_remove(hapd, sta->addr);
2417
    sta->added_unassoc = 0;
2418
  }
2419
  wpabuf_free(data);
2420
}
2421
2422
2423
/**
2424
 * auth_sae_init_committed - Send COMMIT and start SAE in committed state
2425
 * @hapd: BSS data for the device initiating the authentication
2426
 * @sta: the peer to which commit authentication frame is sent
2427
 *
2428
 * This function implements Init event handling (IEEE Std 802.11-2012,
2429
 * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
2430
 * sta->sae structure should be initialized appropriately via a call to
2431
 * sae_prepare_commit().
2432
 */
2433
int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
2434
{
2435
  int ret;
2436
2437
  if (!sta->sae || !sta->sae->tmp)
2438
    return -1;
2439
2440
  if (sta->sae->state != SAE_NOTHING)
2441
    return -1;
2442
2443
  ret = auth_sae_send_commit(hapd, sta, 0, -1);
2444
  if (ret)
2445
    return -1;
2446
2447
  sae_set_state(sta, SAE_COMMITTED, "Init and sent commit");
2448
  sta->sae->sync = 0;
2449
  sae_set_retransmit_timer(hapd, sta);
2450
2451
  return 0;
2452
}
2453
2454
2455
void auth_sae_process_commit(void *eloop_ctx, void *user_ctx)
2456
{
2457
  struct hostapd_data *hapd = eloop_ctx;
2458
  struct hostapd_sae_commit_queue *q;
2459
  unsigned int queue_len;
2460
2461
  q = dl_list_first(&hapd->sae_commit_queue,
2462
        struct hostapd_sae_commit_queue, list);
2463
  if (!q)
2464
    return;
2465
  wpa_printf(MSG_DEBUG,
2466
       "SAE: Process next available message from queue");
2467
  dl_list_del(&q->list);
2468
  handle_auth(hapd, (const struct ieee80211_mgmt *) q->msg, q->len,
2469
        q->rssi, 1);
2470
  os_free(q);
2471
2472
  if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
2473
    return;
2474
  queue_len = dl_list_len(&hapd->sae_commit_queue);
2475
  eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
2476
             hapd, NULL);
2477
}
2478
2479
2480
static void auth_sae_queue(struct hostapd_data *hapd,
2481
         const struct ieee80211_mgmt *mgmt, size_t len,
2482
         int rssi)
2483
{
2484
  struct hostapd_sae_commit_queue *q, *q2;
2485
  unsigned int queue_len;
2486
  const struct ieee80211_mgmt *mgmt2;
2487
2488
  queue_len = dl_list_len(&hapd->sae_commit_queue);
2489
  if (queue_len >= 15) {
2490
    wpa_printf(MSG_DEBUG,
2491
         "SAE: No more room in message queue - drop the new frame from "
2492
         MACSTR, MAC2STR(mgmt->sa));
2493
    return;
2494
  }
2495
2496
  wpa_printf(MSG_DEBUG, "SAE: Queue Authentication message from "
2497
       MACSTR " for processing (queue_len %u)", MAC2STR(mgmt->sa),
2498
       queue_len);
2499
  q = os_zalloc(sizeof(*q) + len);
2500
  if (!q)
2501
    return;
2502
  q->rssi = rssi;
2503
  q->len = len;
2504
  os_memcpy(q->msg, mgmt, len);
2505
2506
  /* Check whether there is already a queued Authentication frame from the
2507
   * same station with the same transaction number and if so, replace that
2508
   * queue entry with the new one. This avoids issues with a peer that
2509
   * sends multiple times (e.g., due to frequent SAE retries). There is no
2510
   * point in us trying to process the old attempts after a new one has
2511
   * obsoleted them. */
2512
  dl_list_for_each(q2, &hapd->sae_commit_queue,
2513
       struct hostapd_sae_commit_queue, list) {
2514
    mgmt2 = (const struct ieee80211_mgmt *) q2->msg;
2515
    if (ether_addr_equal(mgmt->sa, mgmt2->sa) &&
2516
        mgmt->u.auth.auth_transaction ==
2517
        mgmt2->u.auth.auth_transaction) {
2518
      wpa_printf(MSG_DEBUG,
2519
           "SAE: Replace queued message from same STA with same transaction number");
2520
      dl_list_add(&q2->list, &q->list);
2521
      dl_list_del(&q2->list);
2522
      os_free(q2);
2523
      goto queued;
2524
    }
2525
  }
2526
2527
  /* No pending identical entry, so add to the end of the queue */
2528
  dl_list_add_tail(&hapd->sae_commit_queue, &q->list);
2529
2530
queued:
2531
  if (eloop_is_timeout_registered(auth_sae_process_commit, hapd, NULL))
2532
    return;
2533
  eloop_register_timeout(0, queue_len * 10000, auth_sae_process_commit,
2534
             hapd, NULL);
2535
}
2536
2537
2538
static int auth_sae_queued_addr(struct hostapd_data *hapd, const u8 *addr)
2539
{
2540
  struct hostapd_sae_commit_queue *q;
2541
  const struct ieee80211_mgmt *mgmt;
2542
2543
  dl_list_for_each(q, &hapd->sae_commit_queue,
2544
       struct hostapd_sae_commit_queue, list) {
2545
    mgmt = (const struct ieee80211_mgmt *) q->msg;
2546
    if (ether_addr_equal(addr, mgmt->sa))
2547
      return 1;
2548
  }
2549
2550
  return 0;
2551
}
2552
2553
#endif /* CONFIG_SAE */
2554
2555
2556
static u16 wpa_res_to_status_code(enum wpa_validate_result res)
2557
0
{
2558
0
  switch (res) {
2559
0
  case WPA_IE_OK:
2560
0
    return WLAN_STATUS_SUCCESS;
2561
0
  case WPA_INVALID_IE:
2562
0
    return WLAN_STATUS_INVALID_ELEMENT;
2563
0
  case WPA_INVALID_GROUP:
2564
0
    return WLAN_STATUS_INVALID_GROUP_CIPHER;
2565
0
  case WPA_INVALID_PAIRWISE:
2566
0
    return WLAN_STATUS_INVALID_PAIRWISE_CIPHER;
2567
0
  case WPA_INVALID_AKMP:
2568
0
    return WLAN_STATUS_INVALID_AKMP;
2569
0
  case WPA_NOT_ENABLED:
2570
0
    return WLAN_STATUS_INVALID_ELEMENT;
2571
0
  case WPA_ALLOC_FAIL:
2572
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
2573
0
  case WPA_MGMT_FRAME_PROTECTION_VIOLATION:
2574
0
    return WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
2575
0
  case WPA_INVALID_MGMT_GROUP_CIPHER:
2576
0
    return WLAN_STATUS_CIPHER_OUT_OF_POLICY;
2577
0
  case WPA_INVALID_MDIE:
2578
0
    return WLAN_STATUS_INVALID_MDE;
2579
0
  case WPA_INVALID_PROTO:
2580
0
    return WLAN_STATUS_INVALID_ELEMENT;
2581
0
  case WPA_INVALID_PMKID:
2582
0
    return WLAN_STATUS_INVALID_PMKID;
2583
0
  case WPA_DENIED_OTHER_REASON:
2584
0
    return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
2585
0
  }
2586
0
  return WLAN_STATUS_INVALID_ELEMENT;
2587
0
}
2588
2589
2590
#ifdef CONFIG_IEEE8021X_AUTH
2591
2592
static struct wpabuf *
2593
prepare_802_1x_auth_resp(struct hostapd_data *hapd, struct sta_info *sta,
2594
       u16 auth_transaction, u16 status,
2595
       struct rsn_pmksa_cache_entry *cached_pmk,
2596
       const u8 *eap_req, size_t eap_req_len)
2597
{
2598
  struct wpabuf *pub = NULL, *data;
2599
  bool enc_assoc = ap_sta_support_enc_assoc(hapd,
2600
              sta->eap_auth_data.rsnxe,
2601
              sta->eap_auth_data.rsnxe_len);
2602
2603
  data = wpabuf_alloc(1000 + eap_req_len);
2604
  if (!data) {
2605
    wpa_printf(MSG_INFO,
2606
         "Authentication frame buffer allocation failed");
2607
    return NULL;
2608
  }
2609
2610
  /* Encapsulation Length field */
2611
  wpabuf_put_le16(data, eap_req_len);
2612
  /* Encapsulation field */
2613
  wpabuf_put_data(data, eap_req, eap_req_len);
2614
2615
  if (status != WLAN_STATUS_SUCCESS &&
2616
      status != WLAN_STATUS_802_1_X_AUTH_SUCCESS)
2617
    goto reply;
2618
2619
  /* Authentication frames with transaction sequence greater than or
2620
   * equal to 3 contain Authentication fields only.
2621
   */
2622
  if (auth_transaction == 2) {
2623
    /* Per IEEE 802.11bi/D4.0, 12.16.8.3 (IEEE 802.1X), a responder
2624
     * that sets
2625
     * dot11EPPReAssociationFrameEncryptionSupportActivated
2626
     * to false or does not receive the RSNXE in the first
2627
     * Authentication frame with the (Re)Association Frame
2628
     * Encryption Support field set to 1 shall not include
2629
     * a Diffie-Hellman Parameter element nor a Nonce element
2630
     * nor an RSNE in the second Authentication frame for
2631
     * IEEE 802.1X authentication.
2632
     */
2633
    if (enc_assoc) {
2634
      u8 a_nonce[WPA_NONCE_LEN];
2635
      struct hostapd_bss_config *conf = hapd->conf;
2636
      int res;
2637
2638
      /* Derive own public key */
2639
      if (sta->eap_auth_data.ecdh) {
2640
        pub = crypto_ecdh_get_pubkey(
2641
          sta->eap_auth_data.ecdh, 1);
2642
        if (!pub) {
2643
          status =
2644
            WLAN_STATUS_UNSPECIFIED_FAILURE;
2645
          goto reply;
2646
        }
2647
      }
2648
2649
      /* ANonce generation */
2650
      if (random_get_bytes(a_nonce, WPA_NONCE_LEN) < 0) {
2651
        status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2652
        goto reply;
2653
      }
2654
      os_memcpy(sta->eap_auth_data.anonce, a_nonce,
2655
          WPA_NONCE_LEN);
2656
2657
2658
      if (pub && wpabuf_resize(&data, wpabuf_len(pub)) < 0) {
2659
        status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2660
        goto reply;
2661
      }
2662
2663
      /* Per IEEE 802.11bi/D4.0, 12.16.8.3 (IEEE 802.1X),
2664
       * responder shall include an RSNE with the AKM and
2665
       * pairwise cipher suite as indicated in the first
2666
       * Authentication frame.
2667
       */
2668
      res = wpa_write_802_1x_rsne(
2669
        hapd->wpa_auth,
2670
        wpabuf_mhead_u8(data) + wpabuf_len(data),
2671
        (wpabuf_size(data) - wpabuf_len(data)),
2672
        cached_pmk ? cached_pmk->pmkid : NULL,
2673
        sta->eap_auth_data.akm,
2674
        sta->eap_auth_data.cipher,
2675
        conf->wpa_group,
2676
        conf->group_mgmt_cipher,
2677
        conf->ieee80211w);
2678
      if (res < 0) {
2679
        status = WLAN_STATUS_UNSPECIFIED_FAILURE;
2680
        goto reply;
2681
      }
2682
      wpabuf_put(data, res);
2683
2684
      /* DH Parameter element */
2685
      wpabuf_put_u8(data, WLAN_EID_EXTENSION);
2686
      wpabuf_put_u8(data, 1 + 2 + wpabuf_len(pub));
2687
      wpabuf_put_u8(data, WLAN_EID_EXT_OWE_DH_PARAM);
2688
      wpabuf_put_le16(data, sta->eap_auth_data.group);
2689
      wpabuf_put_buf(data, pub);
2690
2691
      /* ANonce in Nonce element */
2692
      wpabuf_put_u8(data, WLAN_EID_EXTENSION);
2693
      wpabuf_put_u8(data, 1 + WPA_NONCE_LEN);
2694
      wpabuf_put_u8(data, WLAN_EID_EXT_NONCE);
2695
      wpabuf_put_data(data, a_nonce, WPA_NONCE_LEN);
2696
    } else {
2697
      /* Per IEEE 802.11bi/D4.0, 12.16.5 (IEEE 802.1X
2698
       * authentication utilizing Authentication frames), the
2699
       * Responser shall construct the second Authentication
2700
       * frame with an AKM Suite Selector element indicating
2701
       * the same IEEE 802.1X AKM indicated in the first
2702
       * Authentication frame.
2703
       */
2704
      wpabuf_put_u8(data, WLAN_EID_EXTENSION);
2705
      wpabuf_put_u8(data, 1 + RSN_SELECTOR_LEN);
2706
      wpabuf_put_u8(data, WLAN_EID_EXT_AKM_SUITE_SELECTOR);
2707
      RSN_SELECTOR_PUT(wpabuf_put(data, RSN_SELECTOR_LEN),
2708
           wpa_akm_to_suite(
2709
             sta->eap_auth_data.akm));
2710
    }
2711
  } /* if (auth_transaction == 2) */
2712
reply:
2713
  wpabuf_free(pub);
2714
  return data;
2715
}
2716
2717
2718
u16 wpa_auth_validate_802_1x_frame(struct hostapd_data *hapd,
2719
           struct sta_info *sta,
2720
           struct ieee802_11_elems *elems)
2721
{
2722
  struct wpa_ie_data rsn;
2723
  const int default_groups[] = { 19, 0 };
2724
  bool enc_assoc = ap_sta_support_enc_assoc(hapd,
2725
              elems->rsnxe,
2726
              elems->rsnxe_len);
2727
2728
  /* Per IEEE P802.11bi/D4.0, 12.16.8.3 (IEEE 802.1X), an originator that
2729
   * sets dot11EPPReAssociationFrameEncryptionSupportActivated to false or
2730
   * does not receive the RSNXE from the responder with the
2731
   * (Re)Association Frame Encryption Support field set to 1 shall not
2732
   * include a Diffie-Hellman Parameter element nor an RSNE nor an RSNXE
2733
   * nor a Nonce element in the first Authentication frame for IEEE 802.1X
2734
   * authentication.
2735
   */
2736
  if (!enc_assoc &&
2737
      (elems->rsn_ie || elems->nonce || elems->owe_dh)) {
2738
    wpa_printf(MSG_INFO,
2739
         "Invalid inclusion of RSNE/Nonce/DHE when (Re)Association frame encryption is not supported");
2740
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
2741
  }
2742
  if (enc_assoc &&
2743
      (!elems->rsn_ie || !elems->nonce ||
2744
       elems->nonce_len != WPA_NONCE_LEN || !elems->owe_dh)) {
2745
    wpa_printf(MSG_ERROR, "Missing RSNE/DHIE/Nonce");
2746
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
2747
  }
2748
2749
  /* Both RSNE and AKM Suite Selector element shall not be present at the
2750
   * same time. */
2751
  if (elems->rsn_ie && elems->akm_suite_selector) {
2752
    wpa_printf(MSG_INFO,
2753
         "Incorrect inclusion of both RSNE and AKM Suite Selector element");
2754
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
2755
  }
2756
2757
  if (enc_assoc &&
2758
      (!elems->rsn_ie ||
2759
       wpa_parse_wpa_ie_rsn(elems->rsn_ie - 2, elems->rsn_ie_len + 2,
2760
          &rsn) < 0)) {
2761
    wpa_printf(MSG_INFO, "No valid RSNE");
2762
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
2763
  }
2764
2765
  if (enc_assoc && elems->rsn_ie) {
2766
    if (!(rsn.pairwise_cipher & hapd->conf->rsn_pairwise)) {
2767
      wpa_printf(MSG_INFO,
2768
           "Invalid pairwise cipher (0x%x) in RSNE",
2769
           rsn.pairwise_cipher);
2770
      return WPA_INVALID_PAIRWISE;
2771
    }
2772
    sta->eap_auth_data.cipher = rsn.pairwise_cipher;
2773
    wpa_printf(MSG_DEBUG, "Received pairwise cipher (0x%x) in RSNE",
2774
         rsn.pairwise_cipher);
2775
2776
    if (!(rsn.key_mgmt &
2777
          (hapd->conf->wpa_key_mgmt |
2778
           hostapd_config_sp_key_mgmt(hapd->conf)))) {
2779
      wpa_printf(MSG_INFO, "Invalid key mgmt (0x%x) in RSNE",
2780
           rsn.key_mgmt);
2781
      return WPA_INVALID_AKMP;
2782
    }
2783
    sta->eap_auth_data.akm = rsn.key_mgmt;
2784
    wpa_printf(MSG_DEBUG, "Received keymgmt (0x%x) in RSNE",
2785
         rsn.key_mgmt);
2786
  }
2787
2788
  /* Validate AKM Suite Selector element */
2789
  if (elems->akm_suite_selector) {
2790
    sta->eap_auth_data.akm = rsn_key_mgmt_to_wpa_akm(
2791
      RSN_SELECTOR_GET(elems->akm_suite_selector));
2792
    if (!(sta->eap_auth_data.akm & hapd->conf->wpa_key_mgmt)) {
2793
      wpa_printf(MSG_INFO,
2794
           "Invalid key mgmt (0x%x) in AKM Suite Selector element",
2795
           sta->eap_auth_data.akm);
2796
      return WPA_INVALID_AKMP;
2797
    }
2798
    wpa_printf(MSG_DEBUG,
2799
         "Received keymgmt (0x%x) in AKM Suite Selector element",
2800
         sta->eap_auth_data.akm);
2801
  }
2802
2803
  if (elems->rsnxe) {
2804
    os_free(sta->eap_auth_data.rsnxe);
2805
    sta->eap_auth_data.rsnxe =
2806
      os_memdup(elems->rsnxe, elems->rsnxe_len);
2807
    sta->eap_auth_data.rsnxe_len = elems->rsnxe_len;
2808
  }
2809
2810
  /* Store SNonce */
2811
  if (elems->nonce && elems->nonce_len == WPA_NONCE_LEN) {
2812
    os_memcpy(sta->eap_auth_data.snonce, elems->nonce,
2813
        WPA_NONCE_LEN);
2814
    wpa_hexdump(MSG_DEBUG, "SNonce", elems->nonce, WPA_NONCE_LEN);
2815
  }
2816
2817
  /* Validate DH Parameter element */
2818
  if (elems->owe_dh) {
2819
    u16 group;
2820
    u8 pubkey_len;
2821
    const u8 *pubkey;
2822
    struct wpabuf *secret;
2823
2824
    group = WPA_GET_LE16(elems->owe_dh);
2825
    if (!int_array_includes(default_groups, group)) {
2826
      wpa_printf(MSG_INFO,
2827
           "Received unsupported group value %u",
2828
           group);
2829
      return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2830
    }
2831
    sta->eap_auth_data.group = group;
2832
    pubkey = elems->owe_dh + 2;
2833
    pubkey_len = elems->owe_dh_len - 2;
2834
2835
    /* TODO: Any more validation of peer public key needed? */
2836
    if (!pubkey_len) {
2837
      wpa_printf(MSG_INFO, "Missing DH public key");
2838
      return WLAN_STATUS_INVALID_PUBLIC_KEY;
2839
    }
2840
2841
    /* Setup ECDH context */
2842
    crypto_ecdh_deinit(sta->eap_auth_data.ecdh);
2843
    sta->eap_auth_data.ecdh = crypto_ecdh_init(group);
2844
    if (!sta->eap_auth_data.ecdh) {
2845
      wpa_printf(MSG_INFO, "Failed to setup ECDH context");
2846
      return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
2847
    }
2848
2849
    /* Generate shared secret */
2850
    wpabuf_clear_free(sta->eap_auth_data.dhss);
2851
    sta->eap_auth_data.dhss = NULL;
2852
    secret = crypto_ecdh_set_peerkey(sta->eap_auth_data.ecdh, 0,
2853
             pubkey, pubkey_len);
2854
    if (!secret) {
2855
      wpa_printf(MSG_INFO, "Invalid peer public key");
2856
      return WLAN_STATUS_UNSPECIFIED_FAILURE;
2857
    }
2858
    wpa_hexdump_buf_key(MSG_DEBUG, "DH shared secret", secret);
2859
    sta->eap_auth_data.dhss = secret;
2860
  }
2861
2862
  return WLAN_STATUS_SUCCESS;
2863
}
2864
2865
2866
/**
2867
 * ieee80211_send_eap_req - Callback function to send EAP-Request message in an
2868
 *  Authentication frame
2869
 *
2870
 * This function is called from ieee802_1x_eapol_send() using the
2871
 * hapd->send_eap_req callback. Its main purpose is to prepend the EAP-Request
2872
 * data with an IEEE 802.1X header and call prepare_802_1x_auth_resp() to send
2873
 * out the next IEEE 802.1X Authentication frame to the station. If this is an
2874
 * EAP-Success frame, it also fetches the MSK derived from the successful EAP
2875
 * authentication to derive PMK and PTK and configure the TK to the driver.
2876
 */
2877
void ieee80211_send_eap_req(struct hostapd_data *hapd, struct sta_info *sta,
2878
          u8 type, u16 auth_transaction, u16 status,
2879
          struct rsn_pmksa_cache_entry *cached_pmk,
2880
          const u8 *eap_req, size_t eap_req_len)
2881
{
2882
  bool enc_assoc = ap_sta_support_enc_assoc(hapd,
2883
              sta->eap_auth_data.rsnxe,
2884
              sta->eap_auth_data.rsnxe_len);
2885
  struct ieee802_1x_hdr *xhdr;
2886
  u8 *data;
2887
  size_t len = eap_req_len + sizeof(struct ieee802_1x_hdr);
2888
  struct wpabuf *reply;
2889
2890
  wpa_printf(MSG_DEBUG,
2891
       "Process EAP-Request data for TX using an Authentication frame");
2892
2893
  data = os_malloc(len);
2894
  if (!data) {
2895
    wpa_printf(MSG_ERROR, "malloc() failed for %s", __func__);
2896
    return;
2897
  }
2898
2899
  xhdr = (struct ieee802_1x_hdr *) data;
2900
  xhdr->version = hapd->conf->eapol_version;
2901
  xhdr->type = type;
2902
  xhdr->length = host_to_be16(eap_req_len);
2903
2904
  if (eap_req && eap_req_len > 0)
2905
    os_memcpy(xhdr + 1, eap_req, eap_req_len);
2906
2907
  wpa_hexdump(MSG_MSGDUMP, "EAP-Request", eap_req, eap_req_len);
2908
2909
  /* EAP-Success */
2910
  if (enc_assoc && eap_req_len > 0 && eap_req[0] == 3) {
2911
    u8 msk[2 * PMK_LEN] = { 0 };
2912
    size_t _len = 2 * PMK_LEN;
2913
    size_t pmk_len, kdk_len;
2914
    bool is_ml = ap_sta_is_mld(hapd, sta);
2915
    enum wpa_alg alg =
2916
      wpa_cipher_to_alg(sta->eap_auth_data.cipher);
2917
    size_t key_len =
2918
      wpa_cipher_key_len(sta->eap_auth_data.cipher);
2919
    const u8 *aa = hapd->own_addr;
2920
    struct rsn_pmksa_cache *pmksa =
2921
      wpa_auth_get_pmksa_cache(hapd->wpa_auth, is_ml);
2922
    struct rsn_pmksa_cache_entry *entry;
2923
#ifdef CONFIG_TESTING_OPTIONS
2924
    bool force_kdk = hapd->conf->force_kdk_derivation;
2925
#else /* CONFIG_TESTING_OPTIONS */
2926
    bool force_kdk = false;
2927
#endif /* CONFIG_TESTING_OPTIONS */
2928
2929
#ifdef CONFIG_IEEE80211BE
2930
    if (is_ml)
2931
      aa = hapd->mld->mld_addr;
2932
#endif /* CONFIG_IEEE80211BE */
2933
2934
    /* Per IEEE 802.11bi/D4.0, 12.16.5 (IEEE 802.1X authentication
2935
     * utilizing Authentication frames), if the IEEE 802.1X
2936
     * authentication is successful, the Status Code field
2937
     * is set to 802_1_X_AUTH_SUCCESS. */
2938
    status = WLAN_STATUS_802_1_X_AUTH_SUCCESS;
2939
    /* TODO: If the IEEE 802.1X authentication fails,
2940
     * the status code is set to 802_1_X_AUTH_FAILED. */
2941
    os_memset(&sta->eap_auth_data.ptk, 0, sizeof(struct wpa_ptk));
2942
    if (wpa_auth_802_1x_get_msk(hapd->wpa_auth, sta->addr,
2943
              msk, &_len)) {
2944
      wpa_printf(MSG_INFO, "Failed to get MSK");
2945
      os_free(data);
2946
      return;
2947
    }
2948
2949
    if (wpa_key_mgmt_sha384(sta->eap_auth_data.akm))
2950
      pmk_len = PMK_LEN_SUITE_B_192;
2951
    else
2952
      pmk_len = PMK_LEN;
2953
2954
    sta->eap_auth_data.pmk_len = pmk_len;
2955
    os_memcpy(sta->eap_auth_data.pmk, msk, pmk_len);
2956
2957
    if (force_kdk ||
2958
        (wpa_auth_ap_support_secure_ltf(hapd->wpa_auth) &&
2959
         ieee802_11_rsnx_capab(sta->eap_auth_data.rsnxe,
2960
             WLAN_RSNX_CAPAB_SECURE_LTF)))
2961
      kdk_len = WPA_KDK_MAX_LEN;
2962
    else
2963
      kdk_len = 0;
2964
    if (wpa_auth_802_1x_pmk_to_ptk(
2965
          msk, sta->eap_auth_data.pmk_len,
2966
          sta->addr, aa,
2967
          sta->eap_auth_data.snonce,
2968
          sta->eap_auth_data.anonce,
2969
          sta->eap_auth_data.akm,
2970
          sta->eap_auth_data.cipher,
2971
          wpabuf_head_u8(sta->eap_auth_data.dhss),
2972
          wpabuf_len(sta->eap_auth_data.dhss),
2973
          &sta->eap_auth_data.ptk, kdk_len)) {
2974
      wpa_printf(MSG_INFO, "Failed to derive the PTK");
2975
      os_free(data);
2976
      return;
2977
    }
2978
    wpa_printf(MSG_DEBUG, "PTK derived successfully");
2979
2980
    if (wpa_auth_802_1x_set_key(hapd->wpa_auth,
2981
              alg, sta->addr,
2982
              sta->eap_auth_data.ptk.tk,
2983
              key_len)) {
2984
      wpa_printf(MSG_INFO,
2985
           "Failed to set the TK to the driver");
2986
      os_free(data);
2987
      return;
2988
    }
2989
2990
    /* Delete DHss after successful PTK derivation */
2991
    wpabuf_clear_free(sta->eap_auth_data.dhss);
2992
    sta->eap_auth_data.dhss = NULL;
2993
2994
    /* TODO: Fill session_timeout? */
2995
    wpa_hexdump_key(MSG_DEBUG, "IEEE802.1X: Cache PMK",
2996
        msk, pmk_len);
2997
2998
    entry = pmksa_cache_auth_add(pmksa, msk, pmk_len, NULL,
2999
               sta->eap_auth_data.ptk.kck,
3000
               sta->eap_auth_data.ptk.kck_len,
3001
               aa, sta->addr, 0, sta->eapol_sm,
3002
               sta->eap_auth_data.akm);
3003
    if (!entry) {
3004
      wpa_printf(MSG_INFO, "Failed to add PMKSA entry");
3005
      return;
3006
    }
3007
    os_memcpy(sta->eap_auth_data.epp_pmkid_cur, entry->pmkid,
3008
        PMKID_LEN);
3009
  }
3010
3011
  reply = prepare_802_1x_auth_resp(hapd, sta, auth_transaction, status,
3012
           cached_pmk, data, len);
3013
  if (reply)
3014
    send_8021x_auth_reply(hapd, sta, auth_transaction, status,
3015
              reply);
3016
  os_free(data);
3017
}
3018
3019
3020
static void handle_auth_802_1x(struct hostapd_data *hapd, struct sta_info *sta,
3021
             const u8 *pos, size_t len, u16 auth_alg,
3022
             u16 auth_transaction)
3023
{
3024
  struct ieee802_1x_hdr *eapol_pdu;
3025
  u16 encap_len, resp = WLAN_STATUS_SUCCESS;
3026
  const u8 *end;
3027
  struct wpabuf *reply;
3028
3029
  if (len < 2) {
3030
    wpa_printf(MSG_INFO, "Missing Encapsulation Length field");
3031
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3032
    goto fail;
3033
  }
3034
  end = pos + len;
3035
  encap_len = WPA_GET_LE16(pos);
3036
  pos += 2;
3037
  if (encap_len > end - pos) {
3038
    wpa_printf(MSG_INFO, "Truncated Encapsulation field");
3039
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3040
    goto fail;
3041
  }
3042
3043
  /* Start of Encapsulation field */
3044
  eapol_pdu = (struct ieee802_1x_hdr *) pos;
3045
3046
  if (auth_transaction == 1 &&
3047
      eapol_pdu->type != IEEE802_1X_TYPE_EAPOL_START) {
3048
    wpa_printf(MSG_INFO,
3049
         "Received unexpected EAPOL PDU type %u in the first Authentication frame",
3050
         eapol_pdu->type);
3051
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3052
    goto fail;
3053
  }
3054
  pos += encap_len;
3055
  sta->eap_auth_data.auth_transaction = auth_transaction;
3056
3057
  /* Process Authentication frame elements
3058
   * Authentication frames with transaction sequence greater
3059
   * than or equal to 3 contain Authentication fields only.
3060
   */
3061
  if (auth_transaction == 1) {
3062
    struct wpa_ie_data data;
3063
    struct ieee802_11_elems elems;
3064
    struct rsn_pmksa_cache_entry *cached_pmk = NULL;
3065
    bool is_ml = ap_sta_is_mld(hapd, sta);
3066
    bool enc_assoc, pmkid_privacy;
3067
    size_t i;
3068
3069
    if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) ==
3070
        ParseFailed) {
3071
      wpa_printf(MSG_INFO, "Could not parse elements");
3072
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3073
      goto fail;
3074
    }
3075
3076
    resp = wpa_auth_validate_802_1x_frame(hapd, sta, &elems);
3077
    if (resp)
3078
      goto fail;
3079
3080
    /* Validate Security Profile element, if present */
3081
    if (hapd->conf->security_profiles && elems.security_profile) {
3082
      wpa_hexdump(MSG_DEBUG,
3083
            "STA Security Profile element body in 802.1X auth",
3084
            elems.security_profile,
3085
            elems.security_profile_len);
3086
3087
      if (!validate_sta_security_profile(
3088
            hapd, sta->addr,
3089
            elems.rsn_ie, elems.rsn_ie_len,
3090
            elems.rsnxe, elems.rsnxe_len,
3091
            elems.security_profile,
3092
            elems.security_profile_len,
3093
            NULL)) {
3094
        wpa_printf(MSG_INFO,
3095
             "Rejecting 802.1X auth from " MACSTR
3096
             " - Security Profile element mismatch",
3097
             MAC2STR(sta->addr));
3098
        resp = WLAN_STATUS_REJECTED_INVALID_SECURITY_PROFILE;
3099
        goto fail;
3100
      }
3101
    }
3102
3103
    enc_assoc = ap_sta_support_enc_assoc(hapd, elems.rsnxe,
3104
                 elems.rsnxe_len);
3105
3106
    pmkid_privacy = hapd->conf->pmksa_caching_privacy &&
3107
      ieee802_11_rsnx_capab_len(
3108
        elems.rsnxe, elems.rsnxe_len,
3109
        WLAN_RSNX_CAPAB_PMKSA_CACHING_PRIVACY);
3110
3111
    os_memset(&data, 0, sizeof(data));
3112
    if (enc_assoc &&
3113
        (!elems.rsn_ie ||
3114
         wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2,
3115
            elems.rsn_ie_len + 2, &data) < 0)) {
3116
      wpa_printf(MSG_INFO, "No valid RSNE");
3117
      goto fail;
3118
    }
3119
3120
    for (i = 0; i < data.num_pmkid; i++) {
3121
      const u8 *aa;
3122
      enum wpa_alg alg;
3123
      size_t key_len, kdk_len;
3124
#ifdef CONFIG_TESTING_OPTIONS
3125
      bool force_kdk = hapd->conf->force_kdk_derivation;
3126
#else /* CONFIG_TESTING_OPTIONS */
3127
      bool force_kdk = false;
3128
#endif /* CONFIG_TESTING_OPTIONS */
3129
3130
      wpa_hexdump(MSG_DEBUG, "RSNE: STA PMKID",
3131
            &data.pmkid[i * PMKID_LEN], PMKID_LEN);
3132
3133
      cached_pmk = pmksa_cache_search(
3134
        hapd, pmkid_privacy ? NULL : sta->addr,
3135
        &data.pmkid[i * PMKID_LEN], is_ml);
3136
      if (!cached_pmk)
3137
        continue;
3138
3139
      aa = hapd->own_addr;
3140
      alg = wpa_cipher_to_alg(sta->eap_auth_data.cipher);
3141
      key_len = wpa_cipher_key_len(sta->eap_auth_data.cipher);
3142
3143
#ifdef CONFIG_IEEE80211BE
3144
      if (ap_sta_is_mld(hapd, sta))
3145
        aa = hapd->mld->mld_addr;
3146
#endif /* CONFIG_IEEE80211BE */
3147
      wpa_printf(MSG_DEBUG,
3148
           "Found a matching PMKSA cache entry");
3149
      os_memcpy(sta->eap_auth_data.epp_pmkid_cur,
3150
          cached_pmk->pmkid, PMKID_LEN);
3151
      reply = prepare_802_1x_auth_resp(
3152
        hapd, sta, auth_transaction + 1,
3153
        WLAN_STATUS_SUCCESS, cached_pmk, NULL, 0);
3154
      if (!reply) {
3155
        wpa_printf(MSG_INFO,
3156
             "Failed to prepare IEEE 802.1X Authentication frame");
3157
        return;
3158
      }
3159
3160
      if (force_kdk ||
3161
          (wpa_auth_ap_support_secure_ltf(hapd->wpa_auth) &&
3162
           ieee802_11_rsnx_capab(sta->eap_auth_data.rsnxe,
3163
               WLAN_RSNX_CAPAB_SECURE_LTF)))
3164
        kdk_len = WPA_KDK_MAX_LEN;
3165
      else
3166
        kdk_len = 0;
3167
3168
      if (wpa_auth_802_1x_pmk_to_ptk(
3169
            cached_pmk->pmk, cached_pmk->pmk_len,
3170
            sta->addr, aa,
3171
            sta->eap_auth_data.snonce,
3172
            sta->eap_auth_data.anonce,
3173
            sta->eap_auth_data.akm,
3174
            sta->eap_auth_data.cipher,
3175
            wpabuf_head_u8(sta->eap_auth_data.dhss),
3176
            wpabuf_len(sta->eap_auth_data.dhss),
3177
            &sta->eap_auth_data.ptk, kdk_len)) {
3178
        wpa_printf(MSG_INFO, "Failed to derive PTK");
3179
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3180
        goto fail;
3181
      }
3182
      wpa_printf(MSG_DEBUG, "PTK derived successfully");
3183
3184
      if (wpa_auth_802_1x_set_key(hapd->wpa_auth, alg,
3185
                sta->addr,
3186
                sta->eap_auth_data.ptk.tk,
3187
                key_len)) {
3188
        wpa_printf(MSG_INFO,
3189
             "Failed to set TK to driver");
3190
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3191
        goto fail;
3192
      }
3193
3194
      sta->flags |= WLAN_STA_AUTH;
3195
      sta->auth_alg = WLAN_AUTH_802_1X;
3196
      sta->eap_auth_data.add_mic = true;
3197
      send_8021x_auth_reply(hapd, sta, auth_transaction + 1,
3198
                WLAN_STATUS_SUCCESS, reply);
3199
      /* Delete DHss after successful PTK derivation */
3200
      wpabuf_clear_free(sta->eap_auth_data.dhss);
3201
      sta->eap_auth_data.dhss = NULL;
3202
      return;
3203
    }
3204
3205
    /* Start EAPOL SM to process EAPOL PDU */
3206
    if (!sta->eapol_sm) {
3207
      sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
3208
      if (!sta->eapol_sm)
3209
        return;
3210
    }
3211
3212
    ieee802_1x_eapol_sm_set_port_enabled(sta->eapol_sm, true);
3213
  }
3214
3215
  /* Forward the extracted EAP PDU to AS */
3216
  ieee802_1x_receive(hapd, sta->addr, (const u8 *) eapol_pdu,
3217
         encap_len, FRAME_NOT_ENCRYPTED);
3218
  return;
3219
3220
fail:
3221
  reply = prepare_802_1x_auth_resp(hapd, sta, auth_transaction + 1,
3222
           resp, NULL, NULL, 0);
3223
  if (reply)
3224
    send_8021x_auth_reply(hapd, sta, auth_transaction + 1, resp,
3225
              reply);
3226
}
3227
3228
#endif /* CONFIG_IEEE8021X_AUTH */
3229
3230
3231
#ifdef CONFIG_FILS
3232
3233
static void handle_auth_fils_finish(struct hostapd_data *hapd,
3234
            struct sta_info *sta, u16 resp,
3235
            struct wpabuf *data, int pub);
3236
3237
void handle_auth_fils(struct hostapd_data *hapd, struct sta_info *sta,
3238
          const u8 *pos, size_t len, u16 auth_alg,
3239
          u16 auth_transaction, u16 status_code,
3240
          void (*cb)(struct hostapd_data *hapd,
3241
         struct sta_info *sta, u16 resp,
3242
         struct wpabuf *data, int pub))
3243
{
3244
  u16 resp = WLAN_STATUS_SUCCESS;
3245
  const u8 *end;
3246
  struct ieee802_11_elems elems;
3247
  enum wpa_validate_result res;
3248
  struct wpa_ie_data rsn;
3249
  struct rsn_pmksa_cache_entry *pmksa = NULL;
3250
3251
  if (auth_transaction != WLAN_AUTH_TR_SEQ_SAE_COMMIT ||
3252
      status_code != WLAN_STATUS_SUCCESS)
3253
    return;
3254
3255
  end = pos + len;
3256
3257
  wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3258
        pos, end - pos);
3259
3260
  /* TODO: FILS PK */
3261
#ifdef CONFIG_FILS_SK_PFS
3262
  if (auth_alg == WLAN_AUTH_FILS_SK_PFS) {
3263
    u16 group;
3264
    struct wpabuf *pub;
3265
    size_t elem_len;
3266
3267
    /* Using FILS PFS */
3268
3269
    /* Finite Cyclic Group */
3270
    if (end - pos < 2) {
3271
      wpa_printf(MSG_DEBUG,
3272
           "FILS: No room for Finite Cyclic Group");
3273
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3274
      goto fail;
3275
    }
3276
    group = WPA_GET_LE16(pos);
3277
    pos += 2;
3278
    if (group != hapd->conf->fils_dh_group) {
3279
      wpa_printf(MSG_DEBUG,
3280
           "FILS: Unsupported Finite Cyclic Group: %u (expected %u)",
3281
           group, hapd->conf->fils_dh_group);
3282
      resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
3283
      goto fail;
3284
    }
3285
3286
    crypto_ecdh_deinit(sta->fils_ecdh);
3287
    sta->fils_ecdh = crypto_ecdh_init(group);
3288
    if (!sta->fils_ecdh) {
3289
      wpa_printf(MSG_INFO,
3290
           "FILS: Could not initialize ECDH with group %d",
3291
           group);
3292
      resp = WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
3293
      goto fail;
3294
    }
3295
3296
    pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
3297
    if (!pub) {
3298
      wpa_printf(MSG_DEBUG,
3299
           "FILS: Failed to derive ECDH public key");
3300
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3301
      goto fail;
3302
    }
3303
    elem_len = wpabuf_len(pub);
3304
    wpabuf_free(pub);
3305
3306
    /* Element */
3307
    if ((size_t) (end - pos) < elem_len) {
3308
      wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3309
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3310
      goto fail;
3311
    }
3312
3313
    wpabuf_free(sta->fils_g_sta);
3314
    sta->fils_g_sta = wpabuf_alloc_copy(pos, elem_len);
3315
    wpabuf_clear_free(sta->fils_dh_ss);
3316
    sta->fils_dh_ss = crypto_ecdh_set_peerkey(sta->fils_ecdh, 1,
3317
                pos, elem_len);
3318
    if (!sta->fils_dh_ss) {
3319
      wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3320
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3321
      goto fail;
3322
    }
3323
    wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", sta->fils_dh_ss);
3324
    pos += elem_len;
3325
  } else {
3326
    crypto_ecdh_deinit(sta->fils_ecdh);
3327
    sta->fils_ecdh = NULL;
3328
    wpabuf_clear_free(sta->fils_dh_ss);
3329
    sta->fils_dh_ss = NULL;
3330
  }
3331
#endif /* CONFIG_FILS_SK_PFS */
3332
3333
  wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3334
  if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3335
    wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
3336
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3337
    goto fail;
3338
  }
3339
3340
  /* RSNE */
3341
  wpa_hexdump(MSG_DEBUG, "FILS: RSN element",
3342
        elems.rsn_ie, elems.rsn_ie_len);
3343
  if (!elems.rsn_ie ||
3344
      wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3345
         &rsn) < 0) {
3346
    wpa_printf(MSG_DEBUG, "FILS: No valid RSN element");
3347
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3348
    goto fail;
3349
  }
3350
3351
  if (!sta->wpa_sm)
3352
    sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
3353
            NULL);
3354
  if (!sta->wpa_sm) {
3355
    wpa_printf(MSG_DEBUG,
3356
         "FILS: Failed to initialize RSN state machine");
3357
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3358
    goto fail;
3359
  }
3360
3361
  wpa_auth_set_rsn_selection(sta->wpa_sm, elems.rsn_selection,
3362
           elems.rsn_selection_len);
3363
  res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
3364
          hapd->iface->freq,
3365
          elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3366
          elems.rsnxe ? elems.rsnxe - 2 : NULL,
3367
          elems.rsnxe ? elems.rsnxe_len + 2 : 0,
3368
          elems.mdie, elems.mdie_len, NULL, 0, NULL,
3369
          ap_sta_is_mld(hapd, sta), NULL);
3370
  resp = wpa_res_to_status_code(res);
3371
  if (resp != WLAN_STATUS_SUCCESS)
3372
    goto fail;
3373
3374
  if (!elems.nonce) {
3375
    wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
3376
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3377
    goto fail;
3378
  }
3379
  wpa_hexdump(MSG_DEBUG, "FILS: SNonce", elems.nonce, NONCE_LEN);
3380
  os_memcpy(sta->fils_snonce, elems.nonce, NONCE_LEN);
3381
3382
  /* PMKID List */
3383
  if (rsn.pmkid && rsn.num_pmkid > 0) {
3384
    u8 num;
3385
    const u8 *pmkid;
3386
3387
    wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3388
          rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3389
3390
    pmkid = rsn.pmkid;
3391
    num = rsn.num_pmkid;
3392
    while (num) {
3393
      wpa_hexdump(MSG_DEBUG, "FILS: PMKID", pmkid, PMKID_LEN);
3394
      pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr,
3395
               pmkid);
3396
      if (pmksa)
3397
        break;
3398
      pmksa = wpa_auth_pmksa_get_fils_cache_id(hapd->wpa_auth,
3399
                 sta->addr,
3400
                 pmkid);
3401
      if (pmksa)
3402
        break;
3403
      pmkid += PMKID_LEN;
3404
      num--;
3405
    }
3406
  }
3407
  if (pmksa && wpa_auth_sta_key_mgmt(sta->wpa_sm) != pmksa->akmp) {
3408
    wpa_printf(MSG_DEBUG,
3409
         "FILS: Matching PMKSA cache entry has different AKMP (0x%x != 0x%x) - ignore",
3410
         wpa_auth_sta_key_mgmt(sta->wpa_sm), pmksa->akmp);
3411
    pmksa = NULL;
3412
  }
3413
  if (pmksa)
3414
    wpa_printf(MSG_DEBUG, "FILS: Found matching PMKSA cache entry");
3415
3416
  /* FILS Session */
3417
  if (!elems.fils_session) {
3418
    wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3419
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3420
    goto fail;
3421
  }
3422
  wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3423
        FILS_SESSION_LEN);
3424
  os_memcpy(sta->fils_session, elems.fils_session, FILS_SESSION_LEN);
3425
3426
  /* Wrapped Data */
3427
  if (elems.wrapped_data) {
3428
    wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3429
          elems.wrapped_data,
3430
          elems.wrapped_data_len);
3431
    if (!pmksa) {
3432
#ifndef CONFIG_NO_RADIUS
3433
      if (!sta->eapol_sm) {
3434
        sta->eapol_sm =
3435
          ieee802_1x_alloc_eapol_sm(hapd, sta);
3436
      }
3437
      wpa_printf(MSG_DEBUG,
3438
           "FILS: Forward EAP-Initiate/Re-auth to authentication server");
3439
      ieee802_1x_encapsulate_radius(
3440
        hapd, sta, elems.wrapped_data,
3441
        elems.wrapped_data_len);
3442
      sta->fils_pending_cb = cb;
3443
      wpa_printf(MSG_DEBUG,
3444
           "FILS: Will send Authentication frame once the response from authentication server is available");
3445
      sta->flags |= WLAN_STA_PENDING_FILS_ERP;
3446
      /* Calculate pending PMKID here so that we do not need
3447
       * to maintain a copy of the EAP-Initiate/Reauth
3448
       * message. */
3449
      if (fils_pmkid_erp(wpa_auth_sta_key_mgmt(sta->wpa_sm),
3450
             elems.wrapped_data,
3451
             elems.wrapped_data_len,
3452
             sta->fils_erp_pmkid) == 0)
3453
        sta->fils_erp_pmkid_set = 1;
3454
      return;
3455
#else /* CONFIG_NO_RADIUS */
3456
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3457
      goto fail;
3458
#endif /* CONFIG_NO_RADIUS */
3459
    }
3460
  }
3461
3462
  if (hapd->conf->security_profiles) {
3463
    resp = validate_security_profile(hapd, sta, &elems, "FILS",
3464
             NULL);
3465
    if (!resp)
3466
      goto fail;
3467
  }
3468
3469
fail:
3470
  if (cb) {
3471
    struct wpabuf *data;
3472
    int pub = 0;
3473
3474
    data = prepare_auth_resp_fils(hapd, sta, &resp, pmksa, NULL,
3475
                NULL, 0, &pub);
3476
    if (!data) {
3477
      wpa_printf(MSG_DEBUG,
3478
           "%s: prepare_auth_resp_fils() returned failure",
3479
           __func__);
3480
    }
3481
3482
    cb(hapd, sta, resp, data, pub);
3483
  }
3484
}
3485
3486
3487
static struct wpabuf *
3488
prepare_auth_resp_fils(struct hostapd_data *hapd,
3489
           struct sta_info *sta, u16 *resp,
3490
           struct rsn_pmksa_cache_entry *pmksa,
3491
           struct wpabuf *erp_resp,
3492
           const u8 *msk, size_t msk_len,
3493
           int *is_pub)
3494
{
3495
  u8 fils_nonce[NONCE_LEN];
3496
  size_t ielen;
3497
  struct wpabuf *data = NULL;
3498
  const u8 *ie;
3499
  u8 *ie_buf = NULL;
3500
  const u8 *pmk = NULL;
3501
  size_t pmk_len = 0;
3502
  u8 pmk_buf[PMK_LEN_MAX];
3503
  struct wpabuf *pub = NULL;
3504
3505
  if (*resp != WLAN_STATUS_SUCCESS)
3506
    goto fail;
3507
3508
  ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &ielen);
3509
  if (!ie) {
3510
    *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3511
    goto fail;
3512
  }
3513
3514
  if (pmksa) {
3515
    /* Add PMKID of the selected PMKSA into RSNE */
3516
    ie_buf = os_malloc(ielen + 2 + 2 + PMKID_LEN);
3517
    if (!ie_buf) {
3518
      *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3519
      goto fail;
3520
    }
3521
3522
    os_memcpy(ie_buf, ie, ielen);
3523
    if (wpa_insert_pmkid(ie_buf, &ielen, pmksa->pmkid, true) < 0) {
3524
      *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3525
      goto fail;
3526
    }
3527
    ie = ie_buf;
3528
  }
3529
3530
  if (random_get_bytes(fils_nonce, NONCE_LEN) < 0) {
3531
    *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3532
    goto fail;
3533
  }
3534
  wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS Nonce",
3535
        fils_nonce, NONCE_LEN);
3536
3537
#ifdef CONFIG_FILS_SK_PFS
3538
  if (sta->fils_dh_ss && sta->fils_ecdh) {
3539
    pub = crypto_ecdh_get_pubkey(sta->fils_ecdh, 1);
3540
    if (!pub) {
3541
      *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3542
      goto fail;
3543
    }
3544
  }
3545
#endif /* CONFIG_FILS_SK_PFS */
3546
3547
  data = wpabuf_alloc(1000 + ielen + (pub ? wpabuf_len(pub) : 0));
3548
  if (!data) {
3549
    *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3550
    goto fail;
3551
  }
3552
3553
  /* TODO: FILS PK */
3554
#ifdef CONFIG_FILS_SK_PFS
3555
  if (pub) {
3556
    /* Finite Cyclic Group */
3557
    wpabuf_put_le16(data, hapd->conf->fils_dh_group);
3558
3559
    /* Element */
3560
    wpabuf_put_buf(data, pub);
3561
  }
3562
#endif /* CONFIG_FILS_SK_PFS */
3563
3564
  /* RSNE */
3565
  wpabuf_put_data(data, ie, ielen);
3566
3567
  /* MDE when using FILS+FT (already included in ie,ielen with RSNE) */
3568
3569
#ifdef CONFIG_IEEE80211R_AP
3570
  if (wpa_key_mgmt_ft(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
3571
    /* FTE[R1KH-ID,R0KH-ID] when using FILS+FT */
3572
    int res;
3573
3574
    res = wpa_auth_write_fte(hapd->wpa_auth, sta->wpa_sm,
3575
           wpabuf_put(data, 0),
3576
           wpabuf_tailroom(data));
3577
    if (res < 0) {
3578
      *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3579
      goto fail;
3580
    }
3581
    wpabuf_put(data, res);
3582
  }
3583
#endif /* CONFIG_IEEE80211R_AP */
3584
3585
  /* FILS Nonce */
3586
  wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
3587
  wpabuf_put_u8(data, 1 + NONCE_LEN); /* Length */
3588
  /* Element ID Extension */
3589
  wpabuf_put_u8(data, WLAN_EID_EXT_NONCE);
3590
  wpabuf_put_data(data, fils_nonce, NONCE_LEN);
3591
3592
  /* FILS Session */
3593
  wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
3594
  wpabuf_put_u8(data, 1 + FILS_SESSION_LEN); /* Length */
3595
  /* Element ID Extension */
3596
  wpabuf_put_u8(data, WLAN_EID_EXT_FILS_SESSION);
3597
  wpabuf_put_data(data, sta->fils_session, FILS_SESSION_LEN);
3598
3599
  /* Wrapped Data */
3600
  if (!pmksa && erp_resp) {
3601
    wpabuf_put_u8(data, WLAN_EID_EXTENSION); /* Element ID */
3602
    wpabuf_put_u8(data, 1 + wpabuf_len(erp_resp)); /* Length */
3603
    /* Element ID Extension */
3604
    wpabuf_put_u8(data, WLAN_EID_EXT_WRAPPED_DATA);
3605
    wpabuf_put_buf(data, erp_resp);
3606
3607
    if (fils_rmsk_to_pmk(wpa_auth_sta_key_mgmt(sta->wpa_sm),
3608
             msk, msk_len, sta->fils_snonce, fils_nonce,
3609
             sta->fils_dh_ss ?
3610
             wpabuf_head(sta->fils_dh_ss) : NULL,
3611
             sta->fils_dh_ss ?
3612
             wpabuf_len(sta->fils_dh_ss) : 0,
3613
             pmk_buf, &pmk_len)) {
3614
      wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
3615
      *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3616
      wpabuf_free(data);
3617
      data = NULL;
3618
      goto fail;
3619
    }
3620
    pmk = pmk_buf;
3621
3622
    /* Don't use DHss in PTK derivation if PMKSA caching is not
3623
     * used. */
3624
    wpabuf_clear_free(sta->fils_dh_ss);
3625
    sta->fils_dh_ss = NULL;
3626
3627
    if (sta->fils_erp_pmkid_set) {
3628
      /* TODO: get PMKLifetime from WPA parameters */
3629
      unsigned int dot11RSNAConfigPMKLifetime = 43200;
3630
      int session_timeout;
3631
3632
      session_timeout = dot11RSNAConfigPMKLifetime;
3633
      if (sta->session_timeout_set) {
3634
        struct os_reltime now, diff;
3635
3636
        os_get_reltime(&now);
3637
        os_reltime_sub(&sta->session_timeout, &now,
3638
                 &diff);
3639
        session_timeout = diff.sec;
3640
      }
3641
3642
      sta->fils_erp_pmkid_set = 0;
3643
      wpa_auth_add_fils_pmk_pmkid(sta->wpa_sm, pmk, pmk_len,
3644
                sta->fils_erp_pmkid);
3645
      if (!hapd->conf->disable_pmksa_caching &&
3646
          wpa_auth_pmksa_add2(
3647
            hapd->wpa_auth, sta->addr,
3648
            pmk, pmk_len,
3649
            sta->fils_erp_pmkid,
3650
            session_timeout,
3651
            wpa_auth_sta_key_mgmt(sta->wpa_sm),
3652
            NULL, ap_sta_is_mld(hapd, sta)) < 0) {
3653
        wpa_printf(MSG_ERROR,
3654
             "FILS: Failed to add PMKSA cache entry based on ERP");
3655
      }
3656
    }
3657
  } else if (pmksa) {
3658
    pmk = pmksa->pmk;
3659
    pmk_len = pmksa->pmk_len;
3660
  }
3661
3662
  if (!pmk) {
3663
    wpa_printf(MSG_DEBUG, "FILS: No PMK available");
3664
    *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3665
    wpabuf_free(data);
3666
    data = NULL;
3667
    goto fail;
3668
  }
3669
3670
  if (fils_auth_pmk_to_ptk(sta->wpa_sm, pmk, pmk_len,
3671
         sta->fils_snonce, fils_nonce,
3672
         sta->fils_dh_ss ?
3673
         wpabuf_head(sta->fils_dh_ss) : NULL,
3674
         sta->fils_dh_ss ?
3675
         wpabuf_len(sta->fils_dh_ss) : 0,
3676
         sta->fils_g_sta, pub) < 0) {
3677
    *resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
3678
    wpabuf_free(data);
3679
    data = NULL;
3680
    goto fail;
3681
  }
3682
3683
fail:
3684
  if (is_pub)
3685
    *is_pub = pub != NULL;
3686
  os_free(ie_buf);
3687
  wpabuf_free(pub);
3688
  wpabuf_clear_free(sta->fils_dh_ss);
3689
  sta->fils_dh_ss = NULL;
3690
#ifdef CONFIG_FILS_SK_PFS
3691
  crypto_ecdh_deinit(sta->fils_ecdh);
3692
  sta->fils_ecdh = NULL;
3693
#endif /* CONFIG_FILS_SK_PFS */
3694
  return data;
3695
}
3696
3697
3698
static void handle_auth_fils_finish(struct hostapd_data *hapd,
3699
            struct sta_info *sta, u16 resp,
3700
            struct wpabuf *data, int pub)
3701
{
3702
  u16 auth_alg;
3703
3704
  auth_alg = (pub ||
3705
        resp == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) ?
3706
    WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
3707
  send_auth_reply(hapd, sta, sta->addr, auth_alg, 2, resp,
3708
      data ? wpabuf_head(data) : (u8 *) "",
3709
      data ? wpabuf_len(data) : 0, "auth-fils-finish");
3710
  wpabuf_free(data);
3711
3712
  if (resp == WLAN_STATUS_SUCCESS) {
3713
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
3714
             HOSTAPD_LEVEL_DEBUG,
3715
             "authentication OK (FILS)");
3716
    sta->flags |= WLAN_STA_AUTH;
3717
    wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
3718
    sta->auth_alg = pub ? WLAN_AUTH_FILS_SK_PFS : WLAN_AUTH_FILS_SK;
3719
    mlme_authenticate_indication(hapd, sta);
3720
  }
3721
}
3722
3723
3724
void ieee802_11_finish_fils_auth(struct hostapd_data *hapd,
3725
         struct sta_info *sta, int success,
3726
         struct wpabuf *erp_resp,
3727
         const u8 *msk, size_t msk_len)
3728
{
3729
  u16 resp;
3730
  u32 flags = sta->flags;
3731
3732
  sta->flags &= ~(WLAN_STA_PENDING_FILS_ERP |
3733
      WLAN_STA_PENDING_PASN_FILS_ERP);
3734
3735
  resp = success ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE;
3736
3737
  if (flags & WLAN_STA_PENDING_FILS_ERP) {
3738
    struct wpabuf *data;
3739
    int pub = 0;
3740
3741
    if (!sta->fils_pending_cb)
3742
      return;
3743
3744
    data = prepare_auth_resp_fils(hapd, sta, &resp, NULL, erp_resp,
3745
                msk, msk_len, &pub);
3746
    if (!data) {
3747
      wpa_printf(MSG_DEBUG,
3748
           "%s: prepare_auth_resp_fils() failure",
3749
           __func__);
3750
    }
3751
    sta->fils_pending_cb(hapd, sta, resp, data, pub);
3752
#ifdef CONFIG_PASN
3753
  } else if (flags & WLAN_STA_PENDING_PASN_FILS_ERP) {
3754
    pasn_fils_auth_resp(hapd, sta, resp, erp_resp,
3755
            msk, msk_len);
3756
#endif /* CONFIG_PASN */
3757
  }
3758
}
3759
3760
#endif /* CONFIG_FILS */
3761
3762
3763
static int ieee802_11_allowed_address(struct hostapd_data *hapd, const u8 *addr,
3764
              const u8 *msg, size_t len,
3765
              struct radius_sta *info)
3766
0
{
3767
0
  int res;
3768
3769
0
  res = hostapd_allowed_address(hapd, addr, msg, len, info, 0);
3770
3771
0
  if (res == HOSTAPD_ACL_REJECT) {
3772
0
    wpa_printf(MSG_DEBUG, "Station " MACSTR
3773
0
         " not allowed to authenticate",
3774
0
         MAC2STR(addr));
3775
0
    return HOSTAPD_ACL_REJECT;
3776
0
  }
3777
3778
0
  if (res == HOSTAPD_ACL_PENDING) {
3779
0
    wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
3780
0
         " waiting for an external authentication",
3781
0
         MAC2STR(addr));
3782
    /* Authentication code will re-send the authentication frame
3783
     * after it has received (and cached) information from the
3784
     * external source. */
3785
0
    return HOSTAPD_ACL_PENDING;
3786
0
  }
3787
3788
0
  return res;
3789
0
}
3790
3791
3792
int ieee802_11_set_radius_info(struct hostapd_data *hapd, struct sta_info *sta,
3793
             int res, struct radius_sta *info)
3794
0
{
3795
0
  u32 session_timeout = info->session_timeout;
3796
0
  u32 acct_interim_interval = info->acct_interim_interval;
3797
0
  struct vlan_description *vlan_id = &info->vlan_id;
3798
0
  struct hostapd_sta_wpa_psk_short *psk = info->psk;
3799
0
  char *identity = info->identity;
3800
0
  char *radius_cui = info->radius_cui;
3801
3802
0
  if (vlan_id->notempty &&
3803
0
      !hostapd_vlan_valid(hapd->conf->vlan, vlan_id)) {
3804
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
3805
0
             HOSTAPD_LEVEL_INFO,
3806
0
             "Invalid VLAN %d%s received from RADIUS server",
3807
0
             vlan_id->untagged,
3808
0
             vlan_id->tagged[0] ? "+" : "");
3809
0
    return -1;
3810
0
  }
3811
0
  if (ap_sta_set_vlan(hapd, sta, vlan_id) < 0)
3812
0
    return -1;
3813
0
  if (sta->vlan_id)
3814
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
3815
0
             HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
3816
3817
0
  hostapd_free_psk_list(sta->psk);
3818
0
  if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED)
3819
0
    hostapd_copy_psk_list(&sta->psk, psk);
3820
0
  else
3821
0
    sta->psk = NULL;
3822
3823
0
  os_free(sta->identity);
3824
0
  if (identity)
3825
0
    sta->identity = os_strdup(identity);
3826
0
  else
3827
0
    sta->identity = NULL;
3828
3829
0
  os_free(sta->radius_cui);
3830
0
  if (radius_cui)
3831
0
    sta->radius_cui = os_strdup(radius_cui);
3832
0
  else
3833
0
    sta->radius_cui = NULL;
3834
3835
0
  if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
3836
0
    sta->acct_interim_interval = acct_interim_interval;
3837
0
  if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT) {
3838
0
    sta->session_timeout_set = 1;
3839
0
    os_get_reltime(&sta->session_timeout);
3840
0
    sta->session_timeout.sec += session_timeout;
3841
0
    ap_sta_session_timeout(hapd, sta, session_timeout);
3842
0
  } else {
3843
0
    sta->session_timeout_set = 0;
3844
0
    ap_sta_no_session_timeout(hapd, sta);
3845
0
  }
3846
3847
0
  return 0;
3848
0
}
3849
3850
3851
#ifdef CONFIG_PASN
3852
#ifdef CONFIG_FILS
3853
3854
static void pasn_fils_auth_resp(struct hostapd_data *hapd,
3855
        struct sta_info *sta, u16 status,
3856
        struct wpabuf *erp_resp,
3857
        const u8 *msk, size_t msk_len)
3858
{
3859
  struct pasn_data *pasn = sta->pasn;
3860
  struct pasn_fils *fils = &pasn->fils;
3861
  u8 pmk[PMK_LEN_MAX];
3862
  size_t pmk_len;
3863
  int ret;
3864
3865
  wpa_printf(MSG_DEBUG, "PASN: FILS: Handle AS response - status=%u",
3866
       status);
3867
3868
  if (status != WLAN_STATUS_SUCCESS)
3869
    goto fail;
3870
3871
  if (!pasn->secret) {
3872
    wpa_printf(MSG_DEBUG, "PASN: FILS: Missing secret");
3873
    goto fail;
3874
  }
3875
3876
  if (random_get_bytes(fils->anonce, NONCE_LEN) < 0) {
3877
    wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to get ANonce");
3878
    goto fail;
3879
  }
3880
3881
  wpa_hexdump(MSG_DEBUG, "RSN: Generated FILS ANonce",
3882
        fils->anonce, NONCE_LEN);
3883
3884
  ret = fils_rmsk_to_pmk(pasn_get_akmp(pasn), msk, msk_len, fils->nonce,
3885
             fils->anonce, NULL, 0, pmk, &pmk_len);
3886
  if (ret) {
3887
    wpa_printf(MSG_DEBUG, "FILS: Failed to derive PMK");
3888
    goto fail;
3889
  }
3890
3891
  ret = pasn_pmk_to_ptk(pmk, pmk_len, sta->addr, hapd->own_addr,
3892
            wpabuf_head(pasn->secret),
3893
            wpabuf_len(pasn->secret),
3894
            pasn_get_ptk(sta->pasn), pasn_get_akmp(sta->pasn),
3895
            pasn_get_cipher(sta->pasn), sta->pasn->kdk_len,
3896
            sta->pasn->kek_len, &sta->pasn->hash_alg,
3897
            pasn->auth_alg == WLAN_AUTH_EPPKE);
3898
  if (ret) {
3899
    wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to derive PTK");
3900
    goto fail;
3901
  }
3902
3903
  if (pasn->secure_ltf) {
3904
    ret = wpa_ltf_keyseed(pasn_get_ptk(pasn), pasn_get_akmp(pasn),
3905
              pasn_get_cipher(pasn));
3906
    if (ret) {
3907
      wpa_printf(MSG_DEBUG,
3908
           "PASN: FILS: Failed to derive LTF keyseed");
3909
      goto fail;
3910
    }
3911
  }
3912
3913
  wpa_printf(MSG_DEBUG, "PASN: PTK successfully derived");
3914
3915
  wpabuf_free(pasn->secret);
3916
  pasn->secret = NULL;
3917
3918
  fils->erp_resp = erp_resp;
3919
  ret = handle_auth_pasn_resp(sta->pasn, hapd->own_addr, sta->addr, NULL,
3920
            WLAN_STATUS_SUCCESS);
3921
  wpabuf_free(pasn->frame);
3922
  pasn->frame = NULL;
3923
  fils->erp_resp = NULL;
3924
3925
  if (ret) {
3926
    wpa_printf(MSG_DEBUG, "PASN: FILS: Failed to send response");
3927
    goto fail;
3928
  }
3929
3930
  fils->state = PASN_FILS_STATE_COMPLETE;
3931
  return;
3932
fail:
3933
  ap_free_sta(hapd, sta);
3934
}
3935
3936
3937
static int pasn_wd_handle_fils(struct hostapd_data *hapd, struct sta_info *sta,
3938
             struct wpabuf *wd)
3939
{
3940
#ifdef CONFIG_NO_RADIUS
3941
  wpa_printf(MSG_DEBUG, "PASN: FILS: RADIUS is not configured. Fail");
3942
  return -1;
3943
#else /* CONFIG_NO_RADIUS */
3944
  struct pasn_data *pasn = sta->pasn;
3945
  struct pasn_fils *fils = &pasn->fils;
3946
  struct ieee802_11_elems elems;
3947
  struct wpa_ie_data rsne_data;
3948
  struct wpabuf *fils_wd;
3949
  const u8 *data;
3950
  size_t buf_len;
3951
  u16 alg, seq, status;
3952
  int ret;
3953
3954
  if (fils->state != PASN_FILS_STATE_NONE) {
3955
    wpa_printf(MSG_DEBUG, "PASN: FILS: Not expecting wrapped data");
3956
    return -1;
3957
  }
3958
3959
  if (!wd) {
3960
    wpa_printf(MSG_DEBUG, "PASN: FILS: No wrapped data");
3961
    return -1;
3962
  }
3963
3964
  data = wpabuf_head_u8(wd);
3965
  buf_len = wpabuf_len(wd);
3966
3967
  if (buf_len < 6) {
3968
    wpa_printf(MSG_DEBUG, "PASN: FILS: Buffer too short. len=%zu",
3969
         buf_len);
3970
    return -1;
3971
  }
3972
3973
  alg = WPA_GET_LE16(data);
3974
  seq = WPA_GET_LE16(data + 2);
3975
  status = WPA_GET_LE16(data + 4);
3976
3977
  wpa_printf(MSG_DEBUG, "PASN: FILS: alg=%u, seq=%u, status=%u",
3978
       alg, seq, status);
3979
3980
  if (alg != WLAN_AUTH_FILS_SK || seq != 1 ||
3981
      status != WLAN_STATUS_SUCCESS) {
3982
    wpa_printf(MSG_DEBUG,
3983
         "PASN: FILS: Dropping peer authentication");
3984
    return -1;
3985
  }
3986
3987
  data += 6;
3988
  buf_len -= 6;
3989
3990
  if (ieee802_11_parse_elems(data, buf_len, &elems, 1) == ParseFailed) {
3991
    wpa_printf(MSG_DEBUG, "PASN: FILS: Could not parse elements");
3992
    return -1;
3993
  }
3994
3995
  if (!elems.rsn_ie || !elems.nonce || !elems.nonce ||
3996
      !elems.wrapped_data || !elems.fils_session) {
3997
    wpa_printf(MSG_DEBUG, "PASN: FILS: Missing IEs");
3998
    return -1;
3999
  }
4000
4001
  ret = wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4002
           &rsne_data);
4003
  if (ret) {
4004
    wpa_printf(MSG_DEBUG, "PASN: FILS: Failed parsing RSNE");
4005
    return -1;
4006
  }
4007
4008
  ret = wpa_pasn_validate_rsne(&rsne_data, false);
4009
  if (ret) {
4010
    wpa_printf(MSG_DEBUG, "PASN: FILS: Failed validating RSNE");
4011
    return -1;
4012
  }
4013
4014
  if (rsne_data.num_pmkid) {
4015
    wpa_printf(MSG_DEBUG,
4016
         "PASN: FILS: Not expecting PMKID in RSNE");
4017
    return -1;
4018
  }
4019
4020
  wpa_hexdump(MSG_DEBUG, "PASN: FILS: Nonce", elems.nonce, NONCE_LEN);
4021
  os_memcpy(fils->nonce, elems.nonce, NONCE_LEN);
4022
4023
  wpa_hexdump(MSG_DEBUG, "PASN: FILS: Session", elems.fils_session,
4024
        FILS_SESSION_LEN);
4025
  os_memcpy(fils->session, elems.fils_session, FILS_SESSION_LEN);
4026
4027
  fils_wd = ieee802_11_defrag(elems.wrapped_data, elems.wrapped_data_len,
4028
            true);
4029
4030
  if (!fils_wd) {
4031
    wpa_printf(MSG_DEBUG, "PASN: FILS: Missing wrapped data");
4032
    return -1;
4033
  }
4034
4035
  if (!sta->eapol_sm)
4036
    sta->eapol_sm = ieee802_1x_alloc_eapol_sm(hapd, sta);
4037
4038
  wpa_printf(MSG_DEBUG,
4039
       "PASN: FILS: Forward EAP-Initiate/Re-auth to AS");
4040
4041
  ieee802_1x_encapsulate_radius(hapd, sta, wpabuf_head(fils_wd),
4042
              wpabuf_len(fils_wd));
4043
4044
  sta->flags |= WLAN_STA_PENDING_PASN_FILS_ERP;
4045
4046
  fils->state = PASN_FILS_STATE_PENDING_AS;
4047
4048
  /*
4049
   * Calculate pending PMKID here so that we do not need to maintain a
4050
   * copy of the EAP-Initiate/Reautt message.
4051
   */
4052
  fils_pmkid_erp(pasn_get_akmp(pasn),
4053
           wpabuf_head(fils_wd), wpabuf_len(fils_wd),
4054
           fils->erp_pmkid);
4055
4056
  wpabuf_free(fils_wd);
4057
  return 0;
4058
#endif /* CONFIG_NO_RADIUS */
4059
}
4060
4061
#endif /* CONFIG_FILS */
4062
4063
4064
static int hapd_pasn_send_mlme(void *ctx, const u8 *data, size_t data_len,
4065
             int noack, unsigned int freq, unsigned int wait)
4066
{
4067
  struct hostapd_data *hapd = ctx;
4068
4069
  return hostapd_drv_send_mlme(hapd, data, data_len, 0, NULL, 0, 0);
4070
}
4071
4072
4073
static struct rsn_pmksa_cache_entry *
4074
pmksa_cache_search(void *ctx, const u8 *spa, const u8 *pmkid, bool is_ml)
4075
{
4076
  struct hostapd_data *hapd = ctx;
4077
  struct rsn_pmksa_cache_entry *entry;
4078
  struct rsn_pmksa_cache *pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth,
4079
                 is_ml);
4080
4081
  entry = pmksa_cache_auth_get(pmksa, spa, pmkid);
4082
  if (entry)
4083
    return entry;
4084
4085
#ifdef CONFIG_IEEE80211BE
4086
  if (is_ml) {
4087
    struct hostapd_data *tmp_hapd;
4088
4089
    /* Search in link caches of each affiliated AP MLD link */
4090
    for_each_mld_link(tmp_hapd, hapd) {
4091
      pmksa = wpa_auth_get_pmksa_cache(tmp_hapd->wpa_auth,
4092
               false);
4093
      entry = pmksa_cache_auth_get(pmksa, spa, pmkid);
4094
      if (entry)
4095
        return entry;
4096
    }
4097
  } else if (hapd->conf->mld_ap) {
4098
    /* Search in the MLD cache */
4099
    pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth, true);
4100
    entry = pmksa_cache_auth_get(pmksa, spa, pmkid);
4101
    if (entry)
4102
      return entry;
4103
  }
4104
#endif /* CONFIG_IEEE80211BE */
4105
4106
  return NULL;
4107
}
4108
4109
4110
#ifdef CONFIG_ENC_ASSOC
4111
static int eppke_set_key(void *ctx, enum wpa_alg alg, const u8 *addr,
4112
       int vlan_id, const u8 *key, size_t key_len)
4113
{
4114
  struct hostapd_data *hapd = ctx;
4115
4116
  return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr,
4117
           0, vlan_id, 1, NULL, 0, key, key_len,
4118
           KEY_FLAG_PAIRWISE_RX_TX);
4119
}
4120
#else /* CONFIG_ENC_ASSOC */
4121
#define eppke_set_key NULL
4122
#endif /* CONFIG_ENC_ASSOC */
4123
4124
4125
#ifdef CONFIG_SAE
4126
/**
4127
 * hapd_pasn_get_pt_for_pw_id - Look up SAE PT for a password identifier
4128
 *
4129
 * Called by the PASN responder when an SAE commit frame contains a password
4130
 * identifier that was not known at PASN-setup time (e.g., for EPPKE where
4131
 * the PT cannot be pre-selected before the commit is received).
4132
 *
4133
 * For plaintext identifiers sae_get_password() returns the pre-computed PT
4134
 * (pw_entry->pt). We must NOT return that pointer directly because the
4135
 * caller will free it; clone it so the caller always owns the returned PT.
4136
 * counter and dec_pw_id are set to 0/NULL for plaintext identifiers.
4137
 *
4138
 * For encrypted password identifiers the PT is not pre-computed (the
4139
 * pre-computed PT is keyed to the decrypted identifier, not the encrypted
4140
 * one). Derive the PT on-the-fly using the raw (encrypted) identifier as
4141
 * the salt, mirroring what auth_build_sae_commit() does for regular SAE.
4142
 * The decrypted blob is parsed to extract the real password identifier
4143
 * (dec_pw_id) and the counter, both returned to the caller. dec_pw_id is
4144
 * heap-allocated and the caller takes ownership (must free with os_free()).
4145
 */
4146
static struct sae_pt *
4147
hapd_pasn_get_pt_for_pw_id(void *ctx, const u8 *pw_id, size_t pw_id_len,
4148
          int group, const char **password,
4149
          unsigned int *counter,
4150
          u8 **dec_pw_id, size_t *dec_pw_id_len)
4151
{
4152
  struct hostapd_data *hapd = ctx;
4153
  struct sae_password_entry *pw_entry = NULL;
4154
  struct sae_pt *pt = NULL;
4155
  int groups[2] = { group, 0 };
4156
4157
  *counter = 0;
4158
  *dec_pw_id = NULL;
4159
  *dec_pw_id_len = 0;
4160
4161
  *password = sae_get_password(hapd, NULL, pw_id, pw_id_len, &pw_entry,
4162
             &pt, NULL);
4163
  if (!*password)
4164
    return NULL;
4165
4166
  if (pt) {
4167
    /* Plaintext identifier: sae_get_password() found a
4168
     * pre-computed PT.  Clone it so the caller can free it
4169
     * without affecting the password entry's own PT.
4170
     * counter and dec_pw_id stay 0/NULL for plaintext. */
4171
    return sae_derive_pt(groups, hapd->conf->ssid.ssid,
4172
             hapd->conf->ssid.ssid_len,
4173
             (const u8 *) pw_entry->password,
4174
             os_strlen(pw_entry->password),
4175
             pw_id, pw_id_len);
4176
  }
4177
4178
  if (pw_entry) {
4179
    /* Encrypted identifier: no pre-computed PT exists for the
4180
     * raw (encrypted) identifier. Decrypt the blob once to
4181
     * extract the real password identifier (dec_pw_id) and the
4182
     * counter, then derive the PT using the encrypted bytes as
4183
     * the salt (matching auth_build_sae_commit()).
4184
     *
4185
     * Decrypted format:
4186
     *   4-byte date | Password ID | NUL padding | 4-byte counter
4187
     */
4188
    if (hapd->conf->sae_pw_id_key &&
4189
        pw_id_len > 4 + 4 + AES_BLOCK_SIZE) {
4190
      u8 *plain;
4191
      size_t plain_len;
4192
4193
      plain = os_malloc(pw_id_len);
4194
      if (plain &&
4195
          aes_siv_decrypt(
4196
            wpabuf_head(hapd->conf->sae_pw_id_key),
4197
            wpabuf_len(hapd->conf->sae_pw_id_key),
4198
            pw_id, pw_id_len,
4199
            0, NULL, NULL, plain) == 0) {
4200
        const u8 *id, *pos;
4201
4202
        plain_len = pw_id_len - AES_BLOCK_SIZE;
4203
        /* Counter is the last 4 bytes */
4204
        *counter = WPA_GET_BE32(plain + plain_len - 4);
4205
        wpa_printf(MSG_DEBUG,
4206
             "SAE: Generation time %u counter %u",
4207
             WPA_GET_BE32(plain), *counter);
4208
        /* Real password ID starts at byte 4,
4209
         * NUL-terminated before the counter */
4210
        id = plain + 4;
4211
        pos = id;
4212
        while (pos < plain + plain_len - 4) {
4213
          if (*pos == 0x00)
4214
            break;
4215
          pos++;
4216
        }
4217
        *dec_pw_id_len = pos - id;
4218
        wpa_hexdump_ascii(
4219
          MSG_DEBUG,
4220
          "SAE: Decrypted password identifier",
4221
          id, *dec_pw_id_len);
4222
        *dec_pw_id = os_memdup(id, *dec_pw_id_len);
4223
        if (!*dec_pw_id)
4224
          *dec_pw_id_len = 0;
4225
      }
4226
      os_free(plain);
4227
    }
4228
4229
    pt = sae_derive_pt(groups, hapd->conf->ssid.ssid,
4230
           hapd->conf->ssid.ssid_len,
4231
           (const u8 *) pw_entry->password,
4232
           os_strlen(pw_entry->password),
4233
           pw_id, pw_id_len);
4234
    if (!pt)
4235
      wpa_printf(MSG_DEBUG,
4236
           "PASN: Failed to derive PT for encrypted password identifier");
4237
    return pt;
4238
  }
4239
4240
  return NULL;
4241
}
4242
#endif /* CONFIG_SAE */
4243
4244
4245
static void hapd_initialize_pasn(struct hostapd_data *hapd,
4246
         struct sta_info *sta)
4247
{
4248
  struct pasn_data *pasn = sta->pasn;
4249
4250
  pasn_register_callbacks(pasn, hapd, hapd_pasn_send_mlme,
4251
        NULL, eppke_set_key, pmksa_cache_search);
4252
  pasn_set_bssid(pasn, hapd->own_addr);
4253
  pasn_set_own_addr(pasn, hapd->own_addr);
4254
#ifdef CONFIG_PMKSA_PRIVACY
4255
  pasn->pmksa_caching_privacy = hapd->conf->pmksa_caching_privacy;
4256
#endif /* CONFIG_PMKSA_PRIVACY */
4257
#if defined(CONFIG_IEEE80211BE) && defined(CONFIG_ENC_ASSOC)
4258
  /* Per IEEE802.11bi/D4.0, 12.16.9 (Enhanced privacy
4259
   * protection key exchange), if (Re)Association frame
4260
   * Encryption is activated, KEK in PASN shall be true.
4261
   */
4262
  if (hapd->conf->assoc_frame_encryption)
4263
    pasn->derive_kek = true;
4264
  if (hapd->conf->mld_ap)
4265
    pasn_set_own_mld_addr(pasn, hapd->mld->mld_addr);
4266
#endif /* CONFIG_IEEE80211BE && CONFIG_ENC_ASSOC */
4267
  pasn_set_peer_addr(pasn, sta->addr);
4268
  pasn_set_wpa_key_mgmt(pasn, hostapd_config_all_key_mgmt(hapd->conf));
4269
  pasn_set_rsn_pairwise(pasn, hapd->conf->rsn_pairwise |
4270
            hapd->conf->rsn_override_pairwise);
4271
  pasn_set_mfp(pasn, hapd->conf->ieee80211w);
4272
  os_free(pasn->pasn_groups);
4273
  pasn->pasn_groups = int_array_dup(hapd->conf->pasn_groups);
4274
  pasn->noauth = hapd->conf->pasn_noauth;
4275
#ifdef CONFIG_ENC_ASSOC
4276
  pasn->eppke_unauth = hapd->conf->eppke_unauth;
4277
#endif /* CONFIG_ENC_ASSOC */
4278
  if (hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_AP)
4279
    pasn_enable_kdk_derivation(pasn);
4280
4281
#ifdef CONFIG_TESTING_OPTIONS
4282
  pasn->corrupt_mic = hapd->conf->pasn_corrupt_mic;
4283
  pasn->pasn_test_groups = hapd->conf->pasn_test_groups;
4284
  if (hapd->conf->force_kdk_derivation)
4285
    pasn_enable_kdk_derivation(pasn);
4286
#endif /* CONFIG_TESTING_OPTIONS */
4287
  pasn->use_anti_clogging = use_anti_clogging(hapd);
4288
  sae_get_password(hapd, sta, NULL, 0, NULL, &pasn->pt, NULL);
4289
#ifdef CONFIG_SAE
4290
  /* Register a callback so the PASN responder can look up the correct
4291
   * SAE PT when the STA's commit frame contains a password identifier
4292
   * that was not known at setup time (EPPKE cases).
4293
   */
4294
  pasn->get_pt_for_pw_id = hapd_pasn_get_pt_for_pw_id;
4295
#endif /* CONFIG_SAE */
4296
  pasn_set_rsne(pasn, wpa_auth_get_wpa_ie(hapd->wpa_auth,
4297
            &pasn->rsn_ie_len));
4298
  pasn_set_rsnxe_ie(pasn, hostapd_wpa_ie(hapd, WLAN_EID_RSNX));
4299
  pasn->disable_pmksa_caching = hapd->conf->disable_pmksa_caching;
4300
#ifdef CONFIG_ENC_ASSOC
4301
  pasn->tk_configured = false;
4302
#endif /* CONFIG_ENC_ASSOC */
4303
  pasn_set_responder_pmksa(
4304
    pasn,
4305
    wpa_auth_get_pmksa_cache(hapd->wpa_auth,
4306
           ap_sta_is_epp(sta) ?
4307
           ap_sta_is_mld(hapd, sta) : false));
4308
4309
  pasn->comeback_after = hapd->conf->pasn_comeback_after;
4310
  pasn->comeback_idx = hapd->comeback_idx;
4311
  pasn->comeback_key =  hapd->comeback_key;
4312
  pasn->comeback_pending_idx = hapd->comeback_pending_idx;
4313
4314
  pasn->sec_prof_enabled = hapd->conf->security_profiles;
4315
}
4316
4317
4318
static int pasn_set_keys_from_cache(struct hostapd_data *hapd,
4319
            const u8 *own_addr, const u8 *sta_addr,
4320
            int cipher, int akmp)
4321
{
4322
  struct ptksa_cache_entry *entry;
4323
4324
  entry = ptksa_cache_get(hapd->ptksa, sta_addr, cipher);
4325
  if (!entry) {
4326
    wpa_printf(MSG_DEBUG, "PASN: peer " MACSTR
4327
         " not present in PTKSA cache", MAC2STR(sta_addr));
4328
    return -1;
4329
  }
4330
4331
  if (!ether_addr_equal(entry->own_addr, own_addr)) {
4332
    wpa_printf(MSG_DEBUG,
4333
         "PASN: own addr " MACSTR " and PTKSA entry own addr "
4334
         MACSTR " differ",
4335
         MAC2STR(own_addr), MAC2STR(entry->own_addr));
4336
    return -1;
4337
  }
4338
4339
  wpa_printf(MSG_DEBUG, "PASN: " MACSTR " present in PTKSA cache",
4340
       MAC2STR(sta_addr));
4341
  hostapd_drv_set_secure_ranging_ctx(hapd, own_addr, sta_addr, cipher,
4342
             entry->ptk.tk_len, entry->ptk.tk,
4343
             entry->ptk.ltf_keyseed_len,
4344
             entry->ptk.ltf_keyseed, 0);
4345
4346
  return 0;
4347
}
4348
4349
4350
static void hapd_pasn_update_params(struct hostapd_data *hapd,
4351
            struct sta_info *sta,
4352
            const struct ieee80211_mgmt *mgmt,
4353
            size_t len)
4354
{
4355
  struct pasn_data *pasn = sta->pasn;
4356
  struct ieee802_11_elems elems;
4357
  struct wpa_ie_data rsn_data;
4358
#ifdef CONFIG_FILS
4359
  struct wpa_pasn_params_data pasn_params;
4360
  struct wpabuf *wrapped_data = NULL;
4361
#endif /* CONFIG_FILS */
4362
  int akmp;
4363
4364
  if (ieee802_11_parse_elems(mgmt->u.auth.variable,
4365
           len - offsetof(struct ieee80211_mgmt,
4366
              u.auth.variable),
4367
           &elems, 0) == ParseFailed) {
4368
    wpa_printf(MSG_DEBUG,
4369
         "PASN: Failed parsing Authentication frame");
4370
    return;
4371
  }
4372
4373
  if (!elems.rsn_ie ||
4374
      wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4375
         &rsn_data)) {
4376
    wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE");
4377
    return;
4378
  }
4379
4380
  if (!(rsn_data.key_mgmt & pasn->wpa_key_mgmt)) {
4381
    wpa_printf(MSG_DEBUG, "PASN: Mismatch in AKMP");
4382
    return;
4383
  }
4384
4385
  if (hapd->conf->security_profiles && sta->sec_prof_used) {
4386
    if (!(rsn_data.pairwise_cipher & WPA_CIPHER_GCMP_256)) {
4387
      wpa_printf(MSG_DEBUG,
4388
           "PASN: Pairwise cipher not allowed for security profile");
4389
      return;
4390
    }
4391
  } else {
4392
    if (!(rsn_data.pairwise_cipher & pasn->rsn_pairwise)) {
4393
      wpa_printf(MSG_DEBUG, "PASN: Mismatch in cipher");
4394
      return;
4395
    }
4396
  }
4397
4398
#ifdef CONFIG_ENC_ASSOC
4399
  pasn->auth_alg = mgmt->u.auth.auth_alg;
4400
  pasn->authorized = ap_sta_is_authorized(sta);
4401
#ifdef CONFIG_IEEE80211BE
4402
  pasn->is_ml_peer = sta->mld_info.mld_sta;
4403
#endif /* CONFIG_IEEE80211BE */
4404
#endif /* CONFIG_ENC_ASSOC */
4405
4406
  pasn_set_akmp(pasn, rsn_data.key_mgmt);
4407
  pasn_set_cipher(pasn, rsn_data.pairwise_cipher);
4408
4409
  if (pasn->derive_kdk &&
4410
      !ieee802_11_rsnx_capab_len(elems.rsnxe, elems.rsnxe_len,
4411
               WLAN_RSNX_CAPAB_SECURE_LTF))
4412
    pasn_disable_kdk_derivation(pasn);
4413
#ifdef CONFIG_TESTING_OPTIONS
4414
  if (hapd->conf->force_kdk_derivation)
4415
    pasn_enable_kdk_derivation(pasn);
4416
#endif /* CONFIG_TESTING_OPTIONS */
4417
  akmp = pasn_get_akmp(pasn);
4418
4419
  if (wpa_key_mgmt_ft(akmp) && rsn_data.num_pmkid) {
4420
#ifdef CONFIG_IEEE80211R_AP
4421
    pasn->pmk_r1_len = 0;
4422
    wpa_ft_fetch_pmk_r1(hapd->wpa_auth, sta->addr,
4423
            rsn_data.pmkid,
4424
            pasn->pmk_r1, &pasn->pmk_r1_len, NULL,
4425
            NULL, NULL, NULL,
4426
            NULL, NULL, NULL);
4427
#endif /* CONFIG_IEEE80211R_AP */
4428
  }
4429
#ifdef CONFIG_FILS
4430
  if (akmp != WPA_KEY_MGMT_FILS_SHA256 &&
4431
      akmp != WPA_KEY_MGMT_FILS_SHA384)
4432
    return;
4433
  if (!elems.pasn_params ||
4434
      wpa_pasn_parse_parameter_ie(elems.pasn_params - 3,
4435
          elems.pasn_params_len + 3,
4436
          false, &pasn_params)) {
4437
    wpa_printf(MSG_DEBUG,
4438
         "PASN: Failed validation of PASN Parameters element");
4439
    return;
4440
  }
4441
  if (pasn_params.wrapped_data_format != WPA_PASN_WRAPPED_DATA_NO) {
4442
    wrapped_data = ieee802_11_defrag(elems.wrapped_data,
4443
             elems.wrapped_data_len, true);
4444
    if (!wrapped_data) {
4445
      wpa_printf(MSG_DEBUG, "PASN: Missing wrapped data");
4446
      return;
4447
    }
4448
    if (pasn_wd_handle_fils(hapd, sta, wrapped_data))
4449
      wpa_printf(MSG_DEBUG,
4450
           "PASN: Failed processing FILS wrapped data");
4451
    else
4452
      pasn->fils_wd_valid = true;
4453
  }
4454
  wpabuf_free(wrapped_data);
4455
#endif /* CONFIG_FILS */
4456
}
4457
4458
4459
static void handle_auth_pasn(struct hostapd_data *hapd, struct sta_info *sta,
4460
           const struct ieee80211_mgmt *mgmt, size_t len,
4461
           u16 trans_seq, u16 status)
4462
{
4463
  int ret;
4464
  struct ieee802_11_elems elems;
4465
4466
  if (len < offsetof(struct ieee80211_mgmt, u.auth.variable)) {
4467
    wpa_printf(MSG_DEBUG, "PASN: Too short Management frame");
4468
    return;
4469
  }
4470
4471
  if (ieee802_11_parse_elems(mgmt->u.auth.variable,
4472
           len - offsetof(struct ieee80211_mgmt,
4473
              u.auth.variable),
4474
           &elems, 1) == ParseFailed) {
4475
    wpa_printf(MSG_DEBUG,
4476
         "PASN: Failed parsing Authentication frame");
4477
    return;
4478
  }
4479
4480
#ifdef CONFIG_P2P
4481
  if ((hapd->conf->p2p & (P2P_ENABLED | P2P_GROUP_OWNER)) ==
4482
      (P2P_ENABLED | P2P_GROUP_OWNER) &&
4483
      hapd->p2p && elems.p2p2_ie && elems.p2p2_ie_len) {
4484
    p2p_pasn_auth_rx(hapd->p2p, mgmt, len, hapd->iface->freq);
4485
    return;
4486
  }
4487
#endif /* CONFIG_P2P */
4488
4489
  if (hapd->conf->wpa != WPA_PROTO_RSN) {
4490
    wpa_printf(MSG_INFO, "PASN: RSN is not configured");
4491
    return;
4492
  }
4493
4494
  wpa_printf(MSG_INFO, "PASN authentication: sta=" MACSTR,
4495
       MAC2STR(sta->addr));
4496
4497
  if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH1) {
4498
    if (sta->pasn && sta->auth_alg != WLAN_AUTH_EPPKE) {
4499
      wpa_printf(MSG_DEBUG,
4500
           "PASN: Not expecting transaction == 1");
4501
      return;
4502
    }
4503
4504
    if (status != WLAN_STATUS_SUCCESS) {
4505
      wpa_printf(MSG_DEBUG,
4506
           "PASN: Failure status in transaction == 1");
4507
      return;
4508
    }
4509
4510
    if (!sta->pasn)
4511
      sta->pasn = pasn_data_init();
4512
    if (!sta->pasn) {
4513
      wpa_printf(MSG_DEBUG,
4514
           "PASN: Failed to allocate PASN context");
4515
      return;
4516
    }
4517
4518
    hapd_initialize_pasn(hapd, sta);
4519
4520
    /* Security Profile element validation during PASN frame 1.
4521
     * If the first Authentication frame includes an RSNE, RSNXE,
4522
     * and a Security Profile element the AP must verify these
4523
     * match an advertised profile; reject with
4524
     * REJECTED_INVALID_SECURITY_PROFILE on mismatch.
4525
     */
4526
    if (hapd->conf->security_profiles &&
4527
        validate_security_profile(hapd, sta, &elems, "PASN",
4528
                NULL)) {
4529
      wpa_printf(MSG_INFO, "PASN: Rejecting auth from " MACSTR
4530
           " - security profile mismatch",
4531
           MAC2STR(sta->addr));
4532
      send_auth_reply(
4533
        hapd, sta, sta->addr,
4534
        le_to_host16(mgmt->u.auth.auth_alg),
4535
        WLAN_AUTH_TR_SEQ_PASN_AUTH2,
4536
        WLAN_STATUS_REJECTED_INVALID_SECURITY_PROFILE,
4537
        NULL, 0,
4538
        "pasn-sec-profile-reject");
4539
      ap_free_sta(hapd, sta);
4540
      return;
4541
    }
4542
4543
    if (sta->sec_prof_used)
4544
      sta->pasn->sec_prof_used = true;
4545
4546
    hapd_pasn_update_params(hapd, sta, mgmt, len);
4547
    ret = handle_auth_pasn_1(sta->pasn, hapd->own_addr, sta->addr,
4548
           mgmt, len, false);
4549
    wpabuf_free(sta->pasn->frame);
4550
    sta->pasn->frame = NULL;
4551
    if (ret < 0) {
4552
      hostapd_drv_set_secure_ranging_ctx(hapd, hapd->own_addr,
4553
                 sta->addr, 0, 0,
4554
                 NULL, 0, NULL, 1);
4555
      ap_free_sta(hapd, sta);
4556
    }
4557
  } else if (trans_seq == WLAN_AUTH_TR_SEQ_PASN_AUTH3) {
4558
    if (!sta->pasn) {
4559
      wpa_printf(MSG_DEBUG,
4560
           "PASN: Not expecting transaction == 3");
4561
      return;
4562
    }
4563
4564
    if (status != WLAN_STATUS_SUCCESS) {
4565
      wpa_printf(MSG_DEBUG,
4566
           "PASN: Failure status in transaction == 3");
4567
      ap_free_sta_pasn(hapd, sta);
4568
      return;
4569
    }
4570
4571
    ret = handle_auth_pasn_3(sta->pasn, hapd->own_addr, sta->addr,
4572
           mgmt, len);
4573
    if (ret == 0) {
4574
#ifdef CONFIG_ENC_ASSOC
4575
      if (ap_sta_is_epp(sta)) {
4576
        sta->auth_alg = WLAN_AUTH_EPPKE;
4577
        sta->flags |= WLAN_STA_AUTH;
4578
      }
4579
#endif /* CONFIG_ENC_ASSOC */
4580
      ptksa_cache_add(hapd->ptksa, hapd->own_addr, sta->addr,
4581
          pasn_get_cipher(sta->pasn), 43200,
4582
          pasn_get_ptk(sta->pasn), NULL, NULL,
4583
          pasn_get_akmp(sta->pasn),
4584
          sta->pasn->auth_alg);
4585
#ifdef CONFIG_ENC_ASSOC
4586
      /* TODO: Support VLAN ID assignment based on configured
4587
       * SAE passwords. */
4588
      if (ap_sta_is_epp(sta) && !sta->pasn->tk_configured &&
4589
          sta->pasn->eppke_set_key)
4590
        sta->pasn->eppke_set_key(
4591
          sta->pasn->cb_ctx,
4592
          wpa_cipher_to_alg(sta->pasn->cipher),
4593
          sta->addr, 0,
4594
          sta->pasn->ptk.tk,
4595
          sta->pasn->ptk.tk_len);
4596
#endif /* CONFIG_ENC_ASSOC */
4597
      if (!ap_sta_is_epp(sta))
4598
        pasn_set_keys_from_cache(
4599
          hapd, hapd->own_addr,
4600
          sta->addr,
4601
          pasn_get_cipher(sta->pasn),
4602
          pasn_get_akmp(sta->pasn));
4603
    }
4604
    if (!ap_sta_is_epp(sta) ||
4605
        (ret < 0 &&
4606
         ap_sta_is_epp(sta) && !ap_sta_is_authorized(sta)))
4607
      ap_free_sta(hapd, sta);
4608
4609
  } else {
4610
    wpa_printf(MSG_DEBUG,
4611
         "PASN: Invalid transaction %u - ignore", trans_seq);
4612
  }
4613
}
4614
4615
#endif /* CONFIG_PASN */
4616
4617
4618
static void handle_auth(struct hostapd_data *hapd,
4619
      const struct ieee80211_mgmt *mgmt, size_t len,
4620
      int rssi, int from_queue)
4621
0
{
4622
0
  u16 auth_alg, auth_transaction, status_code;
4623
0
  u16 resp = WLAN_STATUS_SUCCESS;
4624
0
  struct sta_info *sta = NULL;
4625
0
  int res, reply_res;
4626
0
  u16 fc;
4627
0
  const u8 *challenge = NULL;
4628
0
  u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
4629
0
  size_t resp_ies_len = 0;
4630
0
  u16 seq_ctrl;
4631
0
  struct radius_sta rad_info;
4632
0
  const u8 *dst, *sa;
4633
0
  bool mld_sta = false;
4634
4635
0
  if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
4636
0
    wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
4637
0
         (unsigned long) len);
4638
0
    return;
4639
0
  }
4640
4641
#ifdef CONFIG_TESTING_OPTIONS
4642
  if (hapd->iconf->ignore_auth_probability > 0.0 &&
4643
      drand48() < hapd->iconf->ignore_auth_probability) {
4644
    wpa_printf(MSG_INFO,
4645
         "TESTING: ignoring auth frame from " MACSTR,
4646
         MAC2STR(mgmt->sa));
4647
    return;
4648
  }
4649
#endif /* CONFIG_TESTING_OPTIONS */
4650
4651
0
  sa = mgmt->sa;
4652
0
#ifdef CONFIG_IEEE80211BE
4653
  /*
4654
   * Handle MLO authentication before the station is added to hostapd and
4655
   * the driver so that the station MLD MAC address would be used in both
4656
   * hostapd and the driver.
4657
   */
4658
0
  sa = hostapd_process_ml_auth(hapd, mgmt, len);
4659
0
  if (sa)
4660
0
    mld_sta = true;
4661
0
  else
4662
0
    sa = mgmt->sa;
4663
0
#endif /* CONFIG_IEEE80211BE */
4664
4665
0
  auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4666
0
  auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
4667
0
  status_code = le_to_host16(mgmt->u.auth.status_code);
4668
0
  fc = le_to_host16(mgmt->frame_control);
4669
0
  seq_ctrl = le_to_host16(mgmt->seq_ctrl);
4670
4671
0
  if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
4672
0
      2 + WLAN_AUTH_CHALLENGE_LEN &&
4673
0
      mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
4674
0
      mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
4675
0
    challenge = &mgmt->u.auth.variable[2];
4676
4677
0
  wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
4678
0
       "auth_transaction=%d status_code=%d protected=%d%s "
4679
0
       "seq_ctrl=0x%x%s%s",
4680
0
       MAC2STR(sa), auth_alg, auth_transaction,
4681
0
       status_code, !!(fc & WLAN_FC_PROTECTED),
4682
0
       challenge ? " challenge" : "",
4683
0
       seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "",
4684
0
       from_queue ? " (from queue)" : "");
4685
4686
#ifdef CONFIG_NO_RC4
4687
  if (auth_alg == WLAN_AUTH_SHARED_KEY) {
4688
    wpa_printf(MSG_INFO,
4689
         "Unsupported authentication algorithm (%d)",
4690
         auth_alg);
4691
    resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
4692
    goto fail;
4693
  }
4694
#endif /* CONFIG_NO_RC4 */
4695
4696
#ifdef CONFIG_IEEE8021X_AUTH
4697
  if (auth_alg == WLAN_AUTH_802_1X &&
4698
      !hapd->conf->eap_using_authentication_frames) {
4699
    wpa_printf(MSG_INFO,
4700
         "Unsupported authentication algorithm (%d)",
4701
         auth_alg);
4702
    resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
4703
    goto fail;
4704
  }
4705
#endif /* CONFIG_IEEE8021X_AUTH */
4706
4707
0
  if (hapd->tkip_countermeasures) {
4708
0
    wpa_printf(MSG_DEBUG,
4709
0
         "Ongoing TKIP countermeasures (Michael MIC failure) - reject authentication");
4710
0
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4711
0
    goto fail;
4712
0
  }
4713
4714
0
  if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
4715
0
         auth_alg == WLAN_AUTH_OPEN) ||
4716
0
#ifdef CONFIG_IEEE80211R_AP
4717
0
        (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
4718
0
         auth_alg == WLAN_AUTH_FT) ||
4719
0
#endif /* CONFIG_IEEE80211R_AP */
4720
#ifdef CONFIG_SAE
4721
        (hapd->conf->wpa &&
4722
         wpa_key_mgmt_sae(hostapd_config_all_key_mgmt(hapd->conf)) &&
4723
         auth_alg == WLAN_AUTH_SAE) ||
4724
#endif /* CONFIG_SAE */
4725
#ifdef CONFIG_FILS
4726
        (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
4727
         auth_alg == WLAN_AUTH_FILS_SK) ||
4728
        (hapd->conf->wpa && wpa_key_mgmt_fils(hapd->conf->wpa_key_mgmt) &&
4729
         hapd->conf->fils_dh_group &&
4730
         auth_alg == WLAN_AUTH_FILS_SK_PFS) ||
4731
#endif /* CONFIG_FILS */
4732
#ifdef CONFIG_PASN
4733
        (hapd->conf->wpa &&
4734
         (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PASN) &&
4735
         auth_alg == WLAN_AUTH_PASN) ||
4736
#endif /* CONFIG_PASN */
4737
#ifdef CONFIG_ENC_ASSOC
4738
        (hapd->conf->wpa &&
4739
         ((hostapd_config_all_key_mgmt(hapd->conf)) &
4740
    WPA_KEY_MGMT_EPPKE) &&
4741
         hapd->conf->assoc_frame_encryption &&
4742
         auth_alg == WLAN_AUTH_EPPKE) ||
4743
#endif /* CONFIG_ENC_ASSOC */
4744
#ifdef CONFIG_IEEE8021X_AUTH
4745
        (hapd->conf->wpa &&
4746
         wpa_key_mgmt_wpa_ieee8021x(hapd->conf->wpa_key_mgmt) &&
4747
         auth_alg == WLAN_AUTH_802_1X) ||
4748
#endif /* CONFIG_IEEE8021X_AUTH */
4749
0
        ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
4750
0
         auth_alg == WLAN_AUTH_SHARED_KEY))) {
4751
0
    wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
4752
0
         auth_alg);
4753
0
    resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
4754
0
    goto fail;
4755
0
  }
4756
4757
0
  if (!(auth_transaction == 1 ||
4758
#ifdef CONFIG_SAE
4759
        (auth_alg == WLAN_AUTH_SAE &&
4760
         auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM) ||
4761
#endif /* CONFIG_SAE */
4762
#ifdef CONFIG_PASN
4763
        (auth_alg == WLAN_AUTH_PASN &&
4764
         auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH3) ||
4765
#endif /* CONFIG_PASN */
4766
#ifdef CONFIG_ENC_ASSOC
4767
        (auth_alg == WLAN_AUTH_EPPKE &&
4768
         auth_transaction == WLAN_AUTH_TR_SEQ_PASN_AUTH3) ||
4769
#endif /* CONFIG_ENC_ASSOC */
4770
#ifdef CONFIG_IEEE8021X_AUTH
4771
        /* EAP over Auth involves variable number of frames depending
4772
         * on the EAP authentication method */
4773
        auth_alg == WLAN_AUTH_802_1X ||
4774
#endif /* CONFIG_IEEE8021X_AUTH */
4775
0
        (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
4776
0
    wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
4777
0
         auth_transaction);
4778
0
    resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
4779
0
    goto fail;
4780
0
  }
4781
4782
0
  if (!hostapd_acceptable_sta_addr(hapd, mgmt->sa, sa, mld_sta)) {
4783
0
    wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
4784
0
         MAC2STR(sa));
4785
0
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4786
0
    goto fail;
4787
0
  }
4788
4789
0
  if (hapd->conf->no_auth_if_seen_on) {
4790
0
    struct hostapd_data *other;
4791
4792
0
    other = sta_track_seen_on(hapd->iface, sa,
4793
0
            hapd->conf->no_auth_if_seen_on);
4794
0
    if (other) {
4795
0
      u8 *pos;
4796
0
      u32 info;
4797
0
      u8 op_class, channel, phytype;
4798
4799
0
      wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
4800
0
           MACSTR " since STA has been seen on %s",
4801
0
           hapd->conf->iface, MAC2STR(sa),
4802
0
           hapd->conf->no_auth_if_seen_on);
4803
4804
0
      resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
4805
0
      pos = &resp_ies[0];
4806
0
      *pos++ = WLAN_EID_NEIGHBOR_REPORT;
4807
0
      *pos++ = 13;
4808
0
      os_memcpy(pos, other->own_addr, ETH_ALEN);
4809
0
      pos += ETH_ALEN;
4810
0
      info = 0; /* TODO: BSSID Information */
4811
0
      WPA_PUT_LE32(pos, info);
4812
0
      pos += 4;
4813
0
      if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
4814
0
        phytype = 8; /* dmg */
4815
0
      else if (other->iconf->ieee80211ac)
4816
0
        phytype = 9; /* vht */
4817
0
      else if (other->iconf->ieee80211n)
4818
0
        phytype = 7; /* ht */
4819
0
      else if (other->iconf->hw_mode ==
4820
0
         HOSTAPD_MODE_IEEE80211A)
4821
0
        phytype = 4; /* ofdm */
4822
0
      else if (other->iconf->hw_mode ==
4823
0
         HOSTAPD_MODE_IEEE80211G)
4824
0
        phytype = 6; /* erp */
4825
0
      else
4826
0
        phytype = 5; /* hrdsss */
4827
0
      if (ieee80211_freq_to_channel_ext(
4828
0
            hostapd_hw_get_freq(other,
4829
0
              other->iconf->channel),
4830
0
            other->iconf->secondary_channel,
4831
0
            other->iconf->ieee80211ac,
4832
0
            &op_class, &channel) == NUM_HOSTAPD_MODES) {
4833
0
        op_class = 0;
4834
0
        channel = other->iconf->channel;
4835
0
      }
4836
0
      *pos++ = op_class;
4837
0
      *pos++ = channel;
4838
0
      *pos++ = phytype;
4839
0
      resp_ies_len = pos - &resp_ies[0];
4840
0
      goto fail;
4841
0
    }
4842
0
  }
4843
4844
0
  res = ieee802_11_allowed_address(hapd, sa, (const u8 *) mgmt, len,
4845
0
           &rad_info);
4846
0
  if (res == HOSTAPD_ACL_REJECT) {
4847
0
    wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4848
0
      "Ignore Authentication frame from " MACSTR
4849
0
      " due to ACL reject", MAC2STR(sa));
4850
0
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4851
0
    goto fail;
4852
0
  }
4853
0
  if (res == HOSTAPD_ACL_PENDING)
4854
0
    return;
4855
4856
0
#ifdef CONFIG_IEEE80211BE
4857
0
  if (mld_sta) {
4858
0
    res = ieee802_11_allowed_address(hapd, mgmt->sa,
4859
0
             (const u8 *) mgmt, len,
4860
0
             &rad_info);
4861
0
    if (res == HOSTAPD_ACL_REJECT) {
4862
0
      wpa_msg(hapd->msg_ctx, MSG_DEBUG,
4863
0
        "Ignore Authentication frame from " MACSTR
4864
0
        " due to ACL reject", MAC2STR(mgmt->sa));
4865
0
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4866
0
      goto fail;
4867
0
    }
4868
0
    if (res == HOSTAPD_ACL_PENDING)
4869
0
      return;
4870
0
  }
4871
0
#endif /* CONFIG_IEEE80211BE */
4872
4873
#ifdef CONFIG_SAE
4874
  if (auth_alg == WLAN_AUTH_SAE && !from_queue &&
4875
      (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT ||
4876
       (auth_transaction == WLAN_AUTH_TR_SEQ_SAE_CONFIRM &&
4877
        auth_sae_queued_addr(hapd, sa)))) {
4878
    /* Handle SAE Authentication commit message through a queue to
4879
     * provide more control for postponing the needed heavy
4880
     * processing under a possible DoS attack scenario. In addition,
4881
     * queue SAE Authentication confirm message if there happens to
4882
     * be a queued commit message from the same peer. This is needed
4883
     * to avoid reordering Authentication frames within the same
4884
     * SAE exchange. */
4885
    auth_sae_queue(hapd, mgmt, len, rssi);
4886
    return;
4887
  }
4888
#endif /* CONFIG_SAE */
4889
4890
0
  sta = ap_get_sta(hapd, sa);
4891
0
  if (sta) {
4892
0
    sta->flags &= ~WLAN_STA_PENDING_FILS_ERP;
4893
0
    sta->ft_over_ds = 0;
4894
0
    if ((fc & WLAN_FC_RETRY) &&
4895
0
        sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
4896
0
        sta->last_seq_ctrl == seq_ctrl &&
4897
0
        sta->last_subtype == WLAN_FC_STYPE_AUTH) {
4898
0
      hostapd_logger(hapd, sta->addr,
4899
0
               HOSTAPD_MODULE_IEEE80211,
4900
0
               HOSTAPD_LEVEL_DEBUG,
4901
0
               "Drop repeated authentication frame seq_ctrl=0x%x",
4902
0
               seq_ctrl);
4903
0
      return;
4904
0
    }
4905
#ifdef CONFIG_PASN
4906
    if (auth_alg == WLAN_AUTH_PASN &&
4907
        (sta->flags & WLAN_STA_ASSOC)) {
4908
      wpa_printf(MSG_DEBUG,
4909
           "PASN: auth: Existing station: " MACSTR,
4910
           MAC2STR(sta->addr));
4911
      return;
4912
    }
4913
#endif /* CONFIG_PASN */
4914
0
  } else {
4915
#ifdef CONFIG_MESH
4916
    if (hapd->conf->mesh & MESH_ENABLED) {
4917
      /* if the mesh peer is not available, we don't do auth.
4918
       */
4919
      wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
4920
           " not yet known - drop Authentication frame",
4921
           MAC2STR(sa));
4922
      /*
4923
       * Save a copy of the frame so that it can be processed
4924
       * if a new peer entry is added shortly after this.
4925
       */
4926
      wpabuf_free(hapd->mesh_pending_auth);
4927
      hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
4928
      os_get_reltime(&hapd->mesh_pending_auth_time);
4929
      return;
4930
    }
4931
#endif /* CONFIG_MESH */
4932
4933
0
    sta = ap_sta_add(hapd, sa);
4934
0
    if (!sta) {
4935
0
      wpa_printf(MSG_DEBUG, "ap_sta_add() failed");
4936
0
      resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
4937
0
      goto fail;
4938
0
    }
4939
0
  }
4940
4941
#if defined(CONFIG_ENC_ASSOC) || defined(CONFIG_IEEE8021X_AUTH)
4942
  if (auth_alg == WLAN_AUTH_EPPKE || auth_alg == WLAN_AUTH_802_1X) {
4943
    wpa_printf(MSG_DEBUG, "Mark the station as an EPP peer");
4944
    sta->epp_sta = true;
4945
  }
4946
#endif /* CONFIG_ENC_ASSOC || CONFIG_IEEE8021X_AUTH */
4947
4948
0
#ifdef CONFIG_IEEE80211BE
4949
  /* Set the non-AP MLD information based on the initial Authentication
4950
   * frame. Once the STA entry has been added to the driver, the driver
4951
   * will translate addresses in the frame and we need to avoid overriding
4952
   * peer_addr based on mgmt->sa which would have been translated to the
4953
   * MLD MAC address. */
4954
0
  if (!sta->added_unassoc && auth_transaction == 1) {
4955
0
    ap_sta_free_sta_profile(&sta->mld_info);
4956
0
    os_memset(&sta->mld_info, 0, sizeof(sta->mld_info));
4957
4958
0
    if (mld_sta) {
4959
0
      u8 link_id = hapd->mld_link_id;
4960
4961
0
      ap_sta_set_mld(sta, true);
4962
0
      sta->mld_assoc_link_id = link_id;
4963
4964
      /*
4965
       * Set the MLD address as the station address and the
4966
       * station addresses.
4967
       */
4968
0
      os_memcpy(sta->mld_info.common_info.mld_addr, sa,
4969
0
          ETH_ALEN);
4970
0
      os_memcpy(sta->mld_info.links[link_id].peer_addr,
4971
0
          mgmt->sa, ETH_ALEN);
4972
0
      os_memcpy(sta->mld_info.links[link_id].local_addr,
4973
0
          hapd->own_addr, ETH_ALEN);
4974
0
    }
4975
4976
0
    if ((!(sta->flags & WLAN_STA_MFP) ||
4977
0
         !ap_sta_is_authorized(sta)) && sta->wpa_sm) {
4978
0
      struct wpa_state_machine *sm = sta->wpa_sm;
4979
4980
0
      clear_wpa_sm_for_each_partner_link(hapd, sta);
4981
0
      clear_wpa_sm_for_all_sta(hapd, sm);
4982
0
      wpa_auth_sta_deinit(sm);
4983
0
      sta->wpa_sm = NULL;
4984
0
    }
4985
0
  }
4986
0
#endif /* CONFIG_IEEE80211BE */
4987
4988
0
  sta->last_seq_ctrl = seq_ctrl;
4989
0
  sta->last_subtype = WLAN_FC_STYPE_AUTH;
4990
#ifdef CONFIG_MBO
4991
  sta->auth_rssi = rssi;
4992
#endif /* CONFIG_MBO */
4993
4994
0
  res = ieee802_11_set_radius_info(hapd, sta, res, &rad_info);
4995
0
  if (res) {
4996
0
    wpa_printf(MSG_DEBUG, "ieee802_11_set_radius_info() failed");
4997
0
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
4998
0
    goto fail;
4999
0
  }
5000
5001
0
  sta->flags &= ~WLAN_STA_PREAUTH;
5002
0
  ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
5003
5004
  /*
5005
   * If the driver supports full AP client state, add a station to the
5006
   * driver before sending authentication reply to make sure the driver
5007
   * has resources, and not to go through the entire authentication and
5008
   * association handshake, and fail it at the end.
5009
   *
5010
   * If this is not the first transaction, in a multi-step authentication
5011
   * algorithm, the station already exists in the driver
5012
   * (sta->added_unassoc = 1) so skip it.
5013
   *
5014
   * In mesh mode, the station was already added to the driver when the
5015
   * NEW_PEER_CANDIDATE event is received.
5016
   *
5017
   * If PMF was negotiated for the existing association, skip this to
5018
   * avoid dropping the STA entry and the associated keys. This is needed
5019
   * to allow the original connection work until the attempt can complete
5020
   * (re)association, so that unprotected Authentication frame cannot be
5021
   * used to bypass PMF protection.
5022
   *
5023
   * PASN authentication does not require adding/removing station to the
5024
   * driver so skip this flow in case of PASN authentication.
5025
   */
5026
0
  if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
5027
0
      (!(sta->flags & WLAN_STA_MFP) || !ap_sta_is_authorized(sta)) &&
5028
0
      !(hapd->conf->mesh & MESH_ENABLED) &&
5029
0
      !(sta->added_unassoc) && auth_alg != WLAN_AUTH_PASN) {
5030
0
    if (ap_sta_re_add(hapd, sta) < 0) {
5031
0
      resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
5032
0
      goto fail;
5033
0
    }
5034
0
  }
5035
5036
0
  switch (auth_alg) {
5037
0
  case WLAN_AUTH_OPEN:
5038
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5039
0
             HOSTAPD_LEVEL_DEBUG,
5040
0
             "authentication OK (open system)");
5041
0
    sta->flags |= WLAN_STA_AUTH;
5042
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
5043
0
    sta->auth_alg = WLAN_AUTH_OPEN;
5044
0
    mlme_authenticate_indication(hapd, sta);
5045
0
    break;
5046
#ifdef CONFIG_WEP
5047
#ifndef CONFIG_NO_RC4
5048
  case WLAN_AUTH_SHARED_KEY:
5049
    resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
5050
               fc & WLAN_FC_PROTECTED);
5051
    if (resp != 0)
5052
      wpa_printf(MSG_DEBUG,
5053
           "auth_shared_key() failed: status=%d", resp);
5054
    sta->auth_alg = WLAN_AUTH_SHARED_KEY;
5055
    mlme_authenticate_indication(hapd, sta);
5056
    if (sta->challenge && auth_transaction == 1) {
5057
      resp_ies[0] = WLAN_EID_CHALLENGE;
5058
      resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
5059
      os_memcpy(resp_ies + 2, sta->challenge,
5060
          WLAN_AUTH_CHALLENGE_LEN);
5061
      resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
5062
    }
5063
    break;
5064
#endif /* CONFIG_NO_RC4 */
5065
#endif /* CONFIG_WEP */
5066
0
#ifdef CONFIG_IEEE80211R_AP
5067
0
  case WLAN_AUTH_FT:
5068
0
    sta->auth_alg = WLAN_AUTH_FT;
5069
0
    if (sta->wpa_sm == NULL)
5070
0
      sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
5071
0
              sta->addr, NULL);
5072
0
    if (sta->wpa_sm == NULL) {
5073
0
      wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
5074
0
           "state machine");
5075
0
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5076
0
      goto fail;
5077
0
    }
5078
0
    if (hapd->conf->security_profiles) {
5079
0
      struct ieee802_11_elems elems;
5080
0
      size_t auth_var_len = len - IEEE80211_HDRLEN -
5081
0
        sizeof(mgmt->u.auth);
5082
5083
0
      if (ieee802_11_parse_elems(mgmt->u.auth.variable,
5084
0
               auth_var_len, &elems,
5085
0
               1) == ParseFailed) {
5086
0
        wpa_printf(MSG_DEBUG,
5087
0
             "Failed to parse elements in %s",
5088
0
             __func__);
5089
0
        goto fail;
5090
0
      }
5091
0
      if (validate_security_profile(hapd, sta, &elems, "FT",
5092
0
                  NULL))
5093
0
        goto fail;
5094
0
    }
5095
0
    wpa_ft_process_auth(sta->wpa_sm,
5096
0
            auth_transaction, mgmt->u.auth.variable,
5097
0
            len - IEEE80211_HDRLEN -
5098
0
            sizeof(mgmt->u.auth),
5099
0
            handle_auth_ft_finish, hapd);
5100
    /* handle_auth_ft_finish() callback will complete auth. */
5101
0
    return;
5102
0
#endif /* CONFIG_IEEE80211R_AP */
5103
#ifdef CONFIG_SAE
5104
  case WLAN_AUTH_SAE:
5105
#ifdef CONFIG_MESH
5106
    if (status_code == WLAN_STATUS_SUCCESS &&
5107
        hapd->conf->mesh & MESH_ENABLED) {
5108
      if (sta->wpa_sm == NULL)
5109
        sta->wpa_sm =
5110
          wpa_auth_sta_init(hapd->wpa_auth,
5111
                sta->addr, NULL);
5112
      if (sta->wpa_sm == NULL) {
5113
        wpa_printf(MSG_DEBUG,
5114
             "SAE: Failed to initialize WPA state machine");
5115
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
5116
        goto fail;
5117
      }
5118
    }
5119
#endif /* CONFIG_MESH */
5120
    handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
5121
        status_code);
5122
    return;
5123
#endif /* CONFIG_SAE */
5124
#ifdef CONFIG_FILS
5125
  case WLAN_AUTH_FILS_SK:
5126
  case WLAN_AUTH_FILS_SK_PFS:
5127
    handle_auth_fils(hapd, sta, mgmt->u.auth.variable,
5128
         len - IEEE80211_HDRLEN - sizeof(mgmt->u.auth),
5129
         auth_alg, auth_transaction, status_code,
5130
         handle_auth_fils_finish);
5131
    return;
5132
#endif /* CONFIG_FILS */
5133
#ifdef CONFIG_IEEE8021X_AUTH
5134
  case WLAN_AUTH_802_1X:
5135
    handle_auth_802_1x(hapd, sta, mgmt->u.auth.variable,
5136
           len - IEEE80211_HDRLEN -
5137
           sizeof(mgmt->u.auth),
5138
           auth_alg, auth_transaction);
5139
    return;
5140
#endif /* CONFIG_IEEE8021X_AUTH */
5141
#ifdef CONFIG_ENC_ASSOC
5142
  case WLAN_AUTH_EPPKE:
5143
#endif /* CONFIG_ENC_ASSOC */
5144
#ifdef CONFIG_PASN
5145
  case WLAN_AUTH_PASN:
5146
    handle_auth_pasn(hapd, sta, mgmt, len, auth_transaction,
5147
         status_code);
5148
    return;
5149
#endif /* CONFIG_PASN */
5150
0
  }
5151
5152
0
  if (hapd->conf->security_profiles) {
5153
0
    struct ieee802_11_elems elems;
5154
0
    size_t auth_var_len = len - IEEE80211_HDRLEN -
5155
0
      sizeof(mgmt->u.auth);
5156
5157
0
    if (ieee802_11_parse_elems(mgmt->u.auth.variable, auth_var_len,
5158
0
             &elems, 1) == ParseFailed) {
5159
0
      wpa_printf(MSG_DEBUG, "Failed to parse elements in %s",
5160
0
           __func__);
5161
0
      goto fail;
5162
0
    }
5163
0
    if (validate_security_profile(hapd, sta, &elems,
5164
0
                "Other auth_alg", NULL))
5165
0
      goto fail;
5166
0
  }
5167
5168
0
 fail:
5169
0
  dst = mgmt->sa;
5170
5171
0
#ifdef CONFIG_IEEE80211BE
5172
0
  if (ap_sta_is_mld(hapd, sta))
5173
0
    dst = sta->addr;
5174
0
#endif /* CONFIG_IEEE80211BE */
5175
5176
0
  reply_res = send_auth_reply(hapd, sta, dst, auth_alg,
5177
0
            auth_alg == WLAN_AUTH_SAE ?
5178
0
            auth_transaction : auth_transaction + 1,
5179
0
            resp, resp_ies, resp_ies_len,
5180
0
            "handle-auth");
5181
5182
0
  if (sta && sta->added_unassoc && (resp != WLAN_STATUS_SUCCESS ||
5183
0
            reply_res != WLAN_STATUS_SUCCESS)) {
5184
0
    hostapd_drv_sta_remove(hapd, sta->addr);
5185
0
    sta->added_unassoc = 0;
5186
0
  }
5187
0
}
5188
5189
5190
static u8 hostapd_max_bssid_indicator(struct hostapd_data *hapd)
5191
0
{
5192
0
  size_t num_bss_nontx;
5193
0
  u8 max_bssid_ind = 0;
5194
5195
0
  if (!hapd->iconf->mbssid || hapd->iface->num_bss <= 1)
5196
0
    return 0;
5197
5198
0
  if (hapd->iface->conf->mbssid_max > 0)
5199
0
    num_bss_nontx = hapd->iface->conf->mbssid_max - 1;
5200
0
  else
5201
0
    num_bss_nontx = hapd->iface->conf->num_bss - 1;
5202
0
  while (num_bss_nontx > 0) {
5203
0
    max_bssid_ind++;
5204
0
    num_bss_nontx >>= 1;
5205
0
  }
5206
0
  return max_bssid_ind;
5207
0
}
5208
5209
5210
static u32 hostapd_get_aid_word(struct hostapd_data *hapd,
5211
        struct sta_info *sta, int i)
5212
0
{
5213
0
#ifdef CONFIG_IEEE80211BE
5214
0
  u32 aid_word = 0;
5215
5216
  /* Do not assign an AID that is in use on any of the affiliated links
5217
   * when finding an AID for a non-AP MLD. */
5218
0
  if (hapd->conf->mld_ap && sta->mld_info.mld_sta) {
5219
0
    int j;
5220
5221
0
    for (j = 0; j < MAX_NUM_MLD_LINKS; j++) {
5222
0
      struct hostapd_data *link_bss;
5223
5224
0
      if (!sta->mld_info.links[j].valid)
5225
0
        continue;
5226
5227
0
      link_bss = hostapd_mld_get_link_bss(hapd, j);
5228
0
      if (!link_bss) {
5229
        /* This shouldn't happen, just skip */
5230
0
        wpa_printf(MSG_ERROR,
5231
0
             "MLD: Failed to get link BSS for AID");
5232
0
        continue;
5233
0
      }
5234
5235
0
      aid_word |= link_bss->sta_aid[i];
5236
0
    }
5237
5238
0
    return aid_word;
5239
0
  }
5240
0
#endif /* CONFIG_IEEE80211BE */
5241
5242
0
  return hapd->sta_aid[i];
5243
0
}
5244
5245
5246
int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
5247
0
{
5248
0
  int i, j = 32, aid;
5249
5250
  /* Transmitted and non-transmitted BSSIDs share the same AID pool, so
5251
   * use the shared storage in the transmitted BSS to find the next
5252
   * available value. */
5253
0
  hapd = hostapd_mbssid_get_tx_bss(hapd);
5254
5255
  /* get a unique AID */
5256
0
  if (sta->aid > 0) {
5257
0
    wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
5258
0
    return 0;
5259
0
  }
5260
5261
0
  if (TEST_FAIL())
5262
0
    return -1;
5263
5264
0
  for (i = 0; i < AID_WORDS; i++) {
5265
0
    u32 aid_word = hostapd_get_aid_word(hapd, sta, i);
5266
5267
0
    if (aid_word == (u32) -1)
5268
0
      continue;
5269
0
    for (j = 0; j < 32; j++) {
5270
0
      if (!(aid_word & BIT(j)))
5271
0
        break;
5272
0
    }
5273
0
    if (j < 32)
5274
0
      break;
5275
0
  }
5276
0
  if (j == 32)
5277
0
    return -1;
5278
0
  aid = i * 32 + j + (1 << hostapd_max_bssid_indicator(hapd));
5279
0
  if (aid > 2007)
5280
0
    return -1;
5281
5282
0
  sta->aid = aid;
5283
0
  hapd->sta_aid[i] |= BIT(j);
5284
0
  wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
5285
0
  return 0;
5286
0
}
5287
5288
5289
static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
5290
          const u8 *ssid_ie, size_t ssid_ie_len)
5291
0
{
5292
0
  if (ssid_ie == NULL)
5293
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5294
5295
0
  if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
5296
0
      os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
5297
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5298
0
             HOSTAPD_LEVEL_INFO,
5299
0
             "Station tried to associate with unknown SSID "
5300
0
             "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
5301
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5302
0
  }
5303
5304
0
  return WLAN_STATUS_SUCCESS;
5305
0
}
5306
5307
5308
static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
5309
         const u8 *wmm_ie, size_t wmm_ie_len)
5310
0
{
5311
0
  sta->flags &= ~WLAN_STA_WMM;
5312
0
  sta->qosinfo = 0;
5313
0
  if (wmm_ie && hapd->conf->wmm_enabled) {
5314
0
    struct wmm_information_element *wmm;
5315
5316
0
    if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
5317
0
      hostapd_logger(hapd, sta->addr,
5318
0
               HOSTAPD_MODULE_WPA,
5319
0
               HOSTAPD_LEVEL_DEBUG,
5320
0
               "invalid WMM element in association "
5321
0
               "request");
5322
0
      return WLAN_STATUS_UNSPECIFIED_FAILURE;
5323
0
    }
5324
5325
0
    sta->flags |= WLAN_STA_WMM;
5326
0
    wmm = (struct wmm_information_element *) wmm_ie;
5327
0
    sta->qosinfo = wmm->qos_info;
5328
0
  }
5329
0
  return WLAN_STATUS_SUCCESS;
5330
0
}
5331
5332
static u16 check_multi_ap(struct hostapd_data *hapd, struct sta_info *sta,
5333
        const u8 *multi_ap_ie, size_t multi_ap_len)
5334
0
{
5335
0
  struct multi_ap_params multi_ap;
5336
0
  u16 status;
5337
5338
0
  sta->flags &= ~WLAN_STA_MULTI_AP;
5339
5340
0
  if (!hapd->conf->multi_ap)
5341
0
    return WLAN_STATUS_SUCCESS;
5342
5343
0
  if (!multi_ap_ie) {
5344
0
    if (!(hapd->conf->multi_ap & FRONTHAUL_BSS)) {
5345
0
      hostapd_logger(hapd, sta->addr,
5346
0
               HOSTAPD_MODULE_IEEE80211,
5347
0
               HOSTAPD_LEVEL_INFO,
5348
0
               "Non-Multi-AP STA tries to associate with backhaul-only BSS");
5349
0
      return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
5350
0
    }
5351
5352
0
    return WLAN_STATUS_SUCCESS;
5353
0
  }
5354
5355
0
  status = check_multi_ap_ie(multi_ap_ie + 4, multi_ap_len - 4,
5356
0
           &multi_ap);
5357
0
  if (status != WLAN_STATUS_SUCCESS)
5358
0
    return status;
5359
5360
0
  if (multi_ap.capability && multi_ap.capability != MULTI_AP_BACKHAUL_STA)
5361
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5362
0
             HOSTAPD_LEVEL_INFO,
5363
0
             "Multi-AP IE with unexpected value 0x%02x",
5364
0
             multi_ap.capability);
5365
5366
0
  if (multi_ap.profile == MULTI_AP_PROFILE_1 &&
5367
0
      (hapd->conf->multi_ap_client_disallow &
5368
0
       PROFILE1_CLIENT_ASSOC_DISALLOW)) {
5369
0
    hostapd_logger(hapd, sta->addr,
5370
0
             HOSTAPD_MODULE_IEEE80211,
5371
0
             HOSTAPD_LEVEL_INFO,
5372
0
             "Multi-AP Profile-1 clients not allowed");
5373
0
    return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
5374
0
  }
5375
5376
0
  if (multi_ap.profile >= MULTI_AP_PROFILE_2 &&
5377
0
      (hapd->conf->multi_ap_client_disallow &
5378
0
       PROFILE2_CLIENT_ASSOC_DISALLOW)) {
5379
0
    hostapd_logger(hapd, sta->addr,
5380
0
             HOSTAPD_MODULE_IEEE80211,
5381
0
             HOSTAPD_LEVEL_INFO,
5382
0
             "Multi-AP Profile-2 clients not allowed");
5383
0
    return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
5384
0
  }
5385
5386
0
  if (!(multi_ap.capability & MULTI_AP_BACKHAUL_STA)) {
5387
0
    if (hapd->conf->multi_ap & FRONTHAUL_BSS)
5388
0
      return WLAN_STATUS_SUCCESS;
5389
5390
0
    hostapd_logger(hapd, sta->addr,
5391
0
             HOSTAPD_MODULE_IEEE80211,
5392
0
             HOSTAPD_LEVEL_INFO,
5393
0
             "Non-Multi-AP STA tries to associate with backhaul-only BSS");
5394
0
    return WLAN_STATUS_ASSOC_DENIED_UNSPEC;
5395
0
  }
5396
5397
0
  if (!(hapd->conf->multi_ap & BACKHAUL_BSS))
5398
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5399
0
             HOSTAPD_LEVEL_DEBUG,
5400
0
             "Backhaul STA tries to associate with fronthaul-only BSS");
5401
5402
0
  sta->flags |= WLAN_STA_MULTI_AP;
5403
0
  return WLAN_STATUS_SUCCESS;
5404
0
}
5405
5406
5407
static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
5408
         struct ieee802_11_elems *elems)
5409
0
{
5410
  /* Supported rates not used in IEEE 802.11ad/DMG */
5411
0
  if (hapd->iface->current_mode &&
5412
0
      hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD)
5413
0
    return WLAN_STATUS_SUCCESS;
5414
5415
0
  if (!elems->supp_rates) {
5416
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5417
0
             HOSTAPD_LEVEL_DEBUG,
5418
0
             "No supported rates element in AssocReq");
5419
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5420
0
  }
5421
5422
0
  if (elems->supp_rates_len + elems->ext_supp_rates_len >
5423
0
      sizeof(sta->supported_rates)) {
5424
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5425
0
             HOSTAPD_LEVEL_DEBUG,
5426
0
             "Invalid supported rates element length %d+%d",
5427
0
             elems->supp_rates_len,
5428
0
             elems->ext_supp_rates_len);
5429
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5430
0
  }
5431
5432
0
  sta->supported_rates_len = merge_byte_arrays(
5433
0
    sta->supported_rates, sizeof(sta->supported_rates),
5434
0
    elems->supp_rates, elems->supp_rates_len,
5435
0
    elems->ext_supp_rates, elems->ext_supp_rates_len);
5436
5437
0
  return WLAN_STATUS_SUCCESS;
5438
0
}
5439
5440
5441
#ifdef CONFIG_OWE
5442
5443
static int owe_group_supported(struct hostapd_data *hapd, u16 group)
5444
{
5445
  int i;
5446
  int *groups = hapd->conf->owe_groups;
5447
5448
  if (group != 19 && group != 20 && group != 21)
5449
    return 0;
5450
5451
  if (!groups)
5452
    return 1;
5453
5454
  for (i = 0; groups[i] > 0; i++) {
5455
    if (groups[i] == group)
5456
      return 1;
5457
  }
5458
5459
  return 0;
5460
}
5461
5462
5463
static u16 owe_process_assoc_req(struct hostapd_data *hapd,
5464
         struct sta_info *sta, const u8 *owe_dh,
5465
         u8 owe_dh_len)
5466
{
5467
  struct wpabuf *secret, *pub, *hkey;
5468
  int res;
5469
  u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5470
  const char *info = "OWE Key Generation";
5471
  const u8 *addr[2];
5472
  size_t len[2];
5473
  u16 group;
5474
  size_t hash_len, prime_len;
5475
5476
  if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
5477
    wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
5478
    return WLAN_STATUS_SUCCESS;
5479
  }
5480
5481
  group = WPA_GET_LE16(owe_dh);
5482
  if (!owe_group_supported(hapd, group)) {
5483
    wpa_printf(MSG_DEBUG, "OWE: Unsupported DH group %u", group);
5484
    return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
5485
  }
5486
  if (group == 19)
5487
    prime_len = 32;
5488
  else if (group == 20)
5489
    prime_len = 48;
5490
  else if (group == 21)
5491
    prime_len = 66;
5492
  else
5493
    return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
5494
5495
  if (sta->owe_group == group && sta->owe_ecdh) {
5496
    /* This is a workaround for mac80211 behavior of retransmitting
5497
     * the Association Request frames multiple times if the link
5498
     * layer retries (i.e., seq# remains same) fail. The mac80211
5499
     * initiated retransmission will use a different seq# and as
5500
     * such, will go through duplicate detection. If we were to
5501
     * change our DH key for that attempt, there would be two
5502
     * different DH shared secrets and the STA would likely select
5503
     * the wrong one. */
5504
    wpa_printf(MSG_DEBUG,
5505
         "OWE: Try to reuse own previous DH key since the STA tried to go through OWE association again");
5506
  } else {
5507
    crypto_ecdh_deinit(sta->owe_ecdh);
5508
    sta->owe_ecdh = crypto_ecdh_init(group);
5509
  }
5510
  if (!sta->owe_ecdh)
5511
    return WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED;
5512
  sta->owe_group = group;
5513
5514
  secret = crypto_ecdh_set_peerkey(sta->owe_ecdh, 0, owe_dh + 2,
5515
           owe_dh_len - 2);
5516
  secret = wpabuf_zeropad(secret, prime_len);
5517
  if (!secret) {
5518
    wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5519
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5520
  }
5521
  wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5522
5523
  /* prk = HKDF-extract(C | A | group, z) */
5524
5525
  pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
5526
  if (!pub) {
5527
    wpabuf_clear_free(secret);
5528
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5529
  }
5530
5531
  /* PMKID = Truncate-128(Hash(C | A)) */
5532
  addr[0] = owe_dh + 2;
5533
  len[0] = owe_dh_len - 2;
5534
  addr[1] = wpabuf_head(pub);
5535
  len[1] = wpabuf_len(pub);
5536
  if (group == 19) {
5537
    res = sha256_vector(2, addr, len, pmkid);
5538
    hash_len = SHA256_MAC_LEN;
5539
  } else if (group == 20) {
5540
    res = sha384_vector(2, addr, len, pmkid);
5541
    hash_len = SHA384_MAC_LEN;
5542
  } else if (group == 21) {
5543
    res = sha512_vector(2, addr, len, pmkid);
5544
    hash_len = SHA512_MAC_LEN;
5545
  } else {
5546
    wpabuf_free(pub);
5547
    wpabuf_clear_free(secret);
5548
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5549
  }
5550
  pub = wpabuf_zeropad(pub, prime_len);
5551
  if (res < 0 || !pub) {
5552
    wpabuf_free(pub);
5553
    wpabuf_clear_free(secret);
5554
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5555
  }
5556
5557
  hkey = wpabuf_alloc(owe_dh_len - 2 + wpabuf_len(pub) + 2);
5558
  if (!hkey) {
5559
    wpabuf_free(pub);
5560
    wpabuf_clear_free(secret);
5561
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5562
  }
5563
5564
  wpabuf_put_data(hkey, owe_dh + 2, owe_dh_len - 2); /* C */
5565
  wpabuf_put_buf(hkey, pub); /* A */
5566
  wpabuf_free(pub);
5567
  wpabuf_put_le16(hkey, group); /* group */
5568
  if (group == 19)
5569
    res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5570
          wpabuf_head(secret), wpabuf_len(secret), prk);
5571
  else if (group == 20)
5572
    res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5573
          wpabuf_head(secret), wpabuf_len(secret), prk);
5574
  else if (group == 21)
5575
    res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5576
          wpabuf_head(secret), wpabuf_len(secret), prk);
5577
  wpabuf_clear_free(hkey);
5578
  wpabuf_clear_free(secret);
5579
  if (res < 0)
5580
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5581
5582
  wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5583
5584
  /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5585
5586
  os_free(sta->owe_pmk);
5587
  sta->owe_pmk = os_malloc(hash_len);
5588
  if (!sta->owe_pmk) {
5589
    os_memset(prk, 0, SHA512_MAC_LEN);
5590
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5591
  }
5592
5593
  if (group == 19)
5594
    res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5595
              os_strlen(info), sta->owe_pmk, hash_len);
5596
  else if (group == 20)
5597
    res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5598
              os_strlen(info), sta->owe_pmk, hash_len);
5599
  else if (group == 21)
5600
    res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5601
              os_strlen(info), sta->owe_pmk, hash_len);
5602
  os_memset(prk, 0, SHA512_MAC_LEN);
5603
  if (res < 0) {
5604
    os_free(sta->owe_pmk);
5605
    sta->owe_pmk = NULL;
5606
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
5607
  }
5608
  sta->owe_pmk_len = hash_len;
5609
5610
  wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sta->owe_pmk, sta->owe_pmk_len);
5611
  wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5612
  wpa_auth_pmksa_add2(hapd->wpa_auth, sta->addr, sta->owe_pmk,
5613
          sta->owe_pmk_len, pmkid, 0, WPA_KEY_MGMT_OWE,
5614
          NULL, ap_sta_is_mld(hapd, sta));
5615
5616
  return WLAN_STATUS_SUCCESS;
5617
}
5618
5619
5620
u16 owe_validate_request(struct hostapd_data *hapd, const u8 *peer,
5621
       const u8 *rsn_ie, size_t rsn_ie_len,
5622
       const u8 *owe_dh, size_t owe_dh_len)
5623
{
5624
  struct wpa_ie_data data;
5625
  int res;
5626
5627
  if (!rsn_ie || rsn_ie_len < 2) {
5628
    wpa_printf(MSG_DEBUG, "OWE: Invalid RSNE from " MACSTR,
5629
         MAC2STR(peer));
5630
    return WLAN_STATUS_INVALID_ELEMENT;
5631
  }
5632
  rsn_ie -= 2;
5633
  rsn_ie_len += 2;
5634
5635
  res = wpa_parse_wpa_ie_rsn(rsn_ie, rsn_ie_len, &data);
5636
  if (res) {
5637
    wpa_printf(MSG_DEBUG, "Failed to parse RSNE from " MACSTR
5638
         " (res=%d)", MAC2STR(peer), res);
5639
    wpa_hexdump(MSG_DEBUG, "RSNE", rsn_ie, rsn_ie_len);
5640
    return wpa_res_to_status_code(res);
5641
  }
5642
  if (!(data.key_mgmt & WPA_KEY_MGMT_OWE)) {
5643
    wpa_printf(MSG_DEBUG,
5644
         "OWE: Unexpected key mgmt 0x%x from " MACSTR,
5645
         (unsigned int) data.key_mgmt, MAC2STR(peer));
5646
    return WLAN_STATUS_INVALID_AKMP;
5647
  }
5648
  if (!owe_dh) {
5649
    wpa_printf(MSG_DEBUG,
5650
         "OWE: No Diffie-Hellman Parameter element from "
5651
         MACSTR, MAC2STR(peer));
5652
    return WLAN_STATUS_INVALID_AKMP;
5653
  }
5654
5655
  return WLAN_STATUS_SUCCESS;
5656
}
5657
5658
5659
u16 owe_process_rsn_ie(struct hostapd_data *hapd,
5660
           struct sta_info *sta,
5661
           const u8 *rsn_ie, size_t rsn_ie_len,
5662
           const u8 *owe_dh, size_t owe_dh_len,
5663
           const u8 *link_addr)
5664
{
5665
  u16 status;
5666
  u8 *owe_buf, ie[256 * 2];
5667
  size_t ie_len = 0;
5668
  enum wpa_validate_result res;
5669
5670
  if (!rsn_ie || rsn_ie_len < 2) {
5671
    wpa_printf(MSG_DEBUG, "OWE: No RSNE in (Re)AssocReq");
5672
    status = WLAN_STATUS_INVALID_ELEMENT;
5673
    goto end;
5674
  }
5675
5676
  if (!sta->wpa_sm)
5677
    sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr,
5678
            NULL);
5679
  if (!sta->wpa_sm) {
5680
    wpa_printf(MSG_WARNING,
5681
         "OWE: Failed to initialize WPA state machine");
5682
    status = WLAN_STATUS_UNSPECIFIED_FAILURE;
5683
    goto end;
5684
  }
5685
#ifdef CONFIG_IEEE80211BE
5686
  if (ap_sta_is_mld(hapd, sta))
5687
    wpa_auth_set_ml_info(sta->wpa_sm,
5688
             sta->mld_assoc_link_id, &sta->mld_info);
5689
#endif /* CONFIG_IEEE80211BE */
5690
  rsn_ie -= 2;
5691
  rsn_ie_len += 2;
5692
  res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
5693
          hapd->iface->freq, rsn_ie, rsn_ie_len,
5694
          NULL, 0, NULL, 0, owe_dh, owe_dh_len, NULL,
5695
          ap_sta_is_mld(hapd, sta), NULL);
5696
  status = wpa_res_to_status_code(res);
5697
  if (status != WLAN_STATUS_SUCCESS)
5698
    goto end;
5699
  status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
5700
  if (status != WLAN_STATUS_SUCCESS)
5701
    goto end;
5702
  owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, ie, sizeof(ie));
5703
  if (!owe_buf) {
5704
    status = WLAN_STATUS_UNSPECIFIED_FAILURE;
5705
    goto end;
5706
  }
5707
5708
  if (sta->owe_ecdh) {
5709
    struct wpabuf *pub;
5710
5711
    pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
5712
    if (!pub) {
5713
      status = WLAN_STATUS_UNSPECIFIED_FAILURE;
5714
      goto end;
5715
    }
5716
5717
    /* OWE Diffie-Hellman Parameter element */
5718
    *owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
5719
    *owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
5720
    *owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
5721
               */
5722
    WPA_PUT_LE16(owe_buf, sta->owe_group);
5723
    owe_buf += 2;
5724
    os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
5725
    owe_buf += wpabuf_len(pub);
5726
    wpabuf_free(pub);
5727
    sta->external_dh_updated = 1;
5728
  }
5729
  ie_len = owe_buf - ie;
5730
5731
end:
5732
  wpa_printf(MSG_DEBUG, "OWE: Update status %d, ie len %d for peer "
5733
            MACSTR, status, (unsigned int) ie_len,
5734
            MAC2STR(link_addr ? link_addr : sta->addr));
5735
  hostapd_drv_update_dh_ie(hapd, link_addr ? link_addr : sta->addr,
5736
         status,
5737
         status == WLAN_STATUS_SUCCESS ? ie : NULL,
5738
         ie_len);
5739
5740
  return status;
5741
}
5742
5743
#endif /* CONFIG_OWE */
5744
5745
5746
static bool hapd_is_known_sta(struct hostapd_data *hapd, struct sta_info *sta,
5747
            const u8 *ies, size_t ies_len)
5748
0
{
5749
0
  const u8 *ie, *pos, *end, *timestamp_pos, *mic;
5750
0
  u64 timestamp;
5751
0
  u8 mic_len;
5752
5753
0
  if (!hapd->conf->known_sta_identification)
5754
0
    return false;
5755
5756
0
  ie = get_ie_ext(ies, ies_len, WLAN_EID_EXT_KNOWN_STA_IDENTIFICATION);
5757
0
  if (!ie)
5758
0
    return false;
5759
5760
0
  pos = ie + 3;
5761
0
  end = &ie[2 + ie[1]];
5762
0
  if (end - pos < 8 + 1)
5763
0
    return false; /* truncated element */
5764
0
  timestamp_pos = pos;
5765
0
  timestamp = WPA_GET_LE64(pos);
5766
0
  pos += 8;
5767
0
  mic_len = *pos++;
5768
0
  if (mic_len > end - pos)
5769
0
    return false; /* truncated element */
5770
0
  mic = pos;
5771
5772
0
  wpa_printf(MSG_DEBUG, "RSN: STA " MACSTR
5773
0
       " included Known STA Identification element: Timestamp=0x%llx mic_len=%u",
5774
0
       MAC2STR(sta->addr), (unsigned long long) timestamp, mic_len);
5775
5776
0
  if (timestamp <= sta->last_known_sta_id_timestamp) {
5777
0
    wpa_printf(MSG_DEBUG,
5778
0
         "RSN: Ignore reused or old Known STA Identification");
5779
0
    return false;
5780
0
  }
5781
5782
0
  if (!wpa_auth_sm_known_sta_identification(sta->wpa_sm, timestamp_pos,
5783
0
              mic, mic_len)) {
5784
0
    wpa_printf(MSG_DEBUG,
5785
0
         "RSN: Ignore Known STA Identification with invalid MIC or due to KCK not available");
5786
0
    return false;
5787
0
  }
5788
5789
0
  wpa_printf(MSG_DEBUG, "RSN: Valid Known STA Identification");
5790
0
  sta->last_known_sta_id_timestamp = timestamp;
5791
5792
0
  return true;
5793
0
}
5794
5795
5796
static bool check_sa_query(struct hostapd_data *hapd, struct sta_info *sta,
5797
         int reassoc, const u8 *ies, size_t ies_len,
5798
         bool enc_assoc)
5799
0
{
5800
0
  if ((sta->flags &
5801
0
       (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED)) !=
5802
0
      (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED))
5803
0
    return false;
5804
5805
#ifdef CONFIG_ENC_ASSOC
5806
  if (enc_assoc && sta->epp_sta) {
5807
    /* Skip SA Query since either the STA knows the PTK that is in
5808
     * use in the existing association or a new EPPKE authentication
5809
     * has already authenticated the STA and has replaced the TK and
5810
     * there is not really any point in starting SA Query procedure.
5811
     */
5812
    return false;
5813
  }
5814
#endif /* CONFIG_ENC_ASSOC */
5815
5816
0
  if (!sta->sa_query_timed_out && sta->sa_query_count > 0)
5817
0
    ap_check_sa_query_timeout(hapd, sta);
5818
5819
0
  if (!sta->sa_query_timed_out &&
5820
0
      (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
5821
0
    if (hapd_is_known_sta(hapd, sta, ies, ies_len))
5822
0
      return false;
5823
5824
    /*
5825
     * STA has already been associated with MFP and SA Query timeout
5826
     * has not been reached. Reject the association attempt
5827
     * temporarily and start SA Query, if one is not pending.
5828
     */
5829
0
    if (sta->sa_query_count == 0)
5830
0
      ap_sta_start_sa_query(hapd, sta);
5831
5832
0
    return true;
5833
0
  }
5834
5835
0
  return false;
5836
0
}
5837
5838
5839
static int __check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
5840
           const u8 *ies, size_t ies_len,
5841
           struct ieee802_11_elems *elems,
5842
           enum link_parse_type type,
5843
           struct wpa_state_machine *assoc_wpa_sm)
5844
0
{
5845
0
  int resp;
5846
0
  const u8 *wpa_ie;
5847
0
  size_t wpa_ie_len;
5848
0
  const u8 *p2p_dev_addr = NULL;
5849
0
  const struct security_profile_entry *matched_profile = NULL;
5850
#ifdef CONFIG_PMKSA_PRIVACY
5851
  bool derive_next_pmkid = true;
5852
#endif /* CONFIG_PMKSA_PRIVACY */
5853
#ifdef CONFIG_IEEE8021X_AUTH
5854
  bool mic_check = true;
5855
#endif /* CONFIG_IEEE8021X_AUTH */
5856
#ifdef CONFIG_SAE
5857
  bool epp_sta = false;
5858
5859
#ifdef CONFIG_ENC_ASSOC
5860
  epp_sta = sta->epp_sta;
5861
#endif /* CONFIG_ENC_ASSOC */
5862
#endif /* CONFIG_SAE */
5863
5864
0
  if (type != LINK_PARSE_RECONF) {
5865
0
    resp = check_ssid(hapd, sta, elems->ssid, elems->ssid_len);
5866
0
    if (resp != WLAN_STATUS_SUCCESS)
5867
0
      goto out;
5868
0
  }
5869
5870
0
  resp = check_wmm(hapd, sta, elems->wmm, elems->wmm_len);
5871
0
  if (resp != WLAN_STATUS_SUCCESS)
5872
0
    goto out;
5873
0
  resp = check_ext_capab(hapd, sta, elems->ext_capab,
5874
0
             elems->ext_capab_len);
5875
0
  if (resp != WLAN_STATUS_SUCCESS)
5876
0
    goto out;
5877
0
  resp = copy_supp_rates(hapd, sta, elems);
5878
0
  if (resp != WLAN_STATUS_SUCCESS)
5879
0
    goto out;
5880
5881
0
  resp = check_multi_ap(hapd, sta, elems->multi_ap, elems->multi_ap_len);
5882
0
  if (resp != WLAN_STATUS_SUCCESS)
5883
0
    goto out;
5884
5885
0
  resp = copy_sta_ht_capab(hapd, sta, elems->ht_capabilities);
5886
0
  if (resp != WLAN_STATUS_SUCCESS)
5887
0
    goto out;
5888
0
  if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
5889
0
      !(sta->flags & WLAN_STA_HT)) {
5890
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5891
0
             HOSTAPD_LEVEL_INFO, "Station does not support "
5892
0
             "mandatory HT PHY - reject association");
5893
0
    resp = WLAN_STATUS_ASSOC_DENIED_NO_HT;
5894
0
    goto out;
5895
0
  }
5896
5897
0
#ifdef CONFIG_IEEE80211AC
5898
0
  if (hapd->iconf->ieee80211ac) {
5899
0
    resp = copy_sta_vht_capab(hapd, sta, elems->vht_capabilities);
5900
0
    if (resp != WLAN_STATUS_SUCCESS)
5901
0
      goto out;
5902
5903
0
    resp = set_sta_vht_opmode(hapd, sta, elems->opmode_notif);
5904
0
    if (resp != WLAN_STATUS_SUCCESS)
5905
0
      goto out;
5906
0
  }
5907
5908
0
  if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
5909
0
      !(sta->flags & WLAN_STA_VHT)) {
5910
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
5911
0
             HOSTAPD_LEVEL_INFO, "Station does not support "
5912
0
             "mandatory VHT PHY - reject association");
5913
0
    resp = WLAN_STATUS_ASSOC_DENIED_NO_VHT;
5914
0
    goto out;
5915
0
  }
5916
5917
0
  if (hapd->conf->vendor_vht && !elems->vht_capabilities) {
5918
0
    resp = copy_sta_vendor_vht(hapd, sta, elems->vendor_vht,
5919
0
             elems->vendor_vht_len);
5920
0
    if (resp != WLAN_STATUS_SUCCESS)
5921
0
      goto out;
5922
0
  }
5923
0
#endif /* CONFIG_IEEE80211AC */
5924
0
#ifdef CONFIG_IEEE80211AX
5925
0
  if (hostapd_is_he_enabled(hapd)) {
5926
0
    resp = copy_sta_he_capab(hapd, sta, IEEE80211_MODE_AP,
5927
0
           elems->he_capabilities,
5928
0
           elems->he_capabilities_len);
5929
0
    if (resp != WLAN_STATUS_SUCCESS)
5930
0
      goto out;
5931
5932
0
    if (hapd->iconf->require_he && !(sta->flags & WLAN_STA_HE)) {
5933
0
      hostapd_logger(hapd, sta->addr,
5934
0
               HOSTAPD_MODULE_IEEE80211,
5935
0
               HOSTAPD_LEVEL_INFO,
5936
0
               "Station does not support mandatory HE PHY - reject association");
5937
0
      resp = WLAN_STATUS_DENIED_HE_NOT_SUPPORTED;
5938
0
      goto out;
5939
0
    }
5940
5941
0
    if (is_6ghz_op_class(hapd->iconf->op_class)) {
5942
0
      if (!(sta->flags & WLAN_STA_HE)) {
5943
0
        hostapd_logger(hapd, sta->addr,
5944
0
                 HOSTAPD_MODULE_IEEE80211,
5945
0
                 HOSTAPD_LEVEL_INFO,
5946
0
                 "Station does not support mandatory HE PHY - reject association");
5947
0
        resp = WLAN_STATUS_DENIED_HE_NOT_SUPPORTED;
5948
0
        goto out;
5949
0
      }
5950
0
      resp = copy_sta_he_6ghz_capab(hapd, sta,
5951
0
                  elems->he_6ghz_band_cap);
5952
0
      if (resp != WLAN_STATUS_SUCCESS)
5953
0
        goto out;
5954
0
    }
5955
0
  }
5956
0
#endif /* CONFIG_IEEE80211AX */
5957
0
#ifdef CONFIG_IEEE80211BE
5958
0
  if (hostapd_is_eht_enabled(hapd)) {
5959
0
    resp = copy_sta_eht_capab(hapd, sta, IEEE80211_MODE_AP,
5960
0
            elems->he_capabilities,
5961
0
            elems->he_capabilities_len,
5962
0
            elems->eht_capabilities,
5963
0
            elems->eht_capabilities_len);
5964
0
    if (resp != WLAN_STATUS_SUCCESS)
5965
0
      goto out;
5966
5967
0
    if ((hapd->iconf->require_eht || hapd->conf->bss_require_eht) &&
5968
0
        !(sta->flags & WLAN_STA_EHT)) {
5969
0
      hostapd_logger(hapd, sta->addr,
5970
0
               HOSTAPD_MODULE_IEEE80211,
5971
0
               HOSTAPD_LEVEL_INFO,
5972
0
               "Station does not support mandatory EHT PHY - reject association");
5973
0
      return WLAN_STATUS_DENIED_EHT_NOT_SUPPORTED;
5974
0
    }
5975
5976
0
    if (!assoc_wpa_sm) {
5977
0
      resp = hostapd_process_ml_assoc_req(hapd, elems, sta);
5978
0
      if (resp != WLAN_STATUS_SUCCESS)
5979
0
        goto out;
5980
0
    }
5981
0
  }
5982
0
#endif /* CONFIG_IEEE80211BE */
5983
0
#ifdef CONFIG_IEEE80211BN
5984
0
  if (hostapd_is_uhr_enabled(hapd)) {
5985
0
    resp = copy_sta_uhr_capab(hapd, sta, IEEE80211_MODE_AP,
5986
0
            elems->uhr_capabilities,
5987
0
            elems->uhr_capabilities_len);
5988
0
    if (resp != WLAN_STATUS_SUCCESS)
5989
0
      return resp;
5990
5991
0
    if (hapd->iconf->require_uhr && !(sta->flags & WLAN_STA_UHR)) {
5992
0
      hostapd_logger(hapd, sta->addr,
5993
0
               HOSTAPD_MODULE_IEEE80211,
5994
0
               HOSTAPD_LEVEL_INFO,
5995
0
               "Station does not support mandatory UHR PHY - reject association");
5996
0
      return WLAN_STATUS_DENIED_UHR_NOT_SUPPORTED;
5997
0
    }
5998
0
  }
5999
0
#endif /* CONFIG_IEEE80211BN */
6000
6001
#ifdef CONFIG_P2P
6002
  if (elems->p2p && ies && ies_len) {
6003
    wpabuf_free(sta->p2p_ie);
6004
    sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
6005
                P2P_IE_VENDOR_TYPE);
6006
    if (sta->p2p_ie)
6007
      p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
6008
  } else {
6009
    wpabuf_free(sta->p2p_ie);
6010
    sta->p2p_ie = NULL;
6011
  }
6012
#endif /* CONFIG_P2P */
6013
6014
#ifdef CONFIG_IEEE8021X_AUTH
6015
  /* Per IEEE 802.11bi/D4.0, 12.16.6 ((Re)Association Request/Response
6016
   * frame encryption), if IEEE 802.1X is used and FT protocol is not
6017
   * used, the EPP non-AP STA shall include a MIC element in the
6018
   * (Re)Association Request frame.
6019
   * Skip MIC validation on partner AP MLD links.
6020
   */
6021
#ifdef CONFIG_IEEE80211BE
6022
  if (ap_sta_is_mld(hapd, sta) &&
6023
      hapd->mld_link_id != sta->mld_assoc_link_id)
6024
    mic_check = false;
6025
#endif /* CONFIG_IEEE80211BE */
6026
  if (ap_sta_is_epp(sta) && sta->auth_alg == WLAN_AUTH_802_1X &&
6027
      mic_check) {
6028
    const u8 *data;
6029
    u8 mic_len, data_buf[(255 + 2) * 2], mic[WPA_1X_MAX_MIC_LEN];
6030
    const u8 *aa = hapd->own_addr;
6031
    size_t data_len = 0;
6032
    int ret;
6033
6034
    if (!elems->mic) {
6035
      wpa_printf(MSG_DEBUG, "802.1X: Missing MIC element");
6036
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6037
      goto out;
6038
    }
6039
6040
    if (wpa_key_mgmt_sha384(sta->eap_auth_data.akm))
6041
      mic_len = SHA384_MAC_LEN / 2;
6042
    else
6043
      mic_len = SHA256_MAC_LEN / 2;
6044
6045
    if (mic_len != elems->mic_len) {
6046
      wpa_printf(MSG_DEBUG, "802.1X: Invalid MIC len: %u",
6047
           elems->mic_len);
6048
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6049
      goto out;
6050
    }
6051
6052
#ifdef CONFIG_IEEE80211BE
6053
    if (ap_sta_is_mld(hapd, sta))
6054
      aa = hapd->mld->mld_addr;
6055
#endif /* CONFIG_IEEE80211BE */
6056
6057
    if (hapd->conf->security_profiles) {
6058
      resp = validate_security_profile(
6059
        hapd, sta, elems,
6060
        type == LINK_PARSE_REASSOC ?
6061
        "Reassoc" : "Assoc", &matched_profile);
6062
      if (resp != WLAN_STATUS_SUCCESS) {
6063
        resp = WLAN_STATUS_REJECTED_INVALID_SECURITY_PROFILE;
6064
        goto out;
6065
      }
6066
      wpa_printf(MSG_DEBUG,
6067
           "(Re)Association Request frame Security Profile validated for "
6068
           MACSTR, MAC2STR(sta->addr));
6069
    }
6070
6071
    os_memcpy(data_buf, elems->rsn_ie - 2, elems->rsn_ie_len + 2);
6072
    data_len += 2 + elems->rsn_ie_len;
6073
    os_memcpy(data_buf + data_len, elems->rsnxe - 2,
6074
        elems->rsnxe_len + 2);
6075
    data_len += 2 + elems->rsnxe_len;
6076
6077
    data = data_buf;
6078
    ret = wpa_auth_8021x_mic(sta->eap_auth_data.akm,
6079
           sta->eap_auth_data.ptk.kck,
6080
           sta->eap_auth_data.ptk.kck_len, aa,
6081
           sta->addr, data, data_len,
6082
           NULL, 0, mic);
6083
    wpa_hexdump_key(MSG_DEBUG, "802.1X: Frame MIC",
6084
        elems->mic, elems->mic_len);
6085
    if (ret || os_memcmp(mic, elems->mic, mic_len) != 0) {
6086
      wpa_printf(MSG_INFO, "802.1X: Failed MIC verification");
6087
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6088
      goto out;
6089
    }
6090
  }
6091
#endif /* CONFIG_IEEE8021X_AUTH */
6092
6093
  /* Link Reconfiguration Request frame for add link operation will not
6094
   * have RSN and other security IEs. So, skip the checks.
6095
   */
6096
0
  if (type == LINK_PARSE_RECONF) {
6097
0
    wpa_printf(MSG_DEBUG,
6098
0
         "MLD: Skip security IE checks for Link Reconfiguration request");
6099
0
    goto skip_wpa_ies;
6100
0
  }
6101
6102
0
  if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems->rsn_ie) {
6103
0
    wpa_ie = elems->rsn_ie;
6104
0
    wpa_ie_len = elems->rsn_ie_len;
6105
0
  } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
6106
0
       elems->wpa_ie) {
6107
0
    wpa_ie = elems->wpa_ie;
6108
0
    wpa_ie_len = elems->wpa_ie_len;
6109
0
  } else {
6110
0
    wpa_ie = NULL;
6111
0
    wpa_ie_len = 0;
6112
0
  }
6113
6114
0
#ifdef CONFIG_WPS
6115
0
  sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
6116
0
  if (hapd->conf->wps_state && elems->wps_ie && ies && ies_len) {
6117
0
    wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
6118
0
         "Request - assume WPS is used");
6119
0
    sta->flags |= WLAN_STA_WPS;
6120
0
    wpabuf_free(sta->wps_ie);
6121
0
    sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
6122
0
                WPS_IE_VENDOR_TYPE);
6123
0
    if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
6124
0
      wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
6125
0
      sta->flags |= WLAN_STA_WPS2;
6126
0
    }
6127
0
    wpa_ie = NULL;
6128
0
    wpa_ie_len = 0;
6129
0
    if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
6130
0
      wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
6131
0
           "(Re)Association Request - reject");
6132
0
      resp = WLAN_STATUS_INVALID_ELEMENT;
6133
0
      goto out;
6134
0
    }
6135
0
  } else if (hapd->conf->wps_state && wpa_ie == NULL) {
6136
0
    wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
6137
0
         "(Re)Association Request - possible WPS use");
6138
0
    sta->flags |= WLAN_STA_MAYBE_WPS;
6139
0
  } else
6140
0
#endif /* CONFIG_WPS */
6141
0
  if (hapd->conf->wpa && wpa_ie == NULL) {
6142
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6143
0
             HOSTAPD_LEVEL_INFO,
6144
0
             "No WPA/RSN IE in association request");
6145
0
    resp = WLAN_STATUS_INVALID_ELEMENT;
6146
0
    goto out;
6147
0
  }
6148
6149
0
  if (hapd->conf->wpa && wpa_ie) {
6150
0
    enum wpa_validate_result res;
6151
0
#ifdef CONFIG_IEEE80211BE
6152
0
    struct mld_info *info = &sta->mld_info;
6153
0
    bool init = !sta->wpa_sm;
6154
0
#endif /* CONFIG_IEEE80211BE */
6155
6156
0
    wpa_ie -= 2;
6157
0
    wpa_ie_len += 2;
6158
6159
0
    if (!sta->wpa_sm) {
6160
      /* NOTE: For links other than the assoc-link the
6161
       * separate wpa_sm is only allocated internally to this
6162
       * function.
6163
       */
6164
0
      sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
6165
0
              sta->addr,
6166
0
              p2p_dev_addr);
6167
6168
0
      if (!sta->wpa_sm) {
6169
0
        wpa_printf(MSG_WARNING,
6170
0
             "Failed to initialize RSN state machine");
6171
0
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6172
0
        goto out;
6173
0
      }
6174
#ifdef CONFIG_SAE
6175
      if (sta->sae && sta->sae->state == SAE_ACCEPTED &&
6176
          wpa_key_mgmt_sae_ext_key(sta->sae->akmp))
6177
        wpa_auth_set_hash_alg_sae_ext_key(
6178
          sta->wpa_sm, sta->sae->pmk_len);
6179
#endif /* CONFIG_SAE */
6180
0
    }
6181
6182
0
#ifdef CONFIG_IEEE80211BE
6183
0
    if (ap_sta_is_mld(hapd, sta)) {
6184
0
      wpa_printf(MSG_DEBUG,
6185
0
           "MLD: %s ML info in RSN Authenticator",
6186
0
           init ? "Set" : "Reset");
6187
0
      wpa_auth_set_ml_info(sta->wpa_sm,
6188
0
               sta->mld_assoc_link_id,
6189
0
               info);
6190
0
    }
6191
0
#endif /* CONFIG_IEEE80211BE */
6192
6193
0
    wpa_auth_set_auth_alg(sta->wpa_sm, sta->auth_alg);
6194
0
    if (sta->auth_alg == WLAN_AUTH_SAE)
6195
0
      wpa_auth_set_sae_pw_id(sta->wpa_sm, sta->sae_pw_id,
6196
0
                 sta->sae_pw_id_counter);
6197
0
    wpa_auth_set_rsn_selection(sta->wpa_sm, elems->rsn_selection,
6198
0
             elems->rsn_selection_len);
6199
6200
    /*
6201
     * Security profile validation for SAE and other non-802.1X
6202
     * authentication methods. For 802.1X, this is done above in the
6203
     * CONFIG_IEEE8021X_AUTH block. For SAE, the security profile
6204
     * was validated during the authentication phase, but we need to
6205
     * set get the matched profile here so that
6206
     * wpa_validate_wpa_ie() uses the profile's pairwise cipher
6207
     * (GCMP-256) instead of the AP's rsn_pairwise setting.
6208
     */
6209
0
    if (hapd->conf->security_profiles && !matched_profile &&
6210
0
        elems->security_profile) {
6211
0
      u16 sp_resp = validate_security_profile(
6212
0
        hapd, sta, elems, type == LINK_PARSE_REASSOC ?
6213
0
        "Reassoc" : "Assoc", &matched_profile);
6214
6215
0
      if (sp_resp != WLAN_STATUS_SUCCESS) {
6216
0
        resp = WLAN_STATUS_REJECTED_INVALID_SECURITY_PROFILE;
6217
0
        goto out;
6218
0
      }
6219
0
      wpa_printf(MSG_DEBUG,
6220
0
           "Security Profile validated for "
6221
0
           MACSTR
6222
0
           " in (Re)Association Request frame",
6223
0
           MAC2STR(sta->addr));
6224
0
    }
6225
6226
0
    res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
6227
0
            hapd->iface->freq,
6228
0
            wpa_ie, wpa_ie_len,
6229
0
            elems->rsnxe ? elems->rsnxe - 2 :
6230
0
            NULL,
6231
0
            elems->rsnxe ? elems->rsnxe_len + 2 :
6232
0
            0,
6233
0
            elems->mdie, elems->mdie_len,
6234
0
            elems->owe_dh, elems->owe_dh_len,
6235
0
            assoc_wpa_sm,
6236
0
            ap_sta_is_mld(hapd, sta),
6237
0
            matched_profile);
6238
0
    resp = wpa_res_to_status_code(res);
6239
0
    if (resp != WLAN_STATUS_SUCCESS)
6240
0
      goto out;
6241
6242
0
    if (wpa_auth_uses_mfp(sta->wpa_sm))
6243
0
      sta->flags |= WLAN_STA_MFP;
6244
0
    else
6245
0
      sta->flags &= ~WLAN_STA_MFP;
6246
6247
0
    if (wpa_auth_uses_spp_amsdu(sta->wpa_sm))
6248
0
      sta->flags |= WLAN_STA_SPP_AMSDU;
6249
0
    else
6250
0
      sta->flags &= ~WLAN_STA_SPP_AMSDU;
6251
6252
#ifdef CONFIG_PMKSA_PRIVACY
6253
  /* Per IEEE 802.11bi/D4.0, 12.16.7 (PMKSA caching privacy), when both
6254
   * the AP and non-AP STA support PMKSA caching privacy, the non-AP STA
6255
   * shall include a Nonce element in the (Re)Association Request frame.
6256
   * Skip Nonce element processing for partner AP MLD links. */
6257
#ifdef CONFIG_IEEE80211BE
6258
  if (ap_sta_is_mld(hapd, sta) &&
6259
      hapd->mld_link_id != sta->mld_assoc_link_id)
6260
    derive_next_pmkid = false;
6261
#endif /* CONFIG_IEEE80211BE */
6262
6263
  if (derive_next_pmkid && ap_sta_is_epp(sta) &&
6264
      hapd->conf->pmksa_caching_privacy &&
6265
      ieee802_11_rsnx_capab_len(elems->rsnxe, elems->rsnxe_len,
6266
              WLAN_RSNX_CAPAB_PMKSA_CACHING_PRIVACY)) {
6267
    int akmp;
6268
    size_t pmk_len;
6269
    u8 *pmkid_next;
6270
6271
    if (!elems->nonce) {
6272
      wpa_printf(MSG_DEBUG, "STA " MACSTR
6273
           " did not include Nonce element to compute next PMKID",
6274
           MAC2STR(sta->addr));
6275
      goto skip_pmkid_update;
6276
    }
6277
    os_memcpy(sta->snonce, elems->nonce, NONCE_LEN);
6278
    wpa_hexdump(MSG_DEBUG,
6279
          "RSN: Received SNonce to compute next PMKID",
6280
          sta->snonce, NONCE_LEN);
6281
6282
    switch (sta->auth_alg) {
6283
    case WLAN_AUTH_EPPKE:
6284
      if (!sta->pasn) {
6285
        wpa_printf(MSG_INFO,
6286
             "EPPKE: Missing PASN data - cannot derive a new PMKID");
6287
        goto skip_pmkid_update;
6288
      }
6289
      pmk_len = sta->pasn->pmk_len;
6290
      pmkid_next = sta->epp_pmkid_next;
6291
      break;
6292
#ifdef CONFIG_IEEE8021X_AUTH
6293
    case WLAN_AUTH_802_1X:
6294
      pmk_len = sta->eap_auth_data.pmk_len;
6295
      pmkid_next = sta->eap_auth_data.epp_pmkid_next;
6296
      break;
6297
#endif /* CONFIG_IEEE8021X_AUTH */
6298
    default:
6299
      wpa_printf(MSG_INFO,
6300
           "EPP: Unsupported auth alg %u for PMKID privacy",
6301
           sta->auth_alg);
6302
      goto skip_pmkid_update;
6303
    }
6304
6305
    if (random_get_bytes(sta->anonce, NONCE_LEN) < 0)
6306
      goto skip_pmkid_update;
6307
    wpa_hexdump_key(MSG_DEBUG,
6308
        "EPP: Generated ANonce to compute next PMKID",
6309
        sta->anonce, NONCE_LEN);
6310
6311
    akmp = wpa_auth_sta_key_mgmt(sta->wpa_sm);
6312
    if (akmp < 0 ||
6313
        wpa_auth_epp_derive_new_pmkid(sta->anonce, sta->snonce,
6314
              akmp, pmk_len,
6315
              pmkid_next) < 0) {
6316
      wpa_printf(MSG_INFO,
6317
           "EPP: Failed to generate new PMKID");
6318
      goto skip_pmkid_update;
6319
    }
6320
    wpa_hexdump_key(MSG_DEBUG, "EPP: New PMKID",
6321
        pmkid_next, PMKID_LEN);
6322
  }
6323
skip_pmkid_update:
6324
#endif /* CONFIG_PMKSA_PRIVACY */
6325
6326
0
#ifdef CONFIG_IEEE80211R_AP
6327
0
    if (sta->auth_alg == WLAN_AUTH_FT) {
6328
0
      if (type != LINK_PARSE_REASSOC) {
6329
0
        wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
6330
0
             "to use association (not "
6331
0
             "re-association) with FT auth_alg",
6332
0
             MAC2STR(sta->addr));
6333
0
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6334
0
        goto out;
6335
0
      }
6336
6337
0
      resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
6338
0
                   ies_len);
6339
0
      if (resp != WLAN_STATUS_SUCCESS)
6340
0
        goto out;
6341
0
    }
6342
0
#endif /* CONFIG_IEEE80211R_AP */
6343
6344
0
    if (assoc_wpa_sm)
6345
0
      goto skip_sae_owe;
6346
#ifdef CONFIG_SAE
6347
    if (wpa_auth_uses_sae(sta->wpa_sm) && sta->sae &&
6348
        sta->sae->state == SAE_ACCEPTED) {
6349
      if (!sta->sae->h2e &&
6350
          wpa_key_mgmt_sae_ext_key(wpa_auth_sta_key_mgmt(
6351
                   sta->wpa_sm))) {
6352
        wpa_printf(MSG_DEBUG, "SAE: STA " MACSTR
6353
             " tried to use EXT-KEY AKM without H2E",
6354
             MAC2STR(sta->addr));
6355
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6356
        goto out;
6357
      }
6358
      wpa_auth_add_sae_pmkid(sta->wpa_sm, sta->sae->pmkid);
6359
    }
6360
6361
    if (wpa_auth_uses_sae(sta->wpa_sm) &&
6362
        sta->auth_alg == WLAN_AUTH_OPEN) {
6363
      struct rsn_pmksa_cache_entry *sa;
6364
      sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
6365
      if (!sa || !wpa_key_mgmt_sae(sa->akmp)) {
6366
        wpa_printf(MSG_DEBUG,
6367
             "SAE: No PMKSA cache entry found for "
6368
             MACSTR, MAC2STR(sta->addr));
6369
        resp = WLAN_STATUS_INVALID_PMKID;
6370
        goto out;
6371
      }
6372
      wpa_printf(MSG_DEBUG, "SAE: " MACSTR
6373
           " using PMKSA caching", MAC2STR(sta->addr));
6374
      sae_assign_vlan(hapd, sta, sa->sae_vlan_id);
6375
      if (wpa_key_mgmt_sae_ext_key(sa->akmp))
6376
        wpa_auth_set_hash_alg_sae_ext_key(
6377
          sta->wpa_sm, sa->pmk_len);
6378
    } else if (!epp_sta && wpa_auth_uses_sae(sta->wpa_sm) &&
6379
         sta->auth_alg != WLAN_AUTH_SAE &&
6380
         !(sta->auth_alg == WLAN_AUTH_FT &&
6381
           wpa_auth_uses_ft_sae(sta->wpa_sm))) {
6382
      wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
6383
           "SAE AKM after non-SAE auth_alg %u",
6384
           MAC2STR(sta->addr), sta->auth_alg);
6385
      resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
6386
      goto out;
6387
    }
6388
6389
    if (hapd->conf->sae_pwe == SAE_PWE_BOTH &&
6390
        sta->auth_alg == WLAN_AUTH_SAE &&
6391
        sta->sae && !sta->sae->h2e &&
6392
        (hapd->conf->rsnxe_capab_mask &
6393
         BIT_ULL(WLAN_RSNX_CAPAB_SAE_H2E)) &&
6394
        ieee802_11_rsnx_capab_len(elems->rsnxe, elems->rsnxe_len,
6395
                WLAN_RSNX_CAPAB_SAE_H2E)) {
6396
      if (hapd->conf->sae_accept_h2e_without_use) {
6397
        wpa_printf(MSG_INFO, "SAE: " MACSTR
6398
             " indicates support for SAE H2E, but did not use it - accepting due to sae_accept_h2e_without_use",
6399
             MAC2STR(sta->addr));
6400
      } else {
6401
        wpa_printf(MSG_INFO, "SAE: " MACSTR
6402
             " indicates support for SAE H2E, but did not use it",
6403
             MAC2STR(sta->addr));
6404
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6405
        goto out;
6406
      }
6407
    }
6408
#endif /* CONFIG_SAE */
6409
6410
#ifdef CONFIG_OWE
6411
    if (((hostapd_config_all_key_mgmt(hapd->conf)) &
6412
         WPA_KEY_MGMT_OWE) &&
6413
        wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
6414
        elems->owe_dh) {
6415
      resp = owe_process_assoc_req(hapd, sta, elems->owe_dh,
6416
                 elems->owe_dh_len);
6417
      if (resp != WLAN_STATUS_SUCCESS)
6418
        goto out;
6419
    }
6420
#endif /* CONFIG_OWE */
6421
0
  skip_sae_owe:
6422
6423
#ifdef CONFIG_DPP2
6424
    dpp_pfs_free(sta->dpp_pfs);
6425
    sta->dpp_pfs = NULL;
6426
6427
    if (DPP_VERSION > 1 &&
6428
        (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
6429
        hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
6430
        wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
6431
        elems->owe_dh && !assoc_wpa_sm) {
6432
      sta->dpp_pfs = dpp_pfs_init(
6433
        wpabuf_head(hapd->conf->dpp_netaccesskey),
6434
        wpabuf_len(hapd->conf->dpp_netaccesskey));
6435
      if (!sta->dpp_pfs) {
6436
        wpa_printf(MSG_DEBUG,
6437
             "DPP: Could not initialize PFS");
6438
        /* Try to continue without PFS */
6439
        goto pfs_fail;
6440
      }
6441
6442
      if (dpp_pfs_process(sta->dpp_pfs, elems->owe_dh,
6443
              elems->owe_dh_len) < 0) {
6444
        dpp_pfs_free(sta->dpp_pfs);
6445
        sta->dpp_pfs = NULL;
6446
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6447
        goto out;
6448
      }
6449
    }
6450
    if (!assoc_wpa_sm)
6451
      wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
6452
             sta->dpp_pfs->secret : NULL);
6453
  pfs_fail:
6454
#endif /* CONFIG_DPP2 */
6455
6456
0
    if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
6457
0
        wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
6458
0
      hostapd_logger(hapd, sta->addr,
6459
0
               HOSTAPD_MODULE_IEEE80211,
6460
0
               HOSTAPD_LEVEL_INFO,
6461
0
               "Station tried to use TKIP with HT "
6462
0
               "association");
6463
0
      resp = WLAN_STATUS_CIPHER_OUT_OF_POLICY;
6464
0
      goto out;
6465
0
    }
6466
6467
0
    wpa_auth_set_ssid_protection(
6468
0
      sta->wpa_sm,
6469
0
      hapd->conf->ssid_protection &&
6470
0
      ieee802_11_rsnx_capab_len(
6471
0
        elems->rsnxe, elems->rsnxe_len,
6472
0
        WLAN_RSNX_CAPAB_SSID_PROTECTION));
6473
0
  } else
6474
0
    wpa_auth_sta_no_wpa(sta->wpa_sm);
6475
6476
0
skip_wpa_ies:
6477
6478
#ifdef CONFIG_P2P
6479
  if (ies && ies_len)
6480
    p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
6481
#endif /* CONFIG_P2P */
6482
6483
0
#ifdef CONFIG_HS20
6484
0
  wpabuf_free(sta->hs20_ie);
6485
0
  if (elems->hs20 && elems->hs20_len > 4) {
6486
0
    int release;
6487
6488
0
    sta->hs20_ie = wpabuf_alloc_copy(elems->hs20 + 4,
6489
0
             elems->hs20_len - 4);
6490
0
    release = ((elems->hs20[4] >> 4) & 0x0f) + 1;
6491
0
    if (release >= 2 && !wpa_auth_uses_mfp(sta->wpa_sm) &&
6492
0
        hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
6493
0
      wpa_printf(MSG_DEBUG,
6494
0
           "HS 2.0: PMF not negotiated by release %d station "
6495
0
           MACSTR, release, MAC2STR(sta->addr));
6496
0
      resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
6497
0
      goto out;
6498
0
    }
6499
0
  } else {
6500
0
    sta->hs20_ie = NULL;
6501
0
  }
6502
6503
0
  wpabuf_free(sta->roaming_consortium);
6504
0
  if (elems->roaming_cons_sel)
6505
0
    sta->roaming_consortium = wpabuf_alloc_copy(
6506
0
      elems->roaming_cons_sel + 4,
6507
0
      elems->roaming_cons_sel_len - 4);
6508
0
  else
6509
0
    sta->roaming_consortium = NULL;
6510
0
#endif /* CONFIG_HS20 */
6511
6512
#ifdef CONFIG_FST
6513
  wpabuf_free(sta->mb_ies);
6514
  if (hapd->iface->fst)
6515
    sta->mb_ies = mb_ies_by_info(&elems->mb_ies);
6516
  else
6517
    sta->mb_ies = NULL;
6518
#endif /* CONFIG_FST */
6519
6520
#ifdef CONFIG_MBO
6521
  mbo_ap_check_sta_assoc(hapd, sta, elems);
6522
6523
  if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
6524
      elems->mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
6525
      hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
6526
    wpa_printf(MSG_INFO,
6527
         "MBO: Reject WPA2 association without PMF");
6528
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6529
    goto out;
6530
  }
6531
#endif /* CONFIG_MBO */
6532
6533
#if defined(CONFIG_FILS) && defined(CONFIG_OCV)
6534
  if (type != LINK_PARSE_RECONF &&
6535
      wpa_auth_uses_ocv(sta->wpa_sm) &&
6536
      (sta->auth_alg == WLAN_AUTH_FILS_SK ||
6537
       sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
6538
       sta->auth_alg == WLAN_AUTH_FILS_PK)) {
6539
    struct wpa_channel_info ci;
6540
    int tx_chanwidth;
6541
    int tx_seg1_idx;
6542
    enum oci_verify_result res;
6543
6544
    if (hostapd_drv_channel_info(hapd, &ci) != 0) {
6545
      wpa_printf(MSG_WARNING,
6546
           "Failed to get channel info to validate received OCI in FILS (Re)Association Request frame");
6547
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6548
      goto out;
6549
    }
6550
6551
    if (get_sta_tx_parameters(sta->wpa_sm,
6552
            channel_width_to_int(ci.chanwidth),
6553
            ci.seg1_idx, &tx_chanwidth,
6554
            &tx_seg1_idx) < 0) {
6555
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6556
      goto out;
6557
    }
6558
6559
    res = ocv_verify_tx_params(elems->oci, elems->oci_len, &ci,
6560
             tx_chanwidth, tx_seg1_idx);
6561
    if (wpa_auth_uses_ocv(sta->wpa_sm) == 2 &&
6562
        res == OCI_NOT_FOUND) {
6563
      /* Work around misbehaving STAs */
6564
      wpa_printf(MSG_INFO,
6565
           "FILS: Disable OCV with a STA that does not send OCI");
6566
      wpa_auth_set_ocv(sta->wpa_sm, 0);
6567
    } else if (res != OCI_SUCCESS) {
6568
      wpa_printf(MSG_WARNING, "FILS: OCV failed: %s",
6569
           ocv_errorstr);
6570
      wpa_msg(hapd->msg_ctx, MSG_INFO, OCV_FAILURE "addr="
6571
        MACSTR " frame=fils-reassoc-req error=%s",
6572
        MAC2STR(sta->addr), ocv_errorstr);
6573
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
6574
      goto out;
6575
    }
6576
  }
6577
#endif /* CONFIG_FILS && CONFIG_OCV */
6578
6579
0
  ap_copy_sta_supp_op_classes(sta, elems->supp_op_classes,
6580
0
            elems->supp_op_classes_len);
6581
6582
0
  if ((sta->capability & WLAN_CAPABILITY_RADIO_MEASUREMENT) &&
6583
0
      elems->rrm_enabled &&
6584
0
      elems->rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
6585
0
    os_memcpy(sta->rrm_enabled_capa, elems->rrm_enabled,
6586
0
        sizeof(sta->rrm_enabled_capa));
6587
6588
0
  if (elems->power_capab) {
6589
0
    sta->min_tx_power = elems->power_capab[0];
6590
0
    sta->max_tx_power = elems->power_capab[1];
6591
0
    sta->power_capab = 1;
6592
0
  } else {
6593
0
    sta->power_capab = 0;
6594
0
  }
6595
6596
0
  if (elems->bss_max_idle_period &&
6597
0
      hapd->conf->max_acceptable_idle_period) {
6598
0
    u16 req;
6599
6600
0
    req = WPA_GET_LE16(elems->bss_max_idle_period);
6601
0
    if (req <= hapd->conf->max_acceptable_idle_period)
6602
0
      sta->max_idle_period = req;
6603
0
    else if (hapd->conf->max_acceptable_idle_period >
6604
0
       hapd->conf->ap_max_inactivity)
6605
0
      sta->max_idle_period =
6606
0
        hapd->conf->max_acceptable_idle_period;
6607
0
  }
6608
6609
0
  if (elems->wfa_capab)
6610
0
    hostapd_wfa_capab(hapd, sta, elems->wfa_capab,
6611
0
          elems->wfa_capab + elems->wfa_capab_len);
6612
6613
0
out:
6614
0
  if (resp != WLAN_STATUS_SUCCESS || assoc_wpa_sm) {
6615
0
    struct wpa_state_machine *sm = sta->wpa_sm;
6616
6617
0
#ifdef CONFIG_IEEE80211BE
6618
0
    clear_wpa_sm_for_each_partner_link(hapd, sta);
6619
0
    clear_wpa_sm_for_all_sta(hapd, sm);
6620
0
#endif /* CONFIG_IEEE80211BE */
6621
6622
0
    wpa_auth_sta_deinit(sm);
6623
6624
    /* Only keep a reference to the main wpa_sm and drop the
6625
     * per-link instance.
6626
     * This reference is needed during group rekey handling.
6627
     */
6628
0
    if (resp == WLAN_STATUS_SUCCESS) {
6629
0
      sta->wpa_sm = assoc_wpa_sm;
6630
0
#ifdef CONFIG_IEEE80211BE
6631
0
      set_wpa_sm_for_each_partner_link(hapd, sta,
6632
0
               assoc_wpa_sm);
6633
0
#endif /* CONFIG_IEEE80211BE */
6634
0
    } else {
6635
0
      sta->wpa_sm = NULL;
6636
0
#ifdef CONFIG_IEEE80211BE
6637
0
      clear_wpa_sm_for_each_partner_link(hapd, sta);
6638
0
#endif /* CONFIG_IEEE80211BE */
6639
0
    }
6640
0
  }
6641
6642
0
  if (resp == WLAN_STATUS_SUCCESS && matched_profile)
6643
0
    wpa_auth_set_security_profile(sta->wpa_sm, matched_profile);
6644
6645
0
  return resp;
6646
0
}
6647
6648
6649
static int check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
6650
         const u8 *ies, size_t ies_len,
6651
         enum link_parse_type type)
6652
0
{
6653
0
  struct ieee802_11_elems elems;
6654
6655
0
  if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
6656
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
6657
0
             HOSTAPD_LEVEL_INFO,
6658
0
             "Station sent an invalid association request");
6659
0
    return WLAN_STATUS_UNSPECIFIED_FAILURE;
6660
0
  }
6661
6662
0
  return __check_assoc_ies(hapd, sta, ies, ies_len, &elems, type, NULL);
6663
0
}
6664
6665
6666
#ifdef CONFIG_IEEE80211BE
6667
6668
void ieee80211_ml_build_assoc_resp(struct hostapd_data *hapd,
6669
           struct mld_link_info *link,
6670
           struct sta_info *sta)
6671
0
{
6672
0
  struct hostapd_data *assoc_hapd;
6673
0
  struct sta_info *assoc_sta;
6674
0
  u8 buf[EHT_ML_MAX_STA_PROF_LEN];
6675
0
  u8 *p = buf;
6676
0
  size_t buflen = sizeof(buf);
6677
6678
  /* Capability Info */
6679
0
  WPA_PUT_LE16(p, hostapd_own_capab_info(hapd));
6680
0
  p += 2;
6681
6682
  /* Status Code */
6683
0
  WPA_PUT_LE16(p, link->status);
6684
0
  p += 2;
6685
6686
0
  if (link->status != WLAN_STATUS_SUCCESS)
6687
0
    goto out;
6688
6689
  /* AID is not included */
6690
0
  p = hostapd_eid_supp_rates(hapd, p);
6691
0
  p = hostapd_eid_ext_supp_rates(hapd, p);
6692
0
  p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
6693
0
  p = hostapd_eid_ht_capabilities(hapd, p);
6694
0
  p = hostapd_eid_ht_operation(hapd, p);
6695
6696
0
  if (hostapd_is_vht_enabled(hapd)) {
6697
0
    p = hostapd_eid_vht_capabilities(hapd, p, 0);
6698
0
    p = hostapd_eid_vht_operation(hapd, p);
6699
0
  }
6700
6701
0
  if (hostapd_is_he_enabled(hapd)) {
6702
0
    p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
6703
0
    p = hostapd_eid_he_operation(hapd, p);
6704
0
    p = hostapd_eid_spatial_reuse(hapd, p);
6705
0
    p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
6706
0
    p = hostapd_eid_he_6ghz_band_cap(hapd, p);
6707
0
    if (hostapd_is_eht_enabled(hapd)) {
6708
0
      p = hostapd_eid_eht_capab(hapd, p, IEEE80211_MODE_AP);
6709
0
      p = hostapd_eid_eht_operation(hapd, p);
6710
0
    }
6711
0
  }
6712
6713
0
  p = hostapd_eid_ext_capab(hapd, p, false);
6714
6715
6716
0
  assoc_sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
6717
0
  if (assoc_sta && hapd != assoc_hapd) {
6718
0
    const u8 *rsnxe, *assoc_rsnxe;
6719
#ifdef CONFIG_ENC_ASSOC
6720
    const u8 *rsne, *assoc_rsne;
6721
#endif /* CONFIG_ENC_ASSOC */
6722
0
    u8 ie_count = 0, non_inherit_ie[2];
6723
6724
0
    rsnxe = hostapd_wpa_ie(hapd, WLAN_EID_RSNX);
6725
0
    assoc_rsnxe = hostapd_wpa_ie(assoc_hapd, WLAN_EID_RSNX);
6726
#ifdef CONFIG_ENC_ASSOC
6727
    rsne = hostapd_wpa_ie(hapd, WLAN_EID_RSN);
6728
    assoc_rsne = hostapd_wpa_ie(assoc_hapd, WLAN_EID_RSN);
6729
#endif /* CONFIG_ENC_ASSOC */
6730
6731
0
    if (assoc_hapd->conf->rsn_override_omit_rsnxe)
6732
0
      goto omit_rsnxe;
6733
6734
0
    if (assoc_rsnxe && !rsnxe)
6735
0
      non_inherit_ie[ie_count++] = WLAN_EID_RSNX;
6736
6737
0
    if ((!assoc_rsnxe && rsnxe) ||
6738
0
        (rsnxe && assoc_rsnxe &&
6739
0
         (rsnxe[1] != assoc_rsnxe[1] ||
6740
0
          os_memcmp(rsnxe, assoc_rsnxe, 2 + rsnxe[1]) != 0))) {
6741
0
      size_t rsnx_ie_len;
6742
6743
0
      rsnx_ie_len = 2 + rsnxe[1];
6744
0
      if ((size_t) (buf + buflen - p) >= rsnx_ie_len) {
6745
0
        os_memcpy(p, rsnxe, rsnx_ie_len);
6746
0
        p += rsnx_ie_len;
6747
0
      }
6748
0
    }
6749
0
omit_rsnxe:
6750
#ifdef CONFIG_ENC_ASSOC
6751
    if (ap_sta_is_epp(sta) &&
6752
        wpa_auth_ap_sta_support_assoc_enc(sta->wpa_sm)) {
6753
      if ((rsne && assoc_rsne &&
6754
           (rsne[1] != assoc_rsne[1] ||
6755
            os_memcmp(rsne, assoc_rsne, 2 + rsne[1]) != 0))) {
6756
        size_t rsn_ie_len;
6757
6758
        rsn_ie_len = 2 + rsne[1];
6759
        if ((size_t) (buf + buflen - p) >= rsn_ie_len) {
6760
          os_memcpy(p, rsne, rsn_ie_len);
6761
          p += rsn_ie_len;
6762
        }
6763
      }
6764
    }
6765
#endif /* CONFIG_ENC_ASSOC */
6766
0
    if (ie_count) {
6767
0
      *p++ = WLAN_EID_EXTENSION;
6768
0
      *p++ = 2 + ie_count + 1;
6769
0
      *p++ = WLAN_EID_EXT_NON_INHERITANCE;
6770
0
      *p++ = ie_count;
6771
0
      os_memcpy(p, non_inherit_ie, ie_count);
6772
0
      p += ie_count;
6773
0
      *p++ = 0; /* No Element ID Extension List */
6774
0
    }
6775
0
  }
6776
6777
0
#ifdef CONFIG_IEEE80211BN
6778
0
  if (hostapd_is_uhr_enabled(hapd)) {
6779
0
    p = hostapd_eid_uhr_capab(hapd, p, IEEE80211_MODE_AP);
6780
0
    p = hostapd_eid_uhr_operation(hapd, p, false);
6781
0
  }
6782
0
#endif /* CONFIG_IEEE80211BN */
6783
6784
0
  p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
6785
0
  p = hostapd_eid_wmm(hapd, p);
6786
6787
0
  if (sta)
6788
0
    p = hostapd_eid_wfa_capab(hapd, sta, p);
6789
6790
0
  if (hapd->conf->assocresp_elements &&
6791
0
      (size_t) (buf + buflen - p) >=
6792
0
      wpabuf_len(hapd->conf->assocresp_elements)) {
6793
0
    os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
6794
0
        wpabuf_len(hapd->conf->assocresp_elements));
6795
0
    p += wpabuf_len(hapd->conf->assocresp_elements);
6796
0
  }
6797
6798
0
out:
6799
0
  os_free(link->resp_sta_profile);
6800
0
  link->resp_sta_profile = os_memdup(buf, p - buf);
6801
0
  link->resp_sta_profile_len = link->resp_sta_profile ? p - buf : 0;
6802
0
}
6803
6804
6805
int ieee80211_ml_process_link(struct hostapd_data *hapd,
6806
            struct sta_info *origin_sta,
6807
            struct mld_link_info *link,
6808
            const u8 *ies, size_t ies_len,
6809
            enum link_parse_type type, bool offload,
6810
            bool *set_beacon)
6811
0
{
6812
0
  struct ieee802_11_elems elems;
6813
0
  struct wpabuf *mlbuf = NULL;
6814
0
  struct sta_info *sta = NULL;
6815
0
  u16 status = WLAN_STATUS_SUCCESS;
6816
0
  int i;
6817
6818
0
  wpa_printf(MSG_DEBUG, "MLD: link: link_id=%u, peer=" MACSTR,
6819
0
       hapd->mld_link_id, MAC2STR(link->peer_addr));
6820
6821
0
  if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
6822
0
    wpa_printf(MSG_DEBUG, "MLD: link: Element parsing failed");
6823
0
    status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6824
0
    goto out;
6825
0
  }
6826
6827
0
  sta = ap_get_sta(hapd, origin_sta->addr);
6828
0
  if (sta || TEST_FAIL()) {
6829
0
    wpa_printf(MSG_INFO, "MLD: link: Station already exists");
6830
0
    status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6831
0
    sta = NULL;
6832
0
    goto out;
6833
0
  }
6834
6835
0
  sta = ap_sta_add(hapd, origin_sta->addr);
6836
0
  if (!sta) {
6837
0
    wpa_printf(MSG_DEBUG, "MLD: link: ap_sta_add() failed");
6838
0
    status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
6839
0
    goto out;
6840
0
  }
6841
6842
0
  if (type != LINK_PARSE_RECONF) {
6843
0
    mlbuf = ieee802_11_defrag(elems.basic_mle, elems.basic_mle_len,
6844
0
            true);
6845
0
    if (!mlbuf)
6846
0
      goto out;
6847
6848
0
    if (ieee802_11_parse_link_assoc_req(&elems, mlbuf,
6849
0
                hapd->mld_link_id, true) ==
6850
0
        ParseFailed) {
6851
0
      wpa_printf(MSG_DEBUG,
6852
0
           "MLD: link: Failed to parse association request Multi-Link element");
6853
0
      status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6854
0
      goto out;
6855
0
    }
6856
0
  }
6857
6858
0
  sta->flags |= origin_sta->flags | WLAN_STA_ASSOC_REQ_OK;
6859
0
  sta->mld_assoc_link_id = origin_sta->mld_assoc_link_id;
6860
0
  ap_sta_set_mld(sta, true);
6861
0
  sta->auth_alg = origin_sta->auth_alg;
6862
#ifdef CONFIG_ENC_ASSOC
6863
  sta->epp_sta = origin_sta->epp_sta;
6864
#endif /* CONFIG_ENC_ASSOC */
6865
6866
0
  status = __check_assoc_ies(hapd, sta, NULL, 0, &elems, type,
6867
0
           origin_sta->wpa_sm);
6868
0
  if (status != WLAN_STATUS_SUCCESS) {
6869
0
    wpa_printf(MSG_DEBUG, "MLD: link: Element check failed");
6870
0
    goto out;
6871
0
  }
6872
6873
0
  os_memcpy(&sta->mld_info, &origin_sta->mld_info, sizeof(sta->mld_info));
6874
0
  for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
6875
0
    struct mld_link_info *li = &sta->mld_info.links[i];
6876
6877
0
    li->resp_sta_profile = NULL;
6878
0
    li->resp_sta_profile_len = 0;
6879
6880
0
    if (type == LINK_PARSE_RECONF && i == hapd->mld_link_id) {
6881
0
      os_memcpy(li->local_addr, hapd->own_addr, ETH_ALEN);
6882
0
      os_memcpy(li->peer_addr, link->peer_addr, ETH_ALEN);
6883
0
    }
6884
0
  }
6885
6886
0
  if (!offload) {
6887
    /*
6888
     * Get the AID from the station on which the association was
6889
     * performed, and mark it as used.
6890
     */
6891
0
    sta->aid = origin_sta->aid;
6892
0
    if (sta->aid == 0) {
6893
0
      wpa_printf(MSG_DEBUG, "MLD: link: No AID assigned");
6894
0
      status = WLAN_STATUS_UNSPECIFIED_FAILURE;
6895
0
      goto out;
6896
0
    }
6897
0
    hapd->sta_aid[(sta->aid - 1) / 32] |= BIT((sta->aid - 1) % 32);
6898
0
    sta->listen_interval = origin_sta->listen_interval;
6899
0
    if (update_ht_state(hapd, sta) > 0 && set_beacon)
6900
0
      *set_beacon = true;
6901
0
  }
6902
6903
  /*
6904
   * Do not initialize the EAPOL state machine.
6905
   * TODO: Maybe it is needed?
6906
   */
6907
0
  sta->eapol_sm = NULL;
6908
6909
0
  wpa_printf(MSG_DEBUG, "MLD: link=%u, association OK (aid=%u)",
6910
0
       hapd->mld_link_id, sta->aid);
6911
6912
0
  sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC_REQ_OK;
6913
0
  sta->vlan_id = origin_sta->vlan_id;
6914
6915
  /* TODO: What other processing is required? */
6916
6917
0
  if (!offload &&
6918
0
      add_associated_sta(hapd, sta, type == LINK_PARSE_REASSOC))
6919
0
    status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
6920
0
out:
6921
0
  wpabuf_free(mlbuf);
6922
0
  link->status = status;
6923
6924
0
  if (!offload && type != LINK_PARSE_RECONF)
6925
0
    ieee80211_ml_build_assoc_resp(hapd, link, sta);
6926
6927
0
  wpa_printf(MSG_DEBUG, "MLD: link: status=%u", status);
6928
0
  if (status != WLAN_STATUS_SUCCESS) {
6929
0
    wpa_release_link_auth_ref(origin_sta->wpa_sm,
6930
0
            hapd->mld_link_id, true);
6931
0
    if (sta)
6932
0
      ap_free_sta(hapd, sta);
6933
0
    return -1;
6934
0
  }
6935
6936
0
  return 0;
6937
0
}
6938
6939
6940
bool hostapd_is_multiple_link_mld(struct hostapd_data *hapd)
6941
0
{
6942
0
  struct hostapd_data *bss;
6943
6944
0
  if (!hapd->conf->mld_ap)
6945
0
    return false;
6946
6947
0
  if (!hapd->iface || !hapd->iface->interfaces ||
6948
0
      hapd->iface->interfaces->count <= 1)
6949
0
    return false;
6950
6951
  /*
6952
   * Checking for interfaces count above is not sufficient as there
6953
   * could be non-MLD interfaces or MLD interface that are not affiliated
6954
   * with the same MLD as the currently processing one. So need to check
6955
   * if other partner links exist for this the same AP MLD.
6956
   */
6957
0
  for_each_mld_link(bss, hapd) {
6958
0
    if (bss != hapd)
6959
0
      return true;
6960
0
  }
6961
6962
0
  return false;
6963
0
}
6964
6965
#endif /* CONFIG_IEEE80211BE */
6966
6967
6968
int hostapd_process_assoc_ml_info(struct hostapd_data *hapd,
6969
          struct sta_info *sta,
6970
          const u8 *ies, size_t ies_len,
6971
          bool reassoc, int tx_link_status,
6972
          bool offload,
6973
          bool *set_beacon)
6974
0
{
6975
0
  int ret = 0;
6976
0
#ifdef CONFIG_IEEE80211BE
6977
0
  unsigned int i;
6978
6979
0
  if (!hostapd_is_multiple_link_mld(hapd))
6980
0
    return 0;
6981
6982
0
  for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
6983
0
    struct hostapd_data *bss = NULL;
6984
0
    struct mld_link_info *link = &sta->mld_info.links[i];
6985
0
    bool link_bss_found = false;
6986
6987
0
    if (!link->valid || i == sta->mld_assoc_link_id)
6988
0
      continue;
6989
6990
0
    for_each_mld_link(bss, hapd) {
6991
0
      if (bss == hapd)
6992
0
        continue;
6993
6994
0
      if (bss->mld_link_id != i)
6995
0
        continue;
6996
6997
0
      link_bss_found = true;
6998
0
      break;
6999
0
    }
7000
7001
0
    if (!link_bss_found || TEST_FAIL()) {
7002
0
      wpa_printf(MSG_DEBUG,
7003
0
           "MLD: No link match for link_id=%u", i);
7004
7005
0
      link->status = WLAN_STATUS_UNSPECIFIED_FAILURE;
7006
0
      if (!offload)
7007
0
        ieee80211_ml_build_assoc_resp(hapd, link, sta);
7008
0
    } else if (tx_link_status != WLAN_STATUS_SUCCESS) {
7009
      /* TX link rejected the connection */
7010
0
      link->status = WLAN_STATUS_DENIED_TX_LINK_NOT_ACCEPTED;
7011
0
      if (!offload)
7012
0
        ieee80211_ml_build_assoc_resp(hapd, link, sta);
7013
0
    } else {
7014
0
      if (ieee80211_ml_process_link(
7015
0
            bss, sta, link, ies, ies_len,
7016
0
            reassoc ? LINK_PARSE_REASSOC :
7017
0
            LINK_PARSE_ASSOC, offload,
7018
0
            set_beacon))
7019
0
        ret = -1;
7020
0
    }
7021
0
  }
7022
0
#endif /* CONFIG_IEEE80211BE */
7023
7024
0
  return ret;
7025
0
}
7026
7027
7028
static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
7029
      u16 reason_code)
7030
0
{
7031
0
  int send_len;
7032
0
  struct ieee80211_mgmt reply;
7033
7034
0
  os_memset(&reply, 0, sizeof(reply));
7035
0
  reply.frame_control =
7036
0
    IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
7037
0
  os_memcpy(reply.da, addr, ETH_ALEN);
7038
0
  os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
7039
0
  os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
7040
7041
0
  send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
7042
0
  reply.u.deauth.reason_code = host_to_le16(reason_code);
7043
7044
0
  if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0, NULL, 0, 0) < 0)
7045
0
    wpa_printf(MSG_INFO, "Failed to send deauth: %s",
7046
0
         strerror(errno));
7047
0
}
7048
7049
7050
static int add_associated_sta(struct hostapd_data *hapd,
7051
            struct sta_info *sta, int reassoc)
7052
0
{
7053
0
  struct ieee80211_ht_capabilities ht_cap;
7054
0
  struct ieee80211_vht_capabilities vht_cap;
7055
0
  struct ieee80211_he_capabilities he_cap;
7056
0
  struct ieee80211_eht_capabilities eht_cap;
7057
0
  struct ieee80211_uhr_capabilities uhr_cap;
7058
0
  int set = 1;
7059
0
  const u8 *mld_link_addr = NULL;
7060
0
  bool mld_link_sta = false, epp_sta = false;
7061
0
  u16 eml_cap = 0;
7062
7063
#ifdef CONFIG_ENC_ASSOC
7064
  epp_sta = sta->epp_sta;
7065
#endif /* CONFIG_ENC_ASSOC */
7066
7067
0
#ifdef CONFIG_IEEE80211BE
7068
0
  if (ap_sta_is_mld(hapd, sta)) {
7069
0
    u8 mld_link_id = hapd->mld_link_id;
7070
7071
0
    mld_link_sta = sta->mld_assoc_link_id != mld_link_id;
7072
0
    mld_link_addr = sta->mld_info.links[mld_link_id].peer_addr;
7073
7074
0
    if (hapd->mld_link_id != sta->mld_assoc_link_id)
7075
0
      set = 0;
7076
0
    eml_cap = sta->mld_info.common_info.eml_capa;
7077
0
  }
7078
0
#endif /* CONFIG_IEEE80211BE */
7079
7080
  /*
7081
   * Remove the STA entry to ensure the STA PS state gets cleared and
7082
   * configuration gets updated. This is relevant for cases, such as
7083
   * FT-over-the-DS, where a station re-associates back to the same AP but
7084
   * skips the authentication flow, or if working with a driver that
7085
   * does not support full AP client state.
7086
   *
7087
   * Skip this if the STA has already completed FT reassociation and the
7088
   * TK has been configured since the TX/RX PN must not be reset to 0 for
7089
   * the same key.
7090
   *
7091
   * FT-over-the-DS has a special case where the STA entry (and as such,
7092
   * the TK) has not yet been configured to the driver depending on which
7093
   * driver interface is used. For that case, allow add-STA operation to
7094
   * be used (instead of set-STA). This is needed to allow mac80211-based
7095
   * drivers to accept the STA parameter configuration. Since this is
7096
   * after a new FT-over-DS exchange, a new TK has been derived, so key
7097
   * reinstallation is not a concern for this case.
7098
   */
7099
0
  wpa_printf(MSG_DEBUG, "Add associated STA " MACSTR
7100
0
       " (added_unassoc=%d auth_alg=%u ft_over_ds=%u reassoc=%d authorized=%d ft_tk=%d fils_tk=%d)",
7101
0
       MAC2STR(sta->addr), sta->added_unassoc, sta->auth_alg,
7102
0
       sta->ft_over_ds, reassoc,
7103
0
       !!(sta->flags & WLAN_STA_AUTHORIZED),
7104
0
       wpa_auth_sta_ft_tk_already_set(sta->wpa_sm),
7105
0
       wpa_auth_sta_fils_tk_already_set(sta->wpa_sm));
7106
7107
0
  if (!mld_link_sta && !sta->added_unassoc &&
7108
0
      (!(sta->flags & WLAN_STA_AUTHORIZED) ||
7109
0
       (reassoc && sta->ft_over_ds && sta->auth_alg == WLAN_AUTH_FT) ||
7110
0
       (!wpa_auth_sta_ft_tk_already_set(sta->wpa_sm) &&
7111
0
        !wpa_auth_sta_fils_tk_already_set(sta->wpa_sm)))) {
7112
0
    hostapd_drv_sta_remove(hapd, sta->addr);
7113
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
7114
0
    set = 0;
7115
7116
     /* Do not allow the FT-over-DS exception to be used more than
7117
      * once per authentication exchange to guarantee a new TK is
7118
      * used here */
7119
0
    sta->ft_over_ds = 0;
7120
0
  }
7121
7122
0
  if (sta->flags & WLAN_STA_HT)
7123
0
    hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
7124
0
#ifdef CONFIG_IEEE80211AC
7125
0
  if (sta->flags & WLAN_STA_VHT)
7126
0
    hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
7127
0
#endif /* CONFIG_IEEE80211AC */
7128
0
#ifdef CONFIG_IEEE80211AX
7129
0
  if (sta->flags & WLAN_STA_HE) {
7130
0
    hostapd_get_he_capab(hapd, sta->he_capab, &he_cap,
7131
0
             sta->he_capab_len);
7132
0
  }
7133
0
#endif /* CONFIG_IEEE80211AX */
7134
0
#ifdef CONFIG_IEEE80211BE
7135
0
  if (sta->flags & WLAN_STA_EHT)
7136
0
    hostapd_get_eht_capab(hapd, sta->eht_capab, &eht_cap,
7137
0
              sta->eht_capab_len);
7138
0
#endif /* CONFIG_IEEE80211BE */
7139
0
#ifdef CONFIG_IEEE80211BN
7140
0
  if (sta->flags & WLAN_STA_UHR)
7141
0
    hostapd_get_uhr_capab(hapd, sta->uhr_capab, &uhr_cap,
7142
0
              sta->uhr_capab_len);
7143
0
#endif /* CONFIG_IEEE80211BN */
7144
7145
  /*
7146
   * Add the station with forced WLAN_STA_ASSOC flag. The sta->flags
7147
   * will be set when the ACK frame for the (Re)Association Response frame
7148
   * is processed (TX status driver event).
7149
   */
7150
0
  if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
7151
0
          sta->supported_rates, sta->supported_rates_len,
7152
0
          sta->listen_interval,
7153
0
          sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
7154
0
          sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
7155
0
          sta->flags & WLAN_STA_HE ? &he_cap : NULL,
7156
0
          sta->flags & WLAN_STA_HE ? sta->he_capab_len : 0,
7157
0
          sta->flags & WLAN_STA_EHT ? &eht_cap : NULL,
7158
0
          sta->flags & WLAN_STA_EHT ? sta->eht_capab_len : 0,
7159
0
          sta->flags & WLAN_STA_UHR ? &uhr_cap : NULL,
7160
0
          sta->flags & WLAN_STA_UHR ? sta->uhr_capab_len : 0,
7161
0
          sta->he_6ghz_capab,
7162
0
          sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
7163
0
          sta->vht_opmode, sta->p2p_ie ? 1 : 0,
7164
0
          set, mld_link_addr, mld_link_sta, eml_cap,
7165
0
          epp_sta)) {
7166
0
    hostapd_logger(hapd, sta->addr,
7167
0
             HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
7168
0
             "Could not %s STA to kernel driver",
7169
0
             set ? "set" : "add");
7170
7171
0
    if (sta->added_unassoc) {
7172
0
      hostapd_drv_sta_remove(hapd, sta->addr);
7173
0
      sta->added_unassoc = 0;
7174
0
    }
7175
7176
0
    return -1;
7177
0
  }
7178
7179
0
  sta->added_unassoc = 0;
7180
7181
0
  return 0;
7182
0
}
7183
7184
7185
static u16 send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
7186
         const u8 *addr, u16 status_code, int reassoc,
7187
         const u8 *ies, size_t ies_len, int rssi,
7188
         int omit_rsnxe)
7189
0
{
7190
0
  int send_len;
7191
0
  u8 *buf;
7192
0
  size_t buflen;
7193
0
  struct ieee80211_mgmt *reply;
7194
0
  u8 *p;
7195
0
  u16 res = WLAN_STATUS_SUCCESS;
7196
7197
0
  buflen = sizeof(struct ieee80211_mgmt) + 1024;
7198
#ifdef CONFIG_FILS
7199
  if (sta && sta->fils_hlp_resp)
7200
    buflen += wpabuf_len(sta->fils_hlp_resp);
7201
  if (sta)
7202
    buflen += 150;
7203
#endif /* CONFIG_FILS */
7204
#ifdef CONFIG_OWE
7205
  if (sta &&
7206
      ((hostapd_config_all_key_mgmt(hapd->conf)) & WPA_KEY_MGMT_OWE))
7207
    buflen += 150;
7208
#endif /* CONFIG_OWE */
7209
#ifdef CONFIG_DPP2
7210
  if (sta && sta->dpp_pfs)
7211
    buflen += 5 + sta->dpp_pfs->curve->prime_len;
7212
#endif /* CONFIG_DPP2 */
7213
0
#ifdef CONFIG_IEEE80211BE
7214
0
  if (hostapd_is_eht_enabled(hapd)) {
7215
0
    buflen += hostapd_eid_eht_capab_len(hapd, IEEE80211_MODE_AP);
7216
0
    buflen += 3 + sizeof(struct ieee80211_eht_operation);
7217
0
    if (hapd->iconf->punct_bitmap)
7218
0
      buflen += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
7219
0
    if (ap_sta_is_mld(hapd, sta)) {
7220
0
      buflen += hostapd_eid_eht_ml_tid_to_link_map_len(hapd);
7221
0
      buflen += hostapd_eid_eht_basic_ml_len(hapd, sta,
7222
0
                     false, true);
7223
0
    }
7224
0
  }
7225
0
#endif /* CONFIG_IEEE80211BE */
7226
0
#ifdef CONFIG_IEEE80211BN
7227
0
  if (hostapd_is_uhr_enabled(hapd)) {
7228
0
    buflen += hostapd_eid_uhr_capab_len(hapd, IEEE80211_MODE_AP);
7229
0
    buflen += 3 + IEEE80211_UHR_OPER_MAX_SIZE;
7230
0
  }
7231
0
#endif /* CONFIG_IEEE80211BN */
7232
7233
0
  if (hapd->conf->security_profiles)
7234
0
    buflen += hostapd_security_profile_len(hapd);
7235
7236
0
  buf = os_zalloc(buflen);
7237
0
  if (!buf) {
7238
0
    res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7239
0
    goto done;
7240
0
  }
7241
0
  reply = (struct ieee80211_mgmt *) buf;
7242
0
  reply->frame_control =
7243
0
    IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7244
0
           (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
7245
0
            WLAN_FC_STYPE_ASSOC_RESP));
7246
7247
0
  os_memcpy(reply->da, addr, ETH_ALEN);
7248
0
  os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
7249
0
  os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
7250
7251
0
  send_len = IEEE80211_HDRLEN;
7252
0
  send_len += sizeof(reply->u.assoc_resp);
7253
0
  reply->u.assoc_resp.capab_info =
7254
0
    host_to_le16(hostapd_own_capab_info(hapd));
7255
0
  reply->u.assoc_resp.status_code = host_to_le16(status_code);
7256
7257
0
  reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0) |
7258
0
                 BIT(14) | BIT(15));
7259
  /* Supported rates */
7260
0
  p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
7261
  /* Extended supported rates */
7262
0
  p = hostapd_eid_ext_supp_rates(hapd, p);
7263
7264
  /* Radio measurement capabilities */
7265
0
  p = hostapd_eid_rm_enabled_capab(hapd, p, buf + buflen - p);
7266
7267
#ifdef CONFIG_MBO
7268
  if (status_code == WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
7269
      rssi != 0) {
7270
    int delta = hapd->iconf->rssi_reject_assoc_rssi - rssi;
7271
7272
    p = hostapd_eid_mbo_rssi_assoc_rej(hapd, p, buf + buflen - p,
7273
               delta);
7274
  }
7275
#endif /* CONFIG_MBO */
7276
7277
0
#ifdef CONFIG_IEEE80211R_AP
7278
0
  if (sta && status_code == WLAN_STATUS_SUCCESS) {
7279
    /* IEEE 802.11r: Mobility Domain Information, Fast BSS
7280
     * Transition Information, RSN, [RIC Response] */
7281
0
    p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
7282
0
            buf + buflen - p,
7283
0
            sta->auth_alg, ies, ies_len,
7284
0
            omit_rsnxe, reassoc,
7285
0
            sta->vlan_id);
7286
0
    if (!p) {
7287
0
      wpa_printf(MSG_DEBUG,
7288
0
           "FT: Failed to write AssocResp IEs");
7289
0
      res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7290
0
      goto done;
7291
0
    }
7292
0
  }
7293
0
#endif /* CONFIG_IEEE80211R_AP */
7294
#ifdef CONFIG_FILS
7295
  if (sta && status_code == WLAN_STATUS_SUCCESS &&
7296
      (sta->auth_alg == WLAN_AUTH_FILS_SK ||
7297
       sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
7298
       sta->auth_alg == WLAN_AUTH_FILS_PK))
7299
    p = wpa_auth_write_assoc_resp_fils(sta->wpa_sm, p,
7300
               buf + buflen - p);
7301
#endif /* CONFIG_FILS */
7302
7303
#ifdef CONFIG_OWE
7304
  if (sta && status_code == WLAN_STATUS_SUCCESS &&
7305
      ((hostapd_config_all_key_mgmt(hapd->conf)) & WPA_KEY_MGMT_OWE))
7306
    p = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, p,
7307
              buf + buflen - p);
7308
#endif /* CONFIG_OWE */
7309
7310
0
  if (sta && status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
7311
0
    p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
7312
7313
0
  p = hostapd_eid_ht_capabilities(hapd, p);
7314
0
  p = hostapd_eid_ht_operation(hapd, p);
7315
7316
0
#ifdef CONFIG_IEEE80211AC
7317
0
  if (hostapd_is_vht_enabled(hapd) &&
7318
0
      !is_6ghz_op_class(hapd->iconf->op_class)) {
7319
0
    u32 nsts = 0, sta_nsts;
7320
7321
0
    if (sta && hapd->conf->use_sta_nsts && sta->vht_capabilities) {
7322
0
      struct ieee80211_vht_capabilities *capa;
7323
7324
0
      nsts = (hapd->iface->conf->vht_capab >>
7325
0
        VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
7326
0
      capa = sta->vht_capabilities;
7327
0
      sta_nsts = (le_to_host32(capa->vht_capabilities_info) >>
7328
0
            VHT_CAP_BEAMFORMEE_STS_OFFSET) & 7;
7329
7330
0
      if (nsts < sta_nsts)
7331
0
        nsts = 0;
7332
0
      else
7333
0
        nsts = sta_nsts;
7334
0
    }
7335
0
    p = hostapd_eid_vht_capabilities(hapd, p, nsts);
7336
0
    p = hostapd_eid_vht_operation(hapd, p);
7337
0
  }
7338
0
#endif /* CONFIG_IEEE80211AC */
7339
7340
0
#ifdef CONFIG_IEEE80211AX
7341
0
  if (hostapd_is_he_enabled(hapd)) {
7342
0
    p = hostapd_eid_he_capab(hapd, p, IEEE80211_MODE_AP);
7343
0
    p = hostapd_eid_he_operation(hapd, p);
7344
0
    p = hostapd_eid_cca(hapd, p);
7345
0
    p = hostapd_eid_spatial_reuse(hapd, p);
7346
0
    p = hostapd_eid_he_mu_edca_parameter_set(hapd, p);
7347
0
    p = hostapd_eid_he_6ghz_band_cap(hapd, p);
7348
0
  }
7349
0
#endif /* CONFIG_IEEE80211AX */
7350
7351
0
  p = hostapd_eid_ext_capab(hapd, p, false);
7352
0
  p = hostapd_eid_bss_max_idle_period(hapd, p,
7353
0
              sta ? sta->max_idle_period : 0);
7354
0
  if (sta && sta->qos_map_enabled)
7355
0
    p = hostapd_eid_qos_map_set(hapd, p);
7356
7357
#ifdef CONFIG_FST
7358
  if (hapd->iface->fst_ies) {
7359
    os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
7360
        wpabuf_len(hapd->iface->fst_ies));
7361
    p += wpabuf_len(hapd->iface->fst_ies);
7362
  }
7363
#endif /* CONFIG_FST */
7364
7365
#ifdef CONFIG_TESTING_OPTIONS
7366
  if (hapd->conf->rsnxe_override_ft &&
7367
      buf + buflen - p >=
7368
      (long int) wpabuf_len(hapd->conf->rsnxe_override_ft) &&
7369
      sta && sta->auth_alg == WLAN_AUTH_FT) {
7370
    wpa_printf(MSG_DEBUG, "TESTING: RSNXE FT override");
7371
    os_memcpy(p, wpabuf_head(hapd->conf->rsnxe_override_ft),
7372
        wpabuf_len(hapd->conf->rsnxe_override_ft));
7373
    p += wpabuf_len(hapd->conf->rsnxe_override_ft);
7374
    goto rsnxe_done;
7375
  }
7376
#endif /* CONFIG_TESTING_OPTIONS */
7377
0
  if (!omit_rsnxe)
7378
0
    p = hostapd_eid_rsnxe(hapd, p, buf + buflen - p,
7379
0
              hapd->conf->rsnxe_capab_mask);
7380
#ifdef CONFIG_TESTING_OPTIONS
7381
rsnxe_done:
7382
#endif /* CONFIG_TESTING_OPTIONS */
7383
7384
0
#ifdef CONFIG_IEEE80211BE
7385
0
  if (hostapd_is_eht_enabled(hapd)) {
7386
0
    if (hapd->conf->mld_ap)
7387
0
      p = hostapd_eid_eht_ml_assoc(hapd, sta, p);
7388
0
    p = hostapd_eid_eht_capab(hapd, p, IEEE80211_MODE_AP);
7389
0
    p = hostapd_eid_eht_operation(hapd, p);
7390
0
    if (ap_sta_is_mld(hapd, sta))
7391
0
      p = hostapd_eid_eht_ml_tid_to_link_map(hapd, p);
7392
0
  }
7393
0
#endif /* CONFIG_IEEE80211BE */
7394
7395
0
#ifdef CONFIG_IEEE80211BN
7396
0
  if (hostapd_is_uhr_enabled(hapd)) {
7397
0
    p = hostapd_eid_uhr_capab(hapd, p, IEEE80211_MODE_AP);
7398
0
    p = hostapd_eid_uhr_operation(hapd, p, false);
7399
0
  }
7400
0
#endif /* CONFIG_IEEE80211BN */
7401
7402
#ifdef CONFIG_OWE
7403
  if (((hostapd_config_all_key_mgmt(hapd->conf)) & WPA_KEY_MGMT_OWE) &&
7404
      sta && sta->owe_ecdh && status_code == WLAN_STATUS_SUCCESS &&
7405
      wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
7406
      !wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
7407
    struct wpabuf *pub;
7408
7409
    pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
7410
    if (!pub) {
7411
      res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7412
      goto done;
7413
    }
7414
    /* OWE Diffie-Hellman Parameter element */
7415
    *p++ = WLAN_EID_EXTENSION; /* Element ID */
7416
    *p++ = 1 + 2 + wpabuf_len(pub); /* Length */
7417
    *p++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension */
7418
    WPA_PUT_LE16(p, sta->owe_group);
7419
    p += 2;
7420
    os_memcpy(p, wpabuf_head(pub), wpabuf_len(pub));
7421
    p += wpabuf_len(pub);
7422
    wpabuf_free(pub);
7423
  }
7424
#endif /* CONFIG_OWE */
7425
7426
#ifdef CONFIG_ENC_ASSOC
7427
  if (sta &&
7428
      (sta->auth_alg == WLAN_AUTH_EPPKE ||
7429
       sta->auth_alg == WLAN_AUTH_802_1X) &&
7430
      wpa_auth_ap_sta_support_assoc_enc(sta->wpa_sm) &&
7431
      status_code == WLAN_STATUS_SUCCESS) {
7432
    int left;
7433
7434
    /* Ensure GMK/Counter are initialized before the Key Delivery
7435
     * element is built. For auth-frame STAs the normal 4-way
7436
     * handshake path (SM_STATE AUTHENTICATION2) is skipped, so
7437
     * wpa_group_ensure_init() must be called here instead.
7438
     * Without this, the first non-auth-frame STA that connects
7439
     * later triggers a GTK rotation that is unknown to this STA,
7440
     * breaking broadcast frame reception. */
7441
    wpa_auth_ensure_group_init(sta->wpa_sm);
7442
    reply->frame_control |= WLAN_FC_PROTECTED;
7443
7444
#ifdef CONFIG_PMKSA_PRIVACY
7445
    /* Include a Nonce element (ANonce) to compute next PMKID */
7446
    if (wpa_auth_ap_sta_support_pmkid_privacy(sta->wpa_sm)) {
7447
      switch (sta->auth_alg) {
7448
      case WLAN_AUTH_EPPKE:
7449
#ifdef CONFIG_IEEE8021X_AUTH
7450
      case WLAN_AUTH_802_1X:
7451
#endif /* CONFIG_IEEE8021X_AUTH */
7452
        break;
7453
      default:
7454
        wpa_printf(MSG_INFO,
7455
             "EPP: Unsupported auth alg %u for PMKID privacy support",
7456
             sta->auth_alg);
7457
        goto skip_nonce;
7458
      }
7459
7460
      *p++ = WLAN_EID_EXTENSION; /* Element ID */
7461
      *p++ = 1 + NONCE_LEN; /* Length */
7462
      *p++ = WLAN_EID_EXT_NONCE; /* Element ID Extension */
7463
      os_memcpy(p, sta->anonce, NONCE_LEN);
7464
      p += NONCE_LEN;
7465
    }
7466
  skip_nonce:
7467
#ifdef CONFIG_SAE
7468
    /* For EPPKE with SAE, propagate the password identifier from
7469
     * the PASN wrapped SAE commit to the WPA state machine so that
7470
     * wpa_auth_eid_key_delivery() can include the SAE PW IDs KDE
7471
     * in the encrypted (Re)Association Response frame.
7472
     */
7473
    if (sta->auth_alg == WLAN_AUTH_EPPKE && sta->pasn &&
7474
        wpa_key_mgmt_sae(wpa_auth_sta_key_mgmt(sta->wpa_sm))) {
7475
      const u8 *pw_id = NULL;
7476
      size_t pw_id_len = 0;
7477
7478
      /* Use the decrypted (real) identifier when the STA
7479
       * presented an encrypted alternative identifier.
7480
       * Fall back to the raw parsed identifier for the
7481
       * plaintext case (first connection). */
7482
      if (sta->pasn->dec_pw_id &&
7483
          sta->pasn->dec_pw_id_len) {
7484
        pw_id = sta->pasn->dec_pw_id;
7485
        pw_id_len = sta->pasn->dec_pw_id_len;
7486
      } else if (sta->pasn->sae.tmp) {
7487
        if (sta->pasn->sae.tmp->parsed_pw_id) {
7488
          pw_id = sta->pasn->sae.tmp->parsed_pw_id;
7489
          pw_id_len = sta->pasn->sae.tmp->parsed_pw_id_len;
7490
        } else if (sta->pasn->sae.tmp->pw_id) {
7491
          pw_id = sta->pasn->sae.tmp->pw_id;
7492
          pw_id_len =
7493
          sta->pasn->sae.tmp->pw_id_len;
7494
        }
7495
      }
7496
      if (pw_id && pw_id_len) {
7497
        struct wpabuf *pw_id_buf;
7498
7499
        pw_id_buf = wpabuf_alloc_copy(pw_id, pw_id_len);
7500
        if (pw_id_buf) {
7501
          wpa_auth_set_sae_pw_id(
7502
            sta->wpa_sm, pw_id_buf,
7503
            sta->pasn->sae_pw_id_counter);
7504
          wpabuf_free(pw_id_buf);
7505
        }
7506
      }
7507
    }
7508
#endif /* CONFIG_SAE */
7509
#endif /* CONFIG_PMKSA_PRIVACY */
7510
7511
    left = buf + buflen - p - hostapd_security_profile_len(hapd);
7512
    if (left <= 0) {
7513
      wpa_printf(MSG_ERROR,
7514
           "EPP: No room for EPPKE/security profile in assoc resp");
7515
      goto no_room;
7516
    }
7517
    p = wpa_auth_write_assoc_resp_eppke(sta->wpa_sm, p, left,
7518
                ap_sta_is_mld(hapd, sta));
7519
7520
    if (hapd->conf->security_profiles)
7521
      p = hostapd_eid_security_profile(hapd, p);
7522
  no_room:
7523
  }
7524
#endif /* CONFIG_ENC_ASSOC */
7525
7526
#ifdef CONFIG_DPP2
7527
  if (DPP_VERSION > 1 && (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
7528
      sta && sta->dpp_pfs && status_code == WLAN_STATUS_SUCCESS &&
7529
      wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP) {
7530
    os_memcpy(p, wpabuf_head(sta->dpp_pfs->ie),
7531
        wpabuf_len(sta->dpp_pfs->ie));
7532
    p += wpabuf_len(sta->dpp_pfs->ie);
7533
  }
7534
#endif /* CONFIG_DPP2 */
7535
7536
0
#ifdef CONFIG_IEEE80211AC
7537
0
  if (sta && hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
7538
0
    p = hostapd_eid_vendor_vht(hapd, p);
7539
0
#endif /* CONFIG_IEEE80211AC */
7540
7541
0
  if (sta && (sta->flags & WLAN_STA_WMM))
7542
0
    p = hostapd_eid_wmm(hapd, p);
7543
7544
0
#ifdef CONFIG_WPS
7545
0
  if (sta &&
7546
0
      ((sta->flags & WLAN_STA_WPS) ||
7547
0
       ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa))) {
7548
0
    struct wpabuf *wps = wps_build_assoc_resp_ie();
7549
0
    if (wps) {
7550
0
      os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
7551
0
      p += wpabuf_len(wps);
7552
0
      wpabuf_free(wps);
7553
0
    }
7554
0
  }
7555
0
#endif /* CONFIG_WPS */
7556
7557
0
  if (sta && (sta->flags & WLAN_STA_MULTI_AP))
7558
0
    p = hostapd_eid_multi_ap(hapd, p, buf + buflen - p);
7559
7560
#ifdef CONFIG_P2P
7561
  if (sta && sta->p2p_ie && hapd->p2p_group) {
7562
    struct wpabuf *p2p_resp_ie;
7563
    enum p2p_status_code status;
7564
    switch (status_code) {
7565
    case WLAN_STATUS_SUCCESS:
7566
      status = P2P_SC_SUCCESS;
7567
      break;
7568
    case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
7569
      status = P2P_SC_FAIL_LIMIT_REACHED;
7570
      break;
7571
    default:
7572
      status = P2P_SC_FAIL_INVALID_PARAMS;
7573
      break;
7574
    }
7575
    p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
7576
    if (p2p_resp_ie) {
7577
      os_memcpy(p, wpabuf_head(p2p_resp_ie),
7578
          wpabuf_len(p2p_resp_ie));
7579
      p += wpabuf_len(p2p_resp_ie);
7580
      wpabuf_free(p2p_resp_ie);
7581
    }
7582
  }
7583
#endif /* CONFIG_P2P */
7584
7585
#ifdef CONFIG_P2P_MANAGER
7586
  if (hapd->conf->p2p & P2P_MANAGE)
7587
    p = hostapd_eid_p2p_manage(hapd, p);
7588
#endif /* CONFIG_P2P_MANAGER */
7589
7590
0
  p = hostapd_eid_mbo(hapd, p, buf + buflen - p);
7591
0
  p = hostapd_eid_wfa_capab(hapd, sta, p);
7592
7593
0
  if (hapd->conf->assocresp_elements &&
7594
0
      (size_t) (buf + buflen - p) >=
7595
0
      wpabuf_len(hapd->conf->assocresp_elements)) {
7596
0
    os_memcpy(p, wpabuf_head(hapd->conf->assocresp_elements),
7597
0
        wpabuf_len(hapd->conf->assocresp_elements));
7598
0
    p += wpabuf_len(hapd->conf->assocresp_elements);
7599
0
  }
7600
7601
0
  send_len += p - reply->u.assoc_resp.variable;
7602
7603
#ifdef CONFIG_FILS
7604
  if (sta &&
7605
      (sta->auth_alg == WLAN_AUTH_FILS_SK ||
7606
       sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
7607
       sta->auth_alg == WLAN_AUTH_FILS_PK) &&
7608
      status_code == WLAN_STATUS_SUCCESS) {
7609
    struct ieee802_11_elems elems;
7610
7611
    if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) ==
7612
        ParseFailed || !elems.fils_session) {
7613
      res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7614
      goto done;
7615
    }
7616
7617
    /* FILS Session */
7618
    *p++ = WLAN_EID_EXTENSION; /* Element ID */
7619
    *p++ = 1 + FILS_SESSION_LEN; /* Length */
7620
    *p++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
7621
    os_memcpy(p, elems.fils_session, FILS_SESSION_LEN);
7622
    send_len += 2 + 1 + FILS_SESSION_LEN;
7623
7624
    send_len = fils_encrypt_assoc(sta->wpa_sm, buf, send_len,
7625
                buflen, sta->fils_hlp_resp);
7626
    if (send_len < 0) {
7627
      res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7628
      goto done;
7629
    }
7630
  }
7631
#endif /* CONFIG_FILS */
7632
7633
0
  if (hostapd_drv_send_mlme(hapd, reply, send_len, 0, NULL, 0, 0) < 0) {
7634
0
    wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
7635
0
         strerror(errno));
7636
0
    res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7637
0
  }
7638
7639
0
done:
7640
0
  os_free(buf);
7641
0
  return res;
7642
0
}
7643
7644
7645
#ifdef CONFIG_OWE
7646
u8 * owe_assoc_req_process(struct hostapd_data *hapd, struct sta_info *sta,
7647
         const u8 *owe_dh, u8 owe_dh_len,
7648
         u8 *owe_buf, size_t owe_buf_len, u16 *status)
7649
{
7650
#ifdef CONFIG_TESTING_OPTIONS
7651
  if (hapd->conf->own_ie_override) {
7652
    wpa_printf(MSG_DEBUG, "OWE: Using IE override");
7653
    *status = WLAN_STATUS_SUCCESS;
7654
    return wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
7655
                 owe_buf_len);
7656
  }
7657
#endif /* CONFIG_TESTING_OPTIONS */
7658
7659
  if (wpa_auth_sta_get_pmksa(sta->wpa_sm)) {
7660
    wpa_printf(MSG_DEBUG, "OWE: Using PMKSA caching");
7661
    owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
7662
              owe_buf_len);
7663
    *status = WLAN_STATUS_SUCCESS;
7664
    return owe_buf;
7665
  }
7666
7667
  if (sta->owe_pmk && sta->external_dh_updated) {
7668
    wpa_printf(MSG_DEBUG, "OWE: Using previously derived PMK");
7669
    *status = WLAN_STATUS_SUCCESS;
7670
    return owe_buf;
7671
  }
7672
7673
  *status = owe_process_assoc_req(hapd, sta, owe_dh, owe_dh_len);
7674
  if (*status != WLAN_STATUS_SUCCESS)
7675
    return NULL;
7676
7677
  owe_buf = wpa_auth_write_assoc_resp_owe(sta->wpa_sm, owe_buf,
7678
            owe_buf_len);
7679
7680
  if (sta->owe_ecdh && owe_buf) {
7681
    struct wpabuf *pub;
7682
7683
    pub = crypto_ecdh_get_pubkey(sta->owe_ecdh, 0);
7684
    if (!pub) {
7685
      *status = WLAN_STATUS_UNSPECIFIED_FAILURE;
7686
      return owe_buf;
7687
    }
7688
7689
    /* OWE Diffie-Hellman Parameter element */
7690
    *owe_buf++ = WLAN_EID_EXTENSION; /* Element ID */
7691
    *owe_buf++ = 1 + 2 + wpabuf_len(pub); /* Length */
7692
    *owe_buf++ = WLAN_EID_EXT_OWE_DH_PARAM; /* Element ID Extension
7693
               */
7694
    WPA_PUT_LE16(owe_buf, sta->owe_group);
7695
    owe_buf += 2;
7696
    os_memcpy(owe_buf, wpabuf_head(pub), wpabuf_len(pub));
7697
    owe_buf += wpabuf_len(pub);
7698
    wpabuf_free(pub);
7699
  }
7700
7701
  return owe_buf;
7702
}
7703
#endif /* CONFIG_OWE */
7704
7705
7706
#ifdef CONFIG_FILS
7707
7708
void fils_hlp_finish_assoc(struct hostapd_data *hapd, struct sta_info *sta)
7709
{
7710
  u16 reply_res;
7711
7712
  wpa_printf(MSG_DEBUG, "FILS: Finish association with " MACSTR,
7713
       MAC2STR(sta->addr));
7714
  eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
7715
  if (!sta->fils_pending_assoc_req)
7716
    return;
7717
  reply_res = send_assoc_resp(hapd, sta, sta->addr, WLAN_STATUS_SUCCESS,
7718
            sta->fils_pending_assoc_is_reassoc,
7719
            sta->fils_pending_assoc_req,
7720
            sta->fils_pending_assoc_req_len, 0, 0);
7721
  os_free(sta->fils_pending_assoc_req);
7722
  sta->fils_pending_assoc_req = NULL;
7723
  sta->fils_pending_assoc_req_len = 0;
7724
  wpabuf_free(sta->fils_hlp_resp);
7725
  sta->fils_hlp_resp = NULL;
7726
  wpabuf_free(sta->hlp_dhcp_discover);
7727
  sta->hlp_dhcp_discover = NULL;
7728
7729
  /*
7730
   * Remove the station in case transmission of a success response fails.
7731
   * At this point the station was already added associated to the driver.
7732
   */
7733
  if (reply_res != WLAN_STATUS_SUCCESS)
7734
    hostapd_drv_sta_remove(hapd, sta->addr);
7735
}
7736
7737
7738
void fils_hlp_timeout(void *eloop_ctx, void *eloop_data)
7739
{
7740
  struct hostapd_data *hapd = eloop_ctx;
7741
  struct sta_info *sta = eloop_data;
7742
7743
  wpa_printf(MSG_DEBUG,
7744
       "FILS: HLP response timeout - continue with association response for "
7745
       MACSTR, MAC2STR(sta->addr));
7746
  if (sta->fils_drv_assoc_finish)
7747
    hostapd_notify_assoc_fils_finish(hapd, sta);
7748
  else
7749
    fils_hlp_finish_assoc(hapd, sta);
7750
}
7751
7752
#endif /* CONFIG_FILS */
7753
7754
7755
#ifdef CONFIG_IEEE80211BE
7756
static struct sta_info * handle_mlo_translate(struct hostapd_data *hapd,
7757
                const struct ieee80211_mgmt *mgmt,
7758
                size_t len, bool reassoc,
7759
                struct hostapd_data **assoc_hapd)
7760
0
{
7761
0
  struct sta_info *sta;
7762
0
  struct ieee802_11_elems elems;
7763
0
  u8 mld_addr[ETH_ALEN];
7764
0
  const u8 *pos;
7765
7766
0
  if (!hostapd_is_eht_enabled(hapd))
7767
0
    return NULL;
7768
7769
0
  if (reassoc) {
7770
0
    len -= IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req);
7771
0
    pos = mgmt->u.reassoc_req.variable;
7772
0
  } else {
7773
0
    len -= IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req);
7774
0
    pos = mgmt->u.assoc_req.variable;
7775
0
  }
7776
7777
0
  if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
7778
0
    return NULL;
7779
7780
0
  if (hostapd_process_ml_assoc_req_addr(hapd, elems.basic_mle,
7781
0
                elems.basic_mle_len,
7782
0
                mld_addr))
7783
0
    return NULL;
7784
7785
0
  sta = ap_get_sta(hapd, mld_addr);
7786
0
  if (!sta)
7787
0
    return NULL;
7788
7789
0
  wpa_printf(MSG_DEBUG, "MLD: assoc: mld=" MACSTR ", link=" MACSTR,
7790
0
       MAC2STR(mld_addr), MAC2STR(mgmt->sa));
7791
7792
0
  return hostapd_ml_get_assoc_sta(hapd, sta, assoc_hapd);
7793
0
}
7794
#endif /* CONFIG_IEEE80211BE */
7795
7796
7797
static void handle_assoc(struct hostapd_data *hapd,
7798
       const struct ieee80211_mgmt *mgmt, size_t len,
7799
       int reassoc, int rssi)
7800
0
{
7801
0
  u16 capab_info, listen_interval, seq_ctrl, fc;
7802
0
  int resp = WLAN_STATUS_SUCCESS;
7803
0
  u16 reply_res = WLAN_STATUS_UNSPECIFIED_FAILURE;
7804
0
  const u8 *pos;
7805
0
  int left, i;
7806
0
  struct sta_info *sta;
7807
0
  u8 *tmp = NULL;
7808
#ifdef CONFIG_FILS
7809
  int delay_assoc = 0;
7810
#endif /* CONFIG_FILS */
7811
0
  int omit_rsnxe = 0;
7812
0
  bool set_beacon = false;
7813
0
  bool mld_addrs_not_translated = false;
7814
7815
0
  if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
7816
0
              sizeof(mgmt->u.assoc_req))) {
7817
0
    wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
7818
0
         reassoc, (unsigned long) len);
7819
0
    return;
7820
0
  }
7821
7822
#ifdef CONFIG_TESTING_OPTIONS
7823
  if (reassoc) {
7824
    if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
7825
        drand48() < hapd->iconf->ignore_reassoc_probability) {
7826
      wpa_printf(MSG_INFO,
7827
           "TESTING: ignoring reassoc request from "
7828
           MACSTR, MAC2STR(mgmt->sa));
7829
      return;
7830
    }
7831
  } else {
7832
    if (hapd->iconf->ignore_assoc_probability > 0.0 &&
7833
        drand48() < hapd->iconf->ignore_assoc_probability) {
7834
      wpa_printf(MSG_INFO,
7835
           "TESTING: ignoring assoc request from "
7836
           MACSTR, MAC2STR(mgmt->sa));
7837
      return;
7838
    }
7839
  }
7840
#endif /* CONFIG_TESTING_OPTIONS */
7841
7842
0
  fc = le_to_host16(mgmt->frame_control);
7843
0
  seq_ctrl = le_to_host16(mgmt->seq_ctrl);
7844
7845
0
  if (reassoc) {
7846
0
    capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
7847
0
    listen_interval = le_to_host16(
7848
0
      mgmt->u.reassoc_req.listen_interval);
7849
0
    wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
7850
0
         " capab_info=0x%02x listen_interval=%d current_ap="
7851
0
         MACSTR " seq_ctrl=0x%x%s",
7852
0
         MAC2STR(mgmt->sa), capab_info, listen_interval,
7853
0
         MAC2STR(mgmt->u.reassoc_req.current_ap),
7854
0
         seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
7855
0
    left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
7856
0
    pos = mgmt->u.reassoc_req.variable;
7857
0
  } else {
7858
0
    capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
7859
0
    listen_interval = le_to_host16(
7860
0
      mgmt->u.assoc_req.listen_interval);
7861
0
    wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
7862
0
         " capab_info=0x%02x listen_interval=%d "
7863
0
         "seq_ctrl=0x%x%s",
7864
0
         MAC2STR(mgmt->sa), capab_info, listen_interval,
7865
0
         seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
7866
0
    left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
7867
0
    pos = mgmt->u.assoc_req.variable;
7868
0
  }
7869
7870
0
  sta = ap_get_sta(hapd, mgmt->sa);
7871
7872
0
#ifdef CONFIG_IEEE80211BE
7873
  /*
7874
   * It is possible that the association frame is from an associated
7875
   * non-AP MLD station, that tries to re-associate using different link
7876
   * addresses. In such a case, try to find the station based on the AP
7877
   * MLD MAC address.
7878
   */
7879
0
  if (!sta) {
7880
0
    struct hostapd_data *assoc_hapd;
7881
7882
0
    sta = handle_mlo_translate(hapd, mgmt, len, reassoc,
7883
0
             &assoc_hapd);
7884
0
    if (sta) {
7885
0
      if (hapd != assoc_hapd) {
7886
0
        wpa_printf(MSG_DEBUG,
7887
0
             "MLD: Switching to assoc hapd/station");
7888
0
        hapd = assoc_hapd;
7889
0
        mld_addrs_not_translated = true;
7890
0
      }
7891
7892
      /* Allow link address to be changed if an SA query
7893
       * procedure has expired. */
7894
0
      if (sta->sa_query_timed_out) {
7895
0
        u8 _link = hapd->mld_link_id;
7896
7897
0
        os_memcpy(sta->mld_info.links[_link].peer_addr,
7898
0
            mgmt->sa, ETH_ALEN);
7899
0
      }
7900
7901
0
    }
7902
0
  }
7903
0
#endif /* CONFIG_IEEE80211BE */
7904
7905
0
#ifdef CONFIG_IEEE80211R_AP
7906
0
  if (sta && sta->auth_alg == WLAN_AUTH_FT &&
7907
0
      (sta->flags & WLAN_STA_AUTH) == 0) {
7908
0
    wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
7909
0
         "prior to authentication since it is using "
7910
0
         "over-the-DS FT", MAC2STR(mgmt->sa));
7911
7912
    /*
7913
     * Mark station as authenticated, to avoid adding station
7914
     * entry in the driver as associated and not authenticated
7915
     */
7916
0
    sta->flags |= WLAN_STA_AUTH;
7917
0
  } else
7918
0
#endif /* CONFIG_IEEE80211R_AP */
7919
0
  if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
7920
0
    if (hapd->iface->current_mode &&
7921
0
        hapd->iface->current_mode->mode ==
7922
0
      HOSTAPD_MODE_IEEE80211AD) {
7923
0
      int acl_res;
7924
0
      struct radius_sta info;
7925
7926
0
      acl_res = ieee802_11_allowed_address(hapd, mgmt->sa,
7927
0
                   (const u8 *) mgmt,
7928
0
                   len, &info);
7929
0
      if (acl_res == HOSTAPD_ACL_REJECT) {
7930
0
        wpa_msg(hapd->msg_ctx, MSG_DEBUG,
7931
0
          "Ignore Association Request frame from "
7932
0
          MACSTR " due to ACL reject",
7933
0
          MAC2STR(mgmt->sa));
7934
0
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7935
0
        goto fail;
7936
0
      }
7937
0
      if (acl_res == HOSTAPD_ACL_PENDING)
7938
0
        return;
7939
7940
      /* DMG/IEEE 802.11ad does not use authentication.
7941
       * Allocate sta entry upon association. */
7942
0
      sta = ap_sta_add(hapd, mgmt->sa);
7943
0
      if (!sta) {
7944
0
        hostapd_logger(hapd, mgmt->sa,
7945
0
                 HOSTAPD_MODULE_IEEE80211,
7946
0
                 HOSTAPD_LEVEL_INFO,
7947
0
                 "Failed to add STA");
7948
0
        resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
7949
0
        goto fail;
7950
0
      }
7951
7952
0
      acl_res = ieee802_11_set_radius_info(
7953
0
        hapd, sta, acl_res, &info);
7954
0
      if (acl_res) {
7955
0
        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7956
0
        goto fail;
7957
0
      }
7958
7959
0
      hostapd_logger(hapd, sta->addr,
7960
0
               HOSTAPD_MODULE_IEEE80211,
7961
0
               HOSTAPD_LEVEL_DEBUG,
7962
0
               "Skip authentication for DMG/IEEE 802.11ad");
7963
0
      sta->flags |= WLAN_STA_AUTH;
7964
0
      wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
7965
0
      sta->auth_alg = WLAN_AUTH_OPEN;
7966
0
    } else {
7967
0
      hostapd_logger(hapd, mgmt->sa,
7968
0
               HOSTAPD_MODULE_IEEE80211,
7969
0
               HOSTAPD_LEVEL_INFO,
7970
0
               "Station tried to associate before authentication (aid=%d flags=0x%llx)",
7971
0
               sta ? sta->aid : -1,
7972
0
               sta ? sta->flags : 0);
7973
0
      send_deauth(hapd, mgmt->sa,
7974
0
            WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
7975
0
      return;
7976
0
    }
7977
0
  }
7978
7979
0
  if ((fc & WLAN_FC_RETRY) &&
7980
0
      sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
7981
0
      sta->last_seq_ctrl == seq_ctrl &&
7982
0
      sta->last_subtype == (reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
7983
0
          WLAN_FC_STYPE_ASSOC_REQ)) {
7984
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
7985
0
             HOSTAPD_LEVEL_DEBUG,
7986
0
             "Drop repeated association frame seq_ctrl=0x%x",
7987
0
             seq_ctrl);
7988
0
    return;
7989
0
  }
7990
0
  sta->last_seq_ctrl = seq_ctrl;
7991
0
  sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
7992
0
    WLAN_FC_STYPE_ASSOC_REQ;
7993
7994
0
  if (hapd->tkip_countermeasures) {
7995
0
    resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
7996
0
    goto fail;
7997
0
  }
7998
7999
0
  if (listen_interval > hapd->conf->max_listen_interval) {
8000
0
    hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8001
0
             HOSTAPD_LEVEL_DEBUG,
8002
0
             "Too large Listen Interval (%d)",
8003
0
             listen_interval);
8004
0
    resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
8005
0
    goto fail;
8006
0
  }
8007
8008
#ifdef CONFIG_MBO
8009
  if (hapd->conf->mbo_enabled && hapd->mbo_assoc_disallow) {
8010
    resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
8011
    goto fail;
8012
  }
8013
8014
  if (hapd->iconf->rssi_reject_assoc_rssi && rssi &&
8015
      rssi < hapd->iconf->rssi_reject_assoc_rssi &&
8016
      (sta->auth_rssi == 0 ||
8017
       sta->auth_rssi < hapd->iconf->rssi_reject_assoc_rssi)) {
8018
    resp = WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS;
8019
    goto fail;
8020
  }
8021
#endif /* CONFIG_MBO */
8022
8023
0
  if (hapd->conf->wpa &&
8024
0
      check_sa_query(hapd, sta, reassoc, pos, left,
8025
0
         fc & WLAN_FC_PROTECTED)) {
8026
0
    resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
8027
0
    goto fail;
8028
0
  }
8029
8030
  /*
8031
   * sta->capability is used in check_assoc_ies() for RRM enabled
8032
   * capability element.
8033
   */
8034
0
  sta->capability = capab_info;
8035
8036
#ifdef CONFIG_FILS
8037
  if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
8038
      sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
8039
      sta->auth_alg == WLAN_AUTH_FILS_PK) {
8040
    int res;
8041
8042
    /* The end of the payload is encrypted. Need to decrypt it
8043
     * before parsing. */
8044
8045
    tmp = os_memdup(pos, left);
8046
    if (!tmp) {
8047
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
8048
      goto fail;
8049
    }
8050
8051
    res = fils_decrypt_assoc(sta->wpa_sm, sta->fils_session, mgmt,
8052
           len, tmp, left);
8053
    if (res < 0) {
8054
      resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
8055
      goto fail;
8056
    }
8057
    pos = tmp;
8058
    left = res;
8059
  }
8060
#endif /* CONFIG_FILS */
8061
8062
  /* followed by SSID and Supported rates; and HT capabilities if 802.11n
8063
   * is used */
8064
0
  resp = check_assoc_ies(hapd, sta, pos, left,
8065
0
             reassoc ? LINK_PARSE_REASSOC : LINK_PARSE_ASSOC);
8066
0
  if (resp != WLAN_STATUS_SUCCESS)
8067
0
    goto fail;
8068
0
#ifdef CONFIG_IEEE80211R_AP
8069
0
  if (reassoc && sta->auth_alg == WLAN_AUTH_FT)
8070
0
    omit_rsnxe = !get_ie(pos, left, WLAN_EID_RSNX);
8071
0
#endif /* CONFIG_IEEE80211R_AP */
8072
0
  if (hapd->conf->rsn_override_omit_rsnxe)
8073
0
    omit_rsnxe = 1;
8074
8075
0
  if (hostapd_get_aid(hapd, sta) < 0) {
8076
0
    hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8077
0
             HOSTAPD_LEVEL_INFO, "No room for more AIDs");
8078
0
    resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
8079
0
    goto fail;
8080
0
  }
8081
8082
0
  sta->listen_interval = listen_interval;
8083
8084
0
  if (hapd->iface->current_mode &&
8085
0
      hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
8086
0
    sta->flags |= WLAN_STA_NONERP;
8087
0
  for (i = 0; i < sta->supported_rates_len; i++) {
8088
0
    if ((sta->supported_rates[i] & 0x7f) > 22) {
8089
0
      sta->flags &= ~WLAN_STA_NONERP;
8090
0
      break;
8091
0
    }
8092
0
  }
8093
0
  if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
8094
0
    sta->nonerp_set = 1;
8095
0
    hapd->iface->num_sta_non_erp++;
8096
0
    if (hapd->iface->num_sta_non_erp == 1)
8097
0
      set_beacon = true;
8098
0
  }
8099
8100
0
  if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
8101
0
      !sta->no_short_slot_time_set) {
8102
0
    sta->no_short_slot_time_set = 1;
8103
0
    hapd->iface->num_sta_no_short_slot_time++;
8104
0
    if (hapd->iface->current_mode &&
8105
0
        hapd->iface->current_mode->mode ==
8106
0
        HOSTAPD_MODE_IEEE80211G &&
8107
0
        hapd->iface->num_sta_no_short_slot_time == 1)
8108
0
      set_beacon = true;
8109
0
  }
8110
8111
0
  if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
8112
0
    sta->flags |= WLAN_STA_SHORT_PREAMBLE;
8113
0
  else
8114
0
    sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
8115
8116
0
  if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
8117
0
      !sta->no_short_preamble_set) {
8118
0
    sta->no_short_preamble_set = 1;
8119
0
    hapd->iface->num_sta_no_short_preamble++;
8120
0
    if (hapd->iface->current_mode &&
8121
0
        hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
8122
0
        && hapd->iface->num_sta_no_short_preamble == 1)
8123
0
      set_beacon = true;
8124
0
  }
8125
8126
0
  if (update_ht_state(hapd, sta) > 0)
8127
0
    set_beacon = true;
8128
8129
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8130
0
           HOSTAPD_LEVEL_DEBUG,
8131
0
           "association OK (aid %d)", sta->aid);
8132
  /* Station will be marked associated, after it acknowledges AssocResp
8133
   */
8134
0
  sta->flags |= WLAN_STA_ASSOC_REQ_OK;
8135
8136
0
  if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
8137
0
    wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
8138
0
         "SA Query procedure", reassoc ? "re" : "");
8139
    /* TODO: Send a protected Disassociate frame to the STA using
8140
     * the old key and Reason Code "Previous Authentication no
8141
     * longer valid". Make sure this is only sent protected since
8142
     * unprotected frame would be received by the STA that is now
8143
     * trying to associate.
8144
     */
8145
0
  }
8146
8147
  /* Make sure that the previously registered inactivity timer will not
8148
   * remove the STA immediately. */
8149
0
  sta->timeout_next = STA_NULLFUNC;
8150
8151
#ifdef CONFIG_TAXONOMY
8152
  taxonomy_sta_info_assoc_req(hapd, sta, pos, left);
8153
#endif /* CONFIG_TAXONOMY */
8154
8155
0
  sta->pending_wds_enable = 0;
8156
8157
#ifdef CONFIG_FILS
8158
  if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
8159
      sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
8160
      sta->auth_alg == WLAN_AUTH_FILS_PK) {
8161
    if (fils_process_hlp(hapd, sta, pos, left) > 0)
8162
      delay_assoc = 1;
8163
  }
8164
#endif /* CONFIG_FILS */
8165
8166
0
 fail:
8167
8168
  /*
8169
   * In case of a successful response, add the station to the driver.
8170
   * Otherwise, the kernel may ignore Data frames before we process the
8171
   * ACK frame (TX status). In case of a failure, this station will be
8172
   * removed.
8173
   *
8174
   * Note that this is not compliant with the IEEE 802.11 standard that
8175
   * states that a non-AP station should transition into the
8176
   * authenticated/associated state only after the station acknowledges
8177
   * the (Re)Association Response frame. However, still do this as:
8178
   *
8179
   * 1. In case the station does not acknowledge the (Re)Association
8180
   *    Response frame, it will be removed.
8181
   * 2. Data frames will be dropped in the kernel until the station is
8182
   *    set into authorized state, and there are no significant known
8183
   *    issues with processing other non-Data Class 3 frames during this
8184
   *    window.
8185
   */
8186
0
  if (sta)
8187
0
    hostapd_process_assoc_ml_info(hapd, sta, pos, left, reassoc,
8188
0
                resp, false, &set_beacon);
8189
8190
0
  if (resp == WLAN_STATUS_SUCCESS && sta &&
8191
0
      add_associated_sta(hapd, sta, reassoc))
8192
0
    resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
8193
8194
#ifdef CONFIG_FILS
8195
  if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS &&
8196
      eloop_is_timeout_registered(fils_hlp_timeout, hapd, sta) &&
8197
      sta->fils_pending_assoc_req) {
8198
    if (set_beacon)
8199
      ieee802_11_update_beacons(hapd->iface);
8200
8201
    /* Do not reschedule fils_hlp_timeout in case the station
8202
     * retransmits (Re)Association Request frame while waiting for
8203
     * the previously started FILS HLP wait, so that the timeout can
8204
     * be determined from the first pending attempt. */
8205
    wpa_printf(MSG_DEBUG,
8206
         "FILS: Continue waiting for HLP processing before sending (Re)Association Response frame to "
8207
         MACSTR, MAC2STR(sta->addr));
8208
    os_free(tmp);
8209
    return;
8210
  }
8211
  if (sta) {
8212
    eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
8213
    os_free(sta->fils_pending_assoc_req);
8214
    sta->fils_pending_assoc_req = NULL;
8215
    sta->fils_pending_assoc_req_len = 0;
8216
    wpabuf_free(sta->fils_hlp_resp);
8217
    sta->fils_hlp_resp = NULL;
8218
  }
8219
  if (sta && delay_assoc && resp == WLAN_STATUS_SUCCESS) {
8220
    if (set_beacon)
8221
      ieee802_11_update_beacons(hapd->iface);
8222
8223
    sta->fils_pending_assoc_req = tmp;
8224
    sta->fils_pending_assoc_req_len = left;
8225
    sta->fils_pending_assoc_is_reassoc = reassoc;
8226
    sta->fils_drv_assoc_finish = 0;
8227
    wpa_printf(MSG_DEBUG,
8228
         "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
8229
         MACSTR, MAC2STR(sta->addr));
8230
    eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
8231
    eloop_register_timeout(0, hapd->conf->fils_hlp_wait_time * 1024,
8232
               fils_hlp_timeout, hapd, sta);
8233
    return;
8234
  }
8235
#endif /* CONFIG_FILS */
8236
8237
#ifdef CONFIG_TESTING_OPTIONS
8238
  if (hapd->conf->association_response_status_code >= 0) {
8239
    wpa_printf(MSG_DEBUG,
8240
         "TESTING: Forcing association response status code to %d",
8241
         hapd->conf->association_response_status_code);
8242
    resp = hapd->conf->association_response_status_code;
8243
  }
8244
#endif /* CONFIG_TESTING_OPTIONS */
8245
8246
0
  if (resp >= 0)
8247
0
    reply_res = send_assoc_resp(hapd,
8248
0
              mld_addrs_not_translated ?
8249
0
              NULL : sta,
8250
0
              mgmt->sa, resp, reassoc,
8251
0
              pos, left, rssi, omit_rsnxe);
8252
8253
0
  if (set_beacon)
8254
0
    ieee802_11_update_beacons(hapd->iface);
8255
8256
0
  os_free(tmp);
8257
8258
  /*
8259
   * Remove the station in case transmission of a success response fails
8260
   * (the STA was added associated to the driver) or if the station was
8261
   * previously added unassociated.
8262
   */
8263
0
  if (sta && ((reply_res != WLAN_STATUS_SUCCESS &&
8264
0
         resp == WLAN_STATUS_SUCCESS) || sta->added_unassoc)) {
8265
0
    hostapd_drv_sta_remove(hapd, sta->addr);
8266
0
    sta->added_unassoc = 0;
8267
0
  }
8268
0
}
8269
8270
8271
static void hostapd_deauth_sta(struct hostapd_data *hapd,
8272
             struct sta_info *sta,
8273
             const struct ieee80211_mgmt *mgmt)
8274
0
{
8275
0
  wpa_msg(hapd->msg_ctx, MSG_DEBUG,
8276
0
    "deauthentication: STA=" MACSTR " reason_code=%d",
8277
0
    MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
8278
8279
0
  ap_sta_set_authorized(hapd, sta, 0);
8280
0
  sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
8281
0
  sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
8282
0
      WLAN_STA_ASSOC_REQ_OK);
8283
0
  hostapd_set_sta_flags(hapd, sta);
8284
0
  wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
8285
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8286
0
           HOSTAPD_LEVEL_DEBUG, "deauthenticated");
8287
0
  mlme_deauthenticate_indication(
8288
0
    hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
8289
0
  sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
8290
0
  ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
8291
0
  ap_free_sta(hapd, sta);
8292
0
}
8293
8294
8295
static void hostapd_disassoc_sta(struct hostapd_data *hapd,
8296
         struct sta_info *sta,
8297
         const struct ieee80211_mgmt *mgmt)
8298
0
{
8299
0
  wpa_msg(hapd->msg_ctx, MSG_DEBUG,
8300
0
    "disassocation: STA=" MACSTR " reason_code=%d",
8301
0
    MAC2STR(mgmt->sa), le_to_host16(mgmt->u.disassoc.reason_code));
8302
8303
0
  ap_sta_set_authorized(hapd, sta, 0);
8304
0
  sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
8305
0
  sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
8306
0
  hostapd_set_sta_flags(hapd, sta);
8307
0
  wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
8308
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8309
0
           HOSTAPD_LEVEL_INFO, "disassociated");
8310
0
  sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
8311
0
  ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
8312
  /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
8313
   * authenticated. */
8314
0
  accounting_sta_stop(hapd, sta);
8315
0
  ieee802_1x_free_station(hapd, sta);
8316
0
  if (sta->ipaddr)
8317
0
    hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
8318
0
  ap_sta_ip6addr_del(hapd, sta);
8319
0
  hostapd_drv_sta_remove(hapd, sta->addr);
8320
0
  sta->added_unassoc = 0;
8321
8322
0
  if (sta->timeout_next == STA_NULLFUNC ||
8323
0
      sta->timeout_next == STA_DISASSOC) {
8324
0
    sta->timeout_next = STA_DEAUTH;
8325
0
    eloop_cancel_timeout(ap_handle_timer, hapd, sta);
8326
0
    eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
8327
0
               hapd, sta);
8328
0
  }
8329
8330
0
  mlme_disassociate_indication(
8331
0
    hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
8332
8333
  /* DMG/IEEE 802.11ad does not use deauthication. Deallocate sta upon
8334
   * disassociation. */
8335
0
  if (hapd->iface->current_mode &&
8336
0
      hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
8337
0
    sta->flags &= ~WLAN_STA_AUTH;
8338
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
8339
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8340
0
             HOSTAPD_LEVEL_DEBUG, "deauthenticated");
8341
0
    ap_free_sta(hapd, sta);
8342
0
  }
8343
0
}
8344
8345
8346
static bool hostapd_ml_handle_disconnect(struct hostapd_data *hapd,
8347
           struct sta_info *sta,
8348
           const struct ieee80211_mgmt *mgmt,
8349
           bool disassoc)
8350
0
{
8351
0
#ifdef CONFIG_IEEE80211BE
8352
0
  struct hostapd_data *assoc_hapd, *tmp_hapd;
8353
0
  struct sta_info *assoc_sta;
8354
0
  struct sta_info *tmp_sta;
8355
8356
0
  if (!hostapd_is_multiple_link_mld(hapd))
8357
0
    return false;
8358
8359
  /*
8360
   * Get the station on which the association was performed, as it holds
8361
   * the information about all the other links.
8362
   */
8363
0
  assoc_sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
8364
0
  if (!assoc_sta)
8365
0
    return false;
8366
8367
0
  for_each_mld_link(tmp_hapd, assoc_hapd) {
8368
0
    if (tmp_hapd == assoc_hapd)
8369
0
      continue;
8370
8371
0
    if (!assoc_sta->mld_info.links[tmp_hapd->mld_link_id].valid)
8372
0
      continue;
8373
8374
0
    for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
8375
0
         tmp_sta = tmp_sta->next) {
8376
0
      if (tmp_sta->mld_assoc_link_id !=
8377
0
          assoc_sta->mld_assoc_link_id ||
8378
0
          tmp_sta->aid != assoc_sta->aid)
8379
0
        continue;
8380
8381
0
      if (!disassoc)
8382
0
        hostapd_deauth_sta(tmp_hapd, tmp_sta, mgmt);
8383
0
      else
8384
0
        hostapd_disassoc_sta(tmp_hapd, tmp_sta, mgmt);
8385
0
      break;
8386
0
    }
8387
0
  }
8388
8389
  /* Remove the station on which the association was performed. */
8390
0
  if (!disassoc)
8391
0
    hostapd_deauth_sta(assoc_hapd, assoc_sta, mgmt);
8392
0
  else
8393
0
    hostapd_disassoc_sta(assoc_hapd, assoc_sta, mgmt);
8394
8395
0
  return true;
8396
#else /* CONFIG_IEEE80211BE */
8397
  return false;
8398
#endif /* CONFIG_IEEE80211BE */
8399
0
}
8400
8401
8402
static void handle_disassoc(struct hostapd_data *hapd,
8403
          const struct ieee80211_mgmt *mgmt, size_t len)
8404
0
{
8405
0
  struct sta_info *sta;
8406
8407
0
  if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
8408
0
    wpa_msg(hapd->msg_ctx, MSG_DEBUG,
8409
0
         "handle_disassoc - too short payload (len=%lu)",
8410
0
         (unsigned long) len);
8411
0
    return;
8412
0
  }
8413
8414
0
  sta = ap_get_sta(hapd, mgmt->sa);
8415
0
  if (!sta) {
8416
0
    wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
8417
0
      " trying to disassociate, but it is not associated",
8418
0
      MAC2STR(mgmt->sa));
8419
0
    return;
8420
0
  }
8421
8422
0
  if (hostapd_ml_handle_disconnect(hapd, sta, mgmt, true))
8423
0
    return;
8424
8425
0
  hostapd_disassoc_sta(hapd, sta, mgmt);
8426
0
}
8427
8428
8429
static void handle_deauth(struct hostapd_data *hapd,
8430
        const struct ieee80211_mgmt *mgmt, size_t len)
8431
0
{
8432
0
  struct sta_info *sta;
8433
8434
0
  if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
8435
0
    wpa_msg(hapd->msg_ctx, MSG_DEBUG,
8436
0
      "handle_deauth - too short payload (len=%lu)",
8437
0
      (unsigned long) len);
8438
0
    return;
8439
0
  }
8440
8441
  /* Clear the PTKSA cache entries for PASN */
8442
0
  ptksa_cache_flush(hapd->ptksa, mgmt->sa, WPA_CIPHER_NONE);
8443
8444
0
  sta = ap_get_sta(hapd, mgmt->sa);
8445
0
  if (!sta) {
8446
0
    wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
8447
0
      " trying to deauthenticate, but it is not authenticated",
8448
0
      MAC2STR(mgmt->sa));
8449
0
    return;
8450
0
  }
8451
8452
0
  if (hostapd_ml_handle_disconnect(hapd, sta, mgmt, false))
8453
0
    return;
8454
8455
0
  hostapd_deauth_sta(hapd, sta, mgmt);
8456
0
}
8457
8458
8459
static void handle_beacon(struct hostapd_data *hapd,
8460
        const struct ieee80211_mgmt *mgmt, size_t len,
8461
        struct hostapd_frame_info *fi)
8462
0
{
8463
0
  struct ieee802_11_elems elems;
8464
8465
0
  if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
8466
0
    wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
8467
0
         (unsigned long) len);
8468
0
    return;
8469
0
  }
8470
8471
0
  (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
8472
0
              len - (IEEE80211_HDRLEN +
8473
0
               sizeof(mgmt->u.beacon)), &elems,
8474
0
              0);
8475
8476
0
  ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
8477
0
}
8478
8479
8480
#ifdef CONFIG_ROBUST_AV
8481
static void hostapd_dscp_action(struct hostapd_data *hapd, struct sta_info *sta,
8482
        const u8 *pos, const u8 *end)
8483
0
{
8484
0
  u8 subtype;
8485
8486
0
  if (!hapd->conf->enable_dscp_policy_capa)
8487
0
    return;
8488
8489
0
  if (end - pos < 1) {
8490
0
    wpa_printf(MSG_DEBUG, "DSCP Action: Frame too short");
8491
0
    return;
8492
0
  }
8493
8494
0
  subtype = *pos++;
8495
0
  switch (subtype) {
8496
0
  case QM_DSCP_POLICY_QUERY:
8497
0
    hostapd_handle_dscp_policy_query(hapd, sta, pos, end - pos);
8498
0
    break;
8499
0
  case QM_DSCP_POLICY_RESP:
8500
0
    hostapd_handle_dscp_policy_response(hapd, sta, pos, end - pos);
8501
0
    break;
8502
0
  default:
8503
0
    wpa_printf(MSG_DEBUG, "QM Action: Unknown subtype %u", subtype);
8504
0
    break;
8505
0
  }
8506
0
}
8507
#endif /* CONFIG_ROBUST_AV */
8508
8509
8510
static int hostapd_action_vs(struct hostapd_data *hapd,
8511
           struct sta_info *sta,
8512
           const struct ieee80211_mgmt *mgmt, size_t len,
8513
           unsigned int freq, bool protected)
8514
0
{
8515
0
  const u8 *pos, *end;
8516
0
  u32 oui_type;
8517
8518
0
  pos = (const u8 *) &mgmt->u.action;
8519
0
  end = ((const u8 *) mgmt) + len;
8520
8521
0
  if (end - pos < 1 + 4)
8522
0
    return -1;
8523
0
  pos++;
8524
8525
0
  oui_type = WPA_GET_BE32(pos);
8526
0
  pos += 4;
8527
8528
0
  switch (oui_type) {
8529
0
  case WFA_CAPAB_VENDOR_TYPE:
8530
0
    hostapd_wfa_capab(hapd, sta, pos, end);
8531
0
    return 0;
8532
0
#ifdef CONFIG_ROBUST_AV
8533
0
  case QM_ACTION_VENDOR_TYPE:
8534
0
    if (!protected) {
8535
0
      wpa_printf(MSG_DEBUG,
8536
0
           "DSCP: Ignoring unprotected frame");
8537
0
      return -1;
8538
0
    }
8539
0
    hostapd_dscp_action(hapd, sta, pos, end);
8540
0
    return 0;
8541
0
#endif /* CONFIG_ROBUST_AV */
8542
0
  default:
8543
0
    wpa_printf(MSG_DEBUG,
8544
0
         "Ignore unknown Vendor Specific Action frame OUI/type %08x%s",
8545
0
         oui_type, protected ? " (protected)" : "");
8546
0
    break;
8547
0
  }
8548
8549
0
  return -1;
8550
0
}
8551
8552
8553
static int robust_action_frame(u8 category)
8554
0
{
8555
0
  return category != WLAN_ACTION_PUBLIC &&
8556
0
    category != WLAN_ACTION_HT &&
8557
0
    category != WLAN_ACTION_UNPROTECTED_WNM &&
8558
0
    category != WLAN_ACTION_SELF_PROTECTED &&
8559
0
    category != WLAN_ACTION_UNPROTECTED_DMG &&
8560
0
    category != WLAN_ACTION_VHT &&
8561
0
    category != WLAN_ACTION_UNPROTECTED_S1G &&
8562
0
    category != WLAN_ACTION_HE &&
8563
0
    category != WLAN_ACTION_EHT &&
8564
0
    category != WLAN_ACTION_VENDOR_SPECIFIC;
8565
0
}
8566
8567
8568
static int handle_action(struct hostapd_data *hapd,
8569
       const struct ieee80211_mgmt *mgmt, size_t len,
8570
       unsigned int freq)
8571
0
{
8572
0
  struct sta_info *sta;
8573
0
  u8 *action __maybe_unused;
8574
8575
0
  if (len < IEEE80211_HDRLEN + 2 + 1) {
8576
0
    hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8577
0
             HOSTAPD_LEVEL_DEBUG,
8578
0
             "handle_action - too short payload (len=%lu)",
8579
0
             (unsigned long) len);
8580
0
    return 0;
8581
0
  }
8582
8583
0
  action = (u8 *) &mgmt->u.action.u;
8584
0
  wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR
8585
0
       " da " MACSTR " len %d freq %u",
8586
0
       mgmt->u.action.category, *action,
8587
0
       MAC2STR(mgmt->sa), MAC2STR(mgmt->da), (int) len, freq);
8588
8589
0
  sta = ap_get_sta(hapd, mgmt->sa);
8590
8591
0
  if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
8592
0
      (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
8593
0
    wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
8594
0
         "frame (category=%u) from unassociated STA " MACSTR,
8595
0
         mgmt->u.action.category, MAC2STR(mgmt->sa));
8596
0
    return 0;
8597
0
  }
8598
8599
0
  if (sta && (sta->flags & WLAN_STA_MFP) &&
8600
0
      !(mgmt->frame_control & host_to_le16(WLAN_FC_PROTECTED)) &&
8601
0
      robust_action_frame(mgmt->u.action.category)) {
8602
0
    hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8603
0
             HOSTAPD_LEVEL_DEBUG,
8604
0
             "Dropped unprotected Robust Action frame from "
8605
0
             "an MFP STA");
8606
0
    return 0;
8607
0
  }
8608
8609
0
  if (sta) {
8610
0
    u16 fc = le_to_host16(mgmt->frame_control);
8611
0
    u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
8612
8613
0
    if ((fc & WLAN_FC_RETRY) &&
8614
0
        sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
8615
0
        sta->last_seq_ctrl == seq_ctrl &&
8616
0
        sta->last_subtype == WLAN_FC_STYPE_ACTION) {
8617
0
      hostapd_logger(hapd, sta->addr,
8618
0
               HOSTAPD_MODULE_IEEE80211,
8619
0
               HOSTAPD_LEVEL_DEBUG,
8620
0
               "Drop repeated action frame seq_ctrl=0x%x",
8621
0
               seq_ctrl);
8622
0
      return 1;
8623
0
    }
8624
8625
0
    sta->last_seq_ctrl = seq_ctrl;
8626
0
    sta->last_subtype = WLAN_FC_STYPE_ACTION;
8627
0
  }
8628
8629
0
  switch (mgmt->u.action.category) {
8630
0
#ifdef CONFIG_IEEE80211R_AP
8631
0
  case WLAN_ACTION_FT:
8632
0
    if (!sta ||
8633
0
        wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
8634
0
             len - IEEE80211_HDRLEN))
8635
0
      break;
8636
0
    return 1;
8637
0
#endif /* CONFIG_IEEE80211R_AP */
8638
0
  case WLAN_ACTION_WMM:
8639
0
    hostapd_wmm_action(hapd, mgmt, len);
8640
0
    return 1;
8641
0
  case WLAN_ACTION_SA_QUERY:
8642
0
    ieee802_11_sa_query_action(hapd, mgmt, len);
8643
0
    return 1;
8644
#ifdef CONFIG_WNM_AP
8645
  case WLAN_ACTION_WNM:
8646
    ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
8647
    return 1;
8648
#endif /* CONFIG_WNM_AP */
8649
#ifdef CONFIG_FST
8650
  case WLAN_ACTION_FST:
8651
    if (hapd->iface->fst)
8652
      fst_rx_action(hapd->iface->fst, mgmt, len);
8653
    else
8654
      wpa_printf(MSG_DEBUG,
8655
           "FST: Ignore FST Action frame - no FST attached");
8656
    return 1;
8657
#endif /* CONFIG_FST */
8658
0
  case WLAN_ACTION_PUBLIC:
8659
0
  case WLAN_ACTION_PROTECTED_DUAL:
8660
0
    if (len >= IEEE80211_HDRLEN + 2 &&
8661
0
        mgmt->u.action.u.public_action.action ==
8662
0
        WLAN_PA_20_40_BSS_COEX) {
8663
0
      hostapd_2040_coex_action(hapd, mgmt, len);
8664
0
      return 1;
8665
0
    }
8666
#ifdef CONFIG_DPP
8667
    if (len >= IEEE80211_HDRLEN + 6 &&
8668
        mgmt->u.action.u.vs_public_action.action ==
8669
        WLAN_PA_VENDOR_SPECIFIC &&
8670
        WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
8671
        OUI_WFA &&
8672
        mgmt->u.action.u.vs_public_action.variable[0] ==
8673
        DPP_OUI_TYPE) {
8674
      const u8 *pos, *end;
8675
8676
      pos = mgmt->u.action.u.vs_public_action.oui;
8677
      end = ((const u8 *) mgmt) + len;
8678
      hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
8679
                freq);
8680
      return 1;
8681
    }
8682
    if (len >= IEEE80211_HDRLEN + 2 &&
8683
        (mgmt->u.action.u.public_action.action ==
8684
         WLAN_PA_GAS_INITIAL_RESP ||
8685
         mgmt->u.action.u.public_action.action ==
8686
         WLAN_PA_GAS_COMEBACK_RESP)) {
8687
      const u8 *pos, *end;
8688
8689
      pos = &mgmt->u.action.u.public_action.action;
8690
      end = ((const u8 *) mgmt) + len;
8691
      if (gas_query_ap_rx(hapd->gas, mgmt->sa,
8692
              mgmt->u.action.category,
8693
              pos, end - pos, freq) == 0)
8694
        return 1;
8695
    }
8696
#endif /* CONFIG_DPP */
8697
#ifdef CONFIG_NAN_USD
8698
    if (mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
8699
        len >= IEEE80211_HDRLEN + 5 &&
8700
        mgmt->u.action.u.vs_public_action.action ==
8701
        WLAN_PA_VENDOR_SPECIFIC &&
8702
        WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
8703
        OUI_WFA &&
8704
        mgmt->u.action.u.vs_public_action.variable[0] ==
8705
        NAN_SDF_OUI_TYPE) {
8706
      const u8 *pos, *end;
8707
8708
      pos = mgmt->u.action.u.vs_public_action.variable;
8709
      end = ((const u8 *) mgmt) + len;
8710
      pos++;
8711
      hostapd_nan_usd_rx_sdf(hapd, mgmt->sa, mgmt->bssid,
8712
                 freq, pos, end - pos);
8713
      return 1;
8714
    }
8715
#endif /* CONFIG_NAN_USD */
8716
0
    if (hapd->public_action_cb) {
8717
0
      hapd->public_action_cb(hapd->public_action_cb_ctx,
8718
0
                 (u8 *) mgmt, len, freq);
8719
0
    }
8720
0
    if (hapd->public_action_cb2) {
8721
0
      hapd->public_action_cb2(hapd->public_action_cb2_ctx,
8722
0
            (u8 *) mgmt, len, freq);
8723
0
    }
8724
0
    if (hapd->public_action_cb || hapd->public_action_cb2)
8725
0
      return 1;
8726
0
    break;
8727
0
  case WLAN_ACTION_VENDOR_SPECIFIC:
8728
0
    if (hapd->vendor_action_cb) {
8729
0
      if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
8730
0
               (u8 *) mgmt, len, freq) == 0)
8731
0
        return 1;
8732
0
    }
8733
0
    if (sta &&
8734
0
        hostapd_action_vs(hapd, sta, mgmt, len, freq, false) == 0)
8735
0
      return 1;
8736
0
    break;
8737
0
  case WLAN_ACTION_VENDOR_SPECIFIC_PROTECTED:
8738
0
    if (sta &&
8739
0
        hostapd_action_vs(hapd, sta, mgmt, len, freq, true) == 0)
8740
0
      return 1;
8741
0
    break;
8742
0
#ifndef CONFIG_NO_RRM
8743
0
  case WLAN_ACTION_RADIO_MEASUREMENT:
8744
0
    hostapd_handle_radio_measurement(hapd, (const u8 *) mgmt, len);
8745
0
    return 1;
8746
0
#endif /* CONFIG_NO_RRM */
8747
0
#ifdef CONFIG_IEEE80211BE
8748
0
  case WLAN_ACTION_PROTECTED_EHT:
8749
0
    ieee802_11_rx_protected_eht_action(hapd, mgmt, len);
8750
0
    return 1;
8751
0
#endif /* CONFIG_IEEE80211BE */
8752
0
  }
8753
8754
0
  hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8755
0
           HOSTAPD_LEVEL_DEBUG,
8756
0
           "handle_action - unknown action category %d or invalid "
8757
0
           "frame",
8758
0
           mgmt->u.action.category);
8759
8760
0
  return 1;
8761
0
}
8762
8763
8764
/**
8765
 * notify_mgmt_frame - Notify of Management frames on the control interface
8766
 * @hapd: hostapd BSS data structure (the BSS to which the Management frame was
8767
 * sent to)
8768
 * @buf: Management frame data (starting from the IEEE 802.11 header)
8769
 * @len: Length of frame data in octets
8770
 *
8771
 * Notify the control interface of any received Management frame.
8772
 */
8773
static void notify_mgmt_frame(struct hostapd_data *hapd, const u8 *buf,
8774
            size_t len)
8775
0
{
8776
8777
0
  int hex_len = len * 2 + 1;
8778
0
  char *hex = os_malloc(hex_len);
8779
8780
0
  if (hex) {
8781
0
    wpa_snprintf_hex(hex, hex_len, buf, len);
8782
0
    wpa_msg_ctrl(hapd->msg_ctx, MSG_INFO,
8783
0
           AP_MGMT_FRAME_RECEIVED "buf=%s", hex);
8784
0
    os_free(hex);
8785
0
  }
8786
0
}
8787
8788
8789
/**
8790
 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
8791
 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
8792
 * sent to)
8793
 * @buf: management frame data (starting from IEEE 802.11 header)
8794
 * @len: length of frame data in octets
8795
 * @fi: meta data about received frame (signal level, etc.)
8796
 *
8797
 * Process all incoming IEEE 802.11 management frames. This will be called for
8798
 * each frame received from the kernel driver through wlan#ap interface. In
8799
 * addition, it can be called to re-inserted pending frames (e.g., when using
8800
 * external RADIUS server as an MAC ACL).
8801
 */
8802
int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
8803
        struct hostapd_frame_info *fi)
8804
0
{
8805
0
  struct ieee80211_mgmt *mgmt;
8806
0
  u16 fc, stype;
8807
0
  int ret = 0;
8808
0
  unsigned int freq;
8809
0
  int ssi_signal = fi ? fi->ssi_signal : 0;
8810
8811
0
  if (len < 24)
8812
0
    return 0;
8813
8814
0
  if (fi && fi->freq)
8815
0
    freq = fi->freq;
8816
0
  else
8817
0
    freq = hapd->iface->freq;
8818
8819
0
  mgmt = (struct ieee80211_mgmt *) buf;
8820
0
  fc = le_to_host16(mgmt->frame_control);
8821
0
  stype = WLAN_FC_GET_STYPE(fc);
8822
8823
0
  if (is_multicast_ether_addr(mgmt->sa) ||
8824
0
      is_zero_ether_addr(mgmt->sa) ||
8825
0
      ether_addr_equal(mgmt->sa, hapd->own_addr)) {
8826
    /* Do not process any frames with unexpected/invalid SA so that
8827
     * we do not add any state for unexpected STA addresses or end
8828
     * up sending out frames to unexpected destination. */
8829
0
    wpa_printf(MSG_DEBUG, "MGMT: Invalid SA=" MACSTR
8830
0
         " in received frame - ignore this frame silently",
8831
0
         MAC2STR(mgmt->sa));
8832
0
    return 0;
8833
0
  }
8834
8835
0
  if (stype == WLAN_FC_STYPE_BEACON) {
8836
0
    handle_beacon(hapd, mgmt, len, fi);
8837
0
    return 1;
8838
0
  }
8839
8840
0
  if (!is_broadcast_ether_addr(mgmt->bssid) &&
8841
#ifdef CONFIG_NAN_USD
8842
      !nan_de_is_nan_network_id(mgmt->bssid) &&
8843
      !nan_de_is_p2p_network_id(mgmt->bssid) &&
8844
#endif /* CONFIG_NAN_USD */
8845
#ifdef CONFIG_P2P
8846
      /* Invitation responses can be sent with the peer MAC as BSSID */
8847
      !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
8848
        stype == WLAN_FC_STYPE_ACTION) &&
8849
#endif /* CONFIG_P2P */
8850
#ifdef CONFIG_MESH
8851
      !(hapd->conf->mesh & MESH_ENABLED) &&
8852
#endif /* CONFIG_MESH */
8853
0
#ifdef CONFIG_IEEE80211BE
8854
0
      !(hapd->conf->mld_ap &&
8855
0
        ether_addr_equal(hapd->mld->mld_addr, mgmt->bssid)) &&
8856
0
#endif /* CONFIG_IEEE80211BE */
8857
0
      !ether_addr_equal(mgmt->bssid, hapd->own_addr)) {
8858
0
    wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
8859
0
         MAC2STR(mgmt->bssid));
8860
0
    return 0;
8861
0
  }
8862
8863
0
  if (hapd->iface->state != HAPD_IFACE_ENABLED) {
8864
0
    wpa_printf(MSG_DEBUG, "MGMT: Ignore management frame while interface is not enabled (SA=" MACSTR " DA=" MACSTR " subtype=%u)",
8865
0
         MAC2STR(mgmt->sa), MAC2STR(mgmt->da), stype);
8866
0
    return 1;
8867
0
  }
8868
8869
0
  if (stype == WLAN_FC_STYPE_PROBE_REQ) {
8870
0
    handle_probe_req(hapd, mgmt, len, ssi_signal);
8871
0
    return 1;
8872
0
  }
8873
8874
0
  if ((!is_broadcast_ether_addr(mgmt->da) ||
8875
0
       stype != WLAN_FC_STYPE_ACTION) &&
8876
0
#ifdef CONFIG_IEEE80211BE
8877
0
      !(hapd->conf->mld_ap &&
8878
0
        ether_addr_equal(hapd->mld->mld_addr, mgmt->bssid)) &&
8879
0
#endif /* CONFIG_IEEE80211BE */
8880
#ifdef CONFIG_NAN_USD
8881
      !ether_addr_equal(mgmt->da, nan_network_id) &&
8882
      !ether_addr_equal(mgmt->da, p2p_network_id) &&
8883
#endif /* CONFIG_NAN_USD */
8884
0
      !ether_addr_equal(mgmt->da, hapd->own_addr)) {
8885
0
    hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8886
0
             HOSTAPD_LEVEL_DEBUG,
8887
0
             "MGMT: DA=" MACSTR " not our address",
8888
0
             MAC2STR(mgmt->da));
8889
0
    return 0;
8890
0
  }
8891
8892
0
  if (hapd->iconf->track_sta_max_num)
8893
0
    sta_track_add(hapd->iface, mgmt->sa, ssi_signal);
8894
8895
0
  if (hapd->conf->notify_mgmt_frames)
8896
0
    notify_mgmt_frame(hapd, buf, len);
8897
8898
0
  switch (stype) {
8899
0
  case WLAN_FC_STYPE_AUTH:
8900
0
    wpa_printf(MSG_DEBUG, "mgmt::auth");
8901
0
    handle_auth(hapd, mgmt, len, ssi_signal, 0);
8902
0
    ret = 1;
8903
0
    break;
8904
0
  case WLAN_FC_STYPE_ASSOC_REQ:
8905
0
    wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
8906
0
    handle_assoc(hapd, mgmt, len, 0, ssi_signal);
8907
0
    ret = 1;
8908
0
    break;
8909
0
  case WLAN_FC_STYPE_REASSOC_REQ:
8910
0
    wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
8911
0
    handle_assoc(hapd, mgmt, len, 1, ssi_signal);
8912
0
    ret = 1;
8913
0
    break;
8914
0
  case WLAN_FC_STYPE_DISASSOC:
8915
0
    wpa_printf(MSG_DEBUG, "mgmt::disassoc");
8916
0
    handle_disassoc(hapd, mgmt, len);
8917
0
    ret = 1;
8918
0
    break;
8919
0
  case WLAN_FC_STYPE_DEAUTH:
8920
0
    wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
8921
0
    handle_deauth(hapd, mgmt, len);
8922
0
    ret = 1;
8923
0
    break;
8924
0
  case WLAN_FC_STYPE_ACTION:
8925
0
    wpa_printf(MSG_DEBUG, "mgmt::action");
8926
0
    ret = handle_action(hapd, mgmt, len, freq);
8927
0
    break;
8928
0
  default:
8929
0
    hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
8930
0
             HOSTAPD_LEVEL_DEBUG,
8931
0
             "unknown mgmt frame subtype %d", stype);
8932
0
    break;
8933
0
  }
8934
8935
0
  return ret;
8936
0
}
8937
8938
8939
static void handle_auth_cb(struct hostapd_data *hapd,
8940
         const struct ieee80211_mgmt *mgmt,
8941
         size_t len, int ok)
8942
0
{
8943
0
  u16 auth_alg, auth_transaction, status_code;
8944
0
  struct sta_info *sta;
8945
0
  bool success_status;
8946
8947
0
  sta = ap_get_sta(hapd, mgmt->da);
8948
0
  if (!sta) {
8949
0
    wpa_printf(MSG_DEBUG, "handle_auth_cb: STA " MACSTR
8950
0
         " not found",
8951
0
         MAC2STR(mgmt->da));
8952
0
    return;
8953
0
  }
8954
8955
0
  if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
8956
0
    wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
8957
0
         (unsigned long) len);
8958
0
    auth_alg = 0;
8959
0
    auth_transaction = 0;
8960
0
    status_code = WLAN_STATUS_UNSPECIFIED_FAILURE;
8961
0
    goto fail;
8962
0
  }
8963
8964
0
  auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
8965
0
  auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
8966
0
  status_code = le_to_host16(mgmt->u.auth.status_code);
8967
8968
0
  if (!ok) {
8969
0
    hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
8970
0
             HOSTAPD_LEVEL_NOTICE,
8971
0
             "did not acknowledge authentication response");
8972
0
    goto fail;
8973
0
  }
8974
8975
0
  if (status_code == WLAN_STATUS_SUCCESS &&
8976
0
      ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
8977
0
       (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
8978
0
    hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
8979
0
             HOSTAPD_LEVEL_INFO, "authenticated");
8980
0
    sta->flags |= WLAN_STA_AUTH;
8981
0
    if (sta->added_unassoc)
8982
0
      hostapd_set_sta_flags(hapd, sta);
8983
0
    return;
8984
0
  }
8985
8986
0
fail:
8987
0
  success_status = status_code == WLAN_STATUS_SUCCESS;
8988
#ifdef CONFIG_SAE
8989
  if (auth_alg == WLAN_AUTH_SAE &&
8990
      auth_transaction == WLAN_AUTH_TR_SEQ_SAE_COMMIT)
8991
    success_status = sae_status_success(hapd, status_code);
8992
#endif /* CONFIG_SAE */
8993
#ifdef CONFIG_IEEE8021X_AUTH
8994
  if (auth_alg == WLAN_AUTH_802_1X &&
8995
      status_code == WLAN_STATUS_802_1_X_AUTH_SUCCESS) {
8996
    sta->flags |= WLAN_STA_AUTH;
8997
    sta->auth_alg = WLAN_AUTH_802_1X;
8998
    success_status = true;
8999
  }
9000
#endif  /* CONFIG_IEEE8021X_AUTH */
9001
0
  if (!success_status && sta->added_unassoc) {
9002
0
    hostapd_drv_sta_remove(hapd, sta->addr);
9003
0
    sta->added_unassoc = 0;
9004
0
  }
9005
0
}
9006
9007
9008
static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
9009
               struct sta_info *sta,
9010
               char *ifname_wds)
9011
0
{
9012
#ifdef CONFIG_WEP
9013
  int i;
9014
  struct hostapd_ssid *ssid = &hapd->conf->ssid;
9015
9016
  if (hapd->conf->ieee802_1x || hapd->conf->wpa)
9017
    return;
9018
9019
  for (i = 0; i < 4; i++) {
9020
    if (ssid->wep.key[i] &&
9021
        hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
9022
          0, i == ssid->wep.idx, NULL, 0,
9023
          ssid->wep.key[i], ssid->wep.len[i],
9024
          i == ssid->wep.idx ?
9025
          KEY_FLAG_GROUP_RX_TX_DEFAULT :
9026
          KEY_FLAG_GROUP_RX_TX)) {
9027
      wpa_printf(MSG_WARNING,
9028
           "Could not set WEP keys for WDS interface; %s",
9029
           ifname_wds);
9030
      break;
9031
    }
9032
  }
9033
#endif /* CONFIG_WEP */
9034
0
}
9035
9036
9037
#ifdef CONFIG_IEEE80211BE
9038
static void ieee80211_ml_link_sta_assoc_cb(struct hostapd_data *hapd,
9039
             struct sta_info *sta,
9040
             struct mld_link_info *link,
9041
             bool ok)
9042
0
{
9043
0
  bool updated = false;
9044
9045
0
  if (!ok) {
9046
0
    hostapd_logger(hapd, link->peer_addr, HOSTAPD_MODULE_IEEE80211,
9047
0
             HOSTAPD_LEVEL_DEBUG,
9048
0
             "did not acknowledge association response");
9049
0
    sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
9050
9051
    /* The STA is added only in case of SUCCESS */
9052
0
    if (link->status == WLAN_STATUS_SUCCESS)
9053
0
      hostapd_drv_sta_remove(hapd, sta->addr);
9054
9055
0
    return;
9056
0
  }
9057
9058
0
  if (link->status != WLAN_STATUS_SUCCESS)
9059
0
    return;
9060
9061
0
  sta->flags |= WLAN_STA_ASSOC;
9062
0
  sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
9063
9064
0
  if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
9065
0
    updated = ap_sta_set_authorized_flag(hapd, sta, 1);
9066
9067
0
  hostapd_set_sta_flags(hapd, sta);
9068
0
  if (updated)
9069
0
    ap_sta_set_authorized_event(hapd, sta, 1);
9070
9071
  /*
9072
   * TODOs:
9073
   * - IEEE 802.1X port enablement is not needed as done on the station
9074
   *     doing the connection.
9075
   * - Not handling accounting
9076
   * - Need to handle VLAN configuration
9077
   */
9078
0
}
9079
#endif /* CONFIG_IEEE80211BE */
9080
9081
9082
static void hostapd_ml_handle_assoc_cb(struct hostapd_data *hapd,
9083
               struct sta_info *sta, bool ok)
9084
0
{
9085
0
#ifdef CONFIG_IEEE80211BE
9086
0
  struct hostapd_data *tmp_hapd;
9087
9088
0
  if (!hostapd_is_multiple_link_mld(hapd))
9089
0
    return;
9090
9091
0
  for_each_mld_link(tmp_hapd, hapd) {
9092
0
    struct mld_link_info *link;
9093
0
    struct sta_info *tmp_sta;
9094
9095
0
    if (tmp_hapd == hapd)
9096
0
      continue;
9097
9098
0
    link = &sta->mld_info.links[tmp_hapd->mld_link_id];
9099
0
    if (!link->valid)
9100
0
      continue;
9101
9102
0
    for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
9103
0
         tmp_sta = tmp_sta->next) {
9104
0
      if (tmp_sta == sta ||
9105
0
          tmp_sta->mld_assoc_link_id !=
9106
0
          sta->mld_assoc_link_id ||
9107
0
          tmp_sta->aid != sta->aid)
9108
0
        continue;
9109
9110
0
      ieee80211_ml_link_sta_assoc_cb(tmp_hapd, tmp_sta, link,
9111
0
                   ok);
9112
0
      break;
9113
0
    }
9114
0
  }
9115
0
#endif /* CONFIG_IEEE80211BE */
9116
0
}
9117
9118
9119
static void handle_assoc_cb(struct hostapd_data *hapd,
9120
          const struct ieee80211_mgmt *mgmt,
9121
          size_t len, int reassoc, int ok)
9122
0
{
9123
0
  u16 status;
9124
0
  struct sta_info *sta;
9125
0
  int new_assoc = 1;
9126
9127
0
  sta = ap_get_sta(hapd, mgmt->da);
9128
0
  if (!sta) {
9129
0
    wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
9130
0
         MAC2STR(mgmt->da));
9131
0
    return;
9132
0
  }
9133
9134
0
#ifdef CONFIG_IEEE80211BE
9135
0
  if (ap_sta_is_mld(hapd, sta) &&
9136
0
      hapd->mld_link_id != sta->mld_assoc_link_id) {
9137
    /* See ieee80211_ml_link_sta_assoc_cb() for the MLD case */
9138
0
    wpa_printf(MSG_DEBUG,
9139
0
         "%s: MLD: ignore on link station (%d != %d)",
9140
0
         __func__, hapd->mld_link_id, sta->mld_assoc_link_id);
9141
0
    return;
9142
0
  }
9143
0
#endif /* CONFIG_IEEE80211BE */
9144
9145
0
  if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
9146
0
              sizeof(mgmt->u.assoc_resp))) {
9147
0
    wpa_printf(MSG_INFO,
9148
0
         "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
9149
0
         reassoc, (unsigned long) len);
9150
0
    hostapd_drv_sta_remove(hapd, sta->addr);
9151
0
    return;
9152
0
  }
9153
9154
0
  if (reassoc)
9155
0
    status = le_to_host16(mgmt->u.reassoc_resp.status_code);
9156
0
  else
9157
0
    status = le_to_host16(mgmt->u.assoc_resp.status_code);
9158
9159
0
  if (!ok) {
9160
0
    hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
9161
0
             HOSTAPD_LEVEL_DEBUG,
9162
0
             "did not acknowledge association response");
9163
0
    sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
9164
    /* The STA is added only in case of SUCCESS */
9165
0
    if (status == WLAN_STATUS_SUCCESS)
9166
0
      hostapd_drv_sta_remove(hapd, sta->addr);
9167
9168
0
    goto handle_ml;
9169
0
  }
9170
9171
0
  if (status != WLAN_STATUS_SUCCESS)
9172
0
    goto handle_ml;
9173
9174
  /* Stop previous accounting session, if one is started, and allocate
9175
   * new session id for the new session. */
9176
0
  accounting_sta_stop(hapd, sta);
9177
9178
0
  hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
9179
0
           HOSTAPD_LEVEL_INFO,
9180
0
           "associated (aid %d)",
9181
0
           sta->aid);
9182
9183
0
  if (sta->flags & WLAN_STA_ASSOC)
9184
0
    new_assoc = 0;
9185
0
  sta->flags |= WLAN_STA_ASSOC;
9186
0
  sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
9187
0
  if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
9188
0
      sta->auth_alg == WLAN_AUTH_FILS_SK ||
9189
0
      sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
9190
0
      sta->auth_alg == WLAN_AUTH_FILS_PK ||
9191
0
      sta->auth_alg == WLAN_AUTH_FT ||
9192
0
      sta->auth_alg == WLAN_AUTH_EPPKE ||
9193
0
      sta->auth_alg == WLAN_AUTH_802_1X) {
9194
    /*
9195
     * Open, static WEP, FT protocol, or FILS; no separate
9196
     * authorization step.
9197
     */
9198
0
    ap_sta_set_authorized(hapd, sta, 1);
9199
0
  }
9200
9201
0
  if (reassoc)
9202
0
    mlme_reassociate_indication(hapd, sta);
9203
0
  else
9204
0
    mlme_associate_indication(hapd, sta);
9205
9206
0
  ap_sta_set_sa_query_timeout(hapd, sta, 0);
9207
9208
#ifdef CONFIG_PMKSA_PRIVACY
9209
  if (ok && status == WLAN_STATUS_SUCCESS && sta->epp_sta &&
9210
      wpa_auth_ap_sta_support_pmkid_privacy(sta->wpa_sm)) {
9211
    bool is_ml = ap_sta_is_mld(hapd, sta);
9212
    struct rsn_pmksa_cache_entry *entry, *next;
9213
    struct rsn_pmksa_cache *pmksa, *t_pmksa;
9214
    const u8 *pmkid_cur, *pmkid_next;
9215
9216
    switch (sta->auth_alg) {
9217
    case WLAN_AUTH_EPPKE:
9218
      if (!sta->pasn) {
9219
        wpa_printf(MSG_INFO, "EPP: Missing PASN data");
9220
        goto skip_update;
9221
      }
9222
      pmkid_cur = sta->pasn->epp_pmkid_cur;
9223
      pmkid_next = sta->epp_pmkid_next;
9224
      break;
9225
#ifdef CONFIG_IEEE8021X_AUTH
9226
    case WLAN_AUTH_802_1X:
9227
      pmkid_cur = sta->eap_auth_data.epp_pmkid_cur;
9228
      pmkid_next = sta->eap_auth_data.epp_pmkid_next;
9229
      break;
9230
#endif /* CONFIG_IEEE8021X_AUTH */
9231
    default:
9232
      wpa_printf(MSG_INFO,
9233
           "EPP: Unsupported auth alg %u for PMKID privacy support",
9234
           sta->auth_alg);
9235
      goto skip_update;
9236
    }
9237
9238
    pmksa = t_pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth,
9239
                 is_ml);
9240
9241
    entry = pmksa_cache_auth_get(t_pmksa, NULL, pmkid_cur);
9242
    if (entry)
9243
      goto update_pmksa_entry;
9244
9245
#ifdef CONFIG_IEEE80211BE
9246
    if (!entry && is_ml) {
9247
      struct hostapd_data *tmp_hapd;
9248
9249
      /* Search in link caches of each AP MLD link */
9250
      for_each_mld_link(tmp_hapd, hapd) {
9251
        t_pmksa = wpa_auth_get_pmksa_cache(
9252
          tmp_hapd->wpa_auth, false);
9253
        entry = pmksa_cache_auth_get(t_pmksa, NULL,
9254
                   pmkid_cur);
9255
        if (entry)
9256
          break;
9257
      }
9258
    } else if (!entry && !is_ml && hapd->conf->mld_ap) {
9259
      /* Search in the MLD cache */
9260
      t_pmksa = wpa_auth_get_pmksa_cache(hapd->wpa_auth,
9261
                 true);
9262
      entry = pmksa_cache_auth_get(t_pmksa, NULL, pmkid_cur);
9263
    }
9264
#endif /* CONFIG_IEEE80211BE */
9265
9266
update_pmksa_entry:
9267
    if (entry) {
9268
      wpa_printf(MSG_DEBUG,
9269
           "EPP: PMKSA caching privacy on - update PMKSA cache entry");
9270
      next = os_memdup(entry, sizeof(*entry));
9271
      if (!next)
9272
        goto skip_update;
9273
      os_memcpy(next->pmkid, pmkid_next, PMKID_LEN);
9274
      os_memcpy(next->spa, sta->addr, ETH_ALEN);
9275
      next->vlan_desc = NULL;
9276
      next->identity = NULL;
9277
      next->dpp_pkhash = NULL;
9278
      next->cui = NULL;
9279
      pmksa_cache_from_eapol_data(next, sta->eapol_sm);
9280
      pmksa_cache_free_entry(t_pmksa, entry);
9281
      pmksa_cache_auth_add_entry(pmksa, next);
9282
    }
9283
  }
9284
skip_update:
9285
#endif /* CONFIG_PMKSA_PRIVACY */
9286
9287
0
  if (sta->eapol_sm == NULL) {
9288
    /*
9289
     * This STA does not use RADIUS server for EAP authentication,
9290
     * so bind it to the selected VLAN interface now, since the
9291
     * interface selection is not going to change anymore.
9292
     */
9293
0
    if (ap_sta_bind_vlan(hapd, sta) < 0)
9294
0
      goto handle_ml;
9295
0
  } else if (sta->vlan_id) {
9296
    /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
9297
0
    if (ap_sta_bind_vlan(hapd, sta) < 0)
9298
0
      goto handle_ml;
9299
0
  }
9300
9301
0
  hostapd_set_sta_flags(hapd, sta);
9302
9303
0
  if (!(sta->flags & WLAN_STA_WDS) && sta->pending_wds_enable) {
9304
0
    wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for STA "
9305
0
         MACSTR " based on pending request",
9306
0
         MAC2STR(sta->addr));
9307
0
    sta->pending_wds_enable = 0;
9308
0
    sta->flags |= WLAN_STA_WDS;
9309
0
  }
9310
9311
  /* WPS not supported on backhaul BSS. Disable 4addr mode on fronthaul */
9312
0
  if ((sta->flags & WLAN_STA_WDS) ||
9313
0
      (sta->flags & WLAN_STA_MULTI_AP &&
9314
0
       (hapd->conf->multi_ap & BACKHAUL_BSS) &&
9315
0
       hapd->conf->wds_sta &&
9316
0
       !(sta->flags & WLAN_STA_WPS))) {
9317
0
    int ret;
9318
0
    char ifname_wds[IFNAMSIZ + 1];
9319
9320
0
    wpa_printf(MSG_DEBUG, "Reenable 4-address WDS mode for STA "
9321
0
         MACSTR " (aid %u)",
9322
0
         MAC2STR(sta->addr), sta->aid);
9323
0
    ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
9324
0
            sta->aid, 1);
9325
0
    if (!ret)
9326
0
      hostapd_set_wds_encryption(hapd, sta, ifname_wds);
9327
0
  }
9328
9329
0
  if (sta->auth_alg == WLAN_AUTH_FT)
9330
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
9331
0
  else
9332
0
    wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
9333
0
  hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
9334
0
  ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
9335
9336
#ifdef CONFIG_FILS
9337
  if ((sta->auth_alg == WLAN_AUTH_FILS_SK ||
9338
       sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
9339
       sta->auth_alg == WLAN_AUTH_FILS_PK) &&
9340
      fils_set_tk(sta->wpa_sm) < 0) {
9341
    wpa_printf(MSG_DEBUG, "FILS: TK configuration failed");
9342
    ap_sta_disconnect(hapd, sta, sta->addr,
9343
          WLAN_REASON_UNSPECIFIED);
9344
    return;
9345
  }
9346
#endif /* CONFIG_FILS */
9347
9348
0
  if (sta->pending_eapol_rx) {
9349
0
    struct os_reltime now, age;
9350
9351
0
    os_get_reltime(&now);
9352
0
    os_reltime_sub(&now, &sta->pending_eapol_rx->rx_time, &age);
9353
0
    if (age.sec == 0 && age.usec < 200000) {
9354
0
      wpa_printf(MSG_DEBUG,
9355
0
           "Process pending EAPOL frame that was received from " MACSTR " just before association notification",
9356
0
           MAC2STR(sta->addr));
9357
0
      ieee802_1x_receive(
9358
0
        hapd, mgmt->da,
9359
0
        wpabuf_head(sta->pending_eapol_rx->buf),
9360
0
        wpabuf_len(sta->pending_eapol_rx->buf),
9361
0
        sta->pending_eapol_rx->encrypted);
9362
0
    }
9363
0
    wpabuf_free(sta->pending_eapol_rx->buf);
9364
0
    os_free(sta->pending_eapol_rx);
9365
0
    sta->pending_eapol_rx = NULL;
9366
0
  }
9367
9368
0
#ifdef CONFIG_ROBUST_AV
9369
0
  if (hapd->conf->enable_dscp_policy_capa && sta &&
9370
0
      (sta->flags & WLAN_STA_DSCP_POLICY) && sta->num_dscp_policies > 0) {
9371
0
    int *policy_ids = NULL;
9372
0
    size_t num_policies = 0;
9373
0
    unsigned int i;
9374
9375
0
    policy_ids = os_calloc(sta->num_dscp_policies, sizeof(int));
9376
0
    if (!policy_ids)
9377
0
      return;
9378
9379
0
    for (i = 0; i < sta->num_dscp_policies; i++)
9380
0
      policy_ids[num_policies++] =
9381
0
        sta->policies[i]->policy_id;
9382
9383
0
    hostapd_send_unsolicited_dscp_policy_request(
9384
0
      hapd, sta, 0, policy_ids, num_policies);
9385
0
    os_free(policy_ids);
9386
0
  }
9387
0
#endif /* CONFIG_ROBUST_AV */
9388
9389
0
handle_ml:
9390
0
  hostapd_ml_handle_assoc_cb(hapd, sta, ok);
9391
0
}
9392
9393
9394
static void handle_deauth_cb(struct hostapd_data *hapd,
9395
           const struct ieee80211_mgmt *mgmt,
9396
           size_t len, int ok)
9397
0
{
9398
0
  struct sta_info *sta;
9399
0
  if (is_multicast_ether_addr(mgmt->da))
9400
0
    return;
9401
0
  sta = ap_get_sta(hapd, mgmt->da);
9402
0
  if (!sta) {
9403
0
    wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
9404
0
         " not found", MAC2STR(mgmt->da));
9405
0
    return;
9406
0
  }
9407
0
  if (ok)
9408
0
    wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
9409
0
         MAC2STR(sta->addr));
9410
0
  else
9411
0
    wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
9412
0
         "deauth", MAC2STR(sta->addr));
9413
9414
0
  ap_sta_deauth_cb(hapd, sta);
9415
0
}
9416
9417
9418
static void handle_disassoc_cb(struct hostapd_data *hapd,
9419
             const struct ieee80211_mgmt *mgmt,
9420
             size_t len, int ok)
9421
0
{
9422
0
  struct sta_info *sta;
9423
0
  if (is_multicast_ether_addr(mgmt->da))
9424
0
    return;
9425
0
  sta = ap_get_sta(hapd, mgmt->da);
9426
0
  if (!sta) {
9427
0
    wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
9428
0
         " not found", MAC2STR(mgmt->da));
9429
0
    return;
9430
0
  }
9431
0
  if (ok)
9432
0
    wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
9433
0
         MAC2STR(sta->addr));
9434
0
  else
9435
0
    wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
9436
0
         "disassoc", MAC2STR(sta->addr));
9437
9438
0
  ap_sta_disassoc_cb(hapd, sta);
9439
0
}
9440
9441
9442
static void handle_action_cb(struct hostapd_data *hapd,
9443
           const struct ieee80211_mgmt *mgmt,
9444
           size_t len, int ok)
9445
0
{
9446
0
  struct sta_info *sta;
9447
0
#ifndef CONFIG_NO_RRM
9448
0
  const struct rrm_measurement_report_element *report;
9449
0
#endif /* CONFIG_NO_RRM */
9450
9451
#ifdef CONFIG_DPP
9452
  if (len >= IEEE80211_HDRLEN + 6 &&
9453
      mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
9454
      mgmt->u.action.u.vs_public_action.action ==
9455
      WLAN_PA_VENDOR_SPECIFIC &&
9456
      WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
9457
      OUI_WFA &&
9458
      mgmt->u.action.u.vs_public_action.variable[0] ==
9459
      DPP_OUI_TYPE) {
9460
    const u8 *pos, *end;
9461
9462
    pos = &mgmt->u.action.u.vs_public_action.variable[1];
9463
    end = ((const u8 *) mgmt) + len;
9464
    hostapd_dpp_tx_status(hapd, mgmt->da, pos, end - pos, ok);
9465
    return;
9466
  }
9467
  if (len >= IEEE80211_HDRLEN + 2 &&
9468
      mgmt->u.action.category == WLAN_ACTION_PUBLIC &&
9469
      (mgmt->u.action.u.public_action.action ==
9470
       WLAN_PA_GAS_INITIAL_REQ ||
9471
       mgmt->u.action.u.public_action.action ==
9472
       WLAN_PA_GAS_COMEBACK_REQ)) {
9473
    const u8 *pos, *end;
9474
9475
    pos = mgmt->u.action.u.public_action.variable;
9476
    end = ((const u8 *) mgmt) + len;
9477
    gas_query_ap_tx_status(hapd->gas, mgmt->da, pos, end - pos, ok);
9478
    return;
9479
  }
9480
#endif /* CONFIG_DPP */
9481
0
  if (is_multicast_ether_addr(mgmt->da))
9482
0
    return;
9483
0
  sta = ap_get_sta(hapd, mgmt->da);
9484
0
  if (!sta) {
9485
0
    wpa_printf(MSG_DEBUG, "handle_action_cb: STA " MACSTR
9486
0
         " not found", MAC2STR(mgmt->da));
9487
0
    return;
9488
0
  }
9489
9490
0
#ifdef CONFIG_HS20
9491
0
  if (ok && len >= IEEE80211_HDRLEN + 2 &&
9492
0
      mgmt->u.action.category == WLAN_ACTION_WNM &&
9493
0
      mgmt->u.action.u.vs_public_action.action == WNM_NOTIFICATION_REQ &&
9494
0
      sta->hs20_deauth_on_ack) {
9495
0
    wpa_printf(MSG_DEBUG, "HS 2.0: Deauthenticate STA " MACSTR
9496
0
         " on acknowledging the WNM-Notification",
9497
0
         MAC2STR(sta->addr));
9498
0
    ap_sta_session_timeout(hapd, sta, 0);
9499
0
    return;
9500
0
  }
9501
0
#endif /* CONFIG_HS20 */
9502
9503
0
#ifdef CONFIG_IEEE80211BE
9504
  /* Frame header (24B) + Category (1B) + Action code (1B) +
9505
   * Dialog token (1B) + Count (1B) + Status list (count * 3B)
9506
   */
9507
0
  if (len >= IEEE80211_HDRLEN + 3 + 1 + 3 &&
9508
0
      mgmt->u.action.category == WLAN_ACTION_PROTECTED_EHT &&
9509
0
      mgmt->u.action.u.link_reconf_resp.action ==
9510
0
      WLAN_PROT_EHT_LINK_RECONFIG_RESPONSE) {
9511
0
    hostapd_link_reconf_resp_tx_status(hapd, sta, mgmt, len, ok);
9512
0
    return;
9513
0
  }
9514
0
#endif /* CONFIG_IEEE80211BE */
9515
9516
0
#ifndef CONFIG_NO_RRM
9517
0
  if (len < 24 + 5 + sizeof(*report))
9518
0
    return;
9519
0
  report = (const struct rrm_measurement_report_element *)
9520
0
    &mgmt->u.action.u.rrm.variable[2];
9521
0
  if (mgmt->u.action.category == WLAN_ACTION_RADIO_MEASUREMENT &&
9522
0
      mgmt->u.action.u.rrm.action == WLAN_RRM_RADIO_MEASUREMENT_REQUEST &&
9523
0
      report->eid == WLAN_EID_MEASURE_REQUEST &&
9524
0
      report->len >= 3 &&
9525
0
      report->type == MEASURE_TYPE_BEACON)
9526
0
    hostapd_rrm_beacon_req_tx_status(hapd, mgmt, len, ok);
9527
0
#endif /* CONFIG_NO_RRM */
9528
0
}
9529
9530
9531
/**
9532
 * ieee802_11_mgmt_cb - Process management frame TX status callback
9533
 * @hapd: hostapd BSS data structure (the BSS from which the management frame
9534
 * was sent from)
9535
 * @buf: management frame data (starting from IEEE 802.11 header)
9536
 * @len: length of frame data in octets
9537
 * @stype: management frame subtype from frame control field
9538
 * @ok: Whether the frame was ACK'ed
9539
 */
9540
void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
9541
      u16 stype, int ok)
9542
0
{
9543
0
  const struct ieee80211_mgmt *mgmt;
9544
0
  mgmt = (const struct ieee80211_mgmt *) buf;
9545
9546
#ifdef CONFIG_TESTING_OPTIONS
9547
  if (hapd->ext_mgmt_frame_handling) {
9548
    size_t hex_len = 2 * len + 1;
9549
    char *hex = os_malloc(hex_len);
9550
9551
    if (hex) {
9552
      wpa_snprintf_hex(hex, hex_len, buf, len);
9553
      wpa_msg(hapd->msg_ctx, MSG_INFO,
9554
        "MGMT-TX-STATUS stype=%u ok=%d buf=%s",
9555
        stype, ok, hex);
9556
      os_free(hex);
9557
    }
9558
    return;
9559
  }
9560
#endif /* CONFIG_TESTING_OPTIONS */
9561
9562
0
  switch (stype) {
9563
0
  case WLAN_FC_STYPE_AUTH:
9564
0
    wpa_printf(MSG_DEBUG, "mgmt::auth cb");
9565
0
    handle_auth_cb(hapd, mgmt, len, ok);
9566
0
    break;
9567
0
  case WLAN_FC_STYPE_ASSOC_RESP:
9568
0
    wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
9569
0
    handle_assoc_cb(hapd, mgmt, len, 0, ok);
9570
0
    break;
9571
0
  case WLAN_FC_STYPE_REASSOC_RESP:
9572
0
    wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
9573
0
    handle_assoc_cb(hapd, mgmt, len, 1, ok);
9574
0
    break;
9575
0
  case WLAN_FC_STYPE_PROBE_RESP:
9576
0
    wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb ok=%d", ok);
9577
0
    break;
9578
0
  case WLAN_FC_STYPE_DEAUTH:
9579
0
    wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
9580
0
    handle_deauth_cb(hapd, mgmt, len, ok);
9581
0
    break;
9582
0
  case WLAN_FC_STYPE_DISASSOC:
9583
0
    wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
9584
0
    handle_disassoc_cb(hapd, mgmt, len, ok);
9585
0
    break;
9586
0
  case WLAN_FC_STYPE_ACTION:
9587
0
    wpa_printf(MSG_DEBUG, "mgmt::action cb ok=%d", ok);
9588
0
    handle_action_cb(hapd, mgmt, len, ok);
9589
0
    break;
9590
0
  default:
9591
0
    wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
9592
0
    break;
9593
0
  }
9594
0
}
9595
9596
9597
int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
9598
0
{
9599
  /* TODO */
9600
0
  return 0;
9601
0
}
9602
9603
9604
int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
9605
         char *buf, size_t buflen)
9606
0
{
9607
0
  int len = 0, ret;
9608
9609
0
  ret = os_snprintf(buf + len, buflen - len,
9610
0
        "auth_alg=%d\n",
9611
0
        sta->auth_alg);
9612
0
  if (os_snprintf_error(buflen - len, ret))
9613
0
    return len;
9614
0
  len += ret;
9615
9616
0
  return len;
9617
0
}
9618
9619
9620
void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
9621
           const u8 *buf, size_t len, int ack)
9622
0
{
9623
0
  struct sta_info *sta;
9624
0
  struct hostapd_iface *iface = hapd->iface;
9625
9626
0
  sta = ap_get_sta(hapd, addr);
9627
0
  if (sta == NULL && iface->num_bss > 1) {
9628
0
    size_t j;
9629
0
    for (j = 0; j < iface->num_bss; j++) {
9630
0
      hapd = iface->bss[j];
9631
0
      sta = ap_get_sta(hapd, addr);
9632
0
      if (sta)
9633
0
        break;
9634
0
    }
9635
0
  }
9636
0
  if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
9637
0
    return;
9638
0
  if (sta->flags & WLAN_STA_PENDING_POLL) {
9639
0
    wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
9640
0
         "activity poll", MAC2STR(sta->addr),
9641
0
         ack ? "ACKed" : "did not ACK");
9642
0
    if (ack)
9643
0
      sta->flags &= ~WLAN_STA_PENDING_POLL;
9644
0
  }
9645
9646
0
  ieee802_1x_tx_status(hapd, sta, buf, len, ack);
9647
0
}
9648
9649
9650
void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
9651
0
{
9652
0
  struct sta_info *sta;
9653
0
  struct hostapd_iface *iface = hapd->iface;
9654
9655
0
  sta = ap_get_sta(hapd, addr);
9656
0
  if (sta == NULL && iface->num_bss > 1) {
9657
0
    size_t j;
9658
0
    for (j = 0; j < iface->num_bss; j++) {
9659
0
      hapd = iface->bss[j];
9660
0
      sta = ap_get_sta(hapd, addr);
9661
0
      if (sta)
9662
0
        break;
9663
0
    }
9664
0
  }
9665
0
  if (sta == NULL)
9666
0
    return;
9667
0
  wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POLL_OK MACSTR,
9668
0
    MAC2STR(sta->addr));
9669
0
  if (!(sta->flags & WLAN_STA_PENDING_POLL))
9670
0
    return;
9671
9672
0
  wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
9673
0
       "activity poll", MAC2STR(sta->addr));
9674
0
  sta->flags &= ~WLAN_STA_PENDING_POLL;
9675
0
}
9676
9677
9678
void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
9679
        int wds)
9680
0
{
9681
0
  struct sta_info *sta;
9682
9683
0
  sta = ap_get_sta(hapd, src);
9684
0
  if (sta &&
9685
0
      ((sta->flags & WLAN_STA_ASSOC) ||
9686
0
       ((sta->flags & WLAN_STA_ASSOC_REQ_OK) && wds))) {
9687
0
    if (!hapd->conf->wds_sta)
9688
0
      return;
9689
9690
0
    if ((sta->flags & (WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK)) ==
9691
0
        WLAN_STA_ASSOC_REQ_OK) {
9692
0
      wpa_printf(MSG_DEBUG,
9693
0
           "Postpone 4-address WDS mode enabling for STA "
9694
0
           MACSTR " since TX status for AssocResp is not yet known",
9695
0
           MAC2STR(sta->addr));
9696
0
      sta->pending_wds_enable = 1;
9697
0
      return;
9698
0
    }
9699
9700
0
    if (wds && !(sta->flags & WLAN_STA_WDS)) {
9701
0
      int ret;
9702
0
      char ifname_wds[IFNAMSIZ + 1];
9703
9704
0
      wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
9705
0
           "STA " MACSTR " (aid %u)",
9706
0
           MAC2STR(sta->addr), sta->aid);
9707
0
      sta->flags |= WLAN_STA_WDS;
9708
0
      ret = hostapd_set_wds_sta(hapd, ifname_wds,
9709
0
              sta->addr, sta->aid, 1);
9710
0
      if (!ret)
9711
0
        hostapd_set_wds_encryption(hapd, sta,
9712
0
                 ifname_wds);
9713
0
    }
9714
0
    return;
9715
0
  }
9716
9717
0
  wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
9718
0
       MACSTR, MAC2STR(src));
9719
0
  if (is_multicast_ether_addr(src) || is_zero_ether_addr(src) ||
9720
0
      ether_addr_equal(src, hapd->own_addr)) {
9721
    /* Broadcast bit set in SA or unexpected SA?! Ignore the frame
9722
     * silently. */
9723
0
    return;
9724
0
  }
9725
9726
0
  if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
9727
0
    wpa_printf(MSG_DEBUG, "Association Response to the STA has "
9728
0
         "already been sent, but no TX status yet known - "
9729
0
         "ignore Class 3 frame issue with " MACSTR,
9730
0
         MAC2STR(src));
9731
0
    return;
9732
0
  }
9733
9734
0
  if (sta && (sta->flags & WLAN_STA_AUTH))
9735
0
    hostapd_drv_sta_disassoc(
9736
0
      hapd, src,
9737
0
      WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
9738
0
  else
9739
0
    hostapd_drv_sta_deauth(
9740
0
      hapd, src,
9741
0
      WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
9742
0
}
9743
9744
9745
static u8 * hostapd_add_tpe_info(u8 *eid, u8 tx_pwr_count,
9746
         enum max_tx_pwr_interpretation tx_pwr_intrpn,
9747
         u8 tx_pwr_cat, u8 tx_pwr)
9748
0
{
9749
0
  int i;
9750
9751
0
  *eid++ = WLAN_EID_TRANSMIT_POWER_ENVELOPE; /* Element ID */
9752
0
  *eid++ = 2 + tx_pwr_count; /* Length */
9753
9754
  /*
9755
   * Transmit Power Information field
9756
   *  bits 0-2 : Maximum Transmit Power Count
9757
   *  bits 3-5 : Maximum Transmit Power Interpretation
9758
   *  bits 6-7 : Maximum Transmit Power Category
9759
   */
9760
0
  *eid++ = tx_pwr_count | (tx_pwr_intrpn << 3) | (tx_pwr_cat << 6);
9761
9762
  /* Maximum Transmit Power field */
9763
0
  for (i = 0; i <= tx_pwr_count; i++)
9764
0
    *eid++ = tx_pwr;
9765
9766
0
  return eid;
9767
0
}
9768
9769
9770
/*
9771
 * TODO: Extract power limits from channel data after 6G regulatory
9772
 *  support.
9773
 */
9774
0
#define REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT      (-1) /* dBm/MHz */
9775
0
#define REG_PSD_MAX_TXPOWER_FOR_SUBORDINATE_CLIENT  5    /* dBm/MHz */
9776
9777
u8 * hostapd_eid_txpower_envelope(struct hostapd_data *hapd, u8 *eid)
9778
0
{
9779
0
  struct hostapd_iface *iface = hapd->iface;
9780
0
  struct hostapd_config *iconf = iface->conf;
9781
0
  struct hostapd_hw_modes *mode = iface->current_mode;
9782
0
  struct hostapd_channel_data *chan;
9783
0
  int dfs, i;
9784
0
  u8 channel, tx_pwr_count, local_pwr_constraint;
9785
0
  int max_tx_power;
9786
0
  u8 tx_pwr;
9787
9788
0
  if (!mode)
9789
0
    return eid;
9790
9791
0
  if (ieee80211_freq_to_chan(iface->freq, &channel) == NUM_HOSTAPD_MODES)
9792
0
    return eid;
9793
9794
0
  for (i = 0; i < mode->num_channels; i++) {
9795
0
    if (mode->channels[i].freq == iface->freq)
9796
0
      break;
9797
0
  }
9798
0
  if (i == mode->num_channels)
9799
0
    return eid;
9800
9801
0
#ifdef CONFIG_IEEE80211AX
9802
  /* IEEE Std 802.11ax-2021, Annex E.2.7 (6 GHz band in the United
9803
   * States): An AP that is an Indoor Access Point per regulatory rules
9804
   * shall send at least two Transmit Power Envelope elements in Beacon
9805
   * and Probe Response frames as follows:
9806
   *  - Maximum Transmit Power Category subfield = Default;
9807
   *  Unit interpretation = Regulatory client EIRP PSD
9808
   *  - Maximum Transmit Power Category subfield = Subordinate Device;
9809
   *  Unit interpretation = Regulatory client EIRP PSD
9810
   */
9811
0
  if (is_6ghz_op_class(iconf->op_class)) {
9812
0
    enum max_tx_pwr_interpretation tx_pwr_intrpn;
9813
0
    int err;
9814
0
    int max_eirp_psd = REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT * 2;
9815
0
    int max_eirp_power = iconf->reg_def_cli_eirp;
9816
9817
    /* Same Maximum Transmit Power for all 20 MHz bands */
9818
0
    tx_pwr_count = 0;
9819
0
    tx_pwr_intrpn = REGULATORY_CLIENT_EIRP_PSD;
9820
9821
    /* Default Transmit Power Envelope for Global Operating Class */
9822
0
    err = hostap_afc_get_chan_max_eirp_power(iface, true,
9823
0
               &max_eirp_psd);
9824
0
    if (err < 0) {
9825
0
      if (hapd->iconf->reg_def_cli_eirp_psd != -1)
9826
0
        max_eirp_psd =
9827
0
          hapd->iconf->reg_def_cli_eirp_psd;
9828
0
      else
9829
0
        max_eirp_psd =
9830
0
          REG_PSD_MAX_TXPOWER_FOR_DEFAULT_CLIENT * 2;
9831
0
    }
9832
9833
0
    eid = hostapd_add_tpe_info(eid, tx_pwr_count, tx_pwr_intrpn,
9834
0
             REG_DEFAULT_CLIENT, max_eirp_psd);
9835
9836
    /* Indoor Access Point must include an additional TPE for
9837
     * subordinate devices */
9838
0
    if (he_reg_is_indoor(iconf->he_6ghz_reg_pwr_type)) {
9839
0
      if (err < 0) {
9840
        /* non-AFC connection */
9841
0
        if (hapd->iconf->reg_sub_cli_eirp_psd != -1)
9842
0
          max_eirp_psd =
9843
0
            hapd->iconf->reg_sub_cli_eirp_psd;
9844
0
        else
9845
0
          max_eirp_psd =
9846
0
            REG_PSD_MAX_TXPOWER_FOR_SUBORDINATE_CLIENT * 2;
9847
0
      }
9848
0
      eid = hostapd_add_tpe_info(eid, tx_pwr_count,
9849
0
               tx_pwr_intrpn,
9850
0
               REG_SUBORDINATE_CLIENT,
9851
0
               max_eirp_psd);
9852
0
    }
9853
9854
0
    if (hostap_afc_get_chan_max_eirp_power(iface, false,
9855
0
                   &max_eirp_power)) {
9856
0
      max_eirp_power = iconf->reg_def_cli_eirp;
9857
0
      if (max_eirp_power == -1 ||
9858
0
          !he_reg_is_sp(iconf->he_6ghz_reg_pwr_type))
9859
0
        return eid;
9860
0
    }
9861
9862
0
    return hostapd_add_tpe_info(eid, tx_pwr_count,
9863
0
              REGULATORY_CLIENT_EIRP,
9864
0
              REG_DEFAULT_CLIENT,
9865
0
              max_eirp_power);
9866
0
  }
9867
0
#endif /* CONFIG_IEEE80211AX */
9868
9869
0
  switch (hostapd_get_oper_chwidth(iconf)) {
9870
0
  case CONF_OPER_CHWIDTH_USE_HT:
9871
0
    if (iconf->secondary_channel == 0) {
9872
      /* Max Transmit Power count = 0 (20 MHz) */
9873
0
      tx_pwr_count = 0;
9874
0
    } else {
9875
      /* Max Transmit Power count = 1 (20, 40 MHz) */
9876
0
      tx_pwr_count = 1;
9877
0
    }
9878
0
    break;
9879
0
  case CONF_OPER_CHWIDTH_80MHZ:
9880
    /* Max Transmit Power count = 2 (20, 40, and 80 MHz) */
9881
0
    tx_pwr_count = 2;
9882
0
    break;
9883
0
  case CONF_OPER_CHWIDTH_80P80MHZ:
9884
0
  case CONF_OPER_CHWIDTH_160MHZ:
9885
    /* Max Transmit Power count = 3 (20, 40, 80, 160/80+80 MHz) */
9886
0
    tx_pwr_count = 3;
9887
0
    break;
9888
0
  default:
9889
0
    return eid;
9890
0
  }
9891
9892
  /*
9893
   * Below local_pwr_constraint logic is referred from
9894
   * hostapd_eid_pwr_constraint.
9895
   *
9896
   * Check if DFS is required by regulatory.
9897
   */
9898
0
  dfs = hostapd_is_dfs_required(hapd->iface);
9899
0
  if (dfs < 0)
9900
0
    dfs = 0;
9901
9902
  /*
9903
   * In order to meet regulations when TPC is not implemented using
9904
   * a transmit power that is below the legal maximum (including any
9905
   * mitigation factor) should help. In this case, indicate 3 dB below
9906
   * maximum allowed transmit power.
9907
   */
9908
0
  if (hapd->iconf->local_pwr_constraint == -1)
9909
0
    local_pwr_constraint = (dfs == 0) ? 0 : 3;
9910
0
  else
9911
0
    local_pwr_constraint = hapd->iconf->local_pwr_constraint;
9912
9913
  /*
9914
   * A STA that is not an AP shall use a transmit power less than or
9915
   * equal to the local maximum transmit power level for the channel.
9916
   * The local maximum transmit power can be calculated from the formula:
9917
   * local max TX pwr = max TX pwr - local pwr constraint
9918
   * Where max TX pwr is maximum transmit power level specified for
9919
   * channel in Country element and local pwr constraint is specified
9920
   * for channel in this Power Constraint element.
9921
   */
9922
0
  chan = &mode->channels[i];
9923
0
  max_tx_power = chan->max_tx_power - local_pwr_constraint;
9924
9925
  /*
9926
   * Local Maximum Transmit power is encoded as two's complement
9927
   * with a 0.5 dB step.
9928
   */
9929
0
  max_tx_power *= 2; /* in 0.5 dB steps */
9930
0
  if (max_tx_power > 127) {
9931
    /* 63.5 has special meaning of 63.5 dBm or higher */
9932
0
    max_tx_power = 127;
9933
0
  }
9934
0
  if (max_tx_power < -128)
9935
0
    max_tx_power = -128;
9936
0
  if (max_tx_power < 0)
9937
0
    tx_pwr = 0x80 + max_tx_power + 128;
9938
0
  else
9939
0
    tx_pwr = max_tx_power;
9940
9941
0
  return hostapd_add_tpe_info(eid, tx_pwr_count, LOCAL_EIRP,
9942
0
            0 /* Reserved for bands other than 6 GHz */,
9943
0
            tx_pwr);
9944
0
}
9945
9946
9947
/* Wide Bandwidth Channel Switch subelement */
9948
static u8 * hostapd_eid_wb_channel_switch(struct hostapd_data *hapd, u8 *eid,
9949
            u8 chan1, u8 chan2)
9950
0
{
9951
0
  u8 bw;
9952
9953
  /* bandwidth: 0: 40, 1: 80, 160, 80+80, 4 to 255 reserved as per
9954
   * IEEE Std 802.11-2024, 9.4.2.156 and Table 9-316 (VHT Operation
9955
   * Information subfields).
9956
   */
9957
0
  switch (hapd->cs_freq_params.bandwidth) {
9958
0
  case 320:
9959
    /* As per IEEE Std 802.11be-2024, 35.15.3 (Channel switching
9960
     * methods for an EHT BSS), for EHT BSS operating channel width
9961
     * wider than 160 MHz, the announced BSS bandwidth in the Wide
9962
     * Bandwidth Channel Switch element is less than the BSS
9963
     * bandwidth in the Bandwidth Indication element
9964
     */
9965
9966
    /* Modifying the center frequency to 160 MHz */
9967
0
    if (hapd->cs_freq_params.channel < chan1)
9968
0
      chan1 -= 16;
9969
0
    else
9970
0
      chan1 += 16;
9971
9972
    /* fallthrough */
9973
0
  case 160:
9974
    /* Update the CCFS0 and CCFS1 values in the element based on
9975
     * IEEE Std 802.11-2024, Table 9-316 (VHT Operation
9976
     * Information subfields).
9977
     */
9978
9979
    /* CCFS1 - The channel center frequency index of the 160 MHz
9980
     * channel. */
9981
0
    chan2 = chan1;
9982
9983
    /* CCFS0 - The channel center frequency index of the 80 MHz
9984
     * channel segment that contains the primary channel. */
9985
0
    if (hapd->cs_freq_params.channel < chan1)
9986
0
      chan1 -= 8;
9987
0
    else
9988
0
      chan1 += 8;
9989
9990
0
    bw = 1;
9991
0
    break;
9992
0
  case 80:
9993
0
    bw = 1;
9994
0
    break;
9995
0
  case 40:
9996
0
    bw = 0;
9997
0
    break;
9998
0
  default:
9999
    /* not valid VHT bandwidth or not in CSA */
10000
0
    return eid;
10001
0
  }
10002
10003
0
  *eid++ = WLAN_EID_WIDE_BW_CHSWITCH;
10004
0
  *eid++ = 3; /* Length of Wide Bandwidth Channel Switch element */
10005
0
  *eid++ = bw; /* New Channel Width */
10006
0
  *eid++ = chan1; /* New Channel Center Frequency Segment 0 */
10007
0
  *eid++ = chan2; /* New Channel Center Frequency Segment 1 */
10008
10009
0
  return eid;
10010
0
}
10011
10012
10013
#ifdef CONFIG_IEEE80211BE
10014
/* Bandwidth Indication element that is also used as the Bandwidth Indication
10015
 * For Channel Switch subelement within a Channel Switch Wrapper element. */
10016
static u8 * hostapd_eid_bw_indication(struct hostapd_data *hapd, u8 *eid,
10017
              u8 chan1, u8 chan2)
10018
0
{
10019
0
  u16 punct_bitmap = hapd->cs_freq_params.punct_bitmap;
10020
0
  struct ieee80211_bw_ind_element *bw_ind_elem;
10021
0
  size_t elen = 4;
10022
10023
0
  if (hapd->cs_freq_params.bandwidth <= 160 && !punct_bitmap)
10024
0
    return eid;
10025
10026
0
  if (punct_bitmap)
10027
0
    elen += EHT_OPER_DISABLED_SUBCHAN_BITMAP_SIZE;
10028
10029
0
  *eid++ = WLAN_EID_EXTENSION;
10030
0
  *eid++ = 1 + elen;
10031
0
  *eid++ = WLAN_EID_EXT_BANDWIDTH_INDICATION;
10032
10033
0
  bw_ind_elem = (struct ieee80211_bw_ind_element *) eid;
10034
0
  os_memset(bw_ind_elem, 0, sizeof(struct ieee80211_bw_ind_element));
10035
10036
0
  switch (hapd->cs_freq_params.bandwidth) {
10037
0
  case 320:
10038
0
    bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_320MHZ;
10039
0
    chan2 = chan1;
10040
0
    if (hapd->cs_freq_params.channel < chan1)
10041
0
      chan1 -= 16;
10042
0
    else
10043
0
      chan1 += 16;
10044
0
    break;
10045
0
  case 160:
10046
0
    bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_160MHZ;
10047
0
    chan2 = chan1;
10048
0
    if (hapd->cs_freq_params.channel < chan1)
10049
0
      chan1 -= 8;
10050
0
    else
10051
0
      chan1 += 8;
10052
0
    break;
10053
0
  case 80:
10054
0
    bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_80MHZ;
10055
0
    break;
10056
0
  case 40:
10057
0
    if (hapd->cs_freq_params.sec_channel_offset == 1)
10058
0
      bw_ind_elem->bw_ind_info.control |=
10059
0
        BW_IND_CHANNEL_WIDTH_40MHZ;
10060
0
    else
10061
0
      bw_ind_elem->bw_ind_info.control |=
10062
0
        BW_IND_CHANNEL_WIDTH_20MHZ;
10063
0
    break;
10064
0
  default:
10065
0
    bw_ind_elem->bw_ind_info.control |= BW_IND_CHANNEL_WIDTH_20MHZ;
10066
0
    break;
10067
0
  }
10068
10069
0
  bw_ind_elem->bw_ind_info.ccfs0 = chan1;
10070
0
  bw_ind_elem->bw_ind_info.ccfs1 = chan2;
10071
10072
0
  if (punct_bitmap) {
10073
0
    bw_ind_elem->bw_ind_params |=
10074
0
      BW_IND_PARAMETER_DISABLED_SUBCHAN_BITMAP_PRESENT;
10075
0
    bw_ind_elem->bw_ind_info.disabled_chan_bitmap =
10076
0
      host_to_le16(punct_bitmap);
10077
0
  }
10078
10079
0
  return eid + elen;
10080
0
}
10081
#endif /* CONFIG_IEEE80211BE */
10082
10083
10084
u8 * hostapd_eid_chsw_wrapper(struct hostapd_data *hapd, u8 *eid)
10085
0
{
10086
0
  u8 chan1 = 0, chan2 = 0;
10087
0
  u8 *eid_len_offset;
10088
0
  int freq1;
10089
10090
0
  if (!hostapd_is_vht_enabled(hapd) &&
10091
0
      !hostapd_is_he_enabled(hapd) &&
10092
0
      !hostapd_is_eht_enabled(hapd))
10093
0
    return eid;
10094
10095
0
  if (!hapd->cs_freq_params.channel ||
10096
0
      (!hapd->cs_freq_params.vht_enabled &&
10097
0
       !hapd->cs_freq_params.he_enabled &&
10098
0
       !hapd->cs_freq_params.eht_enabled))
10099
0
    return eid;
10100
10101
0
  freq1 = hapd->cs_freq_params.center_freq1 ?
10102
0
    hapd->cs_freq_params.center_freq1 :
10103
0
    hapd->cs_freq_params.freq;
10104
0
  if (ieee80211_freq_to_chan(freq1, &chan1) !=
10105
0
      HOSTAPD_MODE_IEEE80211A)
10106
0
    return eid;
10107
10108
0
  if (hapd->cs_freq_params.center_freq2 &&
10109
0
      ieee80211_freq_to_chan(hapd->cs_freq_params.center_freq2,
10110
0
           &chan2) != HOSTAPD_MODE_IEEE80211A)
10111
0
    return eid;
10112
10113
0
  *eid++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER;
10114
0
  eid_len_offset = eid++; /* Length of Channel Switch Wrapper element */
10115
10116
0
  eid = hostapd_eid_wb_channel_switch(hapd, eid, chan1, chan2);
10117
10118
0
#ifdef CONFIG_IEEE80211BE
10119
0
  if (hostapd_is_eht_enabled(hapd)) {
10120
    /* Bandwidth Indication For Channel Switch subelement */
10121
0
    eid = hostapd_eid_bw_indication(hapd, eid, chan1, chan2);
10122
0
  }
10123
0
#endif /* CONFIG_IEEE80211BE */
10124
10125
0
  *eid_len_offset = (eid - eid_len_offset) - 1;
10126
0
  return eid;
10127
0
}
10128
10129
10130
static size_t hostapd_eid_nr_db_len(struct hostapd_data *hapd,
10131
            size_t *current_len)
10132
0
{
10133
0
  struct hostapd_neighbor_entry *nr;
10134
0
  size_t total_len = 0, len = *current_len;
10135
10136
0
  dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
10137
0
       list) {
10138
0
    if (!nr->nr || wpabuf_len(nr->nr) < 12)
10139
0
      continue;
10140
10141
0
    if (nr->short_ssid == hapd->conf->ssid.short_ssid)
10142
0
      continue;
10143
10144
    /* Start a new element */
10145
0
    if (!len ||
10146
0
        len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
10147
0
      len = RNR_HEADER_LEN;
10148
0
      total_len += RNR_HEADER_LEN;
10149
0
    }
10150
10151
0
    len += RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN;
10152
0
    total_len += RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN;
10153
0
  }
10154
10155
0
  *current_len = len;
10156
0
  return total_len;
10157
0
}
10158
10159
10160
#ifdef CONFIG_IEEE80211BE
10161
static bool hostapd_mbssid_mld_match(struct hostapd_data *tx_hapd,
10162
             struct hostapd_data *ml_hapd,
10163
             u8 *match_idx)
10164
0
{
10165
0
  size_t bss_idx;
10166
10167
0
  if (!ml_hapd->conf->mld_ap)
10168
0
    return false;
10169
10170
0
  if (!tx_hapd->iconf->mbssid || tx_hapd->iface->num_bss <= 1) {
10171
0
    if (hostapd_is_ml_partner(tx_hapd, ml_hapd)) {
10172
0
      if (match_idx)
10173
0
        *match_idx = 0;
10174
0
      return true;
10175
0
    }
10176
10177
0
    return false;
10178
0
  }
10179
10180
0
  for (bss_idx = 0; bss_idx < tx_hapd->iface->num_bss; bss_idx++) {
10181
0
    struct hostapd_data *bss = tx_hapd->iface->bss[bss_idx];
10182
10183
0
    if (!bss)
10184
0
      continue;
10185
10186
0
    if (hostapd_is_ml_partner(bss, ml_hapd)) {
10187
0
      if (match_idx)
10188
0
        *match_idx = bss_idx;
10189
0
      return true;
10190
0
    }
10191
0
  }
10192
10193
0
  return false;
10194
0
}
10195
#endif /* CONFIG_IEEE80211BE */
10196
10197
10198
struct mbssid_ie_profiles {
10199
  u8 start;
10200
  u8 end;
10201
};
10202
10203
static bool hostapd_skip_rnr(size_t i, struct mbssid_ie_profiles *skip_profiles,
10204
           bool ap_mld, u8 tbtt_info_len, bool mld_update,
10205
           struct hostapd_data *reporting_hapd,
10206
           struct hostapd_data *bss, u8 *match_idx)
10207
0
{
10208
0
  bool reporting_ap_mld = false;
10209
10210
0
#ifdef CONFIG_IEEE80211BE
10211
0
  reporting_ap_mld = !!reporting_hapd->conf->mld_ap;
10212
0
#endif /* CONFIG_IEEE80211BE */
10213
10214
0
  if (!mld_update && skip_profiles &&
10215
0
      i >= skip_profiles->start && i < skip_profiles->end)
10216
0
    return true;
10217
10218
  /* No need to report if length is for normal TBTT and both the reporting
10219
   * AP and neighbor AP are affiliated with an AP MLD. MLD TBTT will
10220
   * include this. */
10221
0
  if (tbtt_info_len == RNR_TBTT_INFO_LEN && ap_mld && reporting_ap_mld)
10222
0
    return true;
10223
10224
  /* No need to report if length is for MLD TBTT and the BSS is not
10225
   * affiliated with an aP MLD. Normal TBTT will include this. */
10226
0
  if (tbtt_info_len == RNR_TBTT_INFO_MLD_LEN && !ap_mld)
10227
0
    return true;
10228
10229
0
#ifdef CONFIG_IEEE80211BE
10230
  /* If building for co-location and they are ML partners, no need to
10231
   * include since the ML RNR will carry this. */
10232
0
  if (!mld_update && hostapd_is_ml_partner(reporting_hapd, bss))
10233
0
    return true;
10234
10235
  /* If building for ML RNR and they are not ML partners, don't include.
10236
   */
10237
0
  if (mld_update &&
10238
0
      !hostapd_mbssid_mld_match(reporting_hapd, bss, match_idx))
10239
0
    return true;
10240
10241
  /* When MLD parameters are added to beacon RNR and in case of EMA
10242
   * beacons we report only affiliated APs belonging to the reported
10243
   * non Tx profiles and TX profile will be reported in every EMA beacon.
10244
   */
10245
0
  if (mld_update && skip_profiles && match_idx &&
10246
0
      (*match_idx < skip_profiles->start ||
10247
0
       *match_idx >= skip_profiles->end))
10248
0
    return true;
10249
0
#endif /* CONFIG_IEEE80211BE */
10250
10251
0
  return false;
10252
0
}
10253
10254
10255
static bool hostapd_rnr_get_bss_info(struct hostapd_data *hapd,
10256
             struct hostapd_data *reporting_hapd,
10257
             struct mbssid_ie_profiles *skip_profiles,
10258
             size_t i, u8 tbtt_info_len,
10259
             bool mld_update,
10260
             u8 *op_class, u8 *channel, u8 *match_idx)
10261
0
{
10262
0
  struct hostapd_data *bss;
10263
0
  bool ap_mld = false;
10264
0
  u8 tmp_match_idx = 255;
10265
0
  enum oper_chan_width bss_chwidth;
10266
0
  int secondary_channel;
10267
0
  u8 seg0, seg1;
10268
10269
0
  if (!hapd->iface || i >= hapd->iface->num_bss || !op_class || !channel)
10270
0
    return false;
10271
10272
0
  bss = hapd->iface->bss[i];
10273
0
  if (!bss || !bss->conf || !bss->started || !bss->beacon_set_done ||
10274
0
      bss == reporting_hapd)
10275
0
    return false;
10276
10277
0
#ifdef CONFIG_IEEE80211BE
10278
0
  ap_mld = !!bss->conf->mld_ap;
10279
0
#endif /* CONFIG_IEEE80211BE */
10280
10281
  /* MLD RNR has to be included for the parameter change count */
10282
0
  if (bss->conf->ignore_broadcast_ssid && !(ap_mld && mld_update))
10283
0
    return false;
10284
10285
0
  if (!match_idx)
10286
0
    match_idx = &tmp_match_idx;
10287
10288
0
  if (hostapd_skip_rnr(i, skip_profiles, ap_mld, tbtt_info_len,
10289
0
           mld_update, reporting_hapd, bss, match_idx))
10290
0
    return false;
10291
10292
0
  hostapd_get_oper_chan_info_of_bss(bss, &bss_chwidth, &seg0, &seg1);
10293
0
  secondary_channel = bss->iconf->secondary_channel;
10294
10295
0
  if (seg0 == bss->iconf->channel &&
10296
0
      bss_chwidth == CONF_OPER_CHWIDTH_USE_HT)
10297
0
    secondary_channel = 0;
10298
10299
0
  if (ieee80211_freq_to_channel_ext(
10300
0
        bss->iface->freq,
10301
0
        secondary_channel, bss_chwidth,
10302
0
        op_class, channel) == NUM_HOSTAPD_MODES)
10303
0
    return false;
10304
10305
0
  return true;
10306
0
}
10307
10308
10309
static size_t
10310
hostapd_eid_rnr_iface_len(struct hostapd_data *hapd,
10311
        struct hostapd_data *reporting_hapd,
10312
        size_t *current_len,
10313
        struct mbssid_ie_profiles *skip_profiles,
10314
        bool mld_update)
10315
0
{
10316
0
  struct hostapd_iface *iface = hapd->iface;
10317
0
  size_t total_len = 0, len = *current_len;
10318
0
  int total_tbtt_count = 0;
10319
0
  size_t i;
10320
0
  u8 tbtt_info_len = mld_update ? RNR_TBTT_INFO_MLD_LEN :
10321
0
    RNR_TBTT_INFO_LEN;
10322
0
  bool reporting_ap_mld = false;
10323
0
  bool have_pending_group;
10324
0
  u8 pending_op_class = 0, pending_channel = 0;
10325
0
  bool *tbtt_added = NULL;
10326
10327
0
#ifdef CONFIG_IEEE80211BE
10328
0
  reporting_ap_mld = !!reporting_hapd->conf->mld_ap;
10329
0
#endif /* CONFIG_IEEE80211BE */
10330
10331
0
repeat_rnr_len:
10332
0
  os_free(tbtt_added);
10333
0
  tbtt_added = os_zalloc(iface->num_bss);
10334
0
  if (!tbtt_added)
10335
0
    return total_len;
10336
10337
0
  have_pending_group = false;
10338
0
  for (;;) {
10339
0
    int tbtt_count = 0;
10340
0
    bool group_found = false, group_pending = false;
10341
0
    u8 rnr_op_class = 0, rnr_channel = 0;
10342
10343
0
    if (have_pending_group) {
10344
0
      rnr_op_class = pending_op_class;
10345
0
      rnr_channel = pending_channel;
10346
0
      group_found = true;
10347
0
    }
10348
10349
0
    if (!group_found) {
10350
0
      for (i = 0; i < iface->num_bss; i++) {
10351
0
        if (tbtt_added[i])
10352
0
          continue;
10353
0
        if (!hostapd_rnr_get_bss_info(
10354
0
              hapd, reporting_hapd, skip_profiles,
10355
0
              i, tbtt_info_len, mld_update,
10356
0
              &rnr_op_class, &rnr_channel, NULL))
10357
0
          continue;
10358
0
        group_found = true;
10359
0
        break;
10360
0
      }
10361
0
    }
10362
10363
0
    if (!group_found)
10364
0
      break;
10365
10366
0
    if (!len ||
10367
0
        len + RNR_TBTT_HEADER_LEN + tbtt_info_len > 255) {
10368
0
      len = RNR_HEADER_LEN;
10369
0
      total_len += RNR_HEADER_LEN;
10370
0
    }
10371
10372
0
    len += RNR_TBTT_HEADER_LEN;
10373
0
    total_len += RNR_TBTT_HEADER_LEN;
10374
10375
0
    for (i = 0; i < iface->num_bss; i++) {
10376
0
      u8 bss_op_class, bss_channel;
10377
10378
0
      if (tbtt_added[i])
10379
0
        continue;
10380
10381
0
      if (!hostapd_rnr_get_bss_info(
10382
0
            hapd, reporting_hapd, skip_profiles,
10383
0
            i, tbtt_info_len, mld_update,
10384
0
            &bss_op_class, &bss_channel, NULL))
10385
0
        continue;
10386
10387
0
      if (rnr_op_class != bss_op_class ||
10388
0
          rnr_channel != bss_channel)
10389
0
        continue;
10390
10391
0
      if (len + tbtt_info_len > 255 ||
10392
0
          tbtt_count >= RNR_TBTT_INFO_COUNT_MAX) {
10393
0
        group_pending = true;
10394
0
        break;
10395
0
      }
10396
10397
0
      len += tbtt_info_len;
10398
0
      total_len += tbtt_info_len;
10399
0
      tbtt_count++;
10400
0
      tbtt_added[i] = true;
10401
0
    }
10402
10403
0
    if (!tbtt_count) {
10404
0
      len -= RNR_TBTT_HEADER_LEN;
10405
0
      total_len -= RNR_TBTT_HEADER_LEN;
10406
0
      break;
10407
0
    }
10408
10409
0
    total_tbtt_count += tbtt_count;
10410
10411
0
    if (group_pending) {
10412
0
      have_pending_group = true;
10413
0
      pending_op_class = rnr_op_class;
10414
0
      pending_channel = rnr_channel;
10415
0
    } else {
10416
0
      have_pending_group = false;
10417
0
    }
10418
0
  }
10419
10420
  /* If building for co-location, re-build again but this time include
10421
   * ML TBTTs if the reporting AP is affiliated with an AP MLD.
10422
   */
10423
0
  if (!mld_update && tbtt_info_len == RNR_TBTT_INFO_LEN &&
10424
0
      reporting_ap_mld) {
10425
0
    tbtt_info_len = RNR_TBTT_INFO_MLD_LEN;
10426
0
    goto repeat_rnr_len;
10427
0
  }
10428
10429
0
  os_free(tbtt_added);
10430
10431
0
  if (!total_tbtt_count)
10432
0
    total_len = 0;
10433
0
  else
10434
0
    *current_len = len;
10435
10436
0
  return total_len;
10437
0
}
10438
10439
10440
enum colocation_mode {
10441
  NO_COLOCATED_6GHZ,
10442
  STANDALONE_6GHZ,
10443
  COLOCATED_6GHZ,
10444
  COLOCATED_LOWER_BAND,
10445
};
10446
10447
static enum colocation_mode get_colocation_mode(struct hostapd_data *hapd)
10448
0
{
10449
0
  u8 i;
10450
0
  bool is_6ghz = is_6ghz_op_class(hapd->iconf->op_class);
10451
10452
0
  if (!hapd->iface || !hapd->iface->interfaces)
10453
0
    return NO_COLOCATED_6GHZ;
10454
10455
0
  if (is_6ghz && hapd->iface->interfaces->count == 1)
10456
0
    return STANDALONE_6GHZ;
10457
10458
0
  for (i = 0; i < hapd->iface->interfaces->count; i++) {
10459
0
    struct hostapd_iface *iface;
10460
0
    bool is_colocated_6ghz;
10461
10462
0
    iface = hapd->iface->interfaces->iface[i];
10463
0
    if (iface == hapd->iface || !iface || !iface->conf)
10464
0
      continue;
10465
10466
0
    is_colocated_6ghz = is_6ghz_op_class(iface->conf->op_class);
10467
0
    if (!is_6ghz && is_colocated_6ghz)
10468
0
      return COLOCATED_LOWER_BAND;
10469
0
    if (is_6ghz && !is_colocated_6ghz)
10470
0
      return COLOCATED_6GHZ;
10471
0
  }
10472
10473
0
  if (is_6ghz)
10474
0
    return STANDALONE_6GHZ;
10475
10476
0
  return NO_COLOCATED_6GHZ;
10477
0
}
10478
10479
10480
static size_t hostapd_eid_rnr_colocation_len(struct hostapd_data *hapd,
10481
               size_t *current_len)
10482
0
{
10483
0
  struct hostapd_iface *iface;
10484
0
  size_t len = 0;
10485
0
  size_t i;
10486
10487
0
  if (!hapd->iface || !hapd->iface->interfaces)
10488
0
    return 0;
10489
10490
0
  for (i = 0; i < hapd->iface->interfaces->count; i++) {
10491
0
    iface = hapd->iface->interfaces->iface[i];
10492
10493
0
    if (!iface || iface == hapd->iface ||
10494
0
        iface->state != HAPD_IFACE_ENABLED ||
10495
0
        !is_6ghz_op_class(iface->conf->op_class))
10496
0
      continue;
10497
10498
0
    len += hostapd_eid_rnr_iface_len(iface->bss[0], hapd,
10499
0
             current_len, NULL, false);
10500
0
  }
10501
10502
0
  return len;
10503
0
}
10504
10505
10506
static size_t hostapd_eid_rnr_mlo_len(struct hostapd_data *hapd, u32 type,
10507
              struct mbssid_ie_profiles *skip_profiles,
10508
              size_t *current_len)
10509
0
{
10510
0
  size_t len = 0;
10511
0
#ifdef CONFIG_IEEE80211BE
10512
0
  struct hostapd_iface *iface;
10513
0
  size_t i;
10514
10515
0
  if (!hapd->iface || !hapd->iface->interfaces)
10516
0
    return 0;
10517
10518
  /* TODO: Allow for FILS/Action as well */
10519
0
  if (type != WLAN_FC_STYPE_BEACON && type != WLAN_FC_STYPE_PROBE_RESP)
10520
0
    return 0;
10521
10522
0
  for (i = 0; i < hapd->iface->interfaces->count; i++) {
10523
0
    iface = hapd->iface->interfaces->iface[i];
10524
10525
0
    if (!iface || iface == hapd->iface ||
10526
0
        hapd->iface->freq == iface->freq)
10527
0
      continue;
10528
10529
0
    len += hostapd_eid_rnr_iface_len(iface->bss[0], hapd,
10530
0
             current_len, skip_profiles,
10531
0
             true);
10532
0
  }
10533
0
#endif /* CONFIG_IEEE80211BE */
10534
10535
0
  return len;
10536
0
}
10537
10538
10539
size_t hostapd_eid_rnr_len(struct hostapd_data *hapd, u32 type,
10540
         bool include_mld_params)
10541
0
{
10542
0
  size_t total_len = 0, current_len = 0;
10543
0
  enum colocation_mode mode = get_colocation_mode(hapd);
10544
10545
0
  switch (type) {
10546
0
  case WLAN_FC_STYPE_BEACON:
10547
0
    if (hapd->conf->rnr)
10548
0
      total_len += hostapd_eid_nr_db_len(hapd, &current_len);
10549
    /* fallthrough */
10550
0
  case WLAN_FC_STYPE_PROBE_RESP:
10551
0
    if (mode == COLOCATED_LOWER_BAND)
10552
0
      total_len +=
10553
0
        hostapd_eid_rnr_colocation_len(hapd,
10554
0
                     &current_len);
10555
10556
0
    if (hapd->conf->rnr && hapd->iface->num_bss > 1 &&
10557
0
        !hapd->iconf->mbssid)
10558
0
      total_len += hostapd_eid_rnr_iface_len(hapd, hapd,
10559
0
                     &current_len,
10560
0
                     NULL, false);
10561
0
    break;
10562
0
  case WLAN_FC_STYPE_ACTION:
10563
0
    if (hapd->iface->num_bss > 1 && mode == STANDALONE_6GHZ)
10564
0
      total_len += hostapd_eid_rnr_iface_len(hapd, hapd,
10565
0
                     &current_len,
10566
0
                     NULL, false);
10567
0
    break;
10568
0
  }
10569
10570
  /* For EMA Beacons, MLD neighbor repoting is added as part of
10571
   * MBSSID RNR. */
10572
0
  if (include_mld_params &&
10573
0
      (type != WLAN_FC_STYPE_BEACON ||
10574
0
       hapd->iconf->mbssid != ENHANCED_MBSSID_ENABLED))
10575
0
    total_len += hostapd_eid_rnr_mlo_len(hapd, type, NULL,
10576
0
                 &current_len);
10577
10578
0
  return total_len;
10579
0
}
10580
10581
10582
static u8 * hostapd_eid_nr_db(struct hostapd_data *hapd, u8 *eid,
10583
            size_t *current_len)
10584
0
{
10585
0
  struct hostapd_neighbor_entry *nr;
10586
0
  size_t len = *current_len;
10587
0
  u8 *size_offset = (eid - len) + 1;
10588
10589
0
  dl_list_for_each(nr, &hapd->nr_db, struct hostapd_neighbor_entry,
10590
0
       list) {
10591
0
    if (!nr->nr || wpabuf_len(nr->nr) < 12)
10592
0
      continue;
10593
10594
0
    if (nr->short_ssid == hapd->conf->ssid.short_ssid)
10595
0
      continue;
10596
10597
    /* Start a new element */
10598
0
    if (!len ||
10599
0
        len + RNR_TBTT_HEADER_LEN + RNR_TBTT_INFO_LEN > 255) {
10600
0
      *eid++ = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
10601
0
      size_offset = eid++;
10602
0
      len = RNR_HEADER_LEN;
10603
0
    }
10604
10605
    /* TBTT Information Header subfield (2 octets) */
10606
0
    *eid++ = 0;
10607
    /* TBTT Information Length */
10608
0
    *eid++ = RNR_TBTT_INFO_LEN;
10609
    /* Operating Class */
10610
0
    *eid++ = wpabuf_head_u8(nr->nr)[10];
10611
    /* Channel Number */
10612
0
    *eid++ = wpabuf_head_u8(nr->nr)[11];
10613
0
    len += RNR_TBTT_HEADER_LEN;
10614
    /* TBTT Information Set */
10615
    /* TBTT Information field */
10616
    /* Neighbor AP TBTT Offset */
10617
0
    *eid++ = RNR_NEIGHBOR_AP_OFFSET_UNKNOWN;
10618
    /* BSSID */
10619
0
    os_memcpy(eid, nr->bssid, ETH_ALEN);
10620
0
    eid += ETH_ALEN;
10621
    /* Short SSID */
10622
0
    os_memcpy(eid, &nr->short_ssid, 4);
10623
0
    eid += 4;
10624
    /* BSS parameters */
10625
0
    *eid++ = nr->bss_parameters;
10626
    /* 20 MHz PSD */
10627
0
    *eid++ = RNR_20_MHZ_PSD_MAX_TXPOWER;
10628
0
    len += RNR_TBTT_INFO_LEN;
10629
0
    *size_offset = (eid - size_offset) - 1;
10630
0
  }
10631
10632
0
  *current_len = len;
10633
0
  return eid;
10634
0
}
10635
10636
10637
static bool hostapd_eid_rnr_bss(struct hostapd_data *hapd,
10638
        struct hostapd_data *reporting_hapd,
10639
        size_t i, u8 *tbtt_count, size_t *len,
10640
        u8 **pos, u8 **tbtt_count_pos, u8 tbtt_info_len,
10641
        u8 op_class, u8 channel,
10642
        u8 match_idx)
10643
0
{
10644
0
  struct hostapd_iface *iface = hapd->iface;
10645
0
  struct hostapd_data *bss = iface->bss[i];
10646
0
  u8 bss_param = 0;
10647
0
#ifdef CONFIG_IEEE80211BE
10648
0
  bool ap_mld = false;
10649
0
#endif /* CONFIG_IEEE80211BE */
10650
0
  u8 *eid = *pos;
10651
10652
0
  if (!bss || !bss->conf || bss == reporting_hapd)
10653
0
    return false;
10654
10655
0
#ifdef CONFIG_IEEE80211BE
10656
0
  ap_mld = !!bss->conf->mld_ap;
10657
0
#endif /* CONFIG_IEEE80211BE */
10658
10659
0
  if (*len + tbtt_info_len > 255 ||
10660
0
      *tbtt_count >= RNR_TBTT_INFO_COUNT_MAX)
10661
0
    return true;
10662
10663
0
  if (!(*tbtt_count)) {
10664
    /* Add neighbor report header info only if there is at least
10665
     * one TBTT info available. */
10666
0
    *tbtt_count_pos = eid++;
10667
0
    *eid++ = tbtt_info_len;
10668
0
    *eid++ = op_class;
10669
0
    *eid++ = channel;
10670
0
    *len += RNR_TBTT_HEADER_LEN;
10671
0
  }
10672
10673
0
  *eid++ = RNR_NEIGHBOR_AP_OFFSET_UNKNOWN;
10674
0
  os_memcpy(eid, bss->own_addr, ETH_ALEN);
10675
0
  eid += ETH_ALEN;
10676
0
  os_memcpy(eid, &bss->conf->ssid.short_ssid, 4);
10677
0
  eid += 4;
10678
0
  if (bss->conf->ssid.short_ssid == reporting_hapd->conf->ssid.short_ssid)
10679
0
    bss_param |= RNR_BSS_PARAM_SAME_SSID;
10680
10681
0
  if (iface->conf->mbssid != MBSSID_DISABLED && iface->num_bss > 1) {
10682
0
    bss_param |= RNR_BSS_PARAM_MULTIPLE_BSSID;
10683
0
    if (bss == hostapd_mbssid_get_tx_bss(hapd))
10684
0
      bss_param |= RNR_BSS_PARAM_TRANSMITTED_BSSID;
10685
0
  }
10686
10687
0
  if (is_6ghz_op_class(op_class) &&
10688
0
      bss->conf->unsol_bcast_probe_resp_interval)
10689
0
    bss_param |= RNR_BSS_PARAM_UNSOLIC_PROBE_RESP_ACTIVE;
10690
10691
0
  bss_param |= RNR_BSS_PARAM_CO_LOCATED;
10692
10693
0
  *eid++ = bss_param;
10694
0
  *eid++ = RNR_20_MHZ_PSD_MAX_TXPOWER;
10695
10696
0
#ifdef CONFIG_IEEE80211BE
10697
  /* Include the MLD parameters only when TBTT length is for ML RNR */
10698
0
  if (ap_mld && tbtt_info_len == RNR_TBTT_INFO_MLD_LEN) {
10699
0
    u8 param_ch = bss->eht_mld_bss_param_change;
10700
10701
    /* If BSS is not a partner of the reporting_hapd or
10702
     * it is one of the nontransmitted hapd,
10703
     *  a) MLD ID advertised shall be 255.
10704
     *  b) Link ID advertised shall be 15.
10705
     *  c) BPCC advertised shall be 255 */
10706
    /* MLD ID */
10707
0
    *eid++ = match_idx;
10708
    /* Link ID (Bit 3 to Bit 0)
10709
     * BPCC (Bit 4 to Bit 7) */
10710
0
    *eid++ = match_idx < 255 ?
10711
0
      bss->mld_link_id | ((param_ch & 0xF) << 4) :
10712
0
      (MAX_NUM_MLD_LINKS | 0xF0);
10713
    /* BPCC (Bit 3 to Bit 0) */
10714
0
    *eid = match_idx < 255 ? ((param_ch & 0xF0) >> 4) : 0x0F;
10715
#ifdef CONFIG_TESTING_OPTIONS
10716
    if (bss->conf->mld_indicate_disabled)
10717
      *eid |= RNR_TBTT_INFO_MLD_PARAM2_LINK_DISABLED;
10718
#endif /* CONFIG_TESTING_OPTIONS */
10719
0
    eid++;
10720
0
  }
10721
0
#endif /* CONFIG_IEEE80211BE */
10722
10723
0
  *len += tbtt_info_len;
10724
0
  (*tbtt_count)++;
10725
0
  *pos = eid;
10726
10727
0
  return false;
10728
0
}
10729
10730
10731
static u8 * hostapd_eid_rnr_iface(struct hostapd_data *hapd,
10732
          struct hostapd_data *reporting_hapd,
10733
          u8 *eid, size_t *current_len,
10734
          struct mbssid_ie_profiles *skip_profiles,
10735
          bool mld_update)
10736
0
{
10737
0
  struct hostapd_iface *iface = hapd->iface;
10738
0
  size_t i;
10739
0
  size_t len = *current_len;
10740
0
  u8 *eid_start = eid, *size_offset = (eid - len) + 1;
10741
0
  u8 *tbtt_count_pos = size_offset + 1;
10742
0
  u8 total_tbtt_count = 0;
10743
0
  u8 tbtt_info_len = mld_update ? RNR_TBTT_INFO_MLD_LEN :
10744
0
    RNR_TBTT_INFO_LEN;
10745
0
  bool reporting_ap_mld = false;
10746
0
  bool have_pending_group;
10747
0
  u8 pending_op_class = 0, pending_channel = 0;
10748
0
  bool *tbtt_added = NULL;
10749
10750
0
  if (!(iface->drv_flags & WPA_DRIVER_FLAGS_AP_CSA) || !iface->freq)
10751
0
    return eid;
10752
10753
0
#ifdef CONFIG_IEEE80211BE
10754
0
  reporting_ap_mld = !!reporting_hapd->conf->mld_ap;
10755
0
#endif /* CONFIG_IEEE80211BE */
10756
10757
0
repeat_rnr:
10758
0
  os_free(tbtt_added);
10759
0
  tbtt_added = os_zalloc(iface->num_bss);
10760
0
  if (!tbtt_added)
10761
0
    return eid;
10762
10763
0
  have_pending_group = false;
10764
0
  for (;;) {
10765
0
    u8 tbtt_count = 0;
10766
0
    bool group_found = false, group_pending = false;
10767
0
    u8 rnr_op_class = 0, rnr_channel = 0;
10768
10769
0
    if (have_pending_group) {
10770
0
      rnr_op_class = pending_op_class;
10771
0
      rnr_channel = pending_channel;
10772
0
      group_found = true;
10773
0
    }
10774
10775
0
    if (!group_found) {
10776
0
      for (i = 0; i < iface->num_bss; i++) {
10777
0
        if (tbtt_added[i])
10778
0
          continue;
10779
0
        if (!hostapd_rnr_get_bss_info(
10780
0
              hapd, reporting_hapd, skip_profiles,
10781
0
              i, tbtt_info_len, mld_update,
10782
0
              &rnr_op_class, &rnr_channel, NULL))
10783
0
          continue;
10784
0
        group_found = true;
10785
0
        break;
10786
0
      }
10787
0
    }
10788
10789
0
    if (!group_found)
10790
0
      break;
10791
10792
0
    if (!len ||
10793
0
        len + RNR_TBTT_HEADER_LEN + tbtt_info_len > 255) {
10794
0
      eid_start = eid;
10795
0
      *eid++ = WLAN_EID_REDUCED_NEIGHBOR_REPORT;
10796
0
      size_offset = eid++;
10797
0
      len = RNR_HEADER_LEN;
10798
0
    }
10799
10800
0
    for (i = 0; i < iface->num_bss; i++) {
10801
0
      u8 op_class, channel, match_idx = 255;
10802
10803
0
      if (tbtt_added[i])
10804
0
        continue;
10805
10806
0
      if (!hostapd_rnr_get_bss_info(
10807
0
            hapd, reporting_hapd, skip_profiles,
10808
0
            i, tbtt_info_len, mld_update,
10809
0
            &op_class, &channel, &match_idx))
10810
0
        continue;
10811
10812
0
      if (rnr_op_class != op_class ||
10813
0
          rnr_channel != channel)
10814
0
        continue;
10815
10816
0
      if (hostapd_eid_rnr_bss(hapd, reporting_hapd, i,
10817
0
            &tbtt_count, &len, &eid,
10818
0
            &tbtt_count_pos, tbtt_info_len,
10819
0
            op_class, channel,
10820
0
            match_idx)) {
10821
0
        group_pending = true;
10822
0
        break;
10823
0
      }
10824
10825
0
      tbtt_added[i] = true;
10826
0
    }
10827
10828
0
    if (tbtt_count) {
10829
0
      *tbtt_count_pos = RNR_TBTT_INFO_COUNT(tbtt_count - 1);
10830
0
      *size_offset = (eid - size_offset) - 1;
10831
0
    } else {
10832
0
      break;
10833
0
    }
10834
10835
0
    total_tbtt_count += tbtt_count;
10836
10837
0
    if (group_pending) {
10838
0
      have_pending_group = true;
10839
0
      pending_op_class = rnr_op_class;
10840
0
      pending_channel = rnr_channel;
10841
0
    } else {
10842
0
      have_pending_group = false;
10843
0
    }
10844
0
  }
10845
10846
  /* If building for co-location, re-build again but this time include
10847
   * ML TBTTs if the reporting AP is affiliated with an AP MLD.
10848
   */
10849
0
  if (!mld_update && tbtt_info_len == RNR_TBTT_INFO_LEN &&
10850
0
      reporting_ap_mld) {
10851
0
    tbtt_info_len = RNR_TBTT_INFO_MLD_LEN;
10852
0
    goto repeat_rnr;
10853
0
  }
10854
10855
0
  os_free(tbtt_added);
10856
10857
0
  if (!total_tbtt_count)
10858
0
    return eid_start;
10859
10860
0
  *current_len = len;
10861
0
  return eid;
10862
0
}
10863
10864
10865
static u8 * hostapd_eid_rnr_colocation(struct hostapd_data *hapd, u8 *eid,
10866
               size_t *current_len)
10867
0
{
10868
0
  struct hostapd_iface *iface;
10869
0
  size_t i;
10870
10871
0
  if (!hapd->iface || !hapd->iface->interfaces)
10872
0
    return eid;
10873
10874
0
  for (i = 0; i < hapd->iface->interfaces->count; i++) {
10875
0
    iface = hapd->iface->interfaces->iface[i];
10876
10877
0
    if (!iface || iface == hapd->iface ||
10878
0
        iface->state != HAPD_IFACE_ENABLED ||
10879
0
        !is_6ghz_op_class(iface->conf->op_class))
10880
0
      continue;
10881
10882
0
    eid = hostapd_eid_rnr_iface(iface->bss[0], hapd, eid,
10883
0
              current_len, NULL, false);
10884
0
  }
10885
10886
0
  return eid;
10887
0
}
10888
10889
10890
static u8 * hostapd_eid_rnr_mlo(struct hostapd_data *hapd, u32 type,
10891
        u8 *eid,
10892
        struct mbssid_ie_profiles *skip_profiles,
10893
        size_t *current_len)
10894
0
{
10895
0
#ifdef CONFIG_IEEE80211BE
10896
0
  struct hostapd_iface *iface;
10897
0
  size_t i;
10898
10899
0
  if (!hapd->iface || !hapd->iface->interfaces)
10900
0
    return eid;
10901
10902
  /* TODO: Allow for FILS/Action as well */
10903
0
  if (type != WLAN_FC_STYPE_BEACON && type != WLAN_FC_STYPE_PROBE_RESP)
10904
0
    return eid;
10905
10906
0
  for (i = 0; i < hapd->iface->interfaces->count; i++) {
10907
0
    iface = hapd->iface->interfaces->iface[i];
10908
10909
0
    if (!iface || iface == hapd->iface ||
10910
0
        hapd->iface->freq == iface->freq)
10911
0
      continue;
10912
10913
0
    eid = hostapd_eid_rnr_iface(iface->bss[0], hapd, eid,
10914
0
              current_len, skip_profiles, true);
10915
0
  }
10916
0
#endif /* CONFIG_IEEE80211BE */
10917
10918
0
  return eid;
10919
0
}
10920
10921
10922
u8 * hostapd_eid_rnr(struct hostapd_data *hapd, u8 *eid, u32 type,
10923
         bool include_mld_params)
10924
0
{
10925
0
  u8 *eid_start = eid;
10926
0
  size_t current_len = 0;
10927
0
  enum colocation_mode mode = get_colocation_mode(hapd);
10928
10929
0
  switch (type) {
10930
0
  case WLAN_FC_STYPE_BEACON:
10931
0
    if (hapd->conf->rnr)
10932
0
      eid = hostapd_eid_nr_db(hapd, eid, &current_len);
10933
    /* fallthrough */
10934
0
  case WLAN_FC_STYPE_PROBE_RESP:
10935
0
    if (mode == COLOCATED_LOWER_BAND)
10936
0
      eid = hostapd_eid_rnr_colocation(hapd, eid,
10937
0
               &current_len);
10938
10939
0
    if (hapd->conf->rnr && hapd->iface->num_bss > 1 &&
10940
0
        !hapd->iconf->mbssid)
10941
0
      eid = hostapd_eid_rnr_iface(hapd, hapd, eid,
10942
0
                &current_len, NULL, false);
10943
0
    break;
10944
0
  case WLAN_FC_STYPE_ACTION:
10945
0
    if (hapd->iface->num_bss > 1 && mode == STANDALONE_6GHZ)
10946
0
      eid = hostapd_eid_rnr_iface(hapd, hapd, eid,
10947
0
                &current_len, NULL, false);
10948
0
    break;
10949
0
  default:
10950
0
    return eid_start;
10951
0
  }
10952
10953
  /* For EMA Beacons, MLD neighbor repoting is added as part of
10954
   * MBSSID RNR. */
10955
0
  if (include_mld_params &&
10956
0
      (type != WLAN_FC_STYPE_BEACON ||
10957
0
       hapd->iconf->mbssid != ENHANCED_MBSSID_ENABLED))
10958
0
    eid = hostapd_eid_rnr_mlo(hapd, type, eid, NULL, &current_len);
10959
10960
0
  if (eid == eid_start + 2)
10961
0
    return eid_start;
10962
10963
0
  return eid;
10964
0
}
10965
10966
10967
static bool mbssid_known_bss(unsigned int i, const u8 *known_bss,
10968
           size_t known_bss_len)
10969
0
{
10970
0
  if (!known_bss || known_bss_len <= i / 8)
10971
0
    return false;
10972
0
  known_bss = &known_bss[i / 8];
10973
0
  return *known_bss & (u8) (BIT(i % 8));
10974
0
}
10975
10976
10977
static size_t hostapd_mbssid_ext_capa(struct hostapd_data *bss,
10978
              struct hostapd_data *tx_bss, u8 *buf)
10979
0
{
10980
0
  u8 ext_capa_tx[20], *ext_capa_tx_end, ext_capa[20], *ext_capa_end;
10981
0
  size_t ext_capa_len, ext_capa_tx_len;
10982
10983
0
  ext_capa_tx_end = hostapd_eid_ext_capab(tx_bss, ext_capa_tx,
10984
0
            true);
10985
0
  ext_capa_tx_len = ext_capa_tx_end - ext_capa_tx;
10986
0
  ext_capa_end = hostapd_eid_ext_capab(bss, ext_capa, true);
10987
0
  ext_capa_len = ext_capa_end - ext_capa;
10988
0
  if (ext_capa_tx_len != ext_capa_len ||
10989
0
      os_memcmp(ext_capa_tx, ext_capa, ext_capa_len) != 0) {
10990
0
    os_memcpy(buf, ext_capa, ext_capa_len);
10991
0
    return ext_capa_len;
10992
0
  }
10993
10994
0
  return 0;
10995
0
}
10996
10997
10998
static size_t hostapd_eid_mbssid_elem_len(struct hostapd_data *hapd,
10999
            u32 frame_type, size_t *bss_index,
11000
            const u8 *known_bss,
11001
            size_t known_bss_len)
11002
0
{
11003
0
  struct hostapd_data *tx_bss = hostapd_mbssid_get_tx_bss(hapd);
11004
0
  size_t len, i, tx_xrate_len;
11005
0
  u8 ext_capa[20], buf[100];
11006
11007
  /* Element ID: 1 octet
11008
   * Length: 1 octet
11009
   * MaxBSSID Indicator: 1 octet
11010
   * Optional Subelements: vatiable
11011
   *
11012
   * Total fixed length: 3 octets
11013
   *
11014
   * 1 octet in len for the MaxBSSID Indicator field.
11015
   */
11016
0
  len = 1;
11017
11018
0
  tx_xrate_len = hostapd_eid_ext_supp_rates(tx_bss, buf) - buf;
11019
11020
0
  for (i = *bss_index; i < hapd->iface->num_bss; i++) {
11021
0
    struct hostapd_data *bss = hapd->iface->bss[i];
11022
0
    const u8 *auth, *rsn = NULL, *rsnx = NULL;
11023
0
    size_t nontx_profile_len, auth_len, xrate_len;
11024
0
    u8 ie_count = 0;
11025
11026
0
    if (!bss || !bss->conf || !bss->started ||
11027
0
        mbssid_known_bss(i, known_bss, known_bss_len))
11028
0
      continue;
11029
11030
    /*
11031
     * Sublement ID: 1 octet
11032
     * Length: 1 octet
11033
     * Nontransmitted capabilities: 4 octets
11034
     * SSID element: 2 + variable
11035
     * Multiple BSSID Index Element: 3 octets (+2 octets in beacons)
11036
     * Fixed length = 1 + 1 + 4 + 2 + 3 = 11
11037
     */
11038
0
    nontx_profile_len = 11 + bss->conf->ssid.ssid_len;
11039
11040
0
    if (frame_type == WLAN_FC_STYPE_BEACON)
11041
0
      nontx_profile_len += 2;
11042
11043
0
    auth = wpa_auth_get_wpa_ie(bss->wpa_auth, &auth_len);
11044
0
    if (auth) {
11045
0
      rsn = get_ie(auth, auth_len, WLAN_EID_RSN);
11046
0
      if (rsn)
11047
0
        nontx_profile_len += 2 + rsn[1];
11048
11049
0
      rsnx = get_ie(auth, auth_len, WLAN_EID_RSNX);
11050
0
      if (rsnx)
11051
0
        nontx_profile_len += 2 + rsnx[1];
11052
0
    }
11053
11054
0
    nontx_profile_len += hostapd_mbssid_ext_capa(bss, tx_bss,
11055
0
                   ext_capa);
11056
11057
0
    if (!rsn && hostapd_wpa_ie(tx_bss, WLAN_EID_RSN))
11058
0
      ie_count++;
11059
0
    if (!rsnx && hostapd_wpa_ie(tx_bss, WLAN_EID_RSNX))
11060
0
      ie_count++;
11061
11062
0
    xrate_len = hostapd_eid_ext_supp_rates(bss, buf) - buf;
11063
11064
0
    if (xrate_len)
11065
0
      nontx_profile_len += xrate_len;
11066
0
    else if (tx_xrate_len)
11067
0
      ie_count++;
11068
11069
0
#ifdef CONFIG_IEEE80211BE
11070
    /* For ML Probe Response frame, the solicited hapd's MLE will
11071
     * be in the frame body */
11072
0
    if (bss->conf->mld_ap &&
11073
0
        (bss != hapd || frame_type != WLAN_FC_STYPE_PROBE_RESP))
11074
0
      nontx_profile_len += hostapd_eid_eht_basic_ml_len(
11075
0
        bss, NULL, true, false);
11076
0
#endif /* CONFIG_IEEE80211BE */
11077
11078
0
    if (ie_count)
11079
0
      nontx_profile_len += 4 + ie_count + 1;
11080
11081
0
    if (len + nontx_profile_len > 255)
11082
0
      break;
11083
11084
0
    len += nontx_profile_len;
11085
0
  }
11086
11087
0
  *bss_index = i;
11088
11089
  /* Add 2 octets to get the full size of the element */
11090
0
  return len + 2;
11091
0
}
11092
11093
11094
size_t hostapd_eid_mbssid_len(struct hostapd_data *hapd_probed, u32 frame_type,
11095
            u8 *elem_count, const u8 *known_bss,
11096
            size_t known_bss_len, size_t *rnr_len)
11097
0
{
11098
0
  struct hostapd_data *hapd = hostapd_mbssid_get_tx_bss(hapd_probed);
11099
0
  size_t len = 0, bss_index = 1;
11100
11101
0
  if (!hapd->iconf->mbssid ||
11102
0
      (frame_type != WLAN_FC_STYPE_BEACON &&
11103
0
       frame_type != WLAN_FC_STYPE_PROBE_RESP))
11104
0
    return 0;
11105
11106
  /*
11107
   * Include the Multiple BSSID element whenever MBSSID is enabled. The
11108
   * element may include zero or more nontransmitted BSSID profiles.
11109
   */
11110
0
  if (hapd->iface->num_bss == 1) {
11111
0
    if (frame_type == WLAN_FC_STYPE_BEACON) {
11112
0
      if (!elem_count) {
11113
0
        wpa_printf(MSG_INFO,
11114
0
             "MBSSID: Insufficient data for Beacon frames");
11115
0
        return 0;
11116
0
      }
11117
0
      *elem_count = 1;
11118
0
    }
11119
0
    return 3;
11120
0
  }
11121
11122
0
  if (frame_type == WLAN_FC_STYPE_BEACON) {
11123
0
    if (!elem_count) {
11124
0
      wpa_printf(MSG_INFO,
11125
0
           "MBSSID: Insufficient data for Beacon frames");
11126
0
      return 0;
11127
0
    }
11128
0
    *elem_count = 0;
11129
0
  }
11130
11131
0
  while (bss_index < hapd->iface->num_bss) {
11132
0
    size_t rnr_count = bss_index;
11133
11134
0
    len += hostapd_eid_mbssid_elem_len(hapd_probed, frame_type,
11135
0
               &bss_index, known_bss,
11136
0
               known_bss_len);
11137
11138
0
    if (frame_type == WLAN_FC_STYPE_BEACON)
11139
0
      *elem_count += 1;
11140
0
    if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED && rnr_len) {
11141
0
      size_t rnr_cur_len = 0;
11142
0
      struct mbssid_ie_profiles skip_profiles = {
11143
0
        rnr_count, bss_index
11144
0
      };
11145
11146
0
      *rnr_len += hostapd_eid_rnr_iface_len(
11147
0
        hapd, hostapd_mbssid_get_tx_bss(hapd),
11148
0
        &rnr_cur_len, &skip_profiles, false);
11149
11150
0
      *rnr_len += hostapd_eid_rnr_mlo_len(
11151
0
        hostapd_mbssid_get_tx_bss(hapd), frame_type,
11152
0
        &skip_profiles, &rnr_cur_len);
11153
0
    }
11154
0
  }
11155
11156
0
  if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED && rnr_len)
11157
0
    *rnr_len += hostapd_eid_rnr_len(hapd, frame_type, false);
11158
11159
0
  return len;
11160
0
}
11161
11162
11163
static u8 * hostapd_eid_mbssid_elem(struct hostapd_data *hapd, u8 *eid, u8 *end,
11164
            u32 frame_type, u8 max_bssid_indicator,
11165
            size_t *bss_index, u8 elem_count,
11166
            const u8 *known_bss, size_t known_bss_len)
11167
0
{
11168
0
  struct hostapd_data *tx_bss = hostapd_mbssid_get_tx_bss(hapd);
11169
0
  size_t i, tx_xrate_len;
11170
0
  u8 *eid_len_offset, *max_bssid_indicator_offset;
11171
0
  u8 buf[100];
11172
11173
0
  *eid++ = WLAN_EID_MULTIPLE_BSSID;
11174
0
  eid_len_offset = eid++;
11175
0
  max_bssid_indicator_offset = eid++;
11176
11177
0
  tx_xrate_len = hostapd_eid_ext_supp_rates(tx_bss, buf) - buf;
11178
11179
0
  for (i = *bss_index; i < hapd->iface->num_bss; i++) {
11180
0
    struct hostapd_data *bss = hapd->iface->bss[i];
11181
0
    struct hostapd_bss_config *conf;
11182
0
    struct hostapd_bss_config *tx_conf = tx_bss->conf;
11183
0
    u8 *eid_len_pos, *nontx_bss_start = eid;
11184
0
    const u8 *auth, *rsn = NULL, *rsnx = NULL;
11185
0
    u8 ie_count = 0, non_inherit_ie[3];
11186
0
    size_t auth_len = 0, xrate_len;
11187
0
    u16 capab_info;
11188
0
    u8 mbssindex = i;
11189
11190
0
    if (!bss || !bss->conf || !bss->started ||
11191
0
        mbssid_known_bss(i, known_bss, known_bss_len))
11192
0
      continue;
11193
0
    conf = bss->conf;
11194
11195
0
    *eid++ = WLAN_MBSSID_SUBELEMENT_NONTRANSMITTED_BSSID_PROFILE;
11196
0
    eid_len_pos = eid++;
11197
11198
0
    capab_info = hostapd_own_capab_info(bss);
11199
0
    *eid++ = WLAN_EID_NONTRANSMITTED_BSSID_CAPA;
11200
0
    *eid++ = sizeof(capab_info);
11201
0
    WPA_PUT_LE16(eid, capab_info);
11202
0
    eid += sizeof(capab_info);
11203
11204
0
    *eid++ = WLAN_EID_SSID;
11205
0
    *eid++ = conf->ssid.ssid_len;
11206
0
    os_memcpy(eid, conf->ssid.ssid, conf->ssid.ssid_len);
11207
0
    eid += conf->ssid.ssid_len;
11208
11209
0
    if (conf->mbssid_index &&
11210
0
        conf->mbssid_index > tx_conf->mbssid_index)
11211
0
      mbssindex = conf->mbssid_index - tx_conf->mbssid_index;
11212
11213
0
    *eid++ = WLAN_EID_MULTIPLE_BSSID_INDEX;
11214
0
    if (frame_type == WLAN_FC_STYPE_BEACON) {
11215
0
      *eid++ = 3;
11216
0
      *eid++ = mbssindex; /* BSSID Index */
11217
0
      if (hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
11218
0
          (conf->dtim_period % elem_count))
11219
0
        conf->dtim_period = elem_count;
11220
0
      *eid++ = conf->dtim_period;
11221
      /* The driver is expected to update the DTIM Count
11222
       * field for each BSS that corresponds to a
11223
       * nontransmitted BSSID. The value is initialized to
11224
       * 0 here so that the DTIM count would be somewhat
11225
       * functional even if the driver were not to update
11226
       * this. */
11227
0
      *eid++ = 0; /* DTIM Count */
11228
0
    } else {
11229
      /* Probe Request frame does not include DTIM Period and
11230
       * DTIM Count fields. */
11231
0
      *eid++ = 1;
11232
0
      *eid++ = mbssindex; /* BSSID Index */
11233
0
    }
11234
11235
0
    auth = wpa_auth_get_wpa_ie(bss->wpa_auth, &auth_len);
11236
0
    if (auth) {
11237
0
      rsn = get_ie(auth, auth_len, WLAN_EID_RSN);
11238
0
      if (rsn) {
11239
0
        os_memcpy(eid, rsn, 2 + rsn[1]);
11240
0
        eid += 2 + rsn[1];
11241
0
      }
11242
11243
0
      rsnx = get_ie(auth, auth_len, WLAN_EID_RSNX);
11244
0
      if (rsnx) {
11245
0
        os_memcpy(eid, rsnx, 2 + rsnx[1]);
11246
0
        eid += 2 + rsnx[1];
11247
0
      }
11248
0
    }
11249
11250
0
    eid += hostapd_mbssid_ext_capa(bss, tx_bss, eid);
11251
0
    xrate_len = hostapd_eid_ext_supp_rates(bss, eid) - eid;
11252
0
    eid += xrate_len;
11253
11254
    /* List of Element ID values in increasing order */
11255
0
    if (!rsn && hostapd_wpa_ie(tx_bss, WLAN_EID_RSN))
11256
0
      non_inherit_ie[ie_count++] = WLAN_EID_RSN;
11257
0
    if (tx_xrate_len && !xrate_len)
11258
0
      non_inherit_ie[ie_count++] = WLAN_EID_EXT_SUPP_RATES;
11259
0
    if (!rsnx && hostapd_wpa_ie(tx_bss, WLAN_EID_RSNX))
11260
0
      non_inherit_ie[ie_count++] = WLAN_EID_RSNX;
11261
0
#ifdef CONFIG_IEEE80211BE
11262
    /* For ML Probe Response frame, the solicited hapd's MLE will
11263
     * be in the frame body */
11264
0
    if (bss->conf->mld_ap &&
11265
0
        (bss != hapd || frame_type != WLAN_FC_STYPE_PROBE_RESP))
11266
0
      eid = hostapd_eid_eht_basic_ml_common(bss, eid, NULL,
11267
0
                    true, false);
11268
0
#endif /* CONFIG_IEEE80211BE */
11269
0
    if (ie_count) {
11270
0
      *eid++ = WLAN_EID_EXTENSION;
11271
0
      *eid++ = 2 + ie_count + 1;
11272
0
      *eid++ = WLAN_EID_EXT_NON_INHERITANCE;
11273
0
      *eid++ = ie_count;
11274
0
      os_memcpy(eid, non_inherit_ie, ie_count);
11275
0
      eid += ie_count;
11276
0
      *eid++ = 0; /* No Element ID Extension List */
11277
0
    }
11278
11279
0
    *eid_len_pos = (eid - eid_len_pos) - 1;
11280
11281
0
    if (((eid - eid_len_offset) - 1) > 255) {
11282
0
      eid = nontx_bss_start;
11283
0
      break;
11284
0
    }
11285
0
  }
11286
11287
0
  *bss_index = i;
11288
0
  *max_bssid_indicator_offset = max_bssid_indicator;
11289
0
  if (*max_bssid_indicator_offset < 1)
11290
0
    *max_bssid_indicator_offset = 1;
11291
0
  *eid_len_offset = (eid - eid_len_offset) - 1;
11292
0
  return eid;
11293
0
}
11294
11295
11296
u8 * hostapd_eid_mbssid(struct hostapd_data *hapd_probed, u8 *eid, u8 *end,
11297
      unsigned int frame_stype, u8 elem_count,
11298
      u8 **elem_offset,
11299
      const u8 *known_bss, size_t known_bss_len, u8 *rnr_eid,
11300
      u8 *rnr_count, u8 **rnr_offset, size_t rnr_len)
11301
0
{
11302
0
  struct hostapd_data *hapd = hostapd_mbssid_get_tx_bss(hapd_probed);
11303
0
  size_t bss_index = 1, cur_len = 0;
11304
0
  u8 elem_index = 0, *rnr_start_eid = rnr_eid;
11305
0
  bool add_rnr;
11306
11307
0
  if (!hapd->iconf->mbssid ||
11308
0
      (frame_stype != WLAN_FC_STYPE_BEACON &&
11309
0
       frame_stype != WLAN_FC_STYPE_PROBE_RESP))
11310
0
    return eid;
11311
11312
0
  if (frame_stype == WLAN_FC_STYPE_BEACON && !elem_offset) {
11313
0
    wpa_printf(MSG_INFO,
11314
0
         "MBSSID: Insufficient data for Beacon frames");
11315
0
    return eid;
11316
0
  }
11317
11318
0
  add_rnr = hapd->iconf->mbssid == ENHANCED_MBSSID_ENABLED &&
11319
0
    frame_stype == WLAN_FC_STYPE_BEACON &&
11320
0
    rnr_eid && rnr_count && rnr_offset && rnr_len;
11321
11322
  /*
11323
   * Include the Multiple BSSID element whenever MBSSID is enabled. The
11324
   * element may include zero or more nontransmitted BSSID profiles.
11325
   */
11326
0
  if (hapd->iface->num_bss == 1) {
11327
0
    if (frame_stype == WLAN_FC_STYPE_BEACON)
11328
0
      elem_offset[0] = eid;
11329
11330
0
    return hostapd_eid_mbssid_elem(
11331
0
      hapd_probed, eid, end, frame_stype,
11332
0
      hostapd_max_bssid_indicator(hapd),
11333
0
      &bss_index, elem_count, known_bss, known_bss_len);
11334
0
  }
11335
11336
0
  while (bss_index < hapd->iface->num_bss) {
11337
0
    unsigned int rnr_start_count = bss_index;
11338
11339
0
    if (frame_stype == WLAN_FC_STYPE_BEACON) {
11340
0
      if (elem_index == elem_count) {
11341
0
        wpa_printf(MSG_WARNING,
11342
0
             "MBSSID: Larger number of elements than there is room in the provided array");
11343
0
        break;
11344
0
      }
11345
11346
0
      elem_offset[elem_index] = eid;
11347
0
      elem_index = elem_index + 1;
11348
0
    }
11349
0
    eid = hostapd_eid_mbssid_elem(hapd_probed, eid, end,
11350
0
                frame_stype,
11351
0
                hostapd_max_bssid_indicator(hapd),
11352
0
                &bss_index, elem_count,
11353
0
                known_bss, known_bss_len);
11354
11355
0
    if (add_rnr) {
11356
0
      struct mbssid_ie_profiles skip_profiles = {
11357
0
        rnr_start_count, bss_index
11358
0
      };
11359
11360
0
      rnr_offset[*rnr_count] = rnr_eid;
11361
0
      *rnr_count = *rnr_count + 1;
11362
0
      cur_len = 0;
11363
0
      rnr_eid = hostapd_eid_rnr_iface(
11364
0
        hapd, hostapd_mbssid_get_tx_bss(hapd),
11365
0
        rnr_eid, &cur_len, &skip_profiles, false);
11366
0
      rnr_eid = hostapd_eid_rnr_mlo(
11367
0
        hostapd_mbssid_get_tx_bss(hapd), frame_stype,
11368
0
        rnr_eid, &skip_profiles, &cur_len);
11369
0
    }
11370
0
  }
11371
11372
0
  if (add_rnr && (size_t) (rnr_eid - rnr_start_eid) < rnr_len) {
11373
0
    rnr_offset[*rnr_count] = rnr_eid;
11374
0
    *rnr_count = *rnr_count + 1;
11375
0
    cur_len = 0;
11376
11377
0
    if (hapd->conf->rnr)
11378
0
      rnr_eid = hostapd_eid_nr_db(hapd, rnr_eid, &cur_len);
11379
0
    if (get_colocation_mode(hapd) == COLOCATED_LOWER_BAND)
11380
0
      rnr_eid = hostapd_eid_rnr_colocation(hapd, rnr_eid,
11381
0
                   &cur_len);
11382
0
  }
11383
11384
0
  return eid;
11385
0
}
11386
11387
#endif /* CONFIG_NATIVE_WINDOWS */