ctrl_iface.c 133 KB

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