]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth_glue.c
HS 2.0: Add mechanism for disabling DGAF
[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 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 "eapol_auth/eapol_auth_sm.h"
14 #include "eapol_auth/eapol_auth_sm_i.h"
15 #include "eap_server/eap.h"
16 #include "l2_packet/l2_packet.h"
17 #include "drivers/driver.h"
18 #include "hostapd.h"
19 #include "ieee802_1x.h"
20 #include "preauth_auth.h"
21 #include "sta_info.h"
22 #include "tkip_countermeasures.h"
23 #include "ap_drv_ops.h"
24 #include "ap_config.h"
25 #include "wpa_auth.h"
26 #include "wpa_auth_glue.h"
27
28
29 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
30 struct wpa_auth_config *wconf)
31 {
32 os_memset(wconf, 0, sizeof(*wconf));
33 wconf->wpa = conf->wpa;
34 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
35 wconf->wpa_pairwise = conf->wpa_pairwise;
36 wconf->wpa_group = conf->wpa_group;
37 wconf->wpa_group_rekey = conf->wpa_group_rekey;
38 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
39 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
40 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
41 wconf->rsn_pairwise = conf->rsn_pairwise;
42 wconf->rsn_preauth = conf->rsn_preauth;
43 wconf->eapol_version = conf->eapol_version;
44 wconf->peerkey = conf->peerkey;
45 wconf->wmm_enabled = conf->wmm_enabled;
46 wconf->wmm_uapsd = conf->wmm_uapsd;
47 wconf->disable_pmksa_caching = conf->disable_pmksa_caching;
48 wconf->okc = conf->okc;
49 #ifdef CONFIG_IEEE80211W
50 wconf->ieee80211w = conf->ieee80211w;
51 #endif /* CONFIG_IEEE80211W */
52 #ifdef CONFIG_IEEE80211R
53 wconf->ssid_len = conf->ssid.ssid_len;
54 if (wconf->ssid_len > SSID_LEN)
55 wconf->ssid_len = SSID_LEN;
56 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
57 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
58 MOBILITY_DOMAIN_ID_LEN);
59 if (conf->nas_identifier &&
60 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
61 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
62 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
63 wconf->r0_key_holder_len);
64 }
65 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
66 wconf->r0_key_lifetime = conf->r0_key_lifetime;
67 wconf->reassociation_deadline = conf->reassociation_deadline;
68 wconf->r0kh_list = conf->r0kh_list;
69 wconf->r1kh_list = conf->r1kh_list;
70 wconf->pmk_r1_push = conf->pmk_r1_push;
71 wconf->ft_over_ds = conf->ft_over_ds;
72 #endif /* CONFIG_IEEE80211R */
73 #ifdef CONFIG_HS20
74 wconf->disable_gtk = conf->disable_dgaf;
75 #endif /* CONFIG_HS20 */
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 struct sta_info *sta = ap_get_sta(hapd, addr);
187 if (sta && sta->psk)
188 return sta->psk;
189 return hostapd_get_psk(hapd->conf, addr, prev_psk);
190 }
191
192
193 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
194 size_t *len)
195 {
196 struct hostapd_data *hapd = ctx;
197 const u8 *key;
198 size_t keylen;
199 struct sta_info *sta;
200
201 sta = ap_get_sta(hapd, addr);
202 if (sta == NULL)
203 return -1;
204
205 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
206 if (key == NULL)
207 return -1;
208
209 if (keylen > *len)
210 keylen = *len;
211 os_memcpy(msk, key, keylen);
212 *len = keylen;
213
214 return 0;
215 }
216
217
218 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
219 const u8 *addr, int idx, u8 *key,
220 size_t key_len)
221 {
222 struct hostapd_data *hapd = ctx;
223 const char *ifname = hapd->conf->iface;
224
225 if (vlan_id > 0) {
226 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
227 if (ifname == NULL)
228 return -1;
229 }
230
231 return hostapd_drv_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
232 key, key_len);
233 }
234
235
236 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
237 u8 *seq)
238 {
239 struct hostapd_data *hapd = ctx;
240 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
241 }
242
243
244 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
245 const u8 *data, size_t data_len,
246 int encrypt)
247 {
248 struct hostapd_data *hapd = ctx;
249 struct sta_info *sta;
250 u32 flags = 0;
251
252 sta = ap_get_sta(hapd, addr);
253 if (sta)
254 flags = hostapd_sta_flags_to_drv(sta->flags);
255
256 return hostapd_drv_hapd_send_eapol(hapd, addr, data, data_len,
257 encrypt, flags);
258 }
259
260
261 static int hostapd_wpa_auth_for_each_sta(
262 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
263 void *cb_ctx)
264 {
265 struct hostapd_data *hapd = ctx;
266 struct sta_info *sta;
267
268 for (sta = hapd->sta_list; sta; sta = sta->next) {
269 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
270 return 1;
271 }
272 return 0;
273 }
274
275
276 struct wpa_auth_iface_iter_data {
277 int (*cb)(struct wpa_authenticator *sm, void *ctx);
278 void *cb_ctx;
279 };
280
281 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
282 {
283 struct wpa_auth_iface_iter_data *data = ctx;
284 size_t i;
285 for (i = 0; i < iface->num_bss; i++) {
286 if (iface->bss[i]->wpa_auth &&
287 data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
288 return 1;
289 }
290 return 0;
291 }
292
293
294 static int hostapd_wpa_auth_for_each_auth(
295 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
296 void *cb_ctx)
297 {
298 struct hostapd_data *hapd = ctx;
299 struct wpa_auth_iface_iter_data data;
300 if (hapd->iface->for_each_interface == NULL)
301 return -1;
302 data.cb = cb;
303 data.cb_ctx = cb_ctx;
304 return hapd->iface->for_each_interface(hapd->iface->interfaces,
305 wpa_auth_iface_iter, &data);
306 }
307
308
309 #ifdef CONFIG_IEEE80211R
310
311 struct wpa_auth_ft_iface_iter_data {
312 struct hostapd_data *src_hapd;
313 const u8 *dst;
314 const u8 *data;
315 size_t data_len;
316 };
317
318
319 static int hostapd_wpa_auth_ft_iter(struct hostapd_iface *iface, void *ctx)
320 {
321 struct wpa_auth_ft_iface_iter_data *idata = ctx;
322 struct hostapd_data *hapd;
323 size_t j;
324
325 for (j = 0; j < iface->num_bss; j++) {
326 hapd = iface->bss[j];
327 if (hapd == idata->src_hapd)
328 continue;
329 if (os_memcmp(hapd->own_addr, idata->dst, ETH_ALEN) == 0) {
330 wpa_printf(MSG_DEBUG, "FT: Send RRB data directly to "
331 "locally managed BSS " MACSTR "@%s -> "
332 MACSTR "@%s",
333 MAC2STR(idata->src_hapd->own_addr),
334 idata->src_hapd->conf->iface,
335 MAC2STR(hapd->own_addr), hapd->conf->iface);
336 wpa_ft_rrb_rx(hapd->wpa_auth,
337 idata->src_hapd->own_addr,
338 idata->data, idata->data_len);
339 return 1;
340 }
341 }
342
343 return 0;
344 }
345
346 #endif /* CONFIG_IEEE80211R */
347
348
349 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
350 const u8 *data, size_t data_len)
351 {
352 struct hostapd_data *hapd = ctx;
353 struct l2_ethhdr *buf;
354 int ret;
355
356 #ifdef CONFIG_IEEE80211R
357 if (proto == ETH_P_RRB && hapd->iface->for_each_interface) {
358 int res;
359 struct wpa_auth_ft_iface_iter_data idata;
360 idata.src_hapd = hapd;
361 idata.dst = dst;
362 idata.data = data;
363 idata.data_len = data_len;
364 res = hapd->iface->for_each_interface(hapd->iface->interfaces,
365 hostapd_wpa_auth_ft_iter,
366 &idata);
367 if (res == 1)
368 return data_len;
369 }
370 #endif /* CONFIG_IEEE80211R */
371
372 if (hapd->driver && hapd->driver->send_ether)
373 return hapd->driver->send_ether(hapd->drv_priv, dst,
374 hapd->own_addr, proto,
375 data, data_len);
376 if (hapd->l2 == NULL)
377 return -1;
378
379 buf = os_malloc(sizeof(*buf) + data_len);
380 if (buf == NULL)
381 return -1;
382 os_memcpy(buf->h_dest, dst, ETH_ALEN);
383 os_memcpy(buf->h_source, hapd->own_addr, ETH_ALEN);
384 buf->h_proto = host_to_be16(proto);
385 os_memcpy(buf + 1, data, data_len);
386 ret = l2_packet_send(hapd->l2, dst, proto, (u8 *) buf,
387 sizeof(*buf) + data_len);
388 os_free(buf);
389 return ret;
390 }
391
392
393 #ifdef CONFIG_IEEE80211R
394
395 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
396 const u8 *data, size_t data_len)
397 {
398 struct hostapd_data *hapd = ctx;
399 int res;
400 struct ieee80211_mgmt *m;
401 size_t mlen;
402 struct sta_info *sta;
403
404 sta = ap_get_sta(hapd, dst);
405 if (sta == NULL || sta->wpa_sm == NULL)
406 return -1;
407
408 m = os_zalloc(sizeof(*m) + data_len);
409 if (m == NULL)
410 return -1;
411 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
412 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
413 WLAN_FC_STYPE_ACTION);
414 os_memcpy(m->da, dst, ETH_ALEN);
415 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
416 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
417 os_memcpy(&m->u, data, data_len);
418
419 res = hostapd_drv_send_mlme(hapd, (u8 *) m, mlen, 0);
420 os_free(m);
421 return res;
422 }
423
424
425 static struct wpa_state_machine *
426 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
427 {
428 struct hostapd_data *hapd = ctx;
429 struct sta_info *sta;
430
431 sta = ap_sta_add(hapd, sta_addr);
432 if (sta == NULL)
433 return NULL;
434 if (sta->wpa_sm) {
435 sta->auth_alg = WLAN_AUTH_FT;
436 return sta->wpa_sm;
437 }
438
439 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
440 if (sta->wpa_sm == NULL) {
441 ap_free_sta(hapd, sta);
442 return NULL;
443 }
444 sta->auth_alg = WLAN_AUTH_FT;
445
446 return sta->wpa_sm;
447 }
448
449
450 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
451 size_t len)
452 {
453 struct hostapd_data *hapd = ctx;
454 struct l2_ethhdr *ethhdr;
455 if (len < sizeof(*ethhdr))
456 return;
457 ethhdr = (struct l2_ethhdr *) buf;
458 wpa_printf(MSG_DEBUG, "FT: RRB received packet " MACSTR " -> "
459 MACSTR, MAC2STR(ethhdr->h_source), MAC2STR(ethhdr->h_dest));
460 wpa_ft_rrb_rx(hapd->wpa_auth, ethhdr->h_source, buf + sizeof(*ethhdr),
461 len - sizeof(*ethhdr));
462 }
463
464 #endif /* CONFIG_IEEE80211R */
465
466
467 int hostapd_setup_wpa(struct hostapd_data *hapd)
468 {
469 struct wpa_auth_config _conf;
470 struct wpa_auth_callbacks cb;
471 const u8 *wpa_ie;
472 size_t wpa_ie_len;
473
474 hostapd_wpa_auth_conf(hapd->conf, &_conf);
475 if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_EAPOL_TX_STATUS)
476 _conf.tx_status = 1;
477 os_memset(&cb, 0, sizeof(cb));
478 cb.ctx = hapd;
479 cb.logger = hostapd_wpa_auth_logger;
480 cb.disconnect = hostapd_wpa_auth_disconnect;
481 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
482 cb.set_eapol = hostapd_wpa_auth_set_eapol;
483 cb.get_eapol = hostapd_wpa_auth_get_eapol;
484 cb.get_psk = hostapd_wpa_auth_get_psk;
485 cb.get_msk = hostapd_wpa_auth_get_msk;
486 cb.set_key = hostapd_wpa_auth_set_key;
487 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
488 cb.send_eapol = hostapd_wpa_auth_send_eapol;
489 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
490 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
491 cb.send_ether = hostapd_wpa_auth_send_ether;
492 #ifdef CONFIG_IEEE80211R
493 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
494 cb.add_sta = hostapd_wpa_auth_add_sta;
495 #endif /* CONFIG_IEEE80211R */
496 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
497 if (hapd->wpa_auth == NULL) {
498 wpa_printf(MSG_ERROR, "WPA initialization failed.");
499 return -1;
500 }
501
502 if (hostapd_set_privacy(hapd, 1)) {
503 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
504 "for interface %s", hapd->conf->iface);
505 return -1;
506 }
507
508 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
509 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
510 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
511 "the kernel driver.");
512 return -1;
513 }
514
515 if (rsn_preauth_iface_init(hapd)) {
516 wpa_printf(MSG_ERROR, "Initialization of RSN "
517 "pre-authentication failed.");
518 return -1;
519 }
520
521 #ifdef CONFIG_IEEE80211R
522 if (!hostapd_drv_none(hapd)) {
523 hapd->l2 = l2_packet_init(hapd->conf->bridge[0] ?
524 hapd->conf->bridge :
525 hapd->conf->iface, NULL, ETH_P_RRB,
526 hostapd_rrb_receive, hapd, 1);
527 if (hapd->l2 == NULL &&
528 (hapd->driver == NULL ||
529 hapd->driver->send_ether == NULL)) {
530 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
531 "interface");
532 return -1;
533 }
534 }
535 #endif /* CONFIG_IEEE80211R */
536
537 return 0;
538
539 }
540
541
542 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
543 {
544 struct wpa_auth_config wpa_auth_conf;
545 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
546 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
547 }
548
549
550 void hostapd_deinit_wpa(struct hostapd_data *hapd)
551 {
552 ieee80211_tkip_countermeasures_deinit(hapd);
553 rsn_preauth_iface_deinit(hapd);
554 if (hapd->wpa_auth) {
555 wpa_deinit(hapd->wpa_auth);
556 hapd->wpa_auth = NULL;
557
558 if (hostapd_set_privacy(hapd, 0)) {
559 wpa_printf(MSG_DEBUG, "Could not disable "
560 "PrivacyInvoked for interface %s",
561 hapd->conf->iface);
562 }
563
564 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
565 wpa_printf(MSG_DEBUG, "Could not remove generic "
566 "information element from interface %s",
567 hapd->conf->iface);
568 }
569 }
570 ieee802_1x_deinit(hapd);
571
572 #ifdef CONFIG_IEEE80211R
573 l2_packet_deinit(hapd->l2);
574 #endif /* CONFIG_IEEE80211R */
575 }