8fb314b182e20752d61e1fcd325c5921ccdcce3b
[cascardo/linux.git] / net / mac80211 / tdls.c
1 /*
2  * mac80211 TDLS handling code
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2014, Intel Corporation
6  * Copyright 2014  Intel Mobile Communications GmbH
7  *
8  * This file is GPLv2 as found in COPYING.
9  */
10
11 #include <linux/ieee80211.h>
12 #include <linux/log2.h>
13 #include <net/cfg80211.h>
14 #include "ieee80211_i.h"
15 #include "driver-ops.h"
16
17 /* give usermode some time for retries in setting up the TDLS session */
18 #define TDLS_PEER_SETUP_TIMEOUT (15 * HZ)
19
20 void 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,
26                              u.mgd.tdls_peer_del_work.work);
27         local = sdata->local;
28
29         mutex_lock(&local->mtx);
30         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);
34         }
35         mutex_unlock(&local->mtx);
36 }
37
38 static 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
51 static u8
52 ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
53                            struct sk_buff *skb, u16 start, u16 end,
54                            u16 spacing)
55 {
56         u8 subband_cnt = 0, ch_cnt = 0;
57         struct ieee80211_channel *ch;
58         struct cfg80211_chan_def chandef;
59         int i, subband_start;
60
61         for (i = start; i <= end; i += spacing) {
62                 if (!ch_cnt)
63                         subband_start = i;
64
65                 ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
66                 if (ch) {
67                         /* we will be active on the channel */
68                         u32 flags = IEEE80211_CHAN_DISABLED |
69                                     IEEE80211_CHAN_NO_IR;
70                         cfg80211_chandef_create(&chandef, ch,
71                                                 NL80211_CHAN_HT20);
72                         if (cfg80211_chandef_usable(sdata->local->hw.wiphy,
73                                                     &chandef, flags)) {
74                                 ch_cnt++;
75                                 continue;
76                         }
77                 }
78
79                 if (ch_cnt) {
80                         u8 *pos = skb_put(skb, 2);
81                         *pos++ = ieee80211_frequency_to_channel(subband_start);
82                         *pos++ = ch_cnt;
83
84                         subband_cnt++;
85                         ch_cnt = 0;
86                 }
87         }
88
89         return subband_cnt;
90 }
91
92 static void
93 ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
94                                  struct sk_buff *skb)
95 {
96         /*
97          * Add possible channels for TDLS. These are channels that are allowed
98          * to be active.
99          */
100         u8 subband_cnt;
101         u8 *pos = skb_put(skb, 2);
102
103         *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
104
105         /*
106          * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
107          * this doesn't happen in real world scenarios.
108          */
109
110         /* 2GHz, with 5MHz spacing */
111         subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
112
113         /* 5GHz, with 20MHz spacing */
114         subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
115
116         /* length */
117         *pos = 2 * subband_cnt;
118 }
119
120 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
121                                         u16 status_code)
122 {
123         struct ieee80211_local *local = sdata->local;
124         u16 capab;
125
126         /* The capability will be 0 when sending a failure code */
127         if (status_code != 0)
128                 return 0;
129
130         capab = 0;
131         if (ieee80211_get_sdata_band(sdata) != IEEE80211_BAND_2GHZ)
132                 return capab;
133
134         if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
135                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
136         if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
137                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
138
139         return capab;
140 }
141
142 static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
143                                        struct sk_buff *skb, const u8 *peer,
144                                        bool initiator)
145 {
146         struct ieee80211_tdls_lnkie *lnkid;
147         const u8 *init_addr, *rsp_addr;
148
149         if (initiator) {
150                 init_addr = sdata->vif.addr;
151                 rsp_addr = peer;
152         } else {
153                 init_addr = peer;
154                 rsp_addr = sdata->vif.addr;
155         }
156
157         lnkid = (void *)skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
158
159         lnkid->ie_type = WLAN_EID_LINK_ID;
160         lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
161
162         memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
163         memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
164         memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
165 }
166
167 /* translate numbering in the WMM parameter IE to the mac80211 notation */
168 static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
169 {
170         switch (ac) {
171         default:
172                 WARN_ON_ONCE(1);
173         case 0:
174                 return IEEE80211_AC_BE;
175         case 1:
176                 return IEEE80211_AC_BK;
177         case 2:
178                 return IEEE80211_AC_VI;
179         case 3:
180                 return IEEE80211_AC_VO;
181         }
182 }
183
184 static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
185 {
186         u8 ret;
187
188         ret = aifsn & 0x0f;
189         if (acm)
190                 ret |= 0x10;
191         ret |= (aci << 5) & 0x60;
192         return ret;
193 }
194
195 static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
196 {
197         return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
198                ((ilog2(cw_max + 1) << 0x4) & 0xf0);
199 }
200
201 static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
202                                             struct sk_buff *skb)
203 {
204         struct ieee80211_wmm_param_ie *wmm;
205         struct ieee80211_tx_queue_params *txq;
206         int i;
207
208         wmm = (void *)skb_put(skb, sizeof(*wmm));
209         memset(wmm, 0, sizeof(*wmm));
210
211         wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
212         wmm->len = sizeof(*wmm) - 2;
213
214         wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
215         wmm->oui[1] = 0x50;
216         wmm->oui[2] = 0xf2;
217         wmm->oui_type = 2; /* WME */
218         wmm->oui_subtype = 1; /* WME param */
219         wmm->version = 1; /* WME ver */
220         wmm->qos_info = 0; /* U-APSD not in use */
221
222         /*
223          * Use the EDCA parameters defined for the BSS, or default if the AP
224          * doesn't support it, as mandated by 802.11-2012 section 10.22.4
225          */
226         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
227                 txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
228                 wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
229                                                                txq->acm, i);
230                 wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
231                 wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
232         }
233 }
234
235 static void
236 ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
237                                    struct sk_buff *skb, const u8 *peer,
238                                    u8 action_code, bool initiator,
239                                    const u8 *extra_ies, size_t extra_ies_len)
240 {
241         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
242         struct ieee80211_local *local = sdata->local;
243         struct ieee80211_supported_band *sband;
244         struct ieee80211_sta_ht_cap ht_cap;
245         struct sta_info *sta = NULL;
246         size_t offset = 0, noffset;
247         u8 *pos;
248
249         rcu_read_lock();
250
251         /* we should have the peer STA if we're already responding */
252         if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
253                 sta = sta_info_get(sdata, peer);
254                 if (WARN_ON_ONCE(!sta)) {
255                         rcu_read_unlock();
256                         return;
257                 }
258         }
259
260         ieee80211_add_srates_ie(sdata, skb, false, band);
261         ieee80211_add_ext_srates_ie(sdata, skb, false, band);
262         ieee80211_tdls_add_supp_channels(sdata, skb);
263
264         /* add any custom IEs that go before Extended Capabilities */
265         if (extra_ies_len) {
266                 static const u8 before_ext_cap[] = {
267                         WLAN_EID_SUPP_RATES,
268                         WLAN_EID_COUNTRY,
269                         WLAN_EID_EXT_SUPP_RATES,
270                         WLAN_EID_SUPPORTED_CHANNELS,
271                         WLAN_EID_RSN,
272                 };
273                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
274                                              before_ext_cap,
275                                              ARRAY_SIZE(before_ext_cap),
276                                              offset);
277                 pos = skb_put(skb, noffset - offset);
278                 memcpy(pos, extra_ies + offset, noffset - offset);
279                 offset = noffset;
280         }
281
282         ieee80211_tdls_add_ext_capab(skb);
283
284         /* add the QoS element if we support it */
285         if (local->hw.queues >= IEEE80211_NUM_ACS &&
286             action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
287                 ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
288
289         /* add any custom IEs that go before HT capabilities */
290         if (extra_ies_len) {
291                 static const u8 before_ht_cap[] = {
292                         WLAN_EID_SUPP_RATES,
293                         WLAN_EID_COUNTRY,
294                         WLAN_EID_EXT_SUPP_RATES,
295                         WLAN_EID_SUPPORTED_CHANNELS,
296                         WLAN_EID_RSN,
297                         WLAN_EID_EXT_CAPABILITY,
298                         WLAN_EID_QOS_CAPA,
299                         WLAN_EID_FAST_BSS_TRANSITION,
300                         WLAN_EID_TIMEOUT_INTERVAL,
301                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
302                 };
303                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
304                                              before_ht_cap,
305                                              ARRAY_SIZE(before_ht_cap),
306                                              offset);
307                 pos = skb_put(skb, noffset - offset);
308                 memcpy(pos, extra_ies + offset, noffset - offset);
309                 offset = noffset;
310         }
311
312         /*
313          * with TDLS we can switch channels, and HT-caps are not necessarily
314          * the same on all bands. The specification limits the setup to a
315          * single HT-cap, so use the current band for now.
316          */
317         sband = local->hw.wiphy->bands[band];
318         memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
319         if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
320              action_code == WLAN_TDLS_SETUP_RESPONSE) &&
321             ht_cap.ht_supported && (!sta || sta->sta.ht_cap.ht_supported)) {
322                 if (action_code == WLAN_TDLS_SETUP_REQUEST) {
323                         ieee80211_apply_htcap_overrides(sdata, &ht_cap);
324
325                         /* disable SMPS in TDLS initiator */
326                         ht_cap.cap |= (WLAN_HT_CAP_SM_PS_DISABLED
327                                        << IEEE80211_HT_CAP_SM_PS_SHIFT);
328                 } else {
329                         /* disable SMPS in TDLS responder */
330                         sta->sta.ht_cap.cap |=
331                                 (WLAN_HT_CAP_SM_PS_DISABLED
332                                  << IEEE80211_HT_CAP_SM_PS_SHIFT);
333
334                         /* the peer caps are already intersected with our own */
335                         memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
336                 }
337
338                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
339                 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
340         }
341
342         rcu_read_unlock();
343
344         /* add any remaining IEs */
345         if (extra_ies_len) {
346                 noffset = extra_ies_len;
347                 pos = skb_put(skb, noffset - offset);
348                 memcpy(pos, extra_ies + offset, noffset - offset);
349         }
350
351         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
352 }
353
354 static void
355 ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
356                                  struct sk_buff *skb, const u8 *peer,
357                                  bool initiator, const u8 *extra_ies,
358                                  size_t extra_ies_len)
359 {
360         struct ieee80211_local *local = sdata->local;
361         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
362         size_t offset = 0, noffset;
363         struct sta_info *sta, *ap_sta;
364         u8 *pos;
365
366         rcu_read_lock();
367
368         sta = sta_info_get(sdata, peer);
369         ap_sta = sta_info_get(sdata, ifmgd->bssid);
370         if (WARN_ON_ONCE(!sta || !ap_sta)) {
371                 rcu_read_unlock();
372                 return;
373         }
374
375         /* add any custom IEs that go before the QoS IE */
376         if (extra_ies_len) {
377                 static const u8 before_qos[] = {
378                         WLAN_EID_RSN,
379                 };
380                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
381                                              before_qos,
382                                              ARRAY_SIZE(before_qos),
383                                              offset);
384                 pos = skb_put(skb, noffset - offset);
385                 memcpy(pos, extra_ies + offset, noffset - offset);
386                 offset = noffset;
387         }
388
389         /* add the QoS param IE if both the peer and we support it */
390         if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
391                 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
392
393         /* add any custom IEs that go before HT operation */
394         if (extra_ies_len) {
395                 static const u8 before_ht_op[] = {
396                         WLAN_EID_RSN,
397                         WLAN_EID_QOS_CAPA,
398                         WLAN_EID_FAST_BSS_TRANSITION,
399                         WLAN_EID_TIMEOUT_INTERVAL,
400                 };
401                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
402                                              before_ht_op,
403                                              ARRAY_SIZE(before_ht_op),
404                                              offset);
405                 pos = skb_put(skb, noffset - offset);
406                 memcpy(pos, extra_ies + offset, noffset - offset);
407                 offset = noffset;
408         }
409
410         /* if HT support is only added in TDLS, we need an HT-operation IE */
411         if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
412                 struct ieee80211_chanctx_conf *chanctx_conf =
413                                 rcu_dereference(sdata->vif.chanctx_conf);
414                 if (!WARN_ON(!chanctx_conf)) {
415                         pos = skb_put(skb, 2 +
416                                       sizeof(struct ieee80211_ht_operation));
417                         /* send an empty HT operation IE */
418                         ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
419                                                    &chanctx_conf->def, 0);
420                 }
421         }
422
423         rcu_read_unlock();
424
425         /* add any remaining IEs */
426         if (extra_ies_len) {
427                 noffset = extra_ies_len;
428                 pos = skb_put(skb, noffset - offset);
429                 memcpy(pos, extra_ies + offset, noffset - offset);
430         }
431
432         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
433 }
434
435 static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
436                                    struct sk_buff *skb, const u8 *peer,
437                                    u8 action_code, u16 status_code,
438                                    bool initiator, const u8 *extra_ies,
439                                    size_t extra_ies_len)
440 {
441         switch (action_code) {
442         case WLAN_TDLS_SETUP_REQUEST:
443         case WLAN_TDLS_SETUP_RESPONSE:
444         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
445                 if (status_code == 0)
446                         ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
447                                                            action_code,
448                                                            initiator,
449                                                            extra_ies,
450                                                            extra_ies_len);
451                 break;
452         case WLAN_TDLS_SETUP_CONFIRM:
453                 if (status_code == 0)
454                         ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
455                                                          initiator, extra_ies,
456                                                          extra_ies_len);
457                 break;
458         case WLAN_TDLS_TEARDOWN:
459         case WLAN_TDLS_DISCOVERY_REQUEST:
460                 if (extra_ies_len)
461                         memcpy(skb_put(skb, extra_ies_len), extra_ies,
462                                extra_ies_len);
463                 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
464                         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
465                 break;
466         }
467
468 }
469
470 static int
471 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
472                                const u8 *peer, u8 action_code, u8 dialog_token,
473                                u16 status_code, struct sk_buff *skb)
474 {
475         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
476         struct ieee80211_tdls_data *tf;
477
478         tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
479
480         memcpy(tf->da, peer, ETH_ALEN);
481         memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
482         tf->ether_type = cpu_to_be16(ETH_P_TDLS);
483         tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
484
485         /* network header is after the ethernet header */
486         skb_set_network_header(skb, ETH_HLEN);
487
488         switch (action_code) {
489         case WLAN_TDLS_SETUP_REQUEST:
490                 tf->category = WLAN_CATEGORY_TDLS;
491                 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
492
493                 skb_put(skb, sizeof(tf->u.setup_req));
494                 tf->u.setup_req.dialog_token = dialog_token;
495                 tf->u.setup_req.capability =
496                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
497                                                                  status_code));
498                 break;
499         case WLAN_TDLS_SETUP_RESPONSE:
500                 tf->category = WLAN_CATEGORY_TDLS;
501                 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
502
503                 skb_put(skb, sizeof(tf->u.setup_resp));
504                 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
505                 tf->u.setup_resp.dialog_token = dialog_token;
506                 tf->u.setup_resp.capability =
507                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
508                                                                  status_code));
509                 break;
510         case WLAN_TDLS_SETUP_CONFIRM:
511                 tf->category = WLAN_CATEGORY_TDLS;
512                 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
513
514                 skb_put(skb, sizeof(tf->u.setup_cfm));
515                 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
516                 tf->u.setup_cfm.dialog_token = dialog_token;
517                 break;
518         case WLAN_TDLS_TEARDOWN:
519                 tf->category = WLAN_CATEGORY_TDLS;
520                 tf->action_code = WLAN_TDLS_TEARDOWN;
521
522                 skb_put(skb, sizeof(tf->u.teardown));
523                 tf->u.teardown.reason_code = cpu_to_le16(status_code);
524                 break;
525         case WLAN_TDLS_DISCOVERY_REQUEST:
526                 tf->category = WLAN_CATEGORY_TDLS;
527                 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
528
529                 skb_put(skb, sizeof(tf->u.discover_req));
530                 tf->u.discover_req.dialog_token = dialog_token;
531                 break;
532         default:
533                 return -EINVAL;
534         }
535
536         return 0;
537 }
538
539 static int
540 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
541                            const u8 *peer, u8 action_code, u8 dialog_token,
542                            u16 status_code, struct sk_buff *skb)
543 {
544         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
545         struct ieee80211_mgmt *mgmt;
546
547         mgmt = (void *)skb_put(skb, 24);
548         memset(mgmt, 0, 24);
549         memcpy(mgmt->da, peer, ETH_ALEN);
550         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
551         memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
552
553         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
554                                           IEEE80211_STYPE_ACTION);
555
556         switch (action_code) {
557         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
558                 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
559                 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
560                 mgmt->u.action.u.tdls_discover_resp.action_code =
561                         WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
562                 mgmt->u.action.u.tdls_discover_resp.dialog_token =
563                         dialog_token;
564                 mgmt->u.action.u.tdls_discover_resp.capability =
565                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
566                                                                  status_code));
567                 break;
568         default:
569                 return -EINVAL;
570         }
571
572         return 0;
573 }
574
575 static int
576 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
577                                 const u8 *peer, u8 action_code,
578                                 u8 dialog_token, u16 status_code,
579                                 u32 peer_capability, bool initiator,
580                                 const u8 *extra_ies, size_t extra_ies_len)
581 {
582         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
583         struct ieee80211_local *local = sdata->local;
584         struct sk_buff *skb = NULL;
585         u32 flags = 0;
586         bool send_direct;
587         struct sta_info *sta;
588         int ret;
589
590         skb = netdev_alloc_skb(dev,
591                                local->hw.extra_tx_headroom +
592                                max(sizeof(struct ieee80211_mgmt),
593                                    sizeof(struct ieee80211_tdls_data)) +
594                                50 + /* supported rates */
595                                7 + /* ext capab */
596                                26 + /* max(WMM-info, WMM-param) */
597                                2 + max(sizeof(struct ieee80211_ht_cap),
598                                        sizeof(struct ieee80211_ht_operation)) +
599                                50 + /* supported channels */
600                                extra_ies_len +
601                                sizeof(struct ieee80211_tdls_lnkie));
602         if (!skb)
603                 return -ENOMEM;
604
605         skb_reserve(skb, local->hw.extra_tx_headroom);
606
607         switch (action_code) {
608         case WLAN_TDLS_SETUP_REQUEST:
609         case WLAN_TDLS_SETUP_RESPONSE:
610         case WLAN_TDLS_SETUP_CONFIRM:
611         case WLAN_TDLS_TEARDOWN:
612         case WLAN_TDLS_DISCOVERY_REQUEST:
613                 ret = ieee80211_prep_tdls_encap_data(wiphy, dev, peer,
614                                                      action_code, dialog_token,
615                                                      status_code, skb);
616                 send_direct = false;
617                 break;
618         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
619                 ret = ieee80211_prep_tdls_direct(wiphy, dev, peer, action_code,
620                                                  dialog_token, status_code,
621                                                  skb);
622                 send_direct = true;
623                 break;
624         default:
625                 ret = -ENOTSUPP;
626                 break;
627         }
628
629         if (ret < 0)
630                 goto fail;
631
632         rcu_read_lock();
633         sta = sta_info_get(sdata, peer);
634
635         /* infer the initiator if we can, to support old userspace */
636         switch (action_code) {
637         case WLAN_TDLS_SETUP_REQUEST:
638                 if (sta) {
639                         set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
640                         sta->sta.tdls_initiator = false;
641                 }
642                 /* fall-through */
643         case WLAN_TDLS_SETUP_CONFIRM:
644         case WLAN_TDLS_DISCOVERY_REQUEST:
645                 initiator = true;
646                 break;
647         case WLAN_TDLS_SETUP_RESPONSE:
648                 /*
649                  * In some testing scenarios, we send a request and response.
650                  * Make the last packet sent take effect for the initiator
651                  * value.
652                  */
653                 if (sta) {
654                         clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
655                         sta->sta.tdls_initiator = true;
656                 }
657                 /* fall-through */
658         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
659                 initiator = false;
660                 break;
661         case WLAN_TDLS_TEARDOWN:
662                 /* any value is ok */
663                 break;
664         default:
665                 ret = -ENOTSUPP;
666                 break;
667         }
668
669         if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
670                 initiator = true;
671
672         rcu_read_unlock();
673         if (ret < 0)
674                 goto fail;
675
676         ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
677                                initiator, extra_ies, extra_ies_len);
678         if (send_direct) {
679                 ieee80211_tx_skb(sdata, skb);
680                 return 0;
681         }
682
683         /*
684          * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
685          * we should default to AC_VI.
686          */
687         switch (action_code) {
688         case WLAN_TDLS_SETUP_REQUEST:
689         case WLAN_TDLS_SETUP_RESPONSE:
690                 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
691                 skb->priority = 2;
692                 break;
693         default:
694                 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
695                 skb->priority = 5;
696                 break;
697         }
698
699         /*
700          * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
701          * Later, if no ACK is returned from peer, we will re-send the teardown
702          * packet through the AP.
703          */
704         if ((action_code == WLAN_TDLS_TEARDOWN) &&
705             (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
706                 struct sta_info *sta = NULL;
707                 bool try_resend; /* Should we keep skb for possible resend */
708
709                 /* If not sending directly to peer - no point in keeping skb */
710                 rcu_read_lock();
711                 sta = sta_info_get(sdata, peer);
712                 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
713                 rcu_read_unlock();
714
715                 spin_lock_bh(&sdata->u.mgd.teardown_lock);
716                 if (try_resend && !sdata->u.mgd.teardown_skb) {
717                         /* Mark it as requiring TX status callback  */
718                         flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
719                                  IEEE80211_TX_INTFL_MLME_CONN_TX;
720
721                         /*
722                          * skb is copied since mac80211 will later set
723                          * properties that might not be the same as the AP,
724                          * such as encryption, QoS, addresses, etc.
725                          *
726                          * No problem if skb_copy() fails, so no need to check.
727                          */
728                         sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
729                         sdata->u.mgd.orig_teardown_skb = skb;
730                 }
731                 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
732         }
733
734         /* disable bottom halves when entering the Tx path */
735         local_bh_disable();
736         __ieee80211_subif_start_xmit(skb, dev, flags);
737         local_bh_enable();
738
739         return ret;
740
741 fail:
742         dev_kfree_skb(skb);
743         return ret;
744 }
745
746 static int
747 ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
748                           const u8 *peer, u8 action_code, u8 dialog_token,
749                           u16 status_code, u32 peer_capability, bool initiator,
750                           const u8 *extra_ies, size_t extra_ies_len)
751 {
752         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
753         struct ieee80211_local *local = sdata->local;
754         int ret;
755
756         mutex_lock(&local->mtx);
757
758         /* we don't support concurrent TDLS peer setups */
759         if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
760             !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
761                 ret = -EBUSY;
762                 goto exit;
763         }
764
765         /*
766          * make sure we have a STA representing the peer so we drop or buffer
767          * non-TDLS-setup frames to the peer. We can't send other packets
768          * during setup through the AP path.
769          * Allow error packets to be sent - sometimes we don't even add a STA
770          * before failing the setup.
771          */
772         if (status_code == 0) {
773                 rcu_read_lock();
774                 if (!sta_info_get(sdata, peer)) {
775                         rcu_read_unlock();
776                         ret = -ENOLINK;
777                         goto exit;
778                 }
779                 rcu_read_unlock();
780         }
781
782         ieee80211_flush_queues(local, sdata);
783
784         ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
785                                               dialog_token, status_code,
786                                               peer_capability, initiator,
787                                               extra_ies, extra_ies_len);
788         if (ret < 0)
789                 goto exit;
790
791         memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
792         ieee80211_queue_delayed_work(&sdata->local->hw,
793                                      &sdata->u.mgd.tdls_peer_del_work,
794                                      TDLS_PEER_SETUP_TIMEOUT);
795
796 exit:
797         mutex_unlock(&local->mtx);
798         return ret;
799 }
800
801 static int
802 ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
803                              const u8 *peer, u8 action_code, u8 dialog_token,
804                              u16 status_code, u32 peer_capability,
805                              bool initiator, const u8 *extra_ies,
806                              size_t extra_ies_len)
807 {
808         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
809         struct ieee80211_local *local = sdata->local;
810         struct sta_info *sta;
811         int ret;
812
813         /*
814          * No packets can be transmitted to the peer via the AP during setup -
815          * the STA is set as a TDLS peer, but is not authorized.
816          * During teardown, we prevent direct transmissions by stopping the
817          * queues and flushing all direct packets.
818          */
819         ieee80211_stop_vif_queues(local, sdata,
820                                   IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
821         ieee80211_flush_queues(local, sdata);
822
823         ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
824                                               dialog_token, status_code,
825                                               peer_capability, initiator,
826                                               extra_ies, extra_ies_len);
827         if (ret < 0)
828                 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
829                           ret);
830
831         /*
832          * Remove the STA AUTH flag to force further traffic through the AP. If
833          * the STA was unreachable, it was already removed.
834          */
835         rcu_read_lock();
836         sta = sta_info_get(sdata, peer);
837         if (sta)
838                 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
839         rcu_read_unlock();
840
841         ieee80211_wake_vif_queues(local, sdata,
842                                   IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
843
844         return 0;
845 }
846
847 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
848                         const u8 *peer, u8 action_code, u8 dialog_token,
849                         u16 status_code, u32 peer_capability,
850                         bool initiator, const u8 *extra_ies,
851                         size_t extra_ies_len)
852 {
853         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
854         int ret;
855
856         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
857                 return -ENOTSUPP;
858
859         /* make sure we are in managed mode, and associated */
860         if (sdata->vif.type != NL80211_IFTYPE_STATION ||
861             !sdata->u.mgd.associated)
862                 return -EINVAL;
863
864         switch (action_code) {
865         case WLAN_TDLS_SETUP_REQUEST:
866         case WLAN_TDLS_SETUP_RESPONSE:
867                 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
868                                                 dialog_token, status_code,
869                                                 peer_capability, initiator,
870                                                 extra_ies, extra_ies_len);
871                 break;
872         case WLAN_TDLS_TEARDOWN:
873                 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
874                                                    action_code, dialog_token,
875                                                    status_code,
876                                                    peer_capability, initiator,
877                                                    extra_ies, extra_ies_len);
878                 break;
879         case WLAN_TDLS_DISCOVERY_REQUEST:
880                 /*
881                  * Protect the discovery so we can hear the TDLS discovery
882                  * response frame. It is transmitted directly and not buffered
883                  * by the AP.
884                  */
885                 drv_mgd_protect_tdls_discover(sdata->local, sdata);
886                 /* fall-through */
887         case WLAN_TDLS_SETUP_CONFIRM:
888         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
889                 /* no special handling */
890                 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
891                                                       action_code,
892                                                       dialog_token,
893                                                       status_code,
894                                                       peer_capability,
895                                                       initiator, extra_ies,
896                                                       extra_ies_len);
897                 break;
898         default:
899                 ret = -EOPNOTSUPP;
900                 break;
901         }
902
903         tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
904                  action_code, peer, ret);
905         return ret;
906 }
907
908 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
909                         const u8 *peer, enum nl80211_tdls_operation oper)
910 {
911         struct sta_info *sta;
912         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
913         struct ieee80211_local *local = sdata->local;
914         int ret;
915
916         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
917                 return -ENOTSUPP;
918
919         if (sdata->vif.type != NL80211_IFTYPE_STATION)
920                 return -EINVAL;
921
922         switch (oper) {
923         case NL80211_TDLS_ENABLE_LINK:
924         case NL80211_TDLS_DISABLE_LINK:
925                 break;
926         case NL80211_TDLS_TEARDOWN:
927         case NL80211_TDLS_SETUP:
928         case NL80211_TDLS_DISCOVERY_REQ:
929                 /* We don't support in-driver setup/teardown/discovery */
930                 return -ENOTSUPP;
931         }
932
933         mutex_lock(&local->mtx);
934         tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
935
936         switch (oper) {
937         case NL80211_TDLS_ENABLE_LINK:
938                 rcu_read_lock();
939                 sta = sta_info_get(sdata, peer);
940                 if (!sta) {
941                         rcu_read_unlock();
942                         ret = -ENOLINK;
943                         break;
944                 }
945
946                 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
947                 rcu_read_unlock();
948
949                 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
950                              !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
951                 ret = 0;
952                 break;
953         case NL80211_TDLS_DISABLE_LINK:
954                 /*
955                  * The teardown message in ieee80211_tdls_mgmt_teardown() was
956                  * created while the queues were stopped, so it might still be
957                  * pending. Before flushing the queues we need to be sure the
958                  * message is handled by the tasklet handling pending messages,
959                  * otherwise we might start destroying the station before
960                  * sending the teardown packet.
961                  * Note that this only forces the tasklet to flush pendings -
962                  * not to stop the tasklet from rescheduling itself.
963                  */
964                 tasklet_kill(&local->tx_pending_tasklet);
965                 /* flush a potentially queued teardown packet */
966                 ieee80211_flush_queues(local, sdata);
967
968                 ret = sta_info_destroy_addr(sdata, peer);
969                 break;
970         default:
971                 ret = -ENOTSUPP;
972                 break;
973         }
974
975         if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
976                 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
977                 eth_zero_addr(sdata->u.mgd.tdls_peer);
978         }
979
980         mutex_unlock(&local->mtx);
981         return ret;
982 }
983
984 void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
985                                  enum nl80211_tdls_operation oper,
986                                  u16 reason_code, gfp_t gfp)
987 {
988         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
989
990         if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
991                 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
992                           oper);
993                 return;
994         }
995
996         cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
997 }
998 EXPORT_SYMBOL(ieee80211_tdls_oper_request);