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