blob: b35b7ba5eef6ab332e203b037cc8cef086af0a43 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
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
34static 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
79static 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
104static 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
115static 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
122static 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
164static 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
182static 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
190static 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
215static 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
233static 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
241static 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 struct sta_info *sta;
247 u32 flags = 0;
248
249 sta = ap_get_sta(hapd, addr);
250 if (sta)
251 flags = hostapd_sta_flags_to_drv(sta->flags);
252
253 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
254 encrypt, flags);
255}
256
257
258static int hostapd_wpa_auth_for_each_sta(
259 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
260 void *cb_ctx)
261{
262 struct hostapd_data *hapd = ctx;
263 struct sta_info *sta;
264
265 for (sta = hapd->sta_list; sta; sta = sta->next) {
266 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
267 return 1;
268 }
269 return 0;
270}
271
272
273struct wpa_auth_iface_iter_data {
274 int (*cb)(struct wpa_authenticator *sm, void *ctx);
275 void *cb_ctx;
276};
277
278static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
279{
280 struct wpa_auth_iface_iter_data *data = ctx;
281 size_t i;
282 for (i = 0; i < iface->num_bss; i++) {
283 if (iface->bss[i]->wpa_auth &&
284 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
285 return 1;
286 }
287 return 0;
288}
289
290
291static int hostapd_wpa_auth_for_each_auth(
292 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
293 void *cb_ctx)
294{
295 struct hostapd_data *hapd = ctx;
296 struct wpa_auth_iface_iter_data data;
297 if (hapd->iface->for_each_interface == NULL)
298 return -1;
299 data.cb = cb;
300 data.cb_ctx = cb_ctx;
301 return hapd->iface->for_each_interface(hapd->iface->interfaces,
302 wpa_auth_iface_iter, &data);
303}
304
305
306#ifdef CONFIG_IEEE80211R
307
308struct wpa_auth_ft_iface_iter_data {
309 struct hostapd_data *src_hapd;
310 const u8 *dst;
311 const u8 *data;
312 size_t data_len;
313};
314
315
316static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
317{
318 struct wpa_auth_ft_iface_iter_data *idata = ctx;
319 struct hostapd_data *hapd;
320 size_t j;
321
322 for (j = 0; j < iface->num_bss; j++) {
323 hapd = iface->bss[j];
324 if (hapd == idata->src_hapd)
325 continue;
326 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
327 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
328 "locally managed BSS " MACSTR "@%s -> "
329 MACSTR "@%s",
330 MAC2STR(idata->src_hapd->own_addr),
331 idata->src_hapd->conf->iface,
332 MAC2STR(hapd->own_addr), hapd->conf->iface);
333 wpa_ft_rrb_rx(hapd->wpa_auth,
334 idata->src_hapd->own_addr,
335 idata->data, idata->data_len);
336 return 1;
337 }
338 }
339
340 return 0;
341}
342
343#endif /* CONFIG_IEEE80211R */
344
345
346static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
347 const u8 *data, size_t data_len)
348{
349 struct hostapd_data *hapd = ctx;
350 struct l2_ethhdr *buf;
351 int ret;
352
353#ifdef CONFIG_IEEE80211R
354 if (proto == ETH_P_RRB && hapd->iface->for_each_interface) {
355 int res;
356 struct wpa_auth_ft_iface_iter_data idata;
357 idata.src_hapd = hapd;
358 idata.dst = dst;
359 idata.data = data;
360 idata.data_len = data_len;
361 res = hapd->iface->for_each_interface(hapd->iface->interfaces,
362 hostapd_wpa_auth_ft_iter,
363 &idata);
364 if (res == 1)
365 return data_len;
366 }
367#endif /* CONFIG_IEEE80211R */
368
369 if (hapd->driver && hapd->driver->send_ether)
370 return hapd->driver->send_ether(hapd->drv_priv, dst,
371 hapd->own_addr, proto,
372 data, data_len);
373 if (hapd->l2 == NULL)
374 return -1;
375
376 buf = os_malloc(sizeof(*buf) + data_len);
377 if (buf == NULL)
378 return -1;
379 os_memcpy(buf->h_dest, dst, ETH_ALEN);
380 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
381 buf->h_proto = host_to_be16(proto);
382 os_memcpy(buf + 1, data, data_len);
383 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
384 sizeof(*buf) + data_len);
385 os_free(buf);
386 return -1;
387}
388
389
390#ifdef CONFIG_IEEE80211R
391
392static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
393 const u8 *data, size_t data_len)
394{
395 struct hostapd_data *hapd = ctx;
396 int res;
397 struct ieee80211_mgmt *m;
398 size_t mlen;
399 struct sta_info *sta;
400
401 sta = ap_get_sta(hapd, dst);
402 if (sta == NULL || sta->wpa_sm == NULL)
403 return -1;
404
405 m = os_zalloc(sizeof(*m) + data_len);
406 if (m == NULL)
407 return -1;
408 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
409 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
410 WLAN_FC_STYPE_ACTION);
411 os_memcpy(m->da, dst, ETH_ALEN);
412 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
413 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
414 os_memcpy(&m->u, data, data_len);
415
416 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen);
417 os_free(m);
418 return res;
419}
420
421
422static struct wpa_state_machine *
423hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
424{
425 struct hostapd_data *hapd = ctx;
426 struct sta_info *sta;
427
428 sta = ap_sta_add(hapd, sta_addr);
429 if (sta == NULL)
430 return NULL;
431 if (sta->wpa_sm) {
432 sta->auth_alg = WLAN_AUTH_FT;
433 return sta->wpa_sm;
434 }
435
436 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
437 if (sta->wpa_sm == NULL) {
438 ap_free_sta(hapd, sta);
439 return NULL;
440 }
441 sta->auth_alg = WLAN_AUTH_FT;
442
443 return sta->wpa_sm;
444}
445
446
447static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
448 size_t len)
449{
450 struct hostapd_data *hapd = ctx;
451 struct l2_ethhdr *ethhdr;
452 if (len < sizeof(*ethhdr))
453 return;
454 ethhdr = (struct l2_ethhdr *) buf;
455 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
456 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
457 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
458 len - sizeof(*ethhdr));
459}
460
461#endif /* CONFIG_IEEE80211R */
462
463
464int hostapd_setup_wpa(struct hostapd_data *hapd)
465{
466 struct wpa_auth_config _conf;
467 struct wpa_auth_callbacks cb;
468 const u8 *wpa_ie;
469 size_t wpa_ie_len;
470
471 hostapd_wpa_auth_conf(hapd->conf, &_conf);
472 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
473 _conf.tx_status = 1;
474 os_memset(&cb, 0, sizeof(cb));
475 cb.ctx = hapd;
476 cb.logger = hostapd_wpa_auth_logger;
477 cb.disconnect = hostapd_wpa_auth_disconnect;
478 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
479 cb.set_eapol = hostapd_wpa_auth_set_eapol;
480 cb.get_eapol = hostapd_wpa_auth_get_eapol;
481 cb.get_psk = hostapd_wpa_auth_get_psk;
482 cb.get_msk = hostapd_wpa_auth_get_msk;
483 cb.set_key = hostapd_wpa_auth_set_key;
484 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
485 cb.send_eapol = hostapd_wpa_auth_send_eapol;
486 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
487 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
488 cb.send_ether = hostapd_wpa_auth_send_ether;
489#ifdef CONFIG_IEEE80211R
490 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
491 cb.add_sta = hostapd_wpa_auth_add_sta;
492#endif /* CONFIG_IEEE80211R */
493 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
494 if (hapd->wpa_auth == NULL) {
495 wpa_printf(MSG_ERROR, "WPA initialization failed.");
496 return -1;
497 }
498
499 if (hostapd_set_privacy(hapd, 1)) {
500 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
501 "for interface %s", hapd->conf->iface);
502 return -1;
503 }
504
505 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
506 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
507 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
508 "the kernel driver.");
509 return -1;
510 }
511
512 if (rsn_preauth_iface_init(hapd)) {
513 wpa_printf(MSG_ERROR, "Initialization of RSN "
514 "pre-authentication failed.");
515 return -1;
516 }
517
518#ifdef CONFIG_IEEE80211R
519 if (!hostapd_drv_none(hapd)) {
520 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
521 hapd->conf->bridge :
522 hapd->conf->iface, NULL, ETH_P_RRB,
523 hostapd_rrb_receive, hapd, 1);
524 if (hapd->l2 == NULL &&
525 (hapd->driver == NULL ||
526 hapd->driver->send_ether == NULL)) {
527 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
528 "interface");
529 return -1;
530 }
531 }
532#endif /* CONFIG_IEEE80211R */
533
534 return 0;
535
536}
537
538
539void hostapd_reconfig_wpa(struct hostapd_data *hapd)
540{
541 struct wpa_auth_config wpa_auth_conf;
542 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
543 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
544}
545
546
547void hostapd_deinit_wpa(struct hostapd_data *hapd)
548{
549 rsn_preauth_iface_deinit(hapd);
550 if (hapd->wpa_auth) {
551 wpa_deinit(hapd->wpa_auth);
552 hapd->wpa_auth = NULL;
553
554 if (hostapd_set_privacy(hapd, 0)) {
555 wpa_printf(MSG_DEBUG, "Could not disable "
556 "PrivacyInvoked for interface %s",
557 hapd->conf->iface);
558 }
559
560 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
561 wpa_printf(MSG_DEBUG, "Could not remove generic "
562 "information element from interface %s",
563 hapd->conf->iface);
564 }
565 }
566 ieee802_1x_deinit(hapd);
567
568#ifdef CONFIG_IEEE80211R
569 l2_packet_deinit(hapd->l2);
570#endif /* CONFIG_IEEE80211R */
571}