ctrl_iface.c 85 KB

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