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