blob: e2976da4e0d9c7e86b21557c1571e2dd7e449db6 [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
95 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +010096
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 local->oper_channel = chan;
100 local->oper_channel_type = NL80211_CHAN_NO_HT;
101 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200102
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200103 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg46900292009-02-15 12:44:28 +0100104
Johannes Bergb59066a2009-05-12 21:18:38 +0200105 /* build supported rates array */
106 pos = supp_rates;
107 for (i = 0; i < sband->n_bitrates; i++) {
108 int rate = sband->bitrates[i].bitrate;
109 u8 basic = 0;
110 if (basic_rates & BIT(i))
111 basic = 0x80;
112 *pos++ = basic | (u8) (rate / 5);
113 }
114
Johannes Berg46900292009-02-15 12:44:28 +0100115 /* Build IBSS probe response */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200116 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
Johannes Berg46900292009-02-15 12:44:28 +0100117 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
118 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
119 IEEE80211_STYPE_PROBE_RESP);
120 memset(mgmt->da, 0xff, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100121 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100122 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200123 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530124 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100125 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
126
127 pos = skb_put(skb, 2 + ifibss->ssid_len);
128 *pos++ = WLAN_EID_SSID;
129 *pos++ = ifibss->ssid_len;
130 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
131
Johannes Bergb59066a2009-05-12 21:18:38 +0200132 rates = sband->n_bitrates;
Johannes Berg46900292009-02-15 12:44:28 +0100133 if (rates > 8)
134 rates = 8;
135 pos = skb_put(skb, 2 + rates);
136 *pos++ = WLAN_EID_SUPP_RATES;
137 *pos++ = rates;
138 memcpy(pos, supp_rates, rates);
139
140 if (sband->band == IEEE80211_BAND_2GHZ) {
141 pos = skb_put(skb, 2 + 1);
142 *pos++ = WLAN_EID_DS_PARAMS;
143 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200144 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100145 }
146
147 pos = skb_put(skb, 2 + 2);
148 *pos++ = WLAN_EID_IBSS_PARAMS;
149 *pos++ = 2;
150 /* FIX: set ATIM window based on scan results */
151 *pos++ = 0;
152 *pos++ = 0;
153
Johannes Bergb59066a2009-05-12 21:18:38 +0200154 if (sband->n_bitrates > 8) {
155 rates = sband->n_bitrates - 8;
Johannes Berg46900292009-02-15 12:44:28 +0100156 pos = skb_put(skb, 2 + rates);
157 *pos++ = WLAN_EID_EXT_SUPP_RATES;
158 *pos++ = rates;
159 memcpy(pos, &supp_rates[8], rates);
160 }
161
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200162 if (ifibss->ie_len)
163 memcpy(skb_put(skb, ifibss->ie_len),
164 ifibss->ie, ifibss->ie_len);
165
166 rcu_assign_pointer(ifibss->presp, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100167
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200168 sdata->vif.bss_conf.beacon_int = beacon_int;
169 bss_change = BSS_CHANGED_BEACON_INT;
170 bss_change |= ieee80211_reset_erp_info(sdata);
171 bss_change |= BSS_CHANGED_BSSID;
172 bss_change |= BSS_CHANGED_BEACON;
173 bss_change |= BSS_CHANGED_BEACON_ENABLED;
174 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100175
Johannes Bergb59066a2009-05-12 21:18:38 +0200176 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
Johannes Berg46900292009-02-15 12:44:28 +0100177
Johannes Berg46900292009-02-15 12:44:28 +0100178 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200179 mod_timer(&ifibss->timer,
180 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100181
Johannes Bergf446d102009-10-28 15:12:32 +0100182 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
183 mgmt, skb->len, 0, GFP_KERNEL);
184 cfg80211_put_bss(bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200185 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100186}
187
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200188static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
189 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100190{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100191 struct cfg80211_bss *cbss =
192 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200193 struct ieee80211_supported_band *sband;
194 u32 basic_rates;
195 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100196 u16 beacon_int = cbss->beacon_interval;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200197
198 if (beacon_int < 10)
199 beacon_int = 10;
200
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100201 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Johannes Bergb59066a2009-05-12 21:18:38 +0200202
203 basic_rates = 0;
204
205 for (i = 0; i < bss->supp_rates_len; i++) {
206 int rate = (bss->supp_rates[i] & 0x7f) * 5;
207 bool is_basic = !!(bss->supp_rates[i] & 0x80);
208
209 for (j = 0; j < sband->n_bitrates; j++) {
210 if (sband->bitrates[j].bitrate == rate) {
211 if (is_basic)
212 basic_rates |= BIT(j);
213 break;
214 }
215 }
216 }
217
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100218 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200219 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100220 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200221 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100222 cbss->capability,
223 cbss->tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100224}
225
226static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
227 struct ieee80211_mgmt *mgmt,
228 size_t len,
229 struct ieee80211_rx_status *rx_status,
230 struct ieee802_11_elems *elems,
231 bool beacon)
232{
233 struct ieee80211_local *local = sdata->local;
234 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100235 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100236 struct ieee80211_bss *bss;
237 struct sta_info *sta;
238 struct ieee80211_channel *channel;
239 u64 beacon_timestamp, rx_timestamp;
240 u32 supp_rates = 0;
241 enum ieee80211_band band = rx_status->band;
242
243 if (elems->ds_params && elems->ds_params_len == 1)
244 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
245 else
246 freq = rx_status->freq;
247
248 channel = ieee80211_get_channel(local->hw.wiphy, freq);
249
250 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
251 return;
252
253 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && elems->supp_rates &&
254 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
255 supp_rates = ieee80211_sta_get_rates(local, elems, band);
256
257 rcu_read_lock();
258
Johannes Bergabe60632009-11-25 17:46:18 +0100259 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100260 if (sta) {
261 u32 prev_rates;
262
263 prev_rates = sta->sta.supp_rates[band];
264 /* make sure mandatory rates are always added */
265 sta->sta.supp_rates[band] = supp_rates |
266 ieee80211_mandatory_rates(local, band);
267
268#ifdef CONFIG_MAC80211_IBSS_DEBUG
269 if (sta->sta.supp_rates[band] != prev_rates)
270 printk(KERN_DEBUG "%s: updated supp_rates set "
271 "for %pM based on beacon info (0x%llx | "
272 "0x%llx -> 0x%llx)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100273 sdata->name,
Johannes Berg46900292009-02-15 12:44:28 +0100274 sta->sta.addr,
275 (unsigned long long) prev_rates,
276 (unsigned long long) supp_rates,
277 (unsigned long long) sta->sta.supp_rates[band]);
278#endif
Johannes Berg34e89502010-02-03 13:59:58 +0100279 rcu_read_unlock();
280 } else {
281 rcu_read_unlock();
282 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
283 supp_rates, GFP_KERNEL);
284 }
Johannes Berg46900292009-02-15 12:44:28 +0100285 }
286
287 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
288 channel, beacon);
289 if (!bss)
290 return;
291
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100292 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
293
Johannes Berg46900292009-02-15 12:44:28 +0100294 /* was just updated in ieee80211_bss_info_update */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100295 beacon_timestamp = cbss->tsf;
Johannes Berg46900292009-02-15 12:44:28 +0100296
297 /* check if we need to merge IBSS */
298
Johannes Berg46900292009-02-15 12:44:28 +0100299 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100300 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100301 goto put_bss;
302
303 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100304 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100305 goto put_bss;
306
307 /* different channel */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100308 if (cbss->channel != local->oper_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100309 goto put_bss;
310
311 /* different SSID */
312 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
313 memcmp(elems->ssid, sdata->u.ibss.ssid,
314 sdata->u.ibss.ssid_len))
315 goto put_bss;
316
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100317 /* same BSSID */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100318 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100319 goto put_bss;
320
Johannes Berg46900292009-02-15 12:44:28 +0100321 if (rx_status->flag & RX_FLAG_TSFT) {
322 /*
323 * For correct IBSS merging we need mactime; since mactime is
324 * defined as the time the first data symbol of the frame hits
325 * the PHY, and the timestamp of the beacon is defined as "the
326 * time that the data symbol containing the first bit of the
327 * timestamp is transmitted to the PHY plus the transmitting
328 * STA's delays through its local PHY from the MAC-PHY
329 * interface to its interface with the WM" (802.11 11.1.2)
330 * - equals the time this bit arrives at the receiver - we have
331 * to take into account the offset between the two.
332 *
333 * E.g. at 1 MBit that means mactime is 192 usec earlier
334 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
335 */
336 int rate;
337
338 if (rx_status->flag & RX_FLAG_HT)
339 rate = 65; /* TODO: HT rates */
340 else
341 rate = local->hw.wiphy->bands[band]->
342 bitrates[rx_status->rate_idx].bitrate;
343
344 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
Johannes Berg24487982009-04-23 18:52:52 +0200345 } else {
346 /*
347 * second best option: get current TSF
348 * (will return -1 if not supported)
349 */
350 rx_timestamp = drv_get_tsf(local);
351 }
Johannes Berg46900292009-02-15 12:44:28 +0100352
353#ifdef CONFIG_MAC80211_IBSS_DEBUG
354 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
355 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
356 mgmt->sa, mgmt->bssid,
357 (unsigned long long)rx_timestamp,
358 (unsigned long long)beacon_timestamp,
359 (unsigned long long)(rx_timestamp - beacon_timestamp),
360 jiffies);
361#endif
362
Alina Friedrichsen4a332a32009-02-22 18:19:33 +0100363 /* give slow hardware some time to do the TSF sync */
364 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
365 goto put_bss;
366
Johannes Berg46900292009-02-15 12:44:28 +0100367 if (beacon_timestamp > rx_timestamp) {
368#ifdef CONFIG_MAC80211_IBSS_DEBUG
369 printk(KERN_DEBUG "%s: beacon TSF higher than "
370 "local TSF - IBSS merge with BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100371 sdata->name, mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100372#endif
373 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg34e89502010-02-03 13:59:58 +0100374 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
375 supp_rates, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100376 }
377
378 put_bss:
379 ieee80211_rx_bss_put(local, bss);
380}
381
382/*
383 * Add a new IBSS station, will also be called by the RX code when,
384 * in IBSS mode, receiving a frame from a yet-unknown station, hence
385 * must be callable in atomic context.
386 */
387struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Johannes Berg34e89502010-02-03 13:59:58 +0100388 u8 *bssid,u8 *addr, u32 supp_rates,
389 gfp_t gfp)
Johannes Berg46900292009-02-15 12:44:28 +0100390{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100391 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100392 struct ieee80211_local *local = sdata->local;
393 struct sta_info *sta;
394 int band = local->hw.conf.channel->band;
395
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200396 /*
397 * XXX: Consider removing the least recently used entry and
398 * allow new one to be added.
399 */
Johannes Berg46900292009-02-15 12:44:28 +0100400 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200401 if (net_ratelimit())
402 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100403 sdata->name, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100404 return NULL;
405 }
406
Felix Fietkau2e10d332009-12-20 19:07:09 +0100407 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
408 return NULL;
409
Johannes Berg46900292009-02-15 12:44:28 +0100410 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
411 return NULL;
412
413#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
414 printk(KERN_DEBUG "%s: Adding new IBSS station %pM (dev=%s)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100415 wiphy_name(local->hw.wiphy), addr, sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100416#endif
417
Johannes Berg34e89502010-02-03 13:59:58 +0100418 sta = sta_info_alloc(sdata, addr, gfp);
Johannes Berg46900292009-02-15 12:44:28 +0100419 if (!sta)
420 return NULL;
421
422 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
423
424 /* make sure mandatory rates are always added */
425 sta->sta.supp_rates[band] = supp_rates |
426 ieee80211_mandatory_rates(local, band);
427
428 rate_control_rate_init(sta);
429
Johannes Berg34e89502010-02-03 13:59:58 +0100430 /* If it fails, maybe we raced another insertion? */
Johannes Berg46900292009-02-15 12:44:28 +0100431 if (sta_info_insert(sta))
Johannes Berg34e89502010-02-03 13:59:58 +0100432 return sta_info_get(sdata, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100433 return sta;
434}
435
436static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
437{
438 struct ieee80211_local *local = sdata->local;
439 int active = 0;
440 struct sta_info *sta;
441
442 rcu_read_lock();
443
444 list_for_each_entry_rcu(sta, &local->sta_list, list) {
445 if (sta->sdata == sdata &&
446 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
447 jiffies)) {
448 active++;
449 break;
450 }
451 }
452
453 rcu_read_unlock();
454
455 return active;
456}
457
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100458/*
459 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
460 */
Johannes Berg46900292009-02-15 12:44:28 +0100461
462static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
463{
464 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
465
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200466 mod_timer(&ifibss->timer,
467 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100468
469 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200470
Sujith450aae32009-11-02 12:33:23 +0530471 if (time_before(jiffies, ifibss->last_scan_completed +
472 IEEE80211_IBSS_MERGE_INTERVAL))
473 return;
474
Johannes Berg46900292009-02-15 12:44:28 +0100475 if (ieee80211_sta_active_ibss(sdata))
476 return;
477
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200478 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100479 return;
480
481 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Johannes Berg47846c92009-11-25 17:46:19 +0100482 "IBSS networks with same SSID (merge)\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100483
Johannes Bergf3b85252009-04-23 16:01:47 +0200484 ieee80211_request_internal_scan(sdata, ifibss->ssid, ifibss->ssid_len);
Johannes Berg46900292009-02-15 12:44:28 +0100485}
486
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200487static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100488{
489 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
490 struct ieee80211_local *local = sdata->local;
491 struct ieee80211_supported_band *sband;
Johannes Berg46900292009-02-15 12:44:28 +0100492 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100493 u16 capability;
494 int i;
495
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200496 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100497 memcpy(bssid, ifibss->bssid, ETH_ALEN);
498 } else {
499 /* Generate random, not broadcast, locally administered BSSID. Mix in
500 * own MAC address to make sure that devices that do not have proper
501 * random number generator get different BSSID. */
502 get_random_bytes(bssid, ETH_ALEN);
503 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100504 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100505 bssid[0] &= ~0x01;
506 bssid[0] |= 0x02;
507 }
508
509 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100510 sdata->name, bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100511
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200512 sband = local->hw.wiphy->bands[ifibss->channel->band];
Johannes Berg46900292009-02-15 12:44:28 +0100513
Johannes Berg46900292009-02-15 12:44:28 +0100514 capability = WLAN_CAPABILITY_IBSS;
515
Johannes Bergfffd0932009-07-08 14:22:54 +0200516 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100517 capability |= WLAN_CAPABILITY_PRIVACY;
518 else
519 sdata->drop_unencrypted = 0;
520
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200521 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Johannes Bergb59066a2009-05-12 21:18:38 +0200522 ifibss->channel, 3, /* first two are basic */
523 capability, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100524}
525
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100526/*
527 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
528 */
529
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200530static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100531{
532 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
533 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100534 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200535 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100536 const u8 *bssid = NULL;
537 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200538 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100539
Johannes Berg46900292009-02-15 12:44:28 +0100540 active_ibss = ieee80211_sta_active_ibss(sdata);
541#ifdef CONFIG_MAC80211_IBSS_DEBUG
542 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100543 sdata->name, active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100544#endif /* CONFIG_MAC80211_IBSS_DEBUG */
545
546 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200547 return;
Johannes Berg46900292009-02-15 12:44:28 +0100548
Johannes Berge0d61882009-05-12 20:47:32 +0200549 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200550 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200551 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200552 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100553 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200554 if (ifibss->fixed_channel)
555 chan = ifibss->channel;
556 if (!is_zero_ether_addr(ifibss->bssid))
557 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100558 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
559 ifibss->ssid, ifibss->ssid_len,
560 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
561 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100562
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100563 if (cbss) {
564 struct ieee80211_bss *bss;
565
566 bss = (void *)cbss->priv;
Johannes Berg46900292009-02-15 12:44:28 +0100567#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg46900292009-02-15 12:44:28 +0100568 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100569 "%pM\n", cbss->bssid, ifibss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100570#endif /* CONFIG_MAC80211_IBSS_DEBUG */
571
Johannes Berg46900292009-02-15 12:44:28 +0100572 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
573 " based on configured SSID\n",
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100574 sdata->name, cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100575
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200576 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100577 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200578 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700579 }
Johannes Berg46900292009-02-15 12:44:28 +0100580
581#ifdef CONFIG_MAC80211_IBSS_DEBUG
582 printk(KERN_DEBUG " did not try to join ibss\n");
583#endif /* CONFIG_MAC80211_IBSS_DEBUG */
584
585 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100586 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100587 IEEE80211_SCAN_INTERVAL)) {
588 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Johannes Berg47846c92009-11-25 17:46:19 +0100589 "join\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100590
Johannes Bergf3b85252009-04-23 16:01:47 +0200591 ieee80211_request_internal_scan(sdata, ifibss->ssid,
592 ifibss->ssid_len);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100593 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100594 int interval = IEEE80211_SCAN_INTERVAL;
595
596 if (time_after(jiffies, ifibss->ibss_join_req +
597 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200598 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
599 ieee80211_sta_create_ibss(sdata);
600 return;
601 }
Johannes Berg46900292009-02-15 12:44:28 +0100602 printk(KERN_DEBUG "%s: IBSS not allowed on"
Johannes Berg47846c92009-11-25 17:46:19 +0100603 " %d MHz\n", sdata->name,
Johannes Berg46900292009-02-15 12:44:28 +0100604 local->hw.conf.channel->center_freq);
605
606 /* No IBSS found - decrease scan interval and continue
607 * scanning. */
608 interval = IEEE80211_SCAN_INTERVAL_SLOW;
609 }
610
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200611 mod_timer(&ifibss->timer,
612 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100613 }
Johannes Berg46900292009-02-15 12:44:28 +0100614}
615
616static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
617 struct ieee80211_mgmt *mgmt,
618 size_t len)
619{
620 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
621 struct ieee80211_local *local = sdata->local;
622 int tx_last_beacon;
623 struct sk_buff *skb;
624 struct ieee80211_mgmt *resp;
625 u8 *pos, *end;
626
627 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200628 len < 24 + 2 || !ifibss->presp)
Johannes Berg46900292009-02-15 12:44:28 +0100629 return;
630
Johannes Berg24487982009-04-23 18:52:52 +0200631 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100632
633#ifdef CONFIG_MAC80211_IBSS_DEBUG
634 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
635 " (tx_last_beacon=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100636 sdata->name, mgmt->sa, mgmt->da,
Johannes Berg46900292009-02-15 12:44:28 +0100637 mgmt->bssid, tx_last_beacon);
638#endif /* CONFIG_MAC80211_IBSS_DEBUG */
639
640 if (!tx_last_beacon)
641 return;
642
643 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
644 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
645 return;
646
647 end = ((u8 *) mgmt) + len;
648 pos = mgmt->u.probe_req.variable;
649 if (pos[0] != WLAN_EID_SSID ||
650 pos + 2 + pos[1] > end) {
651#ifdef CONFIG_MAC80211_IBSS_DEBUG
652 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
653 "from %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100654 sdata->name, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100655#endif
656 return;
657 }
658 if (pos[1] != 0 &&
659 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100660 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100661 /* Ignore ProbeReq for foreign SSID */
662 return;
663 }
664
665 /* Reply with ProbeResp */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200666 skb = skb_copy(ifibss->presp, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100667 if (!skb)
668 return;
669
670 resp = (struct ieee80211_mgmt *) skb->data;
671 memcpy(resp->da, mgmt->sa, ETH_ALEN);
672#ifdef CONFIG_MAC80211_IBSS_DEBUG
673 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100674 sdata->name, resp->da);
Johannes Berg46900292009-02-15 12:44:28 +0100675#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Berg62ae67b2009-11-18 18:42:05 +0100676 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
677 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100678}
679
680static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
681 struct ieee80211_mgmt *mgmt,
682 size_t len,
683 struct ieee80211_rx_status *rx_status)
684{
685 size_t baselen;
686 struct ieee802_11_elems elems;
687
Johannes Berg47846c92009-11-25 17:46:19 +0100688 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Johannes Berg46900292009-02-15 12:44:28 +0100689 return; /* ignore ProbeResp to foreign address */
690
691 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
692 if (baselen > len)
693 return;
694
695 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
696 &elems);
697
698 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
699}
700
701static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
702 struct ieee80211_mgmt *mgmt,
703 size_t len,
704 struct ieee80211_rx_status *rx_status)
705{
706 size_t baselen;
707 struct ieee802_11_elems elems;
708
709 /* Process beacon from the current BSS */
710 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
711 if (baselen > len)
712 return;
713
714 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
715
716 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
717}
718
719static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
720 struct sk_buff *skb)
721{
722 struct ieee80211_rx_status *rx_status;
723 struct ieee80211_mgmt *mgmt;
724 u16 fc;
725
Johannes Bergf1d58c22009-06-17 13:13:00 +0200726 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +0100727 mgmt = (struct ieee80211_mgmt *) skb->data;
728 fc = le16_to_cpu(mgmt->frame_control);
729
730 switch (fc & IEEE80211_FCTL_STYPE) {
731 case IEEE80211_STYPE_PROBE_REQ:
732 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
733 break;
734 case IEEE80211_STYPE_PROBE_RESP:
735 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
736 rx_status);
737 break;
738 case IEEE80211_STYPE_BEACON:
739 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
740 rx_status);
741 break;
742 case IEEE80211_STYPE_AUTH:
743 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
744 break;
745 }
746
747 kfree_skb(skb);
748}
749
750static void ieee80211_ibss_work(struct work_struct *work)
751{
752 struct ieee80211_sub_if_data *sdata =
753 container_of(work, struct ieee80211_sub_if_data, u.ibss.work);
754 struct ieee80211_local *local = sdata->local;
755 struct ieee80211_if_ibss *ifibss;
756 struct sk_buff *skb;
757
Johannes Berg5bb644a2009-05-17 11:40:42 +0200758 if (WARN_ON(local->suspended))
759 return;
760
Johannes Berg9607e6b2009-12-23 13:15:31 +0100761 if (!ieee80211_sdata_running(sdata))
Johannes Berg46900292009-02-15 12:44:28 +0100762 return;
763
Helmut Schaafbe9c422009-07-23 12:14:04 +0200764 if (local->scanning)
Johannes Berg46900292009-02-15 12:44:28 +0100765 return;
766
767 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_ADHOC))
768 return;
769 ifibss = &sdata->u.ibss;
770
771 while ((skb = skb_dequeue(&ifibss->skb_queue)))
772 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
773
774 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request))
775 return;
776
777 switch (ifibss->state) {
778 case IEEE80211_IBSS_MLME_SEARCH:
779 ieee80211_sta_find_ibss(sdata);
780 break;
781 case IEEE80211_IBSS_MLME_JOINED:
782 ieee80211_sta_merge_ibss(sdata);
783 break;
784 default:
785 WARN_ON(1);
786 break;
787 }
788}
789
790static void ieee80211_ibss_timer(unsigned long data)
791{
792 struct ieee80211_sub_if_data *sdata =
793 (struct ieee80211_sub_if_data *) data;
794 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
795 struct ieee80211_local *local = sdata->local;
796
Johannes Berg5bb644a2009-05-17 11:40:42 +0200797 if (local->quiescing) {
798 ifibss->timer_running = true;
799 return;
800 }
801
Johannes Berg46900292009-02-15 12:44:28 +0100802 set_bit(IEEE80211_IBSS_REQ_RUN, &ifibss->request);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400803 ieee80211_queue_work(&local->hw, &ifibss->work);
Johannes Berg46900292009-02-15 12:44:28 +0100804}
805
Johannes Berg5bb644a2009-05-17 11:40:42 +0200806#ifdef CONFIG_PM
807void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
808{
809 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
810
811 cancel_work_sync(&ifibss->work);
812 if (del_timer_sync(&ifibss->timer))
813 ifibss->timer_running = true;
814}
815
816void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
817{
818 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
819
820 if (ifibss->timer_running) {
821 add_timer(&ifibss->timer);
822 ifibss->timer_running = false;
823 }
824}
825#endif
826
Johannes Berg46900292009-02-15 12:44:28 +0100827void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
828{
829 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
830
831 INIT_WORK(&ifibss->work, ieee80211_ibss_work);
832 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
833 (unsigned long) sdata);
834 skb_queue_head_init(&ifibss->skb_queue);
Johannes Berg46900292009-02-15 12:44:28 +0100835}
836
837/* scan finished notification */
838void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
839{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200840 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +0100841
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200842 mutex_lock(&local->iflist_mtx);
843 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100844 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +0200845 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200846 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
847 continue;
Johannes Berg0e41f712009-04-23 11:48:56 +0200848 if (!sdata->u.ibss.ssid_len)
849 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200850 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg51f98f12009-10-11 11:47:57 +0200851 mod_timer(&sdata->u.ibss.timer, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100852 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200853 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100854}
855
856ieee80211_rx_result
Johannes Bergf1d58c22009-06-17 13:13:00 +0200857ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +0100858{
859 struct ieee80211_local *local = sdata->local;
860 struct ieee80211_mgmt *mgmt;
861 u16 fc;
862
863 if (skb->len < 24)
864 return RX_DROP_MONITOR;
865
866 mgmt = (struct ieee80211_mgmt *) skb->data;
867 fc = le16_to_cpu(mgmt->frame_control);
868
869 switch (fc & IEEE80211_FCTL_STYPE) {
870 case IEEE80211_STYPE_PROBE_RESP:
871 case IEEE80211_STYPE_BEACON:
Johannes Berg46900292009-02-15 12:44:28 +0100872 case IEEE80211_STYPE_PROBE_REQ:
873 case IEEE80211_STYPE_AUTH:
874 skb_queue_tail(&sdata->u.ibss.skb_queue, skb);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400875 ieee80211_queue_work(&local->hw, &sdata->u.ibss.work);
Johannes Berg46900292009-02-15 12:44:28 +0100876 return RX_QUEUED;
877 }
878
879 return RX_DROP_MONITOR;
880}
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200881
882int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
883 struct cfg80211_ibss_params *params)
884{
885 struct sk_buff *skb;
886
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200887 if (params->bssid) {
888 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
889 sdata->u.ibss.fixed_bssid = true;
890 } else
891 sdata->u.ibss.fixed_bssid = false;
892
Johannes Bergfffd0932009-07-08 14:22:54 +0200893 sdata->u.ibss.privacy = params->privacy;
894
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200895 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
896
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200897 sdata->u.ibss.channel = params->channel;
898 sdata->u.ibss.fixed_channel = params->channel_fixed;
899
900 if (params->ie) {
901 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
902 GFP_KERNEL);
903 if (sdata->u.ibss.ie)
904 sdata->u.ibss.ie_len = params->ie_len;
905 }
906
907 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
908 36 /* bitrates */ +
909 34 /* SSID */ +
910 3 /* DS params */ +
911 4 /* IBSS params */ +
912 params->ie_len);
913 if (!skb)
914 return -ENOMEM;
915
916 sdata->u.ibss.skb = skb;
917 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
918 sdata->u.ibss.ibss_join_req = jiffies;
919
Johannes Berg0e41f712009-04-23 11:48:56 +0200920 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
921
922 /*
923 * The ssid_len setting below is used to see whether
924 * we are active, and we need all other settings
925 * before that may get visible.
926 */
927 mb();
928
929 sdata->u.ibss.ssid_len = params->ssid_len;
930
Johannes Berg5cff20e2009-04-29 12:26:17 +0200931 ieee80211_recalc_idle(sdata->local);
932
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200933 set_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400934 ieee80211_queue_work(&sdata->local->hw, &sdata->u.ibss.work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200935
936 return 0;
937}
938
939int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
940{
941 struct sk_buff *skb;
942
943 del_timer_sync(&sdata->u.ibss.timer);
944 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
945 cancel_work_sync(&sdata->u.ibss.work);
946 clear_bit(IEEE80211_IBSS_REQ_RUN, &sdata->u.ibss.request);
947
948 sta_info_flush(sdata->local, sdata);
949
950 /* remove beacon */
951 kfree(sdata->u.ibss.ie);
952 skb = sdata->u.ibss.presp;
953 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200954 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200955 synchronize_rcu();
956 kfree_skb(skb);
957
958 skb_queue_purge(&sdata->u.ibss.skb_queue);
959 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200960 sdata->u.ibss.ssid_len = 0;
961
962 ieee80211_recalc_idle(sdata->local);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200963
964 return 0;
965}