wps_supplicant.c 43 KB

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