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