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