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