]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wpas_glue.c
Interworking: Fetch only the needed ANQP information
[thirdparty/hostap.git] / wpa_supplicant / wpas_glue.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Glue code to setup EAPOL and RSN modules
e026159a 3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eapol_supp/eapol_supp_sm.h"
3acb5005 13#include "rsn_supp/wpa.h"
6fc6879b
JM
14#include "eloop.h"
15#include "config.h"
16#include "l2_packet/l2_packet.h"
90973fb2 17#include "common/wpa_common.h"
6fc6879b 18#include "wpa_supplicant_i.h"
2d5b792d 19#include "driver_i.h"
3acb5005 20#include "rsn_supp/pmksa_cache.h"
c2a04078 21#include "sme.h"
90973fb2
JM
22#include "common/ieee802_11_defs.h"
23#include "common/wpa_ctrl.h"
6fc6879b 24#include "wpas_glue.h"
fa201b69 25#include "wps_supplicant.h"
e48f0fb6 26#include "bss.h"
9ba9fa07 27#include "scan.h"
ade74830 28#include "notify.h"
6fc6879b
JM
29
30
31#ifndef CONFIG_NO_CONFIG_BLOBS
32#if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
33static void wpa_supplicant_set_config_blob(void *ctx,
34 struct wpa_config_blob *blob)
35{
36 struct wpa_supplicant *wpa_s = ctx;
37 wpa_config_set_blob(wpa_s->conf, blob);
c08b9180
JM
38 if (wpa_s->conf->update_config) {
39 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
40 if (ret) {
41 wpa_printf(MSG_DEBUG, "Failed to update config after "
42 "blob set");
43 }
44 }
6fc6879b
JM
45}
46
47
48static const struct wpa_config_blob *
49wpa_supplicant_get_config_blob(void *ctx, const char *name)
50{
51 struct wpa_supplicant *wpa_s = ctx;
52 return wpa_config_get_blob(wpa_s->conf, name);
53}
54#endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
55#endif /* CONFIG_NO_CONFIG_BLOBS */
56
57
58#if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
59static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
60 const void *data, u16 data_len,
61 size_t *msg_len, void **data_pos)
62{
63 struct ieee802_1x_hdr *hdr;
64
65 *msg_len = sizeof(*hdr) + data_len;
66 hdr = os_malloc(*msg_len);
67 if (hdr == NULL)
68 return NULL;
69
70 hdr->version = wpa_s->conf->eapol_version;
71 hdr->type = type;
72 hdr->length = host_to_be16(data_len);
73
74 if (data)
75 os_memcpy(hdr + 1, data, data_len);
76 else
77 os_memset(hdr + 1, 0, data_len);
78
79 if (data_pos)
80 *data_pos = hdr + 1;
81
82 return (u8 *) hdr;
83}
84
85
86/**
87 * wpa_ether_send - Send Ethernet frame
88 * @wpa_s: Pointer to wpa_supplicant data
89 * @dest: Destination MAC address
90 * @proto: Ethertype in host byte order
91 * @buf: Frame payload starting from IEEE 802.1X header
92 * @len: Frame payload length
93 * Returns: >=0 on success, <0 on failure
94 */
95static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
96 u16 proto, const u8 *buf, size_t len)
97{
98 if (wpa_s->l2) {
99 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
100 }
101
102 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
103}
104#endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
105
106
107#ifdef IEEE8021X_EAPOL
108
109/**
110 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
111 * @ctx: Pointer to wpa_supplicant data (wpa_s)
112 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
113 * @buf: EAPOL payload (after IEEE 802.1X header)
114 * @len: EAPOL payload length
115 * Returns: >=0 on success, <0 on failure
116 *
117 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
118 * to the current Authenticator.
119 */
120static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
121 size_t len)
122{
123 struct wpa_supplicant *wpa_s = ctx;
124 u8 *msg, *dst, bssid[ETH_ALEN];
125 size_t msglen;
126 int res;
127
128 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
129 * extra copy here */
130
56586197 131 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
6fc6879b
JM
132 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
133 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
134 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
135 * machines. */
136 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
137 "mode (type=%d len=%lu)", type,
138 (unsigned long) len);
139 return -1;
140 }
141
142 if (pmksa_cache_get_current(wpa_s->wpa) &&
143 type == IEEE802_1X_TYPE_EAPOL_START) {
144 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
145 * since they will trigger full EAPOL authentication. */
146 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
147 "EAPOL-Start");
148 return -1;
149 }
150
a8e16edc 151 if (is_zero_ether_addr(wpa_s->bssid)) {
6fc6879b
JM
152 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
153 "EAPOL frame");
154 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
a8e16edc 155 !is_zero_ether_addr(bssid)) {
6fc6879b
JM
156 dst = bssid;
157 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
158 " from the driver as the EAPOL destination",
159 MAC2STR(dst));
160 } else {
161 dst = wpa_s->last_eapol_src;
162 wpa_printf(MSG_DEBUG, "Using the source address of the"
163 " last received EAPOL frame " MACSTR " as "
164 "the EAPOL destination",
165 MAC2STR(dst));
166 }
167 } else {
168 /* BSSID was already set (from (Re)Assoc event, so use it as
169 * the EAPOL destination. */
170 dst = wpa_s->bssid;
171 }
172
173 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
174 if (msg == NULL)
175 return -1;
176
177 wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
178 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
179 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
180 os_free(msg);
181 return res;
182}
183
184
185/**
186 * wpa_eapol_set_wep_key - set WEP key for the driver
187 * @ctx: Pointer to wpa_supplicant data (wpa_s)
188 * @unicast: 1 = individual unicast key, 0 = broadcast key
189 * @keyidx: WEP key index (0..3)
190 * @key: Pointer to key data
191 * @keylen: Key length in bytes
192 * Returns: 0 on success or < 0 on error.
193 */
194static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
195 const u8 *key, size_t keylen)
196{
197 struct wpa_supplicant *wpa_s = ctx;
198 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
199 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
200 WPA_CIPHER_WEP104;
201 if (unicast)
202 wpa_s->pairwise_cipher = cipher;
203 else
204 wpa_s->group_cipher = cipher;
205 }
206 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
0382097e 207 unicast ? wpa_s->bssid : NULL,
da64c266 208 keyidx, unicast, NULL, 0, key, keylen);
6fc6879b
JM
209}
210
211
212static void wpa_supplicant_aborted_cached(void *ctx)
213{
214 struct wpa_supplicant *wpa_s = ctx;
215 wpa_sm_aborted_cached(wpa_s->wpa);
216}
217
218
219static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
220 void *ctx)
221{
222 struct wpa_supplicant *wpa_s = ctx;
223 int res, pmk_len;
224 u8 pmk[PMK_LEN];
225
226 wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
227 success ? "" : "un");
228
fa201b69 229 if (wpas_wps_eapol_cb(wpa_s) > 0)
ad08c363 230 return;
ad08c363 231
0930209d
JM
232 if (!success) {
233 /*
234 * Make sure we do not get stuck here waiting for long EAPOL
235 * timeout if the AP does not disconnect in case of
236 * authentication failure.
237 */
238 wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
239 }
240
c2a04078 241 if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
6fc6879b
JM
242 return;
243
56586197 244 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
6fc6879b
JM
245 return;
246
247 wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
248 "handshake");
249
250 pmk_len = PMK_LEN;
09c395b4
JM
251 if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
252#ifdef CONFIG_IEEE80211R
253 u8 buf[2 * PMK_LEN];
254 wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
255 "driver-based 4-way hs and FT");
256 res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
257 if (res == 0) {
258 os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
259 os_memset(buf, 0, sizeof(buf));
260 }
261#else /* CONFIG_IEEE80211R */
262 res = -1;
263#endif /* CONFIG_IEEE80211R */
264 } else {
265 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
266 if (res) {
267 /*
268 * EAP-LEAP is an exception from other EAP methods: it
269 * uses only 16-byte PMK.
270 */
271 res = eapol_sm_get_key(eapol, pmk, 16);
272 pmk_len = 16;
273 }
6fc6879b
JM
274 }
275
276 if (res) {
277 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
278 "machines");
279 return;
280 }
281
09c395b4
JM
282 wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
283 "handshake", pmk, pmk_len);
284
6fc6879b
JM
285 if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
286 pmk_len)) {
287 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
288 }
289
290 wpa_supplicant_cancel_scan(wpa_s);
291 wpa_supplicant_cancel_auth_timeout(wpa_s);
292 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
293
294}
295
296
297static void wpa_supplicant_notify_eapol_done(void *ctx)
298{
299 struct wpa_supplicant *wpa_s = ctx;
300 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
56586197 301 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6fc6879b
JM
302 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
303 } else {
6fc6879b
JM
304 wpa_supplicant_cancel_auth_timeout(wpa_s);
305 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
306 }
307}
308
309#endif /* IEEE8021X_EAPOL */
310
311
312#ifndef CONFIG_NO_WPA
313
314static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
315{
6fc6879b 316 int ret = 0;
e48f0fb6 317 struct wpa_bss *curr = NULL, *bss;
6fc6879b
JM
318 struct wpa_ssid *ssid = wpa_s->current_ssid;
319 const u8 *ie;
320
e48f0fb6
JM
321 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
322 if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
6fc6879b 323 continue;
6fc6879b 324 if (ssid == NULL ||
e48f0fb6
JM
325 ((bss->ssid_len == ssid->ssid_len &&
326 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
6fc6879b 327 ssid->ssid_len == 0)) {
e48f0fb6 328 curr = bss;
6fc6879b
JM
329 break;
330 }
331 }
332
333 if (curr) {
e48f0fb6 334 ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
6fc6879b
JM
335 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
336 ret = -1;
337
e48f0fb6 338 ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
6fc6879b
JM
339 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
340 ret = -1;
341 } else {
342 ret = -1;
343 }
344
345 return ret;
346}
347
348
349static int wpa_supplicant_get_beacon_ie(void *ctx)
350{
351 struct wpa_supplicant *wpa_s = ctx;
352 if (wpa_get_beacon_ie(wpa_s) == 0) {
353 return 0;
354 }
355
356 /* No WPA/RSN IE found in the cached scan results. Try to get updated
357 * scan results from the driver. */
a1fd2ce5 358 if (wpa_supplicant_update_scan_results(wpa_s) < 0)
6fc6879b 359 return -1;
6fc6879b
JM
360
361 return wpa_get_beacon_ie(wpa_s);
362}
363
364
365static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
366 const void *data, u16 data_len,
367 size_t *msg_len, void **data_pos)
368{
369 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
370}
371
372
373static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
374 const u8 *buf, size_t len)
375{
376 return wpa_ether_send(wpa_s, dest, proto, buf, len);
377}
378
379
6fc6879b
JM
380static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
381{
382 wpa_supplicant_cancel_auth_timeout(wpa_s);
383}
384
385
71934751 386static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
6fc6879b
JM
387{
388 wpa_supplicant_set_state(wpa_s, state);
389}
390
391
392/**
393 * wpa_supplicant_get_state - Get the connection state
394 * @wpa_s: Pointer to wpa_supplicant data
395 * Returns: The current connection state (WPA_*)
396 */
71934751 397static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
6fc6879b
JM
398{
399 return wpa_s->wpa_state;
400}
401
402
71934751 403static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
6fc6879b
JM
404{
405 return wpa_supplicant_get_state(wpa_s);
406}
407
408
409static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
410{
411 wpa_supplicant_disassociate(wpa_s, reason_code);
3e2ad1b9 412 /* Schedule a scan to make sure we continue looking for networks */
83935317 413 wpa_supplicant_req_scan(wpa_s, 5, 0);
6fc6879b
JM
414}
415
416
417static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
418{
419 wpa_supplicant_deauthenticate(wpa_s, reason_code);
3e2ad1b9 420 /* Schedule a scan to make sure we continue looking for networks */
83935317 421 wpa_supplicant_req_scan(wpa_s, 5, 0);
6fc6879b
JM
422}
423
424
425static void * wpa_supplicant_get_network_ctx(void *wpa_s)
426{
427 return wpa_supplicant_get_ssid(wpa_s);
428}
429
430
431static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
432{
433 struct wpa_supplicant *wpa_s = ctx;
6fc6879b
JM
434 return wpa_drv_get_bssid(wpa_s, bssid);
435}
436
437
71934751 438static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
6fc6879b
JM
439 const u8 *addr, int key_idx, int set_tx,
440 const u8 *seq, size_t seq_len,
441 const u8 *key, size_t key_len)
442{
46690a3b
JM
443 struct wpa_supplicant *wpa_s = _wpa_s;
444 if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
445 /* Clear the MIC error counter when setting a new PTK. */
446 wpa_s->mic_errors_seen = 0;
447 }
6fc6879b
JM
448 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
449 key, key_len);
450}
451
452
453static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
454 int protection_type,
455 int key_type)
456{
457 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
458 key_type);
459}
460
461
462static int wpa_supplicant_add_pmkid(void *wpa_s,
463 const u8 *bssid, const u8 *pmkid)
464{
465 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
466}
467
468
469static int wpa_supplicant_remove_pmkid(void *wpa_s,
470 const u8 *bssid, const u8 *pmkid)
471{
472 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
473}
474
475
476#ifdef CONFIG_IEEE80211R
477static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
478 const u8 *ies, size_t ies_len)
479{
480 struct wpa_supplicant *wpa_s = ctx;
c2a04078
JM
481 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
482 return sme_update_ft_ies(wpa_s, md, ies, ies_len);
6fc6879b
JM
483 return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
484}
485
486
487static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
488 const u8 *target_ap,
489 const u8 *ies, size_t ies_len)
490{
491 struct wpa_supplicant *wpa_s = ctx;
6fc6879b
JM
492 return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
493}
2a7e7f4e
JM
494
495
496static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
497{
498 struct wpa_supplicant *wpa_s = ctx;
499 struct wpa_driver_auth_params params;
500 struct wpa_bss *bss;
501
2a7e7f4e
JM
502 bss = wpa_bss_get_bssid(wpa_s, target_ap);
503 if (bss == NULL)
504 return -1;
505
506 os_memset(&params, 0, sizeof(params));
507 params.bssid = target_ap;
508 params.freq = bss->freq;
509 params.ssid = bss->ssid;
510 params.ssid_len = bss->ssid_len;
511 params.auth_alg = WPA_AUTH_ALG_FT;
512 params.local_state_change = 1;
513 return wpa_drv_authenticate(wpa_s, &params);
514}
6fc6879b
JM
515#endif /* CONFIG_IEEE80211R */
516
517#endif /* CONFIG_NO_WPA */
518
519
281ff0aa
GP
520#ifdef CONFIG_TDLS
521
c58ab8f2
AN
522static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
523 int *tdls_ext_setup)
524{
525 struct wpa_supplicant *wpa_s = ctx;
526
527 *tdls_supported = 0;
528 *tdls_ext_setup = 0;
529
530 if (!wpa_s->drv_capa_known)
531 return -1;
532
533 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
534 *tdls_supported = 1;
535
536 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
537 *tdls_ext_setup = 1;
538
539 return 0;
540}
541
542
281ff0aa
GP
543static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
544 u8 action_code, u8 dialog_token,
545 u16 status_code, const u8 *buf,
546 size_t len)
547{
548 struct wpa_supplicant *wpa_s = ctx;
549 return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
550 status_code, buf, len);
551}
552
553
554static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
555{
556 struct wpa_supplicant *wpa_s = ctx;
557 return wpa_drv_tdls_oper(wpa_s, oper, peer);
558}
559
45b722f1
AN
560
561static int wpa_supplicant_tdls_peer_addset(
562 void *ctx, const u8 *peer, int add, u16 capability,
563 const u8 *supp_rates, size_t supp_rates_len)
564{
565 struct wpa_supplicant *wpa_s = ctx;
566 struct hostapd_sta_add_params params;
567
568 params.addr = peer;
569 params.aid = 1;
570 params.capability = capability;
571 params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
572 params.ht_capabilities = NULL;
573 params.listen_interval = 0;
574 params.supp_rates = supp_rates;
575 params.supp_rates_len = supp_rates_len;
576 params.set = !add;
577
578 return wpa_drv_sta_add(wpa_s, &params);
579}
580
281ff0aa
GP
581#endif /* CONFIG_TDLS */
582
583
81c57e22
DW
584enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
585{
586 if (os_strcmp(field, "IDENTITY") == 0)
587 return WPA_CTRL_REQ_EAP_IDENTITY;
588 else if (os_strcmp(field, "PASSWORD") == 0)
589 return WPA_CTRL_REQ_EAP_PASSWORD;
590 else if (os_strcmp(field, "NEW_PASSWORD") == 0)
591 return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
592 else if (os_strcmp(field, "PIN") == 0)
593 return WPA_CTRL_REQ_EAP_PIN;
594 else if (os_strcmp(field, "OTP") == 0)
595 return WPA_CTRL_REQ_EAP_OTP;
596 else if (os_strcmp(field, "PASSPHRASE") == 0)
597 return WPA_CTRL_REQ_EAP_PASSPHRASE;
598 return WPA_CTRL_REQ_UNKNOWN;
599}
600
601
9ef1aaae
DW
602const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
603 const char *default_txt,
604 const char **txt)
605{
606 const char *ret = NULL;
607
608 *txt = default_txt;
609
610 switch (field) {
611 case WPA_CTRL_REQ_EAP_IDENTITY:
612 *txt = "Identity";
613 ret = "IDENTITY";
614 break;
615 case WPA_CTRL_REQ_EAP_PASSWORD:
616 *txt = "Password";
617 ret = "PASSWORD";
618 break;
619 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
620 *txt = "New Password";
621 ret = "NEW_PASSWORD";
622 break;
623 case WPA_CTRL_REQ_EAP_PIN:
624 *txt = "PIN";
625 ret = "PIN";
626 break;
627 case WPA_CTRL_REQ_EAP_OTP:
628 ret = "OTP";
629 break;
630 case WPA_CTRL_REQ_EAP_PASSPHRASE:
631 *txt = "Private key passphrase";
632 ret = "PASSPHRASE";
633 break;
634 default:
635 break;
636 }
637
638 /* txt needs to be something */
639 if (*txt == NULL) {
640 wpa_printf(MSG_WARNING, "No message for request %d", field);
641 ret = NULL;
642 }
643
644 return ret;
645}
646
b1491202 647#ifdef IEEE8021X_EAPOL
6fc6879b 648#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
9ef1aaae
DW
649static void wpa_supplicant_eap_param_needed(void *ctx,
650 enum wpa_ctrl_req_type field,
651 const char *default_txt)
6fc6879b
JM
652{
653 struct wpa_supplicant *wpa_s = ctx;
654 struct wpa_ssid *ssid = wpa_s->current_ssid;
9ef1aaae 655 const char *field_name, *txt = NULL;
6fc6879b
JM
656 char *buf;
657 size_t buflen;
658 int len;
659
660 if (ssid == NULL)
661 return;
662
a9022616
DW
663 wpas_notify_network_request(wpa_s, ssid, field, default_txt);
664
9ef1aaae
DW
665 field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
666 &txt);
667 if (field_name == NULL) {
668 wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
669 field);
670 return;
671 }
672
6fc6879b
JM
673 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
674 buf = os_malloc(buflen);
675 if (buf == NULL)
676 return;
677 len = os_snprintf(buf, buflen,
678 WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
9ef1aaae 679 field_name, ssid->id, txt);
6fc6879b
JM
680 if (len < 0 || (size_t) len >= buflen) {
681 os_free(buf);
682 return;
683 }
684 if (ssid->ssid && buflen > len + ssid->ssid_len) {
685 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
686 len += ssid->ssid_len;
687 buf[len] = '\0';
688 }
689 buf[buflen - 1] = '\0';
690 wpa_msg(wpa_s, MSG_INFO, "%s", buf);
691 os_free(buf);
692}
693#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
694#define wpa_supplicant_eap_param_needed NULL
695#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
696
697
4bc181ec
JM
698static void wpa_supplicant_port_cb(void *ctx, int authorized)
699{
700 struct wpa_supplicant *wpa_s = ctx;
69a6b47a
JM
701#ifdef CONFIG_AP
702 if (wpa_s->ap_iface) {
703 wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
704 "port status: %s",
705 authorized ? "Authorized" : "Unauthorized");
706 return;
707 }
708#endif /* CONFIG_AP */
4bc181ec
JM
709 wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
710 authorized ? "Authorized" : "Unauthorized");
711 wpa_drv_set_supp_port(wpa_s, authorized);
712}
ade74830
MC
713
714
715static void wpa_supplicant_cert_cb(void *ctx, int depth, const char *subject,
716 const char *cert_hash,
717 const struct wpabuf *cert)
718{
719 struct wpa_supplicant *wpa_s = ctx;
720
721 wpas_notify_certification(wpa_s, depth, subject, cert_hash, cert);
722}
dd7fec1f
PS
723
724
725static void wpa_supplicant_status_cb(void *ctx, const char *status,
726 const char *parameter)
727{
728 struct wpa_supplicant *wpa_s = ctx;
729
730 wpas_notify_eap_status(wpa_s, status, parameter);
731}
e026159a
JM
732
733
734static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
735{
736 struct wpa_supplicant *wpa_s = ctx;
737 char *str;
738 int res;
739
740 wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
741 id, len);
742
743 if (wpa_s->current_ssid == NULL)
744 return;
745
746 if (id == NULL) {
747 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
748 "NULL", 0) < 0)
749 return;
750 } else {
751 str = os_malloc(len * 2 + 1);
752 if (str == NULL)
753 return;
754 wpa_snprintf_hex(str, len * 2 + 1, id, len);
755 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
756 str, 0);
757 os_free(str);
758 if (res < 0)
759 return;
760 }
761
762 if (wpa_s->conf->update_config) {
763 res = wpa_config_write(wpa_s->confname, wpa_s->conf);
764 if (res) {
765 wpa_printf(MSG_DEBUG, "Failed to update config after "
766 "anonymous_id update");
767 }
768 }
769}
b1491202 770#endif /* IEEE8021X_EAPOL */
4bc181ec
JM
771
772
6fc6879b
JM
773int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
774{
775#ifdef IEEE8021X_EAPOL
776 struct eapol_ctx *ctx;
777 ctx = os_zalloc(sizeof(*ctx));
778 if (ctx == NULL) {
779 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
780 return -1;
781 }
782
783 ctx->ctx = wpa_s;
784 ctx->msg_ctx = wpa_s;
785 ctx->eapol_send_ctx = wpa_s;
786 ctx->preauth = 0;
787 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
788 ctx->eapol_send = wpa_supplicant_eapol_send;
789 ctx->set_wep_key = wpa_eapol_set_wep_key;
790 ctx->set_config_blob = wpa_supplicant_set_config_blob;
791 ctx->get_config_blob = wpa_supplicant_get_config_blob;
792 ctx->aborted_cached = wpa_supplicant_aborted_cached;
6fc6879b
JM
793 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
794 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
795 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
116654ce 796 ctx->wps = wpa_s->wps;
6fc6879b 797 ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
4bc181ec 798 ctx->port_cb = wpa_supplicant_port_cb;
6fc6879b 799 ctx->cb = wpa_supplicant_eapol_cb;
ade74830 800 ctx->cert_cb = wpa_supplicant_cert_cb;
dd7fec1f 801 ctx->status_cb = wpa_supplicant_status_cb;
e026159a 802 ctx->set_anon_id = wpa_supplicant_set_anon_id;
6fc6879b
JM
803 ctx->cb_ctx = wpa_s;
804 wpa_s->eapol = eapol_sm_init(ctx);
805 if (wpa_s->eapol == NULL) {
806 os_free(ctx);
807 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
808 "machines.");
809 return -1;
810 }
811#endif /* IEEE8021X_EAPOL */
812
813 return 0;
814}
815
816
b14a210c
JB
817static void wpa_supplicant_set_rekey_offload(void *ctx, const u8 *kek,
818 const u8 *kck,
819 const u8 *replay_ctr)
820{
821 struct wpa_supplicant *wpa_s = ctx;
822
823 wpa_drv_set_rekey_info(wpa_s, kek, kck, replay_ctr);
824}
825
826
6fc6879b
JM
827int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
828{
829#ifndef CONFIG_NO_WPA
830 struct wpa_sm_ctx *ctx;
831 ctx = os_zalloc(sizeof(*ctx));
832 if (ctx == NULL) {
833 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
834 return -1;
835 }
836
837 ctx->ctx = wpa_s;
0f057fb2 838 ctx->msg_ctx = wpa_s;
6fc6879b
JM
839 ctx->set_state = _wpa_supplicant_set_state;
840 ctx->get_state = _wpa_supplicant_get_state;
6fc6879b
JM
841 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
842 ctx->disassociate = _wpa_supplicant_disassociate;
843 ctx->set_key = wpa_supplicant_set_key;
844 ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
845 ctx->get_bssid = wpa_supplicant_get_bssid;
846 ctx->ether_send = _wpa_ether_send;
847 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
848 ctx->alloc_eapol = _wpa_alloc_eapol;
849 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
850 ctx->add_pmkid = wpa_supplicant_add_pmkid;
851 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
852#ifndef CONFIG_NO_CONFIG_BLOBS
853 ctx->set_config_blob = wpa_supplicant_set_config_blob;
854 ctx->get_config_blob = wpa_supplicant_get_config_blob;
855#endif /* CONFIG_NO_CONFIG_BLOBS */
856 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
857#ifdef CONFIG_IEEE80211R
858 ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
859 ctx->send_ft_action = wpa_supplicant_send_ft_action;
2a7e7f4e 860 ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
6fc6879b 861#endif /* CONFIG_IEEE80211R */
281ff0aa 862#ifdef CONFIG_TDLS
c58ab8f2 863 ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
281ff0aa
GP
864 ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
865 ctx->tdls_oper = wpa_supplicant_tdls_oper;
45b722f1 866 ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
281ff0aa 867#endif /* CONFIG_TDLS */
b14a210c 868 ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
6fc6879b
JM
869
870 wpa_s->wpa = wpa_sm_init(ctx);
871 if (wpa_s->wpa == NULL) {
872 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
873 "machine");
874 return -1;
875 }
876#endif /* CONFIG_NO_WPA */
877
878 return 0;
879}
880
881
882void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
883 struct wpa_ssid *ssid)
884{
885 struct rsn_supp_config conf;
886 if (ssid) {
887 os_memset(&conf, 0, sizeof(conf));
886a807f 888 conf.network_ctx = ssid;
6fc6879b
JM
889 conf.peerkey_enabled = ssid->peerkey;
890 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
a6a89fea 891#ifdef IEEE8021X_EAPOL
7c444f3c 892 conf.proactive_key_caching = ssid->proactive_key_caching;
6fc6879b
JM
893 conf.eap_workaround = ssid->eap_workaround;
894 conf.eap_conf_ctx = &ssid->eap;
a6a89fea 895#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
896 conf.ssid = ssid->ssid;
897 conf.ssid_len = ssid->ssid_len;
581a8cde 898 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
6fc6879b
JM
899 }
900 wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
901}