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