wps_supplicant.c 47 KB

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