wps_supplicant.c 78 KB

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