wpa_supplicant.c 108 KB

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