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