]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth_glue.c
Get rid of unnecessary typedefs for enums.
[thirdparty/hostap.git] / src / ap / wpa_auth_glue.c
1 /*
2 * hostapd / WPA authenticator glue code
3 * Copyright (c) 2002-2009, 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->okc = conf->okc;
51 #ifdef CONFIG_IEEE80211W
52 wconf->ieee80211w = conf->ieee80211w;
53 #endif /* CONFIG_IEEE80211W */
54 #ifdef CONFIG_IEEE80211R
55 wconf->ssid_len = conf->ssid.ssid_len;
56 if (wconf->ssid_len > SSID_LEN)
57 wconf->ssid_len = SSID_LEN;
58 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
59 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
60 MOBILITY_DOMAIN_ID_LEN);
61 if (conf->nas_identifier &&
62 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
63 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
64 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
65 wconf->r0_key_holder_len);
66 }
67 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
68 wconf->r0_key_lifetime = conf->r0_key_lifetime;
69 wconf->reassociation_deadline = conf->reassociation_deadline;
70 wconf->r0kh_list = conf->r0kh_list;
71 wconf->r1kh_list = conf->r1kh_list;
72 wconf->pmk_r1_push = conf->pmk_r1_push;
73 #endif /* CONFIG_IEEE80211R */
74 }
75
76
77 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
78 logger_level level, const char *txt)
79 {
80 #ifndef CONFIG_NO_HOSTAPD_LOGGER
81 struct hostapd_data *hapd = ctx;
82 int hlevel;
83
84 switch (level) {
85 case LOGGER_WARNING:
86 hlevel = HOSTAPD_LEVEL_WARNING;
87 break;
88 case LOGGER_INFO:
89 hlevel = HOSTAPD_LEVEL_INFO;
90 break;
91 case LOGGER_DEBUG:
92 default:
93 hlevel = HOSTAPD_LEVEL_DEBUG;
94 break;
95 }
96
97 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
98 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
99 }
100
101
102 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
103 u16 reason)
104 {
105 struct hostapd_data *hapd = ctx;
106 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
107 "STA " MACSTR " reason %d",
108 __func__, MAC2STR(addr), reason);
109 ap_sta_disconnect(hapd, NULL, addr, reason);
110 }
111
112
113 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
114 {
115 struct hostapd_data *hapd = ctx;
116 michael_mic_failure(hapd, addr, 0);
117 }
118
119
120 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
121 wpa_eapol_variable var, int value)
122 {
123 struct hostapd_data *hapd = ctx;
124 struct sta_info *sta = ap_get_sta(hapd, addr);
125 if (sta == NULL)
126 return;
127 switch (var) {
128 case WPA_EAPOL_portEnabled:
129 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
130 break;
131 case WPA_EAPOL_portValid:
132 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
133 break;
134 case WPA_EAPOL_authorized:
135 ieee802_1x_set_sta_authorized(hapd, sta, value);
136 break;
137 case WPA_EAPOL_portControl_Auto:
138 if (sta->eapol_sm)
139 sta->eapol_sm->portControl = Auto;
140 break;
141 case WPA_EAPOL_keyRun:
142 if (sta->eapol_sm)
143 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
144 break;
145 case WPA_EAPOL_keyAvailable:
146 if (sta->eapol_sm)
147 sta->eapol_sm->eap_if->eapKeyAvailable =
148 value ? TRUE : FALSE;
149 break;
150 case WPA_EAPOL_keyDone:
151 if (sta->eapol_sm)
152 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
153 break;
154 case WPA_EAPOL_inc_EapolFramesTx:
155 if (sta->eapol_sm)
156 sta->eapol_sm->dot1xAuthEapolFramesTx++;
157 break;
158 }
159 }
160
161
162 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
163 wpa_eapol_variable var)
164 {
165 struct hostapd_data *hapd = ctx;
166 struct sta_info *sta = ap_get_sta(hapd, addr);
167 if (sta == NULL || sta->eapol_sm == NULL)
168 return -1;
169 switch (var) {
170 case WPA_EAPOL_keyRun:
171 return sta->eapol_sm->keyRun;
172 case WPA_EAPOL_keyAvailable:
173 return sta->eapol_sm->eap_if->eapKeyAvailable;
174 default:
175 return -1;
176 }
177 }
178
179
180 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
181 const u8 *prev_psk)
182 {
183 struct hostapd_data *hapd = ctx;
184 return hostapd_get_psk(hapd->conf, addr, prev_psk);
185 }
186
187
188 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
189 size_t *len)
190 {
191 struct hostapd_data *hapd = ctx;
192 const u8 *key;
193 size_t keylen;
194 struct sta_info *sta;
195
196 sta = ap_get_sta(hapd, addr);
197 if (sta == NULL)
198 return -1;
199
200 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
201 if (key == NULL)
202 return -1;
203
204 if (keylen > *len)
205 keylen = *len;
206 os_memcpy(msk, key, keylen);
207 *len = keylen;
208
209 return 0;
210 }
211
212
213 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
214 const u8 *addr, int idx, u8 *key,
215 size_t key_len)
216 {
217 struct hostapd_data *hapd = ctx;
218 const char *ifname = hapd->conf->iface;
219
220 if (vlan_id > 0) {
221 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
222 if (ifname == NULL)
223 return -1;
224 }
225
226 return hapd->drv.set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
227 key, key_len);
228 }
229
230
231 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
232 u8 *seq)
233 {
234 struct hostapd_data *hapd = ctx;
235 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
236 }
237
238
239 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
240 const u8 *data, size_t data_len,
241 int encrypt)
242 {
243 struct hostapd_data *hapd = ctx;
244 return hapd->drv.send_eapol(hapd, addr, data, data_len, encrypt);
245 }
246
247
248 static int hostapd_wpa_auth_for_each_sta(
249 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
250 void *cb_ctx)
251 {
252 struct hostapd_data *hapd = ctx;
253 struct sta_info *sta;
254
255 for (sta = hapd->sta_list; sta; sta = sta->next) {
256 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
257 return 1;
258 }
259 return 0;
260 }
261
262
263 struct wpa_auth_iface_iter_data {
264 int (*cb)(struct wpa_authenticator *sm, void *ctx);
265 void *cb_ctx;
266 };
267
268 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
269 {
270 struct wpa_auth_iface_iter_data *data = ctx;
271 size_t i;
272 for (i = 0; i < iface->num_bss; i++) {
273 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
274 return 1;
275 }
276 return 0;
277 }
278
279
280 static int hostapd_wpa_auth_for_each_auth(
281 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
282 void *cb_ctx)
283 {
284 struct hostapd_data *hapd = ctx;
285 struct wpa_auth_iface_iter_data data;
286 if (hapd->iface->for_each_interface == NULL)
287 return -1;
288 data.cb = cb;
289 data.cb_ctx = cb_ctx;
290 return hapd->iface->for_each_interface(hapd->iface->interfaces,
291 wpa_auth_iface_iter, &data);
292 }
293
294
295 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
296 const u8 *data, size_t data_len)
297 {
298 struct hostapd_data *hapd = ctx;
299
300 if (hapd->driver && hapd->driver->send_ether)
301 return hapd->driver->send_ether(hapd->drv_priv, dst,
302 hapd->own_addr, proto,
303 data, data_len);
304 if (hapd->l2 == NULL)
305 return -1;
306 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
307 }
308
309
310 #ifdef CONFIG_IEEE80211R
311
312 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
313 const u8 *data, size_t data_len)
314 {
315 struct hostapd_data *hapd = ctx;
316 int res;
317 struct ieee80211_mgmt *m;
318 size_t mlen;
319 struct sta_info *sta;
320
321 sta = ap_get_sta(hapd, dst);
322 if (sta == NULL || sta->wpa_sm == NULL)
323 return -1;
324
325 m = os_zalloc(sizeof(*m) + data_len);
326 if (m == NULL)
327 return -1;
328 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
329 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
330 WLAN_FC_STYPE_ACTION);
331 os_memcpy(m->da, dst, ETH_ALEN);
332 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
333 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
334 os_memcpy(&m->u, data, data_len);
335
336 res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
337 os_free(m);
338 return res;
339 }
340
341
342 static struct wpa_state_machine *
343 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
344 {
345 struct hostapd_data *hapd = ctx;
346 struct sta_info *sta;
347
348 sta = ap_sta_add(hapd, sta_addr);
349 if (sta == NULL)
350 return NULL;
351 if (sta->wpa_sm)
352 return sta->wpa_sm;
353
354 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
355 if (sta->wpa_sm == NULL) {
356 ap_free_sta(hapd, sta);
357 return NULL;
358 }
359 sta->auth_alg = WLAN_AUTH_FT;
360
361 return sta->wpa_sm;
362 }
363
364
365 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
366 size_t len)
367 {
368 struct hostapd_data *hapd = ctx;
369 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
370 }
371
372 #endif /* CONFIG_IEEE80211R */
373
374
375 int hostapd_setup_wpa(struct hostapd_data *hapd)
376 {
377 struct wpa_auth_config _conf;
378 struct wpa_auth_callbacks cb;
379 const u8 *wpa_ie;
380 size_t wpa_ie_len;
381
382 hostapd_wpa_auth_conf(hapd->conf, &_conf);
383 os_memset(&cb, 0, sizeof(cb));
384 cb.ctx = hapd;
385 cb.logger = hostapd_wpa_auth_logger;
386 cb.disconnect = hostapd_wpa_auth_disconnect;
387 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
388 cb.set_eapol = hostapd_wpa_auth_set_eapol;
389 cb.get_eapol = hostapd_wpa_auth_get_eapol;
390 cb.get_psk = hostapd_wpa_auth_get_psk;
391 cb.get_msk = hostapd_wpa_auth_get_msk;
392 cb.set_key = hostapd_wpa_auth_set_key;
393 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
394 cb.send_eapol = hostapd_wpa_auth_send_eapol;
395 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
396 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
397 cb.send_ether = hostapd_wpa_auth_send_ether;
398 #ifdef CONFIG_IEEE80211R
399 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
400 cb.add_sta = hostapd_wpa_auth_add_sta;
401 #endif /* CONFIG_IEEE80211R */
402 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
403 if (hapd->wpa_auth == NULL) {
404 wpa_printf(MSG_ERROR, "WPA initialization failed.");
405 return -1;
406 }
407
408 if (hostapd_set_privacy(hapd, 1)) {
409 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
410 "for interface %s", hapd->conf->iface);
411 return -1;
412 }
413
414 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
415 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
416 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
417 "the kernel driver.");
418 return -1;
419 }
420
421 if (rsn_preauth_iface_init(hapd)) {
422 wpa_printf(MSG_ERROR, "Initialization of RSN "
423 "pre-authentication failed.");
424 return -1;
425 }
426
427 #ifdef CONFIG_IEEE80211R
428 if (!hostapd_drv_none(hapd)) {
429 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
430 hostapd_rrb_receive, hapd, 0);
431 if (hapd->l2 == NULL &&
432 (hapd->driver == NULL ||
433 hapd->driver->send_ether == NULL)) {
434 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
435 "interface");
436 return -1;
437 }
438 }
439 #endif /* CONFIG_IEEE80211R */
440
441 return 0;
442
443 }
444
445
446 void hostapd_reconfig_wpa(struct hostapd_data *hapd)
447 {
448 struct wpa_auth_config wpa_auth_conf;
449 hostapd_wpa_auth_conf(hapd->conf, &wpa_auth_conf);
450 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
451 }
452
453
454 void hostapd_deinit_wpa(struct hostapd_data *hapd)
455 {
456 rsn_preauth_iface_deinit(hapd);
457 if (hapd->wpa_auth) {
458 wpa_deinit(hapd->wpa_auth);
459 hapd->wpa_auth = NULL;
460
461 if (hostapd_set_privacy(hapd, 0)) {
462 wpa_printf(MSG_DEBUG, "Could not disable "
463 "PrivacyInvoked for interface %s",
464 hapd->conf->iface);
465 }
466
467 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
468 wpa_printf(MSG_DEBUG, "Could not remove generic "
469 "information element from interface %s",
470 hapd->conf->iface);
471 }
472 }
473 ieee802_1x_deinit(hapd);
474
475 #ifdef CONFIG_IEEE80211R
476 l2_packet_deinit(hapd->l2);
477 #endif /* CONFIG_IEEE80211R */
478 }