wpa_supplicant.c 124 KB

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