blob: 775fb63471c457a68bef8439f59e9335a2392b5b [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
Johannes Berg7a17a332010-07-21 11:30:27 +020046 lockdep_assert_held(&sdata->u.ibss.mtx);
47
Johannes Berg46900292009-02-15 12:44:28 +010048 if (len < 24 + 6)
49 return;
50
51 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
52 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
53 status_code = le16_to_cpu(mgmt->u.auth.status_code);
54
55 /*
56 * IEEE 802.11 standard does not require authentication in IBSS
57 * networks and most implementations do not seem to use it.
58 * However, try to reply to authentication attempts if someone
59 * has actually implemented this.
60 */
61 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
62 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
Johannes Bergfffd0932009-07-08 14:22:54 +020063 sdata->u.ibss.bssid, NULL, 0, 0);
Johannes Berg46900292009-02-15 12:44:28 +010064}
65
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020066static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
67 const u8 *bssid, const int beacon_int,
68 struct ieee80211_channel *chan,
Johannes Bergb59066a2009-05-12 21:18:38 +020069 const u32 basic_rates,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020070 const u16 capability, u64 tsf)
Johannes Berg46900292009-02-15 12:44:28 +010071{
72 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
73 struct ieee80211_local *local = sdata->local;
Johannes Bergb59066a2009-05-12 21:18:38 +020074 int rates, i;
Johannes Berg46900292009-02-15 12:44:28 +010075 struct sk_buff *skb;
76 struct ieee80211_mgmt *mgmt;
77 u8 *pos;
78 struct ieee80211_supported_band *sband;
Johannes Bergf446d102009-10-28 15:12:32 +010079 struct cfg80211_bss *bss;
Johannes Berg57c4d7b2009-04-23 16:10:04 +020080 u32 bss_change;
Johannes Bergb59066a2009-05-12 21:18:38 +020081 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
Johannes Berg24487982009-04-23 18:52:52 +020082
Johannes Berg7a17a332010-07-21 11:30:27 +020083 lockdep_assert_held(&ifibss->mtx);
84
Johannes Berg24487982009-04-23 18:52:52 +020085 /* Reset own TSF to allow time synchronization work. */
86 drv_reset_tsf(local);
Johannes Berg46900292009-02-15 12:44:28 +010087
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020088 skb = ifibss->skb;
89 rcu_assign_pointer(ifibss->presp, NULL);
90 synchronize_rcu();
91 skb->data = skb->head;
92 skb->len = 0;
93 skb_reset_tail_pointer(skb);
94 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
Johannes Berg46900292009-02-15 12:44:28 +010095
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020096 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
97 sta_info_flush(sdata->local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010098
Johannes Berg49b5c7f2010-04-29 21:34:01 +020099 /* if merging, indicate to driver that we leave the old IBSS */
100 if (sdata->vif.bss_conf.ibss_joined) {
101 sdata->vif.bss_conf.ibss_joined = false;
102 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
103 }
104
Johannes Berg46900292009-02-15 12:44:28 +0100105 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100106
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200107 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
Johannes Berg46900292009-02-15 12:44:28 +0100108
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200109 local->oper_channel = chan;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200110 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200111 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200112
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200113 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg46900292009-02-15 12:44:28 +0100114
Johannes Bergb59066a2009-05-12 21:18:38 +0200115 /* build supported rates array */
116 pos = supp_rates;
117 for (i = 0; i < sband->n_bitrates; i++) {
118 int rate = sband->bitrates[i].bitrate;
119 u8 basic = 0;
120 if (basic_rates & BIT(i))
121 basic = 0x80;
122 *pos++ = basic | (u8) (rate / 5);
123 }
124
Johannes Berg46900292009-02-15 12:44:28 +0100125 /* Build IBSS probe response */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200126 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
Johannes Berg46900292009-02-15 12:44:28 +0100127 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
128 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
129 IEEE80211_STYPE_PROBE_RESP);
130 memset(mgmt->da, 0xff, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100131 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100132 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200133 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530134 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100135 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
136
137 pos = skb_put(skb, 2 + ifibss->ssid_len);
138 *pos++ = WLAN_EID_SSID;
139 *pos++ = ifibss->ssid_len;
140 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
141
Johannes Bergb59066a2009-05-12 21:18:38 +0200142 rates = sband->n_bitrates;
Johannes Berg46900292009-02-15 12:44:28 +0100143 if (rates > 8)
144 rates = 8;
145 pos = skb_put(skb, 2 + rates);
146 *pos++ = WLAN_EID_SUPP_RATES;
147 *pos++ = rates;
148 memcpy(pos, supp_rates, rates);
149
150 if (sband->band == IEEE80211_BAND_2GHZ) {
151 pos = skb_put(skb, 2 + 1);
152 *pos++ = WLAN_EID_DS_PARAMS;
153 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200154 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100155 }
156
157 pos = skb_put(skb, 2 + 2);
158 *pos++ = WLAN_EID_IBSS_PARAMS;
159 *pos++ = 2;
160 /* FIX: set ATIM window based on scan results */
161 *pos++ = 0;
162 *pos++ = 0;
163
Johannes Bergb59066a2009-05-12 21:18:38 +0200164 if (sband->n_bitrates > 8) {
165 rates = sband->n_bitrates - 8;
Johannes Berg46900292009-02-15 12:44:28 +0100166 pos = skb_put(skb, 2 + rates);
167 *pos++ = WLAN_EID_EXT_SUPP_RATES;
168 *pos++ = rates;
169 memcpy(pos, &supp_rates[8], rates);
170 }
171
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200172 if (ifibss->ie_len)
173 memcpy(skb_put(skb, ifibss->ie_len),
174 ifibss->ie, ifibss->ie_len);
175
Bruno Randolf9eba6122010-10-04 11:17:30 +0900176 if (local->hw.queues >= 4) {
177 pos = skb_put(skb, 9);
178 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
179 *pos++ = 7; /* len */
180 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
181 *pos++ = 0x50;
182 *pos++ = 0xf2;
183 *pos++ = 2; /* WME */
184 *pos++ = 0; /* WME info */
185 *pos++ = 1; /* WME ver */
186 *pos++ = 0; /* U-APSD no in use */
187 }
188
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200189 rcu_assign_pointer(ifibss->presp, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100190
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200191 sdata->vif.bss_conf.beacon_int = beacon_int;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300192 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200193 bss_change = BSS_CHANGED_BEACON_INT;
194 bss_change |= ieee80211_reset_erp_info(sdata);
195 bss_change |= BSS_CHANGED_BSSID;
196 bss_change |= BSS_CHANGED_BEACON;
197 bss_change |= BSS_CHANGED_BEACON_ENABLED;
Teemu Paasikivi392cfdb2010-06-14 12:55:32 +0300198 bss_change |= BSS_CHANGED_BASIC_RATES;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200199 bss_change |= BSS_CHANGED_IBSS;
200 sdata->vif.bss_conf.ibss_joined = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200201 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100202
Johannes Bergb59066a2009-05-12 21:18:38 +0200203 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
Johannes Berg46900292009-02-15 12:44:28 +0100204
Johannes Berg46900292009-02-15 12:44:28 +0100205 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200206 mod_timer(&ifibss->timer,
207 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100208
Johannes Bergf446d102009-10-28 15:12:32 +0100209 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
210 mgmt, skb->len, 0, GFP_KERNEL);
211 cfg80211_put_bss(bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200212 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100213}
214
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200215static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
216 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100217{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100218 struct cfg80211_bss *cbss =
219 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200220 struct ieee80211_supported_band *sband;
221 u32 basic_rates;
222 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100223 u16 beacon_int = cbss->beacon_interval;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200224
Johannes Berg7a17a332010-07-21 11:30:27 +0200225 lockdep_assert_held(&sdata->u.ibss.mtx);
226
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200227 if (beacon_int < 10)
228 beacon_int = 10;
229
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100230 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Johannes Bergb59066a2009-05-12 21:18:38 +0200231
232 basic_rates = 0;
233
234 for (i = 0; i < bss->supp_rates_len; i++) {
235 int rate = (bss->supp_rates[i] & 0x7f) * 5;
236 bool is_basic = !!(bss->supp_rates[i] & 0x80);
237
238 for (j = 0; j < sband->n_bitrates; j++) {
239 if (sband->bitrates[j].bitrate == rate) {
240 if (is_basic)
241 basic_rates |= BIT(j);
242 break;
243 }
244 }
245 }
246
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100247 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200248 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100249 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200250 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100251 cbss->capability,
252 cbss->tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100253}
254
255static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
256 struct ieee80211_mgmt *mgmt,
257 size_t len,
258 struct ieee80211_rx_status *rx_status,
259 struct ieee802_11_elems *elems,
260 bool beacon)
261{
262 struct ieee80211_local *local = sdata->local;
263 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100264 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100265 struct ieee80211_bss *bss;
266 struct sta_info *sta;
267 struct ieee80211_channel *channel;
268 u64 beacon_timestamp, rx_timestamp;
269 u32 supp_rates = 0;
270 enum ieee80211_band band = rx_status->band;
271
272 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900273 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
274 band);
Johannes Berg46900292009-02-15 12:44:28 +0100275 else
276 freq = rx_status->freq;
277
278 channel = ieee80211_get_channel(local->hw.wiphy, freq);
279
280 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
281 return;
282
Bruno Randolf9eba6122010-10-04 11:17:30 +0900283 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg46900292009-02-15 12:44:28 +0100284 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
Johannes Berg46900292009-02-15 12:44:28 +0100285
286 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100287 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100288
Bruno Randolf9eba6122010-10-04 11:17:30 +0900289 if (elems->supp_rates) {
290 supp_rates = ieee80211_sta_get_rates(local, elems,
291 band);
292 if (sta) {
293 u32 prev_rates;
Johannes Berg46900292009-02-15 12:44:28 +0100294
Bruno Randolf9eba6122010-10-04 11:17:30 +0900295 prev_rates = sta->sta.supp_rates[band];
296 /* make sure mandatory rates are always added */
297 sta->sta.supp_rates[band] = supp_rates |
298 ieee80211_mandatory_rates(local, band);
299
300 if (sta->sta.supp_rates[band] != prev_rates) {
Johannes Berg46900292009-02-15 12:44:28 +0100301#ifdef CONFIG_MAC80211_IBSS_DEBUG
Bruno Randolf9eba6122010-10-04 11:17:30 +0900302 printk(KERN_DEBUG
303 "%s: updated supp_rates set "
304 "for %pM based on beacon"
305 "/probe_resp (0x%x -> 0x%x)\n",
306 sdata->name, sta->sta.addr,
307 prev_rates,
308 sta->sta.supp_rates[band]);
Johannes Berg46900292009-02-15 12:44:28 +0100309#endif
Bruno Randolf9eba6122010-10-04 11:17:30 +0900310 rate_control_rate_init(sta);
311 }
312 } else
313 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
314 mgmt->sa, supp_rates,
315 GFP_ATOMIC);
Johannes Berg34e89502010-02-03 13:59:58 +0100316 }
Bruno Randolf9eba6122010-10-04 11:17:30 +0900317
318 if (sta && elems->wmm_info)
319 set_sta_flags(sta, WLAN_STA_WME);
320
321 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100322 }
323
324 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
325 channel, beacon);
326 if (!bss)
327 return;
328
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100329 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
330
Johannes Berg46900292009-02-15 12:44:28 +0100331 /* was just updated in ieee80211_bss_info_update */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100332 beacon_timestamp = cbss->tsf;
Johannes Berg46900292009-02-15 12:44:28 +0100333
334 /* check if we need to merge IBSS */
335
Johannes Berg46900292009-02-15 12:44:28 +0100336 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100337 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100338 goto put_bss;
339
340 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100341 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100342 goto put_bss;
343
344 /* different channel */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100345 if (cbss->channel != local->oper_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100346 goto put_bss;
347
348 /* different SSID */
349 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
350 memcmp(elems->ssid, sdata->u.ibss.ssid,
351 sdata->u.ibss.ssid_len))
352 goto put_bss;
353
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100354 /* same BSSID */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100355 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100356 goto put_bss;
357
Johannes Berg46900292009-02-15 12:44:28 +0100358 if (rx_status->flag & RX_FLAG_TSFT) {
359 /*
360 * For correct IBSS merging we need mactime; since mactime is
361 * defined as the time the first data symbol of the frame hits
362 * the PHY, and the timestamp of the beacon is defined as "the
363 * time that the data symbol containing the first bit of the
364 * timestamp is transmitted to the PHY plus the transmitting
365 * STA's delays through its local PHY from the MAC-PHY
366 * interface to its interface with the WM" (802.11 11.1.2)
367 * - equals the time this bit arrives at the receiver - we have
368 * to take into account the offset between the two.
369 *
370 * E.g. at 1 MBit that means mactime is 192 usec earlier
371 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
372 */
373 int rate;
374
375 if (rx_status->flag & RX_FLAG_HT)
376 rate = 65; /* TODO: HT rates */
377 else
378 rate = local->hw.wiphy->bands[band]->
379 bitrates[rx_status->rate_idx].bitrate;
380
381 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
Johannes Berg24487982009-04-23 18:52:52 +0200382 } else {
383 /*
384 * second best option: get current TSF
385 * (will return -1 if not supported)
386 */
387 rx_timestamp = drv_get_tsf(local);
388 }
Johannes Berg46900292009-02-15 12:44:28 +0100389
390#ifdef CONFIG_MAC80211_IBSS_DEBUG
391 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
392 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
393 mgmt->sa, mgmt->bssid,
394 (unsigned long long)rx_timestamp,
395 (unsigned long long)beacon_timestamp,
396 (unsigned long long)(rx_timestamp - beacon_timestamp),
397 jiffies);
398#endif
399
Alina Friedrichsen4a332a32009-02-22 18:19:33 +0100400 /* give slow hardware some time to do the TSF sync */
401 if (rx_timestamp < IEEE80211_IBSS_MERGE_DELAY)
402 goto put_bss;
403
Johannes Berg46900292009-02-15 12:44:28 +0100404 if (beacon_timestamp > rx_timestamp) {
405#ifdef CONFIG_MAC80211_IBSS_DEBUG
406 printk(KERN_DEBUG "%s: beacon TSF higher than "
407 "local TSF - IBSS merge with BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100408 sdata->name, mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100409#endif
410 ieee80211_sta_join_ibss(sdata, bss);
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900411 supp_rates = ieee80211_sta_get_rates(local, elems, band);
Johannes Berg34e89502010-02-03 13:59:58 +0100412 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
413 supp_rates, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100414 }
415
416 put_bss:
417 ieee80211_rx_bss_put(local, bss);
418}
419
420/*
421 * Add a new IBSS station, will also be called by the RX code when,
422 * in IBSS mode, receiving a frame from a yet-unknown station, hence
423 * must be callable in atomic context.
424 */
425struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Johannes Berg34e89502010-02-03 13:59:58 +0100426 u8 *bssid,u8 *addr, u32 supp_rates,
427 gfp_t gfp)
Johannes Berg46900292009-02-15 12:44:28 +0100428{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100429 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100430 struct ieee80211_local *local = sdata->local;
431 struct sta_info *sta;
432 int band = local->hw.conf.channel->band;
433
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200434 /*
435 * XXX: Consider removing the least recently used entry and
436 * allow new one to be added.
437 */
Johannes Berg46900292009-02-15 12:44:28 +0100438 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200439 if (net_ratelimit())
440 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100441 sdata->name, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100442 return NULL;
443 }
444
Felix Fietkau2e10d332009-12-20 19:07:09 +0100445 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
446 return NULL;
447
Johannes Berg46900292009-02-15 12:44:28 +0100448 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
449 return NULL;
450
451#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700452 wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
453 addr, sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100454#endif
455
Johannes Berg34e89502010-02-03 13:59:58 +0100456 sta = sta_info_alloc(sdata, addr, gfp);
Johannes Berg46900292009-02-15 12:44:28 +0100457 if (!sta)
458 return NULL;
459
Rajkumar Manoharanc8716d92010-10-23 10:59:57 +0530460 sta->last_rx = jiffies;
Johannes Berg46900292009-02-15 12:44:28 +0100461 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
462
463 /* make sure mandatory rates are always added */
464 sta->sta.supp_rates[band] = supp_rates |
465 ieee80211_mandatory_rates(local, band);
466
467 rate_control_rate_init(sta);
468
Johannes Berg34e89502010-02-03 13:59:58 +0100469 /* If it fails, maybe we raced another insertion? */
Johannes Berg46900292009-02-15 12:44:28 +0100470 if (sta_info_insert(sta))
Johannes Berg34e89502010-02-03 13:59:58 +0100471 return sta_info_get(sdata, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100472 return sta;
473}
474
475static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
476{
477 struct ieee80211_local *local = sdata->local;
478 int active = 0;
479 struct sta_info *sta;
480
Johannes Berg7a17a332010-07-21 11:30:27 +0200481 lockdep_assert_held(&sdata->u.ibss.mtx);
482
Johannes Berg46900292009-02-15 12:44:28 +0100483 rcu_read_lock();
484
485 list_for_each_entry_rcu(sta, &local->sta_list, list) {
486 if (sta->sdata == sdata &&
487 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
488 jiffies)) {
489 active++;
490 break;
491 }
492 }
493
494 rcu_read_unlock();
495
496 return active;
497}
498
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100499/*
500 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
501 */
Johannes Berg46900292009-02-15 12:44:28 +0100502
503static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
504{
505 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
506
Johannes Berg7a17a332010-07-21 11:30:27 +0200507 lockdep_assert_held(&ifibss->mtx);
508
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200509 mod_timer(&ifibss->timer,
510 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100511
512 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200513
Sujith450aae32009-11-02 12:33:23 +0530514 if (time_before(jiffies, ifibss->last_scan_completed +
515 IEEE80211_IBSS_MERGE_INTERVAL))
516 return;
517
Johannes Berg46900292009-02-15 12:44:28 +0100518 if (ieee80211_sta_active_ibss(sdata))
519 return;
520
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200521 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100522 return;
523
524 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Johannes Berg47846c92009-11-25 17:46:19 +0100525 "IBSS networks with same SSID (merge)\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100526
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200527 ieee80211_request_internal_scan(sdata,
528 ifibss->ssid, ifibss->ssid_len,
529 ifibss->fixed_channel ? ifibss->channel : NULL);
Johannes Berg46900292009-02-15 12:44:28 +0100530}
531
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200532static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100533{
534 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
535 struct ieee80211_local *local = sdata->local;
536 struct ieee80211_supported_band *sband;
Johannes Berg46900292009-02-15 12:44:28 +0100537 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100538 u16 capability;
539 int i;
540
Johannes Berg7a17a332010-07-21 11:30:27 +0200541 lockdep_assert_held(&ifibss->mtx);
542
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200543 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100544 memcpy(bssid, ifibss->bssid, ETH_ALEN);
545 } else {
546 /* Generate random, not broadcast, locally administered BSSID. Mix in
547 * own MAC address to make sure that devices that do not have proper
548 * random number generator get different BSSID. */
549 get_random_bytes(bssid, ETH_ALEN);
550 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100551 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100552 bssid[0] &= ~0x01;
553 bssid[0] |= 0x02;
554 }
555
556 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100557 sdata->name, bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100558
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200559 sband = local->hw.wiphy->bands[ifibss->channel->band];
Johannes Berg46900292009-02-15 12:44:28 +0100560
Johannes Berg46900292009-02-15 12:44:28 +0100561 capability = WLAN_CAPABILITY_IBSS;
562
Johannes Bergfffd0932009-07-08 14:22:54 +0200563 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100564 capability |= WLAN_CAPABILITY_PRIVACY;
565 else
566 sdata->drop_unencrypted = 0;
567
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200568 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300569 ifibss->channel, ifibss->basic_rates,
Johannes Bergb59066a2009-05-12 21:18:38 +0200570 capability, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100571}
572
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100573/*
574 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
575 */
576
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200577static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100578{
579 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
580 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100581 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200582 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100583 const u8 *bssid = NULL;
584 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200585 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100586
Johannes Berg7a17a332010-07-21 11:30:27 +0200587 lockdep_assert_held(&ifibss->mtx);
588
Johannes Berg46900292009-02-15 12:44:28 +0100589 active_ibss = ieee80211_sta_active_ibss(sdata);
590#ifdef CONFIG_MAC80211_IBSS_DEBUG
591 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100592 sdata->name, active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100593#endif /* CONFIG_MAC80211_IBSS_DEBUG */
594
595 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200596 return;
Johannes Berg46900292009-02-15 12:44:28 +0100597
Johannes Berge0d61882009-05-12 20:47:32 +0200598 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200599 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200600 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200601 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100602 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200603 if (ifibss->fixed_channel)
604 chan = ifibss->channel;
605 if (!is_zero_ether_addr(ifibss->bssid))
606 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100607 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
608 ifibss->ssid, ifibss->ssid_len,
609 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
610 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100611
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100612 if (cbss) {
613 struct ieee80211_bss *bss;
614
615 bss = (void *)cbss->priv;
Johannes Berg46900292009-02-15 12:44:28 +0100616#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg46900292009-02-15 12:44:28 +0100617 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100618 "%pM\n", cbss->bssid, ifibss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100619#endif /* CONFIG_MAC80211_IBSS_DEBUG */
620
Johannes Berg46900292009-02-15 12:44:28 +0100621 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
622 " based on configured SSID\n",
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100623 sdata->name, cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100624
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200625 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100626 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200627 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700628 }
Johannes Berg46900292009-02-15 12:44:28 +0100629
630#ifdef CONFIG_MAC80211_IBSS_DEBUG
631 printk(KERN_DEBUG " did not try to join ibss\n");
632#endif /* CONFIG_MAC80211_IBSS_DEBUG */
633
634 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100635 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100636 IEEE80211_SCAN_INTERVAL)) {
637 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Johannes Berg47846c92009-11-25 17:46:19 +0100638 "join\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100639
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200640 ieee80211_request_internal_scan(sdata,
641 ifibss->ssid, ifibss->ssid_len,
642 ifibss->fixed_channel ? ifibss->channel : NULL);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100643 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100644 int interval = IEEE80211_SCAN_INTERVAL;
645
646 if (time_after(jiffies, ifibss->ibss_join_req +
647 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200648 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
649 ieee80211_sta_create_ibss(sdata);
650 return;
651 }
Johannes Berg46900292009-02-15 12:44:28 +0100652 printk(KERN_DEBUG "%s: IBSS not allowed on"
Johannes Berg47846c92009-11-25 17:46:19 +0100653 " %d MHz\n", sdata->name,
Johannes Berg46900292009-02-15 12:44:28 +0100654 local->hw.conf.channel->center_freq);
655
656 /* No IBSS found - decrease scan interval and continue
657 * scanning. */
658 interval = IEEE80211_SCAN_INTERVAL_SLOW;
659 }
660
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200661 mod_timer(&ifibss->timer,
662 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100663 }
Johannes Berg46900292009-02-15 12:44:28 +0100664}
665
666static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
667 struct ieee80211_mgmt *mgmt,
668 size_t len)
669{
670 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
671 struct ieee80211_local *local = sdata->local;
672 int tx_last_beacon;
673 struct sk_buff *skb;
674 struct ieee80211_mgmt *resp;
675 u8 *pos, *end;
676
Johannes Berg7a17a332010-07-21 11:30:27 +0200677 lockdep_assert_held(&ifibss->mtx);
678
Johannes Berg46900292009-02-15 12:44:28 +0100679 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200680 len < 24 + 2 || !ifibss->presp)
Johannes Berg46900292009-02-15 12:44:28 +0100681 return;
682
Johannes Berg24487982009-04-23 18:52:52 +0200683 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100684
685#ifdef CONFIG_MAC80211_IBSS_DEBUG
686 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
687 " (tx_last_beacon=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100688 sdata->name, mgmt->sa, mgmt->da,
Johannes Berg46900292009-02-15 12:44:28 +0100689 mgmt->bssid, tx_last_beacon);
690#endif /* CONFIG_MAC80211_IBSS_DEBUG */
691
692 if (!tx_last_beacon)
693 return;
694
695 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
696 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
697 return;
698
699 end = ((u8 *) mgmt) + len;
700 pos = mgmt->u.probe_req.variable;
701 if (pos[0] != WLAN_EID_SSID ||
702 pos + 2 + pos[1] > end) {
703#ifdef CONFIG_MAC80211_IBSS_DEBUG
704 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
705 "from %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100706 sdata->name, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100707#endif
708 return;
709 }
710 if (pos[1] != 0 &&
711 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100712 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100713 /* Ignore ProbeReq for foreign SSID */
714 return;
715 }
716
717 /* Reply with ProbeResp */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200718 skb = skb_copy(ifibss->presp, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100719 if (!skb)
720 return;
721
722 resp = (struct ieee80211_mgmt *) skb->data;
723 memcpy(resp->da, mgmt->sa, ETH_ALEN);
724#ifdef CONFIG_MAC80211_IBSS_DEBUG
725 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100726 sdata->name, resp->da);
Johannes Berg46900292009-02-15 12:44:28 +0100727#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Berg62ae67b2009-11-18 18:42:05 +0100728 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
729 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100730}
731
732static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
733 struct ieee80211_mgmt *mgmt,
734 size_t len,
735 struct ieee80211_rx_status *rx_status)
736{
737 size_t baselen;
738 struct ieee802_11_elems elems;
739
Johannes Berg47846c92009-11-25 17:46:19 +0100740 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Johannes Berg46900292009-02-15 12:44:28 +0100741 return; /* ignore ProbeResp to foreign address */
742
743 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
744 if (baselen > len)
745 return;
746
747 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
748 &elems);
749
750 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
751}
752
753static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
754 struct ieee80211_mgmt *mgmt,
755 size_t len,
756 struct ieee80211_rx_status *rx_status)
757{
758 size_t baselen;
759 struct ieee802_11_elems elems;
760
761 /* Process beacon from the current BSS */
762 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
763 if (baselen > len)
764 return;
765
766 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
767
768 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
769}
770
Johannes Berg1fa57d02010-06-10 10:21:32 +0200771void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
772 struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +0100773{
774 struct ieee80211_rx_status *rx_status;
775 struct ieee80211_mgmt *mgmt;
776 u16 fc;
777
Johannes Bergf1d58c22009-06-17 13:13:00 +0200778 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +0100779 mgmt = (struct ieee80211_mgmt *) skb->data;
780 fc = le16_to_cpu(mgmt->frame_control);
781
Johannes Berg7a17a332010-07-21 11:30:27 +0200782 mutex_lock(&sdata->u.ibss.mtx);
783
Tim Harveyc926d002010-12-09 10:43:13 -0800784 if (!sdata->u.ibss.ssid_len)
785 goto mgmt_out; /* not ready to merge yet */
786
Johannes Berg46900292009-02-15 12:44:28 +0100787 switch (fc & IEEE80211_FCTL_STYPE) {
788 case IEEE80211_STYPE_PROBE_REQ:
789 ieee80211_rx_mgmt_probe_req(sdata, mgmt, skb->len);
790 break;
791 case IEEE80211_STYPE_PROBE_RESP:
792 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
793 rx_status);
794 break;
795 case IEEE80211_STYPE_BEACON:
796 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
797 rx_status);
798 break;
799 case IEEE80211_STYPE_AUTH:
800 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
801 break;
802 }
Johannes Berg7a17a332010-07-21 11:30:27 +0200803
Tim Harveyc926d002010-12-09 10:43:13 -0800804 mgmt_out:
Johannes Berg7a17a332010-07-21 11:30:27 +0200805 mutex_unlock(&sdata->u.ibss.mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100806}
807
Johannes Berg1fa57d02010-06-10 10:21:32 +0200808void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100809{
Johannes Berg1fa57d02010-06-10 10:21:32 +0200810 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100811
Johannes Berg7a17a332010-07-21 11:30:27 +0200812 mutex_lock(&ifibss->mtx);
813
814 /*
815 * Work could be scheduled after scan or similar
816 * when we aren't even joined (or trying) with a
817 * network.
818 */
819 if (!ifibss->ssid_len)
820 goto out;
Johannes Berg46900292009-02-15 12:44:28 +0100821
822 switch (ifibss->state) {
823 case IEEE80211_IBSS_MLME_SEARCH:
824 ieee80211_sta_find_ibss(sdata);
825 break;
826 case IEEE80211_IBSS_MLME_JOINED:
827 ieee80211_sta_merge_ibss(sdata);
828 break;
829 default:
830 WARN_ON(1);
831 break;
832 }
Johannes Berg46900292009-02-15 12:44:28 +0100833
Johannes Berg7a17a332010-07-21 11:30:27 +0200834 out:
835 mutex_unlock(&ifibss->mtx);
Johannes Berg3a4d4aa2010-05-26 16:41:40 +0200836}
837
Johannes Berg46900292009-02-15 12:44:28 +0100838static void ieee80211_ibss_timer(unsigned long data)
839{
840 struct ieee80211_sub_if_data *sdata =
841 (struct ieee80211_sub_if_data *) data;
842 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
843 struct ieee80211_local *local = sdata->local;
844
Johannes Berg5bb644a2009-05-17 11:40:42 +0200845 if (local->quiescing) {
846 ifibss->timer_running = true;
847 return;
848 }
849
Johannes Berg7a17a332010-07-21 11:30:27 +0200850 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100851}
852
Johannes Berg5bb644a2009-05-17 11:40:42 +0200853#ifdef CONFIG_PM
854void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
855{
856 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
857
Johannes Berg5bb644a2009-05-17 11:40:42 +0200858 if (del_timer_sync(&ifibss->timer))
859 ifibss->timer_running = true;
860}
861
862void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
863{
864 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
865
866 if (ifibss->timer_running) {
867 add_timer(&ifibss->timer);
868 ifibss->timer_running = false;
869 }
870}
871#endif
872
Johannes Berg46900292009-02-15 12:44:28 +0100873void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
874{
875 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
876
Johannes Berg46900292009-02-15 12:44:28 +0100877 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
878 (unsigned long) sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200879 mutex_init(&ifibss->mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100880}
881
882/* scan finished notification */
883void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
884{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200885 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +0100886
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200887 mutex_lock(&local->iflist_mtx);
888 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100889 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +0200890 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200891 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
892 continue;
893 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg7a17a332010-07-21 11:30:27 +0200894 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100895 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200896 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100897}
898
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200899int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
900 struct cfg80211_ibss_params *params)
901{
902 struct sk_buff *skb;
903
Johannes Berg7a17a332010-07-21 11:30:27 +0200904 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
905 36 /* bitrates */ +
906 34 /* SSID */ +
907 3 /* DS params */ +
908 4 /* IBSS params */ +
909 params->ie_len);
910 if (!skb)
911 return -ENOMEM;
912
913 mutex_lock(&sdata->u.ibss.mtx);
914
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200915 if (params->bssid) {
916 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
917 sdata->u.ibss.fixed_bssid = true;
918 } else
919 sdata->u.ibss.fixed_bssid = false;
920
Johannes Bergfffd0932009-07-08 14:22:54 +0200921 sdata->u.ibss.privacy = params->privacy;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300922 sdata->u.ibss.basic_rates = params->basic_rates;
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100923 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
924 sizeof(params->mcast_rate));
Johannes Bergfffd0932009-07-08 14:22:54 +0200925
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200926 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
927
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200928 sdata->u.ibss.channel = params->channel;
929 sdata->u.ibss.fixed_channel = params->channel_fixed;
930
Johannes Bergadfba3c2010-05-05 15:33:55 +0200931 /* fix ourselves to that channel now already */
932 if (params->channel_fixed) {
933 sdata->local->oper_channel = params->channel;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200934 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
935 NL80211_CHAN_NO_HT));
Johannes Bergadfba3c2010-05-05 15:33:55 +0200936 }
937
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200938 if (params->ie) {
939 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
940 GFP_KERNEL);
941 if (sdata->u.ibss.ie)
942 sdata->u.ibss.ie_len = params->ie_len;
943 }
944
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200945 sdata->u.ibss.skb = skb;
946 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
947 sdata->u.ibss.ibss_join_req = jiffies;
948
Johannes Berg0e41f712009-04-23 11:48:56 +0200949 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
Johannes Berg0e41f712009-04-23 11:48:56 +0200950 sdata->u.ibss.ssid_len = params->ssid_len;
951
Johannes Berg7da7cc12010-08-05 17:02:38 +0200952 mutex_unlock(&sdata->u.ibss.mtx);
953
954 mutex_lock(&sdata->local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200955 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +0200956 mutex_unlock(&sdata->local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200957
Johannes Berg64592c82010-06-10 10:21:31 +0200958 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200959
960 return 0;
961}
962
963int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
964{
965 struct sk_buff *skb;
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +0300966 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
967 struct ieee80211_local *local = sdata->local;
968 struct cfg80211_bss *cbss;
969 u16 capability;
Johannes Berg7a17a332010-07-21 11:30:27 +0200970 int active_ibss;
971
972 mutex_lock(&sdata->u.ibss.mtx);
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +0300973
974 active_ibss = ieee80211_sta_active_ibss(sdata);
975
976 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
977 capability = WLAN_CAPABILITY_IBSS;
978
979 if (ifibss->privacy)
980 capability |= WLAN_CAPABILITY_PRIVACY;
981
982 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
983 ifibss->bssid, ifibss->ssid,
984 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
985 WLAN_CAPABILITY_PRIVACY,
986 capability);
987
988 if (cbss) {
989 cfg80211_unlink_bss(local->hw.wiphy, cbss);
990 cfg80211_put_bss(cbss);
991 }
992 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200993
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200994 sta_info_flush(sdata->local, sdata);
995
996 /* remove beacon */
997 kfree(sdata->u.ibss.ie);
998 skb = sdata->u.ibss.presp;
999 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
Johannes Berg8fc214b2010-04-28 17:40:43 +02001000 sdata->vif.bss_conf.ibss_joined = false;
1001 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1002 BSS_CHANGED_IBSS);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001003 synchronize_rcu();
1004 kfree_skb(skb);
1005
Johannes Berg35f20c12010-06-10 10:21:30 +02001006 skb_queue_purge(&sdata->skb_queue);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001007 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001008 sdata->u.ibss.ssid_len = 0;
1009
Johannes Bergbc05d192010-07-21 10:52:40 +02001010 del_timer_sync(&sdata->u.ibss.timer);
Johannes Berg7a17a332010-07-21 11:30:27 +02001011
1012 mutex_unlock(&sdata->u.ibss.mtx);
Johannes Bergbc05d192010-07-21 10:52:40 +02001013
Johannes Berg7da7cc12010-08-05 17:02:38 +02001014 mutex_lock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001015 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001016 mutex_unlock(&local->mtx);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001017
1018 return 0;
1019}