wps_supplicant.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008-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 "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "uuid.h"
  18. #include "crypto/dh_group5.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/ieee802_11_common.h"
  21. #include "common/wpa_common.h"
  22. #include "common/wpa_ctrl.h"
  23. #include "eap_common/eap_wsc_common.h"
  24. #include "eap_peer/eap.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "config.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "notify.h"
  30. #include "blacklist.h"
  31. #include "bss.h"
  32. #include "scan.h"
  33. #include "ap.h"
  34. #include "p2p/p2p.h"
  35. #include "p2p_supplicant.h"
  36. #include "wps_supplicant.h"
  37. #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
  38. #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
  39. #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
  40. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  41. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  42. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  43. {
  44. if (!wpa_s->wps_success &&
  45. wpa_s->current_ssid &&
  46. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  47. const u8 *bssid = wpa_s->bssid;
  48. if (is_zero_ether_addr(bssid))
  49. bssid = wpa_s->pending_bssid;
  50. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  51. " did not succeed - continue trying to find "
  52. "suitable AP", MAC2STR(bssid));
  53. wpa_blacklist_add(wpa_s, bssid);
  54. wpa_supplicant_deauthenticate(wpa_s,
  55. WLAN_REASON_DEAUTH_LEAVING);
  56. wpa_s->reassociate = 1;
  57. wpa_supplicant_req_scan(wpa_s,
  58. wpa_s->blacklist_cleared ? 5 : 0, 0);
  59. wpa_s->blacklist_cleared = 0;
  60. return 1;
  61. }
  62. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  63. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
  64. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
  65. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  66. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  67. int disabled = wpa_s->current_ssid->disabled;
  68. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  69. "try to associate with the received credential");
  70. wpa_supplicant_deauthenticate(wpa_s,
  71. WLAN_REASON_DEAUTH_LEAVING);
  72. if (disabled) {
  73. wpa_printf(MSG_DEBUG, "WPS: Current network is "
  74. "disabled - wait for user to enable");
  75. return 1;
  76. }
  77. wpa_s->after_wps = 5;
  78. wpa_s->wps_freq = wpa_s->assoc_freq;
  79. wpa_s->reassociate = 1;
  80. wpa_supplicant_req_scan(wpa_s, 0, 0);
  81. return 1;
  82. }
  83. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  84. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  85. "for external credential processing");
  86. wpas_clear_wps(wpa_s);
  87. wpa_supplicant_deauthenticate(wpa_s,
  88. WLAN_REASON_DEAUTH_LEAVING);
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
  94. struct wpa_ssid *ssid,
  95. const struct wps_credential *cred)
  96. {
  97. struct wpa_driver_capa capa;
  98. struct wpa_bss *bss;
  99. const u8 *ie;
  100. struct wpa_ie_data adv;
  101. int wpa2 = 0, ccmp = 0;
  102. /*
  103. * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
  104. * case they are configured for mixed mode operation (WPA+WPA2 and
  105. * TKIP+CCMP). Try to use scan results to figure out whether the AP
  106. * actually supports stronger security and select that if the client
  107. * has support for it, too.
  108. */
  109. if (wpa_drv_get_capa(wpa_s, &capa))
  110. return; /* Unknown what driver supports */
  111. bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
  112. if (bss == NULL) {
  113. wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
  114. "table - use credential as-is");
  115. return;
  116. }
  117. wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
  118. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  119. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
  120. wpa2 = 1;
  121. if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
  122. ccmp = 1;
  123. } else {
  124. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  125. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
  126. adv.pairwise_cipher & WPA_CIPHER_CCMP)
  127. ccmp = 1;
  128. }
  129. if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
  130. (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
  131. /*
  132. * TODO: This could be the initial AP configuration and the
  133. * Beacon contents could change shortly. Should request a new
  134. * scan and delay addition of the network until the updated
  135. * scan results are available.
  136. */
  137. wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
  138. "support - use credential as-is");
  139. return;
  140. }
  141. if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  142. (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
  143. (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  144. wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
  145. "based on scan results");
  146. if (wpa_s->conf->ap_scan == 1)
  147. ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
  148. else
  149. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  150. }
  151. if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
  152. (ssid->proto & WPA_PROTO_WPA) &&
  153. (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
  154. wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
  155. "based on scan results");
  156. if (wpa_s->conf->ap_scan == 1)
  157. ssid->proto |= WPA_PROTO_RSN;
  158. else
  159. ssid->proto = WPA_PROTO_RSN;
  160. }
  161. }
  162. static int wpa_supplicant_wps_cred(void *ctx,
  163. const struct wps_credential *cred)
  164. {
  165. struct wpa_supplicant *wpa_s = ctx;
  166. struct wpa_ssid *ssid = wpa_s->current_ssid;
  167. u8 key_idx = 0;
  168. u16 auth_type;
  169. int registrar = 0;
  170. if ((wpa_s->conf->wps_cred_processing == 1 ||
  171. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  172. size_t blen = cred->cred_attr_len * 2 + 1;
  173. char *buf = os_malloc(blen);
  174. if (buf) {
  175. wpa_snprintf_hex(buf, blen,
  176. cred->cred_attr, cred->cred_attr_len);
  177. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  178. WPS_EVENT_CRED_RECEIVED, buf);
  179. os_free(buf);
  180. }
  181. wpas_notify_wps_credential(wpa_s, cred);
  182. } else
  183. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  184. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  185. cred->cred_attr, cred->cred_attr_len);
  186. if (wpa_s->conf->wps_cred_processing == 1)
  187. return 0;
  188. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  189. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  190. cred->auth_type);
  191. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  192. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  193. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  194. cred->key, cred->key_len);
  195. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  196. MAC2STR(cred->mac_addr));
  197. auth_type = cred->auth_type;
  198. if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  199. wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
  200. "auth_type into WPA2PSK");
  201. auth_type = WPS_AUTH_WPA2PSK;
  202. }
  203. if (auth_type != WPS_AUTH_OPEN &&
  204. auth_type != WPS_AUTH_SHARED &&
  205. auth_type != WPS_AUTH_WPAPSK &&
  206. auth_type != WPS_AUTH_WPA2PSK) {
  207. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  208. "unsupported authentication type 0x%x",
  209. auth_type);
  210. return 0;
  211. }
  212. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  213. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  214. "on the received credential");
  215. if (ssid->eap.identity &&
  216. ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
  217. os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
  218. WSC_ID_REGISTRAR_LEN) == 0)
  219. registrar = 1;
  220. os_free(ssid->eap.identity);
  221. ssid->eap.identity = NULL;
  222. ssid->eap.identity_len = 0;
  223. os_free(ssid->eap.phase1);
  224. ssid->eap.phase1 = NULL;
  225. os_free(ssid->eap.eap_methods);
  226. ssid->eap.eap_methods = NULL;
  227. if (!ssid->p2p_group)
  228. ssid->temporary = 0;
  229. } else {
  230. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  231. "received credential");
  232. ssid = wpa_config_add_network(wpa_s->conf);
  233. if (ssid == NULL)
  234. return -1;
  235. wpas_notify_network_added(wpa_s, ssid);
  236. }
  237. wpa_config_set_network_defaults(ssid);
  238. os_free(ssid->ssid);
  239. ssid->ssid = os_malloc(cred->ssid_len);
  240. if (ssid->ssid) {
  241. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  242. ssid->ssid_len = cred->ssid_len;
  243. }
  244. switch (cred->encr_type) {
  245. case WPS_ENCR_NONE:
  246. break;
  247. case WPS_ENCR_WEP:
  248. if (cred->key_len <= 0)
  249. break;
  250. if (cred->key_len != 5 && cred->key_len != 13 &&
  251. cred->key_len != 10 && cred->key_len != 26) {
  252. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
  253. "%lu", (unsigned long) cred->key_len);
  254. return -1;
  255. }
  256. if (cred->key_idx > NUM_WEP_KEYS) {
  257. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
  258. cred->key_idx);
  259. return -1;
  260. }
  261. if (cred->key_idx)
  262. key_idx = cred->key_idx - 1;
  263. if (cred->key_len == 10 || cred->key_len == 26) {
  264. if (hexstr2bin((char *) cred->key,
  265. ssid->wep_key[key_idx],
  266. cred->key_len / 2) < 0) {
  267. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
  268. "%d", key_idx);
  269. return -1;
  270. }
  271. ssid->wep_key_len[key_idx] = cred->key_len / 2;
  272. } else {
  273. os_memcpy(ssid->wep_key[key_idx], cred->key,
  274. cred->key_len);
  275. ssid->wep_key_len[key_idx] = cred->key_len;
  276. }
  277. ssid->wep_tx_keyidx = key_idx;
  278. break;
  279. case WPS_ENCR_TKIP:
  280. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  281. break;
  282. case WPS_ENCR_AES:
  283. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  284. break;
  285. }
  286. switch (auth_type) {
  287. case WPS_AUTH_OPEN:
  288. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  289. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  290. ssid->proto = 0;
  291. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  292. if (registrar) {
  293. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
  294. "id=%d - Credentials for an open "
  295. "network disabled by default - use "
  296. "'select_network %d' to enable",
  297. ssid->id, ssid->id);
  298. ssid->disabled = 1;
  299. }
  300. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  301. break;
  302. case WPS_AUTH_SHARED:
  303. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  304. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  305. ssid->proto = 0;
  306. break;
  307. case WPS_AUTH_WPAPSK:
  308. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  309. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  310. ssid->proto = WPA_PROTO_WPA;
  311. break;
  312. case WPS_AUTH_WPA:
  313. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  314. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  315. ssid->proto = WPA_PROTO_WPA;
  316. break;
  317. case WPS_AUTH_WPA2:
  318. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  319. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  320. ssid->proto = WPA_PROTO_RSN;
  321. break;
  322. case WPS_AUTH_WPA2PSK:
  323. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  324. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  325. ssid->proto = WPA_PROTO_RSN;
  326. break;
  327. }
  328. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  329. if (cred->key_len == 2 * PMK_LEN) {
  330. if (hexstr2bin((const char *) cred->key, ssid->psk,
  331. PMK_LEN)) {
  332. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  333. "Key");
  334. return -1;
  335. }
  336. ssid->psk_set = 1;
  337. ssid->export_keys = 1;
  338. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  339. os_free(ssid->passphrase);
  340. ssid->passphrase = os_malloc(cred->key_len + 1);
  341. if (ssid->passphrase == NULL)
  342. return -1;
  343. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  344. ssid->passphrase[cred->key_len] = '\0';
  345. wpa_config_update_psk(ssid);
  346. ssid->export_keys = 1;
  347. } else {
  348. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  349. "length %lu",
  350. (unsigned long) cred->key_len);
  351. return -1;
  352. }
  353. }
  354. wpas_wps_security_workaround(wpa_s, ssid, cred);
  355. #ifndef CONFIG_NO_CONFIG_WRITE
  356. if (wpa_s->conf->update_config &&
  357. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  358. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  359. return -1;
  360. }
  361. #endif /* CONFIG_NO_CONFIG_WRITE */
  362. return 0;
  363. }
  364. #ifdef CONFIG_P2P
  365. static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  366. {
  367. struct wpa_supplicant *wpa_s = eloop_ctx;
  368. wpas_p2p_notif_pbc_overlap(wpa_s);
  369. }
  370. #endif /* CONFIG_P2P */
  371. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  372. struct wps_event_m2d *m2d)
  373. {
  374. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  375. "dev_password_id=%d config_error=%d",
  376. m2d->dev_password_id, m2d->config_error);
  377. wpas_notify_wps_event_m2d(wpa_s, m2d);
  378. #ifdef CONFIG_P2P
  379. if (wpa_s->parent && wpa_s->parent != wpa_s) {
  380. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
  381. "dev_password_id=%d config_error=%d",
  382. m2d->dev_password_id, m2d->config_error);
  383. }
  384. if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
  385. /*
  386. * Notify P2P from eloop timeout to avoid issues with the
  387. * interface getting removed while processing a message.
  388. */
  389. eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
  390. NULL);
  391. }
  392. #endif /* CONFIG_P2P */
  393. }
  394. static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
  395. "No Error", /* WPS_EI_NO_ERROR */
  396. "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
  397. "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
  398. };
  399. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  400. struct wps_event_fail *fail)
  401. {
  402. if (fail->error_indication > 0 &&
  403. fail->error_indication < NUM_WPS_EI_VALUES) {
  404. wpa_msg(wpa_s, MSG_INFO,
  405. WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
  406. fail->msg, fail->config_error, fail->error_indication,
  407. wps_event_fail_reason[fail->error_indication]);
  408. if (wpa_s->parent && wpa_s->parent != wpa_s)
  409. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  410. "msg=%d config_error=%d reason=%d (%s)",
  411. fail->msg, fail->config_error,
  412. fail->error_indication,
  413. wps_event_fail_reason[fail->error_indication]);
  414. } else {
  415. wpa_msg(wpa_s, MSG_INFO,
  416. WPS_EVENT_FAIL "msg=%d config_error=%d",
  417. fail->msg, fail->config_error);
  418. if (wpa_s->parent && wpa_s->parent != wpa_s)
  419. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  420. "msg=%d config_error=%d",
  421. fail->msg, fail->config_error);
  422. }
  423. wpas_clear_wps(wpa_s);
  424. wpas_notify_wps_event_fail(wpa_s, fail);
  425. #ifdef CONFIG_P2P
  426. wpas_p2p_wps_failed(wpa_s, fail);
  427. #endif /* CONFIG_P2P */
  428. }
  429. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  430. {
  431. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  432. wpa_s->wps_success = 1;
  433. wpas_notify_wps_event_success(wpa_s);
  434. #ifdef CONFIG_P2P
  435. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  436. #endif /* CONFIG_P2P */
  437. }
  438. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  439. struct wps_event_er_ap *ap)
  440. {
  441. char uuid_str[100];
  442. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  443. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  444. if (ap->pri_dev_type)
  445. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  446. sizeof(dev_type));
  447. else
  448. dev_type[0] = '\0';
  449. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  450. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  451. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  452. ap->friendly_name ? ap->friendly_name : "",
  453. ap->manufacturer ? ap->manufacturer : "",
  454. ap->model_description ? ap->model_description : "",
  455. ap->model_name ? ap->model_name : "",
  456. ap->manufacturer_url ? ap->manufacturer_url : "",
  457. ap->model_url ? ap->model_url : "");
  458. }
  459. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  460. struct wps_event_er_ap *ap)
  461. {
  462. char uuid_str[100];
  463. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  464. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  465. }
  466. static void wpa_supplicant_wps_event_er_enrollee_add(
  467. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  468. {
  469. char uuid_str[100];
  470. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  471. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  472. if (enrollee->pri_dev_type)
  473. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  474. sizeof(dev_type));
  475. else
  476. dev_type[0] = '\0';
  477. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  478. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  479. "|%s|%s|%s|%s|%s|",
  480. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  481. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  482. enrollee->dev_name ? enrollee->dev_name : "",
  483. enrollee->manufacturer ? enrollee->manufacturer : "",
  484. enrollee->model_name ? enrollee->model_name : "",
  485. enrollee->model_number ? enrollee->model_number : "",
  486. enrollee->serial_number ? enrollee->serial_number : "");
  487. }
  488. static void wpa_supplicant_wps_event_er_enrollee_remove(
  489. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  490. {
  491. char uuid_str[100];
  492. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  493. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  494. uuid_str, MAC2STR(enrollee->mac_addr));
  495. }
  496. static void wpa_supplicant_wps_event_er_ap_settings(
  497. struct wpa_supplicant *wpa_s,
  498. struct wps_event_er_ap_settings *ap_settings)
  499. {
  500. char uuid_str[100];
  501. char key_str[65];
  502. const struct wps_credential *cred = ap_settings->cred;
  503. key_str[0] = '\0';
  504. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  505. if (cred->key_len >= 8 && cred->key_len <= 64) {
  506. os_memcpy(key_str, cred->key, cred->key_len);
  507. key_str[cred->key_len] = '\0';
  508. }
  509. }
  510. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  511. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  512. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  513. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  514. "key=%s",
  515. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  516. cred->auth_type, cred->encr_type, key_str);
  517. }
  518. static void wpa_supplicant_wps_event_er_set_sel_reg(
  519. struct wpa_supplicant *wpa_s,
  520. struct wps_event_er_set_selected_registrar *ev)
  521. {
  522. char uuid_str[100];
  523. uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
  524. switch (ev->state) {
  525. case WPS_ER_SET_SEL_REG_START:
  526. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  527. "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
  528. "sel_reg_config_methods=0x%x",
  529. uuid_str, ev->sel_reg, ev->dev_passwd_id,
  530. ev->sel_reg_config_methods);
  531. break;
  532. case WPS_ER_SET_SEL_REG_DONE:
  533. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  534. "uuid=%s state=DONE", uuid_str);
  535. break;
  536. case WPS_ER_SET_SEL_REG_FAILED:
  537. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
  538. "uuid=%s state=FAILED", uuid_str);
  539. break;
  540. }
  541. }
  542. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  543. union wps_event_data *data)
  544. {
  545. struct wpa_supplicant *wpa_s = ctx;
  546. switch (event) {
  547. case WPS_EV_M2D:
  548. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  549. break;
  550. case WPS_EV_FAIL:
  551. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  552. break;
  553. case WPS_EV_SUCCESS:
  554. wpa_supplicant_wps_event_success(wpa_s);
  555. break;
  556. case WPS_EV_PWD_AUTH_FAIL:
  557. #ifdef CONFIG_AP
  558. if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
  559. wpa_supplicant_ap_pwd_auth_fail(wpa_s);
  560. #endif /* CONFIG_AP */
  561. break;
  562. case WPS_EV_PBC_OVERLAP:
  563. break;
  564. case WPS_EV_PBC_TIMEOUT:
  565. break;
  566. case WPS_EV_ER_AP_ADD:
  567. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  568. break;
  569. case WPS_EV_ER_AP_REMOVE:
  570. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  571. break;
  572. case WPS_EV_ER_ENROLLEE_ADD:
  573. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  574. &data->enrollee);
  575. break;
  576. case WPS_EV_ER_ENROLLEE_REMOVE:
  577. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  578. &data->enrollee);
  579. break;
  580. case WPS_EV_ER_AP_SETTINGS:
  581. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  582. &data->ap_settings);
  583. break;
  584. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  585. wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
  586. &data->set_sel_reg);
  587. break;
  588. }
  589. }
  590. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  591. {
  592. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  593. eap_is_wps_pin_enrollee(&ssid->eap))
  594. return WPS_REQ_ENROLLEE;
  595. else
  596. return WPS_REQ_REGISTRAR;
  597. }
  598. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  599. {
  600. int id;
  601. struct wpa_ssid *ssid, *remove_ssid = NULL;
  602. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  603. /* Remove any existing WPS network from configuration */
  604. ssid = wpa_s->conf->ssid;
  605. while (ssid) {
  606. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  607. if (ssid == wpa_s->current_ssid) {
  608. wpa_s->current_ssid = NULL;
  609. if (ssid != NULL)
  610. wpas_notify_network_changed(wpa_s);
  611. }
  612. id = ssid->id;
  613. remove_ssid = ssid;
  614. } else
  615. id = -1;
  616. ssid = ssid->next;
  617. if (id >= 0) {
  618. wpas_notify_network_removed(wpa_s, remove_ssid);
  619. wpa_config_remove_network(wpa_s->conf, id);
  620. }
  621. }
  622. }
  623. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  624. {
  625. struct wpa_supplicant *wpa_s = eloop_ctx;
  626. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  627. "out");
  628. wpas_clear_wps(wpa_s);
  629. }
  630. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  631. int registrar, const u8 *bssid)
  632. {
  633. struct wpa_ssid *ssid;
  634. ssid = wpa_config_add_network(wpa_s->conf);
  635. if (ssid == NULL)
  636. return NULL;
  637. wpas_notify_network_added(wpa_s, ssid);
  638. wpa_config_set_network_defaults(ssid);
  639. ssid->temporary = 1;
  640. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  641. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  642. wpa_config_set(ssid, "identity", registrar ?
  643. "\"" WSC_ID_REGISTRAR "\"" :
  644. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  645. wpas_notify_network_removed(wpa_s, ssid);
  646. wpa_config_remove_network(wpa_s->conf, ssid->id);
  647. return NULL;
  648. }
  649. if (bssid) {
  650. #ifndef CONFIG_P2P
  651. struct wpa_bss *bss;
  652. int count = 0;
  653. #endif /* CONFIG_P2P */
  654. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  655. ssid->bssid_set = 1;
  656. /*
  657. * Note: With P2P, the SSID may change at the time the WPS
  658. * provisioning is started, so better not filter the AP based
  659. * on the current SSID in the scan results.
  660. */
  661. #ifndef CONFIG_P2P
  662. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  663. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  664. continue;
  665. os_free(ssid->ssid);
  666. ssid->ssid = os_malloc(bss->ssid_len);
  667. if (ssid->ssid == NULL)
  668. break;
  669. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  670. ssid->ssid_len = bss->ssid_len;
  671. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  672. "scan results",
  673. ssid->ssid, ssid->ssid_len);
  674. count++;
  675. }
  676. if (count > 1) {
  677. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  678. "for the AP; use wildcard");
  679. os_free(ssid->ssid);
  680. ssid->ssid = NULL;
  681. ssid->ssid_len = 0;
  682. }
  683. #endif /* CONFIG_P2P */
  684. }
  685. return ssid;
  686. }
  687. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  688. struct wpa_ssid *selected)
  689. {
  690. struct wpa_ssid *ssid;
  691. /* Mark all other networks disabled and trigger reassociation */
  692. ssid = wpa_s->conf->ssid;
  693. while (ssid) {
  694. int was_disabled = ssid->disabled;
  695. /*
  696. * In case the network object corresponds to a persistent group
  697. * then do not send out network disabled signal. In addition,
  698. * do not change disabled status of persistent network objects
  699. * from 2 to 1 should we connect to another network.
  700. */
  701. if (was_disabled != 2) {
  702. ssid->disabled = ssid != selected;
  703. if (was_disabled != ssid->disabled)
  704. wpas_notify_network_enabled_changed(wpa_s,
  705. ssid);
  706. }
  707. ssid = ssid->next;
  708. }
  709. wpa_s->disconnected = 0;
  710. wpa_s->reassociate = 1;
  711. wpa_s->scan_runs = 0;
  712. wpa_s->wps_success = 0;
  713. wpa_s->blacklist_cleared = 0;
  714. wpa_supplicant_req_scan(wpa_s, 0, 0);
  715. }
  716. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  717. int p2p_group)
  718. {
  719. struct wpa_ssid *ssid;
  720. wpas_clear_wps(wpa_s);
  721. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  722. if (ssid == NULL)
  723. return -1;
  724. ssid->temporary = 1;
  725. ssid->p2p_group = p2p_group;
  726. #ifdef CONFIG_P2P
  727. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  728. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  729. if (ssid->ssid) {
  730. ssid->ssid_len = wpa_s->go_params->ssid_len;
  731. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  732. ssid->ssid_len);
  733. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  734. "SSID", ssid->ssid, ssid->ssid_len);
  735. }
  736. }
  737. #endif /* CONFIG_P2P */
  738. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  739. if (wpa_s->wps_fragment_size)
  740. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  741. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  742. wpa_s, NULL);
  743. wpas_wps_reassoc(wpa_s, ssid);
  744. return 0;
  745. }
  746. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  747. const char *pin, int p2p_group, u16 dev_pw_id)
  748. {
  749. struct wpa_ssid *ssid;
  750. char val[128];
  751. unsigned int rpin = 0;
  752. wpas_clear_wps(wpa_s);
  753. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  754. if (ssid == NULL)
  755. return -1;
  756. ssid->temporary = 1;
  757. ssid->p2p_group = p2p_group;
  758. #ifdef CONFIG_P2P
  759. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  760. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  761. if (ssid->ssid) {
  762. ssid->ssid_len = wpa_s->go_params->ssid_len;
  763. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  764. ssid->ssid_len);
  765. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  766. "SSID", ssid->ssid, ssid->ssid_len);
  767. }
  768. }
  769. #endif /* CONFIG_P2P */
  770. if (pin)
  771. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
  772. pin, dev_pw_id);
  773. else {
  774. rpin = wps_generate_pin();
  775. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
  776. rpin, dev_pw_id);
  777. }
  778. wpa_config_set(ssid, "phase1", val, 0);
  779. if (wpa_s->wps_fragment_size)
  780. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  781. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  782. wpa_s, NULL);
  783. wpas_wps_reassoc(wpa_s, ssid);
  784. return rpin;
  785. }
  786. /* Cancel the wps pbc/pin requests */
  787. int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
  788. {
  789. #ifdef CONFIG_AP
  790. if (wpa_s->ap_iface) {
  791. wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
  792. return wpa_supplicant_ap_wps_cancel(wpa_s);
  793. }
  794. #endif /* CONFIG_AP */
  795. if (wpa_s->wpa_state == WPA_SCANNING) {
  796. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
  797. wpa_supplicant_cancel_scan(wpa_s);
  798. wpas_clear_wps(wpa_s);
  799. } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  800. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
  801. "deauthenticate");
  802. wpa_supplicant_deauthenticate(wpa_s,
  803. WLAN_REASON_DEAUTH_LEAVING);
  804. wpas_clear_wps(wpa_s);
  805. }
  806. return 0;
  807. }
  808. #ifdef CONFIG_WPS_OOB
  809. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  810. char *path, char *method, char *name)
  811. {
  812. struct wps_context *wps = wpa_s->wps;
  813. struct oob_device_data *oob_dev;
  814. oob_dev = wps_get_oob_device(device_type);
  815. if (oob_dev == NULL)
  816. return -1;
  817. oob_dev->device_path = path;
  818. oob_dev->device_name = name;
  819. wps->oob_conf.oob_method = wps_get_oob_method(method);
  820. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  821. /*
  822. * Use pre-configured DH keys in order to be able to write the
  823. * key hash into the OOB file.
  824. */
  825. wpabuf_free(wps->dh_pubkey);
  826. wpabuf_free(wps->dh_privkey);
  827. wps->dh_privkey = NULL;
  828. wps->dh_pubkey = NULL;
  829. dh5_free(wps->dh_ctx);
  830. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  831. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  832. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  833. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  834. "Diffie-Hellman handshake");
  835. return -1;
  836. }
  837. }
  838. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  839. wpas_clear_wps(wpa_s);
  840. if (wps_process_oob(wps, oob_dev, 0) < 0)
  841. return -1;
  842. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  843. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  844. wpas_wps_start_pin(wpa_s, NULL,
  845. wpabuf_head(wps->oob_conf.dev_password), 0,
  846. DEV_PW_DEFAULT) < 0)
  847. return -1;
  848. return 0;
  849. }
  850. #endif /* CONFIG_WPS_OOB */
  851. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  852. const char *pin, struct wps_new_ap_settings *settings)
  853. {
  854. struct wpa_ssid *ssid;
  855. char val[200];
  856. char *pos, *end;
  857. int res;
  858. if (!pin)
  859. return -1;
  860. wpas_clear_wps(wpa_s);
  861. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  862. if (ssid == NULL)
  863. return -1;
  864. ssid->temporary = 1;
  865. pos = val;
  866. end = pos + sizeof(val);
  867. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  868. if (res < 0 || res >= end - pos)
  869. return -1;
  870. pos += res;
  871. if (settings) {
  872. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  873. "new_encr=%s new_key=%s",
  874. settings->ssid_hex, settings->auth,
  875. settings->encr, settings->key_hex);
  876. if (res < 0 || res >= end - pos)
  877. return -1;
  878. pos += res;
  879. }
  880. res = os_snprintf(pos, end - pos, "\"");
  881. if (res < 0 || res >= end - pos)
  882. return -1;
  883. wpa_config_set(ssid, "phase1", val, 0);
  884. if (wpa_s->wps_fragment_size)
  885. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  886. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  887. wpa_s, NULL);
  888. wpas_wps_reassoc(wpa_s, ssid);
  889. return 0;
  890. }
  891. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  892. size_t psk_len)
  893. {
  894. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  895. "STA " MACSTR, MAC2STR(mac_addr));
  896. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  897. /* TODO */
  898. return 0;
  899. }
  900. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  901. const struct wps_device_data *dev)
  902. {
  903. char uuid[40], txt[400];
  904. int len;
  905. char devtype[WPS_DEV_TYPE_BUFSIZE];
  906. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  907. return;
  908. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  909. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  910. " [%s|%s|%s|%s|%s|%s]",
  911. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  912. dev->manufacturer, dev->model_name,
  913. dev->model_number, dev->serial_number,
  914. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  915. sizeof(devtype)));
  916. if (len > 0 && len < (int) sizeof(txt))
  917. wpa_printf(MSG_INFO, "%s", txt);
  918. }
  919. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  920. u16 sel_reg_config_methods)
  921. {
  922. #ifdef CONFIG_WPS_ER
  923. struct wpa_supplicant *wpa_s = ctx;
  924. if (wpa_s->wps_er == NULL)
  925. return;
  926. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  927. "dev_password_id=%u sel_reg_config_methods=0x%x",
  928. sel_reg, dev_passwd_id, sel_reg_config_methods);
  929. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  930. sel_reg_config_methods);
  931. #endif /* CONFIG_WPS_ER */
  932. }
  933. static u16 wps_fix_config_methods(u16 config_methods)
  934. {
  935. #ifdef CONFIG_WPS2
  936. if ((config_methods &
  937. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  938. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  939. wpa_printf(MSG_INFO, "WPS: Converting display to "
  940. "virtual_display for WPS 2.0 compliance");
  941. config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  942. }
  943. if ((config_methods &
  944. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  945. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  946. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  947. "virtual_push_button for WPS 2.0 compliance");
  948. config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  949. }
  950. #endif /* CONFIG_WPS2 */
  951. return config_methods;
  952. }
  953. static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
  954. struct wps_context *wps)
  955. {
  956. wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
  957. if (is_nil_uuid(wpa_s->conf->uuid)) {
  958. struct wpa_supplicant *first;
  959. first = wpa_s->global->ifaces;
  960. while (first && first->next)
  961. first = first->next;
  962. if (first && first != wpa_s) {
  963. os_memcpy(wps->uuid, wpa_s->global->ifaces->wps->uuid,
  964. WPS_UUID_LEN);
  965. wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
  966. "interface", wps->uuid, WPS_UUID_LEN);
  967. } else {
  968. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  969. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  970. "address", wps->uuid, WPS_UUID_LEN);
  971. }
  972. } else {
  973. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  974. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
  975. wps->uuid, WPS_UUID_LEN);
  976. }
  977. }
  978. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  979. {
  980. struct wps_context *wps;
  981. struct wps_registrar_config rcfg;
  982. wps = os_zalloc(sizeof(*wps));
  983. if (wps == NULL)
  984. return -1;
  985. wps->cred_cb = wpa_supplicant_wps_cred;
  986. wps->event_cb = wpa_supplicant_wps_event;
  987. wps->cb_ctx = wpa_s;
  988. wps->dev.device_name = wpa_s->conf->device_name;
  989. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  990. wps->dev.model_name = wpa_s->conf->model_name;
  991. wps->dev.model_number = wpa_s->conf->model_number;
  992. wps->dev.serial_number = wpa_s->conf->serial_number;
  993. wps->config_methods =
  994. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  995. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  996. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  997. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  998. "methods are not allowed at the same time");
  999. os_free(wps);
  1000. return -1;
  1001. }
  1002. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1003. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1004. WPS_DEV_TYPE_LEN);
  1005. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1006. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1007. WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
  1008. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1009. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  1010. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  1011. wpas_wps_set_uuid(wpa_s, wps);
  1012. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  1013. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  1014. os_memset(&rcfg, 0, sizeof(rcfg));
  1015. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  1016. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  1017. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  1018. rcfg.cb_ctx = wpa_s;
  1019. wps->registrar = wps_registrar_init(wps, &rcfg);
  1020. if (wps->registrar == NULL) {
  1021. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  1022. os_free(wps);
  1023. return -1;
  1024. }
  1025. wpa_s->wps = wps;
  1026. return 0;
  1027. }
  1028. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  1029. {
  1030. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  1031. if (wpa_s->wps == NULL)
  1032. return;
  1033. #ifdef CONFIG_WPS_ER
  1034. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1035. wpa_s->wps_er = NULL;
  1036. #endif /* CONFIG_WPS_ER */
  1037. wps_registrar_deinit(wpa_s->wps->registrar);
  1038. wpabuf_free(wpa_s->wps->dh_pubkey);
  1039. wpabuf_free(wpa_s->wps->dh_privkey);
  1040. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  1041. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  1042. os_free(wpa_s->wps->network_key);
  1043. os_free(wpa_s->wps);
  1044. wpa_s->wps = NULL;
  1045. }
  1046. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  1047. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  1048. {
  1049. struct wpabuf *wps_ie;
  1050. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  1051. return -1;
  1052. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1053. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1054. if (!wps_ie) {
  1055. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1056. return 0;
  1057. }
  1058. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  1059. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1060. "without active PBC Registrar");
  1061. wpabuf_free(wps_ie);
  1062. return 0;
  1063. }
  1064. /* TODO: overlap detection */
  1065. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1066. "(Active PBC)");
  1067. wpabuf_free(wps_ie);
  1068. return 1;
  1069. }
  1070. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1071. if (!wps_ie) {
  1072. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1073. return 0;
  1074. }
  1075. /*
  1076. * Start with WPS APs that advertise our address as an
  1077. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  1078. * allow any WPS AP after couple of scans since some APs do not
  1079. * set Selected Registrar attribute properly when using
  1080. * external Registrar.
  1081. */
  1082. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  1083. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  1084. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1085. "without active PIN Registrar");
  1086. wpabuf_free(wps_ie);
  1087. return 0;
  1088. }
  1089. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1090. } else {
  1091. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1092. "(Authorized MAC or Active PIN)");
  1093. }
  1094. wpabuf_free(wps_ie);
  1095. return 1;
  1096. }
  1097. if (wps_ie) {
  1098. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1099. wpabuf_free(wps_ie);
  1100. return 1;
  1101. }
  1102. return -1;
  1103. }
  1104. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  1105. struct wpa_ssid *ssid,
  1106. struct wpa_scan_res *bss)
  1107. {
  1108. struct wpabuf *wps_ie = NULL;
  1109. int ret = 0;
  1110. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1111. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1112. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  1113. /* allow wildcard SSID for WPS PBC */
  1114. ret = 1;
  1115. }
  1116. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1117. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1118. if (wps_ie &&
  1119. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  1120. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  1121. /* allow wildcard SSID for WPS PIN */
  1122. ret = 1;
  1123. }
  1124. }
  1125. if (!ret && ssid->bssid_set &&
  1126. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  1127. /* allow wildcard SSID due to hardcoded BSSID match */
  1128. ret = 1;
  1129. }
  1130. #ifdef CONFIG_WPS_STRICT
  1131. if (wps_ie) {
  1132. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  1133. 0, bss->bssid) < 0)
  1134. ret = 0;
  1135. if (bss->beacon_ie_len) {
  1136. struct wpabuf *bcn_wps;
  1137. bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
  1138. bss, WPS_IE_VENDOR_TYPE);
  1139. if (bcn_wps == NULL) {
  1140. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  1141. "missing from AP Beacon");
  1142. ret = 0;
  1143. } else {
  1144. if (wps_validate_beacon(wps_ie) < 0)
  1145. ret = 0;
  1146. wpabuf_free(bcn_wps);
  1147. }
  1148. }
  1149. }
  1150. #endif /* CONFIG_WPS_STRICT */
  1151. wpabuf_free(wps_ie);
  1152. return ret;
  1153. }
  1154. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  1155. struct wpa_bss *selected, struct wpa_ssid *ssid)
  1156. {
  1157. const u8 *sel_uuid, *uuid;
  1158. struct wpabuf *wps_ie;
  1159. int ret = 0;
  1160. struct wpa_bss *bss;
  1161. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  1162. return 0;
  1163. wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
  1164. "present in scan results; selected BSSID " MACSTR,
  1165. MAC2STR(selected->bssid));
  1166. /* Make sure that only one AP is in active PBC mode */
  1167. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  1168. if (wps_ie) {
  1169. sel_uuid = wps_get_uuid_e(wps_ie);
  1170. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
  1171. sel_uuid, UUID_LEN);
  1172. } else {
  1173. wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
  1174. "WPS IE?!");
  1175. sel_uuid = NULL;
  1176. }
  1177. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1178. struct wpabuf *ie;
  1179. if (bss == selected)
  1180. continue;
  1181. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1182. if (!ie)
  1183. continue;
  1184. if (!wps_is_selected_pbc_registrar(ie)) {
  1185. wpabuf_free(ie);
  1186. continue;
  1187. }
  1188. wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
  1189. MACSTR, MAC2STR(bss->bssid));
  1190. uuid = wps_get_uuid_e(ie);
  1191. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
  1192. uuid, UUID_LEN);
  1193. if (sel_uuid == NULL || uuid == NULL ||
  1194. os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
  1195. ret = 1; /* PBC overlap */
  1196. wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
  1197. MACSTR " and " MACSTR,
  1198. MAC2STR(selected->bssid),
  1199. MAC2STR(bss->bssid));
  1200. wpabuf_free(ie);
  1201. break;
  1202. }
  1203. /* TODO: verify that this is reasonable dual-band situation */
  1204. wpabuf_free(ie);
  1205. }
  1206. wpabuf_free(wps_ie);
  1207. return ret;
  1208. }
  1209. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1210. {
  1211. struct wpa_bss *bss;
  1212. unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
  1213. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1214. return;
  1215. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1216. struct wpabuf *ie;
  1217. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1218. if (!ie)
  1219. continue;
  1220. if (wps_is_selected_pbc_registrar(ie))
  1221. pbc++;
  1222. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1223. auth++;
  1224. else if (wps_is_selected_pin_registrar(ie))
  1225. pin++;
  1226. else
  1227. wps++;
  1228. wpabuf_free(ie);
  1229. }
  1230. if (pbc)
  1231. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  1232. else if (auth)
  1233. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
  1234. else if (pin)
  1235. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  1236. else if (wps)
  1237. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  1238. }
  1239. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1240. {
  1241. struct wpa_ssid *ssid;
  1242. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1243. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1244. return 1;
  1245. }
  1246. return 0;
  1247. }
  1248. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1249. char *end)
  1250. {
  1251. struct wpabuf *wps_ie;
  1252. int ret;
  1253. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1254. if (wps_ie == NULL)
  1255. return 0;
  1256. ret = wps_attr_text(wps_ie, buf, end);
  1257. wpabuf_free(wps_ie);
  1258. return ret;
  1259. }
  1260. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1261. {
  1262. #ifdef CONFIG_WPS_ER
  1263. if (wpa_s->wps_er) {
  1264. wps_er_refresh(wpa_s->wps_er);
  1265. return 0;
  1266. }
  1267. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1268. if (wpa_s->wps_er == NULL)
  1269. return -1;
  1270. return 0;
  1271. #else /* CONFIG_WPS_ER */
  1272. return 0;
  1273. #endif /* CONFIG_WPS_ER */
  1274. }
  1275. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1276. {
  1277. #ifdef CONFIG_WPS_ER
  1278. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1279. wpa_s->wps_er = NULL;
  1280. #endif /* CONFIG_WPS_ER */
  1281. return 0;
  1282. }
  1283. #ifdef CONFIG_WPS_ER
  1284. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1285. const char *uuid, const char *pin)
  1286. {
  1287. u8 u[UUID_LEN];
  1288. int any = 0;
  1289. if (os_strcmp(uuid, "any") == 0)
  1290. any = 1;
  1291. else if (uuid_str2bin(uuid, u))
  1292. return -1;
  1293. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1294. any ? NULL : u,
  1295. (const u8 *) pin, os_strlen(pin), 300);
  1296. }
  1297. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1298. {
  1299. u8 u[UUID_LEN];
  1300. if (uuid_str2bin(uuid, u))
  1301. return -1;
  1302. return wps_er_pbc(wpa_s->wps_er, u);
  1303. }
  1304. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1305. const char *pin)
  1306. {
  1307. u8 u[UUID_LEN];
  1308. if (uuid_str2bin(uuid, u))
  1309. return -1;
  1310. return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
  1311. os_strlen(pin));
  1312. }
  1313. int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1314. int id)
  1315. {
  1316. u8 u[UUID_LEN];
  1317. struct wpa_ssid *ssid;
  1318. struct wps_credential cred;
  1319. if (uuid_str2bin(uuid, u))
  1320. return -1;
  1321. ssid = wpa_config_get_network(wpa_s->conf, id);
  1322. if (ssid == NULL || ssid->ssid == NULL)
  1323. return -1;
  1324. os_memset(&cred, 0, sizeof(cred));
  1325. if (ssid->ssid_len > 32)
  1326. return -1;
  1327. os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
  1328. cred.ssid_len = ssid->ssid_len;
  1329. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  1330. cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
  1331. WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
  1332. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  1333. cred.encr_type = WPS_ENCR_AES;
  1334. else
  1335. cred.encr_type = WPS_ENCR_TKIP;
  1336. if (ssid->passphrase) {
  1337. cred.key_len = os_strlen(ssid->passphrase);
  1338. if (cred.key_len >= 64)
  1339. return -1;
  1340. os_memcpy(cred.key, ssid->passphrase, cred.key_len);
  1341. } else if (ssid->psk_set) {
  1342. cred.key_len = 32;
  1343. os_memcpy(cred.key, ssid->psk, 32);
  1344. } else
  1345. return -1;
  1346. } else {
  1347. cred.auth_type = WPS_AUTH_OPEN;
  1348. cred.encr_type = WPS_ENCR_NONE;
  1349. }
  1350. return wps_er_set_config(wpa_s->wps_er, u, &cred);
  1351. }
  1352. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1353. const char *pin, struct wps_new_ap_settings *settings)
  1354. {
  1355. u8 u[UUID_LEN];
  1356. struct wps_credential cred;
  1357. size_t len;
  1358. if (uuid_str2bin(uuid, u))
  1359. return -1;
  1360. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1361. settings->encr == NULL || settings->key_hex == NULL)
  1362. return -1;
  1363. os_memset(&cred, 0, sizeof(cred));
  1364. len = os_strlen(settings->ssid_hex);
  1365. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1366. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1367. return -1;
  1368. cred.ssid_len = len / 2;
  1369. len = os_strlen(settings->key_hex);
  1370. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1371. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1372. return -1;
  1373. cred.key_len = len / 2;
  1374. if (os_strcmp(settings->auth, "OPEN") == 0)
  1375. cred.auth_type = WPS_AUTH_OPEN;
  1376. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1377. cred.auth_type = WPS_AUTH_WPAPSK;
  1378. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1379. cred.auth_type = WPS_AUTH_WPA2PSK;
  1380. else
  1381. return -1;
  1382. if (os_strcmp(settings->encr, "NONE") == 0)
  1383. cred.encr_type = WPS_ENCR_NONE;
  1384. else if (os_strcmp(settings->encr, "WEP") == 0)
  1385. cred.encr_type = WPS_ENCR_WEP;
  1386. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1387. cred.encr_type = WPS_ENCR_TKIP;
  1388. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1389. cred.encr_type = WPS_ENCR_AES;
  1390. else
  1391. return -1;
  1392. return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
  1393. os_strlen(pin), &cred);
  1394. }
  1395. static int callbacks_pending = 0;
  1396. static void wpas_wps_terminate_cb(void *ctx)
  1397. {
  1398. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1399. if (--callbacks_pending <= 0)
  1400. eloop_terminate();
  1401. }
  1402. #endif /* CONFIG_WPS_ER */
  1403. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1404. {
  1405. #ifdef CONFIG_WPS_ER
  1406. if (wpa_s->wps_er) {
  1407. callbacks_pending++;
  1408. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1409. wpa_s->wps_er = NULL;
  1410. return 1;
  1411. }
  1412. #endif /* CONFIG_WPS_ER */
  1413. return 0;
  1414. }
  1415. int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
  1416. {
  1417. struct wpa_ssid *ssid;
  1418. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1419. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  1420. return 1;
  1421. }
  1422. return 0;
  1423. }
  1424. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1425. {
  1426. struct wps_context *wps = wpa_s->wps;
  1427. if (wps == NULL)
  1428. return;
  1429. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1430. wps->config_methods = wps_config_methods_str2bin(
  1431. wpa_s->conf->config_methods);
  1432. if ((wps->config_methods &
  1433. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1434. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1435. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1436. "config methods are not allowed at the "
  1437. "same time");
  1438. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1439. }
  1440. }
  1441. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1442. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  1443. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1444. WPS_DEV_TYPE_LEN);
  1445. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  1446. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1447. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1448. wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1449. }
  1450. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1451. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1452. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
  1453. wpas_wps_set_uuid(wpa_s, wps);
  1454. if (wpa_s->conf->changed_parameters &
  1455. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1456. /* Update pointers to make sure they refer current values */
  1457. wps->dev.device_name = wpa_s->conf->device_name;
  1458. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1459. wps->dev.model_name = wpa_s->conf->model_name;
  1460. wps->dev.model_number = wpa_s->conf->model_number;
  1461. wps->dev.serial_number = wpa_s->conf->serial_number;
  1462. }
  1463. }