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>
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.
15 #include <linux/delay.h>
16 #include <linux/slab.h>
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>
25 #include "ieee80211_i.h"
26 #include "driver-ops.h"
29 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
30 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
31 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
33 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
34 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
36 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
39 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data
*sdata
,
40 struct ieee80211_mgmt
*mgmt
,
43 u16 auth_alg
, auth_transaction
, status_code
;
45 lockdep_assert_held(&sdata
->u
.ibss
.mtx
);
50 auth_alg
= le16_to_cpu(mgmt
->u
.auth
.auth_alg
);
51 auth_transaction
= le16_to_cpu(mgmt
->u
.auth
.auth_transaction
);
52 status_code
= le16_to_cpu(mgmt
->u
.auth
.status_code
);
55 * IEEE 802.11 standard does not require authentication in IBSS
56 * networks and most implementations do not seem to use it.
57 * However, try to reply to authentication attempts if someone
58 * has actually implemented this.
60 if (auth_alg
== WLAN_AUTH_OPEN
&& auth_transaction
== 1)
61 ieee80211_send_auth(sdata
, 2, WLAN_AUTH_OPEN
, NULL
, 0,
62 sdata
->u
.ibss
.bssid
, NULL
, 0, 0);
65 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data
*sdata
,
66 const u8
*bssid
, const int beacon_int
,
67 struct ieee80211_channel
*chan
,
68 const u32 basic_rates
,
69 const u16 capability
, u64 tsf
)
71 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
72 struct ieee80211_local
*local
= sdata
->local
;
75 struct ieee80211_mgmt
*mgmt
;
77 struct ieee80211_supported_band
*sband
;
78 struct cfg80211_bss
*bss
;
80 u8 supp_rates
[IEEE80211_MAX_SUPP_RATES
];
82 lockdep_assert_held(&ifibss
->mtx
);
84 /* Reset own TSF to allow time synchronization work. */
88 rcu_assign_pointer(ifibss
->presp
, NULL
);
90 skb
->data
= skb
->head
;
92 skb_reset_tail_pointer(skb
);
93 skb_reserve(skb
, sdata
->local
->hw
.extra_tx_headroom
);
95 if (memcmp(ifibss
->bssid
, bssid
, ETH_ALEN
))
96 sta_info_flush(sdata
->local
, sdata
);
98 /* if merging, indicate to driver that we leave the old IBSS */
99 if (sdata
->vif
.bss_conf
.ibss_joined
) {
100 sdata
->vif
.bss_conf
.ibss_joined
= false;
101 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_IBSS
);
104 memcpy(ifibss
->bssid
, bssid
, ETH_ALEN
);
106 sdata
->drop_unencrypted
= capability
& WLAN_CAPABILITY_PRIVACY
? 1 : 0;
108 local
->oper_channel
= chan
;
109 WARN_ON(!ieee80211_set_channel_type(local
, sdata
, NL80211_CHAN_NO_HT
));
110 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
112 sband
= local
->hw
.wiphy
->bands
[chan
->band
];
114 /* build supported rates array */
116 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
117 int rate
= sband
->bitrates
[i
].bitrate
;
119 if (basic_rates
& BIT(i
))
121 *pos
++ = basic
| (u8
) (rate
/ 5);
124 /* Build IBSS probe response */
125 mgmt
= (void *) skb_put(skb
, 24 + sizeof(mgmt
->u
.beacon
));
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
);
130 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
131 memcpy(mgmt
->bssid
, ifibss
->bssid
, ETH_ALEN
);
132 mgmt
->u
.beacon
.beacon_int
= cpu_to_le16(beacon_int
);
133 mgmt
->u
.beacon
.timestamp
= cpu_to_le64(tsf
);
134 mgmt
->u
.beacon
.capab_info
= cpu_to_le16(capability
);
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
);
141 rates
= sband
->n_bitrates
;
144 pos
= skb_put(skb
, 2 + rates
);
145 *pos
++ = WLAN_EID_SUPP_RATES
;
147 memcpy(pos
, supp_rates
, rates
);
149 if (sband
->band
== IEEE80211_BAND_2GHZ
) {
150 pos
= skb_put(skb
, 2 + 1);
151 *pos
++ = WLAN_EID_DS_PARAMS
;
153 *pos
++ = ieee80211_frequency_to_channel(chan
->center_freq
);
156 pos
= skb_put(skb
, 2 + 2);
157 *pos
++ = WLAN_EID_IBSS_PARAMS
;
159 /* FIX: set ATIM window based on scan results */
163 if (sband
->n_bitrates
> 8) {
164 rates
= sband
->n_bitrates
- 8;
165 pos
= skb_put(skb
, 2 + rates
);
166 *pos
++ = WLAN_EID_EXT_SUPP_RATES
;
168 memcpy(pos
, &supp_rates
[8], rates
);
172 memcpy(skb_put(skb
, ifibss
->ie_len
),
173 ifibss
->ie
, ifibss
->ie_len
);
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 */
182 *pos
++ = 2; /* WME */
183 *pos
++ = 0; /* WME info */
184 *pos
++ = 1; /* WME ver */
185 *pos
++ = 0; /* U-APSD no in use */
188 rcu_assign_pointer(ifibss
->presp
, skb
);
190 sdata
->vif
.bss_conf
.beacon_int
= beacon_int
;
191 sdata
->vif
.bss_conf
.basic_rates
= basic_rates
;
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
;
197 bss_change
|= BSS_CHANGED_BASIC_RATES
;
198 bss_change
|= BSS_CHANGED_IBSS
;
199 sdata
->vif
.bss_conf
.ibss_joined
= true;
200 ieee80211_bss_info_change_notify(sdata
, bss_change
);
202 ieee80211_sta_def_wmm_params(sdata
, sband
->n_bitrates
, supp_rates
);
204 ifibss
->state
= IEEE80211_IBSS_MLME_JOINED
;
205 mod_timer(&ifibss
->timer
,
206 round_jiffies(jiffies
+ IEEE80211_IBSS_MERGE_INTERVAL
));
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
);
211 cfg80211_ibss_joined(sdata
->dev
, ifibss
->bssid
, GFP_KERNEL
);
214 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data
*sdata
,
215 struct ieee80211_bss
*bss
)
217 struct cfg80211_bss
*cbss
=
218 container_of((void *)bss
, struct cfg80211_bss
, priv
);
219 struct ieee80211_supported_band
*sband
;
222 u16 beacon_int
= cbss
->beacon_interval
;
224 lockdep_assert_held(&sdata
->u
.ibss
.mtx
);
229 sband
= sdata
->local
->hw
.wiphy
->bands
[cbss
->channel
->band
];
233 for (i
= 0; i
< bss
->supp_rates_len
; i
++) {
234 int rate
= (bss
->supp_rates
[i
] & 0x7f) * 5;
235 bool is_basic
= !!(bss
->supp_rates
[i
] & 0x80);
237 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
238 if (sband
->bitrates
[j
].bitrate
== rate
) {
240 basic_rates
|= BIT(j
);
246 __ieee80211_sta_join_ibss(sdata
, cbss
->bssid
,
254 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data
*sdata
,
255 struct ieee80211_mgmt
*mgmt
,
257 struct ieee80211_rx_status
*rx_status
,
258 struct ieee802_11_elems
*elems
,
261 struct ieee80211_local
*local
= sdata
->local
;
263 struct cfg80211_bss
*cbss
;
264 struct ieee80211_bss
*bss
;
265 struct sta_info
*sta
;
266 struct ieee80211_channel
*channel
;
267 u64 beacon_timestamp
, rx_timestamp
;
269 enum ieee80211_band band
= rx_status
->band
;
271 if (elems
->ds_params
&& elems
->ds_params_len
== 1)
272 freq
= ieee80211_channel_to_frequency(elems
->ds_params
[0],
275 freq
= rx_status
->freq
;
277 channel
= ieee80211_get_channel(local
->hw
.wiphy
, freq
);
279 if (!channel
|| channel
->flags
& IEEE80211_CHAN_DISABLED
)
282 if (sdata
->vif
.type
== NL80211_IFTYPE_ADHOC
&&
283 memcmp(mgmt
->bssid
, sdata
->u
.ibss
.bssid
, ETH_ALEN
) == 0) {
286 sta
= sta_info_get(sdata
, mgmt
->sa
);
288 if (elems
->supp_rates
) {
289 supp_rates
= ieee80211_sta_get_rates(local
, elems
,
294 prev_rates
= sta
->sta
.supp_rates
[band
];
295 /* make sure mandatory rates are always added */
296 sta
->sta
.supp_rates
[band
] = supp_rates
|
297 ieee80211_mandatory_rates(local
, band
);
299 if (sta
->sta
.supp_rates
[band
] != prev_rates
) {
300 #ifdef CONFIG_MAC80211_IBSS_DEBUG
302 "%s: updated supp_rates set "
303 "for %pM based on beacon"
304 "/probe_resp (0x%x -> 0x%x)\n",
305 sdata
->name
, sta
->sta
.addr
,
307 sta
->sta
.supp_rates
[band
]);
309 rate_control_rate_init(sta
);
312 sta
= ieee80211_ibss_add_sta(sdata
, mgmt
->bssid
,
313 mgmt
->sa
, supp_rates
,
317 if (sta
&& elems
->wmm_info
)
318 set_sta_flags(sta
, WLAN_STA_WME
);
323 bss
= ieee80211_bss_info_update(local
, rx_status
, mgmt
, len
, elems
,
328 cbss
= container_of((void *)bss
, struct cfg80211_bss
, priv
);
330 /* was just updated in ieee80211_bss_info_update */
331 beacon_timestamp
= cbss
->tsf
;
333 /* check if we need to merge IBSS */
335 /* we use a fixed BSSID */
336 if (sdata
->u
.ibss
.fixed_bssid
)
340 if (!(cbss
->capability
& WLAN_CAPABILITY_IBSS
))
343 /* different channel */
344 if (cbss
->channel
!= local
->oper_channel
)
348 if (elems
->ssid_len
!= sdata
->u
.ibss
.ssid_len
||
349 memcmp(elems
->ssid
, sdata
->u
.ibss
.ssid
,
350 sdata
->u
.ibss
.ssid_len
))
354 if (memcmp(cbss
->bssid
, sdata
->u
.ibss
.bssid
, ETH_ALEN
) == 0)
357 if (rx_status
->flag
& RX_FLAG_MACTIME_MPDU
) {
359 * For correct IBSS merging we need mactime; since mactime is
360 * defined as the time the first data symbol of the frame hits
361 * the PHY, and the timestamp of the beacon is defined as "the
362 * time that the data symbol containing the first bit of the
363 * timestamp is transmitted to the PHY plus the transmitting
364 * STA's delays through its local PHY from the MAC-PHY
365 * interface to its interface with the WM" (802.11 11.1.2)
366 * - equals the time this bit arrives at the receiver - we have
367 * to take into account the offset between the two.
369 * E.g. at 1 MBit that means mactime is 192 usec earlier
370 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
374 if (rx_status
->flag
& RX_FLAG_HT
)
375 rate
= 65; /* TODO: HT rates */
377 rate
= local
->hw
.wiphy
->bands
[band
]->
378 bitrates
[rx_status
->rate_idx
].bitrate
;
380 rx_timestamp
= rx_status
->mactime
+ (24 * 8 * 10 / rate
);
383 * second best option: get current TSF
384 * (will return -1 if not supported)
386 rx_timestamp
= drv_get_tsf(local
);
389 #ifdef CONFIG_MAC80211_IBSS_DEBUG
390 printk(KERN_DEBUG
"RX beacon SA=%pM BSSID="
391 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
392 mgmt
->sa
, mgmt
->bssid
,
393 (unsigned long long)rx_timestamp
,
394 (unsigned long long)beacon_timestamp
,
395 (unsigned long long)(rx_timestamp
- beacon_timestamp
),
399 if (beacon_timestamp
> rx_timestamp
) {
400 #ifdef CONFIG_MAC80211_IBSS_DEBUG
401 printk(KERN_DEBUG
"%s: beacon TSF higher than "
402 "local TSF - IBSS merge with BSSID %pM\n",
403 sdata
->name
, mgmt
->bssid
);
405 ieee80211_sta_join_ibss(sdata
, bss
);
406 supp_rates
= ieee80211_sta_get_rates(local
, elems
, band
);
407 ieee80211_ibss_add_sta(sdata
, mgmt
->bssid
, mgmt
->sa
,
408 supp_rates
, GFP_KERNEL
);
412 ieee80211_rx_bss_put(local
, bss
);
416 * Add a new IBSS station, will also be called by the RX code when,
417 * in IBSS mode, receiving a frame from a yet-unknown station, hence
418 * must be callable in atomic context.
420 struct sta_info
*ieee80211_ibss_add_sta(struct ieee80211_sub_if_data
*sdata
,
421 u8
*bssid
,u8
*addr
, u32 supp_rates
,
424 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
425 struct ieee80211_local
*local
= sdata
->local
;
426 struct sta_info
*sta
;
427 int band
= local
->hw
.conf
.channel
->band
;
430 * XXX: Consider removing the least recently used entry and
431 * allow new one to be added.
433 if (local
->num_sta
>= IEEE80211_IBSS_MAX_STA_ENTRIES
) {
435 printk(KERN_DEBUG
"%s: No room for a new IBSS STA entry %pM\n",
440 if (ifibss
->state
== IEEE80211_IBSS_MLME_SEARCH
)
443 if (compare_ether_addr(bssid
, sdata
->u
.ibss
.bssid
))
446 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
447 wiphy_debug(local
->hw
.wiphy
, "Adding new IBSS station %pM (dev=%s)\n",
451 sta
= sta_info_alloc(sdata
, addr
, gfp
);
455 sta
->last_rx
= jiffies
;
456 set_sta_flags(sta
, WLAN_STA_AUTHORIZED
);
458 /* make sure mandatory rates are always added */
459 sta
->sta
.supp_rates
[band
] = supp_rates
|
460 ieee80211_mandatory_rates(local
, band
);
462 rate_control_rate_init(sta
);
464 /* If it fails, maybe we raced another insertion? */
465 if (sta_info_insert(sta
))
466 return sta_info_get(sdata
, addr
);
470 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data
*sdata
)
472 struct ieee80211_local
*local
= sdata
->local
;
474 struct sta_info
*sta
;
476 lockdep_assert_held(&sdata
->u
.ibss
.mtx
);
480 list_for_each_entry_rcu(sta
, &local
->sta_list
, list
) {
481 if (sta
->sdata
== sdata
&&
482 time_after(sta
->last_rx
+ IEEE80211_IBSS_MERGE_INTERVAL
,
495 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
498 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data
*sdata
)
500 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
502 lockdep_assert_held(&ifibss
->mtx
);
504 mod_timer(&ifibss
->timer
,
505 round_jiffies(jiffies
+ IEEE80211_IBSS_MERGE_INTERVAL
));
507 ieee80211_sta_expire(sdata
, IEEE80211_IBSS_INACTIVITY_LIMIT
);
509 if (time_before(jiffies
, ifibss
->last_scan_completed
+
510 IEEE80211_IBSS_MERGE_INTERVAL
))
513 if (ieee80211_sta_active_ibss(sdata
))
516 if (ifibss
->fixed_channel
)
519 printk(KERN_DEBUG
"%s: No active IBSS STAs - trying to scan for other "
520 "IBSS networks with same SSID (merge)\n", sdata
->name
);
522 ieee80211_request_internal_scan(sdata
,
523 ifibss
->ssid
, ifibss
->ssid_len
,
524 ifibss
->fixed_channel
? ifibss
->channel
: NULL
);
527 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data
*sdata
)
529 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
530 struct ieee80211_local
*local
= sdata
->local
;
531 struct ieee80211_supported_band
*sband
;
536 lockdep_assert_held(&ifibss
->mtx
);
538 if (ifibss
->fixed_bssid
) {
539 memcpy(bssid
, ifibss
->bssid
, ETH_ALEN
);
541 /* Generate random, not broadcast, locally administered BSSID. Mix in
542 * own MAC address to make sure that devices that do not have proper
543 * random number generator get different BSSID. */
544 get_random_bytes(bssid
, ETH_ALEN
);
545 for (i
= 0; i
< ETH_ALEN
; i
++)
546 bssid
[i
] ^= sdata
->vif
.addr
[i
];
551 printk(KERN_DEBUG
"%s: Creating new IBSS network, BSSID %pM\n",
554 sband
= local
->hw
.wiphy
->bands
[ifibss
->channel
->band
];
556 capability
= WLAN_CAPABILITY_IBSS
;
559 capability
|= WLAN_CAPABILITY_PRIVACY
;
561 sdata
->drop_unencrypted
= 0;
563 __ieee80211_sta_join_ibss(sdata
, bssid
, sdata
->vif
.bss_conf
.beacon_int
,
564 ifibss
->channel
, ifibss
->basic_rates
,
569 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
572 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data
*sdata
)
574 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
575 struct ieee80211_local
*local
= sdata
->local
;
576 struct cfg80211_bss
*cbss
;
577 struct ieee80211_channel
*chan
= NULL
;
578 const u8
*bssid
= NULL
;
582 lockdep_assert_held(&ifibss
->mtx
);
584 active_ibss
= ieee80211_sta_active_ibss(sdata
);
585 #ifdef CONFIG_MAC80211_IBSS_DEBUG
586 printk(KERN_DEBUG
"%s: sta_find_ibss (active_ibss=%d)\n",
587 sdata
->name
, active_ibss
);
588 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
593 capability
= WLAN_CAPABILITY_IBSS
;
595 capability
|= WLAN_CAPABILITY_PRIVACY
;
596 if (ifibss
->fixed_bssid
)
597 bssid
= ifibss
->bssid
;
598 if (ifibss
->fixed_channel
)
599 chan
= ifibss
->channel
;
600 if (!is_zero_ether_addr(ifibss
->bssid
))
601 bssid
= ifibss
->bssid
;
602 cbss
= cfg80211_get_bss(local
->hw
.wiphy
, chan
, bssid
,
603 ifibss
->ssid
, ifibss
->ssid_len
,
604 WLAN_CAPABILITY_IBSS
| WLAN_CAPABILITY_PRIVACY
,
608 struct ieee80211_bss
*bss
;
610 bss
= (void *)cbss
->priv
;
611 #ifdef CONFIG_MAC80211_IBSS_DEBUG
612 printk(KERN_DEBUG
" sta_find_ibss: selected %pM current "
613 "%pM\n", cbss
->bssid
, ifibss
->bssid
);
614 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
616 printk(KERN_DEBUG
"%s: Selected IBSS BSSID %pM"
617 " based on configured SSID\n",
618 sdata
->name
, cbss
->bssid
);
620 ieee80211_sta_join_ibss(sdata
, bss
);
621 ieee80211_rx_bss_put(local
, bss
);
625 #ifdef CONFIG_MAC80211_IBSS_DEBUG
626 printk(KERN_DEBUG
" did not try to join ibss\n");
627 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
629 /* Selected IBSS not found in current scan results - try to scan */
630 if (time_after(jiffies
, ifibss
->last_scan_completed
+
631 IEEE80211_SCAN_INTERVAL
)) {
632 printk(KERN_DEBUG
"%s: Trigger new scan to find an IBSS to "
633 "join\n", sdata
->name
);
635 ieee80211_request_internal_scan(sdata
,
636 ifibss
->ssid
, ifibss
->ssid_len
,
637 ifibss
->fixed_channel
? ifibss
->channel
: NULL
);
639 int interval
= IEEE80211_SCAN_INTERVAL
;
641 if (time_after(jiffies
, ifibss
->ibss_join_req
+
642 IEEE80211_IBSS_JOIN_TIMEOUT
)) {
643 if (!(local
->oper_channel
->flags
& IEEE80211_CHAN_NO_IBSS
)) {
644 ieee80211_sta_create_ibss(sdata
);
647 printk(KERN_DEBUG
"%s: IBSS not allowed on"
648 " %d MHz\n", sdata
->name
,
649 local
->hw
.conf
.channel
->center_freq
);
651 /* No IBSS found - decrease scan interval and continue
653 interval
= IEEE80211_SCAN_INTERVAL_SLOW
;
656 mod_timer(&ifibss
->timer
,
657 round_jiffies(jiffies
+ interval
));
661 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data
*sdata
,
664 struct ieee80211_rx_status
*rx_status
= IEEE80211_SKB_RXCB(req
);
665 struct ieee80211_mgmt
*mgmt
= (void *)req
->data
;
666 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
667 struct ieee80211_local
*local
= sdata
->local
;
668 int tx_last_beacon
, len
= req
->len
;
670 struct ieee80211_mgmt
*resp
;
673 lockdep_assert_held(&ifibss
->mtx
);
675 if (ifibss
->state
!= IEEE80211_IBSS_MLME_JOINED
||
676 len
< 24 + 2 || !ifibss
->presp
)
679 tx_last_beacon
= drv_tx_last_beacon(local
);
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",
684 sdata
->name
, mgmt
->sa
, mgmt
->da
,
685 mgmt
->bssid
, tx_last_beacon
);
686 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
688 if (!tx_last_beacon
&& !(rx_status
->rx_flags
& IEEE80211_RX_RA_MATCH
))
691 if (memcmp(mgmt
->bssid
, ifibss
->bssid
, ETH_ALEN
) != 0 &&
692 memcmp(mgmt
->bssid
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
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 "
702 sdata
->name
, mgmt
->sa
);
707 (pos
[1] != ifibss
->ssid_len
||
708 memcmp(pos
+ 2, ifibss
->ssid
, ifibss
->ssid_len
))) {
709 /* Ignore ProbeReq for foreign SSID */
713 /* Reply with ProbeResp */
714 skb
= skb_copy(ifibss
->presp
, GFP_KERNEL
);
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",
722 sdata
->name
, resp
->da
);
723 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
724 IEEE80211_SKB_CB(skb
)->flags
|= IEEE80211_TX_INTFL_DONT_ENCRYPT
;
725 ieee80211_tx_skb(sdata
, skb
);
728 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data
*sdata
,
729 struct ieee80211_mgmt
*mgmt
,
731 struct ieee80211_rx_status
*rx_status
)
734 struct ieee802_11_elems elems
;
736 if (memcmp(mgmt
->da
, sdata
->vif
.addr
, ETH_ALEN
))
737 return; /* ignore ProbeResp to foreign address */
739 baselen
= (u8
*) mgmt
->u
.probe_resp
.variable
- (u8
*) mgmt
;
743 ieee802_11_parse_elems(mgmt
->u
.probe_resp
.variable
, len
- baselen
,
746 ieee80211_rx_bss_info(sdata
, mgmt
, len
, rx_status
, &elems
, false);
749 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data
*sdata
,
750 struct ieee80211_mgmt
*mgmt
,
752 struct ieee80211_rx_status
*rx_status
)
755 struct ieee802_11_elems elems
;
757 /* Process beacon from the current BSS */
758 baselen
= (u8
*) mgmt
->u
.beacon
.variable
- (u8
*) mgmt
;
762 ieee802_11_parse_elems(mgmt
->u
.beacon
.variable
, len
- baselen
, &elems
);
764 ieee80211_rx_bss_info(sdata
, mgmt
, len
, rx_status
, &elems
, true);
767 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data
*sdata
,
770 struct ieee80211_rx_status
*rx_status
;
771 struct ieee80211_mgmt
*mgmt
;
774 rx_status
= IEEE80211_SKB_RXCB(skb
);
775 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
776 fc
= le16_to_cpu(mgmt
->frame_control
);
778 mutex_lock(&sdata
->u
.ibss
.mtx
);
780 if (!sdata
->u
.ibss
.ssid_len
)
781 goto mgmt_out
; /* not ready to merge yet */
783 switch (fc
& IEEE80211_FCTL_STYPE
) {
784 case IEEE80211_STYPE_PROBE_REQ
:
785 ieee80211_rx_mgmt_probe_req(sdata
, skb
);
787 case IEEE80211_STYPE_PROBE_RESP
:
788 ieee80211_rx_mgmt_probe_resp(sdata
, mgmt
, skb
->len
,
791 case IEEE80211_STYPE_BEACON
:
792 ieee80211_rx_mgmt_beacon(sdata
, mgmt
, skb
->len
,
795 case IEEE80211_STYPE_AUTH
:
796 ieee80211_rx_mgmt_auth_ibss(sdata
, mgmt
, skb
->len
);
801 mutex_unlock(&sdata
->u
.ibss
.mtx
);
804 void ieee80211_ibss_work(struct ieee80211_sub_if_data
*sdata
)
806 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
808 mutex_lock(&ifibss
->mtx
);
811 * Work could be scheduled after scan or similar
812 * when we aren't even joined (or trying) with a
815 if (!ifibss
->ssid_len
)
818 switch (ifibss
->state
) {
819 case IEEE80211_IBSS_MLME_SEARCH
:
820 ieee80211_sta_find_ibss(sdata
);
822 case IEEE80211_IBSS_MLME_JOINED
:
823 ieee80211_sta_merge_ibss(sdata
);
831 mutex_unlock(&ifibss
->mtx
);
834 static void ieee80211_ibss_timer(unsigned long data
)
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
;
841 if (local
->quiescing
) {
842 ifibss
->timer_running
= true;
846 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
850 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data
*sdata
)
852 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
854 if (del_timer_sync(&ifibss
->timer
))
855 ifibss
->timer_running
= true;
858 void ieee80211_ibss_restart(struct ieee80211_sub_if_data
*sdata
)
860 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
862 if (ifibss
->timer_running
) {
863 add_timer(&ifibss
->timer
);
864 ifibss
->timer_running
= false;
869 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data
*sdata
)
871 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
873 setup_timer(&ifibss
->timer
, ieee80211_ibss_timer
,
874 (unsigned long) sdata
);
875 mutex_init(&ifibss
->mtx
);
878 /* scan finished notification */
879 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local
*local
)
881 struct ieee80211_sub_if_data
*sdata
;
883 mutex_lock(&local
->iflist_mtx
);
884 list_for_each_entry(sdata
, &local
->interfaces
, list
) {
885 if (!ieee80211_sdata_running(sdata
))
887 if (sdata
->vif
.type
!= NL80211_IFTYPE_ADHOC
)
889 sdata
->u
.ibss
.last_scan_completed
= jiffies
;
890 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
892 mutex_unlock(&local
->iflist_mtx
);
895 int ieee80211_ibss_join(struct ieee80211_sub_if_data
*sdata
,
896 struct cfg80211_ibss_params
*params
)
900 skb
= dev_alloc_skb(sdata
->local
->hw
.extra_tx_headroom
+
904 4 /* IBSS params */ +
909 mutex_lock(&sdata
->u
.ibss
.mtx
);
912 memcpy(sdata
->u
.ibss
.bssid
, params
->bssid
, ETH_ALEN
);
913 sdata
->u
.ibss
.fixed_bssid
= true;
915 sdata
->u
.ibss
.fixed_bssid
= false;
917 sdata
->u
.ibss
.privacy
= params
->privacy
;
918 sdata
->u
.ibss
.basic_rates
= params
->basic_rates
;
919 memcpy(sdata
->vif
.bss_conf
.mcast_rate
, params
->mcast_rate
,
920 sizeof(params
->mcast_rate
));
922 sdata
->vif
.bss_conf
.beacon_int
= params
->beacon_interval
;
924 sdata
->u
.ibss
.channel
= params
->channel
;
925 sdata
->u
.ibss
.fixed_channel
= params
->channel_fixed
;
927 /* fix ourselves to that channel now already */
928 if (params
->channel_fixed
) {
929 sdata
->local
->oper_channel
= params
->channel
;
930 WARN_ON(!ieee80211_set_channel_type(sdata
->local
, sdata
,
931 NL80211_CHAN_NO_HT
));
935 sdata
->u
.ibss
.ie
= kmemdup(params
->ie
, params
->ie_len
,
937 if (sdata
->u
.ibss
.ie
)
938 sdata
->u
.ibss
.ie_len
= params
->ie_len
;
941 sdata
->u
.ibss
.skb
= skb
;
942 sdata
->u
.ibss
.state
= IEEE80211_IBSS_MLME_SEARCH
;
943 sdata
->u
.ibss
.ibss_join_req
= jiffies
;
945 memcpy(sdata
->u
.ibss
.ssid
, params
->ssid
, IEEE80211_MAX_SSID_LEN
);
946 sdata
->u
.ibss
.ssid_len
= params
->ssid_len
;
948 mutex_unlock(&sdata
->u
.ibss
.mtx
);
950 mutex_lock(&sdata
->local
->mtx
);
951 ieee80211_recalc_idle(sdata
->local
);
952 mutex_unlock(&sdata
->local
->mtx
);
954 ieee80211_queue_work(&sdata
->local
->hw
, &sdata
->work
);
959 int ieee80211_ibss_leave(struct ieee80211_sub_if_data
*sdata
)
962 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
963 struct ieee80211_local
*local
= sdata
->local
;
964 struct cfg80211_bss
*cbss
;
968 mutex_lock(&sdata
->u
.ibss
.mtx
);
970 active_ibss
= ieee80211_sta_active_ibss(sdata
);
972 if (!active_ibss
&& !is_zero_ether_addr(ifibss
->bssid
)) {
973 capability
= WLAN_CAPABILITY_IBSS
;
976 capability
|= WLAN_CAPABILITY_PRIVACY
;
978 cbss
= cfg80211_get_bss(local
->hw
.wiphy
, ifibss
->channel
,
979 ifibss
->bssid
, ifibss
->ssid
,
980 ifibss
->ssid_len
, WLAN_CAPABILITY_IBSS
|
981 WLAN_CAPABILITY_PRIVACY
,
985 cfg80211_unlink_bss(local
->hw
.wiphy
, cbss
);
986 cfg80211_put_bss(cbss
);
990 sta_info_flush(sdata
->local
, sdata
);
993 kfree(sdata
->u
.ibss
.ie
);
994 skb
= sdata
->u
.ibss
.presp
;
995 rcu_assign_pointer(sdata
->u
.ibss
.presp
, NULL
);
996 sdata
->vif
.bss_conf
.ibss_joined
= false;
997 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_BEACON_ENABLED
|
1002 skb_queue_purge(&sdata
->skb_queue
);
1003 memset(sdata
->u
.ibss
.bssid
, 0, ETH_ALEN
);
1004 sdata
->u
.ibss
.ssid_len
= 0;
1006 del_timer_sync(&sdata
->u
.ibss
.timer
);
1008 mutex_unlock(&sdata
->u
.ibss
.mtx
);
1010 mutex_lock(&local
->mtx
);
1011 ieee80211_recalc_idle(sdata
->local
);
1012 mutex_unlock(&local
->mtx
);