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