wps_supplicant.c 72 KB

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