2 * IBSS mode implementation
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
9 * Copyright 2013-2014 Intel Mobile Communications GmbH
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/if_ether.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/etherdevice.h>
22 #include <linux/rtnetlink.h>
23 #include <net/mac80211.h>
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
29 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
30 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
32 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
33 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
34 #define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
36 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
38 static struct beacon_data *
39 ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
40 const int beacon_int, const u32 basic_rates,
41 const u16 capability, u64 tsf,
42 struct cfg80211_chan_def *chandef,
43 bool *have_higher_than_11mbit,
44 struct cfg80211_csa_settings *csa_settings)
46 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
47 struct ieee80211_local *local = sdata->local;
48 int rates_n = 0, i, ri;
49 struct ieee80211_mgmt *mgmt;
51 struct ieee80211_supported_band *sband;
52 u32 rate_flags, rates = 0, rates_added = 0;
53 struct beacon_data *presp;
57 /* Build IBSS probe response */
58 frame_len = sizeof(struct ieee80211_hdr_3addr) +
59 12 /* struct ieee80211_mgmt.u.beacon */ +
60 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
61 2 + 8 /* max Supported Rates */ +
62 3 /* max DS params */ +
64 5 /* Channel Switch Announcement */ +
65 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
66 2 + sizeof(struct ieee80211_ht_cap) +
67 2 + sizeof(struct ieee80211_ht_operation) +
69 presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
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 shift = ieee80211_chandef_get_shift(chandef);
96 if (have_higher_than_11mbit)
97 *have_higher_than_11mbit = false;
99 for (i = 0; i < sband->n_bitrates; i++) {
100 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
102 if (sband->bitrates[i].bitrate > 110 &&
103 have_higher_than_11mbit)
104 *have_higher_than_11mbit = true;
110 *pos++ = WLAN_EID_SUPP_RATES;
111 *pos++ = min_t(int, 8, rates_n);
112 for (ri = 0; ri < sband->n_bitrates; ri++) {
113 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
116 if (!(rates & BIT(ri)))
119 if (basic_rates & BIT(ri))
121 *pos++ = basic | (u8) rate;
122 if (++rates_added == 8) {
123 ri++; /* continue at next rate for EXT_SUPP_RATES */
128 if (sband->band == IEEE80211_BAND_2GHZ) {
129 *pos++ = WLAN_EID_DS_PARAMS;
131 *pos++ = ieee80211_frequency_to_channel(
132 chandef->chan->center_freq);
135 *pos++ = WLAN_EID_IBSS_PARAMS;
137 /* FIX: set ATIM window based on scan results */
142 *pos++ = WLAN_EID_CHANNEL_SWITCH;
144 *pos++ = csa_settings->block_tx ? 1 : 0;
145 *pos++ = ieee80211_frequency_to_channel(
146 csa_settings->chandef.chan->center_freq);
147 presp->csa_counter_offsets[0] = (pos - presp->head);
148 *pos++ = csa_settings->count;
151 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
153 *pos++ = WLAN_EID_EXT_SUPP_RATES;
154 *pos++ = rates_n - 8;
155 for (; ri < sband->n_bitrates; ri++) {
156 int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate,
159 if (!(rates & BIT(ri)))
162 if (basic_rates & BIT(ri))
164 *pos++ = basic | (u8) rate;
168 if (ifibss->ie_len) {
169 memcpy(pos, ifibss->ie, ifibss->ie_len);
170 pos += ifibss->ie_len;
173 /* add HT capability and information IEs */
174 if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
175 chandef->width != NL80211_CHAN_WIDTH_5 &&
176 chandef->width != NL80211_CHAN_WIDTH_10 &&
177 sband->ht_cap.ht_supported) {
178 struct ieee80211_sta_ht_cap ht_cap;
180 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
181 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
183 pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
185 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
186 * field and RIFS Mode are reserved in IBSS mode, therefore
189 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
192 /* add VHT capability and information IEs */
193 if (chandef->width != NL80211_CHAN_WIDTH_20 &&
194 chandef->width != NL80211_CHAN_WIDTH_40 &&
195 sband->vht_cap.vht_supported) {
196 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
198 pos = ieee80211_ie_build_vht_oper(pos, &sband->vht_cap,
203 if (local->hw.queues >= IEEE80211_NUM_ACS)
204 pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
206 presp->head_len = pos - presp->head;
207 if (WARN_ON(presp->head_len > frame_len))
216 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
217 const u8 *bssid, const int beacon_int,
218 struct cfg80211_chan_def *req_chandef,
219 const u32 basic_rates,
220 const u16 capability, u64 tsf,
223 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
224 struct ieee80211_local *local = sdata->local;
225 struct ieee80211_mgmt *mgmt;
226 struct cfg80211_bss *bss;
228 struct cfg80211_chan_def chandef;
229 struct ieee80211_channel *chan;
230 struct beacon_data *presp;
231 enum nl80211_bss_scan_width scan_width;
232 bool have_higher_than_11mbit;
236 sdata_assert_lock(sdata);
238 /* Reset own TSF to allow time synchronization work. */
239 drv_reset_tsf(local, sdata);
241 if (!ether_addr_equal(ifibss->bssid, bssid))
242 sta_info_flush(sdata);
244 /* if merging, indicate to driver that we leave the old IBSS */
245 if (sdata->vif.bss_conf.ibss_joined) {
246 sdata->vif.bss_conf.ibss_joined = false;
247 sdata->vif.bss_conf.ibss_creator = false;
248 sdata->vif.bss_conf.enable_beacon = false;
249 netif_carrier_off(sdata->dev);
250 ieee80211_bss_info_change_notify(sdata,
252 BSS_CHANGED_BEACON_ENABLED);
253 drv_leave_ibss(local, sdata);
256 presp = rcu_dereference_protected(ifibss->presp,
257 lockdep_is_held(&sdata->wdev.mtx));
258 RCU_INIT_POINTER(ifibss->presp, NULL);
260 kfree_rcu(presp, rcu_head);
262 /* make a copy of the chandef, it could be modified below. */
263 chandef = *req_chandef;
265 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
266 NL80211_IFTYPE_ADHOC)) {
267 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
268 chandef.width == NL80211_CHAN_WIDTH_10 ||
269 chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
270 chandef.width == NL80211_CHAN_WIDTH_20) {
272 "Failed to join IBSS, beacons forbidden\n");
275 chandef.width = NL80211_CHAN_WIDTH_20;
276 chandef.center_freq1 = chan->center_freq;
277 /* check again for downgraded chandef */
278 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
279 NL80211_IFTYPE_ADHOC)) {
281 "Failed to join IBSS, beacons forbidden\n");
286 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
287 &chandef, NL80211_IFTYPE_ADHOC);
290 "Failed to join IBSS, invalid chandef\n");
293 if (err > 0 && !ifibss->userspace_handles_dfs) {
295 "Failed to join IBSS, DFS channel without control program\n");
299 radar_required = err;
301 mutex_lock(&local->mtx);
302 if (ieee80211_vif_use_channel(sdata, &chandef,
303 ifibss->fixed_channel ?
304 IEEE80211_CHANCTX_SHARED :
305 IEEE80211_CHANCTX_EXCLUSIVE)) {
306 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
307 mutex_unlock(&local->mtx);
310 sdata->radar_required = radar_required;
311 mutex_unlock(&local->mtx);
313 memcpy(ifibss->bssid, bssid, ETH_ALEN);
315 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
316 capability, tsf, &chandef,
317 &have_higher_than_11mbit, NULL);
321 rcu_assign_pointer(ifibss->presp, presp);
322 mgmt = (void *)presp->head;
324 sdata->vif.bss_conf.enable_beacon = true;
325 sdata->vif.bss_conf.beacon_int = beacon_int;
326 sdata->vif.bss_conf.basic_rates = basic_rates;
327 sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
328 memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
329 bss_change = BSS_CHANGED_BEACON_INT;
330 bss_change |= ieee80211_reset_erp_info(sdata);
331 bss_change |= BSS_CHANGED_BSSID;
332 bss_change |= BSS_CHANGED_BEACON;
333 bss_change |= BSS_CHANGED_BEACON_ENABLED;
334 bss_change |= BSS_CHANGED_BASIC_RATES;
335 bss_change |= BSS_CHANGED_HT;
336 bss_change |= BSS_CHANGED_IBSS;
337 bss_change |= BSS_CHANGED_SSID;
340 * In 5 GHz/802.11a, we can always use short slot time.
341 * (IEEE 802.11-2012 18.3.8.7)
343 * In 2.4GHz, we must always use long slots in IBSS for compatibility
345 * (IEEE 802.11-2012 19.4.5)
347 * HT follows these specifications (IEEE 802.11-2012 20.3.18)
349 sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
350 bss_change |= BSS_CHANGED_ERP_SLOT;
352 /* cf. IEEE 802.11 9.2.12 */
353 if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit)
354 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
356 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
358 ieee80211_set_wmm_default(sdata, true);
360 sdata->vif.bss_conf.ibss_joined = true;
361 sdata->vif.bss_conf.ibss_creator = creator;
363 err = drv_join_ibss(local, sdata);
365 sdata->vif.bss_conf.ibss_joined = false;
366 sdata->vif.bss_conf.ibss_creator = false;
367 sdata->vif.bss_conf.enable_beacon = false;
368 sdata->vif.bss_conf.ssid_len = 0;
369 RCU_INIT_POINTER(ifibss->presp, NULL);
370 kfree_rcu(presp, rcu_head);
371 mutex_lock(&local->mtx);
372 ieee80211_vif_release_channel(sdata);
373 mutex_unlock(&local->mtx);
374 sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
379 ieee80211_bss_info_change_notify(sdata, bss_change);
381 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
382 mod_timer(&ifibss->timer,
383 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
385 scan_width = cfg80211_chandef_to_scan_width(&chandef);
386 bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan,
388 presp->head_len, 0, GFP_KERNEL);
389 cfg80211_put_bss(local->hw.wiphy, bss);
390 netif_carrier_on(sdata->dev);
391 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
394 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
395 struct ieee80211_bss *bss)
397 struct cfg80211_bss *cbss =
398 container_of((void *)bss, struct cfg80211_bss, priv);
399 struct ieee80211_supported_band *sband;
400 struct cfg80211_chan_def chandef;
403 u16 beacon_int = cbss->beacon_interval;
404 const struct cfg80211_bss_ies *ies;
405 enum nl80211_channel_type chan_type;
410 sdata_assert_lock(sdata);
415 switch (sdata->u.ibss.chandef.width) {
416 case NL80211_CHAN_WIDTH_20_NOHT:
417 case NL80211_CHAN_WIDTH_20:
418 case NL80211_CHAN_WIDTH_40:
419 chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
420 cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
422 case NL80211_CHAN_WIDTH_5:
423 case NL80211_CHAN_WIDTH_10:
424 cfg80211_chandef_create(&chandef, cbss->channel,
425 NL80211_CHAN_WIDTH_20_NOHT);
426 chandef.width = sdata->u.ibss.chandef.width;
428 case NL80211_CHAN_WIDTH_80:
429 case NL80211_CHAN_WIDTH_160:
430 chandef = sdata->u.ibss.chandef;
431 chandef.chan = cbss->channel;
434 /* fall back to 20 MHz for unsupported modes */
435 cfg80211_chandef_create(&chandef, cbss->channel,
436 NL80211_CHAN_WIDTH_20_NOHT);
440 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
441 rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
442 shift = ieee80211_vif_get_shift(&sdata->vif);
446 for (i = 0; i < bss->supp_rates_len; i++) {
447 int rate = bss->supp_rates[i] & 0x7f;
448 bool is_basic = !!(bss->supp_rates[i] & 0x80);
450 for (j = 0; j < sband->n_bitrates; j++) {
452 if ((rate_flags & sband->bitrates[j].flags)
456 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
460 basic_rates |= BIT(j);
467 ies = rcu_dereference(cbss->ies);
471 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
479 int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
480 struct cfg80211_csa_settings *csa_settings)
482 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
483 struct beacon_data *presp, *old_presp;
484 struct cfg80211_bss *cbss;
485 const struct cfg80211_bss_ies *ies;
490 sdata_assert_lock(sdata);
493 capability = WLAN_CAPABILITY_PRIVACY;
495 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
496 ifibss->bssid, ifibss->ssid,
497 ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
498 IEEE80211_PRIVACY(ifibss->privacy));
500 if (WARN_ON(!cbss)) {
506 ies = rcu_dereference(cbss->ies);
509 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
511 old_presp = rcu_dereference_protected(ifibss->presp,
512 lockdep_is_held(&sdata->wdev.mtx));
514 presp = ieee80211_ibss_build_presp(sdata,
515 sdata->vif.bss_conf.beacon_int,
516 sdata->vif.bss_conf.basic_rates,
517 capability, tsf, &ifibss->chandef,
524 rcu_assign_pointer(ifibss->presp, presp);
526 kfree_rcu(old_presp, rcu_head);
528 return BSS_CHANGED_BEACON;
533 int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
535 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
536 struct cfg80211_bss *cbss;
537 int err, changed = 0;
539 sdata_assert_lock(sdata);
541 /* update cfg80211 bss information with the new channel */
542 if (!is_zero_ether_addr(ifibss->bssid)) {
543 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
544 ifibss->chandef.chan,
545 ifibss->bssid, ifibss->ssid,
547 IEEE80211_BSS_TYPE_IBSS,
548 IEEE80211_PRIVACY(ifibss->privacy));
549 /* XXX: should not really modify cfg80211 data */
551 cbss->channel = sdata->csa_chandef.chan;
552 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
556 ifibss->chandef = sdata->csa_chandef;
558 /* generate the beacon */
559 err = ieee80211_ibss_csa_beacon(sdata, NULL);
568 void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
570 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
572 cancel_work_sync(&ifibss->csa_connection_drop_work);
575 static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
578 struct ieee80211_sub_if_data *sdata = sta->sdata;
581 memcpy(addr, sta->sta.addr, ETH_ALEN);
583 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
585 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
586 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
587 /* authorize the station only if the network is not RSN protected. If
588 * not wait for the userspace to authorize it */
589 if (!sta->sdata->u.ibss.control_port)
590 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
592 rate_control_rate_init(sta);
594 /* If it fails, maybe we raced another insertion? */
595 if (sta_info_insert_rcu(sta))
596 return sta_info_get(sdata, addr);
600 static struct sta_info *
601 ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
602 const u8 *addr, u32 supp_rates)
605 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
606 struct ieee80211_local *local = sdata->local;
607 struct sta_info *sta;
608 struct ieee80211_chanctx_conf *chanctx_conf;
609 struct ieee80211_supported_band *sband;
610 enum nl80211_bss_scan_width scan_width;
614 * XXX: Consider removing the least recently used entry and
615 * allow new one to be added.
617 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
618 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
624 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
629 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
635 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
636 if (WARN_ON_ONCE(!chanctx_conf))
638 band = chanctx_conf->def.chan->band;
639 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
642 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
648 sta->last_rx = jiffies;
650 /* make sure mandatory rates are always added */
651 sband = local->hw.wiphy->bands[band];
652 sta->sta.supp_rates[band] = supp_rates |
653 ieee80211_mandatory_rates(sband, scan_width);
655 return ieee80211_ibss_finish_sta(sta);
658 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
660 struct ieee80211_local *local = sdata->local;
662 struct sta_info *sta;
664 sdata_assert_lock(sdata);
668 list_for_each_entry_rcu(sta, &local->sta_list, list) {
669 if (sta->sdata == sdata &&
670 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
682 static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
684 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
685 struct ieee80211_local *local = sdata->local;
686 struct cfg80211_bss *cbss;
687 struct beacon_data *presp;
688 struct sta_info *sta;
690 if (!is_zero_ether_addr(ifibss->bssid)) {
691 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
692 ifibss->bssid, ifibss->ssid,
694 IEEE80211_BSS_TYPE_IBSS,
695 IEEE80211_PRIVACY(ifibss->privacy));
698 cfg80211_unlink_bss(local->hw.wiphy, cbss);
699 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
703 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
705 sta_info_flush(sdata);
707 spin_lock_bh(&ifibss->incomplete_lock);
708 while (!list_empty(&ifibss->incomplete_stations)) {
709 sta = list_first_entry(&ifibss->incomplete_stations,
710 struct sta_info, list);
711 list_del(&sta->list);
712 spin_unlock_bh(&ifibss->incomplete_lock);
714 sta_info_free(local, sta);
715 spin_lock_bh(&ifibss->incomplete_lock);
717 spin_unlock_bh(&ifibss->incomplete_lock);
719 netif_carrier_off(sdata->dev);
721 sdata->vif.bss_conf.ibss_joined = false;
722 sdata->vif.bss_conf.ibss_creator = false;
723 sdata->vif.bss_conf.enable_beacon = false;
724 sdata->vif.bss_conf.ssid_len = 0;
727 presp = rcu_dereference_protected(ifibss->presp,
728 lockdep_is_held(&sdata->wdev.mtx));
729 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
731 kfree_rcu(presp, rcu_head);
733 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
734 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
736 drv_leave_ibss(local, sdata);
737 mutex_lock(&local->mtx);
738 ieee80211_vif_release_channel(sdata);
739 mutex_unlock(&local->mtx);
742 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
744 struct ieee80211_sub_if_data *sdata =
745 container_of(work, struct ieee80211_sub_if_data,
746 u.ibss.csa_connection_drop_work);
750 ieee80211_ibss_disconnect(sdata);
752 skb_queue_purge(&sdata->skb_queue);
754 /* trigger a scan to find another IBSS network to join */
755 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
760 static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
762 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
765 /* if the current channel is a DFS channel, mark the channel as
768 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
770 NL80211_IFTYPE_ADHOC);
772 cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
777 ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
778 struct ieee802_11_elems *elems,
781 struct cfg80211_csa_settings params;
782 struct ieee80211_csa_ie csa_ie;
783 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
784 enum nl80211_channel_type ch_type;
788 sdata_assert_lock(sdata);
790 sta_flags = IEEE80211_STA_DISABLE_VHT;
791 switch (ifibss->chandef.width) {
792 case NL80211_CHAN_WIDTH_5:
793 case NL80211_CHAN_WIDTH_10:
794 case NL80211_CHAN_WIDTH_20_NOHT:
795 sta_flags |= IEEE80211_STA_DISABLE_HT;
797 case NL80211_CHAN_WIDTH_20:
798 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
804 memset(¶ms, 0, sizeof(params));
805 memset(&csa_ie, 0, sizeof(csa_ie));
806 err = ieee80211_parse_ch_switch_ie(sdata, elems,
807 ifibss->chandef.chan->band,
808 sta_flags, ifibss->bssid, &csa_ie);
809 /* can't switch to destination channel, fail */
813 /* did not contain a CSA */
817 /* channel switch is not supported, disconnect */
818 if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
821 params.count = csa_ie.count;
822 params.chandef = csa_ie.chandef;
824 switch (ifibss->chandef.width) {
825 case NL80211_CHAN_WIDTH_20_NOHT:
826 case NL80211_CHAN_WIDTH_20:
827 case NL80211_CHAN_WIDTH_40:
828 /* keep our current HT mode (HT20/HT40+/HT40-), even if
829 * another mode has been announced. The mode is not adopted
830 * within the beacon while doing CSA and we should therefore
831 * keep the mode which we announce.
833 ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
834 cfg80211_chandef_create(¶ms.chandef, params.chandef.chan,
837 case NL80211_CHAN_WIDTH_5:
838 case NL80211_CHAN_WIDTH_10:
839 if (params.chandef.width != ifibss->chandef.width) {
841 "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
843 params.chandef.chan->center_freq,
844 params.chandef.width,
845 params.chandef.center_freq1,
846 params.chandef.center_freq2);
851 /* should not happen, sta_flags should prevent VHT modes. */
856 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, ¶ms.chandef,
857 NL80211_IFTYPE_ADHOC)) {
859 "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
861 params.chandef.chan->center_freq,
862 params.chandef.width,
863 params.chandef.center_freq1,
864 params.chandef.center_freq2);
868 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
870 NL80211_IFTYPE_ADHOC);
873 if (err > 0 && !ifibss->userspace_handles_dfs) {
874 /* IBSS-DFS only allowed with a control program */
878 params.radar_required = err;
880 if (cfg80211_chandef_identical(¶ms.chandef,
881 &sdata->vif.bss_conf.chandef)) {
883 "received csa with an identical chandef, ignoring\n");
887 /* all checks done, now perform the channel switch. */
889 "received channel switch announcement to go to channel %d MHz\n",
890 params.chandef.chan->center_freq);
892 params.block_tx = !!csa_ie.mode;
894 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
898 ieee80211_ibss_csa_mark_radar(sdata);
902 ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
903 ieee80211_queue_work(&sdata->local->hw,
904 &ifibss->csa_connection_drop_work);
906 ieee80211_ibss_csa_mark_radar(sdata);
912 ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
913 struct ieee80211_mgmt *mgmt, size_t len,
914 struct ieee80211_rx_status *rx_status,
915 struct ieee802_11_elems *elems)
919 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
922 /* CSA is the only action we handle for now */
923 if (mgmt->u.action.u.measurement.action_code !=
924 WLAN_ACTION_SPCT_CHL_SWITCH)
927 required_len = IEEE80211_MIN_ACTION_SIZE +
928 sizeof(mgmt->u.action.u.chan_switch);
929 if (len < required_len)
932 if (!sdata->vif.csa_active)
933 ieee80211_ibss_process_chanswitch(sdata, elems, false);
936 static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
937 struct ieee80211_mgmt *mgmt,
940 u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
942 if (len < IEEE80211_DEAUTH_FRAME_LEN)
945 ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n",
946 mgmt->sa, mgmt->da, mgmt->bssid, reason);
947 sta_info_destroy_addr(sdata, mgmt->sa);
950 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
951 struct ieee80211_mgmt *mgmt,
954 u16 auth_alg, auth_transaction;
956 sdata_assert_lock(sdata);
961 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
962 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
965 "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
966 mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
968 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
972 * IEEE 802.11 standard does not require authentication in IBSS
973 * networks and most implementations do not seem to use it.
974 * However, try to reply to authentication attempts if someone
975 * has actually implemented this.
977 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
978 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
981 static void ieee80211_update_sta_info(struct ieee80211_sub_if_data *sdata,
982 struct ieee80211_mgmt *mgmt, size_t len,
983 struct ieee80211_rx_status *rx_status,
984 struct ieee802_11_elems *elems,
985 struct ieee80211_channel *channel)
987 struct sta_info *sta;
988 enum ieee80211_band band = rx_status->band;
989 enum nl80211_bss_scan_width scan_width;
990 struct ieee80211_local *local = sdata->local;
991 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
992 bool rates_updated = false;
995 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
998 if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
1002 sta = sta_info_get(sdata, mgmt->sa);
1004 if (elems->supp_rates) {
1005 supp_rates = ieee80211_sta_get_rates(sdata, elems,
1010 prev_rates = sta->sta.supp_rates[band];
1011 /* make sure mandatory rates are always added */
1012 scan_width = NL80211_BSS_CHAN_WIDTH_20;
1013 if (rx_status->flag & RX_FLAG_5MHZ)
1014 scan_width = NL80211_BSS_CHAN_WIDTH_5;
1015 if (rx_status->flag & RX_FLAG_10MHZ)
1016 scan_width = NL80211_BSS_CHAN_WIDTH_10;
1018 sta->sta.supp_rates[band] = supp_rates |
1019 ieee80211_mandatory_rates(sband, scan_width);
1020 if (sta->sta.supp_rates[band] != prev_rates) {
1022 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
1023 sta->sta.addr, prev_rates,
1024 sta->sta.supp_rates[band]);
1025 rates_updated = true;
1029 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1030 mgmt->sa, supp_rates);
1034 if (sta && elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS)
1035 sta->sta.wme = true;
1037 if (sta && elems->ht_operation && elems->ht_cap_elem &&
1038 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
1039 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
1040 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
1041 /* we both use HT */
1042 struct ieee80211_ht_cap htcap_ie;
1043 struct cfg80211_chan_def chandef;
1044 enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
1046 ieee80211_ht_oper_to_chandef(channel,
1047 elems->ht_operation,
1050 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
1051 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1055 if (elems->vht_operation && elems->vht_cap_elem &&
1056 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
1057 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
1058 /* we both use VHT */
1059 struct ieee80211_vht_cap cap_ie;
1060 struct ieee80211_sta_vht_cap cap = sta->sta.vht_cap;
1062 ieee80211_vht_oper_to_chandef(channel,
1063 elems->vht_operation,
1065 memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1066 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1068 if (memcmp(&cap, &sta->sta.vht_cap, sizeof(cap)))
1069 rates_updated |= true;
1072 if (bw != sta->sta.bandwidth)
1073 rates_updated |= true;
1075 if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1080 if (sta && rates_updated) {
1081 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1082 u8 rx_nss = sta->sta.rx_nss;
1084 /* Force rx_nss recalculation */
1085 sta->sta.rx_nss = 0;
1086 rate_control_rate_init(sta);
1087 if (sta->sta.rx_nss != rx_nss)
1088 changed |= IEEE80211_RC_NSS_CHANGED;
1090 drv_sta_rc_update(local, sdata, &sta->sta, changed);
1096 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1097 struct ieee80211_mgmt *mgmt, size_t len,
1098 struct ieee80211_rx_status *rx_status,
1099 struct ieee802_11_elems *elems)
1101 struct ieee80211_local *local = sdata->local;
1102 struct cfg80211_bss *cbss;
1103 struct ieee80211_bss *bss;
1104 struct ieee80211_channel *channel;
1105 u64 beacon_timestamp, rx_timestamp;
1107 enum ieee80211_band band = rx_status->band;
1109 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1113 ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
1115 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1120 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1122 /* same for beacon and probe response */
1123 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1125 /* check if we need to merge IBSS */
1128 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
1131 /* different channel */
1132 if (sdata->u.ibss.fixed_channel &&
1133 sdata->u.ibss.chandef.chan != cbss->channel)
1136 /* different SSID */
1137 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
1138 memcmp(elems->ssid, sdata->u.ibss.ssid,
1139 sdata->u.ibss.ssid_len))
1142 /* process channel switch */
1143 if (sdata->vif.csa_active ||
1144 ieee80211_ibss_process_chanswitch(sdata, elems, true))
1148 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
1151 /* we use a fixed BSSID */
1152 if (sdata->u.ibss.fixed_bssid)
1155 if (ieee80211_have_rx_timestamp(rx_status)) {
1156 /* time when timestamp field was received */
1158 ieee80211_calculate_rx_timestamp(local, rx_status,
1162 * second best option: get current TSF
1163 * (will return -1 if not supported)
1165 rx_timestamp = drv_get_tsf(local, sdata);
1169 "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1170 mgmt->sa, mgmt->bssid,
1171 (unsigned long long)rx_timestamp,
1172 (unsigned long long)beacon_timestamp,
1173 (unsigned long long)(rx_timestamp - beacon_timestamp),
1176 if (beacon_timestamp > rx_timestamp) {
1178 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1180 ieee80211_sta_join_ibss(sdata, bss);
1181 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
1182 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
1188 ieee80211_rx_bss_put(local, bss);
1191 void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1192 const u8 *bssid, const u8 *addr,
1195 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1196 struct ieee80211_local *local = sdata->local;
1197 struct sta_info *sta;
1198 struct ieee80211_chanctx_conf *chanctx_conf;
1199 struct ieee80211_supported_band *sband;
1200 enum nl80211_bss_scan_width scan_width;
1204 * XXX: Consider removing the least recently used entry and
1205 * allow new one to be added.
1207 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
1208 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
1213 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
1216 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
1220 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1221 if (WARN_ON_ONCE(!chanctx_conf)) {
1225 band = chanctx_conf->def.chan->band;
1226 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
1229 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
1233 sta->last_rx = jiffies;
1235 /* make sure mandatory rates are always added */
1236 sband = local->hw.wiphy->bands[band];
1237 sta->sta.supp_rates[band] = supp_rates |
1238 ieee80211_mandatory_rates(sband, scan_width);
1240 spin_lock(&ifibss->incomplete_lock);
1241 list_add(&sta->list, &ifibss->incomplete_stations);
1242 spin_unlock(&ifibss->incomplete_lock);
1243 ieee80211_queue_work(&local->hw, &sdata->work);
1246 static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1248 struct ieee80211_local *local = sdata->local;
1249 struct sta_info *sta, *tmp;
1250 unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
1251 unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
1253 mutex_lock(&local->sta_mtx);
1255 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1256 if (sdata != sta->sdata)
1259 if (time_after(jiffies, sta->last_rx + exp_time) ||
1260 (time_after(jiffies, sta->last_rx + exp_rsn_time) &&
1261 sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
1262 sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
1263 sta->sta_state != IEEE80211_STA_AUTHORIZED ?
1264 "not authorized " : "", sta->sta.addr);
1266 WARN_ON(__sta_info_destroy(sta));
1270 mutex_unlock(&local->sta_mtx);
1274 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1277 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1279 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1280 enum nl80211_bss_scan_width scan_width;
1282 sdata_assert_lock(sdata);
1284 mod_timer(&ifibss->timer,
1285 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
1287 ieee80211_ibss_sta_expire(sdata);
1289 if (time_before(jiffies, ifibss->last_scan_completed +
1290 IEEE80211_IBSS_MERGE_INTERVAL))
1293 if (ieee80211_sta_active_ibss(sdata))
1296 if (ifibss->fixed_channel)
1300 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
1302 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
1303 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
1304 NULL, 0, scan_width);
1307 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
1309 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1314 sdata_assert_lock(sdata);
1316 if (ifibss->fixed_bssid) {
1317 memcpy(bssid, ifibss->bssid, ETH_ALEN);
1319 /* Generate random, not broadcast, locally administered BSSID. Mix in
1320 * own MAC address to make sure that devices that do not have proper
1321 * random number generator get different BSSID. */
1322 get_random_bytes(bssid, ETH_ALEN);
1323 for (i = 0; i < ETH_ALEN; i++)
1324 bssid[i] ^= sdata->vif.addr[i];
1329 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
1331 capability = WLAN_CAPABILITY_IBSS;
1333 if (ifibss->privacy)
1334 capability |= WLAN_CAPABILITY_PRIVACY;
1336 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
1337 &ifibss->chandef, ifibss->basic_rates,
1338 capability, 0, true);
1341 static unsigned ibss_setup_channels(struct wiphy *wiphy,
1342 struct ieee80211_channel **channels,
1343 unsigned int channels_max,
1344 u32 center_freq, u32 width)
1346 struct ieee80211_channel *chan = NULL;
1347 unsigned int n_chan = 0;
1348 u32 start_freq, end_freq, freq;
1351 start_freq = center_freq;
1352 end_freq = center_freq;
1354 start_freq = center_freq - width / 2 + 10;
1355 end_freq = center_freq + width / 2 - 10;
1358 for (freq = start_freq; freq <= end_freq; freq += 20) {
1359 chan = ieee80211_get_channel(wiphy, freq);
1362 if (n_chan >= channels_max)
1365 channels[n_chan] = chan;
1373 ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1374 const struct cfg80211_chan_def *chandef,
1375 struct ieee80211_channel **channels,
1376 unsigned int channels_max)
1378 unsigned int n_chan = 0;
1379 u32 width, cf1, cf2 = 0;
1381 switch (chandef->width) {
1382 case NL80211_CHAN_WIDTH_40:
1385 case NL80211_CHAN_WIDTH_80P80:
1386 cf2 = chandef->center_freq2;
1388 case NL80211_CHAN_WIDTH_80:
1391 case NL80211_CHAN_WIDTH_160:
1399 cf1 = chandef->center_freq1;
1401 n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1404 n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1405 channels_max - n_chan, cf2,
1412 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1415 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1417 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1418 struct ieee80211_local *local = sdata->local;
1419 struct cfg80211_bss *cbss;
1420 struct ieee80211_channel *chan = NULL;
1421 const u8 *bssid = NULL;
1422 enum nl80211_bss_scan_width scan_width;
1425 sdata_assert_lock(sdata);
1427 active_ibss = ieee80211_sta_active_ibss(sdata);
1428 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
1433 if (ifibss->fixed_bssid)
1434 bssid = ifibss->bssid;
1435 if (ifibss->fixed_channel)
1436 chan = ifibss->chandef.chan;
1437 if (!is_zero_ether_addr(ifibss->bssid))
1438 bssid = ifibss->bssid;
1439 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1440 ifibss->ssid, ifibss->ssid_len,
1441 IEEE80211_BSS_TYPE_IBSS,
1442 IEEE80211_PRIVACY(ifibss->privacy));
1445 struct ieee80211_bss *bss;
1447 bss = (void *)cbss->priv;
1449 "sta_find_ibss: selected %pM current %pM\n",
1450 cbss->bssid, ifibss->bssid);
1452 "Selected IBSS BSSID %pM based on configured SSID\n",
1455 ieee80211_sta_join_ibss(sdata, bss);
1456 ieee80211_rx_bss_put(local, bss);
1460 /* if a fixed bssid and a fixed freq have been provided create the IBSS
1461 * directly and do not waste time scanning
1463 if (ifibss->fixed_bssid && ifibss->fixed_channel) {
1464 sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
1466 ieee80211_sta_create_ibss(sdata);
1471 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
1473 /* Selected IBSS not found in current scan results - try to scan */
1474 if (time_after(jiffies, ifibss->last_scan_completed +
1475 IEEE80211_SCAN_INTERVAL)) {
1476 struct ieee80211_channel *channels[8];
1479 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
1481 num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1484 ARRAY_SIZE(channels));
1485 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
1486 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1487 ifibss->ssid_len, channels, num,
1490 int interval = IEEE80211_SCAN_INTERVAL;
1492 if (time_after(jiffies, ifibss->ibss_join_req +
1493 IEEE80211_IBSS_JOIN_TIMEOUT))
1494 ieee80211_sta_create_ibss(sdata);
1496 mod_timer(&ifibss->timer,
1497 round_jiffies(jiffies + interval));
1501 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
1502 struct sk_buff *req)
1504 struct ieee80211_mgmt *mgmt = (void *)req->data;
1505 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1506 struct ieee80211_local *local = sdata->local;
1507 int tx_last_beacon, len = req->len;
1508 struct sk_buff *skb;
1509 struct beacon_data *presp;
1512 sdata_assert_lock(sdata);
1514 presp = rcu_dereference_protected(ifibss->presp,
1515 lockdep_is_held(&sdata->wdev.mtx));
1517 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
1518 len < 24 + 2 || !presp)
1521 tx_last_beacon = drv_tx_last_beacon(local);
1524 "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
1525 mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
1527 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
1530 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
1531 !is_broadcast_ether_addr(mgmt->bssid))
1534 end = ((u8 *) mgmt) + len;
1535 pos = mgmt->u.probe_req.variable;
1536 if (pos[0] != WLAN_EID_SSID ||
1537 pos + 2 + pos[1] > end) {
1538 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1543 (pos[1] != ifibss->ssid_len ||
1544 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
1545 /* Ignore ProbeReq for foreign SSID */
1549 /* Reply with ProbeResp */
1550 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
1554 skb_reserve(skb, local->tx_headroom);
1555 memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
1557 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1558 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
1559 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1561 /* avoid excessive retries for probe request to wildcard SSIDs */
1563 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
1565 ieee80211_tx_skb(sdata, skb);
1569 void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
1570 struct ieee80211_mgmt *mgmt, size_t len,
1571 struct ieee80211_rx_status *rx_status)
1574 struct ieee802_11_elems elems;
1576 BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
1577 offsetof(typeof(mgmt->u.beacon), variable));
1580 * either beacon or probe_resp but the variable field is at the
1583 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1587 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1590 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
1593 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1594 struct sk_buff *skb)
1596 struct ieee80211_rx_status *rx_status;
1597 struct ieee80211_mgmt *mgmt;
1599 struct ieee802_11_elems elems;
1602 rx_status = IEEE80211_SKB_RXCB(skb);
1603 mgmt = (struct ieee80211_mgmt *) skb->data;
1604 fc = le16_to_cpu(mgmt->frame_control);
1608 if (!sdata->u.ibss.ssid_len)
1609 goto mgmt_out; /* not ready to merge yet */
1611 switch (fc & IEEE80211_FCTL_STYPE) {
1612 case IEEE80211_STYPE_PROBE_REQ:
1613 ieee80211_rx_mgmt_probe_req(sdata, skb);
1615 case IEEE80211_STYPE_PROBE_RESP:
1616 case IEEE80211_STYPE_BEACON:
1617 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1620 case IEEE80211_STYPE_AUTH:
1621 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1623 case IEEE80211_STYPE_DEAUTH:
1624 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1626 case IEEE80211_STYPE_ACTION:
1627 switch (mgmt->u.action.category) {
1628 case WLAN_CATEGORY_SPECTRUM_MGMT:
1629 ies_len = skb->len -
1630 offsetof(struct ieee80211_mgmt,
1631 u.action.u.chan_switch.variable);
1636 ieee802_11_parse_elems(
1637 mgmt->u.action.u.chan_switch.variable,
1638 ies_len, true, &elems);
1640 if (elems.parse_error)
1643 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
1650 sdata_unlock(sdata);
1653 void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
1655 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1656 struct sta_info *sta;
1661 * Work could be scheduled after scan or similar
1662 * when we aren't even joined (or trying) with a
1665 if (!ifibss->ssid_len)
1668 spin_lock_bh(&ifibss->incomplete_lock);
1669 while (!list_empty(&ifibss->incomplete_stations)) {
1670 sta = list_first_entry(&ifibss->incomplete_stations,
1671 struct sta_info, list);
1672 list_del(&sta->list);
1673 spin_unlock_bh(&ifibss->incomplete_lock);
1675 ieee80211_ibss_finish_sta(sta);
1677 spin_lock_bh(&ifibss->incomplete_lock);
1679 spin_unlock_bh(&ifibss->incomplete_lock);
1681 switch (ifibss->state) {
1682 case IEEE80211_IBSS_MLME_SEARCH:
1683 ieee80211_sta_find_ibss(sdata);
1685 case IEEE80211_IBSS_MLME_JOINED:
1686 ieee80211_sta_merge_ibss(sdata);
1694 sdata_unlock(sdata);
1697 static void ieee80211_ibss_timer(unsigned long data)
1699 struct ieee80211_sub_if_data *sdata =
1700 (struct ieee80211_sub_if_data *) data;
1702 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1705 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1707 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1709 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1710 (unsigned long) sdata);
1711 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1712 spin_lock_init(&ifibss->incomplete_lock);
1713 INIT_WORK(&ifibss->csa_connection_drop_work,
1714 ieee80211_csa_connection_drop_work);
1717 /* scan finished notification */
1718 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1720 struct ieee80211_sub_if_data *sdata;
1722 mutex_lock(&local->iflist_mtx);
1723 list_for_each_entry(sdata, &local->interfaces, list) {
1724 if (!ieee80211_sdata_running(sdata))
1726 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1728 sdata->u.ibss.last_scan_completed = jiffies;
1729 ieee80211_queue_work(&local->hw, &sdata->work);
1731 mutex_unlock(&local->iflist_mtx);
1734 int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1735 struct cfg80211_ibss_params *params)
1739 struct ieee80211_supported_band *sband;
1740 enum ieee80211_chanctx_mode chanmode;
1741 struct ieee80211_local *local = sdata->local;
1742 int radar_detect_width = 0;
1746 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1748 sdata->wdev.iftype);
1753 if (!params->userspace_handles_dfs)
1755 radar_detect_width = BIT(params->chandef.width);
1758 chanmode = (params->channel_fixed && !ret) ?
1759 IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
1761 mutex_lock(&local->chanctx_mtx);
1762 ret = ieee80211_check_combinations(sdata, ¶ms->chandef, chanmode,
1763 radar_detect_width);
1764 mutex_unlock(&local->chanctx_mtx);
1768 if (params->bssid) {
1769 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1770 sdata->u.ibss.fixed_bssid = true;
1772 sdata->u.ibss.fixed_bssid = false;
1774 sdata->u.ibss.privacy = params->privacy;
1775 sdata->u.ibss.control_port = params->control_port;
1776 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
1777 sdata->u.ibss.basic_rates = params->basic_rates;
1778 sdata->u.ibss.last_scan_completed = jiffies;
1780 /* fix basic_rates if channel does not support these rates */
1781 rate_flags = ieee80211_chandef_rate_flags(¶ms->chandef);
1782 sband = local->hw.wiphy->bands[params->chandef.chan->band];
1783 for (i = 0; i < sband->n_bitrates; i++) {
1784 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1785 sdata->u.ibss.basic_rates &= ~BIT(i);
1787 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1788 sizeof(params->mcast_rate));
1790 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1792 sdata->u.ibss.chandef = params->chandef;
1793 sdata->u.ibss.fixed_channel = params->channel_fixed;
1796 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1798 if (sdata->u.ibss.ie)
1799 sdata->u.ibss.ie_len = params->ie_len;
1802 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1803 sdata->u.ibss.ibss_join_req = jiffies;
1805 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
1806 sdata->u.ibss.ssid_len = params->ssid_len;
1808 memcpy(&sdata->u.ibss.ht_capa, ¶ms->ht_capa,
1809 sizeof(sdata->u.ibss.ht_capa));
1810 memcpy(&sdata->u.ibss.ht_capa_mask, ¶ms->ht_capa_mask,
1811 sizeof(sdata->u.ibss.ht_capa_mask));
1814 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1815 * reserved, but an HT STA shall protect HT transmissions as though
1816 * the HT Protection field were set to non-HT mixed mode.
1818 * In an IBSS, the RIFS Mode field of the HT Operation element is
1819 * also reserved, but an HT STA shall operate as though this field
1823 sdata->vif.bss_conf.ht_operation_mode |=
1824 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1825 | IEEE80211_HT_PARAM_RIFS_MODE;
1827 changed |= BSS_CHANGED_HT;
1828 ieee80211_bss_info_change_notify(sdata, changed);
1830 sdata->smps_mode = IEEE80211_SMPS_OFF;
1831 sdata->needed_rx_chains = local->rx_chains;
1833 ieee80211_queue_work(&local->hw, &sdata->work);
1838 int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1840 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1842 ieee80211_ibss_disconnect(sdata);
1843 ifibss->ssid_len = 0;
1844 eth_zero_addr(ifibss->bssid);
1847 kfree(sdata->u.ibss.ie);
1849 /* on the next join, re-program HT parameters */
1850 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1851 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1855 skb_queue_purge(&sdata->skb_queue);
1857 del_timer_sync(&sdata->u.ibss.timer);