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