wpa_supplicant.c 122 KB

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