xref: /freebsd/contrib/wpa/src/drivers/driver_nl80211_capa.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
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 "common/brcm_vendor.h"
20 #include "driver_nl80211.h"
21 
22 
23 static int protocol_feature_handler(struct nl_msg *msg, void *arg)
24 {
25 	u32 *feat = arg;
26 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
27 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
28 
29 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
30 		  genlmsg_attrlen(gnlh, 0), NULL);
31 
32 	if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
33 		*feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
34 
35 	return NL_SKIP;
36 }
37 
38 
39 static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
40 {
41 	u32 feat = 0;
42 	struct nl_msg *msg;
43 
44 	msg = nlmsg_alloc();
45 	if (!msg)
46 		return 0;
47 
48 	if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES)) {
49 		nlmsg_free(msg);
50 		return 0;
51 	}
52 
53 	if (send_and_recv_resp(drv, msg, protocol_feature_handler, &feat) == 0)
54 		return feat;
55 
56 	return 0;
57 }
58 
59 
60 struct wiphy_info_data {
61 	struct wpa_driver_nl80211_data *drv;
62 	struct wpa_driver_capa *capa;
63 
64 	unsigned int num_multichan_concurrent;
65 
66 	unsigned int error:1;
67 	unsigned int device_ap_sme:1;
68 	unsigned int poll_command_supported:1;
69 	unsigned int data_tx_status:1;
70 	unsigned int auth_supported:1;
71 	unsigned int connect_supported:1;
72 	unsigned int p2p_go_supported:1;
73 	unsigned int p2p_client_supported:1;
74 	unsigned int p2p_go_ctwindow_supported:1;
75 	unsigned int p2p_concurrent:1;
76 	unsigned int channel_switch_supported:1;
77 	unsigned int set_qos_map_supported:1;
78 	unsigned int have_low_prio_scan:1;
79 	unsigned int wmm_ac_supported:1;
80 	unsigned int mac_addr_rand_scan_supported:1;
81 	unsigned int mac_addr_rand_sched_scan_supported:1;
82 	unsigned int update_ft_ies_supported:1;
83 	unsigned int has_key_mgmt:1;
84 	unsigned int has_key_mgmt_iftype:1;
85 };
86 
87 
88 static unsigned int probe_resp_offload_support(int supp_protocols)
89 {
90 	unsigned int prot = 0;
91 
92 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
93 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
94 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
95 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
96 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
97 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
98 	if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
99 		prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
100 
101 	return prot;
102 }
103 
104 
105 static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
106 					 struct nlattr *tb)
107 {
108 	struct nlattr *nl_mode;
109 	int i;
110 
111 	if (tb == NULL)
112 		return;
113 
114 	nla_for_each_nested(nl_mode, tb, i) {
115 		switch (nla_type(nl_mode)) {
116 		case NL80211_IFTYPE_AP:
117 			info->capa->flags |= WPA_DRIVER_FLAGS_AP;
118 			break;
119 		case NL80211_IFTYPE_MESH_POINT:
120 			info->capa->flags |= WPA_DRIVER_FLAGS_MESH;
121 			break;
122 		case NL80211_IFTYPE_ADHOC:
123 			info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
124 			break;
125 		case NL80211_IFTYPE_P2P_DEVICE:
126 			info->capa->flags |=
127 				WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
128 			break;
129 		case NL80211_IFTYPE_P2P_GO:
130 			info->p2p_go_supported = 1;
131 			break;
132 		case NL80211_IFTYPE_P2P_CLIENT:
133 			info->p2p_client_supported = 1;
134 			break;
135 		}
136 	}
137 }
138 
139 
140 static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
141 					 struct nlattr *nl_combi)
142 {
143 	struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
144 	struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
145 	struct nlattr *nl_limit, *nl_mode;
146 	int err, rem_limit, rem_mode;
147 	int combination_has_p2p = 0, combination_has_mgd = 0;
148 	static struct nla_policy
149 	iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
150 		[NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
151 		[NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
152 		[NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
153 		[NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
154 		[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
155 	},
156 	iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
157 		[NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
158 		[NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
159 	};
160 
161 	err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
162 			       nl_combi, iface_combination_policy);
163 	if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
164 	    !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
165 	    !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
166 		return 0; /* broken combination */
167 
168 	if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
169 		info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
170 
171 	nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
172 			    rem_limit) {
173 		err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
174 				       nl_limit, iface_limit_policy);
175 		if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
176 			return 0; /* broken combination */
177 
178 		nla_for_each_nested(nl_mode,
179 				    tb_limit[NL80211_IFACE_LIMIT_TYPES],
180 				    rem_mode) {
181 			int ift = nla_type(nl_mode);
182 			if (ift == NL80211_IFTYPE_P2P_GO ||
183 			    ift == NL80211_IFTYPE_P2P_CLIENT)
184 				combination_has_p2p = 1;
185 			if (ift == NL80211_IFTYPE_STATION)
186 				combination_has_mgd = 1;
187 		}
188 		if (combination_has_p2p && combination_has_mgd)
189 			break;
190 	}
191 
192 	if (combination_has_p2p && combination_has_mgd) {
193 		unsigned int num_channels =
194 			nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
195 
196 		info->p2p_concurrent = 1;
197 		if (info->num_multichan_concurrent < num_channels)
198 			info->num_multichan_concurrent = num_channels;
199 	}
200 
201 	return 0;
202 }
203 
204 
205 static void wiphy_info_iface_comb(struct wiphy_info_data *info,
206 				  struct nlattr *tb)
207 {
208 	struct nlattr *nl_combi;
209 	int rem_combi;
210 
211 	if (tb == NULL)
212 		return;
213 
214 	nla_for_each_nested(nl_combi, tb, rem_combi) {
215 		if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
216 			break;
217 	}
218 }
219 
220 
221 static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
222 				 struct nlattr *tb)
223 {
224 	struct nlattr *nl_cmd;
225 	int i;
226 
227 	if (tb == NULL)
228 		return;
229 
230 	nla_for_each_nested(nl_cmd, tb, i) {
231 		switch (nla_get_u32(nl_cmd)) {
232 		case NL80211_CMD_AUTHENTICATE:
233 			info->auth_supported = 1;
234 			break;
235 		case NL80211_CMD_CONNECT:
236 			info->connect_supported = 1;
237 			break;
238 		case NL80211_CMD_START_SCHED_SCAN:
239 			info->capa->sched_scan_supported = 1;
240 			break;
241 		case NL80211_CMD_PROBE_CLIENT:
242 			info->poll_command_supported = 1;
243 			break;
244 		case NL80211_CMD_CHANNEL_SWITCH:
245 			info->channel_switch_supported = 1;
246 			break;
247 		case NL80211_CMD_SET_QOS_MAP:
248 			info->set_qos_map_supported = 1;
249 			break;
250 		case NL80211_CMD_UPDATE_FT_IES:
251 			info->update_ft_ies_supported = 1;
252 			break;
253 		}
254 	}
255 }
256 
257 
258 static unsigned int get_akm_suites_info(struct nlattr *tb)
259 {
260 	int i, num;
261 	unsigned int key_mgmt = 0;
262 	u32 *akms;
263 
264 	if (!tb)
265 		return 0;
266 
267 	num = nla_len(tb) / sizeof(u32);
268 	akms = nla_data(tb);
269 	for (i = 0; i < num; i++) {
270 		switch (akms[i]) {
271 		case RSN_AUTH_KEY_MGMT_UNSPEC_802_1X:
272 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA |
273 				WPA_DRIVER_CAPA_KEY_MGMT_WPA2;
274 			break;
275 		case RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X:
276 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
277 				WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
278 			break;
279 		case RSN_AUTH_KEY_MGMT_FT_802_1X:
280 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT;
281 			break;
282 		case RSN_AUTH_KEY_MGMT_FT_PSK:
283 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
284 			break;
285 		case RSN_AUTH_KEY_MGMT_802_1X_SHA256:
286 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256;
287 			break;
288 		case RSN_AUTH_KEY_MGMT_PSK_SHA256:
289 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256;
290 			break;
291 		case RSN_AUTH_KEY_MGMT_TPK_HANDSHAKE:
292 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE;
293 			break;
294 		case RSN_AUTH_KEY_MGMT_FT_SAE:
295 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE;
296 			break;
297 		case RSN_AUTH_KEY_MGMT_FT_SAE_EXT_KEY:
298 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE_EXT_KEY;
299 			break;
300 		case RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384:
301 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384;
302 			break;
303 		case RSN_AUTH_KEY_MGMT_CCKM:
304 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_CCKM;
305 			break;
306 		case RSN_AUTH_KEY_MGMT_OSEN:
307 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_OSEN;
308 			break;
309 		case RSN_AUTH_KEY_MGMT_802_1X_SUITE_B:
310 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B;
311 			break;
312 		case RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192:
313 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192;
314 			break;
315 		case RSN_AUTH_KEY_MGMT_OWE:
316 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_OWE;
317 			break;
318 		case RSN_AUTH_KEY_MGMT_DPP:
319 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_DPP;
320 			break;
321 		case RSN_AUTH_KEY_MGMT_FILS_SHA256:
322 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256;
323 			break;
324 		case RSN_AUTH_KEY_MGMT_FILS_SHA384:
325 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384;
326 			break;
327 		case RSN_AUTH_KEY_MGMT_FT_FILS_SHA256:
328 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256;
329 			break;
330 		case RSN_AUTH_KEY_MGMT_FT_FILS_SHA384:
331 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384;
332 			break;
333 		case RSN_AUTH_KEY_MGMT_SAE:
334 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_SAE;
335 			break;
336 		case RSN_AUTH_KEY_MGMT_SAE_EXT_KEY:
337 			key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_SAE_EXT_KEY;
338 			break;
339 		}
340 	}
341 
342 	return key_mgmt;
343 }
344 
345 
346 static void get_iface_akm_suites_info(struct wiphy_info_data *info,
347 					struct nlattr *nl_akms)
348 {
349 	struct nlattr *tb[NL80211_IFTYPE_AKM_ATTR_MAX + 1];
350 	struct nlattr *nl_iftype;
351 	unsigned int key_mgmt;
352 	int i;
353 
354 	if (!nl_akms)
355 		return;
356 
357 	nla_parse(tb, NL80211_IFTYPE_AKM_ATTR_MAX,
358 		  nla_data(nl_akms), nla_len(nl_akms), NULL);
359 
360 	if (!tb[NL80211_IFTYPE_AKM_ATTR_IFTYPES] ||
361 	    !tb[NL80211_IFTYPE_AKM_ATTR_SUITES])
362 		return;
363 
364 	info->has_key_mgmt_iftype = 1;
365 	key_mgmt = get_akm_suites_info(tb[NL80211_IFTYPE_AKM_ATTR_SUITES]);
366 
367 	nla_for_each_nested(nl_iftype, tb[NL80211_IFTYPE_AKM_ATTR_IFTYPES], i) {
368 		switch (nla_type(nl_iftype)) {
369 		case NL80211_IFTYPE_ADHOC:
370 			info->drv->capa.key_mgmt_iftype[WPA_IF_IBSS] = key_mgmt;
371 			break;
372 		case NL80211_IFTYPE_STATION:
373 			info->drv->capa.key_mgmt_iftype[WPA_IF_STATION] =
374 				key_mgmt;
375 			break;
376 		case NL80211_IFTYPE_AP:
377 			info->drv->capa.key_mgmt_iftype[WPA_IF_AP_BSS] =
378 				key_mgmt;
379 			break;
380 		case NL80211_IFTYPE_AP_VLAN:
381 			info->drv->capa.key_mgmt_iftype[WPA_IF_AP_VLAN] =
382 				key_mgmt;
383 			break;
384 		case NL80211_IFTYPE_MESH_POINT:
385 			info->drv->capa.key_mgmt_iftype[WPA_IF_MESH] = key_mgmt;
386 			break;
387 		case NL80211_IFTYPE_P2P_CLIENT:
388 			info->drv->capa.key_mgmt_iftype[WPA_IF_P2P_CLIENT] =
389 				key_mgmt;
390 			break;
391 		case NL80211_IFTYPE_P2P_GO:
392 			info->drv->capa.key_mgmt_iftype[WPA_IF_P2P_GO] =
393 				key_mgmt;
394 			break;
395 		case NL80211_IFTYPE_P2P_DEVICE:
396 			info->drv->capa.key_mgmt_iftype[WPA_IF_P2P_DEVICE] =
397 				key_mgmt;
398 			break;
399 		case NL80211_IFTYPE_NAN:
400 			info->drv->capa.key_mgmt_iftype[WPA_IF_NAN] = key_mgmt;
401 			break;
402 		}
403 		wpa_printf(MSG_DEBUG, "nl80211: %s supported key_mgmt 0x%x",
404 			   nl80211_iftype_str(nla_type(nl_iftype)),
405 			   key_mgmt);
406 	}
407 }
408 
409 
410 static void wiphy_info_iftype_akm_suites(struct wiphy_info_data *info,
411 					 struct nlattr *tb)
412 {
413 	struct nlattr *nl_if;
414 	int rem_if;
415 
416 	if (!tb)
417 		return;
418 
419 	nla_for_each_nested(nl_if, tb, rem_if)
420 		get_iface_akm_suites_info(info, nl_if);
421 }
422 
423 
424 static void wiphy_info_akm_suites(struct wiphy_info_data *info,
425 				  struct nlattr *tb)
426 {
427 	if (!tb)
428 		return;
429 
430 	info->has_key_mgmt = 1;
431 	info->capa->key_mgmt = get_akm_suites_info(tb);
432 	wpa_printf(MSG_DEBUG, "nl80211: wiphy supported key_mgmt 0x%x",
433 		   info->capa->key_mgmt);
434 }
435 
436 
437 static void wiphy_info_cipher_suites(struct wiphy_info_data *info,
438 				     struct nlattr *tb)
439 {
440 	int i, num;
441 	u32 *ciphers;
442 
443 	if (tb == NULL)
444 		return;
445 
446 	num = nla_len(tb) / sizeof(u32);
447 	ciphers = nla_data(tb);
448 	for (i = 0; i < num; i++) {
449 		u32 c = ciphers[i];
450 
451 		wpa_printf(MSG_DEBUG, "nl80211: Supported cipher %02x-%02x-%02x:%d",
452 			   c >> 24, (c >> 16) & 0xff,
453 			   (c >> 8) & 0xff, c & 0xff);
454 		switch (c) {
455 		case RSN_CIPHER_SUITE_CCMP_256:
456 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP_256;
457 			break;
458 		case RSN_CIPHER_SUITE_GCMP_256:
459 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP_256;
460 			break;
461 		case RSN_CIPHER_SUITE_CCMP:
462 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_CCMP;
463 			break;
464 		case RSN_CIPHER_SUITE_GCMP:
465 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GCMP;
466 			break;
467 		case RSN_CIPHER_SUITE_TKIP:
468 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_TKIP;
469 			break;
470 		case RSN_CIPHER_SUITE_WEP104:
471 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP104;
472 			break;
473 		case RSN_CIPHER_SUITE_WEP40:
474 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_WEP40;
475 			break;
476 		case RSN_CIPHER_SUITE_AES_128_CMAC:
477 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP;
478 			break;
479 		case RSN_CIPHER_SUITE_BIP_GMAC_128:
480 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_128;
481 			break;
482 		case RSN_CIPHER_SUITE_BIP_GMAC_256:
483 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_GMAC_256;
484 			break;
485 		case RSN_CIPHER_SUITE_BIP_CMAC_256:
486 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_BIP_CMAC_256;
487 			break;
488 		case RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED:
489 			info->capa->enc |= WPA_DRIVER_CAPA_ENC_GTK_NOT_USED;
490 			break;
491 		}
492 	}
493 }
494 
495 
496 static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
497 			       struct nlattr *tb)
498 {
499 	if (tb)
500 		capa->max_remain_on_chan = nla_get_u32(tb);
501 }
502 
503 
504 static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
505 			    struct nlattr *ext_setup)
506 {
507 	if (tdls == NULL)
508 		return;
509 
510 	wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
511 	capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
512 
513 	if (ext_setup) {
514 		wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
515 		capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
516 	}
517 }
518 
519 
520 static int ext_feature_isset(const u8 *ext_features, int ext_features_len,
521 			     enum nl80211_ext_feature_index ftidx)
522 {
523 	u8 ft_byte;
524 
525 	if ((int) ftidx / 8 >= ext_features_len)
526 		return 0;
527 
528 	ft_byte = ext_features[ftidx / 8];
529 	return (ft_byte & BIT(ftidx % 8)) != 0;
530 }
531 
532 
533 static void wiphy_info_ext_feature_flags(struct wiphy_info_data *info,
534 					 struct nlattr *tb)
535 {
536 	struct wpa_driver_capa *capa = info->capa;
537 	u8 *ext_features;
538 	int len;
539 
540 	if (tb == NULL)
541 		return;
542 
543 	ext_features = nla_data(tb);
544 	len = nla_len(tb);
545 
546 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_VHT_IBSS))
547 		capa->flags |= WPA_DRIVER_FLAGS_VHT_IBSS;
548 
549 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_RRM))
550 		capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_RRM;
551 
552 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_FILS_STA))
553 		capa->flags |= WPA_DRIVER_FLAGS_SUPPORT_FILS;
554 
555 	if (ext_feature_isset(ext_features, len,
556 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
557 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY;
558 
559 	if (ext_feature_isset(ext_features, len,
560 			      NL80211_EXT_FEATURE_BEACON_RATE_HT))
561 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_RATE_HT;
562 
563 	if (ext_feature_isset(ext_features, len,
564 			      NL80211_EXT_FEATURE_BEACON_RATE_VHT))
565 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_RATE_VHT;
566 
567 	if (ext_feature_isset(ext_features, len,
568 			      NL80211_EXT_FEATURE_BEACON_RATE_HE))
569 		capa->flags2 |= WPA_DRIVER_FLAGS2_BEACON_RATE_HE;
570 
571 	if (ext_feature_isset(ext_features, len,
572 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL))
573 		capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_SET_SCAN_DWELL;
574 
575 	if (ext_feature_isset(ext_features, len,
576 			      NL80211_EXT_FEATURE_SCAN_START_TIME) &&
577 	    ext_feature_isset(ext_features, len,
578 			      NL80211_EXT_FEATURE_BSS_PARENT_TSF) &&
579 	    ext_feature_isset(ext_features, len,
580 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL))
581 		capa->rrm_flags |= WPA_DRIVER_FLAGS_SUPPORT_BEACON_REPORT;
582 	if (ext_feature_isset(ext_features, len,
583 			      NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA))
584 		capa->flags |= WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA;
585 	if (ext_feature_isset(ext_features, len,
586 			      NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED))
587 		capa->flags |= WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED;
588 	if (ext_feature_isset(ext_features, len,
589 			      NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI))
590 		capa->flags |= WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI;
591 	if (ext_feature_isset(ext_features, len,
592 			      NL80211_EXT_FEATURE_FILS_SK_OFFLOAD))
593 		capa->flags |= WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD;
594 
595 	if (ext_feature_isset(ext_features, len,
596 			      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
597 		capa->flags |= WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK;
598 	if (ext_feature_isset(ext_features, len,
599 			      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
600 		capa->flags |= WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X;
601 
602 	if (ext_feature_isset(ext_features, len,
603 			      NL80211_EXT_FEATURE_SAE_OFFLOAD))
604 		capa->flags2 |= WPA_DRIVER_FLAGS2_SAE_OFFLOAD_STA;
605 
606 	if (ext_feature_isset(ext_features, len,
607 			      NL80211_EXT_FEATURE_MFP_OPTIONAL))
608 		capa->flags |= WPA_DRIVER_FLAGS_MFP_OPTIONAL;
609 
610 	if (ext_feature_isset(ext_features, len,
611 			      NL80211_EXT_FEATURE_DFS_OFFLOAD))
612 		capa->flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
613 
614 #ifdef CONFIG_MBO
615 	if (ext_feature_isset(ext_features, len,
616 			      NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) &&
617 	    ext_feature_isset(ext_features, len,
618 			      NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) &&
619 	    ext_feature_isset(ext_features, len,
620 			      NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) &&
621 	    ext_feature_isset(
622 		    ext_features, len,
623 		    NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION))
624 		capa->flags |= WPA_DRIVER_FLAGS_OCE_STA;
625 #endif /* CONFIG_MBO */
626 
627 	if (ext_feature_isset(ext_features, len,
628 			      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
629 		capa->flags |= WPA_DRIVER_FLAGS_FTM_RESPONDER;
630 
631 	if (ext_feature_isset(ext_features, len,
632 			      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
633 		capa->flags |= WPA_DRIVER_FLAGS_CONTROL_PORT;
634 	if (ext_feature_isset(ext_features, len,
635 			      NL80211_EXT_FEATURE_CONTROL_PORT_NO_PREAUTH))
636 		capa->flags2 |= WPA_DRIVER_FLAGS2_CONTROL_PORT_RX;
637 	if (ext_feature_isset(
638 		    ext_features, len,
639 		    NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211_TX_STATUS))
640 		capa->flags2 |= WPA_DRIVER_FLAGS2_CONTROL_PORT_TX_STATUS;
641 
642 	if (ext_feature_isset(ext_features, len,
643 			      NL80211_EXT_FEATURE_VLAN_OFFLOAD))
644 		capa->flags |= WPA_DRIVER_FLAGS_VLAN_OFFLOAD;
645 
646 	if (ext_feature_isset(ext_features, len,
647 			      NL80211_EXT_FEATURE_CAN_REPLACE_PTK0))
648 		capa->flags |= WPA_DRIVER_FLAGS_SAFE_PTK0_REKEYS;
649 
650 	if (ext_feature_isset(ext_features, len,
651 			      NL80211_EXT_FEATURE_BEACON_PROTECTION))
652 		capa->flags |= WPA_DRIVER_FLAGS_BEACON_PROTECTION;
653 
654 	if (ext_feature_isset(ext_features, len,
655 			      NL80211_EXT_FEATURE_EXT_KEY_ID))
656 		capa->flags |= WPA_DRIVER_FLAGS_EXTENDED_KEY_ID;
657 
658 	if (ext_feature_isset(ext_features, len,
659 			      NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS))
660 		info->drv->multicast_registrations = 1;
661 
662 	if (ext_feature_isset(ext_features, len,
663 			      NL80211_EXT_FEATURE_FILS_DISCOVERY))
664 		info->drv->fils_discovery = 1;
665 
666 	if (ext_feature_isset(ext_features, len,
667 			      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
668 		info->drv->unsol_bcast_probe_resp = 1;
669 
670 	if (ext_feature_isset(ext_features, len, NL80211_EXT_FEATURE_PUNCT))
671 		info->drv->puncturing = 1;
672 
673 	if (ext_feature_isset(ext_features, len,
674 			      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
675 		capa->flags2 |= WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT;
676 
677 	if (ext_feature_isset(ext_features, len,
678 			      NL80211_EXT_FEATURE_OPERATING_CHANNEL_VALIDATION))
679 		capa->flags2 |= WPA_DRIVER_FLAGS2_OCV;
680 
681 	if (ext_feature_isset(ext_features, len,
682 			      NL80211_EXT_FEATURE_RADAR_BACKGROUND))
683 		capa->flags2 |= WPA_DRIVER_FLAGS2_RADAR_BACKGROUND;
684 
685 	if (ext_feature_isset(ext_features, len,
686 			      NL80211_EXT_FEATURE_SECURE_LTF)) {
687 		capa->flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_STA;
688 		capa->flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_AP;
689 	}
690 
691 	if (ext_feature_isset(ext_features, len,
692 			      NL80211_EXT_FEATURE_SECURE_RTT)) {
693 		capa->flags2 |= WPA_DRIVER_FLAGS2_SEC_RTT_STA;
694 		capa->flags2 |= WPA_DRIVER_FLAGS2_SEC_RTT_AP;
695 	}
696 
697 	if (ext_feature_isset(
698 		    ext_features, len,
699 		    NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE)) {
700 		capa->flags2 |= WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA;
701 		capa->flags2 |= WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_AP;
702 	}
703 
704 	if (ext_feature_isset(ext_features, len,
705 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
706 		capa->flags2 |= WPA_DRIVER_FLAGS2_SCAN_MIN_PREQ;
707 
708 	if (ext_feature_isset(ext_features, len,
709 			      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
710 		capa->flags2 |= WPA_DRIVER_FLAGS2_4WAY_HANDSHAKE_AP_PSK;
711 
712 	if (ext_feature_isset(ext_features, len,
713 			      NL80211_EXT_FEATURE_OWE_OFFLOAD))
714 		capa->flags2 |= WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA;
715 
716 	if (ext_feature_isset(ext_features, len,
717 			      NL80211_EXT_FEATURE_OWE_OFFLOAD_AP))
718 		capa->flags2 |= WPA_DRIVER_FLAGS2_OWE_OFFLOAD_AP;
719 
720 	if (ext_feature_isset(ext_features, len,
721 			      NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
722 		capa->flags2 |= WPA_DRIVER_FLAGS2_SAE_OFFLOAD_AP;
723 }
724 
725 
726 static void wiphy_info_feature_flags(struct wiphy_info_data *info,
727 				     struct nlattr *tb)
728 {
729 	u32 flags;
730 	struct wpa_driver_capa *capa = info->capa;
731 
732 	if (tb == NULL)
733 		return;
734 
735 	flags = nla_get_u32(tb);
736 
737 	if (flags & NL80211_FEATURE_SK_TX_STATUS)
738 		info->data_tx_status = 1;
739 
740 	if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
741 		capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
742 
743 	if (flags & NL80211_FEATURE_SAE)
744 		capa->flags |= WPA_DRIVER_FLAGS_SAE;
745 
746 	if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
747 		capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
748 
749 	if (flags & NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)
750 		capa->flags |= WPA_DRIVER_FLAGS_HT_2040_COEX;
751 
752 	if (flags & NL80211_FEATURE_TDLS_CHANNEL_SWITCH) {
753 		wpa_printf(MSG_DEBUG, "nl80211: TDLS channel switch");
754 		capa->flags |= WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH;
755 	}
756 
757 	if (flags & NL80211_FEATURE_P2P_GO_CTWIN)
758 		info->p2p_go_ctwindow_supported = 1;
759 
760 	if (flags & NL80211_FEATURE_LOW_PRIORITY_SCAN)
761 		info->have_low_prio_scan = 1;
762 
763 	if (flags & NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)
764 		info->mac_addr_rand_scan_supported = 1;
765 
766 	if (flags & NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR)
767 		info->mac_addr_rand_sched_scan_supported = 1;
768 
769 	if (flags & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
770 		info->wmm_ac_supported = 1;
771 
772 	if (flags & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES)
773 		capa->rrm_flags |= WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES;
774 
775 	if (flags & NL80211_FEATURE_WFA_TPC_IE_IN_PROBES)
776 		capa->rrm_flags |= WPA_DRIVER_FLAGS_WFA_TPC_IE_IN_PROBES;
777 
778 	if (flags & NL80211_FEATURE_QUIET)
779 		capa->rrm_flags |= WPA_DRIVER_FLAGS_QUIET;
780 
781 	if (flags & NL80211_FEATURE_TX_POWER_INSERTION)
782 		capa->rrm_flags |= WPA_DRIVER_FLAGS_TX_POWER_INSERTION;
783 
784 	if (flags & NL80211_FEATURE_HT_IBSS)
785 		capa->flags |= WPA_DRIVER_FLAGS_HT_IBSS;
786 
787 	if (flags & NL80211_FEATURE_FULL_AP_CLIENT_STATE)
788 		capa->flags |= WPA_DRIVER_FLAGS_FULL_AP_CLIENT_STATE;
789 }
790 
791 
792 static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
793 					  struct nlattr *tb)
794 {
795 	u32 protocols;
796 
797 	if (tb == NULL)
798 		return;
799 
800 	protocols = nla_get_u32(tb);
801 	wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
802 		   "mode");
803 	capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
804 	capa->probe_resp_offloads = probe_resp_offload_support(protocols);
805 }
806 
807 
808 static void wiphy_info_wowlan_triggers(struct wpa_driver_capa *capa,
809 				       struct nlattr *tb)
810 {
811 	struct nlattr *triggers[MAX_NL80211_WOWLAN_TRIG + 1];
812 
813 	if (tb == NULL)
814 		return;
815 
816 	if (nla_parse_nested(triggers, MAX_NL80211_WOWLAN_TRIG,
817 			     tb, NULL))
818 		return;
819 
820 	if (triggers[NL80211_WOWLAN_TRIG_ANY])
821 		capa->wowlan_triggers.any = 1;
822 	if (triggers[NL80211_WOWLAN_TRIG_DISCONNECT])
823 		capa->wowlan_triggers.disconnect = 1;
824 	if (triggers[NL80211_WOWLAN_TRIG_MAGIC_PKT])
825 		capa->wowlan_triggers.magic_pkt = 1;
826 	if (triggers[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
827 		capa->wowlan_triggers.gtk_rekey_failure = 1;
828 	if (triggers[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
829 		capa->wowlan_triggers.eap_identity_req = 1;
830 	if (triggers[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
831 		capa->wowlan_triggers.four_way_handshake = 1;
832 	if (triggers[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
833 		capa->wowlan_triggers.rfkill_release = 1;
834 }
835 
836 
837 static void wiphy_info_extended_capab(struct wpa_driver_nl80211_data *drv,
838 				      struct nlattr *tb)
839 {
840 	int rem = 0, i;
841 	struct nlattr *tb1[NL80211_ATTR_MAX + 1], *attr;
842 
843 	if (!tb || drv->num_iface_capa == NL80211_IFTYPE_MAX)
844 		return;
845 
846 	nla_for_each_nested(attr, tb, rem) {
847 		unsigned int len;
848 		struct drv_nl80211_iface_capa *capa;
849 
850 		nla_parse(tb1, NL80211_ATTR_MAX, nla_data(attr),
851 			  nla_len(attr), NULL);
852 
853 		if (!tb1[NL80211_ATTR_IFTYPE] ||
854 		    !tb1[NL80211_ATTR_EXT_CAPA] ||
855 		    !tb1[NL80211_ATTR_EXT_CAPA_MASK])
856 			continue;
857 
858 		capa = &drv->iface_capa[drv->num_iface_capa];
859 		capa->iftype = nla_get_u32(tb1[NL80211_ATTR_IFTYPE]);
860 		wpa_printf(MSG_DEBUG,
861 			   "nl80211: Driver-advertised extended capabilities for interface type %s",
862 			   nl80211_iftype_str(capa->iftype));
863 
864 		len = nla_len(tb1[NL80211_ATTR_EXT_CAPA]);
865 		capa->ext_capa = os_memdup(nla_data(tb1[NL80211_ATTR_EXT_CAPA]),
866 					   len);
867 		if (!capa->ext_capa)
868 			goto err;
869 
870 		capa->ext_capa_len = len;
871 		wpa_hexdump(MSG_DEBUG, "nl80211: Extended capabilities",
872 			    capa->ext_capa, capa->ext_capa_len);
873 
874 		len = nla_len(tb1[NL80211_ATTR_EXT_CAPA_MASK]);
875 		capa->ext_capa_mask =
876 			os_memdup(nla_data(tb1[NL80211_ATTR_EXT_CAPA_MASK]),
877 				  len);
878 		if (!capa->ext_capa_mask)
879 			goto err;
880 
881 		wpa_hexdump(MSG_DEBUG, "nl80211: Extended capabilities mask",
882 			    capa->ext_capa_mask, capa->ext_capa_len);
883 
884 		if (tb1[NL80211_ATTR_EML_CAPABILITY] &&
885 		    tb1[NL80211_ATTR_MLD_CAPA_AND_OPS]) {
886 			capa->eml_capa =
887 				nla_get_u16(tb1[NL80211_ATTR_EML_CAPABILITY]);
888 			capa->mld_capa_and_ops =
889 				nla_get_u16(tb1[NL80211_ATTR_MLD_CAPA_AND_OPS]);
890 		}
891 
892 		wpa_printf(MSG_DEBUG,
893 			   "nl80211: EML Capability: 0x%x MLD Capability: 0x%x",
894 			   capa->eml_capa, capa->mld_capa_and_ops);
895 
896 		drv->num_iface_capa++;
897 		if (drv->num_iface_capa == NL80211_IFTYPE_MAX)
898 			break;
899 	}
900 
901 	return;
902 
903 err:
904 	/* Cleanup allocated memory on error */
905 	for (i = 0; i < NL80211_IFTYPE_MAX; i++) {
906 		os_free(drv->iface_capa[i].ext_capa);
907 		drv->iface_capa[i].ext_capa = NULL;
908 		os_free(drv->iface_capa[i].ext_capa_mask);
909 		drv->iface_capa[i].ext_capa_mask = NULL;
910 		drv->iface_capa[i].ext_capa_len = 0;
911 	}
912 	drv->num_iface_capa = 0;
913 }
914 
915 
916 static void wiphy_info_mbssid(struct wpa_driver_capa *cap, struct nlattr *attr)
917 {
918 	struct nlattr *config[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
919 
920 	if (nla_parse_nested(config, NL80211_MBSSID_CONFIG_ATTR_MAX, attr,
921 			     NULL))
922 		return;
923 
924 	if (!config[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES])
925 		return;
926 
927 	cap->mbssid_max_interfaces =
928 		nla_get_u8(config[NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES]);
929 
930 	if (config[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY])
931 		cap->ema_max_periodicity =
932 			nla_get_u8(config[NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY]);
933 
934 	wpa_printf(MSG_DEBUG,
935 		   "mbssid: max interfaces %u, max profile periodicity %u",
936 		   cap->mbssid_max_interfaces, cap->ema_max_periodicity);
937 }
938 
939 
940 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
941 {
942 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
943 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
944 	struct wiphy_info_data *info = arg;
945 	struct wpa_driver_capa *capa = info->capa;
946 	struct wpa_driver_nl80211_data *drv = info->drv;
947 
948 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
949 		  genlmsg_attrlen(gnlh, 0), NULL);
950 
951 	if (tb[NL80211_ATTR_WIPHY])
952 		drv->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
953 
954 	if (tb[NL80211_ATTR_WIPHY_NAME])
955 		os_strlcpy(drv->phyname,
956 			   nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
957 			   sizeof(drv->phyname));
958 	if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
959 		capa->max_scan_ssids =
960 			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
961 
962 	if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
963 		capa->max_sched_scan_ssids =
964 			nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
965 
966 	if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS] &&
967 	    tb[NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL] &&
968 	    tb[NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS]) {
969 		capa->max_sched_scan_plans =
970 			nla_get_u32(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS]);
971 
972 		capa->max_sched_scan_plan_interval =
973 			nla_get_u32(tb[NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL]);
974 
975 		capa->max_sched_scan_plan_iterations =
976 			nla_get_u32(tb[NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS]);
977 	}
978 
979 	if (tb[NL80211_ATTR_MAX_MATCH_SETS])
980 		capa->max_match_sets =
981 			nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
982 
983 	if (tb[NL80211_ATTR_MAC_ACL_MAX])
984 		capa->max_acl_mac_addrs =
985 			nla_get_u32(tb[NL80211_ATTR_MAC_ACL_MAX]);
986 
987 	wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
988 	wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
989 	wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
990 	wiphy_info_cipher_suites(info, tb[NL80211_ATTR_CIPHER_SUITES]);
991 	wiphy_info_akm_suites(info, tb[NL80211_ATTR_AKM_SUITES]);
992 	wiphy_info_iftype_akm_suites(info, tb[NL80211_ATTR_IFTYPE_AKM_SUITES]);
993 
994 	if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
995 		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
996 			   "off-channel TX");
997 		capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
998 	}
999 
1000 	if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
1001 		wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
1002 		capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
1003 	}
1004 
1005 	wiphy_info_max_roc(capa,
1006 			   tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
1007 
1008 	if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
1009 		capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
1010 
1011 	wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
1012 			tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
1013 
1014 	if (tb[NL80211_ATTR_DEVICE_AP_SME]) {
1015 		u32 ap_sme_features_flags =
1016 			nla_get_u32(tb[NL80211_ATTR_DEVICE_AP_SME]);
1017 
1018 		if (ap_sme_features_flags & NL80211_AP_SME_SA_QUERY_OFFLOAD)
1019 			capa->flags2 |= WPA_DRIVER_FLAGS2_SA_QUERY_OFFLOAD_AP;
1020 
1021 		info->device_ap_sme = 1;
1022 	}
1023 
1024 	wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
1025 	wiphy_info_ext_feature_flags(info, tb[NL80211_ATTR_EXT_FEATURES]);
1026 	wiphy_info_probe_resp_offload(capa,
1027 				      tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
1028 
1029 	if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
1030 	    drv->extended_capa == NULL) {
1031 		drv->extended_capa =
1032 			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
1033 		if (drv->extended_capa) {
1034 			os_memcpy(drv->extended_capa,
1035 				  nla_data(tb[NL80211_ATTR_EXT_CAPA]),
1036 				  nla_len(tb[NL80211_ATTR_EXT_CAPA]));
1037 			drv->extended_capa_len =
1038 				nla_len(tb[NL80211_ATTR_EXT_CAPA]);
1039 			wpa_hexdump(MSG_DEBUG,
1040 				    "nl80211: Driver-advertised extended capabilities (default)",
1041 				    drv->extended_capa, drv->extended_capa_len);
1042 		}
1043 		drv->extended_capa_mask =
1044 			os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA_MASK]));
1045 		if (drv->extended_capa_mask) {
1046 			os_memcpy(drv->extended_capa_mask,
1047 				  nla_data(tb[NL80211_ATTR_EXT_CAPA_MASK]),
1048 				  nla_len(tb[NL80211_ATTR_EXT_CAPA_MASK]));
1049 			wpa_hexdump(MSG_DEBUG,
1050 				    "nl80211: Driver-advertised extended capabilities mask (default)",
1051 				    drv->extended_capa_mask,
1052 				    drv->extended_capa_len);
1053 		} else {
1054 			os_free(drv->extended_capa);
1055 			drv->extended_capa = NULL;
1056 			drv->extended_capa_len = 0;
1057 		}
1058 	}
1059 
1060 	wiphy_info_extended_capab(drv, tb[NL80211_ATTR_IFTYPE_EXT_CAPA]);
1061 
1062 	if (tb[NL80211_ATTR_VENDOR_DATA]) {
1063 		struct nlattr *nl;
1064 		int rem;
1065 
1066 		nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_DATA], rem) {
1067 			struct nl80211_vendor_cmd_info *vinfo;
1068 			if (nla_len(nl) != sizeof(*vinfo)) {
1069 				wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
1070 				continue;
1071 			}
1072 			vinfo = nla_data(nl);
1073 			if (vinfo->vendor_id == OUI_QCA) {
1074 				switch (vinfo->subcmd) {
1075 				case QCA_NL80211_VENDOR_SUBCMD_TEST:
1076 					drv->vendor_cmd_test_avail = 1;
1077 					break;
1078 #ifdef CONFIG_DRIVER_NL80211_QCA
1079 				case QCA_NL80211_VENDOR_SUBCMD_ROAMING:
1080 					drv->roaming_vendor_cmd_avail = 1;
1081 					break;
1082 				case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
1083 					drv->dfs_vendor_cmd_avail = 1;
1084 					break;
1085 				case QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES:
1086 					drv->get_features_vendor_cmd_avail = 1;
1087 					break;
1088 				case QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST:
1089 					drv->get_pref_freq_list = 1;
1090 					break;
1091 				case QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL:
1092 					drv->set_prob_oper_freq = 1;
1093 					break;
1094 				case QCA_NL80211_VENDOR_SUBCMD_DO_ACS:
1095 					drv->capa.flags |=
1096 						WPA_DRIVER_FLAGS_ACS_OFFLOAD;
1097 					drv->qca_do_acs = 1;
1098 					break;
1099 				case QCA_NL80211_VENDOR_SUBCMD_SETBAND:
1100 					drv->setband_vendor_cmd_avail = 1;
1101 					break;
1102 				case QCA_NL80211_VENDOR_SUBCMD_TRIGGER_SCAN:
1103 					drv->scan_vendor_cmd_avail = 1;
1104 					break;
1105 				case QCA_NL80211_VENDOR_SUBCMD_SET_WIFI_CONFIGURATION:
1106 					drv->set_wifi_conf_vendor_cmd_avail = 1;
1107 					break;
1108 				case QCA_NL80211_VENDOR_SUBCMD_FETCH_BSS_TRANSITION_STATUS:
1109 					drv->fetch_bss_trans_status = 1;
1110 					break;
1111 				case QCA_NL80211_VENDOR_SUBCMD_ROAM:
1112 					drv->roam_vendor_cmd_avail = 1;
1113 					break;
1114 				case QCA_NL80211_VENDOR_SUBCMD_ADD_STA_NODE:
1115 					drv->add_sta_node_vendor_cmd_avail = 1;
1116 					break;
1117 				case QCA_NL80211_VENDOR_SUBCMD_GET_STA_INFO:
1118 					drv->get_sta_info_vendor_cmd_avail = 1;
1119 					break;
1120 				case QCA_NL80211_VENDOR_SUBCMD_SECURE_RANGING_CONTEXT:
1121 					drv->secure_ranging_ctx_vendor_cmd_avail = 1;
1122 					break;
1123 #endif /* CONFIG_DRIVER_NL80211_QCA */
1124 				}
1125 #ifdef CONFIG_DRIVER_NL80211_BRCM
1126 			} else if (vinfo->vendor_id == OUI_BRCM) {
1127 				switch (vinfo->subcmd) {
1128 				case BRCM_VENDOR_SCMD_ACS:
1129 					drv->capa.flags |=
1130 						WPA_DRIVER_FLAGS_ACS_OFFLOAD;
1131 					wpa_printf(MSG_DEBUG,
1132 						   "Enabled BRCM ACS");
1133 					drv->brcm_do_acs = 1;
1134 					break;
1135 				}
1136 #endif /* CONFIG_DRIVER_NL80211_BRCM */
1137 			}
1138 
1139 			wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
1140 				   vinfo->vendor_id, vinfo->subcmd);
1141 		}
1142 	}
1143 
1144 	if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
1145 		struct nlattr *nl;
1146 		int rem;
1147 
1148 		nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
1149 			struct nl80211_vendor_cmd_info *vinfo;
1150 			if (nla_len(nl) != sizeof(*vinfo)) {
1151 				wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
1152 				continue;
1153 			}
1154 			vinfo = nla_data(nl);
1155 			wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
1156 				   vinfo->vendor_id, vinfo->subcmd);
1157 		}
1158 	}
1159 
1160 	wiphy_info_wowlan_triggers(capa,
1161 				   tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
1162 
1163 	if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
1164 		capa->max_stations =
1165 			nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
1166 
1167 	if (tb[NL80211_ATTR_MAX_CSA_COUNTERS])
1168 		capa->max_csa_counters =
1169 			nla_get_u8(tb[NL80211_ATTR_MAX_CSA_COUNTERS]);
1170 
1171 	if (tb[NL80211_ATTR_WIPHY_SELF_MANAGED_REG])
1172 		capa->flags |= WPA_DRIVER_FLAGS_SELF_MANAGED_REGULATORY;
1173 
1174 	if (tb[NL80211_ATTR_MAX_NUM_AKM_SUITES])
1175 		capa->max_num_akms =
1176 			nla_get_u16(tb[NL80211_ATTR_MAX_NUM_AKM_SUITES]);
1177 
1178 	if (tb[NL80211_ATTR_MBSSID_CONFIG])
1179 		wiphy_info_mbssid(capa, tb[NL80211_ATTR_MBSSID_CONFIG]);
1180 
1181 	if (tb[NL80211_ATTR_MLO_SUPPORT])
1182 		capa->flags2 |= WPA_DRIVER_FLAGS2_MLO;
1183 
1184 	return NL_SKIP;
1185 }
1186 
1187 
1188 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
1189 				       struct wiphy_info_data *info)
1190 {
1191 	u32 feat;
1192 	struct nl_msg *msg;
1193 	int flags = 0;
1194 
1195 	os_memset(info, 0, sizeof(*info));
1196 	info->capa = &drv->capa;
1197 	info->drv = drv;
1198 
1199 	feat = get_nl80211_protocol_features(drv);
1200 	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
1201 		flags = NLM_F_DUMP;
1202 	msg = nl80211_cmd_msg(drv->first_bss, flags, NL80211_CMD_GET_WIPHY);
1203 	if (!msg || nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
1204 		nlmsg_free(msg);
1205 		return -1;
1206 	}
1207 
1208 	if (send_and_recv_resp(drv, msg, wiphy_info_handler, info))
1209 		return -1;
1210 
1211 	if (info->auth_supported)
1212 		drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
1213 	else if (!info->connect_supported) {
1214 		wpa_printf(MSG_INFO, "nl80211: Driver does not support "
1215 			   "authentication/association or connect commands");
1216 		info->error = 1;
1217 	}
1218 
1219 	if (info->p2p_go_supported && info->p2p_client_supported)
1220 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
1221 	if (info->p2p_concurrent) {
1222 		wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
1223 			   "interface (driver advertised support)");
1224 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
1225 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
1226 	}
1227 	if (info->num_multichan_concurrent > 1) {
1228 		wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
1229 			   "concurrent (driver advertised support)");
1230 		drv->capa.num_multichan_concurrent =
1231 			info->num_multichan_concurrent;
1232 	}
1233 	if (drv->capa.flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
1234 		wpa_printf(MSG_DEBUG, "nl80211: use P2P_DEVICE support");
1235 
1236 	/* default to 5000 since early versions of mac80211 don't set it */
1237 	if (!drv->capa.max_remain_on_chan)
1238 		drv->capa.max_remain_on_chan = 5000;
1239 
1240 	drv->capa.wmm_ac_supported = info->wmm_ac_supported;
1241 
1242 	drv->capa.mac_addr_rand_sched_scan_supported =
1243 		info->mac_addr_rand_sched_scan_supported;
1244 	drv->capa.mac_addr_rand_scan_supported =
1245 		info->mac_addr_rand_scan_supported;
1246 
1247 	if (info->channel_switch_supported) {
1248 		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
1249 		if (!drv->capa.max_csa_counters)
1250 			drv->capa.max_csa_counters = 1;
1251 	}
1252 
1253 	if (!drv->capa.max_sched_scan_plans) {
1254 		drv->capa.max_sched_scan_plans = 1;
1255 		drv->capa.max_sched_scan_plan_interval = UINT32_MAX;
1256 		drv->capa.max_sched_scan_plan_iterations = 0;
1257 	}
1258 
1259 	if (info->update_ft_ies_supported)
1260 		drv->capa.flags |= WPA_DRIVER_FLAGS_UPDATE_FT_IES;
1261 
1262 	if (!drv->capa.max_num_akms)
1263 		drv->capa.max_num_akms = NL80211_MAX_NR_AKM_SUITES;
1264 
1265 	return 0;
1266 }
1267 
1268 
1269 #ifdef CONFIG_DRIVER_NL80211_QCA
1270 
1271 static int dfs_info_handler(struct nl_msg *msg, void *arg)
1272 {
1273 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1274 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1275 	int *dfs_capability_ptr = arg;
1276 
1277 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1278 		  genlmsg_attrlen(gnlh, 0), NULL);
1279 
1280 	if (tb[NL80211_ATTR_VENDOR_DATA]) {
1281 		struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
1282 		struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
1283 
1284 		nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
1285 			  nla_data(nl_vend), nla_len(nl_vend), NULL);
1286 
1287 		if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
1288 			u32 val;
1289 			val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
1290 			wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
1291 				   val);
1292 			*dfs_capability_ptr = val;
1293 		}
1294 	}
1295 
1296 	return NL_SKIP;
1297 }
1298 
1299 
1300 static void qca_nl80211_check_dfs_capa(struct wpa_driver_nl80211_data *drv)
1301 {
1302 	struct nl_msg *msg;
1303 	int dfs_capability = 0;
1304 	int ret;
1305 
1306 	if (!drv->dfs_vendor_cmd_avail)
1307 		return;
1308 
1309 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
1310 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
1311 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
1312 			QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)) {
1313 		nlmsg_free(msg);
1314 		return;
1315 	}
1316 
1317 	ret = send_and_recv_resp(drv, msg, dfs_info_handler, &dfs_capability);
1318 	if (!ret && dfs_capability)
1319 		drv->capa.flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
1320 }
1321 
1322 
1323 struct features_info {
1324 	u8 *flags;
1325 	size_t flags_len;
1326 	struct wpa_driver_capa *capa;
1327 };
1328 
1329 
1330 static int features_info_handler(struct nl_msg *msg, void *arg)
1331 {
1332 	struct nlattr *tb[NL80211_ATTR_MAX + 1];
1333 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1334 	struct features_info *info = arg;
1335 	struct nlattr *nl_vend, *attr;
1336 
1337 	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1338 		  genlmsg_attrlen(gnlh, 0), NULL);
1339 
1340 	nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
1341 	if (nl_vend) {
1342 		struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
1343 
1344 		nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
1345 			  nla_data(nl_vend), nla_len(nl_vend), NULL);
1346 
1347 		attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_FEATURE_FLAGS];
1348 		if (attr) {
1349 			int len = nla_len(attr);
1350 			info->flags = os_malloc(len);
1351 			if (info->flags != NULL) {
1352 				os_memcpy(info->flags, nla_data(attr), len);
1353 				info->flags_len = len;
1354 			}
1355 		}
1356 		attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_CONCURRENCY_CAPA];
1357 		if (attr)
1358 			info->capa->conc_capab = nla_get_u32(attr);
1359 
1360 		attr = tb_vendor[
1361 			QCA_WLAN_VENDOR_ATTR_MAX_CONCURRENT_CHANNELS_2_4_BAND];
1362 		if (attr)
1363 			info->capa->max_conc_chan_2_4 = nla_get_u32(attr);
1364 
1365 		attr = tb_vendor[
1366 			QCA_WLAN_VENDOR_ATTR_MAX_CONCURRENT_CHANNELS_5_0_BAND];
1367 		if (attr)
1368 			info->capa->max_conc_chan_5_0 = nla_get_u32(attr);
1369 	}
1370 
1371 	return NL_SKIP;
1372 }
1373 
1374 
1375 static int check_feature(enum qca_wlan_vendor_features feature,
1376 			 struct features_info *info)
1377 {
1378 	size_t idx = feature / 8;
1379 
1380 	return (idx < info->flags_len) &&
1381 		(info->flags[idx] & BIT(feature % 8));
1382 }
1383 
1384 
1385 static void qca_nl80211_get_features(struct wpa_driver_nl80211_data *drv)
1386 {
1387 	struct nl_msg *msg;
1388 	struct features_info info;
1389 	int ret;
1390 
1391 	if (!drv->get_features_vendor_cmd_avail)
1392 		return;
1393 
1394 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
1395 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
1396 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
1397 			QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES)) {
1398 		nlmsg_free(msg);
1399 		return;
1400 	}
1401 
1402 	os_memset(&info, 0, sizeof(info));
1403 	info.capa = &drv->capa;
1404 	ret = send_and_recv_resp(drv, msg, features_info_handler, &info);
1405 	if (ret || !info.flags)
1406 		return;
1407 
1408 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_KEY_MGMT_OFFLOAD, &info))
1409 		drv->capa.flags |= WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD;
1410 
1411 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SUPPORT_HW_MODE_ANY, &info))
1412 		drv->capa.flags |= WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY;
1413 
1414 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OFFCHANNEL_SIMULTANEOUS,
1415 			  &info))
1416 		drv->capa.flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS;
1417 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_P2P_LISTEN_OFFLOAD, &info))
1418 		drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD;
1419 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OCE_STA, &info))
1420 		drv->capa.flags |= WPA_DRIVER_FLAGS_OCE_STA;
1421 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OCE_AP, &info))
1422 		drv->capa.flags |= WPA_DRIVER_FLAGS_OCE_AP;
1423 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OCE_STA_CFON, &info))
1424 		drv->capa.flags |= WPA_DRIVER_FLAGS_OCE_STA_CFON;
1425 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SECURE_LTF_STA, &info))
1426 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_STA;
1427 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SECURE_LTF_AP, &info))
1428 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_LTF_AP;
1429 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SECURE_RTT_STA, &info))
1430 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_RTT_STA;
1431 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SECURE_RTT_AP, &info))
1432 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_SEC_RTT_AP;
1433 	if (check_feature(
1434 		    QCA_WLAN_VENDOR_FEATURE_PROT_RANGE_NEGO_AND_MEASURE_STA,
1435 		    &info))
1436 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA;
1437 	if (check_feature(
1438 		    QCA_WLAN_VENDOR_FEATURE_PROT_RANGE_NEGO_AND_MEASURE_AP,
1439 		    &info))
1440 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_AP;
1441 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_AP_ALLOWED_FREQ_LIST,
1442 			  &info))
1443 		drv->qca_ap_allowed_freqs = 1;
1444 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_HT_VHT_TWT_RESPONDER, &info))
1445 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_HT_VHT_TWT_RESPONDER;
1446 	os_free(info.flags);
1447 }
1448 
1449 #endif /* CONFIG_DRIVER_NL80211_QCA */
1450 
1451 
1452 int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
1453 {
1454 	struct wiphy_info_data info;
1455 	int i;
1456 
1457 	if (wpa_driver_nl80211_get_info(drv, &info))
1458 		return -1;
1459 
1460 	if (info.error)
1461 		return -1;
1462 
1463 	drv->has_capability = 1;
1464 	drv->has_driver_key_mgmt = info.has_key_mgmt | info.has_key_mgmt_iftype;
1465 
1466 	/* Fallback to hardcoded defaults if the driver does not advertise any
1467 	 * AKM capabilities. */
1468 	if (!drv->has_driver_key_mgmt) {
1469 		drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1470 			WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1471 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1472 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
1473 			WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B |
1474 			WPA_DRIVER_CAPA_KEY_MGMT_OWE |
1475 			WPA_DRIVER_CAPA_KEY_MGMT_DPP;
1476 
1477 		if (drv->capa.enc & (WPA_DRIVER_CAPA_ENC_CCMP_256 |
1478 				     WPA_DRIVER_CAPA_ENC_GCMP_256))
1479 			drv->capa.key_mgmt |=
1480 				WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192;
1481 
1482 		if (drv->capa.flags & WPA_DRIVER_FLAGS_SME)
1483 			drv->capa.key_mgmt |=
1484 				WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256 |
1485 				WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384 |
1486 				WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256 |
1487 				WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384 |
1488 				WPA_DRIVER_CAPA_KEY_MGMT_SAE;
1489 		else if (drv->capa.flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)
1490 			drv->capa.key_mgmt |=
1491 				WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256 |
1492 				WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384;
1493 	}
1494 
1495 	if (!info.has_key_mgmt_iftype) {
1496 		/* If the driver does not advertize per interface AKM
1497 		 * capabilities, consider all interfaces to support default AKMs
1498 		 * in key_mgmt. */
1499 		for (i = 0; i < WPA_IF_MAX; i++)
1500 			drv->capa.key_mgmt_iftype[i] = drv->capa.key_mgmt;
1501 	} else if (info.has_key_mgmt_iftype && !info.has_key_mgmt) {
1502 		/* If the driver advertizes only per interface supported AKMs
1503 		 * but does not advertize per wiphy AKM capabilities, consider
1504 		 * the default key_mgmt as a mask of per interface supported
1505 		 * AKMs. */
1506 		drv->capa.key_mgmt = 0;
1507 		for (i = 0; i < WPA_IF_MAX; i++)
1508 			drv->capa.key_mgmt |= drv->capa.key_mgmt_iftype[i];
1509 	} else if (info.has_key_mgmt_iftype && info.has_key_mgmt) {
1510 		/* If the driver advertizes AKM capabilities both per wiphy and
1511 		 * per interface, consider the interfaces for which per
1512 		 * interface AKM capabilities were not received to support the
1513 		 * default key_mgmt capabilities.
1514 		 */
1515 		for (i = 0; i < WPA_IF_MAX; i++)
1516 			if (!drv->capa.key_mgmt_iftype[i])
1517 				drv->capa.key_mgmt_iftype[i] =
1518 					drv->capa.key_mgmt;
1519 	}
1520 
1521 	drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1522 		WPA_DRIVER_AUTH_SHARED |
1523 		WPA_DRIVER_AUTH_LEAP;
1524 
1525 	drv->capa.flags |= WPA_DRIVER_FLAGS_VALID_ERROR_CODES;
1526 	drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
1527 	drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1528 
1529 	/*
1530 	 * As all cfg80211 drivers must support cases where the AP interface is
1531 	 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
1532 	 * case that the user space daemon has crashed, they must be able to
1533 	 * cleanup all stations and key entries in the AP tear down flow. Thus,
1534 	 * this flag can/should always be set for cfg80211 drivers.
1535 	 */
1536 	drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
1537 
1538 	if (!info.device_ap_sme) {
1539 		drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
1540 		drv->capa.flags2 |= WPA_DRIVER_FLAGS2_AP_SME;
1541 
1542 		/*
1543 		 * No AP SME is currently assumed to also indicate no AP MLME
1544 		 * in the driver/firmware.
1545 		 */
1546 		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
1547 	}
1548 
1549 	drv->device_ap_sme = info.device_ap_sme;
1550 	drv->poll_command_supported = info.poll_command_supported;
1551 	drv->data_tx_status = info.data_tx_status;
1552 	drv->p2p_go_ctwindow_supported = info.p2p_go_ctwindow_supported;
1553 	if (info.set_qos_map_supported)
1554 		drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
1555 	drv->have_low_prio_scan = info.have_low_prio_scan;
1556 
1557 	/*
1558 	 * If poll command and tx status are supported, mac80211 is new enough
1559 	 * to have everything we need to not need monitor interfaces.
1560 	 */
1561 	drv->use_monitor = !info.device_ap_sme &&
1562 		(!info.poll_command_supported || !info.data_tx_status);
1563 
1564 	/*
1565 	 * If we aren't going to use monitor interfaces, but the
1566 	 * driver doesn't support data TX status, we won't get TX
1567 	 * status for EAPOL frames.
1568 	 */
1569 	if (!drv->use_monitor && !info.data_tx_status)
1570 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1571 
1572 #ifdef CONFIG_DRIVER_NL80211_QCA
1573 	if (!(info.capa->flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD))
1574 		qca_nl80211_check_dfs_capa(drv);
1575 	qca_nl80211_get_features(drv);
1576 
1577 	/*
1578 	 * To enable offchannel simultaneous support in wpa_supplicant, the
1579 	 * underlying driver needs to support the same along with offchannel TX.
1580 	 * Offchannel TX support is needed since remain_on_channel and
1581 	 * action_tx use some common data structures and hence cannot be
1582 	 * scheduled simultaneously.
1583 	 */
1584 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
1585 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS;
1586 #endif /* CONFIG_DRIVER_NL80211_QCA */
1587 
1588 	wpa_printf(MSG_DEBUG,
1589 		   "nl80211: key_mgmt=0x%x enc=0x%x auth=0x%x flags=0x%llx flags2=0x%llx rrm_flags=0x%x probe_resp_offloads=0x%x max_stations=%u max_remain_on_chan=%u max_scan_ssids=%d",
1590 		   drv->capa.key_mgmt, drv->capa.enc, drv->capa.auth,
1591 		   (unsigned long long) drv->capa.flags,
1592 		   (unsigned long long) drv->capa.flags2, drv->capa.rrm_flags,
1593 		   drv->capa.probe_resp_offloads, drv->capa.max_stations,
1594 		   drv->capa.max_remain_on_chan, drv->capa.max_scan_ssids);
1595 	return 0;
1596 }
1597 
1598 
1599 struct phy_info_arg {
1600 	u16 *num_modes;
1601 	struct hostapd_hw_modes *modes;
1602 	int last_mode, last_chan_idx;
1603 	int failed;
1604 	u8 dfs_domain;
1605 };
1606 
1607 static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
1608 			     struct nlattr *ampdu_factor,
1609 			     struct nlattr *ampdu_density,
1610 			     struct nlattr *mcs_set)
1611 {
1612 	if (capa)
1613 		mode->ht_capab = nla_get_u16(capa);
1614 
1615 	if (ampdu_factor)
1616 		mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
1617 
1618 	if (ampdu_density)
1619 		mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
1620 
1621 	if (mcs_set && nla_len(mcs_set) >= 16) {
1622 		u8 *mcs;
1623 		mcs = nla_data(mcs_set);
1624 		os_memcpy(mode->mcs_set, mcs, 16);
1625 	}
1626 }
1627 
1628 
1629 static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
1630 			      struct nlattr *capa,
1631 			      struct nlattr *mcs_set)
1632 {
1633 	if (capa)
1634 		mode->vht_capab = nla_get_u32(capa);
1635 
1636 	if (mcs_set && nla_len(mcs_set) >= 8) {
1637 		u8 *mcs;
1638 		mcs = nla_data(mcs_set);
1639 		os_memcpy(mode->vht_mcs_set, mcs, 8);
1640 	}
1641 }
1642 
1643 
1644 static int phy_info_edmg_capa(struct hostapd_hw_modes *mode,
1645 			      struct nlattr *bw_config,
1646 			      struct nlattr *channels)
1647 {
1648 	if (!bw_config || !channels)
1649 		return NL_OK;
1650 
1651 	mode->edmg.bw_config = nla_get_u8(bw_config);
1652 	mode->edmg.channels = nla_get_u8(channels);
1653 
1654 	if (!mode->edmg.channels || !mode->edmg.bw_config)
1655 		return NL_STOP;
1656 
1657 	return NL_OK;
1658 }
1659 
1660 
1661 static int cw2ecw(unsigned int cw)
1662 {
1663 	int bit;
1664 
1665 	if (cw == 0)
1666 		return 0;
1667 
1668 	for (bit = 1; cw != 1; bit++)
1669 		cw >>= 1;
1670 
1671 	return bit;
1672 }
1673 
1674 
1675 static void phy_info_freq(struct hostapd_hw_modes *mode,
1676 			  struct hostapd_channel_data *chan,
1677 			  struct nlattr *tb_freq[])
1678 {
1679 	u8 channel;
1680 
1681 	os_memset(chan, 0, sizeof(*chan));
1682 	chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1683 	chan->flag = 0;
1684 	chan->allowed_bw = ~0;
1685 	chan->dfs_cac_ms = 0;
1686 	if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
1687 		chan->chan = channel;
1688 	else
1689 		wpa_printf(MSG_DEBUG,
1690 			   "nl80211: No channel number found for frequency %u MHz",
1691 			   chan->freq);
1692 
1693 	if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1694 		chan->flag |= HOSTAPD_CHAN_DISABLED;
1695 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
1696 		chan->flag |= HOSTAPD_CHAN_NO_IR;
1697 	if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1698 		chan->flag |= HOSTAPD_CHAN_RADAR;
1699 	if (tb_freq[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
1700 		chan->flag |= HOSTAPD_CHAN_INDOOR_ONLY;
1701 	if (tb_freq[NL80211_FREQUENCY_ATTR_GO_CONCURRENT])
1702 		chan->flag |= HOSTAPD_CHAN_GO_CONCURRENT;
1703 
1704 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_10MHZ])
1705 		chan->allowed_bw &= ~HOSTAPD_CHAN_WIDTH_10;
1706 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_20MHZ])
1707 		chan->allowed_bw &= ~HOSTAPD_CHAN_WIDTH_20;
1708 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS])
1709 		chan->allowed_bw &= ~HOSTAPD_CHAN_WIDTH_40P;
1710 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS])
1711 		chan->allowed_bw &= ~HOSTAPD_CHAN_WIDTH_40M;
1712 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_80MHZ])
1713 		chan->allowed_bw &= ~HOSTAPD_CHAN_WIDTH_80;
1714 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_160MHZ])
1715 		chan->allowed_bw &= ~HOSTAPD_CHAN_WIDTH_160;
1716 
1717 	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
1718 		enum nl80211_dfs_state state =
1719 			nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
1720 
1721 		switch (state) {
1722 		case NL80211_DFS_USABLE:
1723 			chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
1724 			break;
1725 		case NL80211_DFS_AVAILABLE:
1726 			chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
1727 			break;
1728 		case NL80211_DFS_UNAVAILABLE:
1729 			chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
1730 			break;
1731 		}
1732 	}
1733 
1734 	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
1735 		chan->dfs_cac_ms = nla_get_u32(
1736 			tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
1737 	}
1738 
1739 	chan->wmm_rules_valid = 0;
1740 	if (tb_freq[NL80211_FREQUENCY_ATTR_WMM]) {
1741 		static struct nla_policy wmm_policy[NL80211_WMMR_MAX + 1] = {
1742 			[NL80211_WMMR_CW_MIN] = { .type = NLA_U16 },
1743 			[NL80211_WMMR_CW_MAX] = { .type = NLA_U16 },
1744 			[NL80211_WMMR_AIFSN] = { .type = NLA_U8 },
1745 			[NL80211_WMMR_TXOP] = { .type = NLA_U16 },
1746 		};
1747 		static const u8 wmm_map[4] = {
1748 			[NL80211_AC_BE] = WMM_AC_BE,
1749 			[NL80211_AC_BK] = WMM_AC_BK,
1750 			[NL80211_AC_VI] = WMM_AC_VI,
1751 			[NL80211_AC_VO] = WMM_AC_VO,
1752 		};
1753 		struct nlattr *nl_wmm;
1754 		struct nlattr *tb_wmm[NL80211_WMMR_MAX + 1];
1755 		int rem_wmm, ac, count = 0;
1756 
1757 		nla_for_each_nested(nl_wmm, tb_freq[NL80211_FREQUENCY_ATTR_WMM],
1758 				    rem_wmm) {
1759 			if (nla_parse_nested(tb_wmm, NL80211_WMMR_MAX, nl_wmm,
1760 					     wmm_policy)) {
1761 				wpa_printf(MSG_DEBUG,
1762 					   "nl80211: Failed to parse WMM rules attribute");
1763 				return;
1764 			}
1765 			if (!tb_wmm[NL80211_WMMR_CW_MIN] ||
1766 			    !tb_wmm[NL80211_WMMR_CW_MAX] ||
1767 			    !tb_wmm[NL80211_WMMR_AIFSN] ||
1768 			    !tb_wmm[NL80211_WMMR_TXOP]) {
1769 				wpa_printf(MSG_DEBUG,
1770 					   "nl80211: Channel is missing WMM rule attribute");
1771 				return;
1772 			}
1773 			ac = nl_wmm->nla_type;
1774 			if ((unsigned int) ac >= ARRAY_SIZE(wmm_map)) {
1775 				wpa_printf(MSG_DEBUG,
1776 					   "nl80211: Invalid AC value %d", ac);
1777 				return;
1778 			}
1779 
1780 			ac = wmm_map[ac];
1781 			chan->wmm_rules[ac].min_cwmin =
1782 				cw2ecw(nla_get_u16(
1783 					       tb_wmm[NL80211_WMMR_CW_MIN]));
1784 			chan->wmm_rules[ac].min_cwmax =
1785 				cw2ecw(nla_get_u16(
1786 					       tb_wmm[NL80211_WMMR_CW_MAX]));
1787 			chan->wmm_rules[ac].min_aifs =
1788 				nla_get_u8(tb_wmm[NL80211_WMMR_AIFSN]);
1789 			chan->wmm_rules[ac].max_txop =
1790 				nla_get_u16(tb_wmm[NL80211_WMMR_TXOP]) / 32;
1791 			count++;
1792 		}
1793 
1794 		/* Set valid flag if all the AC rules are present */
1795 		if (count == WMM_AC_NUM)
1796 			chan->wmm_rules_valid = 1;
1797 	}
1798 }
1799 
1800 
1801 static int phy_info_freqs(struct phy_info_arg *phy_info,
1802 			  struct hostapd_hw_modes *mode, struct nlattr *tb)
1803 {
1804 	static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1805 		[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1806 		[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1807 		[NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
1808 		[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1809 		[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1810 		[NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
1811 		[NL80211_FREQUENCY_ATTR_NO_10MHZ] = { .type = NLA_FLAG },
1812 		[NL80211_FREQUENCY_ATTR_NO_20MHZ] = { .type = NLA_FLAG },
1813 		[NL80211_FREQUENCY_ATTR_NO_HT40_PLUS] = { .type = NLA_FLAG },
1814 		[NL80211_FREQUENCY_ATTR_NO_HT40_MINUS] = { .type = NLA_FLAG },
1815 		[NL80211_FREQUENCY_ATTR_NO_80MHZ] = { .type = NLA_FLAG },
1816 		[NL80211_FREQUENCY_ATTR_NO_160MHZ] = { .type = NLA_FLAG },
1817 	};
1818 	int new_channels = 0;
1819 	struct hostapd_channel_data *channel;
1820 	struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1821 	struct nlattr *nl_freq;
1822 	int rem_freq, idx;
1823 
1824 	if (tb == NULL)
1825 		return NL_OK;
1826 
1827 	nla_for_each_nested(nl_freq, tb, rem_freq) {
1828 		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
1829 			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
1830 		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1831 			continue;
1832 		new_channels++;
1833 	}
1834 
1835 	channel = os_realloc_array(mode->channels,
1836 				   mode->num_channels + new_channels,
1837 				   sizeof(struct hostapd_channel_data));
1838 	if (!channel)
1839 		return NL_STOP;
1840 
1841 	mode->channels = channel;
1842 	mode->num_channels += new_channels;
1843 
1844 	idx = phy_info->last_chan_idx;
1845 
1846 	nla_for_each_nested(nl_freq, tb, rem_freq) {
1847 		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
1848 			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
1849 		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1850 			continue;
1851 		phy_info_freq(mode, &mode->channels[idx], tb_freq);
1852 		idx++;
1853 	}
1854 	phy_info->last_chan_idx = idx;
1855 
1856 	return NL_OK;
1857 }
1858 
1859 
1860 static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
1861 {
1862 	static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1863 		[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1864 		[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
1865 		{ .type = NLA_FLAG },
1866 	};
1867 	struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1868 	struct nlattr *nl_rate;
1869 	int rem_rate, idx;
1870 
1871 	if (tb == NULL)
1872 		return NL_OK;
1873 
1874 	nla_for_each_nested(nl_rate, tb, rem_rate) {
1875 		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1876 			  nla_data(nl_rate), nla_len(nl_rate),
1877 			  rate_policy);
1878 		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1879 			continue;
1880 		mode->num_rates++;
1881 	}
1882 
1883 	mode->rates = os_calloc(mode->num_rates, sizeof(int));
1884 	if (!mode->rates)
1885 		return NL_STOP;
1886 
1887 	idx = 0;
1888 
1889 	nla_for_each_nested(nl_rate, tb, rem_rate) {
1890 		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1891 			  nla_data(nl_rate), nla_len(nl_rate),
1892 			  rate_policy);
1893 		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1894 			continue;
1895 		mode->rates[idx] = nla_get_u32(
1896 			tb_rate[NL80211_BITRATE_ATTR_RATE]);
1897 		idx++;
1898 	}
1899 
1900 	return NL_OK;
1901 }
1902 
1903 
1904 static void phy_info_iftype_copy(struct hostapd_hw_modes *mode,
1905 				 enum ieee80211_op_mode opmode,
1906 				 struct nlattr **tb, struct nlattr **tb_flags)
1907 {
1908 	enum nl80211_iftype iftype;
1909 	size_t len;
1910 	struct he_capabilities *he_capab = &mode->he_capab[opmode];
1911 	struct eht_capabilities *eht_capab = &mode->eht_capab[opmode];
1912 
1913 	switch (opmode) {
1914 	case IEEE80211_MODE_INFRA:
1915 		iftype = NL80211_IFTYPE_STATION;
1916 		break;
1917 	case IEEE80211_MODE_IBSS:
1918 		iftype = NL80211_IFTYPE_ADHOC;
1919 		break;
1920 	case IEEE80211_MODE_AP:
1921 		iftype = NL80211_IFTYPE_AP;
1922 		break;
1923 	case IEEE80211_MODE_MESH:
1924 		iftype = NL80211_IFTYPE_MESH_POINT;
1925 		break;
1926 	default:
1927 		return;
1928 	}
1929 
1930 	if (!nla_get_flag(tb_flags[iftype]))
1931 		return;
1932 
1933 	he_capab->he_supported = 1;
1934 
1935 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1936 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1937 
1938 		if (len > sizeof(he_capab->phy_cap))
1939 			len = sizeof(he_capab->phy_cap);
1940 		os_memcpy(he_capab->phy_cap,
1941 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1942 			  len);
1943 	}
1944 
1945 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]) {
1946 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]);
1947 
1948 		if (len > sizeof(he_capab->mac_cap))
1949 			len = sizeof(he_capab->mac_cap);
1950 		os_memcpy(he_capab->mac_cap,
1951 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]),
1952 			  len);
1953 	}
1954 
1955 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]) {
1956 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]);
1957 
1958 		if (len > sizeof(he_capab->mcs))
1959 			len = sizeof(he_capab->mcs);
1960 		os_memcpy(he_capab->mcs,
1961 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]),
1962 			  len);
1963 	}
1964 
1965 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]) {
1966 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]);
1967 
1968 		if (len > sizeof(he_capab->ppet))
1969 			len = sizeof(he_capab->ppet);
1970 		os_memcpy(&he_capab->ppet,
1971 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]),
1972 			  len);
1973 	}
1974 
1975 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA]) {
1976 		u16 capa;
1977 
1978 		capa = nla_get_u16(tb[NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA]);
1979 		he_capab->he_6ghz_capa = le_to_host16(capa);
1980 	}
1981 
1982 	if (!tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC] ||
1983 	    !tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY])
1984 		return;
1985 
1986 	eht_capab->eht_supported = true;
1987 
1988 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC] &&
1989 	    nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC]) >= 2) {
1990 		    const u8 *pos;
1991 
1992 		    pos = nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC]);
1993 		    eht_capab->mac_cap = WPA_GET_LE16(pos);
1994 	}
1995 
1996 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]) {
1997 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]);
1998 		if (len > sizeof(eht_capab->phy_cap))
1999 			len = sizeof(eht_capab->phy_cap);
2000 		os_memcpy(eht_capab->phy_cap,
2001 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]),
2002 			  len);
2003 	}
2004 
2005 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET]) {
2006 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET]);
2007 		if (len > sizeof(eht_capab->mcs))
2008 			len = sizeof(eht_capab->mcs);
2009 		os_memcpy(eht_capab->mcs,
2010 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET]),
2011 			  len);
2012 	}
2013 
2014 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE]) {
2015 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE]);
2016 		if (len > sizeof(eht_capab->ppet))
2017 			len = sizeof(eht_capab->ppet);
2018 		os_memcpy(&eht_capab->ppet,
2019 			  nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE]),
2020 			  len);
2021 	}
2022 }
2023 
2024 
2025 static int phy_info_iftype(struct hostapd_hw_modes *mode,
2026 			   struct nlattr *nl_iftype)
2027 {
2028 	struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
2029 	struct nlattr *tb_flags[NL80211_IFTYPE_MAX + 1];
2030 	unsigned int i;
2031 
2032 	nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
2033 		  nla_data(nl_iftype), nla_len(nl_iftype), NULL);
2034 
2035 	if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
2036 		return NL_STOP;
2037 
2038 	if (nla_parse_nested(tb_flags, NL80211_IFTYPE_MAX,
2039 			     tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES], NULL))
2040 		return NL_STOP;
2041 
2042 	for (i = 0; i < IEEE80211_MODE_NUM; i++)
2043 		phy_info_iftype_copy(mode, i, tb, tb_flags);
2044 
2045 	return NL_OK;
2046 }
2047 
2048 
2049 static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
2050 {
2051 	struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
2052 	struct hostapd_hw_modes *mode;
2053 	int ret;
2054 
2055 	if (phy_info->last_mode != nl_band->nla_type) {
2056 		mode = os_realloc_array(phy_info->modes,
2057 					*phy_info->num_modes + 1,
2058 					sizeof(*mode));
2059 		if (!mode) {
2060 			phy_info->failed = 1;
2061 			return NL_STOP;
2062 		}
2063 		phy_info->modes = mode;
2064 
2065 		mode = &phy_info->modes[*(phy_info->num_modes)];
2066 		os_memset(mode, 0, sizeof(*mode));
2067 		mode->mode = NUM_HOSTAPD_MODES;
2068 		mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
2069 			HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN |
2070 			HOSTAPD_MODE_FLAG_HE_INFO_KNOWN;
2071 
2072 		/*
2073 		 * Unsupported VHT MCS stream is defined as value 3, so the VHT
2074 		 * MCS RX/TX map must be initialized with 0xffff to mark all 8
2075 		 * possible streams as unsupported. This will be overridden if
2076 		 * driver advertises VHT support.
2077 		 */
2078 		mode->vht_mcs_set[0] = 0xff;
2079 		mode->vht_mcs_set[1] = 0xff;
2080 		mode->vht_mcs_set[4] = 0xff;
2081 		mode->vht_mcs_set[5] = 0xff;
2082 
2083 		*(phy_info->num_modes) += 1;
2084 		phy_info->last_mode = nl_band->nla_type;
2085 		phy_info->last_chan_idx = 0;
2086 	} else
2087 		mode = &phy_info->modes[*(phy_info->num_modes) - 1];
2088 
2089 	nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
2090 		  nla_len(nl_band), NULL);
2091 
2092 	phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
2093 			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
2094 			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
2095 			 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
2096 	phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
2097 			  tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
2098 	ret = phy_info_edmg_capa(mode,
2099 				 tb_band[NL80211_BAND_ATTR_EDMG_BW_CONFIG],
2100 				 tb_band[NL80211_BAND_ATTR_EDMG_CHANNELS]);
2101 	if (ret == NL_OK)
2102 		ret = phy_info_freqs(phy_info, mode,
2103 				     tb_band[NL80211_BAND_ATTR_FREQS]);
2104 	if (ret == NL_OK)
2105 		ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
2106 	if (ret != NL_OK) {
2107 		phy_info->failed = 1;
2108 		return ret;
2109 	}
2110 
2111 	if (tb_band[NL80211_BAND_ATTR_IFTYPE_DATA]) {
2112 		struct nlattr *nl_iftype;
2113 		int rem_band;
2114 
2115 		nla_for_each_nested(nl_iftype,
2116 				    tb_band[NL80211_BAND_ATTR_IFTYPE_DATA],
2117 				    rem_band) {
2118 			ret = phy_info_iftype(mode, nl_iftype);
2119 			if (ret != NL_OK)
2120 				return ret;
2121 		}
2122 	}
2123 
2124 	return NL_OK;
2125 }
2126 
2127 
2128 static int phy_info_handler(struct nl_msg *msg, void *arg)
2129 {
2130 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2131 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2132 	struct phy_info_arg *phy_info = arg;
2133 	struct nlattr *nl_band;
2134 	int rem_band;
2135 
2136 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2137 		  genlmsg_attrlen(gnlh, 0), NULL);
2138 
2139 	if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
2140 		return NL_SKIP;
2141 
2142 	nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
2143 	{
2144 		int res = phy_info_band(phy_info, nl_band);
2145 		if (res != NL_OK)
2146 			return res;
2147 	}
2148 
2149 	return NL_SKIP;
2150 }
2151 
2152 
2153 static struct hostapd_hw_modes *
2154 wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
2155 				     u16 *num_modes)
2156 {
2157 	u16 m;
2158 	struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
2159 	int i, mode11g_idx = -1;
2160 
2161 	/* heuristic to set up modes */
2162 	for (m = 0; m < *num_modes; m++) {
2163 		if (!modes[m].num_channels)
2164 			continue;
2165 
2166 		modes[m].is_6ghz = false;
2167 
2168 		if (modes[m].channels[0].freq < 2000) {
2169 			modes[m].num_channels = 0;
2170 			continue;
2171 		} else if (modes[m].channels[0].freq < 4000) {
2172 			modes[m].mode = HOSTAPD_MODE_IEEE80211B;
2173 			for (i = 0; i < modes[m].num_rates; i++) {
2174 				if (modes[m].rates[i] > 200) {
2175 					modes[m].mode = HOSTAPD_MODE_IEEE80211G;
2176 					break;
2177 				}
2178 			}
2179 		} else if (modes[m].channels[0].freq > 50000) {
2180 			modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
2181 		} else if (is_6ghz_freq(modes[m].channels[0].freq)) {
2182 			modes[m].mode = HOSTAPD_MODE_IEEE80211A;
2183 			modes[m].is_6ghz = true;
2184 		} else {
2185 			modes[m].mode = HOSTAPD_MODE_IEEE80211A;
2186 		}
2187 	}
2188 
2189 	/* Remove unsupported bands */
2190 	m = 0;
2191 	while (m < *num_modes) {
2192 		if (modes[m].mode == NUM_HOSTAPD_MODES) {
2193 			wpa_printf(MSG_DEBUG,
2194 				   "nl80211: Remove unsupported mode");
2195 			os_free(modes[m].channels);
2196 			os_free(modes[m].rates);
2197 			if (m + 1 < *num_modes)
2198 				os_memmove(&modes[m], &modes[m + 1],
2199 					   sizeof(struct hostapd_hw_modes) *
2200 					   (*num_modes - (m + 1)));
2201 			(*num_modes)--;
2202 			continue;
2203 		}
2204 		m++;
2205 	}
2206 
2207 	/* If only 802.11g mode is included, use it to construct matching
2208 	 * 802.11b mode data. */
2209 
2210 	for (m = 0; m < *num_modes; m++) {
2211 		if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
2212 			return modes; /* 802.11b already included */
2213 		if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
2214 			mode11g_idx = m;
2215 	}
2216 
2217 	if (mode11g_idx < 0)
2218 		return modes; /* 2.4 GHz band not supported at all */
2219 
2220 	nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
2221 	if (nmodes == NULL)
2222 		return modes; /* Could not add 802.11b mode */
2223 
2224 	mode = &nmodes[*num_modes];
2225 	os_memset(mode, 0, sizeof(*mode));
2226 	(*num_modes)++;
2227 	modes = nmodes;
2228 
2229 	mode->mode = HOSTAPD_MODE_IEEE80211B;
2230 
2231 	mode11g = &modes[mode11g_idx];
2232 	mode->num_channels = mode11g->num_channels;
2233 	mode->channels = os_memdup(mode11g->channels,
2234 				   mode11g->num_channels *
2235 				   sizeof(struct hostapd_channel_data));
2236 	if (mode->channels == NULL) {
2237 		(*num_modes)--;
2238 		return modes; /* Could not add 802.11b mode */
2239 	}
2240 
2241 	mode->num_rates = 0;
2242 	mode->rates = os_malloc(4 * sizeof(int));
2243 	if (mode->rates == NULL) {
2244 		os_free(mode->channels);
2245 		(*num_modes)--;
2246 		return modes; /* Could not add 802.11b mode */
2247 	}
2248 
2249 	for (i = 0; i < mode11g->num_rates; i++) {
2250 		if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
2251 		    mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
2252 			continue;
2253 		mode->rates[mode->num_rates] = mode11g->rates[i];
2254 		mode->num_rates++;
2255 		if (mode->num_rates == 4)
2256 			break;
2257 	}
2258 
2259 	if (mode->num_rates == 0) {
2260 		os_free(mode->channels);
2261 		os_free(mode->rates);
2262 		(*num_modes)--;
2263 		return modes; /* No 802.11b rates */
2264 	}
2265 
2266 	wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
2267 		   "information");
2268 
2269 	return modes;
2270 }
2271 
2272 
2273 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
2274 				  int end)
2275 {
2276 	int c;
2277 
2278 	for (c = 0; c < mode->num_channels; c++) {
2279 		struct hostapd_channel_data *chan = &mode->channels[c];
2280 		if (chan->freq - 10 >= start && chan->freq + 10 <= end)
2281 			chan->flag |= HOSTAPD_CHAN_HT40;
2282 	}
2283 }
2284 
2285 
2286 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
2287 				      int end)
2288 {
2289 	int c;
2290 
2291 	for (c = 0; c < mode->num_channels; c++) {
2292 		struct hostapd_channel_data *chan = &mode->channels[c];
2293 		if (!(chan->flag & HOSTAPD_CHAN_HT40))
2294 			continue;
2295 		if (chan->freq - 30 >= start && chan->freq - 10 <= end)
2296 			chan->flag |= HOSTAPD_CHAN_HT40MINUS;
2297 		if (chan->freq + 10 >= start && chan->freq + 30 <= end)
2298 			chan->flag |= HOSTAPD_CHAN_HT40PLUS;
2299 	}
2300 }
2301 
2302 
2303 static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
2304 				      struct phy_info_arg *results)
2305 {
2306 	u16 m;
2307 
2308 	for (m = 0; m < *results->num_modes; m++) {
2309 		int c;
2310 		struct hostapd_hw_modes *mode = &results->modes[m];
2311 
2312 		for (c = 0; c < mode->num_channels; c++) {
2313 			struct hostapd_channel_data *chan = &mode->channels[c];
2314 			if ((u32) chan->freq - 10 >= start &&
2315 			    (u32) chan->freq + 10 <= end)
2316 				chan->max_tx_power = max_eirp;
2317 		}
2318 	}
2319 }
2320 
2321 
2322 static void nl80211_reg_rule_ht40(u32 start, u32 end,
2323 				  struct phy_info_arg *results)
2324 {
2325 	u16 m;
2326 
2327 	for (m = 0; m < *results->num_modes; m++) {
2328 		if (!(results->modes[m].ht_capab &
2329 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2330 			continue;
2331 		nl80211_set_ht40_mode(&results->modes[m], start, end);
2332 	}
2333 }
2334 
2335 
2336 static void nl80211_reg_rule_sec(struct nlattr *tb[],
2337 				 struct phy_info_arg *results)
2338 {
2339 	u32 start, end, max_bw;
2340 	u16 m;
2341 
2342 	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
2343 	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
2344 	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
2345 		return;
2346 
2347 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
2348 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
2349 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
2350 
2351 	if (max_bw < 20)
2352 		return;
2353 
2354 	for (m = 0; m < *results->num_modes; m++) {
2355 		if (!(results->modes[m].ht_capab &
2356 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2357 			continue;
2358 		nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
2359 	}
2360 }
2361 
2362 
2363 static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
2364 				 int end, int max_bw)
2365 {
2366 	int c;
2367 
2368 	for (c = 0; c < mode->num_channels; c++) {
2369 		struct hostapd_channel_data *chan = &mode->channels[c];
2370 
2371 		if (chan->freq - 10 < start || chan->freq + 10 > end)
2372 			continue;
2373 
2374 		if (max_bw >= 80)
2375 			chan->flag |= HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL;
2376 
2377 		if (max_bw >= 160)
2378 			chan->flag |= HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL;
2379 	}
2380 }
2381 
2382 
2383 static void nl80211_reg_rule_vht(struct nlattr *tb[],
2384 				 struct phy_info_arg *results)
2385 {
2386 	u32 start, end, max_bw;
2387 	u16 m;
2388 
2389 	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
2390 	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
2391 	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
2392 		return;
2393 
2394 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
2395 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
2396 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
2397 
2398 	if (max_bw < 80)
2399 		return;
2400 
2401 	for (m = 0; m < *results->num_modes; m++) {
2402 		if (!(results->modes[m].ht_capab &
2403 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
2404 			continue;
2405 		/* TODO: use a real VHT support indication */
2406 		if (!results->modes[m].vht_capab)
2407 			continue;
2408 
2409 		nl80211_set_vht_mode(&results->modes[m], start, end, max_bw);
2410 	}
2411 }
2412 
2413 
2414 static void nl80211_set_6ghz_mode(struct hostapd_hw_modes *mode, int start,
2415 				  int end, int max_bw)
2416 {
2417 	int c;
2418 
2419 	for (c = 0; c < mode->num_channels; c++) {
2420 		struct hostapd_channel_data *chan = &mode->channels[c];
2421 
2422 		if (chan->freq - 10 < start || chan->freq + 10 > end)
2423 			continue;
2424 
2425 		if (max_bw >= 80)
2426 			chan->flag |= HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL;
2427 
2428 		if (max_bw >= 160)
2429 			chan->flag |= HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL;
2430 
2431 		if (max_bw >= 320)
2432 			chan->flag |= HOSTAPD_CHAN_EHT_320MHZ_SUBCHANNEL;
2433 	}
2434 }
2435 
2436 
2437 static void nl80211_reg_rule_6ghz(struct nlattr *tb[],
2438 				 struct phy_info_arg *results)
2439 {
2440 	u32 start, end, max_bw;
2441 	u16 m;
2442 
2443 	if (!tb[NL80211_ATTR_FREQ_RANGE_START] ||
2444 	    !tb[NL80211_ATTR_FREQ_RANGE_END] ||
2445 	    !tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2446 		return;
2447 
2448 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
2449 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
2450 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
2451 
2452 	if (max_bw < 80)
2453 		return;
2454 
2455 	for (m = 0; m < *results->num_modes; m++) {
2456 		if (results->modes[m].num_channels == 0 ||
2457 		    !is_6ghz_freq(results->modes[m].channels[0].freq))
2458 			continue;
2459 
2460 		nl80211_set_6ghz_mode(&results->modes[m], start, end, max_bw);
2461 	}
2462 }
2463 
2464 
2465 static void nl80211_set_dfs_domain(enum nl80211_dfs_regions region,
2466 				   u8 *dfs_domain)
2467 {
2468 	if (region == NL80211_DFS_FCC)
2469 		*dfs_domain = HOSTAPD_DFS_REGION_FCC;
2470 	else if (region == NL80211_DFS_ETSI)
2471 		*dfs_domain = HOSTAPD_DFS_REGION_ETSI;
2472 	else if (region == NL80211_DFS_JP)
2473 		*dfs_domain = HOSTAPD_DFS_REGION_JP;
2474 	else
2475 		*dfs_domain = 0;
2476 }
2477 
2478 
2479 static const char * dfs_domain_name(enum nl80211_dfs_regions region)
2480 {
2481 	switch (region) {
2482 	case NL80211_DFS_UNSET:
2483 		return "DFS-UNSET";
2484 	case NL80211_DFS_FCC:
2485 		return "DFS-FCC";
2486 	case NL80211_DFS_ETSI:
2487 		return "DFS-ETSI";
2488 	case NL80211_DFS_JP:
2489 		return "DFS-JP";
2490 	default:
2491 		return "DFS-invalid";
2492 	}
2493 }
2494 
2495 
2496 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
2497 {
2498 	struct phy_info_arg *results = arg;
2499 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2500 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2501 	struct nlattr *nl_rule;
2502 	struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
2503 	int rem_rule;
2504 	static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2505 		[NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2506 		[NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2507 		[NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2508 		[NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2509 		[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2510 		[NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2511 	};
2512 
2513 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2514 		  genlmsg_attrlen(gnlh, 0), NULL);
2515 	if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
2516 	    !tb_msg[NL80211_ATTR_REG_RULES]) {
2517 		wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
2518 			   "available");
2519 		return NL_SKIP;
2520 	}
2521 
2522 	if (tb_msg[NL80211_ATTR_DFS_REGION]) {
2523 		enum nl80211_dfs_regions dfs_domain;
2524 		dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
2525 		nl80211_set_dfs_domain(dfs_domain, &results->dfs_domain);
2526 		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
2527 			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
2528 			   dfs_domain_name(dfs_domain));
2529 	} else {
2530 		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
2531 			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
2532 	}
2533 
2534 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
2535 	{
2536 		u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
2537 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
2538 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
2539 		if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
2540 		    tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
2541 			continue;
2542 		start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
2543 		end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
2544 		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2545 			max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
2546 		if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2547 			max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
2548 		if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
2549 			flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
2550 
2551 		wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
2552 			   start, end, max_bw, max_eirp,
2553 			   flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
2554 			   flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
2555 			   flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
2556 			   flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
2557 			   "",
2558 			   flags & NL80211_RRF_DFS ? " (DFS)" : "",
2559 			   flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
2560 			   flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
2561 			   flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
2562 		if (max_bw >= 40)
2563 			nl80211_reg_rule_ht40(start, end, results);
2564 		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2565 			nl80211_reg_rule_max_eirp(start, end, max_eirp,
2566 						  results);
2567 	}
2568 
2569 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
2570 	{
2571 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
2572 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
2573 		nl80211_reg_rule_sec(tb_rule, results);
2574 	}
2575 
2576 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
2577 	{
2578 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
2579 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
2580 		nl80211_reg_rule_vht(tb_rule, results);
2581 	}
2582 
2583 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
2584 	{
2585 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
2586 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
2587 		nl80211_reg_rule_6ghz(tb_rule, results);
2588 	}
2589 
2590 	return NL_SKIP;
2591 }
2592 
2593 
2594 static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
2595 					struct phy_info_arg *results)
2596 {
2597 	struct nl_msg *msg;
2598 
2599 	msg = nlmsg_alloc();
2600 	if (!msg)
2601 		return -ENOMEM;
2602 
2603 	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
2604 	if (drv->capa.flags & WPA_DRIVER_FLAGS_SELF_MANAGED_REGULATORY) {
2605 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, drv->wiphy_idx)) {
2606 			nlmsg_free(msg);
2607 			return -1;
2608 		}
2609 	}
2610 
2611 	return send_and_recv_resp(drv, msg, nl80211_get_reg, results);
2612 }
2613 
2614 
2615 static const char * modestr(enum hostapd_hw_mode mode)
2616 {
2617 	switch (mode) {
2618 	case HOSTAPD_MODE_IEEE80211B:
2619 		return "802.11b";
2620 	case HOSTAPD_MODE_IEEE80211G:
2621 		return "802.11g";
2622 	case HOSTAPD_MODE_IEEE80211A:
2623 		return "802.11a";
2624 	case HOSTAPD_MODE_IEEE80211AD:
2625 		return "802.11ad";
2626 	default:
2627 		return "?";
2628 	}
2629 }
2630 
2631 
2632 static void nl80211_dump_chan_list(struct wpa_driver_nl80211_data *drv,
2633 				   struct hostapd_hw_modes *modes,
2634 				   u16 num_modes)
2635 {
2636 	int i;
2637 
2638 	if (!modes)
2639 		return;
2640 
2641 	for (i = 0; i < num_modes; i++) {
2642 		struct hostapd_hw_modes *mode = &modes[i];
2643 		char str[1000];
2644 		char *pos = str;
2645 		char *end = pos + sizeof(str);
2646 		int j, res;
2647 
2648 		for (j = 0; j < mode->num_channels; j++) {
2649 			struct hostapd_channel_data *chan = &mode->channels[j];
2650 
2651 			if (is_6ghz_freq(chan->freq))
2652 				drv->uses_6ghz = true;
2653 			if (chan->freq >= 900 && chan->freq < 1000)
2654 				drv->uses_s1g = true;
2655 			res = os_snprintf(pos, end - pos, " %d%s%s%s",
2656 					  chan->freq,
2657 					  (chan->flag & HOSTAPD_CHAN_DISABLED) ?
2658 					  "[DISABLED]" : "",
2659 					  (chan->flag & HOSTAPD_CHAN_NO_IR) ?
2660 					  "[NO_IR]" : "",
2661 					  (chan->flag & HOSTAPD_CHAN_RADAR) ?
2662 					  "[RADAR]" : "");
2663 			if (os_snprintf_error(end - pos, res))
2664 				break;
2665 			pos += res;
2666 		}
2667 
2668 		*pos = '\0';
2669 		wpa_printf(MSG_DEBUG, "nl80211: Mode IEEE %s:%s",
2670 			   modestr(mode->mode), str);
2671 	}
2672 }
2673 
2674 
2675 struct hostapd_hw_modes *
2676 nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags,
2677 			    u8 *dfs_domain)
2678 {
2679 	u32 feat;
2680 	struct i802_bss *bss = priv;
2681 	struct wpa_driver_nl80211_data *drv = bss->drv;
2682 	int nl_flags = 0;
2683 	struct nl_msg *msg;
2684 	struct phy_info_arg result = {
2685 		.num_modes = num_modes,
2686 		.modes = NULL,
2687 		.last_mode = -1,
2688 		.failed = 0,
2689 		.dfs_domain = 0,
2690 	};
2691 
2692 	*num_modes = 0;
2693 	*flags = 0;
2694 	*dfs_domain = 0;
2695 
2696 	feat = get_nl80211_protocol_features(drv);
2697 	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
2698 		nl_flags = NLM_F_DUMP;
2699 	if (!(msg = nl80211_cmd_msg(bss, nl_flags, NL80211_CMD_GET_WIPHY)) ||
2700 	    nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
2701 		nlmsg_free(msg);
2702 		return NULL;
2703 	}
2704 
2705 	if (send_and_recv_resp(drv, msg, phy_info_handler, &result) == 0) {
2706 		struct hostapd_hw_modes *modes;
2707 
2708 		nl80211_set_regulatory_flags(drv, &result);
2709 		if (result.failed) {
2710 			int i;
2711 
2712 			for (i = 0; result.modes && i < *num_modes; i++) {
2713 				os_free(result.modes[i].channels);
2714 				os_free(result.modes[i].rates);
2715 			}
2716 			os_free(result.modes);
2717 			*num_modes = 0;
2718 			return NULL;
2719 		}
2720 
2721 		*dfs_domain = result.dfs_domain;
2722 
2723 		modes = wpa_driver_nl80211_postprocess_modes(result.modes,
2724 							     num_modes);
2725 		nl80211_dump_chan_list(drv, modes, *num_modes);
2726 		return modes;
2727 	}
2728 
2729 	return NULL;
2730 }
2731