blob: 5bcd542e49334f2d3f6af2e03ba1ddf2415cc1a5 [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
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300290 rcu_read_lock();
291
292 /* we should have the peer STA if we're already responding */
293 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
294 sta = sta_info_get(sdata, peer);
295 if (WARN_ON_ONCE(!sta)) {
296 rcu_read_unlock();
297 return;
298 }
299 }
300
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300301 ieee80211_add_srates_ie(sdata, skb, false, band);
302 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200303 ieee80211_tdls_add_supp_channels(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300304
305 /* add any custom IEs that go before Extended Capabilities */
306 if (extra_ies_len) {
307 static const u8 before_ext_cap[] = {
308 WLAN_EID_SUPP_RATES,
309 WLAN_EID_COUNTRY,
310 WLAN_EID_EXT_SUPP_RATES,
311 WLAN_EID_SUPPORTED_CHANNELS,
312 WLAN_EID_RSN,
313 };
314 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
315 before_ext_cap,
316 ARRAY_SIZE(before_ext_cap),
317 offset);
318 pos = skb_put(skb, noffset - offset);
319 memcpy(pos, extra_ies + offset, noffset - offset);
320 offset = noffset;
321 }
322
Arik Nemtsov78632a12014-11-09 18:50:14 +0200323 ieee80211_tdls_add_ext_capab(local, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300324
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300325 /* add the QoS element if we support it */
326 if (local->hw.queues >= IEEE80211_NUM_ACS &&
327 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
328 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
329
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300330 /* add any custom IEs that go before HT capabilities */
331 if (extra_ies_len) {
332 static const u8 before_ht_cap[] = {
333 WLAN_EID_SUPP_RATES,
334 WLAN_EID_COUNTRY,
335 WLAN_EID_EXT_SUPP_RATES,
336 WLAN_EID_SUPPORTED_CHANNELS,
337 WLAN_EID_RSN,
338 WLAN_EID_EXT_CAPABILITY,
339 WLAN_EID_QOS_CAPA,
340 WLAN_EID_FAST_BSS_TRANSITION,
341 WLAN_EID_TIMEOUT_INTERVAL,
342 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
343 };
344 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
345 before_ht_cap,
346 ARRAY_SIZE(before_ht_cap),
347 offset);
348 pos = skb_put(skb, noffset - offset);
349 memcpy(pos, extra_ies + offset, noffset - offset);
350 offset = noffset;
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;
986 goto exit;
987 }
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;
1001 goto exit;
1002 }
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 Nemtsovdb67d662014-06-11 17:18:24 +03001007
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001008 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1009 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +03001010 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001011 extra_ies, extra_ies_len, 0,
1012 NULL);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001013 if (ret < 0)
1014 goto exit;
1015
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001016 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001017 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001018 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +03001019 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001020
1021exit:
1022 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001023 return ret;
1024}
1025
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001026static int
1027ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1028 const u8 *peer, u8 action_code, u8 dialog_token,
1029 u16 status_code, u32 peer_capability,
1030 bool initiator, const u8 *extra_ies,
1031 size_t extra_ies_len)
1032{
1033 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1034 struct ieee80211_local *local = sdata->local;
1035 struct sta_info *sta;
1036 int ret;
1037
1038 /*
1039 * No packets can be transmitted to the peer via the AP during setup -
1040 * the STA is set as a TDLS peer, but is not authorized.
1041 * During teardown, we prevent direct transmissions by stopping the
1042 * queues and flushing all direct packets.
1043 */
1044 ieee80211_stop_vif_queues(local, sdata,
1045 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001046 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001047
1048 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1049 dialog_token, status_code,
1050 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001051 extra_ies, extra_ies_len, 0,
1052 NULL);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001053 if (ret < 0)
1054 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1055 ret);
1056
1057 /*
1058 * Remove the STA AUTH flag to force further traffic through the AP. If
1059 * the STA was unreachable, it was already removed.
1060 */
1061 rcu_read_lock();
1062 sta = sta_info_get(sdata, peer);
1063 if (sta)
1064 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1065 rcu_read_unlock();
1066
1067 ieee80211_wake_vif_queues(local, sdata,
1068 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1069
1070 return 0;
1071}
1072
Arik Nemtsov191dd462014-06-11 17:18:23 +03001073int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1074 const u8 *peer, u8 action_code, u8 dialog_token,
1075 u16 status_code, u32 peer_capability,
1076 bool initiator, const u8 *extra_ies,
1077 size_t extra_ies_len)
1078{
1079 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1080 int ret;
1081
1082 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1083 return -ENOTSUPP;
1084
1085 /* make sure we are in managed mode, and associated */
1086 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1087 !sdata->u.mgd.associated)
1088 return -EINVAL;
1089
1090 switch (action_code) {
1091 case WLAN_TDLS_SETUP_REQUEST:
1092 case WLAN_TDLS_SETUP_RESPONSE:
1093 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1094 dialog_token, status_code,
1095 peer_capability, initiator,
1096 extra_ies, extra_ies_len);
1097 break;
1098 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001099 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1100 action_code, dialog_token,
1101 status_code,
1102 peer_capability, initiator,
1103 extra_ies, extra_ies_len);
1104 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +03001105 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +03001106 /*
1107 * Protect the discovery so we can hear the TDLS discovery
1108 * response frame. It is transmitted directly and not buffered
1109 * by the AP.
1110 */
1111 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1112 /* fall-through */
1113 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +03001114 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1115 /* no special handling */
1116 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1117 action_code,
1118 dialog_token,
1119 status_code,
1120 peer_capability,
1121 initiator, extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001122 extra_ies_len, 0, NULL);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001123 break;
1124 default:
1125 ret = -EOPNOTSUPP;
1126 break;
1127 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001128
1129 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1130 action_code, peer, ret);
1131 return ret;
1132}
1133
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001134int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +02001135 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001136{
1137 struct sta_info *sta;
1138 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001139 struct ieee80211_local *local = sdata->local;
1140 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001141
1142 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1143 return -ENOTSUPP;
1144
1145 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1146 return -EINVAL;
1147
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001148 switch (oper) {
1149 case NL80211_TDLS_ENABLE_LINK:
1150 case NL80211_TDLS_DISABLE_LINK:
1151 break;
1152 case NL80211_TDLS_TEARDOWN:
1153 case NL80211_TDLS_SETUP:
1154 case NL80211_TDLS_DISCOVERY_REQ:
1155 /* We don't support in-driver setup/teardown/discovery */
1156 return -ENOTSUPP;
1157 }
1158
1159 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001160 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1161
1162 switch (oper) {
1163 case NL80211_TDLS_ENABLE_LINK:
1164 rcu_read_lock();
1165 sta = sta_info_get(sdata, peer);
1166 if (!sta) {
1167 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001168 ret = -ENOLINK;
1169 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001170 }
1171
1172 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1173 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001174
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001175 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1176 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001177 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001178 break;
1179 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +03001180 /*
1181 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1182 * created while the queues were stopped, so it might still be
1183 * pending. Before flushing the queues we need to be sure the
1184 * message is handled by the tasklet handling pending messages,
1185 * otherwise we might start destroying the station before
1186 * sending the teardown packet.
1187 * Note that this only forces the tasklet to flush pendings -
1188 * not to stop the tasklet from rescheduling itself.
1189 */
1190 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001191 /* flush a potentially queued teardown packet */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001192 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001193
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001194 ret = sta_info_destroy_addr(sdata, peer);
1195 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001196 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001197 ret = -ENOTSUPP;
1198 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001199 }
1200
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001201 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1202 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1203 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001204 }
1205
1206 mutex_unlock(&local->mtx);
1207 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001208}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +03001209
1210void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1211 enum nl80211_tdls_operation oper,
1212 u16 reason_code, gfp_t gfp)
1213{
1214 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1215
1216 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1217 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1218 oper);
1219 return;
1220 }
1221
1222 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1223}
1224EXPORT_SYMBOL(ieee80211_tdls_oper_request);
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +02001225
1226static void
1227iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1228{
1229 struct ieee80211_ch_switch_timing *ch_sw;
1230
1231 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1232 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1233
1234 ch_sw = (void *)buf;
1235 ch_sw->switch_time = cpu_to_le16(switch_time);
1236 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1237}
1238
1239/* find switch timing IE in SKB ready for Tx */
1240static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1241{
1242 struct ieee80211_tdls_data *tf;
1243 const u8 *ie_start;
1244
1245 /*
1246 * Get the offset for the new location of the switch timing IE.
1247 * The SKB network header will now point to the "payload_type"
1248 * element of the TDLS data frame struct.
1249 */
1250 tf = container_of(skb->data + skb_network_offset(skb),
1251 struct ieee80211_tdls_data, payload_type);
1252 ie_start = tf->u.chan_switch_req.variable;
1253 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1254 skb->len - (ie_start - skb->data));
1255}
1256
1257static struct sk_buff *
1258ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1259 struct cfg80211_chan_def *chandef,
1260 u32 *ch_sw_tm_ie_offset)
1261{
1262 struct ieee80211_sub_if_data *sdata = sta->sdata;
1263 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1264 2 + sizeof(struct ieee80211_ch_switch_timing)];
1265 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1266 u8 *pos = extra_ies;
1267 struct sk_buff *skb;
1268
1269 /*
1270 * if chandef points to a wide channel add a Secondary-Channel
1271 * Offset information element
1272 */
1273 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1274 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1275 bool ht40plus;
1276
1277 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1278 *pos++ = sizeof(*sec_chan_ie);
1279 sec_chan_ie = (void *)pos;
1280
1281 ht40plus = cfg80211_get_chandef_type(chandef) ==
1282 NL80211_CHAN_HT40PLUS;
1283 sec_chan_ie->sec_chan_offs = ht40plus ?
1284 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1285 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1286 pos += sizeof(*sec_chan_ie);
1287
1288 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1289 }
1290
1291 /* just set the values to 0, this is a template */
1292 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1293
1294 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1295 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1296 0, 0, !sta->sta.tdls_initiator,
1297 extra_ies, extra_ies_len,
1298 oper_class, chandef);
1299 if (!skb)
1300 return NULL;
1301
1302 skb = ieee80211_build_data_template(sdata, skb, 0);
1303 if (IS_ERR(skb)) {
1304 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1305 return NULL;
1306 }
1307
1308 if (ch_sw_tm_ie_offset) {
1309 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1310
1311 if (!tm_ie) {
1312 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1313 dev_kfree_skb_any(skb);
1314 return NULL;
1315 }
1316
1317 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1318 }
1319
1320 tdls_dbg(sdata,
1321 "TDLS channel switch request template for %pM ch %d width %d\n",
1322 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1323 return skb;
1324}
1325
1326int
1327ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1328 const u8 *addr, u8 oper_class,
1329 struct cfg80211_chan_def *chandef)
1330{
1331 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1332 struct ieee80211_local *local = sdata->local;
1333 struct sta_info *sta;
1334 struct sk_buff *skb = NULL;
1335 u32 ch_sw_tm_ie;
1336 int ret;
1337
1338 mutex_lock(&local->sta_mtx);
1339 sta = sta_info_get(sdata, addr);
1340 if (!sta) {
1341 tdls_dbg(sdata,
1342 "Invalid TDLS peer %pM for channel switch request\n",
1343 addr);
1344 ret = -ENOENT;
1345 goto out;
1346 }
1347
1348 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1349 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1350 addr);
1351 ret = -ENOTSUPP;
1352 goto out;
1353 }
1354
1355 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1356 &ch_sw_tm_ie);
1357 if (!skb) {
1358 ret = -ENOENT;
1359 goto out;
1360 }
1361
1362 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1363 chandef, skb, ch_sw_tm_ie);
1364 if (!ret)
1365 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1366
1367out:
1368 mutex_unlock(&local->sta_mtx);
1369 dev_kfree_skb_any(skb);
1370 return ret;
1371}
1372
1373void
1374ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1375 struct net_device *dev,
1376 const u8 *addr)
1377{
1378 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1379 struct ieee80211_local *local = sdata->local;
1380 struct sta_info *sta;
1381
1382 mutex_lock(&local->sta_mtx);
1383 sta = sta_info_get(sdata, addr);
1384 if (!sta) {
1385 tdls_dbg(sdata,
1386 "Invalid TDLS peer %pM for channel switch cancel\n",
1387 addr);
1388 goto out;
1389 }
1390
1391 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1392 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1393 addr);
1394 goto out;
1395 }
1396
1397 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1398 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1399
1400out:
1401 mutex_unlock(&local->sta_mtx);
1402}
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001403
1404static struct sk_buff *
1405ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1406 u32 *ch_sw_tm_ie_offset)
1407{
1408 struct ieee80211_sub_if_data *sdata = sta->sdata;
1409 struct sk_buff *skb;
1410 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1411
1412 /* initial timing are always zero in the template */
1413 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1414
1415 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1416 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1417 0, 0, !sta->sta.tdls_initiator,
1418 extra_ies, sizeof(extra_ies), 0, NULL);
1419 if (!skb)
1420 return NULL;
1421
1422 skb = ieee80211_build_data_template(sdata, skb, 0);
1423 if (IS_ERR(skb)) {
1424 tdls_dbg(sdata,
1425 "Failed building TDLS channel switch resp frame\n");
1426 return NULL;
1427 }
1428
1429 if (ch_sw_tm_ie_offset) {
1430 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1431
1432 if (!tm_ie) {
1433 tdls_dbg(sdata,
1434 "No switch timing IE in TDLS switch resp\n");
1435 dev_kfree_skb_any(skb);
1436 return NULL;
1437 }
1438
1439 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1440 }
1441
1442 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1443 sta->sta.addr);
1444 return skb;
1445}
1446
1447static int
1448ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1449 struct sk_buff *skb)
1450{
1451 struct ieee80211_local *local = sdata->local;
1452 struct ieee802_11_elems elems;
1453 struct sta_info *sta;
1454 struct ieee80211_tdls_data *tf = (void *)skb->data;
1455 bool local_initiator;
1456 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1457 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1458 struct ieee80211_tdls_ch_sw_params params = {};
1459 int ret;
1460
1461 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1462 params.timestamp = rx_status->device_timestamp;
1463
1464 if (skb->len < baselen) {
1465 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1466 skb->len);
1467 return -EINVAL;
1468 }
1469
1470 mutex_lock(&local->sta_mtx);
1471 sta = sta_info_get(sdata, tf->sa);
1472 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1473 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1474 tf->sa);
1475 ret = -EINVAL;
1476 goto out;
1477 }
1478
1479 params.sta = &sta->sta;
1480 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1481 if (params.status != 0) {
1482 ret = 0;
1483 goto call_drv;
1484 }
1485
1486 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1487 skb->len - baselen, false, &elems);
1488 if (elems.parse_error) {
1489 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1490 ret = -EINVAL;
1491 goto out;
1492 }
1493
1494 if (!elems.ch_sw_timing || !elems.lnk_id) {
1495 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1496 ret = -EINVAL;
1497 goto out;
1498 }
1499
1500 /* validate the initiator is set correctly */
1501 local_initiator =
1502 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1503 if (local_initiator == sta->sta.tdls_initiator) {
1504 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1505 ret = -EINVAL;
1506 goto out;
1507 }
1508
1509 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1510 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1511
1512 params.tmpl_skb =
1513 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1514 if (!params.tmpl_skb) {
1515 ret = -ENOENT;
1516 goto out;
1517 }
1518
1519call_drv:
1520 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1521
1522 tdls_dbg(sdata,
1523 "TDLS channel switch response received from %pM status %d\n",
1524 tf->sa, params.status);
1525
1526out:
1527 mutex_unlock(&local->sta_mtx);
1528 dev_kfree_skb_any(params.tmpl_skb);
1529 return ret;
1530}
1531
1532static int
1533ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1534 struct sk_buff *skb)
1535{
1536 struct ieee80211_local *local = sdata->local;
1537 struct ieee802_11_elems elems;
1538 struct cfg80211_chan_def chandef;
1539 struct ieee80211_channel *chan;
1540 enum nl80211_channel_type chan_type;
1541 int freq;
1542 u8 target_channel, oper_class;
1543 bool local_initiator;
1544 struct sta_info *sta;
1545 enum ieee80211_band band;
1546 struct ieee80211_tdls_data *tf = (void *)skb->data;
1547 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1548 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1549 struct ieee80211_tdls_ch_sw_params params = {};
1550 int ret = 0;
1551
1552 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1553 params.timestamp = rx_status->device_timestamp;
1554
1555 if (skb->len < baselen) {
1556 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1557 skb->len);
1558 return -EINVAL;
1559 }
1560
1561 target_channel = tf->u.chan_switch_req.target_channel;
1562 oper_class = tf->u.chan_switch_req.oper_class;
1563
1564 /*
1565 * We can't easily infer the channel band. The operating class is
1566 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1567 * solution here is to treat channels with number >14 as 5GHz ones,
1568 * and specifically check for the (oper_class, channel) combinations
1569 * where this doesn't hold. These are thankfully unique according to
1570 * IEEE802.11-2012.
1571 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1572 * valid here.
1573 */
1574 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1575 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1576 target_channel < 14)
1577 band = IEEE80211_BAND_5GHZ;
1578 else
1579 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1580 IEEE80211_BAND_5GHZ;
1581
1582 freq = ieee80211_channel_to_frequency(target_channel, band);
1583 if (freq == 0) {
1584 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1585 target_channel);
1586 return -EINVAL;
1587 }
1588
1589 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1590 if (!chan) {
1591 tdls_dbg(sdata,
1592 "Unsupported channel for TDLS chan switch: %d\n",
1593 target_channel);
1594 return -EINVAL;
1595 }
1596
1597 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1598 skb->len - baselen, false, &elems);
1599 if (elems.parse_error) {
1600 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1601 return -EINVAL;
1602 }
1603
1604 if (!elems.ch_sw_timing || !elems.lnk_id) {
1605 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1606 return -EINVAL;
1607 }
1608
1609 mutex_lock(&local->sta_mtx);
1610 sta = sta_info_get(sdata, tf->sa);
1611 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1612 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1613 tf->sa);
1614 ret = -EINVAL;
1615 goto out;
1616 }
1617
1618 params.sta = &sta->sta;
1619
1620 /* validate the initiator is set correctly */
1621 local_initiator =
1622 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1623 if (local_initiator == sta->sta.tdls_initiator) {
1624 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1625 ret = -EINVAL;
1626 goto out;
1627 }
1628
1629 if (!sta->sta.ht_cap.ht_supported) {
1630 chan_type = NL80211_CHAN_NO_HT;
1631 } else if (!elems.sec_chan_offs) {
1632 chan_type = NL80211_CHAN_HT20;
1633 } else {
1634 switch (elems.sec_chan_offs->sec_chan_offs) {
1635 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1636 chan_type = NL80211_CHAN_HT40PLUS;
1637 break;
1638 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1639 chan_type = NL80211_CHAN_HT40MINUS;
1640 break;
1641 default:
1642 chan_type = NL80211_CHAN_HT20;
1643 break;
1644 }
1645 }
1646
1647 cfg80211_chandef_create(&chandef, chan, chan_type);
1648 params.chandef = &chandef;
1649
1650 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1651 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1652
1653 params.tmpl_skb =
1654 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1655 &params.ch_sw_tm_ie);
1656 if (!params.tmpl_skb) {
1657 ret = -ENOENT;
1658 goto out;
1659 }
1660
1661 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1662
1663 tdls_dbg(sdata,
1664 "TDLS ch switch request received from %pM ch %d width %d\n",
1665 tf->sa, params.chandef->chan->center_freq,
1666 params.chandef->width);
1667out:
1668 mutex_unlock(&local->sta_mtx);
1669 dev_kfree_skb_any(params.tmpl_skb);
1670 return ret;
1671}
1672
1673void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1674 struct sk_buff *skb)
1675{
1676 struct ieee80211_tdls_data *tf = (void *)skb->data;
1677 struct wiphy *wiphy = sdata->local->hw.wiphy;
1678
1679 /* make sure the driver supports it */
1680 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1681 return;
1682
1683 /* we want to access the entire packet */
1684 if (skb_linearize(skb))
1685 return;
1686 /*
1687 * The packet/size was already validated by mac80211 Rx path, only look
1688 * at the action type.
1689 */
1690 switch (tf->action_code) {
1691 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1692 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1693 break;
1694 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1695 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1696 break;
1697 default:
1698 WARN_ON_ONCE(1);
1699 return;
1700 }
1701}