ctrl_iface.c 77 KB

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