ctrl_iface.c 129 KB

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