blob: 0c9490722aa53f6d6bb5a19023a2157e9c997828 [file] [log] [blame]
Johannes Bergc2d15602007-07-27 15:43:23 +02001/*
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>
Johannes Bergc2d15602007-07-27 15:43:23 +020021#include <linux/bitmap.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070022#include <net/net_namespace.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020023#include <net/cfg80211.h>
Johannes Bergdabeb342007-11-09 01:57:29 +010024#include <net/rtnetlink.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020025
26#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020027#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040028#include "rate.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010029#include "mesh.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020030#include "wme.h"
Johannes Bergf2753dd2009-04-14 10:09:24 +020031#include "led.h"
Johannes Bergfffd0932009-07-08 14:22:54 +020032#include "wep.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020033
34/* privid for wiphys to determine whether they belong to us or not */
35void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
36
Luis R. Rodriguez9a953712009-01-22 15:05:53 -080037struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
38{
39 struct ieee80211_local *local;
40 BUG_ON(!wiphy);
41
42 local = wiphy_priv(wiphy);
43 return &local->hw;
44}
45EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
Johannes Bergc2d15602007-07-27 15:43:23 +020046
Ron Rindjunsky71364712007-12-25 17:00:36 +020047u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020048 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020049{
Harvey Harrisona494bb12008-06-11 14:21:58 -070050 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020051
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020052 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
53 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020054 return NULL;
55
Harvey Harrisona494bb12008-06-11 14:21:58 -070056 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020057 if (len < 24) /* drop incorrect hdr len (data) */
58 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070059
60 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020061 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070062 if (ieee80211_has_tods(fc))
63 return hdr->addr1;
64 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020065 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070066
67 return hdr->addr3;
68 }
69
70 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020071 if (len < 24) /* drop incorrect hdr len (mgmt) */
72 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020073 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070074 }
75
76 if (ieee80211_is_ctl(fc)) {
77 if(ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020078 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070079
80 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020081 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020082 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020083 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020084 case NL80211_IFTYPE_AP:
85 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020086 return hdr->addr1;
87 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070088 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020089 }
90 }
Johannes Bergc2d15602007-07-27 15:43:23 +020091 }
92
93 return NULL;
94}
95
Johannes Berg5cf121c2008-02-25 16:27:43 +010096void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +020097{
Johannes Berg2de8e0d2009-03-23 17:28:35 +010098 struct sk_buff *skb = tx->skb;
99 struct ieee80211_hdr *hdr;
Johannes Bergc2d15602007-07-27 15:43:23 +0200100
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100101 do {
102 hdr = (struct ieee80211_hdr *) skb->data;
103 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
104 } while ((skb = skb->next));
Johannes Bergc2d15602007-07-27 15:43:23 +0200105}
106
107int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
108 int rate, int erp, int short_preamble)
109{
110 int dur;
111
112 /* calculate duration (in microseconds, rounded up to next higher
113 * integer if it includes a fractional microsecond) to send frame of
114 * len bytes (does not include FCS) at the given rate. Duration will
115 * also include SIFS.
116 *
117 * rate is in 100 kbps, so divident is multiplied by 10 in the
118 * DIV_ROUND_UP() operations.
119 */
120
Johannes Berg8318d782008-01-24 19:38:38 +0100121 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200122 /*
123 * OFDM:
124 *
125 * N_DBPS = DATARATE x 4
126 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
127 * (16 = SIGNAL time, 6 = tail bits)
128 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
129 *
130 * T_SYM = 4 usec
131 * 802.11a - 17.5.2: aSIFSTime = 16 usec
132 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
133 * signal ext = 6 usec
134 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200135 dur = 16; /* SIFS + signal ext */
136 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
137 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
138 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
139 4 * rate); /* T_SYM x N_SYM */
140 } else {
141 /*
142 * 802.11b or 802.11g with 802.11b compatibility:
143 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
144 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
145 *
146 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
147 * aSIFSTime = 10 usec
148 * aPreambleLength = 144 usec or 72 usec with short preamble
149 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
150 */
151 dur = 10; /* aSIFSTime = 10 usec */
152 dur += short_preamble ? (72 + 24) : (144 + 48);
153
154 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
155 }
156
157 return dur;
158}
159
160/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100161__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
162 struct ieee80211_vif *vif,
Johannes Berg8318d782008-01-24 19:38:38 +0100163 size_t frame_len,
164 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200165{
166 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg25d834e2008-09-12 22:52:47 +0200167 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200168 u16 dur;
169 int erp;
Johannes Berg25d834e2008-09-12 22:52:47 +0200170 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200171
Johannes Berg8318d782008-01-24 19:38:38 +0100172 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200173 if (vif) {
174 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200175 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200176 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
177 erp = rate->flags & IEEE80211_RATE_ERP_G;
178 }
Johannes Berg8318d782008-01-24 19:38:38 +0100179
180 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
Johannes Berg25d834e2008-09-12 22:52:47 +0200181 short_preamble);
Johannes Bergc2d15602007-07-27 15:43:23 +0200182
183 return cpu_to_le16(dur);
184}
185EXPORT_SYMBOL(ieee80211_generic_frame_duration);
186
Johannes Berg32bfd352007-12-19 01:31:26 +0100187__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
188 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200189 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200190{
191 struct ieee80211_local *local = hw_to_local(hw);
192 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200193 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100194 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200195 int erp;
196 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200197 struct ieee80211_supported_band *sband;
198
199 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200200
Johannes Berg25d834e2008-09-12 22:52:47 +0200201 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200202
Johannes Berge039fa42008-05-15 12:55:29 +0200203 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100204
205 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200206 if (vif) {
207 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200208 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200209 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
210 erp = rate->flags & IEEE80211_RATE_ERP_G;
211 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200212
213 /* CTS duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100214 dur = ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200215 erp, short_preamble);
216 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100217 dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200218 erp, short_preamble);
219 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100220 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200221 erp, short_preamble);
222
223 return cpu_to_le16(dur);
224}
225EXPORT_SYMBOL(ieee80211_rts_duration);
226
Johannes Berg32bfd352007-12-19 01:31:26 +0100227__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
228 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200229 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200230 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200231{
232 struct ieee80211_local *local = hw_to_local(hw);
233 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200234 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100235 bool short_preamble;
Johannes Bergc2d15602007-07-27 15:43:23 +0200236 int erp;
237 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200238 struct ieee80211_supported_band *sband;
239
240 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200241
Johannes Berg25d834e2008-09-12 22:52:47 +0200242 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200243
Johannes Berge039fa42008-05-15 12:55:29 +0200244 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100245 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200246 if (vif) {
247 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200248 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200249 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
250 erp = rate->flags & IEEE80211_RATE_ERP_G;
251 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200252
253 /* Data frame duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100254 dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200255 erp, short_preamble);
Johannes Berge039fa42008-05-15 12:55:29 +0200256 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200257 /* ACK duration */
Johannes Berg8318d782008-01-24 19:38:38 +0100258 dur += ieee80211_frame_duration(local, 10, rate->bitrate,
Johannes Bergc2d15602007-07-27 15:43:23 +0200259 erp, short_preamble);
260 }
261
Johannes Bergc2d15602007-07-27 15:43:23 +0200262 return cpu_to_le16(dur);
263}
264EXPORT_SYMBOL(ieee80211_ctstoself_duration);
265
Kalle Valoce7c9112008-12-18 23:35:20 +0200266static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
267 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200268{
269 struct ieee80211_local *local = hw_to_local(hw);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100270 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200271
Johannes Bergb5878a22010-04-07 16:48:40 +0200272 trace_wake_queue(local, queue, reason);
273
Johannes Berge4e72fb2009-03-23 17:28:42 +0100274 if (WARN_ON(queue >= hw->queues))
275 return;
Kalle Valoce7c9112008-12-18 23:35:20 +0200276
Johannes Berg96f5e662009-02-12 00:51:53 +0100277 __clear_bit(reason, &local->queue_stop_reasons[queue]);
278
279 if (local->queue_stop_reasons[queue] != 0)
280 /* someone still has this queue stopped */
281 return;
282
Johannes Berg7236fe22010-03-22 13:42:43 -0700283 if (skb_queue_empty(&local->pending[queue])) {
284 rcu_read_lock();
Johannes Berg5b714c62010-08-27 13:45:28 +0200285 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
286 if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
287 continue;
Johannes Berg2337db82010-08-27 13:36:49 +0200288 netif_wake_subqueue(sdata->dev, queue);
Johannes Berg5b714c62010-08-27 13:45:28 +0200289 }
Johannes Berg7236fe22010-03-22 13:42:43 -0700290 rcu_read_unlock();
291 } else
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200292 tasklet_schedule(&local->tx_pending_tasklet);
Johannes Bergc2d15602007-07-27 15:43:23 +0200293}
Kalle Valoce7c9112008-12-18 23:35:20 +0200294
Johannes Berg96f5e662009-02-12 00:51:53 +0100295void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
296 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200297{
298 struct ieee80211_local *local = hw_to_local(hw);
299 unsigned long flags;
300
301 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
302 __ieee80211_wake_queue(hw, queue, reason);
303 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
304}
305
306void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
307{
308 ieee80211_wake_queue_by_reason(hw, queue,
309 IEEE80211_QUEUE_STOP_REASON_DRIVER);
310}
Johannes Bergc2d15602007-07-27 15:43:23 +0200311EXPORT_SYMBOL(ieee80211_wake_queue);
312
Kalle Valoce7c9112008-12-18 23:35:20 +0200313static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
314 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200315{
316 struct ieee80211_local *local = hw_to_local(hw);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100317 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200318
Johannes Bergb5878a22010-04-07 16:48:40 +0200319 trace_stop_queue(local, queue, reason);
320
Johannes Berge4e72fb2009-03-23 17:28:42 +0100321 if (WARN_ON(queue >= hw->queues))
322 return;
Johannes Berg96f5e662009-02-12 00:51:53 +0100323
Johannes Berg2a577d92009-03-23 17:28:37 +0100324 __set_bit(reason, &local->queue_stop_reasons[queue]);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100325
326 rcu_read_lock();
327 list_for_each_entry_rcu(sdata, &local->interfaces, list)
Johannes Berg2337db82010-08-27 13:36:49 +0200328 netif_stop_subqueue(sdata->dev, queue);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100329 rcu_read_unlock();
Johannes Bergc2d15602007-07-27 15:43:23 +0200330}
Kalle Valoce7c9112008-12-18 23:35:20 +0200331
Johannes Berg96f5e662009-02-12 00:51:53 +0100332void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
333 enum queue_stop_reason reason)
Kalle Valoce7c9112008-12-18 23:35:20 +0200334{
335 struct ieee80211_local *local = hw_to_local(hw);
336 unsigned long flags;
337
338 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
339 __ieee80211_stop_queue(hw, queue, reason);
340 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
341}
342
343void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
344{
345 ieee80211_stop_queue_by_reason(hw, queue,
346 IEEE80211_QUEUE_STOP_REASON_DRIVER);
347}
Johannes Bergc2d15602007-07-27 15:43:23 +0200348EXPORT_SYMBOL(ieee80211_stop_queue);
349
Johannes Berg8f77f382009-06-07 21:58:37 +0200350void ieee80211_add_pending_skb(struct ieee80211_local *local,
351 struct sk_buff *skb)
352{
353 struct ieee80211_hw *hw = &local->hw;
354 unsigned long flags;
355 int queue = skb_get_queue_mapping(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200356 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
357
358 if (WARN_ON(!info->control.vif)) {
Roel Kluin08196632009-10-06 15:52:35 +0200359 kfree_skb(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200360 return;
361 }
Johannes Berg8f77f382009-06-07 21:58:37 +0200362
363 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
364 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200365 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200366 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
367 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
368}
369
Johannes Bergb0b97a82011-09-29 16:04:30 +0200370void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
371 struct sk_buff_head *skbs,
372 void (*fn)(void *data), void *data)
Johannes Berg8f77f382009-06-07 21:58:37 +0200373{
374 struct ieee80211_hw *hw = &local->hw;
375 struct sk_buff *skb;
376 unsigned long flags;
Johannes Bergb0b97a82011-09-29 16:04:30 +0200377 int queue, i;
Johannes Berg8f77f382009-06-07 21:58:37 +0200378
379 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
380 for (i = 0; i < hw->queues; i++)
381 __ieee80211_stop_queue(hw, i,
382 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
383
384 while ((skb = skb_dequeue(skbs))) {
Johannes Berga7bc3762009-07-27 10:33:31 +0200385 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
386
387 if (WARN_ON(!info->control.vif)) {
Roel Kluin08196632009-10-06 15:52:35 +0200388 kfree_skb(skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200389 continue;
390 }
391
Johannes Berg8f77f382009-06-07 21:58:37 +0200392 queue = skb_get_queue_mapping(skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200393 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200394 }
395
Johannes Berg50a94322010-11-16 11:50:28 -0800396 if (fn)
397 fn(data);
398
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200399 for (i = 0; i < hw->queues; i++)
Johannes Berg8f77f382009-06-07 21:58:37 +0200400 __ieee80211_wake_queue(hw, i,
401 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
Johannes Berg8f77f382009-06-07 21:58:37 +0200402 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg8f77f382009-06-07 21:58:37 +0200403}
404
Johannes Bergb0b97a82011-09-29 16:04:30 +0200405void ieee80211_add_pending_skbs(struct ieee80211_local *local,
406 struct sk_buff_head *skbs)
Johannes Berg50a94322010-11-16 11:50:28 -0800407{
Johannes Bergb0b97a82011-09-29 16:04:30 +0200408 ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
Johannes Berg50a94322010-11-16 11:50:28 -0800409}
410
Kalle Valoce7c9112008-12-18 23:35:20 +0200411void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
412 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200413{
Kalle Valoce7c9112008-12-18 23:35:20 +0200414 struct ieee80211_local *local = hw_to_local(hw);
415 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200416 int i;
417
Kalle Valoce7c9112008-12-18 23:35:20 +0200418 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
419
Johannes Berg96f5e662009-02-12 00:51:53 +0100420 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200421 __ieee80211_stop_queue(hw, i, reason);
422
423 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
424}
425
426void ieee80211_stop_queues(struct ieee80211_hw *hw)
427{
428 ieee80211_stop_queues_by_reason(hw,
429 IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200430}
431EXPORT_SYMBOL(ieee80211_stop_queues);
432
Tomas Winkler92ab8532008-07-24 21:02:04 +0300433int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
434{
435 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200436 unsigned long flags;
437 int ret;
Johannes Berg96f5e662009-02-12 00:51:53 +0100438
Johannes Berge4e72fb2009-03-23 17:28:42 +0100439 if (WARN_ON(queue >= hw->queues))
440 return true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100441
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200442 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
443 ret = !!local->queue_stop_reasons[queue];
444 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
445 return ret;
Tomas Winkler92ab8532008-07-24 21:02:04 +0300446}
447EXPORT_SYMBOL(ieee80211_queue_stopped);
448
Kalle Valoce7c9112008-12-18 23:35:20 +0200449void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
450 enum queue_stop_reason reason)
Johannes Bergc2d15602007-07-27 15:43:23 +0200451{
Kalle Valoce7c9112008-12-18 23:35:20 +0200452 struct ieee80211_local *local = hw_to_local(hw);
453 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200454 int i;
455
Kalle Valoce7c9112008-12-18 23:35:20 +0200456 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
457
Johannes Berge4e72fb2009-03-23 17:28:42 +0100458 for (i = 0; i < hw->queues; i++)
Kalle Valoce7c9112008-12-18 23:35:20 +0200459 __ieee80211_wake_queue(hw, i, reason);
460
461 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
462}
463
464void ieee80211_wake_queues(struct ieee80211_hw *hw)
465{
466 ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
Johannes Bergc2d15602007-07-27 15:43:23 +0200467}
468EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100469
Johannes Berg32bfd352007-12-19 01:31:26 +0100470void ieee80211_iterate_active_interfaces(
471 struct ieee80211_hw *hw,
472 void (*iterator)(void *data, u8 *mac,
473 struct ieee80211_vif *vif),
474 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100475{
476 struct ieee80211_local *local = hw_to_local(hw);
477 struct ieee80211_sub_if_data *sdata;
478
Johannes Bergc771c9d2009-01-23 22:54:03 +0100479 mutex_lock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200480
481 list_for_each_entry(sdata, &local->interfaces, list) {
482 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200483 case NL80211_IFTYPE_MONITOR:
484 case NL80211_IFTYPE_AP_VLAN:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200485 continue;
Johannes Berg2ca27bc2010-09-16 14:58:23 +0200486 default:
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200487 break;
488 }
Johannes Berg9607e6b2009-12-23 13:15:31 +0100489 if (ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +0100490 iterator(data, sdata->vif.addr,
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200491 &sdata->vif);
492 }
493
Johannes Bergc771c9d2009-01-23 22:54:03 +0100494 mutex_unlock(&local->iflist_mtx);
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200495}
496EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
497
498void ieee80211_iterate_active_interfaces_atomic(
499 struct ieee80211_hw *hw,
500 void (*iterator)(void *data, u8 *mac,
501 struct ieee80211_vif *vif),
502 void *data)
503{
504 struct ieee80211_local *local = hw_to_local(hw);
505 struct ieee80211_sub_if_data *sdata;
506
Johannes Berge38bad42007-11-28 10:55:32 +0100507 rcu_read_lock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100508
Johannes Berge38bad42007-11-28 10:55:32 +0100509 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100510 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200511 case NL80211_IFTYPE_MONITOR:
512 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100513 continue;
Johannes Berg2ca27bc2010-09-16 14:58:23 +0200514 default:
Johannes Bergdabeb342007-11-09 01:57:29 +0100515 break;
516 }
Johannes Berg9607e6b2009-12-23 13:15:31 +0100517 if (ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +0100518 iterator(data, sdata->vif.addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100519 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100520 }
Johannes Berge38bad42007-11-28 10:55:32 +0100521
522 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100523}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200524EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200525
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400526/*
527 * Nothing should have been stuffed into the workqueue during
528 * the suspend->resume cycle. If this WARN is seen then there
529 * is a bug with either the driver suspend or something in
530 * mac80211 stuffing into the workqueue which we haven't yet
531 * cleared during mac80211's suspend cycle.
532 */
533static bool ieee80211_can_queue_work(struct ieee80211_local *local)
534{
Johannes Bergceb99fe2009-11-19 14:29:39 +0100535 if (WARN(local->suspended && !local->resuming,
536 "queueing ieee80211 work while going to suspend\n"))
537 return false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400538
539 return true;
540}
541
542void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
543{
544 struct ieee80211_local *local = hw_to_local(hw);
545
546 if (!ieee80211_can_queue_work(local))
547 return;
548
549 queue_work(local->workqueue, work);
550}
551EXPORT_SYMBOL(ieee80211_queue_work);
552
553void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
554 struct delayed_work *dwork,
555 unsigned long delay)
556{
557 struct ieee80211_local *local = hw_to_local(hw);
558
559 if (!ieee80211_can_queue_work(local))
560 return;
561
562 queue_delayed_work(local->workqueue, dwork, delay);
563}
564EXPORT_SYMBOL(ieee80211_queue_delayed_work);
565
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200566void ieee802_11_parse_elems(u8 *start, size_t len,
567 struct ieee802_11_elems *elems)
568{
Johannes Bergd91f36d2009-04-16 13:17:26 +0200569 ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
570}
571
Johannes Berg5825fe102008-09-09 12:56:01 +0200572void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
573{
574 struct ieee80211_local *local = sdata->local;
575 struct ieee80211_tx_queue_params qparam;
Johannes Bergaa837e12009-05-07 16:16:24 +0200576 int queue;
577 bool use_11b;
578 int aCWmin, aCWmax;
Johannes Berg5825fe102008-09-09 12:56:01 +0200579
580 if (!local->ops->conf_tx)
581 return;
582
583 memset(&qparam, 0, sizeof(qparam));
584
Johannes Bergaa837e12009-05-07 16:16:24 +0200585 use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
586 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
Johannes Berg5825fe102008-09-09 12:56:01 +0200587
Johannes Bergaa837e12009-05-07 16:16:24 +0200588 for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
589 /* Set defaults according to 802.11-2007 Table 7-37 */
590 aCWmax = 1023;
591 if (use_11b)
592 aCWmin = 31;
593 else
594 aCWmin = 15;
Johannes Berg5825fe102008-09-09 12:56:01 +0200595
Johannes Bergaa837e12009-05-07 16:16:24 +0200596 switch (queue) {
597 case 3: /* AC_BK */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200598 qparam.cw_max = aCWmax;
599 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200600 qparam.txop = 0;
601 qparam.aifs = 7;
602 break;
603 default: /* never happens but let's not leave undefined */
604 case 2: /* AC_BE */
Johannes Berg7ba10a82009-05-27 09:41:06 +0200605 qparam.cw_max = aCWmax;
606 qparam.cw_min = aCWmin;
Johannes Bergaa837e12009-05-07 16:16:24 +0200607 qparam.txop = 0;
608 qparam.aifs = 3;
609 break;
610 case 1: /* AC_VI */
611 qparam.cw_max = aCWmin;
612 qparam.cw_min = (aCWmin + 1) / 2 - 1;
613 if (use_11b)
614 qparam.txop = 6016/32;
615 else
616 qparam.txop = 3008/32;
617 qparam.aifs = 2;
618 break;
619 case 0: /* AC_VO */
620 qparam.cw_max = (aCWmin + 1) / 2 - 1;
621 qparam.cw_min = (aCWmin + 1) / 4 - 1;
622 if (use_11b)
623 qparam.txop = 3264/32;
624 else
625 qparam.txop = 1504/32;
626 qparam.aifs = 2;
627 break;
628 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200629
Kalle Valoab133152010-01-12 10:42:31 +0200630 qparam.uapsd = false;
631
Eliad Pellerf6f3def2011-09-25 20:06:54 +0300632 sdata->tx_conf[queue] = qparam;
633 drv_conf_tx(local, sdata, queue, &qparam);
Johannes Bergaa837e12009-05-07 16:16:24 +0200634 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +0200635
636 /* after reinitialize QoS TX queues setting to default,
637 * disable QoS at all */
Sujithd9734972010-07-23 10:47:11 +0530638
639 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
640 sdata->vif.bss_conf.qos =
641 sdata->vif.type != NL80211_IFTYPE_STATION;
642 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
643 }
Johannes Berg5825fe102008-09-09 12:56:01 +0200644}
Johannes Berge50db652008-09-09 15:07:09 +0200645
Johannes Berg46900292009-02-15 12:44:28 +0100646void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
647 const size_t supp_rates_len,
648 const u8 *supp_rates)
649{
650 struct ieee80211_local *local = sdata->local;
651 int i, have_higher_than_11mbit = 0;
652
653 /* cf. IEEE 802.11 9.2.12 */
654 for (i = 0; i < supp_rates_len; i++)
655 if ((supp_rates[i] & 0x7f) * 5 > 110)
656 have_higher_than_11mbit = 1;
657
658 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
659 have_higher_than_11mbit)
660 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
661 else
662 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
663
664 ieee80211_set_wmm_default(sdata);
665}
666
Johannes Berg881d9482009-01-21 15:13:48 +0100667u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
Johannes Berg96dd22a2008-09-11 00:01:57 +0200668 enum ieee80211_band band)
669{
670 struct ieee80211_supported_band *sband;
671 struct ieee80211_rate *bitrates;
Johannes Berg881d9482009-01-21 15:13:48 +0100672 u32 mandatory_rates;
Johannes Berg96dd22a2008-09-11 00:01:57 +0200673 enum ieee80211_rate_flags mandatory_flag;
674 int i;
675
676 sband = local->hw.wiphy->bands[band];
677 if (!sband) {
678 WARN_ON(1);
679 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
680 }
681
682 if (band == IEEE80211_BAND_2GHZ)
683 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
684 else
685 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
686
687 bitrates = sband->bitrates;
688 mandatory_rates = 0;
689 for (i = 0; i < sband->n_bitrates; i++)
690 if (bitrates[i].flags & mandatory_flag)
691 mandatory_rates |= BIT(i);
692 return mandatory_rates;
693}
Johannes Berg46900292009-02-15 12:44:28 +0100694
695void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
696 u16 transaction, u16 auth_alg,
Johannes Bergfffd0932009-07-08 14:22:54 +0200697 u8 *extra, size_t extra_len, const u8 *bssid,
698 const u8 *key, u8 key_len, u8 key_idx)
Johannes Berg46900292009-02-15 12:44:28 +0100699{
700 struct ieee80211_local *local = sdata->local;
701 struct sk_buff *skb;
702 struct ieee80211_mgmt *mgmt;
Johannes Bergfffd0932009-07-08 14:22:54 +0200703 int err;
Johannes Berg46900292009-02-15 12:44:28 +0100704
705 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
Jouni Malinen65fc73a2009-03-20 21:21:16 +0200706 sizeof(*mgmt) + 6 + extra_len);
Joe Perchesd15b8452011-08-29 14:17:31 -0700707 if (!skb)
Johannes Berg46900292009-02-15 12:44:28 +0100708 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700709
Johannes Berg46900292009-02-15 12:44:28 +0100710 skb_reserve(skb, local->hw.extra_tx_headroom);
711
712 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
713 memset(mgmt, 0, 24 + 6);
714 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
715 IEEE80211_STYPE_AUTH);
Johannes Berg46900292009-02-15 12:44:28 +0100716 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100717 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100718 memcpy(mgmt->bssid, bssid, ETH_ALEN);
719 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
720 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
721 mgmt->u.auth.status_code = cpu_to_le16(0);
722 if (extra)
723 memcpy(skb_put(skb, extra_len), extra, extra_len);
Johannes Berg46900292009-02-15 12:44:28 +0100724
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
726 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
727 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
728 WARN_ON(err);
729 }
730
Johannes Berg62ae67b2009-11-18 18:42:05 +0100731 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
732 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100733}
734
Johannes Bergde95a542009-04-01 11:58:36 +0200735int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
Johannes Berg4d36ec582009-10-27 20:59:55 +0100736 const u8 *ie, size_t ie_len,
Jouni Malinen651b5222010-08-28 19:37:51 +0300737 enum ieee80211_band band, u32 rate_mask,
738 u8 channel)
Johannes Bergde95a542009-04-01 11:58:36 +0200739{
740 struct ieee80211_supported_band *sband;
Johannes Berg8e664fb2009-12-23 13:15:38 +0100741 u8 *pos;
742 size_t offset = 0, noffset;
743 int supp_rates_len, i;
Jouni Malinen8dcb2002010-08-28 19:36:10 +0300744 u8 rates[32];
745 int num_rates;
746 int ext_rates_len;
Johannes Bergde95a542009-04-01 11:58:36 +0200747
Johannes Berg4d36ec582009-10-27 20:59:55 +0100748 sband = local->hw.wiphy->bands[band];
Johannes Bergde95a542009-04-01 11:58:36 +0200749
750 pos = buffer;
751
Jouni Malinen8dcb2002010-08-28 19:36:10 +0300752 num_rates = 0;
753 for (i = 0; i < sband->n_bitrates; i++) {
754 if ((BIT(i) & rate_mask) == 0)
755 continue; /* skip rate */
756 rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
757 }
758
759 supp_rates_len = min_t(int, num_rates, 8);
Johannes Berg8e664fb2009-12-23 13:15:38 +0100760
Johannes Bergde95a542009-04-01 11:58:36 +0200761 *pos++ = WLAN_EID_SUPP_RATES;
Johannes Berg8e664fb2009-12-23 13:15:38 +0100762 *pos++ = supp_rates_len;
Jouni Malinen8dcb2002010-08-28 19:36:10 +0300763 memcpy(pos, rates, supp_rates_len);
764 pos += supp_rates_len;
Johannes Bergde95a542009-04-01 11:58:36 +0200765
Johannes Berg8e664fb2009-12-23 13:15:38 +0100766 /* insert "request information" if in custom IEs */
767 if (ie && ie_len) {
768 static const u8 before_extrates[] = {
769 WLAN_EID_SSID,
770 WLAN_EID_SUPP_RATES,
771 WLAN_EID_REQUEST,
772 };
773 noffset = ieee80211_ie_split(ie, ie_len,
774 before_extrates,
775 ARRAY_SIZE(before_extrates),
776 offset);
777 memcpy(pos, ie + offset, noffset - offset);
778 pos += noffset - offset;
779 offset = noffset;
780 }
Johannes Bergde95a542009-04-01 11:58:36 +0200781
Jouni Malinen8dcb2002010-08-28 19:36:10 +0300782 ext_rates_len = num_rates - supp_rates_len;
783 if (ext_rates_len > 0) {
Johannes Berg8e664fb2009-12-23 13:15:38 +0100784 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Jouni Malinen8dcb2002010-08-28 19:36:10 +0300785 *pos++ = ext_rates_len;
786 memcpy(pos, rates + supp_rates_len, ext_rates_len);
787 pos += ext_rates_len;
Johannes Berg8e664fb2009-12-23 13:15:38 +0100788 }
789
Jouni Malinen651b5222010-08-28 19:37:51 +0300790 if (channel && sband->band == IEEE80211_BAND_2GHZ) {
791 *pos++ = WLAN_EID_DS_PARAMS;
792 *pos++ = 1;
793 *pos++ = channel;
794 }
795
Johannes Berg8e664fb2009-12-23 13:15:38 +0100796 /* insert custom IEs that go before HT */
797 if (ie && ie_len) {
798 static const u8 before_ht[] = {
799 WLAN_EID_SSID,
800 WLAN_EID_SUPP_RATES,
801 WLAN_EID_REQUEST,
802 WLAN_EID_EXT_SUPP_RATES,
803 WLAN_EID_DS_PARAMS,
804 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
805 };
806 noffset = ieee80211_ie_split(ie, ie_len,
807 before_ht, ARRAY_SIZE(before_ht),
808 offset);
809 memcpy(pos, ie + offset, noffset - offset);
810 pos += noffset - offset;
811 offset = noffset;
Johannes Bergde95a542009-04-01 11:58:36 +0200812 }
813
Johannes Berg5ef2d412009-03-31 12:12:07 +0200814 if (sband->ht_cap.ht_supported) {
Johannes Bergf38fd122009-12-01 18:29:42 +0100815 u16 cap = sband->ht_cap.cap;
816 __le16 tmp;
817
Johannes Berg5ef2d412009-03-31 12:12:07 +0200818 *pos++ = WLAN_EID_HT_CAPABILITY;
819 *pos++ = sizeof(struct ieee80211_ht_cap);
820 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
Johannes Bergf38fd122009-12-01 18:29:42 +0100821 tmp = cpu_to_le16(cap);
Johannes Berg5ef2d412009-03-31 12:12:07 +0200822 memcpy(pos, &tmp, sizeof(u16));
823 pos += sizeof(u16);
Johannes Berg5ef2d412009-03-31 12:12:07 +0200824 *pos++ = sband->ht_cap.ampdu_factor |
Johannes Bergf38fd122009-12-01 18:29:42 +0100825 (sband->ht_cap.ampdu_density <<
826 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
Johannes Berg5ef2d412009-03-31 12:12:07 +0200827 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
828 pos += sizeof(sband->ht_cap.mcs);
829 pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
830 }
831
Johannes Bergde95a542009-04-01 11:58:36 +0200832 /*
833 * If adding more here, adjust code in main.c
834 * that calculates local->scan_ies_len.
835 */
836
Johannes Berg8e664fb2009-12-23 13:15:38 +0100837 /* add any remaining custom IEs */
838 if (ie && ie_len) {
839 noffset = ie_len;
840 memcpy(pos, ie + offset, noffset - offset);
841 pos += noffset - offset;
Johannes Bergde95a542009-04-01 11:58:36 +0200842 }
843
844 return pos - buffer;
845}
846
Juuso Oikarinena619a4c2010-11-11 08:50:18 +0200847struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Berg85a237f2011-07-18 18:08:36 +0200848 u8 *dst, u32 ratemask,
Juuso Oikarinena619a4c2010-11-11 08:50:18 +0200849 const u8 *ssid, size_t ssid_len,
Paul Stewarta806c552011-06-23 09:00:11 -0800850 const u8 *ie, size_t ie_len,
851 bool directed)
Johannes Berg46900292009-02-15 12:44:28 +0100852{
853 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100854 struct sk_buff *skb;
855 struct ieee80211_mgmt *mgmt;
Kalle Valo7c12ce82010-01-05 20:16:44 +0200856 size_t buf_len;
857 u8 *buf;
Jouni Malinen651b5222010-08-28 19:37:51 +0300858 u8 chan;
Johannes Berg46900292009-02-15 12:44:28 +0100859
Kalle Valo7c12ce82010-01-05 20:16:44 +0200860 /* FIXME: come up with a proper value */
861 buf = kmalloc(200 + ie_len, GFP_KERNEL);
Joe Perchesd15b8452011-08-29 14:17:31 -0700862 if (!buf)
Juuso Oikarinena619a4c2010-11-11 08:50:18 +0200863 return NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100864
Paul Stewarta806c552011-06-23 09:00:11 -0800865 /*
866 * Do not send DS Channel parameter for directed probe requests
867 * in order to maximize the chance that we get a response. Some
868 * badly-behaved APs don't respond when this parameter is included.
869 */
870 if (directed)
871 chan = 0;
872 else
873 chan = ieee80211_frequency_to_channel(
874 local->hw.conf.channel->center_freq);
Jouni Malinen651b5222010-08-28 19:37:51 +0300875
Kalle Valo7c12ce82010-01-05 20:16:44 +0200876 buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
Jouni Malinen8dcb2002010-08-28 19:36:10 +0300877 local->hw.conf.channel->band,
Johannes Berg85a237f2011-07-18 18:08:36 +0200878 ratemask, chan);
Kalle Valo7c12ce82010-01-05 20:16:44 +0200879
880 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
881 ssid, ssid_len,
882 buf, buf_len);
Johannes Berg5b2bbf72011-11-08 13:04:41 +0100883 if (!skb)
884 goto out;
Kalle Valo7c12ce82010-01-05 20:16:44 +0200885
Johannes Berg46900292009-02-15 12:44:28 +0100886 if (dst) {
Kalle Valo7c12ce82010-01-05 20:16:44 +0200887 mgmt = (struct ieee80211_mgmt *) skb->data;
Johannes Berg46900292009-02-15 12:44:28 +0100888 memcpy(mgmt->da, dst, ETH_ALEN);
889 memcpy(mgmt->bssid, dst, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100890 }
Johannes Berg46900292009-02-15 12:44:28 +0100891
Johannes Berg62ae67b2009-11-18 18:42:05 +0100892 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg5b2bbf72011-11-08 13:04:41 +0100893
894 out:
Ming Lei145b6d12010-01-15 21:44:21 +0800895 kfree(buf);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +0200896
897 return skb;
898}
899
900void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
901 const u8 *ssid, size_t ssid_len,
Paul Stewarta806c552011-06-23 09:00:11 -0800902 const u8 *ie, size_t ie_len,
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +0530903 u32 ratemask, bool directed, bool no_cck)
Juuso Oikarinena619a4c2010-11-11 08:50:18 +0200904{
905 struct sk_buff *skb;
906
Johannes Berg85a237f2011-07-18 18:08:36 +0200907 skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
908 ie, ie_len, directed);
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +0530909 if (skb) {
910 if (no_cck)
911 IEEE80211_SKB_CB(skb)->flags |=
912 IEEE80211_TX_CTL_NO_CCK_RATE;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +0200913 ieee80211_tx_skb(sdata, skb);
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +0530914 }
Johannes Berg46900292009-02-15 12:44:28 +0100915}
916
917u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
918 struct ieee802_11_elems *elems,
919 enum ieee80211_band band)
920{
921 struct ieee80211_supported_band *sband;
922 struct ieee80211_rate *bitrates;
923 size_t num_rates;
924 u32 supp_rates;
925 int i, j;
926 sband = local->hw.wiphy->bands[band];
927
928 if (!sband) {
929 WARN_ON(1);
930 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
931 }
932
933 bitrates = sband->bitrates;
934 num_rates = sband->n_bitrates;
935 supp_rates = 0;
936 for (i = 0; i < elems->supp_rates_len +
937 elems->ext_supp_rates_len; i++) {
938 u8 rate = 0;
939 int own_rate;
940 if (i < elems->supp_rates_len)
941 rate = elems->supp_rates[i];
942 else if (elems->ext_supp_rates)
943 rate = elems->ext_supp_rates
944 [i - elems->supp_rates_len];
945 own_rate = 5 * (rate & 0x7f);
946 for (j = 0; j < num_rates; j++)
947 if (bitrates[j].bitrate == own_rate)
948 supp_rates |= BIT(j);
949 }
950 return supp_rates;
951}
Johannes Bergf2753dd2009-04-14 10:09:24 +0200952
Johannes Berg84f6a012009-08-20 20:02:20 +0200953void ieee80211_stop_device(struct ieee80211_local *local)
954{
955 ieee80211_led_radio(local, false);
Johannes Berg67408c82010-11-30 08:59:23 +0100956 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
Johannes Berg84f6a012009-08-20 20:02:20 +0200957
958 cancel_work_sync(&local->reconfig_filter);
Johannes Berg84f6a012009-08-20 20:02:20 +0200959
960 flush_workqueue(local->workqueue);
Lennert Buytenhek678f4152010-01-10 14:07:53 +0100961 drv_stop(local);
Johannes Berg84f6a012009-08-20 20:02:20 +0200962}
963
Johannes Bergf2753dd2009-04-14 10:09:24 +0200964int ieee80211_reconfig(struct ieee80211_local *local)
965{
966 struct ieee80211_hw *hw = &local->hw;
967 struct ieee80211_sub_if_data *sdata;
Johannes Bergf2753dd2009-04-14 10:09:24 +0200968 struct sta_info *sta;
Eliad Peller2683d652011-07-14 20:29:42 +0300969 int res, i;
Johannes Berg5bb644a2009-05-17 11:40:42 +0200970
Johannes Bergeecc4802011-05-04 15:37:29 +0200971#ifdef CONFIG_PM
Johannes Bergceb99fe2009-11-19 14:29:39 +0100972 if (local->suspended)
973 local->resuming = true;
Johannes Bergf2753dd2009-04-14 10:09:24 +0200974
Johannes Bergeecc4802011-05-04 15:37:29 +0200975 if (local->wowlan) {
976 local->wowlan = false;
977 res = drv_resume(local);
978 if (res < 0) {
979 local->resuming = false;
980 return res;
981 }
982 if (res == 0)
983 goto wake_up;
984 WARN_ON(res > 1);
985 /*
986 * res is 1, which means the driver requested
987 * to go through a regular reset on wakeup.
988 */
989 }
990#endif
991
Johannes Berg94f9b972011-07-14 16:48:54 +0200992 /* setup fragmentation threshold */
993 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
Johannes Bergf2753dd2009-04-14 10:09:24 +0200994
Johannes Berg94f9b972011-07-14 16:48:54 +0200995 /* setup RTS threshold */
996 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
997
998 /* reset coverage class */
999 drv_set_coverage_class(local, hw->wiphy->coverage_class);
1000
1001 /* everything else happens only if HW was up & running */
1002 if (!local->open_count)
1003 goto wake_up;
1004
1005 /*
1006 * Upon resume hardware can sometimes be goofy due to
1007 * various platform / driver / bus issues, so restarting
1008 * the device may at times not work immediately. Propagate
1009 * the error.
1010 */
1011 res = drv_start(local);
1012 if (res) {
1013 WARN(local->suspended, "Hardware became unavailable "
1014 "upon resume. This could be a software issue "
1015 "prior to suspend or a hardware issue.\n");
1016 return res;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001017 }
1018
Johannes Berg94f9b972011-07-14 16:48:54 +02001019 ieee80211_led_radio(local, true);
1020 ieee80211_mod_tpt_led_trig(local,
1021 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1022
Johannes Bergf2753dd2009-04-14 10:09:24 +02001023 /* add interfaces */
1024 list_for_each_entry(sdata, &local->interfaces, list) {
1025 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1026 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
Johannes Berg1ed32e42009-12-23 13:15:45 +01001027 ieee80211_sdata_running(sdata))
1028 res = drv_add_interface(local, &sdata->vif);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001029 }
1030
1031 /* add STAs back */
Johannes Berg34e89502010-02-03 13:59:58 +01001032 mutex_lock(&local->sta_mtx);
1033 list_for_each_entry(sta, &local->sta_list, list) {
1034 if (sta->uploaded) {
Johannes Berg5bb644a2009-05-17 11:40:42 +02001035 sdata = sta->sdata;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001036 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1037 sdata = container_of(sdata->bss,
1038 struct ieee80211_sub_if_data,
1039 u.ap);
1040
Eliad Pellerf785d832011-08-08 16:50:22 +03001041 memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
Johannes Berg34e89502010-02-03 13:59:58 +01001042 WARN_ON(drv_sta_add(local, sdata, &sta->sta));
Johannes Bergf2753dd2009-04-14 10:09:24 +02001043 }
Johannes Bergf2753dd2009-04-14 10:09:24 +02001044 }
Johannes Berg34e89502010-02-03 13:59:58 +01001045 mutex_unlock(&local->sta_mtx);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001046
Eliad Peller2683d652011-07-14 20:29:42 +03001047 /* reconfigure tx conf */
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001048 list_for_each_entry(sdata, &local->interfaces, list) {
1049 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1050 sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1051 !ieee80211_sdata_running(sdata))
1052 continue;
1053
1054 for (i = 0; i < hw->queues; i++)
1055 drv_conf_tx(local, sdata, i, &sdata->tx_conf[i]);
1056 }
Eliad Peller2683d652011-07-14 20:29:42 +03001057
Johannes Bergf2753dd2009-04-14 10:09:24 +02001058 /* reconfigure hardware */
1059 ieee80211_hw_config(local, ~0);
1060
Johannes Bergf2753dd2009-04-14 10:09:24 +02001061 ieee80211_configure_filter(local);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001062
1063 /* Finally also reconfigure all the BSS information */
1064 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergac8dd502010-05-05 09:44:02 +02001065 u32 changed;
1066
Johannes Berg9607e6b2009-12-23 13:15:31 +01001067 if (!ieee80211_sdata_running(sdata))
Johannes Bergf2753dd2009-04-14 10:09:24 +02001068 continue;
Johannes Bergac8dd502010-05-05 09:44:02 +02001069
1070 /* common change flags for all interface types */
1071 changed = BSS_CHANGED_ERP_CTS_PROT |
1072 BSS_CHANGED_ERP_PREAMBLE |
1073 BSS_CHANGED_ERP_SLOT |
1074 BSS_CHANGED_HT |
1075 BSS_CHANGED_BASIC_RATES |
1076 BSS_CHANGED_BEACON_INT |
1077 BSS_CHANGED_BSSID |
Johannes Berg4ced3f72010-07-19 16:39:04 +02001078 BSS_CHANGED_CQM |
1079 BSS_CHANGED_QOS;
Johannes Bergac8dd502010-05-05 09:44:02 +02001080
Johannes Bergf2753dd2009-04-14 10:09:24 +02001081 switch (sdata->vif.type) {
1082 case NL80211_IFTYPE_STATION:
Johannes Bergac8dd502010-05-05 09:44:02 +02001083 changed |= BSS_CHANGED_ASSOC;
Eliad Pellera7b545f2011-02-08 18:43:19 +02001084 mutex_lock(&sdata->u.mgd.mtx);
Johannes Bergac8dd502010-05-05 09:44:02 +02001085 ieee80211_bss_info_change_notify(sdata, changed);
Eliad Pellera7b545f2011-02-08 18:43:19 +02001086 mutex_unlock(&sdata->u.mgd.mtx);
Johannes Bergac8dd502010-05-05 09:44:02 +02001087 break;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001088 case NL80211_IFTYPE_ADHOC:
Johannes Bergac8dd502010-05-05 09:44:02 +02001089 changed |= BSS_CHANGED_IBSS;
1090 /* fall through */
Johannes Bergf2753dd2009-04-14 10:09:24 +02001091 case NL80211_IFTYPE_AP:
Arik Nemtsov78274932011-09-04 11:11:32 +03001092 changed |= BSS_CHANGED_SSID;
1093 /* fall through */
Johannes Bergf2753dd2009-04-14 10:09:24 +02001094 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergac8dd502010-05-05 09:44:02 +02001095 changed |= BSS_CHANGED_BEACON |
1096 BSS_CHANGED_BEACON_ENABLED;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001097 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001098 break;
1099 case NL80211_IFTYPE_WDS:
1100 break;
1101 case NL80211_IFTYPE_AP_VLAN:
1102 case NL80211_IFTYPE_MONITOR:
1103 /* ignore virtual */
1104 break;
1105 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +02001106 case NUM_NL80211_IFTYPES:
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001107 case NL80211_IFTYPE_P2P_CLIENT:
1108 case NL80211_IFTYPE_P2P_GO:
Johannes Bergf2753dd2009-04-14 10:09:24 +02001109 WARN_ON(1);
1110 break;
1111 }
1112 }
1113
Johannes Berg2a419052010-06-10 10:21:29 +02001114 /*
1115 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1116 * sessions can be established after a resume.
1117 *
1118 * Also tear down aggregation sessions since reconfiguring
1119 * them in a hardware restart scenario is not easily done
1120 * right now, and the hardware will have lost information
1121 * about the sessions, but we and the AP still think they
1122 * are active. This is really a workaround though.
1123 */
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001124 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
Johannes Berg2a419052010-06-10 10:21:29 +02001125 mutex_lock(&local->sta_mtx);
1126
1127 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Berg53f73c02010-10-05 19:37:40 +02001128 ieee80211_sta_tear_down_BA_sessions(sta, true);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001129 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001130 }
Johannes Berg2a419052010-06-10 10:21:29 +02001131
1132 mutex_unlock(&local->sta_mtx);
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001133 }
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001134
Johannes Bergf2753dd2009-04-14 10:09:24 +02001135 /* add back keys */
1136 list_for_each_entry(sdata, &local->interfaces, list)
Johannes Berg9607e6b2009-12-23 13:15:31 +01001137 if (ieee80211_sdata_running(sdata))
Johannes Bergf2753dd2009-04-14 10:09:24 +02001138 ieee80211_enable_keys(sdata);
1139
Johannes Bergeecc4802011-05-04 15:37:29 +02001140 wake_up:
Johannes Bergf2753dd2009-04-14 10:09:24 +02001141 ieee80211_wake_queues_by_reason(hw,
1142 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1143
Johannes Berg5bb644a2009-05-17 11:40:42 +02001144 /*
1145 * If this is for hw restart things are still running.
1146 * We may want to change that later, however.
1147 */
Johannes Bergceb99fe2009-11-19 14:29:39 +01001148 if (!local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001149 return 0;
1150
1151#ifdef CONFIG_PM
Johannes Bergceb99fe2009-11-19 14:29:39 +01001152 /* first set suspended false, then resuming */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001153 local->suspended = false;
Johannes Bergceb99fe2009-11-19 14:29:39 +01001154 mb();
1155 local->resuming = false;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001156
1157 list_for_each_entry(sdata, &local->interfaces, list) {
1158 switch(sdata->vif.type) {
1159 case NL80211_IFTYPE_STATION:
1160 ieee80211_sta_restart(sdata);
1161 break;
1162 case NL80211_IFTYPE_ADHOC:
1163 ieee80211_ibss_restart(sdata);
1164 break;
1165 case NL80211_IFTYPE_MESH_POINT:
1166 ieee80211_mesh_restart(sdata);
1167 break;
1168 default:
1169 break;
1170 }
1171 }
1172
Johannes Berg26d59532011-04-01 13:52:48 +02001173 mod_timer(&local->sta_cleanup, jiffies + 1);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001174
Johannes Berg34e89502010-02-03 13:59:58 +01001175 mutex_lock(&local->sta_mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001176 list_for_each_entry(sta, &local->sta_list, list)
1177 mesh_plink_restart(sta);
Johannes Berg34e89502010-02-03 13:59:58 +01001178 mutex_unlock(&local->sta_mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001179#else
1180 WARN_ON(1);
1181#endif
Johannes Bergf2753dd2009-04-14 10:09:24 +02001182 return 0;
1183}
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001184
Johannes Berg95acac62011-07-12 12:30:59 +02001185void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1186{
1187 struct ieee80211_sub_if_data *sdata;
1188 struct ieee80211_local *local;
1189 struct ieee80211_key *key;
1190
1191 if (WARN_ON(!vif))
1192 return;
1193
1194 sdata = vif_to_sdata(vif);
1195 local = sdata->local;
1196
1197 if (WARN_ON(!local->resuming))
1198 return;
1199
1200 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1201 return;
1202
1203 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1204
1205 mutex_lock(&local->key_mtx);
1206 list_for_each_entry(key, &sdata->key_list, list)
1207 key->flags |= KEY_FLAG_TAINTED;
1208 mutex_unlock(&local->key_mtx);
1209}
1210EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1211
Johannes Berg0f782312009-12-01 13:37:02 +01001212static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
1213 enum ieee80211_smps_mode *smps_mode)
1214{
1215 if (ifmgd->associated) {
1216 *smps_mode = ifmgd->ap_smps;
1217
1218 if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1219 if (ifmgd->powersave)
1220 *smps_mode = IEEE80211_SMPS_DYNAMIC;
1221 else
1222 *smps_mode = IEEE80211_SMPS_OFF;
1223 }
1224
1225 return 1;
1226 }
1227
1228 return 0;
1229}
1230
1231/* must hold iflist_mtx */
Johannes Berg025e6be2010-10-05 10:41:47 +02001232void ieee80211_recalc_smps(struct ieee80211_local *local)
Johannes Berg0f782312009-12-01 13:37:02 +01001233{
1234 struct ieee80211_sub_if_data *sdata;
1235 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
1236 int count = 0;
1237
Johannes Berg46a5eba2010-09-15 13:28:15 +02001238 lockdep_assert_held(&local->iflist_mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01001239
1240 /*
1241 * This function could be improved to handle multiple
1242 * interfaces better, but right now it makes any
1243 * non-station interfaces force SM PS to be turned
1244 * off. If there are multiple station interfaces it
1245 * could also use the best possible mode, e.g. if
1246 * one is in static and the other in dynamic then
1247 * dynamic is ok.
1248 */
1249
1250 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg26a58452010-08-27 12:35:55 +02001251 if (!ieee80211_sdata_running(sdata))
Johannes Berg0f782312009-12-01 13:37:02 +01001252 continue;
1253 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1254 goto set;
Johannes Berg025e6be2010-10-05 10:41:47 +02001255
1256 count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
Johannes Berg0f782312009-12-01 13:37:02 +01001257
1258 if (count > 1) {
1259 smps_mode = IEEE80211_SMPS_OFF;
1260 break;
1261 }
1262 }
1263
1264 if (smps_mode == local->smps_mode)
1265 return;
1266
1267 set:
1268 local->smps_mode = smps_mode;
1269 /* changed flag is auto-detected for this */
1270 ieee80211_hw_config(local, 0);
1271}
Johannes Berg8e664fb2009-12-23 13:15:38 +01001272
1273static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1274{
1275 int i;
1276
1277 for (i = 0; i < n_ids; i++)
1278 if (ids[i] == id)
1279 return true;
1280 return false;
1281}
1282
1283/**
1284 * ieee80211_ie_split - split an IE buffer according to ordering
1285 *
1286 * @ies: the IE buffer
1287 * @ielen: the length of the IE buffer
1288 * @ids: an array with element IDs that are allowed before
1289 * the split
1290 * @n_ids: the size of the element ID array
1291 * @offset: offset where to start splitting in the buffer
1292 *
1293 * This function splits an IE buffer by updating the @offset
1294 * variable to point to the location where the buffer should be
1295 * split.
1296 *
1297 * It assumes that the given IE buffer is well-formed, this
1298 * has to be guaranteed by the caller!
1299 *
1300 * It also assumes that the IEs in the buffer are ordered
1301 * correctly, if not the result of using this function will not
1302 * be ordered correctly either, i.e. it does no reordering.
1303 *
1304 * The function returns the offset where the next part of the
1305 * buffer starts, which may be @ielen if the entire (remainder)
1306 * of the buffer should be used.
1307 */
1308size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1309 const u8 *ids, int n_ids, size_t offset)
1310{
1311 size_t pos = offset;
1312
1313 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1314 pos += 2 + ies[pos + 1];
1315
1316 return pos;
1317}
1318
1319size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1320{
1321 size_t pos = offset;
1322
1323 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1324 pos += 2 + ies[pos + 1];
1325
1326 return pos;
1327}
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07001328
1329static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
1330 int rssi_min_thold,
1331 int rssi_max_thold)
1332{
1333 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
1334
1335 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1336 return;
1337
1338 /*
1339 * Scale up threshold values before storing it, as the RSSI averaging
1340 * algorithm uses a scaled up value as well. Change this scaling
1341 * factor if the RSSI averaging algorithm changes.
1342 */
1343 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
1344 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
1345}
1346
1347void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
1348 int rssi_min_thold,
1349 int rssi_max_thold)
1350{
1351 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1352
1353 WARN_ON(rssi_min_thold == rssi_max_thold ||
1354 rssi_min_thold > rssi_max_thold);
1355
1356 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
1357 rssi_max_thold);
1358}
1359EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
1360
1361void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
1362{
1363 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1364
1365 _ieee80211_enable_rssi_reports(sdata, 0, 0);
1366}
1367EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
Arik Nemtsov768db342011-09-28 14:12:51 +03001368
1369int ieee80211_add_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
1370{
1371 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1372 struct ieee80211_local *local = sdata->local;
1373 struct ieee80211_supported_band *sband;
1374 int rate;
1375 u8 i, rates, *pos;
1376
1377 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1378 rates = sband->n_bitrates;
1379 if (rates > 8)
1380 rates = 8;
1381
1382 if (skb_tailroom(skb) < rates + 2)
1383 return -ENOMEM;
1384
1385 pos = skb_put(skb, rates + 2);
1386 *pos++ = WLAN_EID_SUPP_RATES;
1387 *pos++ = rates;
1388 for (i = 0; i < rates; i++) {
1389 rate = sband->bitrates[i].bitrate;
1390 *pos++ = (u8) (rate / 5);
1391 }
1392
1393 return 0;
1394}
1395
1396int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
1397{
1398 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1399 struct ieee80211_local *local = sdata->local;
1400 struct ieee80211_supported_band *sband;
1401 int rate;
1402 u8 i, exrates, *pos;
1403
1404 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1405 exrates = sband->n_bitrates;
1406 if (exrates > 8)
1407 exrates -= 8;
1408 else
1409 exrates = 0;
1410
1411 if (skb_tailroom(skb) < exrates + 2)
1412 return -ENOMEM;
1413
1414 if (exrates) {
1415 pos = skb_put(skb, exrates + 2);
1416 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1417 *pos++ = exrates;
1418 for (i = 8; i < sband->n_bitrates; i++) {
1419 rate = sband->bitrates[i].bitrate;
1420 *pos++ = (u8) (rate / 5);
1421 }
1422 }
1423 return 0;
1424}