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