1 /*
2  * Driver interaction with Linux nl80211/cfg80211 - Capabilities
3  * Copyright (c) 2002-2015, 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 "common/ieee802_11_common.h"
16 #include "common/wpa_common.h"
17 #include "common/qca-vendor.h"
18 #include "common/qca-vendor-attr.h"
19 #include "driver_nl80211.h"
20 
21 
22 static int protocol_feature_handler(struct nl_msg *msg, void *arg)
23 {
24 	u32 *feat = arg;
25 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
26 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
27 
28 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
29 		  genlmsg_attrlen(gnlh, 0), NULL);
30 
31 	if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
32 		*feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
33 
34 	return NL_SKIP;
35 }
36 
37 
38 static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
39 {
40 	u32 feat = 0;
41 	struct nl_msg *msg;
42 
43 	msg = nlmsg_alloc();
44 	if (!msg)
45 		return 0;
46 
47 	if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES)) {
48 		nlmsg_free(msg);
49 		return 0;
50 	}
51 
52 	if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
53 		return feat;
54 
55 	return 0;
56 }
57 
58 
59 struct wiphy_info_data {
60 	struct wpa_driver_nl80211_data *drv;
61 	struct wpa_driver_capa *capa;
62 
63 	unsigned int num_multichan_concurrent;
64 
65 	unsigned int error:1;
66 	unsigned int device_ap_sme:1;
67 	unsigned int poll_command_supported:1;
68 	unsigned int data_tx_status:1;
69 	unsigned int auth_supported:1;
70 	unsigned int connect_supported:1;
71 	unsigned int p2p_go_supported:1;
72 	unsigned int p2p_client_supported:1;
73 	unsigned int p2p_go_ctwindow_supported:1;
74 	unsigned int p2p_concurrent:1;
75 	unsigned int channel_switch_supported:1;
76 	unsigned int set_qos_map_supported:1;
77 	unsigned int have_low_prio_scan:1;
78 	unsigned int wmm_ac_supported:1;
79 	unsigned int mac_addr_rand_scan_supported:1;
80 	unsigned int mac_addr_rand_sched_scan_supported:1;
81 };
82 
83 
84 static unsigned int probe_resp_offload_support(int supp_protocols)
85 {
86 	unsigned int prot = 0;
87 
88 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
89 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
90 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
91 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
92 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
93 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
94 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
95 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
96 
97 	return prot;
98 }
99 
100 
101 static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
102 					 struct nlattr *tb)
103 {
104 	struct nlattr *nl_mode;
105 	int i;
106 
107 	if (tb == NULL)
108 		return;
109 
110 	nla_for_each_nested(nl_mode, tb, i) {
111 		switch (nla_type(nl_mode)) {
112 		case NL80211_IFTYPE_AP:
113 			info->capa->flags |= WPA_DRIVER_FLAGS_AP;
114 			break;
115 		case NL80211_IFTYPE_MESH_POINT:
116 			info->capa->flags |= WPA_DRIVER_FLAGS_MESH;
117 			break;
118 		case NL80211_IFTYPE_ADHOC:
119 			info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
120 			break;
121 		case NL80211_IFTYPE_P2P_DEVICE:
122 			info->capa->flags |=
123 				WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
124 			break;
125 		case NL80211_IFTYPE_P2P_GO:
126 			info->p2p_go_supported = 1;
127 			break;
128 		case NL80211_IFTYPE_P2P_CLIENT:
129 			info->p2p_client_supported = 1;
130 			break;
131 		}
132 	}
133 }
134 
135 
136 static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
137 					 struct nlattr *nl_combi)
138 {
139 	struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
140 	struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
141 	struct nlattr *nl_limit, *nl_mode;
142 	int err, rem_limit, rem_mode;
143 	int combination_has_p2p = 0, combination_has_mgd = 0;
144 	static struct nla_policy
145 	iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
146 		[NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
147 		[NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
148 		[NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
149 		[NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
150 		[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
151 	},
152 	iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
153 		[NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
154 		[NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
155 	};
156 
157 	err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
158 			       nl_combi, iface_combination_policy);
159 	if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
160 	    !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
161 	    !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
162 		return 0; /* broken combination */
163 
164 	if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
165 		info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
166 
167 	nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
168 			    rem_limit) {
169 		err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
170 				       nl_limit, iface_limit_policy);
171 		if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
172 			return 0; /* broken combination */
173 
174 		nla_for_each_nested(nl_mode,
175 				    tb_limit[NL80211_IFACE_LIMIT_TYPES],
176 				    rem_mode) {
177 			int ift = nla_type(nl_mode);
178 			if (ift == NL80211_IFTYPE_P2P_GO ||
179 			    ift == NL80211_IFTYPE_P2P_CLIENT)
180 				combination_has_p2p = 1;
181 			if (ift == NL80211_IFTYPE_STATION)
182 				combination_has_mgd = 1;
183 		}
184 		if (combination_has_p2p && combination_has_mgd)
185 			break;
186 	}
187 
188 	if (combination_has_p2p && combination_has_mgd) {
189 		unsigned int num_channels =
190 			nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
191 
192 		info->p2p_concurrent = 1;
193 		if (info->num_multichan_concurrent < num_channels)
194 			info->num_multichan_concurrent = num_channels;
195 	}
196 
197 	return 0;
198 }
199 
200 
201 static void wiphy_info_iface_comb(struct wiphy_info_data *info,
202 				  struct nlattr *tb)
203 {
204 	struct nlattr *nl_combi;
205 	int rem_combi;
206 
207 	if (tb == NULL)
208 		return;
209 
210 	nla_for_each_nested(nl_combi, tb, rem_combi) {
211 		if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
212 			break;
213 	}
214 }
215 
216 
217 static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
218 				 struct nlattr *tb)
219 {
220 	struct nlattr *nl_cmd;
221 	int i;
222 
223 	if (tb == NULL)
224 		return;
225 
226 	nla_for_each_nested(nl_cmd, tb, i) {
227 		switch (nla_get_u32(nl_cmd)) {
228 		case NL80211_CMD_AUTHENTICATE:
229 			info->auth_supported = 1;
230 			break;
231 		case NL80211_CMD_CONNECT:
232 			info->connect_supported = 1;
233 			break;
234 		case NL80211_CMD_START_SCHED_SCAN:
235 			info->capa->sched_scan_supported = 1;
236 			break;
237 		case NL80211_CMD_PROBE_CLIENT:
238 			info->poll_command_supported = 1;
239 			break;
240 		case NL80211_CMD_CHANNEL_SWITCH:
241 			info->channel_switch_supported = 1;
242 			break;
243 		case NL80211_CMD_SET_QOS_MAP:
244 			info->set_qos_map_supported = 1;
245 			break;
246 		}
247 	}
248 }
249 
250 
251 static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
252 				     struct nlattr *tb)
253 {
254 	int i, num;
255 	u32 *ciphers;
256 
257 	if (tb == NULL)
258 		return;
259 
260 	num = nla_len(tb) / sizeof(u32);
261 	ciphers = nla_data(tb);
262 	for (i = 0; i < num; i++) {
263 		u32 c = ciphers[i];
264 
265 		wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
266 			   c >> 24, (c >> 16) & 0xff,
267 			   (c >> 8) & 0xff, c & 0xff);
268 		switch (c) {
269 		case RSN_CIPHER_SUITE_CCMP_256:
270 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
271 			break;
272 		case RSN_CIPHER_SUITE_GCMP_256:
273 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
274 			break;
275 		case RSN_CIPHER_SUITE_CCMP:
276 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
277 			break;
278 		case RSN_CIPHER_SUITE_GCMP:
279 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
280 			break;
281 		case RSN_CIPHER_SUITE_TKIP:
282 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
283 			break;
284 		case RSN_CIPHER_SUITE_WEP104:
285 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
286 			break;
287 		case RSN_CIPHER_SUITE_WEP40:
288 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
289 			break;
290 		case RSN_CIPHER_SUITE_AES_128_CMAC:
291 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
292 			break;
293 		case RSN_CIPHER_SUITE_BIP_GMAC_128:
294 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
295 			break;
296 		case RSN_CIPHER_SUITE_BIP_GMAC_256:
297 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
298 			break;
299 		case RSN_CIPHER_SUITE_BIP_CMAC_256:
300 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
301 			break;
302 		case RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED:
303 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
304 			break;
305 		}
306 	}
307 }
308 
309 
310 static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
311 			       struct nlattr *tb)
312 {
313 	if (tb)
314 		capa->max_remain_on_chan = nla_get_u32(tb);
315 }
316 
317 
318 static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
319 			    struct nlattr *ext_setup)
320 {
321 	if (tdls == NULL)
322 		return;
323 
324 	wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
325 	capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
326 
327 	if (ext_setup) {
328 		wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
329 		capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
330 	}
331 }
332 
333 
334 static int ext_feature_isset(const u8 *ext_features, int ext_features_len,
335 			     enum nl80211_ext_feature_index ftidx)
336 {
337 	u8 ft_byte;
338 
339 	if ((int) ftidx / 8 >= ext_features_len)
340 		return 0;
341 
342 	ft_byte = ext_features[ftidx / 8];
343 	return (ft_byte & BIT(ftidx % 8)) != 0;
344 }
345 
346 
347 static void wiphy_info_ext_feature_flags(struct wiphy_info_data *info,
348 					 struct nlattr *tb)
349 {
350 	struct wpa_driver_capa *capa = info->capa;
351 	u8 *ext_features;
352 	int len;
353 
354 	if (tb == NULL)
355 		return;
356 
357 	ext_features = nla_data(tb);
358 	len = nla_len(tb);
359 
360 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_VHT_IBSS))
361 		capa->flags |= WPA_DRIVER_FLAGS_VHT_IBSS;
362 
363 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_RRM))
364 		capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_RRM;
365 
366 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_FILS_STA))
367 		capa->flags |= WPA_DRIVER_FLAGS_SUPPORT_FILS;
368 
369 	if (ext_feature_isset(ext_features, len,
370 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
371 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY;
372 
373 	if (ext_feature_isset(ext_features, len,
374 			      NL80211_EXT_FEATURE_BEACON_RATE_HT))
375 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_RATE_HT;
376 
377 	if (ext_feature_isset(ext_features, len,
378 			      NL80211_EXT_FEATURE_BEACON_RATE_VHT))
379 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_RATE_VHT;
380 
381 	if (ext_feature_isset(ext_features, len,
382 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL))
383 		capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL;
384 
385 	if (ext_feature_isset(ext_features, len,
386 			      NL80211_EXT_FEATURE_SCAN_START_TIME) &&
387 	    ext_feature_isset(ext_features, len,
388 			      NL80211_EXT_FEATURE_BSS_PARENT_TSF) &&
389 	    ext_feature_isset(ext_features, len,
390 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL))
391 		capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT;
392 	if (ext_feature_isset(ext_features, len,
393 			      NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA))
394 		capa->flags |= WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA;
395 	if (ext_feature_isset(ext_features, len,
396 			      NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED))
397 		capa->flags |= WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED;
398 	if (ext_feature_isset(ext_features, len,
399 			      NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI))
400 		capa->flags |= WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI;
401 	if (ext_feature_isset(ext_features, len,
402 			      NL80211_EXT_FEATURE_FILS_SK_OFFLOAD))
403 		capa->flags |= WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD;
404 
405 	if (ext_feature_isset(ext_features, len,
406 			      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
407 	    ext_feature_isset(ext_features, len,
408 			      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
409 		capa->flags |= WPA_DRIVER_FLAGS_4WAY_HANDSHAKE;
410 
411 	if (ext_feature_isset(ext_features, len,
412 			      NL80211_EXT_FEATURE_MFP_OPTIONAL))
413 		capa->flags |= WPA_DRIVER_FLAGS_MFP_OPTIONAL;
414 
415 	if (ext_feature_isset(ext_features, len,
416 			      NL80211_EXT_FEATURE_DFS_OFFLOAD))
417 		capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
418 
419 #ifdef CONFIG_MBO
420 	if (ext_feature_isset(ext_features, len,
421 			      NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) &&
422 	    ext_feature_isset(ext_features, len,
423 			      NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) &&
424 	    ext_feature_isset(ext_features, len,
425 			      NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) &&
426 	    ext_feature_isset(
427 		    ext_features, len,
428 		    NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION))
429 		capa->flags |= WPA_DRIVER_FLAGS_OCE_STA;
430 #endif /* CONFIG_MBO */
431 }
432 
433 
434 static void wiphy_info_feature_flags(struct wiphy_info_data *info,
435 				     struct nlattr *tb)
436 {
437 	u32 flags;
438 	struct wpa_driver_capa *capa = info->capa;
439 
440 	if (tb == NULL)
441 		return;
442 
443 	flags = nla_get_u32(tb);
444 
445 	if (flags & NL80211_FEATURE_SK_TX_STATUS)
446 		info->data_tx_status = 1;
447 
448 	if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
449 		capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
450 
451 	if (flags & NL80211_FEATURE_SAE)
452 		capa->flags |= WPA_DRIVER_FLAGS_SAE;
453 
454 	if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
455 		capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
456 
457 	if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
458 		capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
459 
460 	if (flags & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) {
461 		wpa_printf(MSG_DEBUG, "nl80211: TDLS channel switch");
462 		capa->flags |= WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH;
463 	}
464 
465 	if (flags & NL80211_FEATURE_P2P_GO_CTWIN)
466 		info->p2p_go_ctwindow_supported = 1;
467 
468 	if (flags & NL80211_FEATURE_LOW_PRIORITY_SCAN)
469 		info->have_low_prio_scan = 1;
470 
471 	if (flags & NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)
472 		info->mac_addr_rand_scan_supported = 1;
473 
474 	if (flags & NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR)
475 		info->mac_addr_rand_sched_scan_supported = 1;
476 
477 	if (flags & NL80211_FEATURE_STATIC_SMPS)
478 		capa->smps_modes |= WPA_DRIVER_SMPS_MODE_STATIC;
479 
480 	if (flags & NL80211_FEATURE_DYNAMIC_SMPS)
481 		capa->smps_modes |= WPA_DRIVER_SMPS_MODE_DYNAMIC;
482 
483 	if (flags & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
484 		info->wmm_ac_supported = 1;
485 
486 	if (flags & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES)
487 		capa->rrm_flags |= WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES;
488 
489 	if (flags & NL80211_FEATURE_WFA_TPC_IE_IN_PROBES)
490 		capa->rrm_flags |= WPA_DRIVER_FLAGS_WFA_TPC_IE_IN_PROBES;
491 
492 	if (flags & NL80211_FEATURE_QUIET)
493 		capa->rrm_flags |= WPA_DRIVER_FLAGS_QUIET;
494 
495 	if (flags & NL80211_FEATURE_TX_POWER_INSERTION)
496 		capa->rrm_flags |= WPA_DRIVER_FLAGS_TX_POWER_INSERTION;
497 
498 	if (flags & NL80211_FEATURE_HT_IBSS)
499 		capa->flags |= WPA_DRIVER_FLAGS_HT_IBSS;
500 
501 	if (flags & NL80211_FEATURE_FULL_AP_CLIENT_STATE)
502 		capa->flags |= WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
503 }
504 
505 
506 static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
507 					  struct nlattr *tb)
508 {
509 	u32 protocols;
510 
511 	if (tb == NULL)
512 		return;
513 
514 	protocols = nla_get_u32(tb);
515 	wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
516 		   "mode");
517 	capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
518 	capa->probe_resp_offloads = probe_resp_offload_support(protocols);
519 }
520 
521 
522 static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
523 				       struct nlattr *tb)
524 {
525 	struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
526 
527 	if (tb == NULL)
528 		return;
529 
530 	if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
531 			     tb, NULL))
532 		return;
533 
534 	if (triggers[NL80211_WOWLAN_TRIG_ANY])
535 		capa->wowlan_triggers.any = 1;
536 	if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
537 		capa->wowlan_triggers.disconnect = 1;
538 	if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
539 		capa->wowlan_triggers.magic_pkt = 1;
540 	if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
541 		capa->wowlan_triggers.gtk_rekey_failure = 1;
542 	if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
543 		capa->wowlan_triggers.eap_identity_req = 1;
544 	if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
545 		capa->wowlan_triggers.four_way_handshake = 1;
546 	if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
547 		capa->wowlan_triggers.rfkill_release = 1;
548 }
549 
550 
551 static void wiphy_info_extended_capab(struct wpa_driver_nl80211_data *drv,
552 				      struct nlattr *tb)
553 {
554 	int rem = 0, i;
555 	struct nlattr *tb1[NL80211_ATTR_MAX + 1], *attr;
556 
557 	if (!tb || drv->num_iface_ext_capa == NL80211_IFTYPE_MAX)
558 		return;
559 
560 	nla_for_each_nested(attr, tb, rem) {
561 		unsigned int len;
562 		struct drv_nl80211_ext_capa *capa;
563 
564 		nla_parse(tb1, NL80211_ATTR_MAX, nla_data(attr),
565 			  nla_len(attr), NULL);
566 
567 		if (!tb1[NL80211_ATTR_IFTYPE] ||
568 		    !tb1[NL80211_ATTR_EXT_CAPA] ||
569 		    !tb1[NL80211_ATTR_EXT_CAPA_MASK])
570 			continue;
571 
572 		capa = &drv->iface_ext_capa[drv->num_iface_ext_capa];
573 		capa->iftype = nla_get_u32(tb1[NL80211_ATTR_IFTYPE]);
574 		wpa_printf(MSG_DEBUG,
575 			   "nl80211: Driver-advertised extended capabilities for interface type %s",
576 			   nl80211_iftype_str(capa->iftype));
577 
578 		len = nla_len(tb1[NL80211_ATTR_EXT_CAPA]);
579 		capa->ext_capa = os_memdup(nla_data(tb1[NL80211_ATTR_EXT_CAPA]),
580 					   len);
581 		if (!capa->ext_capa)
582 			goto err;
583 
584 		capa->ext_capa_len = len;
585 		wpa_hexdump(MSG_DEBUG, "nl80211: Extended capabilities",
586 			    capa->ext_capa, capa->ext_capa_len);
587 
588 		len = nla_len(tb1[NL80211_ATTR_EXT_CAPA_MASK]);
589 		capa->ext_capa_mask =
590 			os_memdup(nla_data(tb1[NL80211_ATTR_EXT_CAPA_MASK]),
591 				  len);
592 		if (!capa->ext_capa_mask)
593 			goto err;
594 
595 		wpa_hexdump(MSG_DEBUG, "nl80211: Extended capabilities mask",
596 			    capa->ext_capa_mask, capa->ext_capa_len);
597 
598 		drv->num_iface_ext_capa++;
599 		if (drv->num_iface_ext_capa == NL80211_IFTYPE_MAX)
600 			break;
601 	}
602 
603 	return;
604 
605 err:
606 	/* Cleanup allocated memory on error */
607 	for (i = 0; i < NL80211_IFTYPE_MAX; i++) {
608 		os_free(drv->iface_ext_capa[i].ext_capa);
609 		drv->iface_ext_capa[i].ext_capa = NULL;
610 		os_free(drv->iface_ext_capa[i].ext_capa_mask);
611 		drv->iface_ext_capa[i].ext_capa_mask = NULL;
612 		drv->iface_ext_capa[i].ext_capa_len = 0;
613 	}
614 	drv->num_iface_ext_capa = 0;
615 }
616 
617 
618 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
619 {
620 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
621 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
622 	struct wiphy_info_data *info = arg;
623 	struct wpa_driver_capa *capa = info->capa;
624 	struct wpa_driver_nl80211_data *drv = info->drv;
625 
626 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
627 		  genlmsg_attrlen(gnlh, 0), NULL);
628 
629 	if (tb[NL80211_ATTR_WIPHY])
630 		drv->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
631 
632 	if (tb[NL80211_ATTR_WIPHY_NAME])
633 		os_strlcpy(drv->phyname,
634 			   nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
635 			   sizeof(drv->phyname));
636 	if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
637 		capa->max_scan_ssids =
638 			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
639 
640 	if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
641 		capa->max_sched_scan_ssids =
642 			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
643 
644 	if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS] &&
645 	    tb[NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL] &&
646 	    tb[NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS]) {
647 		capa->max_sched_scan_plans =
648 			nla_get_u32(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS]);
649 
650 		capa->max_sched_scan_plan_interval =
651 			nla_get_u32(tb[NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL]);
652 
653 		capa->max_sched_scan_plan_iterations =
654 			nla_get_u32(tb[NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS]);
655 	}
656 
657 	if (tb[NL80211_ATTR_MAX_MATCH_SETS])
658 		capa->max_match_sets =
659 			nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
660 
661 	if (tb[NL80211_ATTR_MAC_ACL_MAX])
662 		capa->max_acl_mac_addrs =
663 			nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
664 
665 	wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
666 	wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
667 	wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
668 	wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
669 
670 	if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
671 		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
672 			   "off-channel TX");
673 		capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
674 	}
675 
676 	if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
677 		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
678 		capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
679 	}
680 
681 	wiphy_info_max_roc(capa,
682 			   tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
683 
684 	if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
685 		capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
686 
687 	wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
688 			tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
689 
690 	if (tb[NL80211_ATTR_DEVICE_AP_SME])
691 		info->device_ap_sme = 1;
692 
693 	wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
694 	wiphy_info_ext_feature_flags(info, tb[NL80211_ATTR_EXT_FEATURES]);
695 	wiphy_info_probe_resp_offload(capa,
696 				      tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
697 
698 	if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
699 	    drv->extended_capa == NULL) {
700 		drv->extended_capa =
701 			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
702 		if (drv->extended_capa) {
703 			os_memcpy(drv->extended_capa,
704 				  nla_data(tb[NL80211_ATTR_EXT_CAPA]),
705 				  nla_len(tb[NL80211_ATTR_EXT_CAPA]));
706 			drv->extended_capa_len =
707 				nla_len(tb[NL80211_ATTR_EXT_CAPA]);
708 			wpa_hexdump(MSG_DEBUG,
709 				    "nl80211: Driver-advertised extended capabilities (default)",
710 				    drv->extended_capa, drv->extended_capa_len);
711 		}
712 		drv->extended_capa_mask =
713 			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA_MASK]));
714 		if (drv->extended_capa_mask) {
715 			os_memcpy(drv->extended_capa_mask,
716 				  nla_data(tb[NL80211_ATTR_EXT_CAPA_MASK]),
717 				  nla_len(tb[NL80211_ATTR_EXT_CAPA_MASK]));
718 			wpa_hexdump(MSG_DEBUG,
719 				    "nl80211: Driver-advertised extended capabilities mask (default)",
720 				    drv->extended_capa_mask,
721 				    drv->extended_capa_len);
722 		} else {
723 			os_free(drv->extended_capa);
724 			drv->extended_capa = NULL;
725 			drv->extended_capa_len = 0;
726 		}
727 	}
728 
729 	wiphy_info_extended_capab(drv, tb[NL80211_ATTR_IFTYPE_EXT_CAPA]);
730 
731 	if (tb[NL80211_ATTR_VENDOR_DATA]) {
732 		struct nlattr *nl;
733 		int rem;
734 
735 		nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
736 			struct nl80211_vendor_cmd_info *vinfo;
737 			if (nla_len(nl) != sizeof(*vinfo)) {
738 				wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
739 				continue;
740 			}
741 			vinfo = nla_data(nl);
742 			if (vinfo->vendor_id == OUI_QCA) {
743 				switch (vinfo->subcmd) {
744 				case QCA_NL80211_VENDOR_SUBCMD_TEST:
745 					drv->vendor_cmd_test_avail = 1;
746 					break;
747 #ifdef CONFIG_DRIVER_NL80211_QCA
748 				case QCA_NL80211_VENDOR_SUBCMD_ROAMING:
749 					drv->roaming_vendor_cmd_avail = 1;
750 					break;
751 				case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
752 					drv->dfs_vendor_cmd_avail = 1;
753 					break;
754 				case QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES:
755 					drv->get_features_vendor_cmd_avail = 1;
756 					break;
757 				case QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST:
758 					drv->get_pref_freq_list = 1;
759 					break;
760 				case QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL:
761 					drv->set_prob_oper_freq = 1;
762 					break;
763 				case QCA_NL80211_VENDOR_SUBCMD_DO_ACS:
764 					drv->capa.flags |=
765 						WPA_DRIVER_FLAGS_ACS_OFFLOAD;
766 					break;
767 				case QCA_NL80211_VENDOR_SUBCMD_SETBAND:
768 					drv->setband_vendor_cmd_avail = 1;
769 					break;
770 				case QCA_NL80211_VENDOR_SUBCMD_TRIGGER_SCAN:
771 					drv->scan_vendor_cmd_avail = 1;
772 					break;
773 				case QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION:
774 					drv->set_wifi_conf_vendor_cmd_avail = 1;
775 					break;
776 				case QCA_NL80211_VENDOR_SUBCMD_GET_HE_CAPABILITIES:
777 					drv->he_capab_vendor_cmd_avail = 1;
778 					break;
779 				case QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS:
780 					drv->fetch_bss_trans_status = 1;
781 					break;
782 				case QCA_NL80211_VENDOR_SUBCMD_ROAM:
783 					drv->roam_vendor_cmd_avail = 1;
784 					break;
785 #endif /* CONFIG_DRIVER_NL80211_QCA */
786 				}
787 			}
788 
789 			wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
790 				   vinfo->vendor_id, vinfo->subcmd);
791 		}
792 	}
793 
794 	if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
795 		struct nlattr *nl;
796 		int rem;
797 
798 		nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
799 			struct nl80211_vendor_cmd_info *vinfo;
800 			if (nla_len(nl) != sizeof(*vinfo)) {
801 				wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
802 				continue;
803 			}
804 			vinfo = nla_data(nl);
805 			wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
806 				   vinfo->vendor_id, vinfo->subcmd);
807 		}
808 	}
809 
810 	wiphy_info_wowlan_triggers(capa,
811 				   tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
812 
813 	if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
814 		capa->max_stations =
815 			nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
816 
817 	if (tb[NL80211_ATTR_MAX_CSA_COUNTERS])
818 		capa->max_csa_counters =
819 			nla_get_u8(tb[NL80211_ATTR_MAX_CSA_COUNTERS]);
820 
821 	if (tb[NL80211_ATTR_WIPHY_SELF_MANAGED_REG])
822 		capa->flags |= WPA_DRIVER_FLAGS_SELF_MANAGED_REGULATORY;
823 
824 	return NL_SKIP;
825 }
826 
827 
828 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
829 				       struct wiphy_info_data *info)
830 {
831 	u32 feat;
832 	struct nl_msg *msg;
833 	int flags = 0;
834 
835 	os_memset(info, 0, sizeof(*info));
836 	info->capa = &drv->capa;
837 	info->drv = drv;
838 
839 	feat = get_nl80211_protocol_features(drv);
840 	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
841 		flags = NLM_F_DUMP;
842 	msg = nl80211_cmd_msg(drv->first_bss, flags, NL80211_CMD_GET_WIPHY);
843 	if (!msg || nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
844 		nlmsg_free(msg);
845 		return -1;
846 	}
847 
848 	if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
849 		return -1;
850 
851 	if (info->auth_supported)
852 		drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
853 	else if (!info->connect_supported) {
854 		wpa_printf(MSG_INFO, "nl80211: Driver does not support "
855 			   "authentication/association or connect commands");
856 		info->error = 1;
857 	}
858 
859 	if (info->p2p_go_supported && info->p2p_client_supported)
860 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
861 	if (info->p2p_concurrent) {
862 		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
863 			   "interface (driver advertised support)");
864 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
865 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
866 	}
867 	if (info->num_multichan_concurrent > 1) {
868 		wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
869 			   "concurrent (driver advertised support)");
870 		drv->capa.num_multichan_concurrent =
871 			info->num_multichan_concurrent;
872 	}
873 	if (drv->capa.flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
874 		wpa_printf(MSG_DEBUG, "nl80211: use P2P_DEVICE support");
875 
876 	/* default to 5000 since early versions of mac80211 don't set it */
877 	if (!drv->capa.max_remain_on_chan)
878 		drv->capa.max_remain_on_chan = 5000;
879 
880 	drv->capa.wmm_ac_supported = info->wmm_ac_supported;
881 
882 	drv->capa.mac_addr_rand_sched_scan_supported =
883 		info->mac_addr_rand_sched_scan_supported;
884 	drv->capa.mac_addr_rand_scan_supported =
885 		info->mac_addr_rand_scan_supported;
886 
887 	if (info->channel_switch_supported) {
888 		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
889 		if (!drv->capa.max_csa_counters)
890 			drv->capa.max_csa_counters = 1;
891 	}
892 
893 	if (!drv->capa.max_sched_scan_plans) {
894 		drv->capa.max_sched_scan_plans = 1;
895 		drv->capa.max_sched_scan_plan_interval = UINT32_MAX;
896 		drv->capa.max_sched_scan_plan_iterations = 0;
897 	}
898 
899 	return 0;
900 }
901 
902 
903 #ifdef CONFIG_DRIVER_NL80211_QCA
904 
905 static int dfs_info_handler(struct nl_msg *msg, void *arg)
906 {
907 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
908 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
909 	int *dfs_capability_ptr = arg;
910 
911 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
912 		  genlmsg_attrlen(gnlh, 0), NULL);
913 
914 	if (tb[NL80211_ATTR_VENDOR_DATA]) {
915 		struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
916 		struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
917 
918 		nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
919 			  nla_data(nl_vend), nla_len(nl_vend), NULL);
920 
921 		if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
922 			u32 val;
923 			val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
924 			wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
925 				   val);
926 			*dfs_capability_ptr = val;
927 		}
928 	}
929 
930 	return NL_SKIP;
931 }
932 
933 
934 static void qca_nl80211_check_dfs_capa(struct wpa_driver_nl80211_data *drv)
935 {
936 	struct nl_msg *msg;
937 	int dfs_capability = 0;
938 	int ret;
939 
940 	if (!drv->dfs_vendor_cmd_avail)
941 		return;
942 
943 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
944 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
945 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
946 			QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)) {
947 		nlmsg_free(msg);
948 		return;
949 	}
950 
951 	ret = send_and_recv_msgs(drv, msg, dfs_info_handler, &dfs_capability);
952 	if (!ret && dfs_capability)
953 		drv->capa.flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
954 }
955 
956 
957 static int qca_nl80211_he_capab_handler(struct nl_msg *msg, void *arg)
958 {
959 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
960 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
961 	struct he_capabilities *he_capab = arg;
962 	struct nlattr *nl_vend;
963 	struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_HE_CAPABILITIES_MAX + 1];
964 	size_t len;
965 
966 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
967 		  genlmsg_attrlen(gnlh, 0), NULL);
968 
969 	if (!tb[NL80211_ATTR_VENDOR_DATA])
970 		return NL_SKIP;
971 
972 	nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
973 	nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_HE_CAPABILITIES_MAX,
974 		  nla_data(nl_vend), nla_len(nl_vend), NULL);
975 
976 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_HE_SUPPORTED]) {
977 		u8 he_supported;
978 
979 		he_supported = nla_get_u8(
980 			tb_vendor[QCA_WLAN_VENDOR_ATTR_HE_SUPPORTED]);
981 		wpa_printf(MSG_DEBUG, "nl80211: HE capabilities supported: %u",
982 			   he_supported);
983 		he_capab->he_supported = he_supported;
984 		if (!he_supported)
985 			return NL_SKIP;
986 	}
987 
988 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_PHY_CAPAB]) {
989 		len = nla_len(tb_vendor[QCA_WLAN_VENDOR_ATTR_PHY_CAPAB]);
990 
991 		if (len > sizeof(he_capab->phy_cap))
992 			len = sizeof(he_capab->phy_cap);
993 		os_memcpy(he_capab->phy_cap,
994 			  nla_data(tb_vendor[QCA_WLAN_VENDOR_ATTR_PHY_CAPAB]),
995 			  len);
996 	}
997 
998 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_MAC_CAPAB])
999 		he_capab->mac_cap =
1000 			nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_MAC_CAPAB]);
1001 
1002 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_HE_MCS])
1003 		he_capab->mcs =
1004 			nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_HE_MCS]);
1005 
1006 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_NUM_SS])
1007 		he_capab->ppet.numss_m1 =
1008 			nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_NUM_SS]);
1009 
1010 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_RU_IDX_MASK])
1011 		he_capab->ppet.ru_count =
1012 			nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_RU_IDX_MASK]);
1013 
1014 	if (tb_vendor[QCA_WLAN_VENDOR_ATTR_PPE_THRESHOLD]) {
1015 		len = nla_len(tb_vendor[QCA_WLAN_VENDOR_ATTR_PPE_THRESHOLD]);
1016 
1017 		if (len > sizeof(he_capab->ppet.ppet16_ppet8_ru3_ru0))
1018 			len = sizeof(he_capab->ppet.ppet16_ppet8_ru3_ru0);
1019 		os_memcpy(he_capab->ppet.ppet16_ppet8_ru3_ru0,
1020 			  nla_data(tb_vendor[QCA_WLAN_VENDOR_ATTR_PPE_THRESHOLD]),
1021 			  len);
1022 	}
1023 
1024 	return NL_SKIP;
1025 }
1026 
1027 
1028 static void qca_nl80211_check_he_capab(struct wpa_driver_nl80211_data *drv)
1029 {
1030 	struct nl_msg *msg;
1031 	int ret;
1032 
1033 	if (!drv->he_capab_vendor_cmd_avail)
1034 		return;
1035 
1036 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
1037 		nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
1038 		nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
1039 			    QCA_NL80211_VENDOR_SUBCMD_GET_HE_CAPABILITIES)) {
1040 		nlmsg_free(msg);
1041 		return;
1042 	}
1043 
1044 	ret = send_and_recv_msgs(drv, msg, qca_nl80211_he_capab_handler,
1045 				 &drv->he_capab);
1046 	if (!ret && drv->he_capab.he_supported)
1047 		drv->capa.flags |= WPA_DRIVER_FLAGS_HE_CAPABILITIES;
1048 }
1049 
1050 
1051 struct features_info {
1052 	u8 *flags;
1053 	size_t flags_len;
1054 	struct wpa_driver_capa *capa;
1055 };
1056 
1057 
1058 static int features_info_handler(struct nl_msg *msg, void *arg)
1059 {
1060 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1061 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1062 	struct features_info *info = arg;
1063 	struct nlattr *nl_vend, *attr;
1064 
1065 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1066 		  genlmsg_attrlen(gnlh, 0), NULL);
1067 
1068 	nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
1069 	if (nl_vend) {
1070 		struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
1071 
1072 		nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
1073 			  nla_data(nl_vend), nla_len(nl_vend), NULL);
1074 
1075 		attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_FEATURE_FLAGS];
1076 		if (attr) {
1077 			int len = nla_len(attr);
1078 			info->flags = os_malloc(len);
1079 			if (info->flags != NULL) {
1080 				os_memcpy(info->flags, nla_data(attr), len);
1081 				info->flags_len = len;
1082 			}
1083 		}
1084 		attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_CONCURRENCY_CAPA];
1085 		if (attr)
1086 			info->capa->conc_capab = nla_get_u32(attr);
1087 
1088 		attr = tb_vendor[
1089 			QCA_WLAN_VENDOR_ATTR_MAX_CONCURRENT_CHANNELS_2_4_BAND];
1090 		if (attr)
1091 			info->capa->max_conc_chan_2_4 = nla_get_u32(attr);
1092 
1093 		attr = tb_vendor[
1094 			QCA_WLAN_VENDOR_ATTR_MAX_CONCURRENT_CHANNELS_5_0_BAND];
1095 		if (attr)
1096 			info->capa->max_conc_chan_5_0 = nla_get_u32(attr);
1097 	}
1098 
1099 	return NL_SKIP;
1100 }
1101 
1102 
1103 static int check_feature(enum qca_wlan_vendor_features feature,
1104 			 struct features_info *info)
1105 {
1106 	size_t idx = feature / 8;
1107 
1108 	return (idx < info->flags_len) &&
1109 		(info->flags[idx] & BIT(feature % 8));
1110 }
1111 
1112 
1113 static void qca_nl80211_get_features(struct wpa_driver_nl80211_data *drv)
1114 {
1115 	struct nl_msg *msg;
1116 	struct features_info info;
1117 	int ret;
1118 
1119 	if (!drv->get_features_vendor_cmd_avail)
1120 		return;
1121 
1122 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
1123 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
1124 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
1125 			QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES)) {
1126 		nlmsg_free(msg);
1127 		return;
1128 	}
1129 
1130 	os_memset(&info, 0, sizeof(info));
1131 	info.capa = &drv->capa;
1132 	ret = send_and_recv_msgs(drv, msg, features_info_handler, &info);
1133 	if (ret || !info.flags)
1134 		return;
1135 
1136 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_KEY_MGMT_OFFLOAD, &info))
1137 		drv->capa.flags |= WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD;
1138 
1139 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SUPPORT_HW_MODE_ANY, &info))
1140 		drv->capa.flags |= WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY;
1141 
1142 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OFFCHANNEL_SIMULTANEOUS,
1143 			  &info))
1144 		drv->capa.flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS;
1145 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_P2P_LISTEN_OFFLOAD, &info))
1146 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD;
1147 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OCE_STA, &info))
1148 		drv->capa.flags |= WPA_DRIVER_FLAGS_OCE_STA;
1149 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OCE_AP, &info))
1150 		drv->capa.flags |= WPA_DRIVER_FLAGS_OCE_AP;
1151 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OCE_STA_CFON, &info))
1152 		drv->capa.flags |= WPA_DRIVER_FLAGS_OCE_STA_CFON;
1153 	os_free(info.flags);
1154 }
1155 
1156 #endif /* CONFIG_DRIVER_NL80211_QCA */
1157 
1158 
1159 int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
1160 {
1161 	struct wiphy_info_data info;
1162 	if (wpa_driver_nl80211_get_info(drv, &info))
1163 		return -1;
1164 
1165 	if (info.error)
1166 		return -1;
1167 
1168 	drv->has_capability = 1;
1169 	drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1170 		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1171 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1172 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
1173 		WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B |
1174 		WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192 |
1175 		WPA_DRIVER_CAPA_KEY_MGMT_OWE |
1176 		WPA_DRIVER_CAPA_KEY_MGMT_DPP;
1177 
1178 	if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
1179 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256 |
1180 			WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384 |
1181 			WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256 |
1182 			WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384;
1183 	else if (drv->capa.flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)
1184 		drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256 |
1185 			WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384;
1186 
1187 	drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1188 		WPA_DRIVER_AUTH_SHARED |
1189 		WPA_DRIVER_AUTH_LEAP;
1190 
1191 	drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
1192 	drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
1193 	drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1194 
1195 	/*
1196 	 * As all cfg80211 drivers must support cases where the AP interface is
1197 	 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
1198 	 * case that the user space daemon has crashed, they must be able to
1199 	 * cleanup all stations and key entries in the AP tear down flow. Thus,
1200 	 * this flag can/should always be set for cfg80211 drivers.
1201 	 */
1202 	drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
1203 
1204 	if (!info.device_ap_sme) {
1205 		drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
1206 
1207 		/*
1208 		 * No AP SME is currently assumed to also indicate no AP MLME
1209 		 * in the driver/firmware.
1210 		 */
1211 		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
1212 	}
1213 
1214 	drv->device_ap_sme = info.device_ap_sme;
1215 	drv->poll_command_supported = info.poll_command_supported;
1216 	drv->data_tx_status = info.data_tx_status;
1217 	drv->p2p_go_ctwindow_supported = info.p2p_go_ctwindow_supported;
1218 	if (info.set_qos_map_supported)
1219 		drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
1220 	drv->have_low_prio_scan = info.have_low_prio_scan;
1221 
1222 	/*
1223 	 * If poll command and tx status are supported, mac80211 is new enough
1224 	 * to have everything we need to not need monitor interfaces.
1225 	 */
1226 	drv->use_monitor = !info.device_ap_sme &&
1227 		(!info.poll_command_supported || !info.data_tx_status);
1228 
1229 	/*
1230 	 * If we aren't going to use monitor interfaces, but the
1231 	 * driver doesn't support data TX status, we won't get TX
1232 	 * status for EAPOL frames.
1233 	 */
1234 	if (!drv->use_monitor && !info.data_tx_status)
1235 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1236 
1237 #ifdef CONFIG_DRIVER_NL80211_QCA
1238 	if (!(info.capa->flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD))
1239 		qca_nl80211_check_dfs_capa(drv);
1240 	qca_nl80211_get_features(drv);
1241 	qca_nl80211_check_he_capab(drv);
1242 
1243 	/*
1244 	 * To enable offchannel simultaneous support in wpa_supplicant, the
1245 	 * underlying driver needs to support the same along with offchannel TX.
1246 	 * Offchannel TX support is needed since remain_on_channel and
1247 	 * action_tx use some common data structures and hence cannot be
1248 	 * scheduled simultaneously.
1249 	 */
1250 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
1251 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS;
1252 #endif /* CONFIG_DRIVER_NL80211_QCA */
1253 
1254 	return 0;
1255 }
1256 
1257 
1258 struct phy_info_arg {
1259 	u16 *num_modes;
1260 	struct hostapd_hw_modes *modes;
1261 	int last_mode, last_chan_idx;
1262 	int failed;
1263 	u8 dfs_domain;
1264 };
1265 
1266 static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
1267 			     struct nlattr *ampdu_factor,
1268 			     struct nlattr *ampdu_density,
1269 			     struct nlattr *mcs_set)
1270 {
1271 	if (capa)
1272 		mode->ht_capab = nla_get_u16(capa);
1273 
1274 	if (ampdu_factor)
1275 		mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
1276 
1277 	if (ampdu_density)
1278 		mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
1279 
1280 	if (mcs_set && nla_len(mcs_set) >= 16) {
1281 		u8 *mcs;
1282 		mcs = nla_data(mcs_set);
1283 		os_memcpy(mode->mcs_set, mcs, 16);
1284 	}
1285 }
1286 
1287 
1288 static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
1289 			      struct nlattr *capa,
1290 			      struct nlattr *mcs_set)
1291 {
1292 	if (capa)
1293 		mode->vht_capab = nla_get_u32(capa);
1294 
1295 	if (mcs_set && nla_len(mcs_set) >= 8) {
1296 		u8 *mcs;
1297 		mcs = nla_data(mcs_set);
1298 		os_memcpy(mode->vht_mcs_set, mcs, 8);
1299 	}
1300 }
1301 
1302 
1303 static void phy_info_freq(struct hostapd_hw_modes *mode,
1304 			  struct hostapd_channel_data *chan,
1305 			  struct nlattr *tb_freq[])
1306 {
1307 	u8 channel;
1308 	chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1309 	chan->flag = 0;
1310 	chan->dfs_cac_ms = 0;
1311 	if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
1312 		chan->chan = channel;
1313 
1314 	if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1315 		chan->flag |= HOSTAPD_CHAN_DISABLED;
1316 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
1317 		chan->flag |= HOSTAPD_CHAN_NO_IR;
1318 	if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1319 		chan->flag |= HOSTAPD_CHAN_RADAR;
1320 	if (tb_freq[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
1321 		chan->flag |= HOSTAPD_CHAN_INDOOR_ONLY;
1322 	if (tb_freq[NL80211_FREQUENCY_ATTR_GO_CONCURRENT])
1323 		chan->flag |= HOSTAPD_CHAN_GO_CONCURRENT;
1324 
1325 	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
1326 		enum nl80211_dfs_state state =
1327 			nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
1328 
1329 		switch (state) {
1330 		case NL80211_DFS_USABLE:
1331 			chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
1332 			break;
1333 		case NL80211_DFS_AVAILABLE:
1334 			chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
1335 			break;
1336 		case NL80211_DFS_UNAVAILABLE:
1337 			chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
1338 			break;
1339 		}
1340 	}
1341 
1342 	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
1343 		chan->dfs_cac_ms = nla_get_u32(
1344 			tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
1345 	}
1346 }
1347 
1348 
1349 static int phy_info_freqs(struct phy_info_arg *phy_info,
1350 			  struct hostapd_hw_modes *mode, struct nlattr *tb)
1351 {
1352 	static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1353 		[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1354 		[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1355 		[NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
1356 		[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1357 		[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1358 		[NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
1359 	};
1360 	int new_channels = 0;
1361 	struct hostapd_channel_data *channel;
1362 	struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1363 	struct nlattr *nl_freq;
1364 	int rem_freq, idx;
1365 
1366 	if (tb == NULL)
1367 		return NL_OK;
1368 
1369 	nla_for_each_nested(nl_freq, tb, rem_freq) {
1370 		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
1371 			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
1372 		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1373 			continue;
1374 		new_channels++;
1375 	}
1376 
1377 	channel = os_realloc_array(mode->channels,
1378 				   mode->num_channels + new_channels,
1379 				   sizeof(struct hostapd_channel_data));
1380 	if (!channel)
1381 		return NL_STOP;
1382 
1383 	mode->channels = channel;
1384 	mode->num_channels += new_channels;
1385 
1386 	idx = phy_info->last_chan_idx;
1387 
1388 	nla_for_each_nested(nl_freq, tb, rem_freq) {
1389 		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
1390 			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
1391 		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1392 			continue;
1393 		phy_info_freq(mode, &mode->channels[idx], tb_freq);
1394 		idx++;
1395 	}
1396 	phy_info->last_chan_idx = idx;
1397 
1398 	return NL_OK;
1399 }
1400 
1401 
1402 static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
1403 {
1404 	static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1405 		[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1406 		[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
1407 		{ .type = NLA_FLAG },
1408 	};
1409 	struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1410 	struct nlattr *nl_rate;
1411 	int rem_rate, idx;
1412 
1413 	if (tb == NULL)
1414 		return NL_OK;
1415 
1416 	nla_for_each_nested(nl_rate, tb, rem_rate) {
1417 		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1418 			  nla_data(nl_rate), nla_len(nl_rate),
1419 			  rate_policy);
1420 		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1421 			continue;
1422 		mode->num_rates++;
1423 	}
1424 
1425 	mode->rates = os_calloc(mode->num_rates, sizeof(int));
1426 	if (!mode->rates)
1427 		return NL_STOP;
1428 
1429 	idx = 0;
1430 
1431 	nla_for_each_nested(nl_rate, tb, rem_rate) {
1432 		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1433 			  nla_data(nl_rate), nla_len(nl_rate),
1434 			  rate_policy);
1435 		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1436 			continue;
1437 		mode->rates[idx] = nla_get_u32(
1438 			tb_rate[NL80211_BITRATE_ATTR_RATE]);
1439 		idx++;
1440 	}
1441 
1442 	return NL_OK;
1443 }
1444 
1445 
1446 static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
1447 {
1448 	struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1449 	struct hostapd_hw_modes *mode;
1450 	int ret;
1451 
1452 	if (phy_info->last_mode != nl_band->nla_type) {
1453 		mode = os_realloc_array(phy_info->modes,
1454 					*phy_info->num_modes + 1,
1455 					sizeof(*mode));
1456 		if (!mode) {
1457 			phy_info->failed = 1;
1458 			return NL_STOP;
1459 		}
1460 		phy_info->modes = mode;
1461 
1462 		mode = &phy_info->modes[*(phy_info->num_modes)];
1463 		os_memset(mode, 0, sizeof(*mode));
1464 		mode->mode = NUM_HOSTAPD_MODES;
1465 		mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
1466 			HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
1467 
1468 		/*
1469 		 * Unsupported VHT MCS stream is defined as value 3, so the VHT
1470 		 * MCS RX/TX map must be initialized with 0xffff to mark all 8
1471 		 * possible streams as unsupported. This will be overridden if
1472 		 * driver advertises VHT support.
1473 		 */
1474 		mode->vht_mcs_set[0] = 0xff;
1475 		mode->vht_mcs_set[1] = 0xff;
1476 		mode->vht_mcs_set[4] = 0xff;
1477 		mode->vht_mcs_set[5] = 0xff;
1478 
1479 		*(phy_info->num_modes) += 1;
1480 		phy_info->last_mode = nl_band->nla_type;
1481 		phy_info->last_chan_idx = 0;
1482 	} else
1483 		mode = &phy_info->modes[*(phy_info->num_modes) - 1];
1484 
1485 	nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1486 		  nla_len(nl_band), NULL);
1487 
1488 	phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
1489 			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
1490 			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
1491 			 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
1492 	phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
1493 			  tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
1494 	ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
1495 	if (ret == NL_OK)
1496 		ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
1497 	if (ret != NL_OK) {
1498 		phy_info->failed = 1;
1499 		return ret;
1500 	}
1501 
1502 	return NL_OK;
1503 }
1504 
1505 
1506 static int phy_info_handler(struct nl_msg *msg, void *arg)
1507 {
1508 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1509 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1510 	struct phy_info_arg *phy_info = arg;
1511 	struct nlattr *nl_band;
1512 	int rem_band;
1513 
1514 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1515 		  genlmsg_attrlen(gnlh, 0), NULL);
1516 
1517 	if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1518 		return NL_SKIP;
1519 
1520 	nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
1521 	{
1522 		int res = phy_info_band(phy_info, nl_band);
1523 		if (res != NL_OK)
1524 			return res;
1525 	}
1526 
1527 	return NL_SKIP;
1528 }
1529 
1530 
1531 static struct hostapd_hw_modes *
1532 wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
1533 				     u16 *num_modes)
1534 {
1535 	u16 m;
1536 	struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1537 	int i, mode11g_idx = -1;
1538 
1539 	/* heuristic to set up modes */
1540 	for (m = 0; m < *num_modes; m++) {
1541 		if (!modes[m].num_channels)
1542 			continue;
1543 		if (modes[m].channels[0].freq < 4000) {
1544 			modes[m].mode = HOSTAPD_MODE_IEEE80211B;
1545 			for (i = 0; i < modes[m].num_rates; i++) {
1546 				if (modes[m].rates[i] > 200) {
1547 					modes[m].mode = HOSTAPD_MODE_IEEE80211G;
1548 					break;
1549 				}
1550 			}
1551 		} else if (modes[m].channels[0].freq > 50000)
1552 			modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
1553 		else
1554 			modes[m].mode = HOSTAPD_MODE_IEEE80211A;
1555 	}
1556 
1557 	/* If only 802.11g mode is included, use it to construct matching
1558 	 * 802.11b mode data. */
1559 
1560 	for (m = 0; m < *num_modes; m++) {
1561 		if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1562 			return modes; /* 802.11b already included */
1563 		if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1564 			mode11g_idx = m;
1565 	}
1566 
1567 	if (mode11g_idx < 0)
1568 		return modes; /* 2.4 GHz band not supported at all */
1569 
1570 	nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
1571 	if (nmodes == NULL)
1572 		return modes; /* Could not add 802.11b mode */
1573 
1574 	mode = &nmodes[*num_modes];
1575 	os_memset(mode, 0, sizeof(*mode));
1576 	(*num_modes)++;
1577 	modes = nmodes;
1578 
1579 	mode->mode = HOSTAPD_MODE_IEEE80211B;
1580 
1581 	mode11g = &modes[mode11g_idx];
1582 	mode->num_channels = mode11g->num_channels;
1583 	mode->channels = os_memdup(mode11g->channels,
1584 				   mode11g->num_channels *
1585 				   sizeof(struct hostapd_channel_data));
1586 	if (mode->channels == NULL) {
1587 		(*num_modes)--;
1588 		return modes; /* Could not add 802.11b mode */
1589 	}
1590 
1591 	mode->num_rates = 0;
1592 	mode->rates = os_malloc(4 * sizeof(int));
1593 	if (mode->rates == NULL) {
1594 		os_free(mode->channels);
1595 		(*num_modes)--;
1596 		return modes; /* Could not add 802.11b mode */
1597 	}
1598 
1599 	for (i = 0; i < mode11g->num_rates; i++) {
1600 		if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
1601 		    mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
1602 			continue;
1603 		mode->rates[mode->num_rates] = mode11g->rates[i];
1604 		mode->num_rates++;
1605 		if (mode->num_rates == 4)
1606 			break;
1607 	}
1608 
1609 	if (mode->num_rates == 0) {
1610 		os_free(mode->channels);
1611 		os_free(mode->rates);
1612 		(*num_modes)--;
1613 		return modes; /* No 802.11b rates */
1614 	}
1615 
1616 	wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1617 		   "information");
1618 
1619 	return modes;
1620 }
1621 
1622 
1623 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
1624 				  int end)
1625 {
1626 	int c;
1627 
1628 	for (c = 0; c < mode->num_channels; c++) {
1629 		struct hostapd_channel_data *chan = &mode->channels[c];
1630 		if (chan->freq - 10 >= start && chan->freq + 10 <= end)
1631 			chan->flag |= HOSTAPD_CHAN_HT40;
1632 	}
1633 }
1634 
1635 
1636 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
1637 				      int end)
1638 {
1639 	int c;
1640 
1641 	for (c = 0; c < mode->num_channels; c++) {
1642 		struct hostapd_channel_data *chan = &mode->channels[c];
1643 		if (!(chan->flag & HOSTAPD_CHAN_HT40))
1644 			continue;
1645 		if (chan->freq - 30 >= start && chan->freq - 10 <= end)
1646 			chan->flag |= HOSTAPD_CHAN_HT40MINUS;
1647 		if (chan->freq + 10 >= start && chan->freq + 30 <= end)
1648 			chan->flag |= HOSTAPD_CHAN_HT40PLUS;
1649 	}
1650 }
1651 
1652 
1653 static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
1654 				      struct phy_info_arg *results)
1655 {
1656 	u16 m;
1657 
1658 	for (m = 0; m < *results->num_modes; m++) {
1659 		int c;
1660 		struct hostapd_hw_modes *mode = &results->modes[m];
1661 
1662 		for (c = 0; c < mode->num_channels; c++) {
1663 			struct hostapd_channel_data *chan = &mode->channels[c];
1664 			if ((u32) chan->freq - 10 >= start &&
1665 			    (u32) chan->freq + 10 <= end)
1666 				chan->max_tx_power = max_eirp;
1667 		}
1668 	}
1669 }
1670 
1671 
1672 static void nl80211_reg_rule_ht40(u32 start, u32 end,
1673 				  struct phy_info_arg *results)
1674 {
1675 	u16 m;
1676 
1677 	for (m = 0; m < *results->num_modes; m++) {
1678 		if (!(results->modes[m].ht_capab &
1679 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1680 			continue;
1681 		nl80211_set_ht40_mode(&results->modes[m], start, end);
1682 	}
1683 }
1684 
1685 
1686 static void nl80211_reg_rule_sec(struct nlattr *tb[],
1687 				 struct phy_info_arg *results)
1688 {
1689 	u32 start, end, max_bw;
1690 	u16 m;
1691 
1692 	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1693 	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
1694 	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
1695 		return;
1696 
1697 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1698 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1699 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1700 
1701 	if (max_bw < 20)
1702 		return;
1703 
1704 	for (m = 0; m < *results->num_modes; m++) {
1705 		if (!(results->modes[m].ht_capab &
1706 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1707 			continue;
1708 		nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
1709 	}
1710 }
1711 
1712 
1713 static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
1714 				 int end, int max_bw)
1715 {
1716 	int c;
1717 
1718 	for (c = 0; c < mode->num_channels; c++) {
1719 		struct hostapd_channel_data *chan = &mode->channels[c];
1720 		if (chan->freq - 10 >= start && chan->freq + 70 <= end)
1721 			chan->flag |= HOSTAPD_CHAN_VHT_10_70;
1722 
1723 		if (chan->freq - 30 >= start && chan->freq + 50 <= end)
1724 			chan->flag |= HOSTAPD_CHAN_VHT_30_50;
1725 
1726 		if (chan->freq - 50 >= start && chan->freq + 30 <= end)
1727 			chan->flag |= HOSTAPD_CHAN_VHT_50_30;
1728 
1729 		if (chan->freq - 70 >= start && chan->freq + 10 <= end)
1730 			chan->flag |= HOSTAPD_CHAN_VHT_70_10;
1731 
1732 		if (max_bw >= 160) {
1733 			if (chan->freq - 10 >= start && chan->freq + 150 <= end)
1734 				chan->flag |= HOSTAPD_CHAN_VHT_10_150;
1735 
1736 			if (chan->freq - 30 >= start && chan->freq + 130 <= end)
1737 				chan->flag |= HOSTAPD_CHAN_VHT_30_130;
1738 
1739 			if (chan->freq - 50 >= start && chan->freq + 110 <= end)
1740 				chan->flag |= HOSTAPD_CHAN_VHT_50_110;
1741 
1742 			if (chan->freq - 70 >= start && chan->freq + 90 <= end)
1743 				chan->flag |= HOSTAPD_CHAN_VHT_70_90;
1744 
1745 			if (chan->freq - 90 >= start && chan->freq + 70 <= end)
1746 				chan->flag |= HOSTAPD_CHAN_VHT_90_70;
1747 
1748 			if (chan->freq - 110 >= start && chan->freq + 50 <= end)
1749 				chan->flag |= HOSTAPD_CHAN_VHT_110_50;
1750 
1751 			if (chan->freq - 130 >= start && chan->freq + 30 <= end)
1752 				chan->flag |= HOSTAPD_CHAN_VHT_130_30;
1753 
1754 			if (chan->freq - 150 >= start && chan->freq + 10 <= end)
1755 				chan->flag |= HOSTAPD_CHAN_VHT_150_10;
1756 		}
1757 	}
1758 }
1759 
1760 
1761 static void nl80211_reg_rule_vht(struct nlattr *tb[],
1762 				 struct phy_info_arg *results)
1763 {
1764 	u32 start, end, max_bw;
1765 	u16 m;
1766 
1767 	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1768 	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
1769 	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
1770 		return;
1771 
1772 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1773 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1774 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1775 
1776 	if (max_bw < 80)
1777 		return;
1778 
1779 	for (m = 0; m < *results->num_modes; m++) {
1780 		if (!(results->modes[m].ht_capab &
1781 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1782 			continue;
1783 		/* TODO: use a real VHT support indication */
1784 		if (!results->modes[m].vht_capab)
1785 			continue;
1786 
1787 		nl80211_set_vht_mode(&results->modes[m], start, end, max_bw);
1788 	}
1789 }
1790 
1791 
1792 static void nl80211_set_dfs_domain(enum nl80211_dfs_regions region,
1793 				   u8 *dfs_domain)
1794 {
1795 	if (region == NL80211_DFS_FCC)
1796 		*dfs_domain = HOSTAPD_DFS_REGION_FCC;
1797 	else if (region == NL80211_DFS_ETSI)
1798 		*dfs_domain = HOSTAPD_DFS_REGION_ETSI;
1799 	else if (region == NL80211_DFS_JP)
1800 		*dfs_domain = HOSTAPD_DFS_REGION_JP;
1801 	else
1802 		*dfs_domain = 0;
1803 }
1804 
1805 
1806 static const char * dfs_domain_name(enum nl80211_dfs_regions region)
1807 {
1808 	switch (region) {
1809 	case NL80211_DFS_UNSET:
1810 		return "DFS-UNSET";
1811 	case NL80211_DFS_FCC:
1812 		return "DFS-FCC";
1813 	case NL80211_DFS_ETSI:
1814 		return "DFS-ETSI";
1815 	case NL80211_DFS_JP:
1816 		return "DFS-JP";
1817 	default:
1818 		return "DFS-invalid";
1819 	}
1820 }
1821 
1822 
1823 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
1824 {
1825 	struct phy_info_arg *results = arg;
1826 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1827 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1828 	struct nlattr *nl_rule;
1829 	struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
1830 	int rem_rule;
1831 	static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1832 		[NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1833 		[NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1834 		[NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1835 		[NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1836 		[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1837 		[NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1838 	};
1839 
1840 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1841 		  genlmsg_attrlen(gnlh, 0), NULL);
1842 	if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
1843 	    !tb_msg[NL80211_ATTR_REG_RULES]) {
1844 		wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
1845 			   "available");
1846 		return NL_SKIP;
1847 	}
1848 
1849 	if (tb_msg[NL80211_ATTR_DFS_REGION]) {
1850 		enum nl80211_dfs_regions dfs_domain;
1851 		dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
1852 		nl80211_set_dfs_domain(dfs_domain, &results->dfs_domain);
1853 		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
1854 			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
1855 			   dfs_domain_name(dfs_domain));
1856 	} else {
1857 		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
1858 			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
1859 	}
1860 
1861 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1862 	{
1863 		u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
1864 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1865 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1866 		if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1867 		    tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
1868 			continue;
1869 		start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1870 		end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1871 		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1872 			max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
1873 		if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1874 			max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1875 		if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
1876 			flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
1877 
1878 		wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
1879 			   start, end, max_bw, max_eirp,
1880 			   flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
1881 			   flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
1882 			   flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
1883 			   flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
1884 			   "",
1885 			   flags & NL80211_RRF_DFS ? " (DFS)" : "",
1886 			   flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
1887 			   flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
1888 			   flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
1889 		if (max_bw >= 40)
1890 			nl80211_reg_rule_ht40(start, end, results);
1891 		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1892 			nl80211_reg_rule_max_eirp(start, end, max_eirp,
1893 						  results);
1894 	}
1895 
1896 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1897 	{
1898 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1899 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1900 		nl80211_reg_rule_sec(tb_rule, results);
1901 	}
1902 
1903 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1904 	{
1905 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1906 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1907 		nl80211_reg_rule_vht(tb_rule, results);
1908 	}
1909 
1910 	return NL_SKIP;
1911 }
1912 
1913 
1914 static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
1915 					struct phy_info_arg *results)
1916 {
1917 	struct nl_msg *msg;
1918 
1919 	msg = nlmsg_alloc();
1920 	if (!msg)
1921 		return -ENOMEM;
1922 
1923 	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1924 	if (drv->capa.flags & WPA_DRIVER_FLAGS_SELF_MANAGED_REGULATORY) {
1925 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx)) {
1926 			nlmsg_free(msg);
1927 			return -1;
1928 		}
1929 	}
1930 
1931 	return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
1932 }
1933 
1934 
1935 struct hostapd_hw_modes *
1936 nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags,
1937 			    u8 *dfs_domain)
1938 {
1939 	u32 feat;
1940 	struct i802_bss *bss = priv;
1941 	struct wpa_driver_nl80211_data *drv = bss->drv;
1942 	int nl_flags = 0;
1943 	struct nl_msg *msg;
1944 	struct phy_info_arg result = {
1945 		.num_modes = num_modes,
1946 		.modes = NULL,
1947 		.last_mode = -1,
1948 		.failed = 0,
1949 		.dfs_domain = 0,
1950 	};
1951 
1952 	*num_modes = 0;
1953 	*flags = 0;
1954 	*dfs_domain = 0;
1955 
1956 	feat = get_nl80211_protocol_features(drv);
1957 	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
1958 		nl_flags = NLM_F_DUMP;
1959 	if (!(msg = nl80211_cmd_msg(bss, nl_flags, NL80211_CMD_GET_WIPHY)) ||
1960 	    nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
1961 		nlmsg_free(msg);
1962 		return NULL;
1963 	}
1964 
1965 	if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
1966 		nl80211_set_regulatory_flags(drv, &result);
1967 		if (result.failed) {
1968 			int i;
1969 
1970 			for (i = 0; result.modes && i < *num_modes; i++) {
1971 				os_free(result.modes[i].channels);
1972 				os_free(result.modes[i].rates);
1973 			}
1974 			os_free(result.modes);
1975 			*num_modes = 0;
1976 			return NULL;
1977 		}
1978 
1979 		*dfs_domain = result.dfs_domain;
1980 
1981 		return wpa_driver_nl80211_postprocess_modes(result.modes,
1982 							    num_modes);
1983 	}
1984 
1985 	return NULL;
1986 }
1987