Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / net / mac80211 / ibss.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
46900298
JB
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>
d98ad83e 10 * Copyright 2013-2014 Intel Mobile Communications GmbH
d321cd01 11 * Copyright(c) 2016 Intel Deutschland GmbH
344d18ce 12 * Copyright(c) 2018-2024 Intel Corporation
46900298
JB
13 */
14
15#include <linux/delay.h>
5a0e3ad6 16#include <linux/slab.h>
46900298
JB
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>
46900298
JB
23
24#include "ieee80211_i.h"
24487981 25#include "driver-ops.h"
46900298
JB
26#include "rate.h"
27
28#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46900298
JB
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)
dad9defd 33#define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
46900298
JB
34
35#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
36
d51b70ff
SW
37static struct beacon_data *
38ieee80211_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,
cd7760e6
SW
42 bool *have_higher_than_11mbit,
43 struct cfg80211_csa_settings *csa_settings)
46900298
JB
44{
45 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46 struct ieee80211_local *local = sdata->local;
2103dec1 47 int rates_n = 0, i, ri;
46900298
JB
48 struct ieee80211_mgmt *mgmt;
49 u8 *pos;
50 struct ieee80211_supported_band *sband;
996c15bd 51 u32 rates = 0, rates_added = 0;
c3ffeab4
JB
52 struct beacon_data *presp;
53 int frame_len;
24487981 54
46900298 55 /* Build IBSS probe response */
c3ffeab4
JB
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 */ +
cd7760e6 62 5 /* Channel Switch Announcement */ +
c3ffeab4
JB
63 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
64 2 + sizeof(struct ieee80211_ht_cap) +
65 2 + sizeof(struct ieee80211_ht_operation) +
f1f3e9e2
JB
66 2 + sizeof(struct ieee80211_vht_cap) +
67 2 + sizeof(struct ieee80211_vht_operation) +
c3ffeab4
JB
68 ifibss->ie_len;
69 presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
70 if (!presp)
d51b70ff 71 return NULL;
c3ffeab4
JB
72
73 presp->head = (void *)(presp + 1);
74
75 mgmt = (void *) presp->head;
46900298
JB
76 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
77 IEEE80211_STYPE_PROBE_RESP);
e83e6541 78 eth_broadcast_addr(mgmt->da);
47846c9b 79 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298 80 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
57c4d7b4 81 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
707c1b4e 82 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
46900298
JB
83 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
84
c3ffeab4
JB
85 pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
86
46900298
JB
87 *pos++ = WLAN_EID_SSID;
88 *pos++ = ifibss->ssid_len;
89 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
c3ffeab4 90 pos += ifibss->ssid_len;
46900298 91
d51b70ff 92 sband = local->hw.wiphy->bands[chandef->chan->band];
d51b70ff
SW
93 rates_n = 0;
94 if (have_higher_than_11mbit)
95 *have_higher_than_11mbit = false;
96
2103dec1 97 for (i = 0; i < sband->n_bitrates; i++) {
d51b70ff
SW
98 if (sband->bitrates[i].bitrate > 110 &&
99 have_higher_than_11mbit)
100 *have_higher_than_11mbit = true;
2103dec1
SW
101
102 rates |= BIT(i);
103 rates_n++;
104 }
105
46900298 106 *pos++ = WLAN_EID_SUPP_RATES;
2103dec1
SW
107 *pos++ = min_t(int, 8, rates_n);
108 for (ri = 0; ri < sband->n_bitrates; ri++) {
2400dfe2 109 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, 5);
c3ffeab4 110 u8 basic = 0;
2103dec1
SW
111 if (!(rates & BIT(ri)))
112 continue;
113
114 if (basic_rates & BIT(ri))
c3ffeab4 115 basic = 0x80;
2103dec1 116 *pos++ = basic | (u8) rate;
93c75786
SW
117 if (++rates_added == 8) {
118 ri++; /* continue at next rate for EXT_SUPP_RATES */
2103dec1 119 break;
93c75786 120 }
c3ffeab4 121 }
46900298 122
57fbcce3 123 if (sband->band == NL80211_BAND_2GHZ) {
46900298
JB
124 *pos++ = WLAN_EID_DS_PARAMS;
125 *pos++ = 1;
d51b70ff
SW
126 *pos++ = ieee80211_frequency_to_channel(
127 chandef->chan->center_freq);
46900298
JB
128 }
129
46900298
JB
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
cd7760e6
SW
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);
8552a434 142 presp->cntdwn_counter_offsets[0] = (pos - presp->head);
cd7760e6 143 *pos++ = csa_settings->count;
8552a434 144 presp->cntdwn_current_counter = csa_settings->count;
cd7760e6
SW
145 }
146
2103dec1
SW
147 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
148 if (rates_n > 8) {
46900298 149 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2103dec1
SW
150 *pos++ = rates_n - 8;
151 for (; ri < sband->n_bitrates; ri++) {
2400dfe2 152 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, 5);
c3ffeab4 153 u8 basic = 0;
2103dec1
SW
154 if (!(rates & BIT(ri)))
155 continue;
156
157 if (basic_rates & BIT(ri))
c3ffeab4 158 basic = 0x80;
2103dec1 159 *pos++ = basic | (u8) rate;
c3ffeab4 160 }
46900298
JB
161 }
162
c3ffeab4
JB
163 if (ifibss->ie_len) {
164 memcpy(pos, ifibss->ie, ifibss->ie_len);
165 pos += ifibss->ie_len;
166 }
af8cdcd8 167
13c40c54 168 /* add HT capability and information IEs */
d51b70ff
SW
169 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
170 chandef->width != NL80211_CHAN_WIDTH_5 &&
171 chandef->width != NL80211_CHAN_WIDTH_10 &&
683b6d3b 172 sband->ht_cap.ht_supported) {
822854b0
SW
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);
0d894ec5
AN
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 */
074d46d1 184 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
57f255f5 185 chandef, 0, false);
abcff6ef
JD
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 }
13c40c54
AS
196 }
197
40b861a0
AN
198 if (local->hw.queues >= IEEE80211_NUM_ACS)
199 pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
9eba6125 200
c3ffeab4
JB
201 presp->head_len = pos - presp->head;
202 if (WARN_ON(presp->head_len > frame_len))
d51b70ff
SW
203 goto error;
204
205 return presp;
206error:
207 kfree(presp);
208 return NULL;
209}
210
211static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
212 const u8 *bssid, const int beacon_int,
b35c8097 213 struct cfg80211_chan_def *req_chandef,
d51b70ff
SW
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;
d51b70ff
SW
220 struct ieee80211_mgmt *mgmt;
221 struct cfg80211_bss *bss;
15ddba5f 222 u64 bss_change;
6092077a 223 struct ieee80211_chan_req chanreq = {};
b35c8097 224 struct ieee80211_channel *chan;
d51b70ff 225 struct beacon_data *presp;
61f6bba0 226 struct cfg80211_inform_bss bss_meta = {};
d51b70ff 227 bool have_higher_than_11mbit;
2beb6dab 228 bool radar_required;
55fff501 229 int err;
d51b70ff 230
0cd8080e 231 lockdep_assert_wiphy(local->hw.wiphy);
d51b70ff
SW
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))
ec67d6e0 237 sta_info_flush(sdata, -1);
d51b70ff
SW
238
239 /* if merging, indicate to driver that we leave the old IBSS */
f276e20b
JB
240 if (sdata->vif.cfg.ibss_joined) {
241 sdata->vif.cfg.ibss_joined = false;
242 sdata->vif.cfg.ibss_creator = false;
d51b70ff
SW
243 sdata->vif.bss_conf.enable_beacon = false;
244 netif_carrier_off(sdata->dev);
3bf18e99 245 synchronize_net();
d51b70ff
SW
246 ieee80211_bss_info_change_notify(sdata,
247 BSS_CHANGED_IBSS |
248 BSS_CHANGED_BEACON_ENABLED);
55fff501 249 drv_leave_ibss(local, sdata);
d51b70ff
SW
250 }
251
6858ad75 252 presp = sdata_dereference(ifibss->presp, sdata);
0c2bef46 253 RCU_INIT_POINTER(ifibss->presp, NULL);
d51b70ff
SW
254 if (presp)
255 kfree_rcu(presp, rcu_head);
256
b35c8097 257 /* make a copy of the chandef, it could be modified below. */
6092077a
JB
258 chanreq.oper = *req_chandef;
259 chan = chanreq.oper.chan;
260 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chanreq.oper,
174e0cd2 261 NL80211_IFTYPE_ADHOC)) {
6092077a
JB
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) {
d51b70ff
SW
266 sdata_info(sdata,
267 "Failed to join IBSS, beacons forbidden\n");
268 return;
269 }
6092077a
JB
270 chanreq.oper.width = NL80211_CHAN_WIDTH_20;
271 chanreq.oper.center_freq1 = chan->center_freq;
8e8d347d 272 /* check again for downgraded chandef */
6092077a 273 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chanreq.oper,
174e0cd2 274 NL80211_IFTYPE_ADHOC)) {
8e8d347d
SW
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,
6092077a 282 &chanreq.oper, NL80211_IFTYPE_ADHOC);
6658ab80
LC
283 if (err < 0) {
284 sdata_info(sdata,
285 "Failed to join IBSS, invalid chandef\n");
286 return;
287 }
2beb6dab
LC
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;
d51b70ff
SW
292 }
293
2beb6dab
LC
294 radar_required = err;
295
6092077a 296 if (ieee80211_link_use_channel(&sdata->deflink, &chanreq,
b4f85443 297 ifibss->fixed_channel ?
d51b70ff
SW
298 IEEE80211_CHANCTX_SHARED :
299 IEEE80211_CHANCTX_EXCLUSIVE)) {
300 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
301 return;
302 }
bfd8403a 303 sdata->deflink.radar_required = radar_required;
d51b70ff
SW
304
305 memcpy(ifibss->bssid, bssid, ETH_ALEN);
306
d51b70ff 307 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
6092077a 308 capability, tsf, &chanreq.oper,
cd7760e6 309 &have_higher_than_11mbit, NULL);
d51b70ff 310 if (!presp)
c3ffeab4
JB
311 return;
312
313 rcu_assign_pointer(ifibss->presp, presp);
d51b70ff 314 mgmt = (void *)presp->head;
46900298 315
d6a83228 316 sdata->vif.bss_conf.enable_beacon = true;
2d0ddec5 317 sdata->vif.bss_conf.beacon_int = beacon_int;
fbd2c8dc 318 sdata->vif.bss_conf.basic_rates = basic_rates;
f276e20b
JB
319 sdata->vif.cfg.ssid_len = ifibss->ssid_len;
320 memcpy(sdata->vif.cfg.ssid, ifibss->ssid, ifibss->ssid_len);
2d0ddec5
JB
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;
392cfdb1 326 bss_change |= BSS_CHANGED_BASIC_RATES;
13c40c54 327 bss_change |= BSS_CHANGED_HT;
8fc214ba 328 bss_change |= BSS_CHANGED_IBSS;
0ca54f6c 329 bss_change |= BSS_CHANGED_SSID;
2f91a967
SW
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 */
57fbcce3 341 sdata->vif.bss_conf.use_short_slot = chan->band == NL80211_BAND_5GHZ;
2f91a967
SW
342 bss_change |= BSS_CHANGED_ERP_SLOT;
343
2ec9c1f6 344 /* cf. IEEE 802.11 9.2.12 */
39eac2de
JB
345 sdata->deflink.operating_11g_mode =
346 chan->band == NL80211_BAND_2GHZ && have_higher_than_11mbit;
2ec9c1f6 347
b3e2130b 348 ieee80211_set_wmm_default(&sdata->deflink, true, false);
55fff501 349
f276e20b
JB
350 sdata->vif.cfg.ibss_joined = true;
351 sdata->vif.cfg.ibss_creator = creator;
46900298 352
55fff501
JB
353 err = drv_join_ibss(local, sdata);
354 if (err) {
f276e20b
JB
355 sdata->vif.cfg.ibss_joined = false;
356 sdata->vif.cfg.ibss_creator = false;
55fff501 357 sdata->vif.bss_conf.enable_beacon = false;
f276e20b 358 sdata->vif.cfg.ssid_len = 0;
55fff501
JB
359 RCU_INIT_POINTER(ifibss->presp, NULL);
360 kfree_rcu(presp, rcu_head);
d8675a63 361 ieee80211_link_release_channel(&sdata->deflink);
55fff501
JB
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);
46900298 368
46900298 369 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
af8cdcd8
JB
370 mod_timer(&ifibss->timer,
371 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 372
61f6bba0 373 bss_meta.chan = chan;
61f6bba0
JB
374 bss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, mgmt,
375 presp->head_len, GFP_KERNEL);
376
5b112d3d 377 cfg80211_put_bss(local->hw.wiphy, bss);
86a2ea41 378 netif_carrier_on(sdata->dev);
fe94f3a4 379 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
46900298
JB
380}
381
af8cdcd8
JB
382static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
383 struct ieee80211_bss *bss)
46900298 384{
0c1ad2ca
JB
385 struct cfg80211_bss *cbss =
386 container_of((void *)bss, struct cfg80211_bss, priv);
b59066a2 387 struct ieee80211_supported_band *sband;
75a423f4 388 struct cfg80211_chan_def chandef;
b59066a2
JB
389 u32 basic_rates;
390 int i, j;
0c1ad2ca 391 u16 beacon_int = cbss->beacon_interval;
8cef2c9d 392 const struct cfg80211_bss_ies *ies;
75a423f4 393 enum nl80211_channel_type chan_type;
8cef2c9d 394 u64 tsf;
57c4d7b4 395
076fc877 396 lockdep_assert_wiphy(sdata->local->hw.wiphy);
7a17a33c 397
57c4d7b4
JB
398 if (beacon_int < 10)
399 beacon_int = 10;
400
75a423f4
SW
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,
a4ac6f2e 411 NL80211_CHAN_NO_HT);
75a423f4
SW
412 chandef.width = sdata->u.ibss.chandef.width;
413 break;
abcff6ef 414 case NL80211_CHAN_WIDTH_80:
c39b336d 415 case NL80211_CHAN_WIDTH_80P80:
abcff6ef
JD
416 case NL80211_CHAN_WIDTH_160:
417 chandef = sdata->u.ibss.chandef;
418 chandef.chan = cbss->channel;
419 break;
75a423f4
SW
420 default:
421 /* fall back to 20 MHz for unsupported modes */
422 cfg80211_chandef_create(&chandef, cbss->channel,
a4ac6f2e 423 NL80211_CHAN_NO_HT);
75a423f4
SW
424 break;
425 }
426
0c1ad2ca 427 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
b59066a2
JB
428
429 basic_rates = 0;
430
431 for (i = 0; i < bss->supp_rates_len; i++) {
2103dec1 432 int rate = bss->supp_rates[i] & 0x7f;
b59066a2
JB
433 bool is_basic = !!(bss->supp_rates[i] & 0x80);
434
435 for (j = 0; j < sband->n_bitrates; j++) {
2103dec1 436 int brate;
2103dec1 437
2400dfe2 438 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate, 5);
2103dec1 439 if (brate == rate) {
b59066a2
JB
440 if (is_basic)
441 basic_rates |= BIT(j);
442 break;
443 }
444 }
445 }
446
8cef2c9d
JB
447 rcu_read_lock();
448 ies = rcu_dereference(cbss->ies);
449 tsf = ies->tsf;
450 rcu_read_unlock();
451
0c1ad2ca 452 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
57c4d7b4 453 beacon_int,
75a423f4 454 &chandef,
b59066a2 455 basic_rates,
0c1ad2ca 456 cbss->capability,
8cef2c9d 457 tsf, false);
46900298
JB
458}
459
cd7760e6 460int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
15ddba5f
A
461 struct cfg80211_csa_settings *csa_settings,
462 u64 *changed)
cd7760e6
SW
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;
f181d6a3 468 u16 capability = WLAN_CAPABILITY_IBSS;
cd7760e6 469 u64 tsf;
cd7760e6 470
076fc877 471 lockdep_assert_wiphy(sdata->local->hw.wiphy);
cd7760e6 472
cd7760e6 473 if (ifibss->privacy)
f181d6a3 474 capability |= WLAN_CAPABILITY_PRIVACY;
cd7760e6
SW
475
476 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
477 ifibss->bssid, ifibss->ssid,
6eb18137
DL
478 ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
479 IEEE80211_PRIVACY(ifibss->privacy));
cd7760e6 480
68d83f0a 481 if (unlikely(!cbss))
0323689d 482 return -EINVAL;
cd7760e6
SW
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
6858ad75 490 old_presp = sdata_dereference(ifibss->presp, sdata);
cd7760e6
SW
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);
0323689d 497 if (!presp)
498 return -ENOMEM;
cd7760e6
SW
499
500 rcu_assign_pointer(ifibss->presp, presp);
501 if (old_presp)
502 kfree_rcu(old_presp, rcu_head);
503
15ddba5f
A
504 *changed |= BSS_CHANGED_BEACON;
505 return 0;
cd7760e6
SW
506}
507
15ddba5f 508int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata, u64 *changed)
cd7760e6
SW
509{
510 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
511 struct cfg80211_bss *cbss;
cd7760e6 512
076fc877 513 lockdep_assert_wiphy(sdata->local->hw.wiphy);
bafdc614 514
15bc8966
SRP
515 /* When not connected/joined, sending CSA doesn't make sense. */
516 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED)
517 return -ENOLINK;
518
cd7760e6
SW
519 /* update cfg80211 bss information with the new channel */
520 if (!is_zero_ether_addr(ifibss->bssid)) {
cd7760e6
SW
521 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
522 ifibss->chandef.chan,
523 ifibss->bssid, ifibss->ssid,
6eb18137
DL
524 ifibss->ssid_len,
525 IEEE80211_BSS_TYPE_IBSS,
526 IEEE80211_PRIVACY(ifibss->privacy));
cd7760e6
SW
527 /* XXX: should not really modify cfg80211 data */
528 if (cbss) {
344d18ce 529 cbss->channel = sdata->deflink.csa.chanreq.oper.chan;
cd7760e6
SW
530 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
531 }
532 }
533
344d18ce 534 ifibss->chandef = sdata->deflink.csa.chanreq.oper;
cd7760e6
SW
535
536 /* generate the beacon */
15ddba5f 537 return ieee80211_ibss_csa_beacon(sdata, NULL, changed);
cd7760e6
SW
538}
539
540void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
541{
542 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
543
87351d09
JB
544 wiphy_work_cancel(sdata->local->hw.wiphy,
545 &ifibss->csa_connection_drop_work);
cd7760e6
SW
546}
547
52874a5e 548static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
8bf11d8d
JB
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
bdcbd8e0 556 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
8bf11d8d 557
83d5cc01
JB
558 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
559 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
267335d6
AQ
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);
8bf11d8d 564
f828deb7 565 rate_control_rate_init(&sta->deflink);
8bf11d8d
JB
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
573static struct sta_info *
52874a5e
AQ
574ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
575 const u8 *addr, u32 supp_rates)
8bf11d8d
JB
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;
55de908a 581 struct ieee80211_chanctx_conf *chanctx_conf;
b422c6cd 582 struct ieee80211_supported_band *sband;
55de908a 583 int band;
8bf11d8d
JB
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) {
bdcbd8e0 590 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
e87cc472 591 sdata->name, addr);
8bf11d8d
JB
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
b203ca39 601 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
8bf11d8d
JB
602 rcu_read_lock();
603 return NULL;
604 }
605
55de908a 606 rcu_read_lock();
d0a9123e 607 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
55de908a
JB
608 if (WARN_ON_ONCE(!chanctx_conf))
609 return NULL;
4bf88530 610 band = chanctx_conf->def.chan->band;
55de908a
JB
611 rcu_read_unlock();
612
f36fe0a2 613 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
8bf11d8d
JB
614 if (!sta) {
615 rcu_read_lock();
616 return NULL;
617 }
618
8bf11d8d 619 /* make sure mandatory rates are always added */
b422c6cd 620 sband = local->hw.wiphy->bands[band];
046d2e7c 621 sta->sta.deflink.supp_rates[band] = supp_rates |
5add321c 622 ieee80211_mandatory_rates(sband);
8bf11d8d 623
52874a5e 624 return ieee80211_ibss_finish_sta(sta);
6d810f10
AQ
625}
626
871a4180
SW
627static 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
076fc877 633 lockdep_assert_wiphy(sdata->local->hw.wiphy);
871a4180
SW
634
635 rcu_read_lock();
636
637 list_for_each_entry_rcu(sta, &local->sta_list, list) {
b8da6b6a
JB
638 unsigned long last_active = ieee80211_sta_last_active(sta);
639
871a4180 640 if (sta->sdata == sdata &&
b8da6b6a
JB
641 time_is_after_jiffies(last_active +
642 IEEE80211_IBSS_MERGE_INTERVAL)) {
871a4180
SW
643 active++;
644 break;
645 }
646 }
647
648 rcu_read_unlock();
649
650 return active;
651}
652
653static 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;
871a4180 660
0cd8080e
JB
661 lockdep_assert_wiphy(local->hw.wiphy);
662
d4c80d9d 663 if (!is_zero_ether_addr(ifibss->bssid)) {
871a4180
SW
664 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
665 ifibss->bssid, ifibss->ssid,
6eb18137
DL
666 ifibss->ssid_len,
667 IEEE80211_BSS_TYPE_IBSS,
668 IEEE80211_PRIVACY(ifibss->privacy));
871a4180
SW
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
ec67d6e0 678 sta_info_flush(sdata, -1);
871a4180
SW
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
f276e20b
JB
694 sdata->vif.cfg.ibss_joined = false;
695 sdata->vif.cfg.ibss_creator = false;
871a4180 696 sdata->vif.bss_conf.enable_beacon = false;
f276e20b 697 sdata->vif.cfg.ssid_len = 0;
871a4180
SW
698
699 /* remove beacon */
6858ad75 700 presp = sdata_dereference(ifibss->presp, sdata);
871a4180
SW
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);
55fff501 708 drv_leave_ibss(local, sdata);
d8675a63 709 ieee80211_link_release_channel(&sdata->deflink);
871a4180
SW
710}
711
87351d09
JB
712static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
713 struct wiphy_work *work)
cd7760e6
SW
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 */
16114496 724 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
cd7760e6
SW
725}
726
8e8d347d
SW
727static 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,
2beb6dab
LC
736 &ifibss->chandef,
737 NL80211_IFTYPE_ADHOC);
8e8d347d
SW
738 if (err > 0)
739 cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
740 GFP_ATOMIC);
741}
742
cd7760e6
SW
743static bool
744ieee80211_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;
c0f17eb9 749 struct ieee80211_csa_ie csa_ie;
cd7760e6 750 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
cd7760e6 751 enum nl80211_channel_type ch_type;
82a8e17d 752 int err;
310c8387
JB
753 struct ieee80211_conn_settings conn = {
754 .mode = IEEE80211_CONN_MODE_HT,
755 .bw_limit = IEEE80211_CONN_BW_LIMIT_40,
756 };
2a333a0d 757 u32 vht_cap_info = 0;
cd7760e6 758
076fc877 759 lockdep_assert_wiphy(sdata->local->hw.wiphy);
dbd72850 760
cd7760e6
SW
761 switch (ifibss->chandef.width) {
762 case NL80211_CHAN_WIDTH_5:
763 case NL80211_CHAN_WIDTH_10:
764 case NL80211_CHAN_WIDTH_20_NOHT:
310c8387 765 conn.mode = IEEE80211_CONN_MODE_LEGACY;
fc0561dc 766 fallthrough;
cd7760e6 767 case NL80211_CHAN_WIDTH_20:
310c8387 768 conn.bw_limit = IEEE80211_CONN_BW_LIMIT_20;
cd7760e6
SW
769 break;
770 default:
771 break;
772 }
773
2a333a0d
JB
774 if (elems->vht_cap_elem)
775 vht_cap_info = le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
776
cd7760e6 777 memset(&params, 0, sizeof(params));
84469a45 778 err = ieee80211_parse_ch_switch_ie(sdata, elems,
cd7760e6 779 ifibss->chandef.chan->band,
310c8387 780 vht_cap_info, &conn,
414e090b
JB
781 ifibss->bssid, false,
782 &csa_ie);
cd7760e6
SW
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
0834ae3c
SW
791 /* channel switch is not supported, disconnect */
792 if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
793 goto disconnect;
794
c0f17eb9 795 params.count = csa_ie.count;
6092077a 796 params.chandef = csa_ie.chanreq.oper;
c0f17eb9 797
cd7760e6
SW
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:
ba323e29 825 /* should not happen, conn_flags should prevent VHT modes. */
cd7760e6
SW
826 WARN_ON(1);
827 goto disconnect;
828 }
829
174e0cd2
IP
830 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
831 NL80211_IFTYPE_ADHOC)) {
cd7760e6
SW
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,
2beb6dab
LC
843 &params.chandef,
844 NL80211_IFTYPE_ADHOC);
cd7760e6
SW
845 if (err < 0)
846 goto disconnect;
2beb6dab 847 if (err > 0 && !ifibss->userspace_handles_dfs) {
8e8d347d 848 /* IBSS-DFS only allowed with a control program */
2beb6dab 849 goto disconnect;
cd7760e6
SW
850 }
851
2beb6dab
LC
852 params.radar_required = err;
853
82a8e17d 854 if (cfg80211_chandef_identical(&params.chandef,
6092077a 855 &sdata->vif.bss_conf.chanreq.oper)) {
82a8e17d
LC
856 ibss_dbg(sdata,
857 "received csa with an identical chandef, ignoring\n");
858 return true;
cd7760e6 859 }
cd7760e6
SW
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
c0f17eb9 866 params.block_tx = !!csa_ie.mode;
cd7760e6 867
82a8e17d
LC
868 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
869 &params))
870 goto disconnect;
cd7760e6 871
8e8d347d
SW
872 ieee80211_ibss_csa_mark_radar(sdata);
873
cd7760e6
SW
874 return true;
875disconnect:
876 ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
87351d09
JB
877 wiphy_work_queue(sdata->local->hw.wiphy,
878 &ifibss->csa_connection_drop_work);
cd7760e6 879
8e8d347d
SW
880 ieee80211_ibss_csa_mark_radar(sdata);
881
cd7760e6
SW
882 return true;
883}
884
885static void
886ieee80211_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
d0a9123e 906 if (!sdata->vif.bss_conf.csa_active)
82a8e17d 907 ieee80211_ibss_process_chanswitch(sdata, elems, false);
cd7760e6
SW
908}
909
2cc59e78
AQ
910static 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
c6e57b38
EG
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);
2cc59e78
AQ
921 sta_info_destroy_addr(sdata, mgmt->sa);
922}
923
6d810f10
AQ
924static 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
076fc877 930 lockdep_assert_wiphy(sdata->local->hw.wiphy);
6d810f10
AQ
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
c6e57b38
EG
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);
7bed2050
AQ
941
942 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
943 return;
944
6d810f10
AQ
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 */
700e8ea6 951 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
1672c0e3 952 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
8bf11d8d
JB
953}
954
8a4988d1
JD
955static 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)
46900298 960{
46900298 961 struct sta_info *sta;
57fbcce3 962 enum nl80211_band band = rx_status->band;
8a4988d1 963 struct ieee80211_local *local = sdata->local;
21a8e9dd 964 struct ieee80211_supported_band *sband;
13c40c54 965 bool rates_updated = false;
8a4988d1 966 u32 supp_rates = 0;
46900298 967
8a4988d1 968 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
46900298
JB
969 return;
970
8a4988d1
JD
971 if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
972 return;
46900298 973
21a8e9dd
MSS
974 sband = local->hw.wiphy->bands[band];
975 if (WARN_ON(!sband))
976 return;
977
8a4988d1
JD
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
046d2e7c 987 prev_rates = sta->sta.deflink.supp_rates[band];
8a4988d1 988
046d2e7c 989 sta->sta.deflink.supp_rates[band] = supp_rates |
5add321c 990 ieee80211_mandatory_rates(sband);
046d2e7c 991 if (sta->sta.deflink.supp_rates[band] != prev_rates) {
8a4988d1
JD
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,
046d2e7c 995 sta->sta.deflink.supp_rates[band]);
8a4988d1 996 rates_updated = true;
8bf11d8d 997 }
8a4988d1
JD
998 } else {
999 rcu_read_unlock();
1000 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1001 mgmt->sa, supp_rates);
34e89507 1002 }
8a4988d1 1003 }
9eba6125 1004
96a4688e 1005 if (sta && !sta->sta.wme &&
1d00ce80
TP
1006 (elems->wmm_info || elems->s1g_capab) &&
1007 local->hw.queues >= IEEE80211_NUM_ACS) {
8a4988d1 1008 sta->sta.wme = true;
96a4688e
JB
1009 ieee80211_check_fast_xmit(sta);
1010 }
13c40c54 1011
8a4988d1
JD
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;
046d2e7c 1019 enum ieee80211_sta_rx_bandwidth bw = sta->sta.deflink.bandwidth;
d2954457 1020
8ac3c704
JB
1021 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1022 ieee80211_chandef_ht_oper(elems->ht_operation, &chandef);
d2954457 1023
8a4988d1 1024 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
8a4988d1
JD
1025 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1026 &htcap_ie,
c71420db 1027 &sta->deflink);
abcff6ef
JD
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;
046d2e7c 1034 struct ieee80211_sta_vht_cap cap = sta->sta.deflink.vht_cap;
2a333a0d
JB
1035 u32 vht_cap_info =
1036 le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
abcff6ef 1037
2a333a0d 1038 ieee80211_chandef_vht_oper(&local->hw, vht_cap_info,
7eb26df2
JB
1039 elems->vht_operation,
1040 elems->ht_operation,
8ac3c704 1041 &chandef);
abcff6ef
JD
1042 memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1043 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
084cf2ae 1044 &cap_ie, NULL,
c71420db 1045 &sta->deflink);
046d2e7c 1046 if (memcmp(&cap, &sta->sta.deflink.vht_cap, sizeof(cap)))
abcff6ef 1047 rates_updated |= true;
13c40c54
AS
1048 }
1049
046d2e7c 1050 if (bw != sta->sta.deflink.bandwidth)
abcff6ef 1051 rates_updated |= true;
d2954457 1052
abcff6ef
JD
1053 if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1054 &chandef))
1055 WARN_ON_ONCE(1);
8a4988d1 1056 }
d2954457 1057
8a4988d1
JD
1058 if (sta && rates_updated) {
1059 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
046d2e7c 1060 u8 rx_nss = sta->sta.deflink.rx_nss;
13c40c54 1061
8a4988d1 1062 /* Force rx_nss recalculation */
046d2e7c 1063 sta->sta.deflink.rx_nss = 0;
f828deb7 1064 rate_control_rate_init(&sta->deflink);
046d2e7c 1065 if (sta->sta.deflink.rx_nss != rx_nss)
8a4988d1 1066 changed |= IEEE80211_RC_NSS_CHANGED;
13c40c54 1067
88b67e91
JB
1068 drv_link_sta_rc_update(local, sdata, &sta->sta.deflink,
1069 changed);
46900298
JB
1070 }
1071
8a4988d1
JD
1072 rcu_read_unlock();
1073}
1074
1075static 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;
57fbcce3 1086 enum nl80211_band band = rx_status->band;
8a4988d1
JD
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
4abb52a4 1094 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
46900298
JB
1095 if (!bss)
1096 return;
1097
0c1ad2ca
JB
1098 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1099
8cef2c9d
JB
1100 /* same for beacon and probe response */
1101 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
46900298
JB
1102
1103 /* check if we need to merge IBSS */
1104
46900298 1105 /* not an IBSS */
0c1ad2ca 1106 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
46900298
JB
1107 goto put_bss;
1108
1109 /* different channel */
55de908a 1110 if (sdata->u.ibss.fixed_channel &&
3aede78a 1111 sdata->u.ibss.chandef.chan != cbss->channel)
46900298
JB
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
cd7760e6 1120 /* process channel switch */
d0a9123e 1121 if (sdata->vif.bss_conf.csa_active ||
82a8e17d 1122 ieee80211_ibss_process_chanswitch(sdata, elems, true))
cd7760e6
SW
1123 goto put_bss;
1124
34e8f082 1125 /* same BSSID */
b203ca39 1126 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
34e8f082
AF
1127 goto put_bss;
1128
cd7760e6
SW
1129 /* we use a fixed BSSID */
1130 if (sdata->u.ibss.fixed_bssid)
1131 goto put_bss;
1132
f4bda337
TP
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);
24487981
JB
1138 } else {
1139 /*
1140 * second best option: get current TSF
1141 * (will return -1 if not supported)
1142 */
37a41b4a 1143 rx_timestamp = drv_get_tsf(local, sdata);
24487981 1144 }
46900298 1145
c6e57b38 1146 ibss_dbg(sdata, "RX beacon SA=%pM BSSID=%pM TSF=0x%llx\n",
bdcbd8e0 1147 mgmt->sa, mgmt->bssid,
c6e57b38
EG
1148 (unsigned long long)rx_timestamp);
1149 ibss_dbg(sdata, "\tBCN=0x%llx diff=%lld @%lu\n",
bdcbd8e0
JB
1150 (unsigned long long)beacon_timestamp,
1151 (unsigned long long)(rx_timestamp - beacon_timestamp),
1152 jiffies);
46900298
JB
1153
1154 if (beacon_timestamp > rx_timestamp) {
bdcbd8e0
JB
1155 ibss_dbg(sdata,
1156 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1157 mgmt->bssid);
46900298 1158 ieee80211_sta_join_ibss(sdata, bss);
2103dec1 1159 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
34e89507 1160 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
52874a5e 1161 supp_rates);
8bf11d8d 1162 rcu_read_unlock();
46900298
JB
1163 }
1164
1165 put_bss:
1166 ieee80211_rx_bss_put(local, bss);
1167}
1168
8bf11d8d
JB
1169void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1170 const u8 *bssid, const u8 *addr,
1171 u32 supp_rates)
46900298 1172{
2e10d330 1173 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298
JB
1174 struct ieee80211_local *local = sdata->local;
1175 struct sta_info *sta;
55de908a 1176 struct ieee80211_chanctx_conf *chanctx_conf;
b422c6cd 1177 struct ieee80211_supported_band *sband;
55de908a 1178 int band;
46900298 1179
af8cdcd8
JB
1180 /*
1181 * XXX: Consider removing the least recently used entry and
1182 * allow new one to be added.
1183 */
46900298 1184 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
bdcbd8e0 1185 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
e87cc472 1186 sdata->name, addr);
8bf11d8d 1187 return;
46900298
JB
1188 }
1189
2e10d330 1190 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
8bf11d8d 1191 return;
2e10d330 1192
b203ca39 1193 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
8bf11d8d 1194 return;
46900298 1195
55de908a 1196 rcu_read_lock();
d0a9123e 1197 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
55de908a
JB
1198 if (WARN_ON_ONCE(!chanctx_conf)) {
1199 rcu_read_unlock();
1200 return;
1201 }
4bf88530 1202 band = chanctx_conf->def.chan->band;
55de908a
JB
1203 rcu_read_unlock();
1204
f36fe0a2 1205 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
46900298 1206 if (!sta)
8bf11d8d 1207 return;
46900298 1208
46900298 1209 /* make sure mandatory rates are always added */
b422c6cd 1210 sband = local->hw.wiphy->bands[band];
046d2e7c 1211 sta->sta.deflink.supp_rates[band] = supp_rates |
5add321c 1212 ieee80211_mandatory_rates(sband);
46900298 1213
8bf11d8d
JB
1214 spin_lock(&ifibss->incomplete_lock);
1215 list_add(&sta->list, &ifibss->incomplete_stations);
1216 spin_unlock(&ifibss->incomplete_lock);
16114496 1217 wiphy_work_queue(local->hw.wiphy, &sdata->work);
46900298
JB
1218}
1219
dad9defd
AQ
1220static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1221{
4b08d1b6 1222 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
dad9defd
AQ
1223 struct ieee80211_local *local = sdata->local;
1224 struct sta_info *sta, *tmp;
1225 unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
e5a9f8d0 1226 unsigned long exp_rsn = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
dad9defd 1227
4d3acf43 1228 lockdep_assert_wiphy(local->hw.wiphy);
dad9defd
AQ
1229
1230 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
b8da6b6a
JB
1231 unsigned long last_active = ieee80211_sta_last_active(sta);
1232
dad9defd
AQ
1233 if (sdata != sta->sdata)
1234 continue;
1235
b8da6b6a
JB
1236 if (time_is_before_jiffies(last_active + exp_time) ||
1237 (time_is_before_jiffies(last_active + exp_rsn) &&
dad9defd 1238 sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
4b08d1b6
JB
1239 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1240
dad9defd
AQ
1241 sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
1242 sta->sta_state != IEEE80211_STA_AUTHORIZED ?
1243 "not authorized " : "", sta->sta.addr);
1244
4b08d1b6
JB
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);
dad9defd
AQ
1250 WARN_ON(__sta_info_destroy(sta));
1251 }
1252 }
dad9defd
AQ
1253}
1254
ce9058ae
BP
1255/*
1256 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1257 */
46900298
JB
1258
1259static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1260{
1261 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1262
076fc877 1263 lockdep_assert_wiphy(sdata->local->hw.wiphy);
7a17a33c 1264
af8cdcd8
JB
1265 mod_timer(&ifibss->timer,
1266 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 1267
dad9defd 1268 ieee80211_ibss_sta_expire(sdata);
af8cdcd8 1269
450aae3d
S
1270 if (time_before(jiffies, ifibss->last_scan_completed +
1271 IEEE80211_IBSS_MERGE_INTERVAL))
1272 return;
1273
46900298
JB
1274 if (ieee80211_sta_active_ibss(sdata))
1275 return;
1276
c037b836 1277 if (ifibss->fixed_channel)
46900298
JB
1278 return;
1279
bdcbd8e0
JB
1280 sdata_info(sdata,
1281 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
46900298 1282
34bcf715 1283 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
5add321c 1284 NULL, 0);
46900298
JB
1285}
1286
af8cdcd8 1287static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
1288{
1289 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 1290 u8 bssid[ETH_ALEN];
46900298
JB
1291 u16 capability;
1292 int i;
1293
076fc877 1294 lockdep_assert_wiphy(sdata->local->hw.wiphy);
7a17a33c 1295
af8cdcd8 1296 if (ifibss->fixed_bssid) {
46900298
JB
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++)
47846c9b 1304 bssid[i] ^= sdata->vif.addr[i];
46900298
JB
1305 bssid[0] &= ~0x01;
1306 bssid[0] |= 0x02;
1307 }
1308
bdcbd8e0 1309 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
46900298 1310
46900298
JB
1311 capability = WLAN_CAPABILITY_IBSS;
1312
fffd0934 1313 if (ifibss->privacy)
46900298 1314 capability |= WLAN_CAPABILITY_PRIVACY;
46900298 1315
57c4d7b4 1316 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
75a423f4 1317 &ifibss->chandef, ifibss->basic_rates,
c13a765b 1318 capability, 0, true);
46900298
JB
1319}
1320
be50baa4
XG
1321static unsigned int ibss_setup_channels(struct wiphy *wiphy,
1322 struct ieee80211_channel **channels,
1323 unsigned int channels_max,
1324 u32 center_freq, u32 width)
76bed0f4
JD
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
1352static unsigned int
1353ieee80211_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;
fc0561dc 1367 fallthrough;
76bed0f4
JD
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
ce9058ae
BP
1391/*
1392 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1393 */
1394
af8cdcd8 1395static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
1396{
1397 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1398 struct ieee80211_local *local = sdata->local;
0c1ad2ca 1399 struct cfg80211_bss *cbss;
af8cdcd8 1400 struct ieee80211_channel *chan = NULL;
46900298
JB
1401 const u8 *bssid = NULL;
1402 int active_ibss;
1403
076fc877 1404 lockdep_assert_wiphy(sdata->local->hw.wiphy);
7a17a33c 1405
46900298 1406 active_ibss = ieee80211_sta_active_ibss(sdata);
bdcbd8e0 1407 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
46900298
JB
1408
1409 if (active_ibss)
af8cdcd8 1410 return;
46900298 1411
af8cdcd8
JB
1412 if (ifibss->fixed_bssid)
1413 bssid = ifibss->bssid;
1414 if (ifibss->fixed_channel)
3aede78a 1415 chan = ifibss->chandef.chan;
af8cdcd8 1416 if (!is_zero_ether_addr(ifibss->bssid))
46900298 1417 bssid = ifibss->bssid;
0c1ad2ca
JB
1418 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1419 ifibss->ssid, ifibss->ssid_len,
6eb18137
DL
1420 IEEE80211_BSS_TYPE_IBSS,
1421 IEEE80211_PRIVACY(ifibss->privacy));
0c1ad2ca
JB
1422
1423 if (cbss) {
1424 struct ieee80211_bss *bss;
46900298 1425
0c1ad2ca 1426 bss = (void *)cbss->priv;
bdcbd8e0
JB
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);
46900298 1433
af8cdcd8 1434 ieee80211_sta_join_ibss(sdata, bss);
46900298 1435 ieee80211_rx_bss_put(local, bss);
af8cdcd8 1436 return;
d419b9f0 1437 }
46900298 1438
d8eb741e
AQ
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
bdcbd8e0 1450 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
46900298
JB
1451
1452 /* Selected IBSS not found in current scan results - try to scan */
ce9058ae 1453 if (time_after(jiffies, ifibss->last_scan_completed +
46900298 1454 IEEE80211_SCAN_INTERVAL)) {
76bed0f4
JD
1455 struct ieee80211_channel *channels[8];
1456 unsigned int num;
1457
bdcbd8e0 1458 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
46900298 1459
d321cd01
SS
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,
5add321c 1467 num);
d321cd01
SS
1468 } else {
1469 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
5add321c 1470 ifibss->ssid_len, NULL, 0);
d321cd01 1471 }
ce9058ae 1472 } else {
46900298
JB
1473 int interval = IEEE80211_SCAN_INTERVAL;
1474
1475 if (time_after(jiffies, ifibss->ibss_join_req +
55de908a
JB
1476 IEEE80211_IBSS_JOIN_TIMEOUT))
1477 ieee80211_sta_create_ibss(sdata);
46900298 1478
af8cdcd8
JB
1479 mod_timer(&ifibss->timer,
1480 round_jiffies(jiffies + interval));
46900298 1481 }
46900298
JB
1482}
1483
1484static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
c269a203 1485 struct sk_buff *req)
46900298 1486{
c269a203 1487 struct ieee80211_mgmt *mgmt = (void *)req->data;
46900298
JB
1488 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1489 struct ieee80211_local *local = sdata->local;
c269a203 1490 int tx_last_beacon, len = req->len;
46900298 1491 struct sk_buff *skb;
c3ffeab4 1492 struct beacon_data *presp;
46900298
JB
1493 u8 *pos, *end;
1494
076fc877 1495 lockdep_assert_wiphy(sdata->local->hw.wiphy);
7a17a33c 1496
6858ad75 1497 presp = sdata_dereference(ifibss->presp, sdata);
40b275b6 1498
46900298 1499 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
40b275b6 1500 len < 24 + 2 || !presp)
46900298
JB
1501 return;
1502
24487981 1503 tx_last_beacon = drv_tx_last_beacon(local);
46900298 1504
c6e57b38
EG
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);
46900298 1508
1ed76487 1509 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
46900298
JB
1510 return;
1511
b203ca39 1512 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
888d04df 1513 !is_broadcast_ether_addr(mgmt->bssid))
46900298
JB
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) {
bdcbd8e0
JB
1520 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1521 mgmt->sa);
46900298
JB
1522 return;
1523 }
1524 if (pos[1] != 0 &&
1525 (pos[1] != ifibss->ssid_len ||
0da780c2 1526 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
46900298
JB
1527 /* Ignore ProbeReq for foreign SSID */
1528 return;
1529 }
1530
1531 /* Reply with ProbeResp */
c3ffeab4 1532 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
46900298
JB
1533 if (!skb)
1534 return;
1535
c3ffeab4 1536 skb_reserve(skb, local->tx_headroom);
59ae1d12 1537 skb_put_data(skb, presp->head, presp->head_len);
c3ffeab4
JB
1538
1539 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1540 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
62ae67be 1541 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
691eb61b
SW
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
62ae67be 1547 ieee80211_tx_skb(sdata, skb);
46900298
JB
1548}
1549
d45c4172
EG
1550static
1551void 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)
46900298
JB
1554{
1555 size_t baselen;
5d24828d 1556 struct ieee802_11_elems *elems;
46900298 1557
d45c4172
EG
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 */
46900298
JB
1565 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1566 if (baselen > len)
1567 return;
1568
5d24828d 1569 elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
38c6aa29 1570 len - baselen, false, NULL);
46900298 1571
5d24828d
JB
1572 if (elems) {
1573 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, elems);
1574 kfree(elems);
1575 }
46900298
JB
1576}
1577
1fa57d01
JB
1578void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1579 struct sk_buff *skb)
46900298
JB
1580{
1581 struct ieee80211_rx_status *rx_status;
1582 struct ieee80211_mgmt *mgmt;
1583 u16 fc;
5d24828d 1584 struct ieee802_11_elems *elems;
cd7760e6 1585 int ies_len;
46900298 1586
f1d58c25 1587 rx_status = IEEE80211_SKB_RXCB(skb);
46900298
JB
1588 mgmt = (struct ieee80211_mgmt *) skb->data;
1589 fc = le16_to_cpu(mgmt->frame_control);
1590
c926d006 1591 if (!sdata->u.ibss.ssid_len)
076fc877 1592 return; /* not ready to merge yet */
c926d006 1593
46900298
JB
1594 switch (fc & IEEE80211_FCTL_STYPE) {
1595 case IEEE80211_STYPE_PROBE_REQ:
c269a203 1596 ieee80211_rx_mgmt_probe_req(sdata, skb);
46900298
JB
1597 break;
1598 case IEEE80211_STYPE_PROBE_RESP:
46900298 1599 case IEEE80211_STYPE_BEACON:
d45c4172
EG
1600 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1601 rx_status);
46900298
JB
1602 break;
1603 case IEEE80211_STYPE_AUTH:
1604 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1605 break;
2cc59e78
AQ
1606 case IEEE80211_STYPE_DEAUTH:
1607 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1608 break;
cd7760e6
SW
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
5d24828d 1619 elems = ieee802_11_parse_elems(
cd7760e6 1620 mgmt->u.action.u.chan_switch.variable,
38c6aa29 1621 ies_len, true, NULL);
cd7760e6 1622
8223ac19
JB
1623 if (elems && !elems->parse_error)
1624 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt,
1625 skb->len,
1626 rx_status,
1627 elems);
5d24828d 1628 kfree(elems);
cd7760e6
SW
1629 break;
1630 }
46900298 1631 }
46900298
JB
1632}
1633
1fa57d01 1634void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
46900298 1635{
1fa57d01 1636 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
8bf11d8d 1637 struct sta_info *sta;
46900298 1638
7a17a33c
JB
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)
076fc877 1645 return;
46900298 1646
8bf11d8d
JB
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
52874a5e 1654 ieee80211_ibss_finish_sta(sta);
8bf11d8d
JB
1655 rcu_read_unlock();
1656 spin_lock_bh(&ifibss->incomplete_lock);
1657 }
1658 spin_unlock_bh(&ifibss->incomplete_lock);
1659
46900298
JB
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 }
3a4d4aa2
JB
1671}
1672
34f11cd3 1673static void ieee80211_ibss_timer(struct timer_list *t)
46900298
JB
1674{
1675 struct ieee80211_sub_if_data *sdata =
41cb0855 1676 timer_container_of(sdata, t, u.ibss.timer);
5bb644a0 1677
16114496 1678 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
5bb644a0 1679}
5bb644a0 1680
46900298
JB
1681void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1682{
1683 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1684
34f11cd3 1685 timer_setup(&ifibss->timer, ieee80211_ibss_timer, 0);
8bf11d8d
JB
1686 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1687 spin_lock_init(&ifibss->incomplete_lock);
87351d09
JB
1688 wiphy_work_init(&ifibss->csa_connection_drop_work,
1689 ieee80211_csa_connection_drop_work);
46900298
JB
1690}
1691
1692/* scan finished notification */
1693void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1694{
af8cdcd8 1695 struct ieee80211_sub_if_data *sdata;
46900298 1696
be0df01d
JB
1697 lockdep_assert_wiphy(local->hw.wiphy);
1698
29b4a4f7 1699 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 1700 if (!ieee80211_sdata_running(sdata))
0e41f715 1701 continue;
af8cdcd8
JB
1702 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1703 continue;
1704 sdata->u.ibss.last_scan_completed = jiffies;
46900298
JB
1705 }
1706}
1707
af8cdcd8
JB
1708int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1709 struct cfg80211_ibss_params *params)
1710{
15ddba5f 1711 u64 changed = 0;
71965c1d
LC
1712 enum ieee80211_chanctx_mode chanmode;
1713 struct ieee80211_local *local = sdata->local;
1714 int radar_detect_width = 0;
71965c1d
LC
1715 int ret;
1716
5435af6e
JB
1717 lockdep_assert_wiphy(local->hw.wiphy);
1718
b6011960
TP
1719 if (params->chandef.chan->freq_offset) {
1720 /* this may work, but is untested */
1721 return -EOPNOTSUPP;
1722 }
1723
71965c1d
LC
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
71965c1d 1739 ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
0874bcd0 1740 radar_detect_width, -1);
71965c1d
LC
1741 if (ret < 0)
1742 return ret;
af8cdcd8 1743
af8cdcd8
JB
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
fffd0934 1750 sdata->u.ibss.privacy = params->privacy;
267335d6 1751 sdata->u.ibss.control_port = params->control_port;
8e8d347d 1752 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
fbd2c8dc 1753 sdata->u.ibss.basic_rates = params->basic_rates;
c7d37a66 1754 sdata->u.ibss.last_scan_completed = jiffies;
2103dec1
SW
1755
1756 /* fix basic_rates if channel does not support these rates */
dd5b4cc7
FF
1757 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1758 sizeof(params->mcast_rate));
fffd0934 1759
57c4d7b4
JB
1760 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1761
3aede78a 1762 sdata->u.ibss.chandef = params->chandef;
af8cdcd8
JB
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
af8cdcd8
JB
1772 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1773 sdata->u.ibss.ibss_join_req = jiffies;
1774
badecb00 1775 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
0e41f715
JB
1776 sdata->u.ibss.ssid_len = params->ssid_len;
1777
822854b0
SW
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
ff3cc5f4
SW
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
dcbe73ca 1797 changed |= BSS_CHANGED_HT | BSS_CHANGED_MCAST_RATE;
d8675a63 1798 ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
ff3cc5f4 1799
bfd8403a
JB
1800 sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
1801 sdata->deflink.needed_rx_chains = local->rx_chains;
018f6fbf 1802 sdata->control_port_over_nl80211 = params->control_port_over_nl80211;
04ecd257 1803
16114496 1804 wiphy_work_queue(local->hw.wiphy, &sdata->work);
af8cdcd8
JB
1805
1806 return 0;
1807}
1808
1809int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1810{
5ea096c0 1811 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
5ea096c0 1812
b78a4932 1813 ifibss->ssid_len = 0;
b9caeea9 1814 ieee80211_ibss_disconnect(sdata);
c84a67a2 1815 eth_zero_addr(ifibss->bssid);
af8cdcd8
JB
1816
1817 /* remove beacon */
1818 kfree(sdata->u.ibss.ie);
3bd801b1
MT
1819 sdata->u.ibss.ie = NULL;
1820 sdata->u.ibss.ie_len = 0;
822854b0
SW
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
af8cdcd8 1826 synchronize_rcu();
af8cdcd8 1827
35f20c14 1828 skb_queue_purge(&sdata->skb_queue);
5cff20e6 1829
8fa7292f 1830 timer_delete_sync(&sdata->u.ibss.timer);
7a17a33c 1831
af8cdcd8
JB
1832 return 0;
1833}