blob: bfef1b2150504fa9ed2a0b4b9a7b4b60e229f1de [file] [log] [blame]
Johannes Berg46900292009-02-15 12:44:28 +01001/*
2 * IBSS mode implementation
3 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
Johannes Bergd98ad832014-09-03 15:24:57 +03009 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg46900292009-02-15 12:44:28 +010010 *
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.
14 */
15
16#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Johannes Berg46900292009-02-15 12:44:28 +010018#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>
Johannes Berg46900292009-02-15 12:44:28 +010024
25#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020026#include "driver-ops.h"
Johannes Berg46900292009-02-15 12:44:28 +010027#include "rate.h"
28
29#define IEEE80211_SCAN_INTERVAL (2 * HZ)
Johannes Berg46900292009-02-15 12:44:28 +010030#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
31
32#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
33#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Antonio Quartullidad9def2013-07-26 21:15:02 +020034#define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
Johannes Berg46900292009-02-15 12:44:28 +010035
36#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37
Simon Wunderlichd51b70f2013-08-09 16:35:17 +020038static struct beacon_data *
39ieee80211_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,
Simon Wunderlichcd7760e2013-08-28 13:41:31 +020043 bool *have_higher_than_11mbit,
44 struct cfg80211_csa_settings *csa_settings)
Simon Wunderlichd51b70f2013-08-09 16:35:17 +020045{
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;
50 u8 *pos;
51 struct ieee80211_supported_band *sband;
52 u32 rate_flags, rates = 0, rates_added = 0;
53 struct beacon_data *presp;
54 int frame_len;
55 int shift;
56
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 */ +
63 4 /* IBSS params */ +
Simon Wunderlichcd7760e2013-08-28 13:41:31 +020064 5 /* Channel Switch Announcement */ +
Simon Wunderlichd51b70f2013-08-09 16:35:17 +020065 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
66 2 + sizeof(struct ieee80211_ht_cap) +
67 2 + sizeof(struct ieee80211_ht_operation) +
68 ifibss->ie_len;
69 presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL);
70 if (!presp)
71 return NULL;
72
73 presp->head = (void *)(presp + 1);
74
75 mgmt = (void *) presp->head;
76 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
77 IEEE80211_STYPE_PROBE_RESP);
78 eth_broadcast_addr(mgmt->da);
79 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
80 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
81 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
82 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
83 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
84
85 pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable);
86
87 *pos++ = WLAN_EID_SSID;
88 *pos++ = ifibss->ssid_len;
89 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
90 pos += ifibss->ssid_len;
91
92 sband = local->hw.wiphy->bands[chandef->chan->band];
93 rate_flags = ieee80211_chandef_rate_flags(chandef);
94 shift = ieee80211_chandef_get_shift(chandef);
95 rates_n = 0;
96 if (have_higher_than_11mbit)
97 *have_higher_than_11mbit = false;
98
99 for (i = 0; i < sband->n_bitrates; i++) {
100 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
101 continue;
102 if (sband->bitrates[i].bitrate > 110 &&
103 have_higher_than_11mbit)
104 *have_higher_than_11mbit = true;
105
106 rates |= BIT(i);
107 rates_n++;
108 }
109
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,
114 5 * (1 << shift));
115 u8 basic = 0;
116 if (!(rates & BIT(ri)))
117 continue;
118
119 if (basic_rates & BIT(ri))
120 basic = 0x80;
121 *pos++ = basic | (u8) rate;
122 if (++rates_added == 8) {
123 ri++; /* continue at next rate for EXT_SUPP_RATES */
124 break;
125 }
126 }
127
128 if (sband->band == IEEE80211_BAND_2GHZ) {
129 *pos++ = WLAN_EID_DS_PARAMS;
130 *pos++ = 1;
131 *pos++ = ieee80211_frequency_to_channel(
132 chandef->chan->center_freq);
133 }
134
135 *pos++ = WLAN_EID_IBSS_PARAMS;
136 *pos++ = 2;
137 /* FIX: set ATIM window based on scan results */
138 *pos++ = 0;
139 *pos++ = 0;
140
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200141 if (csa_settings) {
142 *pos++ = WLAN_EID_CHANNEL_SWITCH;
143 *pos++ = 3;
144 *pos++ = csa_settings->block_tx ? 1 : 0;
145 *pos++ = ieee80211_frequency_to_channel(
146 csa_settings->chandef.chan->center_freq);
Michal Kazioraf296bd2014-06-05 14:21:36 +0200147 presp->csa_counter_offsets[0] = (pos - presp->head);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200148 *pos++ = csa_settings->count;
149 }
150
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200151 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
152 if (rates_n > 8) {
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,
157 5 * (1 << shift));
158 u8 basic = 0;
159 if (!(rates & BIT(ri)))
160 continue;
161
162 if (basic_rates & BIT(ri))
163 basic = 0x80;
164 *pos++ = basic | (u8) rate;
165 }
166 }
167
168 if (ifibss->ie_len) {
169 memcpy(pos, ifibss->ie, ifibss->ie_len);
170 pos += ifibss->ie_len;
171 }
172
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;
179
180 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
181 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
182
183 pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
184 /*
185 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
186 * field and RIFS Mode are reserved in IBSS mode, therefore
187 * keep them at 0
188 */
189 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
190 chandef, 0);
Janusz.Dziedzic@tieto.comabcff6e2015-03-20 06:37:01 +0100191
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,
197 sband->vht_cap.cap);
198 pos = ieee80211_ie_build_vht_oper(pos, &sband->vht_cap,
199 chandef);
200 }
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200201 }
202
Arik Nemtsov40b861a2014-07-17 17:14:23 +0300203 if (local->hw.queues >= IEEE80211_NUM_ACS)
204 pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200205
206 presp->head_len = pos - presp->head;
207 if (WARN_ON(presp->head_len > frame_len))
208 goto error;
209
210 return presp;
211error:
212 kfree(presp);
213 return NULL;
214}
Johannes Berg46900292009-02-15 12:44:28 +0100215
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200216static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
217 const u8 *bssid, const int beacon_int,
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200218 struct cfg80211_chan_def *req_chandef,
Johannes Bergb59066a2009-05-12 21:18:38 +0200219 const u32 basic_rates,
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530220 const u16 capability, u64 tsf,
221 bool creator)
Johannes Berg46900292009-02-15 12:44:28 +0100222{
223 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
224 struct ieee80211_local *local = sdata->local;
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200225 struct ieee80211_mgmt *mgmt;
Johannes Bergf446d102009-10-28 15:12:32 +0100226 struct cfg80211_bss *bss;
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200227 u32 bss_change;
Johannes Berg683b6d32012-11-08 21:25:48 +0100228 struct cfg80211_chan_def chandef;
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200229 struct ieee80211_channel *chan;
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100230 struct beacon_data *presp;
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200231 enum nl80211_bss_scan_width scan_width;
232 bool have_higher_than_11mbit;
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200233 bool radar_required;
Johannes Berg55fff502013-08-19 18:48:41 +0200234 int err;
Johannes Berg24487982009-04-23 18:52:52 +0200235
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200236 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200237
Johannes Berg24487982009-04-23 18:52:52 +0200238 /* Reset own TSF to allow time synchronization work. */
Eliad Peller37a41b42011-09-21 14:06:11 +0300239 drv_reset_tsf(local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +0100240
Joe Perchesb203ca32012-05-08 18:56:52 +0000241 if (!ether_addr_equal(ifibss->bssid, bssid))
Johannes Bergb998e8b2012-12-13 23:07:46 +0100242 sta_info_flush(sdata);
Johannes Berg46900292009-02-15 12:44:28 +0100243
Johannes Berg49b5c7f2010-04-29 21:34:01 +0200244 /* 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;
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530247 sdata->vif.bss_conf.ibss_creator = false;
Johannes Berg1852d402013-03-07 20:22:28 +0100248 sdata->vif.bss_conf.enable_beacon = false;
Eliad Peller86a2ea42011-11-08 15:36:59 +0200249 netif_carrier_off(sdata->dev);
Johannes Berg1852d402013-03-07 20:22:28 +0100250 ieee80211_bss_info_change_notify(sdata,
251 BSS_CHANGED_IBSS |
252 BSS_CHANGED_BEACON_ENABLED);
Johannes Berg55fff502013-08-19 18:48:41 +0200253 drv_leave_ibss(local, sdata);
Johannes Berg49b5c7f2010-04-29 21:34:01 +0200254 }
255
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100256 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200257 lockdep_is_held(&sdata->wdev.mtx));
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530258 RCU_INIT_POINTER(ifibss->presp, NULL);
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100259 if (presp)
260 kfree_rcu(presp, rcu_head);
Johannes Berg1852d402013-03-07 20:22:28 +0100261
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200262 /* make a copy of the chandef, it could be modified below. */
263 chandef = *req_chandef;
264 chan = chandef.chan;
Ilan Peer174e0cd2014-02-23 09:13:01 +0200265 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
266 NL80211_IFTYPE_ADHOC)) {
Simon Wunderlich4b42aab2013-07-08 16:55:57 +0200267 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) {
271 sdata_info(sdata,
272 "Failed to join IBSS, beacons forbidden\n");
273 return;
274 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100275 chandef.width = NL80211_CHAN_WIDTH_20;
276 chandef.center_freq1 = chan->center_freq;
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200277 /* check again for downgraded chandef */
Ilan Peer174e0cd2014-02-23 09:13:01 +0200278 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
279 NL80211_IFTYPE_ADHOC)) {
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200280 sdata_info(sdata,
281 "Failed to join IBSS, beacons forbidden\n");
282 return;
283 }
284 }
285
286 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200287 &chandef, NL80211_IFTYPE_ADHOC);
Luciano Coelho6658ab82014-02-20 16:36:23 +0200288 if (err < 0) {
289 sdata_info(sdata,
290 "Failed to join IBSS, invalid chandef\n");
291 return;
292 }
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200293 if (err > 0 && !ifibss->userspace_handles_dfs) {
294 sdata_info(sdata,
295 "Failed to join IBSS, DFS channel without control program\n");
296 return;
Johannes Berg4bf88532012-11-09 11:39:59 +0100297 }
Johannes Berg55de9082012-07-26 17:24:39 +0200298
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200299 radar_required = err;
300
Johannes Berg34a37402013-12-18 09:43:33 +0100301 mutex_lock(&local->mtx);
Johannes Berg4bf88532012-11-09 11:39:59 +0100302 if (ieee80211_vif_use_channel(sdata, &chandef,
Johannes Berg55de9082012-07-26 17:24:39 +0200303 ifibss->fixed_channel ?
304 IEEE80211_CHANCTX_SHARED :
305 IEEE80211_CHANCTX_EXCLUSIVE)) {
306 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
Johannes Berg34a37402013-12-18 09:43:33 +0100307 mutex_unlock(&local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +0200308 return;
Alexander Simon13c40c52011-11-30 16:56:34 +0100309 }
Michal Kaziorcc901de2014-01-29 07:56:20 +0100310 sdata->radar_required = radar_required;
Johannes Berg34a37402013-12-18 09:43:33 +0100311 mutex_unlock(&local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +0200312
313 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200314
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200315 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
316 capability, tsf, &chandef,
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200317 &have_higher_than_11mbit, NULL);
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100318 if (!presp)
319 return;
320
Johannes Bergc3ffeab2013-03-07 20:54:29 +0100321 rcu_assign_pointer(ifibss->presp, presp);
Simon Wunderlichd51b70f2013-08-09 16:35:17 +0200322 mgmt = (void *)presp->head;
Johannes Berg46900292009-02-15 12:44:28 +0100323
Johannes Bergd6a83222012-12-14 14:06:28 +0100324 sdata->vif.bss_conf.enable_beacon = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200325 sdata->vif.bss_conf.beacon_int = beacon_int;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300326 sdata->vif.bss_conf.basic_rates = basic_rates;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +0200327 sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
328 memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200329 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;
Teemu Paasikivi392cfdb2010-06-14 12:55:32 +0300334 bss_change |= BSS_CHANGED_BASIC_RATES;
Alexander Simon13c40c52011-11-30 16:56:34 +0100335 bss_change |= BSS_CHANGED_HT;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200336 bss_change |= BSS_CHANGED_IBSS;
Marek Puzyniak0ca54f62013-04-10 13:19:13 +0200337 bss_change |= BSS_CHANGED_SSID;
Simon Wunderlich2f91a962012-12-03 22:21:30 +0100338
339 /*
340 * In 5 GHz/802.11a, we can always use short slot time.
341 * (IEEE 802.11-2012 18.3.8.7)
342 *
343 * In 2.4GHz, we must always use long slots in IBSS for compatibility
344 * reasons.
345 * (IEEE 802.11-2012 19.4.5)
346 *
347 * HT follows these specifications (IEEE 802.11-2012 20.3.18)
348 */
349 sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ;
350 bss_change |= BSS_CHANGED_ERP_SLOT;
351
Simon Wunderlich2ec9c1f2013-07-11 18:07:46 +0200352 /* 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;
355 else
356 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
357
Johannes Berg55fff502013-08-19 18:48:41 +0200358 ieee80211_set_wmm_default(sdata, true);
359
Johannes Berg8fc214b2010-04-28 17:40:43 +0200360 sdata->vif.bss_conf.ibss_joined = true;
Sujith Manoharanc13a7652012-10-12 17:35:45 +0530361 sdata->vif.bss_conf.ibss_creator = creator;
Johannes Berg46900292009-02-15 12:44:28 +0100362
Johannes Berg55fff502013-08-19 18:48:41 +0200363 err = drv_join_ibss(local, sdata);
364 if (err) {
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);
Johannes Berg34a37402013-12-18 09:43:33 +0100371 mutex_lock(&local->mtx);
Johannes Berg55fff502013-08-19 18:48:41 +0200372 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +0100373 mutex_unlock(&local->mtx);
Johannes Berg55fff502013-08-19 18:48:41 +0200374 sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
375 err);
376 return;
377 }
378
379 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100380
Johannes Berg46900292009-02-15 12:44:28 +0100381 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200382 mod_timer(&ifibss->timer,
383 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100384
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200385 scan_width = cfg80211_chandef_to_scan_width(&chandef);
386 bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan,
387 scan_width, mgmt,
388 presp->head_len, 0, GFP_KERNEL);
Johannes Berg5b112d32013-02-01 01:49:58 +0100389 cfg80211_put_bss(local->hw.wiphy, bss);
Eliad Peller86a2ea42011-11-08 15:36:59 +0200390 netif_carrier_on(sdata->dev);
Antonio Quartullife94f3a2014-01-29 17:53:43 +0100391 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100392}
393
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200394static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
395 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100396{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100397 struct cfg80211_bss *cbss =
398 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200399 struct ieee80211_supported_band *sband;
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200400 struct cfg80211_chan_def chandef;
Johannes Bergb59066a2009-05-12 21:18:38 +0200401 u32 basic_rates;
402 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100403 u16 beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100404 const struct cfg80211_bss_ies *ies;
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200405 enum nl80211_channel_type chan_type;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100406 u64 tsf;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200407 u32 rate_flags;
408 int shift;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200409
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200410 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200411
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200412 if (beacon_int < 10)
413 beacon_int = 10;
414
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200415 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);
421 break;
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;
427 break;
Janusz.Dziedzic@tieto.comabcff6e2015-03-20 06:37:01 +0100428 case NL80211_CHAN_WIDTH_80:
429 case NL80211_CHAN_WIDTH_160:
430 chandef = sdata->u.ibss.chandef;
431 chandef.chan = cbss->channel;
432 break;
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200433 default:
434 /* fall back to 20 MHz for unsupported modes */
435 cfg80211_chandef_create(&chandef, cbss->channel,
436 NL80211_CHAN_WIDTH_20_NOHT);
437 break;
438 }
439
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100440 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200441 rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
442 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Bergb59066a2009-05-12 21:18:38 +0200443
444 basic_rates = 0;
445
446 for (i = 0; i < bss->supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200447 int rate = bss->supp_rates[i] & 0x7f;
Johannes Bergb59066a2009-05-12 21:18:38 +0200448 bool is_basic = !!(bss->supp_rates[i] & 0x80);
449
450 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200451 int brate;
452 if ((rate_flags & sband->bitrates[j].flags)
453 != rate_flags)
454 continue;
455
456 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
457 5 * (1 << shift));
458 if (brate == rate) {
Johannes Bergb59066a2009-05-12 21:18:38 +0200459 if (is_basic)
460 basic_rates |= BIT(j);
461 break;
462 }
463 }
464 }
465
Johannes Berg8cef2c92013-02-05 16:54:31 +0100466 rcu_read_lock();
467 ies = rcu_dereference(cbss->ies);
468 tsf = ies->tsf;
469 rcu_read_unlock();
470
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100471 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200472 beacon_int,
Simon Wunderlich75a423f2013-08-21 15:30:25 +0200473 &chandef,
Johannes Bergb59066a2009-05-12 21:18:38 +0200474 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100475 cbss->capability,
Johannes Berg8cef2c92013-02-05 16:54:31 +0100476 tsf, false);
Johannes Berg46900292009-02-15 12:44:28 +0100477}
478
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200479int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
480 struct cfg80211_csa_settings *csa_settings)
481{
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;
Dedy Lansky6eb18132015-02-08 15:52:03 +0200486 u16 capability = 0;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200487 u64 tsf;
488 int ret = 0;
489
490 sdata_assert_lock(sdata);
491
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200492 if (ifibss->privacy)
Dedy Lansky6eb18132015-02-08 15:52:03 +0200493 capability = WLAN_CAPABILITY_PRIVACY;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200494
495 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
496 ifibss->bssid, ifibss->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200497 ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
498 IEEE80211_PRIVACY(ifibss->privacy));
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200499
500 if (WARN_ON(!cbss)) {
501 ret = -EINVAL;
502 goto out;
503 }
504
505 rcu_read_lock();
506 ies = rcu_dereference(cbss->ies);
507 tsf = ies->tsf;
508 rcu_read_unlock();
509 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
510
511 old_presp = rcu_dereference_protected(ifibss->presp,
512 lockdep_is_held(&sdata->wdev.mtx));
513
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,
518 NULL, csa_settings);
519 if (!presp) {
520 ret = -ENOMEM;
521 goto out;
522 }
523
524 rcu_assign_pointer(ifibss->presp, presp);
525 if (old_presp)
526 kfree_rcu(old_presp, rcu_head);
527
Luciano Coelhodddd5862013-12-13 21:13:51 +0200528 return BSS_CHANGED_BEACON;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200529 out:
530 return ret;
531}
532
533int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
534{
535 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
536 struct cfg80211_bss *cbss;
Michal Kaziorfaf046e2014-01-29 07:56:17 +0100537 int err, changed = 0;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200538
Simon Wunderlichbafdc612013-12-05 19:54:43 +0100539 sdata_assert_lock(sdata);
540
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200541 /* update cfg80211 bss information with the new channel */
542 if (!is_zero_ether_addr(ifibss->bssid)) {
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200543 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
544 ifibss->chandef.chan,
545 ifibss->bssid, ifibss->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200546 ifibss->ssid_len,
547 IEEE80211_BSS_TYPE_IBSS,
548 IEEE80211_PRIVACY(ifibss->privacy));
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200549 /* XXX: should not really modify cfg80211 data */
550 if (cbss) {
Luciano Coelho33787fc2013-11-11 20:34:54 +0200551 cbss->channel = sdata->csa_chandef.chan;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200552 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
553 }
554 }
555
Luciano Coelho33787fc2013-11-11 20:34:54 +0200556 ifibss->chandef = sdata->csa_chandef;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200557
558 /* generate the beacon */
559 err = ieee80211_ibss_csa_beacon(sdata, NULL);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200560 if (err < 0)
561 return err;
562
Michal Kaziorfaf046e2014-01-29 07:56:17 +0100563 changed |= err;
Luciano Coelhodddd5862013-12-13 21:13:51 +0200564
Michal Kaziorfaf046e2014-01-29 07:56:17 +0100565 return changed;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200566}
567
568void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
569{
570 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
571
572 cancel_work_sync(&ifibss->csa_connection_drop_work);
573}
574
Antonio Quartulli52874a52013-06-18 14:20:40 +0200575static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100576 __acquires(RCU)
577{
578 struct ieee80211_sub_if_data *sdata = sta->sdata;
579 u8 addr[ETH_ALEN];
580
581 memcpy(addr, sta->sta.addr, ETH_ALEN);
582
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200583 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100584
Johannes Berg83d5cc02012-01-12 09:31:10 +0100585 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
586 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
Antonio Quartulli267335d2012-01-31 20:25:47 +0100587 /* 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);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100591
592 rate_control_rate_init(sta);
593
594 /* If it fails, maybe we raced another insertion? */
595 if (sta_info_insert_rcu(sta))
596 return sta_info_get(sdata, addr);
597 return sta;
598}
599
600static struct sta_info *
Antonio Quartulli52874a52013-06-18 14:20:40 +0200601ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
602 const u8 *addr, u32 supp_rates)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100603 __acquires(RCU)
604{
605 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
606 struct ieee80211_local *local = sdata->local;
607 struct sta_info *sta;
Johannes Berg55de9082012-07-26 17:24:39 +0200608 struct ieee80211_chanctx_conf *chanctx_conf;
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700609 struct ieee80211_supported_band *sband;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200610 enum nl80211_bss_scan_width scan_width;
Johannes Berg55de9082012-07-26 17:24:39 +0200611 int band;
Johannes Berg8bf11d82011-12-15 11:17:37 +0100612
613 /*
614 * XXX: Consider removing the least recently used entry and
615 * allow new one to be added.
616 */
617 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200618 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
Joe Perchese87cc472012-05-13 21:56:26 +0000619 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100620 rcu_read_lock();
621 return NULL;
622 }
623
624 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
625 rcu_read_lock();
626 return NULL;
627 }
628
Joe Perchesb203ca32012-05-08 18:56:52 +0000629 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
Johannes Berg8bf11d82011-12-15 11:17:37 +0100630 rcu_read_lock();
631 return NULL;
632 }
633
Johannes Berg55de9082012-07-26 17:24:39 +0200634 rcu_read_lock();
635 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
636 if (WARN_ON_ONCE(!chanctx_conf))
637 return NULL;
Johannes Berg4bf88532012-11-09 11:39:59 +0100638 band = chanctx_conf->def.chan->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200639 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200640 rcu_read_unlock();
641
Johannes Berg8bf11d82011-12-15 11:17:37 +0100642 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
643 if (!sta) {
644 rcu_read_lock();
645 return NULL;
646 }
647
648 sta->last_rx = jiffies;
649
650 /* make sure mandatory rates are always added */
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -0700651 sband = local->hw.wiphy->bands[band];
Johannes Berg8bf11d82011-12-15 11:17:37 +0100652 sta->sta.supp_rates[band] = supp_rates |
Simon Wunderlich74608ac2013-07-08 16:55:54 +0200653 ieee80211_mandatory_rates(sband, scan_width);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100654
Antonio Quartulli52874a52013-06-18 14:20:40 +0200655 return ieee80211_ibss_finish_sta(sta);
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100656}
657
Simon Wunderlich871a4182013-08-28 13:41:30 +0200658static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
659{
660 struct ieee80211_local *local = sdata->local;
661 int active = 0;
662 struct sta_info *sta;
663
664 sdata_assert_lock(sdata);
665
666 rcu_read_lock();
667
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,
671 jiffies)) {
672 active++;
673 break;
674 }
675 }
676
677 rcu_read_unlock();
678
679 return active;
680}
681
682static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
683{
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;
Simon Wunderlich871a4182013-08-28 13:41:30 +0200689
Sujith Manoharand4c80d92014-01-30 14:17:28 +0530690 if (!is_zero_ether_addr(ifibss->bssid)) {
Simon Wunderlich871a4182013-08-28 13:41:30 +0200691 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
692 ifibss->bssid, ifibss->ssid,
Dedy Lansky6eb18132015-02-08 15:52:03 +0200693 ifibss->ssid_len,
694 IEEE80211_BSS_TYPE_IBSS,
695 IEEE80211_PRIVACY(ifibss->privacy));
Simon Wunderlich871a4182013-08-28 13:41:30 +0200696
697 if (cbss) {
698 cfg80211_unlink_bss(local->hw.wiphy, cbss);
699 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
700 }
701 }
702
703 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
704
705 sta_info_flush(sdata);
706
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);
713
714 sta_info_free(local, sta);
715 spin_lock_bh(&ifibss->incomplete_lock);
716 }
717 spin_unlock_bh(&ifibss->incomplete_lock);
718
719 netif_carrier_off(sdata->dev);
720
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;
725
726 /* remove beacon */
727 presp = rcu_dereference_protected(ifibss->presp,
728 lockdep_is_held(&sdata->wdev.mtx));
729 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
730 if (presp)
731 kfree_rcu(presp, rcu_head);
732
733 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
734 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
735 BSS_CHANGED_IBSS);
Johannes Berg55fff502013-08-19 18:48:41 +0200736 drv_leave_ibss(local, sdata);
Johannes Berg34a37402013-12-18 09:43:33 +0100737 mutex_lock(&local->mtx);
Simon Wunderlich871a4182013-08-28 13:41:30 +0200738 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +0100739 mutex_unlock(&local->mtx);
Simon Wunderlich871a4182013-08-28 13:41:30 +0200740}
741
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200742static void ieee80211_csa_connection_drop_work(struct work_struct *work)
743{
744 struct ieee80211_sub_if_data *sdata =
745 container_of(work, struct ieee80211_sub_if_data,
746 u.ibss.csa_connection_drop_work);
747
Luciano Coelho6f07d212013-12-11 20:39:46 +0200748 sdata_lock(sdata);
749
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200750 ieee80211_ibss_disconnect(sdata);
751 synchronize_rcu();
752 skb_queue_purge(&sdata->skb_queue);
753
754 /* trigger a scan to find another IBSS network to join */
755 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Luciano Coelho6f07d212013-12-11 20:39:46 +0200756
757 sdata_unlock(sdata);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200758}
759
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200760static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
761{
762 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
763 int err;
764
765 /* if the current channel is a DFS channel, mark the channel as
766 * unavailable.
767 */
768 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200769 &ifibss->chandef,
770 NL80211_IFTYPE_ADHOC);
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200771 if (err > 0)
772 cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
773 GFP_ATOMIC);
774}
775
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200776static bool
777ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
778 struct ieee802_11_elems *elems,
779 bool beacon)
780{
781 struct cfg80211_csa_settings params;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700782 struct ieee80211_csa_ie csa_ie;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200783 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200784 enum nl80211_channel_type ch_type;
Luciano Coelho82a8e172013-12-13 21:13:50 +0200785 int err;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200786 u32 sta_flags;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200787
Michal Kaziordbd72852014-01-29 07:56:21 +0100788 sdata_assert_lock(sdata);
789
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200790 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;
796 /* fall through */
797 case NL80211_CHAN_WIDTH_20:
798 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
799 break;
800 default:
801 break;
802 }
803
804 memset(&params, 0, sizeof(params));
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700805 memset(&csa_ie, 0, sizeof(csa_ie));
Luciano Coelho84469a42014-10-28 13:33:04 +0200806 err = ieee80211_parse_ch_switch_ie(sdata, elems,
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200807 ifibss->chandef.chan->band,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700808 sta_flags, ifibss->bssid, &csa_ie);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200809 /* can't switch to destination channel, fail */
810 if (err < 0)
811 goto disconnect;
812
813 /* did not contain a CSA */
814 if (err)
815 return false;
816
Simon Wunderlich0834ae32013-11-26 16:45:18 +0100817 /* channel switch is not supported, disconnect */
818 if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
819 goto disconnect;
820
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700821 params.count = csa_ie.count;
822 params.chandef = csa_ie.chandef;
823
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200824 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.
832 */
833 ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
834 cfg80211_chandef_create(&params.chandef, params.chandef.chan,
835 ch_type);
836 break;
837 case NL80211_CHAN_WIDTH_5:
838 case NL80211_CHAN_WIDTH_10:
839 if (params.chandef.width != ifibss->chandef.width) {
840 sdata_info(sdata,
841 "IBSS %pM received channel switch from incompatible channel width (%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;
848 }
849 break;
850 default:
851 /* should not happen, sta_flags should prevent VHT modes. */
852 WARN_ON(1);
853 goto disconnect;
854 }
855
Ilan Peer174e0cd2014-02-23 09:13:01 +0200856 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
857 NL80211_IFTYPE_ADHOC)) {
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200858 sdata_info(sdata,
859 "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
860 ifibss->bssid,
861 params.chandef.chan->center_freq,
862 params.chandef.width,
863 params.chandef.center_freq1,
864 params.chandef.center_freq2);
865 goto disconnect;
866 }
867
868 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200869 &params.chandef,
870 NL80211_IFTYPE_ADHOC);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200871 if (err < 0)
872 goto disconnect;
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200873 if (err > 0 && !ifibss->userspace_handles_dfs) {
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200874 /* IBSS-DFS only allowed with a control program */
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200875 goto disconnect;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200876 }
877
Luciano Coelho2beb6dab2014-02-18 11:40:36 +0200878 params.radar_required = err;
879
Luciano Coelho82a8e172013-12-13 21:13:50 +0200880 if (cfg80211_chandef_identical(&params.chandef,
881 &sdata->vif.bss_conf.chandef)) {
882 ibss_dbg(sdata,
883 "received csa with an identical chandef, ignoring\n");
884 return true;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200885 }
886
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200887 /* all checks done, now perform the channel switch. */
888 ibss_dbg(sdata,
889 "received channel switch announcement to go to channel %d MHz\n",
890 params.chandef.chan->center_freq);
891
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700892 params.block_tx = !!csa_ie.mode;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200893
Luciano Coelho82a8e172013-12-13 21:13:50 +0200894 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
895 &params))
896 goto disconnect;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200897
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200898 ieee80211_ibss_csa_mark_radar(sdata);
899
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200900 return true;
901disconnect:
902 ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
903 ieee80211_queue_work(&sdata->local->hw,
904 &ifibss->csa_connection_drop_work);
905
Simon Wunderlich8e8d3472013-10-07 18:41:06 +0200906 ieee80211_ibss_csa_mark_radar(sdata);
907
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200908 return true;
909}
910
911static void
912ieee80211_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)
916{
917 int required_len;
918
919 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
920 return;
921
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)
925 return;
926
927 required_len = IEEE80211_MIN_ACTION_SIZE +
928 sizeof(mgmt->u.action.u.chan_switch);
929 if (len < required_len)
930 return;
931
Luciano Coelho82a8e172013-12-13 21:13:50 +0200932 if (!sdata->vif.csa_active)
933 ieee80211_ibss_process_chanswitch(sdata, elems, false);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +0200934}
935
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200936static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
937 struct ieee80211_mgmt *mgmt,
938 size_t len)
939{
940 u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code);
941
942 if (len < IEEE80211_DEAUTH_FRAME_LEN)
943 return;
944
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);
948}
949
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100950static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
951 struct ieee80211_mgmt *mgmt,
952 size_t len)
953{
954 u16 auth_alg, auth_transaction;
955
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200956 sdata_assert_lock(sdata);
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100957
958 if (len < 24 + 6)
959 return;
960
961 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
962 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
963
Johannes Bergbdcbd8e2012-06-22 11:29:50 +0200964 ibss_dbg(sdata,
965 "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
966 mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction);
Antonio Quartulli7bed2052012-11-25 23:24:27 +0100967
968 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
969 return;
970
Antonio Quartulli2cc59e72012-09-07 13:28:53 +0200971 /*
Antonio Quartulli6d810f12012-01-18 00:10:44 +0100972 * 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.
976 */
Jouni Malinen700e8ea2012-09-30 19:29:37 +0300977 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
Johannes Berg1672c0e32013-01-29 15:02:27 +0100978 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100979}
980
Janusz.Dziedzic@tieto.com8a4988d2015-03-09 07:58:15 +0100981static 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)
986{
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;
993 u32 supp_rates = 0;
994
995 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
996 return;
997
998 if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
999 return;
1000
1001 rcu_read_lock();
1002 sta = sta_info_get(sdata, mgmt->sa);
1003
1004 if (elems->supp_rates) {
1005 supp_rates = ieee80211_sta_get_rates(sdata, elems,
1006 band, NULL);
1007 if (sta) {
1008 u32 prev_rates;
1009
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;
1017
1018 sta->sta.supp_rates[band] = supp_rates |
1019 ieee80211_mandatory_rates(sband, scan_width);
1020 if (sta->sta.supp_rates[band] != prev_rates) {
1021 ibss_dbg(sdata,
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;
1026 }
1027 } else {
1028 rcu_read_unlock();
1029 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1030 mgmt->sa, supp_rates);
1031 }
1032 }
1033
Johannes Berg527871d2015-03-21 08:09:55 +01001034 if (sta && elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS)
Janusz.Dziedzic@tieto.com8a4988d2015-03-09 07:58:15 +01001035 sta->sta.wme = true;
1036
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;
Janusz.Dziedzic@tieto.comabcff6e2015-03-20 06:37:01 +01001044 enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
Janusz.Dziedzic@tieto.com8a4988d2015-03-09 07:58:15 +01001045
1046 ieee80211_ht_oper_to_chandef(channel,
1047 elems->ht_operation,
1048 &chandef);
1049
1050 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
Janusz.Dziedzic@tieto.com8a4988d2015-03-09 07:58:15 +01001051 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1052 &htcap_ie,
1053 sta);
Janusz.Dziedzic@tieto.comabcff6e2015-03-20 06:37:01 +01001054
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;
1061
1062 ieee80211_vht_oper_to_chandef(channel,
1063 elems->vht_operation,
1064 &chandef);
1065 memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1066 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1067 &cap_ie, sta);
1068 if (memcmp(&cap, &sta->sta.vht_cap, sizeof(cap)))
1069 rates_updated |= true;
1070 }
1071
1072 if (bw != sta->sta.bandwidth)
1073 rates_updated |= true;
1074
1075 if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1076 &chandef))
1077 WARN_ON_ONCE(1);
Janusz.Dziedzic@tieto.com8a4988d2015-03-09 07:58:15 +01001078 }
1079
1080 if (sta && rates_updated) {
1081 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1082 u8 rx_nss = sta->sta.rx_nss;
1083
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;
1089
1090 drv_sta_rc_update(local, sdata, &sta->sta, changed);
1091 }
1092
1093 rcu_read_unlock();
1094}
1095
Johannes Berg46900292009-02-15 12:44:28 +01001096static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001097 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg46900292009-02-15 12:44:28 +01001098 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001099 struct ieee802_11_elems *elems)
Johannes Berg46900292009-02-15 12:44:28 +01001100{
1101 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001102 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +01001103 struct ieee80211_bss *bss;
Johannes Berg46900292009-02-15 12:44:28 +01001104 struct ieee80211_channel *channel;
1105 u64 beacon_timestamp, rx_timestamp;
1106 u32 supp_rates = 0;
1107 enum ieee80211_band band = rx_status->band;
1108
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001109 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1110 if (!channel)
Johannes Berg46900292009-02-15 12:44:28 +01001111 return;
1112
Janusz.Dziedzic@tieto.com8a4988d2015-03-09 07:58:15 +01001113 ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
Johannes Berg46900292009-02-15 12:44:28 +01001114
1115 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001116 channel);
Johannes Berg46900292009-02-15 12:44:28 +01001117 if (!bss)
1118 return;
1119
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001120 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1121
Johannes Berg8cef2c92013-02-05 16:54:31 +01001122 /* same for beacon and probe response */
1123 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Johannes Berg46900292009-02-15 12:44:28 +01001124
1125 /* check if we need to merge IBSS */
1126
Johannes Berg46900292009-02-15 12:44:28 +01001127 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001128 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +01001129 goto put_bss;
1130
1131 /* different channel */
Johannes Berg55de9082012-07-26 17:24:39 +02001132 if (sdata->u.ibss.fixed_channel &&
Simon Wunderlich3aede782013-05-16 13:00:36 +02001133 sdata->u.ibss.chandef.chan != cbss->channel)
Johannes Berg46900292009-02-15 12:44:28 +01001134 goto put_bss;
1135
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))
1140 goto put_bss;
1141
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02001142 /* process channel switch */
Luciano Coelho82a8e172013-12-13 21:13:50 +02001143 if (sdata->vif.csa_active ||
1144 ieee80211_ibss_process_chanswitch(sdata, elems, true))
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02001145 goto put_bss;
1146
Alina Friedrichsen34e8f082009-02-22 00:07:28 +01001147 /* same BSSID */
Joe Perchesb203ca32012-05-08 18:56:52 +00001148 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
Alina Friedrichsen34e8f082009-02-22 00:07:28 +01001149 goto put_bss;
1150
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02001151 /* we use a fixed BSSID */
1152 if (sdata->u.ibss.fixed_bssid)
1153 goto put_bss;
1154
Thomas Pedersenf4bda332012-11-13 10:46:27 -08001155 if (ieee80211_have_rx_timestamp(rx_status)) {
1156 /* time when timestamp field was received */
1157 rx_timestamp =
1158 ieee80211_calculate_rx_timestamp(local, rx_status,
1159 len + FCS_LEN, 24);
Johannes Berg24487982009-04-23 18:52:52 +02001160 } else {
1161 /*
1162 * second best option: get current TSF
1163 * (will return -1 if not supported)
1164 */
Eliad Peller37a41b42011-09-21 14:06:11 +03001165 rx_timestamp = drv_get_tsf(local, sdata);
Johannes Berg24487982009-04-23 18:52:52 +02001166 }
Johannes Berg46900292009-02-15 12:44:28 +01001167
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001168 ibss_dbg(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),
1174 jiffies);
Johannes Berg46900292009-02-15 12:44:28 +01001175
1176 if (beacon_timestamp > rx_timestamp) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001177 ibss_dbg(sdata,
1178 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1179 mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +01001180 ieee80211_sta_join_ibss(sdata, bss);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001181 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
Johannes Berg34e89502010-02-03 13:59:58 +01001182 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
Antonio Quartulli52874a52013-06-18 14:20:40 +02001183 supp_rates);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001184 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +01001185 }
1186
1187 put_bss:
1188 ieee80211_rx_bss_put(local, bss);
1189}
1190
Johannes Berg8bf11d82011-12-15 11:17:37 +01001191void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1192 const u8 *bssid, const u8 *addr,
1193 u32 supp_rates)
Johannes Berg46900292009-02-15 12:44:28 +01001194{
Felix Fietkau2e10d332009-12-20 19:07:09 +01001195 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +01001196 struct ieee80211_local *local = sdata->local;
1197 struct sta_info *sta;
Johannes Berg55de9082012-07-26 17:24:39 +02001198 struct ieee80211_chanctx_conf *chanctx_conf;
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -07001199 struct ieee80211_supported_band *sband;
Simon Wunderlich74608ac2013-07-08 16:55:54 +02001200 enum nl80211_bss_scan_width scan_width;
Johannes Berg55de9082012-07-26 17:24:39 +02001201 int band;
Johannes Berg46900292009-02-15 12:44:28 +01001202
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001203 /*
1204 * XXX: Consider removing the least recently used entry and
1205 * allow new one to be added.
1206 */
Johannes Berg46900292009-02-15 12:44:28 +01001207 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001208 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
Joe Perchese87cc472012-05-13 21:56:26 +00001209 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001210 return;
Johannes Berg46900292009-02-15 12:44:28 +01001211 }
1212
Felix Fietkau2e10d332009-12-20 19:07:09 +01001213 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
Johannes Berg8bf11d82011-12-15 11:17:37 +01001214 return;
Felix Fietkau2e10d332009-12-20 19:07:09 +01001215
Joe Perchesb203ca32012-05-08 18:56:52 +00001216 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
Johannes Berg8bf11d82011-12-15 11:17:37 +01001217 return;
Johannes Berg46900292009-02-15 12:44:28 +01001218
Johannes Berg55de9082012-07-26 17:24:39 +02001219 rcu_read_lock();
1220 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1221 if (WARN_ON_ONCE(!chanctx_conf)) {
1222 rcu_read_unlock();
1223 return;
1224 }
Johannes Berg4bf88532012-11-09 11:39:59 +01001225 band = chanctx_conf->def.chan->band;
Simon Wunderlich74608ac2013-07-08 16:55:54 +02001226 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +02001227 rcu_read_unlock();
1228
Johannes Berg8bf11d82011-12-15 11:17:37 +01001229 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
Johannes Berg46900292009-02-15 12:44:28 +01001230 if (!sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +01001231 return;
Johannes Berg46900292009-02-15 12:44:28 +01001232
Rajkumar Manoharanc8716d92010-10-23 10:59:57 +05301233 sta->last_rx = jiffies;
Johannes Bergd9a7ddb2011-12-14 12:35:30 +01001234
Johannes Berg46900292009-02-15 12:44:28 +01001235 /* make sure mandatory rates are always added */
Ashok Nagarajanb422c6c2013-05-10 17:50:51 -07001236 sband = local->hw.wiphy->bands[band];
Johannes Berg46900292009-02-15 12:44:28 +01001237 sta->sta.supp_rates[band] = supp_rates |
Simon Wunderlich74608ac2013-07-08 16:55:54 +02001238 ieee80211_mandatory_rates(sband, scan_width);
Johannes Berg46900292009-02-15 12:44:28 +01001239
Johannes Berg8bf11d82011-12-15 11:17:37 +01001240 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);
Johannes Berg46900292009-02-15 12:44:28 +01001244}
1245
Antonio Quartullidad9def2013-07-26 21:15:02 +02001246static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1247{
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;
1252
1253 mutex_lock(&local->sta_mtx);
1254
1255 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1256 if (sdata != sta->sdata)
1257 continue;
1258
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);
1265
1266 WARN_ON(__sta_info_destroy(sta));
1267 }
1268 }
1269
1270 mutex_unlock(&local->sta_mtx);
1271}
1272
Benoit Papillaultce9058a2010-01-17 22:45:23 +01001273/*
1274 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1275 */
Johannes Berg46900292009-02-15 12:44:28 +01001276
1277static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1278{
1279 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001280 enum nl80211_bss_scan_width scan_width;
Johannes Berg46900292009-02-15 12:44:28 +01001281
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001282 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001283
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001284 mod_timer(&ifibss->timer,
1285 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +01001286
Antonio Quartullidad9def2013-07-26 21:15:02 +02001287 ieee80211_ibss_sta_expire(sdata);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001288
Sujith450aae32009-11-02 12:33:23 +05301289 if (time_before(jiffies, ifibss->last_scan_completed +
1290 IEEE80211_IBSS_MERGE_INTERVAL))
1291 return;
1292
Johannes Berg46900292009-02-15 12:44:28 +01001293 if (ieee80211_sta_active_ibss(sdata))
1294 return;
1295
John W. Linvillec037b832012-01-30 15:28:11 -05001296 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +01001297 return;
1298
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001299 sdata_info(sdata,
1300 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
Johannes Berg46900292009-02-15 12:44:28 +01001301
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001302 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001303 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001304 NULL, 0, scan_width);
Johannes Berg46900292009-02-15 12:44:28 +01001305}
1306
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001307static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +01001308{
1309 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +01001310 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +01001311 u16 capability;
1312 int i;
1313
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001314 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001315
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001316 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +01001317 memcpy(bssid, ifibss->bssid, ETH_ALEN);
1318 } else {
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++)
Johannes Berg47846c92009-11-25 17:46:19 +01001324 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +01001325 bssid[0] &= ~0x01;
1326 bssid[0] |= 0x02;
1327 }
1328
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001329 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
Johannes Berg46900292009-02-15 12:44:28 +01001330
Johannes Berg46900292009-02-15 12:44:28 +01001331 capability = WLAN_CAPABILITY_IBSS;
1332
Johannes Bergfffd0932009-07-08 14:22:54 +02001333 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +01001334 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg46900292009-02-15 12:44:28 +01001335
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001336 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Simon Wunderlich75a423f2013-08-21 15:30:25 +02001337 &ifibss->chandef, ifibss->basic_rates,
Sujith Manoharanc13a7652012-10-12 17:35:45 +05301338 capability, 0, true);
Johannes Berg46900292009-02-15 12:44:28 +01001339}
1340
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001341static unsigned ibss_setup_channels(struct wiphy *wiphy,
1342 struct ieee80211_channel **channels,
1343 unsigned int channels_max,
1344 u32 center_freq, u32 width)
1345{
1346 struct ieee80211_channel *chan = NULL;
1347 unsigned int n_chan = 0;
1348 u32 start_freq, end_freq, freq;
1349
1350 if (width <= 20) {
1351 start_freq = center_freq;
1352 end_freq = center_freq;
1353 } else {
1354 start_freq = center_freq - width / 2 + 10;
1355 end_freq = center_freq + width / 2 - 10;
1356 }
1357
1358 for (freq = start_freq; freq <= end_freq; freq += 20) {
1359 chan = ieee80211_get_channel(wiphy, freq);
1360 if (!chan)
1361 continue;
1362 if (n_chan >= channels_max)
1363 return n_chan;
1364
1365 channels[n_chan] = chan;
1366 n_chan++;
1367 }
1368
1369 return n_chan;
1370}
1371
1372static unsigned int
1373ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1374 const struct cfg80211_chan_def *chandef,
1375 struct ieee80211_channel **channels,
1376 unsigned int channels_max)
1377{
1378 unsigned int n_chan = 0;
1379 u32 width, cf1, cf2 = 0;
1380
1381 switch (chandef->width) {
1382 case NL80211_CHAN_WIDTH_40:
1383 width = 40;
1384 break;
1385 case NL80211_CHAN_WIDTH_80P80:
1386 cf2 = chandef->center_freq2;
1387 /* fall through */
1388 case NL80211_CHAN_WIDTH_80:
1389 width = 80;
1390 break;
1391 case NL80211_CHAN_WIDTH_160:
1392 width = 160;
1393 break;
1394 default:
1395 width = 20;
1396 break;
1397 }
1398
1399 cf1 = chandef->center_freq1;
1400
1401 n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1402
1403 if (cf2)
1404 n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1405 channels_max - n_chan, cf2,
1406 width);
1407
1408 return n_chan;
1409}
1410
Benoit Papillaultce9058a2010-01-17 22:45:23 +01001411/*
1412 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1413 */
1414
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001415static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +01001416{
1417 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1418 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001419 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001420 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +01001421 const u8 *bssid = NULL;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001422 enum nl80211_bss_scan_width scan_width;
Johannes Berg46900292009-02-15 12:44:28 +01001423 int active_ibss;
1424
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001425 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001426
Johannes Berg46900292009-02-15 12:44:28 +01001427 active_ibss = ieee80211_sta_active_ibss(sdata);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001428 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +01001429
1430 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001431 return;
Johannes Berg46900292009-02-15 12:44:28 +01001432
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001433 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +01001434 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001435 if (ifibss->fixed_channel)
Simon Wunderlich3aede782013-05-16 13:00:36 +02001436 chan = ifibss->chandef.chan;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001437 if (!is_zero_ether_addr(ifibss->bssid))
1438 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001439 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1440 ifibss->ssid, ifibss->ssid_len,
Dedy Lansky6eb18132015-02-08 15:52:03 +02001441 IEEE80211_BSS_TYPE_IBSS,
1442 IEEE80211_PRIVACY(ifibss->privacy));
Johannes Berg46900292009-02-15 12:44:28 +01001443
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001444 if (cbss) {
1445 struct ieee80211_bss *bss;
1446
1447 bss = (void *)cbss->priv;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001448 ibss_dbg(sdata,
1449 "sta_find_ibss: selected %pM current %pM\n",
1450 cbss->bssid, ifibss->bssid);
1451 sdata_info(sdata,
1452 "Selected IBSS BSSID %pM based on configured SSID\n",
1453 cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +01001454
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001455 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +01001456 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001457 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -07001458 }
Johannes Berg46900292009-02-15 12:44:28 +01001459
Antonio Quartullid8eb7412013-08-09 18:58:32 +02001460 /* if a fixed bssid and a fixed freq have been provided create the IBSS
1461 * directly and do not waste time scanning
1462 */
1463 if (ifibss->fixed_bssid && ifibss->fixed_channel) {
1464 sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
1465 bssid);
1466 ieee80211_sta_create_ibss(sdata);
1467 return;
1468 }
1469
1470
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001471 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
Johannes Berg46900292009-02-15 12:44:28 +01001472
1473 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +01001474 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +01001475 IEEE80211_SCAN_INTERVAL)) {
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001476 struct ieee80211_channel *channels[8];
1477 unsigned int num;
1478
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001479 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
Johannes Berg46900292009-02-15 12:44:28 +01001480
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001481 num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1482 &ifibss->chandef,
1483 channels,
1484 ARRAY_SIZE(channels));
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001485 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001486 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001487 ifibss->ssid_len, channels, num,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001488 scan_width);
Benoit Papillaultce9058a2010-01-17 22:45:23 +01001489 } else {
Johannes Berg46900292009-02-15 12:44:28 +01001490 int interval = IEEE80211_SCAN_INTERVAL;
1491
1492 if (time_after(jiffies, ifibss->ibss_join_req +
Johannes Berg55de9082012-07-26 17:24:39 +02001493 IEEE80211_IBSS_JOIN_TIMEOUT))
1494 ieee80211_sta_create_ibss(sdata);
Johannes Berg46900292009-02-15 12:44:28 +01001495
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001496 mod_timer(&ifibss->timer,
1497 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +01001498 }
Johannes Berg46900292009-02-15 12:44:28 +01001499}
1500
1501static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Bergc269a202011-02-14 12:20:22 +01001502 struct sk_buff *req)
Johannes Berg46900292009-02-15 12:44:28 +01001503{
Johannes Bergc269a202011-02-14 12:20:22 +01001504 struct ieee80211_mgmt *mgmt = (void *)req->data;
Johannes Berg46900292009-02-15 12:44:28 +01001505 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1506 struct ieee80211_local *local = sdata->local;
Johannes Bergc269a202011-02-14 12:20:22 +01001507 int tx_last_beacon, len = req->len;
Johannes Berg46900292009-02-15 12:44:28 +01001508 struct sk_buff *skb;
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001509 struct beacon_data *presp;
Johannes Berg46900292009-02-15 12:44:28 +01001510 u8 *pos, *end;
1511
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001512 sdata_assert_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001513
Johannes Berg40b275b2011-05-13 14:15:49 +02001514 presp = rcu_dereference_protected(ifibss->presp,
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001515 lockdep_is_held(&sdata->wdev.mtx));
Johannes Berg40b275b2011-05-13 14:15:49 +02001516
Johannes Berg46900292009-02-15 12:44:28 +01001517 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Berg40b275b2011-05-13 14:15:49 +02001518 len < 24 + 2 || !presp)
Johannes Berg46900292009-02-15 12:44:28 +01001519 return;
1520
Johannes Berg24487982009-04-23 18:52:52 +02001521 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +01001522
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001523 ibss_dbg(sdata,
1524 "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
1525 mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
Johannes Berg46900292009-02-15 12:44:28 +01001526
Felix Fietkau1ed76482011-03-24 19:46:18 +01001527 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
Johannes Berg46900292009-02-15 12:44:28 +01001528 return;
1529
Joe Perchesb203ca32012-05-08 18:56:52 +00001530 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
Felix Fietkau888d04d2012-03-01 15:22:09 +01001531 !is_broadcast_ether_addr(mgmt->bssid))
Johannes Berg46900292009-02-15 12:44:28 +01001532 return;
1533
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) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001538 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1539 mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +01001540 return;
1541 }
1542 if (pos[1] != 0 &&
1543 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +01001544 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +01001545 /* Ignore ProbeReq for foreign SSID */
1546 return;
1547 }
1548
1549 /* Reply with ProbeResp */
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001550 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
Johannes Berg46900292009-02-15 12:44:28 +01001551 if (!skb)
1552 return;
1553
Johannes Bergc3ffeab2013-03-07 20:54:29 +01001554 skb_reserve(skb, local->tx_headroom);
1555 memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
1556
1557 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1558 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
Johannes Berg62ae67b2009-11-18 18:42:05 +01001559 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Simon Wunderlich691eb612014-01-24 23:48:29 +01001560
1561 /* avoid excessive retries for probe request to wildcard SSIDs */
1562 if (pos[1] == 0)
1563 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
1564
Johannes Berg62ae67b2009-11-18 18:42:05 +01001565 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +01001566}
1567
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001568static
1569void 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)
Johannes Berg46900292009-02-15 12:44:28 +01001572{
1573 size_t baselen;
1574 struct ieee802_11_elems elems;
1575
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001576 BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
1577 offsetof(typeof(mgmt->u.beacon), variable));
1578
1579 /*
1580 * either beacon or probe_resp but the variable field is at the
1581 * same offset
1582 */
Johannes Berg46900292009-02-15 12:44:28 +01001583 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1584 if (baselen > len)
1585 return;
1586
1587 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01001588 false, &elems);
Johannes Berg46900292009-02-15 12:44:28 +01001589
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001590 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg46900292009-02-15 12:44:28 +01001591}
1592
Johannes Berg1fa57d02010-06-10 10:21:32 +02001593void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1594 struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +01001595{
1596 struct ieee80211_rx_status *rx_status;
1597 struct ieee80211_mgmt *mgmt;
1598 u16 fc;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02001599 struct ieee802_11_elems elems;
1600 int ies_len;
Johannes Berg46900292009-02-15 12:44:28 +01001601
Johannes Bergf1d58c22009-06-17 13:13:00 +02001602 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +01001603 mgmt = (struct ieee80211_mgmt *) skb->data;
1604 fc = le16_to_cpu(mgmt->frame_control);
1605
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001606 sdata_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001607
Tim Harveyc926d002010-12-09 10:43:13 -08001608 if (!sdata->u.ibss.ssid_len)
1609 goto mgmt_out; /* not ready to merge yet */
1610
Johannes Berg46900292009-02-15 12:44:28 +01001611 switch (fc & IEEE80211_FCTL_STYPE) {
1612 case IEEE80211_STYPE_PROBE_REQ:
Johannes Bergc269a202011-02-14 12:20:22 +01001613 ieee80211_rx_mgmt_probe_req(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +01001614 break;
1615 case IEEE80211_STYPE_PROBE_RESP:
Johannes Berg46900292009-02-15 12:44:28 +01001616 case IEEE80211_STYPE_BEACON:
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02001617 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1618 rx_status);
Johannes Berg46900292009-02-15 12:44:28 +01001619 break;
1620 case IEEE80211_STYPE_AUTH:
1621 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1622 break;
Antonio Quartulli2cc59e72012-09-07 13:28:53 +02001623 case IEEE80211_STYPE_DEAUTH:
1624 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1625 break;
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02001626 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);
1632
1633 if (ies_len < 0)
1634 break;
1635
1636 ieee802_11_parse_elems(
1637 mgmt->u.action.u.chan_switch.variable,
1638 ies_len, true, &elems);
1639
1640 if (elems.parse_error)
1641 break;
1642
1643 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
1644 rx_status, &elems);
1645 break;
1646 }
Johannes Berg46900292009-02-15 12:44:28 +01001647 }
Johannes Berg7a17a332010-07-21 11:30:27 +02001648
Tim Harveyc926d002010-12-09 10:43:13 -08001649 mgmt_out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001650 sdata_unlock(sdata);
Johannes Berg46900292009-02-15 12:44:28 +01001651}
1652
Johannes Berg1fa57d02010-06-10 10:21:32 +02001653void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +01001654{
Johannes Berg1fa57d02010-06-10 10:21:32 +02001655 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +01001656 struct sta_info *sta;
Johannes Berg46900292009-02-15 12:44:28 +01001657
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001658 sdata_lock(sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001659
1660 /*
1661 * Work could be scheduled after scan or similar
1662 * when we aren't even joined (or trying) with a
1663 * network.
1664 */
1665 if (!ifibss->ssid_len)
1666 goto out;
Johannes Berg46900292009-02-15 12:44:28 +01001667
Johannes Berg8bf11d82011-12-15 11:17:37 +01001668 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);
1674
Antonio Quartulli52874a52013-06-18 14:20:40 +02001675 ieee80211_ibss_finish_sta(sta);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001676 rcu_read_unlock();
1677 spin_lock_bh(&ifibss->incomplete_lock);
1678 }
1679 spin_unlock_bh(&ifibss->incomplete_lock);
1680
Johannes Berg46900292009-02-15 12:44:28 +01001681 switch (ifibss->state) {
1682 case IEEE80211_IBSS_MLME_SEARCH:
1683 ieee80211_sta_find_ibss(sdata);
1684 break;
1685 case IEEE80211_IBSS_MLME_JOINED:
1686 ieee80211_sta_merge_ibss(sdata);
1687 break;
1688 default:
1689 WARN_ON(1);
1690 break;
1691 }
Johannes Berg46900292009-02-15 12:44:28 +01001692
Johannes Berg7a17a332010-07-21 11:30:27 +02001693 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001694 sdata_unlock(sdata);
Johannes Berg3a4d4aa2010-05-26 16:41:40 +02001695}
1696
Johannes Berg46900292009-02-15 12:44:28 +01001697static void ieee80211_ibss_timer(unsigned long data)
1698{
1699 struct ieee80211_sub_if_data *sdata =
1700 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01001701
Stanislaw Gruszkaa6182942013-02-28 10:55:28 +01001702 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001703}
1704
1705void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1706{
1707 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1708
Johannes Berg46900292009-02-15 12:44:28 +01001709 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1710 (unsigned long) sdata);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001711 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1712 spin_lock_init(&ifibss->incomplete_lock);
Simon Wunderlichcd7760e2013-08-28 13:41:31 +02001713 INIT_WORK(&ifibss->csa_connection_drop_work,
1714 ieee80211_csa_connection_drop_work);
Johannes Berg46900292009-02-15 12:44:28 +01001715}
1716
1717/* scan finished notification */
1718void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1719{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001720 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001721
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001722 mutex_lock(&local->iflist_mtx);
1723 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001724 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +02001725 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001726 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1727 continue;
1728 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg7a17a332010-07-21 11:30:27 +02001729 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001730 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001731 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +01001732}
1733
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001734int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1735 struct cfg80211_ibss_params *params)
1736{
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001737 u32 changed = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001738 u32 rate_flags;
1739 struct ieee80211_supported_band *sband;
Luciano Coelho71965c12014-02-18 17:07:53 +02001740 enum ieee80211_chanctx_mode chanmode;
1741 struct ieee80211_local *local = sdata->local;
1742 int radar_detect_width = 0;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001743 int i;
Luciano Coelho71965c12014-02-18 17:07:53 +02001744 int ret;
1745
1746 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1747 &params->chandef,
1748 sdata->wdev.iftype);
1749 if (ret < 0)
1750 return ret;
1751
1752 if (ret > 0) {
1753 if (!params->userspace_handles_dfs)
1754 return -EINVAL;
1755 radar_detect_width = BIT(params->chandef.width);
1756 }
1757
1758 chanmode = (params->channel_fixed && !ret) ?
1759 IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
1760
1761 mutex_lock(&local->chanctx_mtx);
1762 ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
1763 radar_detect_width);
1764 mutex_unlock(&local->chanctx_mtx);
1765 if (ret < 0)
1766 return ret;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001767
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001768 if (params->bssid) {
1769 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1770 sdata->u.ibss.fixed_bssid = true;
1771 } else
1772 sdata->u.ibss.fixed_bssid = false;
1773
Johannes Bergfffd0932009-07-08 14:22:54 +02001774 sdata->u.ibss.privacy = params->privacy;
Antonio Quartulli267335d2012-01-31 20:25:47 +01001775 sdata->u.ibss.control_port = params->control_port;
Simon Wunderlich8e8d3472013-10-07 18:41:06 +02001776 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03001777 sdata->u.ibss.basic_rates = params->basic_rates;
Krzysztof HaƂasac7d37a62014-05-26 14:14:46 +02001778 sdata->u.ibss.last_scan_completed = jiffies;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001779
1780 /* fix basic_rates if channel does not support these rates */
1781 rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
Luciano Coelho71965c12014-02-18 17:07:53 +02001782 sband = local->hw.wiphy->bands[params->chandef.chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001783 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);
1786 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01001787 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1788 sizeof(params->mcast_rate));
Johannes Bergfffd0932009-07-08 14:22:54 +02001789
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001790 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1791
Simon Wunderlich3aede782013-05-16 13:00:36 +02001792 sdata->u.ibss.chandef = params->chandef;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001793 sdata->u.ibss.fixed_channel = params->channel_fixed;
1794
1795 if (params->ie) {
1796 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1797 GFP_KERNEL);
1798 if (sdata->u.ibss.ie)
1799 sdata->u.ibss.ie_len = params->ie_len;
1800 }
1801
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001802 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1803 sdata->u.ibss.ibss_join_req = jiffies;
1804
Antonio Quartullibadecb02012-10-26 18:54:25 +02001805 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
Johannes Berg0e41f712009-04-23 11:48:56 +02001806 sdata->u.ibss.ssid_len = params->ssid_len;
1807
Simon Wunderlich822854b2013-06-28 10:39:59 +02001808 memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1809 sizeof(sdata->u.ibss.ht_capa));
1810 memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1811 sizeof(sdata->u.ibss.ht_capa_mask));
1812
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001813 /*
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.
1817 *
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
1820 * were set to 1.
1821 */
1822
1823 sdata->vif.bss_conf.ht_operation_mode |=
1824 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1825 | IEEE80211_HT_PARAM_RIFS_MODE;
1826
1827 changed |= BSS_CHANGED_HT;
1828 ieee80211_bss_info_change_notify(sdata, changed);
1829
Johannes Berg04ecd252012-09-11 14:34:12 +02001830 sdata->smps_mode = IEEE80211_SMPS_OFF;
Luciano Coelho71965c12014-02-18 17:07:53 +02001831 sdata->needed_rx_chains = local->rx_chains;
Johannes Berg04ecd252012-09-11 14:34:12 +02001832
Luciano Coelho71965c12014-02-18 17:07:53 +02001833 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001834
1835 return 0;
1836}
1837
1838int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1839{
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001840 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg7a17a332010-07-21 11:30:27 +02001841
Simon Wunderlich871a4182013-08-28 13:41:30 +02001842 ieee80211_ibss_disconnect(sdata);
Simon Wunderlichb78a4932012-11-13 18:43:03 +01001843 ifibss->ssid_len = 0;
Joe Perchesc84a67a2015-03-02 19:54:57 -08001844 eth_zero_addr(ifibss->bssid);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001845
1846 /* remove beacon */
1847 kfree(sdata->u.ibss.ie);
Simon Wunderlich822854b2013-06-28 10:39:59 +02001848
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));
1852
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001853 synchronize_rcu();
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001854
Johannes Berg35f20c12010-06-10 10:21:30 +02001855 skb_queue_purge(&sdata->skb_queue);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001856
Johannes Bergbc05d192010-07-21 10:52:40 +02001857 del_timer_sync(&sdata->u.ibss.timer);
Johannes Berg7a17a332010-07-21 11:30:27 +02001858
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001859 return 0;
1860}