wps_supplicant.c 64 KB

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