mac80211: handle protection mode, RIFS and ADDBA for HT IBSS
[linux-block.git] / net / mac80211 / ibss.c
CommitLineData
46900298
JB
1/*
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>
5a0e3ad6 16#include <linux/slab.h>
46900298
JB
17#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"
24487981 26#include "driver-ops.h"
46900298
JB
27#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{
0915cba3 43 u16 auth_alg, auth_transaction;
46900298 44
7a17a33c
JB
45 lockdep_assert_held(&sdata->u.ibss.mtx);
46
46900298
JB
47 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);
46900298
JB
52
53 /*
54 * IEEE 802.11 standard does not require authentication in IBSS
55 * networks and most implementations do not seem to use it.
56 * However, try to reply to authentication attempts if someone
57 * has actually implemented this.
58 */
59 if (auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1)
60 ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, NULL, 0,
fffd0934 61 sdata->u.ibss.bssid, NULL, 0, 0);
46900298
JB
62}
63
af8cdcd8
JB
64static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
65 const u8 *bssid, const int beacon_int,
66 struct ieee80211_channel *chan,
b59066a2 67 const u32 basic_rates,
af8cdcd8 68 const u16 capability, u64 tsf)
46900298
JB
69{
70 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
71 struct ieee80211_local *local = sdata->local;
b59066a2 72 int rates, i;
46900298
JB
73 struct sk_buff *skb;
74 struct ieee80211_mgmt *mgmt;
75 u8 *pos;
76 struct ieee80211_supported_band *sband;
f446d10f 77 struct cfg80211_bss *bss;
57c4d7b4 78 u32 bss_change;
b59066a2 79 u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
24487981 80
7a17a33c
JB
81 lockdep_assert_held(&ifibss->mtx);
82
24487981 83 /* Reset own TSF to allow time synchronization work. */
37a41b4a 84 drv_reset_tsf(local, sdata);
46900298 85
af8cdcd8 86 skb = ifibss->skb;
a9b3cd7f 87 RCU_INIT_POINTER(ifibss->presp, NULL);
af8cdcd8
JB
88 synchronize_rcu();
89 skb->data = skb->head;
90 skb->len = 0;
91 skb_reset_tail_pointer(skb);
92 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
46900298 93
af8cdcd8
JB
94 if (memcmp(ifibss->bssid, bssid, ETH_ALEN))
95 sta_info_flush(sdata->local, sdata);
46900298 96
49b5c7f4
JB
97 /* if merging, indicate to driver that we leave the old IBSS */
98 if (sdata->vif.bss_conf.ibss_joined) {
99 sdata->vif.bss_conf.ibss_joined = false;
86a2ea41 100 netif_carrier_off(sdata->dev);
49b5c7f4
JB
101 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IBSS);
102 }
103
46900298 104 memcpy(ifibss->bssid, bssid, ETH_ALEN);
46900298 105
af8cdcd8 106 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
46900298 107
af8cdcd8 108 local->oper_channel = chan;
0aaffa9b 109 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
af8cdcd8 110 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
57c4d7b4 111
af8cdcd8 112 sband = local->hw.wiphy->bands[chan->band];
46900298 113
b59066a2
JB
114 /* 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
46900298 124 /* Build IBSS probe response */
af8cdcd8 125 mgmt = (void *) skb_put(skb, 24 + sizeof(mgmt->u.beacon));
46900298
JB
126 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);
47846c9b 130 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298 131 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
57c4d7b4 132 mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int);
707c1b4e 133 mgmt->u.beacon.timestamp = cpu_to_le64(tsf);
46900298
JB
134 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
b59066a2 141 rates = sband->n_bitrates;
46900298
JB
142 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;
af8cdcd8 153 *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
46900298
JB
154 }
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
b59066a2
JB
163 if (sband->n_bitrates > 8) {
164 rates = sband->n_bitrates - 8;
46900298
JB
165 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
af8cdcd8
JB
171 if (ifibss->ie_len)
172 memcpy(skb_put(skb, ifibss->ie_len),
173 ifibss->ie, ifibss->ie_len);
174
9eba6125
BR
175 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
a9b3cd7f 188 RCU_INIT_POINTER(ifibss->presp, skb);
46900298 189
2d0ddec5 190 sdata->vif.bss_conf.beacon_int = beacon_int;
fbd2c8dc 191 sdata->vif.bss_conf.basic_rates = basic_rates;
2d0ddec5
JB
192 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;
392cfdb1 197 bss_change |= BSS_CHANGED_BASIC_RATES;
8fc214ba
JB
198 bss_change |= BSS_CHANGED_IBSS;
199 sdata->vif.bss_conf.ibss_joined = true;
2d0ddec5 200 ieee80211_bss_info_change_notify(sdata, bss_change);
46900298 201
b59066a2 202 ieee80211_sta_def_wmm_params(sdata, sband->n_bitrates, supp_rates);
46900298 203
46900298 204 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
af8cdcd8
JB
205 mod_timer(&ifibss->timer,
206 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298 207
f446d10f
JB
208 bss = cfg80211_inform_bss_frame(local->hw.wiphy, local->hw.conf.channel,
209 mgmt, skb->len, 0, GFP_KERNEL);
210 cfg80211_put_bss(bss);
86a2ea41 211 netif_carrier_on(sdata->dev);
af8cdcd8 212 cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL);
46900298
JB
213}
214
af8cdcd8
JB
215static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
216 struct ieee80211_bss *bss)
46900298 217{
0c1ad2ca
JB
218 struct cfg80211_bss *cbss =
219 container_of((void *)bss, struct cfg80211_bss, priv);
b59066a2
JB
220 struct ieee80211_supported_band *sband;
221 u32 basic_rates;
222 int i, j;
0c1ad2ca 223 u16 beacon_int = cbss->beacon_interval;
57c4d7b4 224
7a17a33c
JB
225 lockdep_assert_held(&sdata->u.ibss.mtx);
226
57c4d7b4
JB
227 if (beacon_int < 10)
228 beacon_int = 10;
229
0c1ad2ca 230 sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
b59066a2
JB
231
232 basic_rates = 0;
233
234 for (i = 0; i < bss->supp_rates_len; i++) {
235 int rate = (bss->supp_rates[i] & 0x7f) * 5;
236 bool is_basic = !!(bss->supp_rates[i] & 0x80);
237
238 for (j = 0; j < sband->n_bitrates; j++) {
239 if (sband->bitrates[j].bitrate == rate) {
240 if (is_basic)
241 basic_rates |= BIT(j);
242 break;
243 }
244 }
245 }
246
0c1ad2ca 247 __ieee80211_sta_join_ibss(sdata, cbss->bssid,
57c4d7b4 248 beacon_int,
0c1ad2ca 249 cbss->channel,
b59066a2 250 basic_rates,
0c1ad2ca
JB
251 cbss->capability,
252 cbss->tsf);
46900298
JB
253}
254
255static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
256 struct ieee80211_mgmt *mgmt,
257 size_t len,
258 struct ieee80211_rx_status *rx_status,
259 struct ieee802_11_elems *elems,
260 bool beacon)
261{
262 struct ieee80211_local *local = sdata->local;
263 int freq;
0c1ad2ca 264 struct cfg80211_bss *cbss;
46900298
JB
265 struct ieee80211_bss *bss;
266 struct sta_info *sta;
267 struct ieee80211_channel *channel;
268 u64 beacon_timestamp, rx_timestamp;
269 u32 supp_rates = 0;
270 enum ieee80211_band band = rx_status->band;
271
272 if (elems->ds_params && elems->ds_params_len == 1)
59eb21a6
BR
273 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
274 band);
46900298
JB
275 else
276 freq = rx_status->freq;
277
278 channel = ieee80211_get_channel(local->hw.wiphy, freq);
279
280 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
281 return;
282
9eba6125 283 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
46900298 284 memcmp(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0) {
46900298
JB
285
286 rcu_read_lock();
abe60632 287 sta = sta_info_get(sdata, mgmt->sa);
46900298 288
9eba6125
BR
289 if (elems->supp_rates) {
290 supp_rates = ieee80211_sta_get_rates(local, elems,
291 band);
292 if (sta) {
293 u32 prev_rates;
294
295 prev_rates = sta->sta.supp_rates[band];
296 /* make sure mandatory rates are always added */
297 sta->sta.supp_rates[band] = supp_rates |
298 ieee80211_mandatory_rates(local, band);
46900298 299
9eba6125 300 if (sta->sta.supp_rates[band] != prev_rates) {
46900298 301#ifdef CONFIG_MAC80211_IBSS_DEBUG
9eba6125
BR
302 printk(KERN_DEBUG
303 "%s: updated supp_rates set "
304 "for %pM based on beacon"
305 "/probe_resp (0x%x -> 0x%x)\n",
306 sdata->name, sta->sta.addr,
307 prev_rates,
308 sta->sta.supp_rates[band]);
46900298 309#endif
9eba6125
BR
310 rate_control_rate_init(sta);
311 }
312 } else
313 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
314 mgmt->sa, supp_rates,
315 GFP_ATOMIC);
34e89507 316 }
9eba6125
BR
317
318 if (sta && elems->wmm_info)
c2c98fde 319 set_sta_flag(sta, WLAN_STA_WME);
9eba6125
BR
320
321 rcu_read_unlock();
46900298
JB
322 }
323
324 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
325 channel, beacon);
326 if (!bss)
327 return;
328
0c1ad2ca
JB
329 cbss = container_of((void *)bss, struct cfg80211_bss, priv);
330
46900298 331 /* was just updated in ieee80211_bss_info_update */
0c1ad2ca 332 beacon_timestamp = cbss->tsf;
46900298
JB
333
334 /* check if we need to merge IBSS */
335
46900298 336 /* we use a fixed BSSID */
a98bfec2 337 if (sdata->u.ibss.fixed_bssid)
46900298
JB
338 goto put_bss;
339
340 /* not an IBSS */
0c1ad2ca 341 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
46900298
JB
342 goto put_bss;
343
344 /* different channel */
0c1ad2ca 345 if (cbss->channel != local->oper_channel)
46900298
JB
346 goto put_bss;
347
348 /* different SSID */
349 if (elems->ssid_len != sdata->u.ibss.ssid_len ||
350 memcmp(elems->ssid, sdata->u.ibss.ssid,
351 sdata->u.ibss.ssid_len))
352 goto put_bss;
353
34e8f082 354 /* same BSSID */
0c1ad2ca 355 if (memcmp(cbss->bssid, sdata->u.ibss.bssid, ETH_ALEN) == 0)
34e8f082
AF
356 goto put_bss;
357
6ebacbb7 358 if (rx_status->flag & RX_FLAG_MACTIME_MPDU) {
46900298
JB
359 /*
360 * For correct IBSS merging we need mactime; since mactime is
361 * defined as the time the first data symbol of the frame hits
362 * the PHY, and the timestamp of the beacon is defined as "the
363 * time that the data symbol containing the first bit of the
364 * timestamp is transmitted to the PHY plus the transmitting
365 * STA's delays through its local PHY from the MAC-PHY
366 * interface to its interface with the WM" (802.11 11.1.2)
367 * - equals the time this bit arrives at the receiver - we have
368 * to take into account the offset between the two.
369 *
370 * E.g. at 1 MBit that means mactime is 192 usec earlier
371 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
372 */
373 int rate;
374
375 if (rx_status->flag & RX_FLAG_HT)
376 rate = 65; /* TODO: HT rates */
377 else
378 rate = local->hw.wiphy->bands[band]->
379 bitrates[rx_status->rate_idx].bitrate;
380
381 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
24487981
JB
382 } else {
383 /*
384 * second best option: get current TSF
385 * (will return -1 if not supported)
386 */
37a41b4a 387 rx_timestamp = drv_get_tsf(local, sdata);
24487981 388 }
46900298
JB
389
390#ifdef CONFIG_MAC80211_IBSS_DEBUG
391 printk(KERN_DEBUG "RX beacon SA=%pM BSSID="
392 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
393 mgmt->sa, mgmt->bssid,
394 (unsigned long long)rx_timestamp,
395 (unsigned long long)beacon_timestamp,
396 (unsigned long long)(rx_timestamp - beacon_timestamp),
397 jiffies);
398#endif
399
400 if (beacon_timestamp > rx_timestamp) {
401#ifdef CONFIG_MAC80211_IBSS_DEBUG
402 printk(KERN_DEBUG "%s: beacon TSF higher than "
403 "local TSF - IBSS merge with BSSID %pM\n",
47846c9b 404 sdata->name, mgmt->bssid);
46900298
JB
405#endif
406 ieee80211_sta_join_ibss(sdata, bss);
09a08cff 407 supp_rates = ieee80211_sta_get_rates(local, elems, band);
34e89507
JB
408 ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa,
409 supp_rates, GFP_KERNEL);
46900298
JB
410 }
411
412 put_bss:
413 ieee80211_rx_bss_put(local, bss);
414}
415
416/*
417 * Add a new IBSS station, will also be called by the RX code when,
418 * in IBSS mode, receiving a frame from a yet-unknown station, hence
419 * must be callable in atomic context.
420 */
421struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
a7ce1c94 422 u8 *bssid, u8 *addr, u32 supp_rates,
34e89507 423 gfp_t gfp)
46900298 424{
2e10d330 425 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298
JB
426 struct ieee80211_local *local = sdata->local;
427 struct sta_info *sta;
428 int band = local->hw.conf.channel->band;
429
af8cdcd8
JB
430 /*
431 * XXX: Consider removing the least recently used entry and
432 * allow new one to be added.
433 */
46900298 434 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
af8cdcd8
JB
435 if (net_ratelimit())
436 printk(KERN_DEBUG "%s: No room for a new IBSS STA entry %pM\n",
47846c9b 437 sdata->name, addr);
46900298
JB
438 return NULL;
439 }
440
2e10d330
FF
441 if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH)
442 return NULL;
443
46900298
JB
444 if (compare_ether_addr(bssid, sdata->u.ibss.bssid))
445 return NULL;
446
447#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0fb9a9ec
JP
448 wiphy_debug(local->hw.wiphy, "Adding new IBSS station %pM (dev=%s)\n",
449 addr, sdata->name);
46900298
JB
450#endif
451
34e89507 452 sta = sta_info_alloc(sdata, addr, gfp);
46900298
JB
453 if (!sta)
454 return NULL;
455
c8716d9d 456 sta->last_rx = jiffies;
c2c98fde 457 set_sta_flag(sta, WLAN_STA_AUTHORIZED);
46900298
JB
458
459 /* make sure mandatory rates are always added */
460 sta->sta.supp_rates[band] = supp_rates |
461 ieee80211_mandatory_rates(local, band);
462
463 rate_control_rate_init(sta);
464
34e89507 465 /* If it fails, maybe we raced another insertion? */
46900298 466 if (sta_info_insert(sta))
34e89507 467 return sta_info_get(sdata, addr);
46900298
JB
468 return sta;
469}
470
471static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
472{
473 struct ieee80211_local *local = sdata->local;
474 int active = 0;
475 struct sta_info *sta;
476
7a17a33c
JB
477 lockdep_assert_held(&sdata->u.ibss.mtx);
478
46900298
JB
479 rcu_read_lock();
480
481 list_for_each_entry_rcu(sta, &local->sta_list, list) {
482 if (sta->sdata == sdata &&
483 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
484 jiffies)) {
485 active++;
486 break;
487 }
488 }
489
490 rcu_read_unlock();
491
492 return active;
493}
494
ce9058ae
BP
495/*
496 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
497 */
46900298
JB
498
499static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
500{
501 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
502
7a17a33c
JB
503 lockdep_assert_held(&ifibss->mtx);
504
af8cdcd8
JB
505 mod_timer(&ifibss->timer,
506 round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
46900298
JB
507
508 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
af8cdcd8 509
450aae3d
S
510 if (time_before(jiffies, ifibss->last_scan_completed +
511 IEEE80211_IBSS_MERGE_INTERVAL))
512 return;
513
46900298
JB
514 if (ieee80211_sta_active_ibss(sdata))
515 return;
516
af8cdcd8 517 if (ifibss->fixed_channel)
46900298
JB
518 return;
519
520 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
47846c9b 521 "IBSS networks with same SSID (merge)\n", sdata->name);
46900298 522
be4a4b6a
JB
523 ieee80211_request_internal_scan(sdata,
524 ifibss->ssid, ifibss->ssid_len,
525 ifibss->fixed_channel ? ifibss->channel : NULL);
46900298
JB
526}
527
af8cdcd8 528static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
529{
530 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 531 u8 bssid[ETH_ALEN];
46900298
JB
532 u16 capability;
533 int i;
534
7a17a33c
JB
535 lockdep_assert_held(&ifibss->mtx);
536
af8cdcd8 537 if (ifibss->fixed_bssid) {
46900298
JB
538 memcpy(bssid, ifibss->bssid, ETH_ALEN);
539 } else {
540 /* Generate random, not broadcast, locally administered BSSID. Mix in
541 * own MAC address to make sure that devices that do not have proper
542 * random number generator get different BSSID. */
543 get_random_bytes(bssid, ETH_ALEN);
544 for (i = 0; i < ETH_ALEN; i++)
47846c9b 545 bssid[i] ^= sdata->vif.addr[i];
46900298
JB
546 bssid[0] &= ~0x01;
547 bssid[0] |= 0x02;
548 }
549
550 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %pM\n",
47846c9b 551 sdata->name, bssid);
46900298 552
46900298
JB
553 capability = WLAN_CAPABILITY_IBSS;
554
fffd0934 555 if (ifibss->privacy)
46900298
JB
556 capability |= WLAN_CAPABILITY_PRIVACY;
557 else
558 sdata->drop_unencrypted = 0;
559
57c4d7b4 560 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
fbd2c8dc 561 ifibss->channel, ifibss->basic_rates,
b59066a2 562 capability, 0);
46900298
JB
563}
564
ce9058ae
BP
565/*
566 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
567 */
568
af8cdcd8 569static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
46900298
JB
570{
571 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
572 struct ieee80211_local *local = sdata->local;
0c1ad2ca 573 struct cfg80211_bss *cbss;
af8cdcd8 574 struct ieee80211_channel *chan = NULL;
46900298
JB
575 const u8 *bssid = NULL;
576 int active_ibss;
e0d61887 577 u16 capability;
46900298 578
7a17a33c
JB
579 lockdep_assert_held(&ifibss->mtx);
580
46900298
JB
581 active_ibss = ieee80211_sta_active_ibss(sdata);
582#ifdef CONFIG_MAC80211_IBSS_DEBUG
583 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
47846c9b 584 sdata->name, active_ibss);
46900298
JB
585#endif /* CONFIG_MAC80211_IBSS_DEBUG */
586
587 if (active_ibss)
af8cdcd8 588 return;
46900298 589
e0d61887 590 capability = WLAN_CAPABILITY_IBSS;
fffd0934 591 if (ifibss->privacy)
e0d61887 592 capability |= WLAN_CAPABILITY_PRIVACY;
af8cdcd8
JB
593 if (ifibss->fixed_bssid)
594 bssid = ifibss->bssid;
595 if (ifibss->fixed_channel)
596 chan = ifibss->channel;
597 if (!is_zero_ether_addr(ifibss->bssid))
46900298 598 bssid = ifibss->bssid;
0c1ad2ca
JB
599 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
600 ifibss->ssid, ifibss->ssid_len,
601 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY,
602 capability);
603
604 if (cbss) {
605 struct ieee80211_bss *bss;
46900298 606
0c1ad2ca 607 bss = (void *)cbss->priv;
46900298 608#ifdef CONFIG_MAC80211_IBSS_DEBUG
46900298 609 printk(KERN_DEBUG " sta_find_ibss: selected %pM current "
0c1ad2ca 610 "%pM\n", cbss->bssid, ifibss->bssid);
46900298
JB
611#endif /* CONFIG_MAC80211_IBSS_DEBUG */
612
46900298
JB
613 printk(KERN_DEBUG "%s: Selected IBSS BSSID %pM"
614 " based on configured SSID\n",
0c1ad2ca 615 sdata->name, cbss->bssid);
46900298 616
af8cdcd8 617 ieee80211_sta_join_ibss(sdata, bss);
46900298 618 ieee80211_rx_bss_put(local, bss);
af8cdcd8 619 return;
d419b9f0 620 }
46900298
JB
621
622#ifdef CONFIG_MAC80211_IBSS_DEBUG
623 printk(KERN_DEBUG " did not try to join ibss\n");
624#endif /* CONFIG_MAC80211_IBSS_DEBUG */
625
626 /* Selected IBSS not found in current scan results - try to scan */
ce9058ae 627 if (time_after(jiffies, ifibss->last_scan_completed +
46900298
JB
628 IEEE80211_SCAN_INTERVAL)) {
629 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
47846c9b 630 "join\n", sdata->name);
46900298 631
be4a4b6a
JB
632 ieee80211_request_internal_scan(sdata,
633 ifibss->ssid, ifibss->ssid_len,
634 ifibss->fixed_channel ? ifibss->channel : NULL);
ce9058ae 635 } else {
46900298
JB
636 int interval = IEEE80211_SCAN_INTERVAL;
637
638 if (time_after(jiffies, ifibss->ibss_join_req +
639 IEEE80211_IBSS_JOIN_TIMEOUT)) {
af8cdcd8
JB
640 if (!(local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS)) {
641 ieee80211_sta_create_ibss(sdata);
642 return;
643 }
46900298 644 printk(KERN_DEBUG "%s: IBSS not allowed on"
47846c9b 645 " %d MHz\n", sdata->name,
46900298
JB
646 local->hw.conf.channel->center_freq);
647
648 /* No IBSS found - decrease scan interval and continue
649 * scanning. */
650 interval = IEEE80211_SCAN_INTERVAL_SLOW;
651 }
652
af8cdcd8
JB
653 mod_timer(&ifibss->timer,
654 round_jiffies(jiffies + interval));
46900298 655 }
46900298
JB
656}
657
658static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
c269a203 659 struct sk_buff *req)
46900298 660{
c269a203 661 struct ieee80211_mgmt *mgmt = (void *)req->data;
46900298
JB
662 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
663 struct ieee80211_local *local = sdata->local;
c269a203 664 int tx_last_beacon, len = req->len;
46900298
JB
665 struct sk_buff *skb;
666 struct ieee80211_mgmt *resp;
40b275b6 667 struct sk_buff *presp;
46900298
JB
668 u8 *pos, *end;
669
7a17a33c
JB
670 lockdep_assert_held(&ifibss->mtx);
671
40b275b6
JB
672 presp = rcu_dereference_protected(ifibss->presp,
673 lockdep_is_held(&ifibss->mtx));
674
46900298 675 if (ifibss->state != IEEE80211_IBSS_MLME_JOINED ||
40b275b6 676 len < 24 + 2 || !presp)
46900298
JB
677 return;
678
24487981 679 tx_last_beacon = drv_tx_last_beacon(local);
46900298
JB
680
681#ifdef CONFIG_MAC80211_IBSS_DEBUG
682 printk(KERN_DEBUG "%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
683 " (tx_last_beacon=%d)\n",
47846c9b 684 sdata->name, mgmt->sa, mgmt->da,
46900298
JB
685 mgmt->bssid, tx_last_beacon);
686#endif /* CONFIG_MAC80211_IBSS_DEBUG */
687
1ed76487 688 if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da))
46900298
JB
689 return;
690
691 if (memcmp(mgmt->bssid, ifibss->bssid, ETH_ALEN) != 0 &&
692 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
693 return;
694
695 end = ((u8 *) mgmt) + len;
696 pos = mgmt->u.probe_req.variable;
697 if (pos[0] != WLAN_EID_SSID ||
698 pos + 2 + pos[1] > end) {
699#ifdef CONFIG_MAC80211_IBSS_DEBUG
700 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
701 "from %pM\n",
47846c9b 702 sdata->name, mgmt->sa);
46900298
JB
703#endif
704 return;
705 }
706 if (pos[1] != 0 &&
707 (pos[1] != ifibss->ssid_len ||
0da780c2 708 memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) {
46900298
JB
709 /* Ignore ProbeReq for foreign SSID */
710 return;
711 }
712
713 /* Reply with ProbeResp */
40b275b6 714 skb = skb_copy(presp, GFP_KERNEL);
46900298
JB
715 if (!skb)
716 return;
717
718 resp = (struct ieee80211_mgmt *) skb->data;
719 memcpy(resp->da, mgmt->sa, ETH_ALEN);
720#ifdef CONFIG_MAC80211_IBSS_DEBUG
721 printk(KERN_DEBUG "%s: Sending ProbeResp to %pM\n",
47846c9b 722 sdata->name, resp->da);
46900298 723#endif /* CONFIG_MAC80211_IBSS_DEBUG */
62ae67be
JB
724 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
725 ieee80211_tx_skb(sdata, skb);
46900298
JB
726}
727
728static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
729 struct ieee80211_mgmt *mgmt,
730 size_t len,
731 struct ieee80211_rx_status *rx_status)
732{
733 size_t baselen;
734 struct ieee802_11_elems elems;
735
47846c9b 736 if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
46900298
JB
737 return; /* ignore ProbeResp to foreign address */
738
739 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
740 if (baselen > len)
741 return;
742
743 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
744 &elems);
745
746 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
747}
748
749static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
750 struct ieee80211_mgmt *mgmt,
751 size_t len,
752 struct ieee80211_rx_status *rx_status)
753{
754 size_t baselen;
755 struct ieee802_11_elems elems;
756
757 /* Process beacon from the current BSS */
758 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
759 if (baselen > len)
760 return;
761
762 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
763
764 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
765}
766
1fa57d01
JB
767void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
768 struct sk_buff *skb)
46900298
JB
769{
770 struct ieee80211_rx_status *rx_status;
771 struct ieee80211_mgmt *mgmt;
772 u16 fc;
773
f1d58c25 774 rx_status = IEEE80211_SKB_RXCB(skb);
46900298
JB
775 mgmt = (struct ieee80211_mgmt *) skb->data;
776 fc = le16_to_cpu(mgmt->frame_control);
777
7a17a33c
JB
778 mutex_lock(&sdata->u.ibss.mtx);
779
c926d006
TH
780 if (!sdata->u.ibss.ssid_len)
781 goto mgmt_out; /* not ready to merge yet */
782
46900298
JB
783 switch (fc & IEEE80211_FCTL_STYPE) {
784 case IEEE80211_STYPE_PROBE_REQ:
c269a203 785 ieee80211_rx_mgmt_probe_req(sdata, skb);
46900298
JB
786 break;
787 case IEEE80211_STYPE_PROBE_RESP:
788 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
789 rx_status);
790 break;
791 case IEEE80211_STYPE_BEACON:
792 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
793 rx_status);
794 break;
795 case IEEE80211_STYPE_AUTH:
796 ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len);
797 break;
798 }
7a17a33c 799
c926d006 800 mgmt_out:
7a17a33c 801 mutex_unlock(&sdata->u.ibss.mtx);
46900298
JB
802}
803
1fa57d01 804void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata)
46900298 805{
1fa57d01 806 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
46900298 807
7a17a33c
JB
808 mutex_lock(&ifibss->mtx);
809
810 /*
811 * Work could be scheduled after scan or similar
812 * when we aren't even joined (or trying) with a
813 * network.
814 */
815 if (!ifibss->ssid_len)
816 goto out;
46900298
JB
817
818 switch (ifibss->state) {
819 case IEEE80211_IBSS_MLME_SEARCH:
820 ieee80211_sta_find_ibss(sdata);
821 break;
822 case IEEE80211_IBSS_MLME_JOINED:
823 ieee80211_sta_merge_ibss(sdata);
824 break;
825 default:
826 WARN_ON(1);
827 break;
828 }
46900298 829
7a17a33c
JB
830 out:
831 mutex_unlock(&ifibss->mtx);
3a4d4aa2
JB
832}
833
46900298
JB
834static void ieee80211_ibss_timer(unsigned long data)
835{
836 struct ieee80211_sub_if_data *sdata =
837 (struct ieee80211_sub_if_data *) data;
838 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
839 struct ieee80211_local *local = sdata->local;
840
5bb644a0
JB
841 if (local->quiescing) {
842 ifibss->timer_running = true;
843 return;
844 }
845
7a17a33c 846 ieee80211_queue_work(&local->hw, &sdata->work);
46900298
JB
847}
848
5bb644a0
JB
849#ifdef CONFIG_PM
850void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data *sdata)
851{
852 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
853
5bb644a0
JB
854 if (del_timer_sync(&ifibss->timer))
855 ifibss->timer_running = true;
856}
857
858void ieee80211_ibss_restart(struct ieee80211_sub_if_data *sdata)
859{
860 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
861
862 if (ifibss->timer_running) {
863 add_timer(&ifibss->timer);
864 ifibss->timer_running = false;
865 }
866}
867#endif
868
46900298
JB
869void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
870{
871 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
872
46900298
JB
873 setup_timer(&ifibss->timer, ieee80211_ibss_timer,
874 (unsigned long) sdata);
7a17a33c 875 mutex_init(&ifibss->mtx);
46900298
JB
876}
877
878/* scan finished notification */
879void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local)
880{
af8cdcd8 881 struct ieee80211_sub_if_data *sdata;
46900298 882
29b4a4f7
JB
883 mutex_lock(&local->iflist_mtx);
884 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 885 if (!ieee80211_sdata_running(sdata))
0e41f715 886 continue;
af8cdcd8
JB
887 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
888 continue;
889 sdata->u.ibss.last_scan_completed = jiffies;
7a17a33c 890 ieee80211_queue_work(&local->hw, &sdata->work);
46900298 891 }
29b4a4f7 892 mutex_unlock(&local->iflist_mtx);
46900298
JB
893}
894
af8cdcd8
JB
895int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
896 struct cfg80211_ibss_params *params)
897{
898 struct sk_buff *skb;
ff3cc5f4 899 u32 changed = 0;
af8cdcd8 900
7a17a33c
JB
901 skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom +
902 36 /* bitrates */ +
903 34 /* SSID */ +
904 3 /* DS params */ +
905 4 /* IBSS params */ +
906 params->ie_len);
907 if (!skb)
908 return -ENOMEM;
909
910 mutex_lock(&sdata->u.ibss.mtx);
911
af8cdcd8
JB
912 if (params->bssid) {
913 memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN);
914 sdata->u.ibss.fixed_bssid = true;
915 } else
916 sdata->u.ibss.fixed_bssid = false;
917
fffd0934 918 sdata->u.ibss.privacy = params->privacy;
fbd2c8dc 919 sdata->u.ibss.basic_rates = params->basic_rates;
dd5b4cc7
FF
920 memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate,
921 sizeof(params->mcast_rate));
fffd0934 922
57c4d7b4
JB
923 sdata->vif.bss_conf.beacon_int = params->beacon_interval;
924
af8cdcd8
JB
925 sdata->u.ibss.channel = params->channel;
926 sdata->u.ibss.fixed_channel = params->channel_fixed;
927
adfba3c7
JB
928 /* fix ourselves to that channel now already */
929 if (params->channel_fixed) {
930 sdata->local->oper_channel = params->channel;
0aaffa9b
JB
931 WARN_ON(!ieee80211_set_channel_type(sdata->local, sdata,
932 NL80211_CHAN_NO_HT));
adfba3c7
JB
933 }
934
af8cdcd8
JB
935 if (params->ie) {
936 sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len,
937 GFP_KERNEL);
938 if (sdata->u.ibss.ie)
939 sdata->u.ibss.ie_len = params->ie_len;
940 }
941
af8cdcd8
JB
942 sdata->u.ibss.skb = skb;
943 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
944 sdata->u.ibss.ibss_join_req = jiffies;
945
0e41f715 946 memcpy(sdata->u.ibss.ssid, params->ssid, IEEE80211_MAX_SSID_LEN);
0e41f715
JB
947 sdata->u.ibss.ssid_len = params->ssid_len;
948
7da7cc1d
JB
949 mutex_unlock(&sdata->u.ibss.mtx);
950
951 mutex_lock(&sdata->local->mtx);
5cff20e6 952 ieee80211_recalc_idle(sdata->local);
7da7cc1d 953 mutex_unlock(&sdata->local->mtx);
5cff20e6 954
ff3cc5f4
SW
955 /*
956 * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is
957 * reserved, but an HT STA shall protect HT transmissions as though
958 * the HT Protection field were set to non-HT mixed mode.
959 *
960 * In an IBSS, the RIFS Mode field of the HT Operation element is
961 * also reserved, but an HT STA shall operate as though this field
962 * were set to 1.
963 */
964
965 sdata->vif.bss_conf.ht_operation_mode |=
966 IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
967 | IEEE80211_HT_PARAM_RIFS_MODE;
968
969 changed |= BSS_CHANGED_HT;
970 ieee80211_bss_info_change_notify(sdata, changed);
971
64592c8f 972 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
af8cdcd8
JB
973
974 return 0;
975}
976
977int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
978{
979 struct sk_buff *skb;
5ea096c0
TP
980 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
981 struct ieee80211_local *local = sdata->local;
982 struct cfg80211_bss *cbss;
983 u16 capability;
7a17a33c
JB
984 int active_ibss;
985
986 mutex_lock(&sdata->u.ibss.mtx);
5ea096c0 987
f3209bea
JB
988 sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH;
989 memset(sdata->u.ibss.bssid, 0, ETH_ALEN);
990 sdata->u.ibss.ssid_len = 0;
991
5ea096c0
TP
992 active_ibss = ieee80211_sta_active_ibss(sdata);
993
994 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
995 capability = WLAN_CAPABILITY_IBSS;
996
997 if (ifibss->privacy)
998 capability |= WLAN_CAPABILITY_PRIVACY;
999
1000 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->channel,
1001 ifibss->bssid, ifibss->ssid,
1002 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1003 WLAN_CAPABILITY_PRIVACY,
1004 capability);
1005
1006 if (cbss) {
1007 cfg80211_unlink_bss(local->hw.wiphy, cbss);
1008 cfg80211_put_bss(cbss);
1009 }
1010 }
af8cdcd8 1011
af8cdcd8 1012 sta_info_flush(sdata->local, sdata);
86a2ea41 1013 netif_carrier_off(sdata->dev);
af8cdcd8
JB
1014
1015 /* remove beacon */
1016 kfree(sdata->u.ibss.ie);
40b275b6
JB
1017 skb = rcu_dereference_protected(sdata->u.ibss.presp,
1018 lockdep_is_held(&sdata->u.ibss.mtx));
a9b3cd7f 1019 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
8fc214ba
JB
1020 sdata->vif.bss_conf.ibss_joined = false;
1021 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1022 BSS_CHANGED_IBSS);
af8cdcd8
JB
1023 synchronize_rcu();
1024 kfree_skb(skb);
1025
35f20c14 1026 skb_queue_purge(&sdata->skb_queue);
5cff20e6 1027
bc05d19f 1028 del_timer_sync(&sdata->u.ibss.timer);
7a17a33c
JB
1029
1030 mutex_unlock(&sdata->u.ibss.mtx);
bc05d19f 1031
7da7cc1d 1032 mutex_lock(&local->mtx);
5cff20e6 1033 ieee80211_recalc_idle(sdata->local);
7da7cc1d 1034 mutex_unlock(&local->mtx);
af8cdcd8
JB
1035
1036 return 0;
1037}