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/if_ether.h>
17 #include <linux/skbuff.h>
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rtnetlink.h>
21 #include <net/mac80211.h>
22 #include <asm/unaligned.h>
24 #include "ieee80211_i.h"
27 #define IEEE80211_SCAN_INTERVAL (2 * HZ)
28 #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
29 #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
31 #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
32 #define IEEE80211_IBSS_MERGE_DELAY 0x400000
33 #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
35 #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
38 static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data
*sdata
,
39 struct ieee80211_mgmt
*mgmt
,
42 u16 auth_alg
, auth_transaction
, status_code
;
47 auth_alg
= le16_to_cpu(mgmt
->u
.auth
.auth_alg
);
48 auth_transaction
= le16_to_cpu(mgmt
->u
.auth
.auth_transaction
);
49 status_code
= le16_to_cpu(mgmt
->u
.auth
.status_code
);
52 * IEEE 802.11 standard does not require authentication in IBSS
53 * networks and most implementations do not seem to use it.
54 * However, try to reply to authentication attempts if someone
55 * has actually implemented this.
57 if (auth_alg
== WLAN_AUTH_OPEN
&& auth_transaction
== 1)
58 ieee80211_send_auth(sdata
, 2, WLAN_AUTH_OPEN
, NULL
, 0,
59 sdata
->u
.ibss
.bssid
, 0);
62 static int __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data
*sdata
,
63 const u8
*bssid
, const int beacon_int
,
65 const size_t supp_rates_len
,
67 const u16 capability
, u64 tsf
)
69 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
70 struct ieee80211_local
*local
= sdata
->local
;
71 int res
= 0, rates
, i
, j
;
73 struct ieee80211_mgmt
*mgmt
;
75 struct ieee80211_supported_band
*sband
;
76 union iwreq_data wrqu
;
78 if (local
->ops
->reset_tsf
) {
79 /* Reset own TSF to allow time synchronization work. */
80 local
->ops
->reset_tsf(local_to_hw(local
));
83 if ((ifibss
->flags
& IEEE80211_IBSS_PREV_BSSID_SET
) &&
84 memcmp(ifibss
->bssid
, bssid
, ETH_ALEN
) == 0)
87 skb
= dev_alloc_skb(local
->hw
.extra_tx_headroom
+ 400);
89 printk(KERN_DEBUG
"%s: failed to allocate buffer for probe "
90 "response\n", sdata
->dev
->name
);
94 if (!(ifibss
->flags
& IEEE80211_IBSS_PREV_BSSID_SET
)) {
95 /* Remove possible STA entries from other IBSS networks. */
96 sta_info_flush_delayed(sdata
);
99 memcpy(ifibss
->bssid
, bssid
, ETH_ALEN
);
100 res
= ieee80211_if_config(sdata
, IEEE80211_IFCC_BSSID
);
104 local
->hw
.conf
.beacon_int
= beacon_int
>= 10 ? beacon_int
: 10;
106 sdata
->drop_unencrypted
= capability
&
107 WLAN_CAPABILITY_PRIVACY
? 1 : 0;
109 res
= ieee80211_set_freq(sdata
, freq
);
114 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
116 /* Build IBSS probe response */
118 skb_reserve(skb
, local
->hw
.extra_tx_headroom
);
120 mgmt
= (struct ieee80211_mgmt
*)
121 skb_put(skb
, 24 + sizeof(mgmt
->u
.beacon
));
122 memset(mgmt
, 0, 24 + sizeof(mgmt
->u
.beacon
));
123 mgmt
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_MGMT
|
124 IEEE80211_STYPE_PROBE_RESP
);
125 memset(mgmt
->da
, 0xff, ETH_ALEN
);
126 memcpy(mgmt
->sa
, sdata
->dev
->dev_addr
, ETH_ALEN
);
127 memcpy(mgmt
->bssid
, ifibss
->bssid
, ETH_ALEN
);
128 mgmt
->u
.beacon
.beacon_int
=
129 cpu_to_le16(local
->hw
.conf
.beacon_int
);
130 mgmt
->u
.beacon
.timestamp
= cpu_to_le64(tsf
);
131 mgmt
->u
.beacon
.capab_info
= cpu_to_le16(capability
);
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
);
138 rates
= supp_rates_len
;
141 pos
= skb_put(skb
, 2 + rates
);
142 *pos
++ = WLAN_EID_SUPP_RATES
;
144 memcpy(pos
, supp_rates
, rates
);
146 if (sband
->band
== IEEE80211_BAND_2GHZ
) {
147 pos
= skb_put(skb
, 2 + 1);
148 *pos
++ = WLAN_EID_DS_PARAMS
;
150 *pos
++ = ieee80211_frequency_to_channel(freq
);
153 pos
= skb_put(skb
, 2 + 2);
154 *pos
++ = WLAN_EID_IBSS_PARAMS
;
156 /* FIX: set ATIM window based on scan results */
160 if (supp_rates_len
> 8) {
161 rates
= supp_rates_len
- 8;
162 pos
= skb_put(skb
, 2 + rates
);
163 *pos
++ = WLAN_EID_EXT_SUPP_RATES
;
165 memcpy(pos
, &supp_rates
[8], rates
);
168 ifibss
->probe_resp
= skb
;
170 ieee80211_if_config(sdata
, IEEE80211_IFCC_BEACON
|
171 IEEE80211_IFCC_BEACON_ENABLED
);
175 for (i
= 0; i
< supp_rates_len
; i
++) {
176 int bitrate
= (supp_rates
[i
] & 0x7f) * 5;
177 for (j
= 0; j
< sband
->n_bitrates
; j
++)
178 if (sband
->bitrates
[j
].bitrate
== bitrate
)
182 ieee80211_sta_def_wmm_params(sdata
, supp_rates_len
, supp_rates
);
184 ifibss
->flags
|= IEEE80211_IBSS_PREV_BSSID_SET
;
185 ifibss
->state
= IEEE80211_IBSS_MLME_JOINED
;
186 mod_timer(&ifibss
->timer
, jiffies
+ IEEE80211_IBSS_MERGE_INTERVAL
);
188 memset(&wrqu
, 0, sizeof(wrqu
));
189 memcpy(wrqu
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
190 wireless_send_event(sdata
->dev
, SIOCGIWAP
, &wrqu
, NULL
);
195 static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data
*sdata
,
196 struct ieee80211_bss
*bss
)
198 return __ieee80211_sta_join_ibss(sdata
,
200 bss
->cbss
.beacon_interval
,
201 bss
->cbss
.channel
->center_freq
,
202 bss
->supp_rates_len
, bss
->supp_rates
,
203 bss
->cbss
.capability
,
207 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data
*sdata
,
208 struct ieee80211_mgmt
*mgmt
,
210 struct ieee80211_rx_status
*rx_status
,
211 struct ieee802_11_elems
*elems
,
214 struct ieee80211_local
*local
= sdata
->local
;
216 struct ieee80211_bss
*bss
;
217 struct sta_info
*sta
;
218 struct ieee80211_channel
*channel
;
219 u64 beacon_timestamp
, rx_timestamp
;
221 enum ieee80211_band band
= rx_status
->band
;
223 if (elems
->ds_params
&& elems
->ds_params_len
== 1)
224 freq
= ieee80211_channel_to_frequency(elems
->ds_params
[0]);
226 freq
= rx_status
->freq
;
228 channel
= ieee80211_get_channel(local
->hw
.wiphy
, freq
);
230 if (!channel
|| channel
->flags
& IEEE80211_CHAN_DISABLED
)
233 if (sdata
->vif
.type
== NL80211_IFTYPE_ADHOC
&& elems
->supp_rates
&&
234 memcmp(mgmt
->bssid
, sdata
->u
.ibss
.bssid
, ETH_ALEN
) == 0) {
235 supp_rates
= ieee80211_sta_get_rates(local
, elems
, band
);
239 sta
= sta_info_get(local
, mgmt
->sa
);
243 prev_rates
= sta
->sta
.supp_rates
[band
];
244 /* make sure mandatory rates are always added */
245 sta
->sta
.supp_rates
[band
] = supp_rates
|
246 ieee80211_mandatory_rates(local
, band
);
248 #ifdef CONFIG_MAC80211_IBSS_DEBUG
249 if (sta
->sta
.supp_rates
[band
] != prev_rates
)
250 printk(KERN_DEBUG
"%s: updated supp_rates set "
251 "for %pM based on beacon info (0x%llx | "
252 "0x%llx -> 0x%llx)\n",
255 (unsigned long long) prev_rates
,
256 (unsigned long long) supp_rates
,
257 (unsigned long long) sta
->sta
.supp_rates
[band
]);
260 ieee80211_ibss_add_sta(sdata
, mgmt
->bssid
, mgmt
->sa
, supp_rates
);
265 bss
= ieee80211_bss_info_update(local
, rx_status
, mgmt
, len
, elems
,
270 /* was just updated in ieee80211_bss_info_update */
271 beacon_timestamp
= bss
->cbss
.tsf
;
273 /* check if we need to merge IBSS */
275 /* merge only on beacons (???) */
279 /* we use a fixed BSSID */
280 if (sdata
->u
.ibss
.flags
& IEEE80211_IBSS_BSSID_SET
)
284 if (!(bss
->cbss
.capability
& WLAN_CAPABILITY_IBSS
))
287 /* different channel */
288 if (bss
->cbss
.channel
!= local
->oper_channel
)
292 if (elems
->ssid_len
!= sdata
->u
.ibss
.ssid_len
||
293 memcmp(elems
->ssid
, sdata
->u
.ibss
.ssid
,
294 sdata
->u
.ibss
.ssid_len
))
298 if (memcmp(bss
->cbss
.bssid
, sdata
->u
.ibss
.bssid
, ETH_ALEN
) == 0)
301 if (rx_status
->flag
& RX_FLAG_TSFT
) {
303 * For correct IBSS merging we need mactime; since mactime is
304 * defined as the time the first data symbol of the frame hits
305 * the PHY, and the timestamp of the beacon is defined as "the
306 * time that the data symbol containing the first bit of the
307 * timestamp is transmitted to the PHY plus the transmitting
308 * STA's delays through its local PHY from the MAC-PHY
309 * interface to its interface with the WM" (802.11 11.1.2)
310 * - equals the time this bit arrives at the receiver - we have
311 * to take into account the offset between the two.
313 * E.g. at 1 MBit that means mactime is 192 usec earlier
314 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
318 if (rx_status
->flag
& RX_FLAG_HT
)
319 rate
= 65; /* TODO: HT rates */
321 rate
= local
->hw
.wiphy
->bands
[band
]->
322 bitrates
[rx_status
->rate_idx
].bitrate
;
324 rx_timestamp
= rx_status
->mactime
+ (24 * 8 * 10 / rate
);
325 } else if (local
&& local
->ops
&& local
->ops
->get_tsf
)
326 /* second best option: get current TSF */
327 rx_timestamp
= local
->ops
->get_tsf(local_to_hw(local
));
329 /* can't merge without knowing the TSF */
330 rx_timestamp
= -1LLU;
332 #ifdef CONFIG_MAC80211_IBSS_DEBUG
333 printk(KERN_DEBUG
"RX beacon SA=%pM BSSID="
334 "%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
335 mgmt
->sa
, mgmt
->bssid
,
336 (unsigned long long)rx_timestamp
,
337 (unsigned long long)beacon_timestamp
,
338 (unsigned long long)(rx_timestamp
- beacon_timestamp
),
342 /* give slow hardware some time to do the TSF sync */
343 if (rx_timestamp
< IEEE80211_IBSS_MERGE_DELAY
)
346 if (beacon_timestamp
> rx_timestamp
) {
347 #ifdef CONFIG_MAC80211_IBSS_DEBUG
348 printk(KERN_DEBUG
"%s: beacon TSF higher than "
349 "local TSF - IBSS merge with BSSID %pM\n",
350 sdata
->dev
->name
, mgmt
->bssid
);
352 ieee80211_sta_join_ibss(sdata
, bss
);
353 ieee80211_ibss_add_sta(sdata
, mgmt
->bssid
, mgmt
->sa
, supp_rates
);
357 ieee80211_rx_bss_put(local
, bss
);
361 * Add a new IBSS station, will also be called by the RX code when,
362 * in IBSS mode, receiving a frame from a yet-unknown station, hence
363 * must be callable in atomic context.
365 struct sta_info
*ieee80211_ibss_add_sta(struct ieee80211_sub_if_data
*sdata
,
366 u8
*bssid
,u8
*addr
, u32 supp_rates
)
368 struct ieee80211_local
*local
= sdata
->local
;
369 struct sta_info
*sta
;
370 int band
= local
->hw
.conf
.channel
->band
;
372 /* TODO: Could consider removing the least recently used entry and
373 * allow new one to be added. */
374 if (local
->num_sta
>= IEEE80211_IBSS_MAX_STA_ENTRIES
) {
375 if (net_ratelimit()) {
376 printk(KERN_DEBUG
"%s: No room for a new IBSS STA "
377 "entry %pM\n", sdata
->dev
->name
, addr
);
382 if (compare_ether_addr(bssid
, sdata
->u
.ibss
.bssid
))
385 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
386 printk(KERN_DEBUG
"%s: Adding new IBSS station %pM (dev=%s)\n",
387 wiphy_name(local
->hw
.wiphy
), addr
, sdata
->dev
->name
);
390 sta
= sta_info_alloc(sdata
, addr
, GFP_ATOMIC
);
394 set_sta_flags(sta
, WLAN_STA_AUTHORIZED
);
396 /* make sure mandatory rates are always added */
397 sta
->sta
.supp_rates
[band
] = supp_rates
|
398 ieee80211_mandatory_rates(local
, band
);
400 rate_control_rate_init(sta
);
402 if (sta_info_insert(sta
))
408 static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data
*sdata
)
410 struct ieee80211_local
*local
= sdata
->local
;
412 struct sta_info
*sta
;
416 list_for_each_entry_rcu(sta
, &local
->sta_list
, list
) {
417 if (sta
->sdata
== sdata
&&
418 time_after(sta
->last_rx
+ IEEE80211_IBSS_MERGE_INTERVAL
,
431 static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data
*sdata
)
433 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
435 mod_timer(&ifibss
->timer
, jiffies
+ IEEE80211_IBSS_MERGE_INTERVAL
);
437 ieee80211_sta_expire(sdata
, IEEE80211_IBSS_INACTIVITY_LIMIT
);
438 if (ieee80211_sta_active_ibss(sdata
))
441 if ((ifibss
->flags
& IEEE80211_IBSS_BSSID_SET
) &&
442 (!(ifibss
->flags
& IEEE80211_IBSS_AUTO_CHANNEL_SEL
)))
445 printk(KERN_DEBUG
"%s: No active IBSS STAs - trying to scan for other "
446 "IBSS networks with same SSID (merge)\n", sdata
->dev
->name
);
448 /* XXX maybe racy? */
449 if (sdata
->local
->scan_req
)
452 memcpy(sdata
->local
->int_scan_req
.ssids
[0].ssid
,
453 ifibss
->ssid
, IEEE80211_MAX_SSID_LEN
);
454 sdata
->local
->int_scan_req
.ssids
[0].ssid_len
= ifibss
->ssid_len
;
455 ieee80211_request_scan(sdata
, &sdata
->local
->int_scan_req
);
458 static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data
*sdata
)
460 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
461 struct ieee80211_local
*local
= sdata
->local
;
462 struct ieee80211_supported_band
*sband
;
465 u8 supp_rates
[IEEE80211_MAX_SUPP_RATES
];
469 if (ifibss
->flags
& IEEE80211_IBSS_BSSID_SET
) {
470 memcpy(bssid
, ifibss
->bssid
, ETH_ALEN
);
472 /* Generate random, not broadcast, locally administered BSSID. Mix in
473 * own MAC address to make sure that devices that do not have proper
474 * random number generator get different BSSID. */
475 get_random_bytes(bssid
, ETH_ALEN
);
476 for (i
= 0; i
< ETH_ALEN
; i
++)
477 bssid
[i
] ^= sdata
->dev
->dev_addr
[i
];
482 printk(KERN_DEBUG
"%s: Creating new IBSS network, BSSID %pM\n",
483 sdata
->dev
->name
, bssid
);
485 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
487 if (local
->hw
.conf
.beacon_int
== 0)
488 local
->hw
.conf
.beacon_int
= 100;
490 capability
= WLAN_CAPABILITY_IBSS
;
492 if (sdata
->default_key
)
493 capability
|= WLAN_CAPABILITY_PRIVACY
;
495 sdata
->drop_unencrypted
= 0;
498 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
499 int rate
= sband
->bitrates
[i
].bitrate
;
500 *pos
++ = (u8
) (rate
/ 5);
503 return __ieee80211_sta_join_ibss(sdata
,
504 bssid
, local
->hw
.conf
.beacon_int
,
505 local
->hw
.conf
.channel
->center_freq
,
506 sband
->n_bitrates
, supp_rates
,
510 static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data
*sdata
)
512 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
513 struct ieee80211_local
*local
= sdata
->local
;
514 struct ieee80211_bss
*bss
;
515 const u8
*bssid
= NULL
;
518 if (ifibss
->ssid_len
== 0)
521 active_ibss
= ieee80211_sta_active_ibss(sdata
);
522 #ifdef CONFIG_MAC80211_IBSS_DEBUG
523 printk(KERN_DEBUG
"%s: sta_find_ibss (active_ibss=%d)\n",
524 sdata
->dev
->name
, active_ibss
);
525 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
530 if (ifibss
->flags
& IEEE80211_IBSS_BSSID_SET
)
531 bssid
= ifibss
->bssid
;
532 bss
= (void *)cfg80211_get_bss(local
->hw
.wiphy
, NULL
, bssid
,
533 ifibss
->ssid
, ifibss
->ssid_len
,
534 WLAN_CAPABILITY_IBSS
,
535 WLAN_CAPABILITY_IBSS
);
537 #ifdef CONFIG_MAC80211_IBSS_DEBUG
539 printk(KERN_DEBUG
" sta_find_ibss: selected %pM current "
540 "%pM\n", bss
->cbss
.bssid
, ifibss
->bssid
);
541 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
544 (!(ifibss
->flags
& IEEE80211_IBSS_PREV_BSSID_SET
) ||
545 memcmp(ifibss
->bssid
, bss
->cbss
.bssid
, ETH_ALEN
))) {
548 printk(KERN_DEBUG
"%s: Selected IBSS BSSID %pM"
549 " based on configured SSID\n",
550 sdata
->dev
->name
, bss
->cbss
.bssid
);
552 ret
= ieee80211_sta_join_ibss(sdata
, bss
);
553 ieee80211_rx_bss_put(local
, bss
);
556 ieee80211_rx_bss_put(local
, bss
);
558 #ifdef CONFIG_MAC80211_IBSS_DEBUG
559 printk(KERN_DEBUG
" did not try to join ibss\n");
560 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
562 /* Selected IBSS not found in current scan results - try to scan */
563 if (ifibss
->state
== IEEE80211_IBSS_MLME_JOINED
&&
564 !ieee80211_sta_active_ibss(sdata
)) {
565 mod_timer(&ifibss
->timer
, jiffies
+
566 IEEE80211_IBSS_MERGE_INTERVAL
);
567 } else if (time_after(jiffies
, local
->last_scan_completed
+
568 IEEE80211_SCAN_INTERVAL
)) {
569 printk(KERN_DEBUG
"%s: Trigger new scan to find an IBSS to "
570 "join\n", sdata
->dev
->name
);
572 /* XXX maybe racy? */
576 memcpy(local
->int_scan_req
.ssids
[0].ssid
,
577 ifibss
->ssid
, IEEE80211_MAX_SSID_LEN
);
578 local
->int_scan_req
.ssids
[0].ssid_len
= ifibss
->ssid_len
;
579 return ieee80211_request_scan(sdata
, &local
->int_scan_req
);
580 } else if (ifibss
->state
!= IEEE80211_IBSS_MLME_JOINED
) {
581 int interval
= IEEE80211_SCAN_INTERVAL
;
583 if (time_after(jiffies
, ifibss
->ibss_join_req
+
584 IEEE80211_IBSS_JOIN_TIMEOUT
)) {
585 if (!(local
->oper_channel
->flags
&
586 IEEE80211_CHAN_NO_IBSS
))
587 return ieee80211_sta_create_ibss(sdata
);
588 printk(KERN_DEBUG
"%s: IBSS not allowed on"
589 " %d MHz\n", sdata
->dev
->name
,
590 local
->hw
.conf
.channel
->center_freq
);
592 /* No IBSS found - decrease scan interval and continue
594 interval
= IEEE80211_SCAN_INTERVAL_SLOW
;
597 ifibss
->state
= IEEE80211_IBSS_MLME_SEARCH
;
598 mod_timer(&ifibss
->timer
, jiffies
+ interval
);
605 static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data
*sdata
,
606 struct ieee80211_mgmt
*mgmt
,
609 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
610 struct ieee80211_local
*local
= sdata
->local
;
613 struct ieee80211_mgmt
*resp
;
616 if (ifibss
->state
!= IEEE80211_IBSS_MLME_JOINED
||
617 len
< 24 + 2 || !ifibss
->probe_resp
)
620 if (local
->ops
->tx_last_beacon
)
621 tx_last_beacon
= local
->ops
->tx_last_beacon(local_to_hw(local
));
625 #ifdef CONFIG_MAC80211_IBSS_DEBUG
626 printk(KERN_DEBUG
"%s: RX ProbeReq SA=%pM DA=%pM BSSID=%pM"
627 " (tx_last_beacon=%d)\n",
628 sdata
->dev
->name
, mgmt
->sa
, mgmt
->da
,
629 mgmt
->bssid
, tx_last_beacon
);
630 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
635 if (memcmp(mgmt
->bssid
, ifibss
->bssid
, ETH_ALEN
) != 0 &&
636 memcmp(mgmt
->bssid
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) != 0)
639 end
= ((u8
*) mgmt
) + len
;
640 pos
= mgmt
->u
.probe_req
.variable
;
641 if (pos
[0] != WLAN_EID_SSID
||
642 pos
+ 2 + pos
[1] > end
) {
643 #ifdef CONFIG_MAC80211_IBSS_DEBUG
644 printk(KERN_DEBUG
"%s: Invalid SSID IE in ProbeReq "
646 sdata
->dev
->name
, mgmt
->sa
);
651 (pos
[1] != ifibss
->ssid_len
||
652 memcmp(pos
+ 2, ifibss
->ssid
, ifibss
->ssid_len
) != 0)) {
653 /* Ignore ProbeReq for foreign SSID */
657 /* Reply with ProbeResp */
658 skb
= skb_copy(ifibss
->probe_resp
, GFP_KERNEL
);
662 resp
= (struct ieee80211_mgmt
*) skb
->data
;
663 memcpy(resp
->da
, mgmt
->sa
, ETH_ALEN
);
664 #ifdef CONFIG_MAC80211_IBSS_DEBUG
665 printk(KERN_DEBUG
"%s: Sending ProbeResp to %pM\n",
666 sdata
->dev
->name
, resp
->da
);
667 #endif /* CONFIG_MAC80211_IBSS_DEBUG */
668 ieee80211_tx_skb(sdata
, skb
, 0);
671 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data
*sdata
,
672 struct ieee80211_mgmt
*mgmt
,
674 struct ieee80211_rx_status
*rx_status
)
677 struct ieee802_11_elems elems
;
679 if (memcmp(mgmt
->da
, sdata
->dev
->dev_addr
, ETH_ALEN
))
680 return; /* ignore ProbeResp to foreign address */
682 baselen
= (u8
*) mgmt
->u
.probe_resp
.variable
- (u8
*) mgmt
;
686 ieee802_11_parse_elems(mgmt
->u
.probe_resp
.variable
, len
- baselen
,
689 ieee80211_rx_bss_info(sdata
, mgmt
, len
, rx_status
, &elems
, false);
692 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data
*sdata
,
693 struct ieee80211_mgmt
*mgmt
,
695 struct ieee80211_rx_status
*rx_status
)
698 struct ieee802_11_elems elems
;
700 /* Process beacon from the current BSS */
701 baselen
= (u8
*) mgmt
->u
.beacon
.variable
- (u8
*) mgmt
;
705 ieee802_11_parse_elems(mgmt
->u
.beacon
.variable
, len
- baselen
, &elems
);
707 ieee80211_rx_bss_info(sdata
, mgmt
, len
, rx_status
, &elems
, true);
710 static void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data
*sdata
,
713 struct ieee80211_rx_status
*rx_status
;
714 struct ieee80211_mgmt
*mgmt
;
717 rx_status
= (struct ieee80211_rx_status
*) skb
->cb
;
718 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
719 fc
= le16_to_cpu(mgmt
->frame_control
);
721 switch (fc
& IEEE80211_FCTL_STYPE
) {
722 case IEEE80211_STYPE_PROBE_REQ
:
723 ieee80211_rx_mgmt_probe_req(sdata
, mgmt
, skb
->len
);
725 case IEEE80211_STYPE_PROBE_RESP
:
726 ieee80211_rx_mgmt_probe_resp(sdata
, mgmt
, skb
->len
,
729 case IEEE80211_STYPE_BEACON
:
730 ieee80211_rx_mgmt_beacon(sdata
, mgmt
, skb
->len
,
733 case IEEE80211_STYPE_AUTH
:
734 ieee80211_rx_mgmt_auth_ibss(sdata
, mgmt
, skb
->len
);
741 static void ieee80211_ibss_work(struct work_struct
*work
)
743 struct ieee80211_sub_if_data
*sdata
=
744 container_of(work
, struct ieee80211_sub_if_data
, u
.ibss
.work
);
745 struct ieee80211_local
*local
= sdata
->local
;
746 struct ieee80211_if_ibss
*ifibss
;
749 if (!netif_running(sdata
->dev
))
752 if (local
->sw_scanning
|| local
->hw_scanning
)
755 if (WARN_ON(sdata
->vif
.type
!= NL80211_IFTYPE_ADHOC
))
757 ifibss
= &sdata
->u
.ibss
;
759 while ((skb
= skb_dequeue(&ifibss
->skb_queue
)))
760 ieee80211_ibss_rx_queued_mgmt(sdata
, skb
);
762 if (!test_and_clear_bit(IEEE80211_IBSS_REQ_RUN
, &ifibss
->request
))
765 switch (ifibss
->state
) {
766 case IEEE80211_IBSS_MLME_SEARCH
:
767 ieee80211_sta_find_ibss(sdata
);
769 case IEEE80211_IBSS_MLME_JOINED
:
770 ieee80211_sta_merge_ibss(sdata
);
778 static void ieee80211_ibss_timer(unsigned long data
)
780 struct ieee80211_sub_if_data
*sdata
=
781 (struct ieee80211_sub_if_data
*) data
;
782 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
783 struct ieee80211_local
*local
= sdata
->local
;
785 set_bit(IEEE80211_IBSS_REQ_RUN
, &ifibss
->request
);
786 queue_work(local
->hw
.workqueue
, &ifibss
->work
);
789 void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data
*sdata
)
791 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
793 INIT_WORK(&ifibss
->work
, ieee80211_ibss_work
);
794 setup_timer(&ifibss
->timer
, ieee80211_ibss_timer
,
795 (unsigned long) sdata
);
796 skb_queue_head_init(&ifibss
->skb_queue
);
798 ifibss
->flags
|= IEEE80211_IBSS_AUTO_BSSID_SEL
|
799 IEEE80211_IBSS_AUTO_CHANNEL_SEL
;
802 int ieee80211_ibss_commit(struct ieee80211_sub_if_data
*sdata
)
804 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
806 ifibss
->flags
&= ~IEEE80211_IBSS_PREV_BSSID_SET
;
808 if (ifibss
->ssid_len
)
809 ifibss
->flags
|= IEEE80211_IBSS_SSID_SET
;
811 ifibss
->flags
&= ~IEEE80211_IBSS_SSID_SET
;
813 ifibss
->ibss_join_req
= jiffies
;
814 ifibss
->state
= IEEE80211_IBSS_MLME_SEARCH
;
815 set_bit(IEEE80211_IBSS_REQ_RUN
, &ifibss
->request
);
820 int ieee80211_ibss_set_ssid(struct ieee80211_sub_if_data
*sdata
, char *ssid
, size_t len
)
822 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
824 if (len
> IEEE80211_MAX_SSID_LEN
)
827 if (ifibss
->ssid_len
!= len
|| memcmp(ifibss
->ssid
, ssid
, len
) != 0) {
828 memset(ifibss
->ssid
, 0, sizeof(ifibss
->ssid
));
829 memcpy(ifibss
->ssid
, ssid
, len
);
830 ifibss
->ssid_len
= len
;
833 return ieee80211_ibss_commit(sdata
);
836 int ieee80211_ibss_get_ssid(struct ieee80211_sub_if_data
*sdata
, char *ssid
, size_t *len
)
838 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
840 memcpy(ssid
, ifibss
->ssid
, ifibss
->ssid_len
);
841 *len
= ifibss
->ssid_len
;
846 int ieee80211_ibss_set_bssid(struct ieee80211_sub_if_data
*sdata
, u8
*bssid
)
848 struct ieee80211_if_ibss
*ifibss
= &sdata
->u
.ibss
;
850 if (is_valid_ether_addr(bssid
)) {
851 memcpy(ifibss
->bssid
, bssid
, ETH_ALEN
);
852 ifibss
->flags
|= IEEE80211_IBSS_BSSID_SET
;
854 memset(ifibss
->bssid
, 0, ETH_ALEN
);
855 ifibss
->flags
&= ~IEEE80211_IBSS_BSSID_SET
;
858 if (netif_running(sdata
->dev
)) {
859 if (ieee80211_if_config(sdata
, IEEE80211_IFCC_BSSID
)) {
860 printk(KERN_DEBUG
"%s: Failed to config new BSSID to "
861 "the low-level driver\n", sdata
->dev
->name
);
865 return ieee80211_ibss_commit(sdata
);
868 /* scan finished notification */
869 void ieee80211_ibss_notify_scan_completed(struct ieee80211_local
*local
)
871 struct ieee80211_sub_if_data
*sdata
= local
->scan_sdata
;
872 struct ieee80211_if_ibss
*ifibss
;
874 if (sdata
&& sdata
->vif
.type
== NL80211_IFTYPE_ADHOC
) {
875 ifibss
= &sdata
->u
.ibss
;
876 if ((!(ifibss
->flags
& IEEE80211_IBSS_PREV_BSSID_SET
)) ||
877 !ieee80211_sta_active_ibss(sdata
))
878 ieee80211_sta_find_ibss(sdata
);
883 ieee80211_ibss_rx_mgmt(struct ieee80211_sub_if_data
*sdata
, struct sk_buff
*skb
,
884 struct ieee80211_rx_status
*rx_status
)
886 struct ieee80211_local
*local
= sdata
->local
;
887 struct ieee80211_mgmt
*mgmt
;
891 return RX_DROP_MONITOR
;
893 mgmt
= (struct ieee80211_mgmt
*) skb
->data
;
894 fc
= le16_to_cpu(mgmt
->frame_control
);
896 switch (fc
& IEEE80211_FCTL_STYPE
) {
897 case IEEE80211_STYPE_PROBE_RESP
:
898 case IEEE80211_STYPE_BEACON
:
899 memcpy(skb
->cb
, rx_status
, sizeof(*rx_status
));
900 case IEEE80211_STYPE_PROBE_REQ
:
901 case IEEE80211_STYPE_AUTH
:
902 skb_queue_tail(&sdata
->u
.ibss
.skb_queue
, skb
);
903 queue_work(local
->hw
.workqueue
, &sdata
->u
.ibss
.work
);
907 return RX_DROP_MONITOR
;