ctrl_iface.c 239 KB

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