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