]>
Commit | Line | Data |
---|---|---|
46900298 JB |
1 | /* |
2 | * IBSS mode implementation | |
3 | * Copyright 2003-2008, Jouni Malinen <[email protected]> | |
4 | * Copyright 2004, Instant802 Networks, Inc. | |
5 | * Copyright 2005, Devicescape Software, Inc. | |
6 | * Copyright 2006-2007 Jiri Benc <[email protected]> | |
7 | * Copyright 2007, Michael Wu <[email protected]> | |
8 | * Copyright 2009, Johannes Berg <[email protected]> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | */ | |
14 | ||
15 | #include <linux/delay.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
46900298 JB |
17 | #include <linux/if_ether.h> |
18 | #include <linux/skbuff.h> | |
19 | #include <linux/if_arp.h> | |
20 | #include <linux/etherdevice.h> | |
21 | #include <linux/rtnetlink.h> | |
22 | #include <net/mac80211.h> | |
46900298 JB |
23 | |
24 | #include "ieee80211_i.h" | |
24487981 | 25 | #include "driver-ops.h" |
46900298 JB |
26 | #include "rate.h" |
27 | ||
28 | #define IEEE80211_SCAN_INTERVAL (2 * HZ) | |
46900298 JB |
29 | #define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ) |
30 | ||
31 | #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ) | |
32 | #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ) | |
dad9defd | 33 | #define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ) |
46900298 JB |
34 | |
35 | #define IEEE80211_IBSS_MAX_STA_ENTRIES 128 | |
36 | ||
37 | ||
af8cdcd8 JB |
38 | static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata, |
39 | const u8 *bssid, const int beacon_int, | |
40 | struct ieee80211_channel *chan, | |
b59066a2 | 41 | const u32 basic_rates, |
c13a765b SM |
42 | const u16 capability, u64 tsf, |
43 | bool creator) | |
46900298 JB |
44 | { |
45 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; | |
46 | struct ieee80211_local *local = sdata->local; | |
2103dec1 | 47 | int rates_n = 0, i, ri; |
46900298 JB |
48 | struct ieee80211_mgmt *mgmt; |
49 | u8 *pos; | |
50 | struct ieee80211_supported_band *sband; | |
f446d10f | 51 | struct cfg80211_bss *bss; |
2103dec1 | 52 | u32 bss_change, rate_flags, rates = 0, rates_added = 0; |
683b6d3b | 53 | struct cfg80211_chan_def chandef; |
7ca15a0a | 54 | enum nl80211_bss_scan_width scan_width; |
2ec9c1f6 | 55 | bool have_higher_than_11mbit = false; |
c3ffeab4 JB |
56 | struct beacon_data *presp; |
57 | int frame_len; | |
2103dec1 | 58 | int shift; |
24487981 | 59 | |
8d61ffa5 | 60 | sdata_assert_lock(sdata); |
7a17a33c | 61 | |
24487981 | 62 | /* Reset own TSF to allow time synchronization work. */ |
37a41b4a | 63 | drv_reset_tsf(local, sdata); |
46900298 | 64 | |
b203ca39 | 65 | if (!ether_addr_equal(ifibss->bssid, bssid)) |
b998e8bb | 66 | sta_info_flush(sdata); |
46900298 | 67 | |
49b5c7f4 JB |
68 | /* if merging, indicate to driver that we leave the old IBSS */ |
69 | if (sdata->vif.bss_conf.ibss_joined) { | |
70 | sdata->vif.bss_conf.ibss_joined = false; | |
c13a765b | 71 | sdata->vif.bss_conf.ibss_creator = false; |
1852d40e | 72 | sdata->vif.bss_conf.enable_beacon = false; |
86a2ea41 | 73 | netif_carrier_off(sdata->dev); |
1852d40e JB |
74 | ieee80211_bss_info_change_notify(sdata, |
75 | BSS_CHANGED_IBSS | | |
76 | BSS_CHANGED_BEACON_ENABLED); | |
49b5c7f4 JB |
77 | } |
78 | ||
c3ffeab4 | 79 | presp = rcu_dereference_protected(ifibss->presp, |
8d61ffa5 | 80 | lockdep_is_held(&sdata->wdev.mtx)); |
c3ffeab4 JB |
81 | rcu_assign_pointer(ifibss->presp, NULL); |
82 | if (presp) | |
83 | kfree_rcu(presp, rcu_head); | |
1852d40e | 84 | |
af8cdcd8 | 85 | sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0; |
46900298 | 86 | |
3aede78a | 87 | chandef = ifibss->chandef; |
4bf88530 | 88 | if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef)) { |
4b42aab1 SW |
89 | if (chandef.width == NL80211_CHAN_WIDTH_5 || |
90 | chandef.width == NL80211_CHAN_WIDTH_10 || | |
91 | chandef.width == NL80211_CHAN_WIDTH_20_NOHT || | |
92 | chandef.width == NL80211_CHAN_WIDTH_20) { | |
93 | sdata_info(sdata, | |
94 | "Failed to join IBSS, beacons forbidden\n"); | |
95 | return; | |
96 | } | |
4bf88530 JB |
97 | chandef.width = NL80211_CHAN_WIDTH_20; |
98 | chandef.center_freq1 = chan->center_freq; | |
99 | } | |
55de908a JB |
100 | |
101 | ieee80211_vif_release_channel(sdata); | |
4bf88530 | 102 | if (ieee80211_vif_use_channel(sdata, &chandef, |
55de908a JB |
103 | ifibss->fixed_channel ? |
104 | IEEE80211_CHANCTX_SHARED : | |
105 | IEEE80211_CHANCTX_EXCLUSIVE)) { | |
106 | sdata_info(sdata, "Failed to join IBSS, no channel context\n"); | |
107 | return; | |
13c40c54 | 108 | } |
55de908a JB |
109 | |
110 | memcpy(ifibss->bssid, bssid, ETH_ALEN); | |
57c4d7b4 | 111 | |
af8cdcd8 | 112 | sband = local->hw.wiphy->bands[chan->band]; |
2103dec1 | 113 | shift = ieee80211_vif_get_shift(&sdata->vif); |
46900298 JB |
114 | |
115 | /* Build IBSS probe response */ | |
c3ffeab4 JB |
116 | frame_len = sizeof(struct ieee80211_hdr_3addr) + |
117 | 12 /* struct ieee80211_mgmt.u.beacon */ + | |
118 | 2 + IEEE80211_MAX_SSID_LEN /* max SSID */ + | |
119 | 2 + 8 /* max Supported Rates */ + | |
120 | 3 /* max DS params */ + | |
121 | 4 /* IBSS params */ + | |
122 | 2 + (IEEE80211_MAX_SUPP_RATES - 8) + | |
123 | 2 + sizeof(struct ieee80211_ht_cap) + | |
124 | 2 + sizeof(struct ieee80211_ht_operation) + | |
125 | ifibss->ie_len; | |
126 | presp = kzalloc(sizeof(*presp) + frame_len, GFP_KERNEL); | |
127 | if (!presp) | |
128 | return; | |
129 | ||
130 | presp->head = (void *)(presp + 1); | |
131 | ||
132 | mgmt = (void *) presp->head; | |
46900298 JB |
133 | mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | |
134 | IEEE80211_STYPE_PROBE_RESP); | |
e83e6541 | 135 | eth_broadcast_addr(mgmt->da); |
47846c9b | 136 | memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); |
46900298 | 137 | memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN); |
57c4d7b4 | 138 | mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_int); |
707c1b4e | 139 | mgmt->u.beacon.timestamp = cpu_to_le64(tsf); |
46900298 JB |
140 | mgmt->u.beacon.capab_info = cpu_to_le16(capability); |
141 | ||
c3ffeab4 JB |
142 | pos = (u8 *)mgmt + offsetof(struct ieee80211_mgmt, u.beacon.variable); |
143 | ||
46900298 JB |
144 | *pos++ = WLAN_EID_SSID; |
145 | *pos++ = ifibss->ssid_len; | |
146 | memcpy(pos, ifibss->ssid, ifibss->ssid_len); | |
c3ffeab4 | 147 | pos += ifibss->ssid_len; |
46900298 | 148 | |
2103dec1 SW |
149 | rate_flags = ieee80211_chandef_rate_flags(&chandef); |
150 | for (i = 0; i < sband->n_bitrates; i++) { | |
151 | if ((rate_flags & sband->bitrates[i].flags) != rate_flags) | |
152 | continue; | |
2ec9c1f6 SW |
153 | if (sband->bitrates[i].bitrate > 110) |
154 | have_higher_than_11mbit = true; | |
2103dec1 SW |
155 | |
156 | rates |= BIT(i); | |
157 | rates_n++; | |
158 | } | |
159 | ||
46900298 | 160 | *pos++ = WLAN_EID_SUPP_RATES; |
2103dec1 SW |
161 | *pos++ = min_t(int, 8, rates_n); |
162 | for (ri = 0; ri < sband->n_bitrates; ri++) { | |
163 | int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, | |
164 | 5 * (1 << shift)); | |
c3ffeab4 | 165 | u8 basic = 0; |
2103dec1 SW |
166 | if (!(rates & BIT(ri))) |
167 | continue; | |
168 | ||
169 | if (basic_rates & BIT(ri)) | |
c3ffeab4 | 170 | basic = 0x80; |
2103dec1 | 171 | *pos++ = basic | (u8) rate; |
93c75786 SW |
172 | if (++rates_added == 8) { |
173 | ri++; /* continue at next rate for EXT_SUPP_RATES */ | |
2103dec1 | 174 | break; |
93c75786 | 175 | } |
c3ffeab4 | 176 | } |
46900298 JB |
177 | |
178 | if (sband->band == IEEE80211_BAND_2GHZ) { | |
46900298 JB |
179 | *pos++ = WLAN_EID_DS_PARAMS; |
180 | *pos++ = 1; | |
af8cdcd8 | 181 | *pos++ = ieee80211_frequency_to_channel(chan->center_freq); |
46900298 JB |
182 | } |
183 | ||
46900298 JB |
184 | *pos++ = WLAN_EID_IBSS_PARAMS; |
185 | *pos++ = 2; | |
186 | /* FIX: set ATIM window based on scan results */ | |
187 | *pos++ = 0; | |
188 | *pos++ = 0; | |
189 | ||
2103dec1 SW |
190 | /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */ |
191 | if (rates_n > 8) { | |
46900298 | 192 | *pos++ = WLAN_EID_EXT_SUPP_RATES; |
2103dec1 SW |
193 | *pos++ = rates_n - 8; |
194 | for (; ri < sband->n_bitrates; ri++) { | |
195 | int rate = DIV_ROUND_UP(sband->bitrates[ri].bitrate, | |
196 | 5 * (1 << shift)); | |
c3ffeab4 | 197 | u8 basic = 0; |
2103dec1 SW |
198 | if (!(rates & BIT(ri))) |
199 | continue; | |
200 | ||
201 | if (basic_rates & BIT(ri)) | |
c3ffeab4 | 202 | basic = 0x80; |
2103dec1 | 203 | *pos++ = basic | (u8) rate; |
c3ffeab4 | 204 | } |
46900298 JB |
205 | } |
206 | ||
c3ffeab4 JB |
207 | if (ifibss->ie_len) { |
208 | memcpy(pos, ifibss->ie, ifibss->ie_len); | |
209 | pos += ifibss->ie_len; | |
210 | } | |
af8cdcd8 | 211 | |
13c40c54 | 212 | /* add HT capability and information IEs */ |
4bf88530 | 213 | if (chandef.width != NL80211_CHAN_WIDTH_20_NOHT && |
0418a445 SW |
214 | chandef.width != NL80211_CHAN_WIDTH_5 && |
215 | chandef.width != NL80211_CHAN_WIDTH_10 && | |
683b6d3b | 216 | sband->ht_cap.ht_supported) { |
822854b0 SW |
217 | struct ieee80211_sta_ht_cap ht_cap; |
218 | ||
219 | memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap)); | |
220 | ieee80211_apply_htcap_overrides(sdata, &ht_cap); | |
221 | ||
222 | pos = ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap); | |
0d894ec5 AN |
223 | /* |
224 | * Note: According to 802.11n-2009 9.13.3.1, HT Protection | |
225 | * field and RIFS Mode are reserved in IBSS mode, therefore | |
226 | * keep them at 0 | |
227 | */ | |
074d46d1 | 228 | pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap, |
4bf88530 | 229 | &chandef, 0); |
13c40c54 AS |
230 | } |
231 | ||
32c5057b | 232 | if (local->hw.queues >= IEEE80211_NUM_ACS) { |
9eba6125 BR |
233 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; |
234 | *pos++ = 7; /* len */ | |
235 | *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */ | |
236 | *pos++ = 0x50; | |
237 | *pos++ = 0xf2; | |
238 | *pos++ = 2; /* WME */ | |
239 | *pos++ = 0; /* WME info */ | |
240 | *pos++ = 1; /* WME ver */ | |
241 | *pos++ = 0; /* U-APSD no in use */ | |
242 | } | |
243 | ||
c3ffeab4 JB |
244 | presp->head_len = pos - presp->head; |
245 | if (WARN_ON(presp->head_len > frame_len)) | |
246 | return; | |
247 | ||
248 | rcu_assign_pointer(ifibss->presp, presp); | |
46900298 | 249 | |
d6a83228 | 250 | sdata->vif.bss_conf.enable_beacon = true; |
2d0ddec5 | 251 | sdata->vif.bss_conf.beacon_int = beacon_int; |
fbd2c8dc | 252 | sdata->vif.bss_conf.basic_rates = basic_rates; |
0ca54f6c MP |
253 | sdata->vif.bss_conf.ssid_len = ifibss->ssid_len; |
254 | memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len); | |
2d0ddec5 JB |
255 | bss_change = BSS_CHANGED_BEACON_INT; |
256 | bss_change |= ieee80211_reset_erp_info(sdata); | |
257 | bss_change |= BSS_CHANGED_BSSID; | |
258 | bss_change |= BSS_CHANGED_BEACON; | |
259 | bss_change |= BSS_CHANGED_BEACON_ENABLED; | |
392cfdb1 | 260 | bss_change |= BSS_CHANGED_BASIC_RATES; |
13c40c54 | 261 | bss_change |= BSS_CHANGED_HT; |
8fc214ba | 262 | bss_change |= BSS_CHANGED_IBSS; |
0ca54f6c | 263 | bss_change |= BSS_CHANGED_SSID; |
2f91a967 SW |
264 | |
265 | /* | |
266 | * In 5 GHz/802.11a, we can always use short slot time. | |
267 | * (IEEE 802.11-2012 18.3.8.7) | |
268 | * | |
269 | * In 2.4GHz, we must always use long slots in IBSS for compatibility | |
270 | * reasons. | |
271 | * (IEEE 802.11-2012 19.4.5) | |
272 | * | |
273 | * HT follows these specifications (IEEE 802.11-2012 20.3.18) | |
274 | */ | |
275 | sdata->vif.bss_conf.use_short_slot = chan->band == IEEE80211_BAND_5GHZ; | |
276 | bss_change |= BSS_CHANGED_ERP_SLOT; | |
277 | ||
2ec9c1f6 SW |
278 | /* cf. IEEE 802.11 9.2.12 */ |
279 | if (chan->band == IEEE80211_BAND_2GHZ && have_higher_than_11mbit) | |
280 | sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE; | |
281 | else | |
282 | sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE; | |
283 | ||
8fc214ba | 284 | sdata->vif.bss_conf.ibss_joined = true; |
c13a765b | 285 | sdata->vif.bss_conf.ibss_creator = creator; |
2d0ddec5 | 286 | ieee80211_bss_info_change_notify(sdata, bss_change); |
46900298 | 287 | |
2ec9c1f6 | 288 | ieee80211_set_wmm_default(sdata, true); |
46900298 | 289 | |
46900298 | 290 | ifibss->state = IEEE80211_IBSS_MLME_JOINED; |
af8cdcd8 JB |
291 | mod_timer(&ifibss->timer, |
292 | round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL)); | |
46900298 | 293 | |
7ca15a0a SW |
294 | scan_width = cfg80211_chandef_to_scan_width(&chandef); |
295 | bss = cfg80211_inform_bss_width_frame(local->hw.wiphy, chan, | |
296 | scan_width, mgmt, | |
297 | presp->head_len, 0, GFP_KERNEL); | |
5b112d3d | 298 | cfg80211_put_bss(local->hw.wiphy, bss); |
86a2ea41 | 299 | netif_carrier_on(sdata->dev); |
af8cdcd8 | 300 | cfg80211_ibss_joined(sdata->dev, ifibss->bssid, GFP_KERNEL); |
46900298 JB |
301 | } |
302 | ||
af8cdcd8 JB |
303 | static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata, |
304 | struct ieee80211_bss *bss) | |
46900298 | 305 | { |
0c1ad2ca JB |
306 | struct cfg80211_bss *cbss = |
307 | container_of((void *)bss, struct cfg80211_bss, priv); | |
b59066a2 JB |
308 | struct ieee80211_supported_band *sband; |
309 | u32 basic_rates; | |
310 | int i, j; | |
0c1ad2ca | 311 | u16 beacon_int = cbss->beacon_interval; |
8cef2c9d JB |
312 | const struct cfg80211_bss_ies *ies; |
313 | u64 tsf; | |
2103dec1 SW |
314 | u32 rate_flags; |
315 | int shift; | |
57c4d7b4 | 316 | |
8d61ffa5 | 317 | sdata_assert_lock(sdata); |
7a17a33c | 318 | |
57c4d7b4 JB |
319 | if (beacon_int < 10) |
320 | beacon_int = 10; | |
321 | ||
0c1ad2ca | 322 | sband = sdata->local->hw.wiphy->bands[cbss->channel->band]; |
2103dec1 SW |
323 | rate_flags = ieee80211_chandef_rate_flags(&sdata->u.ibss.chandef); |
324 | shift = ieee80211_vif_get_shift(&sdata->vif); | |
b59066a2 JB |
325 | |
326 | basic_rates = 0; | |
327 | ||
328 | for (i = 0; i < bss->supp_rates_len; i++) { | |
2103dec1 | 329 | int rate = bss->supp_rates[i] & 0x7f; |
b59066a2 JB |
330 | bool is_basic = !!(bss->supp_rates[i] & 0x80); |
331 | ||
332 | for (j = 0; j < sband->n_bitrates; j++) { | |
2103dec1 SW |
333 | int brate; |
334 | if ((rate_flags & sband->bitrates[j].flags) | |
335 | != rate_flags) | |
336 | continue; | |
337 | ||
338 | brate = DIV_ROUND_UP(sband->bitrates[j].bitrate, | |
339 | 5 * (1 << shift)); | |
340 | if (brate == rate) { | |
b59066a2 JB |
341 | if (is_basic) |
342 | basic_rates |= BIT(j); | |
343 | break; | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
8cef2c9d JB |
348 | rcu_read_lock(); |
349 | ies = rcu_dereference(cbss->ies); | |
350 | tsf = ies->tsf; | |
351 | rcu_read_unlock(); | |
352 | ||
0c1ad2ca | 353 | __ieee80211_sta_join_ibss(sdata, cbss->bssid, |
57c4d7b4 | 354 | beacon_int, |
0c1ad2ca | 355 | cbss->channel, |
b59066a2 | 356 | basic_rates, |
0c1ad2ca | 357 | cbss->capability, |
8cef2c9d | 358 | tsf, false); |
46900298 JB |
359 | } |
360 | ||
52874a5e | 361 | static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta) |
8bf11d8d JB |
362 | __acquires(RCU) |
363 | { | |
364 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
365 | u8 addr[ETH_ALEN]; | |
366 | ||
367 | memcpy(addr, sta->sta.addr, ETH_ALEN); | |
368 | ||
bdcbd8e0 | 369 | ibss_dbg(sdata, "Adding new IBSS station %pM\n", addr); |
8bf11d8d | 370 | |
83d5cc01 JB |
371 | sta_info_pre_move_state(sta, IEEE80211_STA_AUTH); |
372 | sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC); | |
267335d6 AQ |
373 | /* authorize the station only if the network is not RSN protected. If |
374 | * not wait for the userspace to authorize it */ | |
375 | if (!sta->sdata->u.ibss.control_port) | |
376 | sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED); | |
8bf11d8d JB |
377 | |
378 | rate_control_rate_init(sta); | |
379 | ||
380 | /* If it fails, maybe we raced another insertion? */ | |
381 | if (sta_info_insert_rcu(sta)) | |
382 | return sta_info_get(sdata, addr); | |
383 | return sta; | |
384 | } | |
385 | ||
386 | static struct sta_info * | |
52874a5e AQ |
387 | ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid, |
388 | const u8 *addr, u32 supp_rates) | |
8bf11d8d JB |
389 | __acquires(RCU) |
390 | { | |
391 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; | |
392 | struct ieee80211_local *local = sdata->local; | |
393 | struct sta_info *sta; | |
55de908a | 394 | struct ieee80211_chanctx_conf *chanctx_conf; |
b422c6cd | 395 | struct ieee80211_supported_band *sband; |
74608aca | 396 | enum nl80211_bss_scan_width scan_width; |
55de908a | 397 | int band; |
8bf11d8d JB |
398 | |
399 | /* | |
400 | * XXX: Consider removing the least recently used entry and | |
401 | * allow new one to be added. | |
402 | */ | |
403 | if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) { | |
bdcbd8e0 | 404 | net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n", |
e87cc472 | 405 | sdata->name, addr); |
8bf11d8d JB |
406 | rcu_read_lock(); |
407 | return NULL; | |
408 | } | |
409 | ||
410 | if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) { | |
411 | rcu_read_lock(); | |
412 | return NULL; | |
413 | } | |
414 | ||
b203ca39 | 415 | if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) { |
8bf11d8d JB |
416 | rcu_read_lock(); |
417 | return NULL; | |
418 | } | |
419 | ||
55de908a JB |
420 | rcu_read_lock(); |
421 | chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); | |
422 | if (WARN_ON_ONCE(!chanctx_conf)) | |
423 | return NULL; | |
4bf88530 | 424 | band = chanctx_conf->def.chan->band; |
74608aca | 425 | scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def); |
55de908a JB |
426 | rcu_read_unlock(); |
427 | ||
8bf11d8d JB |
428 | sta = sta_info_alloc(sdata, addr, GFP_KERNEL); |
429 | if (!sta) { | |
430 | rcu_read_lock(); | |
431 | return NULL; | |
432 | } | |
433 | ||
434 | sta->last_rx = jiffies; | |
435 | ||
436 | /* make sure mandatory rates are always added */ | |
b422c6cd | 437 | sband = local->hw.wiphy->bands[band]; |
8bf11d8d | 438 | sta->sta.supp_rates[band] = supp_rates | |
74608aca | 439 | ieee80211_mandatory_rates(sband, scan_width); |
8bf11d8d | 440 | |
52874a5e | 441 | return ieee80211_ibss_finish_sta(sta); |
6d810f10 AQ |
442 | } |
443 | ||
2cc59e78 AQ |
444 | static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata, |
445 | struct ieee80211_mgmt *mgmt, | |
446 | size_t len) | |
447 | { | |
448 | u16 reason = le16_to_cpu(mgmt->u.deauth.reason_code); | |
449 | ||
450 | if (len < IEEE80211_DEAUTH_FRAME_LEN) | |
451 | return; | |
452 | ||
453 | ibss_dbg(sdata, "RX DeAuth SA=%pM DA=%pM BSSID=%pM (reason: %d)\n", | |
454 | mgmt->sa, mgmt->da, mgmt->bssid, reason); | |
455 | sta_info_destroy_addr(sdata, mgmt->sa); | |
456 | } | |
457 | ||
6d810f10 AQ |
458 | static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata, |
459 | struct ieee80211_mgmt *mgmt, | |
460 | size_t len) | |
461 | { | |
462 | u16 auth_alg, auth_transaction; | |
463 | ||
8d61ffa5 | 464 | sdata_assert_lock(sdata); |
6d810f10 AQ |
465 | |
466 | if (len < 24 + 6) | |
467 | return; | |
468 | ||
469 | auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg); | |
470 | auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction); | |
471 | ||
bdcbd8e0 JB |
472 | ibss_dbg(sdata, |
473 | "RX Auth SA=%pM DA=%pM BSSID=%pM (auth_transaction=%d)\n", | |
474 | mgmt->sa, mgmt->da, mgmt->bssid, auth_transaction); | |
7bed2050 AQ |
475 | |
476 | if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) | |
477 | return; | |
478 | ||
6d810f10 AQ |
479 | /* |
480 | * IEEE 802.11 standard does not require authentication in IBSS | |
481 | * networks and most implementations do not seem to use it. | |
482 | * However, try to reply to authentication attempts if someone | |
483 | * has actually implemented this. | |
484 | */ | |
700e8ea6 | 485 | ieee80211_send_auth(sdata, 2, WLAN_AUTH_OPEN, 0, NULL, 0, |
1672c0e3 | 486 | mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0); |
8bf11d8d JB |
487 | } |
488 | ||
46900298 | 489 | static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata, |
d45c4172 | 490 | struct ieee80211_mgmt *mgmt, size_t len, |
46900298 | 491 | struct ieee80211_rx_status *rx_status, |
d45c4172 | 492 | struct ieee802_11_elems *elems) |
46900298 JB |
493 | { |
494 | struct ieee80211_local *local = sdata->local; | |
495 | int freq; | |
0c1ad2ca | 496 | struct cfg80211_bss *cbss; |
46900298 JB |
497 | struct ieee80211_bss *bss; |
498 | struct sta_info *sta; | |
499 | struct ieee80211_channel *channel; | |
500 | u64 beacon_timestamp, rx_timestamp; | |
501 | u32 supp_rates = 0; | |
502 | enum ieee80211_band band = rx_status->band; | |
74608aca | 503 | enum nl80211_bss_scan_width scan_width; |
13c40c54 AS |
504 | struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band]; |
505 | bool rates_updated = false; | |
46900298 | 506 | |
1cd8e88e | 507 | if (elems->ds_params) |
59eb21a6 BR |
508 | freq = ieee80211_channel_to_frequency(elems->ds_params[0], |
509 | band); | |
46900298 JB |
510 | else |
511 | freq = rx_status->freq; | |
512 | ||
513 | channel = ieee80211_get_channel(local->hw.wiphy, freq); | |
514 | ||
515 | if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) | |
516 | return; | |
517 | ||
9eba6125 | 518 | if (sdata->vif.type == NL80211_IFTYPE_ADHOC && |
b203ca39 | 519 | ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) { |
46900298 JB |
520 | |
521 | rcu_read_lock(); | |
abe60632 | 522 | sta = sta_info_get(sdata, mgmt->sa); |
46900298 | 523 | |
9eba6125 | 524 | if (elems->supp_rates) { |
2103dec1 | 525 | supp_rates = ieee80211_sta_get_rates(sdata, elems, |
9ebb61a2 | 526 | band, NULL); |
9eba6125 BR |
527 | if (sta) { |
528 | u32 prev_rates; | |
529 | ||
530 | prev_rates = sta->sta.supp_rates[band]; | |
531 | /* make sure mandatory rates are always added */ | |
74608aca SW |
532 | scan_width = NL80211_BSS_CHAN_WIDTH_20; |
533 | if (rx_status->flag & RX_FLAG_5MHZ) | |
534 | scan_width = NL80211_BSS_CHAN_WIDTH_5; | |
535 | if (rx_status->flag & RX_FLAG_10MHZ) | |
536 | scan_width = NL80211_BSS_CHAN_WIDTH_10; | |
46900298 | 537 | |
74608aca SW |
538 | sta->sta.supp_rates[band] = supp_rates | |
539 | ieee80211_mandatory_rates(sband, | |
540 | scan_width); | |
9eba6125 | 541 | if (sta->sta.supp_rates[band] != prev_rates) { |
bdcbd8e0 JB |
542 | ibss_dbg(sdata, |
543 | "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n", | |
544 | sta->sta.addr, prev_rates, | |
545 | sta->sta.supp_rates[band]); | |
13c40c54 | 546 | rates_updated = true; |
9eba6125 | 547 | } |
8bf11d8d JB |
548 | } else { |
549 | rcu_read_unlock(); | |
9eba6125 | 550 | sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid, |
52874a5e | 551 | mgmt->sa, supp_rates); |
8bf11d8d | 552 | } |
34e89507 | 553 | } |
9eba6125 BR |
554 | |
555 | if (sta && elems->wmm_info) | |
c2c98fde | 556 | set_sta_flag(sta, WLAN_STA_WME); |
9eba6125 | 557 | |
074d46d1 | 558 | if (sta && elems->ht_operation && elems->ht_cap_elem && |
3aede78a SW |
559 | sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT && |
560 | sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 && | |
561 | sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) { | |
13c40c54 | 562 | /* we both use HT */ |
e1a0c6b3 | 563 | struct ieee80211_ht_cap htcap_ie; |
4bf88530 JB |
564 | struct cfg80211_chan_def chandef; |
565 | ||
566 | ieee80211_ht_oper_to_chandef(channel, | |
567 | elems->ht_operation, | |
568 | &chandef); | |
13c40c54 | 569 | |
e1a0c6b3 | 570 | memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie)); |
13c40c54 AS |
571 | |
572 | /* | |
573 | * fall back to HT20 if we don't use or use | |
574 | * the other extension channel | |
575 | */ | |
3aede78a SW |
576 | if (chandef.center_freq1 != |
577 | sdata->u.ibss.chandef.center_freq1) | |
e1a0c6b3 JB |
578 | htcap_ie.cap_info &= |
579 | cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40); | |
580 | ||
581 | rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap( | |
582 | sdata, sband, &htcap_ie, sta); | |
13c40c54 AS |
583 | } |
584 | ||
e687f61e AQ |
585 | if (sta && rates_updated) { |
586 | drv_sta_rc_update(local, sdata, &sta->sta, | |
587 | IEEE80211_RC_SUPP_RATES_CHANGED); | |
13c40c54 | 588 | rate_control_rate_init(sta); |
e687f61e | 589 | } |
13c40c54 | 590 | |
9eba6125 | 591 | rcu_read_unlock(); |
46900298 JB |
592 | } |
593 | ||
594 | bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems, | |
d45c4172 | 595 | channel); |
46900298 JB |
596 | if (!bss) |
597 | return; | |
598 | ||
0c1ad2ca JB |
599 | cbss = container_of((void *)bss, struct cfg80211_bss, priv); |
600 | ||
8cef2c9d JB |
601 | /* same for beacon and probe response */ |
602 | beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp); | |
46900298 JB |
603 | |
604 | /* check if we need to merge IBSS */ | |
605 | ||
46900298 | 606 | /* we use a fixed BSSID */ |
a98bfec2 | 607 | if (sdata->u.ibss.fixed_bssid) |
46900298 JB |
608 | goto put_bss; |
609 | ||
610 | /* not an IBSS */ | |
0c1ad2ca | 611 | if (!(cbss->capability & WLAN_CAPABILITY_IBSS)) |
46900298 JB |
612 | goto put_bss; |
613 | ||
614 | /* different channel */ | |
55de908a | 615 | if (sdata->u.ibss.fixed_channel && |
3aede78a | 616 | sdata->u.ibss.chandef.chan != cbss->channel) |
46900298 JB |
617 | goto put_bss; |
618 | ||
619 | /* different SSID */ | |
620 | if (elems->ssid_len != sdata->u.ibss.ssid_len || | |
621 | memcmp(elems->ssid, sdata->u.ibss.ssid, | |
622 | sdata->u.ibss.ssid_len)) | |
623 | goto put_bss; | |
624 | ||
34e8f082 | 625 | /* same BSSID */ |
b203ca39 | 626 | if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid)) |
34e8f082 AF |
627 | goto put_bss; |
628 | ||
f4bda337 TP |
629 | if (ieee80211_have_rx_timestamp(rx_status)) { |
630 | /* time when timestamp field was received */ | |
631 | rx_timestamp = | |
632 | ieee80211_calculate_rx_timestamp(local, rx_status, | |
633 | len + FCS_LEN, 24); | |
24487981 JB |
634 | } else { |
635 | /* | |
636 | * second best option: get current TSF | |
637 | * (will return -1 if not supported) | |
638 | */ | |
37a41b4a | 639 | rx_timestamp = drv_get_tsf(local, sdata); |
24487981 | 640 | } |
46900298 | 641 | |
bdcbd8e0 JB |
642 | ibss_dbg(sdata, |
643 | "RX beacon SA=%pM BSSID=%pM TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n", | |
644 | mgmt->sa, mgmt->bssid, | |
645 | (unsigned long long)rx_timestamp, | |
646 | (unsigned long long)beacon_timestamp, | |
647 | (unsigned long long)(rx_timestamp - beacon_timestamp), | |
648 | jiffies); | |
46900298 JB |
649 | |
650 | if (beacon_timestamp > rx_timestamp) { | |
bdcbd8e0 JB |
651 | ibss_dbg(sdata, |
652 | "beacon TSF higher than local TSF - IBSS merge with BSSID %pM\n", | |
653 | mgmt->bssid); | |
46900298 | 654 | ieee80211_sta_join_ibss(sdata, bss); |
2103dec1 | 655 | supp_rates = ieee80211_sta_get_rates(sdata, elems, band, NULL); |
34e89507 | 656 | ieee80211_ibss_add_sta(sdata, mgmt->bssid, mgmt->sa, |
52874a5e | 657 | supp_rates); |
8bf11d8d | 658 | rcu_read_unlock(); |
46900298 JB |
659 | } |
660 | ||
661 | put_bss: | |
662 | ieee80211_rx_bss_put(local, bss); | |
663 | } | |
664 | ||
8bf11d8d JB |
665 | void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata, |
666 | const u8 *bssid, const u8 *addr, | |
667 | u32 supp_rates) | |
46900298 | 668 | { |
2e10d330 | 669 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
46900298 JB |
670 | struct ieee80211_local *local = sdata->local; |
671 | struct sta_info *sta; | |
55de908a | 672 | struct ieee80211_chanctx_conf *chanctx_conf; |
b422c6cd | 673 | struct ieee80211_supported_band *sband; |
74608aca | 674 | enum nl80211_bss_scan_width scan_width; |
55de908a | 675 | int band; |
46900298 | 676 | |
af8cdcd8 JB |
677 | /* |
678 | * XXX: Consider removing the least recently used entry and | |
679 | * allow new one to be added. | |
680 | */ | |
46900298 | 681 | if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) { |
bdcbd8e0 | 682 | net_info_ratelimited("%s: No room for a new IBSS STA entry %pM\n", |
e87cc472 | 683 | sdata->name, addr); |
8bf11d8d | 684 | return; |
46900298 JB |
685 | } |
686 | ||
2e10d330 | 687 | if (ifibss->state == IEEE80211_IBSS_MLME_SEARCH) |
8bf11d8d | 688 | return; |
2e10d330 | 689 | |
b203ca39 | 690 | if (!ether_addr_equal(bssid, sdata->u.ibss.bssid)) |
8bf11d8d | 691 | return; |
46900298 | 692 | |
55de908a JB |
693 | rcu_read_lock(); |
694 | chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); | |
695 | if (WARN_ON_ONCE(!chanctx_conf)) { | |
696 | rcu_read_unlock(); | |
697 | return; | |
698 | } | |
4bf88530 | 699 | band = chanctx_conf->def.chan->band; |
74608aca | 700 | scan_width = cfg80211_chandef_to_scan_width(&chanctx_conf->def); |
55de908a JB |
701 | rcu_read_unlock(); |
702 | ||
8bf11d8d | 703 | sta = sta_info_alloc(sdata, addr, GFP_ATOMIC); |
46900298 | 704 | if (!sta) |
8bf11d8d | 705 | return; |
46900298 | 706 | |
c8716d9d | 707 | sta->last_rx = jiffies; |
d9a7ddb0 | 708 | |
46900298 | 709 | /* make sure mandatory rates are always added */ |
b422c6cd | 710 | sband = local->hw.wiphy->bands[band]; |
46900298 | 711 | sta->sta.supp_rates[band] = supp_rates | |
74608aca | 712 | ieee80211_mandatory_rates(sband, scan_width); |
46900298 | 713 | |
8bf11d8d JB |
714 | spin_lock(&ifibss->incomplete_lock); |
715 | list_add(&sta->list, &ifibss->incomplete_stations); | |
716 | spin_unlock(&ifibss->incomplete_lock); | |
717 | ieee80211_queue_work(&local->hw, &sdata->work); | |
46900298 JB |
718 | } |
719 | ||
720 | static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata) | |
721 | { | |
722 | struct ieee80211_local *local = sdata->local; | |
723 | int active = 0; | |
724 | struct sta_info *sta; | |
725 | ||
8d61ffa5 | 726 | sdata_assert_lock(sdata); |
7a17a33c | 727 | |
46900298 JB |
728 | rcu_read_lock(); |
729 | ||
730 | list_for_each_entry_rcu(sta, &local->sta_list, list) { | |
731 | if (sta->sdata == sdata && | |
732 | time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL, | |
733 | jiffies)) { | |
734 | active++; | |
735 | break; | |
736 | } | |
737 | } | |
738 | ||
739 | rcu_read_unlock(); | |
740 | ||
741 | return active; | |
742 | } | |
743 | ||
dad9defd AQ |
744 | static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata) |
745 | { | |
746 | struct ieee80211_local *local = sdata->local; | |
747 | struct sta_info *sta, *tmp; | |
748 | unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT; | |
749 | unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT; | |
750 | ||
751 | mutex_lock(&local->sta_mtx); | |
752 | ||
753 | list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { | |
754 | if (sdata != sta->sdata) | |
755 | continue; | |
756 | ||
757 | if (time_after(jiffies, sta->last_rx + exp_time) || | |
758 | (time_after(jiffies, sta->last_rx + exp_rsn_time) && | |
759 | sta->sta_state != IEEE80211_STA_AUTHORIZED)) { | |
760 | sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n", | |
761 | sta->sta_state != IEEE80211_STA_AUTHORIZED ? | |
762 | "not authorized " : "", sta->sta.addr); | |
763 | ||
764 | WARN_ON(__sta_info_destroy(sta)); | |
765 | } | |
766 | } | |
767 | ||
768 | mutex_unlock(&local->sta_mtx); | |
769 | } | |
770 | ||
ce9058ae BP |
771 | /* |
772 | * This function is called with state == IEEE80211_IBSS_MLME_JOINED | |
773 | */ | |
46900298 JB |
774 | |
775 | static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata) | |
776 | { | |
777 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; | |
7ca15a0a | 778 | enum nl80211_bss_scan_width scan_width; |
46900298 | 779 | |
8d61ffa5 | 780 | sdata_assert_lock(sdata); |
7a17a33c | 781 | |
af8cdcd8 JB |
782 | mod_timer(&ifibss->timer, |
783 | round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL)); | |
46900298 | 784 | |
dad9defd | 785 | ieee80211_ibss_sta_expire(sdata); |
af8cdcd8 | 786 | |
450aae3d S |
787 | if (time_before(jiffies, ifibss->last_scan_completed + |
788 | IEEE80211_IBSS_MERGE_INTERVAL)) | |
789 | return; | |
790 | ||
46900298 JB |
791 | if (ieee80211_sta_active_ibss(sdata)) |
792 | return; | |
793 | ||
c037b836 | 794 | if (ifibss->fixed_channel) |
46900298 JB |
795 | return; |
796 | ||
bdcbd8e0 JB |
797 | sdata_info(sdata, |
798 | "No active IBSS STAs - trying to scan for other IBSS networks with same SSID (merge)\n"); | |
46900298 | 799 | |
7ca15a0a | 800 | scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef); |
34bcf715 | 801 | ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len, |
7ca15a0a | 802 | NULL, scan_width); |
46900298 JB |
803 | } |
804 | ||
af8cdcd8 | 805 | static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata) |
46900298 JB |
806 | { |
807 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; | |
46900298 | 808 | u8 bssid[ETH_ALEN]; |
46900298 JB |
809 | u16 capability; |
810 | int i; | |
811 | ||
8d61ffa5 | 812 | sdata_assert_lock(sdata); |
7a17a33c | 813 | |
af8cdcd8 | 814 | if (ifibss->fixed_bssid) { |
46900298 JB |
815 | memcpy(bssid, ifibss->bssid, ETH_ALEN); |
816 | } else { | |
817 | /* Generate random, not broadcast, locally administered BSSID. Mix in | |
818 | * own MAC address to make sure that devices that do not have proper | |
819 | * random number generator get different BSSID. */ | |
820 | get_random_bytes(bssid, ETH_ALEN); | |
821 | for (i = 0; i < ETH_ALEN; i++) | |
47846c9b | 822 | bssid[i] ^= sdata->vif.addr[i]; |
46900298 JB |
823 | bssid[0] &= ~0x01; |
824 | bssid[0] |= 0x02; | |
825 | } | |
826 | ||
bdcbd8e0 | 827 | sdata_info(sdata, "Creating new IBSS network, BSSID %pM\n", bssid); |
46900298 | 828 | |
46900298 JB |
829 | capability = WLAN_CAPABILITY_IBSS; |
830 | ||
fffd0934 | 831 | if (ifibss->privacy) |
46900298 JB |
832 | capability |= WLAN_CAPABILITY_PRIVACY; |
833 | else | |
834 | sdata->drop_unencrypted = 0; | |
835 | ||
57c4d7b4 | 836 | __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int, |
3aede78a | 837 | ifibss->chandef.chan, ifibss->basic_rates, |
c13a765b | 838 | capability, 0, true); |
46900298 JB |
839 | } |
840 | ||
ce9058ae BP |
841 | /* |
842 | * This function is called with state == IEEE80211_IBSS_MLME_SEARCH | |
843 | */ | |
844 | ||
af8cdcd8 | 845 | static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata) |
46900298 JB |
846 | { |
847 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; | |
848 | struct ieee80211_local *local = sdata->local; | |
0c1ad2ca | 849 | struct cfg80211_bss *cbss; |
af8cdcd8 | 850 | struct ieee80211_channel *chan = NULL; |
46900298 | 851 | const u8 *bssid = NULL; |
7ca15a0a | 852 | enum nl80211_bss_scan_width scan_width; |
46900298 | 853 | int active_ibss; |
e0d61887 | 854 | u16 capability; |
46900298 | 855 | |
8d61ffa5 | 856 | sdata_assert_lock(sdata); |
7a17a33c | 857 | |
46900298 | 858 | active_ibss = ieee80211_sta_active_ibss(sdata); |
bdcbd8e0 | 859 | ibss_dbg(sdata, "sta_find_ibss (active_ibss=%d)\n", active_ibss); |
46900298 JB |
860 | |
861 | if (active_ibss) | |
af8cdcd8 | 862 | return; |
46900298 | 863 | |
e0d61887 | 864 | capability = WLAN_CAPABILITY_IBSS; |
fffd0934 | 865 | if (ifibss->privacy) |
e0d61887 | 866 | capability |= WLAN_CAPABILITY_PRIVACY; |
af8cdcd8 JB |
867 | if (ifibss->fixed_bssid) |
868 | bssid = ifibss->bssid; | |
869 | if (ifibss->fixed_channel) | |
3aede78a | 870 | chan = ifibss->chandef.chan; |
af8cdcd8 | 871 | if (!is_zero_ether_addr(ifibss->bssid)) |
46900298 | 872 | bssid = ifibss->bssid; |
0c1ad2ca JB |
873 | cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid, |
874 | ifibss->ssid, ifibss->ssid_len, | |
875 | WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY, | |
876 | capability); | |
877 | ||
878 | if (cbss) { | |
879 | struct ieee80211_bss *bss; | |
46900298 | 880 | |
0c1ad2ca | 881 | bss = (void *)cbss->priv; |
bdcbd8e0 JB |
882 | ibss_dbg(sdata, |
883 | "sta_find_ibss: selected %pM current %pM\n", | |
884 | cbss->bssid, ifibss->bssid); | |
885 | sdata_info(sdata, | |
886 | "Selected IBSS BSSID %pM based on configured SSID\n", | |
887 | cbss->bssid); | |
46900298 | 888 | |
af8cdcd8 | 889 | ieee80211_sta_join_ibss(sdata, bss); |
46900298 | 890 | ieee80211_rx_bss_put(local, bss); |
af8cdcd8 | 891 | return; |
d419b9f0 | 892 | } |
46900298 | 893 | |
bdcbd8e0 | 894 | ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n"); |
46900298 JB |
895 | |
896 | /* Selected IBSS not found in current scan results - try to scan */ | |
ce9058ae | 897 | if (time_after(jiffies, ifibss->last_scan_completed + |
46900298 | 898 | IEEE80211_SCAN_INTERVAL)) { |
bdcbd8e0 | 899 | sdata_info(sdata, "Trigger new scan to find an IBSS to join\n"); |
46900298 | 900 | |
7ca15a0a | 901 | scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef); |
34bcf715 | 902 | ieee80211_request_ibss_scan(sdata, ifibss->ssid, |
7ca15a0a SW |
903 | ifibss->ssid_len, chan, |
904 | scan_width); | |
ce9058ae | 905 | } else { |
46900298 JB |
906 | int interval = IEEE80211_SCAN_INTERVAL; |
907 | ||
908 | if (time_after(jiffies, ifibss->ibss_join_req + | |
55de908a JB |
909 | IEEE80211_IBSS_JOIN_TIMEOUT)) |
910 | ieee80211_sta_create_ibss(sdata); | |
46900298 | 911 | |
af8cdcd8 JB |
912 | mod_timer(&ifibss->timer, |
913 | round_jiffies(jiffies + interval)); | |
46900298 | 914 | } |
46900298 JB |
915 | } |
916 | ||
917 | static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata, | |
c269a203 | 918 | struct sk_buff *req) |
46900298 | 919 | { |
c269a203 | 920 | struct ieee80211_mgmt *mgmt = (void *)req->data; |
46900298 JB |
921 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
922 | struct ieee80211_local *local = sdata->local; | |
c269a203 | 923 | int tx_last_beacon, len = req->len; |
46900298 | 924 | struct sk_buff *skb; |
c3ffeab4 | 925 | struct beacon_data *presp; |
46900298 JB |
926 | u8 *pos, *end; |
927 | ||
8d61ffa5 | 928 | sdata_assert_lock(sdata); |
7a17a33c | 929 | |
40b275b6 | 930 | presp = rcu_dereference_protected(ifibss->presp, |
8d61ffa5 | 931 | lockdep_is_held(&sdata->wdev.mtx)); |
40b275b6 | 932 | |
46900298 | 933 | if (ifibss->state != IEEE80211_IBSS_MLME_JOINED || |
40b275b6 | 934 | len < 24 + 2 || !presp) |
46900298 JB |
935 | return; |
936 | ||
24487981 | 937 | tx_last_beacon = drv_tx_last_beacon(local); |
46900298 | 938 | |
bdcbd8e0 JB |
939 | ibss_dbg(sdata, |
940 | "RX ProbeReq SA=%pM DA=%pM BSSID=%pM (tx_last_beacon=%d)\n", | |
941 | mgmt->sa, mgmt->da, mgmt->bssid, tx_last_beacon); | |
46900298 | 942 | |
1ed76487 | 943 | if (!tx_last_beacon && is_multicast_ether_addr(mgmt->da)) |
46900298 JB |
944 | return; |
945 | ||
b203ca39 | 946 | if (!ether_addr_equal(mgmt->bssid, ifibss->bssid) && |
888d04df | 947 | !is_broadcast_ether_addr(mgmt->bssid)) |
46900298 JB |
948 | return; |
949 | ||
950 | end = ((u8 *) mgmt) + len; | |
951 | pos = mgmt->u.probe_req.variable; | |
952 | if (pos[0] != WLAN_EID_SSID || | |
953 | pos + 2 + pos[1] > end) { | |
bdcbd8e0 JB |
954 | ibss_dbg(sdata, "Invalid SSID IE in ProbeReq from %pM\n", |
955 | mgmt->sa); | |
46900298 JB |
956 | return; |
957 | } | |
958 | if (pos[1] != 0 && | |
959 | (pos[1] != ifibss->ssid_len || | |
0da780c2 | 960 | memcmp(pos + 2, ifibss->ssid, ifibss->ssid_len))) { |
46900298 JB |
961 | /* Ignore ProbeReq for foreign SSID */ |
962 | return; | |
963 | } | |
964 | ||
965 | /* Reply with ProbeResp */ | |
c3ffeab4 | 966 | skb = dev_alloc_skb(local->tx_headroom + presp->head_len); |
46900298 JB |
967 | if (!skb) |
968 | return; | |
969 | ||
c3ffeab4 JB |
970 | skb_reserve(skb, local->tx_headroom); |
971 | memcpy(skb_put(skb, presp->head_len), presp->head, presp->head_len); | |
972 | ||
973 | memcpy(((struct ieee80211_mgmt *) skb->data)->da, mgmt->sa, ETH_ALEN); | |
974 | ibss_dbg(sdata, "Sending ProbeResp to %pM\n", mgmt->sa); | |
62ae67be JB |
975 | IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; |
976 | ieee80211_tx_skb(sdata, skb); | |
46900298 JB |
977 | } |
978 | ||
d45c4172 EG |
979 | static |
980 | void ieee80211_rx_mgmt_probe_beacon(struct ieee80211_sub_if_data *sdata, | |
981 | struct ieee80211_mgmt *mgmt, size_t len, | |
982 | struct ieee80211_rx_status *rx_status) | |
46900298 JB |
983 | { |
984 | size_t baselen; | |
985 | struct ieee802_11_elems elems; | |
986 | ||
d45c4172 EG |
987 | BUILD_BUG_ON(offsetof(typeof(mgmt->u.probe_resp), variable) != |
988 | offsetof(typeof(mgmt->u.beacon), variable)); | |
989 | ||
990 | /* | |
991 | * either beacon or probe_resp but the variable field is at the | |
992 | * same offset | |
993 | */ | |
46900298 JB |
994 | baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt; |
995 | if (baselen > len) | |
996 | return; | |
997 | ||
998 | ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen, | |
b2e506bf | 999 | false, &elems); |
46900298 | 1000 | |
d45c4172 | 1001 | ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems); |
46900298 JB |
1002 | } |
1003 | ||
1fa57d01 JB |
1004 | void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, |
1005 | struct sk_buff *skb) | |
46900298 JB |
1006 | { |
1007 | struct ieee80211_rx_status *rx_status; | |
1008 | struct ieee80211_mgmt *mgmt; | |
1009 | u16 fc; | |
1010 | ||
f1d58c25 | 1011 | rx_status = IEEE80211_SKB_RXCB(skb); |
46900298 JB |
1012 | mgmt = (struct ieee80211_mgmt *) skb->data; |
1013 | fc = le16_to_cpu(mgmt->frame_control); | |
1014 | ||
8d61ffa5 | 1015 | sdata_lock(sdata); |
7a17a33c | 1016 | |
c926d006 TH |
1017 | if (!sdata->u.ibss.ssid_len) |
1018 | goto mgmt_out; /* not ready to merge yet */ | |
1019 | ||
46900298 JB |
1020 | switch (fc & IEEE80211_FCTL_STYPE) { |
1021 | case IEEE80211_STYPE_PROBE_REQ: | |
c269a203 | 1022 | ieee80211_rx_mgmt_probe_req(sdata, skb); |
46900298 JB |
1023 | break; |
1024 | case IEEE80211_STYPE_PROBE_RESP: | |
46900298 | 1025 | case IEEE80211_STYPE_BEACON: |
d45c4172 EG |
1026 | ieee80211_rx_mgmt_probe_beacon(sdata, mgmt, skb->len, |
1027 | rx_status); | |
46900298 JB |
1028 | break; |
1029 | case IEEE80211_STYPE_AUTH: | |
1030 | ieee80211_rx_mgmt_auth_ibss(sdata, mgmt, skb->len); | |
1031 | break; | |
2cc59e78 AQ |
1032 | case IEEE80211_STYPE_DEAUTH: |
1033 | ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len); | |
1034 | break; | |
46900298 | 1035 | } |
7a17a33c | 1036 | |
c926d006 | 1037 | mgmt_out: |
8d61ffa5 | 1038 | sdata_unlock(sdata); |
46900298 JB |
1039 | } |
1040 | ||
1fa57d01 | 1041 | void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata) |
46900298 | 1042 | { |
1fa57d01 | 1043 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
8bf11d8d | 1044 | struct sta_info *sta; |
46900298 | 1045 | |
8d61ffa5 | 1046 | sdata_lock(sdata); |
7a17a33c JB |
1047 | |
1048 | /* | |
1049 | * Work could be scheduled after scan or similar | |
1050 | * when we aren't even joined (or trying) with a | |
1051 | * network. | |
1052 | */ | |
1053 | if (!ifibss->ssid_len) | |
1054 | goto out; | |
46900298 | 1055 | |
8bf11d8d JB |
1056 | spin_lock_bh(&ifibss->incomplete_lock); |
1057 | while (!list_empty(&ifibss->incomplete_stations)) { | |
1058 | sta = list_first_entry(&ifibss->incomplete_stations, | |
1059 | struct sta_info, list); | |
1060 | list_del(&sta->list); | |
1061 | spin_unlock_bh(&ifibss->incomplete_lock); | |
1062 | ||
52874a5e | 1063 | ieee80211_ibss_finish_sta(sta); |
8bf11d8d JB |
1064 | rcu_read_unlock(); |
1065 | spin_lock_bh(&ifibss->incomplete_lock); | |
1066 | } | |
1067 | spin_unlock_bh(&ifibss->incomplete_lock); | |
1068 | ||
46900298 JB |
1069 | switch (ifibss->state) { |
1070 | case IEEE80211_IBSS_MLME_SEARCH: | |
1071 | ieee80211_sta_find_ibss(sdata); | |
1072 | break; | |
1073 | case IEEE80211_IBSS_MLME_JOINED: | |
1074 | ieee80211_sta_merge_ibss(sdata); | |
1075 | break; | |
1076 | default: | |
1077 | WARN_ON(1); | |
1078 | break; | |
1079 | } | |
46900298 | 1080 | |
7a17a33c | 1081 | out: |
8d61ffa5 | 1082 | sdata_unlock(sdata); |
3a4d4aa2 JB |
1083 | } |
1084 | ||
46900298 JB |
1085 | static void ieee80211_ibss_timer(unsigned long data) |
1086 | { | |
1087 | struct ieee80211_sub_if_data *sdata = | |
1088 | (struct ieee80211_sub_if_data *) data; | |
5bb644a0 | 1089 | |
a6182943 | 1090 | ieee80211_queue_work(&sdata->local->hw, &sdata->work); |
5bb644a0 | 1091 | } |
5bb644a0 | 1092 | |
46900298 JB |
1093 | void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata) |
1094 | { | |
1095 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; | |
1096 | ||
46900298 JB |
1097 | setup_timer(&ifibss->timer, ieee80211_ibss_timer, |
1098 | (unsigned long) sdata); | |
8bf11d8d JB |
1099 | INIT_LIST_HEAD(&ifibss->incomplete_stations); |
1100 | spin_lock_init(&ifibss->incomplete_lock); | |
46900298 JB |
1101 | } |
1102 | ||
1103 | /* scan finished notification */ | |
1104 | void ieee80211_ibss_notify_scan_completed(struct ieee80211_local *local) | |
1105 | { | |
af8cdcd8 | 1106 | struct ieee80211_sub_if_data *sdata; |
46900298 | 1107 | |
29b4a4f7 JB |
1108 | mutex_lock(&local->iflist_mtx); |
1109 | list_for_each_entry(sdata, &local->interfaces, list) { | |
9607e6b6 | 1110 | if (!ieee80211_sdata_running(sdata)) |
0e41f715 | 1111 | continue; |
af8cdcd8 JB |
1112 | if (sdata->vif.type != NL80211_IFTYPE_ADHOC) |
1113 | continue; | |
1114 | sdata->u.ibss.last_scan_completed = jiffies; | |
7a17a33c | 1115 | ieee80211_queue_work(&local->hw, &sdata->work); |
46900298 | 1116 | } |
29b4a4f7 | 1117 | mutex_unlock(&local->iflist_mtx); |
46900298 JB |
1118 | } |
1119 | ||
af8cdcd8 JB |
1120 | int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata, |
1121 | struct cfg80211_ibss_params *params) | |
1122 | { | |
ff3cc5f4 | 1123 | u32 changed = 0; |
2103dec1 SW |
1124 | u32 rate_flags; |
1125 | struct ieee80211_supported_band *sband; | |
1126 | int i; | |
af8cdcd8 | 1127 | |
af8cdcd8 JB |
1128 | if (params->bssid) { |
1129 | memcpy(sdata->u.ibss.bssid, params->bssid, ETH_ALEN); | |
1130 | sdata->u.ibss.fixed_bssid = true; | |
1131 | } else | |
1132 | sdata->u.ibss.fixed_bssid = false; | |
1133 | ||
fffd0934 | 1134 | sdata->u.ibss.privacy = params->privacy; |
267335d6 | 1135 | sdata->u.ibss.control_port = params->control_port; |
fbd2c8dc | 1136 | sdata->u.ibss.basic_rates = params->basic_rates; |
2103dec1 SW |
1137 | |
1138 | /* fix basic_rates if channel does not support these rates */ | |
1139 | rate_flags = ieee80211_chandef_rate_flags(¶ms->chandef); | |
1140 | sband = sdata->local->hw.wiphy->bands[params->chandef.chan->band]; | |
1141 | for (i = 0; i < sband->n_bitrates; i++) { | |
1142 | if ((rate_flags & sband->bitrates[i].flags) != rate_flags) | |
1143 | sdata->u.ibss.basic_rates &= ~BIT(i); | |
1144 | } | |
dd5b4cc7 FF |
1145 | memcpy(sdata->vif.bss_conf.mcast_rate, params->mcast_rate, |
1146 | sizeof(params->mcast_rate)); | |
fffd0934 | 1147 | |
57c4d7b4 JB |
1148 | sdata->vif.bss_conf.beacon_int = params->beacon_interval; |
1149 | ||
3aede78a | 1150 | sdata->u.ibss.chandef = params->chandef; |
af8cdcd8 JB |
1151 | sdata->u.ibss.fixed_channel = params->channel_fixed; |
1152 | ||
1153 | if (params->ie) { | |
1154 | sdata->u.ibss.ie = kmemdup(params->ie, params->ie_len, | |
1155 | GFP_KERNEL); | |
1156 | if (sdata->u.ibss.ie) | |
1157 | sdata->u.ibss.ie_len = params->ie_len; | |
1158 | } | |
1159 | ||
af8cdcd8 JB |
1160 | sdata->u.ibss.state = IEEE80211_IBSS_MLME_SEARCH; |
1161 | sdata->u.ibss.ibss_join_req = jiffies; | |
1162 | ||
badecb00 | 1163 | memcpy(sdata->u.ibss.ssid, params->ssid, params->ssid_len); |
0e41f715 JB |
1164 | sdata->u.ibss.ssid_len = params->ssid_len; |
1165 | ||
822854b0 SW |
1166 | memcpy(&sdata->u.ibss.ht_capa, ¶ms->ht_capa, |
1167 | sizeof(sdata->u.ibss.ht_capa)); | |
1168 | memcpy(&sdata->u.ibss.ht_capa_mask, ¶ms->ht_capa_mask, | |
1169 | sizeof(sdata->u.ibss.ht_capa_mask)); | |
1170 | ||
ff3cc5f4 SW |
1171 | /* |
1172 | * 802.11n-2009 9.13.3.1: In an IBSS, the HT Protection field is | |
1173 | * reserved, but an HT STA shall protect HT transmissions as though | |
1174 | * the HT Protection field were set to non-HT mixed mode. | |
1175 | * | |
1176 | * In an IBSS, the RIFS Mode field of the HT Operation element is | |
1177 | * also reserved, but an HT STA shall operate as though this field | |
1178 | * were set to 1. | |
1179 | */ | |
1180 | ||
1181 | sdata->vif.bss_conf.ht_operation_mode |= | |
1182 | IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED | |
1183 | | IEEE80211_HT_PARAM_RIFS_MODE; | |
1184 | ||
1185 | changed |= BSS_CHANGED_HT; | |
1186 | ieee80211_bss_info_change_notify(sdata, changed); | |
1187 | ||
04ecd257 JB |
1188 | sdata->smps_mode = IEEE80211_SMPS_OFF; |
1189 | sdata->needed_rx_chains = sdata->local->rx_chains; | |
1190 | ||
64592c8f | 1191 | ieee80211_queue_work(&sdata->local->hw, &sdata->work); |
af8cdcd8 JB |
1192 | |
1193 | return 0; | |
1194 | } | |
1195 | ||
1196 | int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata) | |
1197 | { | |
5ea096c0 TP |
1198 | struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; |
1199 | struct ieee80211_local *local = sdata->local; | |
1200 | struct cfg80211_bss *cbss; | |
1201 | u16 capability; | |
7a17a33c | 1202 | int active_ibss; |
8bf11d8d | 1203 | struct sta_info *sta; |
c3ffeab4 | 1204 | struct beacon_data *presp; |
7a17a33c | 1205 | |
5ea096c0 TP |
1206 | active_ibss = ieee80211_sta_active_ibss(sdata); |
1207 | ||
1208 | if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) { | |
1209 | capability = WLAN_CAPABILITY_IBSS; | |
1210 | ||
1211 | if (ifibss->privacy) | |
1212 | capability |= WLAN_CAPABILITY_PRIVACY; | |
1213 | ||
3aede78a | 1214 | cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan, |
5ea096c0 TP |
1215 | ifibss->bssid, ifibss->ssid, |
1216 | ifibss->ssid_len, WLAN_CAPABILITY_IBSS | | |
1217 | WLAN_CAPABILITY_PRIVACY, | |
1218 | capability); | |
1219 | ||
1220 | if (cbss) { | |
1221 | cfg80211_unlink_bss(local->hw.wiphy, cbss); | |
5b112d3d | 1222 | cfg80211_put_bss(local->hw.wiphy, cbss); |
5ea096c0 TP |
1223 | } |
1224 | } | |
af8cdcd8 | 1225 | |
b78a4932 SW |
1226 | ifibss->state = IEEE80211_IBSS_MLME_SEARCH; |
1227 | memset(ifibss->bssid, 0, ETH_ALEN); | |
1228 | ifibss->ssid_len = 0; | |
1229 | ||
b998e8bb | 1230 | sta_info_flush(sdata); |
8bf11d8d JB |
1231 | |
1232 | spin_lock_bh(&ifibss->incomplete_lock); | |
1233 | while (!list_empty(&ifibss->incomplete_stations)) { | |
1234 | sta = list_first_entry(&ifibss->incomplete_stations, | |
1235 | struct sta_info, list); | |
1236 | list_del(&sta->list); | |
1237 | spin_unlock_bh(&ifibss->incomplete_lock); | |
1238 | ||
1239 | sta_info_free(local, sta); | |
1240 | spin_lock_bh(&ifibss->incomplete_lock); | |
1241 | } | |
1242 | spin_unlock_bh(&ifibss->incomplete_lock); | |
1243 | ||
86a2ea41 | 1244 | netif_carrier_off(sdata->dev); |
af8cdcd8 JB |
1245 | |
1246 | /* remove beacon */ | |
1247 | kfree(sdata->u.ibss.ie); | |
c3ffeab4 | 1248 | presp = rcu_dereference_protected(ifibss->presp, |
8d61ffa5 | 1249 | lockdep_is_held(&sdata->wdev.mtx)); |
a9b3cd7f | 1250 | RCU_INIT_POINTER(sdata->u.ibss.presp, NULL); |
822854b0 SW |
1251 | |
1252 | /* on the next join, re-program HT parameters */ | |
1253 | memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa)); | |
1254 | memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask)); | |
1255 | ||
8fc214ba | 1256 | sdata->vif.bss_conf.ibss_joined = false; |
c13a765b | 1257 | sdata->vif.bss_conf.ibss_creator = false; |
d6a83228 | 1258 | sdata->vif.bss_conf.enable_beacon = false; |
0ca54f6c | 1259 | sdata->vif.bss_conf.ssid_len = 0; |
d6a83228 | 1260 | clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state); |
8fc214ba JB |
1261 | ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED | |
1262 | BSS_CHANGED_IBSS); | |
af8cdcd8 | 1263 | synchronize_rcu(); |
c3ffeab4 | 1264 | kfree(presp); |
af8cdcd8 | 1265 | |
35f20c14 | 1266 | skb_queue_purge(&sdata->skb_queue); |
5cff20e6 | 1267 | |
bc05d19f | 1268 | del_timer_sync(&sdata->u.ibss.timer); |
7a17a33c | 1269 | |
af8cdcd8 JB |
1270 | return 0; |
1271 | } |