blob: b2cc1fda6cfdb6ba1054aa2bb224f9fce7bba5e8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Johannes Berg46900292009-02-15 12:44:28 +010017#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"
Johannes Berg24487982009-04-23 18:52:52 +020026#include "driver-ops.h"
Johannes Berg46900292009-02-15 12:44:28 +010027#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)
Alina Friedrichsen4a332a32009-02-22 18:19:33 +010034#define IEEE80211_IBSS_MERGE_DELAY 0x400000
Johannes Berg46900292009-02-15 12:44:28 +010035#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,
Johannes Bergfffd0932009-07-08 14:22:54 +020061 sdata->u.ibss.bssid, NULL, 0, 0);
Johannes Berg46900292009-02-15 12:44:28 +010062}
63
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020064static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
65 const u8 *bssid, const int beacon_int,
66 struct ieee80211_channel *chan,
Johannes Bergb59066a2009-05-12 21:18:38 +020067 const u32 basic_rates,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020068 const u16 capability, u64 tsf)
Johannes Berg46900292009-02-15 12:44:28 +010069{
70 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
71 struct ieee80211_local *local = sdata->local;
Johannes Bergb59066a2009-05-12 21:18:38 +020072 int rates, i;
Johannes Berg46900292009-02-15 12:44:28 +010073 struct sk_buff *skb;
74 struct ieee80211_mgmt *mgmt;
75 u8 *pos;
76 struct ieee80211_supported_band *sband;
Johannes Bergf446d102009-10-28 15:12:32 +010077 struct cfg80211_bss *bss;
Johannes Berg57c4d7b2009-04-23 16:10:04 +020078 u32 bss_change;
Johannes Bergb59066a2009-05-12 21:18:38 +020079 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
Johannes Berg24487982009-04-23 18:52:52 +020080
81 /* Reset own TSF to allow time synchronization work. */
82 drv_reset_tsf(local);
Johannes Berg46900292009-02-15 12:44:28 +010083
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020084 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);
Johannes Berg46900292009-02-15 12:44:28 +010091
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020092 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
93 sta_info_flush(sdata->local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010094
Johannes Berg49b5c7f2010-04-29 21:34:01 +020095 /* if merging, indicate to driver that we leave the old IBSS */
96 if (sdata->vif.bss_conf.ibss_joined) {
97 sdata->vif.bss_conf.ibss_joined = false;
98 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
99 }
100
Johannes Berg46900292009-02-15 12:44:28 +0100101 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100102
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200103 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
Johannes Berg46900292009-02-15 12:44:28 +0100104
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200105 local->oper_channel = chan;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200106 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200107 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200108
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200109 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg46900292009-02-15 12:44:28 +0100110
Johannes Bergb59066a2009-05-12 21:18:38 +0200111 /* build supported rates array */
112 pos = supp_rates;
113 for (i = 0; i < sband->n_bitrates; i++) {
114 int rate = sband->bitrates[i].bitrate;
115 u8 basic = 0;
116 if (basic_rates & BIT(i))
117 basic = 0x80;
118 *pos++ = basic | (u8) (rate / 5);
119 }
120
Johannes Berg46900292009-02-15 12:44:28 +0100121 /* Build IBSS probe response */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200122 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
Johannes Berg46900292009-02-15 12:44:28 +0100123 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
124 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
125 IEEE80211_STYPE_PROBE_RESP);
126 memset(mgmt->da, 0xff, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100127 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100128 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200129 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530130 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100131 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
132
133 pos = skb_put(skb, 2 + ifibss->ssid_len);
134 *pos++ = WLAN_EID_SSID;
135 *pos++ = ifibss->ssid_len;
136 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
137
Johannes Bergb59066a2009-05-12 21:18:38 +0200138 rates = sband->n_bitrates;
Johannes Berg46900292009-02-15 12:44:28 +0100139 if (rates > 8)
140 rates = 8;
141 pos = skb_put(skb, 2 + rates);
142 *pos++ = WLAN_EID_SUPP_RATES;
143 *pos++ = rates;
144 memcpy(pos, supp_rates, rates);
145
146 if (sband->band == IEEE80211_BAND_2GHZ) {
147 pos = skb_put(skb, 2 + 1);
148 *pos++ = WLAN_EID_DS_PARAMS;
149 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200150 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100151 }
152
153 pos = skb_put(skb, 2 + 2);
154 *pos++ = WLAN_EID_IBSS_PARAMS;
155 *pos++ = 2;
156 /* FIX: set ATIM window based on scan results */
157 *pos++ = 0;
158 *pos++ = 0;
159
Johannes Bergb59066a2009-05-12 21:18:38 +0200160 if (sband->n_bitrates > 8) {
161 rates = sband->n_bitrates - 8;
Johannes Berg46900292009-02-15 12:44:28 +0100162 pos = skb_put(skb, 2 + rates);
163 *pos++ = WLAN_EID_EXT_SUPP_RATES;
164 *pos++ = rates;
165 memcpy(pos, &supp_rates[8], rates);
166 }
167
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200168 if (ifibss->ie_len)
169 memcpy(skb_put(skb, ifibss->ie_len),
170 ifibss->ie, ifibss->ie_len);
171
172 rcu_assign_pointer(ifibss->presp, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100173
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200174 sdata->vif.bss_conf.beacon_int = beacon_int;
175 bss_change = BSS_CHANGED_BEACON_INT;
176 bss_change |= ieee80211_reset_erp_info(sdata);
177 bss_change |= BSS_CHANGED_BSSID;
178 bss_change |= BSS_CHANGED_BEACON;
179 bss_change |= BSS_CHANGED_BEACON_ENABLED;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200180 bss_change |= BSS_CHANGED_IBSS;
181 sdata->vif.bss_conf.ibss_joined = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200182 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100183
Johannes Bergb59066a2009-05-12 21:18:38 +0200184 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
Johannes Berg46900292009-02-15 12:44:28 +0100185
Johannes Berg46900292009-02-15 12:44:28 +0100186 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200187 mod_timer(&ifibss->timer,
188 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100189
Johannes Bergf446d102009-10-28 15:12:32 +0100190 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
191 mgmt, skb->len, 0, GFP_KERNEL);
192 cfg80211_put_bss(bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200193 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100194}
195
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200196static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
197 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100198{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100199 struct cfg80211_bss *cbss =
200 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200201 struct ieee80211_supported_band *sband;
202 u32 basic_rates;
203 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100204 u16 beacon_int = cbss->beacon_interval;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200205
206 if (beacon_int < 10)
207 beacon_int = 10;
208
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100209 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Johannes Bergb59066a2009-05-12 21:18:38 +0200210
211 basic_rates = 0;
212
213 for (i = 0; i < bss->supp_rates_len; i++) {
214 int rate = (bss->supp_rates[i] & 0x7f) * 5;
215 bool is_basic = !!(bss->supp_rates[i] & 0x80);
216
217 for (j = 0; j < sband->n_bitrates; j++) {
218 if (sband->bitrates[j].bitrate == rate) {
219 if (is_basic)
220 basic_rates |= BIT(j);
221 break;
222 }
223 }
224 }
225
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100226 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200227 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100228 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200229 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100230 cbss->capability,
231 cbss->tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100232}
233
234static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
235 struct ieee80211_mgmt *mgmt,
236 size_t len,
237 struct ieee80211_rx_status *rx_status,
238 struct ieee802_11_elems *elems,
239 bool beacon)
240{
241 struct ieee80211_local *local = sdata->local;
242 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100243 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100244 struct ieee80211_bss *bss;
245 struct sta_info *sta;
246 struct ieee80211_channel *channel;
247 u64 beacon_timestamp, rx_timestamp;
248 u32 supp_rates = 0;
249 enum ieee80211_band band = rx_status->band;
250
251 if (elems->ds_params && elems->ds_params_len == 1)
252 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
253 else
254 freq = rx_status->freq;
255
256 channel = ieee80211_get_channel(local->hw.wiphy, freq);
257
258 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
259 return;
260
261 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
262 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
263 supp_rates = ieee80211_sta_get_rates(local, elems, band);
264
265 rcu_read_lock();
266
Johannes Bergabe60632009-11-25 17:46:18 +0100267 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100268 if (sta) {
269 u32 prev_rates;
270
271 prev_rates = sta->sta.supp_rates[band];
272 /* make sure mandatory rates are always added */
273 sta->sta.supp_rates[band] = supp_rates |
274 ieee80211_mandatory_rates(local, band);
275
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900276 if (sta->sta.supp_rates[band] != prev_rates) {
Johannes Berg46900292009-02-15 12:44:28 +0100277#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg46900292009-02-15 12:44:28 +0100278 printk(KERN_DEBUG "%s: updated supp_rates set "
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900279 "for %pM based on beacon/probe_response "
280 "(0x%x -> 0x%x)\n",
281 sdata->name, sta->sta.addr,
282 prev_rates, sta->sta.supp_rates[band]);
Johannes Berg46900292009-02-15 12:44:28 +0100283#endif
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900284 rate_control_rate_init(sta);
285 }
Johannes Berg34e89502010-02-03 13:59:58 +0100286 rcu_read_unlock();
287 } else {
288 rcu_read_unlock();
289 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
290 supp_rates, GFP_KERNEL);
291 }
Johannes Berg46900292009-02-15 12:44:28 +0100292 }
293
294 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
295 channel, beacon);
296 if (!bss)
297 return;
298
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100299 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
300
Johannes Berg46900292009-02-15 12:44:28 +0100301 /* was just updated in ieee80211_bss_info_update */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100302 beacon_timestamp = cbss->tsf;
Johannes Berg46900292009-02-15 12:44:28 +0100303
304 /* check if we need to merge IBSS */
305
Johannes Berg46900292009-02-15 12:44:28 +0100306 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100307 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100308 goto put_bss;
309
310 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100311 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100312 goto put_bss;
313
314 /* different channel */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100315 if (cbss->channel != local->oper_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100316 goto put_bss;
317
318 /* different SSID */
319 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
320 memcmp(elems->ssid, sdata->u.ibss.ssid,
321 sdata->u.ibss.ssid_len))
322 goto put_bss;
323
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100324 /* same BSSID */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100325 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100326 goto put_bss;
327
Johannes Berg46900292009-02-15 12:44:28 +0100328 if (rx_status->flag & RX_FLAG_TSFT) {
329 /*
330 * For correct IBSS merging we need mactime; since mactime is
331 * defined as the time the first data symbol of the frame hits
332 * the PHY, and the timestamp of the beacon is defined as "the
333 * time that the data symbol containing the first bit of the
334 * timestamp is transmitted to the PHY plus the transmitting
335 * STA's delays through its local PHY from the MAC-PHY
336 * interface to its interface with the WM" (802.11 11.1.2)
337 * - equals the time this bit arrives at the receiver - we have
338 * to take into account the offset between the two.
339 *
340 * E.g. at 1 MBit that means mactime is 192 usec earlier
341 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
342 */
343 int rate;
344
345 if (rx_status->flag & RX_FLAG_HT)
346 rate = 65; /* TODO: HT rates */
347 else
348 rate = local->hw.wiphy->bands[band]->
349 bitrates[rx_status->rate_idx].bitrate;
350
351 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
Johannes Berg24487982009-04-23 18:52:52 +0200352 } else {
353 /*
354 * second best option: get current TSF
355 * (will return -1 if not supported)
356 */
357 rx_timestamp = drv_get_tsf(local);
358 }
Johannes Berg46900292009-02-15 12:44:28 +0100359
360#ifdef CONFIG_MAC80211_IBSS_DEBUG
361 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
362 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
363 mgmt->sa, mgmt->bssid,
364 (unsigned long long)rx_timestamp,
365 (unsigned long long)beacon_timestamp,
366 (unsigned long long)(rx_timestamp - beacon_timestamp),
367 jiffies);
368#endif
369
Alina Friedrichsen4a332a32009-02-22 18:19:33 +0100370 /* give slow hardware some time to do the TSF sync */
371 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
372 goto put_bss;
373
Johannes Berg46900292009-02-15 12:44:28 +0100374 if (beacon_timestamp > rx_timestamp) {
375#ifdef CONFIG_MAC80211_IBSS_DEBUG
376 printk(KERN_DEBUG "%s: beacon TSF higher than "
377 "local TSF - IBSS merge with BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100378 sdata->name, mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100379#endif
380 ieee80211_sta_join_ibss(sdata, bss);
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900381 supp_rates = ieee80211_sta_get_rates(local, elems, band);
Johannes Berg34e89502010-02-03 13:59:58 +0100382 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
383 supp_rates, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100384 }
385
386 put_bss:
387 ieee80211_rx_bss_put(local, bss);
388}
389
390/*
391 * Add a new IBSS station, will also be called by the RX code when,
392 * in IBSS mode, receiving a frame from a yet-unknown station, hence
393 * must be callable in atomic context.
394 */
395struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Johannes Berg34e89502010-02-03 13:59:58 +0100396 u8 *bssid,u8 *addr, u32 supp_rates,
397 gfp_t gfp)
Johannes Berg46900292009-02-15 12:44:28 +0100398{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100399 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100400 struct ieee80211_local *local = sdata->local;
401 struct sta_info *sta;
402 int band = local->hw.conf.channel->band;
403
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200404 /*
405 * XXX: Consider removing the least recently used entry and
406 * allow new one to be added.
407 */
Johannes Berg46900292009-02-15 12:44:28 +0100408 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200409 if (net_ratelimit())
410 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100411 sdata->name, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100412 return NULL;
413 }
414
Felix Fietkau2e10d332009-12-20 19:07:09 +0100415 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
416 return NULL;
417
Johannes Berg46900292009-02-15 12:44:28 +0100418 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
419 return NULL;
420
421#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
422 printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100423 wiphy_name(local->hw.wiphy), addr, sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100424#endif
425
Johannes Berg34e89502010-02-03 13:59:58 +0100426 sta = sta_info_alloc(sdata, addr, gfp);
Johannes Berg46900292009-02-15 12:44:28 +0100427 if (!sta)
428 return NULL;
429
430 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
431
432 /* make sure mandatory rates are always added */
433 sta->sta.supp_rates[band] = supp_rates |
434 ieee80211_mandatory_rates(local, band);
435
436 rate_control_rate_init(sta);
437
Johannes Berg34e89502010-02-03 13:59:58 +0100438 /* If it fails, maybe we raced another insertion? */
Johannes Berg46900292009-02-15 12:44:28 +0100439 if (sta_info_insert(sta))
Johannes Berg34e89502010-02-03 13:59:58 +0100440 return sta_info_get(sdata, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100441 return sta;
442}
443
444static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
445{
446 struct ieee80211_local *local = sdata->local;
447 int active = 0;
448 struct sta_info *sta;
449
450 rcu_read_lock();
451
452 list_for_each_entry_rcu(sta, &local->sta_list, list) {
453 if (sta->sdata == sdata &&
454 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
455 jiffies)) {
456 active++;
457 break;
458 }
459 }
460
461 rcu_read_unlock();
462
463 return active;
464}
465
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100466/*
467 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
468 */
Johannes Berg46900292009-02-15 12:44:28 +0100469
470static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
471{
472 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
473
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200474 mod_timer(&ifibss->timer,
475 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100476
477 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200478
Sujith450aae32009-11-02 12:33:23 +0530479 if (time_before(jiffies, ifibss->last_scan_completed +
480 IEEE80211_IBSS_MERGE_INTERVAL))
481 return;
482
Johannes Berg46900292009-02-15 12:44:28 +0100483 if (ieee80211_sta_active_ibss(sdata))
484 return;
485
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200486 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100487 return;
488
489 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Johannes Berg47846c92009-11-25 17:46:19 +0100490 "IBSS networks with same SSID (merge)\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100491
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200492 ieee80211_request_internal_scan(sdata,
493 ifibss->ssid, ifibss->ssid_len,
494 ifibss->fixed_channel ? ifibss->channel : NULL);
Johannes Berg46900292009-02-15 12:44:28 +0100495}
496
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200497static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100498{
499 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
500 struct ieee80211_local *local = sdata->local;
501 struct ieee80211_supported_band *sband;
Johannes Berg46900292009-02-15 12:44:28 +0100502 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100503 u16 capability;
504 int i;
505
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200506 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100507 memcpy(bssid, ifibss->bssid, ETH_ALEN);
508 } else {
509 /* Generate random, not broadcast, locally administered BSSID. Mix in
510 * own MAC address to make sure that devices that do not have proper
511 * random number generator get different BSSID. */
512 get_random_bytes(bssid, ETH_ALEN);
513 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100514 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100515 bssid[0] &= ~0x01;
516 bssid[0] |= 0x02;
517 }
518
519 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100520 sdata->name, bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100521
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200522 sband = local->hw.wiphy->bands[ifibss->channel->band];
Johannes Berg46900292009-02-15 12:44:28 +0100523
Johannes Berg46900292009-02-15 12:44:28 +0100524 capability = WLAN_CAPABILITY_IBSS;
525
Johannes Bergfffd0932009-07-08 14:22:54 +0200526 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100527 capability |= WLAN_CAPABILITY_PRIVACY;
528 else
529 sdata->drop_unencrypted = 0;
530
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200531 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Johannes Bergb59066a2009-05-12 21:18:38 +0200532 ifibss->channel, 3, /* first two are basic */
533 capability, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100534}
535
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100536/*
537 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
538 */
539
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200540static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100541{
542 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
543 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100544 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200545 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100546 const u8 *bssid = NULL;
547 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200548 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100549
Johannes Berg46900292009-02-15 12:44:28 +0100550 active_ibss = ieee80211_sta_active_ibss(sdata);
551#ifdef CONFIG_MAC80211_IBSS_DEBUG
552 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100553 sdata->name, active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100554#endif /* CONFIG_MAC80211_IBSS_DEBUG */
555
556 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200557 return;
Johannes Berg46900292009-02-15 12:44:28 +0100558
Johannes Berge0d61882009-05-12 20:47:32 +0200559 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200560 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200561 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200562 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100563 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200564 if (ifibss->fixed_channel)
565 chan = ifibss->channel;
566 if (!is_zero_ether_addr(ifibss->bssid))
567 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100568 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
569 ifibss->ssid, ifibss->ssid_len,
570 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
571 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100572
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100573 if (cbss) {
574 struct ieee80211_bss *bss;
575
576 bss = (void *)cbss->priv;
Johannes Berg46900292009-02-15 12:44:28 +0100577#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg46900292009-02-15 12:44:28 +0100578 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100579 "%pM\n", cbss->bssid, ifibss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100580#endif /* CONFIG_MAC80211_IBSS_DEBUG */
581
Johannes Berg46900292009-02-15 12:44:28 +0100582 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
583 " based on configured SSID\n",
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100584 sdata->name, cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100585
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200586 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100587 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200588 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700589 }
Johannes Berg46900292009-02-15 12:44:28 +0100590
591#ifdef CONFIG_MAC80211_IBSS_DEBUG
592 printk(KERN_DEBUG " did not try to join ibss\n");
593#endif /* CONFIG_MAC80211_IBSS_DEBUG */
594
595 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100596 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100597 IEEE80211_SCAN_INTERVAL)) {
598 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Johannes Berg47846c92009-11-25 17:46:19 +0100599 "join\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100600
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200601 ieee80211_request_internal_scan(sdata,
602 ifibss->ssid, ifibss->ssid_len,
603 ifibss->fixed_channel ? ifibss->channel : NULL);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100604 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100605 int interval = IEEE80211_SCAN_INTERVAL;
606
607 if (time_after(jiffies, ifibss->ibss_join_req +
608 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200609 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
610 ieee80211_sta_create_ibss(sdata);
611 return;
612 }
Johannes Berg46900292009-02-15 12:44:28 +0100613 printk(KERN_DEBUG "%s: IBSS not allowed on"
Johannes Berg47846c92009-11-25 17:46:19 +0100614 " %d MHz\n", sdata->name,
Johannes Berg46900292009-02-15 12:44:28 +0100615 local->hw.conf.channel->center_freq);
616
617 /* No IBSS found - decrease scan interval and continue
618 * scanning. */
619 interval = IEEE80211_SCAN_INTERVAL_SLOW;
620 }
621
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200622 mod_timer(&ifibss->timer,
623 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100624 }
Johannes Berg46900292009-02-15 12:44:28 +0100625}
626
627static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
628 struct ieee80211_mgmt *mgmt,
629 size_t len)
630{
631 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
632 struct ieee80211_local *local = sdata->local;
633 int tx_last_beacon;
634 struct sk_buff *skb;
635 struct ieee80211_mgmt *resp;
636 u8 *pos, *end;
637
638 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200639 len < 24 + 2 || !ifibss->presp)
Johannes Berg46900292009-02-15 12:44:28 +0100640 return;
641
Johannes Berg24487982009-04-23 18:52:52 +0200642 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100643
644#ifdef CONFIG_MAC80211_IBSS_DEBUG
645 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
646 " (tx_last_beacon=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100647 sdata->name, mgmt->sa, mgmt->da,
Johannes Berg46900292009-02-15 12:44:28 +0100648 mgmt->bssid, tx_last_beacon);
649#endif /* CONFIG_MAC80211_IBSS_DEBUG */
650
651 if (!tx_last_beacon)
652 return;
653
654 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
655 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
656 return;
657
658 end = ((u8 *) mgmt) + len;
659 pos = mgmt->u.probe_req.variable;
660 if (pos[0] != WLAN_EID_SSID ||
661 pos + 2 + pos[1] > end) {
662#ifdef CONFIG_MAC80211_IBSS_DEBUG
663 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
664 "from %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100665 sdata->name, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100666#endif
667 return;
668 }
669 if (pos[1] != 0 &&
670 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100671 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100672 /* Ignore ProbeReq for foreign SSID */
673 return;
674 }
675
676 /* Reply with ProbeResp */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200677 skb = skb_copy(ifibss->presp, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100678 if (!skb)
679 return;
680
681 resp = (struct ieee80211_mgmt *) skb->data;
682 memcpy(resp->da, mgmt->sa, ETH_ALEN);
683#ifdef CONFIG_MAC80211_IBSS_DEBUG
684 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100685 sdata->name, resp->da);
Johannes Berg46900292009-02-15 12:44:28 +0100686#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Berg62ae67b2009-11-18 18:42:05 +0100687 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
688 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100689}
690
691static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
692 struct ieee80211_mgmt *mgmt,
693 size_t len,
694 struct ieee80211_rx_status *rx_status)
695{
696 size_t baselen;
697 struct ieee802_11_elems elems;
698
Johannes Berg47846c92009-11-25 17:46:19 +0100699 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Johannes Berg46900292009-02-15 12:44:28 +0100700 return; /* ignore ProbeResp to foreign address */
701
702 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
703 if (baselen > len)
704 return;
705
706 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
707 &elems);
708
709 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
710}
711
712static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
713 struct ieee80211_mgmt *mgmt,
714 size_t len,
715 struct ieee80211_rx_status *rx_status)
716{
717 size_t baselen;
718 struct ieee802_11_elems elems;
719
720 /* Process beacon from the current BSS */
721 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
722 if (baselen > len)
723 return;
724
725 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
726
727 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
728}
729
730static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
731 struct sk_buff *skb)
732{
733 struct ieee80211_rx_status *rx_status;
734 struct ieee80211_mgmt *mgmt;
735 u16 fc;
736
Johannes Bergf1d58c22009-06-17 13:13:00 +0200737 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +0100738 mgmt = (struct ieee80211_mgmt *) skb->data;
739 fc = le16_to_cpu(mgmt->frame_control);
740
741 switch (fc & IEEE80211_FCTL_STYPE) {
742 case IEEE80211_STYPE_PROBE_REQ:
743 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
744 break;
745 case IEEE80211_STYPE_PROBE_RESP:
746 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
747 rx_status);
748 break;
749 case IEEE80211_STYPE_BEACON:
750 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
751 rx_status);
752 break;
753 case IEEE80211_STYPE_AUTH:
754 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
755 break;
756 }
757
758 kfree_skb(skb);
759}
760
761static void ieee80211_ibss_work(struct work_struct *work)
762{
763 struct ieee80211_sub_if_data *sdata =
764 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
765 struct ieee80211_local *local = sdata->local;
766 struct ieee80211_if_ibss *ifibss;
767 struct sk_buff *skb;
768
Johannes Berg5bb644a2009-05-17 11:40:42 +0200769 if (WARN_ON(local->suspended))
770 return;
771
Johannes Berg9607e6b2009-12-23 13:15:31 +0100772 if (!ieee80211_sdata_running(sdata))
Johannes Berg46900292009-02-15 12:44:28 +0100773 return;
774
Helmut Schaafbe9c422009-07-23 12:14:04 +0200775 if (local->scanning)
Johannes Berg46900292009-02-15 12:44:28 +0100776 return;
777
778 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
779 return;
780 ifibss = &sdata->u.ibss;
781
782 while ((skb = skb_dequeue(&ifibss->skb_queue)))
783 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
784
785 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
786 return;
787
788 switch (ifibss->state) {
789 case IEEE80211_IBSS_MLME_SEARCH:
790 ieee80211_sta_find_ibss(sdata);
791 break;
792 case IEEE80211_IBSS_MLME_JOINED:
793 ieee80211_sta_merge_ibss(sdata);
794 break;
795 default:
796 WARN_ON(1);
797 break;
798 }
799}
800
801static void ieee80211_ibss_timer(unsigned long data)
802{
803 struct ieee80211_sub_if_data *sdata =
804 (struct ieee80211_sub_if_data *) data;
805 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
806 struct ieee80211_local *local = sdata->local;
807
Johannes Berg5bb644a2009-05-17 11:40:42 +0200808 if (local->quiescing) {
809 ifibss->timer_running = true;
810 return;
811 }
812
Johannes Berg46900292009-02-15 12:44:28 +0100813 set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400814 ieee80211_queue_work(&local->hw, &ifibss->work);
Johannes Berg46900292009-02-15 12:44:28 +0100815}
816
Johannes Berg5bb644a2009-05-17 11:40:42 +0200817#ifdef CONFIG_PM
818void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
819{
820 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
821
822 cancel_work_sync(&ifibss->work);
823 if (del_timer_sync(&ifibss->timer))
824 ifibss->timer_running = true;
825}
826
827void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
828{
829 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
830
831 if (ifibss->timer_running) {
832 add_timer(&ifibss->timer);
833 ifibss->timer_running = false;
834 }
835}
836#endif
837
Johannes Berg46900292009-02-15 12:44:28 +0100838void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
839{
840 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
841
842 INIT_WORK(&ifibss->work, ieee80211_ibss_work);
843 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
844 (unsigned long) sdata);
845 skb_queue_head_init(&ifibss->skb_queue);
Johannes Berg46900292009-02-15 12:44:28 +0100846}
847
848/* scan finished notification */
849void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
850{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200851 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +0100852
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200853 mutex_lock(&local->iflist_mtx);
854 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100855 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +0200856 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200857 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
858 continue;
Johannes Berg0e41f712009-04-23 11:48:56 +0200859 if (!sdata->u.ibss.ssid_len)
860 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200861 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg51f98f12009-10-11 11:47:57 +0200862 mod_timer(&sdata->u.ibss.timer, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100863 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200864 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100865}
866
867ieee80211_rx_result
Johannes Bergf1d58c22009-06-17 13:13:00 +0200868ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +0100869{
870 struct ieee80211_local *local = sdata->local;
871 struct ieee80211_mgmt *mgmt;
872 u16 fc;
873
874 if (skb->len < 24)
875 return RX_DROP_MONITOR;
876
877 mgmt = (struct ieee80211_mgmt *) skb->data;
878 fc = le16_to_cpu(mgmt->frame_control);
879
880 switch (fc & IEEE80211_FCTL_STYPE) {
881 case IEEE80211_STYPE_PROBE_RESP:
882 case IEEE80211_STYPE_BEACON:
Johannes Berg46900292009-02-15 12:44:28 +0100883 case IEEE80211_STYPE_PROBE_REQ:
884 case IEEE80211_STYPE_AUTH:
885 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400886 ieee80211_queue_work(&local->hw, &sdata->u.ibss.work);
Johannes Berg46900292009-02-15 12:44:28 +0100887 return RX_QUEUED;
888 }
889
890 return RX_DROP_MONITOR;
891}
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200892
893int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
894 struct cfg80211_ibss_params *params)
895{
896 struct sk_buff *skb;
897
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200898 if (params->bssid) {
899 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
900 sdata->u.ibss.fixed_bssid = true;
901 } else
902 sdata->u.ibss.fixed_bssid = false;
903
Johannes Bergfffd0932009-07-08 14:22:54 +0200904 sdata->u.ibss.privacy = params->privacy;
905
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200906 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
907
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200908 sdata->u.ibss.channel = params->channel;
909 sdata->u.ibss.fixed_channel = params->channel_fixed;
910
Johannes Bergadfba3c2010-05-05 15:33:55 +0200911 /* fix ourselves to that channel now already */
912 if (params->channel_fixed) {
913 sdata->local->oper_channel = params->channel;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200914 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
915 NL80211_CHAN_NO_HT));
Johannes Bergadfba3c2010-05-05 15:33:55 +0200916 }
917
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200918 if (params->ie) {
919 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
920 GFP_KERNEL);
921 if (sdata->u.ibss.ie)
922 sdata->u.ibss.ie_len = params->ie_len;
923 }
924
925 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
926 36 /* bitrates */ +
927 34 /* SSID */ +
928 3 /* DS params */ +
929 4 /* IBSS params */ +
930 params->ie_len);
931 if (!skb)
932 return -ENOMEM;
933
934 sdata->u.ibss.skb = skb;
935 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
936 sdata->u.ibss.ibss_join_req = jiffies;
937
Johannes Berg0e41f712009-04-23 11:48:56 +0200938 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
939
940 /*
941 * The ssid_len setting below is used to see whether
942 * we are active, and we need all other settings
943 * before that may get visible.
944 */
945 mb();
946
947 sdata->u.ibss.ssid_len = params->ssid_len;
948
Johannes Berg5cff20e2009-04-29 12:26:17 +0200949 ieee80211_recalc_idle(sdata->local);
950
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200951 set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400952 ieee80211_queue_work(&sdata->local->hw, &sdata->u.ibss.work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200953
954 return 0;
955}
956
957int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
958{
959 struct sk_buff *skb;
960
961 del_timer_sync(&sdata->u.ibss.timer);
962 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
963 cancel_work_sync(&sdata->u.ibss.work);
964 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
965
966 sta_info_flush(sdata->local, sdata);
967
968 /* remove beacon */
969 kfree(sdata->u.ibss.ie);
970 skb = sdata->u.ibss.presp;
971 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
Johannes Berg8fc214b2010-04-28 17:40:43 +0200972 sdata->vif.bss_conf.ibss_joined = false;
973 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
974 BSS_CHANGED_IBSS);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200975 synchronize_rcu();
976 kfree_skb(skb);
977
978 skb_queue_purge(&sdata->u.ibss.skb_queue);
979 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200980 sdata->u.ibss.ssid_len = 0;
981
982 ieee80211_recalc_idle(sdata->local);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200983
984 return 0;
985}