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