ctrl_iface.c 130 KB

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