ctrl_iface.c 200 KB

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