blob: 25ff583612ef5d34a2b6e6c5c12ae3841b61f530 [file] [log] [blame]
Johannes Berg46900292009-02-15 12:44:28 +01001/*
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)
Alina Friedrichsen4a332a32009-02-22 18:19:33 +010032#define IEEE80211_IBSS_MERGE_DELAY 0x400000
Johannes Berg46900292009-02-15 12:44:28 +010033#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
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020062static 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)
Johannes Berg46900292009-02-15 12:44:28 +010068{
69 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
70 struct ieee80211_local *local = sdata->local;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020071 int rates, i, j;
Johannes Berg46900292009-02-15 12:44:28 +010072 struct sk_buff *skb;
73 struct ieee80211_mgmt *mgmt;
74 u8 *pos;
75 struct ieee80211_supported_band *sband;
Johannes Berg46900292009-02-15 12:44:28 +010076
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
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020082 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);
Johannes Berg46900292009-02-15 12:44:28 +010089
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020090 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
91 sta_info_flush(sdata->local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010092
93 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +010094
95 local->hw.conf.beacon_int = beacon_int >= 10 ? beacon_int : 10;
96
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020097 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
Johannes Berg46900292009-02-15 12:44:28 +010098
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020099 ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Johannes Berg46900292009-02-15 12:44:28 +0100100
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200101 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];
Johannes Berg46900292009-02-15 12:44:28 +0100105
106 /* Build IBSS probe response */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200107 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
Johannes Berg46900292009-02-15 12:44:28 +0100108 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);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200114 mgmt->u.beacon.beacon_int = cpu_to_le16(local->hw.conf.beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530115 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100116 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;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200135 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100136 }
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
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200153 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);
Johannes Berg46900292009-02-15 12:44:28 +0100158
159 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON |
160 IEEE80211_IFCC_BEACON_ENABLED);
161
Johannes Berg46900292009-02-15 12:44:28 +0100162 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
Johannes Berg46900292009-02-15 12:44:28 +0100172 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200173 mod_timer(&ifibss->timer,
174 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100175
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200176 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);
Johannes Berg46900292009-02-15 12:44:28 +0100179}
180
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200181static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
182 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100183{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200184 __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);
Johannes Berg46900292009-02-15 12:44:28 +0100190}
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 */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200265 if (sdata->u.ibss.bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100266 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
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100282 /* same BSSID */
283 if (memcmp(bss->cbss.bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
284 goto put_bss;
285
Johannes Berg46900292009-02-15 12:44:28 +0100286 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
Alina Friedrichsen4a332a32009-02-22 18:19:33 +0100327 /* give slow hardware some time to do the TSF sync */
328 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
329 goto put_bss;
330
Johannes Berg46900292009-02-15 12:44:28 +0100331 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
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200357 /*
358 * XXX: Consider removing the least recently used entry and
359 * allow new one to be added.
360 */
Johannes Berg46900292009-02-15 12:44:28 +0100361 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200362 if (net_ratelimit())
363 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
364 sdata->dev->name, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100365 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
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200421 mod_timer(&ifibss->timer,
422 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100423
424 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200425
Johannes Berg46900292009-02-15 12:44:28 +0100426 if (ieee80211_sta_active_ibss(sdata))
427 return;
428
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200429 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100430 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
Johannes Bergf3b85252009-04-23 16:01:47 +0200435 ieee80211_request_internal_scan(sdata, ifibss->ssid, ifibss->ssid_len);
Johannes Berg46900292009-02-15 12:44:28 +0100436}
437
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200438static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100439{
440 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
441 struct ieee80211_local *local = sdata->local;
442 struct ieee80211_supported_band *sband;
443 u8 *pos;
444 u8 bssid[ETH_ALEN];
445 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
446 u16 capability;
447 int i;
448
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200449 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100450 memcpy(bssid, ifibss->bssid, ETH_ALEN);
451 } else {
452 /* Generate random, not broadcast, locally administered BSSID. Mix in
453 * own MAC address to make sure that devices that do not have proper
454 * random number generator get different BSSID. */
455 get_random_bytes(bssid, ETH_ALEN);
456 for (i = 0; i < ETH_ALEN; i++)
457 bssid[i] ^= sdata->dev->dev_addr[i];
458 bssid[0] &= ~0x01;
459 bssid[0] |= 0x02;
460 }
461
462 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
463 sdata->dev->name, bssid);
464
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200465 sband = local->hw.wiphy->bands[ifibss->channel->band];
Johannes Berg46900292009-02-15 12:44:28 +0100466
467 if (local->hw.conf.beacon_int == 0)
468 local->hw.conf.beacon_int = 100;
469
470 capability = WLAN_CAPABILITY_IBSS;
471
472 if (sdata->default_key)
473 capability |= WLAN_CAPABILITY_PRIVACY;
474 else
475 sdata->drop_unencrypted = 0;
476
477 pos = supp_rates;
478 for (i = 0; i < sband->n_bitrates; i++) {
479 int rate = sband->bitrates[i].bitrate;
480 *pos++ = (u8) (rate / 5);
481 }
482
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200483 __ieee80211_sta_join_ibss(sdata, bssid, local->hw.conf.beacon_int,
484 ifibss->channel, sband->n_bitrates,
485 supp_rates, capability, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100486}
487
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200488static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100489{
490 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
491 struct ieee80211_local *local = sdata->local;
492 struct ieee80211_bss *bss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200493 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100494 const u8 *bssid = NULL;
495 int active_ibss;
496
Johannes Berg46900292009-02-15 12:44:28 +0100497 active_ibss = ieee80211_sta_active_ibss(sdata);
498#ifdef CONFIG_MAC80211_IBSS_DEBUG
499 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
500 sdata->dev->name, active_ibss);
501#endif /* CONFIG_MAC80211_IBSS_DEBUG */
502
503 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200504 return;
Johannes Berg46900292009-02-15 12:44:28 +0100505
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200506 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100507 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200508 if (ifibss->fixed_channel)
509 chan = ifibss->channel;
510 if (!is_zero_ether_addr(ifibss->bssid))
511 bssid = ifibss->bssid;
512 bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan, bssid,
Johannes Berg46900292009-02-15 12:44:28 +0100513 ifibss->ssid, ifibss->ssid_len,
514 WLAN_CAPABILITY_IBSS,
515 WLAN_CAPABILITY_IBSS);
516
517#ifdef CONFIG_MAC80211_IBSS_DEBUG
518 if (bss)
519 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
520 "%pM\n", bss->cbss.bssid, ifibss->bssid);
521#endif /* CONFIG_MAC80211_IBSS_DEBUG */
522
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200523 if (bss && memcmp(ifibss->bssid, bss->cbss.bssid, ETH_ALEN)) {
Johannes Berg46900292009-02-15 12:44:28 +0100524 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
525 " based on configured SSID\n",
526 sdata->dev->name, bss->cbss.bssid);
527
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200528 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100529 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200530 return;
Johannes Berg46900292009-02-15 12:44:28 +0100531 } else if (bss)
532 ieee80211_rx_bss_put(local, bss);
533
534#ifdef CONFIG_MAC80211_IBSS_DEBUG
535 printk(KERN_DEBUG " did not try to join ibss\n");
536#endif /* CONFIG_MAC80211_IBSS_DEBUG */
537
538 /* Selected IBSS not found in current scan results - try to scan */
539 if (ifibss->state == IEEE80211_IBSS_MLME_JOINED &&
540 !ieee80211_sta_active_ibss(sdata)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200541 mod_timer(&ifibss->timer,
542 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
543 } else if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100544 IEEE80211_SCAN_INTERVAL)) {
545 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
546 "join\n", sdata->dev->name);
547
Johannes Bergf3b85252009-04-23 16:01:47 +0200548 ieee80211_request_internal_scan(sdata, ifibss->ssid,
549 ifibss->ssid_len);
Johannes Berg46900292009-02-15 12:44:28 +0100550 } else if (ifibss->state != IEEE80211_IBSS_MLME_JOINED) {
551 int interval = IEEE80211_SCAN_INTERVAL;
552
553 if (time_after(jiffies, ifibss->ibss_join_req +
554 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200555 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
556 ieee80211_sta_create_ibss(sdata);
557 return;
558 }
Johannes Berg46900292009-02-15 12:44:28 +0100559 printk(KERN_DEBUG "%s: IBSS not allowed on"
560 " %d MHz\n", sdata->dev->name,
561 local->hw.conf.channel->center_freq);
562
563 /* No IBSS found - decrease scan interval and continue
564 * scanning. */
565 interval = IEEE80211_SCAN_INTERVAL_SLOW;
566 }
567
568 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200569 mod_timer(&ifibss->timer,
570 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100571 }
Johannes Berg46900292009-02-15 12:44:28 +0100572}
573
574static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
575 struct ieee80211_mgmt *mgmt,
576 size_t len)
577{
578 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
579 struct ieee80211_local *local = sdata->local;
580 int tx_last_beacon;
581 struct sk_buff *skb;
582 struct ieee80211_mgmt *resp;
583 u8 *pos, *end;
584
585 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200586 len < 24 + 2 || !ifibss->presp)
Johannes Berg46900292009-02-15 12:44:28 +0100587 return;
588
589 if (local->ops->tx_last_beacon)
590 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
591 else
592 tx_last_beacon = 1;
593
594#ifdef CONFIG_MAC80211_IBSS_DEBUG
595 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
596 " (tx_last_beacon=%d)\n",
597 sdata->dev->name, mgmt->sa, mgmt->da,
598 mgmt->bssid, tx_last_beacon);
599#endif /* CONFIG_MAC80211_IBSS_DEBUG */
600
601 if (!tx_last_beacon)
602 return;
603
604 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
605 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
606 return;
607
608 end = ((u8 *) mgmt) + len;
609 pos = mgmt->u.probe_req.variable;
610 if (pos[0] != WLAN_EID_SSID ||
611 pos + 2 + pos[1] > end) {
612#ifdef CONFIG_MAC80211_IBSS_DEBUG
613 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
614 "from %pM\n",
615 sdata->dev->name, mgmt->sa);
616#endif
617 return;
618 }
619 if (pos[1] != 0 &&
620 (pos[1] != ifibss->ssid_len ||
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200621 !memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100622 /* Ignore ProbeReq for foreign SSID */
623 return;
624 }
625
626 /* Reply with ProbeResp */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200627 skb = skb_copy(ifibss->presp, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100628 if (!skb)
629 return;
630
631 resp = (struct ieee80211_mgmt *) skb->data;
632 memcpy(resp->da, mgmt->sa, ETH_ALEN);
633#ifdef CONFIG_MAC80211_IBSS_DEBUG
634 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
635 sdata->dev->name, resp->da);
636#endif /* CONFIG_MAC80211_IBSS_DEBUG */
637 ieee80211_tx_skb(sdata, skb, 0);
638}
639
640static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
641 struct ieee80211_mgmt *mgmt,
642 size_t len,
643 struct ieee80211_rx_status *rx_status)
644{
645 size_t baselen;
646 struct ieee802_11_elems elems;
647
648 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
649 return; /* ignore ProbeResp to foreign address */
650
651 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
652 if (baselen > len)
653 return;
654
655 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
656 &elems);
657
658 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
659}
660
661static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
662 struct ieee80211_mgmt *mgmt,
663 size_t len,
664 struct ieee80211_rx_status *rx_status)
665{
666 size_t baselen;
667 struct ieee802_11_elems elems;
668
669 /* Process beacon from the current BSS */
670 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
671 if (baselen > len)
672 return;
673
674 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
675
676 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
677}
678
679static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
680 struct sk_buff *skb)
681{
682 struct ieee80211_rx_status *rx_status;
683 struct ieee80211_mgmt *mgmt;
684 u16 fc;
685
686 rx_status = (struct ieee80211_rx_status *) skb->cb;
687 mgmt = (struct ieee80211_mgmt *) skb->data;
688 fc = le16_to_cpu(mgmt->frame_control);
689
690 switch (fc & IEEE80211_FCTL_STYPE) {
691 case IEEE80211_STYPE_PROBE_REQ:
692 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
693 break;
694 case IEEE80211_STYPE_PROBE_RESP:
695 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
696 rx_status);
697 break;
698 case IEEE80211_STYPE_BEACON:
699 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
700 rx_status);
701 break;
702 case IEEE80211_STYPE_AUTH:
703 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
704 break;
705 }
706
707 kfree_skb(skb);
708}
709
710static void ieee80211_ibss_work(struct work_struct *work)
711{
712 struct ieee80211_sub_if_data *sdata =
713 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
714 struct ieee80211_local *local = sdata->local;
715 struct ieee80211_if_ibss *ifibss;
716 struct sk_buff *skb;
717
718 if (!netif_running(sdata->dev))
719 return;
720
721 if (local->sw_scanning || local->hw_scanning)
722 return;
723
724 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
725 return;
726 ifibss = &sdata->u.ibss;
727
728 while ((skb = skb_dequeue(&ifibss->skb_queue)))
729 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
730
731 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
732 return;
733
734 switch (ifibss->state) {
735 case IEEE80211_IBSS_MLME_SEARCH:
736 ieee80211_sta_find_ibss(sdata);
737 break;
738 case IEEE80211_IBSS_MLME_JOINED:
739 ieee80211_sta_merge_ibss(sdata);
740 break;
741 default:
742 WARN_ON(1);
743 break;
744 }
745}
746
747static void ieee80211_ibss_timer(unsigned long data)
748{
749 struct ieee80211_sub_if_data *sdata =
750 (struct ieee80211_sub_if_data *) data;
751 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
752 struct ieee80211_local *local = sdata->local;
753
754 set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
755 queue_work(local->hw.workqueue, &ifibss->work);
756}
757
758void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
759{
760 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
761
762 INIT_WORK(&ifibss->work, ieee80211_ibss_work);
763 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
764 (unsigned long) sdata);
765 skb_queue_head_init(&ifibss->skb_queue);
Johannes Berg46900292009-02-15 12:44:28 +0100766}
767
768/* scan finished notification */
769void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
770{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200771 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +0100772
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200773 mutex_lock(&local->iflist_mtx);
774 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg0e41f712009-04-23 11:48:56 +0200775 if (!netif_running(sdata->dev))
776 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200777 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
778 continue;
Johannes Berg0e41f712009-04-23 11:48:56 +0200779 if (!sdata->u.ibss.ssid_len)
780 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200781 sdata->u.ibss.last_scan_completed = jiffies;
782 ieee80211_sta_find_ibss(sdata);
Johannes Berg46900292009-02-15 12:44:28 +0100783 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200784 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100785}
786
787ieee80211_rx_result
788ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
789 struct ieee80211_rx_status *rx_status)
790{
791 struct ieee80211_local *local = sdata->local;
792 struct ieee80211_mgmt *mgmt;
793 u16 fc;
794
795 if (skb->len < 24)
796 return RX_DROP_MONITOR;
797
798 mgmt = (struct ieee80211_mgmt *) skb->data;
799 fc = le16_to_cpu(mgmt->frame_control);
800
801 switch (fc & IEEE80211_FCTL_STYPE) {
802 case IEEE80211_STYPE_PROBE_RESP:
803 case IEEE80211_STYPE_BEACON:
804 memcpy(skb->cb, rx_status, sizeof(*rx_status));
805 case IEEE80211_STYPE_PROBE_REQ:
806 case IEEE80211_STYPE_AUTH:
807 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
808 queue_work(local->hw.workqueue, &sdata->u.ibss.work);
809 return RX_QUEUED;
810 }
811
812 return RX_DROP_MONITOR;
813}
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200814
815int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
816 struct cfg80211_ibss_params *params)
817{
818 struct sk_buff *skb;
819
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200820 if (params->bssid) {
821 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
822 sdata->u.ibss.fixed_bssid = true;
823 } else
824 sdata->u.ibss.fixed_bssid = false;
825
826 sdata->u.ibss.channel = params->channel;
827 sdata->u.ibss.fixed_channel = params->channel_fixed;
828
829 if (params->ie) {
830 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
831 GFP_KERNEL);
832 if (sdata->u.ibss.ie)
833 sdata->u.ibss.ie_len = params->ie_len;
834 }
835
836 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
837 36 /* bitrates */ +
838 34 /* SSID */ +
839 3 /* DS params */ +
840 4 /* IBSS params */ +
841 params->ie_len);
842 if (!skb)
843 return -ENOMEM;
844
845 sdata->u.ibss.skb = skb;
846 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
847 sdata->u.ibss.ibss_join_req = jiffies;
848
Johannes Berg0e41f712009-04-23 11:48:56 +0200849 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
850
851 /*
852 * The ssid_len setting below is used to see whether
853 * we are active, and we need all other settings
854 * before that may get visible.
855 */
856 mb();
857
858 sdata->u.ibss.ssid_len = params->ssid_len;
859
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200860 set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
861 queue_work(sdata->local->hw.workqueue, &sdata->u.ibss.work);
862
863 return 0;
864}
865
866int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
867{
868 struct sk_buff *skb;
869
870 del_timer_sync(&sdata->u.ibss.timer);
871 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
872 cancel_work_sync(&sdata->u.ibss.work);
873 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
874
875 sta_info_flush(sdata->local, sdata);
876
877 /* remove beacon */
878 kfree(sdata->u.ibss.ie);
879 skb = sdata->u.ibss.presp;
880 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
881 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON_ENABLED);
882 synchronize_rcu();
883 kfree_skb(skb);
884
885 skb_queue_purge(&sdata->u.ibss.skb_queue);
886 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
887
888 return 0;
889}