ctrl_iface.c 85 KB

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