Merge tag 'uml-for-linus-6.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / net / mac80211 / ibss.c
blob05a945df3259aafa970c9d6264f50b53aa75fd61
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * IBSS mode implementation
4 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
10 * Copyright 2013-2014 Intel Mobile Communications GmbH
11 * Copyright(c) 2016 Intel Deutschland GmbH
12 * Copyright(c) 2018-2024 Intel Corporation
15 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/if_ether.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/etherdevice.h>
21 #include <linux/rtnetlink.h>
22 #include <net/mac80211.h>
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "rate.h"
28 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
29 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
31 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
32 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
33 #define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
35 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37 static struct beacon_data *
38 ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
39 const int beacon_int, const u32 basic_rates,
40 const u16 capability, u64 tsf,
41 struct cfg80211_chan_def *chandef,
42 bool *have_higher_than_11mbit,
43 struct cfg80211_csa_settings *csa_settings)
45 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46 struct ieee80211_local *local = sdata->local;
47 int rates_n = 0, i, ri;
48 struct ieee80211_mgmt *mgmt;
49 u8 *pos;
50 struct ieee80211_supported_band *sband;
51 u32 rate_flags, rates = 0, rates_added = 0;
52 struct beacon_data *presp;
53 int frame_len;
55 /* Build IBSS probe response */
56 frame_len = sizeof(struct ieee80211_hdr_3addr) +
57 12 /* struct ieee80211_mgmt.u.beacon */ +
58 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
59 2 + 8 /* max Supported Rates */ +
60 3 /* max DS params */ +
61 4 /* IBSS params */ +
62 5 /* Channel Switch Announcement */ +
63 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
64 2 + sizeof(struct ieee80211_ht_cap) +
65 2 + sizeof(struct ieee80211_ht_operation) +
66 2 + sizeof(struct ieee80211_vht_cap) +
67 2 + sizeof(struct ieee80211_vht_operation) +
68 ifibss->ie_len;
69 presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
70 if (!presp)
71 return NULL;
73 presp->head = (void *)(presp + 1);
75 mgmt = (void *) presp->head;
76 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
77 IEEE80211_STYPE_PROBE_RESP);
78 eth_broadcast_addr(mgmt->da);
79 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
80 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
81 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
82 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
83 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
85 pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
87 *pos++ = WLAN_EID_SSID;
88 *pos++ = ifibss->ssid_len;
89 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
90 pos += ifibss->ssid_len;
92 sband = local->hw.wiphy->bands[chandef->chan->band];
93 rate_flags = ieee80211_chandef_rate_flags(chandef);
94 rates_n = 0;
95 if (have_higher_than_11mbit)
96 *have_higher_than_11mbit = false;
98 for (i = 0; i < sband->n_bitrates; i++) {
99 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
100 continue;
101 if (sband->bitrates[i].bitrate > 110 &&
102 have_higher_than_11mbit)
103 *have_higher_than_11mbit = true;
105 rates |= BIT(i);
106 rates_n++;
109 *pos++ = WLAN_EID_SUPP_RATES;
110 *pos++ = min_t(int, 8, rates_n);
111 for (ri = 0; ri < sband->n_bitrates; ri++) {
112 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, 5);
113 u8 basic = 0;
114 if (!(rates & BIT(ri)))
115 continue;
117 if (basic_rates & BIT(ri))
118 basic = 0x80;
119 *pos++ = basic | (u8) rate;
120 if (++rates_added == 8) {
121 ri++; /* continue at next rate for EXT_SUPP_RATES */
122 break;
126 if (sband->band == NL80211_BAND_2GHZ) {
127 *pos++ = WLAN_EID_DS_PARAMS;
128 *pos++ = 1;
129 *pos++ = ieee80211_frequency_to_channel(
130 chandef->chan->center_freq);
133 *pos++ = WLAN_EID_IBSS_PARAMS;
134 *pos++ = 2;
135 /* FIX: set ATIM window based on scan results */
136 *pos++ = 0;
137 *pos++ = 0;
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);
145 presp->cntdwn_counter_offsets[0] = (pos - presp->head);
146 *pos++ = csa_settings->count;
147 presp->cntdwn_current_counter = csa_settings->count;
150 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
151 if (rates_n > 8) {
152 *pos++ = WLAN_EID_EXT_SUPP_RATES;
153 *pos++ = rates_n - 8;
154 for (; ri < sband->n_bitrates; ri++) {
155 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, 5);
156 u8 basic = 0;
157 if (!(rates & BIT(ri)))
158 continue;
160 if (basic_rates & BIT(ri))
161 basic = 0x80;
162 *pos++ = basic | (u8) rate;
166 if (ifibss->ie_len) {
167 memcpy(pos, ifibss->ie, ifibss->ie_len);
168 pos += ifibss->ie_len;
171 /* add HT capability and information IEs */
172 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
173 chandef->width != NL80211_CHAN_WIDTH_5 &&
174 chandef->width != NL80211_CHAN_WIDTH_10 &&
175 sband->ht_cap.ht_supported) {
176 struct ieee80211_sta_ht_cap ht_cap;
178 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
179 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
181 pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
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
187 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
188 chandef, 0, false);
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);
201 if (local->hw.queues >= IEEE80211_NUM_ACS)
202 pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
204 presp->head_len = pos - presp->head;
205 if (WARN_ON(presp->head_len > frame_len))
206 goto error;
208 return presp;
209 error:
210 kfree(presp);
211 return NULL;
214 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
215 const u8 *bssid, const int beacon_int,
216 struct cfg80211_chan_def *req_chandef,
217 const u32 basic_rates,
218 const u16 capability, u64 tsf,
219 bool creator)
221 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
222 struct ieee80211_local *local = sdata->local;
223 struct ieee80211_mgmt *mgmt;
224 struct cfg80211_bss *bss;
225 u64 bss_change;
226 struct ieee80211_chan_req chanreq = {};
227 struct ieee80211_channel *chan;
228 struct beacon_data *presp;
229 struct cfg80211_inform_bss bss_meta = {};
230 bool have_higher_than_11mbit;
231 bool radar_required;
232 int err;
234 lockdep_assert_wiphy(local->hw.wiphy);
236 /* Reset own TSF to allow time synchronization work. */
237 drv_reset_tsf(local, sdata);
239 if (!ether_addr_equal(ifibss->bssid, bssid))
240 sta_info_flush(sdata, -1);
242 /* if merging, indicate to driver that we leave the old IBSS */
243 if (sdata->vif.cfg.ibss_joined) {
244 sdata->vif.cfg.ibss_joined = false;
245 sdata->vif.cfg.ibss_creator = false;
246 sdata->vif.bss_conf.enable_beacon = false;
247 netif_carrier_off(sdata->dev);
248 synchronize_net();
249 ieee80211_bss_info_change_notify(sdata,
250 BSS_CHANGED_IBSS |
251 BSS_CHANGED_BEACON_ENABLED);
252 drv_leave_ibss(local, sdata);
255 presp = sdata_dereference(ifibss->presp, sdata);
256 RCU_INIT_POINTER(ifibss->presp, NULL);
257 if (presp)
258 kfree_rcu(presp, rcu_head);
260 /* make a copy of the chandef, it could be modified below. */
261 chanreq.oper = *req_chandef;
262 chan = chanreq.oper.chan;
263 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chanreq.oper,
264 NL80211_IFTYPE_ADHOC)) {
265 if (chanreq.oper.width == NL80211_CHAN_WIDTH_5 ||
266 chanreq.oper.width == NL80211_CHAN_WIDTH_10 ||
267 chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT ||
268 chanreq.oper.width == NL80211_CHAN_WIDTH_20) {
269 sdata_info(sdata,
270 "Failed to join IBSS, beacons forbidden\n");
271 return;
273 chanreq.oper.width = NL80211_CHAN_WIDTH_20;
274 chanreq.oper.center_freq1 = chan->center_freq;
275 /* check again for downgraded chandef */
276 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chanreq.oper,
277 NL80211_IFTYPE_ADHOC)) {
278 sdata_info(sdata,
279 "Failed to join IBSS, beacons forbidden\n");
280 return;
284 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
285 &chanreq.oper, NL80211_IFTYPE_ADHOC);
286 if (err < 0) {
287 sdata_info(sdata,
288 "Failed to join IBSS, invalid chandef\n");
289 return;
291 if (err > 0 && !ifibss->userspace_handles_dfs) {
292 sdata_info(sdata,
293 "Failed to join IBSS, DFS channel without control program\n");
294 return;
297 radar_required = err;
299 if (ieee80211_link_use_channel(&sdata->deflink, &chanreq,
300 ifibss->fixed_channel ?
301 IEEE80211_CHANCTX_SHARED :
302 IEEE80211_CHANCTX_EXCLUSIVE)) {
303 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
304 return;
306 sdata->deflink.radar_required = radar_required;
308 memcpy(ifibss->bssid, bssid, ETH_ALEN);
310 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
311 capability, tsf, &chanreq.oper,
312 &have_higher_than_11mbit, NULL);
313 if (!presp)
314 return;
316 rcu_assign_pointer(ifibss->presp, presp);
317 mgmt = (void *)presp->head;
319 sdata->vif.bss_conf.enable_beacon = true;
320 sdata->vif.bss_conf.beacon_int = beacon_int;
321 sdata->vif.bss_conf.basic_rates = basic_rates;
322 sdata->vif.cfg.ssid_len = ifibss->ssid_len;
323 memcpy(sdata->vif.cfg.ssid, ifibss->ssid, ifibss->ssid_len);
324 bss_change = BSS_CHANGED_BEACON_INT;
325 bss_change |= ieee80211_reset_erp_info(sdata);
326 bss_change |= BSS_CHANGED_BSSID;
327 bss_change |= BSS_CHANGED_BEACON;
328 bss_change |= BSS_CHANGED_BEACON_ENABLED;
329 bss_change |= BSS_CHANGED_BASIC_RATES;
330 bss_change |= BSS_CHANGED_HT;
331 bss_change |= BSS_CHANGED_IBSS;
332 bss_change |= BSS_CHANGED_SSID;
335 * In 5 GHz/802.11a, we can always use short slot time.
336 * (IEEE 802.11-2012 18.3.8.7)
338 * In 2.4GHz, we must always use long slots in IBSS for compatibility
339 * reasons.
340 * (IEEE 802.11-2012 19.4.5)
342 * HT follows these specifications (IEEE 802.11-2012 20.3.18)
344 sdata->vif.bss_conf.use_short_slot = chan->band == NL80211_BAND_5GHZ;
345 bss_change |= BSS_CHANGED_ERP_SLOT;
347 /* cf. IEEE 802.11 9.2.12 */
348 sdata->deflink.operating_11g_mode =
349 chan->band == NL80211_BAND_2GHZ && have_higher_than_11mbit;
351 ieee80211_set_wmm_default(&sdata->deflink, true, false);
353 sdata->vif.cfg.ibss_joined = true;
354 sdata->vif.cfg.ibss_creator = creator;
356 err = drv_join_ibss(local, sdata);
357 if (err) {
358 sdata->vif.cfg.ibss_joined = false;
359 sdata->vif.cfg.ibss_creator = false;
360 sdata->vif.bss_conf.enable_beacon = false;
361 sdata->vif.cfg.ssid_len = 0;
362 RCU_INIT_POINTER(ifibss->presp, NULL);
363 kfree_rcu(presp, rcu_head);
364 ieee80211_link_release_channel(&sdata->deflink);
365 sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
366 err);
367 return;
370 ieee80211_bss_info_change_notify(sdata, bss_change);
372 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
373 mod_timer(&ifibss->timer,
374 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
376 bss_meta.chan = chan;
377 bss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, mgmt,
378 presp->head_len, GFP_KERNEL);
380 cfg80211_put_bss(local->hw.wiphy, bss);
381 netif_carrier_on(sdata->dev);
382 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
385 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
386 struct ieee80211_bss *bss)
388 struct cfg80211_bss *cbss =
389 container_of((void *)bss, struct cfg80211_bss, priv);
390 struct ieee80211_supported_band *sband;
391 struct cfg80211_chan_def chandef;
392 u32 basic_rates;
393 int i, j;
394 u16 beacon_int = cbss->beacon_interval;
395 const struct cfg80211_bss_ies *ies;
396 enum nl80211_channel_type chan_type;
397 u64 tsf;
398 u32 rate_flags;
400 lockdep_assert_wiphy(sdata->local->hw.wiphy);
402 if (beacon_int < 10)
403 beacon_int = 10;
405 switch (sdata->u.ibss.chandef.width) {
406 case NL80211_CHAN_WIDTH_20_NOHT:
407 case NL80211_CHAN_WIDTH_20:
408 case NL80211_CHAN_WIDTH_40:
409 chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
410 cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
411 break;
412 case NL80211_CHAN_WIDTH_5:
413 case NL80211_CHAN_WIDTH_10:
414 cfg80211_chandef_create(&chandef, cbss->channel,
415 NL80211_CHAN_NO_HT);
416 chandef.width = sdata->u.ibss.chandef.width;
417 break;
418 case NL80211_CHAN_WIDTH_80:
419 case NL80211_CHAN_WIDTH_80P80:
420 case NL80211_CHAN_WIDTH_160:
421 chandef = sdata->u.ibss.chandef;
422 chandef.chan = cbss->channel;
423 break;
424 default:
425 /* fall back to 20 MHz for unsupported modes */
426 cfg80211_chandef_create(&chandef, cbss->channel,
427 NL80211_CHAN_NO_HT);
428 break;
431 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
432 rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
434 basic_rates = 0;
436 for (i = 0; i < bss->supp_rates_len; i++) {
437 int rate = bss->supp_rates[i] & 0x7f;
438 bool is_basic = !!(bss->supp_rates[i] & 0x80);
440 for (j = 0; j < sband->n_bitrates; j++) {
441 int brate;
442 if ((rate_flags & sband->bitrates[j].flags)
443 != rate_flags)
444 continue;
446 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate, 5);
447 if (brate == rate) {
448 if (is_basic)
449 basic_rates |= BIT(j);
450 break;
455 rcu_read_lock();
456 ies = rcu_dereference(cbss->ies);
457 tsf = ies->tsf;
458 rcu_read_unlock();
460 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
461 beacon_int,
462 &chandef,
463 basic_rates,
464 cbss->capability,
465 tsf, false);
468 int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
469 struct cfg80211_csa_settings *csa_settings,
470 u64 *changed)
472 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
473 struct beacon_data *presp, *old_presp;
474 struct cfg80211_bss *cbss;
475 const struct cfg80211_bss_ies *ies;
476 u16 capability = WLAN_CAPABILITY_IBSS;
477 u64 tsf;
479 lockdep_assert_wiphy(sdata->local->hw.wiphy);
481 if (ifibss->privacy)
482 capability |= WLAN_CAPABILITY_PRIVACY;
484 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
485 ifibss->bssid, ifibss->ssid,
486 ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
487 IEEE80211_PRIVACY(ifibss->privacy));
489 if (unlikely(!cbss))
490 return -EINVAL;
492 rcu_read_lock();
493 ies = rcu_dereference(cbss->ies);
494 tsf = ies->tsf;
495 rcu_read_unlock();
496 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
498 old_presp = sdata_dereference(ifibss->presp, sdata);
500 presp = ieee80211_ibss_build_presp(sdata,
501 sdata->vif.bss_conf.beacon_int,
502 sdata->vif.bss_conf.basic_rates,
503 capability, tsf, &ifibss->chandef,
504 NULL, csa_settings);
505 if (!presp)
506 return -ENOMEM;
508 rcu_assign_pointer(ifibss->presp, presp);
509 if (old_presp)
510 kfree_rcu(old_presp, rcu_head);
512 *changed |= BSS_CHANGED_BEACON;
513 return 0;
516 int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata, u64 *changed)
518 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
519 struct cfg80211_bss *cbss;
521 lockdep_assert_wiphy(sdata->local->hw.wiphy);
523 /* When not connected/joined, sending CSA doesn't make sense. */
524 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED)
525 return -ENOLINK;
527 /* update cfg80211 bss information with the new channel */
528 if (!is_zero_ether_addr(ifibss->bssid)) {
529 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
530 ifibss->chandef.chan,
531 ifibss->bssid, ifibss->ssid,
532 ifibss->ssid_len,
533 IEEE80211_BSS_TYPE_IBSS,
534 IEEE80211_PRIVACY(ifibss->privacy));
535 /* XXX: should not really modify cfg80211 data */
536 if (cbss) {
537 cbss->channel = sdata->deflink.csa.chanreq.oper.chan;
538 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
542 ifibss->chandef = sdata->deflink.csa.chanreq.oper;
544 /* generate the beacon */
545 return ieee80211_ibss_csa_beacon(sdata, NULL, changed);
548 void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
550 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
552 wiphy_work_cancel(sdata->local->hw.wiphy,
553 &ifibss->csa_connection_drop_work);
556 static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
557 __acquires(RCU)
559 struct ieee80211_sub_if_data *sdata = sta->sdata;
560 u8 addr[ETH_ALEN];
562 memcpy(addr, sta->sta.addr, ETH_ALEN);
564 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
566 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
567 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
568 /* authorize the station only if the network is not RSN protected. If
569 * not wait for the userspace to authorize it */
570 if (!sta->sdata->u.ibss.control_port)
571 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
573 rate_control_rate_init(&sta->deflink);
575 /* If it fails, maybe we raced another insertion? */
576 if (sta_info_insert_rcu(sta))
577 return sta_info_get(sdata, addr);
578 return sta;
581 static struct sta_info *
582 ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
583 const u8 *addr, u32 supp_rates)
584 __acquires(RCU)
586 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
587 struct ieee80211_local *local = sdata->local;
588 struct sta_info *sta;
589 struct ieee80211_chanctx_conf *chanctx_conf;
590 struct ieee80211_supported_band *sband;
591 int band;
594 * XXX: Consider removing the least recently used entry and
595 * allow new one to be added.
597 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
598 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
599 sdata->name, addr);
600 rcu_read_lock();
601 return NULL;
604 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
605 rcu_read_lock();
606 return NULL;
609 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
610 rcu_read_lock();
611 return NULL;
614 rcu_read_lock();
615 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
616 if (WARN_ON_ONCE(!chanctx_conf))
617 return NULL;
618 band = chanctx_conf->def.chan->band;
619 rcu_read_unlock();
621 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
622 if (!sta) {
623 rcu_read_lock();
624 return NULL;
627 /* make sure mandatory rates are always added */
628 sband = local->hw.wiphy->bands[band];
629 sta->sta.deflink.supp_rates[band] = supp_rates |
630 ieee80211_mandatory_rates(sband);
632 return ieee80211_ibss_finish_sta(sta);
635 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
637 struct ieee80211_local *local = sdata->local;
638 int active = 0;
639 struct sta_info *sta;
641 lockdep_assert_wiphy(sdata->local->hw.wiphy);
643 rcu_read_lock();
645 list_for_each_entry_rcu(sta, &local->sta_list, list) {
646 unsigned long last_active = ieee80211_sta_last_active(sta);
648 if (sta->sdata == sdata &&
649 time_is_after_jiffies(last_active +
650 IEEE80211_IBSS_MERGE_INTERVAL)) {
651 active++;
652 break;
656 rcu_read_unlock();
658 return active;
661 static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
663 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
664 struct ieee80211_local *local = sdata->local;
665 struct cfg80211_bss *cbss;
666 struct beacon_data *presp;
667 struct sta_info *sta;
669 lockdep_assert_wiphy(local->hw.wiphy);
671 if (!is_zero_ether_addr(ifibss->bssid)) {
672 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
673 ifibss->bssid, ifibss->ssid,
674 ifibss->ssid_len,
675 IEEE80211_BSS_TYPE_IBSS,
676 IEEE80211_PRIVACY(ifibss->privacy));
678 if (cbss) {
679 cfg80211_unlink_bss(local->hw.wiphy, cbss);
680 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
684 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
686 sta_info_flush(sdata, -1);
688 spin_lock_bh(&ifibss->incomplete_lock);
689 while (!list_empty(&ifibss->incomplete_stations)) {
690 sta = list_first_entry(&ifibss->incomplete_stations,
691 struct sta_info, list);
692 list_del(&sta->list);
693 spin_unlock_bh(&ifibss->incomplete_lock);
695 sta_info_free(local, sta);
696 spin_lock_bh(&ifibss->incomplete_lock);
698 spin_unlock_bh(&ifibss->incomplete_lock);
700 netif_carrier_off(sdata->dev);
702 sdata->vif.cfg.ibss_joined = false;
703 sdata->vif.cfg.ibss_creator = false;
704 sdata->vif.bss_conf.enable_beacon = false;
705 sdata->vif.cfg.ssid_len = 0;
707 /* remove beacon */
708 presp = sdata_dereference(ifibss->presp, sdata);
709 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
710 if (presp)
711 kfree_rcu(presp, rcu_head);
713 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
714 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
715 BSS_CHANGED_IBSS);
716 drv_leave_ibss(local, sdata);
717 ieee80211_link_release_channel(&sdata->deflink);
720 static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy,
721 struct wiphy_work *work)
723 struct ieee80211_sub_if_data *sdata =
724 container_of(work, struct ieee80211_sub_if_data,
725 u.ibss.csa_connection_drop_work);
727 ieee80211_ibss_disconnect(sdata);
728 synchronize_rcu();
729 skb_queue_purge(&sdata->skb_queue);
731 /* trigger a scan to find another IBSS network to join */
732 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
735 static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
737 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
738 int err;
740 /* if the current channel is a DFS channel, mark the channel as
741 * unavailable.
743 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
744 &ifibss->chandef,
745 NL80211_IFTYPE_ADHOC);
746 if (err > 0)
747 cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
748 GFP_ATOMIC);
751 static bool
752 ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
753 struct ieee802_11_elems *elems,
754 bool beacon)
756 struct cfg80211_csa_settings params;
757 struct ieee80211_csa_ie csa_ie;
758 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
759 enum nl80211_channel_type ch_type;
760 int err;
761 struct ieee80211_conn_settings conn = {
762 .mode = IEEE80211_CONN_MODE_HT,
763 .bw_limit = IEEE80211_CONN_BW_LIMIT_40,
765 u32 vht_cap_info = 0;
767 lockdep_assert_wiphy(sdata->local->hw.wiphy);
769 switch (ifibss->chandef.width) {
770 case NL80211_CHAN_WIDTH_5:
771 case NL80211_CHAN_WIDTH_10:
772 case NL80211_CHAN_WIDTH_20_NOHT:
773 conn.mode = IEEE80211_CONN_MODE_LEGACY;
774 fallthrough;
775 case NL80211_CHAN_WIDTH_20:
776 conn.bw_limit = IEEE80211_CONN_BW_LIMIT_20;
777 break;
778 default:
779 break;
782 if (elems->vht_cap_elem)
783 vht_cap_info = le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
785 memset(&params, 0, sizeof(params));
786 err = ieee80211_parse_ch_switch_ie(sdata, elems,
787 ifibss->chandef.chan->band,
788 vht_cap_info, &conn,
789 ifibss->bssid, false,
790 &csa_ie);
791 /* can't switch to destination channel, fail */
792 if (err < 0)
793 goto disconnect;
795 /* did not contain a CSA */
796 if (err)
797 return false;
799 /* channel switch is not supported, disconnect */
800 if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
801 goto disconnect;
803 params.count = csa_ie.count;
804 params.chandef = csa_ie.chanreq.oper;
806 switch (ifibss->chandef.width) {
807 case NL80211_CHAN_WIDTH_20_NOHT:
808 case NL80211_CHAN_WIDTH_20:
809 case NL80211_CHAN_WIDTH_40:
810 /* keep our current HT mode (HT20/HT40+/HT40-), even if
811 * another mode has been announced. The mode is not adopted
812 * within the beacon while doing CSA and we should therefore
813 * keep the mode which we announce.
815 ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
816 cfg80211_chandef_create(&params.chandef, params.chandef.chan,
817 ch_type);
818 break;
819 case NL80211_CHAN_WIDTH_5:
820 case NL80211_CHAN_WIDTH_10:
821 if (params.chandef.width != ifibss->chandef.width) {
822 sdata_info(sdata,
823 "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
824 ifibss->bssid,
825 params.chandef.chan->center_freq,
826 params.chandef.width,
827 params.chandef.center_freq1,
828 params.chandef.center_freq2);
829 goto disconnect;
831 break;
832 default:
833 /* should not happen, conn_flags should prevent VHT modes. */
834 WARN_ON(1);
835 goto disconnect;
838 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
839 NL80211_IFTYPE_ADHOC)) {
840 sdata_info(sdata,
841 "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
842 ifibss->bssid,
843 params.chandef.chan->center_freq,
844 params.chandef.width,
845 params.chandef.center_freq1,
846 params.chandef.center_freq2);
847 goto disconnect;
850 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
851 &params.chandef,
852 NL80211_IFTYPE_ADHOC);
853 if (err < 0)
854 goto disconnect;
855 if (err > 0 && !ifibss->userspace_handles_dfs) {
856 /* IBSS-DFS only allowed with a control program */
857 goto disconnect;
860 params.radar_required = err;
862 if (cfg80211_chandef_identical(&params.chandef,
863 &sdata->vif.bss_conf.chanreq.oper)) {
864 ibss_dbg(sdata,
865 "received csa with an identical chandef, ignoring\n");
866 return true;
869 /* all checks done, now perform the channel switch. */
870 ibss_dbg(sdata,
871 "received channel switch announcement to go to channel %d MHz\n",
872 params.chandef.chan->center_freq);
874 params.block_tx = !!csa_ie.mode;
876 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
877 &params))
878 goto disconnect;
880 ieee80211_ibss_csa_mark_radar(sdata);
882 return true;
883 disconnect:
884 ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
885 wiphy_work_queue(sdata->local->hw.wiphy,
886 &ifibss->csa_connection_drop_work);
888 ieee80211_ibss_csa_mark_radar(sdata);
890 return true;
893 static void
894 ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
895 struct ieee80211_mgmt *mgmt, size_t len,
896 struct ieee80211_rx_status *rx_status,
897 struct ieee802_11_elems *elems)
899 int required_len;
901 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
902 return;
904 /* CSA is the only action we handle for now */
905 if (mgmt->u.action.u.measurement.action_code !=
906 WLAN_ACTION_SPCT_CHL_SWITCH)
907 return;
909 required_len = IEEE80211_MIN_ACTION_SIZE +
910 sizeof(mgmt->u.action.u.chan_switch);
911 if (len < required_len)
912 return;
914 if (!sdata->vif.bss_conf.csa_active)
915 ieee80211_ibss_process_chanswitch(sdata, elems, false);
918 static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
919 struct ieee80211_mgmt *mgmt,
920 size_t len)
922 u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
924 if (len < IEEE80211_DEAUTH_FRAME_LEN)
925 return;
927 ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
928 ibss_dbg(sdata, "\tBSSID=%pM (reason: %d)\n", mgmt->bssid, reason);
929 sta_info_destroy_addr(sdata, mgmt->sa);
932 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
933 struct ieee80211_mgmt *mgmt,
934 size_t len)
936 u16 auth_alg, auth_transaction;
938 lockdep_assert_wiphy(sdata->local->hw.wiphy);
940 if (len < 24 + 6)
941 return;
943 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
944 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
946 ibss_dbg(sdata, "RX Auth SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
947 ibss_dbg(sdata, "\tBSSID=%pM (auth_transaction=%d)\n",
948 mgmt->bssid, auth_transaction);
950 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
951 return;
954 * IEEE 802.11 standard does not require authentication in IBSS
955 * networks and most implementations do not seem to use it.
956 * However, try to reply to authentication attempts if someone
957 * has actually implemented this.
959 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
960 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
963 static void ieee80211_update_sta_info(struct ieee80211_sub_if_data *sdata,
964 struct ieee80211_mgmt *mgmt, size_t len,
965 struct ieee80211_rx_status *rx_status,
966 struct ieee802_11_elems *elems,
967 struct ieee80211_channel *channel)
969 struct sta_info *sta;
970 enum nl80211_band band = rx_status->band;
971 struct ieee80211_local *local = sdata->local;
972 struct ieee80211_supported_band *sband;
973 bool rates_updated = false;
974 u32 supp_rates = 0;
976 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
977 return;
979 if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
980 return;
982 sband = local->hw.wiphy->bands[band];
983 if (WARN_ON(!sband))
984 return;
986 rcu_read_lock();
987 sta = sta_info_get(sdata, mgmt->sa);
989 if (elems->supp_rates) {
990 supp_rates = ieee80211_sta_get_rates(sdata, elems,
991 band, NULL);
992 if (sta) {
993 u32 prev_rates;
995 prev_rates = sta->sta.deflink.supp_rates[band];
997 sta->sta.deflink.supp_rates[band] = supp_rates |
998 ieee80211_mandatory_rates(sband);
999 if (sta->sta.deflink.supp_rates[band] != prev_rates) {
1000 ibss_dbg(sdata,
1001 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
1002 sta->sta.addr, prev_rates,
1003 sta->sta.deflink.supp_rates[band]);
1004 rates_updated = true;
1006 } else {
1007 rcu_read_unlock();
1008 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1009 mgmt->sa, supp_rates);
1013 if (sta && !sta->sta.wme &&
1014 (elems->wmm_info || elems->s1g_capab) &&
1015 local->hw.queues >= IEEE80211_NUM_ACS) {
1016 sta->sta.wme = true;
1017 ieee80211_check_fast_xmit(sta);
1020 if (sta && elems->ht_operation && elems->ht_cap_elem &&
1021 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
1022 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
1023 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
1024 /* we both use HT */
1025 struct ieee80211_ht_cap htcap_ie;
1026 struct cfg80211_chan_def chandef;
1027 enum ieee80211_sta_rx_bandwidth bw = sta->sta.deflink.bandwidth;
1029 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
1030 ieee80211_chandef_ht_oper(elems->ht_operation, &chandef);
1032 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
1033 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1034 &htcap_ie,
1035 &sta->deflink);
1037 if (elems->vht_operation && elems->vht_cap_elem &&
1038 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
1039 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
1040 /* we both use VHT */
1041 struct ieee80211_vht_cap cap_ie;
1042 struct ieee80211_sta_vht_cap cap = sta->sta.deflink.vht_cap;
1043 u32 vht_cap_info =
1044 le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
1046 ieee80211_chandef_vht_oper(&local->hw, vht_cap_info,
1047 elems->vht_operation,
1048 elems->ht_operation,
1049 &chandef);
1050 memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1051 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1052 &cap_ie, NULL,
1053 &sta->deflink);
1054 if (memcmp(&cap, &sta->sta.deflink.vht_cap, sizeof(cap)))
1055 rates_updated |= true;
1058 if (bw != sta->sta.deflink.bandwidth)
1059 rates_updated |= true;
1061 if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1062 &chandef))
1063 WARN_ON_ONCE(1);
1066 if (sta && rates_updated) {
1067 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1068 u8 rx_nss = sta->sta.deflink.rx_nss;
1070 /* Force rx_nss recalculation */
1071 sta->sta.deflink.rx_nss = 0;
1072 rate_control_rate_init(&sta->deflink);
1073 if (sta->sta.deflink.rx_nss != rx_nss)
1074 changed |= IEEE80211_RC_NSS_CHANGED;
1076 drv_link_sta_rc_update(local, sdata, &sta->sta.deflink,
1077 changed);
1080 rcu_read_unlock();
1083 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1084 struct ieee80211_mgmt *mgmt, size_t len,
1085 struct ieee80211_rx_status *rx_status,
1086 struct ieee802_11_elems *elems)
1088 struct ieee80211_local *local = sdata->local;
1089 struct cfg80211_bss *cbss;
1090 struct ieee80211_bss *bss;
1091 struct ieee80211_channel *channel;
1092 u64 beacon_timestamp, rx_timestamp;
1093 u32 supp_rates = 0;
1094 enum nl80211_band band = rx_status->band;
1096 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1097 if (!channel)
1098 return;
1100 ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
1102 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
1103 if (!bss)
1104 return;
1106 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1108 /* same for beacon and probe response */
1109 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1111 /* check if we need to merge IBSS */
1113 /* not an IBSS */
1114 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
1115 goto put_bss;
1117 /* different channel */
1118 if (sdata->u.ibss.fixed_channel &&
1119 sdata->u.ibss.chandef.chan != cbss->channel)
1120 goto put_bss;
1122 /* different SSID */
1123 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
1124 memcmp(elems->ssid, sdata->u.ibss.ssid,
1125 sdata->u.ibss.ssid_len))
1126 goto put_bss;
1128 /* process channel switch */
1129 if (sdata->vif.bss_conf.csa_active ||
1130 ieee80211_ibss_process_chanswitch(sdata, elems, true))
1131 goto put_bss;
1133 /* same BSSID */
1134 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
1135 goto put_bss;
1137 /* we use a fixed BSSID */
1138 if (sdata->u.ibss.fixed_bssid)
1139 goto put_bss;
1141 if (ieee80211_have_rx_timestamp(rx_status)) {
1142 /* time when timestamp field was received */
1143 rx_timestamp =
1144 ieee80211_calculate_rx_timestamp(local, rx_status,
1145 len + FCS_LEN, 24);
1146 } else {
1148 * second best option: get current TSF
1149 * (will return -1 if not supported)
1151 rx_timestamp = drv_get_tsf(local, sdata);
1154 ibss_dbg(sdata, "RX beacon SA=%pM BSSID=%pM TSF=0x%llx\n",
1155 mgmt->sa, mgmt->bssid,
1156 (unsigned long long)rx_timestamp);
1157 ibss_dbg(sdata, "\tBCN=0x%llx diff=%lld @%lu\n",
1158 (unsigned long long)beacon_timestamp,
1159 (unsigned long long)(rx_timestamp - beacon_timestamp),
1160 jiffies);
1162 if (beacon_timestamp > rx_timestamp) {
1163 ibss_dbg(sdata,
1164 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1165 mgmt->bssid);
1166 ieee80211_sta_join_ibss(sdata, bss);
1167 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
1168 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
1169 supp_rates);
1170 rcu_read_unlock();
1173 put_bss:
1174 ieee80211_rx_bss_put(local, bss);
1177 void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1178 const u8 *bssid, const u8 *addr,
1179 u32 supp_rates)
1181 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1182 struct ieee80211_local *local = sdata->local;
1183 struct sta_info *sta;
1184 struct ieee80211_chanctx_conf *chanctx_conf;
1185 struct ieee80211_supported_band *sband;
1186 int band;
1189 * XXX: Consider removing the least recently used entry and
1190 * allow new one to be added.
1192 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
1193 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
1194 sdata->name, addr);
1195 return;
1198 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
1199 return;
1201 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
1202 return;
1204 rcu_read_lock();
1205 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1206 if (WARN_ON_ONCE(!chanctx_conf)) {
1207 rcu_read_unlock();
1208 return;
1210 band = chanctx_conf->def.chan->band;
1211 rcu_read_unlock();
1213 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
1214 if (!sta)
1215 return;
1217 /* make sure mandatory rates are always added */
1218 sband = local->hw.wiphy->bands[band];
1219 sta->sta.deflink.supp_rates[band] = supp_rates |
1220 ieee80211_mandatory_rates(sband);
1222 spin_lock(&ifibss->incomplete_lock);
1223 list_add(&sta->list, &ifibss->incomplete_stations);
1224 spin_unlock(&ifibss->incomplete_lock);
1225 wiphy_work_queue(local->hw.wiphy, &sdata->work);
1228 static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1230 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1231 struct ieee80211_local *local = sdata->local;
1232 struct sta_info *sta, *tmp;
1233 unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
1234 unsigned long exp_rsn = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
1236 lockdep_assert_wiphy(local->hw.wiphy);
1238 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1239 unsigned long last_active = ieee80211_sta_last_active(sta);
1241 if (sdata != sta->sdata)
1242 continue;
1244 if (time_is_before_jiffies(last_active + exp_time) ||
1245 (time_is_before_jiffies(last_active + exp_rsn) &&
1246 sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
1247 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1249 sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
1250 sta->sta_state != IEEE80211_STA_AUTHORIZED ?
1251 "not authorized " : "", sta->sta.addr);
1253 ieee80211_send_deauth_disassoc(sdata, sta->sta.addr,
1254 ifibss->bssid,
1255 IEEE80211_STYPE_DEAUTH,
1256 WLAN_REASON_DEAUTH_LEAVING,
1257 true, frame_buf);
1258 WARN_ON(__sta_info_destroy(sta));
1264 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1267 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1269 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1271 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1273 mod_timer(&ifibss->timer,
1274 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
1276 ieee80211_ibss_sta_expire(sdata);
1278 if (time_before(jiffies, ifibss->last_scan_completed +
1279 IEEE80211_IBSS_MERGE_INTERVAL))
1280 return;
1282 if (ieee80211_sta_active_ibss(sdata))
1283 return;
1285 if (ifibss->fixed_channel)
1286 return;
1288 sdata_info(sdata,
1289 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
1291 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
1292 NULL, 0);
1295 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
1297 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1298 u8 bssid[ETH_ALEN];
1299 u16 capability;
1300 int i;
1302 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1304 if (ifibss->fixed_bssid) {
1305 memcpy(bssid, ifibss->bssid, ETH_ALEN);
1306 } else {
1307 /* Generate random, not broadcast, locally administered BSSID. Mix in
1308 * own MAC address to make sure that devices that do not have proper
1309 * random number generator get different BSSID. */
1310 get_random_bytes(bssid, ETH_ALEN);
1311 for (i = 0; i < ETH_ALEN; i++)
1312 bssid[i] ^= sdata->vif.addr[i];
1313 bssid[0] &= ~0x01;
1314 bssid[0] |= 0x02;
1317 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
1319 capability = WLAN_CAPABILITY_IBSS;
1321 if (ifibss->privacy)
1322 capability |= WLAN_CAPABILITY_PRIVACY;
1324 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
1325 &ifibss->chandef, ifibss->basic_rates,
1326 capability, 0, true);
1329 static unsigned int ibss_setup_channels(struct wiphy *wiphy,
1330 struct ieee80211_channel **channels,
1331 unsigned int channels_max,
1332 u32 center_freq, u32 width)
1334 struct ieee80211_channel *chan = NULL;
1335 unsigned int n_chan = 0;
1336 u32 start_freq, end_freq, freq;
1338 if (width <= 20) {
1339 start_freq = center_freq;
1340 end_freq = center_freq;
1341 } else {
1342 start_freq = center_freq - width / 2 + 10;
1343 end_freq = center_freq + width / 2 - 10;
1346 for (freq = start_freq; freq <= end_freq; freq += 20) {
1347 chan = ieee80211_get_channel(wiphy, freq);
1348 if (!chan)
1349 continue;
1350 if (n_chan >= channels_max)
1351 return n_chan;
1353 channels[n_chan] = chan;
1354 n_chan++;
1357 return n_chan;
1360 static unsigned int
1361 ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1362 const struct cfg80211_chan_def *chandef,
1363 struct ieee80211_channel **channels,
1364 unsigned int channels_max)
1366 unsigned int n_chan = 0;
1367 u32 width, cf1, cf2 = 0;
1369 switch (chandef->width) {
1370 case NL80211_CHAN_WIDTH_40:
1371 width = 40;
1372 break;
1373 case NL80211_CHAN_WIDTH_80P80:
1374 cf2 = chandef->center_freq2;
1375 fallthrough;
1376 case NL80211_CHAN_WIDTH_80:
1377 width = 80;
1378 break;
1379 case NL80211_CHAN_WIDTH_160:
1380 width = 160;
1381 break;
1382 default:
1383 width = 20;
1384 break;
1387 cf1 = chandef->center_freq1;
1389 n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1391 if (cf2)
1392 n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1393 channels_max - n_chan, cf2,
1394 width);
1396 return n_chan;
1400 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1403 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1405 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1406 struct ieee80211_local *local = sdata->local;
1407 struct cfg80211_bss *cbss;
1408 struct ieee80211_channel *chan = NULL;
1409 const u8 *bssid = NULL;
1410 int active_ibss;
1412 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1414 active_ibss = ieee80211_sta_active_ibss(sdata);
1415 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
1417 if (active_ibss)
1418 return;
1420 if (ifibss->fixed_bssid)
1421 bssid = ifibss->bssid;
1422 if (ifibss->fixed_channel)
1423 chan = ifibss->chandef.chan;
1424 if (!is_zero_ether_addr(ifibss->bssid))
1425 bssid = ifibss->bssid;
1426 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1427 ifibss->ssid, ifibss->ssid_len,
1428 IEEE80211_BSS_TYPE_IBSS,
1429 IEEE80211_PRIVACY(ifibss->privacy));
1431 if (cbss) {
1432 struct ieee80211_bss *bss;
1434 bss = (void *)cbss->priv;
1435 ibss_dbg(sdata,
1436 "sta_find_ibss: selected %pM current %pM\n",
1437 cbss->bssid, ifibss->bssid);
1438 sdata_info(sdata,
1439 "Selected IBSS BSSID %pM based on configured SSID\n",
1440 cbss->bssid);
1442 ieee80211_sta_join_ibss(sdata, bss);
1443 ieee80211_rx_bss_put(local, bss);
1444 return;
1447 /* if a fixed bssid and a fixed freq have been provided create the IBSS
1448 * directly and do not waste time scanning
1450 if (ifibss->fixed_bssid && ifibss->fixed_channel) {
1451 sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
1452 bssid);
1453 ieee80211_sta_create_ibss(sdata);
1454 return;
1458 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
1460 /* Selected IBSS not found in current scan results - try to scan */
1461 if (time_after(jiffies, ifibss->last_scan_completed +
1462 IEEE80211_SCAN_INTERVAL)) {
1463 struct ieee80211_channel *channels[8];
1464 unsigned int num;
1466 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
1468 if (ifibss->fixed_channel) {
1469 num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1470 &ifibss->chandef,
1471 channels,
1472 ARRAY_SIZE(channels));
1473 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1474 ifibss->ssid_len, channels,
1475 num);
1476 } else {
1477 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1478 ifibss->ssid_len, NULL, 0);
1480 } else {
1481 int interval = IEEE80211_SCAN_INTERVAL;
1483 if (time_after(jiffies, ifibss->ibss_join_req +
1484 IEEE80211_IBSS_JOIN_TIMEOUT))
1485 ieee80211_sta_create_ibss(sdata);
1487 mod_timer(&ifibss->timer,
1488 round_jiffies(jiffies + interval));
1492 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1493 struct sk_buff *req)
1495 struct ieee80211_mgmt *mgmt = (void *)req->data;
1496 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1497 struct ieee80211_local *local = sdata->local;
1498 int tx_last_beacon, len = req->len;
1499 struct sk_buff *skb;
1500 struct beacon_data *presp;
1501 u8 *pos, *end;
1503 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1505 presp = sdata_dereference(ifibss->presp, sdata);
1507 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
1508 len < 24 + 2 || !presp)
1509 return;
1511 tx_last_beacon = drv_tx_last_beacon(local);
1513 ibss_dbg(sdata, "RX ProbeReq SA=%pM DA=%pM\n", mgmt->sa, mgmt->da);
1514 ibss_dbg(sdata, "\tBSSID=%pM (tx_last_beacon=%d)\n",
1515 mgmt->bssid, tx_last_beacon);
1517 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
1518 return;
1520 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
1521 !is_broadcast_ether_addr(mgmt->bssid))
1522 return;
1524 end = ((u8 *) mgmt) + len;
1525 pos = mgmt->u.probe_req.variable;
1526 if (pos[0] != WLAN_EID_SSID ||
1527 pos + 2 + pos[1] > end) {
1528 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1529 mgmt->sa);
1530 return;
1532 if (pos[1] != 0 &&
1533 (pos[1] != ifibss->ssid_len ||
1534 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
1535 /* Ignore ProbeReq for foreign SSID */
1536 return;
1539 /* Reply with ProbeResp */
1540 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
1541 if (!skb)
1542 return;
1544 skb_reserve(skb, local->tx_headroom);
1545 skb_put_data(skb, presp->head, presp->head_len);
1547 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1548 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
1549 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1551 /* avoid excessive retries for probe request to wildcard SSIDs */
1552 if (pos[1] == 0)
1553 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
1555 ieee80211_tx_skb(sdata, skb);
1558 static
1559 void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
1560 struct ieee80211_mgmt *mgmt, size_t len,
1561 struct ieee80211_rx_status *rx_status)
1563 size_t baselen;
1564 struct ieee802_11_elems *elems;
1566 BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
1567 offsetof(typeof(mgmt->u.beacon), variable));
1570 * either beacon or probe_resp but the variable field is at the
1571 * same offset
1573 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1574 if (baselen > len)
1575 return;
1577 elems = ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1578 len - baselen, false, NULL);
1580 if (elems) {
1581 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, elems);
1582 kfree(elems);
1586 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1587 struct sk_buff *skb)
1589 struct ieee80211_rx_status *rx_status;
1590 struct ieee80211_mgmt *mgmt;
1591 u16 fc;
1592 struct ieee802_11_elems *elems;
1593 int ies_len;
1595 rx_status = IEEE80211_SKB_RXCB(skb);
1596 mgmt = (struct ieee80211_mgmt *) skb->data;
1597 fc = le16_to_cpu(mgmt->frame_control);
1599 if (!sdata->u.ibss.ssid_len)
1600 return; /* not ready to merge yet */
1602 switch (fc & IEEE80211_FCTL_STYPE) {
1603 case IEEE80211_STYPE_PROBE_REQ:
1604 ieee80211_rx_mgmt_probe_req(sdata, skb);
1605 break;
1606 case IEEE80211_STYPE_PROBE_RESP:
1607 case IEEE80211_STYPE_BEACON:
1608 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1609 rx_status);
1610 break;
1611 case IEEE80211_STYPE_AUTH:
1612 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1613 break;
1614 case IEEE80211_STYPE_DEAUTH:
1615 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1616 break;
1617 case IEEE80211_STYPE_ACTION:
1618 switch (mgmt->u.action.category) {
1619 case WLAN_CATEGORY_SPECTRUM_MGMT:
1620 ies_len = skb->len -
1621 offsetof(struct ieee80211_mgmt,
1622 u.action.u.chan_switch.variable);
1624 if (ies_len < 0)
1625 break;
1627 elems = ieee802_11_parse_elems(
1628 mgmt->u.action.u.chan_switch.variable,
1629 ies_len, true, NULL);
1631 if (elems && !elems->parse_error)
1632 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt,
1633 skb->len,
1634 rx_status,
1635 elems);
1636 kfree(elems);
1637 break;
1642 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
1644 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1645 struct sta_info *sta;
1648 * Work could be scheduled after scan or similar
1649 * when we aren't even joined (or trying) with a
1650 * network.
1652 if (!ifibss->ssid_len)
1653 return;
1655 spin_lock_bh(&ifibss->incomplete_lock);
1656 while (!list_empty(&ifibss->incomplete_stations)) {
1657 sta = list_first_entry(&ifibss->incomplete_stations,
1658 struct sta_info, list);
1659 list_del(&sta->list);
1660 spin_unlock_bh(&ifibss->incomplete_lock);
1662 ieee80211_ibss_finish_sta(sta);
1663 rcu_read_unlock();
1664 spin_lock_bh(&ifibss->incomplete_lock);
1666 spin_unlock_bh(&ifibss->incomplete_lock);
1668 switch (ifibss->state) {
1669 case IEEE80211_IBSS_MLME_SEARCH:
1670 ieee80211_sta_find_ibss(sdata);
1671 break;
1672 case IEEE80211_IBSS_MLME_JOINED:
1673 ieee80211_sta_merge_ibss(sdata);
1674 break;
1675 default:
1676 WARN_ON(1);
1677 break;
1681 static void ieee80211_ibss_timer(struct timer_list *t)
1683 struct ieee80211_sub_if_data *sdata =
1684 from_timer(sdata, t, u.ibss.timer);
1686 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
1689 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1691 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1693 timer_setup(&ifibss->timer, ieee80211_ibss_timer, 0);
1694 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1695 spin_lock_init(&ifibss->incomplete_lock);
1696 wiphy_work_init(&ifibss->csa_connection_drop_work,
1697 ieee80211_csa_connection_drop_work);
1700 /* scan finished notification */
1701 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1703 struct ieee80211_sub_if_data *sdata;
1705 lockdep_assert_wiphy(local->hw.wiphy);
1707 list_for_each_entry(sdata, &local->interfaces, list) {
1708 if (!ieee80211_sdata_running(sdata))
1709 continue;
1710 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1711 continue;
1712 sdata->u.ibss.last_scan_completed = jiffies;
1716 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1717 struct cfg80211_ibss_params *params)
1719 u64 changed = 0;
1720 u32 rate_flags;
1721 struct ieee80211_supported_band *sband;
1722 enum ieee80211_chanctx_mode chanmode;
1723 struct ieee80211_local *local = sdata->local;
1724 int radar_detect_width = 0;
1725 int i;
1726 int ret;
1728 lockdep_assert_wiphy(local->hw.wiphy);
1730 if (params->chandef.chan->freq_offset) {
1731 /* this may work, but is untested */
1732 return -EOPNOTSUPP;
1735 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1736 &params->chandef,
1737 sdata->wdev.iftype);
1738 if (ret < 0)
1739 return ret;
1741 if (ret > 0) {
1742 if (!params->userspace_handles_dfs)
1743 return -EINVAL;
1744 radar_detect_width = BIT(params->chandef.width);
1747 chanmode = (params->channel_fixed && !ret) ?
1748 IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
1750 ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
1751 radar_detect_width, -1);
1752 if (ret < 0)
1753 return ret;
1755 if (params->bssid) {
1756 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1757 sdata->u.ibss.fixed_bssid = true;
1758 } else
1759 sdata->u.ibss.fixed_bssid = false;
1761 sdata->u.ibss.privacy = params->privacy;
1762 sdata->u.ibss.control_port = params->control_port;
1763 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
1764 sdata->u.ibss.basic_rates = params->basic_rates;
1765 sdata->u.ibss.last_scan_completed = jiffies;
1767 /* fix basic_rates if channel does not support these rates */
1768 rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
1769 sband = local->hw.wiphy->bands[params->chandef.chan->band];
1770 for (i = 0; i < sband->n_bitrates; i++) {
1771 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1772 sdata->u.ibss.basic_rates &= ~BIT(i);
1774 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1775 sizeof(params->mcast_rate));
1777 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1779 sdata->u.ibss.chandef = params->chandef;
1780 sdata->u.ibss.fixed_channel = params->channel_fixed;
1782 if (params->ie) {
1783 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1784 GFP_KERNEL);
1785 if (sdata->u.ibss.ie)
1786 sdata->u.ibss.ie_len = params->ie_len;
1789 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1790 sdata->u.ibss.ibss_join_req = jiffies;
1792 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
1793 sdata->u.ibss.ssid_len = params->ssid_len;
1795 memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1796 sizeof(sdata->u.ibss.ht_capa));
1797 memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1798 sizeof(sdata->u.ibss.ht_capa_mask));
1801 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1802 * reserved, but an HT STA shall protect HT transmissions as though
1803 * the HT Protection field were set to non-HT mixed mode.
1805 * In an IBSS, the RIFS Mode field of the HT Operation element is
1806 * also reserved, but an HT STA shall operate as though this field
1807 * were set to 1.
1810 sdata->vif.bss_conf.ht_operation_mode |=
1811 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1812 | IEEE80211_HT_PARAM_RIFS_MODE;
1814 changed |= BSS_CHANGED_HT | BSS_CHANGED_MCAST_RATE;
1815 ieee80211_link_info_change_notify(sdata, &sdata->deflink, changed);
1817 sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
1818 sdata->deflink.needed_rx_chains = local->rx_chains;
1819 sdata->control_port_over_nl80211 = params->control_port_over_nl80211;
1821 wiphy_work_queue(local->hw.wiphy, &sdata->work);
1823 return 0;
1826 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1828 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1830 ifibss->ssid_len = 0;
1831 ieee80211_ibss_disconnect(sdata);
1832 eth_zero_addr(ifibss->bssid);
1834 /* remove beacon */
1835 kfree(sdata->u.ibss.ie);
1836 sdata->u.ibss.ie = NULL;
1837 sdata->u.ibss.ie_len = 0;
1839 /* on the next join, re-program HT parameters */
1840 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1841 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1843 synchronize_rcu();
1845 skb_queue_purge(&sdata->skb_queue);
1847 del_timer_sync(&sdata->u.ibss.timer);
1849 return 0;