blob: 14883966374e76347d5b84e13cc9844526fb6543 [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)
34#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
35
36#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
37
38
39static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
40 struct ieee80211_mgmt *mgmt,
41 size_t len)
42{
43 u16 auth_alg, auth_transaction, status_code;
44
Johannes Berg7a17a332010-07-21 11:30:27 +020045 lockdep_assert_held(&sdata->u.ibss.mtx);
46
Johannes Berg46900292009-02-15 12:44:28 +010047 if (len < 24 + 6)
48 return;
49
50 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
51 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
52 status_code = le16_to_cpu(mgmt->u.auth.status_code);
53
54 /*
55 * IEEE 802.11 standard does not require authentication in IBSS
56 * networks and most implementations do not seem to use it.
57 * However, try to reply to authentication attempts if someone
58 * has actually implemented this.
59 */
60 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
61 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
Johannes Bergfffd0932009-07-08 14:22:54 +020062 sdata->u.ibss.bssid, NULL, 0, 0);
Johannes Berg46900292009-02-15 12:44:28 +010063}
64
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020065static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
66 const u8 *bssid, const int beacon_int,
67 struct ieee80211_channel *chan,
Johannes Bergb59066a2009-05-12 21:18:38 +020068 const u32 basic_rates,
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020069 const u16 capability, u64 tsf)
Johannes Berg46900292009-02-15 12:44:28 +010070{
71 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
72 struct ieee80211_local *local = sdata->local;
Johannes Bergb59066a2009-05-12 21:18:38 +020073 int rates, i;
Johannes Berg46900292009-02-15 12:44:28 +010074 struct sk_buff *skb;
75 struct ieee80211_mgmt *mgmt;
76 u8 *pos;
77 struct ieee80211_supported_band *sband;
Johannes Bergf446d102009-10-28 15:12:32 +010078 struct cfg80211_bss *bss;
Johannes Berg57c4d7b2009-04-23 16:10:04 +020079 u32 bss_change;
Johannes Bergb59066a2009-05-12 21:18:38 +020080 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
Johannes Berg24487982009-04-23 18:52:52 +020081
Johannes Berg7a17a332010-07-21 11:30:27 +020082 lockdep_assert_held(&ifibss->mtx);
83
Johannes Berg24487982009-04-23 18:52:52 +020084 /* Reset own TSF to allow time synchronization work. */
85 drv_reset_tsf(local);
Johannes Berg46900292009-02-15 12:44:28 +010086
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020087 skb = ifibss->skb;
88 rcu_assign_pointer(ifibss->presp, NULL);
89 synchronize_rcu();
90 skb->data = skb->head;
91 skb->len = 0;
92 skb_reset_tail_pointer(skb);
93 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
Johannes Berg46900292009-02-15 12:44:28 +010094
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020095 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
96 sta_info_flush(sdata->local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010097
Johannes Berg49b5c7f2010-04-29 21:34:01 +020098 /* if merging, indicate to driver that we leave the old IBSS */
99 if (sdata->vif.bss_conf.ibss_joined) {
100 sdata->vif.bss_conf.ibss_joined = false;
101 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
102 }
103
Johannes Berg46900292009-02-15 12:44:28 +0100104 memcpy(ifibss->bssid, bssid, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100105
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200106 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
Johannes Berg46900292009-02-15 12:44:28 +0100107
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200108 local->oper_channel = chan;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200109 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200110 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200111
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200112 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg46900292009-02-15 12:44:28 +0100113
Johannes Bergb59066a2009-05-12 21:18:38 +0200114 /* build supported rates array */
115 pos = supp_rates;
116 for (i = 0; i < sband->n_bitrates; i++) {
117 int rate = sband->bitrates[i].bitrate;
118 u8 basic = 0;
119 if (basic_rates & BIT(i))
120 basic = 0x80;
121 *pos++ = basic | (u8) (rate / 5);
122 }
123
Johannes Berg46900292009-02-15 12:44:28 +0100124 /* Build IBSS probe response */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200125 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
Johannes Berg46900292009-02-15 12:44:28 +0100126 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
127 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
128 IEEE80211_STYPE_PROBE_RESP);
129 memset(mgmt->da, 0xff, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100130 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100131 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200132 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530133 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100134 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
135
136 pos = skb_put(skb, 2 + ifibss->ssid_len);
137 *pos++ = WLAN_EID_SSID;
138 *pos++ = ifibss->ssid_len;
139 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
140
Johannes Bergb59066a2009-05-12 21:18:38 +0200141 rates = sband->n_bitrates;
Johannes Berg46900292009-02-15 12:44:28 +0100142 if (rates > 8)
143 rates = 8;
144 pos = skb_put(skb, 2 + rates);
145 *pos++ = WLAN_EID_SUPP_RATES;
146 *pos++ = rates;
147 memcpy(pos, supp_rates, rates);
148
149 if (sband->band == IEEE80211_BAND_2GHZ) {
150 pos = skb_put(skb, 2 + 1);
151 *pos++ = WLAN_EID_DS_PARAMS;
152 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200153 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100154 }
155
156 pos = skb_put(skb, 2 + 2);
157 *pos++ = WLAN_EID_IBSS_PARAMS;
158 *pos++ = 2;
159 /* FIX: set ATIM window based on scan results */
160 *pos++ = 0;
161 *pos++ = 0;
162
Johannes Bergb59066a2009-05-12 21:18:38 +0200163 if (sband->n_bitrates > 8) {
164 rates = sband->n_bitrates - 8;
Johannes Berg46900292009-02-15 12:44:28 +0100165 pos = skb_put(skb, 2 + rates);
166 *pos++ = WLAN_EID_EXT_SUPP_RATES;
167 *pos++ = rates;
168 memcpy(pos, &supp_rates[8], rates);
169 }
170
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200171 if (ifibss->ie_len)
172 memcpy(skb_put(skb, ifibss->ie_len),
173 ifibss->ie, ifibss->ie_len);
174
Bruno Randolf9eba6122010-10-04 11:17:30 +0900175 if (local->hw.queues >= 4) {
176 pos = skb_put(skb, 9);
177 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
178 *pos++ = 7; /* len */
179 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
180 *pos++ = 0x50;
181 *pos++ = 0xf2;
182 *pos++ = 2; /* WME */
183 *pos++ = 0; /* WME info */
184 *pos++ = 1; /* WME ver */
185 *pos++ = 0; /* U-APSD no in use */
186 }
187
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200188 rcu_assign_pointer(ifibss->presp, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100189
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200190 sdata->vif.bss_conf.beacon_int = beacon_int;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300191 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200192 bss_change = BSS_CHANGED_BEACON_INT;
193 bss_change |= ieee80211_reset_erp_info(sdata);
194 bss_change |= BSS_CHANGED_BSSID;
195 bss_change |= BSS_CHANGED_BEACON;
196 bss_change |= BSS_CHANGED_BEACON_ENABLED;
Teemu Paasikivi392cfdb2010-06-14 12:55:32 +0300197 bss_change |= BSS_CHANGED_BASIC_RATES;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200198 bss_change |= BSS_CHANGED_IBSS;
199 sdata->vif.bss_conf.ibss_joined = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200200 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100201
Johannes Bergb59066a2009-05-12 21:18:38 +0200202 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
Johannes Berg46900292009-02-15 12:44:28 +0100203
Johannes Berg46900292009-02-15 12:44:28 +0100204 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200205 mod_timer(&ifibss->timer,
206 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100207
Johannes Bergf446d102009-10-28 15:12:32 +0100208 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
209 mgmt, skb->len, 0, GFP_KERNEL);
210 cfg80211_put_bss(bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200211 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100212}
213
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200214static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
215 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100216{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100217 struct cfg80211_bss *cbss =
218 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200219 struct ieee80211_supported_band *sband;
220 u32 basic_rates;
221 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100222 u16 beacon_int = cbss->beacon_interval;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200223
Johannes Berg7a17a332010-07-21 11:30:27 +0200224 lockdep_assert_held(&sdata->u.ibss.mtx);
225
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200226 if (beacon_int < 10)
227 beacon_int = 10;
228
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100229 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Johannes Bergb59066a2009-05-12 21:18:38 +0200230
231 basic_rates = 0;
232
233 for (i = 0; i < bss->supp_rates_len; i++) {
234 int rate = (bss->supp_rates[i] & 0x7f) * 5;
235 bool is_basic = !!(bss->supp_rates[i] & 0x80);
236
237 for (j = 0; j < sband->n_bitrates; j++) {
238 if (sband->bitrates[j].bitrate == rate) {
239 if (is_basic)
240 basic_rates |= BIT(j);
241 break;
242 }
243 }
244 }
245
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100246 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200247 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100248 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200249 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100250 cbss->capability,
251 cbss->tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100252}
253
254static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
255 struct ieee80211_mgmt *mgmt,
256 size_t len,
257 struct ieee80211_rx_status *rx_status,
258 struct ieee802_11_elems *elems,
259 bool beacon)
260{
261 struct ieee80211_local *local = sdata->local;
262 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100263 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100264 struct ieee80211_bss *bss;
265 struct sta_info *sta;
266 struct ieee80211_channel *channel;
267 u64 beacon_timestamp, rx_timestamp;
268 u32 supp_rates = 0;
269 enum ieee80211_band band = rx_status->band;
270
271 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900272 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
273 band);
Johannes Berg46900292009-02-15 12:44:28 +0100274 else
275 freq = rx_status->freq;
276
277 channel = ieee80211_get_channel(local->hw.wiphy, freq);
278
279 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
280 return;
281
Bruno Randolf9eba6122010-10-04 11:17:30 +0900282 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg46900292009-02-15 12:44:28 +0100283 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
Johannes Berg46900292009-02-15 12:44:28 +0100284
285 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100286 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100287
Bruno Randolf9eba6122010-10-04 11:17:30 +0900288 if (elems->supp_rates) {
289 supp_rates = ieee80211_sta_get_rates(local, elems,
290 band);
291 if (sta) {
292 u32 prev_rates;
Johannes Berg46900292009-02-15 12:44:28 +0100293
Bruno Randolf9eba6122010-10-04 11:17:30 +0900294 prev_rates = sta->sta.supp_rates[band];
295 /* make sure mandatory rates are always added */
296 sta->sta.supp_rates[band] = supp_rates |
297 ieee80211_mandatory_rates(local, band);
298
299 if (sta->sta.supp_rates[band] != prev_rates) {
Johannes Berg46900292009-02-15 12:44:28 +0100300#ifdef CONFIG_MAC80211_IBSS_DEBUG
Bruno Randolf9eba6122010-10-04 11:17:30 +0900301 printk(KERN_DEBUG
302 "%s: updated supp_rates set "
303 "for %pM based on beacon"
304 "/probe_resp (0x%x -> 0x%x)\n",
305 sdata->name, sta->sta.addr,
306 prev_rates,
307 sta->sta.supp_rates[band]);
Johannes Berg46900292009-02-15 12:44:28 +0100308#endif
Bruno Randolf9eba6122010-10-04 11:17:30 +0900309 rate_control_rate_init(sta);
310 }
311 } else
312 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
313 mgmt->sa, supp_rates,
314 GFP_ATOMIC);
Johannes Berg34e89502010-02-03 13:59:58 +0100315 }
Bruno Randolf9eba6122010-10-04 11:17:30 +0900316
317 if (sta && elems->wmm_info)
318 set_sta_flags(sta, WLAN_STA_WME);
319
320 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100321 }
322
323 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
324 channel, beacon);
325 if (!bss)
326 return;
327
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100328 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
329
Johannes Berg46900292009-02-15 12:44:28 +0100330 /* was just updated in ieee80211_bss_info_update */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100331 beacon_timestamp = cbss->tsf;
Johannes Berg46900292009-02-15 12:44:28 +0100332
333 /* check if we need to merge IBSS */
334
Johannes Berg46900292009-02-15 12:44:28 +0100335 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100336 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100337 goto put_bss;
338
339 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100340 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100341 goto put_bss;
342
343 /* different channel */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100344 if (cbss->channel != local->oper_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100345 goto put_bss;
346
347 /* different SSID */
348 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
349 memcmp(elems->ssid, sdata->u.ibss.ssid,
350 sdata->u.ibss.ssid_len))
351 goto put_bss;
352
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100353 /* same BSSID */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100354 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100355 goto put_bss;
356
Johannes Berg6ebacbb2011-02-23 15:06:08 +0100357 if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
Johannes Berg46900292009-02-15 12:44:28 +0100358 /*
359 * For correct IBSS merging we need mactime; since mactime is
360 * defined as the time the first data symbol of the frame hits
361 * the PHY, and the timestamp of the beacon is defined as "the
362 * time that the data symbol containing the first bit of the
363 * timestamp is transmitted to the PHY plus the transmitting
364 * STA's delays through its local PHY from the MAC-PHY
365 * interface to its interface with the WM" (802.11 11.1.2)
366 * - equals the time this bit arrives at the receiver - we have
367 * to take into account the offset between the two.
368 *
369 * E.g. at 1 MBit that means mactime is 192 usec earlier
370 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
371 */
372 int rate;
373
374 if (rx_status->flag & RX_FLAG_HT)
375 rate = 65; /* TODO: HT rates */
376 else
377 rate = local->hw.wiphy->bands[band]->
378 bitrates[rx_status->rate_idx].bitrate;
379
380 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
Johannes Berg24487982009-04-23 18:52:52 +0200381 } else {
382 /*
383 * second best option: get current TSF
384 * (will return -1 if not supported)
385 */
386 rx_timestamp = drv_get_tsf(local);
387 }
Johannes Berg46900292009-02-15 12:44:28 +0100388
389#ifdef CONFIG_MAC80211_IBSS_DEBUG
390 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
391 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
392 mgmt->sa, mgmt->bssid,
393 (unsigned long long)rx_timestamp,
394 (unsigned long long)beacon_timestamp,
395 (unsigned long long)(rx_timestamp - beacon_timestamp),
396 jiffies);
397#endif
398
399 if (beacon_timestamp > rx_timestamp) {
400#ifdef CONFIG_MAC80211_IBSS_DEBUG
401 printk(KERN_DEBUG "%s: beacon TSF higher than "
402 "local TSF - IBSS merge with BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100403 sdata->name, mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100404#endif
405 ieee80211_sta_join_ibss(sdata, bss);
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900406 supp_rates = ieee80211_sta_get_rates(local, elems, band);
Johannes Berg34e89502010-02-03 13:59:58 +0100407 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
408 supp_rates, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100409 }
410
411 put_bss:
412 ieee80211_rx_bss_put(local, bss);
413}
414
415/*
416 * Add a new IBSS station, will also be called by the RX code when,
417 * in IBSS mode, receiving a frame from a yet-unknown station, hence
418 * must be callable in atomic context.
419 */
420struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Johannes Berg34e89502010-02-03 13:59:58 +0100421 u8 *bssid,u8 *addr, u32 supp_rates,
422 gfp_t gfp)
Johannes Berg46900292009-02-15 12:44:28 +0100423{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100424 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100425 struct ieee80211_local *local = sdata->local;
426 struct sta_info *sta;
427 int band = local->hw.conf.channel->band;
428
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200429 /*
430 * XXX: Consider removing the least recently used entry and
431 * allow new one to be added.
432 */
Johannes Berg46900292009-02-15 12:44:28 +0100433 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200434 if (net_ratelimit())
435 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100436 sdata->name, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100437 return NULL;
438 }
439
Felix Fietkau2e10d332009-12-20 19:07:09 +0100440 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
441 return NULL;
442
Johannes Berg46900292009-02-15 12:44:28 +0100443 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
444 return NULL;
445
446#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700447 wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
448 addr, sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100449#endif
450
Johannes Berg34e89502010-02-03 13:59:58 +0100451 sta = sta_info_alloc(sdata, addr, gfp);
Johannes Berg46900292009-02-15 12:44:28 +0100452 if (!sta)
453 return NULL;
454
Rajkumar Manoharanc8716d92010-10-23 10:59:57 +0530455 sta->last_rx = jiffies;
Johannes Berg46900292009-02-15 12:44:28 +0100456 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
457
458 /* make sure mandatory rates are always added */
459 sta->sta.supp_rates[band] = supp_rates |
460 ieee80211_mandatory_rates(local, band);
461
462 rate_control_rate_init(sta);
463
Johannes Berg34e89502010-02-03 13:59:58 +0100464 /* If it fails, maybe we raced another insertion? */
Johannes Berg46900292009-02-15 12:44:28 +0100465 if (sta_info_insert(sta))
Johannes Berg34e89502010-02-03 13:59:58 +0100466 return sta_info_get(sdata, addr);
Johannes Berg46900292009-02-15 12:44:28 +0100467 return sta;
468}
469
470static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
471{
472 struct ieee80211_local *local = sdata->local;
473 int active = 0;
474 struct sta_info *sta;
475
Johannes Berg7a17a332010-07-21 11:30:27 +0200476 lockdep_assert_held(&sdata->u.ibss.mtx);
477
Johannes Berg46900292009-02-15 12:44:28 +0100478 rcu_read_lock();
479
480 list_for_each_entry_rcu(sta, &local->sta_list, list) {
481 if (sta->sdata == sdata &&
482 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
483 jiffies)) {
484 active++;
485 break;
486 }
487 }
488
489 rcu_read_unlock();
490
491 return active;
492}
493
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100494/*
495 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
496 */
Johannes Berg46900292009-02-15 12:44:28 +0100497
498static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
499{
500 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
501
Johannes Berg7a17a332010-07-21 11:30:27 +0200502 lockdep_assert_held(&ifibss->mtx);
503
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200504 mod_timer(&ifibss->timer,
505 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100506
507 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200508
Sujith450aae32009-11-02 12:33:23 +0530509 if (time_before(jiffies, ifibss->last_scan_completed +
510 IEEE80211_IBSS_MERGE_INTERVAL))
511 return;
512
Johannes Berg46900292009-02-15 12:44:28 +0100513 if (ieee80211_sta_active_ibss(sdata))
514 return;
515
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200516 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100517 return;
518
519 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Johannes Berg47846c92009-11-25 17:46:19 +0100520 "IBSS networks with same SSID (merge)\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100521
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200522 ieee80211_request_internal_scan(sdata,
523 ifibss->ssid, ifibss->ssid_len,
524 ifibss->fixed_channel ? ifibss->channel : NULL);
Johannes Berg46900292009-02-15 12:44:28 +0100525}
526
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200527static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100528{
529 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
530 struct ieee80211_local *local = sdata->local;
531 struct ieee80211_supported_band *sband;
Johannes Berg46900292009-02-15 12:44:28 +0100532 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100533 u16 capability;
534 int i;
535
Johannes Berg7a17a332010-07-21 11:30:27 +0200536 lockdep_assert_held(&ifibss->mtx);
537
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200538 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100539 memcpy(bssid, ifibss->bssid, ETH_ALEN);
540 } else {
541 /* Generate random, not broadcast, locally administered BSSID. Mix in
542 * own MAC address to make sure that devices that do not have proper
543 * random number generator get different BSSID. */
544 get_random_bytes(bssid, ETH_ALEN);
545 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100546 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100547 bssid[0] &= ~0x01;
548 bssid[0] |= 0x02;
549 }
550
551 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100552 sdata->name, bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100553
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200554 sband = local->hw.wiphy->bands[ifibss->channel->band];
Johannes Berg46900292009-02-15 12:44:28 +0100555
Johannes Berg46900292009-02-15 12:44:28 +0100556 capability = WLAN_CAPABILITY_IBSS;
557
Johannes Bergfffd0932009-07-08 14:22:54 +0200558 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100559 capability |= WLAN_CAPABILITY_PRIVACY;
560 else
561 sdata->drop_unencrypted = 0;
562
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200563 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300564 ifibss->channel, ifibss->basic_rates,
Johannes Bergb59066a2009-05-12 21:18:38 +0200565 capability, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100566}
567
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100568/*
569 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
570 */
571
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200572static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100573{
574 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
575 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100576 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200577 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100578 const u8 *bssid = NULL;
579 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200580 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100581
Johannes Berg7a17a332010-07-21 11:30:27 +0200582 lockdep_assert_held(&ifibss->mtx);
583
Johannes Berg46900292009-02-15 12:44:28 +0100584 active_ibss = ieee80211_sta_active_ibss(sdata);
585#ifdef CONFIG_MAC80211_IBSS_DEBUG
586 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100587 sdata->name, active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100588#endif /* CONFIG_MAC80211_IBSS_DEBUG */
589
590 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200591 return;
Johannes Berg46900292009-02-15 12:44:28 +0100592
Johannes Berge0d61882009-05-12 20:47:32 +0200593 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200594 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200595 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200596 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100597 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200598 if (ifibss->fixed_channel)
599 chan = ifibss->channel;
600 if (!is_zero_ether_addr(ifibss->bssid))
601 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100602 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
603 ifibss->ssid, ifibss->ssid_len,
604 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
605 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100606
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100607 if (cbss) {
608 struct ieee80211_bss *bss;
609
610 bss = (void *)cbss->priv;
Johannes Berg46900292009-02-15 12:44:28 +0100611#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg46900292009-02-15 12:44:28 +0100612 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100613 "%pM\n", cbss->bssid, ifibss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100614#endif /* CONFIG_MAC80211_IBSS_DEBUG */
615
Johannes Berg46900292009-02-15 12:44:28 +0100616 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
617 " based on configured SSID\n",
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100618 sdata->name, cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100619
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200620 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100621 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200622 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700623 }
Johannes Berg46900292009-02-15 12:44:28 +0100624
625#ifdef CONFIG_MAC80211_IBSS_DEBUG
626 printk(KERN_DEBUG " did not try to join ibss\n");
627#endif /* CONFIG_MAC80211_IBSS_DEBUG */
628
629 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100630 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100631 IEEE80211_SCAN_INTERVAL)) {
632 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Johannes Berg47846c92009-11-25 17:46:19 +0100633 "join\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100634
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200635 ieee80211_request_internal_scan(sdata,
636 ifibss->ssid, ifibss->ssid_len,
637 ifibss->fixed_channel ? ifibss->channel : NULL);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100638 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100639 int interval = IEEE80211_SCAN_INTERVAL;
640
641 if (time_after(jiffies, ifibss->ibss_join_req +
642 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200643 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
644 ieee80211_sta_create_ibss(sdata);
645 return;
646 }
Johannes Berg46900292009-02-15 12:44:28 +0100647 printk(KERN_DEBUG "%s: IBSS not allowed on"
Johannes Berg47846c92009-11-25 17:46:19 +0100648 " %d MHz\n", sdata->name,
Johannes Berg46900292009-02-15 12:44:28 +0100649 local->hw.conf.channel->center_freq);
650
651 /* No IBSS found - decrease scan interval and continue
652 * scanning. */
653 interval = IEEE80211_SCAN_INTERVAL_SLOW;
654 }
655
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200656 mod_timer(&ifibss->timer,
657 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100658 }
Johannes Berg46900292009-02-15 12:44:28 +0100659}
660
661static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Bergc269a202011-02-14 12:20:22 +0100662 struct sk_buff *req)
Johannes Berg46900292009-02-15 12:44:28 +0100663{
Johannes Bergc269a202011-02-14 12:20:22 +0100664 struct ieee80211_mgmt *mgmt = (void *)req->data;
Johannes Berg46900292009-02-15 12:44:28 +0100665 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
666 struct ieee80211_local *local = sdata->local;
Johannes Bergc269a202011-02-14 12:20:22 +0100667 int tx_last_beacon, len = req->len;
Johannes Berg46900292009-02-15 12:44:28 +0100668 struct sk_buff *skb;
669 struct ieee80211_mgmt *resp;
670 u8 *pos, *end;
671
Johannes Berg7a17a332010-07-21 11:30:27 +0200672 lockdep_assert_held(&ifibss->mtx);
673
Johannes Berg46900292009-02-15 12:44:28 +0100674 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200675 len < 24 + 2 || !ifibss->presp)
Johannes Berg46900292009-02-15 12:44:28 +0100676 return;
677
Johannes Berg24487982009-04-23 18:52:52 +0200678 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100679
680#ifdef CONFIG_MAC80211_IBSS_DEBUG
681 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
682 " (tx_last_beacon=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100683 sdata->name, mgmt->sa, mgmt->da,
Johannes Berg46900292009-02-15 12:44:28 +0100684 mgmt->bssid, tx_last_beacon);
685#endif /* CONFIG_MAC80211_IBSS_DEBUG */
686
Felix Fietkau1ed76482011-03-24 19:46:18 +0100687 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
Johannes Berg46900292009-02-15 12:44:28 +0100688 return;
689
690 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
691 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
692 return;
693
694 end = ((u8 *) mgmt) + len;
695 pos = mgmt->u.probe_req.variable;
696 if (pos[0] != WLAN_EID_SSID ||
697 pos + 2 + pos[1] > end) {
698#ifdef CONFIG_MAC80211_IBSS_DEBUG
699 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
700 "from %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100701 sdata->name, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100702#endif
703 return;
704 }
705 if (pos[1] != 0 &&
706 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100707 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100708 /* Ignore ProbeReq for foreign SSID */
709 return;
710 }
711
712 /* Reply with ProbeResp */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200713 skb = skb_copy(ifibss->presp, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100714 if (!skb)
715 return;
716
717 resp = (struct ieee80211_mgmt *) skb->data;
718 memcpy(resp->da, mgmt->sa, ETH_ALEN);
719#ifdef CONFIG_MAC80211_IBSS_DEBUG
720 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100721 sdata->name, resp->da);
Johannes Berg46900292009-02-15 12:44:28 +0100722#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Berg62ae67b2009-11-18 18:42:05 +0100723 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
724 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100725}
726
727static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
728 struct ieee80211_mgmt *mgmt,
729 size_t len,
730 struct ieee80211_rx_status *rx_status)
731{
732 size_t baselen;
733 struct ieee802_11_elems elems;
734
Johannes Berg47846c92009-11-25 17:46:19 +0100735 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Johannes Berg46900292009-02-15 12:44:28 +0100736 return; /* ignore ProbeResp to foreign address */
737
738 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
739 if (baselen > len)
740 return;
741
742 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
743 &elems);
744
745 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
746}
747
748static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
749 struct ieee80211_mgmt *mgmt,
750 size_t len,
751 struct ieee80211_rx_status *rx_status)
752{
753 size_t baselen;
754 struct ieee802_11_elems elems;
755
756 /* Process beacon from the current BSS */
757 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
758 if (baselen > len)
759 return;
760
761 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
762
763 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
764}
765
Johannes Berg1fa57d02010-06-10 10:21:32 +0200766void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
767 struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +0100768{
769 struct ieee80211_rx_status *rx_status;
770 struct ieee80211_mgmt *mgmt;
771 u16 fc;
772
Johannes Bergf1d58c22009-06-17 13:13:00 +0200773 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +0100774 mgmt = (struct ieee80211_mgmt *) skb->data;
775 fc = le16_to_cpu(mgmt->frame_control);
776
Johannes Berg7a17a332010-07-21 11:30:27 +0200777 mutex_lock(&sdata->u.ibss.mtx);
778
Tim Harveyc926d002010-12-09 10:43:13 -0800779 if (!sdata->u.ibss.ssid_len)
780 goto mgmt_out; /* not ready to merge yet */
781
Johannes Berg46900292009-02-15 12:44:28 +0100782 switch (fc & IEEE80211_FCTL_STYPE) {
783 case IEEE80211_STYPE_PROBE_REQ:
Johannes Bergc269a202011-02-14 12:20:22 +0100784 ieee80211_rx_mgmt_probe_req(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100785 break;
786 case IEEE80211_STYPE_PROBE_RESP:
787 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
788 rx_status);
789 break;
790 case IEEE80211_STYPE_BEACON:
791 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
792 rx_status);
793 break;
794 case IEEE80211_STYPE_AUTH:
795 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
796 break;
797 }
Johannes Berg7a17a332010-07-21 11:30:27 +0200798
Tim Harveyc926d002010-12-09 10:43:13 -0800799 mgmt_out:
Johannes Berg7a17a332010-07-21 11:30:27 +0200800 mutex_unlock(&sdata->u.ibss.mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100801}
802
Johannes Berg1fa57d02010-06-10 10:21:32 +0200803void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100804{
Johannes Berg1fa57d02010-06-10 10:21:32 +0200805 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100806
Johannes Berg7a17a332010-07-21 11:30:27 +0200807 mutex_lock(&ifibss->mtx);
808
809 /*
810 * Work could be scheduled after scan or similar
811 * when we aren't even joined (or trying) with a
812 * network.
813 */
814 if (!ifibss->ssid_len)
815 goto out;
Johannes Berg46900292009-02-15 12:44:28 +0100816
817 switch (ifibss->state) {
818 case IEEE80211_IBSS_MLME_SEARCH:
819 ieee80211_sta_find_ibss(sdata);
820 break;
821 case IEEE80211_IBSS_MLME_JOINED:
822 ieee80211_sta_merge_ibss(sdata);
823 break;
824 default:
825 WARN_ON(1);
826 break;
827 }
Johannes Berg46900292009-02-15 12:44:28 +0100828
Johannes Berg7a17a332010-07-21 11:30:27 +0200829 out:
830 mutex_unlock(&ifibss->mtx);
Johannes Berg3a4d4aa2010-05-26 16:41:40 +0200831}
832
Johannes Berg46900292009-02-15 12:44:28 +0100833static void ieee80211_ibss_timer(unsigned long data)
834{
835 struct ieee80211_sub_if_data *sdata =
836 (struct ieee80211_sub_if_data *) data;
837 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
838 struct ieee80211_local *local = sdata->local;
839
Johannes Berg5bb644a2009-05-17 11:40:42 +0200840 if (local->quiescing) {
841 ifibss->timer_running = true;
842 return;
843 }
844
Johannes Berg7a17a332010-07-21 11:30:27 +0200845 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100846}
847
Johannes Berg5bb644a2009-05-17 11:40:42 +0200848#ifdef CONFIG_PM
849void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
850{
851 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
852
Johannes Berg5bb644a2009-05-17 11:40:42 +0200853 if (del_timer_sync(&ifibss->timer))
854 ifibss->timer_running = true;
855}
856
857void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
858{
859 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
860
861 if (ifibss->timer_running) {
862 add_timer(&ifibss->timer);
863 ifibss->timer_running = false;
864 }
865}
866#endif
867
Johannes Berg46900292009-02-15 12:44:28 +0100868void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
869{
870 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
871
Johannes Berg46900292009-02-15 12:44:28 +0100872 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
873 (unsigned long) sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +0200874 mutex_init(&ifibss->mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100875}
876
877/* scan finished notification */
878void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
879{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200880 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +0100881
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200882 mutex_lock(&local->iflist_mtx);
883 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100884 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +0200885 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200886 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
887 continue;
888 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg7a17a332010-07-21 11:30:27 +0200889 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100890 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +0200891 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100892}
893
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200894int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
895 struct cfg80211_ibss_params *params)
896{
897 struct sk_buff *skb;
898
Johannes Berg7a17a332010-07-21 11:30:27 +0200899 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
900 36 /* bitrates */ +
901 34 /* SSID */ +
902 3 /* DS params */ +
903 4 /* IBSS params */ +
904 params->ie_len);
905 if (!skb)
906 return -ENOMEM;
907
908 mutex_lock(&sdata->u.ibss.mtx);
909
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200910 if (params->bssid) {
911 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
912 sdata->u.ibss.fixed_bssid = true;
913 } else
914 sdata->u.ibss.fixed_bssid = false;
915
Johannes Bergfffd0932009-07-08 14:22:54 +0200916 sdata->u.ibss.privacy = params->privacy;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300917 sdata->u.ibss.basic_rates = params->basic_rates;
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100918 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
919 sizeof(params->mcast_rate));
Johannes Bergfffd0932009-07-08 14:22:54 +0200920
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200921 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
922
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200923 sdata->u.ibss.channel = params->channel;
924 sdata->u.ibss.fixed_channel = params->channel_fixed;
925
Johannes Bergadfba3c2010-05-05 15:33:55 +0200926 /* fix ourselves to that channel now already */
927 if (params->channel_fixed) {
928 sdata->local->oper_channel = params->channel;
Johannes Berg0aaffa92010-05-05 15:28:27 +0200929 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
930 NL80211_CHAN_NO_HT));
Johannes Bergadfba3c2010-05-05 15:33:55 +0200931 }
932
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200933 if (params->ie) {
934 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
935 GFP_KERNEL);
936 if (sdata->u.ibss.ie)
937 sdata->u.ibss.ie_len = params->ie_len;
938 }
939
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200940 sdata->u.ibss.skb = skb;
941 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
942 sdata->u.ibss.ibss_join_req = jiffies;
943
Johannes Berg0e41f712009-04-23 11:48:56 +0200944 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
Johannes Berg0e41f712009-04-23 11:48:56 +0200945 sdata->u.ibss.ssid_len = params->ssid_len;
946
Johannes Berg7da7cc12010-08-05 17:02:38 +0200947 mutex_unlock(&sdata->u.ibss.mtx);
948
949 mutex_lock(&sdata->local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200950 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +0200951 mutex_unlock(&sdata->local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200952
Johannes Berg64592c82010-06-10 10:21:31 +0200953 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200954
955 return 0;
956}
957
958int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
959{
960 struct sk_buff *skb;
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +0300961 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
962 struct ieee80211_local *local = sdata->local;
963 struct cfg80211_bss *cbss;
964 u16 capability;
Johannes Berg7a17a332010-07-21 11:30:27 +0200965 int active_ibss;
966
967 mutex_lock(&sdata->u.ibss.mtx);
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +0300968
969 active_ibss = ieee80211_sta_active_ibss(sdata);
970
971 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
972 capability = WLAN_CAPABILITY_IBSS;
973
974 if (ifibss->privacy)
975 capability |= WLAN_CAPABILITY_PRIVACY;
976
977 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
978 ifibss->bssid, ifibss->ssid,
979 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
980 WLAN_CAPABILITY_PRIVACY,
981 capability);
982
983 if (cbss) {
984 cfg80211_unlink_bss(local->hw.wiphy, cbss);
985 cfg80211_put_bss(cbss);
986 }
987 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200988
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200989 sta_info_flush(sdata->local, sdata);
990
991 /* remove beacon */
992 kfree(sdata->u.ibss.ie);
993 skb = sdata->u.ibss.presp;
994 rcu_assign_pointer(sdata->u.ibss.presp, NULL);
Johannes Berg8fc214b2010-04-28 17:40:43 +0200995 sdata->vif.bss_conf.ibss_joined = false;
996 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
997 BSS_CHANGED_IBSS);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200998 synchronize_rcu();
999 kfree_skb(skb);
1000
Johannes Berg35f20c12010-06-10 10:21:30 +02001001 skb_queue_purge(&sdata->skb_queue);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001002 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001003 sdata->u.ibss.ssid_len = 0;
1004
Johannes Bergbc05d192010-07-21 10:52:40 +02001005 del_timer_sync(&sdata->u.ibss.timer);
Johannes Berg7a17a332010-07-21 11:30:27 +02001006
1007 mutex_unlock(&sdata->u.ibss.mtx);
Johannes Bergbc05d192010-07-21 10:52:40 +02001008
Johannes Berg7da7cc12010-08-05 17:02:38 +02001009 mutex_lock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001010 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001011 mutex_unlock(&local->mtx);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001012
1013 return 0;
1014}