nl80211: Avoid undefined behavior in pointer arithmetic
[mech_eap.git] / src / drivers / driver_nl80211_scan.c
1 /*
2  * Driver interaction with Linux nl80211/cfg80211 - Scanning
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 "utils/eloop.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/qca-vendor.h"
18 #include "driver_nl80211.h"
19
20
21 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
22 {
23         struct nlattr *tb[NL80211_ATTR_MAX + 1];
24         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
25         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
26         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
27                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
28                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
29         };
30         struct wpa_scan_results *scan_results = arg;
31         struct wpa_scan_res *scan_res;
32         size_t i;
33
34         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
35                   genlmsg_attrlen(gnlh, 0), NULL);
36
37         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
38                 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
39                 return NL_SKIP;
40         }
41
42         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
43                              tb[NL80211_ATTR_SURVEY_INFO],
44                              survey_policy)) {
45                 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
46                            "attributes");
47                 return NL_SKIP;
48         }
49
50         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
51                 return NL_SKIP;
52
53         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
54                 return NL_SKIP;
55
56         for (i = 0; i < scan_results->num; ++i) {
57                 scan_res = scan_results->res[i];
58                 if (!scan_res)
59                         continue;
60                 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
61                     scan_res->freq)
62                         continue;
63                 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
64                         continue;
65                 scan_res->noise = (s8)
66                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
67                 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
68         }
69
70         return NL_SKIP;
71 }
72
73
74 static int nl80211_get_noise_for_scan_results(
75         struct wpa_driver_nl80211_data *drv,
76         struct wpa_scan_results *scan_res)
77 {
78         struct nl_msg *msg;
79
80         msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
81         return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
82                                   scan_res);
83 }
84
85
86 /**
87  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
88  * @eloop_ctx: Driver private data
89  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
90  *
91  * This function can be used as registered timeout when starting a scan to
92  * generate a scan completed event if the driver does not report this.
93  */
94 void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
95 {
96         struct wpa_driver_nl80211_data *drv = eloop_ctx;
97         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
98                 wpa_driver_nl80211_set_mode(drv->first_bss,
99                                             drv->ap_scan_as_station);
100                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
101         }
102         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
103         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
104 }
105
106
107 static struct nl_msg *
108 nl80211_scan_common(struct i802_bss *bss, u8 cmd,
109                     struct wpa_driver_scan_params *params)
110 {
111         struct wpa_driver_nl80211_data *drv = bss->drv;
112         struct nl_msg *msg;
113         size_t i;
114         u32 scan_flags = 0;
115
116         msg = nl80211_cmd_msg(bss, 0, cmd);
117         if (!msg)
118                 return NULL;
119
120         if (params->num_ssids) {
121                 struct nlattr *ssids;
122
123                 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
124                 if (ssids == NULL)
125                         goto fail;
126                 for (i = 0; i < params->num_ssids; i++) {
127                         wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
128                                           params->ssids[i].ssid,
129                                           params->ssids[i].ssid_len);
130                         if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
131                                     params->ssids[i].ssid))
132                                 goto fail;
133                 }
134                 nla_nest_end(msg, ssids);
135         }
136
137         if (params->extra_ies) {
138                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
139                             params->extra_ies, params->extra_ies_len);
140                 if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
141                             params->extra_ies))
142                         goto fail;
143         }
144
145         if (params->freqs) {
146                 struct nlattr *freqs;
147                 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
148                 if (freqs == NULL)
149                         goto fail;
150                 for (i = 0; params->freqs[i]; i++) {
151                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
152                                    "MHz", params->freqs[i]);
153                         if (nla_put_u32(msg, i + 1, params->freqs[i]))
154                                 goto fail;
155                 }
156                 nla_nest_end(msg, freqs);
157         }
158
159         os_free(drv->filter_ssids);
160         drv->filter_ssids = params->filter_ssids;
161         params->filter_ssids = NULL;
162         drv->num_filter_ssids = params->num_filter_ssids;
163
164         if (params->only_new_results) {
165                 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
166                 scan_flags |= NL80211_SCAN_FLAG_FLUSH;
167         }
168
169         if (params->low_priority && drv->have_low_prio_scan) {
170                 wpa_printf(MSG_DEBUG,
171                            "nl80211: Add NL80211_SCAN_FLAG_LOW_PRIORITY");
172                 scan_flags |= NL80211_SCAN_FLAG_LOW_PRIORITY;
173         }
174
175         if (params->mac_addr_rand) {
176                 wpa_printf(MSG_DEBUG,
177                            "nl80211: Add NL80211_SCAN_FLAG_RANDOM_ADDR");
178                 scan_flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
179
180                 if (params->mac_addr) {
181                         wpa_printf(MSG_DEBUG, "nl80211: MAC address: " MACSTR,
182                                    MAC2STR(params->mac_addr));
183                         if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
184                                     params->mac_addr))
185                                 goto fail;
186                 }
187
188                 if (params->mac_addr_mask) {
189                         wpa_printf(MSG_DEBUG, "nl80211: MAC address mask: "
190                                    MACSTR, MAC2STR(params->mac_addr_mask));
191                         if (nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
192                                     params->mac_addr_mask))
193                                 goto fail;
194                 }
195         }
196
197         if (scan_flags &&
198             nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags))
199                 goto fail;
200
201         return msg;
202
203 fail:
204         nlmsg_free(msg);
205         return NULL;
206 }
207
208
209 /**
210  * wpa_driver_nl80211_scan - Request the driver to initiate scan
211  * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
212  * @params: Scan parameters
213  * Returns: 0 on success, -1 on failure
214  */
215 int wpa_driver_nl80211_scan(struct i802_bss *bss,
216                             struct wpa_driver_scan_params *params)
217 {
218         struct wpa_driver_nl80211_data *drv = bss->drv;
219         int ret = -1, timeout;
220         struct nl_msg *msg = NULL;
221
222         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
223         drv->scan_for_auth = 0;
224
225         if (TEST_FAIL())
226                 return -1;
227
228         msg = nl80211_scan_common(bss, NL80211_CMD_TRIGGER_SCAN, params);
229         if (!msg)
230                 return -1;
231
232         if (params->p2p_probe) {
233                 struct nlattr *rates;
234
235                 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
236
237                 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
238                 if (rates == NULL)
239                         goto fail;
240
241                 /*
242                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
243                  * by masking out everything else apart from the OFDM rates 6,
244                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
245                  * rates are left enabled.
246                  */
247                 if (nla_put(msg, NL80211_BAND_2GHZ, 8,
248                             "\x0c\x12\x18\x24\x30\x48\x60\x6c"))
249                         goto fail;
250                 nla_nest_end(msg, rates);
251
252                 if (nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE))
253                         goto fail;
254         }
255
256         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
257         msg = NULL;
258         if (ret) {
259                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
260                            "(%s)", ret, strerror(-ret));
261                 if (drv->hostapd && is_ap_interface(drv->nlmode)) {
262                         enum nl80211_iftype old_mode = drv->nlmode;
263
264                         /*
265                          * mac80211 does not allow scan requests in AP mode, so
266                          * try to do this in station mode.
267                          */
268                         if (wpa_driver_nl80211_set_mode(
269                                     bss, NL80211_IFTYPE_STATION))
270                                 goto fail;
271
272                         if (wpa_driver_nl80211_scan(bss, params)) {
273                                 wpa_driver_nl80211_set_mode(bss, old_mode);
274                                 goto fail;
275                         }
276
277                         /* Restore AP mode when processing scan results */
278                         drv->ap_scan_as_station = old_mode;
279                         ret = 0;
280                 } else
281                         goto fail;
282         }
283
284         drv->scan_state = SCAN_REQUESTED;
285         /* Not all drivers generate "scan completed" wireless event, so try to
286          * read results after a timeout. */
287         timeout = 10;
288         if (drv->scan_complete_events) {
289                 /*
290                  * The driver seems to deliver events to notify when scan is
291                  * complete, so use longer timeout to avoid race conditions
292                  * with scanning and following association request.
293                  */
294                 timeout = 30;
295         }
296         wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
297                    "seconds", ret, timeout);
298         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
299         eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
300                                drv, drv->ctx);
301         drv->last_scan_cmd = NL80211_CMD_TRIGGER_SCAN;
302
303 fail:
304         nlmsg_free(msg);
305         return ret;
306 }
307
308
309 /**
310  * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
311  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
312  * @params: Scan parameters
313  * @interval: Interval between scan cycles in milliseconds
314  * Returns: 0 on success, -1 on failure or if not supported
315  */
316 int wpa_driver_nl80211_sched_scan(void *priv,
317                                   struct wpa_driver_scan_params *params,
318                                   u32 interval)
319 {
320         struct i802_bss *bss = priv;
321         struct wpa_driver_nl80211_data *drv = bss->drv;
322         int ret = -1;
323         struct nl_msg *msg;
324         size_t i;
325
326         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
327
328 #ifdef ANDROID
329         if (!drv->capa.sched_scan_supported)
330                 return android_pno_start(bss, params);
331 #endif /* ANDROID */
332
333         msg = nl80211_scan_common(bss, NL80211_CMD_START_SCHED_SCAN, params);
334         if (!msg ||
335             nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval))
336                 goto fail;
337
338         if ((drv->num_filter_ssids &&
339             (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
340             params->filter_rssi) {
341                 struct nlattr *match_sets;
342                 match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
343                 if (match_sets == NULL)
344                         goto fail;
345
346                 for (i = 0; i < drv->num_filter_ssids; i++) {
347                         struct nlattr *match_set_ssid;
348                         wpa_hexdump_ascii(MSG_MSGDUMP,
349                                           "nl80211: Sched scan filter SSID",
350                                           drv->filter_ssids[i].ssid,
351                                           drv->filter_ssids[i].ssid_len);
352
353                         match_set_ssid = nla_nest_start(msg, i + 1);
354                         if (match_set_ssid == NULL ||
355                             nla_put(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
356                                     drv->filter_ssids[i].ssid_len,
357                                     drv->filter_ssids[i].ssid) ||
358                             (params->filter_rssi &&
359                              nla_put_u32(msg,
360                                          NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
361                                          params->filter_rssi)))
362                                 goto fail;
363
364                         nla_nest_end(msg, match_set_ssid);
365                 }
366
367                 /*
368                  * Due to backward compatibility code, newer kernels treat this
369                  * matchset (with only an RSSI filter) as the default for all
370                  * other matchsets, unless it's the only one, in which case the
371                  * matchset will actually allow all SSIDs above the RSSI.
372                  */
373                 if (params->filter_rssi) {
374                         struct nlattr *match_set_rssi;
375                         match_set_rssi = nla_nest_start(msg, 0);
376                         if (match_set_rssi == NULL ||
377                             nla_put_u32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
378                                         params->filter_rssi))
379                                 goto fail;
380                         wpa_printf(MSG_MSGDUMP,
381                                    "nl80211: Sched scan RSSI filter %d dBm",
382                                    params->filter_rssi);
383                         nla_nest_end(msg, match_set_rssi);
384                 }
385
386                 nla_nest_end(msg, match_sets);
387         }
388
389         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
390
391         /* TODO: if we get an error here, we should fall back to normal scan */
392
393         msg = NULL;
394         if (ret) {
395                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
396                            "ret=%d (%s)", ret, strerror(-ret));
397                 goto fail;
398         }
399
400         wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
401                    "scan interval %d msec", ret, interval);
402
403 fail:
404         nlmsg_free(msg);
405         return ret;
406 }
407
408
409 /**
410  * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
411  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
412  * Returns: 0 on success, -1 on failure or if not supported
413  */
414 int wpa_driver_nl80211_stop_sched_scan(void *priv)
415 {
416         struct i802_bss *bss = priv;
417         struct wpa_driver_nl80211_data *drv = bss->drv;
418         int ret;
419         struct nl_msg *msg;
420
421 #ifdef ANDROID
422         if (!drv->capa.sched_scan_supported)
423                 return android_pno_stop(bss);
424 #endif /* ANDROID */
425
426         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_STOP_SCHED_SCAN);
427         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
428         if (ret) {
429                 wpa_printf(MSG_DEBUG,
430                            "nl80211: Sched scan stop failed: ret=%d (%s)",
431                            ret, strerror(-ret));
432         } else {
433                 wpa_printf(MSG_DEBUG,
434                            "nl80211: Sched scan stop sent");
435         }
436
437         return ret;
438 }
439
440
441 const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
442 {
443         const u8 *end, *pos;
444
445         if (ies == NULL)
446                 return NULL;
447
448         pos = ies;
449         end = ies + ies_len;
450
451         while (end - pos > 1) {
452                 if (2 + pos[1] > end - pos)
453                         break;
454                 if (pos[0] == ie)
455                         return pos;
456                 pos += 2 + pos[1];
457         }
458
459         return NULL;
460 }
461
462
463 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
464                                  const u8 *ie, size_t ie_len)
465 {
466         const u8 *ssid;
467         size_t i;
468
469         if (drv->filter_ssids == NULL)
470                 return 0;
471
472         ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
473         if (ssid == NULL)
474                 return 1;
475
476         for (i = 0; i < drv->num_filter_ssids; i++) {
477                 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
478                     os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
479                     0)
480                         return 0;
481         }
482
483         return 1;
484 }
485
486
487 int bss_info_handler(struct nl_msg *msg, void *arg)
488 {
489         struct nlattr *tb[NL80211_ATTR_MAX + 1];
490         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
491         struct nlattr *bss[NL80211_BSS_MAX + 1];
492         static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
493                 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
494                 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
495                 [NL80211_BSS_TSF] = { .type = NLA_U64 },
496                 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
497                 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
498                 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
499                 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
500                 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
501                 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
502                 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
503                 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
504         };
505         struct nl80211_bss_info_arg *_arg = arg;
506         struct wpa_scan_results *res = _arg->res;
507         struct wpa_scan_res **tmp;
508         struct wpa_scan_res *r;
509         const u8 *ie, *beacon_ie;
510         size_t ie_len, beacon_ie_len;
511         u8 *pos;
512         size_t i;
513
514         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
515                   genlmsg_attrlen(gnlh, 0), NULL);
516         if (!tb[NL80211_ATTR_BSS])
517                 return NL_SKIP;
518         if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
519                              bss_policy))
520                 return NL_SKIP;
521         if (bss[NL80211_BSS_STATUS]) {
522                 enum nl80211_bss_status status;
523                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
524                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
525                     bss[NL80211_BSS_FREQUENCY]) {
526                         _arg->assoc_freq =
527                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
528                         wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
529                                    _arg->assoc_freq);
530                 }
531                 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
532                     bss[NL80211_BSS_FREQUENCY]) {
533                         _arg->ibss_freq =
534                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
535                         wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
536                                    _arg->ibss_freq);
537                 }
538                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
539                     bss[NL80211_BSS_BSSID]) {
540                         os_memcpy(_arg->assoc_bssid,
541                                   nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
542                         wpa_printf(MSG_DEBUG, "nl80211: Associated with "
543                                    MACSTR, MAC2STR(_arg->assoc_bssid));
544                 }
545         }
546         if (!res)
547                 return NL_SKIP;
548         if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
549                 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
550                 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
551         } else {
552                 ie = NULL;
553                 ie_len = 0;
554         }
555         if (bss[NL80211_BSS_BEACON_IES]) {
556                 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
557                 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
558         } else {
559                 beacon_ie = NULL;
560                 beacon_ie_len = 0;
561         }
562
563         if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
564                                   ie ? ie_len : beacon_ie_len))
565                 return NL_SKIP;
566
567         r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
568         if (r == NULL)
569                 return NL_SKIP;
570         if (bss[NL80211_BSS_BSSID])
571                 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
572                           ETH_ALEN);
573         if (bss[NL80211_BSS_FREQUENCY])
574                 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
575         if (bss[NL80211_BSS_BEACON_INTERVAL])
576                 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
577         if (bss[NL80211_BSS_CAPABILITY])
578                 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
579         r->flags |= WPA_SCAN_NOISE_INVALID;
580         if (bss[NL80211_BSS_SIGNAL_MBM]) {
581                 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
582                 r->level /= 100; /* mBm to dBm */
583                 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
584         } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
585                 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
586                 r->flags |= WPA_SCAN_QUAL_INVALID;
587         } else
588                 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
589         if (bss[NL80211_BSS_TSF])
590                 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
591         if (bss[NL80211_BSS_BEACON_TSF]) {
592                 u64 tsf = nla_get_u64(bss[NL80211_BSS_BEACON_TSF]);
593                 if (tsf > r->tsf)
594                         r->tsf = tsf;
595         }
596         if (bss[NL80211_BSS_SEEN_MS_AGO])
597                 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
598         r->ie_len = ie_len;
599         pos = (u8 *) (r + 1);
600         if (ie) {
601                 os_memcpy(pos, ie, ie_len);
602                 pos += ie_len;
603         }
604         r->beacon_ie_len = beacon_ie_len;
605         if (beacon_ie)
606                 os_memcpy(pos, beacon_ie, beacon_ie_len);
607
608         if (bss[NL80211_BSS_STATUS]) {
609                 enum nl80211_bss_status status;
610                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
611                 switch (status) {
612                 case NL80211_BSS_STATUS_ASSOCIATED:
613                         r->flags |= WPA_SCAN_ASSOCIATED;
614                         break;
615                 default:
616                         break;
617                 }
618         }
619
620         /*
621          * cfg80211 maintains separate BSS table entries for APs if the same
622          * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
623          * not use frequency as a separate key in the BSS table, so filter out
624          * duplicated entries. Prefer associated BSS entry in such a case in
625          * order to get the correct frequency into the BSS table. Similarly,
626          * prefer newer entries over older.
627          */
628         for (i = 0; i < res->num; i++) {
629                 const u8 *s1, *s2;
630                 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
631                         continue;
632
633                 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
634                                     res->res[i]->ie_len, WLAN_EID_SSID);
635                 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
636                 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
637                     os_memcmp(s1, s2, 2 + s1[1]) != 0)
638                         continue;
639
640                 /* Same BSSID,SSID was already included in scan results */
641                 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
642                            "for " MACSTR, MAC2STR(r->bssid));
643
644                 if (((r->flags & WPA_SCAN_ASSOCIATED) &&
645                      !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) ||
646                     r->age < res->res[i]->age) {
647                         os_free(res->res[i]);
648                         res->res[i] = r;
649                 } else
650                         os_free(r);
651                 return NL_SKIP;
652         }
653
654         tmp = os_realloc_array(res->res, res->num + 1,
655                                sizeof(struct wpa_scan_res *));
656         if (tmp == NULL) {
657                 os_free(r);
658                 return NL_SKIP;
659         }
660         tmp[res->num++] = r;
661         res->res = tmp;
662
663         return NL_SKIP;
664 }
665
666
667 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
668                                  const u8 *addr)
669 {
670         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
671                 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
672                            "mismatch (" MACSTR ")", MAC2STR(addr));
673                 wpa_driver_nl80211_mlme(drv, addr,
674                                         NL80211_CMD_DEAUTHENTICATE,
675                                         WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
676         }
677 }
678
679
680 static void wpa_driver_nl80211_check_bss_status(
681         struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
682 {
683         size_t i;
684
685         for (i = 0; i < res->num; i++) {
686                 struct wpa_scan_res *r = res->res[i];
687
688                 if (r->flags & WPA_SCAN_ASSOCIATED) {
689                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
690                                    "indicate BSS status with " MACSTR
691                                    " as associated",
692                                    MAC2STR(r->bssid));
693                         if (is_sta_interface(drv->nlmode) &&
694                             !drv->associated) {
695                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
696                                            "(not associated) does not match "
697                                            "with BSS state");
698                                 clear_state_mismatch(drv, r->bssid);
699                         } else if (is_sta_interface(drv->nlmode) &&
700                                    os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
701                                    0) {
702                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
703                                            "(associated with " MACSTR ") does "
704                                            "not match with BSS state",
705                                            MAC2STR(drv->bssid));
706                                 clear_state_mismatch(drv, r->bssid);
707                                 clear_state_mismatch(drv, drv->bssid);
708                         }
709                 }
710         }
711 }
712
713
714 static struct wpa_scan_results *
715 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
716 {
717         struct nl_msg *msg;
718         struct wpa_scan_results *res;
719         int ret;
720         struct nl80211_bss_info_arg arg;
721
722         res = os_zalloc(sizeof(*res));
723         if (res == NULL)
724                 return NULL;
725         if (!(msg = nl80211_cmd_msg(drv->first_bss, NLM_F_DUMP,
726                                     NL80211_CMD_GET_SCAN))) {
727                 wpa_scan_results_free(res);
728                 return NULL;
729         }
730
731         arg.drv = drv;
732         arg.res = res;
733         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
734         if (ret == 0) {
735                 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
736                            "BSSes)", (unsigned long) res->num);
737                 nl80211_get_noise_for_scan_results(drv, res);
738                 return res;
739         }
740         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
741                    "(%s)", ret, strerror(-ret));
742         wpa_scan_results_free(res);
743         return NULL;
744 }
745
746
747 /**
748  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
749  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
750  * Returns: Scan results on success, -1 on failure
751  */
752 struct wpa_scan_results * wpa_driver_nl80211_get_scan_results(void *priv)
753 {
754         struct i802_bss *bss = priv;
755         struct wpa_driver_nl80211_data *drv = bss->drv;
756         struct wpa_scan_results *res;
757
758         res = nl80211_get_scan_results(drv);
759         if (res)
760                 wpa_driver_nl80211_check_bss_status(drv, res);
761         return res;
762 }
763
764
765 void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
766 {
767         struct wpa_scan_results *res;
768         size_t i;
769
770         res = nl80211_get_scan_results(drv);
771         if (res == NULL) {
772                 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
773                 return;
774         }
775
776         wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
777         for (i = 0; i < res->num; i++) {
778                 struct wpa_scan_res *r = res->res[i];
779                 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s",
780                            (int) i, (int) res->num, MAC2STR(r->bssid),
781                            r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
782         }
783
784         wpa_scan_results_free(res);
785 }
786
787
788 #ifdef CONFIG_DRIVER_NL80211_QCA
789
790 static int scan_cookie_handler(struct nl_msg *msg, void *arg)
791 {
792         struct nlattr *tb[NL80211_ATTR_MAX + 1];
793         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
794         u64 *cookie = arg;
795
796         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
797                   genlmsg_attrlen(gnlh, 0), NULL);
798
799         if (tb[NL80211_ATTR_VENDOR_DATA]) {
800                 struct nlattr *nl_vendor = tb[NL80211_ATTR_VENDOR_DATA];
801                 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1];
802
803                 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_SCAN_MAX,
804                           nla_data(nl_vendor), nla_len(nl_vendor), NULL);
805
806                 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE])
807                         *cookie = nla_get_u64(
808                                 tb_vendor[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]);
809         }
810
811         return NL_SKIP;
812 }
813
814
815 /**
816  * wpa_driver_nl80211_vendor_scan - Request the driver to initiate a vendor scan
817  * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
818  * @params: Scan parameters
819  * Returns: 0 on success, -1 on failure
820  */
821 int wpa_driver_nl80211_vendor_scan(struct i802_bss *bss,
822                                    struct wpa_driver_scan_params *params)
823 {
824         struct wpa_driver_nl80211_data *drv = bss->drv;
825         struct nl_msg *msg = NULL;
826         struct nlattr *attr;
827         size_t i;
828         u32 scan_flags = 0;
829         int ret = -1;
830         u64 cookie = 0;
831
832         wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: vendor scan request");
833         drv->scan_for_auth = 0;
834
835         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
836             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
837             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
838                         QCA_NL80211_VENDOR_SUBCMD_TRIGGER_SCAN) )
839                 goto fail;
840
841         attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
842         if (attr == NULL)
843                 goto fail;
844
845         if (params->num_ssids) {
846                 struct nlattr *ssids;
847
848                 ssids = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_SCAN_SSIDS);
849                 if (ssids == NULL)
850                         goto fail;
851                 for (i = 0; i < params->num_ssids; i++) {
852                         wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
853                                         params->ssids[i].ssid,
854                                         params->ssids[i].ssid_len);
855                         if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
856                                     params->ssids[i].ssid))
857                                 goto fail;
858                 }
859                 nla_nest_end(msg, ssids);
860         }
861
862         if (params->extra_ies) {
863                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
864                             params->extra_ies, params->extra_ies_len);
865                 if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SCAN_IE,
866                             params->extra_ies_len, params->extra_ies))
867                         goto fail;
868         }
869
870         if (params->freqs) {
871                 struct nlattr *freqs;
872
873                 freqs = nla_nest_start(msg,
874                                        QCA_WLAN_VENDOR_ATTR_SCAN_FREQUENCIES);
875                 if (freqs == NULL)
876                         goto fail;
877                 for (i = 0; params->freqs[i]; i++) {
878                         wpa_printf(MSG_MSGDUMP,
879                                    "nl80211: Scan frequency %u MHz",
880                                    params->freqs[i]);
881                         if (nla_put_u32(msg, i + 1, params->freqs[i]))
882                                 goto fail;
883                 }
884                 nla_nest_end(msg, freqs);
885         }
886
887         os_free(drv->filter_ssids);
888         drv->filter_ssids = params->filter_ssids;
889         params->filter_ssids = NULL;
890         drv->num_filter_ssids = params->num_filter_ssids;
891
892         if (params->low_priority && drv->have_low_prio_scan) {
893                 wpa_printf(MSG_DEBUG,
894                            "nl80211: Add NL80211_SCAN_FLAG_LOW_PRIORITY");
895                 scan_flags |= NL80211_SCAN_FLAG_LOW_PRIORITY;
896         }
897
898         if (params->mac_addr_rand) {
899                 wpa_printf(MSG_DEBUG,
900                            "nl80211: Add NL80211_SCAN_FLAG_RANDOM_ADDR");
901                 scan_flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
902
903                 if (params->mac_addr) {
904                         wpa_printf(MSG_DEBUG, "nl80211: MAC address: " MACSTR,
905                                    MAC2STR(params->mac_addr));
906                         if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SCAN_MAC,
907                                     ETH_ALEN, params->mac_addr))
908                                 goto fail;
909                 }
910
911                 if (params->mac_addr_mask) {
912                         wpa_printf(MSG_DEBUG, "nl80211: MAC address mask: "
913                                    MACSTR, MAC2STR(params->mac_addr_mask));
914                         if (nla_put(msg, QCA_WLAN_VENDOR_ATTR_SCAN_MAC_MASK,
915                                     ETH_ALEN, params->mac_addr_mask))
916                                 goto fail;
917                 }
918         }
919
920         if (scan_flags &&
921             nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags))
922                 goto fail;
923
924         if (params->p2p_probe) {
925                 struct nlattr *rates;
926
927                 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
928
929                 rates = nla_nest_start(msg,
930                                        QCA_WLAN_VENDOR_ATTR_SCAN_SUPP_RATES);
931                 if (rates == NULL)
932                         goto fail;
933
934                 /*
935                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
936                  * by masking out everything else apart from the OFDM rates 6,
937                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
938                  * rates are left enabled.
939                  */
940                 if (nla_put(msg, NL80211_BAND_2GHZ, 8,
941                             "\x0c\x12\x18\x24\x30\x48\x60\x6c"))
942                         goto fail;
943                 nla_nest_end(msg, rates);
944
945                 if (nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_SCAN_TX_NO_CCK_RATE))
946                         goto fail;
947         }
948
949         nla_nest_end(msg, attr);
950
951         ret = send_and_recv_msgs(drv, msg, scan_cookie_handler, &cookie);
952         msg = NULL;
953         if (ret) {
954                 wpa_printf(MSG_DEBUG,
955                            "nl80211: Vendor scan trigger failed: ret=%d (%s)",
956                            ret, strerror(-ret));
957                 goto fail;
958         }
959
960         drv->vendor_scan_cookie = cookie;
961         drv->scan_state = SCAN_REQUESTED;
962
963         wpa_printf(MSG_DEBUG,
964                    "nl80211: Vendor scan requested (ret=%d) - scan timeout 30 seconds, scan cookie:0x%llx",
965                    ret, (long long unsigned int) cookie);
966         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
967         eloop_register_timeout(30, 0, wpa_driver_nl80211_scan_timeout,
968                                drv, drv->ctx);
969         drv->last_scan_cmd = NL80211_CMD_VENDOR;
970
971 fail:
972         nlmsg_free(msg);
973         return ret;
974 }
975
976 #endif /* CONFIG_DRIVER_NL80211_QCA */