]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth_glue.c
d9f917e4a528eafffd478ac73a31d9e56a08a246
[thirdparty/hostap.git] / src / ap / wpa_auth_glue.c
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 "pmksa_cache_auth.h"
31 #include "wpa_auth.h"
32 #include "wpa_auth_glue.h"
33
34
35 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
36 struct hostapd_config *iconf,
37 struct wpa_auth_config *wconf)
38 {
39 os_memset(wconf, 0, sizeof(*wconf));
40 wconf->wpa = conf->wpa;
41 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
42 wconf->wpa_pairwise = conf->wpa_pairwise;
43 wconf->wpa_group = conf->wpa_group;
44 wconf->wpa_group_rekey = conf->wpa_group_rekey;
45 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
46 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
47 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
48 wconf->wpa_group_update_count = conf->wpa_group_update_count;
49 wconf->wpa_pairwise_update_count = conf->wpa_pairwise_update_count;
50 wconf->rsn_pairwise = conf->rsn_pairwise;
51 wconf->rsn_preauth = conf->rsn_preauth;
52 wconf->eapol_version = conf->eapol_version;
53 wconf->wmm_enabled = conf->wmm_enabled;
54 wconf->wmm_uapsd = conf->wmm_uapsd;
55 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
56 wconf->okc = conf->okc;
57 #ifdef CONFIG_IEEE80211W
58 wconf->ieee80211w = conf->ieee80211w;
59 wconf->group_mgmt_cipher = conf->group_mgmt_cipher;
60 #endif /* CONFIG_IEEE80211W */
61 #ifdef CONFIG_IEEE80211R_AP
62 wconf->ssid_len = conf->ssid.ssid_len;
63 if (wconf->ssid_len > SSID_MAX_LEN)
64 wconf->ssid_len = SSID_MAX_LEN;
65 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
66 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
67 MOBILITY_DOMAIN_ID_LEN);
68 if (conf->nas_identifier &&
69 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
70 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
71 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
72 wconf->r0_key_holder_len);
73 }
74 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
75 wconf->r0_key_lifetime = conf->r0_key_lifetime;
76 wconf->reassociation_deadline = conf->reassociation_deadline;
77 wconf->rkh_pos_timeout = conf->rkh_pos_timeout;
78 wconf->rkh_neg_timeout = conf->rkh_neg_timeout;
79 wconf->rkh_pull_timeout = conf->rkh_pull_timeout;
80 wconf->rkh_pull_retries = conf->rkh_pull_retries;
81 wconf->r0kh_list = &conf->r0kh_list;
82 wconf->r1kh_list = &conf->r1kh_list;
83 wconf->pmk_r1_push = conf->pmk_r1_push;
84 wconf->ft_over_ds = conf->ft_over_ds;
85 wconf->ft_psk_generate_local = conf->ft_psk_generate_local;
86 #endif /* CONFIG_IEEE80211R_AP */
87 #ifdef CONFIG_HS20
88 wconf->disable_gtk = conf->disable_dgaf;
89 if (conf->osen) {
90 wconf->disable_gtk = 1;
91 wconf->wpa = WPA_PROTO_OSEN;
92 wconf->wpa_key_mgmt = WPA_KEY_MGMT_OSEN;
93 wconf->wpa_pairwise = 0;
94 wconf->wpa_group = WPA_CIPHER_CCMP;
95 wconf->rsn_pairwise = WPA_CIPHER_CCMP;
96 wconf->rsn_preauth = 0;
97 wconf->disable_pmksa_caching = 1;
98 #ifdef CONFIG_IEEE80211W
99 wconf->ieee80211w = 1;
100 #endif /* CONFIG_IEEE80211W */
101 }
102 #endif /* CONFIG_HS20 */
103 #ifdef CONFIG_TESTING_OPTIONS
104 wconf->corrupt_gtk_rekey_mic_probability =
105 iconf->corrupt_gtk_rekey_mic_probability;
106 if (conf->own_ie_override &&
107 wpabuf_len(conf->own_ie_override) <= MAX_OWN_IE_OVERRIDE) {
108 wconf->own_ie_override_len = wpabuf_len(conf->own_ie_override);
109 os_memcpy(wconf->own_ie_override,
110 wpabuf_head(conf->own_ie_override),
111 wconf->own_ie_override_len);
112 }
113 #endif /* CONFIG_TESTING_OPTIONS */
114 #ifdef CONFIG_P2P
115 os_memcpy(wconf->ip_addr_go, conf->ip_addr_go, 4);
116 os_memcpy(wconf->ip_addr_mask, conf->ip_addr_mask, 4);
117 os_memcpy(wconf->ip_addr_start, conf->ip_addr_start, 4);
118 os_memcpy(wconf->ip_addr_end, conf->ip_addr_end, 4);
119 #endif /* CONFIG_P2P */
120 #ifdef CONFIG_FILS
121 wconf->fils_cache_id_set = conf->fils_cache_id_set;
122 os_memcpy(wconf->fils_cache_id, conf->fils_cache_id,
123 FILS_CACHE_ID_LEN);
124 #endif /* CONFIG_FILS */
125 }
126
127
128 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
129 logger_level level, const char *txt)
130 {
131 #ifndef CONFIG_NO_HOSTAPD_LOGGER
132 struct hostapd_data *hapd = ctx;
133 int hlevel;
134
135 switch (level) {
136 case LOGGER_WARNING:
137 hlevel = HOSTAPD_LEVEL_WARNING;
138 break;
139 case LOGGER_INFO:
140 hlevel = HOSTAPD_LEVEL_INFO;
141 break;
142 case LOGGER_DEBUG:
143 default:
144 hlevel = HOSTAPD_LEVEL_DEBUG;
145 break;
146 }
147
148 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
149 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
150 }
151
152
153 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
154 u16 reason)
155 {
156 struct hostapd_data *hapd = ctx;
157 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
158 "STA " MACSTR " reason %d",
159 __func__, MAC2STR(addr), reason);
160 ap_sta_disconnect(hapd, NULL, addr, reason);
161 }
162
163
164 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
165 {
166 struct hostapd_data *hapd = ctx;
167 return michael_mic_failure(hapd, addr, 0);
168 }
169
170
171 static void hostapd_wpa_auth_psk_failure_report(void *ctx, const u8 *addr)
172 {
173 struct hostapd_data *hapd = ctx;
174 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_POSSIBLE_PSK_MISMATCH MACSTR,
175 MAC2STR(addr));
176 }
177
178
179 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
180 wpa_eapol_variable var, int value)
181 {
182 struct hostapd_data *hapd = ctx;
183 struct sta_info *sta = ap_get_sta(hapd, addr);
184 if (sta == NULL)
185 return;
186 switch (var) {
187 case WPA_EAPOL_portEnabled:
188 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
189 break;
190 case WPA_EAPOL_portValid:
191 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
192 break;
193 case WPA_EAPOL_authorized:
194 ieee802_1x_set_sta_authorized(hapd, sta, value);
195 break;
196 case WPA_EAPOL_portControl_Auto:
197 if (sta->eapol_sm)
198 sta->eapol_sm->portControl = Auto;
199 break;
200 case WPA_EAPOL_keyRun:
201 if (sta->eapol_sm)
202 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
203 break;
204 case WPA_EAPOL_keyAvailable:
205 if (sta->eapol_sm)
206 sta->eapol_sm->eap_if->eapKeyAvailable =
207 value ? TRUE : FALSE;
208 break;
209 case WPA_EAPOL_keyDone:
210 if (sta->eapol_sm)
211 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
212 break;
213 case WPA_EAPOL_inc_EapolFramesTx:
214 if (sta->eapol_sm)
215 sta->eapol_sm->dot1xAuthEapolFramesTx++;
216 break;
217 }
218 }
219
220
221 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
222 wpa_eapol_variable var)
223 {
224 struct hostapd_data *hapd = ctx;
225 struct sta_info *sta = ap_get_sta(hapd, addr);
226 if (sta == NULL || sta->eapol_sm == NULL)
227 return -1;
228 switch (var) {
229 case WPA_EAPOL_keyRun:
230 return sta->eapol_sm->keyRun;
231 case WPA_EAPOL_keyAvailable:
232 return sta->eapol_sm->eap_if->eapKeyAvailable;
233 default:
234 return -1;
235 }
236 }
237
238
239 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
240 const u8 *p2p_dev_addr,
241 const u8 *prev_psk, size_t *psk_len)
242 {
243 struct hostapd_data *hapd = ctx;
244 struct sta_info *sta = ap_get_sta(hapd, addr);
245 const u8 *psk;
246
247 if (psk_len)
248 *psk_len = PMK_LEN;
249
250 #ifdef CONFIG_SAE
251 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
252 if (!sta->sae || prev_psk)
253 return NULL;
254 return sta->sae->pmk;
255 }
256 if (sta && wpa_auth_uses_sae(sta->wpa_sm)) {
257 wpa_printf(MSG_DEBUG,
258 "No PSK for STA trying to use SAE with PMKSA caching");
259 return NULL;
260 }
261 #endif /* CONFIG_SAE */
262
263 #ifdef CONFIG_OWE
264 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
265 sta && sta->owe_pmk) {
266 if (psk_len)
267 *psk_len = sta->owe_pmk_len;
268 return sta->owe_pmk;
269 }
270 if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) && sta) {
271 struct rsn_pmksa_cache_entry *sa;
272
273 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
274 if (sa && sa->akmp == WPA_KEY_MGMT_OWE) {
275 if (psk_len)
276 *psk_len = sa->pmk_len;
277 return sa->pmk;
278 }
279 }
280 #endif /* CONFIG_OWE */
281
282 psk = hostapd_get_psk(hapd->conf, addr, p2p_dev_addr, prev_psk);
283 /*
284 * This is about to iterate over all psks, prev_psk gives the last
285 * returned psk which should not be returned again.
286 * logic list (all hostapd_get_psk; all sta->psk)
287 */
288 if (sta && sta->psk && !psk) {
289 struct hostapd_sta_wpa_psk_short *pos;
290 psk = sta->psk->psk;
291 for (pos = sta->psk; pos; pos = pos->next) {
292 if (pos->is_passphrase) {
293 pbkdf2_sha1(pos->passphrase,
294 hapd->conf->ssid.ssid,
295 hapd->conf->ssid.ssid_len, 4096,
296 pos->psk, PMK_LEN);
297 pos->is_passphrase = 0;
298 }
299 if (pos->psk == prev_psk) {
300 psk = pos->next ? pos->next->psk : NULL;
301 break;
302 }
303 }
304 }
305 return psk;
306 }
307
308
309 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
310 size_t *len)
311 {
312 struct hostapd_data *hapd = ctx;
313 const u8 *key;
314 size_t keylen;
315 struct sta_info *sta;
316
317 sta = ap_get_sta(hapd, addr);
318 if (sta == NULL) {
319 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Cannot find STA");
320 return -1;
321 }
322
323 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
324 if (key == NULL) {
325 wpa_printf(MSG_DEBUG, "AUTH_GET_MSK: Key is null, eapol_sm: %p",
326 sta->eapol_sm);
327 return -1;
328 }
329
330 if (keylen > *len)
331 keylen = *len;
332 os_memcpy(msk, key, keylen);
333 *len = keylen;
334
335 return 0;
336 }
337
338
339 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
340 const u8 *addr, int idx, u8 *key,
341 size_t key_len)
342 {
343 struct hostapd_data *hapd = ctx;
344 const char *ifname = hapd->conf->iface;
345
346 if (vlan_id > 0) {
347 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
348 if (ifname == NULL)
349 return -1;
350 }
351
352 #ifdef CONFIG_TESTING_OPTIONS
353 if (addr && !is_broadcast_ether_addr(addr)) {
354 struct sta_info *sta;
355
356 sta = ap_get_sta(hapd, addr);
357 if (sta) {
358 sta->last_tk_alg = alg;
359 sta->last_tk_key_idx = idx;
360 if (key)
361 os_memcpy(sta->last_tk, key, key_len);
362 sta->last_tk_len = key_len;
363 }
364 #ifdef CONFIG_IEEE80211W
365 } else if (alg == WPA_CIPHER_AES_128_CMAC ||
366 alg == WPA_CIPHER_BIP_GMAC_128 ||
367 alg == WPA_CIPHER_BIP_GMAC_256 ||
368 alg == WPA_CIPHER_BIP_CMAC_256) {
369 hapd->last_igtk_alg = alg;
370 hapd->last_igtk_key_idx = idx;
371 if (key)
372 os_memcpy(hapd->last_igtk, key, key_len);
373 hapd->last_igtk_len = key_len;
374 #endif /* CONFIG_IEEE80211W */
375 } else {
376 hapd->last_gtk_alg = alg;
377 hapd->last_gtk_key_idx = idx;
378 if (key)
379 os_memcpy(hapd->last_gtk, key, key_len);
380 hapd->last_gtk_len = key_len;
381 }
382 #endif /* CONFIG_TESTING_OPTIONS */
383 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
384 key, key_len);
385 }
386
387
388 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
389 u8 *seq)
390 {
391 struct hostapd_data *hapd = ctx;
392 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
393 }
394
395
396 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
397 const u8 *data, size_t data_len,
398 int encrypt)
399 {
400 struct hostapd_data *hapd = ctx;
401 struct sta_info *sta;
402 u32 flags = 0;
403
404 #ifdef CONFIG_TESTING_OPTIONS
405 if (hapd->ext_eapol_frame_io) {
406 size_t hex_len = 2 * data_len + 1;
407 char *hex = os_malloc(hex_len);
408
409 if (hex == NULL)
410 return -1;
411 wpa_snprintf_hex(hex, hex_len, data, data_len);
412 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
413 MAC2STR(addr), hex);
414 os_free(hex);
415 return 0;
416 }
417 #endif /* CONFIG_TESTING_OPTIONS */
418
419 sta = ap_get_sta(hapd, addr);
420 if (sta)
421 flags = hostapd_sta_flags_to_drv(sta->flags);
422
423 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
424 encrypt, flags);
425 }
426
427
428 static int hostapd_wpa_auth_for_each_sta(
429 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
430 void *cb_ctx)
431 {
432 struct hostapd_data *hapd = ctx;
433 struct sta_info *sta;
434
435 for (sta = hapd->sta_list; sta; sta = sta->next) {
436 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
437 return 1;
438 }
439 return 0;
440 }
441
442
443 struct wpa_auth_iface_iter_data {
444 int (*cb)(struct wpa_authenticator *sm, void *ctx);
445 void *cb_ctx;
446 };
447
448 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
449 {
450 struct wpa_auth_iface_iter_data *data = ctx;
451 size_t i;
452 for (i = 0; i < iface->num_bss; i++) {
453 if (iface->bss[i]->wpa_auth &&
454 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
455 return 1;
456 }
457 return 0;
458 }
459
460
461 static int hostapd_wpa_auth_for_each_auth(
462 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
463 void *cb_ctx)
464 {
465 struct hostapd_data *hapd = ctx;
466 struct wpa_auth_iface_iter_data data;
467 if (hapd->iface->interfaces == NULL ||
468 hapd->iface->interfaces->for_each_interface == NULL)
469 return -1;
470 data.cb = cb;
471 data.cb_ctx = cb_ctx;
472 return hapd->iface->interfaces->for_each_interface(
473 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
474 }
475
476
477 #ifdef CONFIG_IEEE80211R_AP
478
479 struct wpa_ft_rrb_rx_later_data {
480 struct dl_list list;
481 u8 addr[ETH_ALEN];
482 size_t data_len;
483 /* followed by data_len octets of data */
484 };
485
486 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
487 {
488 struct hostapd_data *hapd = eloop_ctx;
489 struct wpa_ft_rrb_rx_later_data *data, *n;
490
491 dl_list_for_each_safe(data, n, &hapd->l2_queue,
492 struct wpa_ft_rrb_rx_later_data, list) {
493 if (hapd->wpa_auth) {
494 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
495 (const u8 *) (data + 1),
496 data->data_len);
497 }
498 dl_list_del(&data->list);
499 os_free(data);
500 }
501 }
502
503
504 struct wpa_auth_ft_iface_iter_data {
505 struct hostapd_data *src_hapd;
506 const u8 *dst;
507 const u8 *data;
508 size_t data_len;
509 };
510
511
512 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
513 {
514 struct wpa_auth_ft_iface_iter_data *idata = ctx;
515 struct wpa_ft_rrb_rx_later_data *data;
516 struct hostapd_data *hapd;
517 size_t j;
518
519 for (j = 0; j < iface->num_bss; j++) {
520 hapd = iface->bss[j];
521 if (hapd == idata->src_hapd ||
522 !hapd->wpa_auth ||
523 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
524 continue;
525
526 wpa_printf(MSG_DEBUG,
527 "FT: Send RRB data directly to locally managed BSS "
528 MACSTR "@%s -> " MACSTR "@%s",
529 MAC2STR(idata->src_hapd->own_addr),
530 idata->src_hapd->conf->iface,
531 MAC2STR(hapd->own_addr), hapd->conf->iface);
532
533 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
534 * when it would be triggered when reading from a socket.
535 * This avoids
536 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
537 * that is calling hapd0:recv handler from within
538 * hapd0:send directly.
539 */
540 data = os_zalloc(sizeof(*data) + idata->data_len);
541 if (!data)
542 return 1;
543
544 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
545 os_memcpy(data + 1, idata->data, idata->data_len);
546 data->data_len = idata->data_len;
547
548 dl_list_add(&hapd->l2_queue, &data->list);
549
550 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
551 hapd, NULL))
552 eloop_register_timeout(0, 0,
553 hostapd_wpa_ft_rrb_rx_later,
554 hapd, NULL);
555
556 return 1;
557 }
558
559 return 0;
560 }
561
562 #endif /* CONFIG_IEEE80211R_AP */
563
564
565 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
566 const u8 *data, size_t data_len)
567 {
568 struct hostapd_data *hapd = ctx;
569 struct l2_ethhdr *buf;
570 int ret;
571
572 #ifdef CONFIG_TESTING_OPTIONS
573 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
574 size_t hex_len = 2 * data_len + 1;
575 char *hex = os_malloc(hex_len);
576
577 if (hex == NULL)
578 return -1;
579 wpa_snprintf_hex(hex, hex_len, data, data_len);
580 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
581 MAC2STR(dst), hex);
582 os_free(hex);
583 return 0;
584 }
585 #endif /* CONFIG_TESTING_OPTIONS */
586
587 #ifdef CONFIG_IEEE80211R_AP
588 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
589 hapd->iface->interfaces->for_each_interface) {
590 int res;
591 struct wpa_auth_ft_iface_iter_data idata;
592 idata.src_hapd = hapd;
593 idata.dst = dst;
594 idata.data = data;
595 idata.data_len = data_len;
596 res = hapd->iface->interfaces->for_each_interface(
597 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
598 &idata);
599 if (res == 1)
600 return data_len;
601 }
602 #endif /* CONFIG_IEEE80211R_AP */
603
604 if (hapd->driver && hapd->driver->send_ether)
605 return hapd->driver->send_ether(hapd->drv_priv, dst,
606 hapd->own_addr, proto,
607 data, data_len);
608 if (hapd->l2 == NULL)
609 return -1;
610
611 buf = os_malloc(sizeof(*buf) + data_len);
612 if (buf == NULL)
613 return -1;
614 os_memcpy(buf->h_dest, dst, ETH_ALEN);
615 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
616 buf->h_proto = host_to_be16(proto);
617 os_memcpy(buf + 1, data, data_len);
618 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
619 sizeof(*buf) + data_len);
620 os_free(buf);
621 return ret;
622 }
623
624
625 #ifdef CONFIG_ETH_P_OUI
626 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
627 u8 oui_suffix)
628 {
629 switch (oui_suffix) {
630 #ifdef CONFIG_IEEE80211R_AP
631 case FT_PACKET_R0KH_R1KH_PULL:
632 return hapd->oui_pull;
633 case FT_PACKET_R0KH_R1KH_RESP:
634 return hapd->oui_resp;
635 case FT_PACKET_R0KH_R1KH_PUSH:
636 return hapd->oui_push;
637 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
638 return hapd->oui_sreq;
639 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
640 return hapd->oui_sresp;
641 #endif /* CONFIG_IEEE80211R_AP */
642 default:
643 return NULL;
644 }
645 }
646 #endif /* CONFIG_ETH_P_OUI */
647
648
649 #ifdef CONFIG_IEEE80211R_AP
650
651 struct oui_deliver_later_data {
652 struct dl_list list;
653 u8 src_addr[ETH_ALEN];
654 u8 dst_addr[ETH_ALEN];
655 size_t data_len;
656 u8 oui_suffix;
657 /* followed by data_len octets of data */
658 };
659
660 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
661 {
662 struct hostapd_data *hapd = eloop_ctx;
663 struct oui_deliver_later_data *data, *n;
664 struct eth_p_oui_ctx *oui_ctx;
665
666 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
667 struct oui_deliver_later_data, list) {
668 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
669 if (hapd->wpa_auth && oui_ctx) {
670 eth_p_oui_deliver(oui_ctx, data->src_addr,
671 data->dst_addr,
672 (const u8 *) (data + 1),
673 data->data_len);
674 }
675 dl_list_del(&data->list);
676 os_free(data);
677 }
678 }
679
680
681 struct wpa_auth_oui_iface_iter_data {
682 struct hostapd_data *src_hapd;
683 const u8 *dst_addr;
684 const u8 *data;
685 size_t data_len;
686 u8 oui_suffix;
687 };
688
689 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
690 {
691 struct wpa_auth_oui_iface_iter_data *idata = ctx;
692 struct oui_deliver_later_data *data;
693 struct hostapd_data *hapd;
694 size_t j;
695
696 for (j = 0; j < iface->num_bss; j++) {
697 hapd = iface->bss[j];
698 if (hapd == idata->src_hapd)
699 continue;
700 if (!is_multicast_ether_addr(idata->dst_addr) &&
701 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
702 continue;
703
704 /* defer eth_p_oui_deliver until next eloop step as this is
705 * when it would be triggerd from reading from sock
706 * This avoids
707 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
708 * that is calling hapd0:recv handler from within
709 * hapd0:send directly.
710 */
711 data = os_zalloc(sizeof(*data) + idata->data_len);
712 if (!data)
713 return 1;
714
715 os_memcpy(data->src_addr, idata->src_hapd->own_addr, ETH_ALEN);
716 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
717 os_memcpy(data + 1, idata->data, idata->data_len);
718 data->data_len = idata->data_len;
719 data->oui_suffix = idata->oui_suffix;
720
721 dl_list_add(&hapd->l2_oui_queue, &data->list);
722
723 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
724 hapd, NULL))
725 eloop_register_timeout(0, 0,
726 hostapd_oui_deliver_later,
727 hapd, NULL);
728
729 return 1;
730 }
731
732 return 0;
733 }
734
735 #endif /* CONFIG_IEEE80211R_AP */
736
737
738 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
739 const u8 *data, size_t data_len)
740 {
741 #ifdef CONFIG_ETH_P_OUI
742 struct hostapd_data *hapd = ctx;
743 struct eth_p_oui_ctx *oui_ctx;
744
745 #ifdef CONFIG_IEEE80211R_AP
746 if (hapd->iface->interfaces &&
747 hapd->iface->interfaces->for_each_interface) {
748 struct wpa_auth_oui_iface_iter_data idata;
749 int res;
750
751 idata.src_hapd = hapd;
752 idata.dst_addr = dst;
753 idata.data = data;
754 idata.data_len = data_len;
755 idata.oui_suffix = oui_suffix;
756 res = hapd->iface->interfaces->for_each_interface(
757 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
758 &idata);
759 if (res == 1)
760 return data_len;
761 }
762 #endif /* CONFIG_IEEE80211R_AP */
763
764 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
765 if (!oui_ctx)
766 return -1;
767
768 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
769 #else /* CONFIG_ETH_P_OUI */
770 return -1;
771 #endif /* CONFIG_ETH_P_OUI */
772 }
773
774
775 #ifdef CONFIG_IEEE80211R_AP
776
777 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
778 const u8 *data, size_t data_len)
779 {
780 struct hostapd_data *hapd = ctx;
781 int res;
782 struct ieee80211_mgmt *m;
783 size_t mlen;
784 struct sta_info *sta;
785
786 sta = ap_get_sta(hapd, dst);
787 if (sta == NULL || sta->wpa_sm == NULL)
788 return -1;
789
790 m = os_zalloc(sizeof(*m) + data_len);
791 if (m == NULL)
792 return -1;
793 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
794 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
795 WLAN_FC_STYPE_ACTION);
796 os_memcpy(m->da, dst, ETH_ALEN);
797 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
798 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
799 os_memcpy(&m->u, data, data_len);
800
801 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
802 os_free(m);
803 return res;
804 }
805
806
807 static struct wpa_state_machine *
808 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
809 {
810 struct hostapd_data *hapd = ctx;
811 struct sta_info *sta;
812
813 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
814 return NULL;
815
816 sta = ap_sta_add(hapd, sta_addr);
817 if (sta == NULL)
818 return NULL;
819 if (sta->wpa_sm) {
820 sta->auth_alg = WLAN_AUTH_FT;
821 return sta->wpa_sm;
822 }
823
824 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
825 if (sta->wpa_sm == NULL) {
826 ap_free_sta(hapd, sta);
827 return NULL;
828 }
829 sta->auth_alg = WLAN_AUTH_FT;
830
831 return sta->wpa_sm;
832 }
833
834
835 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
836 size_t len)
837 {
838 struct hostapd_data *hapd = ctx;
839 struct l2_ethhdr *ethhdr;
840 if (len < sizeof(*ethhdr))
841 return;
842 ethhdr = (struct l2_ethhdr *) buf;
843 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
844 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
845 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
846 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
847 return;
848 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
849 len - sizeof(*ethhdr));
850 }
851
852
853 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
854 const u8 *dst_addr, u8 oui_suffix,
855 const u8 *buf, size_t len)
856 {
857 struct hostapd_data *hapd = ctx;
858
859 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
860 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
861 if (!is_multicast_ether_addr(dst_addr) &&
862 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
863 return;
864 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
865 len);
866 }
867
868
869 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
870 u8 *tspec_ie, size_t tspec_ielen)
871 {
872 struct hostapd_data *hapd = ctx;
873 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
874 }
875
876
877
878 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
879 const char *ft_iface)
880 {
881 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
882 FT_PACKET_R0KH_R1KH_PULL,
883 hostapd_rrb_oui_receive, hapd);
884 if (!hapd->oui_pull)
885 return -1;
886
887 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
888 FT_PACKET_R0KH_R1KH_RESP,
889 hostapd_rrb_oui_receive, hapd);
890 if (!hapd->oui_resp)
891 return -1;
892
893 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
894 FT_PACKET_R0KH_R1KH_PUSH,
895 hostapd_rrb_oui_receive, hapd);
896 if (!hapd->oui_push)
897 return -1;
898
899 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
900 FT_PACKET_R0KH_R1KH_SEQ_REQ,
901 hostapd_rrb_oui_receive, hapd);
902 if (!hapd->oui_sreq)
903 return -1;
904
905 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
906 FT_PACKET_R0KH_R1KH_SEQ_RESP,
907 hostapd_rrb_oui_receive, hapd);
908 if (!hapd->oui_sresp)
909 return -1;
910
911 return 0;
912 }
913
914
915 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
916 {
917 eth_p_oui_unregister(hapd->oui_pull);
918 hapd->oui_pull = NULL;
919 eth_p_oui_unregister(hapd->oui_resp);
920 hapd->oui_resp = NULL;
921 eth_p_oui_unregister(hapd->oui_push);
922 hapd->oui_push = NULL;
923 eth_p_oui_unregister(hapd->oui_sreq);
924 hapd->oui_sreq = NULL;
925 eth_p_oui_unregister(hapd->oui_sresp);
926 hapd->oui_sresp = NULL;
927 }
928 #endif /* CONFIG_IEEE80211R_AP */
929
930
931 int hostapd_setup_wpa(struct hostapd_data *hapd)
932 {
933 struct wpa_auth_config _conf;
934 static const struct wpa_auth_callbacks cb = {
935 .logger = hostapd_wpa_auth_logger,
936 .disconnect = hostapd_wpa_auth_disconnect,
937 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
938 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
939 .set_eapol = hostapd_wpa_auth_set_eapol,
940 .get_eapol = hostapd_wpa_auth_get_eapol,
941 .get_psk = hostapd_wpa_auth_get_psk,
942 .get_msk = hostapd_wpa_auth_get_msk,
943 .set_key = hostapd_wpa_auth_set_key,
944 .get_seqnum = hostapd_wpa_auth_get_seqnum,
945 .send_eapol = hostapd_wpa_auth_send_eapol,
946 .for_each_sta = hostapd_wpa_auth_for_each_sta,
947 .for_each_auth = hostapd_wpa_auth_for_each_auth,
948 .send_ether = hostapd_wpa_auth_send_ether,
949 .send_oui = hostapd_wpa_auth_send_oui,
950 #ifdef CONFIG_IEEE80211R_AP
951 .send_ft_action = hostapd_wpa_auth_send_ft_action,
952 .add_sta = hostapd_wpa_auth_add_sta,
953 .add_tspec = hostapd_wpa_auth_add_tspec,
954 #endif /* CONFIG_IEEE80211R_AP */
955 };
956 const u8 *wpa_ie;
957 size_t wpa_ie_len;
958
959 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
960 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
961 _conf.tx_status = 1;
962 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
963 _conf.ap_mlme = 1;
964 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
965 if (hapd->wpa_auth == NULL) {
966 wpa_printf(MSG_ERROR, "WPA initialization failed.");
967 return -1;
968 }
969
970 if (hostapd_set_privacy(hapd, 1)) {
971 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
972 "for interface %s", hapd->conf->iface);
973 return -1;
974 }
975
976 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
977 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
978 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
979 "the kernel driver.");
980 return -1;
981 }
982
983 if (rsn_preauth_iface_init(hapd)) {
984 wpa_printf(MSG_ERROR, "Initialization of RSN "
985 "pre-authentication failed.");
986 return -1;
987 }
988
989 #ifdef CONFIG_IEEE80211R_AP
990 if (!hostapd_drv_none(hapd) &&
991 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
992 const char *ft_iface;
993
994 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
995 hapd->conf->iface;
996 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
997 hostapd_rrb_receive, hapd, 1);
998 if (hapd->l2 == NULL &&
999 (hapd->driver == NULL ||
1000 hapd->driver->send_ether == NULL)) {
1001 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1002 "interface");
1003 return -1;
1004 }
1005
1006 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
1007 wpa_printf(MSG_ERROR,
1008 "Failed to open ETH_P_OUI interface");
1009 return -1;
1010 }
1011 }
1012 #endif /* CONFIG_IEEE80211R_AP */
1013
1014 return 0;
1015
1016 }
1017
1018
1019 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
1020 {
1021 struct wpa_auth_config wpa_auth_conf;
1022 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
1023 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
1024 }
1025
1026
1027 void hostapd_deinit_wpa(struct hostapd_data *hapd)
1028 {
1029 ieee80211_tkip_countermeasures_deinit(hapd);
1030 rsn_preauth_iface_deinit(hapd);
1031 if (hapd->wpa_auth) {
1032 wpa_deinit(hapd->wpa_auth);
1033 hapd->wpa_auth = NULL;
1034
1035 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1036 wpa_printf(MSG_DEBUG, "Could not disable "
1037 "PrivacyInvoked for interface %s",
1038 hapd->conf->iface);
1039 }
1040
1041 if (hapd->drv_priv &&
1042 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1043 wpa_printf(MSG_DEBUG, "Could not remove generic "
1044 "information element from interface %s",
1045 hapd->conf->iface);
1046 }
1047 }
1048 ieee802_1x_deinit(hapd);
1049
1050 #ifdef CONFIG_IEEE80211R_AP
1051 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1052 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1053 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1054 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1055 l2_packet_deinit(hapd->l2);
1056 hapd->l2 = NULL;
1057 hostapd_wpa_unregister_ft_oui(hapd);
1058 #endif /* CONFIG_IEEE80211R_AP */
1059 }