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