wpa_supplicant.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295
  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. sme_sched_obss_scan(wpa_s, 1);
  529. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  530. state == WPA_ASSOCIATED) {
  531. wpa_s->new_connection = 1;
  532. wpa_drv_set_operstate(wpa_s, 0);
  533. #ifndef IEEE8021X_EAPOL
  534. wpa_drv_set_supp_port(wpa_s, 0);
  535. #endif /* IEEE8021X_EAPOL */
  536. sme_sched_obss_scan(wpa_s, 0);
  537. }
  538. wpa_s->wpa_state = state;
  539. #ifdef CONFIG_BGSCAN
  540. if (state == WPA_COMPLETED)
  541. wpa_supplicant_start_bgscan(wpa_s);
  542. else
  543. wpa_supplicant_stop_bgscan(wpa_s);
  544. #endif /* CONFIG_BGSCAN */
  545. if (wpa_s->wpa_state != old_state) {
  546. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  547. if (wpa_s->wpa_state == WPA_COMPLETED ||
  548. old_state == WPA_COMPLETED)
  549. wpas_notify_auth_changed(wpa_s);
  550. }
  551. }
  552. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  553. {
  554. int pending = 0;
  555. #ifdef CONFIG_WPS
  556. struct wpa_supplicant *wpa_s = global->ifaces;
  557. while (wpa_s) {
  558. if (wpas_wps_terminate_pending(wpa_s) == 1)
  559. pending = 1;
  560. wpa_s = wpa_s->next;
  561. }
  562. #endif /* CONFIG_WPS */
  563. if (pending)
  564. return;
  565. eloop_terminate();
  566. }
  567. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  568. {
  569. struct wpa_global *global = signal_ctx;
  570. wpa_supplicant_terminate_proc(global);
  571. }
  572. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  573. {
  574. enum wpa_states old_state = wpa_s->wpa_state;
  575. wpa_s->pairwise_cipher = 0;
  576. wpa_s->group_cipher = 0;
  577. wpa_s->mgmt_group_cipher = 0;
  578. wpa_s->key_mgmt = 0;
  579. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  580. wpa_s->wpa_state = WPA_DISCONNECTED;
  581. if (wpa_s->wpa_state != old_state)
  582. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  583. }
  584. /**
  585. * wpa_supplicant_reload_configuration - Reload configuration data
  586. * @wpa_s: Pointer to wpa_supplicant data
  587. * Returns: 0 on success or -1 if configuration parsing failed
  588. *
  589. * This function can be used to request that the configuration data is reloaded
  590. * (e.g., after configuration file change). This function is reloading
  591. * configuration only for one interface, so this may need to be called multiple
  592. * times if %wpa_supplicant is controlling multiple interfaces and all
  593. * interfaces need reconfiguration.
  594. */
  595. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  596. {
  597. struct wpa_config *conf;
  598. int reconf_ctrl;
  599. int old_ap_scan;
  600. if (wpa_s->confname == NULL)
  601. return -1;
  602. conf = wpa_config_read(wpa_s->confname);
  603. if (conf == NULL) {
  604. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  605. "file '%s' - exiting", wpa_s->confname);
  606. return -1;
  607. }
  608. conf->changed_parameters = (unsigned int) -1;
  609. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  610. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  611. os_strcmp(conf->ctrl_interface,
  612. wpa_s->conf->ctrl_interface) != 0);
  613. if (reconf_ctrl && wpa_s->ctrl_iface) {
  614. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  615. wpa_s->ctrl_iface = NULL;
  616. }
  617. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  618. if (wpa_s->current_ssid) {
  619. wpa_supplicant_deauthenticate(wpa_s,
  620. WLAN_REASON_DEAUTH_LEAVING);
  621. }
  622. /*
  623. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  624. * pkcs11_engine_path, pkcs11_module_path.
  625. */
  626. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  627. /*
  628. * Clear forced success to clear EAP state for next
  629. * authentication.
  630. */
  631. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  632. }
  633. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  634. wpa_sm_set_config(wpa_s->wpa, NULL);
  635. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  636. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  637. rsn_preauth_deinit(wpa_s->wpa);
  638. old_ap_scan = wpa_s->conf->ap_scan;
  639. wpa_config_free(wpa_s->conf);
  640. wpa_s->conf = conf;
  641. if (old_ap_scan != wpa_s->conf->ap_scan)
  642. wpas_notify_ap_scan_changed(wpa_s);
  643. if (reconf_ctrl)
  644. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  645. wpa_supplicant_update_config(wpa_s);
  646. wpa_supplicant_clear_status(wpa_s);
  647. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  648. wpa_s->reassociate = 1;
  649. wpa_supplicant_req_scan(wpa_s, 0, 0);
  650. }
  651. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  652. return 0;
  653. }
  654. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  655. {
  656. struct wpa_global *global = signal_ctx;
  657. struct wpa_supplicant *wpa_s;
  658. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  659. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  660. sig);
  661. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  662. wpa_supplicant_terminate_proc(global);
  663. }
  664. }
  665. }
  666. enum wpa_cipher cipher_suite2driver(int cipher)
  667. {
  668. switch (cipher) {
  669. case WPA_CIPHER_NONE:
  670. return CIPHER_NONE;
  671. case WPA_CIPHER_WEP40:
  672. return CIPHER_WEP40;
  673. case WPA_CIPHER_WEP104:
  674. return CIPHER_WEP104;
  675. case WPA_CIPHER_CCMP:
  676. return CIPHER_CCMP;
  677. case WPA_CIPHER_TKIP:
  678. default:
  679. return CIPHER_TKIP;
  680. }
  681. }
  682. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  683. {
  684. switch (key_mgmt) {
  685. case WPA_KEY_MGMT_NONE:
  686. return KEY_MGMT_NONE;
  687. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  688. return KEY_MGMT_802_1X_NO_WPA;
  689. case WPA_KEY_MGMT_IEEE8021X:
  690. return KEY_MGMT_802_1X;
  691. case WPA_KEY_MGMT_WPA_NONE:
  692. return KEY_MGMT_WPA_NONE;
  693. case WPA_KEY_MGMT_FT_IEEE8021X:
  694. return KEY_MGMT_FT_802_1X;
  695. case WPA_KEY_MGMT_FT_PSK:
  696. return KEY_MGMT_FT_PSK;
  697. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  698. return KEY_MGMT_802_1X_SHA256;
  699. case WPA_KEY_MGMT_PSK_SHA256:
  700. return KEY_MGMT_PSK_SHA256;
  701. case WPA_KEY_MGMT_WPS:
  702. return KEY_MGMT_WPS;
  703. case WPA_KEY_MGMT_PSK:
  704. default:
  705. return KEY_MGMT_PSK;
  706. }
  707. }
  708. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  709. struct wpa_ssid *ssid,
  710. struct wpa_ie_data *ie)
  711. {
  712. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  713. if (ret) {
  714. if (ret == -2) {
  715. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  716. "from association info");
  717. }
  718. return -1;
  719. }
  720. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  721. "cipher suites");
  722. if (!(ie->group_cipher & ssid->group_cipher)) {
  723. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  724. "cipher 0x%x (mask 0x%x) - reject",
  725. ie->group_cipher, ssid->group_cipher);
  726. return -1;
  727. }
  728. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  729. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  730. "cipher 0x%x (mask 0x%x) - reject",
  731. ie->pairwise_cipher, ssid->pairwise_cipher);
  732. return -1;
  733. }
  734. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  735. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  736. "management 0x%x (mask 0x%x) - reject",
  737. ie->key_mgmt, ssid->key_mgmt);
  738. return -1;
  739. }
  740. #ifdef CONFIG_IEEE80211W
  741. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  742. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  743. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  744. "that does not support management frame protection - "
  745. "reject");
  746. return -1;
  747. }
  748. #endif /* CONFIG_IEEE80211W */
  749. return 0;
  750. }
  751. /**
  752. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  753. * @wpa_s: Pointer to wpa_supplicant data
  754. * @bss: Scan results for the selected BSS, or %NULL if not available
  755. * @ssid: Configuration data for the selected network
  756. * @wpa_ie: Buffer for the WPA/RSN IE
  757. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  758. * used buffer length in case the functions returns success.
  759. * Returns: 0 on success or -1 on failure
  760. *
  761. * This function is used to configure authentication and encryption parameters
  762. * based on the network configuration and scan result for the selected BSS (if
  763. * available).
  764. */
  765. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  766. struct wpa_bss *bss, struct wpa_ssid *ssid,
  767. u8 *wpa_ie, size_t *wpa_ie_len)
  768. {
  769. struct wpa_ie_data ie;
  770. int sel, proto;
  771. const u8 *bss_wpa, *bss_rsn;
  772. if (bss) {
  773. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  774. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  775. } else
  776. bss_wpa = bss_rsn = NULL;
  777. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  778. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  779. (ie.group_cipher & ssid->group_cipher) &&
  780. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  781. (ie.key_mgmt & ssid->key_mgmt)) {
  782. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  783. proto = WPA_PROTO_RSN;
  784. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  785. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  786. (ie.group_cipher & ssid->group_cipher) &&
  787. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  788. (ie.key_mgmt & ssid->key_mgmt)) {
  789. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  790. proto = WPA_PROTO_WPA;
  791. } else if (bss) {
  792. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  793. return -1;
  794. } else {
  795. if (ssid->proto & WPA_PROTO_RSN)
  796. proto = WPA_PROTO_RSN;
  797. else
  798. proto = WPA_PROTO_WPA;
  799. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  800. os_memset(&ie, 0, sizeof(ie));
  801. ie.group_cipher = ssid->group_cipher;
  802. ie.pairwise_cipher = ssid->pairwise_cipher;
  803. ie.key_mgmt = ssid->key_mgmt;
  804. #ifdef CONFIG_IEEE80211W
  805. ie.mgmt_group_cipher =
  806. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  807. WPA_CIPHER_AES_128_CMAC : 0;
  808. #endif /* CONFIG_IEEE80211W */
  809. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  810. "based on configuration");
  811. } else
  812. proto = ie.proto;
  813. }
  814. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  815. "pairwise %d key_mgmt %d proto %d",
  816. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  817. #ifdef CONFIG_IEEE80211W
  818. if (ssid->ieee80211w) {
  819. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  820. ie.mgmt_group_cipher);
  821. }
  822. #endif /* CONFIG_IEEE80211W */
  823. wpa_s->wpa_proto = proto;
  824. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  825. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  826. !!(ssid->proto & WPA_PROTO_RSN));
  827. if (bss || !wpa_s->ap_ies_from_associnfo) {
  828. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  829. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  830. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  831. bss_rsn ? 2 + bss_rsn[1] : 0))
  832. return -1;
  833. }
  834. sel = ie.group_cipher & ssid->group_cipher;
  835. if (sel & WPA_CIPHER_CCMP) {
  836. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  837. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  838. } else if (sel & WPA_CIPHER_TKIP) {
  839. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  840. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  841. } else if (sel & WPA_CIPHER_WEP104) {
  842. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  843. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  844. } else if (sel & WPA_CIPHER_WEP40) {
  845. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  846. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  847. } else {
  848. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  849. "cipher");
  850. return -1;
  851. }
  852. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  853. if (sel & WPA_CIPHER_CCMP) {
  854. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  855. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  856. } else if (sel & WPA_CIPHER_TKIP) {
  857. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  858. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  859. } else if (sel & WPA_CIPHER_NONE) {
  860. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  861. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  862. } else {
  863. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  864. "cipher");
  865. return -1;
  866. }
  867. sel = ie.key_mgmt & ssid->key_mgmt;
  868. if (0) {
  869. #ifdef CONFIG_IEEE80211R
  870. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  871. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  872. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  873. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  874. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  875. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  876. #endif /* CONFIG_IEEE80211R */
  877. #ifdef CONFIG_IEEE80211W
  878. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  879. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  880. wpa_dbg(wpa_s, MSG_DEBUG,
  881. "WPA: using KEY_MGMT 802.1X with SHA256");
  882. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  883. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  884. wpa_dbg(wpa_s, MSG_DEBUG,
  885. "WPA: using KEY_MGMT PSK with SHA256");
  886. #endif /* CONFIG_IEEE80211W */
  887. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  888. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  889. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  890. } else if (sel & WPA_KEY_MGMT_PSK) {
  891. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  892. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  893. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  894. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  895. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  896. } else {
  897. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  898. "authenticated key management type");
  899. return -1;
  900. }
  901. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  902. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  903. wpa_s->pairwise_cipher);
  904. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  905. #ifdef CONFIG_IEEE80211W
  906. sel = ie.mgmt_group_cipher;
  907. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  908. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  909. sel = 0;
  910. if (sel & WPA_CIPHER_AES_128_CMAC) {
  911. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  912. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  913. "AES-128-CMAC");
  914. } else {
  915. wpa_s->mgmt_group_cipher = 0;
  916. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  917. }
  918. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  919. wpa_s->mgmt_group_cipher);
  920. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  921. #endif /* CONFIG_IEEE80211W */
  922. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  923. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  924. return -1;
  925. }
  926. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  927. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  928. #ifndef CONFIG_NO_PBKDF2
  929. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  930. ssid->passphrase) {
  931. u8 psk[PMK_LEN];
  932. pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
  933. bss->ssid_len, 4096, psk, PMK_LEN);
  934. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  935. psk, PMK_LEN);
  936. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  937. }
  938. #endif /* CONFIG_NO_PBKDF2 */
  939. } else
  940. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  941. return 0;
  942. }
  943. /**
  944. * wpa_supplicant_associate - Request association
  945. * @wpa_s: Pointer to wpa_supplicant data
  946. * @bss: Scan results for the selected BSS, or %NULL if not available
  947. * @ssid: Configuration data for the selected network
  948. *
  949. * This function is used to request %wpa_supplicant to associate with a BSS.
  950. */
  951. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  952. struct wpa_bss *bss, struct wpa_ssid *ssid)
  953. {
  954. u8 wpa_ie[200];
  955. size_t wpa_ie_len;
  956. int use_crypt, ret, i, bssid_changed;
  957. int algs = WPA_AUTH_ALG_OPEN;
  958. enum wpa_cipher cipher_pairwise, cipher_group;
  959. struct wpa_driver_associate_params params;
  960. int wep_keys_set = 0;
  961. struct wpa_driver_capa capa;
  962. int assoc_failed = 0;
  963. struct wpa_ssid *old_ssid;
  964. #ifdef CONFIG_HT_OVERRIDES
  965. struct ieee80211_ht_capabilities htcaps;
  966. struct ieee80211_ht_capabilities htcaps_mask;
  967. #endif /* CONFIG_HT_OVERRIDES */
  968. #ifdef CONFIG_IBSS_RSN
  969. ibss_rsn_deinit(wpa_s->ibss_rsn);
  970. wpa_s->ibss_rsn = NULL;
  971. #endif /* CONFIG_IBSS_RSN */
  972. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  973. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  974. #ifdef CONFIG_AP
  975. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  976. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  977. "mode");
  978. return;
  979. }
  980. wpa_supplicant_create_ap(wpa_s, ssid);
  981. wpa_s->current_bss = bss;
  982. #else /* CONFIG_AP */
  983. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  984. "the build");
  985. #endif /* CONFIG_AP */
  986. return;
  987. }
  988. #ifdef CONFIG_TDLS
  989. if (bss)
  990. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  991. bss->ie_len);
  992. #endif /* CONFIG_TDLS */
  993. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  994. ssid->mode == IEEE80211_MODE_INFRA) {
  995. sme_authenticate(wpa_s, bss, ssid);
  996. return;
  997. }
  998. os_memset(&params, 0, sizeof(params));
  999. wpa_s->reassociate = 0;
  1000. if (bss && !wpas_driver_bss_selection(wpa_s)) {
  1001. #ifdef CONFIG_IEEE80211R
  1002. const u8 *ie, *md = NULL;
  1003. #endif /* CONFIG_IEEE80211R */
  1004. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1005. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1006. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1007. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1008. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1009. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1010. if (bssid_changed)
  1011. wpas_notify_bssid_changed(wpa_s);
  1012. #ifdef CONFIG_IEEE80211R
  1013. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1014. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1015. md = ie + 2;
  1016. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1017. if (md) {
  1018. /* Prepare for the next transition */
  1019. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1020. }
  1021. #endif /* CONFIG_IEEE80211R */
  1022. #ifdef CONFIG_WPS
  1023. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1024. wpa_s->conf->ap_scan == 2 &&
  1025. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1026. /* Use ap_scan==1 style network selection to find the network
  1027. */
  1028. wpa_s->scan_req = 2;
  1029. wpa_s->reassociate = 1;
  1030. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1031. return;
  1032. #endif /* CONFIG_WPS */
  1033. } else {
  1034. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1035. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1036. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1037. }
  1038. wpa_supplicant_cancel_sched_scan(wpa_s);
  1039. wpa_supplicant_cancel_scan(wpa_s);
  1040. /* Starting new association, so clear the possibly used WPA IE from the
  1041. * previous association. */
  1042. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1043. #ifdef IEEE8021X_EAPOL
  1044. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1045. if (ssid->leap) {
  1046. if (ssid->non_leap == 0)
  1047. algs = WPA_AUTH_ALG_LEAP;
  1048. else
  1049. algs |= WPA_AUTH_ALG_LEAP;
  1050. }
  1051. }
  1052. #endif /* IEEE8021X_EAPOL */
  1053. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1054. if (ssid->auth_alg) {
  1055. algs = ssid->auth_alg;
  1056. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1057. "0x%x", algs);
  1058. }
  1059. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1060. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1061. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1062. int try_opportunistic;
  1063. try_opportunistic = ssid->proactive_key_caching &&
  1064. (ssid->proto & WPA_PROTO_RSN);
  1065. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1066. wpa_s->current_ssid,
  1067. try_opportunistic) == 0)
  1068. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1069. wpa_ie_len = sizeof(wpa_ie);
  1070. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1071. wpa_ie, &wpa_ie_len)) {
  1072. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1073. "key management and encryption suites");
  1074. return;
  1075. }
  1076. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1077. wpa_ie_len = sizeof(wpa_ie);
  1078. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1079. wpa_ie, &wpa_ie_len)) {
  1080. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1081. "key management and encryption suites (no "
  1082. "scan results)");
  1083. return;
  1084. }
  1085. #ifdef CONFIG_WPS
  1086. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1087. struct wpabuf *wps_ie;
  1088. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1089. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1090. wpa_ie_len = wpabuf_len(wps_ie);
  1091. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1092. } else
  1093. wpa_ie_len = 0;
  1094. wpabuf_free(wps_ie);
  1095. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1096. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1097. params.wps = WPS_MODE_PRIVACY;
  1098. else
  1099. params.wps = WPS_MODE_OPEN;
  1100. wpa_s->wpa_proto = 0;
  1101. #endif /* CONFIG_WPS */
  1102. } else {
  1103. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1104. wpa_ie_len = 0;
  1105. wpa_s->wpa_proto = 0;
  1106. }
  1107. #ifdef CONFIG_P2P
  1108. if (wpa_s->global->p2p) {
  1109. u8 *pos;
  1110. size_t len;
  1111. int res;
  1112. pos = wpa_ie + wpa_ie_len;
  1113. len = sizeof(wpa_ie) - wpa_ie_len;
  1114. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  1115. ssid->p2p_group);
  1116. if (res >= 0)
  1117. wpa_ie_len += res;
  1118. }
  1119. wpa_s->cross_connect_disallowed = 0;
  1120. if (bss) {
  1121. struct wpabuf *p2p;
  1122. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1123. if (p2p) {
  1124. wpa_s->cross_connect_disallowed =
  1125. p2p_get_cross_connect_disallowed(p2p);
  1126. wpabuf_free(p2p);
  1127. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1128. "connection",
  1129. wpa_s->cross_connect_disallowed ?
  1130. "disallows" : "allows");
  1131. }
  1132. }
  1133. #endif /* CONFIG_P2P */
  1134. #ifdef CONFIG_INTERWORKING
  1135. if (wpa_s->conf->interworking) {
  1136. u8 *pos = wpa_ie;
  1137. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1138. pos += 2 + pos[1];
  1139. os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
  1140. wpa_ie_len += 6;
  1141. *pos++ = WLAN_EID_EXT_CAPAB;
  1142. *pos++ = 4;
  1143. *pos++ = 0x00;
  1144. *pos++ = 0x00;
  1145. *pos++ = 0x00;
  1146. *pos++ = 0x80; /* Bit 31 - Interworking */
  1147. }
  1148. #endif /* CONFIG_INTERWORKING */
  1149. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1150. use_crypt = 1;
  1151. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1152. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1153. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1154. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1155. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1156. use_crypt = 0;
  1157. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1158. use_crypt = 1;
  1159. wep_keys_set = 1;
  1160. }
  1161. }
  1162. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1163. use_crypt = 0;
  1164. #ifdef IEEE8021X_EAPOL
  1165. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1166. if ((ssid->eapol_flags &
  1167. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1168. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1169. !wep_keys_set) {
  1170. use_crypt = 0;
  1171. } else {
  1172. /* Assume that dynamic WEP-104 keys will be used and
  1173. * set cipher suites in order for drivers to expect
  1174. * encryption. */
  1175. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1176. }
  1177. }
  1178. #endif /* IEEE8021X_EAPOL */
  1179. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1180. /* Set the key before (and later after) association */
  1181. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1182. }
  1183. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1184. if (bss) {
  1185. params.ssid = bss->ssid;
  1186. params.ssid_len = bss->ssid_len;
  1187. if (!wpas_driver_bss_selection(wpa_s)) {
  1188. params.bssid = bss->bssid;
  1189. params.freq = bss->freq;
  1190. }
  1191. } else {
  1192. params.ssid = ssid->ssid;
  1193. params.ssid_len = ssid->ssid_len;
  1194. }
  1195. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  1196. wpa_s->conf->ap_scan == 2) {
  1197. params.bssid = ssid->bssid;
  1198. params.fixed_bssid = 1;
  1199. }
  1200. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1201. params.freq == 0)
  1202. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1203. params.wpa_ie = wpa_ie;
  1204. params.wpa_ie_len = wpa_ie_len;
  1205. params.pairwise_suite = cipher_pairwise;
  1206. params.group_suite = cipher_group;
  1207. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1208. params.wpa_proto = wpa_s->wpa_proto;
  1209. params.auth_alg = algs;
  1210. params.mode = ssid->mode;
  1211. params.bg_scan_period = ssid->bg_scan_period;
  1212. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1213. if (ssid->wep_key_len[i])
  1214. params.wep_key[i] = ssid->wep_key[i];
  1215. params.wep_key_len[i] = ssid->wep_key_len[i];
  1216. }
  1217. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1218. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1219. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1220. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1221. params.passphrase = ssid->passphrase;
  1222. if (ssid->psk_set)
  1223. params.psk = ssid->psk;
  1224. }
  1225. params.drop_unencrypted = use_crypt;
  1226. #ifdef CONFIG_IEEE80211W
  1227. params.mgmt_frame_protection = ssid->ieee80211w;
  1228. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1229. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1230. struct wpa_ie_data ie;
  1231. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1232. ie.capabilities &
  1233. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1234. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1235. "MFP: require MFP");
  1236. params.mgmt_frame_protection =
  1237. MGMT_FRAME_PROTECTION_REQUIRED;
  1238. }
  1239. }
  1240. #endif /* CONFIG_IEEE80211W */
  1241. params.p2p = ssid->p2p_group;
  1242. if (wpa_s->parent->set_sta_uapsd)
  1243. params.uapsd = wpa_s->parent->sta_uapsd;
  1244. else
  1245. params.uapsd = -1;
  1246. #ifdef CONFIG_HT_OVERRIDES
  1247. os_memset(&htcaps, 0, sizeof(htcaps));
  1248. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1249. params.htcaps = (u8 *) &htcaps;
  1250. params.htcaps_mask = (u8 *) &htcaps_mask;
  1251. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  1252. #endif /* CONFIG_HT_OVERRIDES */
  1253. ret = wpa_drv_associate(wpa_s, &params);
  1254. if (ret < 0) {
  1255. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1256. "failed");
  1257. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1258. /*
  1259. * The driver is known to mean what is saying, so we
  1260. * can stop right here; the association will not
  1261. * succeed.
  1262. */
  1263. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1264. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1265. return;
  1266. }
  1267. /* try to continue anyway; new association will be tried again
  1268. * after timeout */
  1269. assoc_failed = 1;
  1270. }
  1271. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1272. /* Set the key after the association just in case association
  1273. * cleared the previously configured key. */
  1274. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1275. /* No need to timeout authentication since there is no key
  1276. * management. */
  1277. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1278. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1279. #ifdef CONFIG_IBSS_RSN
  1280. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1281. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1282. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1283. /*
  1284. * RSN IBSS authentication is per-STA and we can disable the
  1285. * per-BSSID authentication.
  1286. */
  1287. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1288. #endif /* CONFIG_IBSS_RSN */
  1289. } else {
  1290. /* Timeout for IEEE 802.11 authentication and association */
  1291. int timeout = 60;
  1292. if (assoc_failed) {
  1293. /* give IBSS a bit more time */
  1294. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1295. } else if (wpa_s->conf->ap_scan == 1) {
  1296. /* give IBSS a bit more time */
  1297. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1298. }
  1299. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1300. }
  1301. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1302. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1303. /* Set static WEP keys again */
  1304. wpa_set_wep_keys(wpa_s, ssid);
  1305. }
  1306. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1307. /*
  1308. * Do not allow EAP session resumption between different
  1309. * network configurations.
  1310. */
  1311. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1312. }
  1313. old_ssid = wpa_s->current_ssid;
  1314. wpa_s->current_ssid = ssid;
  1315. wpa_s->current_bss = bss;
  1316. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1317. wpa_supplicant_initiate_eapol(wpa_s);
  1318. if (old_ssid != wpa_s->current_ssid)
  1319. wpas_notify_network_changed(wpa_s);
  1320. }
  1321. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1322. const u8 *addr)
  1323. {
  1324. struct wpa_ssid *old_ssid;
  1325. wpa_clear_keys(wpa_s, addr);
  1326. wpa_supplicant_mark_disassoc(wpa_s);
  1327. old_ssid = wpa_s->current_ssid;
  1328. wpa_s->current_ssid = NULL;
  1329. wpa_s->current_bss = NULL;
  1330. wpa_sm_set_config(wpa_s->wpa, NULL);
  1331. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1332. if (old_ssid != wpa_s->current_ssid)
  1333. wpas_notify_network_changed(wpa_s);
  1334. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1335. }
  1336. /**
  1337. * wpa_supplicant_disassociate - Disassociate the current connection
  1338. * @wpa_s: Pointer to wpa_supplicant data
  1339. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1340. *
  1341. * This function is used to request %wpa_supplicant to disassociate with the
  1342. * current AP.
  1343. */
  1344. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1345. int reason_code)
  1346. {
  1347. u8 *addr = NULL;
  1348. union wpa_event_data event;
  1349. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1350. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1351. addr = wpa_s->bssid;
  1352. os_memset(&event, 0, sizeof(event));
  1353. event.disassoc_info.reason_code = (u16) reason_code;
  1354. event.disassoc_info.locally_generated = 1;
  1355. wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
  1356. }
  1357. wpa_supplicant_clear_connection(wpa_s, addr);
  1358. }
  1359. /**
  1360. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1361. * @wpa_s: Pointer to wpa_supplicant data
  1362. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1363. *
  1364. * This function is used to request %wpa_supplicant to deauthenticate from the
  1365. * current AP.
  1366. */
  1367. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1368. int reason_code)
  1369. {
  1370. u8 *addr = NULL;
  1371. union wpa_event_data event;
  1372. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1373. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
  1374. addr = wpa_s->bssid;
  1375. os_memset(&event, 0, sizeof(event));
  1376. event.deauth_info.reason_code = (u16) reason_code;
  1377. event.deauth_info.locally_generated = 1;
  1378. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  1379. }
  1380. wpa_supplicant_clear_connection(wpa_s, addr);
  1381. }
  1382. /**
  1383. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1384. * @wpa_s: wpa_supplicant structure for a network interface
  1385. * @ssid: wpa_ssid structure for a configured network or %NULL
  1386. *
  1387. * Enables the specified network or all networks if no network specified.
  1388. */
  1389. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1390. struct wpa_ssid *ssid)
  1391. {
  1392. struct wpa_ssid *other_ssid;
  1393. int was_disabled;
  1394. if (ssid == NULL) {
  1395. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1396. other_ssid = other_ssid->next) {
  1397. if (other_ssid->disabled == 2)
  1398. continue; /* do not change persistent P2P group
  1399. * data */
  1400. if (other_ssid == wpa_s->current_ssid &&
  1401. other_ssid->disabled)
  1402. wpa_s->reassociate = 1;
  1403. was_disabled = other_ssid->disabled;
  1404. other_ssid->disabled = 0;
  1405. if (was_disabled != other_ssid->disabled)
  1406. wpas_notify_network_enabled_changed(
  1407. wpa_s, other_ssid);
  1408. }
  1409. if (wpa_s->reassociate)
  1410. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1411. } else if (ssid->disabled && ssid->disabled != 2) {
  1412. if (wpa_s->current_ssid == NULL) {
  1413. /*
  1414. * Try to reassociate since there is no current
  1415. * configuration and a new network was made available.
  1416. */
  1417. wpa_s->reassociate = 1;
  1418. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1419. }
  1420. was_disabled = ssid->disabled;
  1421. ssid->disabled = 0;
  1422. if (was_disabled != ssid->disabled)
  1423. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1424. }
  1425. }
  1426. /**
  1427. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1428. * @wpa_s: wpa_supplicant structure for a network interface
  1429. * @ssid: wpa_ssid structure for a configured network or %NULL
  1430. *
  1431. * Disables the specified network or all networks if no network specified.
  1432. */
  1433. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1434. struct wpa_ssid *ssid)
  1435. {
  1436. struct wpa_ssid *other_ssid;
  1437. int was_disabled;
  1438. if (ssid == NULL) {
  1439. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1440. other_ssid = other_ssid->next) {
  1441. was_disabled = other_ssid->disabled;
  1442. if (was_disabled == 2)
  1443. continue; /* do not change persistent P2P group
  1444. * data */
  1445. other_ssid->disabled = 1;
  1446. if (was_disabled != other_ssid->disabled)
  1447. wpas_notify_network_enabled_changed(
  1448. wpa_s, other_ssid);
  1449. }
  1450. if (wpa_s->current_ssid)
  1451. wpa_supplicant_disassociate(
  1452. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1453. } else if (ssid->disabled != 2) {
  1454. if (ssid == wpa_s->current_ssid)
  1455. wpa_supplicant_disassociate(
  1456. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1457. was_disabled = ssid->disabled;
  1458. ssid->disabled = 1;
  1459. if (was_disabled != ssid->disabled)
  1460. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1461. }
  1462. }
  1463. /**
  1464. * wpa_supplicant_select_network - Attempt association with a network
  1465. * @wpa_s: wpa_supplicant structure for a network interface
  1466. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1467. */
  1468. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1469. struct wpa_ssid *ssid)
  1470. {
  1471. struct wpa_ssid *other_ssid;
  1472. int disconnected = 0;
  1473. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  1474. wpa_supplicant_disassociate(
  1475. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1476. disconnected = 1;
  1477. }
  1478. /*
  1479. * Mark all other networks disabled or mark all networks enabled if no
  1480. * network specified.
  1481. */
  1482. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1483. other_ssid = other_ssid->next) {
  1484. int was_disabled = other_ssid->disabled;
  1485. if (was_disabled == 2)
  1486. continue; /* do not change persistent P2P group data */
  1487. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1488. if (was_disabled != other_ssid->disabled)
  1489. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1490. }
  1491. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1492. /* We are already associated with the selected network */
  1493. wpa_printf(MSG_DEBUG, "Already associated with the "
  1494. "selected network - do nothing");
  1495. return;
  1496. }
  1497. if (ssid)
  1498. wpa_s->current_ssid = ssid;
  1499. wpa_s->connect_without_scan = NULL;
  1500. wpa_s->disconnected = 0;
  1501. wpa_s->reassociate = 1;
  1502. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  1503. if (ssid)
  1504. wpas_notify_network_selected(wpa_s, ssid);
  1505. }
  1506. /**
  1507. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1508. * @wpa_s: wpa_supplicant structure for a network interface
  1509. * @ap_scan: AP scan mode
  1510. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1511. *
  1512. */
  1513. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1514. {
  1515. int old_ap_scan;
  1516. if (ap_scan < 0 || ap_scan > 2)
  1517. return -1;
  1518. #ifdef ANDROID
  1519. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  1520. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  1521. wpa_s->wpa_state < WPA_COMPLETED) {
  1522. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  1523. "associating", wpa_s->conf->ap_scan, ap_scan);
  1524. return 0;
  1525. }
  1526. #endif /* ANDROID */
  1527. old_ap_scan = wpa_s->conf->ap_scan;
  1528. wpa_s->conf->ap_scan = ap_scan;
  1529. if (old_ap_scan != wpa_s->conf->ap_scan)
  1530. wpas_notify_ap_scan_changed(wpa_s);
  1531. return 0;
  1532. }
  1533. /**
  1534. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1535. * @wpa_s: wpa_supplicant structure for a network interface
  1536. * @expire_age: Expiration age in seconds
  1537. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1538. *
  1539. */
  1540. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1541. unsigned int bss_expire_age)
  1542. {
  1543. if (bss_expire_age < 10) {
  1544. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1545. bss_expire_age);
  1546. return -1;
  1547. }
  1548. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1549. bss_expire_age);
  1550. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1551. return 0;
  1552. }
  1553. /**
  1554. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1555. * @wpa_s: wpa_supplicant structure for a network interface
  1556. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1557. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1558. *
  1559. */
  1560. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1561. unsigned int bss_expire_count)
  1562. {
  1563. if (bss_expire_count < 1) {
  1564. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1565. bss_expire_count);
  1566. return -1;
  1567. }
  1568. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1569. bss_expire_count);
  1570. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1571. return 0;
  1572. }
  1573. /**
  1574. * wpa_supplicant_set_scan_interval - Set scan interval
  1575. * @wpa_s: wpa_supplicant structure for a network interface
  1576. * @scan_interval: scan interval in seconds
  1577. * Returns: 0 if succeed or -1 if scan_interval has an invalid value
  1578. *
  1579. */
  1580. int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
  1581. int scan_interval)
  1582. {
  1583. if (scan_interval < 0) {
  1584. wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
  1585. scan_interval);
  1586. return -1;
  1587. }
  1588. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
  1589. scan_interval);
  1590. wpa_s->scan_interval = scan_interval;
  1591. return 0;
  1592. }
  1593. /**
  1594. * wpa_supplicant_set_debug_params - Set global debug params
  1595. * @global: wpa_global structure
  1596. * @debug_level: debug level
  1597. * @debug_timestamp: determines if show timestamp in debug data
  1598. * @debug_show_keys: determines if show keys in debug data
  1599. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1600. */
  1601. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1602. int debug_timestamp, int debug_show_keys)
  1603. {
  1604. int old_level, old_timestamp, old_show_keys;
  1605. /* check for allowed debuglevels */
  1606. if (debug_level != MSG_EXCESSIVE &&
  1607. debug_level != MSG_MSGDUMP &&
  1608. debug_level != MSG_DEBUG &&
  1609. debug_level != MSG_INFO &&
  1610. debug_level != MSG_WARNING &&
  1611. debug_level != MSG_ERROR)
  1612. return -1;
  1613. old_level = wpa_debug_level;
  1614. old_timestamp = wpa_debug_timestamp;
  1615. old_show_keys = wpa_debug_show_keys;
  1616. wpa_debug_level = debug_level;
  1617. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1618. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1619. if (wpa_debug_level != old_level)
  1620. wpas_notify_debug_level_changed(global);
  1621. if (wpa_debug_timestamp != old_timestamp)
  1622. wpas_notify_debug_timestamp_changed(global);
  1623. if (wpa_debug_show_keys != old_show_keys)
  1624. wpas_notify_debug_show_keys_changed(global);
  1625. return 0;
  1626. }
  1627. /**
  1628. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1629. * @wpa_s: Pointer to wpa_supplicant data
  1630. * Returns: A pointer to the current network structure or %NULL on failure
  1631. */
  1632. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1633. {
  1634. struct wpa_ssid *entry;
  1635. u8 ssid[MAX_SSID_LEN];
  1636. int res;
  1637. size_t ssid_len;
  1638. u8 bssid[ETH_ALEN];
  1639. int wired;
  1640. res = wpa_drv_get_ssid(wpa_s, ssid);
  1641. if (res < 0) {
  1642. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1643. "driver");
  1644. return NULL;
  1645. }
  1646. ssid_len = res;
  1647. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1648. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1649. "driver");
  1650. return NULL;
  1651. }
  1652. wired = wpa_s->conf->ap_scan == 0 &&
  1653. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1654. entry = wpa_s->conf->ssid;
  1655. while (entry) {
  1656. if (!entry->disabled &&
  1657. ((ssid_len == entry->ssid_len &&
  1658. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1659. (!entry->bssid_set ||
  1660. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1661. return entry;
  1662. #ifdef CONFIG_WPS
  1663. if (!entry->disabled &&
  1664. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1665. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1666. (!entry->bssid_set ||
  1667. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1668. return entry;
  1669. #endif /* CONFIG_WPS */
  1670. if (!entry->disabled && entry->bssid_set &&
  1671. entry->ssid_len == 0 &&
  1672. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1673. return entry;
  1674. entry = entry->next;
  1675. }
  1676. return NULL;
  1677. }
  1678. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  1679. {
  1680. struct wpa_global *global = wpa_s->global;
  1681. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  1682. global->drv_priv[i] = wpa_drivers[i]->global_init();
  1683. if (global->drv_priv[i] == NULL) {
  1684. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  1685. "'%s'", wpa_drivers[i]->name);
  1686. return -1;
  1687. }
  1688. }
  1689. wpa_s->driver = wpa_drivers[i];
  1690. wpa_s->global_drv_priv = global->drv_priv[i];
  1691. return 0;
  1692. }
  1693. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1694. const char *name)
  1695. {
  1696. int i;
  1697. size_t len;
  1698. const char *pos, *driver = name;
  1699. if (wpa_s == NULL)
  1700. return -1;
  1701. if (wpa_drivers[0] == NULL) {
  1702. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1703. "wpa_supplicant");
  1704. return -1;
  1705. }
  1706. if (name == NULL) {
  1707. /* default to first driver in the list */
  1708. return select_driver(wpa_s, 0);
  1709. }
  1710. do {
  1711. pos = os_strchr(driver, ',');
  1712. if (pos)
  1713. len = pos - driver;
  1714. else
  1715. len = os_strlen(driver);
  1716. for (i = 0; wpa_drivers[i]; i++) {
  1717. if (os_strlen(wpa_drivers[i]->name) == len &&
  1718. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1719. 0) {
  1720. /* First driver that succeeds wins */
  1721. if (select_driver(wpa_s, i) == 0)
  1722. return 0;
  1723. }
  1724. }
  1725. driver = pos + 1;
  1726. } while (pos);
  1727. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1728. return -1;
  1729. }
  1730. /**
  1731. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1732. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1733. * with struct wpa_driver_ops::init()
  1734. * @src_addr: Source address of the EAPOL frame
  1735. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1736. * @len: Length of the EAPOL data
  1737. *
  1738. * This function is called for each received EAPOL frame. Most driver
  1739. * interfaces rely on more generic OS mechanism for receiving frames through
  1740. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1741. * take care of received EAPOL frames and deliver them to the core supplicant
  1742. * code by calling this function.
  1743. */
  1744. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1745. const u8 *buf, size_t len)
  1746. {
  1747. struct wpa_supplicant *wpa_s = ctx;
  1748. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1749. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1750. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1751. /*
  1752. * There is possible race condition between receiving the
  1753. * association event and the EAPOL frame since they are coming
  1754. * through different paths from the driver. In order to avoid
  1755. * issues in trying to process the EAPOL frame before receiving
  1756. * association information, lets queue it for processing until
  1757. * the association event is received.
  1758. */
  1759. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1760. "of received EAPOL frame");
  1761. wpabuf_free(wpa_s->pending_eapol_rx);
  1762. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1763. if (wpa_s->pending_eapol_rx) {
  1764. os_get_time(&wpa_s->pending_eapol_rx_time);
  1765. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1766. ETH_ALEN);
  1767. }
  1768. return;
  1769. }
  1770. #ifdef CONFIG_AP
  1771. if (wpa_s->ap_iface) {
  1772. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1773. return;
  1774. }
  1775. #endif /* CONFIG_AP */
  1776. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1777. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1778. "no key management is configured");
  1779. return;
  1780. }
  1781. if (wpa_s->eapol_received == 0 &&
  1782. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1783. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1784. wpa_s->wpa_state != WPA_COMPLETED) &&
  1785. (wpa_s->current_ssid == NULL ||
  1786. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1787. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1788. wpa_supplicant_req_auth_timeout(
  1789. wpa_s,
  1790. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1791. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1792. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1793. 70 : 10, 0);
  1794. }
  1795. wpa_s->eapol_received++;
  1796. if (wpa_s->countermeasures) {
  1797. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1798. "EAPOL packet");
  1799. return;
  1800. }
  1801. #ifdef CONFIG_IBSS_RSN
  1802. if (wpa_s->current_ssid &&
  1803. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1804. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1805. return;
  1806. }
  1807. #endif /* CONFIG_IBSS_RSN */
  1808. /* Source address of the incoming EAPOL frame could be compared to the
  1809. * current BSSID. However, it is possible that a centralized
  1810. * Authenticator could be using another MAC address than the BSSID of
  1811. * an AP, so just allow any address to be used for now. The replies are
  1812. * still sent to the current BSSID (if available), though. */
  1813. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1814. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1815. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1816. return;
  1817. wpa_drv_poll(wpa_s);
  1818. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1819. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1820. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1821. /*
  1822. * Set portValid = TRUE here since we are going to skip 4-way
  1823. * handshake processing which would normally set portValid. We
  1824. * need this to allow the EAPOL state machines to be completed
  1825. * without going through EAPOL-Key handshake.
  1826. */
  1827. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1828. }
  1829. }
  1830. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  1831. {
  1832. if (wpa_s->driver->send_eapol) {
  1833. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1834. if (addr)
  1835. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1836. } else if (!(wpa_s->drv_flags &
  1837. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1838. l2_packet_deinit(wpa_s->l2);
  1839. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1840. wpa_drv_get_mac_addr(wpa_s),
  1841. ETH_P_EAPOL,
  1842. wpa_supplicant_rx_eapol, wpa_s, 0);
  1843. if (wpa_s->l2 == NULL)
  1844. return -1;
  1845. } else {
  1846. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1847. if (addr)
  1848. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1849. }
  1850. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1851. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  1852. return -1;
  1853. }
  1854. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  1855. MAC2STR(wpa_s->own_addr));
  1856. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  1857. return 0;
  1858. }
  1859. static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
  1860. const u8 *buf, size_t len)
  1861. {
  1862. struct wpa_supplicant *wpa_s = ctx;
  1863. const struct l2_ethhdr *eth;
  1864. if (len < sizeof(*eth))
  1865. return;
  1866. eth = (const struct l2_ethhdr *) buf;
  1867. if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
  1868. !(eth->h_dest[0] & 0x01)) {
  1869. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  1870. " (bridge - not for this interface - ignore)",
  1871. MAC2STR(src_addr), MAC2STR(eth->h_dest));
  1872. return;
  1873. }
  1874. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  1875. " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
  1876. wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
  1877. len - sizeof(*eth));
  1878. }
  1879. /**
  1880. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1881. * @wpa_s: Pointer to wpa_supplicant data
  1882. * Returns: 0 on success, -1 on failure
  1883. *
  1884. * This function is called to initialize driver interface parameters.
  1885. * wpa_drv_init() must have been called before this function to initialize the
  1886. * driver interface.
  1887. */
  1888. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1889. {
  1890. static int interface_count = 0;
  1891. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  1892. return -1;
  1893. if (wpa_s->bridge_ifname[0]) {
  1894. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  1895. "interface '%s'", wpa_s->bridge_ifname);
  1896. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1897. wpa_s->own_addr,
  1898. ETH_P_EAPOL,
  1899. wpa_supplicant_rx_eapol_bridge,
  1900. wpa_s, 1);
  1901. if (wpa_s->l2_br == NULL) {
  1902. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  1903. "connection for the bridge interface '%s'",
  1904. wpa_s->bridge_ifname);
  1905. return -1;
  1906. }
  1907. }
  1908. wpa_clear_keys(wpa_s, NULL);
  1909. /* Make sure that TKIP countermeasures are not left enabled (could
  1910. * happen if wpa_supplicant is killed during countermeasures. */
  1911. wpa_drv_set_countermeasures(wpa_s, 0);
  1912. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1913. wpa_drv_flush_pmkid(wpa_s);
  1914. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  1915. wpa_s->prev_scan_wildcard = 0;
  1916. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  1917. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  1918. 100000))
  1919. wpa_supplicant_req_scan(wpa_s, interface_count,
  1920. 100000);
  1921. interface_count++;
  1922. } else
  1923. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1924. return 0;
  1925. }
  1926. static int wpa_supplicant_daemon(const char *pid_file)
  1927. {
  1928. wpa_printf(MSG_DEBUG, "Daemonize..");
  1929. return os_daemonize(pid_file);
  1930. }
  1931. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1932. {
  1933. struct wpa_supplicant *wpa_s;
  1934. wpa_s = os_zalloc(sizeof(*wpa_s));
  1935. if (wpa_s == NULL)
  1936. return NULL;
  1937. wpa_s->scan_req = 1;
  1938. wpa_s->scan_interval = 5;
  1939. wpa_s->new_connection = 1;
  1940. wpa_s->parent = wpa_s;
  1941. wpa_s->sched_scanning = 0;
  1942. return wpa_s;
  1943. }
  1944. #ifdef CONFIG_HT_OVERRIDES
  1945. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  1946. struct ieee80211_ht_capabilities *htcaps,
  1947. struct ieee80211_ht_capabilities *htcaps_mask,
  1948. const char *ht_mcs)
  1949. {
  1950. /* parse ht_mcs into hex array */
  1951. int i;
  1952. const char *tmp = ht_mcs;
  1953. char *end = NULL;
  1954. /* If ht_mcs is null, do not set anything */
  1955. if (!ht_mcs)
  1956. return 0;
  1957. /* This is what we are setting in the kernel */
  1958. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  1959. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  1960. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  1961. errno = 0;
  1962. long v = strtol(tmp, &end, 16);
  1963. if (errno == 0) {
  1964. wpa_msg(wpa_s, MSG_DEBUG,
  1965. "htcap value[%i]: %ld end: %p tmp: %p",
  1966. i, v, end, tmp);
  1967. if (end == tmp)
  1968. break;
  1969. htcaps->supported_mcs_set[i] = v;
  1970. tmp = end;
  1971. } else {
  1972. wpa_msg(wpa_s, MSG_ERROR,
  1973. "Failed to parse ht-mcs: %s, error: %s\n",
  1974. ht_mcs, strerror(errno));
  1975. return -1;
  1976. }
  1977. }
  1978. /*
  1979. * If we were able to parse any values, then set mask for the MCS set.
  1980. */
  1981. if (i) {
  1982. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  1983. IEEE80211_HT_MCS_MASK_LEN - 1);
  1984. /* skip the 3 reserved bits */
  1985. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  1986. 0x1f;
  1987. }
  1988. return 0;
  1989. }
  1990. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  1991. struct ieee80211_ht_capabilities *htcaps,
  1992. struct ieee80211_ht_capabilities *htcaps_mask,
  1993. int disabled)
  1994. {
  1995. u16 msk;
  1996. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  1997. if (disabled == -1)
  1998. return 0;
  1999. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  2000. htcaps_mask->ht_capabilities_info |= msk;
  2001. if (disabled)
  2002. htcaps->ht_capabilities_info &= msk;
  2003. else
  2004. htcaps->ht_capabilities_info |= msk;
  2005. return 0;
  2006. }
  2007. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  2008. struct ieee80211_ht_capabilities *htcaps,
  2009. struct ieee80211_ht_capabilities *htcaps_mask,
  2010. int factor)
  2011. {
  2012. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  2013. if (factor == -1)
  2014. return 0;
  2015. if (factor < 0 || factor > 3) {
  2016. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  2017. "Must be 0-3 or -1", factor);
  2018. return -EINVAL;
  2019. }
  2020. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  2021. htcaps->a_mpdu_params &= ~0x3;
  2022. htcaps->a_mpdu_params |= factor & 0x3;
  2023. return 0;
  2024. }
  2025. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  2026. struct ieee80211_ht_capabilities *htcaps,
  2027. struct ieee80211_ht_capabilities *htcaps_mask,
  2028. int density)
  2029. {
  2030. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  2031. if (density == -1)
  2032. return 0;
  2033. if (density < 0 || density > 7) {
  2034. wpa_msg(wpa_s, MSG_ERROR,
  2035. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  2036. density);
  2037. return -EINVAL;
  2038. }
  2039. htcaps_mask->a_mpdu_params |= 0x1C;
  2040. htcaps->a_mpdu_params &= ~(0x1C);
  2041. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  2042. return 0;
  2043. }
  2044. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  2045. struct ieee80211_ht_capabilities *htcaps,
  2046. struct ieee80211_ht_capabilities *htcaps_mask,
  2047. int disabled)
  2048. {
  2049. /* Masking these out disables HT40 */
  2050. u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  2051. HT_CAP_INFO_SHORT_GI40MHZ);
  2052. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  2053. if (disabled)
  2054. htcaps->ht_capabilities_info &= ~msk;
  2055. else
  2056. htcaps->ht_capabilities_info |= msk;
  2057. htcaps_mask->ht_capabilities_info |= msk;
  2058. return 0;
  2059. }
  2060. void wpa_supplicant_apply_ht_overrides(
  2061. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2062. struct wpa_driver_associate_params *params)
  2063. {
  2064. struct ieee80211_ht_capabilities *htcaps;
  2065. struct ieee80211_ht_capabilities *htcaps_mask;
  2066. if (!ssid)
  2067. return;
  2068. params->disable_ht = ssid->disable_ht;
  2069. if (!params->htcaps || !params->htcaps_mask)
  2070. return;
  2071. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  2072. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  2073. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  2074. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  2075. ssid->disable_max_amsdu);
  2076. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  2077. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  2078. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  2079. }
  2080. #endif /* CONFIG_HT_OVERRIDES */
  2081. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  2082. {
  2083. #ifdef PCSC_FUNCS
  2084. size_t len;
  2085. if (!wpa_s->conf->pcsc_reader)
  2086. return 0;
  2087. wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
  2088. if (!wpa_s->scard)
  2089. return 1;
  2090. if (wpa_s->conf->pcsc_pin &&
  2091. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  2092. scard_deinit(wpa_s->scard);
  2093. wpa_s->scard = NULL;
  2094. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  2095. return -1;
  2096. }
  2097. len = sizeof(wpa_s->imsi) - 1;
  2098. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  2099. scard_deinit(wpa_s->scard);
  2100. wpa_s->scard = NULL;
  2101. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  2102. return -1;
  2103. }
  2104. wpa_s->imsi[len] = '\0';
  2105. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  2106. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  2107. wpa_s->imsi, wpa_s->mnc_len);
  2108. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  2109. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  2110. #endif /* PCSC_FUNCS */
  2111. return 0;
  2112. }
  2113. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  2114. struct wpa_interface *iface)
  2115. {
  2116. const char *ifname, *driver;
  2117. struct wpa_driver_capa capa;
  2118. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  2119. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  2120. iface->confname ? iface->confname : "N/A",
  2121. iface->driver ? iface->driver : "default",
  2122. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  2123. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  2124. if (iface->confname) {
  2125. #ifdef CONFIG_BACKEND_FILE
  2126. wpa_s->confname = os_rel2abs_path(iface->confname);
  2127. if (wpa_s->confname == NULL) {
  2128. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  2129. "for configuration file '%s'.",
  2130. iface->confname);
  2131. return -1;
  2132. }
  2133. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  2134. iface->confname, wpa_s->confname);
  2135. #else /* CONFIG_BACKEND_FILE */
  2136. wpa_s->confname = os_strdup(iface->confname);
  2137. #endif /* CONFIG_BACKEND_FILE */
  2138. wpa_s->conf = wpa_config_read(wpa_s->confname);
  2139. if (wpa_s->conf == NULL) {
  2140. wpa_printf(MSG_ERROR, "Failed to read or parse "
  2141. "configuration '%s'.", wpa_s->confname);
  2142. return -1;
  2143. }
  2144. /*
  2145. * Override ctrl_interface and driver_param if set on command
  2146. * line.
  2147. */
  2148. if (iface->ctrl_interface) {
  2149. os_free(wpa_s->conf->ctrl_interface);
  2150. wpa_s->conf->ctrl_interface =
  2151. os_strdup(iface->ctrl_interface);
  2152. }
  2153. if (iface->driver_param) {
  2154. os_free(wpa_s->conf->driver_param);
  2155. wpa_s->conf->driver_param =
  2156. os_strdup(iface->driver_param);
  2157. }
  2158. } else
  2159. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  2160. iface->driver_param);
  2161. if (wpa_s->conf == NULL) {
  2162. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  2163. return -1;
  2164. }
  2165. if (iface->ifname == NULL) {
  2166. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  2167. return -1;
  2168. }
  2169. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  2170. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  2171. iface->ifname);
  2172. return -1;
  2173. }
  2174. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  2175. if (iface->bridge_ifname) {
  2176. if (os_strlen(iface->bridge_ifname) >=
  2177. sizeof(wpa_s->bridge_ifname)) {
  2178. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  2179. "name '%s'.", iface->bridge_ifname);
  2180. return -1;
  2181. }
  2182. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  2183. sizeof(wpa_s->bridge_ifname));
  2184. }
  2185. /* RSNA Supplicant Key Management - INITIALIZE */
  2186. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  2187. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  2188. /* Initialize driver interface and register driver event handler before
  2189. * L2 receive handler so that association events are processed before
  2190. * EAPOL-Key packets if both become available for the same select()
  2191. * call. */
  2192. driver = iface->driver;
  2193. next_driver:
  2194. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  2195. return -1;
  2196. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  2197. if (wpa_s->drv_priv == NULL) {
  2198. const char *pos;
  2199. pos = driver ? os_strchr(driver, ',') : NULL;
  2200. if (pos) {
  2201. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  2202. "driver interface - try next driver wrapper");
  2203. driver = pos + 1;
  2204. goto next_driver;
  2205. }
  2206. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  2207. "interface");
  2208. return -1;
  2209. }
  2210. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  2211. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  2212. "driver_param '%s'", wpa_s->conf->driver_param);
  2213. return -1;
  2214. }
  2215. ifname = wpa_drv_get_ifname(wpa_s);
  2216. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  2217. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  2218. "interface name with '%s'", ifname);
  2219. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  2220. }
  2221. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  2222. return -1;
  2223. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  2224. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  2225. NULL);
  2226. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  2227. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  2228. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  2229. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  2230. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2231. "dot11RSNAConfigPMKLifetime");
  2232. return -1;
  2233. }
  2234. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  2235. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  2236. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  2237. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2238. "dot11RSNAConfigPMKReauthThreshold");
  2239. return -1;
  2240. }
  2241. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2242. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2243. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2244. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2245. "dot11RSNAConfigSATimeout");
  2246. return -1;
  2247. }
  2248. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  2249. &wpa_s->hw.num_modes,
  2250. &wpa_s->hw.flags);
  2251. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2252. wpa_s->drv_capa_known = 1;
  2253. wpa_s->drv_flags = capa.flags;
  2254. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  2255. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2256. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2257. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2258. wpa_s->max_match_sets = capa.max_match_sets;
  2259. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2260. wpa_s->max_stations = capa.max_stations;
  2261. }
  2262. if (wpa_s->max_remain_on_chan == 0)
  2263. wpa_s->max_remain_on_chan = 1000;
  2264. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2265. return -1;
  2266. #ifdef CONFIG_TDLS
  2267. if (wpa_tdls_init(wpa_s->wpa))
  2268. return -1;
  2269. #endif /* CONFIG_TDLS */
  2270. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2271. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2272. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2273. return -1;
  2274. }
  2275. if (wpas_wps_init(wpa_s))
  2276. return -1;
  2277. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2278. return -1;
  2279. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2280. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2281. if (wpa_s->ctrl_iface == NULL) {
  2282. wpa_printf(MSG_ERROR,
  2283. "Failed to initialize control interface '%s'.\n"
  2284. "You may have another wpa_supplicant process "
  2285. "already running or the file was\n"
  2286. "left by an unclean termination of wpa_supplicant "
  2287. "in which case you will need\n"
  2288. "to manually remove this file before starting "
  2289. "wpa_supplicant again.\n",
  2290. wpa_s->conf->ctrl_interface);
  2291. return -1;
  2292. }
  2293. wpa_s->gas = gas_query_init(wpa_s);
  2294. if (wpa_s->gas == NULL) {
  2295. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2296. return -1;
  2297. }
  2298. #ifdef CONFIG_P2P
  2299. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2300. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2301. return -1;
  2302. }
  2303. #endif /* CONFIG_P2P */
  2304. if (wpa_bss_init(wpa_s) < 0)
  2305. return -1;
  2306. if (pcsc_reader_init(wpa_s) < 0)
  2307. return -1;
  2308. return 0;
  2309. }
  2310. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2311. int notify, int terminate)
  2312. {
  2313. if (wpa_s->drv_priv) {
  2314. wpa_supplicant_deauthenticate(wpa_s,
  2315. WLAN_REASON_DEAUTH_LEAVING);
  2316. wpa_drv_set_countermeasures(wpa_s, 0);
  2317. wpa_clear_keys(wpa_s, NULL);
  2318. }
  2319. wpa_supplicant_cleanup(wpa_s);
  2320. if (wpa_s->drv_priv)
  2321. wpa_drv_deinit(wpa_s);
  2322. if (notify)
  2323. wpas_notify_iface_removed(wpa_s);
  2324. if (terminate)
  2325. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  2326. if (wpa_s->ctrl_iface) {
  2327. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  2328. wpa_s->ctrl_iface = NULL;
  2329. }
  2330. if (wpa_s->conf != NULL) {
  2331. wpa_config_free(wpa_s->conf);
  2332. wpa_s->conf = NULL;
  2333. }
  2334. }
  2335. /**
  2336. * wpa_supplicant_add_iface - Add a new network interface
  2337. * @global: Pointer to global data from wpa_supplicant_init()
  2338. * @iface: Interface configuration options
  2339. * Returns: Pointer to the created interface or %NULL on failure
  2340. *
  2341. * This function is used to add new network interfaces for %wpa_supplicant.
  2342. * This can be called before wpa_supplicant_run() to add interfaces before the
  2343. * main event loop has been started. In addition, new interfaces can be added
  2344. * dynamically while %wpa_supplicant is already running. This could happen,
  2345. * e.g., when a hotplug network adapter is inserted.
  2346. */
  2347. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2348. struct wpa_interface *iface)
  2349. {
  2350. struct wpa_supplicant *wpa_s;
  2351. struct wpa_interface t_iface;
  2352. struct wpa_ssid *ssid;
  2353. if (global == NULL || iface == NULL)
  2354. return NULL;
  2355. wpa_s = wpa_supplicant_alloc();
  2356. if (wpa_s == NULL)
  2357. return NULL;
  2358. wpa_s->global = global;
  2359. t_iface = *iface;
  2360. if (global->params.override_driver) {
  2361. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2362. "('%s' -> '%s')",
  2363. iface->driver, global->params.override_driver);
  2364. t_iface.driver = global->params.override_driver;
  2365. }
  2366. if (global->params.override_ctrl_interface) {
  2367. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2368. "ctrl_interface ('%s' -> '%s')",
  2369. iface->ctrl_interface,
  2370. global->params.override_ctrl_interface);
  2371. t_iface.ctrl_interface =
  2372. global->params.override_ctrl_interface;
  2373. }
  2374. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2375. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2376. iface->ifname);
  2377. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  2378. os_free(wpa_s);
  2379. return NULL;
  2380. }
  2381. /* Notify the control interfaces about new iface */
  2382. if (wpas_notify_iface_added(wpa_s)) {
  2383. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  2384. os_free(wpa_s);
  2385. return NULL;
  2386. }
  2387. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2388. wpas_notify_network_added(wpa_s, ssid);
  2389. wpa_s->next = global->ifaces;
  2390. global->ifaces = wpa_s;
  2391. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2392. return wpa_s;
  2393. }
  2394. /**
  2395. * wpa_supplicant_remove_iface - Remove a network interface
  2396. * @global: Pointer to global data from wpa_supplicant_init()
  2397. * @wpa_s: Pointer to the network interface to be removed
  2398. * Returns: 0 if interface was removed, -1 if interface was not found
  2399. *
  2400. * This function can be used to dynamically remove network interfaces from
  2401. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2402. * addition, this function is used to remove all remaining interfaces when
  2403. * %wpa_supplicant is terminated.
  2404. */
  2405. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2406. struct wpa_supplicant *wpa_s,
  2407. int terminate)
  2408. {
  2409. struct wpa_supplicant *prev;
  2410. /* Remove interface from the global list of interfaces */
  2411. prev = global->ifaces;
  2412. if (prev == wpa_s) {
  2413. global->ifaces = wpa_s->next;
  2414. } else {
  2415. while (prev && prev->next != wpa_s)
  2416. prev = prev->next;
  2417. if (prev == NULL)
  2418. return -1;
  2419. prev->next = wpa_s->next;
  2420. }
  2421. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2422. if (global->p2p_group_formation == wpa_s)
  2423. global->p2p_group_formation = NULL;
  2424. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  2425. os_free(wpa_s);
  2426. return 0;
  2427. }
  2428. /**
  2429. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2430. * @wpa_s: Pointer to the network interface
  2431. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2432. */
  2433. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2434. {
  2435. const char *eapol_method;
  2436. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2437. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2438. return "NO-EAP";
  2439. }
  2440. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2441. if (eapol_method == NULL)
  2442. return "UNKNOWN-EAP";
  2443. return eapol_method;
  2444. }
  2445. /**
  2446. * wpa_supplicant_get_iface - Get a new network interface
  2447. * @global: Pointer to global data from wpa_supplicant_init()
  2448. * @ifname: Interface name
  2449. * Returns: Pointer to the interface or %NULL if not found
  2450. */
  2451. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2452. const char *ifname)
  2453. {
  2454. struct wpa_supplicant *wpa_s;
  2455. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2456. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2457. return wpa_s;
  2458. }
  2459. return NULL;
  2460. }
  2461. #ifndef CONFIG_NO_WPA_MSG
  2462. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2463. {
  2464. struct wpa_supplicant *wpa_s = ctx;
  2465. if (wpa_s == NULL)
  2466. return NULL;
  2467. return wpa_s->ifname;
  2468. }
  2469. #endif /* CONFIG_NO_WPA_MSG */
  2470. /**
  2471. * wpa_supplicant_init - Initialize %wpa_supplicant
  2472. * @params: Parameters for %wpa_supplicant
  2473. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2474. *
  2475. * This function is used to initialize %wpa_supplicant. After successful
  2476. * initialization, the returned data pointer can be used to add and remove
  2477. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2478. */
  2479. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2480. {
  2481. struct wpa_global *global;
  2482. int ret, i;
  2483. if (params == NULL)
  2484. return NULL;
  2485. #ifdef CONFIG_DRIVER_NDIS
  2486. {
  2487. void driver_ndis_init_ops(void);
  2488. driver_ndis_init_ops();
  2489. }
  2490. #endif /* CONFIG_DRIVER_NDIS */
  2491. #ifndef CONFIG_NO_WPA_MSG
  2492. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2493. #endif /* CONFIG_NO_WPA_MSG */
  2494. wpa_debug_open_file(params->wpa_debug_file_path);
  2495. if (params->wpa_debug_syslog)
  2496. wpa_debug_open_syslog();
  2497. if (params->wpa_debug_tracing) {
  2498. ret = wpa_debug_open_linux_tracing();
  2499. if (ret) {
  2500. wpa_printf(MSG_ERROR,
  2501. "Failed to enable trace logging");
  2502. return NULL;
  2503. }
  2504. }
  2505. ret = eap_register_methods();
  2506. if (ret) {
  2507. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2508. if (ret == -2)
  2509. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2510. "the same EAP type.");
  2511. return NULL;
  2512. }
  2513. global = os_zalloc(sizeof(*global));
  2514. if (global == NULL)
  2515. return NULL;
  2516. dl_list_init(&global->p2p_srv_bonjour);
  2517. dl_list_init(&global->p2p_srv_upnp);
  2518. global->params.daemonize = params->daemonize;
  2519. global->params.wait_for_monitor = params->wait_for_monitor;
  2520. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2521. if (params->pid_file)
  2522. global->params.pid_file = os_strdup(params->pid_file);
  2523. if (params->ctrl_interface)
  2524. global->params.ctrl_interface =
  2525. os_strdup(params->ctrl_interface);
  2526. if (params->override_driver)
  2527. global->params.override_driver =
  2528. os_strdup(params->override_driver);
  2529. if (params->override_ctrl_interface)
  2530. global->params.override_ctrl_interface =
  2531. os_strdup(params->override_ctrl_interface);
  2532. wpa_debug_level = global->params.wpa_debug_level =
  2533. params->wpa_debug_level;
  2534. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2535. params->wpa_debug_show_keys;
  2536. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2537. params->wpa_debug_timestamp;
  2538. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2539. if (eloop_init()) {
  2540. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2541. wpa_supplicant_deinit(global);
  2542. return NULL;
  2543. }
  2544. random_init(params->entropy_file);
  2545. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2546. if (global->ctrl_iface == NULL) {
  2547. wpa_supplicant_deinit(global);
  2548. return NULL;
  2549. }
  2550. if (wpas_notify_supplicant_initialized(global)) {
  2551. wpa_supplicant_deinit(global);
  2552. return NULL;
  2553. }
  2554. for (i = 0; wpa_drivers[i]; i++)
  2555. global->drv_count++;
  2556. if (global->drv_count == 0) {
  2557. wpa_printf(MSG_ERROR, "No drivers enabled");
  2558. wpa_supplicant_deinit(global);
  2559. return NULL;
  2560. }
  2561. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2562. if (global->drv_priv == NULL) {
  2563. wpa_supplicant_deinit(global);
  2564. return NULL;
  2565. }
  2566. return global;
  2567. }
  2568. /**
  2569. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2570. * @global: Pointer to global data from wpa_supplicant_init()
  2571. * Returns: 0 after successful event loop run, -1 on failure
  2572. *
  2573. * This function starts the main event loop and continues running as long as
  2574. * there are any remaining events. In most cases, this function is running as
  2575. * long as the %wpa_supplicant process in still in use.
  2576. */
  2577. int wpa_supplicant_run(struct wpa_global *global)
  2578. {
  2579. struct wpa_supplicant *wpa_s;
  2580. if (global->params.daemonize &&
  2581. wpa_supplicant_daemon(global->params.pid_file))
  2582. return -1;
  2583. if (global->params.wait_for_monitor) {
  2584. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2585. if (wpa_s->ctrl_iface)
  2586. wpa_supplicant_ctrl_iface_wait(
  2587. wpa_s->ctrl_iface);
  2588. }
  2589. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2590. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2591. eloop_run();
  2592. return 0;
  2593. }
  2594. /**
  2595. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2596. * @global: Pointer to global data from wpa_supplicant_init()
  2597. *
  2598. * This function is called to deinitialize %wpa_supplicant and to free all
  2599. * allocated resources. Remaining network interfaces will also be removed.
  2600. */
  2601. void wpa_supplicant_deinit(struct wpa_global *global)
  2602. {
  2603. int i;
  2604. if (global == NULL)
  2605. return;
  2606. #ifdef CONFIG_P2P
  2607. wpas_p2p_deinit_global(global);
  2608. #endif /* CONFIG_P2P */
  2609. while (global->ifaces)
  2610. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  2611. if (global->ctrl_iface)
  2612. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2613. wpas_notify_supplicant_deinitialized(global);
  2614. eap_peer_unregister_methods();
  2615. #ifdef CONFIG_AP
  2616. eap_server_unregister_methods();
  2617. #endif /* CONFIG_AP */
  2618. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2619. if (!global->drv_priv[i])
  2620. continue;
  2621. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2622. }
  2623. os_free(global->drv_priv);
  2624. random_deinit();
  2625. eloop_destroy();
  2626. if (global->params.pid_file) {
  2627. os_daemonize_terminate(global->params.pid_file);
  2628. os_free(global->params.pid_file);
  2629. }
  2630. os_free(global->params.ctrl_interface);
  2631. os_free(global->params.override_driver);
  2632. os_free(global->params.override_ctrl_interface);
  2633. os_free(global->p2p_disallow_freq);
  2634. os_free(global);
  2635. wpa_debug_close_syslog();
  2636. wpa_debug_close_file();
  2637. wpa_debug_close_linux_tracing();
  2638. }
  2639. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2640. {
  2641. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2642. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2643. char country[3];
  2644. country[0] = wpa_s->conf->country[0];
  2645. country[1] = wpa_s->conf->country[1];
  2646. country[2] = '\0';
  2647. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2648. wpa_printf(MSG_ERROR, "Failed to set country code "
  2649. "'%s'", country);
  2650. }
  2651. }
  2652. #ifdef CONFIG_WPS
  2653. wpas_wps_update_config(wpa_s);
  2654. #endif /* CONFIG_WPS */
  2655. #ifdef CONFIG_P2P
  2656. wpas_p2p_update_config(wpa_s);
  2657. #endif /* CONFIG_P2P */
  2658. wpa_s->conf->changed_parameters = 0;
  2659. }
  2660. static void add_freq(int *freqs, int *num_freqs, int freq)
  2661. {
  2662. int i;
  2663. for (i = 0; i < *num_freqs; i++) {
  2664. if (freqs[i] == freq)
  2665. return;
  2666. }
  2667. freqs[*num_freqs] = freq;
  2668. (*num_freqs)++;
  2669. }
  2670. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2671. {
  2672. struct wpa_bss *bss, *cbss;
  2673. const int max_freqs = 10;
  2674. int *freqs;
  2675. int num_freqs = 0;
  2676. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2677. if (freqs == NULL)
  2678. return NULL;
  2679. cbss = wpa_s->current_bss;
  2680. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2681. if (bss == cbss)
  2682. continue;
  2683. if (bss->ssid_len == cbss->ssid_len &&
  2684. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2685. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2686. add_freq(freqs, &num_freqs, bss->freq);
  2687. if (num_freqs == max_freqs)
  2688. break;
  2689. }
  2690. }
  2691. if (num_freqs == 0) {
  2692. os_free(freqs);
  2693. freqs = NULL;
  2694. }
  2695. return freqs;
  2696. }
  2697. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2698. {
  2699. int timeout;
  2700. int count;
  2701. int *freqs = NULL;
  2702. /*
  2703. * Remove possible authentication timeout since the connection failed.
  2704. */
  2705. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2706. /*
  2707. * Add the failed BSSID into the blacklist and speed up next scan
  2708. * attempt if there could be other APs that could accept association.
  2709. * The current blacklist count indicates how many times we have tried
  2710. * connecting to this AP and multiple attempts mean that other APs are
  2711. * either not available or has already been tried, so that we can start
  2712. * increasing the delay here to avoid constant scanning.
  2713. */
  2714. count = wpa_blacklist_add(wpa_s, bssid);
  2715. if (count == 1 && wpa_s->current_bss) {
  2716. /*
  2717. * This BSS was not in the blacklist before. If there is
  2718. * another BSS available for the same ESS, we should try that
  2719. * next. Otherwise, we may as well try this one once more
  2720. * before allowing other, likely worse, ESSes to be considered.
  2721. */
  2722. freqs = get_bss_freqs_in_ess(wpa_s);
  2723. if (freqs) {
  2724. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2725. "has been seen; try it next");
  2726. wpa_blacklist_add(wpa_s, bssid);
  2727. /*
  2728. * On the next scan, go through only the known channels
  2729. * used in this ESS based on previous scans to speed up
  2730. * common load balancing use case.
  2731. */
  2732. os_free(wpa_s->next_scan_freqs);
  2733. wpa_s->next_scan_freqs = freqs;
  2734. }
  2735. }
  2736. switch (count) {
  2737. case 1:
  2738. timeout = 100;
  2739. break;
  2740. case 2:
  2741. timeout = 500;
  2742. break;
  2743. case 3:
  2744. timeout = 1000;
  2745. break;
  2746. default:
  2747. timeout = 5000;
  2748. }
  2749. /*
  2750. * TODO: if more than one possible AP is available in scan results,
  2751. * could try the other ones before requesting a new scan.
  2752. */
  2753. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2754. 1000 * (timeout % 1000));
  2755. }
  2756. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  2757. {
  2758. return wpa_s->conf->ap_scan == 2 ||
  2759. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  2760. }
  2761. #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
  2762. int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
  2763. struct wpa_ssid *ssid,
  2764. const char *field,
  2765. const char *value)
  2766. {
  2767. #ifdef IEEE8021X_EAPOL
  2768. struct eap_peer_config *eap = &ssid->eap;
  2769. wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
  2770. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
  2771. (const u8 *) value, os_strlen(value));
  2772. switch (wpa_supplicant_ctrl_req_from_string(field)) {
  2773. case WPA_CTRL_REQ_EAP_IDENTITY:
  2774. os_free(eap->identity);
  2775. eap->identity = (u8 *) os_strdup(value);
  2776. eap->identity_len = os_strlen(value);
  2777. eap->pending_req_identity = 0;
  2778. if (ssid == wpa_s->current_ssid)
  2779. wpa_s->reassociate = 1;
  2780. break;
  2781. case WPA_CTRL_REQ_EAP_PASSWORD:
  2782. os_free(eap->password);
  2783. eap->password = (u8 *) os_strdup(value);
  2784. eap->password_len = os_strlen(value);
  2785. eap->pending_req_password = 0;
  2786. if (ssid == wpa_s->current_ssid)
  2787. wpa_s->reassociate = 1;
  2788. break;
  2789. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  2790. os_free(eap->new_password);
  2791. eap->new_password = (u8 *) os_strdup(value);
  2792. eap->new_password_len = os_strlen(value);
  2793. eap->pending_req_new_password = 0;
  2794. if (ssid == wpa_s->current_ssid)
  2795. wpa_s->reassociate = 1;
  2796. break;
  2797. case WPA_CTRL_REQ_EAP_PIN:
  2798. os_free(eap->pin);
  2799. eap->pin = os_strdup(value);
  2800. eap->pending_req_pin = 0;
  2801. if (ssid == wpa_s->current_ssid)
  2802. wpa_s->reassociate = 1;
  2803. break;
  2804. case WPA_CTRL_REQ_EAP_OTP:
  2805. os_free(eap->otp);
  2806. eap->otp = (u8 *) os_strdup(value);
  2807. eap->otp_len = os_strlen(value);
  2808. os_free(eap->pending_req_otp);
  2809. eap->pending_req_otp = NULL;
  2810. eap->pending_req_otp_len = 0;
  2811. break;
  2812. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  2813. os_free(eap->private_key_passwd);
  2814. eap->private_key_passwd = (u8 *) os_strdup(value);
  2815. eap->pending_req_passphrase = 0;
  2816. if (ssid == wpa_s->current_ssid)
  2817. wpa_s->reassociate = 1;
  2818. break;
  2819. default:
  2820. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
  2821. return -1;
  2822. }
  2823. return 0;
  2824. #else /* IEEE8021X_EAPOL */
  2825. wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
  2826. return -1;
  2827. #endif /* IEEE8021X_EAPOL */
  2828. }
  2829. #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */