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