1 /*
2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/list.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/sae.h"
16 #include "common/wpa_ctrl.h"
17 #include "common/ptksa_cache.h"
18 #include "crypto/sha1.h"
19 #include "eapol_auth/eapol_auth_sm.h"
20 #include "eapol_auth/eapol_auth_sm_i.h"
21 #include "eap_server/eap.h"
22 #include "l2_packet/l2_packet.h"
23 #include "eth_p_oui.h"
24 #include "hostapd.h"
25 #include "ieee802_1x.h"
26 #include "preauth_auth.h"
27 #include "sta_info.h"
28 #include "tkip_countermeasures.h"
29 #include "ap_drv_ops.h"
30 #include "ap_config.h"
31 #include "ieee802_11.h"
32 #include "pmksa_cache_auth.h"
33 #include "wpa_auth.h"
34 #include "wpa_auth_glue.h"
35
36
hostapd_wpa_auth_conf(struct hostapd_bss_config * conf,struct hostapd_config * iconf,struct wpa_auth_config * wconf)37 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
38 struct hostapd_config *iconf,
39 struct wpa_auth_config *wconf)
40 {
41 int sae_pw_id;
42
43 os_memset(wconf, 0, sizeof(*wconf));
44 wconf->wpa = conf->wpa;
45 wconf->extended_key_id = conf->extended_key_id;
46 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
47 wconf->wpa_pairwise = conf->wpa_pairwise;
48 wconf->wpa_group = conf->wpa_group;
49 wconf->wpa_group_rekey = conf->wpa_group_rekey;
50 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
51 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
52 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
53 wconf->wpa_group_update_count = conf->wpa_group_update_count;
54 wconf->wpa_disable_eapol_key_retries =
55 conf->wpa_disable_eapol_key_retries;
56 wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
57 wconf->rsn_pairwise = conf->rsn_pairwise;
58 wconf->rsn_preauth = conf->rsn_preauth;
59 wconf->eapol_version = conf->eapol_version;
60 #ifdef CONFIG_MACSEC
61 if (wconf->eapol_version > 2)
62 wconf->eapol_version = 2;
63 #endif /* CONFIG_MACSEC */
64 wconf->wmm_enabled = conf->wmm_enabled;
65 wconf->wmm_uapsd = conf->wmm_uapsd;
66 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
67 #ifdef CONFIG_OCV
68 wconf->ocv = conf->ocv;
69 #endif /* CONFIG_OCV */
70 wconf->okc = conf->okc;
71 wconf->ieee80211w = conf->ieee80211w;
72 wconf->beacon_prot = conf->beacon_prot;
73 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
74 wconf->sae_require_mfp = conf->sae_require_mfp;
75 #ifdef CONFIG_IEEE80211R_AP
76 wconf->ssid_len = conf->ssid.ssid_len;
77 if (wconf->ssid_len > SSID_MAX_LEN)
78 wconf->ssid_len = SSID_MAX_LEN;
79 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
80 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
81 MOBILITY_DOMAIN_ID_LEN);
82 if (conf->nas_identifier &&
83 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
84 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
85 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
86 wconf->r0_key_holder_len);
87 }
88 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
89 wconf->r0_key_lifetime = conf->r0_key_lifetime;
90 wconf->r1_max_key_lifetime = conf->r1_max_key_lifetime;
91 wconf->reassociation_deadline = conf->reassociation_deadline;
92 wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
93 wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
94 wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
95 wconf->rkh_pull_retries = conf->rkh_pull_retries;
96 wconf->r0kh_list = &conf->r0kh_list;
97 wconf->r1kh_list = &conf->r1kh_list;
98 wconf->pmk_r1_push = conf->pmk_r1_push;
99 wconf->ft_over_ds = conf->ft_over_ds;
100 wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
101 #endif /* CONFIG_IEEE80211R_AP */
102 #ifdef CONFIG_HS20
103 wconf->disable_gtk = conf->disable_dgaf;
104 if (conf->osen) {
105 wconf->disable_gtk = 1;
106 wconf->wpa = WPA_PROTO_OSEN;
107 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
108 wconf->wpa_pairwise = 0;
109 wconf->wpa_group = WPA_CIPHER_CCMP;
110 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
111 wconf->rsn_preauth = 0;
112 wconf->disable_pmksa_caching = 1;
113 wconf->ieee80211w = 1;
114 }
115 #endif /* CONFIG_HS20 */
116 #ifdef CONFIG_TESTING_OPTIONS
117 wconf->corrupt_gtk_rekey_mic_probability =
118 iconf->corrupt_gtk_rekey_mic_probability;
119 if (conf->own_ie_override &&
120 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
121 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
122 os_memcpy(wconf->own_ie_override,
123 wpabuf_head(conf->own_ie_override),
124 wconf->own_ie_override_len);
125 }
126 if (conf->rsne_override_eapol &&
127 wpabuf_len(conf->rsne_override_eapol) <= MAX_OWN_IE_OVERRIDE) {
128 wconf->rsne_override_eapol_set = 1;
129 wconf->rsne_override_eapol_len =
130 wpabuf_len(conf->rsne_override_eapol);
131 os_memcpy(wconf->rsne_override_eapol,
132 wpabuf_head(conf->rsne_override_eapol),
133 wconf->rsne_override_eapol_len);
134 }
135 if (conf->rsnxe_override_eapol &&
136 wpabuf_len(conf->rsnxe_override_eapol) <= MAX_OWN_IE_OVERRIDE) {
137 wconf->rsnxe_override_eapol_set = 1;
138 wconf->rsnxe_override_eapol_len =
139 wpabuf_len(conf->rsnxe_override_eapol);
140 os_memcpy(wconf->rsnxe_override_eapol,
141 wpabuf_head(conf->rsnxe_override_eapol),
142 wconf->rsnxe_override_eapol_len);
143 }
144 if (conf->rsne_override_ft &&
145 wpabuf_len(conf->rsne_override_ft) <= MAX_OWN_IE_OVERRIDE) {
146 wconf->rsne_override_ft_set = 1;
147 wconf->rsne_override_ft_len =
148 wpabuf_len(conf->rsne_override_ft);
149 os_memcpy(wconf->rsne_override_ft,
150 wpabuf_head(conf->rsne_override_ft),
151 wconf->rsne_override_ft_len);
152 }
153 if (conf->rsnxe_override_ft &&
154 wpabuf_len(conf->rsnxe_override_ft) <= MAX_OWN_IE_OVERRIDE) {
155 wconf->rsnxe_override_ft_set = 1;
156 wconf->rsnxe_override_ft_len =
157 wpabuf_len(conf->rsnxe_override_ft);
158 os_memcpy(wconf->rsnxe_override_ft,
159 wpabuf_head(conf->rsnxe_override_ft),
160 wconf->rsnxe_override_ft_len);
161 }
162 if (conf->gtk_rsc_override &&
163 wpabuf_len(conf->gtk_rsc_override) > 0 &&
164 wpabuf_len(conf->gtk_rsc_override) <= WPA_KEY_RSC_LEN) {
165 os_memcpy(wconf->gtk_rsc_override,
166 wpabuf_head(conf->gtk_rsc_override),
167 wpabuf_len(conf->gtk_rsc_override));
168 wconf->gtk_rsc_override_set = 1;
169 }
170 if (conf->igtk_rsc_override &&
171 wpabuf_len(conf->igtk_rsc_override) > 0 &&
172 wpabuf_len(conf->igtk_rsc_override) <= WPA_KEY_RSC_LEN) {
173 os_memcpy(wconf->igtk_rsc_override,
174 wpabuf_head(conf->igtk_rsc_override),
175 wpabuf_len(conf->igtk_rsc_override));
176 wconf->igtk_rsc_override_set = 1;
177 }
178 wconf->ft_rsnxe_used = conf->ft_rsnxe_used;
179 wconf->oci_freq_override_eapol_m3 = conf->oci_freq_override_eapol_m3;
180 wconf->oci_freq_override_eapol_g1 = conf->oci_freq_override_eapol_g1;
181 wconf->oci_freq_override_ft_assoc = conf->oci_freq_override_ft_assoc;
182 wconf->oci_freq_override_fils_assoc =
183 conf->oci_freq_override_fils_assoc;
184 wconf->skip_send_eapol = iconf->skip_send_eapol;
185 wconf->enable_eapol_large_timeout = iconf->enable_eapol_large_timeout;
186 #endif /* CONFIG_TESTING_OPTIONS */
187 #ifdef CONFIG_P2P
188 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
189 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
190 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
191 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
192 #endif /* CONFIG_P2P */
193 #ifdef CONFIG_FILS
194 wconf->fils_cache_id_set = conf->fils_cache_id_set;
195 os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
196 FILS_CACHE_ID_LEN);
197 #endif /* CONFIG_FILS */
198 wconf->sae_pwe = conf->sae_pwe;
199 sae_pw_id = hostapd_sae_pw_id_in_use(conf);
200 if (sae_pw_id == 2 && wconf->sae_pwe != 3)
201 wconf->sae_pwe = 1;
202 else if (sae_pw_id == 1 && wconf->sae_pwe == 0)
203 wconf->sae_pwe = 2;
204 #ifdef CONFIG_SAE_PK
205 wconf->sae_pk = hostapd_sae_pk_in_use(conf);
206 #endif /* CONFIG_SAE_PK */
207 #ifdef CONFIG_OWE
208 wconf->owe_ptk_workaround = conf->owe_ptk_workaround;
209 #endif /* CONFIG_OWE */
210 wconf->transition_disable = conf->transition_disable;
211 #ifdef CONFIG_DPP2
212 wconf->dpp_pfs = conf->dpp_pfs;
213 #endif /* CONFIG_DPP2 */
214 #ifdef CONFIG_PASN
215 #ifdef CONFIG_TESTING_OPTIONS
216 wconf->force_kdk_derivation = conf->force_kdk_derivation;
217 #endif /* CONFIG_TESTING_OPTIONS */
218 #endif /* CONFIG_PASN */
219 }
220
221
hostapd_wpa_auth_logger(void * ctx,const u8 * addr,logger_level level,const char * txt)222 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
223 logger_level level, const char *txt)
224 {
225 #ifndef CONFIG_NO_HOSTAPD_LOGGER
226 struct hostapd_data *hapd = ctx;
227 int hlevel;
228
229 switch (level) {
230 case LOGGER_WARNING:
231 hlevel = HOSTAPD_LEVEL_WARNING;
232 break;
233 case LOGGER_INFO:
234 hlevel = HOSTAPD_LEVEL_INFO;
235 break;
236 case LOGGER_DEBUG:
237 default:
238 hlevel = HOSTAPD_LEVEL_DEBUG;
239 break;
240 }
241
242 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
243 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
244 }
245
246
hostapd_wpa_auth_disconnect(void * ctx,const u8 * addr,u16 reason)247 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
248 u16 reason)
249 {
250 struct hostapd_data *hapd = ctx;
251 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
252 "STA " MACSTR " reason %d",
253 __func__, MAC2STR(addr), reason);
254 ap_sta_disconnect(hapd, NULL, addr, reason);
255 }
256
257
hostapd_wpa_auth_mic_failure_report(void * ctx,const u8 * addr)258 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
259 {
260 struct hostapd_data *hapd = ctx;
261 return michael_mic_failure(hapd, addr, 0);
262 }
263
264
hostapd_wpa_auth_psk_failure_report(void * ctx,const u8 * addr)265 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
266 {
267 struct hostapd_data *hapd = ctx;
268 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
269 MAC2STR(addr));
270 }
271
272
hostapd_wpa_auth_set_eapol(void * ctx,const u8 * addr,wpa_eapol_variable var,int value)273 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
274 wpa_eapol_variable var, int value)
275 {
276 struct hostapd_data *hapd = ctx;
277 struct sta_info *sta = ap_get_sta(hapd, addr);
278 if (sta == NULL)
279 return;
280 switch (var) {
281 case WPA_EAPOL_portEnabled:
282 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
283 break;
284 case WPA_EAPOL_portValid:
285 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
286 break;
287 case WPA_EAPOL_authorized:
288 ieee802_1x_set_sta_authorized(hapd, sta, value);
289 break;
290 case WPA_EAPOL_portControl_Auto:
291 if (sta->eapol_sm)
292 sta->eapol_sm->portControl = Auto;
293 break;
294 case WPA_EAPOL_keyRun:
295 if (sta->eapol_sm)
296 sta->eapol_sm->keyRun = value;
297 break;
298 case WPA_EAPOL_keyAvailable:
299 if (sta->eapol_sm)
300 sta->eapol_sm->eap_if->eapKeyAvailable = value;
301 break;
302 case WPA_EAPOL_keyDone:
303 if (sta->eapol_sm)
304 sta->eapol_sm->keyDone = value;
305 break;
306 case WPA_EAPOL_inc_EapolFramesTx:
307 if (sta->eapol_sm)
308 sta->eapol_sm->dot1xAuthEapolFramesTx++;
309 break;
310 }
311 }
312
313
hostapd_wpa_auth_get_eapol(void * ctx,const u8 * addr,wpa_eapol_variable var)314 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
315 wpa_eapol_variable var)
316 {
317 struct hostapd_data *hapd = ctx;
318 struct sta_info *sta = ap_get_sta(hapd, addr);
319 if (sta == NULL || sta->eapol_sm == NULL)
320 return -1;
321 switch (var) {
322 case WPA_EAPOL_keyRun:
323 return sta->eapol_sm->keyRun;
324 case WPA_EAPOL_keyAvailable:
325 return sta->eapol_sm->eap_if->eapKeyAvailable;
326 default:
327 return -1;
328 }
329 }
330
331
hostapd_wpa_auth_get_psk(void * ctx,const u8 * addr,const u8 * p2p_dev_addr,const u8 * prev_psk,size_t * psk_len,int * vlan_id)332 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
333 const u8 *p2p_dev_addr,
334 const u8 *prev_psk, size_t *psk_len,
335 int *vlan_id)
336 {
337 struct hostapd_data *hapd = ctx;
338 struct sta_info *sta = ap_get_sta(hapd, addr);
339 const u8 *psk;
340
341 if (vlan_id)
342 *vlan_id = 0;
343 if (psk_len)
344 *psk_len = PMK_LEN;
345
346 #ifdef CONFIG_SAE
347 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
348 if (!sta->sae || prev_psk)
349 return NULL;
350 return sta->sae->pmk;
351 }
352 if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
353 wpa_printf(MSG_DEBUG,
354 "No PSK for STA trying to use SAE with PMKSA caching");
355 return NULL;
356 }
357 #endif /* CONFIG_SAE */
358
359 #ifdef CONFIG_OWE
360 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
361 sta && sta->owe_pmk) {
362 if (psk_len)
363 *psk_len = sta->owe_pmk_len;
364 return sta->owe_pmk;
365 }
366 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
367 struct rsn_pmksa_cache_entry *sa;
368
369 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
370 if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
371 if (psk_len)
372 *psk_len = sa->pmk_len;
373 return sa->pmk;
374 }
375 }
376 #endif /* CONFIG_OWE */
377
378 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk,
379 vlan_id);
380 /*
381 * This is about to iterate over all psks, prev_psk gives the last
382 * returned psk which should not be returned again.
383 * logic list (all hostapd_get_psk; all sta->psk)
384 */
385 if (sta && sta->psk && !psk) {
386 struct hostapd_sta_wpa_psk_short *pos;
387
388 if (vlan_id)
389 *vlan_id = 0;
390 psk = sta->psk->psk;
391 for (pos = sta->psk; pos; pos = pos->next) {
392 if (pos->is_passphrase) {
393 pbkdf2_sha1(pos->passphrase,
394 hapd->conf->ssid.ssid,
395 hapd->conf->ssid.ssid_len, 4096,
396 pos->psk, PMK_LEN);
397 pos->is_passphrase = 0;
398 }
399 if (pos->psk == prev_psk) {
400 psk = pos->next ? pos->next->psk : NULL;
401 break;
402 }
403 }
404 }
405 return psk;
406 }
407
408
hostapd_wpa_auth_get_msk(void * ctx,const u8 * addr,u8 * msk,size_t * len)409 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
410 size_t *len)
411 {
412 struct hostapd_data *hapd = ctx;
413 const u8 *key;
414 size_t keylen;
415 struct sta_info *sta;
416
417 sta = ap_get_sta(hapd, addr);
418 if (sta == NULL) {
419 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
420 return -1;
421 }
422
423 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
424 if (key == NULL) {
425 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
426 sta->eapol_sm);
427 return -1;
428 }
429
430 if (keylen > *len)
431 keylen = *len;
432 os_memcpy(msk, key, keylen);
433 *len = keylen;
434
435 return 0;
436 }
437
438
hostapd_wpa_auth_set_key(void * ctx,int vlan_id,enum wpa_alg alg,const u8 * addr,int idx,u8 * key,size_t key_len,enum key_flag key_flag)439 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
440 const u8 *addr, int idx, u8 *key,
441 size_t key_len, enum key_flag key_flag)
442 {
443 struct hostapd_data *hapd = ctx;
444 const char *ifname = hapd->conf->iface;
445
446 if (vlan_id > 0) {
447 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
448 if (!ifname) {
449 if (!(hapd->iface->drv_flags &
450 WPA_DRIVER_FLAGS_VLAN_OFFLOAD))
451 return -1;
452 ifname = hapd->conf->iface;
453 }
454 }
455
456 #ifdef CONFIG_TESTING_OPTIONS
457 if (key_flag & KEY_FLAG_MODIFY) {
458 /* We are updating an already installed key. Don't overwrite
459 * the already stored key information with zeros.
460 */
461 } else if (addr && !is_broadcast_ether_addr(addr)) {
462 struct sta_info *sta;
463
464 sta = ap_get_sta(hapd, addr);
465 if (sta) {
466 sta->last_tk_alg = alg;
467 sta->last_tk_key_idx = idx;
468 if (key)
469 os_memcpy(sta->last_tk, key, key_len);
470 sta->last_tk_len = key_len;
471 }
472 } else if (alg == WPA_ALG_BIP_CMAC_128 ||
473 alg == WPA_ALG_BIP_GMAC_128 ||
474 alg == WPA_ALG_BIP_GMAC_256 ||
475 alg == WPA_ALG_BIP_CMAC_256) {
476 if (idx == 4 || idx == 5) {
477 hapd->last_igtk_alg = alg;
478 hapd->last_igtk_key_idx = idx;
479 if (key)
480 os_memcpy(hapd->last_igtk, key, key_len);
481 hapd->last_igtk_len = key_len;
482 } else if (idx == 6 || idx == 7) {
483 hapd->last_bigtk_alg = alg;
484 hapd->last_bigtk_key_idx = idx;
485 if (key)
486 os_memcpy(hapd->last_bigtk, key, key_len);
487 hapd->last_bigtk_len = key_len;
488 }
489 } else {
490 hapd->last_gtk_alg = alg;
491 hapd->last_gtk_key_idx = idx;
492 if (key)
493 os_memcpy(hapd->last_gtk, key, key_len);
494 hapd->last_gtk_len = key_len;
495 }
496 #endif /* CONFIG_TESTING_OPTIONS */
497 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, vlan_id, 1,
498 NULL, 0, key, key_len, key_flag);
499 }
500
501
hostapd_wpa_auth_get_seqnum(void * ctx,const u8 * addr,int idx,u8 * seq)502 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
503 u8 *seq)
504 {
505 struct hostapd_data *hapd = ctx;
506 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
507 }
508
509
hostapd_wpa_auth_send_eapol(void * ctx,const u8 * addr,const u8 * data,size_t data_len,int encrypt)510 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
511 const u8 *data, size_t data_len,
512 int encrypt)
513 {
514 struct hostapd_data *hapd = ctx;
515 struct sta_info *sta;
516 u32 flags = 0;
517
518 #ifdef CONFIG_TESTING_OPTIONS
519 if (hapd->ext_eapol_frame_io) {
520 size_t hex_len = 2 * data_len + 1;
521 char *hex = os_malloc(hex_len);
522
523 if (hex == NULL)
524 return -1;
525 wpa_snprintf_hex(hex, hex_len, data, data_len);
526 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
527 MAC2STR(addr), hex);
528 os_free(hex);
529 return 0;
530 }
531 #endif /* CONFIG_TESTING_OPTIONS */
532
533 sta = ap_get_sta(hapd, addr);
534 if (sta)
535 flags = hostapd_sta_flags_to_drv(sta->flags);
536
537 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
538 encrypt, flags);
539 }
540
541
hostapd_wpa_auth_for_each_sta(void * ctx,int (* cb)(struct wpa_state_machine * sm,void * ctx),void * cb_ctx)542 static int hostapd_wpa_auth_for_each_sta(
543 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
544 void *cb_ctx)
545 {
546 struct hostapd_data *hapd = ctx;
547 struct sta_info *sta;
548
549 for (sta = hapd->sta_list; sta; sta = sta->next) {
550 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
551 return 1;
552 }
553 return 0;
554 }
555
556
557 struct wpa_auth_iface_iter_data {
558 int (*cb)(struct wpa_authenticator *sm, void *ctx);
559 void *cb_ctx;
560 };
561
wpa_auth_iface_iter(struct hostapd_iface * iface,void * ctx)562 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
563 {
564 struct wpa_auth_iface_iter_data *data = ctx;
565 size_t i;
566 for (i = 0; i < iface->num_bss; i++) {
567 if (iface->bss[i]->wpa_auth &&
568 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
569 return 1;
570 }
571 return 0;
572 }
573
574
hostapd_wpa_auth_for_each_auth(void * ctx,int (* cb)(struct wpa_authenticator * sm,void * ctx),void * cb_ctx)575 static int hostapd_wpa_auth_for_each_auth(
576 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
577 void *cb_ctx)
578 {
579 struct hostapd_data *hapd = ctx;
580 struct wpa_auth_iface_iter_data data;
581 if (hapd->iface->interfaces == NULL ||
582 hapd->iface->interfaces->for_each_interface == NULL)
583 return -1;
584 data.cb = cb;
585 data.cb_ctx = cb_ctx;
586 return hapd->iface->interfaces->for_each_interface(
587 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
588 }
589
590
591 #ifdef CONFIG_IEEE80211R_AP
592
593 struct wpa_ft_rrb_rx_later_data {
594 struct dl_list list;
595 u8 addr[ETH_ALEN];
596 size_t data_len;
597 /* followed by data_len octets of data */
598 };
599
hostapd_wpa_ft_rrb_rx_later(void * eloop_ctx,void * timeout_ctx)600 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
601 {
602 struct hostapd_data *hapd = eloop_ctx;
603 struct wpa_ft_rrb_rx_later_data *data, *n;
604
605 dl_list_for_each_safe(data, n, &hapd->l2_queue,
606 struct wpa_ft_rrb_rx_later_data, list) {
607 if (hapd->wpa_auth) {
608 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
609 (const u8 *) (data + 1),
610 data->data_len);
611 }
612 dl_list_del(&data->list);
613 os_free(data);
614 }
615 }
616
617
618 struct wpa_auth_ft_iface_iter_data {
619 struct hostapd_data *src_hapd;
620 const u8 *dst;
621 const u8 *data;
622 size_t data_len;
623 };
624
625
hostapd_wpa_auth_ft_iter(struct hostapd_iface * iface,void * ctx)626 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
627 {
628 struct wpa_auth_ft_iface_iter_data *idata = ctx;
629 struct wpa_ft_rrb_rx_later_data *data;
630 struct hostapd_data *hapd;
631 size_t j;
632
633 for (j = 0; j < iface->num_bss; j++) {
634 hapd = iface->bss[j];
635 if (hapd == idata->src_hapd ||
636 !hapd->wpa_auth ||
637 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
638 continue;
639
640 wpa_printf(MSG_DEBUG,
641 "FT: Send RRB data directly to locally managed BSS "
642 MACSTR "@%s -> " MACSTR "@%s",
643 MAC2STR(idata->src_hapd->own_addr),
644 idata->src_hapd->conf->iface,
645 MAC2STR(hapd->own_addr), hapd->conf->iface);
646
647 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
648 * when it would be triggered when reading from a socket.
649 * This avoids
650 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
651 * that is calling hapd0:recv handler from within
652 * hapd0:send directly.
653 */
654 data = os_zalloc(sizeof(*data) + idata->data_len);
655 if (!data)
656 return 1;
657
658 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
659 os_memcpy(data + 1, idata->data, idata->data_len);
660 data->data_len = idata->data_len;
661
662 dl_list_add(&hapd->l2_queue, &data->list);
663
664 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
665 hapd, NULL))
666 eloop_register_timeout(0, 0,
667 hostapd_wpa_ft_rrb_rx_later,
668 hapd, NULL);
669
670 return 1;
671 }
672
673 return 0;
674 }
675
676 #endif /* CONFIG_IEEE80211R_AP */
677
678
hostapd_wpa_auth_send_ether(void * ctx,const u8 * dst,u16 proto,const u8 * data,size_t data_len)679 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
680 const u8 *data, size_t data_len)
681 {
682 struct hostapd_data *hapd = ctx;
683 struct l2_ethhdr *buf;
684 int ret;
685
686 #ifdef CONFIG_TESTING_OPTIONS
687 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
688 size_t hex_len = 2 * data_len + 1;
689 char *hex = os_malloc(hex_len);
690
691 if (hex == NULL)
692 return -1;
693 wpa_snprintf_hex(hex, hex_len, data, data_len);
694 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
695 MAC2STR(dst), hex);
696 os_free(hex);
697 return 0;
698 }
699 #endif /* CONFIG_TESTING_OPTIONS */
700
701 #ifdef CONFIG_IEEE80211R_AP
702 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
703 hapd->iface->interfaces->for_each_interface) {
704 int res;
705 struct wpa_auth_ft_iface_iter_data idata;
706 idata.src_hapd = hapd;
707 idata.dst = dst;
708 idata.data = data;
709 idata.data_len = data_len;
710 res = hapd->iface->interfaces->for_each_interface(
711 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
712 &idata);
713 if (res == 1)
714 return data_len;
715 }
716 #endif /* CONFIG_IEEE80211R_AP */
717
718 if (hapd->l2 == NULL)
719 return -1;
720
721 buf = os_malloc(sizeof(*buf) + data_len);
722 if (buf == NULL)
723 return -1;
724 os_memcpy(buf->h_dest, dst, ETH_ALEN);
725 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
726 buf->h_proto = host_to_be16(proto);
727 os_memcpy(buf + 1, data, data_len);
728 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
729 sizeof(*buf) + data_len);
730 os_free(buf);
731 return ret;
732 }
733
734
735 #ifdef CONFIG_ETH_P_OUI
hostapd_wpa_get_oui(struct hostapd_data * hapd,u8 oui_suffix)736 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
737 u8 oui_suffix)
738 {
739 switch (oui_suffix) {
740 #ifdef CONFIG_IEEE80211R_AP
741 case FT_PACKET_R0KH_R1KH_PULL:
742 return hapd->oui_pull;
743 case FT_PACKET_R0KH_R1KH_RESP:
744 return hapd->oui_resp;
745 case FT_PACKET_R0KH_R1KH_PUSH:
746 return hapd->oui_push;
747 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
748 return hapd->oui_sreq;
749 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
750 return hapd->oui_sresp;
751 #endif /* CONFIG_IEEE80211R_AP */
752 default:
753 return NULL;
754 }
755 }
756 #endif /* CONFIG_ETH_P_OUI */
757
758
759 #ifdef CONFIG_IEEE80211R_AP
760
761 struct oui_deliver_later_data {
762 struct dl_list list;
763 u8 src_addr[ETH_ALEN];
764 u8 dst_addr[ETH_ALEN];
765 size_t data_len;
766 u8 oui_suffix;
767 /* followed by data_len octets of data */
768 };
769
hostapd_oui_deliver_later(void * eloop_ctx,void * timeout_ctx)770 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
771 {
772 struct hostapd_data *hapd = eloop_ctx;
773 struct oui_deliver_later_data *data, *n;
774 struct eth_p_oui_ctx *oui_ctx;
775
776 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
777 struct oui_deliver_later_data, list) {
778 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
779 wpa_printf(MSG_DEBUG, "RRB(%s): %s src=" MACSTR " dst=" MACSTR
780 " oui_suffix=%u data_len=%u data=%p",
781 hapd->conf->iface, __func__,
782 MAC2STR(data->src_addr), MAC2STR(data->dst_addr),
783 data->oui_suffix, (unsigned int) data->data_len,
784 data);
785 if (hapd->wpa_auth && oui_ctx) {
786 eth_p_oui_deliver(oui_ctx, data->src_addr,
787 data->dst_addr,
788 (const u8 *) (data + 1),
789 data->data_len);
790 }
791 dl_list_del(&data->list);
792 os_free(data);
793 }
794 }
795
796
797 struct wpa_auth_oui_iface_iter_data {
798 struct hostapd_data *src_hapd;
799 const u8 *dst_addr;
800 const u8 *data;
801 size_t data_len;
802 u8 oui_suffix;
803 };
804
hostapd_wpa_auth_oui_iter(struct hostapd_iface * iface,void * ctx)805 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
806 {
807 struct wpa_auth_oui_iface_iter_data *idata = ctx;
808 struct oui_deliver_later_data *data;
809 struct hostapd_data *hapd, *src_hapd = idata->src_hapd;
810 size_t j;
811
812 for (j = 0; j < iface->num_bss; j++) {
813 hapd = iface->bss[j];
814 if (hapd == src_hapd)
815 continue; /* don't deliver back to same interface */
816 if (!wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) ||
817 hapd->conf->ssid.ssid_len !=
818 src_hapd->conf->ssid.ssid_len ||
819 os_memcmp(hapd->conf->ssid.ssid,
820 src_hapd->conf->ssid.ssid,
821 hapd->conf->ssid.ssid_len) != 0 ||
822 os_memcmp(hapd->conf->mobility_domain,
823 src_hapd->conf->mobility_domain,
824 MOBILITY_DOMAIN_ID_LEN) != 0)
825 continue; /* no matching FT SSID/mobility domain */
826 if (!is_multicast_ether_addr(idata->dst_addr) &&
827 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
828 continue; /* destination address does not match */
829
830 /* defer eth_p_oui_deliver until next eloop step as this is
831 * when it would be triggerd from reading from sock
832 * This avoids
833 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
834 * that is calling hapd0:recv handler from within
835 * hapd0:send directly.
836 */
837 data = os_zalloc(sizeof(*data) + idata->data_len);
838 if (!data)
839 return 1;
840 wpa_printf(MSG_DEBUG,
841 "RRB(%s): local delivery to %s dst=" MACSTR
842 " oui_suffix=%u data_len=%u data=%p",
843 src_hapd->conf->iface, hapd->conf->iface,
844 MAC2STR(idata->dst_addr), idata->oui_suffix,
845 (unsigned int) idata->data_len, data);
846
847 os_memcpy(data->src_addr, src_hapd->own_addr, ETH_ALEN);
848 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
849 os_memcpy(data + 1, idata->data, idata->data_len);
850 data->data_len = idata->data_len;
851 data->oui_suffix = idata->oui_suffix;
852
853 dl_list_add_tail(&hapd->l2_oui_queue, &data->list);
854
855 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
856 hapd, NULL))
857 eloop_register_timeout(0, 0,
858 hostapd_oui_deliver_later,
859 hapd, NULL);
860
861 /* If dst_addr is a multicast address, do not return any
862 * non-zero value here. Otherwise, the iteration of
863 * for_each_interface() will be stopped. */
864 if (!is_multicast_ether_addr(idata->dst_addr))
865 return 1;
866 }
867
868 return 0;
869 }
870
871 #endif /* CONFIG_IEEE80211R_AP */
872
873
hostapd_wpa_auth_send_oui(void * ctx,const u8 * dst,u8 oui_suffix,const u8 * data,size_t data_len)874 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
875 const u8 *data, size_t data_len)
876 {
877 #ifdef CONFIG_ETH_P_OUI
878 struct hostapd_data *hapd = ctx;
879 struct eth_p_oui_ctx *oui_ctx;
880
881 wpa_printf(MSG_DEBUG, "RRB(%s): send to dst=" MACSTR
882 " oui_suffix=%u data_len=%u",
883 hapd->conf->iface, MAC2STR(dst), oui_suffix,
884 (unsigned int) data_len);
885 #ifdef CONFIG_IEEE80211R_AP
886 if (hapd->iface->interfaces &&
887 hapd->iface->interfaces->for_each_interface) {
888 struct wpa_auth_oui_iface_iter_data idata;
889 int res;
890
891 idata.src_hapd = hapd;
892 idata.dst_addr = dst;
893 idata.data = data;
894 idata.data_len = data_len;
895 idata.oui_suffix = oui_suffix;
896 res = hapd->iface->interfaces->for_each_interface(
897 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
898 &idata);
899 if (res == 1)
900 return data_len;
901 }
902 #endif /* CONFIG_IEEE80211R_AP */
903
904 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
905 if (!oui_ctx)
906 return -1;
907
908 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
909 #else /* CONFIG_ETH_P_OUI */
910 return -1;
911 #endif /* CONFIG_ETH_P_OUI */
912 }
913
914
hostapd_channel_info(void * ctx,struct wpa_channel_info * ci)915 static int hostapd_channel_info(void *ctx, struct wpa_channel_info *ci)
916 {
917 struct hostapd_data *hapd = ctx;
918
919 return hostapd_drv_channel_info(hapd, ci);
920 }
921
922
923 #ifdef CONFIG_PASN
924
hostapd_store_ptksa(void * ctx,const u8 * addr,int cipher,u32 life_time,const struct wpa_ptk * ptk)925 static void hostapd_store_ptksa(void *ctx, const u8 *addr,int cipher,
926 u32 life_time, const struct wpa_ptk *ptk)
927 {
928 struct hostapd_data *hapd = ctx;
929
930 ptksa_cache_add(hapd->ptksa, addr, cipher, life_time, ptk);
931 }
932
933
hostapd_clear_ptksa(void * ctx,const u8 * addr,int cipher)934 static void hostapd_clear_ptksa(void *ctx, const u8 *addr, int cipher)
935 {
936 struct hostapd_data *hapd = ctx;
937
938 ptksa_cache_flush(hapd->ptksa, addr, cipher);
939 }
940
941 #endif /* CONFIG_PASN */
942
943
hostapd_wpa_auth_update_vlan(void * ctx,const u8 * addr,int vlan_id)944 static int hostapd_wpa_auth_update_vlan(void *ctx, const u8 *addr, int vlan_id)
945 {
946 #ifndef CONFIG_NO_VLAN
947 struct hostapd_data *hapd = ctx;
948 struct sta_info *sta;
949
950 sta = ap_get_sta(hapd, addr);
951 if (!sta)
952 return -1;
953
954 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
955 struct vlan_description vlan_desc;
956
957 os_memset(&vlan_desc, 0, sizeof(vlan_desc));
958 vlan_desc.notempty = 1;
959 vlan_desc.untagged = vlan_id;
960 if (!hostapd_vlan_valid(hapd->conf->vlan, &vlan_desc)) {
961 wpa_printf(MSG_INFO,
962 "Invalid VLAN ID %d in wpa_psk_file",
963 vlan_id);
964 return -1;
965 }
966
967 if (ap_sta_set_vlan(hapd, sta, &vlan_desc) < 0) {
968 wpa_printf(MSG_INFO,
969 "Failed to assign VLAN ID %d from wpa_psk_file to "
970 MACSTR, vlan_id, MAC2STR(sta->addr));
971 return -1;
972 }
973 } else {
974 sta->vlan_id = vlan_id;
975 }
976
977 wpa_printf(MSG_INFO,
978 "Assigned VLAN ID %d from wpa_psk_file to " MACSTR,
979 vlan_id, MAC2STR(sta->addr));
980 if ((sta->flags & WLAN_STA_ASSOC) &&
981 ap_sta_bind_vlan(hapd, sta) < 0)
982 return -1;
983 #endif /* CONFIG_NO_VLAN */
984
985 return 0;
986 }
987
988
989 #ifdef CONFIG_OCV
hostapd_get_sta_tx_params(void * ctx,const u8 * addr,int ap_max_chanwidth,int ap_seg1_idx,int * bandwidth,int * seg1_idx)990 static int hostapd_get_sta_tx_params(void *ctx, const u8 *addr,
991 int ap_max_chanwidth, int ap_seg1_idx,
992 int *bandwidth, int *seg1_idx)
993 {
994 struct hostapd_data *hapd = ctx;
995 struct sta_info *sta;
996
997 sta = ap_get_sta(hapd, addr);
998 if (!sta) {
999 hostapd_wpa_auth_logger(hapd, addr, LOGGER_INFO,
1000 "Failed to get STA info to validate received OCI");
1001 return -1;
1002 }
1003
1004 return get_tx_parameters(sta, ap_max_chanwidth, ap_seg1_idx, bandwidth,
1005 seg1_idx);
1006 }
1007 #endif /* CONFIG_OCV */
1008
1009
1010 #ifdef CONFIG_IEEE80211R_AP
1011
hostapd_wpa_auth_send_ft_action(void * ctx,const u8 * dst,const u8 * data,size_t data_len)1012 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1013 const u8 *data, size_t data_len)
1014 {
1015 struct hostapd_data *hapd = ctx;
1016 int res;
1017 struct ieee80211_mgmt *m;
1018 size_t mlen;
1019 struct sta_info *sta;
1020
1021 sta = ap_get_sta(hapd, dst);
1022 if (sta == NULL || sta->wpa_sm == NULL)
1023 return -1;
1024
1025 m = os_zalloc(sizeof(*m) + data_len);
1026 if (m == NULL)
1027 return -1;
1028 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1029 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1030 WLAN_FC_STYPE_ACTION);
1031 os_memcpy(m->da, dst, ETH_ALEN);
1032 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1033 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1034 os_memcpy(&m->u, data, data_len);
1035
1036 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0, NULL, 0, 0);
1037 os_free(m);
1038 return res;
1039 }
1040
1041
1042 static struct wpa_state_machine *
hostapd_wpa_auth_add_sta(void * ctx,const u8 * sta_addr)1043 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1044 {
1045 struct hostapd_data *hapd = ctx;
1046 struct sta_info *sta;
1047 int ret;
1048
1049 wpa_printf(MSG_DEBUG, "Add station entry for " MACSTR
1050 " based on WPA authenticator callback",
1051 MAC2STR(sta_addr));
1052 ret = hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT);
1053
1054 /*
1055 * The expected return values from hostapd_add_sta_node() are
1056 * 0: successfully added STA entry
1057 * -EOPNOTSUPP: driver or driver wrapper does not support/need this
1058 * operations
1059 * any other negative value: error in adding the STA entry */
1060 if (ret < 0 && ret != -EOPNOTSUPP)
1061 return NULL;
1062
1063 sta = ap_sta_add(hapd, sta_addr);
1064 if (sta == NULL)
1065 return NULL;
1066 if (ret == 0)
1067 sta->added_unassoc = 1;
1068
1069 sta->ft_over_ds = 1;
1070 if (sta->wpa_sm) {
1071 sta->auth_alg = WLAN_AUTH_FT;
1072 return sta->wpa_sm;
1073 }
1074
1075 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
1076 if (sta->wpa_sm == NULL) {
1077 ap_free_sta(hapd, sta);
1078 return NULL;
1079 }
1080 sta->auth_alg = WLAN_AUTH_FT;
1081
1082 return sta->wpa_sm;
1083 }
1084
1085
hostapd_wpa_auth_add_sta_ft(void * ctx,const u8 * sta_addr)1086 static int hostapd_wpa_auth_add_sta_ft(void *ctx, const u8 *sta_addr)
1087 {
1088 struct hostapd_data *hapd = ctx;
1089 struct sta_info *sta;
1090
1091 sta = ap_get_sta(hapd, sta_addr);
1092 if (!sta)
1093 return -1;
1094
1095 if (FULL_AP_CLIENT_STATE_SUPP(hapd->iface->drv_flags) &&
1096 (sta->flags & WLAN_STA_MFP) && ap_sta_is_authorized(sta) &&
1097 !(hapd->conf->mesh & MESH_ENABLED) && !(sta->added_unassoc)) {
1098 /* We could not do this in handle_auth() since there was a
1099 * PMF-enabled association for the STA and the new
1100 * authentication attempt was not yet fully processed. Now that
1101 * we are ready to configure the TK to the driver,
1102 * authentication has succeeded and we can clean up the driver
1103 * STA entry to avoid issues with any maintained state from the
1104 * previous association. */
1105 wpa_printf(MSG_DEBUG,
1106 "FT: Remove and re-add driver STA entry after successful FT authentication");
1107 return ap_sta_re_add(hapd, sta);
1108 }
1109
1110 return 0;
1111 }
1112
1113
hostapd_wpa_auth_set_vlan(void * ctx,const u8 * sta_addr,struct vlan_description * vlan)1114 static int hostapd_wpa_auth_set_vlan(void *ctx, const u8 *sta_addr,
1115 struct vlan_description *vlan)
1116 {
1117 struct hostapd_data *hapd = ctx;
1118 struct sta_info *sta;
1119
1120 sta = ap_get_sta(hapd, sta_addr);
1121 if (!sta || !sta->wpa_sm)
1122 return -1;
1123
1124 if (vlan->notempty &&
1125 !hostapd_vlan_valid(hapd->conf->vlan, vlan)) {
1126 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1127 HOSTAPD_LEVEL_INFO,
1128 "Invalid VLAN %d%s received from FT",
1129 vlan->untagged, vlan->tagged[0] ? "+" : "");
1130 return -1;
1131 }
1132
1133 if (ap_sta_set_vlan(hapd, sta, vlan) < 0)
1134 return -1;
1135 /* Configure wpa_group for GTK but ignore error due to driver not
1136 * knowing this STA. */
1137 ap_sta_bind_vlan(hapd, sta);
1138
1139 if (sta->vlan_id)
1140 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1141 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
1142
1143 return 0;
1144 }
1145
1146
hostapd_wpa_auth_get_vlan(void * ctx,const u8 * sta_addr,struct vlan_description * vlan)1147 static int hostapd_wpa_auth_get_vlan(void *ctx, const u8 *sta_addr,
1148 struct vlan_description *vlan)
1149 {
1150 struct hostapd_data *hapd = ctx;
1151 struct sta_info *sta;
1152
1153 sta = ap_get_sta(hapd, sta_addr);
1154 if (!sta)
1155 return -1;
1156
1157 if (sta->vlan_desc)
1158 *vlan = *sta->vlan_desc;
1159 else
1160 os_memset(vlan, 0, sizeof(*vlan));
1161
1162 return 0;
1163 }
1164
1165
1166 static int
hostapd_wpa_auth_set_identity(void * ctx,const u8 * sta_addr,const u8 * identity,size_t identity_len)1167 hostapd_wpa_auth_set_identity(void *ctx, const u8 *sta_addr,
1168 const u8 *identity, size_t identity_len)
1169 {
1170 struct hostapd_data *hapd = ctx;
1171 struct sta_info *sta;
1172
1173 sta = ap_get_sta(hapd, sta_addr);
1174 if (!sta)
1175 return -1;
1176
1177 os_free(sta->identity);
1178 sta->identity = NULL;
1179
1180 if (sta->eapol_sm) {
1181 os_free(sta->eapol_sm->identity);
1182 sta->eapol_sm->identity = NULL;
1183 sta->eapol_sm->identity_len = 0;
1184 }
1185
1186 if (!identity_len)
1187 return 0;
1188
1189 /* sta->identity is NULL terminated */
1190 sta->identity = os_zalloc(identity_len + 1);
1191 if (!sta->identity)
1192 return -1;
1193 os_memcpy(sta->identity, identity, identity_len);
1194
1195 if (sta->eapol_sm) {
1196 sta->eapol_sm->identity = os_zalloc(identity_len);
1197 if (!sta->eapol_sm->identity)
1198 return -1;
1199 os_memcpy(sta->eapol_sm->identity, identity, identity_len);
1200 sta->eapol_sm->identity_len = identity_len;
1201 }
1202
1203 return 0;
1204 }
1205
1206
1207 static size_t
hostapd_wpa_auth_get_identity(void * ctx,const u8 * sta_addr,const u8 ** buf)1208 hostapd_wpa_auth_get_identity(void *ctx, const u8 *sta_addr, const u8 **buf)
1209 {
1210 struct hostapd_data *hapd = ctx;
1211 struct sta_info *sta;
1212 size_t len;
1213 char *identity;
1214
1215 sta = ap_get_sta(hapd, sta_addr);
1216 if (!sta)
1217 return 0;
1218
1219 *buf = ieee802_1x_get_identity(sta->eapol_sm, &len);
1220 if (*buf && len)
1221 return len;
1222
1223 if (!sta->identity) {
1224 *buf = NULL;
1225 return 0;
1226 }
1227
1228 identity = sta->identity;
1229 len = os_strlen(identity);
1230 *buf = (u8 *) identity;
1231
1232 return len;
1233 }
1234
1235
1236 static int
hostapd_wpa_auth_set_radius_cui(void * ctx,const u8 * sta_addr,const u8 * radius_cui,size_t radius_cui_len)1237 hostapd_wpa_auth_set_radius_cui(void *ctx, const u8 *sta_addr,
1238 const u8 *radius_cui, size_t radius_cui_len)
1239 {
1240 struct hostapd_data *hapd = ctx;
1241 struct sta_info *sta;
1242
1243 sta = ap_get_sta(hapd, sta_addr);
1244 if (!sta)
1245 return -1;
1246
1247 os_free(sta->radius_cui);
1248 sta->radius_cui = NULL;
1249
1250 if (sta->eapol_sm) {
1251 wpabuf_free(sta->eapol_sm->radius_cui);
1252 sta->eapol_sm->radius_cui = NULL;
1253 }
1254
1255 if (!radius_cui)
1256 return 0;
1257
1258 /* sta->radius_cui is NULL terminated */
1259 sta->radius_cui = os_zalloc(radius_cui_len + 1);
1260 if (!sta->radius_cui)
1261 return -1;
1262 os_memcpy(sta->radius_cui, radius_cui, radius_cui_len);
1263
1264 if (sta->eapol_sm) {
1265 sta->eapol_sm->radius_cui = wpabuf_alloc_copy(radius_cui,
1266 radius_cui_len);
1267 if (!sta->eapol_sm->radius_cui)
1268 return -1;
1269 }
1270
1271 return 0;
1272 }
1273
1274
1275 static size_t
hostapd_wpa_auth_get_radius_cui(void * ctx,const u8 * sta_addr,const u8 ** buf)1276 hostapd_wpa_auth_get_radius_cui(void *ctx, const u8 *sta_addr, const u8 **buf)
1277 {
1278 struct hostapd_data *hapd = ctx;
1279 struct sta_info *sta;
1280 struct wpabuf *b;
1281 size_t len;
1282 char *radius_cui;
1283
1284 sta = ap_get_sta(hapd, sta_addr);
1285 if (!sta)
1286 return 0;
1287
1288 b = ieee802_1x_get_radius_cui(sta->eapol_sm);
1289 if (b) {
1290 len = wpabuf_len(b);
1291 *buf = wpabuf_head(b);
1292 return len;
1293 }
1294
1295 if (!sta->radius_cui) {
1296 *buf = NULL;
1297 return 0;
1298 }
1299
1300 radius_cui = sta->radius_cui;
1301 len = os_strlen(radius_cui);
1302 *buf = (u8 *) radius_cui;
1303
1304 return len;
1305 }
1306
1307
hostapd_wpa_auth_set_session_timeout(void * ctx,const u8 * sta_addr,int session_timeout)1308 static void hostapd_wpa_auth_set_session_timeout(void *ctx, const u8 *sta_addr,
1309 int session_timeout)
1310 {
1311 struct hostapd_data *hapd = ctx;
1312 struct sta_info *sta;
1313
1314 sta = ap_get_sta(hapd, sta_addr);
1315 if (!sta)
1316 return;
1317
1318 if (session_timeout) {
1319 os_get_reltime(&sta->session_timeout);
1320 sta->session_timeout.sec += session_timeout;
1321 sta->session_timeout_set = 1;
1322 ap_sta_session_timeout(hapd, sta, session_timeout);
1323 } else {
1324 sta->session_timeout_set = 0;
1325 ap_sta_no_session_timeout(hapd, sta);
1326 }
1327 }
1328
1329
hostapd_wpa_auth_get_session_timeout(void * ctx,const u8 * sta_addr)1330 static int hostapd_wpa_auth_get_session_timeout(void *ctx, const u8 *sta_addr)
1331 {
1332 struct hostapd_data *hapd = ctx;
1333 struct sta_info *sta;
1334 struct os_reltime now, remaining;
1335
1336 sta = ap_get_sta(hapd, sta_addr);
1337 if (!sta || !sta->session_timeout_set)
1338 return 0;
1339
1340 os_get_reltime(&now);
1341 if (os_reltime_before(&sta->session_timeout, &now)) {
1342 /* already expired, return >0 as timeout was set */
1343 return 1;
1344 }
1345
1346 os_reltime_sub(&sta->session_timeout, &now, &remaining);
1347
1348 return (remaining.sec > 0) ? remaining.sec : 1;
1349 }
1350
1351
hostapd_rrb_receive(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1352 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1353 size_t len)
1354 {
1355 struct hostapd_data *hapd = ctx;
1356 struct l2_ethhdr *ethhdr;
1357 if (len < sizeof(*ethhdr))
1358 return;
1359 ethhdr = (struct l2_ethhdr *) buf;
1360 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1361 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
1362 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
1363 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
1364 return;
1365 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
1366 len - sizeof(*ethhdr));
1367 }
1368
1369
hostapd_rrb_oui_receive(void * ctx,const u8 * src_addr,const u8 * dst_addr,u8 oui_suffix,const u8 * buf,size_t len)1370 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
1371 const u8 *dst_addr, u8 oui_suffix,
1372 const u8 *buf, size_t len)
1373 {
1374 struct hostapd_data *hapd = ctx;
1375
1376 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
1377 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
1378 if (!is_multicast_ether_addr(dst_addr) &&
1379 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
1380 return;
1381 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
1382 len);
1383 }
1384
1385
hostapd_wpa_auth_add_tspec(void * ctx,const u8 * sta_addr,u8 * tspec_ie,size_t tspec_ielen)1386 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
1387 u8 *tspec_ie, size_t tspec_ielen)
1388 {
1389 struct hostapd_data *hapd = ctx;
1390 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
1391 }
1392
1393
1394
hostapd_wpa_register_ft_oui(struct hostapd_data * hapd,const char * ft_iface)1395 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
1396 const char *ft_iface)
1397 {
1398 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
1399 FT_PACKET_R0KH_R1KH_PULL,
1400 hostapd_rrb_oui_receive, hapd);
1401 if (!hapd->oui_pull)
1402 return -1;
1403
1404 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
1405 FT_PACKET_R0KH_R1KH_RESP,
1406 hostapd_rrb_oui_receive, hapd);
1407 if (!hapd->oui_resp)
1408 return -1;
1409
1410 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
1411 FT_PACKET_R0KH_R1KH_PUSH,
1412 hostapd_rrb_oui_receive, hapd);
1413 if (!hapd->oui_push)
1414 return -1;
1415
1416 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
1417 FT_PACKET_R0KH_R1KH_SEQ_REQ,
1418 hostapd_rrb_oui_receive, hapd);
1419 if (!hapd->oui_sreq)
1420 return -1;
1421
1422 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
1423 FT_PACKET_R0KH_R1KH_SEQ_RESP,
1424 hostapd_rrb_oui_receive, hapd);
1425 if (!hapd->oui_sresp)
1426 return -1;
1427
1428 return 0;
1429 }
1430
1431
hostapd_wpa_unregister_ft_oui(struct hostapd_data * hapd)1432 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
1433 {
1434 eth_p_oui_unregister(hapd->oui_pull);
1435 hapd->oui_pull = NULL;
1436 eth_p_oui_unregister(hapd->oui_resp);
1437 hapd->oui_resp = NULL;
1438 eth_p_oui_unregister(hapd->oui_push);
1439 hapd->oui_push = NULL;
1440 eth_p_oui_unregister(hapd->oui_sreq);
1441 hapd->oui_sreq = NULL;
1442 eth_p_oui_unregister(hapd->oui_sresp);
1443 hapd->oui_sresp = NULL;
1444 }
1445 #endif /* CONFIG_IEEE80211R_AP */
1446
1447
hostapd_setup_wpa(struct hostapd_data * hapd)1448 int hostapd_setup_wpa(struct hostapd_data *hapd)
1449 {
1450 struct wpa_auth_config _conf;
1451 static const struct wpa_auth_callbacks cb = {
1452 .logger = hostapd_wpa_auth_logger,
1453 .disconnect = hostapd_wpa_auth_disconnect,
1454 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
1455 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
1456 .set_eapol = hostapd_wpa_auth_set_eapol,
1457 .get_eapol = hostapd_wpa_auth_get_eapol,
1458 .get_psk = hostapd_wpa_auth_get_psk,
1459 .get_msk = hostapd_wpa_auth_get_msk,
1460 .set_key = hostapd_wpa_auth_set_key,
1461 .get_seqnum = hostapd_wpa_auth_get_seqnum,
1462 .send_eapol = hostapd_wpa_auth_send_eapol,
1463 .for_each_sta = hostapd_wpa_auth_for_each_sta,
1464 .for_each_auth = hostapd_wpa_auth_for_each_auth,
1465 .send_ether = hostapd_wpa_auth_send_ether,
1466 .send_oui = hostapd_wpa_auth_send_oui,
1467 .channel_info = hostapd_channel_info,
1468 .update_vlan = hostapd_wpa_auth_update_vlan,
1469 #ifdef CONFIG_PASN
1470 .store_ptksa = hostapd_store_ptksa,
1471 .clear_ptksa = hostapd_clear_ptksa,
1472 #endif /* CONFIG_PASN */
1473
1474 #ifdef CONFIG_OCV
1475 .get_sta_tx_params = hostapd_get_sta_tx_params,
1476 #endif /* CONFIG_OCV */
1477 #ifdef CONFIG_IEEE80211R_AP
1478 .send_ft_action = hostapd_wpa_auth_send_ft_action,
1479 .add_sta = hostapd_wpa_auth_add_sta,
1480 .add_sta_ft = hostapd_wpa_auth_add_sta_ft,
1481 .add_tspec = hostapd_wpa_auth_add_tspec,
1482 .set_vlan = hostapd_wpa_auth_set_vlan,
1483 .get_vlan = hostapd_wpa_auth_get_vlan,
1484 .set_identity = hostapd_wpa_auth_set_identity,
1485 .get_identity = hostapd_wpa_auth_get_identity,
1486 .set_radius_cui = hostapd_wpa_auth_set_radius_cui,
1487 .get_radius_cui = hostapd_wpa_auth_get_radius_cui,
1488 .set_session_timeout = hostapd_wpa_auth_set_session_timeout,
1489 .get_session_timeout = hostapd_wpa_auth_get_session_timeout,
1490 #endif /* CONFIG_IEEE80211R_AP */
1491 };
1492 const u8 *wpa_ie;
1493 size_t wpa_ie_len;
1494
1495 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
1496 _conf.msg_ctx = hapd->msg_ctx;
1497 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
1498 _conf.tx_status = 1;
1499 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
1500 _conf.ap_mlme = 1;
1501
1502 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_WIRED) &&
1503 (hapd->conf->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_NEVER ||
1504 (hapd->conf->wpa_deny_ptk0_rekey == PTK0_REKEY_ALLOW_LOCAL_OK &&
1505 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS)))) {
1506 wpa_msg(hapd->msg_ctx, MSG_INFO,
1507 "Disable PTK0 rekey support - replaced with disconnect");
1508 _conf.wpa_deny_ptk0_rekey = 1;
1509 }
1510
1511 if (_conf.extended_key_id &&
1512 (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EXTENDED_KEY_ID))
1513 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Extended Key ID supported");
1514 else
1515 _conf.extended_key_id = 0;
1516
1517 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION))
1518 _conf.beacon_prot = 0;
1519
1520 #ifdef CONFIG_OCV
1521 if (!(hapd->iface->drv_flags2 &
1522 (WPA_DRIVER_FLAGS2_AP_SME | WPA_DRIVER_FLAGS2_OCV)))
1523 _conf.ocv = 0;
1524 #endif /* CONFIG_OCV */
1525
1526 _conf.secure_ltf =
1527 !!(hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF);
1528 _conf.secure_rtt =
1529 !!(hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_RTT);
1530 _conf.prot_range_neg =
1531 !!(hapd->iface->drv_flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG);
1532
1533 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
1534 if (hapd->wpa_auth == NULL) {
1535 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1536 return -1;
1537 }
1538
1539 if (hostapd_set_privacy(hapd, 1)) {
1540 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1541 "for interface %s", hapd->conf->iface);
1542 return -1;
1543 }
1544
1545 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1546 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1547 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1548 "the kernel driver.");
1549 return -1;
1550 }
1551
1552 if (rsn_preauth_iface_init(hapd)) {
1553 wpa_printf(MSG_ERROR, "Initialization of RSN "
1554 "pre-authentication failed.");
1555 return -1;
1556 }
1557
1558 hapd->ptksa = ptksa_cache_init();
1559 if (!hapd->ptksa) {
1560 wpa_printf(MSG_ERROR, "Failed to allocate PTKSA cache");
1561 return -1;
1562 }
1563
1564 #ifdef CONFIG_IEEE80211R_AP
1565 if (!hostapd_drv_none(hapd) &&
1566 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
1567 const char *ft_iface;
1568
1569 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
1570 hapd->conf->iface;
1571 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
1572 hostapd_rrb_receive, hapd, 1);
1573 if (!hapd->l2) {
1574 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1575 "interface");
1576 return -1;
1577 }
1578
1579 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1580 wpa_printf(MSG_ERROR,
1581 "Failed to open ETH_P_OUI interface");
1582 return -1;
1583 }
1584 }
1585 #endif /* CONFIG_IEEE80211R_AP */
1586
1587 return 0;
1588
1589 }
1590
1591
hostapd_reconfig_wpa(struct hostapd_data * hapd)1592 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1593 {
1594 struct wpa_auth_config wpa_auth_conf;
1595 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1596 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1597 }
1598
1599
hostapd_deinit_wpa(struct hostapd_data * hapd)1600 void hostapd_deinit_wpa(struct hostapd_data *hapd)
1601 {
1602 ieee80211_tkip_countermeasures_deinit(hapd);
1603 ptksa_cache_deinit(hapd->ptksa);
1604 hapd->ptksa = NULL;
1605
1606 rsn_preauth_iface_deinit(hapd);
1607 if (hapd->wpa_auth) {
1608 wpa_deinit(hapd->wpa_auth);
1609 hapd->wpa_auth = NULL;
1610
1611 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1612 wpa_printf(MSG_DEBUG, "Could not disable "
1613 "PrivacyInvoked for interface %s",
1614 hapd->conf->iface);
1615 }
1616
1617 if (hapd->drv_priv &&
1618 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1619 wpa_printf(MSG_DEBUG, "Could not remove generic "
1620 "information element from interface %s",
1621 hapd->conf->iface);
1622 }
1623 }
1624 ieee802_1x_deinit(hapd);
1625
1626 #ifdef CONFIG_IEEE80211R_AP
1627 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1628 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1629 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1630 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1631 l2_packet_deinit(hapd->l2);
1632 hapd->l2 = NULL;
1633 hostapd_wpa_unregister_ft_oui(hapd);
1634 #endif /* CONFIG_IEEE80211R_AP */
1635 }
1636