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