5c71603c9e4f3a90b5f66d89a13e0063593ad6dd
[mech_eap.git] / src / drivers / driver_nl80211_capa.c
1 /*
2  * Driver interaction with Linux nl80211/cfg80211 - Capabilities
3  * Copyright (c) 2002-2014, 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]));
524                 if (drv->extended_capa_mask) {
525                         os_memcpy(drv->extended_capa_mask,
526                                   nla_data(tb[NL80211_ATTR_EXT_CAPA]),
527                                   nla_len(tb[NL80211_ATTR_EXT_CAPA]));
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_ROAMING:
548                                 drv->roaming_vendor_cmd_avail = 1;
549                                 break;
550                         case QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY:
551                                 drv->dfs_vendor_cmd_avail = 1;
552                                 break;
553                         case QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES:
554                                 drv->get_features_vendor_cmd_avail = 1;
555                                 break;
556                         case QCA_NL80211_VENDOR_SUBCMD_DO_ACS:
557                                 drv->capa.flags |= WPA_DRIVER_FLAGS_ACS_OFFLOAD;
558                                 break;
559                         }
560
561                         wpa_printf(MSG_DEBUG, "nl80211: Supported vendor command: vendor_id=0x%x subcmd=%u",
562                                    vinfo->vendor_id, vinfo->subcmd);
563                 }
564         }
565
566         if (tb[NL80211_ATTR_VENDOR_EVENTS]) {
567                 struct nlattr *nl;
568                 int rem;
569
570                 nla_for_each_nested(nl, tb[NL80211_ATTR_VENDOR_EVENTS], rem) {
571                         struct nl80211_vendor_cmd_info *vinfo;
572                         if (nla_len(nl) != sizeof(*vinfo)) {
573                                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected vendor data info");
574                                 continue;
575                         }
576                         vinfo = nla_data(nl);
577                         wpa_printf(MSG_DEBUG, "nl80211: Supported vendor event: vendor_id=0x%x subcmd=%u",
578                                    vinfo->vendor_id, vinfo->subcmd);
579                 }
580         }
581
582         wiphy_info_wowlan_triggers(capa,
583                                    tb[NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED]);
584
585         if (tb[NL80211_ATTR_MAX_AP_ASSOC_STA])
586                 capa->max_stations =
587                         nla_get_u32(tb[NL80211_ATTR_MAX_AP_ASSOC_STA]);
588
589         return NL_SKIP;
590 }
591
592
593 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
594                                        struct wiphy_info_data *info)
595 {
596         u32 feat;
597         struct nl_msg *msg;
598         int flags = 0;
599
600         os_memset(info, 0, sizeof(*info));
601         info->capa = &drv->capa;
602         info->drv = drv;
603
604         feat = get_nl80211_protocol_features(drv);
605         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
606                 flags = NLM_F_DUMP;
607         msg = nl80211_cmd_msg(drv->first_bss, flags, NL80211_CMD_GET_WIPHY);
608         if (!msg || nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
609                 nlmsg_free(msg);
610                 return -1;
611         }
612
613         if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
614                 return -1;
615
616         if (info->auth_supported)
617                 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
618         else if (!info->connect_supported) {
619                 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
620                            "authentication/association or connect commands");
621                 info->error = 1;
622         }
623
624         if (info->p2p_go_supported && info->p2p_client_supported)
625                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
626         if (info->p2p_concurrent) {
627                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
628                            "interface (driver advertised support)");
629                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
630                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
631         }
632         if (info->num_multichan_concurrent > 1) {
633                 wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
634                            "concurrent (driver advertised support)");
635                 drv->capa.num_multichan_concurrent =
636                         info->num_multichan_concurrent;
637         }
638         if (drv->capa.flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
639                 wpa_printf(MSG_DEBUG, "nl80211: use P2P_DEVICE support");
640
641         /* default to 5000 since early versions of mac80211 don't set it */
642         if (!drv->capa.max_remain_on_chan)
643                 drv->capa.max_remain_on_chan = 5000;
644
645         if (info->channel_switch_supported)
646                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_CSA;
647         drv->capa.wmm_ac_supported = info->wmm_ac_supported;
648
649         drv->capa.mac_addr_rand_sched_scan_supported =
650                 info->mac_addr_rand_sched_scan_supported;
651         drv->capa.mac_addr_rand_scan_supported =
652                 info->mac_addr_rand_scan_supported;
653
654         return 0;
655 }
656
657
658 static int dfs_info_handler(struct nl_msg *msg, void *arg)
659 {
660         struct nlattr *tb[NL80211_ATTR_MAX + 1];
661         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
662         int *dfs_capability_ptr = arg;
663
664         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
665                   genlmsg_attrlen(gnlh, 0), NULL);
666
667         if (tb[NL80211_ATTR_VENDOR_DATA]) {
668                 struct nlattr *nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
669                 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
670
671                 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
672                           nla_data(nl_vend), nla_len(nl_vend), NULL);
673
674                 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]) {
675                         u32 val;
676                         val = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_DFS]);
677                         wpa_printf(MSG_DEBUG, "nl80211: DFS offload capability: %u",
678                                    val);
679                         *dfs_capability_ptr = val;
680                 }
681         }
682
683         return NL_SKIP;
684 }
685
686
687 static void qca_nl80211_check_dfs_capa(struct wpa_driver_nl80211_data *drv)
688 {
689         struct nl_msg *msg;
690         int dfs_capability = 0;
691         int ret;
692
693         if (!drv->dfs_vendor_cmd_avail)
694                 return;
695
696         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
697             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
698             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
699                         QCA_NL80211_VENDOR_SUBCMD_DFS_CAPABILITY)) {
700                 nlmsg_free(msg);
701                 return;
702         }
703
704         ret = send_and_recv_msgs(drv, msg, dfs_info_handler, &dfs_capability);
705         if (!ret && dfs_capability)
706                 drv->capa.flags |= WPA_DRIVER_FLAGS_DFS_OFFLOAD;
707 }
708
709
710 struct features_info {
711         u8 *flags;
712         size_t flags_len;
713 };
714
715
716 static int features_info_handler(struct nl_msg *msg, void *arg)
717 {
718         struct nlattr *tb[NL80211_ATTR_MAX + 1];
719         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
720         struct features_info *info = arg;
721         struct nlattr *nl_vend, *attr;
722
723         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
724                   genlmsg_attrlen(gnlh, 0), NULL);
725
726         nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
727         if (nl_vend) {
728                 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
729
730                 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
731                           nla_data(nl_vend), nla_len(nl_vend), NULL);
732
733                 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_FEATURE_FLAGS];
734                 if (attr) {
735                         info->flags = nla_data(attr);
736                         info->flags_len = nla_len(attr);
737                 }
738         }
739
740         return NL_SKIP;
741 }
742
743
744 static int check_feature(enum qca_wlan_vendor_features feature,
745                          struct features_info *info)
746 {
747         size_t idx = feature / 8;
748
749         return (idx < info->flags_len) &&
750                 (info->flags[idx] & BIT(feature % 8));
751 }
752
753
754 static void qca_nl80211_get_features(struct wpa_driver_nl80211_data *drv)
755 {
756         struct nl_msg *msg;
757         struct features_info info;
758         int ret;
759
760         if (!drv->get_features_vendor_cmd_avail)
761                 return;
762
763         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
764             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
765             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
766                         QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES)) {
767                 nlmsg_free(msg);
768                 return;
769         }
770
771         os_memset(&info, 0, sizeof(info));
772         ret = send_and_recv_msgs(drv, msg, features_info_handler, &info);
773         if (ret || !info.flags)
774                 return;
775
776         if (check_feature(QCA_WLAN_VENDOR_FEATURE_KEY_MGMT_OFFLOAD, &info))
777                 drv->capa.flags |= WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD;
778 }
779
780
781 int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
782 {
783         struct wiphy_info_data info;
784         if (wpa_driver_nl80211_get_info(drv, &info))
785                 return -1;
786
787         if (info.error)
788                 return -1;
789
790         drv->has_capability = 1;
791         drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
792                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
793                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
794                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
795         drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
796                 WPA_DRIVER_AUTH_SHARED |
797                 WPA_DRIVER_AUTH_LEAP;
798
799         drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
800         drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
801         drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
802
803         /*
804          * As all cfg80211 drivers must support cases where the AP interface is
805          * removed without the knowledge of wpa_supplicant/hostapd, e.g., in
806          * case that the user space daemon has crashed, they must be able to
807          * cleanup all stations and key entries in the AP tear down flow. Thus,
808          * this flag can/should always be set for cfg80211 drivers.
809          */
810         drv->capa.flags |= WPA_DRIVER_FLAGS_AP_TEARDOWN_SUPPORT;
811
812         if (!info.device_ap_sme) {
813                 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
814
815                 /*
816                  * No AP SME is currently assumed to also indicate no AP MLME
817                  * in the driver/firmware.
818                  */
819                 drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
820         }
821
822         drv->device_ap_sme = info.device_ap_sme;
823         drv->poll_command_supported = info.poll_command_supported;
824         drv->data_tx_status = info.data_tx_status;
825         if (info.set_qos_map_supported)
826                 drv->capa.flags |= WPA_DRIVER_FLAGS_QOS_MAPPING;
827         drv->have_low_prio_scan = info.have_low_prio_scan;
828
829         /*
830          * If poll command and tx status are supported, mac80211 is new enough
831          * to have everything we need to not need monitor interfaces.
832          */
833         drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
834
835         if (drv->device_ap_sme && drv->use_monitor) {
836                 /*
837                  * Non-mac80211 drivers may not support monitor interface.
838                  * Make sure we do not get stuck with incorrect capability here
839                  * by explicitly testing this.
840                  */
841                 if (!info.monitor_supported) {
842                         wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
843                                    "with device_ap_sme since no monitor mode "
844                                    "support detected");
845                         drv->use_monitor = 0;
846                 }
847         }
848
849         /*
850          * If we aren't going to use monitor interfaces, but the
851          * driver doesn't support data TX status, we won't get TX
852          * status for EAPOL frames.
853          */
854         if (!drv->use_monitor && !info.data_tx_status)
855                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
856
857         qca_nl80211_check_dfs_capa(drv);
858         qca_nl80211_get_features(drv);
859
860         return 0;
861 }
862
863
864 struct phy_info_arg {
865         u16 *num_modes;
866         struct hostapd_hw_modes *modes;
867         int last_mode, last_chan_idx;
868 };
869
870 static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
871                              struct nlattr *ampdu_factor,
872                              struct nlattr *ampdu_density,
873                              struct nlattr *mcs_set)
874 {
875         if (capa)
876                 mode->ht_capab = nla_get_u16(capa);
877
878         if (ampdu_factor)
879                 mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
880
881         if (ampdu_density)
882                 mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
883
884         if (mcs_set && nla_len(mcs_set) >= 16) {
885                 u8 *mcs;
886                 mcs = nla_data(mcs_set);
887                 os_memcpy(mode->mcs_set, mcs, 16);
888         }
889 }
890
891
892 static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
893                               struct nlattr *capa,
894                               struct nlattr *mcs_set)
895 {
896         if (capa)
897                 mode->vht_capab = nla_get_u32(capa);
898
899         if (mcs_set && nla_len(mcs_set) >= 8) {
900                 u8 *mcs;
901                 mcs = nla_data(mcs_set);
902                 os_memcpy(mode->vht_mcs_set, mcs, 8);
903         }
904 }
905
906
907 static void phy_info_freq(struct hostapd_hw_modes *mode,
908                           struct hostapd_channel_data *chan,
909                           struct nlattr *tb_freq[])
910 {
911         u8 channel;
912         chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
913         chan->flag = 0;
914         chan->dfs_cac_ms = 0;
915         if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
916                 chan->chan = channel;
917
918         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
919                 chan->flag |= HOSTAPD_CHAN_DISABLED;
920         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
921                 chan->flag |= HOSTAPD_CHAN_NO_IR;
922         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
923                 chan->flag |= HOSTAPD_CHAN_RADAR;
924         if (tb_freq[NL80211_FREQUENCY_ATTR_INDOOR_ONLY])
925                 chan->flag |= HOSTAPD_CHAN_INDOOR_ONLY;
926         if (tb_freq[NL80211_FREQUENCY_ATTR_GO_CONCURRENT])
927                 chan->flag |= HOSTAPD_CHAN_GO_CONCURRENT;
928
929         if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
930                 enum nl80211_dfs_state state =
931                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
932
933                 switch (state) {
934                 case NL80211_DFS_USABLE:
935                         chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
936                         break;
937                 case NL80211_DFS_AVAILABLE:
938                         chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
939                         break;
940                 case NL80211_DFS_UNAVAILABLE:
941                         chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
942                         break;
943                 }
944         }
945
946         if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]) {
947                 chan->dfs_cac_ms = nla_get_u32(
948                         tb_freq[NL80211_FREQUENCY_ATTR_DFS_CAC_TIME]);
949         }
950 }
951
952
953 static int phy_info_freqs(struct phy_info_arg *phy_info,
954                           struct hostapd_hw_modes *mode, struct nlattr *tb)
955 {
956         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
957                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
958                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
959                 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
960                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
961                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
962                 [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
963         };
964         int new_channels = 0;
965         struct hostapd_channel_data *channel;
966         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
967         struct nlattr *nl_freq;
968         int rem_freq, idx;
969
970         if (tb == NULL)
971                 return NL_OK;
972
973         nla_for_each_nested(nl_freq, tb, rem_freq) {
974                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
975                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
976                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
977                         continue;
978                 new_channels++;
979         }
980
981         channel = os_realloc_array(mode->channels,
982                                    mode->num_channels + new_channels,
983                                    sizeof(struct hostapd_channel_data));
984         if (!channel)
985                 return NL_SKIP;
986
987         mode->channels = channel;
988         mode->num_channels += new_channels;
989
990         idx = phy_info->last_chan_idx;
991
992         nla_for_each_nested(nl_freq, tb, rem_freq) {
993                 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
994                           nla_data(nl_freq), nla_len(nl_freq), freq_policy);
995                 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
996                         continue;
997                 phy_info_freq(mode, &mode->channels[idx], tb_freq);
998                 idx++;
999         }
1000         phy_info->last_chan_idx = idx;
1001
1002         return NL_OK;
1003 }
1004
1005
1006 static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
1007 {
1008         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1009                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1010                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
1011                 { .type = NLA_FLAG },
1012         };
1013         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1014         struct nlattr *nl_rate;
1015         int rem_rate, idx;
1016
1017         if (tb == NULL)
1018                 return NL_OK;
1019
1020         nla_for_each_nested(nl_rate, tb, rem_rate) {
1021                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1022                           nla_data(nl_rate), nla_len(nl_rate),
1023                           rate_policy);
1024                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1025                         continue;
1026                 mode->num_rates++;
1027         }
1028
1029         mode->rates = os_calloc(mode->num_rates, sizeof(int));
1030         if (!mode->rates)
1031                 return NL_SKIP;
1032
1033         idx = 0;
1034
1035         nla_for_each_nested(nl_rate, tb, rem_rate) {
1036                 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
1037                           nla_data(nl_rate), nla_len(nl_rate),
1038                           rate_policy);
1039                 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1040                         continue;
1041                 mode->rates[idx] = nla_get_u32(
1042                         tb_rate[NL80211_BITRATE_ATTR_RATE]);
1043                 idx++;
1044         }
1045
1046         return NL_OK;
1047 }
1048
1049
1050 static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
1051 {
1052         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1053         struct hostapd_hw_modes *mode;
1054         int ret;
1055
1056         if (phy_info->last_mode != nl_band->nla_type) {
1057                 mode = os_realloc_array(phy_info->modes,
1058                                         *phy_info->num_modes + 1,
1059                                         sizeof(*mode));
1060                 if (!mode)
1061                         return NL_SKIP;
1062                 phy_info->modes = mode;
1063
1064                 mode = &phy_info->modes[*(phy_info->num_modes)];
1065                 os_memset(mode, 0, sizeof(*mode));
1066                 mode->mode = NUM_HOSTAPD_MODES;
1067                 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
1068                         HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
1069
1070                 /*
1071                  * Unsupported VHT MCS stream is defined as value 3, so the VHT
1072                  * MCS RX/TX map must be initialized with 0xffff to mark all 8
1073                  * possible streams as unsupported. This will be overridden if
1074                  * driver advertises VHT support.
1075                  */
1076                 mode->vht_mcs_set[0] = 0xff;
1077                 mode->vht_mcs_set[1] = 0xff;
1078                 mode->vht_mcs_set[4] = 0xff;
1079                 mode->vht_mcs_set[5] = 0xff;
1080
1081                 *(phy_info->num_modes) += 1;
1082                 phy_info->last_mode = nl_band->nla_type;
1083                 phy_info->last_chan_idx = 0;
1084         } else
1085                 mode = &phy_info->modes[*(phy_info->num_modes) - 1];
1086
1087         nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1088                   nla_len(nl_band), NULL);
1089
1090         phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
1091                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
1092                          tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
1093                          tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
1094         phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
1095                           tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
1096         ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
1097         if (ret != NL_OK)
1098                 return ret;
1099         ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
1100         if (ret != NL_OK)
1101                 return ret;
1102
1103         return NL_OK;
1104 }
1105
1106
1107 static int phy_info_handler(struct nl_msg *msg, void *arg)
1108 {
1109         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1110         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1111         struct phy_info_arg *phy_info = arg;
1112         struct nlattr *nl_band;
1113         int rem_band;
1114
1115         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1116                   genlmsg_attrlen(gnlh, 0), NULL);
1117
1118         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1119                 return NL_SKIP;
1120
1121         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
1122         {
1123                 int res = phy_info_band(phy_info, nl_band);
1124                 if (res != NL_OK)
1125                         return res;
1126         }
1127
1128         return NL_SKIP;
1129 }
1130
1131
1132 static struct hostapd_hw_modes *
1133 wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
1134                                      u16 *num_modes)
1135 {
1136         u16 m;
1137         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
1138         int i, mode11g_idx = -1;
1139
1140         /* heuristic to set up modes */
1141         for (m = 0; m < *num_modes; m++) {
1142                 if (!modes[m].num_channels)
1143                         continue;
1144                 if (modes[m].channels[0].freq < 4000) {
1145                         modes[m].mode = HOSTAPD_MODE_IEEE80211B;
1146                         for (i = 0; i < modes[m].num_rates; i++) {
1147                                 if (modes[m].rates[i] > 200) {
1148                                         modes[m].mode = HOSTAPD_MODE_IEEE80211G;
1149                                         break;
1150                                 }
1151                         }
1152                 } else if (modes[m].channels[0].freq > 50000)
1153                         modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
1154                 else
1155                         modes[m].mode = HOSTAPD_MODE_IEEE80211A;
1156         }
1157
1158         /* If only 802.11g mode is included, use it to construct matching
1159          * 802.11b mode data. */
1160
1161         for (m = 0; m < *num_modes; m++) {
1162                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
1163                         return modes; /* 802.11b already included */
1164                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
1165                         mode11g_idx = m;
1166         }
1167
1168         if (mode11g_idx < 0)
1169                 return modes; /* 2.4 GHz band not supported at all */
1170
1171         nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
1172         if (nmodes == NULL)
1173                 return modes; /* Could not add 802.11b mode */
1174
1175         mode = &nmodes[*num_modes];
1176         os_memset(mode, 0, sizeof(*mode));
1177         (*num_modes)++;
1178         modes = nmodes;
1179
1180         mode->mode = HOSTAPD_MODE_IEEE80211B;
1181
1182         mode11g = &modes[mode11g_idx];
1183         mode->num_channels = mode11g->num_channels;
1184         mode->channels = os_malloc(mode11g->num_channels *
1185                                    sizeof(struct hostapd_channel_data));
1186         if (mode->channels == NULL) {
1187                 (*num_modes)--;
1188                 return modes; /* Could not add 802.11b mode */
1189         }
1190         os_memcpy(mode->channels, mode11g->channels,
1191                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
1192
1193         mode->num_rates = 0;
1194         mode->rates = os_malloc(4 * sizeof(int));
1195         if (mode->rates == NULL) {
1196                 os_free(mode->channels);
1197                 (*num_modes)--;
1198                 return modes; /* Could not add 802.11b mode */
1199         }
1200
1201         for (i = 0; i < mode11g->num_rates; i++) {
1202                 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
1203                     mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
1204                         continue;
1205                 mode->rates[mode->num_rates] = mode11g->rates[i];
1206                 mode->num_rates++;
1207                 if (mode->num_rates == 4)
1208                         break;
1209         }
1210
1211         if (mode->num_rates == 0) {
1212                 os_free(mode->channels);
1213                 os_free(mode->rates);
1214                 (*num_modes)--;
1215                 return modes; /* No 802.11b rates */
1216         }
1217
1218         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
1219                    "information");
1220
1221         return modes;
1222 }
1223
1224
1225 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
1226                                   int end)
1227 {
1228         int c;
1229
1230         for (c = 0; c < mode->num_channels; c++) {
1231                 struct hostapd_channel_data *chan = &mode->channels[c];
1232                 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
1233                         chan->flag |= HOSTAPD_CHAN_HT40;
1234         }
1235 }
1236
1237
1238 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
1239                                       int end)
1240 {
1241         int c;
1242
1243         for (c = 0; c < mode->num_channels; c++) {
1244                 struct hostapd_channel_data *chan = &mode->channels[c];
1245                 if (!(chan->flag & HOSTAPD_CHAN_HT40))
1246                         continue;
1247                 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
1248                         chan->flag |= HOSTAPD_CHAN_HT40MINUS;
1249                 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
1250                         chan->flag |= HOSTAPD_CHAN_HT40PLUS;
1251         }
1252 }
1253
1254
1255 static void nl80211_reg_rule_max_eirp(u32 start, u32 end, u32 max_eirp,
1256                                       struct phy_info_arg *results)
1257 {
1258         u16 m;
1259
1260         for (m = 0; m < *results->num_modes; m++) {
1261                 int c;
1262                 struct hostapd_hw_modes *mode = &results->modes[m];
1263
1264                 for (c = 0; c < mode->num_channels; c++) {
1265                         struct hostapd_channel_data *chan = &mode->channels[c];
1266                         if ((u32) chan->freq - 10 >= start &&
1267                             (u32) chan->freq + 10 <= end)
1268                                 chan->max_tx_power = max_eirp;
1269                 }
1270         }
1271 }
1272
1273
1274 static void nl80211_reg_rule_ht40(u32 start, u32 end,
1275                                   struct phy_info_arg *results)
1276 {
1277         u16 m;
1278
1279         for (m = 0; m < *results->num_modes; m++) {
1280                 if (!(results->modes[m].ht_capab &
1281                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1282                         continue;
1283                 nl80211_set_ht40_mode(&results->modes[m], start, end);
1284         }
1285 }
1286
1287
1288 static void nl80211_reg_rule_sec(struct nlattr *tb[],
1289                                  struct phy_info_arg *results)
1290 {
1291         u32 start, end, max_bw;
1292         u16 m;
1293
1294         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1295             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
1296             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
1297                 return;
1298
1299         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1300         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1301         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1302
1303         if (max_bw < 20)
1304                 return;
1305
1306         for (m = 0; m < *results->num_modes; m++) {
1307                 if (!(results->modes[m].ht_capab &
1308                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1309                         continue;
1310                 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
1311         }
1312 }
1313
1314
1315 static void nl80211_set_vht_mode(struct hostapd_hw_modes *mode, int start,
1316                                  int end)
1317 {
1318         int c;
1319
1320         for (c = 0; c < mode->num_channels; c++) {
1321                 struct hostapd_channel_data *chan = &mode->channels[c];
1322                 if (chan->freq - 10 >= start && chan->freq + 70 <= end)
1323                         chan->flag |= HOSTAPD_CHAN_VHT_10_70;
1324
1325                 if (chan->freq - 30 >= start && chan->freq + 50 <= end)
1326                         chan->flag |= HOSTAPD_CHAN_VHT_30_50;
1327
1328                 if (chan->freq - 50 >= start && chan->freq + 30 <= end)
1329                         chan->flag |= HOSTAPD_CHAN_VHT_50_30;
1330
1331                 if (chan->freq - 70 >= start && chan->freq + 10 <= end)
1332                         chan->flag |= HOSTAPD_CHAN_VHT_70_10;
1333         }
1334 }
1335
1336
1337 static void nl80211_reg_rule_vht(struct nlattr *tb[],
1338                                  struct phy_info_arg *results)
1339 {
1340         u32 start, end, max_bw;
1341         u16 m;
1342
1343         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1344             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
1345             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
1346                 return;
1347
1348         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1349         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1350         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1351
1352         if (max_bw < 80)
1353                 return;
1354
1355         for (m = 0; m < *results->num_modes; m++) {
1356                 if (!(results->modes[m].ht_capab &
1357                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
1358                         continue;
1359                 /* TODO: use a real VHT support indication */
1360                 if (!results->modes[m].vht_capab)
1361                         continue;
1362
1363                 nl80211_set_vht_mode(&results->modes[m], start, end);
1364         }
1365 }
1366
1367
1368 static const char * dfs_domain_name(enum nl80211_dfs_regions region)
1369 {
1370         switch (region) {
1371         case NL80211_DFS_UNSET:
1372                 return "DFS-UNSET";
1373         case NL80211_DFS_FCC:
1374                 return "DFS-FCC";
1375         case NL80211_DFS_ETSI:
1376                 return "DFS-ETSI";
1377         case NL80211_DFS_JP:
1378                 return "DFS-JP";
1379         default:
1380                 return "DFS-invalid";
1381         }
1382 }
1383
1384
1385 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
1386 {
1387         struct phy_info_arg *results = arg;
1388         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1389         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1390         struct nlattr *nl_rule;
1391         struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
1392         int rem_rule;
1393         static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1394                 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1395                 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1396                 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1397                 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1398                 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1399                 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1400         };
1401
1402         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1403                   genlmsg_attrlen(gnlh, 0), NULL);
1404         if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
1405             !tb_msg[NL80211_ATTR_REG_RULES]) {
1406                 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
1407                            "available");
1408                 return NL_SKIP;
1409         }
1410
1411         if (tb_msg[NL80211_ATTR_DFS_REGION]) {
1412                 enum nl80211_dfs_regions dfs_domain;
1413                 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
1414                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s (%s)",
1415                            (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]),
1416                            dfs_domain_name(dfs_domain));
1417         } else {
1418                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
1419                            (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
1420         }
1421
1422         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1423         {
1424                 u32 start, end, max_eirp = 0, max_bw = 0, flags = 0;
1425                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1426                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1427                 if (tb_rule[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
1428                     tb_rule[NL80211_ATTR_FREQ_RANGE_END] == NULL)
1429                         continue;
1430                 start = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
1431                 end = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
1432                 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1433                         max_eirp = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]) / 100;
1434                 if (tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1435                         max_bw = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
1436                 if (tb_rule[NL80211_ATTR_REG_RULE_FLAGS])
1437                         flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
1438
1439                 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz %u mBm%s%s%s%s%s%s%s%s",
1440                            start, end, max_bw, max_eirp,
1441                            flags & NL80211_RRF_NO_OFDM ? " (no OFDM)" : "",
1442                            flags & NL80211_RRF_NO_CCK ? " (no CCK)" : "",
1443                            flags & NL80211_RRF_NO_INDOOR ? " (no indoor)" : "",
1444                            flags & NL80211_RRF_NO_OUTDOOR ? " (no outdoor)" :
1445                            "",
1446                            flags & NL80211_RRF_DFS ? " (DFS)" : "",
1447                            flags & NL80211_RRF_PTP_ONLY ? " (PTP only)" : "",
1448                            flags & NL80211_RRF_PTMP_ONLY ? " (PTMP only)" : "",
1449                            flags & NL80211_RRF_NO_IR ? " (no IR)" : "");
1450                 if (max_bw >= 40)
1451                         nl80211_reg_rule_ht40(start, end, results);
1452                 if (tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1453                         nl80211_reg_rule_max_eirp(start, end, max_eirp,
1454                                                   results);
1455         }
1456
1457         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1458         {
1459                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1460                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1461                 nl80211_reg_rule_sec(tb_rule, results);
1462         }
1463
1464         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
1465         {
1466                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
1467                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
1468                 nl80211_reg_rule_vht(tb_rule, results);
1469         }
1470
1471         return NL_SKIP;
1472 }
1473
1474
1475 static int nl80211_set_regulatory_flags(struct wpa_driver_nl80211_data *drv,
1476                                         struct phy_info_arg *results)
1477 {
1478         struct nl_msg *msg;
1479
1480         msg = nlmsg_alloc();
1481         if (!msg)
1482                 return -ENOMEM;
1483
1484         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1485         return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
1486 }
1487
1488
1489 struct hostapd_hw_modes *
1490 nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
1491 {
1492         u32 feat;
1493         struct i802_bss *bss = priv;
1494         struct wpa_driver_nl80211_data *drv = bss->drv;
1495         int nl_flags = 0;
1496         struct nl_msg *msg;
1497         struct phy_info_arg result = {
1498                 .num_modes = num_modes,
1499                 .modes = NULL,
1500                 .last_mode = -1,
1501         };
1502
1503         *num_modes = 0;
1504         *flags = 0;
1505
1506         feat = get_nl80211_protocol_features(drv);
1507         if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
1508                 nl_flags = NLM_F_DUMP;
1509         if (!(msg = nl80211_cmd_msg(bss, nl_flags, NL80211_CMD_GET_WIPHY)) ||
1510             nla_put_flag(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP)) {
1511                 nlmsg_free(msg);
1512                 return NULL;
1513         }
1514
1515         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
1516                 nl80211_set_regulatory_flags(drv, &result);
1517                 return wpa_driver_nl80211_postprocess_modes(result.modes,
1518                                                             num_modes);
1519         }
1520
1521         return NULL;
1522 }