wps_supplicant.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "uuid.h"
  12. #include "crypto/random.h"
  13. #include "crypto/dh_group5.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "common/ieee802_11_common.h"
  16. #include "common/wpa_common.h"
  17. #include "common/wpa_ctrl.h"
  18. #include "eap_common/eap_wsc_common.h"
  19. #include "eap_peer/eap.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "wps/wps_attr_parse.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "notify.h"
  27. #include "blacklist.h"
  28. #include "bss.h"
  29. #include "scan.h"
  30. #include "ap.h"
  31. #include "p2p/p2p.h"
  32. #include "p2p_supplicant.h"
  33. #include "wps_supplicant.h"
  34. #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
  35. #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
  36. #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
  37. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  38. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  39. static void wpas_wps_clear_ap_info(struct wpa_supplicant *wpa_s)
  40. {
  41. os_free(wpa_s->wps_ap);
  42. wpa_s->wps_ap = NULL;
  43. wpa_s->num_wps_ap = 0;
  44. wpa_s->wps_ap_iter = 0;
  45. }
  46. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  47. {
  48. if (!wpa_s->wps_success &&
  49. wpa_s->current_ssid &&
  50. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  51. const u8 *bssid = wpa_s->bssid;
  52. if (is_zero_ether_addr(bssid))
  53. bssid = wpa_s->pending_bssid;
  54. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  55. " did not succeed - continue trying to find "
  56. "suitable AP", MAC2STR(bssid));
  57. wpa_blacklist_add(wpa_s, bssid);
  58. wpa_supplicant_deauthenticate(wpa_s,
  59. WLAN_REASON_DEAUTH_LEAVING);
  60. wpa_s->reassociate = 1;
  61. wpa_supplicant_req_scan(wpa_s,
  62. wpa_s->blacklist_cleared ? 5 : 0, 0);
  63. wpa_s->blacklist_cleared = 0;
  64. return 1;
  65. }
  66. wpas_wps_clear_ap_info(wpa_s);
  67. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  68. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
  69. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
  70. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  71. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  72. int disabled = wpa_s->current_ssid->disabled;
  73. unsigned int freq = wpa_s->assoc_freq;
  74. struct wpa_bss *bss;
  75. struct wpa_ssid *ssid = NULL;
  76. int use_fast_assoc = 0;
  77. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  78. "try to associate with the received credential "
  79. "(freq=%u)", freq);
  80. wpa_supplicant_deauthenticate(wpa_s,
  81. WLAN_REASON_DEAUTH_LEAVING);
  82. if (disabled) {
  83. wpa_printf(MSG_DEBUG, "WPS: Current network is "
  84. "disabled - wait for user to enable");
  85. return 1;
  86. }
  87. wpa_s->after_wps = 5;
  88. wpa_s->wps_freq = freq;
  89. wpa_s->normal_scans = 0;
  90. wpa_s->reassociate = 1;
  91. wpa_printf(MSG_DEBUG, "WPS: Checking whether fast association "
  92. "without a new scan can be used");
  93. bss = wpa_supplicant_pick_network(wpa_s, &ssid);
  94. if (bss) {
  95. struct wpabuf *wps;
  96. struct wps_parse_attr attr;
  97. wps = wpa_bss_get_vendor_ie_multi(bss,
  98. WPS_IE_VENDOR_TYPE);
  99. if (wps && wps_parse_msg(wps, &attr) == 0 &&
  100. attr.wps_state &&
  101. *attr.wps_state == WPS_STATE_CONFIGURED)
  102. use_fast_assoc = 1;
  103. wpabuf_free(wps);
  104. }
  105. if (!use_fast_assoc ||
  106. wpa_supplicant_fast_associate(wpa_s) != 1)
  107. wpa_supplicant_req_scan(wpa_s, 0, 0);
  108. return 1;
  109. }
  110. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  111. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  112. "for external credential processing");
  113. wpas_clear_wps(wpa_s);
  114. wpa_supplicant_deauthenticate(wpa_s,
  115. WLAN_REASON_DEAUTH_LEAVING);
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
  121. struct wpa_ssid *ssid,
  122. const struct wps_credential *cred)
  123. {
  124. struct wpa_driver_capa capa;
  125. struct wpa_bss *bss;
  126. const u8 *ie;
  127. struct wpa_ie_data adv;
  128. int wpa2 = 0, ccmp = 0;
  129. /*
  130. * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
  131. * case they are configured for mixed mode operation (WPA+WPA2 and
  132. * TKIP+CCMP). Try to use scan results to figure out whether the AP
  133. * actually supports stronger security and select that if the client
  134. * has support for it, too.
  135. */
  136. if (wpa_drv_get_capa(wpa_s, &capa))
  137. return; /* Unknown what driver supports */
  138. if (ssid->ssid == NULL)
  139. return;
  140. bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
  141. if (bss == NULL) {
  142. wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
  143. "table - use credential as-is");
  144. return;
  145. }
  146. wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
  147. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  148. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
  149. wpa2 = 1;
  150. if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
  151. ccmp = 1;
  152. } else {
  153. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  154. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
  155. adv.pairwise_cipher & WPA_CIPHER_CCMP)
  156. ccmp = 1;
  157. }
  158. if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
  159. (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
  160. /*
  161. * TODO: This could be the initial AP configuration and the
  162. * Beacon contents could change shortly. Should request a new
  163. * scan and delay addition of the network until the updated
  164. * scan results are available.
  165. */
  166. wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
  167. "support - use credential as-is");
  168. return;
  169. }
  170. if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  171. (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
  172. (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  173. wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
  174. "based on scan results");
  175. if (wpa_s->conf->ap_scan == 1)
  176. ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
  177. else
  178. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  179. }
  180. if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
  181. (ssid->proto & WPA_PROTO_WPA) &&
  182. (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
  183. wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
  184. "based on scan results");
  185. if (wpa_s->conf->ap_scan == 1)
  186. ssid->proto |= WPA_PROTO_RSN;
  187. else
  188. ssid->proto = WPA_PROTO_RSN;
  189. }
  190. }
  191. static void wpas_wps_remove_dup_network(struct wpa_supplicant *wpa_s,
  192. struct wpa_ssid *new_ssid)
  193. {
  194. struct wpa_ssid *ssid, *next;
  195. for (ssid = wpa_s->conf->ssid, next = ssid ? ssid->next : NULL; ssid;
  196. ssid = next, next = ssid ? ssid->next : NULL) {
  197. /*
  198. * new_ssid has already been added to the list in
  199. * wpas_wps_add_network(), so skip it.
  200. */
  201. if (ssid == new_ssid)
  202. continue;
  203. if (ssid->bssid_set || new_ssid->bssid_set) {
  204. if (ssid->bssid_set != new_ssid->bssid_set)
  205. continue;
  206. if (os_memcmp(ssid->bssid, new_ssid->bssid, ETH_ALEN) !=
  207. 0)
  208. continue;
  209. }
  210. /* compare SSID */
  211. if (ssid->ssid_len == 0 || ssid->ssid_len != new_ssid->ssid_len)
  212. continue;
  213. if (ssid->ssid && new_ssid->ssid) {
  214. if (os_memcmp(ssid->ssid, new_ssid->ssid,
  215. ssid->ssid_len) != 0)
  216. continue;
  217. } else if (ssid->ssid || new_ssid->ssid)
  218. continue;
  219. /* compare security parameters */
  220. if (ssid->auth_alg != new_ssid->auth_alg ||
  221. ssid->key_mgmt != new_ssid->key_mgmt ||
  222. ssid->proto != new_ssid->proto ||
  223. ssid->pairwise_cipher != new_ssid->pairwise_cipher ||
  224. ssid->group_cipher != new_ssid->group_cipher)
  225. continue;
  226. if (ssid->passphrase && new_ssid->passphrase) {
  227. if (os_strlen(ssid->passphrase) !=
  228. os_strlen(new_ssid->passphrase))
  229. continue;
  230. if (os_strcmp(ssid->passphrase, new_ssid->passphrase) !=
  231. 0)
  232. continue;
  233. } else if (ssid->passphrase || new_ssid->passphrase)
  234. continue;
  235. if ((ssid->psk_set || new_ssid->psk_set) &&
  236. os_memcmp(ssid->psk, new_ssid->psk, sizeof(ssid->psk)) != 0)
  237. continue;
  238. if (ssid->auth_alg == WPA_ALG_WEP) {
  239. if (ssid->wep_tx_keyidx != new_ssid->wep_tx_keyidx)
  240. continue;
  241. if (os_memcmp(ssid->wep_key, new_ssid->wep_key,
  242. sizeof(ssid->wep_key)))
  243. continue;
  244. if (os_memcmp(ssid->wep_key_len, new_ssid->wep_key_len,
  245. sizeof(ssid->wep_key_len)))
  246. continue;
  247. }
  248. /* Remove the duplicated older network entry. */
  249. wpa_printf(MSG_DEBUG, "Remove duplicate network %d", ssid->id);
  250. wpas_notify_network_removed(wpa_s, ssid);
  251. wpa_config_remove_network(wpa_s->conf, ssid->id);
  252. }
  253. }
  254. static int wpa_supplicant_wps_cred(void *ctx,
  255. const struct wps_credential *cred)
  256. {
  257. struct wpa_supplicant *wpa_s = ctx;
  258. struct wpa_ssid *ssid = wpa_s->current_ssid;
  259. u8 key_idx = 0;
  260. u16 auth_type;
  261. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  262. int registrar = 0;
  263. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  264. if ((wpa_s->conf->wps_cred_processing == 1 ||
  265. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  266. size_t blen = cred->cred_attr_len * 2 + 1;
  267. char *buf = os_malloc(blen);
  268. if (buf) {
  269. wpa_snprintf_hex(buf, blen,
  270. cred->cred_attr, cred->cred_attr_len);
  271. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  272. WPS_EVENT_CRED_RECEIVED, buf);
  273. os_free(buf);
  274. }
  275. wpas_notify_wps_credential(wpa_s, cred);
  276. } else
  277. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  278. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  279. cred->cred_attr, cred->cred_attr_len);
  280. if (wpa_s->conf->wps_cred_processing == 1)
  281. return 0;
  282. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  283. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  284. cred->auth_type);
  285. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  286. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  287. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  288. cred->key, cred->key_len);
  289. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  290. MAC2STR(cred->mac_addr));
  291. auth_type = cred->auth_type;
  292. if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  293. wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
  294. "auth_type into WPA2PSK");
  295. auth_type = WPS_AUTH_WPA2PSK;
  296. }
  297. if (auth_type != WPS_AUTH_OPEN &&
  298. auth_type != WPS_AUTH_SHARED &&
  299. auth_type != WPS_AUTH_WPAPSK &&
  300. auth_type != WPS_AUTH_WPA2PSK) {
  301. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  302. "unsupported authentication type 0x%x",
  303. auth_type);
  304. return 0;
  305. }
  306. if (auth_type == WPS_AUTH_WPAPSK || auth_type == WPS_AUTH_WPA2PSK) {
  307. if (cred->key_len < 8 || cred->key_len > 2 * PMK_LEN) {
  308. wpa_printf(MSG_ERROR, "WPS: Reject PSK credential with "
  309. "invalid Network Key length %lu",
  310. (unsigned long) cred->key_len);
  311. return -1;
  312. }
  313. }
  314. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  315. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  316. "on the received credential");
  317. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  318. if (ssid->eap.identity &&
  319. ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
  320. os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
  321. WSC_ID_REGISTRAR_LEN) == 0)
  322. registrar = 1;
  323. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  324. os_free(ssid->eap.identity);
  325. ssid->eap.identity = NULL;
  326. ssid->eap.identity_len = 0;
  327. os_free(ssid->eap.phase1);
  328. ssid->eap.phase1 = NULL;
  329. os_free(ssid->eap.eap_methods);
  330. ssid->eap.eap_methods = NULL;
  331. if (!ssid->p2p_group) {
  332. ssid->temporary = 0;
  333. ssid->bssid_set = 0;
  334. }
  335. ssid->disabled_until.sec = 0;
  336. ssid->disabled_until.usec = 0;
  337. ssid->auth_failures = 0;
  338. } else {
  339. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  340. "received credential");
  341. ssid = wpa_config_add_network(wpa_s->conf);
  342. if (ssid == NULL)
  343. return -1;
  344. wpas_notify_network_added(wpa_s, ssid);
  345. }
  346. wpa_config_set_network_defaults(ssid);
  347. os_free(ssid->ssid);
  348. ssid->ssid = os_malloc(cred->ssid_len);
  349. if (ssid->ssid) {
  350. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  351. ssid->ssid_len = cred->ssid_len;
  352. }
  353. switch (cred->encr_type) {
  354. case WPS_ENCR_NONE:
  355. break;
  356. case WPS_ENCR_WEP:
  357. if (cred->key_len <= 0)
  358. break;
  359. if (cred->key_len != 5 && cred->key_len != 13 &&
  360. cred->key_len != 10 && cred->key_len != 26) {
  361. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
  362. "%lu", (unsigned long) cred->key_len);
  363. return -1;
  364. }
  365. if (cred->key_idx > NUM_WEP_KEYS) {
  366. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
  367. cred->key_idx);
  368. return -1;
  369. }
  370. if (cred->key_idx)
  371. key_idx = cred->key_idx - 1;
  372. if (cred->key_len == 10 || cred->key_len == 26) {
  373. if (hexstr2bin((char *) cred->key,
  374. ssid->wep_key[key_idx],
  375. cred->key_len / 2) < 0) {
  376. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
  377. "%d", key_idx);
  378. return -1;
  379. }
  380. ssid->wep_key_len[key_idx] = cred->key_len / 2;
  381. } else {
  382. os_memcpy(ssid->wep_key[key_idx], cred->key,
  383. cred->key_len);
  384. ssid->wep_key_len[key_idx] = cred->key_len;
  385. }
  386. ssid->wep_tx_keyidx = key_idx;
  387. break;
  388. case WPS_ENCR_TKIP:
  389. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  390. break;
  391. case WPS_ENCR_AES:
  392. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  393. break;
  394. }
  395. switch (auth_type) {
  396. case WPS_AUTH_OPEN:
  397. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  398. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  399. ssid->proto = 0;
  400. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  401. if (registrar) {
  402. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
  403. "id=%d - Credentials for an open "
  404. "network disabled by default - use "
  405. "'select_network %d' to enable",
  406. ssid->id, ssid->id);
  407. ssid->disabled = 1;
  408. }
  409. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  410. break;
  411. case WPS_AUTH_SHARED:
  412. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  413. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  414. ssid->proto = 0;
  415. break;
  416. case WPS_AUTH_WPAPSK:
  417. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  418. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  419. ssid->proto = WPA_PROTO_WPA;
  420. break;
  421. case WPS_AUTH_WPA2PSK:
  422. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  423. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  424. ssid->proto = WPA_PROTO_RSN;
  425. break;
  426. }
  427. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  428. if (cred->key_len == 2 * PMK_LEN) {
  429. if (hexstr2bin((const char *) cred->key, ssid->psk,
  430. PMK_LEN)) {
  431. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  432. "Key");
  433. return -1;
  434. }
  435. ssid->psk_set = 1;
  436. ssid->export_keys = 1;
  437. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  438. os_free(ssid->passphrase);
  439. ssid->passphrase = os_malloc(cred->key_len + 1);
  440. if (ssid->passphrase == NULL)
  441. return -1;
  442. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  443. ssid->passphrase[cred->key_len] = '\0';
  444. wpa_config_update_psk(ssid);
  445. ssid->export_keys = 1;
  446. } else {
  447. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  448. "length %lu",
  449. (unsigned long) cred->key_len);
  450. return -1;
  451. }
  452. }
  453. wpas_wps_security_workaround(wpa_s, ssid, cred);
  454. if (cred->ap_channel)
  455. wpa_s->wps_ap_channel = cred->ap_channel;
  456. wpas_wps_remove_dup_network(wpa_s, ssid);
  457. #ifndef CONFIG_NO_CONFIG_WRITE
  458. if (wpa_s->conf->update_config &&
  459. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  460. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  461. return -1;
  462. }
  463. #endif /* CONFIG_NO_CONFIG_WRITE */
  464. /*
  465. * Optimize the post-WPS scan based on the channel used during
  466. * the provisioning in case EAP-Failure is not received.
  467. */
  468. wpa_s->after_wps = 5;
  469. wpa_s->wps_freq = wpa_s->assoc_freq;
  470. return 0;
  471. }
  472. #ifdef CONFIG_P2P
  473. static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  474. {
  475. struct wpa_supplicant *wpa_s = eloop_ctx;
  476. wpas_p2p_notif_pbc_overlap(wpa_s);
  477. }
  478. #endif /* CONFIG_P2P */
  479. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  480. struct wps_event_m2d *m2d)
  481. {
  482. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  483. "dev_password_id=%d config_error=%d",
  484. m2d->dev_password_id, m2d->config_error);
  485. wpas_notify_wps_event_m2d(wpa_s, m2d);
  486. #ifdef CONFIG_P2P
  487. if (wpa_s->parent && wpa_s->parent != wpa_s) {
  488. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
  489. "dev_password_id=%d config_error=%d",
  490. m2d->dev_password_id, m2d->config_error);
  491. }
  492. if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
  493. /*
  494. * Notify P2P from eloop timeout to avoid issues with the
  495. * interface getting removed while processing a message.
  496. */
  497. eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
  498. NULL);
  499. }
  500. #endif /* CONFIG_P2P */
  501. }
  502. static void wpas_wps_clear_timeout(void *eloop_ctx, void *timeout_ctx)
  503. {
  504. struct wpa_supplicant *wpa_s = eloop_ctx;
  505. wpa_printf(MSG_DEBUG, "WPS: Clear WPS network from timeout");
  506. wpas_clear_wps(wpa_s);
  507. }
  508. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  509. struct wps_event_fail *fail)
  510. {
  511. if (fail->error_indication > 0 &&
  512. fail->error_indication < NUM_WPS_EI_VALUES) {
  513. wpa_msg(wpa_s, MSG_INFO,
  514. WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
  515. fail->msg, fail->config_error, fail->error_indication,
  516. wps_ei_str(fail->error_indication));
  517. if (wpa_s->parent && wpa_s->parent != wpa_s)
  518. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  519. "msg=%d config_error=%d reason=%d (%s)",
  520. fail->msg, fail->config_error,
  521. fail->error_indication,
  522. wps_ei_str(fail->error_indication));
  523. } else {
  524. wpa_msg(wpa_s, MSG_INFO,
  525. WPS_EVENT_FAIL "msg=%d config_error=%d",
  526. fail->msg, fail->config_error);
  527. if (wpa_s->parent && wpa_s->parent != wpa_s)
  528. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  529. "msg=%d config_error=%d",
  530. fail->msg, fail->config_error);
  531. }
  532. /*
  533. * Need to allow WPS processing to complete, e.g., by sending WSC_NACK.
  534. */
  535. wpa_printf(MSG_DEBUG, "WPS: Register timeout to clear WPS network");
  536. eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
  537. eloop_register_timeout(0, 100000, wpas_wps_clear_timeout, wpa_s, NULL);
  538. wpas_notify_wps_event_fail(wpa_s, fail);
  539. #ifdef CONFIG_P2P
  540. wpas_p2p_wps_failed(wpa_s, fail);
  541. #endif /* CONFIG_P2P */
  542. }
  543. static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx);
  544. static void wpas_wps_reenable_networks(struct wpa_supplicant *wpa_s)
  545. {
  546. struct wpa_ssid *ssid;
  547. int changed = 0;
  548. eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
  549. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  550. if (ssid->disabled_for_connect && ssid->disabled) {
  551. ssid->disabled_for_connect = 0;
  552. ssid->disabled = 0;
  553. wpas_notify_network_enabled_changed(wpa_s, ssid);
  554. changed++;
  555. }
  556. }
  557. if (changed) {
  558. #ifndef CONFIG_NO_CONFIG_WRITE
  559. if (wpa_s->conf->update_config &&
  560. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  561. wpa_printf(MSG_DEBUG, "WPS: Failed to update "
  562. "configuration");
  563. }
  564. #endif /* CONFIG_NO_CONFIG_WRITE */
  565. }
  566. }
  567. static void wpas_wps_reenable_networks_cb(void *eloop_ctx, void *timeout_ctx)
  568. {
  569. struct wpa_supplicant *wpa_s = eloop_ctx;
  570. /* Enable the networks disabled during wpas_wps_reassoc */
  571. wpas_wps_reenable_networks(wpa_s);
  572. }
  573. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  574. {
  575. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  576. wpa_s->wps_success = 1;
  577. wpas_notify_wps_event_success(wpa_s);
  578. if (wpa_s->current_ssid)
  579. wpas_clear_temp_disabled(wpa_s, wpa_s->current_ssid, 1);
  580. wpa_s->extra_blacklist_count = 0;
  581. /*
  582. * Enable the networks disabled during wpas_wps_reassoc after 10
  583. * seconds. The 10 seconds timer is to allow the data connection to be
  584. * formed before allowing other networks to be selected.
  585. */
  586. eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
  587. NULL);
  588. #ifdef CONFIG_P2P
  589. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  590. #endif /* CONFIG_P2P */
  591. }
  592. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  593. struct wps_event_er_ap *ap)
  594. {
  595. char uuid_str[100];
  596. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  597. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  598. if (ap->pri_dev_type)
  599. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  600. sizeof(dev_type));
  601. else
  602. dev_type[0] = '\0';
  603. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  604. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  605. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  606. ap->friendly_name ? ap->friendly_name : "",
  607. ap->manufacturer ? ap->manufacturer : "",
  608. ap->model_description ? ap->model_description : "",
  609. ap->model_name ? ap->model_name : "",
  610. ap->manufacturer_url ? ap->manufacturer_url : "",
  611. ap->model_url ? ap->model_url : "");
  612. }
  613. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  614. struct wps_event_er_ap *ap)
  615. {
  616. char uuid_str[100];
  617. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  618. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  619. }
  620. static void wpa_supplicant_wps_event_er_enrollee_add(
  621. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  622. {
  623. char uuid_str[100];
  624. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  625. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  626. if (enrollee->pri_dev_type)
  627. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  628. sizeof(dev_type));
  629. else
  630. dev_type[0] = '\0';
  631. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  632. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  633. "|%s|%s|%s|%s|%s|",
  634. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  635. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  636. enrollee->dev_name ? enrollee->dev_name : "",
  637. enrollee->manufacturer ? enrollee->manufacturer : "",
  638. enrollee->model_name ? enrollee->model_name : "",
  639. enrollee->model_number ? enrollee->model_number : "",
  640. enrollee->serial_number ? enrollee->serial_number : "");
  641. }
  642. static void wpa_supplicant_wps_event_er_enrollee_remove(
  643. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  644. {
  645. char uuid_str[100];
  646. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  647. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  648. uuid_str, MAC2STR(enrollee->mac_addr));
  649. }
  650. static void wpa_supplicant_wps_event_er_ap_settings(
  651. struct wpa_supplicant *wpa_s,
  652. struct wps_event_er_ap_settings *ap_settings)
  653. {
  654. char uuid_str[100];
  655. char key_str[65];
  656. const struct wps_credential *cred = ap_settings->cred;
  657. key_str[0] = '\0';
  658. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  659. if (cred->key_len >= 8 && cred->key_len <= 64) {
  660. os_memcpy(key_str, cred->key, cred->key_len);
  661. key_str[cred->key_len] = '\0';
  662. }
  663. }
  664. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  665. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  666. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  667. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  668. "key=%s",
  669. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  670. cred->auth_type, cred->encr_type, key_str);
  671. }
  672. static void wpa_supplicant_wps_event_er_set_sel_reg(
  673. struct wpa_supplicant *wpa_s,
  674. struct wps_event_er_set_selected_registrar *ev)
  675. {
  676. char uuid_str[100];
  677. uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
  678. switch (ev->state) {
  679. case WPS_ER_SET_SEL_REG_START:
  680. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  681. "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
  682. "sel_reg_config_methods=0x%x",
  683. uuid_str, ev->sel_reg, ev->dev_passwd_id,
  684. ev->sel_reg_config_methods);
  685. break;
  686. case WPS_ER_SET_SEL_REG_DONE:
  687. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  688. "uuid=%s state=DONE", uuid_str);
  689. break;
  690. case WPS_ER_SET_SEL_REG_FAILED:
  691. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
  692. "uuid=%s state=FAILED", uuid_str);
  693. break;
  694. }
  695. }
  696. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  697. union wps_event_data *data)
  698. {
  699. struct wpa_supplicant *wpa_s = ctx;
  700. switch (event) {
  701. case WPS_EV_M2D:
  702. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  703. break;
  704. case WPS_EV_FAIL:
  705. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  706. break;
  707. case WPS_EV_SUCCESS:
  708. wpa_supplicant_wps_event_success(wpa_s);
  709. break;
  710. case WPS_EV_PWD_AUTH_FAIL:
  711. #ifdef CONFIG_AP
  712. if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
  713. wpa_supplicant_ap_pwd_auth_fail(wpa_s);
  714. #endif /* CONFIG_AP */
  715. break;
  716. case WPS_EV_PBC_OVERLAP:
  717. break;
  718. case WPS_EV_PBC_TIMEOUT:
  719. break;
  720. case WPS_EV_PBC_ACTIVE:
  721. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ACTIVE);
  722. break;
  723. case WPS_EV_PBC_DISABLE:
  724. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_DISABLE);
  725. break;
  726. case WPS_EV_ER_AP_ADD:
  727. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  728. break;
  729. case WPS_EV_ER_AP_REMOVE:
  730. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  731. break;
  732. case WPS_EV_ER_ENROLLEE_ADD:
  733. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  734. &data->enrollee);
  735. break;
  736. case WPS_EV_ER_ENROLLEE_REMOVE:
  737. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  738. &data->enrollee);
  739. break;
  740. case WPS_EV_ER_AP_SETTINGS:
  741. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  742. &data->ap_settings);
  743. break;
  744. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  745. wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
  746. &data->set_sel_reg);
  747. break;
  748. case WPS_EV_AP_PIN_SUCCESS:
  749. break;
  750. }
  751. }
  752. static int wpa_supplicant_wps_rf_band(void *ctx)
  753. {
  754. struct wpa_supplicant *wpa_s = ctx;
  755. if (!wpa_s->current_ssid || !wpa_s->assoc_freq)
  756. return 0;
  757. return (wpa_s->assoc_freq > 2484) ? WPS_RF_50GHZ : WPS_RF_24GHZ;
  758. }
  759. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  760. {
  761. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  762. eap_is_wps_pin_enrollee(&ssid->eap))
  763. return WPS_REQ_ENROLLEE;
  764. else
  765. return WPS_REQ_REGISTRAR;
  766. }
  767. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  768. {
  769. int id;
  770. struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
  771. wpa_s->after_wps = 0;
  772. wpa_s->known_wps_freq = 0;
  773. prev_current = wpa_s->current_ssid;
  774. /* Enable the networks disabled during wpas_wps_reassoc */
  775. wpas_wps_reenable_networks(wpa_s);
  776. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  777. /* Remove any existing WPS network from configuration */
  778. ssid = wpa_s->conf->ssid;
  779. while (ssid) {
  780. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  781. if (ssid == wpa_s->current_ssid) {
  782. wpa_supplicant_deauthenticate(
  783. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  784. }
  785. id = ssid->id;
  786. remove_ssid = ssid;
  787. } else
  788. id = -1;
  789. ssid = ssid->next;
  790. if (id >= 0) {
  791. if (prev_current == remove_ssid) {
  792. wpa_sm_set_config(wpa_s->wpa, NULL);
  793. eapol_sm_notify_config(wpa_s->eapol, NULL,
  794. NULL);
  795. }
  796. wpas_notify_network_removed(wpa_s, remove_ssid);
  797. wpa_config_remove_network(wpa_s->conf, id);
  798. }
  799. }
  800. wpas_wps_clear_ap_info(wpa_s);
  801. }
  802. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  803. {
  804. struct wpa_supplicant *wpa_s = eloop_ctx;
  805. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  806. "out");
  807. wpas_clear_wps(wpa_s);
  808. }
  809. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  810. int registrar, const u8 *bssid)
  811. {
  812. struct wpa_ssid *ssid;
  813. ssid = wpa_config_add_network(wpa_s->conf);
  814. if (ssid == NULL)
  815. return NULL;
  816. wpas_notify_network_added(wpa_s, ssid);
  817. wpa_config_set_network_defaults(ssid);
  818. ssid->temporary = 1;
  819. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  820. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  821. wpa_config_set(ssid, "identity", registrar ?
  822. "\"" WSC_ID_REGISTRAR "\"" :
  823. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  824. wpas_notify_network_removed(wpa_s, ssid);
  825. wpa_config_remove_network(wpa_s->conf, ssid->id);
  826. return NULL;
  827. }
  828. if (bssid) {
  829. #ifndef CONFIG_P2P
  830. struct wpa_bss *bss;
  831. int count = 0;
  832. #endif /* CONFIG_P2P */
  833. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  834. ssid->bssid_set = 1;
  835. /*
  836. * Note: With P2P, the SSID may change at the time the WPS
  837. * provisioning is started, so better not filter the AP based
  838. * on the current SSID in the scan results.
  839. */
  840. #ifndef CONFIG_P2P
  841. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  842. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  843. continue;
  844. os_free(ssid->ssid);
  845. ssid->ssid = os_malloc(bss->ssid_len);
  846. if (ssid->ssid == NULL)
  847. break;
  848. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  849. ssid->ssid_len = bss->ssid_len;
  850. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  851. "scan results",
  852. ssid->ssid, ssid->ssid_len);
  853. count++;
  854. }
  855. if (count > 1) {
  856. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  857. "for the AP; use wildcard");
  858. os_free(ssid->ssid);
  859. ssid->ssid = NULL;
  860. ssid->ssid_len = 0;
  861. }
  862. #endif /* CONFIG_P2P */
  863. }
  864. return ssid;
  865. }
  866. static void wpas_wps_temp_disable(struct wpa_supplicant *wpa_s,
  867. struct wpa_ssid *selected)
  868. {
  869. struct wpa_ssid *ssid;
  870. if (wpa_s->current_ssid)
  871. wpa_supplicant_deauthenticate(
  872. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  873. /* Mark all other networks disabled and trigger reassociation */
  874. ssid = wpa_s->conf->ssid;
  875. while (ssid) {
  876. int was_disabled = ssid->disabled;
  877. ssid->disabled_for_connect = 0;
  878. /*
  879. * In case the network object corresponds to a persistent group
  880. * then do not send out network disabled signal. In addition,
  881. * do not change disabled status of persistent network objects
  882. * from 2 to 1 should we connect to another network.
  883. */
  884. if (was_disabled != 2) {
  885. ssid->disabled = ssid != selected;
  886. if (was_disabled != ssid->disabled) {
  887. if (ssid->disabled)
  888. ssid->disabled_for_connect = 1;
  889. wpas_notify_network_enabled_changed(wpa_s,
  890. ssid);
  891. }
  892. }
  893. ssid = ssid->next;
  894. }
  895. }
  896. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  897. struct wpa_ssid *selected, const u8 *bssid)
  898. {
  899. struct wpa_bss *bss;
  900. wpa_s->after_wps = 0;
  901. wpa_s->known_wps_freq = 0;
  902. if (bssid) {
  903. bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
  904. if (bss && bss->freq > 0) {
  905. wpa_s->known_wps_freq = 1;
  906. wpa_s->wps_freq = bss->freq;
  907. }
  908. }
  909. wpas_wps_temp_disable(wpa_s, selected);
  910. wpa_s->disconnected = 0;
  911. wpa_s->reassociate = 1;
  912. wpa_s->scan_runs = 0;
  913. wpa_s->normal_scans = 0;
  914. wpa_s->wps_success = 0;
  915. wpa_s->blacklist_cleared = 0;
  916. wpa_supplicant_req_scan(wpa_s, 0, 0);
  917. }
  918. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  919. int p2p_group)
  920. {
  921. struct wpa_ssid *ssid;
  922. wpas_clear_wps(wpa_s);
  923. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  924. if (ssid == NULL)
  925. return -1;
  926. ssid->temporary = 1;
  927. ssid->p2p_group = p2p_group;
  928. #ifdef CONFIG_P2P
  929. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  930. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  931. if (ssid->ssid) {
  932. ssid->ssid_len = wpa_s->go_params->ssid_len;
  933. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  934. ssid->ssid_len);
  935. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  936. "SSID", ssid->ssid, ssid->ssid_len);
  937. }
  938. }
  939. #endif /* CONFIG_P2P */
  940. if (wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0) < 0)
  941. return -1;
  942. if (wpa_s->wps_fragment_size)
  943. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  944. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  945. wpa_s, NULL);
  946. wpas_wps_reassoc(wpa_s, ssid, bssid);
  947. return 0;
  948. }
  949. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  950. const char *pin, int p2p_group, u16 dev_pw_id)
  951. {
  952. struct wpa_ssid *ssid;
  953. char val[128];
  954. unsigned int rpin = 0;
  955. wpas_clear_wps(wpa_s);
  956. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  957. if (ssid == NULL)
  958. return -1;
  959. ssid->temporary = 1;
  960. ssid->p2p_group = p2p_group;
  961. #ifdef CONFIG_P2P
  962. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  963. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  964. if (ssid->ssid) {
  965. ssid->ssid_len = wpa_s->go_params->ssid_len;
  966. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  967. ssid->ssid_len);
  968. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  969. "SSID", ssid->ssid, ssid->ssid_len);
  970. }
  971. }
  972. #endif /* CONFIG_P2P */
  973. if (pin)
  974. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
  975. pin, dev_pw_id);
  976. else {
  977. rpin = wps_generate_pin();
  978. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
  979. rpin, dev_pw_id);
  980. }
  981. if (wpa_config_set(ssid, "phase1", val, 0) < 0)
  982. return -1;
  983. if (wpa_s->wps_fragment_size)
  984. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  985. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  986. wpa_s, NULL);
  987. wpa_s->wps_ap_iter = 1;
  988. wpas_wps_reassoc(wpa_s, ssid, bssid);
  989. return rpin;
  990. }
  991. /* Cancel the wps pbc/pin requests */
  992. int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
  993. {
  994. #ifdef CONFIG_AP
  995. if (wpa_s->ap_iface) {
  996. wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
  997. return wpa_supplicant_ap_wps_cancel(wpa_s);
  998. }
  999. #endif /* CONFIG_AP */
  1000. if (wpa_s->wpa_state == WPA_SCANNING ||
  1001. wpa_s->wpa_state == WPA_DISCONNECTED) {
  1002. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
  1003. wpa_supplicant_cancel_scan(wpa_s);
  1004. wpas_clear_wps(wpa_s);
  1005. } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1006. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
  1007. "deauthenticate");
  1008. wpa_supplicant_deauthenticate(wpa_s,
  1009. WLAN_REASON_DEAUTH_LEAVING);
  1010. wpas_clear_wps(wpa_s);
  1011. } else {
  1012. wpas_wps_reenable_networks(wpa_s);
  1013. wpas_wps_clear_ap_info(wpa_s);
  1014. }
  1015. wpa_s->after_wps = 0;
  1016. return 0;
  1017. }
  1018. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  1019. const char *pin, struct wps_new_ap_settings *settings)
  1020. {
  1021. struct wpa_ssid *ssid;
  1022. char val[200];
  1023. char *pos, *end;
  1024. int res;
  1025. if (!pin)
  1026. return -1;
  1027. wpas_clear_wps(wpa_s);
  1028. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  1029. if (ssid == NULL)
  1030. return -1;
  1031. ssid->temporary = 1;
  1032. pos = val;
  1033. end = pos + sizeof(val);
  1034. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  1035. if (res < 0 || res >= end - pos)
  1036. return -1;
  1037. pos += res;
  1038. if (settings) {
  1039. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  1040. "new_encr=%s new_key=%s",
  1041. settings->ssid_hex, settings->auth,
  1042. settings->encr, settings->key_hex);
  1043. if (res < 0 || res >= end - pos)
  1044. return -1;
  1045. pos += res;
  1046. }
  1047. res = os_snprintf(pos, end - pos, "\"");
  1048. if (res < 0 || res >= end - pos)
  1049. return -1;
  1050. if (wpa_config_set(ssid, "phase1", val, 0) < 0)
  1051. return -1;
  1052. if (wpa_s->wps_fragment_size)
  1053. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  1054. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  1055. wpa_s, NULL);
  1056. wpas_wps_reassoc(wpa_s, ssid, bssid);
  1057. return 0;
  1058. }
  1059. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr,
  1060. const u8 *p2p_dev_addr, const u8 *psk,
  1061. size_t psk_len)
  1062. {
  1063. if (is_zero_ether_addr(p2p_dev_addr)) {
  1064. wpa_printf(MSG_DEBUG,
  1065. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR,
  1066. MAC2STR(mac_addr));
  1067. } else {
  1068. wpa_printf(MSG_DEBUG,
  1069. "Received new WPA/WPA2-PSK from WPS for STA " MACSTR
  1070. " P2P Device Addr " MACSTR,
  1071. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  1072. }
  1073. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  1074. /* TODO */
  1075. return 0;
  1076. }
  1077. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  1078. const struct wps_device_data *dev)
  1079. {
  1080. char uuid[40], txt[400];
  1081. int len;
  1082. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1083. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  1084. return;
  1085. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  1086. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  1087. " [%s|%s|%s|%s|%s|%s]",
  1088. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  1089. dev->manufacturer, dev->model_name,
  1090. dev->model_number, dev->serial_number,
  1091. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  1092. sizeof(devtype)));
  1093. if (len > 0 && len < (int) sizeof(txt))
  1094. wpa_printf(MSG_INFO, "%s", txt);
  1095. }
  1096. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  1097. u16 sel_reg_config_methods)
  1098. {
  1099. #ifdef CONFIG_WPS_ER
  1100. struct wpa_supplicant *wpa_s = ctx;
  1101. if (wpa_s->wps_er == NULL)
  1102. return;
  1103. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  1104. "dev_password_id=%u sel_reg_config_methods=0x%x",
  1105. sel_reg, dev_passwd_id, sel_reg_config_methods);
  1106. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  1107. sel_reg_config_methods);
  1108. #endif /* CONFIG_WPS_ER */
  1109. }
  1110. static u16 wps_fix_config_methods(u16 config_methods)
  1111. {
  1112. #ifdef CONFIG_WPS2
  1113. if ((config_methods &
  1114. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  1115. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  1116. wpa_printf(MSG_INFO, "WPS: Converting display to "
  1117. "virtual_display for WPS 2.0 compliance");
  1118. config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  1119. }
  1120. if ((config_methods &
  1121. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  1122. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  1123. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  1124. "virtual_push_button for WPS 2.0 compliance");
  1125. config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  1126. }
  1127. #endif /* CONFIG_WPS2 */
  1128. return config_methods;
  1129. }
  1130. static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
  1131. struct wps_context *wps)
  1132. {
  1133. wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
  1134. if (is_nil_uuid(wpa_s->conf->uuid)) {
  1135. struct wpa_supplicant *first;
  1136. first = wpa_s->global->ifaces;
  1137. while (first && first->next)
  1138. first = first->next;
  1139. if (first && first != wpa_s) {
  1140. if (wps != wpa_s->global->ifaces->wps)
  1141. os_memcpy(wps->uuid,
  1142. wpa_s->global->ifaces->wps->uuid,
  1143. WPS_UUID_LEN);
  1144. wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
  1145. "interface", wps->uuid, WPS_UUID_LEN);
  1146. } else {
  1147. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  1148. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  1149. "address", wps->uuid, WPS_UUID_LEN);
  1150. }
  1151. } else {
  1152. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  1153. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
  1154. wps->uuid, WPS_UUID_LEN);
  1155. }
  1156. }
  1157. static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
  1158. struct wps_context *wps)
  1159. {
  1160. wpabuf_free(wps->dev.vendor_ext_m1);
  1161. wps->dev.vendor_ext_m1 = NULL;
  1162. if (wpa_s->conf->wps_vendor_ext_m1) {
  1163. wps->dev.vendor_ext_m1 =
  1164. wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
  1165. if (!wps->dev.vendor_ext_m1) {
  1166. wpa_printf(MSG_ERROR, "WPS: Cannot "
  1167. "allocate memory for vendor_ext_m1");
  1168. }
  1169. }
  1170. }
  1171. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  1172. {
  1173. struct wps_context *wps;
  1174. struct wps_registrar_config rcfg;
  1175. struct hostapd_hw_modes *modes;
  1176. u16 m;
  1177. wps = os_zalloc(sizeof(*wps));
  1178. if (wps == NULL)
  1179. return -1;
  1180. wps->cred_cb = wpa_supplicant_wps_cred;
  1181. wps->event_cb = wpa_supplicant_wps_event;
  1182. wps->rf_band_cb = wpa_supplicant_wps_rf_band;
  1183. wps->cb_ctx = wpa_s;
  1184. wps->dev.device_name = wpa_s->conf->device_name;
  1185. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1186. wps->dev.model_name = wpa_s->conf->model_name;
  1187. wps->dev.model_number = wpa_s->conf->model_number;
  1188. wps->dev.serial_number = wpa_s->conf->serial_number;
  1189. wps->config_methods =
  1190. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  1191. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1192. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1193. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  1194. "methods are not allowed at the same time");
  1195. os_free(wps);
  1196. return -1;
  1197. }
  1198. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1199. wps->dev.config_methods = wps->config_methods;
  1200. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1201. WPS_DEV_TYPE_LEN);
  1202. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1203. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1204. WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
  1205. wpas_wps_set_vendor_ext_m1(wpa_s, wps);
  1206. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1207. modes = wpa_s->hw.modes;
  1208. if (modes) {
  1209. for (m = 0; m < wpa_s->hw.num_modes; m++) {
  1210. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
  1211. modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  1212. wps->dev.rf_bands |= WPS_RF_24GHZ;
  1213. else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
  1214. wps->dev.rf_bands |= WPS_RF_50GHZ;
  1215. }
  1216. }
  1217. if (wps->dev.rf_bands == 0) {
  1218. /*
  1219. * Default to claiming support for both bands if the driver
  1220. * does not provide support for fetching supported bands.
  1221. */
  1222. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
  1223. }
  1224. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  1225. wpas_wps_set_uuid(wpa_s, wps);
  1226. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  1227. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  1228. os_memset(&rcfg, 0, sizeof(rcfg));
  1229. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  1230. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  1231. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  1232. rcfg.cb_ctx = wpa_s;
  1233. wps->registrar = wps_registrar_init(wps, &rcfg);
  1234. if (wps->registrar == NULL) {
  1235. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  1236. os_free(wps);
  1237. return -1;
  1238. }
  1239. wpa_s->wps = wps;
  1240. return 0;
  1241. }
  1242. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  1243. {
  1244. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  1245. eloop_cancel_timeout(wpas_wps_clear_timeout, wpa_s, NULL);
  1246. eloop_cancel_timeout(wpas_wps_reenable_networks_cb, wpa_s, NULL);
  1247. wpas_wps_clear_ap_info(wpa_s);
  1248. if (wpa_s->wps == NULL)
  1249. return;
  1250. #ifdef CONFIG_WPS_ER
  1251. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1252. wpa_s->wps_er = NULL;
  1253. #endif /* CONFIG_WPS_ER */
  1254. wps_registrar_deinit(wpa_s->wps->registrar);
  1255. wpabuf_free(wpa_s->wps->dh_pubkey);
  1256. wpabuf_free(wpa_s->wps->dh_privkey);
  1257. wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
  1258. os_free(wpa_s->wps->network_key);
  1259. os_free(wpa_s->wps);
  1260. wpa_s->wps = NULL;
  1261. }
  1262. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  1263. struct wpa_ssid *ssid, struct wpa_bss *bss)
  1264. {
  1265. struct wpabuf *wps_ie;
  1266. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  1267. return -1;
  1268. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1269. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1270. if (!wps_ie) {
  1271. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1272. return 0;
  1273. }
  1274. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  1275. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1276. "without active PBC Registrar");
  1277. wpabuf_free(wps_ie);
  1278. return 0;
  1279. }
  1280. /* TODO: overlap detection */
  1281. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1282. "(Active PBC)");
  1283. wpabuf_free(wps_ie);
  1284. return 1;
  1285. }
  1286. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1287. if (!wps_ie) {
  1288. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1289. return 0;
  1290. }
  1291. /*
  1292. * Start with WPS APs that advertise our address as an
  1293. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  1294. * allow any WPS AP after couple of scans since some APs do not
  1295. * set Selected Registrar attribute properly when using
  1296. * external Registrar.
  1297. */
  1298. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  1299. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  1300. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1301. "without active PIN Registrar");
  1302. wpabuf_free(wps_ie);
  1303. return 0;
  1304. }
  1305. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1306. } else {
  1307. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1308. "(Authorized MAC or Active PIN)");
  1309. }
  1310. wpabuf_free(wps_ie);
  1311. return 1;
  1312. }
  1313. if (wps_ie) {
  1314. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1315. wpabuf_free(wps_ie);
  1316. return 1;
  1317. }
  1318. return -1;
  1319. }
  1320. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  1321. struct wpa_ssid *ssid,
  1322. struct wpa_bss *bss)
  1323. {
  1324. struct wpabuf *wps_ie = NULL;
  1325. int ret = 0;
  1326. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1327. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1328. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  1329. /* allow wildcard SSID for WPS PBC */
  1330. ret = 1;
  1331. }
  1332. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1333. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1334. if (wps_ie &&
  1335. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  1336. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  1337. /* allow wildcard SSID for WPS PIN */
  1338. ret = 1;
  1339. }
  1340. }
  1341. if (!ret && ssid->bssid_set &&
  1342. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  1343. /* allow wildcard SSID due to hardcoded BSSID match */
  1344. ret = 1;
  1345. }
  1346. #ifdef CONFIG_WPS_STRICT
  1347. if (wps_ie) {
  1348. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  1349. 0, bss->bssid) < 0)
  1350. ret = 0;
  1351. if (bss->beacon_ie_len) {
  1352. struct wpabuf *bcn_wps;
  1353. bcn_wps = wpa_bss_get_vendor_ie_multi_beacon(
  1354. bss, WPS_IE_VENDOR_TYPE);
  1355. if (bcn_wps == NULL) {
  1356. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  1357. "missing from AP Beacon");
  1358. ret = 0;
  1359. } else {
  1360. if (wps_validate_beacon(wps_ie) < 0)
  1361. ret = 0;
  1362. wpabuf_free(bcn_wps);
  1363. }
  1364. }
  1365. }
  1366. #endif /* CONFIG_WPS_STRICT */
  1367. wpabuf_free(wps_ie);
  1368. return ret;
  1369. }
  1370. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  1371. struct wpa_bss *selected, struct wpa_ssid *ssid)
  1372. {
  1373. const u8 *sel_uuid, *uuid;
  1374. struct wpabuf *wps_ie;
  1375. int ret = 0;
  1376. struct wpa_bss *bss;
  1377. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  1378. return 0;
  1379. wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
  1380. "present in scan results; selected BSSID " MACSTR,
  1381. MAC2STR(selected->bssid));
  1382. /* Make sure that only one AP is in active PBC mode */
  1383. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  1384. if (wps_ie) {
  1385. sel_uuid = wps_get_uuid_e(wps_ie);
  1386. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
  1387. sel_uuid, UUID_LEN);
  1388. } else {
  1389. wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
  1390. "WPS IE?!");
  1391. sel_uuid = NULL;
  1392. }
  1393. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1394. struct wpabuf *ie;
  1395. if (bss == selected)
  1396. continue;
  1397. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1398. if (!ie)
  1399. continue;
  1400. if (!wps_is_selected_pbc_registrar(ie)) {
  1401. wpabuf_free(ie);
  1402. continue;
  1403. }
  1404. wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
  1405. MACSTR, MAC2STR(bss->bssid));
  1406. uuid = wps_get_uuid_e(ie);
  1407. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
  1408. uuid, UUID_LEN);
  1409. if (sel_uuid == NULL || uuid == NULL ||
  1410. os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
  1411. ret = 1; /* PBC overlap */
  1412. wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
  1413. MACSTR " and " MACSTR,
  1414. MAC2STR(selected->bssid),
  1415. MAC2STR(bss->bssid));
  1416. wpabuf_free(ie);
  1417. break;
  1418. }
  1419. /* TODO: verify that this is reasonable dual-band situation */
  1420. wpabuf_free(ie);
  1421. }
  1422. wpabuf_free(wps_ie);
  1423. return ret;
  1424. }
  1425. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1426. {
  1427. struct wpa_bss *bss;
  1428. unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
  1429. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1430. return;
  1431. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1432. struct wpabuf *ie;
  1433. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1434. if (!ie)
  1435. continue;
  1436. if (wps_is_selected_pbc_registrar(ie))
  1437. pbc++;
  1438. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1439. auth++;
  1440. else if (wps_is_selected_pin_registrar(ie))
  1441. pin++;
  1442. else
  1443. wps++;
  1444. wpabuf_free(ie);
  1445. }
  1446. if (pbc)
  1447. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  1448. else if (auth)
  1449. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
  1450. else if (pin)
  1451. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  1452. else if (wps)
  1453. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  1454. }
  1455. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1456. {
  1457. struct wpa_ssid *ssid;
  1458. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1459. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1460. return 1;
  1461. }
  1462. return 0;
  1463. }
  1464. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1465. char *end)
  1466. {
  1467. struct wpabuf *wps_ie;
  1468. int ret;
  1469. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1470. if (wps_ie == NULL)
  1471. return 0;
  1472. ret = wps_attr_text(wps_ie, buf, end);
  1473. wpabuf_free(wps_ie);
  1474. return ret;
  1475. }
  1476. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1477. {
  1478. #ifdef CONFIG_WPS_ER
  1479. if (wpa_s->wps_er) {
  1480. wps_er_refresh(wpa_s->wps_er);
  1481. return 0;
  1482. }
  1483. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1484. if (wpa_s->wps_er == NULL)
  1485. return -1;
  1486. return 0;
  1487. #else /* CONFIG_WPS_ER */
  1488. return 0;
  1489. #endif /* CONFIG_WPS_ER */
  1490. }
  1491. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1492. {
  1493. #ifdef CONFIG_WPS_ER
  1494. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1495. wpa_s->wps_er = NULL;
  1496. #endif /* CONFIG_WPS_ER */
  1497. return 0;
  1498. }
  1499. #ifdef CONFIG_WPS_ER
  1500. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1501. const char *uuid, const char *pin)
  1502. {
  1503. u8 u[UUID_LEN];
  1504. const u8 *use_uuid = NULL;
  1505. u8 addr_buf[ETH_ALEN];
  1506. if (os_strcmp(uuid, "any") == 0) {
  1507. } else if (uuid_str2bin(uuid, u) == 0) {
  1508. use_uuid = u;
  1509. } else if (hwaddr_aton(uuid, addr_buf) == 0) {
  1510. use_uuid = wps_er_get_sta_uuid(wpa_s->wps_er, addr_buf);
  1511. if (use_uuid == NULL)
  1512. return -1;
  1513. } else
  1514. return -1;
  1515. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1516. use_uuid,
  1517. (const u8 *) pin, os_strlen(pin), 300);
  1518. }
  1519. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1520. {
  1521. u8 u[UUID_LEN], *use_uuid = NULL;
  1522. u8 addr[ETH_ALEN], *use_addr = NULL;
  1523. if (uuid_str2bin(uuid, u) == 0)
  1524. use_uuid = u;
  1525. else if (hwaddr_aton(uuid, addr) == 0)
  1526. use_addr = addr;
  1527. else
  1528. return -1;
  1529. return wps_er_pbc(wpa_s->wps_er, use_uuid, use_addr);
  1530. }
  1531. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1532. const char *pin)
  1533. {
  1534. u8 u[UUID_LEN], *use_uuid = NULL;
  1535. u8 addr[ETH_ALEN], *use_addr = NULL;
  1536. if (uuid_str2bin(uuid, u) == 0)
  1537. use_uuid = u;
  1538. else if (hwaddr_aton(uuid, addr) == 0)
  1539. use_addr = addr;
  1540. else
  1541. return -1;
  1542. return wps_er_learn(wpa_s->wps_er, use_uuid, use_addr, (const u8 *) pin,
  1543. os_strlen(pin));
  1544. }
  1545. static int wpas_wps_network_to_cred(struct wpa_ssid *ssid,
  1546. struct wps_credential *cred)
  1547. {
  1548. os_memset(cred, 0, sizeof(*cred));
  1549. if (ssid->ssid_len > 32)
  1550. return -1;
  1551. os_memcpy(cred->ssid, ssid->ssid, ssid->ssid_len);
  1552. cred->ssid_len = ssid->ssid_len;
  1553. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  1554. cred->auth_type = (ssid->proto & WPA_PROTO_RSN) ?
  1555. WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
  1556. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  1557. cred->encr_type = WPS_ENCR_AES;
  1558. else
  1559. cred->encr_type = WPS_ENCR_TKIP;
  1560. if (ssid->passphrase) {
  1561. cred->key_len = os_strlen(ssid->passphrase);
  1562. if (cred->key_len >= 64)
  1563. return -1;
  1564. os_memcpy(cred->key, ssid->passphrase, cred->key_len);
  1565. } else if (ssid->psk_set) {
  1566. cred->key_len = 32;
  1567. os_memcpy(cred->key, ssid->psk, 32);
  1568. } else
  1569. return -1;
  1570. } else {
  1571. cred->auth_type = WPS_AUTH_OPEN;
  1572. cred->encr_type = WPS_ENCR_NONE;
  1573. }
  1574. return 0;
  1575. }
  1576. int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1577. int id)
  1578. {
  1579. u8 u[UUID_LEN], *use_uuid = NULL;
  1580. u8 addr[ETH_ALEN], *use_addr = NULL;
  1581. struct wpa_ssid *ssid;
  1582. struct wps_credential cred;
  1583. if (uuid_str2bin(uuid, u) == 0)
  1584. use_uuid = u;
  1585. else if (hwaddr_aton(uuid, addr) == 0)
  1586. use_addr = addr;
  1587. else
  1588. return -1;
  1589. ssid = wpa_config_get_network(wpa_s->conf, id);
  1590. if (ssid == NULL || ssid->ssid == NULL)
  1591. return -1;
  1592. if (wpas_wps_network_to_cred(ssid, &cred) < 0)
  1593. return -1;
  1594. return wps_er_set_config(wpa_s->wps_er, use_uuid, use_addr, &cred);
  1595. }
  1596. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1597. const char *pin, struct wps_new_ap_settings *settings)
  1598. {
  1599. u8 u[UUID_LEN], *use_uuid = NULL;
  1600. u8 addr[ETH_ALEN], *use_addr = NULL;
  1601. struct wps_credential cred;
  1602. size_t len;
  1603. if (uuid_str2bin(uuid, u) == 0)
  1604. use_uuid = u;
  1605. else if (hwaddr_aton(uuid, addr) == 0)
  1606. use_addr = addr;
  1607. else
  1608. return -1;
  1609. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1610. settings->encr == NULL || settings->key_hex == NULL)
  1611. return -1;
  1612. os_memset(&cred, 0, sizeof(cred));
  1613. len = os_strlen(settings->ssid_hex);
  1614. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1615. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1616. return -1;
  1617. cred.ssid_len = len / 2;
  1618. len = os_strlen(settings->key_hex);
  1619. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1620. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1621. return -1;
  1622. cred.key_len = len / 2;
  1623. if (os_strcmp(settings->auth, "OPEN") == 0)
  1624. cred.auth_type = WPS_AUTH_OPEN;
  1625. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1626. cred.auth_type = WPS_AUTH_WPAPSK;
  1627. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1628. cred.auth_type = WPS_AUTH_WPA2PSK;
  1629. else
  1630. return -1;
  1631. if (os_strcmp(settings->encr, "NONE") == 0)
  1632. cred.encr_type = WPS_ENCR_NONE;
  1633. else if (os_strcmp(settings->encr, "WEP") == 0)
  1634. cred.encr_type = WPS_ENCR_WEP;
  1635. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1636. cred.encr_type = WPS_ENCR_TKIP;
  1637. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1638. cred.encr_type = WPS_ENCR_AES;
  1639. else
  1640. return -1;
  1641. return wps_er_config(wpa_s->wps_er, use_uuid, use_addr,
  1642. (const u8 *) pin, os_strlen(pin), &cred);
  1643. }
  1644. #ifdef CONFIG_WPS_NFC
  1645. struct wpabuf * wpas_wps_er_nfc_config_token(struct wpa_supplicant *wpa_s,
  1646. int ndef, const char *uuid)
  1647. {
  1648. struct wpabuf *ret;
  1649. u8 u[UUID_LEN], *use_uuid = NULL;
  1650. u8 addr[ETH_ALEN], *use_addr = NULL;
  1651. if (!wpa_s->wps_er)
  1652. return NULL;
  1653. if (uuid_str2bin(uuid, u) == 0)
  1654. use_uuid = u;
  1655. else if (hwaddr_aton(uuid, addr) == 0)
  1656. use_addr = addr;
  1657. else
  1658. return NULL;
  1659. ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
  1660. if (ndef && ret) {
  1661. struct wpabuf *tmp;
  1662. tmp = ndef_build_wifi(ret);
  1663. wpabuf_free(ret);
  1664. if (tmp == NULL)
  1665. return NULL;
  1666. ret = tmp;
  1667. }
  1668. return ret;
  1669. }
  1670. #endif /* CONFIG_WPS_NFC */
  1671. static int callbacks_pending = 0;
  1672. static void wpas_wps_terminate_cb(void *ctx)
  1673. {
  1674. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1675. if (--callbacks_pending <= 0)
  1676. eloop_terminate();
  1677. }
  1678. #endif /* CONFIG_WPS_ER */
  1679. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1680. {
  1681. #ifdef CONFIG_WPS_ER
  1682. if (wpa_s->wps_er) {
  1683. callbacks_pending++;
  1684. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1685. wpa_s->wps_er = NULL;
  1686. return 1;
  1687. }
  1688. #endif /* CONFIG_WPS_ER */
  1689. return 0;
  1690. }
  1691. int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
  1692. {
  1693. struct wpa_ssid *ssid;
  1694. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1695. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  1696. return 1;
  1697. }
  1698. return 0;
  1699. }
  1700. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1701. {
  1702. struct wps_context *wps = wpa_s->wps;
  1703. if (wps == NULL)
  1704. return;
  1705. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1706. wps->config_methods = wps_config_methods_str2bin(
  1707. wpa_s->conf->config_methods);
  1708. if ((wps->config_methods &
  1709. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1710. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1711. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1712. "config methods are not allowed at the "
  1713. "same time");
  1714. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1715. }
  1716. }
  1717. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1718. wps->dev.config_methods = wps->config_methods;
  1719. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  1720. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1721. WPS_DEV_TYPE_LEN);
  1722. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  1723. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1724. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1725. wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1726. }
  1727. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
  1728. wpas_wps_set_vendor_ext_m1(wpa_s, wps);
  1729. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1730. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1731. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
  1732. wpas_wps_set_uuid(wpa_s, wps);
  1733. if (wpa_s->conf->changed_parameters &
  1734. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1735. /* Update pointers to make sure they refer current values */
  1736. wps->dev.device_name = wpa_s->conf->device_name;
  1737. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1738. wps->dev.model_name = wpa_s->conf->model_name;
  1739. wps->dev.model_number = wpa_s->conf->model_number;
  1740. wps->dev.serial_number = wpa_s->conf->serial_number;
  1741. }
  1742. }
  1743. #ifdef CONFIG_WPS_NFC
  1744. #ifdef CONFIG_WPS_ER
  1745. static struct wpabuf *
  1746. wpas_wps_network_config_token(struct wpa_supplicant *wpa_s, int ndef,
  1747. struct wpa_ssid *ssid)
  1748. {
  1749. struct wpabuf *ret;
  1750. struct wps_credential cred;
  1751. if (wpas_wps_network_to_cred(ssid, &cred) < 0)
  1752. return NULL;
  1753. ret = wps_er_config_token_from_cred(wpa_s->wps, &cred);
  1754. if (ndef && ret) {
  1755. struct wpabuf *tmp;
  1756. tmp = ndef_build_wifi(ret);
  1757. wpabuf_free(ret);
  1758. if (tmp == NULL)
  1759. return NULL;
  1760. ret = tmp;
  1761. }
  1762. return ret;
  1763. }
  1764. #endif /* CONFIG_WPS_ER */
  1765. struct wpabuf * wpas_wps_nfc_config_token(struct wpa_supplicant *wpa_s,
  1766. int ndef, const char *id_str)
  1767. {
  1768. #ifdef CONFIG_WPS_ER
  1769. if (id_str) {
  1770. int id;
  1771. char *end = NULL;
  1772. struct wpa_ssid *ssid;
  1773. id = strtol(id_str, &end, 10);
  1774. if (end && *end)
  1775. return NULL;
  1776. ssid = wpa_config_get_network(wpa_s->conf, id);
  1777. if (ssid == NULL)
  1778. return NULL;
  1779. return wpas_wps_network_config_token(wpa_s, ndef, ssid);
  1780. }
  1781. #endif /* CONFIG_WPS_ER */
  1782. #ifdef CONFIG_AP
  1783. if (wpa_s->ap_iface)
  1784. return wpas_ap_wps_nfc_config_token(wpa_s, ndef);
  1785. #endif /* CONFIG_AP */
  1786. return NULL;
  1787. }
  1788. struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
  1789. {
  1790. if (wpa_s->conf->wps_nfc_pw_from_config) {
  1791. return wps_nfc_token_build(ndef,
  1792. wpa_s->conf->wps_nfc_dev_pw_id,
  1793. wpa_s->conf->wps_nfc_dh_pubkey,
  1794. wpa_s->conf->wps_nfc_dev_pw);
  1795. }
  1796. return wps_nfc_token_gen(ndef, &wpa_s->conf->wps_nfc_dev_pw_id,
  1797. &wpa_s->conf->wps_nfc_dh_pubkey,
  1798. &wpa_s->conf->wps_nfc_dh_privkey,
  1799. &wpa_s->conf->wps_nfc_dev_pw);
  1800. }
  1801. int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  1802. {
  1803. struct wps_context *wps = wpa_s->wps;
  1804. char pw[32 * 2 + 1];
  1805. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  1806. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  1807. wpa_s->conf->wps_nfc_dev_pw == NULL)
  1808. return -1;
  1809. dh5_free(wps->dh_ctx);
  1810. wpabuf_free(wps->dh_pubkey);
  1811. wpabuf_free(wps->dh_privkey);
  1812. wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  1813. wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  1814. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  1815. wps->dh_ctx = NULL;
  1816. wpabuf_free(wps->dh_pubkey);
  1817. wps->dh_pubkey = NULL;
  1818. wpabuf_free(wps->dh_privkey);
  1819. wps->dh_privkey = NULL;
  1820. return -1;
  1821. }
  1822. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  1823. if (wps->dh_ctx == NULL) {
  1824. wpabuf_free(wps->dh_pubkey);
  1825. wps->dh_pubkey = NULL;
  1826. wpabuf_free(wps->dh_privkey);
  1827. wps->dh_privkey = NULL;
  1828. return -1;
  1829. }
  1830. wpa_snprintf_hex_uppercase(pw, sizeof(pw),
  1831. wpabuf_head(wpa_s->conf->wps_nfc_dev_pw),
  1832. wpabuf_len(wpa_s->conf->wps_nfc_dev_pw));
  1833. return wpas_wps_start_pin(wpa_s, bssid, pw, 0,
  1834. wpa_s->conf->wps_nfc_dev_pw_id);
  1835. }
  1836. static int wpas_wps_use_cred(struct wpa_supplicant *wpa_s,
  1837. struct wps_parse_attr *attr)
  1838. {
  1839. wpa_s->wps_ap_channel = 0;
  1840. /*
  1841. * Disable existing networks temporarily to allow the newly learned
  1842. * credential to be preferred. Enable the temporarily disabled networks
  1843. * after 10 seconds.
  1844. */
  1845. wpas_wps_temp_disable(wpa_s, NULL);
  1846. eloop_register_timeout(10, 0, wpas_wps_reenable_networks_cb, wpa_s,
  1847. NULL);
  1848. if (wps_oob_use_cred(wpa_s->wps, attr) < 0)
  1849. return -1;
  1850. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1851. return 0;
  1852. wpa_printf(MSG_DEBUG, "WPS: Request reconnection with new network "
  1853. "based on the received credential added");
  1854. wpa_s->normal_scans = 0;
  1855. wpa_supplicant_reinit_autoscan(wpa_s);
  1856. if (wpa_s->wps_ap_channel) {
  1857. u16 chan = wpa_s->wps_ap_channel;
  1858. int freq = 0;
  1859. if (chan >= 1 && chan <= 13)
  1860. freq = 2407 + 5 * chan;
  1861. else if (chan == 14)
  1862. freq = 2484;
  1863. else if (chan >= 30)
  1864. freq = 5000 + 5 * chan;
  1865. if (freq) {
  1866. wpa_printf(MSG_DEBUG, "WPS: Credential indicated "
  1867. "AP channel %u -> %u MHz", chan, freq);
  1868. wpa_s->after_wps = 5;
  1869. wpa_s->wps_freq = freq;
  1870. }
  1871. }
  1872. wpa_s->disconnected = 0;
  1873. wpa_s->reassociate = 1;
  1874. wpa_supplicant_cancel_sched_scan(wpa_s);
  1875. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1876. return 0;
  1877. }
  1878. #ifdef CONFIG_WPS_ER
  1879. static int wpas_wps_add_nfc_password_token(struct wpa_supplicant *wpa_s,
  1880. struct wps_parse_attr *attr)
  1881. {
  1882. return wps_registrar_add_nfc_password_token(
  1883. wpa_s->wps->registrar, attr->oob_dev_password,
  1884. attr->oob_dev_password_len);
  1885. }
  1886. #endif /* CONFIG_WPS_ER */
  1887. static int wpas_wps_nfc_tag_process(struct wpa_supplicant *wpa_s,
  1888. const struct wpabuf *wps)
  1889. {
  1890. struct wps_parse_attr attr;
  1891. wpa_hexdump_buf(MSG_DEBUG, "WPS: Received NFC tag payload", wps);
  1892. if (wps_parse_msg(wps, &attr)) {
  1893. wpa_printf(MSG_DEBUG, "WPS: Ignore invalid data from NFC tag");
  1894. return -1;
  1895. }
  1896. if (attr.num_cred)
  1897. return wpas_wps_use_cred(wpa_s, &attr);
  1898. #ifdef CONFIG_WPS_ER
  1899. if (attr.oob_dev_password)
  1900. return wpas_wps_add_nfc_password_token(wpa_s, &attr);
  1901. #endif /* CONFIG_WPS_ER */
  1902. wpa_printf(MSG_DEBUG, "WPS: Ignore unrecognized NFC tag");
  1903. return -1;
  1904. }
  1905. int wpas_wps_nfc_tag_read(struct wpa_supplicant *wpa_s,
  1906. const struct wpabuf *data)
  1907. {
  1908. const struct wpabuf *wps = data;
  1909. struct wpabuf *tmp = NULL;
  1910. int ret;
  1911. if (wpabuf_len(data) < 4)
  1912. return -1;
  1913. if (*wpabuf_head_u8(data) != 0x10) {
  1914. /* Assume this contains full NDEF record */
  1915. tmp = ndef_parse_wifi(data);
  1916. if (tmp == NULL) {
  1917. wpa_printf(MSG_DEBUG, "WPS: Could not parse NDEF");
  1918. return -1;
  1919. }
  1920. wps = tmp;
  1921. }
  1922. ret = wpas_wps_nfc_tag_process(wpa_s, wps);
  1923. wpabuf_free(tmp);
  1924. return ret;
  1925. }
  1926. struct wpabuf * wpas_wps_nfc_handover_req(struct wpa_supplicant *wpa_s, int cr)
  1927. {
  1928. if (cr)
  1929. return ndef_build_wifi_hc(1);
  1930. return ndef_build_wifi_hr();
  1931. }
  1932. #ifdef CONFIG_WPS_NFC
  1933. struct wpabuf * wpas_wps_er_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  1934. int ndef, const char *uuid)
  1935. {
  1936. #ifdef CONFIG_WPS_ER
  1937. struct wpabuf *ret;
  1938. u8 u[UUID_LEN], *use_uuid = NULL;
  1939. u8 addr[ETH_ALEN], *use_addr = NULL;
  1940. if (!wpa_s->wps_er)
  1941. return NULL;
  1942. if (uuid == NULL)
  1943. return NULL;
  1944. if (uuid_str2bin(uuid, u) == 0)
  1945. use_uuid = u;
  1946. else if (hwaddr_aton(uuid, addr) == 0)
  1947. use_addr = addr;
  1948. else
  1949. return NULL;
  1950. /*
  1951. * Handover Select carrier record for WPS uses the same format as
  1952. * configuration token.
  1953. */
  1954. ret = wps_er_nfc_config_token(wpa_s->wps_er, use_uuid, use_addr);
  1955. if (ndef && ret) {
  1956. struct wpabuf *tmp;
  1957. tmp = ndef_build_wifi(ret);
  1958. wpabuf_free(ret);
  1959. if (tmp == NULL)
  1960. return NULL;
  1961. ret = tmp;
  1962. }
  1963. return ret;
  1964. #else /* CONFIG_WPS_ER */
  1965. return NULL;
  1966. #endif /* CONFIG_WPS_ER */
  1967. }
  1968. #endif /* CONFIG_WPS_NFC */
  1969. struct wpabuf * wpas_wps_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  1970. int ndef, int cr, const char *uuid)
  1971. {
  1972. struct wpabuf *ret;
  1973. if (!cr)
  1974. return NULL;
  1975. ret = wpas_ap_wps_nfc_handover_sel(wpa_s, ndef);
  1976. if (ret)
  1977. return ret;
  1978. return wpas_wps_er_nfc_handover_sel(wpa_s, ndef, uuid);
  1979. }
  1980. int wpas_wps_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
  1981. const struct wpabuf *data)
  1982. {
  1983. /* TODO */
  1984. return -1;
  1985. }
  1986. int wpas_wps_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  1987. const struct wpabuf *data)
  1988. {
  1989. struct wpabuf *wps;
  1990. int ret;
  1991. wps = ndef_parse_wifi(data);
  1992. if (wps == NULL)
  1993. return -1;
  1994. wpa_printf(MSG_DEBUG, "WPS: Received application/vnd.wfa.wsc "
  1995. "payload from NFC connection handover");
  1996. wpa_hexdump_buf_key(MSG_DEBUG, "WPS: NFC payload", wps);
  1997. ret = wpas_wps_nfc_tag_process(wpa_s, wps);
  1998. wpabuf_free(wps);
  1999. return ret;
  2000. }
  2001. int wpas_wps_nfc_report_handover(struct wpa_supplicant *wpa_s,
  2002. const struct wpabuf *req,
  2003. const struct wpabuf *sel)
  2004. {
  2005. wpa_printf(MSG_DEBUG, "NFC: WPS connection handover reported");
  2006. wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in request", req);
  2007. wpa_hexdump_buf_key(MSG_DEBUG, "WPS: Carrier record in select", sel);
  2008. return wpas_wps_nfc_rx_handover_sel(wpa_s, sel);
  2009. }
  2010. #endif /* CONFIG_WPS_NFC */
  2011. extern int wpa_debug_level;
  2012. static void wpas_wps_dump_ap_info(struct wpa_supplicant *wpa_s)
  2013. {
  2014. size_t i;
  2015. struct os_time now;
  2016. if (wpa_debug_level > MSG_DEBUG)
  2017. return;
  2018. if (wpa_s->wps_ap == NULL)
  2019. return;
  2020. os_get_time(&now);
  2021. for (i = 0; i < wpa_s->num_wps_ap; i++) {
  2022. struct wps_ap_info *ap = &wpa_s->wps_ap[i];
  2023. struct wpa_blacklist *e = wpa_blacklist_get(wpa_s, ap->bssid);
  2024. wpa_printf(MSG_DEBUG, "WPS: AP[%d] " MACSTR " type=%d "
  2025. "tries=%d last_attempt=%d sec ago blacklist=%d",
  2026. (int) i, MAC2STR(ap->bssid), ap->type, ap->tries,
  2027. ap->last_attempt.sec > 0 ?
  2028. (int) now.sec - (int) ap->last_attempt.sec : -1,
  2029. e ? e->count : 0);
  2030. }
  2031. }
  2032. static struct wps_ap_info * wpas_wps_get_ap_info(struct wpa_supplicant *wpa_s,
  2033. const u8 *bssid)
  2034. {
  2035. size_t i;
  2036. if (wpa_s->wps_ap == NULL)
  2037. return NULL;
  2038. for (i = 0; i < wpa_s->num_wps_ap; i++) {
  2039. struct wps_ap_info *ap = &wpa_s->wps_ap[i];
  2040. if (os_memcmp(ap->bssid, bssid, ETH_ALEN) == 0)
  2041. return ap;
  2042. }
  2043. return NULL;
  2044. }
  2045. static void wpas_wps_update_ap_info_bss(struct wpa_supplicant *wpa_s,
  2046. struct wpa_scan_res *res)
  2047. {
  2048. struct wpabuf *wps;
  2049. enum wps_ap_info_type type;
  2050. struct wps_ap_info *ap;
  2051. int r;
  2052. if (wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE) == NULL)
  2053. return;
  2054. wps = wpa_scan_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
  2055. if (wps == NULL)
  2056. return;
  2057. r = wps_is_addr_authorized(wps, wpa_s->own_addr, 1);
  2058. if (r == 2)
  2059. type = WPS_AP_SEL_REG_OUR;
  2060. else if (r == 1)
  2061. type = WPS_AP_SEL_REG;
  2062. else
  2063. type = WPS_AP_NOT_SEL_REG;
  2064. wpabuf_free(wps);
  2065. ap = wpas_wps_get_ap_info(wpa_s, res->bssid);
  2066. if (ap) {
  2067. if (ap->type != type) {
  2068. wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR
  2069. " changed type %d -> %d",
  2070. MAC2STR(res->bssid), ap->type, type);
  2071. ap->type = type;
  2072. if (type != WPS_AP_NOT_SEL_REG)
  2073. wpa_blacklist_del(wpa_s, ap->bssid);
  2074. }
  2075. return;
  2076. }
  2077. ap = os_realloc_array(wpa_s->wps_ap, wpa_s->num_wps_ap + 1,
  2078. sizeof(struct wps_ap_info));
  2079. if (ap == NULL)
  2080. return;
  2081. wpa_s->wps_ap = ap;
  2082. ap = &wpa_s->wps_ap[wpa_s->num_wps_ap];
  2083. wpa_s->num_wps_ap++;
  2084. os_memset(ap, 0, sizeof(*ap));
  2085. os_memcpy(ap->bssid, res->bssid, ETH_ALEN);
  2086. ap->type = type;
  2087. wpa_printf(MSG_DEBUG, "WPS: AP " MACSTR " type %d added",
  2088. MAC2STR(ap->bssid), ap->type);
  2089. }
  2090. void wpas_wps_update_ap_info(struct wpa_supplicant *wpa_s,
  2091. struct wpa_scan_results *scan_res)
  2092. {
  2093. size_t i;
  2094. for (i = 0; i < scan_res->num; i++)
  2095. wpas_wps_update_ap_info_bss(wpa_s, scan_res->res[i]);
  2096. wpas_wps_dump_ap_info(wpa_s);
  2097. }
  2098. void wpas_wps_notify_assoc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2099. {
  2100. struct wps_ap_info *ap;
  2101. wpa_s->after_wps = 0;
  2102. if (!wpa_s->wps_ap_iter)
  2103. return;
  2104. ap = wpas_wps_get_ap_info(wpa_s, bssid);
  2105. if (ap == NULL)
  2106. return;
  2107. ap->tries++;
  2108. os_get_time(&ap->last_attempt);
  2109. }