Merge ssh://master.kernel.org/pub/scm/linux/kernel/git/linville/wireless-2.6
[linux-2.6.git] / 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/bitmap.h>
22 #include <linux/crc32.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 "driver-ops.h"
29 #include "rate.h"
30 #include "mesh.h"
31 #include "wme.h"
32 #include "led.h"
33 #include "wep.h"
34
35 /* privid for wiphys to determine whether they belong to us or not */
36 void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
37
38 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
39 {
40         struct ieee80211_local *local;
41         BUG_ON(!wiphy);
42
43         local = wiphy_priv(wiphy);
44         return &local->hw;
45 }
46 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
47
48 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
49                         enum nl80211_iftype type)
50 {
51         __le16 fc = hdr->frame_control;
52
53          /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
54         if (len < 16)
55                 return NULL;
56
57         if (ieee80211_is_data(fc)) {
58                 if (len < 24) /* drop incorrect hdr len (data) */
59                         return NULL;
60
61                 if (ieee80211_has_a4(fc))
62                         return NULL;
63                 if (ieee80211_has_tods(fc))
64                         return hdr->addr1;
65                 if (ieee80211_has_fromds(fc))
66                         return hdr->addr2;
67
68                 return hdr->addr3;
69         }
70
71         if (ieee80211_is_mgmt(fc)) {
72                 if (len < 24) /* drop incorrect hdr len (mgmt) */
73                         return NULL;
74                 return hdr->addr3;
75         }
76
77         if (ieee80211_is_ctl(fc)) {
78                 if(ieee80211_is_pspoll(fc))
79                         return hdr->addr1;
80
81                 if (ieee80211_is_back_req(fc)) {
82                         switch (type) {
83                         case NL80211_IFTYPE_STATION:
84                                 return hdr->addr2;
85                         case NL80211_IFTYPE_AP:
86                         case NL80211_IFTYPE_AP_VLAN:
87                                 return hdr->addr1;
88                         default:
89                                 break; /* fall through to the return */
90                         }
91                 }
92         }
93
94         return NULL;
95 }
96
97 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
98 {
99         struct sk_buff *skb = tx->skb;
100         struct ieee80211_hdr *hdr;
101
102         do {
103                 hdr = (struct ieee80211_hdr *) skb->data;
104                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
105         } while ((skb = skb->next));
106 }
107
108 int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
109                              int rate, int erp, int short_preamble)
110 {
111         int dur;
112
113         /* calculate duration (in microseconds, rounded up to next higher
114          * integer if it includes a fractional microsecond) to send frame of
115          * len bytes (does not include FCS) at the given rate. Duration will
116          * also include SIFS.
117          *
118          * rate is in 100 kbps, so divident is multiplied by 10 in the
119          * DIV_ROUND_UP() operations.
120          */
121
122         if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
123                 /*
124                  * OFDM:
125                  *
126                  * N_DBPS = DATARATE x 4
127                  * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
128                  *      (16 = SIGNAL time, 6 = tail bits)
129                  * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
130                  *
131                  * T_SYM = 4 usec
132                  * 802.11a - 17.5.2: aSIFSTime = 16 usec
133                  * 802.11g - 19.8.4: aSIFSTime = 10 usec +
134                  *      signal ext = 6 usec
135                  */
136                 dur = 16; /* SIFS + signal ext */
137                 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
138                 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
139                 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
140                                         4 * rate); /* T_SYM x N_SYM */
141         } else {
142                 /*
143                  * 802.11b or 802.11g with 802.11b compatibility:
144                  * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
145                  * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
146                  *
147                  * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
148                  * aSIFSTime = 10 usec
149                  * aPreambleLength = 144 usec or 72 usec with short preamble
150                  * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
151                  */
152                 dur = 10; /* aSIFSTime = 10 usec */
153                 dur += short_preamble ? (72 + 24) : (144 + 48);
154
155                 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
156         }
157
158         return dur;
159 }
160
161 /* Exported duration function for driver use */
162 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
163                                         struct ieee80211_vif *vif,
164                                         size_t frame_len,
165                                         struct ieee80211_rate *rate)
166 {
167         struct ieee80211_local *local = hw_to_local(hw);
168         struct ieee80211_sub_if_data *sdata;
169         u16 dur;
170         int erp;
171         bool short_preamble = false;
172
173         erp = 0;
174         if (vif) {
175                 sdata = vif_to_sdata(vif);
176                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
177                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
178                         erp = rate->flags & IEEE80211_RATE_ERP_G;
179         }
180
181         dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
182                                        short_preamble);
183
184         return cpu_to_le16(dur);
185 }
186 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
187
188 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
189                               struct ieee80211_vif *vif, size_t frame_len,
190                               const struct ieee80211_tx_info *frame_txctl)
191 {
192         struct ieee80211_local *local = hw_to_local(hw);
193         struct ieee80211_rate *rate;
194         struct ieee80211_sub_if_data *sdata;
195         bool short_preamble;
196         int erp;
197         u16 dur;
198         struct ieee80211_supported_band *sband;
199
200         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
201
202         short_preamble = false;
203
204         rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
205
206         erp = 0;
207         if (vif) {
208                 sdata = vif_to_sdata(vif);
209                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
210                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
211                         erp = rate->flags & IEEE80211_RATE_ERP_G;
212         }
213
214         /* CTS duration */
215         dur = ieee80211_frame_duration(local, 10, rate->bitrate,
216                                        erp, short_preamble);
217         /* Data frame duration */
218         dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
219                                         erp, short_preamble);
220         /* ACK duration */
221         dur += ieee80211_frame_duration(local, 10, rate->bitrate,
222                                         erp, short_preamble);
223
224         return cpu_to_le16(dur);
225 }
226 EXPORT_SYMBOL(ieee80211_rts_duration);
227
228 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
229                                     struct ieee80211_vif *vif,
230                                     size_t frame_len,
231                                     const struct ieee80211_tx_info *frame_txctl)
232 {
233         struct ieee80211_local *local = hw_to_local(hw);
234         struct ieee80211_rate *rate;
235         struct ieee80211_sub_if_data *sdata;
236         bool short_preamble;
237         int erp;
238         u16 dur;
239         struct ieee80211_supported_band *sband;
240
241         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
242
243         short_preamble = false;
244
245         rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
246         erp = 0;
247         if (vif) {
248                 sdata = vif_to_sdata(vif);
249                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
250                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
251                         erp = rate->flags & IEEE80211_RATE_ERP_G;
252         }
253
254         /* Data frame duration */
255         dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
256                                        erp, short_preamble);
257         if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
258                 /* ACK duration */
259                 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
260                                                 erp, short_preamble);
261         }
262
263         return cpu_to_le16(dur);
264 }
265 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
266
267 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
268                                    enum queue_stop_reason reason)
269 {
270         struct ieee80211_local *local = hw_to_local(hw);
271         struct ieee80211_sub_if_data *sdata;
272
273         trace_wake_queue(local, queue, reason);
274
275         if (WARN_ON(queue >= hw->queues))
276                 return;
277
278         __clear_bit(reason, &local->queue_stop_reasons[queue]);
279
280         if (local->queue_stop_reasons[queue] != 0)
281                 /* someone still has this queue stopped */
282                 return;
283
284         if (skb_queue_empty(&local->pending[queue])) {
285                 rcu_read_lock();
286                 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
287                         if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
288                                 continue;
289                         netif_wake_subqueue(sdata->dev, queue);
290                 }
291                 rcu_read_unlock();
292         } else
293                 tasklet_schedule(&local->tx_pending_tasklet);
294 }
295
296 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
297                                     enum queue_stop_reason reason)
298 {
299         struct ieee80211_local *local = hw_to_local(hw);
300         unsigned long flags;
301
302         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
303         __ieee80211_wake_queue(hw, queue, reason);
304         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
305 }
306
307 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
308 {
309         ieee80211_wake_queue_by_reason(hw, queue,
310                                        IEEE80211_QUEUE_STOP_REASON_DRIVER);
311 }
312 EXPORT_SYMBOL(ieee80211_wake_queue);
313
314 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
315                                    enum queue_stop_reason reason)
316 {
317         struct ieee80211_local *local = hw_to_local(hw);
318         struct ieee80211_sub_if_data *sdata;
319
320         trace_stop_queue(local, queue, reason);
321
322         if (WARN_ON(queue >= hw->queues))
323                 return;
324
325         __set_bit(reason, &local->queue_stop_reasons[queue]);
326
327         rcu_read_lock();
328         list_for_each_entry_rcu(sdata, &local->interfaces, list)
329                 netif_stop_subqueue(sdata->dev, queue);
330         rcu_read_unlock();
331 }
332
333 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
334                                     enum queue_stop_reason reason)
335 {
336         struct ieee80211_local *local = hw_to_local(hw);
337         unsigned long flags;
338
339         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
340         __ieee80211_stop_queue(hw, queue, reason);
341         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
342 }
343
344 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
345 {
346         ieee80211_stop_queue_by_reason(hw, queue,
347                                        IEEE80211_QUEUE_STOP_REASON_DRIVER);
348 }
349 EXPORT_SYMBOL(ieee80211_stop_queue);
350
351 void ieee80211_add_pending_skb(struct ieee80211_local *local,
352                                struct sk_buff *skb)
353 {
354         struct ieee80211_hw *hw = &local->hw;
355         unsigned long flags;
356         int queue = skb_get_queue_mapping(skb);
357         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
358
359         if (WARN_ON(!info->control.vif)) {
360                 kfree_skb(skb);
361                 return;
362         }
363
364         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
365         __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
366         __skb_queue_tail(&local->pending[queue], skb);
367         __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
368         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
369 }
370
371 int ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
372                                   struct sk_buff_head *skbs,
373                                   void (*fn)(void *data), void *data)
374 {
375         struct ieee80211_hw *hw = &local->hw;
376         struct sk_buff *skb;
377         unsigned long flags;
378         int queue, ret = 0, i;
379
380         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
381         for (i = 0; i < hw->queues; i++)
382                 __ieee80211_stop_queue(hw, i,
383                         IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
384
385         while ((skb = skb_dequeue(skbs))) {
386                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
387
388                 if (WARN_ON(!info->control.vif)) {
389                         kfree_skb(skb);
390                         continue;
391                 }
392
393                 ret++;
394                 queue = skb_get_queue_mapping(skb);
395                 __skb_queue_tail(&local->pending[queue], skb);
396         }
397
398         if (fn)
399                 fn(data);
400
401         for (i = 0; i < hw->queues; i++)
402                 __ieee80211_wake_queue(hw, i,
403                         IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
404         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
405
406         return ret;
407 }
408
409 int ieee80211_add_pending_skbs(struct ieee80211_local *local,
410                                struct sk_buff_head *skbs)
411 {
412         return ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
413 }
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         unsigned long flags;
441         int ret;
442
443         if (WARN_ON(queue >= hw->queues))
444                 return true;
445
446         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
447         ret = !!local->queue_stop_reasons[queue];
448         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
449         return ret;
450 }
451 EXPORT_SYMBOL(ieee80211_queue_stopped);
452
453 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
454                                      enum queue_stop_reason reason)
455 {
456         struct ieee80211_local *local = hw_to_local(hw);
457         unsigned long flags;
458         int i;
459
460         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
461
462         for (i = 0; i < hw->queues; i++)
463                 __ieee80211_wake_queue(hw, i, reason);
464
465         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
466 }
467
468 void ieee80211_wake_queues(struct ieee80211_hw *hw)
469 {
470         ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
471 }
472 EXPORT_SYMBOL(ieee80211_wake_queues);
473
474 void ieee80211_iterate_active_interfaces(
475         struct ieee80211_hw *hw,
476         void (*iterator)(void *data, u8 *mac,
477                          struct ieee80211_vif *vif),
478         void *data)
479 {
480         struct ieee80211_local *local = hw_to_local(hw);
481         struct ieee80211_sub_if_data *sdata;
482
483         mutex_lock(&local->iflist_mtx);
484
485         list_for_each_entry(sdata, &local->interfaces, list) {
486                 switch (sdata->vif.type) {
487                 case NL80211_IFTYPE_MONITOR:
488                 case NL80211_IFTYPE_AP_VLAN:
489                         continue;
490                 default:
491                         break;
492                 }
493                 if (ieee80211_sdata_running(sdata))
494                         iterator(data, sdata->vif.addr,
495                                  &sdata->vif);
496         }
497
498         mutex_unlock(&local->iflist_mtx);
499 }
500 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
501
502 void ieee80211_iterate_active_interfaces_atomic(
503         struct ieee80211_hw *hw,
504         void (*iterator)(void *data, u8 *mac,
505                          struct ieee80211_vif *vif),
506         void *data)
507 {
508         struct ieee80211_local *local = hw_to_local(hw);
509         struct ieee80211_sub_if_data *sdata;
510
511         rcu_read_lock();
512
513         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
514                 switch (sdata->vif.type) {
515                 case NL80211_IFTYPE_MONITOR:
516                 case NL80211_IFTYPE_AP_VLAN:
517                         continue;
518                 default:
519                         break;
520                 }
521                 if (ieee80211_sdata_running(sdata))
522                         iterator(data, sdata->vif.addr,
523                                  &sdata->vif);
524         }
525
526         rcu_read_unlock();
527 }
528 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
529
530 /*
531  * Nothing should have been stuffed into the workqueue during
532  * the suspend->resume cycle. If this WARN is seen then there
533  * is a bug with either the driver suspend or something in
534  * mac80211 stuffing into the workqueue which we haven't yet
535  * cleared during mac80211's suspend cycle.
536  */
537 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
538 {
539         if (WARN(local->suspended && !local->resuming,
540                  "queueing ieee80211 work while going to suspend\n"))
541                 return false;
542
543         return true;
544 }
545
546 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
547 {
548         struct ieee80211_local *local = hw_to_local(hw);
549
550         if (!ieee80211_can_queue_work(local))
551                 return;
552
553         queue_work(local->workqueue, work);
554 }
555 EXPORT_SYMBOL(ieee80211_queue_work);
556
557 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
558                                   struct delayed_work *dwork,
559                                   unsigned long delay)
560 {
561         struct ieee80211_local *local = hw_to_local(hw);
562
563         if (!ieee80211_can_queue_work(local))
564                 return;
565
566         queue_delayed_work(local->workqueue, dwork, delay);
567 }
568 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
569
570 void ieee802_11_parse_elems(u8 *start, size_t len,
571                             struct ieee802_11_elems *elems)
572 {
573         ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
574 }
575
576 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
577                                struct ieee802_11_elems *elems,
578                                u64 filter, u32 crc)
579 {
580         size_t left = len;
581         u8 *pos = start;
582         bool calc_crc = filter != 0;
583
584         memset(elems, 0, sizeof(*elems));
585         elems->ie_start = start;
586         elems->total_len = len;
587
588         while (left >= 2) {
589                 u8 id, elen;
590
591                 id = *pos++;
592                 elen = *pos++;
593                 left -= 2;
594
595                 if (elen > left)
596                         break;
597
598                 if (calc_crc && id < 64 && (filter & (1ULL << id)))
599                         crc = crc32_be(crc, pos - 2, elen + 2);
600
601                 switch (id) {
602                 case WLAN_EID_SSID:
603                         elems->ssid = pos;
604                         elems->ssid_len = elen;
605                         break;
606                 case WLAN_EID_SUPP_RATES:
607                         elems->supp_rates = pos;
608                         elems->supp_rates_len = elen;
609                         break;
610                 case WLAN_EID_FH_PARAMS:
611                         elems->fh_params = pos;
612                         elems->fh_params_len = elen;
613                         break;
614                 case WLAN_EID_DS_PARAMS:
615                         elems->ds_params = pos;
616                         elems->ds_params_len = elen;
617                         break;
618                 case WLAN_EID_CF_PARAMS:
619                         elems->cf_params = pos;
620                         elems->cf_params_len = elen;
621                         break;
622                 case WLAN_EID_TIM:
623                         if (elen >= sizeof(struct ieee80211_tim_ie)) {
624                                 elems->tim = (void *)pos;
625                                 elems->tim_len = elen;
626                         }
627                         break;
628                 case WLAN_EID_IBSS_PARAMS:
629                         elems->ibss_params = pos;
630                         elems->ibss_params_len = elen;
631                         break;
632                 case WLAN_EID_CHALLENGE:
633                         elems->challenge = pos;
634                         elems->challenge_len = elen;
635                         break;
636                 case WLAN_EID_VENDOR_SPECIFIC:
637                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
638                             pos[2] == 0xf2) {
639                                 /* Microsoft OUI (00:50:F2) */
640
641                                 if (calc_crc)
642                                         crc = crc32_be(crc, pos - 2, elen + 2);
643
644                                 if (pos[3] == 1) {
645                                         /* OUI Type 1 - WPA IE */
646                                         elems->wpa = pos;
647                                         elems->wpa_len = elen;
648                                 } else if (elen >= 5 && pos[3] == 2) {
649                                         /* OUI Type 2 - WMM IE */
650                                         if (pos[4] == 0) {
651                                                 elems->wmm_info = pos;
652                                                 elems->wmm_info_len = elen;
653                                         } else if (pos[4] == 1) {
654                                                 elems->wmm_param = pos;
655                                                 elems->wmm_param_len = elen;
656                                         }
657                                 }
658                         }
659                         break;
660                 case WLAN_EID_RSN:
661                         elems->rsn = pos;
662                         elems->rsn_len = elen;
663                         break;
664                 case WLAN_EID_ERP_INFO:
665                         elems->erp_info = pos;
666                         elems->erp_info_len = elen;
667                         break;
668                 case WLAN_EID_EXT_SUPP_RATES:
669                         elems->ext_supp_rates = pos;
670                         elems->ext_supp_rates_len = elen;
671                         break;
672                 case WLAN_EID_HT_CAPABILITY:
673                         if (elen >= sizeof(struct ieee80211_ht_cap))
674                                 elems->ht_cap_elem = (void *)pos;
675                         break;
676                 case WLAN_EID_HT_INFORMATION:
677                         if (elen >= sizeof(struct ieee80211_ht_info))
678                                 elems->ht_info_elem = (void *)pos;
679                         break;
680                 case WLAN_EID_MESH_ID:
681                         elems->mesh_id = pos;
682                         elems->mesh_id_len = elen;
683                         break;
684                 case WLAN_EID_MESH_CONFIG:
685                         if (elen >= sizeof(struct ieee80211_meshconf_ie))
686                                 elems->mesh_config = (void *)pos;
687                         break;
688                 case WLAN_EID_PEER_LINK:
689                         elems->peer_link = pos;
690                         elems->peer_link_len = elen;
691                         break;
692                 case WLAN_EID_PREQ:
693                         elems->preq = pos;
694                         elems->preq_len = elen;
695                         break;
696                 case WLAN_EID_PREP:
697                         elems->prep = pos;
698                         elems->prep_len = elen;
699                         break;
700                 case WLAN_EID_PERR:
701                         elems->perr = pos;
702                         elems->perr_len = elen;
703                         break;
704                 case WLAN_EID_RANN:
705                         if (elen >= sizeof(struct ieee80211_rann_ie))
706                                 elems->rann = (void *)pos;
707                         break;
708                 case WLAN_EID_CHANNEL_SWITCH:
709                         elems->ch_switch_elem = pos;
710                         elems->ch_switch_elem_len = elen;
711                         break;
712                 case WLAN_EID_QUIET:
713                         if (!elems->quiet_elem) {
714                                 elems->quiet_elem = pos;
715                                 elems->quiet_elem_len = elen;
716                         }
717                         elems->num_of_quiet_elem++;
718                         break;
719                 case WLAN_EID_COUNTRY:
720                         elems->country_elem = pos;
721                         elems->country_elem_len = elen;
722                         break;
723                 case WLAN_EID_PWR_CONSTRAINT:
724                         elems->pwr_constr_elem = pos;
725                         elems->pwr_constr_elem_len = elen;
726                         break;
727                 case WLAN_EID_TIMEOUT_INTERVAL:
728                         elems->timeout_int = pos;
729                         elems->timeout_int_len = elen;
730                         break;
731                 default:
732                         break;
733                 }
734
735                 left -= elen;
736                 pos += elen;
737         }
738
739         return crc;
740 }
741
742 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
743 {
744         struct ieee80211_local *local = sdata->local;
745         struct ieee80211_tx_queue_params qparam;
746         int queue;
747         bool use_11b;
748         int aCWmin, aCWmax;
749
750         if (!local->ops->conf_tx)
751                 return;
752
753         memset(&qparam, 0, sizeof(qparam));
754
755         use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
756                  !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
757
758         for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
759                 /* Set defaults according to 802.11-2007 Table 7-37 */
760                 aCWmax = 1023;
761                 if (use_11b)
762                         aCWmin = 31;
763                 else
764                         aCWmin = 15;
765
766                 switch (queue) {
767                 case 3: /* AC_BK */
768                         qparam.cw_max = aCWmax;
769                         qparam.cw_min = aCWmin;
770                         qparam.txop = 0;
771                         qparam.aifs = 7;
772                         break;
773                 default: /* never happens but let's not leave undefined */
774                 case 2: /* AC_BE */
775                         qparam.cw_max = aCWmax;
776                         qparam.cw_min = aCWmin;
777                         qparam.txop = 0;
778                         qparam.aifs = 3;
779                         break;
780                 case 1: /* AC_VI */
781                         qparam.cw_max = aCWmin;
782                         qparam.cw_min = (aCWmin + 1) / 2 - 1;
783                         if (use_11b)
784                                 qparam.txop = 6016/32;
785                         else
786                                 qparam.txop = 3008/32;
787                         qparam.aifs = 2;
788                         break;
789                 case 0: /* AC_VO */
790                         qparam.cw_max = (aCWmin + 1) / 2 - 1;
791                         qparam.cw_min = (aCWmin + 1) / 4 - 1;
792                         if (use_11b)
793                                 qparam.txop = 3264/32;
794                         else
795                                 qparam.txop = 1504/32;
796                         qparam.aifs = 2;
797                         break;
798                 }
799
800                 qparam.uapsd = false;
801
802                 drv_conf_tx(local, queue, &qparam);
803         }
804
805         /* after reinitialize QoS TX queues setting to default,
806          * disable QoS at all */
807
808         if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
809                 sdata->vif.bss_conf.qos =
810                         sdata->vif.type != NL80211_IFTYPE_STATION;
811                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
812         }
813 }
814
815 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
816                                   const size_t supp_rates_len,
817                                   const u8 *supp_rates)
818 {
819         struct ieee80211_local *local = sdata->local;
820         int i, have_higher_than_11mbit = 0;
821
822         /* cf. IEEE 802.11 9.2.12 */
823         for (i = 0; i < supp_rates_len; i++)
824                 if ((supp_rates[i] & 0x7f) * 5 > 110)
825                         have_higher_than_11mbit = 1;
826
827         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
828             have_higher_than_11mbit)
829                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
830         else
831                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
832
833         ieee80211_set_wmm_default(sdata);
834 }
835
836 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
837                               enum ieee80211_band band)
838 {
839         struct ieee80211_supported_band *sband;
840         struct ieee80211_rate *bitrates;
841         u32 mandatory_rates;
842         enum ieee80211_rate_flags mandatory_flag;
843         int i;
844
845         sband = local->hw.wiphy->bands[band];
846         if (!sband) {
847                 WARN_ON(1);
848                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
849         }
850
851         if (band == IEEE80211_BAND_2GHZ)
852                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
853         else
854                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
855
856         bitrates = sband->bitrates;
857         mandatory_rates = 0;
858         for (i = 0; i < sband->n_bitrates; i++)
859                 if (bitrates[i].flags & mandatory_flag)
860                         mandatory_rates |= BIT(i);
861         return mandatory_rates;
862 }
863
864 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
865                          u16 transaction, u16 auth_alg,
866                          u8 *extra, size_t extra_len, const u8 *bssid,
867                          const u8 *key, u8 key_len, u8 key_idx)
868 {
869         struct ieee80211_local *local = sdata->local;
870         struct sk_buff *skb;
871         struct ieee80211_mgmt *mgmt;
872         int err;
873
874         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
875                             sizeof(*mgmt) + 6 + extra_len);
876         if (!skb) {
877                 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
878                        "frame\n", sdata->name);
879                 return;
880         }
881         skb_reserve(skb, local->hw.extra_tx_headroom);
882
883         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
884         memset(mgmt, 0, 24 + 6);
885         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
886                                           IEEE80211_STYPE_AUTH);
887         memcpy(mgmt->da, bssid, ETH_ALEN);
888         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
889         memcpy(mgmt->bssid, bssid, ETH_ALEN);
890         mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
891         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
892         mgmt->u.auth.status_code = cpu_to_le16(0);
893         if (extra)
894                 memcpy(skb_put(skb, extra_len), extra, extra_len);
895
896         if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
897                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
898                 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
899                 WARN_ON(err);
900         }
901
902         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
903         ieee80211_tx_skb(sdata, skb);
904 }
905
906 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
907                              const u8 *ie, size_t ie_len,
908                              enum ieee80211_band band, u32 rate_mask,
909                              u8 channel)
910 {
911         struct ieee80211_supported_band *sband;
912         u8 *pos;
913         size_t offset = 0, noffset;
914         int supp_rates_len, i;
915         u8 rates[32];
916         int num_rates;
917         int ext_rates_len;
918
919         sband = local->hw.wiphy->bands[band];
920
921         pos = buffer;
922
923         num_rates = 0;
924         for (i = 0; i < sband->n_bitrates; i++) {
925                 if ((BIT(i) & rate_mask) == 0)
926                         continue; /* skip rate */
927                 rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
928         }
929
930         supp_rates_len = min_t(int, num_rates, 8);
931
932         *pos++ = WLAN_EID_SUPP_RATES;
933         *pos++ = supp_rates_len;
934         memcpy(pos, rates, supp_rates_len);
935         pos += supp_rates_len;
936
937         /* insert "request information" if in custom IEs */
938         if (ie && ie_len) {
939                 static const u8 before_extrates[] = {
940                         WLAN_EID_SSID,
941                         WLAN_EID_SUPP_RATES,
942                         WLAN_EID_REQUEST,
943                 };
944                 noffset = ieee80211_ie_split(ie, ie_len,
945                                              before_extrates,
946                                              ARRAY_SIZE(before_extrates),
947                                              offset);
948                 memcpy(pos, ie + offset, noffset - offset);
949                 pos += noffset - offset;
950                 offset = noffset;
951         }
952
953         ext_rates_len = num_rates - supp_rates_len;
954         if (ext_rates_len > 0) {
955                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
956                 *pos++ = ext_rates_len;
957                 memcpy(pos, rates + supp_rates_len, ext_rates_len);
958                 pos += ext_rates_len;
959         }
960
961         if (channel && sband->band == IEEE80211_BAND_2GHZ) {
962                 *pos++ = WLAN_EID_DS_PARAMS;
963                 *pos++ = 1;
964                 *pos++ = channel;
965         }
966
967         /* insert custom IEs that go before HT */
968         if (ie && ie_len) {
969                 static const u8 before_ht[] = {
970                         WLAN_EID_SSID,
971                         WLAN_EID_SUPP_RATES,
972                         WLAN_EID_REQUEST,
973                         WLAN_EID_EXT_SUPP_RATES,
974                         WLAN_EID_DS_PARAMS,
975                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
976                 };
977                 noffset = ieee80211_ie_split(ie, ie_len,
978                                              before_ht, ARRAY_SIZE(before_ht),
979                                              offset);
980                 memcpy(pos, ie + offset, noffset - offset);
981                 pos += noffset - offset;
982                 offset = noffset;
983         }
984
985         if (sband->ht_cap.ht_supported) {
986                 u16 cap = sband->ht_cap.cap;
987                 __le16 tmp;
988
989                 *pos++ = WLAN_EID_HT_CAPABILITY;
990                 *pos++ = sizeof(struct ieee80211_ht_cap);
991                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
992                 tmp = cpu_to_le16(cap);
993                 memcpy(pos, &tmp, sizeof(u16));
994                 pos += sizeof(u16);
995                 *pos++ = sband->ht_cap.ampdu_factor |
996                          (sband->ht_cap.ampdu_density <<
997                                 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
998                 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
999                 pos += sizeof(sband->ht_cap.mcs);
1000                 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
1001         }
1002
1003         /*
1004          * If adding more here, adjust code in main.c
1005          * that calculates local->scan_ies_len.
1006          */
1007
1008         /* add any remaining custom IEs */
1009         if (ie && ie_len) {
1010                 noffset = ie_len;
1011                 memcpy(pos, ie + offset, noffset - offset);
1012                 pos += noffset - offset;
1013         }
1014
1015         return pos - buffer;
1016 }
1017
1018 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1019                                           u8 *dst,
1020                                           const u8 *ssid, size_t ssid_len,
1021                                           const u8 *ie, size_t ie_len)
1022 {
1023         struct ieee80211_local *local = sdata->local;
1024         struct sk_buff *skb;
1025         struct ieee80211_mgmt *mgmt;
1026         size_t buf_len;
1027         u8 *buf;
1028         u8 chan;
1029
1030         /* FIXME: come up with a proper value */
1031         buf = kmalloc(200 + ie_len, GFP_KERNEL);
1032         if (!buf) {
1033                 printk(KERN_DEBUG "%s: failed to allocate temporary IE "
1034                        "buffer\n", sdata->name);
1035                 return NULL;
1036         }
1037
1038         chan = ieee80211_frequency_to_channel(
1039                 local->hw.conf.channel->center_freq);
1040
1041         buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
1042                                            local->hw.conf.channel->band,
1043                                            sdata->rc_rateidx_mask
1044                                            [local->hw.conf.channel->band],
1045                                            chan);
1046
1047         skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
1048                                      ssid, ssid_len,
1049                                      buf, buf_len);
1050
1051         if (dst) {
1052                 mgmt = (struct ieee80211_mgmt *) skb->data;
1053                 memcpy(mgmt->da, dst, ETH_ALEN);
1054                 memcpy(mgmt->bssid, dst, ETH_ALEN);
1055         }
1056
1057         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1058         kfree(buf);
1059
1060         return skb;
1061 }
1062
1063 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1064                               const u8 *ssid, size_t ssid_len,
1065                               const u8 *ie, size_t ie_len)
1066 {
1067         struct sk_buff *skb;
1068
1069         skb = ieee80211_build_probe_req(sdata, dst, ssid, ssid_len, ie, ie_len);
1070         if (skb)
1071                 ieee80211_tx_skb(sdata, skb);
1072 }
1073
1074 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
1075                             struct ieee802_11_elems *elems,
1076                             enum ieee80211_band band)
1077 {
1078         struct ieee80211_supported_band *sband;
1079         struct ieee80211_rate *bitrates;
1080         size_t num_rates;
1081         u32 supp_rates;
1082         int i, j;
1083         sband = local->hw.wiphy->bands[band];
1084
1085         if (!sband) {
1086                 WARN_ON(1);
1087                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1088         }
1089
1090         bitrates = sband->bitrates;
1091         num_rates = sband->n_bitrates;
1092         supp_rates = 0;
1093         for (i = 0; i < elems->supp_rates_len +
1094                      elems->ext_supp_rates_len; i++) {
1095                 u8 rate = 0;
1096                 int own_rate;
1097                 if (i < elems->supp_rates_len)
1098                         rate = elems->supp_rates[i];
1099                 else if (elems->ext_supp_rates)
1100                         rate = elems->ext_supp_rates
1101                                 [i - elems->supp_rates_len];
1102                 own_rate = 5 * (rate & 0x7f);
1103                 for (j = 0; j < num_rates; j++)
1104                         if (bitrates[j].bitrate == own_rate)
1105                                 supp_rates |= BIT(j);
1106         }
1107         return supp_rates;
1108 }
1109
1110 void ieee80211_stop_device(struct ieee80211_local *local)
1111 {
1112         ieee80211_led_radio(local, false);
1113         ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
1114
1115         cancel_work_sync(&local->reconfig_filter);
1116
1117         flush_workqueue(local->workqueue);
1118         drv_stop(local);
1119 }
1120
1121 int ieee80211_reconfig(struct ieee80211_local *local)
1122 {
1123         struct ieee80211_hw *hw = &local->hw;
1124         struct ieee80211_sub_if_data *sdata;
1125         struct sta_info *sta;
1126         int res;
1127
1128         if (local->suspended)
1129                 local->resuming = true;
1130
1131         /* restart hardware */
1132         if (local->open_count) {
1133                 /*
1134                  * Upon resume hardware can sometimes be goofy due to
1135                  * various platform / driver / bus issues, so restarting
1136                  * the device may at times not work immediately. Propagate
1137                  * the error.
1138                  */
1139                 res = drv_start(local);
1140                 if (res) {
1141                         WARN(local->suspended, "Hardware became unavailable "
1142                              "upon resume. This could be a software issue "
1143                              "prior to suspend or a hardware issue.\n");
1144                         return res;
1145                 }
1146
1147                 ieee80211_led_radio(local, true);
1148                 ieee80211_mod_tpt_led_trig(local,
1149                                            IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1150         }
1151
1152         /* add interfaces */
1153         list_for_each_entry(sdata, &local->interfaces, list) {
1154                 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1155                     sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1156                     ieee80211_sdata_running(sdata))
1157                         res = drv_add_interface(local, &sdata->vif);
1158         }
1159
1160         /* add STAs back */
1161         mutex_lock(&local->sta_mtx);
1162         list_for_each_entry(sta, &local->sta_list, list) {
1163                 if (sta->uploaded) {
1164                         sdata = sta->sdata;
1165                         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1166                                 sdata = container_of(sdata->bss,
1167                                              struct ieee80211_sub_if_data,
1168                                              u.ap);
1169
1170                         WARN_ON(drv_sta_add(local, sdata, &sta->sta));
1171                 }
1172         }
1173         mutex_unlock(&local->sta_mtx);
1174
1175         /* setup fragmentation threshold */
1176         drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
1177
1178         /* setup RTS threshold */
1179         drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1180
1181         /* reconfigure hardware */
1182         ieee80211_hw_config(local, ~0);
1183
1184         ieee80211_configure_filter(local);
1185
1186         /* Finally also reconfigure all the BSS information */
1187         list_for_each_entry(sdata, &local->interfaces, list) {
1188                 u32 changed;
1189
1190                 if (!ieee80211_sdata_running(sdata))
1191                         continue;
1192
1193                 /* common change flags for all interface types */
1194                 changed = BSS_CHANGED_ERP_CTS_PROT |
1195                           BSS_CHANGED_ERP_PREAMBLE |
1196                           BSS_CHANGED_ERP_SLOT |
1197                           BSS_CHANGED_HT |
1198                           BSS_CHANGED_BASIC_RATES |
1199                           BSS_CHANGED_BEACON_INT |
1200                           BSS_CHANGED_BSSID |
1201                           BSS_CHANGED_CQM |
1202                           BSS_CHANGED_QOS;
1203
1204                 switch (sdata->vif.type) {
1205                 case NL80211_IFTYPE_STATION:
1206                         changed |= BSS_CHANGED_ASSOC;
1207                         mutex_lock(&sdata->u.mgd.mtx);
1208                         ieee80211_bss_info_change_notify(sdata, changed);
1209                         mutex_unlock(&sdata->u.mgd.mtx);
1210                         break;
1211                 case NL80211_IFTYPE_ADHOC:
1212                         changed |= BSS_CHANGED_IBSS;
1213                         /* fall through */
1214                 case NL80211_IFTYPE_AP:
1215                 case NL80211_IFTYPE_MESH_POINT:
1216                         changed |= BSS_CHANGED_BEACON |
1217                                    BSS_CHANGED_BEACON_ENABLED;
1218                         ieee80211_bss_info_change_notify(sdata, changed);
1219                         break;
1220                 case NL80211_IFTYPE_WDS:
1221                         break;
1222                 case NL80211_IFTYPE_AP_VLAN:
1223                 case NL80211_IFTYPE_MONITOR:
1224                         /* ignore virtual */
1225                         break;
1226                 case NL80211_IFTYPE_UNSPECIFIED:
1227                 case NUM_NL80211_IFTYPES:
1228                 case NL80211_IFTYPE_P2P_CLIENT:
1229                 case NL80211_IFTYPE_P2P_GO:
1230                         WARN_ON(1);
1231                         break;
1232                 }
1233         }
1234
1235         /*
1236          * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1237          * sessions can be established after a resume.
1238          *
1239          * Also tear down aggregation sessions since reconfiguring
1240          * them in a hardware restart scenario is not easily done
1241          * right now, and the hardware will have lost information
1242          * about the sessions, but we and the AP still think they
1243          * are active. This is really a workaround though.
1244          */
1245         if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1246                 mutex_lock(&local->sta_mtx);
1247
1248                 list_for_each_entry(sta, &local->sta_list, list) {
1249                         ieee80211_sta_tear_down_BA_sessions(sta, true);
1250                         clear_sta_flags(sta, WLAN_STA_BLOCK_BA);
1251                 }
1252
1253                 mutex_unlock(&local->sta_mtx);
1254         }
1255
1256         /* add back keys */
1257         list_for_each_entry(sdata, &local->interfaces, list)
1258                 if (ieee80211_sdata_running(sdata))
1259                         ieee80211_enable_keys(sdata);
1260
1261         ieee80211_wake_queues_by_reason(hw,
1262                         IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1263
1264         /*
1265          * If this is for hw restart things are still running.
1266          * We may want to change that later, however.
1267          */
1268         if (!local->suspended)
1269                 return 0;
1270
1271 #ifdef CONFIG_PM
1272         /* first set suspended false, then resuming */
1273         local->suspended = false;
1274         mb();
1275         local->resuming = false;
1276
1277         list_for_each_entry(sdata, &local->interfaces, list) {
1278                 switch(sdata->vif.type) {
1279                 case NL80211_IFTYPE_STATION:
1280                         ieee80211_sta_restart(sdata);
1281                         break;
1282                 case NL80211_IFTYPE_ADHOC:
1283                         ieee80211_ibss_restart(sdata);
1284                         break;
1285                 case NL80211_IFTYPE_MESH_POINT:
1286                         ieee80211_mesh_restart(sdata);
1287                         break;
1288                 default:
1289                         break;
1290                 }
1291         }
1292
1293         add_timer(&local->sta_cleanup);
1294
1295         mutex_lock(&local->sta_mtx);
1296         list_for_each_entry(sta, &local->sta_list, list)
1297                 mesh_plink_restart(sta);
1298         mutex_unlock(&local->sta_mtx);
1299 #else
1300         WARN_ON(1);
1301 #endif
1302         return 0;
1303 }
1304
1305 static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1306                           enum ieee80211_smps_mode *smps_mode)
1307 {
1308         if (ifmgd->associated) {
1309                 *smps_mode = ifmgd->ap_smps;
1310
1311                 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1312                         if (ifmgd->powersave)
1313                                 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1314                         else
1315                                 *smps_mode = IEEE80211_SMPS_OFF;
1316                 }
1317
1318                 return 1;
1319         }
1320
1321         return 0;
1322 }
1323
1324 /* must hold iflist_mtx */
1325 void ieee80211_recalc_smps(struct ieee80211_local *local)
1326 {
1327         struct ieee80211_sub_if_data *sdata;
1328         enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1329         int count = 0;
1330
1331         lockdep_assert_held(&local->iflist_mtx);
1332
1333         /*
1334          * This function could be improved to handle multiple
1335          * interfaces better, but right now it makes any
1336          * non-station interfaces force SM PS to be turned
1337          * off. If there are multiple station interfaces it
1338          * could also use the best possible mode, e.g. if
1339          * one is in static and the other in dynamic then
1340          * dynamic is ok.
1341          */
1342
1343         list_for_each_entry(sdata, &local->interfaces, list) {
1344                 if (!ieee80211_sdata_running(sdata))
1345                         continue;
1346                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1347                         goto set;
1348
1349                 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1350
1351                 if (count > 1) {
1352                         smps_mode = IEEE80211_SMPS_OFF;
1353                         break;
1354                 }
1355         }
1356
1357         if (smps_mode == local->smps_mode)
1358                 return;
1359
1360  set:
1361         local->smps_mode = smps_mode;
1362         /* changed flag is auto-detected for this */
1363         ieee80211_hw_config(local, 0);
1364 }
1365
1366 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1367 {
1368         int i;
1369
1370         for (i = 0; i < n_ids; i++)
1371                 if (ids[i] == id)
1372                         return true;
1373         return false;
1374 }
1375
1376 /**
1377  * ieee80211_ie_split - split an IE buffer according to ordering
1378  *
1379  * @ies: the IE buffer
1380  * @ielen: the length of the IE buffer
1381  * @ids: an array with element IDs that are allowed before
1382  *      the split
1383  * @n_ids: the size of the element ID array
1384  * @offset: offset where to start splitting in the buffer
1385  *
1386  * This function splits an IE buffer by updating the @offset
1387  * variable to point to the location where the buffer should be
1388  * split.
1389  *
1390  * It assumes that the given IE buffer is well-formed, this
1391  * has to be guaranteed by the caller!
1392  *
1393  * It also assumes that the IEs in the buffer are ordered
1394  * correctly, if not the result of using this function will not
1395  * be ordered correctly either, i.e. it does no reordering.
1396  *
1397  * The function returns the offset where the next part of the
1398  * buffer starts, which may be @ielen if the entire (remainder)
1399  * of the buffer should be used.
1400  */
1401 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1402                           const u8 *ids, int n_ids, size_t offset)
1403 {
1404         size_t pos = offset;
1405
1406         while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1407                 pos += 2 + ies[pos + 1];
1408
1409         return pos;
1410 }
1411
1412 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1413 {
1414         size_t pos = offset;
1415
1416         while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1417                 pos += 2 + ies[pos + 1];
1418
1419         return pos;
1420 }