wpa_supplicant.c 143 KB

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