Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[libeap.git] / 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
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 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
45 {
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;
51
52         if (wpa_s->disconnected && !wpa_s->scan_req)
53                 return;
54
55         enabled = 0;
56         ssid = wpa_s->conf->ssid;
57         while (ssid) {
58                 if (!ssid->disabled) {
59                         enabled++;
60                         break;
61                 }
62                 ssid = ssid->next;
63         }
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;
68         }
69         scan_req = wpa_s->scan_req;
70         wpa_s->scan_req = 0;
71
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;
77         }
78
79         if (wpa_s->conf->ap_scan == 0) {
80                 wpa_supplicant_gen_assoc_event(wpa_s);
81                 return;
82         }
83
84         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
85             wpa_s->wpa_state == WPA_INACTIVE)
86                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
87
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;
94                         }
95                         ssid = ssid->next;
96                 }
97         }
98         while (ssid) {
99                 if (!ssid->disabled &&
100                     (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
101                         break;
102                 ssid = ssid->next;
103         }
104
105         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
106                 /*
107                  * ap_scan=2 mode - try to associate with each SSID instead of
108                  * scanning for each scan_ssid=1 network.
109                  */
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;
116                 }
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;
123                 }
124                 wpa_supplicant_associate(wpa_s, NULL, ssid);
125                 return;
126         }
127
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;
136
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;
145         }
146
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);
155         }
156
157         if (ret) {
158                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
159                 wpa_supplicant_req_scan(wpa_s, 10, 0);
160         }
161 }
162
163
164 /**
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
169  *
170  * This function is used to schedule a scan for neighboring access points after
171  * the specified time.
172  */
173 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
174 {
175         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
176                 sec, usec);
177         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
178         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
179 }
180
181
182 /**
183  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
184  * @wpa_s: Pointer to wpa_supplicant data
185  *
186  * This function is used to cancel a scan request scheduled with
187  * wpa_supplicant_req_scan().
188  */
189 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
190 {
191         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
192         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
193 }