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