xref: /linux/net/mac80211/ibss.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * IBSS mode implementation
4  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
5  * Copyright 2004, Instant802 Networks, Inc.
6  * Copyright 2005, Devicescape Software, Inc.
7  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
8  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9  * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
10  * Copyright 2013-2014  Intel Mobile Communications GmbH
11  * Copyright(c) 2016 Intel Deutschland GmbH
12  * Copyright(c) 2018-2026 Intel Corporation
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/if_ether.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/etherdevice.h>
21 #include <linux/rtnetlink.h>
22 #include <net/mac80211.h>
23 
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "rate.h"
27 
28 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
29 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
30 
31 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
32 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
33 #define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
34 
35 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
36 
37 static struct beacon_data *
38 ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
39 			   const int beacon_int, const u32 basic_rates,
40 			   const u16 capability, u64 tsf,
41 			   struct cfg80211_chan_def *chandef,
42 			   bool *have_higher_than_11mbit,
43 			   struct cfg80211_csa_settings *csa_settings)
44 {
45 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46 	struct ieee80211_local *local = sdata->local;
47 	int rates_n = 0, i, ri;
48 	struct ieee80211_mgmt *mgmt;
49 	u8 *pos;
50 	struct ieee80211_supported_band *sband;
51 	u32 rates = 0, rates_added = 0;
52 	struct beacon_data *presp;
53 	int frame_len;
54 
55 	/* Build IBSS probe response */
56 	frame_len = sizeof(struct ieee80211_hdr_3addr) +
57 		    12 /* struct ieee80211_mgmt.u.beacon */ +
58 		    2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
59 		    2 + 8 /* max Supported Rates */ +
60 		    3 /* max DS params */ +
61 		    4 /* IBSS params */ +
62 		    5 /* Channel Switch Announcement */ +
63 		    2 + (IEEE80211_MAX_SUPP_RATES - 8) +
64 		    2 + sizeof(struct ieee80211_ht_cap) +
65 		    2 + sizeof(struct ieee80211_ht_operation) +
66 		    2 + sizeof(struct ieee80211_vht_cap) +
67 		    2 + sizeof(struct ieee80211_vht_operation) +
68 		    ifibss->ie_len;
69 	presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
70 	if (!presp)
71 		return NULL;
72 
73 	presp->head = (void *)(presp + 1);
74 
75 	mgmt = (void *) presp->head;
76 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
77 					  IEEE80211_STYPE_PROBE_RESP);
78 	eth_broadcast_addr(mgmt->da);
79 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
80 	memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
81 	mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
82 	mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
83 	mgmt->u.beacon.capab_info = cpu_to_le16(capability);
84 
85 	pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
86 
87 	*pos++ = WLAN_EID_SSID;
88 	*pos++ = ifibss->ssid_len;
89 	memcpy(pos, ifibss->ssid, ifibss->ssid_len);
90 	pos += ifibss->ssid_len;
91 
92 	sband = local->hw.wiphy->bands[chandef->chan->band];
93 	rates_n = 0;
94 	if (have_higher_than_11mbit)
95 		*have_higher_than_11mbit = false;
96 
97 	for (i = 0; i < sband->n_bitrates; i++) {
98 		if (sband->bitrates[i].bitrate > 110 &&
99 		    have_higher_than_11mbit)
100 			*have_higher_than_11mbit = true;
101 
102 		rates |= BIT(i);
103 		rates_n++;
104 	}
105 
106 	*pos++ = WLAN_EID_SUPP_RATES;
107 	*pos++ = min_t(int, 8, rates_n);
108 	for (ri = 0; ri < sband->n_bitrates; ri++) {
109 		int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, 5);
110 		u8 basic = 0;
111 		if (!(rates & BIT(ri)))
112 			continue;
113 
114 		if (basic_rates & BIT(ri))
115 			basic = 0x80;
116 		*pos++ = basic | (u8) rate;
117 		if (++rates_added == 8) {
118 			ri++; /* continue at next rate for EXT_SUPP_RATES */
119 			break;
120 		}
121 	}
122 
123 	if (sband->band == NL80211_BAND_2GHZ) {
124 		*pos++ = WLAN_EID_DS_PARAMS;
125 		*pos++ = 1;
126 		*pos++ = ieee80211_frequency_to_channel(
127 				chandef->chan->center_freq);
128 	}
129 
130 	*pos++ = WLAN_EID_IBSS_PARAMS;
131 	*pos++ = 2;
132 	/* FIX: set ATIM window based on scan results */
133 	*pos++ = 0;
134 	*pos++ = 0;
135 
136 	if (csa_settings) {
137 		*pos++ = WLAN_EID_CHANNEL_SWITCH;
138 		*pos++ = 3;
139 		*pos++ = csa_settings->block_tx ? 1 : 0;
140 		*pos++ = ieee80211_frequency_to_channel(
141 				csa_settings->chandef.chan->center_freq);
142 		presp->cntdwn_counter_offsets[0] = (pos - presp->head);
143 		*pos++ = csa_settings->count;
144 		presp->cntdwn_current_counter = csa_settings->count;
145 	}
146 
147 	/* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
148 	if (rates_n > 8) {
149 		*pos++ = WLAN_EID_EXT_SUPP_RATES;
150 		*pos++ = rates_n - 8;
151 		for (; ri < sband->n_bitrates; ri++) {
152 			int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, 5);
153 			u8 basic = 0;
154 			if (!(rates & BIT(ri)))
155 				continue;
156 
157 			if (basic_rates & BIT(ri))
158 				basic = 0x80;
159 			*pos++ = basic | (u8) rate;
160 		}
161 	}
162 
163 	if (ifibss->ie_len) {
164 		memcpy(pos, ifibss->ie, ifibss->ie_len);
165 		pos += ifibss->ie_len;
166 	}
167 
168 	/* add HT capability and information IEs */
169 	if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
170 	    sband->ht_cap.ht_supported) {
171 		struct ieee80211_sta_ht_cap ht_cap;
172 
173 		memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
174 		ieee80211_apply_htcap_overrides(sdata, &ht_cap);
175 
176 		pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
177 		/*
178 		 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
179 		 * field and RIFS Mode are reserved in IBSS mode, therefore
180 		 * keep them at 0
181 		 */
182 		pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
183 						 chandef, 0, false);
184 
185 		/* add VHT capability and information IEs */
186 		if (chandef->width != NL80211_CHAN_WIDTH_20 &&
187 		    chandef->width != NL80211_CHAN_WIDTH_40 &&
188 		    sband->vht_cap.vht_supported) {
189 			pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
190 							 sband->vht_cap.cap);
191 			pos = ieee80211_ie_build_vht_oper(pos, &sband->vht_cap,
192 							  chandef);
193 		}
194 	}
195 
196 	if (local->hw.queues >= IEEE80211_NUM_ACS)
197 		pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
198 
199 	presp->head_len = pos - presp->head;
200 	if (WARN_ON(presp->head_len > frame_len))
201 		goto error;
202 
203 	return presp;
204 error:
205 	kfree(presp);
206 	return NULL;
207 }
208 
209 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
210 				      const u8 *bssid, const int beacon_int,
211 				      struct cfg80211_chan_def *req_chandef,
212 				      const u32 basic_rates,
213 				      const u16 capability, u64 tsf,
214 				      bool creator)
215 {
216 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
217 	struct ieee80211_local *local = sdata->local;
218 	struct ieee80211_mgmt *mgmt;
219 	struct cfg80211_bss *bss;
220 	u64 bss_change;
221 	struct ieee80211_chan_req chanreq = {};
222 	struct ieee80211_channel *chan;
223 	struct beacon_data *presp;
224 	struct cfg80211_inform_bss bss_meta = {};
225 	bool have_higher_than_11mbit;
226 	bool radar_required;
227 	int err;
228 
229 	lockdep_assert_wiphy(local->hw.wiphy);
230 
231 	/* Reset own TSF to allow time synchronization work. */
232 	drv_reset_tsf(local, sdata);
233 
234 	if (!ether_addr_equal(ifibss->bssid, bssid))
235 		sta_info_flush(sdata, -1);
236 
237 	/* if merging, indicate to driver that we leave the old IBSS */
238 	if (sdata->vif.cfg.ibss_joined) {
239 		sdata->vif.cfg.ibss_joined = false;
240 		sdata->vif.cfg.ibss_creator = false;
241 		sdata->vif.bss_conf.enable_beacon = false;
242 		netif_carrier_off(sdata->dev);
243 		synchronize_net();
244 		ieee80211_bss_info_change_notify(sdata,
245 						 BSS_CHANGED_IBSS |
246 						 BSS_CHANGED_BEACON_ENABLED);
247 		drv_leave_ibss(local, sdata);
248 	}
249 
250 	presp = sdata_dereference(ifibss->presp, sdata);
251 	RCU_INIT_POINTER(ifibss->presp, NULL);
252 	if (presp)
253 		kfree_rcu(presp, rcu_head);
254 
255 	/* make a copy of the chandef, it could be modified below. */
256 	chanreq.oper = *req_chandef;
257 	chan = chanreq.oper.chan;
258 	if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chanreq.oper,
259 				     NL80211_IFTYPE_ADHOC)) {
260 		if (chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT ||
261 		    chanreq.oper.width == NL80211_CHAN_WIDTH_20) {
262 			sdata_info(sdata,
263 				   "Failed to join IBSS, beacons forbidden\n");
264 			return;
265 		}
266 		chanreq.oper.width = NL80211_CHAN_WIDTH_20;
267 		chanreq.oper.center_freq1 = chan->center_freq;
268 		/* check again for downgraded chandef */
269 		if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chanreq.oper,
270 					     NL80211_IFTYPE_ADHOC)) {
271 			sdata_info(sdata,
272 				   "Failed to join IBSS, beacons forbidden\n");
273 			return;
274 		}
275 	}
276 
277 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
278 					    &chanreq.oper, NL80211_IFTYPE_ADHOC);
279 	if (err < 0) {
280 		sdata_info(sdata,
281 			   "Failed to join IBSS, invalid chandef\n");
282 		return;
283 	}
284 	if (err > 0 && !ifibss->userspace_handles_dfs) {
285 		sdata_info(sdata,
286 			   "Failed to join IBSS, DFS channel without control program\n");
287 		return;
288 	}
289 
290 	radar_required = err;
291 
292 	if (ieee80211_link_use_channel(&sdata->deflink, &chanreq,
293 				       ifibss->fixed_channel ?
294 					IEEE80211_CHANCTX_SHARED :
295 					IEEE80211_CHANCTX_EXCLUSIVE)) {
296 		sdata_info(sdata, "Failed to join IBSS, no channel context\n");
297 		return;
298 	}
299 	sdata->deflink.radar_required = radar_required;
300 
301 	memcpy(ifibss->bssid, bssid, ETH_ALEN);
302 
303 	presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
304 					   capability, tsf, &chanreq.oper,
305 					   &have_higher_than_11mbit, NULL);
306 	if (!presp)
307 		return;
308 
309 	rcu_assign_pointer(ifibss->presp, presp);
310 	mgmt = (void *)presp->head;
311 
312 	sdata->vif.bss_conf.enable_beacon = true;
313 	sdata->vif.bss_conf.beacon_int = beacon_int;
314 	sdata->vif.bss_conf.basic_rates = basic_rates;
315 	sdata->vif.cfg.ssid_len = ifibss->ssid_len;
316 	memcpy(sdata->vif.cfg.ssid, ifibss->ssid, ifibss->ssid_len);
317 	bss_change = BSS_CHANGED_BEACON_INT;
318 	bss_change |= ieee80211_reset_erp_info(sdata);
319 	bss_change |= BSS_CHANGED_BSSID;
320 	bss_change |= BSS_CHANGED_BEACON;
321 	bss_change |= BSS_CHANGED_BEACON_ENABLED;
322 	bss_change |= BSS_CHANGED_BASIC_RATES;
323 	bss_change |= BSS_CHANGED_HT;
324 	bss_change |= BSS_CHANGED_IBSS;
325 	bss_change |= BSS_CHANGED_SSID;
326 
327 	/*
328 	 * In 5 GHz/802.11a, we can always use short slot time.
329 	 * (IEEE 802.11-2012 18.3.8.7)
330 	 *
331 	 * In 2.4GHz, we must always use long slots in IBSS for compatibility
332 	 * reasons.
333 	 * (IEEE 802.11-2012 19.4.5)
334 	 *
335 	 * HT follows these specifications (IEEE 802.11-2012 20.3.18)
336 	 */
337 	sdata->vif.bss_conf.use_short_slot = chan->band == NL80211_BAND_5GHZ;
338 	bss_change |= BSS_CHANGED_ERP_SLOT;
339 
340 	/* cf. IEEE 802.11 9.2.12 */
341 	sdata->deflink.operating_11g_mode =
342 		chan->band == NL80211_BAND_2GHZ && have_higher_than_11mbit;
343 
344 	ieee80211_set_wmm_default(&sdata->deflink, true, false);
345 
346 	sdata->vif.cfg.ibss_joined = true;
347 	sdata->vif.cfg.ibss_creator = creator;
348 
349 	err = drv_join_ibss(local, sdata);
350 	if (err) {
351 		sdata->vif.cfg.ibss_joined = false;
352 		sdata->vif.cfg.ibss_creator = false;
353 		sdata->vif.bss_conf.enable_beacon = false;
354 		sdata->vif.cfg.ssid_len = 0;
355 		RCU_INIT_POINTER(ifibss->presp, NULL);
356 		kfree_rcu(presp, rcu_head);
357 		ieee80211_link_release_channel(&sdata->deflink);
358 		sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
359 			   err);
360 		return;
361 	}
362 
363 	ieee80211_bss_info_change_notify(sdata, bss_change);
364 
365 	ifibss->state = IEEE80211_IBSS_MLME_JOINED;
366 	mod_timer(&ifibss->timer,
367 		  round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
368 
369 	bss_meta.chan = chan;
370 	bss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, mgmt,
371 					     presp->head_len, GFP_KERNEL);
372 
373 	cfg80211_put_bss(local->hw.wiphy, bss);
374 	netif_carrier_on(sdata->dev);
375 	cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
376 }
377 
378 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
379 				    struct ieee80211_bss *bss)
380 {
381 	struct cfg80211_bss *cbss =
382 		container_of((void *)bss, struct cfg80211_bss, priv);
383 	struct ieee80211_supported_band *sband;
384 	struct cfg80211_chan_def chandef;
385 	u32 basic_rates;
386 	int i, j;
387 	u16 beacon_int = cbss->beacon_interval;
388 	const struct cfg80211_bss_ies *ies;
389 	enum nl80211_channel_type chan_type;
390 	u64 tsf;
391 
392 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
393 
394 	if (beacon_int < 10)
395 		beacon_int = 10;
396 
397 	switch (sdata->u.ibss.chandef.width) {
398 	case NL80211_CHAN_WIDTH_20_NOHT:
399 	case NL80211_CHAN_WIDTH_20:
400 	case NL80211_CHAN_WIDTH_40:
401 		chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
402 		cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
403 		break;
404 	case NL80211_CHAN_WIDTH_80:
405 	case NL80211_CHAN_WIDTH_80P80:
406 	case NL80211_CHAN_WIDTH_160:
407 		chandef = sdata->u.ibss.chandef;
408 		chandef.chan = cbss->channel;
409 		break;
410 	default:
411 		/* fall back to 20 MHz for unsupported modes */
412 		cfg80211_chandef_create(&chandef, cbss->channel,
413 					NL80211_CHAN_NO_HT);
414 		break;
415 	}
416 
417 	sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
418 
419 	basic_rates = 0;
420 
421 	for (i = 0; i < bss->supp_rates_len; i++) {
422 		int rate = bss->supp_rates[i] & 0x7f;
423 		bool is_basic = !!(bss->supp_rates[i] & 0x80);
424 
425 		for (j = 0; j < sband->n_bitrates; j++) {
426 			int brate;
427 
428 			brate = DIV_ROUND_UP(sband->bitrates[j].bitrate, 5);
429 			if (brate == rate) {
430 				if (is_basic)
431 					basic_rates |= BIT(j);
432 				break;
433 			}
434 		}
435 	}
436 
437 	rcu_read_lock();
438 	ies = rcu_dereference(cbss->ies);
439 	tsf = ies->tsf;
440 	rcu_read_unlock();
441 
442 	__ieee80211_sta_join_ibss(sdata, cbss->bssid,
443 				  beacon_int,
444 				  &chandef,
445 				  basic_rates,
446 				  cbss->capability,
447 				  tsf, false);
448 }
449 
450 int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
451 			      struct cfg80211_csa_settings *csa_settings,
452 			      u64 *changed)
453 {
454 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
455 	struct beacon_data *presp, *old_presp;
456 	struct cfg80211_bss *cbss;
457 	const struct cfg80211_bss_ies *ies;
458 	u16 capability = WLAN_CAPABILITY_IBSS;
459 	u64 tsf;
460 
461 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
462 
463 	if (ifibss->privacy)
464 		capability |= WLAN_CAPABILITY_PRIVACY;
465 
466 	cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
467 				ifibss->bssid, ifibss->ssid,
468 				ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
469 				IEEE80211_PRIVACY(ifibss->privacy));
470 
471 	if (unlikely(!cbss))
472 		return -EINVAL;
473 
474 	rcu_read_lock();
475 	ies = rcu_dereference(cbss->ies);
476 	tsf = ies->tsf;
477 	rcu_read_unlock();
478 	cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
479 
480 	old_presp = sdata_dereference(ifibss->presp, sdata);
481 
482 	presp = ieee80211_ibss_build_presp(sdata,
483 					   sdata->vif.bss_conf.beacon_int,
484 					   sdata->vif.bss_conf.basic_rates,
485 					   capability, tsf, &ifibss->chandef,
486 					   NULL, csa_settings);
487 	if (!presp)
488 		return -ENOMEM;
489 
490 	rcu_assign_pointer(ifibss->presp, presp);
491 	if (old_presp)
492 		kfree_rcu(old_presp, rcu_head);
493 
494 	*changed |= BSS_CHANGED_BEACON;
495 	return 0;
496 }
497 
498 int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata, u64 *changed)
499 {
500 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
501 	struct cfg80211_bss *cbss;
502 
503 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
504 
505 	/* When not connected/joined, sending CSA doesn't make sense. */
506 	if (ifibss->state != IEEE80211_IBSS_MLME_JOINED)
507 		return -ENOLINK;
508 
509 	/* update cfg80211 bss information with the new channel */
510 	if (!is_zero_ether_addr(ifibss->bssid)) {
511 		cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
512 					ifibss->chandef.chan,
513 					ifibss->bssid, ifibss->ssid,
514 					ifibss->ssid_len,
515 					IEEE80211_BSS_TYPE_IBSS,
516 					IEEE80211_PRIVACY(ifibss->privacy));
517 		/* XXX: should not really modify cfg80211 data */
518 		if (cbss) {
519 			cbss->channel = sdata->deflink.csa.chanreq.oper.chan;
520 			cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
521 		}
522 	}
523 
524 	ifibss->chandef = sdata->deflink.csa.chanreq.oper;
525 
526 	/* generate the beacon */
527 	return ieee80211_ibss_csa_beacon(sdata, NULL, changed);
528 }
529 
530 void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
531 {
532 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
533 
534 	wiphy_work_cancel(sdata->local->hw.wiphy,
535 			  &ifibss->csa_connection_drop_work);
536 }
537 
538 static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
539 	__acquires(RCU)
540 {
541 	struct ieee80211_sub_if_data *sdata = sta->sdata;
542 	u8 addr[ETH_ALEN];
543 
544 	memcpy(addr, sta->sta.addr, ETH_ALEN);
545 
546 	ieee80211_sta_init_nss_bw_capa(&sta->deflink,
547 				       &sdata->deflink.conf->chanreq.oper);
548 
549 	ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
550 
551 	sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
552 	sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
553 	/* authorize the station only if the network is not RSN protected. If
554 	 * not wait for the userspace to authorize it */
555 	if (!sta->sdata->u.ibss.control_port)
556 		sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
557 
558 	rate_control_rate_init(&sta->deflink);
559 
560 	/* If it fails, maybe we raced another insertion? */
561 	if (sta_info_insert_rcu(sta))
562 		return sta_info_get(sdata, addr);
563 	return sta;
564 }
565 
566 static struct sta_info *
567 ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
568 		       const u8 *addr, u32 supp_rates)
569 	__acquires(RCU)
570 {
571 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
572 	struct ieee80211_local *local = sdata->local;
573 	struct sta_info *sta;
574 	struct ieee80211_chanctx_conf *chanctx_conf;
575 	struct ieee80211_supported_band *sband;
576 	int band;
577 
578 	/*
579 	 * XXX: Consider removing the least recently used entry and
580 	 * 	allow new one to be added.
581 	 */
582 	if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
583 		net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
584 				    sdata->name, addr);
585 		rcu_read_lock();
586 		return NULL;
587 	}
588 
589 	if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
590 		rcu_read_lock();
591 		return NULL;
592 	}
593 
594 	if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
595 		rcu_read_lock();
596 		return NULL;
597 	}
598 
599 	rcu_read_lock();
600 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
601 	if (WARN_ON_ONCE(!chanctx_conf))
602 		return NULL;
603 	band = chanctx_conf->def.chan->band;
604 	rcu_read_unlock();
605 
606 	sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
607 	if (!sta) {
608 		rcu_read_lock();
609 		return NULL;
610 	}
611 
612 	/* make sure mandatory rates are always added */
613 	sband = local->hw.wiphy->bands[band];
614 	sta->sta.deflink.supp_rates[band] = supp_rates |
615 			ieee80211_mandatory_rates(sband);
616 
617 	return ieee80211_ibss_finish_sta(sta);
618 }
619 
620 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
621 {
622 	struct ieee80211_local *local = sdata->local;
623 	int active = 0;
624 	struct sta_info *sta;
625 
626 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
627 
628 	rcu_read_lock();
629 
630 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
631 		unsigned long last_active = ieee80211_sta_last_active(sta, -1);
632 
633 		if (sta->sdata == sdata &&
634 		    time_is_after_jiffies(last_active +
635 					  IEEE80211_IBSS_MERGE_INTERVAL)) {
636 			active++;
637 			break;
638 		}
639 	}
640 
641 	rcu_read_unlock();
642 
643 	return active;
644 }
645 
646 static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
647 {
648 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
649 	struct ieee80211_local *local = sdata->local;
650 	struct cfg80211_bss *cbss;
651 	struct beacon_data *presp;
652 	struct sta_info *sta;
653 
654 	lockdep_assert_wiphy(local->hw.wiphy);
655 
656 	if (!is_zero_ether_addr(ifibss->bssid)) {
657 		cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
658 					ifibss->bssid, ifibss->ssid,
659 					ifibss->ssid_len,
660 					IEEE80211_BSS_TYPE_IBSS,
661 					IEEE80211_PRIVACY(ifibss->privacy));
662 
663 		if (cbss) {
664 			cfg80211_unlink_bss(local->hw.wiphy, cbss);
665 			cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
666 		}
667 	}
668 
669 	ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
670 
671 	sta_info_flush(sdata, -1);
672 
673 	spin_lock_bh(&ifibss->incomplete_lock);
674 	while (!list_empty(&ifibss->incomplete_stations)) {
675 		sta = list_first_entry(&ifibss->incomplete_stations,
676 				       struct sta_info, list);
677 		list_del(&sta->list);
678 		spin_unlock_bh(&ifibss->incomplete_lock);
679 
680 		sta_info_free(local, sta);
681 		spin_lock_bh(&ifibss->incomplete_lock);
682 	}
683 	spin_unlock_bh(&ifibss->incomplete_lock);
684 
685 	netif_carrier_off(sdata->dev);
686 
687 	sdata->vif.cfg.ibss_joined = false;
688 	sdata->vif.cfg.ibss_creator = false;
689 	sdata->vif.bss_conf.enable_beacon = false;
690 	sdata->vif.cfg.ssid_len = 0;
691 
692 	/* remove beacon */
693 	presp = sdata_dereference(ifibss->presp, sdata);
694 	RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
695 	if (presp)
696 		kfree_rcu(presp, rcu_head);
697 
698 	clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
699 	ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
700 						BSS_CHANGED_IBSS);
701 	drv_leave_ibss(local, sdata);
702 	ieee80211_link_release_channel(&sdata->deflink);
703 }
704 
705 static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
706 					       struct wiphy_work *work)
707 {
708 	struct ieee80211_sub_if_data *sdata =
709 		container_of(work, struct ieee80211_sub_if_data,
710 			     u.ibss.csa_connection_drop_work);
711 
712 	ieee80211_ibss_disconnect(sdata);
713 	synchronize_rcu();
714 	skb_queue_purge(&sdata->skb_queue);
715 
716 	/* trigger a scan to find another IBSS network to join */
717 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
718 }
719 
720 static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
721 {
722 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
723 	int err;
724 
725 	/* if the current channel is a DFS channel, mark the channel as
726 	 * unavailable.
727 	 */
728 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
729 					    &ifibss->chandef,
730 					    NL80211_IFTYPE_ADHOC);
731 	if (err > 0)
732 		cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
733 				     GFP_ATOMIC);
734 }
735 
736 static bool
737 ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
738 				  struct ieee802_11_elems *elems,
739 				  bool beacon)
740 {
741 	struct cfg80211_csa_settings params;
742 	struct ieee80211_csa_ie csa_ie;
743 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
744 	enum nl80211_channel_type ch_type;
745 	int err;
746 	struct ieee80211_conn_settings conn = {
747 		.mode = IEEE80211_CONN_MODE_HT,
748 		.bw_limit = IEEE80211_CONN_BW_LIMIT_40,
749 	};
750 	u32 vht_cap_info = 0;
751 
752 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
753 
754 	switch (ifibss->chandef.width) {
755 	case NL80211_CHAN_WIDTH_20_NOHT:
756 		conn.mode = IEEE80211_CONN_MODE_LEGACY;
757 		fallthrough;
758 	case NL80211_CHAN_WIDTH_20:
759 		conn.bw_limit = IEEE80211_CONN_BW_LIMIT_20;
760 		break;
761 	default:
762 		break;
763 	}
764 
765 	if (elems->vht_cap_elem)
766 		vht_cap_info = le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
767 
768 	memset(&params, 0, sizeof(params));
769 	err = ieee80211_parse_ch_switch_ie(sdata, elems,
770 					   ifibss->chandef.chan->band,
771 					   vht_cap_info, &conn,
772 					   ifibss->bssid, false,
773 					   &csa_ie);
774 	/* can't switch to destination channel, fail */
775 	if (err < 0)
776 		goto disconnect;
777 
778 	/* did not contain a CSA */
779 	if (err)
780 		return false;
781 
782 	/* channel switch is not supported, disconnect */
783 	if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
784 		goto disconnect;
785 
786 	params.count = csa_ie.count;
787 	params.chandef = csa_ie.chanreq.oper;
788 
789 	switch (ifibss->chandef.width) {
790 	case NL80211_CHAN_WIDTH_20_NOHT:
791 	case NL80211_CHAN_WIDTH_20:
792 	case NL80211_CHAN_WIDTH_40:
793 		/* keep our current HT mode (HT20/HT40+/HT40-), even if
794 		 * another mode  has been announced. The mode is not adopted
795 		 * within the beacon while doing CSA and we should therefore
796 		 * keep the mode which we announce.
797 		 */
798 		ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
799 		cfg80211_chandef_create(&params.chandef, params.chandef.chan,
800 					ch_type);
801 		break;
802 	default:
803 		/* should not happen, conn_flags should prevent VHT modes. */
804 		WARN_ON(1);
805 		goto disconnect;
806 	}
807 
808 	if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
809 				     NL80211_IFTYPE_ADHOC)) {
810 		sdata_info(sdata,
811 			   "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
812 			   ifibss->bssid,
813 			   params.chandef.chan->center_freq,
814 			   params.chandef.width,
815 			   params.chandef.center_freq1,
816 			   params.chandef.center_freq2);
817 		goto disconnect;
818 	}
819 
820 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
821 					    &params.chandef,
822 					    NL80211_IFTYPE_ADHOC);
823 	if (err < 0)
824 		goto disconnect;
825 	if (err > 0 && !ifibss->userspace_handles_dfs) {
826 		/* IBSS-DFS only allowed with a control program */
827 		goto disconnect;
828 	}
829 
830 	params.radar_required = err;
831 
832 	if (cfg80211_chandef_identical(&params.chandef,
833 				       &sdata->vif.bss_conf.chanreq.oper)) {
834 		ibss_dbg(sdata,
835 			 "received csa with an identical chandef, ignoring\n");
836 		return true;
837 	}
838 
839 	/* all checks done, now perform the channel switch. */
840 	ibss_dbg(sdata,
841 		 "received channel switch announcement to go to channel %d MHz\n",
842 		 params.chandef.chan->center_freq);
843 
844 	params.block_tx = !!csa_ie.mode;
845 
846 	if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
847 				     &params))
848 		goto disconnect;
849 
850 	ieee80211_ibss_csa_mark_radar(sdata);
851 
852 	return true;
853 disconnect:
854 	ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
855 	wiphy_work_queue(sdata->local->hw.wiphy,
856 			 &ifibss->csa_connection_drop_work);
857 
858 	ieee80211_ibss_csa_mark_radar(sdata);
859 
860 	return true;
861 }
862 
863 static void
864 ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
865 				struct ieee80211_mgmt *mgmt, size_t len,
866 				struct ieee80211_rx_status *rx_status,
867 				struct ieee802_11_elems *elems)
868 {
869 	if (len < IEEE80211_MIN_ACTION_SIZE(chan_switch))
870 		return;
871 
872 	/* CSA is the only action we handle for now */
873 	if (mgmt->u.action.action_code != WLAN_ACTION_SPCT_CHL_SWITCH)
874 		return;
875 
876 	if (!sdata->vif.bss_conf.csa_active)
877 		ieee80211_ibss_process_chanswitch(sdata, elems, false);
878 }
879 
880 static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
881 					  struct ieee80211_mgmt *mgmt,
882 					  size_t len)
883 {
884 	u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
885 
886 	if (len < IEEE80211_DEAUTH_FRAME_LEN)
887 		return;
888 
889 	ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
890 	ibss_dbg(sdata, "\tBSSID=%pM (reason: %d)\n", mgmt->bssid, reason);
891 	sta_info_destroy_addr(sdata, mgmt->sa);
892 }
893 
894 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
895 					struct ieee80211_mgmt *mgmt,
896 					size_t len)
897 {
898 	u16 auth_alg, auth_transaction;
899 
900 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
901 
902 	if (len < 24 + 6)
903 		return;
904 
905 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
906 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
907 
908 	ibss_dbg(sdata, "RX Auth SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
909 	ibss_dbg(sdata, "\tBSSID=%pM (auth_transaction=%d)\n",
910 		 mgmt->bssid, auth_transaction);
911 
912 	if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
913 		return;
914 
915 	/*
916 	 * IEEE 802.11 standard does not require authentication in IBSS
917 	 * networks and most implementations do not seem to use it.
918 	 * However, try to reply to authentication attempts if someone
919 	 * has actually implemented this.
920 	 */
921 	ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
922 			    mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
923 }
924 
925 static void ieee80211_update_sta_info(struct ieee80211_sub_if_data *sdata,
926 				      struct ieee80211_mgmt *mgmt, size_t len,
927 				      struct ieee80211_rx_status *rx_status,
928 				      struct ieee802_11_elems *elems,
929 				      struct ieee80211_channel *channel)
930 {
931 	struct sta_info *sta;
932 	enum nl80211_band band = rx_status->band;
933 	struct ieee80211_local *local = sdata->local;
934 	struct ieee80211_supported_band *sband;
935 	bool rates_updated = false;
936 	u32 supp_rates = 0;
937 
938 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
939 		return;
940 
941 	if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
942 		return;
943 
944 	sband = local->hw.wiphy->bands[band];
945 	if (WARN_ON(!sband))
946 		return;
947 
948 	rcu_read_lock();
949 	sta = sta_info_get(sdata, mgmt->sa);
950 
951 	if (elems->supp_rates) {
952 		supp_rates = ieee80211_sta_get_rates(sdata, elems,
953 						     band, NULL);
954 		if (sta) {
955 			u32 prev_rates;
956 
957 			prev_rates = sta->sta.deflink.supp_rates[band];
958 
959 			sta->sta.deflink.supp_rates[band] = supp_rates |
960 				ieee80211_mandatory_rates(sband);
961 			if (sta->sta.deflink.supp_rates[band] != prev_rates) {
962 				ibss_dbg(sdata,
963 					 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
964 					 sta->sta.addr, prev_rates,
965 					 sta->sta.deflink.supp_rates[band]);
966 				rates_updated = true;
967 			}
968 		} else {
969 			rcu_read_unlock();
970 			sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
971 						     mgmt->sa, supp_rates);
972 		}
973 	}
974 
975 	if (sta && !sta->sta.wme &&
976 	    (elems->wmm_info || elems->s1g_capab) &&
977 	    local->hw.queues >= IEEE80211_NUM_ACS) {
978 		sta->sta.wme = true;
979 		ieee80211_check_fast_xmit(sta);
980 	}
981 
982 	if (sta && elems->ht_operation && elems->ht_cap_elem &&
983 	    sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
984 		/* we both use HT */
985 		struct ieee80211_ht_cap htcap_ie;
986 		struct cfg80211_chan_def chandef;
987 		enum ieee80211_sta_rx_bandwidth bw = sta->sta.deflink.bandwidth;
988 
989 		cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
990 		ieee80211_chandef_ht_oper(elems->ht_operation, &chandef);
991 
992 		memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
993 		rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata,
994 								   &sband->ht_cap,
995 								   &htcap_ie,
996 								   &sta->deflink);
997 
998 		if (elems->vht_operation && elems->vht_cap_elem &&
999 		    sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
1000 		    sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
1001 			/* we both use VHT */
1002 			struct ieee80211_vht_cap cap_ie;
1003 			struct ieee80211_sta_vht_cap cap = sta->sta.deflink.vht_cap;
1004 			u32 vht_cap_info =
1005 				le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1006 
1007 			ieee80211_chandef_vht_oper(&local->hw, vht_cap_info,
1008 						   elems->vht_operation,
1009 						   elems->ht_operation,
1010 						   &chandef);
1011 			memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1012 			ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1013 							    &sband->vht_cap,
1014 							    &cap_ie, NULL,
1015 							    &sta->deflink);
1016 			if (memcmp(&cap, &sta->sta.deflink.vht_cap, sizeof(cap)))
1017 				rates_updated |= true;
1018 		}
1019 
1020 		if (bw != sta->sta.deflink.bandwidth)
1021 			rates_updated |= true;
1022 
1023 		if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1024 						 &chandef))
1025 			WARN_ON_ONCE(1);
1026 	}
1027 
1028 	if (sta && rates_updated) {
1029 		u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1030 		u8 rx_nss = sta->sta.deflink.rx_nss;
1031 
1032 		/* Force rx_nss recalculation */
1033 		sta->sta.deflink.rx_nss = 0;
1034 		rate_control_rate_init(&sta->deflink);
1035 		if (sta->sta.deflink.rx_nss != rx_nss)
1036 			changed |= IEEE80211_RC_NSS_CHANGED;
1037 
1038 		drv_link_sta_rc_update(local, sdata, &sta->sta.deflink,
1039 				       changed);
1040 	}
1041 
1042 	rcu_read_unlock();
1043 }
1044 
1045 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1046 				  struct ieee80211_mgmt *mgmt, size_t len,
1047 				  struct ieee80211_rx_status *rx_status,
1048 				  struct ieee802_11_elems *elems)
1049 {
1050 	struct ieee80211_local *local = sdata->local;
1051 	struct cfg80211_bss *cbss;
1052 	struct ieee80211_bss *bss;
1053 	struct ieee80211_channel *channel;
1054 	u64 beacon_timestamp, rx_timestamp;
1055 	u32 supp_rates = 0;
1056 	enum nl80211_band band = rx_status->band;
1057 
1058 	channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1059 	if (!channel)
1060 		return;
1061 
1062 	ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
1063 
1064 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
1065 	if (!bss)
1066 		return;
1067 
1068 	cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1069 
1070 	/* same for beacon and probe response */
1071 	beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1072 
1073 	/* check if we need to merge IBSS */
1074 
1075 	/* not an IBSS */
1076 	if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
1077 		goto put_bss;
1078 
1079 	/* different channel */
1080 	if (sdata->u.ibss.fixed_channel &&
1081 	    sdata->u.ibss.chandef.chan != cbss->channel)
1082 		goto put_bss;
1083 
1084 	/* different SSID */
1085 	if (elems->ssid_len != sdata->u.ibss.ssid_len ||
1086 	    memcmp(elems->ssid, sdata->u.ibss.ssid,
1087 				sdata->u.ibss.ssid_len))
1088 		goto put_bss;
1089 
1090 	/* process channel switch */
1091 	if (sdata->vif.bss_conf.csa_active ||
1092 	    ieee80211_ibss_process_chanswitch(sdata, elems, true))
1093 		goto put_bss;
1094 
1095 	/* same BSSID */
1096 	if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
1097 		goto put_bss;
1098 
1099 	/* we use a fixed BSSID */
1100 	if (sdata->u.ibss.fixed_bssid)
1101 		goto put_bss;
1102 
1103 	if (ieee80211_have_rx_timestamp(rx_status)) {
1104 		/* time when timestamp field was received */
1105 		rx_timestamp =
1106 			ieee80211_calculate_rx_timestamp(&local->hw, rx_status,
1107 							 len + FCS_LEN, 24);
1108 	} else {
1109 		/*
1110 		 * second best option: get current TSF
1111 		 * (will return -1 if not supported)
1112 		 */
1113 		rx_timestamp = drv_get_tsf(local, sdata);
1114 	}
1115 
1116 	ibss_dbg(sdata, "RX beacon SA=%pM BSSID=%pM TSF=0x%llx\n",
1117 		 mgmt->sa, mgmt->bssid,
1118 		 (unsigned long long)rx_timestamp);
1119 	ibss_dbg(sdata, "\tBCN=0x%llx diff=%lld @%lu\n",
1120 		 (unsigned long long)beacon_timestamp,
1121 		 (unsigned long long)(rx_timestamp - beacon_timestamp),
1122 		 jiffies);
1123 
1124 	if (beacon_timestamp > rx_timestamp) {
1125 		ibss_dbg(sdata,
1126 			 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1127 			 mgmt->bssid);
1128 		ieee80211_sta_join_ibss(sdata, bss);
1129 		supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
1130 		ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
1131 				       supp_rates);
1132 		rcu_read_unlock();
1133 	}
1134 
1135  put_bss:
1136 	ieee80211_rx_bss_put(local, bss);
1137 }
1138 
1139 void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1140 			      const u8 *bssid, const u8 *addr,
1141 			      u32 supp_rates)
1142 {
1143 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1144 	struct ieee80211_local *local = sdata->local;
1145 	struct sta_info *sta;
1146 	struct ieee80211_chanctx_conf *chanctx_conf;
1147 	struct ieee80211_supported_band *sband;
1148 	int band;
1149 
1150 	/*
1151 	 * XXX: Consider removing the least recently used entry and
1152 	 * 	allow new one to be added.
1153 	 */
1154 	if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
1155 		net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
1156 				    sdata->name, addr);
1157 		return;
1158 	}
1159 
1160 	if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
1161 		return;
1162 
1163 	if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
1164 		return;
1165 
1166 	rcu_read_lock();
1167 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1168 	if (WARN_ON_ONCE(!chanctx_conf)) {
1169 		rcu_read_unlock();
1170 		return;
1171 	}
1172 	band = chanctx_conf->def.chan->band;
1173 	rcu_read_unlock();
1174 
1175 	sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
1176 	if (!sta)
1177 		return;
1178 
1179 	/* make sure mandatory rates are always added */
1180 	sband = local->hw.wiphy->bands[band];
1181 	sta->sta.deflink.supp_rates[band] = supp_rates |
1182 			ieee80211_mandatory_rates(sband);
1183 
1184 	spin_lock(&ifibss->incomplete_lock);
1185 	list_add(&sta->list, &ifibss->incomplete_stations);
1186 	spin_unlock(&ifibss->incomplete_lock);
1187 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
1188 }
1189 
1190 static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1191 {
1192 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1193 	struct ieee80211_local *local = sdata->local;
1194 	struct sta_info *sta, *tmp;
1195 	unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
1196 	unsigned long exp_rsn = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
1197 
1198 	lockdep_assert_wiphy(local->hw.wiphy);
1199 
1200 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1201 		unsigned long last_active = ieee80211_sta_last_active(sta, -1);
1202 
1203 		if (sdata != sta->sdata)
1204 			continue;
1205 
1206 		if (time_is_before_jiffies(last_active + exp_time) ||
1207 		    (time_is_before_jiffies(last_active + exp_rsn) &&
1208 		     sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
1209 			u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1210 
1211 			sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
1212 				sta->sta_state != IEEE80211_STA_AUTHORIZED ?
1213 				"not authorized " : "", sta->sta.addr);
1214 
1215 			ieee80211_send_deauth_disassoc(sdata, sta->sta.addr,
1216 						       ifibss->bssid,
1217 						       IEEE80211_STYPE_DEAUTH,
1218 						       WLAN_REASON_DEAUTH_LEAVING,
1219 						       true, frame_buf);
1220 			WARN_ON(__sta_info_destroy(sta));
1221 		}
1222 	}
1223 }
1224 
1225 /*
1226  * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1227  */
1228 
1229 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1230 {
1231 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1232 
1233 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1234 
1235 	mod_timer(&ifibss->timer,
1236 		  round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
1237 
1238 	ieee80211_ibss_sta_expire(sdata);
1239 
1240 	if (time_before(jiffies, ifibss->last_scan_completed +
1241 		       IEEE80211_IBSS_MERGE_INTERVAL))
1242 		return;
1243 
1244 	if (ieee80211_sta_active_ibss(sdata))
1245 		return;
1246 
1247 	if (ifibss->fixed_channel)
1248 		return;
1249 
1250 	sdata_info(sdata,
1251 		   "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
1252 
1253 	ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
1254 				    NULL, 0);
1255 }
1256 
1257 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
1258 {
1259 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1260 	u8 bssid[ETH_ALEN];
1261 	u16 capability;
1262 	int i;
1263 
1264 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1265 
1266 	if (ifibss->fixed_bssid) {
1267 		memcpy(bssid, ifibss->bssid, ETH_ALEN);
1268 	} else {
1269 		/* Generate random, not broadcast, locally administered BSSID. Mix in
1270 		 * own MAC address to make sure that devices that do not have proper
1271 		 * random number generator get different BSSID. */
1272 		get_random_bytes(bssid, ETH_ALEN);
1273 		for (i = 0; i < ETH_ALEN; i++)
1274 			bssid[i] ^= sdata->vif.addr[i];
1275 		bssid[0] &= ~0x01;
1276 		bssid[0] |= 0x02;
1277 	}
1278 
1279 	sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
1280 
1281 	capability = WLAN_CAPABILITY_IBSS;
1282 
1283 	if (ifibss->privacy)
1284 		capability |= WLAN_CAPABILITY_PRIVACY;
1285 
1286 	__ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
1287 				  &ifibss->chandef, ifibss->basic_rates,
1288 				  capability, 0, true);
1289 }
1290 
1291 static unsigned int ibss_setup_channels(struct wiphy *wiphy,
1292 					struct ieee80211_channel **channels,
1293 					unsigned int channels_max,
1294 					u32 center_freq, u32 width)
1295 {
1296 	struct ieee80211_channel *chan = NULL;
1297 	unsigned int n_chan = 0;
1298 	u32 start_freq, end_freq, freq;
1299 
1300 	if (width <= 20) {
1301 		start_freq = center_freq;
1302 		end_freq = center_freq;
1303 	} else {
1304 		start_freq = center_freq - width / 2 + 10;
1305 		end_freq = center_freq + width / 2 - 10;
1306 	}
1307 
1308 	for (freq = start_freq; freq <= end_freq; freq += 20) {
1309 		chan = ieee80211_get_channel(wiphy, freq);
1310 		if (!chan)
1311 			continue;
1312 		if (n_chan >= channels_max)
1313 			return n_chan;
1314 
1315 		channels[n_chan] = chan;
1316 		n_chan++;
1317 	}
1318 
1319 	return n_chan;
1320 }
1321 
1322 static unsigned int
1323 ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1324 				   const struct cfg80211_chan_def *chandef,
1325 				   struct ieee80211_channel **channels,
1326 				   unsigned int channels_max)
1327 {
1328 	unsigned int n_chan = 0;
1329 	u32 width, cf1, cf2 = 0;
1330 
1331 	switch (chandef->width) {
1332 	case NL80211_CHAN_WIDTH_40:
1333 		width = 40;
1334 		break;
1335 	case NL80211_CHAN_WIDTH_80P80:
1336 		cf2 = chandef->center_freq2;
1337 		fallthrough;
1338 	case NL80211_CHAN_WIDTH_80:
1339 		width = 80;
1340 		break;
1341 	case NL80211_CHAN_WIDTH_160:
1342 		width = 160;
1343 		break;
1344 	default:
1345 		width = 20;
1346 		break;
1347 	}
1348 
1349 	cf1 = chandef->center_freq1;
1350 
1351 	n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1352 
1353 	if (cf2)
1354 		n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1355 					      channels_max - n_chan, cf2,
1356 					      width);
1357 
1358 	return n_chan;
1359 }
1360 
1361 /*
1362  * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1363  */
1364 
1365 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1366 {
1367 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1368 	struct ieee80211_local *local = sdata->local;
1369 	struct cfg80211_bss *cbss;
1370 	struct ieee80211_channel *chan = NULL;
1371 	const u8 *bssid = NULL;
1372 	int active_ibss;
1373 
1374 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1375 
1376 	active_ibss = ieee80211_sta_active_ibss(sdata);
1377 	ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
1378 
1379 	if (active_ibss)
1380 		return;
1381 
1382 	if (ifibss->fixed_bssid)
1383 		bssid = ifibss->bssid;
1384 	if (ifibss->fixed_channel)
1385 		chan = ifibss->chandef.chan;
1386 	if (!is_zero_ether_addr(ifibss->bssid))
1387 		bssid = ifibss->bssid;
1388 	cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1389 				ifibss->ssid, ifibss->ssid_len,
1390 				IEEE80211_BSS_TYPE_IBSS,
1391 				IEEE80211_PRIVACY(ifibss->privacy));
1392 
1393 	if (cbss) {
1394 		struct ieee80211_bss *bss;
1395 
1396 		bss = (void *)cbss->priv;
1397 		ibss_dbg(sdata,
1398 			 "sta_find_ibss: selected %pM current %pM\n",
1399 			 cbss->bssid, ifibss->bssid);
1400 		sdata_info(sdata,
1401 			   "Selected IBSS BSSID %pM based on configured SSID\n",
1402 			   cbss->bssid);
1403 
1404 		ieee80211_sta_join_ibss(sdata, bss);
1405 		ieee80211_rx_bss_put(local, bss);
1406 		return;
1407 	}
1408 
1409 	/* if a fixed bssid and a fixed freq have been provided create the IBSS
1410 	 * directly and do not waste time scanning
1411 	 */
1412 	if (ifibss->fixed_bssid && ifibss->fixed_channel) {
1413 		sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
1414 			   bssid);
1415 		ieee80211_sta_create_ibss(sdata);
1416 		return;
1417 	}
1418 
1419 
1420 	ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
1421 
1422 	/* Selected IBSS not found in current scan results - try to scan */
1423 	if (time_after(jiffies, ifibss->last_scan_completed +
1424 					IEEE80211_SCAN_INTERVAL)) {
1425 		struct ieee80211_channel *channels[8];
1426 		unsigned int num;
1427 
1428 		sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
1429 
1430 		if (ifibss->fixed_channel) {
1431 			num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1432 								 &ifibss->chandef,
1433 								 channels,
1434 								 ARRAY_SIZE(channels));
1435 			ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1436 						    ifibss->ssid_len, channels,
1437 						    num);
1438 		} else {
1439 			ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1440 						    ifibss->ssid_len, NULL, 0);
1441 		}
1442 	} else {
1443 		int interval = IEEE80211_SCAN_INTERVAL;
1444 
1445 		if (time_after(jiffies, ifibss->ibss_join_req +
1446 			       IEEE80211_IBSS_JOIN_TIMEOUT))
1447 			ieee80211_sta_create_ibss(sdata);
1448 
1449 		mod_timer(&ifibss->timer,
1450 			  round_jiffies(jiffies + interval));
1451 	}
1452 }
1453 
1454 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1455 					struct sk_buff *req)
1456 {
1457 	struct ieee80211_mgmt *mgmt = (void *)req->data;
1458 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1459 	struct ieee80211_local *local = sdata->local;
1460 	int tx_last_beacon, len = req->len;
1461 	struct sk_buff *skb;
1462 	struct beacon_data *presp;
1463 	u8 *pos, *end;
1464 
1465 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1466 
1467 	presp = sdata_dereference(ifibss->presp, sdata);
1468 
1469 	if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
1470 	    len < 24 + 2 || !presp)
1471 		return;
1472 
1473 	tx_last_beacon = drv_tx_last_beacon(local);
1474 
1475 	ibss_dbg(sdata, "RX ProbeReq SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
1476 	ibss_dbg(sdata, "\tBSSID=%pM (tx_last_beacon=%d)\n",
1477 		 mgmt->bssid, tx_last_beacon);
1478 
1479 	if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
1480 		return;
1481 
1482 	if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
1483 	    !is_broadcast_ether_addr(mgmt->bssid))
1484 		return;
1485 
1486 	end = ((u8 *) mgmt) + len;
1487 	pos = mgmt->u.probe_req.variable;
1488 	if (pos[0] != WLAN_EID_SSID ||
1489 	    pos + 2 + pos[1] > end) {
1490 		ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1491 			 mgmt->sa);
1492 		return;
1493 	}
1494 	if (pos[1] != 0 &&
1495 	    (pos[1] != ifibss->ssid_len ||
1496 	     memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
1497 		/* Ignore ProbeReq for foreign SSID */
1498 		return;
1499 	}
1500 
1501 	/* Reply with ProbeResp */
1502 	skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
1503 	if (!skb)
1504 		return;
1505 
1506 	skb_reserve(skb, local->tx_headroom);
1507 	skb_put_data(skb, presp->head, presp->head_len);
1508 
1509 	memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1510 	ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
1511 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1512 
1513 	/* avoid excessive retries for probe request to wildcard SSIDs */
1514 	if (pos[1] == 0)
1515 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
1516 
1517 	ieee80211_tx_skb(sdata, skb);
1518 }
1519 
1520 static
1521 void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
1522 				    struct ieee80211_mgmt *mgmt, size_t len,
1523 				    struct ieee80211_rx_status *rx_status)
1524 {
1525 	size_t baselen;
1526 	struct ieee802_11_elems *elems;
1527 	u16 type;
1528 
1529 	BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
1530 		     offsetof(typeof(mgmt->u.beacon), variable));
1531 
1532 	/*
1533 	 * either beacon or probe_resp but the variable field is at the
1534 	 * same offset
1535 	 */
1536 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1537 	if (baselen > len)
1538 		return;
1539 
1540 	type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE;
1541 	elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1542 				       len - baselen, type, NULL);
1543 
1544 	if (elems) {
1545 		ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, elems);
1546 		kfree(elems);
1547 	}
1548 }
1549 
1550 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1551 				   struct sk_buff *skb)
1552 {
1553 	struct ieee80211_rx_status *rx_status;
1554 	struct ieee80211_mgmt *mgmt;
1555 	u16 fc;
1556 	struct ieee802_11_elems *elems;
1557 	int ies_len;
1558 
1559 	rx_status = IEEE80211_SKB_RXCB(skb);
1560 	mgmt = (struct ieee80211_mgmt *) skb->data;
1561 	fc = le16_to_cpu(mgmt->frame_control);
1562 
1563 	if (!sdata->u.ibss.ssid_len)
1564 		return; /* not ready to merge yet */
1565 
1566 	switch (fc & IEEE80211_FCTL_STYPE) {
1567 	case IEEE80211_STYPE_PROBE_REQ:
1568 		ieee80211_rx_mgmt_probe_req(sdata, skb);
1569 		break;
1570 	case IEEE80211_STYPE_PROBE_RESP:
1571 	case IEEE80211_STYPE_BEACON:
1572 		ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1573 					       rx_status);
1574 		break;
1575 	case IEEE80211_STYPE_AUTH:
1576 		ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1577 		break;
1578 	case IEEE80211_STYPE_DEAUTH:
1579 		ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1580 		break;
1581 	case IEEE80211_STYPE_ACTION:
1582 		switch (mgmt->u.action.category) {
1583 		case WLAN_CATEGORY_SPECTRUM_MGMT:
1584 			ies_len = skb->len -
1585 				  offsetof(struct ieee80211_mgmt,
1586 					   u.action.chan_switch.variable);
1587 
1588 			if (ies_len < 0)
1589 				break;
1590 
1591 			elems = ieee802_11_parse_elems(mgmt->u.action.chan_switch.variable,
1592 						       ies_len,
1593 						       IEEE80211_FTYPE_MGMT |
1594 						       IEEE80211_STYPE_ACTION,
1595 						       NULL);
1596 
1597 			if (elems && !elems->parse_error)
1598 				ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt,
1599 								skb->len,
1600 								rx_status,
1601 								elems);
1602 			kfree(elems);
1603 			break;
1604 		}
1605 	}
1606 }
1607 
1608 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
1609 {
1610 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1611 	struct sta_info *sta;
1612 
1613 	/*
1614 	 * Work could be scheduled after scan or similar
1615 	 * when we aren't even joined (or trying) with a
1616 	 * network.
1617 	 */
1618 	if (!ifibss->ssid_len)
1619 		return;
1620 
1621 	spin_lock_bh(&ifibss->incomplete_lock);
1622 	while (!list_empty(&ifibss->incomplete_stations)) {
1623 		sta = list_first_entry(&ifibss->incomplete_stations,
1624 				       struct sta_info, list);
1625 		list_del(&sta->list);
1626 		spin_unlock_bh(&ifibss->incomplete_lock);
1627 
1628 		ieee80211_ibss_finish_sta(sta);
1629 		rcu_read_unlock();
1630 		spin_lock_bh(&ifibss->incomplete_lock);
1631 	}
1632 	spin_unlock_bh(&ifibss->incomplete_lock);
1633 
1634 	switch (ifibss->state) {
1635 	case IEEE80211_IBSS_MLME_SEARCH:
1636 		ieee80211_sta_find_ibss(sdata);
1637 		break;
1638 	case IEEE80211_IBSS_MLME_JOINED:
1639 		ieee80211_sta_merge_ibss(sdata);
1640 		break;
1641 	default:
1642 		WARN_ON(1);
1643 		break;
1644 	}
1645 }
1646 
1647 static void ieee80211_ibss_timer(struct timer_list *t)
1648 {
1649 	struct ieee80211_sub_if_data *sdata =
1650 		timer_container_of(sdata, t, u.ibss.timer);
1651 
1652 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
1653 }
1654 
1655 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1656 {
1657 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1658 
1659 	timer_setup(&ifibss->timer, ieee80211_ibss_timer, 0);
1660 	INIT_LIST_HEAD(&ifibss->incomplete_stations);
1661 	spin_lock_init(&ifibss->incomplete_lock);
1662 	wiphy_work_init(&ifibss->csa_connection_drop_work,
1663 			ieee80211_csa_connection_drop_work);
1664 }
1665 
1666 /* scan finished notification */
1667 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1668 {
1669 	struct ieee80211_sub_if_data *sdata;
1670 
1671 	lockdep_assert_wiphy(local->hw.wiphy);
1672 
1673 	list_for_each_entry(sdata, &local->interfaces, list) {
1674 		if (!ieee80211_sdata_running(sdata))
1675 			continue;
1676 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1677 			continue;
1678 		sdata->u.ibss.last_scan_completed = jiffies;
1679 	}
1680 }
1681 
1682 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1683 			struct cfg80211_ibss_params *params)
1684 {
1685 	u64 changed = 0;
1686 	enum ieee80211_chanctx_mode chanmode;
1687 	struct ieee80211_local *local = sdata->local;
1688 	int radar_detect_width = 0;
1689 	int ret;
1690 
1691 	lockdep_assert_wiphy(local->hw.wiphy);
1692 
1693 	if (params->chandef.chan->freq_offset) {
1694 		/* this may work, but is untested */
1695 		return -EOPNOTSUPP;
1696 	}
1697 
1698 	ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1699 					    &params->chandef,
1700 					    sdata->wdev.iftype);
1701 	if (ret < 0)
1702 		return ret;
1703 
1704 	if (ret > 0) {
1705 		if (!params->userspace_handles_dfs)
1706 			return -EINVAL;
1707 		radar_detect_width = BIT(params->chandef.width);
1708 	}
1709 
1710 	chanmode = (params->channel_fixed && !ret) ?
1711 		IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
1712 
1713 	ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
1714 					   radar_detect_width, -1);
1715 	if (ret < 0)
1716 		return ret;
1717 
1718 	if (params->bssid) {
1719 		memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1720 		sdata->u.ibss.fixed_bssid = true;
1721 	} else
1722 		sdata->u.ibss.fixed_bssid = false;
1723 
1724 	sdata->u.ibss.privacy = params->privacy;
1725 	sdata->u.ibss.control_port = params->control_port;
1726 	sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
1727 	sdata->u.ibss.basic_rates = params->basic_rates;
1728 	sdata->u.ibss.last_scan_completed = jiffies;
1729 
1730 	/* fix basic_rates if channel does not support these rates */
1731 	memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1732 	       sizeof(params->mcast_rate));
1733 
1734 	sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1735 
1736 	sdata->u.ibss.chandef = params->chandef;
1737 	sdata->u.ibss.fixed_channel = params->channel_fixed;
1738 
1739 	if (params->ie) {
1740 		sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1741 					   GFP_KERNEL);
1742 		if (sdata->u.ibss.ie)
1743 			sdata->u.ibss.ie_len = params->ie_len;
1744 	}
1745 
1746 	sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1747 	sdata->u.ibss.ibss_join_req = jiffies;
1748 
1749 	memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
1750 	sdata->u.ibss.ssid_len = params->ssid_len;
1751 
1752 	memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1753 	       sizeof(sdata->u.ibss.ht_capa));
1754 	memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1755 	       sizeof(sdata->u.ibss.ht_capa_mask));
1756 
1757 	/*
1758 	 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1759 	 * reserved, but an HT STA shall protect HT transmissions as though
1760 	 * the HT Protection field were set to non-HT mixed mode.
1761 	 *
1762 	 * In an IBSS, the RIFS Mode field of the HT Operation element is
1763 	 * also reserved, but an HT STA shall operate as though this field
1764 	 * were set to 1.
1765 	 */
1766 
1767 	sdata->vif.bss_conf.ht_operation_mode |=
1768 		  IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1769 		| IEEE80211_HT_PARAM_RIFS_MODE;
1770 
1771 	changed |= BSS_CHANGED_HT | BSS_CHANGED_MCAST_RATE;
1772 	ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1773 
1774 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
1775 	sdata->deflink.needed_rx_chains = local->rx_chains;
1776 	sdata->control_port_over_nl80211 = params->control_port_over_nl80211;
1777 
1778 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
1779 
1780 	return 0;
1781 }
1782 
1783 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1784 {
1785 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1786 
1787 	ifibss->ssid_len = 0;
1788 	ieee80211_ibss_disconnect(sdata);
1789 	eth_zero_addr(ifibss->bssid);
1790 
1791 	/* remove beacon */
1792 	kfree(sdata->u.ibss.ie);
1793 	sdata->u.ibss.ie = NULL;
1794 	sdata->u.ibss.ie_len = 0;
1795 
1796 	/* on the next join, re-program HT parameters */
1797 	memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1798 	memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1799 
1800 	synchronize_rcu();
1801 
1802 	skb_queue_purge(&sdata->skb_queue);
1803 
1804 	timer_delete_sync(&sdata->u.ibss.timer);
1805 
1806 	return 0;
1807 }
1808