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