wps_supplicant.c 73 KB

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