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