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