• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Driver interaction with Linux nl80211/cfg80211 - Capabilities
3  * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
5  * Copyright (c) 2009-2010, Atheros Communications
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10 
11 #include "includes.h"
12 #include <netlink/genl/genl.h>
13 
14 #include "utils/common.h"
15 #include "common/ieee802_11_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 			int len = nla_len(attr);
824 			info->flags = os_malloc(len);
825 			if (info->flags != NULL) {
826 				os_memcpy(info->flags, nla_data(attr), len);
827 				info->flags_len = len;
828 			}
829 		}
830 		attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_CONCURRENCY_CAPA];
831 		if (attr)
832 			info->capa->conc_capab = nla_get_u32(attr);
833 
834 		attr = tb_vendor[
835 			QCA_WLAN_VENDOR_ATTR_MAX_CONCURRENT_CHANNELS_2_4_BAND];
836 		if (attr)
837 			info->capa->max_conc_chan_2_4 = nla_get_u32(attr);
838 
839 		attr = tb_vendor[
840 			QCA_WLAN_VENDOR_ATTR_MAX_CONCURRENT_CHANNELS_5_0_BAND];
841 		if (attr)
842 			info->capa->max_conc_chan_5_0 = nla_get_u32(attr);
843 	}
844 
845 	return NL_SKIP;
846 }
847 
848 
check_feature(enum qca_wlan_vendor_features feature,struct features_info * info)849 static int check_feature(enum qca_wlan_vendor_features feature,
850 			 struct features_info *info)
851 {
852 	size_t idx = feature / 8;
853 
854 	return (idx < info->flags_len) &&
855 		(info->flags[idx] & BIT(feature % 8));
856 }
857 
858 
qca_nl80211_get_features(struct wpa_driver_nl80211_data * drv)859 static void qca_nl80211_get_features(struct wpa_driver_nl80211_data *drv)
860 {
861 	struct nl_msg *msg;
862 	struct features_info info;
863 	int ret;
864 
865 	if (!drv->get_features_vendor_cmd_avail)
866 		return;
867 
868 	if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
869 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
870 	    nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
871 			QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES)) {
872 		nlmsg_free(msg);
873 		return;
874 	}
875 
876 	os_memset(&info, 0, sizeof(info));
877 	info.capa = &drv->capa;
878 	ret = send_and_recv_msgs(drv, msg, features_info_handler, &info);
879 	if (ret || !info.flags)
880 		return;
881 
882 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_KEY_MGMT_OFFLOAD, &info))
883 		drv->capa.flags |= WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD;
884 
885 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_SUPPORT_HW_MODE_ANY, &info))
886 		drv->capa.flags |= WPA_DRIVER_FLAGS_SUPPORT_HW_MODE_ANY;
887 
888 	if (check_feature(QCA_WLAN_VENDOR_FEATURE_OFFCHANNEL_SIMULTANEOUS,
889 			  &info))
890 		drv->capa.flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS;
891 	os_free(info.flags);
892 }
893 
894 #endif /* CONFIG_DRIVER_NL80211_QCA */
895 
896 
wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data * drv)897 int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
898 {
899 	struct wiphy_info_data info;
900 	if (wpa_driver_nl80211_get_info(drv, &info))
901 		return -1;
902 
903 	if (info.error)
904 		return -1;
905 
906 	drv->has_capability = 1;
907 	drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
908 		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
909 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
910 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
911 		WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B |
912 		WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192;
913 	drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
914 		WPA_DRIVER_AUTH_SHARED |
915 		WPA_DRIVER_AUTH_LEAP;
916 
917 	drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
918 	drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
919 	drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
920 
921 	/*
922 	 * As all cfg80211 drivers must support cases where the AP interface is
923 	 * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
924 	 * case that the user space daemon has crashed, they must be able to
925 	 * cleanup all stations and key entries in the AP tear down flow. Thus,
926 	 * this flag can/should always be set for cfg80211 drivers.
927 	 */
928 	drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
929 
930 	if (!info.device_ap_sme) {
931 		drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
932 
933 		/*
934 		 * No AP SME is currently assumed to also indicate no AP MLME
935 		 * in the driver/firmware.
936 		 */
937 		drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
938 	}
939 
940 	drv->device_ap_sme = info.device_ap_sme;
941 	drv->poll_command_supported = info.poll_command_supported;
942 	drv->data_tx_status = info.data_tx_status;
943 	drv->p2p_go_ctwindow_supported = info.p2p_go_ctwindow_supported;
944 	if (info.set_qos_map_supported)
945 		drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
946 	drv->have_low_prio_scan = info.have_low_prio_scan;
947 
948 	/*
949 	 * If poll command and tx status are supported, mac80211 is new enough
950 	 * to have everything we need to not need monitor interfaces.
951 	 */
952 	drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
953 
954 	if (drv->device_ap_sme && drv->use_monitor) {
955 		/*
956 		 * Non-mac80211 drivers may not support monitor interface.
957 		 * Make sure we do not get stuck with incorrect capability here
958 		 * by explicitly testing this.
959 		 */
960 		if (!info.monitor_supported) {
961 			wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
962 				   "with device_ap_sme since no monitor mode "
963 				   "support detected");
964 			drv->use_monitor = 0;
965 		}
966 	}
967 
968 	/*
969 	 * If we aren't going to use monitor interfaces, but the
970 	 * driver doesn't support data TX status, we won't get TX
971 	 * status for EAPOL frames.
972 	 */
973 	if (!drv->use_monitor && !info.data_tx_status)
974 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
975 
976 #ifdef CONFIG_DRIVER_NL80211_QCA
977 	qca_nl80211_check_dfs_capa(drv);
978 	qca_nl80211_get_features(drv);
979 
980 	/*
981 	 * To enable offchannel simultaneous support in wpa_supplicant, the
982 	 * underlying driver needs to support the same along with offchannel TX.
983 	 * Offchannel TX support is needed since remain_on_channel and
984 	 * action_tx use some common data structures and hence cannot be
985 	 * scheduled simultaneously.
986 	 */
987 	if (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
988 		drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS;
989 #endif /* CONFIG_DRIVER_NL80211_QCA */
990 
991 	return 0;
992 }
993 
994 
995 struct phy_info_arg {
996 	u16 *num_modes;
997 	struct hostapd_hw_modes *modes;
998 	int last_mode, last_chan_idx;
999 	int failed;
1000 };
1001 
phy_info_ht_capa(struct hostapd_hw_modes * mode,struct nlattr * capa,struct nlattr * ampdu_factor,struct nlattr * ampdu_density,struct nlattr * mcs_set)1002 static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
1003 			     struct nlattr *ampdu_factor,
1004 			     struct nlattr *ampdu_density,
1005 			     struct nlattr *mcs_set)
1006 {
1007 	if (capa)
1008 		mode->ht_capab = nla_get_u16(capa);
1009 
1010 	if (ampdu_factor)
1011 		mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
1012 
1013 	if (ampdu_density)
1014 		mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
1015 
1016 	if (mcs_set && nla_len(mcs_set) >= 16) {
1017 		u8 *mcs;
1018 		mcs = nla_data(mcs_set);
1019 		os_memcpy(mode->mcs_set, mcs, 16);
1020 	}
1021 }
1022 
1023 
phy_info_vht_capa(struct hostapd_hw_modes * mode,struct nlattr * capa,struct nlattr * mcs_set)1024 static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
1025 			      struct nlattr *capa,
1026 			      struct nlattr *mcs_set)
1027 {
1028 	if (capa)
1029 		mode->vht_capab = nla_get_u32(capa);
1030 
1031 	if (mcs_set && nla_len(mcs_set) >= 8) {
1032 		u8 *mcs;
1033 		mcs = nla_data(mcs_set);
1034 		os_memcpy(mode->vht_mcs_set, mcs, 8);
1035 	}
1036 }
1037 
1038 
phy_info_freq(struct hostapd_hw_modes * mode,struct hostapd_channel_data * chan,struct nlattr * tb_freq[])1039 static void phy_info_freq(struct hostapd_hw_modes *mode,
1040 			  struct hostapd_channel_data *chan,
1041 			  struct nlattr *tb_freq[])
1042 {
1043 	u8 channel;
1044 	chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1045 	chan->flag = 0;
1046 	chan->dfs_cac_ms = 0;
1047 	if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
1048 		chan->chan = channel;
1049 
1050 	if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1051 		chan->flag |= HOSTAPD_CHAN_DISABLED;
1052 	if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
1053 		chan->flag |= HOSTAPD_CHAN_NO_IR;
1054 	if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
1055 		chan->flag |= HOSTAPD_CHAN_RADAR;
1056 	if (tb_freq[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
1057 		chan->flag |= HOSTAPD_CHAN_INDOOR_ONLY;
1058 	if (tb_freq[NL80211_FREQUENCY_ATTR_GO_CONCURRENT])
1059 		chan->flag |= HOSTAPD_CHAN_GO_CONCURRENT;
1060 
1061 	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
1062 		enum nl80211_dfs_state state =
1063 			nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
1064 
1065 		switch (state) {
1066 		case NL80211_DFS_USABLE:
1067 			chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
1068 			break;
1069 		case NL80211_DFS_AVAILABLE:
1070 			chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
1071 			break;
1072 		case NL80211_DFS_UNAVAILABLE:
1073 			chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
1074 			break;
1075 		}
1076 	}
1077 
1078 	if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
1079 		chan->dfs_cac_ms = nla_get_u32(
1080 			tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
1081 	}
1082 }
1083 
1084 
phy_info_freqs(struct phy_info_arg * phy_info,struct hostapd_hw_modes * mode,struct nlattr * tb)1085 static int phy_info_freqs(struct phy_info_arg *phy_info,
1086 			  struct hostapd_hw_modes *mode, struct nlattr *tb)
1087 {
1088 	static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1089 		[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1090 		[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1091 		[NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
1092 		[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1093 		[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
1094 		[NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
1095 	};
1096 	int new_channels = 0;
1097 	struct hostapd_channel_data *channel;
1098 	struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1099 	struct nlattr *nl_freq;
1100 	int rem_freq, idx;
1101 
1102 	if (tb == NULL)
1103 		return NL_OK;
1104 
1105 	nla_for_each_nested(nl_freq, tb, rem_freq) {
1106 		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
1107 			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
1108 		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1109 			continue;
1110 		new_channels++;
1111 	}
1112 
1113 	channel = os_realloc_array(mode->channels,
1114 				   mode->num_channels + new_channels,
1115 				   sizeof(struct hostapd_channel_data));
1116 	if (!channel)
1117 		return NL_STOP;
1118 
1119 	mode->channels = channel;
1120 	mode->num_channels += new_channels;
1121 
1122 	idx = phy_info->last_chan_idx;
1123 
1124 	nla_for_each_nested(nl_freq, tb, rem_freq) {
1125 		nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
1126 			  nla_data(nl_freq), nla_len(nl_freq), freq_policy);
1127 		if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1128 			continue;
1129 		phy_info_freq(mode, &mode->channels[idx], tb_freq);
1130 		idx++;
1131 	}
1132 	phy_info->last_chan_idx = idx;
1133 
1134 	return NL_OK;
1135 }
1136 
1137 
phy_info_rates(struct hostapd_hw_modes * mode,struct nlattr * tb)1138 static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
1139 {
1140 	static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1141 		[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1142 		[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
1143 		{ .type = NLA_FLAG },
1144 	};
1145 	struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1146 	struct nlattr *nl_rate;
1147 	int rem_rate, idx;
1148 
1149 	if (tb == NULL)
1150 		return NL_OK;
1151 
1152 	nla_for_each_nested(nl_rate, tb, rem_rate) {
1153 		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1154 			  nla_data(nl_rate), nla_len(nl_rate),
1155 			  rate_policy);
1156 		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1157 			continue;
1158 		mode->num_rates++;
1159 	}
1160 
1161 	mode->rates = os_calloc(mode->num_rates, sizeof(int));
1162 	if (!mode->rates)
1163 		return NL_STOP;
1164 
1165 	idx = 0;
1166 
1167 	nla_for_each_nested(nl_rate, tb, rem_rate) {
1168 		nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1169 			  nla_data(nl_rate), nla_len(nl_rate),
1170 			  rate_policy);
1171 		if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1172 			continue;
1173 		mode->rates[idx] = nla_get_u32(
1174 			tb_rate[NL80211_BITRATE_ATTR_RATE]);
1175 		idx++;
1176 	}
1177 
1178 	return NL_OK;
1179 }
1180 
1181 
phy_info_band(struct phy_info_arg * phy_info,struct nlattr * nl_band)1182 static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
1183 {
1184 	struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1185 	struct hostapd_hw_modes *mode;
1186 	int ret;
1187 
1188 	if (phy_info->last_mode != nl_band->nla_type) {
1189 		mode = os_realloc_array(phy_info->modes,
1190 					*phy_info->num_modes + 1,
1191 					sizeof(*mode));
1192 		if (!mode) {
1193 			phy_info->failed = 1;
1194 			return NL_STOP;
1195 		}
1196 		phy_info->modes = mode;
1197 
1198 		mode = &phy_info->modes[*(phy_info->num_modes)];
1199 		os_memset(mode, 0, sizeof(*mode));
1200 		mode->mode = NUM_HOSTAPD_MODES;
1201 		mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
1202 			HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
1203 
1204 		/*
1205 		 * Unsupported VHT MCS stream is defined as value 3, so the VHT
1206 		 * MCS RX/TX map must be initialized with 0xffff to mark all 8
1207 		 * possible streams as unsupported. This will be overridden if
1208 		 * driver advertises VHT support.
1209 		 */
1210 		mode->vht_mcs_set[0] = 0xff;
1211 		mode->vht_mcs_set[1] = 0xff;
1212 		mode->vht_mcs_set[4] = 0xff;
1213 		mode->vht_mcs_set[5] = 0xff;
1214 
1215 		*(phy_info->num_modes) += 1;
1216 		phy_info->last_mode = nl_band->nla_type;
1217 		phy_info->last_chan_idx = 0;
1218 	} else
1219 		mode = &phy_info->modes[*(phy_info->num_modes) - 1];
1220 
1221 	nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1222 		  nla_len(nl_band), NULL);
1223 
1224 	phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
1225 			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
1226 			 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
1227 			 tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
1228 	phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
1229 			  tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
1230 	ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
1231 	if (ret == NL_OK)
1232 		ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
1233 	if (ret != NL_OK) {
1234 		phy_info->failed = 1;
1235 		return ret;
1236 	}
1237 
1238 	return NL_OK;
1239 }
1240 
1241 
phy_info_handler(struct nl_msg * msg,void * arg)1242 static int phy_info_handler(struct nl_msg *msg, void *arg)
1243 {
1244 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1245 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1246 	struct phy_info_arg *phy_info = arg;
1247 	struct nlattr *nl_band;
1248 	int rem_band;
1249 
1250 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1251 		  genlmsg_attrlen(gnlh, 0), NULL);
1252 
1253 	if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1254 		return NL_SKIP;
1255 
1256 	nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
1257 	{
1258 		int res = phy_info_band(phy_info, nl_band);
1259 		if (res != NL_OK)
1260 			return res;
1261 	}
1262 
1263 	return NL_SKIP;
1264 }
1265 
1266 
1267 static struct hostapd_hw_modes *
wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes * modes,u16 * num_modes)1268 wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
1269 				     u16 *num_modes)
1270 {
1271 	u16 m;
1272 	struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1273 	int i, mode11g_idx = -1;
1274 
1275 	/* heuristic to set up modes */
1276 	for (m = 0; m < *num_modes; m++) {
1277 		if (!modes[m].num_channels)
1278 			continue;
1279 		if (modes[m].channels[0].freq < 4000) {
1280 			modes[m].mode = HOSTAPD_MODE_IEEE80211B;
1281 			for (i = 0; i < modes[m].num_rates; i++) {
1282 				if (modes[m].rates[i] > 200) {
1283 					modes[m].mode = HOSTAPD_MODE_IEEE80211G;
1284 					break;
1285 				}
1286 			}
1287 		} else if (modes[m].channels[0].freq > 50000)
1288 			modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
1289 		else
1290 			modes[m].mode = HOSTAPD_MODE_IEEE80211A;
1291 	}
1292 
1293 	/* If only 802.11g mode is included, use it to construct matching
1294 	 * 802.11b mode data. */
1295 
1296 	for (m = 0; m < *num_modes; m++) {
1297 		if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1298 			return modes; /* 802.11b already included */
1299 		if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1300 			mode11g_idx = m;
1301 	}
1302 
1303 	if (mode11g_idx < 0)
1304 		return modes; /* 2.4 GHz band not supported at all */
1305 
1306 	nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
1307 	if (nmodes == NULL)
1308 		return modes; /* Could not add 802.11b mode */
1309 
1310 	mode = &nmodes[*num_modes];
1311 	os_memset(mode, 0, sizeof(*mode));
1312 	(*num_modes)++;
1313 	modes = nmodes;
1314 
1315 	mode->mode = HOSTAPD_MODE_IEEE80211B;
1316 
1317 	mode11g = &modes[mode11g_idx];
1318 	mode->num_channels = mode11g->num_channels;
1319 	mode->channels = os_malloc(mode11g->num_channels *
1320 				   sizeof(struct hostapd_channel_data));
1321 	if (mode->channels == NULL) {
1322 		(*num_modes)--;
1323 		return modes; /* Could not add 802.11b mode */
1324 	}
1325 	os_memcpy(mode->channels, mode11g->channels,
1326 		  mode11g->num_channels * sizeof(struct hostapd_channel_data));
1327 
1328 	mode->num_rates = 0;
1329 	mode->rates = os_malloc(4 * sizeof(int));
1330 	if (mode->rates == NULL) {
1331 		os_free(mode->channels);
1332 		(*num_modes)--;
1333 		return modes; /* Could not add 802.11b mode */
1334 	}
1335 
1336 	for (i = 0; i < mode11g->num_rates; i++) {
1337 		if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
1338 		    mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
1339 			continue;
1340 		mode->rates[mode->num_rates] = mode11g->rates[i];
1341 		mode->num_rates++;
1342 		if (mode->num_rates == 4)
1343 			break;
1344 	}
1345 
1346 	if (mode->num_rates == 0) {
1347 		os_free(mode->channels);
1348 		os_free(mode->rates);
1349 		(*num_modes)--;
1350 		return modes; /* No 802.11b rates */
1351 	}
1352 
1353 	wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1354 		   "information");
1355 
1356 	return modes;
1357 }
1358 
1359 
nl80211_set_ht40_mode(struct hostapd_hw_modes * mode,int start,int end)1360 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
1361 				  int end)
1362 {
1363 	int c;
1364 
1365 	for (c = 0; c < mode->num_channels; c++) {
1366 		struct hostapd_channel_data *chan = &mode->channels[c];
1367 		if (chan->freq - 10 >= start && chan->freq + 10 <= end)
1368 			chan->flag |= HOSTAPD_CHAN_HT40;
1369 	}
1370 }
1371 
1372 
nl80211_set_ht40_mode_sec(struct hostapd_hw_modes * mode,int start,int end)1373 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
1374 				      int end)
1375 {
1376 	int c;
1377 
1378 	for (c = 0; c < mode->num_channels; c++) {
1379 		struct hostapd_channel_data *chan = &mode->channels[c];
1380 		if (!(chan->flag & HOSTAPD_CHAN_HT40))
1381 			continue;
1382 		if (chan->freq - 30 >= start && chan->freq - 10 <= end)
1383 			chan->flag |= HOSTAPD_CHAN_HT40MINUS;
1384 		if (chan->freq + 10 >= start && chan->freq + 30 <= end)
1385 			chan->flag |= HOSTAPD_CHAN_HT40PLUS;
1386 	}
1387 }
1388 
1389 
nl80211_reg_rule_max_eirp(u32 start,u32 end,u32 max_eirp,struct phy_info_arg * results)1390 static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
1391 				      struct phy_info_arg *results)
1392 {
1393 	u16 m;
1394 
1395 	for (m = 0; m < *results->num_modes; m++) {
1396 		int c;
1397 		struct hostapd_hw_modes *mode = &results->modes[m];
1398 
1399 		for (c = 0; c < mode->num_channels; c++) {
1400 			struct hostapd_channel_data *chan = &mode->channels[c];
1401 			if ((u32) chan->freq - 10 >= start &&
1402 			    (u32) chan->freq + 10 <= end)
1403 				chan->max_tx_power = max_eirp;
1404 		}
1405 	}
1406 }
1407 
1408 
nl80211_reg_rule_ht40(u32 start,u32 end,struct phy_info_arg * results)1409 static void nl80211_reg_rule_ht40(u32 start, u32 end,
1410 				  struct phy_info_arg *results)
1411 {
1412 	u16 m;
1413 
1414 	for (m = 0; m < *results->num_modes; m++) {
1415 		if (!(results->modes[m].ht_capab &
1416 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1417 			continue;
1418 		nl80211_set_ht40_mode(&results->modes[m], start, end);
1419 	}
1420 }
1421 
1422 
nl80211_reg_rule_sec(struct nlattr * tb[],struct phy_info_arg * results)1423 static void nl80211_reg_rule_sec(struct nlattr *tb[],
1424 				 struct phy_info_arg *results)
1425 {
1426 	u32 start, end, max_bw;
1427 	u16 m;
1428 
1429 	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1430 	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
1431 	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
1432 		return;
1433 
1434 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1435 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1436 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1437 
1438 	if (max_bw < 20)
1439 		return;
1440 
1441 	for (m = 0; m < *results->num_modes; m++) {
1442 		if (!(results->modes[m].ht_capab &
1443 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1444 			continue;
1445 		nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
1446 	}
1447 }
1448 
1449 
nl80211_set_vht_mode(struct hostapd_hw_modes * mode,int start,int end,int max_bw)1450 static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
1451 				 int end, int max_bw)
1452 {
1453 	int c;
1454 
1455 	for (c = 0; c < mode->num_channels; c++) {
1456 		struct hostapd_channel_data *chan = &mode->channels[c];
1457 		if (chan->freq - 10 >= start && chan->freq + 70 <= end)
1458 			chan->flag |= HOSTAPD_CHAN_VHT_10_70;
1459 
1460 		if (chan->freq - 30 >= start && chan->freq + 50 <= end)
1461 			chan->flag |= HOSTAPD_CHAN_VHT_30_50;
1462 
1463 		if (chan->freq - 50 >= start && chan->freq + 30 <= end)
1464 			chan->flag |= HOSTAPD_CHAN_VHT_50_30;
1465 
1466 		if (chan->freq - 70 >= start && chan->freq + 10 <= end)
1467 			chan->flag |= HOSTAPD_CHAN_VHT_70_10;
1468 
1469 		if (max_bw >= 160) {
1470 			if (chan->freq - 10 >= start && chan->freq + 150 <= end)
1471 				chan->flag |= HOSTAPD_CHAN_VHT_10_150;
1472 
1473 			if (chan->freq - 30 >= start && chan->freq + 130 <= end)
1474 				chan->flag |= HOSTAPD_CHAN_VHT_30_130;
1475 
1476 			if (chan->freq - 50 >= start && chan->freq + 110 <= end)
1477 				chan->flag |= HOSTAPD_CHAN_VHT_50_110;
1478 
1479 			if (chan->freq - 70 >= start && chan->freq + 90 <= end)
1480 				chan->flag |= HOSTAPD_CHAN_VHT_70_90;
1481 
1482 			if (chan->freq - 90 >= start && chan->freq + 70 <= end)
1483 				chan->flag |= HOSTAPD_CHAN_VHT_90_70;
1484 
1485 			if (chan->freq - 110 >= start && chan->freq + 50 <= end)
1486 				chan->flag |= HOSTAPD_CHAN_VHT_110_50;
1487 
1488 			if (chan->freq - 130 >= start && chan->freq + 30 <= end)
1489 				chan->flag |= HOSTAPD_CHAN_VHT_130_30;
1490 
1491 			if (chan->freq - 150 >= start && chan->freq + 10 <= end)
1492 				chan->flag |= HOSTAPD_CHAN_VHT_150_10;
1493 		}
1494 	}
1495 }
1496 
1497 
nl80211_reg_rule_vht(struct nlattr * tb[],struct phy_info_arg * results)1498 static void nl80211_reg_rule_vht(struct nlattr *tb[],
1499 				 struct phy_info_arg *results)
1500 {
1501 	u32 start, end, max_bw;
1502 	u16 m;
1503 
1504 	if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1505 	    tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
1506 	    tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
1507 		return;
1508 
1509 	start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1510 	end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1511 	max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1512 
1513 	if (max_bw < 80)
1514 		return;
1515 
1516 	for (m = 0; m < *results->num_modes; m++) {
1517 		if (!(results->modes[m].ht_capab &
1518 		      HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1519 			continue;
1520 		/* TODO: use a real VHT support indication */
1521 		if (!results->modes[m].vht_capab)
1522 			continue;
1523 
1524 		nl80211_set_vht_mode(&results->modes[m], start, end, max_bw);
1525 	}
1526 }
1527 
1528 
dfs_domain_name(enum nl80211_dfs_regions region)1529 static const char * dfs_domain_name(enum nl80211_dfs_regions region)
1530 {
1531 	switch (region) {
1532 	case NL80211_DFS_UNSET:
1533 		return "DFS-UNSET";
1534 	case NL80211_DFS_FCC:
1535 		return "DFS-FCC";
1536 	case NL80211_DFS_ETSI:
1537 		return "DFS-ETSI";
1538 	case NL80211_DFS_JP:
1539 		return "DFS-JP";
1540 	default:
1541 		return "DFS-invalid";
1542 	}
1543 }
1544 
1545 
nl80211_get_reg(struct nl_msg * msg,void * arg)1546 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
1547 {
1548 	struct phy_info_arg *results = arg;
1549 	struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1550 	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1551 	struct nlattr *nl_rule;
1552 	struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
1553 	int rem_rule;
1554 	static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1555 		[NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1556 		[NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1557 		[NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1558 		[NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1559 		[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1560 		[NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1561 	};
1562 
1563 	nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1564 		  genlmsg_attrlen(gnlh, 0), NULL);
1565 	if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
1566 	    !tb_msg[NL80211_ATTR_REG_RULES]) {
1567 		wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
1568 			   "available");
1569 		return NL_SKIP;
1570 	}
1571 
1572 	if (tb_msg[NL80211_ATTR_DFS_REGION]) {
1573 		enum nl80211_dfs_regions dfs_domain;
1574 		dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
1575 		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
1576 			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
1577 			   dfs_domain_name(dfs_domain));
1578 	} else {
1579 		wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
1580 			   (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
1581 	}
1582 
1583 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1584 	{
1585 		u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
1586 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1587 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1588 		if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1589 		    tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
1590 			continue;
1591 		start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1592 		end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1593 		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1594 			max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
1595 		if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1596 			max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1597 		if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
1598 			flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
1599 
1600 		wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
1601 			   start, end, max_bw, max_eirp,
1602 			   flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
1603 			   flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
1604 			   flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
1605 			   flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
1606 			   "",
1607 			   flags & NL80211_RRF_DFS ? " (DFS)" : "",
1608 			   flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
1609 			   flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
1610 			   flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
1611 		if (max_bw >= 40)
1612 			nl80211_reg_rule_ht40(start, end, results);
1613 		if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1614 			nl80211_reg_rule_max_eirp(start, end, max_eirp,
1615 						  results);
1616 	}
1617 
1618 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1619 	{
1620 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1621 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1622 		nl80211_reg_rule_sec(tb_rule, results);
1623 	}
1624 
1625 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1626 	{
1627 		nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1628 			  nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1629 		nl80211_reg_rule_vht(tb_rule, results);
1630 	}
1631 
1632 	return NL_SKIP;
1633 }
1634 
1635 
nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data * drv,struct phy_info_arg * results)1636 static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
1637 					struct phy_info_arg *results)
1638 {
1639 	struct nl_msg *msg;
1640 
1641 	msg = nlmsg_alloc();
1642 	if (!msg)
1643 		return -ENOMEM;
1644 
1645 	nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1646 	return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
1647 }
1648 
1649 
1650 struct hostapd_hw_modes *
nl80211_get_hw_feature_data(void * priv,u16 * num_modes,u16 * flags)1651 nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
1652 {
1653 	u32 feat;
1654 	struct i802_bss *bss = priv;
1655 	struct wpa_driver_nl80211_data *drv = bss->drv;
1656 	int nl_flags = 0;
1657 	struct nl_msg *msg;
1658 	struct phy_info_arg result = {
1659 		.num_modes = num_modes,
1660 		.modes = NULL,
1661 		.last_mode = -1,
1662 		.failed = 0,
1663 	};
1664 
1665 	*num_modes = 0;
1666 	*flags = 0;
1667 
1668 	feat = get_nl80211_protocol_features(drv);
1669 	if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
1670 		nl_flags = NLM_F_DUMP;
1671 	if (!(msg = nl80211_cmd_msg(bss, nl_flags, NL80211_CMD_GET_WIPHY)) ||
1672 	    nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
1673 		nlmsg_free(msg);
1674 		return NULL;
1675 	}
1676 
1677 	if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
1678 		nl80211_set_regulatory_flags(drv, &result);
1679 		if (result.failed) {
1680 			int i;
1681 
1682 			for (i = 0; result.modes && i < *num_modes; i++) {
1683 				os_free(result.modes[i].channels);
1684 				os_free(result.modes[i].rates);
1685 			}
1686 			os_free(result.modes);
1687 			return NULL;
1688 		}
1689 		return wpa_driver_nl80211_postprocess_modes(result.modes,
1690 							    num_modes);
1691 	}
1692 
1693 	return NULL;
1694 }
1695