wps_supplicant.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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. }
  426. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  427. {
  428. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  429. wpa_s->wps_success = 1;
  430. wpas_notify_wps_event_success(wpa_s);
  431. #ifdef CONFIG_P2P
  432. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  433. #endif /* CONFIG_P2P */
  434. }
  435. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  436. struct wps_event_er_ap *ap)
  437. {
  438. char uuid_str[100];
  439. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  440. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  441. if (ap->pri_dev_type)
  442. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  443. sizeof(dev_type));
  444. else
  445. dev_type[0] = '\0';
  446. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  447. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  448. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  449. ap->friendly_name ? ap->friendly_name : "",
  450. ap->manufacturer ? ap->manufacturer : "",
  451. ap->model_description ? ap->model_description : "",
  452. ap->model_name ? ap->model_name : "",
  453. ap->manufacturer_url ? ap->manufacturer_url : "",
  454. ap->model_url ? ap->model_url : "");
  455. }
  456. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  457. struct wps_event_er_ap *ap)
  458. {
  459. char uuid_str[100];
  460. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  461. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  462. }
  463. static void wpa_supplicant_wps_event_er_enrollee_add(
  464. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  465. {
  466. char uuid_str[100];
  467. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  468. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  469. if (enrollee->pri_dev_type)
  470. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  471. sizeof(dev_type));
  472. else
  473. dev_type[0] = '\0';
  474. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  475. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  476. "|%s|%s|%s|%s|%s|",
  477. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  478. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  479. enrollee->dev_name ? enrollee->dev_name : "",
  480. enrollee->manufacturer ? enrollee->manufacturer : "",
  481. enrollee->model_name ? enrollee->model_name : "",
  482. enrollee->model_number ? enrollee->model_number : "",
  483. enrollee->serial_number ? enrollee->serial_number : "");
  484. }
  485. static void wpa_supplicant_wps_event_er_enrollee_remove(
  486. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  487. {
  488. char uuid_str[100];
  489. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  490. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  491. uuid_str, MAC2STR(enrollee->mac_addr));
  492. }
  493. static void wpa_supplicant_wps_event_er_ap_settings(
  494. struct wpa_supplicant *wpa_s,
  495. struct wps_event_er_ap_settings *ap_settings)
  496. {
  497. char uuid_str[100];
  498. char key_str[65];
  499. const struct wps_credential *cred = ap_settings->cred;
  500. key_str[0] = '\0';
  501. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  502. if (cred->key_len >= 8 && cred->key_len <= 64) {
  503. os_memcpy(key_str, cred->key, cred->key_len);
  504. key_str[cred->key_len] = '\0';
  505. }
  506. }
  507. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  508. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  509. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  510. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  511. "key=%s",
  512. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  513. cred->auth_type, cred->encr_type, key_str);
  514. }
  515. static void wpa_supplicant_wps_event_er_set_sel_reg(
  516. struct wpa_supplicant *wpa_s,
  517. struct wps_event_er_set_selected_registrar *ev)
  518. {
  519. char uuid_str[100];
  520. uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
  521. switch (ev->state) {
  522. case WPS_ER_SET_SEL_REG_START:
  523. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  524. "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
  525. "sel_reg_config_methods=0x%x",
  526. uuid_str, ev->sel_reg, ev->dev_passwd_id,
  527. ev->sel_reg_config_methods);
  528. break;
  529. case WPS_ER_SET_SEL_REG_DONE:
  530. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  531. "uuid=%s state=DONE", uuid_str);
  532. break;
  533. case WPS_ER_SET_SEL_REG_FAILED:
  534. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
  535. "uuid=%s state=FAILED", uuid_str);
  536. break;
  537. }
  538. }
  539. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  540. union wps_event_data *data)
  541. {
  542. struct wpa_supplicant *wpa_s = ctx;
  543. switch (event) {
  544. case WPS_EV_M2D:
  545. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  546. break;
  547. case WPS_EV_FAIL:
  548. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  549. break;
  550. case WPS_EV_SUCCESS:
  551. wpa_supplicant_wps_event_success(wpa_s);
  552. break;
  553. case WPS_EV_PWD_AUTH_FAIL:
  554. #ifdef CONFIG_AP
  555. if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
  556. wpa_supplicant_ap_pwd_auth_fail(wpa_s);
  557. #endif /* CONFIG_AP */
  558. break;
  559. case WPS_EV_PBC_OVERLAP:
  560. break;
  561. case WPS_EV_PBC_TIMEOUT:
  562. break;
  563. case WPS_EV_ER_AP_ADD:
  564. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  565. break;
  566. case WPS_EV_ER_AP_REMOVE:
  567. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  568. break;
  569. case WPS_EV_ER_ENROLLEE_ADD:
  570. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  571. &data->enrollee);
  572. break;
  573. case WPS_EV_ER_ENROLLEE_REMOVE:
  574. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  575. &data->enrollee);
  576. break;
  577. case WPS_EV_ER_AP_SETTINGS:
  578. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  579. &data->ap_settings);
  580. break;
  581. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  582. wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
  583. &data->set_sel_reg);
  584. break;
  585. }
  586. }
  587. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  588. {
  589. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  590. eap_is_wps_pin_enrollee(&ssid->eap))
  591. return WPS_REQ_ENROLLEE;
  592. else
  593. return WPS_REQ_REGISTRAR;
  594. }
  595. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  596. {
  597. int id;
  598. struct wpa_ssid *ssid, *remove_ssid = NULL;
  599. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  600. /* Remove any existing WPS network from configuration */
  601. ssid = wpa_s->conf->ssid;
  602. while (ssid) {
  603. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  604. if (ssid == wpa_s->current_ssid) {
  605. wpa_s->current_ssid = NULL;
  606. if (ssid != NULL)
  607. wpas_notify_network_changed(wpa_s);
  608. }
  609. id = ssid->id;
  610. remove_ssid = ssid;
  611. } else
  612. id = -1;
  613. ssid = ssid->next;
  614. if (id >= 0) {
  615. wpas_notify_network_removed(wpa_s, remove_ssid);
  616. wpa_config_remove_network(wpa_s->conf, id);
  617. }
  618. }
  619. }
  620. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  621. {
  622. struct wpa_supplicant *wpa_s = eloop_ctx;
  623. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  624. "out");
  625. wpas_clear_wps(wpa_s);
  626. }
  627. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  628. int registrar, const u8 *bssid)
  629. {
  630. struct wpa_ssid *ssid;
  631. ssid = wpa_config_add_network(wpa_s->conf);
  632. if (ssid == NULL)
  633. return NULL;
  634. wpas_notify_network_added(wpa_s, ssid);
  635. wpa_config_set_network_defaults(ssid);
  636. ssid->temporary = 1;
  637. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  638. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  639. wpa_config_set(ssid, "identity", registrar ?
  640. "\"" WSC_ID_REGISTRAR "\"" :
  641. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  642. wpas_notify_network_removed(wpa_s, ssid);
  643. wpa_config_remove_network(wpa_s->conf, ssid->id);
  644. return NULL;
  645. }
  646. if (bssid) {
  647. #ifndef CONFIG_P2P
  648. struct wpa_bss *bss;
  649. int count = 0;
  650. #endif /* CONFIG_P2P */
  651. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  652. ssid->bssid_set = 1;
  653. /*
  654. * Note: With P2P, the SSID may change at the time the WPS
  655. * provisioning is started, so better not filter the AP based
  656. * on the current SSID in the scan results.
  657. */
  658. #ifndef CONFIG_P2P
  659. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  660. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  661. continue;
  662. os_free(ssid->ssid);
  663. ssid->ssid = os_malloc(bss->ssid_len);
  664. if (ssid->ssid == NULL)
  665. break;
  666. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  667. ssid->ssid_len = bss->ssid_len;
  668. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  669. "scan results",
  670. ssid->ssid, ssid->ssid_len);
  671. count++;
  672. }
  673. if (count > 1) {
  674. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  675. "for the AP; use wildcard");
  676. os_free(ssid->ssid);
  677. ssid->ssid = NULL;
  678. ssid->ssid_len = 0;
  679. }
  680. #endif /* CONFIG_P2P */
  681. }
  682. return ssid;
  683. }
  684. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  685. struct wpa_ssid *selected)
  686. {
  687. struct wpa_ssid *ssid;
  688. /* Mark all other networks disabled and trigger reassociation */
  689. ssid = wpa_s->conf->ssid;
  690. while (ssid) {
  691. int was_disabled = ssid->disabled;
  692. ssid->disabled = ssid != selected;
  693. if (was_disabled != ssid->disabled)
  694. wpas_notify_network_enabled_changed(wpa_s, ssid);
  695. ssid = ssid->next;
  696. }
  697. wpa_s->disconnected = 0;
  698. wpa_s->reassociate = 1;
  699. wpa_s->scan_runs = 0;
  700. wpa_s->wps_success = 0;
  701. wpa_s->blacklist_cleared = 0;
  702. wpa_supplicant_req_scan(wpa_s, 0, 0);
  703. }
  704. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  705. int p2p_group)
  706. {
  707. struct wpa_ssid *ssid;
  708. wpas_clear_wps(wpa_s);
  709. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  710. if (ssid == NULL)
  711. return -1;
  712. ssid->temporary = 1;
  713. ssid->p2p_group = p2p_group;
  714. #ifdef CONFIG_P2P
  715. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  716. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  717. if (ssid->ssid) {
  718. ssid->ssid_len = wpa_s->go_params->ssid_len;
  719. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  720. ssid->ssid_len);
  721. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  722. "SSID", ssid->ssid, ssid->ssid_len);
  723. }
  724. }
  725. #endif /* CONFIG_P2P */
  726. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  727. if (wpa_s->wps_fragment_size)
  728. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  729. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  730. wpa_s, NULL);
  731. wpas_wps_reassoc(wpa_s, ssid);
  732. return 0;
  733. }
  734. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  735. const char *pin, int p2p_group, u16 dev_pw_id)
  736. {
  737. struct wpa_ssid *ssid;
  738. char val[128];
  739. unsigned int rpin = 0;
  740. wpas_clear_wps(wpa_s);
  741. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  742. if (ssid == NULL)
  743. return -1;
  744. ssid->temporary = 1;
  745. ssid->p2p_group = p2p_group;
  746. #ifdef CONFIG_P2P
  747. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  748. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  749. if (ssid->ssid) {
  750. ssid->ssid_len = wpa_s->go_params->ssid_len;
  751. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  752. ssid->ssid_len);
  753. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  754. "SSID", ssid->ssid, ssid->ssid_len);
  755. }
  756. }
  757. #endif /* CONFIG_P2P */
  758. if (pin)
  759. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
  760. pin, dev_pw_id);
  761. else {
  762. rpin = wps_generate_pin();
  763. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
  764. rpin, dev_pw_id);
  765. }
  766. wpa_config_set(ssid, "phase1", val, 0);
  767. if (wpa_s->wps_fragment_size)
  768. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  769. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  770. wpa_s, NULL);
  771. wpas_wps_reassoc(wpa_s, ssid);
  772. return rpin;
  773. }
  774. /* Cancel the wps pbc/pin requests */
  775. int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
  776. {
  777. #ifdef CONFIG_AP
  778. if (wpa_s->ap_iface) {
  779. wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
  780. return wpa_supplicant_ap_wps_cancel(wpa_s);
  781. }
  782. #endif /* CONFIG_AP */
  783. if (wpa_s->wpa_state == WPA_SCANNING) {
  784. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
  785. wpa_supplicant_cancel_scan(wpa_s);
  786. wpas_clear_wps(wpa_s);
  787. } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  788. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
  789. "deauthenticate");
  790. wpa_supplicant_deauthenticate(wpa_s,
  791. WLAN_REASON_DEAUTH_LEAVING);
  792. wpas_clear_wps(wpa_s);
  793. }
  794. return 0;
  795. }
  796. #ifdef CONFIG_WPS_OOB
  797. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  798. char *path, char *method, char *name)
  799. {
  800. struct wps_context *wps = wpa_s->wps;
  801. struct oob_device_data *oob_dev;
  802. oob_dev = wps_get_oob_device(device_type);
  803. if (oob_dev == NULL)
  804. return -1;
  805. oob_dev->device_path = path;
  806. oob_dev->device_name = name;
  807. wps->oob_conf.oob_method = wps_get_oob_method(method);
  808. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  809. /*
  810. * Use pre-configured DH keys in order to be able to write the
  811. * key hash into the OOB file.
  812. */
  813. wpabuf_free(wps->dh_pubkey);
  814. wpabuf_free(wps->dh_privkey);
  815. wps->dh_privkey = NULL;
  816. wps->dh_pubkey = NULL;
  817. dh5_free(wps->dh_ctx);
  818. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  819. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  820. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  821. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  822. "Diffie-Hellman handshake");
  823. return -1;
  824. }
  825. }
  826. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  827. wpas_clear_wps(wpa_s);
  828. if (wps_process_oob(wps, oob_dev, 0) < 0)
  829. return -1;
  830. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  831. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  832. wpas_wps_start_pin(wpa_s, NULL,
  833. wpabuf_head(wps->oob_conf.dev_password), 0,
  834. DEV_PW_DEFAULT) < 0)
  835. return -1;
  836. return 0;
  837. }
  838. #endif /* CONFIG_WPS_OOB */
  839. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  840. const char *pin, struct wps_new_ap_settings *settings)
  841. {
  842. struct wpa_ssid *ssid;
  843. char val[200];
  844. char *pos, *end;
  845. int res;
  846. if (!pin)
  847. return -1;
  848. wpas_clear_wps(wpa_s);
  849. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  850. if (ssid == NULL)
  851. return -1;
  852. ssid->temporary = 1;
  853. pos = val;
  854. end = pos + sizeof(val);
  855. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  856. if (res < 0 || res >= end - pos)
  857. return -1;
  858. pos += res;
  859. if (settings) {
  860. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  861. "new_encr=%s new_key=%s",
  862. settings->ssid_hex, settings->auth,
  863. settings->encr, settings->key_hex);
  864. if (res < 0 || res >= end - pos)
  865. return -1;
  866. pos += res;
  867. }
  868. res = os_snprintf(pos, end - pos, "\"");
  869. if (res < 0 || res >= end - pos)
  870. return -1;
  871. wpa_config_set(ssid, "phase1", val, 0);
  872. if (wpa_s->wps_fragment_size)
  873. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  874. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  875. wpa_s, NULL);
  876. wpas_wps_reassoc(wpa_s, ssid);
  877. return 0;
  878. }
  879. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  880. size_t psk_len)
  881. {
  882. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  883. "STA " MACSTR, MAC2STR(mac_addr));
  884. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  885. /* TODO */
  886. return 0;
  887. }
  888. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  889. const struct wps_device_data *dev)
  890. {
  891. char uuid[40], txt[400];
  892. int len;
  893. char devtype[WPS_DEV_TYPE_BUFSIZE];
  894. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  895. return;
  896. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  897. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  898. " [%s|%s|%s|%s|%s|%s]",
  899. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  900. dev->manufacturer, dev->model_name,
  901. dev->model_number, dev->serial_number,
  902. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  903. sizeof(devtype)));
  904. if (len > 0 && len < (int) sizeof(txt))
  905. wpa_printf(MSG_INFO, "%s", txt);
  906. }
  907. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  908. u16 sel_reg_config_methods)
  909. {
  910. #ifdef CONFIG_WPS_ER
  911. struct wpa_supplicant *wpa_s = ctx;
  912. if (wpa_s->wps_er == NULL)
  913. return;
  914. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  915. "dev_password_id=%u sel_reg_config_methods=0x%x",
  916. sel_reg, dev_passwd_id, sel_reg_config_methods);
  917. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  918. sel_reg_config_methods);
  919. #endif /* CONFIG_WPS_ER */
  920. }
  921. static u16 wps_fix_config_methods(u16 config_methods)
  922. {
  923. #ifdef CONFIG_WPS2
  924. if ((config_methods &
  925. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  926. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  927. wpa_printf(MSG_INFO, "WPS: Converting display to "
  928. "virtual_display for WPS 2.0 compliance");
  929. config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  930. }
  931. if ((config_methods &
  932. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  933. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  934. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  935. "virtual_push_button for WPS 2.0 compliance");
  936. config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  937. }
  938. #endif /* CONFIG_WPS2 */
  939. return config_methods;
  940. }
  941. static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
  942. struct wps_context *wps)
  943. {
  944. wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
  945. if (is_nil_uuid(wpa_s->conf->uuid)) {
  946. struct wpa_supplicant *first;
  947. first = wpa_s->global->ifaces;
  948. while (first && first->next)
  949. first = first->next;
  950. if (first && first != wpa_s) {
  951. os_memcpy(wps->uuid, wpa_s->global->ifaces->wps->uuid,
  952. WPS_UUID_LEN);
  953. wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
  954. "interface", wps->uuid, WPS_UUID_LEN);
  955. } else {
  956. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  957. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  958. "address", wps->uuid, WPS_UUID_LEN);
  959. }
  960. } else {
  961. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  962. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
  963. wps->uuid, WPS_UUID_LEN);
  964. }
  965. }
  966. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  967. {
  968. struct wps_context *wps;
  969. struct wps_registrar_config rcfg;
  970. int i;
  971. wps = os_zalloc(sizeof(*wps));
  972. if (wps == NULL)
  973. return -1;
  974. wps->cred_cb = wpa_supplicant_wps_cred;
  975. wps->event_cb = wpa_supplicant_wps_event;
  976. wps->cb_ctx = wpa_s;
  977. wps->dev.device_name = wpa_s->conf->device_name;
  978. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  979. wps->dev.model_name = wpa_s->conf->model_name;
  980. wps->dev.model_number = wpa_s->conf->model_number;
  981. wps->dev.serial_number = wpa_s->conf->serial_number;
  982. wps->config_methods =
  983. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  984. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  985. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  986. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  987. "methods are not allowed at the same time");
  988. os_free(wps);
  989. return -1;
  990. }
  991. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  992. if (wpa_s->conf->device_type &&
  993. wps_dev_type_str2bin(wpa_s->conf->device_type,
  994. wps->dev.pri_dev_type) < 0) {
  995. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  996. os_free(wps);
  997. return -1;
  998. }
  999. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  1000. if (wpa_s->conf->sec_device_type[i] == NULL)
  1001. continue;
  1002. if (wps_dev_type_str2bin(
  1003. wpa_s->conf->sec_device_type[i],
  1004. wps->dev.sec_dev_type[wps->dev.num_sec_dev_types])
  1005. < 0) {
  1006. wpa_printf(MSG_ERROR, "WPS: Invalid sec_device_type");
  1007. return -1;
  1008. }
  1009. wps->dev.num_sec_dev_types++;
  1010. if (wps->dev.num_sec_dev_types == WPS_SEC_DEVICE_TYPES)
  1011. break;
  1012. }
  1013. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1014. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
  1015. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  1016. wpas_wps_set_uuid(wpa_s, wps);
  1017. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  1018. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  1019. os_memset(&rcfg, 0, sizeof(rcfg));
  1020. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  1021. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  1022. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  1023. rcfg.cb_ctx = wpa_s;
  1024. wps->registrar = wps_registrar_init(wps, &rcfg);
  1025. if (wps->registrar == NULL) {
  1026. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  1027. os_free(wps);
  1028. return -1;
  1029. }
  1030. wpa_s->wps = wps;
  1031. return 0;
  1032. }
  1033. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  1034. {
  1035. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  1036. if (wpa_s->wps == NULL)
  1037. return;
  1038. #ifdef CONFIG_WPS_ER
  1039. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1040. wpa_s->wps_er = NULL;
  1041. #endif /* CONFIG_WPS_ER */
  1042. wps_registrar_deinit(wpa_s->wps->registrar);
  1043. wpabuf_free(wpa_s->wps->dh_pubkey);
  1044. wpabuf_free(wpa_s->wps->dh_privkey);
  1045. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  1046. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  1047. os_free(wpa_s->wps->network_key);
  1048. os_free(wpa_s->wps);
  1049. wpa_s->wps = NULL;
  1050. }
  1051. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  1052. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  1053. {
  1054. struct wpabuf *wps_ie;
  1055. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  1056. return -1;
  1057. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1058. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1059. if (!wps_ie) {
  1060. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1061. return 0;
  1062. }
  1063. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  1064. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1065. "without active PBC Registrar");
  1066. wpabuf_free(wps_ie);
  1067. return 0;
  1068. }
  1069. /* TODO: overlap detection */
  1070. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1071. "(Active PBC)");
  1072. wpabuf_free(wps_ie);
  1073. return 1;
  1074. }
  1075. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1076. if (!wps_ie) {
  1077. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1078. return 0;
  1079. }
  1080. /*
  1081. * Start with WPS APs that advertise our address as an
  1082. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  1083. * allow any WPS AP after couple of scans since some APs do not
  1084. * set Selected Registrar attribute properly when using
  1085. * external Registrar.
  1086. */
  1087. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  1088. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  1089. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1090. "without active PIN Registrar");
  1091. wpabuf_free(wps_ie);
  1092. return 0;
  1093. }
  1094. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1095. } else {
  1096. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1097. "(Authorized MAC or Active PIN)");
  1098. }
  1099. wpabuf_free(wps_ie);
  1100. return 1;
  1101. }
  1102. if (wps_ie) {
  1103. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1104. wpabuf_free(wps_ie);
  1105. return 1;
  1106. }
  1107. return -1;
  1108. }
  1109. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  1110. struct wpa_ssid *ssid,
  1111. struct wpa_scan_res *bss)
  1112. {
  1113. struct wpabuf *wps_ie = NULL;
  1114. int ret = 0;
  1115. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1116. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1117. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  1118. /* allow wildcard SSID for WPS PBC */
  1119. ret = 1;
  1120. }
  1121. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1122. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1123. if (wps_ie &&
  1124. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  1125. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  1126. /* allow wildcard SSID for WPS PIN */
  1127. ret = 1;
  1128. }
  1129. }
  1130. if (!ret && ssid->bssid_set &&
  1131. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  1132. /* allow wildcard SSID due to hardcoded BSSID match */
  1133. ret = 1;
  1134. }
  1135. #ifdef CONFIG_WPS_STRICT
  1136. if (wps_ie) {
  1137. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  1138. 0, bss->bssid) < 0)
  1139. ret = 0;
  1140. if (bss->beacon_ie_len) {
  1141. struct wpabuf *bcn_wps;
  1142. bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
  1143. bss, WPS_IE_VENDOR_TYPE);
  1144. if (bcn_wps == NULL) {
  1145. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  1146. "missing from AP Beacon");
  1147. ret = 0;
  1148. } else {
  1149. if (wps_validate_beacon(wps_ie) < 0)
  1150. ret = 0;
  1151. wpabuf_free(bcn_wps);
  1152. }
  1153. }
  1154. }
  1155. #endif /* CONFIG_WPS_STRICT */
  1156. wpabuf_free(wps_ie);
  1157. return ret;
  1158. }
  1159. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  1160. struct wpa_bss *selected, struct wpa_ssid *ssid)
  1161. {
  1162. const u8 *sel_uuid, *uuid;
  1163. struct wpabuf *wps_ie;
  1164. int ret = 0;
  1165. struct wpa_bss *bss;
  1166. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  1167. return 0;
  1168. wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
  1169. "present in scan results; selected BSSID " MACSTR,
  1170. MAC2STR(selected->bssid));
  1171. /* Make sure that only one AP is in active PBC mode */
  1172. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  1173. if (wps_ie) {
  1174. sel_uuid = wps_get_uuid_e(wps_ie);
  1175. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
  1176. sel_uuid, UUID_LEN);
  1177. } else {
  1178. wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
  1179. "WPS IE?!");
  1180. sel_uuid = NULL;
  1181. }
  1182. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1183. struct wpabuf *ie;
  1184. if (bss == selected)
  1185. continue;
  1186. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1187. if (!ie)
  1188. continue;
  1189. if (!wps_is_selected_pbc_registrar(ie)) {
  1190. wpabuf_free(ie);
  1191. continue;
  1192. }
  1193. wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
  1194. MACSTR, MAC2STR(bss->bssid));
  1195. uuid = wps_get_uuid_e(ie);
  1196. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
  1197. uuid, UUID_LEN);
  1198. if (sel_uuid == NULL || uuid == NULL ||
  1199. os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
  1200. ret = 1; /* PBC overlap */
  1201. wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
  1202. MACSTR " and " MACSTR,
  1203. MAC2STR(selected->bssid),
  1204. MAC2STR(bss->bssid));
  1205. wpabuf_free(ie);
  1206. break;
  1207. }
  1208. /* TODO: verify that this is reasonable dual-band situation */
  1209. wpabuf_free(ie);
  1210. }
  1211. wpabuf_free(wps_ie);
  1212. return ret;
  1213. }
  1214. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1215. {
  1216. struct wpa_bss *bss;
  1217. unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
  1218. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1219. return;
  1220. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1221. struct wpabuf *ie;
  1222. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1223. if (!ie)
  1224. continue;
  1225. if (wps_is_selected_pbc_registrar(ie))
  1226. pbc++;
  1227. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1228. auth++;
  1229. else if (wps_is_selected_pin_registrar(ie))
  1230. pin++;
  1231. else
  1232. wps++;
  1233. wpabuf_free(ie);
  1234. }
  1235. if (pbc)
  1236. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  1237. else if (auth)
  1238. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
  1239. else if (pin)
  1240. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  1241. else if (wps)
  1242. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  1243. }
  1244. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1245. {
  1246. struct wpa_ssid *ssid;
  1247. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1248. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1249. return 1;
  1250. }
  1251. return 0;
  1252. }
  1253. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1254. char *end)
  1255. {
  1256. struct wpabuf *wps_ie;
  1257. int ret;
  1258. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1259. if (wps_ie == NULL)
  1260. return 0;
  1261. ret = wps_attr_text(wps_ie, buf, end);
  1262. wpabuf_free(wps_ie);
  1263. return ret;
  1264. }
  1265. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1266. {
  1267. #ifdef CONFIG_WPS_ER
  1268. if (wpa_s->wps_er) {
  1269. wps_er_refresh(wpa_s->wps_er);
  1270. return 0;
  1271. }
  1272. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1273. if (wpa_s->wps_er == NULL)
  1274. return -1;
  1275. return 0;
  1276. #else /* CONFIG_WPS_ER */
  1277. return 0;
  1278. #endif /* CONFIG_WPS_ER */
  1279. }
  1280. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1281. {
  1282. #ifdef CONFIG_WPS_ER
  1283. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1284. wpa_s->wps_er = NULL;
  1285. #endif /* CONFIG_WPS_ER */
  1286. return 0;
  1287. }
  1288. #ifdef CONFIG_WPS_ER
  1289. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1290. const char *uuid, const char *pin)
  1291. {
  1292. u8 u[UUID_LEN];
  1293. int any = 0;
  1294. if (os_strcmp(uuid, "any") == 0)
  1295. any = 1;
  1296. else if (uuid_str2bin(uuid, u))
  1297. return -1;
  1298. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1299. any ? NULL : u,
  1300. (const u8 *) pin, os_strlen(pin), 300);
  1301. }
  1302. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1303. {
  1304. u8 u[UUID_LEN];
  1305. if (uuid_str2bin(uuid, u))
  1306. return -1;
  1307. return wps_er_pbc(wpa_s->wps_er, u);
  1308. }
  1309. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1310. const char *pin)
  1311. {
  1312. u8 u[UUID_LEN];
  1313. if (uuid_str2bin(uuid, u))
  1314. return -1;
  1315. return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
  1316. os_strlen(pin));
  1317. }
  1318. int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1319. int id)
  1320. {
  1321. u8 u[UUID_LEN];
  1322. struct wpa_ssid *ssid;
  1323. struct wps_credential cred;
  1324. if (uuid_str2bin(uuid, u))
  1325. return -1;
  1326. ssid = wpa_config_get_network(wpa_s->conf, id);
  1327. if (ssid == NULL || ssid->ssid == NULL)
  1328. return -1;
  1329. os_memset(&cred, 0, sizeof(cred));
  1330. if (ssid->ssid_len > 32)
  1331. return -1;
  1332. os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
  1333. cred.ssid_len = ssid->ssid_len;
  1334. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  1335. cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
  1336. WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
  1337. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  1338. cred.encr_type = WPS_ENCR_AES;
  1339. else
  1340. cred.encr_type = WPS_ENCR_TKIP;
  1341. if (ssid->passphrase) {
  1342. cred.key_len = os_strlen(ssid->passphrase);
  1343. if (cred.key_len >= 64)
  1344. return -1;
  1345. os_memcpy(cred.key, ssid->passphrase, cred.key_len);
  1346. } else if (ssid->psk_set) {
  1347. cred.key_len = 32;
  1348. os_memcpy(cred.key, ssid->psk, 32);
  1349. } else
  1350. return -1;
  1351. } else {
  1352. cred.auth_type = WPS_AUTH_OPEN;
  1353. cred.encr_type = WPS_ENCR_NONE;
  1354. }
  1355. return wps_er_set_config(wpa_s->wps_er, u, &cred);
  1356. }
  1357. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1358. const char *pin, struct wps_new_ap_settings *settings)
  1359. {
  1360. u8 u[UUID_LEN];
  1361. struct wps_credential cred;
  1362. size_t len;
  1363. if (uuid_str2bin(uuid, u))
  1364. return -1;
  1365. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1366. settings->encr == NULL || settings->key_hex == NULL)
  1367. return -1;
  1368. os_memset(&cred, 0, sizeof(cred));
  1369. len = os_strlen(settings->ssid_hex);
  1370. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1371. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1372. return -1;
  1373. cred.ssid_len = len / 2;
  1374. len = os_strlen(settings->key_hex);
  1375. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1376. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1377. return -1;
  1378. cred.key_len = len / 2;
  1379. if (os_strcmp(settings->auth, "OPEN") == 0)
  1380. cred.auth_type = WPS_AUTH_OPEN;
  1381. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1382. cred.auth_type = WPS_AUTH_WPAPSK;
  1383. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1384. cred.auth_type = WPS_AUTH_WPA2PSK;
  1385. else
  1386. return -1;
  1387. if (os_strcmp(settings->encr, "NONE") == 0)
  1388. cred.encr_type = WPS_ENCR_NONE;
  1389. else if (os_strcmp(settings->encr, "WEP") == 0)
  1390. cred.encr_type = WPS_ENCR_WEP;
  1391. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1392. cred.encr_type = WPS_ENCR_TKIP;
  1393. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1394. cred.encr_type = WPS_ENCR_AES;
  1395. else
  1396. return -1;
  1397. return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
  1398. os_strlen(pin), &cred);
  1399. }
  1400. static int callbacks_pending = 0;
  1401. static void wpas_wps_terminate_cb(void *ctx)
  1402. {
  1403. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1404. if (--callbacks_pending <= 0)
  1405. eloop_terminate();
  1406. }
  1407. #endif /* CONFIG_WPS_ER */
  1408. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1409. {
  1410. #ifdef CONFIG_WPS_ER
  1411. if (wpa_s->wps_er) {
  1412. callbacks_pending++;
  1413. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1414. wpa_s->wps_er = NULL;
  1415. return 1;
  1416. }
  1417. #endif /* CONFIG_WPS_ER */
  1418. return 0;
  1419. }
  1420. int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
  1421. {
  1422. struct wpa_ssid *ssid;
  1423. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1424. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  1425. return 1;
  1426. }
  1427. return 0;
  1428. }
  1429. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1430. {
  1431. struct wps_context *wps = wpa_s->wps;
  1432. if (wps == NULL)
  1433. return;
  1434. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1435. wps->config_methods = wps_config_methods_str2bin(
  1436. wpa_s->conf->config_methods);
  1437. if ((wps->config_methods &
  1438. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1439. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1440. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1441. "config methods are not allowed at the "
  1442. "same time");
  1443. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1444. }
  1445. }
  1446. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1447. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) {
  1448. if (wpa_s->conf->device_type &&
  1449. wps_dev_type_str2bin(wpa_s->conf->device_type,
  1450. wps->dev.pri_dev_type) < 0)
  1451. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  1452. }
  1453. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  1454. int i;
  1455. wps->dev.num_sec_dev_types = 0;
  1456. for (i = 0; i < MAX_SEC_DEVICE_TYPES; i++) {
  1457. if (wpa_s->conf->sec_device_type[i] == NULL)
  1458. continue;
  1459. if (wps_dev_type_str2bin(
  1460. wpa_s->conf->sec_device_type[i],
  1461. wps->dev.sec_dev_type[
  1462. wps->dev.num_sec_dev_types]) < 0) {
  1463. wpa_printf(MSG_ERROR,
  1464. "WPS: Invalid sec_device_type");
  1465. continue;
  1466. }
  1467. wps->dev.num_sec_dev_types++;
  1468. if (wps->dev.num_sec_dev_types == WPS_SEC_DEVICE_TYPES)
  1469. break;
  1470. }
  1471. }
  1472. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1473. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1474. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
  1475. wpas_wps_set_uuid(wpa_s, wps);
  1476. if (wpa_s->conf->changed_parameters &
  1477. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1478. /* Update pointers to make sure they refer current values */
  1479. wps->dev.device_name = wpa_s->conf->device_name;
  1480. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1481. wps->dev.model_name = wpa_s->conf->model_name;
  1482. wps->dev.model_number = wpa_s->conf->model_number;
  1483. wps->dev.serial_number = wpa_s->conf->serial_number;
  1484. }
  1485. }