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