fb64ca586ddcdb6db3098e205f08198d135c23db
[wpasupplicant] / wpa_supplicant / scan.c
1 /*
2  * WPA Supplicant - Scanning
3  * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "mlme.h"
22 #include "wps/wps.h"
23
24
25 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
26 {
27         struct wpa_ssid *ssid;
28         union wpa_event_data data;
29
30         ssid = wpa_supplicant_get_ssid(wpa_s);
31         if (ssid == NULL)
32                 return;
33
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);
41 }
42
43
44 #ifdef CONFIG_WPS
45 static int wpas_wps_in_use(struct wpa_config *conf)
46 {
47         struct wpa_ssid *ssid;
48         int wps = 0;
49
50         for (ssid = conf->ssid; ssid; ssid = ssid->next) {
51                 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
52                         continue;
53
54                 wps = 1;
55                 if (!ssid->eap.phase1)
56                         continue;
57
58                 if (os_strstr(ssid->eap.phase1, "pbc=1"))
59                         return 2;
60         }
61
62         return wps;
63 }
64 #endif /* CONFIG_WPS */
65
66 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
67 {
68         struct wpa_supplicant *wpa_s = eloop_ctx;
69         struct wpa_ssid *ssid;
70         int enabled, scan_req = 0, ret;
71         struct wpabuf *wps_ie = NULL;
72         const u8 *extra_ie = NULL;
73         size_t extra_ie_len = 0;
74         int wps = 0;
75
76         if (wpa_s->disconnected && !wpa_s->scan_req)
77                 return;
78
79         enabled = 0;
80         ssid = wpa_s->conf->ssid;
81         while (ssid) {
82                 if (!ssid->disabled) {
83                         enabled++;
84                         break;
85                 }
86                 ssid = ssid->next;
87         }
88         if (!enabled && !wpa_s->scan_req) {
89                 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
90                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
91                 return;
92         }
93         scan_req = wpa_s->scan_req;
94         wpa_s->scan_req = 0;
95
96         if (wpa_s->conf->ap_scan != 0 &&
97             wpa_s->driver && IS_WIRED(wpa_s->driver)) {
98                 wpa_printf(MSG_DEBUG, "Using wired authentication - "
99                            "overriding ap_scan configuration");
100                 wpa_s->conf->ap_scan = 0;
101         }
102
103         if (wpa_s->conf->ap_scan == 0) {
104                 wpa_supplicant_gen_assoc_event(wpa_s);
105                 return;
106         }
107
108         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
109             wpa_s->wpa_state == WPA_INACTIVE)
110                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
111
112         ssid = wpa_s->conf->ssid;
113         if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
114                 while (ssid) {
115                         if (ssid == wpa_s->prev_scan_ssid) {
116                                 ssid = ssid->next;
117                                 break;
118                         }
119                         ssid = ssid->next;
120                 }
121         }
122         while (ssid) {
123                 if (!ssid->disabled &&
124                     (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
125                         break;
126                 ssid = ssid->next;
127         }
128
129         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
130                 /*
131                  * ap_scan=2 mode - try to associate with each SSID instead of
132                  * scanning for each scan_ssid=1 network.
133                  */
134                 if (ssid == NULL) {
135                         wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
136                                    "end of scan list - go back to beginning");
137                         wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
138                         wpa_supplicant_req_scan(wpa_s, 0, 0);
139                         return;
140                 }
141                 if (ssid->next) {
142                         /* Continue from the next SSID on the next attempt. */
143                         wpa_s->prev_scan_ssid = ssid;
144                 } else {
145                         /* Start from the beginning of the SSID list. */
146                         wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
147                 }
148                 wpa_supplicant_associate(wpa_s, NULL, ssid);
149                 return;
150         }
151
152         wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
153                    ssid ? "specific": "broadcast");
154         if (ssid) {
155                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
156                                   ssid->ssid, ssid->ssid_len);
157                 wpa_s->prev_scan_ssid = ssid;
158         } else
159                 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
160
161 #ifdef CONFIG_WPS
162         wps = wpas_wps_in_use(wpa_s->conf);
163 #endif /* CONFIG_WPS */
164
165         if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
166             !wpa_s->use_client_mlme && wps != 2) {
167                 wpa_s->scan_res_tried++;
168                 wpa_s->scan_req = scan_req;
169                 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
170                            "first without requesting a new scan to speed up "
171                            "initial association");
172                 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
173                 return;
174         }
175
176 #ifdef CONFIG_WPS
177         if (wps) {
178                 wps_ie = wps_enrollee_build_probe_req_ie(wps == 2,
179                                                          &wpa_s->wps->dev,
180                                                          wpa_s->conf->uuid);
181                 if (wps_ie) {
182                         extra_ie = wpabuf_head(wps_ie);
183                         extra_ie_len = wpabuf_len(wps_ie);
184                 }
185         }
186 #endif /* CONFIG_WPS */
187
188         if (wpa_s->use_client_mlme) {
189                 ieee80211_sta_set_probe_req_ie(wpa_s, extra_ie, extra_ie_len);
190                 ret = ieee80211_sta_req_scan(wpa_s, ssid ? ssid->ssid : NULL,
191                                              ssid ? ssid->ssid_len : 0);
192         } else {
193                 wpa_drv_set_probe_req_ie(wpa_s, extra_ie, extra_ie_len);
194                 ret = wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
195                                    ssid ? ssid->ssid_len : 0);
196         }
197
198         wpabuf_free(wps_ie);
199
200         if (ret) {
201                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
202                 wpa_supplicant_req_scan(wpa_s, 10, 0);
203         }
204 }
205
206
207 /**
208  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
209  * @wpa_s: Pointer to wpa_supplicant data
210  * @sec: Number of seconds after which to scan
211  * @usec: Number of microseconds after which to scan
212  *
213  * This function is used to schedule a scan for neighboring access points after
214  * the specified time.
215  */
216 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
217 {
218         /* If there's at least one network that should be specifically scanned
219          * then don't cancel the scan and reschedule.  Some drivers do
220          * background scanning which generates frequent scan results, and that
221          * causes the specific SSID scan to get continually pushed back and
222          * never happen, which causes hidden APs to never get probe-scanned.
223          */
224         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
225             wpa_s->conf->ap_scan == 1) {
226                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
227
228                 while (ssid) {
229                         if (!ssid->disabled && ssid->scan_ssid)
230                                 break;
231                         ssid = ssid->next;
232                 }
233                 if (ssid) {
234                         wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
235                                 "ensure that specific SSID scans occur");
236                         return;
237                 }
238         }
239
240         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
241                 sec, usec);
242         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
243         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
244 }
245
246
247 /**
248  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
249  * @wpa_s: Pointer to wpa_supplicant data
250  *
251  * This function is used to cancel a scan request scheduled with
252  * wpa_supplicant_req_scan().
253  */
254 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
255 {
256         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
257         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
258 }