• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Driver interaction with Linux nl80211/cfg80211 - Event processing
3  * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
5  * Copyright (c) 2009-2010, Atheros Communications
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10 
11 #include "includes.h"
12 #include <netlink/genl/genl.h>
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "common/qca-vendor.h"
17 #include "common/qca-vendor-attr.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "driver_nl80211.h"
21 #ifdef CONFIG_DRIVER_NL80211_BRCM
22 #include "common/brcm_vendor.h"
23 #endif /* CONFIG_DRIVER_NL80211_BRCM */
24 
25 static void
26 nl80211_control_port_frame_tx_status(struct wpa_driver_nl80211_data *drv,
27 				     const u8 *frame, size_t len,
28 				     struct nlattr *ack, struct nlattr *cookie);
29 
30 
nl80211_command_to_string(enum nl80211_commands cmd)31 static const char * nl80211_command_to_string(enum nl80211_commands cmd)
32 {
33 #define C2S(x) case x: return #x;
34 	switch (cmd) {
35 	C2S(NL80211_CMD_UNSPEC)
36 	C2S(NL80211_CMD_GET_WIPHY)
37 	C2S(NL80211_CMD_SET_WIPHY)
38 	C2S(NL80211_CMD_NEW_WIPHY)
39 	C2S(NL80211_CMD_DEL_WIPHY)
40 	C2S(NL80211_CMD_GET_INTERFACE)
41 	C2S(NL80211_CMD_SET_INTERFACE)
42 	C2S(NL80211_CMD_NEW_INTERFACE)
43 	C2S(NL80211_CMD_DEL_INTERFACE)
44 	C2S(NL80211_CMD_GET_KEY)
45 	C2S(NL80211_CMD_SET_KEY)
46 	C2S(NL80211_CMD_NEW_KEY)
47 	C2S(NL80211_CMD_DEL_KEY)
48 	C2S(NL80211_CMD_GET_BEACON)
49 	C2S(NL80211_CMD_SET_BEACON)
50 	C2S(NL80211_CMD_START_AP)
51 	C2S(NL80211_CMD_STOP_AP)
52 	C2S(NL80211_CMD_GET_STATION)
53 	C2S(NL80211_CMD_SET_STATION)
54 	C2S(NL80211_CMD_NEW_STATION)
55 	C2S(NL80211_CMD_DEL_STATION)
56 	C2S(NL80211_CMD_GET_MPATH)
57 	C2S(NL80211_CMD_SET_MPATH)
58 	C2S(NL80211_CMD_NEW_MPATH)
59 	C2S(NL80211_CMD_DEL_MPATH)
60 	C2S(NL80211_CMD_SET_BSS)
61 	C2S(NL80211_CMD_SET_REG)
62 	C2S(NL80211_CMD_REQ_SET_REG)
63 	C2S(NL80211_CMD_GET_MESH_CONFIG)
64 	C2S(NL80211_CMD_SET_MESH_CONFIG)
65 	C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
66 	C2S(NL80211_CMD_GET_REG)
67 	C2S(NL80211_CMD_GET_SCAN)
68 	C2S(NL80211_CMD_TRIGGER_SCAN)
69 	C2S(NL80211_CMD_NEW_SCAN_RESULTS)
70 	C2S(NL80211_CMD_SCAN_ABORTED)
71 	C2S(NL80211_CMD_REG_CHANGE)
72 	C2S(NL80211_CMD_AUTHENTICATE)
73 	C2S(NL80211_CMD_ASSOCIATE)
74 	C2S(NL80211_CMD_DEAUTHENTICATE)
75 	C2S(NL80211_CMD_DISASSOCIATE)
76 	C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
77 	C2S(NL80211_CMD_REG_BEACON_HINT)
78 	C2S(NL80211_CMD_JOIN_IBSS)
79 	C2S(NL80211_CMD_LEAVE_IBSS)
80 	C2S(NL80211_CMD_TESTMODE)
81 	C2S(NL80211_CMD_CONNECT)
82 	C2S(NL80211_CMD_ROAM)
83 	C2S(NL80211_CMD_DISCONNECT)
84 	C2S(NL80211_CMD_SET_WIPHY_NETNS)
85 	C2S(NL80211_CMD_GET_SURVEY)
86 	C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
87 	C2S(NL80211_CMD_SET_PMKSA)
88 	C2S(NL80211_CMD_DEL_PMKSA)
89 	C2S(NL80211_CMD_FLUSH_PMKSA)
90 	C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
91 	C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
92 	C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
93 	C2S(NL80211_CMD_REGISTER_FRAME)
94 	C2S(NL80211_CMD_FRAME)
95 	C2S(NL80211_CMD_FRAME_TX_STATUS)
96 	C2S(NL80211_CMD_SET_POWER_SAVE)
97 	C2S(NL80211_CMD_GET_POWER_SAVE)
98 	C2S(NL80211_CMD_SET_CQM)
99 	C2S(NL80211_CMD_NOTIFY_CQM)
100 	C2S(NL80211_CMD_SET_CHANNEL)
101 	C2S(NL80211_CMD_SET_WDS_PEER)
102 	C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
103 	C2S(NL80211_CMD_JOIN_MESH)
104 	C2S(NL80211_CMD_LEAVE_MESH)
105 	C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
106 	C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
107 	C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
108 	C2S(NL80211_CMD_GET_WOWLAN)
109 	C2S(NL80211_CMD_SET_WOWLAN)
110 	C2S(NL80211_CMD_START_SCHED_SCAN)
111 	C2S(NL80211_CMD_STOP_SCHED_SCAN)
112 	C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
113 	C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
114 	C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
115 	C2S(NL80211_CMD_PMKSA_CANDIDATE)
116 	C2S(NL80211_CMD_TDLS_OPER)
117 	C2S(NL80211_CMD_TDLS_MGMT)
118 	C2S(NL80211_CMD_UNEXPECTED_FRAME)
119 	C2S(NL80211_CMD_PROBE_CLIENT)
120 	C2S(NL80211_CMD_REGISTER_BEACONS)
121 	C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
122 	C2S(NL80211_CMD_SET_NOACK_MAP)
123 	C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
124 	C2S(NL80211_CMD_START_P2P_DEVICE)
125 	C2S(NL80211_CMD_STOP_P2P_DEVICE)
126 	C2S(NL80211_CMD_CONN_FAILED)
127 	C2S(NL80211_CMD_SET_MCAST_RATE)
128 	C2S(NL80211_CMD_SET_MAC_ACL)
129 	C2S(NL80211_CMD_RADAR_DETECT)
130 	C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
131 	C2S(NL80211_CMD_UPDATE_FT_IES)
132 	C2S(NL80211_CMD_FT_EVENT)
133 	C2S(NL80211_CMD_CRIT_PROTOCOL_START)
134 	C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
135 	C2S(NL80211_CMD_GET_COALESCE)
136 	C2S(NL80211_CMD_SET_COALESCE)
137 	C2S(NL80211_CMD_CHANNEL_SWITCH)
138 	C2S(NL80211_CMD_VENDOR)
139 	C2S(NL80211_CMD_SET_QOS_MAP)
140 	C2S(NL80211_CMD_ADD_TX_TS)
141 	C2S(NL80211_CMD_DEL_TX_TS)
142 	C2S(NL80211_CMD_WIPHY_REG_CHANGE)
143 	C2S(NL80211_CMD_PORT_AUTHORIZED)
144 	C2S(NL80211_CMD_EXTERNAL_AUTH)
145 	C2S(NL80211_CMD_STA_OPMODE_CHANGED)
146 	C2S(NL80211_CMD_CONTROL_PORT_FRAME)
147 	C2S(NL80211_CMD_UPDATE_OWE_INFO)
148 	C2S(NL80211_CMD_UNPROT_BEACON)
149 	C2S(NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS)
150 
151 	default:
152 		return "NL80211_CMD_UNKNOWN";
153 	}
154 #undef C2S
155 }
156 
157 
mlme_event_auth(struct wpa_driver_nl80211_data * drv,const u8 * frame,size_t len)158 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
159 			    const u8 *frame, size_t len)
160 {
161 	const struct ieee80211_mgmt *mgmt;
162 	union wpa_event_data event;
163 
164 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
165 	    drv->force_connect_cmd) {
166 		/*
167 		 * Avoid reporting two association events that would confuse
168 		 * the core code.
169 		 */
170 		wpa_printf(MSG_DEBUG,
171 			   "nl80211: Ignore auth event when using driver SME");
172 		return;
173 	}
174 
175 	wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
176 	mgmt = (const struct ieee80211_mgmt *) frame;
177 	if (len < 24 + sizeof(mgmt->u.auth)) {
178 		wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
179 			   "frame");
180 		return;
181 	}
182 
183 	os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
184 	os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
185 	os_memset(&event, 0, sizeof(event));
186 	os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
187 	event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
188 	event.auth.auth_transaction =
189 		le_to_host16(mgmt->u.auth.auth_transaction);
190 	event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
191 	if (len > 24 + sizeof(mgmt->u.auth)) {
192 		event.auth.ies = mgmt->u.auth.variable;
193 		event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
194 	}
195 
196 	wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
197 }
198 
199 
nl80211_parse_wmm_params(struct nlattr * wmm_attr,struct wmm_params * wmm_params)200 static void nl80211_parse_wmm_params(struct nlattr *wmm_attr,
201 				     struct wmm_params *wmm_params)
202 {
203 	struct nlattr *wmm_info[NL80211_STA_WME_MAX + 1];
204 	static struct nla_policy wme_policy[NL80211_STA_WME_MAX + 1] = {
205 		[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
206 	};
207 
208 	if (!wmm_attr ||
209 	    nla_parse_nested(wmm_info, NL80211_STA_WME_MAX, wmm_attr,
210 			     wme_policy) ||
211 	    !wmm_info[NL80211_STA_WME_UAPSD_QUEUES])
212 		return;
213 
214 	wmm_params->uapsd_queues =
215 		nla_get_u8(wmm_info[NL80211_STA_WME_UAPSD_QUEUES]);
216 	wmm_params->info_bitmap |= WMM_PARAMS_UAPSD_QUEUES_INFO;
217 }
218 
219 
mlme_event_assoc(struct wpa_driver_nl80211_data * drv,const u8 * frame,size_t len,struct nlattr * wmm,struct nlattr * req_ie)220 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
221 			     const u8 *frame, size_t len, struct nlattr *wmm,
222 			     struct nlattr *req_ie)
223 {
224 	const struct ieee80211_mgmt *mgmt;
225 	union wpa_event_data event;
226 	u16 status;
227 	int ssid_len;
228 
229 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
230 	    drv->force_connect_cmd) {
231 		/*
232 		 * Avoid reporting two association events that would confuse
233 		 * the core code.
234 		 */
235 		wpa_printf(MSG_DEBUG,
236 			   "nl80211: Ignore assoc event when using driver SME");
237 		return;
238 	}
239 
240 	wpa_printf(MSG_DEBUG, "nl80211: Associate event");
241 	mgmt = (const struct ieee80211_mgmt *) frame;
242 	if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
243 		wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
244 			   "frame");
245 		return;
246 	}
247 
248 	status = le_to_host16(mgmt->u.assoc_resp.status_code);
249 	if (status != WLAN_STATUS_SUCCESS) {
250 		os_memset(&event, 0, sizeof(event));
251 		event.assoc_reject.bssid = mgmt->bssid;
252 		if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
253 			event.assoc_reject.resp_ies =
254 				(u8 *) mgmt->u.assoc_resp.variable;
255 			event.assoc_reject.resp_ies_len =
256 				len - 24 - sizeof(mgmt->u.assoc_resp);
257 		}
258 		event.assoc_reject.status_code = status;
259 
260 		wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
261 		return;
262 	}
263 
264 	drv->associated = 1;
265 	os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
266 	os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
267 
268 	os_memset(&event, 0, sizeof(event));
269 	event.assoc_info.resp_frame = frame;
270 	event.assoc_info.resp_frame_len = len;
271 	if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
272 		event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
273 		event.assoc_info.resp_ies_len =
274 			len - 24 - sizeof(mgmt->u.assoc_resp);
275 	}
276 
277 	if (req_ie) {
278 		event.assoc_info.req_ies = nla_data(req_ie);
279 		event.assoc_info.req_ies_len = nla_len(req_ie);
280 	}
281 
282 	/* When this association was initiated outside of wpa_supplicant,
283 	 * drv->ssid needs to be set here to satisfy later checking. */
284 	ssid_len = nl80211_get_assoc_ssid(drv, drv->ssid);
285 	if (ssid_len > 0) {
286 		drv->ssid_len = ssid_len;
287 		wpa_printf(MSG_DEBUG,
288 			   "nl80211: Set drv->ssid based on scan res info to '%s'",
289 			   wpa_ssid_txt(drv->ssid, drv->ssid_len));
290 	}
291 
292 	event.assoc_info.freq = drv->assoc_freq;
293 	drv->first_bss->freq = drv->assoc_freq;
294 
295 	nl80211_parse_wmm_params(wmm, &event.assoc_info.wmm_params);
296 
297 	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
298 }
299 
300 
301 #ifdef CONFIG_DRIVER_NL80211_QCA
302 
qca_drv_connect_fail_reason_code_handler(struct nl_msg * msg,void * arg)303 static int qca_drv_connect_fail_reason_code_handler(struct nl_msg *msg,
304 						    void *arg)
305 {
306 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
307 	struct nlattr *tb_sta_info[QCA_WLAN_VENDOR_ATTR_GET_STA_INFO_MAX + 1];
308 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
309 	u32 *reason_code = arg;
310 
311 	*reason_code = 0;
312 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
313 		  genlmsg_attrlen(gnlh, 0), NULL);
314 
315 	if (!tb[NL80211_ATTR_VENDOR_DATA]) {
316 		wpa_printf(MSG_ERROR, "%s: Vendor data not found", __func__);
317 		return NL_SKIP;
318 	}
319 
320 	nla_parse(tb_sta_info, QCA_WLAN_VENDOR_ATTR_GET_STA_INFO_MAX,
321 		  nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
322 		  nla_len(tb[NL80211_ATTR_VENDOR_DATA]), NULL);
323 
324 	if (!tb_sta_info[QCA_WLAN_VENDOR_ATTR_GET_STA_INFO_CONNECT_FAIL_REASON_CODE]) {
325 		wpa_printf(MSG_INFO, "%s: Vendor attr not found", __func__);
326 		return NL_SKIP;
327 	}
328 
329 	*reason_code = nla_get_u32(tb_sta_info[QCA_WLAN_VENDOR_ATTR_GET_STA_INFO_CONNECT_FAIL_REASON_CODE]);
330 
331 	return NL_SKIP;
332 }
333 
334 
335 static enum qca_sta_connect_fail_reason_codes
drv_get_connect_fail_reason_code(struct wpa_driver_nl80211_data * drv)336 drv_get_connect_fail_reason_code(struct wpa_driver_nl80211_data *drv)
337 {
338 	enum qca_sta_connect_fail_reason_codes reason_code;
339 	struct nl_msg *msg;
340 	int ret;
341 
342 	msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR);
343 	if (!msg || nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
344 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
345 			QCA_NL80211_VENDOR_SUBCMD_GET_STA_INFO)) {
346 		nlmsg_free(msg);
347 		return 0;
348 	}
349 
350 	ret = send_and_recv_msgs(drv, msg,
351 				 qca_drv_connect_fail_reason_code_handler,
352 				 &reason_code, NULL, NULL);
353 	if (ret)
354 		wpa_printf(MSG_DEBUG,
355 			   "nl80211: Get connect fail reason_code failed: ret=%d (%s)",
356 			   ret, strerror(-ret));
357 
358 	return reason_code;
359 }
360 
361 
362 static enum sta_connect_fail_reason_codes
convert_connect_fail_reason_codes(enum qca_sta_connect_fail_reason_codes reason_code)363 convert_connect_fail_reason_codes(enum qca_sta_connect_fail_reason_codes
364 				  reason_code)
365 {
366 	switch (reason_code) {
367 	case QCA_STA_CONNECT_FAIL_REASON_NO_BSS_FOUND:
368 		return STA_CONNECT_FAIL_REASON_NO_BSS_FOUND;
369 	case QCA_STA_CONNECT_FAIL_REASON_AUTH_TX_FAIL:
370 		return STA_CONNECT_FAIL_REASON_AUTH_TX_FAIL;
371 	case QCA_STA_CONNECT_FAIL_REASON_AUTH_NO_ACK_RECEIVED:
372 		return STA_CONNECT_FAIL_REASON_AUTH_NO_ACK_RECEIVED;
373 	case QCA_STA_CONNECT_FAIL_REASON_AUTH_NO_RESP_RECEIVED:
374 		return STA_CONNECT_FAIL_REASON_AUTH_NO_RESP_RECEIVED;
375 	case QCA_STA_CONNECT_FAIL_REASON_ASSOC_REQ_TX_FAIL:
376 		return STA_CONNECT_FAIL_REASON_ASSOC_REQ_TX_FAIL;
377 	case QCA_STA_CONNECT_FAIL_REASON_ASSOC_NO_ACK_RECEIVED:
378 		return STA_CONNECT_FAIL_REASON_ASSOC_NO_ACK_RECEIVED;
379 	case QCA_STA_CONNECT_FAIL_REASON_ASSOC_NO_RESP_RECEIVED:
380 		return STA_CONNECT_FAIL_REASON_ASSOC_NO_RESP_RECEIVED;
381 	default:
382 		return STA_CONNECT_FAIL_REASON_UNSPECIFIED;
383 	}
384 }
385 
386 #endif /* CONFIG_DRIVER_NL80211_QCA */
387 
388 
mlme_event_connect(struct wpa_driver_nl80211_data * drv,enum nl80211_commands cmd,struct nlattr * status,struct nlattr * addr,struct nlattr * req_ie,struct nlattr * resp_ie,struct nlattr * timed_out,struct nlattr * timeout_reason,struct nlattr * authorized,struct nlattr * key_replay_ctr,struct nlattr * ptk_kck,struct nlattr * ptk_kek,struct nlattr * subnet_status,struct nlattr * fils_erp_next_seq_num,struct nlattr * fils_pmk,struct nlattr * fils_pmkid)389 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
390 			       enum nl80211_commands cmd, struct nlattr *status,
391 			       struct nlattr *addr, struct nlattr *req_ie,
392 			       struct nlattr *resp_ie,
393 			       struct nlattr *timed_out,
394 			       struct nlattr *timeout_reason,
395 			       struct nlattr *authorized,
396 			       struct nlattr *key_replay_ctr,
397 			       struct nlattr *ptk_kck,
398 			       struct nlattr *ptk_kek,
399 			       struct nlattr *subnet_status,
400 			       struct nlattr *fils_erp_next_seq_num,
401 			       struct nlattr *fils_pmk,
402 			       struct nlattr *fils_pmkid)
403 {
404 	union wpa_event_data event;
405 	const u8 *ssid = NULL;
406 	u16 status_code;
407 	int ssid_len;
408 
409 	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
410 		/*
411 		 * Avoid reporting two association events that would confuse
412 		 * the core code.
413 		 */
414 		wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
415 			   "when using userspace SME", cmd);
416 		return;
417 	}
418 
419 	drv->connect_reassoc = 0;
420 
421 	status_code = status ? nla_get_u16(status) : WLAN_STATUS_SUCCESS;
422 
423 	if (cmd == NL80211_CMD_CONNECT) {
424 		wpa_printf(MSG_DEBUG,
425 			   "nl80211: Connect event (status=%u ignore_next_local_disconnect=%d)",
426 			   status_code, drv->ignore_next_local_disconnect);
427 	} else if (cmd == NL80211_CMD_ROAM) {
428 		wpa_printf(MSG_DEBUG, "nl80211: Roam event");
429 	}
430 
431 	os_memset(&event, 0, sizeof(event));
432 	if (cmd == NL80211_CMD_CONNECT && status_code != WLAN_STATUS_SUCCESS) {
433 		if (addr)
434 			event.assoc_reject.bssid = nla_data(addr);
435 		if (drv->ignore_next_local_disconnect) {
436 			drv->ignore_next_local_disconnect = 0;
437 			if (!event.assoc_reject.bssid ||
438 			    (os_memcmp(event.assoc_reject.bssid,
439 				       drv->auth_attempt_bssid,
440 				       ETH_ALEN) != 0)) {
441 				/*
442 				 * Ignore the event that came without a BSSID or
443 				 * for the old connection since this is likely
444 				 * not relevant to the new Connect command.
445 				 */
446 				wpa_printf(MSG_DEBUG,
447 					   "nl80211: Ignore connection failure event triggered during reassociation");
448 				return;
449 			}
450 		}
451 		if (resp_ie) {
452 			event.assoc_reject.resp_ies = nla_data(resp_ie);
453 			event.assoc_reject.resp_ies_len = nla_len(resp_ie);
454 		}
455 		event.assoc_reject.status_code = status_code;
456 		event.assoc_reject.timed_out = timed_out != NULL;
457 		if (timed_out && timeout_reason) {
458 			enum nl80211_timeout_reason reason;
459 
460 			reason = nla_get_u32(timeout_reason);
461 			switch (reason) {
462 			case NL80211_TIMEOUT_SCAN:
463 				event.assoc_reject.timeout_reason = "scan";
464 				break;
465 			case NL80211_TIMEOUT_AUTH:
466 				event.assoc_reject.timeout_reason = "auth";
467 				break;
468 			case NL80211_TIMEOUT_ASSOC:
469 				event.assoc_reject.timeout_reason = "assoc";
470 				break;
471 			default:
472 				break;
473 			}
474 		}
475 		if (fils_erp_next_seq_num)
476 			event.assoc_reject.fils_erp_next_seq_num =
477 				nla_get_u16(fils_erp_next_seq_num);
478 
479 #ifdef CONFIG_DRIVER_NL80211_QCA
480 		if (drv->get_sta_info_vendor_cmd_avail) {
481 			enum qca_sta_connect_fail_reason_codes reason_code;
482 
483 			reason_code = drv_get_connect_fail_reason_code(drv);
484 			event.assoc_reject.reason_code =
485 				convert_connect_fail_reason_codes(reason_code);
486 		}
487 #endif /* CONFIG_DRIVER_NL80211_QCA */
488 
489 		wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
490 		return;
491 	}
492 
493 	drv->associated = 1;
494 	if (addr) {
495 		os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
496 		os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
497 	}
498 
499 	if (req_ie) {
500 		event.assoc_info.req_ies = nla_data(req_ie);
501 		event.assoc_info.req_ies_len = nla_len(req_ie);
502 
503 		if (cmd == NL80211_CMD_ROAM) {
504 			ssid = get_ie(event.assoc_info.req_ies,
505 				      event.assoc_info.req_ies_len,
506 				      WLAN_EID_SSID);
507 			if (ssid && ssid[1] > 0 && ssid[1] <= 32) {
508 				drv->ssid_len = ssid[1];
509 				os_memcpy(drv->ssid, ssid + 2, ssid[1]);
510 				wpa_printf(MSG_DEBUG,
511 					   "nl80211: Set drv->ssid based on req_ie to '%s'",
512 					   wpa_ssid_txt(drv->ssid,
513 							drv->ssid_len));
514 			}
515 		}
516 	}
517 	if (resp_ie) {
518 		event.assoc_info.resp_ies = nla_data(resp_ie);
519 		event.assoc_info.resp_ies_len = nla_len(resp_ie);
520 	}
521 
522 	event.assoc_info.freq = nl80211_get_assoc_freq(drv);
523 	drv->first_bss->freq = drv->assoc_freq;
524 
525 	if ((!ssid || ssid[1] == 0 || ssid[1] > 32) &&
526 	    (ssid_len = nl80211_get_assoc_ssid(drv, drv->ssid)) > 0) {
527 		/* When this connection was initiated outside of wpa_supplicant,
528 		 * drv->ssid needs to be set here to satisfy later checking. */
529 		drv->ssid_len = ssid_len;
530 		wpa_printf(MSG_DEBUG,
531 			   "nl80211: Set drv->ssid based on scan res info to '%s'",
532 			   wpa_ssid_txt(drv->ssid, drv->ssid_len));
533 	}
534 
535 	if (authorized && nla_get_u8(authorized)) {
536 		event.assoc_info.authorized = 1;
537 		wpa_printf(MSG_DEBUG, "nl80211: connection authorized");
538 	}
539 	if (key_replay_ctr) {
540 		event.assoc_info.key_replay_ctr = nla_data(key_replay_ctr);
541 		event.assoc_info.key_replay_ctr_len = nla_len(key_replay_ctr);
542 	}
543 	if (ptk_kck) {
544 		event.assoc_info.ptk_kck = nla_data(ptk_kck);
545 		event.assoc_info.ptk_kck_len = nla_len(ptk_kck);
546 	}
547 	if (ptk_kek) {
548 		event.assoc_info.ptk_kek = nla_data(ptk_kek);
549 		event.assoc_info.ptk_kek_len = nla_len(ptk_kek);
550 	}
551 
552 	if (subnet_status) {
553 		/*
554 		 * At least for now, this is only available from
555 		 * QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_SUBNET_STATUS and that
556 		 * attribute has the same values 0, 1, 2 as are used in the
557 		 * variable here, so no mapping between different values are
558 		 * needed.
559 		 */
560 		event.assoc_info.subnet_status = nla_get_u8(subnet_status);
561 	}
562 
563 	if (fils_erp_next_seq_num)
564 		event.assoc_info.fils_erp_next_seq_num =
565 			nla_get_u16(fils_erp_next_seq_num);
566 
567 	if (fils_pmk) {
568 		event.assoc_info.fils_pmk = nla_data(fils_pmk);
569 		event.assoc_info.fils_pmk_len = nla_len(fils_pmk);
570 	}
571 
572 	if (fils_pmkid)
573 		event.assoc_info.fils_pmkid = nla_data(fils_pmkid);
574 
575 	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
576 
577 	/* Avoid a race condition by stopping to ignore any following
578 	 * disconnection events now that the driver has indicated it is
579 	 * connected since that connection could have been triggered by a roam
580 	 * operation that happened in parallel with the disconnection request.
581 	 */
582 	drv->ignore_next_local_disconnect = 0;
583 }
584 
585 
mlme_event_disconnect(struct wpa_driver_nl80211_data * drv,struct nlattr * reason,struct nlattr * addr,struct nlattr * by_ap)586 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
587 				  struct nlattr *reason, struct nlattr *addr,
588 				  struct nlattr *by_ap)
589 {
590 	union wpa_event_data data;
591 	unsigned int locally_generated = by_ap == NULL;
592 
593 	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
594 		/*
595 		 * Avoid reporting two disassociation events that could
596 		 * confuse the core code.
597 		 */
598 		wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
599 			   "event when using userspace SME");
600 		return;
601 	}
602 
603 	if (drv->ignore_next_local_disconnect) {
604 		drv->ignore_next_local_disconnect = 0;
605 		if (locally_generated) {
606 			wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
607 				   "event triggered during reassociation");
608 			return;
609 		}
610 		wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
611 			   "disconnect but got another disconnect "
612 			   "event first");
613 	}
614 
615 	wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
616 	nl80211_mark_disconnected(drv);
617 	os_memset(&data, 0, sizeof(data));
618 	if (reason)
619 		data.deauth_info.reason_code = nla_get_u16(reason);
620 	data.deauth_info.locally_generated = by_ap == NULL;
621 	wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
622 }
623 
624 
calculate_chan_offset(int width,int freq,int cf1,int cf2)625 static int calculate_chan_offset(int width, int freq, int cf1, int cf2)
626 {
627 	int freq1 = 0;
628 
629 	switch (convert2width(width)) {
630 	case CHAN_WIDTH_20_NOHT:
631 	case CHAN_WIDTH_20:
632 		return 0;
633 	case CHAN_WIDTH_40:
634 		freq1 = cf1 - 10;
635 		break;
636 	case CHAN_WIDTH_80:
637 		freq1 = cf1 - 30;
638 		break;
639 	case CHAN_WIDTH_160:
640 		freq1 = cf1 - 70;
641 		break;
642 	case CHAN_WIDTH_UNKNOWN:
643 	case CHAN_WIDTH_80P80:
644 	case CHAN_WIDTH_2160:
645 	case CHAN_WIDTH_4320:
646 	case CHAN_WIDTH_6480:
647 	case CHAN_WIDTH_8640:
648 		/* FIXME: implement this */
649 		return 0;
650 	}
651 
652 	return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
653 }
654 
655 
mlme_event_ch_switch(struct wpa_driver_nl80211_data * drv,struct nlattr * ifindex,struct nlattr * freq,struct nlattr * type,struct nlattr * bw,struct nlattr * cf1,struct nlattr * cf2,int finished)656 static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
657 				 struct nlattr *ifindex, struct nlattr *freq,
658 				 struct nlattr *type, struct nlattr *bw,
659 				 struct nlattr *cf1, struct nlattr *cf2,
660 				 int finished)
661 {
662 	struct i802_bss *bss;
663 	union wpa_event_data data;
664 	int ht_enabled = 1;
665 	int chan_offset = 0;
666 	int ifidx;
667 
668 	wpa_printf(MSG_DEBUG, "nl80211: Channel switch%s event",
669 		   finished ? "" : " started");
670 
671 	if (!freq)
672 		return;
673 
674 	ifidx = nla_get_u32(ifindex);
675 	bss = get_bss_ifindex(drv, ifidx);
676 	if (bss == NULL) {
677 		wpa_printf(MSG_WARNING, "nl80211: Unknown ifindex (%d) for channel switch, ignoring",
678 			   ifidx);
679 		return;
680 	}
681 
682 	if (type) {
683 		enum nl80211_channel_type ch_type = nla_get_u32(type);
684 
685 		wpa_printf(MSG_DEBUG, "nl80211: Channel type: %d", ch_type);
686 		switch (ch_type) {
687 		case NL80211_CHAN_NO_HT:
688 			ht_enabled = 0;
689 			break;
690 		case NL80211_CHAN_HT20:
691 			break;
692 		case NL80211_CHAN_HT40PLUS:
693 			chan_offset = 1;
694 			break;
695 		case NL80211_CHAN_HT40MINUS:
696 			chan_offset = -1;
697 			break;
698 		}
699 	} else if (bw && cf1) {
700 		/* This can happen for example with VHT80 ch switch */
701 		chan_offset = calculate_chan_offset(nla_get_u32(bw),
702 						    nla_get_u32(freq),
703 						    nla_get_u32(cf1),
704 						    cf2 ? nla_get_u32(cf2) : 0);
705 	} else {
706 		wpa_printf(MSG_WARNING, "nl80211: Unknown secondary channel information - following channel definition calculations may fail");
707 	}
708 
709 	os_memset(&data, 0, sizeof(data));
710 	data.ch_switch.freq = nla_get_u32(freq);
711 	data.ch_switch.ht_enabled = ht_enabled;
712 	data.ch_switch.ch_offset = chan_offset;
713 	if (bw)
714 		data.ch_switch.ch_width = convert2width(nla_get_u32(bw));
715 	if (cf1)
716 		data.ch_switch.cf1 = nla_get_u32(cf1);
717 	if (cf2)
718 		data.ch_switch.cf2 = nla_get_u32(cf2);
719 
720 	if (finished)
721 		bss->freq = data.ch_switch.freq;
722 	drv->assoc_freq = data.ch_switch.freq;
723 
724 	wpa_supplicant_event(bss->ctx, finished ?
725 			     EVENT_CH_SWITCH : EVENT_CH_SWITCH_STARTED, &data);
726 }
727 
728 
mlme_timeout_event(struct wpa_driver_nl80211_data * drv,enum nl80211_commands cmd,struct nlattr * addr)729 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
730 			       enum nl80211_commands cmd, struct nlattr *addr)
731 {
732 	union wpa_event_data event;
733 	enum wpa_event_type ev;
734 
735 	if (nla_len(addr) != ETH_ALEN)
736 		return;
737 
738 	wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
739 		   cmd, MAC2STR((u8 *) nla_data(addr)));
740 
741 	if (cmd == NL80211_CMD_AUTHENTICATE)
742 		ev = EVENT_AUTH_TIMED_OUT;
743 	else if (cmd == NL80211_CMD_ASSOCIATE)
744 		ev = EVENT_ASSOC_TIMED_OUT;
745 	else
746 		return;
747 
748 	os_memset(&event, 0, sizeof(event));
749 	os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
750 	wpa_supplicant_event(drv->ctx, ev, &event);
751 }
752 
753 
mlme_event_mgmt(struct i802_bss * bss,struct nlattr * freq,struct nlattr * sig,const u8 * frame,size_t len)754 static void mlme_event_mgmt(struct i802_bss *bss,
755 			    struct nlattr *freq, struct nlattr *sig,
756 			    const u8 *frame, size_t len)
757 {
758 	struct wpa_driver_nl80211_data *drv = bss->drv;
759 	const struct ieee80211_mgmt *mgmt;
760 	union wpa_event_data event;
761 	u16 fc, stype;
762 	int ssi_signal = 0;
763 	int rx_freq = 0;
764 
765 	wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
766 	mgmt = (const struct ieee80211_mgmt *) frame;
767 	if (len < 24) {
768 		wpa_printf(MSG_DEBUG, "nl80211: Too short management frame");
769 		return;
770 	}
771 
772 	fc = le_to_host16(mgmt->frame_control);
773 	stype = WLAN_FC_GET_STYPE(fc);
774 
775 	if (sig)
776 		ssi_signal = (s32) nla_get_u32(sig);
777 
778 	os_memset(&event, 0, sizeof(event));
779 	if (freq) {
780 		event.rx_mgmt.freq = nla_get_u32(freq);
781 		rx_freq = drv->last_mgmt_freq = event.rx_mgmt.freq;
782 	}
783 	wpa_printf(MSG_DEBUG,
784 		   "nl80211: RX frame da=" MACSTR " sa=" MACSTR " bssid=" MACSTR
785 		   " freq=%d ssi_signal=%d fc=0x%x seq_ctrl=0x%x stype=%u (%s) len=%u",
786 		   MAC2STR(mgmt->da), MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid),
787 		   rx_freq, ssi_signal, fc,
788 		   le_to_host16(mgmt->seq_ctrl), stype, fc2str(fc),
789 		   (unsigned int) len);
790 	event.rx_mgmt.frame = frame;
791 	event.rx_mgmt.frame_len = len;
792 	event.rx_mgmt.ssi_signal = ssi_signal;
793 	event.rx_mgmt.drv_priv = bss;
794 	wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
795 }
796 
797 
mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data * drv,struct nlattr * cookie,const u8 * frame,size_t len,struct nlattr * ack)798 static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
799 				      struct nlattr *cookie, const u8 *frame,
800 				      size_t len, struct nlattr *ack)
801 {
802 	union wpa_event_data event;
803 	const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) frame;
804 	u16 fc = le_to_host16(hdr->frame_control);
805 	u64 cookie_val = 0;
806 
807 	if (cookie)
808 		cookie_val = nla_get_u64(cookie);
809 	wpa_printf(MSG_DEBUG,
810 		   "nl80211: Frame TX status event A1=" MACSTR
811 		   " %sstype=%d cookie=0x%llx%s ack=%d",
812 		   MAC2STR(hdr->addr1),
813 		   WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ? "not-mgmt " : "",
814 		   WLAN_FC_GET_STYPE(fc), (long long unsigned int) cookie_val,
815 		   cookie ? "" : "(N/A)", ack != NULL);
816 
817 	if (cookie_val && cookie_val == drv->eapol_tx_cookie &&
818 	    len >= ETH_HLEN &&
819 	    WPA_GET_BE16(frame + 2 * ETH_ALEN) == ETH_P_PAE) {
820 		wpa_printf(MSG_DEBUG,
821 			   "nl80211: Work around misdelivered control port TX status for EAPOL");
822 		nl80211_control_port_frame_tx_status(drv, frame, len, ack,
823 						     cookie);
824 		return;
825 	}
826 
827 	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT)
828 		return;
829 
830 	if (!is_ap_interface(drv->nlmode) &&
831 	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
832 		if (!cookie)
833 			return;
834 
835 		wpa_printf(MSG_DEBUG,
836 			   "nl80211: Frame TX status: cookie=0x%llx%s (ack=%d)",
837 			   (long long unsigned int) cookie_val,
838 			   cookie_val == drv->send_frame_cookie ?
839 			   " (match)" : " (unknown)", ack != NULL);
840 		if (cookie_val != drv->send_frame_cookie)
841 			return;
842 	} else if (!is_ap_interface(drv->nlmode) &&
843 		   WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
844 		wpa_printf(MSG_DEBUG,
845 			   "nl80211: Authentication frame TX status: ack=%d",
846 			   !!ack);
847 	}
848 
849 	os_memset(&event, 0, sizeof(event));
850 	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
851 	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
852 	event.tx_status.dst = hdr->addr1;
853 	event.tx_status.data = frame;
854 	event.tx_status.data_len = len;
855 	event.tx_status.ack = ack != NULL;
856 	wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
857 }
858 
859 
mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data * drv,enum wpa_event_type type,const u8 * frame,size_t len)860 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
861 				       enum wpa_event_type type,
862 				       const u8 *frame, size_t len)
863 {
864 	const struct ieee80211_mgmt *mgmt;
865 	union wpa_event_data event;
866 	const u8 *bssid = NULL;
867 	u16 reason_code = 0;
868 
869 	if (type == EVENT_DEAUTH)
870 		wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
871 	else
872 		wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
873 
874 	mgmt = (const struct ieee80211_mgmt *) frame;
875 	if (len >= 24) {
876 		bssid = mgmt->bssid;
877 
878 		if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
879 		    !drv->associated &&
880 		    os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
881 		    os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
882 		    os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
883 			/*
884 			 * Avoid issues with some roaming cases where
885 			 * disconnection event for the old AP may show up after
886 			 * we have started connection with the new AP.
887 			 * In case of locally generated event clear
888 			 * ignore_next_local_deauth as well, to avoid next local
889 			 * deauth event be wrongly ignored.
890 			 */
891 			if (!os_memcmp(mgmt->sa, drv->first_bss->addr,
892 				       ETH_ALEN)) {
893 				wpa_printf(MSG_DEBUG,
894 					   "nl80211: Received a locally generated deauth event. Clear ignore_next_local_deauth flag");
895 				drv->ignore_next_local_deauth = 0;
896 			} else {
897 				wpa_printf(MSG_DEBUG,
898 					   "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
899 					   MAC2STR(bssid),
900 					   MAC2STR(drv->auth_attempt_bssid));
901 			}
902 			return;
903 		}
904 
905 		if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
906 		    drv->connect_reassoc && drv->associated &&
907 		    os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0 &&
908 		    os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0) {
909 			/*
910 			 * Avoid issues with some roaming cases where
911 			 * disconnection event for the old AP may show up after
912 			 * we have started connection with the new AP.
913 			 */
914 			wpa_printf(MSG_DEBUG,
915 				   "nl80211: Ignore deauth/disassoc event from old AP "
916 				   MACSTR
917 				   " when already connecting with " MACSTR,
918 				   MAC2STR(bssid),
919 				   MAC2STR(drv->auth_attempt_bssid));
920 			return;
921 		}
922 
923 		if (drv->associated != 0 &&
924 		    os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
925 		    os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
926 			/*
927 			 * We have presumably received this deauth as a
928 			 * response to a clear_state_mismatch() outgoing
929 			 * deauth.  Don't let it take us offline!
930 			 */
931 			wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
932 				   "from Unknown BSSID " MACSTR " -- ignoring",
933 				   MAC2STR(bssid));
934 			return;
935 		}
936 	}
937 
938 	nl80211_mark_disconnected(drv);
939 	os_memset(&event, 0, sizeof(event));
940 
941 	/* Note: Same offset for Reason Code in both frame subtypes */
942 	if (len >= 24 + sizeof(mgmt->u.deauth))
943 		reason_code = le_to_host16(mgmt->u.deauth.reason_code);
944 
945 	if (type == EVENT_DISASSOC) {
946 		event.disassoc_info.locally_generated =
947 			!os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
948 		event.disassoc_info.addr = bssid;
949 		event.disassoc_info.reason_code = reason_code;
950 		if (frame + len > mgmt->u.disassoc.variable) {
951 			event.disassoc_info.ie = mgmt->u.disassoc.variable;
952 			event.disassoc_info.ie_len = frame + len -
953 				mgmt->u.disassoc.variable;
954 		}
955 	} else {
956 		event.deauth_info.locally_generated =
957 			!os_memcmp(mgmt->sa, drv->first_bss->addr, ETH_ALEN);
958 		if (drv->ignore_deauth_event) {
959 			wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event due to previous forced deauth-during-auth");
960 			drv->ignore_deauth_event = 0;
961 			if (event.deauth_info.locally_generated)
962 				drv->ignore_next_local_deauth = 0;
963 			return;
964 		}
965 		if (drv->ignore_next_local_deauth) {
966 			drv->ignore_next_local_deauth = 0;
967 			if (event.deauth_info.locally_generated) {
968 				wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth event triggered due to own deauth request");
969 				return;
970 			}
971 			wpa_printf(MSG_WARNING, "nl80211: Was expecting local deauth but got another disconnect event first");
972 		}
973 		event.deauth_info.addr = bssid;
974 		event.deauth_info.reason_code = reason_code;
975 		if (frame + len > mgmt->u.deauth.variable) {
976 			event.deauth_info.ie = mgmt->u.deauth.variable;
977 			event.deauth_info.ie_len = frame + len -
978 				mgmt->u.deauth.variable;
979 		}
980 	}
981 
982 	wpa_supplicant_event(drv->ctx, type, &event);
983 }
984 
985 
mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data * drv,enum wpa_event_type type,const u8 * frame,size_t len)986 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
987 					 enum wpa_event_type type,
988 					 const u8 *frame, size_t len)
989 {
990 	const struct ieee80211_mgmt *mgmt;
991 	union wpa_event_data event;
992 	u16 reason_code = 0;
993 
994 	if (type == EVENT_UNPROT_DEAUTH)
995 		wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
996 	else
997 		wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
998 
999 	if (len < 24)
1000 		return;
1001 
1002 	mgmt = (const struct ieee80211_mgmt *) frame;
1003 
1004 	os_memset(&event, 0, sizeof(event));
1005 	/* Note: Same offset for Reason Code in both frame subtypes */
1006 	if (len >= 24 + sizeof(mgmt->u.deauth))
1007 		reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1008 
1009 	if (type == EVENT_UNPROT_DISASSOC) {
1010 		event.unprot_disassoc.sa = mgmt->sa;
1011 		event.unprot_disassoc.da = mgmt->da;
1012 		event.unprot_disassoc.reason_code = reason_code;
1013 	} else {
1014 		event.unprot_deauth.sa = mgmt->sa;
1015 		event.unprot_deauth.da = mgmt->da;
1016 		event.unprot_deauth.reason_code = reason_code;
1017 	}
1018 
1019 	wpa_supplicant_event(drv->ctx, type, &event);
1020 }
1021 
1022 
mlme_event_unprot_beacon(struct wpa_driver_nl80211_data * drv,const u8 * frame,size_t len)1023 static void mlme_event_unprot_beacon(struct wpa_driver_nl80211_data *drv,
1024 				     const u8 *frame, size_t len)
1025 {
1026 	const struct ieee80211_mgmt *mgmt;
1027 	union wpa_event_data event;
1028 
1029 	if (len < 24)
1030 		return;
1031 
1032 	mgmt = (const struct ieee80211_mgmt *) frame;
1033 
1034 	os_memset(&event, 0, sizeof(event));
1035 	event.unprot_beacon.sa = mgmt->sa;
1036 	wpa_supplicant_event(drv->ctx, EVENT_UNPROT_BEACON, &event);
1037 }
1038 
1039 
mlme_event(struct i802_bss * bss,enum nl80211_commands cmd,struct nlattr * frame,struct nlattr * addr,struct nlattr * timed_out,struct nlattr * freq,struct nlattr * ack,struct nlattr * cookie,struct nlattr * sig,struct nlattr * wmm,struct nlattr * req_ie)1040 static void mlme_event(struct i802_bss *bss,
1041 		       enum nl80211_commands cmd, struct nlattr *frame,
1042 		       struct nlattr *addr, struct nlattr *timed_out,
1043 		       struct nlattr *freq, struct nlattr *ack,
1044 		       struct nlattr *cookie, struct nlattr *sig,
1045 		       struct nlattr *wmm, struct nlattr *req_ie)
1046 {
1047 	struct wpa_driver_nl80211_data *drv = bss->drv;
1048 	const u8 *data;
1049 	size_t len;
1050 
1051 	if (timed_out && addr) {
1052 		mlme_timeout_event(drv, cmd, addr);
1053 		return;
1054 	}
1055 
1056 	if (frame == NULL) {
1057 		wpa_printf(MSG_DEBUG,
1058 			   "nl80211: MLME event %d (%s) without frame data",
1059 			   cmd, nl80211_command_to_string(cmd));
1060 		return;
1061 	}
1062 
1063 	data = nla_data(frame);
1064 	len = nla_len(frame);
1065 	if (len < 4 + 2 * ETH_ALEN) {
1066 		wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
1067 			   MACSTR ") - too short",
1068 			   cmd, nl80211_command_to_string(cmd), bss->ifname,
1069 			   MAC2STR(bss->addr));
1070 		return;
1071 	}
1072 	wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
1073 		   ") A1=" MACSTR " A2=" MACSTR, cmd,
1074 		   nl80211_command_to_string(cmd), bss->ifname,
1075 		   MAC2STR(bss->addr), MAC2STR(data + 4),
1076 		   MAC2STR(data + 4 + ETH_ALEN));
1077 	if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
1078 	    os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
1079 	    (is_zero_ether_addr(bss->rand_addr) ||
1080 	     os_memcmp(bss->rand_addr, data + 4, ETH_ALEN) != 0) &&
1081 	    os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
1082 		wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
1083 			   "for foreign address", bss->ifname);
1084 		return;
1085 	}
1086 	wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1087 		    nla_data(frame), nla_len(frame));
1088 
1089 	switch (cmd) {
1090 	case NL80211_CMD_AUTHENTICATE:
1091 		mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1092 		break;
1093 	case NL80211_CMD_ASSOCIATE:
1094 		mlme_event_assoc(drv, nla_data(frame), nla_len(frame), wmm,
1095 				 req_ie);
1096 		break;
1097 	case NL80211_CMD_DEAUTHENTICATE:
1098 		mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1099 					   nla_data(frame), nla_len(frame));
1100 		break;
1101 	case NL80211_CMD_DISASSOCIATE:
1102 		mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1103 					   nla_data(frame), nla_len(frame));
1104 		break;
1105 	case NL80211_CMD_FRAME:
1106 		mlme_event_mgmt(bss, freq, sig, nla_data(frame),
1107 				nla_len(frame));
1108 		break;
1109 	case NL80211_CMD_FRAME_TX_STATUS:
1110 		mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1111 					  nla_len(frame), ack);
1112 		break;
1113 	case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1114 		mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1115 					     nla_data(frame), nla_len(frame));
1116 		break;
1117 	case NL80211_CMD_UNPROT_DISASSOCIATE:
1118 		mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1119 					     nla_data(frame), nla_len(frame));
1120 		break;
1121 	case NL80211_CMD_UNPROT_BEACON:
1122 		mlme_event_unprot_beacon(drv, nla_data(frame), nla_len(frame));
1123 		break;
1124 	default:
1125 		break;
1126 	}
1127 }
1128 
1129 
mlme_event_michael_mic_failure(struct i802_bss * bss,struct nlattr * tb[])1130 static void mlme_event_michael_mic_failure(struct i802_bss *bss,
1131 					   struct nlattr *tb[])
1132 {
1133 	union wpa_event_data data;
1134 
1135 	wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1136 	os_memset(&data, 0, sizeof(data));
1137 	if (tb[NL80211_ATTR_MAC]) {
1138 		wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1139 			    nla_data(tb[NL80211_ATTR_MAC]),
1140 			    nla_len(tb[NL80211_ATTR_MAC]));
1141 		data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1142 	}
1143 	if (tb[NL80211_ATTR_KEY_SEQ]) {
1144 		wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1145 			    nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1146 			    nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1147 	}
1148 	if (tb[NL80211_ATTR_KEY_TYPE]) {
1149 		enum nl80211_key_type key_type =
1150 			nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1151 		wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1152 		if (key_type == NL80211_KEYTYPE_PAIRWISE)
1153 			data.michael_mic_failure.unicast = 1;
1154 	} else
1155 		data.michael_mic_failure.unicast = 1;
1156 
1157 	if (tb[NL80211_ATTR_KEY_IDX]) {
1158 		u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1159 		wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1160 	}
1161 
1162 	wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1163 }
1164 
1165 
mlme_event_join_ibss(struct wpa_driver_nl80211_data * drv,struct nlattr * tb[])1166 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1167 				 struct nlattr *tb[])
1168 {
1169 	unsigned int freq;
1170 	union wpa_event_data event;
1171 
1172 	if (tb[NL80211_ATTR_MAC] == NULL) {
1173 		wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1174 			   "event");
1175 		return;
1176 	}
1177 	os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1178 
1179 	drv->associated = 1;
1180 	wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1181 		   MAC2STR(drv->bssid));
1182 
1183 	freq = nl80211_get_assoc_freq(drv);
1184 	if (freq) {
1185 		wpa_printf(MSG_DEBUG, "nl80211: IBSS on frequency %u MHz",
1186 			   freq);
1187 		drv->first_bss->freq = freq;
1188 	}
1189 
1190 	os_memset(&event, 0, sizeof(event));
1191 	event.assoc_info.freq = freq;
1192 
1193 	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1194 }
1195 
1196 
mlme_event_remain_on_channel(struct wpa_driver_nl80211_data * drv,int cancel_event,struct nlattr * tb[])1197 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1198 					 int cancel_event, struct nlattr *tb[])
1199 {
1200 	unsigned int freq, chan_type, duration;
1201 	union wpa_event_data data;
1202 	u64 cookie;
1203 
1204 	if (tb[NL80211_ATTR_WIPHY_FREQ])
1205 		freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1206 	else
1207 		freq = 0;
1208 
1209 	if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1210 		chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1211 	else
1212 		chan_type = 0;
1213 
1214 	if (tb[NL80211_ATTR_DURATION])
1215 		duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1216 	else
1217 		duration = 0;
1218 
1219 	if (tb[NL80211_ATTR_COOKIE])
1220 		cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1221 	else
1222 		cookie = 0;
1223 
1224 	wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1225 		   "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1226 		   cancel_event, freq, chan_type, duration,
1227 		   (long long unsigned int) cookie,
1228 		   cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1229 
1230 	if (cookie != drv->remain_on_chan_cookie)
1231 		return; /* not for us */
1232 
1233 	if (cancel_event)
1234 		drv->pending_remain_on_chan = 0;
1235 
1236 	os_memset(&data, 0, sizeof(data));
1237 	data.remain_on_channel.freq = freq;
1238 	data.remain_on_channel.duration = duration;
1239 	wpa_supplicant_event(drv->ctx, cancel_event ?
1240 			     EVENT_CANCEL_REMAIN_ON_CHANNEL :
1241 			     EVENT_REMAIN_ON_CHANNEL, &data);
1242 }
1243 
1244 
mlme_event_ft_event(struct wpa_driver_nl80211_data * drv,struct nlattr * tb[])1245 static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
1246 				struct nlattr *tb[])
1247 {
1248 	union wpa_event_data data;
1249 
1250 	os_memset(&data, 0, sizeof(data));
1251 
1252 	if (tb[NL80211_ATTR_IE]) {
1253 		data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
1254 		data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
1255 	}
1256 
1257 	if (tb[NL80211_ATTR_IE_RIC]) {
1258 		data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
1259 		data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
1260 	}
1261 
1262 	if (tb[NL80211_ATTR_MAC])
1263 		os_memcpy(data.ft_ies.target_ap,
1264 			  nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1265 
1266 	wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
1267 		   MAC2STR(data.ft_ies.target_ap));
1268 
1269 	wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
1270 }
1271 
1272 
mlme_event_dh_event(struct wpa_driver_nl80211_data * drv,struct i802_bss * bss,struct nlattr * tb[])1273 static void mlme_event_dh_event(struct wpa_driver_nl80211_data *drv,
1274 				struct i802_bss *bss,
1275 				struct nlattr *tb[])
1276 {
1277 	union wpa_event_data data;
1278 
1279 	if (!is_ap_interface(drv->nlmode))
1280 		return;
1281 	if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_IE])
1282 		return;
1283 
1284 	os_memset(&data, 0, sizeof(data));
1285 	data.update_dh.peer = nla_data(tb[NL80211_ATTR_MAC]);
1286 	data.update_dh.ie = nla_data(tb[NL80211_ATTR_IE]);
1287 	data.update_dh.ie_len = nla_len(tb[NL80211_ATTR_IE]);
1288 
1289 	wpa_printf(MSG_DEBUG, "nl80211: DH event - peer " MACSTR,
1290 		   MAC2STR(data.update_dh.peer));
1291 
1292 	wpa_supplicant_event(bss->ctx, EVENT_UPDATE_DH, &data);
1293 }
1294 
1295 
send_scan_event(struct wpa_driver_nl80211_data * drv,int aborted,struct nlattr * tb[],int external_scan)1296 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1297 			    struct nlattr *tb[], int external_scan)
1298 {
1299 	union wpa_event_data event;
1300 	struct nlattr *nl;
1301 	int rem;
1302 	struct scan_info *info;
1303 #define MAX_REPORT_FREQS 50
1304 	int freqs[MAX_REPORT_FREQS];
1305 	int num_freqs = 0;
1306 
1307 	if (!external_scan && drv->scan_for_auth) {
1308 		drv->scan_for_auth = 0;
1309 		wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1310 			   "cfg80211 BSS entry");
1311 		wpa_driver_nl80211_authenticate_retry(drv);
1312 		return;
1313 	}
1314 
1315 	os_memset(&event, 0, sizeof(event));
1316 	info = &event.scan_info;
1317 	info->aborted = aborted;
1318 	info->external_scan = external_scan;
1319 	info->nl_scan_event = 1;
1320 
1321 	if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1322 		nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1323 			struct wpa_driver_scan_ssid *s =
1324 				&info->ssids[info->num_ssids];
1325 			s->ssid = nla_data(nl);
1326 			s->ssid_len = nla_len(nl);
1327 			wpa_printf(MSG_DEBUG, "nl80211: Scan probed for SSID '%s'",
1328 				   wpa_ssid_txt(s->ssid, s->ssid_len));
1329 			info->num_ssids++;
1330 			if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1331 				break;
1332 		}
1333 	}
1334 	if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1335 		char msg[300], *pos, *end;
1336 		int res;
1337 
1338 		pos = msg;
1339 		end = pos + sizeof(msg);
1340 		*pos = '\0';
1341 
1342 		nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1343 		{
1344 			freqs[num_freqs] = nla_get_u32(nl);
1345 			res = os_snprintf(pos, end - pos, " %d",
1346 					  freqs[num_freqs]);
1347 			if (!os_snprintf_error(end - pos, res))
1348 				pos += res;
1349 			num_freqs++;
1350 			if (num_freqs == MAX_REPORT_FREQS - 1)
1351 				break;
1352 		}
1353 		info->freqs = freqs;
1354 		info->num_freqs = num_freqs;
1355 		wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
1356 			   msg);
1357 	}
1358 
1359 	if (tb[NL80211_ATTR_SCAN_START_TIME_TSF] &&
1360 	    tb[NL80211_ATTR_SCAN_START_TIME_TSF_BSSID]) {
1361 		info->scan_start_tsf =
1362 			nla_get_u64(tb[NL80211_ATTR_SCAN_START_TIME_TSF]);
1363 		os_memcpy(info->scan_start_tsf_bssid,
1364 			  nla_data(tb[NL80211_ATTR_SCAN_START_TIME_TSF_BSSID]),
1365 			  ETH_ALEN);
1366 	}
1367 
1368 	wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1369 }
1370 
1371 
nl80211_cqm_event(struct wpa_driver_nl80211_data * drv,struct nlattr * tb[])1372 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1373 			      struct nlattr *tb[])
1374 {
1375 	static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1376 		[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1377 		[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1378 		[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1379 		[NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1380 		[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
1381 		[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
1382 		[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
1383 		[NL80211_ATTR_CQM_BEACON_LOSS_EVENT] = { .type = NLA_FLAG },
1384 	};
1385 	struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1386 	enum nl80211_cqm_rssi_threshold_event event;
1387 	union wpa_event_data ed;
1388 	int res;
1389 
1390 	if (tb[NL80211_ATTR_CQM] == NULL ||
1391 	    nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1392 			     cqm_policy)) {
1393 		wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1394 		return;
1395 	}
1396 
1397 	os_memset(&ed, 0, sizeof(ed));
1398 
1399 	if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1400 		if (!tb[NL80211_ATTR_MAC])
1401 			return;
1402 		os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1403 			  ETH_ALEN);
1404 		ed.low_ack.num_packets =
1405 			nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
1406 		wpa_printf(MSG_DEBUG, "nl80211: Packet loss event for " MACSTR
1407 			   " (num_packets %u)",
1408 			   MAC2STR(ed.low_ack.addr), ed.low_ack.num_packets);
1409 		wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1410 		return;
1411 	}
1412 
1413 	if (cqm[NL80211_ATTR_CQM_BEACON_LOSS_EVENT]) {
1414 		wpa_printf(MSG_DEBUG, "nl80211: Beacon loss event");
1415 		wpa_supplicant_event(drv->ctx, EVENT_BEACON_LOSS, NULL);
1416 		return;
1417 	}
1418 
1419 	if (cqm[NL80211_ATTR_CQM_TXE_RATE] &&
1420 	    cqm[NL80211_ATTR_CQM_TXE_PKTS] &&
1421 	    cqm[NL80211_ATTR_CQM_TXE_INTVL] &&
1422 	    cqm[NL80211_ATTR_MAC]) {
1423 		wpa_printf(MSG_DEBUG, "nl80211: CQM TXE event for " MACSTR
1424 			   " (rate: %u pkts: %u interval: %u)",
1425 			   MAC2STR((u8 *) nla_data(cqm[NL80211_ATTR_MAC])),
1426 			   nla_get_u32(cqm[NL80211_ATTR_CQM_TXE_RATE]),
1427 			   nla_get_u32(cqm[NL80211_ATTR_CQM_TXE_PKTS]),
1428 			   nla_get_u32(cqm[NL80211_ATTR_CQM_TXE_INTVL]));
1429 		return;
1430 	}
1431 
1432 	if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL) {
1433 		wpa_printf(MSG_DEBUG,
1434 			   "nl80211: Not a CQM RSSI threshold event");
1435 		return;
1436 	}
1437 	event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1438 
1439 	if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1440 		wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1441 			   "event: RSSI high");
1442 		ed.signal_change.above_threshold = 1;
1443 	} else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1444 		wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1445 			   "event: RSSI low");
1446 		ed.signal_change.above_threshold = 0;
1447 	} else {
1448 		wpa_printf(MSG_DEBUG,
1449 			   "nl80211: Unknown CQM RSSI threshold event: %d",
1450 			   event);
1451 		return;
1452 	}
1453 
1454 	/*
1455 	 * nl80211_get_link_signal() and nl80211_get_link_noise() set default
1456 	 * values in case querying the driver fails.
1457 	 */
1458 	res = nl80211_get_link_signal(drv, &ed.signal_change);
1459 	if (res == 0) {
1460 		wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
1461 			   ed.signal_change.current_signal,
1462 			   ed.signal_change.current_txrate);
1463 	} else {
1464 		wpa_printf(MSG_DEBUG,
1465 			   "nl80211: Querying the driver for signal info failed");
1466 	}
1467 
1468 	res = nl80211_get_link_noise(drv, &ed.signal_change);
1469 	if (res == 0) {
1470 		wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1471 			   ed.signal_change.current_noise);
1472 	} else {
1473 		wpa_printf(MSG_DEBUG,
1474 			   "nl80211: Querying the driver for noise info failed");
1475 	}
1476 
1477 	wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1478 }
1479 
1480 
nl80211_new_peer_candidate(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1481 static void nl80211_new_peer_candidate(struct wpa_driver_nl80211_data *drv,
1482 				       struct nlattr **tb)
1483 {
1484 	const u8 *addr;
1485 	union wpa_event_data data;
1486 
1487 	if (drv->nlmode != NL80211_IFTYPE_MESH_POINT ||
1488 	    !tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_IE])
1489 		return;
1490 
1491 	addr = nla_data(tb[NL80211_ATTR_MAC]);
1492 	wpa_printf(MSG_DEBUG, "nl80211: New peer candidate " MACSTR,
1493 		   MAC2STR(addr));
1494 
1495 	os_memset(&data, 0, sizeof(data));
1496 	data.mesh_peer.peer = addr;
1497 	data.mesh_peer.ies = nla_data(tb[NL80211_ATTR_IE]);
1498 	data.mesh_peer.ie_len = nla_len(tb[NL80211_ATTR_IE]);
1499 	wpa_supplicant_event(drv->ctx, EVENT_NEW_PEER_CANDIDATE, &data);
1500 }
1501 
1502 
nl80211_new_station_event(struct wpa_driver_nl80211_data * drv,struct i802_bss * bss,struct nlattr ** tb)1503 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1504 				      struct i802_bss *bss,
1505 				      struct nlattr **tb)
1506 {
1507 	u8 *addr;
1508 	union wpa_event_data data;
1509 
1510 	if (tb[NL80211_ATTR_MAC] == NULL)
1511 		return;
1512 	addr = nla_data(tb[NL80211_ATTR_MAC]);
1513 	wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
1514 
1515 	if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
1516 		u8 *ies = NULL;
1517 		size_t ies_len = 0;
1518 		if (tb[NL80211_ATTR_IE]) {
1519 			ies = nla_data(tb[NL80211_ATTR_IE]);
1520 			ies_len = nla_len(tb[NL80211_ATTR_IE]);
1521 		}
1522 		wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1523 		drv_event_assoc(bss->ctx, addr, ies, ies_len, 0);
1524 		return;
1525 	}
1526 
1527 	if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1528 		return;
1529 
1530 	os_memset(&data, 0, sizeof(data));
1531 	os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1532 	wpa_supplicant_event(bss->ctx, EVENT_IBSS_RSN_START, &data);
1533 }
1534 
1535 
nl80211_del_station_event(struct wpa_driver_nl80211_data * drv,struct i802_bss * bss,struct nlattr ** tb)1536 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1537 				      struct i802_bss *bss,
1538 				      struct nlattr **tb)
1539 {
1540 	u8 *addr;
1541 	union wpa_event_data data;
1542 
1543 	if (tb[NL80211_ATTR_MAC] == NULL)
1544 		return;
1545 	addr = nla_data(tb[NL80211_ATTR_MAC]);
1546 	wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1547 		   MAC2STR(addr));
1548 
1549 	if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
1550 		drv_event_disassoc(bss->ctx, addr);
1551 		return;
1552 	}
1553 
1554 	if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1555 		return;
1556 
1557 	os_memset(&data, 0, sizeof(data));
1558 	os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1559 	wpa_supplicant_event(bss->ctx, EVENT_IBSS_PEER_LOST, &data);
1560 }
1561 
1562 
nl80211_rekey_offload_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1563 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
1564 					struct nlattr **tb)
1565 {
1566 	struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
1567 	static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
1568 		[NL80211_REKEY_DATA_KEK] = {
1569 			.minlen = NL80211_KEK_LEN,
1570 			.maxlen = NL80211_KEK_LEN,
1571 		},
1572 		[NL80211_REKEY_DATA_KCK] = {
1573 			.minlen = NL80211_KCK_LEN,
1574 			.maxlen = NL80211_KCK_LEN,
1575 		},
1576 		[NL80211_REKEY_DATA_REPLAY_CTR] = {
1577 			.minlen = NL80211_REPLAY_CTR_LEN,
1578 			.maxlen = NL80211_REPLAY_CTR_LEN,
1579 		},
1580 	};
1581 	union wpa_event_data data;
1582 
1583 	if (!tb[NL80211_ATTR_MAC] ||
1584 	    !tb[NL80211_ATTR_REKEY_DATA] ||
1585 	    nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
1586 			     tb[NL80211_ATTR_REKEY_DATA], rekey_policy) ||
1587 	    !rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
1588 		return;
1589 
1590 	os_memset(&data, 0, sizeof(data));
1591 	data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
1592 	wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
1593 		   MAC2STR(data.driver_gtk_rekey.bssid));
1594 	data.driver_gtk_rekey.replay_ctr =
1595 		nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
1596 	wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
1597 		    data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
1598 	wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
1599 }
1600 
1601 
nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1602 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
1603 					  struct nlattr **tb)
1604 {
1605 	struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
1606 	static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
1607 		[NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
1608 		[NL80211_PMKSA_CANDIDATE_BSSID] = {
1609 			.minlen = ETH_ALEN,
1610 			.maxlen = ETH_ALEN,
1611 		},
1612 		[NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
1613 	};
1614 	union wpa_event_data data;
1615 
1616 	wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
1617 
1618 	if (!tb[NL80211_ATTR_PMKSA_CANDIDATE] ||
1619 	    nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
1620 			     tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy) ||
1621 	    !cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
1622 	    !cand[NL80211_PMKSA_CANDIDATE_BSSID])
1623 		return;
1624 
1625 	os_memset(&data, 0, sizeof(data));
1626 	os_memcpy(data.pmkid_candidate.bssid,
1627 		  nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
1628 	data.pmkid_candidate.index =
1629 		nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
1630 	data.pmkid_candidate.preauth =
1631 		cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
1632 	wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
1633 }
1634 
1635 
nl80211_client_probe_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1636 static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
1637 				       struct nlattr **tb)
1638 {
1639 	union wpa_event_data data;
1640 	const u8 *addr;
1641 	u64 cookie = 0;
1642 
1643 	addr = nla_data(tb[NL80211_ATTR_MAC]);
1644 	if (!addr)
1645 		return;
1646 	if (tb[NL80211_ATTR_COOKIE])
1647 		cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1648 	wpa_printf(MSG_DEBUG, "nl80211: Probe client event (addr=" MACSTR
1649 		   " ack=%d cookie=%llu)", MAC2STR(addr),
1650 		   tb[NL80211_ATTR_ACK] != NULL,
1651 		   (long long unsigned int) cookie);
1652 	if (!tb[NL80211_ATTR_ACK])
1653 		return;
1654 
1655 	os_memset(&data, 0, sizeof(data));
1656 	os_memcpy(data.client_poll.addr, addr, ETH_ALEN);
1657 	wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
1658 }
1659 
1660 
nl80211_tdls_oper_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1661 static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
1662 				    struct nlattr **tb)
1663 {
1664 	union wpa_event_data data;
1665 
1666 	wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
1667 
1668 	if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
1669 		return;
1670 
1671 	os_memset(&data, 0, sizeof(data));
1672 	os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1673 	switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
1674 	case NL80211_TDLS_SETUP:
1675 		wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
1676 			   MACSTR, MAC2STR(data.tdls.peer));
1677 		data.tdls.oper = TDLS_REQUEST_SETUP;
1678 		break;
1679 	case NL80211_TDLS_TEARDOWN:
1680 		wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
1681 			   MACSTR, MAC2STR(data.tdls.peer));
1682 		data.tdls.oper = TDLS_REQUEST_TEARDOWN;
1683 		break;
1684 	case NL80211_TDLS_DISCOVERY_REQ:
1685 		wpa_printf(MSG_DEBUG,
1686 			   "nl80211: TDLS discovery request for peer " MACSTR,
1687 			   MAC2STR(data.tdls.peer));
1688 		data.tdls.oper = TDLS_REQUEST_DISCOVER;
1689 		break;
1690 	default:
1691 		wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
1692 			   "event");
1693 		return;
1694 	}
1695 	if (tb[NL80211_ATTR_REASON_CODE]) {
1696 		data.tdls.reason_code =
1697 			nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
1698 	}
1699 
1700 	wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
1701 }
1702 
1703 
nl80211_stop_ap(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1704 static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
1705 			    struct nlattr **tb)
1706 {
1707 	wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
1708 }
1709 
1710 
nl80211_connect_failed_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1711 static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
1712 					 struct nlattr **tb)
1713 {
1714 	union wpa_event_data data;
1715 	u32 reason;
1716 
1717 	wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
1718 
1719 	if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
1720 		return;
1721 
1722 	os_memset(&data, 0, sizeof(data));
1723 	os_memcpy(data.connect_failed_reason.addr,
1724 		  nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1725 
1726 	reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
1727 	switch (reason) {
1728 	case NL80211_CONN_FAIL_MAX_CLIENTS:
1729 		wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
1730 		data.connect_failed_reason.code = MAX_CLIENT_REACHED;
1731 		break;
1732 	case NL80211_CONN_FAIL_BLOCKED_CLIENT:
1733 		wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
1734 			   " tried to connect",
1735 			   MAC2STR(data.connect_failed_reason.addr));
1736 		data.connect_failed_reason.code = BLOCKED_CLIENT;
1737 		break;
1738 	default:
1739 		wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
1740 			   "%u", reason);
1741 		return;
1742 	}
1743 
1744 	wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
1745 }
1746 
1747 
nl80211_radar_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)1748 static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
1749 				struct nlattr **tb)
1750 {
1751 	union wpa_event_data data;
1752 	enum nl80211_radar_event event_type;
1753 
1754 	if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
1755 		return;
1756 
1757 	os_memset(&data, 0, sizeof(data));
1758 	data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1759 	event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
1760 
1761 	/* Check HT params */
1762 	if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1763 		data.dfs_event.ht_enabled = 1;
1764 		data.dfs_event.chan_offset = 0;
1765 
1766 		switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
1767 		case NL80211_CHAN_NO_HT:
1768 			data.dfs_event.ht_enabled = 0;
1769 			break;
1770 		case NL80211_CHAN_HT20:
1771 			break;
1772 		case NL80211_CHAN_HT40PLUS:
1773 			data.dfs_event.chan_offset = 1;
1774 			break;
1775 		case NL80211_CHAN_HT40MINUS:
1776 			data.dfs_event.chan_offset = -1;
1777 			break;
1778 		}
1779 	}
1780 
1781 	/* Get VHT params */
1782 	if (tb[NL80211_ATTR_CHANNEL_WIDTH])
1783 		data.dfs_event.chan_width =
1784 			convert2width(nla_get_u32(
1785 					      tb[NL80211_ATTR_CHANNEL_WIDTH]));
1786 	if (tb[NL80211_ATTR_CENTER_FREQ1])
1787 		data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
1788 	if (tb[NL80211_ATTR_CENTER_FREQ2])
1789 		data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
1790 
1791 	wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
1792 		   data.dfs_event.freq, data.dfs_event.ht_enabled,
1793 		   data.dfs_event.chan_offset, data.dfs_event.chan_width,
1794 		   data.dfs_event.cf1, data.dfs_event.cf2);
1795 
1796 	switch (event_type) {
1797 	case NL80211_RADAR_DETECTED:
1798 		wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
1799 		break;
1800 	case NL80211_RADAR_CAC_FINISHED:
1801 		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
1802 		break;
1803 	case NL80211_RADAR_CAC_ABORTED:
1804 		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
1805 		break;
1806 	case NL80211_RADAR_NOP_FINISHED:
1807 		wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
1808 		break;
1809 	case NL80211_RADAR_PRE_CAC_EXPIRED:
1810 		wpa_supplicant_event(drv->ctx, EVENT_DFS_PRE_CAC_EXPIRED,
1811 				     &data);
1812 		break;
1813 	case NL80211_RADAR_CAC_STARTED:
1814 		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_STARTED, &data);
1815 		break;
1816 	default:
1817 		wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
1818 			   "received", event_type);
1819 		break;
1820 	}
1821 }
1822 
1823 
nl80211_spurious_frame(struct i802_bss * bss,struct nlattr ** tb,int wds)1824 static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
1825 				   int wds)
1826 {
1827 	struct wpa_driver_nl80211_data *drv = bss->drv;
1828 	union wpa_event_data event;
1829 
1830 	if (!tb[NL80211_ATTR_MAC])
1831 		return;
1832 
1833 	os_memset(&event, 0, sizeof(event));
1834 	event.rx_from_unknown.bssid = bss->addr;
1835 	event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
1836 	event.rx_from_unknown.wds = wds;
1837 
1838 	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
1839 }
1840 
1841 #ifdef CONFIG_DRIVER_NL80211_BRCM
brcm_nl80211_acs_select_ch(struct wpa_driver_nl80211_data * drv,const u8 * data,size_t len)1842 static void brcm_nl80211_acs_select_ch(struct wpa_driver_nl80211_data
1843 	*drv, const u8 *data, size_t len)
1844 {
1845 	struct nlattr *tb[BRCM_VENDOR_ATTR_ACS_LAST + 1];
1846 	union wpa_event_data event;
1847 
1848 	wpa_printf(MSG_DEBUG, "nl80211: vendor ACS channel selection vendor even received");
1849 
1850 	if (nla_parse(tb, BRCM_VENDOR_ATTR_ACS_LAST, (struct nlattr*) data, len, NULL)
1851 		|| (!tb[BRCM_VENDOR_ATTR_ACS_PRIMARY_FREQ])
1852 			|| (!tb[BRCM_VENDOR_ATTR_ACS_SECONDARY_FREQ])) {
1853 			return;
1854 		}
1855 
1856 	os_memset(&event, 0, sizeof(event));
1857 	if (tb[BRCM_VENDOR_ATTR_ACS_PRIMARY_FREQ]) {
1858 		event.acs_selected_channels.pri_freq =
1859 				nla_get_u32(tb[BRCM_VENDOR_ATTR_ACS_PRIMARY_FREQ]);
1860 	}
1861 
1862 	wpa_printf(MSG_MSGDUMP, "got pri_freq=%d\n",
1863 		event.acs_selected_channels.pri_freq );
1864 
1865 	if (tb[BRCM_VENDOR_ATTR_ACS_SECONDARY_FREQ]) {
1866 		event.acs_selected_channels.sec_freq =
1867 			nla_get_u32(tb[BRCM_VENDOR_ATTR_ACS_SECONDARY_FREQ]);
1868 	}
1869 
1870 	wpa_printf(MSG_MSGDUMP, "got sec_freq=%d\n",
1871 		event.acs_selected_channels.sec_freq );
1872 	if (tb[BRCM_VENDOR_ATTR_ACS_VHT_SEG0_CENTER_CHANNEL]) {
1873 		event.acs_selected_channels.vht_seg0_center_ch =
1874 			nla_get_u8(tb[BRCM_VENDOR_ATTR_ACS_VHT_SEG0_CENTER_CHANNEL]);
1875 	}
1876 
1877 	if (tb[BRCM_VENDOR_ATTR_ACS_VHT_SEG0_CENTER_CHANNEL]) {
1878 		event.acs_selected_channels.vht_seg1_center_ch =
1879 			nla_get_u8(tb[BRCM_VENDOR_ATTR_ACS_VHT_SEG1_CENTER_CHANNEL]);
1880 	}
1881 	if (tb[BRCM_VENDOR_ATTR_ACS_CHWIDTH]) {
1882 		event.acs_selected_channels.ch_width =
1883 			nla_get_u16(tb[BRCM_VENDOR_ATTR_ACS_CHWIDTH]);
1884 	}
1885 	if (tb[BRCM_VENDOR_ATTR_ACS_HW_MODE]) {
1886 		event.acs_selected_channels.hw_mode =
1887 		nla_get_u8(tb[BRCM_VENDOR_ATTR_ACS_HW_MODE]);
1888 		if ((event.acs_selected_channels.hw_mode == NUM_HOSTAPD_MODES)
1889 			|| (event.acs_selected_channels.hw_mode
1890 				== HOSTAPD_MODE_IEEE80211ANY)) {
1891 			wpa_printf(MSG_ERROR, "nl80211: Invalid hw_mode %d in ACS selection event",
1892 				event.acs_selected_channels.hw_mode);
1893 			return;
1894 		}
1895 	}
1896 
1897 	wpa_printf(MSG_INFO, "nl80211: ACS Results: PCH: %d SCH: %d BW: %d"
1898 		" VHT0: %d VHT1: %d HW_MODE: %d",
1899 		event.acs_selected_channels.pri_freq,
1900 		event.acs_selected_channels.sec_freq,
1901 		event.acs_selected_channels.ch_width,
1902 		event.acs_selected_channels.vht_seg0_center_ch,
1903 		event.acs_selected_channels.vht_seg1_center_ch,
1904 		event.acs_selected_channels.hw_mode);
1905 	wpa_supplicant_event(drv->ctx, EVENT_ACS_CHANNEL_SELECTED,
1906 		&event);
1907 	return;
1908 }
1909 
nl80211_vendor_event_brcm(struct wpa_driver_nl80211_data * drv,u32 subcmd,u8 * data,size_t len)1910 static void nl80211_vendor_event_brcm( struct wpa_driver_nl80211_data *drv,
1911 	u32 subcmd, u8 *data, size_t len)
1912 {
1913 	union wpa_event_data event;
1914         const struct nlattr *iter;
1915         int rem = len;
1916 
1917 	wpa_printf(MSG_MSGDUMP, "got vendor event %d", subcmd);
1918 	memset(&event, 0, sizeof(event));
1919 	switch (subcmd) {
1920 	case BRCM_VENDOR_EVENT_ACS:
1921 		wpa_printf(MSG_DEBUG, "nl80211: Received VENDOR_EVENT_ACS");
1922 		brcm_nl80211_acs_select_ch(drv, data, len);
1923 		break;
1924 	default:
1925 		wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event %u", subcmd);
1926 		break;
1927 	}
1928 
1929 }
1930 #endif /* CONFIG_DRIVER_NL80211_BRCM */
1931 #ifdef CONFIG_DRIVER_NL80211_QCA
1932 
qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data * drv,const u8 * data,size_t len)1933 static void qca_nl80211_avoid_freq(struct wpa_driver_nl80211_data *drv,
1934 				   const u8 *data, size_t len)
1935 {
1936 	u32 i, count;
1937 	union wpa_event_data event;
1938 	struct wpa_freq_range *range = NULL;
1939 	const struct qca_avoid_freq_list *freq_range;
1940 
1941 	freq_range = (const struct qca_avoid_freq_list *) data;
1942 	if (len < sizeof(freq_range->count))
1943 		return;
1944 
1945 	count = freq_range->count;
1946 	if (len < sizeof(freq_range->count) +
1947 	    count * sizeof(struct qca_avoid_freq_range)) {
1948 		wpa_printf(MSG_DEBUG, "nl80211: Ignored too short avoid frequency list (len=%u)",
1949 			   (unsigned int) len);
1950 		return;
1951 	}
1952 
1953 	if (count > 0) {
1954 		range = os_calloc(count, sizeof(struct wpa_freq_range));
1955 		if (range == NULL)
1956 			return;
1957 	}
1958 
1959 	os_memset(&event, 0, sizeof(event));
1960 	for (i = 0; i < count; i++) {
1961 		unsigned int idx = event.freq_range.num;
1962 		range[idx].min = freq_range->range[i].start_freq;
1963 		range[idx].max = freq_range->range[i].end_freq;
1964 		wpa_printf(MSG_DEBUG, "nl80211: Avoid frequency range: %u-%u",
1965 			   range[idx].min, range[idx].max);
1966 		if (range[idx].min > range[idx].max) {
1967 			wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid frequency range");
1968 			continue;
1969 		}
1970 		event.freq_range.num++;
1971 	}
1972 	event.freq_range.range = range;
1973 
1974 	wpa_supplicant_event(drv->ctx, EVENT_AVOID_FREQUENCIES, &event);
1975 
1976 	os_free(range);
1977 }
1978 
1979 
get_qca_hw_mode(u8 hw_mode)1980 static enum hostapd_hw_mode get_qca_hw_mode(u8 hw_mode)
1981 {
1982 	switch (hw_mode) {
1983 	case QCA_ACS_MODE_IEEE80211B:
1984 		return HOSTAPD_MODE_IEEE80211B;
1985 	case QCA_ACS_MODE_IEEE80211G:
1986 		return HOSTAPD_MODE_IEEE80211G;
1987 	case QCA_ACS_MODE_IEEE80211A:
1988 		return HOSTAPD_MODE_IEEE80211A;
1989 	case QCA_ACS_MODE_IEEE80211AD:
1990 		return HOSTAPD_MODE_IEEE80211AD;
1991 	case QCA_ACS_MODE_IEEE80211ANY:
1992 		return HOSTAPD_MODE_IEEE80211ANY;
1993 	default:
1994 		return NUM_HOSTAPD_MODES;
1995 	}
1996 }
1997 
1998 
chan_to_freq(struct wpa_driver_nl80211_data * drv,u8 chan,enum hostapd_hw_mode hw_mode)1999 static unsigned int chan_to_freq(struct wpa_driver_nl80211_data *drv,
2000 				 u8 chan, enum hostapd_hw_mode hw_mode)
2001 {
2002 	if (hw_mode == NUM_HOSTAPD_MODES) {
2003 		/* For drivers that do not report ACS_HW_MODE */
2004 		u16 num_modes, flags;
2005 		struct hostapd_hw_modes *modes;
2006 		u8 dfs_domain;
2007 		int i;
2008 
2009 		modes = nl80211_get_hw_feature_data(drv->first_bss, &num_modes,
2010 						    &flags, &dfs_domain);
2011 		if (!modes) {
2012 			wpa_printf(MSG_DEBUG,
2013 				   "nl80211: Fetching hardware mode failed");
2014 			goto try_2_4_or_5;
2015 		}
2016 		if (num_modes == 1)
2017 			hw_mode = modes[0].mode;
2018 
2019 		for (i = 0; i < num_modes; i++) {
2020 			os_free(modes[i].channels);
2021 			os_free(modes[i].rates);
2022 		}
2023 
2024 		os_free(modes);
2025 	}
2026 
2027 	if (hw_mode == HOSTAPD_MODE_IEEE80211AD) {
2028 		if (chan >= 1 && chan <= 6)
2029 			return 56160 + (2160 * chan);
2030 		return 0;
2031 	}
2032 
2033 try_2_4_or_5:
2034 	if (chan >= 1 && chan <= 13)
2035 		return 2407 + 5 * chan;
2036 	if (chan == 14)
2037 		return 2484;
2038 	if (chan >= 36 && chan <= 177)
2039 		return 5000 + 5 * chan;
2040 
2041 	return 0;
2042 }
2043 
2044 
qca_nl80211_acs_select_ch(struct wpa_driver_nl80211_data * drv,const u8 * data,size_t len)2045 static void qca_nl80211_acs_select_ch(struct wpa_driver_nl80211_data *drv,
2046 				   const u8 *data, size_t len)
2047 {
2048 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_ACS_MAX + 1];
2049 	union wpa_event_data event;
2050 	u8 chan;
2051 
2052 	wpa_printf(MSG_DEBUG,
2053 		   "nl80211: ACS channel selection vendor event received");
2054 
2055 	if (nla_parse(tb, QCA_WLAN_VENDOR_ATTR_ACS_MAX,
2056 		      (struct nlattr *) data, len, NULL) ||
2057 	    (!tb[QCA_WLAN_VENDOR_ATTR_ACS_PRIMARY_FREQUENCY] &&
2058 	     !tb[QCA_WLAN_VENDOR_ATTR_ACS_PRIMARY_CHANNEL]) ||
2059 	    (!tb[QCA_WLAN_VENDOR_ATTR_ACS_SECONDARY_FREQUENCY] &&
2060 	     !tb[QCA_WLAN_VENDOR_ATTR_ACS_SECONDARY_CHANNEL]))
2061 		return;
2062 
2063 	os_memset(&event, 0, sizeof(event));
2064 	event.acs_selected_channels.hw_mode = NUM_HOSTAPD_MODES;
2065 
2066 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE]) {
2067 		u8 hw_mode = nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE]);
2068 
2069 		event.acs_selected_channels.hw_mode = get_qca_hw_mode(hw_mode);
2070 		if (event.acs_selected_channels.hw_mode == NUM_HOSTAPD_MODES ||
2071 		    event.acs_selected_channels.hw_mode ==
2072 		    HOSTAPD_MODE_IEEE80211ANY) {
2073 			wpa_printf(MSG_DEBUG,
2074 				   "nl80211: Invalid hw_mode %d in ACS selection event",
2075 				   hw_mode);
2076 			return;
2077 		}
2078 	}
2079 
2080 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_PRIMARY_FREQUENCY]) {
2081 		event.acs_selected_channels.pri_freq = nla_get_u32(
2082 			tb[QCA_WLAN_VENDOR_ATTR_ACS_PRIMARY_FREQUENCY]);
2083 	} else {
2084 		chan = nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_ACS_PRIMARY_CHANNEL]);
2085 		event.acs_selected_channels.pri_freq =
2086 			chan_to_freq(drv, chan,
2087 				     event.acs_selected_channels.hw_mode);
2088 	}
2089 
2090 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_SECONDARY_FREQUENCY]) {
2091 		event.acs_selected_channels.sec_freq = nla_get_u32(
2092 			tb[QCA_WLAN_VENDOR_ATTR_ACS_SECONDARY_FREQUENCY]);
2093 	} else {
2094 		chan = nla_get_u8(
2095 			tb[QCA_WLAN_VENDOR_ATTR_ACS_SECONDARY_CHANNEL]);
2096 		event.acs_selected_channels.sec_freq =
2097 			chan_to_freq(drv, chan,
2098 				     event.acs_selected_channels.hw_mode);
2099 	}
2100 
2101 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_EDMG_CHANNEL])
2102 		event.acs_selected_channels.edmg_channel =
2103 			nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_ACS_EDMG_CHANNEL]);
2104 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_VHT_SEG0_CENTER_CHANNEL])
2105 		event.acs_selected_channels.vht_seg0_center_ch =
2106 			nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_ACS_VHT_SEG0_CENTER_CHANNEL]);
2107 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_VHT_SEG1_CENTER_CHANNEL])
2108 		event.acs_selected_channels.vht_seg1_center_ch =
2109 			nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_ACS_VHT_SEG1_CENTER_CHANNEL]);
2110 	if (tb[QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH])
2111 		event.acs_selected_channels.ch_width =
2112 			nla_get_u16(tb[QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH]);
2113 	wpa_printf(MSG_INFO,
2114 		   "nl80211: ACS Results: PFreq: %d SFreq: %d BW: %d VHT0: %d VHT1: %d HW_MODE: %d EDMGCH: %d",
2115 		   event.acs_selected_channels.pri_freq,
2116 		   event.acs_selected_channels.sec_freq,
2117 		   event.acs_selected_channels.ch_width,
2118 		   event.acs_selected_channels.vht_seg0_center_ch,
2119 		   event.acs_selected_channels.vht_seg1_center_ch,
2120 		   event.acs_selected_channels.hw_mode,
2121 		   event.acs_selected_channels.edmg_channel);
2122 
2123 	/* Ignore ACS channel list check for backwards compatibility */
2124 
2125 	wpa_supplicant_event(drv->ctx, EVENT_ACS_CHANNEL_SELECTED, &event);
2126 }
2127 
2128 
qca_nl80211_key_mgmt_auth(struct wpa_driver_nl80211_data * drv,const u8 * data,size_t len)2129 static void qca_nl80211_key_mgmt_auth(struct wpa_driver_nl80211_data *drv,
2130 				      const u8 *data, size_t len)
2131 {
2132 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_MAX + 1];
2133 	u8 *bssid;
2134 
2135 	wpa_printf(MSG_DEBUG,
2136 		   "nl80211: Key management roam+auth vendor event received");
2137 
2138 	if (nla_parse(tb, QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_MAX,
2139 		      (struct nlattr *) data, len, NULL) ||
2140 	    !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID] ||
2141 	    nla_len(tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID]) != ETH_ALEN ||
2142 	    !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_REQ_IE] ||
2143 	    !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_RESP_IE] ||
2144 	    !tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_AUTHORIZED])
2145 		return;
2146 
2147 	bssid = nla_data(tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID]);
2148 	wpa_printf(MSG_DEBUG, "  * roam BSSID " MACSTR, MAC2STR(bssid));
2149 
2150 	mlme_event_connect(drv, NL80211_CMD_ROAM, NULL,
2151 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_BSSID],
2152 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_REQ_IE],
2153 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_RESP_IE],
2154 			   NULL, NULL,
2155 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_AUTHORIZED],
2156 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_KEY_REPLAY_CTR],
2157 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_PTK_KCK],
2158 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_PTK_KEK],
2159 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_SUBNET_STATUS],
2160 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_FILS_ERP_NEXT_SEQ_NUM],
2161 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_PMK],
2162 			   tb[QCA_WLAN_VENDOR_ATTR_ROAM_AUTH_PMKID]);
2163 }
2164 
2165 
2166 static void
qca_nl80211_key_mgmt_auth_handler(struct wpa_driver_nl80211_data * drv,const u8 * data,size_t len)2167 qca_nl80211_key_mgmt_auth_handler(struct wpa_driver_nl80211_data *drv,
2168 				  const u8 *data, size_t len)
2169 {
2170 	if (!drv->roam_indication_done) {
2171 		wpa_printf(MSG_DEBUG,
2172 			   "nl80211: Pending roam indication, delay processing roam+auth vendor event");
2173 		os_get_reltime(&drv->pending_roam_ind_time);
2174 
2175 		os_free(drv->pending_roam_data);
2176 		drv->pending_roam_data = os_memdup(data, len);
2177 		if (!drv->pending_roam_data)
2178 			return;
2179 		drv->pending_roam_data_len = len;
2180 		return;
2181 	}
2182 	drv->roam_indication_done = false;
2183 	qca_nl80211_key_mgmt_auth(drv, data, len);
2184 }
2185 
2186 
qca_nl80211_dfs_offload_radar_event(struct wpa_driver_nl80211_data * drv,u32 subcmd,u8 * msg,int length)2187 static void qca_nl80211_dfs_offload_radar_event(
2188 	struct wpa_driver_nl80211_data *drv, u32 subcmd, u8 *msg, int length)
2189 {
2190 	union wpa_event_data data;
2191 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
2192 
2193 	wpa_printf(MSG_DEBUG,
2194 		   "nl80211: DFS offload radar vendor event received");
2195 
2196 	if (nla_parse(tb, NL80211_ATTR_MAX,
2197 		      (struct nlattr *) msg, length, NULL))
2198 		return;
2199 
2200 	if (!tb[NL80211_ATTR_WIPHY_FREQ]) {
2201 		wpa_printf(MSG_INFO,
2202 			   "nl80211: Error parsing WIPHY_FREQ in FS offload radar vendor event");
2203 		return;
2204 	}
2205 
2206 	os_memset(&data, 0, sizeof(data));
2207 	data.dfs_event.freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
2208 
2209 	wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz",
2210 		   data.dfs_event.freq);
2211 
2212 	/* Check HT params */
2213 	if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2214 		data.dfs_event.ht_enabled = 1;
2215 		data.dfs_event.chan_offset = 0;
2216 
2217 		switch (nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
2218 		case NL80211_CHAN_NO_HT:
2219 			data.dfs_event.ht_enabled = 0;
2220 			break;
2221 		case NL80211_CHAN_HT20:
2222 			break;
2223 		case NL80211_CHAN_HT40PLUS:
2224 			data.dfs_event.chan_offset = 1;
2225 			break;
2226 		case NL80211_CHAN_HT40MINUS:
2227 			data.dfs_event.chan_offset = -1;
2228 			break;
2229 		}
2230 	}
2231 
2232 	/* Get VHT params */
2233 	if (tb[NL80211_ATTR_CHANNEL_WIDTH])
2234 		data.dfs_event.chan_width =
2235 			convert2width(nla_get_u32(
2236 					      tb[NL80211_ATTR_CHANNEL_WIDTH]));
2237 	if (tb[NL80211_ATTR_CENTER_FREQ1])
2238 		data.dfs_event.cf1 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
2239 	if (tb[NL80211_ATTR_CENTER_FREQ2])
2240 		data.dfs_event.cf2 = nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
2241 
2242 	wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz, ht: %d, "
2243 		    "offset: %d, width: %d, cf1: %dMHz, cf2: %dMHz",
2244 		    data.dfs_event.freq, data.dfs_event.ht_enabled,
2245 		    data.dfs_event.chan_offset, data.dfs_event.chan_width,
2246 		    data.dfs_event.cf1, data.dfs_event.cf2);
2247 
2248 	switch (subcmd) {
2249 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_RADAR_DETECTED:
2250 		wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
2251 		break;
2252 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_STARTED:
2253 		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_STARTED, &data);
2254 		break;
2255 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_FINISHED:
2256 		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
2257 		break;
2258 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_ABORTED:
2259 		wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
2260 		break;
2261 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_NOP_FINISHED:
2262 		wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
2263 		break;
2264 	default:
2265 		wpa_printf(MSG_DEBUG,
2266 			   "nl80211: Unknown DFS offload radar event %d received",
2267 			   subcmd);
2268 		break;
2269 	}
2270 }
2271 
2272 
qca_nl80211_scan_trigger_event(struct wpa_driver_nl80211_data * drv,u8 * data,size_t len)2273 static void qca_nl80211_scan_trigger_event(struct wpa_driver_nl80211_data *drv,
2274 					   u8 *data, size_t len)
2275 {
2276 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1];
2277 	u64 cookie = 0;
2278 	union wpa_event_data event;
2279 	struct scan_info *info;
2280 
2281 	if (nla_parse(tb, QCA_WLAN_VENDOR_ATTR_SCAN_MAX,
2282 		      (struct nlattr *) data, len, NULL) ||
2283 	    !tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE])
2284 		return;
2285 
2286 	cookie = nla_get_u64(tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]);
2287 	if (cookie != drv->vendor_scan_cookie) {
2288 		/* External scan trigger event, ignore */
2289 		return;
2290 	}
2291 
2292 	/* Cookie match, own scan */
2293 	os_memset(&event, 0, sizeof(event));
2294 	info = &event.scan_info;
2295 	info->external_scan = 0;
2296 	info->nl_scan_event = 0;
2297 
2298 	drv->scan_state = SCAN_STARTED;
2299 	wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, &event);
2300 }
2301 
2302 
send_vendor_scan_event(struct wpa_driver_nl80211_data * drv,int aborted,struct nlattr * tb[],int external_scan)2303 static void send_vendor_scan_event(struct wpa_driver_nl80211_data *drv,
2304 				   int aborted, struct nlattr *tb[],
2305 				   int external_scan)
2306 {
2307 	union wpa_event_data event;
2308 	struct nlattr *nl;
2309 	int rem;
2310 	struct scan_info *info;
2311 	int freqs[MAX_REPORT_FREQS];
2312 	int num_freqs = 0;
2313 
2314 	os_memset(&event, 0, sizeof(event));
2315 	info = &event.scan_info;
2316 	info->aborted = aborted;
2317 	info->external_scan = external_scan;
2318 
2319 	if (tb[QCA_WLAN_VENDOR_ATTR_SCAN_SSIDS]) {
2320 		nla_for_each_nested(nl,
2321 				    tb[QCA_WLAN_VENDOR_ATTR_SCAN_SSIDS], rem) {
2322 			struct wpa_driver_scan_ssid *s =
2323 				&info->ssids[info->num_ssids];
2324 			s->ssid = nla_data(nl);
2325 			s->ssid_len = nla_len(nl);
2326 			wpa_printf(MSG_DEBUG,
2327 				   "nl80211: Scan probed for SSID '%s'",
2328 				   wpa_ssid_txt(s->ssid, s->ssid_len));
2329 			info->num_ssids++;
2330 			if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
2331 				break;
2332 		}
2333 	}
2334 
2335 	if (tb[QCA_WLAN_VENDOR_ATTR_SCAN_FREQUENCIES]) {
2336 		char msg[300], *pos, *end;
2337 		int res;
2338 
2339 		pos = msg;
2340 		end = pos + sizeof(msg);
2341 		*pos = '\0';
2342 
2343 		nla_for_each_nested(nl,
2344 				    tb[QCA_WLAN_VENDOR_ATTR_SCAN_FREQUENCIES],
2345 				    rem) {
2346 			freqs[num_freqs] = nla_get_u32(nl);
2347 			res = os_snprintf(pos, end - pos, " %d",
2348 					  freqs[num_freqs]);
2349 			if (!os_snprintf_error(end - pos, res))
2350 				pos += res;
2351 			num_freqs++;
2352 			if (num_freqs == MAX_REPORT_FREQS - 1)
2353 				break;
2354 		}
2355 
2356 		info->freqs = freqs;
2357 		info->num_freqs = num_freqs;
2358 		wpa_printf(MSG_DEBUG, "nl80211: Scan included frequencies:%s",
2359 			   msg);
2360 	}
2361 	wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
2362 }
2363 
2364 
qca_nl80211_scan_done_event(struct wpa_driver_nl80211_data * drv,u8 * data,size_t len)2365 static void qca_nl80211_scan_done_event(struct wpa_driver_nl80211_data *drv,
2366 					u8 *data, size_t len)
2367 {
2368 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1];
2369 	u64 cookie = 0;
2370 	enum scan_status status;
2371 	int external_scan;
2372 
2373 	if (nla_parse(tb, QCA_WLAN_VENDOR_ATTR_SCAN_MAX,
2374 		      (struct nlattr *) data, len, NULL) ||
2375 	    !tb[QCA_WLAN_VENDOR_ATTR_SCAN_STATUS] ||
2376 	    !tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE])
2377 		return;
2378 
2379 	status = nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_SCAN_STATUS]);
2380 	if (status >= VENDOR_SCAN_STATUS_MAX)
2381 		return; /* invalid status */
2382 
2383 	cookie = nla_get_u64(tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]);
2384 	if (cookie != drv->vendor_scan_cookie) {
2385 		/* Event from an external scan, get scan results */
2386 		external_scan = 1;
2387 	} else {
2388 		external_scan = 0;
2389 		if (status == VENDOR_SCAN_STATUS_NEW_RESULTS)
2390 			drv->scan_state = SCAN_COMPLETED;
2391 		else
2392 			drv->scan_state = SCAN_ABORTED;
2393 
2394 		eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2395 				     drv->ctx);
2396 		drv->vendor_scan_cookie = 0;
2397 		drv->last_scan_cmd = 0;
2398 	}
2399 
2400 	send_vendor_scan_event(drv, (status == VENDOR_SCAN_STATUS_ABORTED), tb,
2401 			       external_scan);
2402 }
2403 
2404 
qca_nl80211_p2p_lo_stop_event(struct wpa_driver_nl80211_data * drv,u8 * data,size_t len)2405 static void qca_nl80211_p2p_lo_stop_event(struct wpa_driver_nl80211_data *drv,
2406 					  u8 *data, size_t len)
2407 {
2408 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_MAX + 1];
2409 	union wpa_event_data event;
2410 
2411 	wpa_printf(MSG_DEBUG,
2412 		   "nl80211: P2P listen offload stop vendor event received");
2413 
2414 	if (nla_parse(tb, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_MAX,
2415 		      (struct nlattr *) data, len, NULL) ||
2416 	    !tb[QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_STOP_REASON])
2417 		return;
2418 
2419 	os_memset(&event, 0, sizeof(event));
2420 	event.p2p_lo_stop.reason_code =
2421 		nla_get_u8(tb[QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_STOP_REASON]);
2422 
2423 	wpa_printf(MSG_DEBUG,
2424 		   "nl80211: P2P Listen offload stop reason: %d",
2425 		   event.p2p_lo_stop.reason_code);
2426 	wpa_supplicant_event(drv->ctx, EVENT_P2P_LO_STOP, &event);
2427 }
2428 
2429 #endif /* CONFIG_DRIVER_NL80211_QCA */
2430 
2431 
nl80211_vendor_event_qca(struct wpa_driver_nl80211_data * drv,u32 subcmd,u8 * data,size_t len)2432 static void nl80211_vendor_event_qca(struct wpa_driver_nl80211_data *drv,
2433 				     u32 subcmd, u8 *data, size_t len)
2434 {
2435 	switch (subcmd) {
2436 	case QCA_NL80211_VENDOR_SUBCMD_TEST:
2437 		wpa_hexdump(MSG_DEBUG, "nl80211: QCA test event", data, len);
2438 		break;
2439 #ifdef CONFIG_DRIVER_NL80211_QCA
2440 	case QCA_NL80211_VENDOR_SUBCMD_AVOID_FREQUENCY:
2441 		qca_nl80211_avoid_freq(drv, data, len);
2442 		break;
2443 	case QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_ROAM_AUTH:
2444 		qca_nl80211_key_mgmt_auth_handler(drv, data, len);
2445 		break;
2446 	case QCA_NL80211_VENDOR_SUBCMD_DO_ACS:
2447 		qca_nl80211_acs_select_ch(drv, data, len);
2448 		break;
2449 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_STARTED:
2450 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_FINISHED:
2451 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_ABORTED:
2452 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_CAC_NOP_FINISHED:
2453 	case QCA_NL80211_VENDOR_SUBCMD_DFS_OFFLOAD_RADAR_DETECTED:
2454 		qca_nl80211_dfs_offload_radar_event(drv, subcmd, data, len);
2455 		break;
2456 	case QCA_NL80211_VENDOR_SUBCMD_TRIGGER_SCAN:
2457 		qca_nl80211_scan_trigger_event(drv, data, len);
2458 		break;
2459 	case QCA_NL80211_VENDOR_SUBCMD_SCAN_DONE:
2460 		qca_nl80211_scan_done_event(drv, data, len);
2461 		break;
2462 	case QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP:
2463 		qca_nl80211_p2p_lo_stop_event(drv, data, len);
2464 		break;
2465 #endif /* CONFIG_DRIVER_NL80211_QCA */
2466 	default:
2467 		wpa_printf(MSG_DEBUG,
2468 			   "nl80211: Ignore unsupported QCA vendor event %u",
2469 			   subcmd);
2470 		break;
2471 	}
2472 }
2473 
2474 
nl80211_vendor_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)2475 static void nl80211_vendor_event(struct wpa_driver_nl80211_data *drv,
2476 				 struct nlattr **tb)
2477 {
2478 	u32 vendor_id, subcmd, wiphy = 0;
2479 	int wiphy_idx;
2480 	u8 *data = NULL;
2481 	size_t len = 0;
2482 
2483 	if (!tb[NL80211_ATTR_VENDOR_ID] ||
2484 	    !tb[NL80211_ATTR_VENDOR_SUBCMD])
2485 		return;
2486 
2487 	vendor_id = nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]);
2488 	subcmd = nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]);
2489 
2490 	if (tb[NL80211_ATTR_WIPHY])
2491 		wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2492 
2493 	wpa_printf(MSG_DEBUG, "nl80211: Vendor event: wiphy=%u vendor_id=0x%x subcmd=%u",
2494 		   wiphy, vendor_id, subcmd);
2495 
2496 	if (tb[NL80211_ATTR_VENDOR_DATA]) {
2497 		data = nla_data(tb[NL80211_ATTR_VENDOR_DATA]);
2498 		len = nla_len(tb[NL80211_ATTR_VENDOR_DATA]);
2499 		wpa_hexdump(MSG_MSGDUMP, "nl80211: Vendor data", data, len);
2500 	}
2501 
2502 	wiphy_idx = nl80211_get_wiphy_index(drv->first_bss);
2503 	if (wiphy_idx >= 0 && wiphy_idx != (int) wiphy) {
2504 		wpa_printf(MSG_DEBUG, "nl80211: Ignore vendor event for foreign wiphy %u (own: %d)",
2505 			   wiphy, wiphy_idx);
2506 		return;
2507 	}
2508 
2509 #ifdef ANDROID
2510 #ifdef ANDROID_LIB_EVENT
2511        wpa_driver_nl80211_driver_event(drv, vendor_id, subcmd, data, len);
2512 #endif /* ANDROID_LIB_EVENT */
2513 #endif /* ANDROID */
2514 
2515 	switch (vendor_id) {
2516 	case OUI_QCA:
2517 		nl80211_vendor_event_qca(drv, subcmd, data, len);
2518 		break;
2519 #ifdef CONFIG_DRIVER_NL80211_BRCM
2520 	case OUI_BRCM:
2521 	        nl80211_vendor_event_brcm(drv, subcmd, data, len);
2522 	        break;
2523 #endif /* CONFIG_DRIVER_NL80211_BRCM */
2524 	default:
2525 		wpa_printf(MSG_DEBUG, "nl80211: Ignore unsupported vendor event");
2526 		break;
2527 	}
2528 }
2529 
2530 
nl80211_reg_change_event(struct wpa_driver_nl80211_data * drv,struct nlattr * tb[])2531 static void nl80211_reg_change_event(struct wpa_driver_nl80211_data *drv,
2532 				     struct nlattr *tb[])
2533 {
2534 	union wpa_event_data data;
2535 	enum nl80211_reg_initiator init;
2536 
2537 	wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2538 
2539 	if (tb[NL80211_ATTR_REG_INITIATOR] == NULL)
2540 		return;
2541 
2542 	os_memset(&data, 0, sizeof(data));
2543 	init = nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]);
2544 	wpa_printf(MSG_DEBUG, " * initiator=%d", init);
2545 	switch (init) {
2546 	case NL80211_REGDOM_SET_BY_CORE:
2547 		data.channel_list_changed.initiator = REGDOM_SET_BY_CORE;
2548 		break;
2549 	case NL80211_REGDOM_SET_BY_USER:
2550 		data.channel_list_changed.initiator = REGDOM_SET_BY_USER;
2551 		break;
2552 	case NL80211_REGDOM_SET_BY_DRIVER:
2553 		data.channel_list_changed.initiator = REGDOM_SET_BY_DRIVER;
2554 		break;
2555 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2556 		data.channel_list_changed.initiator = REGDOM_SET_BY_COUNTRY_IE;
2557 		break;
2558 	}
2559 
2560 	if (tb[NL80211_ATTR_REG_TYPE]) {
2561 		enum nl80211_reg_type type;
2562 		type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
2563 		wpa_printf(MSG_DEBUG, " * type=%d", type);
2564 		switch (type) {
2565 		case NL80211_REGDOM_TYPE_COUNTRY:
2566 			data.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
2567 			break;
2568 		case NL80211_REGDOM_TYPE_WORLD:
2569 			data.channel_list_changed.type = REGDOM_TYPE_WORLD;
2570 			break;
2571 		case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
2572 			data.channel_list_changed.type =
2573 				REGDOM_TYPE_CUSTOM_WORLD;
2574 			break;
2575 		case NL80211_REGDOM_TYPE_INTERSECTION:
2576 			data.channel_list_changed.type =
2577 				REGDOM_TYPE_INTERSECTION;
2578 			break;
2579 		}
2580 	}
2581 
2582 	if (tb[NL80211_ATTR_REG_ALPHA2]) {
2583 		os_strlcpy(data.channel_list_changed.alpha2,
2584 			   nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
2585 			   sizeof(data.channel_list_changed.alpha2));
2586 		wpa_printf(MSG_DEBUG, " * alpha2=%s",
2587 			   data.channel_list_changed.alpha2);
2588 	}
2589 
2590 	wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
2591 }
2592 
2593 
nl80211_dump_freq(const char * title,struct nlattr * nl_freq)2594 static void nl80211_dump_freq(const char *title, struct nlattr *nl_freq)
2595 {
2596 	static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2597 		[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2598 		[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2599 		[NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
2600 		[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2601 		[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2602 	};
2603 	struct nlattr *tb[NL80211_FREQUENCY_ATTR_MAX + 1];
2604 	u32 freq = 0, max_tx_power = 0;
2605 
2606 	nla_parse(tb, NL80211_FREQUENCY_ATTR_MAX,
2607 		  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
2608 
2609 	if (tb[NL80211_FREQUENCY_ATTR_FREQ])
2610 		freq = nla_get_u32(tb[NL80211_FREQUENCY_ATTR_FREQ]);
2611 	if (tb[NL80211_FREQUENCY_ATTR_MAX_TX_POWER])
2612 		max_tx_power =
2613 			nla_get_u32(tb[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]);
2614 
2615 	wpa_printf(MSG_DEBUG,
2616 		   "nl80211: Channel (%s): freq=%u max_tx_power=%u%s%s%s",
2617 		   title, freq, max_tx_power,
2618 		   tb[NL80211_FREQUENCY_ATTR_DISABLED] ? " disabled" : "",
2619 		   tb[NL80211_FREQUENCY_ATTR_NO_IR] ? " no-IR" : "",
2620 		   tb[NL80211_FREQUENCY_ATTR_RADAR] ? " radar" : "");
2621 }
2622 
2623 
nl80211_reg_beacon_hint_event(struct wpa_driver_nl80211_data * drv,struct nlattr * tb[])2624 static void nl80211_reg_beacon_hint_event(struct wpa_driver_nl80211_data *drv,
2625 					   struct nlattr *tb[])
2626 {
2627 	union wpa_event_data data;
2628 
2629 	wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2630 	os_memset(&data, 0, sizeof(data));
2631 	data.channel_list_changed.initiator = REGDOM_BEACON_HINT;
2632 
2633 	if (tb[NL80211_ATTR_FREQ_BEFORE])
2634 		nl80211_dump_freq("before", tb[NL80211_ATTR_FREQ_BEFORE]);
2635 	if (tb[NL80211_ATTR_FREQ_AFTER])
2636 		nl80211_dump_freq("after", tb[NL80211_ATTR_FREQ_AFTER]);
2637 
2638 	wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED, &data);
2639 }
2640 
2641 
nl80211_external_auth(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)2642 static void nl80211_external_auth(struct wpa_driver_nl80211_data *drv,
2643 				  struct nlattr **tb)
2644 {
2645 	union wpa_event_data event;
2646 	enum nl80211_external_auth_action act;
2647 
2648 	if (!tb[NL80211_ATTR_AKM_SUITES] ||
2649 	    !tb[NL80211_ATTR_EXTERNAL_AUTH_ACTION] ||
2650 	    !tb[NL80211_ATTR_BSSID] ||
2651 	    !tb[NL80211_ATTR_SSID])
2652 		return;
2653 
2654 	os_memset(&event, 0, sizeof(event));
2655 	act = nla_get_u32(tb[NL80211_ATTR_EXTERNAL_AUTH_ACTION]);
2656 	switch (act) {
2657 	case NL80211_EXTERNAL_AUTH_START:
2658 		event.external_auth.action = EXT_AUTH_START;
2659 		break;
2660 	case NL80211_EXTERNAL_AUTH_ABORT:
2661 		event.external_auth.action = EXT_AUTH_ABORT;
2662 		break;
2663 	default:
2664 		return;
2665 	}
2666 
2667 	event.external_auth.key_mgmt_suite =
2668 		nla_get_u32(tb[NL80211_ATTR_AKM_SUITES]);
2669 
2670 	event.external_auth.ssid_len = nla_len(tb[NL80211_ATTR_SSID]);
2671 	if (event.external_auth.ssid_len > SSID_MAX_LEN)
2672 		return;
2673 	event.external_auth.ssid = nla_data(tb[NL80211_ATTR_SSID]);
2674 
2675 	event.external_auth.bssid = nla_data(tb[NL80211_ATTR_BSSID]);
2676 
2677 	wpa_printf(MSG_DEBUG,
2678 		   "nl80211: External auth action: %u, AKM: 0x%x",
2679 		   event.external_auth.action,
2680 		   event.external_auth.key_mgmt_suite);
2681 	wpa_supplicant_event(drv->ctx, EVENT_EXTERNAL_AUTH, &event);
2682 }
2683 
2684 
nl80211_port_authorized(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)2685 static void nl80211_port_authorized(struct wpa_driver_nl80211_data *drv,
2686 				    struct nlattr **tb)
2687 {
2688 	const u8 *addr;
2689 
2690 	if (!tb[NL80211_ATTR_MAC] ||
2691 	    nla_len(tb[NL80211_ATTR_MAC]) != ETH_ALEN) {
2692 		wpa_printf(MSG_DEBUG,
2693 			   "nl80211: Ignore port authorized event without BSSID");
2694 		return;
2695 	}
2696 
2697 	addr = nla_data(tb[NL80211_ATTR_MAC]);
2698 	if (os_memcmp(addr, drv->bssid, ETH_ALEN) != 0) {
2699 		wpa_printf(MSG_DEBUG,
2700 			   "nl80211: Ignore port authorized event for " MACSTR
2701 			   " (not the currently connected BSSID " MACSTR ")",
2702 			   MAC2STR(addr), MAC2STR(drv->bssid));
2703 		return;
2704 	}
2705 
2706 	wpa_supplicant_event(drv->ctx, EVENT_PORT_AUTHORIZED, NULL);
2707 }
2708 
2709 
nl80211_sta_opmode_change_event(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)2710 static void nl80211_sta_opmode_change_event(struct wpa_driver_nl80211_data *drv,
2711 					    struct nlattr **tb)
2712 {
2713 	union wpa_event_data ed;
2714 	u8 smps_mode, max_bw;
2715 
2716 	if (!tb[NL80211_ATTR_MAC] ||
2717 	    (!tb[NL80211_ATTR_CHANNEL_WIDTH] &&
2718 	     !tb[NL80211_ATTR_SMPS_MODE] &&
2719 	     !tb[NL80211_ATTR_NSS]))
2720 		return;
2721 
2722 	ed.sta_opmode.smps_mode = SMPS_INVALID;
2723 	ed.sta_opmode.chan_width = CHAN_WIDTH_UNKNOWN;
2724 	ed.sta_opmode.rx_nss = 0xff;
2725 	ed.sta_opmode.addr = nla_data(tb[NL80211_ATTR_MAC]);
2726 
2727 	if (tb[NL80211_ATTR_SMPS_MODE]) {
2728 		smps_mode = nla_get_u8(tb[NL80211_ATTR_SMPS_MODE]);
2729 		switch (smps_mode) {
2730 		case NL80211_SMPS_OFF:
2731 			ed.sta_opmode.smps_mode = SMPS_OFF;
2732 			break;
2733 		case NL80211_SMPS_STATIC:
2734 			ed.sta_opmode.smps_mode = SMPS_STATIC;
2735 			break;
2736 		case NL80211_SMPS_DYNAMIC:
2737 			ed.sta_opmode.smps_mode = SMPS_DYNAMIC;
2738 			break;
2739 		default:
2740 			ed.sta_opmode.smps_mode = SMPS_INVALID;
2741 			break;
2742 		}
2743 	}
2744 
2745 	if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
2746 		max_bw = nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]);
2747 		switch (max_bw) {
2748 		case NL80211_CHAN_WIDTH_20_NOHT:
2749 			ed.sta_opmode.chan_width = CHAN_WIDTH_20_NOHT;
2750 			break;
2751 		case NL80211_CHAN_WIDTH_20:
2752 			ed.sta_opmode.chan_width = CHAN_WIDTH_20;
2753 			break;
2754 		case NL80211_CHAN_WIDTH_40:
2755 			ed.sta_opmode.chan_width = CHAN_WIDTH_40;
2756 			break;
2757 		case NL80211_CHAN_WIDTH_80:
2758 			ed.sta_opmode.chan_width = CHAN_WIDTH_80;
2759 			break;
2760 		case NL80211_CHAN_WIDTH_80P80:
2761 			ed.sta_opmode.chan_width = CHAN_WIDTH_80P80;
2762 			break;
2763 		case NL80211_CHAN_WIDTH_160:
2764 			ed.sta_opmode.chan_width = CHAN_WIDTH_160;
2765 			break;
2766 		default:
2767 			ed.sta_opmode.chan_width = CHAN_WIDTH_UNKNOWN;
2768 			break;
2769 
2770 		}
2771 	}
2772 
2773 	if (tb[NL80211_ATTR_NSS])
2774 		ed.sta_opmode.rx_nss = nla_get_u8(tb[NL80211_ATTR_NSS]);
2775 
2776 	wpa_supplicant_event(drv->ctx, EVENT_STATION_OPMODE_CHANGED, &ed);
2777 }
2778 
2779 
nl80211_control_port_frame(struct wpa_driver_nl80211_data * drv,struct nlattr ** tb)2780 static void nl80211_control_port_frame(struct wpa_driver_nl80211_data *drv,
2781 				       struct nlattr **tb)
2782 {
2783 	u8 *src_addr;
2784 	u16 ethertype;
2785 
2786 	if (!tb[NL80211_ATTR_MAC] ||
2787 	    !tb[NL80211_ATTR_FRAME] ||
2788 	    !tb[NL80211_ATTR_CONTROL_PORT_ETHERTYPE])
2789 		return;
2790 
2791 	src_addr = nla_data(tb[NL80211_ATTR_MAC]);
2792 	ethertype = nla_get_u16(tb[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
2793 
2794 	switch (ethertype) {
2795 	case ETH_P_RSN_PREAUTH:
2796 		wpa_printf(MSG_INFO, "nl80211: Got pre-auth frame from "
2797 			   MACSTR " over control port unexpectedly",
2798 			   MAC2STR(src_addr));
2799 		break;
2800 	case ETH_P_PAE:
2801 		drv_event_eapol_rx(drv->ctx, src_addr,
2802 				   nla_data(tb[NL80211_ATTR_FRAME]),
2803 				   nla_len(tb[NL80211_ATTR_FRAME]));
2804 		break;
2805 	default:
2806 		wpa_printf(MSG_INFO, "nl80211: Unxpected ethertype 0x%04x from "
2807 			   MACSTR " over control port",
2808 			   ethertype, MAC2STR(src_addr));
2809 		break;
2810 	}
2811 }
2812 
2813 
2814 static void
nl80211_control_port_frame_tx_status(struct wpa_driver_nl80211_data * drv,const u8 * frame,size_t len,struct nlattr * ack,struct nlattr * cookie)2815 nl80211_control_port_frame_tx_status(struct wpa_driver_nl80211_data *drv,
2816 				     const u8 *frame, size_t len,
2817 				     struct nlattr *ack, struct nlattr *cookie)
2818 {
2819 	union wpa_event_data event;
2820 
2821 	if (!cookie || len < ETH_HLEN)
2822 		return;
2823 
2824 	wpa_printf(MSG_DEBUG,
2825 		   "nl80211: Control port TX status (ack=%d), cookie=%llu",
2826 		   ack != NULL, (long long unsigned int) nla_get_u64(cookie));
2827 
2828 	os_memset(&event, 0, sizeof(event));
2829 	event.eapol_tx_status.dst = frame;
2830 	event.eapol_tx_status.data = frame + ETH_HLEN;
2831 	event.eapol_tx_status.data_len = len - ETH_HLEN;
2832 	event.eapol_tx_status.ack = ack != NULL;
2833 	wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2834 }
2835 
2836 
do_process_drv_event(struct i802_bss * bss,int cmd,struct nlattr ** tb)2837 static void do_process_drv_event(struct i802_bss *bss, int cmd,
2838 				 struct nlattr **tb)
2839 {
2840 	struct wpa_driver_nl80211_data *drv = bss->drv;
2841 	int external_scan_event = 0;
2842 	struct nlattr *frame = tb[NL80211_ATTR_FRAME];
2843 
2844 	wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
2845 		   cmd, nl80211_command_to_string(cmd), bss->ifname);
2846 
2847 #ifdef CONFIG_DRIVER_NL80211_QCA
2848 	if (cmd == NL80211_CMD_ROAM &&
2849 	    (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2850 		if (drv->pending_roam_data) {
2851 			struct os_reltime now, age;
2852 
2853 			os_get_reltime(&now);
2854 			os_reltime_sub(&now, &drv->pending_roam_ind_time, &age);
2855 			if (age.sec == 0 && age.usec < 100000) {
2856 				wpa_printf(MSG_DEBUG,
2857 					   "nl80211: Process pending roam+auth vendor event");
2858 				qca_nl80211_key_mgmt_auth(
2859 					drv, drv->pending_roam_data,
2860 					drv->pending_roam_data_len);
2861 			}
2862 			os_free(drv->pending_roam_data);
2863 			drv->pending_roam_data = NULL;
2864 			return;
2865 		}
2866 		/*
2867 		 * Device will use roam+auth vendor event to indicate
2868 		 * roaming, so ignore the regular roam event.
2869 		 */
2870 		drv->roam_indication_done = true;
2871 		wpa_printf(MSG_DEBUG,
2872 			   "nl80211: Ignore roam event (cmd=%d), device will use vendor event roam+auth",
2873 			   cmd);
2874 		return;
2875 	}
2876 #endif /* CONFIG_DRIVER_NL80211_QCA */
2877 
2878 	if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2879 	    (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2880 	     cmd == NL80211_CMD_SCAN_ABORTED))
2881 		nl80211_restore_ap_mode(bss);
2882 
2883 	switch (cmd) {
2884 	case NL80211_CMD_TRIGGER_SCAN:
2885 		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
2886 		drv->scan_state = SCAN_STARTED;
2887 		if (drv->scan_for_auth) {
2888 			/*
2889 			 * Cannot indicate EVENT_SCAN_STARTED here since we skip
2890 			 * EVENT_SCAN_RESULTS in scan_for_auth case and the
2891 			 * upper layer implementation could get confused about
2892 			 * scanning state.
2893 			 */
2894 			wpa_printf(MSG_DEBUG, "nl80211: Do not indicate scan-start event due to internal scan_for_auth");
2895 			break;
2896 		}
2897 		wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
2898 		break;
2899 	case NL80211_CMD_START_SCHED_SCAN:
2900 		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
2901 		drv->scan_state = SCHED_SCAN_STARTED;
2902 		break;
2903 	case NL80211_CMD_SCHED_SCAN_STOPPED:
2904 		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
2905 		drv->scan_state = SCHED_SCAN_STOPPED;
2906 		wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2907 		break;
2908 	case NL80211_CMD_NEW_SCAN_RESULTS:
2909 		wpa_dbg(drv->ctx, MSG_DEBUG,
2910 			"nl80211: New scan results available");
2911 		if (drv->last_scan_cmd != NL80211_CMD_VENDOR)
2912 			drv->scan_state = SCAN_COMPLETED;
2913 		drv->scan_complete_events = 1;
2914 		if (drv->last_scan_cmd == NL80211_CMD_TRIGGER_SCAN) {
2915 			eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout,
2916 					     drv, drv->ctx);
2917 			drv->last_scan_cmd = 0;
2918 		} else {
2919 			external_scan_event = 1;
2920 		}
2921 		send_scan_event(drv, 0, tb, external_scan_event);
2922 		break;
2923 	case NL80211_CMD_SCHED_SCAN_RESULTS:
2924 		wpa_dbg(drv->ctx, MSG_DEBUG,
2925 			"nl80211: New sched scan results available");
2926 		drv->scan_state = SCHED_SCAN_RESULTS;
2927 		send_scan_event(drv, 0, tb, 0);
2928 		break;
2929 	case NL80211_CMD_SCAN_ABORTED:
2930 		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
2931 		if (drv->last_scan_cmd != NL80211_CMD_VENDOR)
2932 			drv->scan_state = SCAN_ABORTED;
2933 		if (drv->last_scan_cmd == NL80211_CMD_TRIGGER_SCAN) {
2934 			/*
2935 			 * Need to indicate that scan results are available in
2936 			 * order not to make wpa_supplicant stop its scanning.
2937 			 */
2938 			eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout,
2939 					     drv, drv->ctx);
2940 			drv->last_scan_cmd = 0;
2941 		} else {
2942 			external_scan_event = 1;
2943 		}
2944 		send_scan_event(drv, 1, tb, external_scan_event);
2945 		break;
2946 	case NL80211_CMD_AUTHENTICATE:
2947 	case NL80211_CMD_ASSOCIATE:
2948 	case NL80211_CMD_DEAUTHENTICATE:
2949 	case NL80211_CMD_DISASSOCIATE:
2950 	case NL80211_CMD_FRAME_TX_STATUS:
2951 	case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2952 	case NL80211_CMD_UNPROT_DISASSOCIATE:
2953 		mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
2954 			   tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2955 			   tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2956 			   tb[NL80211_ATTR_COOKIE],
2957 			   tb[NL80211_ATTR_RX_SIGNAL_DBM],
2958 			   tb[NL80211_ATTR_STA_WME],
2959 			   tb[NL80211_ATTR_REQ_IE]);
2960 		break;
2961 	case NL80211_CMD_CONNECT:
2962 	case NL80211_CMD_ROAM:
2963 		mlme_event_connect(drv, cmd,
2964 				   tb[NL80211_ATTR_STATUS_CODE],
2965 				   tb[NL80211_ATTR_MAC],
2966 				   tb[NL80211_ATTR_REQ_IE],
2967 				   tb[NL80211_ATTR_RESP_IE],
2968 				   tb[NL80211_ATTR_TIMED_OUT],
2969 				   tb[NL80211_ATTR_TIMEOUT_REASON],
2970 				   NULL, NULL, NULL,
2971 				   tb[NL80211_ATTR_FILS_KEK],
2972 				   NULL,
2973 				   tb[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM],
2974 				   tb[NL80211_ATTR_PMK],
2975 				   tb[NL80211_ATTR_PMKID]);
2976 		break;
2977 	case NL80211_CMD_CH_SWITCH_STARTED_NOTIFY:
2978 		mlme_event_ch_switch(drv,
2979 				     tb[NL80211_ATTR_IFINDEX],
2980 				     tb[NL80211_ATTR_WIPHY_FREQ],
2981 				     tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2982 				     tb[NL80211_ATTR_CHANNEL_WIDTH],
2983 				     tb[NL80211_ATTR_CENTER_FREQ1],
2984 				     tb[NL80211_ATTR_CENTER_FREQ2],
2985 				     0);
2986 		break;
2987 	case NL80211_CMD_CH_SWITCH_NOTIFY:
2988 		mlme_event_ch_switch(drv,
2989 				     tb[NL80211_ATTR_IFINDEX],
2990 				     tb[NL80211_ATTR_WIPHY_FREQ],
2991 				     tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2992 				     tb[NL80211_ATTR_CHANNEL_WIDTH],
2993 				     tb[NL80211_ATTR_CENTER_FREQ1],
2994 				     tb[NL80211_ATTR_CENTER_FREQ2],
2995 				     1);
2996 		break;
2997 	case NL80211_CMD_DISCONNECT:
2998 		mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
2999 				      tb[NL80211_ATTR_MAC],
3000 				      tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
3001 		break;
3002 	case NL80211_CMD_MICHAEL_MIC_FAILURE:
3003 		mlme_event_michael_mic_failure(bss, tb);
3004 		break;
3005 	case NL80211_CMD_JOIN_IBSS:
3006 		mlme_event_join_ibss(drv, tb);
3007 		break;
3008 	case NL80211_CMD_REMAIN_ON_CHANNEL:
3009 		mlme_event_remain_on_channel(drv, 0, tb);
3010 		break;
3011 	case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
3012 		mlme_event_remain_on_channel(drv, 1, tb);
3013 		break;
3014 	case NL80211_CMD_NOTIFY_CQM:
3015 		nl80211_cqm_event(drv, tb);
3016 		break;
3017 	case NL80211_CMD_REG_CHANGE:
3018 	case NL80211_CMD_WIPHY_REG_CHANGE:
3019 		nl80211_reg_change_event(drv, tb);
3020 		break;
3021 	case NL80211_CMD_REG_BEACON_HINT:
3022 		nl80211_reg_beacon_hint_event(drv, tb);
3023 		break;
3024 	case NL80211_CMD_NEW_STATION:
3025 		nl80211_new_station_event(drv, bss, tb);
3026 		break;
3027 	case NL80211_CMD_DEL_STATION:
3028 		nl80211_del_station_event(drv, bss, tb);
3029 		break;
3030 	case NL80211_CMD_SET_REKEY_OFFLOAD:
3031 		nl80211_rekey_offload_event(drv, tb);
3032 		break;
3033 	case NL80211_CMD_PMKSA_CANDIDATE:
3034 		nl80211_pmksa_candidate_event(drv, tb);
3035 		break;
3036 	case NL80211_CMD_PROBE_CLIENT:
3037 		nl80211_client_probe_event(drv, tb);
3038 		break;
3039 	case NL80211_CMD_TDLS_OPER:
3040 		nl80211_tdls_oper_event(drv, tb);
3041 		break;
3042 	case NL80211_CMD_CONN_FAILED:
3043 		nl80211_connect_failed_event(drv, tb);
3044 		break;
3045 	case NL80211_CMD_FT_EVENT:
3046 		mlme_event_ft_event(drv, tb);
3047 		break;
3048 	case NL80211_CMD_RADAR_DETECT:
3049 		nl80211_radar_event(drv, tb);
3050 		break;
3051 	case NL80211_CMD_STOP_AP:
3052 		nl80211_stop_ap(drv, tb);
3053 		break;
3054 	case NL80211_CMD_VENDOR:
3055 		nl80211_vendor_event(drv, tb);
3056 		break;
3057 	case NL80211_CMD_NEW_PEER_CANDIDATE:
3058 		nl80211_new_peer_candidate(drv, tb);
3059 		break;
3060 	case NL80211_CMD_PORT_AUTHORIZED:
3061 		nl80211_port_authorized(drv, tb);
3062 		break;
3063 	case NL80211_CMD_STA_OPMODE_CHANGED:
3064 		nl80211_sta_opmode_change_event(drv, tb);
3065 		break;
3066 	case NL80211_CMD_UPDATE_OWE_INFO:
3067 		mlme_event_dh_event(drv, bss, tb);
3068 		break;
3069 	case NL80211_CMD_UNPROT_BEACON:
3070 		if (frame)
3071 			mlme_event_unprot_beacon(drv, nla_data(frame),
3072 						 nla_len(frame));
3073 		break;
3074 	case NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS:
3075 		if (!frame)
3076 			break;
3077 		nl80211_control_port_frame_tx_status(drv,
3078 						     nla_data(frame),
3079 						     nla_len(frame),
3080 						     tb[NL80211_ATTR_ACK],
3081 						     tb[NL80211_ATTR_COOKIE]);
3082 		break;
3083 	default:
3084 		wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
3085 			"(cmd=%d)", cmd);
3086 		break;
3087 	}
3088 }
3089 
3090 
process_global_event(struct nl_msg * msg,void * arg)3091 int process_global_event(struct nl_msg *msg, void *arg)
3092 {
3093 	struct nl80211_global *global = arg;
3094 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3095 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
3096 	struct wpa_driver_nl80211_data *drv, *tmp;
3097 	int ifidx = -1, wiphy_idx = -1, wiphy_idx_rx = -1;
3098 	struct i802_bss *bss;
3099 	u64 wdev_id = 0;
3100 	int wdev_id_set = 0;
3101 	int wiphy_idx_set = 0;
3102 
3103 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3104 		  genlmsg_attrlen(gnlh, 0), NULL);
3105 
3106 	if (tb[NL80211_ATTR_IFINDEX])
3107 		ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3108 	else if (tb[NL80211_ATTR_WDEV]) {
3109 		wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
3110 		wdev_id_set = 1;
3111 	} else if (tb[NL80211_ATTR_WIPHY]) {
3112 		wiphy_idx_rx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
3113 		wiphy_idx_set = 1;
3114 	}
3115 
3116 	dl_list_for_each_safe(drv, tmp, &global->interfaces,
3117 			      struct wpa_driver_nl80211_data, list) {
3118 		for (bss = drv->first_bss; bss; bss = bss->next) {
3119 			if (wiphy_idx_set)
3120 				wiphy_idx = nl80211_get_wiphy_index(bss);
3121 			if ((ifidx == -1 && !wiphy_idx_set && !wdev_id_set) ||
3122 			    ifidx == bss->ifindex ||
3123 			    (wiphy_idx_set && wiphy_idx == wiphy_idx_rx) ||
3124 			    (wdev_id_set && bss->wdev_id_set &&
3125 			     wdev_id == bss->wdev_id)) {
3126 				do_process_drv_event(bss, gnlh->cmd, tb);
3127 				return NL_SKIP;
3128 			}
3129 		}
3130 		wpa_printf(MSG_DEBUG,
3131 			   "nl80211: Ignored event %d (%s) for foreign interface (ifindex %d wdev 0x%llx)",
3132 			   gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3133 			   ifidx, (long long unsigned int) wdev_id);
3134 	}
3135 
3136 	return NL_SKIP;
3137 }
3138 
3139 
process_bss_event(struct nl_msg * msg,void * arg)3140 int process_bss_event(struct nl_msg *msg, void *arg)
3141 {
3142 	struct i802_bss *bss = arg;
3143 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3144 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
3145 
3146 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3147 		  genlmsg_attrlen(gnlh, 0), NULL);
3148 
3149 	wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
3150 		   gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
3151 		   bss->ifname);
3152 
3153 	switch (gnlh->cmd) {
3154 	case NL80211_CMD_FRAME:
3155 	case NL80211_CMD_FRAME_TX_STATUS:
3156 		mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
3157 			   tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
3158 			   tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
3159 			   tb[NL80211_ATTR_COOKIE],
3160 			   tb[NL80211_ATTR_RX_SIGNAL_DBM],
3161 			   tb[NL80211_ATTR_STA_WME], NULL);
3162 		break;
3163 	case NL80211_CMD_UNEXPECTED_FRAME:
3164 		nl80211_spurious_frame(bss, tb, 0);
3165 		break;
3166 	case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
3167 		nl80211_spurious_frame(bss, tb, 1);
3168 		break;
3169 	case NL80211_CMD_EXTERNAL_AUTH:
3170 		nl80211_external_auth(bss->drv, tb);
3171 		break;
3172 	case NL80211_CMD_CONTROL_PORT_FRAME:
3173 		nl80211_control_port_frame(bss->drv, tb);
3174 		break;
3175 	default:
3176 		wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
3177 			   "(cmd=%d)", gnlh->cmd);
3178 		break;
3179 	}
3180 
3181 	return NL_SKIP;
3182 }
3183