mac80211: fix TX a-MPDU locking
[linux-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>
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>
23#include <asm/unaligned.h>
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)
30#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
31#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
32
33#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
34#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
35
36#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37
38
39static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
40 struct ieee80211_mgmt *mgmt,
41 size_t len)
42{
0915cba3 43 u16 auth_alg, auth_transaction;
46900298 44
7a17a33c
JB
45 lockdep_assert_held(&sdata->u.ibss.mtx);
46
46900298
JB
47 if (len < 24 + 6)
48 return;
49
50 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
51 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
46900298
JB
52
53 /*
54 * IEEE 802.11 standard does not require authentication in IBSS
55 * networks and most implementations do not seem to use it.
56 * However, try to reply to authentication attempts if someone
57 * has actually implemented this.
58 */
59 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
60 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
fffd0934 61 sdata->u.ibss.bssid, NULL, 0, 0);
46900298
JB
62}
63
af8cdcd8
JB
64static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
65 const u8 *bssid, const int beacon_int,
66 struct ieee80211_channel *chan,
b59066a2 67 const u32 basic_rates,
af8cdcd8 68 const u16 capability, u64 tsf)
46900298
JB
69{
70 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
71 struct ieee80211_local *local = sdata->local;
b59066a2 72 int rates, i;
46900298
JB
73 struct sk_buff *skb;
74 struct ieee80211_mgmt *mgmt;
75 u8 *pos;
76 struct ieee80211_supported_band *sband;
f446d10f 77 struct cfg80211_bss *bss;
57c4d7b4 78 u32 bss_change;
b59066a2 79 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
24487981 80
7a17a33c
JB
81 lockdep_assert_held(&ifibss->mtx);
82
24487981
JB
83 /* Reset own TSF to allow time synchronization work. */
84 drv_reset_tsf(local);
46900298 85
af8cdcd8
JB
86 skb = ifibss->skb;
87 rcu_assign_pointer(ifibss->presp, NULL);
88 synchronize_rcu();
89 skb->data = skb->head;
90 skb->len = 0;
91 skb_reset_tail_pointer(skb);
92 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
46900298 93
af8cdcd8
JB
94 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
95 sta_info_flush(sdata->local, sdata);
46900298 96
49b5c7f4
JB
97 /* if merging, indicate to driver that we leave the old IBSS */
98 if (sdata->vif.bss_conf.ibss_joined) {
99 sdata->vif.bss_conf.ibss_joined = false;
100 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
101 }
102
46900298 103 memcpy(ifibss->bssid, bssid, ETH_ALEN);
46900298 104
af8cdcd8 105 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
46900298 106
af8cdcd8 107 local->oper_channel = chan;
0aaffa9b 108 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
af8cdcd8 109 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
57c4d7b4 110
af8cdcd8 111 sband = local->hw.wiphy->bands[chan->band];
46900298 112
b59066a2
JB
113 /* build supported rates array */
114 pos = supp_rates;
115 for (i = 0; i < sband->n_bitrates; i++) {
116 int rate = sband->bitrates[i].bitrate;
117 u8 basic = 0;
118 if (basic_rates & BIT(i))
119 basic = 0x80;
120 *pos++ = basic | (u8) (rate / 5);
121 }
122
46900298 123 /* Build IBSS probe response */
af8cdcd8 124 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
46900298
JB
125 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
126 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
127 IEEE80211_STYPE_PROBE_RESP);
128 memset(mgmt->da, 0xff, ETH_ALEN);
47846c9b 129 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298 130 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
57c4d7b4 131 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
707c1b4e 132 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
46900298
JB
133 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
134
135 pos = skb_put(skb, 2 + ifibss->ssid_len);
136 *pos++ = WLAN_EID_SSID;
137 *pos++ = ifibss->ssid_len;
138 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
139
b59066a2 140 rates = sband->n_bitrates;
46900298
JB
141 if (rates > 8)
142 rates = 8;
143 pos = skb_put(skb, 2 + rates);
144 *pos++ = WLAN_EID_SUPP_RATES;
145 *pos++ = rates;
146 memcpy(pos, supp_rates, rates);
147
148 if (sband->band == IEEE80211_BAND_2GHZ) {
149 pos = skb_put(skb, 2 + 1);
150 *pos++ = WLAN_EID_DS_PARAMS;
151 *pos++ = 1;
af8cdcd8 152 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
46900298
JB
153 }
154
155 pos = skb_put(skb, 2 + 2);
156 *pos++ = WLAN_EID_IBSS_PARAMS;
157 *pos++ = 2;
158 /* FIX: set ATIM window based on scan results */
159 *pos++ = 0;
160 *pos++ = 0;
161
b59066a2
JB
162 if (sband->n_bitrates > 8) {
163 rates = sband->n_bitrates - 8;
46900298
JB
164 pos = skb_put(skb, 2 + rates);
165 *pos++ = WLAN_EID_EXT_SUPP_RATES;
166 *pos++ = rates;
167 memcpy(pos, &supp_rates[8], rates);
168 }
169
af8cdcd8
JB
170 if (ifibss->ie_len)
171 memcpy(skb_put(skb, ifibss->ie_len),
172 ifibss->ie, ifibss->ie_len);
173
9eba6125
BR
174 if (local->hw.queues >= 4) {
175 pos = skb_put(skb, 9);
176 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
177 *pos++ = 7; /* len */
178 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
179 *pos++ = 0x50;
180 *pos++ = 0xf2;
181 *pos++ = 2; /* WME */
182 *pos++ = 0; /* WME info */
183 *pos++ = 1; /* WME ver */
184 *pos++ = 0; /* U-APSD no in use */
185 }
186
af8cdcd8 187 rcu_assign_pointer(ifibss->presp, skb);
46900298 188
2d0ddec5 189 sdata->vif.bss_conf.beacon_int = beacon_int;
fbd2c8dc 190 sdata->vif.bss_conf.basic_rates = basic_rates;
2d0ddec5
JB
191 bss_change = BSS_CHANGED_BEACON_INT;
192 bss_change |= ieee80211_reset_erp_info(sdata);
193 bss_change |= BSS_CHANGED_BSSID;
194 bss_change |= BSS_CHANGED_BEACON;
195 bss_change |= BSS_CHANGED_BEACON_ENABLED;
392cfdb1 196 bss_change |= BSS_CHANGED_BASIC_RATES;
8fc214ba
JB
197 bss_change |= BSS_CHANGED_IBSS;
198 sdata->vif.bss_conf.ibss_joined = true;
2d0ddec5 199 ieee80211_bss_info_change_notify(sdata, bss_change);
46900298 200
b59066a2 201 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
46900298 202
46900298 203 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
af8cdcd8
JB
204 mod_timer(&ifibss->timer,
205 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 206
f446d10f
JB
207 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
208 mgmt, skb->len, 0, GFP_KERNEL);
209 cfg80211_put_bss(bss);
af8cdcd8 210 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
46900298
JB
211}
212
af8cdcd8
JB
213static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
214 struct ieee80211_bss *bss)
46900298 215{
0c1ad2ca
JB
216 struct cfg80211_bss *cbss =
217 container_of((void *)bss, struct cfg80211_bss, priv);
b59066a2
JB
218 struct ieee80211_supported_band *sband;
219 u32 basic_rates;
220 int i, j;
0c1ad2ca 221 u16 beacon_int = cbss->beacon_interval;
57c4d7b4 222
7a17a33c
JB
223 lockdep_assert_held(&sdata->u.ibss.mtx);
224
57c4d7b4
JB
225 if (beacon_int < 10)
226 beacon_int = 10;
227
0c1ad2ca 228 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
b59066a2
JB
229
230 basic_rates = 0;
231
232 for (i = 0; i < bss->supp_rates_len; i++) {
233 int rate = (bss->supp_rates[i] & 0x7f) * 5;
234 bool is_basic = !!(bss->supp_rates[i] & 0x80);
235
236 for (j = 0; j < sband->n_bitrates; j++) {
237 if (sband->bitrates[j].bitrate == rate) {
238 if (is_basic)
239 basic_rates |= BIT(j);
240 break;
241 }
242 }
243 }
244
0c1ad2ca 245 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
57c4d7b4 246 beacon_int,
0c1ad2ca 247 cbss->channel,
b59066a2 248 basic_rates,
0c1ad2ca
JB
249 cbss->capability,
250 cbss->tsf);
46900298
JB
251}
252
253static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
254 struct ieee80211_mgmt *mgmt,
255 size_t len,
256 struct ieee80211_rx_status *rx_status,
257 struct ieee802_11_elems *elems,
258 bool beacon)
259{
260 struct ieee80211_local *local = sdata->local;
261 int freq;
0c1ad2ca 262 struct cfg80211_bss *cbss;
46900298
JB
263 struct ieee80211_bss *bss;
264 struct sta_info *sta;
265 struct ieee80211_channel *channel;
266 u64 beacon_timestamp, rx_timestamp;
267 u32 supp_rates = 0;
268 enum ieee80211_band band = rx_status->band;
269
270 if (elems->ds_params && elems->ds_params_len == 1)
59eb21a6
BR
271 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
272 band);
46900298
JB
273 else
274 freq = rx_status->freq;
275
276 channel = ieee80211_get_channel(local->hw.wiphy, freq);
277
278 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
279 return;
280
9eba6125 281 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
46900298 282 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
46900298
JB
283
284 rcu_read_lock();
abe60632 285 sta = sta_info_get(sdata, mgmt->sa);
46900298 286
9eba6125
BR
287 if (elems->supp_rates) {
288 supp_rates = ieee80211_sta_get_rates(local, elems,
289 band);
290 if (sta) {
291 u32 prev_rates;
292
293 prev_rates = sta->sta.supp_rates[band];
294 /* make sure mandatory rates are always added */
295 sta->sta.supp_rates[band] = supp_rates |
296 ieee80211_mandatory_rates(local, band);
46900298 297
9eba6125 298 if (sta->sta.supp_rates[band] != prev_rates) {
46900298 299#ifdef CONFIG_MAC80211_IBSS_DEBUG
9eba6125
BR
300 printk(KERN_DEBUG
301 "%s: updated supp_rates set "
302 "for %pM based on beacon"
303 "/probe_resp (0x%x -> 0x%x)\n",
304 sdata->name, sta->sta.addr,
305 prev_rates,
306 sta->sta.supp_rates[band]);
46900298 307#endif
9eba6125
BR
308 rate_control_rate_init(sta);
309 }
310 } else
311 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
312 mgmt->sa, supp_rates,
313 GFP_ATOMIC);
34e89507 314 }
9eba6125
BR
315
316 if (sta && elems->wmm_info)
317 set_sta_flags(sta, WLAN_STA_WME);
318
319 rcu_read_unlock();
46900298
JB
320 }
321
322 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
323 channel, beacon);
324 if (!bss)
325 return;
326
0c1ad2ca
JB
327 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
328
46900298 329 /* was just updated in ieee80211_bss_info_update */
0c1ad2ca 330 beacon_timestamp = cbss->tsf;
46900298
JB
331
332 /* check if we need to merge IBSS */
333
46900298 334 /* we use a fixed BSSID */
a98bfec2 335 if (sdata->u.ibss.fixed_bssid)
46900298
JB
336 goto put_bss;
337
338 /* not an IBSS */
0c1ad2ca 339 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
46900298
JB
340 goto put_bss;
341
342 /* different channel */
0c1ad2ca 343 if (cbss->channel != local->oper_channel)
46900298
JB
344 goto put_bss;
345
346 /* different SSID */
347 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
348 memcmp(elems->ssid, sdata->u.ibss.ssid,
349 sdata->u.ibss.ssid_len))
350 goto put_bss;
351
34e8f082 352 /* same BSSID */
0c1ad2ca 353 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
34e8f082
AF
354 goto put_bss;
355
6ebacbb7 356 if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
46900298
JB
357 /*
358 * For correct IBSS merging we need mactime; since mactime is
359 * defined as the time the first data symbol of the frame hits
360 * the PHY, and the timestamp of the beacon is defined as "the
361 * time that the data symbol containing the first bit of the
362 * timestamp is transmitted to the PHY plus the transmitting
363 * STA's delays through its local PHY from the MAC-PHY
364 * interface to its interface with the WM" (802.11 11.1.2)
365 * - equals the time this bit arrives at the receiver - we have
366 * to take into account the offset between the two.
367 *
368 * E.g. at 1 MBit that means mactime is 192 usec earlier
369 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
370 */
371 int rate;
372
373 if (rx_status->flag & RX_FLAG_HT)
374 rate = 65; /* TODO: HT rates */
375 else
376 rate = local->hw.wiphy->bands[band]->
377 bitrates[rx_status->rate_idx].bitrate;
378
379 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
24487981
JB
380 } else {
381 /*
382 * second best option: get current TSF
383 * (will return -1 if not supported)
384 */
385 rx_timestamp = drv_get_tsf(local);
386 }
46900298
JB
387
388#ifdef CONFIG_MAC80211_IBSS_DEBUG
389 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
390 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
391 mgmt->sa, mgmt->bssid,
392 (unsigned long long)rx_timestamp,
393 (unsigned long long)beacon_timestamp,
394 (unsigned long long)(rx_timestamp - beacon_timestamp),
395 jiffies);
396#endif
397
398 if (beacon_timestamp > rx_timestamp) {
399#ifdef CONFIG_MAC80211_IBSS_DEBUG
400 printk(KERN_DEBUG "%s: beacon TSF higher than "
401 "local TSF - IBSS merge with BSSID %pM\n",
47846c9b 402 sdata->name, mgmt->bssid);
46900298
JB
403#endif
404 ieee80211_sta_join_ibss(sdata, bss);
09a08cff 405 supp_rates = ieee80211_sta_get_rates(local, elems, band);
34e89507
JB
406 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
407 supp_rates, GFP_KERNEL);
46900298
JB
408 }
409
410 put_bss:
411 ieee80211_rx_bss_put(local, bss);
412}
413
414/*
415 * Add a new IBSS station, will also be called by the RX code when,
416 * in IBSS mode, receiving a frame from a yet-unknown station, hence
417 * must be callable in atomic context.
418 */
419struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
34e89507
JB
420 u8 *bssid,u8 *addr, u32 supp_rates,
421 gfp_t gfp)
46900298 422{
2e10d330 423 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298
JB
424 struct ieee80211_local *local = sdata->local;
425 struct sta_info *sta;
426 int band = local->hw.conf.channel->band;
427
af8cdcd8
JB
428 /*
429 * XXX: Consider removing the least recently used entry and
430 * allow new one to be added.
431 */
46900298 432 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
af8cdcd8
JB
433 if (net_ratelimit())
434 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
47846c9b 435 sdata->name, addr);
46900298
JB
436 return NULL;
437 }
438
2e10d330
FF
439 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
440 return NULL;
441
46900298
JB
442 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
443 return NULL;
444
445#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0fb9a9ec
JP
446 wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
447 addr, sdata->name);
46900298
JB
448#endif
449
34e89507 450 sta = sta_info_alloc(sdata, addr, gfp);
46900298
JB
451 if (!sta)
452 return NULL;
453
c8716d9d 454 sta->last_rx = jiffies;
46900298
JB
455 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
456
457 /* make sure mandatory rates are always added */
458 sta->sta.supp_rates[band] = supp_rates |
459 ieee80211_mandatory_rates(local, band);
460
461 rate_control_rate_init(sta);
462
34e89507 463 /* If it fails, maybe we raced another insertion? */
46900298 464 if (sta_info_insert(sta))
34e89507 465 return sta_info_get(sdata, addr);
46900298
JB
466 return sta;
467}
468
469static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
470{
471 struct ieee80211_local *local = sdata->local;
472 int active = 0;
473 struct sta_info *sta;
474
7a17a33c
JB
475 lockdep_assert_held(&sdata->u.ibss.mtx);
476
46900298
JB
477 rcu_read_lock();
478
479 list_for_each_entry_rcu(sta, &local->sta_list, list) {
480 if (sta->sdata == sdata &&
481 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
482 jiffies)) {
483 active++;
484 break;
485 }
486 }
487
488 rcu_read_unlock();
489
490 return active;
491}
492
ce9058ae
BP
493/*
494 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
495 */
46900298
JB
496
497static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
498{
499 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
500
7a17a33c
JB
501 lockdep_assert_held(&ifibss->mtx);
502
af8cdcd8
JB
503 mod_timer(&ifibss->timer,
504 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298
JB
505
506 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
af8cdcd8 507
450aae3d
S
508 if (time_before(jiffies, ifibss->last_scan_completed +
509 IEEE80211_IBSS_MERGE_INTERVAL))
510 return;
511
46900298
JB
512 if (ieee80211_sta_active_ibss(sdata))
513 return;
514
af8cdcd8 515 if (ifibss->fixed_channel)
46900298
JB
516 return;
517
518 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
47846c9b 519 "IBSS networks with same SSID (merge)\n", sdata->name);
46900298 520
be4a4b6a
JB
521 ieee80211_request_internal_scan(sdata,
522 ifibss->ssid, ifibss->ssid_len,
523 ifibss->fixed_channel ? ifibss->channel : NULL);
46900298
JB
524}
525
af8cdcd8 526static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
527{
528 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 529 u8 bssid[ETH_ALEN];
46900298
JB
530 u16 capability;
531 int i;
532
7a17a33c
JB
533 lockdep_assert_held(&ifibss->mtx);
534
af8cdcd8 535 if (ifibss->fixed_bssid) {
46900298
JB
536 memcpy(bssid, ifibss->bssid, ETH_ALEN);
537 } else {
538 /* Generate random, not broadcast, locally administered BSSID. Mix in
539 * own MAC address to make sure that devices that do not have proper
540 * random number generator get different BSSID. */
541 get_random_bytes(bssid, ETH_ALEN);
542 for (i = 0; i < ETH_ALEN; i++)
47846c9b 543 bssid[i] ^= sdata->vif.addr[i];
46900298
JB
544 bssid[0] &= ~0x01;
545 bssid[0] |= 0x02;
546 }
547
548 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
47846c9b 549 sdata->name, bssid);
46900298 550
46900298
JB
551 capability = WLAN_CAPABILITY_IBSS;
552
fffd0934 553 if (ifibss->privacy)
46900298
JB
554 capability |= WLAN_CAPABILITY_PRIVACY;
555 else
556 sdata->drop_unencrypted = 0;
557
57c4d7b4 558 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
fbd2c8dc 559 ifibss->channel, ifibss->basic_rates,
b59066a2 560 capability, 0);
46900298
JB
561}
562
ce9058ae
BP
563/*
564 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
565 */
566
af8cdcd8 567static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
568{
569 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
570 struct ieee80211_local *local = sdata->local;
0c1ad2ca 571 struct cfg80211_bss *cbss;
af8cdcd8 572 struct ieee80211_channel *chan = NULL;
46900298
JB
573 const u8 *bssid = NULL;
574 int active_ibss;
e0d61887 575 u16 capability;
46900298 576
7a17a33c
JB
577 lockdep_assert_held(&ifibss->mtx);
578
46900298
JB
579 active_ibss = ieee80211_sta_active_ibss(sdata);
580#ifdef CONFIG_MAC80211_IBSS_DEBUG
581 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
47846c9b 582 sdata->name, active_ibss);
46900298
JB
583#endif /* CONFIG_MAC80211_IBSS_DEBUG */
584
585 if (active_ibss)
af8cdcd8 586 return;
46900298 587
e0d61887 588 capability = WLAN_CAPABILITY_IBSS;
fffd0934 589 if (ifibss->privacy)
e0d61887 590 capability |= WLAN_CAPABILITY_PRIVACY;
af8cdcd8
JB
591 if (ifibss->fixed_bssid)
592 bssid = ifibss->bssid;
593 if (ifibss->fixed_channel)
594 chan = ifibss->channel;
595 if (!is_zero_ether_addr(ifibss->bssid))
46900298 596 bssid = ifibss->bssid;
0c1ad2ca
JB
597 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
598 ifibss->ssid, ifibss->ssid_len,
599 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
600 capability);
601
602 if (cbss) {
603 struct ieee80211_bss *bss;
46900298 604
0c1ad2ca 605 bss = (void *)cbss->priv;
46900298 606#ifdef CONFIG_MAC80211_IBSS_DEBUG
46900298 607 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
0c1ad2ca 608 "%pM\n", cbss->bssid, ifibss->bssid);
46900298
JB
609#endif /* CONFIG_MAC80211_IBSS_DEBUG */
610
46900298
JB
611 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
612 " based on configured SSID\n",
0c1ad2ca 613 sdata->name, cbss->bssid);
46900298 614
af8cdcd8 615 ieee80211_sta_join_ibss(sdata, bss);
46900298 616 ieee80211_rx_bss_put(local, bss);
af8cdcd8 617 return;
d419b9f0 618 }
46900298
JB
619
620#ifdef CONFIG_MAC80211_IBSS_DEBUG
621 printk(KERN_DEBUG " did not try to join ibss\n");
622#endif /* CONFIG_MAC80211_IBSS_DEBUG */
623
624 /* Selected IBSS not found in current scan results - try to scan */
ce9058ae 625 if (time_after(jiffies, ifibss->last_scan_completed +
46900298
JB
626 IEEE80211_SCAN_INTERVAL)) {
627 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
47846c9b 628 "join\n", sdata->name);
46900298 629
be4a4b6a
JB
630 ieee80211_request_internal_scan(sdata,
631 ifibss->ssid, ifibss->ssid_len,
632 ifibss->fixed_channel ? ifibss->channel : NULL);
ce9058ae 633 } else {
46900298
JB
634 int interval = IEEE80211_SCAN_INTERVAL;
635
636 if (time_after(jiffies, ifibss->ibss_join_req +
637 IEEE80211_IBSS_JOIN_TIMEOUT)) {
af8cdcd8
JB
638 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
639 ieee80211_sta_create_ibss(sdata);
640 return;
641 }
46900298 642 printk(KERN_DEBUG "%s: IBSS not allowed on"
47846c9b 643 " %d MHz\n", sdata->name,
46900298
JB
644 local->hw.conf.channel->center_freq);
645
646 /* No IBSS found - decrease scan interval and continue
647 * scanning. */
648 interval = IEEE80211_SCAN_INTERVAL_SLOW;
649 }
650
af8cdcd8
JB
651 mod_timer(&ifibss->timer,
652 round_jiffies(jiffies + interval));
46900298 653 }
46900298
JB
654}
655
656static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
c269a203 657 struct sk_buff *req)
46900298 658{
c269a203 659 struct ieee80211_mgmt *mgmt = (void *)req->data;
46900298
JB
660 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
661 struct ieee80211_local *local = sdata->local;
c269a203 662 int tx_last_beacon, len = req->len;
46900298
JB
663 struct sk_buff *skb;
664 struct ieee80211_mgmt *resp;
665 u8 *pos, *end;
666
7a17a33c
JB
667 lockdep_assert_held(&ifibss->mtx);
668
46900298 669 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
af8cdcd8 670 len < 24 + 2 || !ifibss->presp)
46900298
JB
671 return;
672
24487981 673 tx_last_beacon = drv_tx_last_beacon(local);
46900298
JB
674
675#ifdef CONFIG_MAC80211_IBSS_DEBUG
676 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
677 " (tx_last_beacon=%d)\n",
47846c9b 678 sdata->name, mgmt->sa, mgmt->da,
46900298
JB
679 mgmt->bssid, tx_last_beacon);
680#endif /* CONFIG_MAC80211_IBSS_DEBUG */
681
1ed76487 682 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
46900298
JB
683 return;
684
685 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
686 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
687 return;
688
689 end = ((u8 *) mgmt) + len;
690 pos = mgmt->u.probe_req.variable;
691 if (pos[0] != WLAN_EID_SSID ||
692 pos + 2 + pos[1] > end) {
693#ifdef CONFIG_MAC80211_IBSS_DEBUG
694 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
695 "from %pM\n",
47846c9b 696 sdata->name, mgmt->sa);
46900298
JB
697#endif
698 return;
699 }
700 if (pos[1] != 0 &&
701 (pos[1] != ifibss->ssid_len ||
0da780c2 702 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
46900298
JB
703 /* Ignore ProbeReq for foreign SSID */
704 return;
705 }
706
707 /* Reply with ProbeResp */
af8cdcd8 708 skb = skb_copy(ifibss->presp, GFP_KERNEL);
46900298
JB
709 if (!skb)
710 return;
711
712 resp = (struct ieee80211_mgmt *) skb->data;
713 memcpy(resp->da, mgmt->sa, ETH_ALEN);
714#ifdef CONFIG_MAC80211_IBSS_DEBUG
715 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
47846c9b 716 sdata->name, resp->da);
46900298 717#endif /* CONFIG_MAC80211_IBSS_DEBUG */
62ae67be
JB
718 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
719 ieee80211_tx_skb(sdata, skb);
46900298
JB
720}
721
722static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
723 struct ieee80211_mgmt *mgmt,
724 size_t len,
725 struct ieee80211_rx_status *rx_status)
726{
727 size_t baselen;
728 struct ieee802_11_elems elems;
729
47846c9b 730 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
46900298
JB
731 return; /* ignore ProbeResp to foreign address */
732
733 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
734 if (baselen > len)
735 return;
736
737 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
738 &elems);
739
740 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
741}
742
743static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
744 struct ieee80211_mgmt *mgmt,
745 size_t len,
746 struct ieee80211_rx_status *rx_status)
747{
748 size_t baselen;
749 struct ieee802_11_elems elems;
750
751 /* Process beacon from the current BSS */
752 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
753 if (baselen > len)
754 return;
755
756 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
757
758 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
759}
760
1fa57d01
JB
761void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
762 struct sk_buff *skb)
46900298
JB
763{
764 struct ieee80211_rx_status *rx_status;
765 struct ieee80211_mgmt *mgmt;
766 u16 fc;
767
f1d58c25 768 rx_status = IEEE80211_SKB_RXCB(skb);
46900298
JB
769 mgmt = (struct ieee80211_mgmt *) skb->data;
770 fc = le16_to_cpu(mgmt->frame_control);
771
7a17a33c
JB
772 mutex_lock(&sdata->u.ibss.mtx);
773
c926d006
TH
774 if (!sdata->u.ibss.ssid_len)
775 goto mgmt_out; /* not ready to merge yet */
776
46900298
JB
777 switch (fc & IEEE80211_FCTL_STYPE) {
778 case IEEE80211_STYPE_PROBE_REQ:
c269a203 779 ieee80211_rx_mgmt_probe_req(sdata, skb);
46900298
JB
780 break;
781 case IEEE80211_STYPE_PROBE_RESP:
782 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
783 rx_status);
784 break;
785 case IEEE80211_STYPE_BEACON:
786 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
787 rx_status);
788 break;
789 case IEEE80211_STYPE_AUTH:
790 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
791 break;
792 }
7a17a33c 793
c926d006 794 mgmt_out:
7a17a33c 795 mutex_unlock(&sdata->u.ibss.mtx);
46900298
JB
796}
797
1fa57d01 798void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
46900298 799{
1fa57d01 800 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 801
7a17a33c
JB
802 mutex_lock(&ifibss->mtx);
803
804 /*
805 * Work could be scheduled after scan or similar
806 * when we aren't even joined (or trying) with a
807 * network.
808 */
809 if (!ifibss->ssid_len)
810 goto out;
46900298
JB
811
812 switch (ifibss->state) {
813 case IEEE80211_IBSS_MLME_SEARCH:
814 ieee80211_sta_find_ibss(sdata);
815 break;
816 case IEEE80211_IBSS_MLME_JOINED:
817 ieee80211_sta_merge_ibss(sdata);
818 break;
819 default:
820 WARN_ON(1);
821 break;
822 }
46900298 823
7a17a33c
JB
824 out:
825 mutex_unlock(&ifibss->mtx);
3a4d4aa2
JB
826}
827
46900298
JB
828static void ieee80211_ibss_timer(unsigned long data)
829{
830 struct ieee80211_sub_if_data *sdata =
831 (struct ieee80211_sub_if_data *) data;
832 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
833 struct ieee80211_local *local = sdata->local;
834
5bb644a0
JB
835 if (local->quiescing) {
836 ifibss->timer_running = true;
837 return;
838 }
839
7a17a33c 840 ieee80211_queue_work(&local->hw, &sdata->work);
46900298
JB
841}
842
5bb644a0
JB
843#ifdef CONFIG_PM
844void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
845{
846 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
847
5bb644a0
JB
848 if (del_timer_sync(&ifibss->timer))
849 ifibss->timer_running = true;
850}
851
852void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
853{
854 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
855
856 if (ifibss->timer_running) {
857 add_timer(&ifibss->timer);
858 ifibss->timer_running = false;
859 }
860}
861#endif
862
46900298
JB
863void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
864{
865 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
866
46900298
JB
867 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
868 (unsigned long) sdata);
7a17a33c 869 mutex_init(&ifibss->mtx);
46900298
JB
870}
871
872/* scan finished notification */
873void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
874{
af8cdcd8 875 struct ieee80211_sub_if_data *sdata;
46900298 876
29b4a4f7
JB
877 mutex_lock(&local->iflist_mtx);
878 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 879 if (!ieee80211_sdata_running(sdata))
0e41f715 880 continue;
af8cdcd8
JB
881 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
882 continue;
883 sdata->u.ibss.last_scan_completed = jiffies;
7a17a33c 884 ieee80211_queue_work(&local->hw, &sdata->work);
46900298 885 }
29b4a4f7 886 mutex_unlock(&local->iflist_mtx);
46900298
JB
887}
888
af8cdcd8
JB
889int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
890 struct cfg80211_ibss_params *params)
891{
892 struct sk_buff *skb;
893
7a17a33c
JB
894 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
895 36 /* bitrates */ +
896 34 /* SSID */ +
897 3 /* DS params */ +
898 4 /* IBSS params */ +
899 params->ie_len);
900 if (!skb)
901 return -ENOMEM;
902
903 mutex_lock(&sdata->u.ibss.mtx);
904
af8cdcd8
JB
905 if (params->bssid) {
906 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
907 sdata->u.ibss.fixed_bssid = true;
908 } else
909 sdata->u.ibss.fixed_bssid = false;
910
fffd0934 911 sdata->u.ibss.privacy = params->privacy;
fbd2c8dc 912 sdata->u.ibss.basic_rates = params->basic_rates;
dd5b4cc7
FF
913 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
914 sizeof(params->mcast_rate));
fffd0934 915
57c4d7b4
JB
916 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
917
af8cdcd8
JB
918 sdata->u.ibss.channel = params->channel;
919 sdata->u.ibss.fixed_channel = params->channel_fixed;
920
adfba3c7
JB
921 /* fix ourselves to that channel now already */
922 if (params->channel_fixed) {
923 sdata->local->oper_channel = params->channel;
0aaffa9b
JB
924 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
925 NL80211_CHAN_NO_HT));
adfba3c7
JB
926 }
927
af8cdcd8
JB
928 if (params->ie) {
929 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
930 GFP_KERNEL);
931 if (sdata->u.ibss.ie)
932 sdata->u.ibss.ie_len = params->ie_len;
933 }
934
af8cdcd8
JB
935 sdata->u.ibss.skb = skb;
936 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
937 sdata->u.ibss.ibss_join_req = jiffies;
938
0e41f715 939 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
0e41f715
JB
940 sdata->u.ibss.ssid_len = params->ssid_len;
941
7da7cc1d
JB
942 mutex_unlock(&sdata->u.ibss.mtx);
943
944 mutex_lock(&sdata->local->mtx);
5cff20e6 945 ieee80211_recalc_idle(sdata->local);
7da7cc1d 946 mutex_unlock(&sdata->local->mtx);
5cff20e6 947
64592c8f 948 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
af8cdcd8
JB
949
950 return 0;
951}
952
953int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
954{
955 struct sk_buff *skb;
5ea096c0
TP
956 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
957 struct ieee80211_local *local = sdata->local;
958 struct cfg80211_bss *cbss;
959 u16 capability;
7a17a33c
JB
960 int active_ibss;
961
962 mutex_lock(&sdata->u.ibss.mtx);
5ea096c0
TP
963
964 active_ibss = ieee80211_sta_active_ibss(sdata);
965
966 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
967 capability = WLAN_CAPABILITY_IBSS;
968
969 if (ifibss->privacy)
970 capability |= WLAN_CAPABILITY_PRIVACY;
971
972 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
973 ifibss->bssid, ifibss->ssid,
974 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
975 WLAN_CAPABILITY_PRIVACY,
976 capability);
977
978 if (cbss) {
979 cfg80211_unlink_bss(local->hw.wiphy, cbss);
980 cfg80211_put_bss(cbss);
981 }
982 }
af8cdcd8 983
af8cdcd8
JB
984 sta_info_flush(sdata->local, sdata);
985
986 /* remove beacon */
987 kfree(sdata->u.ibss.ie);
988 skb = sdata->u.ibss.presp;
989 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
8fc214ba
JB
990 sdata->vif.bss_conf.ibss_joined = false;
991 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
992 BSS_CHANGED_IBSS);
af8cdcd8
JB
993 synchronize_rcu();
994 kfree_skb(skb);
995
35f20c14 996 skb_queue_purge(&sdata->skb_queue);
af8cdcd8 997 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
5cff20e6
JB
998 sdata->u.ibss.ssid_len = 0;
999
bc05d19f 1000 del_timer_sync(&sdata->u.ibss.timer);
7a17a33c
JB
1001
1002 mutex_unlock(&sdata->u.ibss.mtx);
bc05d19f 1003
7da7cc1d 1004 mutex_lock(&local->mtx);
5cff20e6 1005 ieee80211_recalc_idle(sdata->local);
7da7cc1d 1006 mutex_unlock(&local->mtx);
af8cdcd8
JB
1007
1008 return 0;
1009}