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