ctrl_iface.c 127 KB

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