blob: b4f368e2cb3bb7624925157c7e4b5d6610c95ca8 [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 Nemtsov95224fe2014-05-01 10:17:28 +030038static void ieee80211_tdls_add_ext_capab(struct sk_buff *skb)
39{
40 u8 *pos = (void *)skb_put(skb, 7);
41
42 *pos++ = WLAN_EID_EXT_CAPABILITY;
43 *pos++ = 5; /* len */
44 *pos++ = 0x0;
45 *pos++ = 0x0;
46 *pos++ = 0x0;
47 *pos++ = 0x0;
48 *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
49}
50
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +030051static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
52 u16 status_code)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030053{
54 struct ieee80211_local *local = sdata->local;
55 u16 capab;
56
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +030057 /* The capability will be 0 when sending a failure code */
58 if (status_code != 0)
59 return 0;
60
Arik Nemtsov95224fe2014-05-01 10:17:28 +030061 capab = 0;
62 if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
63 return capab;
64
65 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
66 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
67 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
68 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
69
70 return capab;
71}
72
Arik Nemtsov1606ef42014-07-17 17:14:21 +030073static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
74 struct sk_buff *skb, const u8 *peer,
75 bool initiator)
Arik Nemtsov95224fe2014-05-01 10:17:28 +030076{
77 struct ieee80211_tdls_lnkie *lnkid;
Arik Nemtsov1606ef42014-07-17 17:14:21 +030078 const u8 *init_addr, *rsp_addr;
79
80 if (initiator) {
81 init_addr = sdata->vif.addr;
82 rsp_addr = peer;
83 } else {
84 init_addr = peer;
85 rsp_addr = sdata->vif.addr;
86 }
Arik Nemtsov95224fe2014-05-01 10:17:28 +030087
88 lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
89
90 lnkid->ie_type = WLAN_EID_LINK_ID;
91 lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
92
Arik Nemtsov1606ef42014-07-17 17:14:21 +030093 memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
94 memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
95 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
Arik Nemtsov95224fe2014-05-01 10:17:28 +030096}
97
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +030098/* translate numbering in the WMM parameter IE to the mac80211 notation */
99static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
100{
101 switch (ac) {
102 default:
103 WARN_ON_ONCE(1);
104 case 0:
105 return IEEE80211_AC_BE;
106 case 1:
107 return IEEE80211_AC_BK;
108 case 2:
109 return IEEE80211_AC_VI;
110 case 3:
111 return IEEE80211_AC_VO;
112 }
113}
114
115static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
116{
117 u8 ret;
118
119 ret = aifsn & 0x0f;
120 if (acm)
121 ret |= 0x10;
122 ret |= (aci << 5) & 0x60;
123 return ret;
124}
125
126static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
127{
128 return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
129 ((ilog2(cw_max + 1) << 0x4) & 0xf0);
130}
131
132static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
133 struct sk_buff *skb)
134{
135 struct ieee80211_wmm_param_ie *wmm;
136 struct ieee80211_tx_queue_params *txq;
137 int i;
138
139 wmm = (void *)skb_put(skb, sizeof(*wmm));
140 memset(wmm, 0, sizeof(*wmm));
141
142 wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
143 wmm->len = sizeof(*wmm) - 2;
144
145 wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
146 wmm->oui[1] = 0x50;
147 wmm->oui[2] = 0xf2;
148 wmm->oui_type = 2; /* WME */
149 wmm->oui_subtype = 1; /* WME param */
150 wmm->version = 1; /* WME ver */
151 wmm->qos_info = 0; /* U-APSD not in use */
152
153 /*
154 * Use the EDCA parameters defined for the BSS, or default if the AP
155 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
156 */
157 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
158 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
159 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
160 txq->acm, i);
161 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
162 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
163 }
164}
165
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300166static void
167ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
168 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300169 u8 action_code, bool initiator,
170 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300171{
172 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300173 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300174 struct ieee80211_supported_band *sband;
175 struct ieee80211_sta_ht_cap ht_cap;
176 struct sta_info *sta = NULL;
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300177 size_t offset = 0, noffset;
178 u8 *pos;
179
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300180 rcu_read_lock();
181
182 /* we should have the peer STA if we're already responding */
183 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
184 sta = sta_info_get(sdata, peer);
185 if (WARN_ON_ONCE(!sta)) {
186 rcu_read_unlock();
187 return;
188 }
189 }
190
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300191 ieee80211_add_srates_ie(sdata, skb, false, band);
192 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
193
194 /* add any custom IEs that go before Extended Capabilities */
195 if (extra_ies_len) {
196 static const u8 before_ext_cap[] = {
197 WLAN_EID_SUPP_RATES,
198 WLAN_EID_COUNTRY,
199 WLAN_EID_EXT_SUPP_RATES,
200 WLAN_EID_SUPPORTED_CHANNELS,
201 WLAN_EID_RSN,
202 };
203 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
204 before_ext_cap,
205 ARRAY_SIZE(before_ext_cap),
206 offset);
207 pos = skb_put(skb, noffset - offset);
208 memcpy(pos, extra_ies + offset, noffset - offset);
209 offset = noffset;
210 }
211
212 ieee80211_tdls_add_ext_capab(skb);
213
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300214 /* add the QoS element if we support it */
215 if (local->hw.queues >= IEEE80211_NUM_ACS &&
216 action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
217 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
218
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300219 /* add any custom IEs that go before HT capabilities */
220 if (extra_ies_len) {
221 static const u8 before_ht_cap[] = {
222 WLAN_EID_SUPP_RATES,
223 WLAN_EID_COUNTRY,
224 WLAN_EID_EXT_SUPP_RATES,
225 WLAN_EID_SUPPORTED_CHANNELS,
226 WLAN_EID_RSN,
227 WLAN_EID_EXT_CAPABILITY,
228 WLAN_EID_QOS_CAPA,
229 WLAN_EID_FAST_BSS_TRANSITION,
230 WLAN_EID_TIMEOUT_INTERVAL,
231 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
232 };
233 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
234 before_ht_cap,
235 ARRAY_SIZE(before_ht_cap),
236 offset);
237 pos = skb_put(skb, noffset - offset);
238 memcpy(pos, extra_ies + offset, noffset - offset);
239 offset = noffset;
240 }
241
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300242 /*
243 * with TDLS we can switch channels, and HT-caps are not necessarily
244 * the same on all bands. The specification limits the setup to a
245 * single HT-cap, so use the current band for now.
246 */
247 sband = local->hw.wiphy->bands[band];
248 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
249 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
250 action_code == WLAN_TDLS_SETUP_RESPONSE) &&
251 ht_cap.ht_supported && (!sta || sta->sta.ht_cap.ht_supported)) {
252 if (action_code == WLAN_TDLS_SETUP_REQUEST) {
253 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
254
255 /* disable SMPS in TDLS initiator */
256 ht_cap.cap |= (WLAN_HT_CAP_SM_PS_DISABLED
257 << IEEE80211_HT_CAP_SM_PS_SHIFT);
258 } else {
259 /* disable SMPS in TDLS responder */
260 sta->sta.ht_cap.cap |=
261 (WLAN_HT_CAP_SM_PS_DISABLED
262 << IEEE80211_HT_CAP_SM_PS_SHIFT);
263
264 /* the peer caps are already intersected with our own */
265 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
266 }
267
268 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
269 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
270 }
271
272 rcu_read_unlock();
273
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300274 /* add any remaining IEs */
275 if (extra_ies_len) {
276 noffset = extra_ies_len;
277 pos = skb_put(skb, noffset - offset);
278 memcpy(pos, extra_ies + offset, noffset - offset);
279 }
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300280
281 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300282}
283
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300284static void
285ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
286 struct sk_buff *skb, const u8 *peer,
287 bool initiator, const u8 *extra_ies,
288 size_t extra_ies_len)
289{
290 struct ieee80211_local *local = sdata->local;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300291 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300292 size_t offset = 0, noffset;
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300293 struct sta_info *sta, *ap_sta;
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300294 u8 *pos;
295
296 rcu_read_lock();
297
298 sta = sta_info_get(sdata, peer);
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300299 ap_sta = sta_info_get(sdata, ifmgd->bssid);
300 if (WARN_ON_ONCE(!sta || !ap_sta)) {
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300301 rcu_read_unlock();
302 return;
303 }
304
305 /* add any custom IEs that go before the QoS IE */
306 if (extra_ies_len) {
307 static const u8 before_qos[] = {
308 WLAN_EID_RSN,
309 };
310 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
311 before_qos,
312 ARRAY_SIZE(before_qos),
313 offset);
314 pos = skb_put(skb, noffset - offset);
315 memcpy(pos, extra_ies + offset, noffset - offset);
316 offset = noffset;
317 }
318
319 /* add the QoS param IE if both the peer and we support it */
Johannes Berga74a8c82014-07-22 14:50:47 +0200320 if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300321 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
322
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300323 /* add any custom IEs that go before HT operation */
324 if (extra_ies_len) {
325 static const u8 before_ht_op[] = {
326 WLAN_EID_RSN,
327 WLAN_EID_QOS_CAPA,
328 WLAN_EID_FAST_BSS_TRANSITION,
329 WLAN_EID_TIMEOUT_INTERVAL,
330 };
331 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
332 before_ht_op,
333 ARRAY_SIZE(before_ht_op),
334 offset);
335 pos = skb_put(skb, noffset - offset);
336 memcpy(pos, extra_ies + offset, noffset - offset);
337 offset = noffset;
338 }
339
340 /* if HT support is only added in TDLS, we need an HT-operation IE */
341 if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
342 struct ieee80211_chanctx_conf *chanctx_conf =
343 rcu_dereference(sdata->vif.chanctx_conf);
344 if (!WARN_ON(!chanctx_conf)) {
345 pos = skb_put(skb, 2 +
346 sizeof(struct ieee80211_ht_operation));
347 /* send an empty HT operation IE */
348 ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
349 &chanctx_conf->def, 0);
350 }
351 }
352
353 rcu_read_unlock();
354
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300355 /* add any remaining IEs */
356 if (extra_ies_len) {
357 noffset = extra_ies_len;
358 pos = skb_put(skb, noffset - offset);
359 memcpy(pos, extra_ies + offset, noffset - offset);
360 }
361
362 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300363}
364
Arik Nemtsov46792a22014-07-17 17:14:19 +0300365static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
366 struct sk_buff *skb, const u8 *peer,
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300367 u8 action_code, u16 status_code,
368 bool initiator, const u8 *extra_ies,
369 size_t extra_ies_len)
Arik Nemtsov46792a22014-07-17 17:14:19 +0300370{
Arik Nemtsov46792a22014-07-17 17:14:19 +0300371 switch (action_code) {
372 case WLAN_TDLS_SETUP_REQUEST:
373 case WLAN_TDLS_SETUP_RESPONSE:
374 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300375 if (status_code == 0)
376 ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
377 action_code,
378 initiator,
379 extra_ies,
380 extra_ies_len);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300381 break;
382 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov6f7eaa42014-07-17 17:14:24 +0300383 if (status_code == 0)
384 ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
385 initiator, extra_ies,
386 extra_ies_len);
387 break;
Arik Nemtsov46792a22014-07-17 17:14:19 +0300388 case WLAN_TDLS_TEARDOWN:
389 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovf09a87d2014-07-17 17:14:20 +0300390 if (extra_ies_len)
391 memcpy(skb_put(skb, extra_ies_len), extra_ies,
392 extra_ies_len);
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300393 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
394 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
Arik Nemtsov46792a22014-07-17 17:14:19 +0300395 break;
396 }
397
Arik Nemtsov46792a22014-07-17 17:14:19 +0300398}
399
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300400static int
401ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200402 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300403 u16 status_code, struct sk_buff *skb)
404{
405 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300406 struct ieee80211_tdls_data *tf;
407
408 tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
409
410 memcpy(tf->da, peer, ETH_ALEN);
411 memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
412 tf->ether_type = cpu_to_be16(ETH_P_TDLS);
413 tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
414
Arik Nemtsov59cd85c2014-09-09 17:11:02 +0300415 /* network header is after the ethernet header */
416 skb_set_network_header(skb, ETH_HLEN);
417
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300418 switch (action_code) {
419 case WLAN_TDLS_SETUP_REQUEST:
420 tf->category = WLAN_CATEGORY_TDLS;
421 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
422
423 skb_put(skb, sizeof(tf->u.setup_req));
424 tf->u.setup_req.dialog_token = dialog_token;
425 tf->u.setup_req.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300426 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
427 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300428 break;
429 case WLAN_TDLS_SETUP_RESPONSE:
430 tf->category = WLAN_CATEGORY_TDLS;
431 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
432
433 skb_put(skb, sizeof(tf->u.setup_resp));
434 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
435 tf->u.setup_resp.dialog_token = dialog_token;
436 tf->u.setup_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300437 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
438 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300439 break;
440 case WLAN_TDLS_SETUP_CONFIRM:
441 tf->category = WLAN_CATEGORY_TDLS;
442 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
443
444 skb_put(skb, sizeof(tf->u.setup_cfm));
445 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
446 tf->u.setup_cfm.dialog_token = dialog_token;
447 break;
448 case WLAN_TDLS_TEARDOWN:
449 tf->category = WLAN_CATEGORY_TDLS;
450 tf->action_code = WLAN_TDLS_TEARDOWN;
451
452 skb_put(skb, sizeof(tf->u.teardown));
453 tf->u.teardown.reason_code = cpu_to_le16(status_code);
454 break;
455 case WLAN_TDLS_DISCOVERY_REQUEST:
456 tf->category = WLAN_CATEGORY_TDLS;
457 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
458
459 skb_put(skb, sizeof(tf->u.discover_req));
460 tf->u.discover_req.dialog_token = dialog_token;
461 break;
462 default:
463 return -EINVAL;
464 }
465
466 return 0;
467}
468
469static int
470ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200471 const u8 *peer, u8 action_code, u8 dialog_token,
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300472 u16 status_code, struct sk_buff *skb)
473{
474 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300475 struct ieee80211_mgmt *mgmt;
476
477 mgmt = (void *)skb_put(skb, 24);
478 memset(mgmt, 0, 24);
479 memcpy(mgmt->da, peer, ETH_ALEN);
480 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
481 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
482
483 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
484 IEEE80211_STYPE_ACTION);
485
486 switch (action_code) {
487 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
488 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
489 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
490 mgmt->u.action.u.tdls_discover_resp.action_code =
491 WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
492 mgmt->u.action.u.tdls_discover_resp.dialog_token =
493 dialog_token;
494 mgmt->u.action.u.tdls_discover_resp.capability =
Arik Nemtsovdd8c0b02014-07-17 17:14:22 +0300495 cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
496 status_code));
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300497 break;
498 default:
499 return -EINVAL;
500 }
501
502 return 0;
503}
504
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300505static int
506ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
507 const u8 *peer, u8 action_code,
508 u8 dialog_token, u16 status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300509 u32 peer_capability, bool initiator,
510 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300511{
512 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
513 struct ieee80211_local *local = sdata->local;
514 struct sk_buff *skb = NULL;
515 bool send_direct;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300516 struct sta_info *sta;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300517 int ret;
518
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300519 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
520 max(sizeof(struct ieee80211_mgmt),
521 sizeof(struct ieee80211_tdls_data)) +
522 50 + /* supported rates */
523 7 + /* ext capab */
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300524 26 + /* max(WMM-info, WMM-param) */
Arik Nemtsov13cc8a42014-07-17 17:14:26 +0300525 2 + max(sizeof(struct ieee80211_ht_cap),
526 sizeof(struct ieee80211_ht_operation)) +
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300527 extra_ies_len +
528 sizeof(struct ieee80211_tdls_lnkie));
529 if (!skb)
530 return -ENOMEM;
531
532 skb_reserve(skb, local->hw.extra_tx_headroom);
533
534 switch (action_code) {
535 case WLAN_TDLS_SETUP_REQUEST:
536 case WLAN_TDLS_SETUP_RESPONSE:
537 case WLAN_TDLS_SETUP_CONFIRM:
538 case WLAN_TDLS_TEARDOWN:
539 case WLAN_TDLS_DISCOVERY_REQUEST:
540 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
541 action_code, dialog_token,
542 status_code, skb);
543 send_direct = false;
544 break;
545 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
546 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
547 dialog_token, status_code,
548 skb);
549 send_direct = true;
550 break;
551 default:
552 ret = -ENOTSUPP;
553 break;
554 }
555
556 if (ret < 0)
557 goto fail;
558
Arik Nemtsov626911c2014-07-17 17:14:17 +0300559 rcu_read_lock();
560 sta = sta_info_get(sdata, peer);
561
562 /* infer the initiator if we can, to support old userspace */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300563 switch (action_code) {
564 case WLAN_TDLS_SETUP_REQUEST:
Arik Nemtsov8b941482014-10-22 12:32:48 +0300565 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300566 set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300567 sta->sta.tdls_initiator = false;
568 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300569 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300570 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300571 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300572 initiator = true;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300573 break;
574 case WLAN_TDLS_SETUP_RESPONSE:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300575 /*
576 * In some testing scenarios, we send a request and response.
577 * Make the last packet sent take effect for the initiator
578 * value.
579 */
Arik Nemtsov8b941482014-10-22 12:32:48 +0300580 if (sta) {
Arik Nemtsov626911c2014-07-17 17:14:17 +0300581 clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
Arik Nemtsov8b941482014-10-22 12:32:48 +0300582 sta->sta.tdls_initiator = true;
583 }
Arik Nemtsov626911c2014-07-17 17:14:17 +0300584 /* fall-through */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300585 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
Arik Nemtsov626911c2014-07-17 17:14:17 +0300586 initiator = false;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300587 break;
588 case WLAN_TDLS_TEARDOWN:
589 /* any value is ok */
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300590 break;
591 default:
592 ret = -ENOTSUPP;
Arik Nemtsov626911c2014-07-17 17:14:17 +0300593 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300594 }
595
Arik Nemtsov46792a22014-07-17 17:14:19 +0300596 if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
597 initiator = true;
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300598
Arik Nemtsov626911c2014-07-17 17:14:17 +0300599 rcu_read_unlock();
600 if (ret < 0)
601 goto fail;
602
Arik Nemtsov1606ef42014-07-17 17:14:21 +0300603 ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
604 initiator, extra_ies, extra_ies_len);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300605 if (send_direct) {
606 ieee80211_tx_skb(sdata, skb);
607 return 0;
608 }
609
610 /*
611 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
612 * we should default to AC_VI.
613 */
614 switch (action_code) {
615 case WLAN_TDLS_SETUP_REQUEST:
616 case WLAN_TDLS_SETUP_RESPONSE:
617 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
618 skb->priority = 2;
619 break;
620 default:
621 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
622 skb->priority = 5;
623 break;
624 }
625
626 /* disable bottom halves when entering the Tx path */
627 local_bh_disable();
628 ret = ieee80211_subif_start_xmit(skb, dev);
629 local_bh_enable();
630
631 return ret;
632
633fail:
634 dev_kfree_skb(skb);
635 return ret;
636}
637
Arik Nemtsov191dd462014-06-11 17:18:23 +0300638static int
639ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
640 const u8 *peer, u8 action_code, u8 dialog_token,
641 u16 status_code, u32 peer_capability, bool initiator,
642 const u8 *extra_ies, size_t extra_ies_len)
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300643{
644 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
645 struct ieee80211_local *local = sdata->local;
646 int ret;
647
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300648 mutex_lock(&local->mtx);
649
650 /* we don't support concurrent TDLS peer setups */
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300651 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
652 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300653 ret = -EBUSY;
654 goto exit;
655 }
656
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300657 /*
658 * make sure we have a STA representing the peer so we drop or buffer
659 * non-TDLS-setup frames to the peer. We can't send other packets
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300660 * during setup through the AP path.
661 * Allow error packets to be sent - sometimes we don't even add a STA
662 * before failing the setup.
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300663 */
Arik Nemtsov6ae32e52014-07-17 17:14:18 +0300664 if (status_code == 0) {
665 rcu_read_lock();
666 if (!sta_info_get(sdata, peer)) {
667 rcu_read_unlock();
668 ret = -ENOLINK;
669 goto exit;
670 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300671 rcu_read_unlock();
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300672 }
Arik Nemtsov7adc3e42014-06-11 17:18:26 +0300673
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300674 ieee80211_flush_queues(local, sdata);
675
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300676 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
677 dialog_token, status_code,
Arik Nemtsov2fb6b9b2014-06-11 17:18:22 +0300678 peer_capability, initiator,
679 extra_ies, extra_ies_len);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300680 if (ret < 0)
681 goto exit;
682
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300683 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
Arik Nemtsov191dd462014-06-11 17:18:23 +0300684 ieee80211_queue_delayed_work(&sdata->local->hw,
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300685 &sdata->u.mgd.tdls_peer_del_work,
Arik Nemtsov191dd462014-06-11 17:18:23 +0300686 TDLS_PEER_SETUP_TIMEOUT);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300687
688exit:
689 mutex_unlock(&local->mtx);
Arik Nemtsov191dd462014-06-11 17:18:23 +0300690 return ret;
691}
692
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300693static int
694ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
695 const u8 *peer, u8 action_code, u8 dialog_token,
696 u16 status_code, u32 peer_capability,
697 bool initiator, const u8 *extra_ies,
698 size_t extra_ies_len)
699{
700 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
701 struct ieee80211_local *local = sdata->local;
702 struct sta_info *sta;
703 int ret;
704
705 /*
706 * No packets can be transmitted to the peer via the AP during setup -
707 * the STA is set as a TDLS peer, but is not authorized.
708 * During teardown, we prevent direct transmissions by stopping the
709 * queues and flushing all direct packets.
710 */
711 ieee80211_stop_vif_queues(local, sdata,
712 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
713 ieee80211_flush_queues(local, sdata);
714
715 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
716 dialog_token, status_code,
717 peer_capability, initiator,
718 extra_ies, extra_ies_len);
719 if (ret < 0)
720 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
721 ret);
722
723 /*
724 * Remove the STA AUTH flag to force further traffic through the AP. If
725 * the STA was unreachable, it was already removed.
726 */
727 rcu_read_lock();
728 sta = sta_info_get(sdata, peer);
729 if (sta)
730 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
731 rcu_read_unlock();
732
733 ieee80211_wake_vif_queues(local, sdata,
734 IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
735
736 return 0;
737}
738
Arik Nemtsov191dd462014-06-11 17:18:23 +0300739int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
740 const u8 *peer, u8 action_code, u8 dialog_token,
741 u16 status_code, u32 peer_capability,
742 bool initiator, const u8 *extra_ies,
743 size_t extra_ies_len)
744{
745 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
746 int ret;
747
748 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
749 return -ENOTSUPP;
750
751 /* make sure we are in managed mode, and associated */
752 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
753 !sdata->u.mgd.associated)
754 return -EINVAL;
755
756 switch (action_code) {
757 case WLAN_TDLS_SETUP_REQUEST:
758 case WLAN_TDLS_SETUP_RESPONSE:
759 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
760 dialog_token, status_code,
761 peer_capability, initiator,
762 extra_ies, extra_ies_len);
763 break;
764 case WLAN_TDLS_TEARDOWN:
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300765 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
766 action_code, dialog_token,
767 status_code,
768 peer_capability, initiator,
769 extra_ies, extra_ies_len);
770 break;
Arik Nemtsov191dd462014-06-11 17:18:23 +0300771 case WLAN_TDLS_DISCOVERY_REQUEST:
Arik Nemtsovee10f2c2014-06-11 17:18:27 +0300772 /*
773 * Protect the discovery so we can hear the TDLS discovery
774 * response frame. It is transmitted directly and not buffered
775 * by the AP.
776 */
777 drv_mgd_protect_tdls_discover(sdata->local, sdata);
778 /* fall-through */
779 case WLAN_TDLS_SETUP_CONFIRM:
Arik Nemtsov191dd462014-06-11 17:18:23 +0300780 case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
781 /* no special handling */
782 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
783 action_code,
784 dialog_token,
785 status_code,
786 peer_capability,
787 initiator, extra_ies,
788 extra_ies_len);
789 break;
790 default:
791 ret = -EOPNOTSUPP;
792 break;
793 }
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300794
795 tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
796 action_code, peer, ret);
797 return ret;
798}
799
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300800int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
Johannes Berg3b3a0162014-05-19 17:19:31 +0200801 const u8 *peer, enum nl80211_tdls_operation oper)
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300802{
803 struct sta_info *sta;
804 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300805 struct ieee80211_local *local = sdata->local;
806 int ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300807
808 if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
809 return -ENOTSUPP;
810
811 if (sdata->vif.type != NL80211_IFTYPE_STATION)
812 return -EINVAL;
813
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300814 switch (oper) {
815 case NL80211_TDLS_ENABLE_LINK:
816 case NL80211_TDLS_DISABLE_LINK:
817 break;
818 case NL80211_TDLS_TEARDOWN:
819 case NL80211_TDLS_SETUP:
820 case NL80211_TDLS_DISCOVERY_REQ:
821 /* We don't support in-driver setup/teardown/discovery */
822 return -ENOTSUPP;
823 }
824
825 mutex_lock(&local->mtx);
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300826 tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
827
828 switch (oper) {
829 case NL80211_TDLS_ENABLE_LINK:
830 rcu_read_lock();
831 sta = sta_info_get(sdata, peer);
832 if (!sta) {
833 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300834 ret = -ENOLINK;
835 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300836 }
837
838 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
839 rcu_read_unlock();
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300840
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300841 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
842 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300843 ret = 0;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300844 break;
845 case NL80211_TDLS_DISABLE_LINK:
Liad Kaufmanbb3f8482014-07-17 17:14:31 +0300846 /*
847 * The teardown message in ieee80211_tdls_mgmt_teardown() was
848 * created while the queues were stopped, so it might still be
849 * pending. Before flushing the queues we need to be sure the
850 * message is handled by the tasklet handling pending messages,
851 * otherwise we might start destroying the station before
852 * sending the teardown packet.
853 * Note that this only forces the tasklet to flush pendings -
854 * not to stop the tasklet from rescheduling itself.
855 */
856 tasklet_kill(&local->tx_pending_tasklet);
Arik Nemtsovdb67d662014-06-11 17:18:24 +0300857 /* flush a potentially queued teardown packet */
858 ieee80211_flush_queues(local, sdata);
859
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300860 ret = sta_info_destroy_addr(sdata, peer);
861 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300862 default:
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300863 ret = -ENOTSUPP;
864 break;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300865 }
866
Arik Nemtsov81dd2b82014-07-17 17:14:25 +0300867 if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
868 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
869 eth_zero_addr(sdata->u.mgd.tdls_peer);
Arik Nemtsov17e6a592014-06-11 17:18:20 +0300870 }
871
872 mutex_unlock(&local->mtx);
873 return ret;
Arik Nemtsov95224fe2014-05-01 10:17:28 +0300874}
Arik Nemtsovc887f0d32014-06-11 17:18:25 +0300875
876void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
877 enum nl80211_tdls_operation oper,
878 u16 reason_code, gfp_t gfp)
879{
880 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
881
882 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
883 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
884 oper);
885 return;
886 }
887
888 cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
889}
890EXPORT_SYMBOL(ieee80211_tdls_oper_request);