ctrl_iface.c 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-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. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/version.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/wpa_ctrl.h"
  16. #include "eap_peer/eap.h"
  17. #include "eapol_supp/eapol_supp_sm.h"
  18. #include "rsn_supp/wpa.h"
  19. #include "rsn_supp/preauth.h"
  20. #include "rsn_supp/pmksa_cache.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wps/wps.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "wps_supplicant.h"
  27. #include "ibss_rsn.h"
  28. #include "ap.h"
  29. #include "p2p_supplicant.h"
  30. #include "p2p/p2p.h"
  31. #include "hs20_supplicant.h"
  32. #include "wifi_display.h"
  33. #include "notify.h"
  34. #include "bss.h"
  35. #include "scan.h"
  36. #include "ctrl_iface.h"
  37. #include "interworking.h"
  38. #include "blacklist.h"
  39. #include "autoscan.h"
  40. #include "wnm_sta.h"
  41. #include "offchannel.h"
  42. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  43. char *buf, int len);
  44. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  45. char *buf, int len);
  46. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  47. char *val);
  48. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  49. {
  50. char *pos;
  51. u8 addr[ETH_ALEN], *filter = NULL, *n;
  52. size_t count = 0;
  53. pos = val;
  54. while (pos) {
  55. if (*pos == '\0')
  56. break;
  57. if (hwaddr_aton(pos, addr)) {
  58. os_free(filter);
  59. return -1;
  60. }
  61. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  62. if (n == NULL) {
  63. os_free(filter);
  64. return -1;
  65. }
  66. filter = n;
  67. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  68. count++;
  69. pos = os_strchr(pos, ' ');
  70. if (pos)
  71. pos++;
  72. }
  73. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  74. os_free(wpa_s->bssid_filter);
  75. wpa_s->bssid_filter = filter;
  76. wpa_s->bssid_filter_count = count;
  77. return 0;
  78. }
  79. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  80. {
  81. char *pos;
  82. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  83. struct wpa_ssid_value *ssid = NULL, *ns;
  84. size_t count = 0, ssid_count = 0;
  85. struct wpa_ssid *c;
  86. /*
  87. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  88. * SSID_SPEC ::= ssid <SSID_HEX>
  89. * BSSID_SPEC ::= bssid <BSSID_HEX>
  90. */
  91. pos = val;
  92. while (pos) {
  93. if (*pos == '\0')
  94. break;
  95. if (os_strncmp(pos, "bssid ", 6) == 0) {
  96. int res;
  97. pos += 6;
  98. res = hwaddr_aton2(pos, addr);
  99. if (res < 0) {
  100. os_free(ssid);
  101. os_free(bssid);
  102. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  103. "BSSID value '%s'", pos);
  104. return -1;
  105. }
  106. pos += res;
  107. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  108. if (n == NULL) {
  109. os_free(ssid);
  110. os_free(bssid);
  111. return -1;
  112. }
  113. bssid = n;
  114. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  115. count++;
  116. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  117. char *end;
  118. pos += 5;
  119. end = pos;
  120. while (*end) {
  121. if (*end == '\0' || *end == ' ')
  122. break;
  123. end++;
  124. }
  125. ns = os_realloc_array(ssid, ssid_count + 1,
  126. sizeof(struct wpa_ssid_value));
  127. if (ns == NULL) {
  128. os_free(ssid);
  129. os_free(bssid);
  130. return -1;
  131. }
  132. ssid = ns;
  133. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  134. hexstr2bin(pos, ssid[ssid_count].ssid,
  135. (end - pos) / 2) < 0) {
  136. os_free(ssid);
  137. os_free(bssid);
  138. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  139. "SSID value '%s'", pos);
  140. return -1;
  141. }
  142. ssid[ssid_count].ssid_len = (end - pos) / 2;
  143. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  144. ssid[ssid_count].ssid,
  145. ssid[ssid_count].ssid_len);
  146. ssid_count++;
  147. pos = end;
  148. } else {
  149. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  150. "'%s'", pos);
  151. os_free(ssid);
  152. os_free(bssid);
  153. return -1;
  154. }
  155. pos = os_strchr(pos, ' ');
  156. if (pos)
  157. pos++;
  158. }
  159. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  160. os_free(wpa_s->disallow_aps_bssid);
  161. wpa_s->disallow_aps_bssid = bssid;
  162. wpa_s->disallow_aps_bssid_count = count;
  163. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  164. os_free(wpa_s->disallow_aps_ssid);
  165. wpa_s->disallow_aps_ssid = ssid;
  166. wpa_s->disallow_aps_ssid_count = ssid_count;
  167. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  168. return 0;
  169. c = wpa_s->current_ssid;
  170. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  171. return 0;
  172. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  173. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  174. return 0;
  175. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  176. "because current AP was marked disallowed");
  177. #ifdef CONFIG_SME
  178. wpa_s->sme.prev_bssid_set = 0;
  179. #endif /* CONFIG_SME */
  180. wpa_s->reassociate = 1;
  181. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  182. wpa_supplicant_req_scan(wpa_s, 0, 0);
  183. return 0;
  184. }
  185. #ifndef CONFIG_NO_CONFIG_BLOBS
  186. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  187. {
  188. char *name = pos;
  189. struct wpa_config_blob *blob;
  190. size_t len;
  191. pos = os_strchr(pos, ' ');
  192. if (pos == NULL)
  193. return -1;
  194. *pos++ = '\0';
  195. len = os_strlen(pos);
  196. if (len & 1)
  197. return -1;
  198. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  199. blob = os_zalloc(sizeof(*blob));
  200. if (blob == NULL)
  201. return -1;
  202. blob->name = os_strdup(name);
  203. blob->data = os_malloc(len / 2);
  204. if (blob->name == NULL || blob->data == NULL) {
  205. wpa_config_free_blob(blob);
  206. return -1;
  207. }
  208. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  209. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  210. wpa_config_free_blob(blob);
  211. return -1;
  212. }
  213. blob->len = len / 2;
  214. wpa_config_set_blob(wpa_s->conf, blob);
  215. return 0;
  216. }
  217. #endif /* CONFIG_NO_CONFIG_BLOBS */
  218. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  219. {
  220. char *params;
  221. char *pos;
  222. int *freqs = NULL;
  223. int ret;
  224. if (atoi(cmd)) {
  225. params = os_strchr(cmd, ' ');
  226. os_free(wpa_s->manual_sched_scan_freqs);
  227. if (params) {
  228. params++;
  229. pos = os_strstr(params, "freq=");
  230. if (pos)
  231. freqs = freq_range_to_channel_list(wpa_s,
  232. pos + 5);
  233. }
  234. wpa_s->manual_sched_scan_freqs = freqs;
  235. ret = wpas_start_pno(wpa_s);
  236. } else {
  237. ret = wpas_stop_pno(wpa_s);
  238. }
  239. return ret;
  240. }
  241. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  242. char *cmd)
  243. {
  244. char *value;
  245. int ret = 0;
  246. value = os_strchr(cmd, ' ');
  247. if (value == NULL)
  248. return -1;
  249. *value++ = '\0';
  250. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  251. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  252. eapol_sm_configure(wpa_s->eapol,
  253. atoi(value), -1, -1, -1);
  254. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  255. eapol_sm_configure(wpa_s->eapol,
  256. -1, atoi(value), -1, -1);
  257. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  258. eapol_sm_configure(wpa_s->eapol,
  259. -1, -1, atoi(value), -1);
  260. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  261. eapol_sm_configure(wpa_s->eapol,
  262. -1, -1, -1, atoi(value));
  263. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  264. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  265. atoi(value)))
  266. ret = -1;
  267. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  268. 0) {
  269. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  270. atoi(value)))
  271. ret = -1;
  272. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  273. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  274. ret = -1;
  275. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  276. wpa_s->wps_fragment_size = atoi(value);
  277. #ifdef CONFIG_WPS_TESTING
  278. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  279. long int val;
  280. val = strtol(value, NULL, 0);
  281. if (val < 0 || val > 0xff) {
  282. ret = -1;
  283. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  284. "wps_version_number %ld", val);
  285. } else {
  286. wps_version_number = val;
  287. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  288. "version %u.%u",
  289. (wps_version_number & 0xf0) >> 4,
  290. wps_version_number & 0x0f);
  291. }
  292. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  293. wps_testing_dummy_cred = atoi(value);
  294. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  295. wps_testing_dummy_cred);
  296. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  297. wps_corrupt_pkhash = atoi(value);
  298. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  299. wps_corrupt_pkhash);
  300. #endif /* CONFIG_WPS_TESTING */
  301. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  302. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  303. ret = -1;
  304. #ifdef CONFIG_TDLS
  305. #ifdef CONFIG_TDLS_TESTING
  306. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  307. extern unsigned int tdls_testing;
  308. tdls_testing = strtol(value, NULL, 0);
  309. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  310. #endif /* CONFIG_TDLS_TESTING */
  311. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  312. int disabled = atoi(value);
  313. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  314. if (disabled) {
  315. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  316. ret = -1;
  317. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  318. ret = -1;
  319. wpa_tdls_enable(wpa_s->wpa, !disabled);
  320. #endif /* CONFIG_TDLS */
  321. } else if (os_strcasecmp(cmd, "pno") == 0) {
  322. ret = wpas_ctrl_pno(wpa_s, value);
  323. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  324. int disabled = atoi(value);
  325. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  326. ret = -1;
  327. else if (disabled)
  328. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  329. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  330. if (os_strcmp(value, "disable") == 0)
  331. wpa_s->set_sta_uapsd = 0;
  332. else {
  333. int be, bk, vi, vo;
  334. char *pos;
  335. /* format: BE,BK,VI,VO;max SP Length */
  336. be = atoi(value);
  337. pos = os_strchr(value, ',');
  338. if (pos == NULL)
  339. return -1;
  340. pos++;
  341. bk = atoi(pos);
  342. pos = os_strchr(pos, ',');
  343. if (pos == NULL)
  344. return -1;
  345. pos++;
  346. vi = atoi(pos);
  347. pos = os_strchr(pos, ',');
  348. if (pos == NULL)
  349. return -1;
  350. pos++;
  351. vo = atoi(pos);
  352. /* ignore max SP Length for now */
  353. wpa_s->set_sta_uapsd = 1;
  354. wpa_s->sta_uapsd = 0;
  355. if (be)
  356. wpa_s->sta_uapsd |= BIT(0);
  357. if (bk)
  358. wpa_s->sta_uapsd |= BIT(1);
  359. if (vi)
  360. wpa_s->sta_uapsd |= BIT(2);
  361. if (vo)
  362. wpa_s->sta_uapsd |= BIT(3);
  363. }
  364. } else if (os_strcasecmp(cmd, "ps") == 0) {
  365. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  366. #ifdef CONFIG_WIFI_DISPLAY
  367. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  368. int enabled = !!atoi(value);
  369. if (enabled && !wpa_s->global->p2p)
  370. ret = -1;
  371. else
  372. wifi_display_enable(wpa_s->global, enabled);
  373. #endif /* CONFIG_WIFI_DISPLAY */
  374. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  375. ret = set_bssid_filter(wpa_s, value);
  376. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  377. ret = set_disallow_aps(wpa_s, value);
  378. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  379. wpa_s->no_keep_alive = !!atoi(value);
  380. #ifdef CONFIG_TESTING_OPTIONS
  381. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  382. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  383. #endif /* CONFIG_TESTING_OPTIONS */
  384. #ifndef CONFIG_NO_CONFIG_BLOBS
  385. } else if (os_strcmp(cmd, "blob") == 0) {
  386. ret = wpas_ctrl_set_blob(wpa_s, value);
  387. #endif /* CONFIG_NO_CONFIG_BLOBS */
  388. } else {
  389. value[-1] = '=';
  390. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  391. if (ret == 0)
  392. wpa_supplicant_update_config(wpa_s);
  393. }
  394. return ret;
  395. }
  396. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  397. char *cmd, char *buf, size_t buflen)
  398. {
  399. int res = -1;
  400. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  401. if (os_strcmp(cmd, "version") == 0) {
  402. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  403. } else if (os_strcasecmp(cmd, "country") == 0) {
  404. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  405. res = os_snprintf(buf, buflen, "%c%c",
  406. wpa_s->conf->country[0],
  407. wpa_s->conf->country[1]);
  408. #ifdef CONFIG_WIFI_DISPLAY
  409. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  410. int enabled;
  411. if (wpa_s->global->p2p == NULL ||
  412. wpa_s->global->p2p_disabled)
  413. enabled = 0;
  414. else
  415. enabled = wpa_s->global->wifi_display;
  416. res = os_snprintf(buf, buflen, "%d", enabled);
  417. if (res < 0 || (unsigned int) res >= buflen)
  418. return -1;
  419. return res;
  420. #endif /* CONFIG_WIFI_DISPLAY */
  421. #ifdef CONFIG_TESTING_GET_GTK
  422. } else if (os_strcmp(cmd, "gtk") == 0) {
  423. if (wpa_s->last_gtk_len == 0)
  424. return -1;
  425. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  426. wpa_s->last_gtk_len);
  427. return res;
  428. #endif /* CONFIG_TESTING_GET_GTK */
  429. }
  430. if (res < 0 || (unsigned int) res >= buflen)
  431. return -1;
  432. return res;
  433. }
  434. #ifdef IEEE8021X_EAPOL
  435. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  436. char *addr)
  437. {
  438. u8 bssid[ETH_ALEN];
  439. struct wpa_ssid *ssid = wpa_s->current_ssid;
  440. if (hwaddr_aton(addr, bssid)) {
  441. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  442. "'%s'", addr);
  443. return -1;
  444. }
  445. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  446. rsn_preauth_deinit(wpa_s->wpa);
  447. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  448. return -1;
  449. return 0;
  450. }
  451. #endif /* IEEE8021X_EAPOL */
  452. #ifdef CONFIG_PEERKEY
  453. /* MLME-STKSTART.request(peer) */
  454. static int wpa_supplicant_ctrl_iface_stkstart(
  455. struct wpa_supplicant *wpa_s, char *addr)
  456. {
  457. u8 peer[ETH_ALEN];
  458. if (hwaddr_aton(addr, peer)) {
  459. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  460. "address '%s'", addr);
  461. return -1;
  462. }
  463. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  464. MAC2STR(peer));
  465. return wpa_sm_stkstart(wpa_s->wpa, peer);
  466. }
  467. #endif /* CONFIG_PEERKEY */
  468. #ifdef CONFIG_TDLS
  469. static int wpa_supplicant_ctrl_iface_tdls_discover(
  470. struct wpa_supplicant *wpa_s, char *addr)
  471. {
  472. u8 peer[ETH_ALEN];
  473. int ret;
  474. if (hwaddr_aton(addr, peer)) {
  475. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  476. "address '%s'", addr);
  477. return -1;
  478. }
  479. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  480. MAC2STR(peer));
  481. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  482. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  483. else
  484. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  485. return ret;
  486. }
  487. static int wpa_supplicant_ctrl_iface_tdls_setup(
  488. struct wpa_supplicant *wpa_s, char *addr)
  489. {
  490. u8 peer[ETH_ALEN];
  491. int ret;
  492. if (hwaddr_aton(addr, peer)) {
  493. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  494. "address '%s'", addr);
  495. return -1;
  496. }
  497. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  498. MAC2STR(peer));
  499. if ((wpa_s->conf->tdls_external_control) &&
  500. wpa_tdls_is_external_setup(wpa_s->wpa))
  501. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  502. wpa_tdls_remove(wpa_s->wpa, peer);
  503. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  504. ret = wpa_tdls_start(wpa_s->wpa, peer);
  505. else
  506. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  507. return ret;
  508. }
  509. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  510. struct wpa_supplicant *wpa_s, char *addr)
  511. {
  512. u8 peer[ETH_ALEN];
  513. int ret;
  514. if (hwaddr_aton(addr, peer)) {
  515. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  516. "address '%s'", addr);
  517. return -1;
  518. }
  519. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  520. MAC2STR(peer));
  521. if ((wpa_s->conf->tdls_external_control) &&
  522. wpa_tdls_is_external_setup(wpa_s->wpa))
  523. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  524. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  525. ret = wpa_tdls_teardown_link(
  526. wpa_s->wpa, peer,
  527. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  528. else
  529. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  530. return ret;
  531. }
  532. static int ctrl_iface_get_capability_tdls(
  533. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  534. {
  535. int ret;
  536. ret = os_snprintf(buf, buflen, "%s\n",
  537. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  538. (wpa_s->drv_flags &
  539. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  540. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  541. if (ret < 0 || (size_t) ret > buflen)
  542. return -1;
  543. return ret;
  544. }
  545. #endif /* CONFIG_TDLS */
  546. #ifdef CONFIG_IEEE80211R
  547. static int wpa_supplicant_ctrl_iface_ft_ds(
  548. struct wpa_supplicant *wpa_s, char *addr)
  549. {
  550. u8 target_ap[ETH_ALEN];
  551. struct wpa_bss *bss;
  552. const u8 *mdie;
  553. if (hwaddr_aton(addr, target_ap)) {
  554. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  555. "address '%s'", addr);
  556. return -1;
  557. }
  558. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  559. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  560. if (bss)
  561. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  562. else
  563. mdie = NULL;
  564. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  565. }
  566. #endif /* CONFIG_IEEE80211R */
  567. #ifdef CONFIG_WPS
  568. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  569. char *cmd)
  570. {
  571. u8 bssid[ETH_ALEN], *_bssid = bssid;
  572. #ifdef CONFIG_P2P
  573. u8 p2p_dev_addr[ETH_ALEN];
  574. #endif /* CONFIG_P2P */
  575. #ifdef CONFIG_AP
  576. u8 *_p2p_dev_addr = NULL;
  577. #endif /* CONFIG_AP */
  578. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  579. _bssid = NULL;
  580. #ifdef CONFIG_P2P
  581. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  582. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  583. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  584. "P2P Device Address '%s'",
  585. cmd + 13);
  586. return -1;
  587. }
  588. _p2p_dev_addr = p2p_dev_addr;
  589. #endif /* CONFIG_P2P */
  590. } else if (hwaddr_aton(cmd, bssid)) {
  591. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  592. cmd);
  593. return -1;
  594. }
  595. #ifdef CONFIG_AP
  596. if (wpa_s->ap_iface)
  597. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  598. #endif /* CONFIG_AP */
  599. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  600. }
  601. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  602. char *cmd, char *buf,
  603. size_t buflen)
  604. {
  605. u8 bssid[ETH_ALEN], *_bssid = bssid;
  606. char *pin;
  607. int ret;
  608. pin = os_strchr(cmd, ' ');
  609. if (pin)
  610. *pin++ = '\0';
  611. if (os_strcmp(cmd, "any") == 0)
  612. _bssid = NULL;
  613. else if (os_strcmp(cmd, "get") == 0) {
  614. ret = wps_generate_pin();
  615. goto done;
  616. } else if (hwaddr_aton(cmd, bssid)) {
  617. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  618. cmd);
  619. return -1;
  620. }
  621. #ifdef CONFIG_AP
  622. if (wpa_s->ap_iface) {
  623. int timeout = 0;
  624. char *pos;
  625. if (pin) {
  626. pos = os_strchr(pin, ' ');
  627. if (pos) {
  628. *pos++ = '\0';
  629. timeout = atoi(pos);
  630. }
  631. }
  632. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  633. buf, buflen, timeout);
  634. }
  635. #endif /* CONFIG_AP */
  636. if (pin) {
  637. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  638. DEV_PW_DEFAULT);
  639. if (ret < 0)
  640. return -1;
  641. ret = os_snprintf(buf, buflen, "%s", pin);
  642. if (ret < 0 || (size_t) ret >= buflen)
  643. return -1;
  644. return ret;
  645. }
  646. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  647. if (ret < 0)
  648. return -1;
  649. done:
  650. /* Return the generated PIN */
  651. ret = os_snprintf(buf, buflen, "%08d", ret);
  652. if (ret < 0 || (size_t) ret >= buflen)
  653. return -1;
  654. return ret;
  655. }
  656. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  657. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  658. {
  659. char pin[9];
  660. size_t len;
  661. char *pos;
  662. int ret;
  663. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  664. (u8 *) cmd, os_strlen(cmd));
  665. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  666. if (*pos < '0' || *pos > '9')
  667. continue;
  668. pin[len++] = *pos;
  669. if (len == 9) {
  670. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  671. return -1;
  672. }
  673. }
  674. if (len != 4 && len != 8) {
  675. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  676. return -1;
  677. }
  678. pin[len] = '\0';
  679. if (len == 8) {
  680. unsigned int pin_val;
  681. pin_val = atoi(pin);
  682. if (!wps_pin_valid(pin_val)) {
  683. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  684. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  685. if (ret < 0 || (size_t) ret >= buflen)
  686. return -1;
  687. return ret;
  688. }
  689. }
  690. ret = os_snprintf(buf, buflen, "%s", pin);
  691. if (ret < 0 || (size_t) ret >= buflen)
  692. return -1;
  693. return ret;
  694. }
  695. #ifdef CONFIG_WPS_NFC
  696. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  697. char *cmd)
  698. {
  699. u8 bssid[ETH_ALEN], *_bssid = bssid;
  700. if (cmd == NULL || cmd[0] == '\0')
  701. _bssid = NULL;
  702. else if (hwaddr_aton(cmd, bssid))
  703. return -1;
  704. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  705. 0, 0);
  706. }
  707. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  708. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  709. {
  710. int ndef;
  711. struct wpabuf *buf;
  712. int res;
  713. char *pos;
  714. pos = os_strchr(cmd, ' ');
  715. if (pos)
  716. *pos++ = '\0';
  717. if (os_strcmp(cmd, "WPS") == 0)
  718. ndef = 0;
  719. else if (os_strcmp(cmd, "NDEF") == 0)
  720. ndef = 1;
  721. else
  722. return -1;
  723. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  724. if (buf == NULL)
  725. return -1;
  726. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  727. wpabuf_len(buf));
  728. reply[res++] = '\n';
  729. reply[res] = '\0';
  730. wpabuf_free(buf);
  731. return res;
  732. }
  733. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  734. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  735. {
  736. int ndef;
  737. struct wpabuf *buf;
  738. int res;
  739. if (os_strcmp(cmd, "WPS") == 0)
  740. ndef = 0;
  741. else if (os_strcmp(cmd, "NDEF") == 0)
  742. ndef = 1;
  743. else
  744. return -1;
  745. buf = wpas_wps_nfc_token(wpa_s, ndef);
  746. if (buf == NULL)
  747. return -1;
  748. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  749. wpabuf_len(buf));
  750. reply[res++] = '\n';
  751. reply[res] = '\0';
  752. wpabuf_free(buf);
  753. return res;
  754. }
  755. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  756. struct wpa_supplicant *wpa_s, char *pos)
  757. {
  758. size_t len;
  759. struct wpabuf *buf;
  760. int ret;
  761. char *freq;
  762. int forced_freq = 0;
  763. freq = strstr(pos, " freq=");
  764. if (freq) {
  765. *freq = '\0';
  766. freq += 6;
  767. forced_freq = atoi(freq);
  768. }
  769. len = os_strlen(pos);
  770. if (len & 0x01)
  771. return -1;
  772. len /= 2;
  773. buf = wpabuf_alloc(len);
  774. if (buf == NULL)
  775. return -1;
  776. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  777. wpabuf_free(buf);
  778. return -1;
  779. }
  780. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  781. wpabuf_free(buf);
  782. return ret;
  783. }
  784. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  785. char *reply, size_t max_len,
  786. int ndef)
  787. {
  788. struct wpabuf *buf;
  789. int res;
  790. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  791. if (buf == NULL)
  792. return -1;
  793. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  794. wpabuf_len(buf));
  795. reply[res++] = '\n';
  796. reply[res] = '\0';
  797. wpabuf_free(buf);
  798. return res;
  799. }
  800. #ifdef CONFIG_P2P
  801. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  802. char *reply, size_t max_len,
  803. int ndef)
  804. {
  805. struct wpabuf *buf;
  806. int res;
  807. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  808. if (buf == NULL) {
  809. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  810. return -1;
  811. }
  812. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  813. wpabuf_len(buf));
  814. reply[res++] = '\n';
  815. reply[res] = '\0';
  816. wpabuf_free(buf);
  817. return res;
  818. }
  819. #endif /* CONFIG_P2P */
  820. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  821. char *cmd, char *reply,
  822. size_t max_len)
  823. {
  824. char *pos;
  825. int ndef;
  826. pos = os_strchr(cmd, ' ');
  827. if (pos == NULL)
  828. return -1;
  829. *pos++ = '\0';
  830. if (os_strcmp(cmd, "WPS") == 0)
  831. ndef = 0;
  832. else if (os_strcmp(cmd, "NDEF") == 0)
  833. ndef = 1;
  834. else
  835. return -1;
  836. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  837. if (!ndef)
  838. return -1;
  839. return wpas_ctrl_nfc_get_handover_req_wps(
  840. wpa_s, reply, max_len, ndef);
  841. }
  842. #ifdef CONFIG_P2P
  843. if (os_strcmp(pos, "P2P-CR") == 0) {
  844. return wpas_ctrl_nfc_get_handover_req_p2p(
  845. wpa_s, reply, max_len, ndef);
  846. }
  847. #endif /* CONFIG_P2P */
  848. return -1;
  849. }
  850. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  851. char *reply, size_t max_len,
  852. int ndef, int cr, char *uuid)
  853. {
  854. struct wpabuf *buf;
  855. int res;
  856. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  857. if (buf == NULL)
  858. return -1;
  859. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  860. wpabuf_len(buf));
  861. reply[res++] = '\n';
  862. reply[res] = '\0';
  863. wpabuf_free(buf);
  864. return res;
  865. }
  866. #ifdef CONFIG_P2P
  867. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  868. char *reply, size_t max_len,
  869. int ndef, int tag)
  870. {
  871. struct wpabuf *buf;
  872. int res;
  873. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  874. if (buf == NULL)
  875. return -1;
  876. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  877. wpabuf_len(buf));
  878. reply[res++] = '\n';
  879. reply[res] = '\0';
  880. wpabuf_free(buf);
  881. return res;
  882. }
  883. #endif /* CONFIG_P2P */
  884. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  885. char *cmd, char *reply,
  886. size_t max_len)
  887. {
  888. char *pos, *pos2;
  889. int ndef;
  890. pos = os_strchr(cmd, ' ');
  891. if (pos == NULL)
  892. return -1;
  893. *pos++ = '\0';
  894. if (os_strcmp(cmd, "WPS") == 0)
  895. ndef = 0;
  896. else if (os_strcmp(cmd, "NDEF") == 0)
  897. ndef = 1;
  898. else
  899. return -1;
  900. pos2 = os_strchr(pos, ' ');
  901. if (pos2)
  902. *pos2++ = '\0';
  903. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  904. if (!ndef)
  905. return -1;
  906. return wpas_ctrl_nfc_get_handover_sel_wps(
  907. wpa_s, reply, max_len, ndef,
  908. os_strcmp(pos, "WPS-CR") == 0, pos2);
  909. }
  910. #ifdef CONFIG_P2P
  911. if (os_strcmp(pos, "P2P-CR") == 0) {
  912. return wpas_ctrl_nfc_get_handover_sel_p2p(
  913. wpa_s, reply, max_len, ndef, 0);
  914. }
  915. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  916. return wpas_ctrl_nfc_get_handover_sel_p2p(
  917. wpa_s, reply, max_len, ndef, 1);
  918. }
  919. #endif /* CONFIG_P2P */
  920. return -1;
  921. }
  922. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  923. char *cmd)
  924. {
  925. size_t len;
  926. struct wpabuf *req, *sel;
  927. int ret;
  928. char *pos, *role, *type, *pos2;
  929. #ifdef CONFIG_P2P
  930. char *freq;
  931. int forced_freq = 0;
  932. freq = strstr(cmd, " freq=");
  933. if (freq) {
  934. *freq = '\0';
  935. freq += 6;
  936. forced_freq = atoi(freq);
  937. }
  938. #endif /* CONFIG_P2P */
  939. role = cmd;
  940. pos = os_strchr(role, ' ');
  941. if (pos == NULL) {
  942. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  943. return -1;
  944. }
  945. *pos++ = '\0';
  946. type = pos;
  947. pos = os_strchr(type, ' ');
  948. if (pos == NULL) {
  949. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  950. return -1;
  951. }
  952. *pos++ = '\0';
  953. pos2 = os_strchr(pos, ' ');
  954. if (pos2 == NULL) {
  955. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  956. return -1;
  957. }
  958. *pos2++ = '\0';
  959. len = os_strlen(pos);
  960. if (len & 0x01) {
  961. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  962. return -1;
  963. }
  964. len /= 2;
  965. req = wpabuf_alloc(len);
  966. if (req == NULL) {
  967. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  968. return -1;
  969. }
  970. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  971. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  972. wpabuf_free(req);
  973. return -1;
  974. }
  975. len = os_strlen(pos2);
  976. if (len & 0x01) {
  977. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  978. wpabuf_free(req);
  979. return -1;
  980. }
  981. len /= 2;
  982. sel = wpabuf_alloc(len);
  983. if (sel == NULL) {
  984. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  985. wpabuf_free(req);
  986. return -1;
  987. }
  988. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  989. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  990. wpabuf_free(req);
  991. wpabuf_free(sel);
  992. return -1;
  993. }
  994. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  995. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  996. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  997. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  998. #ifdef CONFIG_AP
  999. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1000. {
  1001. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1002. if (ret < 0)
  1003. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1004. #endif /* CONFIG_AP */
  1005. #ifdef CONFIG_P2P
  1006. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1007. {
  1008. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1009. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1010. {
  1011. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1012. forced_freq);
  1013. #endif /* CONFIG_P2P */
  1014. } else {
  1015. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1016. "reported: role=%s type=%s", role, type);
  1017. ret = -1;
  1018. }
  1019. wpabuf_free(req);
  1020. wpabuf_free(sel);
  1021. if (ret)
  1022. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1023. return ret;
  1024. }
  1025. #endif /* CONFIG_WPS_NFC */
  1026. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1027. char *cmd)
  1028. {
  1029. u8 bssid[ETH_ALEN];
  1030. char *pin;
  1031. char *new_ssid;
  1032. char *new_auth;
  1033. char *new_encr;
  1034. char *new_key;
  1035. struct wps_new_ap_settings ap;
  1036. pin = os_strchr(cmd, ' ');
  1037. if (pin == NULL)
  1038. return -1;
  1039. *pin++ = '\0';
  1040. if (hwaddr_aton(cmd, bssid)) {
  1041. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1042. cmd);
  1043. return -1;
  1044. }
  1045. new_ssid = os_strchr(pin, ' ');
  1046. if (new_ssid == NULL)
  1047. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1048. *new_ssid++ = '\0';
  1049. new_auth = os_strchr(new_ssid, ' ');
  1050. if (new_auth == NULL)
  1051. return -1;
  1052. *new_auth++ = '\0';
  1053. new_encr = os_strchr(new_auth, ' ');
  1054. if (new_encr == NULL)
  1055. return -1;
  1056. *new_encr++ = '\0';
  1057. new_key = os_strchr(new_encr, ' ');
  1058. if (new_key == NULL)
  1059. return -1;
  1060. *new_key++ = '\0';
  1061. os_memset(&ap, 0, sizeof(ap));
  1062. ap.ssid_hex = new_ssid;
  1063. ap.auth = new_auth;
  1064. ap.encr = new_encr;
  1065. ap.key_hex = new_key;
  1066. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1067. }
  1068. #ifdef CONFIG_AP
  1069. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1070. char *cmd, char *buf,
  1071. size_t buflen)
  1072. {
  1073. int timeout = 300;
  1074. char *pos;
  1075. const char *pin_txt;
  1076. if (!wpa_s->ap_iface)
  1077. return -1;
  1078. pos = os_strchr(cmd, ' ');
  1079. if (pos)
  1080. *pos++ = '\0';
  1081. if (os_strcmp(cmd, "disable") == 0) {
  1082. wpas_wps_ap_pin_disable(wpa_s);
  1083. return os_snprintf(buf, buflen, "OK\n");
  1084. }
  1085. if (os_strcmp(cmd, "random") == 0) {
  1086. if (pos)
  1087. timeout = atoi(pos);
  1088. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1089. if (pin_txt == NULL)
  1090. return -1;
  1091. return os_snprintf(buf, buflen, "%s", pin_txt);
  1092. }
  1093. if (os_strcmp(cmd, "get") == 0) {
  1094. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1095. if (pin_txt == NULL)
  1096. return -1;
  1097. return os_snprintf(buf, buflen, "%s", pin_txt);
  1098. }
  1099. if (os_strcmp(cmd, "set") == 0) {
  1100. char *pin;
  1101. if (pos == NULL)
  1102. return -1;
  1103. pin = pos;
  1104. pos = os_strchr(pos, ' ');
  1105. if (pos) {
  1106. *pos++ = '\0';
  1107. timeout = atoi(pos);
  1108. }
  1109. if (os_strlen(pin) > buflen)
  1110. return -1;
  1111. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1112. return -1;
  1113. return os_snprintf(buf, buflen, "%s", pin);
  1114. }
  1115. return -1;
  1116. }
  1117. #endif /* CONFIG_AP */
  1118. #ifdef CONFIG_WPS_ER
  1119. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1120. char *cmd)
  1121. {
  1122. char *uuid = cmd, *pin, *pos;
  1123. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1124. pin = os_strchr(uuid, ' ');
  1125. if (pin == NULL)
  1126. return -1;
  1127. *pin++ = '\0';
  1128. pos = os_strchr(pin, ' ');
  1129. if (pos) {
  1130. *pos++ = '\0';
  1131. if (hwaddr_aton(pos, addr_buf) == 0)
  1132. addr = addr_buf;
  1133. }
  1134. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1135. }
  1136. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1137. char *cmd)
  1138. {
  1139. char *uuid = cmd, *pin;
  1140. pin = os_strchr(uuid, ' ');
  1141. if (pin == NULL)
  1142. return -1;
  1143. *pin++ = '\0';
  1144. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1145. }
  1146. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1147. struct wpa_supplicant *wpa_s, char *cmd)
  1148. {
  1149. char *uuid = cmd, *id;
  1150. id = os_strchr(uuid, ' ');
  1151. if (id == NULL)
  1152. return -1;
  1153. *id++ = '\0';
  1154. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1155. }
  1156. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1157. struct wpa_supplicant *wpa_s, char *cmd)
  1158. {
  1159. char *pin;
  1160. char *new_ssid;
  1161. char *new_auth;
  1162. char *new_encr;
  1163. char *new_key;
  1164. struct wps_new_ap_settings ap;
  1165. pin = os_strchr(cmd, ' ');
  1166. if (pin == NULL)
  1167. return -1;
  1168. *pin++ = '\0';
  1169. new_ssid = os_strchr(pin, ' ');
  1170. if (new_ssid == NULL)
  1171. return -1;
  1172. *new_ssid++ = '\0';
  1173. new_auth = os_strchr(new_ssid, ' ');
  1174. if (new_auth == NULL)
  1175. return -1;
  1176. *new_auth++ = '\0';
  1177. new_encr = os_strchr(new_auth, ' ');
  1178. if (new_encr == NULL)
  1179. return -1;
  1180. *new_encr++ = '\0';
  1181. new_key = os_strchr(new_encr, ' ');
  1182. if (new_key == NULL)
  1183. return -1;
  1184. *new_key++ = '\0';
  1185. os_memset(&ap, 0, sizeof(ap));
  1186. ap.ssid_hex = new_ssid;
  1187. ap.auth = new_auth;
  1188. ap.encr = new_encr;
  1189. ap.key_hex = new_key;
  1190. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1191. }
  1192. #ifdef CONFIG_WPS_NFC
  1193. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1194. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1195. {
  1196. int ndef;
  1197. struct wpabuf *buf;
  1198. int res;
  1199. char *uuid;
  1200. uuid = os_strchr(cmd, ' ');
  1201. if (uuid == NULL)
  1202. return -1;
  1203. *uuid++ = '\0';
  1204. if (os_strcmp(cmd, "WPS") == 0)
  1205. ndef = 0;
  1206. else if (os_strcmp(cmd, "NDEF") == 0)
  1207. ndef = 1;
  1208. else
  1209. return -1;
  1210. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1211. if (buf == NULL)
  1212. return -1;
  1213. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1214. wpabuf_len(buf));
  1215. reply[res++] = '\n';
  1216. reply[res] = '\0';
  1217. wpabuf_free(buf);
  1218. return res;
  1219. }
  1220. #endif /* CONFIG_WPS_NFC */
  1221. #endif /* CONFIG_WPS_ER */
  1222. #endif /* CONFIG_WPS */
  1223. #ifdef CONFIG_IBSS_RSN
  1224. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1225. struct wpa_supplicant *wpa_s, char *addr)
  1226. {
  1227. u8 peer[ETH_ALEN];
  1228. if (hwaddr_aton(addr, peer)) {
  1229. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1230. "address '%s'", addr);
  1231. return -1;
  1232. }
  1233. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1234. MAC2STR(peer));
  1235. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1236. }
  1237. #endif /* CONFIG_IBSS_RSN */
  1238. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1239. char *rsp)
  1240. {
  1241. #ifdef IEEE8021X_EAPOL
  1242. char *pos, *id_pos;
  1243. int id;
  1244. struct wpa_ssid *ssid;
  1245. pos = os_strchr(rsp, '-');
  1246. if (pos == NULL)
  1247. return -1;
  1248. *pos++ = '\0';
  1249. id_pos = pos;
  1250. pos = os_strchr(pos, ':');
  1251. if (pos == NULL)
  1252. return -1;
  1253. *pos++ = '\0';
  1254. id = atoi(id_pos);
  1255. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1256. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1257. (u8 *) pos, os_strlen(pos));
  1258. ssid = wpa_config_get_network(wpa_s->conf, id);
  1259. if (ssid == NULL) {
  1260. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1261. "to update", id);
  1262. return -1;
  1263. }
  1264. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1265. pos);
  1266. #else /* IEEE8021X_EAPOL */
  1267. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1268. return -1;
  1269. #endif /* IEEE8021X_EAPOL */
  1270. }
  1271. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1272. const char *params,
  1273. char *buf, size_t buflen)
  1274. {
  1275. char *pos, *end, tmp[30];
  1276. int res, verbose, wps, ret;
  1277. #ifdef CONFIG_HS20
  1278. const u8 *hs20;
  1279. #endif /* CONFIG_HS20 */
  1280. if (os_strcmp(params, "-DRIVER") == 0)
  1281. return wpa_drv_status(wpa_s, buf, buflen);
  1282. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1283. wps = os_strcmp(params, "-WPS") == 0;
  1284. pos = buf;
  1285. end = buf + buflen;
  1286. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1287. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1288. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1289. MAC2STR(wpa_s->bssid));
  1290. if (ret < 0 || ret >= end - pos)
  1291. return pos - buf;
  1292. pos += ret;
  1293. if (ssid) {
  1294. u8 *_ssid = ssid->ssid;
  1295. size_t ssid_len = ssid->ssid_len;
  1296. u8 ssid_buf[MAX_SSID_LEN];
  1297. if (ssid_len == 0) {
  1298. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1299. if (_res < 0)
  1300. ssid_len = 0;
  1301. else
  1302. ssid_len = _res;
  1303. _ssid = ssid_buf;
  1304. }
  1305. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1306. wpa_ssid_txt(_ssid, ssid_len),
  1307. ssid->id);
  1308. if (ret < 0 || ret >= end - pos)
  1309. return pos - buf;
  1310. pos += ret;
  1311. if (wps && ssid->passphrase &&
  1312. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1313. (ssid->mode == WPAS_MODE_AP ||
  1314. ssid->mode == WPAS_MODE_P2P_GO)) {
  1315. ret = os_snprintf(pos, end - pos,
  1316. "passphrase=%s\n",
  1317. ssid->passphrase);
  1318. if (ret < 0 || ret >= end - pos)
  1319. return pos - buf;
  1320. pos += ret;
  1321. }
  1322. if (ssid->id_str) {
  1323. ret = os_snprintf(pos, end - pos,
  1324. "id_str=%s\n",
  1325. ssid->id_str);
  1326. if (ret < 0 || ret >= end - pos)
  1327. return pos - buf;
  1328. pos += ret;
  1329. }
  1330. switch (ssid->mode) {
  1331. case WPAS_MODE_INFRA:
  1332. ret = os_snprintf(pos, end - pos,
  1333. "mode=station\n");
  1334. break;
  1335. case WPAS_MODE_IBSS:
  1336. ret = os_snprintf(pos, end - pos,
  1337. "mode=IBSS\n");
  1338. break;
  1339. case WPAS_MODE_AP:
  1340. ret = os_snprintf(pos, end - pos,
  1341. "mode=AP\n");
  1342. break;
  1343. case WPAS_MODE_P2P_GO:
  1344. ret = os_snprintf(pos, end - pos,
  1345. "mode=P2P GO\n");
  1346. break;
  1347. case WPAS_MODE_P2P_GROUP_FORMATION:
  1348. ret = os_snprintf(pos, end - pos,
  1349. "mode=P2P GO - group "
  1350. "formation\n");
  1351. break;
  1352. default:
  1353. ret = 0;
  1354. break;
  1355. }
  1356. if (ret < 0 || ret >= end - pos)
  1357. return pos - buf;
  1358. pos += ret;
  1359. }
  1360. #ifdef CONFIG_AP
  1361. if (wpa_s->ap_iface) {
  1362. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1363. end - pos,
  1364. verbose);
  1365. } else
  1366. #endif /* CONFIG_AP */
  1367. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1368. }
  1369. #ifdef CONFIG_SAE
  1370. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1371. #ifdef CONFIG_AP
  1372. !wpa_s->ap_iface &&
  1373. #endif /* CONFIG_AP */
  1374. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1375. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1376. wpa_s->sme.sae.group);
  1377. if (ret < 0 || ret >= end - pos)
  1378. return pos - buf;
  1379. pos += ret;
  1380. }
  1381. #endif /* CONFIG_SAE */
  1382. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1383. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1384. if (ret < 0 || ret >= end - pos)
  1385. return pos - buf;
  1386. pos += ret;
  1387. if (wpa_s->l2 &&
  1388. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1389. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1390. if (ret < 0 || ret >= end - pos)
  1391. return pos - buf;
  1392. pos += ret;
  1393. }
  1394. #ifdef CONFIG_P2P
  1395. if (wpa_s->global->p2p) {
  1396. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1397. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1398. if (ret < 0 || ret >= end - pos)
  1399. return pos - buf;
  1400. pos += ret;
  1401. }
  1402. #endif /* CONFIG_P2P */
  1403. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1404. MAC2STR(wpa_s->own_addr));
  1405. if (ret < 0 || ret >= end - pos)
  1406. return pos - buf;
  1407. pos += ret;
  1408. #ifdef CONFIG_HS20
  1409. if (wpa_s->current_bss &&
  1410. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1411. HS20_IE_VENDOR_TYPE)) &&
  1412. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1413. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1414. int release = 1;
  1415. if (hs20[1] >= 5) {
  1416. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1417. release = rel_num + 1;
  1418. }
  1419. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1420. if (ret < 0 || ret >= end - pos)
  1421. return pos - buf;
  1422. pos += ret;
  1423. }
  1424. if (wpa_s->current_ssid) {
  1425. struct wpa_cred *cred;
  1426. char *type;
  1427. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1428. size_t i;
  1429. if (wpa_s->current_ssid->parent_cred != cred)
  1430. continue;
  1431. if (cred->provisioning_sp) {
  1432. ret = os_snprintf(pos, end - pos,
  1433. "provisioning_sp=%s\n",
  1434. cred->provisioning_sp);
  1435. if (ret < 0 || ret >= end - pos)
  1436. return pos - buf;
  1437. pos += ret;
  1438. }
  1439. if (!cred->domain)
  1440. goto no_domain;
  1441. i = 0;
  1442. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1443. struct wpabuf *names =
  1444. wpa_s->current_bss->anqp->domain_name;
  1445. for (i = 0; names && i < cred->num_domain; i++)
  1446. {
  1447. if (domain_name_list_contains(
  1448. names, cred->domain[i], 1))
  1449. break;
  1450. }
  1451. if (i == cred->num_domain)
  1452. i = 0; /* show first entry by default */
  1453. }
  1454. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1455. cred->domain[i]);
  1456. if (ret < 0 || ret >= end - pos)
  1457. return pos - buf;
  1458. pos += ret;
  1459. no_domain:
  1460. if (wpa_s->current_bss == NULL ||
  1461. wpa_s->current_bss->anqp == NULL)
  1462. res = -1;
  1463. else
  1464. res = interworking_home_sp_cred(
  1465. wpa_s, cred,
  1466. wpa_s->current_bss->anqp->domain_name);
  1467. if (res > 0)
  1468. type = "home";
  1469. else if (res == 0)
  1470. type = "roaming";
  1471. else
  1472. type = "unknown";
  1473. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1474. if (ret < 0 || ret >= end - pos)
  1475. return pos - buf;
  1476. pos += ret;
  1477. break;
  1478. }
  1479. }
  1480. #endif /* CONFIG_HS20 */
  1481. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1482. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1483. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1484. verbose);
  1485. if (res >= 0)
  1486. pos += res;
  1487. }
  1488. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1489. if (res >= 0)
  1490. pos += res;
  1491. #ifdef CONFIG_WPS
  1492. {
  1493. char uuid_str[100];
  1494. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1495. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1496. if (ret < 0 || ret >= end - pos)
  1497. return pos - buf;
  1498. pos += ret;
  1499. }
  1500. #endif /* CONFIG_WPS */
  1501. #ifdef ANDROID
  1502. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1503. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1504. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1505. wpa_s->wpa_state,
  1506. MAC2STR(wpa_s->bssid),
  1507. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1508. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1509. wpa_s->current_ssid->ssid_len) : "");
  1510. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1511. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1512. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1513. "- connection to " MACSTR
  1514. " completed %s [id=%d id_str=%s]",
  1515. MAC2STR(wpa_s->bssid), "(auth)",
  1516. ssid ? ssid->id : -1,
  1517. ssid && ssid->id_str ? ssid->id_str : "");
  1518. }
  1519. #endif /* ANDROID */
  1520. return pos - buf;
  1521. }
  1522. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1523. char *cmd)
  1524. {
  1525. char *pos;
  1526. int id;
  1527. struct wpa_ssid *ssid;
  1528. u8 bssid[ETH_ALEN];
  1529. /* cmd: "<network id> <BSSID>" */
  1530. pos = os_strchr(cmd, ' ');
  1531. if (pos == NULL)
  1532. return -1;
  1533. *pos++ = '\0';
  1534. id = atoi(cmd);
  1535. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1536. if (hwaddr_aton(pos, bssid)) {
  1537. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1538. return -1;
  1539. }
  1540. ssid = wpa_config_get_network(wpa_s->conf, id);
  1541. if (ssid == NULL) {
  1542. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1543. "to update", id);
  1544. return -1;
  1545. }
  1546. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1547. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1548. return 0;
  1549. }
  1550. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1551. char *cmd, char *buf,
  1552. size_t buflen)
  1553. {
  1554. u8 bssid[ETH_ALEN];
  1555. struct wpa_blacklist *e;
  1556. char *pos, *end;
  1557. int ret;
  1558. /* cmd: "BLACKLIST [<BSSID>]" */
  1559. if (*cmd == '\0') {
  1560. pos = buf;
  1561. end = buf + buflen;
  1562. e = wpa_s->blacklist;
  1563. while (e) {
  1564. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1565. MAC2STR(e->bssid));
  1566. if (ret < 0 || ret >= end - pos)
  1567. return pos - buf;
  1568. pos += ret;
  1569. e = e->next;
  1570. }
  1571. return pos - buf;
  1572. }
  1573. cmd++;
  1574. if (os_strncmp(cmd, "clear", 5) == 0) {
  1575. wpa_blacklist_clear(wpa_s);
  1576. os_memcpy(buf, "OK\n", 3);
  1577. return 3;
  1578. }
  1579. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1580. if (hwaddr_aton(cmd, bssid)) {
  1581. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1582. return -1;
  1583. }
  1584. /*
  1585. * Add the BSSID twice, so its count will be 2, causing it to be
  1586. * skipped when processing scan results.
  1587. */
  1588. ret = wpa_blacklist_add(wpa_s, bssid);
  1589. if (ret < 0)
  1590. return -1;
  1591. ret = wpa_blacklist_add(wpa_s, bssid);
  1592. if (ret < 0)
  1593. return -1;
  1594. os_memcpy(buf, "OK\n", 3);
  1595. return 3;
  1596. }
  1597. static const char * debug_level_str(int level)
  1598. {
  1599. switch (level) {
  1600. case MSG_EXCESSIVE:
  1601. return "EXCESSIVE";
  1602. case MSG_MSGDUMP:
  1603. return "MSGDUMP";
  1604. case MSG_DEBUG:
  1605. return "DEBUG";
  1606. case MSG_INFO:
  1607. return "INFO";
  1608. case MSG_WARNING:
  1609. return "WARNING";
  1610. case MSG_ERROR:
  1611. return "ERROR";
  1612. default:
  1613. return "?";
  1614. }
  1615. }
  1616. static int str_to_debug_level(const char *s)
  1617. {
  1618. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1619. return MSG_EXCESSIVE;
  1620. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1621. return MSG_MSGDUMP;
  1622. if (os_strcasecmp(s, "DEBUG") == 0)
  1623. return MSG_DEBUG;
  1624. if (os_strcasecmp(s, "INFO") == 0)
  1625. return MSG_INFO;
  1626. if (os_strcasecmp(s, "WARNING") == 0)
  1627. return MSG_WARNING;
  1628. if (os_strcasecmp(s, "ERROR") == 0)
  1629. return MSG_ERROR;
  1630. return -1;
  1631. }
  1632. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1633. char *cmd, char *buf,
  1634. size_t buflen)
  1635. {
  1636. char *pos, *end, *stamp;
  1637. int ret;
  1638. if (cmd == NULL) {
  1639. return -1;
  1640. }
  1641. /* cmd: "LOG_LEVEL [<level>]" */
  1642. if (*cmd == '\0') {
  1643. pos = buf;
  1644. end = buf + buflen;
  1645. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1646. "Timestamp: %d\n",
  1647. debug_level_str(wpa_debug_level),
  1648. wpa_debug_timestamp);
  1649. if (ret < 0 || ret >= end - pos)
  1650. ret = 0;
  1651. return ret;
  1652. }
  1653. while (*cmd == ' ')
  1654. cmd++;
  1655. stamp = os_strchr(cmd, ' ');
  1656. if (stamp) {
  1657. *stamp++ = '\0';
  1658. while (*stamp == ' ') {
  1659. stamp++;
  1660. }
  1661. }
  1662. if (cmd && os_strlen(cmd)) {
  1663. int level = str_to_debug_level(cmd);
  1664. if (level < 0)
  1665. return -1;
  1666. wpa_debug_level = level;
  1667. }
  1668. if (stamp && os_strlen(stamp))
  1669. wpa_debug_timestamp = atoi(stamp);
  1670. os_memcpy(buf, "OK\n", 3);
  1671. return 3;
  1672. }
  1673. static int wpa_supplicant_ctrl_iface_list_networks(
  1674. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1675. {
  1676. char *pos, *end;
  1677. struct wpa_ssid *ssid;
  1678. int ret;
  1679. pos = buf;
  1680. end = buf + buflen;
  1681. ret = os_snprintf(pos, end - pos,
  1682. "network id / ssid / bssid / flags\n");
  1683. if (ret < 0 || ret >= end - pos)
  1684. return pos - buf;
  1685. pos += ret;
  1686. ssid = wpa_s->conf->ssid;
  1687. while (ssid) {
  1688. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1689. ssid->id,
  1690. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1691. if (ret < 0 || ret >= end - pos)
  1692. return pos - buf;
  1693. pos += ret;
  1694. if (ssid->bssid_set) {
  1695. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1696. MAC2STR(ssid->bssid));
  1697. } else {
  1698. ret = os_snprintf(pos, end - pos, "\tany");
  1699. }
  1700. if (ret < 0 || ret >= end - pos)
  1701. return pos - buf;
  1702. pos += ret;
  1703. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1704. ssid == wpa_s->current_ssid ?
  1705. "[CURRENT]" : "",
  1706. ssid->disabled ? "[DISABLED]" : "",
  1707. ssid->disabled_until.sec ?
  1708. "[TEMP-DISABLED]" : "",
  1709. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1710. "");
  1711. if (ret < 0 || ret >= end - pos)
  1712. return pos - buf;
  1713. pos += ret;
  1714. ret = os_snprintf(pos, end - pos, "\n");
  1715. if (ret < 0 || ret >= end - pos)
  1716. return pos - buf;
  1717. pos += ret;
  1718. ssid = ssid->next;
  1719. }
  1720. return pos - buf;
  1721. }
  1722. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1723. {
  1724. int ret;
  1725. ret = os_snprintf(pos, end - pos, "-");
  1726. if (ret < 0 || ret >= end - pos)
  1727. return pos;
  1728. pos += ret;
  1729. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1730. if (ret < 0)
  1731. return pos;
  1732. pos += ret;
  1733. return pos;
  1734. }
  1735. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1736. const u8 *ie, size_t ie_len)
  1737. {
  1738. struct wpa_ie_data data;
  1739. char *start;
  1740. int ret;
  1741. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1742. if (ret < 0 || ret >= end - pos)
  1743. return pos;
  1744. pos += ret;
  1745. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1746. ret = os_snprintf(pos, end - pos, "?]");
  1747. if (ret < 0 || ret >= end - pos)
  1748. return pos;
  1749. pos += ret;
  1750. return pos;
  1751. }
  1752. start = pos;
  1753. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1754. ret = os_snprintf(pos, end - pos, "%sEAP",
  1755. pos == start ? "" : "+");
  1756. if (ret < 0 || ret >= end - pos)
  1757. return pos;
  1758. pos += ret;
  1759. }
  1760. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1761. ret = os_snprintf(pos, end - pos, "%sPSK",
  1762. pos == start ? "" : "+");
  1763. if (ret < 0 || ret >= end - pos)
  1764. return pos;
  1765. pos += ret;
  1766. }
  1767. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1768. ret = os_snprintf(pos, end - pos, "%sNone",
  1769. pos == start ? "" : "+");
  1770. if (ret < 0 || ret >= end - pos)
  1771. return pos;
  1772. pos += ret;
  1773. }
  1774. #ifdef CONFIG_IEEE80211R
  1775. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1776. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1777. pos == start ? "" : "+");
  1778. if (ret < 0 || ret >= end - pos)
  1779. return pos;
  1780. pos += ret;
  1781. }
  1782. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1783. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1784. pos == start ? "" : "+");
  1785. if (ret < 0 || ret >= end - pos)
  1786. return pos;
  1787. pos += ret;
  1788. }
  1789. #endif /* CONFIG_IEEE80211R */
  1790. #ifdef CONFIG_IEEE80211W
  1791. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1792. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1793. pos == start ? "" : "+");
  1794. if (ret < 0 || ret >= end - pos)
  1795. return pos;
  1796. pos += ret;
  1797. }
  1798. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1799. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1800. pos == start ? "" : "+");
  1801. if (ret < 0 || ret >= end - pos)
  1802. return pos;
  1803. pos += ret;
  1804. }
  1805. #endif /* CONFIG_IEEE80211W */
  1806. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1807. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1808. ret = os_snprintf(pos, end - pos, "-preauth");
  1809. if (ret < 0 || ret >= end - pos)
  1810. return pos;
  1811. pos += ret;
  1812. }
  1813. ret = os_snprintf(pos, end - pos, "]");
  1814. if (ret < 0 || ret >= end - pos)
  1815. return pos;
  1816. pos += ret;
  1817. return pos;
  1818. }
  1819. #ifdef CONFIG_WPS
  1820. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1821. char *pos, char *end,
  1822. struct wpabuf *wps_ie)
  1823. {
  1824. int ret;
  1825. const char *txt;
  1826. if (wps_ie == NULL)
  1827. return pos;
  1828. if (wps_is_selected_pbc_registrar(wps_ie))
  1829. txt = "[WPS-PBC]";
  1830. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1831. txt = "[WPS-AUTH]";
  1832. else if (wps_is_selected_pin_registrar(wps_ie))
  1833. txt = "[WPS-PIN]";
  1834. else
  1835. txt = "[WPS]";
  1836. ret = os_snprintf(pos, end - pos, "%s", txt);
  1837. if (ret >= 0 && ret < end - pos)
  1838. pos += ret;
  1839. wpabuf_free(wps_ie);
  1840. return pos;
  1841. }
  1842. #endif /* CONFIG_WPS */
  1843. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1844. char *pos, char *end,
  1845. const struct wpa_bss *bss)
  1846. {
  1847. #ifdef CONFIG_WPS
  1848. struct wpabuf *wps_ie;
  1849. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1850. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1851. #else /* CONFIG_WPS */
  1852. return pos;
  1853. #endif /* CONFIG_WPS */
  1854. }
  1855. /* Format one result on one text line into a buffer. */
  1856. static int wpa_supplicant_ctrl_iface_scan_result(
  1857. struct wpa_supplicant *wpa_s,
  1858. const struct wpa_bss *bss, char *buf, size_t buflen)
  1859. {
  1860. char *pos, *end;
  1861. int ret;
  1862. const u8 *ie, *ie2, *p2p;
  1863. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1864. if (!p2p)
  1865. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1866. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1867. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1868. 0)
  1869. return 0; /* Do not show P2P listen discovery results here */
  1870. pos = buf;
  1871. end = buf + buflen;
  1872. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1873. MAC2STR(bss->bssid), bss->freq, bss->level);
  1874. if (ret < 0 || ret >= end - pos)
  1875. return -1;
  1876. pos += ret;
  1877. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1878. if (ie)
  1879. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1880. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1881. if (ie2)
  1882. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1883. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1884. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1885. ret = os_snprintf(pos, end - pos, "[WEP]");
  1886. if (ret < 0 || ret >= end - pos)
  1887. return -1;
  1888. pos += ret;
  1889. }
  1890. if (bss_is_dmg(bss)) {
  1891. const char *s;
  1892. ret = os_snprintf(pos, end - pos, "[DMG]");
  1893. if (ret < 0 || ret >= end - pos)
  1894. return -1;
  1895. pos += ret;
  1896. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  1897. case IEEE80211_CAP_DMG_IBSS:
  1898. s = "[IBSS]";
  1899. break;
  1900. case IEEE80211_CAP_DMG_AP:
  1901. s = "[ESS]";
  1902. break;
  1903. case IEEE80211_CAP_DMG_PBSS:
  1904. s = "[PBSS]";
  1905. break;
  1906. default:
  1907. s = "";
  1908. break;
  1909. }
  1910. ret = os_snprintf(pos, end - pos, "%s", s);
  1911. if (ret < 0 || ret >= end - pos)
  1912. return -1;
  1913. pos += ret;
  1914. } else {
  1915. if (bss->caps & IEEE80211_CAP_IBSS) {
  1916. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1917. if (ret < 0 || ret >= end - pos)
  1918. return -1;
  1919. pos += ret;
  1920. }
  1921. if (bss->caps & IEEE80211_CAP_ESS) {
  1922. ret = os_snprintf(pos, end - pos, "[ESS]");
  1923. if (ret < 0 || ret >= end - pos)
  1924. return -1;
  1925. pos += ret;
  1926. }
  1927. }
  1928. if (p2p) {
  1929. ret = os_snprintf(pos, end - pos, "[P2P]");
  1930. if (ret < 0 || ret >= end - pos)
  1931. return -1;
  1932. pos += ret;
  1933. }
  1934. #ifdef CONFIG_HS20
  1935. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1936. ret = os_snprintf(pos, end - pos, "[HS20]");
  1937. if (ret < 0 || ret >= end - pos)
  1938. return -1;
  1939. pos += ret;
  1940. }
  1941. #endif /* CONFIG_HS20 */
  1942. ret = os_snprintf(pos, end - pos, "\t%s",
  1943. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1944. if (ret < 0 || ret >= end - pos)
  1945. return -1;
  1946. pos += ret;
  1947. ret = os_snprintf(pos, end - pos, "\n");
  1948. if (ret < 0 || ret >= end - pos)
  1949. return -1;
  1950. pos += ret;
  1951. return pos - buf;
  1952. }
  1953. static int wpa_supplicant_ctrl_iface_scan_results(
  1954. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1955. {
  1956. char *pos, *end;
  1957. struct wpa_bss *bss;
  1958. int ret;
  1959. pos = buf;
  1960. end = buf + buflen;
  1961. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1962. "flags / ssid\n");
  1963. if (ret < 0 || ret >= end - pos)
  1964. return pos - buf;
  1965. pos += ret;
  1966. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1967. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1968. end - pos);
  1969. if (ret < 0 || ret >= end - pos)
  1970. return pos - buf;
  1971. pos += ret;
  1972. }
  1973. return pos - buf;
  1974. }
  1975. static int wpa_supplicant_ctrl_iface_select_network(
  1976. struct wpa_supplicant *wpa_s, char *cmd)
  1977. {
  1978. int id;
  1979. struct wpa_ssid *ssid;
  1980. char *pos;
  1981. /* cmd: "<network id>" or "any" */
  1982. if (os_strncmp(cmd, "any", 3) == 0) {
  1983. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1984. ssid = NULL;
  1985. } else {
  1986. id = atoi(cmd);
  1987. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1988. ssid = wpa_config_get_network(wpa_s->conf, id);
  1989. if (ssid == NULL) {
  1990. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1991. "network id=%d", id);
  1992. return -1;
  1993. }
  1994. if (ssid->disabled == 2) {
  1995. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1996. "SELECT_NETWORK with persistent P2P group");
  1997. return -1;
  1998. }
  1999. }
  2000. pos = os_strstr(cmd, " freq=");
  2001. if (pos) {
  2002. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2003. if (freqs) {
  2004. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2005. os_free(wpa_s->manual_scan_freqs);
  2006. wpa_s->manual_scan_freqs = freqs;
  2007. }
  2008. }
  2009. wpa_supplicant_select_network(wpa_s, ssid);
  2010. return 0;
  2011. }
  2012. static int wpa_supplicant_ctrl_iface_enable_network(
  2013. struct wpa_supplicant *wpa_s, char *cmd)
  2014. {
  2015. int id;
  2016. struct wpa_ssid *ssid;
  2017. /* cmd: "<network id>" or "all" */
  2018. if (os_strcmp(cmd, "all") == 0) {
  2019. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2020. ssid = NULL;
  2021. } else {
  2022. id = atoi(cmd);
  2023. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2024. ssid = wpa_config_get_network(wpa_s->conf, id);
  2025. if (ssid == NULL) {
  2026. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2027. "network id=%d", id);
  2028. return -1;
  2029. }
  2030. if (ssid->disabled == 2) {
  2031. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2032. "ENABLE_NETWORK with persistent P2P group");
  2033. return -1;
  2034. }
  2035. if (os_strstr(cmd, " no-connect")) {
  2036. ssid->disabled = 0;
  2037. return 0;
  2038. }
  2039. }
  2040. wpa_supplicant_enable_network(wpa_s, ssid);
  2041. return 0;
  2042. }
  2043. static int wpa_supplicant_ctrl_iface_disable_network(
  2044. struct wpa_supplicant *wpa_s, char *cmd)
  2045. {
  2046. int id;
  2047. struct wpa_ssid *ssid;
  2048. /* cmd: "<network id>" or "all" */
  2049. if (os_strcmp(cmd, "all") == 0) {
  2050. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2051. ssid = NULL;
  2052. } else {
  2053. id = atoi(cmd);
  2054. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2055. ssid = wpa_config_get_network(wpa_s->conf, id);
  2056. if (ssid == NULL) {
  2057. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2058. "network id=%d", id);
  2059. return -1;
  2060. }
  2061. if (ssid->disabled == 2) {
  2062. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2063. "DISABLE_NETWORK with persistent P2P "
  2064. "group");
  2065. return -1;
  2066. }
  2067. }
  2068. wpa_supplicant_disable_network(wpa_s, ssid);
  2069. return 0;
  2070. }
  2071. static int wpa_supplicant_ctrl_iface_add_network(
  2072. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2073. {
  2074. struct wpa_ssid *ssid;
  2075. int ret;
  2076. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2077. ssid = wpa_config_add_network(wpa_s->conf);
  2078. if (ssid == NULL)
  2079. return -1;
  2080. wpas_notify_network_added(wpa_s, ssid);
  2081. ssid->disabled = 1;
  2082. wpa_config_set_network_defaults(ssid);
  2083. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2084. if (ret < 0 || (size_t) ret >= buflen)
  2085. return -1;
  2086. return ret;
  2087. }
  2088. static int wpa_supplicant_ctrl_iface_remove_network(
  2089. struct wpa_supplicant *wpa_s, char *cmd)
  2090. {
  2091. int id;
  2092. struct wpa_ssid *ssid;
  2093. int was_disabled;
  2094. /* cmd: "<network id>" or "all" */
  2095. if (os_strcmp(cmd, "all") == 0) {
  2096. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2097. if (wpa_s->sched_scanning)
  2098. wpa_supplicant_cancel_sched_scan(wpa_s);
  2099. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2100. if (wpa_s->current_ssid) {
  2101. #ifdef CONFIG_SME
  2102. wpa_s->sme.prev_bssid_set = 0;
  2103. #endif /* CONFIG_SME */
  2104. wpa_sm_set_config(wpa_s->wpa, NULL);
  2105. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2106. wpa_supplicant_deauthenticate(
  2107. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2108. }
  2109. ssid = wpa_s->conf->ssid;
  2110. while (ssid) {
  2111. struct wpa_ssid *remove_ssid = ssid;
  2112. id = ssid->id;
  2113. ssid = ssid->next;
  2114. wpas_notify_network_removed(wpa_s, remove_ssid);
  2115. wpa_config_remove_network(wpa_s->conf, id);
  2116. }
  2117. return 0;
  2118. }
  2119. id = atoi(cmd);
  2120. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2121. ssid = wpa_config_get_network(wpa_s->conf, id);
  2122. if (ssid)
  2123. wpas_notify_network_removed(wpa_s, ssid);
  2124. if (ssid == NULL) {
  2125. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2126. "id=%d", id);
  2127. return -1;
  2128. }
  2129. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  2130. #ifdef CONFIG_SME
  2131. wpa_s->sme.prev_bssid_set = 0;
  2132. #endif /* CONFIG_SME */
  2133. /*
  2134. * Invalidate the EAP session cache if the current or
  2135. * previously used network is removed.
  2136. */
  2137. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2138. }
  2139. if (ssid == wpa_s->current_ssid) {
  2140. wpa_sm_set_config(wpa_s->wpa, NULL);
  2141. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2142. wpa_supplicant_deauthenticate(wpa_s,
  2143. WLAN_REASON_DEAUTH_LEAVING);
  2144. }
  2145. was_disabled = ssid->disabled;
  2146. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2147. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2148. "network id=%d", id);
  2149. return -1;
  2150. }
  2151. if (!was_disabled && wpa_s->sched_scanning) {
  2152. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2153. "network from filters");
  2154. wpa_supplicant_cancel_sched_scan(wpa_s);
  2155. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2156. }
  2157. return 0;
  2158. }
  2159. static int wpa_supplicant_ctrl_iface_update_network(
  2160. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2161. char *name, char *value)
  2162. {
  2163. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2164. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2165. "variable '%s'", name);
  2166. return -1;
  2167. }
  2168. if (os_strcmp(name, "bssid") != 0 &&
  2169. os_strcmp(name, "priority") != 0)
  2170. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2171. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2172. /*
  2173. * Invalidate the EAP session cache if anything in the current
  2174. * or previously used configuration changes.
  2175. */
  2176. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2177. }
  2178. if ((os_strcmp(name, "psk") == 0 &&
  2179. value[0] == '"' && ssid->ssid_len) ||
  2180. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2181. wpa_config_update_psk(ssid);
  2182. else if (os_strcmp(name, "priority") == 0)
  2183. wpa_config_update_prio_list(wpa_s->conf);
  2184. return 0;
  2185. }
  2186. static int wpa_supplicant_ctrl_iface_set_network(
  2187. struct wpa_supplicant *wpa_s, char *cmd)
  2188. {
  2189. int id;
  2190. struct wpa_ssid *ssid;
  2191. char *name, *value;
  2192. /* cmd: "<network id> <variable name> <value>" */
  2193. name = os_strchr(cmd, ' ');
  2194. if (name == NULL)
  2195. return -1;
  2196. *name++ = '\0';
  2197. value = os_strchr(name, ' ');
  2198. if (value == NULL)
  2199. return -1;
  2200. *value++ = '\0';
  2201. id = atoi(cmd);
  2202. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2203. id, name);
  2204. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2205. (u8 *) value, os_strlen(value));
  2206. ssid = wpa_config_get_network(wpa_s->conf, id);
  2207. if (ssid == NULL) {
  2208. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2209. "id=%d", id);
  2210. return -1;
  2211. }
  2212. return wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2213. value);
  2214. }
  2215. static int wpa_supplicant_ctrl_iface_get_network(
  2216. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2217. {
  2218. int id;
  2219. size_t res;
  2220. struct wpa_ssid *ssid;
  2221. char *name, *value;
  2222. /* cmd: "<network id> <variable name>" */
  2223. name = os_strchr(cmd, ' ');
  2224. if (name == NULL || buflen == 0)
  2225. return -1;
  2226. *name++ = '\0';
  2227. id = atoi(cmd);
  2228. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2229. id, name);
  2230. ssid = wpa_config_get_network(wpa_s->conf, id);
  2231. if (ssid == NULL) {
  2232. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2233. "id=%d", id);
  2234. return -1;
  2235. }
  2236. value = wpa_config_get_no_key(ssid, name);
  2237. if (value == NULL) {
  2238. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2239. "variable '%s'", name);
  2240. return -1;
  2241. }
  2242. res = os_strlcpy(buf, value, buflen);
  2243. if (res >= buflen) {
  2244. os_free(value);
  2245. return -1;
  2246. }
  2247. os_free(value);
  2248. return res;
  2249. }
  2250. static int wpa_supplicant_ctrl_iface_dup_network(
  2251. struct wpa_supplicant *wpa_s, char *cmd)
  2252. {
  2253. struct wpa_ssid *ssid_s, *ssid_d;
  2254. char *name, *id, *value;
  2255. int id_s, id_d, ret;
  2256. /* cmd: "<src network id> <dst network id> <variable name>" */
  2257. id = os_strchr(cmd, ' ');
  2258. if (id == NULL)
  2259. return -1;
  2260. *id++ = '\0';
  2261. name = os_strchr(id, ' ');
  2262. if (name == NULL)
  2263. return -1;
  2264. *name++ = '\0';
  2265. id_s = atoi(cmd);
  2266. id_d = atoi(id);
  2267. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
  2268. id_s, id_d, name);
  2269. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2270. if (ssid_s == NULL) {
  2271. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2272. "network id=%d", id_s);
  2273. return -1;
  2274. }
  2275. ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
  2276. if (ssid_d == NULL) {
  2277. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2278. "network id=%d", id_s);
  2279. return -1;
  2280. }
  2281. value = wpa_config_get(ssid_s, name);
  2282. if (value == NULL) {
  2283. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2284. "variable '%s'", name);
  2285. return -1;
  2286. }
  2287. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
  2288. value);
  2289. os_free(value);
  2290. return ret;
  2291. }
  2292. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2293. char *buf, size_t buflen)
  2294. {
  2295. char *pos, *end;
  2296. struct wpa_cred *cred;
  2297. int ret;
  2298. pos = buf;
  2299. end = buf + buflen;
  2300. ret = os_snprintf(pos, end - pos,
  2301. "cred id / realm / username / domain / imsi\n");
  2302. if (ret < 0 || ret >= end - pos)
  2303. return pos - buf;
  2304. pos += ret;
  2305. cred = wpa_s->conf->cred;
  2306. while (cred) {
  2307. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2308. cred->id, cred->realm ? cred->realm : "",
  2309. cred->username ? cred->username : "",
  2310. cred->domain ? cred->domain[0] : "",
  2311. cred->imsi ? cred->imsi : "");
  2312. if (ret < 0 || ret >= end - pos)
  2313. return pos - buf;
  2314. pos += ret;
  2315. cred = cred->next;
  2316. }
  2317. return pos - buf;
  2318. }
  2319. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2320. char *buf, size_t buflen)
  2321. {
  2322. struct wpa_cred *cred;
  2323. int ret;
  2324. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2325. cred = wpa_config_add_cred(wpa_s->conf);
  2326. if (cred == NULL)
  2327. return -1;
  2328. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2329. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2330. if (ret < 0 || (size_t) ret >= buflen)
  2331. return -1;
  2332. return ret;
  2333. }
  2334. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2335. struct wpa_cred *cred)
  2336. {
  2337. struct wpa_ssid *ssid;
  2338. char str[20];
  2339. int id;
  2340. if (cred == NULL) {
  2341. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2342. return -1;
  2343. }
  2344. id = cred->id;
  2345. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2346. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2347. return -1;
  2348. }
  2349. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2350. /* Remove any network entry created based on the removed credential */
  2351. ssid = wpa_s->conf->ssid;
  2352. while (ssid) {
  2353. if (ssid->parent_cred == cred) {
  2354. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2355. "used the removed credential", ssid->id);
  2356. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2357. ssid = ssid->next;
  2358. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2359. } else
  2360. ssid = ssid->next;
  2361. }
  2362. return 0;
  2363. }
  2364. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2365. char *cmd)
  2366. {
  2367. int id;
  2368. struct wpa_cred *cred, *prev;
  2369. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2370. * "provisioning_sp=<FQDN> */
  2371. if (os_strcmp(cmd, "all") == 0) {
  2372. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2373. cred = wpa_s->conf->cred;
  2374. while (cred) {
  2375. prev = cred;
  2376. cred = cred->next;
  2377. wpas_ctrl_remove_cred(wpa_s, prev);
  2378. }
  2379. return 0;
  2380. }
  2381. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2382. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2383. cmd + 8);
  2384. cred = wpa_s->conf->cred;
  2385. while (cred) {
  2386. prev = cred;
  2387. cred = cred->next;
  2388. if (prev->domain) {
  2389. size_t i;
  2390. for (i = 0; i < prev->num_domain; i++) {
  2391. if (os_strcmp(prev->domain[i], cmd + 8)
  2392. != 0)
  2393. continue;
  2394. wpas_ctrl_remove_cred(wpa_s, prev);
  2395. break;
  2396. }
  2397. }
  2398. }
  2399. return 0;
  2400. }
  2401. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2402. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2403. cmd + 16);
  2404. cred = wpa_s->conf->cred;
  2405. while (cred) {
  2406. prev = cred;
  2407. cred = cred->next;
  2408. if (prev->provisioning_sp &&
  2409. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2410. wpas_ctrl_remove_cred(wpa_s, prev);
  2411. }
  2412. return 0;
  2413. }
  2414. id = atoi(cmd);
  2415. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2416. cred = wpa_config_get_cred(wpa_s->conf, id);
  2417. return wpas_ctrl_remove_cred(wpa_s, cred);
  2418. }
  2419. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2420. char *cmd)
  2421. {
  2422. int id;
  2423. struct wpa_cred *cred;
  2424. char *name, *value;
  2425. /* cmd: "<cred id> <variable name> <value>" */
  2426. name = os_strchr(cmd, ' ');
  2427. if (name == NULL)
  2428. return -1;
  2429. *name++ = '\0';
  2430. value = os_strchr(name, ' ');
  2431. if (value == NULL)
  2432. return -1;
  2433. *value++ = '\0';
  2434. id = atoi(cmd);
  2435. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2436. id, name);
  2437. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2438. (u8 *) value, os_strlen(value));
  2439. cred = wpa_config_get_cred(wpa_s->conf, id);
  2440. if (cred == NULL) {
  2441. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2442. id);
  2443. return -1;
  2444. }
  2445. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2446. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2447. "variable '%s'", name);
  2448. return -1;
  2449. }
  2450. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2451. return 0;
  2452. }
  2453. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2454. char *cmd, char *buf,
  2455. size_t buflen)
  2456. {
  2457. int id;
  2458. size_t res;
  2459. struct wpa_cred *cred;
  2460. char *name, *value;
  2461. /* cmd: "<cred id> <variable name>" */
  2462. name = os_strchr(cmd, ' ');
  2463. if (name == NULL)
  2464. return -1;
  2465. *name++ = '\0';
  2466. id = atoi(cmd);
  2467. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2468. id, name);
  2469. cred = wpa_config_get_cred(wpa_s->conf, id);
  2470. if (cred == NULL) {
  2471. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2472. id);
  2473. return -1;
  2474. }
  2475. value = wpa_config_get_cred_no_key(cred, name);
  2476. if (value == NULL) {
  2477. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2478. name);
  2479. return -1;
  2480. }
  2481. res = os_strlcpy(buf, value, buflen);
  2482. if (res >= buflen) {
  2483. os_free(value);
  2484. return -1;
  2485. }
  2486. os_free(value);
  2487. return res;
  2488. }
  2489. #ifndef CONFIG_NO_CONFIG_WRITE
  2490. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2491. {
  2492. int ret;
  2493. if (!wpa_s->conf->update_config) {
  2494. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2495. "to update configuration (update_config=0)");
  2496. return -1;
  2497. }
  2498. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2499. if (ret) {
  2500. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2501. "update configuration");
  2502. } else {
  2503. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2504. " updated");
  2505. }
  2506. return ret;
  2507. }
  2508. #endif /* CONFIG_NO_CONFIG_WRITE */
  2509. struct cipher_info {
  2510. unsigned int capa;
  2511. const char *name;
  2512. int group_only;
  2513. };
  2514. static const struct cipher_info ciphers[] = {
  2515. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2516. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2517. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2518. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2519. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2520. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2521. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2522. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2523. };
  2524. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2525. struct wpa_driver_capa *capa,
  2526. char *buf, size_t buflen)
  2527. {
  2528. int ret;
  2529. char *pos, *end;
  2530. size_t len;
  2531. unsigned int i;
  2532. pos = buf;
  2533. end = pos + buflen;
  2534. if (res < 0) {
  2535. if (strict)
  2536. return 0;
  2537. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2538. if (len >= buflen)
  2539. return -1;
  2540. return len;
  2541. }
  2542. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2543. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2544. ret = os_snprintf(pos, end - pos, "%s%s",
  2545. pos == buf ? "" : " ",
  2546. ciphers[i].name);
  2547. if (ret < 0 || ret >= end - pos)
  2548. return pos - buf;
  2549. pos += ret;
  2550. }
  2551. }
  2552. return pos - buf;
  2553. }
  2554. static int ctrl_iface_get_capability_group(int res, char *strict,
  2555. struct wpa_driver_capa *capa,
  2556. char *buf, size_t buflen)
  2557. {
  2558. int ret;
  2559. char *pos, *end;
  2560. size_t len;
  2561. unsigned int i;
  2562. pos = buf;
  2563. end = pos + buflen;
  2564. if (res < 0) {
  2565. if (strict)
  2566. return 0;
  2567. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2568. if (len >= buflen)
  2569. return -1;
  2570. return len;
  2571. }
  2572. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2573. if (capa->enc & ciphers[i].capa) {
  2574. ret = os_snprintf(pos, end - pos, "%s%s",
  2575. pos == buf ? "" : " ",
  2576. ciphers[i].name);
  2577. if (ret < 0 || ret >= end - pos)
  2578. return pos - buf;
  2579. pos += ret;
  2580. }
  2581. }
  2582. return pos - buf;
  2583. }
  2584. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2585. struct wpa_driver_capa *capa,
  2586. char *buf, size_t buflen)
  2587. {
  2588. int ret;
  2589. char *pos, *end;
  2590. size_t len;
  2591. pos = buf;
  2592. end = pos + buflen;
  2593. if (res < 0) {
  2594. if (strict)
  2595. return 0;
  2596. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2597. "NONE", buflen);
  2598. if (len >= buflen)
  2599. return -1;
  2600. return len;
  2601. }
  2602. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2603. if (ret < 0 || ret >= end - pos)
  2604. return pos - buf;
  2605. pos += ret;
  2606. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2607. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2608. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2609. if (ret < 0 || ret >= end - pos)
  2610. return pos - buf;
  2611. pos += ret;
  2612. }
  2613. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2614. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2615. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2616. if (ret < 0 || ret >= end - pos)
  2617. return pos - buf;
  2618. pos += ret;
  2619. }
  2620. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2621. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2622. if (ret < 0 || ret >= end - pos)
  2623. return pos - buf;
  2624. pos += ret;
  2625. }
  2626. return pos - buf;
  2627. }
  2628. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2629. struct wpa_driver_capa *capa,
  2630. char *buf, size_t buflen)
  2631. {
  2632. int ret;
  2633. char *pos, *end;
  2634. size_t len;
  2635. pos = buf;
  2636. end = pos + buflen;
  2637. if (res < 0) {
  2638. if (strict)
  2639. return 0;
  2640. len = os_strlcpy(buf, "RSN WPA", buflen);
  2641. if (len >= buflen)
  2642. return -1;
  2643. return len;
  2644. }
  2645. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2646. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2647. ret = os_snprintf(pos, end - pos, "%sRSN",
  2648. pos == buf ? "" : " ");
  2649. if (ret < 0 || ret >= end - pos)
  2650. return pos - buf;
  2651. pos += ret;
  2652. }
  2653. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2654. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2655. ret = os_snprintf(pos, end - pos, "%sWPA",
  2656. pos == buf ? "" : " ");
  2657. if (ret < 0 || ret >= end - pos)
  2658. return pos - buf;
  2659. pos += ret;
  2660. }
  2661. return pos - buf;
  2662. }
  2663. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2664. struct wpa_driver_capa *capa,
  2665. char *buf, size_t buflen)
  2666. {
  2667. int ret;
  2668. char *pos, *end;
  2669. size_t len;
  2670. pos = buf;
  2671. end = pos + buflen;
  2672. if (res < 0) {
  2673. if (strict)
  2674. return 0;
  2675. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2676. if (len >= buflen)
  2677. return -1;
  2678. return len;
  2679. }
  2680. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2681. ret = os_snprintf(pos, end - pos, "%sOPEN",
  2682. pos == buf ? "" : " ");
  2683. if (ret < 0 || ret >= end - pos)
  2684. return pos - buf;
  2685. pos += ret;
  2686. }
  2687. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2688. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2689. pos == buf ? "" : " ");
  2690. if (ret < 0 || ret >= end - pos)
  2691. return pos - buf;
  2692. pos += ret;
  2693. }
  2694. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2695. ret = os_snprintf(pos, end - pos, "%sLEAP",
  2696. pos == buf ? "" : " ");
  2697. if (ret < 0 || ret >= end - pos)
  2698. return pos - buf;
  2699. pos += ret;
  2700. }
  2701. return pos - buf;
  2702. }
  2703. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2704. struct wpa_driver_capa *capa,
  2705. char *buf, size_t buflen)
  2706. {
  2707. int ret;
  2708. char *pos, *end;
  2709. size_t len;
  2710. pos = buf;
  2711. end = pos + buflen;
  2712. if (res < 0) {
  2713. if (strict)
  2714. return 0;
  2715. len = os_strlcpy(buf, "IBSS AP", buflen);
  2716. if (len >= buflen)
  2717. return -1;
  2718. return len;
  2719. }
  2720. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2721. ret = os_snprintf(pos, end - pos, "%sIBSS",
  2722. pos == buf ? "" : " ");
  2723. if (ret < 0 || ret >= end - pos)
  2724. return pos - buf;
  2725. pos += ret;
  2726. }
  2727. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2728. ret = os_snprintf(pos, end - pos, "%sAP",
  2729. pos == buf ? "" : " ");
  2730. if (ret < 0 || ret >= end - pos)
  2731. return pos - buf;
  2732. pos += ret;
  2733. }
  2734. return pos - buf;
  2735. }
  2736. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2737. char *buf, size_t buflen)
  2738. {
  2739. struct hostapd_channel_data *chnl;
  2740. int ret, i, j;
  2741. char *pos, *end, *hmode;
  2742. pos = buf;
  2743. end = pos + buflen;
  2744. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2745. switch (wpa_s->hw.modes[j].mode) {
  2746. case HOSTAPD_MODE_IEEE80211B:
  2747. hmode = "B";
  2748. break;
  2749. case HOSTAPD_MODE_IEEE80211G:
  2750. hmode = "G";
  2751. break;
  2752. case HOSTAPD_MODE_IEEE80211A:
  2753. hmode = "A";
  2754. break;
  2755. case HOSTAPD_MODE_IEEE80211AD:
  2756. hmode = "AD";
  2757. break;
  2758. default:
  2759. continue;
  2760. }
  2761. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2762. if (ret < 0 || ret >= end - pos)
  2763. return pos - buf;
  2764. pos += ret;
  2765. chnl = wpa_s->hw.modes[j].channels;
  2766. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2767. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2768. continue;
  2769. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2770. if (ret < 0 || ret >= end - pos)
  2771. return pos - buf;
  2772. pos += ret;
  2773. }
  2774. ret = os_snprintf(pos, end - pos, "\n");
  2775. if (ret < 0 || ret >= end - pos)
  2776. return pos - buf;
  2777. pos += ret;
  2778. }
  2779. return pos - buf;
  2780. }
  2781. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2782. char *buf, size_t buflen)
  2783. {
  2784. struct hostapd_channel_data *chnl;
  2785. int ret, i, j;
  2786. char *pos, *end, *hmode;
  2787. pos = buf;
  2788. end = pos + buflen;
  2789. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2790. switch (wpa_s->hw.modes[j].mode) {
  2791. case HOSTAPD_MODE_IEEE80211B:
  2792. hmode = "B";
  2793. break;
  2794. case HOSTAPD_MODE_IEEE80211G:
  2795. hmode = "G";
  2796. break;
  2797. case HOSTAPD_MODE_IEEE80211A:
  2798. hmode = "A";
  2799. break;
  2800. case HOSTAPD_MODE_IEEE80211AD:
  2801. hmode = "AD";
  2802. break;
  2803. default:
  2804. continue;
  2805. }
  2806. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2807. hmode);
  2808. if (ret < 0 || ret >= end - pos)
  2809. return pos - buf;
  2810. pos += ret;
  2811. chnl = wpa_s->hw.modes[j].channels;
  2812. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2813. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2814. continue;
  2815. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  2816. chnl[i].chan, chnl[i].freq,
  2817. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2818. " (NO_IBSS)" : "",
  2819. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  2820. " (DFS)" : "");
  2821. if (ret < 0 || ret >= end - pos)
  2822. return pos - buf;
  2823. pos += ret;
  2824. }
  2825. ret = os_snprintf(pos, end - pos, "\n");
  2826. if (ret < 0 || ret >= end - pos)
  2827. return pos - buf;
  2828. pos += ret;
  2829. }
  2830. return pos - buf;
  2831. }
  2832. static int wpa_supplicant_ctrl_iface_get_capability(
  2833. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2834. size_t buflen)
  2835. {
  2836. struct wpa_driver_capa capa;
  2837. int res;
  2838. char *strict;
  2839. char field[30];
  2840. size_t len;
  2841. /* Determine whether or not strict checking was requested */
  2842. len = os_strlcpy(field, _field, sizeof(field));
  2843. if (len >= sizeof(field))
  2844. return -1;
  2845. strict = os_strchr(field, ' ');
  2846. if (strict != NULL) {
  2847. *strict++ = '\0';
  2848. if (os_strcmp(strict, "strict") != 0)
  2849. return -1;
  2850. }
  2851. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2852. field, strict ? strict : "");
  2853. if (os_strcmp(field, "eap") == 0) {
  2854. return eap_get_names(buf, buflen);
  2855. }
  2856. res = wpa_drv_get_capa(wpa_s, &capa);
  2857. if (os_strcmp(field, "pairwise") == 0)
  2858. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2859. buf, buflen);
  2860. if (os_strcmp(field, "group") == 0)
  2861. return ctrl_iface_get_capability_group(res, strict, &capa,
  2862. buf, buflen);
  2863. if (os_strcmp(field, "key_mgmt") == 0)
  2864. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2865. buf, buflen);
  2866. if (os_strcmp(field, "proto") == 0)
  2867. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2868. buf, buflen);
  2869. if (os_strcmp(field, "auth_alg") == 0)
  2870. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2871. buf, buflen);
  2872. if (os_strcmp(field, "modes") == 0)
  2873. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2874. buf, buflen);
  2875. if (os_strcmp(field, "channels") == 0)
  2876. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2877. if (os_strcmp(field, "freq") == 0)
  2878. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2879. #ifdef CONFIG_TDLS
  2880. if (os_strcmp(field, "tdls") == 0)
  2881. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  2882. #endif /* CONFIG_TDLS */
  2883. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2884. field);
  2885. return -1;
  2886. }
  2887. #ifdef CONFIG_INTERWORKING
  2888. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2889. struct wpabuf *data)
  2890. {
  2891. char *start = pos;
  2892. size_t i;
  2893. int ret;
  2894. const u8 *d;
  2895. if (data == NULL)
  2896. return start;
  2897. ret = os_snprintf(pos, end - pos, "%s=", title);
  2898. if (ret < 0 || ret >= end - pos)
  2899. return start;
  2900. pos += ret;
  2901. d = wpabuf_head_u8(data);
  2902. for (i = 0; i < wpabuf_len(data); i++) {
  2903. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2904. if (ret < 0 || ret >= end - pos)
  2905. return start;
  2906. pos += ret;
  2907. }
  2908. ret = os_snprintf(pos, end - pos, "\n");
  2909. if (ret < 0 || ret >= end - pos)
  2910. return start;
  2911. pos += ret;
  2912. return pos;
  2913. }
  2914. #endif /* CONFIG_INTERWORKING */
  2915. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2916. unsigned long mask, char *buf, size_t buflen)
  2917. {
  2918. size_t i;
  2919. int ret;
  2920. char *pos, *end;
  2921. const u8 *ie, *ie2;
  2922. pos = buf;
  2923. end = buf + buflen;
  2924. if (mask & WPA_BSS_MASK_ID) {
  2925. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2926. if (ret < 0 || ret >= end - pos)
  2927. return 0;
  2928. pos += ret;
  2929. }
  2930. if (mask & WPA_BSS_MASK_BSSID) {
  2931. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2932. MAC2STR(bss->bssid));
  2933. if (ret < 0 || ret >= end - pos)
  2934. return 0;
  2935. pos += ret;
  2936. }
  2937. if (mask & WPA_BSS_MASK_FREQ) {
  2938. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2939. if (ret < 0 || ret >= end - pos)
  2940. return 0;
  2941. pos += ret;
  2942. }
  2943. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2944. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2945. bss->beacon_int);
  2946. if (ret < 0 || ret >= end - pos)
  2947. return 0;
  2948. pos += ret;
  2949. }
  2950. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2951. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2952. bss->caps);
  2953. if (ret < 0 || ret >= end - pos)
  2954. return 0;
  2955. pos += ret;
  2956. }
  2957. if (mask & WPA_BSS_MASK_QUAL) {
  2958. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2959. if (ret < 0 || ret >= end - pos)
  2960. return 0;
  2961. pos += ret;
  2962. }
  2963. if (mask & WPA_BSS_MASK_NOISE) {
  2964. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2965. if (ret < 0 || ret >= end - pos)
  2966. return 0;
  2967. pos += ret;
  2968. }
  2969. if (mask & WPA_BSS_MASK_LEVEL) {
  2970. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2971. if (ret < 0 || ret >= end - pos)
  2972. return 0;
  2973. pos += ret;
  2974. }
  2975. if (mask & WPA_BSS_MASK_TSF) {
  2976. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2977. (unsigned long long) bss->tsf);
  2978. if (ret < 0 || ret >= end - pos)
  2979. return 0;
  2980. pos += ret;
  2981. }
  2982. if (mask & WPA_BSS_MASK_AGE) {
  2983. struct os_reltime now;
  2984. os_get_reltime(&now);
  2985. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2986. (int) (now.sec - bss->last_update.sec));
  2987. if (ret < 0 || ret >= end - pos)
  2988. return 0;
  2989. pos += ret;
  2990. }
  2991. if (mask & WPA_BSS_MASK_IE) {
  2992. ret = os_snprintf(pos, end - pos, "ie=");
  2993. if (ret < 0 || ret >= end - pos)
  2994. return 0;
  2995. pos += ret;
  2996. ie = (const u8 *) (bss + 1);
  2997. for (i = 0; i < bss->ie_len; i++) {
  2998. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2999. if (ret < 0 || ret >= end - pos)
  3000. return 0;
  3001. pos += ret;
  3002. }
  3003. ret = os_snprintf(pos, end - pos, "\n");
  3004. if (ret < 0 || ret >= end - pos)
  3005. return 0;
  3006. pos += ret;
  3007. }
  3008. if (mask & WPA_BSS_MASK_FLAGS) {
  3009. ret = os_snprintf(pos, end - pos, "flags=");
  3010. if (ret < 0 || ret >= end - pos)
  3011. return 0;
  3012. pos += ret;
  3013. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3014. if (ie)
  3015. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3016. 2 + ie[1]);
  3017. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3018. if (ie2)
  3019. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3020. 2 + ie2[1]);
  3021. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3022. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  3023. ret = os_snprintf(pos, end - pos, "[WEP]");
  3024. if (ret < 0 || ret >= end - pos)
  3025. return 0;
  3026. pos += ret;
  3027. }
  3028. if (bss_is_dmg(bss)) {
  3029. const char *s;
  3030. ret = os_snprintf(pos, end - pos, "[DMG]");
  3031. if (ret < 0 || ret >= end - pos)
  3032. return 0;
  3033. pos += ret;
  3034. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3035. case IEEE80211_CAP_DMG_IBSS:
  3036. s = "[IBSS]";
  3037. break;
  3038. case IEEE80211_CAP_DMG_AP:
  3039. s = "[ESS]";
  3040. break;
  3041. case IEEE80211_CAP_DMG_PBSS:
  3042. s = "[PBSS]";
  3043. break;
  3044. default:
  3045. s = "";
  3046. break;
  3047. }
  3048. ret = os_snprintf(pos, end - pos, "%s", s);
  3049. if (ret < 0 || ret >= end - pos)
  3050. return 0;
  3051. pos += ret;
  3052. } else {
  3053. if (bss->caps & IEEE80211_CAP_IBSS) {
  3054. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3055. if (ret < 0 || ret >= end - pos)
  3056. return 0;
  3057. pos += ret;
  3058. }
  3059. if (bss->caps & IEEE80211_CAP_ESS) {
  3060. ret = os_snprintf(pos, end - pos, "[ESS]");
  3061. if (ret < 0 || ret >= end - pos)
  3062. return 0;
  3063. pos += ret;
  3064. }
  3065. }
  3066. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3067. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3068. ret = os_snprintf(pos, end - pos, "[P2P]");
  3069. if (ret < 0 || ret >= end - pos)
  3070. return 0;
  3071. pos += ret;
  3072. }
  3073. #ifdef CONFIG_HS20
  3074. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3075. ret = os_snprintf(pos, end - pos, "[HS20]");
  3076. if (ret < 0 || ret >= end - pos)
  3077. return 0;
  3078. pos += ret;
  3079. }
  3080. #endif /* CONFIG_HS20 */
  3081. ret = os_snprintf(pos, end - pos, "\n");
  3082. if (ret < 0 || ret >= end - pos)
  3083. return 0;
  3084. pos += ret;
  3085. }
  3086. if (mask & WPA_BSS_MASK_SSID) {
  3087. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3088. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3089. if (ret < 0 || ret >= end - pos)
  3090. return 0;
  3091. pos += ret;
  3092. }
  3093. #ifdef CONFIG_WPS
  3094. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3095. ie = (const u8 *) (bss + 1);
  3096. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3097. if (ret < 0 || ret >= end - pos)
  3098. return 0;
  3099. pos += ret;
  3100. }
  3101. #endif /* CONFIG_WPS */
  3102. #ifdef CONFIG_P2P
  3103. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3104. ie = (const u8 *) (bss + 1);
  3105. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3106. if (ret < 0 || ret >= end - pos)
  3107. return 0;
  3108. pos += ret;
  3109. }
  3110. #endif /* CONFIG_P2P */
  3111. #ifdef CONFIG_WIFI_DISPLAY
  3112. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3113. struct wpabuf *wfd;
  3114. ie = (const u8 *) (bss + 1);
  3115. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3116. WFD_IE_VENDOR_TYPE);
  3117. if (wfd) {
  3118. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3119. if (ret < 0 || ret >= end - pos) {
  3120. wpabuf_free(wfd);
  3121. return 0;
  3122. }
  3123. pos += ret;
  3124. pos += wpa_snprintf_hex(pos, end - pos,
  3125. wpabuf_head(wfd),
  3126. wpabuf_len(wfd));
  3127. wpabuf_free(wfd);
  3128. ret = os_snprintf(pos, end - pos, "\n");
  3129. if (ret < 0 || ret >= end - pos)
  3130. return 0;
  3131. pos += ret;
  3132. }
  3133. }
  3134. #endif /* CONFIG_WIFI_DISPLAY */
  3135. #ifdef CONFIG_INTERWORKING
  3136. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3137. struct wpa_bss_anqp *anqp = bss->anqp;
  3138. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3139. anqp->venue_name);
  3140. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3141. anqp->network_auth_type);
  3142. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3143. anqp->roaming_consortium);
  3144. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3145. anqp->ip_addr_type_availability);
  3146. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3147. anqp->nai_realm);
  3148. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3149. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3150. anqp->domain_name);
  3151. #ifdef CONFIG_HS20
  3152. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3153. anqp->hs20_operator_friendly_name);
  3154. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3155. anqp->hs20_wan_metrics);
  3156. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3157. anqp->hs20_connection_capability);
  3158. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3159. anqp->hs20_operating_class);
  3160. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3161. anqp->hs20_osu_providers_list);
  3162. #endif /* CONFIG_HS20 */
  3163. }
  3164. #endif /* CONFIG_INTERWORKING */
  3165. if (mask & WPA_BSS_MASK_DELIM) {
  3166. ret = os_snprintf(pos, end - pos, "====\n");
  3167. if (ret < 0 || ret >= end - pos)
  3168. return 0;
  3169. pos += ret;
  3170. }
  3171. return pos - buf;
  3172. }
  3173. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3174. const char *cmd, char *buf,
  3175. size_t buflen)
  3176. {
  3177. u8 bssid[ETH_ALEN];
  3178. size_t i;
  3179. struct wpa_bss *bss;
  3180. struct wpa_bss *bsslast = NULL;
  3181. struct dl_list *next;
  3182. int ret = 0;
  3183. int len;
  3184. char *ctmp;
  3185. unsigned long mask = WPA_BSS_MASK_ALL;
  3186. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3187. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3188. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3189. list_id);
  3190. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3191. list_id);
  3192. } else { /* N1-N2 */
  3193. unsigned int id1, id2;
  3194. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3195. wpa_printf(MSG_INFO, "Wrong BSS range "
  3196. "format");
  3197. return 0;
  3198. }
  3199. if (*(cmd + 6) == '-')
  3200. id1 = 0;
  3201. else
  3202. id1 = atoi(cmd + 6);
  3203. ctmp++;
  3204. if (*ctmp >= '0' && *ctmp <= '9')
  3205. id2 = atoi(ctmp);
  3206. else
  3207. id2 = (unsigned int) -1;
  3208. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3209. if (id2 == (unsigned int) -1)
  3210. bsslast = dl_list_last(&wpa_s->bss_id,
  3211. struct wpa_bss,
  3212. list_id);
  3213. else {
  3214. bsslast = wpa_bss_get_id(wpa_s, id2);
  3215. if (bsslast == NULL && bss && id2 > id1) {
  3216. struct wpa_bss *tmp = bss;
  3217. for (;;) {
  3218. next = tmp->list_id.next;
  3219. if (next == &wpa_s->bss_id)
  3220. break;
  3221. tmp = dl_list_entry(
  3222. next, struct wpa_bss,
  3223. list_id);
  3224. if (tmp->id > id2)
  3225. break;
  3226. bsslast = tmp;
  3227. }
  3228. }
  3229. }
  3230. }
  3231. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3232. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3233. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3234. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3235. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3236. i = atoi(cmd + 3);
  3237. bss = wpa_bss_get_id(wpa_s, i);
  3238. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3239. i = atoi(cmd + 5);
  3240. bss = wpa_bss_get_id(wpa_s, i);
  3241. if (bss) {
  3242. next = bss->list_id.next;
  3243. if (next == &wpa_s->bss_id)
  3244. bss = NULL;
  3245. else
  3246. bss = dl_list_entry(next, struct wpa_bss,
  3247. list_id);
  3248. }
  3249. #ifdef CONFIG_P2P
  3250. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3251. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3252. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3253. else
  3254. bss = NULL;
  3255. #endif /* CONFIG_P2P */
  3256. } else if (hwaddr_aton(cmd, bssid) == 0)
  3257. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3258. else {
  3259. struct wpa_bss *tmp;
  3260. i = atoi(cmd);
  3261. bss = NULL;
  3262. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3263. {
  3264. if (i-- == 0) {
  3265. bss = tmp;
  3266. break;
  3267. }
  3268. }
  3269. }
  3270. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3271. mask = strtoul(ctmp + 5, NULL, 0x10);
  3272. if (mask == 0)
  3273. mask = WPA_BSS_MASK_ALL;
  3274. }
  3275. if (bss == NULL)
  3276. return 0;
  3277. if (bsslast == NULL)
  3278. bsslast = bss;
  3279. do {
  3280. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3281. ret += len;
  3282. buf += len;
  3283. buflen -= len;
  3284. if (bss == bsslast) {
  3285. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3286. (bss == dl_list_last(&wpa_s->bss_id,
  3287. struct wpa_bss, list_id)))
  3288. os_snprintf(buf - 5, 5, "####\n");
  3289. break;
  3290. }
  3291. next = bss->list_id.next;
  3292. if (next == &wpa_s->bss_id)
  3293. break;
  3294. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3295. } while (bss && len);
  3296. return ret;
  3297. }
  3298. static int wpa_supplicant_ctrl_iface_ap_scan(
  3299. struct wpa_supplicant *wpa_s, char *cmd)
  3300. {
  3301. int ap_scan = atoi(cmd);
  3302. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3303. }
  3304. static int wpa_supplicant_ctrl_iface_scan_interval(
  3305. struct wpa_supplicant *wpa_s, char *cmd)
  3306. {
  3307. int scan_int = atoi(cmd);
  3308. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3309. }
  3310. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3311. struct wpa_supplicant *wpa_s, char *cmd)
  3312. {
  3313. int expire_age = atoi(cmd);
  3314. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3315. }
  3316. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3317. struct wpa_supplicant *wpa_s, char *cmd)
  3318. {
  3319. int expire_count = atoi(cmd);
  3320. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3321. }
  3322. static int wpa_supplicant_ctrl_iface_bss_flush(
  3323. struct wpa_supplicant *wpa_s, char *cmd)
  3324. {
  3325. int flush_age = atoi(cmd);
  3326. if (flush_age == 0)
  3327. wpa_bss_flush(wpa_s);
  3328. else
  3329. wpa_bss_flush_by_age(wpa_s, flush_age);
  3330. return 0;
  3331. }
  3332. #ifdef CONFIG_TESTING_OPTIONS
  3333. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3334. {
  3335. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3336. /* MLME-DELETEKEYS.request */
  3337. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3338. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3339. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3340. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3341. #ifdef CONFIG_IEEE80211W
  3342. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3343. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3344. #endif /* CONFIG_IEEE80211W */
  3345. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3346. 0);
  3347. /* MLME-SETPROTECTION.request(None) */
  3348. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3349. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3350. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3351. wpa_sm_drop_sa(wpa_s->wpa);
  3352. }
  3353. #endif /* CONFIG_TESTING_OPTIONS */
  3354. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3355. char *addr)
  3356. {
  3357. #ifdef CONFIG_NO_SCAN_PROCESSING
  3358. return -1;
  3359. #else /* CONFIG_NO_SCAN_PROCESSING */
  3360. u8 bssid[ETH_ALEN];
  3361. struct wpa_bss *bss;
  3362. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3363. if (hwaddr_aton(addr, bssid)) {
  3364. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3365. "address '%s'", addr);
  3366. return -1;
  3367. }
  3368. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3369. if (!ssid) {
  3370. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3371. "configuration known for the target AP");
  3372. return -1;
  3373. }
  3374. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3375. if (!bss) {
  3376. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3377. "from BSS table");
  3378. return -1;
  3379. }
  3380. /*
  3381. * TODO: Find best network configuration block from configuration to
  3382. * allow roaming to other networks
  3383. */
  3384. wpa_s->reassociate = 1;
  3385. wpa_supplicant_connect(wpa_s, bss, ssid);
  3386. return 0;
  3387. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3388. }
  3389. #ifdef CONFIG_P2P
  3390. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3391. {
  3392. unsigned int timeout = atoi(cmd);
  3393. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3394. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3395. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3396. char *pos;
  3397. unsigned int search_delay;
  3398. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3399. wpa_dbg(wpa_s, MSG_INFO,
  3400. "Reject P2P_FIND since interface is disabled");
  3401. return -1;
  3402. }
  3403. if (os_strstr(cmd, "type=social"))
  3404. type = P2P_FIND_ONLY_SOCIAL;
  3405. else if (os_strstr(cmd, "type=progressive"))
  3406. type = P2P_FIND_PROGRESSIVE;
  3407. pos = os_strstr(cmd, "dev_id=");
  3408. if (pos) {
  3409. pos += 7;
  3410. if (hwaddr_aton(pos, dev_id))
  3411. return -1;
  3412. _dev_id = dev_id;
  3413. }
  3414. pos = os_strstr(cmd, "dev_type=");
  3415. if (pos) {
  3416. pos += 9;
  3417. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3418. return -1;
  3419. _dev_type = dev_type;
  3420. }
  3421. pos = os_strstr(cmd, "delay=");
  3422. if (pos) {
  3423. pos += 6;
  3424. search_delay = atoi(pos);
  3425. } else
  3426. search_delay = wpas_p2p_search_delay(wpa_s);
  3427. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3428. _dev_id, search_delay);
  3429. }
  3430. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3431. char *buf, size_t buflen)
  3432. {
  3433. u8 addr[ETH_ALEN];
  3434. char *pos, *pos2;
  3435. char *pin = NULL;
  3436. enum p2p_wps_method wps_method;
  3437. int new_pin;
  3438. int ret;
  3439. int persistent_group, persistent_id = -1;
  3440. int join;
  3441. int auth;
  3442. int automatic;
  3443. int go_intent = -1;
  3444. int freq = 0;
  3445. int pd;
  3446. int ht40, vht;
  3447. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3448. * [persistent|persistent=<network id>]
  3449. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3450. * [ht40] [vht] */
  3451. if (hwaddr_aton(cmd, addr))
  3452. return -1;
  3453. pos = cmd + 17;
  3454. if (*pos != ' ')
  3455. return -1;
  3456. pos++;
  3457. persistent_group = os_strstr(pos, " persistent") != NULL;
  3458. pos2 = os_strstr(pos, " persistent=");
  3459. if (pos2) {
  3460. struct wpa_ssid *ssid;
  3461. persistent_id = atoi(pos2 + 12);
  3462. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3463. if (ssid == NULL || ssid->disabled != 2 ||
  3464. ssid->mode != WPAS_MODE_P2P_GO) {
  3465. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3466. "SSID id=%d for persistent P2P group (GO)",
  3467. persistent_id);
  3468. return -1;
  3469. }
  3470. }
  3471. join = os_strstr(pos, " join") != NULL;
  3472. auth = os_strstr(pos, " auth") != NULL;
  3473. automatic = os_strstr(pos, " auto") != NULL;
  3474. pd = os_strstr(pos, " provdisc") != NULL;
  3475. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3476. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3477. vht;
  3478. pos2 = os_strstr(pos, " go_intent=");
  3479. if (pos2) {
  3480. pos2 += 11;
  3481. go_intent = atoi(pos2);
  3482. if (go_intent < 0 || go_intent > 15)
  3483. return -1;
  3484. }
  3485. pos2 = os_strstr(pos, " freq=");
  3486. if (pos2) {
  3487. pos2 += 6;
  3488. freq = atoi(pos2);
  3489. if (freq <= 0)
  3490. return -1;
  3491. }
  3492. if (os_strncmp(pos, "pin", 3) == 0) {
  3493. /* Request random PIN (to be displayed) and enable the PIN */
  3494. wps_method = WPS_PIN_DISPLAY;
  3495. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3496. wps_method = WPS_PBC;
  3497. } else {
  3498. pin = pos;
  3499. pos = os_strchr(pin, ' ');
  3500. wps_method = WPS_PIN_KEYPAD;
  3501. if (pos) {
  3502. *pos++ = '\0';
  3503. if (os_strncmp(pos, "display", 7) == 0)
  3504. wps_method = WPS_PIN_DISPLAY;
  3505. }
  3506. if (!wps_pin_str_valid(pin)) {
  3507. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3508. return 17;
  3509. }
  3510. }
  3511. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3512. persistent_group, automatic, join,
  3513. auth, go_intent, freq, persistent_id, pd,
  3514. ht40, vht);
  3515. if (new_pin == -2) {
  3516. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3517. return 25;
  3518. }
  3519. if (new_pin == -3) {
  3520. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3521. return 25;
  3522. }
  3523. if (new_pin < 0)
  3524. return -1;
  3525. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3526. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3527. if (ret < 0 || (size_t) ret >= buflen)
  3528. return -1;
  3529. return ret;
  3530. }
  3531. os_memcpy(buf, "OK\n", 3);
  3532. return 3;
  3533. }
  3534. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3535. {
  3536. unsigned int timeout = atoi(cmd);
  3537. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3538. wpa_dbg(wpa_s, MSG_INFO,
  3539. "Reject P2P_LISTEN since interface is disabled");
  3540. return -1;
  3541. }
  3542. return wpas_p2p_listen(wpa_s, timeout);
  3543. }
  3544. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3545. {
  3546. u8 addr[ETH_ALEN];
  3547. char *pos;
  3548. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3549. /* <addr> <config method> [join|auto] */
  3550. if (hwaddr_aton(cmd, addr))
  3551. return -1;
  3552. pos = cmd + 17;
  3553. if (*pos != ' ')
  3554. return -1;
  3555. pos++;
  3556. if (os_strstr(pos, " join") != NULL)
  3557. use = WPAS_P2P_PD_FOR_JOIN;
  3558. else if (os_strstr(pos, " auto") != NULL)
  3559. use = WPAS_P2P_PD_AUTO;
  3560. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3561. }
  3562. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3563. size_t buflen)
  3564. {
  3565. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3566. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3567. ssid->passphrase == NULL)
  3568. return -1;
  3569. os_strlcpy(buf, ssid->passphrase, buflen);
  3570. return os_strlen(buf);
  3571. }
  3572. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3573. char *buf, size_t buflen)
  3574. {
  3575. u64 ref;
  3576. int res;
  3577. u8 dst_buf[ETH_ALEN], *dst;
  3578. struct wpabuf *tlvs;
  3579. char *pos;
  3580. size_t len;
  3581. if (hwaddr_aton(cmd, dst_buf))
  3582. return -1;
  3583. dst = dst_buf;
  3584. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3585. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3586. dst = NULL;
  3587. pos = cmd + 17;
  3588. if (*pos != ' ')
  3589. return -1;
  3590. pos++;
  3591. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3592. u8 version;
  3593. pos += 5;
  3594. if (hexstr2bin(pos, &version, 1) < 0)
  3595. return -1;
  3596. pos += 2;
  3597. if (*pos != ' ')
  3598. return -1;
  3599. pos++;
  3600. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3601. #ifdef CONFIG_WIFI_DISPLAY
  3602. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3603. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3604. #endif /* CONFIG_WIFI_DISPLAY */
  3605. } else {
  3606. len = os_strlen(pos);
  3607. if (len & 1)
  3608. return -1;
  3609. len /= 2;
  3610. tlvs = wpabuf_alloc(len);
  3611. if (tlvs == NULL)
  3612. return -1;
  3613. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3614. wpabuf_free(tlvs);
  3615. return -1;
  3616. }
  3617. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3618. wpabuf_free(tlvs);
  3619. }
  3620. if (ref == 0)
  3621. return -1;
  3622. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3623. if (res < 0 || (unsigned) res >= buflen)
  3624. return -1;
  3625. return res;
  3626. }
  3627. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3628. char *cmd)
  3629. {
  3630. long long unsigned val;
  3631. u64 req;
  3632. if (sscanf(cmd, "%llx", &val) != 1)
  3633. return -1;
  3634. req = val;
  3635. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3636. }
  3637. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3638. {
  3639. int freq;
  3640. u8 dst[ETH_ALEN];
  3641. u8 dialog_token;
  3642. struct wpabuf *resp_tlvs;
  3643. char *pos, *pos2;
  3644. size_t len;
  3645. pos = os_strchr(cmd, ' ');
  3646. if (pos == NULL)
  3647. return -1;
  3648. *pos++ = '\0';
  3649. freq = atoi(cmd);
  3650. if (freq == 0)
  3651. return -1;
  3652. if (hwaddr_aton(pos, dst))
  3653. return -1;
  3654. pos += 17;
  3655. if (*pos != ' ')
  3656. return -1;
  3657. pos++;
  3658. pos2 = os_strchr(pos, ' ');
  3659. if (pos2 == NULL)
  3660. return -1;
  3661. *pos2++ = '\0';
  3662. dialog_token = atoi(pos);
  3663. len = os_strlen(pos2);
  3664. if (len & 1)
  3665. return -1;
  3666. len /= 2;
  3667. resp_tlvs = wpabuf_alloc(len);
  3668. if (resp_tlvs == NULL)
  3669. return -1;
  3670. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3671. wpabuf_free(resp_tlvs);
  3672. return -1;
  3673. }
  3674. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3675. wpabuf_free(resp_tlvs);
  3676. return 0;
  3677. }
  3678. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3679. char *cmd)
  3680. {
  3681. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3682. return -1;
  3683. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3684. return 0;
  3685. }
  3686. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3687. char *cmd)
  3688. {
  3689. char *pos;
  3690. size_t len;
  3691. struct wpabuf *query, *resp;
  3692. pos = os_strchr(cmd, ' ');
  3693. if (pos == NULL)
  3694. return -1;
  3695. *pos++ = '\0';
  3696. len = os_strlen(cmd);
  3697. if (len & 1)
  3698. return -1;
  3699. len /= 2;
  3700. query = wpabuf_alloc(len);
  3701. if (query == NULL)
  3702. return -1;
  3703. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3704. wpabuf_free(query);
  3705. return -1;
  3706. }
  3707. len = os_strlen(pos);
  3708. if (len & 1) {
  3709. wpabuf_free(query);
  3710. return -1;
  3711. }
  3712. len /= 2;
  3713. resp = wpabuf_alloc(len);
  3714. if (resp == NULL) {
  3715. wpabuf_free(query);
  3716. return -1;
  3717. }
  3718. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3719. wpabuf_free(query);
  3720. wpabuf_free(resp);
  3721. return -1;
  3722. }
  3723. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3724. wpabuf_free(query);
  3725. wpabuf_free(resp);
  3726. return -1;
  3727. }
  3728. return 0;
  3729. }
  3730. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3731. {
  3732. char *pos;
  3733. u8 version;
  3734. pos = os_strchr(cmd, ' ');
  3735. if (pos == NULL)
  3736. return -1;
  3737. *pos++ = '\0';
  3738. if (hexstr2bin(cmd, &version, 1) < 0)
  3739. return -1;
  3740. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3741. }
  3742. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3743. {
  3744. char *pos;
  3745. pos = os_strchr(cmd, ' ');
  3746. if (pos == NULL)
  3747. return -1;
  3748. *pos++ = '\0';
  3749. if (os_strcmp(cmd, "bonjour") == 0)
  3750. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3751. if (os_strcmp(cmd, "upnp") == 0)
  3752. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3753. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3754. return -1;
  3755. }
  3756. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3757. char *cmd)
  3758. {
  3759. size_t len;
  3760. struct wpabuf *query;
  3761. int ret;
  3762. len = os_strlen(cmd);
  3763. if (len & 1)
  3764. return -1;
  3765. len /= 2;
  3766. query = wpabuf_alloc(len);
  3767. if (query == NULL)
  3768. return -1;
  3769. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3770. wpabuf_free(query);
  3771. return -1;
  3772. }
  3773. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3774. wpabuf_free(query);
  3775. return ret;
  3776. }
  3777. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3778. {
  3779. char *pos;
  3780. u8 version;
  3781. pos = os_strchr(cmd, ' ');
  3782. if (pos == NULL)
  3783. return -1;
  3784. *pos++ = '\0';
  3785. if (hexstr2bin(cmd, &version, 1) < 0)
  3786. return -1;
  3787. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3788. }
  3789. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3790. {
  3791. char *pos;
  3792. pos = os_strchr(cmd, ' ');
  3793. if (pos == NULL)
  3794. return -1;
  3795. *pos++ = '\0';
  3796. if (os_strcmp(cmd, "bonjour") == 0)
  3797. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3798. if (os_strcmp(cmd, "upnp") == 0)
  3799. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3800. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3801. return -1;
  3802. }
  3803. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3804. {
  3805. u8 addr[ETH_ALEN];
  3806. /* <addr> */
  3807. if (hwaddr_aton(cmd, addr))
  3808. return -1;
  3809. return wpas_p2p_reject(wpa_s, addr);
  3810. }
  3811. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3812. {
  3813. char *pos;
  3814. int id;
  3815. struct wpa_ssid *ssid;
  3816. u8 *_peer = NULL, peer[ETH_ALEN];
  3817. int freq = 0, pref_freq = 0;
  3818. int ht40, vht;
  3819. id = atoi(cmd);
  3820. pos = os_strstr(cmd, " peer=");
  3821. if (pos) {
  3822. pos += 6;
  3823. if (hwaddr_aton(pos, peer))
  3824. return -1;
  3825. _peer = peer;
  3826. }
  3827. ssid = wpa_config_get_network(wpa_s->conf, id);
  3828. if (ssid == NULL || ssid->disabled != 2) {
  3829. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3830. "for persistent P2P group",
  3831. id);
  3832. return -1;
  3833. }
  3834. pos = os_strstr(cmd, " freq=");
  3835. if (pos) {
  3836. pos += 6;
  3837. freq = atoi(pos);
  3838. if (freq <= 0)
  3839. return -1;
  3840. }
  3841. pos = os_strstr(cmd, " pref=");
  3842. if (pos) {
  3843. pos += 6;
  3844. pref_freq = atoi(pos);
  3845. if (pref_freq <= 0)
  3846. return -1;
  3847. }
  3848. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3849. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3850. vht;
  3851. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3852. pref_freq);
  3853. }
  3854. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3855. {
  3856. char *pos;
  3857. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3858. pos = os_strstr(cmd, " peer=");
  3859. if (!pos)
  3860. return -1;
  3861. *pos = '\0';
  3862. pos += 6;
  3863. if (hwaddr_aton(pos, peer)) {
  3864. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3865. return -1;
  3866. }
  3867. pos = os_strstr(pos, " go_dev_addr=");
  3868. if (pos) {
  3869. pos += 13;
  3870. if (hwaddr_aton(pos, go_dev_addr)) {
  3871. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3872. pos);
  3873. return -1;
  3874. }
  3875. go_dev = go_dev_addr;
  3876. }
  3877. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3878. }
  3879. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3880. {
  3881. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3882. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3883. if (os_strncmp(cmd, "group=", 6) == 0)
  3884. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3885. return -1;
  3886. }
  3887. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3888. char *cmd, int freq, int ht40,
  3889. int vht)
  3890. {
  3891. int id;
  3892. struct wpa_ssid *ssid;
  3893. id = atoi(cmd);
  3894. ssid = wpa_config_get_network(wpa_s->conf, id);
  3895. if (ssid == NULL || ssid->disabled != 2) {
  3896. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3897. "for persistent P2P group",
  3898. id);
  3899. return -1;
  3900. }
  3901. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
  3902. NULL, 0);
  3903. }
  3904. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3905. {
  3906. int freq = 0, ht40, vht;
  3907. char *pos;
  3908. pos = os_strstr(cmd, "freq=");
  3909. if (pos)
  3910. freq = atoi(pos + 5);
  3911. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3912. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3913. vht;
  3914. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3915. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3916. ht40, vht);
  3917. if (os_strcmp(cmd, "persistent") == 0 ||
  3918. os_strncmp(cmd, "persistent ", 11) == 0)
  3919. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3920. if (os_strncmp(cmd, "freq=", 5) == 0)
  3921. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3922. if (ht40)
  3923. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3924. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3925. cmd);
  3926. return -1;
  3927. }
  3928. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3929. char *buf, size_t buflen)
  3930. {
  3931. u8 addr[ETH_ALEN], *addr_ptr;
  3932. int next, res;
  3933. const struct p2p_peer_info *info;
  3934. char *pos, *end;
  3935. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3936. struct wpa_ssid *ssid;
  3937. size_t i;
  3938. if (!wpa_s->global->p2p)
  3939. return -1;
  3940. if (os_strcmp(cmd, "FIRST") == 0) {
  3941. addr_ptr = NULL;
  3942. next = 0;
  3943. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3944. if (hwaddr_aton(cmd + 5, addr) < 0)
  3945. return -1;
  3946. addr_ptr = addr;
  3947. next = 1;
  3948. } else {
  3949. if (hwaddr_aton(cmd, addr) < 0)
  3950. return -1;
  3951. addr_ptr = addr;
  3952. next = 0;
  3953. }
  3954. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3955. if (info == NULL)
  3956. return -1;
  3957. pos = buf;
  3958. end = buf + buflen;
  3959. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3960. "pri_dev_type=%s\n"
  3961. "device_name=%s\n"
  3962. "manufacturer=%s\n"
  3963. "model_name=%s\n"
  3964. "model_number=%s\n"
  3965. "serial_number=%s\n"
  3966. "config_methods=0x%x\n"
  3967. "dev_capab=0x%x\n"
  3968. "group_capab=0x%x\n"
  3969. "level=%d\n",
  3970. MAC2STR(info->p2p_device_addr),
  3971. wps_dev_type_bin2str(info->pri_dev_type,
  3972. devtype, sizeof(devtype)),
  3973. info->device_name,
  3974. info->manufacturer,
  3975. info->model_name,
  3976. info->model_number,
  3977. info->serial_number,
  3978. info->config_methods,
  3979. info->dev_capab,
  3980. info->group_capab,
  3981. info->level);
  3982. if (res < 0 || res >= end - pos)
  3983. return pos - buf;
  3984. pos += res;
  3985. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3986. {
  3987. const u8 *t;
  3988. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3989. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3990. wps_dev_type_bin2str(t, devtype,
  3991. sizeof(devtype)));
  3992. if (res < 0 || res >= end - pos)
  3993. return pos - buf;
  3994. pos += res;
  3995. }
  3996. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3997. if (ssid) {
  3998. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3999. if (res < 0 || res >= end - pos)
  4000. return pos - buf;
  4001. pos += res;
  4002. }
  4003. res = p2p_get_peer_info_txt(info, pos, end - pos);
  4004. if (res < 0)
  4005. return pos - buf;
  4006. pos += res;
  4007. return pos - buf;
  4008. }
  4009. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  4010. const char *param)
  4011. {
  4012. unsigned int i;
  4013. if (wpa_s->global->p2p == NULL)
  4014. return -1;
  4015. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  4016. return -1;
  4017. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  4018. struct wpa_freq_range *freq;
  4019. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  4020. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  4021. freq->min, freq->max);
  4022. }
  4023. wpas_p2p_update_channel_list(wpa_s);
  4024. return 0;
  4025. }
  4026. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  4027. {
  4028. char *param;
  4029. if (wpa_s->global->p2p == NULL)
  4030. return -1;
  4031. param = os_strchr(cmd, ' ');
  4032. if (param == NULL)
  4033. return -1;
  4034. *param++ = '\0';
  4035. if (os_strcmp(cmd, "discoverability") == 0) {
  4036. p2p_set_client_discoverability(wpa_s->global->p2p,
  4037. atoi(param));
  4038. return 0;
  4039. }
  4040. if (os_strcmp(cmd, "managed") == 0) {
  4041. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  4042. return 0;
  4043. }
  4044. if (os_strcmp(cmd, "listen_channel") == 0) {
  4045. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  4046. atoi(param));
  4047. }
  4048. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  4049. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  4050. os_strlen(param));
  4051. }
  4052. if (os_strcmp(cmd, "noa") == 0) {
  4053. char *pos;
  4054. int count, start, duration;
  4055. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  4056. count = atoi(param);
  4057. pos = os_strchr(param, ',');
  4058. if (pos == NULL)
  4059. return -1;
  4060. pos++;
  4061. start = atoi(pos);
  4062. pos = os_strchr(pos, ',');
  4063. if (pos == NULL)
  4064. return -1;
  4065. pos++;
  4066. duration = atoi(pos);
  4067. if (count < 0 || count > 255 || start < 0 || duration < 0)
  4068. return -1;
  4069. if (count == 0 && duration > 0)
  4070. return -1;
  4071. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  4072. "start=%d duration=%d", count, start, duration);
  4073. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  4074. }
  4075. if (os_strcmp(cmd, "ps") == 0)
  4076. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  4077. if (os_strcmp(cmd, "oppps") == 0)
  4078. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  4079. if (os_strcmp(cmd, "ctwindow") == 0)
  4080. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  4081. if (os_strcmp(cmd, "disabled") == 0) {
  4082. wpa_s->global->p2p_disabled = atoi(param);
  4083. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  4084. wpa_s->global->p2p_disabled ?
  4085. "disabled" : "enabled");
  4086. if (wpa_s->global->p2p_disabled) {
  4087. wpas_p2p_stop_find(wpa_s);
  4088. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4089. p2p_flush(wpa_s->global->p2p);
  4090. }
  4091. return 0;
  4092. }
  4093. if (os_strcmp(cmd, "conc_pref") == 0) {
  4094. if (os_strcmp(param, "sta") == 0)
  4095. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  4096. else if (os_strcmp(param, "p2p") == 0)
  4097. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  4098. else {
  4099. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  4100. return -1;
  4101. }
  4102. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  4103. "%s", param);
  4104. return 0;
  4105. }
  4106. if (os_strcmp(cmd, "force_long_sd") == 0) {
  4107. wpa_s->force_long_sd = atoi(param);
  4108. return 0;
  4109. }
  4110. if (os_strcmp(cmd, "peer_filter") == 0) {
  4111. u8 addr[ETH_ALEN];
  4112. if (hwaddr_aton(param, addr))
  4113. return -1;
  4114. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  4115. return 0;
  4116. }
  4117. if (os_strcmp(cmd, "cross_connect") == 0)
  4118. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  4119. if (os_strcmp(cmd, "go_apsd") == 0) {
  4120. if (os_strcmp(param, "disable") == 0)
  4121. wpa_s->set_ap_uapsd = 0;
  4122. else {
  4123. wpa_s->set_ap_uapsd = 1;
  4124. wpa_s->ap_uapsd = atoi(param);
  4125. }
  4126. return 0;
  4127. }
  4128. if (os_strcmp(cmd, "client_apsd") == 0) {
  4129. if (os_strcmp(param, "disable") == 0)
  4130. wpa_s->set_sta_uapsd = 0;
  4131. else {
  4132. int be, bk, vi, vo;
  4133. char *pos;
  4134. /* format: BE,BK,VI,VO;max SP Length */
  4135. be = atoi(param);
  4136. pos = os_strchr(param, ',');
  4137. if (pos == NULL)
  4138. return -1;
  4139. pos++;
  4140. bk = atoi(pos);
  4141. pos = os_strchr(pos, ',');
  4142. if (pos == NULL)
  4143. return -1;
  4144. pos++;
  4145. vi = atoi(pos);
  4146. pos = os_strchr(pos, ',');
  4147. if (pos == NULL)
  4148. return -1;
  4149. pos++;
  4150. vo = atoi(pos);
  4151. /* ignore max SP Length for now */
  4152. wpa_s->set_sta_uapsd = 1;
  4153. wpa_s->sta_uapsd = 0;
  4154. if (be)
  4155. wpa_s->sta_uapsd |= BIT(0);
  4156. if (bk)
  4157. wpa_s->sta_uapsd |= BIT(1);
  4158. if (vi)
  4159. wpa_s->sta_uapsd |= BIT(2);
  4160. if (vo)
  4161. wpa_s->sta_uapsd |= BIT(3);
  4162. }
  4163. return 0;
  4164. }
  4165. if (os_strcmp(cmd, "disallow_freq") == 0)
  4166. return p2p_ctrl_disallow_freq(wpa_s, param);
  4167. if (os_strcmp(cmd, "disc_int") == 0) {
  4168. int min_disc_int, max_disc_int, max_disc_tu;
  4169. char *pos;
  4170. pos = param;
  4171. min_disc_int = atoi(pos);
  4172. pos = os_strchr(pos, ' ');
  4173. if (pos == NULL)
  4174. return -1;
  4175. *pos++ = '\0';
  4176. max_disc_int = atoi(pos);
  4177. pos = os_strchr(pos, ' ');
  4178. if (pos == NULL)
  4179. return -1;
  4180. *pos++ = '\0';
  4181. max_disc_tu = atoi(pos);
  4182. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  4183. max_disc_int, max_disc_tu);
  4184. }
  4185. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  4186. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  4187. return 0;
  4188. }
  4189. #ifdef CONFIG_WPS_NFC
  4190. if (os_strcmp(cmd, "nfc_tag") == 0)
  4191. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  4192. #endif /* CONFIG_WPS_NFC */
  4193. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  4194. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  4195. return 0;
  4196. }
  4197. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  4198. cmd);
  4199. return -1;
  4200. }
  4201. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  4202. {
  4203. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4204. wpa_s->force_long_sd = 0;
  4205. if (wpa_s->global->p2p)
  4206. p2p_flush(wpa_s->global->p2p);
  4207. }
  4208. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  4209. {
  4210. char *pos, *pos2;
  4211. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  4212. if (cmd[0]) {
  4213. pos = os_strchr(cmd, ' ');
  4214. if (pos == NULL)
  4215. return -1;
  4216. *pos++ = '\0';
  4217. dur1 = atoi(cmd);
  4218. pos2 = os_strchr(pos, ' ');
  4219. if (pos2)
  4220. *pos2++ = '\0';
  4221. int1 = atoi(pos);
  4222. } else
  4223. pos2 = NULL;
  4224. if (pos2) {
  4225. pos = os_strchr(pos2, ' ');
  4226. if (pos == NULL)
  4227. return -1;
  4228. *pos++ = '\0';
  4229. dur2 = atoi(pos2);
  4230. int2 = atoi(pos);
  4231. }
  4232. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  4233. }
  4234. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4235. {
  4236. char *pos;
  4237. unsigned int period = 0, interval = 0;
  4238. if (cmd[0]) {
  4239. pos = os_strchr(cmd, ' ');
  4240. if (pos == NULL)
  4241. return -1;
  4242. *pos++ = '\0';
  4243. period = atoi(cmd);
  4244. interval = atoi(pos);
  4245. }
  4246. return wpas_p2p_ext_listen(wpa_s, period, interval);
  4247. }
  4248. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  4249. {
  4250. const char *pos;
  4251. u8 peer[ETH_ALEN];
  4252. int iface_addr = 0;
  4253. pos = cmd;
  4254. if (os_strncmp(pos, "iface=", 6) == 0) {
  4255. iface_addr = 1;
  4256. pos += 6;
  4257. }
  4258. if (hwaddr_aton(pos, peer))
  4259. return -1;
  4260. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  4261. return 0;
  4262. }
  4263. #endif /* CONFIG_P2P */
  4264. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  4265. {
  4266. struct wpa_freq_range_list ranges;
  4267. int *freqs = NULL;
  4268. struct hostapd_hw_modes *mode;
  4269. u16 i;
  4270. if (wpa_s->hw.modes == NULL)
  4271. return NULL;
  4272. os_memset(&ranges, 0, sizeof(ranges));
  4273. if (freq_range_list_parse(&ranges, val) < 0)
  4274. return NULL;
  4275. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4276. int j;
  4277. mode = &wpa_s->hw.modes[i];
  4278. for (j = 0; j < mode->num_channels; j++) {
  4279. unsigned int freq;
  4280. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4281. continue;
  4282. freq = mode->channels[j].freq;
  4283. if (!freq_range_list_includes(&ranges, freq))
  4284. continue;
  4285. int_array_add_unique(&freqs, freq);
  4286. }
  4287. }
  4288. os_free(ranges.range);
  4289. return freqs;
  4290. }
  4291. #ifdef CONFIG_INTERWORKING
  4292. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  4293. {
  4294. int auto_sel = 0;
  4295. int *freqs = NULL;
  4296. if (param) {
  4297. char *pos;
  4298. auto_sel = os_strstr(param, "auto") != NULL;
  4299. pos = os_strstr(param, "freq=");
  4300. if (pos) {
  4301. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4302. if (freqs == NULL)
  4303. return -1;
  4304. }
  4305. }
  4306. return interworking_select(wpa_s, auto_sel, freqs);
  4307. }
  4308. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4309. {
  4310. u8 bssid[ETH_ALEN];
  4311. struct wpa_bss *bss;
  4312. if (hwaddr_aton(dst, bssid)) {
  4313. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4314. return -1;
  4315. }
  4316. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4317. if (bss == NULL) {
  4318. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4319. MAC2STR(bssid));
  4320. return -1;
  4321. }
  4322. return interworking_connect(wpa_s, bss);
  4323. }
  4324. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4325. {
  4326. u8 dst_addr[ETH_ALEN];
  4327. int used;
  4328. char *pos;
  4329. #define MAX_ANQP_INFO_ID 100
  4330. u16 id[MAX_ANQP_INFO_ID];
  4331. size_t num_id = 0;
  4332. u32 subtypes = 0;
  4333. used = hwaddr_aton2(dst, dst_addr);
  4334. if (used < 0)
  4335. return -1;
  4336. pos = dst + used;
  4337. while (num_id < MAX_ANQP_INFO_ID) {
  4338. if (os_strncmp(pos, "hs20:", 5) == 0) {
  4339. #ifdef CONFIG_HS20
  4340. int num = atoi(pos + 5);
  4341. if (num <= 0 || num > 31)
  4342. return -1;
  4343. subtypes |= BIT(num);
  4344. #else /* CONFIG_HS20 */
  4345. return -1;
  4346. #endif /* CONFIG_HS20 */
  4347. } else {
  4348. id[num_id] = atoi(pos);
  4349. if (id[num_id])
  4350. num_id++;
  4351. }
  4352. pos = os_strchr(pos + 1, ',');
  4353. if (pos == NULL)
  4354. break;
  4355. pos++;
  4356. }
  4357. if (num_id == 0)
  4358. return -1;
  4359. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  4360. }
  4361. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4362. {
  4363. u8 dst_addr[ETH_ALEN];
  4364. struct wpabuf *advproto, *query = NULL;
  4365. int used, ret = -1;
  4366. char *pos, *end;
  4367. size_t len;
  4368. used = hwaddr_aton2(cmd, dst_addr);
  4369. if (used < 0)
  4370. return -1;
  4371. pos = cmd + used;
  4372. while (*pos == ' ')
  4373. pos++;
  4374. /* Advertisement Protocol ID */
  4375. end = os_strchr(pos, ' ');
  4376. if (end)
  4377. len = end - pos;
  4378. else
  4379. len = os_strlen(pos);
  4380. if (len & 0x01)
  4381. return -1;
  4382. len /= 2;
  4383. if (len == 0)
  4384. return -1;
  4385. advproto = wpabuf_alloc(len);
  4386. if (advproto == NULL)
  4387. return -1;
  4388. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4389. goto fail;
  4390. if (end) {
  4391. /* Optional Query Request */
  4392. pos = end + 1;
  4393. while (*pos == ' ')
  4394. pos++;
  4395. len = os_strlen(pos);
  4396. if (len) {
  4397. if (len & 0x01)
  4398. goto fail;
  4399. len /= 2;
  4400. if (len == 0)
  4401. goto fail;
  4402. query = wpabuf_alloc(len);
  4403. if (query == NULL)
  4404. goto fail;
  4405. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4406. goto fail;
  4407. }
  4408. }
  4409. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4410. fail:
  4411. wpabuf_free(advproto);
  4412. wpabuf_free(query);
  4413. return ret;
  4414. }
  4415. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4416. size_t buflen)
  4417. {
  4418. u8 addr[ETH_ALEN];
  4419. int dialog_token;
  4420. int used;
  4421. char *pos;
  4422. size_t resp_len, start, requested_len;
  4423. struct wpabuf *resp;
  4424. int ret;
  4425. used = hwaddr_aton2(cmd, addr);
  4426. if (used < 0)
  4427. return -1;
  4428. pos = cmd + used;
  4429. while (*pos == ' ')
  4430. pos++;
  4431. dialog_token = atoi(pos);
  4432. if (wpa_s->last_gas_resp &&
  4433. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4434. dialog_token == wpa_s->last_gas_dialog_token)
  4435. resp = wpa_s->last_gas_resp;
  4436. else if (wpa_s->prev_gas_resp &&
  4437. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4438. dialog_token == wpa_s->prev_gas_dialog_token)
  4439. resp = wpa_s->prev_gas_resp;
  4440. else
  4441. return -1;
  4442. resp_len = wpabuf_len(resp);
  4443. start = 0;
  4444. requested_len = resp_len;
  4445. pos = os_strchr(pos, ' ');
  4446. if (pos) {
  4447. start = atoi(pos);
  4448. if (start > resp_len)
  4449. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4450. pos = os_strchr(pos, ',');
  4451. if (pos == NULL)
  4452. return -1;
  4453. pos++;
  4454. requested_len = atoi(pos);
  4455. if (start + requested_len > resp_len)
  4456. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4457. }
  4458. if (requested_len * 2 + 1 > buflen)
  4459. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4460. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4461. requested_len);
  4462. if (start + requested_len == resp_len) {
  4463. /*
  4464. * Free memory by dropping the response after it has been
  4465. * fetched.
  4466. */
  4467. if (resp == wpa_s->prev_gas_resp) {
  4468. wpabuf_free(wpa_s->prev_gas_resp);
  4469. wpa_s->prev_gas_resp = NULL;
  4470. } else {
  4471. wpabuf_free(wpa_s->last_gas_resp);
  4472. wpa_s->last_gas_resp = NULL;
  4473. }
  4474. }
  4475. return ret;
  4476. }
  4477. #endif /* CONFIG_INTERWORKING */
  4478. #ifdef CONFIG_HS20
  4479. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4480. {
  4481. u8 dst_addr[ETH_ALEN];
  4482. int used;
  4483. char *pos;
  4484. u32 subtypes = 0;
  4485. used = hwaddr_aton2(dst, dst_addr);
  4486. if (used < 0)
  4487. return -1;
  4488. pos = dst + used;
  4489. for (;;) {
  4490. int num = atoi(pos);
  4491. if (num <= 0 || num > 31)
  4492. return -1;
  4493. subtypes |= BIT(num);
  4494. pos = os_strchr(pos + 1, ',');
  4495. if (pos == NULL)
  4496. break;
  4497. pos++;
  4498. }
  4499. if (subtypes == 0)
  4500. return -1;
  4501. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4502. }
  4503. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4504. const u8 *addr, const char *realm)
  4505. {
  4506. u8 *buf;
  4507. size_t rlen, len;
  4508. int ret;
  4509. rlen = os_strlen(realm);
  4510. len = 3 + rlen;
  4511. buf = os_malloc(len);
  4512. if (buf == NULL)
  4513. return -1;
  4514. buf[0] = 1; /* NAI Home Realm Count */
  4515. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4516. buf[2] = rlen;
  4517. os_memcpy(buf + 3, realm, rlen);
  4518. ret = hs20_anqp_send_req(wpa_s, addr,
  4519. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4520. buf, len);
  4521. os_free(buf);
  4522. return ret;
  4523. }
  4524. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4525. char *dst)
  4526. {
  4527. struct wpa_cred *cred = wpa_s->conf->cred;
  4528. u8 dst_addr[ETH_ALEN];
  4529. int used;
  4530. u8 *buf;
  4531. size_t len;
  4532. int ret;
  4533. used = hwaddr_aton2(dst, dst_addr);
  4534. if (used < 0)
  4535. return -1;
  4536. while (dst[used] == ' ')
  4537. used++;
  4538. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4539. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4540. dst + used + 6);
  4541. len = os_strlen(dst + used);
  4542. if (len == 0 && cred && cred->realm)
  4543. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4544. if (len % 1)
  4545. return -1;
  4546. len /= 2;
  4547. buf = os_malloc(len);
  4548. if (buf == NULL)
  4549. return -1;
  4550. if (hexstr2bin(dst + used, buf, len) < 0) {
  4551. os_free(buf);
  4552. return -1;
  4553. }
  4554. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4555. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4556. buf, len);
  4557. os_free(buf);
  4558. return ret;
  4559. }
  4560. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
  4561. {
  4562. u8 dst_addr[ETH_ALEN];
  4563. int used;
  4564. char *icon;
  4565. used = hwaddr_aton2(cmd, dst_addr);
  4566. if (used < 0)
  4567. return -1;
  4568. while (cmd[used] == ' ')
  4569. used++;
  4570. icon = &cmd[used];
  4571. wpa_s->fetch_osu_icon_in_progress = 0;
  4572. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  4573. (u8 *) icon, os_strlen(icon));
  4574. }
  4575. #endif /* CONFIG_HS20 */
  4576. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4577. struct wpa_supplicant *wpa_s, char *cmd)
  4578. {
  4579. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4580. return 0;
  4581. }
  4582. #ifdef CONFIG_AUTOSCAN
  4583. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4584. char *cmd)
  4585. {
  4586. enum wpa_states state = wpa_s->wpa_state;
  4587. char *new_params = NULL;
  4588. if (os_strlen(cmd) > 0) {
  4589. new_params = os_strdup(cmd);
  4590. if (new_params == NULL)
  4591. return -1;
  4592. }
  4593. os_free(wpa_s->conf->autoscan);
  4594. wpa_s->conf->autoscan = new_params;
  4595. if (wpa_s->conf->autoscan == NULL)
  4596. autoscan_deinit(wpa_s);
  4597. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4598. autoscan_init(wpa_s, 1);
  4599. else if (state == WPA_SCANNING)
  4600. wpa_supplicant_reinit_autoscan(wpa_s);
  4601. return 0;
  4602. }
  4603. #endif /* CONFIG_AUTOSCAN */
  4604. #ifdef CONFIG_WNM
  4605. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4606. {
  4607. int enter;
  4608. int intval = 0;
  4609. char *pos;
  4610. int ret;
  4611. struct wpabuf *tfs_req = NULL;
  4612. if (os_strncmp(cmd, "enter", 5) == 0)
  4613. enter = 1;
  4614. else if (os_strncmp(cmd, "exit", 4) == 0)
  4615. enter = 0;
  4616. else
  4617. return -1;
  4618. pos = os_strstr(cmd, " interval=");
  4619. if (pos)
  4620. intval = atoi(pos + 10);
  4621. pos = os_strstr(cmd, " tfs_req=");
  4622. if (pos) {
  4623. char *end;
  4624. size_t len;
  4625. pos += 9;
  4626. end = os_strchr(pos, ' ');
  4627. if (end)
  4628. len = end - pos;
  4629. else
  4630. len = os_strlen(pos);
  4631. if (len & 1)
  4632. return -1;
  4633. len /= 2;
  4634. tfs_req = wpabuf_alloc(len);
  4635. if (tfs_req == NULL)
  4636. return -1;
  4637. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4638. wpabuf_free(tfs_req);
  4639. return -1;
  4640. }
  4641. }
  4642. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4643. WNM_SLEEP_MODE_EXIT, intval,
  4644. tfs_req);
  4645. wpabuf_free(tfs_req);
  4646. return ret;
  4647. }
  4648. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4649. {
  4650. int query_reason;
  4651. query_reason = atoi(cmd);
  4652. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4653. query_reason);
  4654. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4655. }
  4656. #endif /* CONFIG_WNM */
  4657. /* Get string representation of channel width */
  4658. static const char * channel_width_name(enum chan_width width)
  4659. {
  4660. switch (width) {
  4661. case CHAN_WIDTH_20_NOHT:
  4662. return "20 MHz (no HT)";
  4663. case CHAN_WIDTH_20:
  4664. return "20 MHz";
  4665. case CHAN_WIDTH_40:
  4666. return "40 MHz";
  4667. case CHAN_WIDTH_80:
  4668. return "80 MHz";
  4669. case CHAN_WIDTH_80P80:
  4670. return "80+80 MHz";
  4671. case CHAN_WIDTH_160:
  4672. return "160 MHz";
  4673. default:
  4674. return "unknown";
  4675. }
  4676. }
  4677. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4678. size_t buflen)
  4679. {
  4680. struct wpa_signal_info si;
  4681. int ret;
  4682. char *pos, *end;
  4683. ret = wpa_drv_signal_poll(wpa_s, &si);
  4684. if (ret)
  4685. return -1;
  4686. pos = buf;
  4687. end = buf + buflen;
  4688. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4689. "NOISE=%d\nFREQUENCY=%u\n",
  4690. si.current_signal, si.current_txrate / 1000,
  4691. si.current_noise, si.frequency);
  4692. if (ret < 0 || ret > end - pos)
  4693. return -1;
  4694. pos += ret;
  4695. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4696. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4697. channel_width_name(si.chanwidth));
  4698. if (ret < 0 || ret > end - pos)
  4699. return -1;
  4700. pos += ret;
  4701. }
  4702. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4703. ret = os_snprintf(pos, end - pos,
  4704. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4705. si.center_frq1, si.center_frq2);
  4706. if (ret < 0 || ret > end - pos)
  4707. return -1;
  4708. pos += ret;
  4709. }
  4710. if (si.avg_signal) {
  4711. ret = os_snprintf(pos, end - pos,
  4712. "AVG_RSSI=%d\n", si.avg_signal);
  4713. if (ret < 0 || ret >= end - pos)
  4714. return -1;
  4715. pos += ret;
  4716. }
  4717. return pos - buf;
  4718. }
  4719. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4720. size_t buflen)
  4721. {
  4722. struct hostap_sta_driver_data sta;
  4723. int ret;
  4724. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4725. if (ret)
  4726. return -1;
  4727. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4728. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4729. if (ret < 0 || (size_t) ret > buflen)
  4730. return -1;
  4731. return ret;
  4732. }
  4733. #ifdef ANDROID
  4734. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4735. char *buf, size_t buflen)
  4736. {
  4737. int ret;
  4738. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4739. if (ret == 0) {
  4740. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4741. struct p2p_data *p2p = wpa_s->global->p2p;
  4742. if (p2p) {
  4743. char country[3];
  4744. country[0] = cmd[8];
  4745. country[1] = cmd[9];
  4746. country[2] = 0x04;
  4747. p2p_set_country(p2p, country);
  4748. }
  4749. }
  4750. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4751. }
  4752. return ret;
  4753. }
  4754. #endif /* ANDROID */
  4755. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4756. char *buf, size_t buflen)
  4757. {
  4758. int ret;
  4759. char *pos;
  4760. u8 *data = NULL;
  4761. unsigned int vendor_id, subcmd;
  4762. struct wpabuf *reply;
  4763. size_t data_len = 0;
  4764. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  4765. vendor_id = strtoul(cmd, &pos, 16);
  4766. if (!isblank(*pos))
  4767. return -EINVAL;
  4768. subcmd = strtoul(pos, &pos, 10);
  4769. if (*pos != '\0') {
  4770. if (!isblank(*pos++))
  4771. return -EINVAL;
  4772. data_len = os_strlen(pos);
  4773. }
  4774. if (data_len) {
  4775. data_len /= 2;
  4776. data = os_malloc(data_len);
  4777. if (!data)
  4778. return -1;
  4779. if (hexstr2bin(pos, data, data_len)) {
  4780. wpa_printf(MSG_DEBUG,
  4781. "Vendor command: wrong parameter format");
  4782. os_free(data);
  4783. return -EINVAL;
  4784. }
  4785. }
  4786. reply = wpabuf_alloc((buflen - 1) / 2);
  4787. if (!reply) {
  4788. os_free(data);
  4789. return -1;
  4790. }
  4791. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  4792. reply);
  4793. if (ret == 0)
  4794. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  4795. wpabuf_len(reply));
  4796. wpabuf_free(reply);
  4797. os_free(data);
  4798. return ret;
  4799. }
  4800. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4801. {
  4802. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4803. #ifdef CONFIG_P2P
  4804. wpas_p2p_cancel(wpa_s);
  4805. wpas_p2p_stop_find(wpa_s);
  4806. p2p_ctrl_flush(wpa_s);
  4807. wpas_p2p_group_remove(wpa_s, "*");
  4808. wpas_p2p_service_flush(wpa_s);
  4809. wpa_s->global->p2p_disabled = 0;
  4810. wpa_s->global->p2p_per_sta_psk = 0;
  4811. wpa_s->conf->num_sec_device_types = 0;
  4812. wpa_s->p2p_disable_ip_addr_req = 0;
  4813. os_free(wpa_s->global->p2p_go_avoid_freq.range);
  4814. wpa_s->global->p2p_go_avoid_freq.range = NULL;
  4815. #endif /* CONFIG_P2P */
  4816. #ifdef CONFIG_WPS_TESTING
  4817. wps_version_number = 0x20;
  4818. wps_testing_dummy_cred = 0;
  4819. wps_corrupt_pkhash = 0;
  4820. #endif /* CONFIG_WPS_TESTING */
  4821. #ifdef CONFIG_WPS
  4822. wpa_s->wps_fragment_size = 0;
  4823. wpas_wps_cancel(wpa_s);
  4824. #endif /* CONFIG_WPS */
  4825. wpa_s->after_wps = 0;
  4826. wpa_s->known_wps_freq = 0;
  4827. #ifdef CONFIG_TDLS
  4828. #ifdef CONFIG_TDLS_TESTING
  4829. extern unsigned int tdls_testing;
  4830. tdls_testing = 0;
  4831. #endif /* CONFIG_TDLS_TESTING */
  4832. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4833. wpa_tdls_enable(wpa_s->wpa, 1);
  4834. #endif /* CONFIG_TDLS */
  4835. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4836. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4837. wpa_s->no_keep_alive = 0;
  4838. os_free(wpa_s->disallow_aps_bssid);
  4839. wpa_s->disallow_aps_bssid = NULL;
  4840. wpa_s->disallow_aps_bssid_count = 0;
  4841. os_free(wpa_s->disallow_aps_ssid);
  4842. wpa_s->disallow_aps_ssid = NULL;
  4843. wpa_s->disallow_aps_ssid_count = 0;
  4844. wpa_s->set_sta_uapsd = 0;
  4845. wpa_s->sta_uapsd = 0;
  4846. wpa_drv_radio_disable(wpa_s, 0);
  4847. wpa_bss_flush(wpa_s);
  4848. wpa_blacklist_clear(wpa_s);
  4849. wpa_s->extra_blacklist_count = 0;
  4850. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4851. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4852. wpa_config_flush_blobs(wpa_s->conf);
  4853. wpa_s->conf->auto_interworking = 0;
  4854. wpa_s->conf->okc = 0;
  4855. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  4856. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  4857. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  4858. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4859. radio_remove_works(wpa_s, NULL, 1);
  4860. wpa_s->next_ssid = NULL;
  4861. #ifdef CONFIG_INTERWORKING
  4862. hs20_cancel_fetch_osu(wpa_s);
  4863. #endif /* CONFIG_INTERWORKING */
  4864. wpa_s->ext_mgmt_frame_handling = 0;
  4865. }
  4866. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  4867. char *buf, size_t buflen)
  4868. {
  4869. struct wpa_radio_work *work;
  4870. char *pos, *end;
  4871. struct os_reltime now, diff;
  4872. pos = buf;
  4873. end = buf + buflen;
  4874. os_get_reltime(&now);
  4875. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4876. {
  4877. int ret;
  4878. os_reltime_sub(&now, &work->time, &diff);
  4879. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  4880. work->type, work->wpa_s->ifname, work->freq,
  4881. work->started, diff.sec, diff.usec);
  4882. if (ret < 0 || ret >= end - pos)
  4883. break;
  4884. pos += ret;
  4885. }
  4886. return pos - buf;
  4887. }
  4888. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  4889. {
  4890. struct wpa_radio_work *work = eloop_ctx;
  4891. struct wpa_external_work *ework = work->ctx;
  4892. wpa_dbg(work->wpa_s, MSG_DEBUG,
  4893. "Timing out external radio work %u (%s)",
  4894. ework->id, work->type);
  4895. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  4896. radio_work_done(work);
  4897. os_free(ework);
  4898. }
  4899. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  4900. {
  4901. struct wpa_external_work *ework = work->ctx;
  4902. if (deinit) {
  4903. if (work->started)
  4904. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  4905. work, NULL);
  4906. os_free(ework);
  4907. return;
  4908. }
  4909. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  4910. ework->id, ework->type);
  4911. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  4912. if (!ework->timeout)
  4913. ework->timeout = 10;
  4914. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  4915. work, NULL);
  4916. }
  4917. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  4918. char *buf, size_t buflen)
  4919. {
  4920. struct wpa_external_work *ework;
  4921. char *pos, *pos2;
  4922. size_t type_len;
  4923. int ret;
  4924. unsigned int freq = 0;
  4925. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  4926. ework = os_zalloc(sizeof(*ework));
  4927. if (ework == NULL)
  4928. return -1;
  4929. pos = os_strchr(cmd, ' ');
  4930. if (pos) {
  4931. type_len = pos - cmd;
  4932. pos++;
  4933. pos2 = os_strstr(pos, "freq=");
  4934. if (pos2)
  4935. freq = atoi(pos2 + 5);
  4936. pos2 = os_strstr(pos, "timeout=");
  4937. if (pos2)
  4938. ework->timeout = atoi(pos2 + 8);
  4939. } else {
  4940. type_len = os_strlen(cmd);
  4941. }
  4942. if (4 + type_len >= sizeof(ework->type))
  4943. type_len = sizeof(ework->type) - 4 - 1;
  4944. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  4945. os_memcpy(ework->type + 4, cmd, type_len);
  4946. ework->type[4 + type_len] = '\0';
  4947. wpa_s->ext_work_id++;
  4948. if (wpa_s->ext_work_id == 0)
  4949. wpa_s->ext_work_id++;
  4950. ework->id = wpa_s->ext_work_id;
  4951. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  4952. ework) < 0) {
  4953. os_free(ework);
  4954. return -1;
  4955. }
  4956. ret = os_snprintf(buf, buflen, "%u", ework->id);
  4957. if (ret < 0 || (size_t) ret >= buflen)
  4958. return -1;
  4959. return ret;
  4960. }
  4961. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  4962. {
  4963. struct wpa_radio_work *work;
  4964. unsigned int id = atoi(cmd);
  4965. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4966. {
  4967. struct wpa_external_work *ework;
  4968. if (os_strncmp(work->type, "ext:", 4) != 0)
  4969. continue;
  4970. ework = work->ctx;
  4971. if (id && ework->id != id)
  4972. continue;
  4973. wpa_dbg(wpa_s, MSG_DEBUG,
  4974. "Completed external radio work %u (%s)",
  4975. ework->id, ework->type);
  4976. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  4977. radio_work_done(work);
  4978. os_free(ework);
  4979. return 3; /* "OK\n" */
  4980. }
  4981. return -1;
  4982. }
  4983. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  4984. char *buf, size_t buflen)
  4985. {
  4986. if (os_strcmp(cmd, "show") == 0)
  4987. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  4988. if (os_strncmp(cmd, "add ", 4) == 0)
  4989. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  4990. if (os_strncmp(cmd, "done ", 5) == 0)
  4991. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  4992. return -1;
  4993. }
  4994. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  4995. {
  4996. struct wpa_radio_work *work, *tmp;
  4997. if (!wpa_s || !wpa_s->radio)
  4998. return;
  4999. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  5000. struct wpa_radio_work, list) {
  5001. struct wpa_external_work *ework;
  5002. if (os_strncmp(work->type, "ext:", 4) != 0)
  5003. continue;
  5004. ework = work->ctx;
  5005. wpa_dbg(wpa_s, MSG_DEBUG,
  5006. "Flushing%s external radio work %u (%s)",
  5007. work->started ? " started" : "", ework->id,
  5008. ework->type);
  5009. if (work->started)
  5010. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5011. work, NULL);
  5012. radio_work_done(work);
  5013. os_free(ework);
  5014. }
  5015. }
  5016. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  5017. {
  5018. struct wpa_supplicant *wpa_s = eloop_ctx;
  5019. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  5020. }
  5021. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  5022. {
  5023. int *freqs = NULL;
  5024. freqs = freq_range_to_channel_list(wpa_s, val);
  5025. if (freqs == NULL)
  5026. return -1;
  5027. os_free(wpa_s->manual_scan_freqs);
  5028. wpa_s->manual_scan_freqs = freqs;
  5029. return 0;
  5030. }
  5031. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  5032. char *reply, int reply_size, int *reply_len)
  5033. {
  5034. char *pos;
  5035. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  5036. *reply_len = -1;
  5037. return;
  5038. }
  5039. wpa_s->manual_scan_passive = 0;
  5040. wpa_s->manual_scan_use_id = 0;
  5041. wpa_s->manual_scan_only_new = 0;
  5042. if (params) {
  5043. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  5044. wpa_s->scan_res_handler = scan_only_handler;
  5045. pos = os_strstr(params, "freq=");
  5046. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  5047. *reply_len = -1;
  5048. return;
  5049. }
  5050. pos = os_strstr(params, "passive=");
  5051. if (pos)
  5052. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  5053. pos = os_strstr(params, "use_id=");
  5054. if (pos)
  5055. wpa_s->manual_scan_use_id = atoi(pos + 7);
  5056. pos = os_strstr(params, "only_new=1");
  5057. if (pos)
  5058. wpa_s->manual_scan_only_new = 1;
  5059. } else {
  5060. os_free(wpa_s->manual_scan_freqs);
  5061. wpa_s->manual_scan_freqs = NULL;
  5062. if (wpa_s->scan_res_handler == scan_only_handler)
  5063. wpa_s->scan_res_handler = NULL;
  5064. }
  5065. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  5066. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  5067. (wpa_s->wpa_state == WPA_COMPLETED))) {
  5068. wpa_s->normal_scans = 0;
  5069. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5070. wpa_s->after_wps = 0;
  5071. wpa_s->known_wps_freq = 0;
  5072. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5073. if (wpa_s->manual_scan_use_id) {
  5074. wpa_s->manual_scan_id++;
  5075. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5076. wpa_s->manual_scan_id);
  5077. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5078. wpa_s->manual_scan_id);
  5079. }
  5080. } else if (wpa_s->sched_scanning) {
  5081. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  5082. wpa_supplicant_cancel_sched_scan(wpa_s);
  5083. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5084. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5085. if (wpa_s->manual_scan_use_id) {
  5086. wpa_s->manual_scan_id++;
  5087. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5088. wpa_s->manual_scan_id);
  5089. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5090. wpa_s->manual_scan_id);
  5091. }
  5092. } else {
  5093. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  5094. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  5095. }
  5096. }
  5097. #ifdef CONFIG_TESTING_OPTIONS
  5098. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  5099. unsigned int freq, const u8 *dst,
  5100. const u8 *src, const u8 *bssid,
  5101. const u8 *data, size_t data_len,
  5102. enum offchannel_send_action_result
  5103. result)
  5104. {
  5105. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  5106. " src=" MACSTR " bssid=" MACSTR " result=%s",
  5107. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  5108. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  5109. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  5110. "NO_ACK" : "FAILED"));
  5111. }
  5112. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5113. {
  5114. char *pos, *param;
  5115. size_t len;
  5116. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  5117. int res, used;
  5118. int freq = 0, no_cck = 0, wait_time = 0;
  5119. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  5120. * <action=Action frame payload> */
  5121. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  5122. pos = cmd;
  5123. used = hwaddr_aton2(pos, da);
  5124. if (used < 0)
  5125. return -1;
  5126. pos += used;
  5127. while (*pos == ' ')
  5128. pos++;
  5129. used = hwaddr_aton2(pos, bssid);
  5130. if (used < 0)
  5131. return -1;
  5132. pos += used;
  5133. param = os_strstr(pos, " freq=");
  5134. if (param) {
  5135. param += 6;
  5136. freq = atoi(param);
  5137. }
  5138. param = os_strstr(pos, " no_cck=");
  5139. if (param) {
  5140. param += 8;
  5141. no_cck = atoi(param);
  5142. }
  5143. param = os_strstr(pos, " wait_time=");
  5144. if (param) {
  5145. param += 11;
  5146. wait_time = atoi(param);
  5147. }
  5148. param = os_strstr(pos, " action=");
  5149. if (param == NULL)
  5150. return -1;
  5151. param += 8;
  5152. len = os_strlen(param);
  5153. if (len & 1)
  5154. return -1;
  5155. len /= 2;
  5156. buf = os_malloc(len);
  5157. if (buf == NULL)
  5158. return -1;
  5159. if (hexstr2bin(param, buf, len) < 0) {
  5160. os_free(buf);
  5161. return -1;
  5162. }
  5163. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  5164. buf, len, wait_time,
  5165. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  5166. os_free(buf);
  5167. return res;
  5168. }
  5169. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  5170. {
  5171. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  5172. offchannel_send_action_done(wpa_s);
  5173. }
  5174. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  5175. {
  5176. char *pos, *param;
  5177. union wpa_event_data event;
  5178. enum wpa_event_type ev;
  5179. /* <event name> [parameters..] */
  5180. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  5181. pos = cmd;
  5182. param = os_strchr(pos, ' ');
  5183. if (param)
  5184. *param++ = '\0';
  5185. os_memset(&event, 0, sizeof(event));
  5186. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  5187. ev = EVENT_INTERFACE_ENABLED;
  5188. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  5189. ev = EVENT_INTERFACE_DISABLED;
  5190. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  5191. ev = EVENT_AVOID_FREQUENCIES;
  5192. if (param == NULL)
  5193. param = "";
  5194. if (freq_range_list_parse(&event.freq_range, param) < 0)
  5195. return -1;
  5196. wpa_supplicant_event(wpa_s, ev, &event);
  5197. os_free(event.freq_range.range);
  5198. return 0;
  5199. } else {
  5200. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  5201. cmd);
  5202. return -1;
  5203. }
  5204. wpa_supplicant_event(wpa_s, ev, &event);
  5205. return 0;
  5206. }
  5207. #endif /* CONFIG_TESTING_OPTIONS */
  5208. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  5209. char *buf, size_t *resp_len)
  5210. {
  5211. char *reply;
  5212. const int reply_size = 4096;
  5213. int reply_len;
  5214. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  5215. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5216. if (wpa_debug_show_keys)
  5217. wpa_dbg(wpa_s, MSG_DEBUG,
  5218. "Control interface command '%s'", buf);
  5219. else
  5220. wpa_dbg(wpa_s, MSG_DEBUG,
  5221. "Control interface command '%s [REMOVED]'",
  5222. os_strncmp(buf, WPA_CTRL_RSP,
  5223. os_strlen(WPA_CTRL_RSP)) == 0 ?
  5224. WPA_CTRL_RSP : "SET_NETWORK");
  5225. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  5226. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  5227. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  5228. (const u8 *) buf, os_strlen(buf));
  5229. } else {
  5230. int level = MSG_DEBUG;
  5231. if (os_strcmp(buf, "PING") == 0)
  5232. level = MSG_EXCESSIVE;
  5233. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  5234. }
  5235. reply = os_malloc(reply_size);
  5236. if (reply == NULL) {
  5237. *resp_len = 1;
  5238. return NULL;
  5239. }
  5240. os_memcpy(reply, "OK\n", 3);
  5241. reply_len = 3;
  5242. if (os_strcmp(buf, "PING") == 0) {
  5243. os_memcpy(reply, "PONG\n", 5);
  5244. reply_len = 5;
  5245. } else if (os_strcmp(buf, "IFNAME") == 0) {
  5246. reply_len = os_strlen(wpa_s->ifname);
  5247. os_memcpy(reply, wpa_s->ifname, reply_len);
  5248. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  5249. if (wpa_debug_reopen_file() < 0)
  5250. reply_len = -1;
  5251. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  5252. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  5253. } else if (os_strcmp(buf, "MIB") == 0) {
  5254. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  5255. if (reply_len >= 0) {
  5256. int res;
  5257. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  5258. reply_size - reply_len);
  5259. if (res < 0)
  5260. reply_len = -1;
  5261. else
  5262. reply_len += res;
  5263. }
  5264. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  5265. reply_len = wpa_supplicant_ctrl_iface_status(
  5266. wpa_s, buf + 6, reply, reply_size);
  5267. } else if (os_strcmp(buf, "PMKSA") == 0) {
  5268. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  5269. reply_size);
  5270. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5271. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  5272. reply_len = -1;
  5273. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  5274. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  5275. reply, reply_size);
  5276. } else if (os_strcmp(buf, "LOGON") == 0) {
  5277. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5278. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  5279. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  5280. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  5281. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5282. reply_len = -1;
  5283. else
  5284. wpas_request_connection(wpa_s);
  5285. } else if (os_strcmp(buf, "REATTACH") == 0) {
  5286. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  5287. !wpa_s->current_ssid)
  5288. reply_len = -1;
  5289. else {
  5290. wpa_s->reattach = 1;
  5291. wpas_request_connection(wpa_s);
  5292. }
  5293. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  5294. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5295. reply_len = -1;
  5296. else if (wpa_s->disconnected)
  5297. wpas_request_connection(wpa_s);
  5298. #ifdef IEEE8021X_EAPOL
  5299. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  5300. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  5301. reply_len = -1;
  5302. #endif /* IEEE8021X_EAPOL */
  5303. #ifdef CONFIG_PEERKEY
  5304. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  5305. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  5306. reply_len = -1;
  5307. #endif /* CONFIG_PEERKEY */
  5308. #ifdef CONFIG_IEEE80211R
  5309. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  5310. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  5311. reply_len = -1;
  5312. #endif /* CONFIG_IEEE80211R */
  5313. #ifdef CONFIG_WPS
  5314. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  5315. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  5316. if (res == -2) {
  5317. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5318. reply_len = 17;
  5319. } else if (res)
  5320. reply_len = -1;
  5321. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  5322. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  5323. if (res == -2) {
  5324. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5325. reply_len = 17;
  5326. } else if (res)
  5327. reply_len = -1;
  5328. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  5329. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  5330. reply,
  5331. reply_size);
  5332. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  5333. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  5334. wpa_s, buf + 14, reply, reply_size);
  5335. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  5336. if (wpas_wps_cancel(wpa_s))
  5337. reply_len = -1;
  5338. #ifdef CONFIG_WPS_NFC
  5339. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  5340. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  5341. reply_len = -1;
  5342. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  5343. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  5344. reply_len = -1;
  5345. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  5346. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  5347. wpa_s, buf + 21, reply, reply_size);
  5348. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  5349. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  5350. wpa_s, buf + 14, reply, reply_size);
  5351. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  5352. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  5353. buf + 17))
  5354. reply_len = -1;
  5355. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  5356. reply_len = wpas_ctrl_nfc_get_handover_req(
  5357. wpa_s, buf + 21, reply, reply_size);
  5358. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  5359. reply_len = wpas_ctrl_nfc_get_handover_sel(
  5360. wpa_s, buf + 21, reply, reply_size);
  5361. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  5362. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  5363. reply_len = -1;
  5364. #endif /* CONFIG_WPS_NFC */
  5365. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  5366. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  5367. reply_len = -1;
  5368. #ifdef CONFIG_AP
  5369. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  5370. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  5371. wpa_s, buf + 11, reply, reply_size);
  5372. #endif /* CONFIG_AP */
  5373. #ifdef CONFIG_WPS_ER
  5374. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  5375. if (wpas_wps_er_start(wpa_s, NULL))
  5376. reply_len = -1;
  5377. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  5378. if (wpas_wps_er_start(wpa_s, buf + 13))
  5379. reply_len = -1;
  5380. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  5381. if (wpas_wps_er_stop(wpa_s))
  5382. reply_len = -1;
  5383. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  5384. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  5385. reply_len = -1;
  5386. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  5387. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  5388. if (ret == -2) {
  5389. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5390. reply_len = 17;
  5391. } else if (ret == -3) {
  5392. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  5393. reply_len = 18;
  5394. } else if (ret == -4) {
  5395. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  5396. reply_len = 20;
  5397. } else if (ret)
  5398. reply_len = -1;
  5399. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  5400. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  5401. reply_len = -1;
  5402. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  5403. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  5404. buf + 18))
  5405. reply_len = -1;
  5406. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  5407. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  5408. reply_len = -1;
  5409. #ifdef CONFIG_WPS_NFC
  5410. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  5411. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  5412. wpa_s, buf + 24, reply, reply_size);
  5413. #endif /* CONFIG_WPS_NFC */
  5414. #endif /* CONFIG_WPS_ER */
  5415. #endif /* CONFIG_WPS */
  5416. #ifdef CONFIG_IBSS_RSN
  5417. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  5418. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  5419. reply_len = -1;
  5420. #endif /* CONFIG_IBSS_RSN */
  5421. #ifdef CONFIG_P2P
  5422. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  5423. if (p2p_ctrl_find(wpa_s, buf + 9))
  5424. reply_len = -1;
  5425. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  5426. if (p2p_ctrl_find(wpa_s, ""))
  5427. reply_len = -1;
  5428. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  5429. wpas_p2p_stop_find(wpa_s);
  5430. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  5431. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  5432. reply_size);
  5433. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  5434. if (p2p_ctrl_listen(wpa_s, buf + 11))
  5435. reply_len = -1;
  5436. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  5437. if (p2p_ctrl_listen(wpa_s, ""))
  5438. reply_len = -1;
  5439. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  5440. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  5441. reply_len = -1;
  5442. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  5443. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  5444. reply_len = -1;
  5445. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  5446. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  5447. reply_len = -1;
  5448. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  5449. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  5450. reply_len = -1;
  5451. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  5452. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  5453. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  5454. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  5455. reply_size);
  5456. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  5457. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  5458. reply_len = -1;
  5459. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  5460. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  5461. reply_len = -1;
  5462. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  5463. wpas_p2p_sd_service_update(wpa_s);
  5464. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  5465. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  5466. reply_len = -1;
  5467. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  5468. wpas_p2p_service_flush(wpa_s);
  5469. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  5470. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  5471. reply_len = -1;
  5472. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  5473. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  5474. reply_len = -1;
  5475. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  5476. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  5477. reply_len = -1;
  5478. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  5479. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  5480. reply_len = -1;
  5481. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  5482. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  5483. reply_size);
  5484. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  5485. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  5486. reply_len = -1;
  5487. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  5488. p2p_ctrl_flush(wpa_s);
  5489. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  5490. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  5491. reply_len = -1;
  5492. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  5493. if (wpas_p2p_cancel(wpa_s))
  5494. reply_len = -1;
  5495. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  5496. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  5497. reply_len = -1;
  5498. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  5499. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  5500. reply_len = -1;
  5501. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  5502. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  5503. reply_len = -1;
  5504. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  5505. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  5506. reply_len = -1;
  5507. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  5508. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  5509. reply_len = -1;
  5510. #endif /* CONFIG_P2P */
  5511. #ifdef CONFIG_WIFI_DISPLAY
  5512. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  5513. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  5514. reply_len = -1;
  5515. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  5516. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  5517. reply, reply_size);
  5518. #endif /* CONFIG_WIFI_DISPLAY */
  5519. #ifdef CONFIG_INTERWORKING
  5520. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  5521. if (interworking_fetch_anqp(wpa_s) < 0)
  5522. reply_len = -1;
  5523. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  5524. interworking_stop_fetch_anqp(wpa_s);
  5525. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  5526. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  5527. reply_len = -1;
  5528. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  5529. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  5530. reply_len = -1;
  5531. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  5532. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  5533. reply_len = -1;
  5534. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  5535. if (get_anqp(wpa_s, buf + 9) < 0)
  5536. reply_len = -1;
  5537. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  5538. if (gas_request(wpa_s, buf + 12) < 0)
  5539. reply_len = -1;
  5540. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  5541. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  5542. reply_size);
  5543. #endif /* CONFIG_INTERWORKING */
  5544. #ifdef CONFIG_HS20
  5545. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  5546. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  5547. reply_len = -1;
  5548. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  5549. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  5550. reply_len = -1;
  5551. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  5552. if (hs20_icon_request(wpa_s, buf + 18) < 0)
  5553. reply_len = -1;
  5554. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  5555. if (hs20_fetch_osu(wpa_s) < 0)
  5556. reply_len = -1;
  5557. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  5558. hs20_cancel_fetch_osu(wpa_s);
  5559. #endif /* CONFIG_HS20 */
  5560. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  5561. {
  5562. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  5563. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  5564. reply_len = -1;
  5565. else {
  5566. /*
  5567. * Notify response from timeout to allow the control
  5568. * interface response to be sent first.
  5569. */
  5570. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  5571. wpa_s, NULL);
  5572. }
  5573. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  5574. if (wpa_supplicant_reload_configuration(wpa_s))
  5575. reply_len = -1;
  5576. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5577. wpa_supplicant_terminate_proc(wpa_s->global);
  5578. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  5579. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  5580. reply_len = -1;
  5581. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  5582. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  5583. wpa_s, buf + 9, reply, reply_size);
  5584. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  5585. reply_len = wpa_supplicant_ctrl_iface_log_level(
  5586. wpa_s, buf + 9, reply, reply_size);
  5587. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  5588. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  5589. wpa_s, reply, reply_size);
  5590. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  5591. #ifdef CONFIG_SME
  5592. wpa_s->sme.prev_bssid_set = 0;
  5593. #endif /* CONFIG_SME */
  5594. wpa_s->reassociate = 0;
  5595. wpa_s->disconnected = 1;
  5596. wpa_supplicant_cancel_sched_scan(wpa_s);
  5597. wpa_supplicant_cancel_scan(wpa_s);
  5598. wpa_supplicant_deauthenticate(wpa_s,
  5599. WLAN_REASON_DEAUTH_LEAVING);
  5600. } else if (os_strcmp(buf, "SCAN") == 0) {
  5601. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  5602. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  5603. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  5604. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  5605. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  5606. wpa_s, reply, reply_size);
  5607. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  5608. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  5609. reply_len = -1;
  5610. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  5611. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  5612. reply_len = -1;
  5613. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  5614. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  5615. reply_len = -1;
  5616. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  5617. reply_len = wpa_supplicant_ctrl_iface_add_network(
  5618. wpa_s, reply, reply_size);
  5619. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  5620. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  5621. reply_len = -1;
  5622. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5623. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  5624. reply_len = -1;
  5625. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  5626. reply_len = wpa_supplicant_ctrl_iface_get_network(
  5627. wpa_s, buf + 12, reply, reply_size);
  5628. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  5629. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
  5630. reply_len = -1;
  5631. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  5632. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  5633. wpa_s, reply, reply_size);
  5634. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  5635. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  5636. wpa_s, reply, reply_size);
  5637. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  5638. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  5639. reply_len = -1;
  5640. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  5641. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  5642. reply_len = -1;
  5643. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  5644. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  5645. reply,
  5646. reply_size);
  5647. #ifndef CONFIG_NO_CONFIG_WRITE
  5648. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5649. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  5650. reply_len = -1;
  5651. #endif /* CONFIG_NO_CONFIG_WRITE */
  5652. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  5653. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  5654. wpa_s, buf + 15, reply, reply_size);
  5655. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  5656. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  5657. reply_len = -1;
  5658. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  5659. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  5660. reply_len = -1;
  5661. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5662. reply_len = wpa_supplicant_global_iface_list(
  5663. wpa_s->global, reply, reply_size);
  5664. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5665. reply_len = wpa_supplicant_global_iface_interfaces(
  5666. wpa_s->global, reply, reply_size);
  5667. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  5668. reply_len = wpa_supplicant_ctrl_iface_bss(
  5669. wpa_s, buf + 4, reply, reply_size);
  5670. #ifdef CONFIG_AP
  5671. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  5672. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  5673. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  5674. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  5675. reply_size);
  5676. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  5677. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  5678. reply_size);
  5679. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  5680. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  5681. reply_len = -1;
  5682. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  5683. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  5684. reply_len = -1;
  5685. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  5686. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  5687. reply_len = -1;
  5688. #endif /* CONFIG_AP */
  5689. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5690. wpas_notify_suspend(wpa_s->global);
  5691. } else if (os_strcmp(buf, "RESUME") == 0) {
  5692. wpas_notify_resume(wpa_s->global);
  5693. #ifdef CONFIG_TESTING_OPTIONS
  5694. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  5695. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  5696. #endif /* CONFIG_TESTING_OPTIONS */
  5697. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  5698. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  5699. reply_len = -1;
  5700. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  5701. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  5702. reply_len = -1;
  5703. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  5704. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  5705. reply_len = -1;
  5706. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  5707. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  5708. buf + 17))
  5709. reply_len = -1;
  5710. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  5711. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  5712. reply_len = -1;
  5713. #ifdef CONFIG_TDLS
  5714. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  5715. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  5716. reply_len = -1;
  5717. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  5718. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  5719. reply_len = -1;
  5720. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  5721. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  5722. reply_len = -1;
  5723. #endif /* CONFIG_TDLS */
  5724. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  5725. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  5726. reply_size);
  5727. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  5728. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  5729. reply_size);
  5730. #ifdef CONFIG_AUTOSCAN
  5731. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  5732. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  5733. reply_len = -1;
  5734. #endif /* CONFIG_AUTOSCAN */
  5735. #ifdef ANDROID
  5736. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  5737. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  5738. reply_size);
  5739. #endif /* ANDROID */
  5740. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  5741. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  5742. reply_size);
  5743. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  5744. pmksa_cache_clear_current(wpa_s->wpa);
  5745. eapol_sm_request_reauth(wpa_s->eapol);
  5746. #ifdef CONFIG_WNM
  5747. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  5748. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  5749. reply_len = -1;
  5750. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  5751. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  5752. reply_len = -1;
  5753. #endif /* CONFIG_WNM */
  5754. } else if (os_strcmp(buf, "FLUSH") == 0) {
  5755. wpa_supplicant_ctrl_iface_flush(wpa_s);
  5756. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  5757. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  5758. reply_size);
  5759. #ifdef CONFIG_TESTING_OPTIONS
  5760. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  5761. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  5762. reply_len = -1;
  5763. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  5764. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  5765. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  5766. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  5767. reply_len = -1;
  5768. #endif /* CONFIG_TESTING_OPTIONS */
  5769. } else {
  5770. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5771. reply_len = 16;
  5772. }
  5773. if (reply_len < 0) {
  5774. os_memcpy(reply, "FAIL\n", 5);
  5775. reply_len = 5;
  5776. }
  5777. *resp_len = reply_len;
  5778. return reply;
  5779. }
  5780. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  5781. char *cmd)
  5782. {
  5783. struct wpa_interface iface;
  5784. char *pos;
  5785. /*
  5786. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  5787. * TAB<bridge_ifname>
  5788. */
  5789. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  5790. os_memset(&iface, 0, sizeof(iface));
  5791. do {
  5792. iface.ifname = pos = cmd;
  5793. pos = os_strchr(pos, '\t');
  5794. if (pos)
  5795. *pos++ = '\0';
  5796. if (iface.ifname[0] == '\0')
  5797. return -1;
  5798. if (pos == NULL)
  5799. break;
  5800. iface.confname = pos;
  5801. pos = os_strchr(pos, '\t');
  5802. if (pos)
  5803. *pos++ = '\0';
  5804. if (iface.confname[0] == '\0')
  5805. iface.confname = NULL;
  5806. if (pos == NULL)
  5807. break;
  5808. iface.driver = pos;
  5809. pos = os_strchr(pos, '\t');
  5810. if (pos)
  5811. *pos++ = '\0';
  5812. if (iface.driver[0] == '\0')
  5813. iface.driver = NULL;
  5814. if (pos == NULL)
  5815. break;
  5816. iface.ctrl_interface = pos;
  5817. pos = os_strchr(pos, '\t');
  5818. if (pos)
  5819. *pos++ = '\0';
  5820. if (iface.ctrl_interface[0] == '\0')
  5821. iface.ctrl_interface = NULL;
  5822. if (pos == NULL)
  5823. break;
  5824. iface.driver_param = pos;
  5825. pos = os_strchr(pos, '\t');
  5826. if (pos)
  5827. *pos++ = '\0';
  5828. if (iface.driver_param[0] == '\0')
  5829. iface.driver_param = NULL;
  5830. if (pos == NULL)
  5831. break;
  5832. iface.bridge_ifname = pos;
  5833. pos = os_strchr(pos, '\t');
  5834. if (pos)
  5835. *pos++ = '\0';
  5836. if (iface.bridge_ifname[0] == '\0')
  5837. iface.bridge_ifname = NULL;
  5838. if (pos == NULL)
  5839. break;
  5840. } while (0);
  5841. if (wpa_supplicant_get_iface(global, iface.ifname))
  5842. return -1;
  5843. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  5844. }
  5845. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  5846. char *cmd)
  5847. {
  5848. struct wpa_supplicant *wpa_s;
  5849. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  5850. wpa_s = wpa_supplicant_get_iface(global, cmd);
  5851. if (wpa_s == NULL)
  5852. return -1;
  5853. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  5854. }
  5855. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  5856. {
  5857. struct wpa_interface_info *prev;
  5858. while (iface) {
  5859. prev = iface;
  5860. iface = iface->next;
  5861. os_free(prev->ifname);
  5862. os_free(prev->desc);
  5863. os_free(prev);
  5864. }
  5865. }
  5866. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  5867. char *buf, int len)
  5868. {
  5869. int i, res;
  5870. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  5871. char *pos, *end;
  5872. for (i = 0; wpa_drivers[i]; i++) {
  5873. struct wpa_driver_ops *drv = wpa_drivers[i];
  5874. if (drv->get_interfaces == NULL)
  5875. continue;
  5876. tmp = drv->get_interfaces(global->drv_priv[i]);
  5877. if (tmp == NULL)
  5878. continue;
  5879. if (last == NULL)
  5880. iface = last = tmp;
  5881. else
  5882. last->next = tmp;
  5883. while (last->next)
  5884. last = last->next;
  5885. }
  5886. pos = buf;
  5887. end = buf + len;
  5888. for (tmp = iface; tmp; tmp = tmp->next) {
  5889. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5890. tmp->drv_name, tmp->ifname,
  5891. tmp->desc ? tmp->desc : "");
  5892. if (res < 0 || res >= end - pos) {
  5893. *pos = '\0';
  5894. break;
  5895. }
  5896. pos += res;
  5897. }
  5898. wpa_free_iface_info(iface);
  5899. return pos - buf;
  5900. }
  5901. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5902. char *buf, int len)
  5903. {
  5904. int res;
  5905. char *pos, *end;
  5906. struct wpa_supplicant *wpa_s;
  5907. wpa_s = global->ifaces;
  5908. pos = buf;
  5909. end = buf + len;
  5910. while (wpa_s) {
  5911. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5912. if (res < 0 || res >= end - pos) {
  5913. *pos = '\0';
  5914. break;
  5915. }
  5916. pos += res;
  5917. wpa_s = wpa_s->next;
  5918. }
  5919. return pos - buf;
  5920. }
  5921. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5922. const char *ifname,
  5923. char *cmd, size_t *resp_len)
  5924. {
  5925. struct wpa_supplicant *wpa_s;
  5926. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5927. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5928. break;
  5929. }
  5930. if (wpa_s == NULL) {
  5931. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5932. if (resp)
  5933. *resp_len = os_strlen(resp);
  5934. else
  5935. *resp_len = 1;
  5936. return resp;
  5937. }
  5938. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5939. }
  5940. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5941. char *buf, size_t *resp_len)
  5942. {
  5943. #ifdef CONFIG_P2P
  5944. static const char * cmd[] = {
  5945. "LIST_NETWORKS",
  5946. "P2P_FIND",
  5947. "P2P_STOP_FIND",
  5948. "P2P_LISTEN",
  5949. "P2P_GROUP_ADD",
  5950. "P2P_GET_PASSPHRASE",
  5951. "P2P_SERVICE_UPDATE",
  5952. "P2P_SERVICE_FLUSH",
  5953. "P2P_FLUSH",
  5954. "P2P_CANCEL",
  5955. "P2P_PRESENCE_REQ",
  5956. "P2P_EXT_LISTEN",
  5957. NULL
  5958. };
  5959. static const char * prefix[] = {
  5960. #ifdef ANDROID
  5961. "DRIVER ",
  5962. #endif /* ANDROID */
  5963. "GET_NETWORK ",
  5964. "REMOVE_NETWORK ",
  5965. "P2P_FIND ",
  5966. "P2P_CONNECT ",
  5967. "P2P_LISTEN ",
  5968. "P2P_GROUP_REMOVE ",
  5969. "P2P_GROUP_ADD ",
  5970. "P2P_PROV_DISC ",
  5971. "P2P_SERV_DISC_REQ ",
  5972. "P2P_SERV_DISC_CANCEL_REQ ",
  5973. "P2P_SERV_DISC_RESP ",
  5974. "P2P_SERV_DISC_EXTERNAL ",
  5975. "P2P_SERVICE_ADD ",
  5976. "P2P_SERVICE_DEL ",
  5977. "P2P_REJECT ",
  5978. "P2P_INVITE ",
  5979. "P2P_PEER ",
  5980. "P2P_SET ",
  5981. "P2P_UNAUTHORIZE ",
  5982. "P2P_PRESENCE_REQ ",
  5983. "P2P_EXT_LISTEN ",
  5984. "P2P_REMOVE_CLIENT ",
  5985. "NFC_GET_HANDOVER_SEL ",
  5986. "NFC_GET_HANDOVER_REQ ",
  5987. "NFC_REPORT_HANDOVER ",
  5988. NULL
  5989. };
  5990. int found = 0;
  5991. int i;
  5992. if (global->p2p_init_wpa_s == NULL)
  5993. return NULL;
  5994. for (i = 0; !found && cmd[i]; i++) {
  5995. if (os_strcmp(buf, cmd[i]) == 0)
  5996. found = 1;
  5997. }
  5998. for (i = 0; !found && prefix[i]; i++) {
  5999. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  6000. found = 1;
  6001. }
  6002. if (found)
  6003. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6004. buf, resp_len);
  6005. #endif /* CONFIG_P2P */
  6006. return NULL;
  6007. }
  6008. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  6009. char *buf, size_t *resp_len)
  6010. {
  6011. #ifdef CONFIG_WIFI_DISPLAY
  6012. if (global->p2p_init_wpa_s == NULL)
  6013. return NULL;
  6014. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  6015. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  6016. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6017. buf, resp_len);
  6018. #endif /* CONFIG_WIFI_DISPLAY */
  6019. return NULL;
  6020. }
  6021. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  6022. char *buf, size_t *resp_len)
  6023. {
  6024. char *ret;
  6025. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  6026. if (ret)
  6027. return ret;
  6028. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  6029. if (ret)
  6030. return ret;
  6031. return NULL;
  6032. }
  6033. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  6034. {
  6035. char *value;
  6036. value = os_strchr(cmd, ' ');
  6037. if (value == NULL)
  6038. return -1;
  6039. *value++ = '\0';
  6040. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  6041. #ifdef CONFIG_WIFI_DISPLAY
  6042. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  6043. wifi_display_enable(global, !!atoi(value));
  6044. return 0;
  6045. }
  6046. #endif /* CONFIG_WIFI_DISPLAY */
  6047. /* Restore cmd to its original value to allow redirection */
  6048. value[-1] = ' ';
  6049. return -1;
  6050. }
  6051. #ifndef CONFIG_NO_CONFIG_WRITE
  6052. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  6053. {
  6054. int ret = 0, saved = 0;
  6055. struct wpa_supplicant *wpa_s;
  6056. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6057. if (!wpa_s->conf->update_config) {
  6058. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  6059. continue;
  6060. }
  6061. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  6062. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  6063. ret = 1;
  6064. } else {
  6065. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  6066. saved++;
  6067. }
  6068. }
  6069. if (!saved && !ret) {
  6070. wpa_dbg(wpa_s, MSG_DEBUG,
  6071. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  6072. ret = 1;
  6073. }
  6074. return ret;
  6075. }
  6076. #endif /* CONFIG_NO_CONFIG_WRITE */
  6077. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  6078. char *buf, size_t buflen)
  6079. {
  6080. char *pos, *end;
  6081. int ret;
  6082. struct wpa_supplicant *wpa_s;
  6083. pos = buf;
  6084. end = buf + buflen;
  6085. #ifdef CONFIG_P2P
  6086. if (global->p2p && !global->p2p_disabled) {
  6087. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  6088. "\n"
  6089. "p2p_state=%s\n",
  6090. MAC2STR(global->p2p_dev_addr),
  6091. p2p_get_state_txt(global->p2p));
  6092. if (ret < 0 || ret >= end - pos)
  6093. return pos - buf;
  6094. pos += ret;
  6095. } else if (global->p2p) {
  6096. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  6097. if (ret < 0 || ret >= end - pos)
  6098. return pos - buf;
  6099. pos += ret;
  6100. }
  6101. #endif /* CONFIG_P2P */
  6102. #ifdef CONFIG_WIFI_DISPLAY
  6103. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  6104. !!global->wifi_display);
  6105. if (ret < 0 || ret >= end - pos)
  6106. return pos - buf;
  6107. pos += ret;
  6108. #endif /* CONFIG_WIFI_DISPLAY */
  6109. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6110. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  6111. "address=" MACSTR "\n",
  6112. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  6113. if (ret < 0 || ret >= end - pos)
  6114. return pos - buf;
  6115. pos += ret;
  6116. }
  6117. return pos - buf;
  6118. }
  6119. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  6120. char *buf, size_t *resp_len)
  6121. {
  6122. char *reply;
  6123. const int reply_size = 2048;
  6124. int reply_len;
  6125. int level = MSG_DEBUG;
  6126. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  6127. char *pos = os_strchr(buf + 7, ' ');
  6128. if (pos) {
  6129. *pos++ = '\0';
  6130. return wpas_global_ctrl_iface_ifname(global,
  6131. buf + 7, pos,
  6132. resp_len);
  6133. }
  6134. }
  6135. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  6136. if (reply)
  6137. return reply;
  6138. if (os_strcmp(buf, "PING") == 0)
  6139. level = MSG_EXCESSIVE;
  6140. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  6141. (const u8 *) buf, os_strlen(buf));
  6142. reply = os_malloc(reply_size);
  6143. if (reply == NULL) {
  6144. *resp_len = 1;
  6145. return NULL;
  6146. }
  6147. os_memcpy(reply, "OK\n", 3);
  6148. reply_len = 3;
  6149. if (os_strcmp(buf, "PING") == 0) {
  6150. os_memcpy(reply, "PONG\n", 5);
  6151. reply_len = 5;
  6152. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  6153. if (wpa_supplicant_global_iface_add(global, buf + 14))
  6154. reply_len = -1;
  6155. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  6156. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  6157. reply_len = -1;
  6158. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6159. reply_len = wpa_supplicant_global_iface_list(
  6160. global, reply, reply_size);
  6161. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6162. reply_len = wpa_supplicant_global_iface_interfaces(
  6163. global, reply, reply_size);
  6164. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6165. wpa_supplicant_terminate_proc(global);
  6166. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6167. wpas_notify_suspend(global);
  6168. } else if (os_strcmp(buf, "RESUME") == 0) {
  6169. wpas_notify_resume(global);
  6170. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  6171. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  6172. #ifdef CONFIG_P2P
  6173. if (global->p2p_init_wpa_s) {
  6174. os_free(reply);
  6175. /* Check if P2P redirection would work for this
  6176. * command. */
  6177. return wpa_supplicant_ctrl_iface_process(
  6178. global->p2p_init_wpa_s,
  6179. buf, resp_len);
  6180. }
  6181. #endif /* CONFIG_P2P */
  6182. reply_len = -1;
  6183. }
  6184. #ifndef CONFIG_NO_CONFIG_WRITE
  6185. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6186. if (wpas_global_ctrl_iface_save_config(global))
  6187. reply_len = -1;
  6188. #endif /* CONFIG_NO_CONFIG_WRITE */
  6189. } else if (os_strcmp(buf, "STATUS") == 0) {
  6190. reply_len = wpas_global_ctrl_iface_status(global, reply,
  6191. reply_size);
  6192. #ifdef CONFIG_MODULE_TESTS
  6193. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  6194. int wpas_module_tests(void);
  6195. if (wpas_module_tests() < 0)
  6196. reply_len = -1;
  6197. #endif /* CONFIG_MODULE_TESTS */
  6198. } else {
  6199. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6200. reply_len = 16;
  6201. }
  6202. if (reply_len < 0) {
  6203. os_memcpy(reply, "FAIL\n", 5);
  6204. reply_len = 5;
  6205. }
  6206. *resp_len = reply_len;
  6207. return reply;
  6208. }