blob: 7b278e9aa1a46a7e7728250825139ea2b15adde5 [file] [log] [blame]
Johannes Bergc2d15602007-07-27 15:43:23 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * utilities for mac80211
12 */
13
14#include <net/mac80211.h>
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/if_arp.h>
21#include <linux/wireless.h>
22#include <linux/bitmap.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070023#include <net/net_namespace.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020024#include <net/cfg80211.h>
Johannes Bergdabeb342007-11-09 01:57:29 +010025#include <net/rtnetlink.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020026
27#include "ieee80211_i.h"
28#include "ieee80211_rate.h"
29#include "wme.h"
30
31/* privid for wiphys to determine whether they belong to us or not */
32void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
33
34/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
35/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
36const unsigned char rfc1042_header[] =
37 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
38
39/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
40const unsigned char bridge_tunnel_header[] =
41 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
42
43/* No encapsulation header if EtherType < 0x600 (=length) */
44static const unsigned char eapol_header[] =
45 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
46
47
48static int rate_list_match(const int *rate_list, int rate)
49{
50 int i;
51
52 if (!rate_list)
53 return 0;
54
55 for (i = 0; rate_list[i] >= 0; i++)
56 if (rate_list[i] == rate)
57 return 1;
58
59 return 0;
60}
61
62void ieee80211_prepare_rates(struct ieee80211_local *local,
63 struct ieee80211_hw_mode *mode)
64{
65 int i;
66
67 for (i = 0; i < mode->num_rates; i++) {
68 struct ieee80211_rate *rate = &mode->rates[i];
69
70 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
71 IEEE80211_RATE_BASIC);
72
73 if (local->supp_rates[mode->mode]) {
74 if (!rate_list_match(local->supp_rates[mode->mode],
75 rate->rate))
76 continue;
77 }
78
79 rate->flags |= IEEE80211_RATE_SUPPORTED;
80
81 /* Use configured basic rate set if it is available. If not,
82 * use defaults that are sane for most cases. */
83 if (local->basic_rates[mode->mode]) {
84 if (rate_list_match(local->basic_rates[mode->mode],
85 rate->rate))
86 rate->flags |= IEEE80211_RATE_BASIC;
87 } else switch (mode->mode) {
88 case MODE_IEEE80211A:
89 if (rate->rate == 60 || rate->rate == 120 ||
90 rate->rate == 240)
91 rate->flags |= IEEE80211_RATE_BASIC;
92 break;
93 case MODE_IEEE80211B:
94 if (rate->rate == 10 || rate->rate == 20)
95 rate->flags |= IEEE80211_RATE_BASIC;
96 break;
Johannes Bergc2d15602007-07-27 15:43:23 +020097 case MODE_IEEE80211G:
98 if (rate->rate == 10 || rate->rate == 20 ||
99 rate->rate == 55 || rate->rate == 110)
100 rate->flags |= IEEE80211_RATE_BASIC;
101 break;
Johannes Berg693d4542007-09-18 17:29:20 -0400102 case NUM_IEEE80211_MODES:
103 /* not useful */
104 break;
Johannes Bergc2d15602007-07-27 15:43:23 +0200105 }
106
107 /* Set ERP and MANDATORY flags based on phymode */
108 switch (mode->mode) {
109 case MODE_IEEE80211A:
110 if (rate->rate == 60 || rate->rate == 120 ||
111 rate->rate == 240)
112 rate->flags |= IEEE80211_RATE_MANDATORY;
113 break;
114 case MODE_IEEE80211B:
115 if (rate->rate == 10)
116 rate->flags |= IEEE80211_RATE_MANDATORY;
117 break;
Johannes Bergc2d15602007-07-27 15:43:23 +0200118 case MODE_IEEE80211G:
119 if (rate->rate == 10 || rate->rate == 20 ||
120 rate->rate == 55 || rate->rate == 110 ||
121 rate->rate == 60 || rate->rate == 120 ||
122 rate->rate == 240)
123 rate->flags |= IEEE80211_RATE_MANDATORY;
124 break;
Johannes Berg693d4542007-09-18 17:29:20 -0400125 case NUM_IEEE80211_MODES:
126 /* not useful */
127 break;
Johannes Bergc2d15602007-07-27 15:43:23 +0200128 }
129 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
130 rate->flags |= IEEE80211_RATE_ERP;
131 }
132}
133
134u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
135{
136 u16 fc;
137
138 if (len < 24)
139 return NULL;
140
141 fc = le16_to_cpu(hdr->frame_control);
142
143 switch (fc & IEEE80211_FCTL_FTYPE) {
144 case IEEE80211_FTYPE_DATA:
145 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
146 case IEEE80211_FCTL_TODS:
147 return hdr->addr1;
148 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
149 return NULL;
150 case IEEE80211_FCTL_FROMDS:
151 return hdr->addr2;
152 case 0:
153 return hdr->addr3;
154 }
155 break;
156 case IEEE80211_FTYPE_MGMT:
157 return hdr->addr3;
158 case IEEE80211_FTYPE_CTL:
159 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
160 return hdr->addr1;
161 else
162 return NULL;
163 }
164
165 return NULL;
166}
167
168int ieee80211_get_hdrlen(u16 fc)
169{
170 int hdrlen = 24;
171
172 switch (fc & IEEE80211_FCTL_FTYPE) {
173 case IEEE80211_FTYPE_DATA:
174 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
175 hdrlen = 30; /* Addr4 */
176 /*
177 * The QoS Control field is two bytes and its presence is
178 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
179 * hdrlen if that bit is set.
180 * This works by masking out the bit and shifting it to
181 * bit position 1 so the result has the value 0 or 2.
182 */
183 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
184 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
185 break;
186 case IEEE80211_FTYPE_CTL:
187 /*
188 * ACK and CTS are 10 bytes, all others 16. To see how
189 * to get this condition consider
190 * subtype mask: 0b0000000011110000 (0x00F0)
191 * ACK subtype: 0b0000000011010000 (0x00D0)
192 * CTS subtype: 0b0000000011000000 (0x00C0)
193 * bits that matter: ^^^ (0x00E0)
194 * value of those: 0b0000000011000000 (0x00C0)
195 */
196 if ((fc & 0xE0) == 0xC0)
197 hdrlen = 10;
198 else
199 hdrlen = 16;
200 break;
201 }
202
203 return hdrlen;
204}
205EXPORT_SYMBOL(ieee80211_get_hdrlen);
206
207int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
208{
209 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
210 int hdrlen;
211
212 if (unlikely(skb->len < 10))
213 return 0;
214 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
215 if (unlikely(hdrlen > skb->len))
216 return 0;
217 return hdrlen;
218}
219EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
220
Ron Rindjunsky76ee65b2007-11-22 19:49:12 +0200221int ieee80211_is_eapol(const struct sk_buff *skb, int hdrlen)
Johannes Bergc2d15602007-07-27 15:43:23 +0200222{
Johannes Bergc2d15602007-07-27 15:43:23 +0200223 if (unlikely(skb->len < 10))
224 return 0;
225
Johannes Bergc2d15602007-07-27 15:43:23 +0200226 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
227 memcmp(skb->data + hdrlen, eapol_header,
228 sizeof(eapol_header)) == 0))
229 return 1;
230
231 return 0;
232}
233
234void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
235{
236 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
237
238 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
239 if (tx->u.tx.extra_frag) {
240 struct ieee80211_hdr *fhdr;
241 int i;
242 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
243 fhdr = (struct ieee80211_hdr *)
244 tx->u.tx.extra_frag[i]->data;
245 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
246 }
247 }
248}
249
250int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
251 int rate, int erp, int short_preamble)
252{
253 int dur;
254
255 /* calculate duration (in microseconds, rounded up to next higher
256 * integer if it includes a fractional microsecond) to send frame of
257 * len bytes (does not include FCS) at the given rate. Duration will
258 * also include SIFS.
259 *
260 * rate is in 100 kbps, so divident is multiplied by 10 in the
261 * DIV_ROUND_UP() operations.
262 */
263
Johannes Bergb708e612007-09-14 11:10:25 -0400264 if (local->hw.conf.phymode == MODE_IEEE80211A || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200265 /*
266 * OFDM:
267 *
268 * N_DBPS = DATARATE x 4
269 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
270 * (16 = SIGNAL time, 6 = tail bits)
271 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
272 *
273 * T_SYM = 4 usec
274 * 802.11a - 17.5.2: aSIFSTime = 16 usec
275 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
276 * signal ext = 6 usec
277 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200278 dur = 16; /* SIFS + signal ext */
279 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
280 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
281 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
282 4 * rate); /* T_SYM x N_SYM */
283 } else {
284 /*
285 * 802.11b or 802.11g with 802.11b compatibility:
286 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
287 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
288 *
289 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
290 * aSIFSTime = 10 usec
291 * aPreambleLength = 144 usec or 72 usec with short preamble
292 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
293 */
294 dur = 10; /* aSIFSTime = 10 usec */
295 dur += short_preamble ? (72 + 24) : (144 + 48);
296
297 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
298 }
299
300 return dur;
301}
302
303/* Exported duration function for driver use */
Daniel Drake7e9ed182007-07-27 15:43:24 +0200304__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw, int if_id,
Johannes Bergc2d15602007-07-27 15:43:23 +0200305 size_t frame_len, int rate)
306{
307 struct ieee80211_local *local = hw_to_local(hw);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700308 struct net_device *bdev = dev_get_by_index(&init_net, if_id);
Daniel Drake7e9ed182007-07-27 15:43:24 +0200309 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200310 u16 dur;
311 int erp;
312
Daniel Drake7e9ed182007-07-27 15:43:24 +0200313 if (unlikely(!bdev))
314 return 0;
315
316 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
Johannes Bergc2d15602007-07-27 15:43:23 +0200317 erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
318 dur = ieee80211_frame_duration(local, frame_len, rate,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400319 erp, sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE);
Johannes Bergc2d15602007-07-27 15:43:23 +0200320
Daniel Drake7e9ed182007-07-27 15:43:24 +0200321 dev_put(bdev);
Johannes Bergc2d15602007-07-27 15:43:23 +0200322 return cpu_to_le16(dur);
323}
324EXPORT_SYMBOL(ieee80211_generic_frame_duration);
325
Daniel Drake7e9ed182007-07-27 15:43:24 +0200326__le16 ieee80211_rts_duration(struct ieee80211_hw *hw, int if_id,
Johannes Bergc2d15602007-07-27 15:43:23 +0200327 size_t frame_len,
328 const struct ieee80211_tx_control *frame_txctl)
329{
330 struct ieee80211_local *local = hw_to_local(hw);
331 struct ieee80211_rate *rate;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700332 struct net_device *bdev = dev_get_by_index(&init_net, if_id);
Daniel Drake7e9ed182007-07-27 15:43:24 +0200333 struct ieee80211_sub_if_data *sdata;
334 int short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200335 int erp;
336 u16 dur;
337
Daniel Drake7e9ed182007-07-27 15:43:24 +0200338 if (unlikely(!bdev))
339 return 0;
340
341 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400342 short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200343
Johannes Bergc2d15602007-07-27 15:43:23 +0200344 rate = frame_txctl->rts_rate;
345 erp = !!(rate->flags & IEEE80211_RATE_ERP);
346
347 /* CTS duration */
348 dur = ieee80211_frame_duration(local, 10, rate->rate,
349 erp, short_preamble);
350 /* Data frame duration */
351 dur += ieee80211_frame_duration(local, frame_len, rate->rate,
352 erp, short_preamble);
353 /* ACK duration */
354 dur += ieee80211_frame_duration(local, 10, rate->rate,
355 erp, short_preamble);
356
Daniel Drake7e9ed182007-07-27 15:43:24 +0200357 dev_put(bdev);
Johannes Bergc2d15602007-07-27 15:43:23 +0200358 return cpu_to_le16(dur);
359}
360EXPORT_SYMBOL(ieee80211_rts_duration);
361
Daniel Drake7e9ed182007-07-27 15:43:24 +0200362__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw, int if_id,
Johannes Bergc2d15602007-07-27 15:43:23 +0200363 size_t frame_len,
364 const struct ieee80211_tx_control *frame_txctl)
365{
366 struct ieee80211_local *local = hw_to_local(hw);
367 struct ieee80211_rate *rate;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700368 struct net_device *bdev = dev_get_by_index(&init_net, if_id);
Daniel Drake7e9ed182007-07-27 15:43:24 +0200369 struct ieee80211_sub_if_data *sdata;
370 int short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200371 int erp;
372 u16 dur;
373
Daniel Drake7e9ed182007-07-27 15:43:24 +0200374 if (unlikely(!bdev))
375 return 0;
376
377 sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400378 short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200379
Johannes Bergc2d15602007-07-27 15:43:23 +0200380 rate = frame_txctl->rts_rate;
381 erp = !!(rate->flags & IEEE80211_RATE_ERP);
382
383 /* Data frame duration */
384 dur = ieee80211_frame_duration(local, frame_len, rate->rate,
385 erp, short_preamble);
386 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
387 /* ACK duration */
388 dur += ieee80211_frame_duration(local, 10, rate->rate,
389 erp, short_preamble);
390 }
391
Daniel Drake7e9ed182007-07-27 15:43:24 +0200392 dev_put(bdev);
Johannes Bergc2d15602007-07-27 15:43:23 +0200393 return cpu_to_le16(dur);
394}
395EXPORT_SYMBOL(ieee80211_ctstoself_duration);
396
397struct ieee80211_rate *
398ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
399{
400 struct ieee80211_hw_mode *mode;
401 int r;
402
403 list_for_each_entry(mode, &local->modes_list, list) {
404 if (mode->mode != phymode)
405 continue;
406 for (r = 0; r < mode->num_rates; r++) {
407 struct ieee80211_rate *rate = &mode->rates[r];
408 if (rate->val == hw_rate ||
409 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
410 rate->val2 == hw_rate))
411 return rate;
412 }
413 }
414
415 return NULL;
416}
417
418void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
419{
420 struct ieee80211_local *local = hw_to_local(hw);
421
422 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
423 &local->state[queue])) {
424 if (test_bit(IEEE80211_LINK_STATE_PENDING,
425 &local->state[queue]))
426 tasklet_schedule(&local->tx_pending_tasklet);
427 else
428 if (!ieee80211_qdisc_installed(local->mdev)) {
429 if (queue == 0)
430 netif_wake_queue(local->mdev);
431 } else
432 __netif_schedule(local->mdev);
433 }
434}
435EXPORT_SYMBOL(ieee80211_wake_queue);
436
437void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
438{
439 struct ieee80211_local *local = hw_to_local(hw);
440
441 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
442 netif_stop_queue(local->mdev);
443 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
444}
445EXPORT_SYMBOL(ieee80211_stop_queue);
446
447void ieee80211_start_queues(struct ieee80211_hw *hw)
448{
449 struct ieee80211_local *local = hw_to_local(hw);
450 int i;
451
452 for (i = 0; i < local->hw.queues; i++)
453 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
454 if (!ieee80211_qdisc_installed(local->mdev))
455 netif_start_queue(local->mdev);
456}
457EXPORT_SYMBOL(ieee80211_start_queues);
458
459void ieee80211_stop_queues(struct ieee80211_hw *hw)
460{
461 int i;
462
463 for (i = 0; i < hw->queues; i++)
464 ieee80211_stop_queue(hw, i);
465}
466EXPORT_SYMBOL(ieee80211_stop_queues);
467
468void ieee80211_wake_queues(struct ieee80211_hw *hw)
469{
470 int i;
471
472 for (i = 0; i < hw->queues; i++)
473 ieee80211_wake_queue(hw, i);
474}
475EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100476
477void ieee80211_iterate_active_interfaces(struct ieee80211_hw *hw,
478 void (*iterator)(void *data, u8 *mac,
479 int if_id),
480 void *data)
481{
482 struct ieee80211_local *local = hw_to_local(hw);
483 struct ieee80211_sub_if_data *sdata;
484
Johannes Berge38bad42007-11-28 10:55:32 +0100485 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100486
Johannes Berge38bad42007-11-28 10:55:32 +0100487 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Bergdabeb342007-11-09 01:57:29 +0100488 switch (sdata->type) {
489 case IEEE80211_IF_TYPE_INVALID:
490 case IEEE80211_IF_TYPE_MNTR:
491 case IEEE80211_IF_TYPE_VLAN:
492 continue;
493 case IEEE80211_IF_TYPE_AP:
494 case IEEE80211_IF_TYPE_STA:
495 case IEEE80211_IF_TYPE_IBSS:
496 case IEEE80211_IF_TYPE_WDS:
497 break;
498 }
499 if (sdata->dev == local->mdev)
500 continue;
501 if (netif_running(sdata->dev))
502 iterator(data, sdata->dev->dev_addr,
503 sdata->dev->ifindex);
504 }
Johannes Berge38bad42007-11-28 10:55:32 +0100505
506 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100507}
508EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);