xref: /linux/net/mac80211/ibss.c (revision fafb66e5903c2bcfc7b7e259042a8282f18a6faa)
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 	netif_carrier_off(sdata->dev);
672 	if (!sta_info_flush(sdata, -1))
673 		synchronize_net();
674 
675 	spin_lock_bh(&ifibss->incomplete_lock);
676 	while (!list_empty(&ifibss->incomplete_stations)) {
677 		sta = list_first_entry(&ifibss->incomplete_stations,
678 				       struct sta_info, list);
679 		list_del(&sta->list);
680 		spin_unlock_bh(&ifibss->incomplete_lock);
681 
682 		sta_info_free(local, sta);
683 		spin_lock_bh(&ifibss->incomplete_lock);
684 	}
685 	spin_unlock_bh(&ifibss->incomplete_lock);
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 	skb_queue_purge(&sdata->skb_queue);
714 
715 	/* trigger a scan to find another IBSS network to join */
716 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
717 }
718 
719 static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
720 {
721 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
722 	int err;
723 
724 	/* if the current channel is a DFS channel, mark the channel as
725 	 * unavailable.
726 	 */
727 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
728 					    &ifibss->chandef,
729 					    NL80211_IFTYPE_ADHOC);
730 	if (err > 0)
731 		cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
732 				     GFP_ATOMIC);
733 }
734 
735 static bool
736 ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
737 				  struct ieee802_11_elems *elems,
738 				  bool beacon)
739 {
740 	struct cfg80211_csa_settings params;
741 	struct ieee80211_csa_ie csa_ie;
742 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
743 	enum nl80211_channel_type ch_type;
744 	int err;
745 	struct ieee80211_conn_settings conn = {
746 		.mode = IEEE80211_CONN_MODE_HT,
747 		.bw_limit = IEEE80211_CONN_BW_LIMIT_40,
748 	};
749 	u32 vht_cap_info = 0;
750 
751 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
752 
753 	switch (ifibss->chandef.width) {
754 	case NL80211_CHAN_WIDTH_20_NOHT:
755 		conn.mode = IEEE80211_CONN_MODE_LEGACY;
756 		fallthrough;
757 	case NL80211_CHAN_WIDTH_20:
758 		conn.bw_limit = IEEE80211_CONN_BW_LIMIT_20;
759 		break;
760 	default:
761 		break;
762 	}
763 
764 	if (elems->vht_cap_elem)
765 		vht_cap_info = le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
766 
767 	memset(&params, 0, sizeof(params));
768 	err = ieee80211_parse_ch_switch_ie(sdata, elems,
769 					   ifibss->chandef.chan->band,
770 					   vht_cap_info, &conn,
771 					   ifibss->bssid, false,
772 					   &csa_ie);
773 	/* can't switch to destination channel, fail */
774 	if (err < 0)
775 		goto disconnect;
776 
777 	/* did not contain a CSA */
778 	if (err)
779 		return false;
780 
781 	/* channel switch is not supported, disconnect */
782 	if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
783 		goto disconnect;
784 
785 	params.count = csa_ie.count;
786 	params.chandef = csa_ie.chanreq.oper;
787 
788 	switch (ifibss->chandef.width) {
789 	case NL80211_CHAN_WIDTH_20_NOHT:
790 	case NL80211_CHAN_WIDTH_20:
791 	case NL80211_CHAN_WIDTH_40:
792 		/* keep our current HT mode (HT20/HT40+/HT40-), even if
793 		 * another mode  has been announced. The mode is not adopted
794 		 * within the beacon while doing CSA and we should therefore
795 		 * keep the mode which we announce.
796 		 */
797 		ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
798 		cfg80211_chandef_create(&params.chandef, params.chandef.chan,
799 					ch_type);
800 		break;
801 	default:
802 		/* should not happen, conn_flags should prevent VHT modes. */
803 		WARN_ON(1);
804 		goto disconnect;
805 	}
806 
807 	if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
808 				     NL80211_IFTYPE_ADHOC)) {
809 		sdata_info(sdata,
810 			   "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
811 			   ifibss->bssid,
812 			   params.chandef.chan->center_freq,
813 			   params.chandef.width,
814 			   params.chandef.center_freq1,
815 			   params.chandef.center_freq2);
816 		goto disconnect;
817 	}
818 
819 	err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
820 					    &params.chandef,
821 					    NL80211_IFTYPE_ADHOC);
822 	if (err < 0)
823 		goto disconnect;
824 	if (err > 0 && !ifibss->userspace_handles_dfs) {
825 		/* IBSS-DFS only allowed with a control program */
826 		goto disconnect;
827 	}
828 
829 	params.radar_required = err;
830 
831 	if (cfg80211_chandef_identical(&params.chandef,
832 				       &sdata->vif.bss_conf.chanreq.oper)) {
833 		ibss_dbg(sdata,
834 			 "received csa with an identical chandef, ignoring\n");
835 		return true;
836 	}
837 
838 	/* all checks done, now perform the channel switch. */
839 	ibss_dbg(sdata,
840 		 "received channel switch announcement to go to channel %d MHz\n",
841 		 params.chandef.chan->center_freq);
842 
843 	params.block_tx = !!csa_ie.mode;
844 
845 	if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
846 				     &params))
847 		goto disconnect;
848 
849 	ieee80211_ibss_csa_mark_radar(sdata);
850 
851 	return true;
852 disconnect:
853 	ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
854 	wiphy_work_queue(sdata->local->hw.wiphy,
855 			 &ifibss->csa_connection_drop_work);
856 
857 	ieee80211_ibss_csa_mark_radar(sdata);
858 
859 	return true;
860 }
861 
862 static void
863 ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
864 				struct ieee80211_mgmt *mgmt, size_t len,
865 				struct ieee80211_rx_status *rx_status,
866 				struct ieee802_11_elems *elems)
867 {
868 	if (len < IEEE80211_MIN_ACTION_SIZE(chan_switch))
869 		return;
870 
871 	/* CSA is the only action we handle for now */
872 	if (mgmt->u.action.action_code != WLAN_ACTION_SPCT_CHL_SWITCH)
873 		return;
874 
875 	if (!sdata->vif.bss_conf.csa_active)
876 		ieee80211_ibss_process_chanswitch(sdata, elems, false);
877 }
878 
879 static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
880 					  struct ieee80211_mgmt *mgmt,
881 					  size_t len)
882 {
883 	u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
884 
885 	if (len < IEEE80211_DEAUTH_FRAME_LEN)
886 		return;
887 
888 	ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
889 	ibss_dbg(sdata, "\tBSSID=%pM (reason: %d)\n", mgmt->bssid, reason);
890 	sta_info_destroy_addr(sdata, mgmt->sa);
891 }
892 
893 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
894 					struct ieee80211_mgmt *mgmt,
895 					size_t len)
896 {
897 	u16 auth_alg, auth_transaction;
898 
899 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
900 
901 	if (len < 24 + 6)
902 		return;
903 
904 	auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
905 	auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
906 
907 	ibss_dbg(sdata, "RX Auth SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
908 	ibss_dbg(sdata, "\tBSSID=%pM (auth_transaction=%d)\n",
909 		 mgmt->bssid, auth_transaction);
910 
911 	if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
912 		return;
913 
914 	/*
915 	 * IEEE 802.11 standard does not require authentication in IBSS
916 	 * networks and most implementations do not seem to use it.
917 	 * However, try to reply to authentication attempts if someone
918 	 * has actually implemented this.
919 	 */
920 	ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
921 			    mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
922 }
923 
924 static void ieee80211_update_sta_info(struct ieee80211_sub_if_data *sdata,
925 				      struct ieee80211_mgmt *mgmt, size_t len,
926 				      struct ieee80211_rx_status *rx_status,
927 				      struct ieee802_11_elems *elems,
928 				      struct ieee80211_channel *channel)
929 {
930 	struct sta_info *sta;
931 	enum nl80211_band band = rx_status->band;
932 	struct ieee80211_local *local = sdata->local;
933 	struct ieee80211_supported_band *sband;
934 	bool rates_updated = false;
935 	u32 supp_rates = 0;
936 
937 	if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
938 		return;
939 
940 	if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
941 		return;
942 
943 	sband = local->hw.wiphy->bands[band];
944 	if (WARN_ON(!sband))
945 		return;
946 
947 	rcu_read_lock();
948 	sta = sta_info_get(sdata, mgmt->sa);
949 
950 	if (elems->supp_rates) {
951 		supp_rates = ieee80211_sta_get_rates(sdata, elems,
952 						     band, NULL);
953 		if (sta) {
954 			u32 prev_rates;
955 
956 			prev_rates = sta->sta.deflink.supp_rates[band];
957 
958 			sta->sta.deflink.supp_rates[band] = supp_rates |
959 				ieee80211_mandatory_rates(sband);
960 			if (sta->sta.deflink.supp_rates[band] != prev_rates) {
961 				ibss_dbg(sdata,
962 					 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
963 					 sta->sta.addr, prev_rates,
964 					 sta->sta.deflink.supp_rates[band]);
965 				rates_updated = true;
966 			}
967 		} else {
968 			rcu_read_unlock();
969 			sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
970 						     mgmt->sa, supp_rates);
971 		}
972 	}
973 
974 	if (sta && !sta->sta.wme &&
975 	    (elems->wmm_info || elems->s1g_capab) &&
976 	    local->hw.queues >= IEEE80211_NUM_ACS) {
977 		sta->sta.wme = true;
978 		ieee80211_check_fast_xmit(sta);
979 	}
980 
981 	if (sta && elems->ht_operation && elems->ht_cap_elem &&
982 	    sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
983 		/* we both use HT */
984 		struct ieee80211_ht_cap htcap_ie;
985 		struct cfg80211_chan_def chandef;
986 		enum ieee80211_sta_rx_bandwidth bw = sta->sta.deflink.bandwidth;
987 
988 		cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
989 		ieee80211_chandef_ht_oper(elems->ht_operation, &chandef);
990 
991 		memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
992 		rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata,
993 								   &sband->ht_cap,
994 								   &htcap_ie,
995 								   &sta->deflink);
996 
997 		if (elems->vht_operation && elems->vht_cap_elem &&
998 		    sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
999 		    sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
1000 			/* we both use VHT */
1001 			struct ieee80211_vht_cap cap_ie;
1002 			struct ieee80211_sta_vht_cap cap = sta->sta.deflink.vht_cap;
1003 			u32 vht_cap_info =
1004 				le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1005 
1006 			ieee80211_chandef_vht_oper(&local->hw, vht_cap_info,
1007 						   elems->vht_operation,
1008 						   elems->ht_operation,
1009 						   &chandef);
1010 			memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1011 			ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1012 							    &sband->vht_cap,
1013 							    &cap_ie, NULL,
1014 							    &sta->deflink);
1015 			if (memcmp(&cap, &sta->sta.deflink.vht_cap, sizeof(cap)))
1016 				rates_updated |= true;
1017 		}
1018 
1019 		if (bw != sta->sta.deflink.bandwidth)
1020 			rates_updated |= true;
1021 
1022 		if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1023 						 &chandef))
1024 			WARN_ON_ONCE(1);
1025 	}
1026 
1027 	if (sta && rates_updated) {
1028 		u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1029 		u8 rx_nss = sta->sta.deflink.rx_nss;
1030 
1031 		ieee80211_sta_init_nss_bw_capa(&sta->deflink,
1032 					       &sdata->deflink.conf->chanreq.oper);
1033 		rate_control_rate_init(&sta->deflink);
1034 		if (sta->sta.deflink.rx_nss != rx_nss)
1035 			changed |= IEEE80211_RC_NSS_CHANGED;
1036 
1037 		drv_link_sta_rc_update(local, sdata, &sta->sta.deflink,
1038 				       changed);
1039 	}
1040 
1041 	rcu_read_unlock();
1042 }
1043 
1044 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1045 				  struct ieee80211_mgmt *mgmt, size_t len,
1046 				  struct ieee80211_rx_status *rx_status,
1047 				  struct ieee802_11_elems *elems)
1048 {
1049 	struct ieee80211_local *local = sdata->local;
1050 	struct cfg80211_bss *cbss;
1051 	struct ieee80211_bss *bss;
1052 	struct ieee80211_channel *channel;
1053 	u64 beacon_timestamp, rx_timestamp;
1054 	u32 supp_rates = 0;
1055 	enum nl80211_band band = rx_status->band;
1056 
1057 	channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1058 	if (!channel)
1059 		return;
1060 
1061 	ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
1062 
1063 	bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
1064 	if (!bss)
1065 		return;
1066 
1067 	cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1068 
1069 	/* same for beacon and probe response */
1070 	beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1071 
1072 	/* check if we need to merge IBSS */
1073 
1074 	/* not an IBSS */
1075 	if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
1076 		goto put_bss;
1077 
1078 	/* different channel */
1079 	if (sdata->u.ibss.fixed_channel &&
1080 	    sdata->u.ibss.chandef.chan != cbss->channel)
1081 		goto put_bss;
1082 
1083 	/* different SSID */
1084 	if (elems->ssid_len != sdata->u.ibss.ssid_len ||
1085 	    memcmp(elems->ssid, sdata->u.ibss.ssid,
1086 				sdata->u.ibss.ssid_len))
1087 		goto put_bss;
1088 
1089 	/* process channel switch */
1090 	if (sdata->vif.bss_conf.csa_active ||
1091 	    ieee80211_ibss_process_chanswitch(sdata, elems, true))
1092 		goto put_bss;
1093 
1094 	/* same BSSID */
1095 	if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
1096 		goto put_bss;
1097 
1098 	/* we use a fixed BSSID */
1099 	if (sdata->u.ibss.fixed_bssid)
1100 		goto put_bss;
1101 
1102 	if (ieee80211_have_rx_timestamp(rx_status)) {
1103 		/* time when timestamp field was received */
1104 		rx_timestamp =
1105 			ieee80211_calculate_rx_timestamp(&local->hw, rx_status,
1106 							 len + FCS_LEN, 24);
1107 	} else {
1108 		/*
1109 		 * second best option: get current TSF
1110 		 * (will return -1 if not supported)
1111 		 */
1112 		rx_timestamp = drv_get_tsf(local, sdata);
1113 	}
1114 
1115 	ibss_dbg(sdata, "RX beacon SA=%pM BSSID=%pM TSF=0x%llx\n",
1116 		 mgmt->sa, mgmt->bssid,
1117 		 (unsigned long long)rx_timestamp);
1118 	ibss_dbg(sdata, "\tBCN=0x%llx diff=%lld @%lu\n",
1119 		 (unsigned long long)beacon_timestamp,
1120 		 (unsigned long long)(rx_timestamp - beacon_timestamp),
1121 		 jiffies);
1122 
1123 	if (beacon_timestamp > rx_timestamp) {
1124 		ibss_dbg(sdata,
1125 			 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1126 			 mgmt->bssid);
1127 		ieee80211_sta_join_ibss(sdata, bss);
1128 		supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
1129 		ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
1130 				       supp_rates);
1131 		rcu_read_unlock();
1132 	}
1133 
1134  put_bss:
1135 	ieee80211_rx_bss_put(local, bss);
1136 }
1137 
1138 void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1139 			      const u8 *bssid, const u8 *addr,
1140 			      u32 supp_rates)
1141 {
1142 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1143 	struct ieee80211_local *local = sdata->local;
1144 	struct sta_info *sta;
1145 	struct ieee80211_chanctx_conf *chanctx_conf;
1146 	struct ieee80211_supported_band *sband;
1147 	int band;
1148 
1149 	/*
1150 	 * XXX: Consider removing the least recently used entry and
1151 	 * 	allow new one to be added.
1152 	 */
1153 	if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
1154 		net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
1155 				    sdata->name, addr);
1156 		return;
1157 	}
1158 
1159 	if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
1160 		return;
1161 
1162 	if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
1163 		return;
1164 
1165 	rcu_read_lock();
1166 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1167 	if (WARN_ON_ONCE(!chanctx_conf)) {
1168 		rcu_read_unlock();
1169 		return;
1170 	}
1171 	band = chanctx_conf->def.chan->band;
1172 	rcu_read_unlock();
1173 
1174 	sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
1175 	if (!sta)
1176 		return;
1177 
1178 	/* make sure mandatory rates are always added */
1179 	sband = local->hw.wiphy->bands[band];
1180 	sta->sta.deflink.supp_rates[band] = supp_rates |
1181 			ieee80211_mandatory_rates(sband);
1182 
1183 	spin_lock(&ifibss->incomplete_lock);
1184 	list_add(&sta->list, &ifibss->incomplete_stations);
1185 	spin_unlock(&ifibss->incomplete_lock);
1186 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
1187 }
1188 
1189 static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1190 {
1191 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1192 	struct ieee80211_local *local = sdata->local;
1193 	struct sta_info *sta, *tmp;
1194 	unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
1195 	unsigned long exp_rsn = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
1196 
1197 	lockdep_assert_wiphy(local->hw.wiphy);
1198 
1199 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1200 		unsigned long last_active = ieee80211_sta_last_active(sta, -1);
1201 
1202 		if (sdata != sta->sdata)
1203 			continue;
1204 
1205 		if (time_is_before_jiffies(last_active + exp_time) ||
1206 		    (time_is_before_jiffies(last_active + exp_rsn) &&
1207 		     sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
1208 			u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1209 
1210 			sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
1211 				sta->sta_state != IEEE80211_STA_AUTHORIZED ?
1212 				"not authorized " : "", sta->sta.addr);
1213 
1214 			ieee80211_send_deauth_disassoc(sdata, sta->sta.addr,
1215 						       ifibss->bssid,
1216 						       IEEE80211_STYPE_DEAUTH,
1217 						       WLAN_REASON_DEAUTH_LEAVING,
1218 						       true, frame_buf);
1219 			WARN_ON(__sta_info_destroy(sta));
1220 		}
1221 	}
1222 }
1223 
1224 /*
1225  * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1226  */
1227 
1228 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1229 {
1230 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1231 
1232 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1233 
1234 	mod_timer(&ifibss->timer,
1235 		  round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
1236 
1237 	ieee80211_ibss_sta_expire(sdata);
1238 
1239 	if (time_before(jiffies, ifibss->last_scan_completed +
1240 		       IEEE80211_IBSS_MERGE_INTERVAL))
1241 		return;
1242 
1243 	if (ieee80211_sta_active_ibss(sdata))
1244 		return;
1245 
1246 	if (ifibss->fixed_channel)
1247 		return;
1248 
1249 	sdata_info(sdata,
1250 		   "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
1251 
1252 	ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
1253 				    NULL, 0);
1254 }
1255 
1256 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
1257 {
1258 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1259 	u8 bssid[ETH_ALEN];
1260 	u16 capability;
1261 	int i;
1262 
1263 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1264 
1265 	if (ifibss->fixed_bssid) {
1266 		memcpy(bssid, ifibss->bssid, ETH_ALEN);
1267 	} else {
1268 		/* Generate random, not broadcast, locally administered BSSID. Mix in
1269 		 * own MAC address to make sure that devices that do not have proper
1270 		 * random number generator get different BSSID. */
1271 		get_random_bytes(bssid, ETH_ALEN);
1272 		for (i = 0; i < ETH_ALEN; i++)
1273 			bssid[i] ^= sdata->vif.addr[i];
1274 		bssid[0] &= ~0x01;
1275 		bssid[0] |= 0x02;
1276 	}
1277 
1278 	sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
1279 
1280 	capability = WLAN_CAPABILITY_IBSS;
1281 
1282 	if (ifibss->privacy)
1283 		capability |= WLAN_CAPABILITY_PRIVACY;
1284 
1285 	__ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
1286 				  &ifibss->chandef, ifibss->basic_rates,
1287 				  capability, 0, true);
1288 }
1289 
1290 static unsigned int ibss_setup_channels(struct wiphy *wiphy,
1291 					struct ieee80211_channel **channels,
1292 					unsigned int channels_max,
1293 					u32 center_freq, u32 width)
1294 {
1295 	struct ieee80211_channel *chan = NULL;
1296 	unsigned int n_chan = 0;
1297 	u32 start_freq, end_freq, freq;
1298 
1299 	if (width <= 20) {
1300 		start_freq = center_freq;
1301 		end_freq = center_freq;
1302 	} else {
1303 		start_freq = center_freq - width / 2 + 10;
1304 		end_freq = center_freq + width / 2 - 10;
1305 	}
1306 
1307 	for (freq = start_freq; freq <= end_freq; freq += 20) {
1308 		chan = ieee80211_get_channel(wiphy, freq);
1309 		if (!chan)
1310 			continue;
1311 		if (n_chan >= channels_max)
1312 			return n_chan;
1313 
1314 		channels[n_chan] = chan;
1315 		n_chan++;
1316 	}
1317 
1318 	return n_chan;
1319 }
1320 
1321 static unsigned int
1322 ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1323 				   const struct cfg80211_chan_def *chandef,
1324 				   struct ieee80211_channel **channels,
1325 				   unsigned int channels_max)
1326 {
1327 	unsigned int n_chan = 0;
1328 	u32 width, cf1, cf2 = 0;
1329 
1330 	switch (chandef->width) {
1331 	case NL80211_CHAN_WIDTH_40:
1332 		width = 40;
1333 		break;
1334 	case NL80211_CHAN_WIDTH_80P80:
1335 		cf2 = chandef->center_freq2;
1336 		fallthrough;
1337 	case NL80211_CHAN_WIDTH_80:
1338 		width = 80;
1339 		break;
1340 	case NL80211_CHAN_WIDTH_160:
1341 		width = 160;
1342 		break;
1343 	default:
1344 		width = 20;
1345 		break;
1346 	}
1347 
1348 	cf1 = chandef->center_freq1;
1349 
1350 	n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1351 
1352 	if (cf2)
1353 		n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1354 					      channels_max - n_chan, cf2,
1355 					      width);
1356 
1357 	return n_chan;
1358 }
1359 
1360 /*
1361  * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1362  */
1363 
1364 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1365 {
1366 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1367 	struct ieee80211_local *local = sdata->local;
1368 	struct cfg80211_bss *cbss;
1369 	struct ieee80211_channel *chan = NULL;
1370 	const u8 *bssid = NULL;
1371 	int active_ibss;
1372 
1373 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1374 
1375 	active_ibss = ieee80211_sta_active_ibss(sdata);
1376 	ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
1377 
1378 	if (active_ibss)
1379 		return;
1380 
1381 	if (ifibss->fixed_bssid)
1382 		bssid = ifibss->bssid;
1383 	if (ifibss->fixed_channel)
1384 		chan = ifibss->chandef.chan;
1385 	if (!is_zero_ether_addr(ifibss->bssid))
1386 		bssid = ifibss->bssid;
1387 	cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1388 				ifibss->ssid, ifibss->ssid_len,
1389 				IEEE80211_BSS_TYPE_IBSS,
1390 				IEEE80211_PRIVACY(ifibss->privacy));
1391 
1392 	if (cbss) {
1393 		struct ieee80211_bss *bss;
1394 
1395 		bss = (void *)cbss->priv;
1396 		ibss_dbg(sdata,
1397 			 "sta_find_ibss: selected %pM current %pM\n",
1398 			 cbss->bssid, ifibss->bssid);
1399 		sdata_info(sdata,
1400 			   "Selected IBSS BSSID %pM based on configured SSID\n",
1401 			   cbss->bssid);
1402 
1403 		ieee80211_sta_join_ibss(sdata, bss);
1404 		ieee80211_rx_bss_put(local, bss);
1405 		return;
1406 	}
1407 
1408 	/* if a fixed bssid and a fixed freq have been provided create the IBSS
1409 	 * directly and do not waste time scanning
1410 	 */
1411 	if (ifibss->fixed_bssid && ifibss->fixed_channel) {
1412 		sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
1413 			   bssid);
1414 		ieee80211_sta_create_ibss(sdata);
1415 		return;
1416 	}
1417 
1418 
1419 	ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
1420 
1421 	/* Selected IBSS not found in current scan results - try to scan */
1422 	if (time_after(jiffies, ifibss->last_scan_completed +
1423 					IEEE80211_SCAN_INTERVAL)) {
1424 		struct ieee80211_channel *channels[8];
1425 		unsigned int num;
1426 
1427 		sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
1428 
1429 		if (ifibss->fixed_channel) {
1430 			num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1431 								 &ifibss->chandef,
1432 								 channels,
1433 								 ARRAY_SIZE(channels));
1434 			ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1435 						    ifibss->ssid_len, channels,
1436 						    num);
1437 		} else {
1438 			ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1439 						    ifibss->ssid_len, NULL, 0);
1440 		}
1441 	} else {
1442 		int interval = IEEE80211_SCAN_INTERVAL;
1443 
1444 		if (time_after(jiffies, ifibss->ibss_join_req +
1445 			       IEEE80211_IBSS_JOIN_TIMEOUT))
1446 			ieee80211_sta_create_ibss(sdata);
1447 
1448 		mod_timer(&ifibss->timer,
1449 			  round_jiffies(jiffies + interval));
1450 	}
1451 }
1452 
1453 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1454 					struct sk_buff *req)
1455 {
1456 	struct ieee80211_mgmt *mgmt = (void *)req->data;
1457 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1458 	struct ieee80211_local *local = sdata->local;
1459 	int tx_last_beacon, len = req->len;
1460 	struct sk_buff *skb;
1461 	struct beacon_data *presp;
1462 	u8 *pos, *end;
1463 
1464 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1465 
1466 	presp = sdata_dereference(ifibss->presp, sdata);
1467 
1468 	if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
1469 	    len < 24 + 2 || !presp)
1470 		return;
1471 
1472 	tx_last_beacon = drv_tx_last_beacon(local);
1473 
1474 	ibss_dbg(sdata, "RX ProbeReq SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
1475 	ibss_dbg(sdata, "\tBSSID=%pM (tx_last_beacon=%d)\n",
1476 		 mgmt->bssid, tx_last_beacon);
1477 
1478 	if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
1479 		return;
1480 
1481 	if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
1482 	    !is_broadcast_ether_addr(mgmt->bssid))
1483 		return;
1484 
1485 	end = ((u8 *) mgmt) + len;
1486 	pos = mgmt->u.probe_req.variable;
1487 	if (pos[0] != WLAN_EID_SSID ||
1488 	    pos + 2 + pos[1] > end) {
1489 		ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1490 			 mgmt->sa);
1491 		return;
1492 	}
1493 	if (pos[1] != 0 &&
1494 	    (pos[1] != ifibss->ssid_len ||
1495 	     memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
1496 		/* Ignore ProbeReq for foreign SSID */
1497 		return;
1498 	}
1499 
1500 	/* Reply with ProbeResp */
1501 	skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
1502 	if (!skb)
1503 		return;
1504 
1505 	skb_reserve(skb, local->tx_headroom);
1506 	skb_put_data(skb, presp->head, presp->head_len);
1507 
1508 	memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1509 	ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
1510 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1511 
1512 	/* avoid excessive retries for probe request to wildcard SSIDs */
1513 	if (pos[1] == 0)
1514 		IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
1515 
1516 	ieee80211_tx_skb(sdata, skb);
1517 }
1518 
1519 static
1520 void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
1521 				    struct ieee80211_mgmt *mgmt, size_t len,
1522 				    struct ieee80211_rx_status *rx_status)
1523 {
1524 	size_t baselen;
1525 	struct ieee802_11_elems *elems;
1526 	u16 type;
1527 
1528 	BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
1529 		     offsetof(typeof(mgmt->u.beacon), variable));
1530 
1531 	/*
1532 	 * either beacon or probe_resp but the variable field is at the
1533 	 * same offset
1534 	 */
1535 	baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1536 	if (baselen > len)
1537 		return;
1538 
1539 	type = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_TYPE;
1540 	elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1541 				       len - baselen, type, NULL);
1542 
1543 	if (elems) {
1544 		ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, elems);
1545 		kfree(elems);
1546 	}
1547 }
1548 
1549 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1550 				   struct sk_buff *skb)
1551 {
1552 	struct ieee80211_rx_status *rx_status;
1553 	struct ieee80211_mgmt *mgmt;
1554 	u16 fc;
1555 	struct ieee802_11_elems *elems;
1556 	int ies_len;
1557 
1558 	rx_status = IEEE80211_SKB_RXCB(skb);
1559 	mgmt = (struct ieee80211_mgmt *) skb->data;
1560 	fc = le16_to_cpu(mgmt->frame_control);
1561 
1562 	if (!sdata->u.ibss.ssid_len)
1563 		return; /* not ready to merge yet */
1564 
1565 	switch (fc & IEEE80211_FCTL_STYPE) {
1566 	case IEEE80211_STYPE_PROBE_REQ:
1567 		ieee80211_rx_mgmt_probe_req(sdata, skb);
1568 		break;
1569 	case IEEE80211_STYPE_PROBE_RESP:
1570 	case IEEE80211_STYPE_BEACON:
1571 		ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1572 					       rx_status);
1573 		break;
1574 	case IEEE80211_STYPE_AUTH:
1575 		ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1576 		break;
1577 	case IEEE80211_STYPE_DEAUTH:
1578 		ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1579 		break;
1580 	case IEEE80211_STYPE_ACTION:
1581 		switch (mgmt->u.action.category) {
1582 		case WLAN_CATEGORY_SPECTRUM_MGMT:
1583 			ies_len = skb->len -
1584 				  offsetof(struct ieee80211_mgmt,
1585 					   u.action.chan_switch.variable);
1586 
1587 			if (ies_len < 0)
1588 				break;
1589 
1590 			elems = ieee802_11_parse_elems(mgmt->u.action.chan_switch.variable,
1591 						       ies_len,
1592 						       IEEE80211_FTYPE_MGMT |
1593 						       IEEE80211_STYPE_ACTION,
1594 						       NULL);
1595 
1596 			if (elems && !elems->parse_error)
1597 				ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt,
1598 								skb->len,
1599 								rx_status,
1600 								elems);
1601 			kfree(elems);
1602 			break;
1603 		}
1604 	}
1605 }
1606 
1607 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
1608 {
1609 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1610 	struct sta_info *sta;
1611 
1612 	/*
1613 	 * Work could be scheduled after scan or similar
1614 	 * when we aren't even joined (or trying) with a
1615 	 * network.
1616 	 */
1617 	if (!ifibss->ssid_len)
1618 		return;
1619 
1620 	spin_lock_bh(&ifibss->incomplete_lock);
1621 	while (!list_empty(&ifibss->incomplete_stations)) {
1622 		sta = list_first_entry(&ifibss->incomplete_stations,
1623 				       struct sta_info, list);
1624 		list_del(&sta->list);
1625 		spin_unlock_bh(&ifibss->incomplete_lock);
1626 
1627 		ieee80211_ibss_finish_sta(sta);
1628 		rcu_read_unlock();
1629 		spin_lock_bh(&ifibss->incomplete_lock);
1630 	}
1631 	spin_unlock_bh(&ifibss->incomplete_lock);
1632 
1633 	switch (ifibss->state) {
1634 	case IEEE80211_IBSS_MLME_SEARCH:
1635 		ieee80211_sta_find_ibss(sdata);
1636 		break;
1637 	case IEEE80211_IBSS_MLME_JOINED:
1638 		ieee80211_sta_merge_ibss(sdata);
1639 		break;
1640 	default:
1641 		WARN_ON(1);
1642 		break;
1643 	}
1644 }
1645 
1646 static void ieee80211_ibss_timer(struct timer_list *t)
1647 {
1648 	struct ieee80211_sub_if_data *sdata =
1649 		timer_container_of(sdata, t, u.ibss.timer);
1650 
1651 	wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
1652 }
1653 
1654 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1655 {
1656 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1657 
1658 	timer_setup(&ifibss->timer, ieee80211_ibss_timer, 0);
1659 	INIT_LIST_HEAD(&ifibss->incomplete_stations);
1660 	spin_lock_init(&ifibss->incomplete_lock);
1661 	wiphy_work_init(&ifibss->csa_connection_drop_work,
1662 			ieee80211_csa_connection_drop_work);
1663 }
1664 
1665 /* scan finished notification */
1666 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1667 {
1668 	struct ieee80211_sub_if_data *sdata;
1669 
1670 	lockdep_assert_wiphy(local->hw.wiphy);
1671 
1672 	list_for_each_entry(sdata, &local->interfaces, list) {
1673 		if (!ieee80211_sdata_running(sdata))
1674 			continue;
1675 		if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1676 			continue;
1677 		sdata->u.ibss.last_scan_completed = jiffies;
1678 	}
1679 }
1680 
1681 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1682 			struct cfg80211_ibss_params *params)
1683 {
1684 	u64 changed = 0;
1685 	enum ieee80211_chanctx_mode chanmode;
1686 	struct ieee80211_local *local = sdata->local;
1687 	int radar_detect_width = 0;
1688 	int ret;
1689 
1690 	lockdep_assert_wiphy(local->hw.wiphy);
1691 
1692 	if (params->chandef.chan->freq_offset) {
1693 		/* this may work, but is untested */
1694 		return -EOPNOTSUPP;
1695 	}
1696 
1697 	ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1698 					    &params->chandef,
1699 					    sdata->wdev.iftype);
1700 	if (ret < 0)
1701 		return ret;
1702 
1703 	if (ret > 0) {
1704 		if (!params->userspace_handles_dfs)
1705 			return -EINVAL;
1706 		radar_detect_width = BIT(params->chandef.width);
1707 	}
1708 
1709 	chanmode = (params->channel_fixed && !ret) ?
1710 		IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
1711 
1712 	ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
1713 					   radar_detect_width, -1);
1714 	if (ret < 0)
1715 		return ret;
1716 
1717 	if (params->bssid) {
1718 		memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1719 		sdata->u.ibss.fixed_bssid = true;
1720 	} else
1721 		sdata->u.ibss.fixed_bssid = false;
1722 
1723 	sdata->u.ibss.privacy = params->privacy;
1724 	sdata->u.ibss.control_port = params->control_port;
1725 	sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
1726 	sdata->u.ibss.basic_rates = params->basic_rates;
1727 	sdata->u.ibss.last_scan_completed = jiffies;
1728 
1729 	/* fix basic_rates if channel does not support these rates */
1730 	memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1731 	       sizeof(params->mcast_rate));
1732 
1733 	sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1734 
1735 	sdata->u.ibss.chandef = params->chandef;
1736 	sdata->u.ibss.fixed_channel = params->channel_fixed;
1737 
1738 	if (params->ie) {
1739 		sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1740 					   GFP_KERNEL);
1741 		if (sdata->u.ibss.ie)
1742 			sdata->u.ibss.ie_len = params->ie_len;
1743 	}
1744 
1745 	sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1746 	sdata->u.ibss.ibss_join_req = jiffies;
1747 
1748 	memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
1749 	sdata->u.ibss.ssid_len = params->ssid_len;
1750 
1751 	memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1752 	       sizeof(sdata->u.ibss.ht_capa));
1753 	memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1754 	       sizeof(sdata->u.ibss.ht_capa_mask));
1755 
1756 	/*
1757 	 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1758 	 * reserved, but an HT STA shall protect HT transmissions as though
1759 	 * the HT Protection field were set to non-HT mixed mode.
1760 	 *
1761 	 * In an IBSS, the RIFS Mode field of the HT Operation element is
1762 	 * also reserved, but an HT STA shall operate as though this field
1763 	 * were set to 1.
1764 	 */
1765 
1766 	sdata->vif.bss_conf.ht_operation_mode |=
1767 		  IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1768 		| IEEE80211_HT_PARAM_RIFS_MODE;
1769 
1770 	changed |= BSS_CHANGED_HT | BSS_CHANGED_MCAST_RATE;
1771 	ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1772 
1773 	sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
1774 	sdata->deflink.needed_rx_chains = local->rx_chains;
1775 	sdata->control_port_over_nl80211 = params->control_port_over_nl80211;
1776 
1777 	wiphy_work_queue(local->hw.wiphy, &sdata->work);
1778 
1779 	return 0;
1780 }
1781 
1782 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1783 {
1784 	struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1785 
1786 	ifibss->ssid_len = 0;
1787 	ieee80211_ibss_disconnect(sdata);
1788 	eth_zero_addr(ifibss->bssid);
1789 
1790 	/* remove beacon */
1791 	kfree(sdata->u.ibss.ie);
1792 	sdata->u.ibss.ie = NULL;
1793 	sdata->u.ibss.ie_len = 0;
1794 
1795 	/* on the next join, re-program HT parameters */
1796 	memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1797 	memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1798 
1799 	skb_queue_purge(&sdata->skb_queue);
1800 
1801 	timer_delete_sync(&sdata->u.ibss.timer);
1802 
1803 	return 0;
1804 }
1805