blob: a4643969a13b22524a7f90f0304c18d917f025be [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{
Rajkumar Manoharan0915cba2011-04-25 15:56:17 +053043 u16 auth_alg, auth_transaction;
Johannes Berg46900292009-02-15 12:44:28 +010044
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);
Johannes Berg46900292009-02-15 12:44:28 +010052
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];
Alexander Simon13c40c52011-11-30 16:56:34 +010080 enum nl80211_channel_type channel_type;
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. */
Eliad Peller37a41b42011-09-21 14:06:11 +030085 drv_reset_tsf(local, sdata);
Johannes Berg46900292009-02-15 12:44:28 +010086
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020087 skb = ifibss->skb;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +000088 RCU_INIT_POINTER(ifibss->presp, NULL);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +020089 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;
Eliad Peller86a2ea42011-11-08 15:36:59 +0200101 netif_carrier_off(sdata->dev);
Johannes Berg49b5c7f2010-04-29 21:34:01 +0200102 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
Eliad Pellerba196022012-01-10 15:19:54 +0200109 local->oper_channel = chan;
Alexander Simon13c40c52011-11-30 16:56:34 +0100110 channel_type = ifibss->channel_type;
111 if (channel_type > NL80211_CHAN_HT20 &&
112 !cfg80211_can_beacon_sec_chan(local->hw.wiphy, chan, channel_type))
113 channel_type = NL80211_CHAN_HT20;
114 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
115 /* can only fail due to HT40+/- mismatch */
116 channel_type = NL80211_CHAN_HT20;
117 WARN_ON(!ieee80211_set_channel_type(local, sdata,
118 NL80211_CHAN_HT20));
119 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200120 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200121
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200122 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg46900292009-02-15 12:44:28 +0100123
Johannes Bergb59066a2009-05-12 21:18:38 +0200124 /* build supported rates array */
125 pos = supp_rates;
126 for (i = 0; i < sband->n_bitrates; i++) {
127 int rate = sband->bitrates[i].bitrate;
128 u8 basic = 0;
129 if (basic_rates & BIT(i))
130 basic = 0x80;
131 *pos++ = basic | (u8) (rate / 5);
132 }
133
Johannes Berg46900292009-02-15 12:44:28 +0100134 /* Build IBSS probe response */
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200135 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
Johannes Berg46900292009-02-15 12:44:28 +0100136 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
137 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
138 IEEE80211_STYPE_PROBE_RESP);
139 memset(mgmt->da, 0xff, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100140 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100141 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200142 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
Sujith707c1b42009-03-03 10:15:10 +0530143 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100144 mgmt->u.beacon.capab_info = cpu_to_le16(capability);
145
146 pos = skb_put(skb, 2 + ifibss->ssid_len);
147 *pos++ = WLAN_EID_SSID;
148 *pos++ = ifibss->ssid_len;
149 memcpy(pos, ifibss->ssid, ifibss->ssid_len);
150
Johannes Bergb59066a2009-05-12 21:18:38 +0200151 rates = sband->n_bitrates;
Johannes Berg46900292009-02-15 12:44:28 +0100152 if (rates > 8)
153 rates = 8;
154 pos = skb_put(skb, 2 + rates);
155 *pos++ = WLAN_EID_SUPP_RATES;
156 *pos++ = rates;
157 memcpy(pos, supp_rates, rates);
158
159 if (sband->band == IEEE80211_BAND_2GHZ) {
160 pos = skb_put(skb, 2 + 1);
161 *pos++ = WLAN_EID_DS_PARAMS;
162 *pos++ = 1;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200163 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
Johannes Berg46900292009-02-15 12:44:28 +0100164 }
165
166 pos = skb_put(skb, 2 + 2);
167 *pos++ = WLAN_EID_IBSS_PARAMS;
168 *pos++ = 2;
169 /* FIX: set ATIM window based on scan results */
170 *pos++ = 0;
171 *pos++ = 0;
172
Johannes Bergb59066a2009-05-12 21:18:38 +0200173 if (sband->n_bitrates > 8) {
174 rates = sband->n_bitrates - 8;
Johannes Berg46900292009-02-15 12:44:28 +0100175 pos = skb_put(skb, 2 + rates);
176 *pos++ = WLAN_EID_EXT_SUPP_RATES;
177 *pos++ = rates;
178 memcpy(pos, &supp_rates[8], rates);
179 }
180
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200181 if (ifibss->ie_len)
182 memcpy(skb_put(skb, ifibss->ie_len),
183 ifibss->ie, ifibss->ie_len);
184
Alexander Simon13c40c52011-11-30 16:56:34 +0100185 /* add HT capability and information IEs */
186 if (channel_type && sband->ht_cap.ht_supported) {
187 pos = skb_put(skb, 4 +
188 sizeof(struct ieee80211_ht_cap) +
189 sizeof(struct ieee80211_ht_info));
190 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
191 sband->ht_cap.cap);
192 pos = ieee80211_ie_build_ht_info(pos,
193 &sband->ht_cap,
194 chan,
195 channel_type);
196 }
197
Bruno Randolf9eba6122010-10-04 11:17:30 +0900198 if (local->hw.queues >= 4) {
199 pos = skb_put(skb, 9);
200 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
201 *pos++ = 7; /* len */
202 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
203 *pos++ = 0x50;
204 *pos++ = 0xf2;
205 *pos++ = 2; /* WME */
206 *pos++ = 0; /* WME info */
207 *pos++ = 1; /* WME ver */
208 *pos++ = 0; /* U-APSD no in use */
209 }
210
Eric Dumazetcf778b02012-01-12 04:41:32 +0000211 rcu_assign_pointer(ifibss->presp, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100212
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200213 sdata->vif.bss_conf.beacon_int = beacon_int;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300214 sdata->vif.bss_conf.basic_rates = basic_rates;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200215 bss_change = BSS_CHANGED_BEACON_INT;
216 bss_change |= ieee80211_reset_erp_info(sdata);
217 bss_change |= BSS_CHANGED_BSSID;
218 bss_change |= BSS_CHANGED_BEACON;
219 bss_change |= BSS_CHANGED_BEACON_ENABLED;
Teemu Paasikivi392cfdb2010-06-14 12:55:32 +0300220 bss_change |= BSS_CHANGED_BASIC_RATES;
Alexander Simon13c40c52011-11-30 16:56:34 +0100221 bss_change |= BSS_CHANGED_HT;
Johannes Berg8fc214b2010-04-28 17:40:43 +0200222 bss_change |= BSS_CHANGED_IBSS;
223 sdata->vif.bss_conf.ibss_joined = true;
Johannes Berg2d0ddec2009-04-23 16:13:26 +0200224 ieee80211_bss_info_change_notify(sdata, bss_change);
Johannes Berg46900292009-02-15 12:44:28 +0100225
Johannes Bergb59066a2009-05-12 21:18:38 +0200226 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
Johannes Berg46900292009-02-15 12:44:28 +0100227
Johannes Berg46900292009-02-15 12:44:28 +0100228 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200229 mod_timer(&ifibss->timer,
230 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100231
Johannes Bergf446d102009-10-28 15:12:32 +0100232 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
233 mgmt, skb->len, 0, GFP_KERNEL);
234 cfg80211_put_bss(bss);
Eliad Peller86a2ea42011-11-08 15:36:59 +0200235 netif_carrier_on(sdata->dev);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200236 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100237}
238
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200239static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
240 struct ieee80211_bss *bss)
Johannes Berg46900292009-02-15 12:44:28 +0100241{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100242 struct cfg80211_bss *cbss =
243 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergb59066a2009-05-12 21:18:38 +0200244 struct ieee80211_supported_band *sband;
245 u32 basic_rates;
246 int i, j;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100247 u16 beacon_int = cbss->beacon_interval;
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200248
Johannes Berg7a17a332010-07-21 11:30:27 +0200249 lockdep_assert_held(&sdata->u.ibss.mtx);
250
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200251 if (beacon_int < 10)
252 beacon_int = 10;
253
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100254 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
Johannes Bergb59066a2009-05-12 21:18:38 +0200255
256 basic_rates = 0;
257
258 for (i = 0; i < bss->supp_rates_len; i++) {
259 int rate = (bss->supp_rates[i] & 0x7f) * 5;
260 bool is_basic = !!(bss->supp_rates[i] & 0x80);
261
262 for (j = 0; j < sband->n_bitrates; j++) {
263 if (sband->bitrates[j].bitrate == rate) {
264 if (is_basic)
265 basic_rates |= BIT(j);
266 break;
267 }
268 }
269 }
270
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100271 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200272 beacon_int,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100273 cbss->channel,
Johannes Bergb59066a2009-05-12 21:18:38 +0200274 basic_rates,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100275 cbss->capability,
276 cbss->tsf);
Johannes Berg46900292009-02-15 12:44:28 +0100277}
278
Johannes Berg8bf11d82011-12-15 11:17:37 +0100279static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
280 __acquires(RCU)
281{
282 struct ieee80211_sub_if_data *sdata = sta->sdata;
283 u8 addr[ETH_ALEN];
284
285 memcpy(addr, sta->sta.addr, ETH_ALEN);
286
287#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
288 wiphy_debug(sdata->local->hw.wiphy,
289 "Adding new IBSS station %pM (dev=%s)\n",
290 addr, sdata->name);
291#endif
292
293 sta_info_move_state(sta, IEEE80211_STA_AUTH);
294 sta_info_move_state(sta, IEEE80211_STA_ASSOC);
295 sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
296
297 rate_control_rate_init(sta);
298
299 /* If it fails, maybe we raced another insertion? */
300 if (sta_info_insert_rcu(sta))
301 return sta_info_get(sdata, addr);
302 return sta;
303}
304
305static struct sta_info *
306ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
307 const u8 *bssid, const u8 *addr,
308 u32 supp_rates)
309 __acquires(RCU)
310{
311 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
312 struct ieee80211_local *local = sdata->local;
313 struct sta_info *sta;
314 int band = local->hw.conf.channel->band;
315
316 /*
317 * XXX: Consider removing the least recently used entry and
318 * allow new one to be added.
319 */
320 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
321 if (net_ratelimit())
322 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
323 sdata->name, addr);
324 rcu_read_lock();
325 return NULL;
326 }
327
328 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) {
329 rcu_read_lock();
330 return NULL;
331 }
332
333 if (compare_ether_addr(bssid, sdata->u.ibss.bssid)) {
334 rcu_read_lock();
335 return NULL;
336 }
337
338 sta = sta_info_alloc(sdata, addr, GFP_KERNEL);
339 if (!sta) {
340 rcu_read_lock();
341 return NULL;
342 }
343
344 sta->last_rx = jiffies;
345
346 /* make sure mandatory rates are always added */
347 sta->sta.supp_rates[band] = supp_rates |
348 ieee80211_mandatory_rates(local, band);
349
350 return ieee80211_ibss_finish_sta(sta);
351}
352
Johannes Berg46900292009-02-15 12:44:28 +0100353static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
354 struct ieee80211_mgmt *mgmt,
355 size_t len,
356 struct ieee80211_rx_status *rx_status,
357 struct ieee802_11_elems *elems,
358 bool beacon)
359{
360 struct ieee80211_local *local = sdata->local;
361 int freq;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100362 struct cfg80211_bss *cbss;
Johannes Berg46900292009-02-15 12:44:28 +0100363 struct ieee80211_bss *bss;
364 struct sta_info *sta;
365 struct ieee80211_channel *channel;
366 u64 beacon_timestamp, rx_timestamp;
367 u32 supp_rates = 0;
368 enum ieee80211_band band = rx_status->band;
Alexander Simon13c40c52011-11-30 16:56:34 +0100369 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
370 bool rates_updated = false;
Johannes Berg46900292009-02-15 12:44:28 +0100371
372 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900373 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
374 band);
Johannes Berg46900292009-02-15 12:44:28 +0100375 else
376 freq = rx_status->freq;
377
378 channel = ieee80211_get_channel(local->hw.wiphy, freq);
379
380 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
381 return;
382
Bruno Randolf9eba6122010-10-04 11:17:30 +0900383 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg46900292009-02-15 12:44:28 +0100384 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
Johannes Berg46900292009-02-15 12:44:28 +0100385
386 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +0100387 sta = sta_info_get(sdata, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100388
Bruno Randolf9eba6122010-10-04 11:17:30 +0900389 if (elems->supp_rates) {
390 supp_rates = ieee80211_sta_get_rates(local, elems,
391 band);
392 if (sta) {
393 u32 prev_rates;
Johannes Berg46900292009-02-15 12:44:28 +0100394
Bruno Randolf9eba6122010-10-04 11:17:30 +0900395 prev_rates = sta->sta.supp_rates[band];
396 /* make sure mandatory rates are always added */
397 sta->sta.supp_rates[band] = supp_rates |
398 ieee80211_mandatory_rates(local, band);
399
400 if (sta->sta.supp_rates[band] != prev_rates) {
Johannes Berg46900292009-02-15 12:44:28 +0100401#ifdef CONFIG_MAC80211_IBSS_DEBUG
Bruno Randolf9eba6122010-10-04 11:17:30 +0900402 printk(KERN_DEBUG
403 "%s: updated supp_rates set "
404 "for %pM based on beacon"
405 "/probe_resp (0x%x -> 0x%x)\n",
406 sdata->name, sta->sta.addr,
407 prev_rates,
408 sta->sta.supp_rates[band]);
Johannes Berg46900292009-02-15 12:44:28 +0100409#endif
Alexander Simon13c40c52011-11-30 16:56:34 +0100410 rates_updated = true;
Bruno Randolf9eba6122010-10-04 11:17:30 +0900411 }
Johannes Berg8bf11d82011-12-15 11:17:37 +0100412 } else {
413 rcu_read_unlock();
Bruno Randolf9eba6122010-10-04 11:17:30 +0900414 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
Johannes Berg8bf11d82011-12-15 11:17:37 +0100415 mgmt->sa, supp_rates);
416 }
Johannes Berg34e89502010-02-03 13:59:58 +0100417 }
Bruno Randolf9eba6122010-10-04 11:17:30 +0900418
419 if (sta && elems->wmm_info)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200420 set_sta_flag(sta, WLAN_STA_WME);
Bruno Randolf9eba6122010-10-04 11:17:30 +0900421
Alexander Simon13c40c52011-11-30 16:56:34 +0100422 if (sta && elems->ht_info_elem && elems->ht_cap_elem &&
423 sdata->u.ibss.channel_type != NL80211_CHAN_NO_HT) {
424 /* we both use HT */
425 struct ieee80211_sta_ht_cap sta_ht_cap_new;
426 enum nl80211_channel_type channel_type =
427 ieee80211_ht_info_to_channel_type(
428 elems->ht_info_elem);
429
430 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
431 elems->ht_cap_elem,
432 &sta_ht_cap_new);
433
434 /*
435 * fall back to HT20 if we don't use or use
436 * the other extension channel
437 */
438 if ((channel_type == NL80211_CHAN_HT40MINUS ||
439 channel_type == NL80211_CHAN_HT40PLUS) &&
440 channel_type != sdata->u.ibss.channel_type)
441 sta_ht_cap_new.cap &=
442 ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
443
444 if (memcmp(&sta->sta.ht_cap, &sta_ht_cap_new,
445 sizeof(sta_ht_cap_new))) {
446 memcpy(&sta->sta.ht_cap, &sta_ht_cap_new,
447 sizeof(sta_ht_cap_new));
448 rates_updated = true;
449 }
450 }
451
452 if (sta && rates_updated)
453 rate_control_rate_init(sta);
454
Bruno Randolf9eba6122010-10-04 11:17:30 +0900455 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100456 }
457
458 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
459 channel, beacon);
460 if (!bss)
461 return;
462
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100463 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
464
Johannes Berg46900292009-02-15 12:44:28 +0100465 /* was just updated in ieee80211_bss_info_update */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100466 beacon_timestamp = cbss->tsf;
Johannes Berg46900292009-02-15 12:44:28 +0100467
468 /* check if we need to merge IBSS */
469
Johannes Berg46900292009-02-15 12:44:28 +0100470 /* we use a fixed BSSID */
Benoit Papillaulta98bfec2010-01-17 22:45:24 +0100471 if (sdata->u.ibss.fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100472 goto put_bss;
473
474 /* not an IBSS */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100475 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
Johannes Berg46900292009-02-15 12:44:28 +0100476 goto put_bss;
477
478 /* different channel */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100479 if (cbss->channel != local->oper_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100480 goto put_bss;
481
482 /* different SSID */
483 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
484 memcmp(elems->ssid, sdata->u.ibss.ssid,
485 sdata->u.ibss.ssid_len))
486 goto put_bss;
487
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100488 /* same BSSID */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100489 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
Alina Friedrichsen34e8f082009-02-22 00:07:28 +0100490 goto put_bss;
491
Johannes Berg6ebacbb2011-02-23 15:06:08 +0100492 if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
Johannes Berg46900292009-02-15 12:44:28 +0100493 /*
494 * For correct IBSS merging we need mactime; since mactime is
495 * defined as the time the first data symbol of the frame hits
496 * the PHY, and the timestamp of the beacon is defined as "the
497 * time that the data symbol containing the first bit of the
498 * timestamp is transmitted to the PHY plus the transmitting
499 * STA's delays through its local PHY from the MAC-PHY
500 * interface to its interface with the WM" (802.11 11.1.2)
501 * - equals the time this bit arrives at the receiver - we have
502 * to take into account the offset between the two.
503 *
504 * E.g. at 1 MBit that means mactime is 192 usec earlier
505 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
506 */
507 int rate;
508
509 if (rx_status->flag & RX_FLAG_HT)
510 rate = 65; /* TODO: HT rates */
511 else
512 rate = local->hw.wiphy->bands[band]->
513 bitrates[rx_status->rate_idx].bitrate;
514
515 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
Johannes Berg24487982009-04-23 18:52:52 +0200516 } else {
517 /*
518 * second best option: get current TSF
519 * (will return -1 if not supported)
520 */
Eliad Peller37a41b42011-09-21 14:06:11 +0300521 rx_timestamp = drv_get_tsf(local, sdata);
Johannes Berg24487982009-04-23 18:52:52 +0200522 }
Johannes Berg46900292009-02-15 12:44:28 +0100523
524#ifdef CONFIG_MAC80211_IBSS_DEBUG
525 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
526 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
527 mgmt->sa, mgmt->bssid,
528 (unsigned long long)rx_timestamp,
529 (unsigned long long)beacon_timestamp,
530 (unsigned long long)(rx_timestamp - beacon_timestamp),
531 jiffies);
532#endif
533
534 if (beacon_timestamp > rx_timestamp) {
535#ifdef CONFIG_MAC80211_IBSS_DEBUG
536 printk(KERN_DEBUG "%s: beacon TSF higher than "
537 "local TSF - IBSS merge with BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100538 sdata->name, mgmt->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100539#endif
540 ieee80211_sta_join_ibss(sdata, bss);
Bruno Randolf09a08cf2010-03-03 18:45:42 +0900541 supp_rates = ieee80211_sta_get_rates(local, elems, band);
Johannes Berg34e89502010-02-03 13:59:58 +0100542 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
Johannes Berg8bf11d82011-12-15 11:17:37 +0100543 supp_rates);
544 rcu_read_unlock();
Johannes Berg46900292009-02-15 12:44:28 +0100545 }
546
547 put_bss:
548 ieee80211_rx_bss_put(local, bss);
549}
550
Johannes Berg8bf11d82011-12-15 11:17:37 +0100551void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
552 const u8 *bssid, const u8 *addr,
553 u32 supp_rates)
Johannes Berg46900292009-02-15 12:44:28 +0100554{
Felix Fietkau2e10d332009-12-20 19:07:09 +0100555 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100556 struct ieee80211_local *local = sdata->local;
557 struct sta_info *sta;
558 int band = local->hw.conf.channel->band;
559
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200560 /*
561 * XXX: Consider removing the least recently used entry and
562 * allow new one to be added.
563 */
Johannes Berg46900292009-02-15 12:44:28 +0100564 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200565 if (net_ratelimit())
566 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100567 sdata->name, addr);
Johannes Berg8bf11d82011-12-15 11:17:37 +0100568 return;
Johannes Berg46900292009-02-15 12:44:28 +0100569 }
570
Felix Fietkau2e10d332009-12-20 19:07:09 +0100571 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100572 return;
Felix Fietkau2e10d332009-12-20 19:07:09 +0100573
Johannes Berg46900292009-02-15 12:44:28 +0100574 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
Johannes Berg8bf11d82011-12-15 11:17:37 +0100575 return;
Johannes Berg46900292009-02-15 12:44:28 +0100576
Johannes Berg8bf11d82011-12-15 11:17:37 +0100577 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
Johannes Berg46900292009-02-15 12:44:28 +0100578 if (!sta)
Johannes Berg8bf11d82011-12-15 11:17:37 +0100579 return;
Johannes Berg46900292009-02-15 12:44:28 +0100580
Rajkumar Manoharanc8716d92010-10-23 10:59:57 +0530581 sta->last_rx = jiffies;
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100582
Johannes Berg46900292009-02-15 12:44:28 +0100583 /* make sure mandatory rates are always added */
584 sta->sta.supp_rates[band] = supp_rates |
585 ieee80211_mandatory_rates(local, band);
586
Johannes Berg8bf11d82011-12-15 11:17:37 +0100587 spin_lock(&ifibss->incomplete_lock);
588 list_add(&sta->list, &ifibss->incomplete_stations);
589 spin_unlock(&ifibss->incomplete_lock);
590 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100591}
592
593static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
594{
595 struct ieee80211_local *local = sdata->local;
596 int active = 0;
597 struct sta_info *sta;
598
Johannes Berg7a17a332010-07-21 11:30:27 +0200599 lockdep_assert_held(&sdata->u.ibss.mtx);
600
Johannes Berg46900292009-02-15 12:44:28 +0100601 rcu_read_lock();
602
603 list_for_each_entry_rcu(sta, &local->sta_list, list) {
604 if (sta->sdata == sdata &&
605 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
606 jiffies)) {
607 active++;
608 break;
609 }
610 }
611
612 rcu_read_unlock();
613
614 return active;
615}
616
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100617/*
618 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
619 */
Johannes Berg46900292009-02-15 12:44:28 +0100620
621static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
622{
623 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
624
Johannes Berg7a17a332010-07-21 11:30:27 +0200625 lockdep_assert_held(&ifibss->mtx);
626
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200627 mod_timer(&ifibss->timer,
628 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
Johannes Berg46900292009-02-15 12:44:28 +0100629
630 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200631
Sujith450aae32009-11-02 12:33:23 +0530632 if (time_before(jiffies, ifibss->last_scan_completed +
633 IEEE80211_IBSS_MERGE_INTERVAL))
634 return;
635
Johannes Berg46900292009-02-15 12:44:28 +0100636 if (ieee80211_sta_active_ibss(sdata))
637 return;
638
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200639 if (ifibss->fixed_channel)
Johannes Berg46900292009-02-15 12:44:28 +0100640 return;
641
642 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Johannes Berg47846c92009-11-25 17:46:19 +0100643 "IBSS networks with same SSID (merge)\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100644
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200645 ieee80211_request_internal_scan(sdata,
646 ifibss->ssid, ifibss->ssid_len,
647 ifibss->fixed_channel ? ifibss->channel : NULL);
Johannes Berg46900292009-02-15 12:44:28 +0100648}
649
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200650static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100651{
652 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg46900292009-02-15 12:44:28 +0100653 u8 bssid[ETH_ALEN];
Johannes Berg46900292009-02-15 12:44:28 +0100654 u16 capability;
655 int i;
656
Johannes Berg7a17a332010-07-21 11:30:27 +0200657 lockdep_assert_held(&ifibss->mtx);
658
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200659 if (ifibss->fixed_bssid) {
Johannes Berg46900292009-02-15 12:44:28 +0100660 memcpy(bssid, ifibss->bssid, ETH_ALEN);
661 } else {
662 /* Generate random, not broadcast, locally administered BSSID. Mix in
663 * own MAC address to make sure that devices that do not have proper
664 * random number generator get different BSSID. */
665 get_random_bytes(bssid, ETH_ALEN);
666 for (i = 0; i < ETH_ALEN; i++)
Johannes Berg47846c92009-11-25 17:46:19 +0100667 bssid[i] ^= sdata->vif.addr[i];
Johannes Berg46900292009-02-15 12:44:28 +0100668 bssid[0] &= ~0x01;
669 bssid[0] |= 0x02;
670 }
671
672 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100673 sdata->name, bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100674
Johannes Berg46900292009-02-15 12:44:28 +0100675 capability = WLAN_CAPABILITY_IBSS;
676
Johannes Bergfffd0932009-07-08 14:22:54 +0200677 if (ifibss->privacy)
Johannes Berg46900292009-02-15 12:44:28 +0100678 capability |= WLAN_CAPABILITY_PRIVACY;
679 else
680 sdata->drop_unencrypted = 0;
681
Johannes Berg57c4d7b2009-04-23 16:10:04 +0200682 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +0300683 ifibss->channel, ifibss->basic_rates,
Johannes Bergb59066a2009-05-12 21:18:38 +0200684 capability, 0);
Johannes Berg46900292009-02-15 12:44:28 +0100685}
686
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100687/*
688 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
689 */
690
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200691static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100692{
693 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
694 struct ieee80211_local *local = sdata->local;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100695 struct cfg80211_bss *cbss;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200696 struct ieee80211_channel *chan = NULL;
Johannes Berg46900292009-02-15 12:44:28 +0100697 const u8 *bssid = NULL;
698 int active_ibss;
Johannes Berge0d61882009-05-12 20:47:32 +0200699 u16 capability;
Johannes Berg46900292009-02-15 12:44:28 +0100700
Johannes Berg7a17a332010-07-21 11:30:27 +0200701 lockdep_assert_held(&ifibss->mtx);
702
Johannes Berg46900292009-02-15 12:44:28 +0100703 active_ibss = ieee80211_sta_active_ibss(sdata);
704#ifdef CONFIG_MAC80211_IBSS_DEBUG
705 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100706 sdata->name, active_ibss);
Johannes Berg46900292009-02-15 12:44:28 +0100707#endif /* CONFIG_MAC80211_IBSS_DEBUG */
708
709 if (active_ibss)
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200710 return;
Johannes Berg46900292009-02-15 12:44:28 +0100711
Johannes Berge0d61882009-05-12 20:47:32 +0200712 capability = WLAN_CAPABILITY_IBSS;
Johannes Bergfffd0932009-07-08 14:22:54 +0200713 if (ifibss->privacy)
Johannes Berge0d61882009-05-12 20:47:32 +0200714 capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200715 if (ifibss->fixed_bssid)
Johannes Berg46900292009-02-15 12:44:28 +0100716 bssid = ifibss->bssid;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200717 if (ifibss->fixed_channel)
718 chan = ifibss->channel;
719 if (!is_zero_ether_addr(ifibss->bssid))
720 bssid = ifibss->bssid;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100721 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
722 ifibss->ssid, ifibss->ssid_len,
723 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
724 capability);
Johannes Berg46900292009-02-15 12:44:28 +0100725
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100726 if (cbss) {
727 struct ieee80211_bss *bss;
728
729 bss = (void *)cbss->priv;
Johannes Berg46900292009-02-15 12:44:28 +0100730#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Berg46900292009-02-15 12:44:28 +0100731 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100732 "%pM\n", cbss->bssid, ifibss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100733#endif /* CONFIG_MAC80211_IBSS_DEBUG */
734
Johannes Berg46900292009-02-15 12:44:28 +0100735 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
736 " based on configured SSID\n",
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100737 sdata->name, cbss->bssid);
Johannes Berg46900292009-02-15 12:44:28 +0100738
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200739 ieee80211_sta_join_ibss(sdata, bss);
Johannes Berg46900292009-02-15 12:44:28 +0100740 ieee80211_rx_bss_put(local, bss);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200741 return;
Reinette Chatred419b9f2009-10-19 14:55:37 -0700742 }
Johannes Berg46900292009-02-15 12:44:28 +0100743
744#ifdef CONFIG_MAC80211_IBSS_DEBUG
745 printk(KERN_DEBUG " did not try to join ibss\n");
746#endif /* CONFIG_MAC80211_IBSS_DEBUG */
747
748 /* Selected IBSS not found in current scan results - try to scan */
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100749 if (time_after(jiffies, ifibss->last_scan_completed +
Johannes Berg46900292009-02-15 12:44:28 +0100750 IEEE80211_SCAN_INTERVAL)) {
751 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Johannes Berg47846c92009-11-25 17:46:19 +0100752 "join\n", sdata->name);
Johannes Berg46900292009-02-15 12:44:28 +0100753
Johannes Bergbe4a4b62010-05-03 08:49:48 +0200754 ieee80211_request_internal_scan(sdata,
755 ifibss->ssid, ifibss->ssid_len,
756 ifibss->fixed_channel ? ifibss->channel : NULL);
Benoit Papillaultce9058a2010-01-17 22:45:23 +0100757 } else {
Johannes Berg46900292009-02-15 12:44:28 +0100758 int interval = IEEE80211_SCAN_INTERVAL;
759
760 if (time_after(jiffies, ifibss->ibss_join_req +
761 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200762 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
763 ieee80211_sta_create_ibss(sdata);
764 return;
765 }
Johannes Berg46900292009-02-15 12:44:28 +0100766 printk(KERN_DEBUG "%s: IBSS not allowed on"
Johannes Berg47846c92009-11-25 17:46:19 +0100767 " %d MHz\n", sdata->name,
Johannes Berg46900292009-02-15 12:44:28 +0100768 local->hw.conf.channel->center_freq);
769
770 /* No IBSS found - decrease scan interval and continue
771 * scanning. */
772 interval = IEEE80211_SCAN_INTERVAL_SLOW;
773 }
774
Johannes Bergaf8cdcd2009-04-19 21:25:43 +0200775 mod_timer(&ifibss->timer,
776 round_jiffies(jiffies + interval));
Johannes Berg46900292009-02-15 12:44:28 +0100777 }
Johannes Berg46900292009-02-15 12:44:28 +0100778}
779
780static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Johannes Bergc269a202011-02-14 12:20:22 +0100781 struct sk_buff *req)
Johannes Berg46900292009-02-15 12:44:28 +0100782{
Johannes Bergc269a202011-02-14 12:20:22 +0100783 struct ieee80211_mgmt *mgmt = (void *)req->data;
Johannes Berg46900292009-02-15 12:44:28 +0100784 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
785 struct ieee80211_local *local = sdata->local;
Johannes Bergc269a202011-02-14 12:20:22 +0100786 int tx_last_beacon, len = req->len;
Johannes Berg46900292009-02-15 12:44:28 +0100787 struct sk_buff *skb;
788 struct ieee80211_mgmt *resp;
Johannes Berg40b275b2011-05-13 14:15:49 +0200789 struct sk_buff *presp;
Johannes Berg46900292009-02-15 12:44:28 +0100790 u8 *pos, *end;
791
Johannes Berg7a17a332010-07-21 11:30:27 +0200792 lockdep_assert_held(&ifibss->mtx);
793
Johannes Berg40b275b2011-05-13 14:15:49 +0200794 presp = rcu_dereference_protected(ifibss->presp,
795 lockdep_is_held(&ifibss->mtx));
796
Johannes Berg46900292009-02-15 12:44:28 +0100797 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
Johannes Berg40b275b2011-05-13 14:15:49 +0200798 len < 24 + 2 || !presp)
Johannes Berg46900292009-02-15 12:44:28 +0100799 return;
800
Johannes Berg24487982009-04-23 18:52:52 +0200801 tx_last_beacon = drv_tx_last_beacon(local);
Johannes Berg46900292009-02-15 12:44:28 +0100802
803#ifdef CONFIG_MAC80211_IBSS_DEBUG
804 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
805 " (tx_last_beacon=%d)\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100806 sdata->name, mgmt->sa, mgmt->da,
Johannes Berg46900292009-02-15 12:44:28 +0100807 mgmt->bssid, tx_last_beacon);
808#endif /* CONFIG_MAC80211_IBSS_DEBUG */
809
Felix Fietkau1ed76482011-03-24 19:46:18 +0100810 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
Johannes Berg46900292009-02-15 12:44:28 +0100811 return;
812
813 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
814 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
815 return;
816
817 end = ((u8 *) mgmt) + len;
818 pos = mgmt->u.probe_req.variable;
819 if (pos[0] != WLAN_EID_SSID ||
820 pos + 2 + pos[1] > end) {
821#ifdef CONFIG_MAC80211_IBSS_DEBUG
822 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
823 "from %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100824 sdata->name, mgmt->sa);
Johannes Berg46900292009-02-15 12:44:28 +0100825#endif
826 return;
827 }
828 if (pos[1] != 0 &&
829 (pos[1] != ifibss->ssid_len ||
Benoit Papillault0da780c2010-02-05 01:21:03 +0100830 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
Johannes Berg46900292009-02-15 12:44:28 +0100831 /* Ignore ProbeReq for foreign SSID */
832 return;
833 }
834
835 /* Reply with ProbeResp */
Johannes Berg40b275b2011-05-13 14:15:49 +0200836 skb = skb_copy(presp, GFP_KERNEL);
Johannes Berg46900292009-02-15 12:44:28 +0100837 if (!skb)
838 return;
839
840 resp = (struct ieee80211_mgmt *) skb->data;
841 memcpy(resp->da, mgmt->sa, ETH_ALEN);
842#ifdef CONFIG_MAC80211_IBSS_DEBUG
843 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100844 sdata->name, resp->da);
Johannes Berg46900292009-02-15 12:44:28 +0100845#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Johannes Berg62ae67b2009-11-18 18:42:05 +0100846 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
847 ieee80211_tx_skb(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100848}
849
850static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
851 struct ieee80211_mgmt *mgmt,
852 size_t len,
853 struct ieee80211_rx_status *rx_status)
854{
855 size_t baselen;
856 struct ieee802_11_elems elems;
857
Johannes Berg47846c92009-11-25 17:46:19 +0100858 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
Johannes Berg46900292009-02-15 12:44:28 +0100859 return; /* ignore ProbeResp to foreign address */
860
861 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
862 if (baselen > len)
863 return;
864
865 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
866 &elems);
867
868 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
869}
870
871static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
872 struct ieee80211_mgmt *mgmt,
873 size_t len,
874 struct ieee80211_rx_status *rx_status)
875{
876 size_t baselen;
877 struct ieee802_11_elems elems;
878
879 /* Process beacon from the current BSS */
880 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
881 if (baselen > len)
882 return;
883
884 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
885
886 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
887}
888
Johannes Berg1fa57d02010-06-10 10:21:32 +0200889void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
890 struct sk_buff *skb)
Johannes Berg46900292009-02-15 12:44:28 +0100891{
892 struct ieee80211_rx_status *rx_status;
893 struct ieee80211_mgmt *mgmt;
894 u16 fc;
895
Johannes Bergf1d58c22009-06-17 13:13:00 +0200896 rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Berg46900292009-02-15 12:44:28 +0100897 mgmt = (struct ieee80211_mgmt *) skb->data;
898 fc = le16_to_cpu(mgmt->frame_control);
899
Johannes Berg7a17a332010-07-21 11:30:27 +0200900 mutex_lock(&sdata->u.ibss.mtx);
901
Tim Harveyc926d002010-12-09 10:43:13 -0800902 if (!sdata->u.ibss.ssid_len)
903 goto mgmt_out; /* not ready to merge yet */
904
Johannes Berg46900292009-02-15 12:44:28 +0100905 switch (fc & IEEE80211_FCTL_STYPE) {
906 case IEEE80211_STYPE_PROBE_REQ:
Johannes Bergc269a202011-02-14 12:20:22 +0100907 ieee80211_rx_mgmt_probe_req(sdata, skb);
Johannes Berg46900292009-02-15 12:44:28 +0100908 break;
909 case IEEE80211_STYPE_PROBE_RESP:
910 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
911 rx_status);
912 break;
913 case IEEE80211_STYPE_BEACON:
914 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
915 rx_status);
916 break;
917 case IEEE80211_STYPE_AUTH:
918 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
919 break;
920 }
Johannes Berg7a17a332010-07-21 11:30:27 +0200921
Tim Harveyc926d002010-12-09 10:43:13 -0800922 mgmt_out:
Johannes Berg7a17a332010-07-21 11:30:27 +0200923 mutex_unlock(&sdata->u.ibss.mtx);
Johannes Berg46900292009-02-15 12:44:28 +0100924}
925
Johannes Berg1fa57d02010-06-10 10:21:32 +0200926void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg46900292009-02-15 12:44:28 +0100927{
Johannes Berg1fa57d02010-06-10 10:21:32 +0200928 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +0100929 struct sta_info *sta;
Johannes Berg46900292009-02-15 12:44:28 +0100930
Johannes Berg7a17a332010-07-21 11:30:27 +0200931 mutex_lock(&ifibss->mtx);
932
933 /*
934 * Work could be scheduled after scan or similar
935 * when we aren't even joined (or trying) with a
936 * network.
937 */
938 if (!ifibss->ssid_len)
939 goto out;
Johannes Berg46900292009-02-15 12:44:28 +0100940
Johannes Berg8bf11d82011-12-15 11:17:37 +0100941 spin_lock_bh(&ifibss->incomplete_lock);
942 while (!list_empty(&ifibss->incomplete_stations)) {
943 sta = list_first_entry(&ifibss->incomplete_stations,
944 struct sta_info, list);
945 list_del(&sta->list);
946 spin_unlock_bh(&ifibss->incomplete_lock);
947
948 ieee80211_ibss_finish_sta(sta);
949 rcu_read_unlock();
950 spin_lock_bh(&ifibss->incomplete_lock);
951 }
952 spin_unlock_bh(&ifibss->incomplete_lock);
953
Johannes Berg46900292009-02-15 12:44:28 +0100954 switch (ifibss->state) {
955 case IEEE80211_IBSS_MLME_SEARCH:
956 ieee80211_sta_find_ibss(sdata);
957 break;
958 case IEEE80211_IBSS_MLME_JOINED:
959 ieee80211_sta_merge_ibss(sdata);
960 break;
961 default:
962 WARN_ON(1);
963 break;
964 }
Johannes Berg46900292009-02-15 12:44:28 +0100965
Johannes Berg7a17a332010-07-21 11:30:27 +0200966 out:
967 mutex_unlock(&ifibss->mtx);
Johannes Berg3a4d4aa2010-05-26 16:41:40 +0200968}
969
Johannes Berg46900292009-02-15 12:44:28 +0100970static void ieee80211_ibss_timer(unsigned long data)
971{
972 struct ieee80211_sub_if_data *sdata =
973 (struct ieee80211_sub_if_data *) data;
974 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
975 struct ieee80211_local *local = sdata->local;
976
Johannes Berg5bb644a2009-05-17 11:40:42 +0200977 if (local->quiescing) {
978 ifibss->timer_running = true;
979 return;
980 }
981
Johannes Berg7a17a332010-07-21 11:30:27 +0200982 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +0100983}
984
Johannes Berg5bb644a2009-05-17 11:40:42 +0200985#ifdef CONFIG_PM
986void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
987{
988 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
989
Johannes Berg5bb644a2009-05-17 11:40:42 +0200990 if (del_timer_sync(&ifibss->timer))
991 ifibss->timer_running = true;
992}
993
994void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
995{
996 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
997
998 if (ifibss->timer_running) {
999 add_timer(&ifibss->timer);
1000 ifibss->timer_running = false;
1001 }
1002}
1003#endif
1004
Johannes Berg46900292009-02-15 12:44:28 +01001005void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1006{
1007 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1008
Johannes Berg46900292009-02-15 12:44:28 +01001009 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
1010 (unsigned long) sdata);
Johannes Berg7a17a332010-07-21 11:30:27 +02001011 mutex_init(&ifibss->mtx);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001012 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1013 spin_lock_init(&ifibss->incomplete_lock);
Johannes Berg46900292009-02-15 12:44:28 +01001014}
1015
1016/* scan finished notification */
1017void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
1018{
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001019 struct ieee80211_sub_if_data *sdata;
Johannes Berg46900292009-02-15 12:44:28 +01001020
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001021 mutex_lock(&local->iflist_mtx);
1022 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001023 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e41f712009-04-23 11:48:56 +02001024 continue;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001025 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1026 continue;
1027 sdata->u.ibss.last_scan_completed = jiffies;
Johannes Berg7a17a332010-07-21 11:30:27 +02001028 ieee80211_queue_work(&local->hw, &sdata->work);
Johannes Berg46900292009-02-15 12:44:28 +01001029 }
Johannes Berg29b4a4f2009-04-21 00:30:49 +02001030 mutex_unlock(&local->iflist_mtx);
Johannes Berg46900292009-02-15 12:44:28 +01001031}
1032
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001033int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1034 struct cfg80211_ibss_params *params)
1035{
1036 struct sk_buff *skb;
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001037 u32 changed = 0;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001038
Johannes Berg7a17a332010-07-21 11:30:27 +02001039 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
Alexander Simon13c40c52011-11-30 16:56:34 +01001040 sizeof(struct ieee80211_hdr_3addr) +
1041 12 /* struct ieee80211_mgmt.u.beacon */ +
1042 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ +
1043 2 + 8 /* max Supported Rates */ +
1044 3 /* max DS params */ +
1045 4 /* IBSS params */ +
1046 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
1047 2 + sizeof(struct ieee80211_ht_cap) +
1048 2 + sizeof(struct ieee80211_ht_info) +
Johannes Berg7a17a332010-07-21 11:30:27 +02001049 params->ie_len);
1050 if (!skb)
1051 return -ENOMEM;
1052
1053 mutex_lock(&sdata->u.ibss.mtx);
1054
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001055 if (params->bssid) {
1056 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
1057 sdata->u.ibss.fixed_bssid = true;
1058 } else
1059 sdata->u.ibss.fixed_bssid = false;
1060
Johannes Bergfffd0932009-07-08 14:22:54 +02001061 sdata->u.ibss.privacy = params->privacy;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03001062 sdata->u.ibss.basic_rates = params->basic_rates;
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01001063 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
1064 sizeof(params->mcast_rate));
Johannes Bergfffd0932009-07-08 14:22:54 +02001065
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001066 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
1067
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001068 sdata->u.ibss.channel = params->channel;
Alexander Simon13c40c52011-11-30 16:56:34 +01001069 sdata->u.ibss.channel_type = params->channel_type;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001070 sdata->u.ibss.fixed_channel = params->channel_fixed;
1071
Johannes Bergadfba3c2010-05-05 15:33:55 +02001072 /* fix ourselves to that channel now already */
1073 if (params->channel_fixed) {
1074 sdata->local->oper_channel = params->channel;
Alexander Simon13c40c52011-11-30 16:56:34 +01001075 if (!ieee80211_set_channel_type(sdata->local, sdata,
Dan Carpenterfb03c5e2011-12-08 09:49:03 +03001076 params->channel_type)) {
1077 mutex_unlock(&sdata->u.ibss.mtx);
Simon Wunderlichcb71b8d2011-12-14 13:33:30 +01001078 kfree_skb(skb);
Alexander Simon13c40c52011-11-30 16:56:34 +01001079 return -EINVAL;
Dan Carpenterfb03c5e2011-12-08 09:49:03 +03001080 }
Johannes Bergadfba3c2010-05-05 15:33:55 +02001081 }
1082
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001083 if (params->ie) {
1084 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
1085 GFP_KERNEL);
1086 if (sdata->u.ibss.ie)
1087 sdata->u.ibss.ie_len = params->ie_len;
1088 }
1089
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001090 sdata->u.ibss.skb = skb;
1091 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1092 sdata->u.ibss.ibss_join_req = jiffies;
1093
Johannes Berg0e41f712009-04-23 11:48:56 +02001094 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
Johannes Berg0e41f712009-04-23 11:48:56 +02001095 sdata->u.ibss.ssid_len = params->ssid_len;
1096
Johannes Berg7da7cc12010-08-05 17:02:38 +02001097 mutex_unlock(&sdata->u.ibss.mtx);
1098
1099 mutex_lock(&sdata->local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001100 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001101 mutex_unlock(&sdata->local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001102
Simon Wunderlichff3cc5f2011-11-30 16:56:33 +01001103 /*
1104 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
1105 * reserved, but an HT STA shall protect HT transmissions as though
1106 * the HT Protection field were set to non-HT mixed mode.
1107 *
1108 * In an IBSS, the RIFS Mode field of the HT Operation element is
1109 * also reserved, but an HT STA shall operate as though this field
1110 * were set to 1.
1111 */
1112
1113 sdata->vif.bss_conf.ht_operation_mode |=
1114 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
1115 | IEEE80211_HT_PARAM_RIFS_MODE;
1116
1117 changed |= BSS_CHANGED_HT;
1118 ieee80211_bss_info_change_notify(sdata, changed);
1119
Johannes Berg64592c82010-06-10 10:21:31 +02001120 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001121
1122 return 0;
1123}
1124
1125int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1126{
1127 struct sk_buff *skb;
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001128 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1129 struct ieee80211_local *local = sdata->local;
1130 struct cfg80211_bss *cbss;
1131 u16 capability;
Johannes Berg7a17a332010-07-21 11:30:27 +02001132 int active_ibss;
Johannes Berg8bf11d82011-12-15 11:17:37 +01001133 struct sta_info *sta;
Johannes Berg7a17a332010-07-21 11:30:27 +02001134
1135 mutex_lock(&sdata->u.ibss.mtx);
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001136
Johannes Bergf3209be2011-06-08 13:27:29 +02001137 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
1138 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
1139 sdata->u.ibss.ssid_len = 0;
1140
Teemu Paasikivi5ea096c2010-06-14 12:55:33 +03001141 active_ibss = ieee80211_sta_active_ibss(sdata);
1142
1143 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1144 capability = WLAN_CAPABILITY_IBSS;
1145
1146 if (ifibss->privacy)
1147 capability |= WLAN_CAPABILITY_PRIVACY;
1148
1149 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
1150 ifibss->bssid, ifibss->ssid,
1151 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1152 WLAN_CAPABILITY_PRIVACY,
1153 capability);
1154
1155 if (cbss) {
1156 cfg80211_unlink_bss(local->hw.wiphy, cbss);
1157 cfg80211_put_bss(cbss);
1158 }
1159 }
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001160
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001161 sta_info_flush(sdata->local, sdata);
Johannes Berg8bf11d82011-12-15 11:17:37 +01001162
1163 spin_lock_bh(&ifibss->incomplete_lock);
1164 while (!list_empty(&ifibss->incomplete_stations)) {
1165 sta = list_first_entry(&ifibss->incomplete_stations,
1166 struct sta_info, list);
1167 list_del(&sta->list);
1168 spin_unlock_bh(&ifibss->incomplete_lock);
1169
1170 sta_info_free(local, sta);
1171 spin_lock_bh(&ifibss->incomplete_lock);
1172 }
1173 spin_unlock_bh(&ifibss->incomplete_lock);
1174
Eliad Peller86a2ea42011-11-08 15:36:59 +02001175 netif_carrier_off(sdata->dev);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001176
1177 /* remove beacon */
1178 kfree(sdata->u.ibss.ie);
Johannes Berg40b275b2011-05-13 14:15:49 +02001179 skb = rcu_dereference_protected(sdata->u.ibss.presp,
1180 lockdep_is_held(&sdata->u.ibss.mtx));
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001181 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
Johannes Berg8fc214b2010-04-28 17:40:43 +02001182 sdata->vif.bss_conf.ibss_joined = false;
1183 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1184 BSS_CHANGED_IBSS);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001185 synchronize_rcu();
1186 kfree_skb(skb);
1187
Johannes Berg35f20c12010-06-10 10:21:30 +02001188 skb_queue_purge(&sdata->skb_queue);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001189
Johannes Bergbc05d192010-07-21 10:52:40 +02001190 del_timer_sync(&sdata->u.ibss.timer);
Johannes Berg7a17a332010-07-21 11:30:27 +02001191
1192 mutex_unlock(&sdata->u.ibss.mtx);
Johannes Bergbc05d192010-07-21 10:52:40 +02001193
Johannes Berg7da7cc12010-08-05 17:02:38 +02001194 mutex_lock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001195 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001196 mutex_unlock(&local->mtx);
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02001197
1198 return 0;
1199}