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