bss.c 7.5 KB

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