scan_helpers.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * WPA Supplicant - Helper functions for scan result processing
  3. * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "drivers/driver.h"
  17. #include "ieee802_11_defs.h"
  18. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  19. {
  20. const u8 *end, *pos;
  21. pos = (const u8 *) (res + 1);
  22. end = pos + res->ie_len;
  23. while (pos + 1 < end) {
  24. if (pos + 2 + pos[1] > end)
  25. break;
  26. if (pos[0] == ie)
  27. return pos;
  28. pos += 2 + pos[1];
  29. }
  30. return NULL;
  31. }
  32. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  33. u32 vendor_type)
  34. {
  35. const u8 *end, *pos;
  36. pos = (const u8 *) (res + 1);
  37. end = pos + res->ie_len;
  38. while (pos + 1 < end) {
  39. if (pos + 2 + pos[1] > end)
  40. break;
  41. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  42. vendor_type == WPA_GET_BE32(&pos[2]))
  43. return pos;
  44. pos += 2 + pos[1];
  45. }
  46. return NULL;
  47. }
  48. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  49. u32 vendor_type)
  50. {
  51. struct wpabuf *buf;
  52. const u8 *end, *pos;
  53. buf = wpabuf_alloc(res->ie_len);
  54. if (buf == NULL)
  55. return NULL;
  56. pos = (const u8 *) (res + 1);
  57. end = pos + res->ie_len;
  58. while (pos + 1 < end) {
  59. if (pos + 2 + pos[1] > end)
  60. break;
  61. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  62. vendor_type == WPA_GET_BE32(&pos[2]))
  63. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  64. pos += 2 + pos[1];
  65. }
  66. if (wpabuf_len(buf) == 0) {
  67. wpabuf_free(buf);
  68. buf = NULL;
  69. }
  70. return buf;
  71. }
  72. int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  73. {
  74. int rate = 0;
  75. const u8 *ie;
  76. int i;
  77. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  78. for (i = 0; ie && i < ie[1]; i++) {
  79. if ((ie[i + 2] & 0x7f) > rate)
  80. rate = ie[i + 2] & 0x7f;
  81. }
  82. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  83. for (i = 0; ie && i < ie[1]; i++) {
  84. if ((ie[i + 2] & 0x7f) > rate)
  85. rate = ie[i + 2] & 0x7f;
  86. }
  87. return rate;
  88. }
  89. void wpa_scan_results_free(struct wpa_scan_results *res)
  90. {
  91. size_t i;
  92. if (res == NULL)
  93. return;
  94. for (i = 0; i < res->num; i++)
  95. os_free(res->res[i]);
  96. os_free(res->res);
  97. os_free(res);
  98. }
  99. /* Compare function for sorting scan results. Return >0 if @b is considered
  100. * better. */
  101. static int wpa_scan_result_compar(const void *a, const void *b)
  102. {
  103. struct wpa_scan_res **_wa = (void *) a;
  104. struct wpa_scan_res **_wb = (void *) b;
  105. struct wpa_scan_res *wa = *_wa;
  106. struct wpa_scan_res *wb = *_wb;
  107. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  108. /* WPA/WPA2 support preferred */
  109. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  110. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  111. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  112. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  113. if (wpa_b && !wpa_a)
  114. return 1;
  115. if (!wpa_b && wpa_a)
  116. return -1;
  117. /* privacy support preferred */
  118. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  119. (wb->caps & IEEE80211_CAP_PRIVACY))
  120. return 1;
  121. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  122. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  123. return -1;
  124. /* best/max rate preferred if signal level close enough XXX */
  125. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  126. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  127. maxrate_a = wpa_scan_get_max_rate(wa);
  128. maxrate_b = wpa_scan_get_max_rate(wb);
  129. if (maxrate_a != maxrate_b)
  130. return maxrate_b - maxrate_a;
  131. }
  132. /* use freq for channel preference */
  133. /* all things being equal, use signal level; if signal levels are
  134. * identical, use quality values since some drivers may only report
  135. * that value and leave the signal level zero */
  136. if (wb->level == wa->level)
  137. return wb->qual - wa->qual;
  138. return wb->level - wa->level;
  139. }
  140. void wpa_scan_sort_results(struct wpa_scan_results *res)
  141. {
  142. qsort(res->res, res->num, sizeof(struct wpa_scan_res *),
  143. wpa_scan_result_compar);
  144. }