wpa_supplicant.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2012, 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. * This file implements functions for registering and unregistering
  9. * %wpa_supplicant interfaces. In addition, this file contains number of
  10. * functions for managing network connections.
  11. */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "crypto/random.h"
  15. #include "crypto/sha1.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "eap_peer/eap.h"
  18. #include "eap_server/eap_methods.h"
  19. #include "rsn_supp/wpa.h"
  20. #include "eloop.h"
  21. #include "config.h"
  22. #include "l2_packet/l2_packet.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "ctrl_iface.h"
  26. #include "pcsc_funcs.h"
  27. #include "common/version.h"
  28. #include "rsn_supp/preauth.h"
  29. #include "rsn_supp/pmksa_cache.h"
  30. #include "common/wpa_ctrl.h"
  31. #include "common/ieee802_11_defs.h"
  32. #include "p2p/p2p.h"
  33. #include "blacklist.h"
  34. #include "wpas_glue.h"
  35. #include "wps_supplicant.h"
  36. #include "ibss_rsn.h"
  37. #include "sme.h"
  38. #include "gas_query.h"
  39. #include "ap.h"
  40. #include "p2p_supplicant.h"
  41. #include "notify.h"
  42. #include "bgscan.h"
  43. #include "bss.h"
  44. #include "scan.h"
  45. #include "offchannel.h"
  46. const char *wpa_supplicant_version =
  47. "wpa_supplicant v" VERSION_STR "\n"
  48. "Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
  49. const char *wpa_supplicant_license =
  50. "This software may be distributed under the terms of the BSD license.\n"
  51. "See README for more details.\n"
  52. #ifdef EAP_TLS_OPENSSL
  53. "\nThis product includes software developed by the OpenSSL Project\n"
  54. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  55. #endif /* EAP_TLS_OPENSSL */
  56. ;
  57. #ifndef CONFIG_NO_STDOUT_DEBUG
  58. /* Long text divided into parts in order to fit in C89 strings size limits. */
  59. const char *wpa_supplicant_full_license1 =
  60. "";
  61. const char *wpa_supplicant_full_license2 =
  62. "This software may be distributed under the terms of the BSD license.\n"
  63. "\n"
  64. "Redistribution and use in source and binary forms, with or without\n"
  65. "modification, are permitted provided that the following conditions are\n"
  66. "met:\n"
  67. "\n";
  68. const char *wpa_supplicant_full_license3 =
  69. "1. Redistributions of source code must retain the above copyright\n"
  70. " notice, this list of conditions and the following disclaimer.\n"
  71. "\n"
  72. "2. Redistributions in binary form must reproduce the above copyright\n"
  73. " notice, this list of conditions and the following disclaimer in the\n"
  74. " documentation and/or other materials provided with the distribution.\n"
  75. "\n";
  76. const char *wpa_supplicant_full_license4 =
  77. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  78. " names of its contributors may be used to endorse or promote products\n"
  79. " derived from this software without specific prior written permission.\n"
  80. "\n"
  81. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  82. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  83. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  84. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  85. const char *wpa_supplicant_full_license5 =
  86. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  87. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  88. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  89. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  90. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  91. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  92. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  93. "\n";
  94. #endif /* CONFIG_NO_STDOUT_DEBUG */
  95. extern int wpa_debug_level;
  96. extern int wpa_debug_show_keys;
  97. extern int wpa_debug_timestamp;
  98. extern struct wpa_driver_ops *wpa_drivers[];
  99. /* Configure default/group WEP keys for static WEP */
  100. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  101. {
  102. int i, set = 0;
  103. for (i = 0; i < NUM_WEP_KEYS; i++) {
  104. if (ssid->wep_key_len[i] == 0)
  105. continue;
  106. set = 1;
  107. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  108. i, i == ssid->wep_tx_keyidx, NULL, 0,
  109. ssid->wep_key[i], ssid->wep_key_len[i]);
  110. }
  111. return set;
  112. }
  113. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  114. struct wpa_ssid *ssid)
  115. {
  116. u8 key[32];
  117. size_t keylen;
  118. enum wpa_alg alg;
  119. u8 seq[6] = { 0 };
  120. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  121. * sending unicast and multicast packets. */
  122. if (ssid->mode != WPAS_MODE_IBSS) {
  123. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  124. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  125. return -1;
  126. }
  127. if (!ssid->psk_set) {
  128. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  129. "WPA-None");
  130. return -1;
  131. }
  132. switch (wpa_s->group_cipher) {
  133. case WPA_CIPHER_CCMP:
  134. os_memcpy(key, ssid->psk, 16);
  135. keylen = 16;
  136. alg = WPA_ALG_CCMP;
  137. break;
  138. case WPA_CIPHER_TKIP:
  139. /* WPA-None uses the same Michael MIC key for both TX and RX */
  140. os_memcpy(key, ssid->psk, 16 + 8);
  141. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  142. keylen = 32;
  143. alg = WPA_ALG_TKIP;
  144. break;
  145. default:
  146. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  147. "WPA-None", wpa_s->group_cipher);
  148. return -1;
  149. }
  150. /* TODO: should actually remember the previously used seq#, both for TX
  151. * and RX from each STA.. */
  152. return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  153. }
  154. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  155. {
  156. struct wpa_supplicant *wpa_s = eloop_ctx;
  157. const u8 *bssid = wpa_s->bssid;
  158. if (is_zero_ether_addr(bssid))
  159. bssid = wpa_s->pending_bssid;
  160. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  161. MAC2STR(bssid));
  162. wpa_blacklist_add(wpa_s, bssid);
  163. wpa_sm_notify_disassoc(wpa_s->wpa);
  164. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  165. wpa_s->reassociate = 1;
  166. /*
  167. * If we timed out, the AP or the local radio may be busy.
  168. * So, wait a second until scanning again.
  169. */
  170. wpa_supplicant_req_scan(wpa_s, 1, 0);
  171. }
  172. /**
  173. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  174. * @wpa_s: Pointer to wpa_supplicant data
  175. * @sec: Number of seconds after which to time out authentication
  176. * @usec: Number of microseconds after which to time out authentication
  177. *
  178. * This function is used to schedule a timeout for the current authentication
  179. * attempt.
  180. */
  181. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  182. int sec, int usec)
  183. {
  184. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  185. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  186. return;
  187. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  188. "%d usec", sec, usec);
  189. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  190. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  191. }
  192. /**
  193. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  194. * @wpa_s: Pointer to wpa_supplicant data
  195. *
  196. * This function is used to cancel authentication timeout scheduled with
  197. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  198. * been completed.
  199. */
  200. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  201. {
  202. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  203. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  204. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  205. }
  206. /**
  207. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  208. * @wpa_s: Pointer to wpa_supplicant data
  209. *
  210. * This function is used to configure EAPOL state machine based on the selected
  211. * authentication mode.
  212. */
  213. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  214. {
  215. #ifdef IEEE8021X_EAPOL
  216. struct eapol_config eapol_conf;
  217. struct wpa_ssid *ssid = wpa_s->current_ssid;
  218. #ifdef CONFIG_IBSS_RSN
  219. if (ssid->mode == WPAS_MODE_IBSS &&
  220. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  221. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  222. /*
  223. * RSN IBSS authentication is per-STA and we can disable the
  224. * per-BSSID EAPOL authentication.
  225. */
  226. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  227. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  228. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  229. return;
  230. }
  231. #endif /* CONFIG_IBSS_RSN */
  232. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  233. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  234. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  235. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  236. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  237. else
  238. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  239. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  240. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  241. eapol_conf.accept_802_1x_keys = 1;
  242. eapol_conf.required_keys = 0;
  243. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  244. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  245. }
  246. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  247. eapol_conf.required_keys |=
  248. EAPOL_REQUIRE_KEY_BROADCAST;
  249. }
  250. if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  251. eapol_conf.required_keys = 0;
  252. }
  253. if (wpa_s->conf)
  254. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  255. eapol_conf.workaround = ssid->eap_workaround;
  256. eapol_conf.eap_disabled =
  257. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  258. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  259. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  260. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  261. #endif /* IEEE8021X_EAPOL */
  262. }
  263. /**
  264. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  265. * @wpa_s: Pointer to wpa_supplicant data
  266. * @ssid: Configuration data for the network
  267. *
  268. * This function is used to configure WPA state machine and related parameters
  269. * to a mode where WPA is not enabled. This is called as part of the
  270. * authentication configuration when the selected network does not use WPA.
  271. */
  272. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  273. struct wpa_ssid *ssid)
  274. {
  275. int i;
  276. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  277. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  278. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  279. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  280. else
  281. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  282. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  283. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  284. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  285. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  286. wpa_s->group_cipher = WPA_CIPHER_NONE;
  287. wpa_s->mgmt_group_cipher = 0;
  288. for (i = 0; i < NUM_WEP_KEYS; i++) {
  289. if (ssid->wep_key_len[i] > 5) {
  290. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  291. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  292. break;
  293. } else if (ssid->wep_key_len[i] > 0) {
  294. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  295. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  296. break;
  297. }
  298. }
  299. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  300. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  301. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  302. wpa_s->pairwise_cipher);
  303. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  304. #ifdef CONFIG_IEEE80211W
  305. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  306. wpa_s->mgmt_group_cipher);
  307. #endif /* CONFIG_IEEE80211W */
  308. pmksa_cache_clear_current(wpa_s->wpa);
  309. }
  310. void free_hw_features(struct wpa_supplicant *wpa_s)
  311. {
  312. int i;
  313. if (wpa_s->hw.modes == NULL)
  314. return;
  315. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  316. os_free(wpa_s->hw.modes[i].channels);
  317. os_free(wpa_s->hw.modes[i].rates);
  318. }
  319. os_free(wpa_s->hw.modes);
  320. wpa_s->hw.modes = NULL;
  321. }
  322. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  323. {
  324. bgscan_deinit(wpa_s);
  325. scard_deinit(wpa_s->scard);
  326. wpa_s->scard = NULL;
  327. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  328. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  329. l2_packet_deinit(wpa_s->l2);
  330. wpa_s->l2 = NULL;
  331. if (wpa_s->l2_br) {
  332. l2_packet_deinit(wpa_s->l2_br);
  333. wpa_s->l2_br = NULL;
  334. }
  335. if (wpa_s->conf != NULL) {
  336. struct wpa_ssid *ssid;
  337. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  338. wpas_notify_network_removed(wpa_s, ssid);
  339. }
  340. os_free(wpa_s->confname);
  341. wpa_s->confname = NULL;
  342. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  343. eapol_sm_deinit(wpa_s->eapol);
  344. wpa_s->eapol = NULL;
  345. rsn_preauth_deinit(wpa_s->wpa);
  346. #ifdef CONFIG_TDLS
  347. wpa_tdls_deinit(wpa_s->wpa);
  348. #endif /* CONFIG_TDLS */
  349. pmksa_candidate_free(wpa_s->wpa);
  350. wpa_sm_deinit(wpa_s->wpa);
  351. wpa_s->wpa = NULL;
  352. wpa_blacklist_clear(wpa_s);
  353. wpa_bss_deinit(wpa_s);
  354. wpa_supplicant_cancel_scan(wpa_s);
  355. wpa_supplicant_cancel_auth_timeout(wpa_s);
  356. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  357. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  358. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  359. wpa_s, NULL);
  360. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  361. wpas_wps_deinit(wpa_s);
  362. wpabuf_free(wpa_s->pending_eapol_rx);
  363. wpa_s->pending_eapol_rx = NULL;
  364. #ifdef CONFIG_IBSS_RSN
  365. ibss_rsn_deinit(wpa_s->ibss_rsn);
  366. wpa_s->ibss_rsn = NULL;
  367. #endif /* CONFIG_IBSS_RSN */
  368. sme_deinit(wpa_s);
  369. #ifdef CONFIG_AP
  370. wpa_supplicant_ap_deinit(wpa_s);
  371. #endif /* CONFIG_AP */
  372. #ifdef CONFIG_P2P
  373. wpas_p2p_deinit(wpa_s);
  374. #endif /* CONFIG_P2P */
  375. #ifdef CONFIG_OFFCHANNEL
  376. offchannel_deinit(wpa_s);
  377. #endif /* CONFIG_OFFCHANNEL */
  378. wpa_supplicant_cancel_sched_scan(wpa_s);
  379. os_free(wpa_s->next_scan_freqs);
  380. wpa_s->next_scan_freqs = NULL;
  381. gas_query_deinit(wpa_s->gas);
  382. wpa_s->gas = NULL;
  383. free_hw_features(wpa_s);
  384. os_free(wpa_s->bssid_filter);
  385. wpa_s->bssid_filter = NULL;
  386. }
  387. /**
  388. * wpa_clear_keys - Clear keys configured for the driver
  389. * @wpa_s: Pointer to wpa_supplicant data
  390. * @addr: Previously used BSSID or %NULL if not available
  391. *
  392. * This function clears the encryption keys that has been previously configured
  393. * for the driver.
  394. */
  395. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  396. {
  397. if (wpa_s->keys_cleared) {
  398. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  399. * timing issues with keys being cleared just before new keys
  400. * are set or just after association or something similar. This
  401. * shows up in group key handshake failing often because of the
  402. * client not receiving the first encrypted packets correctly.
  403. * Skipping some of the extra key clearing steps seems to help
  404. * in completing group key handshake more reliably. */
  405. wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
  406. "skip key clearing");
  407. return;
  408. }
  409. /* MLME-DELETEKEYS.request */
  410. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  411. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  412. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  413. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  414. #ifdef CONFIG_IEEE80211W
  415. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  416. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  417. #endif /* CONFIG_IEEE80211W */
  418. if (addr) {
  419. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  420. 0);
  421. /* MLME-SETPROTECTION.request(None) */
  422. wpa_drv_mlme_setprotection(
  423. wpa_s, addr,
  424. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  425. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  426. }
  427. wpa_s->keys_cleared = 1;
  428. }
  429. /**
  430. * wpa_supplicant_state_txt - Get the connection state name as a text string
  431. * @state: State (wpa_state; WPA_*)
  432. * Returns: The state name as a printable text string
  433. */
  434. const char * wpa_supplicant_state_txt(enum wpa_states state)
  435. {
  436. switch (state) {
  437. case WPA_DISCONNECTED:
  438. return "DISCONNECTED";
  439. case WPA_INACTIVE:
  440. return "INACTIVE";
  441. case WPA_INTERFACE_DISABLED:
  442. return "INTERFACE_DISABLED";
  443. case WPA_SCANNING:
  444. return "SCANNING";
  445. case WPA_AUTHENTICATING:
  446. return "AUTHENTICATING";
  447. case WPA_ASSOCIATING:
  448. return "ASSOCIATING";
  449. case WPA_ASSOCIATED:
  450. return "ASSOCIATED";
  451. case WPA_4WAY_HANDSHAKE:
  452. return "4WAY_HANDSHAKE";
  453. case WPA_GROUP_HANDSHAKE:
  454. return "GROUP_HANDSHAKE";
  455. case WPA_COMPLETED:
  456. return "COMPLETED";
  457. default:
  458. return "UNKNOWN";
  459. }
  460. }
  461. #ifdef CONFIG_BGSCAN
  462. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  463. {
  464. if (wpas_driver_bss_selection(wpa_s))
  465. return;
  466. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  467. return;
  468. bgscan_deinit(wpa_s);
  469. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  470. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  471. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  472. "bgscan");
  473. /*
  474. * Live without bgscan; it is only used as a roaming
  475. * optimization, so the initial connection is not
  476. * affected.
  477. */
  478. } else
  479. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  480. } else
  481. wpa_s->bgscan_ssid = NULL;
  482. }
  483. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  484. {
  485. if (wpa_s->bgscan_ssid != NULL) {
  486. bgscan_deinit(wpa_s);
  487. wpa_s->bgscan_ssid = NULL;
  488. }
  489. }
  490. #endif /* CONFIG_BGSCAN */
  491. /**
  492. * wpa_supplicant_set_state - Set current connection state
  493. * @wpa_s: Pointer to wpa_supplicant data
  494. * @state: The new connection state
  495. *
  496. * This function is called whenever the connection state changes, e.g.,
  497. * association is completed for WPA/WPA2 4-Way Handshake is started.
  498. */
  499. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  500. enum wpa_states state)
  501. {
  502. enum wpa_states old_state = wpa_s->wpa_state;
  503. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  504. wpa_supplicant_state_txt(wpa_s->wpa_state),
  505. wpa_supplicant_state_txt(state));
  506. if (state != WPA_SCANNING)
  507. wpa_supplicant_notify_scanning(wpa_s, 0);
  508. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  509. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  510. struct wpa_ssid *ssid = wpa_s->current_ssid;
  511. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  512. MACSTR " completed %s [id=%d id_str=%s]",
  513. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  514. "(reauth)" : "(auth)",
  515. ssid ? ssid->id : -1,
  516. ssid && ssid->id_str ? ssid->id_str : "");
  517. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  518. wpa_s->new_connection = 0;
  519. wpa_s->reassociated_connection = 1;
  520. wpa_drv_set_operstate(wpa_s, 1);
  521. #ifndef IEEE8021X_EAPOL
  522. wpa_drv_set_supp_port(wpa_s, 1);
  523. #endif /* IEEE8021X_EAPOL */
  524. wpa_s->after_wps = 0;
  525. #ifdef CONFIG_P2P
  526. wpas_p2p_completed(wpa_s);
  527. #endif /* CONFIG_P2P */
  528. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  529. state == WPA_ASSOCIATED) {
  530. wpa_s->new_connection = 1;
  531. wpa_drv_set_operstate(wpa_s, 0);
  532. #ifndef IEEE8021X_EAPOL
  533. wpa_drv_set_supp_port(wpa_s, 0);
  534. #endif /* IEEE8021X_EAPOL */
  535. }
  536. wpa_s->wpa_state = state;
  537. #ifdef CONFIG_BGSCAN
  538. if (state == WPA_COMPLETED)
  539. wpa_supplicant_start_bgscan(wpa_s);
  540. else
  541. wpa_supplicant_stop_bgscan(wpa_s);
  542. #endif /* CONFIG_BGSCAN */
  543. if (wpa_s->wpa_state != old_state) {
  544. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  545. if (wpa_s->wpa_state == WPA_COMPLETED ||
  546. old_state == WPA_COMPLETED)
  547. wpas_notify_auth_changed(wpa_s);
  548. }
  549. }
  550. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  551. {
  552. int pending = 0;
  553. #ifdef CONFIG_WPS
  554. struct wpa_supplicant *wpa_s = global->ifaces;
  555. while (wpa_s) {
  556. if (wpas_wps_terminate_pending(wpa_s) == 1)
  557. pending = 1;
  558. wpa_s = wpa_s->next;
  559. }
  560. #endif /* CONFIG_WPS */
  561. if (pending)
  562. return;
  563. eloop_terminate();
  564. }
  565. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  566. {
  567. struct wpa_global *global = signal_ctx;
  568. wpa_supplicant_terminate_proc(global);
  569. }
  570. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  571. {
  572. enum wpa_states old_state = wpa_s->wpa_state;
  573. wpa_s->pairwise_cipher = 0;
  574. wpa_s->group_cipher = 0;
  575. wpa_s->mgmt_group_cipher = 0;
  576. wpa_s->key_mgmt = 0;
  577. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  578. wpa_s->wpa_state = WPA_DISCONNECTED;
  579. if (wpa_s->wpa_state != old_state)
  580. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  581. }
  582. /**
  583. * wpa_supplicant_reload_configuration - Reload configuration data
  584. * @wpa_s: Pointer to wpa_supplicant data
  585. * Returns: 0 on success or -1 if configuration parsing failed
  586. *
  587. * This function can be used to request that the configuration data is reloaded
  588. * (e.g., after configuration file change). This function is reloading
  589. * configuration only for one interface, so this may need to be called multiple
  590. * times if %wpa_supplicant is controlling multiple interfaces and all
  591. * interfaces need reconfiguration.
  592. */
  593. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  594. {
  595. struct wpa_config *conf;
  596. int reconf_ctrl;
  597. int old_ap_scan;
  598. if (wpa_s->confname == NULL)
  599. return -1;
  600. conf = wpa_config_read(wpa_s->confname);
  601. if (conf == NULL) {
  602. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  603. "file '%s' - exiting", wpa_s->confname);
  604. return -1;
  605. }
  606. conf->changed_parameters = (unsigned int) -1;
  607. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  608. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  609. os_strcmp(conf->ctrl_interface,
  610. wpa_s->conf->ctrl_interface) != 0);
  611. if (reconf_ctrl && wpa_s->ctrl_iface) {
  612. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  613. wpa_s->ctrl_iface = NULL;
  614. }
  615. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  616. if (wpa_s->current_ssid) {
  617. wpa_supplicant_deauthenticate(wpa_s,
  618. WLAN_REASON_DEAUTH_LEAVING);
  619. }
  620. /*
  621. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  622. * pkcs11_engine_path, pkcs11_module_path.
  623. */
  624. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  625. /*
  626. * Clear forced success to clear EAP state for next
  627. * authentication.
  628. */
  629. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  630. }
  631. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  632. wpa_sm_set_config(wpa_s->wpa, NULL);
  633. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  634. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  635. rsn_preauth_deinit(wpa_s->wpa);
  636. old_ap_scan = wpa_s->conf->ap_scan;
  637. wpa_config_free(wpa_s->conf);
  638. wpa_s->conf = conf;
  639. if (old_ap_scan != wpa_s->conf->ap_scan)
  640. wpas_notify_ap_scan_changed(wpa_s);
  641. if (reconf_ctrl)
  642. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  643. wpa_supplicant_update_config(wpa_s);
  644. wpa_supplicant_clear_status(wpa_s);
  645. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  646. wpa_s->reassociate = 1;
  647. wpa_supplicant_req_scan(wpa_s, 0, 0);
  648. }
  649. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  650. return 0;
  651. }
  652. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  653. {
  654. struct wpa_global *global = signal_ctx;
  655. struct wpa_supplicant *wpa_s;
  656. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  657. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  658. sig);
  659. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  660. wpa_supplicant_terminate_proc(global);
  661. }
  662. }
  663. }
  664. enum wpa_cipher cipher_suite2driver(int cipher)
  665. {
  666. switch (cipher) {
  667. case WPA_CIPHER_NONE:
  668. return CIPHER_NONE;
  669. case WPA_CIPHER_WEP40:
  670. return CIPHER_WEP40;
  671. case WPA_CIPHER_WEP104:
  672. return CIPHER_WEP104;
  673. case WPA_CIPHER_CCMP:
  674. return CIPHER_CCMP;
  675. case WPA_CIPHER_TKIP:
  676. default:
  677. return CIPHER_TKIP;
  678. }
  679. }
  680. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  681. {
  682. switch (key_mgmt) {
  683. case WPA_KEY_MGMT_NONE:
  684. return KEY_MGMT_NONE;
  685. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  686. return KEY_MGMT_802_1X_NO_WPA;
  687. case WPA_KEY_MGMT_IEEE8021X:
  688. return KEY_MGMT_802_1X;
  689. case WPA_KEY_MGMT_WPA_NONE:
  690. return KEY_MGMT_WPA_NONE;
  691. case WPA_KEY_MGMT_FT_IEEE8021X:
  692. return KEY_MGMT_FT_802_1X;
  693. case WPA_KEY_MGMT_FT_PSK:
  694. return KEY_MGMT_FT_PSK;
  695. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  696. return KEY_MGMT_802_1X_SHA256;
  697. case WPA_KEY_MGMT_PSK_SHA256:
  698. return KEY_MGMT_PSK_SHA256;
  699. case WPA_KEY_MGMT_WPS:
  700. return KEY_MGMT_WPS;
  701. case WPA_KEY_MGMT_PSK:
  702. default:
  703. return KEY_MGMT_PSK;
  704. }
  705. }
  706. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  707. struct wpa_ssid *ssid,
  708. struct wpa_ie_data *ie)
  709. {
  710. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  711. if (ret) {
  712. if (ret == -2) {
  713. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  714. "from association info");
  715. }
  716. return -1;
  717. }
  718. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  719. "cipher suites");
  720. if (!(ie->group_cipher & ssid->group_cipher)) {
  721. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  722. "cipher 0x%x (mask 0x%x) - reject",
  723. ie->group_cipher, ssid->group_cipher);
  724. return -1;
  725. }
  726. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  727. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  728. "cipher 0x%x (mask 0x%x) - reject",
  729. ie->pairwise_cipher, ssid->pairwise_cipher);
  730. return -1;
  731. }
  732. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  733. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  734. "management 0x%x (mask 0x%x) - reject",
  735. ie->key_mgmt, ssid->key_mgmt);
  736. return -1;
  737. }
  738. #ifdef CONFIG_IEEE80211W
  739. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  740. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  741. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  742. "that does not support management frame protection - "
  743. "reject");
  744. return -1;
  745. }
  746. #endif /* CONFIG_IEEE80211W */
  747. return 0;
  748. }
  749. /**
  750. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  751. * @wpa_s: Pointer to wpa_supplicant data
  752. * @bss: Scan results for the selected BSS, or %NULL if not available
  753. * @ssid: Configuration data for the selected network
  754. * @wpa_ie: Buffer for the WPA/RSN IE
  755. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  756. * used buffer length in case the functions returns success.
  757. * Returns: 0 on success or -1 on failure
  758. *
  759. * This function is used to configure authentication and encryption parameters
  760. * based on the network configuration and scan result for the selected BSS (if
  761. * available).
  762. */
  763. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  764. struct wpa_bss *bss, struct wpa_ssid *ssid,
  765. u8 *wpa_ie, size_t *wpa_ie_len)
  766. {
  767. struct wpa_ie_data ie;
  768. int sel, proto;
  769. const u8 *bss_wpa, *bss_rsn;
  770. if (bss) {
  771. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  772. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  773. } else
  774. bss_wpa = bss_rsn = NULL;
  775. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  776. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  777. (ie.group_cipher & ssid->group_cipher) &&
  778. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  779. (ie.key_mgmt & ssid->key_mgmt)) {
  780. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  781. proto = WPA_PROTO_RSN;
  782. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  783. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  784. (ie.group_cipher & ssid->group_cipher) &&
  785. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  786. (ie.key_mgmt & ssid->key_mgmt)) {
  787. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  788. proto = WPA_PROTO_WPA;
  789. } else if (bss) {
  790. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  791. return -1;
  792. } else {
  793. if (ssid->proto & WPA_PROTO_RSN)
  794. proto = WPA_PROTO_RSN;
  795. else
  796. proto = WPA_PROTO_WPA;
  797. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  798. os_memset(&ie, 0, sizeof(ie));
  799. ie.group_cipher = ssid->group_cipher;
  800. ie.pairwise_cipher = ssid->pairwise_cipher;
  801. ie.key_mgmt = ssid->key_mgmt;
  802. #ifdef CONFIG_IEEE80211W
  803. ie.mgmt_group_cipher =
  804. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  805. WPA_CIPHER_AES_128_CMAC : 0;
  806. #endif /* CONFIG_IEEE80211W */
  807. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  808. "based on configuration");
  809. } else
  810. proto = ie.proto;
  811. }
  812. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  813. "pairwise %d key_mgmt %d proto %d",
  814. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  815. #ifdef CONFIG_IEEE80211W
  816. if (ssid->ieee80211w) {
  817. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  818. ie.mgmt_group_cipher);
  819. }
  820. #endif /* CONFIG_IEEE80211W */
  821. wpa_s->wpa_proto = proto;
  822. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  823. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  824. !!(ssid->proto & WPA_PROTO_RSN));
  825. if (bss || !wpa_s->ap_ies_from_associnfo) {
  826. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  827. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  828. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  829. bss_rsn ? 2 + bss_rsn[1] : 0))
  830. return -1;
  831. }
  832. sel = ie.group_cipher & ssid->group_cipher;
  833. if (sel & WPA_CIPHER_CCMP) {
  834. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  835. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  836. } else if (sel & WPA_CIPHER_TKIP) {
  837. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  838. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  839. } else if (sel & WPA_CIPHER_WEP104) {
  840. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  841. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  842. } else if (sel & WPA_CIPHER_WEP40) {
  843. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  844. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  845. } else {
  846. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  847. "cipher");
  848. return -1;
  849. }
  850. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  851. if (sel & WPA_CIPHER_CCMP) {
  852. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  853. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  854. } else if (sel & WPA_CIPHER_TKIP) {
  855. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  856. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  857. } else if (sel & WPA_CIPHER_NONE) {
  858. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  859. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  860. } else {
  861. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  862. "cipher");
  863. return -1;
  864. }
  865. sel = ie.key_mgmt & ssid->key_mgmt;
  866. if (0) {
  867. #ifdef CONFIG_IEEE80211R
  868. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  869. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  870. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  871. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  872. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  873. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  874. #endif /* CONFIG_IEEE80211R */
  875. #ifdef CONFIG_IEEE80211W
  876. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  877. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  878. wpa_dbg(wpa_s, MSG_DEBUG,
  879. "WPA: using KEY_MGMT 802.1X with SHA256");
  880. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  881. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  882. wpa_dbg(wpa_s, MSG_DEBUG,
  883. "WPA: using KEY_MGMT PSK with SHA256");
  884. #endif /* CONFIG_IEEE80211W */
  885. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  886. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  887. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  888. } else if (sel & WPA_KEY_MGMT_PSK) {
  889. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  890. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  891. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  892. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  893. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  894. } else {
  895. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  896. "authenticated key management type");
  897. return -1;
  898. }
  899. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  900. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  901. wpa_s->pairwise_cipher);
  902. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  903. #ifdef CONFIG_IEEE80211W
  904. sel = ie.mgmt_group_cipher;
  905. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  906. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  907. sel = 0;
  908. if (sel & WPA_CIPHER_AES_128_CMAC) {
  909. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  910. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  911. "AES-128-CMAC");
  912. } else {
  913. wpa_s->mgmt_group_cipher = 0;
  914. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  915. }
  916. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  917. wpa_s->mgmt_group_cipher);
  918. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  919. #endif /* CONFIG_IEEE80211W */
  920. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  921. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  922. return -1;
  923. }
  924. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  925. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  926. #ifndef CONFIG_NO_PBKDF2
  927. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  928. ssid->passphrase) {
  929. u8 psk[PMK_LEN];
  930. pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
  931. bss->ssid_len, 4096, psk, PMK_LEN);
  932. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  933. psk, PMK_LEN);
  934. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  935. }
  936. #endif /* CONFIG_NO_PBKDF2 */
  937. } else
  938. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  939. return 0;
  940. }
  941. /**
  942. * wpa_supplicant_associate - Request association
  943. * @wpa_s: Pointer to wpa_supplicant data
  944. * @bss: Scan results for the selected BSS, or %NULL if not available
  945. * @ssid: Configuration data for the selected network
  946. *
  947. * This function is used to request %wpa_supplicant to associate with a BSS.
  948. */
  949. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  950. struct wpa_bss *bss, struct wpa_ssid *ssid)
  951. {
  952. u8 wpa_ie[200];
  953. size_t wpa_ie_len;
  954. int use_crypt, ret, i, bssid_changed;
  955. int algs = WPA_AUTH_ALG_OPEN;
  956. enum wpa_cipher cipher_pairwise, cipher_group;
  957. struct wpa_driver_associate_params params;
  958. int wep_keys_set = 0;
  959. struct wpa_driver_capa capa;
  960. int assoc_failed = 0;
  961. struct wpa_ssid *old_ssid;
  962. #ifdef CONFIG_HT_OVERRIDES
  963. struct ieee80211_ht_capabilities htcaps;
  964. struct ieee80211_ht_capabilities htcaps_mask;
  965. #endif /* CONFIG_HT_OVERRIDES */
  966. #ifdef CONFIG_IBSS_RSN
  967. ibss_rsn_deinit(wpa_s->ibss_rsn);
  968. wpa_s->ibss_rsn = NULL;
  969. #endif /* CONFIG_IBSS_RSN */
  970. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  971. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  972. #ifdef CONFIG_AP
  973. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  974. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  975. "mode");
  976. return;
  977. }
  978. wpa_supplicant_create_ap(wpa_s, ssid);
  979. wpa_s->current_bss = bss;
  980. #else /* CONFIG_AP */
  981. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  982. "the build");
  983. #endif /* CONFIG_AP */
  984. return;
  985. }
  986. #ifdef CONFIG_TDLS
  987. if (bss)
  988. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  989. bss->ie_len);
  990. #endif /* CONFIG_TDLS */
  991. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  992. ssid->mode == IEEE80211_MODE_INFRA) {
  993. sme_authenticate(wpa_s, bss, ssid);
  994. return;
  995. }
  996. os_memset(&params, 0, sizeof(params));
  997. wpa_s->reassociate = 0;
  998. if (bss && !wpas_driver_bss_selection(wpa_s)) {
  999. #ifdef CONFIG_IEEE80211R
  1000. const u8 *ie, *md = NULL;
  1001. #endif /* CONFIG_IEEE80211R */
  1002. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1003. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1004. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1005. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1006. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1007. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1008. if (bssid_changed)
  1009. wpas_notify_bssid_changed(wpa_s);
  1010. #ifdef CONFIG_IEEE80211R
  1011. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1012. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1013. md = ie + 2;
  1014. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1015. if (md) {
  1016. /* Prepare for the next transition */
  1017. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1018. }
  1019. #endif /* CONFIG_IEEE80211R */
  1020. #ifdef CONFIG_WPS
  1021. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1022. wpa_s->conf->ap_scan == 2 &&
  1023. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1024. /* Use ap_scan==1 style network selection to find the network
  1025. */
  1026. wpa_s->scan_req = 2;
  1027. wpa_s->reassociate = 1;
  1028. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1029. return;
  1030. #endif /* CONFIG_WPS */
  1031. } else {
  1032. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1033. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1034. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1035. }
  1036. wpa_supplicant_cancel_sched_scan(wpa_s);
  1037. wpa_supplicant_cancel_scan(wpa_s);
  1038. /* Starting new association, so clear the possibly used WPA IE from the
  1039. * previous association. */
  1040. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1041. #ifdef IEEE8021X_EAPOL
  1042. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1043. if (ssid->leap) {
  1044. if (ssid->non_leap == 0)
  1045. algs = WPA_AUTH_ALG_LEAP;
  1046. else
  1047. algs |= WPA_AUTH_ALG_LEAP;
  1048. }
  1049. }
  1050. #endif /* IEEE8021X_EAPOL */
  1051. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1052. if (ssid->auth_alg) {
  1053. algs = ssid->auth_alg;
  1054. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1055. "0x%x", algs);
  1056. }
  1057. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1058. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1059. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1060. int try_opportunistic;
  1061. try_opportunistic = ssid->proactive_key_caching &&
  1062. (ssid->proto & WPA_PROTO_RSN);
  1063. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1064. wpa_s->current_ssid,
  1065. try_opportunistic) == 0)
  1066. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1067. wpa_ie_len = sizeof(wpa_ie);
  1068. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1069. wpa_ie, &wpa_ie_len)) {
  1070. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1071. "key management and encryption suites");
  1072. return;
  1073. }
  1074. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1075. wpa_ie_len = sizeof(wpa_ie);
  1076. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1077. wpa_ie, &wpa_ie_len)) {
  1078. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1079. "key management and encryption suites (no "
  1080. "scan results)");
  1081. return;
  1082. }
  1083. #ifdef CONFIG_WPS
  1084. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1085. struct wpabuf *wps_ie;
  1086. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1087. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1088. wpa_ie_len = wpabuf_len(wps_ie);
  1089. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1090. } else
  1091. wpa_ie_len = 0;
  1092. wpabuf_free(wps_ie);
  1093. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1094. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1095. params.wps = WPS_MODE_PRIVACY;
  1096. else
  1097. params.wps = WPS_MODE_OPEN;
  1098. wpa_s->wpa_proto = 0;
  1099. #endif /* CONFIG_WPS */
  1100. } else {
  1101. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1102. wpa_ie_len = 0;
  1103. wpa_s->wpa_proto = 0;
  1104. }
  1105. #ifdef CONFIG_P2P
  1106. if (wpa_s->global->p2p) {
  1107. u8 *pos;
  1108. size_t len;
  1109. int res;
  1110. pos = wpa_ie + wpa_ie_len;
  1111. len = sizeof(wpa_ie) - wpa_ie_len;
  1112. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  1113. ssid->p2p_group);
  1114. if (res >= 0)
  1115. wpa_ie_len += res;
  1116. }
  1117. wpa_s->cross_connect_disallowed = 0;
  1118. if (bss) {
  1119. struct wpabuf *p2p;
  1120. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1121. if (p2p) {
  1122. wpa_s->cross_connect_disallowed =
  1123. p2p_get_cross_connect_disallowed(p2p);
  1124. wpabuf_free(p2p);
  1125. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1126. "connection",
  1127. wpa_s->cross_connect_disallowed ?
  1128. "disallows" : "allows");
  1129. }
  1130. }
  1131. #endif /* CONFIG_P2P */
  1132. #ifdef CONFIG_INTERWORKING
  1133. if (wpa_s->conf->interworking) {
  1134. u8 *pos = wpa_ie;
  1135. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1136. pos += 2 + pos[1];
  1137. os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
  1138. wpa_ie_len += 6;
  1139. *pos++ = WLAN_EID_EXT_CAPAB;
  1140. *pos++ = 4;
  1141. *pos++ = 0x00;
  1142. *pos++ = 0x00;
  1143. *pos++ = 0x00;
  1144. *pos++ = 0x80; /* Bit 31 - Interworking */
  1145. }
  1146. #endif /* CONFIG_INTERWORKING */
  1147. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1148. use_crypt = 1;
  1149. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1150. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1151. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1152. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1153. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1154. use_crypt = 0;
  1155. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1156. use_crypt = 1;
  1157. wep_keys_set = 1;
  1158. }
  1159. }
  1160. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1161. use_crypt = 0;
  1162. #ifdef IEEE8021X_EAPOL
  1163. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1164. if ((ssid->eapol_flags &
  1165. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1166. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1167. !wep_keys_set) {
  1168. use_crypt = 0;
  1169. } else {
  1170. /* Assume that dynamic WEP-104 keys will be used and
  1171. * set cipher suites in order for drivers to expect
  1172. * encryption. */
  1173. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1174. }
  1175. }
  1176. #endif /* IEEE8021X_EAPOL */
  1177. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1178. /* Set the key before (and later after) association */
  1179. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1180. }
  1181. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1182. if (bss) {
  1183. params.ssid = bss->ssid;
  1184. params.ssid_len = bss->ssid_len;
  1185. if (!wpas_driver_bss_selection(wpa_s)) {
  1186. params.bssid = bss->bssid;
  1187. params.freq = bss->freq;
  1188. }
  1189. } else {
  1190. params.ssid = ssid->ssid;
  1191. params.ssid_len = ssid->ssid_len;
  1192. }
  1193. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  1194. wpa_s->conf->ap_scan == 2) {
  1195. params.bssid = ssid->bssid;
  1196. params.fixed_bssid = 1;
  1197. }
  1198. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1199. params.freq == 0)
  1200. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1201. params.wpa_ie = wpa_ie;
  1202. params.wpa_ie_len = wpa_ie_len;
  1203. params.pairwise_suite = cipher_pairwise;
  1204. params.group_suite = cipher_group;
  1205. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1206. params.wpa_proto = wpa_s->wpa_proto;
  1207. params.auth_alg = algs;
  1208. params.mode = ssid->mode;
  1209. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1210. if (ssid->wep_key_len[i])
  1211. params.wep_key[i] = ssid->wep_key[i];
  1212. params.wep_key_len[i] = ssid->wep_key_len[i];
  1213. }
  1214. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1215. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1216. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1217. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1218. params.passphrase = ssid->passphrase;
  1219. if (ssid->psk_set)
  1220. params.psk = ssid->psk;
  1221. }
  1222. params.drop_unencrypted = use_crypt;
  1223. #ifdef CONFIG_IEEE80211W
  1224. params.mgmt_frame_protection = ssid->ieee80211w;
  1225. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1226. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1227. struct wpa_ie_data ie;
  1228. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1229. ie.capabilities &
  1230. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1231. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1232. "MFP: require MFP");
  1233. params.mgmt_frame_protection =
  1234. MGMT_FRAME_PROTECTION_REQUIRED;
  1235. }
  1236. }
  1237. #endif /* CONFIG_IEEE80211W */
  1238. params.p2p = ssid->p2p_group;
  1239. if (wpa_s->parent->set_sta_uapsd)
  1240. params.uapsd = wpa_s->parent->sta_uapsd;
  1241. else
  1242. params.uapsd = -1;
  1243. #ifdef CONFIG_HT_OVERRIDES
  1244. os_memset(&htcaps, 0, sizeof(htcaps));
  1245. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1246. params.htcaps = (u8 *) &htcaps;
  1247. params.htcaps_mask = (u8 *) &htcaps_mask;
  1248. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  1249. #endif /* CONFIG_HT_OVERRIDES */
  1250. ret = wpa_drv_associate(wpa_s, &params);
  1251. if (ret < 0) {
  1252. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1253. "failed");
  1254. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1255. /*
  1256. * The driver is known to mean what is saying, so we
  1257. * can stop right here; the association will not
  1258. * succeed.
  1259. */
  1260. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1261. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1262. return;
  1263. }
  1264. /* try to continue anyway; new association will be tried again
  1265. * after timeout */
  1266. assoc_failed = 1;
  1267. }
  1268. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1269. /* Set the key after the association just in case association
  1270. * cleared the previously configured key. */
  1271. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1272. /* No need to timeout authentication since there is no key
  1273. * management. */
  1274. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1275. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1276. #ifdef CONFIG_IBSS_RSN
  1277. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1278. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1279. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1280. /*
  1281. * RSN IBSS authentication is per-STA and we can disable the
  1282. * per-BSSID authentication.
  1283. */
  1284. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1285. #endif /* CONFIG_IBSS_RSN */
  1286. } else {
  1287. /* Timeout for IEEE 802.11 authentication and association */
  1288. int timeout = 60;
  1289. if (assoc_failed) {
  1290. /* give IBSS a bit more time */
  1291. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1292. } else if (wpa_s->conf->ap_scan == 1) {
  1293. /* give IBSS a bit more time */
  1294. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1295. }
  1296. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1297. }
  1298. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1299. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1300. /* Set static WEP keys again */
  1301. wpa_set_wep_keys(wpa_s, ssid);
  1302. }
  1303. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1304. /*
  1305. * Do not allow EAP session resumption between different
  1306. * network configurations.
  1307. */
  1308. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1309. }
  1310. old_ssid = wpa_s->current_ssid;
  1311. wpa_s->current_ssid = ssid;
  1312. wpa_s->current_bss = bss;
  1313. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1314. wpa_supplicant_initiate_eapol(wpa_s);
  1315. if (old_ssid != wpa_s->current_ssid)
  1316. wpas_notify_network_changed(wpa_s);
  1317. }
  1318. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1319. const u8 *addr)
  1320. {
  1321. struct wpa_ssid *old_ssid;
  1322. wpa_clear_keys(wpa_s, addr);
  1323. wpa_supplicant_mark_disassoc(wpa_s);
  1324. old_ssid = wpa_s->current_ssid;
  1325. wpa_s->current_ssid = NULL;
  1326. wpa_s->current_bss = NULL;
  1327. wpa_sm_set_config(wpa_s->wpa, NULL);
  1328. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1329. if (old_ssid != wpa_s->current_ssid)
  1330. wpas_notify_network_changed(wpa_s);
  1331. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1332. }
  1333. /**
  1334. * wpa_supplicant_disassociate - Disassociate the current connection
  1335. * @wpa_s: Pointer to wpa_supplicant data
  1336. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1337. *
  1338. * This function is used to request %wpa_supplicant to disassociate with the
  1339. * current AP.
  1340. */
  1341. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1342. int reason_code)
  1343. {
  1344. u8 *addr = NULL;
  1345. union wpa_event_data event;
  1346. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1347. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1348. addr = wpa_s->bssid;
  1349. os_memset(&event, 0, sizeof(event));
  1350. event.disassoc_info.reason_code = (u16) reason_code;
  1351. event.disassoc_info.locally_generated = 1;
  1352. wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
  1353. }
  1354. wpa_supplicant_clear_connection(wpa_s, addr);
  1355. }
  1356. /**
  1357. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1358. * @wpa_s: Pointer to wpa_supplicant data
  1359. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1360. *
  1361. * This function is used to request %wpa_supplicant to deauthenticate from the
  1362. * current AP.
  1363. */
  1364. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1365. int reason_code)
  1366. {
  1367. u8 *addr = NULL;
  1368. union wpa_event_data event;
  1369. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1370. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
  1371. addr = wpa_s->bssid;
  1372. os_memset(&event, 0, sizeof(event));
  1373. event.deauth_info.reason_code = (u16) reason_code;
  1374. event.deauth_info.locally_generated = 1;
  1375. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  1376. }
  1377. wpa_supplicant_clear_connection(wpa_s, addr);
  1378. }
  1379. /**
  1380. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1381. * @wpa_s: wpa_supplicant structure for a network interface
  1382. * @ssid: wpa_ssid structure for a configured network or %NULL
  1383. *
  1384. * Enables the specified network or all networks if no network specified.
  1385. */
  1386. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1387. struct wpa_ssid *ssid)
  1388. {
  1389. struct wpa_ssid *other_ssid;
  1390. int was_disabled;
  1391. if (ssid == NULL) {
  1392. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1393. other_ssid = other_ssid->next) {
  1394. if (other_ssid->disabled == 2)
  1395. continue; /* do not change persistent P2P group
  1396. * data */
  1397. if (other_ssid == wpa_s->current_ssid &&
  1398. other_ssid->disabled)
  1399. wpa_s->reassociate = 1;
  1400. was_disabled = other_ssid->disabled;
  1401. other_ssid->disabled = 0;
  1402. if (was_disabled != other_ssid->disabled)
  1403. wpas_notify_network_enabled_changed(
  1404. wpa_s, other_ssid);
  1405. }
  1406. if (wpa_s->reassociate)
  1407. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1408. } else if (ssid->disabled && ssid->disabled != 2) {
  1409. if (wpa_s->current_ssid == NULL) {
  1410. /*
  1411. * Try to reassociate since there is no current
  1412. * configuration and a new network was made available.
  1413. */
  1414. wpa_s->reassociate = 1;
  1415. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1416. }
  1417. was_disabled = ssid->disabled;
  1418. ssid->disabled = 0;
  1419. if (was_disabled != ssid->disabled)
  1420. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1421. }
  1422. }
  1423. /**
  1424. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1425. * @wpa_s: wpa_supplicant structure for a network interface
  1426. * @ssid: wpa_ssid structure for a configured network or %NULL
  1427. *
  1428. * Disables the specified network or all networks if no network specified.
  1429. */
  1430. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1431. struct wpa_ssid *ssid)
  1432. {
  1433. struct wpa_ssid *other_ssid;
  1434. int was_disabled;
  1435. if (ssid == NULL) {
  1436. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1437. other_ssid = other_ssid->next) {
  1438. was_disabled = other_ssid->disabled;
  1439. if (was_disabled == 2)
  1440. continue; /* do not change persistent P2P group
  1441. * data */
  1442. other_ssid->disabled = 1;
  1443. if (was_disabled != other_ssid->disabled)
  1444. wpas_notify_network_enabled_changed(
  1445. wpa_s, other_ssid);
  1446. }
  1447. if (wpa_s->current_ssid)
  1448. wpa_supplicant_disassociate(
  1449. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1450. } else if (ssid->disabled != 2) {
  1451. if (ssid == wpa_s->current_ssid)
  1452. wpa_supplicant_disassociate(
  1453. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1454. was_disabled = ssid->disabled;
  1455. ssid->disabled = 1;
  1456. if (was_disabled != ssid->disabled)
  1457. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1458. }
  1459. }
  1460. /**
  1461. * wpa_supplicant_select_network - Attempt association with a network
  1462. * @wpa_s: wpa_supplicant structure for a network interface
  1463. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1464. */
  1465. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1466. struct wpa_ssid *ssid)
  1467. {
  1468. struct wpa_ssid *other_ssid;
  1469. int disconnected = 0;
  1470. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  1471. wpa_supplicant_disassociate(
  1472. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1473. disconnected = 1;
  1474. }
  1475. /*
  1476. * Mark all other networks disabled or mark all networks enabled if no
  1477. * network specified.
  1478. */
  1479. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1480. other_ssid = other_ssid->next) {
  1481. int was_disabled = other_ssid->disabled;
  1482. if (was_disabled == 2)
  1483. continue; /* do not change persistent P2P group data */
  1484. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1485. if (was_disabled != other_ssid->disabled)
  1486. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1487. }
  1488. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1489. /* We are already associated with the selected network */
  1490. wpa_printf(MSG_DEBUG, "Already associated with the "
  1491. "selected network - do nothing");
  1492. return;
  1493. }
  1494. if (ssid)
  1495. wpa_s->current_ssid = ssid;
  1496. wpa_s->connect_without_scan = NULL;
  1497. wpa_s->disconnected = 0;
  1498. wpa_s->reassociate = 1;
  1499. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  1500. if (ssid)
  1501. wpas_notify_network_selected(wpa_s, ssid);
  1502. }
  1503. /**
  1504. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1505. * @wpa_s: wpa_supplicant structure for a network interface
  1506. * @ap_scan: AP scan mode
  1507. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1508. *
  1509. */
  1510. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1511. {
  1512. int old_ap_scan;
  1513. if (ap_scan < 0 || ap_scan > 2)
  1514. return -1;
  1515. #ifdef ANDROID
  1516. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  1517. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  1518. wpa_s->wpa_state < WPA_COMPLETED) {
  1519. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  1520. "associating", wpa_s->conf->ap_scan, ap_scan);
  1521. return 0;
  1522. }
  1523. #endif /* ANDROID */
  1524. old_ap_scan = wpa_s->conf->ap_scan;
  1525. wpa_s->conf->ap_scan = ap_scan;
  1526. if (old_ap_scan != wpa_s->conf->ap_scan)
  1527. wpas_notify_ap_scan_changed(wpa_s);
  1528. return 0;
  1529. }
  1530. /**
  1531. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1532. * @wpa_s: wpa_supplicant structure for a network interface
  1533. * @expire_age: Expiration age in seconds
  1534. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1535. *
  1536. */
  1537. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1538. unsigned int bss_expire_age)
  1539. {
  1540. if (bss_expire_age < 10) {
  1541. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1542. bss_expire_age);
  1543. return -1;
  1544. }
  1545. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1546. bss_expire_age);
  1547. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1548. return 0;
  1549. }
  1550. /**
  1551. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1552. * @wpa_s: wpa_supplicant structure for a network interface
  1553. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1554. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1555. *
  1556. */
  1557. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1558. unsigned int bss_expire_count)
  1559. {
  1560. if (bss_expire_count < 1) {
  1561. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1562. bss_expire_count);
  1563. return -1;
  1564. }
  1565. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1566. bss_expire_count);
  1567. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1568. return 0;
  1569. }
  1570. /**
  1571. * wpa_supplicant_set_debug_params - Set global debug params
  1572. * @global: wpa_global structure
  1573. * @debug_level: debug level
  1574. * @debug_timestamp: determines if show timestamp in debug data
  1575. * @debug_show_keys: determines if show keys in debug data
  1576. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1577. */
  1578. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1579. int debug_timestamp, int debug_show_keys)
  1580. {
  1581. int old_level, old_timestamp, old_show_keys;
  1582. /* check for allowed debuglevels */
  1583. if (debug_level != MSG_EXCESSIVE &&
  1584. debug_level != MSG_MSGDUMP &&
  1585. debug_level != MSG_DEBUG &&
  1586. debug_level != MSG_INFO &&
  1587. debug_level != MSG_WARNING &&
  1588. debug_level != MSG_ERROR)
  1589. return -1;
  1590. old_level = wpa_debug_level;
  1591. old_timestamp = wpa_debug_timestamp;
  1592. old_show_keys = wpa_debug_show_keys;
  1593. wpa_debug_level = debug_level;
  1594. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1595. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1596. if (wpa_debug_level != old_level)
  1597. wpas_notify_debug_level_changed(global);
  1598. if (wpa_debug_timestamp != old_timestamp)
  1599. wpas_notify_debug_timestamp_changed(global);
  1600. if (wpa_debug_show_keys != old_show_keys)
  1601. wpas_notify_debug_show_keys_changed(global);
  1602. return 0;
  1603. }
  1604. /**
  1605. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1606. * @wpa_s: Pointer to wpa_supplicant data
  1607. * Returns: A pointer to the current network structure or %NULL on failure
  1608. */
  1609. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1610. {
  1611. struct wpa_ssid *entry;
  1612. u8 ssid[MAX_SSID_LEN];
  1613. int res;
  1614. size_t ssid_len;
  1615. u8 bssid[ETH_ALEN];
  1616. int wired;
  1617. res = wpa_drv_get_ssid(wpa_s, ssid);
  1618. if (res < 0) {
  1619. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1620. "driver");
  1621. return NULL;
  1622. }
  1623. ssid_len = res;
  1624. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1625. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1626. "driver");
  1627. return NULL;
  1628. }
  1629. wired = wpa_s->conf->ap_scan == 0 &&
  1630. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1631. entry = wpa_s->conf->ssid;
  1632. while (entry) {
  1633. if (!entry->disabled &&
  1634. ((ssid_len == entry->ssid_len &&
  1635. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1636. (!entry->bssid_set ||
  1637. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1638. return entry;
  1639. #ifdef CONFIG_WPS
  1640. if (!entry->disabled &&
  1641. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1642. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1643. (!entry->bssid_set ||
  1644. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1645. return entry;
  1646. #endif /* CONFIG_WPS */
  1647. if (!entry->disabled && entry->bssid_set &&
  1648. entry->ssid_len == 0 &&
  1649. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1650. return entry;
  1651. entry = entry->next;
  1652. }
  1653. return NULL;
  1654. }
  1655. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  1656. {
  1657. struct wpa_global *global = wpa_s->global;
  1658. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  1659. global->drv_priv[i] = wpa_drivers[i]->global_init();
  1660. if (global->drv_priv[i] == NULL) {
  1661. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  1662. "'%s'", wpa_drivers[i]->name);
  1663. return -1;
  1664. }
  1665. }
  1666. wpa_s->driver = wpa_drivers[i];
  1667. wpa_s->global_drv_priv = global->drv_priv[i];
  1668. return 0;
  1669. }
  1670. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1671. const char *name)
  1672. {
  1673. int i;
  1674. size_t len;
  1675. const char *pos, *driver = name;
  1676. if (wpa_s == NULL)
  1677. return -1;
  1678. if (wpa_drivers[0] == NULL) {
  1679. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1680. "wpa_supplicant");
  1681. return -1;
  1682. }
  1683. if (name == NULL) {
  1684. /* default to first driver in the list */
  1685. return select_driver(wpa_s, 0);
  1686. }
  1687. do {
  1688. pos = os_strchr(driver, ',');
  1689. if (pos)
  1690. len = pos - driver;
  1691. else
  1692. len = os_strlen(driver);
  1693. for (i = 0; wpa_drivers[i]; i++) {
  1694. if (os_strlen(wpa_drivers[i]->name) == len &&
  1695. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1696. 0) {
  1697. /* First driver that succeeds wins */
  1698. if (select_driver(wpa_s, i) == 0)
  1699. return 0;
  1700. }
  1701. }
  1702. driver = pos + 1;
  1703. } while (pos);
  1704. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1705. return -1;
  1706. }
  1707. /**
  1708. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1709. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1710. * with struct wpa_driver_ops::init()
  1711. * @src_addr: Source address of the EAPOL frame
  1712. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1713. * @len: Length of the EAPOL data
  1714. *
  1715. * This function is called for each received EAPOL frame. Most driver
  1716. * interfaces rely on more generic OS mechanism for receiving frames through
  1717. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1718. * take care of received EAPOL frames and deliver them to the core supplicant
  1719. * code by calling this function.
  1720. */
  1721. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1722. const u8 *buf, size_t len)
  1723. {
  1724. struct wpa_supplicant *wpa_s = ctx;
  1725. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1726. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1727. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1728. /*
  1729. * There is possible race condition between receiving the
  1730. * association event and the EAPOL frame since they are coming
  1731. * through different paths from the driver. In order to avoid
  1732. * issues in trying to process the EAPOL frame before receiving
  1733. * association information, lets queue it for processing until
  1734. * the association event is received.
  1735. */
  1736. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1737. "of received EAPOL frame");
  1738. wpabuf_free(wpa_s->pending_eapol_rx);
  1739. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1740. if (wpa_s->pending_eapol_rx) {
  1741. os_get_time(&wpa_s->pending_eapol_rx_time);
  1742. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1743. ETH_ALEN);
  1744. }
  1745. return;
  1746. }
  1747. #ifdef CONFIG_AP
  1748. if (wpa_s->ap_iface) {
  1749. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1750. return;
  1751. }
  1752. #endif /* CONFIG_AP */
  1753. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1754. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1755. "no key management is configured");
  1756. return;
  1757. }
  1758. if (wpa_s->eapol_received == 0 &&
  1759. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1760. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1761. wpa_s->wpa_state != WPA_COMPLETED) &&
  1762. (wpa_s->current_ssid == NULL ||
  1763. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1764. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1765. wpa_supplicant_req_auth_timeout(
  1766. wpa_s,
  1767. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1768. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1769. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1770. 70 : 10, 0);
  1771. }
  1772. wpa_s->eapol_received++;
  1773. if (wpa_s->countermeasures) {
  1774. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1775. "EAPOL packet");
  1776. return;
  1777. }
  1778. #ifdef CONFIG_IBSS_RSN
  1779. if (wpa_s->current_ssid &&
  1780. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1781. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1782. return;
  1783. }
  1784. #endif /* CONFIG_IBSS_RSN */
  1785. /* Source address of the incoming EAPOL frame could be compared to the
  1786. * current BSSID. However, it is possible that a centralized
  1787. * Authenticator could be using another MAC address than the BSSID of
  1788. * an AP, so just allow any address to be used for now. The replies are
  1789. * still sent to the current BSSID (if available), though. */
  1790. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1791. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1792. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1793. return;
  1794. wpa_drv_poll(wpa_s);
  1795. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1796. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1797. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1798. /*
  1799. * Set portValid = TRUE here since we are going to skip 4-way
  1800. * handshake processing which would normally set portValid. We
  1801. * need this to allow the EAPOL state machines to be completed
  1802. * without going through EAPOL-Key handshake.
  1803. */
  1804. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1805. }
  1806. }
  1807. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  1808. {
  1809. if (wpa_s->driver->send_eapol) {
  1810. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1811. if (addr)
  1812. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1813. } else if (!(wpa_s->drv_flags &
  1814. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1815. l2_packet_deinit(wpa_s->l2);
  1816. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1817. wpa_drv_get_mac_addr(wpa_s),
  1818. ETH_P_EAPOL,
  1819. wpa_supplicant_rx_eapol, wpa_s, 0);
  1820. if (wpa_s->l2 == NULL)
  1821. return -1;
  1822. } else {
  1823. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1824. if (addr)
  1825. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1826. }
  1827. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1828. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  1829. return -1;
  1830. }
  1831. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  1832. MAC2STR(wpa_s->own_addr));
  1833. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  1834. return 0;
  1835. }
  1836. /**
  1837. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1838. * @wpa_s: Pointer to wpa_supplicant data
  1839. * Returns: 0 on success, -1 on failure
  1840. *
  1841. * This function is called to initialize driver interface parameters.
  1842. * wpa_drv_init() must have been called before this function to initialize the
  1843. * driver interface.
  1844. */
  1845. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1846. {
  1847. static int interface_count = 0;
  1848. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  1849. return -1;
  1850. if (wpa_s->bridge_ifname[0]) {
  1851. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  1852. "interface '%s'", wpa_s->bridge_ifname);
  1853. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1854. wpa_s->own_addr,
  1855. ETH_P_EAPOL,
  1856. wpa_supplicant_rx_eapol, wpa_s,
  1857. 0);
  1858. if (wpa_s->l2_br == NULL) {
  1859. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  1860. "connection for the bridge interface '%s'",
  1861. wpa_s->bridge_ifname);
  1862. return -1;
  1863. }
  1864. }
  1865. wpa_clear_keys(wpa_s, NULL);
  1866. /* Make sure that TKIP countermeasures are not left enabled (could
  1867. * happen if wpa_supplicant is killed during countermeasures. */
  1868. wpa_drv_set_countermeasures(wpa_s, 0);
  1869. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1870. wpa_drv_flush_pmkid(wpa_s);
  1871. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  1872. wpa_s->prev_scan_wildcard = 0;
  1873. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  1874. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  1875. 100000))
  1876. wpa_supplicant_req_scan(wpa_s, interface_count,
  1877. 100000);
  1878. interface_count++;
  1879. } else
  1880. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1881. return 0;
  1882. }
  1883. static int wpa_supplicant_daemon(const char *pid_file)
  1884. {
  1885. wpa_printf(MSG_DEBUG, "Daemonize..");
  1886. return os_daemonize(pid_file);
  1887. }
  1888. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1889. {
  1890. struct wpa_supplicant *wpa_s;
  1891. wpa_s = os_zalloc(sizeof(*wpa_s));
  1892. if (wpa_s == NULL)
  1893. return NULL;
  1894. wpa_s->scan_req = 1;
  1895. wpa_s->scan_interval = 5;
  1896. wpa_s->new_connection = 1;
  1897. wpa_s->parent = wpa_s;
  1898. wpa_s->sched_scanning = 0;
  1899. return wpa_s;
  1900. }
  1901. #ifdef CONFIG_HT_OVERRIDES
  1902. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  1903. struct ieee80211_ht_capabilities *htcaps,
  1904. struct ieee80211_ht_capabilities *htcaps_mask,
  1905. const char *ht_mcs)
  1906. {
  1907. /* parse ht_mcs into hex array */
  1908. int i;
  1909. const char *tmp = ht_mcs;
  1910. char *end = NULL;
  1911. /* If ht_mcs is null, do not set anything */
  1912. if (!ht_mcs)
  1913. return 0;
  1914. /* This is what we are setting in the kernel */
  1915. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  1916. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  1917. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  1918. errno = 0;
  1919. long v = strtol(tmp, &end, 16);
  1920. if (errno == 0) {
  1921. wpa_msg(wpa_s, MSG_DEBUG,
  1922. "htcap value[%i]: %ld end: %p tmp: %p",
  1923. i, v, end, tmp);
  1924. if (end == tmp)
  1925. break;
  1926. htcaps->supported_mcs_set[i] = v;
  1927. tmp = end;
  1928. } else {
  1929. wpa_msg(wpa_s, MSG_ERROR,
  1930. "Failed to parse ht-mcs: %s, error: %s\n",
  1931. ht_mcs, strerror(errno));
  1932. return -1;
  1933. }
  1934. }
  1935. /*
  1936. * If we were able to parse any values, then set mask for the MCS set.
  1937. */
  1938. if (i) {
  1939. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  1940. IEEE80211_HT_MCS_MASK_LEN - 1);
  1941. /* skip the 3 reserved bits */
  1942. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  1943. 0x1f;
  1944. }
  1945. return 0;
  1946. }
  1947. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  1948. struct ieee80211_ht_capabilities *htcaps,
  1949. struct ieee80211_ht_capabilities *htcaps_mask,
  1950. int disabled)
  1951. {
  1952. u16 msk;
  1953. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  1954. if (disabled == -1)
  1955. return 0;
  1956. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  1957. htcaps_mask->ht_capabilities_info |= msk;
  1958. if (disabled)
  1959. htcaps->ht_capabilities_info &= msk;
  1960. else
  1961. htcaps->ht_capabilities_info |= msk;
  1962. return 0;
  1963. }
  1964. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  1965. struct ieee80211_ht_capabilities *htcaps,
  1966. struct ieee80211_ht_capabilities *htcaps_mask,
  1967. int factor)
  1968. {
  1969. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  1970. if (factor == -1)
  1971. return 0;
  1972. if (factor < 0 || factor > 3) {
  1973. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  1974. "Must be 0-3 or -1", factor);
  1975. return -EINVAL;
  1976. }
  1977. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  1978. htcaps->a_mpdu_params &= ~0x3;
  1979. htcaps->a_mpdu_params |= factor & 0x3;
  1980. return 0;
  1981. }
  1982. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  1983. struct ieee80211_ht_capabilities *htcaps,
  1984. struct ieee80211_ht_capabilities *htcaps_mask,
  1985. int density)
  1986. {
  1987. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  1988. if (density == -1)
  1989. return 0;
  1990. if (density < 0 || density > 7) {
  1991. wpa_msg(wpa_s, MSG_ERROR,
  1992. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  1993. density);
  1994. return -EINVAL;
  1995. }
  1996. htcaps_mask->a_mpdu_params |= 0x1C;
  1997. htcaps->a_mpdu_params &= ~(0x1C);
  1998. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  1999. return 0;
  2000. }
  2001. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  2002. struct ieee80211_ht_capabilities *htcaps,
  2003. struct ieee80211_ht_capabilities *htcaps_mask,
  2004. int disabled)
  2005. {
  2006. /* Masking these out disables HT40 */
  2007. u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  2008. HT_CAP_INFO_SHORT_GI40MHZ);
  2009. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  2010. if (disabled)
  2011. htcaps->ht_capabilities_info &= ~msk;
  2012. else
  2013. htcaps->ht_capabilities_info |= msk;
  2014. htcaps_mask->ht_capabilities_info |= msk;
  2015. return 0;
  2016. }
  2017. void wpa_supplicant_apply_ht_overrides(
  2018. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2019. struct wpa_driver_associate_params *params)
  2020. {
  2021. struct ieee80211_ht_capabilities *htcaps;
  2022. struct ieee80211_ht_capabilities *htcaps_mask;
  2023. if (!ssid)
  2024. return;
  2025. params->disable_ht = ssid->disable_ht;
  2026. if (!params->htcaps || !params->htcaps_mask)
  2027. return;
  2028. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  2029. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  2030. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  2031. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  2032. ssid->disable_max_amsdu);
  2033. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  2034. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  2035. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  2036. }
  2037. #endif /* CONFIG_HT_OVERRIDES */
  2038. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  2039. {
  2040. #ifdef PCSC_FUNCS
  2041. size_t len;
  2042. if (!wpa_s->conf->pcsc_reader)
  2043. return 0;
  2044. wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
  2045. if (!wpa_s->scard)
  2046. return 1;
  2047. if (wpa_s->conf->pcsc_pin &&
  2048. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  2049. scard_deinit(wpa_s->scard);
  2050. wpa_s->scard = NULL;
  2051. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  2052. return -1;
  2053. }
  2054. len = sizeof(wpa_s->imsi) - 1;
  2055. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  2056. scard_deinit(wpa_s->scard);
  2057. wpa_s->scard = NULL;
  2058. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  2059. return -1;
  2060. }
  2061. wpa_s->imsi[len] = '\0';
  2062. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  2063. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  2064. wpa_s->imsi, wpa_s->mnc_len);
  2065. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  2066. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  2067. #endif /* PCSC_FUNCS */
  2068. return 0;
  2069. }
  2070. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  2071. struct wpa_interface *iface)
  2072. {
  2073. const char *ifname, *driver;
  2074. struct wpa_driver_capa capa;
  2075. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  2076. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  2077. iface->confname ? iface->confname : "N/A",
  2078. iface->driver ? iface->driver : "default",
  2079. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  2080. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  2081. if (iface->confname) {
  2082. #ifdef CONFIG_BACKEND_FILE
  2083. wpa_s->confname = os_rel2abs_path(iface->confname);
  2084. if (wpa_s->confname == NULL) {
  2085. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  2086. "for configuration file '%s'.",
  2087. iface->confname);
  2088. return -1;
  2089. }
  2090. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  2091. iface->confname, wpa_s->confname);
  2092. #else /* CONFIG_BACKEND_FILE */
  2093. wpa_s->confname = os_strdup(iface->confname);
  2094. #endif /* CONFIG_BACKEND_FILE */
  2095. wpa_s->conf = wpa_config_read(wpa_s->confname);
  2096. if (wpa_s->conf == NULL) {
  2097. wpa_printf(MSG_ERROR, "Failed to read or parse "
  2098. "configuration '%s'.", wpa_s->confname);
  2099. return -1;
  2100. }
  2101. /*
  2102. * Override ctrl_interface and driver_param if set on command
  2103. * line.
  2104. */
  2105. if (iface->ctrl_interface) {
  2106. os_free(wpa_s->conf->ctrl_interface);
  2107. wpa_s->conf->ctrl_interface =
  2108. os_strdup(iface->ctrl_interface);
  2109. }
  2110. if (iface->driver_param) {
  2111. os_free(wpa_s->conf->driver_param);
  2112. wpa_s->conf->driver_param =
  2113. os_strdup(iface->driver_param);
  2114. }
  2115. } else
  2116. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  2117. iface->driver_param);
  2118. if (wpa_s->conf == NULL) {
  2119. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  2120. return -1;
  2121. }
  2122. if (iface->ifname == NULL) {
  2123. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  2124. return -1;
  2125. }
  2126. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  2127. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  2128. iface->ifname);
  2129. return -1;
  2130. }
  2131. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  2132. if (iface->bridge_ifname) {
  2133. if (os_strlen(iface->bridge_ifname) >=
  2134. sizeof(wpa_s->bridge_ifname)) {
  2135. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  2136. "name '%s'.", iface->bridge_ifname);
  2137. return -1;
  2138. }
  2139. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  2140. sizeof(wpa_s->bridge_ifname));
  2141. }
  2142. /* RSNA Supplicant Key Management - INITIALIZE */
  2143. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  2144. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  2145. /* Initialize driver interface and register driver event handler before
  2146. * L2 receive handler so that association events are processed before
  2147. * EAPOL-Key packets if both become available for the same select()
  2148. * call. */
  2149. driver = iface->driver;
  2150. next_driver:
  2151. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  2152. return -1;
  2153. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  2154. if (wpa_s->drv_priv == NULL) {
  2155. const char *pos;
  2156. pos = driver ? os_strchr(driver, ',') : NULL;
  2157. if (pos) {
  2158. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  2159. "driver interface - try next driver wrapper");
  2160. driver = pos + 1;
  2161. goto next_driver;
  2162. }
  2163. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  2164. "interface");
  2165. return -1;
  2166. }
  2167. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  2168. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  2169. "driver_param '%s'", wpa_s->conf->driver_param);
  2170. return -1;
  2171. }
  2172. ifname = wpa_drv_get_ifname(wpa_s);
  2173. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  2174. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  2175. "interface name with '%s'", ifname);
  2176. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  2177. }
  2178. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  2179. return -1;
  2180. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  2181. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  2182. NULL);
  2183. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  2184. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  2185. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  2186. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  2187. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2188. "dot11RSNAConfigPMKLifetime");
  2189. return -1;
  2190. }
  2191. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  2192. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  2193. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  2194. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2195. "dot11RSNAConfigPMKReauthThreshold");
  2196. return -1;
  2197. }
  2198. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2199. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2200. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2201. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2202. "dot11RSNAConfigSATimeout");
  2203. return -1;
  2204. }
  2205. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  2206. &wpa_s->hw.num_modes,
  2207. &wpa_s->hw.flags);
  2208. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2209. wpa_s->drv_capa_known = 1;
  2210. wpa_s->drv_flags = capa.flags;
  2211. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  2212. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2213. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2214. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2215. wpa_s->max_match_sets = capa.max_match_sets;
  2216. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2217. wpa_s->max_stations = capa.max_stations;
  2218. }
  2219. if (wpa_s->max_remain_on_chan == 0)
  2220. wpa_s->max_remain_on_chan = 1000;
  2221. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2222. return -1;
  2223. #ifdef CONFIG_TDLS
  2224. if (wpa_tdls_init(wpa_s->wpa))
  2225. return -1;
  2226. #endif /* CONFIG_TDLS */
  2227. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2228. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2229. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2230. return -1;
  2231. }
  2232. if (wpas_wps_init(wpa_s))
  2233. return -1;
  2234. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2235. return -1;
  2236. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2237. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2238. if (wpa_s->ctrl_iface == NULL) {
  2239. wpa_printf(MSG_ERROR,
  2240. "Failed to initialize control interface '%s'.\n"
  2241. "You may have another wpa_supplicant process "
  2242. "already running or the file was\n"
  2243. "left by an unclean termination of wpa_supplicant "
  2244. "in which case you will need\n"
  2245. "to manually remove this file before starting "
  2246. "wpa_supplicant again.\n",
  2247. wpa_s->conf->ctrl_interface);
  2248. return -1;
  2249. }
  2250. wpa_s->gas = gas_query_init(wpa_s);
  2251. if (wpa_s->gas == NULL) {
  2252. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2253. return -1;
  2254. }
  2255. #ifdef CONFIG_P2P
  2256. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2257. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2258. return -1;
  2259. }
  2260. #endif /* CONFIG_P2P */
  2261. if (wpa_bss_init(wpa_s) < 0)
  2262. return -1;
  2263. if (pcsc_reader_init(wpa_s) < 0)
  2264. return -1;
  2265. return 0;
  2266. }
  2267. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2268. int notify, int terminate)
  2269. {
  2270. if (wpa_s->drv_priv) {
  2271. wpa_supplicant_deauthenticate(wpa_s,
  2272. WLAN_REASON_DEAUTH_LEAVING);
  2273. wpa_drv_set_countermeasures(wpa_s, 0);
  2274. wpa_clear_keys(wpa_s, NULL);
  2275. }
  2276. wpa_supplicant_cleanup(wpa_s);
  2277. if (wpa_s->drv_priv)
  2278. wpa_drv_deinit(wpa_s);
  2279. if (notify)
  2280. wpas_notify_iface_removed(wpa_s);
  2281. if (terminate)
  2282. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  2283. if (wpa_s->ctrl_iface) {
  2284. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  2285. wpa_s->ctrl_iface = NULL;
  2286. }
  2287. if (wpa_s->conf != NULL) {
  2288. wpa_config_free(wpa_s->conf);
  2289. wpa_s->conf = NULL;
  2290. }
  2291. }
  2292. /**
  2293. * wpa_supplicant_add_iface - Add a new network interface
  2294. * @global: Pointer to global data from wpa_supplicant_init()
  2295. * @iface: Interface configuration options
  2296. * Returns: Pointer to the created interface or %NULL on failure
  2297. *
  2298. * This function is used to add new network interfaces for %wpa_supplicant.
  2299. * This can be called before wpa_supplicant_run() to add interfaces before the
  2300. * main event loop has been started. In addition, new interfaces can be added
  2301. * dynamically while %wpa_supplicant is already running. This could happen,
  2302. * e.g., when a hotplug network adapter is inserted.
  2303. */
  2304. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2305. struct wpa_interface *iface)
  2306. {
  2307. struct wpa_supplicant *wpa_s;
  2308. struct wpa_interface t_iface;
  2309. struct wpa_ssid *ssid;
  2310. if (global == NULL || iface == NULL)
  2311. return NULL;
  2312. wpa_s = wpa_supplicant_alloc();
  2313. if (wpa_s == NULL)
  2314. return NULL;
  2315. wpa_s->global = global;
  2316. t_iface = *iface;
  2317. if (global->params.override_driver) {
  2318. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2319. "('%s' -> '%s')",
  2320. iface->driver, global->params.override_driver);
  2321. t_iface.driver = global->params.override_driver;
  2322. }
  2323. if (global->params.override_ctrl_interface) {
  2324. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2325. "ctrl_interface ('%s' -> '%s')",
  2326. iface->ctrl_interface,
  2327. global->params.override_ctrl_interface);
  2328. t_iface.ctrl_interface =
  2329. global->params.override_ctrl_interface;
  2330. }
  2331. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2332. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2333. iface->ifname);
  2334. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  2335. os_free(wpa_s);
  2336. return NULL;
  2337. }
  2338. /* Notify the control interfaces about new iface */
  2339. if (wpas_notify_iface_added(wpa_s)) {
  2340. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  2341. os_free(wpa_s);
  2342. return NULL;
  2343. }
  2344. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2345. wpas_notify_network_added(wpa_s, ssid);
  2346. wpa_s->next = global->ifaces;
  2347. global->ifaces = wpa_s;
  2348. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2349. return wpa_s;
  2350. }
  2351. /**
  2352. * wpa_supplicant_remove_iface - Remove a network interface
  2353. * @global: Pointer to global data from wpa_supplicant_init()
  2354. * @wpa_s: Pointer to the network interface to be removed
  2355. * Returns: 0 if interface was removed, -1 if interface was not found
  2356. *
  2357. * This function can be used to dynamically remove network interfaces from
  2358. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2359. * addition, this function is used to remove all remaining interfaces when
  2360. * %wpa_supplicant is terminated.
  2361. */
  2362. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2363. struct wpa_supplicant *wpa_s,
  2364. int terminate)
  2365. {
  2366. struct wpa_supplicant *prev;
  2367. /* Remove interface from the global list of interfaces */
  2368. prev = global->ifaces;
  2369. if (prev == wpa_s) {
  2370. global->ifaces = wpa_s->next;
  2371. } else {
  2372. while (prev && prev->next != wpa_s)
  2373. prev = prev->next;
  2374. if (prev == NULL)
  2375. return -1;
  2376. prev->next = wpa_s->next;
  2377. }
  2378. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2379. if (global->p2p_group_formation == wpa_s)
  2380. global->p2p_group_formation = NULL;
  2381. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  2382. os_free(wpa_s);
  2383. return 0;
  2384. }
  2385. /**
  2386. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2387. * @wpa_s: Pointer to the network interface
  2388. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2389. */
  2390. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2391. {
  2392. const char *eapol_method;
  2393. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2394. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2395. return "NO-EAP";
  2396. }
  2397. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2398. if (eapol_method == NULL)
  2399. return "UNKNOWN-EAP";
  2400. return eapol_method;
  2401. }
  2402. /**
  2403. * wpa_supplicant_get_iface - Get a new network interface
  2404. * @global: Pointer to global data from wpa_supplicant_init()
  2405. * @ifname: Interface name
  2406. * Returns: Pointer to the interface or %NULL if not found
  2407. */
  2408. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2409. const char *ifname)
  2410. {
  2411. struct wpa_supplicant *wpa_s;
  2412. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2413. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2414. return wpa_s;
  2415. }
  2416. return NULL;
  2417. }
  2418. #ifndef CONFIG_NO_WPA_MSG
  2419. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2420. {
  2421. struct wpa_supplicant *wpa_s = ctx;
  2422. if (wpa_s == NULL)
  2423. return NULL;
  2424. return wpa_s->ifname;
  2425. }
  2426. #endif /* CONFIG_NO_WPA_MSG */
  2427. /**
  2428. * wpa_supplicant_init - Initialize %wpa_supplicant
  2429. * @params: Parameters for %wpa_supplicant
  2430. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2431. *
  2432. * This function is used to initialize %wpa_supplicant. After successful
  2433. * initialization, the returned data pointer can be used to add and remove
  2434. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2435. */
  2436. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2437. {
  2438. struct wpa_global *global;
  2439. int ret, i;
  2440. if (params == NULL)
  2441. return NULL;
  2442. #ifdef CONFIG_DRIVER_NDIS
  2443. {
  2444. void driver_ndis_init_ops(void);
  2445. driver_ndis_init_ops();
  2446. }
  2447. #endif /* CONFIG_DRIVER_NDIS */
  2448. #ifndef CONFIG_NO_WPA_MSG
  2449. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2450. #endif /* CONFIG_NO_WPA_MSG */
  2451. wpa_debug_open_file(params->wpa_debug_file_path);
  2452. if (params->wpa_debug_syslog)
  2453. wpa_debug_open_syslog();
  2454. ret = eap_register_methods();
  2455. if (ret) {
  2456. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2457. if (ret == -2)
  2458. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2459. "the same EAP type.");
  2460. return NULL;
  2461. }
  2462. global = os_zalloc(sizeof(*global));
  2463. if (global == NULL)
  2464. return NULL;
  2465. dl_list_init(&global->p2p_srv_bonjour);
  2466. dl_list_init(&global->p2p_srv_upnp);
  2467. global->params.daemonize = params->daemonize;
  2468. global->params.wait_for_monitor = params->wait_for_monitor;
  2469. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2470. if (params->pid_file)
  2471. global->params.pid_file = os_strdup(params->pid_file);
  2472. if (params->ctrl_interface)
  2473. global->params.ctrl_interface =
  2474. os_strdup(params->ctrl_interface);
  2475. if (params->override_driver)
  2476. global->params.override_driver =
  2477. os_strdup(params->override_driver);
  2478. if (params->override_ctrl_interface)
  2479. global->params.override_ctrl_interface =
  2480. os_strdup(params->override_ctrl_interface);
  2481. wpa_debug_level = global->params.wpa_debug_level =
  2482. params->wpa_debug_level;
  2483. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2484. params->wpa_debug_show_keys;
  2485. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2486. params->wpa_debug_timestamp;
  2487. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2488. if (eloop_init()) {
  2489. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2490. wpa_supplicant_deinit(global);
  2491. return NULL;
  2492. }
  2493. random_init(params->entropy_file);
  2494. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2495. if (global->ctrl_iface == NULL) {
  2496. wpa_supplicant_deinit(global);
  2497. return NULL;
  2498. }
  2499. if (wpas_notify_supplicant_initialized(global)) {
  2500. wpa_supplicant_deinit(global);
  2501. return NULL;
  2502. }
  2503. for (i = 0; wpa_drivers[i]; i++)
  2504. global->drv_count++;
  2505. if (global->drv_count == 0) {
  2506. wpa_printf(MSG_ERROR, "No drivers enabled");
  2507. wpa_supplicant_deinit(global);
  2508. return NULL;
  2509. }
  2510. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2511. if (global->drv_priv == NULL) {
  2512. wpa_supplicant_deinit(global);
  2513. return NULL;
  2514. }
  2515. return global;
  2516. }
  2517. /**
  2518. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2519. * @global: Pointer to global data from wpa_supplicant_init()
  2520. * Returns: 0 after successful event loop run, -1 on failure
  2521. *
  2522. * This function starts the main event loop and continues running as long as
  2523. * there are any remaining events. In most cases, this function is running as
  2524. * long as the %wpa_supplicant process in still in use.
  2525. */
  2526. int wpa_supplicant_run(struct wpa_global *global)
  2527. {
  2528. struct wpa_supplicant *wpa_s;
  2529. if (global->params.daemonize &&
  2530. wpa_supplicant_daemon(global->params.pid_file))
  2531. return -1;
  2532. if (global->params.wait_for_monitor) {
  2533. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2534. if (wpa_s->ctrl_iface)
  2535. wpa_supplicant_ctrl_iface_wait(
  2536. wpa_s->ctrl_iface);
  2537. }
  2538. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2539. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2540. eloop_run();
  2541. return 0;
  2542. }
  2543. /**
  2544. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2545. * @global: Pointer to global data from wpa_supplicant_init()
  2546. *
  2547. * This function is called to deinitialize %wpa_supplicant and to free all
  2548. * allocated resources. Remaining network interfaces will also be removed.
  2549. */
  2550. void wpa_supplicant_deinit(struct wpa_global *global)
  2551. {
  2552. int i;
  2553. if (global == NULL)
  2554. return;
  2555. #ifdef CONFIG_P2P
  2556. wpas_p2p_deinit_global(global);
  2557. #endif /* CONFIG_P2P */
  2558. while (global->ifaces)
  2559. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  2560. if (global->ctrl_iface)
  2561. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2562. wpas_notify_supplicant_deinitialized(global);
  2563. eap_peer_unregister_methods();
  2564. #ifdef CONFIG_AP
  2565. eap_server_unregister_methods();
  2566. #endif /* CONFIG_AP */
  2567. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2568. if (!global->drv_priv[i])
  2569. continue;
  2570. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2571. }
  2572. os_free(global->drv_priv);
  2573. random_deinit();
  2574. eloop_destroy();
  2575. if (global->params.pid_file) {
  2576. os_daemonize_terminate(global->params.pid_file);
  2577. os_free(global->params.pid_file);
  2578. }
  2579. os_free(global->params.ctrl_interface);
  2580. os_free(global->params.override_driver);
  2581. os_free(global->params.override_ctrl_interface);
  2582. os_free(global->p2p_disallow_freq);
  2583. os_free(global);
  2584. wpa_debug_close_syslog();
  2585. wpa_debug_close_file();
  2586. }
  2587. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2588. {
  2589. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2590. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2591. char country[3];
  2592. country[0] = wpa_s->conf->country[0];
  2593. country[1] = wpa_s->conf->country[1];
  2594. country[2] = '\0';
  2595. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2596. wpa_printf(MSG_ERROR, "Failed to set country code "
  2597. "'%s'", country);
  2598. }
  2599. }
  2600. #ifdef CONFIG_WPS
  2601. wpas_wps_update_config(wpa_s);
  2602. #endif /* CONFIG_WPS */
  2603. #ifdef CONFIG_P2P
  2604. wpas_p2p_update_config(wpa_s);
  2605. #endif /* CONFIG_P2P */
  2606. wpa_s->conf->changed_parameters = 0;
  2607. }
  2608. static void add_freq(int *freqs, int *num_freqs, int freq)
  2609. {
  2610. int i;
  2611. for (i = 0; i < *num_freqs; i++) {
  2612. if (freqs[i] == freq)
  2613. return;
  2614. }
  2615. freqs[*num_freqs] = freq;
  2616. (*num_freqs)++;
  2617. }
  2618. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2619. {
  2620. struct wpa_bss *bss, *cbss;
  2621. const int max_freqs = 10;
  2622. int *freqs;
  2623. int num_freqs = 0;
  2624. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2625. if (freqs == NULL)
  2626. return NULL;
  2627. cbss = wpa_s->current_bss;
  2628. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2629. if (bss == cbss)
  2630. continue;
  2631. if (bss->ssid_len == cbss->ssid_len &&
  2632. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2633. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2634. add_freq(freqs, &num_freqs, bss->freq);
  2635. if (num_freqs == max_freqs)
  2636. break;
  2637. }
  2638. }
  2639. if (num_freqs == 0) {
  2640. os_free(freqs);
  2641. freqs = NULL;
  2642. }
  2643. return freqs;
  2644. }
  2645. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2646. {
  2647. int timeout;
  2648. int count;
  2649. int *freqs = NULL;
  2650. /*
  2651. * Remove possible authentication timeout since the connection failed.
  2652. */
  2653. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2654. /*
  2655. * Add the failed BSSID into the blacklist and speed up next scan
  2656. * attempt if there could be other APs that could accept association.
  2657. * The current blacklist count indicates how many times we have tried
  2658. * connecting to this AP and multiple attempts mean that other APs are
  2659. * either not available or has already been tried, so that we can start
  2660. * increasing the delay here to avoid constant scanning.
  2661. */
  2662. count = wpa_blacklist_add(wpa_s, bssid);
  2663. if (count == 1 && wpa_s->current_bss) {
  2664. /*
  2665. * This BSS was not in the blacklist before. If there is
  2666. * another BSS available for the same ESS, we should try that
  2667. * next. Otherwise, we may as well try this one once more
  2668. * before allowing other, likely worse, ESSes to be considered.
  2669. */
  2670. freqs = get_bss_freqs_in_ess(wpa_s);
  2671. if (freqs) {
  2672. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2673. "has been seen; try it next");
  2674. wpa_blacklist_add(wpa_s, bssid);
  2675. /*
  2676. * On the next scan, go through only the known channels
  2677. * used in this ESS based on previous scans to speed up
  2678. * common load balancing use case.
  2679. */
  2680. os_free(wpa_s->next_scan_freqs);
  2681. wpa_s->next_scan_freqs = freqs;
  2682. }
  2683. }
  2684. switch (count) {
  2685. case 1:
  2686. timeout = 100;
  2687. break;
  2688. case 2:
  2689. timeout = 500;
  2690. break;
  2691. case 3:
  2692. timeout = 1000;
  2693. break;
  2694. default:
  2695. timeout = 5000;
  2696. }
  2697. /*
  2698. * TODO: if more than one possible AP is available in scan results,
  2699. * could try the other ones before requesting a new scan.
  2700. */
  2701. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2702. 1000 * (timeout % 1000));
  2703. }
  2704. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  2705. {
  2706. return wpa_s->conf->ap_scan == 2 ||
  2707. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  2708. }