]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/mac80211/ibss.c
mac80211: move ieee80211_set_channel function
[mirror_ubuntu-artful-kernel.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>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/delay.h>
5a0e3ad6 16#include <linux/slab.h>
46900298
JB
17#include <linux/if_ether.h>
18#include <linux/skbuff.h>
19#include <linux/if_arp.h>
20#include <linux/etherdevice.h>
21#include <linux/rtnetlink.h>
22#include <net/mac80211.h>
46900298
JB
23
24#include "ieee80211_i.h"
24487981 25#include "driver-ops.h"
46900298
JB
26#include "rate.h"
27
28#define IEEE80211_SCAN_INTERVAL (2 * HZ)
29#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
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)
34
35#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
36
37
af8cdcd8
JB
38static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
39 const u8 *bssid, const int beacon_int,
40 struct ieee80211_channel *chan,
b59066a2 41 const u32 basic_rates,
af8cdcd8 42 const u16 capability, u64 tsf)
46900298
JB
43{
44 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
45 struct ieee80211_local *local = sdata->local;
b59066a2 46 int rates, i;
46900298
JB
47 struct sk_buff *skb;
48 struct ieee80211_mgmt *mgmt;
49 u8 *pos;
50 struct ieee80211_supported_band *sband;
f446d10f 51 struct cfg80211_bss *bss;
57c4d7b4 52 u32 bss_change;
b59066a2 53 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
13c40c54 54 enum nl80211_channel_type channel_type;
24487981 55
7a17a33c
JB
56 lockdep_assert_held(&ifibss->mtx);
57
24487981 58 /* Reset own TSF to allow time synchronization work. */
37a41b4a 59 drv_reset_tsf(local, sdata);
46900298 60
af8cdcd8 61 skb = ifibss->skb;
a9b3cd7f 62 RCU_INIT_POINTER(ifibss->presp, NULL);
af8cdcd8
JB
63 synchronize_rcu();
64 skb->data = skb->head;
65 skb->len = 0;
66 skb_reset_tail_pointer(skb);
67 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
46900298 68
b203ca39 69 if (!ether_addr_equal(ifibss->bssid, bssid))
af8cdcd8 70 sta_info_flush(sdata->local, sdata);
46900298 71
49b5c7f4
JB
72 /* if merging, indicate to driver that we leave the old IBSS */
73 if (sdata->vif.bss_conf.ibss_joined) {
74 sdata->vif.bss_conf.ibss_joined = false;
86a2ea41 75 netif_carrier_off(sdata->dev);
49b5c7f4
JB
76 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
77 }
78
46900298 79 memcpy(ifibss->bssid, bssid, ETH_ALEN);
46900298 80
af8cdcd8 81 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
46900298 82
ba196025 83 local->oper_channel = chan;
13c40c54
AS
84 channel_type = ifibss->channel_type;
85 if (channel_type > NL80211_CHAN_HT20 &&
86 !cfg80211_can_beacon_sec_chan(local->hw.wiphy, chan, channel_type))
87 channel_type = NL80211_CHAN_HT20;
88 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
89 /* can only fail due to HT40+/- mismatch */
90 channel_type = NL80211_CHAN_HT20;
91 WARN_ON(!ieee80211_set_channel_type(local, sdata,
92 NL80211_CHAN_HT20));
93 }
af8cdcd8 94 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
57c4d7b4 95
af8cdcd8 96 sband = local->hw.wiphy->bands[chan->band];
46900298 97
b59066a2
JB
98 /* build supported rates array */
99 pos = supp_rates;
100 for (i = 0; i < sband->n_bitrates; i++) {
101 int rate = sband->bitrates[i].bitrate;
102 u8 basic = 0;
103 if (basic_rates & BIT(i))
104 basic = 0x80;
105 *pos++ = basic | (u8) (rate / 5);
106 }
107
46900298 108 /* Build IBSS probe response */
af8cdcd8 109 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
46900298
JB
110 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
111 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
112 IEEE80211_STYPE_PROBE_RESP);
113 memset(mgmt->da, 0xff, ETH_ALEN);
47846c9b 114 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298 115 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
57c4d7b4 116 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
707c1b4e 117 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
46900298
JB
118 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
119
120 pos = skb_put(skb, 2 + ifibss->ssid_len);
121 *pos++ = WLAN_EID_SSID;
122 *pos++ = ifibss->ssid_len;
123 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
124
b59066a2 125 rates = sband->n_bitrates;
46900298
JB
126 if (rates > 8)
127 rates = 8;
128 pos = skb_put(skb, 2 + rates);
129 *pos++ = WLAN_EID_SUPP_RATES;
130 *pos++ = rates;
131 memcpy(pos, supp_rates, rates);
132
133 if (sband->band == IEEE80211_BAND_2GHZ) {
134 pos = skb_put(skb, 2 + 1);
135 *pos++ = WLAN_EID_DS_PARAMS;
136 *pos++ = 1;
af8cdcd8 137 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
46900298
JB
138 }
139
140 pos = skb_put(skb, 2 + 2);
141 *pos++ = WLAN_EID_IBSS_PARAMS;
142 *pos++ = 2;
143 /* FIX: set ATIM window based on scan results */
144 *pos++ = 0;
145 *pos++ = 0;
146
b59066a2
JB
147 if (sband->n_bitrates > 8) {
148 rates = sband->n_bitrates - 8;
46900298
JB
149 pos = skb_put(skb, 2 + rates);
150 *pos++ = WLAN_EID_EXT_SUPP_RATES;
151 *pos++ = rates;
152 memcpy(pos, &supp_rates[8], rates);
153 }
154
af8cdcd8
JB
155 if (ifibss->ie_len)
156 memcpy(skb_put(skb, ifibss->ie_len),
157 ifibss->ie, ifibss->ie_len);
158
13c40c54
AS
159 /* add HT capability and information IEs */
160 if (channel_type && sband->ht_cap.ht_supported) {
161 pos = skb_put(skb, 4 +
162 sizeof(struct ieee80211_ht_cap) +
074d46d1 163 sizeof(struct ieee80211_ht_operation));
13c40c54
AS
164 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
165 sband->ht_cap.cap);
0d894ec5
AN
166 /*
167 * Note: According to 802.11n-2009 9.13.3.1, HT Protection
168 * field and RIFS Mode are reserved in IBSS mode, therefore
169 * keep them at 0
170 */
074d46d1 171 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
431e3154 172 chan, channel_type, 0);
13c40c54
AS
173 }
174
32c5057b 175 if (local->hw.queues >= IEEE80211_NUM_ACS) {
9eba6125
BR
176 pos = skb_put(skb, 9);
177 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
178 *pos++ = 7; /* len */
179 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
180 *pos++ = 0x50;
181 *pos++ = 0xf2;
182 *pos++ = 2; /* WME */
183 *pos++ = 0; /* WME info */
184 *pos++ = 1; /* WME ver */
185 *pos++ = 0; /* U-APSD no in use */
186 }
187
cf778b00 188 rcu_assign_pointer(ifibss->presp, skb);
46900298 189
2d0ddec5 190 sdata->vif.bss_conf.beacon_int = beacon_int;
fbd2c8dc 191 sdata->vif.bss_conf.basic_rates = basic_rates;
2d0ddec5
JB
192 bss_change = BSS_CHANGED_BEACON_INT;
193 bss_change |= ieee80211_reset_erp_info(sdata);
194 bss_change |= BSS_CHANGED_BSSID;
195 bss_change |= BSS_CHANGED_BEACON;
196 bss_change |= BSS_CHANGED_BEACON_ENABLED;
392cfdb1 197 bss_change |= BSS_CHANGED_BASIC_RATES;
13c40c54 198 bss_change |= BSS_CHANGED_HT;
8fc214ba
JB
199 bss_change |= BSS_CHANGED_IBSS;
200 sdata->vif.bss_conf.ibss_joined = true;
2d0ddec5 201 ieee80211_bss_info_change_notify(sdata, bss_change);
46900298 202
b59066a2 203 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
46900298 204
46900298 205 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
af8cdcd8
JB
206 mod_timer(&ifibss->timer,
207 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 208
f446d10f
JB
209 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
210 mgmt, skb->len, 0, GFP_KERNEL);
211 cfg80211_put_bss(bss);
86a2ea41 212 netif_carrier_on(sdata->dev);
af8cdcd8 213 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
46900298
JB
214}
215
af8cdcd8
JB
216static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
217 struct ieee80211_bss *bss)
46900298 218{
0c1ad2ca
JB
219 struct cfg80211_bss *cbss =
220 container_of((void *)bss, struct cfg80211_bss, priv);
b59066a2
JB
221 struct ieee80211_supported_band *sband;
222 u32 basic_rates;
223 int i, j;
0c1ad2ca 224 u16 beacon_int = cbss->beacon_interval;
57c4d7b4 225
7a17a33c
JB
226 lockdep_assert_held(&sdata->u.ibss.mtx);
227
57c4d7b4
JB
228 if (beacon_int < 10)
229 beacon_int = 10;
230
0c1ad2ca 231 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
b59066a2
JB
232
233 basic_rates = 0;
234
235 for (i = 0; i < bss->supp_rates_len; i++) {
236 int rate = (bss->supp_rates[i] & 0x7f) * 5;
237 bool is_basic = !!(bss->supp_rates[i] & 0x80);
238
239 for (j = 0; j < sband->n_bitrates; j++) {
240 if (sband->bitrates[j].bitrate == rate) {
241 if (is_basic)
242 basic_rates |= BIT(j);
243 break;
244 }
245 }
246 }
247
0c1ad2ca 248 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
57c4d7b4 249 beacon_int,
0c1ad2ca 250 cbss->channel,
b59066a2 251 basic_rates,
0c1ad2ca
JB
252 cbss->capability,
253 cbss->tsf);
46900298
JB
254}
255
6d810f10
AQ
256static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta,
257 bool auth)
8bf11d8d
JB
258 __acquires(RCU)
259{
260 struct ieee80211_sub_if_data *sdata = sta->sdata;
261 u8 addr[ETH_ALEN];
262
263 memcpy(addr, sta->sta.addr, ETH_ALEN);
264
265#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
266 wiphy_debug(sdata->local->hw.wiphy,
267 "Adding new IBSS station %pM (dev=%s)\n",
268 addr, sdata->name);
269#endif
270
83d5cc01
JB
271 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
272 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
267335d6
AQ
273 /* authorize the station only if the network is not RSN protected. If
274 * not wait for the userspace to authorize it */
275 if (!sta->sdata->u.ibss.control_port)
276 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
8bf11d8d
JB
277
278 rate_control_rate_init(sta);
279
280 /* If it fails, maybe we raced another insertion? */
281 if (sta_info_insert_rcu(sta))
282 return sta_info_get(sdata, addr);
6d810f10 283 if (auth) {
499f42bb
JP
284 ibss_vdbg("TX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=1)\n",
285 sdata->vif.addr, sdata->u.ibss.bssid, addr);
6d810f10
AQ
286 ieee80211_send_auth(sdata, 1, WLAN_AUTH_OPEN, NULL, 0,
287 addr, sdata->u.ibss.bssid, NULL, 0, 0);
288 }
8bf11d8d
JB
289 return sta;
290}
291
292static struct sta_info *
293ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
294 const u8 *bssid, const u8 *addr,
6d810f10 295 u32 supp_rates, bool auth)
8bf11d8d
JB
296 __acquires(RCU)
297{
298 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
299 struct ieee80211_local *local = sdata->local;
300 struct sta_info *sta;
301 int band = local->hw.conf.channel->band;
302
303 /*
304 * XXX: Consider removing the least recently used entry and
305 * allow new one to be added.
306 */
307 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
e87cc472
JP
308 net_dbg_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
309 sdata->name, addr);
8bf11d8d
JB
310 rcu_read_lock();
311 return NULL;
312 }
313
314 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
315 rcu_read_lock();
316 return NULL;
317 }
318
b203ca39 319 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) {
8bf11d8d
JB
320 rcu_read_lock();
321 return NULL;
322 }
323
324 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
325 if (!sta) {
326 rcu_read_lock();
327 return NULL;
328 }
329
330 sta->last_rx = jiffies;
331
332 /* make sure mandatory rates are always added */
333 sta->sta.supp_rates[band] = supp_rates |
334 ieee80211_mandatory_rates(local, band);
335
6d810f10
AQ
336 return ieee80211_ibss_finish_sta(sta, auth);
337}
338
339static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
340 struct ieee80211_mgmt *mgmt,
341 size_t len)
342{
343 u16 auth_alg, auth_transaction;
344
345 lockdep_assert_held(&sdata->u.ibss.mtx);
346
347 if (len < 24 + 6)
348 return;
349
350 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
351 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
352
353 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
354 return;
499f42bb
JP
355 ibss_vdbg("%s: RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n",
356 sdata->name, mgmt->sa, mgmt->da, mgmt->bssid,
357 auth_transaction);
6d810f10
AQ
358 sta_info_destroy_addr(sdata, mgmt->sa);
359 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, 0, false);
360 rcu_read_unlock();
361
362 /*
363 * IEEE 802.11 standard does not require authentication in IBSS
364 * networks and most implementations do not seem to use it.
365 * However, try to reply to authentication attempts if someone
366 * has actually implemented this.
367 */
368 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
369 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0);
8bf11d8d
JB
370}
371
46900298
JB
372static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
373 struct ieee80211_mgmt *mgmt,
374 size_t len,
375 struct ieee80211_rx_status *rx_status,
376 struct ieee802_11_elems *elems,
377 bool beacon)
378{
379 struct ieee80211_local *local = sdata->local;
380 int freq;
0c1ad2ca 381 struct cfg80211_bss *cbss;
46900298
JB
382 struct ieee80211_bss *bss;
383 struct sta_info *sta;
384 struct ieee80211_channel *channel;
385 u64 beacon_timestamp, rx_timestamp;
386 u32 supp_rates = 0;
387 enum ieee80211_band band = rx_status->band;
13c40c54
AS
388 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
389 bool rates_updated = false;
46900298
JB
390
391 if (elems->ds_params && elems->ds_params_len == 1)
59eb21a6
BR
392 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
393 band);
46900298
JB
394 else
395 freq = rx_status->freq;
396
397 channel = ieee80211_get_channel(local->hw.wiphy, freq);
398
399 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
400 return;
401
9eba6125 402 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
b203ca39 403 ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) {
46900298
JB
404
405 rcu_read_lock();
abe60632 406 sta = sta_info_get(sdata, mgmt->sa);
46900298 407
9eba6125
BR
408 if (elems->supp_rates) {
409 supp_rates = ieee80211_sta_get_rates(local, elems,
9ebb61a2 410 band, NULL);
9eba6125
BR
411 if (sta) {
412 u32 prev_rates;
413
414 prev_rates = sta->sta.supp_rates[band];
415 /* make sure mandatory rates are always added */
416 sta->sta.supp_rates[band] = supp_rates |
417 ieee80211_mandatory_rates(local, band);
46900298 418
9eba6125 419 if (sta->sta.supp_rates[band] != prev_rates) {
499f42bb
JP
420 ibss_vdbg("%s: updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
421 sdata->name, sta->sta.addr,
422 prev_rates,
423 sta->sta.supp_rates[band]);
13c40c54 424 rates_updated = true;
9eba6125 425 }
8bf11d8d
JB
426 } else {
427 rcu_read_unlock();
9eba6125 428 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
6d810f10 429 mgmt->sa, supp_rates, true);
8bf11d8d 430 }
34e89507 431 }
9eba6125
BR
432
433 if (sta && elems->wmm_info)
c2c98fde 434 set_sta_flag(sta, WLAN_STA_WME);
9eba6125 435
074d46d1 436 if (sta && elems->ht_operation && elems->ht_cap_elem &&
13c40c54
AS
437 sdata->u.ibss.channel_type != NL80211_CHAN_NO_HT) {
438 /* we both use HT */
439 struct ieee80211_sta_ht_cap sta_ht_cap_new;
440 enum nl80211_channel_type channel_type =
074d46d1
JB
441 ieee80211_ht_oper_to_channel_type(
442 elems->ht_operation);
13c40c54
AS
443
444 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
445 elems->ht_cap_elem,
446 &sta_ht_cap_new);
447
448 /*
449 * fall back to HT20 if we don't use or use
450 * the other extension channel
451 */
6741e7f0
FF
452 if (!(channel_type == NL80211_CHAN_HT40MINUS ||
453 channel_type == NL80211_CHAN_HT40PLUS) ||
13c40c54
AS
454 channel_type != sdata->u.ibss.channel_type)
455 sta_ht_cap_new.cap &=
456 ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
457
458 if (memcmp(&sta->sta.ht_cap, &sta_ht_cap_new,
459 sizeof(sta_ht_cap_new))) {
460 memcpy(&sta->sta.ht_cap, &sta_ht_cap_new,
461 sizeof(sta_ht_cap_new));
462 rates_updated = true;
463 }
464 }
465
466 if (sta && rates_updated)
467 rate_control_rate_init(sta);
468
9eba6125 469 rcu_read_unlock();
46900298
JB
470 }
471
472 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
473 channel, beacon);
474 if (!bss)
475 return;
476
0c1ad2ca
JB
477 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
478
46900298 479 /* was just updated in ieee80211_bss_info_update */
0c1ad2ca 480 beacon_timestamp = cbss->tsf;
46900298
JB
481
482 /* check if we need to merge IBSS */
483
46900298 484 /* we use a fixed BSSID */
a98bfec2 485 if (sdata->u.ibss.fixed_bssid)
46900298
JB
486 goto put_bss;
487
488 /* not an IBSS */
0c1ad2ca 489 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
46900298
JB
490 goto put_bss;
491
492 /* different channel */
0c1ad2ca 493 if (cbss->channel != local->oper_channel)
46900298
JB
494 goto put_bss;
495
496 /* different SSID */
497 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
498 memcmp(elems->ssid, sdata->u.ibss.ssid,
499 sdata->u.ibss.ssid_len))
500 goto put_bss;
501
34e8f082 502 /* same BSSID */
b203ca39 503 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
34e8f082
AF
504 goto put_bss;
505
6ebacbb7 506 if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
46900298
JB
507 /*
508 * For correct IBSS merging we need mactime; since mactime is
509 * defined as the time the first data symbol of the frame hits
510 * the PHY, and the timestamp of the beacon is defined as "the
511 * time that the data symbol containing the first bit of the
512 * timestamp is transmitted to the PHY plus the transmitting
513 * STA's delays through its local PHY from the MAC-PHY
514 * interface to its interface with the WM" (802.11 11.1.2)
515 * - equals the time this bit arrives at the receiver - we have
516 * to take into account the offset between the two.
517 *
518 * E.g. at 1 MBit that means mactime is 192 usec earlier
519 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
520 */
521 int rate;
522
523 if (rx_status->flag & RX_FLAG_HT)
524 rate = 65; /* TODO: HT rates */
525 else
526 rate = local->hw.wiphy->bands[band]->
527 bitrates[rx_status->rate_idx].bitrate;
528
529 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
24487981
JB
530 } else {
531 /*
532 * second best option: get current TSF
533 * (will return -1 if not supported)
534 */
37a41b4a 535 rx_timestamp = drv_get_tsf(local, sdata);
24487981 536 }
46900298 537
499f42bb
JP
538 ibss_vdbg("RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
539 mgmt->sa, mgmt->bssid,
540 (unsigned long long)rx_timestamp,
541 (unsigned long long)beacon_timestamp,
542 (unsigned long long)(rx_timestamp - beacon_timestamp),
543 jiffies);
46900298
JB
544
545 if (beacon_timestamp > rx_timestamp) {
499f42bb
JP
546 ibss_vdbg("%s: beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n",
547 sdata->name, mgmt->bssid);
46900298 548 ieee80211_sta_join_ibss(sdata, bss);
9ebb61a2 549 supp_rates = ieee80211_sta_get_rates(local, elems, band, NULL);
34e89507 550 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
6d810f10 551 supp_rates, true);
8bf11d8d 552 rcu_read_unlock();
46900298
JB
553 }
554
555 put_bss:
556 ieee80211_rx_bss_put(local, bss);
557}
558
8bf11d8d
JB
559void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
560 const u8 *bssid, const u8 *addr,
561 u32 supp_rates)
46900298 562{
2e10d330 563 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298
JB
564 struct ieee80211_local *local = sdata->local;
565 struct sta_info *sta;
566 int band = local->hw.conf.channel->band;
567
af8cdcd8
JB
568 /*
569 * XXX: Consider removing the least recently used entry and
570 * allow new one to be added.
571 */
46900298 572 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
e87cc472
JP
573 net_dbg_ratelimited("%s: No room for a new IBSS STA entry %pM\n",
574 sdata->name, addr);
8bf11d8d 575 return;
46900298
JB
576 }
577
2e10d330 578 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
8bf11d8d 579 return;
2e10d330 580
b203ca39 581 if (!ether_addr_equal(bssid, sdata->u.ibss.bssid))
8bf11d8d 582 return;
46900298 583
8bf11d8d 584 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
46900298 585 if (!sta)
8bf11d8d 586 return;
46900298 587
c8716d9d 588 sta->last_rx = jiffies;
d9a7ddb0 589
46900298
JB
590 /* make sure mandatory rates are always added */
591 sta->sta.supp_rates[band] = supp_rates |
592 ieee80211_mandatory_rates(local, band);
593
8bf11d8d
JB
594 spin_lock(&ifibss->incomplete_lock);
595 list_add(&sta->list, &ifibss->incomplete_stations);
596 spin_unlock(&ifibss->incomplete_lock);
597 ieee80211_queue_work(&local->hw, &sdata->work);
46900298
JB
598}
599
600static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
601{
602 struct ieee80211_local *local = sdata->local;
603 int active = 0;
604 struct sta_info *sta;
605
7a17a33c
JB
606 lockdep_assert_held(&sdata->u.ibss.mtx);
607
46900298
JB
608 rcu_read_lock();
609
610 list_for_each_entry_rcu(sta, &local->sta_list, list) {
611 if (sta->sdata == sdata &&
612 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
613 jiffies)) {
614 active++;
615 break;
616 }
617 }
618
619 rcu_read_unlock();
620
621 return active;
622}
623
ce9058ae
BP
624/*
625 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
626 */
46900298
JB
627
628static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
629{
630 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
631
7a17a33c
JB
632 lockdep_assert_held(&ifibss->mtx);
633
af8cdcd8
JB
634 mod_timer(&ifibss->timer,
635 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298
JB
636
637 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
af8cdcd8 638
450aae3d
S
639 if (time_before(jiffies, ifibss->last_scan_completed +
640 IEEE80211_IBSS_MERGE_INTERVAL))
641 return;
642
46900298
JB
643 if (ieee80211_sta_active_ibss(sdata))
644 return;
645
c037b836 646 if (ifibss->fixed_channel)
46900298
JB
647 return;
648
f0d23208
JP
649 pr_debug("%s: No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n",
650 sdata->name);
46900298 651
be4a4b6a 652 ieee80211_request_internal_scan(sdata,
c037b836 653 ifibss->ssid, ifibss->ssid_len, NULL);
46900298
JB
654}
655
af8cdcd8 656static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
657{
658 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 659 u8 bssid[ETH_ALEN];
46900298
JB
660 u16 capability;
661 int i;
662
7a17a33c
JB
663 lockdep_assert_held(&ifibss->mtx);
664
af8cdcd8 665 if (ifibss->fixed_bssid) {
46900298
JB
666 memcpy(bssid, ifibss->bssid, ETH_ALEN);
667 } else {
668 /* Generate random, not broadcast, locally administered BSSID. Mix in
669 * own MAC address to make sure that devices that do not have proper
670 * random number generator get different BSSID. */
671 get_random_bytes(bssid, ETH_ALEN);
672 for (i = 0; i < ETH_ALEN; i++)
47846c9b 673 bssid[i] ^= sdata->vif.addr[i];
46900298
JB
674 bssid[0] &= ~0x01;
675 bssid[0] |= 0x02;
676 }
677
f0d23208
JP
678 pr_debug("%s: Creating new IBSS network, BSSID %pM\n",
679 sdata->name, bssid);
46900298 680
46900298
JB
681 capability = WLAN_CAPABILITY_IBSS;
682
fffd0934 683 if (ifibss->privacy)
46900298
JB
684 capability |= WLAN_CAPABILITY_PRIVACY;
685 else
686 sdata->drop_unencrypted = 0;
687
57c4d7b4 688 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
fbd2c8dc 689 ifibss->channel, ifibss->basic_rates,
b59066a2 690 capability, 0);
46900298
JB
691}
692
ce9058ae
BP
693/*
694 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
695 */
696
af8cdcd8 697static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
698{
699 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
700 struct ieee80211_local *local = sdata->local;
0c1ad2ca 701 struct cfg80211_bss *cbss;
af8cdcd8 702 struct ieee80211_channel *chan = NULL;
46900298
JB
703 const u8 *bssid = NULL;
704 int active_ibss;
e0d61887 705 u16 capability;
46900298 706
7a17a33c
JB
707 lockdep_assert_held(&ifibss->mtx);
708
46900298 709 active_ibss = ieee80211_sta_active_ibss(sdata);
499f42bb
JP
710 ibss_vdbg("%s: sta_find_ibss (active_ibss=%d)\n",
711 sdata->name, active_ibss);
46900298
JB
712
713 if (active_ibss)
af8cdcd8 714 return;
46900298 715
e0d61887 716 capability = WLAN_CAPABILITY_IBSS;
fffd0934 717 if (ifibss->privacy)
e0d61887 718 capability |= WLAN_CAPABILITY_PRIVACY;
af8cdcd8
JB
719 if (ifibss->fixed_bssid)
720 bssid = ifibss->bssid;
721 if (ifibss->fixed_channel)
722 chan = ifibss->channel;
723 if (!is_zero_ether_addr(ifibss->bssid))
46900298 724 bssid = ifibss->bssid;
0c1ad2ca
JB
725 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
726 ifibss->ssid, ifibss->ssid_len,
727 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
728 capability);
729
730 if (cbss) {
731 struct ieee80211_bss *bss;
46900298 732
0c1ad2ca 733 bss = (void *)cbss->priv;
499f42bb
JP
734 ibss_vdbg(" sta_find_ibss: selected %pM current %pM\n",
735 cbss->bssid, ifibss->bssid);
f0d23208
JP
736 pr_debug("%s: Selected IBSS BSSID %pM based on configured SSID\n",
737 sdata->name, cbss->bssid);
46900298 738
af8cdcd8 739 ieee80211_sta_join_ibss(sdata, bss);
46900298 740 ieee80211_rx_bss_put(local, bss);
af8cdcd8 741 return;
d419b9f0 742 }
46900298 743
499f42bb 744 ibss_vdbg(" did not try to join ibss\n");
46900298
JB
745
746 /* Selected IBSS not found in current scan results - try to scan */
ce9058ae 747 if (time_after(jiffies, ifibss->last_scan_completed +
46900298 748 IEEE80211_SCAN_INTERVAL)) {
f0d23208
JP
749 pr_debug("%s: Trigger new scan to find an IBSS to join\n",
750 sdata->name);
46900298 751
be4a4b6a
JB
752 ieee80211_request_internal_scan(sdata,
753 ifibss->ssid, ifibss->ssid_len,
754 ifibss->fixed_channel ? ifibss->channel : NULL);
ce9058ae 755 } else {
46900298
JB
756 int interval = IEEE80211_SCAN_INTERVAL;
757
758 if (time_after(jiffies, ifibss->ibss_join_req +
759 IEEE80211_IBSS_JOIN_TIMEOUT)) {
af8cdcd8
JB
760 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
761 ieee80211_sta_create_ibss(sdata);
762 return;
763 }
f0d23208
JP
764 pr_debug("%s: IBSS not allowed on %d MHz\n",
765 sdata->name,
766 local->hw.conf.channel->center_freq);
46900298
JB
767
768 /* No IBSS found - decrease scan interval and continue
769 * scanning. */
770 interval = IEEE80211_SCAN_INTERVAL_SLOW;
771 }
772
af8cdcd8
JB
773 mod_timer(&ifibss->timer,
774 round_jiffies(jiffies + interval));
46900298 775 }
46900298
JB
776}
777
778static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
c269a203 779 struct sk_buff *req)
46900298 780{
c269a203 781 struct ieee80211_mgmt *mgmt = (void *)req->data;
46900298
JB
782 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
783 struct ieee80211_local *local = sdata->local;
c269a203 784 int tx_last_beacon, len = req->len;
46900298
JB
785 struct sk_buff *skb;
786 struct ieee80211_mgmt *resp;
40b275b6 787 struct sk_buff *presp;
46900298
JB
788 u8 *pos, *end;
789
7a17a33c
JB
790 lockdep_assert_held(&ifibss->mtx);
791
40b275b6
JB
792 presp = rcu_dereference_protected(ifibss->presp,
793 lockdep_is_held(&ifibss->mtx));
794
46900298 795 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
40b275b6 796 len < 24 + 2 || !presp)
46900298
JB
797 return;
798
24487981 799 tx_last_beacon = drv_tx_last_beacon(local);
46900298 800
499f42bb
JP
801 ibss_vdbg("%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n",
802 sdata->name, mgmt->sa, mgmt->da,
803 mgmt->bssid, tx_last_beacon);
46900298 804
1ed76487 805 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
46900298
JB
806 return;
807
b203ca39 808 if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) &&
888d04df 809 !is_broadcast_ether_addr(mgmt->bssid))
46900298
JB
810 return;
811
812 end = ((u8 *) mgmt) + len;
813 pos = mgmt->u.probe_req.variable;
814 if (pos[0] != WLAN_EID_SSID ||
815 pos + 2 + pos[1] > end) {
499f42bb
JP
816 ibss_vdbg("%s: Invalid SSID IE in ProbeReq from %pM\n",
817 sdata->name, mgmt->sa);
46900298
JB
818 return;
819 }
820 if (pos[1] != 0 &&
821 (pos[1] != ifibss->ssid_len ||
0da780c2 822 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
46900298
JB
823 /* Ignore ProbeReq for foreign SSID */
824 return;
825 }
826
827 /* Reply with ProbeResp */
40b275b6 828 skb = skb_copy(presp, GFP_KERNEL);
46900298
JB
829 if (!skb)
830 return;
831
832 resp = (struct ieee80211_mgmt *) skb->data;
833 memcpy(resp->da, mgmt->sa, ETH_ALEN);
499f42bb 834 ibss_vdbg("%s: Sending ProbeResp to %pM\n", sdata->name, resp->da);
62ae67be
JB
835 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
836 ieee80211_tx_skb(sdata, skb);
46900298
JB
837}
838
839static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
840 struct ieee80211_mgmt *mgmt,
841 size_t len,
842 struct ieee80211_rx_status *rx_status)
843{
844 size_t baselen;
845 struct ieee802_11_elems elems;
846
46900298
JB
847 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
848 if (baselen > len)
849 return;
850
851 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
852 &elems);
853
854 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
855}
856
857static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
858 struct ieee80211_mgmt *mgmt,
859 size_t len,
860 struct ieee80211_rx_status *rx_status)
861{
862 size_t baselen;
863 struct ieee802_11_elems elems;
864
865 /* Process beacon from the current BSS */
866 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
867 if (baselen > len)
868 return;
869
870 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
871
872 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
873}
874
1fa57d01
JB
875void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
876 struct sk_buff *skb)
46900298
JB
877{
878 struct ieee80211_rx_status *rx_status;
879 struct ieee80211_mgmt *mgmt;
880 u16 fc;
881
f1d58c25 882 rx_status = IEEE80211_SKB_RXCB(skb);
46900298
JB
883 mgmt = (struct ieee80211_mgmt *) skb->data;
884 fc = le16_to_cpu(mgmt->frame_control);
885
7a17a33c
JB
886 mutex_lock(&sdata->u.ibss.mtx);
887
c926d006
TH
888 if (!sdata->u.ibss.ssid_len)
889 goto mgmt_out; /* not ready to merge yet */
890
46900298
JB
891 switch (fc & IEEE80211_FCTL_STYPE) {
892 case IEEE80211_STYPE_PROBE_REQ:
c269a203 893 ieee80211_rx_mgmt_probe_req(sdata, skb);
46900298
JB
894 break;
895 case IEEE80211_STYPE_PROBE_RESP:
896 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
897 rx_status);
898 break;
899 case IEEE80211_STYPE_BEACON:
900 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
901 rx_status);
902 break;
903 case IEEE80211_STYPE_AUTH:
904 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
905 break;
906 }
7a17a33c 907
c926d006 908 mgmt_out:
7a17a33c 909 mutex_unlock(&sdata->u.ibss.mtx);
46900298
JB
910}
911
1fa57d01 912void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
46900298 913{
1fa57d01 914 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
8bf11d8d 915 struct sta_info *sta;
46900298 916
7a17a33c
JB
917 mutex_lock(&ifibss->mtx);
918
919 /*
920 * Work could be scheduled after scan or similar
921 * when we aren't even joined (or trying) with a
922 * network.
923 */
924 if (!ifibss->ssid_len)
925 goto out;
46900298 926
8bf11d8d
JB
927 spin_lock_bh(&ifibss->incomplete_lock);
928 while (!list_empty(&ifibss->incomplete_stations)) {
929 sta = list_first_entry(&ifibss->incomplete_stations,
930 struct sta_info, list);
931 list_del(&sta->list);
932 spin_unlock_bh(&ifibss->incomplete_lock);
933
6d810f10 934 ieee80211_ibss_finish_sta(sta, true);
8bf11d8d
JB
935 rcu_read_unlock();
936 spin_lock_bh(&ifibss->incomplete_lock);
937 }
938 spin_unlock_bh(&ifibss->incomplete_lock);
939
46900298
JB
940 switch (ifibss->state) {
941 case IEEE80211_IBSS_MLME_SEARCH:
942 ieee80211_sta_find_ibss(sdata);
943 break;
944 case IEEE80211_IBSS_MLME_JOINED:
945 ieee80211_sta_merge_ibss(sdata);
946 break;
947 default:
948 WARN_ON(1);
949 break;
950 }
46900298 951
7a17a33c
JB
952 out:
953 mutex_unlock(&ifibss->mtx);
3a4d4aa2
JB
954}
955
46900298
JB
956static void ieee80211_ibss_timer(unsigned long data)
957{
958 struct ieee80211_sub_if_data *sdata =
959 (struct ieee80211_sub_if_data *) data;
960 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
961 struct ieee80211_local *local = sdata->local;
962
5bb644a0
JB
963 if (local->quiescing) {
964 ifibss->timer_running = true;
965 return;
966 }
967
7a17a33c 968 ieee80211_queue_work(&local->hw, &sdata->work);
46900298
JB
969}
970
5bb644a0
JB
971#ifdef CONFIG_PM
972void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
973{
974 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
975
5bb644a0
JB
976 if (del_timer_sync(&ifibss->timer))
977 ifibss->timer_running = true;
978}
979
980void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
981{
982 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
983
984 if (ifibss->timer_running) {
985 add_timer(&ifibss->timer);
986 ifibss->timer_running = false;
987 }
988}
989#endif
990
46900298
JB
991void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
992{
993 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
994
46900298
JB
995 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
996 (unsigned long) sdata);
7a17a33c 997 mutex_init(&ifibss->mtx);
8bf11d8d
JB
998 INIT_LIST_HEAD(&ifibss->incomplete_stations);
999 spin_lock_init(&ifibss->incomplete_lock);
46900298
JB
1000}
1001
1002/* scan finished notification */
1003void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1004{
af8cdcd8 1005 struct ieee80211_sub_if_data *sdata;
46900298 1006
29b4a4f7
JB
1007 mutex_lock(&local->iflist_mtx);
1008 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 1009 if (!ieee80211_sdata_running(sdata))
0e41f715 1010 continue;
af8cdcd8
JB
1011 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1012 continue;
1013 sdata->u.ibss.last_scan_completed = jiffies;
7a17a33c 1014 ieee80211_queue_work(&local->hw, &sdata->work);
46900298 1015 }
29b4a4f7 1016 mutex_unlock(&local->iflist_mtx);
46900298
JB
1017}
1018
af8cdcd8
JB
1019int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1020 struct cfg80211_ibss_params *params)
1021{
1022 struct sk_buff *skb;
ff3cc5f4 1023 u32 changed = 0;
af8cdcd8 1024
7a17a33c 1025 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
13c40c54
AS
1026 sizeof(struct ieee80211_hdr_3addr) +
1027 12 /* struct ieee80211_mgmt.u.beacon */ +
1028 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
1029 2 + 8 /* max Supported Rates */ +
1030 3 /* max DS params */ +
1031 4 /* IBSS params */ +
1032 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
1033 2 + sizeof(struct ieee80211_ht_cap) +
074d46d1 1034 2 + sizeof(struct ieee80211_ht_operation) +
7a17a33c
JB
1035 params->ie_len);
1036 if (!skb)
1037 return -ENOMEM;
1038
1039 mutex_lock(&sdata->u.ibss.mtx);
1040
af8cdcd8
JB
1041 if (params->bssid) {
1042 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1043 sdata->u.ibss.fixed_bssid = true;
1044 } else
1045 sdata->u.ibss.fixed_bssid = false;
1046
fffd0934 1047 sdata->u.ibss.privacy = params->privacy;
267335d6 1048 sdata->u.ibss.control_port = params->control_port;
fbd2c8dc 1049 sdata->u.ibss.basic_rates = params->basic_rates;
dd5b4cc7
FF
1050 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1051 sizeof(params->mcast_rate));
fffd0934 1052
57c4d7b4
JB
1053 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1054
af8cdcd8 1055 sdata->u.ibss.channel = params->channel;
13c40c54 1056 sdata->u.ibss.channel_type = params->channel_type;
af8cdcd8
JB
1057 sdata->u.ibss.fixed_channel = params->channel_fixed;
1058
adfba3c7
JB
1059 /* fix ourselves to that channel now already */
1060 if (params->channel_fixed) {
1061 sdata->local->oper_channel = params->channel;
13c40c54 1062 if (!ieee80211_set_channel_type(sdata->local, sdata,
fb03c5eb
DC
1063 params->channel_type)) {
1064 mutex_unlock(&sdata->u.ibss.mtx);
cb71b8d8 1065 kfree_skb(skb);
13c40c54 1066 return -EINVAL;
fb03c5eb 1067 }
adfba3c7
JB
1068 }
1069
af8cdcd8
JB
1070 if (params->ie) {
1071 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1072 GFP_KERNEL);
1073 if (sdata->u.ibss.ie)
1074 sdata->u.ibss.ie_len = params->ie_len;
1075 }
1076
af8cdcd8
JB
1077 sdata->u.ibss.skb = skb;
1078 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1079 sdata->u.ibss.ibss_join_req = jiffies;
1080
0e41f715 1081 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
0e41f715
JB
1082 sdata->u.ibss.ssid_len = params->ssid_len;
1083
7da7cc1d
JB
1084 mutex_unlock(&sdata->u.ibss.mtx);
1085
1086 mutex_lock(&sdata->local->mtx);
5cff20e6 1087 ieee80211_recalc_idle(sdata->local);
7da7cc1d 1088 mutex_unlock(&sdata->local->mtx);
5cff20e6 1089
ff3cc5f4
SW
1090 /*
1091 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1092 * reserved, but an HT STA shall protect HT transmissions as though
1093 * the HT Protection field were set to non-HT mixed mode.
1094 *
1095 * In an IBSS, the RIFS Mode field of the HT Operation element is
1096 * also reserved, but an HT STA shall operate as though this field
1097 * were set to 1.
1098 */
1099
1100 sdata->vif.bss_conf.ht_operation_mode |=
1101 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1102 | IEEE80211_HT_PARAM_RIFS_MODE;
1103
1104 changed |= BSS_CHANGED_HT;
1105 ieee80211_bss_info_change_notify(sdata, changed);
1106
64592c8f 1107 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
af8cdcd8
JB
1108
1109 return 0;
1110}
1111
1112int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1113{
1114 struct sk_buff *skb;
5ea096c0
TP
1115 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1116 struct ieee80211_local *local = sdata->local;
1117 struct cfg80211_bss *cbss;
1118 u16 capability;
7a17a33c 1119 int active_ibss;
8bf11d8d 1120 struct sta_info *sta;
7a17a33c
JB
1121
1122 mutex_lock(&sdata->u.ibss.mtx);
5ea096c0 1123
f3209bea
JB
1124 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1125 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
1126 sdata->u.ibss.ssid_len = 0;
1127
5ea096c0
TP
1128 active_ibss = ieee80211_sta_active_ibss(sdata);
1129
1130 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1131 capability = WLAN_CAPABILITY_IBSS;
1132
1133 if (ifibss->privacy)
1134 capability |= WLAN_CAPABILITY_PRIVACY;
1135
1136 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
1137 ifibss->bssid, ifibss->ssid,
1138 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1139 WLAN_CAPABILITY_PRIVACY,
1140 capability);
1141
1142 if (cbss) {
1143 cfg80211_unlink_bss(local->hw.wiphy, cbss);
1144 cfg80211_put_bss(cbss);
1145 }
1146 }
af8cdcd8 1147
af8cdcd8 1148 sta_info_flush(sdata->local, sdata);
8bf11d8d
JB
1149
1150 spin_lock_bh(&ifibss->incomplete_lock);
1151 while (!list_empty(&ifibss->incomplete_stations)) {
1152 sta = list_first_entry(&ifibss->incomplete_stations,
1153 struct sta_info, list);
1154 list_del(&sta->list);
1155 spin_unlock_bh(&ifibss->incomplete_lock);
1156
1157 sta_info_free(local, sta);
1158 spin_lock_bh(&ifibss->incomplete_lock);
1159 }
1160 spin_unlock_bh(&ifibss->incomplete_lock);
1161
86a2ea41 1162 netif_carrier_off(sdata->dev);
af8cdcd8
JB
1163
1164 /* remove beacon */
1165 kfree(sdata->u.ibss.ie);
40b275b6
JB
1166 skb = rcu_dereference_protected(sdata->u.ibss.presp,
1167 lockdep_is_held(&sdata->u.ibss.mtx));
a9b3cd7f 1168 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
8fc214ba
JB
1169 sdata->vif.bss_conf.ibss_joined = false;
1170 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1171 BSS_CHANGED_IBSS);
af8cdcd8
JB
1172 synchronize_rcu();
1173 kfree_skb(skb);
1174
35f20c14 1175 skb_queue_purge(&sdata->skb_queue);
5cff20e6 1176
bc05d19f 1177 del_timer_sync(&sdata->u.ibss.timer);
7a17a33c
JB
1178
1179 mutex_unlock(&sdata->u.ibss.mtx);
bc05d19f 1180
7da7cc1d 1181 mutex_lock(&local->mtx);
5cff20e6 1182 ieee80211_recalc_idle(sdata->local);
7da7cc1d 1183 mutex_unlock(&local->mtx);
af8cdcd8
JB
1184
1185 return 0;
1186}