1 #include "ieee80211softmac_priv.h"
6 * Before you can associate, you have to authenticate.
10 /* Sends out an association request to the desired AP */
12 ieee80211softmac_assoc(struct ieee80211softmac_device *mac, struct ieee80211softmac_network *net)
16 /* Switch to correct channel for this network */
17 mac->set_channel(mac->dev, net->channel);
19 /* Send association request */
20 ieee80211softmac_send_mgt_frame(mac, net, IEEE80211_STYPE_ASSOC_REQ, 0);
22 dprintk(KERN_INFO PFX "sent association request!\n");
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);
30 /* Set a timer for timeout */
31 /* FIXME: make timeout configurable */
32 schedule_delayed_work(&mac->associnfo.timeout, 5 * HZ);
36 ieee80211softmac_assoc_timeout(void *d)
38 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
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);
50 mac->associnfo.associating = 0;
51 mac->associnfo.bssvalid = 0;
53 spin_unlock_irqrestore(&mac->lock, flags);
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);
61 ieee80211softmac_reassoc(struct ieee80211softmac_device *mac)
67 /* Sends out a disassociation request to the desired AP */
69 ieee80211softmac_disassoc(struct ieee80211softmac_device *mac, u16 reason)
72 struct ieee80211softmac_network *found;
75 if (mac->associnfo.bssvalid && mac->associated) {
76 found = ieee80211softmac_get_network_by_bssid(mac, mac->associnfo.bssid);
78 ieee80211softmac_send_mgt_frame(mac, found, IEEE80211_STYPE_DISASSOC, reason);
79 } else if (mac->associnfo.associating) {
80 cancel_delayed_work(&mac->associnfo.timeout);
83 /* Change our state */
84 spin_lock_irqsave(&mac->lock, flags);
85 /* Do NOT clear bssvalid as that will break ieee80211softmac_assoc_work! */
87 mac->associnfo.associating = 0;
88 spin_unlock_irqrestore(&mac->lock, flags);
92 we_support_all_basic_rates(struct ieee80211softmac_device *mac, u8 *from, u8 from_len)
94 int idx, search, found;
97 for (idx = 0; idx < (from_len); idx++) {
99 if (!(rate & IEEE80211_BASIC_RATE_MASK))
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) {
118 network_matches_request(struct ieee80211softmac_device *mac, struct ieee80211_network *net)
120 /* we cannot associate to networks whose name we don't know */
121 if (ieee80211_is_empty_essid(net->ssid, net->ssid_len))
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))
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))
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))
134 if (net->ssid_len != mac->associnfo.req_essid.len)
136 if (!memcmp(net->ssid, mac->associnfo.req_essid.data, mac->associnfo.req_essid.len))
142 ieee80211softmac_assoc_notify(struct net_device *dev, void *context)
144 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
145 ieee80211softmac_assoc_work((void*)mac);
148 /* This function is called to handle userspace requests (asynchronously) */
150 ieee80211softmac_assoc_work(void *d)
152 struct ieee80211softmac_device *mac = (struct ieee80211softmac_device *)d;
153 struct ieee80211softmac_network *found = NULL;
154 struct ieee80211_network *net = NULL, *best = NULL;
161 ieee80211softmac_disassoc(mac, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
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);
167 /* Search the ieee80211 networks for this network if we didn't find it */
170 s8 rssi = -128; /* if I don't initialise, gcc emits an invalid warning
171 because it cannot follow the best pointer logic. */
172 spin_lock_irqsave(&mac->ieee->lock, flags);
173 list_for_each_entry(net, &mac->ieee->network_list, list) {
174 /* we're supposed to find the network with
175 * the best signal here, as we're asked to join
176 * any network with a specific ESSID, and many
177 * different ones could have that.
179 * I'll for now just go with the reported rssi.
181 * We also should take into account the rateset
182 * here to find the best BSSID to try.
184 if (network_matches_request(mac, net)) {
187 rssi = best->stats.rssi;
190 /* we already had a matching network, so
191 * compare their properties to get the
192 * better of the two ... (see above)
194 if (rssi < net->stats.rssi) {
196 rssi = best->stats.rssi;
200 /* if we unlock here, we might get interrupted and the `best'
201 * pointer could go stale */
203 found = ieee80211softmac_create_network(mac, best);
204 /* if found is still NULL, then we got -ENOMEM somewhere */
206 ieee80211softmac_add_network(mac, found);
208 spin_unlock_irqrestore(&mac->ieee->lock, flags);
212 if (mac->associnfo.scan_retry > 0) {
213 spin_lock_irqsave(&mac->lock, flags);
214 mac->associnfo.scan_retry--;
215 spin_unlock_irqrestore(&mac->lock, flags);
217 /* We know of no such network. Let's scan.
218 * NB: this also happens if we had no memory to copy the network info...
219 * Maybe we can hope to have more memory after scanning finishes ;)
221 dprintk(KERN_INFO PFX "Associate: Network not known, trying to initiate scan: ");
222 ieee80211softmac_notify(mac->dev, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, ieee80211softmac_assoc_notify, NULL);
223 if (ieee80211softmac_start_scan(mac))
224 dprintk("failed.\n");
230 spin_lock_irqsave(&mac->lock, flags);
231 mac->associnfo.associating = 0;
233 spin_unlock_irqrestore(&mac->lock, flags);
235 dprintk(KERN_INFO PFX "Unable to find network after scan!\n");
236 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_NET_NOT_FOUND, NULL);
241 mac->associnfo.bssvalid = 1;
242 memcpy(mac->associnfo.bssid, found->bssid, ETH_ALEN);
243 /* copy the ESSID for displaying it */
244 mac->associnfo.associate_essid.len = found->essid.len;
245 memcpy(mac->associnfo.associate_essid.data, found->essid.data, IW_ESSID_MAX_SIZE + 1);
247 /* we found a network! authenticate (if necessary) and associate to it. */
248 if (!found->authenticated) {
249 /* This relies on the fact that _auth_req only queues the work,
250 * otherwise adding the notification would be racy. */
251 if (!ieee80211softmac_auth_req(mac, found)) {
252 dprintk(KERN_INFO PFX "cannot associate without being authenticated, requested authentication\n");
253 ieee80211softmac_notify_internal(mac, IEEE80211SOFTMAC_EVENT_ANY, found, ieee80211softmac_assoc_notify, NULL, GFP_KERNEL);
255 printkl(KERN_WARNING PFX "Not authenticated, but requesting authentication failed. Giving up to associate\n");
256 ieee80211softmac_call_events(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, found);
260 /* finally! now we can start associating */
261 ieee80211softmac_assoc(mac, found);
264 /* call this to do whatever is necessary when we're associated */
266 ieee80211softmac_associated(struct ieee80211softmac_device *mac,
267 struct ieee80211_assoc_response * resp,
268 struct ieee80211softmac_network *net)
270 mac->associnfo.associating = 0;
272 if (mac->set_bssid_filter)
273 mac->set_bssid_filter(mac->dev, net->bssid);
274 memcpy(mac->ieee->bssid, net->bssid, ETH_ALEN);
275 netif_carrier_on(mac->dev);
277 mac->association_id = le16_to_cpup(&resp->aid);
280 /* received frame handling functions */
282 ieee80211softmac_handle_assoc_response(struct net_device * dev,
283 struct ieee80211_assoc_response * resp,
284 struct ieee80211_network * _ieee80211_network_do_not_use)
286 /* NOTE: the network parameter has to be ignored by
287 * this code because it is the ieee80211's pointer
288 * to the struct, not ours (we made a copy)
290 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
291 u16 status = le16_to_cpup(&resp->status);
292 struct ieee80211softmac_network *network = NULL;
295 spin_lock_irqsave(&mac->lock, flags);
297 if (!mac->associnfo.associating) {
298 /* we race against the timeout function, so make sure
299 * only one of us can do work */
300 spin_unlock_irqrestore(&mac->lock, flags);
303 network = ieee80211softmac_get_network_by_bssid_locked(mac, resp->header.addr3);
305 /* someone sending us things without us knowing him? Ignore. */
307 dprintk(KERN_INFO PFX "Received unrequested assocation response from " MAC_FMT "\n", MAC_ARG(resp->header.addr3));
308 spin_unlock_irqrestore(&mac->lock, flags);
312 /* now that we know it was for us, we can cancel the timeout */
313 cancel_delayed_work(&mac->associnfo.timeout);
317 dprintk(KERN_INFO PFX "associated!\n");
318 ieee80211softmac_associated(mac, resp, network);
319 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATED, network);
321 case WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH:
322 if (!network->auth_desynced_once) {
323 /* there seem to be a few rare cases where our view of
324 * the world is obscured, or buggy APs that don't DEAUTH
325 * us properly. So we handle that, but allow it only once.
327 printkl(KERN_INFO PFX "We were not authenticated during association, retrying...\n");
328 network->authenticated = 0;
329 /* we don't want to do this more than once ... */
330 network->auth_desynced_once = 1;
331 schedule_work(&mac->associnfo.work);
335 dprintk(KERN_INFO PFX "associating failed (reason: 0x%x)!\n", status);
336 mac->associnfo.associating = 0;
337 mac->associnfo.bssvalid = 0;
339 ieee80211softmac_call_events_locked(mac, IEEE80211SOFTMAC_EVENT_ASSOCIATE_FAILED, network);
342 spin_unlock_irqrestore(&mac->lock, flags);
347 ieee80211softmac_handle_disassoc(struct net_device * dev,
348 struct ieee80211_disassoc *disassoc)
350 struct ieee80211softmac_device *mac = ieee80211_priv(dev);
352 /* FIXME: check that this frame is from the right AP!! */
353 dprintk(KERN_INFO PFX "got disassoc frame\n");
354 netif_carrier_off(dev);
355 spin_lock_irqsave(&mac->lock, flags);
356 mac->associnfo.bssvalid = 0;
358 schedule_work(&mac->associnfo.work);
359 spin_unlock_irqrestore(&mac->lock, flags);