Commit | Line | Data |
---|---|---|
370121e5 JB |
1 | #include "ieee80211softmac_priv.h" |
2 | ||
3 | /* | |
4 | * Overview | |
5 | * | |
6 | * Before you can associate, you have to authenticate. | |
7 | * | |
8 | */ | |
9 | ||
10 | /* Sends out an association request to the desired AP */ | |
11 | static void | |
12 | ieee80211softmac_assoc(struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net) | |
13 | { | |
14 | unsigned long flags; | |
15 | function_enter(); | |
16 | /* Switch to correct channel for this network */ | |
17 | mac->set_channel(mac->dev, net->channel); | |
18 | ||
19 | /* Send association request */ | |
20 | ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_ASSOC_REQ, 0); | |
21 | ||
22 | dprintk(KERN_INFO PFX "sent association request!\n"); | |
23 | ||
24 | /* Change the state to associating */ | |
25 | spin_lock_irqsave(&mac->lock, flags); | |
26 | mac->associnfo.associating = 1; | |
27 | mac->associated = 0; /* just to make sure */ | |
28 | spin_unlock_irqrestore(&mac->lock, flags); | |
29 | ||
30 | /* Set a timer for timeout */ | |
31 | /* FIXME: make timeout configurable */ | |
5c4df6da | 32 | schedule_delayed_work(&mac->associnfo.timeout, 5 * HZ); |
370121e5 JB |
33 | } |
34 | ||
35 | void | |
36 | ieee80211softmac_assoc_timeout(void *d) | |
37 | { | |
38 | struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d; | |
39 | unsigned long flags; | |
40 | ||
41 | function_enter(); | |
42 | ||
43 | spin_lock_irqsave(&mac->lock, flags); | |
44 | /* we might race against ieee80211softmac_handle_assoc_response, | |
45 | * so make sure only one of us does something */ | |
46 | if (!mac->associnfo.associating) { | |
47 | spin_unlock_irqrestore(&mac->lock, flags); | |
48 | return; | |
49 | } | |
50 | mac->associnfo.associating = 0; | |
51 | mac->associnfo.bssvalid = 0; | |
52 | mac->associated = 0; | |
53 | spin_unlock_irqrestore(&mac->lock, flags); | |
54 | ||
55 | dprintk(KERN_INFO PFX "assoc request timed out!\n"); | |
56 | /* FIXME: we need to know the network here. that requires a bit of restructuring */ | |
57 | ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_TIMEOUT, NULL); | |
58 | } | |
59 | ||
60 | static void | |
61 | ieee80211softmac_reassoc(struct ieee80211softmac_device *mac) | |
62 | { | |
63 | function_enter(); | |
64 | } | |
65 | ||
66 | ||
67 | /* Sends out a disassociation request to the desired AP */ | |
68 | static void | |
69 | ieee80211softmac_disassoc(struct ieee80211softmac_device *mac, u16 reason) | |
70 | { | |
71 | unsigned long flags; | |
72 | struct ieee80211softmac_network *found; | |
73 | function_enter(); | |
74 | ||
75 | if (mac->associnfo.bssvalid && mac->associated) { | |
76 | found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid); | |
77 | if (found) | |
78 | ieee80211softmac_send_mgt_frame(mac, found, IEEE80211_STYPE_DISASSOC, reason); | |
79 | } else if (mac->associnfo.associating) { | |
80 | cancel_delayed_work(&mac->associnfo.timeout); | |
81 | } | |
82 | ||
83 | /* Change our state */ | |
84 | spin_lock_irqsave(&mac->lock, flags); | |
85 | /* Do NOT clear bssvalid as that will break ieee80211softmac_assoc_work! */ | |
86 | mac->associated = 0; | |
87 | mac->associnfo.associating = 0; | |
88 | spin_unlock_irqrestore(&mac->lock, flags); | |
89 | } | |
90 | ||
91 | static inline int | |
92 | we_support_all_basic_rates(struct ieee80211softmac_device *mac, u8 *from, u8 from_len) | |
93 | { | |
94 | int idx, search, found; | |
95 | u8 rate, search_rate; | |
96 | ||
97 | for (idx = 0; idx < (from_len); idx++) { | |
98 | rate = (from)[idx]; | |
99 | if (!(rate & IEEE80211_BASIC_RATE_MASK)) | |
100 | continue; | |
101 | found = 0; | |
102 | rate &= ~IEEE80211_BASIC_RATE_MASK; | |
103 | for (search = 0; search < mac->ratesinfo.count; search++) { | |
104 | search_rate = mac->ratesinfo.rates[search]; | |
105 | search_rate &= ~IEEE80211_BASIC_RATE_MASK; | |
106 | if (rate == search_rate) { | |
107 | found = 1; | |
108 | break; | |
109 | } | |
110 | } | |
111 | if (!found) | |
112 | return 0; | |
113 | } | |
114 | return 1; | |
115 | } | |
116 | ||
117 | static int | |
118 | network_matches_request(struct ieee80211softmac_device *mac, struct ieee80211_network *net) | |
119 | { | |
120 | /* we cannot associate to networks whose name we don't know */ | |
121 | if (ieee80211_is_empty_essid(net->ssid, net->ssid_len)) | |
122 | return 0; | |
123 | /* do not associate to a network whose BSSBasicRateSet we cannot support */ | |
124 | if (!we_support_all_basic_rates(mac, net->rates, net->rates_len)) | |
125 | return 0; | |
126 | /* do we really need to check the ex rates? */ | |
127 | if (!we_support_all_basic_rates(mac, net->rates_ex, net->rates_ex_len)) | |
128 | return 0; | |
129 | ||
130 | /* if 'ANY' network requested, take any that doesn't have privacy enabled */ | |
131 | if (mac->associnfo.req_essid.len == 0 | |
132 | && !(net->capability & WLAN_CAPABILITY_PRIVACY)) | |
133 | return 1; | |
134 | if (net->ssid_len != mac->associnfo.req_essid.len) | |
135 | return 0; | |
136 | if (!memcmp(net->ssid, mac->associnfo.req_essid.data, mac->associnfo.req_essid.len)) | |
137 | return 1; | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static void | |
142 | ieee80211softmac_assoc_notify(struct net_device *dev, void *context) | |
143 | { | |
144 | struct ieee80211softmac_device *mac = ieee80211_priv(dev); | |
145 | ieee80211softmac_assoc_work((void*)mac); | |
146 | } | |
147 | ||
148 | /* This function is called to handle userspace requests (asynchronously) */ | |
149 | void | |
150 | ieee80211softmac_assoc_work(void *d) | |
151 | { | |
152 | struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d; | |
153 | struct ieee80211softmac_network *found = NULL; | |
154 | struct ieee80211_network *net = NULL, *best = NULL; | |
155 | unsigned long flags; | |
156 | ||
157 | function_enter(); | |
158 | ||
159 | /* meh */ | |
160 | if (mac->associated) | |
161 | ieee80211softmac_disassoc(mac, WLAN_REASON_DISASSOC_STA_HAS_LEFT); | |
162 | ||
163 | /* try to find the requested network in our list, if we found one already */ | |
164 | if (mac->associnfo.bssvalid) | |
165 | found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid); | |
166 | ||
167 | /* Search the ieee80211 networks for this network if we didn't find it */ | |
168 | if (!found) | |
169 | { | |
170 | spin_lock_irqsave(&mac->ieee->lock, flags); | |
171 | list_for_each_entry(net, &mac->ieee->network_list, list) { | |
172 | /* we're supposed to find the network with | |
173 | * the best signal here, as we're asked to join | |
174 | * any network with a specific ESSID, and many | |
175 | * different ones could have that. | |
176 | * | |
177 | * I'll for now implement just finding one at all | |
178 | * | |
179 | * We also should take into account the rateset | |
180 | * here to find the best BSSID to try. | |
181 | */ | |
182 | if (network_matches_request(mac, net)) { | |
183 | if (!best) { | |
184 | best = net; | |
185 | continue; | |
186 | } | |
187 | /* we already had a matching network, so | |
188 | * compare their properties to get the | |
189 | * better of the two ... (see above) | |
190 | */ | |
191 | /* TODO */ | |
192 | /* for now, just */ | |
193 | break; | |
194 | } | |
195 | } | |
196 | /* if we unlock here, we might get interrupted and the `best' | |
197 | * pointer could go stale */ | |
198 | if (best) { | |
199 | found = ieee80211softmac_create_network(mac, best); | |
200 | /* if found is still NULL, then we got -ENOMEM somewhere */ | |
201 | if (found) | |
202 | ieee80211softmac_add_network(mac, found); | |
203 | } | |
204 | spin_unlock_irqrestore(&mac->ieee->lock, flags); | |
205 | } | |
206 | ||
207 | if (!found) { | |
208 | if (mac->associnfo.scan_retry > 0) { | |
209 | spin_lock_irqsave(&mac->lock, flags); | |
210 | mac->associnfo.scan_retry--; | |
211 | spin_unlock_irqrestore(&mac->lock, flags); | |
212 | ||
213 | /* We know of no such network. Let's scan. | |
214 | * NB: this also happens if we had no memory to copy the network info... | |
215 | * Maybe we can hope to have more memory after scanning finishes ;) | |
216 | */ | |
217 | dprintk(KERN_INFO PFX "Associate: Network not known, trying to initiate scan: "); | |
218 | ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify, NULL); | |
219 | if (ieee80211softmac_start_scan(mac)) | |
220 | dprintk("failed.\n"); | |
221 | else | |
222 | dprintk("ok.\n"); | |
223 | return; | |
224 | } | |
225 | else { | |
226 | spin_lock_irqsave(&mac->lock, flags); | |
227 | mac->associnfo.associating = 0; | |
228 | mac->associated = 0; | |
229 | spin_unlock_irqrestore(&mac->lock, flags); | |
230 | ||
231 | dprintk(KERN_INFO PFX "Unable to find network after scan!\n"); | |
232 | ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL); | |
233 | return; | |
234 | } | |
235 | } | |
236 | ||
237 | mac->associnfo.bssvalid = 1; | |
238 | memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN); | |
239 | /* copy the ESSID for displaying it */ | |
240 | mac->associnfo.associate_essid.len = found->essid.len; | |
241 | memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1); | |
242 | ||
243 | /* we found a network! authenticate (if necessary) and associate to it. */ | |
244 | if (!found->authenticated) { | |
245 | /* This relies on the fact that _auth_req only queues the work, | |
246 | * otherwise adding the notification would be racy. */ | |
247 | if (!ieee80211softmac_auth_req(mac, found)) { | |
248 | dprintk(KERN_INFO PFX "cannot associate without being authenticated, requested authentication\n"); | |
249 | ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify, NULL, GFP_KERNEL); | |
250 | } else { | |
251 | printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n"); | |
252 | ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found); | |
253 | } | |
254 | return; | |
255 | } | |
256 | /* finally! now we can start associating */ | |
257 | ieee80211softmac_assoc(mac, found); | |
258 | } | |
259 | ||
260 | /* call this to do whatever is necessary when we're associated */ | |
261 | static void | |
262 | ieee80211softmac_associated(struct ieee80211softmac_device *mac, | |
263 | struct ieee80211_assoc_response * resp, | |
264 | struct ieee80211softmac_network *net) | |
265 | { | |
266 | mac->associnfo.associating = 0; | |
267 | mac->associated = 1; | |
268 | if (mac->set_bssid_filter) | |
269 | mac->set_bssid_filter(mac->dev, net->bssid); | |
270 | memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN); | |
2dd50801 | 271 | netif_carrier_on(mac->dev); |
370121e5 JB |
272 | |
273 | mac->association_id = le16_to_cpup(&resp->aid); | |
274 | } | |
275 | ||
276 | /* received frame handling functions */ | |
277 | int | |
278 | ieee80211softmac_handle_assoc_response(struct net_device * dev, | |
279 | struct ieee80211_assoc_response * resp, | |
280 | struct ieee80211_network * _ieee80211_network_do_not_use) | |
281 | { | |
282 | /* NOTE: the network parameter has to be ignored by | |
283 | * this code because it is the ieee80211's pointer | |
284 | * to the struct, not ours (we made a copy) | |
285 | */ | |
286 | struct ieee80211softmac_device *mac = ieee80211_priv(dev); | |
287 | u16 status = le16_to_cpup(&resp->status); | |
288 | struct ieee80211softmac_network *network = NULL; | |
289 | unsigned long flags; | |
290 | ||
291 | spin_lock_irqsave(&mac->lock, flags); | |
292 | ||
293 | if (!mac->associnfo.associating) { | |
294 | /* we race against the timeout function, so make sure | |
295 | * only one of us can do work */ | |
296 | spin_unlock_irqrestore(&mac->lock, flags); | |
297 | return 0; | |
298 | } | |
299 | network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3); | |
300 | ||
301 | /* someone sending us things without us knowing him? Ignore. */ | |
302 | if (!network) { | |
303 | dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3)); | |
304 | spin_unlock_irqrestore(&mac->lock, flags); | |
305 | return 0; | |
306 | } | |
307 | ||
308 | /* now that we know it was for us, we can cancel the timeout */ | |
309 | cancel_delayed_work(&mac->associnfo.timeout); | |
310 | ||
311 | switch (status) { | |
312 | case 0: | |
313 | dprintk(KERN_INFO PFX "associated!\n"); | |
314 | ieee80211softmac_associated(mac, resp, network); | |
315 | ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network); | |
316 | break; | |
317 | case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH: | |
318 | if (!network->auth_desynced_once) { | |
319 | /* there seem to be a few rare cases where our view of | |
320 | * the world is obscured, or buggy APs that don't DEAUTH | |
321 | * us properly. So we handle that, but allow it only once. | |
322 | */ | |
323 | printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n"); | |
324 | network->authenticated = 0; | |
325 | /* we don't want to do this more than once ... */ | |
326 | network->auth_desynced_once = 1; | |
5c4df6da | 327 | schedule_work(&mac->associnfo.work); |
370121e5 JB |
328 | break; |
329 | } | |
330 | default: | |
331 | dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status); | |
332 | mac->associnfo.associating = 0; | |
333 | mac->associnfo.bssvalid = 0; | |
334 | mac->associated = 0; | |
335 | ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network); | |
336 | } | |
337 | ||
338 | spin_unlock_irqrestore(&mac->lock, flags); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | int | |
343 | ieee80211softmac_handle_disassoc(struct net_device * dev, | |
344 | struct ieee80211_disassoc *disassoc) | |
345 | { | |
346 | struct ieee80211softmac_device *mac = ieee80211_priv(dev); | |
347 | unsigned long flags; | |
51da28a8 | 348 | /* FIXME: check that this frame is from the right AP!! */ |
370121e5 | 349 | dprintk(KERN_INFO PFX "got disassoc frame\n"); |
2dd50801 | 350 | netif_carrier_off(dev); |
370121e5 JB |
351 | spin_lock_irqsave(&mac->lock, flags); |
352 | mac->associnfo.bssvalid = 0; | |
353 | mac->associated = 0; | |
d1469cf2 | 354 | schedule_work(&mac->associnfo.work); |
370121e5 JB |
355 | spin_unlock_irqrestore(&mac->lock, flags); |
356 | ||
357 | return 0; | |
358 | } |