mac80211/cfg80211: add station events
[deliverable/linux.git] / net / mac80211 / util.c
CommitLineData
c2d1560a
JB
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>
d91f36db 23#include <linux/crc32.h>
881d966b 24#include <net/net_namespace.h>
c2d1560a 25#include <net/cfg80211.h>
dabeb344 26#include <net/rtnetlink.h>
c2d1560a
JB
27
28#include "ieee80211_i.h"
24487981 29#include "driver-ops.h"
2c8dccc7 30#include "rate.h"
ee385855 31#include "mesh.h"
c2d1560a 32#include "wme.h"
f2753ddb 33#include "led.h"
fffd0934 34#include "wep.h"
c2d1560a
JB
35
36/* privid for wiphys to determine whether they belong to us or not */
37void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
38
9a95371a
LR
39struct 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}
47EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
c2d1560a 48
71364716 49u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
05c914fe 50 enum nl80211_iftype type)
c2d1560a 51{
a494bb1c 52 __le16 fc = hdr->frame_control;
c2d1560a 53
98f0b0a3
RR
54 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
55 if (len < 16)
c2d1560a
JB
56 return NULL;
57
a494bb1c 58 if (ieee80211_is_data(fc)) {
98f0b0a3
RR
59 if (len < 24) /* drop incorrect hdr len (data) */
60 return NULL;
a494bb1c
HH
61
62 if (ieee80211_has_a4(fc))
c2d1560a 63 return NULL;
a494bb1c
HH
64 if (ieee80211_has_tods(fc))
65 return hdr->addr1;
66 if (ieee80211_has_fromds(fc))
c2d1560a 67 return hdr->addr2;
a494bb1c
HH
68
69 return hdr->addr3;
70 }
71
72 if (ieee80211_is_mgmt(fc)) {
98f0b0a3
RR
73 if (len < 24) /* drop incorrect hdr len (mgmt) */
74 return NULL;
c2d1560a 75 return hdr->addr3;
a494bb1c
HH
76 }
77
78 if (ieee80211_is_ctl(fc)) {
79 if(ieee80211_is_pspoll(fc))
c2d1560a 80 return hdr->addr1;
a494bb1c
HH
81
82 if (ieee80211_is_back_req(fc)) {
71364716 83 switch (type) {
05c914fe 84 case NL80211_IFTYPE_STATION:
71364716 85 return hdr->addr2;
05c914fe
JB
86 case NL80211_IFTYPE_AP:
87 case NL80211_IFTYPE_AP_VLAN:
71364716
RR
88 return hdr->addr1;
89 default:
a494bb1c 90 break; /* fall through to the return */
71364716
RR
91 }
92 }
c2d1560a
JB
93 }
94
95 return NULL;
96}
97
5cf121c3 98void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
c2d1560a 99{
2de8e0d9
JB
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));
c2d1560a
JB
107}
108
109int 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
8318d78a 123 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
c2d1560a
JB
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 */
c2d1560a
JB
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 */
32bfd35d
JB
163__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
164 struct ieee80211_vif *vif,
8318d78a
JB
165 size_t frame_len,
166 struct ieee80211_rate *rate)
c2d1560a
JB
167{
168 struct ieee80211_local *local = hw_to_local(hw);
25d834e1 169 struct ieee80211_sub_if_data *sdata;
c2d1560a
JB
170 u16 dur;
171 int erp;
25d834e1 172 bool short_preamble = false;
c2d1560a 173
8318d78a 174 erp = 0;
25d834e1
JB
175 if (vif) {
176 sdata = vif_to_sdata(vif);
bda3933a 177 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
178 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
179 erp = rate->flags & IEEE80211_RATE_ERP_G;
180 }
8318d78a
JB
181
182 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
25d834e1 183 short_preamble);
c2d1560a
JB
184
185 return cpu_to_le16(dur);
186}
187EXPORT_SYMBOL(ieee80211_generic_frame_duration);
188
32bfd35d
JB
189__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
190 struct ieee80211_vif *vif, size_t frame_len,
e039fa4a 191 const struct ieee80211_tx_info *frame_txctl)
c2d1560a
JB
192{
193 struct ieee80211_local *local = hw_to_local(hw);
194 struct ieee80211_rate *rate;
25d834e1 195 struct ieee80211_sub_if_data *sdata;
471b3efd 196 bool short_preamble;
c2d1560a
JB
197 int erp;
198 u16 dur;
2e92e6f2
JB
199 struct ieee80211_supported_band *sband;
200
201 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
c2d1560a 202
25d834e1 203 short_preamble = false;
7e9ed188 204
e039fa4a 205 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
8318d78a
JB
206
207 erp = 0;
25d834e1
JB
208 if (vif) {
209 sdata = vif_to_sdata(vif);
bda3933a 210 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
211 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
212 erp = rate->flags & IEEE80211_RATE_ERP_G;
213 }
c2d1560a
JB
214
215 /* CTS duration */
8318d78a 216 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
c2d1560a
JB
217 erp, short_preamble);
218 /* Data frame duration */
8318d78a 219 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
c2d1560a
JB
220 erp, short_preamble);
221 /* ACK duration */
8318d78a 222 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
c2d1560a
JB
223 erp, short_preamble);
224
225 return cpu_to_le16(dur);
226}
227EXPORT_SYMBOL(ieee80211_rts_duration);
228
32bfd35d
JB
229__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
230 struct ieee80211_vif *vif,
c2d1560a 231 size_t frame_len,
e039fa4a 232 const struct ieee80211_tx_info *frame_txctl)
c2d1560a
JB
233{
234 struct ieee80211_local *local = hw_to_local(hw);
235 struct ieee80211_rate *rate;
25d834e1 236 struct ieee80211_sub_if_data *sdata;
471b3efd 237 bool short_preamble;
c2d1560a
JB
238 int erp;
239 u16 dur;
2e92e6f2
JB
240 struct ieee80211_supported_band *sband;
241
242 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
c2d1560a 243
25d834e1 244 short_preamble = false;
7e9ed188 245
e039fa4a 246 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
8318d78a 247 erp = 0;
25d834e1
JB
248 if (vif) {
249 sdata = vif_to_sdata(vif);
bda3933a 250 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
251 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
252 erp = rate->flags & IEEE80211_RATE_ERP_G;
253 }
c2d1560a
JB
254
255 /* Data frame duration */
8318d78a 256 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
c2d1560a 257 erp, short_preamble);
e039fa4a 258 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
c2d1560a 259 /* ACK duration */
8318d78a 260 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
c2d1560a
JB
261 erp, short_preamble);
262 }
263
264 return cpu_to_le16(dur);
265}
266EXPORT_SYMBOL(ieee80211_ctstoself_duration);
267
ce7c9111
KV
268static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
269 enum queue_stop_reason reason)
c2d1560a
JB
270{
271 struct ieee80211_local *local = hw_to_local(hw);
272
e4e72fb4
JB
273 if (WARN_ON(queue >= hw->queues))
274 return;
ce7c9111 275
96f5e66e
JB
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
3b8d81e0
JB
282 if (!skb_queue_empty(&local->pending[queue]))
283 tasklet_schedule(&local->tx_pending_tasklet);
c2d1560a 284}
ce7c9111 285
96f5e66e
JB
286void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
287 enum queue_stop_reason reason)
ce7c9111
KV
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
297void 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}
c2d1560a
JB
302EXPORT_SYMBOL(ieee80211_wake_queue);
303
ce7c9111
KV
304static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
305 enum queue_stop_reason reason)
c2d1560a
JB
306{
307 struct ieee80211_local *local = hw_to_local(hw);
308
e4e72fb4
JB
309 if (WARN_ON(queue >= hw->queues))
310 return;
96f5e66e 311
2a577d98 312 __set_bit(reason, &local->queue_stop_reasons[queue]);
c2d1560a 313}
ce7c9111 314
96f5e66e
JB
315void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
316 enum queue_stop_reason reason)
ce7c9111
KV
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
326void 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}
c2d1560a
JB
331EXPORT_SYMBOL(ieee80211_stop_queue);
332
8f77f384
JB
333void 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);
a7bc376c
JB
339 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
340
341 if (WARN_ON(!info->control.vif)) {
0819663d 342 kfree_skb(skb);
a7bc376c
JB
343 return;
344 }
8f77f384
JB
345
346 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
347 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
3b8d81e0 348 __skb_queue_tail(&local->pending[queue], skb);
8f77f384
JB
349 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
350 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
351}
352
353int ieee80211_add_pending_skbs(struct ieee80211_local *local,
354 struct sk_buff_head *skbs)
355{
356 struct ieee80211_hw *hw = &local->hw;
357 struct sk_buff *skb;
358 unsigned long flags;
359 int queue, ret = 0, i;
360
361 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
362 for (i = 0; i < hw->queues; i++)
363 __ieee80211_stop_queue(hw, i,
364 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
365
366 while ((skb = skb_dequeue(skbs))) {
a7bc376c
JB
367 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
368
369 if (WARN_ON(!info->control.vif)) {
0819663d 370 kfree_skb(skb);
a7bc376c
JB
371 continue;
372 }
373
8f77f384
JB
374 ret++;
375 queue = skb_get_queue_mapping(skb);
3b8d81e0 376 __skb_queue_tail(&local->pending[queue], skb);
8f77f384
JB
377 }
378
3b8d81e0 379 for (i = 0; i < hw->queues; i++)
8f77f384
JB
380 __ieee80211_wake_queue(hw, i,
381 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
8f77f384
JB
382 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
383
384 return ret;
385}
386
ce7c9111
KV
387void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
388 enum queue_stop_reason reason)
c2d1560a 389{
ce7c9111
KV
390 struct ieee80211_local *local = hw_to_local(hw);
391 unsigned long flags;
c2d1560a
JB
392 int i;
393
ce7c9111
KV
394 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
395
96f5e66e 396 for (i = 0; i < hw->queues; i++)
ce7c9111
KV
397 __ieee80211_stop_queue(hw, i, reason);
398
399 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
400}
401
402void ieee80211_stop_queues(struct ieee80211_hw *hw)
403{
404 ieee80211_stop_queues_by_reason(hw,
405 IEEE80211_QUEUE_STOP_REASON_DRIVER);
c2d1560a
JB
406}
407EXPORT_SYMBOL(ieee80211_stop_queues);
408
92ab8535
TW
409int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
410{
411 struct ieee80211_local *local = hw_to_local(hw);
3b8d81e0
JB
412 unsigned long flags;
413 int ret;
96f5e66e 414
e4e72fb4
JB
415 if (WARN_ON(queue >= hw->queues))
416 return true;
96f5e66e 417
3b8d81e0
JB
418 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
419 ret = !!local->queue_stop_reasons[queue];
420 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
421 return ret;
92ab8535
TW
422}
423EXPORT_SYMBOL(ieee80211_queue_stopped);
424
ce7c9111
KV
425void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
426 enum queue_stop_reason reason)
c2d1560a 427{
ce7c9111
KV
428 struct ieee80211_local *local = hw_to_local(hw);
429 unsigned long flags;
c2d1560a
JB
430 int i;
431
ce7c9111
KV
432 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
433
e4e72fb4 434 for (i = 0; i < hw->queues; i++)
ce7c9111
KV
435 __ieee80211_wake_queue(hw, i, reason);
436
437 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
438}
439
440void ieee80211_wake_queues(struct ieee80211_hw *hw)
441{
442 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
c2d1560a
JB
443}
444EXPORT_SYMBOL(ieee80211_wake_queues);
dabeb344 445
32bfd35d
JB
446void ieee80211_iterate_active_interfaces(
447 struct ieee80211_hw *hw,
448 void (*iterator)(void *data, u8 *mac,
449 struct ieee80211_vif *vif),
450 void *data)
dabeb344
JB
451{
452 struct ieee80211_local *local = hw_to_local(hw);
453 struct ieee80211_sub_if_data *sdata;
454
c771c9d8 455 mutex_lock(&local->iflist_mtx);
2f561feb
ID
456
457 list_for_each_entry(sdata, &local->interfaces, list) {
458 switch (sdata->vif.type) {
05c914fe
JB
459 case __NL80211_IFTYPE_AFTER_LAST:
460 case NL80211_IFTYPE_UNSPECIFIED:
461 case NL80211_IFTYPE_MONITOR:
462 case NL80211_IFTYPE_AP_VLAN:
2f561feb 463 continue;
05c914fe
JB
464 case NL80211_IFTYPE_AP:
465 case NL80211_IFTYPE_STATION:
466 case NL80211_IFTYPE_ADHOC:
467 case NL80211_IFTYPE_WDS:
468 case NL80211_IFTYPE_MESH_POINT:
2f561feb
ID
469 break;
470 }
9607e6b6 471 if (ieee80211_sdata_running(sdata))
47846c9b 472 iterator(data, sdata->vif.addr,
2f561feb
ID
473 &sdata->vif);
474 }
475
c771c9d8 476 mutex_unlock(&local->iflist_mtx);
2f561feb
ID
477}
478EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
479
480void ieee80211_iterate_active_interfaces_atomic(
481 struct ieee80211_hw *hw,
482 void (*iterator)(void *data, u8 *mac,
483 struct ieee80211_vif *vif),
484 void *data)
485{
486 struct ieee80211_local *local = hw_to_local(hw);
487 struct ieee80211_sub_if_data *sdata;
488
e38bad47 489 rcu_read_lock();
dabeb344 490
e38bad47 491 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
51fb61e7 492 switch (sdata->vif.type) {
05c914fe
JB
493 case __NL80211_IFTYPE_AFTER_LAST:
494 case NL80211_IFTYPE_UNSPECIFIED:
495 case NL80211_IFTYPE_MONITOR:
496 case NL80211_IFTYPE_AP_VLAN:
dabeb344 497 continue;
05c914fe
JB
498 case NL80211_IFTYPE_AP:
499 case NL80211_IFTYPE_STATION:
500 case NL80211_IFTYPE_ADHOC:
501 case NL80211_IFTYPE_WDS:
502 case NL80211_IFTYPE_MESH_POINT:
dabeb344
JB
503 break;
504 }
9607e6b6 505 if (ieee80211_sdata_running(sdata))
47846c9b 506 iterator(data, sdata->vif.addr,
32bfd35d 507 &sdata->vif);
dabeb344 508 }
e38bad47
JB
509
510 rcu_read_unlock();
dabeb344 511}
2f561feb 512EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
37ffc8da 513
42935eca
LR
514/*
515 * Nothing should have been stuffed into the workqueue during
516 * the suspend->resume cycle. If this WARN is seen then there
517 * is a bug with either the driver suspend or something in
518 * mac80211 stuffing into the workqueue which we haven't yet
519 * cleared during mac80211's suspend cycle.
520 */
521static bool ieee80211_can_queue_work(struct ieee80211_local *local)
522{
ceb99fe0
JB
523 if (WARN(local->suspended && !local->resuming,
524 "queueing ieee80211 work while going to suspend\n"))
525 return false;
42935eca
LR
526
527 return true;
528}
529
530void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
531{
532 struct ieee80211_local *local = hw_to_local(hw);
533
534 if (!ieee80211_can_queue_work(local))
535 return;
536
537 queue_work(local->workqueue, work);
538}
539EXPORT_SYMBOL(ieee80211_queue_work);
540
541void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
542 struct delayed_work *dwork,
543 unsigned long delay)
544{
545 struct ieee80211_local *local = hw_to_local(hw);
546
547 if (!ieee80211_can_queue_work(local))
548 return;
549
550 queue_delayed_work(local->workqueue, dwork, delay);
551}
552EXPORT_SYMBOL(ieee80211_queue_delayed_work);
553
37ffc8da
JB
554void ieee802_11_parse_elems(u8 *start, size_t len,
555 struct ieee802_11_elems *elems)
d91f36db
JB
556{
557 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
558}
559
560u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
561 struct ieee802_11_elems *elems,
562 u64 filter, u32 crc)
37ffc8da
JB
563{
564 size_t left = len;
565 u8 *pos = start;
d91f36db 566 bool calc_crc = filter != 0;
37ffc8da
JB
567
568 memset(elems, 0, sizeof(*elems));
569 elems->ie_start = start;
570 elems->total_len = len;
571
572 while (left >= 2) {
573 u8 id, elen;
574
575 id = *pos++;
576 elen = *pos++;
577 left -= 2;
578
579 if (elen > left)
d91f36db
JB
580 break;
581
1814077f 582 if (calc_crc && id < 64 && (filter & (1ULL << id)))
d91f36db 583 crc = crc32_be(crc, pos - 2, elen + 2);
37ffc8da
JB
584
585 switch (id) {
586 case WLAN_EID_SSID:
587 elems->ssid = pos;
588 elems->ssid_len = elen;
589 break;
590 case WLAN_EID_SUPP_RATES:
591 elems->supp_rates = pos;
592 elems->supp_rates_len = elen;
593 break;
594 case WLAN_EID_FH_PARAMS:
595 elems->fh_params = pos;
596 elems->fh_params_len = elen;
597 break;
598 case WLAN_EID_DS_PARAMS:
599 elems->ds_params = pos;
600 elems->ds_params_len = elen;
601 break;
602 case WLAN_EID_CF_PARAMS:
603 elems->cf_params = pos;
604 elems->cf_params_len = elen;
605 break;
606 case WLAN_EID_TIM:
e7ec86f5
JB
607 if (elen >= sizeof(struct ieee80211_tim_ie)) {
608 elems->tim = (void *)pos;
609 elems->tim_len = elen;
610 }
37ffc8da
JB
611 break;
612 case WLAN_EID_IBSS_PARAMS:
613 elems->ibss_params = pos;
614 elems->ibss_params_len = elen;
615 break;
616 case WLAN_EID_CHALLENGE:
617 elems->challenge = pos;
618 elems->challenge_len = elen;
619 break;
d91f36db 620 case WLAN_EID_VENDOR_SPECIFIC:
37ffc8da
JB
621 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
622 pos[2] == 0xf2) {
623 /* Microsoft OUI (00:50:F2) */
d91f36db
JB
624
625 if (calc_crc)
626 crc = crc32_be(crc, pos - 2, elen + 2);
627
37ffc8da
JB
628 if (pos[3] == 1) {
629 /* OUI Type 1 - WPA IE */
630 elems->wpa = pos;
631 elems->wpa_len = elen;
632 } else if (elen >= 5 && pos[3] == 2) {
d91f36db 633 /* OUI Type 2 - WMM IE */
37ffc8da
JB
634 if (pos[4] == 0) {
635 elems->wmm_info = pos;
636 elems->wmm_info_len = elen;
637 } else if (pos[4] == 1) {
638 elems->wmm_param = pos;
639 elems->wmm_param_len = elen;
640 }
641 }
642 }
643 break;
644 case WLAN_EID_RSN:
645 elems->rsn = pos;
646 elems->rsn_len = elen;
647 break;
648 case WLAN_EID_ERP_INFO:
649 elems->erp_info = pos;
650 elems->erp_info_len = elen;
651 break;
652 case WLAN_EID_EXT_SUPP_RATES:
653 elems->ext_supp_rates = pos;
654 elems->ext_supp_rates_len = elen;
655 break;
656 case WLAN_EID_HT_CAPABILITY:
09914813
JB
657 if (elen >= sizeof(struct ieee80211_ht_cap))
658 elems->ht_cap_elem = (void *)pos;
37ffc8da 659 break;
d9fe60de
JB
660 case WLAN_EID_HT_INFORMATION:
661 if (elen >= sizeof(struct ieee80211_ht_info))
09914813 662 elems->ht_info_elem = (void *)pos;
37ffc8da
JB
663 break;
664 case WLAN_EID_MESH_ID:
665 elems->mesh_id = pos;
666 elems->mesh_id_len = elen;
667 break;
668 case WLAN_EID_MESH_CONFIG:
136cfa28
RP
669 if (elen >= sizeof(struct ieee80211_meshconf_ie))
670 elems->mesh_config = (void *)pos;
37ffc8da
JB
671 break;
672 case WLAN_EID_PEER_LINK:
673 elems->peer_link = pos;
674 elems->peer_link_len = elen;
675 break;
676 case WLAN_EID_PREQ:
677 elems->preq = pos;
678 elems->preq_len = elen;
679 break;
680 case WLAN_EID_PREP:
681 elems->prep = pos;
682 elems->prep_len = elen;
683 break;
684 case WLAN_EID_PERR:
685 elems->perr = pos;
686 elems->perr_len = elen;
687 break;
90a5e169
RP
688 case WLAN_EID_RANN:
689 if (elen >= sizeof(struct ieee80211_rann_ie))
690 elems->rann = (void *)pos;
691 break;
37ffc8da
JB
692 case WLAN_EID_CHANNEL_SWITCH:
693 elems->ch_switch_elem = pos;
694 elems->ch_switch_elem_len = elen;
695 break;
696 case WLAN_EID_QUIET:
697 if (!elems->quiet_elem) {
698 elems->quiet_elem = pos;
699 elems->quiet_elem_len = elen;
700 }
701 elems->num_of_quiet_elem++;
702 break;
703 case WLAN_EID_COUNTRY:
704 elems->country_elem = pos;
705 elems->country_elem_len = elen;
706 break;
707 case WLAN_EID_PWR_CONSTRAINT:
708 elems->pwr_constr_elem = pos;
709 elems->pwr_constr_elem_len = elen;
710 break;
f797eb7e
JM
711 case WLAN_EID_TIMEOUT_INTERVAL:
712 elems->timeout_int = pos;
713 elems->timeout_int_len = elen;
63a5ab82 714 break;
37ffc8da
JB
715 default:
716 break;
717 }
718
719 left -= elen;
720 pos += elen;
721 }
d91f36db
JB
722
723 return crc;
37ffc8da 724}
5825fe10
JB
725
726void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
727{
728 struct ieee80211_local *local = sdata->local;
729 struct ieee80211_tx_queue_params qparam;
aa837e1d
JB
730 int queue;
731 bool use_11b;
732 int aCWmin, aCWmax;
5825fe10
JB
733
734 if (!local->ops->conf_tx)
735 return;
736
737 memset(&qparam, 0, sizeof(qparam));
738
aa837e1d
JB
739 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
740 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
5825fe10 741
aa837e1d
JB
742 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
743 /* Set defaults according to 802.11-2007 Table 7-37 */
744 aCWmax = 1023;
745 if (use_11b)
746 aCWmin = 31;
747 else
748 aCWmin = 15;
749
750 switch (queue) {
751 case 3: /* AC_BK */
7ba10a8e
JB
752 qparam.cw_max = aCWmax;
753 qparam.cw_min = aCWmin;
aa837e1d
JB
754 qparam.txop = 0;
755 qparam.aifs = 7;
756 break;
757 default: /* never happens but let's not leave undefined */
758 case 2: /* AC_BE */
7ba10a8e
JB
759 qparam.cw_max = aCWmax;
760 qparam.cw_min = aCWmin;
aa837e1d
JB
761 qparam.txop = 0;
762 qparam.aifs = 3;
763 break;
764 case 1: /* AC_VI */
765 qparam.cw_max = aCWmin;
766 qparam.cw_min = (aCWmin + 1) / 2 - 1;
767 if (use_11b)
768 qparam.txop = 6016/32;
769 else
770 qparam.txop = 3008/32;
771 qparam.aifs = 2;
772 break;
773 case 0: /* AC_VO */
774 qparam.cw_max = (aCWmin + 1) / 2 - 1;
775 qparam.cw_min = (aCWmin + 1) / 4 - 1;
776 if (use_11b)
777 qparam.txop = 3264/32;
778 else
779 qparam.txop = 1504/32;
780 qparam.aifs = 2;
781 break;
782 }
5825fe10 783
aa837e1d
JB
784 drv_conf_tx(local, queue, &qparam);
785 }
5825fe10 786}
e50db65c 787
46900298
JB
788void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
789 const size_t supp_rates_len,
790 const u8 *supp_rates)
791{
792 struct ieee80211_local *local = sdata->local;
793 int i, have_higher_than_11mbit = 0;
794
795 /* cf. IEEE 802.11 9.2.12 */
796 for (i = 0; i < supp_rates_len; i++)
797 if ((supp_rates[i] & 0x7f) * 5 > 110)
798 have_higher_than_11mbit = 1;
799
800 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
801 have_higher_than_11mbit)
802 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
803 else
804 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
805
806 ieee80211_set_wmm_default(sdata);
807}
808
881d948c 809u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
96dd22ac
JB
810 enum ieee80211_band band)
811{
812 struct ieee80211_supported_band *sband;
813 struct ieee80211_rate *bitrates;
881d948c 814 u32 mandatory_rates;
96dd22ac
JB
815 enum ieee80211_rate_flags mandatory_flag;
816 int i;
817
818 sband = local->hw.wiphy->bands[band];
819 if (!sband) {
820 WARN_ON(1);
821 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
822 }
823
824 if (band == IEEE80211_BAND_2GHZ)
825 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
826 else
827 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
828
829 bitrates = sband->bitrates;
830 mandatory_rates = 0;
831 for (i = 0; i < sband->n_bitrates; i++)
832 if (bitrates[i].flags & mandatory_flag)
833 mandatory_rates |= BIT(i);
834 return mandatory_rates;
835}
46900298
JB
836
837void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
838 u16 transaction, u16 auth_alg,
fffd0934
JB
839 u8 *extra, size_t extra_len, const u8 *bssid,
840 const u8 *key, u8 key_len, u8 key_idx)
46900298
JB
841{
842 struct ieee80211_local *local = sdata->local;
843 struct sk_buff *skb;
844 struct ieee80211_mgmt *mgmt;
fffd0934 845 int err;
46900298
JB
846
847 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
65fc73ac 848 sizeof(*mgmt) + 6 + extra_len);
46900298
JB
849 if (!skb) {
850 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
47846c9b 851 "frame\n", sdata->name);
46900298
JB
852 return;
853 }
854 skb_reserve(skb, local->hw.extra_tx_headroom);
855
856 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
857 memset(mgmt, 0, 24 + 6);
858 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
859 IEEE80211_STYPE_AUTH);
46900298 860 memcpy(mgmt->da, bssid, ETH_ALEN);
47846c9b 861 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298
JB
862 memcpy(mgmt->bssid, bssid, ETH_ALEN);
863 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
864 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
865 mgmt->u.auth.status_code = cpu_to_le16(0);
866 if (extra)
867 memcpy(skb_put(skb, extra_len), extra, extra_len);
46900298 868
fffd0934
JB
869 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
870 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
871 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
872 WARN_ON(err);
873 }
874
62ae67be
JB
875 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
876 ieee80211_tx_skb(sdata, skb);
46900298
JB
877}
878
de95a54b 879int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
4d36ec58
JB
880 const u8 *ie, size_t ie_len,
881 enum ieee80211_band band)
de95a54b
JB
882{
883 struct ieee80211_supported_band *sband;
8e664fb3
JB
884 u8 *pos;
885 size_t offset = 0, noffset;
886 int supp_rates_len, i;
de95a54b 887
4d36ec58 888 sband = local->hw.wiphy->bands[band];
de95a54b
JB
889
890 pos = buffer;
891
8e664fb3
JB
892 supp_rates_len = min_t(int, sband->n_bitrates, 8);
893
de95a54b 894 *pos++ = WLAN_EID_SUPP_RATES;
8e664fb3 895 *pos++ = supp_rates_len;
de95a54b 896
8e664fb3
JB
897 for (i = 0; i < supp_rates_len; i++) {
898 int rate = sband->bitrates[i].bitrate;
899 *pos++ = (u8) (rate / 5);
900 }
901
902 /* insert "request information" if in custom IEs */
903 if (ie && ie_len) {
904 static const u8 before_extrates[] = {
905 WLAN_EID_SSID,
906 WLAN_EID_SUPP_RATES,
907 WLAN_EID_REQUEST,
908 };
909 noffset = ieee80211_ie_split(ie, ie_len,
910 before_extrates,
911 ARRAY_SIZE(before_extrates),
912 offset);
913 memcpy(pos, ie + offset, noffset - offset);
914 pos += noffset - offset;
915 offset = noffset;
916 }
917
918 if (sband->n_bitrates > i) {
919 *pos++ = WLAN_EID_EXT_SUPP_RATES;
920 *pos++ = sband->n_bitrates - i;
921
922 for (; i < sband->n_bitrates; i++) {
923 int rate = sband->bitrates[i].bitrate;
924 *pos++ = (u8) (rate / 5);
925 }
926 }
927
928 /* insert custom IEs that go before HT */
929 if (ie && ie_len) {
930 static const u8 before_ht[] = {
931 WLAN_EID_SSID,
932 WLAN_EID_SUPP_RATES,
933 WLAN_EID_REQUEST,
934 WLAN_EID_EXT_SUPP_RATES,
935 WLAN_EID_DS_PARAMS,
936 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
937 };
938 noffset = ieee80211_ie_split(ie, ie_len,
939 before_ht, ARRAY_SIZE(before_ht),
940 offset);
941 memcpy(pos, ie + offset, noffset - offset);
942 pos += noffset - offset;
943 offset = noffset;
de95a54b
JB
944 }
945
5ef2d41a 946 if (sband->ht_cap.ht_supported) {
f38fd12f
JB
947 u16 cap = sband->ht_cap.cap;
948 __le16 tmp;
949
950 if (ieee80211_disable_40mhz_24ghz &&
951 sband->band == IEEE80211_BAND_2GHZ) {
952 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
953 cap &= ~IEEE80211_HT_CAP_SGI_40;
954 }
5ef2d41a
JB
955
956 *pos++ = WLAN_EID_HT_CAPABILITY;
957 *pos++ = sizeof(struct ieee80211_ht_cap);
958 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
f38fd12f 959 tmp = cpu_to_le16(cap);
5ef2d41a
JB
960 memcpy(pos, &tmp, sizeof(u16));
961 pos += sizeof(u16);
5ef2d41a 962 *pos++ = sband->ht_cap.ampdu_factor |
f38fd12f
JB
963 (sband->ht_cap.ampdu_density <<
964 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
5ef2d41a
JB
965 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
966 pos += sizeof(sband->ht_cap.mcs);
967 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
968 }
969
de95a54b
JB
970 /*
971 * If adding more here, adjust code in main.c
972 * that calculates local->scan_ies_len.
973 */
974
8e664fb3
JB
975 /* add any remaining custom IEs */
976 if (ie && ie_len) {
977 noffset = ie_len;
978 memcpy(pos, ie + offset, noffset - offset);
979 pos += noffset - offset;
de95a54b
JB
980 }
981
982 return pos - buffer;
983}
984
46900298 985void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
de95a54b
JB
986 const u8 *ssid, size_t ssid_len,
987 const u8 *ie, size_t ie_len)
46900298
JB
988{
989 struct ieee80211_local *local = sdata->local;
46900298
JB
990 struct sk_buff *skb;
991 struct ieee80211_mgmt *mgmt;
de95a54b 992 u8 *pos;
46900298
JB
993
994 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
65fc73ac 995 ie_len);
46900298
JB
996 if (!skb) {
997 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
47846c9b 998 "request\n", sdata->name);
46900298
JB
999 return;
1000 }
1001 skb_reserve(skb, local->hw.extra_tx_headroom);
1002
1003 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1004 memset(mgmt, 0, 24);
1005 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1006 IEEE80211_STYPE_PROBE_REQ);
47846c9b 1007 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298
JB
1008 if (dst) {
1009 memcpy(mgmt->da, dst, ETH_ALEN);
1010 memcpy(mgmt->bssid, dst, ETH_ALEN);
1011 } else {
1012 memset(mgmt->da, 0xff, ETH_ALEN);
1013 memset(mgmt->bssid, 0xff, ETH_ALEN);
1014 }
1015 pos = skb_put(skb, 2 + ssid_len);
1016 *pos++ = WLAN_EID_SSID;
1017 *pos++ = ssid_len;
1018 memcpy(pos, ssid, ssid_len);
de95a54b 1019 pos += ssid_len;
46900298 1020
4d36ec58
JB
1021 skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len,
1022 local->hw.conf.channel->band));
46900298 1023
62ae67be
JB
1024 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1025 ieee80211_tx_skb(sdata, skb);
46900298
JB
1026}
1027
1028u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
1029 struct ieee802_11_elems *elems,
1030 enum ieee80211_band band)
1031{
1032 struct ieee80211_supported_band *sband;
1033 struct ieee80211_rate *bitrates;
1034 size_t num_rates;
1035 u32 supp_rates;
1036 int i, j;
1037 sband = local->hw.wiphy->bands[band];
1038
1039 if (!sband) {
1040 WARN_ON(1);
1041 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1042 }
1043
1044 bitrates = sband->bitrates;
1045 num_rates = sband->n_bitrates;
1046 supp_rates = 0;
1047 for (i = 0; i < elems->supp_rates_len +
1048 elems->ext_supp_rates_len; i++) {
1049 u8 rate = 0;
1050 int own_rate;
1051 if (i < elems->supp_rates_len)
1052 rate = elems->supp_rates[i];
1053 else if (elems->ext_supp_rates)
1054 rate = elems->ext_supp_rates
1055 [i - elems->supp_rates_len];
1056 own_rate = 5 * (rate & 0x7f);
1057 for (j = 0; j < num_rates; j++)
1058 if (bitrates[j].bitrate == own_rate)
1059 supp_rates |= BIT(j);
1060 }
1061 return supp_rates;
1062}
f2753ddb 1063
84f6a01c
JB
1064void ieee80211_stop_device(struct ieee80211_local *local)
1065{
1066 ieee80211_led_radio(local, false);
1067
1068 cancel_work_sync(&local->reconfig_filter);
1069 drv_stop(local);
1070
1071 flush_workqueue(local->workqueue);
1072}
1073
f2753ddb
JB
1074int ieee80211_reconfig(struct ieee80211_local *local)
1075{
1076 struct ieee80211_hw *hw = &local->hw;
1077 struct ieee80211_sub_if_data *sdata;
1078 struct ieee80211_if_init_conf conf;
1079 struct sta_info *sta;
1080 unsigned long flags;
1081 int res;
5bb644a0 1082
ceb99fe0
JB
1083 if (local->suspended)
1084 local->resuming = true;
f2753ddb
JB
1085
1086 /* restart hardware */
1087 if (local->open_count) {
24487981 1088 res = drv_start(local);
f2753ddb 1089
1f87f7d3 1090 ieee80211_led_radio(local, true);
f2753ddb
JB
1091 }
1092
1093 /* add interfaces */
1094 list_for_each_entry(sdata, &local->interfaces, list) {
1095 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1096 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
9607e6b6 1097 ieee80211_sdata_running(sdata)) {
f2753ddb
JB
1098 conf.vif = &sdata->vif;
1099 conf.type = sdata->vif.type;
47846c9b 1100 conf.mac_addr = sdata->vif.addr;
24487981 1101 res = drv_add_interface(local, &conf);
f2753ddb
JB
1102 }
1103 }
1104
1105 /* add STAs back */
1106 if (local->ops->sta_notify) {
1107 spin_lock_irqsave(&local->sta_lock, flags);
1108 list_for_each_entry(sta, &local->sta_list, list) {
5bb644a0 1109 sdata = sta->sdata;
f2753ddb
JB
1110 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1111 sdata = container_of(sdata->bss,
1112 struct ieee80211_sub_if_data,
1113 u.ap);
1114
12375ef9 1115 drv_sta_notify(local, sdata, STA_NOTIFY_ADD,
24487981 1116 &sta->sta);
f2753ddb
JB
1117 }
1118 spin_unlock_irqrestore(&local->sta_lock, flags);
1119 }
1120
1121 /* Clear Suspend state so that ADDBA requests can be processed */
1122
1123 rcu_read_lock();
1124
1125 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1126 list_for_each_entry_rcu(sta, &local->sta_list, list) {
1127 clear_sta_flags(sta, WLAN_STA_SUSPEND);
1128 }
1129 }
1130
1131 rcu_read_unlock();
1132
1133 /* setup RTS threshold */
24487981 1134 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
f2753ddb
JB
1135
1136 /* reconfigure hardware */
1137 ieee80211_hw_config(local, ~0);
1138
f2753ddb 1139 ieee80211_configure_filter(local);
f2753ddb
JB
1140
1141 /* Finally also reconfigure all the BSS information */
1142 list_for_each_entry(sdata, &local->interfaces, list) {
1143 u32 changed = ~0;
9607e6b6 1144 if (!ieee80211_sdata_running(sdata))
f2753ddb
JB
1145 continue;
1146 switch (sdata->vif.type) {
1147 case NL80211_IFTYPE_STATION:
1148 /* disable beacon change bits */
2d0ddec5
JB
1149 changed &= ~(BSS_CHANGED_BEACON |
1150 BSS_CHANGED_BEACON_ENABLED);
f2753ddb
JB
1151 /* fall through */
1152 case NL80211_IFTYPE_ADHOC:
1153 case NL80211_IFTYPE_AP:
1154 case NL80211_IFTYPE_MESH_POINT:
2d0ddec5 1155 ieee80211_bss_info_change_notify(sdata, changed);
f2753ddb
JB
1156 break;
1157 case NL80211_IFTYPE_WDS:
1158 break;
1159 case NL80211_IFTYPE_AP_VLAN:
1160 case NL80211_IFTYPE_MONITOR:
1161 /* ignore virtual */
1162 break;
1163 case NL80211_IFTYPE_UNSPECIFIED:
1164 case __NL80211_IFTYPE_AFTER_LAST:
1165 WARN_ON(1);
1166 break;
1167 }
1168 }
1169
1170 /* add back keys */
1171 list_for_each_entry(sdata, &local->interfaces, list)
9607e6b6 1172 if (ieee80211_sdata_running(sdata))
f2753ddb
JB
1173 ieee80211_enable_keys(sdata);
1174
1175 ieee80211_wake_queues_by_reason(hw,
1176 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1177
5bb644a0
JB
1178 /*
1179 * If this is for hw restart things are still running.
1180 * We may want to change that later, however.
1181 */
ceb99fe0 1182 if (!local->suspended)
5bb644a0
JB
1183 return 0;
1184
1185#ifdef CONFIG_PM
ceb99fe0 1186 /* first set suspended false, then resuming */
5bb644a0 1187 local->suspended = false;
ceb99fe0
JB
1188 mb();
1189 local->resuming = false;
5bb644a0
JB
1190
1191 list_for_each_entry(sdata, &local->interfaces, list) {
1192 switch(sdata->vif.type) {
1193 case NL80211_IFTYPE_STATION:
1194 ieee80211_sta_restart(sdata);
1195 break;
1196 case NL80211_IFTYPE_ADHOC:
1197 ieee80211_ibss_restart(sdata);
1198 break;
1199 case NL80211_IFTYPE_MESH_POINT:
1200 ieee80211_mesh_restart(sdata);
1201 break;
1202 default:
1203 break;
1204 }
1205 }
1206
1207 add_timer(&local->sta_cleanup);
1208
1209 spin_lock_irqsave(&local->sta_lock, flags);
1210 list_for_each_entry(sta, &local->sta_list, list)
1211 mesh_plink_restart(sta);
1212 spin_unlock_irqrestore(&local->sta_lock, flags);
1213#else
1214 WARN_ON(1);
1215#endif
f2753ddb
JB
1216 return 0;
1217}
42935eca 1218
0f78231b
JB
1219static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1220 enum ieee80211_smps_mode *smps_mode)
1221{
1222 if (ifmgd->associated) {
1223 *smps_mode = ifmgd->ap_smps;
1224
1225 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1226 if (ifmgd->powersave)
1227 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1228 else
1229 *smps_mode = IEEE80211_SMPS_OFF;
1230 }
1231
1232 return 1;
1233 }
1234
1235 return 0;
1236}
1237
1238/* must hold iflist_mtx */
1239void ieee80211_recalc_smps(struct ieee80211_local *local,
1240 struct ieee80211_sub_if_data *forsdata)
1241{
1242 struct ieee80211_sub_if_data *sdata;
1243 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1244 int count = 0;
1245
1246 if (forsdata)
1247 WARN_ON(!mutex_is_locked(&forsdata->u.mgd.mtx));
1248
1249 WARN_ON(!mutex_is_locked(&local->iflist_mtx));
1250
1251 /*
1252 * This function could be improved to handle multiple
1253 * interfaces better, but right now it makes any
1254 * non-station interfaces force SM PS to be turned
1255 * off. If there are multiple station interfaces it
1256 * could also use the best possible mode, e.g. if
1257 * one is in static and the other in dynamic then
1258 * dynamic is ok.
1259 */
1260
1261 list_for_each_entry(sdata, &local->interfaces, list) {
1262 if (!netif_running(sdata->dev))
1263 continue;
1264 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1265 goto set;
1266 if (sdata != forsdata) {
1267 /*
1268 * This nested is ok -- we are holding the iflist_mtx
1269 * so can't get here twice or so. But it's required
1270 * since normally we acquire it first and then the
1271 * iflist_mtx.
1272 */
1273 mutex_lock_nested(&sdata->u.mgd.mtx, SINGLE_DEPTH_NESTING);
1274 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1275 mutex_unlock(&sdata->u.mgd.mtx);
1276 } else
1277 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
1278
1279 if (count > 1) {
1280 smps_mode = IEEE80211_SMPS_OFF;
1281 break;
1282 }
1283 }
1284
1285 if (smps_mode == local->smps_mode)
1286 return;
1287
1288 set:
1289 local->smps_mode = smps_mode;
1290 /* changed flag is auto-detected for this */
1291 ieee80211_hw_config(local, 0);
1292}
8e664fb3
JB
1293
1294static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1295{
1296 int i;
1297
1298 for (i = 0; i < n_ids; i++)
1299 if (ids[i] == id)
1300 return true;
1301 return false;
1302}
1303
1304/**
1305 * ieee80211_ie_split - split an IE buffer according to ordering
1306 *
1307 * @ies: the IE buffer
1308 * @ielen: the length of the IE buffer
1309 * @ids: an array with element IDs that are allowed before
1310 * the split
1311 * @n_ids: the size of the element ID array
1312 * @offset: offset where to start splitting in the buffer
1313 *
1314 * This function splits an IE buffer by updating the @offset
1315 * variable to point to the location where the buffer should be
1316 * split.
1317 *
1318 * It assumes that the given IE buffer is well-formed, this
1319 * has to be guaranteed by the caller!
1320 *
1321 * It also assumes that the IEs in the buffer are ordered
1322 * correctly, if not the result of using this function will not
1323 * be ordered correctly either, i.e. it does no reordering.
1324 *
1325 * The function returns the offset where the next part of the
1326 * buffer starts, which may be @ielen if the entire (remainder)
1327 * of the buffer should be used.
1328 */
1329size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1330 const u8 *ids, int n_ids, size_t offset)
1331{
1332 size_t pos = offset;
1333
1334 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1335 pos += 2 + ies[pos + 1];
1336
1337 return pos;
1338}
1339
1340size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1341{
1342 size_t pos = offset;
1343
1344 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1345 pos += 2 + ies[pos + 1];
1346
1347 return pos;
1348}
This page took 0.319641 seconds and 5 git commands to generate.