ctrl_iface.c 75 KB

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