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_MERGE_DELAY 0x400000
35 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
37 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
40 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data
*sdata
,
41 struct ieee80211_mgmt
*mgmt
,
44 u16 auth_alg
, auth_transaction
, status_code
;
46 lockdep_assert_held(&sdata
->u
.ibss
.mtx
);
51 auth_alg
= le16_to_cpu(mgmt
->u
.auth
.auth_alg
);
52 auth_transaction
= le16_to_cpu(mgmt
->u
.auth
.auth_transaction
);
53 status_code
= le16_to_cpu(mgmt
->u
.auth
.status_code
);
56 * IEEE 802.11 standard does not require authentication in IBSS
57 * networks and most implementations do not seem to use it.
58 * However, try to reply to authentication attempts if someone
59 * has actually implemented this.
61 if (auth_alg
== WLAN_AUTH_OPEN
&& auth_transaction
== 1)
62 ieee80211_send_auth(sdata
, 2, WLAN_AUTH_OPEN
, NULL
, 0,
63 sdata
->u
.ibss
.bssid
, NULL
, 0, 0);
66 static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data
*sdata
,
67 const u8
*bssid
, const int beacon_int
,
68 struct ieee80211_channel
*chan
,
69 const u32 basic_rates
,
70 const u16 capability
, u64 tsf
)
72 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
73 struct ieee80211_local
*local
= sdata
->local
;
76 struct ieee80211_mgmt
*mgmt
;
78 struct ieee80211_supported_band
*sband
;
79 struct cfg80211_bss
*bss
;
81 u8 supp_rates
[IEEE80211_MAX_SUPP_RATES
];
83 lockdep_assert_held(&ifibss
->mtx
);
85 /* Reset own TSF to allow time synchronization work. */
89 rcu_assign_pointer(ifibss
->presp
, NULL
);
91 skb
->data
= skb
->head
;
93 skb_reset_tail_pointer(skb
);
94 skb_reserve(skb
, sdata
->local
->hw
.extra_tx_headroom
);
96 if (memcmp(ifibss
->bssid
, bssid
, ETH_ALEN
))
97 sta_info_flush(sdata
->local
, sdata
);
99 /* if merging, indicate to driver that we leave the old IBSS */
100 if (sdata
->vif
.bss_conf
.ibss_joined
) {
101 sdata
->vif
.bss_conf
.ibss_joined
= false;
102 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_IBSS
);
105 memcpy(ifibss
->bssid
, bssid
, ETH_ALEN
);
107 sdata
->drop_unencrypted
= capability
& WLAN_CAPABILITY_PRIVACY
? 1 : 0;
109 local
->oper_channel
= chan
;
110 WARN_ON(!ieee80211_set_channel_type(local
, sdata
, NL80211_CHAN_NO_HT
));
111 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
113 sband
= local
->hw
.wiphy
->bands
[chan
->band
];
115 /* build supported rates array */
117 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
118 int rate
= sband
->bitrates
[i
].bitrate
;
120 if (basic_rates
& BIT(i
))
122 *pos
++ = basic
| (u8
) (rate
/ 5);
125 /* Build IBSS probe response */
126 mgmt
= (void *) skb_put(skb
, 24 + sizeof(mgmt
->u
.beacon
));
127 memset(mgmt
, 0, 24 + sizeof(mgmt
->u
.beacon
));
128 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
129 IEEE80211_STYPE_PROBE_RESP
);
130 memset(mgmt
->da
, 0xff, ETH_ALEN
);
131 memcpy(mgmt
->sa
, sdata
->vif
.addr
, ETH_ALEN
);
132 memcpy(mgmt
->bssid
, ifibss
->bssid
, ETH_ALEN
);
133 mgmt
->u
.beacon
.beacon_int
= cpu_to_le16(beacon_int
);
134 mgmt
->u
.beacon
.timestamp
= cpu_to_le64(tsf
);
135 mgmt
->u
.beacon
.capab_info
= cpu_to_le16(capability
);
137 pos
= skb_put(skb
, 2 + ifibss
->ssid_len
);
138 *pos
++ = WLAN_EID_SSID
;
139 *pos
++ = ifibss
->ssid_len
;
140 memcpy(pos
, ifibss
->ssid
, ifibss
->ssid_len
);
142 rates
= sband
->n_bitrates
;
145 pos
= skb_put(skb
, 2 + rates
);
146 *pos
++ = WLAN_EID_SUPP_RATES
;
148 memcpy(pos
, supp_rates
, rates
);
150 if (sband
->band
== IEEE80211_BAND_2GHZ
) {
151 pos
= skb_put(skb
, 2 + 1);
152 *pos
++ = WLAN_EID_DS_PARAMS
;
154 *pos
++ = ieee80211_frequency_to_channel(chan
->center_freq
);
157 pos
= skb_put(skb
, 2 + 2);
158 *pos
++ = WLAN_EID_IBSS_PARAMS
;
160 /* FIX: set ATIM window based on scan results */
164 if (sband
->n_bitrates
> 8) {
165 rates
= sband
->n_bitrates
- 8;
166 pos
= skb_put(skb
, 2 + rates
);
167 *pos
++ = WLAN_EID_EXT_SUPP_RATES
;
169 memcpy(pos
, &supp_rates
[8], rates
);
173 memcpy(skb_put(skb
, ifibss
->ie_len
),
174 ifibss
->ie
, ifibss
->ie_len
);
176 rcu_assign_pointer(ifibss
->presp
, skb
);
178 sdata
->vif
.bss_conf
.beacon_int
= beacon_int
;
179 sdata
->vif
.bss_conf
.basic_rates
= basic_rates
;
180 bss_change
= BSS_CHANGED_BEACON_INT
;
181 bss_change
|= ieee80211_reset_erp_info(sdata
);
182 bss_change
|= BSS_CHANGED_BSSID
;
183 bss_change
|= BSS_CHANGED_BEACON
;
184 bss_change
|= BSS_CHANGED_BEACON_ENABLED
;
185 bss_change
|= BSS_CHANGED_BASIC_RATES
;
186 bss_change
|= BSS_CHANGED_IBSS
;
187 sdata
->vif
.bss_conf
.ibss_joined
= true;
188 ieee80211_bss_info_change_notify(sdata
, bss_change
);
190 ieee80211_sta_def_wmm_params(sdata
, sband
->n_bitrates
, supp_rates
);
192 ifibss
->state
= IEEE80211_IBSS_MLME_JOINED
;
193 mod_timer(&ifibss
->timer
,
194 round_jiffies(jiffies
+ IEEE80211_IBSS_MERGE_INTERVAL
));
196 bss
= cfg80211_inform_bss_frame(local
->hw
.wiphy
, local
->hw
.conf
.channel
,
197 mgmt
, skb
->len
, 0, GFP_KERNEL
);
198 cfg80211_put_bss(bss
);
199 cfg80211_ibss_joined(sdata
->dev
, ifibss
->bssid
, GFP_KERNEL
);
202 static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data
*sdata
,
203 struct ieee80211_bss
*bss
)
205 struct cfg80211_bss
*cbss
=
206 container_of((void *)bss
, struct cfg80211_bss
, priv
);
207 struct ieee80211_supported_band
*sband
;
210 u16 beacon_int
= cbss
->beacon_interval
;
212 lockdep_assert_held(&sdata
->u
.ibss
.mtx
);
217 sband
= sdata
->local
->hw
.wiphy
->bands
[cbss
->channel
->band
];
221 for (i
= 0; i
< bss
->supp_rates_len
; i
++) {
222 int rate
= (bss
->supp_rates
[i
] & 0x7f) * 5;
223 bool is_basic
= !!(bss
->supp_rates
[i
] & 0x80);
225 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
226 if (sband
->bitrates
[j
].bitrate
== rate
) {
228 basic_rates
|= BIT(j
);
234 __ieee80211_sta_join_ibss(sdata
, cbss
->bssid
,
242 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data
*sdata
,
243 struct ieee80211_mgmt
*mgmt
,
245 struct ieee80211_rx_status
*rx_status
,
246 struct ieee802_11_elems
*elems
,
249 struct ieee80211_local
*local
= sdata
->local
;
251 struct cfg80211_bss
*cbss
;
252 struct ieee80211_bss
*bss
;
253 struct sta_info
*sta
;
254 struct ieee80211_channel
*channel
;
255 u64 beacon_timestamp
, rx_timestamp
;
257 enum ieee80211_band band
= rx_status
->band
;
259 if (elems
->ds_params
&& elems
->ds_params_len
== 1)
260 freq
= ieee80211_channel_to_frequency(elems
->ds_params
[0]);
262 freq
= rx_status
->freq
;
264 channel
= ieee80211_get_channel(local
->hw
.wiphy
, freq
);
266 if (!channel
|| channel
->flags
& IEEE80211_CHAN_DISABLED
)
269 if (sdata
->vif
.type
== NL80211_IFTYPE_ADHOC
&& elems
->supp_rates
&&
270 memcmp(mgmt
->bssid
, sdata
->u
.ibss
.bssid
, ETH_ALEN
) == 0) {
271 supp_rates
= ieee80211_sta_get_rates(local
, elems
, band
);
275 sta
= sta_info_get(sdata
, mgmt
->sa
);
279 prev_rates
= sta
->sta
.supp_rates
[band
];
280 /* make sure mandatory rates are always added */
281 sta
->sta
.supp_rates
[band
] = supp_rates
|
282 ieee80211_mandatory_rates(local
, band
);
284 if (sta
->sta
.supp_rates
[band
] != prev_rates
) {
285 #ifdef CONFIG_MAC80211_IBSS_DEBUG
286 printk(KERN_DEBUG
"%s: updated supp_rates set "
287 "for %pM based on beacon/probe_response "
289 sdata
->name
, sta
->sta
.addr
,
290 prev_rates
, sta
->sta
.supp_rates
[band
]);
292 rate_control_rate_init(sta
);
297 ieee80211_ibss_add_sta(sdata
, mgmt
->bssid
, mgmt
->sa
,
298 supp_rates
, GFP_KERNEL
);
302 bss
= ieee80211_bss_info_update(local
, rx_status
, mgmt
, len
, elems
,
307 cbss
= container_of((void *)bss
, struct cfg80211_bss
, priv
);
309 /* was just updated in ieee80211_bss_info_update */
310 beacon_timestamp
= cbss
->tsf
;
312 /* check if we need to merge IBSS */
314 /* we use a fixed BSSID */
315 if (sdata
->u
.ibss
.fixed_bssid
)
319 if (!(cbss
->capability
& WLAN_CAPABILITY_IBSS
))
322 /* different channel */
323 if (cbss
->channel
!= local
->oper_channel
)
327 if (elems
->ssid_len
!= sdata
->u
.ibss
.ssid_len
||
328 memcmp(elems
->ssid
, sdata
->u
.ibss
.ssid
,
329 sdata
->u
.ibss
.ssid_len
))
333 if (memcmp(cbss
->bssid
, sdata
->u
.ibss
.bssid
, ETH_ALEN
) == 0)
336 if (rx_status
->flag
& RX_FLAG_TSFT
) {
338 * For correct IBSS merging we need mactime; since mactime is
339 * defined as the time the first data symbol of the frame hits
340 * the PHY, and the timestamp of the beacon is defined as "the
341 * time that the data symbol containing the first bit of the
342 * timestamp is transmitted to the PHY plus the transmitting
343 * STA's delays through its local PHY from the MAC-PHY
344 * interface to its interface with the WM" (802.11 11.1.2)
345 * - equals the time this bit arrives at the receiver - we have
346 * to take into account the offset between the two.
348 * E.g. at 1 MBit that means mactime is 192 usec earlier
349 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
353 if (rx_status
->flag
& RX_FLAG_HT
)
354 rate
= 65; /* TODO: HT rates */
356 rate
= local
->hw
.wiphy
->bands
[band
]->
357 bitrates
[rx_status
->rate_idx
].bitrate
;
359 rx_timestamp
= rx_status
->mactime
+ (24 * 8 * 10 / rate
);
362 * second best option: get current TSF
363 * (will return -1 if not supported)
365 rx_timestamp
= drv_get_tsf(local
);
368 #ifdef CONFIG_MAC80211_IBSS_DEBUG
369 printk(KERN_DEBUG
"RX beacon SA=%pM BSSID="
370 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
371 mgmt
->sa
, mgmt
->bssid
,
372 (unsigned long long)rx_timestamp
,
373 (unsigned long long)beacon_timestamp
,
374 (unsigned long long)(rx_timestamp
- beacon_timestamp
),
378 /* give slow hardware some time to do the TSF sync */
379 if (rx_timestamp
< IEEE80211_IBSS_MERGE_DELAY
)
382 if (beacon_timestamp
> rx_timestamp
) {
383 #ifdef CONFIG_MAC80211_IBSS_DEBUG
384 printk(KERN_DEBUG
"%s: beacon TSF higher than "
385 "local TSF - IBSS merge with BSSID %pM\n",
386 sdata
->name
, mgmt
->bssid
);
388 ieee80211_sta_join_ibss(sdata
, bss
);
389 supp_rates
= ieee80211_sta_get_rates(local
, elems
, band
);
390 ieee80211_ibss_add_sta(sdata
, mgmt
->bssid
, mgmt
->sa
,
391 supp_rates
, GFP_KERNEL
);
395 ieee80211_rx_bss_put(local
, bss
);
399 * Add a new IBSS station, will also be called by the RX code when,
400 * in IBSS mode, receiving a frame from a yet-unknown station, hence
401 * must be callable in atomic context.
403 struct sta_info
*ieee80211_ibss_add_sta(struct ieee80211_sub_if_data
*sdata
,
404 u8
*bssid
,u8
*addr
, u32 supp_rates
,
407 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
408 struct ieee80211_local
*local
= sdata
->local
;
409 struct sta_info
*sta
;
410 int band
= local
->hw
.conf
.channel
->band
;
412 if (local
->num_sta
>= IEEE80211_IBSS_MAX_STA_ENTRIES
) {
414 printk(KERN_DEBUG
"%s: No room for a new IBSS STA entry %pM\n",
419 if (ifibss
->state
== IEEE80211_IBSS_MLME_SEARCH
)
422 if (compare_ether_addr(bssid
, sdata
->u
.ibss
.bssid
))
425 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
426 printk(KERN_DEBUG
"%s: Adding new IBSS station %pM (dev=%s)\n",
427 wiphy_name(local
->hw
.wiphy
), addr
, sdata
->name
);
430 sta
= sta_info_alloc(sdata
, addr
, gfp
);
434 sta
->last_rx
= jiffies
;
435 set_sta_flags(sta
, WLAN_STA_AUTHORIZED
);
437 /* make sure mandatory rates are always added */
438 sta
->sta
.supp_rates
[band
] = supp_rates
|
439 ieee80211_mandatory_rates(local
, band
);
441 rate_control_rate_init(sta
);
443 /* If it fails, maybe we raced another insertion? */
444 if (sta_info_insert(sta
))
445 return sta_info_get(sdata
, addr
);
449 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data
*sdata
)
451 struct ieee80211_local
*local
= sdata
->local
;
453 struct sta_info
*sta
;
455 lockdep_assert_held(&sdata
->u
.ibss
.mtx
);
459 list_for_each_entry_rcu(sta
, &local
->sta_list
, list
) {
460 if (sta
->sdata
== sdata
&&
461 time_after(sta
->last_rx
+ IEEE80211_IBSS_MERGE_INTERVAL
,
474 * This function is called with state == IEEE80211_IBSS_MLME_JOINED
477 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data
*sdata
)
479 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
481 lockdep_assert_held(&ifibss
->mtx
);
483 mod_timer(&ifibss
->timer
,
484 round_jiffies(jiffies
+ IEEE80211_IBSS_MERGE_INTERVAL
));
486 ieee80211_sta_expire(sdata
, IEEE80211_IBSS_INACTIVITY_LIMIT
);
488 if (time_before(jiffies
, ifibss
->last_scan_completed
+
489 IEEE80211_IBSS_MERGE_INTERVAL
))
492 if (ieee80211_sta_active_ibss(sdata
))
495 if (ifibss
->fixed_channel
)
498 printk(KERN_DEBUG
"%s: No active IBSS STAs - trying to scan for other "
499 "IBSS networks with same SSID (merge)\n", sdata
->name
);
501 ieee80211_request_internal_scan(sdata
,
502 ifibss
->ssid
, ifibss
->ssid_len
,
503 ifibss
->fixed_channel
? ifibss
->channel
: NULL
);
506 static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data
*sdata
)
508 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
509 struct ieee80211_local
*local
= sdata
->local
;
510 struct ieee80211_supported_band
*sband
;
515 lockdep_assert_held(&ifibss
->mtx
);
517 if (ifibss
->fixed_bssid
) {
518 memcpy(bssid
, ifibss
->bssid
, ETH_ALEN
);
520 /* Generate random, not broadcast, locally administered BSSID. Mix in
521 * own MAC address to make sure that devices that do not have proper
522 * random number generator get different BSSID. */
523 get_random_bytes(bssid
, ETH_ALEN
);
524 for (i
= 0; i
< ETH_ALEN
; i
++)
525 bssid
[i
] ^= sdata
->vif
.addr
[i
];
530 printk(KERN_DEBUG
"%s: Creating new IBSS network, BSSID %pM\n",
533 sband
= local
->hw
.wiphy
->bands
[ifibss
->channel
->band
];
535 capability
= WLAN_CAPABILITY_IBSS
;
538 capability
|= WLAN_CAPABILITY_PRIVACY
;
540 sdata
->drop_unencrypted
= 0;
542 __ieee80211_sta_join_ibss(sdata
, bssid
, sdata
->vif
.bss_conf
.beacon_int
,
543 ifibss
->channel
, ifibss
->basic_rates
,
548 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
551 static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data
*sdata
)
553 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
554 struct ieee80211_local
*local
= sdata
->local
;
555 struct cfg80211_bss
*cbss
;
556 struct ieee80211_channel
*chan
= NULL
;
557 const u8
*bssid
= NULL
;
561 lockdep_assert_held(&ifibss
->mtx
);
563 active_ibss
= ieee80211_sta_active_ibss(sdata
);
564 #ifdef CONFIG_MAC80211_IBSS_DEBUG
565 printk(KERN_DEBUG
"%s: sta_find_ibss (active_ibss=%d)\n",
566 sdata
->name
, active_ibss
);
567 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
572 capability
= WLAN_CAPABILITY_IBSS
;
574 capability
|= WLAN_CAPABILITY_PRIVACY
;
575 if (ifibss
->fixed_bssid
)
576 bssid
= ifibss
->bssid
;
577 if (ifibss
->fixed_channel
)
578 chan
= ifibss
->channel
;
579 if (!is_zero_ether_addr(ifibss
->bssid
))
580 bssid
= ifibss
->bssid
;
581 cbss
= cfg80211_get_bss(local
->hw
.wiphy
, chan
, bssid
,
582 ifibss
->ssid
, ifibss
->ssid_len
,
583 WLAN_CAPABILITY_IBSS
| WLAN_CAPABILITY_PRIVACY
,
587 struct ieee80211_bss
*bss
;
589 bss
= (void *)cbss
->priv
;
590 #ifdef CONFIG_MAC80211_IBSS_DEBUG
591 printk(KERN_DEBUG
" sta_find_ibss: selected %pM current "
592 "%pM\n", cbss
->bssid
, ifibss
->bssid
);
593 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
595 printk(KERN_DEBUG
"%s: Selected IBSS BSSID %pM"
596 " based on configured SSID\n",
597 sdata
->name
, cbss
->bssid
);
599 ieee80211_sta_join_ibss(sdata
, bss
);
600 ieee80211_rx_bss_put(local
, bss
);
604 #ifdef CONFIG_MAC80211_IBSS_DEBUG
605 printk(KERN_DEBUG
" did not try to join ibss\n");
606 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
608 /* Selected IBSS not found in current scan results - try to scan */
609 if (time_after(jiffies
, ifibss
->last_scan_completed
+
610 IEEE80211_SCAN_INTERVAL
)) {
611 printk(KERN_DEBUG
"%s: Trigger new scan to find an IBSS to "
612 "join\n", sdata
->name
);
614 ieee80211_request_internal_scan(sdata
,
615 ifibss
->ssid
, ifibss
->ssid_len
,
616 ifibss
->fixed_channel
? ifibss
->channel
: NULL
);
618 int interval
= IEEE80211_SCAN_INTERVAL
;
620 if (time_after(jiffies
, ifibss
->ibss_join_req
+
621 IEEE80211_IBSS_JOIN_TIMEOUT
)) {
622 if (!(local
->oper_channel
->flags
& IEEE80211_CHAN_NO_IBSS
)) {
623 ieee80211_sta_create_ibss(sdata
);
626 printk(KERN_DEBUG
"%s: IBSS not allowed on"
627 " %d MHz\n", sdata
->name
,
628 local
->hw
.conf
.channel
->center_freq
);
630 /* No IBSS found - decrease scan interval and continue
632 interval
= IEEE80211_SCAN_INTERVAL_SLOW
;
635 mod_timer(&ifibss
->timer
,
636 round_jiffies(jiffies
+ interval
));
640 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data
*sdata
,
641 struct ieee80211_mgmt
*mgmt
,
644 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
645 struct ieee80211_local
*local
= sdata
->local
;
648 struct ieee80211_mgmt
*resp
;
651 lockdep_assert_held(&ifibss
->mtx
);
653 if (ifibss
->state
!= IEEE80211_IBSS_MLME_JOINED
||
654 len
< 24 + 2 || !ifibss
->presp
)
657 tx_last_beacon
= drv_tx_last_beacon(local
);
659 #ifdef CONFIG_MAC80211_IBSS_DEBUG
660 printk(KERN_DEBUG
"%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
661 " (tx_last_beacon=%d)\n",
662 sdata
->name
, mgmt
->sa
, mgmt
->da
,
663 mgmt
->bssid
, tx_last_beacon
);
664 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
669 if (memcmp(mgmt
->bssid
, ifibss
->bssid
, ETH_ALEN
) != 0 &&
670 memcmp(mgmt
->bssid
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
673 end
= ((u8
*) mgmt
) + len
;
674 pos
= mgmt
->u
.probe_req
.variable
;
675 if (pos
[0] != WLAN_EID_SSID
||
676 pos
+ 2 + pos
[1] > end
) {
677 #ifdef CONFIG_MAC80211_IBSS_DEBUG
678 printk(KERN_DEBUG
"%s: Invalid SSID IE in ProbeReq "
680 sdata
->name
, mgmt
->sa
);
685 (pos
[1] != ifibss
->ssid_len
||
686 memcmp(pos
+ 2, ifibss
->ssid
, ifibss
->ssid_len
))) {
687 /* Ignore ProbeReq for foreign SSID */
691 /* Reply with ProbeResp */
692 skb
= skb_copy(ifibss
->presp
, GFP_KERNEL
);
696 resp
= (struct ieee80211_mgmt
*) skb
->data
;
697 memcpy(resp
->da
, mgmt
->sa
, ETH_ALEN
);
698 #ifdef CONFIG_MAC80211_IBSS_DEBUG
699 printk(KERN_DEBUG
"%s: Sending ProbeResp to %pM\n",
700 sdata
->name
, resp
->da
);
701 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
702 IEEE80211_SKB_CB(skb
)->flags
|= IEEE80211_TX_INTFL_DONT_ENCRYPT
;
703 ieee80211_tx_skb(sdata
, skb
);
706 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data
*sdata
,
707 struct ieee80211_mgmt
*mgmt
,
709 struct ieee80211_rx_status
*rx_status
)
712 struct ieee802_11_elems elems
;
714 if (memcmp(mgmt
->da
, sdata
->vif
.addr
, ETH_ALEN
))
715 return; /* ignore ProbeResp to foreign address */
717 baselen
= (u8
*) mgmt
->u
.probe_resp
.variable
- (u8
*) mgmt
;
721 ieee802_11_parse_elems(mgmt
->u
.probe_resp
.variable
, len
- baselen
,
724 ieee80211_rx_bss_info(sdata
, mgmt
, len
, rx_status
, &elems
, false);
727 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data
*sdata
,
728 struct ieee80211_mgmt
*mgmt
,
730 struct ieee80211_rx_status
*rx_status
)
733 struct ieee802_11_elems elems
;
735 /* Process beacon from the current BSS */
736 baselen
= (u8
*) mgmt
->u
.beacon
.variable
- (u8
*) mgmt
;
740 ieee802_11_parse_elems(mgmt
->u
.beacon
.variable
, len
- baselen
, &elems
);
742 ieee80211_rx_bss_info(sdata
, mgmt
, len
, rx_status
, &elems
, true);
745 void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data
*sdata
,
748 struct ieee80211_rx_status
*rx_status
;
749 struct ieee80211_mgmt
*mgmt
;
752 rx_status
= IEEE80211_SKB_RXCB(skb
);
753 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
754 fc
= le16_to_cpu(mgmt
->frame_control
);
756 mutex_lock(&sdata
->u
.ibss
.mtx
);
758 switch (fc
& IEEE80211_FCTL_STYPE
) {
759 case IEEE80211_STYPE_PROBE_REQ
:
760 ieee80211_rx_mgmt_probe_req(sdata
, mgmt
, skb
->len
);
762 case IEEE80211_STYPE_PROBE_RESP
:
763 ieee80211_rx_mgmt_probe_resp(sdata
, mgmt
, skb
->len
,
766 case IEEE80211_STYPE_BEACON
:
767 ieee80211_rx_mgmt_beacon(sdata
, mgmt
, skb
->len
,
770 case IEEE80211_STYPE_AUTH
:
771 ieee80211_rx_mgmt_auth_ibss(sdata
, mgmt
, skb
->len
);
775 mutex_unlock(&sdata
->u
.ibss
.mtx
);
778 void ieee80211_ibss_work(struct ieee80211_sub_if_data
*sdata
)
780 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
782 mutex_lock(&ifibss
->mtx
);
785 * Work could be scheduled after scan or similar
786 * when we aren't even joined (or trying) with a
789 if (!ifibss
->ssid_len
)
792 switch (ifibss
->state
) {
793 case IEEE80211_IBSS_MLME_SEARCH
:
794 ieee80211_sta_find_ibss(sdata
);
796 case IEEE80211_IBSS_MLME_JOINED
:
797 ieee80211_sta_merge_ibss(sdata
);
805 mutex_unlock(&ifibss
->mtx
);
808 static void ieee80211_ibss_timer(unsigned long data
)
810 struct ieee80211_sub_if_data
*sdata
=
811 (struct ieee80211_sub_if_data
*) data
;
812 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
813 struct ieee80211_local
*local
= sdata
->local
;
815 if (local
->quiescing
) {
816 ifibss
->timer_running
= true;
820 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
824 void ieee80211_ibss_quiesce(struct ieee80211_sub_if_data
*sdata
)
826 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
828 if (del_timer_sync(&ifibss
->timer
))
829 ifibss
->timer_running
= true;
832 void ieee80211_ibss_restart(struct ieee80211_sub_if_data
*sdata
)
834 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
836 if (ifibss
->timer_running
) {
837 add_timer(&ifibss
->timer
);
838 ifibss
->timer_running
= false;
843 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data
*sdata
)
845 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
847 setup_timer(&ifibss
->timer
, ieee80211_ibss_timer
,
848 (unsigned long) sdata
);
849 mutex_init(&ifibss
->mtx
);
852 /* scan finished notification */
853 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local
*local
)
855 struct ieee80211_sub_if_data
*sdata
;
857 mutex_lock(&local
->iflist_mtx
);
858 list_for_each_entry(sdata
, &local
->interfaces
, list
) {
859 if (!ieee80211_sdata_running(sdata
))
861 if (sdata
->vif
.type
!= NL80211_IFTYPE_ADHOC
)
863 sdata
->u
.ibss
.last_scan_completed
= jiffies
;
864 ieee80211_queue_work(&local
->hw
, &sdata
->work
);
866 mutex_unlock(&local
->iflist_mtx
);
869 int ieee80211_ibss_join(struct ieee80211_sub_if_data
*sdata
,
870 struct cfg80211_ibss_params
*params
)
874 skb
= dev_alloc_skb(sdata
->local
->hw
.extra_tx_headroom
+
878 4 /* IBSS params */ +
883 mutex_lock(&sdata
->u
.ibss
.mtx
);
886 memcpy(sdata
->u
.ibss
.bssid
, params
->bssid
, ETH_ALEN
);
887 sdata
->u
.ibss
.fixed_bssid
= true;
889 sdata
->u
.ibss
.fixed_bssid
= false;
891 sdata
->u
.ibss
.privacy
= params
->privacy
;
892 sdata
->u
.ibss
.basic_rates
= params
->basic_rates
;
894 sdata
->vif
.bss_conf
.beacon_int
= params
->beacon_interval
;
896 sdata
->u
.ibss
.channel
= params
->channel
;
897 sdata
->u
.ibss
.fixed_channel
= params
->channel_fixed
;
899 /* fix ourselves to that channel now already */
900 if (params
->channel_fixed
) {
901 sdata
->local
->oper_channel
= params
->channel
;
902 WARN_ON(!ieee80211_set_channel_type(sdata
->local
, sdata
,
903 NL80211_CHAN_NO_HT
));
907 sdata
->u
.ibss
.ie
= kmemdup(params
->ie
, params
->ie_len
,
909 if (sdata
->u
.ibss
.ie
)
910 sdata
->u
.ibss
.ie_len
= params
->ie_len
;
913 sdata
->u
.ibss
.skb
= skb
;
914 sdata
->u
.ibss
.state
= IEEE80211_IBSS_MLME_SEARCH
;
915 sdata
->u
.ibss
.ibss_join_req
= jiffies
;
917 memcpy(sdata
->u
.ibss
.ssid
, params
->ssid
, IEEE80211_MAX_SSID_LEN
);
918 sdata
->u
.ibss
.ssid_len
= params
->ssid_len
;
920 ieee80211_recalc_idle(sdata
->local
);
922 ieee80211_queue_work(&sdata
->local
->hw
, &sdata
->work
);
924 mutex_unlock(&sdata
->u
.ibss
.mtx
);
929 int ieee80211_ibss_leave(struct ieee80211_sub_if_data
*sdata
)
932 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
933 struct ieee80211_local
*local
= sdata
->local
;
934 struct cfg80211_bss
*cbss
;
938 mutex_lock(&sdata
->u
.ibss
.mtx
);
940 active_ibss
= ieee80211_sta_active_ibss(sdata
);
942 if (!active_ibss
&& !is_zero_ether_addr(ifibss
->bssid
)) {
943 capability
= WLAN_CAPABILITY_IBSS
;
946 capability
|= WLAN_CAPABILITY_PRIVACY
;
948 cbss
= cfg80211_get_bss(local
->hw
.wiphy
, ifibss
->channel
,
949 ifibss
->bssid
, ifibss
->ssid
,
950 ifibss
->ssid_len
, WLAN_CAPABILITY_IBSS
|
951 WLAN_CAPABILITY_PRIVACY
,
955 cfg80211_unlink_bss(local
->hw
.wiphy
, cbss
);
956 cfg80211_put_bss(cbss
);
960 sta_info_flush(sdata
->local
, sdata
);
963 kfree(sdata
->u
.ibss
.ie
);
964 skb
= sdata
->u
.ibss
.presp
;
965 rcu_assign_pointer(sdata
->u
.ibss
.presp
, NULL
);
966 sdata
->vif
.bss_conf
.ibss_joined
= false;
967 ieee80211_bss_info_change_notify(sdata
, BSS_CHANGED_BEACON_ENABLED
|
972 skb_queue_purge(&sdata
->skb_queue
);
973 memset(sdata
->u
.ibss
.bssid
, 0, ETH_ALEN
);
974 sdata
->u
.ibss
.ssid_len
= 0;
976 del_timer_sync(&sdata
->u
.ibss
.timer
);
978 mutex_unlock(&sdata
->u
.ibss
.mtx
);
980 ieee80211_recalc_idle(sdata
->local
);