ctrl_iface.c 98 KB

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