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