ctrl_iface.c 103 KB

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