wps_supplicant.c 76 KB

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