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