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