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