]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth_glue.c
Remove all PeerKey functionality
[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 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
353 key, key_len);
354 }
355
356
357 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
358 u8 *seq)
359 {
360 struct hostapd_data *hapd = ctx;
361 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
362 }
363
364
365 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
366 const u8 *data, size_t data_len,
367 int encrypt)
368 {
369 struct hostapd_data *hapd = ctx;
370 struct sta_info *sta;
371 u32 flags = 0;
372
373 #ifdef CONFIG_TESTING_OPTIONS
374 if (hapd->ext_eapol_frame_io) {
375 size_t hex_len = 2 * data_len + 1;
376 char *hex = os_malloc(hex_len);
377
378 if (hex == NULL)
379 return -1;
380 wpa_snprintf_hex(hex, hex_len, data, data_len);
381 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
382 MAC2STR(addr), hex);
383 os_free(hex);
384 return 0;
385 }
386 #endif /* CONFIG_TESTING_OPTIONS */
387
388 sta = ap_get_sta(hapd, addr);
389 if (sta)
390 flags = hostapd_sta_flags_to_drv(sta->flags);
391
392 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
393 encrypt, flags);
394 }
395
396
397 static int hostapd_wpa_auth_for_each_sta(
398 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
399 void *cb_ctx)
400 {
401 struct hostapd_data *hapd = ctx;
402 struct sta_info *sta;
403
404 for (sta = hapd->sta_list; sta; sta = sta->next) {
405 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
406 return 1;
407 }
408 return 0;
409 }
410
411
412 struct wpa_auth_iface_iter_data {
413 int (*cb)(struct wpa_authenticator *sm, void *ctx);
414 void *cb_ctx;
415 };
416
417 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
418 {
419 struct wpa_auth_iface_iter_data *data = ctx;
420 size_t i;
421 for (i = 0; i < iface->num_bss; i++) {
422 if (iface->bss[i]->wpa_auth &&
423 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
424 return 1;
425 }
426 return 0;
427 }
428
429
430 static int hostapd_wpa_auth_for_each_auth(
431 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
432 void *cb_ctx)
433 {
434 struct hostapd_data *hapd = ctx;
435 struct wpa_auth_iface_iter_data data;
436 if (hapd->iface->interfaces == NULL ||
437 hapd->iface->interfaces->for_each_interface == NULL)
438 return -1;
439 data.cb = cb;
440 data.cb_ctx = cb_ctx;
441 return hapd->iface->interfaces->for_each_interface(
442 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
443 }
444
445
446 #ifdef CONFIG_IEEE80211R_AP
447
448 struct wpa_ft_rrb_rx_later_data {
449 struct dl_list list;
450 u8 addr[ETH_ALEN];
451 size_t data_len;
452 /* followed by data_len octets of data */
453 };
454
455 static void hostapd_wpa_ft_rrb_rx_later(void *eloop_ctx, void *timeout_ctx)
456 {
457 struct hostapd_data *hapd = eloop_ctx;
458 struct wpa_ft_rrb_rx_later_data *data, *n;
459
460 dl_list_for_each_safe(data, n, &hapd->l2_queue,
461 struct wpa_ft_rrb_rx_later_data, list) {
462 if (hapd->wpa_auth) {
463 wpa_ft_rrb_rx(hapd->wpa_auth, data->addr,
464 (const u8 *) (data + 1),
465 data->data_len);
466 }
467 dl_list_del(&data->list);
468 os_free(data);
469 }
470 }
471
472
473 struct wpa_auth_ft_iface_iter_data {
474 struct hostapd_data *src_hapd;
475 const u8 *dst;
476 const u8 *data;
477 size_t data_len;
478 };
479
480
481 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
482 {
483 struct wpa_auth_ft_iface_iter_data *idata = ctx;
484 struct wpa_ft_rrb_rx_later_data *data;
485 struct hostapd_data *hapd;
486 size_t j;
487
488 for (j = 0; j < iface->num_bss; j++) {
489 hapd = iface->bss[j];
490 if (hapd == idata->src_hapd ||
491 !hapd->wpa_auth ||
492 os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) != 0)
493 continue;
494
495 wpa_printf(MSG_DEBUG,
496 "FT: Send RRB data directly to locally managed BSS "
497 MACSTR "@%s -> " MACSTR "@%s",
498 MAC2STR(idata->src_hapd->own_addr),
499 idata->src_hapd->conf->iface,
500 MAC2STR(hapd->own_addr), hapd->conf->iface);
501
502 /* Defer wpa_ft_rrb_rx() until next eloop step as this is
503 * when it would be triggered when reading from a socket.
504 * This avoids
505 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
506 * that is calling hapd0:recv handler from within
507 * hapd0:send directly.
508 */
509 data = os_zalloc(sizeof(*data) + idata->data_len);
510 if (!data)
511 return 1;
512
513 os_memcpy(data->addr, idata->src_hapd->own_addr, ETH_ALEN);
514 os_memcpy(data + 1, idata->data, idata->data_len);
515 data->data_len = idata->data_len;
516
517 dl_list_add(&hapd->l2_queue, &data->list);
518
519 if (!eloop_is_timeout_registered(hostapd_wpa_ft_rrb_rx_later,
520 hapd, NULL))
521 eloop_register_timeout(0, 0,
522 hostapd_wpa_ft_rrb_rx_later,
523 hapd, NULL);
524
525 return 1;
526 }
527
528 return 0;
529 }
530
531 #endif /* CONFIG_IEEE80211R_AP */
532
533
534 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
535 const u8 *data, size_t data_len)
536 {
537 struct hostapd_data *hapd = ctx;
538 struct l2_ethhdr *buf;
539 int ret;
540
541 #ifdef CONFIG_TESTING_OPTIONS
542 if (hapd->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
543 size_t hex_len = 2 * data_len + 1;
544 char *hex = os_malloc(hex_len);
545
546 if (hex == NULL)
547 return -1;
548 wpa_snprintf_hex(hex, hex_len, data, data_len);
549 wpa_msg(hapd->msg_ctx, MSG_INFO, "EAPOL-TX " MACSTR " %s",
550 MAC2STR(dst), hex);
551 os_free(hex);
552 return 0;
553 }
554 #endif /* CONFIG_TESTING_OPTIONS */
555
556 #ifdef CONFIG_IEEE80211R_AP
557 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
558 hapd->iface->interfaces->for_each_interface) {
559 int res;
560 struct wpa_auth_ft_iface_iter_data idata;
561 idata.src_hapd = hapd;
562 idata.dst = dst;
563 idata.data = data;
564 idata.data_len = data_len;
565 res = hapd->iface->interfaces->for_each_interface(
566 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
567 &idata);
568 if (res == 1)
569 return data_len;
570 }
571 #endif /* CONFIG_IEEE80211R_AP */
572
573 if (hapd->driver && hapd->driver->send_ether)
574 return hapd->driver->send_ether(hapd->drv_priv, dst,
575 hapd->own_addr, proto,
576 data, data_len);
577 if (hapd->l2 == NULL)
578 return -1;
579
580 buf = os_malloc(sizeof(*buf) + data_len);
581 if (buf == NULL)
582 return -1;
583 os_memcpy(buf->h_dest, dst, ETH_ALEN);
584 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
585 buf->h_proto = host_to_be16(proto);
586 os_memcpy(buf + 1, data, data_len);
587 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
588 sizeof(*buf) + data_len);
589 os_free(buf);
590 return ret;
591 }
592
593
594 #ifdef CONFIG_ETH_P_OUI
595 static struct eth_p_oui_ctx * hostapd_wpa_get_oui(struct hostapd_data *hapd,
596 u8 oui_suffix)
597 {
598 switch (oui_suffix) {
599 #ifdef CONFIG_IEEE80211R_AP
600 case FT_PACKET_R0KH_R1KH_PULL:
601 return hapd->oui_pull;
602 case FT_PACKET_R0KH_R1KH_RESP:
603 return hapd->oui_resp;
604 case FT_PACKET_R0KH_R1KH_PUSH:
605 return hapd->oui_push;
606 case FT_PACKET_R0KH_R1KH_SEQ_REQ:
607 return hapd->oui_sreq;
608 case FT_PACKET_R0KH_R1KH_SEQ_RESP:
609 return hapd->oui_sresp;
610 #endif /* CONFIG_IEEE80211R_AP */
611 default:
612 return NULL;
613 }
614 }
615 #endif /* CONFIG_ETH_P_OUI */
616
617
618 #ifdef CONFIG_IEEE80211R_AP
619
620 struct oui_deliver_later_data {
621 struct dl_list list;
622 u8 src_addr[ETH_ALEN];
623 u8 dst_addr[ETH_ALEN];
624 size_t data_len;
625 u8 oui_suffix;
626 /* followed by data_len octets of data */
627 };
628
629 static void hostapd_oui_deliver_later(void *eloop_ctx, void *timeout_ctx)
630 {
631 struct hostapd_data *hapd = eloop_ctx;
632 struct oui_deliver_later_data *data, *n;
633 struct eth_p_oui_ctx *oui_ctx;
634
635 dl_list_for_each_safe(data, n, &hapd->l2_oui_queue,
636 struct oui_deliver_later_data, list) {
637 oui_ctx = hostapd_wpa_get_oui(hapd, data->oui_suffix);
638 if (hapd->wpa_auth && oui_ctx) {
639 eth_p_oui_deliver(oui_ctx, data->src_addr,
640 data->dst_addr,
641 (const u8 *) (data + 1),
642 data->data_len);
643 }
644 dl_list_del(&data->list);
645 os_free(data);
646 }
647 }
648
649
650 struct wpa_auth_oui_iface_iter_data {
651 struct hostapd_data *src_hapd;
652 const u8 *dst_addr;
653 const u8 *data;
654 size_t data_len;
655 u8 oui_suffix;
656 };
657
658 static int hostapd_wpa_auth_oui_iter(struct hostapd_iface *iface, void *ctx)
659 {
660 struct wpa_auth_oui_iface_iter_data *idata = ctx;
661 struct oui_deliver_later_data *data;
662 struct hostapd_data *hapd;
663 size_t j;
664
665 for (j = 0; j < iface->num_bss; j++) {
666 hapd = iface->bss[j];
667 if (hapd == idata->src_hapd)
668 continue;
669 if (!is_multicast_ether_addr(idata->dst_addr) &&
670 os_memcmp(hapd->own_addr, idata->dst_addr, ETH_ALEN) != 0)
671 continue;
672
673 /* defer eth_p_oui_deliver until next eloop step as this is
674 * when it would be triggerd from reading from sock
675 * This avoids
676 * hapd0:send -> hapd1:recv -> hapd1:send -> hapd0:recv,
677 * that is calling hapd0:recv handler from within
678 * hapd0:send directly.
679 */
680 data = os_zalloc(sizeof(*data) + idata->data_len);
681 if (!data)
682 return 1;
683
684 os_memcpy(data->src_addr, idata->src_hapd->own_addr, ETH_ALEN);
685 os_memcpy(data->dst_addr, idata->dst_addr, ETH_ALEN);
686 os_memcpy(data + 1, idata->data, idata->data_len);
687 data->data_len = idata->data_len;
688 data->oui_suffix = idata->oui_suffix;
689
690 dl_list_add(&hapd->l2_oui_queue, &data->list);
691
692 if (!eloop_is_timeout_registered(hostapd_oui_deliver_later,
693 hapd, NULL))
694 eloop_register_timeout(0, 0,
695 hostapd_oui_deliver_later,
696 hapd, NULL);
697
698 return 1;
699 }
700
701 return 0;
702 }
703
704 #endif /* CONFIG_IEEE80211R_AP */
705
706
707 static int hostapd_wpa_auth_send_oui(void *ctx, const u8 *dst, u8 oui_suffix,
708 const u8 *data, size_t data_len)
709 {
710 #ifdef CONFIG_ETH_P_OUI
711 struct hostapd_data *hapd = ctx;
712 struct eth_p_oui_ctx *oui_ctx;
713
714 #ifdef CONFIG_IEEE80211R_AP
715 if (hapd->iface->interfaces &&
716 hapd->iface->interfaces->for_each_interface) {
717 struct wpa_auth_oui_iface_iter_data idata;
718 int res;
719
720 idata.src_hapd = hapd;
721 idata.dst_addr = dst;
722 idata.data = data;
723 idata.data_len = data_len;
724 idata.oui_suffix = oui_suffix;
725 res = hapd->iface->interfaces->for_each_interface(
726 hapd->iface->interfaces, hostapd_wpa_auth_oui_iter,
727 &idata);
728 if (res == 1)
729 return data_len;
730 }
731 #endif /* CONFIG_IEEE80211R_AP */
732
733 oui_ctx = hostapd_wpa_get_oui(hapd, oui_suffix);
734 if (!oui_ctx)
735 return -1;
736
737 return eth_p_oui_send(oui_ctx, hapd->own_addr, dst, data, data_len);
738 #else /* CONFIG_ETH_P_OUI */
739 return -1;
740 #endif /* CONFIG_ETH_P_OUI */
741 }
742
743
744 #ifdef CONFIG_IEEE80211R_AP
745
746 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
747 const u8 *data, size_t data_len)
748 {
749 struct hostapd_data *hapd = ctx;
750 int res;
751 struct ieee80211_mgmt *m;
752 size_t mlen;
753 struct sta_info *sta;
754
755 sta = ap_get_sta(hapd, dst);
756 if (sta == NULL || sta->wpa_sm == NULL)
757 return -1;
758
759 m = os_zalloc(sizeof(*m) + data_len);
760 if (m == NULL)
761 return -1;
762 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
763 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
764 WLAN_FC_STYPE_ACTION);
765 os_memcpy(m->da, dst, ETH_ALEN);
766 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
767 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
768 os_memcpy(&m->u, data, data_len);
769
770 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
771 os_free(m);
772 return res;
773 }
774
775
776 static struct wpa_state_machine *
777 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
778 {
779 struct hostapd_data *hapd = ctx;
780 struct sta_info *sta;
781
782 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
783 return NULL;
784
785 sta = ap_sta_add(hapd, sta_addr);
786 if (sta == NULL)
787 return NULL;
788 if (sta->wpa_sm) {
789 sta->auth_alg = WLAN_AUTH_FT;
790 return sta->wpa_sm;
791 }
792
793 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr, NULL);
794 if (sta->wpa_sm == NULL) {
795 ap_free_sta(hapd, sta);
796 return NULL;
797 }
798 sta->auth_alg = WLAN_AUTH_FT;
799
800 return sta->wpa_sm;
801 }
802
803
804 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
805 size_t len)
806 {
807 struct hostapd_data *hapd = ctx;
808 struct l2_ethhdr *ethhdr;
809 if (len < sizeof(*ethhdr))
810 return;
811 ethhdr = (struct l2_ethhdr *) buf;
812 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
813 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
814 if (!is_multicast_ether_addr(ethhdr->h_dest) &&
815 os_memcmp(hapd->own_addr, ethhdr->h_dest, ETH_ALEN) != 0)
816 return;
817 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
818 len - sizeof(*ethhdr));
819 }
820
821
822 static void hostapd_rrb_oui_receive(void *ctx, const u8 *src_addr,
823 const u8 *dst_addr, u8 oui_suffix,
824 const u8 *buf, size_t len)
825 {
826 struct hostapd_data *hapd = ctx;
827
828 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
829 MACSTR, MAC2STR(src_addr), MAC2STR(dst_addr));
830 if (!is_multicast_ether_addr(dst_addr) &&
831 os_memcmp(hapd->own_addr, dst_addr, ETH_ALEN) != 0)
832 return;
833 wpa_ft_rrb_oui_rx(hapd->wpa_auth, src_addr, dst_addr, oui_suffix, buf,
834 len);
835 }
836
837
838 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
839 u8 *tspec_ie, size_t tspec_ielen)
840 {
841 struct hostapd_data *hapd = ctx;
842 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
843 }
844
845
846
847 static int hostapd_wpa_register_ft_oui(struct hostapd_data *hapd,
848 const char *ft_iface)
849 {
850 hapd->oui_pull = eth_p_oui_register(hapd, ft_iface,
851 FT_PACKET_R0KH_R1KH_PULL,
852 hostapd_rrb_oui_receive, hapd);
853 if (!hapd->oui_pull)
854 return -1;
855
856 hapd->oui_resp = eth_p_oui_register(hapd, ft_iface,
857 FT_PACKET_R0KH_R1KH_RESP,
858 hostapd_rrb_oui_receive, hapd);
859 if (!hapd->oui_resp)
860 return -1;
861
862 hapd->oui_push = eth_p_oui_register(hapd, ft_iface,
863 FT_PACKET_R0KH_R1KH_PUSH,
864 hostapd_rrb_oui_receive, hapd);
865 if (!hapd->oui_push)
866 return -1;
867
868 hapd->oui_sreq = eth_p_oui_register(hapd, ft_iface,
869 FT_PACKET_R0KH_R1KH_SEQ_REQ,
870 hostapd_rrb_oui_receive, hapd);
871 if (!hapd->oui_sreq)
872 return -1;
873
874 hapd->oui_sresp = eth_p_oui_register(hapd, ft_iface,
875 FT_PACKET_R0KH_R1KH_SEQ_RESP,
876 hostapd_rrb_oui_receive, hapd);
877 if (!hapd->oui_sresp)
878 return -1;
879
880 return 0;
881 }
882
883
884 static void hostapd_wpa_unregister_ft_oui(struct hostapd_data *hapd)
885 {
886 eth_p_oui_unregister(hapd->oui_pull);
887 hapd->oui_pull = NULL;
888 eth_p_oui_unregister(hapd->oui_resp);
889 hapd->oui_resp = NULL;
890 eth_p_oui_unregister(hapd->oui_push);
891 hapd->oui_push = NULL;
892 eth_p_oui_unregister(hapd->oui_sreq);
893 hapd->oui_sreq = NULL;
894 eth_p_oui_unregister(hapd->oui_sresp);
895 hapd->oui_sresp = NULL;
896 }
897 #endif /* CONFIG_IEEE80211R_AP */
898
899
900 int hostapd_setup_wpa(struct hostapd_data *hapd)
901 {
902 struct wpa_auth_config _conf;
903 static const struct wpa_auth_callbacks cb = {
904 .logger = hostapd_wpa_auth_logger,
905 .disconnect = hostapd_wpa_auth_disconnect,
906 .mic_failure_report = hostapd_wpa_auth_mic_failure_report,
907 .psk_failure_report = hostapd_wpa_auth_psk_failure_report,
908 .set_eapol = hostapd_wpa_auth_set_eapol,
909 .get_eapol = hostapd_wpa_auth_get_eapol,
910 .get_psk = hostapd_wpa_auth_get_psk,
911 .get_msk = hostapd_wpa_auth_get_msk,
912 .set_key = hostapd_wpa_auth_set_key,
913 .get_seqnum = hostapd_wpa_auth_get_seqnum,
914 .send_eapol = hostapd_wpa_auth_send_eapol,
915 .for_each_sta = hostapd_wpa_auth_for_each_sta,
916 .for_each_auth = hostapd_wpa_auth_for_each_auth,
917 .send_ether = hostapd_wpa_auth_send_ether,
918 .send_oui = hostapd_wpa_auth_send_oui,
919 #ifdef CONFIG_IEEE80211R_AP
920 .send_ft_action = hostapd_wpa_auth_send_ft_action,
921 .add_sta = hostapd_wpa_auth_add_sta,
922 .add_tspec = hostapd_wpa_auth_add_tspec,
923 #endif /* CONFIG_IEEE80211R_AP */
924 };
925 const u8 *wpa_ie;
926 size_t wpa_ie_len;
927
928 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &_conf);
929 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
930 _conf.tx_status = 1;
931 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
932 _conf.ap_mlme = 1;
933 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb, hapd);
934 if (hapd->wpa_auth == NULL) {
935 wpa_printf(MSG_ERROR, "WPA initialization failed.");
936 return -1;
937 }
938
939 if (hostapd_set_privacy(hapd, 1)) {
940 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
941 "for interface %s", hapd->conf->iface);
942 return -1;
943 }
944
945 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
946 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
947 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
948 "the kernel driver.");
949 return -1;
950 }
951
952 if (rsn_preauth_iface_init(hapd)) {
953 wpa_printf(MSG_ERROR, "Initialization of RSN "
954 "pre-authentication failed.");
955 return -1;
956 }
957
958 #ifdef CONFIG_IEEE80211R_AP
959 if (!hostapd_drv_none(hapd) &&
960 wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt)) {
961 const char *ft_iface;
962
963 ft_iface = hapd->conf->bridge[0] ? hapd->conf->bridge :
964 hapd->conf->iface;
965 hapd->l2 = l2_packet_init(ft_iface, NULL, ETH_P_RRB,
966 hostapd_rrb_receive, hapd, 1);
967 if (hapd->l2 == NULL &&
968 (hapd->driver == NULL ||
969 hapd->driver->send_ether == NULL)) {
970 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
971 "interface");
972 return -1;
973 }
974
975 if (hostapd_wpa_register_ft_oui(hapd, ft_iface)) {
976 wpa_printf(MSG_ERROR,
977 "Failed to open ETH_P_OUI interface");
978 return -1;
979 }
980 }
981 #endif /* CONFIG_IEEE80211R_AP */
982
983 return 0;
984
985 }
986
987
988 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
989 {
990 struct wpa_auth_config wpa_auth_conf;
991 hostapd_wpa_auth_conf(hapd->conf, hapd->iconf, &wpa_auth_conf);
992 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
993 }
994
995
996 void hostapd_deinit_wpa(struct hostapd_data *hapd)
997 {
998 ieee80211_tkip_countermeasures_deinit(hapd);
999 rsn_preauth_iface_deinit(hapd);
1000 if (hapd->wpa_auth) {
1001 wpa_deinit(hapd->wpa_auth);
1002 hapd->wpa_auth = NULL;
1003
1004 if (hapd->drv_priv && hostapd_set_privacy(hapd, 0)) {
1005 wpa_printf(MSG_DEBUG, "Could not disable "
1006 "PrivacyInvoked for interface %s",
1007 hapd->conf->iface);
1008 }
1009
1010 if (hapd->drv_priv &&
1011 hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
1012 wpa_printf(MSG_DEBUG, "Could not remove generic "
1013 "information element from interface %s",
1014 hapd->conf->iface);
1015 }
1016 }
1017 ieee802_1x_deinit(hapd);
1018
1019 #ifdef CONFIG_IEEE80211R_AP
1020 eloop_cancel_timeout(hostapd_wpa_ft_rrb_rx_later, hapd, ELOOP_ALL_CTX);
1021 hostapd_wpa_ft_rrb_rx_later(hapd, NULL); /* flush without delivering */
1022 eloop_cancel_timeout(hostapd_oui_deliver_later, hapd, ELOOP_ALL_CTX);
1023 hostapd_oui_deliver_later(hapd, NULL); /* flush without delivering */
1024 l2_packet_deinit(hapd->l2);
1025 hapd->l2 = NULL;
1026 hostapd_wpa_unregister_ft_oui(hapd);
1027 #endif /* CONFIG_IEEE80211R_AP */
1028 }