blob: bc7e4049896f5244a40b77b9159f5f9e11454cdc [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 Nemtsov95224fe2014-05-01 10:17:28 +03007 *
8 * This file is GPLv2 as found in COPYING.
9 */
10
11#include <linux/ieee80211.h>
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +030012#include <linux/log2.h>
Arik Nemtsovc887f0d32014-06-11 17:18:25 +030013#include <net/cfg80211.h>
Arik Nemtsov95224fe2014-05-01 10:17:28 +030014#include "ieee80211_i.h"
Arik Nemtsovee10f2c2014-06-11 17:18:27 +030015#include "driver-ops.h"
Arik Nemtsov95224fe2014-05-01 10:17:28 +030016
Arik Nemtsov17e6a592014-06-11 17:18:20 +030017/* give usermode some time for retries in setting up the TDLS session */
18#define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
19
20void ieee80211_tdls_peer_del_work(struct work_struct *wk)
21{
22 struct ieee80211_sub_if_data *sdata;
23 struct ieee80211_local *local;
24
25 sdata = container_of(wk, struct ieee80211_sub_if_data,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030026 u.mgd.tdls_peer_del_work.work);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030027 local = sdata->local;
28
29 mutex_lock(&local->mtx);
Arik Nemtsov81dd2b82014-07-17 17:14:25 +030030 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
31 tdls_dbg(sdata, "TDLS del peer %pM\n", sdata->u.mgd.tdls_peer);
32 sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
33 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +030034 }
35 mutex_unlock(&local->mtx);
36}
37
Arik Nemtsov78632a12014-11-09 18:50:14 +020038static void ieee80211_tdls_add_ext_capab(struct ieee80211_local *local,
39 struct sk_buff *skb)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030040{
41 u8 *pos = (void *)skb_put(skb, 7);
Arik Nemtsov78632a12014-11-09 18:50:14 +020042 bool chan_switch = local->hw.wiphy->features &
43 NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030044
45 *pos++ = WLAN_EID_EXT_CAPABILITY;
46 *pos++ = 5; /* len */
47 *pos++ = 0x0;
48 *pos++ = 0x0;
49 *pos++ = 0x0;
Arik Nemtsov78632a12014-11-09 18:50:14 +020050 *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +030051 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
52}
53
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020054static u8
55ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
56 struct sk_buff *skb, u16 start, u16 end,
57 u16 spacing)
58{
59 u8 subband_cnt = 0, ch_cnt = 0;
60 struct ieee80211_channel *ch;
61 struct cfg80211_chan_def chandef;
62 int i, subband_start;
63
64 for (i = start; i <= end; i += spacing) {
65 if (!ch_cnt)
66 subband_start = i;
67
68 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
69 if (ch) {
70 /* we will be active on the channel */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020071 cfg80211_chandef_create(&chandef, ch,
Arik Nemtsov50075892015-01-07 16:45:07 +020072 NL80211_CHAN_NO_HT);
73 if (cfg80211_reg_can_beacon(sdata->local->hw.wiphy,
74 &chandef,
75 sdata->wdev.iftype)) {
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020076 ch_cnt++;
Arik Nemtsov50075892015-01-07 16:45:07 +020077 /*
78 * check if the next channel is also part of
79 * this allowed range
80 */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020081 continue;
82 }
83 }
84
Arik Nemtsov50075892015-01-07 16:45:07 +020085 /*
86 * we've reached the end of a range, with allowed channels
87 * found
88 */
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +020089 if (ch_cnt) {
90 u8 *pos = skb_put(skb, 2);
91 *pos++ = ieee80211_frequency_to_channel(subband_start);
92 *pos++ = ch_cnt;
93
94 subband_cnt++;
95 ch_cnt = 0;
96 }
97 }
98
Arik Nemtsov50075892015-01-07 16:45:07 +020099 /* all channels in the requested range are allowed - add them here */
100 if (ch_cnt) {
101 u8 *pos = skb_put(skb, 2);
102 *pos++ = ieee80211_frequency_to_channel(subband_start);
103 *pos++ = ch_cnt;
104
105 subband_cnt++;
106 }
107
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200108 return subband_cnt;
109}
110
111static void
112ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
113 struct sk_buff *skb)
114{
115 /*
116 * Add possible channels for TDLS. These are channels that are allowed
117 * to be active.
118 */
119 u8 subband_cnt;
120 u8 *pos = skb_put(skb, 2);
121
122 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
123
124 /*
125 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
126 * this doesn't happen in real world scenarios.
127 */
128
129 /* 2GHz, with 5MHz spacing */
130 subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
131
132 /* 5GHz, with 20MHz spacing */
133 subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
134
135 /* length */
136 *pos = 2 * subband_cnt;
137}
138
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200139static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
140{
141 u8 *pos = (void *)skb_put(skb, 3);
142
143 *pos++ = WLAN_EID_BSS_COEX_2040;
144 *pos++ = 1; /* len */
145
146 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
147}
148
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300149static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
150 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300151{
152 struct ieee80211_local *local = sdata->local;
153 u16 capab;
154
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300155 /* The capability will be 0 when sending a failure code */
156 if (status_code != 0)
157 return 0;
158
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300159 capab = 0;
160 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
161 return capab;
162
163 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
164 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
165 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
166 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
167
168 return capab;
169}
170
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300171static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
172 struct sk_buff *skb, const u8 *peer,
173 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300174{
175 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300176 const u8 *init_addr, *rsp_addr;
177
178 if (initiator) {
179 init_addr = sdata->vif.addr;
180 rsp_addr = peer;
181 } else {
182 init_addr = peer;
183 rsp_addr = sdata->vif.addr;
184 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300185
186 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
187
188 lnkid->ie_type = WLAN_EID_LINK_ID;
189 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
190
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300191 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
192 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
193 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300194}
195
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200196static void
197ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
198{
199 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
200 u8 *pos = (void *)skb_put(skb, 4);
201
202 *pos++ = WLAN_EID_AID;
203 *pos++ = 2; /* len */
204 put_unaligned_le16(ifmgd->aid, pos);
205}
206
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300207/* translate numbering in the WMM parameter IE to the mac80211 notation */
208static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
209{
210 switch (ac) {
211 default:
212 WARN_ON_ONCE(1);
213 case 0:
214 return IEEE80211_AC_BE;
215 case 1:
216 return IEEE80211_AC_BK;
217 case 2:
218 return IEEE80211_AC_VI;
219 case 3:
220 return IEEE80211_AC_VO;
221 }
222}
223
224static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
225{
226 u8 ret;
227
228 ret = aifsn & 0x0f;
229 if (acm)
230 ret |= 0x10;
231 ret |= (aci << 5) & 0x60;
232 return ret;
233}
234
235static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
236{
237 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
238 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
239}
240
241static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
242 struct sk_buff *skb)
243{
244 struct ieee80211_wmm_param_ie *wmm;
245 struct ieee80211_tx_queue_params *txq;
246 int i;
247
248 wmm = (void *)skb_put(skb, sizeof(*wmm));
249 memset(wmm, 0, sizeof(*wmm));
250
251 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
252 wmm->len = sizeof(*wmm) - 2;
253
254 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
255 wmm->oui[1] = 0x50;
256 wmm->oui[2] = 0xf2;
257 wmm->oui_type = 2; /* WME */
258 wmm->oui_subtype = 1; /* WME param */
259 wmm->version = 1; /* WME ver */
260 wmm->qos_info = 0; /* U-APSD not in use */
261
262 /*
263 * Use the EDCA parameters defined for the BSS, or default if the AP
264 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
265 */
266 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
267 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
268 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
269 txq->acm, i);
270 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
271 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
272 }
273}
274
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300275static void
276ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
277 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300278 u8 action_code, bool initiator,
279 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300280{
281 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300282 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300283 struct ieee80211_supported_band *sband;
284 struct ieee80211_sta_ht_cap ht_cap;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200285 struct ieee80211_sta_vht_cap vht_cap;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300286 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300287 size_t offset = 0, noffset;
288 u8 *pos;
289
290 ieee80211_add_srates_ie(sdata, skb, false, band);
291 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200292 ieee80211_tdls_add_supp_channels(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300293
294 /* add any custom IEs that go before Extended Capabilities */
295 if (extra_ies_len) {
296 static const u8 before_ext_cap[] = {
297 WLAN_EID_SUPP_RATES,
298 WLAN_EID_COUNTRY,
299 WLAN_EID_EXT_SUPP_RATES,
300 WLAN_EID_SUPPORTED_CHANNELS,
301 WLAN_EID_RSN,
302 };
303 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
304 before_ext_cap,
305 ARRAY_SIZE(before_ext_cap),
306 offset);
307 pos = skb_put(skb, noffset - offset);
308 memcpy(pos, extra_ies + offset, noffset - offset);
309 offset = noffset;
310 }
311
Arik Nemtsov78632a12014-11-09 18:50:14 +0200312 ieee80211_tdls_add_ext_capab(local, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300313
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300314 /* add the QoS element if we support it */
315 if (local->hw.queues >= IEEE80211_NUM_ACS &&
316 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
317 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
318
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300319 /* add any custom IEs that go before HT capabilities */
320 if (extra_ies_len) {
321 static const u8 before_ht_cap[] = {
322 WLAN_EID_SUPP_RATES,
323 WLAN_EID_COUNTRY,
324 WLAN_EID_EXT_SUPP_RATES,
325 WLAN_EID_SUPPORTED_CHANNELS,
326 WLAN_EID_RSN,
327 WLAN_EID_EXT_CAPABILITY,
328 WLAN_EID_QOS_CAPA,
329 WLAN_EID_FAST_BSS_TRANSITION,
330 WLAN_EID_TIMEOUT_INTERVAL,
331 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
332 };
333 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
334 before_ht_cap,
335 ARRAY_SIZE(before_ht_cap),
336 offset);
337 pos = skb_put(skb, noffset - offset);
338 memcpy(pos, extra_ies + offset, noffset - offset);
339 offset = noffset;
340 }
341
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200342 rcu_read_lock();
343
344 /* we should have the peer STA if we're already responding */
345 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
346 sta = sta_info_get(sdata, peer);
347 if (WARN_ON_ONCE(!sta)) {
348 rcu_read_unlock();
349 return;
350 }
351 }
352
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300353 /*
354 * with TDLS we can switch channels, and HT-caps are not necessarily
355 * the same on all bands. The specification limits the setup to a
356 * single HT-cap, so use the current band for now.
357 */
358 sband = local->hw.wiphy->bands[band];
359 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300360
Johannes Bergc5309ba2015-01-23 11:42:14 +0100361 if (action_code == WLAN_TDLS_SETUP_REQUEST && ht_cap.ht_supported) {
362 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300363
Johannes Bergc5309ba2015-01-23 11:42:14 +0100364 /* disable SMPS in TDLS initiator */
365 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
366 << IEEE80211_HT_CAP_SM_PS_SHIFT;
367
368 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
369 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
370 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
371 ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
372 /* disable SMPS in TDLS responder */
373 sta->sta.ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
374 << IEEE80211_HT_CAP_SM_PS_SHIFT;
375
376 /* the peer caps are already intersected with our own */
377 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300378
379 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
380 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
381 }
382
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200383 if (ht_cap.ht_supported &&
384 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
385 ieee80211_tdls_add_bss_coex_ie(skb);
386
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200387 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
388
389 /* add any custom IEs that go before VHT capabilities */
390 if (extra_ies_len) {
391 static const u8 before_vht_cap[] = {
392 WLAN_EID_SUPP_RATES,
393 WLAN_EID_COUNTRY,
394 WLAN_EID_EXT_SUPP_RATES,
395 WLAN_EID_SUPPORTED_CHANNELS,
396 WLAN_EID_RSN,
397 WLAN_EID_EXT_CAPABILITY,
398 WLAN_EID_QOS_CAPA,
399 WLAN_EID_FAST_BSS_TRANSITION,
400 WLAN_EID_TIMEOUT_INTERVAL,
401 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
402 WLAN_EID_MULTI_BAND,
403 };
404 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
405 before_vht_cap,
406 ARRAY_SIZE(before_vht_cap),
407 offset);
408 pos = skb_put(skb, noffset - offset);
409 memcpy(pos, extra_ies + offset, noffset - offset);
410 offset = noffset;
411 }
412
413 /* build the VHT-cap similarly to the HT-cap */
414 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
415 if (action_code == WLAN_TDLS_SETUP_REQUEST && vht_cap.vht_supported) {
416 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
417
418 /* the AID is present only when VHT is implemented */
419 ieee80211_tdls_add_aid(sdata, skb);
420
421 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
422 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
423 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
424 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
425 /* the peer caps are already intersected with our own */
426 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
427
428 /* the AID is present only when VHT is implemented */
429 ieee80211_tdls_add_aid(sdata, skb);
430
431 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
432 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
433 }
434
435 rcu_read_unlock();
436
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300437 /* add any remaining IEs */
438 if (extra_ies_len) {
439 noffset = extra_ies_len;
440 pos = skb_put(skb, noffset - offset);
441 memcpy(pos, extra_ies + offset, noffset - offset);
442 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300443
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300444}
445
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300446static void
447ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
448 struct sk_buff *skb, const u8 *peer,
449 bool initiator, const u8 *extra_ies,
450 size_t extra_ies_len)
451{
452 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300453 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300454 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300455 struct sta_info *sta, *ap_sta;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200456 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300457 u8 *pos;
458
459 rcu_read_lock();
460
461 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300462 ap_sta = sta_info_get(sdata, ifmgd->bssid);
463 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300464 rcu_read_unlock();
465 return;
466 }
467
468 /* add any custom IEs that go before the QoS IE */
469 if (extra_ies_len) {
470 static const u8 before_qos[] = {
471 WLAN_EID_RSN,
472 };
473 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
474 before_qos,
475 ARRAY_SIZE(before_qos),
476 offset);
477 pos = skb_put(skb, noffset - offset);
478 memcpy(pos, extra_ies + offset, noffset - offset);
479 offset = noffset;
480 }
481
482 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200483 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300484 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
485
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300486 /* add any custom IEs that go before HT operation */
487 if (extra_ies_len) {
488 static const u8 before_ht_op[] = {
489 WLAN_EID_RSN,
490 WLAN_EID_QOS_CAPA,
491 WLAN_EID_FAST_BSS_TRANSITION,
492 WLAN_EID_TIMEOUT_INTERVAL,
493 };
494 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
495 before_ht_op,
496 ARRAY_SIZE(before_ht_op),
497 offset);
498 pos = skb_put(skb, noffset - offset);
499 memcpy(pos, extra_ies + offset, noffset - offset);
500 offset = noffset;
501 }
502
503 /* if HT support is only added in TDLS, we need an HT-operation IE */
504 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
505 struct ieee80211_chanctx_conf *chanctx_conf =
506 rcu_dereference(sdata->vif.chanctx_conf);
507 if (!WARN_ON(!chanctx_conf)) {
508 pos = skb_put(skb, 2 +
509 sizeof(struct ieee80211_ht_operation));
510 /* send an empty HT operation IE */
511 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
512 &chanctx_conf->def, 0);
513 }
514 }
515
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200516 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
517
518 /* only include VHT-operation if not on the 2.4GHz band */
519 if (band != IEEE80211_BAND_2GHZ && !ap_sta->sta.vht_cap.vht_supported &&
520 sta->sta.vht_cap.vht_supported) {
521 struct ieee80211_chanctx_conf *chanctx_conf =
522 rcu_dereference(sdata->vif.chanctx_conf);
523 if (!WARN_ON(!chanctx_conf)) {
524 pos = skb_put(skb, 2 +
525 sizeof(struct ieee80211_vht_operation));
526 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
527 &chanctx_conf->def);
528 }
529 }
530
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300531 rcu_read_unlock();
532
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300533 /* add any remaining IEs */
534 if (extra_ies_len) {
535 noffset = extra_ies_len;
536 pos = skb_put(skb, noffset - offset);
537 memcpy(pos, extra_ies + offset, noffset - offset);
538 }
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300539}
540
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200541static void
542ieee80211_tdls_add_chan_switch_req_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, u8 oper_class,
546 struct cfg80211_chan_def *chandef)
547{
548 struct ieee80211_tdls_data *tf;
549 size_t offset = 0, noffset;
550 u8 *pos;
551
552 if (WARN_ON_ONCE(!chandef))
553 return;
554
555 tf = (void *)skb->data;
556 tf->u.chan_switch_req.target_channel =
557 ieee80211_frequency_to_channel(chandef->chan->center_freq);
558 tf->u.chan_switch_req.oper_class = oper_class;
559
560 if (extra_ies_len) {
561 static const u8 before_lnkie[] = {
562 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
563 };
564 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
565 before_lnkie,
566 ARRAY_SIZE(before_lnkie),
567 offset);
568 pos = skb_put(skb, noffset - offset);
569 memcpy(pos, extra_ies + offset, noffset - offset);
570 offset = noffset;
571 }
572
573 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
574
575 /* add any remaining IEs */
576 if (extra_ies_len) {
577 noffset = extra_ies_len;
578 pos = skb_put(skb, noffset - offset);
579 memcpy(pos, extra_ies + offset, noffset - offset);
580 }
581}
582
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200583static void
584ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
585 struct sk_buff *skb, const u8 *peer,
586 u16 status_code, bool initiator,
587 const u8 *extra_ies,
588 size_t extra_ies_len)
589{
590 if (status_code == 0)
591 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
592
593 if (extra_ies_len)
594 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
595}
596
Arik Nemtsov46792a22014-07-17 17:14:19 +0300597static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
598 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300599 u8 action_code, u16 status_code,
600 bool initiator, const u8 *extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200601 size_t extra_ies_len, u8 oper_class,
602 struct cfg80211_chan_def *chandef)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300603{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300604 switch (action_code) {
605 case WLAN_TDLS_SETUP_REQUEST:
606 case WLAN_TDLS_SETUP_RESPONSE:
607 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300608 if (status_code == 0)
609 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
610 action_code,
611 initiator,
612 extra_ies,
613 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300614 break;
615 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300616 if (status_code == 0)
617 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
618 initiator, extra_ies,
619 extra_ies_len);
620 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300621 case WLAN_TDLS_TEARDOWN:
622 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300623 if (extra_ies_len)
624 memcpy(skb_put(skb, extra_ies_len), extra_ies,
625 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300626 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
627 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300628 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200629 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
630 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
631 initiator, extra_ies,
632 extra_ies_len,
633 oper_class, chandef);
634 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200635 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
636 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
637 status_code,
638 initiator, extra_ies,
639 extra_ies_len);
640 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300641 }
642
Arik Nemtsov46792a22014-07-17 17:14:19 +0300643}
644
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300645static int
646ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200647 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300648 u16 status_code, struct sk_buff *skb)
649{
650 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300651 struct ieee80211_tdls_data *tf;
652
653 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
654
655 memcpy(tf->da, peer, ETH_ALEN);
656 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
657 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
658 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
659
Arik Nemtsov59cd85c2014-09-09 17:11:02 +0300660 /* network header is after the ethernet header */
661 skb_set_network_header(skb, ETH_HLEN);
662
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300663 switch (action_code) {
664 case WLAN_TDLS_SETUP_REQUEST:
665 tf->category = WLAN_CATEGORY_TDLS;
666 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
667
668 skb_put(skb, sizeof(tf->u.setup_req));
669 tf->u.setup_req.dialog_token = dialog_token;
670 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300671 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
672 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300673 break;
674 case WLAN_TDLS_SETUP_RESPONSE:
675 tf->category = WLAN_CATEGORY_TDLS;
676 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
677
678 skb_put(skb, sizeof(tf->u.setup_resp));
679 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
680 tf->u.setup_resp.dialog_token = dialog_token;
681 tf->u.setup_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300682 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
683 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300684 break;
685 case WLAN_TDLS_SETUP_CONFIRM:
686 tf->category = WLAN_CATEGORY_TDLS;
687 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
688
689 skb_put(skb, sizeof(tf->u.setup_cfm));
690 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
691 tf->u.setup_cfm.dialog_token = dialog_token;
692 break;
693 case WLAN_TDLS_TEARDOWN:
694 tf->category = WLAN_CATEGORY_TDLS;
695 tf->action_code = WLAN_TDLS_TEARDOWN;
696
697 skb_put(skb, sizeof(tf->u.teardown));
698 tf->u.teardown.reason_code = cpu_to_le16(status_code);
699 break;
700 case WLAN_TDLS_DISCOVERY_REQUEST:
701 tf->category = WLAN_CATEGORY_TDLS;
702 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
703
704 skb_put(skb, sizeof(tf->u.discover_req));
705 tf->u.discover_req.dialog_token = dialog_token;
706 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200707 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
708 tf->category = WLAN_CATEGORY_TDLS;
709 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
710
711 skb_put(skb, sizeof(tf->u.chan_switch_req));
712 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200713 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
714 tf->category = WLAN_CATEGORY_TDLS;
715 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
716
717 skb_put(skb, sizeof(tf->u.chan_switch_resp));
718 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
719 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300720 default:
721 return -EINVAL;
722 }
723
724 return 0;
725}
726
727static int
728ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200729 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300730 u16 status_code, struct sk_buff *skb)
731{
732 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300733 struct ieee80211_mgmt *mgmt;
734
735 mgmt = (void *)skb_put(skb, 24);
736 memset(mgmt, 0, 24);
737 memcpy(mgmt->da, peer, ETH_ALEN);
738 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
739 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
740
741 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
742 IEEE80211_STYPE_ACTION);
743
744 switch (action_code) {
745 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
746 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
747 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
748 mgmt->u.action.u.tdls_discover_resp.action_code =
749 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
750 mgmt->u.action.u.tdls_discover_resp.dialog_token =
751 dialog_token;
752 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300753 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
754 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300755 break;
756 default:
757 return -EINVAL;
758 }
759
760 return 0;
761}
762
Arik Nemtsovc2733902014-11-09 18:50:16 +0200763static struct sk_buff *
764ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
765 const u8 *peer, u8 action_code,
766 u8 dialog_token, u16 status_code,
767 bool initiator, const u8 *extra_ies,
768 size_t extra_ies_len, u8 oper_class,
769 struct cfg80211_chan_def *chandef)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300770{
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300771 struct ieee80211_local *local = sdata->local;
Arik Nemtsovc2733902014-11-09 18:50:16 +0200772 struct sk_buff *skb;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300773 int ret;
774
Arik Nemtsovc2733902014-11-09 18:50:16 +0200775 skb = netdev_alloc_skb(sdata->dev,
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200776 local->hw.extra_tx_headroom +
777 max(sizeof(struct ieee80211_mgmt),
778 sizeof(struct ieee80211_tdls_data)) +
779 50 + /* supported rates */
780 7 + /* ext capab */
781 26 + /* max(WMM-info, WMM-param) */
782 2 + max(sizeof(struct ieee80211_ht_cap),
783 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200784 2 + max(sizeof(struct ieee80211_vht_cap),
785 sizeof(struct ieee80211_vht_operation)) +
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200786 50 + /* supported channels */
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200787 3 + /* 40/20 BSS coex */
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200788 4 + /* AID */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200789 extra_ies_len +
790 sizeof(struct ieee80211_tdls_lnkie));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300791 if (!skb)
Arik Nemtsovc2733902014-11-09 18:50:16 +0200792 return NULL;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300793
794 skb_reserve(skb, local->hw.extra_tx_headroom);
795
796 switch (action_code) {
797 case WLAN_TDLS_SETUP_REQUEST:
798 case WLAN_TDLS_SETUP_RESPONSE:
799 case WLAN_TDLS_SETUP_CONFIRM:
800 case WLAN_TDLS_TEARDOWN:
801 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200802 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200803 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200804 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
805 sdata->dev, peer,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300806 action_code, dialog_token,
807 status_code, skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300808 break;
809 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200810 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
811 peer, action_code,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300812 dialog_token, status_code,
813 skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300814 break;
815 default:
816 ret = -ENOTSUPP;
817 break;
818 }
819
820 if (ret < 0)
821 goto fail;
822
Arik Nemtsovc2733902014-11-09 18:50:16 +0200823 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
824 initiator, extra_ies, extra_ies_len, oper_class,
825 chandef);
826 return skb;
827
828fail:
829 dev_kfree_skb(skb);
830 return NULL;
831}
832
833static int
834ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
835 const u8 *peer, u8 action_code, u8 dialog_token,
836 u16 status_code, u32 peer_capability,
837 bool initiator, const u8 *extra_ies,
838 size_t extra_ies_len, u8 oper_class,
839 struct cfg80211_chan_def *chandef)
840{
841 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
842 struct sk_buff *skb = NULL;
843 struct sta_info *sta;
844 u32 flags = 0;
845 int ret = 0;
846
Arik Nemtsov626911c2014-07-17 17:14:17 +0300847 rcu_read_lock();
848 sta = sta_info_get(sdata, peer);
849
850 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300851 switch (action_code) {
852 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov8b941482014-10-22 12:32:48 +0300853 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300854 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300855 sta->sta.tdls_initiator = false;
856 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300857 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300858 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300859 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300860 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300861 break;
862 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300863 /*
864 * In some testing scenarios, we send a request and response.
865 * Make the last packet sent take effect for the initiator
866 * value.
867 */
Arik Nemtsov8b941482014-10-22 12:32:48 +0300868 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300869 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300870 sta->sta.tdls_initiator = true;
871 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300872 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300873 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300874 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300875 break;
876 case WLAN_TDLS_TEARDOWN:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200877 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200878 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300879 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300880 break;
881 default:
882 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300883 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300884 }
885
Arik Nemtsov46792a22014-07-17 17:14:19 +0300886 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
887 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300888
Arik Nemtsov626911c2014-07-17 17:14:17 +0300889 rcu_read_unlock();
890 if (ret < 0)
891 goto fail;
892
Arik Nemtsovc2733902014-11-09 18:50:16 +0200893 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
894 dialog_token, status_code,
895 initiator, extra_ies,
896 extra_ies_len, oper_class,
897 chandef);
898 if (!skb) {
899 ret = -EINVAL;
900 goto fail;
901 }
902
903 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300904 ieee80211_tx_skb(sdata, skb);
905 return 0;
906 }
907
908 /*
909 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
910 * we should default to AC_VI.
911 */
912 switch (action_code) {
913 case WLAN_TDLS_SETUP_REQUEST:
914 case WLAN_TDLS_SETUP_RESPONSE:
915 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
916 skb->priority = 2;
917 break;
918 default:
919 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
920 skb->priority = 5;
921 break;
922 }
923
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200924 /*
925 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
926 * Later, if no ACK is returned from peer, we will re-send the teardown
927 * packet through the AP.
928 */
929 if ((action_code == WLAN_TDLS_TEARDOWN) &&
Arik Nemtsovc2733902014-11-09 18:50:16 +0200930 (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200931 bool try_resend; /* Should we keep skb for possible resend */
932
933 /* If not sending directly to peer - no point in keeping skb */
934 rcu_read_lock();
935 sta = sta_info_get(sdata, peer);
936 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
937 rcu_read_unlock();
938
939 spin_lock_bh(&sdata->u.mgd.teardown_lock);
940 if (try_resend && !sdata->u.mgd.teardown_skb) {
941 /* Mark it as requiring TX status callback */
942 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
943 IEEE80211_TX_INTFL_MLME_CONN_TX;
944
945 /*
946 * skb is copied since mac80211 will later set
947 * properties that might not be the same as the AP,
948 * such as encryption, QoS, addresses, etc.
949 *
950 * No problem if skb_copy() fails, so no need to check.
951 */
952 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
953 sdata->u.mgd.orig_teardown_skb = skb;
954 }
955 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
956 }
957
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300958 /* disable bottom halves when entering the Tx path */
959 local_bh_disable();
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200960 __ieee80211_subif_start_xmit(skb, dev, flags);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300961 local_bh_enable();
962
963 return ret;
964
965fail:
966 dev_kfree_skb(skb);
967 return ret;
968}
969
Arik Nemtsov191dd462014-06-11 17:18:23 +0300970static int
971ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
972 const u8 *peer, u8 action_code, u8 dialog_token,
973 u16 status_code, u32 peer_capability, bool initiator,
974 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300975{
976 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
977 struct ieee80211_local *local = sdata->local;
978 int ret;
979
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300980 mutex_lock(&local->mtx);
981
982 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300983 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
984 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300985 ret = -EBUSY;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200986 goto out_unlock;
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300987 }
988
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300989 /*
990 * make sure we have a STA representing the peer so we drop or buffer
991 * non-TDLS-setup frames to the peer. We can't send other packets
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300992 * during setup through the AP path.
993 * Allow error packets to be sent - sometimes we don't even add a STA
994 * before failing the setup.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300995 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300996 if (status_code == 0) {
997 rcu_read_lock();
998 if (!sta_info_get(sdata, peer)) {
999 rcu_read_unlock();
1000 ret = -ENOLINK;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001001 goto out_unlock;
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001002 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001003 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001004 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001005
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001006 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001007 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1008 mutex_unlock(&local->mtx);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001009
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001010 /* we cannot take the mutex while preparing the setup packet */
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001011 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1012 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +03001013 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001014 extra_ies, extra_ies_len, 0,
1015 NULL);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001016 if (ret < 0) {
1017 mutex_lock(&local->mtx);
1018 eth_zero_addr(sdata->u.mgd.tdls_peer);
1019 mutex_unlock(&local->mtx);
1020 return ret;
1021 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001022
Arik Nemtsov191dd462014-06-11 17:18:23 +03001023 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001024 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +03001025 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001026 return 0;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001027
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001028out_unlock:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001029 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001030 return ret;
1031}
1032
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001033static int
1034ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1035 const u8 *peer, u8 action_code, u8 dialog_token,
1036 u16 status_code, u32 peer_capability,
1037 bool initiator, const u8 *extra_ies,
1038 size_t extra_ies_len)
1039{
1040 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1041 struct ieee80211_local *local = sdata->local;
1042 struct sta_info *sta;
1043 int ret;
1044
1045 /*
1046 * No packets can be transmitted to the peer via the AP during setup -
1047 * the STA is set as a TDLS peer, but is not authorized.
1048 * During teardown, we prevent direct transmissions by stopping the
1049 * queues and flushing all direct packets.
1050 */
1051 ieee80211_stop_vif_queues(local, sdata,
1052 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001053 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001054
1055 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1056 dialog_token, status_code,
1057 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001058 extra_ies, extra_ies_len, 0,
1059 NULL);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001060 if (ret < 0)
1061 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1062 ret);
1063
1064 /*
1065 * Remove the STA AUTH flag to force further traffic through the AP. If
1066 * the STA was unreachable, it was already removed.
1067 */
1068 rcu_read_lock();
1069 sta = sta_info_get(sdata, peer);
1070 if (sta)
1071 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1072 rcu_read_unlock();
1073
1074 ieee80211_wake_vif_queues(local, sdata,
1075 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1076
1077 return 0;
1078}
1079
Arik Nemtsov191dd462014-06-11 17:18:23 +03001080int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1081 const u8 *peer, u8 action_code, u8 dialog_token,
1082 u16 status_code, u32 peer_capability,
1083 bool initiator, const u8 *extra_ies,
1084 size_t extra_ies_len)
1085{
1086 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1087 int ret;
1088
1089 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1090 return -ENOTSUPP;
1091
1092 /* make sure we are in managed mode, and associated */
1093 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1094 !sdata->u.mgd.associated)
1095 return -EINVAL;
1096
1097 switch (action_code) {
1098 case WLAN_TDLS_SETUP_REQUEST:
1099 case WLAN_TDLS_SETUP_RESPONSE:
1100 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1101 dialog_token, status_code,
1102 peer_capability, initiator,
1103 extra_ies, extra_ies_len);
1104 break;
1105 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001106 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1107 action_code, dialog_token,
1108 status_code,
1109 peer_capability, initiator,
1110 extra_ies, extra_ies_len);
1111 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +03001112 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +03001113 /*
1114 * Protect the discovery so we can hear the TDLS discovery
1115 * response frame. It is transmitted directly and not buffered
1116 * by the AP.
1117 */
1118 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1119 /* fall-through */
1120 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +03001121 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1122 /* no special handling */
1123 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1124 action_code,
1125 dialog_token,
1126 status_code,
1127 peer_capability,
1128 initiator, extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001129 extra_ies_len, 0, NULL);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001130 break;
1131 default:
1132 ret = -EOPNOTSUPP;
1133 break;
1134 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001135
1136 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1137 action_code, peer, ret);
1138 return ret;
1139}
1140
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001141int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +02001142 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001143{
1144 struct sta_info *sta;
1145 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001146 struct ieee80211_local *local = sdata->local;
1147 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001148
1149 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1150 return -ENOTSUPP;
1151
1152 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1153 return -EINVAL;
1154
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001155 switch (oper) {
1156 case NL80211_TDLS_ENABLE_LINK:
1157 case NL80211_TDLS_DISABLE_LINK:
1158 break;
1159 case NL80211_TDLS_TEARDOWN:
1160 case NL80211_TDLS_SETUP:
1161 case NL80211_TDLS_DISCOVERY_REQ:
1162 /* We don't support in-driver setup/teardown/discovery */
1163 return -ENOTSUPP;
1164 }
1165
1166 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001167 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1168
1169 switch (oper) {
1170 case NL80211_TDLS_ENABLE_LINK:
1171 rcu_read_lock();
1172 sta = sta_info_get(sdata, peer);
1173 if (!sta) {
1174 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001175 ret = -ENOLINK;
1176 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001177 }
1178
1179 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1180 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001181
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001182 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1183 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001184 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001185 break;
1186 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +03001187 /*
1188 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1189 * created while the queues were stopped, so it might still be
1190 * pending. Before flushing the queues we need to be sure the
1191 * message is handled by the tasklet handling pending messages,
1192 * otherwise we might start destroying the station before
1193 * sending the teardown packet.
1194 * Note that this only forces the tasklet to flush pendings -
1195 * not to stop the tasklet from rescheduling itself.
1196 */
1197 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001198 /* flush a potentially queued teardown packet */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001199 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001200
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001201 ret = sta_info_destroy_addr(sdata, peer);
1202 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001203 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001204 ret = -ENOTSUPP;
1205 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001206 }
1207
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001208 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1209 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1210 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001211 }
1212
1213 mutex_unlock(&local->mtx);
1214 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001215}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +03001216
1217void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1218 enum nl80211_tdls_operation oper,
1219 u16 reason_code, gfp_t gfp)
1220{
1221 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1222
1223 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1224 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1225 oper);
1226 return;
1227 }
1228
1229 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1230}
1231EXPORT_SYMBOL(ieee80211_tdls_oper_request);
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +02001232
1233static void
1234iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1235{
1236 struct ieee80211_ch_switch_timing *ch_sw;
1237
1238 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1239 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1240
1241 ch_sw = (void *)buf;
1242 ch_sw->switch_time = cpu_to_le16(switch_time);
1243 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1244}
1245
1246/* find switch timing IE in SKB ready for Tx */
1247static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1248{
1249 struct ieee80211_tdls_data *tf;
1250 const u8 *ie_start;
1251
1252 /*
1253 * Get the offset for the new location of the switch timing IE.
1254 * The SKB network header will now point to the "payload_type"
1255 * element of the TDLS data frame struct.
1256 */
1257 tf = container_of(skb->data + skb_network_offset(skb),
1258 struct ieee80211_tdls_data, payload_type);
1259 ie_start = tf->u.chan_switch_req.variable;
1260 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1261 skb->len - (ie_start - skb->data));
1262}
1263
1264static struct sk_buff *
1265ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1266 struct cfg80211_chan_def *chandef,
1267 u32 *ch_sw_tm_ie_offset)
1268{
1269 struct ieee80211_sub_if_data *sdata = sta->sdata;
1270 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1271 2 + sizeof(struct ieee80211_ch_switch_timing)];
1272 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1273 u8 *pos = extra_ies;
1274 struct sk_buff *skb;
1275
1276 /*
1277 * if chandef points to a wide channel add a Secondary-Channel
1278 * Offset information element
1279 */
1280 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1281 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1282 bool ht40plus;
1283
1284 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1285 *pos++ = sizeof(*sec_chan_ie);
1286 sec_chan_ie = (void *)pos;
1287
1288 ht40plus = cfg80211_get_chandef_type(chandef) ==
1289 NL80211_CHAN_HT40PLUS;
1290 sec_chan_ie->sec_chan_offs = ht40plus ?
1291 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1292 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1293 pos += sizeof(*sec_chan_ie);
1294
1295 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1296 }
1297
1298 /* just set the values to 0, this is a template */
1299 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1300
1301 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1302 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1303 0, 0, !sta->sta.tdls_initiator,
1304 extra_ies, extra_ies_len,
1305 oper_class, chandef);
1306 if (!skb)
1307 return NULL;
1308
1309 skb = ieee80211_build_data_template(sdata, skb, 0);
1310 if (IS_ERR(skb)) {
1311 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1312 return NULL;
1313 }
1314
1315 if (ch_sw_tm_ie_offset) {
1316 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1317
1318 if (!tm_ie) {
1319 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1320 dev_kfree_skb_any(skb);
1321 return NULL;
1322 }
1323
1324 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1325 }
1326
1327 tdls_dbg(sdata,
1328 "TDLS channel switch request template for %pM ch %d width %d\n",
1329 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1330 return skb;
1331}
1332
1333int
1334ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1335 const u8 *addr, u8 oper_class,
1336 struct cfg80211_chan_def *chandef)
1337{
1338 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1339 struct ieee80211_local *local = sdata->local;
1340 struct sta_info *sta;
1341 struct sk_buff *skb = NULL;
1342 u32 ch_sw_tm_ie;
1343 int ret;
1344
1345 mutex_lock(&local->sta_mtx);
1346 sta = sta_info_get(sdata, addr);
1347 if (!sta) {
1348 tdls_dbg(sdata,
1349 "Invalid TDLS peer %pM for channel switch request\n",
1350 addr);
1351 ret = -ENOENT;
1352 goto out;
1353 }
1354
1355 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1356 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1357 addr);
1358 ret = -ENOTSUPP;
1359 goto out;
1360 }
1361
1362 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1363 &ch_sw_tm_ie);
1364 if (!skb) {
1365 ret = -ENOENT;
1366 goto out;
1367 }
1368
1369 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1370 chandef, skb, ch_sw_tm_ie);
1371 if (!ret)
1372 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1373
1374out:
1375 mutex_unlock(&local->sta_mtx);
1376 dev_kfree_skb_any(skb);
1377 return ret;
1378}
1379
1380void
1381ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1382 struct net_device *dev,
1383 const u8 *addr)
1384{
1385 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1386 struct ieee80211_local *local = sdata->local;
1387 struct sta_info *sta;
1388
1389 mutex_lock(&local->sta_mtx);
1390 sta = sta_info_get(sdata, addr);
1391 if (!sta) {
1392 tdls_dbg(sdata,
1393 "Invalid TDLS peer %pM for channel switch cancel\n",
1394 addr);
1395 goto out;
1396 }
1397
1398 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1399 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1400 addr);
1401 goto out;
1402 }
1403
1404 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1405 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1406
1407out:
1408 mutex_unlock(&local->sta_mtx);
1409}
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001410
1411static struct sk_buff *
1412ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1413 u32 *ch_sw_tm_ie_offset)
1414{
1415 struct ieee80211_sub_if_data *sdata = sta->sdata;
1416 struct sk_buff *skb;
1417 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1418
1419 /* initial timing are always zero in the template */
1420 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1421
1422 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1423 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1424 0, 0, !sta->sta.tdls_initiator,
1425 extra_ies, sizeof(extra_ies), 0, NULL);
1426 if (!skb)
1427 return NULL;
1428
1429 skb = ieee80211_build_data_template(sdata, skb, 0);
1430 if (IS_ERR(skb)) {
1431 tdls_dbg(sdata,
1432 "Failed building TDLS channel switch resp frame\n");
1433 return NULL;
1434 }
1435
1436 if (ch_sw_tm_ie_offset) {
1437 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1438
1439 if (!tm_ie) {
1440 tdls_dbg(sdata,
1441 "No switch timing IE in TDLS switch resp\n");
1442 dev_kfree_skb_any(skb);
1443 return NULL;
1444 }
1445
1446 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1447 }
1448
1449 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1450 sta->sta.addr);
1451 return skb;
1452}
1453
1454static int
1455ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1456 struct sk_buff *skb)
1457{
1458 struct ieee80211_local *local = sdata->local;
1459 struct ieee802_11_elems elems;
1460 struct sta_info *sta;
1461 struct ieee80211_tdls_data *tf = (void *)skb->data;
1462 bool local_initiator;
1463 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1464 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1465 struct ieee80211_tdls_ch_sw_params params = {};
1466 int ret;
1467
1468 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1469 params.timestamp = rx_status->device_timestamp;
1470
1471 if (skb->len < baselen) {
1472 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1473 skb->len);
1474 return -EINVAL;
1475 }
1476
1477 mutex_lock(&local->sta_mtx);
1478 sta = sta_info_get(sdata, tf->sa);
1479 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1480 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1481 tf->sa);
1482 ret = -EINVAL;
1483 goto out;
1484 }
1485
1486 params.sta = &sta->sta;
1487 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1488 if (params.status != 0) {
1489 ret = 0;
1490 goto call_drv;
1491 }
1492
1493 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1494 skb->len - baselen, false, &elems);
1495 if (elems.parse_error) {
1496 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1497 ret = -EINVAL;
1498 goto out;
1499 }
1500
1501 if (!elems.ch_sw_timing || !elems.lnk_id) {
1502 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1503 ret = -EINVAL;
1504 goto out;
1505 }
1506
1507 /* validate the initiator is set correctly */
1508 local_initiator =
1509 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1510 if (local_initiator == sta->sta.tdls_initiator) {
1511 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1512 ret = -EINVAL;
1513 goto out;
1514 }
1515
1516 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1517 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1518
1519 params.tmpl_skb =
1520 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1521 if (!params.tmpl_skb) {
1522 ret = -ENOENT;
1523 goto out;
1524 }
1525
1526call_drv:
1527 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1528
1529 tdls_dbg(sdata,
1530 "TDLS channel switch response received from %pM status %d\n",
1531 tf->sa, params.status);
1532
1533out:
1534 mutex_unlock(&local->sta_mtx);
1535 dev_kfree_skb_any(params.tmpl_skb);
1536 return ret;
1537}
1538
1539static int
1540ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1541 struct sk_buff *skb)
1542{
1543 struct ieee80211_local *local = sdata->local;
1544 struct ieee802_11_elems elems;
1545 struct cfg80211_chan_def chandef;
1546 struct ieee80211_channel *chan;
1547 enum nl80211_channel_type chan_type;
1548 int freq;
1549 u8 target_channel, oper_class;
1550 bool local_initiator;
1551 struct sta_info *sta;
1552 enum ieee80211_band band;
1553 struct ieee80211_tdls_data *tf = (void *)skb->data;
1554 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1555 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1556 struct ieee80211_tdls_ch_sw_params params = {};
1557 int ret = 0;
1558
1559 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1560 params.timestamp = rx_status->device_timestamp;
1561
1562 if (skb->len < baselen) {
1563 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1564 skb->len);
1565 return -EINVAL;
1566 }
1567
1568 target_channel = tf->u.chan_switch_req.target_channel;
1569 oper_class = tf->u.chan_switch_req.oper_class;
1570
1571 /*
1572 * We can't easily infer the channel band. The operating class is
1573 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1574 * solution here is to treat channels with number >14 as 5GHz ones,
1575 * and specifically check for the (oper_class, channel) combinations
1576 * where this doesn't hold. These are thankfully unique according to
1577 * IEEE802.11-2012.
1578 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1579 * valid here.
1580 */
1581 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1582 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1583 target_channel < 14)
1584 band = IEEE80211_BAND_5GHZ;
1585 else
1586 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1587 IEEE80211_BAND_5GHZ;
1588
1589 freq = ieee80211_channel_to_frequency(target_channel, band);
1590 if (freq == 0) {
1591 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1592 target_channel);
1593 return -EINVAL;
1594 }
1595
1596 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1597 if (!chan) {
1598 tdls_dbg(sdata,
1599 "Unsupported channel for TDLS chan switch: %d\n",
1600 target_channel);
1601 return -EINVAL;
1602 }
1603
1604 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1605 skb->len - baselen, false, &elems);
1606 if (elems.parse_error) {
1607 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1608 return -EINVAL;
1609 }
1610
1611 if (!elems.ch_sw_timing || !elems.lnk_id) {
1612 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1613 return -EINVAL;
1614 }
1615
1616 mutex_lock(&local->sta_mtx);
1617 sta = sta_info_get(sdata, tf->sa);
1618 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1619 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1620 tf->sa);
1621 ret = -EINVAL;
1622 goto out;
1623 }
1624
1625 params.sta = &sta->sta;
1626
1627 /* validate the initiator is set correctly */
1628 local_initiator =
1629 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1630 if (local_initiator == sta->sta.tdls_initiator) {
1631 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1632 ret = -EINVAL;
1633 goto out;
1634 }
1635
1636 if (!sta->sta.ht_cap.ht_supported) {
1637 chan_type = NL80211_CHAN_NO_HT;
1638 } else if (!elems.sec_chan_offs) {
1639 chan_type = NL80211_CHAN_HT20;
1640 } else {
1641 switch (elems.sec_chan_offs->sec_chan_offs) {
1642 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1643 chan_type = NL80211_CHAN_HT40PLUS;
1644 break;
1645 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1646 chan_type = NL80211_CHAN_HT40MINUS;
1647 break;
1648 default:
1649 chan_type = NL80211_CHAN_HT20;
1650 break;
1651 }
1652 }
1653
1654 cfg80211_chandef_create(&chandef, chan, chan_type);
1655 params.chandef = &chandef;
1656
1657 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1658 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1659
1660 params.tmpl_skb =
1661 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1662 &params.ch_sw_tm_ie);
1663 if (!params.tmpl_skb) {
1664 ret = -ENOENT;
1665 goto out;
1666 }
1667
1668 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1669
1670 tdls_dbg(sdata,
1671 "TDLS ch switch request received from %pM ch %d width %d\n",
1672 tf->sa, params.chandef->chan->center_freq,
1673 params.chandef->width);
1674out:
1675 mutex_unlock(&local->sta_mtx);
1676 dev_kfree_skb_any(params.tmpl_skb);
1677 return ret;
1678}
1679
1680void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1681 struct sk_buff *skb)
1682{
1683 struct ieee80211_tdls_data *tf = (void *)skb->data;
1684 struct wiphy *wiphy = sdata->local->hw.wiphy;
1685
1686 /* make sure the driver supports it */
1687 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1688 return;
1689
1690 /* we want to access the entire packet */
1691 if (skb_linearize(skb))
1692 return;
1693 /*
1694 * The packet/size was already validated by mac80211 Rx path, only look
1695 * at the action type.
1696 */
1697 switch (tf->action_code) {
1698 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1699 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1700 break;
1701 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1702 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1703 break;
1704 default:
1705 WARN_ON_ONCE(1);
1706 return;
1707 }
1708}