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