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