]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth_glue.c
SAE: Use PMK in 4-way handshake
[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 "common/ieee802_11_defs.h"
13 #include "common/sae.h"
14 #include "eapol_auth/eapol_auth_sm.h"
15 #include "eapol_auth/eapol_auth_sm_i.h"
16 #include "eap_server/eap.h"
17 #include "l2_packet/l2_packet.h"
18 #include "drivers/driver.h"
19 #include "hostapd.h"
20 #include "ieee802_1x.h"
21 #include "preauth_auth.h"
22 #include "sta_info.h"
23 #include "tkip_countermeasures.h"
24 #include "ap_drv_ops.h"
25 #include "ap_config.h"
26 #include "wpa_auth.h"
27 #include "wpa_auth_glue.h"
28
29
30 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
31 struct wpa_auth_config *wconf)
32 {
33 os_memset(wconf, 0, sizeof(*wconf));
34 wconf->wpa = conf->wpa;
35 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
36 wconf->wpa_pairwise = conf->wpa_pairwise;
37 wconf->wpa_group = conf->wpa_group;
38 wconf->wpa_group_rekey = conf->wpa_group_rekey;
39 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
40 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
41 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
42 wconf->rsn_pairwise = conf->rsn_pairwise;
43 wconf->rsn_preauth = conf->rsn_preauth;
44 wconf->eapol_version = conf->eapol_version;
45 wconf->peerkey = conf->peerkey;
46 wconf->wmm_enabled = conf->wmm_enabled;
47 wconf->wmm_uapsd = conf->wmm_uapsd;
48 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
49 wconf->okc = conf->okc;
50 #ifdef CONFIG_IEEE80211W
51 wconf->ieee80211w = conf->ieee80211w;
52 #endif /* CONFIG_IEEE80211W */
53 #ifdef CONFIG_IEEE80211R
54 wconf->ssid_len = conf->ssid.ssid_len;
55 if (wconf->ssid_len > SSID_LEN)
56 wconf->ssid_len = SSID_LEN;
57 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
58 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
59 MOBILITY_DOMAIN_ID_LEN);
60 if (conf->nas_identifier &&
61 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
62 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
63 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
64 wconf->r0_key_holder_len);
65 }
66 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
67 wconf->r0_key_lifetime = conf->r0_key_lifetime;
68 wconf->reassociation_deadline = conf->reassociation_deadline;
69 wconf->r0kh_list = conf->r0kh_list;
70 wconf->r1kh_list = conf->r1kh_list;
71 wconf->pmk_r1_push = conf->pmk_r1_push;
72 wconf->ft_over_ds = conf->ft_over_ds;
73 #endif /* CONFIG_IEEE80211R */
74 #ifdef CONFIG_HS20
75 wconf->disable_gtk = conf->disable_dgaf;
76 #endif /* CONFIG_HS20 */
77 }
78
79
80 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
81 logger_level level, const char *txt)
82 {
83 #ifndef CONFIG_NO_HOSTAPD_LOGGER
84 struct hostapd_data *hapd = ctx;
85 int hlevel;
86
87 switch (level) {
88 case LOGGER_WARNING:
89 hlevel = HOSTAPD_LEVEL_WARNING;
90 break;
91 case LOGGER_INFO:
92 hlevel = HOSTAPD_LEVEL_INFO;
93 break;
94 case LOGGER_DEBUG:
95 default:
96 hlevel = HOSTAPD_LEVEL_DEBUG;
97 break;
98 }
99
100 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
101 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
102 }
103
104
105 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
106 u16 reason)
107 {
108 struct hostapd_data *hapd = ctx;
109 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
110 "STA " MACSTR " reason %d",
111 __func__, MAC2STR(addr), reason);
112 ap_sta_disconnect(hapd, NULL, addr, reason);
113 }
114
115
116 static int hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
117 {
118 struct hostapd_data *hapd = ctx;
119 return michael_mic_failure(hapd, addr, 0);
120 }
121
122
123 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
124 wpa_eapol_variable var, int value)
125 {
126 struct hostapd_data *hapd = ctx;
127 struct sta_info *sta = ap_get_sta(hapd, addr);
128 if (sta == NULL)
129 return;
130 switch (var) {
131 case WPA_EAPOL_portEnabled:
132 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
133 break;
134 case WPA_EAPOL_portValid:
135 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
136 break;
137 case WPA_EAPOL_authorized:
138 ieee802_1x_set_sta_authorized(hapd, sta, value);
139 break;
140 case WPA_EAPOL_portControl_Auto:
141 if (sta->eapol_sm)
142 sta->eapol_sm->portControl = Auto;
143 break;
144 case WPA_EAPOL_keyRun:
145 if (sta->eapol_sm)
146 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
147 break;
148 case WPA_EAPOL_keyAvailable:
149 if (sta->eapol_sm)
150 sta->eapol_sm->eap_if->eapKeyAvailable =
151 value ? TRUE : FALSE;
152 break;
153 case WPA_EAPOL_keyDone:
154 if (sta->eapol_sm)
155 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
156 break;
157 case WPA_EAPOL_inc_EapolFramesTx:
158 if (sta->eapol_sm)
159 sta->eapol_sm->dot1xAuthEapolFramesTx++;
160 break;
161 }
162 }
163
164
165 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
166 wpa_eapol_variable var)
167 {
168 struct hostapd_data *hapd = ctx;
169 struct sta_info *sta = ap_get_sta(hapd, addr);
170 if (sta == NULL || sta->eapol_sm == NULL)
171 return -1;
172 switch (var) {
173 case WPA_EAPOL_keyRun:
174 return sta->eapol_sm->keyRun;
175 case WPA_EAPOL_keyAvailable:
176 return sta->eapol_sm->eap_if->eapKeyAvailable;
177 default:
178 return -1;
179 }
180 }
181
182
183 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
184 const u8 *prev_psk)
185 {
186 struct hostapd_data *hapd = ctx;
187 struct sta_info *sta = ap_get_sta(hapd, addr);
188 const u8 *psk;
189
190 #ifdef CONFIG_SAE
191 if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
192 if (!sta->sae || prev_psk)
193 return NULL;
194 return sta->sae->pmk;
195 }
196 #endif /* CONFIG_SAE */
197
198 psk = hostapd_get_psk(hapd->conf, addr, prev_psk);
199 /*
200 * This is about to iterate over all psks, prev_psk gives the last
201 * returned psk which should not be returned again.
202 * logic list (all hostapd_get_psk; all sta->psk)
203 */
204 if (sta && sta->psk && !psk) {
205 struct hostapd_sta_wpa_psk_short *pos;
206 psk = sta->psk->psk;
207 for (pos = sta->psk; pos; pos = pos->next) {
208 if (pos->psk == prev_psk) {
209 psk = pos->next ? pos->next->psk : NULL;
210 break;
211 }
212 }
213 }
214 return psk;
215 }
216
217
218 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
219 size_t *len)
220 {
221 struct hostapd_data *hapd = ctx;
222 const u8 *key;
223 size_t keylen;
224 struct sta_info *sta;
225
226 sta = ap_get_sta(hapd, addr);
227 if (sta == NULL)
228 return -1;
229
230 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
231 if (key == NULL)
232 return -1;
233
234 if (keylen > *len)
235 keylen = *len;
236 os_memcpy(msk, key, keylen);
237 *len = keylen;
238
239 return 0;
240 }
241
242
243 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
244 const u8 *addr, int idx, u8 *key,
245 size_t key_len)
246 {
247 struct hostapd_data *hapd = ctx;
248 const char *ifname = hapd->conf->iface;
249
250 if (vlan_id > 0) {
251 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
252 if (ifname == NULL)
253 return -1;
254 }
255
256 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
257 key, key_len);
258 }
259
260
261 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
262 u8 *seq)
263 {
264 struct hostapd_data *hapd = ctx;
265 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
266 }
267
268
269 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
270 const u8 *data, size_t data_len,
271 int encrypt)
272 {
273 struct hostapd_data *hapd = ctx;
274 struct sta_info *sta;
275 u32 flags = 0;
276
277 sta = ap_get_sta(hapd, addr);
278 if (sta)
279 flags = hostapd_sta_flags_to_drv(sta->flags);
280
281 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
282 encrypt, flags);
283 }
284
285
286 static int hostapd_wpa_auth_for_each_sta(
287 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
288 void *cb_ctx)
289 {
290 struct hostapd_data *hapd = ctx;
291 struct sta_info *sta;
292
293 for (sta = hapd->sta_list; sta; sta = sta->next) {
294 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
295 return 1;
296 }
297 return 0;
298 }
299
300
301 struct wpa_auth_iface_iter_data {
302 int (*cb)(struct wpa_authenticator *sm, void *ctx);
303 void *cb_ctx;
304 };
305
306 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
307 {
308 struct wpa_auth_iface_iter_data *data = ctx;
309 size_t i;
310 for (i = 0; i < iface->num_bss; i++) {
311 if (iface->bss[i]->wpa_auth &&
312 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
313 return 1;
314 }
315 return 0;
316 }
317
318
319 static int hostapd_wpa_auth_for_each_auth(
320 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
321 void *cb_ctx)
322 {
323 struct hostapd_data *hapd = ctx;
324 struct wpa_auth_iface_iter_data data;
325 if (hapd->iface->interfaces == NULL ||
326 hapd->iface->interfaces->for_each_interface == NULL)
327 return -1;
328 data.cb = cb;
329 data.cb_ctx = cb_ctx;
330 return hapd->iface->interfaces->for_each_interface(
331 hapd->iface->interfaces, wpa_auth_iface_iter, &data);
332 }
333
334
335 #ifdef CONFIG_IEEE80211R
336
337 struct wpa_auth_ft_iface_iter_data {
338 struct hostapd_data *src_hapd;
339 const u8 *dst;
340 const u8 *data;
341 size_t data_len;
342 };
343
344
345 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
346 {
347 struct wpa_auth_ft_iface_iter_data *idata = ctx;
348 struct hostapd_data *hapd;
349 size_t j;
350
351 for (j = 0; j < iface->num_bss; j++) {
352 hapd = iface->bss[j];
353 if (hapd == idata->src_hapd)
354 continue;
355 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
356 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
357 "locally managed BSS " MACSTR "@%s -> "
358 MACSTR "@%s",
359 MAC2STR(idata->src_hapd->own_addr),
360 idata->src_hapd->conf->iface,
361 MAC2STR(hapd->own_addr), hapd->conf->iface);
362 wpa_ft_rrb_rx(hapd->wpa_auth,
363 idata->src_hapd->own_addr,
364 idata->data, idata->data_len);
365 return 1;
366 }
367 }
368
369 return 0;
370 }
371
372 #endif /* CONFIG_IEEE80211R */
373
374
375 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
376 const u8 *data, size_t data_len)
377 {
378 struct hostapd_data *hapd = ctx;
379 struct l2_ethhdr *buf;
380 int ret;
381
382 #ifdef CONFIG_IEEE80211R
383 if (proto == ETH_P_RRB && hapd->iface->interfaces &&
384 hapd->iface->interfaces->for_each_interface) {
385 int res;
386 struct wpa_auth_ft_iface_iter_data idata;
387 idata.src_hapd = hapd;
388 idata.dst = dst;
389 idata.data = data;
390 idata.data_len = data_len;
391 res = hapd->iface->interfaces->for_each_interface(
392 hapd->iface->interfaces, hostapd_wpa_auth_ft_iter,
393 &idata);
394 if (res == 1)
395 return data_len;
396 }
397 #endif /* CONFIG_IEEE80211R */
398
399 if (hapd->driver && hapd->driver->send_ether)
400 return hapd->driver->send_ether(hapd->drv_priv, dst,
401 hapd->own_addr, proto,
402 data, data_len);
403 if (hapd->l2 == NULL)
404 return -1;
405
406 buf = os_malloc(sizeof(*buf) + data_len);
407 if (buf == NULL)
408 return -1;
409 os_memcpy(buf->h_dest, dst, ETH_ALEN);
410 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
411 buf->h_proto = host_to_be16(proto);
412 os_memcpy(buf + 1, data, data_len);
413 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
414 sizeof(*buf) + data_len);
415 os_free(buf);
416 return ret;
417 }
418
419
420 #ifdef CONFIG_IEEE80211R
421
422 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
423 const u8 *data, size_t data_len)
424 {
425 struct hostapd_data *hapd = ctx;
426 int res;
427 struct ieee80211_mgmt *m;
428 size_t mlen;
429 struct sta_info *sta;
430
431 sta = ap_get_sta(hapd, dst);
432 if (sta == NULL || sta->wpa_sm == NULL)
433 return -1;
434
435 m = os_zalloc(sizeof(*m) + data_len);
436 if (m == NULL)
437 return -1;
438 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
439 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
440 WLAN_FC_STYPE_ACTION);
441 os_memcpy(m->da, dst, ETH_ALEN);
442 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
443 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
444 os_memcpy(&m->u, data, data_len);
445
446 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
447 os_free(m);
448 return res;
449 }
450
451
452 static struct wpa_state_machine *
453 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
454 {
455 struct hostapd_data *hapd = ctx;
456 struct sta_info *sta;
457
458 if (hostapd_add_sta_node(hapd, sta_addr, WLAN_AUTH_FT) < 0)
459 return NULL;
460
461 sta = ap_sta_add(hapd, sta_addr);
462 if (sta == NULL)
463 return NULL;
464 if (sta->wpa_sm) {
465 sta->auth_alg = WLAN_AUTH_FT;
466 return sta->wpa_sm;
467 }
468
469 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
470 if (sta->wpa_sm == NULL) {
471 ap_free_sta(hapd, sta);
472 return NULL;
473 }
474 sta->auth_alg = WLAN_AUTH_FT;
475
476 return sta->wpa_sm;
477 }
478
479
480 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
481 size_t len)
482 {
483 struct hostapd_data *hapd = ctx;
484 struct l2_ethhdr *ethhdr;
485 if (len < sizeof(*ethhdr))
486 return;
487 ethhdr = (struct l2_ethhdr *) buf;
488 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
489 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
490 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
491 len - sizeof(*ethhdr));
492 }
493
494
495 static int hostapd_wpa_auth_add_tspec(void *ctx, const u8 *sta_addr,
496 u8 *tspec_ie, size_t tspec_ielen)
497 {
498 struct hostapd_data *hapd = ctx;
499 return hostapd_add_tspec(hapd, sta_addr, tspec_ie, tspec_ielen);
500 }
501
502 #endif /* CONFIG_IEEE80211R */
503
504
505 int hostapd_setup_wpa(struct hostapd_data *hapd)
506 {
507 struct wpa_auth_config _conf;
508 struct wpa_auth_callbacks cb;
509 const u8 *wpa_ie;
510 size_t wpa_ie_len;
511
512 hostapd_wpa_auth_conf(hapd->conf, &_conf);
513 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
514 _conf.tx_status = 1;
515 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_AP_MLME)
516 _conf.ap_mlme = 1;
517 os_memset(&cb, 0, sizeof(cb));
518 cb.ctx = hapd;
519 cb.logger = hostapd_wpa_auth_logger;
520 cb.disconnect = hostapd_wpa_auth_disconnect;
521 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
522 cb.set_eapol = hostapd_wpa_auth_set_eapol;
523 cb.get_eapol = hostapd_wpa_auth_get_eapol;
524 cb.get_psk = hostapd_wpa_auth_get_psk;
525 cb.get_msk = hostapd_wpa_auth_get_msk;
526 cb.set_key = hostapd_wpa_auth_set_key;
527 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
528 cb.send_eapol = hostapd_wpa_auth_send_eapol;
529 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
530 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
531 cb.send_ether = hostapd_wpa_auth_send_ether;
532 #ifdef CONFIG_IEEE80211R
533 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
534 cb.add_sta = hostapd_wpa_auth_add_sta;
535 cb.add_tspec = hostapd_wpa_auth_add_tspec;
536 #endif /* CONFIG_IEEE80211R */
537 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
538 if (hapd->wpa_auth == NULL) {
539 wpa_printf(MSG_ERROR, "WPA initialization failed.");
540 return -1;
541 }
542
543 if (hostapd_set_privacy(hapd, 1)) {
544 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
545 "for interface %s", hapd->conf->iface);
546 return -1;
547 }
548
549 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
550 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
551 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
552 "the kernel driver.");
553 return -1;
554 }
555
556 if (rsn_preauth_iface_init(hapd)) {
557 wpa_printf(MSG_ERROR, "Initialization of RSN "
558 "pre-authentication failed.");
559 return -1;
560 }
561
562 #ifdef CONFIG_IEEE80211R
563 if (!hostapd_drv_none(hapd)) {
564 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
565 hapd->conf->bridge :
566 hapd->conf->iface, NULL, ETH_P_RRB,
567 hostapd_rrb_receive, hapd, 1);
568 if (hapd->l2 == NULL &&
569 (hapd->driver == NULL ||
570 hapd->driver->send_ether == NULL)) {
571 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
572 "interface");
573 return -1;
574 }
575 }
576 #endif /* CONFIG_IEEE80211R */
577
578 return 0;
579
580 }
581
582
583 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
584 {
585 struct wpa_auth_config wpa_auth_conf;
586 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
587 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
588 }
589
590
591 void hostapd_deinit_wpa(struct hostapd_data *hapd)
592 {
593 ieee80211_tkip_countermeasures_deinit(hapd);
594 rsn_preauth_iface_deinit(hapd);
595 if (hapd->wpa_auth) {
596 wpa_deinit(hapd->wpa_auth);
597 hapd->wpa_auth = NULL;
598
599 if (hostapd_set_privacy(hapd, 0)) {
600 wpa_printf(MSG_DEBUG, "Could not disable "
601 "PrivacyInvoked for interface %s",
602 hapd->conf->iface);
603 }
604
605 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
606 wpa_printf(MSG_DEBUG, "Could not remove generic "
607 "information element from interface %s",
608 hapd->conf->iface);
609 }
610 }
611 ieee802_1x_deinit(hapd);
612
613 #ifdef CONFIG_IEEE80211R
614 l2_packet_deinit(hapd->l2);
615 #endif /* CONFIG_IEEE80211R */
616 }