]>
Commit | Line | Data |
---|---|---|
2a519311 JB |
1 | /* |
2 | * cfg80211 scan result handling | |
3 | * | |
4 | * Copyright 2008 Johannes Berg <johannes@sipsolutions.net> | |
5 | */ | |
6 | #include <linux/kernel.h> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
2a519311 JB |
8 | #include <linux/module.h> |
9 | #include <linux/netdevice.h> | |
10 | #include <linux/wireless.h> | |
11 | #include <linux/nl80211.h> | |
12 | #include <linux/etherdevice.h> | |
13 | #include <net/arp.h> | |
14 | #include <net/cfg80211.h> | |
262eb9b2 | 15 | #include <net/cfg80211-wext.h> |
2a519311 JB |
16 | #include <net/iw_handler.h> |
17 | #include "core.h" | |
18 | #include "nl80211.h" | |
a9a11622 | 19 | #include "wext-compat.h" |
e35e4d28 | 20 | #include "rdev-ops.h" |
2a519311 | 21 | |
f9616e0f | 22 | #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ) |
2a519311 | 23 | |
e8e27c66 AK |
24 | static void bss_release(struct kref *ref) |
25 | { | |
9caf0364 | 26 | struct cfg80211_bss_ies *ies; |
e8e27c66 AK |
27 | struct cfg80211_internal_bss *bss; |
28 | ||
29 | bss = container_of(ref, struct cfg80211_internal_bss, ref); | |
b629ea3d JB |
30 | |
31 | if (WARN_ON(atomic_read(&bss->hold))) | |
32 | return; | |
33 | ||
9caf0364 JB |
34 | ies = (void *)rcu_access_pointer(bss->pub.beacon_ies); |
35 | if (ies) | |
36 | kfree_rcu(ies, rcu_head); | |
37 | ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies); | |
38 | if (ies) | |
39 | kfree_rcu(ies, rcu_head); | |
e8e27c66 | 40 | |
e8e27c66 AK |
41 | kfree(bss); |
42 | } | |
43 | ||
e8e27c66 AK |
44 | static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev, |
45 | struct cfg80211_internal_bss *bss) | |
46 | { | |
4b1af479 JB |
47 | lockdep_assert_held(&dev->bss_lock); |
48 | ||
e8e27c66 AK |
49 | list_del_init(&bss->list); |
50 | rb_erase(&bss->rbn, &dev->bss_tree); | |
51 | kref_put(&bss->ref, bss_release); | |
52 | } | |
53 | ||
15d6030b SL |
54 | static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev, |
55 | unsigned long expire_time) | |
56 | { | |
57 | struct cfg80211_internal_bss *bss, *tmp; | |
58 | bool expired = false; | |
59 | ||
4b1af479 JB |
60 | lockdep_assert_held(&dev->bss_lock); |
61 | ||
15d6030b SL |
62 | list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) { |
63 | if (atomic_read(&bss->hold)) | |
64 | continue; | |
65 | if (!time_after(expire_time, bss->ts)) | |
66 | continue; | |
67 | ||
68 | __cfg80211_unlink_bss(dev, bss); | |
69 | expired = true; | |
70 | } | |
71 | ||
72 | if (expired) | |
73 | dev->bss_generation++; | |
74 | } | |
75 | ||
01a0ac41 | 76 | void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak) |
2a519311 | 77 | { |
667503dd | 78 | struct cfg80211_scan_request *request; |
fd014284 | 79 | struct wireless_dev *wdev; |
3d23e349 | 80 | #ifdef CONFIG_CFG80211_WEXT |
2a519311 JB |
81 | union iwreq_data wrqu; |
82 | #endif | |
83 | ||
01a0ac41 JB |
84 | ASSERT_RDEV_LOCK(rdev); |
85 | ||
667503dd JB |
86 | request = rdev->scan_req; |
87 | ||
01a0ac41 JB |
88 | if (!request) |
89 | return; | |
90 | ||
fd014284 | 91 | wdev = request->wdev; |
2a519311 | 92 | |
6829c878 JB |
93 | /* |
94 | * This must be before sending the other events! | |
95 | * Otherwise, wpa_supplicant gets completely confused with | |
96 | * wext events. | |
97 | */ | |
fd014284 JB |
98 | if (wdev->netdev) |
99 | cfg80211_sme_scan_done(wdev->netdev); | |
6829c878 | 100 | |
15d6030b | 101 | if (request->aborted) { |
fd014284 | 102 | nl80211_send_scan_aborted(rdev, wdev); |
15d6030b SL |
103 | } else { |
104 | if (request->flags & NL80211_SCAN_FLAG_FLUSH) { | |
105 | /* flush entries from previous scans */ | |
106 | spin_lock_bh(&rdev->bss_lock); | |
107 | __cfg80211_bss_expire(rdev, request->scan_start); | |
108 | spin_unlock_bh(&rdev->bss_lock); | |
109 | } | |
fd014284 | 110 | nl80211_send_scan_done(rdev, wdev); |
15d6030b | 111 | } |
2a519311 | 112 | |
3d23e349 | 113 | #ifdef CONFIG_CFG80211_WEXT |
fd014284 | 114 | if (wdev->netdev && !request->aborted) { |
2a519311 JB |
115 | memset(&wrqu, 0, sizeof(wrqu)); |
116 | ||
fd014284 | 117 | wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL); |
2a519311 JB |
118 | } |
119 | #endif | |
120 | ||
fd014284 JB |
121 | if (wdev->netdev) |
122 | dev_put(wdev->netdev); | |
2a519311 | 123 | |
36e6fea8 | 124 | rdev->scan_req = NULL; |
01a0ac41 JB |
125 | |
126 | /* | |
127 | * OK. If this is invoked with "leak" then we can't | |
128 | * free this ... but we've cleaned it up anyway. The | |
129 | * driver failed to call the scan_done callback, so | |
130 | * all bets are off, it might still be trying to use | |
131 | * the scan request or not ... if it accesses the dev | |
132 | * in there (it shouldn't anyway) then it may crash. | |
133 | */ | |
134 | if (!leak) | |
135 | kfree(request); | |
2a519311 | 136 | } |
667503dd | 137 | |
36e6fea8 JB |
138 | void __cfg80211_scan_done(struct work_struct *wk) |
139 | { | |
140 | struct cfg80211_registered_device *rdev; | |
141 | ||
142 | rdev = container_of(wk, struct cfg80211_registered_device, | |
143 | scan_done_wk); | |
144 | ||
145 | cfg80211_lock_rdev(rdev); | |
01a0ac41 | 146 | ___cfg80211_scan_done(rdev, false); |
36e6fea8 JB |
147 | cfg80211_unlock_rdev(rdev); |
148 | } | |
149 | ||
667503dd JB |
150 | void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted) |
151 | { | |
4ee3e063 | 152 | trace_cfg80211_scan_done(request, aborted); |
667503dd JB |
153 | WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); |
154 | ||
155 | request->aborted = aborted; | |
e60d7443 | 156 | queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk); |
667503dd | 157 | } |
2a519311 JB |
158 | EXPORT_SYMBOL(cfg80211_scan_done); |
159 | ||
807f8a8c LC |
160 | void __cfg80211_sched_scan_results(struct work_struct *wk) |
161 | { | |
162 | struct cfg80211_registered_device *rdev; | |
15d6030b | 163 | struct cfg80211_sched_scan_request *request; |
807f8a8c LC |
164 | |
165 | rdev = container_of(wk, struct cfg80211_registered_device, | |
166 | sched_scan_results_wk); | |
167 | ||
15d6030b SL |
168 | request = rdev->sched_scan_req; |
169 | ||
c10841ca | 170 | mutex_lock(&rdev->sched_scan_mtx); |
807f8a8c LC |
171 | |
172 | /* we don't have sched_scan_req anymore if the scan is stopping */ | |
15d6030b SL |
173 | if (request) { |
174 | if (request->flags & NL80211_SCAN_FLAG_FLUSH) { | |
175 | /* flush entries from previous scans */ | |
176 | spin_lock_bh(&rdev->bss_lock); | |
177 | __cfg80211_bss_expire(rdev, request->scan_start); | |
178 | spin_unlock_bh(&rdev->bss_lock); | |
179 | request->scan_start = | |
180 | jiffies + msecs_to_jiffies(request->interval); | |
181 | } | |
182 | nl80211_send_sched_scan_results(rdev, request->dev); | |
183 | } | |
807f8a8c | 184 | |
c10841ca | 185 | mutex_unlock(&rdev->sched_scan_mtx); |
807f8a8c LC |
186 | } |
187 | ||
188 | void cfg80211_sched_scan_results(struct wiphy *wiphy) | |
189 | { | |
4ee3e063 | 190 | trace_cfg80211_sched_scan_results(wiphy); |
807f8a8c LC |
191 | /* ignore if we're not scanning */ |
192 | if (wiphy_to_dev(wiphy)->sched_scan_req) | |
193 | queue_work(cfg80211_wq, | |
194 | &wiphy_to_dev(wiphy)->sched_scan_results_wk); | |
195 | } | |
196 | EXPORT_SYMBOL(cfg80211_sched_scan_results); | |
197 | ||
85a9994a | 198 | void cfg80211_sched_scan_stopped(struct wiphy *wiphy) |
807f8a8c | 199 | { |
85a9994a | 200 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
807f8a8c | 201 | |
4ee3e063 BL |
202 | trace_cfg80211_sched_scan_stopped(wiphy); |
203 | ||
c10841ca | 204 | mutex_lock(&rdev->sched_scan_mtx); |
807f8a8c | 205 | __cfg80211_stop_sched_scan(rdev, true); |
c10841ca | 206 | mutex_unlock(&rdev->sched_scan_mtx); |
807f8a8c | 207 | } |
807f8a8c LC |
208 | EXPORT_SYMBOL(cfg80211_sched_scan_stopped); |
209 | ||
210 | int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev, | |
211 | bool driver_initiated) | |
212 | { | |
807f8a8c LC |
213 | struct net_device *dev; |
214 | ||
c10841ca | 215 | lockdep_assert_held(&rdev->sched_scan_mtx); |
807f8a8c LC |
216 | |
217 | if (!rdev->sched_scan_req) | |
1a84ff75 | 218 | return -ENOENT; |
807f8a8c LC |
219 | |
220 | dev = rdev->sched_scan_req->dev; | |
221 | ||
85a9994a | 222 | if (!driver_initiated) { |
e35e4d28 | 223 | int err = rdev_sched_scan_stop(rdev, dev); |
85a9994a LC |
224 | if (err) |
225 | return err; | |
226 | } | |
807f8a8c LC |
227 | |
228 | nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED); | |
229 | ||
230 | kfree(rdev->sched_scan_req); | |
231 | rdev->sched_scan_req = NULL; | |
232 | ||
3b4670ff | 233 | return 0; |
807f8a8c LC |
234 | } |
235 | ||
cb3a8eec DW |
236 | void cfg80211_bss_age(struct cfg80211_registered_device *dev, |
237 | unsigned long age_secs) | |
238 | { | |
239 | struct cfg80211_internal_bss *bss; | |
240 | unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC); | |
241 | ||
2ca813ad | 242 | spin_lock_bh(&dev->bss_lock); |
915de2ff | 243 | list_for_each_entry(bss, &dev->bss_list, list) |
cb3a8eec | 244 | bss->ts -= age_jiffies; |
2ca813ad | 245 | spin_unlock_bh(&dev->bss_lock); |
cb3a8eec DW |
246 | } |
247 | ||
2a519311 JB |
248 | void cfg80211_bss_expire(struct cfg80211_registered_device *dev) |
249 | { | |
15d6030b | 250 | __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE); |
2a519311 JB |
251 | } |
252 | ||
c21dbf92 | 253 | const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len) |
2a519311 | 254 | { |
c21dbf92 | 255 | while (len > 2 && ies[0] != eid) { |
2a519311 JB |
256 | len -= ies[1] + 2; |
257 | ies += ies[1] + 2; | |
258 | } | |
259 | if (len < 2) | |
260 | return NULL; | |
261 | if (len < 2 + ies[1]) | |
262 | return NULL; | |
263 | return ies; | |
264 | } | |
c21dbf92 | 265 | EXPORT_SYMBOL(cfg80211_find_ie); |
2a519311 | 266 | |
0c28ec58 EP |
267 | const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type, |
268 | const u8 *ies, int len) | |
269 | { | |
270 | struct ieee80211_vendor_ie *ie; | |
271 | const u8 *pos = ies, *end = ies + len; | |
272 | int ie_oui; | |
273 | ||
274 | while (pos < end) { | |
275 | pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos, | |
276 | end - pos); | |
277 | if (!pos) | |
278 | return NULL; | |
279 | ||
280 | if (end - pos < sizeof(*ie)) | |
281 | return NULL; | |
282 | ||
283 | ie = (struct ieee80211_vendor_ie *)pos; | |
284 | ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2]; | |
285 | if (ie_oui == oui && ie->oui_type == oui_type) | |
286 | return pos; | |
287 | ||
288 | pos += 2 + ie->len; | |
289 | } | |
290 | return NULL; | |
291 | } | |
292 | EXPORT_SYMBOL(cfg80211_find_vendor_ie); | |
293 | ||
915de2ff | 294 | static bool is_bss(struct cfg80211_bss *a, const u8 *bssid, |
2a519311 JB |
295 | const u8 *ssid, size_t ssid_len) |
296 | { | |
9caf0364 | 297 | const struct cfg80211_bss_ies *ies; |
2a519311 JB |
298 | const u8 *ssidie; |
299 | ||
ac422d3c | 300 | if (bssid && !ether_addr_equal(a->bssid, bssid)) |
2a519311 JB |
301 | return false; |
302 | ||
79420f09 JB |
303 | if (!ssid) |
304 | return true; | |
305 | ||
9caf0364 JB |
306 | ies = rcu_access_pointer(a->ies); |
307 | if (!ies) | |
308 | return false; | |
309 | ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len); | |
2a519311 JB |
310 | if (!ssidie) |
311 | return false; | |
312 | if (ssidie[1] != ssid_len) | |
313 | return false; | |
314 | return memcmp(ssidie + 2, ssid, ssid_len) == 0; | |
315 | } | |
316 | ||
4593c4cb JB |
317 | /** |
318 | * enum bss_compare_mode - BSS compare mode | |
319 | * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find) | |
320 | * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode | |
321 | * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode | |
322 | */ | |
323 | enum bss_compare_mode { | |
324 | BSS_CMP_REGULAR, | |
325 | BSS_CMP_HIDE_ZLEN, | |
326 | BSS_CMP_HIDE_NUL, | |
327 | }; | |
328 | ||
dd9dfb9f | 329 | static int cmp_bss(struct cfg80211_bss *a, |
5622f5bb | 330 | struct cfg80211_bss *b, |
4593c4cb | 331 | enum bss_compare_mode mode) |
dd9dfb9f | 332 | { |
9caf0364 | 333 | const struct cfg80211_bss_ies *a_ies, *b_ies; |
3af6341c JB |
334 | const u8 *ie1 = NULL; |
335 | const u8 *ie2 = NULL; | |
5622f5bb | 336 | int i, r; |
dd9dfb9f | 337 | |
3af6341c JB |
338 | if (a->channel != b->channel) |
339 | return b->channel->center_freq - a->channel->center_freq; | |
dd9dfb9f | 340 | |
9caf0364 JB |
341 | a_ies = rcu_access_pointer(a->ies); |
342 | if (!a_ies) | |
343 | return -1; | |
344 | b_ies = rcu_access_pointer(b->ies); | |
345 | if (!b_ies) | |
346 | return 1; | |
347 | ||
3af6341c JB |
348 | if (WLAN_CAPABILITY_IS_STA_BSS(a->capability)) |
349 | ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID, | |
350 | a_ies->data, a_ies->len); | |
351 | if (WLAN_CAPABILITY_IS_STA_BSS(b->capability)) | |
352 | ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID, | |
353 | b_ies->data, b_ies->len); | |
354 | if (ie1 && ie2) { | |
355 | int mesh_id_cmp; | |
356 | ||
357 | if (ie1[1] == ie2[1]) | |
358 | mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]); | |
359 | else | |
360 | mesh_id_cmp = ie2[1] - ie1[1]; | |
361 | ||
362 | ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, | |
363 | a_ies->data, a_ies->len); | |
364 | ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, | |
365 | b_ies->data, b_ies->len); | |
366 | if (ie1 && ie2) { | |
367 | if (mesh_id_cmp) | |
368 | return mesh_id_cmp; | |
369 | if (ie1[1] != ie2[1]) | |
370 | return ie2[1] - ie1[1]; | |
371 | return memcmp(ie1 + 2, ie2 + 2, ie1[1]); | |
372 | } | |
373 | } | |
374 | ||
375 | /* | |
376 | * we can't use compare_ether_addr here since we need a < > operator. | |
377 | * The binary return value of compare_ether_addr isn't enough | |
378 | */ | |
379 | r = memcmp(a->bssid, b->bssid, sizeof(a->bssid)); | |
380 | if (r) | |
381 | return r; | |
382 | ||
9caf0364 JB |
383 | ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len); |
384 | ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len); | |
dd9dfb9f | 385 | |
5622f5bb JB |
386 | if (!ie1 && !ie2) |
387 | return 0; | |
388 | ||
f94f8b16 | 389 | /* |
5622f5bb JB |
390 | * Note that with "hide_ssid", the function returns a match if |
391 | * the already-present BSS ("b") is a hidden SSID beacon for | |
392 | * the new BSS ("a"). | |
f94f8b16 | 393 | */ |
dd9dfb9f DT |
394 | |
395 | /* sort missing IE before (left of) present IE */ | |
396 | if (!ie1) | |
397 | return -1; | |
398 | if (!ie2) | |
399 | return 1; | |
400 | ||
4593c4cb JB |
401 | switch (mode) { |
402 | case BSS_CMP_HIDE_ZLEN: | |
403 | /* | |
404 | * In ZLEN mode we assume the BSS entry we're | |
405 | * looking for has a zero-length SSID. So if | |
406 | * the one we're looking at right now has that, | |
407 | * return 0. Otherwise, return the difference | |
408 | * in length, but since we're looking for the | |
409 | * 0-length it's really equivalent to returning | |
410 | * the length of the one we're looking at. | |
411 | * | |
412 | * No content comparison is needed as we assume | |
413 | * the content length is zero. | |
414 | */ | |
415 | return ie2[1]; | |
416 | case BSS_CMP_REGULAR: | |
417 | default: | |
418 | /* sort by length first, then by contents */ | |
419 | if (ie1[1] != ie2[1]) | |
420 | return ie2[1] - ie1[1]; | |
5622f5bb | 421 | return memcmp(ie1 + 2, ie2 + 2, ie1[1]); |
4593c4cb JB |
422 | case BSS_CMP_HIDE_NUL: |
423 | if (ie1[1] != ie2[1]) | |
424 | return ie2[1] - ie1[1]; | |
425 | /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */ | |
426 | for (i = 0; i < ie2[1]; i++) | |
427 | if (ie2[i + 2]) | |
428 | return -1; | |
429 | return 0; | |
430 | } | |
dd9dfb9f DT |
431 | } |
432 | ||
2a519311 JB |
433 | struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy, |
434 | struct ieee80211_channel *channel, | |
435 | const u8 *bssid, | |
79420f09 JB |
436 | const u8 *ssid, size_t ssid_len, |
437 | u16 capa_mask, u16 capa_val) | |
2a519311 JB |
438 | { |
439 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); | |
440 | struct cfg80211_internal_bss *bss, *res = NULL; | |
ccb6c136 | 441 | unsigned long now = jiffies; |
2a519311 | 442 | |
4ee3e063 BL |
443 | trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask, |
444 | capa_val); | |
445 | ||
2a519311 JB |
446 | spin_lock_bh(&dev->bss_lock); |
447 | ||
448 | list_for_each_entry(bss, &dev->bss_list, list) { | |
79420f09 JB |
449 | if ((bss->pub.capability & capa_mask) != capa_val) |
450 | continue; | |
2a519311 JB |
451 | if (channel && bss->pub.channel != channel) |
452 | continue; | |
ccb6c136 JB |
453 | /* Don't get expired BSS structs */ |
454 | if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) && | |
455 | !atomic_read(&bss->hold)) | |
456 | continue; | |
2a519311 JB |
457 | if (is_bss(&bss->pub, bssid, ssid, ssid_len)) { |
458 | res = bss; | |
459 | kref_get(&res->ref); | |
460 | break; | |
461 | } | |
462 | } | |
463 | ||
464 | spin_unlock_bh(&dev->bss_lock); | |
465 | if (!res) | |
466 | return NULL; | |
4ee3e063 | 467 | trace_cfg80211_return_bss(&res->pub); |
2a519311 JB |
468 | return &res->pub; |
469 | } | |
470 | EXPORT_SYMBOL(cfg80211_get_bss); | |
471 | ||
2a519311 JB |
472 | static void rb_insert_bss(struct cfg80211_registered_device *dev, |
473 | struct cfg80211_internal_bss *bss) | |
474 | { | |
475 | struct rb_node **p = &dev->bss_tree.rb_node; | |
476 | struct rb_node *parent = NULL; | |
477 | struct cfg80211_internal_bss *tbss; | |
478 | int cmp; | |
479 | ||
480 | while (*p) { | |
481 | parent = *p; | |
482 | tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn); | |
483 | ||
4593c4cb | 484 | cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR); |
2a519311 JB |
485 | |
486 | if (WARN_ON(!cmp)) { | |
487 | /* will sort of leak this BSS */ | |
488 | return; | |
489 | } | |
490 | ||
491 | if (cmp < 0) | |
492 | p = &(*p)->rb_left; | |
493 | else | |
494 | p = &(*p)->rb_right; | |
495 | } | |
496 | ||
497 | rb_link_node(&bss->rbn, parent, p); | |
498 | rb_insert_color(&bss->rbn, &dev->bss_tree); | |
499 | } | |
500 | ||
501 | static struct cfg80211_internal_bss * | |
502 | rb_find_bss(struct cfg80211_registered_device *dev, | |
5622f5bb | 503 | struct cfg80211_internal_bss *res, |
4593c4cb | 504 | enum bss_compare_mode mode) |
dd9dfb9f DT |
505 | { |
506 | struct rb_node *n = dev->bss_tree.rb_node; | |
507 | struct cfg80211_internal_bss *bss; | |
508 | int r; | |
509 | ||
510 | while (n) { | |
511 | bss = rb_entry(n, struct cfg80211_internal_bss, rbn); | |
4593c4cb | 512 | r = cmp_bss(&res->pub, &bss->pub, mode); |
dd9dfb9f DT |
513 | |
514 | if (r == 0) | |
515 | return bss; | |
516 | else if (r < 0) | |
517 | n = n->rb_left; | |
518 | else | |
519 | n = n->rb_right; | |
520 | } | |
521 | ||
522 | return NULL; | |
523 | } | |
524 | ||
525 | static void | |
526 | copy_hidden_ies(struct cfg80211_internal_bss *res, | |
915de2ff | 527 | struct cfg80211_internal_bss *hidden) |
dd9dfb9f | 528 | { |
9caf0364 JB |
529 | const struct cfg80211_bss_ies *ies; |
530 | ||
531 | if (rcu_access_pointer(res->pub.beacon_ies)) | |
dd9dfb9f DT |
532 | return; |
533 | ||
9caf0364 JB |
534 | ies = rcu_access_pointer(hidden->pub.beacon_ies); |
535 | if (WARN_ON(!ies)) | |
dd9dfb9f DT |
536 | return; |
537 | ||
9caf0364 JB |
538 | ies = kmemdup(ies, sizeof(*ies) + ies->len, GFP_ATOMIC); |
539 | if (unlikely(!ies)) | |
540 | return; | |
541 | rcu_assign_pointer(res->pub.beacon_ies, ies); | |
dd9dfb9f DT |
542 | } |
543 | ||
2a519311 JB |
544 | static struct cfg80211_internal_bss * |
545 | cfg80211_bss_update(struct cfg80211_registered_device *dev, | |
9caf0364 | 546 | struct cfg80211_internal_bss *tmp) |
2a519311 JB |
547 | { |
548 | struct cfg80211_internal_bss *found = NULL; | |
2a519311 | 549 | |
9caf0364 | 550 | if (WARN_ON(!tmp->pub.channel)) |
2a519311 | 551 | return NULL; |
2a519311 | 552 | |
9caf0364 | 553 | tmp->ts = jiffies; |
2a519311 | 554 | |
2a519311 JB |
555 | spin_lock_bh(&dev->bss_lock); |
556 | ||
9caf0364 JB |
557 | if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) { |
558 | spin_unlock_bh(&dev->bss_lock); | |
559 | return NULL; | |
560 | } | |
561 | ||
4593c4cb | 562 | found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR); |
2a519311 | 563 | |
cd1658f5 | 564 | if (found) { |
9caf0364 JB |
565 | found->pub.beacon_interval = tmp->pub.beacon_interval; |
566 | found->pub.tsf = tmp->pub.tsf; | |
567 | found->pub.signal = tmp->pub.signal; | |
568 | found->pub.capability = tmp->pub.capability; | |
569 | found->ts = tmp->ts; | |
cd1658f5 | 570 | |
34a6eddb | 571 | /* Update IEs */ |
9caf0364 JB |
572 | if (rcu_access_pointer(tmp->pub.proberesp_ies)) { |
573 | const struct cfg80211_bss_ies *old; | |
574 | ||
575 | old = rcu_access_pointer(found->pub.proberesp_ies); | |
cd1658f5 | 576 | |
9caf0364 JB |
577 | rcu_assign_pointer(found->pub.proberesp_ies, |
578 | tmp->pub.proberesp_ies); | |
34a6eddb | 579 | /* Override possible earlier Beacon frame IEs */ |
9caf0364 JB |
580 | rcu_assign_pointer(found->pub.ies, |
581 | tmp->pub.proberesp_ies); | |
582 | if (old) | |
583 | kfree_rcu((struct cfg80211_bss_ies *)old, | |
584 | rcu_head); | |
585 | } else if (rcu_access_pointer(tmp->pub.beacon_ies)) { | |
9537f227 | 586 | const struct cfg80211_bss_ies *old; |
915de2ff | 587 | |
9caf0364 | 588 | old = rcu_access_pointer(found->pub.beacon_ies); |
9caf0364 JB |
589 | |
590 | rcu_assign_pointer(found->pub.beacon_ies, | |
591 | tmp->pub.beacon_ies); | |
01123e23 SN |
592 | |
593 | /* Override IEs if they were from a beacon before */ | |
9537f227 | 594 | if (old == rcu_access_pointer(found->pub.ies)) |
9caf0364 JB |
595 | rcu_assign_pointer(found->pub.ies, |
596 | tmp->pub.beacon_ies); | |
cd1658f5 | 597 | |
9caf0364 JB |
598 | if (old) |
599 | kfree_rcu((struct cfg80211_bss_ies *)old, | |
600 | rcu_head); | |
601 | } | |
2a519311 | 602 | } else { |
9caf0364 | 603 | struct cfg80211_internal_bss *new; |
dd9dfb9f | 604 | struct cfg80211_internal_bss *hidden; |
9caf0364 | 605 | struct cfg80211_bss_ies *ies; |
dd9dfb9f DT |
606 | |
607 | /* First check if the beacon is a probe response from | |
608 | * a hidden bss. If so, copy beacon ies (with nullified | |
609 | * ssid) into the probe response bss entry (with real ssid). | |
610 | * It is required basically for PSM implementation | |
611 | * (probe responses do not contain tim ie) */ | |
612 | ||
613 | /* TODO: The code is not trying to update existing probe | |
614 | * response bss entries when beacon ies are | |
615 | * getting changed. */ | |
4593c4cb JB |
616 | hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN); |
617 | if (hidden) { | |
9caf0364 | 618 | copy_hidden_ies(tmp, hidden); |
4593c4cb JB |
619 | } else { |
620 | hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_NUL); | |
621 | if (hidden) | |
622 | copy_hidden_ies(tmp, hidden); | |
623 | } | |
9caf0364 JB |
624 | |
625 | /* | |
626 | * create a copy -- the "res" variable that is passed in | |
627 | * is allocated on the stack since it's not needed in the | |
628 | * more common case of an update | |
629 | */ | |
630 | new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size, | |
631 | GFP_ATOMIC); | |
632 | if (!new) { | |
633 | ies = (void *)rcu_dereference(tmp->pub.beacon_ies); | |
634 | if (ies) | |
635 | kfree_rcu(ies, rcu_head); | |
636 | ies = (void *)rcu_dereference(tmp->pub.proberesp_ies); | |
637 | if (ies) | |
638 | kfree_rcu(ies, rcu_head); | |
639 | spin_unlock_bh(&dev->bss_lock); | |
640 | return NULL; | |
641 | } | |
642 | memcpy(new, tmp, sizeof(*new)); | |
643 | kref_init(&new->ref); | |
644 | list_add_tail(&new->list, &dev->bss_list); | |
645 | rb_insert_bss(dev, new); | |
646 | found = new; | |
2a519311 JB |
647 | } |
648 | ||
649 | dev->bss_generation++; | |
650 | spin_unlock_bh(&dev->bss_lock); | |
651 | ||
652 | kref_get(&found->ref); | |
653 | return found; | |
654 | } | |
655 | ||
0172bb75 JB |
656 | static struct ieee80211_channel * |
657 | cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen, | |
658 | struct ieee80211_channel *channel) | |
659 | { | |
660 | const u8 *tmp; | |
661 | u32 freq; | |
662 | int channel_number = -1; | |
663 | ||
664 | tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen); | |
665 | if (tmp && tmp[1] == 1) { | |
666 | channel_number = tmp[2]; | |
667 | } else { | |
668 | tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen); | |
669 | if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) { | |
670 | struct ieee80211_ht_operation *htop = (void *)(tmp + 2); | |
671 | ||
672 | channel_number = htop->primary_chan; | |
673 | } | |
674 | } | |
675 | ||
676 | if (channel_number < 0) | |
677 | return channel; | |
678 | ||
679 | freq = ieee80211_channel_to_frequency(channel_number, channel->band); | |
680 | channel = ieee80211_get_channel(wiphy, freq); | |
681 | if (!channel) | |
682 | return NULL; | |
683 | if (channel->flags & IEEE80211_CHAN_DISABLED) | |
684 | return NULL; | |
685 | return channel; | |
686 | } | |
687 | ||
06aa7afa JK |
688 | struct cfg80211_bss* |
689 | cfg80211_inform_bss(struct wiphy *wiphy, | |
690 | struct ieee80211_channel *channel, | |
7b8bcff2 JB |
691 | const u8 *bssid, u64 tsf, u16 capability, |
692 | u16 beacon_interval, const u8 *ie, size_t ielen, | |
06aa7afa JK |
693 | s32 signal, gfp_t gfp) |
694 | { | |
9caf0364 JB |
695 | struct cfg80211_bss_ies *ies; |
696 | struct cfg80211_internal_bss tmp = {}, *res; | |
06aa7afa JK |
697 | |
698 | if (WARN_ON(!wiphy)) | |
699 | return NULL; | |
700 | ||
22fe88d3 | 701 | if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC && |
06aa7afa JK |
702 | (signal < 0 || signal > 100))) |
703 | return NULL; | |
704 | ||
0172bb75 JB |
705 | channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel); |
706 | if (!channel) | |
707 | return NULL; | |
708 | ||
9caf0364 JB |
709 | memcpy(tmp.pub.bssid, bssid, ETH_ALEN); |
710 | tmp.pub.channel = channel; | |
711 | tmp.pub.signal = signal; | |
712 | tmp.pub.tsf = tsf; | |
713 | tmp.pub.beacon_interval = beacon_interval; | |
714 | tmp.pub.capability = capability; | |
34a6eddb JM |
715 | /* |
716 | * Since we do not know here whether the IEs are from a Beacon or Probe | |
717 | * Response frame, we need to pick one of the options and only use it | |
718 | * with the driver that does not provide the full Beacon/Probe Response | |
719 | * frame. Use Beacon frame pointer to avoid indicating that this should | |
50521aa8 | 720 | * override the IEs pointer should we have received an earlier |
9caf0364 | 721 | * indication of Probe Response data. |
34a6eddb | 722 | */ |
9caf0364 JB |
723 | ies = kmalloc(sizeof(*ies) + ielen, gfp); |
724 | if (!ies) | |
725 | return NULL; | |
726 | ies->len = ielen; | |
727 | memcpy(ies->data, ie, ielen); | |
06aa7afa | 728 | |
9caf0364 JB |
729 | rcu_assign_pointer(tmp.pub.beacon_ies, ies); |
730 | rcu_assign_pointer(tmp.pub.ies, ies); | |
06aa7afa | 731 | |
9caf0364 | 732 | res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp); |
06aa7afa JK |
733 | if (!res) |
734 | return NULL; | |
735 | ||
736 | if (res->pub.capability & WLAN_CAPABILITY_ESS) | |
737 | regulatory_hint_found_beacon(wiphy, channel, gfp); | |
738 | ||
4ee3e063 | 739 | trace_cfg80211_return_bss(&res->pub); |
06aa7afa JK |
740 | /* cfg80211_bss_update gives us a referenced result */ |
741 | return &res->pub; | |
742 | } | |
743 | EXPORT_SYMBOL(cfg80211_inform_bss); | |
744 | ||
2a519311 JB |
745 | struct cfg80211_bss * |
746 | cfg80211_inform_bss_frame(struct wiphy *wiphy, | |
747 | struct ieee80211_channel *channel, | |
748 | struct ieee80211_mgmt *mgmt, size_t len, | |
77965c97 | 749 | s32 signal, gfp_t gfp) |
2a519311 | 750 | { |
9caf0364 JB |
751 | struct cfg80211_internal_bss tmp = {}, *res; |
752 | struct cfg80211_bss_ies *ies; | |
2a519311 JB |
753 | size_t ielen = len - offsetof(struct ieee80211_mgmt, |
754 | u.probe_resp.variable); | |
bef9bacc | 755 | |
0172bb75 JB |
756 | BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) != |
757 | offsetof(struct ieee80211_mgmt, u.beacon.variable)); | |
758 | ||
4ee3e063 BL |
759 | trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal); |
760 | ||
bef9bacc MK |
761 | if (WARN_ON(!mgmt)) |
762 | return NULL; | |
763 | ||
764 | if (WARN_ON(!wiphy)) | |
765 | return NULL; | |
2a519311 | 766 | |
22fe88d3 | 767 | if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC && |
768be59f | 768 | (signal < 0 || signal > 100))) |
2a519311 JB |
769 | return NULL; |
770 | ||
bef9bacc | 771 | if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable))) |
2a519311 JB |
772 | return NULL; |
773 | ||
0172bb75 JB |
774 | channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable, |
775 | ielen, channel); | |
776 | if (!channel) | |
777 | return NULL; | |
778 | ||
9caf0364 JB |
779 | ies = kmalloc(sizeof(*ies) + ielen, gfp); |
780 | if (!ies) | |
2a519311 | 781 | return NULL; |
9caf0364 JB |
782 | ies->len = ielen; |
783 | memcpy(ies->data, mgmt->u.probe_resp.variable, ielen); | |
2a519311 | 784 | |
9caf0364 JB |
785 | if (ieee80211_is_probe_resp(mgmt->frame_control)) |
786 | rcu_assign_pointer(tmp.pub.proberesp_ies, ies); | |
787 | else | |
788 | rcu_assign_pointer(tmp.pub.beacon_ies, ies); | |
789 | rcu_assign_pointer(tmp.pub.ies, ies); | |
790 | ||
791 | memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN); | |
792 | tmp.pub.channel = channel; | |
793 | tmp.pub.signal = signal; | |
794 | tmp.pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); | |
795 | tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int); | |
796 | tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info); | |
797 | ||
798 | res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp); | |
2a519311 JB |
799 | if (!res) |
800 | return NULL; | |
801 | ||
e38f8a7a LR |
802 | if (res->pub.capability & WLAN_CAPABILITY_ESS) |
803 | regulatory_hint_found_beacon(wiphy, channel, gfp); | |
804 | ||
4ee3e063 | 805 | trace_cfg80211_return_bss(&res->pub); |
2a519311 JB |
806 | /* cfg80211_bss_update gives us a referenced result */ |
807 | return &res->pub; | |
808 | } | |
809 | EXPORT_SYMBOL(cfg80211_inform_bss_frame); | |
810 | ||
4c0c0b75 JB |
811 | void cfg80211_ref_bss(struct cfg80211_bss *pub) |
812 | { | |
813 | struct cfg80211_internal_bss *bss; | |
814 | ||
815 | if (!pub) | |
816 | return; | |
817 | ||
818 | bss = container_of(pub, struct cfg80211_internal_bss, pub); | |
819 | kref_get(&bss->ref); | |
820 | } | |
821 | EXPORT_SYMBOL(cfg80211_ref_bss); | |
822 | ||
2a519311 JB |
823 | void cfg80211_put_bss(struct cfg80211_bss *pub) |
824 | { | |
825 | struct cfg80211_internal_bss *bss; | |
826 | ||
827 | if (!pub) | |
828 | return; | |
829 | ||
830 | bss = container_of(pub, struct cfg80211_internal_bss, pub); | |
831 | kref_put(&bss->ref, bss_release); | |
832 | } | |
833 | EXPORT_SYMBOL(cfg80211_put_bss); | |
834 | ||
d491af19 JB |
835 | void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) |
836 | { | |
837 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); | |
838 | struct cfg80211_internal_bss *bss; | |
839 | ||
840 | if (WARN_ON(!pub)) | |
841 | return; | |
842 | ||
843 | bss = container_of(pub, struct cfg80211_internal_bss, pub); | |
844 | ||
845 | spin_lock_bh(&dev->bss_lock); | |
3207390a | 846 | if (!list_empty(&bss->list)) { |
2b78ac9b | 847 | __cfg80211_unlink_bss(dev, bss); |
3207390a | 848 | dev->bss_generation++; |
3207390a | 849 | } |
d491af19 | 850 | spin_unlock_bh(&dev->bss_lock); |
d491af19 JB |
851 | } |
852 | EXPORT_SYMBOL(cfg80211_unlink_bss); | |
853 | ||
3d23e349 | 854 | #ifdef CONFIG_CFG80211_WEXT |
2a519311 JB |
855 | int cfg80211_wext_siwscan(struct net_device *dev, |
856 | struct iw_request_info *info, | |
857 | union iwreq_data *wrqu, char *extra) | |
858 | { | |
859 | struct cfg80211_registered_device *rdev; | |
860 | struct wiphy *wiphy; | |
861 | struct iw_scan_req *wreq = NULL; | |
65486c8b | 862 | struct cfg80211_scan_request *creq = NULL; |
2a519311 JB |
863 | int i, err, n_channels = 0; |
864 | enum ieee80211_band band; | |
865 | ||
866 | if (!netif_running(dev)) | |
867 | return -ENETDOWN; | |
868 | ||
b2e3abdc HS |
869 | if (wrqu->data.length == sizeof(struct iw_scan_req)) |
870 | wreq = (struct iw_scan_req *)extra; | |
871 | ||
463d0183 | 872 | rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); |
2a519311 JB |
873 | |
874 | if (IS_ERR(rdev)) | |
875 | return PTR_ERR(rdev); | |
876 | ||
877 | if (rdev->scan_req) { | |
878 | err = -EBUSY; | |
879 | goto out; | |
880 | } | |
881 | ||
882 | wiphy = &rdev->wiphy; | |
883 | ||
b2e3abdc HS |
884 | /* Determine number of channels, needed to allocate creq */ |
885 | if (wreq && wreq->num_channels) | |
886 | n_channels = wreq->num_channels; | |
887 | else { | |
888 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) | |
889 | if (wiphy->bands[band]) | |
890 | n_channels += wiphy->bands[band]->n_channels; | |
891 | } | |
2a519311 JB |
892 | |
893 | creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) + | |
894 | n_channels * sizeof(void *), | |
895 | GFP_ATOMIC); | |
896 | if (!creq) { | |
897 | err = -ENOMEM; | |
898 | goto out; | |
899 | } | |
900 | ||
901 | creq->wiphy = wiphy; | |
fd014284 | 902 | creq->wdev = dev->ieee80211_ptr; |
5ba63533 JB |
903 | /* SSIDs come after channels */ |
904 | creq->ssids = (void *)&creq->channels[n_channels]; | |
2a519311 JB |
905 | creq->n_channels = n_channels; |
906 | creq->n_ssids = 1; | |
15d6030b | 907 | creq->scan_start = jiffies; |
2a519311 | 908 | |
b2e3abdc | 909 | /* translate "Scan on frequencies" request */ |
2a519311 JB |
910 | i = 0; |
911 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
912 | int j; | |
584991dc | 913 | |
2a519311 JB |
914 | if (!wiphy->bands[band]) |
915 | continue; | |
584991dc | 916 | |
2a519311 | 917 | for (j = 0; j < wiphy->bands[band]->n_channels; j++) { |
584991dc JB |
918 | /* ignore disabled channels */ |
919 | if (wiphy->bands[band]->channels[j].flags & | |
920 | IEEE80211_CHAN_DISABLED) | |
921 | continue; | |
b2e3abdc HS |
922 | |
923 | /* If we have a wireless request structure and the | |
924 | * wireless request specifies frequencies, then search | |
925 | * for the matching hardware channel. | |
926 | */ | |
927 | if (wreq && wreq->num_channels) { | |
928 | int k; | |
929 | int wiphy_freq = wiphy->bands[band]->channels[j].center_freq; | |
930 | for (k = 0; k < wreq->num_channels; k++) { | |
a4e7b730 | 931 | int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]); |
b2e3abdc HS |
932 | if (wext_freq == wiphy_freq) |
933 | goto wext_freq_found; | |
934 | } | |
935 | goto wext_freq_not_found; | |
936 | } | |
937 | ||
938 | wext_freq_found: | |
2a519311 JB |
939 | creq->channels[i] = &wiphy->bands[band]->channels[j]; |
940 | i++; | |
b2e3abdc | 941 | wext_freq_not_found: ; |
2a519311 JB |
942 | } |
943 | } | |
8862dc5f HS |
944 | /* No channels found? */ |
945 | if (!i) { | |
946 | err = -EINVAL; | |
947 | goto out; | |
948 | } | |
2a519311 | 949 | |
b2e3abdc HS |
950 | /* Set real number of channels specified in creq->channels[] */ |
951 | creq->n_channels = i; | |
2a519311 | 952 | |
b2e3abdc HS |
953 | /* translate "Scan for SSID" request */ |
954 | if (wreq) { | |
2a519311 | 955 | if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { |
65486c8b JB |
956 | if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) { |
957 | err = -EINVAL; | |
958 | goto out; | |
959 | } | |
2a519311 JB |
960 | memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len); |
961 | creq->ssids[0].ssid_len = wreq->essid_len; | |
962 | } | |
963 | if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) | |
964 | creq->n_ssids = 0; | |
965 | } | |
966 | ||
34850ab2 | 967 | for (i = 0; i < IEEE80211_NUM_BANDS; i++) |
a401d2bb JB |
968 | if (wiphy->bands[i]) |
969 | creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1; | |
34850ab2 | 970 | |
2a519311 | 971 | rdev->scan_req = creq; |
e35e4d28 | 972 | err = rdev_scan(rdev, creq); |
2a519311 JB |
973 | if (err) { |
974 | rdev->scan_req = NULL; | |
65486c8b | 975 | /* creq will be freed below */ |
463d0183 | 976 | } else { |
fd014284 | 977 | nl80211_send_scan_start(rdev, dev->ieee80211_ptr); |
65486c8b JB |
978 | /* creq now owned by driver */ |
979 | creq = NULL; | |
463d0183 JB |
980 | dev_hold(dev); |
981 | } | |
2a519311 | 982 | out: |
65486c8b | 983 | kfree(creq); |
4d0c8aea | 984 | cfg80211_unlock_rdev(rdev); |
2a519311 JB |
985 | return err; |
986 | } | |
ba44cb72 | 987 | EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan); |
2a519311 JB |
988 | |
989 | static void ieee80211_scan_add_ies(struct iw_request_info *info, | |
9caf0364 | 990 | const struct cfg80211_bss_ies *ies, |
2a519311 JB |
991 | char **current_ev, char *end_buf) |
992 | { | |
9caf0364 | 993 | const u8 *pos, *end, *next; |
2a519311 JB |
994 | struct iw_event iwe; |
995 | ||
9caf0364 | 996 | if (!ies) |
2a519311 JB |
997 | return; |
998 | ||
999 | /* | |
1000 | * If needed, fragment the IEs buffer (at IE boundaries) into short | |
1001 | * enough fragments to fit into IW_GENERIC_IE_MAX octet messages. | |
1002 | */ | |
9caf0364 JB |
1003 | pos = ies->data; |
1004 | end = pos + ies->len; | |
2a519311 JB |
1005 | |
1006 | while (end - pos > IW_GENERIC_IE_MAX) { | |
1007 | next = pos + 2 + pos[1]; | |
1008 | while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX) | |
1009 | next = next + 2 + next[1]; | |
1010 | ||
1011 | memset(&iwe, 0, sizeof(iwe)); | |
1012 | iwe.cmd = IWEVGENIE; | |
1013 | iwe.u.data.length = next - pos; | |
1014 | *current_ev = iwe_stream_add_point(info, *current_ev, | |
9caf0364 JB |
1015 | end_buf, &iwe, |
1016 | (void *)pos); | |
2a519311 JB |
1017 | |
1018 | pos = next; | |
1019 | } | |
1020 | ||
1021 | if (end > pos) { | |
1022 | memset(&iwe, 0, sizeof(iwe)); | |
1023 | iwe.cmd = IWEVGENIE; | |
1024 | iwe.u.data.length = end - pos; | |
1025 | *current_ev = iwe_stream_add_point(info, *current_ev, | |
9caf0364 JB |
1026 | end_buf, &iwe, |
1027 | (void *)pos); | |
2a519311 JB |
1028 | } |
1029 | } | |
1030 | ||
cb3a8eec DW |
1031 | static inline unsigned int elapsed_jiffies_msecs(unsigned long start) |
1032 | { | |
1033 | unsigned long end = jiffies; | |
1034 | ||
1035 | if (end >= start) | |
1036 | return jiffies_to_msecs(end - start); | |
1037 | ||
1038 | return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1); | |
1039 | } | |
2a519311 JB |
1040 | |
1041 | static char * | |
77965c97 JB |
1042 | ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info, |
1043 | struct cfg80211_internal_bss *bss, char *current_ev, | |
1044 | char *end_buf) | |
2a519311 | 1045 | { |
9caf0364 | 1046 | const struct cfg80211_bss_ies *ies; |
2a519311 | 1047 | struct iw_event iwe; |
9caf0364 | 1048 | const u8 *ie; |
2a519311 | 1049 | u8 *buf, *cfg, *p; |
9caf0364 | 1050 | int rem, i, sig; |
2a519311 JB |
1051 | bool ismesh = false; |
1052 | ||
1053 | memset(&iwe, 0, sizeof(iwe)); | |
1054 | iwe.cmd = SIOCGIWAP; | |
1055 | iwe.u.ap_addr.sa_family = ARPHRD_ETHER; | |
1056 | memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); | |
1057 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, | |
1058 | IW_EV_ADDR_LEN); | |
1059 | ||
1060 | memset(&iwe, 0, sizeof(iwe)); | |
1061 | iwe.cmd = SIOCGIWFREQ; | |
1062 | iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); | |
1063 | iwe.u.freq.e = 0; | |
1064 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, | |
1065 | IW_EV_FREQ_LEN); | |
1066 | ||
1067 | memset(&iwe, 0, sizeof(iwe)); | |
1068 | iwe.cmd = SIOCGIWFREQ; | |
1069 | iwe.u.freq.m = bss->pub.channel->center_freq; | |
1070 | iwe.u.freq.e = 6; | |
1071 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, | |
1072 | IW_EV_FREQ_LEN); | |
1073 | ||
77965c97 | 1074 | if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) { |
2a519311 JB |
1075 | memset(&iwe, 0, sizeof(iwe)); |
1076 | iwe.cmd = IWEVQUAL; | |
1077 | iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED | | |
1078 | IW_QUAL_NOISE_INVALID | | |
a77b8552 | 1079 | IW_QUAL_QUAL_UPDATED; |
77965c97 | 1080 | switch (wiphy->signal_type) { |
2a519311 | 1081 | case CFG80211_SIGNAL_TYPE_MBM: |
a77b8552 JB |
1082 | sig = bss->pub.signal / 100; |
1083 | iwe.u.qual.level = sig; | |
2a519311 | 1084 | iwe.u.qual.updated |= IW_QUAL_DBM; |
a77b8552 JB |
1085 | if (sig < -110) /* rather bad */ |
1086 | sig = -110; | |
1087 | else if (sig > -40) /* perfect */ | |
1088 | sig = -40; | |
1089 | /* will give a range of 0 .. 70 */ | |
1090 | iwe.u.qual.qual = sig + 110; | |
2a519311 JB |
1091 | break; |
1092 | case CFG80211_SIGNAL_TYPE_UNSPEC: | |
1093 | iwe.u.qual.level = bss->pub.signal; | |
a77b8552 JB |
1094 | /* will give range 0 .. 100 */ |
1095 | iwe.u.qual.qual = bss->pub.signal; | |
2a519311 JB |
1096 | break; |
1097 | default: | |
1098 | /* not reached */ | |
1099 | break; | |
1100 | } | |
1101 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, | |
1102 | &iwe, IW_EV_QUAL_LEN); | |
1103 | } | |
1104 | ||
1105 | memset(&iwe, 0, sizeof(iwe)); | |
1106 | iwe.cmd = SIOCGIWENCODE; | |
1107 | if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY) | |
1108 | iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; | |
1109 | else | |
1110 | iwe.u.data.flags = IW_ENCODE_DISABLED; | |
1111 | iwe.u.data.length = 0; | |
1112 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, | |
1113 | &iwe, ""); | |
1114 | ||
9caf0364 JB |
1115 | rcu_read_lock(); |
1116 | ies = rcu_dereference(bss->pub.ies); | |
1117 | if (ies) { | |
1118 | rem = ies->len; | |
1119 | ie = ies->data; | |
1120 | } else { | |
1121 | rem = 0; | |
1122 | ie = NULL; | |
1123 | } | |
1124 | ||
1125 | while (ies && rem >= 2) { | |
2a519311 JB |
1126 | /* invalid data */ |
1127 | if (ie[1] > rem - 2) | |
1128 | break; | |
1129 | ||
1130 | switch (ie[0]) { | |
1131 | case WLAN_EID_SSID: | |
1132 | memset(&iwe, 0, sizeof(iwe)); | |
1133 | iwe.cmd = SIOCGIWESSID; | |
1134 | iwe.u.data.length = ie[1]; | |
1135 | iwe.u.data.flags = 1; | |
1136 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, | |
9caf0364 | 1137 | &iwe, (u8 *)ie + 2); |
2a519311 JB |
1138 | break; |
1139 | case WLAN_EID_MESH_ID: | |
1140 | memset(&iwe, 0, sizeof(iwe)); | |
1141 | iwe.cmd = SIOCGIWESSID; | |
1142 | iwe.u.data.length = ie[1]; | |
1143 | iwe.u.data.flags = 1; | |
1144 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, | |
9caf0364 | 1145 | &iwe, (u8 *)ie + 2); |
2a519311 JB |
1146 | break; |
1147 | case WLAN_EID_MESH_CONFIG: | |
1148 | ismesh = true; | |
136cfa28 | 1149 | if (ie[1] != sizeof(struct ieee80211_meshconf_ie)) |
2a519311 JB |
1150 | break; |
1151 | buf = kmalloc(50, GFP_ATOMIC); | |
1152 | if (!buf) | |
1153 | break; | |
9caf0364 | 1154 | cfg = (u8 *)ie + 2; |
2a519311 JB |
1155 | memset(&iwe, 0, sizeof(iwe)); |
1156 | iwe.cmd = IWEVCUSTOM; | |
76aa5e70 RP |
1157 | sprintf(buf, "Mesh Network Path Selection Protocol ID: " |
1158 | "0x%02X", cfg[0]); | |
2a519311 JB |
1159 | iwe.u.data.length = strlen(buf); |
1160 | current_ev = iwe_stream_add_point(info, current_ev, | |
1161 | end_buf, | |
1162 | &iwe, buf); | |
76aa5e70 RP |
1163 | sprintf(buf, "Path Selection Metric ID: 0x%02X", |
1164 | cfg[1]); | |
2a519311 JB |
1165 | iwe.u.data.length = strlen(buf); |
1166 | current_ev = iwe_stream_add_point(info, current_ev, | |
1167 | end_buf, | |
1168 | &iwe, buf); | |
76aa5e70 RP |
1169 | sprintf(buf, "Congestion Control Mode ID: 0x%02X", |
1170 | cfg[2]); | |
2a519311 JB |
1171 | iwe.u.data.length = strlen(buf); |
1172 | current_ev = iwe_stream_add_point(info, current_ev, | |
1173 | end_buf, | |
1174 | &iwe, buf); | |
76aa5e70 | 1175 | sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]); |
2a519311 JB |
1176 | iwe.u.data.length = strlen(buf); |
1177 | current_ev = iwe_stream_add_point(info, current_ev, | |
1178 | end_buf, | |
1179 | &iwe, buf); | |
76aa5e70 RP |
1180 | sprintf(buf, "Authentication ID: 0x%02X", cfg[4]); |
1181 | iwe.u.data.length = strlen(buf); | |
1182 | current_ev = iwe_stream_add_point(info, current_ev, | |
1183 | end_buf, | |
1184 | &iwe, buf); | |
1185 | sprintf(buf, "Formation Info: 0x%02X", cfg[5]); | |
1186 | iwe.u.data.length = strlen(buf); | |
1187 | current_ev = iwe_stream_add_point(info, current_ev, | |
1188 | end_buf, | |
1189 | &iwe, buf); | |
1190 | sprintf(buf, "Capabilities: 0x%02X", cfg[6]); | |
2a519311 JB |
1191 | iwe.u.data.length = strlen(buf); |
1192 | current_ev = iwe_stream_add_point(info, current_ev, | |
1193 | end_buf, | |
1194 | &iwe, buf); | |
1195 | kfree(buf); | |
1196 | break; | |
1197 | case WLAN_EID_SUPP_RATES: | |
1198 | case WLAN_EID_EXT_SUPP_RATES: | |
1199 | /* display all supported rates in readable format */ | |
1200 | p = current_ev + iwe_stream_lcp_len(info); | |
1201 | ||
1202 | memset(&iwe, 0, sizeof(iwe)); | |
1203 | iwe.cmd = SIOCGIWRATE; | |
1204 | /* Those two flags are ignored... */ | |
1205 | iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; | |
1206 | ||
1207 | for (i = 0; i < ie[1]; i++) { | |
1208 | iwe.u.bitrate.value = | |
1209 | ((ie[i + 2] & 0x7f) * 500000); | |
1210 | p = iwe_stream_add_value(info, current_ev, p, | |
1211 | end_buf, &iwe, IW_EV_PARAM_LEN); | |
1212 | } | |
1213 | current_ev = p; | |
1214 | break; | |
1215 | } | |
1216 | rem -= ie[1] + 2; | |
1217 | ie += ie[1] + 2; | |
1218 | } | |
1219 | ||
f64f9e71 JP |
1220 | if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) || |
1221 | ismesh) { | |
2a519311 JB |
1222 | memset(&iwe, 0, sizeof(iwe)); |
1223 | iwe.cmd = SIOCGIWMODE; | |
1224 | if (ismesh) | |
1225 | iwe.u.mode = IW_MODE_MESH; | |
1226 | else if (bss->pub.capability & WLAN_CAPABILITY_ESS) | |
1227 | iwe.u.mode = IW_MODE_MASTER; | |
1228 | else | |
1229 | iwe.u.mode = IW_MODE_ADHOC; | |
1230 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, | |
1231 | &iwe, IW_EV_UINT_LEN); | |
1232 | } | |
1233 | ||
1234 | buf = kmalloc(30, GFP_ATOMIC); | |
1235 | if (buf) { | |
1236 | memset(&iwe, 0, sizeof(iwe)); | |
1237 | iwe.cmd = IWEVCUSTOM; | |
1238 | sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf)); | |
1239 | iwe.u.data.length = strlen(buf); | |
1240 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, | |
1241 | &iwe, buf); | |
1242 | memset(&iwe, 0, sizeof(iwe)); | |
1243 | iwe.cmd = IWEVCUSTOM; | |
cb3a8eec DW |
1244 | sprintf(buf, " Last beacon: %ums ago", |
1245 | elapsed_jiffies_msecs(bss->ts)); | |
2a519311 JB |
1246 | iwe.u.data.length = strlen(buf); |
1247 | current_ev = iwe_stream_add_point(info, current_ev, | |
1248 | end_buf, &iwe, buf); | |
1249 | kfree(buf); | |
1250 | } | |
1251 | ||
9caf0364 JB |
1252 | ieee80211_scan_add_ies(info, ies, ¤t_ev, end_buf); |
1253 | rcu_read_unlock(); | |
2a519311 JB |
1254 | |
1255 | return current_ev; | |
1256 | } | |
1257 | ||
1258 | ||
1259 | static int ieee80211_scan_results(struct cfg80211_registered_device *dev, | |
1260 | struct iw_request_info *info, | |
1261 | char *buf, size_t len) | |
1262 | { | |
1263 | char *current_ev = buf; | |
1264 | char *end_buf = buf + len; | |
1265 | struct cfg80211_internal_bss *bss; | |
1266 | ||
1267 | spin_lock_bh(&dev->bss_lock); | |
1268 | cfg80211_bss_expire(dev); | |
1269 | ||
1270 | list_for_each_entry(bss, &dev->bss_list, list) { | |
1271 | if (buf + len - current_ev <= IW_EV_ADDR_LEN) { | |
1272 | spin_unlock_bh(&dev->bss_lock); | |
1273 | return -E2BIG; | |
1274 | } | |
77965c97 JB |
1275 | current_ev = ieee80211_bss(&dev->wiphy, info, bss, |
1276 | current_ev, end_buf); | |
2a519311 JB |
1277 | } |
1278 | spin_unlock_bh(&dev->bss_lock); | |
1279 | return current_ev - buf; | |
1280 | } | |
1281 | ||
1282 | ||
1283 | int cfg80211_wext_giwscan(struct net_device *dev, | |
1284 | struct iw_request_info *info, | |
1285 | struct iw_point *data, char *extra) | |
1286 | { | |
1287 | struct cfg80211_registered_device *rdev; | |
1288 | int res; | |
1289 | ||
1290 | if (!netif_running(dev)) | |
1291 | return -ENETDOWN; | |
1292 | ||
463d0183 | 1293 | rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); |
2a519311 JB |
1294 | |
1295 | if (IS_ERR(rdev)) | |
1296 | return PTR_ERR(rdev); | |
1297 | ||
1298 | if (rdev->scan_req) { | |
1299 | res = -EAGAIN; | |
1300 | goto out; | |
1301 | } | |
1302 | ||
1303 | res = ieee80211_scan_results(rdev, info, extra, data->length); | |
1304 | data->length = 0; | |
1305 | if (res >= 0) { | |
1306 | data->length = res; | |
1307 | res = 0; | |
1308 | } | |
1309 | ||
1310 | out: | |
4d0c8aea | 1311 | cfg80211_unlock_rdev(rdev); |
2a519311 JB |
1312 | return res; |
1313 | } | |
ba44cb72 | 1314 | EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan); |
2a519311 | 1315 | #endif |