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