blob: 4e202d0679b26a0dfa0b89c5ae189f2ce7b56883 [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 Nemtsovd51c2ea2015-06-14 16:53:46 +03007 * Copyright 2015 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 Nemtsov95224fe2014-05-01 10:17:28 +030018
Arik Nemtsov17e6a592014-06-11 17:18:20 +030019/* give usermode some time for retries in setting up the TDLS session */
20#define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
21
22void ieee80211_tdls_peer_del_work(struct work_struct *wk)
23{
24 struct ieee80211_sub_if_data *sdata;
25 struct ieee80211_local *local;
26
27 sdata = container_of(wk, struct ieee80211_sub_if_data,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030028 u.mgd.tdls_peer_del_work.work);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030029 local = sdata->local;
30
31 mutex_lock(&local->mtx);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030032 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
33 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
34 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
35 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030036 }
37 mutex_unlock(&local->mtx);
38}
39
Arik Nemtsovb98fb442015-06-10 20:42:59 +030040static void ieee80211_tdls_add_ext_capab(struct ieee80211_sub_if_data *sdata,
Arik Nemtsov78632a12014-11-09 18:50:14 +020041 struct sk_buff *skb)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030042{
Arik Nemtsovb98fb442015-06-10 20:42:59 +030043 struct ieee80211_local *local = sdata->local;
Arik Nemtsov78632a12014-11-09 18:50:14 +020044 bool chan_switch = local->hw.wiphy->features &
45 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
Arik Nemtsovb98fb442015-06-10 20:42:59 +030046 bool wider_band = ieee80211_hw_check(&local->hw, TDLS_WIDER_BW);
47 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
48 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
49 bool vht = sband && sband->vht_cap.vht_supported;
50 u8 *pos = (void *)skb_put(skb, 10);
Arik Nemtsov95224fe2014-05-01 10:17:28 +030051
52 *pos++ = WLAN_EID_EXT_CAPABILITY;
Arik Nemtsovb98fb442015-06-10 20:42:59 +030053 *pos++ = 8; /* len */
Arik Nemtsov95224fe2014-05-01 10:17:28 +030054 *pos++ = 0x0;
55 *pos++ = 0x0;
56 *pos++ = 0x0;
Arik Nemtsov78632a12014-11-09 18:50:14 +020057 *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030058 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
Arik Nemtsovb98fb442015-06-10 20:42:59 +030059 *pos++ = 0;
60 *pos++ = 0;
61 *pos++ = (vht && wider_band) ? WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED : 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030062}
63
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020064static u8
65ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
66 struct sk_buff *skb, u16 start, u16 end,
67 u16 spacing)
68{
69 u8 subband_cnt = 0, ch_cnt = 0;
70 struct ieee80211_channel *ch;
71 struct cfg80211_chan_def chandef;
72 int i, subband_start;
Arik Nemtsov923b3522015-07-08 15:41:44 +030073 struct wiphy *wiphy = sdata->local->hw.wiphy;
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020074
75 for (i = start; i <= end; i += spacing) {
76 if (!ch_cnt)
77 subband_start = i;
78
79 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
80 if (ch) {
81 /* we will be active on the channel */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020082 cfg80211_chandef_create(&chandef, ch,
Arik Nemtsov50075892015-01-07 16:45:07 +020083 NL80211_CHAN_NO_HT);
Arik Nemtsov923b3522015-07-08 15:41:44 +030084 if (cfg80211_reg_can_beacon_relax(wiphy, &chandef,
85 sdata->wdev.iftype)) {
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020086 ch_cnt++;
Arik Nemtsov50075892015-01-07 16:45:07 +020087 /*
88 * check if the next channel is also part of
89 * this allowed range
90 */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020091 continue;
92 }
93 }
94
Arik Nemtsov50075892015-01-07 16:45:07 +020095 /*
96 * we've reached the end of a range, with allowed channels
97 * found
98 */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020099 if (ch_cnt) {
100 u8 *pos = skb_put(skb, 2);
101 *pos++ = ieee80211_frequency_to_channel(subband_start);
102 *pos++ = ch_cnt;
103
104 subband_cnt++;
105 ch_cnt = 0;
106 }
107 }
108
Arik Nemtsov50075892015-01-07 16:45:07 +0200109 /* all channels in the requested range are allowed - add them here */
110 if (ch_cnt) {
111 u8 *pos = skb_put(skb, 2);
112 *pos++ = ieee80211_frequency_to_channel(subband_start);
113 *pos++ = ch_cnt;
114
115 subband_cnt++;
116 }
117
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200118 return subband_cnt;
119}
120
121static void
122ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
123 struct sk_buff *skb)
124{
125 /*
126 * Add possible channels for TDLS. These are channels that are allowed
127 * to be active.
128 */
129 u8 subband_cnt;
130 u8 *pos = skb_put(skb, 2);
131
132 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
133
134 /*
135 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
136 * this doesn't happen in real world scenarios.
137 */
138
139 /* 2GHz, with 5MHz spacing */
140 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
141
142 /* 5GHz, with 20MHz spacing */
143 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
144
145 /* length */
146 *pos = 2 * subband_cnt;
147}
148
Arik Nemtsova38700d2015-03-18 08:46:08 +0200149static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
150 struct sk_buff *skb)
151{
152 u8 *pos;
153 u8 op_class;
154
155 if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
156 &op_class))
157 return;
158
159 pos = skb_put(skb, 4);
160 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
161 *pos++ = 2; /* len */
162
163 *pos++ = op_class;
164 *pos++ = op_class; /* give current operating class as alternate too */
165}
166
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200167static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
168{
169 u8 *pos = (void *)skb_put(skb, 3);
170
171 *pos++ = WLAN_EID_BSS_COEX_2040;
172 *pos++ = 1; /* len */
173
174 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
175}
176
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300177static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
178 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300179{
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300180 /* The capability will be 0 when sending a failure code */
181 if (status_code != 0)
182 return 0;
183
Johannes Bergea1b2b452015-06-02 20:15:49 +0200184 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_2GHZ) {
185 return WLAN_CAPABILITY_SHORT_SLOT_TIME |
186 WLAN_CAPABILITY_SHORT_PREAMBLE;
187 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300188
Johannes Bergea1b2b452015-06-02 20:15:49 +0200189 return 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300190}
191
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300192static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
193 struct sk_buff *skb, const u8 *peer,
194 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300195{
196 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300197 const u8 *init_addr, *rsp_addr;
198
199 if (initiator) {
200 init_addr = sdata->vif.addr;
201 rsp_addr = peer;
202 } else {
203 init_addr = peer;
204 rsp_addr = sdata->vif.addr;
205 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300206
207 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
208
209 lnkid->ie_type = WLAN_EID_LINK_ID;
210 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
211
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300212 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
213 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
214 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300215}
216
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200217static void
218ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
219{
220 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
221 u8 *pos = (void *)skb_put(skb, 4);
222
223 *pos++ = WLAN_EID_AID;
224 *pos++ = 2; /* len */
225 put_unaligned_le16(ifmgd->aid, pos);
226}
227
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300228/* translate numbering in the WMM parameter IE to the mac80211 notation */
229static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
230{
231 switch (ac) {
232 default:
233 WARN_ON_ONCE(1);
234 case 0:
235 return IEEE80211_AC_BE;
236 case 1:
237 return IEEE80211_AC_BK;
238 case 2:
239 return IEEE80211_AC_VI;
240 case 3:
241 return IEEE80211_AC_VO;
242 }
243}
244
245static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
246{
247 u8 ret;
248
249 ret = aifsn & 0x0f;
250 if (acm)
251 ret |= 0x10;
252 ret |= (aci << 5) & 0x60;
253 return ret;
254}
255
256static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
257{
258 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
259 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
260}
261
262static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
263 struct sk_buff *skb)
264{
265 struct ieee80211_wmm_param_ie *wmm;
266 struct ieee80211_tx_queue_params *txq;
267 int i;
268
269 wmm = (void *)skb_put(skb, sizeof(*wmm));
270 memset(wmm, 0, sizeof(*wmm));
271
272 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
273 wmm->len = sizeof(*wmm) - 2;
274
275 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
276 wmm->oui[1] = 0x50;
277 wmm->oui[2] = 0xf2;
278 wmm->oui_type = 2; /* WME */
279 wmm->oui_subtype = 1; /* WME param */
280 wmm->version = 1; /* WME ver */
281 wmm->qos_info = 0; /* U-APSD not in use */
282
283 /*
284 * Use the EDCA parameters defined for the BSS, or default if the AP
285 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
286 */
287 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
288 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
289 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
290 txq->acm, i);
291 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
292 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
293 }
294}
295
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300296static void
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300297ieee80211_tdls_chandef_vht_upgrade(struct ieee80211_sub_if_data *sdata,
298 struct sta_info *sta)
299{
300 /* IEEE802.11ac-2013 Table E-4 */
301 u16 centers_80mhz[] = { 5210, 5290, 5530, 5610, 5690, 5775 };
302 struct cfg80211_chan_def uc = sta->tdls_chandef;
303 enum nl80211_chan_width max_width = ieee80211_get_sta_bw(&sta->sta);
304 int i;
305
306 /* only support upgrading non-narrow channels up to 80Mhz */
307 if (max_width == NL80211_CHAN_WIDTH_5 ||
308 max_width == NL80211_CHAN_WIDTH_10)
309 return;
310
311 if (max_width > NL80211_CHAN_WIDTH_80)
312 max_width = NL80211_CHAN_WIDTH_80;
313
314 if (uc.width == max_width)
315 return;
316 /*
317 * Channel usage constrains in the IEEE802.11ac-2013 specification only
318 * allow expanding a 20MHz channel to 80MHz in a single way. In
319 * addition, there are no 40MHz allowed channels that are not part of
320 * the allowed 80MHz range in the 5GHz spectrum (the relevant one here).
321 */
322 for (i = 0; i < ARRAY_SIZE(centers_80mhz); i++)
323 if (abs(uc.chan->center_freq - centers_80mhz[i]) <= 30) {
324 uc.center_freq1 = centers_80mhz[i];
325 uc.width = NL80211_CHAN_WIDTH_80;
326 break;
327 }
328
329 if (!uc.center_freq1)
330 return;
331
332 /* proceed to downgrade the chandef until usable or the same */
333 while (uc.width > max_width &&
334 !cfg80211_reg_can_beacon(sdata->local->hw.wiphy,
335 &uc, sdata->wdev.iftype))
336 ieee80211_chandef_downgrade(&uc);
337
338 if (!cfg80211_chandef_identical(&uc, &sta->tdls_chandef)) {
339 tdls_dbg(sdata, "TDLS ch width upgraded %d -> %d\n",
340 sta->tdls_chandef.width, uc.width);
341
342 /*
343 * the station is not yet authorized when BW upgrade is done,
344 * locking is not required
345 */
346 sta->tdls_chandef = uc;
347 }
348}
349
350static void
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300351ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
352 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300353 u8 action_code, bool initiator,
354 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300355{
356 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300357 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300358 struct ieee80211_supported_band *sband;
359 struct ieee80211_sta_ht_cap ht_cap;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200360 struct ieee80211_sta_vht_cap vht_cap;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300361 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300362 size_t offset = 0, noffset;
363 u8 *pos;
364
365 ieee80211_add_srates_ie(sdata, skb, false, band);
366 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200367 ieee80211_tdls_add_supp_channels(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300368
369 /* add any custom IEs that go before Extended Capabilities */
370 if (extra_ies_len) {
371 static const u8 before_ext_cap[] = {
372 WLAN_EID_SUPP_RATES,
373 WLAN_EID_COUNTRY,
374 WLAN_EID_EXT_SUPP_RATES,
375 WLAN_EID_SUPPORTED_CHANNELS,
376 WLAN_EID_RSN,
377 };
378 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
379 before_ext_cap,
380 ARRAY_SIZE(before_ext_cap),
381 offset);
382 pos = skb_put(skb, noffset - offset);
383 memcpy(pos, extra_ies + offset, noffset - offset);
384 offset = noffset;
385 }
386
Arik Nemtsovb98fb442015-06-10 20:42:59 +0300387 ieee80211_tdls_add_ext_capab(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300388
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300389 /* add the QoS element if we support it */
390 if (local->hw.queues >= IEEE80211_NUM_ACS &&
391 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
392 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
393
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300394 /* add any custom IEs that go before HT capabilities */
395 if (extra_ies_len) {
396 static const u8 before_ht_cap[] = {
397 WLAN_EID_SUPP_RATES,
398 WLAN_EID_COUNTRY,
399 WLAN_EID_EXT_SUPP_RATES,
400 WLAN_EID_SUPPORTED_CHANNELS,
401 WLAN_EID_RSN,
402 WLAN_EID_EXT_CAPABILITY,
403 WLAN_EID_QOS_CAPA,
404 WLAN_EID_FAST_BSS_TRANSITION,
405 WLAN_EID_TIMEOUT_INTERVAL,
406 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
407 };
408 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
409 before_ht_cap,
410 ARRAY_SIZE(before_ht_cap),
411 offset);
412 pos = skb_put(skb, noffset - offset);
413 memcpy(pos, extra_ies + offset, noffset - offset);
414 offset = noffset;
415 }
416
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300417 mutex_lock(&local->sta_mtx);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200418
419 /* we should have the peer STA if we're already responding */
420 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
421 sta = sta_info_get(sdata, peer);
422 if (WARN_ON_ONCE(!sta)) {
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300423 mutex_unlock(&local->sta_mtx);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200424 return;
425 }
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300426
427 sta->tdls_chandef = sdata->vif.bss_conf.chandef;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200428 }
429
Arik Nemtsova38700d2015-03-18 08:46:08 +0200430 ieee80211_tdls_add_oper_classes(sdata, skb);
431
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300432 /*
433 * with TDLS we can switch channels, and HT-caps are not necessarily
434 * the same on all bands. The specification limits the setup to a
435 * single HT-cap, so use the current band for now.
436 */
437 sband = local->hw.wiphy->bands[band];
438 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300439
Arik Nemtsov070e1762015-03-30 11:16:23 +0300440 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
441 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
442 ht_cap.ht_supported) {
Johannes Bergc5309ba2015-01-23 11:42:14 +0100443 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300444
Johannes Bergc5309ba2015-01-23 11:42:14 +0100445 /* disable SMPS in TDLS initiator */
446 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
447 << IEEE80211_HT_CAP_SM_PS_SHIFT;
448
449 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
450 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
451 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
452 ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
Johannes Bergc5309ba2015-01-23 11:42:14 +0100453 /* the peer caps are already intersected with our own */
454 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300455
456 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
457 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
458 }
459
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200460 if (ht_cap.ht_supported &&
461 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
462 ieee80211_tdls_add_bss_coex_ie(skb);
463
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200464 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
465
466 /* add any custom IEs that go before VHT capabilities */
467 if (extra_ies_len) {
468 static const u8 before_vht_cap[] = {
469 WLAN_EID_SUPP_RATES,
470 WLAN_EID_COUNTRY,
471 WLAN_EID_EXT_SUPP_RATES,
472 WLAN_EID_SUPPORTED_CHANNELS,
473 WLAN_EID_RSN,
474 WLAN_EID_EXT_CAPABILITY,
475 WLAN_EID_QOS_CAPA,
476 WLAN_EID_FAST_BSS_TRANSITION,
477 WLAN_EID_TIMEOUT_INTERVAL,
478 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
479 WLAN_EID_MULTI_BAND,
480 };
481 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
482 before_vht_cap,
483 ARRAY_SIZE(before_vht_cap),
484 offset);
485 pos = skb_put(skb, noffset - offset);
486 memcpy(pos, extra_ies + offset, noffset - offset);
487 offset = noffset;
488 }
489
490 /* build the VHT-cap similarly to the HT-cap */
491 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Arik Nemtsov070e1762015-03-30 11:16:23 +0300492 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
493 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
494 vht_cap.vht_supported) {
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200495 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
496
497 /* the AID is present only when VHT is implemented */
Arik Nemtsov070e1762015-03-30 11:16:23 +0300498 if (action_code == WLAN_TDLS_SETUP_REQUEST)
499 ieee80211_tdls_add_aid(sdata, skb);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200500
501 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
502 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
503 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
504 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
505 /* the peer caps are already intersected with our own */
506 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
507
508 /* the AID is present only when VHT is implemented */
509 ieee80211_tdls_add_aid(sdata, skb);
510
511 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
512 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300513
514 /*
515 * if both peers support WIDER_BW, we can expand the chandef to
516 * a wider compatible one, up to 80MHz
517 */
518 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
519 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200520 }
521
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300522 mutex_unlock(&local->sta_mtx);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200523
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300524 /* add any remaining IEs */
525 if (extra_ies_len) {
526 noffset = extra_ies_len;
527 pos = skb_put(skb, noffset - offset);
528 memcpy(pos, extra_ies + offset, noffset - offset);
529 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300530
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300531}
532
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300533static void
534ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
535 struct sk_buff *skb, const u8 *peer,
536 bool initiator, const u8 *extra_ies,
537 size_t extra_ies_len)
538{
539 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300540 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300541 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300542 struct sta_info *sta, *ap_sta;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200543 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300544 u8 *pos;
545
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300546 mutex_lock(&local->sta_mtx);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300547
548 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300549 ap_sta = sta_info_get(sdata, ifmgd->bssid);
550 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300551 mutex_unlock(&local->sta_mtx);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300552 return;
553 }
554
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300555 sta->tdls_chandef = sdata->vif.bss_conf.chandef;
556
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300557 /* add any custom IEs that go before the QoS IE */
558 if (extra_ies_len) {
559 static const u8 before_qos[] = {
560 WLAN_EID_RSN,
561 };
562 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
563 before_qos,
564 ARRAY_SIZE(before_qos),
565 offset);
566 pos = skb_put(skb, noffset - offset);
567 memcpy(pos, extra_ies + offset, noffset - offset);
568 offset = noffset;
569 }
570
571 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200572 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300573 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
574
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300575 /* add any custom IEs that go before HT operation */
576 if (extra_ies_len) {
577 static const u8 before_ht_op[] = {
578 WLAN_EID_RSN,
579 WLAN_EID_QOS_CAPA,
580 WLAN_EID_FAST_BSS_TRANSITION,
581 WLAN_EID_TIMEOUT_INTERVAL,
582 };
583 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
584 before_ht_op,
585 ARRAY_SIZE(before_ht_op),
586 offset);
587 pos = skb_put(skb, noffset - offset);
588 memcpy(pos, extra_ies + offset, noffset - offset);
589 offset = noffset;
590 }
591
592 /* if HT support is only added in TDLS, we need an HT-operation IE */
593 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
Arik Nemtsov890b7872015-05-07 15:30:41 +0300594 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
595 /* send an empty HT operation IE */
596 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
597 &sdata->vif.bss_conf.chandef, 0);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300598 }
599
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200600 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
601
602 /* only include VHT-operation if not on the 2.4GHz band */
Arik Nemtsov890b7872015-05-07 15:30:41 +0300603 if (band != IEEE80211_BAND_2GHZ && sta->sta.vht_cap.vht_supported) {
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300604 /*
605 * if both peers support WIDER_BW, we can expand the chandef to
606 * a wider compatible one, up to 80MHz
607 */
608 if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
609 ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
610
Arik Nemtsov890b7872015-05-07 15:30:41 +0300611 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
612 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300613 &sta->tdls_chandef);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200614 }
615
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300616 mutex_unlock(&local->sta_mtx);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300617
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300618 /* add any remaining IEs */
619 if (extra_ies_len) {
620 noffset = extra_ies_len;
621 pos = skb_put(skb, noffset - offset);
622 memcpy(pos, extra_ies + offset, noffset - offset);
623 }
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300624}
625
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200626static void
627ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
628 struct sk_buff *skb, const u8 *peer,
629 bool initiator, const u8 *extra_ies,
630 size_t extra_ies_len, u8 oper_class,
631 struct cfg80211_chan_def *chandef)
632{
633 struct ieee80211_tdls_data *tf;
634 size_t offset = 0, noffset;
635 u8 *pos;
636
637 if (WARN_ON_ONCE(!chandef))
638 return;
639
640 tf = (void *)skb->data;
641 tf->u.chan_switch_req.target_channel =
642 ieee80211_frequency_to_channel(chandef->chan->center_freq);
643 tf->u.chan_switch_req.oper_class = oper_class;
644
645 if (extra_ies_len) {
646 static const u8 before_lnkie[] = {
647 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
648 };
649 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
650 before_lnkie,
651 ARRAY_SIZE(before_lnkie),
652 offset);
653 pos = skb_put(skb, noffset - offset);
654 memcpy(pos, extra_ies + offset, noffset - offset);
655 offset = noffset;
656 }
657
658 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
659
660 /* add any remaining IEs */
661 if (extra_ies_len) {
662 noffset = extra_ies_len;
663 pos = skb_put(skb, noffset - offset);
664 memcpy(pos, extra_ies + offset, noffset - offset);
665 }
666}
667
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200668static void
669ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
670 struct sk_buff *skb, const u8 *peer,
671 u16 status_code, bool initiator,
672 const u8 *extra_ies,
673 size_t extra_ies_len)
674{
675 if (status_code == 0)
676 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
677
678 if (extra_ies_len)
679 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
680}
681
Arik Nemtsov46792a22014-07-17 17:14:19 +0300682static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
683 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300684 u8 action_code, u16 status_code,
685 bool initiator, const u8 *extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200686 size_t extra_ies_len, u8 oper_class,
687 struct cfg80211_chan_def *chandef)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300688{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300689 switch (action_code) {
690 case WLAN_TDLS_SETUP_REQUEST:
691 case WLAN_TDLS_SETUP_RESPONSE:
692 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300693 if (status_code == 0)
694 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
695 action_code,
696 initiator,
697 extra_ies,
698 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300699 break;
700 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300701 if (status_code == 0)
702 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
703 initiator, extra_ies,
704 extra_ies_len);
705 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300706 case WLAN_TDLS_TEARDOWN:
707 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300708 if (extra_ies_len)
709 memcpy(skb_put(skb, extra_ies_len), extra_ies,
710 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300711 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
712 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300713 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200714 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
715 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
716 initiator, extra_ies,
717 extra_ies_len,
718 oper_class, chandef);
719 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200720 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
721 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
722 status_code,
723 initiator, extra_ies,
724 extra_ies_len);
725 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300726 }
727
Arik Nemtsov46792a22014-07-17 17:14:19 +0300728}
729
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300730static int
731ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200732 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300733 u16 status_code, struct sk_buff *skb)
734{
735 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300736 struct ieee80211_tdls_data *tf;
737
738 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
739
740 memcpy(tf->da, peer, ETH_ALEN);
741 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
742 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
743 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
744
Arik Nemtsov59cd85c2014-09-09 17:11:02 +0300745 /* network header is after the ethernet header */
746 skb_set_network_header(skb, ETH_HLEN);
747
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300748 switch (action_code) {
749 case WLAN_TDLS_SETUP_REQUEST:
750 tf->category = WLAN_CATEGORY_TDLS;
751 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
752
753 skb_put(skb, sizeof(tf->u.setup_req));
754 tf->u.setup_req.dialog_token = dialog_token;
755 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300756 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
757 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300758 break;
759 case WLAN_TDLS_SETUP_RESPONSE:
760 tf->category = WLAN_CATEGORY_TDLS;
761 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
762
763 skb_put(skb, sizeof(tf->u.setup_resp));
764 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
765 tf->u.setup_resp.dialog_token = dialog_token;
766 tf->u.setup_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300767 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
768 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300769 break;
770 case WLAN_TDLS_SETUP_CONFIRM:
771 tf->category = WLAN_CATEGORY_TDLS;
772 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
773
774 skb_put(skb, sizeof(tf->u.setup_cfm));
775 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
776 tf->u.setup_cfm.dialog_token = dialog_token;
777 break;
778 case WLAN_TDLS_TEARDOWN:
779 tf->category = WLAN_CATEGORY_TDLS;
780 tf->action_code = WLAN_TDLS_TEARDOWN;
781
782 skb_put(skb, sizeof(tf->u.teardown));
783 tf->u.teardown.reason_code = cpu_to_le16(status_code);
784 break;
785 case WLAN_TDLS_DISCOVERY_REQUEST:
786 tf->category = WLAN_CATEGORY_TDLS;
787 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
788
789 skb_put(skb, sizeof(tf->u.discover_req));
790 tf->u.discover_req.dialog_token = dialog_token;
791 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200792 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
793 tf->category = WLAN_CATEGORY_TDLS;
794 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
795
796 skb_put(skb, sizeof(tf->u.chan_switch_req));
797 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200798 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
799 tf->category = WLAN_CATEGORY_TDLS;
800 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
801
802 skb_put(skb, sizeof(tf->u.chan_switch_resp));
803 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
804 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300805 default:
806 return -EINVAL;
807 }
808
809 return 0;
810}
811
812static int
813ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200814 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300815 u16 status_code, struct sk_buff *skb)
816{
817 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300818 struct ieee80211_mgmt *mgmt;
819
820 mgmt = (void *)skb_put(skb, 24);
821 memset(mgmt, 0, 24);
822 memcpy(mgmt->da, peer, ETH_ALEN);
823 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
824 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
825
826 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
827 IEEE80211_STYPE_ACTION);
828
829 switch (action_code) {
830 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
831 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
832 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
833 mgmt->u.action.u.tdls_discover_resp.action_code =
834 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
835 mgmt->u.action.u.tdls_discover_resp.dialog_token =
836 dialog_token;
837 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300838 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
839 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300840 break;
841 default:
842 return -EINVAL;
843 }
844
845 return 0;
846}
847
Arik Nemtsovc2733902014-11-09 18:50:16 +0200848static struct sk_buff *
849ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
850 const u8 *peer, u8 action_code,
851 u8 dialog_token, u16 status_code,
852 bool initiator, const u8 *extra_ies,
853 size_t extra_ies_len, u8 oper_class,
854 struct cfg80211_chan_def *chandef)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300855{
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300856 struct ieee80211_local *local = sdata->local;
Arik Nemtsovc2733902014-11-09 18:50:16 +0200857 struct sk_buff *skb;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300858 int ret;
859
Arik Nemtsovc2733902014-11-09 18:50:16 +0200860 skb = netdev_alloc_skb(sdata->dev,
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200861 local->hw.extra_tx_headroom +
862 max(sizeof(struct ieee80211_mgmt),
863 sizeof(struct ieee80211_tdls_data)) +
864 50 + /* supported rates */
Arik Nemtsovb98fb442015-06-10 20:42:59 +0300865 10 + /* ext capab */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200866 26 + /* max(WMM-info, WMM-param) */
867 2 + max(sizeof(struct ieee80211_ht_cap),
868 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200869 2 + max(sizeof(struct ieee80211_vht_cap),
870 sizeof(struct ieee80211_vht_operation)) +
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200871 50 + /* supported channels */
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200872 3 + /* 40/20 BSS coex */
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200873 4 + /* AID */
Arik Nemtsova38700d2015-03-18 08:46:08 +0200874 4 + /* oper classes */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200875 extra_ies_len +
876 sizeof(struct ieee80211_tdls_lnkie));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300877 if (!skb)
Arik Nemtsovc2733902014-11-09 18:50:16 +0200878 return NULL;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300879
880 skb_reserve(skb, local->hw.extra_tx_headroom);
881
882 switch (action_code) {
883 case WLAN_TDLS_SETUP_REQUEST:
884 case WLAN_TDLS_SETUP_RESPONSE:
885 case WLAN_TDLS_SETUP_CONFIRM:
886 case WLAN_TDLS_TEARDOWN:
887 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200888 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200889 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200890 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
891 sdata->dev, peer,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300892 action_code, dialog_token,
893 status_code, skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300894 break;
895 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200896 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
897 peer, action_code,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300898 dialog_token, status_code,
899 skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300900 break;
901 default:
902 ret = -ENOTSUPP;
903 break;
904 }
905
906 if (ret < 0)
907 goto fail;
908
Arik Nemtsovc2733902014-11-09 18:50:16 +0200909 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
910 initiator, extra_ies, extra_ies_len, oper_class,
911 chandef);
912 return skb;
913
914fail:
915 dev_kfree_skb(skb);
916 return NULL;
917}
918
919static int
920ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
921 const u8 *peer, u8 action_code, u8 dialog_token,
922 u16 status_code, u32 peer_capability,
923 bool initiator, const u8 *extra_ies,
924 size_t extra_ies_len, u8 oper_class,
925 struct cfg80211_chan_def *chandef)
926{
927 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
928 struct sk_buff *skb = NULL;
929 struct sta_info *sta;
930 u32 flags = 0;
931 int ret = 0;
932
Arik Nemtsov626911c2014-07-17 17:14:17 +0300933 rcu_read_lock();
934 sta = sta_info_get(sdata, peer);
935
936 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300937 switch (action_code) {
938 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov8b941482014-10-22 12:32:48 +0300939 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300940 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300941 sta->sta.tdls_initiator = false;
942 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300943 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300944 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300945 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300946 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300947 break;
948 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300949 /*
950 * In some testing scenarios, we send a request and response.
951 * Make the last packet sent take effect for the initiator
952 * value.
953 */
Arik Nemtsov8b941482014-10-22 12:32:48 +0300954 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300955 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300956 sta->sta.tdls_initiator = true;
957 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300958 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300959 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300960 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300961 break;
962 case WLAN_TDLS_TEARDOWN:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200963 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200964 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300965 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300966 break;
967 default:
968 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300969 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300970 }
971
Arik Nemtsov46792a22014-07-17 17:14:19 +0300972 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
973 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300974
Arik Nemtsov626911c2014-07-17 17:14:17 +0300975 rcu_read_unlock();
976 if (ret < 0)
977 goto fail;
978
Arik Nemtsovc2733902014-11-09 18:50:16 +0200979 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
980 dialog_token, status_code,
981 initiator, extra_ies,
982 extra_ies_len, oper_class,
983 chandef);
984 if (!skb) {
985 ret = -EINVAL;
986 goto fail;
987 }
988
989 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300990 ieee80211_tx_skb(sdata, skb);
991 return 0;
992 }
993
994 /*
995 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
996 * we should default to AC_VI.
997 */
998 switch (action_code) {
999 case WLAN_TDLS_SETUP_REQUEST:
1000 case WLAN_TDLS_SETUP_RESPONSE:
1001 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
1002 skb->priority = 2;
1003 break;
1004 default:
1005 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
1006 skb->priority = 5;
1007 break;
1008 }
1009
Liad Kaufman1277b4a2014-11-09 18:50:08 +02001010 /*
1011 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
1012 * Later, if no ACK is returned from peer, we will re-send the teardown
1013 * packet through the AP.
1014 */
1015 if ((action_code == WLAN_TDLS_TEARDOWN) &&
Johannes Berg30686bf2015-06-02 21:39:54 +02001016 ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
Liad Kaufman1277b4a2014-11-09 18:50:08 +02001017 bool try_resend; /* Should we keep skb for possible resend */
1018
1019 /* If not sending directly to peer - no point in keeping skb */
1020 rcu_read_lock();
1021 sta = sta_info_get(sdata, peer);
1022 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1023 rcu_read_unlock();
1024
1025 spin_lock_bh(&sdata->u.mgd.teardown_lock);
1026 if (try_resend && !sdata->u.mgd.teardown_skb) {
1027 /* Mark it as requiring TX status callback */
1028 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1029 IEEE80211_TX_INTFL_MLME_CONN_TX;
1030
1031 /*
1032 * skb is copied since mac80211 will later set
1033 * properties that might not be the same as the AP,
1034 * such as encryption, QoS, addresses, etc.
1035 *
1036 * No problem if skb_copy() fails, so no need to check.
1037 */
1038 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
1039 sdata->u.mgd.orig_teardown_skb = skb;
1040 }
1041 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
1042 }
1043
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001044 /* disable bottom halves when entering the Tx path */
1045 local_bh_disable();
Liad Kaufman1277b4a2014-11-09 18:50:08 +02001046 __ieee80211_subif_start_xmit(skb, dev, flags);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001047 local_bh_enable();
1048
1049 return ret;
1050
1051fail:
1052 dev_kfree_skb(skb);
1053 return ret;
1054}
1055
Arik Nemtsov191dd462014-06-11 17:18:23 +03001056static int
1057ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
1058 const u8 *peer, u8 action_code, u8 dialog_token,
1059 u16 status_code, u32 peer_capability, bool initiator,
1060 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001061{
1062 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1063 struct ieee80211_local *local = sdata->local;
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001064 enum ieee80211_smps_mode smps_mode = sdata->u.mgd.driver_smps_mode;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001065 int ret;
1066
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001067 /* don't support setup with forced SMPS mode that's not off */
1068 if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
1069 smps_mode != IEEE80211_SMPS_OFF) {
1070 tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d\n",
1071 smps_mode);
1072 return -ENOTSUPP;
1073 }
1074
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001075 mutex_lock(&local->mtx);
1076
1077 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001078 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
1079 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001080 ret = -EBUSY;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001081 goto out_unlock;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001082 }
1083
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001084 /*
1085 * make sure we have a STA representing the peer so we drop or buffer
1086 * non-TDLS-setup frames to the peer. We can't send other packets
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001087 * during setup through the AP path.
1088 * Allow error packets to be sent - sometimes we don't even add a STA
1089 * before failing the setup.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001090 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001091 if (status_code == 0) {
1092 rcu_read_lock();
1093 if (!sta_info_get(sdata, peer)) {
1094 rcu_read_unlock();
1095 ret = -ENOLINK;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001096 goto out_unlock;
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001097 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001098 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001099 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001100
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001101 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001102 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1103 mutex_unlock(&local->mtx);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001104
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001105 /* we cannot take the mutex while preparing the setup packet */
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001106 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1107 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +03001108 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001109 extra_ies, extra_ies_len, 0,
1110 NULL);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001111 if (ret < 0) {
1112 mutex_lock(&local->mtx);
1113 eth_zero_addr(sdata->u.mgd.tdls_peer);
1114 mutex_unlock(&local->mtx);
1115 return ret;
1116 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001117
Arik Nemtsov191dd462014-06-11 17:18:23 +03001118 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001119 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +03001120 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001121 return 0;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001122
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001123out_unlock:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001124 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001125 return ret;
1126}
1127
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001128static int
1129ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1130 const u8 *peer, u8 action_code, u8 dialog_token,
1131 u16 status_code, u32 peer_capability,
1132 bool initiator, const u8 *extra_ies,
1133 size_t extra_ies_len)
1134{
1135 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1136 struct ieee80211_local *local = sdata->local;
1137 struct sta_info *sta;
1138 int ret;
1139
1140 /*
1141 * No packets can be transmitted to the peer via the AP during setup -
1142 * the STA is set as a TDLS peer, but is not authorized.
1143 * During teardown, we prevent direct transmissions by stopping the
1144 * queues and flushing all direct packets.
1145 */
1146 ieee80211_stop_vif_queues(local, sdata,
1147 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001148 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001149
1150 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1151 dialog_token, status_code,
1152 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001153 extra_ies, extra_ies_len, 0,
1154 NULL);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001155 if (ret < 0)
1156 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1157 ret);
1158
1159 /*
1160 * Remove the STA AUTH flag to force further traffic through the AP. If
1161 * the STA was unreachable, it was already removed.
1162 */
1163 rcu_read_lock();
1164 sta = sta_info_get(sdata, peer);
1165 if (sta)
1166 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1167 rcu_read_unlock();
1168
1169 ieee80211_wake_vif_queues(local, sdata,
1170 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1171
1172 return 0;
1173}
1174
Arik Nemtsov191dd462014-06-11 17:18:23 +03001175int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1176 const u8 *peer, u8 action_code, u8 dialog_token,
1177 u16 status_code, u32 peer_capability,
1178 bool initiator, const u8 *extra_ies,
1179 size_t extra_ies_len)
1180{
1181 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1182 int ret;
1183
1184 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1185 return -ENOTSUPP;
1186
1187 /* make sure we are in managed mode, and associated */
1188 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1189 !sdata->u.mgd.associated)
1190 return -EINVAL;
1191
1192 switch (action_code) {
1193 case WLAN_TDLS_SETUP_REQUEST:
1194 case WLAN_TDLS_SETUP_RESPONSE:
1195 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1196 dialog_token, status_code,
1197 peer_capability, initiator,
1198 extra_ies, extra_ies_len);
1199 break;
1200 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001201 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1202 action_code, dialog_token,
1203 status_code,
1204 peer_capability, initiator,
1205 extra_ies, extra_ies_len);
1206 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +03001207 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +03001208 /*
1209 * Protect the discovery so we can hear the TDLS discovery
1210 * response frame. It is transmitted directly and not buffered
1211 * by the AP.
1212 */
1213 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1214 /* fall-through */
1215 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +03001216 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1217 /* no special handling */
1218 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1219 action_code,
1220 dialog_token,
1221 status_code,
1222 peer_capability,
1223 initiator, extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001224 extra_ies_len, 0, NULL);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001225 break;
1226 default:
1227 ret = -EOPNOTSUPP;
1228 break;
1229 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001230
1231 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1232 action_code, peer, ret);
1233 return ret;
1234}
1235
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001236static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata)
1237{
1238 struct ieee80211_local *local = sdata->local;
1239 struct ieee80211_chanctx_conf *conf;
1240 struct ieee80211_chanctx *ctx;
1241
1242 mutex_lock(&local->chanctx_mtx);
1243 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1244 lockdep_is_held(&local->chanctx_mtx));
1245 if (conf) {
1246 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1247 ieee80211_recalc_chanctx_chantype(local, ctx);
1248 }
1249 mutex_unlock(&local->chanctx_mtx);
1250}
1251
Avri Altman22f66892015-08-18 16:52:07 +03001252static int iee80211_tdls_have_ht_peers(struct ieee80211_sub_if_data *sdata)
1253{
1254 struct sta_info *sta;
1255 bool result = false;
1256
1257 rcu_read_lock();
1258 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1259 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1260 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
1261 !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH) ||
1262 !sta->sta.ht_cap.ht_supported)
1263 continue;
1264 result = true;
1265 break;
1266 }
1267 rcu_read_unlock();
1268
1269 return result;
1270}
1271
1272static void
1273iee80211_tdls_recalc_ht_protection(struct ieee80211_sub_if_data *sdata,
1274 struct sta_info *sta)
1275{
1276 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1277 bool tdls_ht;
1278 u16 protection = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
1279 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
1280 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
1281 u16 opmode;
1282
1283 /* Nothing to do if the BSS connection uses HT */
1284 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
1285 return;
1286
1287 tdls_ht = (sta && sta->sta.ht_cap.ht_supported) ||
1288 iee80211_tdls_have_ht_peers(sdata);
1289
1290 opmode = sdata->vif.bss_conf.ht_operation_mode;
1291
1292 if (tdls_ht)
1293 opmode |= protection;
1294 else
1295 opmode &= ~protection;
1296
1297 if (opmode == sdata->vif.bss_conf.ht_operation_mode)
1298 return;
1299
1300 sdata->vif.bss_conf.ht_operation_mode = opmode;
1301 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1302}
1303
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001304int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +02001305 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001306{
1307 struct sta_info *sta;
1308 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001309 struct ieee80211_local *local = sdata->local;
1310 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001311
1312 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1313 return -ENOTSUPP;
1314
1315 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1316 return -EINVAL;
1317
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001318 switch (oper) {
1319 case NL80211_TDLS_ENABLE_LINK:
1320 case NL80211_TDLS_DISABLE_LINK:
1321 break;
1322 case NL80211_TDLS_TEARDOWN:
1323 case NL80211_TDLS_SETUP:
1324 case NL80211_TDLS_DISCOVERY_REQ:
1325 /* We don't support in-driver setup/teardown/discovery */
1326 return -ENOTSUPP;
1327 }
1328
Avri Altman22f66892015-08-18 16:52:07 +03001329 /* protect possible bss_conf changes and avoid concurrency in
1330 * ieee80211_bss_info_change_notify()
1331 */
1332 sdata_lock(sdata);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001333 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001334 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1335
1336 switch (oper) {
1337 case NL80211_TDLS_ENABLE_LINK:
Arik Nemtsovc5a71682015-05-19 14:36:48 +03001338 if (sdata->vif.csa_active) {
1339 tdls_dbg(sdata, "TDLS: disallow link during CSA\n");
1340 ret = -EBUSY;
1341 break;
1342 }
1343
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001344 iee80211_tdls_recalc_chanctx(sdata);
1345
Avri Altman22f66892015-08-18 16:52:07 +03001346 mutex_lock(&local->sta_mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001347 sta = sta_info_get(sdata, peer);
1348 if (!sta) {
Avri Altman22f66892015-08-18 16:52:07 +03001349 mutex_unlock(&local->sta_mtx);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001350 ret = -ENOLINK;
1351 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001352 }
1353
Avri Altman22f66892015-08-18 16:52:07 +03001354 iee80211_tdls_recalc_ht_protection(sdata, sta);
1355
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001356 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
Avri Altman22f66892015-08-18 16:52:07 +03001357 mutex_unlock(&local->sta_mtx);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001358
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001359 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1360 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001361 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001362 break;
1363 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +03001364 /*
1365 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1366 * created while the queues were stopped, so it might still be
1367 * pending. Before flushing the queues we need to be sure the
1368 * message is handled by the tasklet handling pending messages,
1369 * otherwise we might start destroying the station before
1370 * sending the teardown packet.
1371 * Note that this only forces the tasklet to flush pendings -
1372 * not to stop the tasklet from rescheduling itself.
1373 */
1374 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001375 /* flush a potentially queued teardown packet */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001376 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001377
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001378 ret = sta_info_destroy_addr(sdata, peer);
Avri Altman22f66892015-08-18 16:52:07 +03001379
1380 mutex_lock(&local->sta_mtx);
1381 iee80211_tdls_recalc_ht_protection(sdata, NULL);
1382 mutex_unlock(&local->sta_mtx);
1383
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +03001384 iee80211_tdls_recalc_chanctx(sdata);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001385 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001386 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001387 ret = -ENOTSUPP;
1388 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001389 }
1390
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001391 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1392 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1393 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001394 }
1395
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001396 if (ret == 0)
1397 ieee80211_queue_work(&sdata->local->hw,
1398 &sdata->u.mgd.request_smps_work);
1399
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001400 mutex_unlock(&local->mtx);
Avri Altman22f66892015-08-18 16:52:07 +03001401 sdata_unlock(sdata);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001402 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001403}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +03001404
1405void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1406 enum nl80211_tdls_operation oper,
1407 u16 reason_code, gfp_t gfp)
1408{
1409 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1410
1411 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1412 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1413 oper);
1414 return;
1415 }
1416
1417 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1418}
1419EXPORT_SYMBOL(ieee80211_tdls_oper_request);
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +02001420
1421static void
1422iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1423{
1424 struct ieee80211_ch_switch_timing *ch_sw;
1425
1426 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1427 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1428
1429 ch_sw = (void *)buf;
1430 ch_sw->switch_time = cpu_to_le16(switch_time);
1431 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1432}
1433
1434/* find switch timing IE in SKB ready for Tx */
1435static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1436{
1437 struct ieee80211_tdls_data *tf;
1438 const u8 *ie_start;
1439
1440 /*
1441 * Get the offset for the new location of the switch timing IE.
1442 * The SKB network header will now point to the "payload_type"
1443 * element of the TDLS data frame struct.
1444 */
1445 tf = container_of(skb->data + skb_network_offset(skb),
1446 struct ieee80211_tdls_data, payload_type);
1447 ie_start = tf->u.chan_switch_req.variable;
1448 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1449 skb->len - (ie_start - skb->data));
1450}
1451
1452static struct sk_buff *
1453ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1454 struct cfg80211_chan_def *chandef,
1455 u32 *ch_sw_tm_ie_offset)
1456{
1457 struct ieee80211_sub_if_data *sdata = sta->sdata;
1458 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1459 2 + sizeof(struct ieee80211_ch_switch_timing)];
1460 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1461 u8 *pos = extra_ies;
1462 struct sk_buff *skb;
1463
1464 /*
1465 * if chandef points to a wide channel add a Secondary-Channel
1466 * Offset information element
1467 */
1468 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1469 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1470 bool ht40plus;
1471
1472 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1473 *pos++ = sizeof(*sec_chan_ie);
1474 sec_chan_ie = (void *)pos;
1475
1476 ht40plus = cfg80211_get_chandef_type(chandef) ==
1477 NL80211_CHAN_HT40PLUS;
1478 sec_chan_ie->sec_chan_offs = ht40plus ?
1479 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1480 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1481 pos += sizeof(*sec_chan_ie);
1482
1483 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1484 }
1485
1486 /* just set the values to 0, this is a template */
1487 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1488
1489 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1490 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1491 0, 0, !sta->sta.tdls_initiator,
1492 extra_ies, extra_ies_len,
1493 oper_class, chandef);
1494 if (!skb)
1495 return NULL;
1496
1497 skb = ieee80211_build_data_template(sdata, skb, 0);
1498 if (IS_ERR(skb)) {
1499 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1500 return NULL;
1501 }
1502
1503 if (ch_sw_tm_ie_offset) {
1504 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1505
1506 if (!tm_ie) {
1507 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1508 dev_kfree_skb_any(skb);
1509 return NULL;
1510 }
1511
1512 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1513 }
1514
1515 tdls_dbg(sdata,
1516 "TDLS channel switch request template for %pM ch %d width %d\n",
1517 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1518 return skb;
1519}
1520
1521int
1522ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1523 const u8 *addr, u8 oper_class,
1524 struct cfg80211_chan_def *chandef)
1525{
1526 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1527 struct ieee80211_local *local = sdata->local;
1528 struct sta_info *sta;
1529 struct sk_buff *skb = NULL;
1530 u32 ch_sw_tm_ie;
1531 int ret;
1532
1533 mutex_lock(&local->sta_mtx);
1534 sta = sta_info_get(sdata, addr);
1535 if (!sta) {
1536 tdls_dbg(sdata,
1537 "Invalid TDLS peer %pM for channel switch request\n",
1538 addr);
1539 ret = -ENOENT;
1540 goto out;
1541 }
1542
1543 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1544 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1545 addr);
1546 ret = -ENOTSUPP;
1547 goto out;
1548 }
1549
1550 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1551 &ch_sw_tm_ie);
1552 if (!skb) {
1553 ret = -ENOENT;
1554 goto out;
1555 }
1556
1557 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1558 chandef, skb, ch_sw_tm_ie);
1559 if (!ret)
1560 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1561
1562out:
1563 mutex_unlock(&local->sta_mtx);
1564 dev_kfree_skb_any(skb);
1565 return ret;
1566}
1567
1568void
1569ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1570 struct net_device *dev,
1571 const u8 *addr)
1572{
1573 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1574 struct ieee80211_local *local = sdata->local;
1575 struct sta_info *sta;
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 cancel\n",
1582 addr);
1583 goto out;
1584 }
1585
1586 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1587 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1588 addr);
1589 goto out;
1590 }
1591
1592 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1593 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1594
1595out:
1596 mutex_unlock(&local->sta_mtx);
1597}
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001598
1599static struct sk_buff *
1600ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1601 u32 *ch_sw_tm_ie_offset)
1602{
1603 struct ieee80211_sub_if_data *sdata = sta->sdata;
1604 struct sk_buff *skb;
1605 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1606
1607 /* initial timing are always zero in the template */
1608 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1609
1610 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1611 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1612 0, 0, !sta->sta.tdls_initiator,
1613 extra_ies, sizeof(extra_ies), 0, NULL);
1614 if (!skb)
1615 return NULL;
1616
1617 skb = ieee80211_build_data_template(sdata, skb, 0);
1618 if (IS_ERR(skb)) {
1619 tdls_dbg(sdata,
1620 "Failed building TDLS channel switch resp frame\n");
1621 return NULL;
1622 }
1623
1624 if (ch_sw_tm_ie_offset) {
1625 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1626
1627 if (!tm_ie) {
1628 tdls_dbg(sdata,
1629 "No switch timing IE in TDLS switch resp\n");
1630 dev_kfree_skb_any(skb);
1631 return NULL;
1632 }
1633
1634 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1635 }
1636
1637 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1638 sta->sta.addr);
1639 return skb;
1640}
1641
1642static int
1643ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1644 struct sk_buff *skb)
1645{
1646 struct ieee80211_local *local = sdata->local;
1647 struct ieee802_11_elems elems;
1648 struct sta_info *sta;
1649 struct ieee80211_tdls_data *tf = (void *)skb->data;
1650 bool local_initiator;
1651 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1652 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1653 struct ieee80211_tdls_ch_sw_params params = {};
1654 int ret;
1655
1656 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1657 params.timestamp = rx_status->device_timestamp;
1658
1659 if (skb->len < baselen) {
1660 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1661 skb->len);
1662 return -EINVAL;
1663 }
1664
1665 mutex_lock(&local->sta_mtx);
1666 sta = sta_info_get(sdata, tf->sa);
1667 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1668 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1669 tf->sa);
1670 ret = -EINVAL;
1671 goto out;
1672 }
1673
1674 params.sta = &sta->sta;
1675 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1676 if (params.status != 0) {
1677 ret = 0;
1678 goto call_drv;
1679 }
1680
1681 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1682 skb->len - baselen, false, &elems);
1683 if (elems.parse_error) {
1684 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1685 ret = -EINVAL;
1686 goto out;
1687 }
1688
1689 if (!elems.ch_sw_timing || !elems.lnk_id) {
1690 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1691 ret = -EINVAL;
1692 goto out;
1693 }
1694
1695 /* validate the initiator is set correctly */
1696 local_initiator =
1697 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1698 if (local_initiator == sta->sta.tdls_initiator) {
1699 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1700 ret = -EINVAL;
1701 goto out;
1702 }
1703
1704 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1705 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1706
1707 params.tmpl_skb =
1708 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1709 if (!params.tmpl_skb) {
1710 ret = -ENOENT;
1711 goto out;
1712 }
1713
1714call_drv:
1715 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1716
1717 tdls_dbg(sdata,
1718 "TDLS channel switch response received from %pM status %d\n",
1719 tf->sa, params.status);
1720
1721out:
1722 mutex_unlock(&local->sta_mtx);
1723 dev_kfree_skb_any(params.tmpl_skb);
1724 return ret;
1725}
1726
1727static int
1728ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1729 struct sk_buff *skb)
1730{
1731 struct ieee80211_local *local = sdata->local;
1732 struct ieee802_11_elems elems;
1733 struct cfg80211_chan_def chandef;
1734 struct ieee80211_channel *chan;
1735 enum nl80211_channel_type chan_type;
1736 int freq;
1737 u8 target_channel, oper_class;
1738 bool local_initiator;
1739 struct sta_info *sta;
1740 enum ieee80211_band band;
1741 struct ieee80211_tdls_data *tf = (void *)skb->data;
1742 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1743 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1744 struct ieee80211_tdls_ch_sw_params params = {};
1745 int ret = 0;
1746
1747 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1748 params.timestamp = rx_status->device_timestamp;
1749
1750 if (skb->len < baselen) {
1751 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1752 skb->len);
1753 return -EINVAL;
1754 }
1755
1756 target_channel = tf->u.chan_switch_req.target_channel;
1757 oper_class = tf->u.chan_switch_req.oper_class;
1758
1759 /*
1760 * We can't easily infer the channel band. The operating class is
1761 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1762 * solution here is to treat channels with number >14 as 5GHz ones,
1763 * and specifically check for the (oper_class, channel) combinations
1764 * where this doesn't hold. These are thankfully unique according to
1765 * IEEE802.11-2012.
1766 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1767 * valid here.
1768 */
1769 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1770 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1771 target_channel < 14)
1772 band = IEEE80211_BAND_5GHZ;
1773 else
1774 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1775 IEEE80211_BAND_5GHZ;
1776
1777 freq = ieee80211_channel_to_frequency(target_channel, band);
1778 if (freq == 0) {
1779 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1780 target_channel);
1781 return -EINVAL;
1782 }
1783
1784 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1785 if (!chan) {
1786 tdls_dbg(sdata,
1787 "Unsupported channel for TDLS chan switch: %d\n",
1788 target_channel);
1789 return -EINVAL;
1790 }
1791
1792 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1793 skb->len - baselen, false, &elems);
1794 if (elems.parse_error) {
1795 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1796 return -EINVAL;
1797 }
1798
1799 if (!elems.ch_sw_timing || !elems.lnk_id) {
1800 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1801 return -EINVAL;
1802 }
1803
Arik Nemtsov42d8d782015-07-08 15:41:46 +03001804 if (!elems.sec_chan_offs) {
1805 chan_type = NL80211_CHAN_HT20;
1806 } else {
1807 switch (elems.sec_chan_offs->sec_chan_offs) {
1808 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1809 chan_type = NL80211_CHAN_HT40PLUS;
1810 break;
1811 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1812 chan_type = NL80211_CHAN_HT40MINUS;
1813 break;
1814 default:
1815 chan_type = NL80211_CHAN_HT20;
1816 break;
1817 }
1818 }
1819
1820 cfg80211_chandef_create(&chandef, chan, chan_type);
1821
1822 /* we will be active on the TDLS link */
1823 if (!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &chandef,
1824 sdata->wdev.iftype)) {
1825 tdls_dbg(sdata, "TDLS chan switch to forbidden channel\n");
1826 return -EINVAL;
1827 }
1828
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001829 mutex_lock(&local->sta_mtx);
1830 sta = sta_info_get(sdata, tf->sa);
1831 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1832 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1833 tf->sa);
1834 ret = -EINVAL;
1835 goto out;
1836 }
1837
1838 params.sta = &sta->sta;
1839
1840 /* validate the initiator is set correctly */
1841 local_initiator =
1842 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1843 if (local_initiator == sta->sta.tdls_initiator) {
1844 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1845 ret = -EINVAL;
1846 goto out;
1847 }
1848
Arik Nemtsov42d8d782015-07-08 15:41:46 +03001849 /* peer should have known better */
1850 if (!sta->sta.ht_cap.ht_supported && elems.sec_chan_offs &&
1851 elems.sec_chan_offs->sec_chan_offs) {
1852 tdls_dbg(sdata, "TDLS chan switch - wide chan unsupported\n");
1853 ret = -ENOTSUPP;
1854 goto out;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001855 }
1856
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001857 params.chandef = &chandef;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001858 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1859 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1860
1861 params.tmpl_skb =
1862 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1863 &params.ch_sw_tm_ie);
1864 if (!params.tmpl_skb) {
1865 ret = -ENOENT;
1866 goto out;
1867 }
1868
1869 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1870
1871 tdls_dbg(sdata,
1872 "TDLS ch switch request received from %pM ch %d width %d\n",
1873 tf->sa, params.chandef->chan->center_freq,
1874 params.chandef->width);
1875out:
1876 mutex_unlock(&local->sta_mtx);
1877 dev_kfree_skb_any(params.tmpl_skb);
1878 return ret;
1879}
1880
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03001881static void
1882ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1883 struct sk_buff *skb)
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001884{
1885 struct ieee80211_tdls_data *tf = (void *)skb->data;
1886 struct wiphy *wiphy = sdata->local->hw.wiphy;
1887
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03001888 ASSERT_RTNL();
1889
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001890 /* make sure the driver supports it */
1891 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1892 return;
1893
1894 /* we want to access the entire packet */
1895 if (skb_linearize(skb))
1896 return;
1897 /*
1898 * The packet/size was already validated by mac80211 Rx path, only look
1899 * at the action type.
1900 */
1901 switch (tf->action_code) {
1902 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1903 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1904 break;
1905 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1906 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1907 break;
1908 default:
1909 WARN_ON_ONCE(1);
1910 return;
1911 }
1912}
Arik Nemtsovd51c2ea2015-06-14 16:53:46 +03001913
1914void ieee80211_teardown_tdls_peers(struct ieee80211_sub_if_data *sdata)
1915{
1916 struct sta_info *sta;
1917 u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
1918
1919 rcu_read_lock();
1920 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
1921 if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
1922 !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1923 continue;
1924
1925 ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
1926 NL80211_TDLS_TEARDOWN, reason,
1927 GFP_ATOMIC);
1928 }
1929 rcu_read_unlock();
1930}
Arik Nemtsovc8ff71e2015-07-08 15:41:45 +03001931
1932void ieee80211_tdls_chsw_work(struct work_struct *wk)
1933{
1934 struct ieee80211_local *local =
1935 container_of(wk, struct ieee80211_local, tdls_chsw_work);
1936 struct ieee80211_sub_if_data *sdata;
1937 struct sk_buff *skb;
1938 struct ieee80211_tdls_data *tf;
1939
1940 rtnl_lock();
1941 while ((skb = skb_dequeue(&local->skb_queue_tdls_chsw))) {
1942 tf = (struct ieee80211_tdls_data *)skb->data;
1943 list_for_each_entry(sdata, &local->interfaces, list) {
1944 if (!ieee80211_sdata_running(sdata) ||
1945 sdata->vif.type != NL80211_IFTYPE_STATION ||
1946 !ether_addr_equal(tf->da, sdata->vif.addr))
1947 continue;
1948
1949 ieee80211_process_tdls_channel_switch(sdata, skb);
1950 break;
1951 }
1952
1953 kfree_skb(skb);
1954 }
1955 rtnl_unlock();
1956}