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