blob: c379c99cd1d835c1978bddfa8ba7a464455c8701 [file] [log] [blame]
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001/*
2 * mac80211 TDLS handling code
3 *
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2014, Intel Corporation
Johannes Bergd98ad832014-09-03 15:24:57 +03006 * Copyright 2014 Intel Mobile Communications GmbH
Arik Nemtsov59021c62016-03-02 23:28:32 +02007 * Copyright 2015 - 2016 Intel Deutschland GmbH
Arik Nemtsov95224fe2014-05-01 10:17:28 +03008 *
9 * This file is GPLv2 as found in COPYING.
10 */
11
12#include <linux/ieee80211.h>
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +030013#include <linux/log2.h>
Arik Nemtsovc887f0d32014-06-11 17:18:25 +030014#include <net/cfg80211.h>
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +030015#include <linux/rtnetlink.h>
Arik Nemtsov95224fe2014-05-01 10:17:28 +030016#include "ieee80211_i.h"
Arik Nemtsovee10f2c2014-06-11 17:18:27 +030017#include "driver-ops.h"
Arik Nemtsov59021c62016-03-02 23:28:32 +020018#include "rate.h"
Arik Nemtsov95224fe2014-05-01 10:17:28 +030019
Arik Nemtsov17e6a592014-06-11 17:18:20 +030020/* give usermode some time for retries in setting up the TDLS session */
21#define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
22
23void ieee80211_tdls_peer_del_work(struct work_struct *wk)
24{
25 struct ieee80211_sub_if_data *sdata;
26 struct ieee80211_local *local;
27
28 sdata = container_of(wk, struct ieee80211_sub_if_data,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030029 u.mgd.tdls_peer_del_work.work);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030030 local = sdata->local;
31
32 mutex_lock(&local->mtx);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030033 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
34 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
35 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
36 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030037 }
38 mutex_unlock(&local->mtx);
39}
40
Arik Nemtsovb98fb442015-06-10 20:42:59 +030041static void ieee80211_tdls_add_ext_capab(struct ieee80211_sub_if_data *sdata,
Arik Nemtsov78632a12014-11-09 18:50:14 +020042 struct sk_buff *skb)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030043{
Arik Nemtsovb98fb442015-06-10 20:42:59 +030044 struct ieee80211_local *local = sdata->local;
Arik Nemtsov82c0cc90d2015-08-15 22:39:46 +030045 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov78632a12014-11-09 18:50:14 +020046 bool chan_switch = local->hw.wiphy->features &
47 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
Arik Nemtsov82c0cc90d2015-08-15 22:39:46 +030048 bool wider_band = ieee80211_hw_check(&local->hw, TDLS_WIDER_BW) &&
49 !ifmgd->tdls_wider_bw_prohibited;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +053050 struct ieee80211_supported_band *sband = ieee80211_get_sband(sdata);
Arik Nemtsovb98fb442015-06-10 20:42:59 +030051 bool vht = sband && sband->vht_cap.vht_supported;
52 u8 *pos = (void *)skb_put(skb, 10);
Arik Nemtsov95224fe2014-05-01 10:17:28 +030053
54 *pos++ = WLAN_EID_EXT_CAPABILITY;
Arik Nemtsovb98fb442015-06-10 20:42:59 +030055 *pos++ = 8; /* len */
Arik Nemtsov95224fe2014-05-01 10:17:28 +030056 *pos++ = 0x0;
57 *pos++ = 0x0;
58 *pos++ = 0x0;
Arik Nemtsov78632a12014-11-09 18:50:14 +020059 *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030060 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
Arik Nemtsovb98fb442015-06-10 20:42:59 +030061 *pos++ = 0;
62 *pos++ = 0;
63 *pos++ = (vht && wider_band) ? WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED : 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030064}
65
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020066static u8
67ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
68 struct sk_buff *skb, u16 start, u16 end,
69 u16 spacing)
70{
71 u8 subband_cnt = 0, ch_cnt = 0;
72 struct ieee80211_channel *ch;
73 struct cfg80211_chan_def chandef;
74 int i, subband_start;
Arik Nemtsov923b3522015-07-08 15:41:44 +030075 struct wiphy *wiphy = sdata->local->hw.wiphy;
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020076
77 for (i = start; i <= end; i += spacing) {
78 if (!ch_cnt)
79 subband_start = i;
80
81 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
82 if (ch) {
83 /* we will be active on the channel */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020084 cfg80211_chandef_create(&chandef, ch,
Arik Nemtsov50075892015-01-07 16:45:07 +020085 NL80211_CHAN_NO_HT);
Arik Nemtsov923b3522015-07-08 15:41:44 +030086 if (cfg80211_reg_can_beacon_relax(wiphy, &chandef,
87 sdata->wdev.iftype)) {
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020088 ch_cnt++;
Arik Nemtsov50075892015-01-07 16:45:07 +020089 /*
90 * check if the next channel is also part of
91 * this allowed range
92 */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020093 continue;
94 }
95 }
96
Arik Nemtsov50075892015-01-07 16:45:07 +020097 /*
98 * we've reached the end of a range, with allowed channels
99 * found
100 */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200101 if (ch_cnt) {
102 u8 *pos = skb_put(skb, 2);
103 *pos++ = ieee80211_frequency_to_channel(subband_start);
104 *pos++ = ch_cnt;
105
106 subband_cnt++;
107 ch_cnt = 0;
108 }
109 }
110
Arik Nemtsov50075892015-01-07 16:45:07 +0200111 /* all channels in the requested range are allowed - add them here */
112 if (ch_cnt) {
113 u8 *pos = skb_put(skb, 2);
114 *pos++ = ieee80211_frequency_to_channel(subband_start);
115 *pos++ = ch_cnt;
116
117 subband_cnt++;
118 }
119
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200120 return subband_cnt;
121}
122
123static void
124ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
125 struct sk_buff *skb)
126{
127 /*
128 * Add possible channels for TDLS. These are channels that are allowed
129 * to be active.
130 */
131 u8 subband_cnt;
132 u8 *pos = skb_put(skb, 2);
133
134 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
135
136 /*
137 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
138 * this doesn't happen in real world scenarios.
139 */
140
141 /* 2GHz, with 5MHz spacing */
142 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
143
144 /* 5GHz, with 20MHz spacing */
145 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
146
147 /* length */
148 *pos = 2 * subband_cnt;
149}
150
Arik Nemtsova38700d2015-03-18 08:46:08 +0200151static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
152 struct sk_buff *skb)
153{
154 u8 *pos;
155 u8 op_class;
156
157 if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
158 &op_class))
159 return;
160
161 pos = skb_put(skb, 4);
162 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
163 *pos++ = 2; /* len */
164
165 *pos++ = op_class;
166 *pos++ = op_class; /* give current operating class as alternate too */
167}
168
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200169static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
170{
171 u8 *pos = (void *)skb_put(skb, 3);
172
173 *pos++ = WLAN_EID_BSS_COEX_2040;
174 *pos++ = 1; /* len */
175
176 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
177}
178
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300179static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
180 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300181{
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530182 struct ieee80211_supported_band *sband;
183
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300184 /* The capability will be 0 when sending a failure code */
185 if (status_code != 0)
186 return 0;
187
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530188 sband = ieee80211_get_sband(sdata);
189 if (sband && sband->band == NL80211_BAND_2GHZ) {
Johannes Bergea1b2b452015-06-02 20:15:49 +0200190 return WLAN_CAPABILITY_SHORT_SLOT_TIME |
191 WLAN_CAPABILITY_SHORT_PREAMBLE;
192 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300193
Johannes Bergea1b2b452015-06-02 20:15:49 +0200194 return 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300195}
196
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300197static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
198 struct sk_buff *skb, const u8 *peer,
199 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300200{
201 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300202 const u8 *init_addr, *rsp_addr;
203
204 if (initiator) {
205 init_addr = sdata->vif.addr;
206 rsp_addr = peer;
207 } else {
208 init_addr = peer;
209 rsp_addr = sdata->vif.addr;
210 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300211
212 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
213
214 lnkid->ie_type = WLAN_EID_LINK_ID;
215 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
216
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300217 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
218 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
219 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300220}
221
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200222static void
223ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
224{
225 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
226 u8 *pos = (void *)skb_put(skb, 4);
227
228 *pos++ = WLAN_EID_AID;
229 *pos++ = 2; /* len */
230 put_unaligned_le16(ifmgd->aid, pos);
231}
232
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300233/* translate numbering in the WMM parameter IE to the mac80211 notation */
234static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
235{
236 switch (ac) {
237 default:
238 WARN_ON_ONCE(1);
239 case 0:
240 return IEEE80211_AC_BE;
241 case 1:
242 return IEEE80211_AC_BK;
243 case 2:
244 return IEEE80211_AC_VI;
245 case 3:
246 return IEEE80211_AC_VO;
247 }
248}
249
250static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
251{
252 u8 ret;
253
254 ret = aifsn & 0x0f;
255 if (acm)
256 ret |= 0x10;
257 ret |= (aci << 5) & 0x60;
258 return ret;
259}
260
261static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
262{
263 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
264 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
265}
266
267static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
268 struct sk_buff *skb)
269{
270 struct ieee80211_wmm_param_ie *wmm;
271 struct ieee80211_tx_queue_params *txq;
272 int i;
273
Johannes Bergb080db52017-06-16 14:29:19 +0200274 wmm = skb_put_zero(skb, sizeof(*wmm));
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300275
276 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
277 wmm->len = sizeof(*wmm) - 2;
278
279 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
280 wmm->oui[1] = 0x50;
281 wmm->oui[2] = 0xf2;
282 wmm->oui_type = 2; /* WME */
283 wmm->oui_subtype = 1; /* WME param */
284 wmm->version = 1; /* WME ver */
285 wmm->qos_info = 0; /* U-APSD not in use */
286
287 /*
288 * Use the EDCA parameters defined for the BSS, or default if the AP
289 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
290 */
291 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
292 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
293 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
294 txq->acm, i);
295 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
296 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
297 }
298}
299
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300300static void
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300301ieee80211_tdls_chandef_vht_upgrade(struct ieee80211_sub_if_data *sdata,
302 struct sta_info *sta)
303{
304 /* IEEE802.11ac-2013 Table E-4 */
305 u16 centers_80mhz[] = { 5210, 5290, 5530, 5610, 5690, 5775 };
306 struct cfg80211_chan_def uc = sta->tdls_chandef;
Arik Nemtsov59021c62016-03-02 23:28:32 +0200307 enum nl80211_chan_width max_width = ieee80211_sta_cap_chan_bw(sta);
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300308 int i;
309
310 /* only support upgrading non-narrow channels up to 80Mhz */
311 if (max_width == NL80211_CHAN_WIDTH_5 ||
312 max_width == NL80211_CHAN_WIDTH_10)
313 return;
314
315 if (max_width > NL80211_CHAN_WIDTH_80)
316 max_width = NL80211_CHAN_WIDTH_80;
317
Ilan Peer4b559ec2016-03-08 13:35:31 +0200318 if (uc.width >= max_width)
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300319 return;
320 /*
321 * Channel usage constrains in the IEEE802.11ac-2013 specification only
322 * allow expanding a 20MHz channel to 80MHz in a single way. In
323 * addition, there are no 40MHz allowed channels that are not part of
324 * the allowed 80MHz range in the 5GHz spectrum (the relevant one here).
325 */
326 for (i = 0; i < ARRAY_SIZE(centers_80mhz); i++)
327 if (abs(uc.chan->center_freq - centers_80mhz[i]) <= 30) {
328 uc.center_freq1 = centers_80mhz[i];
Ilan Peer4b559ec2016-03-08 13:35:31 +0200329 uc.center_freq2 = 0;
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300330 uc.width = NL80211_CHAN_WIDTH_80;
331 break;
332 }
333
334 if (!uc.center_freq1)
335 return;
336
Arik Nemtsov554d0722016-08-29 12:37:35 +0300337 /* proceed to downgrade the chandef until usable or the same as AP BW */
Arik Nemtsovdb8d9972016-03-02 23:28:31 +0200338 while (uc.width > max_width ||
Arik Nemtsov554d0722016-08-29 12:37:35 +0300339 (uc.width > sta->tdls_chandef.width &&
340 !cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &uc,
341 sdata->wdev.iftype)))
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300342 ieee80211_chandef_downgrade(&uc);
343
344 if (!cfg80211_chandef_identical(&uc, &sta->tdls_chandef)) {
345 tdls_dbg(sdata, "TDLS ch width upgraded %d -> %d\n",
346 sta->tdls_chandef.width, uc.width);
347
348 /*
349 * the station is not yet authorized when BW upgrade is done,
350 * locking is not required
351 */
352 sta->tdls_chandef = uc;
353 }
354}
355
356static void
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300357ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
358 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300359 u8 action_code, bool initiator,
360 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300361{
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300362 struct ieee80211_supported_band *sband;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530363 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300364 struct ieee80211_sta_ht_cap ht_cap;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200365 struct ieee80211_sta_vht_cap vht_cap;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300366 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300367 size_t offset = 0, noffset;
368 u8 *pos;
369
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530370 sband = ieee80211_get_sband(sdata);
371 if (!sband)
372 return;
373
374 ieee80211_add_srates_ie(sdata, skb, false, sband->band);
375 ieee80211_add_ext_srates_ie(sdata, skb, false, sband->band);
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200376 ieee80211_tdls_add_supp_channels(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300377
378 /* add any custom IEs that go before Extended Capabilities */
379 if (extra_ies_len) {
380 static const u8 before_ext_cap[] = {
381 WLAN_EID_SUPP_RATES,
382 WLAN_EID_COUNTRY,
383 WLAN_EID_EXT_SUPP_RATES,
384 WLAN_EID_SUPPORTED_CHANNELS,
385 WLAN_EID_RSN,
386 };
387 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
388 before_ext_cap,
389 ARRAY_SIZE(before_ext_cap),
390 offset);
391 pos = skb_put(skb, noffset - offset);
392 memcpy(pos, extra_ies + offset, noffset - offset);
393 offset = noffset;
394 }
395
Arik Nemtsovb98fb442015-06-10 20:42:59 +0300396 ieee80211_tdls_add_ext_capab(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300397
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300398 /* add the QoS element if we support it */
399 if (local->hw.queues >= IEEE80211_NUM_ACS &&
400 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
401 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
402
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300403 /* add any custom IEs that go before HT capabilities */
404 if (extra_ies_len) {
405 static const u8 before_ht_cap[] = {
406 WLAN_EID_SUPP_RATES,
407 WLAN_EID_COUNTRY,
408 WLAN_EID_EXT_SUPP_RATES,
409 WLAN_EID_SUPPORTED_CHANNELS,
410 WLAN_EID_RSN,
411 WLAN_EID_EXT_CAPABILITY,
412 WLAN_EID_QOS_CAPA,
413 WLAN_EID_FAST_BSS_TRANSITION,
414 WLAN_EID_TIMEOUT_INTERVAL,
415 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
416 };
417 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
418 before_ht_cap,
419 ARRAY_SIZE(before_ht_cap),
420 offset);
421 pos = skb_put(skb, noffset - offset);
422 memcpy(pos, extra_ies + offset, noffset - offset);
423 offset = noffset;
424 }
425
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300426 mutex_lock(&local->sta_mtx);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200427
428 /* we should have the peer STA if we're already responding */
429 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
430 sta = sta_info_get(sdata, peer);
431 if (WARN_ON_ONCE(!sta)) {
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300432 mutex_unlock(&local->sta_mtx);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200433 return;
434 }
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300435
436 sta->tdls_chandef = sdata->vif.bss_conf.chandef;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200437 }
438
Arik Nemtsova38700d2015-03-18 08:46:08 +0200439 ieee80211_tdls_add_oper_classes(sdata, skb);
440
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300441 /*
442 * with TDLS we can switch channels, and HT-caps are not necessarily
443 * the same on all bands. The specification limits the setup to a
444 * single HT-cap, so use the current band for now.
445 */
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300446 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300447
Arik Nemtsov070e1762015-03-30 11:16:23 +0300448 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
449 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
450 ht_cap.ht_supported) {
Johannes Bergc5309ba2015-01-23 11:42:14 +0100451 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300452
Johannes Bergc5309ba2015-01-23 11:42:14 +0100453 /* disable SMPS in TDLS initiator */
454 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
455 << IEEE80211_HT_CAP_SM_PS_SHIFT;
456
457 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
458 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
459 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
460 ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
Johannes Bergc5309ba2015-01-23 11:42:14 +0100461 /* the peer caps are already intersected with our own */
462 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300463
464 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
465 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
466 }
467
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200468 if (ht_cap.ht_supported &&
469 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
470 ieee80211_tdls_add_bss_coex_ie(skb);
471
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200472 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
473
474 /* add any custom IEs that go before VHT capabilities */
475 if (extra_ies_len) {
476 static const u8 before_vht_cap[] = {
477 WLAN_EID_SUPP_RATES,
478 WLAN_EID_COUNTRY,
479 WLAN_EID_EXT_SUPP_RATES,
480 WLAN_EID_SUPPORTED_CHANNELS,
481 WLAN_EID_RSN,
482 WLAN_EID_EXT_CAPABILITY,
483 WLAN_EID_QOS_CAPA,
484 WLAN_EID_FAST_BSS_TRANSITION,
485 WLAN_EID_TIMEOUT_INTERVAL,
486 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
487 WLAN_EID_MULTI_BAND,
488 };
489 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
490 before_vht_cap,
491 ARRAY_SIZE(before_vht_cap),
492 offset);
493 pos = skb_put(skb, noffset - offset);
494 memcpy(pos, extra_ies + offset, noffset - offset);
495 offset = noffset;
496 }
497
498 /* build the VHT-cap similarly to the HT-cap */
499 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Arik Nemtsov070e1762015-03-30 11:16:23 +0300500 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
501 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
502 vht_cap.vht_supported) {
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200503 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
504
505 /* the AID is present only when VHT is implemented */
Arik Nemtsov070e1762015-03-30 11:16:23 +0300506 if (action_code == WLAN_TDLS_SETUP_REQUEST)
507 ieee80211_tdls_add_aid(sdata, skb);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200508
509 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
510 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
511 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
512 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
513 /* the peer caps are already intersected with our own */
514 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
515
516 /* the AID is present only when VHT is implemented */
517 ieee80211_tdls_add_aid(sdata, skb);
518
519 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
520 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300521
522 /*
523 * if both peers support WIDER_BW, we can expand the chandef to
524 * a wider compatible one, up to 80MHz
525 */
526 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
527 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200528 }
529
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300530 mutex_unlock(&local->sta_mtx);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200531
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300532 /* add any remaining IEs */
533 if (extra_ies_len) {
534 noffset = extra_ies_len;
535 pos = skb_put(skb, noffset - offset);
536 memcpy(pos, extra_ies + offset, noffset - offset);
537 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300538
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300539}
540
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300541static void
542ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
543 struct sk_buff *skb, const u8 *peer,
544 bool initiator, const u8 *extra_ies,
545 size_t extra_ies_len)
546{
547 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300548 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300549 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300550 struct sta_info *sta, *ap_sta;
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530551 struct ieee80211_supported_band *sband;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300552 u8 *pos;
553
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530554 sband = ieee80211_get_sband(sdata);
555 if (!sband)
556 return;
557
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300558 mutex_lock(&local->sta_mtx);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300559
560 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300561 ap_sta = sta_info_get(sdata, ifmgd->bssid);
562 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300563 mutex_unlock(&local->sta_mtx);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300564 return;
565 }
566
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300567 sta->tdls_chandef = sdata->vif.bss_conf.chandef;
568
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300569 /* add any custom IEs that go before the QoS IE */
570 if (extra_ies_len) {
571 static const u8 before_qos[] = {
572 WLAN_EID_RSN,
573 };
574 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
575 before_qos,
576 ARRAY_SIZE(before_qos),
577 offset);
578 pos = skb_put(skb, noffset - offset);
579 memcpy(pos, extra_ies + offset, noffset - offset);
580 offset = noffset;
581 }
582
583 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200584 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300585 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
586
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300587 /* add any custom IEs that go before HT operation */
588 if (extra_ies_len) {
589 static const u8 before_ht_op[] = {
590 WLAN_EID_RSN,
591 WLAN_EID_QOS_CAPA,
592 WLAN_EID_FAST_BSS_TRANSITION,
593 WLAN_EID_TIMEOUT_INTERVAL,
594 };
595 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
596 before_ht_op,
597 ARRAY_SIZE(before_ht_op),
598 offset);
599 pos = skb_put(skb, noffset - offset);
600 memcpy(pos, extra_ies + offset, noffset - offset);
601 offset = noffset;
602 }
603
Arik Nemtsov57f255f2015-10-25 10:59:34 +0200604 /*
605 * if HT support is only added in TDLS, we need an HT-operation IE.
606 * add the IE as required by IEEE802.11-2012 9.23.3.2.
607 */
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300608 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
Arik Nemtsov57f255f2015-10-25 10:59:34 +0200609 u16 prot = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
610 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
611 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
612
Arik Nemtsov890b7872015-05-07 15:30:41 +0300613 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
Arik Nemtsov890b7872015-05-07 15:30:41 +0300614 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
Arik Nemtsov57f255f2015-10-25 10:59:34 +0200615 &sdata->vif.bss_conf.chandef, prot,
616 true);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300617 }
618
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200619 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
620
621 /* only include VHT-operation if not on the 2.4GHz band */
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530622 if (sband->band != NL80211_BAND_2GHZ &&
623 sta->sta.vht_cap.vht_supported) {
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300624 /*
625 * if both peers support WIDER_BW, we can expand the chandef to
626 * a wider compatible one, up to 80MHz
627 */
628 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
629 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
630
Arik Nemtsov890b7872015-05-07 15:30:41 +0300631 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
632 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300633 &sta->tdls_chandef);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200634 }
635
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300636 mutex_unlock(&local->sta_mtx);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300637
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300638 /* add any remaining IEs */
639 if (extra_ies_len) {
640 noffset = extra_ies_len;
641 pos = skb_put(skb, noffset - offset);
642 memcpy(pos, extra_ies + offset, noffset - offset);
643 }
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300644}
645
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200646static void
647ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
648 struct sk_buff *skb, const u8 *peer,
649 bool initiator, const u8 *extra_ies,
650 size_t extra_ies_len, u8 oper_class,
651 struct cfg80211_chan_def *chandef)
652{
653 struct ieee80211_tdls_data *tf;
654 size_t offset = 0, noffset;
655 u8 *pos;
656
657 if (WARN_ON_ONCE(!chandef))
658 return;
659
660 tf = (void *)skb->data;
661 tf->u.chan_switch_req.target_channel =
662 ieee80211_frequency_to_channel(chandef->chan->center_freq);
663 tf->u.chan_switch_req.oper_class = oper_class;
664
665 if (extra_ies_len) {
666 static const u8 before_lnkie[] = {
667 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
668 };
669 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
670 before_lnkie,
671 ARRAY_SIZE(before_lnkie),
672 offset);
673 pos = skb_put(skb, noffset - offset);
674 memcpy(pos, extra_ies + offset, noffset - offset);
675 offset = noffset;
676 }
677
678 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
679
680 /* add any remaining IEs */
681 if (extra_ies_len) {
682 noffset = extra_ies_len;
683 pos = skb_put(skb, noffset - offset);
684 memcpy(pos, extra_ies + offset, noffset - offset);
685 }
686}
687
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200688static void
689ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
690 struct sk_buff *skb, const u8 *peer,
691 u16 status_code, bool initiator,
692 const u8 *extra_ies,
693 size_t extra_ies_len)
694{
695 if (status_code == 0)
696 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
697
698 if (extra_ies_len)
699 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
700}
701
Arik Nemtsov46792a22014-07-17 17:14:19 +0300702static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
703 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300704 u8 action_code, u16 status_code,
705 bool initiator, const u8 *extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200706 size_t extra_ies_len, u8 oper_class,
707 struct cfg80211_chan_def *chandef)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300708{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300709 switch (action_code) {
710 case WLAN_TDLS_SETUP_REQUEST:
711 case WLAN_TDLS_SETUP_RESPONSE:
712 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300713 if (status_code == 0)
714 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
715 action_code,
716 initiator,
717 extra_ies,
718 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300719 break;
720 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300721 if (status_code == 0)
722 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
723 initiator, extra_ies,
724 extra_ies_len);
725 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300726 case WLAN_TDLS_TEARDOWN:
727 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300728 if (extra_ies_len)
729 memcpy(skb_put(skb, extra_ies_len), extra_ies,
730 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300731 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
732 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300733 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200734 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
735 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
736 initiator, extra_ies,
737 extra_ies_len,
738 oper_class, chandef);
739 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200740 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
741 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
742 status_code,
743 initiator, extra_ies,
744 extra_ies_len);
745 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300746 }
747
Arik Nemtsov46792a22014-07-17 17:14:19 +0300748}
749
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300750static int
751ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200752 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300753 u16 status_code, struct sk_buff *skb)
754{
755 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300756 struct ieee80211_tdls_data *tf;
757
758 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
759
760 memcpy(tf->da, peer, ETH_ALEN);
761 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
762 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
763 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
764
Arik Nemtsov59cd85c2014-09-09 17:11:02 +0300765 /* network header is after the ethernet header */
766 skb_set_network_header(skb, ETH_HLEN);
767
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300768 switch (action_code) {
769 case WLAN_TDLS_SETUP_REQUEST:
770 tf->category = WLAN_CATEGORY_TDLS;
771 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
772
773 skb_put(skb, sizeof(tf->u.setup_req));
774 tf->u.setup_req.dialog_token = dialog_token;
775 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300776 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
777 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300778 break;
779 case WLAN_TDLS_SETUP_RESPONSE:
780 tf->category = WLAN_CATEGORY_TDLS;
781 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
782
783 skb_put(skb, sizeof(tf->u.setup_resp));
784 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
785 tf->u.setup_resp.dialog_token = dialog_token;
786 tf->u.setup_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300787 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
788 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300789 break;
790 case WLAN_TDLS_SETUP_CONFIRM:
791 tf->category = WLAN_CATEGORY_TDLS;
792 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
793
794 skb_put(skb, sizeof(tf->u.setup_cfm));
795 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
796 tf->u.setup_cfm.dialog_token = dialog_token;
797 break;
798 case WLAN_TDLS_TEARDOWN:
799 tf->category = WLAN_CATEGORY_TDLS;
800 tf->action_code = WLAN_TDLS_TEARDOWN;
801
802 skb_put(skb, sizeof(tf->u.teardown));
803 tf->u.teardown.reason_code = cpu_to_le16(status_code);
804 break;
805 case WLAN_TDLS_DISCOVERY_REQUEST:
806 tf->category = WLAN_CATEGORY_TDLS;
807 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
808
809 skb_put(skb, sizeof(tf->u.discover_req));
810 tf->u.discover_req.dialog_token = dialog_token;
811 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200812 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
813 tf->category = WLAN_CATEGORY_TDLS;
814 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
815
816 skb_put(skb, sizeof(tf->u.chan_switch_req));
817 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200818 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
819 tf->category = WLAN_CATEGORY_TDLS;
820 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
821
822 skb_put(skb, sizeof(tf->u.chan_switch_resp));
823 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
824 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300825 default:
826 return -EINVAL;
827 }
828
829 return 0;
830}
831
832static int
833ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200834 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300835 u16 status_code, struct sk_buff *skb)
836{
837 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300838 struct ieee80211_mgmt *mgmt;
839
Johannes Bergb080db52017-06-16 14:29:19 +0200840 mgmt = skb_put_zero(skb, 24);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300841 memcpy(mgmt->da, peer, ETH_ALEN);
842 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
843 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
844
845 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
846 IEEE80211_STYPE_ACTION);
847
848 switch (action_code) {
849 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
850 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
851 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
852 mgmt->u.action.u.tdls_discover_resp.action_code =
853 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
854 mgmt->u.action.u.tdls_discover_resp.dialog_token =
855 dialog_token;
856 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300857 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
858 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300859 break;
860 default:
861 return -EINVAL;
862 }
863
864 return 0;
865}
866
Arik Nemtsovc2733902014-11-09 18:50:16 +0200867static struct sk_buff *
868ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
869 const u8 *peer, u8 action_code,
870 u8 dialog_token, u16 status_code,
871 bool initiator, const u8 *extra_ies,
872 size_t extra_ies_len, u8 oper_class,
873 struct cfg80211_chan_def *chandef)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300874{
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300875 struct ieee80211_local *local = sdata->local;
Arik Nemtsovc2733902014-11-09 18:50:16 +0200876 struct sk_buff *skb;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300877 int ret;
878
Arik Nemtsovc2733902014-11-09 18:50:16 +0200879 skb = netdev_alloc_skb(sdata->dev,
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200880 local->hw.extra_tx_headroom +
881 max(sizeof(struct ieee80211_mgmt),
882 sizeof(struct ieee80211_tdls_data)) +
883 50 + /* supported rates */
Arik Nemtsovb98fb442015-06-10 20:42:59 +0300884 10 + /* ext capab */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200885 26 + /* max(WMM-info, WMM-param) */
886 2 + max(sizeof(struct ieee80211_ht_cap),
887 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200888 2 + max(sizeof(struct ieee80211_vht_cap),
889 sizeof(struct ieee80211_vht_operation)) +
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200890 50 + /* supported channels */
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200891 3 + /* 40/20 BSS coex */
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200892 4 + /* AID */
Arik Nemtsova38700d2015-03-18 08:46:08 +0200893 4 + /* oper classes */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200894 extra_ies_len +
895 sizeof(struct ieee80211_tdls_lnkie));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300896 if (!skb)
Arik Nemtsovc2733902014-11-09 18:50:16 +0200897 return NULL;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300898
899 skb_reserve(skb, local->hw.extra_tx_headroom);
900
901 switch (action_code) {
902 case WLAN_TDLS_SETUP_REQUEST:
903 case WLAN_TDLS_SETUP_RESPONSE:
904 case WLAN_TDLS_SETUP_CONFIRM:
905 case WLAN_TDLS_TEARDOWN:
906 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200907 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200908 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200909 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
910 sdata->dev, peer,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300911 action_code, dialog_token,
912 status_code, skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300913 break;
914 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200915 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
916 peer, action_code,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300917 dialog_token, status_code,
918 skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300919 break;
920 default:
921 ret = -ENOTSUPP;
922 break;
923 }
924
925 if (ret < 0)
926 goto fail;
927
Arik Nemtsovc2733902014-11-09 18:50:16 +0200928 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
929 initiator, extra_ies, extra_ies_len, oper_class,
930 chandef);
931 return skb;
932
933fail:
934 dev_kfree_skb(skb);
935 return NULL;
936}
937
938static int
939ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
940 const u8 *peer, u8 action_code, u8 dialog_token,
941 u16 status_code, u32 peer_capability,
942 bool initiator, const u8 *extra_ies,
943 size_t extra_ies_len, u8 oper_class,
944 struct cfg80211_chan_def *chandef)
945{
946 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
947 struct sk_buff *skb = NULL;
948 struct sta_info *sta;
949 u32 flags = 0;
950 int ret = 0;
951
Arik Nemtsov626911c2014-07-17 17:14:17 +0300952 rcu_read_lock();
953 sta = sta_info_get(sdata, peer);
954
955 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300956 switch (action_code) {
957 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov8b941482014-10-22 12:32:48 +0300958 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300959 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300960 sta->sta.tdls_initiator = false;
961 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300962 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300963 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300964 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300965 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300966 break;
967 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300968 /*
969 * In some testing scenarios, we send a request and response.
970 * Make the last packet sent take effect for the initiator
971 * value.
972 */
Arik Nemtsov8b941482014-10-22 12:32:48 +0300973 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300974 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300975 sta->sta.tdls_initiator = true;
976 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300977 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300978 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300979 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300980 break;
981 case WLAN_TDLS_TEARDOWN:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200982 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200983 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300984 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300985 break;
986 default:
987 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300988 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300989 }
990
Arik Nemtsov46792a22014-07-17 17:14:19 +0300991 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
992 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300993
Arik Nemtsov626911c2014-07-17 17:14:17 +0300994 rcu_read_unlock();
995 if (ret < 0)
996 goto fail;
997
Arik Nemtsovc2733902014-11-09 18:50:16 +0200998 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
999 dialog_token, status_code,
1000 initiator, extra_ies,
1001 extra_ies_len, oper_class,
1002 chandef);
1003 if (!skb) {
1004 ret = -EINVAL;
1005 goto fail;
1006 }
1007
1008 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001009 ieee80211_tx_skb(sdata, skb);
1010 return 0;
1011 }
1012
1013 /*
1014 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
1015 * we should default to AC_VI.
1016 */
1017 switch (action_code) {
1018 case WLAN_TDLS_SETUP_REQUEST:
1019 case WLAN_TDLS_SETUP_RESPONSE:
1020 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
1021 skb->priority = 2;
1022 break;
1023 default:
1024 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
1025 skb->priority = 5;
1026 break;
1027 }
1028
Liad Kaufman1277b4a2014-11-09 18:50:08 +02001029 /*
1030 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
1031 * Later, if no ACK is returned from peer, we will re-send the teardown
1032 * packet through the AP.
1033 */
1034 if ((action_code == WLAN_TDLS_TEARDOWN) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001035 ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Liad Kaufman1277b4a2014-11-09 18:50:08 +02001036 bool try_resend; /* Should we keep skb for possible resend */
1037
1038 /* If not sending directly to peer - no point in keeping skb */
1039 rcu_read_lock();
1040 sta = sta_info_get(sdata, peer);
1041 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1042 rcu_read_unlock();
1043
1044 spin_lock_bh(&sdata->u.mgd.teardown_lock);
1045 if (try_resend && !sdata->u.mgd.teardown_skb) {
1046 /* Mark it as requiring TX status callback */
1047 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1048 IEEE80211_TX_INTFL_MLME_CONN_TX;
1049
1050 /*
1051 * skb is copied since mac80211 will later set
1052 * properties that might not be the same as the AP,
1053 * such as encryption, QoS, addresses, etc.
1054 *
1055 * No problem if skb_copy() fails, so no need to check.
1056 */
1057 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
1058 sdata->u.mgd.orig_teardown_skb = skb;
1059 }
1060 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
1061 }
1062
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001063 /* disable bottom halves when entering the Tx path */
1064 local_bh_disable();
Liad Kaufman1277b4a2014-11-09 18:50:08 +02001065 __ieee80211_subif_start_xmit(skb, dev, flags);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001066 local_bh_enable();
1067
1068 return ret;
1069
1070fail:
1071 dev_kfree_skb(skb);
1072 return ret;
1073}
1074
Arik Nemtsov191dd462014-06-11 17:18:23 +03001075static int
1076ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
1077 const u8 *peer, u8 action_code, u8 dialog_token,
1078 u16 status_code, u32 peer_capability, bool initiator,
1079 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001080{
1081 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1082 struct ieee80211_local *local = sdata->local;
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001083 enum ieee80211_smps_mode smps_mode = sdata->u.mgd.driver_smps_mode;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001084 int ret;
1085
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001086 /* don't support setup with forced SMPS mode that's not off */
1087 if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
1088 smps_mode != IEEE80211_SMPS_OFF) {
1089 tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d\n",
1090 smps_mode);
1091 return -ENOTSUPP;
1092 }
1093
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001094 mutex_lock(&local->mtx);
1095
1096 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001097 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
1098 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001099 ret = -EBUSY;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001100 goto out_unlock;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001101 }
1102
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001103 /*
1104 * make sure we have a STA representing the peer so we drop or buffer
1105 * non-TDLS-setup frames to the peer. We can't send other packets
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001106 * during setup through the AP path.
1107 * Allow error packets to be sent - sometimes we don't even add a STA
1108 * before failing the setup.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001109 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001110 if (status_code == 0) {
1111 rcu_read_lock();
1112 if (!sta_info_get(sdata, peer)) {
1113 rcu_read_unlock();
1114 ret = -ENOLINK;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001115 goto out_unlock;
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001116 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001117 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001118 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001119
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001120 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001121 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1122 mutex_unlock(&local->mtx);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001123
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001124 /* we cannot take the mutex while preparing the setup packet */
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001125 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1126 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +03001127 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001128 extra_ies, extra_ies_len, 0,
1129 NULL);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001130 if (ret < 0) {
1131 mutex_lock(&local->mtx);
1132 eth_zero_addr(sdata->u.mgd.tdls_peer);
1133 mutex_unlock(&local->mtx);
1134 return ret;
1135 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001136
Arik Nemtsov191dd462014-06-11 17:18:23 +03001137 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001138 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +03001139 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001140 return 0;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001141
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001142out_unlock:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001143 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001144 return ret;
1145}
1146
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001147static int
1148ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1149 const u8 *peer, u8 action_code, u8 dialog_token,
1150 u16 status_code, u32 peer_capability,
1151 bool initiator, const u8 *extra_ies,
1152 size_t extra_ies_len)
1153{
1154 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1155 struct ieee80211_local *local = sdata->local;
1156 struct sta_info *sta;
1157 int ret;
1158
1159 /*
1160 * No packets can be transmitted to the peer via the AP during setup -
1161 * the STA is set as a TDLS peer, but is not authorized.
1162 * During teardown, we prevent direct transmissions by stopping the
1163 * queues and flushing all direct packets.
1164 */
1165 ieee80211_stop_vif_queues(local, sdata,
1166 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001167 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001168
1169 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1170 dialog_token, status_code,
1171 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001172 extra_ies, extra_ies_len, 0,
1173 NULL);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001174 if (ret < 0)
1175 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1176 ret);
1177
1178 /*
1179 * Remove the STA AUTH flag to force further traffic through the AP. If
1180 * the STA was unreachable, it was already removed.
1181 */
1182 rcu_read_lock();
1183 sta = sta_info_get(sdata, peer);
1184 if (sta)
1185 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1186 rcu_read_unlock();
1187
1188 ieee80211_wake_vif_queues(local, sdata,
1189 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1190
1191 return 0;
1192}
1193
Arik Nemtsov191dd462014-06-11 17:18:23 +03001194int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1195 const u8 *peer, u8 action_code, u8 dialog_token,
1196 u16 status_code, u32 peer_capability,
1197 bool initiator, const u8 *extra_ies,
1198 size_t extra_ies_len)
1199{
1200 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1201 int ret;
1202
1203 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1204 return -ENOTSUPP;
1205
1206 /* make sure we are in managed mode, and associated */
1207 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1208 !sdata->u.mgd.associated)
1209 return -EINVAL;
1210
1211 switch (action_code) {
1212 case WLAN_TDLS_SETUP_REQUEST:
1213 case WLAN_TDLS_SETUP_RESPONSE:
1214 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1215 dialog_token, status_code,
1216 peer_capability, initiator,
1217 extra_ies, extra_ies_len);
1218 break;
1219 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001220 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1221 action_code, dialog_token,
1222 status_code,
1223 peer_capability, initiator,
1224 extra_ies, extra_ies_len);
1225 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +03001226 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +03001227 /*
1228 * Protect the discovery so we can hear the TDLS discovery
1229 * response frame. It is transmitted directly and not buffered
1230 * by the AP.
1231 */
1232 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1233 /* fall-through */
1234 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +03001235 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1236 /* no special handling */
1237 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1238 action_code,
1239 dialog_token,
1240 status_code,
1241 peer_capability,
1242 initiator, extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001243 extra_ies_len, 0, NULL);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001244 break;
1245 default:
1246 ret = -EOPNOTSUPP;
1247 break;
1248 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001249
1250 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1251 action_code, peer, ret);
1252 return ret;
1253}
1254
Arik Nemtsov59021c62016-03-02 23:28:32 +02001255static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata,
1256 struct sta_info *sta)
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001257{
1258 struct ieee80211_local *local = sdata->local;
1259 struct ieee80211_chanctx_conf *conf;
1260 struct ieee80211_chanctx *ctx;
Arik Nemtsov59021c62016-03-02 23:28:32 +02001261 enum nl80211_chan_width width;
1262 struct ieee80211_supported_band *sband;
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001263
1264 mutex_lock(&local->chanctx_mtx);
1265 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1266 lockdep_is_held(&local->chanctx_mtx));
1267 if (conf) {
Arik Nemtsov59021c62016-03-02 23:28:32 +02001268 width = conf->def.width;
1269 sband = local->hw.wiphy->bands[conf->def.chan->band];
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001270 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1271 ieee80211_recalc_chanctx_chantype(local, ctx);
Arik Nemtsov59021c62016-03-02 23:28:32 +02001272
1273 /* if width changed and a peer is given, update its BW */
1274 if (width != conf->def.width && sta &&
1275 test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW)) {
1276 enum ieee80211_sta_rx_bandwidth bw;
1277
1278 bw = ieee80211_chan_width_to_rx_bw(conf->def.width);
1279 bw = min(bw, ieee80211_sta_cap_rx_bw(sta));
1280 if (bw != sta->sta.bandwidth) {
1281 sta->sta.bandwidth = bw;
1282 rate_control_rate_update(local, sband, sta,
1283 IEEE80211_RC_BW_CHANGED);
1284 /*
1285 * if a TDLS peer BW was updated, we need to
1286 * recalc the chandef width again, to get the
1287 * correct chanctx min_def
1288 */
1289 ieee80211_recalc_chanctx_chantype(local, ctx);
1290 }
1291 }
1292
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001293 }
1294 mutex_unlock(&local->chanctx_mtx);
1295}
1296
Avri Altman22f66892015-08-18 16:52:07 +03001297static int iee80211_tdls_have_ht_peers(struct ieee80211_sub_if_data *sdata)
1298{
1299 struct sta_info *sta;
1300 bool result = false;
1301
1302 rcu_read_lock();
1303 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1304 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1305 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
1306 !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH) ||
1307 !sta->sta.ht_cap.ht_supported)
1308 continue;
1309 result = true;
1310 break;
1311 }
1312 rcu_read_unlock();
1313
1314 return result;
1315}
1316
1317static void
1318iee80211_tdls_recalc_ht_protection(struct ieee80211_sub_if_data *sdata,
1319 struct sta_info *sta)
1320{
1321 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1322 bool tdls_ht;
1323 u16 protection = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
1324 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
1325 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
1326 u16 opmode;
1327
1328 /* Nothing to do if the BSS connection uses HT */
1329 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
1330 return;
1331
1332 tdls_ht = (sta && sta->sta.ht_cap.ht_supported) ||
1333 iee80211_tdls_have_ht_peers(sdata);
1334
1335 opmode = sdata->vif.bss_conf.ht_operation_mode;
1336
1337 if (tdls_ht)
1338 opmode |= protection;
1339 else
1340 opmode &= ~protection;
1341
1342 if (opmode == sdata->vif.bss_conf.ht_operation_mode)
1343 return;
1344
1345 sdata->vif.bss_conf.ht_operation_mode = opmode;
1346 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1347}
1348
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001349int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +02001350 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001351{
1352 struct sta_info *sta;
1353 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001354 struct ieee80211_local *local = sdata->local;
1355 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001356
1357 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1358 return -ENOTSUPP;
1359
1360 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1361 return -EINVAL;
1362
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001363 switch (oper) {
1364 case NL80211_TDLS_ENABLE_LINK:
1365 case NL80211_TDLS_DISABLE_LINK:
1366 break;
1367 case NL80211_TDLS_TEARDOWN:
1368 case NL80211_TDLS_SETUP:
1369 case NL80211_TDLS_DISCOVERY_REQ:
1370 /* We don't support in-driver setup/teardown/discovery */
1371 return -ENOTSUPP;
1372 }
1373
Avri Altman22f66892015-08-18 16:52:07 +03001374 /* protect possible bss_conf changes and avoid concurrency in
1375 * ieee80211_bss_info_change_notify()
1376 */
1377 sdata_lock(sdata);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001378 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001379 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1380
1381 switch (oper) {
1382 case NL80211_TDLS_ENABLE_LINK:
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001383 if (sdata->vif.csa_active) {
1384 tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
1385 ret = -EBUSY;
1386 break;
1387 }
1388
Avri Altman22f66892015-08-18 16:52:07 +03001389 mutex_lock(&local->sta_mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001390 sta = sta_info_get(sdata, peer);
1391 if (!sta) {
Avri Altman22f66892015-08-18 16:52:07 +03001392 mutex_unlock(&local->sta_mtx);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001393 ret = -ENOLINK;
1394 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001395 }
1396
Arik Nemtsov59021c62016-03-02 23:28:32 +02001397 iee80211_tdls_recalc_chanctx(sdata, sta);
Avri Altman22f66892015-08-18 16:52:07 +03001398 iee80211_tdls_recalc_ht_protection(sdata, sta);
1399
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001400 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
Avri Altman22f66892015-08-18 16:52:07 +03001401 mutex_unlock(&local->sta_mtx);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001402
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001403 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1404 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001405 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001406 break;
1407 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +03001408 /*
1409 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1410 * created while the queues were stopped, so it might still be
1411 * pending. Before flushing the queues we need to be sure the
1412 * message is handled by the tasklet handling pending messages,
1413 * otherwise we might start destroying the station before
1414 * sending the teardown packet.
1415 * Note that this only forces the tasklet to flush pendings -
1416 * not to stop the tasklet from rescheduling itself.
1417 */
1418 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001419 /* flush a potentially queued teardown packet */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001420 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001421
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001422 ret = sta_info_destroy_addr(sdata, peer);
Avri Altman22f66892015-08-18 16:52:07 +03001423
1424 mutex_lock(&local->sta_mtx);
1425 iee80211_tdls_recalc_ht_protection(sdata, NULL);
1426 mutex_unlock(&local->sta_mtx);
1427
Arik Nemtsov59021c62016-03-02 23:28:32 +02001428 iee80211_tdls_recalc_chanctx(sdata, NULL);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001429 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001430 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001431 ret = -ENOTSUPP;
1432 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001433 }
1434
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001435 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1436 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1437 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001438 }
1439
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001440 if (ret == 0)
1441 ieee80211_queue_work(&sdata->local->hw,
1442 &sdata->u.mgd.request_smps_work);
1443
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001444 mutex_unlock(&local->mtx);
Avri Altman22f66892015-08-18 16:52:07 +03001445 sdata_unlock(sdata);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001446 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001447}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +03001448
1449void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1450 enum nl80211_tdls_operation oper,
1451 u16 reason_code, gfp_t gfp)
1452{
1453 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1454
1455 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1456 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1457 oper);
1458 return;
1459 }
1460
1461 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1462}
1463EXPORT_SYMBOL(ieee80211_tdls_oper_request);
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +02001464
1465static void
1466iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1467{
1468 struct ieee80211_ch_switch_timing *ch_sw;
1469
1470 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1471 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1472
1473 ch_sw = (void *)buf;
1474 ch_sw->switch_time = cpu_to_le16(switch_time);
1475 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1476}
1477
1478/* find switch timing IE in SKB ready for Tx */
1479static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1480{
1481 struct ieee80211_tdls_data *tf;
1482 const u8 *ie_start;
1483
1484 /*
1485 * Get the offset for the new location of the switch timing IE.
1486 * The SKB network header will now point to the "payload_type"
1487 * element of the TDLS data frame struct.
1488 */
1489 tf = container_of(skb->data + skb_network_offset(skb),
1490 struct ieee80211_tdls_data, payload_type);
1491 ie_start = tf->u.chan_switch_req.variable;
1492 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1493 skb->len - (ie_start - skb->data));
1494}
1495
1496static struct sk_buff *
1497ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1498 struct cfg80211_chan_def *chandef,
1499 u32 *ch_sw_tm_ie_offset)
1500{
1501 struct ieee80211_sub_if_data *sdata = sta->sdata;
1502 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1503 2 + sizeof(struct ieee80211_ch_switch_timing)];
1504 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1505 u8 *pos = extra_ies;
1506 struct sk_buff *skb;
1507
1508 /*
1509 * if chandef points to a wide channel add a Secondary-Channel
1510 * Offset information element
1511 */
1512 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1513 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1514 bool ht40plus;
1515
1516 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1517 *pos++ = sizeof(*sec_chan_ie);
1518 sec_chan_ie = (void *)pos;
1519
1520 ht40plus = cfg80211_get_chandef_type(chandef) ==
1521 NL80211_CHAN_HT40PLUS;
1522 sec_chan_ie->sec_chan_offs = ht40plus ?
1523 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1524 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1525 pos += sizeof(*sec_chan_ie);
1526
1527 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1528 }
1529
1530 /* just set the values to 0, this is a template */
1531 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1532
1533 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1534 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1535 0, 0, !sta->sta.tdls_initiator,
1536 extra_ies, extra_ies_len,
1537 oper_class, chandef);
1538 if (!skb)
1539 return NULL;
1540
1541 skb = ieee80211_build_data_template(sdata, skb, 0);
1542 if (IS_ERR(skb)) {
1543 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1544 return NULL;
1545 }
1546
1547 if (ch_sw_tm_ie_offset) {
1548 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1549
1550 if (!tm_ie) {
1551 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1552 dev_kfree_skb_any(skb);
1553 return NULL;
1554 }
1555
1556 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1557 }
1558
1559 tdls_dbg(sdata,
1560 "TDLS channel switch request template for %pM ch %d width %d\n",
1561 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1562 return skb;
1563}
1564
1565int
1566ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1567 const u8 *addr, u8 oper_class,
1568 struct cfg80211_chan_def *chandef)
1569{
1570 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1571 struct ieee80211_local *local = sdata->local;
1572 struct sta_info *sta;
1573 struct sk_buff *skb = NULL;
1574 u32 ch_sw_tm_ie;
1575 int ret;
1576
1577 mutex_lock(&local->sta_mtx);
1578 sta = sta_info_get(sdata, addr);
1579 if (!sta) {
1580 tdls_dbg(sdata,
1581 "Invalid TDLS peer %pM for channel switch request\n",
1582 addr);
1583 ret = -ENOENT;
1584 goto out;
1585 }
1586
1587 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1588 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1589 addr);
1590 ret = -ENOTSUPP;
1591 goto out;
1592 }
1593
1594 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1595 &ch_sw_tm_ie);
1596 if (!skb) {
1597 ret = -ENOENT;
1598 goto out;
1599 }
1600
1601 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1602 chandef, skb, ch_sw_tm_ie);
1603 if (!ret)
1604 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1605
1606out:
1607 mutex_unlock(&local->sta_mtx);
1608 dev_kfree_skb_any(skb);
1609 return ret;
1610}
1611
1612void
1613ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1614 struct net_device *dev,
1615 const u8 *addr)
1616{
1617 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1618 struct ieee80211_local *local = sdata->local;
1619 struct sta_info *sta;
1620
1621 mutex_lock(&local->sta_mtx);
1622 sta = sta_info_get(sdata, addr);
1623 if (!sta) {
1624 tdls_dbg(sdata,
1625 "Invalid TDLS peer %pM for channel switch cancel\n",
1626 addr);
1627 goto out;
1628 }
1629
1630 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1631 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1632 addr);
1633 goto out;
1634 }
1635
1636 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1637 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1638
1639out:
1640 mutex_unlock(&local->sta_mtx);
1641}
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001642
1643static struct sk_buff *
1644ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1645 u32 *ch_sw_tm_ie_offset)
1646{
1647 struct ieee80211_sub_if_data *sdata = sta->sdata;
1648 struct sk_buff *skb;
1649 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1650
1651 /* initial timing are always zero in the template */
1652 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1653
1654 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1655 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1656 0, 0, !sta->sta.tdls_initiator,
1657 extra_ies, sizeof(extra_ies), 0, NULL);
1658 if (!skb)
1659 return NULL;
1660
1661 skb = ieee80211_build_data_template(sdata, skb, 0);
1662 if (IS_ERR(skb)) {
1663 tdls_dbg(sdata,
1664 "Failed building TDLS channel switch resp frame\n");
1665 return NULL;
1666 }
1667
1668 if (ch_sw_tm_ie_offset) {
1669 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1670
1671 if (!tm_ie) {
1672 tdls_dbg(sdata,
1673 "No switch timing IE in TDLS switch resp\n");
1674 dev_kfree_skb_any(skb);
1675 return NULL;
1676 }
1677
1678 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1679 }
1680
1681 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1682 sta->sta.addr);
1683 return skb;
1684}
1685
1686static int
1687ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1688 struct sk_buff *skb)
1689{
1690 struct ieee80211_local *local = sdata->local;
1691 struct ieee802_11_elems elems;
1692 struct sta_info *sta;
1693 struct ieee80211_tdls_data *tf = (void *)skb->data;
1694 bool local_initiator;
1695 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1696 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1697 struct ieee80211_tdls_ch_sw_params params = {};
1698 int ret;
1699
1700 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1701 params.timestamp = rx_status->device_timestamp;
1702
1703 if (skb->len < baselen) {
1704 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1705 skb->len);
1706 return -EINVAL;
1707 }
1708
1709 mutex_lock(&local->sta_mtx);
1710 sta = sta_info_get(sdata, tf->sa);
1711 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1712 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1713 tf->sa);
1714 ret = -EINVAL;
1715 goto out;
1716 }
1717
1718 params.sta = &sta->sta;
1719 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1720 if (params.status != 0) {
1721 ret = 0;
1722 goto call_drv;
1723 }
1724
1725 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1726 skb->len - baselen, false, &elems);
1727 if (elems.parse_error) {
1728 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1729 ret = -EINVAL;
1730 goto out;
1731 }
1732
1733 if (!elems.ch_sw_timing || !elems.lnk_id) {
1734 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1735 ret = -EINVAL;
1736 goto out;
1737 }
1738
1739 /* validate the initiator is set correctly */
1740 local_initiator =
1741 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1742 if (local_initiator == sta->sta.tdls_initiator) {
1743 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1749 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1750
1751 params.tmpl_skb =
1752 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1753 if (!params.tmpl_skb) {
1754 ret = -ENOENT;
1755 goto out;
1756 }
1757
Dan Carpenter49708e32016-06-27 17:31:18 +03001758 ret = 0;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001759call_drv:
1760 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1761
1762 tdls_dbg(sdata,
1763 "TDLS channel switch response received from %pM status %d\n",
1764 tf->sa, params.status);
1765
1766out:
1767 mutex_unlock(&local->sta_mtx);
1768 dev_kfree_skb_any(params.tmpl_skb);
1769 return ret;
1770}
1771
1772static int
1773ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1774 struct sk_buff *skb)
1775{
1776 struct ieee80211_local *local = sdata->local;
1777 struct ieee802_11_elems elems;
1778 struct cfg80211_chan_def chandef;
1779 struct ieee80211_channel *chan;
1780 enum nl80211_channel_type chan_type;
1781 int freq;
1782 u8 target_channel, oper_class;
1783 bool local_initiator;
1784 struct sta_info *sta;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001785 enum nl80211_band band;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001786 struct ieee80211_tdls_data *tf = (void *)skb->data;
1787 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1788 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1789 struct ieee80211_tdls_ch_sw_params params = {};
1790 int ret = 0;
1791
1792 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1793 params.timestamp = rx_status->device_timestamp;
1794
1795 if (skb->len < baselen) {
1796 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1797 skb->len);
1798 return -EINVAL;
1799 }
1800
1801 target_channel = tf->u.chan_switch_req.target_channel;
1802 oper_class = tf->u.chan_switch_req.oper_class;
1803
1804 /*
1805 * We can't easily infer the channel band. The operating class is
1806 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1807 * solution here is to treat channels with number >14 as 5GHz ones,
1808 * and specifically check for the (oper_class, channel) combinations
1809 * where this doesn't hold. These are thankfully unique according to
1810 * IEEE802.11-2012.
1811 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1812 * valid here.
1813 */
1814 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1815 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1816 target_channel < 14)
Johannes Berg57fbcce2016-04-12 15:56:15 +02001817 band = NL80211_BAND_5GHZ;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001818 else
Johannes Berg57fbcce2016-04-12 15:56:15 +02001819 band = target_channel < 14 ? NL80211_BAND_2GHZ :
1820 NL80211_BAND_5GHZ;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001821
1822 freq = ieee80211_channel_to_frequency(target_channel, band);
1823 if (freq == 0) {
1824 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1825 target_channel);
1826 return -EINVAL;
1827 }
1828
1829 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1830 if (!chan) {
1831 tdls_dbg(sdata,
1832 "Unsupported channel for TDLS chan switch: %d\n",
1833 target_channel);
1834 return -EINVAL;
1835 }
1836
1837 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1838 skb->len - baselen, false, &elems);
1839 if (elems.parse_error) {
1840 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1841 return -EINVAL;
1842 }
1843
1844 if (!elems.ch_sw_timing || !elems.lnk_id) {
1845 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1846 return -EINVAL;
1847 }
1848
Arik Nemtsov42d8d782015-07-08 15:41:46 +03001849 if (!elems.sec_chan_offs) {
1850 chan_type = NL80211_CHAN_HT20;
1851 } else {
1852 switch (elems.sec_chan_offs->sec_chan_offs) {
1853 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1854 chan_type = NL80211_CHAN_HT40PLUS;
1855 break;
1856 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1857 chan_type = NL80211_CHAN_HT40MINUS;
1858 break;
1859 default:
1860 chan_type = NL80211_CHAN_HT20;
1861 break;
1862 }
1863 }
1864
1865 cfg80211_chandef_create(&chandef, chan, chan_type);
1866
1867 /* we will be active on the TDLS link */
1868 if (!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &chandef,
1869 sdata->wdev.iftype)) {
1870 tdls_dbg(sdata, "TDLS chan switch to forbidden channel\n");
1871 return -EINVAL;
1872 }
1873
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001874 mutex_lock(&local->sta_mtx);
1875 sta = sta_info_get(sdata, tf->sa);
1876 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1877 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1878 tf->sa);
1879 ret = -EINVAL;
1880 goto out;
1881 }
1882
1883 params.sta = &sta->sta;
1884
1885 /* validate the initiator is set correctly */
1886 local_initiator =
1887 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1888 if (local_initiator == sta->sta.tdls_initiator) {
1889 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1890 ret = -EINVAL;
1891 goto out;
1892 }
1893
Arik Nemtsov42d8d782015-07-08 15:41:46 +03001894 /* peer should have known better */
1895 if (!sta->sta.ht_cap.ht_supported && elems.sec_chan_offs &&
1896 elems.sec_chan_offs->sec_chan_offs) {
1897 tdls_dbg(sdata, "TDLS chan switch - wide chan unsupported\n");
1898 ret = -ENOTSUPP;
1899 goto out;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001900 }
1901
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001902 params.chandef = &chandef;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001903 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1904 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1905
1906 params.tmpl_skb =
1907 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1908 &params.ch_sw_tm_ie);
1909 if (!params.tmpl_skb) {
1910 ret = -ENOENT;
1911 goto out;
1912 }
1913
1914 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1915
1916 tdls_dbg(sdata,
1917 "TDLS ch switch request received from %pM ch %d width %d\n",
1918 tf->sa, params.chandef->chan->center_freq,
1919 params.chandef->width);
1920out:
1921 mutex_unlock(&local->sta_mtx);
1922 dev_kfree_skb_any(params.tmpl_skb);
1923 return ret;
1924}
1925
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03001926static void
1927ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1928 struct sk_buff *skb)
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001929{
1930 struct ieee80211_tdls_data *tf = (void *)skb->data;
1931 struct wiphy *wiphy = sdata->local->hw.wiphy;
1932
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03001933 ASSERT_RTNL();
1934
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001935 /* make sure the driver supports it */
1936 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1937 return;
1938
1939 /* we want to access the entire packet */
1940 if (skb_linearize(skb))
1941 return;
1942 /*
1943 * The packet/size was already validated by mac80211 Rx path, only look
1944 * at the action type.
1945 */
1946 switch (tf->action_code) {
1947 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1948 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1949 break;
1950 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1951 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1952 break;
1953 default:
1954 WARN_ON_ONCE(1);
1955 return;
1956 }
1957}
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001958
1959void ieee80211_teardown_tdls_peers(struct ieee80211_sub_if_data *sdata)
1960{
1961 struct sta_info *sta;
1962 u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
1963
1964 rcu_read_lock();
1965 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1966 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1967 !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1968 continue;
1969
1970 ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
1971 NL80211_TDLS_TEARDOWN, reason,
1972 GFP_ATOMIC);
1973 }
1974 rcu_read_unlock();
1975}
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03001976
1977void ieee80211_tdls_chsw_work(struct work_struct *wk)
1978{
1979 struct ieee80211_local *local =
1980 container_of(wk, struct ieee80211_local, tdls_chsw_work);
1981 struct ieee80211_sub_if_data *sdata;
1982 struct sk_buff *skb;
1983 struct ieee80211_tdls_data *tf;
1984
1985 rtnl_lock();
1986 while ((skb = skb_dequeue(&local->skb_queue_tdls_chsw))) {
1987 tf = (struct ieee80211_tdls_data *)skb->data;
1988 list_for_each_entry(sdata, &local->interfaces, list) {
1989 if (!ieee80211_sdata_running(sdata) ||
1990 sdata->vif.type != NL80211_IFTYPE_STATION ||
1991 !ether_addr_equal(tf->da, sdata->vif.addr))
1992 continue;
1993
1994 ieee80211_process_tdls_channel_switch(sdata, skb);
1995 break;
1996 }
1997
1998 kfree_skb(skb);
1999 }
2000 rtnl_unlock();
2001}