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