mac80211: Fix incorrectly named last_hop_metric variable in mesh_rx_path_sel_frame
[linux-2.6-block.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);
abcff6ef
JD
191
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 }
13c40c54
AS
201 }
202
40b861a0
AN
203 if (local->hw.queues >= IEEE80211_NUM_ACS)
204 pos = ieee80211_add_wmm_info_ie(pos, 0); /* U-APSD not in use */
9eba6125 205
c3ffeab4
JB
206 presp->head_len = pos - presp->head;
207 if (WARN_ON(presp->head_len > frame_len))
d51b70ff
SW
208 goto error;
209
210 return presp;
211error:
212 kfree(presp);
213 return NULL;
214}
215
216static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
217 const u8 *bssid, const int beacon_int,
b35c8097 218 struct cfg80211_chan_def *req_chandef,
d51b70ff
SW
219 const u32 basic_rates,
220 const u16 capability, u64 tsf,
221 bool creator)
222{
223 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
224 struct ieee80211_local *local = sdata->local;
d51b70ff
SW
225 struct ieee80211_mgmt *mgmt;
226 struct cfg80211_bss *bss;
227 u32 bss_change;
228 struct cfg80211_chan_def chandef;
b35c8097 229 struct ieee80211_channel *chan;
d51b70ff
SW
230 struct beacon_data *presp;
231 enum nl80211_bss_scan_width scan_width;
232 bool have_higher_than_11mbit;
2beb6dab 233 bool radar_required;
55fff501 234 int err;
d51b70ff
SW
235
236 sdata_assert_lock(sdata);
237
238 /* Reset own TSF to allow time synchronization work. */
239 drv_reset_tsf(local, sdata);
240
241 if (!ether_addr_equal(ifibss->bssid, bssid))
242 sta_info_flush(sdata);
243
244 /* if merging, indicate to driver that we leave the old IBSS */
245 if (sdata->vif.bss_conf.ibss_joined) {
246 sdata->vif.bss_conf.ibss_joined = false;
247 sdata->vif.bss_conf.ibss_creator = false;
248 sdata->vif.bss_conf.enable_beacon = false;
249 netif_carrier_off(sdata->dev);
250 ieee80211_bss_info_change_notify(sdata,
251 BSS_CHANGED_IBSS |
252 BSS_CHANGED_BEACON_ENABLED);
55fff501 253 drv_leave_ibss(local, sdata);
d51b70ff
SW
254 }
255
256 presp = rcu_dereference_protected(ifibss->presp,
257 lockdep_is_held(&sdata->wdev.mtx));
0c2bef46 258 RCU_INIT_POINTER(ifibss->presp, NULL);
d51b70ff
SW
259 if (presp)
260 kfree_rcu(presp, rcu_head);
261
b35c8097
JL
262 /* make a copy of the chandef, it could be modified below. */
263 chandef = *req_chandef;
264 chan = chandef.chan;
174e0cd2
IP
265 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
266 NL80211_IFTYPE_ADHOC)) {
d51b70ff
SW
267 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
268 chandef.width == NL80211_CHAN_WIDTH_10 ||
269 chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
270 chandef.width == NL80211_CHAN_WIDTH_20) {
271 sdata_info(sdata,
272 "Failed to join IBSS, beacons forbidden\n");
273 return;
274 }
275 chandef.width = NL80211_CHAN_WIDTH_20;
276 chandef.center_freq1 = chan->center_freq;
8e8d347d 277 /* check again for downgraded chandef */
174e0cd2
IP
278 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef,
279 NL80211_IFTYPE_ADHOC)) {
8e8d347d
SW
280 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,
2beb6dab 287 &chandef, NL80211_IFTYPE_ADHOC);
6658ab80
LC
288 if (err < 0) {
289 sdata_info(sdata,
290 "Failed to join IBSS, invalid chandef\n");
291 return;
292 }
2beb6dab
LC
293 if (err > 0 && !ifibss->userspace_handles_dfs) {
294 sdata_info(sdata,
295 "Failed to join IBSS, DFS channel without control program\n");
296 return;
d51b70ff
SW
297 }
298
2beb6dab
LC
299 radar_required = err;
300
34a3740d 301 mutex_lock(&local->mtx);
d51b70ff
SW
302 if (ieee80211_vif_use_channel(sdata, &chandef,
303 ifibss->fixed_channel ?
304 IEEE80211_CHANCTX_SHARED :
305 IEEE80211_CHANCTX_EXCLUSIVE)) {
306 sdata_info(sdata, "Failed to join IBSS, no channel context\n");
34a3740d 307 mutex_unlock(&local->mtx);
d51b70ff
SW
308 return;
309 }
cc901de1 310 sdata->radar_required = radar_required;
34a3740d 311 mutex_unlock(&local->mtx);
d51b70ff
SW
312
313 memcpy(ifibss->bssid, bssid, ETH_ALEN);
314
d51b70ff
SW
315 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
316 capability, tsf, &chandef,
cd7760e6 317 &have_higher_than_11mbit, NULL);
d51b70ff 318 if (!presp)
c3ffeab4
JB
319 return;
320
321 rcu_assign_pointer(ifibss->presp, presp);
d51b70ff 322 mgmt = (void *)presp->head;
46900298 323
d6a83228 324 sdata->vif.bss_conf.enable_beacon = true;
2d0ddec5 325 sdata->vif.bss_conf.beacon_int = beacon_int;
fbd2c8dc 326 sdata->vif.bss_conf.basic_rates = basic_rates;
0ca54f6c
MP
327 sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
328 memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
2d0ddec5
JB
329 bss_change = BSS_CHANGED_BEACON_INT;
330 bss_change |= ieee80211_reset_erp_info(sdata);
331 bss_change |= BSS_CHANGED_BSSID;
332 bss_change |= BSS_CHANGED_BEACON;
333 bss_change |= BSS_CHANGED_BEACON_ENABLED;
392cfdb1 334 bss_change |= BSS_CHANGED_BASIC_RATES;
13c40c54 335 bss_change |= BSS_CHANGED_HT;
8fc214ba 336 bss_change |= BSS_CHANGED_IBSS;
0ca54f6c 337 bss_change |= BSS_CHANGED_SSID;
2f91a967
SW
338
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
2ec9c1f6
SW
352 /* cf. IEEE 802.11 9.2.12 */
353 if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit)
354 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
355 else
356 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
357
55fff501
JB
358 ieee80211_set_wmm_default(sdata, true);
359
8fc214ba 360 sdata->vif.bss_conf.ibss_joined = true;
c13a765b 361 sdata->vif.bss_conf.ibss_creator = creator;
46900298 362
55fff501
JB
363 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);
34a3740d 371 mutex_lock(&local->mtx);
55fff501 372 ieee80211_vif_release_channel(sdata);
34a3740d 373 mutex_unlock(&local->mtx);
55fff501
JB
374 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);
46900298 380
46900298 381 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
af8cdcd8
JB
382 mod_timer(&ifibss->timer,
383 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 384
7ca15a0a
SW
385 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);
5b112d3d 389 cfg80211_put_bss(local->hw.wiphy, bss);
86a2ea41 390 netif_carrier_on(sdata->dev);
fe94f3a4 391 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, chan, GFP_KERNEL);
46900298
JB
392}
393
af8cdcd8
JB
394static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
395 struct ieee80211_bss *bss)
46900298 396{
0c1ad2ca
JB
397 struct cfg80211_bss *cbss =
398 container_of((void *)bss, struct cfg80211_bss, priv);
b59066a2 399 struct ieee80211_supported_band *sband;
75a423f4 400 struct cfg80211_chan_def chandef;
b59066a2
JB
401 u32 basic_rates;
402 int i, j;
0c1ad2ca 403 u16 beacon_int = cbss->beacon_interval;
8cef2c9d 404 const struct cfg80211_bss_ies *ies;
75a423f4 405 enum nl80211_channel_type chan_type;
8cef2c9d 406 u64 tsf;
2103dec1
SW
407 u32 rate_flags;
408 int shift;
57c4d7b4 409
8d61ffa5 410 sdata_assert_lock(sdata);
7a17a33c 411
57c4d7b4
JB
412 if (beacon_int < 10)
413 beacon_int = 10;
414
75a423f4
SW
415 switch (sdata->u.ibss.chandef.width) {
416 case NL80211_CHAN_WIDTH_20_NOHT:
417 case NL80211_CHAN_WIDTH_20:
418 case NL80211_CHAN_WIDTH_40:
419 chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
420 cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
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;
abcff6ef
JD
428 case NL80211_CHAN_WIDTH_80:
429 case NL80211_CHAN_WIDTH_160:
430 chandef = sdata->u.ibss.chandef;
431 chandef.chan = cbss->channel;
432 break;
75a423f4
SW
433 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
0c1ad2ca 440 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
2103dec1
SW
441 rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef);
442 shift = ieee80211_vif_get_shift(&sdata->vif);
b59066a2
JB
443
444 basic_rates = 0;
445
446 for (i = 0; i < bss->supp_rates_len; i++) {
2103dec1 447 int rate = bss->supp_rates[i] & 0x7f;
b59066a2
JB
448 bool is_basic = !!(bss->supp_rates[i] & 0x80);
449
450 for (j = 0; j < sband->n_bitrates; j++) {
2103dec1
SW
451 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) {
b59066a2
JB
459 if (is_basic)
460 basic_rates |= BIT(j);
461 break;
462 }
463 }
464 }
465
8cef2c9d
JB
466 rcu_read_lock();
467 ies = rcu_dereference(cbss->ies);
468 tsf = ies->tsf;
469 rcu_read_unlock();
470
0c1ad2ca 471 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
57c4d7b4 472 beacon_int,
75a423f4 473 &chandef,
b59066a2 474 basic_rates,
0c1ad2ca 475 cbss->capability,
8cef2c9d 476 tsf, false);
46900298
JB
477}
478
cd7760e6
SW
479int 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;
6eb18137 486 u16 capability = 0;
cd7760e6
SW
487 u64 tsf;
488 int ret = 0;
489
490 sdata_assert_lock(sdata);
491
cd7760e6 492 if (ifibss->privacy)
6eb18137 493 capability = WLAN_CAPABILITY_PRIVACY;
cd7760e6
SW
494
495 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
496 ifibss->bssid, ifibss->ssid,
6eb18137
DL
497 ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
498 IEEE80211_PRIVACY(ifibss->privacy));
cd7760e6
SW
499
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
dddd586b 528 return BSS_CHANGED_BEACON;
cd7760e6
SW
529 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;
faf046e7 537 int err, changed = 0;
cd7760e6 538
bafdc614
SW
539 sdata_assert_lock(sdata);
540
cd7760e6
SW
541 /* update cfg80211 bss information with the new channel */
542 if (!is_zero_ether_addr(ifibss->bssid)) {
cd7760e6
SW
543 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
544 ifibss->chandef.chan,
545 ifibss->bssid, ifibss->ssid,
6eb18137
DL
546 ifibss->ssid_len,
547 IEEE80211_BSS_TYPE_IBSS,
548 IEEE80211_PRIVACY(ifibss->privacy));
cd7760e6
SW
549 /* XXX: should not really modify cfg80211 data */
550 if (cbss) {
33787fc4 551 cbss->channel = sdata->csa_chandef.chan;
cd7760e6
SW
552 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
553 }
554 }
555
33787fc4 556 ifibss->chandef = sdata->csa_chandef;
cd7760e6
SW
557
558 /* generate the beacon */
559 err = ieee80211_ibss_csa_beacon(sdata, NULL);
cd7760e6
SW
560 if (err < 0)
561 return err;
562
faf046e7 563 changed |= err;
dddd586b 564
faf046e7 565 return changed;
cd7760e6
SW
566}
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
52874a5e 575static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
8bf11d8d
JB
576 __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
bdcbd8e0 583 ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr);
8bf11d8d 584
83d5cc01
JB
585 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
586 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
267335d6
AQ
587 /* authorize the station only if the network is not RSN protected. If
588 * not wait for the userspace to authorize it */
589 if (!sta->sdata->u.ibss.control_port)
590 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
8bf11d8d
JB
591
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 *
52874a5e
AQ
601ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
602 const u8 *addr, u32 supp_rates)
8bf11d8d
JB
603 __acquires(RCU)
604{
605 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
606 struct ieee80211_local *local = sdata->local;
607 struct sta_info *sta;
55de908a 608 struct ieee80211_chanctx_conf *chanctx_conf;
b422c6cd 609 struct ieee80211_supported_band *sband;
74608aca 610 enum nl80211_bss_scan_width scan_width;
55de908a 611 int band;
8bf11d8d
JB
612
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) {
bdcbd8e0 618 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
e87cc472 619 sdata->name, addr);
8bf11d8d
JB
620 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
b203ca39 629 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
8bf11d8d
JB
630 rcu_read_lock();
631 return NULL;
632 }
633
55de908a
JB
634 rcu_read_lock();
635 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
636 if (WARN_ON_ONCE(!chanctx_conf))
637 return NULL;
4bf88530 638 band = chanctx_conf->def.chan->band;
74608aca 639 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
55de908a
JB
640 rcu_read_unlock();
641
8bf11d8d
JB
642 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 */
b422c6cd 651 sband = local->hw.wiphy->bands[band];
8bf11d8d 652 sta->sta.supp_rates[band] = supp_rates |
74608aca 653 ieee80211_mandatory_rates(sband, scan_width);
8bf11d8d 654
52874a5e 655 return ieee80211_ibss_finish_sta(sta);
6d810f10
AQ
656}
657
871a4180
SW
658static 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;
871a4180 689
d4c80d9d 690 if (!is_zero_ether_addr(ifibss->bssid)) {
871a4180
SW
691 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
692 ifibss->bssid, ifibss->ssid,
6eb18137
DL
693 ifibss->ssid_len,
694 IEEE80211_BSS_TYPE_IBSS,
695 IEEE80211_PRIVACY(ifibss->privacy));
871a4180
SW
696
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);
55fff501 736 drv_leave_ibss(local, sdata);
34a3740d 737 mutex_lock(&local->mtx);
871a4180 738 ieee80211_vif_release_channel(sdata);
34a3740d 739 mutex_unlock(&local->mtx);
871a4180
SW
740}
741
cd7760e6
SW
742static 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
6f07d216
LC
748 sdata_lock(sdata);
749
cd7760e6
SW
750 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);
6f07d216
LC
756
757 sdata_unlock(sdata);
cd7760e6
SW
758}
759
8e8d347d
SW
760static 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,
2beb6dab
LC
769 &ifibss->chandef,
770 NL80211_IFTYPE_ADHOC);
8e8d347d
SW
771 if (err > 0)
772 cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
773 GFP_ATOMIC);
774}
775
cd7760e6
SW
776static 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;
c0f17eb9 782 struct ieee80211_csa_ie csa_ie;
cd7760e6 783 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
cd7760e6 784 enum nl80211_channel_type ch_type;
82a8e17d 785 int err;
cd7760e6 786 u32 sta_flags;
cd7760e6 787
dbd72850
MK
788 sdata_assert_lock(sdata);
789
cd7760e6
SW
790 sta_flags = IEEE80211_STA_DISABLE_VHT;
791 switch (ifibss->chandef.width) {
792 case NL80211_CHAN_WIDTH_5:
793 case NL80211_CHAN_WIDTH_10:
794 case NL80211_CHAN_WIDTH_20_NOHT:
795 sta_flags |= IEEE80211_STA_DISABLE_HT;
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));
c0f17eb9 805 memset(&csa_ie, 0, sizeof(csa_ie));
84469a45 806 err = ieee80211_parse_ch_switch_ie(sdata, elems,
cd7760e6 807 ifibss->chandef.chan->band,
c0f17eb9 808 sta_flags, ifibss->bssid, &csa_ie);
cd7760e6
SW
809 /* 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
0834ae3c
SW
817 /* channel switch is not supported, disconnect */
818 if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
819 goto disconnect;
820
c0f17eb9
CYY
821 params.count = csa_ie.count;
822 params.chandef = csa_ie.chandef;
823
cd7760e6
SW
824 switch (ifibss->chandef.width) {
825 case NL80211_CHAN_WIDTH_20_NOHT:
826 case NL80211_CHAN_WIDTH_20:
827 case NL80211_CHAN_WIDTH_40:
828 /* keep our current HT mode (HT20/HT40+/HT40-), even if
829 * another mode has been announced. The mode is not adopted
830 * within the beacon while doing CSA and we should therefore
831 * keep the mode which we announce.
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
174e0cd2
IP
856 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
857 NL80211_IFTYPE_ADHOC)) {
cd7760e6
SW
858 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,
2beb6dab
LC
869 &params.chandef,
870 NL80211_IFTYPE_ADHOC);
cd7760e6
SW
871 if (err < 0)
872 goto disconnect;
2beb6dab 873 if (err > 0 && !ifibss->userspace_handles_dfs) {
8e8d347d 874 /* IBSS-DFS only allowed with a control program */
2beb6dab 875 goto disconnect;
cd7760e6
SW
876 }
877
2beb6dab
LC
878 params.radar_required = err;
879
82a8e17d
LC
880 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;
cd7760e6 885 }
cd7760e6
SW
886
887 /* 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
c0f17eb9 892 params.block_tx = !!csa_ie.mode;
cd7760e6 893
82a8e17d
LC
894 if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
895 &params))
896 goto disconnect;
cd7760e6 897
8e8d347d
SW
898 ieee80211_ibss_csa_mark_radar(sdata);
899
cd7760e6
SW
900 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
8e8d347d
SW
906 ieee80211_ibss_csa_mark_radar(sdata);
907
cd7760e6
SW
908 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
82a8e17d
LC
932 if (!sdata->vif.csa_active)
933 ieee80211_ibss_process_chanswitch(sdata, elems, false);
cd7760e6
SW
934}
935
2cc59e78
AQ
936static 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
6d810f10
AQ
950static 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
8d61ffa5 956 sdata_assert_lock(sdata);
6d810f10
AQ
957
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
bdcbd8e0
JB
964 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);
7bed2050
AQ
967
968 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
969 return;
970
6d810f10
AQ
971 /*
972 * IEEE 802.11 standard does not require authentication in IBSS
973 * networks and most implementations do not seem to use it.
974 * However, try to reply to authentication attempts if someone
975 * has actually implemented this.
976 */
700e8ea6 977 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0,
1672c0e3 978 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
8bf11d8d
JB
979}
980
8a4988d1
JD
981static 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)
46900298 986{
46900298 987 struct sta_info *sta;
46900298 988 enum ieee80211_band band = rx_status->band;
74608aca 989 enum nl80211_bss_scan_width scan_width;
8a4988d1 990 struct ieee80211_local *local = sdata->local;
13c40c54
AS
991 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
992 bool rates_updated = false;
8a4988d1 993 u32 supp_rates = 0;
46900298 994
8a4988d1 995 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
46900298
JB
996 return;
997
8a4988d1
JD
998 if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
999 return;
46900298 1000
8a4988d1
JD
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;
8bf11d8d 1026 }
8a4988d1
JD
1027 } else {
1028 rcu_read_unlock();
1029 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1030 mgmt->sa, supp_rates);
34e89507 1031 }
8a4988d1 1032 }
9eba6125 1033
96a4688e
JB
1034 if (sta && !sta->sta.wme &&
1035 elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS) {
8a4988d1 1036 sta->sta.wme = true;
96a4688e
JB
1037 ieee80211_check_fast_xmit(sta);
1038 }
13c40c54 1039
8a4988d1
JD
1040 if (sta && elems->ht_operation && elems->ht_cap_elem &&
1041 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
1042 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
1043 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
1044 /* we both use HT */
1045 struct ieee80211_ht_cap htcap_ie;
1046 struct cfg80211_chan_def chandef;
abcff6ef 1047 enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
d2954457 1048
8a4988d1
JD
1049 ieee80211_ht_oper_to_chandef(channel,
1050 elems->ht_operation,
1051 &chandef);
d2954457 1052
8a4988d1 1053 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
8a4988d1
JD
1054 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1055 &htcap_ie,
1056 sta);
abcff6ef
JD
1057
1058 if (elems->vht_operation && elems->vht_cap_elem &&
1059 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
1060 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
1061 /* we both use VHT */
1062 struct ieee80211_vht_cap cap_ie;
1063 struct ieee80211_sta_vht_cap cap = sta->sta.vht_cap;
1064
1065 ieee80211_vht_oper_to_chandef(channel,
1066 elems->vht_operation,
1067 &chandef);
1068 memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1069 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1070 &cap_ie, sta);
1071 if (memcmp(&cap, &sta->sta.vht_cap, sizeof(cap)))
1072 rates_updated |= true;
13c40c54
AS
1073 }
1074
abcff6ef
JD
1075 if (bw != sta->sta.bandwidth)
1076 rates_updated |= true;
d2954457 1077
abcff6ef
JD
1078 if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1079 &chandef))
1080 WARN_ON_ONCE(1);
8a4988d1 1081 }
d2954457 1082
8a4988d1
JD
1083 if (sta && rates_updated) {
1084 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1085 u8 rx_nss = sta->sta.rx_nss;
13c40c54 1086
8a4988d1
JD
1087 /* Force rx_nss recalculation */
1088 sta->sta.rx_nss = 0;
1089 rate_control_rate_init(sta);
1090 if (sta->sta.rx_nss != rx_nss)
1091 changed |= IEEE80211_RC_NSS_CHANGED;
13c40c54 1092
8a4988d1 1093 drv_sta_rc_update(local, sdata, &sta->sta, changed);
46900298
JB
1094 }
1095
8a4988d1
JD
1096 rcu_read_unlock();
1097}
1098
1099static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1100 struct ieee80211_mgmt *mgmt, size_t len,
1101 struct ieee80211_rx_status *rx_status,
1102 struct ieee802_11_elems *elems)
1103{
1104 struct ieee80211_local *local = sdata->local;
1105 struct cfg80211_bss *cbss;
1106 struct ieee80211_bss *bss;
1107 struct ieee80211_channel *channel;
1108 u64 beacon_timestamp, rx_timestamp;
1109 u32 supp_rates = 0;
1110 enum ieee80211_band band = rx_status->band;
1111
1112 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1113 if (!channel)
1114 return;
1115
1116 ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
1117
46900298 1118 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
d45c4172 1119 channel);
46900298
JB
1120 if (!bss)
1121 return;
1122
0c1ad2ca
JB
1123 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
1124
8cef2c9d
JB
1125 /* same for beacon and probe response */
1126 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
46900298
JB
1127
1128 /* check if we need to merge IBSS */
1129
46900298 1130 /* not an IBSS */
0c1ad2ca 1131 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
46900298
JB
1132 goto put_bss;
1133
1134 /* different channel */
55de908a 1135 if (sdata->u.ibss.fixed_channel &&
3aede78a 1136 sdata->u.ibss.chandef.chan != cbss->channel)
46900298
JB
1137 goto put_bss;
1138
1139 /* different SSID */
1140 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
1141 memcmp(elems->ssid, sdata->u.ibss.ssid,
1142 sdata->u.ibss.ssid_len))
1143 goto put_bss;
1144
cd7760e6 1145 /* process channel switch */
82a8e17d
LC
1146 if (sdata->vif.csa_active ||
1147 ieee80211_ibss_process_chanswitch(sdata, elems, true))
cd7760e6
SW
1148 goto put_bss;
1149
34e8f082 1150 /* same BSSID */
b203ca39 1151 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
34e8f082
AF
1152 goto put_bss;
1153
cd7760e6
SW
1154 /* we use a fixed BSSID */
1155 if (sdata->u.ibss.fixed_bssid)
1156 goto put_bss;
1157
f4bda337
TP
1158 if (ieee80211_have_rx_timestamp(rx_status)) {
1159 /* time when timestamp field was received */
1160 rx_timestamp =
1161 ieee80211_calculate_rx_timestamp(local, rx_status,
1162 len + FCS_LEN, 24);
24487981
JB
1163 } else {
1164 /*
1165 * second best option: get current TSF
1166 * (will return -1 if not supported)
1167 */
37a41b4a 1168 rx_timestamp = drv_get_tsf(local, sdata);
24487981 1169 }
46900298 1170
bdcbd8e0
JB
1171 ibss_dbg(sdata,
1172 "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
1173 mgmt->sa, mgmt->bssid,
1174 (unsigned long long)rx_timestamp,
1175 (unsigned long long)beacon_timestamp,
1176 (unsigned long long)(rx_timestamp - beacon_timestamp),
1177 jiffies);
46900298
JB
1178
1179 if (beacon_timestamp > rx_timestamp) {
bdcbd8e0
JB
1180 ibss_dbg(sdata,
1181 "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
1182 mgmt->bssid);
46900298 1183 ieee80211_sta_join_ibss(sdata, bss);
2103dec1 1184 supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL);
34e89507 1185 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
52874a5e 1186 supp_rates);
8bf11d8d 1187 rcu_read_unlock();
46900298
JB
1188 }
1189
1190 put_bss:
1191 ieee80211_rx_bss_put(local, bss);
1192}
1193
8bf11d8d
JB
1194void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
1195 const u8 *bssid, const u8 *addr,
1196 u32 supp_rates)
46900298 1197{
2e10d330 1198 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298
JB
1199 struct ieee80211_local *local = sdata->local;
1200 struct sta_info *sta;
55de908a 1201 struct ieee80211_chanctx_conf *chanctx_conf;
b422c6cd 1202 struct ieee80211_supported_band *sband;
74608aca 1203 enum nl80211_bss_scan_width scan_width;
55de908a 1204 int band;
46900298 1205
af8cdcd8
JB
1206 /*
1207 * XXX: Consider removing the least recently used entry and
1208 * allow new one to be added.
1209 */
46900298 1210 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
bdcbd8e0 1211 net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
e87cc472 1212 sdata->name, addr);
8bf11d8d 1213 return;
46900298
JB
1214 }
1215
2e10d330 1216 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
8bf11d8d 1217 return;
2e10d330 1218
b203ca39 1219 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
8bf11d8d 1220 return;
46900298 1221
55de908a
JB
1222 rcu_read_lock();
1223 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1224 if (WARN_ON_ONCE(!chanctx_conf)) {
1225 rcu_read_unlock();
1226 return;
1227 }
4bf88530 1228 band = chanctx_conf->def.chan->band;
74608aca 1229 scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def);
55de908a
JB
1230 rcu_read_unlock();
1231
8bf11d8d 1232 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
46900298 1233 if (!sta)
8bf11d8d 1234 return;
46900298 1235
c8716d9d 1236 sta->last_rx = jiffies;
d9a7ddb0 1237
46900298 1238 /* make sure mandatory rates are always added */
b422c6cd 1239 sband = local->hw.wiphy->bands[band];
46900298 1240 sta->sta.supp_rates[band] = supp_rates |
74608aca 1241 ieee80211_mandatory_rates(sband, scan_width);
46900298 1242
8bf11d8d
JB
1243 spin_lock(&ifibss->incomplete_lock);
1244 list_add(&sta->list, &ifibss->incomplete_stations);
1245 spin_unlock(&ifibss->incomplete_lock);
1246 ieee80211_queue_work(&local->hw, &sdata->work);
46900298
JB
1247}
1248
dad9defd
AQ
1249static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
1250{
1251 struct ieee80211_local *local = sdata->local;
1252 struct sta_info *sta, *tmp;
1253 unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
1254 unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
1255
1256 mutex_lock(&local->sta_mtx);
1257
1258 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1259 if (sdata != sta->sdata)
1260 continue;
1261
1262 if (time_after(jiffies, sta->last_rx + exp_time) ||
1263 (time_after(jiffies, sta->last_rx + exp_rsn_time) &&
1264 sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
1265 sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
1266 sta->sta_state != IEEE80211_STA_AUTHORIZED ?
1267 "not authorized " : "", sta->sta.addr);
1268
1269 WARN_ON(__sta_info_destroy(sta));
1270 }
1271 }
1272
1273 mutex_unlock(&local->sta_mtx);
1274}
1275
ce9058ae
BP
1276/*
1277 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
1278 */
46900298
JB
1279
1280static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1281{
1282 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
7ca15a0a 1283 enum nl80211_bss_scan_width scan_width;
46900298 1284
8d61ffa5 1285 sdata_assert_lock(sdata);
7a17a33c 1286
af8cdcd8
JB
1287 mod_timer(&ifibss->timer,
1288 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 1289
dad9defd 1290 ieee80211_ibss_sta_expire(sdata);
af8cdcd8 1291
450aae3d
S
1292 if (time_before(jiffies, ifibss->last_scan_completed +
1293 IEEE80211_IBSS_MERGE_INTERVAL))
1294 return;
1295
46900298
JB
1296 if (ieee80211_sta_active_ibss(sdata))
1297 return;
1298
c037b836 1299 if (ifibss->fixed_channel)
46900298
JB
1300 return;
1301
bdcbd8e0
JB
1302 sdata_info(sdata,
1303 "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n");
46900298 1304
7ca15a0a 1305 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
34bcf715 1306 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
76bed0f4 1307 NULL, 0, scan_width);
46900298
JB
1308}
1309
af8cdcd8 1310static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
1311{
1312 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 1313 u8 bssid[ETH_ALEN];
46900298
JB
1314 u16 capability;
1315 int i;
1316
8d61ffa5 1317 sdata_assert_lock(sdata);
7a17a33c 1318
af8cdcd8 1319 if (ifibss->fixed_bssid) {
46900298
JB
1320 memcpy(bssid, ifibss->bssid, ETH_ALEN);
1321 } else {
1322 /* Generate random, not broadcast, locally administered BSSID. Mix in
1323 * own MAC address to make sure that devices that do not have proper
1324 * random number generator get different BSSID. */
1325 get_random_bytes(bssid, ETH_ALEN);
1326 for (i = 0; i < ETH_ALEN; i++)
47846c9b 1327 bssid[i] ^= sdata->vif.addr[i];
46900298
JB
1328 bssid[0] &= ~0x01;
1329 bssid[0] |= 0x02;
1330 }
1331
bdcbd8e0 1332 sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid);
46900298 1333
46900298
JB
1334 capability = WLAN_CAPABILITY_IBSS;
1335
fffd0934 1336 if (ifibss->privacy)
46900298 1337 capability |= WLAN_CAPABILITY_PRIVACY;
46900298 1338
57c4d7b4 1339 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
75a423f4 1340 &ifibss->chandef, ifibss->basic_rates,
c13a765b 1341 capability, 0, true);
46900298
JB
1342}
1343
76bed0f4
JD
1344static unsigned ibss_setup_channels(struct wiphy *wiphy,
1345 struct ieee80211_channel **channels,
1346 unsigned int channels_max,
1347 u32 center_freq, u32 width)
1348{
1349 struct ieee80211_channel *chan = NULL;
1350 unsigned int n_chan = 0;
1351 u32 start_freq, end_freq, freq;
1352
1353 if (width <= 20) {
1354 start_freq = center_freq;
1355 end_freq = center_freq;
1356 } else {
1357 start_freq = center_freq - width / 2 + 10;
1358 end_freq = center_freq + width / 2 - 10;
1359 }
1360
1361 for (freq = start_freq; freq <= end_freq; freq += 20) {
1362 chan = ieee80211_get_channel(wiphy, freq);
1363 if (!chan)
1364 continue;
1365 if (n_chan >= channels_max)
1366 return n_chan;
1367
1368 channels[n_chan] = chan;
1369 n_chan++;
1370 }
1371
1372 return n_chan;
1373}
1374
1375static unsigned int
1376ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1377 const struct cfg80211_chan_def *chandef,
1378 struct ieee80211_channel **channels,
1379 unsigned int channels_max)
1380{
1381 unsigned int n_chan = 0;
1382 u32 width, cf1, cf2 = 0;
1383
1384 switch (chandef->width) {
1385 case NL80211_CHAN_WIDTH_40:
1386 width = 40;
1387 break;
1388 case NL80211_CHAN_WIDTH_80P80:
1389 cf2 = chandef->center_freq2;
1390 /* fall through */
1391 case NL80211_CHAN_WIDTH_80:
1392 width = 80;
1393 break;
1394 case NL80211_CHAN_WIDTH_160:
1395 width = 160;
1396 break;
1397 default:
1398 width = 20;
1399 break;
1400 }
1401
1402 cf1 = chandef->center_freq1;
1403
1404 n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1405
1406 if (cf2)
1407 n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1408 channels_max - n_chan, cf2,
1409 width);
1410
1411 return n_chan;
1412}
1413
ce9058ae
BP
1414/*
1415 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1416 */
1417
af8cdcd8 1418static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
1419{
1420 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1421 struct ieee80211_local *local = sdata->local;
0c1ad2ca 1422 struct cfg80211_bss *cbss;
af8cdcd8 1423 struct ieee80211_channel *chan = NULL;
46900298 1424 const u8 *bssid = NULL;
7ca15a0a 1425 enum nl80211_bss_scan_width scan_width;
46900298
JB
1426 int active_ibss;
1427
8d61ffa5 1428 sdata_assert_lock(sdata);
7a17a33c 1429
46900298 1430 active_ibss = ieee80211_sta_active_ibss(sdata);
bdcbd8e0 1431 ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss);
46900298
JB
1432
1433 if (active_ibss)
af8cdcd8 1434 return;
46900298 1435
af8cdcd8
JB
1436 if (ifibss->fixed_bssid)
1437 bssid = ifibss->bssid;
1438 if (ifibss->fixed_channel)
3aede78a 1439 chan = ifibss->chandef.chan;
af8cdcd8 1440 if (!is_zero_ether_addr(ifibss->bssid))
46900298 1441 bssid = ifibss->bssid;
0c1ad2ca
JB
1442 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1443 ifibss->ssid, ifibss->ssid_len,
6eb18137
DL
1444 IEEE80211_BSS_TYPE_IBSS,
1445 IEEE80211_PRIVACY(ifibss->privacy));
0c1ad2ca
JB
1446
1447 if (cbss) {
1448 struct ieee80211_bss *bss;
46900298 1449
0c1ad2ca 1450 bss = (void *)cbss->priv;
bdcbd8e0
JB
1451 ibss_dbg(sdata,
1452 "sta_find_ibss: selected %pM current %pM\n",
1453 cbss->bssid, ifibss->bssid);
1454 sdata_info(sdata,
1455 "Selected IBSS BSSID %pM based on configured SSID\n",
1456 cbss->bssid);
46900298 1457
af8cdcd8 1458 ieee80211_sta_join_ibss(sdata, bss);
46900298 1459 ieee80211_rx_bss_put(local, bss);
af8cdcd8 1460 return;
d419b9f0 1461 }
46900298 1462
d8eb741e
AQ
1463 /* if a fixed bssid and a fixed freq have been provided create the IBSS
1464 * directly and do not waste time scanning
1465 */
1466 if (ifibss->fixed_bssid && ifibss->fixed_channel) {
1467 sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
1468 bssid);
1469 ieee80211_sta_create_ibss(sdata);
1470 return;
1471 }
1472
1473
bdcbd8e0 1474 ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
46900298
JB
1475
1476 /* Selected IBSS not found in current scan results - try to scan */
ce9058ae 1477 if (time_after(jiffies, ifibss->last_scan_completed +
46900298 1478 IEEE80211_SCAN_INTERVAL)) {
76bed0f4
JD
1479 struct ieee80211_channel *channels[8];
1480 unsigned int num;
1481
bdcbd8e0 1482 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
46900298 1483
76bed0f4
JD
1484 num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1485 &ifibss->chandef,
1486 channels,
1487 ARRAY_SIZE(channels));
7ca15a0a 1488 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
34bcf715 1489 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
76bed0f4 1490 ifibss->ssid_len, channels, num,
7ca15a0a 1491 scan_width);
ce9058ae 1492 } else {
46900298
JB
1493 int interval = IEEE80211_SCAN_INTERVAL;
1494
1495 if (time_after(jiffies, ifibss->ibss_join_req +
55de908a
JB
1496 IEEE80211_IBSS_JOIN_TIMEOUT))
1497 ieee80211_sta_create_ibss(sdata);
46900298 1498
af8cdcd8
JB
1499 mod_timer(&ifibss->timer,
1500 round_jiffies(jiffies + interval));
46900298 1501 }
46900298
JB
1502}
1503
1504static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
c269a203 1505 struct sk_buff *req)
46900298 1506{
c269a203 1507 struct ieee80211_mgmt *mgmt = (void *)req->data;
46900298
JB
1508 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1509 struct ieee80211_local *local = sdata->local;
c269a203 1510 int tx_last_beacon, len = req->len;
46900298 1511 struct sk_buff *skb;
c3ffeab4 1512 struct beacon_data *presp;
46900298
JB
1513 u8 *pos, *end;
1514
8d61ffa5 1515 sdata_assert_lock(sdata);
7a17a33c 1516
40b275b6 1517 presp = rcu_dereference_protected(ifibss->presp,
8d61ffa5 1518 lockdep_is_held(&sdata->wdev.mtx));
40b275b6 1519
46900298 1520 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
40b275b6 1521 len < 24 + 2 || !presp)
46900298
JB
1522 return;
1523
24487981 1524 tx_last_beacon = drv_tx_last_beacon(local);
46900298 1525
bdcbd8e0
JB
1526 ibss_dbg(sdata,
1527 "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
1528 mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon);
46900298 1529
1ed76487 1530 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
46900298
JB
1531 return;
1532
b203ca39 1533 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
888d04df 1534 !is_broadcast_ether_addr(mgmt->bssid))
46900298
JB
1535 return;
1536
1537 end = ((u8 *) mgmt) + len;
1538 pos = mgmt->u.probe_req.variable;
1539 if (pos[0] != WLAN_EID_SSID ||
1540 pos + 2 + pos[1] > end) {
bdcbd8e0
JB
1541 ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n",
1542 mgmt->sa);
46900298
JB
1543 return;
1544 }
1545 if (pos[1] != 0 &&
1546 (pos[1] != ifibss->ssid_len ||
0da780c2 1547 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
46900298
JB
1548 /* Ignore ProbeReq for foreign SSID */
1549 return;
1550 }
1551
1552 /* Reply with ProbeResp */
c3ffeab4 1553 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
46900298
JB
1554 if (!skb)
1555 return;
1556
c3ffeab4
JB
1557 skb_reserve(skb, local->tx_headroom);
1558 memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len);
1559
1560 memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN);
1561 ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa);
62ae67be 1562 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
691eb61b
SW
1563
1564 /* avoid excessive retries for probe request to wildcard SSIDs */
1565 if (pos[1] == 0)
1566 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_NO_ACK;
1567
62ae67be 1568 ieee80211_tx_skb(sdata, skb);
46900298
JB
1569}
1570
d45c4172
EG
1571static
1572void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata,
1573 struct ieee80211_mgmt *mgmt, size_t len,
1574 struct ieee80211_rx_status *rx_status)
46900298
JB
1575{
1576 size_t baselen;
1577 struct ieee802_11_elems elems;
1578
d45c4172
EG
1579 BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) !=
1580 offsetof(typeof(mgmt->u.beacon), variable));
1581
1582 /*
1583 * either beacon or probe_resp but the variable field is at the
1584 * same offset
1585 */
46900298
JB
1586 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1587 if (baselen > len)
1588 return;
1589
1590 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
b2e506bf 1591 false, &elems);
46900298 1592
d45c4172 1593 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
46900298
JB
1594}
1595
1fa57d01
JB
1596void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1597 struct sk_buff *skb)
46900298
JB
1598{
1599 struct ieee80211_rx_status *rx_status;
1600 struct ieee80211_mgmt *mgmt;
1601 u16 fc;
cd7760e6
SW
1602 struct ieee802_11_elems elems;
1603 int ies_len;
46900298 1604
f1d58c25 1605 rx_status = IEEE80211_SKB_RXCB(skb);
46900298
JB
1606 mgmt = (struct ieee80211_mgmt *) skb->data;
1607 fc = le16_to_cpu(mgmt->frame_control);
1608
8d61ffa5 1609 sdata_lock(sdata);
7a17a33c 1610
c926d006
TH
1611 if (!sdata->u.ibss.ssid_len)
1612 goto mgmt_out; /* not ready to merge yet */
1613
46900298
JB
1614 switch (fc & IEEE80211_FCTL_STYPE) {
1615 case IEEE80211_STYPE_PROBE_REQ:
c269a203 1616 ieee80211_rx_mgmt_probe_req(sdata, skb);
46900298
JB
1617 break;
1618 case IEEE80211_STYPE_PROBE_RESP:
46900298 1619 case IEEE80211_STYPE_BEACON:
d45c4172
EG
1620 ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len,
1621 rx_status);
46900298
JB
1622 break;
1623 case IEEE80211_STYPE_AUTH:
1624 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
1625 break;
2cc59e78
AQ
1626 case IEEE80211_STYPE_DEAUTH:
1627 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1628 break;
cd7760e6
SW
1629 case IEEE80211_STYPE_ACTION:
1630 switch (mgmt->u.action.category) {
1631 case WLAN_CATEGORY_SPECTRUM_MGMT:
1632 ies_len = skb->len -
1633 offsetof(struct ieee80211_mgmt,
1634 u.action.u.chan_switch.variable);
1635
1636 if (ies_len < 0)
1637 break;
1638
1639 ieee802_11_parse_elems(
1640 mgmt->u.action.u.chan_switch.variable,
1641 ies_len, true, &elems);
1642
1643 if (elems.parse_error)
1644 break;
1645
1646 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
1647 rx_status, &elems);
1648 break;
1649 }
46900298 1650 }
7a17a33c 1651
c926d006 1652 mgmt_out:
8d61ffa5 1653 sdata_unlock(sdata);
46900298
JB
1654}
1655
1fa57d01 1656void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
46900298 1657{
1fa57d01 1658 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
8bf11d8d 1659 struct sta_info *sta;
46900298 1660
8d61ffa5 1661 sdata_lock(sdata);
7a17a33c
JB
1662
1663 /*
1664 * Work could be scheduled after scan or similar
1665 * when we aren't even joined (or trying) with a
1666 * network.
1667 */
1668 if (!ifibss->ssid_len)
1669 goto out;
46900298 1670
8bf11d8d
JB
1671 spin_lock_bh(&ifibss->incomplete_lock);
1672 while (!list_empty(&ifibss->incomplete_stations)) {
1673 sta = list_first_entry(&ifibss->incomplete_stations,
1674 struct sta_info, list);
1675 list_del(&sta->list);
1676 spin_unlock_bh(&ifibss->incomplete_lock);
1677
52874a5e 1678 ieee80211_ibss_finish_sta(sta);
8bf11d8d
JB
1679 rcu_read_unlock();
1680 spin_lock_bh(&ifibss->incomplete_lock);
1681 }
1682 spin_unlock_bh(&ifibss->incomplete_lock);
1683
46900298
JB
1684 switch (ifibss->state) {
1685 case IEEE80211_IBSS_MLME_SEARCH:
1686 ieee80211_sta_find_ibss(sdata);
1687 break;
1688 case IEEE80211_IBSS_MLME_JOINED:
1689 ieee80211_sta_merge_ibss(sdata);
1690 break;
1691 default:
1692 WARN_ON(1);
1693 break;
1694 }
46900298 1695
7a17a33c 1696 out:
8d61ffa5 1697 sdata_unlock(sdata);
3a4d4aa2
JB
1698}
1699
46900298
JB
1700static void ieee80211_ibss_timer(unsigned long data)
1701{
1702 struct ieee80211_sub_if_data *sdata =
1703 (struct ieee80211_sub_if_data *) data;
5bb644a0 1704
a6182943 1705 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
5bb644a0 1706}
5bb644a0 1707
46900298
JB
1708void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1709{
1710 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1711
46900298
JB
1712 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1713 (unsigned long) sdata);
8bf11d8d
JB
1714 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1715 spin_lock_init(&ifibss->incomplete_lock);
cd7760e6
SW
1716 INIT_WORK(&ifibss->csa_connection_drop_work,
1717 ieee80211_csa_connection_drop_work);
46900298
JB
1718}
1719
1720/* scan finished notification */
1721void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1722{
af8cdcd8 1723 struct ieee80211_sub_if_data *sdata;
46900298 1724
29b4a4f7
JB
1725 mutex_lock(&local->iflist_mtx);
1726 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 1727 if (!ieee80211_sdata_running(sdata))
0e41f715 1728 continue;
af8cdcd8
JB
1729 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1730 continue;
1731 sdata->u.ibss.last_scan_completed = jiffies;
7a17a33c 1732 ieee80211_queue_work(&local->hw, &sdata->work);
46900298 1733 }
29b4a4f7 1734 mutex_unlock(&local->iflist_mtx);
46900298
JB
1735}
1736
af8cdcd8
JB
1737int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1738 struct cfg80211_ibss_params *params)
1739{
ff3cc5f4 1740 u32 changed = 0;
2103dec1
SW
1741 u32 rate_flags;
1742 struct ieee80211_supported_band *sband;
71965c1d
LC
1743 enum ieee80211_chanctx_mode chanmode;
1744 struct ieee80211_local *local = sdata->local;
1745 int radar_detect_width = 0;
2103dec1 1746 int i;
71965c1d
LC
1747 int ret;
1748
1749 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1750 &params->chandef,
1751 sdata->wdev.iftype);
1752 if (ret < 0)
1753 return ret;
1754
1755 if (ret > 0) {
1756 if (!params->userspace_handles_dfs)
1757 return -EINVAL;
1758 radar_detect_width = BIT(params->chandef.width);
1759 }
1760
1761 chanmode = (params->channel_fixed && !ret) ?
1762 IEEE80211_CHANCTX_SHARED : IEEE80211_CHANCTX_EXCLUSIVE;
1763
1764 mutex_lock(&local->chanctx_mtx);
1765 ret = ieee80211_check_combinations(sdata, &params->chandef, chanmode,
1766 radar_detect_width);
1767 mutex_unlock(&local->chanctx_mtx);
1768 if (ret < 0)
1769 return ret;
af8cdcd8 1770
af8cdcd8
JB
1771 if (params->bssid) {
1772 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1773 sdata->u.ibss.fixed_bssid = true;
1774 } else
1775 sdata->u.ibss.fixed_bssid = false;
1776
fffd0934 1777 sdata->u.ibss.privacy = params->privacy;
267335d6 1778 sdata->u.ibss.control_port = params->control_port;
8e8d347d 1779 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
fbd2c8dc 1780 sdata->u.ibss.basic_rates = params->basic_rates;
c7d37a66 1781 sdata->u.ibss.last_scan_completed = jiffies;
2103dec1
SW
1782
1783 /* fix basic_rates if channel does not support these rates */
1784 rate_flags = ieee80211_chandef_rate_flags(&params->chandef);
71965c1d 1785 sband = local->hw.wiphy->bands[params->chandef.chan->band];
2103dec1
SW
1786 for (i = 0; i < sband->n_bitrates; i++) {
1787 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1788 sdata->u.ibss.basic_rates &= ~BIT(i);
1789 }
dd5b4cc7
FF
1790 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1791 sizeof(params->mcast_rate));
fffd0934 1792
57c4d7b4
JB
1793 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1794
3aede78a 1795 sdata->u.ibss.chandef = params->chandef;
af8cdcd8
JB
1796 sdata->u.ibss.fixed_channel = params->channel_fixed;
1797
1798 if (params->ie) {
1799 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1800 GFP_KERNEL);
1801 if (sdata->u.ibss.ie)
1802 sdata->u.ibss.ie_len = params->ie_len;
1803 }
1804
af8cdcd8
JB
1805 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1806 sdata->u.ibss.ibss_join_req = jiffies;
1807
badecb00 1808 memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len);
0e41f715
JB
1809 sdata->u.ibss.ssid_len = params->ssid_len;
1810
822854b0
SW
1811 memcpy(&sdata->u.ibss.ht_capa, &params->ht_capa,
1812 sizeof(sdata->u.ibss.ht_capa));
1813 memcpy(&sdata->u.ibss.ht_capa_mask, &params->ht_capa_mask,
1814 sizeof(sdata->u.ibss.ht_capa_mask));
1815
ff3cc5f4
SW
1816 /*
1817 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1818 * reserved, but an HT STA shall protect HT transmissions as though
1819 * the HT Protection field were set to non-HT mixed mode.
1820 *
1821 * In an IBSS, the RIFS Mode field of the HT Operation element is
1822 * also reserved, but an HT STA shall operate as though this field
1823 * were set to 1.
1824 */
1825
1826 sdata->vif.bss_conf.ht_operation_mode |=
1827 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1828 | IEEE80211_HT_PARAM_RIFS_MODE;
1829
1830 changed |= BSS_CHANGED_HT;
1831 ieee80211_bss_info_change_notify(sdata, changed);
1832
04ecd257 1833 sdata->smps_mode = IEEE80211_SMPS_OFF;
71965c1d 1834 sdata->needed_rx_chains = local->rx_chains;
04ecd257 1835
71965c1d 1836 ieee80211_queue_work(&local->hw, &sdata->work);
af8cdcd8
JB
1837
1838 return 0;
1839}
1840
1841int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1842{
5ea096c0 1843 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
5ea096c0 1844
871a4180 1845 ieee80211_ibss_disconnect(sdata);
b78a4932 1846 ifibss->ssid_len = 0;
c84a67a2 1847 eth_zero_addr(ifibss->bssid);
af8cdcd8
JB
1848
1849 /* remove beacon */
1850 kfree(sdata->u.ibss.ie);
822854b0
SW
1851
1852 /* on the next join, re-program HT parameters */
1853 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1854 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1855
af8cdcd8 1856 synchronize_rcu();
af8cdcd8 1857
35f20c14 1858 skb_queue_purge(&sdata->skb_queue);
5cff20e6 1859
bc05d19f 1860 del_timer_sync(&sdata->u.ibss.timer);
7a17a33c 1861
af8cdcd8
JB
1862 return 0;
1863}