ctrl_iface.c 88 KB

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