]> Pileus Git - ~andy/linux/blob - net/mac80211/util.c
[NET]: Make the device list and device lookups per namespace.
[~andy/linux] / net / mac80211 / util.c
1 /*
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>
23 #include <net/net_namespace.h>
24 #include <net/cfg80211.h>
25
26 #include "ieee80211_i.h"
27 #include "ieee80211_rate.h"
28 #include "wme.h"
29
30 /* privid for wiphys to determine whether they belong to us or not */
31 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
32
33 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
34 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
35 const unsigned char rfc1042_header[] =
36         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
37
38 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
39 const unsigned char bridge_tunnel_header[] =
40         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
41
42 /* No encapsulation header if EtherType < 0x600 (=length) */
43 static const unsigned char eapol_header[] =
44         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
45
46
47 static int rate_list_match(const int *rate_list, int rate)
48 {
49         int i;
50
51         if (!rate_list)
52                 return 0;
53
54         for (i = 0; rate_list[i] >= 0; i++)
55                 if (rate_list[i] == rate)
56                         return 1;
57
58         return 0;
59 }
60
61 void ieee80211_prepare_rates(struct ieee80211_local *local,
62                              struct ieee80211_hw_mode *mode)
63 {
64         int i;
65
66         for (i = 0; i < mode->num_rates; i++) {
67                 struct ieee80211_rate *rate = &mode->rates[i];
68
69                 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
70                                  IEEE80211_RATE_BASIC);
71
72                 if (local->supp_rates[mode->mode]) {
73                         if (!rate_list_match(local->supp_rates[mode->mode],
74                                              rate->rate))
75                                 continue;
76                 }
77
78                 rate->flags |= IEEE80211_RATE_SUPPORTED;
79
80                 /* Use configured basic rate set if it is available. If not,
81                  * use defaults that are sane for most cases. */
82                 if (local->basic_rates[mode->mode]) {
83                         if (rate_list_match(local->basic_rates[mode->mode],
84                                             rate->rate))
85                                 rate->flags |= IEEE80211_RATE_BASIC;
86                 } else switch (mode->mode) {
87                 case MODE_IEEE80211A:
88                         if (rate->rate == 60 || rate->rate == 120 ||
89                             rate->rate == 240)
90                                 rate->flags |= IEEE80211_RATE_BASIC;
91                         break;
92                 case MODE_IEEE80211B:
93                         if (rate->rate == 10 || rate->rate == 20)
94                                 rate->flags |= IEEE80211_RATE_BASIC;
95                         break;
96                 case MODE_ATHEROS_TURBO:
97                         if (rate->rate == 120 || rate->rate == 240 ||
98                             rate->rate == 480)
99                                 rate->flags |= IEEE80211_RATE_BASIC;
100                         break;
101                 case MODE_IEEE80211G:
102                         if (rate->rate == 10 || rate->rate == 20 ||
103                             rate->rate == 55 || rate->rate == 110)
104                                 rate->flags |= IEEE80211_RATE_BASIC;
105                         break;
106                 }
107
108                 /* Set ERP and MANDATORY flags based on phymode */
109                 switch (mode->mode) {
110                 case MODE_IEEE80211A:
111                         if (rate->rate == 60 || rate->rate == 120 ||
112                             rate->rate == 240)
113                                 rate->flags |= IEEE80211_RATE_MANDATORY;
114                         break;
115                 case MODE_IEEE80211B:
116                         if (rate->rate == 10)
117                                 rate->flags |= IEEE80211_RATE_MANDATORY;
118                         break;
119                 case MODE_ATHEROS_TURBO:
120                         break;
121                 case MODE_IEEE80211G:
122                         if (rate->rate == 10 || rate->rate == 20 ||
123                             rate->rate == 55 || rate->rate == 110 ||
124                             rate->rate == 60 || rate->rate == 120 ||
125                             rate->rate == 240)
126                                 rate->flags |= IEEE80211_RATE_MANDATORY;
127                         break;
128                 }
129                 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
130                         rate->flags |= IEEE80211_RATE_ERP;
131         }
132 }
133
134 u8 *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
168 int 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 }
205 EXPORT_SYMBOL(ieee80211_get_hdrlen);
206
207 int 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 }
219 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
220
221 int ieee80211_is_eapol(const struct sk_buff *skb)
222 {
223         const struct ieee80211_hdr *hdr;
224         u16 fc;
225         int hdrlen;
226
227         if (unlikely(skb->len < 10))
228                 return 0;
229
230         hdr = (const struct ieee80211_hdr *) skb->data;
231         fc = le16_to_cpu(hdr->frame_control);
232
233         if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
234                 return 0;
235
236         hdrlen = ieee80211_get_hdrlen(fc);
237
238         if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
239                      memcmp(skb->data + hdrlen, eapol_header,
240                             sizeof(eapol_header)) == 0))
241                 return 1;
242
243         return 0;
244 }
245
246 void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
247 {
248         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
249
250         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
251         if (tx->u.tx.extra_frag) {
252                 struct ieee80211_hdr *fhdr;
253                 int i;
254                 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
255                         fhdr = (struct ieee80211_hdr *)
256                                 tx->u.tx.extra_frag[i]->data;
257                         fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
258                 }
259         }
260 }
261
262 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
263                              int rate, int erp, int short_preamble)
264 {
265         int dur;
266
267         /* calculate duration (in microseconds, rounded up to next higher
268          * integer if it includes a fractional microsecond) to send frame of
269          * len bytes (does not include FCS) at the given rate. Duration will
270          * also include SIFS.
271          *
272          * rate is in 100 kbps, so divident is multiplied by 10 in the
273          * DIV_ROUND_UP() operations.
274          */
275
276         if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
277             local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
278                 /*
279                  * OFDM:
280                  *
281                  * N_DBPS = DATARATE x 4
282                  * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
283                  *      (16 = SIGNAL time, 6 = tail bits)
284                  * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
285                  *
286                  * T_SYM = 4 usec
287                  * 802.11a - 17.5.2: aSIFSTime = 16 usec
288                  * 802.11g - 19.8.4: aSIFSTime = 10 usec +
289                  *      signal ext = 6 usec
290                  */
291                 /* FIX: Atheros Turbo may have different (shorter) duration? */
292                 dur = 16; /* SIFS + signal ext */
293                 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
294                 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
295                 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
296                                         4 * rate); /* T_SYM x N_SYM */
297         } else {
298                 /*
299                  * 802.11b or 802.11g with 802.11b compatibility:
300                  * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
301                  * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
302                  *
303                  * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
304                  * aSIFSTime = 10 usec
305                  * aPreambleLength = 144 usec or 72 usec with short preamble
306                  * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
307                  */
308                 dur = 10; /* aSIFSTime = 10 usec */
309                 dur += short_preamble ? (72 + 24) : (144 + 48);
310
311                 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
312         }
313
314         return dur;
315 }
316
317 /* Exported duration function for driver use */
318 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw, int if_id,
319                                         size_t frame_len, int rate)
320 {
321         struct ieee80211_local *local = hw_to_local(hw);
322         struct net_device *bdev = dev_get_by_index(&init_net, if_id);
323         struct ieee80211_sub_if_data *sdata;
324         u16 dur;
325         int erp;
326
327         if (unlikely(!bdev))
328                 return 0;
329
330         sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
331         erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
332         dur = ieee80211_frame_duration(local, frame_len, rate,
333                        erp, sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE);
334
335         dev_put(bdev);
336         return cpu_to_le16(dur);
337 }
338 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
339
340 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw, int if_id,
341                               size_t frame_len,
342                               const struct ieee80211_tx_control *frame_txctl)
343 {
344         struct ieee80211_local *local = hw_to_local(hw);
345         struct ieee80211_rate *rate;
346         struct net_device *bdev = dev_get_by_index(&init_net, if_id);
347         struct ieee80211_sub_if_data *sdata;
348         int short_preamble;
349         int erp;
350         u16 dur;
351
352         if (unlikely(!bdev))
353                 return 0;
354
355         sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
356         short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
357
358         rate = frame_txctl->rts_rate;
359         erp = !!(rate->flags & IEEE80211_RATE_ERP);
360
361         /* CTS duration */
362         dur = ieee80211_frame_duration(local, 10, rate->rate,
363                                        erp, short_preamble);
364         /* Data frame duration */
365         dur += ieee80211_frame_duration(local, frame_len, rate->rate,
366                                         erp, short_preamble);
367         /* ACK duration */
368         dur += ieee80211_frame_duration(local, 10, rate->rate,
369                                         erp, short_preamble);
370
371         dev_put(bdev);
372         return cpu_to_le16(dur);
373 }
374 EXPORT_SYMBOL(ieee80211_rts_duration);
375
376 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw, int if_id,
377                                     size_t frame_len,
378                                     const struct ieee80211_tx_control *frame_txctl)
379 {
380         struct ieee80211_local *local = hw_to_local(hw);
381         struct ieee80211_rate *rate;
382         struct net_device *bdev = dev_get_by_index(&init_net, if_id);
383         struct ieee80211_sub_if_data *sdata;
384         int short_preamble;
385         int erp;
386         u16 dur;
387
388         if (unlikely(!bdev))
389                 return 0;
390
391         sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
392         short_preamble = sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE;
393
394         rate = frame_txctl->rts_rate;
395         erp = !!(rate->flags & IEEE80211_RATE_ERP);
396
397         /* Data frame duration */
398         dur = ieee80211_frame_duration(local, frame_len, rate->rate,
399                                        erp, short_preamble);
400         if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
401                 /* ACK duration */
402                 dur += ieee80211_frame_duration(local, 10, rate->rate,
403                                                 erp, short_preamble);
404         }
405
406         dev_put(bdev);
407         return cpu_to_le16(dur);
408 }
409 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
410
411 struct ieee80211_rate *
412 ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
413 {
414         struct ieee80211_hw_mode *mode;
415         int r;
416
417         list_for_each_entry(mode, &local->modes_list, list) {
418                 if (mode->mode != phymode)
419                         continue;
420                 for (r = 0; r < mode->num_rates; r++) {
421                         struct ieee80211_rate *rate = &mode->rates[r];
422                         if (rate->val == hw_rate ||
423                             (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
424                              rate->val2 == hw_rate))
425                                 return rate;
426                 }
427         }
428
429         return NULL;
430 }
431
432 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
433 {
434         struct ieee80211_local *local = hw_to_local(hw);
435
436         if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
437                                &local->state[queue])) {
438                 if (test_bit(IEEE80211_LINK_STATE_PENDING,
439                              &local->state[queue]))
440                         tasklet_schedule(&local->tx_pending_tasklet);
441                 else
442                         if (!ieee80211_qdisc_installed(local->mdev)) {
443                                 if (queue == 0)
444                                         netif_wake_queue(local->mdev);
445                         } else
446                                 __netif_schedule(local->mdev);
447         }
448 }
449 EXPORT_SYMBOL(ieee80211_wake_queue);
450
451 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
452 {
453         struct ieee80211_local *local = hw_to_local(hw);
454
455         if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
456                 netif_stop_queue(local->mdev);
457         set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
458 }
459 EXPORT_SYMBOL(ieee80211_stop_queue);
460
461 void ieee80211_start_queues(struct ieee80211_hw *hw)
462 {
463         struct ieee80211_local *local = hw_to_local(hw);
464         int i;
465
466         for (i = 0; i < local->hw.queues; i++)
467                 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
468         if (!ieee80211_qdisc_installed(local->mdev))
469                 netif_start_queue(local->mdev);
470 }
471 EXPORT_SYMBOL(ieee80211_start_queues);
472
473 void ieee80211_stop_queues(struct ieee80211_hw *hw)
474 {
475         int i;
476
477         for (i = 0; i < hw->queues; i++)
478                 ieee80211_stop_queue(hw, i);
479 }
480 EXPORT_SYMBOL(ieee80211_stop_queues);
481
482 void ieee80211_wake_queues(struct ieee80211_hw *hw)
483 {
484         int i;
485
486         for (i = 0; i < hw->queues; i++)
487                 ieee80211_wake_queue(hw, i);
488 }
489 EXPORT_SYMBOL(ieee80211_wake_queues);