ctrl_iface.c 120 KB

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