• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant - Glue code to setup EAPOL and RSN modules
3  * Copyright (c) 2003-2012, 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 "includes.h"
10 
11 #include "common.h"
12 #include "eapol_supp/eapol_supp_sm.h"
13 #include "rsn_supp/wpa.h"
14 #include "eloop.h"
15 #include "config.h"
16 #include "l2_packet/l2_packet.h"
17 #include "common/wpa_common.h"
18 #include "wpa_supplicant_i.h"
19 #include "driver_i.h"
20 #include "rsn_supp/pmksa_cache.h"
21 #include "sme.h"
22 #include "common/ieee802_11_defs.h"
23 #include "common/wpa_ctrl.h"
24 #include "wpas_glue.h"
25 #include "wps_supplicant.h"
26 #include "bss.h"
27 #include "scan.h"
28 #include "notify.h"
29 
30 
31 #ifndef CONFIG_NO_CONFIG_BLOBS
32 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
wpa_supplicant_set_config_blob(void * ctx,struct wpa_config_blob * blob)33 static 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);
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 	}
45 }
46 
47 
48 static const struct wpa_config_blob *
wpa_supplicant_get_config_blob(void * ctx,const char * name)49 wpa_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)
wpa_alloc_eapol(const struct wpa_supplicant * wpa_s,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)59 static 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  */
wpa_ether_send(struct wpa_supplicant * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)95 static 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  */
wpa_supplicant_eapol_send(void * ctx,int type,const u8 * buf,size_t len)120 static 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 
131 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
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 
151 	if (is_zero_ether_addr(wpa_s->bssid)) {
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 &&
155 		    !is_zero_ether_addr(bssid)) {
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  */
wpa_eapol_set_wep_key(void * ctx,int unicast,int keyidx,const u8 * key,size_t keylen)194 static 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,
207 			       unicast ? wpa_s->bssid : NULL,
208 			       keyidx, unicast, NULL, 0, key, keylen);
209 }
210 
211 
wpa_supplicant_aborted_cached(void * ctx)212 static 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 
wpa_supplicant_eapol_cb(struct eapol_sm * eapol,int success,void * ctx)219 static 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 
229 	if (wpas_wps_eapol_cb(wpa_s) > 0)
230 		return;
231 
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 
241 	if (!success || !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
242 		return;
243 
244 	if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
245 		return;
246 
247 	wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
248 		   "handshake");
249 
250 	pmk_len = PMK_LEN;
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 		}
274 	}
275 
276 	if (res) {
277 		wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
278 			   "machines");
279 		return;
280 	}
281 
282 	wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
283 			"handshake", pmk, pmk_len);
284 
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 
wpa_supplicant_notify_eapol_done(void * ctx)297 static 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");
301 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
302 		wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
303 	} else {
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 
wpa_get_beacon_ie(struct wpa_supplicant * wpa_s)314 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
315 {
316 	int ret = 0;
317 	struct wpa_bss *curr = NULL, *bss;
318 	struct wpa_ssid *ssid = wpa_s->current_ssid;
319 	const u8 *ie;
320 
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)
323 			continue;
324 		if (ssid == NULL ||
325 		    ((bss->ssid_len == ssid->ssid_len &&
326 		      os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
327 		     ssid->ssid_len == 0)) {
328 			curr = bss;
329 			break;
330 		}
331 	}
332 
333 	if (curr) {
334 		ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
335 		if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
336 			ret = -1;
337 
338 		ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
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 
wpa_supplicant_get_beacon_ie(void * ctx)349 static 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. */
358 	if (wpa_supplicant_update_scan_results(wpa_s) < 0)
359 		return -1;
360 
361 	return wpa_get_beacon_ie(wpa_s);
362 }
363 
364 
_wpa_alloc_eapol(void * wpa_s,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)365 static 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 
_wpa_ether_send(void * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)373 static 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 
_wpa_supplicant_cancel_auth_timeout(void * wpa_s)380 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
381 {
382 	wpa_supplicant_cancel_auth_timeout(wpa_s);
383 }
384 
385 
_wpa_supplicant_set_state(void * wpa_s,enum wpa_states state)386 static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
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  */
wpa_supplicant_get_state(struct wpa_supplicant * wpa_s)397 static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
398 {
399 	return wpa_s->wpa_state;
400 }
401 
402 
_wpa_supplicant_get_state(void * wpa_s)403 static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
404 {
405 	return wpa_supplicant_get_state(wpa_s);
406 }
407 
408 
_wpa_supplicant_deauthenticate(void * wpa_s,int reason_code)409 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
410 {
411 	wpa_supplicant_deauthenticate(wpa_s, reason_code);
412 	/* Schedule a scan to make sure we continue looking for networks */
413 	wpa_supplicant_req_scan(wpa_s, 5, 0);
414 }
415 
416 
wpa_supplicant_get_network_ctx(void * wpa_s)417 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
418 {
419 	return wpa_supplicant_get_ssid(wpa_s);
420 }
421 
422 
wpa_supplicant_get_bssid(void * ctx,u8 * bssid)423 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
424 {
425 	struct wpa_supplicant *wpa_s = ctx;
426 	return wpa_drv_get_bssid(wpa_s, bssid);
427 }
428 
429 
wpa_supplicant_set_key(void * _wpa_s,enum wpa_alg alg,const u8 * addr,int key_idx,int set_tx,const u8 * seq,size_t seq_len,const u8 * key,size_t key_len)430 static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
431 				  const u8 *addr, int key_idx, int set_tx,
432 				  const u8 *seq, size_t seq_len,
433 				  const u8 *key, size_t key_len)
434 {
435 	struct wpa_supplicant *wpa_s = _wpa_s;
436 	if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
437 		/* Clear the MIC error counter when setting a new PTK. */
438 		wpa_s->mic_errors_seen = 0;
439 	}
440 	return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
441 			       key, key_len);
442 }
443 
444 
wpa_supplicant_mlme_setprotection(void * wpa_s,const u8 * addr,int protection_type,int key_type)445 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
446 					     int protection_type,
447 					     int key_type)
448 {
449 	return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
450 					  key_type);
451 }
452 
453 
wpa_supplicant_add_pmkid(void * wpa_s,const u8 * bssid,const u8 * pmkid)454 static int wpa_supplicant_add_pmkid(void *wpa_s,
455 				    const u8 *bssid, const u8 *pmkid)
456 {
457 	return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
458 }
459 
460 
wpa_supplicant_remove_pmkid(void * wpa_s,const u8 * bssid,const u8 * pmkid)461 static int wpa_supplicant_remove_pmkid(void *wpa_s,
462 				       const u8 *bssid, const u8 *pmkid)
463 {
464 	return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
465 }
466 
467 
468 #ifdef CONFIG_IEEE80211R
wpa_supplicant_update_ft_ies(void * ctx,const u8 * md,const u8 * ies,size_t ies_len)469 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
470 					const u8 *ies, size_t ies_len)
471 {
472 	struct wpa_supplicant *wpa_s = ctx;
473 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
474 		return sme_update_ft_ies(wpa_s, md, ies, ies_len);
475 	return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
476 }
477 
478 
wpa_supplicant_send_ft_action(void * ctx,u8 action,const u8 * target_ap,const u8 * ies,size_t ies_len)479 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
480 					 const u8 *target_ap,
481 					 const u8 *ies, size_t ies_len)
482 {
483 	struct wpa_supplicant *wpa_s = ctx;
484 	return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
485 }
486 
487 
wpa_supplicant_mark_authenticated(void * ctx,const u8 * target_ap)488 static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
489 {
490 	struct wpa_supplicant *wpa_s = ctx;
491 	struct wpa_driver_auth_params params;
492 	struct wpa_bss *bss;
493 
494 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
495 	if (bss == NULL)
496 		return -1;
497 
498 	os_memset(&params, 0, sizeof(params));
499 	params.bssid = target_ap;
500 	params.freq = bss->freq;
501 	params.ssid = bss->ssid;
502 	params.ssid_len = bss->ssid_len;
503 	params.auth_alg = WPA_AUTH_ALG_FT;
504 	params.local_state_change = 1;
505 	return wpa_drv_authenticate(wpa_s, &params);
506 }
507 #endif /* CONFIG_IEEE80211R */
508 
509 #endif /* CONFIG_NO_WPA */
510 
511 
512 #ifdef CONFIG_TDLS
513 
wpa_supplicant_tdls_get_capa(void * ctx,int * tdls_supported,int * tdls_ext_setup)514 static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
515 					int *tdls_ext_setup)
516 {
517 	struct wpa_supplicant *wpa_s = ctx;
518 
519 	*tdls_supported = 0;
520 	*tdls_ext_setup = 0;
521 
522 	if (!wpa_s->drv_capa_known)
523 		return -1;
524 
525 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
526 		*tdls_supported = 1;
527 
528 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
529 		*tdls_ext_setup = 1;
530 
531 	return 0;
532 }
533 
534 
wpa_supplicant_send_tdls_mgmt(void * ctx,const u8 * dst,u8 action_code,u8 dialog_token,u16 status_code,const u8 * buf,size_t len)535 static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
536 					 u8 action_code, u8 dialog_token,
537 					 u16 status_code, const u8 *buf,
538 					 size_t len)
539 {
540 	struct wpa_supplicant *wpa_s = ctx;
541 	return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
542 				      status_code, buf, len);
543 }
544 
545 
wpa_supplicant_tdls_oper(void * ctx,int oper,const u8 * peer)546 static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
547 {
548 	struct wpa_supplicant *wpa_s = ctx;
549 	return wpa_drv_tdls_oper(wpa_s, oper, peer);
550 }
551 
552 
wpa_supplicant_tdls_peer_addset(void * ctx,const u8 * peer,int add,u16 capability,const u8 * supp_rates,size_t supp_rates_len,const struct ieee80211_ht_capabilities * ht_capab,const struct ieee80211_vht_capabilities * vht_capab,u8 qosinfo,const u8 * ext_capab,size_t ext_capab_len)553 static int wpa_supplicant_tdls_peer_addset(
554 	void *ctx, const u8 *peer, int add, u16 capability,
555 	const u8 *supp_rates, size_t supp_rates_len,
556 	const struct ieee80211_ht_capabilities *ht_capab,
557 	const struct ieee80211_vht_capabilities *vht_capab,
558 	u8 qosinfo, const u8 *ext_capab, size_t ext_capab_len)
559 {
560 	struct wpa_supplicant *wpa_s = ctx;
561 	struct hostapd_sta_add_params params;
562 
563 	os_memset(&params, 0, sizeof(params));
564 
565 	params.addr = peer;
566 	params.aid = 1;
567 	params.capability = capability;
568 	params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
569 
570 	/*
571 	 * TDLS Setup frames do not contain WMM IEs, hence need to depend on
572 	 * qosinfo to check if the peer is WMM capable.
573 	 */
574 	if (qosinfo)
575 		params.flags |= WPA_STA_WMM;
576 
577 	params.ht_capabilities = ht_capab;
578 	params.vht_capabilities = vht_capab;
579 	params.qosinfo = qosinfo;
580 	params.listen_interval = 0;
581 	params.supp_rates = supp_rates;
582 	params.supp_rates_len = supp_rates_len;
583 	params.set = !add;
584 	params.ext_capab = ext_capab;
585 	params.ext_capab_len = ext_capab_len;
586 
587 	return wpa_drv_sta_add(wpa_s, &params);
588 }
589 
590 #endif /* CONFIG_TDLS */
591 
592 
wpa_supplicant_ctrl_req_from_string(const char * field)593 enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
594 {
595 	if (os_strcmp(field, "IDENTITY") == 0)
596 		return WPA_CTRL_REQ_EAP_IDENTITY;
597 	else if (os_strcmp(field, "PASSWORD") == 0)
598 		return WPA_CTRL_REQ_EAP_PASSWORD;
599 	else if (os_strcmp(field, "NEW_PASSWORD") == 0)
600 		return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
601 	else if (os_strcmp(field, "PIN") == 0)
602 		return WPA_CTRL_REQ_EAP_PIN;
603 	else if (os_strcmp(field, "OTP") == 0)
604 		return WPA_CTRL_REQ_EAP_OTP;
605 	else if (os_strcmp(field, "PASSPHRASE") == 0)
606 		return WPA_CTRL_REQ_EAP_PASSPHRASE;
607 	return WPA_CTRL_REQ_UNKNOWN;
608 }
609 
610 
wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,const char * default_txt,const char ** txt)611 const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
612 					       const char *default_txt,
613 					       const char **txt)
614 {
615 	const char *ret = NULL;
616 
617 	*txt = default_txt;
618 
619 	switch (field) {
620 	case WPA_CTRL_REQ_EAP_IDENTITY:
621 		*txt = "Identity";
622 		ret = "IDENTITY";
623 		break;
624 	case WPA_CTRL_REQ_EAP_PASSWORD:
625 		*txt = "Password";
626 		ret = "PASSWORD";
627 		break;
628 	case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
629 		*txt = "New Password";
630 		ret = "NEW_PASSWORD";
631 		break;
632 	case WPA_CTRL_REQ_EAP_PIN:
633 		*txt = "PIN";
634 		ret = "PIN";
635 		break;
636 	case WPA_CTRL_REQ_EAP_OTP:
637 		ret = "OTP";
638 		break;
639 	case WPA_CTRL_REQ_EAP_PASSPHRASE:
640 		*txt = "Private key passphrase";
641 		ret = "PASSPHRASE";
642 		break;
643 	default:
644 		break;
645 	}
646 
647 	/* txt needs to be something */
648 	if (*txt == NULL) {
649 		wpa_printf(MSG_WARNING, "No message for request %d", field);
650 		ret = NULL;
651 	}
652 
653 	return ret;
654 }
655 
656 #ifdef IEEE8021X_EAPOL
657 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
wpa_supplicant_eap_param_needed(void * ctx,enum wpa_ctrl_req_type field,const char * default_txt)658 static void wpa_supplicant_eap_param_needed(void *ctx,
659 					    enum wpa_ctrl_req_type field,
660 					    const char *default_txt)
661 {
662 	struct wpa_supplicant *wpa_s = ctx;
663 	struct wpa_ssid *ssid = wpa_s->current_ssid;
664 	const char *field_name, *txt = NULL;
665 	char *buf;
666 	size_t buflen;
667 	int len;
668 
669 	if (ssid == NULL)
670 		return;
671 
672 	wpas_notify_network_request(wpa_s, ssid, field, default_txt);
673 
674 	field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
675 						       &txt);
676 	if (field_name == NULL) {
677 		wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
678 			   field);
679 		return;
680 	}
681 
682 	wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
683 
684 	buflen = 100 + os_strlen(txt) + ssid->ssid_len;
685 	buf = os_malloc(buflen);
686 	if (buf == NULL)
687 		return;
688 	len = os_snprintf(buf, buflen,
689 			  WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
690 			  field_name, ssid->id, txt);
691 	if (len < 0 || (size_t) len >= buflen) {
692 		os_free(buf);
693 		return;
694 	}
695 	if (ssid->ssid && buflen > len + ssid->ssid_len) {
696 		os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
697 		len += ssid->ssid_len;
698 		buf[len] = '\0';
699 	}
700 	buf[buflen - 1] = '\0';
701 	wpa_msg(wpa_s, MSG_INFO, "%s", buf);
702 	os_free(buf);
703 }
704 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
705 #define wpa_supplicant_eap_param_needed NULL
706 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
707 
708 
wpa_supplicant_port_cb(void * ctx,int authorized)709 static void wpa_supplicant_port_cb(void *ctx, int authorized)
710 {
711 	struct wpa_supplicant *wpa_s = ctx;
712 #ifdef CONFIG_AP
713 	if (wpa_s->ap_iface) {
714 		wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
715 			   "port status: %s",
716 			   authorized ? "Authorized" : "Unauthorized");
717 		return;
718 	}
719 #endif /* CONFIG_AP */
720 	wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
721 		   authorized ? "Authorized" : "Unauthorized");
722 	wpa_drv_set_supp_port(wpa_s, authorized);
723 }
724 
725 
wpa_supplicant_cert_cb(void * ctx,int depth,const char * subject,const char * cert_hash,const struct wpabuf * cert)726 static void wpa_supplicant_cert_cb(void *ctx, int depth, const char *subject,
727 				   const char *cert_hash,
728 				   const struct wpabuf *cert)
729 {
730 	struct wpa_supplicant *wpa_s = ctx;
731 
732 	wpas_notify_certification(wpa_s, depth, subject, cert_hash, cert);
733 }
734 
735 
wpa_supplicant_status_cb(void * ctx,const char * status,const char * parameter)736 static void wpa_supplicant_status_cb(void *ctx, const char *status,
737 				     const char *parameter)
738 {
739 	struct wpa_supplicant *wpa_s = ctx;
740 
741 	wpas_notify_eap_status(wpa_s, status, parameter);
742 }
743 
744 
wpa_supplicant_set_anon_id(void * ctx,const u8 * id,size_t len)745 static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
746 {
747 	struct wpa_supplicant *wpa_s = ctx;
748 	char *str;
749 	int res;
750 
751 	wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
752 			  id, len);
753 
754 	if (wpa_s->current_ssid == NULL)
755 		return;
756 
757 	if (id == NULL) {
758 		if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
759 				   "NULL", 0) < 0)
760 			return;
761 	} else {
762 		str = os_malloc(len * 2 + 1);
763 		if (str == NULL)
764 			return;
765 		wpa_snprintf_hex(str, len * 2 + 1, id, len);
766 		res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
767 				     str, 0);
768 		os_free(str);
769 		if (res < 0)
770 			return;
771 	}
772 
773 	if (wpa_s->conf->update_config) {
774 		res = wpa_config_write(wpa_s->confname, wpa_s->conf);
775 		if (res) {
776 			wpa_printf(MSG_DEBUG, "Failed to update config after "
777 				   "anonymous_id update");
778 		}
779 	}
780 }
781 #endif /* IEEE8021X_EAPOL */
782 
783 
wpa_supplicant_init_eapol(struct wpa_supplicant * wpa_s)784 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
785 {
786 #ifdef IEEE8021X_EAPOL
787 	struct eapol_ctx *ctx;
788 	ctx = os_zalloc(sizeof(*ctx));
789 	if (ctx == NULL) {
790 		wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
791 		return -1;
792 	}
793 
794 	ctx->ctx = wpa_s;
795 	ctx->msg_ctx = wpa_s;
796 	ctx->eapol_send_ctx = wpa_s;
797 	ctx->preauth = 0;
798 	ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
799 	ctx->eapol_send = wpa_supplicant_eapol_send;
800 	ctx->set_wep_key = wpa_eapol_set_wep_key;
801 #ifndef CONFIG_NO_CONFIG_BLOBS
802 	ctx->set_config_blob = wpa_supplicant_set_config_blob;
803 	ctx->get_config_blob = wpa_supplicant_get_config_blob;
804 #endif /* CONFIG_NO_CONFIG_BLOBS */
805 	ctx->aborted_cached = wpa_supplicant_aborted_cached;
806 	ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
807 	ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
808 	ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
809 	ctx->wps = wpa_s->wps;
810 	ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
811 	ctx->port_cb = wpa_supplicant_port_cb;
812 	ctx->cb = wpa_supplicant_eapol_cb;
813 	ctx->cert_cb = wpa_supplicant_cert_cb;
814 	ctx->status_cb = wpa_supplicant_status_cb;
815 	ctx->set_anon_id = wpa_supplicant_set_anon_id;
816 	ctx->cb_ctx = wpa_s;
817 	wpa_s->eapol = eapol_sm_init(ctx);
818 	if (wpa_s->eapol == NULL) {
819 		os_free(ctx);
820 		wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
821 			   "machines.");
822 		return -1;
823 	}
824 #endif /* IEEE8021X_EAPOL */
825 
826 	return 0;
827 }
828 
829 
830 #ifndef CONFIG_NO_WPA
wpa_supplicant_set_rekey_offload(void * ctx,const u8 * kek,const u8 * kck,const u8 * replay_ctr)831 static void wpa_supplicant_set_rekey_offload(void *ctx, const u8 *kek,
832 					     const u8 *kck,
833 					     const u8 *replay_ctr)
834 {
835 	struct wpa_supplicant *wpa_s = ctx;
836 
837 	wpa_drv_set_rekey_info(wpa_s, kek, kck, replay_ctr);
838 }
839 #endif /* CONFIG_NO_WPA */
840 
841 
wpa_supplicant_init_wpa(struct wpa_supplicant * wpa_s)842 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
843 {
844 #ifndef CONFIG_NO_WPA
845 	struct wpa_sm_ctx *ctx;
846 	ctx = os_zalloc(sizeof(*ctx));
847 	if (ctx == NULL) {
848 		wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
849 		return -1;
850 	}
851 
852 	ctx->ctx = wpa_s;
853 	ctx->msg_ctx = wpa_s;
854 	ctx->set_state = _wpa_supplicant_set_state;
855 	ctx->get_state = _wpa_supplicant_get_state;
856 	ctx->deauthenticate = _wpa_supplicant_deauthenticate;
857 	ctx->set_key = wpa_supplicant_set_key;
858 	ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
859 	ctx->get_bssid = wpa_supplicant_get_bssid;
860 	ctx->ether_send = _wpa_ether_send;
861 	ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
862 	ctx->alloc_eapol = _wpa_alloc_eapol;
863 	ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
864 	ctx->add_pmkid = wpa_supplicant_add_pmkid;
865 	ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
866 #ifndef CONFIG_NO_CONFIG_BLOBS
867 	ctx->set_config_blob = wpa_supplicant_set_config_blob;
868 	ctx->get_config_blob = wpa_supplicant_get_config_blob;
869 #endif /* CONFIG_NO_CONFIG_BLOBS */
870 	ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
871 #ifdef CONFIG_IEEE80211R
872 	ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
873 	ctx->send_ft_action = wpa_supplicant_send_ft_action;
874 	ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
875 #endif /* CONFIG_IEEE80211R */
876 #ifdef CONFIG_TDLS
877 	ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
878 	ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
879 	ctx->tdls_oper = wpa_supplicant_tdls_oper;
880 	ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
881 #endif /* CONFIG_TDLS */
882 	ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
883 
884 	wpa_s->wpa = wpa_sm_init(ctx);
885 	if (wpa_s->wpa == NULL) {
886 		wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
887 			   "machine");
888 		return -1;
889 	}
890 #endif /* CONFIG_NO_WPA */
891 
892 	return 0;
893 }
894 
895 
wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)896 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
897 					struct wpa_ssid *ssid)
898 {
899 	struct rsn_supp_config conf;
900 	if (ssid) {
901 		os_memset(&conf, 0, sizeof(conf));
902 		conf.network_ctx = ssid;
903 		conf.peerkey_enabled = ssid->peerkey;
904 		conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
905 #ifdef IEEE8021X_EAPOL
906 		conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
907 			wpa_s->conf->okc : ssid->proactive_key_caching;
908 		conf.eap_workaround = ssid->eap_workaround;
909 		conf.eap_conf_ctx = &ssid->eap;
910 #endif /* IEEE8021X_EAPOL */
911 		conf.ssid = ssid->ssid;
912 		conf.ssid_len = ssid->ssid_len;
913 		conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
914 	}
915 	wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
916 }
917