ctrl_iface.c 87 KB

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