ctrl_iface.c 78 KB

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