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