blob: 4e8513cfdae50274730f9a7c66208f543586a35d [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040016#include <linux/export.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020017#include <linux/types.h>
18#include <linux/slab.h>
19#include <linux/skbuff.h>
20#include <linux/etherdevice.h>
21#include <linux/if_arp.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020022#include <linux/bitmap.h>
Johannes Bergdd769862011-11-18 16:54:50 +010023#include <linux/crc32.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070024#include <net/net_namespace.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020025#include <net/cfg80211.h>
Johannes Bergdabeb342007-11-09 01:57:29 +010026#include <net/rtnetlink.h>
Johannes Bergc2d15602007-07-27 15:43:23 +020027
28#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010031#include "mesh.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020032#include "wme.h"
Johannes Bergf2753dd2009-04-14 10:09:24 +020033#include "led.h"
Johannes Bergfffd0932009-07-08 14:22:54 +020034#include "wep.h"
Johannes Bergc2d15602007-07-27 15:43:23 +020035
36/* privid for wiphys to determine whether they belong to us or not */
Johannes Berg8a47cea2014-01-20 23:55:44 +010037const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
Johannes Bergc2d15602007-07-27 15:43:23 +020038
Luis R. Rodriguez9a953712009-01-22 15:05:53 -080039struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
40{
41 struct ieee80211_local *local;
42 BUG_ON(!wiphy);
43
44 local = wiphy_priv(wiphy);
45 return &local->hw;
46}
47EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
Johannes Bergc2d15602007-07-27 15:43:23 +020048
Ron Rindjunsky71364712007-12-25 17:00:36 +020049u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
Johannes Berg05c914f2008-09-11 00:01:58 +020050 enum nl80211_iftype type)
Johannes Bergc2d15602007-07-27 15:43:23 +020051{
Harvey Harrisona494bb12008-06-11 14:21:58 -070052 __le16 fc = hdr->frame_control;
Johannes Bergc2d15602007-07-27 15:43:23 +020053
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020054 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
55 if (len < 16)
Johannes Bergc2d15602007-07-27 15:43:23 +020056 return NULL;
57
Harvey Harrisona494bb12008-06-11 14:21:58 -070058 if (ieee80211_is_data(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020059 if (len < 24) /* drop incorrect hdr len (data) */
60 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070061
62 if (ieee80211_has_a4(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020063 return NULL;
Harvey Harrisona494bb12008-06-11 14:21:58 -070064 if (ieee80211_has_tods(fc))
65 return hdr->addr1;
66 if (ieee80211_has_fromds(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020067 return hdr->addr2;
Harvey Harrisona494bb12008-06-11 14:21:58 -070068
69 return hdr->addr3;
70 }
71
72 if (ieee80211_is_mgmt(fc)) {
Ron Rindjunsky98f0b0a2007-12-18 17:23:53 +020073 if (len < 24) /* drop incorrect hdr len (mgmt) */
74 return NULL;
Johannes Bergc2d15602007-07-27 15:43:23 +020075 return hdr->addr3;
Harvey Harrisona494bb12008-06-11 14:21:58 -070076 }
77
78 if (ieee80211_is_ctl(fc)) {
Weilong Chenf359d3f2013-12-18 15:44:16 +080079 if (ieee80211_is_pspoll(fc))
Johannes Bergc2d15602007-07-27 15:43:23 +020080 return hdr->addr1;
Harvey Harrisona494bb12008-06-11 14:21:58 -070081
82 if (ieee80211_is_back_req(fc)) {
Ron Rindjunsky71364712007-12-25 17:00:36 +020083 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020084 case NL80211_IFTYPE_STATION:
Ron Rindjunsky71364712007-12-25 17:00:36 +020085 return hdr->addr2;
Johannes Berg05c914f2008-09-11 00:01:58 +020086 case NL80211_IFTYPE_AP:
87 case NL80211_IFTYPE_AP_VLAN:
Ron Rindjunsky71364712007-12-25 17:00:36 +020088 return hdr->addr1;
89 default:
Harvey Harrisona494bb12008-06-11 14:21:58 -070090 break; /* fall through to the return */
Ron Rindjunsky71364712007-12-25 17:00:36 +020091 }
92 }
Johannes Bergc2d15602007-07-27 15:43:23 +020093 }
94
95 return NULL;
96}
97
Johannes Berg5cf121c2008-02-25 16:27:43 +010098void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
Johannes Bergc2d15602007-07-27 15:43:23 +020099{
Johannes Berg252b86c2011-11-16 15:28:55 +0100100 struct sk_buff *skb;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100101 struct ieee80211_hdr *hdr;
Johannes Bergc2d15602007-07-27 15:43:23 +0200102
Johannes Berg252b86c2011-11-16 15:28:55 +0100103 skb_queue_walk(&tx->skbs, skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100104 hdr = (struct ieee80211_hdr *) skb->data;
105 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
Johannes Berg252b86c2011-11-16 15:28:55 +0100106 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200107}
108
Michal Kazior4ee73f32012-04-11 08:47:56 +0200109int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200110 int rate, int erp, int short_preamble,
111 int shift)
Johannes Bergc2d15602007-07-27 15:43:23 +0200112{
113 int dur;
114
115 /* calculate duration (in microseconds, rounded up to next higher
116 * integer if it includes a fractional microsecond) to send frame of
117 * len bytes (does not include FCS) at the given rate. Duration will
118 * also include SIFS.
119 *
120 * rate is in 100 kbps, so divident is multiplied by 10 in the
121 * DIV_ROUND_UP() operations.
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200122 *
123 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
124 * is assumed to be 0 otherwise.
Johannes Bergc2d15602007-07-27 15:43:23 +0200125 */
126
Michal Kazior4ee73f32012-04-11 08:47:56 +0200127 if (band == IEEE80211_BAND_5GHZ || erp) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200128 /*
129 * OFDM:
130 *
131 * N_DBPS = DATARATE x 4
132 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
133 * (16 = SIGNAL time, 6 = tail bits)
134 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
135 *
136 * T_SYM = 4 usec
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200137 * 802.11a - 18.5.2: aSIFSTime = 16 usec
Johannes Bergc2d15602007-07-27 15:43:23 +0200138 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
139 * signal ext = 6 usec
140 */
Johannes Bergc2d15602007-07-27 15:43:23 +0200141 dur = 16; /* SIFS + signal ext */
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200142 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
143 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200144
145 /* IEEE 802.11-2012 18.3.2.4: all values above are:
146 * * times 4 for 5 MHz
147 * * times 2 for 10 MHz
148 */
149 dur *= 1 << shift;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200150
151 /* rates should already consider the channel bandwidth,
152 * don't apply divisor again.
153 */
154 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
155 4 * rate); /* T_SYM x N_SYM */
Johannes Bergc2d15602007-07-27 15:43:23 +0200156 } else {
157 /*
158 * 802.11b or 802.11g with 802.11b compatibility:
159 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
160 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
161 *
162 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
163 * aSIFSTime = 10 usec
164 * aPreambleLength = 144 usec or 72 usec with short preamble
165 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
166 */
167 dur = 10; /* aSIFSTime = 10 usec */
168 dur += short_preamble ? (72 + 24) : (144 + 48);
169
170 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
171 }
172
173 return dur;
174}
175
176/* Exported duration function for driver use */
Johannes Berg32bfd352007-12-19 01:31:26 +0100177__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
178 struct ieee80211_vif *vif,
Michal Kazior4ee73f32012-04-11 08:47:56 +0200179 enum ieee80211_band band,
Johannes Berg8318d782008-01-24 19:38:38 +0100180 size_t frame_len,
181 struct ieee80211_rate *rate)
Johannes Bergc2d15602007-07-27 15:43:23 +0200182{
Johannes Berg25d834e2008-09-12 22:52:47 +0200183 struct ieee80211_sub_if_data *sdata;
Johannes Bergc2d15602007-07-27 15:43:23 +0200184 u16 dur;
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200185 int erp, shift = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200186 bool short_preamble = false;
Johannes Bergc2d15602007-07-27 15:43:23 +0200187
Johannes Berg8318d782008-01-24 19:38:38 +0100188 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200189 if (vif) {
190 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200191 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200192 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
193 erp = rate->flags & IEEE80211_RATE_ERP_G;
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200194 shift = ieee80211_vif_get_shift(vif);
Johannes Berg25d834e2008-09-12 22:52:47 +0200195 }
Johannes Berg8318d782008-01-24 19:38:38 +0100196
Michal Kazior4ee73f32012-04-11 08:47:56 +0200197 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200198 short_preamble, shift);
Johannes Bergc2d15602007-07-27 15:43:23 +0200199
200 return cpu_to_le16(dur);
201}
202EXPORT_SYMBOL(ieee80211_generic_frame_duration);
203
Johannes Berg32bfd352007-12-19 01:31:26 +0100204__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
205 struct ieee80211_vif *vif, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200206 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200207{
208 struct ieee80211_local *local = hw_to_local(hw);
209 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200210 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100211 bool short_preamble;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200212 int erp, shift = 0, bitrate;
Johannes Bergc2d15602007-07-27 15:43:23 +0200213 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200214 struct ieee80211_supported_band *sband;
215
Michal Kazior4ee73f32012-04-11 08:47:56 +0200216 sband = local->hw.wiphy->bands[frame_txctl->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200217
Johannes Berg25d834e2008-09-12 22:52:47 +0200218 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200219
Johannes Berge039fa42008-05-15 12:55:29 +0200220 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100221
222 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200223 if (vif) {
224 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200225 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200226 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
227 erp = rate->flags & IEEE80211_RATE_ERP_G;
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200228 shift = ieee80211_vif_get_shift(vif);
Johannes Berg25d834e2008-09-12 22:52:47 +0200229 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200230
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200231 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
232
Johannes Bergc2d15602007-07-27 15:43:23 +0200233 /* CTS duration */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200234 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200235 erp, short_preamble, shift);
Johannes Bergc2d15602007-07-27 15:43:23 +0200236 /* Data frame duration */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200237 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200238 erp, short_preamble, shift);
Johannes Bergc2d15602007-07-27 15:43:23 +0200239 /* ACK duration */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200240 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200241 erp, short_preamble, shift);
Johannes Bergc2d15602007-07-27 15:43:23 +0200242
243 return cpu_to_le16(dur);
244}
245EXPORT_SYMBOL(ieee80211_rts_duration);
246
Johannes Berg32bfd352007-12-19 01:31:26 +0100247__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
248 struct ieee80211_vif *vif,
Johannes Bergc2d15602007-07-27 15:43:23 +0200249 size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200250 const struct ieee80211_tx_info *frame_txctl)
Johannes Bergc2d15602007-07-27 15:43:23 +0200251{
252 struct ieee80211_local *local = hw_to_local(hw);
253 struct ieee80211_rate *rate;
Johannes Berg25d834e2008-09-12 22:52:47 +0200254 struct ieee80211_sub_if_data *sdata;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100255 bool short_preamble;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200256 int erp, shift = 0, bitrate;
Johannes Bergc2d15602007-07-27 15:43:23 +0200257 u16 dur;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200258 struct ieee80211_supported_band *sband;
259
Michal Kazior4ee73f32012-04-11 08:47:56 +0200260 sband = local->hw.wiphy->bands[frame_txctl->band];
Johannes Bergc2d15602007-07-27 15:43:23 +0200261
Johannes Berg25d834e2008-09-12 22:52:47 +0200262 short_preamble = false;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200263
Johannes Berge039fa42008-05-15 12:55:29 +0200264 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100265 erp = 0;
Johannes Berg25d834e2008-09-12 22:52:47 +0200266 if (vif) {
267 sdata = vif_to_sdata(vif);
Johannes Bergbda39332008-10-11 01:51:51 +0200268 short_preamble = sdata->vif.bss_conf.use_short_preamble;
Johannes Berg25d834e2008-09-12 22:52:47 +0200269 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
270 erp = rate->flags & IEEE80211_RATE_ERP_G;
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200271 shift = ieee80211_vif_get_shift(vif);
Johannes Berg25d834e2008-09-12 22:52:47 +0200272 }
Johannes Bergc2d15602007-07-27 15:43:23 +0200273
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200274 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
275
Johannes Bergc2d15602007-07-27 15:43:23 +0200276 /* Data frame duration */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200277 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200278 erp, short_preamble, shift);
Johannes Berge039fa42008-05-15 12:55:29 +0200279 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
Johannes Bergc2d15602007-07-27 15:43:23 +0200280 /* ACK duration */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200281 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
Simon Wunderlich438b61b2013-07-08 16:55:51 +0200282 erp, short_preamble, shift);
Johannes Bergc2d15602007-07-27 15:43:23 +0200283 }
284
Johannes Bergc2d15602007-07-27 15:43:23 +0200285 return cpu_to_le16(dur);
286}
287EXPORT_SYMBOL(ieee80211_ctstoself_duration);
288
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200289void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
290{
291 struct ieee80211_sub_if_data *sdata;
Johannes Berga6f38ac2012-07-04 12:49:59 +0200292 int n_acs = IEEE80211_NUM_ACS;
293
294 if (local->hw.queues < IEEE80211_NUM_ACS)
295 n_acs = 1;
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200296
297 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
298 int ac;
299
Johannes Bergf142c6b2012-06-18 20:07:15 +0200300 if (!sdata->dev)
301 continue;
302
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200303 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
304 local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
305 continue;
306
Johannes Berga6f38ac2012-07-04 12:49:59 +0200307 for (ac = 0; ac < n_acs; ac++) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200308 int ac_queue = sdata->vif.hw_queue[ac];
309
310 if (ac_queue == queue ||
311 (sdata->vif.cab_queue == queue &&
312 local->queue_stop_reasons[ac_queue] == 0 &&
313 skb_queue_empty(&local->pending[ac_queue])))
314 netif_wake_subqueue(sdata->dev, ac);
315 }
316 }
317}
318
Kalle Valoce7c9112008-12-18 23:35:20 +0200319static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300320 enum queue_stop_reason reason,
321 bool refcounted)
Johannes Bergc2d15602007-07-27 15:43:23 +0200322{
323 struct ieee80211_local *local = hw_to_local(hw);
324
Johannes Bergb5878a22010-04-07 16:48:40 +0200325 trace_wake_queue(local, queue, reason);
326
Johannes Berge4e72fb2009-03-23 17:28:42 +0100327 if (WARN_ON(queue >= hw->queues))
328 return;
Kalle Valoce7c9112008-12-18 23:35:20 +0200329
Johannes Bergada15122012-03-28 11:04:27 +0200330 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
331 return;
332
Luciano Coelhocca07b02014-06-13 16:30:05 +0300333 if (!refcounted)
334 local->q_stop_reasons[queue][reason] = 0;
335 else
336 local->q_stop_reasons[queue][reason]--;
337
338 if (local->q_stop_reasons[queue][reason] == 0)
339 __clear_bit(reason, &local->queue_stop_reasons[queue]);
Johannes Berg96f5e662009-02-12 00:51:53 +0100340
341 if (local->queue_stop_reasons[queue] != 0)
342 /* someone still has this queue stopped */
343 return;
344
Johannes Berg7236fe22010-03-22 13:42:43 -0700345 if (skb_queue_empty(&local->pending[queue])) {
346 rcu_read_lock();
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200347 ieee80211_propagate_queue_wake(local, queue);
Johannes Berg7236fe22010-03-22 13:42:43 -0700348 rcu_read_unlock();
349 } else
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200350 tasklet_schedule(&local->tx_pending_tasklet);
Johannes Bergc2d15602007-07-27 15:43:23 +0200351}
Kalle Valoce7c9112008-12-18 23:35:20 +0200352
Johannes Berg96f5e662009-02-12 00:51:53 +0100353void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300354 enum queue_stop_reason reason,
355 bool refcounted)
Kalle Valoce7c9112008-12-18 23:35:20 +0200356{
357 struct ieee80211_local *local = hw_to_local(hw);
358 unsigned long flags;
359
360 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Luciano Coelhocca07b02014-06-13 16:30:05 +0300361 __ieee80211_wake_queue(hw, queue, reason, refcounted);
Kalle Valoce7c9112008-12-18 23:35:20 +0200362 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
363}
364
365void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
366{
367 ieee80211_wake_queue_by_reason(hw, queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300368 IEEE80211_QUEUE_STOP_REASON_DRIVER,
369 false);
Kalle Valoce7c9112008-12-18 23:35:20 +0200370}
Johannes Bergc2d15602007-07-27 15:43:23 +0200371EXPORT_SYMBOL(ieee80211_wake_queue);
372
Kalle Valoce7c9112008-12-18 23:35:20 +0200373static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300374 enum queue_stop_reason reason,
375 bool refcounted)
Johannes Bergc2d15602007-07-27 15:43:23 +0200376{
377 struct ieee80211_local *local = hw_to_local(hw);
Johannes Bergcf0277e2010-01-05 18:00:58 +0100378 struct ieee80211_sub_if_data *sdata;
Johannes Berga6f38ac2012-07-04 12:49:59 +0200379 int n_acs = IEEE80211_NUM_ACS;
Johannes Bergc2d15602007-07-27 15:43:23 +0200380
Johannes Bergb5878a22010-04-07 16:48:40 +0200381 trace_stop_queue(local, queue, reason);
382
Johannes Berge4e72fb2009-03-23 17:28:42 +0100383 if (WARN_ON(queue >= hw->queues))
384 return;
Johannes Berg96f5e662009-02-12 00:51:53 +0100385
Luciano Coelhocca07b02014-06-13 16:30:05 +0300386 if (!refcounted)
387 local->q_stop_reasons[queue][reason] = 1;
388 else
389 local->q_stop_reasons[queue][reason]++;
Johannes Bergada15122012-03-28 11:04:27 +0200390
Luciano Coelhocca07b02014-06-13 16:30:05 +0300391 if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
392 return;
Johannes Bergcf0277e2010-01-05 18:00:58 +0100393
Johannes Berga6f38ac2012-07-04 12:49:59 +0200394 if (local->hw.queues < IEEE80211_NUM_ACS)
395 n_acs = 1;
396
Johannes Bergcf0277e2010-01-05 18:00:58 +0100397 rcu_read_lock();
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200398 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
399 int ac;
400
Johannes Bergf142c6b2012-06-18 20:07:15 +0200401 if (!sdata->dev)
402 continue;
403
Johannes Berga6f38ac2012-07-04 12:49:59 +0200404 for (ac = 0; ac < n_acs; ac++) {
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200405 if (sdata->vif.hw_queue[ac] == queue ||
406 sdata->vif.cab_queue == queue)
407 netif_stop_subqueue(sdata->dev, ac);
408 }
409 }
Johannes Bergcf0277e2010-01-05 18:00:58 +0100410 rcu_read_unlock();
Johannes Bergc2d15602007-07-27 15:43:23 +0200411}
Kalle Valoce7c9112008-12-18 23:35:20 +0200412
Johannes Berg96f5e662009-02-12 00:51:53 +0100413void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300414 enum queue_stop_reason reason,
415 bool refcounted)
Kalle Valoce7c9112008-12-18 23:35:20 +0200416{
417 struct ieee80211_local *local = hw_to_local(hw);
418 unsigned long flags;
419
420 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Luciano Coelhocca07b02014-06-13 16:30:05 +0300421 __ieee80211_stop_queue(hw, queue, reason, refcounted);
Kalle Valoce7c9112008-12-18 23:35:20 +0200422 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
423}
424
425void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
426{
427 ieee80211_stop_queue_by_reason(hw, queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300428 IEEE80211_QUEUE_STOP_REASON_DRIVER,
429 false);
Kalle Valoce7c9112008-12-18 23:35:20 +0200430}
Johannes Bergc2d15602007-07-27 15:43:23 +0200431EXPORT_SYMBOL(ieee80211_stop_queue);
432
Johannes Berg8f77f382009-06-07 21:58:37 +0200433void ieee80211_add_pending_skb(struct ieee80211_local *local,
434 struct sk_buff *skb)
435{
436 struct ieee80211_hw *hw = &local->hw;
437 unsigned long flags;
Johannes Berga7bc3762009-07-27 10:33:31 +0200438 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200439 int queue = info->hw_queue;
Johannes Berga7bc3762009-07-27 10:33:31 +0200440
441 if (WARN_ON(!info->control.vif)) {
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200442 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200443 return;
444 }
Johannes Berg8f77f382009-06-07 21:58:37 +0200445
446 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Luciano Coelhocca07b02014-06-13 16:30:05 +0300447 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
448 false);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200449 __skb_queue_tail(&local->pending[queue], skb);
Luciano Coelhocca07b02014-06-13 16:30:05 +0300450 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
451 false);
Johannes Berg8f77f382009-06-07 21:58:37 +0200452 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
453}
454
Johannes Berge3685e02014-02-20 11:19:58 +0100455void ieee80211_add_pending_skbs(struct ieee80211_local *local,
456 struct sk_buff_head *skbs)
Johannes Berg8f77f382009-06-07 21:58:37 +0200457{
458 struct ieee80211_hw *hw = &local->hw;
459 struct sk_buff *skb;
460 unsigned long flags;
Johannes Bergb0b97a82011-09-29 16:04:30 +0200461 int queue, i;
Johannes Berg8f77f382009-06-07 21:58:37 +0200462
463 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg8f77f382009-06-07 21:58:37 +0200464 while ((skb = skb_dequeue(skbs))) {
Johannes Berga7bc3762009-07-27 10:33:31 +0200465 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
466
467 if (WARN_ON(!info->control.vif)) {
Felix Fietkaud4fa14c2012-10-10 22:40:23 +0200468 ieee80211_free_txskb(&local->hw, skb);
Johannes Berga7bc3762009-07-27 10:33:31 +0200469 continue;
470 }
471
Johannes Berg3a25a8c2012-04-03 16:28:50 +0200472 queue = info->hw_queue;
Johannes Berg4644ae82012-03-28 11:04:28 +0200473
474 __ieee80211_stop_queue(hw, queue,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300475 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
476 false);
Johannes Berg4644ae82012-03-28 11:04:28 +0200477
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200478 __skb_queue_tail(&local->pending[queue], skb);
Johannes Berg8f77f382009-06-07 21:58:37 +0200479 }
480
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200481 for (i = 0; i < hw->queues; i++)
Johannes Berg8f77f382009-06-07 21:58:37 +0200482 __ieee80211_wake_queue(hw, i,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300483 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
484 false);
Johannes Berg8f77f382009-06-07 21:58:37 +0200485 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg8f77f382009-06-07 21:58:37 +0200486}
487
Kalle Valoce7c9112008-12-18 23:35:20 +0200488void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100489 unsigned long queues,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300490 enum queue_stop_reason reason,
491 bool refcounted)
Johannes Bergc2d15602007-07-27 15:43:23 +0200492{
Kalle Valoce7c9112008-12-18 23:35:20 +0200493 struct ieee80211_local *local = hw_to_local(hw);
494 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200495 int i;
496
Kalle Valoce7c9112008-12-18 23:35:20 +0200497 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
498
Johannes Berg445ea4e2013-02-13 12:25:28 +0100499 for_each_set_bit(i, &queues, hw->queues)
Luciano Coelhocca07b02014-06-13 16:30:05 +0300500 __ieee80211_stop_queue(hw, i, reason, refcounted);
Kalle Valoce7c9112008-12-18 23:35:20 +0200501
502 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
503}
504
505void ieee80211_stop_queues(struct ieee80211_hw *hw)
506{
Johannes Berg445ea4e2013-02-13 12:25:28 +0100507 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300508 IEEE80211_QUEUE_STOP_REASON_DRIVER,
509 false);
Johannes Bergc2d15602007-07-27 15:43:23 +0200510}
511EXPORT_SYMBOL(ieee80211_stop_queues);
512
Tomas Winkler92ab8532008-07-24 21:02:04 +0300513int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
514{
515 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200516 unsigned long flags;
517 int ret;
Johannes Berg96f5e662009-02-12 00:51:53 +0100518
Johannes Berge4e72fb2009-03-23 17:28:42 +0100519 if (WARN_ON(queue >= hw->queues))
520 return true;
Johannes Berg96f5e662009-02-12 00:51:53 +0100521
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200522 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Thomas Pedersen2419ea12013-04-10 15:41:40 -0700523 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
524 &local->queue_stop_reasons[queue]);
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200525 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
526 return ret;
Tomas Winkler92ab8532008-07-24 21:02:04 +0300527}
528EXPORT_SYMBOL(ieee80211_queue_stopped);
529
Kalle Valoce7c9112008-12-18 23:35:20 +0200530void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100531 unsigned long queues,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300532 enum queue_stop_reason reason,
533 bool refcounted)
Johannes Bergc2d15602007-07-27 15:43:23 +0200534{
Kalle Valoce7c9112008-12-18 23:35:20 +0200535 struct ieee80211_local *local = hw_to_local(hw);
536 unsigned long flags;
Johannes Bergc2d15602007-07-27 15:43:23 +0200537 int i;
538
Kalle Valoce7c9112008-12-18 23:35:20 +0200539 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
540
Johannes Berg445ea4e2013-02-13 12:25:28 +0100541 for_each_set_bit(i, &queues, hw->queues)
Luciano Coelhocca07b02014-06-13 16:30:05 +0300542 __ieee80211_wake_queue(hw, i, reason, refcounted);
Kalle Valoce7c9112008-12-18 23:35:20 +0200543
544 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
545}
546
547void ieee80211_wake_queues(struct ieee80211_hw *hw)
548{
Johannes Berg445ea4e2013-02-13 12:25:28 +0100549 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300550 IEEE80211_QUEUE_STOP_REASON_DRIVER,
551 false);
Johannes Bergc2d15602007-07-27 15:43:23 +0200552}
553EXPORT_SYMBOL(ieee80211_wake_queues);
Johannes Bergdabeb342007-11-09 01:57:29 +0100554
Johannes Berg39ecc012013-02-13 12:11:00 +0100555void ieee80211_flush_queues(struct ieee80211_local *local,
556 struct ieee80211_sub_if_data *sdata)
557{
558 u32 queues;
559
560 if (!local->ops->flush)
561 return;
562
563 if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
564 int ac;
565
566 queues = 0;
567
568 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
569 queues |= BIT(sdata->vif.hw_queue[ac]);
570 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
571 queues |= BIT(sdata->vif.cab_queue);
572 } else {
573 /* all queues */
574 queues = BIT(local->hw.queues) - 1;
575 }
576
Luciano Coelho59f48fe2014-06-13 16:30:04 +0300577 ieee80211_stop_queues_by_reason(&local->hw, queues,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300578 IEEE80211_QUEUE_STOP_REASON_FLUSH,
579 false);
Johannes Berg445ea4e2013-02-13 12:25:28 +0100580
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +0200581 drv_flush(local, sdata, queues, false);
Johannes Berg445ea4e2013-02-13 12:25:28 +0100582
Luciano Coelho59f48fe2014-06-13 16:30:04 +0300583 ieee80211_wake_queues_by_reason(&local->hw, queues,
Luciano Coelhocca07b02014-06-13 16:30:05 +0300584 IEEE80211_QUEUE_STOP_REASON_FLUSH,
585 false);
Johannes Berg39ecc012013-02-13 12:11:00 +0100586}
587
Johannes Bergc7c71062013-08-21 22:07:20 +0200588static void __iterate_active_interfaces(struct ieee80211_local *local,
589 u32 iter_flags,
590 void (*iterator)(void *data, u8 *mac,
591 struct ieee80211_vif *vif),
592 void *data)
Johannes Bergdabeb342007-11-09 01:57:29 +0100593{
Johannes Bergdabeb342007-11-09 01:57:29 +0100594 struct ieee80211_sub_if_data *sdata;
595
Johannes Berge38bad42007-11-28 10:55:32 +0100596 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +0100597 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200598 case NL80211_IFTYPE_MONITOR:
Felix Fietkau31eba5b2013-05-28 13:01:53 +0200599 if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
600 continue;
601 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200602 case NL80211_IFTYPE_AP_VLAN:
Johannes Bergdabeb342007-11-09 01:57:29 +0100603 continue;
Johannes Berg2ca27bc2010-09-16 14:58:23 +0200604 default:
Johannes Bergdabeb342007-11-09 01:57:29 +0100605 break;
606 }
Johannes Berg8b2c9822012-11-06 20:23:30 +0100607 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
608 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
609 continue;
Johannes Berg9607e6b2009-12-23 13:15:31 +0100610 if (ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +0100611 iterator(data, sdata->vif.addr,
Johannes Berg32bfd352007-12-19 01:31:26 +0100612 &sdata->vif);
Johannes Bergdabeb342007-11-09 01:57:29 +0100613 }
Johannes Berge38bad42007-11-28 10:55:32 +0100614
Johannes Bergc7c71062013-08-21 22:07:20 +0200615 sdata = rcu_dereference_check(local->monitor_sdata,
616 lockdep_is_held(&local->iflist_mtx) ||
617 lockdep_rtnl_is_held());
Johannes Berg8b2c9822012-11-06 20:23:30 +0100618 if (sdata &&
619 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
620 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
Johannes Berg685fb722012-07-11 16:38:09 +0200621 iterator(data, sdata->vif.addr, &sdata->vif);
Johannes Bergc7c71062013-08-21 22:07:20 +0200622}
Johannes Berg685fb722012-07-11 16:38:09 +0200623
Johannes Bergc7c71062013-08-21 22:07:20 +0200624void ieee80211_iterate_active_interfaces(
625 struct ieee80211_hw *hw, u32 iter_flags,
626 void (*iterator)(void *data, u8 *mac,
627 struct ieee80211_vif *vif),
628 void *data)
629{
630 struct ieee80211_local *local = hw_to_local(hw);
631
632 mutex_lock(&local->iflist_mtx);
633 __iterate_active_interfaces(local, iter_flags, iterator, data);
634 mutex_unlock(&local->iflist_mtx);
635}
636EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
637
638void ieee80211_iterate_active_interfaces_atomic(
639 struct ieee80211_hw *hw, u32 iter_flags,
640 void (*iterator)(void *data, u8 *mac,
641 struct ieee80211_vif *vif),
642 void *data)
643{
644 struct ieee80211_local *local = hw_to_local(hw);
645
646 rcu_read_lock();
647 __iterate_active_interfaces(local, iter_flags, iterator, data);
Johannes Berge38bad42007-11-28 10:55:32 +0100648 rcu_read_unlock();
Johannes Bergdabeb342007-11-09 01:57:29 +0100649}
Ivo van Doorn2f561fe2008-05-10 13:40:49 +0200650EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
Johannes Berg37ffc8d2008-09-08 16:40:36 +0200651
Johannes Bergc7c71062013-08-21 22:07:20 +0200652void ieee80211_iterate_active_interfaces_rtnl(
653 struct ieee80211_hw *hw, u32 iter_flags,
654 void (*iterator)(void *data, u8 *mac,
655 struct ieee80211_vif *vif),
656 void *data)
657{
658 struct ieee80211_local *local = hw_to_local(hw);
659
660 ASSERT_RTNL();
661
662 __iterate_active_interfaces(local, iter_flags, iterator, data);
663}
664EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
665
Johannes Bergad7e7182013-11-13 13:37:47 +0100666struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
667{
668 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
669
670 if (!ieee80211_sdata_running(sdata) ||
671 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
672 return NULL;
673 return &sdata->vif;
674}
675EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
676
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400677/*
678 * Nothing should have been stuffed into the workqueue during
679 * the suspend->resume cycle. If this WARN is seen then there
680 * is a bug with either the driver suspend or something in
681 * mac80211 stuffing into the workqueue which we haven't yet
682 * cleared during mac80211's suspend cycle.
683 */
684static bool ieee80211_can_queue_work(struct ieee80211_local *local)
685{
Johannes Bergceb99fe2009-11-19 14:29:39 +0100686 if (WARN(local->suspended && !local->resuming,
687 "queueing ieee80211 work while going to suspend\n"))
688 return false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400689
690 return true;
691}
692
693void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
694{
695 struct ieee80211_local *local = hw_to_local(hw);
696
697 if (!ieee80211_can_queue_work(local))
698 return;
699
700 queue_work(local->workqueue, work);
701}
702EXPORT_SYMBOL(ieee80211_queue_work);
703
704void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
705 struct delayed_work *dwork,
706 unsigned long delay)
707{
708 struct ieee80211_local *local = hw_to_local(hw);
709
710 if (!ieee80211_can_queue_work(local))
711 return;
712
713 queue_delayed_work(local->workqueue, dwork, delay);
714}
715EXPORT_SYMBOL(ieee80211_queue_delayed_work);
716
Johannes Berg35d865a2013-05-28 10:54:03 +0200717u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
Johannes Bergdd769862011-11-18 16:54:50 +0100718 struct ieee802_11_elems *elems,
719 u64 filter, u32 crc)
720{
721 size_t left = len;
Johannes Berg35d865a2013-05-28 10:54:03 +0200722 const u8 *pos = start;
Johannes Bergdd769862011-11-18 16:54:50 +0100723 bool calc_crc = filter != 0;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800724 DECLARE_BITMAP(seen_elems, 256);
Johannes Bergb2e506b2013-03-26 14:54:16 +0100725 const u8 *ie;
Johannes Bergdd769862011-11-18 16:54:50 +0100726
Paul Stewartfcff4f12012-02-23 17:59:53 -0800727 bitmap_zero(seen_elems, 256);
Johannes Bergdd769862011-11-18 16:54:50 +0100728 memset(elems, 0, sizeof(*elems));
729 elems->ie_start = start;
730 elems->total_len = len;
731
732 while (left >= 2) {
733 u8 id, elen;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800734 bool elem_parse_failed;
Johannes Bergdd769862011-11-18 16:54:50 +0100735
736 id = *pos++;
737 elen = *pos++;
738 left -= 2;
739
Paul Stewartfcff4f12012-02-23 17:59:53 -0800740 if (elen > left) {
741 elems->parse_error = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100742 break;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800743 }
744
Johannes Berg9690fb12012-10-24 14:19:53 +0200745 switch (id) {
746 case WLAN_EID_SSID:
747 case WLAN_EID_SUPP_RATES:
748 case WLAN_EID_FH_PARAMS:
749 case WLAN_EID_DS_PARAMS:
750 case WLAN_EID_CF_PARAMS:
751 case WLAN_EID_TIM:
752 case WLAN_EID_IBSS_PARAMS:
753 case WLAN_EID_CHALLENGE:
754 case WLAN_EID_RSN:
755 case WLAN_EID_ERP_INFO:
756 case WLAN_EID_EXT_SUPP_RATES:
757 case WLAN_EID_HT_CAPABILITY:
758 case WLAN_EID_HT_OPERATION:
759 case WLAN_EID_VHT_CAPABILITY:
760 case WLAN_EID_VHT_OPERATION:
761 case WLAN_EID_MESH_ID:
762 case WLAN_EID_MESH_CONFIG:
763 case WLAN_EID_PEER_MGMT:
764 case WLAN_EID_PREQ:
765 case WLAN_EID_PREP:
766 case WLAN_EID_PERR:
767 case WLAN_EID_RANN:
768 case WLAN_EID_CHANNEL_SWITCH:
769 case WLAN_EID_EXT_CHANSWITCH_ANN:
770 case WLAN_EID_COUNTRY:
771 case WLAN_EID_PWR_CONSTRAINT:
772 case WLAN_EID_TIMEOUT_INTERVAL:
Johannes Berg85220d72013-03-25 18:29:27 +0100773 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
Johannes Bergb2e506b2013-03-26 14:54:16 +0100774 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -0700775 case WLAN_EID_CHAN_SWITCH_PARAM:
Johannes Bergb2e506b2013-03-26 14:54:16 +0100776 /*
777 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
778 * that if the content gets bigger it might be needed more than once
779 */
Johannes Berg9690fb12012-10-24 14:19:53 +0200780 if (test_bit(id, seen_elems)) {
781 elems->parse_error = true;
782 left -= elen;
783 pos += elen;
784 continue;
785 }
786 break;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800787 }
Johannes Bergdd769862011-11-18 16:54:50 +0100788
789 if (calc_crc && id < 64 && (filter & (1ULL << id)))
790 crc = crc32_be(crc, pos - 2, elen + 2);
791
Paul Stewartfcff4f12012-02-23 17:59:53 -0800792 elem_parse_failed = false;
793
Johannes Bergdd769862011-11-18 16:54:50 +0100794 switch (id) {
795 case WLAN_EID_SSID:
796 elems->ssid = pos;
797 elems->ssid_len = elen;
798 break;
799 case WLAN_EID_SUPP_RATES:
800 elems->supp_rates = pos;
801 elems->supp_rates_len = elen;
802 break;
Johannes Bergdd769862011-11-18 16:54:50 +0100803 case WLAN_EID_DS_PARAMS:
Johannes Berg1cd8e882013-03-27 14:30:12 +0100804 if (elen >= 1)
805 elems->ds_params = pos;
806 else
807 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100808 break;
Johannes Bergdd769862011-11-18 16:54:50 +0100809 case WLAN_EID_TIM:
810 if (elen >= sizeof(struct ieee80211_tim_ie)) {
811 elems->tim = (void *)pos;
812 elems->tim_len = elen;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800813 } else
814 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100815 break;
Johannes Bergdd769862011-11-18 16:54:50 +0100816 case WLAN_EID_CHALLENGE:
817 elems->challenge = pos;
818 elems->challenge_len = elen;
819 break;
820 case WLAN_EID_VENDOR_SPECIFIC:
821 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
822 pos[2] == 0xf2) {
823 /* Microsoft OUI (00:50:F2) */
824
825 if (calc_crc)
826 crc = crc32_be(crc, pos - 2, elen + 2);
827
Johannes Berg441a33b2013-02-12 16:27:04 +0100828 if (elen >= 5 && pos[3] == 2) {
Johannes Bergdd769862011-11-18 16:54:50 +0100829 /* OUI Type 2 - WMM IE */
830 if (pos[4] == 0) {
831 elems->wmm_info = pos;
832 elems->wmm_info_len = elen;
833 } else if (pos[4] == 1) {
834 elems->wmm_param = pos;
835 elems->wmm_param_len = elen;
836 }
837 }
838 }
839 break;
840 case WLAN_EID_RSN:
841 elems->rsn = pos;
842 elems->rsn_len = elen;
843 break;
844 case WLAN_EID_ERP_INFO:
Johannes Berg1946bed2013-03-27 14:31:53 +0100845 if (elen >= 1)
846 elems->erp_info = pos;
847 else
848 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100849 break;
850 case WLAN_EID_EXT_SUPP_RATES:
851 elems->ext_supp_rates = pos;
852 elems->ext_supp_rates_len = elen;
853 break;
854 case WLAN_EID_HT_CAPABILITY:
855 if (elen >= sizeof(struct ieee80211_ht_cap))
856 elems->ht_cap_elem = (void *)pos;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800857 else
858 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100859 break;
Johannes Berg074d46d2012-03-15 19:45:16 +0100860 case WLAN_EID_HT_OPERATION:
861 if (elen >= sizeof(struct ieee80211_ht_operation))
862 elems->ht_operation = (void *)pos;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800863 else
864 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100865 break;
Mahesh Palivela818255e2012-10-10 11:33:04 +0000866 case WLAN_EID_VHT_CAPABILITY:
867 if (elen >= sizeof(struct ieee80211_vht_cap))
868 elems->vht_cap_elem = (void *)pos;
869 else
870 elem_parse_failed = true;
871 break;
872 case WLAN_EID_VHT_OPERATION:
873 if (elen >= sizeof(struct ieee80211_vht_operation))
874 elems->vht_operation = (void *)pos;
875 else
876 elem_parse_failed = true;
877 break;
Johannes Bergbee7f582013-02-07 22:24:55 +0100878 case WLAN_EID_OPMODE_NOTIF:
879 if (elen > 0)
880 elems->opmode_notif = pos;
881 else
882 elem_parse_failed = true;
883 break;
Johannes Bergdd769862011-11-18 16:54:50 +0100884 case WLAN_EID_MESH_ID:
885 elems->mesh_id = pos;
886 elems->mesh_id_len = elen;
887 break;
888 case WLAN_EID_MESH_CONFIG:
889 if (elen >= sizeof(struct ieee80211_meshconf_ie))
890 elems->mesh_config = (void *)pos;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800891 else
892 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100893 break;
894 case WLAN_EID_PEER_MGMT:
895 elems->peering = pos;
896 elems->peering_len = elen;
897 break;
Marco Porsch3f52b7e2013-01-30 18:14:08 +0100898 case WLAN_EID_MESH_AWAKE_WINDOW:
899 if (elen >= 2)
900 elems->awake_window = (void *)pos;
901 break;
Johannes Bergdd769862011-11-18 16:54:50 +0100902 case WLAN_EID_PREQ:
903 elems->preq = pos;
904 elems->preq_len = elen;
905 break;
906 case WLAN_EID_PREP:
907 elems->prep = pos;
908 elems->prep_len = elen;
909 break;
910 case WLAN_EID_PERR:
911 elems->perr = pos;
912 elems->perr_len = elen;
913 break;
914 case WLAN_EID_RANN:
915 if (elen >= sizeof(struct ieee80211_rann_ie))
916 elems->rann = (void *)pos;
Paul Stewartfcff4f12012-02-23 17:59:53 -0800917 else
918 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100919 break;
920 case WLAN_EID_CHANNEL_SWITCH:
Johannes Berg5bc14202012-08-01 16:13:02 +0200921 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
922 elem_parse_failed = true;
923 break;
924 }
925 elems->ch_switch_ie = (void *)pos;
Johannes Bergdd769862011-11-18 16:54:50 +0100926 break;
Johannes Bergb4f286a12013-03-26 14:13:58 +0100927 case WLAN_EID_EXT_CHANSWITCH_ANN:
928 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
929 elem_parse_failed = true;
930 break;
931 }
932 elems->ext_chansw_ie = (void *)pos;
933 break;
Johannes Berg85220d72013-03-25 18:29:27 +0100934 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
935 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
936 elem_parse_failed = true;
937 break;
938 }
939 elems->sec_chan_offs = (void *)pos;
940 break;
Chun-Yeow Yeoh8f2535b2013-10-14 19:08:27 -0700941 case WLAN_EID_CHAN_SWITCH_PARAM:
942 if (elen !=
943 sizeof(*elems->mesh_chansw_params_ie)) {
944 elem_parse_failed = true;
945 break;
946 }
947 elems->mesh_chansw_params_ie = (void *)pos;
948 break;
Johannes Bergb2e506b2013-03-26 14:54:16 +0100949 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
950 if (!action ||
951 elen != sizeof(*elems->wide_bw_chansw_ie)) {
952 elem_parse_failed = true;
953 break;
954 }
955 elems->wide_bw_chansw_ie = (void *)pos;
956 break;
957 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
958 if (action) {
959 elem_parse_failed = true;
960 break;
961 }
962 /*
963 * This is a bit tricky, but as we only care about
964 * the wide bandwidth channel switch element, so
965 * just parse it out manually.
966 */
967 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
968 pos, elen);
969 if (ie) {
970 if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
971 elems->wide_bw_chansw_ie =
972 (void *)(ie + 2);
973 else
974 elem_parse_failed = true;
975 }
976 break;
Johannes Bergdd769862011-11-18 16:54:50 +0100977 case WLAN_EID_COUNTRY:
978 elems->country_elem = pos;
979 elems->country_elem_len = elen;
980 break;
981 case WLAN_EID_PWR_CONSTRAINT:
Johannes Berg761a48d2012-09-05 13:07:00 +0200982 if (elen != 1) {
983 elem_parse_failed = true;
984 break;
985 }
Johannes Bergdd769862011-11-18 16:54:50 +0100986 elems->pwr_constr_elem = pos;
Johannes Bergdd769862011-11-18 16:54:50 +0100987 break;
988 case WLAN_EID_TIMEOUT_INTERVAL:
Johannes Berg79ba1d82013-03-27 14:38:07 +0100989 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
990 elems->timeout_int = (void *)pos;
991 else
992 elem_parse_failed = true;
Johannes Bergdd769862011-11-18 16:54:50 +0100993 break;
994 default:
995 break;
996 }
997
Paul Stewartfcff4f12012-02-23 17:59:53 -0800998 if (elem_parse_failed)
999 elems->parse_error = true;
1000 else
Johannes Berg5df45692012-10-24 14:22:37 +02001001 __set_bit(id, seen_elems);
Paul Stewartfcff4f12012-02-23 17:59:53 -08001002
Johannes Bergdd769862011-11-18 16:54:50 +01001003 left -= elen;
1004 pos += elen;
1005 }
1006
Paul Stewartfcff4f12012-02-23 17:59:53 -08001007 if (left != 0)
1008 elems->parse_error = true;
1009
Johannes Bergdd769862011-11-18 16:54:50 +01001010 return crc;
1011}
1012
Johannes Berg3abead52012-03-02 15:56:59 +01001013void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1014 bool bss_notify)
Johannes Berg5825fe102008-09-09 12:56:01 +02001015{
1016 struct ieee80211_local *local = sdata->local;
1017 struct ieee80211_tx_queue_params qparam;
Johannes Berg55de9082012-07-26 17:24:39 +02001018 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Berg54bcbc62012-03-28 11:04:25 +02001019 int ac;
Stanislaw Gruszkaa8ce8542012-05-30 10:56:46 +02001020 bool use_11b, enable_qos;
Johannes Bergaa837e12009-05-07 16:16:24 +02001021 int aCWmin, aCWmax;
Johannes Berg5825fe102008-09-09 12:56:01 +02001022
1023 if (!local->ops->conf_tx)
1024 return;
1025
Johannes Berg54bcbc62012-03-28 11:04:25 +02001026 if (local->hw.queues < IEEE80211_NUM_ACS)
1027 return;
1028
Johannes Berg5825fe102008-09-09 12:56:01 +02001029 memset(&qparam, 0, sizeof(qparam));
1030
Johannes Berg55de9082012-07-26 17:24:39 +02001031 rcu_read_lock();
1032 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1033 use_11b = (chanctx_conf &&
Johannes Berg4bf88532012-11-09 11:39:59 +01001034 chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
Johannes Bergaa837e12009-05-07 16:16:24 +02001035 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
Johannes Berg55de9082012-07-26 17:24:39 +02001036 rcu_read_unlock();
Johannes Berg5825fe102008-09-09 12:56:01 +02001037
Stanislaw Gruszkaa8ce8542012-05-30 10:56:46 +02001038 /*
1039 * By default disable QoS in STA mode for old access points, which do
1040 * not support 802.11e. New APs will provide proper queue parameters,
1041 * that we will configure later.
1042 */
1043 enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
1044
Fred Zhou1f4ffde2013-09-09 23:03:41 +08001045 /* Set defaults according to 802.11-2007 Table 7-37 */
1046 aCWmax = 1023;
1047 if (use_11b)
1048 aCWmin = 31;
1049 else
1050 aCWmin = 15;
Johannes Berg5825fe102008-09-09 12:56:01 +02001051
Fred Zhou1f4ffde2013-09-09 23:03:41 +08001052 /* Confiure old 802.11b/g medium access rules. */
1053 qparam.cw_max = aCWmax;
1054 qparam.cw_min = aCWmin;
1055 qparam.txop = 0;
1056 qparam.aifs = 2;
1057
1058 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1059 /* Update if QoS is enabled. */
Stanislaw Gruszkaa8ce8542012-05-30 10:56:46 +02001060 if (enable_qos) {
1061 switch (ac) {
1062 case IEEE80211_AC_BK:
1063 qparam.cw_max = aCWmax;
1064 qparam.cw_min = aCWmin;
1065 qparam.txop = 0;
1066 qparam.aifs = 7;
1067 break;
1068 /* never happens but let's not leave undefined */
1069 default:
1070 case IEEE80211_AC_BE:
1071 qparam.cw_max = aCWmax;
1072 qparam.cw_min = aCWmin;
1073 qparam.txop = 0;
1074 qparam.aifs = 3;
1075 break;
1076 case IEEE80211_AC_VI:
1077 qparam.cw_max = aCWmin;
1078 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1079 if (use_11b)
1080 qparam.txop = 6016/32;
1081 else
1082 qparam.txop = 3008/32;
1083 qparam.aifs = 2;
1084 break;
1085 case IEEE80211_AC_VO:
1086 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1087 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1088 if (use_11b)
1089 qparam.txop = 3264/32;
1090 else
1091 qparam.txop = 1504/32;
1092 qparam.aifs = 2;
1093 break;
1094 }
Johannes Bergaa837e12009-05-07 16:16:24 +02001095 }
Johannes Berg5825fe102008-09-09 12:56:01 +02001096
Kalle Valoab133152010-01-12 10:42:31 +02001097 qparam.uapsd = false;
1098
Johannes Berg54bcbc62012-03-28 11:04:25 +02001099 sdata->tx_conf[ac] = qparam;
1100 drv_conf_tx(local, sdata, ac, &qparam);
Johannes Bergaa837e12009-05-07 16:16:24 +02001101 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001102
Johannes Bergf142c6b2012-06-18 20:07:15 +02001103 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1104 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
Stanislaw Gruszkaa8ce8542012-05-30 10:56:46 +02001105 sdata->vif.bss_conf.qos = enable_qos;
Johannes Berg3abead52012-03-02 15:56:59 +01001106 if (bss_notify)
1107 ieee80211_bss_info_change_notify(sdata,
1108 BSS_CHANGED_QOS);
Sujithd9734972010-07-23 10:47:11 +05301109 }
Johannes Berg5825fe102008-09-09 12:56:01 +02001110}
Johannes Berge50db652008-09-09 15:07:09 +02001111
Johannes Berg46900292009-02-15 12:44:28 +01001112void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001113 u16 transaction, u16 auth_alg, u16 status,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001114 const u8 *extra, size_t extra_len, const u8 *da,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001115 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1116 u32 tx_flags)
Johannes Berg46900292009-02-15 12:44:28 +01001117{
1118 struct ieee80211_local *local = sdata->local;
1119 struct sk_buff *skb;
1120 struct ieee80211_mgmt *mgmt;
Johannes Bergfffd0932009-07-08 14:22:54 +02001121 int err;
Johannes Berg46900292009-02-15 12:44:28 +01001122
Fred Zhou15e230a2013-09-24 10:33:01 +08001123 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1124 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24 + 6 + extra_len);
Joe Perchesd15b8452011-08-29 14:17:31 -07001125 if (!skb)
Johannes Berg46900292009-02-15 12:44:28 +01001126 return;
Joe Perchesd15b8452011-08-29 14:17:31 -07001127
Johannes Berg46900292009-02-15 12:44:28 +01001128 skb_reserve(skb, local->hw.extra_tx_headroom);
1129
1130 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
1131 memset(mgmt, 0, 24 + 6);
1132 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1133 IEEE80211_STYPE_AUTH);
Antonio Quartulliefa6a092012-01-09 19:43:06 +01001134 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001135 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01001136 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1137 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1138 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001139 mgmt->u.auth.status_code = cpu_to_le16(status);
Johannes Berg46900292009-02-15 12:44:28 +01001140 if (extra)
1141 memcpy(skb_put(skb, extra_len), extra, extra_len);
Johannes Berg46900292009-02-15 12:44:28 +01001142
Johannes Bergfffd0932009-07-08 14:22:54 +02001143 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1144 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1145 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1146 WARN_ON(err);
1147 }
1148
Johannes Berg1672c0e32013-01-29 15:02:27 +01001149 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1150 tx_flags;
Johannes Berg62ae67b2009-11-18 18:42:05 +01001151 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +01001152}
1153
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001154void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1155 const u8 *bssid, u16 stype, u16 reason,
1156 bool send_frame, u8 *frame_buf)
1157{
1158 struct ieee80211_local *local = sdata->local;
1159 struct sk_buff *skb;
1160 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1161
1162 /* build frame */
1163 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1164 mgmt->duration = 0; /* initialize only */
1165 mgmt->seq_ctrl = 0; /* initialize only */
1166 memcpy(mgmt->da, bssid, ETH_ALEN);
1167 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1168 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1169 /* u.deauth.reason_code == u.disassoc.reason_code */
1170 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1171
1172 if (send_frame) {
1173 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1174 IEEE80211_DEAUTH_FRAME_LEN);
1175 if (!skb)
1176 return;
1177
1178 skb_reserve(skb, local->hw.extra_tx_headroom);
1179
1180 /* copy in frame */
1181 memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
1182 mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1183
1184 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1185 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1186 IEEE80211_SKB_CB(skb)->flags |=
1187 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1188
1189 ieee80211_tx_skb(sdata, skb);
1190 }
1191}
1192
Johannes Bergde95a542009-04-01 11:58:36 +02001193int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
Johannes Bergc604b9f2012-11-29 12:45:18 +01001194 size_t buffer_len, const u8 *ie, size_t ie_len,
Jouni Malinen651b5222010-08-28 19:37:51 +03001195 enum ieee80211_band band, u32 rate_mask,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001196 struct cfg80211_chan_def *chandef)
Johannes Bergde95a542009-04-01 11:58:36 +02001197{
1198 struct ieee80211_supported_band *sband;
Johannes Bergc604b9f2012-11-29 12:45:18 +01001199 u8 *pos = buffer, *end = buffer + buffer_len;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001200 size_t offset = 0, noffset;
1201 int supp_rates_len, i;
Jouni Malinen8dcb2002010-08-28 19:36:10 +03001202 u8 rates[32];
1203 int num_rates;
1204 int ext_rates_len;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001205 int shift;
1206 u32 rate_flags;
Johannes Bergde95a542009-04-01 11:58:36 +02001207
Johannes Berg4d36ec582009-10-27 20:59:55 +01001208 sband = local->hw.wiphy->bands[band];
Arik Nemtsovd811b3d2012-07-09 19:57:28 +03001209 if (WARN_ON_ONCE(!sband))
1210 return 0;
Johannes Bergde95a542009-04-01 11:58:36 +02001211
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001212 rate_flags = ieee80211_chandef_rate_flags(chandef);
1213 shift = ieee80211_chandef_get_shift(chandef);
1214
Jouni Malinen8dcb2002010-08-28 19:36:10 +03001215 num_rates = 0;
1216 for (i = 0; i < sband->n_bitrates; i++) {
1217 if ((BIT(i) & rate_mask) == 0)
1218 continue; /* skip rate */
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001219 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1220 continue;
1221
1222 rates[num_rates++] =
1223 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1224 (1 << shift) * 5);
Jouni Malinen8dcb2002010-08-28 19:36:10 +03001225 }
1226
1227 supp_rates_len = min_t(int, num_rates, 8);
Johannes Berg8e664fb2009-12-23 13:15:38 +01001228
Johannes Bergc604b9f2012-11-29 12:45:18 +01001229 if (end - pos < 2 + supp_rates_len)
1230 goto out_err;
Johannes Bergde95a542009-04-01 11:58:36 +02001231 *pos++ = WLAN_EID_SUPP_RATES;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001232 *pos++ = supp_rates_len;
Jouni Malinen8dcb2002010-08-28 19:36:10 +03001233 memcpy(pos, rates, supp_rates_len);
1234 pos += supp_rates_len;
Johannes Bergde95a542009-04-01 11:58:36 +02001235
Johannes Berg8e664fb2009-12-23 13:15:38 +01001236 /* insert "request information" if in custom IEs */
1237 if (ie && ie_len) {
1238 static const u8 before_extrates[] = {
1239 WLAN_EID_SSID,
1240 WLAN_EID_SUPP_RATES,
1241 WLAN_EID_REQUEST,
1242 };
1243 noffset = ieee80211_ie_split(ie, ie_len,
1244 before_extrates,
1245 ARRAY_SIZE(before_extrates),
1246 offset);
Johannes Bergc604b9f2012-11-29 12:45:18 +01001247 if (end - pos < noffset - offset)
1248 goto out_err;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001249 memcpy(pos, ie + offset, noffset - offset);
1250 pos += noffset - offset;
1251 offset = noffset;
1252 }
Johannes Bergde95a542009-04-01 11:58:36 +02001253
Jouni Malinen8dcb2002010-08-28 19:36:10 +03001254 ext_rates_len = num_rates - supp_rates_len;
1255 if (ext_rates_len > 0) {
Johannes Bergc604b9f2012-11-29 12:45:18 +01001256 if (end - pos < 2 + ext_rates_len)
1257 goto out_err;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001258 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Jouni Malinen8dcb2002010-08-28 19:36:10 +03001259 *pos++ = ext_rates_len;
1260 memcpy(pos, rates + supp_rates_len, ext_rates_len);
1261 pos += ext_rates_len;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001262 }
1263
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001264 if (chandef->chan && sband->band == IEEE80211_BAND_2GHZ) {
Johannes Bergc604b9f2012-11-29 12:45:18 +01001265 if (end - pos < 3)
1266 goto out_err;
Jouni Malinen651b5222010-08-28 19:37:51 +03001267 *pos++ = WLAN_EID_DS_PARAMS;
1268 *pos++ = 1;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001269 *pos++ = ieee80211_frequency_to_channel(
1270 chandef->chan->center_freq);
Jouni Malinen651b5222010-08-28 19:37:51 +03001271 }
1272
Johannes Berg8e664fb2009-12-23 13:15:38 +01001273 /* insert custom IEs that go before HT */
1274 if (ie && ie_len) {
1275 static const u8 before_ht[] = {
1276 WLAN_EID_SSID,
1277 WLAN_EID_SUPP_RATES,
1278 WLAN_EID_REQUEST,
1279 WLAN_EID_EXT_SUPP_RATES,
1280 WLAN_EID_DS_PARAMS,
1281 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1282 };
1283 noffset = ieee80211_ie_split(ie, ie_len,
1284 before_ht, ARRAY_SIZE(before_ht),
1285 offset);
Johannes Bergc604b9f2012-11-29 12:45:18 +01001286 if (end - pos < noffset - offset)
1287 goto out_err;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001288 memcpy(pos, ie + offset, noffset - offset);
1289 pos += noffset - offset;
1290 offset = noffset;
Johannes Bergde95a542009-04-01 11:58:36 +02001291 }
1292
Johannes Bergc604b9f2012-11-29 12:45:18 +01001293 if (sband->ht_cap.ht_supported) {
1294 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1295 goto out_err;
Ben Greearef96a8422011-11-18 11:32:00 -08001296 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1297 sband->ht_cap.cap);
Johannes Bergc604b9f2012-11-29 12:45:18 +01001298 }
Johannes Berg5ef2d412009-03-31 12:12:07 +02001299
Johannes Bergde95a542009-04-01 11:58:36 +02001300 /*
1301 * If adding more here, adjust code in main.c
1302 * that calculates local->scan_ies_len.
1303 */
1304
Johannes Berg4d952302014-02-04 09:48:34 +01001305 /* insert custom IEs that go before VHT */
Johannes Berg8e664fb2009-12-23 13:15:38 +01001306 if (ie && ie_len) {
Johannes Berg4d952302014-02-04 09:48:34 +01001307 static const u8 before_vht[] = {
1308 WLAN_EID_SSID,
1309 WLAN_EID_SUPP_RATES,
1310 WLAN_EID_REQUEST,
1311 WLAN_EID_EXT_SUPP_RATES,
1312 WLAN_EID_DS_PARAMS,
1313 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1314 WLAN_EID_HT_CAPABILITY,
1315 WLAN_EID_BSS_COEX_2040,
1316 WLAN_EID_EXT_CAPABILITY,
1317 WLAN_EID_SSID_LIST,
1318 WLAN_EID_CHANNEL_USAGE,
1319 WLAN_EID_INTERWORKING,
1320 /* mesh ID can't happen here */
1321 /* 60 GHz can't happen here right now */
1322 };
1323 noffset = ieee80211_ie_split(ie, ie_len,
1324 before_vht, ARRAY_SIZE(before_vht),
1325 offset);
Johannes Bergc604b9f2012-11-29 12:45:18 +01001326 if (end - pos < noffset - offset)
1327 goto out_err;
Johannes Berg8e664fb2009-12-23 13:15:38 +01001328 memcpy(pos, ie + offset, noffset - offset);
1329 pos += noffset - offset;
Johannes Berg4d952302014-02-04 09:48:34 +01001330 offset = noffset;
Johannes Bergde95a542009-04-01 11:58:36 +02001331 }
1332
Johannes Bergc604b9f2012-11-29 12:45:18 +01001333 if (sband->vht_cap.vht_supported) {
1334 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1335 goto out_err;
Mahesh Palivelaba0afa2f2012-07-02 11:25:12 +00001336 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1337 sband->vht_cap.cap);
Johannes Bergc604b9f2012-11-29 12:45:18 +01001338 }
Mahesh Palivelaba0afa2f2012-07-02 11:25:12 +00001339
Johannes Berg4d952302014-02-04 09:48:34 +01001340 /* add any remaining custom IEs */
1341 if (ie && ie_len) {
1342 noffset = ie_len;
1343 if (end - pos < noffset - offset)
1344 goto out_err;
1345 memcpy(pos, ie + offset, noffset - offset);
1346 pos += noffset - offset;
1347 }
1348
Johannes Bergde95a542009-04-01 11:58:36 +02001349 return pos - buffer;
Johannes Bergc604b9f2012-11-29 12:45:18 +01001350 out_err:
1351 WARN_ONCE(1, "not enough space for preq IEs\n");
1352 return pos - buffer;
Johannes Bergde95a542009-04-01 11:58:36 +02001353}
1354
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001355struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Berg85a237f2011-07-18 18:08:36 +02001356 u8 *dst, u32 ratemask,
Johannes Berg6b778632012-07-23 14:53:27 +02001357 struct ieee80211_channel *chan,
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001358 const u8 *ssid, size_t ssid_len,
Paul Stewarta806c552011-06-23 09:00:11 -08001359 const u8 *ie, size_t ie_len,
1360 bool directed)
Johannes Berg46900292009-02-15 12:44:28 +01001361{
1362 struct ieee80211_local *local = sdata->local;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001363 struct cfg80211_chan_def chandef;
Johannes Berg46900292009-02-15 12:44:28 +01001364 struct sk_buff *skb;
1365 struct ieee80211_mgmt *mgmt;
Johannes Bergb9a9ada2012-11-29 13:00:10 +01001366 int ies_len;
Johannes Berg46900292009-02-15 12:44:28 +01001367
Paul Stewarta806c552011-06-23 09:00:11 -08001368 /*
1369 * Do not send DS Channel parameter for directed probe requests
1370 * in order to maximize the chance that we get a response. Some
1371 * badly-behaved APs don't respond when this parameter is included.
1372 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001373 chandef.width = sdata->vif.bss_conf.chandef.width;
Paul Stewarta806c552011-06-23 09:00:11 -08001374 if (directed)
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001375 chandef.chan = NULL;
Paul Stewarta806c552011-06-23 09:00:11 -08001376 else
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001377 chandef.chan = chan;
Jouni Malinen651b5222010-08-28 19:37:51 +03001378
Kalle Valo7c12ce82010-01-05 20:16:44 +02001379 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
Johannes Bergb9a9ada2012-11-29 13:00:10 +01001380 ssid, ssid_len, 100 + ie_len);
Johannes Berg5b2bbf72011-11-08 13:04:41 +01001381 if (!skb)
Johannes Bergb9a9ada2012-11-29 13:00:10 +01001382 return NULL;
1383
1384 ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1385 skb_tailroom(skb),
Johannes Bergc604b9f2012-11-29 12:45:18 +01001386 ie, ie_len, chan->band,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001387 ratemask, &chandef);
Johannes Bergb9a9ada2012-11-29 13:00:10 +01001388 skb_put(skb, ies_len);
Kalle Valo7c12ce82010-01-05 20:16:44 +02001389
Johannes Berg46900292009-02-15 12:44:28 +01001390 if (dst) {
Kalle Valo7c12ce82010-01-05 20:16:44 +02001391 mgmt = (struct ieee80211_mgmt *) skb->data;
Johannes Berg46900292009-02-15 12:44:28 +01001392 memcpy(mgmt->da, dst, ETH_ALEN);
1393 memcpy(mgmt->bssid, dst, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01001394 }
Johannes Berg46900292009-02-15 12:44:28 +01001395
Johannes Berg62ae67b2009-11-18 18:42:05 +01001396 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg5b2bbf72011-11-08 13:04:41 +01001397
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001398 return skb;
1399}
1400
1401void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1402 const u8 *ssid, size_t ssid_len,
Paul Stewarta806c552011-06-23 09:00:11 -08001403 const u8 *ie, size_t ie_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001404 u32 ratemask, bool directed, u32 tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02001405 struct ieee80211_channel *channel, bool scan)
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001406{
1407 struct sk_buff *skb;
1408
Johannes Bergfe94fe02012-07-30 12:26:34 +02001409 skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001410 ssid, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001411 ie, ie_len, directed);
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +05301412 if (skb) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01001413 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
Johannes Berg55de9082012-07-26 17:24:39 +02001414 if (scan)
1415 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
1416 else
1417 ieee80211_tx_skb(sdata, skb);
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +05301418 }
Johannes Berg46900292009-02-15 12:44:28 +01001419}
1420
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001421u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
Johannes Berg46900292009-02-15 12:44:28 +01001422 struct ieee802_11_elems *elems,
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -07001423 enum ieee80211_band band, u32 *basic_rates)
Johannes Berg46900292009-02-15 12:44:28 +01001424{
1425 struct ieee80211_supported_band *sband;
Johannes Berg46900292009-02-15 12:44:28 +01001426 size_t num_rates;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001427 u32 supp_rates, rate_flags;
1428 int i, j, shift;
1429 sband = sdata->local->hw.wiphy->bands[band];
1430
1431 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
1432 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg46900292009-02-15 12:44:28 +01001433
Michal Kazior4ee73f32012-04-11 08:47:56 +02001434 if (WARN_ON(!sband))
1435 return 1;
Johannes Berg46900292009-02-15 12:44:28 +01001436
Johannes Berg46900292009-02-15 12:44:28 +01001437 num_rates = sband->n_bitrates;
1438 supp_rates = 0;
1439 for (i = 0; i < elems->supp_rates_len +
1440 elems->ext_supp_rates_len; i++) {
1441 u8 rate = 0;
1442 int own_rate;
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -07001443 bool is_basic;
Johannes Berg46900292009-02-15 12:44:28 +01001444 if (i < elems->supp_rates_len)
1445 rate = elems->supp_rates[i];
1446 else if (elems->ext_supp_rates)
1447 rate = elems->ext_supp_rates
1448 [i - elems->supp_rates_len];
1449 own_rate = 5 * (rate & 0x7f);
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -07001450 is_basic = !!(rate & 0x80);
1451
1452 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1453 continue;
1454
1455 for (j = 0; j < num_rates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001456 int brate;
1457 if ((rate_flags & sband->bitrates[j].flags)
1458 != rate_flags)
1459 continue;
1460
1461 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
1462 1 << shift);
1463
1464 if (brate == own_rate) {
Johannes Berg46900292009-02-15 12:44:28 +01001465 supp_rates |= BIT(j);
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -07001466 if (basic_rates && is_basic)
1467 *basic_rates |= BIT(j);
1468 }
1469 }
Johannes Berg46900292009-02-15 12:44:28 +01001470 }
1471 return supp_rates;
1472}
Johannes Bergf2753dd2009-04-14 10:09:24 +02001473
Johannes Berg84f6a012009-08-20 20:02:20 +02001474void ieee80211_stop_device(struct ieee80211_local *local)
1475{
1476 ieee80211_led_radio(local, false);
Johannes Berg67408c82010-11-30 08:59:23 +01001477 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
Johannes Berg84f6a012009-08-20 20:02:20 +02001478
1479 cancel_work_sync(&local->reconfig_filter);
Johannes Berg84f6a012009-08-20 20:02:20 +02001480
1481 flush_workqueue(local->workqueue);
Lennert Buytenhek678f4152010-01-10 14:07:53 +01001482 drv_stop(local);
Johannes Berg84f6a012009-08-20 20:02:20 +02001483}
1484
Johannes Bergf6837ba2014-04-30 14:19:04 +02001485static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1486{
1487 struct ieee80211_sub_if_data *sdata;
1488 struct ieee80211_chanctx *ctx;
1489
1490 /*
1491 * We get here if during resume the device can't be restarted properly.
1492 * We might also get here if this happens during HW reset, which is a
1493 * slightly different situation and we need to drop all connections in
1494 * the latter case.
1495 *
1496 * Ask cfg80211 to turn off all interfaces, this will result in more
1497 * warnings but at least we'll then get into a clean stopped state.
1498 */
1499
1500 local->resuming = false;
1501 local->suspended = false;
1502 local->started = false;
1503
1504 /* scheduled scan clearly can't be running any more, but tell
1505 * cfg80211 and clear local state
1506 */
1507 ieee80211_sched_scan_end(local);
1508
1509 list_for_each_entry(sdata, &local->interfaces, list)
1510 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
1511
1512 /* Mark channel contexts as not being in the driver any more to avoid
1513 * removing them from the driver during the shutdown process...
1514 */
1515 mutex_lock(&local->chanctx_mtx);
1516 list_for_each_entry(ctx, &local->chanctx_list, list)
1517 ctx->driver_present = false;
1518 mutex_unlock(&local->chanctx_mtx);
1519
1520 cfg80211_shutdown_all_interfaces(local->hw.wiphy);
1521}
1522
Stanislaw Gruszka153a5fc2013-02-28 10:55:30 +01001523static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1524 struct ieee80211_sub_if_data *sdata)
1525{
1526 struct ieee80211_chanctx_conf *conf;
1527 struct ieee80211_chanctx *ctx;
1528
1529 if (!local->use_chanctx)
1530 return;
1531
1532 mutex_lock(&local->chanctx_mtx);
1533 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1534 lockdep_is_held(&local->chanctx_mtx));
1535 if (conf) {
1536 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1537 drv_assign_vif_chanctx(local, sdata, ctx);
1538 }
1539 mutex_unlock(&local->chanctx_mtx);
1540}
1541
Johannes Bergf2753dd2009-04-14 10:09:24 +02001542int ieee80211_reconfig(struct ieee80211_local *local)
1543{
1544 struct ieee80211_hw *hw = &local->hw;
1545 struct ieee80211_sub_if_data *sdata;
Johannes Berg55de9082012-07-26 17:24:39 +02001546 struct ieee80211_chanctx *ctx;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001547 struct sta_info *sta;
Eliad Peller2683d652011-07-14 20:29:42 +03001548 int res, i;
Johannes Bergd8881302013-01-10 23:55:33 +01001549 bool reconfig_due_to_wowlan = false;
David Spinadeld43c6b62013-12-08 21:48:57 +02001550 struct ieee80211_sub_if_data *sched_scan_sdata;
1551 bool sched_scan_stopped = false;
Johannes Bergd8881302013-01-10 23:55:33 +01001552
Johannes Berg8f21b0a2013-01-11 00:28:01 +01001553#ifdef CONFIG_PM
Johannes Bergceb99fe2009-11-19 14:29:39 +01001554 if (local->suspended)
1555 local->resuming = true;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001556
Johannes Bergeecc4802011-05-04 15:37:29 +02001557 if (local->wowlan) {
Johannes Bergeecc4802011-05-04 15:37:29 +02001558 res = drv_resume(local);
Johannes Berg27b3eb92013-08-07 20:11:55 +02001559 local->wowlan = false;
Johannes Bergeecc4802011-05-04 15:37:29 +02001560 if (res < 0) {
1561 local->resuming = false;
1562 return res;
1563 }
1564 if (res == 0)
1565 goto wake_up;
1566 WARN_ON(res > 1);
1567 /*
1568 * res is 1, which means the driver requested
1569 * to go through a regular reset on wakeup.
1570 */
Johannes Bergd8881302013-01-10 23:55:33 +01001571 reconfig_due_to_wowlan = true;
Johannes Bergeecc4802011-05-04 15:37:29 +02001572 }
1573#endif
Johannes Berg94f9b972011-07-14 16:48:54 +02001574 /* everything else happens only if HW was up & running */
1575 if (!local->open_count)
1576 goto wake_up;
1577
1578 /*
1579 * Upon resume hardware can sometimes be goofy due to
1580 * various platform / driver / bus issues, so restarting
1581 * the device may at times not work immediately. Propagate
1582 * the error.
1583 */
1584 res = drv_start(local);
1585 if (res) {
Johannes Bergf6837ba2014-04-30 14:19:04 +02001586 if (local->suspended)
1587 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
1588 else
1589 WARN(1, "Hardware became unavailable during restart.\n");
1590 ieee80211_handle_reconfig_failure(local);
Johannes Berg94f9b972011-07-14 16:48:54 +02001591 return res;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001592 }
1593
Yogesh Ashok Powar7f281972011-12-30 16:34:25 +05301594 /* setup fragmentation threshold */
1595 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
1596
1597 /* setup RTS threshold */
1598 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1599
1600 /* reset coverage class */
1601 drv_set_coverage_class(local, hw->wiphy->coverage_class);
1602
Johannes Berg94f9b972011-07-14 16:48:54 +02001603 ieee80211_led_radio(local, true);
1604 ieee80211_mod_tpt_led_trig(local,
1605 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1606
Johannes Bergf2753dd2009-04-14 10:09:24 +02001607 /* add interfaces */
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001608 sdata = rtnl_dereference(local->monitor_sdata);
1609 if (sdata) {
Johannes Berg3c3e21e2013-03-27 23:20:27 +01001610 /* in HW restart it exists already */
1611 WARN_ON(local->resuming);
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001612 res = drv_add_interface(local, sdata);
1613 if (WARN_ON(res)) {
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301614 RCU_INIT_POINTER(local->monitor_sdata, NULL);
Johannes Berg4b6f1dd2012-04-03 14:35:57 +02001615 synchronize_net();
1616 kfree(sdata);
1617 }
1618 }
1619
Johannes Bergf2753dd2009-04-14 10:09:24 +02001620 list_for_each_entry(sdata, &local->interfaces, list) {
1621 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1622 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
Johannes Berg1ed32e42009-12-23 13:15:45 +01001623 ieee80211_sdata_running(sdata))
Johannes Berg7b7eab62011-11-03 14:41:13 +01001624 res = drv_add_interface(local, sdata);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001625 }
1626
Johannes Berg55de9082012-07-26 17:24:39 +02001627 /* add channel contexts */
Arend van Sprielf0dea9c2012-11-19 12:01:05 +01001628 if (local->use_chanctx) {
1629 mutex_lock(&local->chanctx_mtx);
1630 list_for_each_entry(ctx, &local->chanctx_list, list)
1631 WARN_ON(drv_add_chanctx(local, ctx));
1632 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001633
Zhao, Gang7df180f2014-04-26 09:43:40 +08001634 list_for_each_entry(sdata, &local->interfaces, list) {
1635 if (!ieee80211_sdata_running(sdata))
1636 continue;
1637 ieee80211_assign_chanctx(local, sdata);
1638 }
Johannes Berg6352c872012-11-07 12:40:41 +01001639
Zhao, Gang7df180f2014-04-26 09:43:40 +08001640 sdata = rtnl_dereference(local->monitor_sdata);
1641 if (sdata && ieee80211_sdata_running(sdata))
1642 ieee80211_assign_chanctx(local, sdata);
1643 }
Johannes Bergfe5f2552012-11-19 22:19:08 +01001644
Johannes Bergf2753dd2009-04-14 10:09:24 +02001645 /* add STAs back */
Johannes Berg34e89502010-02-03 13:59:58 +01001646 mutex_lock(&local->sta_mtx);
1647 list_for_each_entry(sta, &local->sta_list, list) {
Arik Nemtsov2e8d3972012-06-03 23:31:56 +03001648 enum ieee80211_sta_state state;
Johannes Bergf09603a2012-01-20 13:55:21 +01001649
Arik Nemtsov2e8d3972012-06-03 23:31:56 +03001650 if (!sta->uploaded)
1651 continue;
1652
1653 /* AP-mode stations will be added later */
1654 if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1655 continue;
1656
1657 for (state = IEEE80211_STA_NOTEXIST;
1658 state < sta->sta_state; state++)
1659 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1660 state + 1));
Johannes Bergf2753dd2009-04-14 10:09:24 +02001661 }
Johannes Berg34e89502010-02-03 13:59:58 +01001662 mutex_unlock(&local->sta_mtx);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001663
Eliad Peller2683d652011-07-14 20:29:42 +03001664 /* reconfigure tx conf */
Johannes Berg54bcbc62012-03-28 11:04:25 +02001665 if (hw->queues >= IEEE80211_NUM_ACS) {
1666 list_for_each_entry(sdata, &local->interfaces, list) {
1667 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1668 sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1669 !ieee80211_sdata_running(sdata))
1670 continue;
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001671
Johannes Berg54bcbc62012-03-28 11:04:25 +02001672 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1673 drv_conf_tx(local, sdata, i,
1674 &sdata->tx_conf[i]);
1675 }
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001676 }
Eliad Peller2683d652011-07-14 20:29:42 +03001677
Johannes Bergf2753dd2009-04-14 10:09:24 +02001678 /* reconfigure hardware */
1679 ieee80211_hw_config(local, ~0);
1680
Johannes Bergf2753dd2009-04-14 10:09:24 +02001681 ieee80211_configure_filter(local);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001682
1683 /* Finally also reconfigure all the BSS information */
1684 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergac8dd502010-05-05 09:44:02 +02001685 u32 changed;
1686
Johannes Berg9607e6b2009-12-23 13:15:31 +01001687 if (!ieee80211_sdata_running(sdata))
Johannes Bergf2753dd2009-04-14 10:09:24 +02001688 continue;
Johannes Bergac8dd502010-05-05 09:44:02 +02001689
1690 /* common change flags for all interface types */
1691 changed = BSS_CHANGED_ERP_CTS_PROT |
1692 BSS_CHANGED_ERP_PREAMBLE |
1693 BSS_CHANGED_ERP_SLOT |
1694 BSS_CHANGED_HT |
1695 BSS_CHANGED_BASIC_RATES |
1696 BSS_CHANGED_BEACON_INT |
1697 BSS_CHANGED_BSSID |
Johannes Berg4ced3f72010-07-19 16:39:04 +02001698 BSS_CHANGED_CQM |
Eliad Peller55de47f2011-11-01 15:16:55 +02001699 BSS_CHANGED_QOS |
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001700 BSS_CHANGED_IDLE |
1701 BSS_CHANGED_TXPOWER;
Johannes Bergac8dd502010-05-05 09:44:02 +02001702
Johannes Bergf2753dd2009-04-14 10:09:24 +02001703 switch (sdata->vif.type) {
1704 case NL80211_IFTYPE_STATION:
Eliad Peller0d392e92011-12-12 14:10:49 +02001705 changed |= BSS_CHANGED_ASSOC |
Eliad Pellerab095872012-07-27 12:33:22 +03001706 BSS_CHANGED_ARP_FILTER |
1707 BSS_CHANGED_PS;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001708
Alexander Bondar989c6502013-05-16 17:34:17 +03001709 /* Re-send beacon info report to the driver */
1710 if (sdata->u.mgd.have_beacon)
1711 changed |= BSS_CHANGED_BEACON_INFO;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001712
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001713 sdata_lock(sdata);
Johannes Bergac8dd502010-05-05 09:44:02 +02001714 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001715 sdata_unlock(sdata);
Johannes Bergac8dd502010-05-05 09:44:02 +02001716 break;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001717 case NL80211_IFTYPE_ADHOC:
Johannes Bergac8dd502010-05-05 09:44:02 +02001718 changed |= BSS_CHANGED_IBSS;
1719 /* fall through */
Johannes Bergf2753dd2009-04-14 10:09:24 +02001720 case NL80211_IFTYPE_AP:
Johannes Berg339afbf2012-11-14 15:21:17 +01001721 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
Arik Nemtsove7979ac2011-11-22 19:33:18 +02001722
Johannes Berg10416382012-10-19 15:44:42 +02001723 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Arik Nemtsove7979ac2011-11-22 19:33:18 +02001724 changed |= BSS_CHANGED_AP_PROBE_RESP;
1725
Johannes Berg10416382012-10-19 15:44:42 +02001726 if (rcu_access_pointer(sdata->u.ap.beacon))
1727 drv_start_ap(local, sdata);
1728 }
1729
Arik Nemtsov78274932011-09-04 11:11:32 +03001730 /* fall through */
Johannes Bergf2753dd2009-04-14 10:09:24 +02001731 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg8da34932012-12-14 14:17:26 +01001732 if (sdata->vif.bss_conf.enable_beacon) {
1733 changed |= BSS_CHANGED_BEACON |
1734 BSS_CHANGED_BEACON_ENABLED;
1735 ieee80211_bss_info_change_notify(sdata, changed);
1736 }
Johannes Bergf2753dd2009-04-14 10:09:24 +02001737 break;
1738 case NL80211_IFTYPE_WDS:
Johannes Bergf2753dd2009-04-14 10:09:24 +02001739 case NL80211_IFTYPE_AP_VLAN:
1740 case NL80211_IFTYPE_MONITOR:
Johannes Berg98104fde2012-06-16 00:19:54 +02001741 case NL80211_IFTYPE_P2P_DEVICE:
Zhao, Gangb2057862014-04-26 09:43:41 +08001742 /* nothing to do */
Johannes Bergf142c6b2012-06-18 20:07:15 +02001743 break;
Johannes Bergf2753dd2009-04-14 10:09:24 +02001744 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +02001745 case NUM_NL80211_IFTYPES:
Johannes Berg2ca27bc2010-09-16 14:58:23 +02001746 case NL80211_IFTYPE_P2P_CLIENT:
1747 case NL80211_IFTYPE_P2P_GO:
Johannes Bergf2753dd2009-04-14 10:09:24 +02001748 WARN_ON(1);
1749 break;
1750 }
1751 }
1752
Eyal Shapira8e1b23b2011-11-09 12:29:06 +02001753 ieee80211_recalc_ps(local, -1);
1754
Johannes Berg2a419052010-06-10 10:21:29 +02001755 /*
Eliad Peller6e1b1b22012-01-26 13:36:05 +02001756 * The sta might be in psm against the ap (e.g. because
1757 * this was the state before a hw restart), so we
1758 * explicitly send a null packet in order to make sure
1759 * it'll sync against the ap (and get out of psm).
1760 */
1761 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
1762 list_for_each_entry(sdata, &local->interfaces, list) {
1763 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1764 continue;
Johannes Berg20f544e2012-11-08 14:06:28 +01001765 if (!sdata->u.mgd.associated)
1766 continue;
Eliad Peller6e1b1b22012-01-26 13:36:05 +02001767
1768 ieee80211_send_nullfunc(local, sdata, 0);
1769 }
1770 }
1771
Arik Nemtsov2e8d3972012-06-03 23:31:56 +03001772 /* APs are now beaconing, add back stations */
1773 mutex_lock(&local->sta_mtx);
1774 list_for_each_entry(sta, &local->sta_list, list) {
1775 enum ieee80211_sta_state state;
1776
1777 if (!sta->uploaded)
1778 continue;
1779
1780 if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
1781 continue;
1782
1783 for (state = IEEE80211_STA_NOTEXIST;
1784 state < sta->sta_state; state++)
1785 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1786 state + 1));
1787 }
1788 mutex_unlock(&local->sta_mtx);
1789
Eyal Shapira7b21aea2012-05-29 02:00:22 -07001790 /* add back keys */
1791 list_for_each_entry(sdata, &local->interfaces, list)
1792 if (ieee80211_sdata_running(sdata))
1793 ieee80211_enable_keys(sdata);
1794
Eliad Pellerc6209482012-07-02 15:08:25 +03001795 wake_up:
Arik Nemtsov04800ad2012-06-06 11:25:02 +03001796 local->in_reconfig = false;
1797 barrier();
1798
Johannes Berg3c3e21e2013-03-27 23:20:27 +01001799 if (local->monitors == local->open_count && local->monitors > 0)
1800 ieee80211_add_virtual_monitor(local);
1801
Eliad Peller6e1b1b22012-01-26 13:36:05 +02001802 /*
Johannes Berg2a419052010-06-10 10:21:29 +02001803 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1804 * sessions can be established after a resume.
1805 *
1806 * Also tear down aggregation sessions since reconfiguring
1807 * them in a hardware restart scenario is not easily done
1808 * right now, and the hardware will have lost information
1809 * about the sessions, but we and the AP still think they
1810 * are active. This is really a workaround though.
1811 */
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001812 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
Johannes Berg2a419052010-06-10 10:21:29 +02001813 mutex_lock(&local->sta_mtx);
1814
1815 list_for_each_entry(sta, &local->sta_list, list) {
Johannes Bergc82c4a82012-07-18 13:31:31 +02001816 ieee80211_sta_tear_down_BA_sessions(
1817 sta, AGG_STOP_LOCAL_REQUEST);
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001818 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001819 }
Johannes Berg2a419052010-06-10 10:21:29 +02001820
1821 mutex_unlock(&local->sta_mtx);
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001822 }
Wey-Yi Guy74e2bd12010-02-03 09:28:55 -08001823
Johannes Berg445ea4e2013-02-13 12:25:28 +01001824 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
Luciano Coelhocca07b02014-06-13 16:30:05 +03001825 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
1826 false);
Johannes Bergf2753dd2009-04-14 10:09:24 +02001827
Johannes Berg5bb644a2009-05-17 11:40:42 +02001828 /*
Arik Nemtsov32769812014-02-11 12:27:19 +02001829 * Reconfigure sched scan if it was interrupted by FW restart or
1830 * suspend.
1831 */
1832 mutex_lock(&local->mtx);
1833 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1834 lockdep_is_held(&local->mtx));
1835 if (sched_scan_sdata && local->sched_scan_req)
1836 /*
1837 * Sched scan stopped, but we don't want to report it. Instead,
1838 * we're trying to reschedule.
1839 */
1840 if (__ieee80211_request_sched_scan_start(sched_scan_sdata,
1841 local->sched_scan_req))
1842 sched_scan_stopped = true;
1843 mutex_unlock(&local->mtx);
1844
1845 if (sched_scan_stopped)
Eliad Pellere669ba22014-04-30 16:14:24 +03001846 cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy);
Arik Nemtsov32769812014-02-11 12:27:19 +02001847
1848 /*
Johannes Berg5bb644a2009-05-17 11:40:42 +02001849 * If this is for hw restart things are still running.
1850 * We may want to change that later, however.
1851 */
Johannes Berg8f21b0a2013-01-11 00:28:01 +01001852 if (!local->suspended || reconfig_due_to_wowlan)
Johannes Berg9214ad72012-11-06 19:18:13 +01001853 drv_restart_complete(local);
Johannes Berg8f21b0a2013-01-11 00:28:01 +01001854
1855 if (!local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001856 return 0;
1857
1858#ifdef CONFIG_PM
Johannes Bergceb99fe2009-11-19 14:29:39 +01001859 /* first set suspended false, then resuming */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001860 local->suspended = false;
Johannes Bergceb99fe2009-11-19 14:29:39 +01001861 mb();
1862 local->resuming = false;
Johannes Berg5bb644a2009-05-17 11:40:42 +02001863
Johannes Bergb8360ab2013-04-29 14:57:44 +02001864 list_for_each_entry(sdata, &local->interfaces, list) {
1865 if (!ieee80211_sdata_running(sdata))
1866 continue;
1867 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1868 ieee80211_sta_restart(sdata);
1869 }
1870
Johannes Berg26d59532011-04-01 13:52:48 +02001871 mod_timer(&local->sta_cleanup, jiffies + 1);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001872#else
1873 WARN_ON(1);
1874#endif
David Spinadeld43c6b62013-12-08 21:48:57 +02001875
Johannes Bergf2753dd2009-04-14 10:09:24 +02001876 return 0;
1877}
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001878
Johannes Berg95acac62011-07-12 12:30:59 +02001879void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1880{
1881 struct ieee80211_sub_if_data *sdata;
1882 struct ieee80211_local *local;
1883 struct ieee80211_key *key;
1884
1885 if (WARN_ON(!vif))
1886 return;
1887
1888 sdata = vif_to_sdata(vif);
1889 local = sdata->local;
1890
1891 if (WARN_ON(!local->resuming))
1892 return;
1893
1894 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1895 return;
1896
1897 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1898
1899 mutex_lock(&local->key_mtx);
1900 list_for_each_entry(key, &sdata->key_list, list)
1901 key->flags |= KEY_FLAG_TAINTED;
1902 mutex_unlock(&local->key_mtx);
1903}
1904EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1905
Johannes Berg04ecd252012-09-11 14:34:12 +02001906void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
Johannes Berg0f782312009-12-01 13:37:02 +01001907{
Johannes Berg04ecd252012-09-11 14:34:12 +02001908 struct ieee80211_local *local = sdata->local;
1909 struct ieee80211_chanctx_conf *chanctx_conf;
1910 struct ieee80211_chanctx *chanctx;
Johannes Berg0f782312009-12-01 13:37:02 +01001911
Johannes Berg04ecd252012-09-11 14:34:12 +02001912 mutex_lock(&local->chanctx_mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01001913
Johannes Berg04ecd252012-09-11 14:34:12 +02001914 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1915 lockdep_is_held(&local->chanctx_mtx));
Johannes Berg0f782312009-12-01 13:37:02 +01001916
Johannes Berg04ecd252012-09-11 14:34:12 +02001917 if (WARN_ON_ONCE(!chanctx_conf))
Johannes Berg5d8e4232012-09-11 10:17:11 +02001918 goto unlock;
Johannes Berg0f782312009-12-01 13:37:02 +01001919
Johannes Berg04ecd252012-09-11 14:34:12 +02001920 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1921 ieee80211_recalc_smps_chanctx(local, chanctx);
Johannes Berg5d8e4232012-09-11 10:17:11 +02001922 unlock:
Johannes Berg04ecd252012-09-11 14:34:12 +02001923 mutex_unlock(&local->chanctx_mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01001924}
Johannes Berg8e664fb2009-12-23 13:15:38 +01001925
Eliad Peller21f659b2013-11-11 20:14:01 +02001926void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
1927{
1928 struct ieee80211_local *local = sdata->local;
1929 struct ieee80211_chanctx_conf *chanctx_conf;
1930 struct ieee80211_chanctx *chanctx;
1931
1932 mutex_lock(&local->chanctx_mtx);
1933
1934 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1935 lockdep_is_held(&local->chanctx_mtx));
1936
1937 if (WARN_ON_ONCE(!chanctx_conf))
1938 goto unlock;
1939
1940 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1941 ieee80211_recalc_chanctx_min_def(local, chanctx);
1942 unlock:
1943 mutex_unlock(&local->chanctx_mtx);
1944}
1945
Johannes Berg8e664fb2009-12-23 13:15:38 +01001946static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1947{
1948 int i;
1949
1950 for (i = 0; i < n_ids; i++)
1951 if (ids[i] == id)
1952 return true;
1953 return false;
1954}
1955
1956/**
1957 * ieee80211_ie_split - split an IE buffer according to ordering
1958 *
1959 * @ies: the IE buffer
1960 * @ielen: the length of the IE buffer
1961 * @ids: an array with element IDs that are allowed before
1962 * the split
1963 * @n_ids: the size of the element ID array
1964 * @offset: offset where to start splitting in the buffer
1965 *
1966 * This function splits an IE buffer by updating the @offset
1967 * variable to point to the location where the buffer should be
1968 * split.
1969 *
1970 * It assumes that the given IE buffer is well-formed, this
1971 * has to be guaranteed by the caller!
1972 *
1973 * It also assumes that the IEs in the buffer are ordered
1974 * correctly, if not the result of using this function will not
1975 * be ordered correctly either, i.e. it does no reordering.
1976 *
1977 * The function returns the offset where the next part of the
1978 * buffer starts, which may be @ielen if the entire (remainder)
1979 * of the buffer should be used.
1980 */
1981size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1982 const u8 *ids, int n_ids, size_t offset)
1983{
1984 size_t pos = offset;
1985
1986 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1987 pos += 2 + ies[pos + 1];
1988
1989 return pos;
1990}
1991
1992size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1993{
1994 size_t pos = offset;
1995
1996 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1997 pos += 2 + ies[pos + 1];
1998
1999 return pos;
2000}
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002001
2002static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2003 int rssi_min_thold,
2004 int rssi_max_thold)
2005{
2006 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2007
2008 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2009 return;
2010
2011 /*
2012 * Scale up threshold values before storing it, as the RSSI averaging
2013 * algorithm uses a scaled up value as well. Change this scaling
2014 * factor if the RSSI averaging algorithm changes.
2015 */
2016 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2017 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2018}
2019
2020void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2021 int rssi_min_thold,
2022 int rssi_max_thold)
2023{
2024 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2025
2026 WARN_ON(rssi_min_thold == rssi_max_thold ||
2027 rssi_min_thold > rssi_max_thold);
2028
2029 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2030 rssi_max_thold);
2031}
2032EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2033
2034void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2035{
2036 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2037
2038 _ieee80211_enable_rssi_reports(sdata, 0, 0);
2039}
2040EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
Arik Nemtsov768db342011-09-28 14:12:51 +03002041
Ben Greearef96a8422011-11-18 11:32:00 -08002042u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
Alexander Simon42e7aa72011-10-26 14:47:26 -07002043 u16 cap)
2044{
2045 __le16 tmp;
2046
2047 *pos++ = WLAN_EID_HT_CAPABILITY;
2048 *pos++ = sizeof(struct ieee80211_ht_cap);
2049 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
2050
2051 /* capability flags */
2052 tmp = cpu_to_le16(cap);
2053 memcpy(pos, &tmp, sizeof(u16));
2054 pos += sizeof(u16);
2055
2056 /* AMPDU parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08002057 *pos++ = ht_cap->ampdu_factor |
2058 (ht_cap->ampdu_density <<
Alexander Simon42e7aa72011-10-26 14:47:26 -07002059 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
2060
2061 /* MCS set */
Ben Greearef96a8422011-11-18 11:32:00 -08002062 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2063 pos += sizeof(ht_cap->mcs);
Alexander Simon42e7aa72011-10-26 14:47:26 -07002064
2065 /* extended capabilities */
2066 pos += sizeof(__le16);
2067
2068 /* BF capabilities */
2069 pos += sizeof(__le32);
2070
2071 /* antenna selection */
2072 pos += sizeof(u8);
2073
2074 return pos;
2075}
2076
Mahesh Palivelaba0afa2f2012-07-02 11:25:12 +00002077u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
Johannes Bergcc3983d2012-12-07 12:45:06 +01002078 u32 cap)
Mahesh Palivelaba0afa2f2012-07-02 11:25:12 +00002079{
2080 __le32 tmp;
2081
2082 *pos++ = WLAN_EID_VHT_CAPABILITY;
Mahesh Palivelad4950282012-10-10 11:25:40 +00002083 *pos++ = sizeof(struct ieee80211_vht_cap);
2084 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
Mahesh Palivelaba0afa2f2012-07-02 11:25:12 +00002085
2086 /* capability flags */
2087 tmp = cpu_to_le32(cap);
2088 memcpy(pos, &tmp, sizeof(u32));
2089 pos += sizeof(u32);
2090
2091 /* VHT MCS set */
2092 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2093 pos += sizeof(vht_cap->vht_mcs);
2094
2095 return pos;
2096}
2097
Johannes Berg074d46d2012-03-15 19:45:16 +01002098u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
Johannes Berg4bf88532012-11-09 11:39:59 +01002099 const struct cfg80211_chan_def *chandef,
Ashok Nagarajan431e3152012-04-30 14:20:29 -07002100 u16 prot_mode)
Alexander Simon42e7aa72011-10-26 14:47:26 -07002101{
Johannes Berg074d46d2012-03-15 19:45:16 +01002102 struct ieee80211_ht_operation *ht_oper;
Alexander Simon42e7aa72011-10-26 14:47:26 -07002103 /* Build HT Information */
Johannes Berg074d46d2012-03-15 19:45:16 +01002104 *pos++ = WLAN_EID_HT_OPERATION;
2105 *pos++ = sizeof(struct ieee80211_ht_operation);
2106 ht_oper = (struct ieee80211_ht_operation *)pos;
Johannes Berg4bf88532012-11-09 11:39:59 +01002107 ht_oper->primary_chan = ieee80211_frequency_to_channel(
2108 chandef->chan->center_freq);
2109 switch (chandef->width) {
2110 case NL80211_CHAN_WIDTH_160:
2111 case NL80211_CHAN_WIDTH_80P80:
2112 case NL80211_CHAN_WIDTH_80:
2113 case NL80211_CHAN_WIDTH_40:
2114 if (chandef->center_freq1 > chandef->chan->center_freq)
2115 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2116 else
2117 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
Alexander Simon42e7aa72011-10-26 14:47:26 -07002118 break;
Alexander Simon42e7aa72011-10-26 14:47:26 -07002119 default:
Johannes Berg074d46d2012-03-15 19:45:16 +01002120 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
Alexander Simon42e7aa72011-10-26 14:47:26 -07002121 break;
2122 }
Thomas Pedersenaee286c2012-04-18 19:24:14 -07002123 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
Johannes Berg4bf88532012-11-09 11:39:59 +01002124 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
2125 chandef->width != NL80211_CHAN_WIDTH_20)
Johannes Berg074d46d2012-03-15 19:45:16 +01002126 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01002127
Ashok Nagarajan431e3152012-04-30 14:20:29 -07002128 ht_oper->operation_mode = cpu_to_le16(prot_mode);
Johannes Berg074d46d2012-03-15 19:45:16 +01002129 ht_oper->stbc_param = 0x0000;
Alexander Simon42e7aa72011-10-26 14:47:26 -07002130
2131 /* It seems that Basic MCS set and Supported MCS set
2132 are identical for the first 10 bytes */
Johannes Berg074d46d2012-03-15 19:45:16 +01002133 memset(&ht_oper->basic_set, 0, 16);
2134 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
Alexander Simon42e7aa72011-10-26 14:47:26 -07002135
Johannes Berg074d46d2012-03-15 19:45:16 +01002136 return pos + sizeof(struct ieee80211_ht_operation);
Alexander Simon42e7aa72011-10-26 14:47:26 -07002137}
2138
Johannes Berg4bf88532012-11-09 11:39:59 +01002139void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
Johannes Berg4a3cb702013-02-12 16:43:19 +01002140 const struct ieee80211_ht_operation *ht_oper,
Johannes Berg4bf88532012-11-09 11:39:59 +01002141 struct cfg80211_chan_def *chandef)
Alexander Simon42e7aa72011-10-26 14:47:26 -07002142{
2143 enum nl80211_channel_type channel_type;
2144
Johannes Berg4bf88532012-11-09 11:39:59 +01002145 if (!ht_oper) {
2146 cfg80211_chandef_create(chandef, control_chan,
2147 NL80211_CHAN_NO_HT);
2148 return;
2149 }
Alexander Simon42e7aa72011-10-26 14:47:26 -07002150
Johannes Berg074d46d2012-03-15 19:45:16 +01002151 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Alexander Simon42e7aa72011-10-26 14:47:26 -07002152 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
2153 channel_type = NL80211_CHAN_HT20;
2154 break;
2155 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
2156 channel_type = NL80211_CHAN_HT40PLUS;
2157 break;
2158 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
2159 channel_type = NL80211_CHAN_HT40MINUS;
2160 break;
2161 default:
2162 channel_type = NL80211_CHAN_NO_HT;
2163 }
2164
Johannes Berg4bf88532012-11-09 11:39:59 +01002165 cfg80211_chandef_create(chandef, control_chan, channel_type);
Alexander Simon42e7aa72011-10-26 14:47:26 -07002166}
2167
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002168int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
2169 const struct ieee80211_supported_band *sband,
2170 const u8 *srates, int srates_len, u32 *rates)
2171{
2172 u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
2173 int shift = ieee80211_chandef_get_shift(chandef);
2174 struct ieee80211_rate *br;
2175 int brate, rate, i, j, count = 0;
2176
2177 *rates = 0;
2178
2179 for (i = 0; i < srates_len; i++) {
2180 rate = srates[i] & 0x7f;
2181
2182 for (j = 0; j < sband->n_bitrates; j++) {
2183 br = &sband->bitrates[j];
2184 if ((rate_flags & br->flags) != rate_flags)
2185 continue;
2186
2187 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2188 if (brate == rate) {
2189 *rates |= BIT(j);
2190 count++;
2191 break;
2192 }
2193 }
2194 }
2195 return count;
2196}
2197
Johannes Bergfc8a7322012-06-28 10:33:25 +02002198int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b778632012-07-23 14:53:27 +02002199 struct sk_buff *skb, bool need_basic,
2200 enum ieee80211_band band)
Arik Nemtsov768db342011-09-28 14:12:51 +03002201{
Arik Nemtsov768db342011-09-28 14:12:51 +03002202 struct ieee80211_local *local = sdata->local;
2203 struct ieee80211_supported_band *sband;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002204 int rate, shift;
Arik Nemtsov768db342011-09-28 14:12:51 +03002205 u8 i, rates, *pos;
Johannes Bergfc8a7322012-06-28 10:33:25 +02002206 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002207 u32 rate_flags;
Arik Nemtsov768db342011-09-28 14:12:51 +03002208
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002209 shift = ieee80211_vif_get_shift(&sdata->vif);
2210 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
Johannes Berg6b778632012-07-23 14:53:27 +02002211 sband = local->hw.wiphy->bands[band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002212 rates = 0;
2213 for (i = 0; i < sband->n_bitrates; i++) {
2214 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2215 continue;
2216 rates++;
2217 }
Arik Nemtsov768db342011-09-28 14:12:51 +03002218 if (rates > 8)
2219 rates = 8;
2220
2221 if (skb_tailroom(skb) < rates + 2)
2222 return -ENOMEM;
2223
2224 pos = skb_put(skb, rates + 2);
2225 *pos++ = WLAN_EID_SUPP_RATES;
2226 *pos++ = rates;
2227 for (i = 0; i < rates; i++) {
Ashok Nagarajan657c3e02012-04-02 21:21:20 -07002228 u8 basic = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002229 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2230 continue;
2231
Ashok Nagarajan657c3e02012-04-02 21:21:20 -07002232 if (need_basic && basic_rates & BIT(i))
2233 basic = 0x80;
Arik Nemtsov768db342011-09-28 14:12:51 +03002234 rate = sband->bitrates[i].bitrate;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002235 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2236 5 * (1 << shift));
2237 *pos++ = basic | (u8) rate;
Arik Nemtsov768db342011-09-28 14:12:51 +03002238 }
2239
2240 return 0;
2241}
2242
Johannes Bergfc8a7322012-06-28 10:33:25 +02002243int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b778632012-07-23 14:53:27 +02002244 struct sk_buff *skb, bool need_basic,
2245 enum ieee80211_band band)
Arik Nemtsov768db342011-09-28 14:12:51 +03002246{
Arik Nemtsov768db342011-09-28 14:12:51 +03002247 struct ieee80211_local *local = sdata->local;
2248 struct ieee80211_supported_band *sband;
Chun-Yeow Yeohcc63ec72013-09-07 23:40:44 -07002249 int rate, shift;
Arik Nemtsov768db342011-09-28 14:12:51 +03002250 u8 i, exrates, *pos;
Johannes Bergfc8a7322012-06-28 10:33:25 +02002251 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002252 u32 rate_flags;
2253
2254 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2255 shift = ieee80211_vif_get_shift(&sdata->vif);
Arik Nemtsov768db342011-09-28 14:12:51 +03002256
Johannes Berg6b778632012-07-23 14:53:27 +02002257 sband = local->hw.wiphy->bands[band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002258 exrates = 0;
2259 for (i = 0; i < sband->n_bitrates; i++) {
2260 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2261 continue;
2262 exrates++;
2263 }
2264
Arik Nemtsov768db342011-09-28 14:12:51 +03002265 if (exrates > 8)
2266 exrates -= 8;
2267 else
2268 exrates = 0;
2269
2270 if (skb_tailroom(skb) < exrates + 2)
2271 return -ENOMEM;
2272
2273 if (exrates) {
2274 pos = skb_put(skb, exrates + 2);
2275 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2276 *pos++ = exrates;
2277 for (i = 8; i < sband->n_bitrates; i++) {
Ashok Nagarajan657c3e02012-04-02 21:21:20 -07002278 u8 basic = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002279 if ((rate_flags & sband->bitrates[i].flags)
2280 != rate_flags)
2281 continue;
Ashok Nagarajan657c3e02012-04-02 21:21:20 -07002282 if (need_basic && basic_rates & BIT(i))
2283 basic = 0x80;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002284 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2285 5 * (1 << shift));
2286 *pos++ = basic | (u8) rate;
Arik Nemtsov768db342011-09-28 14:12:51 +03002287 }
2288 }
2289 return 0;
2290}
Wey-Yi Guy1dae27f2012-04-13 12:02:57 -07002291
2292int ieee80211_ave_rssi(struct ieee80211_vif *vif)
2293{
2294 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2295 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2296
Wey-Yi Guybe6bcab2012-04-23 09:30:32 -07002297 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2298 /* non-managed type inferfaces */
2299 return 0;
2300 }
Emmanuel Grumbach3a7bba62013-03-20 17:05:45 +02002301 return ifmgd->ave_beacon_signal / 16;
Wey-Yi Guy1dae27f2012-04-13 12:02:57 -07002302}
Wey-Yi Guy0d8a0a12012-04-20 11:57:00 -07002303EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
Johannes Berg04ecd252012-09-11 14:34:12 +02002304
2305u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
2306{
2307 if (!mcs)
2308 return 1;
2309
2310 /* TODO: consider rx_highest */
2311
2312 if (mcs->rx_mask[3])
2313 return 4;
2314 if (mcs->rx_mask[2])
2315 return 3;
2316 if (mcs->rx_mask[1])
2317 return 2;
2318 return 1;
2319}
Thomas Pedersenf4bda332012-11-13 10:46:27 -08002320
2321/**
2322 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2323 * @local: mac80211 hw info struct
2324 * @status: RX status
2325 * @mpdu_len: total MPDU length (including FCS)
2326 * @mpdu_offset: offset into MPDU to calculate timestamp at
2327 *
2328 * This function calculates the RX timestamp at the given MPDU offset, taking
2329 * into account what the RX timestamp was. An offset of 0 will just normalize
2330 * the timestamp to TSF at beginning of MPDU reception.
2331 */
2332u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2333 struct ieee80211_rx_status *status,
2334 unsigned int mpdu_len,
2335 unsigned int mpdu_offset)
2336{
2337 u64 ts = status->mactime;
2338 struct rate_info ri;
2339 u16 rate;
2340
2341 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2342 return 0;
2343
2344 memset(&ri, 0, sizeof(ri));
2345
2346 /* Fill cfg80211 rate info */
2347 if (status->flag & RX_FLAG_HT) {
2348 ri.mcs = status->rate_idx;
2349 ri.flags |= RATE_INFO_FLAGS_MCS;
2350 if (status->flag & RX_FLAG_40MHZ)
2351 ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2352 if (status->flag & RX_FLAG_SHORT_GI)
2353 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
Johannes Berg56146182012-11-09 15:07:02 +01002354 } else if (status->flag & RX_FLAG_VHT) {
2355 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
2356 ri.mcs = status->rate_idx;
2357 ri.nss = status->vht_nss;
2358 if (status->flag & RX_FLAG_40MHZ)
2359 ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +02002360 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
Johannes Berg56146182012-11-09 15:07:02 +01002361 ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +02002362 if (status->vht_flag & RX_VHT_FLAG_80P80MHZ)
Johannes Berg56146182012-11-09 15:07:02 +01002363 ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
Emmanuel Grumbach1b8d2422014-02-05 16:37:11 +02002364 if (status->vht_flag & RX_VHT_FLAG_160MHZ)
Johannes Berg56146182012-11-09 15:07:02 +01002365 ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
2366 if (status->flag & RX_FLAG_SHORT_GI)
2367 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
Thomas Pedersenf4bda332012-11-13 10:46:27 -08002368 } else {
2369 struct ieee80211_supported_band *sband;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002370 int shift = 0;
2371 int bitrate;
2372
2373 if (status->flag & RX_FLAG_10MHZ)
2374 shift = 1;
2375 if (status->flag & RX_FLAG_5MHZ)
2376 shift = 2;
Thomas Pedersenf4bda332012-11-13 10:46:27 -08002377
2378 sband = local->hw.wiphy->bands[status->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002379 bitrate = sband->bitrates[status->rate_idx].bitrate;
2380 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
Thomas Pedersenf4bda332012-11-13 10:46:27 -08002381 }
2382
2383 rate = cfg80211_calculate_bitrate(&ri);
Johannes Bergd86aa4f2013-10-11 15:47:06 +02002384 if (WARN_ONCE(!rate,
2385 "Invalid bitrate: flags=0x%x, idx=%d, vht_nss=%d\n",
2386 status->flag, status->rate_idx, status->vht_nss))
2387 return 0;
Thomas Pedersenf4bda332012-11-13 10:46:27 -08002388
2389 /* rewind from end of MPDU */
2390 if (status->flag & RX_FLAG_MACTIME_END)
2391 ts -= mpdu_len * 8 * 10 / rate;
2392
2393 ts += mpdu_offset * 8 * 10 / rate;
2394
2395 return ts;
2396}
Simon Wunderlich164eb022013-02-08 18:16:20 +01002397
2398void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2399{
2400 struct ieee80211_sub_if_data *sdata;
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002401 struct cfg80211_chan_def chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01002402
Johannes Berg34a37402013-12-18 09:43:33 +01002403 mutex_lock(&local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01002404 mutex_lock(&local->iflist_mtx);
2405 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg34a37402013-12-18 09:43:33 +01002406 /* it might be waiting for the local->mtx, but then
2407 * by the time it gets it, sdata->wdev.cac_started
2408 * will no longer be true
2409 */
2410 cancel_delayed_work(&sdata->dfs_cac_timer_work);
Simon Wunderlich164eb022013-02-08 18:16:20 +01002411
2412 if (sdata->wdev.cac_started) {
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002413 chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01002414 ieee80211_vif_release_channel(sdata);
2415 cfg80211_cac_event(sdata->dev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002416 &chandef,
Simon Wunderlich164eb022013-02-08 18:16:20 +01002417 NL80211_RADAR_CAC_ABORTED,
2418 GFP_KERNEL);
2419 }
2420 }
2421 mutex_unlock(&local->iflist_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +01002422 mutex_unlock(&local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01002423}
2424
2425void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2426{
2427 struct ieee80211_local *local =
2428 container_of(work, struct ieee80211_local, radar_detected_work);
Janusz Dziedzic84a3d1c2013-11-05 14:48:46 +01002429 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01002430
2431 ieee80211_dfs_cac_cancel(local);
2432
2433 if (local->use_chanctx)
2434 /* currently not handled */
2435 WARN_ON(1);
Janusz Dziedzic84a3d1c2013-11-05 14:48:46 +01002436 else
Simon Wunderlich164eb022013-02-08 18:16:20 +01002437 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
Simon Wunderlich164eb022013-02-08 18:16:20 +01002438}
2439
2440void ieee80211_radar_detected(struct ieee80211_hw *hw)
2441{
2442 struct ieee80211_local *local = hw_to_local(hw);
2443
2444 trace_api_radar_detected(local);
2445
2446 ieee80211_queue_work(hw, &local->radar_detected_work);
2447}
2448EXPORT_SYMBOL(ieee80211_radar_detected);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02002449
2450u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2451{
2452 u32 ret;
2453 int tmp;
2454
2455 switch (c->width) {
2456 case NL80211_CHAN_WIDTH_20:
2457 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2458 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2459 break;
2460 case NL80211_CHAN_WIDTH_40:
2461 c->width = NL80211_CHAN_WIDTH_20;
2462 c->center_freq1 = c->chan->center_freq;
2463 ret = IEEE80211_STA_DISABLE_40MHZ |
2464 IEEE80211_STA_DISABLE_VHT;
2465 break;
2466 case NL80211_CHAN_WIDTH_80:
2467 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2468 /* n_P40 */
2469 tmp /= 2;
2470 /* freq_P40 */
2471 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2472 c->width = NL80211_CHAN_WIDTH_40;
2473 ret = IEEE80211_STA_DISABLE_VHT;
2474 break;
2475 case NL80211_CHAN_WIDTH_80P80:
2476 c->center_freq2 = 0;
2477 c->width = NL80211_CHAN_WIDTH_80;
2478 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2479 IEEE80211_STA_DISABLE_160MHZ;
2480 break;
2481 case NL80211_CHAN_WIDTH_160:
2482 /* n_P20 */
2483 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2484 /* n_P80 */
2485 tmp /= 4;
2486 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2487 c->width = NL80211_CHAN_WIDTH_80;
2488 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2489 IEEE80211_STA_DISABLE_160MHZ;
2490 break;
2491 default:
2492 case NL80211_CHAN_WIDTH_20_NOHT:
2493 WARN_ON_ONCE(1);
2494 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2495 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2496 break;
2497 case NL80211_CHAN_WIDTH_5:
2498 case NL80211_CHAN_WIDTH_10:
2499 WARN_ON_ONCE(1);
2500 /* keep c->width */
2501 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2502 break;
2503 }
2504
2505 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2506
2507 return ret;
2508}
Emmanuel Grumbach687da132013-10-01 16:45:43 +03002509
2510/*
2511 * Returns true if smps_mode_new is strictly more restrictive than
2512 * smps_mode_old.
2513 */
2514bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2515 enum ieee80211_smps_mode smps_mode_new)
2516{
2517 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2518 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2519 return false;
2520
2521 switch (smps_mode_old) {
2522 case IEEE80211_SMPS_STATIC:
2523 return false;
2524 case IEEE80211_SMPS_DYNAMIC:
2525 return smps_mode_new == IEEE80211_SMPS_STATIC;
2526 case IEEE80211_SMPS_OFF:
2527 return smps_mode_new != IEEE80211_SMPS_OFF;
2528 default:
2529 WARN_ON(1);
2530 }
2531
2532 return false;
2533}
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07002534
2535int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2536 struct cfg80211_csa_settings *csa_settings)
2537{
2538 struct sk_buff *skb;
2539 struct ieee80211_mgmt *mgmt;
2540 struct ieee80211_local *local = sdata->local;
2541 int freq;
2542 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2543 sizeof(mgmt->u.action.u.chan_switch);
2544 u8 *pos;
2545
2546 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2547 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2548 return -EOPNOTSUPP;
2549
2550 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2551 5 + /* channel switch announcement element */
2552 3 + /* secondary channel offset element */
2553 8); /* mesh channel switch parameters element */
2554 if (!skb)
2555 return -ENOMEM;
2556
2557 skb_reserve(skb, local->tx_headroom);
2558 mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2559 memset(mgmt, 0, hdr_len);
2560 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2561 IEEE80211_STYPE_ACTION);
2562
2563 eth_broadcast_addr(mgmt->da);
2564 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2565 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2566 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2567 } else {
2568 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2569 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2570 }
2571 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2572 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2573 pos = skb_put(skb, 5);
2574 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
2575 *pos++ = 3; /* IE length */
2576 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
2577 freq = csa_settings->chandef.chan->center_freq;
2578 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
2579 *pos++ = csa_settings->count; /* count */
2580
2581 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2582 enum nl80211_channel_type ch_type;
2583
2584 skb_put(skb, 3);
2585 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
2586 *pos++ = 1; /* IE length */
2587 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
2588 if (ch_type == NL80211_CHAN_HT40PLUS)
2589 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2590 else
2591 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2592 }
2593
2594 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2595 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07002596
2597 skb_put(skb, 8);
2598 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
2599 *pos++ = 6; /* IE length */
2600 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
2601 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
2602 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
2603 *pos++ |= csa_settings->block_tx ?
2604 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
2605 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
2606 pos += 2;
Chun-Yeow Yeohca91dc92013-11-12 10:31:48 +08002607 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07002608 pos += 2;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07002609 }
2610
2611 ieee80211_tx_skb(sdata, skb);
2612 return 0;
2613}
Max Stepanov2475b1cc2013-03-24 14:23:27 +02002614
2615bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
2616{
2617 return !(cs == NULL || cs->cipher == 0 ||
2618 cs->hdr_len < cs->pn_len + cs->pn_off ||
2619 cs->hdr_len <= cs->key_idx_off ||
2620 cs->key_idx_shift > 7 ||
2621 cs->key_idx_mask == 0);
2622}
2623
2624bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
2625{
2626 int i;
2627
2628 /* Ensure we have enough iftype bitmap space for all iftype values */
2629 WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
2630
2631 for (i = 0; i < n; i++)
2632 if (!ieee80211_cs_valid(&cs[i]))
2633 return false;
2634
2635 return true;
2636}
2637
2638const struct ieee80211_cipher_scheme *
2639ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
2640 enum nl80211_iftype iftype)
2641{
2642 const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
2643 int n = local->hw.n_cipher_schemes;
2644 int i;
2645 const struct ieee80211_cipher_scheme *cs = NULL;
2646
2647 for (i = 0; i < n; i++) {
2648 if (l[i].cipher == cipher) {
2649 cs = &l[i];
2650 break;
2651 }
2652 }
2653
2654 if (!cs || !(cs->iftype & BIT(iftype)))
2655 return NULL;
2656
2657 return cs;
2658}
2659
2660int ieee80211_cs_headroom(struct ieee80211_local *local,
2661 struct cfg80211_crypto_settings *crypto,
2662 enum nl80211_iftype iftype)
2663{
2664 const struct ieee80211_cipher_scheme *cs;
2665 int headroom = IEEE80211_ENCRYPT_HEADROOM;
2666 int i;
2667
2668 for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
2669 cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
2670 iftype);
2671
2672 if (cs && headroom < cs->hdr_len)
2673 headroom = cs->hdr_len;
2674 }
2675
2676 cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
2677 if (cs && headroom < cs->hdr_len)
2678 headroom = cs->hdr_len;
2679
2680 return headroom;
2681}
Felix Fietkaua7022e62013-12-16 21:49:14 +01002682
2683static bool
2684ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
2685{
2686 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
2687 int skip;
2688
2689 if (end > 0)
2690 return false;
2691
2692 /* End time is in the past, check for repetitions */
2693 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
2694 if (data->count[i] < 255) {
2695 if (data->count[i] <= skip) {
2696 data->count[i] = 0;
2697 return false;
2698 }
2699
2700 data->count[i] -= skip;
2701 }
2702
2703 data->desc[i].start += skip * data->desc[i].interval;
2704
2705 return true;
2706}
2707
2708static bool
2709ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
2710 s32 *offset)
2711{
2712 bool ret = false;
2713 int i;
2714
2715 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2716 s32 cur;
2717
2718 if (!data->count[i])
2719 continue;
2720
2721 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
2722 ret = true;
2723
2724 cur = data->desc[i].start - tsf;
2725 if (cur > *offset)
2726 continue;
2727
2728 cur = data->desc[i].start + data->desc[i].duration - tsf;
2729 if (cur > *offset)
2730 *offset = cur;
2731 }
2732
2733 return ret;
2734}
2735
2736static u32
2737ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
2738{
2739 s32 offset = 0;
2740 int tries = 0;
2741 /*
2742 * arbitrary limit, used to avoid infinite loops when combined NoA
2743 * descriptors cover the full time period.
2744 */
2745 int max_tries = 5;
2746
2747 ieee80211_extend_absent_time(data, tsf, &offset);
2748 do {
2749 if (!ieee80211_extend_absent_time(data, tsf, &offset))
2750 break;
2751
2752 tries++;
2753 } while (tries < max_tries);
2754
2755 return offset;
2756}
2757
2758void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
2759{
2760 u32 next_offset = BIT(31) - 1;
2761 int i;
2762
2763 data->absent = 0;
2764 data->has_next_tsf = false;
2765 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2766 s32 start;
2767
2768 if (!data->count[i])
2769 continue;
2770
2771 ieee80211_extend_noa_desc(data, tsf, i);
2772 start = data->desc[i].start - tsf;
2773 if (start <= 0)
2774 data->absent |= BIT(i);
2775
2776 if (next_offset > start)
2777 next_offset = start;
2778
2779 data->has_next_tsf = true;
2780 }
2781
2782 if (data->absent)
2783 next_offset = ieee80211_get_noa_absent_time(data, tsf);
2784
2785 data->next_tsf = tsf + next_offset;
2786}
2787EXPORT_SYMBOL(ieee80211_update_p2p_noa);
2788
2789int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
2790 struct ieee80211_noa_data *data, u32 tsf)
2791{
2792 int ret = 0;
2793 int i;
2794
2795 memset(data, 0, sizeof(*data));
2796
2797 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2798 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
2799
2800 if (!desc->count || !desc->duration)
2801 continue;
2802
2803 data->count[i] = desc->count;
2804 data->desc[i].start = le32_to_cpu(desc->start_time);
2805 data->desc[i].duration = le32_to_cpu(desc->duration);
2806 data->desc[i].interval = le32_to_cpu(desc->interval);
2807
2808 if (data->count[i] > 1 &&
2809 data->desc[i].interval < data->desc[i].duration)
2810 continue;
2811
2812 ieee80211_extend_noa_desc(data, tsf, i);
2813 ret++;
2814 }
2815
2816 if (ret)
2817 ieee80211_update_p2p_noa(data, tsf);
2818
2819 return ret;
2820}
2821EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
Thomas Pedersen057d5f42013-12-19 10:25:15 -08002822
2823void ieee80211_recalc_dtim(struct ieee80211_local *local,
2824 struct ieee80211_sub_if_data *sdata)
2825{
2826 u64 tsf = drv_get_tsf(local, sdata);
2827 u64 dtim_count = 0;
2828 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
2829 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
2830 struct ps_data *ps;
2831 u8 bcns_from_dtim;
2832
2833 if (tsf == -1ULL || !beacon_int || !dtim_period)
2834 return;
2835
2836 if (sdata->vif.type == NL80211_IFTYPE_AP ||
2837 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
2838 if (!sdata->bss)
2839 return;
2840
2841 ps = &sdata->bss->ps;
2842 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2843 ps = &sdata->u.mesh.ps;
2844 } else {
2845 return;
2846 }
2847
2848 /*
2849 * actually finds last dtim_count, mac80211 will update in
2850 * __beacon_add_tim().
2851 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
2852 */
2853 do_div(tsf, beacon_int);
2854 bcns_from_dtim = do_div(tsf, dtim_period);
2855 /* just had a DTIM */
2856 if (!bcns_from_dtim)
2857 dtim_count = 0;
2858 else
2859 dtim_count = dtim_period - bcns_from_dtim;
2860
2861 ps->dtim_count = dtim_count;
2862}
Luciano Coelho73de86a2014-02-13 11:31:59 +02002863
2864int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
2865 const struct cfg80211_chan_def *chandef,
2866 enum ieee80211_chanctx_mode chanmode,
2867 u8 radar_detect)
2868{
2869 struct ieee80211_local *local = sdata->local;
2870 struct ieee80211_sub_if_data *sdata_iter;
2871 enum nl80211_iftype iftype = sdata->wdev.iftype;
2872 int num[NUM_NL80211_IFTYPES];
2873 struct ieee80211_chanctx *ctx;
Luciano Coelhob6a55012014-02-27 11:07:21 +02002874 int num_different_channels = 0;
Luciano Coelho73de86a2014-02-13 11:31:59 +02002875 int total = 1;
2876
2877 lockdep_assert_held(&local->chanctx_mtx);
2878
2879 if (WARN_ON(hweight32(radar_detect) > 1))
2880 return -EINVAL;
2881
Luciano Coelhob6a55012014-02-27 11:07:21 +02002882 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
2883 !chandef->chan))
Luciano Coelho73de86a2014-02-13 11:31:59 +02002884 return -EINVAL;
2885
Luciano Coelhob6a55012014-02-27 11:07:21 +02002886 if (chandef)
2887 num_different_channels = 1;
2888
Luciano Coelho73de86a2014-02-13 11:31:59 +02002889 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
2890 return -EINVAL;
2891
2892 /* Always allow software iftypes */
2893 if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
2894 if (radar_detect)
2895 return -EINVAL;
2896 return 0;
2897 }
2898
2899 memset(num, 0, sizeof(num));
2900
2901 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
2902 num[iftype] = 1;
2903
2904 list_for_each_entry(ctx, &local->chanctx_list, list) {
2905 if (ctx->conf.radar_enabled)
2906 radar_detect |= BIT(ctx->conf.def.width);
2907 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
2908 num_different_channels++;
2909 continue;
2910 }
Luciano Coelhob6a55012014-02-27 11:07:21 +02002911 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
Luciano Coelho73de86a2014-02-13 11:31:59 +02002912 cfg80211_chandef_compatible(chandef,
2913 &ctx->conf.def))
2914 continue;
2915 num_different_channels++;
2916 }
2917
2918 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
2919 struct wireless_dev *wdev_iter;
2920
2921 wdev_iter = &sdata_iter->wdev;
2922
2923 if (sdata_iter == sdata ||
2924 rcu_access_pointer(sdata_iter->vif.chanctx_conf) == NULL ||
2925 local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
2926 continue;
2927
2928 num[wdev_iter->iftype]++;
2929 total++;
2930 }
2931
2932 if (total == 1 && !radar_detect)
2933 return 0;
2934
2935 return cfg80211_check_combinations(local->hw.wiphy,
2936 num_different_channels,
2937 radar_detect, num);
2938}
Michal Kazior6fa001b2014-04-09 15:29:23 +02002939
2940static void
2941ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
2942 void *data)
2943{
2944 u32 *max_num_different_channels = data;
2945
2946 *max_num_different_channels = max(*max_num_different_channels,
2947 c->num_different_channels);
2948}
2949
2950int ieee80211_max_num_channels(struct ieee80211_local *local)
2951{
2952 struct ieee80211_sub_if_data *sdata;
2953 int num[NUM_NL80211_IFTYPES] = {};
2954 struct ieee80211_chanctx *ctx;
2955 int num_different_channels = 0;
2956 u8 radar_detect = 0;
2957 u32 max_num_different_channels = 1;
2958 int err;
2959
2960 lockdep_assert_held(&local->chanctx_mtx);
2961
2962 list_for_each_entry(ctx, &local->chanctx_list, list) {
2963 num_different_channels++;
2964
2965 if (ctx->conf.radar_enabled)
2966 radar_detect |= BIT(ctx->conf.def.width);
2967 }
2968
2969 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2970 num[sdata->wdev.iftype]++;
2971
2972 err = cfg80211_iter_combinations(local->hw.wiphy,
2973 num_different_channels, radar_detect,
2974 num, ieee80211_iter_max_chans,
2975 &max_num_different_channels);
2976 if (err < 0)
2977 return err;
2978
2979 return max_num_different_channels;
2980}