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