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
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
12 #include <netlink/genl/genl.h>
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"
21 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
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 },
30 struct wpa_scan_results *scan_results = arg;
31 struct wpa_scan_res *scan_res;
34 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
35 genlmsg_attrlen(gnlh, 0), NULL);
37 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
38 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
42 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
43 tb[NL80211_ATTR_SURVEY_INFO],
45 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
50 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
53 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
56 for (i = 0; i < scan_results->num; ++i) {
57 scan_res = scan_results->res[i];
60 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
63 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
65 scan_res->noise = (s8)
66 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
67 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
74 static int nl80211_get_noise_for_scan_results(
75 struct wpa_driver_nl80211_data *drv,
76 struct wpa_scan_results *scan_res)
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,
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()
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.
94 void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
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;
102 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
103 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
107 static struct nl_msg *
108 nl80211_scan_common(struct i802_bss *bss, u8 cmd,
109 struct wpa_driver_scan_params *params)
111 struct wpa_driver_nl80211_data *drv = bss->drv;
116 msg = nl80211_cmd_msg(bss, 0, cmd);
120 if (params->num_ssids) {
121 struct nlattr *ssids;
123 ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
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))
134 nla_nest_end(msg, ssids);
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,
146 struct nlattr *freqs;
147 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
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]))
156 nla_nest_end(msg, freqs);
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;
164 if (params->only_new_results) {
165 wpa_printf(MSG_DEBUG, "nl80211: Add NL80211_SCAN_FLAG_FLUSH");
166 scan_flags |= NL80211_SCAN_FLAG_FLUSH;
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;
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;
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,
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))
198 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags))
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
215 int wpa_driver_nl80211_scan(struct i802_bss *bss,
216 struct wpa_driver_scan_params *params)
218 struct wpa_driver_nl80211_data *drv = bss->drv;
219 int ret = -1, timeout;
220 struct nl_msg *msg = NULL;
222 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
223 drv->scan_for_auth = 0;
228 msg = nl80211_scan_common(bss, NL80211_CMD_TRIGGER_SCAN, params);
232 if (params->p2p_probe) {
233 struct nlattr *rates;
235 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
237 rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
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.
247 if (nla_put(msg, NL80211_BAND_2GHZ, 8,
248 "\x0c\x12\x18\x24\x30\x48\x60\x6c"))
250 nla_nest_end(msg, rates);
252 if (nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE))
256 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
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;
265 * mac80211 does not allow scan requests in AP mode, so
266 * try to do this in station mode.
268 if (wpa_driver_nl80211_set_mode(
269 bss, NL80211_IFTYPE_STATION))
272 if (wpa_driver_nl80211_scan(bss, params)) {
273 wpa_driver_nl80211_set_mode(bss, old_mode);
277 /* Restore AP mode when processing scan results */
278 drv->ap_scan_as_station = old_mode;
284 drv->scan_state = SCAN_REQUESTED;
285 /* Not all drivers generate "scan completed" wireless event, so try to
286 * read results after a timeout. */
288 if (drv->scan_complete_events) {
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.
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,
301 drv->last_scan_cmd = NL80211_CMD_TRIGGER_SCAN;
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
316 int wpa_driver_nl80211_sched_scan(void *priv,
317 struct wpa_driver_scan_params *params,
320 struct i802_bss *bss = priv;
321 struct wpa_driver_nl80211_data *drv = bss->drv;
326 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
329 if (!drv->capa.sched_scan_supported)
330 return android_pno_start(bss, params);
333 msg = nl80211_scan_common(bss, NL80211_CMD_START_SCHED_SCAN, params);
335 nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval))
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)
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);
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 &&
360 NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
361 params->filter_rssi)))
364 nla_nest_end(msg, match_set_ssid);
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.
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))
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);
386 nla_nest_end(msg, match_sets);
389 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
391 /* TODO: if we get an error here, we should fall back to normal scan */
395 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
396 "ret=%d (%s)", ret, strerror(-ret));
400 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
401 "scan interval %d msec", ret, interval);
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
414 int wpa_driver_nl80211_stop_sched_scan(void *priv)
416 struct i802_bss *bss = priv;
417 struct wpa_driver_nl80211_data *drv = bss->drv;
422 if (!drv->capa.sched_scan_supported)
423 return android_pno_stop(bss);
426 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_STOP_SCHED_SCAN);
427 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
429 wpa_printf(MSG_DEBUG,
430 "nl80211: Sched scan stop failed: ret=%d (%s)",
431 ret, strerror(-ret));
433 wpa_printf(MSG_DEBUG,
434 "nl80211: Sched scan stop sent");
441 const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
451 while (pos + 1 < end) {
452 if (pos + 2 + pos[1] > end)
463 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
464 const u8 *ie, size_t ie_len)
469 if (drv->filter_ssids == NULL)
472 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
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]) ==
487 int bss_info_handler(struct nl_msg *msg, void *arg)
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 },
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;
514 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
515 genlmsg_attrlen(gnlh, 0), NULL);
516 if (!tb[NL80211_ATTR_BSS])
518 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
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]) {
527 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
528 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
531 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
532 bss[NL80211_BSS_FREQUENCY]) {
534 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
535 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
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));
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]);
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]);
563 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
564 ie ? ie_len : beacon_ie_len))
567 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
570 if (bss[NL80211_BSS_BSSID])
571 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
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;
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]);
596 if (bss[NL80211_BSS_SEEN_MS_AGO])
597 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
599 pos = (u8 *) (r + 1);
601 os_memcpy(pos, ie, ie_len);
604 r->beacon_ie_len = beacon_ie_len;
606 os_memcpy(pos, beacon_ie, beacon_ie_len);
608 if (bss[NL80211_BSS_STATUS]) {
609 enum nl80211_bss_status status;
610 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
612 case NL80211_BSS_STATUS_ASSOCIATED:
613 r->flags |= WPA_SCAN_ASSOCIATED;
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.
628 for (i = 0; i < res->num; i++) {
630 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
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)
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));
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]);
654 tmp = os_realloc_array(res->res, res->num + 1,
655 sizeof(struct wpa_scan_res *));
667 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
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);
680 static void wpa_driver_nl80211_check_bss_status(
681 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
685 for (i = 0; i < res->num; i++) {
686 struct wpa_scan_res *r = res->res[i];
688 if (r->flags & WPA_SCAN_ASSOCIATED) {
689 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
690 "indicate BSS status with " MACSTR
693 if (is_sta_interface(drv->nlmode) &&
695 wpa_printf(MSG_DEBUG, "nl80211: Local state "
696 "(not associated) does not match "
698 clear_state_mismatch(drv, r->bssid);
699 } else if (is_sta_interface(drv->nlmode) &&
700 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
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);
714 static struct wpa_scan_results *
715 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
718 struct wpa_scan_results *res;
720 struct nl80211_bss_info_arg arg;
722 res = os_zalloc(sizeof(*res));
725 if (!(msg = nl80211_cmd_msg(drv->first_bss, NLM_F_DUMP,
726 NL80211_CMD_GET_SCAN))) {
727 wpa_scan_results_free(res);
733 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
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);
740 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
741 "(%s)", ret, strerror(-ret));
742 wpa_scan_results_free(res);
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
752 struct wpa_scan_results * wpa_driver_nl80211_get_scan_results(void *priv)
754 struct i802_bss *bss = priv;
755 struct wpa_driver_nl80211_data *drv = bss->drv;
756 struct wpa_scan_results *res;
758 res = nl80211_get_scan_results(drv);
760 wpa_driver_nl80211_check_bss_status(drv, res);
765 void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
767 struct wpa_scan_results *res;
770 res = nl80211_get_scan_results(drv);
772 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
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]" : "");
784 wpa_scan_results_free(res);
788 #ifdef CONFIG_DRIVER_NL80211_QCA
790 static int scan_cookie_handler(struct nl_msg *msg, void *arg)
792 struct nlattr *tb[NL80211_ATTR_MAX + 1];
793 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
796 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
797 genlmsg_attrlen(gnlh, 0), NULL);
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];
803 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_SCAN_MAX,
804 nla_data(nl_vendor), nla_len(nl_vendor), NULL);
806 if (tb_vendor[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE])
807 *cookie = nla_get_u64(
808 tb_vendor[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]);
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
821 int wpa_driver_nl80211_vendor_scan(struct i802_bss *bss,
822 struct wpa_driver_scan_params *params)
824 struct wpa_driver_nl80211_data *drv = bss->drv;
825 struct nl_msg *msg = NULL;
832 wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: vendor scan request");
833 drv->scan_for_auth = 0;
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) )
841 attr = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
845 if (params->num_ssids) {
846 struct nlattr *ssids;
848 ssids = nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_SCAN_SSIDS);
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))
859 nla_nest_end(msg, ssids);
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))
871 struct nlattr *freqs;
873 freqs = nla_nest_start(msg,
874 QCA_WLAN_VENDOR_ATTR_SCAN_FREQUENCIES);
877 for (i = 0; params->freqs[i]; i++) {
878 wpa_printf(MSG_MSGDUMP,
879 "nl80211: Scan frequency %u MHz",
881 if (nla_put_u32(msg, i + 1, params->freqs[i]))
884 nla_nest_end(msg, freqs);
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;
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;
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;
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))
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))
921 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, scan_flags))
924 if (params->p2p_probe) {
925 struct nlattr *rates;
927 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
929 rates = nla_nest_start(msg,
930 QCA_WLAN_VENDOR_ATTR_SCAN_SUPP_RATES);
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.
940 if (nla_put(msg, NL80211_BAND_2GHZ, 8,
941 "\x0c\x12\x18\x24\x30\x48\x60\x6c"))
943 nla_nest_end(msg, rates);
945 if (nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_SCAN_TX_NO_CCK_RATE))
949 nla_nest_end(msg, attr);
951 ret = send_and_recv_msgs(drv, msg, scan_cookie_handler, &cookie);
954 wpa_printf(MSG_DEBUG,
955 "nl80211: Vendor scan trigger failed: ret=%d (%s)",
956 ret, strerror(-ret));
960 drv->vendor_scan_cookie = cookie;
961 drv->scan_state = SCAN_REQUESTED;
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,
969 drv->last_scan_cmd = NL80211_CMD_VENDOR;
976 #endif /* CONFIG_DRIVER_NL80211_QCA */