bss.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/defs.h"
  17. #include "common/ieee802_11_common.h"
  18. #include "crypto/sha1.h"
  19. #include "wlantest.h"
  20. struct wlantest_bss * bss_get(struct wlantest *wt, const u8 *bssid)
  21. {
  22. struct wlantest_bss *bss;
  23. if (bssid[0] & 0x01)
  24. return NULL; /* Skip group addressed frames */
  25. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  26. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  27. return bss;
  28. }
  29. bss = os_zalloc(sizeof(*bss));
  30. if (bss == NULL)
  31. return NULL;
  32. dl_list_init(&bss->sta);
  33. dl_list_init(&bss->pmk);
  34. os_memcpy(bss->bssid, bssid, ETH_ALEN);
  35. dl_list_add(&wt->bss, &bss->list);
  36. wpa_printf(MSG_DEBUG, "Discovered new BSS - " MACSTR,
  37. MAC2STR(bss->bssid));
  38. return bss;
  39. }
  40. void pmk_deinit(struct wlantest_pmk *pmk)
  41. {
  42. dl_list_del(&pmk->list);
  43. os_free(pmk);
  44. }
  45. void bss_deinit(struct wlantest_bss *bss)
  46. {
  47. struct wlantest_sta *sta, *n;
  48. struct wlantest_pmk *pmk, *np;
  49. dl_list_for_each_safe(sta, n, &bss->sta, struct wlantest_sta, list)
  50. sta_deinit(sta);
  51. dl_list_for_each_safe(pmk, np, &bss->pmk, struct wlantest_pmk, list)
  52. pmk_deinit(pmk);
  53. dl_list_del(&bss->list);
  54. os_free(bss);
  55. }
  56. static void bss_add_pmk(struct wlantest *wt, struct wlantest_bss *bss)
  57. {
  58. struct wlantest_passphrase *p;
  59. struct wlantest_pmk *pmk;
  60. dl_list_for_each(p, &wt->passphrase, struct wlantest_passphrase, list)
  61. {
  62. if (!is_zero_ether_addr(p->bssid) &&
  63. os_memcmp(p->bssid, bss->bssid, ETH_ALEN) != 0)
  64. continue;
  65. if (p->ssid_len &&
  66. (p->ssid_len != bss->ssid_len ||
  67. os_memcmp(p->ssid, bss->ssid, p->ssid_len) != 0))
  68. continue;
  69. pmk = os_zalloc(sizeof(*pmk));
  70. if (pmk == NULL)
  71. break;
  72. if (pbkdf2_sha1(p->passphrase, (char *) bss->ssid,
  73. bss->ssid_len, 4096,
  74. pmk->pmk, sizeof(pmk->pmk)) < 0) {
  75. os_free(pmk);
  76. continue;
  77. }
  78. wpa_printf(MSG_INFO, "Add possible PMK for BSSID " MACSTR
  79. " based on passphrase '%s'",
  80. MAC2STR(bss->bssid), p->passphrase);
  81. wpa_hexdump(MSG_DEBUG, "Possible PMK",
  82. pmk->pmk, sizeof(pmk->pmk));
  83. dl_list_add(&bss->pmk, &pmk->list);
  84. }
  85. }
  86. void bss_update(struct wlantest *wt, struct wlantest_bss *bss,
  87. struct ieee802_11_elems *elems)
  88. {
  89. struct wpa_ie_data data;
  90. int update = 0;
  91. if (elems->ssid == NULL || elems->ssid_len > 32) {
  92. wpa_printf(MSG_INFO, "Invalid or missing SSID in a Beacon "
  93. "frame for " MACSTR, MAC2STR(bss->bssid));
  94. bss->parse_error_reported = 1;
  95. return;
  96. }
  97. if (bss->ssid_len != elems->ssid_len ||
  98. os_memcmp(bss->ssid, elems->ssid, bss->ssid_len) != 0) {
  99. wpa_printf(MSG_DEBUG, "Store SSID '%s' for BSSID " MACSTR,
  100. wpa_ssid_txt(elems->ssid, elems->ssid_len),
  101. MAC2STR(bss->bssid));
  102. os_memcpy(bss->ssid, elems->ssid, elems->ssid_len);
  103. bss->ssid_len = elems->ssid_len;
  104. bss_add_pmk(wt, bss);
  105. }
  106. if (elems->rsn_ie == NULL) {
  107. if (bss->rsnie[0]) {
  108. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE removed",
  109. MAC2STR(bss->bssid));
  110. bss->rsnie[0] = 0;
  111. update = 1;
  112. }
  113. } else {
  114. if (bss->rsnie[0] == 0 ||
  115. os_memcmp(bss->rsnie, elems->rsn_ie - 2,
  116. elems->rsn_ie_len + 2) != 0) {
  117. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE "
  118. "stored", MAC2STR(bss->bssid));
  119. wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
  120. elems->rsn_ie_len + 2);
  121. update = 1;
  122. }
  123. os_memcpy(bss->rsnie, elems->rsn_ie - 2,
  124. elems->rsn_ie_len + 2);
  125. }
  126. if (elems->wpa_ie == NULL) {
  127. if (bss->wpaie[0]) {
  128. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE removed",
  129. MAC2STR(bss->bssid));
  130. bss->wpaie[0] = 0;
  131. update = 1;
  132. }
  133. } else {
  134. if (bss->wpaie[0] == 0 ||
  135. os_memcmp(bss->wpaie, elems->wpa_ie - 2,
  136. elems->wpa_ie_len + 2) != 0) {
  137. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE "
  138. "stored", MAC2STR(bss->bssid));
  139. wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
  140. elems->wpa_ie_len + 2);
  141. update = 1;
  142. }
  143. os_memcpy(bss->wpaie, elems->wpa_ie - 2,
  144. elems->wpa_ie_len + 2);
  145. }
  146. if (!update)
  147. return;
  148. bss->proto = 0;
  149. bss->pairwise_cipher = 0;
  150. bss->group_cipher = 0;
  151. bss->key_mgmt = 0;
  152. bss->rsn_capab = 0;
  153. bss->mgmt_group_cipher = 0;
  154. if (bss->wpaie[0]) {
  155. if (wpa_parse_wpa_ie_wpa(bss->wpaie, 2 + bss->wpaie[1], &data)
  156. < 0) {
  157. wpa_printf(MSG_INFO, "Failed to parse WPA IE from "
  158. MACSTR, MAC2STR(bss->bssid));
  159. } else {
  160. bss->proto |= data.proto;
  161. bss->pairwise_cipher |= data.pairwise_cipher;
  162. bss->group_cipher |= data.group_cipher;
  163. bss->key_mgmt |= data.key_mgmt;
  164. bss->rsn_capab = data.capabilities;
  165. bss->mgmt_group_cipher |= data.mgmt_group_cipher;
  166. }
  167. }
  168. if (bss->rsnie[0]) {
  169. if (wpa_parse_wpa_ie_rsn(bss->rsnie, 2 + bss->rsnie[1], &data)
  170. < 0) {
  171. wpa_printf(MSG_INFO, "Failed to parse RSN IE from "
  172. MACSTR, MAC2STR(bss->bssid));
  173. } else {
  174. bss->proto |= data.proto;
  175. bss->pairwise_cipher |= data.pairwise_cipher;
  176. bss->group_cipher |= data.group_cipher;
  177. bss->key_mgmt |= data.key_mgmt;
  178. bss->rsn_capab = data.capabilities;
  179. bss->mgmt_group_cipher |= data.mgmt_group_cipher;
  180. }
  181. }
  182. if (!(bss->proto & WPA_PROTO_RSN) ||
  183. !(bss->rsn_capab & WPA_CAPABILITY_MFPC))
  184. bss->mgmt_group_cipher = 0;
  185. wpa_printf(MSG_INFO, "BSS " MACSTR
  186. " proto=%s%s%s"
  187. "pairwise=%s%s%s%s"
  188. "group=%s%s%s%s%s%s"
  189. "mgmt_group_cipher=%s"
  190. "key_mgmt=%s%s%s%s%s%s%s%s"
  191. "rsn_capab=%s%s%s%s%s",
  192. MAC2STR(bss->bssid),
  193. bss->proto == 0 ? "OPEN " : "",
  194. bss->proto & WPA_PROTO_WPA ? "WPA " : "",
  195. bss->proto & WPA_PROTO_RSN ? "WPA2 " : "",
  196. bss->pairwise_cipher == 0 ? "N/A " : "",
  197. bss->pairwise_cipher & WPA_CIPHER_NONE ? "NONE " : "",
  198. bss->pairwise_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
  199. bss->pairwise_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
  200. bss->group_cipher == 0 ? "N/A " : "",
  201. bss->group_cipher & WPA_CIPHER_NONE ? "NONE " : "",
  202. bss->group_cipher & WPA_CIPHER_WEP40 ? "WEP40 " : "",
  203. bss->group_cipher & WPA_CIPHER_WEP104 ? "WEP104 " : "",
  204. bss->group_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
  205. bss->group_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
  206. bss->mgmt_group_cipher & WPA_CIPHER_AES_128_CMAC ? "BIP " :
  207. "N/A ",
  208. bss->key_mgmt == 0 ? "N/A " : "",
  209. bss->key_mgmt & WPA_KEY_MGMT_IEEE8021X ? "EAP " : "",
  210. bss->key_mgmt & WPA_KEY_MGMT_PSK ? "PSK " : "",
  211. bss->key_mgmt & WPA_KEY_MGMT_WPA_NONE ? "WPA-NONE " : "",
  212. bss->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X ? "FT-EAP " : "",
  213. bss->key_mgmt & WPA_KEY_MGMT_FT_PSK ? "FT-PSK " : "",
  214. bss->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256 ?
  215. "EAP-SHA256 " : "",
  216. bss->key_mgmt & WPA_KEY_MGMT_PSK_SHA256 ?
  217. "PSK-SHA256 " : "",
  218. bss->rsn_capab & WPA_CAPABILITY_PREAUTH ? "PREAUTH " : "",
  219. bss->rsn_capab & WPA_CAPABILITY_NO_PAIRWISE ?
  220. "NO_PAIRWISE " : "",
  221. bss->rsn_capab & WPA_CAPABILITY_MFPR ? "MFPR " : "",
  222. bss->rsn_capab & WPA_CAPABILITY_MFPC ? "MFPC " : "",
  223. bss->rsn_capab & WPA_CAPABILITY_PEERKEY_ENABLED ?
  224. "PEERKEY " : "");
  225. }