Blame view

net/mac80211/tdls.c 54.8 KB
95224fe83   Arik Nemtsov   mac80211: move TD...
1
2
3
4
5
  /*
   * mac80211 TDLS handling code
   *
   * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
   * Copyright 2014, Intel Corporation
d98ad83ee   Johannes Berg   mac80211: add Int...
6
   * Copyright 2014  Intel Mobile Communications GmbH
59021c675   Arik Nemtsov   mac80211: TDLS: c...
7
   * Copyright 2015 - 2016 Intel Deutschland GmbH
95224fe83   Arik Nemtsov   mac80211: move TD...
8
9
10
11
12
   *
   * This file is GPLv2 as found in COPYING.
   */
  
  #include <linux/ieee80211.h>
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
13
  #include <linux/log2.h>
c887f0d3a   Arik Nemtsov   mac80211: add API...
14
  #include <net/cfg80211.h>
c8ff71e66   Arik Nemtsov   mac80211: TDLS: h...
15
  #include <linux/rtnetlink.h>
95224fe83   Arik Nemtsov   mac80211: move TD...
16
  #include "ieee80211_i.h"
ee10f2c77   Arik Nemtsov   mac80211: protect...
17
  #include "driver-ops.h"
59021c675   Arik Nemtsov   mac80211: TDLS: c...
18
  #include "rate.h"
ebec37ed2   Johannes Berg   mac80211: TDLS: f...
19
  #include "wme.h"
95224fe83   Arik Nemtsov   mac80211: move TD...
20

17e6a59a3   Arik Nemtsov   mac80211: cleanup...
21
22
23
24
25
26
27
28
29
  /* give usermode some time for retries in setting up the TDLS session */
  #define TDLS_PEER_SETUP_TIMEOUT	(15 * HZ)
  
  void ieee80211_tdls_peer_del_work(struct work_struct *wk)
  {
  	struct ieee80211_sub_if_data *sdata;
  	struct ieee80211_local *local;
  
  	sdata = container_of(wk, struct ieee80211_sub_if_data,
81dd2b882   Arik Nemtsov   mac80211: move TD...
30
  			     u.mgd.tdls_peer_del_work.work);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
31
32
33
  	local = sdata->local;
  
  	mutex_lock(&local->mtx);
81dd2b882   Arik Nemtsov   mac80211: move TD...
34
35
36
37
38
  	if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer)) {
  		tdls_dbg(sdata, "TDLS del peer %pM
  ", sdata->u.mgd.tdls_peer);
  		sta_info_destroy_addr(sdata, sdata->u.mgd.tdls_peer);
  		eth_zero_addr(sdata->u.mgd.tdls_peer);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
39
40
41
  	}
  	mutex_unlock(&local->mtx);
  }
b98fb44ff   Arik Nemtsov   mac80211: define ...
42
  static void ieee80211_tdls_add_ext_capab(struct ieee80211_sub_if_data *sdata,
78632a17e   Arik Nemtsov   cfg/mac80211: def...
43
  					 struct sk_buff *skb)
95224fe83   Arik Nemtsov   mac80211: move TD...
44
  {
b98fb44ff   Arik Nemtsov   mac80211: define ...
45
  	struct ieee80211_local *local = sdata->local;
82c0cc90d   Arik Nemtsov   mac80211: debugfs...
46
  	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
78632a17e   Arik Nemtsov   cfg/mac80211: def...
47
48
  	bool chan_switch = local->hw.wiphy->features &
  			   NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
82c0cc90d   Arik Nemtsov   mac80211: debugfs...
49
50
  	bool wider_band = ieee80211_hw_check(&local->hw, TDLS_WIDER_BW) &&
  			  !ifmgd->tdls_wider_bw_prohibited;
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
51
  	struct ieee80211_supported_band *sband = ieee80211_get_sband(sdata);
b98fb44ff   Arik Nemtsov   mac80211: define ...
52
  	bool vht = sband && sband->vht_cap.vht_supported;
4df864c1d   Johannes Berg   networking: make ...
53
  	u8 *pos = skb_put(skb, 10);
95224fe83   Arik Nemtsov   mac80211: move TD...
54
55
  
  	*pos++ = WLAN_EID_EXT_CAPABILITY;
b98fb44ff   Arik Nemtsov   mac80211: define ...
56
  	*pos++ = 8; /* len */
95224fe83   Arik Nemtsov   mac80211: move TD...
57
58
59
  	*pos++ = 0x0;
  	*pos++ = 0x0;
  	*pos++ = 0x0;
78632a17e   Arik Nemtsov   cfg/mac80211: def...
60
  	*pos++ = chan_switch ? WLAN_EXT_CAPA4_TDLS_CHAN_SWITCH : 0;
95224fe83   Arik Nemtsov   mac80211: move TD...
61
  	*pos++ = WLAN_EXT_CAPA5_TDLS_ENABLED;
b98fb44ff   Arik Nemtsov   mac80211: define ...
62
63
64
  	*pos++ = 0;
  	*pos++ = 0;
  	*pos++ = (vht && wider_band) ? WLAN_EXT_CAPA8_TDLS_WIDE_BW_ENABLED : 0;
95224fe83   Arik Nemtsov   mac80211: move TD...
65
  }
f0d29cb97   Arik Nemtsov   mac80211: add sup...
66
67
68
69
70
71
72
73
74
  static u8
  ieee80211_tdls_add_subband(struct ieee80211_sub_if_data *sdata,
  			   struct sk_buff *skb, u16 start, u16 end,
  			   u16 spacing)
  {
  	u8 subband_cnt = 0, ch_cnt = 0;
  	struct ieee80211_channel *ch;
  	struct cfg80211_chan_def chandef;
  	int i, subband_start;
923b352f1   Arik Nemtsov   cfg80211: use RTN...
75
  	struct wiphy *wiphy = sdata->local->hw.wiphy;
f0d29cb97   Arik Nemtsov   mac80211: add sup...
76
77
78
79
80
81
82
83
  
  	for (i = start; i <= end; i += spacing) {
  		if (!ch_cnt)
  			subband_start = i;
  
  		ch = ieee80211_get_channel(sdata->local->hw.wiphy, i);
  		if (ch) {
  			/* we will be active on the channel */
f0d29cb97   Arik Nemtsov   mac80211: add sup...
84
  			cfg80211_chandef_create(&chandef, ch,
50075892b   Arik Nemtsov   mac80211: add TDL...
85
  						NL80211_CHAN_NO_HT);
923b352f1   Arik Nemtsov   cfg80211: use RTN...
86
87
  			if (cfg80211_reg_can_beacon_relax(wiphy, &chandef,
  							  sdata->wdev.iftype)) {
f0d29cb97   Arik Nemtsov   mac80211: add sup...
88
  				ch_cnt++;
50075892b   Arik Nemtsov   mac80211: add TDL...
89
90
91
92
  				/*
  				 * check if the next channel is also part of
  				 * this allowed range
  				 */
f0d29cb97   Arik Nemtsov   mac80211: add sup...
93
94
95
  				continue;
  			}
  		}
50075892b   Arik Nemtsov   mac80211: add TDL...
96
97
98
99
  		/*
  		 * we've reached the end of a range, with allowed channels
  		 * found
  		 */
f0d29cb97   Arik Nemtsov   mac80211: add sup...
100
101
102
103
104
105
106
107
108
  		if (ch_cnt) {
  			u8 *pos = skb_put(skb, 2);
  			*pos++ = ieee80211_frequency_to_channel(subband_start);
  			*pos++ = ch_cnt;
  
  			subband_cnt++;
  			ch_cnt = 0;
  		}
  	}
50075892b   Arik Nemtsov   mac80211: add TDL...
109
110
111
112
113
114
115
116
  	/* all channels in the requested range are allowed - add them here */
  	if (ch_cnt) {
  		u8 *pos = skb_put(skb, 2);
  		*pos++ = ieee80211_frequency_to_channel(subband_start);
  		*pos++ = ch_cnt;
  
  		subband_cnt++;
  	}
f0d29cb97   Arik Nemtsov   mac80211: add sup...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  	return subband_cnt;
  }
  
  static void
  ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
  				 struct sk_buff *skb)
  {
  	/*
  	 * Add possible channels for TDLS. These are channels that are allowed
  	 * to be active.
  	 */
  	u8 subband_cnt;
  	u8 *pos = skb_put(skb, 2);
  
  	*pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  
  	/*
  	 * 5GHz and 2GHz channels numbers can overlap. Ignore this for now, as
  	 * this doesn't happen in real world scenarios.
  	 */
  
  	/* 2GHz, with 5MHz spacing */
  	subband_cnt = ieee80211_tdls_add_subband(sdata, skb, 2412, 2472, 5);
  
  	/* 5GHz, with 20MHz spacing */
  	subband_cnt += ieee80211_tdls_add_subband(sdata, skb, 5000, 5825, 20);
  
  	/* length */
  	*pos = 2 * subband_cnt;
  }
a38700dd4   Arik Nemtsov   cfg/mac80211: add...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
  					    struct sk_buff *skb)
  {
  	u8 *pos;
  	u8 op_class;
  
  	if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
  						  &op_class))
  		return;
  
  	pos = skb_put(skb, 4);
  	*pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
  	*pos++ = 2; /* len */
  
  	*pos++ = op_class;
  	*pos++ = op_class; /* give current operating class as alternate too */
  }
2cedd8796   Arik Nemtsov   mac80211: add BSS...
164
165
  static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
  {
4df864c1d   Johannes Berg   networking: make ...
166
  	u8 *pos = skb_put(skb, 3);
2cedd8796   Arik Nemtsov   mac80211: add BSS...
167
168
169
170
171
172
  
  	*pos++ = WLAN_EID_BSS_COEX_2040;
  	*pos++ = 1; /* len */
  
  	*pos++ = WLAN_BSS_COEX_INFORMATION_REQUEST;
  }
dd8c0b03d   Arik Nemtsov   mac80211: set TDL...
173
174
  static u16 ieee80211_get_tdls_sta_capab(struct ieee80211_sub_if_data *sdata,
  					u16 status_code)
95224fe83   Arik Nemtsov   mac80211: move TD...
175
  {
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
176
  	struct ieee80211_supported_band *sband;
dd8c0b03d   Arik Nemtsov   mac80211: set TDL...
177
178
179
  	/* The capability will be 0 when sending a failure code */
  	if (status_code != 0)
  		return 0;
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
180
181
  	sband = ieee80211_get_sband(sdata);
  	if (sband && sband->band == NL80211_BAND_2GHZ) {
ea1b2b45f   Johannes Berg   mac80211: remove ...
182
183
184
  		return WLAN_CAPABILITY_SHORT_SLOT_TIME |
  		       WLAN_CAPABILITY_SHORT_PREAMBLE;
  	}
95224fe83   Arik Nemtsov   mac80211: move TD...
185

ea1b2b45f   Johannes Berg   mac80211: remove ...
186
  	return 0;
95224fe83   Arik Nemtsov   mac80211: move TD...
187
  }
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
188
189
190
  static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
  				       struct sk_buff *skb, const u8 *peer,
  				       bool initiator)
95224fe83   Arik Nemtsov   mac80211: move TD...
191
192
  {
  	struct ieee80211_tdls_lnkie *lnkid;
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
193
194
195
196
197
198
199
200
201
  	const u8 *init_addr, *rsp_addr;
  
  	if (initiator) {
  		init_addr = sdata->vif.addr;
  		rsp_addr = peer;
  	} else {
  		init_addr = peer;
  		rsp_addr = sdata->vif.addr;
  	}
95224fe83   Arik Nemtsov   mac80211: move TD...
202

4df864c1d   Johannes Berg   networking: make ...
203
  	lnkid = skb_put(skb, sizeof(struct ieee80211_tdls_lnkie));
95224fe83   Arik Nemtsov   mac80211: move TD...
204
205
206
  
  	lnkid->ie_type = WLAN_EID_LINK_ID;
  	lnkid->ie_len = sizeof(struct ieee80211_tdls_lnkie) - 2;
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
207
208
209
  	memcpy(lnkid->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  	memcpy(lnkid->init_sta, init_addr, ETH_ALEN);
  	memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
95224fe83   Arik Nemtsov   mac80211: move TD...
210
  }
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
211
212
213
214
  static void
  ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  {
  	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4df864c1d   Johannes Berg   networking: make ...
215
  	u8 *pos = skb_put(skb, 4);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
216
217
218
219
220
  
  	*pos++ = WLAN_EID_AID;
  	*pos++ = 2; /* len */
  	put_unaligned_le16(ifmgd->aid, pos);
  }
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  /* translate numbering in the WMM parameter IE to the mac80211 notation */
  static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
  {
  	switch (ac) {
  	default:
  		WARN_ON_ONCE(1);
  	case 0:
  		return IEEE80211_AC_BE;
  	case 1:
  		return IEEE80211_AC_BK;
  	case 2:
  		return IEEE80211_AC_VI;
  	case 3:
  		return IEEE80211_AC_VO;
  	}
  }
  
  static u8 ieee80211_wmm_aci_aifsn(int aifsn, bool acm, int aci)
  {
  	u8 ret;
  
  	ret = aifsn & 0x0f;
  	if (acm)
  		ret |= 0x10;
  	ret |= (aci << 5) & 0x60;
  	return ret;
  }
  
  static u8 ieee80211_wmm_ecw(u16 cw_min, u16 cw_max)
  {
  	return ((ilog2(cw_min + 1) << 0x0) & 0x0f) |
  	       ((ilog2(cw_max + 1) << 0x4) & 0xf0);
  }
  
  static void ieee80211_tdls_add_wmm_param_ie(struct ieee80211_sub_if_data *sdata,
  					    struct sk_buff *skb)
  {
  	struct ieee80211_wmm_param_ie *wmm;
  	struct ieee80211_tx_queue_params *txq;
  	int i;
b080db585   Johannes Berg   networking: conve...
261
  	wmm = skb_put_zero(skb, sizeof(*wmm));
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  	wmm->element_id = WLAN_EID_VENDOR_SPECIFIC;
  	wmm->len = sizeof(*wmm) - 2;
  
  	wmm->oui[0] = 0x00; /* Microsoft OUI 00:50:F2 */
  	wmm->oui[1] = 0x50;
  	wmm->oui[2] = 0xf2;
  	wmm->oui_type = 2; /* WME */
  	wmm->oui_subtype = 1; /* WME param */
  	wmm->version = 1; /* WME ver */
  	wmm->qos_info = 0; /* U-APSD not in use */
  
  	/*
  	 * Use the EDCA parameters defined for the BSS, or default if the AP
  	 * doesn't support it, as mandated by 802.11-2012 section 10.22.4
  	 */
  	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  		txq = &sdata->tx_conf[ieee80211_ac_from_wmm(i)];
  		wmm->ac[i].aci_aifsn = ieee80211_wmm_aci_aifsn(txq->aifs,
  							       txq->acm, i);
  		wmm->ac[i].cw = ieee80211_wmm_ecw(txq->cw_min, txq->cw_max);
  		wmm->ac[i].txop_limit = cpu_to_le16(txq->txop);
  	}
  }
f09a87d27   Arik Nemtsov   mac80211: split e...
286
  static void
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
287
288
289
290
291
292
  ieee80211_tdls_chandef_vht_upgrade(struct ieee80211_sub_if_data *sdata,
  				   struct sta_info *sta)
  {
  	/* IEEE802.11ac-2013 Table E-4 */
  	u16 centers_80mhz[] = { 5210, 5290, 5530, 5610, 5690, 5775 };
  	struct cfg80211_chan_def uc = sta->tdls_chandef;
59021c675   Arik Nemtsov   mac80211: TDLS: c...
293
  	enum nl80211_chan_width max_width = ieee80211_sta_cap_chan_bw(sta);
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
294
295
296
297
298
299
300
301
302
  	int i;
  
  	/* only support upgrading non-narrow channels up to 80Mhz */
  	if (max_width == NL80211_CHAN_WIDTH_5 ||
  	    max_width == NL80211_CHAN_WIDTH_10)
  		return;
  
  	if (max_width > NL80211_CHAN_WIDTH_80)
  		max_width = NL80211_CHAN_WIDTH_80;
4b559ec0b   Ilan Peer   mac80211: Fix BW ...
303
  	if (uc.width >= max_width)
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
304
305
306
307
308
309
310
311
312
313
  		return;
  	/*
  	 * Channel usage constrains in the IEEE802.11ac-2013 specification only
  	 * allow expanding a 20MHz channel to 80MHz in a single way. In
  	 * addition, there are no 40MHz allowed channels that are not part of
  	 * the allowed 80MHz range in the 5GHz spectrum (the relevant one here).
  	 */
  	for (i = 0; i < ARRAY_SIZE(centers_80mhz); i++)
  		if (abs(uc.chan->center_freq - centers_80mhz[i]) <= 30) {
  			uc.center_freq1 = centers_80mhz[i];
4b559ec0b   Ilan Peer   mac80211: Fix BW ...
314
  			uc.center_freq2 = 0;
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
315
316
317
318
319
320
  			uc.width = NL80211_CHAN_WIDTH_80;
  			break;
  		}
  
  	if (!uc.center_freq1)
  		return;
554d072e7   Arik Nemtsov   mac80211: TDLS: d...
321
  	/* proceed to downgrade the chandef until usable or the same as AP BW */
db8d99774   Arik Nemtsov   mac80211: TDLS: a...
322
  	while (uc.width > max_width ||
554d072e7   Arik Nemtsov   mac80211: TDLS: d...
323
324
325
  	       (uc.width > sta->tdls_chandef.width &&
  		!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &uc,
  					       sdata->wdev.iftype)))
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  		ieee80211_chandef_downgrade(&uc);
  
  	if (!cfg80211_chandef_identical(&uc, &sta->tdls_chandef)) {
  		tdls_dbg(sdata, "TDLS ch width upgraded %d -> %d
  ",
  			 sta->tdls_chandef.width, uc.width);
  
  		/*
  		 * the station is not yet authorized when BW upgrade is done,
  		 * locking is not required
  		 */
  		sta->tdls_chandef = uc;
  	}
  }
  
  static void
f09a87d27   Arik Nemtsov   mac80211: split e...
342
343
  ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
  				   struct sk_buff *skb, const u8 *peer,
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
344
345
  				   u8 action_code, bool initiator,
  				   const u8 *extra_ies, size_t extra_ies_len)
f09a87d27   Arik Nemtsov   mac80211: split e...
346
  {
13cc8a4a1   Arik Nemtsov   mac80211: support...
347
  	struct ieee80211_supported_band *sband;
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
348
  	struct ieee80211_local *local = sdata->local;
13cc8a4a1   Arik Nemtsov   mac80211: support...
349
  	struct ieee80211_sta_ht_cap ht_cap;
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
350
  	struct ieee80211_sta_vht_cap vht_cap;
13cc8a4a1   Arik Nemtsov   mac80211: support...
351
  	struct sta_info *sta = NULL;
f09a87d27   Arik Nemtsov   mac80211: split e...
352
353
  	size_t offset = 0, noffset;
  	u8 *pos;
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
354
355
356
357
358
359
  	sband = ieee80211_get_sband(sdata);
  	if (!sband)
  		return;
  
  	ieee80211_add_srates_ie(sdata, skb, false, sband->band);
  	ieee80211_add_ext_srates_ie(sdata, skb, false, sband->band);
f0d29cb97   Arik Nemtsov   mac80211: add sup...
360
  	ieee80211_tdls_add_supp_channels(sdata, skb);
f09a87d27   Arik Nemtsov   mac80211: split e...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  
  	/* add any custom IEs that go before Extended Capabilities */
  	if (extra_ies_len) {
  		static const u8 before_ext_cap[] = {
  			WLAN_EID_SUPP_RATES,
  			WLAN_EID_COUNTRY,
  			WLAN_EID_EXT_SUPP_RATES,
  			WLAN_EID_SUPPORTED_CHANNELS,
  			WLAN_EID_RSN,
  		};
  		noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  					     before_ext_cap,
  					     ARRAY_SIZE(before_ext_cap),
  					     offset);
b952f4dff   yuan linyu   net: manual clean...
375
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
f09a87d27   Arik Nemtsov   mac80211: split e...
376
377
  		offset = noffset;
  	}
b98fb44ff   Arik Nemtsov   mac80211: define ...
378
  	ieee80211_tdls_add_ext_capab(sdata, skb);
f09a87d27   Arik Nemtsov   mac80211: split e...
379

40b861a0e   Arik Nemtsov   mac80211: add QoS...
380
381
382
383
  	/* add the QoS element if we support it */
  	if (local->hw.queues >= IEEE80211_NUM_ACS &&
  	    action_code != WLAN_PUB_ACTION_TDLS_DISCOVER_RES)
  		ieee80211_add_wmm_info_ie(skb_put(skb, 9), 0); /* no U-APSD */
f09a87d27   Arik Nemtsov   mac80211: split e...
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  	/* add any custom IEs that go before HT capabilities */
  	if (extra_ies_len) {
  		static const u8 before_ht_cap[] = {
  			WLAN_EID_SUPP_RATES,
  			WLAN_EID_COUNTRY,
  			WLAN_EID_EXT_SUPP_RATES,
  			WLAN_EID_SUPPORTED_CHANNELS,
  			WLAN_EID_RSN,
  			WLAN_EID_EXT_CAPABILITY,
  			WLAN_EID_QOS_CAPA,
  			WLAN_EID_FAST_BSS_TRANSITION,
  			WLAN_EID_TIMEOUT_INTERVAL,
  			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  		};
  		noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  					     before_ht_cap,
  					     ARRAY_SIZE(before_ht_cap),
  					     offset);
b952f4dff   yuan linyu   net: manual clean...
402
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
f09a87d27   Arik Nemtsov   mac80211: split e...
403
404
  		offset = noffset;
  	}
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
405
  	mutex_lock(&local->sta_mtx);
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
406
407
408
409
410
  
  	/* we should have the peer STA if we're already responding */
  	if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
  		sta = sta_info_get(sdata, peer);
  		if (WARN_ON_ONCE(!sta)) {
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
411
  			mutex_unlock(&local->sta_mtx);
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
412
413
  			return;
  		}
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
414
415
  
  		sta->tdls_chandef = sdata->vif.bss_conf.chandef;
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
416
  	}
a38700dd4   Arik Nemtsov   cfg/mac80211: add...
417
  	ieee80211_tdls_add_oper_classes(sdata, skb);
13cc8a4a1   Arik Nemtsov   mac80211: support...
418
419
420
421
422
  	/*
  	 * with TDLS we can switch channels, and HT-caps are not necessarily
  	 * the same on all bands. The specification limits the setup to a
  	 * single HT-cap, so use the current band for now.
  	 */
13cc8a4a1   Arik Nemtsov   mac80211: support...
423
  	memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
c5309ba78   Johannes Berg   mac80211: tdls: d...
424

070e176a7   Arik Nemtsov   mac80211: send HT...
425
426
427
  	if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
  	     action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
  	    ht_cap.ht_supported) {
c5309ba78   Johannes Berg   mac80211: tdls: d...
428
429
430
431
432
433
434
435
436
437
  		ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  
  		/* disable SMPS in TDLS initiator */
  		ht_cap.cap |= WLAN_HT_CAP_SM_PS_DISABLED
  				<< IEEE80211_HT_CAP_SM_PS_SHIFT;
  
  		pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  		ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  	} else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
  		   ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
c5309ba78   Johannes Berg   mac80211: tdls: d...
438
439
  		/* the peer caps are already intersected with our own */
  		memcpy(&ht_cap, &sta->sta.ht_cap, sizeof(ht_cap));
13cc8a4a1   Arik Nemtsov   mac80211: support...
440
441
442
443
  
  		pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  		ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
  	}
2cedd8796   Arik Nemtsov   mac80211: add BSS...
444
445
446
  	if (ht_cap.ht_supported &&
  	    (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  		ieee80211_tdls_add_bss_coex_ie(skb);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
  	ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  
  	/* add any custom IEs that go before VHT capabilities */
  	if (extra_ies_len) {
  		static const u8 before_vht_cap[] = {
  			WLAN_EID_SUPP_RATES,
  			WLAN_EID_COUNTRY,
  			WLAN_EID_EXT_SUPP_RATES,
  			WLAN_EID_SUPPORTED_CHANNELS,
  			WLAN_EID_RSN,
  			WLAN_EID_EXT_CAPABILITY,
  			WLAN_EID_QOS_CAPA,
  			WLAN_EID_FAST_BSS_TRANSITION,
  			WLAN_EID_TIMEOUT_INTERVAL,
  			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  			WLAN_EID_MULTI_BAND,
  		};
  		noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  					     before_vht_cap,
  					     ARRAY_SIZE(before_vht_cap),
  					     offset);
b952f4dff   yuan linyu   net: manual clean...
468
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
469
470
471
472
473
  		offset = noffset;
  	}
  
  	/* build the VHT-cap similarly to the HT-cap */
  	memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
070e176a7   Arik Nemtsov   mac80211: send HT...
474
475
476
  	if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
  	     action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
  	    vht_cap.vht_supported) {
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
477
478
479
  		ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
  
  		/* the AID is present only when VHT is implemented */
070e176a7   Arik Nemtsov   mac80211: send HT...
480
481
  		if (action_code == WLAN_TDLS_SETUP_REQUEST)
  			ieee80211_tdls_add_aid(sdata, skb);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
482
483
484
485
486
487
488
489
490
491
492
493
494
  
  		pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  		ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
  	} else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
  		   vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
  		/* the peer caps are already intersected with our own */
  		memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
  
  		/* the AID is present only when VHT is implemented */
  		ieee80211_tdls_add_aid(sdata, skb);
  
  		pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  		ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
495
496
497
498
499
500
501
  
  		/*
  		 * if both peers support WIDER_BW, we can expand the chandef to
  		 * a wider compatible one, up to 80MHz
  		 */
  		if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
  			ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
502
  	}
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
503
  	mutex_unlock(&local->sta_mtx);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
504

f09a87d27   Arik Nemtsov   mac80211: split e...
505
506
507
  	/* add any remaining IEs */
  	if (extra_ies_len) {
  		noffset = extra_ies_len;
b952f4dff   yuan linyu   net: manual clean...
508
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
f09a87d27   Arik Nemtsov   mac80211: split e...
509
  	}
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
510

f09a87d27   Arik Nemtsov   mac80211: split e...
511
  }
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
512
513
514
515
516
517
518
  static void
  ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
  				 struct sk_buff *skb, const u8 *peer,
  				 bool initiator, const u8 *extra_ies,
  				 size_t extra_ies_len)
  {
  	struct ieee80211_local *local = sdata->local;
13cc8a4a1   Arik Nemtsov   mac80211: support...
519
  	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
520
  	size_t offset = 0, noffset;
13cc8a4a1   Arik Nemtsov   mac80211: support...
521
  	struct sta_info *sta, *ap_sta;
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
522
  	struct ieee80211_supported_band *sband;
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
523
  	u8 *pos;
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
524
525
526
  	sband = ieee80211_get_sband(sdata);
  	if (!sband)
  		return;
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
527
  	mutex_lock(&local->sta_mtx);
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
528
529
  
  	sta = sta_info_get(sdata, peer);
13cc8a4a1   Arik Nemtsov   mac80211: support...
530
531
  	ap_sta = sta_info_get(sdata, ifmgd->bssid);
  	if (WARN_ON_ONCE(!sta || !ap_sta)) {
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
532
  		mutex_unlock(&local->sta_mtx);
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
533
534
  		return;
  	}
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
535
  	sta->tdls_chandef = sdata->vif.bss_conf.chandef;
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
536
537
538
539
540
541
542
543
544
  	/* add any custom IEs that go before the QoS IE */
  	if (extra_ies_len) {
  		static const u8 before_qos[] = {
  			WLAN_EID_RSN,
  		};
  		noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  					     before_qos,
  					     ARRAY_SIZE(before_qos),
  					     offset);
b952f4dff   yuan linyu   net: manual clean...
545
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
546
547
548
549
  		offset = noffset;
  	}
  
  	/* add the QoS param IE if both the peer and we support it */
a74a8c846   Johannes Berg   mac80211: don't d...
550
  	if (local->hw.queues >= IEEE80211_NUM_ACS && sta->sta.wme)
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
551
  		ieee80211_tdls_add_wmm_param_ie(sdata, skb);
13cc8a4a1   Arik Nemtsov   mac80211: support...
552
553
554
555
556
557
558
559
560
561
562
563
  	/* add any custom IEs that go before HT operation */
  	if (extra_ies_len) {
  		static const u8 before_ht_op[] = {
  			WLAN_EID_RSN,
  			WLAN_EID_QOS_CAPA,
  			WLAN_EID_FAST_BSS_TRANSITION,
  			WLAN_EID_TIMEOUT_INTERVAL,
  		};
  		noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  					     before_ht_op,
  					     ARRAY_SIZE(before_ht_op),
  					     offset);
b952f4dff   yuan linyu   net: manual clean...
564
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
13cc8a4a1   Arik Nemtsov   mac80211: support...
565
566
  		offset = noffset;
  	}
57f255f58   Arik Nemtsov   mac80211: TDLS: a...
567
568
569
570
  	/*
  	 * if HT support is only added in TDLS, we need an HT-operation IE.
  	 * add the IE as required by IEEE802.11-2012 9.23.3.2.
  	 */
13cc8a4a1   Arik Nemtsov   mac80211: support...
571
  	if (!ap_sta->sta.ht_cap.ht_supported && sta->sta.ht_cap.ht_supported) {
57f255f58   Arik Nemtsov   mac80211: TDLS: a...
572
573
574
  		u16 prot = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
  			   IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
  			   IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
890b7878e   Arik Nemtsov   mac80211: TDLS: u...
575
  		pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
890b7878e   Arik Nemtsov   mac80211: TDLS: u...
576
  		ieee80211_ie_build_ht_oper(pos, &sta->sta.ht_cap,
57f255f58   Arik Nemtsov   mac80211: TDLS: a...
577
578
  					   &sdata->vif.bss_conf.chandef, prot,
  					   true);
13cc8a4a1   Arik Nemtsov   mac80211: support...
579
  	}
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
580
581
582
  	ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  
  	/* only include VHT-operation if not on the 2.4GHz band */
21a8e9dd5   Mohammed Shafi Shajakhan   mac80211: Fix pos...
583
584
  	if (sband->band != NL80211_BAND_2GHZ &&
  	    sta->sta.vht_cap.vht_supported) {
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
585
586
587
588
589
590
  		/*
  		 * if both peers support WIDER_BW, we can expand the chandef to
  		 * a wider compatible one, up to 80MHz
  		 */
  		if (test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW))
  			ieee80211_tdls_chandef_vht_upgrade(sdata, sta);
890b7878e   Arik Nemtsov   mac80211: TDLS: u...
591
592
  		pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
  		ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
593
  					    &sta->tdls_chandef);
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
594
  	}
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
595
  	mutex_unlock(&local->sta_mtx);
13cc8a4a1   Arik Nemtsov   mac80211: support...
596

6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
597
598
599
  	/* add any remaining IEs */
  	if (extra_ies_len) {
  		noffset = extra_ies_len;
b952f4dff   yuan linyu   net: manual clean...
600
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
601
  	}
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
602
  }
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
603
604
605
606
607
608
609
610
611
  static void
  ieee80211_tdls_add_chan_switch_req_ies(struct ieee80211_sub_if_data *sdata,
  				       struct sk_buff *skb, const u8 *peer,
  				       bool initiator, const u8 *extra_ies,
  				       size_t extra_ies_len, u8 oper_class,
  				       struct cfg80211_chan_def *chandef)
  {
  	struct ieee80211_tdls_data *tf;
  	size_t offset = 0, noffset;
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
  
  	if (WARN_ON_ONCE(!chandef))
  		return;
  
  	tf = (void *)skb->data;
  	tf->u.chan_switch_req.target_channel =
  		ieee80211_frequency_to_channel(chandef->chan->center_freq);
  	tf->u.chan_switch_req.oper_class = oper_class;
  
  	if (extra_ies_len) {
  		static const u8 before_lnkie[] = {
  			WLAN_EID_SECONDARY_CHANNEL_OFFSET,
  		};
  		noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
  					     before_lnkie,
  					     ARRAY_SIZE(before_lnkie),
  					     offset);
b952f4dff   yuan linyu   net: manual clean...
629
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
630
631
632
633
634
635
636
637
  		offset = noffset;
  	}
  
  	ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  
  	/* add any remaining IEs */
  	if (extra_ies_len) {
  		noffset = extra_ies_len;
b952f4dff   yuan linyu   net: manual clean...
638
  		skb_put_data(skb, extra_ies + offset, noffset - offset);
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
639
640
  	}
  }
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
641
642
643
644
645
646
647
648
649
650
651
  static void
  ieee80211_tdls_add_chan_switch_resp_ies(struct ieee80211_sub_if_data *sdata,
  					struct sk_buff *skb, const u8 *peer,
  					u16 status_code, bool initiator,
  					const u8 *extra_ies,
  					size_t extra_ies_len)
  {
  	if (status_code == 0)
  		ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
  
  	if (extra_ies_len)
59ae1d127   Johannes Berg   networking: intro...
652
  		skb_put_data(skb, extra_ies, extra_ies_len);
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
653
  }
46792a2df   Arik Nemtsov   mac80211: consoli...
654
655
  static void ieee80211_tdls_add_ies(struct ieee80211_sub_if_data *sdata,
  				   struct sk_buff *skb, const u8 *peer,
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
656
657
  				   u8 action_code, u16 status_code,
  				   bool initiator, const u8 *extra_ies,
c27339056   Arik Nemtsov   mac80211: prepare...
658
659
  				   size_t extra_ies_len, u8 oper_class,
  				   struct cfg80211_chan_def *chandef)
46792a2df   Arik Nemtsov   mac80211: consoli...
660
  {
46792a2df   Arik Nemtsov   mac80211: consoli...
661
662
663
664
  	switch (action_code) {
  	case WLAN_TDLS_SETUP_REQUEST:
  	case WLAN_TDLS_SETUP_RESPONSE:
  	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
665
666
667
668
669
670
  		if (status_code == 0)
  			ieee80211_tdls_add_setup_start_ies(sdata, skb, peer,
  							   action_code,
  							   initiator,
  							   extra_ies,
  							   extra_ies_len);
46792a2df   Arik Nemtsov   mac80211: consoli...
671
672
  		break;
  	case WLAN_TDLS_SETUP_CONFIRM:
6f7eaa47e   Arik Nemtsov   mac80211: add TDL...
673
674
675
676
677
  		if (status_code == 0)
  			ieee80211_tdls_add_setup_cfm_ies(sdata, skb, peer,
  							 initiator, extra_ies,
  							 extra_ies_len);
  		break;
46792a2df   Arik Nemtsov   mac80211: consoli...
678
679
  	case WLAN_TDLS_TEARDOWN:
  	case WLAN_TDLS_DISCOVERY_REQUEST:
f09a87d27   Arik Nemtsov   mac80211: split e...
680
  		if (extra_ies_len)
59ae1d127   Johannes Berg   networking: intro...
681
  			skb_put_data(skb, extra_ies, extra_ies_len);
1606ef4a9   Arik Nemtsov   mac80211: avoid a...
682
683
  		if (status_code == 0 || action_code == WLAN_TDLS_TEARDOWN)
  			ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
46792a2df   Arik Nemtsov   mac80211: consoli...
684
  		break;
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
685
686
687
688
689
690
  	case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  		ieee80211_tdls_add_chan_switch_req_ies(sdata, skb, peer,
  						       initiator, extra_ies,
  						       extra_ies_len,
  						       oper_class, chandef);
  		break;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
691
692
693
694
695
696
  	case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  		ieee80211_tdls_add_chan_switch_resp_ies(sdata, skb, peer,
  							status_code,
  							initiator, extra_ies,
  							extra_ies_len);
  		break;
46792a2df   Arik Nemtsov   mac80211: consoli...
697
  	}
46792a2df   Arik Nemtsov   mac80211: consoli...
698
  }
95224fe83   Arik Nemtsov   mac80211: move TD...
699
700
  static int
  ieee80211_prep_tdls_encap_data(struct wiphy *wiphy, struct net_device *dev,
3b3a0162f   Johannes Berg   cfg80211: constif...
701
  			       const u8 *peer, u8 action_code, u8 dialog_token,
95224fe83   Arik Nemtsov   mac80211: move TD...
702
703
704
  			       u16 status_code, struct sk_buff *skb)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
95224fe83   Arik Nemtsov   mac80211: move TD...
705
  	struct ieee80211_tdls_data *tf;
4df864c1d   Johannes Berg   networking: make ...
706
  	tf = skb_put(skb, offsetof(struct ieee80211_tdls_data, u));
95224fe83   Arik Nemtsov   mac80211: move TD...
707
708
709
710
711
  
  	memcpy(tf->da, peer, ETH_ALEN);
  	memcpy(tf->sa, sdata->vif.addr, ETH_ALEN);
  	tf->ether_type = cpu_to_be16(ETH_P_TDLS);
  	tf->payload_type = WLAN_TDLS_SNAP_RFTYPE;
59cd85cbc   Arik Nemtsov   mac80211: set net...
712
713
  	/* network header is after the ethernet header */
  	skb_set_network_header(skb, ETH_HLEN);
95224fe83   Arik Nemtsov   mac80211: move TD...
714
715
716
717
718
719
720
721
  	switch (action_code) {
  	case WLAN_TDLS_SETUP_REQUEST:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_SETUP_REQUEST;
  
  		skb_put(skb, sizeof(tf->u.setup_req));
  		tf->u.setup_req.dialog_token = dialog_token;
  		tf->u.setup_req.capability =
dd8c0b03d   Arik Nemtsov   mac80211: set TDL...
722
723
  			cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  								 status_code));
95224fe83   Arik Nemtsov   mac80211: move TD...
724
725
726
727
728
729
730
731
732
  		break;
  	case WLAN_TDLS_SETUP_RESPONSE:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_SETUP_RESPONSE;
  
  		skb_put(skb, sizeof(tf->u.setup_resp));
  		tf->u.setup_resp.status_code = cpu_to_le16(status_code);
  		tf->u.setup_resp.dialog_token = dialog_token;
  		tf->u.setup_resp.capability =
dd8c0b03d   Arik Nemtsov   mac80211: set TDL...
733
734
  			cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  								 status_code));
95224fe83   Arik Nemtsov   mac80211: move TD...
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
  		break;
  	case WLAN_TDLS_SETUP_CONFIRM:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_SETUP_CONFIRM;
  
  		skb_put(skb, sizeof(tf->u.setup_cfm));
  		tf->u.setup_cfm.status_code = cpu_to_le16(status_code);
  		tf->u.setup_cfm.dialog_token = dialog_token;
  		break;
  	case WLAN_TDLS_TEARDOWN:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_TEARDOWN;
  
  		skb_put(skb, sizeof(tf->u.teardown));
  		tf->u.teardown.reason_code = cpu_to_le16(status_code);
  		break;
  	case WLAN_TDLS_DISCOVERY_REQUEST:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_DISCOVERY_REQUEST;
  
  		skb_put(skb, sizeof(tf->u.discover_req));
  		tf->u.discover_req.dialog_token = dialog_token;
  		break;
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
758
759
760
761
762
763
  	case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
  
  		skb_put(skb, sizeof(tf->u.chan_switch_req));
  		break;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
764
765
766
767
768
769
770
  	case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  		tf->category = WLAN_CATEGORY_TDLS;
  		tf->action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
  
  		skb_put(skb, sizeof(tf->u.chan_switch_resp));
  		tf->u.chan_switch_resp.status_code = cpu_to_le16(status_code);
  		break;
95224fe83   Arik Nemtsov   mac80211: move TD...
771
772
773
774
775
776
777
778
779
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int
  ieee80211_prep_tdls_direct(struct wiphy *wiphy, struct net_device *dev,
3b3a0162f   Johannes Berg   cfg80211: constif...
780
  			   const u8 *peer, u8 action_code, u8 dialog_token,
95224fe83   Arik Nemtsov   mac80211: move TD...
781
782
783
  			   u16 status_code, struct sk_buff *skb)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
95224fe83   Arik Nemtsov   mac80211: move TD...
784
  	struct ieee80211_mgmt *mgmt;
b080db585   Johannes Berg   networking: conve...
785
  	mgmt = skb_put_zero(skb, 24);
95224fe83   Arik Nemtsov   mac80211: move TD...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
  	memcpy(mgmt->da, peer, ETH_ALEN);
  	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  	memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  
  	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  					  IEEE80211_STYPE_ACTION);
  
  	switch (action_code) {
  	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  		skb_put(skb, 1 + sizeof(mgmt->u.action.u.tdls_discover_resp));
  		mgmt->u.action.category = WLAN_CATEGORY_PUBLIC;
  		mgmt->u.action.u.tdls_discover_resp.action_code =
  			WLAN_PUB_ACTION_TDLS_DISCOVER_RES;
  		mgmt->u.action.u.tdls_discover_resp.dialog_token =
  			dialog_token;
  		mgmt->u.action.u.tdls_discover_resp.capability =
dd8c0b03d   Arik Nemtsov   mac80211: set TDL...
802
803
  			cpu_to_le16(ieee80211_get_tdls_sta_capab(sdata,
  								 status_code));
95224fe83   Arik Nemtsov   mac80211: move TD...
804
805
806
807
808
809
810
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
c27339056   Arik Nemtsov   mac80211: prepare...
811
812
813
814
815
816
817
  static struct sk_buff *
  ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
  				      const u8 *peer, u8 action_code,
  				      u8 dialog_token, u16 status_code,
  				      bool initiator, const u8 *extra_ies,
  				      size_t extra_ies_len, u8 oper_class,
  				      struct cfg80211_chan_def *chandef)
95224fe83   Arik Nemtsov   mac80211: move TD...
818
  {
95224fe83   Arik Nemtsov   mac80211: move TD...
819
  	struct ieee80211_local *local = sdata->local;
c27339056   Arik Nemtsov   mac80211: prepare...
820
  	struct sk_buff *skb;
95224fe83   Arik Nemtsov   mac80211: move TD...
821
  	int ret;
c27339056   Arik Nemtsov   mac80211: prepare...
822
  	skb = netdev_alloc_skb(sdata->dev,
1277b4a9f   Liad Kaufman   mac80211: retrans...
823
824
825
826
  			       local->hw.extra_tx_headroom +
  			       max(sizeof(struct ieee80211_mgmt),
  				   sizeof(struct ieee80211_tdls_data)) +
  			       50 + /* supported rates */
b98fb44ff   Arik Nemtsov   mac80211: define ...
827
  			       10 + /* ext capab */
1277b4a9f   Liad Kaufman   mac80211: retrans...
828
829
830
  			       26 + /* max(WMM-info, WMM-param) */
  			       2 + max(sizeof(struct ieee80211_ht_cap),
  				       sizeof(struct ieee80211_ht_operation)) +
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
831
832
  			       2 + max(sizeof(struct ieee80211_vht_cap),
  				       sizeof(struct ieee80211_vht_operation)) +
f0d29cb97   Arik Nemtsov   mac80211: add sup...
833
  			       50 + /* supported channels */
2cedd8796   Arik Nemtsov   mac80211: add BSS...
834
  			       3 + /* 40/20 BSS coex */
fb28ec0ce   Arik Nemtsov   mac80211: TDLS: s...
835
  			       4 + /* AID */
a38700dd4   Arik Nemtsov   cfg/mac80211: add...
836
  			       4 + /* oper classes */
1277b4a9f   Liad Kaufman   mac80211: retrans...
837
838
  			       extra_ies_len +
  			       sizeof(struct ieee80211_tdls_lnkie));
95224fe83   Arik Nemtsov   mac80211: move TD...
839
  	if (!skb)
c27339056   Arik Nemtsov   mac80211: prepare...
840
  		return NULL;
95224fe83   Arik Nemtsov   mac80211: move TD...
841
842
843
844
845
846
847
848
849
  
  	skb_reserve(skb, local->hw.extra_tx_headroom);
  
  	switch (action_code) {
  	case WLAN_TDLS_SETUP_REQUEST:
  	case WLAN_TDLS_SETUP_RESPONSE:
  	case WLAN_TDLS_SETUP_CONFIRM:
  	case WLAN_TDLS_TEARDOWN:
  	case WLAN_TDLS_DISCOVERY_REQUEST:
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
850
  	case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
851
  	case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
c27339056   Arik Nemtsov   mac80211: prepare...
852
853
  		ret = ieee80211_prep_tdls_encap_data(local->hw.wiphy,
  						     sdata->dev, peer,
95224fe83   Arik Nemtsov   mac80211: move TD...
854
855
  						     action_code, dialog_token,
  						     status_code, skb);
95224fe83   Arik Nemtsov   mac80211: move TD...
856
857
  		break;
  	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
c27339056   Arik Nemtsov   mac80211: prepare...
858
859
  		ret = ieee80211_prep_tdls_direct(local->hw.wiphy, sdata->dev,
  						 peer, action_code,
95224fe83   Arik Nemtsov   mac80211: move TD...
860
861
  						 dialog_token, status_code,
  						 skb);
95224fe83   Arik Nemtsov   mac80211: move TD...
862
863
864
865
866
867
868
869
  		break;
  	default:
  		ret = -ENOTSUPP;
  		break;
  	}
  
  	if (ret < 0)
  		goto fail;
c27339056   Arik Nemtsov   mac80211: prepare...
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  	ieee80211_tdls_add_ies(sdata, skb, peer, action_code, status_code,
  			       initiator, extra_ies, extra_ies_len, oper_class,
  			       chandef);
  	return skb;
  
  fail:
  	dev_kfree_skb(skb);
  	return NULL;
  }
  
  static int
  ieee80211_tdls_prep_mgmt_packet(struct wiphy *wiphy, struct net_device *dev,
  				const u8 *peer, u8 action_code, u8 dialog_token,
  				u16 status_code, u32 peer_capability,
  				bool initiator, const u8 *extra_ies,
  				size_t extra_ies_len, u8 oper_class,
  				struct cfg80211_chan_def *chandef)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  	struct sk_buff *skb = NULL;
  	struct sta_info *sta;
  	u32 flags = 0;
  	int ret = 0;
626911cc6   Arik Nemtsov   mac80211: track T...
893
894
895
896
  	rcu_read_lock();
  	sta = sta_info_get(sdata, peer);
  
  	/* infer the initiator if we can, to support old userspace */
95224fe83   Arik Nemtsov   mac80211: move TD...
897
898
  	switch (action_code) {
  	case WLAN_TDLS_SETUP_REQUEST:
8b94148cf   Arik Nemtsov   mac80211: expose ...
899
  		if (sta) {
626911cc6   Arik Nemtsov   mac80211: track T...
900
  			set_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
8b94148cf   Arik Nemtsov   mac80211: expose ...
901
902
  			sta->sta.tdls_initiator = false;
  		}
626911cc6   Arik Nemtsov   mac80211: track T...
903
  		/* fall-through */
95224fe83   Arik Nemtsov   mac80211: move TD...
904
  	case WLAN_TDLS_SETUP_CONFIRM:
95224fe83   Arik Nemtsov   mac80211: move TD...
905
  	case WLAN_TDLS_DISCOVERY_REQUEST:
626911cc6   Arik Nemtsov   mac80211: track T...
906
  		initiator = true;
95224fe83   Arik Nemtsov   mac80211: move TD...
907
908
  		break;
  	case WLAN_TDLS_SETUP_RESPONSE:
626911cc6   Arik Nemtsov   mac80211: track T...
909
910
911
912
913
  		/*
  		 * In some testing scenarios, we send a request and response.
  		 * Make the last packet sent take effect for the initiator
  		 * value.
  		 */
8b94148cf   Arik Nemtsov   mac80211: expose ...
914
  		if (sta) {
626911cc6   Arik Nemtsov   mac80211: track T...
915
  			clear_sta_flag(sta, WLAN_STA_TDLS_INITIATOR);
8b94148cf   Arik Nemtsov   mac80211: expose ...
916
917
  			sta->sta.tdls_initiator = true;
  		}
626911cc6   Arik Nemtsov   mac80211: track T...
918
  		/* fall-through */
95224fe83   Arik Nemtsov   mac80211: move TD...
919
  	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
626911cc6   Arik Nemtsov   mac80211: track T...
920
  		initiator = false;
2fb6b9b8e   Arik Nemtsov   mac80211: use TDL...
921
922
  		break;
  	case WLAN_TDLS_TEARDOWN:
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
923
  	case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
924
  	case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
2fb6b9b8e   Arik Nemtsov   mac80211: use TDL...
925
  		/* any value is ok */
95224fe83   Arik Nemtsov   mac80211: move TD...
926
927
928
  		break;
  	default:
  		ret = -ENOTSUPP;
626911cc6   Arik Nemtsov   mac80211: track T...
929
  		break;
95224fe83   Arik Nemtsov   mac80211: move TD...
930
  	}
46792a2df   Arik Nemtsov   mac80211: consoli...
931
932
  	if (sta && test_sta_flag(sta, WLAN_STA_TDLS_INITIATOR))
  		initiator = true;
2fb6b9b8e   Arik Nemtsov   mac80211: use TDL...
933

626911cc6   Arik Nemtsov   mac80211: track T...
934
935
936
  	rcu_read_unlock();
  	if (ret < 0)
  		goto fail;
c27339056   Arik Nemtsov   mac80211: prepare...
937
938
939
940
941
942
943
944
945
946
947
  	skb = ieee80211_tdls_build_mgmt_packet_data(sdata, peer, action_code,
  						    dialog_token, status_code,
  						    initiator, extra_ies,
  						    extra_ies_len, oper_class,
  						    chandef);
  	if (!skb) {
  		ret = -EINVAL;
  		goto fail;
  	}
  
  	if (action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) {
95224fe83   Arik Nemtsov   mac80211: move TD...
948
949
950
951
952
953
954
955
956
957
958
  		ieee80211_tx_skb(sdata, skb);
  		return 0;
  	}
  
  	/*
  	 * According to 802.11z: Setup req/resp are sent in AC_BK, otherwise
  	 * we should default to AC_VI.
  	 */
  	switch (action_code) {
  	case WLAN_TDLS_SETUP_REQUEST:
  	case WLAN_TDLS_SETUP_RESPONSE:
ebec37ed2   Johannes Berg   mac80211: TDLS: f...
959
  		skb->priority = 256 + 2;
95224fe83   Arik Nemtsov   mac80211: move TD...
960
961
  		break;
  	default:
ebec37ed2   Johannes Berg   mac80211: TDLS: f...
962
  		skb->priority = 256 + 5;
95224fe83   Arik Nemtsov   mac80211: move TD...
963
964
  		break;
  	}
ebec37ed2   Johannes Berg   mac80211: TDLS: f...
965
  	skb_set_queue_mapping(skb, ieee80211_select_queue(sdata, skb));
95224fe83   Arik Nemtsov   mac80211: move TD...
966

1277b4a9f   Liad Kaufman   mac80211: retrans...
967
968
969
970
971
972
  	/*
  	 * Set the WLAN_TDLS_TEARDOWN flag to indicate a teardown in progress.
  	 * Later, if no ACK is returned from peer, we will re-send the teardown
  	 * packet through the AP.
  	 */
  	if ((action_code == WLAN_TDLS_TEARDOWN) &&
30686bf7f   Johannes Berg   mac80211: convert...
973
  	    ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
1277b4a9f   Liad Kaufman   mac80211: retrans...
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
  		bool try_resend; /* Should we keep skb for possible resend */
  
  		/* If not sending directly to peer - no point in keeping skb */
  		rcu_read_lock();
  		sta = sta_info_get(sdata, peer);
  		try_resend = sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  		rcu_read_unlock();
  
  		spin_lock_bh(&sdata->u.mgd.teardown_lock);
  		if (try_resend && !sdata->u.mgd.teardown_skb) {
  			/* Mark it as requiring TX status callback  */
  			flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
  				 IEEE80211_TX_INTFL_MLME_CONN_TX;
  
  			/*
  			 * skb is copied since mac80211 will later set
  			 * properties that might not be the same as the AP,
  			 * such as encryption, QoS, addresses, etc.
  			 *
  			 * No problem if skb_copy() fails, so no need to check.
  			 */
  			sdata->u.mgd.teardown_skb = skb_copy(skb, GFP_ATOMIC);
  			sdata->u.mgd.orig_teardown_skb = skb;
  		}
  		spin_unlock_bh(&sdata->u.mgd.teardown_lock);
  	}
95224fe83   Arik Nemtsov   mac80211: move TD...
1000
1001
  	/* disable bottom halves when entering the Tx path */
  	local_bh_disable();
1277b4a9f   Liad Kaufman   mac80211: retrans...
1002
  	__ieee80211_subif_start_xmit(skb, dev, flags);
95224fe83   Arik Nemtsov   mac80211: move TD...
1003
1004
1005
1006
1007
1008
1009
1010
  	local_bh_enable();
  
  	return ret;
  
  fail:
  	dev_kfree_skb(skb);
  	return ret;
  }
191dd4690   Arik Nemtsov   mac80211: split t...
1011
1012
1013
1014
1015
  static int
  ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
  			  const u8 *peer, u8 action_code, u8 dialog_token,
  			  u16 status_code, u32 peer_capability, bool initiator,
  			  const u8 *extra_ies, size_t extra_ies_len)
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1016
1017
1018
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  	struct ieee80211_local *local = sdata->local;
d51c2ea37   Arik Nemtsov   mac80211: TDLS: c...
1019
  	enum ieee80211_smps_mode smps_mode = sdata->u.mgd.driver_smps_mode;
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1020
  	int ret;
d51c2ea37   Arik Nemtsov   mac80211: TDLS: c...
1021
1022
1023
1024
1025
1026
1027
1028
  	/* don't support setup with forced SMPS mode that's not off */
  	if (smps_mode != IEEE80211_SMPS_AUTOMATIC &&
  	    smps_mode != IEEE80211_SMPS_OFF) {
  		tdls_dbg(sdata, "Aborting TDLS setup due to SMPS mode %d
  ",
  			 smps_mode);
  		return -ENOTSUPP;
  	}
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1029
1030
1031
  	mutex_lock(&local->mtx);
  
  	/* we don't support concurrent TDLS peer setups */
81dd2b882   Arik Nemtsov   mac80211: move TD...
1032
1033
  	if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
  	    !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1034
  		ret = -EBUSY;
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1035
  		goto out_unlock;
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1036
  	}
7adc3e466   Arik Nemtsov   mac80211: make su...
1037
1038
1039
  	/*
  	 * make sure we have a STA representing the peer so we drop or buffer
  	 * non-TDLS-setup frames to the peer. We can't send other packets
6ae32e5d2   Arik Nemtsov   mac80211: fix err...
1040
1041
1042
  	 * during setup through the AP path.
  	 * Allow error packets to be sent - sometimes we don't even add a STA
  	 * before failing the setup.
7adc3e466   Arik Nemtsov   mac80211: make su...
1043
  	 */
6ae32e5d2   Arik Nemtsov   mac80211: fix err...
1044
1045
1046
1047
1048
  	if (status_code == 0) {
  		rcu_read_lock();
  		if (!sta_info_get(sdata, peer)) {
  			rcu_read_unlock();
  			ret = -ENOLINK;
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1049
  			goto out_unlock;
6ae32e5d2   Arik Nemtsov   mac80211: fix err...
1050
  		}
7adc3e466   Arik Nemtsov   mac80211: make su...
1051
  		rcu_read_unlock();
7adc3e466   Arik Nemtsov   mac80211: make su...
1052
  	}
7adc3e466   Arik Nemtsov   mac80211: make su...
1053

3b24f4c65   Emmanuel Grumbach   mac80211: let flu...
1054
  	ieee80211_flush_queues(local, sdata, false);
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1055
1056
  	memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
  	mutex_unlock(&local->mtx);
db67d661e   Arik Nemtsov   mac80211: impleme...
1057

ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1058
  	/* we cannot take the mutex while preparing the setup packet */
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1059
1060
  	ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
  					      dialog_token, status_code,
2fb6b9b8e   Arik Nemtsov   mac80211: use TDL...
1061
  					      peer_capability, initiator,
c27339056   Arik Nemtsov   mac80211: prepare...
1062
1063
  					      extra_ies, extra_ies_len, 0,
  					      NULL);
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1064
1065
1066
1067
1068
1069
  	if (ret < 0) {
  		mutex_lock(&local->mtx);
  		eth_zero_addr(sdata->u.mgd.tdls_peer);
  		mutex_unlock(&local->mtx);
  		return ret;
  	}
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1070

191dd4690   Arik Nemtsov   mac80211: split t...
1071
  	ieee80211_queue_delayed_work(&sdata->local->hw,
81dd2b882   Arik Nemtsov   mac80211: move TD...
1072
  				     &sdata->u.mgd.tdls_peer_del_work,
191dd4690   Arik Nemtsov   mac80211: split t...
1073
  				     TDLS_PEER_SETUP_TIMEOUT);
ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1074
  	return 0;
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1075

ae2e9fba8   Arik Nemtsov   mac80211: allow T...
1076
  out_unlock:
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1077
  	mutex_unlock(&local->mtx);
191dd4690   Arik Nemtsov   mac80211: split t...
1078
1079
  	return ret;
  }
db67d661e   Arik Nemtsov   mac80211: impleme...
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
  static int
  ieee80211_tdls_mgmt_teardown(struct wiphy *wiphy, struct net_device *dev,
  			     const u8 *peer, u8 action_code, u8 dialog_token,
  			     u16 status_code, u32 peer_capability,
  			     bool initiator, const u8 *extra_ies,
  			     size_t extra_ies_len)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  	struct ieee80211_local *local = sdata->local;
  	struct sta_info *sta;
  	int ret;
  
  	/*
  	 * No packets can be transmitted to the peer via the AP during setup -
  	 * the STA is set as a TDLS peer, but is not authorized.
  	 * During teardown, we prevent direct transmissions by stopping the
  	 * queues and flushing all direct packets.
  	 */
  	ieee80211_stop_vif_queues(local, sdata,
  				  IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
3b24f4c65   Emmanuel Grumbach   mac80211: let flu...
1100
  	ieee80211_flush_queues(local, sdata, false);
db67d661e   Arik Nemtsov   mac80211: impleme...
1101
1102
1103
1104
  
  	ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
  					      dialog_token, status_code,
  					      peer_capability, initiator,
c27339056   Arik Nemtsov   mac80211: prepare...
1105
1106
  					      extra_ies, extra_ies_len, 0,
  					      NULL);
db67d661e   Arik Nemtsov   mac80211: impleme...
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
  	if (ret < 0)
  		sdata_err(sdata, "Failed sending TDLS teardown packet %d
  ",
  			  ret);
  
  	/*
  	 * Remove the STA AUTH flag to force further traffic through the AP. If
  	 * the STA was unreachable, it was already removed.
  	 */
  	rcu_read_lock();
  	sta = sta_info_get(sdata, peer);
  	if (sta)
  		clear_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
  	rcu_read_unlock();
  
  	ieee80211_wake_vif_queues(local, sdata,
  				  IEEE80211_QUEUE_STOP_REASON_TDLS_TEARDOWN);
  
  	return 0;
  }
191dd4690   Arik Nemtsov   mac80211: split t...
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
  int ieee80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
  			const u8 *peer, u8 action_code, u8 dialog_token,
  			u16 status_code, u32 peer_capability,
  			bool initiator, const u8 *extra_ies,
  			size_t extra_ies_len)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  	int ret;
  
  	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  		return -ENOTSUPP;
  
  	/* make sure we are in managed mode, and associated */
  	if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  	    !sdata->u.mgd.associated)
  		return -EINVAL;
  
  	switch (action_code) {
  	case WLAN_TDLS_SETUP_REQUEST:
  	case WLAN_TDLS_SETUP_RESPONSE:
  		ret = ieee80211_tdls_mgmt_setup(wiphy, dev, peer, action_code,
  						dialog_token, status_code,
  						peer_capability, initiator,
  						extra_ies, extra_ies_len);
  		break;
  	case WLAN_TDLS_TEARDOWN:
db67d661e   Arik Nemtsov   mac80211: impleme...
1153
1154
1155
1156
1157
1158
  		ret = ieee80211_tdls_mgmt_teardown(wiphy, dev, peer,
  						   action_code, dialog_token,
  						   status_code,
  						   peer_capability, initiator,
  						   extra_ies, extra_ies_len);
  		break;
191dd4690   Arik Nemtsov   mac80211: split t...
1159
  	case WLAN_TDLS_DISCOVERY_REQUEST:
ee10f2c77   Arik Nemtsov   mac80211: protect...
1160
1161
1162
1163
1164
1165
1166
1167
  		/*
  		 * Protect the discovery so we can hear the TDLS discovery
  		 * response frame. It is transmitted directly and not buffered
  		 * by the AP.
  		 */
  		drv_mgd_protect_tdls_discover(sdata->local, sdata);
  		/* fall-through */
  	case WLAN_TDLS_SETUP_CONFIRM:
191dd4690   Arik Nemtsov   mac80211: split t...
1168
1169
1170
1171
1172
1173
1174
1175
  	case WLAN_PUB_ACTION_TDLS_DISCOVER_RES:
  		/* no special handling */
  		ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer,
  						      action_code,
  						      dialog_token,
  						      status_code,
  						      peer_capability,
  						      initiator, extra_ies,
c27339056   Arik Nemtsov   mac80211: prepare...
1176
  						      extra_ies_len, 0, NULL);
191dd4690   Arik Nemtsov   mac80211: split t...
1177
1178
1179
1180
1181
  		break;
  	default:
  		ret = -EOPNOTSUPP;
  		break;
  	}
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1182
1183
1184
1185
1186
1187
  
  	tdls_dbg(sdata, "TDLS mgmt action %d peer %pM status %d
  ",
  		 action_code, peer, ret);
  	return ret;
  }
59021c675   Arik Nemtsov   mac80211: TDLS: c...
1188
1189
  static void iee80211_tdls_recalc_chanctx(struct ieee80211_sub_if_data *sdata,
  					 struct sta_info *sta)
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
1190
1191
1192
1193
  {
  	struct ieee80211_local *local = sdata->local;
  	struct ieee80211_chanctx_conf *conf;
  	struct ieee80211_chanctx *ctx;
59021c675   Arik Nemtsov   mac80211: TDLS: c...
1194
1195
  	enum nl80211_chan_width width;
  	struct ieee80211_supported_band *sband;
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
1196
1197
1198
1199
1200
  
  	mutex_lock(&local->chanctx_mtx);
  	conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  					 lockdep_is_held(&local->chanctx_mtx));
  	if (conf) {
59021c675   Arik Nemtsov   mac80211: TDLS: c...
1201
1202
  		width = conf->def.width;
  		sband = local->hw.wiphy->bands[conf->def.chan->band];
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
1203
1204
  		ctx = container_of(conf, struct ieee80211_chanctx, conf);
  		ieee80211_recalc_chanctx_chantype(local, ctx);
59021c675   Arik Nemtsov   mac80211: TDLS: c...
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
  
  		/* if width changed and a peer is given, update its BW */
  		if (width != conf->def.width && sta &&
  		    test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW)) {
  			enum ieee80211_sta_rx_bandwidth bw;
  
  			bw = ieee80211_chan_width_to_rx_bw(conf->def.width);
  			bw = min(bw, ieee80211_sta_cap_rx_bw(sta));
  			if (bw != sta->sta.bandwidth) {
  				sta->sta.bandwidth = bw;
  				rate_control_rate_update(local, sband, sta,
  							 IEEE80211_RC_BW_CHANGED);
  				/*
  				 * if a TDLS peer BW was updated, we need to
  				 * recalc the chandef width again, to get the
  				 * correct chanctx min_def
  				 */
  				ieee80211_recalc_chanctx_chantype(local, ctx);
  			}
  		}
0fabfaafe   Arik Nemtsov   mac80211: upgrade...
1225
1226
1227
  	}
  	mutex_unlock(&local->chanctx_mtx);
  }
22f66895e   Avri Altman   mac80211: protect...
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
  static int iee80211_tdls_have_ht_peers(struct ieee80211_sub_if_data *sdata)
  {
  	struct sta_info *sta;
  	bool result = false;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  		if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
  		    !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
  		    !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH) ||
  		    !sta->sta.ht_cap.ht_supported)
  			continue;
  		result = true;
  		break;
  	}
  	rcu_read_unlock();
  
  	return result;
  }
  
  static void
  iee80211_tdls_recalc_ht_protection(struct ieee80211_sub_if_data *sdata,
  				   struct sta_info *sta)
  {
  	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  	bool tdls_ht;
  	u16 protection = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED |
  			 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
  			 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
  	u16 opmode;
  
  	/* Nothing to do if the BSS connection uses HT */
  	if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  		return;
  
  	tdls_ht = (sta && sta->sta.ht_cap.ht_supported) ||
  		  iee80211_tdls_have_ht_peers(sdata);
  
  	opmode = sdata->vif.bss_conf.ht_operation_mode;
  
  	if (tdls_ht)
  		opmode |= protection;
  	else
  		opmode &= ~protection;
  
  	if (opmode == sdata->vif.bss_conf.ht_operation_mode)
  		return;
  
  	sdata->vif.bss_conf.ht_operation_mode = opmode;
  	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
  }
95224fe83   Arik Nemtsov   mac80211: move TD...
1279
  int ieee80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
3b3a0162f   Johannes Berg   cfg80211: constif...
1280
  			const u8 *peer, enum nl80211_tdls_operation oper)
95224fe83   Arik Nemtsov   mac80211: move TD...
1281
1282
1283
  {
  	struct sta_info *sta;
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1284
1285
  	struct ieee80211_local *local = sdata->local;
  	int ret;
95224fe83   Arik Nemtsov   mac80211: move TD...
1286
1287
1288
1289
1290
1291
  
  	if (!(wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS))
  		return -ENOTSUPP;
  
  	if (sdata->vif.type != NL80211_IFTYPE_STATION)
  		return -EINVAL;
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
  	switch (oper) {
  	case NL80211_TDLS_ENABLE_LINK:
  	case NL80211_TDLS_DISABLE_LINK:
  		break;
  	case NL80211_TDLS_TEARDOWN:
  	case NL80211_TDLS_SETUP:
  	case NL80211_TDLS_DISCOVERY_REQ:
  		/* We don't support in-driver setup/teardown/discovery */
  		return -ENOTSUPP;
  	}
22f66895e   Avri Altman   mac80211: protect...
1302
1303
1304
1305
  	/* protect possible bss_conf changes and avoid concurrency in
  	 * ieee80211_bss_info_change_notify()
  	 */
  	sdata_lock(sdata);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1306
  	mutex_lock(&local->mtx);
95224fe83   Arik Nemtsov   mac80211: move TD...
1307
1308
1309
1310
1311
  	tdls_dbg(sdata, "TDLS oper %d peer %pM
  ", oper, peer);
  
  	switch (oper) {
  	case NL80211_TDLS_ENABLE_LINK:
c5a71688e   Arik Nemtsov   mac80211: disconn...
1312
1313
1314
1315
1316
1317
  		if (sdata->vif.csa_active) {
  			tdls_dbg(sdata, "TDLS: disallow link during CSA
  ");
  			ret = -EBUSY;
  			break;
  		}
22f66895e   Avri Altman   mac80211: protect...
1318
  		mutex_lock(&local->sta_mtx);
95224fe83   Arik Nemtsov   mac80211: move TD...
1319
1320
  		sta = sta_info_get(sdata, peer);
  		if (!sta) {
22f66895e   Avri Altman   mac80211: protect...
1321
  			mutex_unlock(&local->sta_mtx);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1322
1323
  			ret = -ENOLINK;
  			break;
95224fe83   Arik Nemtsov   mac80211: move TD...
1324
  		}
59021c675   Arik Nemtsov   mac80211: TDLS: c...
1325
  		iee80211_tdls_recalc_chanctx(sdata, sta);
22f66895e   Avri Altman   mac80211: protect...
1326
  		iee80211_tdls_recalc_ht_protection(sdata, sta);
95224fe83   Arik Nemtsov   mac80211: move TD...
1327
  		set_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH);
22f66895e   Avri Altman   mac80211: protect...
1328
  		mutex_unlock(&local->sta_mtx);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1329

81dd2b882   Arik Nemtsov   mac80211: move TD...
1330
1331
  		WARN_ON_ONCE(is_zero_ether_addr(sdata->u.mgd.tdls_peer) ||
  			     !ether_addr_equal(sdata->u.mgd.tdls_peer, peer));
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1332
  		ret = 0;
95224fe83   Arik Nemtsov   mac80211: move TD...
1333
1334
  		break;
  	case NL80211_TDLS_DISABLE_LINK:
bb3f84860   Liad Kaufman   mac80211: make su...
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
  		/*
  		 * The teardown message in ieee80211_tdls_mgmt_teardown() was
  		 * created while the queues were stopped, so it might still be
  		 * pending. Before flushing the queues we need to be sure the
  		 * message is handled by the tasklet handling pending messages,
  		 * otherwise we might start destroying the station before
  		 * sending the teardown packet.
  		 * Note that this only forces the tasklet to flush pendings -
  		 * not to stop the tasklet from rescheduling itself.
  		 */
  		tasklet_kill(&local->tx_pending_tasklet);
db67d661e   Arik Nemtsov   mac80211: impleme...
1346
  		/* flush a potentially queued teardown packet */
3b24f4c65   Emmanuel Grumbach   mac80211: let flu...
1347
  		ieee80211_flush_queues(local, sdata, false);
db67d661e   Arik Nemtsov   mac80211: impleme...
1348

17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1349
  		ret = sta_info_destroy_addr(sdata, peer);
22f66895e   Avri Altman   mac80211: protect...
1350
1351
1352
1353
  
  		mutex_lock(&local->sta_mtx);
  		iee80211_tdls_recalc_ht_protection(sdata, NULL);
  		mutex_unlock(&local->sta_mtx);
59021c675   Arik Nemtsov   mac80211: TDLS: c...
1354
  		iee80211_tdls_recalc_chanctx(sdata, NULL);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1355
  		break;
95224fe83   Arik Nemtsov   mac80211: move TD...
1356
  	default:
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1357
1358
  		ret = -ENOTSUPP;
  		break;
95224fe83   Arik Nemtsov   mac80211: move TD...
1359
  	}
81dd2b882   Arik Nemtsov   mac80211: move TD...
1360
1361
1362
  	if (ret == 0 && ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
  		cancel_delayed_work(&sdata->u.mgd.tdls_peer_del_work);
  		eth_zero_addr(sdata->u.mgd.tdls_peer);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1363
  	}
d51c2ea37   Arik Nemtsov   mac80211: TDLS: c...
1364
1365
1366
  	if (ret == 0)
  		ieee80211_queue_work(&sdata->local->hw,
  				     &sdata->u.mgd.request_smps_work);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1367
  	mutex_unlock(&local->mtx);
22f66895e   Avri Altman   mac80211: protect...
1368
  	sdata_unlock(sdata);
17e6a59a3   Arik Nemtsov   mac80211: cleanup...
1369
  	return ret;
95224fe83   Arik Nemtsov   mac80211: move TD...
1370
  }
c887f0d3a   Arik Nemtsov   mac80211: add API...
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
  
  void ieee80211_tdls_oper_request(struct ieee80211_vif *vif, const u8 *peer,
  				 enum nl80211_tdls_operation oper,
  				 u16 reason_code, gfp_t gfp)
  {
  	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  
  	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc) {
  		sdata_err(sdata, "Discarding TDLS oper %d - not STA or disconnected
  ",
  			  oper);
  		return;
  	}
  
  	cfg80211_tdls_oper_request(sdata->dev, peer, oper, reason_code, gfp);
  }
  EXPORT_SYMBOL(ieee80211_tdls_oper_request);
a7a6bdd06   Arik Nemtsov   mac80211: introdu...
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
  
  static void
  iee80211_tdls_add_ch_switch_timing(u8 *buf, u16 switch_time, u16 switch_timeout)
  {
  	struct ieee80211_ch_switch_timing *ch_sw;
  
  	*buf++ = WLAN_EID_CHAN_SWITCH_TIMING;
  	*buf++ = sizeof(struct ieee80211_ch_switch_timing);
  
  	ch_sw = (void *)buf;
  	ch_sw->switch_time = cpu_to_le16(switch_time);
  	ch_sw->switch_timeout = cpu_to_le16(switch_timeout);
  }
  
  /* find switch timing IE in SKB ready for Tx */
  static const u8 *ieee80211_tdls_find_sw_timing_ie(struct sk_buff *skb)
  {
  	struct ieee80211_tdls_data *tf;
  	const u8 *ie_start;
  
  	/*
  	 * Get the offset for the new location of the switch timing IE.
  	 * The SKB network header will now point to the "payload_type"
  	 * element of the TDLS data frame struct.
  	 */
  	tf = container_of(skb->data + skb_network_offset(skb),
  			  struct ieee80211_tdls_data, payload_type);
  	ie_start = tf->u.chan_switch_req.variable;
  	return cfg80211_find_ie(WLAN_EID_CHAN_SWITCH_TIMING, ie_start,
  				skb->len - (ie_start - skb->data));
  }
  
  static struct sk_buff *
  ieee80211_tdls_ch_sw_tmpl_get(struct sta_info *sta, u8 oper_class,
  			      struct cfg80211_chan_def *chandef,
  			      u32 *ch_sw_tm_ie_offset)
  {
  	struct ieee80211_sub_if_data *sdata = sta->sdata;
  	u8 extra_ies[2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
  		     2 + sizeof(struct ieee80211_ch_switch_timing)];
  	int extra_ies_len = 2 + sizeof(struct ieee80211_ch_switch_timing);
  	u8 *pos = extra_ies;
  	struct sk_buff *skb;
  
  	/*
  	 * if chandef points to a wide channel add a Secondary-Channel
  	 * Offset information element
  	 */
  	if (chandef->width == NL80211_CHAN_WIDTH_40) {
  		struct ieee80211_sec_chan_offs_ie *sec_chan_ie;
  		bool ht40plus;
  
  		*pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET;
  		*pos++ = sizeof(*sec_chan_ie);
  		sec_chan_ie = (void *)pos;
  
  		ht40plus = cfg80211_get_chandef_type(chandef) ==
  							NL80211_CHAN_HT40PLUS;
  		sec_chan_ie->sec_chan_offs = ht40plus ?
  					     IEEE80211_HT_PARAM_CHA_SEC_ABOVE :
  					     IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  		pos += sizeof(*sec_chan_ie);
  
  		extra_ies_len += 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
  	}
  
  	/* just set the values to 0, this is a template */
  	iee80211_tdls_add_ch_switch_timing(pos, 0, 0);
  
  	skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
  					      WLAN_TDLS_CHANNEL_SWITCH_REQUEST,
  					      0, 0, !sta->sta.tdls_initiator,
  					      extra_ies, extra_ies_len,
  					      oper_class, chandef);
  	if (!skb)
  		return NULL;
  
  	skb = ieee80211_build_data_template(sdata, skb, 0);
  	if (IS_ERR(skb)) {
  		tdls_dbg(sdata, "Failed building TDLS channel switch frame
  ");
  		return NULL;
  	}
  
  	if (ch_sw_tm_ie_offset) {
  		const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
  
  		if (!tm_ie) {
  			tdls_dbg(sdata, "No switch timing IE in TDLS switch
  ");
  			dev_kfree_skb_any(skb);
  			return NULL;
  		}
  
  		*ch_sw_tm_ie_offset = tm_ie - skb->data;
  	}
  
  	tdls_dbg(sdata,
  		 "TDLS channel switch request template for %pM ch %d width %d
  ",
  		 sta->sta.addr, chandef->chan->center_freq, chandef->width);
  	return skb;
  }
  
  int
  ieee80211_tdls_channel_switch(struct wiphy *wiphy, struct net_device *dev,
  			      const u8 *addr, u8 oper_class,
  			      struct cfg80211_chan_def *chandef)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  	struct ieee80211_local *local = sdata->local;
  	struct sta_info *sta;
  	struct sk_buff *skb = NULL;
  	u32 ch_sw_tm_ie;
  	int ret;
  
  	mutex_lock(&local->sta_mtx);
  	sta = sta_info_get(sdata, addr);
  	if (!sta) {
  		tdls_dbg(sdata,
  			 "Invalid TDLS peer %pM for channel switch request
  ",
  			 addr);
  		ret = -ENOENT;
  		goto out;
  	}
  
  	if (!test_sta_flag(sta, WLAN_STA_TDLS_CHAN_SWITCH)) {
  		tdls_dbg(sdata, "TDLS channel switch unsupported by %pM
  ",
  			 addr);
  		ret = -ENOTSUPP;
  		goto out;
  	}
  
  	skb = ieee80211_tdls_ch_sw_tmpl_get(sta, oper_class, chandef,
  					    &ch_sw_tm_ie);
  	if (!skb) {
  		ret = -ENOENT;
  		goto out;
  	}
  
  	ret = drv_tdls_channel_switch(local, sdata, &sta->sta, oper_class,
  				      chandef, skb, ch_sw_tm_ie);
  	if (!ret)
  		set_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  
  out:
  	mutex_unlock(&local->sta_mtx);
  	dev_kfree_skb_any(skb);
  	return ret;
  }
  
  void
  ieee80211_tdls_cancel_channel_switch(struct wiphy *wiphy,
  				     struct net_device *dev,
  				     const u8 *addr)
  {
  	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  	struct ieee80211_local *local = sdata->local;
  	struct sta_info *sta;
  
  	mutex_lock(&local->sta_mtx);
  	sta = sta_info_get(sdata, addr);
  	if (!sta) {
  		tdls_dbg(sdata,
  			 "Invalid TDLS peer %pM for channel switch cancel
  ",
  			 addr);
  		goto out;
  	}
  
  	if (!test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
  		tdls_dbg(sdata, "TDLS channel switch not initiated by %pM
  ",
  			 addr);
  		goto out;
  	}
  
  	drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
  	clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
  
  out:
  	mutex_unlock(&local->sta_mtx);
  }
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
  
  static struct sk_buff *
  ieee80211_tdls_ch_sw_resp_tmpl_get(struct sta_info *sta,
  				   u32 *ch_sw_tm_ie_offset)
  {
  	struct ieee80211_sub_if_data *sdata = sta->sdata;
  	struct sk_buff *skb;
  	u8 extra_ies[2 + sizeof(struct ieee80211_ch_switch_timing)];
  
  	/* initial timing are always zero in the template */
  	iee80211_tdls_add_ch_switch_timing(extra_ies, 0, 0);
  
  	skb = ieee80211_tdls_build_mgmt_packet_data(sdata, sta->sta.addr,
  					WLAN_TDLS_CHANNEL_SWITCH_RESPONSE,
  					0, 0, !sta->sta.tdls_initiator,
  					extra_ies, sizeof(extra_ies), 0, NULL);
  	if (!skb)
  		return NULL;
  
  	skb = ieee80211_build_data_template(sdata, skb, 0);
  	if (IS_ERR(skb)) {
  		tdls_dbg(sdata,
  			 "Failed building TDLS channel switch resp frame
  ");
  		return NULL;
  	}
  
  	if (ch_sw_tm_ie_offset) {
  		const u8 *tm_ie = ieee80211_tdls_find_sw_timing_ie(skb);
  
  		if (!tm_ie) {
  			tdls_dbg(sdata,
  				 "No switch timing IE in TDLS switch resp
  ");
  			dev_kfree_skb_any(skb);
  			return NULL;
  		}
  
  		*ch_sw_tm_ie_offset = tm_ie - skb->data;
  	}
  
  	tdls_dbg(sdata, "TDLS get channel switch response template for %pM
  ",
  		 sta->sta.addr);
  	return skb;
  }
  
  static int
  ieee80211_process_tdls_channel_switch_resp(struct ieee80211_sub_if_data *sdata,
  					   struct sk_buff *skb)
  {
  	struct ieee80211_local *local = sdata->local;
  	struct ieee802_11_elems elems;
  	struct sta_info *sta;
  	struct ieee80211_tdls_data *tf = (void *)skb->data;
  	bool local_initiator;
  	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  	int baselen = offsetof(typeof(*tf), u.chan_switch_resp.variable);
  	struct ieee80211_tdls_ch_sw_params params = {};
  	int ret;
  
  	params.action_code = WLAN_TDLS_CHANNEL_SWITCH_RESPONSE;
  	params.timestamp = rx_status->device_timestamp;
  
  	if (skb->len < baselen) {
  		tdls_dbg(sdata, "TDLS channel switch resp too short: %d
  ",
  			 skb->len);
  		return -EINVAL;
  	}
  
  	mutex_lock(&local->sta_mtx);
  	sta = sta_info_get(sdata, tf->sa);
  	if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
  		tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM
  ",
  			 tf->sa);
  		ret = -EINVAL;
  		goto out;
  	}
  
  	params.sta = &sta->sta;
  	params.status = le16_to_cpu(tf->u.chan_switch_resp.status_code);
  	if (params.status != 0) {
  		ret = 0;
  		goto call_drv;
  	}
  
  	ieee802_11_parse_elems(tf->u.chan_switch_resp.variable,
  			       skb->len - baselen, false, &elems);
  	if (elems.parse_error) {
  		tdls_dbg(sdata, "Invalid IEs in TDLS channel switch resp
  ");
  		ret = -EINVAL;
  		goto out;
  	}
  
  	if (!elems.ch_sw_timing || !elems.lnk_id) {
  		tdls_dbg(sdata, "TDLS channel switch resp - missing IEs
  ");
  		ret = -EINVAL;
  		goto out;
  	}
  
  	/* validate the initiator is set correctly */
  	local_initiator =
  		!memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
  	if (local_initiator == sta->sta.tdls_initiator) {
  		tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator
  ");
  		ret = -EINVAL;
  		goto out;
  	}
  
  	params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
  	params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
  
  	params.tmpl_skb =
  		ieee80211_tdls_ch_sw_resp_tmpl_get(sta, &params.ch_sw_tm_ie);
  	if (!params.tmpl_skb) {
  		ret = -ENOENT;
  		goto out;
  	}
49708e377   Dan Carpenter   mac80211: silence...
1696
  	ret = 0;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
  call_drv:
  	drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
  
  	tdls_dbg(sdata,
  		 "TDLS channel switch response received from %pM status %d
  ",
  		 tf->sa, params.status);
  
  out:
  	mutex_unlock(&local->sta_mtx);
  	dev_kfree_skb_any(params.tmpl_skb);
  	return ret;
  }
  
  static int
  ieee80211_process_tdls_channel_switch_req(struct ieee80211_sub_if_data *sdata,
  					  struct sk_buff *skb)
  {
  	struct ieee80211_local *local = sdata->local;
  	struct ieee802_11_elems elems;
  	struct cfg80211_chan_def chandef;
  	struct ieee80211_channel *chan;
  	enum nl80211_channel_type chan_type;
  	int freq;
  	u8 target_channel, oper_class;
  	bool local_initiator;
  	struct sta_info *sta;
57fbcce37   Johannes Berg   cfg80211: remove ...
1724
  	enum nl80211_band band;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
  	struct ieee80211_tdls_data *tf = (void *)skb->data;
  	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  	int baselen = offsetof(typeof(*tf), u.chan_switch_req.variable);
  	struct ieee80211_tdls_ch_sw_params params = {};
  	int ret = 0;
  
  	params.action_code = WLAN_TDLS_CHANNEL_SWITCH_REQUEST;
  	params.timestamp = rx_status->device_timestamp;
  
  	if (skb->len < baselen) {
  		tdls_dbg(sdata, "TDLS channel switch req too short: %d
  ",
  			 skb->len);
  		return -EINVAL;
  	}
  
  	target_channel = tf->u.chan_switch_req.target_channel;
  	oper_class = tf->u.chan_switch_req.oper_class;
  
  	/*
  	 * We can't easily infer the channel band. The operating class is
  	 * ambiguous - there are multiple tables (US/Europe/JP/Global). The
  	 * solution here is to treat channels with number >14 as 5GHz ones,
  	 * and specifically check for the (oper_class, channel) combinations
  	 * where this doesn't hold. These are thankfully unique according to
  	 * IEEE802.11-2012.
  	 * We consider only the 2GHz and 5GHz bands and 20MHz+ channels as
  	 * valid here.
  	 */
  	if ((oper_class == 112 || oper_class == 2 || oper_class == 3 ||
  	     oper_class == 4 || oper_class == 5 || oper_class == 6) &&
  	     target_channel < 14)
57fbcce37   Johannes Berg   cfg80211: remove ...
1757
  		band = NL80211_BAND_5GHZ;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1758
  	else
57fbcce37   Johannes Berg   cfg80211: remove ...
1759
1760
  		band = target_channel < 14 ? NL80211_BAND_2GHZ :
  					     NL80211_BAND_5GHZ;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
  
  	freq = ieee80211_channel_to_frequency(target_channel, band);
  	if (freq == 0) {
  		tdls_dbg(sdata, "Invalid channel in TDLS chan switch: %d
  ",
  			 target_channel);
  		return -EINVAL;
  	}
  
  	chan = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  	if (!chan) {
  		tdls_dbg(sdata,
  			 "Unsupported channel for TDLS chan switch: %d
  ",
  			 target_channel);
  		return -EINVAL;
  	}
  
  	ieee802_11_parse_elems(tf->u.chan_switch_req.variable,
  			       skb->len - baselen, false, &elems);
  	if (elems.parse_error) {
  		tdls_dbg(sdata, "Invalid IEs in TDLS channel switch req
  ");
  		return -EINVAL;
  	}
  
  	if (!elems.ch_sw_timing || !elems.lnk_id) {
  		tdls_dbg(sdata, "TDLS channel switch req - missing IEs
  ");
  		return -EINVAL;
  	}
42d8d7896   Arik Nemtsov   mac80211: TDLS: d...
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
  	if (!elems.sec_chan_offs) {
  		chan_type = NL80211_CHAN_HT20;
  	} else {
  		switch (elems.sec_chan_offs->sec_chan_offs) {
  		case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  			chan_type = NL80211_CHAN_HT40PLUS;
  			break;
  		case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  			chan_type = NL80211_CHAN_HT40MINUS;
  			break;
  		default:
  			chan_type = NL80211_CHAN_HT20;
  			break;
  		}
  	}
  
  	cfg80211_chandef_create(&chandef, chan, chan_type);
  
  	/* we will be active on the TDLS link */
  	if (!cfg80211_reg_can_beacon_relax(sdata->local->hw.wiphy, &chandef,
  					   sdata->wdev.iftype)) {
  		tdls_dbg(sdata, "TDLS chan switch to forbidden channel
  ");
  		return -EINVAL;
  	}
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
  	mutex_lock(&local->sta_mtx);
  	sta = sta_info_get(sdata, tf->sa);
  	if (!sta || !test_sta_flag(sta, WLAN_STA_TDLS_PEER_AUTH)) {
  		tdls_dbg(sdata, "TDLS chan switch from non-peer sta %pM
  ",
  			 tf->sa);
  		ret = -EINVAL;
  		goto out;
  	}
  
  	params.sta = &sta->sta;
  
  	/* validate the initiator is set correctly */
  	local_initiator =
  		!memcmp(elems.lnk_id->init_sta, sdata->vif.addr, ETH_ALEN);
  	if (local_initiator == sta->sta.tdls_initiator) {
  		tdls_dbg(sdata, "TDLS chan switch invalid lnk-id initiator
  ");
  		ret = -EINVAL;
  		goto out;
  	}
42d8d7896   Arik Nemtsov   mac80211: TDLS: d...
1838
1839
1840
1841
1842
1843
1844
  	/* peer should have known better */
  	if (!sta->sta.ht_cap.ht_supported && elems.sec_chan_offs &&
  	    elems.sec_chan_offs->sec_chan_offs) {
  		tdls_dbg(sdata, "TDLS chan switch - wide chan unsupported
  ");
  		ret = -ENOTSUPP;
  		goto out;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1845
  	}
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1846
  	params.chandef = &chandef;
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
  	params.switch_time = le16_to_cpu(elems.ch_sw_timing->switch_time);
  	params.switch_timeout = le16_to_cpu(elems.ch_sw_timing->switch_timeout);
  
  	params.tmpl_skb =
  		ieee80211_tdls_ch_sw_resp_tmpl_get(sta,
  						   &params.ch_sw_tm_ie);
  	if (!params.tmpl_skb) {
  		ret = -ENOENT;
  		goto out;
  	}
  
  	drv_tdls_recv_channel_switch(sdata->local, sdata, &params);
  
  	tdls_dbg(sdata,
  		 "TDLS ch switch request received from %pM ch %d width %d
  ",
  		 tf->sa, params.chandef->chan->center_freq,
  		 params.chandef->width);
  out:
  	mutex_unlock(&local->sta_mtx);
  	dev_kfree_skb_any(params.tmpl_skb);
  	return ret;
  }
c8ff71e66   Arik Nemtsov   mac80211: TDLS: h...
1870
1871
1872
  static void
  ieee80211_process_tdls_channel_switch(struct ieee80211_sub_if_data *sdata,
  				      struct sk_buff *skb)
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1873
1874
1875
  {
  	struct ieee80211_tdls_data *tf = (void *)skb->data;
  	struct wiphy *wiphy = sdata->local->hw.wiphy;
c8ff71e66   Arik Nemtsov   mac80211: TDLS: h...
1876
  	ASSERT_RTNL();
8a4d32f30   Arik Nemtsov   mac80211: add TDL...
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
  	/* make sure the driver supports it */
  	if (!(wiphy->features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
  		return;
  
  	/* we want to access the entire packet */
  	if (skb_linearize(skb))
  		return;
  	/*
  	 * The packet/size was already validated by mac80211 Rx path, only look
  	 * at the action type.
  	 */
  	switch (tf->action_code) {
  	case WLAN_TDLS_CHANNEL_SWITCH_REQUEST:
  		ieee80211_process_tdls_channel_switch_req(sdata, skb);
  		break;
  	case WLAN_TDLS_CHANNEL_SWITCH_RESPONSE:
  		ieee80211_process_tdls_channel_switch_resp(sdata, skb);
  		break;
  	default:
  		WARN_ON_ONCE(1);
  		return;
  	}
  }
d51c2ea37   Arik Nemtsov   mac80211: TDLS: c...
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
  
  void ieee80211_teardown_tdls_peers(struct ieee80211_sub_if_data *sdata)
  {
  	struct sta_info *sta;
  	u16 reason = WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  		if (!sta->sta.tdls || sta->sdata != sdata || !sta->uploaded ||
  		    !test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  			continue;
  
  		ieee80211_tdls_oper_request(&sdata->vif, sta->sta.addr,
  					    NL80211_TDLS_TEARDOWN, reason,
  					    GFP_ATOMIC);
  	}
  	rcu_read_unlock();
  }
c8ff71e66   Arik Nemtsov   mac80211: TDLS: h...
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
  
  void ieee80211_tdls_chsw_work(struct work_struct *wk)
  {
  	struct ieee80211_local *local =
  		container_of(wk, struct ieee80211_local, tdls_chsw_work);
  	struct ieee80211_sub_if_data *sdata;
  	struct sk_buff *skb;
  	struct ieee80211_tdls_data *tf;
  
  	rtnl_lock();
  	while ((skb = skb_dequeue(&local->skb_queue_tdls_chsw))) {
  		tf = (struct ieee80211_tdls_data *)skb->data;
  		list_for_each_entry(sdata, &local->interfaces, list) {
  			if (!ieee80211_sdata_running(sdata) ||
  			    sdata->vif.type != NL80211_IFTYPE_STATION ||
  			    !ether_addr_equal(tf->da, sdata->vif.addr))
  				continue;
  
  			ieee80211_process_tdls_channel_switch(sdata, skb);
  			break;
  		}
  
  		kfree_skb(skb);
  	}
  	rtnl_unlock();
  }