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