ctrl_iface.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555
  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 first = 1, ret;
  1453. ret = os_snprintf(pos, end - pos, "-");
  1454. if (ret < 0 || ret >= end - pos)
  1455. return pos;
  1456. pos += ret;
  1457. if (cipher & WPA_CIPHER_NONE) {
  1458. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  1459. if (ret < 0 || ret >= end - pos)
  1460. return pos;
  1461. pos += ret;
  1462. first = 0;
  1463. }
  1464. if (cipher & WPA_CIPHER_WEP40) {
  1465. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  1466. if (ret < 0 || ret >= end - pos)
  1467. return pos;
  1468. pos += ret;
  1469. first = 0;
  1470. }
  1471. if (cipher & WPA_CIPHER_WEP104) {
  1472. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1473. first ? "" : "+");
  1474. if (ret < 0 || ret >= end - pos)
  1475. return pos;
  1476. pos += ret;
  1477. first = 0;
  1478. }
  1479. if (cipher & WPA_CIPHER_TKIP) {
  1480. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  1481. if (ret < 0 || ret >= end - pos)
  1482. return pos;
  1483. pos += ret;
  1484. first = 0;
  1485. }
  1486. if (cipher & WPA_CIPHER_CCMP) {
  1487. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  1488. if (ret < 0 || ret >= end - pos)
  1489. return pos;
  1490. pos += ret;
  1491. first = 0;
  1492. }
  1493. if (cipher & WPA_CIPHER_GCMP) {
  1494. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : "+");
  1495. if (ret < 0 || ret >= end - pos)
  1496. return pos;
  1497. pos += ret;
  1498. first = 0;
  1499. }
  1500. return pos;
  1501. }
  1502. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1503. const u8 *ie, size_t ie_len)
  1504. {
  1505. struct wpa_ie_data data;
  1506. int first, ret;
  1507. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1508. if (ret < 0 || ret >= end - pos)
  1509. return pos;
  1510. pos += ret;
  1511. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1512. ret = os_snprintf(pos, end - pos, "?]");
  1513. if (ret < 0 || ret >= end - pos)
  1514. return pos;
  1515. pos += ret;
  1516. return pos;
  1517. }
  1518. first = 1;
  1519. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1520. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1521. if (ret < 0 || ret >= end - pos)
  1522. return pos;
  1523. pos += ret;
  1524. first = 0;
  1525. }
  1526. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1527. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1528. if (ret < 0 || ret >= end - pos)
  1529. return pos;
  1530. pos += ret;
  1531. first = 0;
  1532. }
  1533. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1534. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1535. if (ret < 0 || ret >= end - pos)
  1536. return pos;
  1537. pos += ret;
  1538. first = 0;
  1539. }
  1540. #ifdef CONFIG_IEEE80211R
  1541. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1542. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1543. first ? "" : "+");
  1544. if (ret < 0 || ret >= end - pos)
  1545. return pos;
  1546. pos += ret;
  1547. first = 0;
  1548. }
  1549. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1550. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1551. first ? "" : "+");
  1552. if (ret < 0 || ret >= end - pos)
  1553. return pos;
  1554. pos += ret;
  1555. first = 0;
  1556. }
  1557. #endif /* CONFIG_IEEE80211R */
  1558. #ifdef CONFIG_IEEE80211W
  1559. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1560. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1561. first ? "" : "+");
  1562. if (ret < 0 || ret >= end - pos)
  1563. return pos;
  1564. pos += ret;
  1565. first = 0;
  1566. }
  1567. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1568. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1569. first ? "" : "+");
  1570. if (ret < 0 || ret >= end - pos)
  1571. return pos;
  1572. pos += ret;
  1573. first = 0;
  1574. }
  1575. #endif /* CONFIG_IEEE80211W */
  1576. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1577. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1578. ret = os_snprintf(pos, end - pos, "-preauth");
  1579. if (ret < 0 || ret >= end - pos)
  1580. return pos;
  1581. pos += ret;
  1582. }
  1583. ret = os_snprintf(pos, end - pos, "]");
  1584. if (ret < 0 || ret >= end - pos)
  1585. return pos;
  1586. pos += ret;
  1587. return pos;
  1588. }
  1589. #ifdef CONFIG_WPS
  1590. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1591. char *pos, char *end,
  1592. struct wpabuf *wps_ie)
  1593. {
  1594. int ret;
  1595. const char *txt;
  1596. if (wps_ie == NULL)
  1597. return pos;
  1598. if (wps_is_selected_pbc_registrar(wps_ie))
  1599. txt = "[WPS-PBC]";
  1600. #ifdef CONFIG_WPS2
  1601. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1602. txt = "[WPS-AUTH]";
  1603. #endif /* CONFIG_WPS2 */
  1604. else if (wps_is_selected_pin_registrar(wps_ie))
  1605. txt = "[WPS-PIN]";
  1606. else
  1607. txt = "[WPS]";
  1608. ret = os_snprintf(pos, end - pos, "%s", txt);
  1609. if (ret >= 0 && ret < end - pos)
  1610. pos += ret;
  1611. wpabuf_free(wps_ie);
  1612. return pos;
  1613. }
  1614. #endif /* CONFIG_WPS */
  1615. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1616. char *pos, char *end,
  1617. const struct wpa_bss *bss)
  1618. {
  1619. #ifdef CONFIG_WPS
  1620. struct wpabuf *wps_ie;
  1621. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1622. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1623. #else /* CONFIG_WPS */
  1624. return pos;
  1625. #endif /* CONFIG_WPS */
  1626. }
  1627. /* Format one result on one text line into a buffer. */
  1628. static int wpa_supplicant_ctrl_iface_scan_result(
  1629. struct wpa_supplicant *wpa_s,
  1630. const struct wpa_bss *bss, char *buf, size_t buflen)
  1631. {
  1632. char *pos, *end;
  1633. int ret;
  1634. const u8 *ie, *ie2, *p2p;
  1635. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1636. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1637. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1638. 0)
  1639. return 0; /* Do not show P2P listen discovery results here */
  1640. pos = buf;
  1641. end = buf + buflen;
  1642. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1643. MAC2STR(bss->bssid), bss->freq, bss->level);
  1644. if (ret < 0 || ret >= end - pos)
  1645. return -1;
  1646. pos += ret;
  1647. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1648. if (ie)
  1649. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1650. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1651. if (ie2)
  1652. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1653. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1654. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1655. ret = os_snprintf(pos, end - pos, "[WEP]");
  1656. if (ret < 0 || ret >= end - pos)
  1657. return -1;
  1658. pos += ret;
  1659. }
  1660. if (bss->caps & IEEE80211_CAP_IBSS) {
  1661. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1662. if (ret < 0 || ret >= end - pos)
  1663. return -1;
  1664. pos += ret;
  1665. }
  1666. if (bss->caps & IEEE80211_CAP_ESS) {
  1667. ret = os_snprintf(pos, end - pos, "[ESS]");
  1668. if (ret < 0 || ret >= end - pos)
  1669. return -1;
  1670. pos += ret;
  1671. }
  1672. if (p2p) {
  1673. ret = os_snprintf(pos, end - pos, "[P2P]");
  1674. if (ret < 0 || ret >= end - pos)
  1675. return -1;
  1676. pos += ret;
  1677. }
  1678. #ifdef CONFIG_HS20
  1679. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1680. ret = os_snprintf(pos, end - pos, "[HS20]");
  1681. if (ret < 0 || ret >= end - pos)
  1682. return -1;
  1683. pos += ret;
  1684. }
  1685. #endif /* CONFIG_HS20 */
  1686. ret = os_snprintf(pos, end - pos, "\t%s",
  1687. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1688. if (ret < 0 || ret >= end - pos)
  1689. return -1;
  1690. pos += ret;
  1691. ret = os_snprintf(pos, end - pos, "\n");
  1692. if (ret < 0 || ret >= end - pos)
  1693. return -1;
  1694. pos += ret;
  1695. return pos - buf;
  1696. }
  1697. static int wpa_supplicant_ctrl_iface_scan_results(
  1698. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1699. {
  1700. char *pos, *end;
  1701. struct wpa_bss *bss;
  1702. int ret;
  1703. pos = buf;
  1704. end = buf + buflen;
  1705. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1706. "flags / ssid\n");
  1707. if (ret < 0 || ret >= end - pos)
  1708. return pos - buf;
  1709. pos += ret;
  1710. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1711. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1712. end - pos);
  1713. if (ret < 0 || ret >= end - pos)
  1714. return pos - buf;
  1715. pos += ret;
  1716. }
  1717. return pos - buf;
  1718. }
  1719. static int wpa_supplicant_ctrl_iface_select_network(
  1720. struct wpa_supplicant *wpa_s, char *cmd)
  1721. {
  1722. int id;
  1723. struct wpa_ssid *ssid;
  1724. /* cmd: "<network id>" or "any" */
  1725. if (os_strcmp(cmd, "any") == 0) {
  1726. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1727. ssid = NULL;
  1728. } else {
  1729. id = atoi(cmd);
  1730. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1731. ssid = wpa_config_get_network(wpa_s->conf, id);
  1732. if (ssid == NULL) {
  1733. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1734. "network id=%d", id);
  1735. return -1;
  1736. }
  1737. if (ssid->disabled == 2) {
  1738. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1739. "SELECT_NETWORK with persistent P2P group");
  1740. return -1;
  1741. }
  1742. }
  1743. wpa_supplicant_select_network(wpa_s, ssid);
  1744. return 0;
  1745. }
  1746. static int wpa_supplicant_ctrl_iface_enable_network(
  1747. struct wpa_supplicant *wpa_s, char *cmd)
  1748. {
  1749. int id;
  1750. struct wpa_ssid *ssid;
  1751. /* cmd: "<network id>" or "all" */
  1752. if (os_strcmp(cmd, "all") == 0) {
  1753. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1754. ssid = NULL;
  1755. } else {
  1756. id = atoi(cmd);
  1757. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1758. ssid = wpa_config_get_network(wpa_s->conf, id);
  1759. if (ssid == NULL) {
  1760. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1761. "network id=%d", id);
  1762. return -1;
  1763. }
  1764. if (ssid->disabled == 2) {
  1765. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1766. "ENABLE_NETWORK with persistent P2P group");
  1767. return -1;
  1768. }
  1769. if (os_strstr(cmd, " no-connect")) {
  1770. ssid->disabled = 0;
  1771. return 0;
  1772. }
  1773. }
  1774. wpa_supplicant_enable_network(wpa_s, ssid);
  1775. return 0;
  1776. }
  1777. static int wpa_supplicant_ctrl_iface_disable_network(
  1778. struct wpa_supplicant *wpa_s, char *cmd)
  1779. {
  1780. int id;
  1781. struct wpa_ssid *ssid;
  1782. /* cmd: "<network id>" or "all" */
  1783. if (os_strcmp(cmd, "all") == 0) {
  1784. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1785. ssid = NULL;
  1786. } else {
  1787. id = atoi(cmd);
  1788. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1789. ssid = wpa_config_get_network(wpa_s->conf, id);
  1790. if (ssid == NULL) {
  1791. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1792. "network id=%d", id);
  1793. return -1;
  1794. }
  1795. if (ssid->disabled == 2) {
  1796. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1797. "DISABLE_NETWORK with persistent P2P "
  1798. "group");
  1799. return -1;
  1800. }
  1801. }
  1802. wpa_supplicant_disable_network(wpa_s, ssid);
  1803. return 0;
  1804. }
  1805. static int wpa_supplicant_ctrl_iface_add_network(
  1806. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1807. {
  1808. struct wpa_ssid *ssid;
  1809. int ret;
  1810. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1811. ssid = wpa_config_add_network(wpa_s->conf);
  1812. if (ssid == NULL)
  1813. return -1;
  1814. wpas_notify_network_added(wpa_s, ssid);
  1815. ssid->disabled = 1;
  1816. wpa_config_set_network_defaults(ssid);
  1817. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1818. if (ret < 0 || (size_t) ret >= buflen)
  1819. return -1;
  1820. return ret;
  1821. }
  1822. static int wpa_supplicant_ctrl_iface_remove_network(
  1823. struct wpa_supplicant *wpa_s, char *cmd)
  1824. {
  1825. int id;
  1826. struct wpa_ssid *ssid;
  1827. /* cmd: "<network id>" or "all" */
  1828. if (os_strcmp(cmd, "all") == 0) {
  1829. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1830. ssid = wpa_s->conf->ssid;
  1831. while (ssid) {
  1832. struct wpa_ssid *remove_ssid = ssid;
  1833. id = ssid->id;
  1834. ssid = ssid->next;
  1835. wpas_notify_network_removed(wpa_s, remove_ssid);
  1836. wpa_config_remove_network(wpa_s->conf, id);
  1837. }
  1838. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1839. if (wpa_s->current_ssid) {
  1840. #ifdef CONFIG_SME
  1841. wpa_s->sme.prev_bssid_set = 0;
  1842. #endif /* CONFIG_SME */
  1843. wpa_sm_set_config(wpa_s->wpa, NULL);
  1844. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1845. wpa_supplicant_deauthenticate(
  1846. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1847. }
  1848. return 0;
  1849. }
  1850. id = atoi(cmd);
  1851. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1852. ssid = wpa_config_get_network(wpa_s->conf, id);
  1853. if (ssid)
  1854. wpas_notify_network_removed(wpa_s, ssid);
  1855. if (ssid == NULL) {
  1856. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1857. "id=%d", id);
  1858. return -1;
  1859. }
  1860. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1861. #ifdef CONFIG_SME
  1862. wpa_s->sme.prev_bssid_set = 0;
  1863. #endif /* CONFIG_SME */
  1864. /*
  1865. * Invalidate the EAP session cache if the current or
  1866. * previously used network is removed.
  1867. */
  1868. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1869. }
  1870. if (ssid == wpa_s->current_ssid) {
  1871. wpa_sm_set_config(wpa_s->wpa, NULL);
  1872. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1873. wpa_supplicant_deauthenticate(wpa_s,
  1874. WLAN_REASON_DEAUTH_LEAVING);
  1875. }
  1876. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1877. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  1878. "network id=%d", id);
  1879. return -1;
  1880. }
  1881. return 0;
  1882. }
  1883. static int wpa_supplicant_ctrl_iface_set_network(
  1884. struct wpa_supplicant *wpa_s, char *cmd)
  1885. {
  1886. int id;
  1887. struct wpa_ssid *ssid;
  1888. char *name, *value;
  1889. /* cmd: "<network id> <variable name> <value>" */
  1890. name = os_strchr(cmd, ' ');
  1891. if (name == NULL)
  1892. return -1;
  1893. *name++ = '\0';
  1894. value = os_strchr(name, ' ');
  1895. if (value == NULL)
  1896. return -1;
  1897. *value++ = '\0';
  1898. id = atoi(cmd);
  1899. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1900. id, name);
  1901. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1902. (u8 *) value, os_strlen(value));
  1903. ssid = wpa_config_get_network(wpa_s->conf, id);
  1904. if (ssid == NULL) {
  1905. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1906. "id=%d", id);
  1907. return -1;
  1908. }
  1909. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1910. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1911. "variable '%s'", name);
  1912. return -1;
  1913. }
  1914. if (os_strcmp(name, "bssid") != 0 &&
  1915. os_strcmp(name, "priority") != 0)
  1916. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1917. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  1918. /*
  1919. * Invalidate the EAP session cache if anything in the current
  1920. * or previously used configuration changes.
  1921. */
  1922. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1923. }
  1924. if ((os_strcmp(name, "psk") == 0 &&
  1925. value[0] == '"' && ssid->ssid_len) ||
  1926. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1927. wpa_config_update_psk(ssid);
  1928. else if (os_strcmp(name, "priority") == 0)
  1929. wpa_config_update_prio_list(wpa_s->conf);
  1930. return 0;
  1931. }
  1932. static int wpa_supplicant_ctrl_iface_get_network(
  1933. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1934. {
  1935. int id;
  1936. size_t res;
  1937. struct wpa_ssid *ssid;
  1938. char *name, *value;
  1939. /* cmd: "<network id> <variable name>" */
  1940. name = os_strchr(cmd, ' ');
  1941. if (name == NULL || buflen == 0)
  1942. return -1;
  1943. *name++ = '\0';
  1944. id = atoi(cmd);
  1945. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1946. id, name);
  1947. ssid = wpa_config_get_network(wpa_s->conf, id);
  1948. if (ssid == NULL) {
  1949. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1950. "id=%d", id);
  1951. return -1;
  1952. }
  1953. value = wpa_config_get_no_key(ssid, name);
  1954. if (value == NULL) {
  1955. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1956. "variable '%s'", name);
  1957. return -1;
  1958. }
  1959. res = os_strlcpy(buf, value, buflen);
  1960. if (res >= buflen) {
  1961. os_free(value);
  1962. return -1;
  1963. }
  1964. os_free(value);
  1965. return res;
  1966. }
  1967. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  1968. char *buf, size_t buflen)
  1969. {
  1970. char *pos, *end;
  1971. struct wpa_cred *cred;
  1972. int ret;
  1973. pos = buf;
  1974. end = buf + buflen;
  1975. ret = os_snprintf(pos, end - pos,
  1976. "cred id / realm / username / domain / imsi\n");
  1977. if (ret < 0 || ret >= end - pos)
  1978. return pos - buf;
  1979. pos += ret;
  1980. cred = wpa_s->conf->cred;
  1981. while (cred) {
  1982. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  1983. cred->id, cred->realm ? cred->realm : "",
  1984. cred->username ? cred->username : "",
  1985. cred->domain ? cred->domain : "",
  1986. cred->imsi ? cred->imsi : "");
  1987. if (ret < 0 || ret >= end - pos)
  1988. return pos - buf;
  1989. pos += ret;
  1990. cred = cred->next;
  1991. }
  1992. return pos - buf;
  1993. }
  1994. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  1995. char *buf, size_t buflen)
  1996. {
  1997. struct wpa_cred *cred;
  1998. int ret;
  1999. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2000. cred = wpa_config_add_cred(wpa_s->conf);
  2001. if (cred == NULL)
  2002. return -1;
  2003. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2004. if (ret < 0 || (size_t) ret >= buflen)
  2005. return -1;
  2006. return ret;
  2007. }
  2008. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2009. struct wpa_cred *cred)
  2010. {
  2011. struct wpa_ssid *ssid;
  2012. char str[20];
  2013. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2014. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2015. return -1;
  2016. }
  2017. /* Remove any network entry created based on the removed credential */
  2018. ssid = wpa_s->conf->ssid;
  2019. while (ssid) {
  2020. if (ssid->parent_cred == cred) {
  2021. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2022. "used the removed credential", ssid->id);
  2023. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2024. ssid = ssid->next;
  2025. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2026. } else
  2027. ssid = ssid->next;
  2028. }
  2029. return 0;
  2030. }
  2031. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2032. char *cmd)
  2033. {
  2034. int id;
  2035. struct wpa_cred *cred, *prev;
  2036. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2037. if (os_strcmp(cmd, "all") == 0) {
  2038. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2039. cred = wpa_s->conf->cred;
  2040. while (cred) {
  2041. prev = cred;
  2042. cred = cred->next;
  2043. wpas_ctrl_remove_cred(wpa_s, prev);
  2044. }
  2045. return 0;
  2046. }
  2047. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2048. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2049. cmd + 8);
  2050. cred = wpa_s->conf->cred;
  2051. while (cred) {
  2052. prev = cred;
  2053. cred = cred->next;
  2054. if (prev->domain &&
  2055. os_strcmp(prev->domain, cmd + 8) == 0)
  2056. wpas_ctrl_remove_cred(wpa_s, prev);
  2057. }
  2058. return 0;
  2059. }
  2060. id = atoi(cmd);
  2061. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2062. cred = wpa_config_get_cred(wpa_s->conf, id);
  2063. return wpas_ctrl_remove_cred(wpa_s, cred);
  2064. }
  2065. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2066. char *cmd)
  2067. {
  2068. int id;
  2069. struct wpa_cred *cred;
  2070. char *name, *value;
  2071. /* cmd: "<cred id> <variable name> <value>" */
  2072. name = os_strchr(cmd, ' ');
  2073. if (name == NULL)
  2074. return -1;
  2075. *name++ = '\0';
  2076. value = os_strchr(name, ' ');
  2077. if (value == NULL)
  2078. return -1;
  2079. *value++ = '\0';
  2080. id = atoi(cmd);
  2081. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2082. id, name);
  2083. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2084. (u8 *) value, os_strlen(value));
  2085. cred = wpa_config_get_cred(wpa_s->conf, id);
  2086. if (cred == NULL) {
  2087. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2088. id);
  2089. return -1;
  2090. }
  2091. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2092. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2093. "variable '%s'", name);
  2094. return -1;
  2095. }
  2096. return 0;
  2097. }
  2098. #ifndef CONFIG_NO_CONFIG_WRITE
  2099. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2100. {
  2101. int ret;
  2102. if (!wpa_s->conf->update_config) {
  2103. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2104. "to update configuration (update_config=0)");
  2105. return -1;
  2106. }
  2107. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2108. if (ret) {
  2109. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2110. "update configuration");
  2111. } else {
  2112. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2113. " updated");
  2114. }
  2115. return ret;
  2116. }
  2117. #endif /* CONFIG_NO_CONFIG_WRITE */
  2118. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2119. struct wpa_driver_capa *capa,
  2120. char *buf, size_t buflen)
  2121. {
  2122. int ret, first = 1;
  2123. char *pos, *end;
  2124. size_t len;
  2125. pos = buf;
  2126. end = pos + buflen;
  2127. if (res < 0) {
  2128. if (strict)
  2129. return 0;
  2130. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2131. if (len >= buflen)
  2132. return -1;
  2133. return len;
  2134. }
  2135. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2136. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2137. if (ret < 0 || ret >= end - pos)
  2138. return pos - buf;
  2139. pos += ret;
  2140. first = 0;
  2141. }
  2142. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2143. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2144. if (ret < 0 || ret >= end - pos)
  2145. return pos - buf;
  2146. pos += ret;
  2147. first = 0;
  2148. }
  2149. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2150. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2151. if (ret < 0 || ret >= end - pos)
  2152. return pos - buf;
  2153. pos += ret;
  2154. first = 0;
  2155. }
  2156. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2157. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  2158. if (ret < 0 || ret >= end - pos)
  2159. return pos - buf;
  2160. pos += ret;
  2161. first = 0;
  2162. }
  2163. return pos - buf;
  2164. }
  2165. static int ctrl_iface_get_capability_group(int res, char *strict,
  2166. struct wpa_driver_capa *capa,
  2167. char *buf, size_t buflen)
  2168. {
  2169. int ret, first = 1;
  2170. char *pos, *end;
  2171. size_t len;
  2172. pos = buf;
  2173. end = pos + buflen;
  2174. if (res < 0) {
  2175. if (strict)
  2176. return 0;
  2177. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2178. if (len >= buflen)
  2179. return -1;
  2180. return len;
  2181. }
  2182. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2183. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2184. if (ret < 0 || ret >= end - pos)
  2185. return pos - buf;
  2186. pos += ret;
  2187. first = 0;
  2188. }
  2189. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2190. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2191. if (ret < 0 || ret >= end - pos)
  2192. return pos - buf;
  2193. pos += ret;
  2194. first = 0;
  2195. }
  2196. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2197. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2198. if (ret < 0 || ret >= end - pos)
  2199. return pos - buf;
  2200. pos += ret;
  2201. first = 0;
  2202. }
  2203. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2204. ret = os_snprintf(pos, end - pos, "%sWEP104",
  2205. first ? "" : " ");
  2206. if (ret < 0 || ret >= end - pos)
  2207. return pos - buf;
  2208. pos += ret;
  2209. first = 0;
  2210. }
  2211. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2212. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  2213. if (ret < 0 || ret >= end - pos)
  2214. return pos - buf;
  2215. pos += ret;
  2216. first = 0;
  2217. }
  2218. return pos - buf;
  2219. }
  2220. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2221. struct wpa_driver_capa *capa,
  2222. char *buf, size_t buflen)
  2223. {
  2224. int ret;
  2225. char *pos, *end;
  2226. size_t len;
  2227. pos = buf;
  2228. end = pos + buflen;
  2229. if (res < 0) {
  2230. if (strict)
  2231. return 0;
  2232. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2233. "NONE", buflen);
  2234. if (len >= buflen)
  2235. return -1;
  2236. return len;
  2237. }
  2238. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2239. if (ret < 0 || ret >= end - pos)
  2240. return pos - buf;
  2241. pos += ret;
  2242. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2243. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2244. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2245. if (ret < 0 || ret >= end - pos)
  2246. return pos - buf;
  2247. pos += ret;
  2248. }
  2249. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2250. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2251. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2252. if (ret < 0 || ret >= end - pos)
  2253. return pos - buf;
  2254. pos += ret;
  2255. }
  2256. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2257. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2258. if (ret < 0 || ret >= end - pos)
  2259. return pos - buf;
  2260. pos += ret;
  2261. }
  2262. return pos - buf;
  2263. }
  2264. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2265. struct wpa_driver_capa *capa,
  2266. char *buf, size_t buflen)
  2267. {
  2268. int ret, first = 1;
  2269. char *pos, *end;
  2270. size_t len;
  2271. pos = buf;
  2272. end = pos + buflen;
  2273. if (res < 0) {
  2274. if (strict)
  2275. return 0;
  2276. len = os_strlcpy(buf, "RSN WPA", buflen);
  2277. if (len >= buflen)
  2278. return -1;
  2279. return len;
  2280. }
  2281. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2282. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2283. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2284. if (ret < 0 || ret >= end - pos)
  2285. return pos - buf;
  2286. pos += ret;
  2287. first = 0;
  2288. }
  2289. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2290. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2291. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2292. if (ret < 0 || ret >= end - pos)
  2293. return pos - buf;
  2294. pos += ret;
  2295. first = 0;
  2296. }
  2297. return pos - buf;
  2298. }
  2299. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2300. struct wpa_driver_capa *capa,
  2301. char *buf, size_t buflen)
  2302. {
  2303. int ret, first = 1;
  2304. char *pos, *end;
  2305. size_t len;
  2306. pos = buf;
  2307. end = pos + buflen;
  2308. if (res < 0) {
  2309. if (strict)
  2310. return 0;
  2311. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2312. if (len >= buflen)
  2313. return -1;
  2314. return len;
  2315. }
  2316. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2317. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2318. if (ret < 0 || ret >= end - pos)
  2319. return pos - buf;
  2320. pos += ret;
  2321. first = 0;
  2322. }
  2323. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2324. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2325. first ? "" : " ");
  2326. if (ret < 0 || ret >= end - pos)
  2327. return pos - buf;
  2328. pos += ret;
  2329. first = 0;
  2330. }
  2331. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2332. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2333. if (ret < 0 || ret >= end - pos)
  2334. return pos - buf;
  2335. pos += ret;
  2336. first = 0;
  2337. }
  2338. return pos - buf;
  2339. }
  2340. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2341. char *buf, size_t buflen)
  2342. {
  2343. struct hostapd_channel_data *chnl;
  2344. int ret, i, j;
  2345. char *pos, *end, *hmode;
  2346. pos = buf;
  2347. end = pos + buflen;
  2348. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2349. switch (wpa_s->hw.modes[j].mode) {
  2350. case HOSTAPD_MODE_IEEE80211B:
  2351. hmode = "B";
  2352. break;
  2353. case HOSTAPD_MODE_IEEE80211G:
  2354. hmode = "G";
  2355. break;
  2356. case HOSTAPD_MODE_IEEE80211A:
  2357. hmode = "A";
  2358. break;
  2359. case HOSTAPD_MODE_IEEE80211AD:
  2360. hmode = "AD";
  2361. break;
  2362. default:
  2363. continue;
  2364. }
  2365. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2366. if (ret < 0 || ret >= end - pos)
  2367. return pos - buf;
  2368. pos += ret;
  2369. chnl = wpa_s->hw.modes[j].channels;
  2370. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2371. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2372. continue;
  2373. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2374. if (ret < 0 || ret >= end - pos)
  2375. return pos - buf;
  2376. pos += ret;
  2377. }
  2378. ret = os_snprintf(pos, end - pos, "\n");
  2379. if (ret < 0 || ret >= end - pos)
  2380. return pos - buf;
  2381. pos += ret;
  2382. }
  2383. return pos - buf;
  2384. }
  2385. static int wpa_supplicant_ctrl_iface_get_capability(
  2386. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2387. size_t buflen)
  2388. {
  2389. struct wpa_driver_capa capa;
  2390. int res;
  2391. char *strict;
  2392. char field[30];
  2393. size_t len;
  2394. /* Determine whether or not strict checking was requested */
  2395. len = os_strlcpy(field, _field, sizeof(field));
  2396. if (len >= sizeof(field))
  2397. return -1;
  2398. strict = os_strchr(field, ' ');
  2399. if (strict != NULL) {
  2400. *strict++ = '\0';
  2401. if (os_strcmp(strict, "strict") != 0)
  2402. return -1;
  2403. }
  2404. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2405. field, strict ? strict : "");
  2406. if (os_strcmp(field, "eap") == 0) {
  2407. return eap_get_names(buf, buflen);
  2408. }
  2409. res = wpa_drv_get_capa(wpa_s, &capa);
  2410. if (os_strcmp(field, "pairwise") == 0)
  2411. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2412. buf, buflen);
  2413. if (os_strcmp(field, "group") == 0)
  2414. return ctrl_iface_get_capability_group(res, strict, &capa,
  2415. buf, buflen);
  2416. if (os_strcmp(field, "key_mgmt") == 0)
  2417. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2418. buf, buflen);
  2419. if (os_strcmp(field, "proto") == 0)
  2420. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2421. buf, buflen);
  2422. if (os_strcmp(field, "auth_alg") == 0)
  2423. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2424. buf, buflen);
  2425. if (os_strcmp(field, "channels") == 0)
  2426. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2427. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2428. field);
  2429. return -1;
  2430. }
  2431. #ifdef CONFIG_INTERWORKING
  2432. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2433. struct wpabuf *data)
  2434. {
  2435. char *start = pos;
  2436. size_t i;
  2437. int ret;
  2438. const u8 *d;
  2439. if (data == NULL)
  2440. return start;
  2441. ret = os_snprintf(pos, end - pos, "%s=", title);
  2442. if (ret < 0 || ret >= end - pos)
  2443. return start;
  2444. pos += ret;
  2445. d = wpabuf_head_u8(data);
  2446. for (i = 0; i < wpabuf_len(data); i++) {
  2447. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2448. if (ret < 0 || ret >= end - pos)
  2449. return start;
  2450. pos += ret;
  2451. }
  2452. ret = os_snprintf(pos, end - pos, "\n");
  2453. if (ret < 0 || ret >= end - pos)
  2454. return start;
  2455. pos += ret;
  2456. return pos;
  2457. }
  2458. #endif /* CONFIG_INTERWORKING */
  2459. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2460. unsigned long mask, char *buf, size_t buflen)
  2461. {
  2462. size_t i;
  2463. int ret;
  2464. char *pos, *end;
  2465. const u8 *ie, *ie2;
  2466. pos = buf;
  2467. end = buf + buflen;
  2468. if (mask & WPA_BSS_MASK_ID) {
  2469. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2470. if (ret < 0 || ret >= end - pos)
  2471. return 0;
  2472. pos += ret;
  2473. }
  2474. if (mask & WPA_BSS_MASK_BSSID) {
  2475. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2476. MAC2STR(bss->bssid));
  2477. if (ret < 0 || ret >= end - pos)
  2478. return 0;
  2479. pos += ret;
  2480. }
  2481. if (mask & WPA_BSS_MASK_FREQ) {
  2482. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2483. if (ret < 0 || ret >= end - pos)
  2484. return 0;
  2485. pos += ret;
  2486. }
  2487. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2488. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2489. bss->beacon_int);
  2490. if (ret < 0 || ret >= end - pos)
  2491. return 0;
  2492. pos += ret;
  2493. }
  2494. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2495. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2496. bss->caps);
  2497. if (ret < 0 || ret >= end - pos)
  2498. return 0;
  2499. pos += ret;
  2500. }
  2501. if (mask & WPA_BSS_MASK_QUAL) {
  2502. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2503. if (ret < 0 || ret >= end - pos)
  2504. return 0;
  2505. pos += ret;
  2506. }
  2507. if (mask & WPA_BSS_MASK_NOISE) {
  2508. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2509. if (ret < 0 || ret >= end - pos)
  2510. return 0;
  2511. pos += ret;
  2512. }
  2513. if (mask & WPA_BSS_MASK_LEVEL) {
  2514. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2515. if (ret < 0 || ret >= end - pos)
  2516. return 0;
  2517. pos += ret;
  2518. }
  2519. if (mask & WPA_BSS_MASK_TSF) {
  2520. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2521. (unsigned long long) bss->tsf);
  2522. if (ret < 0 || ret >= end - pos)
  2523. return 0;
  2524. pos += ret;
  2525. }
  2526. if (mask & WPA_BSS_MASK_AGE) {
  2527. struct os_time now;
  2528. os_get_time(&now);
  2529. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2530. (int) (now.sec - bss->last_update.sec));
  2531. if (ret < 0 || ret >= end - pos)
  2532. return 0;
  2533. pos += ret;
  2534. }
  2535. if (mask & WPA_BSS_MASK_IE) {
  2536. ret = os_snprintf(pos, end - pos, "ie=");
  2537. if (ret < 0 || ret >= end - pos)
  2538. return 0;
  2539. pos += ret;
  2540. ie = (const u8 *) (bss + 1);
  2541. for (i = 0; i < bss->ie_len; i++) {
  2542. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2543. if (ret < 0 || ret >= end - pos)
  2544. return 0;
  2545. pos += ret;
  2546. }
  2547. ret = os_snprintf(pos, end - pos, "\n");
  2548. if (ret < 0 || ret >= end - pos)
  2549. return 0;
  2550. pos += ret;
  2551. }
  2552. if (mask & WPA_BSS_MASK_FLAGS) {
  2553. ret = os_snprintf(pos, end - pos, "flags=");
  2554. if (ret < 0 || ret >= end - pos)
  2555. return 0;
  2556. pos += ret;
  2557. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2558. if (ie)
  2559. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2560. 2 + ie[1]);
  2561. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2562. if (ie2)
  2563. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2564. 2 + ie2[1]);
  2565. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2566. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2567. ret = os_snprintf(pos, end - pos, "[WEP]");
  2568. if (ret < 0 || ret >= end - pos)
  2569. return 0;
  2570. pos += ret;
  2571. }
  2572. if (bss->caps & IEEE80211_CAP_IBSS) {
  2573. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2574. if (ret < 0 || ret >= end - pos)
  2575. return 0;
  2576. pos += ret;
  2577. }
  2578. if (bss->caps & IEEE80211_CAP_ESS) {
  2579. ret = os_snprintf(pos, end - pos, "[ESS]");
  2580. if (ret < 0 || ret >= end - pos)
  2581. return 0;
  2582. pos += ret;
  2583. }
  2584. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  2585. ret = os_snprintf(pos, end - pos, "[P2P]");
  2586. if (ret < 0 || ret >= end - pos)
  2587. return 0;
  2588. pos += ret;
  2589. }
  2590. #ifdef CONFIG_HS20
  2591. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2592. ret = os_snprintf(pos, end - pos, "[HS20]");
  2593. if (ret < 0 || ret >= end - pos)
  2594. return -1;
  2595. pos += ret;
  2596. }
  2597. #endif /* CONFIG_HS20 */
  2598. ret = os_snprintf(pos, end - pos, "\n");
  2599. if (ret < 0 || ret >= end - pos)
  2600. return 0;
  2601. pos += ret;
  2602. }
  2603. if (mask & WPA_BSS_MASK_SSID) {
  2604. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2605. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2606. if (ret < 0 || ret >= end - pos)
  2607. return 0;
  2608. pos += ret;
  2609. }
  2610. #ifdef CONFIG_WPS
  2611. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2612. ie = (const u8 *) (bss + 1);
  2613. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2614. if (ret < 0 || ret >= end - pos)
  2615. return 0;
  2616. pos += ret;
  2617. }
  2618. #endif /* CONFIG_WPS */
  2619. #ifdef CONFIG_P2P
  2620. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2621. ie = (const u8 *) (bss + 1);
  2622. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2623. if (ret < 0 || ret >= end - pos)
  2624. return 0;
  2625. pos += ret;
  2626. }
  2627. #endif /* CONFIG_P2P */
  2628. #ifdef CONFIG_WIFI_DISPLAY
  2629. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2630. struct wpabuf *wfd;
  2631. ie = (const u8 *) (bss + 1);
  2632. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2633. WFD_IE_VENDOR_TYPE);
  2634. if (wfd) {
  2635. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2636. if (ret < 0 || ret >= end - pos)
  2637. return pos - buf;
  2638. pos += ret;
  2639. pos += wpa_snprintf_hex(pos, end - pos,
  2640. wpabuf_head(wfd),
  2641. wpabuf_len(wfd));
  2642. wpabuf_free(wfd);
  2643. ret = os_snprintf(pos, end - pos, "\n");
  2644. if (ret < 0 || ret >= end - pos)
  2645. return pos - buf;
  2646. pos += ret;
  2647. }
  2648. }
  2649. #endif /* CONFIG_WIFI_DISPLAY */
  2650. #ifdef CONFIG_INTERWORKING
  2651. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2652. struct wpa_bss_anqp *anqp = bss->anqp;
  2653. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2654. anqp->venue_name);
  2655. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2656. anqp->network_auth_type);
  2657. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2658. anqp->roaming_consortium);
  2659. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2660. anqp->ip_addr_type_availability);
  2661. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2662. anqp->nai_realm);
  2663. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2664. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2665. anqp->domain_name);
  2666. #ifdef CONFIG_HS20
  2667. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2668. anqp->hs20_operator_friendly_name);
  2669. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2670. anqp->hs20_wan_metrics);
  2671. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2672. anqp->hs20_connection_capability);
  2673. #endif /* CONFIG_HS20 */
  2674. }
  2675. #endif /* CONFIG_INTERWORKING */
  2676. return pos - buf;
  2677. }
  2678. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2679. const char *cmd, char *buf,
  2680. size_t buflen)
  2681. {
  2682. u8 bssid[ETH_ALEN];
  2683. size_t i;
  2684. struct wpa_bss *bss;
  2685. struct wpa_bss *bsslast = NULL;
  2686. struct dl_list *next;
  2687. int ret = 0;
  2688. int len;
  2689. char *ctmp;
  2690. unsigned long mask = WPA_BSS_MASK_ALL;
  2691. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2692. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2693. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2694. list_id);
  2695. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2696. list_id);
  2697. } else { /* N1-N2 */
  2698. unsigned int id1, id2;
  2699. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2700. wpa_printf(MSG_INFO, "Wrong BSS range "
  2701. "format");
  2702. return 0;
  2703. }
  2704. id1 = atoi(cmd + 6);
  2705. bss = wpa_bss_get_id(wpa_s, id1);
  2706. id2 = atoi(ctmp + 1);
  2707. if (id2 == 0)
  2708. bsslast = dl_list_last(&wpa_s->bss_id,
  2709. struct wpa_bss,
  2710. list_id);
  2711. else {
  2712. bsslast = wpa_bss_get_id(wpa_s, id2);
  2713. if (bsslast == NULL && bss && id2 > id1) {
  2714. struct wpa_bss *tmp = bss;
  2715. for (;;) {
  2716. next = tmp->list_id.next;
  2717. if (next == &wpa_s->bss_id)
  2718. break;
  2719. tmp = dl_list_entry(
  2720. next, struct wpa_bss,
  2721. list_id);
  2722. if (tmp->id > id2)
  2723. break;
  2724. bsslast = tmp;
  2725. }
  2726. }
  2727. }
  2728. }
  2729. } else if (os_strcmp(cmd, "FIRST") == 0)
  2730. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2731. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2732. i = atoi(cmd + 3);
  2733. bss = wpa_bss_get_id(wpa_s, i);
  2734. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2735. i = atoi(cmd + 5);
  2736. bss = wpa_bss_get_id(wpa_s, i);
  2737. if (bss) {
  2738. next = bss->list_id.next;
  2739. if (next == &wpa_s->bss_id)
  2740. bss = NULL;
  2741. else
  2742. bss = dl_list_entry(next, struct wpa_bss,
  2743. list_id);
  2744. }
  2745. #ifdef CONFIG_P2P
  2746. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2747. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2748. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2749. else
  2750. bss = NULL;
  2751. #endif /* CONFIG_P2P */
  2752. } else if (hwaddr_aton(cmd, bssid) == 0)
  2753. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2754. else {
  2755. struct wpa_bss *tmp;
  2756. i = atoi(cmd);
  2757. bss = NULL;
  2758. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2759. {
  2760. if (i-- == 0) {
  2761. bss = tmp;
  2762. break;
  2763. }
  2764. }
  2765. }
  2766. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2767. mask = strtoul(ctmp + 5, NULL, 0x10);
  2768. if (mask == 0)
  2769. mask = WPA_BSS_MASK_ALL;
  2770. }
  2771. if (bss == NULL)
  2772. return 0;
  2773. if (bsslast == NULL)
  2774. bsslast = bss;
  2775. do {
  2776. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2777. ret += len;
  2778. buf += len;
  2779. buflen -= len;
  2780. if (bss == bsslast)
  2781. break;
  2782. next = bss->list_id.next;
  2783. if (next == &wpa_s->bss_id)
  2784. break;
  2785. bss = dl_list_entry(next, struct wpa_bss, list_id);
  2786. } while (bss && len);
  2787. return ret;
  2788. }
  2789. static int wpa_supplicant_ctrl_iface_ap_scan(
  2790. struct wpa_supplicant *wpa_s, char *cmd)
  2791. {
  2792. int ap_scan = atoi(cmd);
  2793. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2794. }
  2795. static int wpa_supplicant_ctrl_iface_scan_interval(
  2796. struct wpa_supplicant *wpa_s, char *cmd)
  2797. {
  2798. int scan_int = atoi(cmd);
  2799. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  2800. }
  2801. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  2802. struct wpa_supplicant *wpa_s, char *cmd)
  2803. {
  2804. int expire_age = atoi(cmd);
  2805. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  2806. }
  2807. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  2808. struct wpa_supplicant *wpa_s, char *cmd)
  2809. {
  2810. int expire_count = atoi(cmd);
  2811. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  2812. }
  2813. static int wpa_supplicant_ctrl_iface_bss_flush(
  2814. struct wpa_supplicant *wpa_s, char *cmd)
  2815. {
  2816. int flush_age = atoi(cmd);
  2817. if (flush_age == 0)
  2818. wpa_bss_flush(wpa_s);
  2819. else
  2820. wpa_bss_flush_by_age(wpa_s, flush_age);
  2821. return 0;
  2822. }
  2823. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  2824. {
  2825. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  2826. /* MLME-DELETEKEYS.request */
  2827. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  2828. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  2829. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  2830. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  2831. #ifdef CONFIG_IEEE80211W
  2832. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  2833. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  2834. #endif /* CONFIG_IEEE80211W */
  2835. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  2836. 0);
  2837. /* MLME-SETPROTECTION.request(None) */
  2838. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  2839. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  2840. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  2841. wpa_sm_drop_sa(wpa_s->wpa);
  2842. }
  2843. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  2844. char *addr)
  2845. {
  2846. #ifdef CONFIG_NO_SCAN_PROCESSING
  2847. return -1;
  2848. #else /* CONFIG_NO_SCAN_PROCESSING */
  2849. u8 bssid[ETH_ALEN];
  2850. struct wpa_bss *bss;
  2851. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2852. if (hwaddr_aton(addr, bssid)) {
  2853. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  2854. "address '%s'", addr);
  2855. return -1;
  2856. }
  2857. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  2858. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2859. if (!bss) {
  2860. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  2861. "from BSS table");
  2862. return -1;
  2863. }
  2864. /*
  2865. * TODO: Find best network configuration block from configuration to
  2866. * allow roaming to other networks
  2867. */
  2868. if (!ssid) {
  2869. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  2870. "configuration known for the target AP");
  2871. return -1;
  2872. }
  2873. wpa_s->reassociate = 1;
  2874. wpa_supplicant_connect(wpa_s, bss, ssid);
  2875. return 0;
  2876. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2877. }
  2878. #ifdef CONFIG_P2P
  2879. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  2880. {
  2881. unsigned int timeout = atoi(cmd);
  2882. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  2883. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  2884. char *pos;
  2885. unsigned int search_delay;
  2886. if (os_strstr(cmd, "type=social"))
  2887. type = P2P_FIND_ONLY_SOCIAL;
  2888. else if (os_strstr(cmd, "type=progressive"))
  2889. type = P2P_FIND_PROGRESSIVE;
  2890. pos = os_strstr(cmd, "dev_id=");
  2891. if (pos) {
  2892. pos += 7;
  2893. if (hwaddr_aton(pos, dev_id))
  2894. return -1;
  2895. _dev_id = dev_id;
  2896. }
  2897. pos = os_strstr(cmd, "delay=");
  2898. if (pos) {
  2899. pos += 6;
  2900. search_delay = atoi(pos);
  2901. } else
  2902. search_delay = wpas_p2p_search_delay(wpa_s);
  2903. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  2904. search_delay);
  2905. }
  2906. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  2907. char *buf, size_t buflen)
  2908. {
  2909. u8 addr[ETH_ALEN];
  2910. char *pos, *pos2;
  2911. char *pin = NULL;
  2912. enum p2p_wps_method wps_method;
  2913. int new_pin;
  2914. int ret;
  2915. int persistent_group, persistent_id = -1;
  2916. int join;
  2917. int auth;
  2918. int automatic;
  2919. int go_intent = -1;
  2920. int freq = 0;
  2921. int pd;
  2922. int ht40;
  2923. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  2924. * [persistent|persistent=<network id>]
  2925. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  2926. * [ht40] */
  2927. if (hwaddr_aton(cmd, addr))
  2928. return -1;
  2929. pos = cmd + 17;
  2930. if (*pos != ' ')
  2931. return -1;
  2932. pos++;
  2933. persistent_group = os_strstr(pos, " persistent") != NULL;
  2934. pos2 = os_strstr(pos, " persistent=");
  2935. if (pos2) {
  2936. struct wpa_ssid *ssid;
  2937. persistent_id = atoi(pos2 + 12);
  2938. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  2939. if (ssid == NULL || ssid->disabled != 2 ||
  2940. ssid->mode != WPAS_MODE_P2P_GO) {
  2941. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2942. "SSID id=%d for persistent P2P group (GO)",
  2943. persistent_id);
  2944. return -1;
  2945. }
  2946. }
  2947. join = os_strstr(pos, " join") != NULL;
  2948. auth = os_strstr(pos, " auth") != NULL;
  2949. automatic = os_strstr(pos, " auto") != NULL;
  2950. pd = os_strstr(pos, " provdisc") != NULL;
  2951. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  2952. pos2 = os_strstr(pos, " go_intent=");
  2953. if (pos2) {
  2954. pos2 += 11;
  2955. go_intent = atoi(pos2);
  2956. if (go_intent < 0 || go_intent > 15)
  2957. return -1;
  2958. }
  2959. pos2 = os_strstr(pos, " freq=");
  2960. if (pos2) {
  2961. pos2 += 6;
  2962. freq = atoi(pos2);
  2963. if (freq <= 0)
  2964. return -1;
  2965. }
  2966. if (os_strncmp(pos, "pin", 3) == 0) {
  2967. /* Request random PIN (to be displayed) and enable the PIN */
  2968. wps_method = WPS_PIN_DISPLAY;
  2969. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  2970. wps_method = WPS_PBC;
  2971. } else {
  2972. pin = pos;
  2973. pos = os_strchr(pin, ' ');
  2974. wps_method = WPS_PIN_KEYPAD;
  2975. if (pos) {
  2976. *pos++ = '\0';
  2977. if (os_strncmp(pos, "display", 7) == 0)
  2978. wps_method = WPS_PIN_DISPLAY;
  2979. }
  2980. if (!wps_pin_str_valid(pin)) {
  2981. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  2982. return 17;
  2983. }
  2984. }
  2985. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  2986. persistent_group, automatic, join,
  2987. auth, go_intent, freq, persistent_id, pd,
  2988. ht40);
  2989. if (new_pin == -2) {
  2990. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  2991. return 25;
  2992. }
  2993. if (new_pin == -3) {
  2994. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  2995. return 25;
  2996. }
  2997. if (new_pin < 0)
  2998. return -1;
  2999. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3000. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3001. if (ret < 0 || (size_t) ret >= buflen)
  3002. return -1;
  3003. return ret;
  3004. }
  3005. os_memcpy(buf, "OK\n", 3);
  3006. return 3;
  3007. }
  3008. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3009. {
  3010. unsigned int timeout = atoi(cmd);
  3011. return wpas_p2p_listen(wpa_s, timeout);
  3012. }
  3013. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3014. {
  3015. u8 addr[ETH_ALEN];
  3016. char *pos;
  3017. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3018. /* <addr> <config method> [join|auto] */
  3019. if (hwaddr_aton(cmd, addr))
  3020. return -1;
  3021. pos = cmd + 17;
  3022. if (*pos != ' ')
  3023. return -1;
  3024. pos++;
  3025. if (os_strstr(pos, " join") != NULL)
  3026. use = WPAS_P2P_PD_FOR_JOIN;
  3027. else if (os_strstr(pos, " auto") != NULL)
  3028. use = WPAS_P2P_PD_AUTO;
  3029. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3030. }
  3031. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3032. size_t buflen)
  3033. {
  3034. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3035. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3036. ssid->passphrase == NULL)
  3037. return -1;
  3038. os_strlcpy(buf, ssid->passphrase, buflen);
  3039. return os_strlen(buf);
  3040. }
  3041. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3042. char *buf, size_t buflen)
  3043. {
  3044. u64 ref;
  3045. int res;
  3046. u8 dst_buf[ETH_ALEN], *dst;
  3047. struct wpabuf *tlvs;
  3048. char *pos;
  3049. size_t len;
  3050. if (hwaddr_aton(cmd, dst_buf))
  3051. return -1;
  3052. dst = dst_buf;
  3053. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3054. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3055. dst = NULL;
  3056. pos = cmd + 17;
  3057. if (*pos != ' ')
  3058. return -1;
  3059. pos++;
  3060. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3061. u8 version;
  3062. pos += 5;
  3063. if (hexstr2bin(pos, &version, 1) < 0)
  3064. return -1;
  3065. pos += 2;
  3066. if (*pos != ' ')
  3067. return -1;
  3068. pos++;
  3069. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3070. #ifdef CONFIG_WIFI_DISPLAY
  3071. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3072. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3073. #endif /* CONFIG_WIFI_DISPLAY */
  3074. } else {
  3075. len = os_strlen(pos);
  3076. if (len & 1)
  3077. return -1;
  3078. len /= 2;
  3079. tlvs = wpabuf_alloc(len);
  3080. if (tlvs == NULL)
  3081. return -1;
  3082. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3083. wpabuf_free(tlvs);
  3084. return -1;
  3085. }
  3086. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3087. wpabuf_free(tlvs);
  3088. }
  3089. if (ref == 0)
  3090. return -1;
  3091. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3092. if (res < 0 || (unsigned) res >= buflen)
  3093. return -1;
  3094. return res;
  3095. }
  3096. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3097. char *cmd)
  3098. {
  3099. long long unsigned val;
  3100. u64 req;
  3101. if (sscanf(cmd, "%llx", &val) != 1)
  3102. return -1;
  3103. req = val;
  3104. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3105. }
  3106. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3107. {
  3108. int freq;
  3109. u8 dst[ETH_ALEN];
  3110. u8 dialog_token;
  3111. struct wpabuf *resp_tlvs;
  3112. char *pos, *pos2;
  3113. size_t len;
  3114. pos = os_strchr(cmd, ' ');
  3115. if (pos == NULL)
  3116. return -1;
  3117. *pos++ = '\0';
  3118. freq = atoi(cmd);
  3119. if (freq == 0)
  3120. return -1;
  3121. if (hwaddr_aton(pos, dst))
  3122. return -1;
  3123. pos += 17;
  3124. if (*pos != ' ')
  3125. return -1;
  3126. pos++;
  3127. pos2 = os_strchr(pos, ' ');
  3128. if (pos2 == NULL)
  3129. return -1;
  3130. *pos2++ = '\0';
  3131. dialog_token = atoi(pos);
  3132. len = os_strlen(pos2);
  3133. if (len & 1)
  3134. return -1;
  3135. len /= 2;
  3136. resp_tlvs = wpabuf_alloc(len);
  3137. if (resp_tlvs == NULL)
  3138. return -1;
  3139. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3140. wpabuf_free(resp_tlvs);
  3141. return -1;
  3142. }
  3143. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3144. wpabuf_free(resp_tlvs);
  3145. return 0;
  3146. }
  3147. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3148. char *cmd)
  3149. {
  3150. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3151. return -1;
  3152. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3153. return 0;
  3154. }
  3155. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3156. char *cmd)
  3157. {
  3158. char *pos;
  3159. size_t len;
  3160. struct wpabuf *query, *resp;
  3161. pos = os_strchr(cmd, ' ');
  3162. if (pos == NULL)
  3163. return -1;
  3164. *pos++ = '\0';
  3165. len = os_strlen(cmd);
  3166. if (len & 1)
  3167. return -1;
  3168. len /= 2;
  3169. query = wpabuf_alloc(len);
  3170. if (query == NULL)
  3171. return -1;
  3172. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3173. wpabuf_free(query);
  3174. return -1;
  3175. }
  3176. len = os_strlen(pos);
  3177. if (len & 1) {
  3178. wpabuf_free(query);
  3179. return -1;
  3180. }
  3181. len /= 2;
  3182. resp = wpabuf_alloc(len);
  3183. if (resp == NULL) {
  3184. wpabuf_free(query);
  3185. return -1;
  3186. }
  3187. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3188. wpabuf_free(query);
  3189. wpabuf_free(resp);
  3190. return -1;
  3191. }
  3192. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3193. wpabuf_free(query);
  3194. wpabuf_free(resp);
  3195. return -1;
  3196. }
  3197. return 0;
  3198. }
  3199. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3200. {
  3201. char *pos;
  3202. u8 version;
  3203. pos = os_strchr(cmd, ' ');
  3204. if (pos == NULL)
  3205. return -1;
  3206. *pos++ = '\0';
  3207. if (hexstr2bin(cmd, &version, 1) < 0)
  3208. return -1;
  3209. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3210. }
  3211. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3212. {
  3213. char *pos;
  3214. pos = os_strchr(cmd, ' ');
  3215. if (pos == NULL)
  3216. return -1;
  3217. *pos++ = '\0';
  3218. if (os_strcmp(cmd, "bonjour") == 0)
  3219. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3220. if (os_strcmp(cmd, "upnp") == 0)
  3221. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3222. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3223. return -1;
  3224. }
  3225. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3226. char *cmd)
  3227. {
  3228. size_t len;
  3229. struct wpabuf *query;
  3230. int ret;
  3231. len = os_strlen(cmd);
  3232. if (len & 1)
  3233. return -1;
  3234. len /= 2;
  3235. query = wpabuf_alloc(len);
  3236. if (query == NULL)
  3237. return -1;
  3238. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3239. wpabuf_free(query);
  3240. return -1;
  3241. }
  3242. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3243. wpabuf_free(query);
  3244. return ret;
  3245. }
  3246. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3247. {
  3248. char *pos;
  3249. u8 version;
  3250. pos = os_strchr(cmd, ' ');
  3251. if (pos == NULL)
  3252. return -1;
  3253. *pos++ = '\0';
  3254. if (hexstr2bin(cmd, &version, 1) < 0)
  3255. return -1;
  3256. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3257. }
  3258. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3259. {
  3260. char *pos;
  3261. pos = os_strchr(cmd, ' ');
  3262. if (pos == NULL)
  3263. return -1;
  3264. *pos++ = '\0';
  3265. if (os_strcmp(cmd, "bonjour") == 0)
  3266. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3267. if (os_strcmp(cmd, "upnp") == 0)
  3268. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3269. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3270. return -1;
  3271. }
  3272. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3273. {
  3274. u8 addr[ETH_ALEN];
  3275. /* <addr> */
  3276. if (hwaddr_aton(cmd, addr))
  3277. return -1;
  3278. return wpas_p2p_reject(wpa_s, addr);
  3279. }
  3280. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3281. {
  3282. char *pos;
  3283. int id;
  3284. struct wpa_ssid *ssid;
  3285. u8 *_peer = NULL, peer[ETH_ALEN];
  3286. int freq = 0;
  3287. int ht40;
  3288. id = atoi(cmd);
  3289. pos = os_strstr(cmd, " peer=");
  3290. if (pos) {
  3291. pos += 6;
  3292. if (hwaddr_aton(pos, peer))
  3293. return -1;
  3294. _peer = peer;
  3295. }
  3296. ssid = wpa_config_get_network(wpa_s->conf, id);
  3297. if (ssid == NULL || ssid->disabled != 2) {
  3298. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3299. "for persistent P2P group",
  3300. id);
  3301. return -1;
  3302. }
  3303. pos = os_strstr(cmd, " freq=");
  3304. if (pos) {
  3305. pos += 6;
  3306. freq = atoi(pos);
  3307. if (freq <= 0)
  3308. return -1;
  3309. }
  3310. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3311. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40);
  3312. }
  3313. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3314. {
  3315. char *pos;
  3316. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3317. pos = os_strstr(cmd, " peer=");
  3318. if (!pos)
  3319. return -1;
  3320. *pos = '\0';
  3321. pos += 6;
  3322. if (hwaddr_aton(pos, peer)) {
  3323. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3324. return -1;
  3325. }
  3326. pos = os_strstr(pos, " go_dev_addr=");
  3327. if (pos) {
  3328. pos += 13;
  3329. if (hwaddr_aton(pos, go_dev_addr)) {
  3330. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3331. pos);
  3332. return -1;
  3333. }
  3334. go_dev = go_dev_addr;
  3335. }
  3336. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3337. }
  3338. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3339. {
  3340. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3341. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3342. if (os_strncmp(cmd, "group=", 6) == 0)
  3343. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3344. return -1;
  3345. }
  3346. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3347. char *cmd, int freq, int ht40)
  3348. {
  3349. int id;
  3350. struct wpa_ssid *ssid;
  3351. id = atoi(cmd);
  3352. ssid = wpa_config_get_network(wpa_s->conf, id);
  3353. if (ssid == NULL || ssid->disabled != 2) {
  3354. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3355. "for persistent P2P group",
  3356. id);
  3357. return -1;
  3358. }
  3359. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
  3360. }
  3361. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3362. {
  3363. int freq = 0, ht40;
  3364. char *pos;
  3365. pos = os_strstr(cmd, "freq=");
  3366. if (pos)
  3367. freq = atoi(pos + 5);
  3368. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3369. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3370. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3371. ht40);
  3372. if (os_strcmp(cmd, "persistent") == 0 ||
  3373. os_strncmp(cmd, "persistent ", 11) == 0)
  3374. return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
  3375. if (os_strncmp(cmd, "freq=", 5) == 0)
  3376. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3377. if (ht40)
  3378. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3379. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3380. cmd);
  3381. return -1;
  3382. }
  3383. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3384. char *buf, size_t buflen)
  3385. {
  3386. u8 addr[ETH_ALEN], *addr_ptr;
  3387. int next, res;
  3388. const struct p2p_peer_info *info;
  3389. char *pos, *end;
  3390. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3391. struct wpa_ssid *ssid;
  3392. size_t i;
  3393. if (!wpa_s->global->p2p)
  3394. return -1;
  3395. if (os_strcmp(cmd, "FIRST") == 0) {
  3396. addr_ptr = NULL;
  3397. next = 0;
  3398. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3399. if (hwaddr_aton(cmd + 5, addr) < 0)
  3400. return -1;
  3401. addr_ptr = addr;
  3402. next = 1;
  3403. } else {
  3404. if (hwaddr_aton(cmd, addr) < 0)
  3405. return -1;
  3406. addr_ptr = addr;
  3407. next = 0;
  3408. }
  3409. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3410. if (info == NULL)
  3411. return -1;
  3412. pos = buf;
  3413. end = buf + buflen;
  3414. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3415. "pri_dev_type=%s\n"
  3416. "device_name=%s\n"
  3417. "manufacturer=%s\n"
  3418. "model_name=%s\n"
  3419. "model_number=%s\n"
  3420. "serial_number=%s\n"
  3421. "config_methods=0x%x\n"
  3422. "dev_capab=0x%x\n"
  3423. "group_capab=0x%x\n"
  3424. "level=%d\n",
  3425. MAC2STR(info->p2p_device_addr),
  3426. wps_dev_type_bin2str(info->pri_dev_type,
  3427. devtype, sizeof(devtype)),
  3428. info->device_name,
  3429. info->manufacturer,
  3430. info->model_name,
  3431. info->model_number,
  3432. info->serial_number,
  3433. info->config_methods,
  3434. info->dev_capab,
  3435. info->group_capab,
  3436. info->level);
  3437. if (res < 0 || res >= end - pos)
  3438. return pos - buf;
  3439. pos += res;
  3440. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3441. {
  3442. const u8 *t;
  3443. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3444. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3445. wps_dev_type_bin2str(t, devtype,
  3446. sizeof(devtype)));
  3447. if (res < 0 || res >= end - pos)
  3448. return pos - buf;
  3449. pos += res;
  3450. }
  3451. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3452. if (ssid) {
  3453. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3454. if (res < 0 || res >= end - pos)
  3455. return pos - buf;
  3456. pos += res;
  3457. }
  3458. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3459. if (res < 0)
  3460. return pos - buf;
  3461. pos += res;
  3462. return pos - buf;
  3463. }
  3464. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3465. const char *param)
  3466. {
  3467. struct wpa_freq_range *freq = NULL, *n;
  3468. unsigned int count = 0, i;
  3469. const char *pos, *pos2, *pos3;
  3470. if (wpa_s->global->p2p == NULL)
  3471. return -1;
  3472. /*
  3473. * param includes comma separated frequency range.
  3474. * For example: 2412-2432,2462,5000-6000
  3475. */
  3476. pos = param;
  3477. while (pos && pos[0]) {
  3478. n = os_realloc_array(freq, count + 1,
  3479. sizeof(struct wpa_freq_range));
  3480. if (n == NULL) {
  3481. os_free(freq);
  3482. return -1;
  3483. }
  3484. freq = n;
  3485. freq[count].min = atoi(pos);
  3486. pos2 = os_strchr(pos, '-');
  3487. pos3 = os_strchr(pos, ',');
  3488. if (pos2 && (!pos3 || pos2 < pos3)) {
  3489. pos2++;
  3490. freq[count].max = atoi(pos2);
  3491. } else
  3492. freq[count].max = freq[count].min;
  3493. pos = pos3;
  3494. if (pos)
  3495. pos++;
  3496. count++;
  3497. }
  3498. for (i = 0; i < count; i++) {
  3499. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3500. freq[i].min, freq[i].max);
  3501. }
  3502. os_free(wpa_s->global->p2p_disallow_freq);
  3503. wpa_s->global->p2p_disallow_freq = freq;
  3504. wpa_s->global->num_p2p_disallow_freq = count;
  3505. wpas_p2p_update_channel_list(wpa_s);
  3506. return 0;
  3507. }
  3508. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3509. {
  3510. char *param;
  3511. if (wpa_s->global->p2p == NULL)
  3512. return -1;
  3513. param = os_strchr(cmd, ' ');
  3514. if (param == NULL)
  3515. return -1;
  3516. *param++ = '\0';
  3517. if (os_strcmp(cmd, "discoverability") == 0) {
  3518. p2p_set_client_discoverability(wpa_s->global->p2p,
  3519. atoi(param));
  3520. return 0;
  3521. }
  3522. if (os_strcmp(cmd, "managed") == 0) {
  3523. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3524. return 0;
  3525. }
  3526. if (os_strcmp(cmd, "listen_channel") == 0) {
  3527. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3528. atoi(param));
  3529. }
  3530. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3531. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3532. os_strlen(param));
  3533. }
  3534. if (os_strcmp(cmd, "noa") == 0) {
  3535. char *pos;
  3536. int count, start, duration;
  3537. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3538. count = atoi(param);
  3539. pos = os_strchr(param, ',');
  3540. if (pos == NULL)
  3541. return -1;
  3542. pos++;
  3543. start = atoi(pos);
  3544. pos = os_strchr(pos, ',');
  3545. if (pos == NULL)
  3546. return -1;
  3547. pos++;
  3548. duration = atoi(pos);
  3549. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3550. return -1;
  3551. if (count == 0 && duration > 0)
  3552. return -1;
  3553. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3554. "start=%d duration=%d", count, start, duration);
  3555. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3556. }
  3557. if (os_strcmp(cmd, "ps") == 0)
  3558. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3559. if (os_strcmp(cmd, "oppps") == 0)
  3560. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3561. if (os_strcmp(cmd, "ctwindow") == 0)
  3562. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3563. if (os_strcmp(cmd, "disabled") == 0) {
  3564. wpa_s->global->p2p_disabled = atoi(param);
  3565. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3566. wpa_s->global->p2p_disabled ?
  3567. "disabled" : "enabled");
  3568. if (wpa_s->global->p2p_disabled) {
  3569. wpas_p2p_stop_find(wpa_s);
  3570. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3571. p2p_flush(wpa_s->global->p2p);
  3572. }
  3573. return 0;
  3574. }
  3575. if (os_strcmp(cmd, "conc_pref") == 0) {
  3576. if (os_strcmp(param, "sta") == 0)
  3577. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3578. else if (os_strcmp(param, "p2p") == 0)
  3579. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3580. else {
  3581. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3582. return -1;
  3583. }
  3584. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3585. "%s", param);
  3586. return 0;
  3587. }
  3588. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3589. wpa_s->force_long_sd = atoi(param);
  3590. return 0;
  3591. }
  3592. if (os_strcmp(cmd, "peer_filter") == 0) {
  3593. u8 addr[ETH_ALEN];
  3594. if (hwaddr_aton(param, addr))
  3595. return -1;
  3596. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3597. return 0;
  3598. }
  3599. if (os_strcmp(cmd, "cross_connect") == 0)
  3600. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3601. if (os_strcmp(cmd, "go_apsd") == 0) {
  3602. if (os_strcmp(param, "disable") == 0)
  3603. wpa_s->set_ap_uapsd = 0;
  3604. else {
  3605. wpa_s->set_ap_uapsd = 1;
  3606. wpa_s->ap_uapsd = atoi(param);
  3607. }
  3608. return 0;
  3609. }
  3610. if (os_strcmp(cmd, "client_apsd") == 0) {
  3611. if (os_strcmp(param, "disable") == 0)
  3612. wpa_s->set_sta_uapsd = 0;
  3613. else {
  3614. int be, bk, vi, vo;
  3615. char *pos;
  3616. /* format: BE,BK,VI,VO;max SP Length */
  3617. be = atoi(param);
  3618. pos = os_strchr(param, ',');
  3619. if (pos == NULL)
  3620. return -1;
  3621. pos++;
  3622. bk = atoi(pos);
  3623. pos = os_strchr(pos, ',');
  3624. if (pos == NULL)
  3625. return -1;
  3626. pos++;
  3627. vi = atoi(pos);
  3628. pos = os_strchr(pos, ',');
  3629. if (pos == NULL)
  3630. return -1;
  3631. pos++;
  3632. vo = atoi(pos);
  3633. /* ignore max SP Length for now */
  3634. wpa_s->set_sta_uapsd = 1;
  3635. wpa_s->sta_uapsd = 0;
  3636. if (be)
  3637. wpa_s->sta_uapsd |= BIT(0);
  3638. if (bk)
  3639. wpa_s->sta_uapsd |= BIT(1);
  3640. if (vi)
  3641. wpa_s->sta_uapsd |= BIT(2);
  3642. if (vo)
  3643. wpa_s->sta_uapsd |= BIT(3);
  3644. }
  3645. return 0;
  3646. }
  3647. if (os_strcmp(cmd, "disallow_freq") == 0)
  3648. return p2p_ctrl_disallow_freq(wpa_s, param);
  3649. if (os_strcmp(cmd, "disc_int") == 0) {
  3650. int min_disc_int, max_disc_int, max_disc_tu;
  3651. char *pos;
  3652. pos = param;
  3653. min_disc_int = atoi(pos);
  3654. pos = os_strchr(pos, ' ');
  3655. if (pos == NULL)
  3656. return -1;
  3657. *pos++ = '\0';
  3658. max_disc_int = atoi(pos);
  3659. pos = os_strchr(pos, ' ');
  3660. if (pos == NULL)
  3661. return -1;
  3662. *pos++ = '\0';
  3663. max_disc_tu = atoi(pos);
  3664. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3665. max_disc_int, max_disc_tu);
  3666. }
  3667. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3668. cmd);
  3669. return -1;
  3670. }
  3671. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3672. {
  3673. char *pos, *pos2;
  3674. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3675. if (cmd[0]) {
  3676. pos = os_strchr(cmd, ' ');
  3677. if (pos == NULL)
  3678. return -1;
  3679. *pos++ = '\0';
  3680. dur1 = atoi(cmd);
  3681. pos2 = os_strchr(pos, ' ');
  3682. if (pos2)
  3683. *pos2++ = '\0';
  3684. int1 = atoi(pos);
  3685. } else
  3686. pos2 = NULL;
  3687. if (pos2) {
  3688. pos = os_strchr(pos2, ' ');
  3689. if (pos == NULL)
  3690. return -1;
  3691. *pos++ = '\0';
  3692. dur2 = atoi(pos2);
  3693. int2 = atoi(pos);
  3694. }
  3695. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3696. }
  3697. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3698. {
  3699. char *pos;
  3700. unsigned int period = 0, interval = 0;
  3701. if (cmd[0]) {
  3702. pos = os_strchr(cmd, ' ');
  3703. if (pos == NULL)
  3704. return -1;
  3705. *pos++ = '\0';
  3706. period = atoi(cmd);
  3707. interval = atoi(pos);
  3708. }
  3709. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3710. }
  3711. #endif /* CONFIG_P2P */
  3712. #ifdef CONFIG_INTERWORKING
  3713. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3714. {
  3715. u8 bssid[ETH_ALEN];
  3716. struct wpa_bss *bss;
  3717. if (hwaddr_aton(dst, bssid)) {
  3718. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3719. return -1;
  3720. }
  3721. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3722. if (bss == NULL) {
  3723. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3724. MAC2STR(bssid));
  3725. return -1;
  3726. }
  3727. return interworking_connect(wpa_s, bss);
  3728. }
  3729. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3730. {
  3731. u8 dst_addr[ETH_ALEN];
  3732. int used;
  3733. char *pos;
  3734. #define MAX_ANQP_INFO_ID 100
  3735. u16 id[MAX_ANQP_INFO_ID];
  3736. size_t num_id = 0;
  3737. used = hwaddr_aton2(dst, dst_addr);
  3738. if (used < 0)
  3739. return -1;
  3740. pos = dst + used;
  3741. while (num_id < MAX_ANQP_INFO_ID) {
  3742. id[num_id] = atoi(pos);
  3743. if (id[num_id])
  3744. num_id++;
  3745. pos = os_strchr(pos + 1, ',');
  3746. if (pos == NULL)
  3747. break;
  3748. pos++;
  3749. }
  3750. if (num_id == 0)
  3751. return -1;
  3752. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3753. }
  3754. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  3755. {
  3756. u8 dst_addr[ETH_ALEN];
  3757. struct wpabuf *advproto, *query = NULL;
  3758. int used, ret = -1;
  3759. char *pos, *end;
  3760. size_t len;
  3761. used = hwaddr_aton2(cmd, dst_addr);
  3762. if (used < 0)
  3763. return -1;
  3764. pos = cmd + used;
  3765. while (*pos == ' ')
  3766. pos++;
  3767. /* Advertisement Protocol ID */
  3768. end = os_strchr(pos, ' ');
  3769. if (end)
  3770. len = end - pos;
  3771. else
  3772. len = os_strlen(pos);
  3773. if (len & 0x01)
  3774. return -1;
  3775. len /= 2;
  3776. if (len == 0)
  3777. return -1;
  3778. advproto = wpabuf_alloc(len);
  3779. if (advproto == NULL)
  3780. return -1;
  3781. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  3782. goto fail;
  3783. if (end) {
  3784. /* Optional Query Request */
  3785. pos = end + 1;
  3786. while (*pos == ' ')
  3787. pos++;
  3788. len = os_strlen(pos);
  3789. if (len) {
  3790. if (len & 0x01)
  3791. goto fail;
  3792. len /= 2;
  3793. if (len == 0)
  3794. goto fail;
  3795. query = wpabuf_alloc(len);
  3796. if (query == NULL)
  3797. goto fail;
  3798. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  3799. goto fail;
  3800. }
  3801. }
  3802. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  3803. fail:
  3804. wpabuf_free(advproto);
  3805. wpabuf_free(query);
  3806. return ret;
  3807. }
  3808. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  3809. size_t buflen)
  3810. {
  3811. u8 addr[ETH_ALEN];
  3812. int dialog_token;
  3813. int used;
  3814. char *pos;
  3815. size_t resp_len, start, requested_len;
  3816. if (!wpa_s->last_gas_resp)
  3817. return -1;
  3818. used = hwaddr_aton2(cmd, addr);
  3819. if (used < 0)
  3820. return -1;
  3821. pos = cmd + used;
  3822. while (*pos == ' ')
  3823. pos++;
  3824. dialog_token = atoi(pos);
  3825. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  3826. dialog_token != wpa_s->last_gas_dialog_token)
  3827. return -1;
  3828. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  3829. start = 0;
  3830. requested_len = resp_len;
  3831. pos = os_strchr(pos, ' ');
  3832. if (pos) {
  3833. start = atoi(pos);
  3834. if (start > resp_len)
  3835. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  3836. pos = os_strchr(pos, ',');
  3837. if (pos == NULL)
  3838. return -1;
  3839. pos++;
  3840. requested_len = atoi(pos);
  3841. if (start + requested_len > resp_len)
  3842. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  3843. }
  3844. if (requested_len * 2 + 1 > buflen)
  3845. return os_snprintf(buf, buflen, "FAIL-Too long response");
  3846. return wpa_snprintf_hex(buf, buflen,
  3847. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  3848. requested_len);
  3849. }
  3850. #endif /* CONFIG_INTERWORKING */
  3851. #ifdef CONFIG_HS20
  3852. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3853. {
  3854. u8 dst_addr[ETH_ALEN];
  3855. int used;
  3856. char *pos;
  3857. u32 subtypes = 0;
  3858. used = hwaddr_aton2(dst, dst_addr);
  3859. if (used < 0)
  3860. return -1;
  3861. pos = dst + used;
  3862. for (;;) {
  3863. int num = atoi(pos);
  3864. if (num <= 0 || num > 31)
  3865. return -1;
  3866. subtypes |= BIT(num);
  3867. pos = os_strchr(pos + 1, ',');
  3868. if (pos == NULL)
  3869. break;
  3870. pos++;
  3871. }
  3872. if (subtypes == 0)
  3873. return -1;
  3874. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  3875. }
  3876. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  3877. const u8 *addr, const char *realm)
  3878. {
  3879. u8 *buf;
  3880. size_t rlen, len;
  3881. int ret;
  3882. rlen = os_strlen(realm);
  3883. len = 3 + rlen;
  3884. buf = os_malloc(len);
  3885. if (buf == NULL)
  3886. return -1;
  3887. buf[0] = 1; /* NAI Home Realm Count */
  3888. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  3889. buf[2] = rlen;
  3890. os_memcpy(buf + 3, realm, rlen);
  3891. ret = hs20_anqp_send_req(wpa_s, addr,
  3892. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  3893. buf, len);
  3894. os_free(buf);
  3895. return ret;
  3896. }
  3897. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  3898. char *dst)
  3899. {
  3900. struct wpa_cred *cred = wpa_s->conf->cred;
  3901. u8 dst_addr[ETH_ALEN];
  3902. int used;
  3903. u8 *buf;
  3904. size_t len;
  3905. int ret;
  3906. used = hwaddr_aton2(dst, dst_addr);
  3907. if (used < 0)
  3908. return -1;
  3909. while (dst[used] == ' ')
  3910. used++;
  3911. if (os_strncmp(dst + used, "realm=", 6) == 0)
  3912. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  3913. dst + used + 6);
  3914. len = os_strlen(dst + used);
  3915. if (len == 0 && cred && cred->realm)
  3916. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  3917. if (len % 1)
  3918. return -1;
  3919. len /= 2;
  3920. buf = os_malloc(len);
  3921. if (buf == NULL)
  3922. return -1;
  3923. if (hexstr2bin(dst + used, buf, len) < 0) {
  3924. os_free(buf);
  3925. return -1;
  3926. }
  3927. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  3928. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  3929. buf, len);
  3930. os_free(buf);
  3931. return ret;
  3932. }
  3933. #endif /* CONFIG_HS20 */
  3934. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  3935. struct wpa_supplicant *wpa_s, char *cmd)
  3936. {
  3937. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  3938. return 0;
  3939. }
  3940. #ifdef CONFIG_AUTOSCAN
  3941. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  3942. char *cmd)
  3943. {
  3944. enum wpa_states state = wpa_s->wpa_state;
  3945. char *new_params = NULL;
  3946. if (os_strlen(cmd) > 0) {
  3947. new_params = os_strdup(cmd);
  3948. if (new_params == NULL)
  3949. return -1;
  3950. }
  3951. os_free(wpa_s->conf->autoscan);
  3952. wpa_s->conf->autoscan = new_params;
  3953. if (wpa_s->conf->autoscan == NULL)
  3954. autoscan_deinit(wpa_s);
  3955. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  3956. autoscan_init(wpa_s, 1);
  3957. else if (state == WPA_SCANNING)
  3958. wpa_supplicant_reinit_autoscan(wpa_s);
  3959. return 0;
  3960. }
  3961. #endif /* CONFIG_AUTOSCAN */
  3962. #ifdef CONFIG_WNM
  3963. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  3964. {
  3965. int enter;
  3966. int intval = 0;
  3967. char *pos;
  3968. int ret;
  3969. struct wpabuf *tfs_req = NULL;
  3970. if (os_strncmp(cmd, "enter", 5) == 0)
  3971. enter = 1;
  3972. else if (os_strncmp(cmd, "exit", 4) == 0)
  3973. enter = 0;
  3974. else
  3975. return -1;
  3976. pos = os_strstr(cmd, " interval=");
  3977. if (pos)
  3978. intval = atoi(pos + 10);
  3979. pos = os_strstr(cmd, " tfs_req=");
  3980. if (pos) {
  3981. char *end;
  3982. size_t len;
  3983. pos += 9;
  3984. end = os_strchr(pos, ' ');
  3985. if (end)
  3986. len = end - pos;
  3987. else
  3988. len = os_strlen(pos);
  3989. if (len & 1)
  3990. return -1;
  3991. len /= 2;
  3992. tfs_req = wpabuf_alloc(len);
  3993. if (tfs_req == NULL)
  3994. return -1;
  3995. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  3996. wpabuf_free(tfs_req);
  3997. return -1;
  3998. }
  3999. }
  4000. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4001. WNM_SLEEP_MODE_EXIT, intval,
  4002. tfs_req);
  4003. wpabuf_free(tfs_req);
  4004. return ret;
  4005. }
  4006. #endif /* CONFIG_WNM */
  4007. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4008. size_t buflen)
  4009. {
  4010. struct wpa_signal_info si;
  4011. int ret;
  4012. ret = wpa_drv_signal_poll(wpa_s, &si);
  4013. if (ret)
  4014. return -1;
  4015. ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
  4016. "NOISE=%d\nFREQUENCY=%u\n",
  4017. si.current_signal, si.current_txrate / 1000,
  4018. si.current_noise, si.frequency);
  4019. if (ret < 0 || (unsigned int) ret > buflen)
  4020. return -1;
  4021. return ret;
  4022. }
  4023. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4024. size_t buflen)
  4025. {
  4026. struct hostap_sta_driver_data sta;
  4027. int ret;
  4028. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4029. if (ret)
  4030. return -1;
  4031. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4032. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4033. if (ret < 0 || (size_t) ret > buflen)
  4034. return -1;
  4035. return ret;
  4036. }
  4037. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4038. char *buf, size_t *resp_len)
  4039. {
  4040. char *reply;
  4041. const int reply_size = 4096;
  4042. int ctrl_rsp = 0;
  4043. int reply_len;
  4044. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4045. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4046. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4047. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4048. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4049. (const u8 *) buf, os_strlen(buf));
  4050. } else {
  4051. int level = MSG_DEBUG;
  4052. if (os_strcmp(buf, "PING") == 0)
  4053. level = MSG_EXCESSIVE;
  4054. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4055. (const u8 *) buf, os_strlen(buf));
  4056. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4057. }
  4058. reply = os_malloc(reply_size);
  4059. if (reply == NULL) {
  4060. *resp_len = 1;
  4061. return NULL;
  4062. }
  4063. os_memcpy(reply, "OK\n", 3);
  4064. reply_len = 3;
  4065. if (os_strcmp(buf, "PING") == 0) {
  4066. os_memcpy(reply, "PONG\n", 5);
  4067. reply_len = 5;
  4068. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4069. reply_len = os_strlen(wpa_s->ifname);
  4070. os_memcpy(reply, wpa_s->ifname, reply_len);
  4071. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4072. if (wpa_debug_reopen_file() < 0)
  4073. reply_len = -1;
  4074. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4075. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4076. } else if (os_strcmp(buf, "MIB") == 0) {
  4077. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4078. if (reply_len >= 0) {
  4079. int res;
  4080. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4081. reply_size - reply_len);
  4082. if (res < 0)
  4083. reply_len = -1;
  4084. else
  4085. reply_len += res;
  4086. }
  4087. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4088. reply_len = wpa_supplicant_ctrl_iface_status(
  4089. wpa_s, buf + 6, reply, reply_size);
  4090. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4091. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4092. reply_size);
  4093. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4094. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4095. reply_len = -1;
  4096. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4097. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4098. reply, reply_size);
  4099. } else if (os_strcmp(buf, "LOGON") == 0) {
  4100. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4101. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4102. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4103. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4104. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4105. reply_len = -1;
  4106. else
  4107. wpas_request_connection(wpa_s);
  4108. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4109. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4110. reply_len = -1;
  4111. else if (wpa_s->disconnected)
  4112. wpas_request_connection(wpa_s);
  4113. #ifdef IEEE8021X_EAPOL
  4114. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4115. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4116. reply_len = -1;
  4117. #endif /* IEEE8021X_EAPOL */
  4118. #ifdef CONFIG_PEERKEY
  4119. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4120. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4121. reply_len = -1;
  4122. #endif /* CONFIG_PEERKEY */
  4123. #ifdef CONFIG_IEEE80211R
  4124. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4125. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4126. reply_len = -1;
  4127. #endif /* CONFIG_IEEE80211R */
  4128. #ifdef CONFIG_WPS
  4129. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4130. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4131. if (res == -2) {
  4132. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4133. reply_len = 17;
  4134. } else if (res)
  4135. reply_len = -1;
  4136. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4137. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4138. if (res == -2) {
  4139. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4140. reply_len = 17;
  4141. } else if (res)
  4142. reply_len = -1;
  4143. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4144. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4145. reply,
  4146. reply_size);
  4147. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4148. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4149. wpa_s, buf + 14, reply, reply_size);
  4150. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4151. if (wpas_wps_cancel(wpa_s))
  4152. reply_len = -1;
  4153. #ifdef CONFIG_WPS_NFC
  4154. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4155. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4156. reply_len = -1;
  4157. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4158. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4159. reply_len = -1;
  4160. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4161. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4162. wpa_s, buf + 14, reply, reply_size);
  4163. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4164. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4165. buf + 17))
  4166. reply_len = -1;
  4167. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4168. reply_len = wpas_ctrl_nfc_get_handover_req(
  4169. wpa_s, buf + 21, reply, reply_size);
  4170. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4171. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4172. wpa_s, buf + 21, reply, reply_size);
  4173. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4174. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4175. wpa_s, buf + 20, reply, reply_size);
  4176. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4177. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4178. reply_len = -1;
  4179. #endif /* CONFIG_WPS_NFC */
  4180. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4181. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4182. reply_len = -1;
  4183. #ifdef CONFIG_AP
  4184. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4185. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4186. wpa_s, buf + 11, reply, reply_size);
  4187. #endif /* CONFIG_AP */
  4188. #ifdef CONFIG_WPS_ER
  4189. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4190. if (wpas_wps_er_start(wpa_s, NULL))
  4191. reply_len = -1;
  4192. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4193. if (wpas_wps_er_start(wpa_s, buf + 13))
  4194. reply_len = -1;
  4195. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4196. if (wpas_wps_er_stop(wpa_s))
  4197. reply_len = -1;
  4198. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4199. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4200. reply_len = -1;
  4201. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4202. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4203. if (ret == -2) {
  4204. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4205. reply_len = 17;
  4206. } else if (ret == -3) {
  4207. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4208. reply_len = 18;
  4209. } else if (ret == -4) {
  4210. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4211. reply_len = 20;
  4212. } else if (ret)
  4213. reply_len = -1;
  4214. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4215. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4216. reply_len = -1;
  4217. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4218. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4219. buf + 18))
  4220. reply_len = -1;
  4221. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4222. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4223. reply_len = -1;
  4224. #ifdef CONFIG_WPS_NFC
  4225. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4226. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4227. wpa_s, buf + 24, reply, reply_size);
  4228. #endif /* CONFIG_WPS_NFC */
  4229. #endif /* CONFIG_WPS_ER */
  4230. #endif /* CONFIG_WPS */
  4231. #ifdef CONFIG_IBSS_RSN
  4232. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4233. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4234. reply_len = -1;
  4235. #endif /* CONFIG_IBSS_RSN */
  4236. #ifdef CONFIG_P2P
  4237. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4238. if (p2p_ctrl_find(wpa_s, buf + 9))
  4239. reply_len = -1;
  4240. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4241. if (p2p_ctrl_find(wpa_s, ""))
  4242. reply_len = -1;
  4243. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4244. wpas_p2p_stop_find(wpa_s);
  4245. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4246. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4247. reply_size);
  4248. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4249. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4250. reply_len = -1;
  4251. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4252. if (p2p_ctrl_listen(wpa_s, ""))
  4253. reply_len = -1;
  4254. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4255. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4256. reply_len = -1;
  4257. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4258. if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
  4259. reply_len = -1;
  4260. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4261. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4262. reply_len = -1;
  4263. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4264. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4265. reply_len = -1;
  4266. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4267. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4268. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4269. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4270. reply_size);
  4271. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4272. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4273. reply_len = -1;
  4274. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4275. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4276. reply_len = -1;
  4277. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4278. wpas_p2p_sd_service_update(wpa_s);
  4279. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4280. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4281. reply_len = -1;
  4282. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4283. wpas_p2p_service_flush(wpa_s);
  4284. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4285. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4286. reply_len = -1;
  4287. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4288. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4289. reply_len = -1;
  4290. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4291. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4292. reply_len = -1;
  4293. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4294. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4295. reply_len = -1;
  4296. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4297. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4298. reply_size);
  4299. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4300. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4301. reply_len = -1;
  4302. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4303. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4304. wpa_s->force_long_sd = 0;
  4305. if (wpa_s->global->p2p)
  4306. p2p_flush(wpa_s->global->p2p);
  4307. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4308. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4309. reply_len = -1;
  4310. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4311. if (wpas_p2p_cancel(wpa_s))
  4312. reply_len = -1;
  4313. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4314. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4315. reply_len = -1;
  4316. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4317. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4318. reply_len = -1;
  4319. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4320. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4321. reply_len = -1;
  4322. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4323. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4324. reply_len = -1;
  4325. #endif /* CONFIG_P2P */
  4326. #ifdef CONFIG_WIFI_DISPLAY
  4327. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4328. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4329. reply_len = -1;
  4330. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4331. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4332. reply, reply_size);
  4333. #endif /* CONFIG_WIFI_DISPLAY */
  4334. #ifdef CONFIG_INTERWORKING
  4335. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4336. if (interworking_fetch_anqp(wpa_s) < 0)
  4337. reply_len = -1;
  4338. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4339. interworking_stop_fetch_anqp(wpa_s);
  4340. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4341. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4342. NULL) < 0)
  4343. reply_len = -1;
  4344. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4345. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4346. reply_len = -1;
  4347. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4348. if (get_anqp(wpa_s, buf + 9) < 0)
  4349. reply_len = -1;
  4350. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4351. if (gas_request(wpa_s, buf + 12) < 0)
  4352. reply_len = -1;
  4353. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4354. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4355. reply_size);
  4356. #endif /* CONFIG_INTERWORKING */
  4357. #ifdef CONFIG_HS20
  4358. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4359. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4360. reply_len = -1;
  4361. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4362. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4363. reply_len = -1;
  4364. #endif /* CONFIG_HS20 */
  4365. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4366. {
  4367. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4368. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4369. reply_len = -1;
  4370. else
  4371. ctrl_rsp = 1;
  4372. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4373. if (wpa_supplicant_reload_configuration(wpa_s))
  4374. reply_len = -1;
  4375. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4376. wpa_supplicant_terminate_proc(wpa_s->global);
  4377. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4378. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4379. reply_len = -1;
  4380. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4381. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4382. wpa_s, buf + 9, reply, reply_size);
  4383. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4384. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4385. wpa_s, buf + 9, reply, reply_size);
  4386. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4387. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4388. wpa_s, reply, reply_size);
  4389. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4390. #ifdef CONFIG_SME
  4391. wpa_s->sme.prev_bssid_set = 0;
  4392. #endif /* CONFIG_SME */
  4393. wpa_s->reassociate = 0;
  4394. wpa_s->disconnected = 1;
  4395. wpa_supplicant_cancel_sched_scan(wpa_s);
  4396. wpa_supplicant_cancel_scan(wpa_s);
  4397. wpa_supplicant_deauthenticate(wpa_s,
  4398. WLAN_REASON_DEAUTH_LEAVING);
  4399. } else if (os_strcmp(buf, "SCAN") == 0) {
  4400. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4401. reply_len = -1;
  4402. else {
  4403. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4404. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4405. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4406. wpa_s->normal_scans = 0;
  4407. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4408. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4409. } else if (wpa_s->sched_scanning) {
  4410. wpa_printf(MSG_DEBUG, "Stop ongoing "
  4411. "sched_scan to allow requested "
  4412. "full scan to proceed");
  4413. wpa_supplicant_cancel_sched_scan(wpa_s);
  4414. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4415. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4416. } else {
  4417. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  4418. "reject new request");
  4419. reply_len = os_snprintf(reply, reply_size,
  4420. "FAIL-BUSY\n");
  4421. }
  4422. }
  4423. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4424. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4425. wpa_s, reply, reply_size);
  4426. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4427. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4428. reply_len = -1;
  4429. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4430. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4431. reply_len = -1;
  4432. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4433. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4434. reply_len = -1;
  4435. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4436. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4437. wpa_s, reply, reply_size);
  4438. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4439. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4440. reply_len = -1;
  4441. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4442. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4443. reply_len = -1;
  4444. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4445. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4446. wpa_s, buf + 12, reply, reply_size);
  4447. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4448. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4449. wpa_s, reply, reply_size);
  4450. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4451. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4452. wpa_s, reply, reply_size);
  4453. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4454. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4455. reply_len = -1;
  4456. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4457. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4458. reply_len = -1;
  4459. #ifndef CONFIG_NO_CONFIG_WRITE
  4460. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4461. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4462. reply_len = -1;
  4463. #endif /* CONFIG_NO_CONFIG_WRITE */
  4464. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4465. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4466. wpa_s, buf + 15, reply, reply_size);
  4467. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4468. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4469. reply_len = -1;
  4470. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4471. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4472. reply_len = -1;
  4473. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4474. reply_len = wpa_supplicant_global_iface_list(
  4475. wpa_s->global, reply, reply_size);
  4476. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4477. reply_len = wpa_supplicant_global_iface_interfaces(
  4478. wpa_s->global, reply, reply_size);
  4479. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4480. reply_len = wpa_supplicant_ctrl_iface_bss(
  4481. wpa_s, buf + 4, reply, reply_size);
  4482. #ifdef CONFIG_AP
  4483. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4484. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4485. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4486. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4487. reply_size);
  4488. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4489. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4490. reply_size);
  4491. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4492. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4493. reply_len = -1;
  4494. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4495. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4496. reply_len = -1;
  4497. #endif /* CONFIG_AP */
  4498. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4499. wpas_notify_suspend(wpa_s->global);
  4500. } else if (os_strcmp(buf, "RESUME") == 0) {
  4501. wpas_notify_resume(wpa_s->global);
  4502. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4503. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4504. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4505. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4506. reply_len = -1;
  4507. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4508. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4509. reply_len = -1;
  4510. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4511. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4512. reply_len = -1;
  4513. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4514. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4515. buf + 17))
  4516. reply_len = -1;
  4517. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4518. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4519. reply_len = -1;
  4520. #ifdef CONFIG_TDLS
  4521. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4522. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4523. reply_len = -1;
  4524. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4525. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4526. reply_len = -1;
  4527. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4528. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4529. reply_len = -1;
  4530. #endif /* CONFIG_TDLS */
  4531. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4532. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4533. reply_size);
  4534. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4535. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4536. reply_size);
  4537. #ifdef CONFIG_AUTOSCAN
  4538. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4539. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4540. reply_len = -1;
  4541. #endif /* CONFIG_AUTOSCAN */
  4542. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4543. pmksa_cache_clear_current(wpa_s->wpa);
  4544. eapol_sm_request_reauth(wpa_s->eapol);
  4545. #ifdef CONFIG_WNM
  4546. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  4547. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  4548. reply_len = -1;
  4549. #endif /* CONFIG_WNM */
  4550. } else {
  4551. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4552. reply_len = 16;
  4553. }
  4554. if (reply_len < 0) {
  4555. os_memcpy(reply, "FAIL\n", 5);
  4556. reply_len = 5;
  4557. }
  4558. if (ctrl_rsp)
  4559. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4560. *resp_len = reply_len;
  4561. return reply;
  4562. }
  4563. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  4564. char *cmd)
  4565. {
  4566. struct wpa_interface iface;
  4567. char *pos;
  4568. /*
  4569. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  4570. * TAB<bridge_ifname>
  4571. */
  4572. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  4573. os_memset(&iface, 0, sizeof(iface));
  4574. do {
  4575. iface.ifname = pos = cmd;
  4576. pos = os_strchr(pos, '\t');
  4577. if (pos)
  4578. *pos++ = '\0';
  4579. if (iface.ifname[0] == '\0')
  4580. return -1;
  4581. if (pos == NULL)
  4582. break;
  4583. iface.confname = pos;
  4584. pos = os_strchr(pos, '\t');
  4585. if (pos)
  4586. *pos++ = '\0';
  4587. if (iface.confname[0] == '\0')
  4588. iface.confname = NULL;
  4589. if (pos == NULL)
  4590. break;
  4591. iface.driver = pos;
  4592. pos = os_strchr(pos, '\t');
  4593. if (pos)
  4594. *pos++ = '\0';
  4595. if (iface.driver[0] == '\0')
  4596. iface.driver = NULL;
  4597. if (pos == NULL)
  4598. break;
  4599. iface.ctrl_interface = pos;
  4600. pos = os_strchr(pos, '\t');
  4601. if (pos)
  4602. *pos++ = '\0';
  4603. if (iface.ctrl_interface[0] == '\0')
  4604. iface.ctrl_interface = NULL;
  4605. if (pos == NULL)
  4606. break;
  4607. iface.driver_param = pos;
  4608. pos = os_strchr(pos, '\t');
  4609. if (pos)
  4610. *pos++ = '\0';
  4611. if (iface.driver_param[0] == '\0')
  4612. iface.driver_param = NULL;
  4613. if (pos == NULL)
  4614. break;
  4615. iface.bridge_ifname = pos;
  4616. pos = os_strchr(pos, '\t');
  4617. if (pos)
  4618. *pos++ = '\0';
  4619. if (iface.bridge_ifname[0] == '\0')
  4620. iface.bridge_ifname = NULL;
  4621. if (pos == NULL)
  4622. break;
  4623. } while (0);
  4624. if (wpa_supplicant_get_iface(global, iface.ifname))
  4625. return -1;
  4626. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  4627. }
  4628. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  4629. char *cmd)
  4630. {
  4631. struct wpa_supplicant *wpa_s;
  4632. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  4633. wpa_s = wpa_supplicant_get_iface(global, cmd);
  4634. if (wpa_s == NULL)
  4635. return -1;
  4636. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  4637. }
  4638. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  4639. {
  4640. struct wpa_interface_info *prev;
  4641. while (iface) {
  4642. prev = iface;
  4643. iface = iface->next;
  4644. os_free(prev->ifname);
  4645. os_free(prev->desc);
  4646. os_free(prev);
  4647. }
  4648. }
  4649. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  4650. char *buf, int len)
  4651. {
  4652. int i, res;
  4653. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  4654. char *pos, *end;
  4655. for (i = 0; wpa_drivers[i]; i++) {
  4656. struct wpa_driver_ops *drv = wpa_drivers[i];
  4657. if (drv->get_interfaces == NULL)
  4658. continue;
  4659. tmp = drv->get_interfaces(global->drv_priv[i]);
  4660. if (tmp == NULL)
  4661. continue;
  4662. if (last == NULL)
  4663. iface = last = tmp;
  4664. else
  4665. last->next = tmp;
  4666. while (last->next)
  4667. last = last->next;
  4668. }
  4669. pos = buf;
  4670. end = buf + len;
  4671. for (tmp = iface; tmp; tmp = tmp->next) {
  4672. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  4673. tmp->drv_name, tmp->ifname,
  4674. tmp->desc ? tmp->desc : "");
  4675. if (res < 0 || res >= end - pos) {
  4676. *pos = '\0';
  4677. break;
  4678. }
  4679. pos += res;
  4680. }
  4681. wpa_free_iface_info(iface);
  4682. return pos - buf;
  4683. }
  4684. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  4685. char *buf, int len)
  4686. {
  4687. int res;
  4688. char *pos, *end;
  4689. struct wpa_supplicant *wpa_s;
  4690. wpa_s = global->ifaces;
  4691. pos = buf;
  4692. end = buf + len;
  4693. while (wpa_s) {
  4694. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  4695. if (res < 0 || res >= end - pos) {
  4696. *pos = '\0';
  4697. break;
  4698. }
  4699. pos += res;
  4700. wpa_s = wpa_s->next;
  4701. }
  4702. return pos - buf;
  4703. }
  4704. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  4705. char *buf, size_t *resp_len)
  4706. {
  4707. char *reply;
  4708. const int reply_size = 2048;
  4709. int reply_len;
  4710. int level = MSG_DEBUG;
  4711. if (os_strcmp(buf, "PING") == 0)
  4712. level = MSG_EXCESSIVE;
  4713. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  4714. (const u8 *) buf, os_strlen(buf));
  4715. reply = os_malloc(reply_size);
  4716. if (reply == NULL) {
  4717. *resp_len = 1;
  4718. return NULL;
  4719. }
  4720. os_memcpy(reply, "OK\n", 3);
  4721. reply_len = 3;
  4722. if (os_strcmp(buf, "PING") == 0) {
  4723. os_memcpy(reply, "PONG\n", 5);
  4724. reply_len = 5;
  4725. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  4726. if (wpa_supplicant_global_iface_add(global, buf + 14))
  4727. reply_len = -1;
  4728. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  4729. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  4730. reply_len = -1;
  4731. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4732. reply_len = wpa_supplicant_global_iface_list(
  4733. global, reply, reply_size);
  4734. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4735. reply_len = wpa_supplicant_global_iface_interfaces(
  4736. global, reply, reply_size);
  4737. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4738. wpa_supplicant_terminate_proc(global);
  4739. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4740. wpas_notify_suspend(global);
  4741. } else if (os_strcmp(buf, "RESUME") == 0) {
  4742. wpas_notify_resume(global);
  4743. } else {
  4744. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4745. reply_len = 16;
  4746. }
  4747. if (reply_len < 0) {
  4748. os_memcpy(reply, "FAIL\n", 5);
  4749. reply_len = 5;
  4750. }
  4751. *resp_len = reply_len;
  4752. return reply;
  4753. }