]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ieee80211/softmac/ieee80211softmac_module.c
[PATCH] softmac: add copyright and license headers
[linux-2.6-omap-h63xx.git] / net / ieee80211 / softmac / ieee80211softmac_module.c
1 /*
2  * Contains some basic softmac functions along with module registration code etc.
3  *
4  * Copyright (c) 2005 Johannes Berg <johannes@sipsolutions.net>
5  *                    Joseph Jezak <josejx@gentoo.org>
6  *                    Larry Finger <Larry.Finger@lwfinger.net>
7  *                    Danny van Dyk <kugelfang@gentoo.org>
8  *                    Michael Buesch <mbuesch@freenet.de>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  *
23  * The full GNU General Public License is included in this distribution in the
24  * file called COPYING.
25  */
26
27 #include "ieee80211softmac_priv.h"
28 #include <linux/sort.h>
29
30 struct net_device *alloc_ieee80211softmac(int sizeof_priv)
31 {
32         struct ieee80211softmac_device *softmac;
33         struct net_device *dev;
34         
35         dev = alloc_ieee80211(sizeof(struct ieee80211softmac_device) + sizeof_priv);
36         softmac = ieee80211_priv(dev);
37         softmac->dev = dev;
38         softmac->ieee = netdev_priv(dev);
39         spin_lock_init(&softmac->lock);
40         
41         softmac->ieee->handle_auth = ieee80211softmac_auth_resp;
42         softmac->ieee->handle_deauth = ieee80211softmac_deauth_resp;
43         softmac->ieee->handle_assoc_response = ieee80211softmac_handle_assoc_response;
44         softmac->ieee->handle_disassoc = ieee80211softmac_handle_disassoc;
45         softmac->scaninfo = NULL;
46
47         /* TODO: initialise all the other callbacks in the ieee struct
48          *       (once they're written)
49          */
50
51         INIT_LIST_HEAD(&softmac->auth_queue);
52         INIT_LIST_HEAD(&softmac->network_list);
53         INIT_LIST_HEAD(&softmac->events);
54
55         INIT_WORK(&softmac->associnfo.work, ieee80211softmac_assoc_work, softmac);
56         INIT_WORK(&softmac->associnfo.timeout, ieee80211softmac_assoc_timeout, softmac);
57         softmac->start_scan = ieee80211softmac_start_scan_implementation;
58         softmac->wait_for_scan = ieee80211softmac_wait_for_scan_implementation;
59         softmac->stop_scan = ieee80211softmac_stop_scan_implementation;
60
61         //TODO: The mcast rate has to be assigned dynamically somewhere (in scanning, association. Not sure...)
62         //      It has to be set to the highest rate all stations in the current network can handle.
63         softmac->txrates.mcast_rate = IEEE80211_CCK_RATE_1MB;
64         softmac->txrates.mcast_fallback = IEEE80211_CCK_RATE_1MB;
65         /* This is reassigned in ieee80211softmac_start to sane values. */
66         softmac->txrates.default_rate = IEEE80211_CCK_RATE_1MB;
67         softmac->txrates.default_fallback = IEEE80211_CCK_RATE_1MB;
68
69         /* to start with, we can't send anything ... */
70         netif_carrier_off(dev);
71         
72         return dev;
73 }
74
75 /* Clears the pending work queue items, stops all scans, etc. */
76 void 
77 ieee80211softmac_clear_pending_work(struct ieee80211softmac_device *sm)
78 {
79         unsigned long flags;
80         struct ieee80211softmac_event *eventptr, *eventtmp;
81         struct ieee80211softmac_auth_queue_item *authptr, *authtmp;
82         struct ieee80211softmac_network *netptr, *nettmp;
83         
84         ieee80211softmac_stop_scan(sm);
85         ieee80211softmac_wait_for_scan(sm);
86         
87         spin_lock_irqsave(&sm->lock, flags);
88         /* Free all pending assoc work items */
89         cancel_delayed_work(&sm->associnfo.work);
90         
91         /* Free all pending scan work items */
92         if(sm->scaninfo != NULL)
93                 cancel_delayed_work(&sm->scaninfo->softmac_scan);       
94         
95         /* Free all pending auth work items */
96         list_for_each_entry(authptr, &sm->auth_queue, list)
97                 cancel_delayed_work(&authptr->work);
98         
99         /* delete all pending event calls and work items */
100         list_for_each_entry_safe(eventptr, eventtmp, &sm->events, list)
101                 cancel_delayed_work(&eventptr->work);
102
103         spin_unlock_irqrestore(&sm->lock, flags);
104         flush_scheduled_work();
105
106         /* now we should be save and no longer need locking... */
107         spin_lock_irqsave(&sm->lock, flags);
108         /* Free all pending auth work items */
109         list_for_each_entry_safe(authptr, authtmp, &sm->auth_queue, list) {
110                 list_del(&authptr->list);
111                 kfree(authptr);
112         }
113         
114         /* delete all pending event calls and work items */
115         list_for_each_entry_safe(eventptr, eventtmp, &sm->events, list) {
116                 list_del(&eventptr->list);
117                 kfree(eventptr);
118         }
119                 
120         /* Free all networks */
121         list_for_each_entry_safe(netptr, nettmp, &sm->network_list, list) {
122                 ieee80211softmac_del_network_locked(sm, netptr);
123                 if(netptr->challenge != NULL)
124                         kfree(netptr->challenge);
125                 kfree(netptr);
126         }
127
128         spin_unlock_irqrestore(&sm->lock, flags);
129 }
130
131 void free_ieee80211softmac(struct net_device *dev)
132 {
133         struct ieee80211softmac_device *sm = ieee80211_priv(dev);
134         ieee80211softmac_clear_pending_work(sm);        
135         kfree(sm->scaninfo);
136         kfree(sm->wpa.IE);
137         free_ieee80211(dev);
138 }
139
140 static void ieee80211softmac_start_check_rates(struct ieee80211softmac_device *mac)
141 {
142         struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
143         /* I took out the sorting check, we're seperating by modulation now. */
144         if (ri->count)
145                 return;
146         /* otherwise assume we hav'em all! */
147         if (mac->ieee->modulation & IEEE80211_CCK_MODULATION) {
148                 ri->rates[ri->count++] = IEEE80211_CCK_RATE_1MB;
149                 ri->rates[ri->count++] = IEEE80211_CCK_RATE_2MB;
150                 ri->rates[ri->count++] = IEEE80211_CCK_RATE_5MB;
151                 ri->rates[ri->count++] = IEEE80211_CCK_RATE_11MB;
152         }
153         if (mac->ieee->modulation & IEEE80211_OFDM_MODULATION) {
154                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_6MB;
155                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_9MB;
156                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_12MB;
157                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_18MB;
158                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_24MB;
159                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_36MB;
160                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_48MB;
161                 ri->rates[ri->count++] = IEEE80211_OFDM_RATE_54MB;
162         }
163 }
164
165 void ieee80211softmac_start(struct net_device *dev)
166 {
167         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
168         struct ieee80211_device *ieee = mac->ieee;
169         u32 change = 0;
170         struct ieee80211softmac_txrates oldrates;
171
172         ieee80211softmac_start_check_rates(mac);
173
174         /* TODO: We need some kind of state machine to lower the default rates
175          *       if we loose too many packets.
176          */
177         /* Change the default txrate to the highest possible value.
178          * The txrate machine will lower it, if it is too high.
179          */
180         if (mac->txrates_change)
181                 oldrates = mac->txrates;
182         if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
183                 mac->txrates.default_rate = IEEE80211_OFDM_RATE_54MB;
184                 change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
185                 mac->txrates.default_fallback = IEEE80211_OFDM_RATE_24MB;
186                 change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
187         } else if (ieee->modulation & IEEE80211_CCK_MODULATION) {
188                 mac->txrates.default_rate = IEEE80211_CCK_RATE_11MB;
189                 change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
190                 mac->txrates.default_fallback = IEEE80211_CCK_RATE_5MB;
191                 change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
192         } else
193                 assert(0);
194         if (mac->txrates_change)
195                 mac->txrates_change(dev, change, &oldrates);
196 }
197
198 void ieee80211softmac_stop(struct net_device *dev)
199 {
200         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
201
202         ieee80211softmac_clear_pending_work(mac);
203 }
204
205 void ieee80211softmac_set_rates(struct net_device *dev, u8 count, u8 *rates)
206 {
207         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
208         unsigned long flags;
209         
210         spin_lock_irqsave(&mac->lock, flags);
211         memcpy(mac->ratesinfo.rates, rates, count);
212         mac->ratesinfo.count = count;
213         spin_unlock_irqrestore(&mac->lock, flags);
214 }
215
216 static u8 raise_rate(struct ieee80211softmac_device *mac, u8 rate)
217 {
218         int i;
219         struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
220         
221         for (i=0; i<ri->count-1; i++) {
222                 if (ri->rates[i] == rate)
223                         return ri->rates[i+1];
224         }
225         /* I guess we can't go any higher... */
226         return ri->rates[ri->count];
227 }
228
229 u8 ieee80211softmac_lower_rate_delta(struct ieee80211softmac_device *mac, u8 rate, int delta)
230 {
231         int i;
232         struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
233         
234         for (i=delta; i<ri->count; i++) {
235                 if (ri->rates[i] == rate)
236                         return ri->rates[i-delta];
237         }
238         /* I guess we can't go any lower... */
239         return ri->rates[0];
240 }
241
242 static void ieee80211softmac_add_txrates_badness(struct ieee80211softmac_device *mac,
243                                                  int amount)
244 {
245         struct ieee80211softmac_txrates oldrates;
246         u8 default_rate = mac->txrates.default_rate;
247         u8 default_fallback = mac->txrates.default_fallback;
248         u32 changes = 0;
249
250         //TODO: This is highly experimental code.
251         //      Maybe the dynamic rate selection does not work
252         //      and it has to be removed again.
253
254 printk("badness %d\n", mac->txrate_badness);
255         mac->txrate_badness += amount;
256         if (mac->txrate_badness <= -1000) {
257                 /* Very small badness. Try a faster bitrate. */
258                 if (mac->txrates_change)
259                         memcpy(&oldrates, &mac->txrates, sizeof(oldrates));
260                 default_rate = raise_rate(mac, default_rate);
261                 changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
262                 default_fallback = get_fallback_rate(mac, default_rate);
263                 changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
264                 mac->txrate_badness = 0;
265 printk("Bitrate raised to %u\n", default_rate);
266         } else if (mac->txrate_badness >= 10000) {
267                 /* Very high badness. Try a slower bitrate. */
268                 if (mac->txrates_change)
269                         memcpy(&oldrates, &mac->txrates, sizeof(oldrates));
270                 default_rate = lower_rate(mac, default_rate);
271                 changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
272                 default_fallback = get_fallback_rate(mac, default_rate);
273                 changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
274                 mac->txrate_badness = 0;
275 printk("Bitrate lowered to %u\n", default_rate);
276         }
277
278         mac->txrates.default_rate = default_rate;
279         mac->txrates.default_fallback = default_fallback;
280
281         if (changes && mac->txrates_change)
282                 mac->txrates_change(mac->dev, changes, &oldrates);
283 }
284
285 void ieee80211softmac_fragment_lost(struct net_device *dev,
286                                     u16 wl_seq)
287 {
288         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
289         unsigned long flags;
290
291         spin_lock_irqsave(&mac->lock, flags);
292         ieee80211softmac_add_txrates_badness(mac, 1000);
293         //TODO
294
295         spin_unlock_irqrestore(&mac->lock, flags);
296 }
297
298 static int rate_cmp(const void *a_, const void *b_) {
299         u8 *a, *b;
300         a = (u8*)a_;
301         b = (u8*)b_;
302         return ((*a & ~IEEE80211_BASIC_RATE_MASK) - (*b & ~IEEE80211_BASIC_RATE_MASK));
303 }
304
305 /* Allocate a softmac network struct and fill it from a network */
306 struct ieee80211softmac_network *
307 ieee80211softmac_create_network(struct ieee80211softmac_device *mac,
308         struct ieee80211_network *net)
309 {
310         struct ieee80211softmac_network *softnet;
311         softnet = kzalloc(sizeof(struct ieee80211softmac_network), GFP_ATOMIC);
312         if(softnet == NULL)
313                 return NULL;
314         memcpy(softnet->bssid, net->bssid, ETH_ALEN);
315         softnet->channel = net->channel;
316         softnet->essid.len = net->ssid_len;
317         memcpy(softnet->essid.data, net->ssid, softnet->essid.len);
318         
319         /* copy rates over */
320         softnet->supported_rates.count = net->rates_len;
321         memcpy(&softnet->supported_rates.rates[0], net->rates, net->rates_len);
322         memcpy(&softnet->supported_rates.rates[softnet->supported_rates.count], net->rates_ex, net->rates_ex_len);
323         softnet->supported_rates.count += net->rates_ex_len;
324         sort(softnet->supported_rates.rates, softnet->supported_rates.count, sizeof(softnet->supported_rates.rates[0]), rate_cmp, NULL);
325         
326         softnet->capabilities = net->capability;
327         return softnet;
328 }
329
330
331 /* Add a network to the list, while locked */
332 void
333 ieee80211softmac_add_network_locked(struct ieee80211softmac_device *mac,
334         struct ieee80211softmac_network *add_net)
335 {
336         struct list_head *list_ptr;
337         struct ieee80211softmac_network *softmac_net = NULL;
338
339         list_for_each(list_ptr, &mac->network_list) {
340                 softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
341                 if(!memcmp(softmac_net->bssid, add_net->bssid, ETH_ALEN))
342                         break;
343                 else
344                         softmac_net = NULL;
345         }
346         if(softmac_net == NULL)
347                 list_add(&(add_net->list), &mac->network_list);
348 }
349
350 /* Add a network to the list, with locking */
351 void
352 ieee80211softmac_add_network(struct ieee80211softmac_device *mac,
353         struct ieee80211softmac_network *add_net)
354 {
355         unsigned long flags;
356         spin_lock_irqsave(&mac->lock, flags);
357         ieee80211softmac_add_network_locked(mac, add_net);
358         spin_unlock_irqrestore(&mac->lock, flags);
359 }
360
361
362 /* Delete a network from the list, while locked*/
363 void
364 ieee80211softmac_del_network_locked(struct ieee80211softmac_device *mac,
365         struct ieee80211softmac_network *del_net)
366 {
367         list_del(&(del_net->list));
368 }
369
370 /* Delete a network from the list with locking */
371 void
372 ieee80211softmac_del_network(struct ieee80211softmac_device *mac,
373         struct ieee80211softmac_network *del_net)
374 {
375         unsigned long flags;
376         spin_lock_irqsave(&mac->lock, flags);
377         ieee80211softmac_del_network_locked(mac, del_net);
378         spin_unlock_irqrestore(&mac->lock, flags);
379 }
380
381 /* Get a network from the list by MAC while locked */
382 struct ieee80211softmac_network *
383 ieee80211softmac_get_network_by_bssid_locked(struct ieee80211softmac_device *mac,
384         u8 *bssid)
385 {
386         struct list_head *list_ptr;
387         struct ieee80211softmac_network *softmac_net = NULL;
388         list_for_each(list_ptr, &mac->network_list) {
389                 softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
390                 if(!memcmp(softmac_net->bssid, bssid, ETH_ALEN))
391                         break;
392                 else
393                         softmac_net = NULL;
394         }
395         return softmac_net;
396 }
397
398 /* Get a network from the list by BSSID with locking */
399 struct ieee80211softmac_network *
400 ieee80211softmac_get_network_by_bssid(struct ieee80211softmac_device *mac,
401         u8 *bssid)
402 {
403         unsigned long flags;
404         struct ieee80211softmac_network *softmac_net;
405         
406         spin_lock_irqsave(&mac->lock, flags);
407         softmac_net = ieee80211softmac_get_network_by_bssid_locked(mac, bssid);
408         spin_unlock_irqrestore(&mac->lock, flags);
409         return softmac_net;
410 }
411
412 /* Get a network from the list by ESSID while locked */
413 struct ieee80211softmac_network *
414 ieee80211softmac_get_network_by_essid_locked(struct ieee80211softmac_device *mac,
415         struct ieee80211softmac_essid *essid)
416 {
417         struct list_head *list_ptr;
418         struct ieee80211softmac_network *softmac_net = NULL;
419
420         list_for_each(list_ptr, &mac->network_list) {
421                 softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
422                 if (softmac_net->essid.len == essid->len &&
423                         !memcmp(softmac_net->essid.data, essid->data, essid->len))
424                         return softmac_net;
425         }
426         return NULL;
427 }
428
429 /* Get a network from the list by ESSID with locking */
430 struct ieee80211softmac_network *
431 ieee80211softmac_get_network_by_essid(struct ieee80211softmac_device *mac,
432         struct ieee80211softmac_essid *essid)   
433 {
434         unsigned long flags;
435         struct ieee80211softmac_network *softmac_net = NULL;
436
437         spin_lock_irqsave(&mac->lock, flags);
438         softmac_net = ieee80211softmac_get_network_by_essid_locked(mac, essid); 
439         spin_unlock_irqrestore(&mac->lock, flags);
440         return softmac_net;
441 }
442
443 MODULE_LICENSE("GPL");
444
445 EXPORT_SYMBOL_GPL(alloc_ieee80211softmac);
446 EXPORT_SYMBOL_GPL(free_ieee80211softmac);
447 EXPORT_SYMBOL_GPL(ieee80211softmac_set_rates);
448 EXPORT_SYMBOL_GPL(ieee80211softmac_start);
449 EXPORT_SYMBOL_GPL(ieee80211softmac_stop);
450 EXPORT_SYMBOL_GPL(ieee80211softmac_fragment_lost);
451 EXPORT_SYMBOL_GPL(ieee80211softmac_clear_pending_work);