wps_supplicant.c 64 KB

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