ctrl_iface.c 80 KB

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