cfg/mac80211: add regulatory classes IE during TDLS setup
[firefly-linux-kernel-4.4.55.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 ieee80211_local *local,
39                                          struct sk_buff *skb)
40 {
41         u8 *pos = (void *)skb_put(skb, 7);
42         bool chan_switch = local->hw.wiphy->features &
43                            NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
44
45         *pos++ = WLAN_EID_EXT_CAPABILITY;
46         *pos++ = 5; /* len */
47         *pos++ = 0x0;
48         *pos++ = 0x0;
49         *pos++ = 0x0;
50         *pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
51         *pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
52 }
53
54 static u8
55 ieee80211_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 */
71                         cfg80211_chandef_create(&chandef, ch,
72                                                 NL80211_CHAN_NO_HT);
73                         if (cfg80211_reg_can_beacon(sdata->local->hw.wiphy,
74                                                     &chandef,
75                                                     sdata->wdev.iftype)) {
76                                 ch_cnt++;
77                                 /*
78                                  * check if the next channel is also part of
79                                  * this allowed range
80                                  */
81                                 continue;
82                         }
83                 }
84
85                 /*
86                  * we've reached the end of a range, with allowed channels
87                  * found
88                  */
89                 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
99         /* 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
108         return subband_cnt;
109 }
110
111 static void
112 ieee80211_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
139 static 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
157 static 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
167 static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
168                                         u16 status_code)
169 {
170         struct ieee80211_local *local = sdata->local;
171         u16 capab;
172
173         /* The capability will be 0 when sending a failure code */
174         if (status_code != 0)
175                 return 0;
176
177         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
189 static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
190                                        struct sk_buff *skb, const u8 *peer,
191                                        bool initiator)
192 {
193         struct ieee80211_tdls_lnkie *lnkid;
194         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         }
203
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
209         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);
212 }
213
214 static void
215 ieee80211_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
225 /* translate numbering in the WMM parameter IE to the mac80211 notation */
226 static 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
242 static 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
253 static 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
259 static 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
293 static void
294 ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
295                                    struct sk_buff *skb, const u8 *peer,
296                                    u8 action_code, bool initiator,
297                                    const u8 *extra_ies, size_t extra_ies_len)
298 {
299         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
300         struct ieee80211_local *local = sdata->local;
301         struct ieee80211_supported_band *sband;
302         struct ieee80211_sta_ht_cap ht_cap;
303         struct ieee80211_sta_vht_cap vht_cap;
304         struct sta_info *sta = NULL;
305         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);
310         ieee80211_tdls_add_supp_channels(sdata, skb);
311
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
330         ieee80211_tdls_add_ext_capab(local, skb);
331
332         /* 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
337         /* 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
360         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
371         ieee80211_tdls_add_oper_classes(sdata, skb);
372
373         /*
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));
380
381         if (action_code == WLAN_TDLS_SETUP_REQUEST && ht_cap.ht_supported) {
382                 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
383
384                 /* disable SMPS in TDLS initiator */
385                 ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
386                                 << IEEE80211_HT_CAP_SM_PS_SHIFT;
387
388                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
389                 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
390         } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
391                    ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
392                 /* disable SMPS in TDLS responder */
393                 sta->sta.ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
394                                         << IEEE80211_HT_CAP_SM_PS_SHIFT;
395
396                 /* the peer caps are already intersected with our own */
397                 memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
398
399                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
400                 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
401         }
402
403         if (ht_cap.ht_supported &&
404             (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
405                 ieee80211_tdls_add_bss_coex_ie(skb);
406
407         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
408
409         /* add any custom IEs that go before VHT capabilities */
410         if (extra_ies_len) {
411                 static const u8 before_vht_cap[] = {
412                         WLAN_EID_SUPP_RATES,
413                         WLAN_EID_COUNTRY,
414                         WLAN_EID_EXT_SUPP_RATES,
415                         WLAN_EID_SUPPORTED_CHANNELS,
416                         WLAN_EID_RSN,
417                         WLAN_EID_EXT_CAPABILITY,
418                         WLAN_EID_QOS_CAPA,
419                         WLAN_EID_FAST_BSS_TRANSITION,
420                         WLAN_EID_TIMEOUT_INTERVAL,
421                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
422                         WLAN_EID_MULTI_BAND,
423                 };
424                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
425                                              before_vht_cap,
426                                              ARRAY_SIZE(before_vht_cap),
427                                              offset);
428                 pos = skb_put(skb, noffset - offset);
429                 memcpy(pos, extra_ies + offset, noffset - offset);
430                 offset = noffset;
431         }
432
433         /* build the VHT-cap similarly to the HT-cap */
434         memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
435         if (action_code == WLAN_TDLS_SETUP_REQUEST && vht_cap.vht_supported) {
436                 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
437
438                 /* the AID is present only when VHT is implemented */
439                 ieee80211_tdls_add_aid(sdata, skb);
440
441                 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
442                 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
443         } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
444                    vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
445                 /* the peer caps are already intersected with our own */
446                 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
447
448                 /* the AID is present only when VHT is implemented */
449                 ieee80211_tdls_add_aid(sdata, skb);
450
451                 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
452                 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
453         }
454
455         rcu_read_unlock();
456
457         /* add any remaining IEs */
458         if (extra_ies_len) {
459                 noffset = extra_ies_len;
460                 pos = skb_put(skb, noffset - offset);
461                 memcpy(pos, extra_ies + offset, noffset - offset);
462         }
463
464 }
465
466 static void
467 ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
468                                  struct sk_buff *skb, const u8 *peer,
469                                  bool initiator, const u8 *extra_ies,
470                                  size_t extra_ies_len)
471 {
472         struct ieee80211_local *local = sdata->local;
473         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
474         size_t offset = 0, noffset;
475         struct sta_info *sta, *ap_sta;
476         enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
477         u8 *pos;
478
479         rcu_read_lock();
480
481         sta = sta_info_get(sdata, peer);
482         ap_sta = sta_info_get(sdata, ifmgd->bssid);
483         if (WARN_ON_ONCE(!sta || !ap_sta)) {
484                 rcu_read_unlock();
485                 return;
486         }
487
488         /* add any custom IEs that go before the QoS IE */
489         if (extra_ies_len) {
490                 static const u8 before_qos[] = {
491                         WLAN_EID_RSN,
492                 };
493                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
494                                              before_qos,
495                                              ARRAY_SIZE(before_qos),
496                                              offset);
497                 pos = skb_put(skb, noffset - offset);
498                 memcpy(pos, extra_ies + offset, noffset - offset);
499                 offset = noffset;
500         }
501
502         /* add the QoS param IE if both the peer and we support it */
503         if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
504                 ieee80211_tdls_add_wmm_param_ie(sdata, skb);
505
506         /* add any custom IEs that go before HT operation */
507         if (extra_ies_len) {
508                 static const u8 before_ht_op[] = {
509                         WLAN_EID_RSN,
510                         WLAN_EID_QOS_CAPA,
511                         WLAN_EID_FAST_BSS_TRANSITION,
512                         WLAN_EID_TIMEOUT_INTERVAL,
513                 };
514                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
515                                              before_ht_op,
516                                              ARRAY_SIZE(before_ht_op),
517                                              offset);
518                 pos = skb_put(skb, noffset - offset);
519                 memcpy(pos, extra_ies + offset, noffset - offset);
520                 offset = noffset;
521         }
522
523         /* if HT support is only added in TDLS, we need an HT-operation IE */
524         if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
525                 struct ieee80211_chanctx_conf *chanctx_conf =
526                                 rcu_dereference(sdata->vif.chanctx_conf);
527                 if (!WARN_ON(!chanctx_conf)) {
528                         pos = skb_put(skb, 2 +
529                                       sizeof(struct ieee80211_ht_operation));
530                         /* send an empty HT operation IE */
531                         ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
532                                                    &chanctx_conf->def, 0);
533                 }
534         }
535
536         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
537
538         /* only include VHT-operation if not on the 2.4GHz band */
539         if (band != IEEE80211_BAND_2GHZ && !ap_sta->sta.vht_cap.vht_supported &&
540             sta->sta.vht_cap.vht_supported) {
541                 struct ieee80211_chanctx_conf *chanctx_conf =
542                                 rcu_dereference(sdata->vif.chanctx_conf);
543                 if (!WARN_ON(!chanctx_conf)) {
544                         pos = skb_put(skb, 2 +
545                                       sizeof(struct ieee80211_vht_operation));
546                         ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
547                                                     &chanctx_conf->def);
548                 }
549         }
550
551         rcu_read_unlock();
552
553         /* add any remaining IEs */
554         if (extra_ies_len) {
555                 noffset = extra_ies_len;
556                 pos = skb_put(skb, noffset - offset);
557                 memcpy(pos, extra_ies + offset, noffset - offset);
558         }
559 }
560
561 static void
562 ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
563                                        struct sk_buff *skb, const u8 *peer,
564                                        bool initiator, const u8 *extra_ies,
565                                        size_t extra_ies_len, u8 oper_class,
566                                        struct cfg80211_chan_def *chandef)
567 {
568         struct ieee80211_tdls_data *tf;
569         size_t offset = 0, noffset;
570         u8 *pos;
571
572         if (WARN_ON_ONCE(!chandef))
573                 return;
574
575         tf = (void *)skb->data;
576         tf->u.chan_switch_req.target_channel =
577                 ieee80211_frequency_to_channel(chandef->chan->center_freq);
578         tf->u.chan_switch_req.oper_class = oper_class;
579
580         if (extra_ies_len) {
581                 static const u8 before_lnkie[] = {
582                         WLAN_EID_SECONDARY_CHANNEL_OFFSET,
583                 };
584                 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
585                                              before_lnkie,
586                                              ARRAY_SIZE(before_lnkie),
587                                              offset);
588                 pos = skb_put(skb, noffset - offset);
589                 memcpy(pos, extra_ies + offset, noffset - offset);
590                 offset = noffset;
591         }
592
593         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
594
595         /* add any remaining IEs */
596         if (extra_ies_len) {
597                 noffset = extra_ies_len;
598                 pos = skb_put(skb, noffset - offset);
599                 memcpy(pos, extra_ies + offset, noffset - offset);
600         }
601 }
602
603 static void
604 ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
605                                         struct sk_buff *skb, const u8 *peer,
606                                         u16 status_code, bool initiator,
607                                         const u8 *extra_ies,
608                                         size_t extra_ies_len)
609 {
610         if (status_code == 0)
611                 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
612
613         if (extra_ies_len)
614                 memcpy(skb_put(skb, extra_ies_len), extra_ies, extra_ies_len);
615 }
616
617 static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
618                                    struct sk_buff *skb, const u8 *peer,
619                                    u8 action_code, u16 status_code,
620                                    bool initiator, const u8 *extra_ies,
621                                    size_t extra_ies_len, u8 oper_class,
622                                    struct cfg80211_chan_def *chandef)
623 {
624         switch (action_code) {
625         case WLAN_TDLS_SETUP_REQUEST:
626         case WLAN_TDLS_SETUP_RESPONSE:
627         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
628                 if (status_code == 0)
629                         ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
630                                                            action_code,
631                                                            initiator,
632                                                            extra_ies,
633                                                            extra_ies_len);
634                 break;
635         case WLAN_TDLS_SETUP_CONFIRM:
636                 if (status_code == 0)
637                         ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
638                                                          initiator, extra_ies,
639                                                          extra_ies_len);
640                 break;
641         case WLAN_TDLS_TEARDOWN:
642         case WLAN_TDLS_DISCOVERY_REQUEST:
643                 if (extra_ies_len)
644                         memcpy(skb_put(skb, extra_ies_len), extra_ies,
645                                extra_ies_len);
646                 if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
647                         ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
648                 break;
649         case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
650                 ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
651                                                        initiator, extra_ies,
652                                                        extra_ies_len,
653                                                        oper_class, chandef);
654                 break;
655         case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
656                 ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
657                                                         status_code,
658                                                         initiator, extra_ies,
659                                                         extra_ies_len);
660                 break;
661         }
662
663 }
664
665 static int
666 ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
667                                const u8 *peer, u8 action_code, u8 dialog_token,
668                                u16 status_code, struct sk_buff *skb)
669 {
670         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
671         struct ieee80211_tdls_data *tf;
672
673         tf = (void *)skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
674
675         memcpy(tf->da, peer, ETH_ALEN);
676         memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
677         tf->ether_type = cpu_to_be16(ETH_P_TDLS);
678         tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
679
680         /* network header is after the ethernet header */
681         skb_set_network_header(skb, ETH_HLEN);
682
683         switch (action_code) {
684         case WLAN_TDLS_SETUP_REQUEST:
685                 tf->category = WLAN_CATEGORY_TDLS;
686                 tf->action_code = WLAN_TDLS_SETUP_REQUEST;
687
688                 skb_put(skb, sizeof(tf->u.setup_req));
689                 tf->u.setup_req.dialog_token = dialog_token;
690                 tf->u.setup_req.capability =
691                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
692                                                                  status_code));
693                 break;
694         case WLAN_TDLS_SETUP_RESPONSE:
695                 tf->category = WLAN_CATEGORY_TDLS;
696                 tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
697
698                 skb_put(skb, sizeof(tf->u.setup_resp));
699                 tf->u.setup_resp.status_code = cpu_to_le16(status_code);
700                 tf->u.setup_resp.dialog_token = dialog_token;
701                 tf->u.setup_resp.capability =
702                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
703                                                                  status_code));
704                 break;
705         case WLAN_TDLS_SETUP_CONFIRM:
706                 tf->category = WLAN_CATEGORY_TDLS;
707                 tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
708
709                 skb_put(skb, sizeof(tf->u.setup_cfm));
710                 tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
711                 tf->u.setup_cfm.dialog_token = dialog_token;
712                 break;
713         case WLAN_TDLS_TEARDOWN:
714                 tf->category = WLAN_CATEGORY_TDLS;
715                 tf->action_code = WLAN_TDLS_TEARDOWN;
716
717                 skb_put(skb, sizeof(tf->u.teardown));
718                 tf->u.teardown.reason_code = cpu_to_le16(status_code);
719                 break;
720         case WLAN_TDLS_DISCOVERY_REQUEST:
721                 tf->category = WLAN_CATEGORY_TDLS;
722                 tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
723
724                 skb_put(skb, sizeof(tf->u.discover_req));
725                 tf->u.discover_req.dialog_token = dialog_token;
726                 break;
727         case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
728                 tf->category = WLAN_CATEGORY_TDLS;
729                 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
730
731                 skb_put(skb, sizeof(tf->u.chan_switch_req));
732                 break;
733         case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
734                 tf->category = WLAN_CATEGORY_TDLS;
735                 tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
736
737                 skb_put(skb, sizeof(tf->u.chan_switch_resp));
738                 tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
739                 break;
740         default:
741                 return -EINVAL;
742         }
743
744         return 0;
745 }
746
747 static int
748 ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
749                            const u8 *peer, u8 action_code, u8 dialog_token,
750                            u16 status_code, struct sk_buff *skb)
751 {
752         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
753         struct ieee80211_mgmt *mgmt;
754
755         mgmt = (void *)skb_put(skb, 24);
756         memset(mgmt, 0, 24);
757         memcpy(mgmt->da, peer, ETH_ALEN);
758         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
759         memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
760
761         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
762                                           IEEE80211_STYPE_ACTION);
763
764         switch (action_code) {
765         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
766                 skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
767                 mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
768                 mgmt->u.action.u.tdls_discover_resp.action_code =
769                         WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
770                 mgmt->u.action.u.tdls_discover_resp.dialog_token =
771                         dialog_token;
772                 mgmt->u.action.u.tdls_discover_resp.capability =
773                         cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
774                                                                  status_code));
775                 break;
776         default:
777                 return -EINVAL;
778         }
779
780         return 0;
781 }
782
783 static struct sk_buff *
784 ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
785                                       const u8 *peer, u8 action_code,
786                                       u8 dialog_token, u16 status_code,
787                                       bool initiator, const u8 *extra_ies,
788                                       size_t extra_ies_len, u8 oper_class,
789                                       struct cfg80211_chan_def *chandef)
790 {
791         struct ieee80211_local *local = sdata->local;
792         struct sk_buff *skb;
793         int ret;
794
795         skb = netdev_alloc_skb(sdata->dev,
796                                local->hw.extra_tx_headroom +
797                                max(sizeof(struct ieee80211_mgmt),
798                                    sizeof(struct ieee80211_tdls_data)) +
799                                50 + /* supported rates */
800                                7 + /* ext capab */
801                                26 + /* max(WMM-info, WMM-param) */
802                                2 + max(sizeof(struct ieee80211_ht_cap),
803                                        sizeof(struct ieee80211_ht_operation)) +
804                                2 + max(sizeof(struct ieee80211_vht_cap),
805                                        sizeof(struct ieee80211_vht_operation)) +
806                                50 + /* supported channels */
807                                3 + /* 40/20 BSS coex */
808                                4 + /* AID */
809                                4 + /* oper classes */
810                                extra_ies_len +
811                                sizeof(struct ieee80211_tdls_lnkie));
812         if (!skb)
813                 return NULL;
814
815         skb_reserve(skb, local->hw.extra_tx_headroom);
816
817         switch (action_code) {
818         case WLAN_TDLS_SETUP_REQUEST:
819         case WLAN_TDLS_SETUP_RESPONSE:
820         case WLAN_TDLS_SETUP_CONFIRM:
821         case WLAN_TDLS_TEARDOWN:
822         case WLAN_TDLS_DISCOVERY_REQUEST:
823         case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
824         case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
825                 ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
826                                                      sdata->dev, peer,
827                                                      action_code, dialog_token,
828                                                      status_code, skb);
829                 break;
830         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
831                 ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
832                                                  peer, action_code,
833                                                  dialog_token, status_code,
834                                                  skb);
835                 break;
836         default:
837                 ret = -ENOTSUPP;
838                 break;
839         }
840
841         if (ret < 0)
842                 goto fail;
843
844         ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
845                                initiator, extra_ies, extra_ies_len, oper_class,
846                                chandef);
847         return skb;
848
849 fail:
850         dev_kfree_skb(skb);
851         return NULL;
852 }
853
854 static int
855 ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
856                                 const u8 *peer, u8 action_code, u8 dialog_token,
857                                 u16 status_code, u32 peer_capability,
858                                 bool initiator, const u8 *extra_ies,
859                                 size_t extra_ies_len, u8 oper_class,
860                                 struct cfg80211_chan_def *chandef)
861 {
862         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
863         struct sk_buff *skb = NULL;
864         struct sta_info *sta;
865         u32 flags = 0;
866         int ret = 0;
867
868         rcu_read_lock();
869         sta = sta_info_get(sdata, peer);
870
871         /* infer the initiator if we can, to support old userspace */
872         switch (action_code) {
873         case WLAN_TDLS_SETUP_REQUEST:
874                 if (sta) {
875                         set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
876                         sta->sta.tdls_initiator = false;
877                 }
878                 /* fall-through */
879         case WLAN_TDLS_SETUP_CONFIRM:
880         case WLAN_TDLS_DISCOVERY_REQUEST:
881                 initiator = true;
882                 break;
883         case WLAN_TDLS_SETUP_RESPONSE:
884                 /*
885                  * In some testing scenarios, we send a request and response.
886                  * Make the last packet sent take effect for the initiator
887                  * value.
888                  */
889                 if (sta) {
890                         clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
891                         sta->sta.tdls_initiator = true;
892                 }
893                 /* fall-through */
894         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
895                 initiator = false;
896                 break;
897         case WLAN_TDLS_TEARDOWN:
898         case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
899         case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
900                 /* any value is ok */
901                 break;
902         default:
903                 ret = -ENOTSUPP;
904                 break;
905         }
906
907         if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
908                 initiator = true;
909
910         rcu_read_unlock();
911         if (ret < 0)
912                 goto fail;
913
914         skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
915                                                     dialog_token, status_code,
916                                                     initiator, extra_ies,
917                                                     extra_ies_len, oper_class,
918                                                     chandef);
919         if (!skb) {
920                 ret = -EINVAL;
921                 goto fail;
922         }
923
924         if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
925                 ieee80211_tx_skb(sdata, skb);
926                 return 0;
927         }
928
929         /*
930          * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
931          * we should default to AC_VI.
932          */
933         switch (action_code) {
934         case WLAN_TDLS_SETUP_REQUEST:
935         case WLAN_TDLS_SETUP_RESPONSE:
936                 skb_set_queue_mapping(skb, IEEE80211_AC_BK);
937                 skb->priority = 2;
938                 break;
939         default:
940                 skb_set_queue_mapping(skb, IEEE80211_AC_VI);
941                 skb->priority = 5;
942                 break;
943         }
944
945         /*
946          * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
947          * Later, if no ACK is returned from peer, we will re-send the teardown
948          * packet through the AP.
949          */
950         if ((action_code == WLAN_TDLS_TEARDOWN) &&
951             (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
952                 bool try_resend; /* Should we keep skb for possible resend */
953
954                 /* If not sending directly to peer - no point in keeping skb */
955                 rcu_read_lock();
956                 sta = sta_info_get(sdata, peer);
957                 try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
958                 rcu_read_unlock();
959
960                 spin_lock_bh(&sdata->u.mgd.teardown_lock);
961                 if (try_resend && !sdata->u.mgd.teardown_skb) {
962                         /* Mark it as requiring TX status callback  */
963                         flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
964                                  IEEE80211_TX_INTFL_MLME_CONN_TX;
965
966                         /*
967                          * skb is copied since mac80211 will later set
968                          * properties that might not be the same as the AP,
969                          * such as encryption, QoS, addresses, etc.
970                          *
971                          * No problem if skb_copy() fails, so no need to check.
972                          */
973                         sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
974                         sdata->u.mgd.orig_teardown_skb = skb;
975                 }
976                 spin_unlock_bh(&sdata->u.mgd.teardown_lock);
977         }
978
979         /* disable bottom halves when entering the Tx path */
980         local_bh_disable();
981         __ieee80211_subif_start_xmit(skb, dev, flags);
982         local_bh_enable();
983
984         return ret;
985
986 fail:
987         dev_kfree_skb(skb);
988         return ret;
989 }
990
991 static int
992 ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
993                           const u8 *peer, u8 action_code, u8 dialog_token,
994                           u16 status_code, u32 peer_capability, bool initiator,
995                           const u8 *extra_ies, size_t extra_ies_len)
996 {
997         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
998         struct ieee80211_local *local = sdata->local;
999         int ret;
1000
1001         mutex_lock(&local->mtx);
1002
1003         /* we don't support concurrent TDLS peer setups */
1004         if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
1005             !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1006                 ret = -EBUSY;
1007                 goto out_unlock;
1008         }
1009
1010         /*
1011          * make sure we have a STA representing the peer so we drop or buffer
1012          * non-TDLS-setup frames to the peer. We can't send other packets
1013          * during setup through the AP path.
1014          * Allow error packets to be sent - sometimes we don't even add a STA
1015          * before failing the setup.
1016          */
1017         if (status_code == 0) {
1018                 rcu_read_lock();
1019                 if (!sta_info_get(sdata, peer)) {
1020                         rcu_read_unlock();
1021                         ret = -ENOLINK;
1022                         goto out_unlock;
1023                 }
1024                 rcu_read_unlock();
1025         }
1026
1027         ieee80211_flush_queues(local, sdata, false);
1028         memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1029         mutex_unlock(&local->mtx);
1030
1031         /* we cannot take the mutex while preparing the setup packet */
1032         ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1033                                               dialog_token, status_code,
1034                                               peer_capability, initiator,
1035                                               extra_ies, extra_ies_len, 0,
1036                                               NULL);
1037         if (ret < 0) {
1038                 mutex_lock(&local->mtx);
1039                 eth_zero_addr(sdata->u.mgd.tdls_peer);
1040                 mutex_unlock(&local->mtx);
1041                 return ret;
1042         }
1043
1044         ieee80211_queue_delayed_work(&sdata->local->hw,
1045                                      &sdata->u.mgd.tdls_peer_del_work,
1046                                      TDLS_PEER_SETUP_TIMEOUT);
1047         return 0;
1048
1049 out_unlock:
1050         mutex_unlock(&local->mtx);
1051         return ret;
1052 }
1053
1054 static int
1055 ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
1056                              const u8 *peer, u8 action_code, u8 dialog_token,
1057                              u16 status_code, u32 peer_capability,
1058                              bool initiator, const u8 *extra_ies,
1059                              size_t extra_ies_len)
1060 {
1061         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1062         struct ieee80211_local *local = sdata->local;
1063         struct sta_info *sta;
1064         int ret;
1065
1066         /*
1067          * No packets can be transmitted to the peer via the AP during setup -
1068          * the STA is set as a TDLS peer, but is not authorized.
1069          * During teardown, we prevent direct transmissions by stopping the
1070          * queues and flushing all direct packets.
1071          */
1072         ieee80211_stop_vif_queues(local, sdata,
1073                                   IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1074         ieee80211_flush_queues(local, sdata, false);
1075
1076         ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
1077                                               dialog_token, status_code,
1078                                               peer_capability, initiator,
1079                                               extra_ies, extra_ies_len, 0,
1080                                               NULL);
1081         if (ret < 0)
1082                 sdata_err(sdata, "Failed sending TDLS teardown packet %d\n",
1083                           ret);
1084
1085         /*
1086          * Remove the STA AUTH flag to force further traffic through the AP. If
1087          * the STA was unreachable, it was already removed.
1088          */
1089         rcu_read_lock();
1090         sta = sta_info_get(sdata, peer);
1091         if (sta)
1092                 clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1093         rcu_read_unlock();
1094
1095         ieee80211_wake_vif_queues(local, sdata,
1096                                   IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
1097
1098         return 0;
1099 }
1100
1101 int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
1102                         const u8 *peer, u8 action_code, u8 dialog_token,
1103                         u16 status_code, u32 peer_capability,
1104                         bool initiator, const u8 *extra_ies,
1105                         size_t extra_ies_len)
1106 {
1107         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1108         int ret;
1109
1110         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1111                 return -ENOTSUPP;
1112
1113         /* make sure we are in managed mode, and associated */
1114         if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1115             !sdata->u.mgd.associated)
1116                 return -EINVAL;
1117
1118         switch (action_code) {
1119         case WLAN_TDLS_SETUP_REQUEST:
1120         case WLAN_TDLS_SETUP_RESPONSE:
1121                 ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
1122                                                 dialog_token, status_code,
1123                                                 peer_capability, initiator,
1124                                                 extra_ies, extra_ies_len);
1125                 break;
1126         case WLAN_TDLS_TEARDOWN:
1127                 ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
1128                                                    action_code, dialog_token,
1129                                                    status_code,
1130                                                    peer_capability, initiator,
1131                                                    extra_ies, extra_ies_len);
1132                 break;
1133         case WLAN_TDLS_DISCOVERY_REQUEST:
1134                 /*
1135                  * Protect the discovery so we can hear the TDLS discovery
1136                  * response frame. It is transmitted directly and not buffered
1137                  * by the AP.
1138                  */
1139                 drv_mgd_protect_tdls_discover(sdata->local, sdata);
1140                 /* fall-through */
1141         case WLAN_TDLS_SETUP_CONFIRM:
1142         case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1143                 /* no special handling */
1144                 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
1145                                                       action_code,
1146                                                       dialog_token,
1147                                                       status_code,
1148                                                       peer_capability,
1149                                                       initiator, extra_ies,
1150                                                       extra_ies_len, 0, NULL);
1151                 break;
1152         default:
1153                 ret = -EOPNOTSUPP;
1154                 break;
1155         }
1156
1157         tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d\n",
1158                  action_code, peer, ret);
1159         return ret;
1160 }
1161
1162 int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
1163                         const u8 *peer, enum nl80211_tdls_operation oper)
1164 {
1165         struct sta_info *sta;
1166         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1167         struct ieee80211_local *local = sdata->local;
1168         int ret;
1169
1170         if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
1171                 return -ENOTSUPP;
1172
1173         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1174                 return -EINVAL;
1175
1176         switch (oper) {
1177         case NL80211_TDLS_ENABLE_LINK:
1178         case NL80211_TDLS_DISABLE_LINK:
1179                 break;
1180         case NL80211_TDLS_TEARDOWN:
1181         case NL80211_TDLS_SETUP:
1182         case NL80211_TDLS_DISCOVERY_REQ:
1183                 /* We don't support in-driver setup/teardown/discovery */
1184                 return -ENOTSUPP;
1185         }
1186
1187         mutex_lock(&local->mtx);
1188         tdls_dbg(sdata, "TDLS oper %d peer %pM\n", oper, peer);
1189
1190         switch (oper) {
1191         case NL80211_TDLS_ENABLE_LINK:
1192                 rcu_read_lock();
1193                 sta = sta_info_get(sdata, peer);
1194                 if (!sta) {
1195                         rcu_read_unlock();
1196                         ret = -ENOLINK;
1197                         break;
1198                 }
1199
1200                 set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
1201                 rcu_read_unlock();
1202
1203                 WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
1204                              !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
1205                 ret = 0;
1206                 break;
1207         case NL80211_TDLS_DISABLE_LINK:
1208                 /*
1209                  * The teardown message in ieee80211_tdls_mgmt_teardown() was
1210                  * created while the queues were stopped, so it might still be
1211                  * pending. Before flushing the queues we need to be sure the
1212                  * message is handled by the tasklet handling pending messages,
1213                  * otherwise we might start destroying the station before
1214                  * sending the teardown packet.
1215                  * Note that this only forces the tasklet to flush pendings -
1216                  * not to stop the tasklet from rescheduling itself.
1217                  */
1218                 tasklet_kill(&local->tx_pending_tasklet);
1219                 /* flush a potentially queued teardown packet */
1220                 ieee80211_flush_queues(local, sdata, false);
1221
1222                 ret = sta_info_destroy_addr(sdata, peer);
1223                 break;
1224         default:
1225                 ret = -ENOTSUPP;
1226                 break;
1227         }
1228
1229         if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
1230                 cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
1231                 eth_zero_addr(sdata->u.mgd.tdls_peer);
1232         }
1233
1234         mutex_unlock(&local->mtx);
1235         return ret;
1236 }
1237
1238 void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
1239                                  enum nl80211_tdls_operation oper,
1240                                  u16 reason_code, gfp_t gfp)
1241 {
1242         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1243
1244         if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
1245                 sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected\n",
1246                           oper);
1247                 return;
1248         }
1249
1250         cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
1251 }
1252 EXPORT_SYMBOL(ieee80211_tdls_oper_request);
1253
1254 static void
1255 iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
1256 {
1257         struct ieee80211_ch_switch_timing *ch_sw;
1258
1259         *buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
1260         *buf++ = sizeof(struct ieee80211_ch_switch_timing);
1261
1262         ch_sw = (void *)buf;
1263         ch_sw->switch_time = cpu_to_le16(switch_time);
1264         ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
1265 }
1266
1267 /* find switch timing IE in SKB ready for Tx */
1268 static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
1269 {
1270         struct ieee80211_tdls_data *tf;
1271         const u8 *ie_start;
1272
1273         /*
1274          * Get the offset for the new location of the switch timing IE.
1275          * The SKB network header will now point to the "payload_type"
1276          * element of the TDLS data frame struct.
1277          */
1278         tf = container_of(skb->data + skb_network_offset(skb),
1279                           struct ieee80211_tdls_data, payload_type);
1280         ie_start = tf->u.chan_switch_req.variable;
1281         return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
1282                                 skb->len - (ie_start - skb->data));
1283 }
1284
1285 static struct sk_buff *
1286 ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
1287                               struct cfg80211_chan_def *chandef,
1288                               u32 *ch_sw_tm_ie_offset)
1289 {
1290         struct ieee80211_sub_if_data *sdata = sta->sdata;
1291         u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
1292                      2 + sizeof(struct ieee80211_ch_switch_timing)];
1293         int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
1294         u8 *pos = extra_ies;
1295         struct sk_buff *skb;
1296
1297         /*
1298          * if chandef points to a wide channel add a Secondary-Channel
1299          * Offset information element
1300          */
1301         if (chandef->width == NL80211_CHAN_WIDTH_40) {
1302                 struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
1303                 bool ht40plus;
1304
1305                 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
1306                 *pos++ = sizeof(*sec_chan_ie);
1307                 sec_chan_ie = (void *)pos;
1308
1309                 ht40plus = cfg80211_get_chandef_type(chandef) ==
1310                                                         NL80211_CHAN_HT40PLUS;
1311                 sec_chan_ie->sec_chan_offs = ht40plus ?
1312                                              IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
1313                                              IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1314                 pos += sizeof(*sec_chan_ie);
1315
1316                 extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
1317         }
1318
1319         /* just set the values to 0, this is a template */
1320         iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
1321
1322         skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1323                                               WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
1324                                               0, 0, !sta->sta.tdls_initiator,
1325                                               extra_ies, extra_ies_len,
1326                                               oper_class, chandef);
1327         if (!skb)
1328                 return NULL;
1329
1330         skb = ieee80211_build_data_template(sdata, skb, 0);
1331         if (IS_ERR(skb)) {
1332                 tdls_dbg(sdata, "Failed building TDLS channel switch frame\n");
1333                 return NULL;
1334         }
1335
1336         if (ch_sw_tm_ie_offset) {
1337                 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1338
1339                 if (!tm_ie) {
1340                         tdls_dbg(sdata, "No switch timing IE in TDLS switch\n");
1341                         dev_kfree_skb_any(skb);
1342                         return NULL;
1343                 }
1344
1345                 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1346         }
1347
1348         tdls_dbg(sdata,
1349                  "TDLS channel switch request template for %pM ch %d width %d\n",
1350                  sta->sta.addr, chandef->chan->center_freq, chandef->width);
1351         return skb;
1352 }
1353
1354 int
1355 ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1356                               const u8 *addr, u8 oper_class,
1357                               struct cfg80211_chan_def *chandef)
1358 {
1359         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1360         struct ieee80211_local *local = sdata->local;
1361         struct sta_info *sta;
1362         struct sk_buff *skb = NULL;
1363         u32 ch_sw_tm_ie;
1364         int ret;
1365
1366         mutex_lock(&local->sta_mtx);
1367         sta = sta_info_get(sdata, addr);
1368         if (!sta) {
1369                 tdls_dbg(sdata,
1370                          "Invalid TDLS peer %pM for channel switch request\n",
1371                          addr);
1372                 ret = -ENOENT;
1373                 goto out;
1374         }
1375
1376         if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
1377                 tdls_dbg(sdata, "TDLS channel switch unsupported by %pM\n",
1378                          addr);
1379                 ret = -ENOTSUPP;
1380                 goto out;
1381         }
1382
1383         skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
1384                                             &ch_sw_tm_ie);
1385         if (!skb) {
1386                 ret = -ENOENT;
1387                 goto out;
1388         }
1389
1390         ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
1391                                       chandef, skb, ch_sw_tm_ie);
1392         if (!ret)
1393                 set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1394
1395 out:
1396         mutex_unlock(&local->sta_mtx);
1397         dev_kfree_skb_any(skb);
1398         return ret;
1399 }
1400
1401 void
1402 ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
1403                                      struct net_device *dev,
1404                                      const u8 *addr)
1405 {
1406         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1407         struct ieee80211_local *local = sdata->local;
1408         struct sta_info *sta;
1409
1410         mutex_lock(&local->sta_mtx);
1411         sta = sta_info_get(sdata, addr);
1412         if (!sta) {
1413                 tdls_dbg(sdata,
1414                          "Invalid TDLS peer %pM for channel switch cancel\n",
1415                          addr);
1416                 goto out;
1417         }
1418
1419         if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1420                 tdls_dbg(sdata, "TDLS channel switch not initiated by %pM\n",
1421                          addr);
1422                 goto out;
1423         }
1424
1425         drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1426         clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1427
1428 out:
1429         mutex_unlock(&local->sta_mtx);
1430 }
1431
1432 static struct sk_buff *
1433 ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
1434                                    u32 *ch_sw_tm_ie_offset)
1435 {
1436         struct ieee80211_sub_if_data *sdata = sta->sdata;
1437         struct sk_buff *skb;
1438         u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
1439
1440         /* initial timing are always zero in the template */
1441         iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
1442
1443         skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
1444                                         WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
1445                                         0, 0, !sta->sta.tdls_initiator,
1446                                         extra_ies, sizeof(extra_ies), 0, NULL);
1447         if (!skb)
1448                 return NULL;
1449
1450         skb = ieee80211_build_data_template(sdata, skb, 0);
1451         if (IS_ERR(skb)) {
1452                 tdls_dbg(sdata,
1453                          "Failed building TDLS channel switch resp frame\n");
1454                 return NULL;
1455         }
1456
1457         if (ch_sw_tm_ie_offset) {
1458                 const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
1459
1460                 if (!tm_ie) {
1461                         tdls_dbg(sdata,
1462                                  "No switch timing IE in TDLS switch resp\n");
1463                         dev_kfree_skb_any(skb);
1464                         return NULL;
1465                 }
1466
1467                 *ch_sw_tm_ie_offset = tm_ie - skb->data;
1468         }
1469
1470         tdls_dbg(sdata, "TDLS get channel switch response template for %pM\n",
1471                  sta->sta.addr);
1472         return skb;
1473 }
1474
1475 static int
1476 ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
1477                                            struct sk_buff *skb)
1478 {
1479         struct ieee80211_local *local = sdata->local;
1480         struct ieee802_11_elems elems;
1481         struct sta_info *sta;
1482         struct ieee80211_tdls_data *tf = (void *)skb->data;
1483         bool local_initiator;
1484         struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1485         int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
1486         struct ieee80211_tdls_ch_sw_params params = {};
1487         int ret;
1488
1489         params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
1490         params.timestamp = rx_status->device_timestamp;
1491
1492         if (skb->len < baselen) {
1493                 tdls_dbg(sdata, "TDLS channel switch resp too short: %d\n",
1494                          skb->len);
1495                 return -EINVAL;
1496         }
1497
1498         mutex_lock(&local->sta_mtx);
1499         sta = sta_info_get(sdata, tf->sa);
1500         if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1501                 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1502                          tf->sa);
1503                 ret = -EINVAL;
1504                 goto out;
1505         }
1506
1507         params.sta = &sta->sta;
1508         params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
1509         if (params.status != 0) {
1510                 ret = 0;
1511                 goto call_drv;
1512         }
1513
1514         ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
1515                                skb->len - baselen, false, &elems);
1516         if (elems.parse_error) {
1517                 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp\n");
1518                 ret = -EINVAL;
1519                 goto out;
1520         }
1521
1522         if (!elems.ch_sw_timing || !elems.lnk_id) {
1523                 tdls_dbg(sdata, "TDLS channel switch resp - missing IEs\n");
1524                 ret = -EINVAL;
1525                 goto out;
1526         }
1527
1528         /* validate the initiator is set correctly */
1529         local_initiator =
1530                 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1531         if (local_initiator == sta->sta.tdls_initiator) {
1532                 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1533                 ret = -EINVAL;
1534                 goto out;
1535         }
1536
1537         params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1538         params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1539
1540         params.tmpl_skb =
1541                 ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
1542         if (!params.tmpl_skb) {
1543                 ret = -ENOENT;
1544                 goto out;
1545         }
1546
1547 call_drv:
1548         drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1549
1550         tdls_dbg(sdata,
1551                  "TDLS channel switch response received from %pM status %d\n",
1552                  tf->sa, params.status);
1553
1554 out:
1555         mutex_unlock(&local->sta_mtx);
1556         dev_kfree_skb_any(params.tmpl_skb);
1557         return ret;
1558 }
1559
1560 static int
1561 ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
1562                                           struct sk_buff *skb)
1563 {
1564         struct ieee80211_local *local = sdata->local;
1565         struct ieee802_11_elems elems;
1566         struct cfg80211_chan_def chandef;
1567         struct ieee80211_channel *chan;
1568         enum nl80211_channel_type chan_type;
1569         int freq;
1570         u8 target_channel, oper_class;
1571         bool local_initiator;
1572         struct sta_info *sta;
1573         enum ieee80211_band band;
1574         struct ieee80211_tdls_data *tf = (void *)skb->data;
1575         struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1576         int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
1577         struct ieee80211_tdls_ch_sw_params params = {};
1578         int ret = 0;
1579
1580         params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
1581         params.timestamp = rx_status->device_timestamp;
1582
1583         if (skb->len < baselen) {
1584                 tdls_dbg(sdata, "TDLS channel switch req too short: %d\n",
1585                          skb->len);
1586                 return -EINVAL;
1587         }
1588
1589         target_channel = tf->u.chan_switch_req.target_channel;
1590         oper_class = tf->u.chan_switch_req.oper_class;
1591
1592         /*
1593          * We can't easily infer the channel band. The operating class is
1594          * ambiguous - there are multiple tables (US/Europe/JP/Global). The
1595          * solution here is to treat channels with number >14 as 5GHz ones,
1596          * and specifically check for the (oper_class, channel) combinations
1597          * where this doesn't hold. These are thankfully unique according to
1598          * IEEE802.11-2012.
1599          * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
1600          * valid here.
1601          */
1602         if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
1603              oper_class == 4 || oper_class == 5 || oper_class == 6) &&
1604              target_channel < 14)
1605                 band = IEEE80211_BAND_5GHZ;
1606         else
1607                 band = target_channel < 14 ? IEEE80211_BAND_2GHZ :
1608                                              IEEE80211_BAND_5GHZ;
1609
1610         freq = ieee80211_channel_to_frequency(target_channel, band);
1611         if (freq == 0) {
1612                 tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d\n",
1613                          target_channel);
1614                 return -EINVAL;
1615         }
1616
1617         chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
1618         if (!chan) {
1619                 tdls_dbg(sdata,
1620                          "Unsupported channel for TDLS chan switch: %d\n",
1621                          target_channel);
1622                 return -EINVAL;
1623         }
1624
1625         ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
1626                                skb->len - baselen, false, &elems);
1627         if (elems.parse_error) {
1628                 tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req\n");
1629                 return -EINVAL;
1630         }
1631
1632         if (!elems.ch_sw_timing || !elems.lnk_id) {
1633                 tdls_dbg(sdata, "TDLS channel switch req - missing IEs\n");
1634                 return -EINVAL;
1635         }
1636
1637         mutex_lock(&local->sta_mtx);
1638         sta = sta_info_get(sdata, tf->sa);
1639         if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
1640                 tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM\n",
1641                          tf->sa);
1642                 ret = -EINVAL;
1643                 goto out;
1644         }
1645
1646         params.sta = &sta->sta;
1647
1648         /* validate the initiator is set correctly */
1649         local_initiator =
1650                 !memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
1651         if (local_initiator == sta->sta.tdls_initiator) {
1652                 tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator\n");
1653                 ret = -EINVAL;
1654                 goto out;
1655         }
1656
1657         if (!sta->sta.ht_cap.ht_supported) {
1658                 chan_type = NL80211_CHAN_NO_HT;
1659         } else if (!elems.sec_chan_offs) {
1660                 chan_type = NL80211_CHAN_HT20;
1661         } else {
1662                 switch (elems.sec_chan_offs->sec_chan_offs) {
1663                 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1664                         chan_type = NL80211_CHAN_HT40PLUS;
1665                         break;
1666                 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1667                         chan_type = NL80211_CHAN_HT40MINUS;
1668                         break;
1669                 default:
1670                         chan_type = NL80211_CHAN_HT20;
1671                         break;
1672                 }
1673         }
1674
1675         cfg80211_chandef_create(&chandef, chan, chan_type);
1676         params.chandef = &chandef;
1677
1678         params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
1679         params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
1680
1681         params.tmpl_skb =
1682                 ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
1683                                                    &params.ch_sw_tm_ie);
1684         if (!params.tmpl_skb) {
1685                 ret = -ENOENT;
1686                 goto out;
1687         }
1688
1689         drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
1690
1691         tdls_dbg(sdata,
1692                  "TDLS ch switch request received from %pM ch %d width %d\n",
1693                  tf->sa, params.chandef->chan->center_freq,
1694                  params.chandef->width);
1695 out:
1696         mutex_unlock(&local->sta_mtx);
1697         dev_kfree_skb_any(params.tmpl_skb);
1698         return ret;
1699 }
1700
1701 void ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
1702                                            struct sk_buff *skb)
1703 {
1704         struct ieee80211_tdls_data *tf = (void *)skb->data;
1705         struct wiphy *wiphy = sdata->local->hw.wiphy;
1706
1707         /* make sure the driver supports it */
1708         if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
1709                 return;
1710
1711         /* we want to access the entire packet */
1712         if (skb_linearize(skb))
1713                 return;
1714         /*
1715          * The packet/size was already validated by mac80211 Rx path, only look
1716          * at the action type.
1717          */
1718         switch (tf->action_code) {
1719         case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
1720                 ieee80211_process_tdls_channel_switch_req(sdata, skb);
1721                 break;
1722         case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
1723                 ieee80211_process_tdls_channel_switch_resp(sdata, skb);
1724                 break;
1725         default:
1726                 WARN_ON_ONCE(1);
1727                 return;
1728         }
1729 }