ctrl_iface.c 80 KB

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