wps_supplicant.c 74 KB

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