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