summaryrefslogtreecommitdiff
path: root/net/ieee80211/softmac/ieee80211softmac_module.c
blob: be83bdc1644a54e35b64a0ad21a5e1e23208f71a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * Contains some basic softmac functions along with module registration code etc.
 *
 * Copyright (c) 2005, 2006 Johannes Berg <johannes@sipsolutions.net>
 *                          Joseph Jezak <josejx@gentoo.org>
 *                          Larry Finger <Larry.Finger@lwfinger.net>
 *                          Danny van Dyk <kugelfang@gentoo.org>
 *                          Michael Buesch <mbuesch@freenet.de>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called COPYING.
 */

#include "ieee80211softmac_priv.h"
#include <linux/sort.h>

struct net_device *alloc_ieee80211softmac(int sizeof_priv)
{
	struct ieee80211softmac_device *softmac;
	struct net_device *dev;
	
	dev = alloc_ieee80211(sizeof(struct ieee80211softmac_device) + sizeof_priv);
	softmac = ieee80211_priv(dev);
	softmac->dev = dev;
	softmac->ieee = netdev_priv(dev);
	spin_lock_init(&softmac->lock);
	
	softmac->ieee->handle_auth = ieee80211softmac_auth_resp;
	softmac->ieee->handle_deauth = ieee80211softmac_deauth_resp;
	softmac->ieee->handle_assoc_response = ieee80211softmac_handle_assoc_response;
	softmac->ieee->handle_reassoc_request = ieee80211softmac_handle_reassoc_req;
	softmac->ieee->handle_disassoc = ieee80211softmac_handle_disassoc;
	softmac->scaninfo = NULL;

	softmac->associnfo.scan_retry = IEEE80211SOFTMAC_ASSOC_SCAN_RETRY_LIMIT;

	/* TODO: initialise all the other callbacks in the ieee struct
	 *	 (once they're written)
	 */

	INIT_LIST_HEAD(&softmac->auth_queue);
	INIT_LIST_HEAD(&softmac->network_list);
	INIT_LIST_HEAD(&softmac->events);

	INIT_WORK(&softmac->associnfo.work, ieee80211softmac_assoc_work, softmac);
	INIT_WORK(&softmac->associnfo.timeout, ieee80211softmac_assoc_timeout, softmac);
	softmac->start_scan = ieee80211softmac_start_scan_implementation;
	softmac->wait_for_scan = ieee80211softmac_wait_for_scan_implementation;
	softmac->stop_scan = ieee80211softmac_stop_scan_implementation;

	//TODO: The mcast rate has to be assigned dynamically somewhere (in scanning, association. Not sure...)
	//      It has to be set to the highest rate all stations in the current network can handle.
	softmac->txrates.mcast_rate = IEEE80211_CCK_RATE_1MB;
	softmac->txrates.mcast_fallback = IEEE80211_CCK_RATE_1MB;
	/* This is reassigned in ieee80211softmac_start to sane values. */
	softmac->txrates.default_rate = IEEE80211_CCK_RATE_1MB;
	softmac->txrates.default_fallback = IEEE80211_CCK_RATE_1MB;

	/* to start with, we can't send anything ... */
	netif_carrier_off(dev);
	
	return dev;
}
EXPORT_SYMBOL_GPL(alloc_ieee80211softmac);

/* Clears the pending work queue items, stops all scans, etc. */
void 
ieee80211softmac_clear_pending_work(struct ieee80211softmac_device *sm)
{
	unsigned long flags;
	struct ieee80211softmac_event *eventptr, *eventtmp;
	struct ieee80211softmac_auth_queue_item *authptr, *authtmp;
	struct ieee80211softmac_network *netptr, *nettmp;
	
	ieee80211softmac_stop_scan(sm);
	ieee80211softmac_wait_for_scan(sm);
	
	spin_lock_irqsave(&sm->lock, flags);
	/* Free all pending assoc work items */
	cancel_delayed_work(&sm->associnfo.work);
	
	/* Free all pending scan work items */
	if(sm->scaninfo != NULL)
		cancel_delayed_work(&sm->scaninfo->softmac_scan);	
	
	/* Free all pending auth work items */
	list_for_each_entry(authptr, &sm->auth_queue, list)
		cancel_delayed_work(&authptr->work);
	
	/* delete all pending event calls and work items */
	list_for_each_entry_safe(eventptr, eventtmp, &sm->events, list)
		cancel_delayed_work(&eventptr->work);

	spin_unlock_irqrestore(&sm->lock, flags);
	flush_scheduled_work();

	/* now we should be save and no longer need locking... */
	spin_lock_irqsave(&sm->lock, flags);
	/* Free all pending auth work items */
	list_for_each_entry_safe(authptr, authtmp, &sm->auth_queue, list) {
		list_del(&authptr->list);
		kfree(authptr);
	}
	
	/* delete all pending event calls and work items */
	list_for_each_entry_safe(eventptr, eventtmp, &sm->events, list) {
		list_del(&eventptr->list);
		kfree(eventptr);
	}
		
	/* Free all networks */
	list_for_each_entry_safe(netptr, nettmp, &sm->network_list, list) {
		ieee80211softmac_del_network_locked(sm, netptr);
		if(netptr->challenge != NULL)
			kfree(netptr->challenge);
		kfree(netptr);
	}

	spin_unlock_irqrestore(&sm->lock, flags);
}
EXPORT_SYMBOL_GPL(ieee80211softmac_clear_pending_work);

void free_ieee80211softmac(struct net_device *dev)
{
	struct ieee80211softmac_device *sm = ieee80211_priv(dev);
	ieee80211softmac_clear_pending_work(sm);	
	kfree(sm->scaninfo);
	kfree(sm->wpa.IE);
	free_ieee80211(dev);
}
EXPORT_SYMBOL_GPL(free_ieee80211softmac);

static void ieee80211softmac_start_check_rates(struct ieee80211softmac_device *mac)
{
	struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
	/* I took out the sorting check, we're seperating by modulation now. */
	if (ri->count)
		return;
	/* otherwise assume we hav'em all! */
	if (mac->ieee->modulation & IEEE80211_CCK_MODULATION) {
		ri->rates[ri->count++] = IEEE80211_CCK_RATE_1MB;
		ri->rates[ri->count++] = IEEE80211_CCK_RATE_2MB;
		ri->rates[ri->count++] = IEEE80211_CCK_RATE_5MB;
		ri->rates[ri->count++] = IEEE80211_CCK_RATE_11MB;
	}
	if (mac->ieee->modulation & IEEE80211_OFDM_MODULATION) {
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_6MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_9MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_12MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_18MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_24MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_36MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_48MB;
		ri->rates[ri->count++] = IEEE80211_OFDM_RATE_54MB;
	}
}

void ieee80211softmac_start(struct net_device *dev)
{
	struct ieee80211softmac_device *mac = ieee80211_priv(dev);
	struct ieee80211_device *ieee = mac->ieee;
	u32 change = 0;
	struct ieee80211softmac_txrates oldrates;

	ieee80211softmac_start_check_rates(mac);

	/* TODO: We need some kind of state machine to lower the default rates
	 *       if we loose too many packets.
	 */
	/* Change the default txrate to the highest possible value.
	 * The txrate machine will lower it, if it is too high.
	 */
	if (mac->txrates_change)
		oldrates = mac->txrates;
	/* FIXME: We don't correctly handle backing down to lower
	   rates, so 801.11g devices start off at 11M for now. People
	   can manually change it if they really need to, but 11M is
	   more reliable. Note similar logic in
	   ieee80211softmac_wx_set_rate() */	 
	if (ieee->modulation & IEEE80211_CCK_MODULATION) {
		mac->txrates.default_rate = IEEE80211_CCK_RATE_11MB;
		change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
		mac->txrates.default_fallback = IEEE80211_CCK_RATE_5MB;
		change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
	} else if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
		mac->txrates.default_rate = IEEE80211_OFDM_RATE_54MB;
		change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
		mac->txrates.default_fallback = IEEE80211_OFDM_RATE_24MB;
		change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
	} else
		assert(0);
	if (mac->txrates_change)
		mac->txrates_change(dev, change, &oldrates);
}
EXPORT_SYMBOL_GPL(ieee80211softmac_start);

void ieee80211softmac_stop(struct net_device *dev)
{
	struct ieee80211softmac_device *mac = ieee80211_priv(dev);

	ieee80211softmac_clear_pending_work(mac);
}
EXPORT_SYMBOL_GPL(ieee80211softmac_stop);

void ieee80211softmac_set_rates(struct net_device *dev, u8 count, u8 *rates)
{
	struct ieee80211softmac_device *mac = ieee80211_priv(dev);
	unsigned long flags;
	
	spin_lock_irqsave(&mac->lock, flags);
	memcpy(mac->ratesinfo.rates, rates, count);
	mac->ratesinfo.count = count;
	spin_unlock_irqrestore(&mac->lock, flags);
}
EXPORT_SYMBOL_GPL(ieee80211softmac_set_rates);

static u8 raise_rate(struct ieee80211softmac_device *mac, u8 rate)
{
	int i;
	struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
	
	for (i=0; i<ri->count-1; i++) {
		if (ri->rates[i] == rate)
			return ri->rates[i+1];
	}
	/* I guess we can't go any higher... */
	return ri->rates[ri->count];
}

u8 ieee80211softmac_lower_rate_delta(struct ieee80211softmac_device *mac, u8 rate, int delta)
{
	int i;
	struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
	
	for (i=delta; i<ri->count; i++) {
		if (ri->rates[i] == rate)
			return ri->rates[i-delta];
	}
	/* I guess we can't go any lower... */
	return ri->rates[0];
}

static void ieee80211softmac_add_txrates_badness(struct ieee80211softmac_device *mac,
						 int amount)
{
	struct ieee80211softmac_txrates oldrates;
	u8 default_rate = mac->txrates.default_rate;
	u8 default_fallback = mac->txrates.default_fallback;
	u32 changes = 0;

	//TODO: This is highly experimental code.
	//      Maybe the dynamic rate selection does not work
	//      and it has to be removed again.

printk("badness %d\n", mac->txrate_badness);
	mac->txrate_badness += amount;
	if (mac->txrate_badness <= -1000) {
		/* Very small badness. Try a faster bitrate. */
		if (mac->txrates_change)
			memcpy(&oldrates, &mac->txrates, sizeof(oldrates));
		default_rate = raise_rate(mac, default_rate);
		changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
		default_fallback = get_fallback_rate(mac, default_rate);
		changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
		mac->txrate_badness = 0;
printk("Bitrate raised to %u\n", default_rate);
	} else if (mac->txrate_badness >= 10000) {
		/* Very high badness. Try a slower bitrate. */
		if (mac->txrates_change)
			memcpy(&oldrates, &mac->txrates, sizeof(oldrates));
		default_rate = lower_rate(mac, default_rate);
		changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
		default_fallback = get_fallback_rate(mac, default_rate);
		changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
		mac->txrate_badness = 0;
printk("Bitrate lowered to %u\n", default_rate);
	}

	mac->txrates.default_rate = default_rate;
	mac->txrates.default_fallback = default_fallback;

	if (changes && mac->txrates_change)
		mac->txrates_change(mac->dev, changes, &oldrates);
}

void ieee80211softmac_fragment_lost(struct net_device *dev,
				    u16 wl_seq)
{
	struct ieee80211softmac_device *mac = ieee80211_priv(dev);
	unsigned long flags;

	spin_lock_irqsave(&mac->lock, flags);
	ieee80211softmac_add_txrates_badness(mac, 1000);
	//TODO

	spin_unlock_irqrestore(&mac->lock, flags);
}
EXPORT_SYMBOL_GPL(ieee80211softmac_fragment_lost);

static int rate_cmp(const void *a_, const void *b_) {
	u8 *a, *b;
	a = (u8*)a_;
	b = (u8*)b_;
	return ((*a & ~IEEE80211_BASIC_RATE_MASK) - (*b & ~IEEE80211_BASIC_RATE_MASK));
}

/* Allocate a softmac network struct and fill it from a network */
struct ieee80211softmac_network *
ieee80211softmac_create_network(struct ieee80211softmac_device *mac,
	struct ieee80211_network *net)
{
	struct ieee80211softmac_network *softnet;
	softnet = kzalloc(sizeof(struct ieee80211softmac_network), GFP_ATOMIC);
	if(softnet == NULL)
		return NULL;
	memcpy(softnet->bssid, net->bssid, ETH_ALEN);
	softnet->channel = net->channel;
	softnet->essid.len = net->ssid_len;
	memcpy(softnet->essid.data, net->ssid, softnet->essid.len);
	
	/* copy rates over */
	softnet->supported_rates.count = net->rates_len;
	memcpy(&softnet->supported_rates.rates[0], net->rates, net->rates_len);
	memcpy(&softnet->supported_rates.rates[softnet->supported_rates.count], net->rates_ex, net->rates_ex_len);
	softnet->supported_rates.count += net->rates_ex_len;
	sort(softnet->supported_rates.rates, softnet->supported_rates.count, sizeof(softnet->supported_rates.rates[0]), rate_cmp, NULL);
	
	softnet->capabilities = net->capability;
	return softnet;
}


/* Add a network to the list, while locked */
void
ieee80211softmac_add_network_locked(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_network *add_net)
{
	struct list_head *list_ptr;
	struct ieee80211softmac_network *softmac_net = NULL;

	list_for_each(list_ptr, &mac->network_list) {
		softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
		if(!memcmp(softmac_net->bssid, add_net->bssid, ETH_ALEN))
			break;
		else
			softmac_net = NULL;
	}
	if(softmac_net == NULL)
		list_add(&(add_net->list), &mac->network_list);
}

/* Add a network to the list, with locking */
void
ieee80211softmac_add_network(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_network *add_net)
{
	unsigned long flags;
	spin_lock_irqsave(&mac->lock, flags);
	ieee80211softmac_add_network_locked(mac, add_net);
	spin_unlock_irqrestore(&mac->lock, flags);
}


/* Delete a network from the list, while locked*/
void
ieee80211softmac_del_network_locked(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_network *del_net)
{
	list_del(&(del_net->list));
}

/* Delete a network from the list with locking */
void
ieee80211softmac_del_network(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_network *del_net)
{
	unsigned long flags;
	spin_lock_irqsave(&mac->lock, flags);
	ieee80211softmac_del_network_locked(mac, del_net);
	spin_unlock_irqrestore(&mac->lock, flags);
}

/* Get a network from the list by MAC while locked */
struct ieee80211softmac_network *
ieee80211softmac_get_network_by_bssid_locked(struct ieee80211softmac_device *mac,
	u8 *bssid)
{
	struct list_head *list_ptr;
	struct ieee80211softmac_network *softmac_net = NULL;
	list_for_each(list_ptr, &mac->network_list) {
		softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
		if(!memcmp(softmac_net->bssid, bssid, ETH_ALEN))
			break;
		else
			softmac_net = NULL;
	}
	return softmac_net;
}

/* Get a network from the list by BSSID with locking */
struct ieee80211softmac_network *
ieee80211softmac_get_network_by_bssid(struct ieee80211softmac_device *mac,
	u8 *bssid)
{
	unsigned long flags;
	struct ieee80211softmac_network *softmac_net;
	
	spin_lock_irqsave(&mac->lock, flags);
	softmac_net = ieee80211softmac_get_network_by_bssid_locked(mac, bssid);
	spin_unlock_irqrestore(&mac->lock, flags);
	return softmac_net;
}

/* Get a network from the list by ESSID while locked */
struct ieee80211softmac_network *
ieee80211softmac_get_network_by_essid_locked(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_essid *essid)
{
	struct list_head *list_ptr;
	struct ieee80211softmac_network *softmac_net = NULL;

	list_for_each(list_ptr, &mac->network_list) {
		softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
		if (softmac_net->essid.len == essid->len &&
			!memcmp(softmac_net->essid.data, essid->data, essid->len))
			return softmac_net;
	}
	return NULL;
}

/* Get a network from the list by ESSID with locking */
struct ieee80211softmac_network *
ieee80211softmac_get_network_by_essid(struct ieee80211softmac_device *mac,
	struct ieee80211softmac_essid *essid)	
{
	unsigned long flags;
	struct ieee80211softmac_network *softmac_net = NULL;

	spin_lock_irqsave(&mac->lock, flags);
	softmac_net = ieee80211softmac_get_network_by_essid_locked(mac, essid);	
	spin_unlock_irqrestore(&mac->lock, flags);
	return softmac_net;
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Johannes Berg");
MODULE_AUTHOR("Joseph Jezak");
MODULE_AUTHOR("Larry Finger");
MODULE_AUTHOR("Danny van Dyk");
MODULE_AUTHOR("Michael Buesch");
MODULE_DESCRIPTION("802.11 software MAC");