ctrl_iface.c 94 KB

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