ctrl_iface.c 159 KB

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