Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / wpa_supplicant / scan.c
blobc2549e284846130d2df7d7a2fe2acccc755262b2
1 /*
2 * WPA Supplicant - Scanning
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eloop.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "mlme.h"
22 #include "uuid.h"
25 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
27 struct wpa_ssid *ssid;
28 union wpa_event_data data;
30 ssid = wpa_supplicant_get_ssid(wpa_s);
31 if (ssid == NULL)
32 return;
34 if (wpa_s->current_ssid == NULL)
35 wpa_s->current_ssid = ssid;
36 wpa_supplicant_initiate_eapol(wpa_s);
37 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
38 "generating associated event");
39 os_memset(&data, 0, sizeof(data));
40 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
44 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
46 struct wpa_supplicant *wpa_s = eloop_ctx;
47 struct wpa_ssid *ssid;
48 int enabled, scan_req = 0, ret;
49 const u8 *extra_ie = NULL;
50 size_t extra_ie_len = 0;
52 if (wpa_s->disconnected && !wpa_s->scan_req)
53 return;
55 enabled = 0;
56 ssid = wpa_s->conf->ssid;
57 while (ssid) {
58 if (!ssid->disabled) {
59 enabled++;
60 break;
62 ssid = ssid->next;
64 if (!enabled && !wpa_s->scan_req) {
65 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
66 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
67 return;
69 scan_req = wpa_s->scan_req;
70 wpa_s->scan_req = 0;
72 if (wpa_s->conf->ap_scan != 0 &&
73 wpa_s->driver && os_strcmp(wpa_s->driver->name, "wired") == 0) {
74 wpa_printf(MSG_DEBUG, "Using wired driver - overriding "
75 "ap_scan configuration");
76 wpa_s->conf->ap_scan = 0;
79 if (wpa_s->conf->ap_scan == 0) {
80 wpa_supplicant_gen_assoc_event(wpa_s);
81 return;
84 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
85 wpa_s->wpa_state == WPA_INACTIVE)
86 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
88 ssid = wpa_s->conf->ssid;
89 if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
90 while (ssid) {
91 if (ssid == wpa_s->prev_scan_ssid) {
92 ssid = ssid->next;
93 break;
95 ssid = ssid->next;
98 while (ssid) {
99 if (!ssid->disabled &&
100 (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
101 break;
102 ssid = ssid->next;
105 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
107 * ap_scan=2 mode - try to associate with each SSID instead of
108 * scanning for each scan_ssid=1 network.
110 if (ssid == NULL) {
111 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
112 "end of scan list - go back to beginning");
113 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
114 wpa_supplicant_req_scan(wpa_s, 0, 0);
115 return;
117 if (ssid->next) {
118 /* Continue from the next SSID on the next attempt. */
119 wpa_s->prev_scan_ssid = ssid;
120 } else {
121 /* Start from the beginning of the SSID list. */
122 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
124 wpa_supplicant_associate(wpa_s, NULL, ssid);
125 return;
128 wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
129 ssid ? "specific": "broadcast");
130 if (ssid) {
131 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
132 ssid->ssid, ssid->ssid_len);
133 wpa_s->prev_scan_ssid = ssid;
134 } else
135 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
137 if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
138 !wpa_s->use_client_mlme) {
139 wpa_s->scan_res_tried++;
140 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
141 "first without requesting a new scan to speed up "
142 "initial association");
143 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
144 return;
147 if (wpa_s->use_client_mlme) {
148 ieee80211_sta_set_probe_req_ie(wpa_s, extra_ie, extra_ie_len);
149 ret = ieee80211_sta_req_scan(wpa_s, ssid ? ssid->ssid : NULL,
150 ssid ? ssid->ssid_len : 0);
151 } else {
152 wpa_drv_set_probe_req_ie(wpa_s, extra_ie, extra_ie_len);
153 ret = wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
154 ssid ? ssid->ssid_len : 0);
157 if (ret) {
158 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
159 wpa_supplicant_req_scan(wpa_s, 10, 0);
165 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
166 * @wpa_s: Pointer to wpa_supplicant data
167 * @sec: Number of seconds after which to scan
168 * @usec: Number of microseconds after which to scan
170 * This function is used to schedule a scan for neighboring access points after
171 * the specified time.
173 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
175 /* If there's at least one network that should be specifically scanned
176 * then don't cancel the scan and reschedule. Some drivers do
177 * background scanning which generates frequent scan results, and that
178 * causes the specific SSID scan to get continually pushed back and
179 * never happen, which causes hidden APs to never get probe-scanned.
181 if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
182 wpa_s->conf->ap_scan == 1) {
183 struct wpa_ssid *ssid = wpa_s->conf->ssid;
185 while (ssid) {
186 if (!ssid->disabled && ssid->scan_ssid)
187 break;
188 ssid = ssid->next;
190 if (ssid) {
191 wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
192 "ensure that specific SSID scans occur");
193 return;
197 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
198 sec, usec);
199 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
200 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
205 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
206 * @wpa_s: Pointer to wpa_supplicant data
208 * This function is used to cancel a scan request scheduled with
209 * wpa_supplicant_req_scan().
211 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
213 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
214 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);