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