ctrl_iface.c 79 KB

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