ctrl_iface.c 97 KB

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