wps_supplicant.c 78 KB

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