blob: 8a92a920ff17fa78b76640439f5ed81fd7a0ccb3 [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 Nemtsova38700d2015-03-18 08:46:08 +0200139static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
140 struct sk_buff *skb)
141{
142 u8 *pos;
143 u8 op_class;
144
145 if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
146 &op_class))
147 return;
148
149 pos = skb_put(skb, 4);
150 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
151 *pos++ = 2; /* len */
152
153 *pos++ = op_class;
154 *pos++ = op_class; /* give current operating class as alternate too */
155}
156
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200157static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
158{
159 u8 *pos = (void *)skb_put(skb, 3);
160
161 *pos++ = WLAN_EID_BSS_COEX_2040;
162 *pos++ = 1; /* len */
163
164 *pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
165}
166
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300167static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
168 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300169{
170 struct ieee80211_local *local = sdata->local;
171 u16 capab;
172
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300173 /* The capability will be 0 when sending a failure code */
174 if (status_code != 0)
175 return 0;
176
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300177 capab = 0;
178 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
179 return capab;
180
181 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
182 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
183 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
184 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
185
186 return capab;
187}
188
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300189static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
190 struct sk_buff *skb, const u8 *peer,
191 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300192{
193 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300194 const u8 *init_addr, *rsp_addr;
195
196 if (initiator) {
197 init_addr = sdata->vif.addr;
198 rsp_addr = peer;
199 } else {
200 init_addr = peer;
201 rsp_addr = sdata->vif.addr;
202 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300203
204 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
205
206 lnkid->ie_type = WLAN_EID_LINK_ID;
207 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
208
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300209 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
210 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
211 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300212}
213
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200214static void
215ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
216{
217 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
218 u8 *pos = (void *)skb_put(skb, 4);
219
220 *pos++ = WLAN_EID_AID;
221 *pos++ = 2; /* len */
222 put_unaligned_le16(ifmgd->aid, pos);
223}
224
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300225/* translate numbering in the WMM parameter IE to the mac80211 notation */
226static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
227{
228 switch (ac) {
229 default:
230 WARN_ON_ONCE(1);
231 case 0:
232 return IEEE80211_AC_BE;
233 case 1:
234 return IEEE80211_AC_BK;
235 case 2:
236 return IEEE80211_AC_VI;
237 case 3:
238 return IEEE80211_AC_VO;
239 }
240}
241
242static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
243{
244 u8 ret;
245
246 ret = aifsn & 0x0f;
247 if (acm)
248 ret |= 0x10;
249 ret |= (aci << 5) & 0x60;
250 return ret;
251}
252
253static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
254{
255 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
256 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
257}
258
259static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
260 struct sk_buff *skb)
261{
262 struct ieee80211_wmm_param_ie *wmm;
263 struct ieee80211_tx_queue_params *txq;
264 int i;
265
266 wmm = (void *)skb_put(skb, sizeof(*wmm));
267 memset(wmm, 0, sizeof(*wmm));
268
269 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
270 wmm->len = sizeof(*wmm) - 2;
271
272 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
273 wmm->oui[1] = 0x50;
274 wmm->oui[2] = 0xf2;
275 wmm->oui_type = 2; /* WME */
276 wmm->oui_subtype = 1; /* WME param */
277 wmm->version = 1; /* WME ver */
278 wmm->qos_info = 0; /* U-APSD not in use */
279
280 /*
281 * Use the EDCA parameters defined for the BSS, or default if the AP
282 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
283 */
284 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
285 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
286 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
287 txq->acm, i);
288 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
289 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
290 }
291}
292
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300293static void
294ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
295 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300296 u8 action_code, bool initiator,
297 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300298{
299 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300300 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300301 struct ieee80211_supported_band *sband;
302 struct ieee80211_sta_ht_cap ht_cap;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200303 struct ieee80211_sta_vht_cap vht_cap;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300304 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300305 size_t offset = 0, noffset;
306 u8 *pos;
307
308 ieee80211_add_srates_ie(sdata, skb, false, band);
309 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200310 ieee80211_tdls_add_supp_channels(sdata, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300311
312 /* add any custom IEs that go before Extended Capabilities */
313 if (extra_ies_len) {
314 static const u8 before_ext_cap[] = {
315 WLAN_EID_SUPP_RATES,
316 WLAN_EID_COUNTRY,
317 WLAN_EID_EXT_SUPP_RATES,
318 WLAN_EID_SUPPORTED_CHANNELS,
319 WLAN_EID_RSN,
320 };
321 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
322 before_ext_cap,
323 ARRAY_SIZE(before_ext_cap),
324 offset);
325 pos = skb_put(skb, noffset - offset);
326 memcpy(pos, extra_ies + offset, noffset - offset);
327 offset = noffset;
328 }
329
Arik Nemtsov78632a12014-11-09 18:50:14 +0200330 ieee80211_tdls_add_ext_capab(local, skb);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300331
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300332 /* add the QoS element if we support it */
333 if (local->hw.queues >= IEEE80211_NUM_ACS &&
334 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
335 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
336
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300337 /* add any custom IEs that go before HT capabilities */
338 if (extra_ies_len) {
339 static const u8 before_ht_cap[] = {
340 WLAN_EID_SUPP_RATES,
341 WLAN_EID_COUNTRY,
342 WLAN_EID_EXT_SUPP_RATES,
343 WLAN_EID_SUPPORTED_CHANNELS,
344 WLAN_EID_RSN,
345 WLAN_EID_EXT_CAPABILITY,
346 WLAN_EID_QOS_CAPA,
347 WLAN_EID_FAST_BSS_TRANSITION,
348 WLAN_EID_TIMEOUT_INTERVAL,
349 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
350 };
351 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
352 before_ht_cap,
353 ARRAY_SIZE(before_ht_cap),
354 offset);
355 pos = skb_put(skb, noffset - offset);
356 memcpy(pos, extra_ies + offset, noffset - offset);
357 offset = noffset;
358 }
359
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +0200360 rcu_read_lock();
361
362 /* we should have the peer STA if we're already responding */
363 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
364 sta = sta_info_get(sdata, peer);
365 if (WARN_ON_ONCE(!sta)) {
366 rcu_read_unlock();
367 return;
368 }
369 }
370
Arik Nemtsova38700d2015-03-18 08:46:08 +0200371 ieee80211_tdls_add_oper_classes(sdata, skb);
372
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300373 /*
374 * with TDLS we can switch channels, and HT-caps are not necessarily
375 * the same on all bands. The specification limits the setup to a
376 * single HT-cap, so use the current band for now.
377 */
378 sband = local->hw.wiphy->bands[band];
379 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300380
Arik Nemtsov070e1762015-03-30 11:16:23 +0300381 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
382 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
383 ht_cap.ht_supported) {
Johannes Bergc5309ba2015-01-23 11:42:14 +0100384 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300385
Johannes Bergc5309ba2015-01-23 11:42:14 +0100386 /* disable SMPS in TDLS initiator */
387 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
388 << IEEE80211_HT_CAP_SM_PS_SHIFT;
389
390 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
391 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
392 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
393 ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
394 /* disable SMPS in TDLS responder */
395 sta->sta.ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
396 << IEEE80211_HT_CAP_SM_PS_SHIFT;
397
398 /* the peer caps are already intersected with our own */
399 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300400
401 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
402 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
403 }
404
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200405 if (ht_cap.ht_supported &&
406 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
407 ieee80211_tdls_add_bss_coex_ie(skb);
408
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200409 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
410
411 /* add any custom IEs that go before VHT capabilities */
412 if (extra_ies_len) {
413 static const u8 before_vht_cap[] = {
414 WLAN_EID_SUPP_RATES,
415 WLAN_EID_COUNTRY,
416 WLAN_EID_EXT_SUPP_RATES,
417 WLAN_EID_SUPPORTED_CHANNELS,
418 WLAN_EID_RSN,
419 WLAN_EID_EXT_CAPABILITY,
420 WLAN_EID_QOS_CAPA,
421 WLAN_EID_FAST_BSS_TRANSITION,
422 WLAN_EID_TIMEOUT_INTERVAL,
423 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
424 WLAN_EID_MULTI_BAND,
425 };
426 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
427 before_vht_cap,
428 ARRAY_SIZE(before_vht_cap),
429 offset);
430 pos = skb_put(skb, noffset - offset);
431 memcpy(pos, extra_ies + offset, noffset - offset);
432 offset = noffset;
433 }
434
435 /* build the VHT-cap similarly to the HT-cap */
436 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Arik Nemtsov070e1762015-03-30 11:16:23 +0300437 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
438 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
439 vht_cap.vht_supported) {
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200440 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
441
442 /* the AID is present only when VHT is implemented */
Arik Nemtsov070e1762015-03-30 11:16:23 +0300443 if (action_code == WLAN_TDLS_SETUP_REQUEST)
444 ieee80211_tdls_add_aid(sdata, skb);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200445
446 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
447 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
448 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
449 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
450 /* the peer caps are already intersected with our own */
451 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
452
453 /* the AID is present only when VHT is implemented */
454 ieee80211_tdls_add_aid(sdata, skb);
455
456 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
457 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
458 }
459
460 rcu_read_unlock();
461
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300462 /* add any remaining IEs */
463 if (extra_ies_len) {
464 noffset = extra_ies_len;
465 pos = skb_put(skb, noffset - offset);
466 memcpy(pos, extra_ies + offset, noffset - offset);
467 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300468
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300469}
470
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300471static void
472ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
473 struct sk_buff *skb, const u8 *peer,
474 bool initiator, const u8 *extra_ies,
475 size_t extra_ies_len)
476{
477 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300478 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300479 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300480 struct sta_info *sta, *ap_sta;
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200481 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300482 u8 *pos;
483
484 rcu_read_lock();
485
486 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300487 ap_sta = sta_info_get(sdata, ifmgd->bssid);
488 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300489 rcu_read_unlock();
490 return;
491 }
492
493 /* add any custom IEs that go before the QoS IE */
494 if (extra_ies_len) {
495 static const u8 before_qos[] = {
496 WLAN_EID_RSN,
497 };
498 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
499 before_qos,
500 ARRAY_SIZE(before_qos),
501 offset);
502 pos = skb_put(skb, noffset - offset);
503 memcpy(pos, extra_ies + offset, noffset - offset);
504 offset = noffset;
505 }
506
507 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200508 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300509 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
510
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300511 /* add any custom IEs that go before HT operation */
512 if (extra_ies_len) {
513 static const u8 before_ht_op[] = {
514 WLAN_EID_RSN,
515 WLAN_EID_QOS_CAPA,
516 WLAN_EID_FAST_BSS_TRANSITION,
517 WLAN_EID_TIMEOUT_INTERVAL,
518 };
519 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
520 before_ht_op,
521 ARRAY_SIZE(before_ht_op),
522 offset);
523 pos = skb_put(skb, noffset - offset);
524 memcpy(pos, extra_ies + offset, noffset - offset);
525 offset = noffset;
526 }
527
528 /* if HT support is only added in TDLS, we need an HT-operation IE */
529 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
Arik Nemtsov890b7872015-05-07 15:30:41 +0300530 pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
531 /* send an empty HT operation IE */
532 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
533 &sdata->vif.bss_conf.chandef, 0);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300534 }
535
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200536 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
537
538 /* only include VHT-operation if not on the 2.4GHz band */
Arik Nemtsov890b7872015-05-07 15:30:41 +0300539 if (band != IEEE80211_BAND_2GHZ && sta->sta.vht_cap.vht_supported) {
540 pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
541 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
542 &sdata->vif.bss_conf.chandef);
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200543 }
544
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300545 rcu_read_unlock();
546
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300547 /* add any remaining IEs */
548 if (extra_ies_len) {
549 noffset = extra_ies_len;
550 pos = skb_put(skb, noffset - offset);
551 memcpy(pos, extra_ies + offset, noffset - offset);
552 }
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300553}
554
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200555static void
556ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
557 struct sk_buff *skb, const u8 *peer,
558 bool initiator, const u8 *extra_ies,
559 size_t extra_ies_len, u8 oper_class,
560 struct cfg80211_chan_def *chandef)
561{
562 struct ieee80211_tdls_data *tf;
563 size_t offset = 0, noffset;
564 u8 *pos;
565
566 if (WARN_ON_ONCE(!chandef))
567 return;
568
569 tf = (void *)skb->data;
570 tf->u.chan_switch_req.target_channel =
571 ieee80211_frequency_to_channel(chandef->chan->center_freq);
572 tf->u.chan_switch_req.oper_class = oper_class;
573
574 if (extra_ies_len) {
575 static const u8 before_lnkie[] = {
576 WLAN_EID_SECONDARY_CHANNEL_OFFSET,
577 };
578 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
579 before_lnkie,
580 ARRAY_SIZE(before_lnkie),
581 offset);
582 pos = skb_put(skb, noffset - offset);
583 memcpy(pos, extra_ies + offset, noffset - offset);
584 offset = noffset;
585 }
586
587 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
588
589 /* add any remaining IEs */
590 if (extra_ies_len) {
591 noffset = extra_ies_len;
592 pos = skb_put(skb, noffset - offset);
593 memcpy(pos, extra_ies + offset, noffset - offset);
594 }
595}
596
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200597static void
598ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
599 struct sk_buff *skb, const u8 *peer,
600 u16 status_code, bool initiator,
601 const u8 *extra_ies,
602 size_t extra_ies_len)
603{
604 if (status_code == 0)
605 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
606
607 if (extra_ies_len)
608 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
609}
610
Arik Nemtsov46792a22014-07-17 17:14:19 +0300611static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
612 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300613 u8 action_code, u16 status_code,
614 bool initiator, const u8 *extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +0200615 size_t extra_ies_len, u8 oper_class,
616 struct cfg80211_chan_def *chandef)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300617{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300618 switch (action_code) {
619 case WLAN_TDLS_SETUP_REQUEST:
620 case WLAN_TDLS_SETUP_RESPONSE:
621 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300622 if (status_code == 0)
623 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
624 action_code,
625 initiator,
626 extra_ies,
627 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300628 break;
629 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300630 if (status_code == 0)
631 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
632 initiator, extra_ies,
633 extra_ies_len);
634 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300635 case WLAN_TDLS_TEARDOWN:
636 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300637 if (extra_ies_len)
638 memcpy(skb_put(skb, extra_ies_len), extra_ies,
639 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300640 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
641 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300642 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200643 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
644 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
645 initiator, extra_ies,
646 extra_ies_len,
647 oper_class, chandef);
648 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200649 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
650 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
651 status_code,
652 initiator, extra_ies,
653 extra_ies_len);
654 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300655 }
656
Arik Nemtsov46792a22014-07-17 17:14:19 +0300657}
658
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300659static int
660ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200661 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300662 u16 status_code, struct sk_buff *skb)
663{
664 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300665 struct ieee80211_tdls_data *tf;
666
667 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
668
669 memcpy(tf->da, peer, ETH_ALEN);
670 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
671 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
672 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
673
Arik Nemtsov59cd85c2014-09-09 17:11:02 +0300674 /* network header is after the ethernet header */
675 skb_set_network_header(skb, ETH_HLEN);
676
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300677 switch (action_code) {
678 case WLAN_TDLS_SETUP_REQUEST:
679 tf->category = WLAN_CATEGORY_TDLS;
680 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
681
682 skb_put(skb, sizeof(tf->u.setup_req));
683 tf->u.setup_req.dialog_token = dialog_token;
684 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300685 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
686 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300687 break;
688 case WLAN_TDLS_SETUP_RESPONSE:
689 tf->category = WLAN_CATEGORY_TDLS;
690 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
691
692 skb_put(skb, sizeof(tf->u.setup_resp));
693 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
694 tf->u.setup_resp.dialog_token = dialog_token;
695 tf->u.setup_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300696 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
697 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300698 break;
699 case WLAN_TDLS_SETUP_CONFIRM:
700 tf->category = WLAN_CATEGORY_TDLS;
701 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
702
703 skb_put(skb, sizeof(tf->u.setup_cfm));
704 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
705 tf->u.setup_cfm.dialog_token = dialog_token;
706 break;
707 case WLAN_TDLS_TEARDOWN:
708 tf->category = WLAN_CATEGORY_TDLS;
709 tf->action_code = WLAN_TDLS_TEARDOWN;
710
711 skb_put(skb, sizeof(tf->u.teardown));
712 tf->u.teardown.reason_code = cpu_to_le16(status_code);
713 break;
714 case WLAN_TDLS_DISCOVERY_REQUEST:
715 tf->category = WLAN_CATEGORY_TDLS;
716 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
717
718 skb_put(skb, sizeof(tf->u.discover_req));
719 tf->u.discover_req.dialog_token = dialog_token;
720 break;
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200721 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
722 tf->category = WLAN_CATEGORY_TDLS;
723 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
724
725 skb_put(skb, sizeof(tf->u.chan_switch_req));
726 break;
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200727 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
728 tf->category = WLAN_CATEGORY_TDLS;
729 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
730
731 skb_put(skb, sizeof(tf->u.chan_switch_resp));
732 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
733 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300734 default:
735 return -EINVAL;
736 }
737
738 return 0;
739}
740
741static int
742ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200743 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300744 u16 status_code, struct sk_buff *skb)
745{
746 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300747 struct ieee80211_mgmt *mgmt;
748
749 mgmt = (void *)skb_put(skb, 24);
750 memset(mgmt, 0, 24);
751 memcpy(mgmt->da, peer, ETH_ALEN);
752 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
753 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
754
755 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
756 IEEE80211_STYPE_ACTION);
757
758 switch (action_code) {
759 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
760 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
761 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
762 mgmt->u.action.u.tdls_discover_resp.action_code =
763 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
764 mgmt->u.action.u.tdls_discover_resp.dialog_token =
765 dialog_token;
766 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300767 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
768 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300769 break;
770 default:
771 return -EINVAL;
772 }
773
774 return 0;
775}
776
Arik Nemtsovc2733902014-11-09 18:50:16 +0200777static struct sk_buff *
778ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
779 const u8 *peer, u8 action_code,
780 u8 dialog_token, u16 status_code,
781 bool initiator, const u8 *extra_ies,
782 size_t extra_ies_len, u8 oper_class,
783 struct cfg80211_chan_def *chandef)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300784{
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300785 struct ieee80211_local *local = sdata->local;
Arik Nemtsovc2733902014-11-09 18:50:16 +0200786 struct sk_buff *skb;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300787 int ret;
788
Arik Nemtsovc2733902014-11-09 18:50:16 +0200789 skb = netdev_alloc_skb(sdata->dev,
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200790 local->hw.extra_tx_headroom +
791 max(sizeof(struct ieee80211_mgmt),
792 sizeof(struct ieee80211_tdls_data)) +
793 50 + /* supported rates */
794 7 + /* ext capab */
795 26 + /* max(WMM-info, WMM-param) */
796 2 + max(sizeof(struct ieee80211_ht_cap),
797 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200798 2 + max(sizeof(struct ieee80211_vht_cap),
799 sizeof(struct ieee80211_vht_operation)) +
Arik Nemtsovf0d29cb2014-11-09 18:50:12 +0200800 50 + /* supported channels */
Arik Nemtsov2cedd872014-11-09 18:50:13 +0200801 3 + /* 40/20 BSS coex */
Arik Nemtsovfb28ec02015-03-01 09:10:02 +0200802 4 + /* AID */
Arik Nemtsova38700d2015-03-18 08:46:08 +0200803 4 + /* oper classes */
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200804 extra_ies_len +
805 sizeof(struct ieee80211_tdls_lnkie));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300806 if (!skb)
Arik Nemtsovc2733902014-11-09 18:50:16 +0200807 return NULL;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300808
809 skb_reserve(skb, local->hw.extra_tx_headroom);
810
811 switch (action_code) {
812 case WLAN_TDLS_SETUP_REQUEST:
813 case WLAN_TDLS_SETUP_RESPONSE:
814 case WLAN_TDLS_SETUP_CONFIRM:
815 case WLAN_TDLS_TEARDOWN:
816 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200817 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200818 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200819 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
820 sdata->dev, peer,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300821 action_code, dialog_token,
822 status_code, skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300823 break;
824 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsovc2733902014-11-09 18:50:16 +0200825 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
826 peer, action_code,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300827 dialog_token, status_code,
828 skb);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300829 break;
830 default:
831 ret = -ENOTSUPP;
832 break;
833 }
834
835 if (ret < 0)
836 goto fail;
837
Arik Nemtsovc2733902014-11-09 18:50:16 +0200838 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
839 initiator, extra_ies, extra_ies_len, oper_class,
840 chandef);
841 return skb;
842
843fail:
844 dev_kfree_skb(skb);
845 return NULL;
846}
847
848static int
849ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
850 const u8 *peer, u8 action_code, u8 dialog_token,
851 u16 status_code, u32 peer_capability,
852 bool initiator, const u8 *extra_ies,
853 size_t extra_ies_len, u8 oper_class,
854 struct cfg80211_chan_def *chandef)
855{
856 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
857 struct sk_buff *skb = NULL;
858 struct sta_info *sta;
859 u32 flags = 0;
860 int ret = 0;
861
Arik Nemtsov626911c2014-07-17 17:14:17 +0300862 rcu_read_lock();
863 sta = sta_info_get(sdata, peer);
864
865 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300866 switch (action_code) {
867 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov8b941482014-10-22 12:32:48 +0300868 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300869 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300870 sta->sta.tdls_initiator = false;
871 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300872 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300873 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300874 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300875 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300876 break;
877 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300878 /*
879 * In some testing scenarios, we send a request and response.
880 * Make the last packet sent take effect for the initiator
881 * value.
882 */
Arik Nemtsov8b941482014-10-22 12:32:48 +0300883 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300884 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300885 sta->sta.tdls_initiator = true;
886 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300887 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300888 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300889 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300890 break;
891 case WLAN_TDLS_TEARDOWN:
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +0200892 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +0200893 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300894 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300895 break;
896 default:
897 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300898 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300899 }
900
Arik Nemtsov46792a22014-07-17 17:14:19 +0300901 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
902 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300903
Arik Nemtsov626911c2014-07-17 17:14:17 +0300904 rcu_read_unlock();
905 if (ret < 0)
906 goto fail;
907
Arik Nemtsovc2733902014-11-09 18:50:16 +0200908 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
909 dialog_token, status_code,
910 initiator, extra_ies,
911 extra_ies_len, oper_class,
912 chandef);
913 if (!skb) {
914 ret = -EINVAL;
915 goto fail;
916 }
917
918 if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300919 ieee80211_tx_skb(sdata, skb);
920 return 0;
921 }
922
923 /*
924 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
925 * we should default to AC_VI.
926 */
927 switch (action_code) {
928 case WLAN_TDLS_SETUP_REQUEST:
929 case WLAN_TDLS_SETUP_RESPONSE:
930 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
931 skb->priority = 2;
932 break;
933 default:
934 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
935 skb->priority = 5;
936 break;
937 }
938
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200939 /*
940 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
941 * Later, if no ACK is returned from peer, we will re-send the teardown
942 * packet through the AP.
943 */
944 if ((action_code == WLAN_TDLS_TEARDOWN) &&
Arik Nemtsovc2733902014-11-09 18:50:16 +0200945 (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200946 bool try_resend; /* Should we keep skb for possible resend */
947
948 /* If not sending directly to peer - no point in keeping skb */
949 rcu_read_lock();
950 sta = sta_info_get(sdata, peer);
951 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
952 rcu_read_unlock();
953
954 spin_lock_bh(&sdata->u.mgd.teardown_lock);
955 if (try_resend && !sdata->u.mgd.teardown_skb) {
956 /* Mark it as requiring TX status callback */
957 flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
958 IEEE80211_TX_INTFL_MLME_CONN_TX;
959
960 /*
961 * skb is copied since mac80211 will later set
962 * properties that might not be the same as the AP,
963 * such as encryption, QoS, addresses, etc.
964 *
965 * No problem if skb_copy() fails, so no need to check.
966 */
967 sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
968 sdata->u.mgd.orig_teardown_skb = skb;
969 }
970 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
971 }
972
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300973 /* disable bottom halves when entering the Tx path */
974 local_bh_disable();
Liad Kaufman1277b4a2014-11-09 18:50:08 +0200975 __ieee80211_subif_start_xmit(skb, dev, flags);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300976 local_bh_enable();
977
978 return ret;
979
980fail:
981 dev_kfree_skb(skb);
982 return ret;
983}
984
Arik Nemtsov191dd462014-06-11 17:18:23 +0300985static int
986ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
987 const u8 *peer, u8 action_code, u8 dialog_token,
988 u16 status_code, u32 peer_capability, bool initiator,
989 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300990{
991 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
992 struct ieee80211_local *local = sdata->local;
993 int ret;
994
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300995 mutex_lock(&local->mtx);
996
997 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300998 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
999 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001000 ret = -EBUSY;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001001 goto out_unlock;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001002 }
1003
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001004 /*
1005 * make sure we have a STA representing the peer so we drop or buffer
1006 * non-TDLS-setup frames to the peer. We can't send other packets
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001007 * during setup through the AP path.
1008 * Allow error packets to be sent - sometimes we don't even add a STA
1009 * before failing the setup.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001010 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001011 if (status_code == 0) {
1012 rcu_read_lock();
1013 if (!sta_info_get(sdata, peer)) {
1014 rcu_read_unlock();
1015 ret = -ENOLINK;
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001016 goto out_unlock;
Arik Nemtsov6ae32e52014-07-17 17:14:18 +03001017 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001018 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001019 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +03001020
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001021 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001022 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1023 mutex_unlock(&local->mtx);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001024
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001025 /* we cannot take the mutex while preparing the setup packet */
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001026 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1027 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +03001028 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001029 extra_ies, extra_ies_len, 0,
1030 NULL);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001031 if (ret < 0) {
1032 mutex_lock(&local->mtx);
1033 eth_zero_addr(sdata->u.mgd.tdls_peer);
1034 mutex_unlock(&local->mtx);
1035 return ret;
1036 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001037
Arik Nemtsov191dd462014-06-11 17:18:23 +03001038 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001039 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +03001040 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001041 return 0;
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001042
Arik Nemtsovae2e9fb2015-03-01 09:10:09 +02001043out_unlock:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001044 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001045 return ret;
1046}
1047
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001048static int
1049ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1050 const u8 *peer, u8 action_code, u8 dialog_token,
1051 u16 status_code, u32 peer_capability,
1052 bool initiator, const u8 *extra_ies,
1053 size_t extra_ies_len)
1054{
1055 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1056 struct ieee80211_local *local = sdata->local;
1057 struct sta_info *sta;
1058 int ret;
1059
1060 /*
1061 * No packets can be transmitted to the peer via the AP during setup -
1062 * the STA is set as a TDLS peer, but is not authorized.
1063 * During teardown, we prevent direct transmissions by stopping the
1064 * queues and flushing all direct packets.
1065 */
1066 ieee80211_stop_vif_queues(local, sdata,
1067 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001068 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001069
1070 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1071 dialog_token, status_code,
1072 peer_capability, initiator,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001073 extra_ies, extra_ies_len, 0,
1074 NULL);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001075 if (ret < 0)
1076 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1077 ret);
1078
1079 /*
1080 * Remove the STA AUTH flag to force further traffic through the AP. If
1081 * the STA was unreachable, it was already removed.
1082 */
1083 rcu_read_lock();
1084 sta = sta_info_get(sdata, peer);
1085 if (sta)
1086 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1087 rcu_read_unlock();
1088
1089 ieee80211_wake_vif_queues(local, sdata,
1090 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1091
1092 return 0;
1093}
1094
Arik Nemtsov191dd462014-06-11 17:18:23 +03001095int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1096 const u8 *peer, u8 action_code, u8 dialog_token,
1097 u16 status_code, u32 peer_capability,
1098 bool initiator, const u8 *extra_ies,
1099 size_t extra_ies_len)
1100{
1101 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1102 int ret;
1103
1104 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1105 return -ENOTSUPP;
1106
1107 /* make sure we are in managed mode, and associated */
1108 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1109 !sdata->u.mgd.associated)
1110 return -EINVAL;
1111
1112 switch (action_code) {
1113 case WLAN_TDLS_SETUP_REQUEST:
1114 case WLAN_TDLS_SETUP_RESPONSE:
1115 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1116 dialog_token, status_code,
1117 peer_capability, initiator,
1118 extra_ies, extra_ies_len);
1119 break;
1120 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001121 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1122 action_code, dialog_token,
1123 status_code,
1124 peer_capability, initiator,
1125 extra_ies, extra_ies_len);
1126 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +03001127 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +03001128 /*
1129 * Protect the discovery so we can hear the TDLS discovery
1130 * response frame. It is transmitted directly and not buffered
1131 * by the AP.
1132 */
1133 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1134 /* fall-through */
1135 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +03001136 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1137 /* no special handling */
1138 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1139 action_code,
1140 dialog_token,
1141 status_code,
1142 peer_capability,
1143 initiator, extra_ies,
Arik Nemtsovc2733902014-11-09 18:50:16 +02001144 extra_ies_len, 0, NULL);
Arik Nemtsov191dd462014-06-11 17:18:23 +03001145 break;
1146 default:
1147 ret = -EOPNOTSUPP;
1148 break;
1149 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001150
1151 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1152 action_code, peer, ret);
1153 return ret;
1154}
1155
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001156int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +02001157 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001158{
1159 struct sta_info *sta;
1160 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001161 struct ieee80211_local *local = sdata->local;
1162 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001163
1164 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1165 return -ENOTSUPP;
1166
1167 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1168 return -EINVAL;
1169
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001170 switch (oper) {
1171 case NL80211_TDLS_ENABLE_LINK:
1172 case NL80211_TDLS_DISABLE_LINK:
1173 break;
1174 case NL80211_TDLS_TEARDOWN:
1175 case NL80211_TDLS_SETUP:
1176 case NL80211_TDLS_DISCOVERY_REQ:
1177 /* We don't support in-driver setup/teardown/discovery */
1178 return -ENOTSUPP;
1179 }
1180
1181 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001182 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1183
1184 switch (oper) {
1185 case NL80211_TDLS_ENABLE_LINK:
1186 rcu_read_lock();
1187 sta = sta_info_get(sdata, peer);
1188 if (!sta) {
1189 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001190 ret = -ENOLINK;
1191 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001192 }
1193
1194 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1195 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001196
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001197 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1198 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001199 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001200 break;
1201 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +03001202 /*
1203 * The teardown message in ieee80211_tdls_mgmt_teardown() was
1204 * created while the queues were stopped, so it might still be
1205 * pending. Before flushing the queues we need to be sure the
1206 * message is handled by the tasklet handling pending messages,
1207 * otherwise we might start destroying the station before
1208 * sending the teardown packet.
1209 * Note that this only forces the tasklet to flush pendings -
1210 * not to stop the tasklet from rescheduling itself.
1211 */
1212 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001213 /* flush a potentially queued teardown packet */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +02001214 ieee80211_flush_queues(local, sdata, false);
Arik Nemtsovdb67d662014-06-11 17:18:24 +03001215
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001216 ret = sta_info_destroy_addr(sdata, peer);
1217 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001218 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001219 ret = -ENOTSUPP;
1220 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001221 }
1222
Arik Nemtsov81dd2b82014-07-17 17:14:25 +03001223 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1224 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1225 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +03001226 }
1227
1228 mutex_unlock(&local->mtx);
1229 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +03001230}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +03001231
1232void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1233 enum nl80211_tdls_operation oper,
1234 u16 reason_code, gfp_t gfp)
1235{
1236 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1237
1238 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1239 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1240 oper);
1241 return;
1242 }
1243
1244 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1245}
1246EXPORT_SYMBOL(ieee80211_tdls_oper_request);
Arik Nemtsova7a6bdd2014-11-09 18:50:19 +02001247
1248static void
1249iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1250{
1251 struct ieee80211_ch_switch_timing *ch_sw;
1252
1253 *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1254 *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1255
1256 ch_sw = (void *)buf;
1257 ch_sw->switch_time = cpu_to_le16(switch_time);
1258 ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1259}
1260
1261/* find switch timing IE in SKB ready for Tx */
1262static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1263{
1264 struct ieee80211_tdls_data *tf;
1265 const u8 *ie_start;
1266
1267 /*
1268 * Get the offset for the new location of the switch timing IE.
1269 * The SKB network header will now point to the "payload_type"
1270 * element of the TDLS data frame struct.
1271 */
1272 tf = container_of(skb->data + skb_network_offset(skb),
1273 struct ieee80211_tdls_data, payload_type);
1274 ie_start = tf->u.chan_switch_req.variable;
1275 return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1276 skb->len - (ie_start - skb->data));
1277}
1278
1279static struct sk_buff *
1280ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1281 struct cfg80211_chan_def *chandef,
1282 u32 *ch_sw_tm_ie_offset)
1283{
1284 struct ieee80211_sub_if_data *sdata = sta->sdata;
1285 u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1286 2 + sizeof(struct ieee80211_ch_switch_timing)];
1287 int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1288 u8 *pos = extra_ies;
1289 struct sk_buff *skb;
1290
1291 /*
1292 * if chandef points to a wide channel add a Secondary-Channel
1293 * Offset information element
1294 */
1295 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1296 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1297 bool ht40plus;
1298
1299 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1300 *pos++ = sizeof(*sec_chan_ie);
1301 sec_chan_ie = (void *)pos;
1302
1303 ht40plus = cfg80211_get_chandef_type(chandef) ==
1304 NL80211_CHAN_HT40PLUS;
1305 sec_chan_ie->sec_chan_offs = ht40plus ?
1306 IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1307 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1308 pos += sizeof(*sec_chan_ie);
1309
1310 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1311 }
1312
1313 /* just set the values to 0, this is a template */
1314 iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1315
1316 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1317 WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1318 0, 0, !sta->sta.tdls_initiator,
1319 extra_ies, extra_ies_len,
1320 oper_class, chandef);
1321 if (!skb)
1322 return NULL;
1323
1324 skb = ieee80211_build_data_template(sdata, skb, 0);
1325 if (IS_ERR(skb)) {
1326 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1327 return NULL;
1328 }
1329
1330 if (ch_sw_tm_ie_offset) {
1331 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1332
1333 if (!tm_ie) {
1334 tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1335 dev_kfree_skb_any(skb);
1336 return NULL;
1337 }
1338
1339 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1340 }
1341
1342 tdls_dbg(sdata,
1343 "TDLS channel switch request template for %pM ch %d width %d\n",
1344 sta->sta.addr, chandef->chan->center_freq, chandef->width);
1345 return skb;
1346}
1347
1348int
1349ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1350 const u8 *addr, u8 oper_class,
1351 struct cfg80211_chan_def *chandef)
1352{
1353 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1354 struct ieee80211_local *local = sdata->local;
1355 struct sta_info *sta;
1356 struct sk_buff *skb = NULL;
1357 u32 ch_sw_tm_ie;
1358 int ret;
1359
1360 mutex_lock(&local->sta_mtx);
1361 sta = sta_info_get(sdata, addr);
1362 if (!sta) {
1363 tdls_dbg(sdata,
1364 "Invalid TDLS peer %pM for channel switch request\n",
1365 addr);
1366 ret = -ENOENT;
1367 goto out;
1368 }
1369
1370 if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1371 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1372 addr);
1373 ret = -ENOTSUPP;
1374 goto out;
1375 }
1376
1377 skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1378 &ch_sw_tm_ie);
1379 if (!skb) {
1380 ret = -ENOENT;
1381 goto out;
1382 }
1383
1384 ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1385 chandef, skb, ch_sw_tm_ie);
1386 if (!ret)
1387 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1388
1389out:
1390 mutex_unlock(&local->sta_mtx);
1391 dev_kfree_skb_any(skb);
1392 return ret;
1393}
1394
1395void
1396ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1397 struct net_device *dev,
1398 const u8 *addr)
1399{
1400 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1401 struct ieee80211_local *local = sdata->local;
1402 struct sta_info *sta;
1403
1404 mutex_lock(&local->sta_mtx);
1405 sta = sta_info_get(sdata, addr);
1406 if (!sta) {
1407 tdls_dbg(sdata,
1408 "Invalid TDLS peer %pM for channel switch cancel\n",
1409 addr);
1410 goto out;
1411 }
1412
1413 if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1414 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1415 addr);
1416 goto out;
1417 }
1418
1419 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1420 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1421
1422out:
1423 mutex_unlock(&local->sta_mtx);
1424}
Arik Nemtsov8a4d32f2014-11-09 18:50:20 +02001425
1426static struct sk_buff *
1427ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1428 u32 *ch_sw_tm_ie_offset)
1429{
1430 struct ieee80211_sub_if_data *sdata = sta->sdata;
1431 struct sk_buff *skb;
1432 u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1433
1434 /* initial timing are always zero in the template */
1435 iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1436
1437 skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1438 WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1439 0, 0, !sta->sta.tdls_initiator,
1440 extra_ies, sizeof(extra_ies), 0, NULL);
1441 if (!skb)
1442 return NULL;
1443
1444 skb = ieee80211_build_data_template(sdata, skb, 0);
1445 if (IS_ERR(skb)) {
1446 tdls_dbg(sdata,
1447 "Failed building TDLS channel switch resp frame\n");
1448 return NULL;
1449 }
1450
1451 if (ch_sw_tm_ie_offset) {
1452 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1453
1454 if (!tm_ie) {
1455 tdls_dbg(sdata,
1456 "No switch timing IE in TDLS switch resp\n");
1457 dev_kfree_skb_any(skb);
1458 return NULL;
1459 }
1460
1461 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1462 }
1463
1464 tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1465 sta->sta.addr);
1466 return skb;
1467}
1468
1469static int
1470ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1471 struct sk_buff *skb)
1472{
1473 struct ieee80211_local *local = sdata->local;
1474 struct ieee802_11_elems elems;
1475 struct sta_info *sta;
1476 struct ieee80211_tdls_data *tf = (void *)skb->data;
1477 bool local_initiator;
1478 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1479 int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1480 struct ieee80211_tdls_ch_sw_params params = {};
1481 int ret;
1482
1483 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1484 params.timestamp = rx_status->device_timestamp;
1485
1486 if (skb->len < baselen) {
1487 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1488 skb->len);
1489 return -EINVAL;
1490 }
1491
1492 mutex_lock(&local->sta_mtx);
1493 sta = sta_info_get(sdata, tf->sa);
1494 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1495 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1496 tf->sa);
1497 ret = -EINVAL;
1498 goto out;
1499 }
1500
1501 params.sta = &sta->sta;
1502 params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1503 if (params.status != 0) {
1504 ret = 0;
1505 goto call_drv;
1506 }
1507
1508 ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1509 skb->len - baselen, false, &elems);
1510 if (elems.parse_error) {
1511 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1512 ret = -EINVAL;
1513 goto out;
1514 }
1515
1516 if (!elems.ch_sw_timing || !elems.lnk_id) {
1517 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1518 ret = -EINVAL;
1519 goto out;
1520 }
1521
1522 /* validate the initiator is set correctly */
1523 local_initiator =
1524 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1525 if (local_initiator == sta->sta.tdls_initiator) {
1526 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1527 ret = -EINVAL;
1528 goto out;
1529 }
1530
1531 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1532 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1533
1534 params.tmpl_skb =
1535 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1536 if (!params.tmpl_skb) {
1537 ret = -ENOENT;
1538 goto out;
1539 }
1540
1541call_drv:
1542 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1543
1544 tdls_dbg(sdata,
1545 "TDLS channel switch response received from %pM status %d\n",
1546 tf->sa, params.status);
1547
1548out:
1549 mutex_unlock(&local->sta_mtx);
1550 dev_kfree_skb_any(params.tmpl_skb);
1551 return ret;
1552}
1553
1554static int
1555ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1556 struct sk_buff *skb)
1557{
1558 struct ieee80211_local *local = sdata->local;
1559 struct ieee802_11_elems elems;
1560 struct cfg80211_chan_def chandef;
1561 struct ieee80211_channel *chan;
1562 enum nl80211_channel_type chan_type;
1563 int freq;
1564 u8 target_channel, oper_class;
1565 bool local_initiator;
1566 struct sta_info *sta;
1567 enum ieee80211_band band;
1568 struct ieee80211_tdls_data *tf = (void *)skb->data;
1569 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1570 int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1571 struct ieee80211_tdls_ch_sw_params params = {};
1572 int ret = 0;
1573
1574 params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1575 params.timestamp = rx_status->device_timestamp;
1576
1577 if (skb->len < baselen) {
1578 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1579 skb->len);
1580 return -EINVAL;
1581 }
1582
1583 target_channel = tf->u.chan_switch_req.target_channel;
1584 oper_class = tf->u.chan_switch_req.oper_class;
1585
1586 /*
1587 * We can't easily infer the channel band. The operating class is
1588 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1589 * solution here is to treat channels with number >14 as 5GHz ones,
1590 * and specifically check for the (oper_class, channel) combinations
1591 * where this doesn't hold. These are thankfully unique according to
1592 * IEEE802.11-2012.
1593 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1594 * valid here.
1595 */
1596 if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1597 oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1598 target_channel < 14)
1599 band = IEEE80211_BAND_5GHZ;
1600 else
1601 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1602 IEEE80211_BAND_5GHZ;
1603
1604 freq = ieee80211_channel_to_frequency(target_channel, band);
1605 if (freq == 0) {
1606 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1607 target_channel);
1608 return -EINVAL;
1609 }
1610
1611 chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1612 if (!chan) {
1613 tdls_dbg(sdata,
1614 "Unsupported channel for TDLS chan switch: %d\n",
1615 target_channel);
1616 return -EINVAL;
1617 }
1618
1619 ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1620 skb->len - baselen, false, &elems);
1621 if (elems.parse_error) {
1622 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1623 return -EINVAL;
1624 }
1625
1626 if (!elems.ch_sw_timing || !elems.lnk_id) {
1627 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1628 return -EINVAL;
1629 }
1630
1631 mutex_lock(&local->sta_mtx);
1632 sta = sta_info_get(sdata, tf->sa);
1633 if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1634 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1635 tf->sa);
1636 ret = -EINVAL;
1637 goto out;
1638 }
1639
1640 params.sta = &sta->sta;
1641
1642 /* validate the initiator is set correctly */
1643 local_initiator =
1644 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1645 if (local_initiator == sta->sta.tdls_initiator) {
1646 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1647 ret = -EINVAL;
1648 goto out;
1649 }
1650
1651 if (!sta->sta.ht_cap.ht_supported) {
1652 chan_type = NL80211_CHAN_NO_HT;
1653 } else if (!elems.sec_chan_offs) {
1654 chan_type = NL80211_CHAN_HT20;
1655 } else {
1656 switch (elems.sec_chan_offs->sec_chan_offs) {
1657 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1658 chan_type = NL80211_CHAN_HT40PLUS;
1659 break;
1660 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1661 chan_type = NL80211_CHAN_HT40MINUS;
1662 break;
1663 default:
1664 chan_type = NL80211_CHAN_HT20;
1665 break;
1666 }
1667 }
1668
1669 cfg80211_chandef_create(&chandef, chan, chan_type);
1670 params.chandef = &chandef;
1671
1672 params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1673 params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1674
1675 params.tmpl_skb =
1676 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1677 &params.ch_sw_tm_ie);
1678 if (!params.tmpl_skb) {
1679 ret = -ENOENT;
1680 goto out;
1681 }
1682
1683 drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1684
1685 tdls_dbg(sdata,
1686 "TDLS ch switch request received from %pM ch %d width %d\n",
1687 tf->sa, params.chandef->chan->center_freq,
1688 params.chandef->width);
1689out:
1690 mutex_unlock(&local->sta_mtx);
1691 dev_kfree_skb_any(params.tmpl_skb);
1692 return ret;
1693}
1694
1695void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1696 struct sk_buff *skb)
1697{
1698 struct ieee80211_tdls_data *tf = (void *)skb->data;
1699 struct wiphy *wiphy = sdata->local->hw.wiphy;
1700
1701 /* make sure the driver supports it */
1702 if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1703 return;
1704
1705 /* we want to access the entire packet */
1706 if (skb_linearize(skb))
1707 return;
1708 /*
1709 * The packet/size was already validated by mac80211 Rx path, only look
1710 * at the action type.
1711 */
1712 switch (tf->action_code) {
1713 case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1714 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1715 break;
1716 case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1717 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1718 break;
1719 default:
1720 WARN_ON_ONCE(1);
1721 return;
1722 }
1723}