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