bss.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * BSS list
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "common/defs.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "crypto/sha1.h"
  14. #include "wlantest.h"
  15. struct wlantest_bss * bss_find(struct wlantest *wt, const u8 *bssid)
  16. {
  17. struct wlantest_bss *bss;
  18. dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) {
  19. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  20. return bss;
  21. }
  22. return NULL;
  23. }
  24. struct wlantest_bss * bss_get(struct wlantest *wt, const u8 *bssid)
  25. {
  26. struct wlantest_bss *bss;
  27. if (bssid[0] & 0x01)
  28. return NULL; /* Skip group addressed frames */
  29. bss = bss_find(wt, bssid);
  30. if (bss)
  31. return bss;
  32. bss = os_zalloc(sizeof(*bss));
  33. if (bss == NULL)
  34. return NULL;
  35. dl_list_init(&bss->sta);
  36. dl_list_init(&bss->pmk);
  37. dl_list_init(&bss->tdls);
  38. os_memcpy(bss->bssid, bssid, ETH_ALEN);
  39. dl_list_add(&wt->bss, &bss->list);
  40. wpa_printf(MSG_DEBUG, "Discovered new BSS - " MACSTR,
  41. MAC2STR(bss->bssid));
  42. return bss;
  43. }
  44. void pmk_deinit(struct wlantest_pmk *pmk)
  45. {
  46. dl_list_del(&pmk->list);
  47. os_free(pmk);
  48. }
  49. void tdls_deinit(struct wlantest_tdls *tdls)
  50. {
  51. dl_list_del(&tdls->list);
  52. os_free(tdls);
  53. }
  54. void bss_deinit(struct wlantest_bss *bss)
  55. {
  56. struct wlantest_sta *sta, *n;
  57. struct wlantest_pmk *pmk, *np;
  58. struct wlantest_tdls *tdls, *nt;
  59. dl_list_for_each_safe(sta, n, &bss->sta, struct wlantest_sta, list)
  60. sta_deinit(sta);
  61. dl_list_for_each_safe(pmk, np, &bss->pmk, struct wlantest_pmk, list)
  62. pmk_deinit(pmk);
  63. dl_list_for_each_safe(tdls, nt, &bss->tdls, struct wlantest_tdls, list)
  64. tdls_deinit(tdls);
  65. dl_list_del(&bss->list);
  66. os_free(bss);
  67. }
  68. int bss_add_pmk_from_passphrase(struct wlantest_bss *bss,
  69. const char *passphrase)
  70. {
  71. struct wlantest_pmk *pmk;
  72. pmk = os_zalloc(sizeof(*pmk));
  73. if (pmk == NULL)
  74. return -1;
  75. if (pbkdf2_sha1(passphrase, bss->ssid, bss->ssid_len, 4096,
  76. pmk->pmk, PMK_LEN) < 0) {
  77. os_free(pmk);
  78. return -1;
  79. }
  80. wpa_printf(MSG_INFO, "Add possible PMK for BSSID " MACSTR
  81. " based on passphrase '%s'",
  82. MAC2STR(bss->bssid), passphrase);
  83. wpa_hexdump(MSG_DEBUG, "Possible PMK", pmk->pmk, PMK_LEN);
  84. dl_list_add(&bss->pmk, &pmk->list);
  85. return 0;
  86. }
  87. static void bss_add_pmk(struct wlantest *wt, struct wlantest_bss *bss)
  88. {
  89. struct wlantest_passphrase *p;
  90. dl_list_for_each(p, &wt->passphrase, struct wlantest_passphrase, list)
  91. {
  92. if (!is_zero_ether_addr(p->bssid) &&
  93. os_memcmp(p->bssid, bss->bssid, ETH_ALEN) != 0)
  94. continue;
  95. if (p->ssid_len &&
  96. (p->ssid_len != bss->ssid_len ||
  97. os_memcmp(p->ssid, bss->ssid, p->ssid_len) != 0))
  98. continue;
  99. if (bss_add_pmk_from_passphrase(bss, p->passphrase) < 0)
  100. break;
  101. }
  102. }
  103. void bss_update(struct wlantest *wt, struct wlantest_bss *bss,
  104. struct ieee802_11_elems *elems)
  105. {
  106. struct wpa_ie_data data;
  107. int update = 0;
  108. if (bss->capab_info != bss->prev_capab_info)
  109. update = 1;
  110. if (elems->ssid == NULL || elems->ssid_len > 32) {
  111. wpa_printf(MSG_INFO, "Invalid or missing SSID in a Beacon "
  112. "frame for " MACSTR, MAC2STR(bss->bssid));
  113. bss->parse_error_reported = 1;
  114. return;
  115. }
  116. if (bss->ssid_len != elems->ssid_len ||
  117. os_memcmp(bss->ssid, elems->ssid, bss->ssid_len) != 0) {
  118. wpa_printf(MSG_DEBUG, "Store SSID '%s' for BSSID " MACSTR,
  119. wpa_ssid_txt(elems->ssid, elems->ssid_len),
  120. MAC2STR(bss->bssid));
  121. os_memcpy(bss->ssid, elems->ssid, elems->ssid_len);
  122. bss->ssid_len = elems->ssid_len;
  123. bss_add_pmk(wt, bss);
  124. }
  125. if (elems->osen == NULL) {
  126. if (bss->osenie[0]) {
  127. add_note(wt, MSG_INFO, "BSS " MACSTR
  128. " - OSEN IE removed", MAC2STR(bss->bssid));
  129. bss->rsnie[0] = 0;
  130. update = 1;
  131. }
  132. } else {
  133. if (bss->osenie[0] == 0 ||
  134. os_memcmp(bss->osenie, elems->osen - 2,
  135. elems->osen_len + 2) != 0) {
  136. wpa_printf(MSG_INFO, "BSS " MACSTR " - OSEN IE "
  137. "stored", MAC2STR(bss->bssid));
  138. wpa_hexdump(MSG_DEBUG, "OSEN IE", elems->osen - 2,
  139. elems->osen_len + 2);
  140. update = 1;
  141. }
  142. os_memcpy(bss->osenie, elems->osen - 2,
  143. elems->osen_len + 2);
  144. }
  145. if (elems->rsn_ie == NULL) {
  146. if (bss->rsnie[0]) {
  147. add_note(wt, MSG_INFO, "BSS " MACSTR
  148. " - RSN IE removed", MAC2STR(bss->bssid));
  149. bss->rsnie[0] = 0;
  150. update = 1;
  151. }
  152. } else {
  153. if (bss->rsnie[0] == 0 ||
  154. os_memcmp(bss->rsnie, elems->rsn_ie - 2,
  155. elems->rsn_ie_len + 2) != 0) {
  156. wpa_printf(MSG_INFO, "BSS " MACSTR " - RSN IE "
  157. "stored", MAC2STR(bss->bssid));
  158. wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2,
  159. elems->rsn_ie_len + 2);
  160. update = 1;
  161. }
  162. os_memcpy(bss->rsnie, elems->rsn_ie - 2,
  163. elems->rsn_ie_len + 2);
  164. }
  165. if (elems->wpa_ie == NULL) {
  166. if (bss->wpaie[0]) {
  167. add_note(wt, MSG_INFO, "BSS " MACSTR
  168. " - WPA IE removed", MAC2STR(bss->bssid));
  169. bss->wpaie[0] = 0;
  170. update = 1;
  171. }
  172. } else {
  173. if (bss->wpaie[0] == 0 ||
  174. os_memcmp(bss->wpaie, elems->wpa_ie - 2,
  175. elems->wpa_ie_len + 2) != 0) {
  176. wpa_printf(MSG_INFO, "BSS " MACSTR " - WPA IE "
  177. "stored", MAC2STR(bss->bssid));
  178. wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2,
  179. elems->wpa_ie_len + 2);
  180. update = 1;
  181. }
  182. os_memcpy(bss->wpaie, elems->wpa_ie - 2,
  183. elems->wpa_ie_len + 2);
  184. }
  185. if (elems->mdie)
  186. os_memcpy(bss->mdid, elems->mdie, 2);
  187. if (!update)
  188. return;
  189. bss->beacon_seen = 1;
  190. bss->prev_capab_info = bss->capab_info;
  191. bss->proto = 0;
  192. bss->pairwise_cipher = 0;
  193. bss->group_cipher = 0;
  194. bss->key_mgmt = 0;
  195. bss->rsn_capab = 0;
  196. bss->mgmt_group_cipher = 0;
  197. if (bss->wpaie[0]) {
  198. if (wpa_parse_wpa_ie_wpa(bss->wpaie, 2 + bss->wpaie[1], &data)
  199. < 0) {
  200. add_note(wt, MSG_INFO, "Failed to parse WPA IE from "
  201. MACSTR, MAC2STR(bss->bssid));
  202. } else {
  203. bss->proto |= data.proto;
  204. bss->pairwise_cipher |= data.pairwise_cipher;
  205. bss->group_cipher |= data.group_cipher;
  206. bss->key_mgmt |= data.key_mgmt;
  207. bss->rsn_capab = data.capabilities;
  208. bss->mgmt_group_cipher |= data.mgmt_group_cipher;
  209. }
  210. }
  211. if (bss->rsnie[0]) {
  212. if (wpa_parse_wpa_ie_rsn(bss->rsnie, 2 + bss->rsnie[1], &data)
  213. < 0) {
  214. add_note(wt, MSG_INFO, "Failed to parse RSN IE from "
  215. MACSTR, MAC2STR(bss->bssid));
  216. } else {
  217. bss->proto |= data.proto;
  218. bss->pairwise_cipher |= data.pairwise_cipher;
  219. bss->group_cipher |= data.group_cipher;
  220. bss->key_mgmt |= data.key_mgmt;
  221. bss->rsn_capab = data.capabilities;
  222. bss->mgmt_group_cipher |= data.mgmt_group_cipher;
  223. }
  224. }
  225. if (bss->osenie[0]) {
  226. bss->proto |= WPA_PROTO_OSEN;
  227. bss->pairwise_cipher |= WPA_CIPHER_CCMP;
  228. bss->group_cipher |= WPA_CIPHER_CCMP;
  229. bss->key_mgmt |= WPA_KEY_MGMT_OSEN;
  230. }
  231. if (!(bss->proto & WPA_PROTO_RSN) ||
  232. !(bss->rsn_capab & WPA_CAPABILITY_MFPC))
  233. bss->mgmt_group_cipher = 0;
  234. if (!bss->wpaie[0] && !bss->rsnie[0] && !bss->osenie[0] &&
  235. (bss->capab_info & WLAN_CAPABILITY_PRIVACY))
  236. bss->group_cipher = WPA_CIPHER_WEP40;
  237. wpa_printf(MSG_INFO, "BSS " MACSTR
  238. " proto=%s%s%s%s"
  239. "pairwise=%s%s%s%s%s%s%s"
  240. "group=%s%s%s%s%s%s%s%s%s"
  241. "mgmt_group_cipher=%s%s%s%s%s"
  242. "key_mgmt=%s%s%s%s%s%s%s%s%s"
  243. "rsn_capab=%s%s%s%s%s",
  244. MAC2STR(bss->bssid),
  245. bss->proto == 0 ? "OPEN " : "",
  246. bss->proto & WPA_PROTO_WPA ? "WPA " : "",
  247. bss->proto & WPA_PROTO_RSN ? "WPA2 " : "",
  248. bss->proto & WPA_PROTO_OSEN ? "OSEN " : "",
  249. bss->pairwise_cipher == 0 ? "N/A " : "",
  250. bss->pairwise_cipher & WPA_CIPHER_NONE ? "NONE " : "",
  251. bss->pairwise_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
  252. bss->pairwise_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
  253. bss->pairwise_cipher & WPA_CIPHER_CCMP_256 ? "CCMP-256 " :
  254. "",
  255. bss->pairwise_cipher & WPA_CIPHER_GCMP ? "GCMP " : "",
  256. bss->pairwise_cipher & WPA_CIPHER_GCMP_256 ? "GCMP-256 " :
  257. "",
  258. bss->group_cipher == 0 ? "N/A " : "",
  259. bss->group_cipher & WPA_CIPHER_NONE ? "NONE " : "",
  260. bss->group_cipher & WPA_CIPHER_WEP40 ? "WEP40 " : "",
  261. bss->group_cipher & WPA_CIPHER_WEP104 ? "WEP104 " : "",
  262. bss->group_cipher & WPA_CIPHER_TKIP ? "TKIP " : "",
  263. bss->group_cipher & WPA_CIPHER_CCMP ? "CCMP " : "",
  264. bss->group_cipher & WPA_CIPHER_CCMP_256 ? "CCMP-256 " : "",
  265. bss->group_cipher & WPA_CIPHER_GCMP ? "GCMP " : "",
  266. bss->group_cipher & WPA_CIPHER_GCMP_256 ? "GCMP-256 " : "",
  267. bss->mgmt_group_cipher == 0 ? "N/A " : "",
  268. bss->mgmt_group_cipher & WPA_CIPHER_AES_128_CMAC ?
  269. "BIP " : "",
  270. bss->mgmt_group_cipher & WPA_CIPHER_BIP_GMAC_128 ?
  271. "BIP-GMAC-128 " : "",
  272. bss->mgmt_group_cipher & WPA_CIPHER_BIP_GMAC_256 ?
  273. "BIP-GMAC-256 " : "",
  274. bss->mgmt_group_cipher & WPA_CIPHER_BIP_CMAC_256 ?
  275. "BIP-CMAC-256 " : "",
  276. bss->key_mgmt == 0 ? "N/A " : "",
  277. bss->key_mgmt & WPA_KEY_MGMT_IEEE8021X ? "EAP " : "",
  278. bss->key_mgmt & WPA_KEY_MGMT_PSK ? "PSK " : "",
  279. bss->key_mgmt & WPA_KEY_MGMT_WPA_NONE ? "WPA-NONE " : "",
  280. bss->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X ? "FT-EAP " : "",
  281. bss->key_mgmt & WPA_KEY_MGMT_FT_PSK ? "FT-PSK " : "",
  282. bss->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256 ?
  283. "EAP-SHA256 " : "",
  284. bss->key_mgmt & WPA_KEY_MGMT_PSK_SHA256 ?
  285. "PSK-SHA256 " : "",
  286. bss->key_mgmt & WPA_KEY_MGMT_OSEN ? "OSEN " : "",
  287. bss->rsn_capab & WPA_CAPABILITY_PREAUTH ? "PREAUTH " : "",
  288. bss->rsn_capab & WPA_CAPABILITY_NO_PAIRWISE ?
  289. "NO_PAIRWISE " : "",
  290. bss->rsn_capab & WPA_CAPABILITY_MFPR ? "MFPR " : "",
  291. bss->rsn_capab & WPA_CAPABILITY_MFPC ? "MFPC " : "",
  292. bss->rsn_capab & WPA_CAPABILITY_PEERKEY_ENABLED ?
  293. "PEERKEY " : "");
  294. }
  295. void bss_flush(struct wlantest *wt)
  296. {
  297. struct wlantest_bss *bss, *n;
  298. dl_list_for_each_safe(bss, n, &wt->bss, struct wlantest_bss, list)
  299. bss_deinit(bss);
  300. }