mac80211: rewrite fragmentation
[safe/jmp/linux-2.6] / 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 #include <net/rtnetlink.h>
26
27 #include "ieee80211_i.h"
28 #include "rate.h"
29 #include "mesh.h"
30 #include "wme.h"
31
32 /* privid for wiphys to determine whether they belong to us or not */
33 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
34
35 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
36 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
37 const unsigned char rfc1042_header[] __aligned(2) =
38         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
39
40 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
41 const unsigned char bridge_tunnel_header[] __aligned(2) =
42         { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
43
44 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
45 {
46         struct ieee80211_local *local;
47         BUG_ON(!wiphy);
48
49         local = wiphy_priv(wiphy);
50         return &local->hw;
51 }
52 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
53
54 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
55                         enum nl80211_iftype type)
56 {
57         __le16 fc = hdr->frame_control;
58
59          /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
60         if (len < 16)
61                 return NULL;
62
63         if (ieee80211_is_data(fc)) {
64                 if (len < 24) /* drop incorrect hdr len (data) */
65                         return NULL;
66
67                 if (ieee80211_has_a4(fc))
68                         return NULL;
69                 if (ieee80211_has_tods(fc))
70                         return hdr->addr1;
71                 if (ieee80211_has_fromds(fc))
72                         return hdr->addr2;
73
74                 return hdr->addr3;
75         }
76
77         if (ieee80211_is_mgmt(fc)) {
78                 if (len < 24) /* drop incorrect hdr len (mgmt) */
79                         return NULL;
80                 return hdr->addr3;
81         }
82
83         if (ieee80211_is_ctl(fc)) {
84                 if(ieee80211_is_pspoll(fc))
85                         return hdr->addr1;
86
87                 if (ieee80211_is_back_req(fc)) {
88                         switch (type) {
89                         case NL80211_IFTYPE_STATION:
90                                 return hdr->addr2;
91                         case NL80211_IFTYPE_AP:
92                         case NL80211_IFTYPE_AP_VLAN:
93                                 return hdr->addr1;
94                         default:
95                                 break; /* fall through to the return */
96                         }
97                 }
98         }
99
100         return NULL;
101 }
102
103 unsigned int ieee80211_hdrlen(__le16 fc)
104 {
105         unsigned int hdrlen = 24;
106
107         if (ieee80211_is_data(fc)) {
108                 if (ieee80211_has_a4(fc))
109                         hdrlen = 30;
110                 if (ieee80211_is_data_qos(fc))
111                         hdrlen += IEEE80211_QOS_CTL_LEN;
112                 goto out;
113         }
114
115         if (ieee80211_is_ctl(fc)) {
116                 /*
117                  * ACK and CTS are 10 bytes, all others 16. To see how
118                  * to get this condition consider
119                  *   subtype mask:   0b0000000011110000 (0x00F0)
120                  *   ACK subtype:    0b0000000011010000 (0x00D0)
121                  *   CTS subtype:    0b0000000011000000 (0x00C0)
122                  *   bits that matter:         ^^^      (0x00E0)
123                  *   value of those: 0b0000000011000000 (0x00C0)
124                  */
125                 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
126                         hdrlen = 10;
127                 else
128                         hdrlen = 16;
129         }
130 out:
131         return hdrlen;
132 }
133 EXPORT_SYMBOL(ieee80211_hdrlen);
134
135 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
136 {
137         const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *)skb->data;
138         unsigned int hdrlen;
139
140         if (unlikely(skb->len < 10))
141                 return 0;
142         hdrlen = ieee80211_hdrlen(hdr->frame_control);
143         if (unlikely(hdrlen > skb->len))
144                 return 0;
145         return hdrlen;
146 }
147 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
148
149 int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
150 {
151         int ae = meshhdr->flags & IEEE80211S_FLAGS_AE;
152         /* 7.1.3.5a.2 */
153         switch (ae) {
154         case 0:
155                 return 6;
156         case 1:
157                 return 12;
158         case 2:
159                 return 18;
160         case 3:
161                 return 24;
162         default:
163                 return 6;
164         }
165 }
166
167 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
168 {
169         struct sk_buff *skb = tx->skb;
170         struct ieee80211_hdr *hdr;
171
172         do {
173                 hdr = (struct ieee80211_hdr *) skb->data;
174                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
175         } while ((skb = skb->next));
176 }
177
178 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
179                              int rate, int erp, int short_preamble)
180 {
181         int dur;
182
183         /* calculate duration (in microseconds, rounded up to next higher
184          * integer if it includes a fractional microsecond) to send frame of
185          * len bytes (does not include FCS) at the given rate. Duration will
186          * also include SIFS.
187          *
188          * rate is in 100 kbps, so divident is multiplied by 10 in the
189          * DIV_ROUND_UP() operations.
190          */
191
192         if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
193                 /*
194                  * OFDM:
195                  *
196                  * N_DBPS = DATARATE x 4
197                  * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
198                  *      (16 = SIGNAL time, 6 = tail bits)
199                  * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
200                  *
201                  * T_SYM = 4 usec
202                  * 802.11a - 17.5.2: aSIFSTime = 16 usec
203                  * 802.11g - 19.8.4: aSIFSTime = 10 usec +
204                  *      signal ext = 6 usec
205                  */
206                 dur = 16; /* SIFS + signal ext */
207                 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
208                 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
209                 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
210                                         4 * rate); /* T_SYM x N_SYM */
211         } else {
212                 /*
213                  * 802.11b or 802.11g with 802.11b compatibility:
214                  * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
215                  * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
216                  *
217                  * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
218                  * aSIFSTime = 10 usec
219                  * aPreambleLength = 144 usec or 72 usec with short preamble
220                  * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
221                  */
222                 dur = 10; /* aSIFSTime = 10 usec */
223                 dur += short_preamble ? (72 + 24) : (144 + 48);
224
225                 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
226         }
227
228         return dur;
229 }
230
231 /* Exported duration function for driver use */
232 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
233                                         struct ieee80211_vif *vif,
234                                         size_t frame_len,
235                                         struct ieee80211_rate *rate)
236 {
237         struct ieee80211_local *local = hw_to_local(hw);
238         struct ieee80211_sub_if_data *sdata;
239         u16 dur;
240         int erp;
241         bool short_preamble = false;
242
243         erp = 0;
244         if (vif) {
245                 sdata = vif_to_sdata(vif);
246                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
247                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
248                         erp = rate->flags & IEEE80211_RATE_ERP_G;
249         }
250
251         dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
252                                        short_preamble);
253
254         return cpu_to_le16(dur);
255 }
256 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
257
258 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
259                               struct ieee80211_vif *vif, size_t frame_len,
260                               const struct ieee80211_tx_info *frame_txctl)
261 {
262         struct ieee80211_local *local = hw_to_local(hw);
263         struct ieee80211_rate *rate;
264         struct ieee80211_sub_if_data *sdata;
265         bool short_preamble;
266         int erp;
267         u16 dur;
268         struct ieee80211_supported_band *sband;
269
270         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
271
272         short_preamble = false;
273
274         rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
275
276         erp = 0;
277         if (vif) {
278                 sdata = vif_to_sdata(vif);
279                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
280                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
281                         erp = rate->flags & IEEE80211_RATE_ERP_G;
282         }
283
284         /* CTS duration */
285         dur = ieee80211_frame_duration(local, 10, rate->bitrate,
286                                        erp, short_preamble);
287         /* Data frame duration */
288         dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
289                                         erp, short_preamble);
290         /* ACK duration */
291         dur += ieee80211_frame_duration(local, 10, rate->bitrate,
292                                         erp, short_preamble);
293
294         return cpu_to_le16(dur);
295 }
296 EXPORT_SYMBOL(ieee80211_rts_duration);
297
298 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
299                                     struct ieee80211_vif *vif,
300                                     size_t frame_len,
301                                     const struct ieee80211_tx_info *frame_txctl)
302 {
303         struct ieee80211_local *local = hw_to_local(hw);
304         struct ieee80211_rate *rate;
305         struct ieee80211_sub_if_data *sdata;
306         bool short_preamble;
307         int erp;
308         u16 dur;
309         struct ieee80211_supported_band *sband;
310
311         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
312
313         short_preamble = false;
314
315         rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
316         erp = 0;
317         if (vif) {
318                 sdata = vif_to_sdata(vif);
319                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
320                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
321                         erp = rate->flags & IEEE80211_RATE_ERP_G;
322         }
323
324         /* Data frame duration */
325         dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
326                                        erp, short_preamble);
327         if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
328                 /* ACK duration */
329                 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
330                                                 erp, short_preamble);
331         }
332
333         return cpu_to_le16(dur);
334 }
335 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
336
337 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
338                                    enum queue_stop_reason reason)
339 {
340         struct ieee80211_local *local = hw_to_local(hw);
341
342         if (queue >= hw->queues) {
343                 if (local->ampdu_ac_queue[queue - hw->queues] < 0)
344                         return;
345
346                 /*
347                  * for virtual aggregation queues, we need to refcount the
348                  * internal mac80211 disable (multiple times!), keep track of
349                  * driver disable _and_ make sure the regular queue is
350                  * actually enabled.
351                  */
352                 if (reason == IEEE80211_QUEUE_STOP_REASON_AGGREGATION)
353                         local->amdpu_ac_stop_refcnt[queue - hw->queues]--;
354                 else
355                         __clear_bit(reason, &local->queue_stop_reasons[queue]);
356
357                 if (local->queue_stop_reasons[queue] ||
358                     local->amdpu_ac_stop_refcnt[queue - hw->queues])
359                         return;
360
361                 /* now go on to treat the corresponding regular queue */
362                 queue = local->ampdu_ac_queue[queue - hw->queues];
363                 reason = IEEE80211_QUEUE_STOP_REASON_AGGREGATION;
364         }
365
366         __clear_bit(reason, &local->queue_stop_reasons[queue]);
367
368         if (local->queue_stop_reasons[queue] != 0)
369                 /* someone still has this queue stopped */
370                 return;
371
372         if (test_bit(queue, local->queues_pending)) {
373                 set_bit(queue, local->queues_pending_run);
374                 tasklet_schedule(&local->tx_pending_tasklet);
375         } else {
376                 netif_wake_subqueue(local->mdev, queue);
377         }
378 }
379
380 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
381                                     enum queue_stop_reason reason)
382 {
383         struct ieee80211_local *local = hw_to_local(hw);
384         unsigned long flags;
385
386         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
387         __ieee80211_wake_queue(hw, queue, reason);
388         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
389 }
390
391 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
392 {
393         ieee80211_wake_queue_by_reason(hw, queue,
394                                        IEEE80211_QUEUE_STOP_REASON_DRIVER);
395 }
396 EXPORT_SYMBOL(ieee80211_wake_queue);
397
398 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
399                                    enum queue_stop_reason reason)
400 {
401         struct ieee80211_local *local = hw_to_local(hw);
402
403         if (queue >= hw->queues) {
404                 if (local->ampdu_ac_queue[queue - hw->queues] < 0)
405                         return;
406
407                 /*
408                  * for virtual aggregation queues, we need to refcount the
409                  * internal mac80211 disable (multiple times!), keep track of
410                  * driver disable _and_ make sure the regular queue is
411                  * actually enabled.
412                  */
413                 if (reason == IEEE80211_QUEUE_STOP_REASON_AGGREGATION)
414                         local->amdpu_ac_stop_refcnt[queue - hw->queues]++;
415                 else
416                         __set_bit(reason, &local->queue_stop_reasons[queue]);
417
418                 /* now go on to treat the corresponding regular queue */
419                 queue = local->ampdu_ac_queue[queue - hw->queues];
420                 reason = IEEE80211_QUEUE_STOP_REASON_AGGREGATION;
421         }
422
423         __set_bit(reason, &local->queue_stop_reasons[queue]);
424
425         netif_stop_subqueue(local->mdev, queue);
426 }
427
428 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
429                                     enum queue_stop_reason reason)
430 {
431         struct ieee80211_local *local = hw_to_local(hw);
432         unsigned long flags;
433
434         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
435         __ieee80211_stop_queue(hw, queue, reason);
436         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
437 }
438
439 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
440 {
441         ieee80211_stop_queue_by_reason(hw, queue,
442                                        IEEE80211_QUEUE_STOP_REASON_DRIVER);
443 }
444 EXPORT_SYMBOL(ieee80211_stop_queue);
445
446 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
447                                     enum queue_stop_reason reason)
448 {
449         struct ieee80211_local *local = hw_to_local(hw);
450         unsigned long flags;
451         int i;
452
453         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
454
455         for (i = 0; i < hw->queues; i++)
456                 __ieee80211_stop_queue(hw, i, reason);
457
458         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
459 }
460
461 void ieee80211_stop_queues(struct ieee80211_hw *hw)
462 {
463         ieee80211_stop_queues_by_reason(hw,
464                                         IEEE80211_QUEUE_STOP_REASON_DRIVER);
465 }
466 EXPORT_SYMBOL(ieee80211_stop_queues);
467
468 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
469 {
470         struct ieee80211_local *local = hw_to_local(hw);
471         unsigned long flags;
472
473         if (queue >= hw->queues) {
474                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
475                 queue = local->ampdu_ac_queue[queue - hw->queues];
476                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
477                 if (queue < 0)
478                         return true;
479         }
480
481         return __netif_subqueue_stopped(local->mdev, queue);
482 }
483 EXPORT_SYMBOL(ieee80211_queue_stopped);
484
485 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
486                                      enum queue_stop_reason reason)
487 {
488         struct ieee80211_local *local = hw_to_local(hw);
489         unsigned long flags;
490         int i;
491
492         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
493
494         for (i = 0; i < hw->queues + hw->ampdu_queues; i++)
495                 __ieee80211_wake_queue(hw, i, reason);
496
497         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
498 }
499
500 void ieee80211_wake_queues(struct ieee80211_hw *hw)
501 {
502         ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
503 }
504 EXPORT_SYMBOL(ieee80211_wake_queues);
505
506 void ieee80211_iterate_active_interfaces(
507         struct ieee80211_hw *hw,
508         void (*iterator)(void *data, u8 *mac,
509                          struct ieee80211_vif *vif),
510         void *data)
511 {
512         struct ieee80211_local *local = hw_to_local(hw);
513         struct ieee80211_sub_if_data *sdata;
514
515         mutex_lock(&local->iflist_mtx);
516
517         list_for_each_entry(sdata, &local->interfaces, list) {
518                 switch (sdata->vif.type) {
519                 case __NL80211_IFTYPE_AFTER_LAST:
520                 case NL80211_IFTYPE_UNSPECIFIED:
521                 case NL80211_IFTYPE_MONITOR:
522                 case NL80211_IFTYPE_AP_VLAN:
523                         continue;
524                 case NL80211_IFTYPE_AP:
525                 case NL80211_IFTYPE_STATION:
526                 case NL80211_IFTYPE_ADHOC:
527                 case NL80211_IFTYPE_WDS:
528                 case NL80211_IFTYPE_MESH_POINT:
529                         break;
530                 }
531                 if (netif_running(sdata->dev))
532                         iterator(data, sdata->dev->dev_addr,
533                                  &sdata->vif);
534         }
535
536         mutex_unlock(&local->iflist_mtx);
537 }
538 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
539
540 void ieee80211_iterate_active_interfaces_atomic(
541         struct ieee80211_hw *hw,
542         void (*iterator)(void *data, u8 *mac,
543                          struct ieee80211_vif *vif),
544         void *data)
545 {
546         struct ieee80211_local *local = hw_to_local(hw);
547         struct ieee80211_sub_if_data *sdata;
548
549         rcu_read_lock();
550
551         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
552                 switch (sdata->vif.type) {
553                 case __NL80211_IFTYPE_AFTER_LAST:
554                 case NL80211_IFTYPE_UNSPECIFIED:
555                 case NL80211_IFTYPE_MONITOR:
556                 case NL80211_IFTYPE_AP_VLAN:
557                         continue;
558                 case NL80211_IFTYPE_AP:
559                 case NL80211_IFTYPE_STATION:
560                 case NL80211_IFTYPE_ADHOC:
561                 case NL80211_IFTYPE_WDS:
562                 case NL80211_IFTYPE_MESH_POINT:
563                         break;
564                 }
565                 if (netif_running(sdata->dev))
566                         iterator(data, sdata->dev->dev_addr,
567                                  &sdata->vif);
568         }
569
570         rcu_read_unlock();
571 }
572 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
573
574 void ieee802_11_parse_elems(u8 *start, size_t len,
575                             struct ieee802_11_elems *elems)
576 {
577         size_t left = len;
578         u8 *pos = start;
579
580         memset(elems, 0, sizeof(*elems));
581         elems->ie_start = start;
582         elems->total_len = len;
583
584         while (left >= 2) {
585                 u8 id, elen;
586
587                 id = *pos++;
588                 elen = *pos++;
589                 left -= 2;
590
591                 if (elen > left)
592                         return;
593
594                 switch (id) {
595                 case WLAN_EID_SSID:
596                         elems->ssid = pos;
597                         elems->ssid_len = elen;
598                         break;
599                 case WLAN_EID_SUPP_RATES:
600                         elems->supp_rates = pos;
601                         elems->supp_rates_len = elen;
602                         break;
603                 case WLAN_EID_FH_PARAMS:
604                         elems->fh_params = pos;
605                         elems->fh_params_len = elen;
606                         break;
607                 case WLAN_EID_DS_PARAMS:
608                         elems->ds_params = pos;
609                         elems->ds_params_len = elen;
610                         break;
611                 case WLAN_EID_CF_PARAMS:
612                         elems->cf_params = pos;
613                         elems->cf_params_len = elen;
614                         break;
615                 case WLAN_EID_TIM:
616                         elems->tim = pos;
617                         elems->tim_len = elen;
618                         break;
619                 case WLAN_EID_IBSS_PARAMS:
620                         elems->ibss_params = pos;
621                         elems->ibss_params_len = elen;
622                         break;
623                 case WLAN_EID_CHALLENGE:
624                         elems->challenge = pos;
625                         elems->challenge_len = elen;
626                         break;
627                 case WLAN_EID_WPA:
628                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
629                             pos[2] == 0xf2) {
630                                 /* Microsoft OUI (00:50:F2) */
631                                 if (pos[3] == 1) {
632                                         /* OUI Type 1 - WPA IE */
633                                         elems->wpa = pos;
634                                         elems->wpa_len = elen;
635                                 } else if (elen >= 5 && pos[3] == 2) {
636                                         if (pos[4] == 0) {
637                                                 elems->wmm_info = pos;
638                                                 elems->wmm_info_len = elen;
639                                         } else if (pos[4] == 1) {
640                                                 elems->wmm_param = pos;
641                                                 elems->wmm_param_len = elen;
642                                         }
643                                 }
644                         }
645                         break;
646                 case WLAN_EID_RSN:
647                         elems->rsn = pos;
648                         elems->rsn_len = elen;
649                         break;
650                 case WLAN_EID_ERP_INFO:
651                         elems->erp_info = pos;
652                         elems->erp_info_len = elen;
653                         break;
654                 case WLAN_EID_EXT_SUPP_RATES:
655                         elems->ext_supp_rates = pos;
656                         elems->ext_supp_rates_len = elen;
657                         break;
658                 case WLAN_EID_HT_CAPABILITY:
659                         if (elen >= sizeof(struct ieee80211_ht_cap))
660                                 elems->ht_cap_elem = (void *)pos;
661                         break;
662                 case WLAN_EID_HT_INFORMATION:
663                         if (elen >= sizeof(struct ieee80211_ht_info))
664                                 elems->ht_info_elem = (void *)pos;
665                         break;
666                 case WLAN_EID_MESH_ID:
667                         elems->mesh_id = pos;
668                         elems->mesh_id_len = elen;
669                         break;
670                 case WLAN_EID_MESH_CONFIG:
671                         elems->mesh_config = pos;
672                         elems->mesh_config_len = elen;
673                         break;
674                 case WLAN_EID_PEER_LINK:
675                         elems->peer_link = pos;
676                         elems->peer_link_len = elen;
677                         break;
678                 case WLAN_EID_PREQ:
679                         elems->preq = pos;
680                         elems->preq_len = elen;
681                         break;
682                 case WLAN_EID_PREP:
683                         elems->prep = pos;
684                         elems->prep_len = elen;
685                         break;
686                 case WLAN_EID_PERR:
687                         elems->perr = pos;
688                         elems->perr_len = elen;
689                         break;
690                 case WLAN_EID_CHANNEL_SWITCH:
691                         elems->ch_switch_elem = pos;
692                         elems->ch_switch_elem_len = elen;
693                         break;
694                 case WLAN_EID_QUIET:
695                         if (!elems->quiet_elem) {
696                                 elems->quiet_elem = pos;
697                                 elems->quiet_elem_len = elen;
698                         }
699                         elems->num_of_quiet_elem++;
700                         break;
701                 case WLAN_EID_COUNTRY:
702                         elems->country_elem = pos;
703                         elems->country_elem_len = elen;
704                         break;
705                 case WLAN_EID_PWR_CONSTRAINT:
706                         elems->pwr_constr_elem = pos;
707                         elems->pwr_constr_elem_len = elen;
708                         break;
709                 case WLAN_EID_TIMEOUT_INTERVAL:
710                         elems->timeout_int = pos;
711                         elems->timeout_int_len = elen;
712                         break;
713                 default:
714                         break;
715                 }
716
717                 left -= elen;
718                 pos += elen;
719         }
720 }
721
722 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
723 {
724         struct ieee80211_local *local = sdata->local;
725         struct ieee80211_tx_queue_params qparam;
726         int i;
727
728         if (!local->ops->conf_tx)
729                 return;
730
731         memset(&qparam, 0, sizeof(qparam));
732
733         qparam.aifs = 2;
734
735         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
736             !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
737                 qparam.cw_min = 31;
738         else
739                 qparam.cw_min = 15;
740
741         qparam.cw_max = 1023;
742         qparam.txop = 0;
743
744         for (i = 0; i < local_to_hw(local)->queues; i++)
745                 local->ops->conf_tx(local_to_hw(local), i, &qparam);
746 }
747
748 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
749                                   const size_t supp_rates_len,
750                                   const u8 *supp_rates)
751 {
752         struct ieee80211_local *local = sdata->local;
753         int i, have_higher_than_11mbit = 0;
754
755         /* cf. IEEE 802.11 9.2.12 */
756         for (i = 0; i < supp_rates_len; i++)
757                 if ((supp_rates[i] & 0x7f) * 5 > 110)
758                         have_higher_than_11mbit = 1;
759
760         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
761             have_higher_than_11mbit)
762                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
763         else
764                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
765
766         ieee80211_set_wmm_default(sdata);
767 }
768
769 void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
770                       int encrypt)
771 {
772         skb->dev = sdata->local->mdev;
773         skb_set_mac_header(skb, 0);
774         skb_set_network_header(skb, 0);
775         skb_set_transport_header(skb, 0);
776
777         skb->iif = sdata->dev->ifindex;
778         skb->do_not_encrypt = !encrypt;
779
780         dev_queue_xmit(skb);
781 }
782
783 int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freqMHz)
784 {
785         int ret = -EINVAL;
786         struct ieee80211_channel *chan;
787         struct ieee80211_local *local = sdata->local;
788
789         chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
790
791         if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
792                 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
793                     chan->flags & IEEE80211_CHAN_NO_IBSS)
794                         return ret;
795                 local->oper_channel = chan;
796                 local->oper_channel_type = NL80211_CHAN_NO_HT;
797
798                 if (local->sw_scanning || local->hw_scanning)
799                         ret = 0;
800                 else
801                         ret = ieee80211_hw_config(
802                                 local, IEEE80211_CONF_CHANGE_CHANNEL);
803         }
804
805         return ret;
806 }
807
808 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
809                               enum ieee80211_band band)
810 {
811         struct ieee80211_supported_band *sband;
812         struct ieee80211_rate *bitrates;
813         u32 mandatory_rates;
814         enum ieee80211_rate_flags mandatory_flag;
815         int i;
816
817         sband = local->hw.wiphy->bands[band];
818         if (!sband) {
819                 WARN_ON(1);
820                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
821         }
822
823         if (band == IEEE80211_BAND_2GHZ)
824                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
825         else
826                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
827
828         bitrates = sband->bitrates;
829         mandatory_rates = 0;
830         for (i = 0; i < sband->n_bitrates; i++)
831                 if (bitrates[i].flags & mandatory_flag)
832                         mandatory_rates |= BIT(i);
833         return mandatory_rates;
834 }
835
836 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
837                          u16 transaction, u16 auth_alg,
838                          u8 *extra, size_t extra_len,
839                          const u8 *bssid, int encrypt)
840 {
841         struct ieee80211_local *local = sdata->local;
842         struct sk_buff *skb;
843         struct ieee80211_mgmt *mgmt;
844
845         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
846                             sizeof(*mgmt) + 6 + extra_len);
847         if (!skb) {
848                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
849                        "frame\n", sdata->dev->name);
850                 return;
851         }
852         skb_reserve(skb, local->hw.extra_tx_headroom);
853
854         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
855         memset(mgmt, 0, 24 + 6);
856         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
857                                           IEEE80211_STYPE_AUTH);
858         if (encrypt)
859                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
860         memcpy(mgmt->da, bssid, ETH_ALEN);
861         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
862         memcpy(mgmt->bssid, bssid, ETH_ALEN);
863         mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
864         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
865         mgmt->u.auth.status_code = cpu_to_le16(0);
866         if (extra)
867                 memcpy(skb_put(skb, extra_len), extra, extra_len);
868
869         ieee80211_tx_skb(sdata, skb, encrypt);
870 }
871
872 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
873                               u8 *ssid, size_t ssid_len,
874                               u8 *ie, size_t ie_len)
875 {
876         struct ieee80211_local *local = sdata->local;
877         struct ieee80211_supported_band *sband;
878         struct sk_buff *skb;
879         struct ieee80211_mgmt *mgmt;
880         u8 *pos, *supp_rates, *esupp_rates = NULL;
881         int i;
882
883         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
884                             ie_len);
885         if (!skb) {
886                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
887                        "request\n", sdata->dev->name);
888                 return;
889         }
890         skb_reserve(skb, local->hw.extra_tx_headroom);
891
892         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
893         memset(mgmt, 0, 24);
894         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
895                                           IEEE80211_STYPE_PROBE_REQ);
896         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
897         if (dst) {
898                 memcpy(mgmt->da, dst, ETH_ALEN);
899                 memcpy(mgmt->bssid, dst, ETH_ALEN);
900         } else {
901                 memset(mgmt->da, 0xff, ETH_ALEN);
902                 memset(mgmt->bssid, 0xff, ETH_ALEN);
903         }
904         pos = skb_put(skb, 2 + ssid_len);
905         *pos++ = WLAN_EID_SSID;
906         *pos++ = ssid_len;
907         memcpy(pos, ssid, ssid_len);
908
909         supp_rates = skb_put(skb, 2);
910         supp_rates[0] = WLAN_EID_SUPP_RATES;
911         supp_rates[1] = 0;
912         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
913
914         for (i = 0; i < sband->n_bitrates; i++) {
915                 struct ieee80211_rate *rate = &sband->bitrates[i];
916                 if (esupp_rates) {
917                         pos = skb_put(skb, 1);
918                         esupp_rates[1]++;
919                 } else if (supp_rates[1] == 8) {
920                         esupp_rates = skb_put(skb, 3);
921                         esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
922                         esupp_rates[1] = 1;
923                         pos = &esupp_rates[2];
924                 } else {
925                         pos = skb_put(skb, 1);
926                         supp_rates[1]++;
927                 }
928                 *pos = rate->bitrate / 5;
929         }
930
931         if (ie)
932                 memcpy(skb_put(skb, ie_len), ie, ie_len);
933
934         ieee80211_tx_skb(sdata, skb, 0);
935 }
936
937 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
938                             struct ieee802_11_elems *elems,
939                             enum ieee80211_band band)
940 {
941         struct ieee80211_supported_band *sband;
942         struct ieee80211_rate *bitrates;
943         size_t num_rates;
944         u32 supp_rates;
945         int i, j;
946         sband = local->hw.wiphy->bands[band];
947
948         if (!sband) {
949                 WARN_ON(1);
950                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
951         }
952
953         bitrates = sband->bitrates;
954         num_rates = sband->n_bitrates;
955         supp_rates = 0;
956         for (i = 0; i < elems->supp_rates_len +
957                      elems->ext_supp_rates_len; i++) {
958                 u8 rate = 0;
959                 int own_rate;
960                 if (i < elems->supp_rates_len)
961                         rate = elems->supp_rates[i];
962                 else if (elems->ext_supp_rates)
963                         rate = elems->ext_supp_rates
964                                 [i - elems->supp_rates_len];
965                 own_rate = 5 * (rate & 0x7f);
966                 for (j = 0; j < num_rates; j++)
967                         if (bitrates[j].bitrate == own_rate)
968                                 supp_rates |= BIT(j);
969         }
970         return supp_rates;
971 }