wps_supplicant.c 75 KB

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