ctrl_iface.c 158 KB

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