bss.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * BSS list
  3. * Copyright (c) 2010, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "common/ieee802_11_common.h"
  17. #include "crypto/sha1.h"
  18. #include "wlantest.h"
  19. struct wlantest_bss * bss_get(struct wlantest *wt, const u8 *bssid)
  20. {
  21. struct wlantest_bss *bss;
  22. if (bssid[0] & 0x01)
  23. return NULL; /* Skip group addressed frames */
  24. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  25. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  26. return bss;
  27. }
  28. bss = os_zalloc(sizeof(*bss));
  29. if (bss == NULL)
  30. return NULL;
  31. dl_list_init(&bss->sta);
  32. dl_list_init(&bss->pmk);
  33. os_memcpy(bss->bssid, bssid, ETH_ALEN);
  34. dl_list_add(&wt->bss, &bss->list);
  35. wpa_printf(MSG_DEBUG, "Discovered new BSS - " MACSTR,
  36. MAC2STR(bss->bssid));
  37. return bss;
  38. }
  39. void pmk_deinit(struct wlantest_pmk *pmk)
  40. {
  41. dl_list_del(&pmk->list);
  42. os_free(pmk);
  43. }
  44. void bss_deinit(struct wlantest_bss *bss)
  45. {
  46. struct wlantest_sta *sta, *n;
  47. struct wlantest_pmk *pmk, *np;
  48. dl_list_for_each_safe(sta, n, &bss->sta, struct wlantest_sta, list)
  49. sta_deinit(sta);
  50. dl_list_for_each_safe(pmk, np, &bss->pmk, struct wlantest_pmk, list)
  51. pmk_deinit(pmk);
  52. dl_list_del(&bss->list);
  53. os_free(bss);
  54. }
  55. static void bss_add_pmk(struct wlantest *wt, struct wlantest_bss *bss)
  56. {
  57. struct wlantest_passphrase *p;
  58. struct wlantest_pmk *pmk;
  59. dl_list_for_each(p, &wt->passphrase, struct wlantest_passphrase, list)
  60. {
  61. if (!is_zero_ether_addr(p->bssid) &&
  62. os_memcmp(p->bssid, bss->bssid, ETH_ALEN) != 0)
  63. continue;
  64. if (p->ssid_len &&
  65. (p->ssid_len != bss->ssid_len ||
  66. os_memcmp(p->ssid, bss->ssid, p->ssid_len) != 0))
  67. continue;
  68. pmk = os_zalloc(sizeof(*pmk));
  69. if (pmk == NULL)
  70. break;
  71. if (pbkdf2_sha1(p->passphrase, (char *) bss->ssid,
  72. bss->ssid_len, 4096,
  73. pmk->pmk, sizeof(pmk->pmk)) < 0) {
  74. os_free(pmk);
  75. continue;
  76. }
  77. wpa_printf(MSG_INFO, "Add possible PMK for BSSID " MACSTR
  78. " based on passphrase '%s'",
  79. MAC2STR(bss->bssid), p->passphrase);
  80. wpa_hexdump(MSG_DEBUG, "Possible PMK",
  81. pmk->pmk, sizeof(pmk->pmk));
  82. dl_list_add(&bss->pmk, &pmk->list);
  83. }
  84. }
  85. void bss_update(struct wlantest *wt, struct wlantest_bss *bss,
  86. struct ieee802_11_elems *elems)
  87. {
  88. if (elems->ssid == NULL || elems->ssid_len > 32) {
  89. wpa_printf(MSG_INFO, "Invalid or missing SSID in a Beacon "
  90. "frame for " MACSTR, MAC2STR(bss->bssid));
  91. bss->parse_error_reported = 1;
  92. return;
  93. }
  94. if (bss->ssid_len != elems->ssid_len ||
  95. os_memcmp(bss->ssid, elems->ssid, bss->ssid_len) != 0) {
  96. wpa_printf(MSG_DEBUG, "Store SSID '%s' for BSSID " MACSTR,
  97. wpa_ssid_txt(elems->ssid, elems->ssid_len),
  98. MAC2STR(bss->bssid));
  99. os_memcpy(bss->ssid, elems->ssid, elems->ssid_len);
  100. bss->ssid_len = elems->ssid_len;
  101. bss_add_pmk(wt, bss);
  102. }
  103. if (elems->rsn_ie == NULL) {
  104. if (bss->rsnie[0]) {
  105. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE removed",
  106. MAC2STR(bss->bssid));
  107. bss->rsnie[0] = 0;
  108. }
  109. } else {
  110. if (bss->rsnie[0] == 0 ||
  111. os_memcmp(bss->rsnie, elems->rsn_ie - 2,
  112. elems->rsn_ie_len + 2) != 0) {
  113. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE "
  114. "stored", MAC2STR(bss->bssid));
  115. wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
  116. elems->rsn_ie_len + 2);
  117. }
  118. os_memcpy(bss->rsnie, elems->rsn_ie - 2,
  119. elems->rsn_ie_len + 2);
  120. }
  121. if (elems->wpa_ie == NULL) {
  122. if (bss->wpaie[0]) {
  123. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE removed",
  124. MAC2STR(bss->bssid));
  125. bss->wpaie[0] = 0;
  126. }
  127. } else {
  128. if (bss->wpaie[0] == 0 ||
  129. os_memcmp(bss->wpaie, elems->wpa_ie - 2,
  130. elems->wpa_ie_len + 2) != 0) {
  131. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE "
  132. "stored", MAC2STR(bss->bssid));
  133. wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
  134. elems->wpa_ie_len + 2);
  135. }
  136. os_memcpy(bss->wpaie, elems->wpa_ie - 2,
  137. elems->wpa_ie_len + 2);
  138. }
  139. }