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