Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

wps_supplicant.c 46KB

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