ctrl_iface.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255
  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. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  1632. char *buf, size_t buflen)
  1633. {
  1634. char *pos, *end;
  1635. struct wpa_cred *cred;
  1636. int ret;
  1637. pos = buf;
  1638. end = buf + buflen;
  1639. ret = os_snprintf(pos, end - pos,
  1640. "cred id / realm / username / domain / imsi\n");
  1641. if (ret < 0 || ret >= end - pos)
  1642. return pos - buf;
  1643. pos += ret;
  1644. cred = wpa_s->conf->cred;
  1645. while (cred) {
  1646. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  1647. cred->id, cred->realm ? cred->realm : "",
  1648. cred->username ? cred->username : "",
  1649. cred->domain ? cred->domain : "",
  1650. cred->imsi ? cred->imsi : "");
  1651. if (ret < 0 || ret >= end - pos)
  1652. return pos - buf;
  1653. pos += ret;
  1654. cred = cred->next;
  1655. }
  1656. return pos - buf;
  1657. }
  1658. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  1659. char *buf, size_t buflen)
  1660. {
  1661. struct wpa_cred *cred;
  1662. int ret;
  1663. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  1664. cred = wpa_config_add_cred(wpa_s->conf);
  1665. if (cred == NULL)
  1666. return -1;
  1667. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  1668. if (ret < 0 || (size_t) ret >= buflen)
  1669. return -1;
  1670. return ret;
  1671. }
  1672. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  1673. char *cmd)
  1674. {
  1675. int id;
  1676. struct wpa_cred *cred;
  1677. /* cmd: "<cred id>" or "all" */
  1678. if (os_strcmp(cmd, "all") == 0) {
  1679. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  1680. cred = wpa_s->conf->cred;
  1681. while (cred) {
  1682. id = cred->id;
  1683. cred = cred->next;
  1684. wpa_config_remove_cred(wpa_s->conf, id);
  1685. }
  1686. return 0;
  1687. }
  1688. id = atoi(cmd);
  1689. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  1690. cred = wpa_config_get_cred(wpa_s->conf, id);
  1691. if (cred == NULL ||
  1692. wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  1693. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  1694. id);
  1695. return -1;
  1696. }
  1697. return 0;
  1698. }
  1699. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  1700. char *cmd)
  1701. {
  1702. int id;
  1703. struct wpa_cred *cred;
  1704. char *name, *value;
  1705. /* cmd: "<cred id> <variable name> <value>" */
  1706. name = os_strchr(cmd, ' ');
  1707. if (name == NULL)
  1708. return -1;
  1709. *name++ = '\0';
  1710. value = os_strchr(name, ' ');
  1711. if (value == NULL)
  1712. return -1;
  1713. *value++ = '\0';
  1714. id = atoi(cmd);
  1715. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  1716. id, name);
  1717. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1718. (u8 *) value, os_strlen(value));
  1719. cred = wpa_config_get_cred(wpa_s->conf, id);
  1720. if (cred == NULL) {
  1721. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  1722. id);
  1723. return -1;
  1724. }
  1725. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  1726. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  1727. "variable '%s'", name);
  1728. return -1;
  1729. }
  1730. return 0;
  1731. }
  1732. #ifndef CONFIG_NO_CONFIG_WRITE
  1733. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1734. {
  1735. int ret;
  1736. if (!wpa_s->conf->update_config) {
  1737. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1738. "to update configuration (update_config=0)");
  1739. return -1;
  1740. }
  1741. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1742. if (ret) {
  1743. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1744. "update configuration");
  1745. } else {
  1746. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1747. " updated");
  1748. }
  1749. return ret;
  1750. }
  1751. #endif /* CONFIG_NO_CONFIG_WRITE */
  1752. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1753. struct wpa_driver_capa *capa,
  1754. char *buf, size_t buflen)
  1755. {
  1756. int ret, first = 1;
  1757. char *pos, *end;
  1758. size_t len;
  1759. pos = buf;
  1760. end = pos + buflen;
  1761. if (res < 0) {
  1762. if (strict)
  1763. return 0;
  1764. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1765. if (len >= buflen)
  1766. return -1;
  1767. return len;
  1768. }
  1769. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1770. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1771. if (ret < 0 || ret >= end - pos)
  1772. return pos - buf;
  1773. pos += ret;
  1774. first = 0;
  1775. }
  1776. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1777. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1778. if (ret < 0 || ret >= end - pos)
  1779. return pos - buf;
  1780. pos += ret;
  1781. first = 0;
  1782. }
  1783. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1784. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1785. if (ret < 0 || ret >= end - pos)
  1786. return pos - buf;
  1787. pos += ret;
  1788. first = 0;
  1789. }
  1790. return pos - buf;
  1791. }
  1792. static int ctrl_iface_get_capability_group(int res, char *strict,
  1793. struct wpa_driver_capa *capa,
  1794. char *buf, size_t buflen)
  1795. {
  1796. int ret, first = 1;
  1797. char *pos, *end;
  1798. size_t len;
  1799. pos = buf;
  1800. end = pos + buflen;
  1801. if (res < 0) {
  1802. if (strict)
  1803. return 0;
  1804. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1805. if (len >= buflen)
  1806. return -1;
  1807. return len;
  1808. }
  1809. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1810. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1811. if (ret < 0 || ret >= end - pos)
  1812. return pos - buf;
  1813. pos += ret;
  1814. first = 0;
  1815. }
  1816. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1817. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1818. if (ret < 0 || ret >= end - pos)
  1819. return pos - buf;
  1820. pos += ret;
  1821. first = 0;
  1822. }
  1823. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1824. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1825. first ? "" : " ");
  1826. if (ret < 0 || ret >= end - pos)
  1827. return pos - buf;
  1828. pos += ret;
  1829. first = 0;
  1830. }
  1831. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1832. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1833. if (ret < 0 || ret >= end - pos)
  1834. return pos - buf;
  1835. pos += ret;
  1836. first = 0;
  1837. }
  1838. return pos - buf;
  1839. }
  1840. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1841. struct wpa_driver_capa *capa,
  1842. char *buf, size_t buflen)
  1843. {
  1844. int ret;
  1845. char *pos, *end;
  1846. size_t len;
  1847. pos = buf;
  1848. end = pos + buflen;
  1849. if (res < 0) {
  1850. if (strict)
  1851. return 0;
  1852. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1853. "NONE", buflen);
  1854. if (len >= buflen)
  1855. return -1;
  1856. return len;
  1857. }
  1858. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1859. if (ret < 0 || ret >= end - pos)
  1860. return pos - buf;
  1861. pos += ret;
  1862. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1863. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1864. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1865. if (ret < 0 || ret >= end - pos)
  1866. return pos - buf;
  1867. pos += ret;
  1868. }
  1869. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1870. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1871. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1872. if (ret < 0 || ret >= end - pos)
  1873. return pos - buf;
  1874. pos += ret;
  1875. }
  1876. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1877. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1878. if (ret < 0 || ret >= end - pos)
  1879. return pos - buf;
  1880. pos += ret;
  1881. }
  1882. return pos - buf;
  1883. }
  1884. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1885. struct wpa_driver_capa *capa,
  1886. char *buf, size_t buflen)
  1887. {
  1888. int ret, first = 1;
  1889. char *pos, *end;
  1890. size_t len;
  1891. pos = buf;
  1892. end = pos + buflen;
  1893. if (res < 0) {
  1894. if (strict)
  1895. return 0;
  1896. len = os_strlcpy(buf, "RSN WPA", buflen);
  1897. if (len >= buflen)
  1898. return -1;
  1899. return len;
  1900. }
  1901. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1902. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1903. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1904. if (ret < 0 || ret >= end - pos)
  1905. return pos - buf;
  1906. pos += ret;
  1907. first = 0;
  1908. }
  1909. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1910. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1911. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1912. if (ret < 0 || ret >= end - pos)
  1913. return pos - buf;
  1914. pos += ret;
  1915. first = 0;
  1916. }
  1917. return pos - buf;
  1918. }
  1919. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1920. struct wpa_driver_capa *capa,
  1921. char *buf, size_t buflen)
  1922. {
  1923. int ret, first = 1;
  1924. char *pos, *end;
  1925. size_t len;
  1926. pos = buf;
  1927. end = pos + buflen;
  1928. if (res < 0) {
  1929. if (strict)
  1930. return 0;
  1931. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1932. if (len >= buflen)
  1933. return -1;
  1934. return len;
  1935. }
  1936. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1937. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1938. if (ret < 0 || ret >= end - pos)
  1939. return pos - buf;
  1940. pos += ret;
  1941. first = 0;
  1942. }
  1943. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1944. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1945. first ? "" : " ");
  1946. if (ret < 0 || ret >= end - pos)
  1947. return pos - buf;
  1948. pos += ret;
  1949. first = 0;
  1950. }
  1951. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1952. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1953. if (ret < 0 || ret >= end - pos)
  1954. return pos - buf;
  1955. pos += ret;
  1956. first = 0;
  1957. }
  1958. return pos - buf;
  1959. }
  1960. static int wpa_supplicant_ctrl_iface_get_capability(
  1961. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1962. size_t buflen)
  1963. {
  1964. struct wpa_driver_capa capa;
  1965. int res;
  1966. char *strict;
  1967. char field[30];
  1968. size_t len;
  1969. /* Determine whether or not strict checking was requested */
  1970. len = os_strlcpy(field, _field, sizeof(field));
  1971. if (len >= sizeof(field))
  1972. return -1;
  1973. strict = os_strchr(field, ' ');
  1974. if (strict != NULL) {
  1975. *strict++ = '\0';
  1976. if (os_strcmp(strict, "strict") != 0)
  1977. return -1;
  1978. }
  1979. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1980. field, strict ? strict : "");
  1981. if (os_strcmp(field, "eap") == 0) {
  1982. return eap_get_names(buf, buflen);
  1983. }
  1984. res = wpa_drv_get_capa(wpa_s, &capa);
  1985. if (os_strcmp(field, "pairwise") == 0)
  1986. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1987. buf, buflen);
  1988. if (os_strcmp(field, "group") == 0)
  1989. return ctrl_iface_get_capability_group(res, strict, &capa,
  1990. buf, buflen);
  1991. if (os_strcmp(field, "key_mgmt") == 0)
  1992. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1993. buf, buflen);
  1994. if (os_strcmp(field, "proto") == 0)
  1995. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1996. buf, buflen);
  1997. if (os_strcmp(field, "auth_alg") == 0)
  1998. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1999. buf, buflen);
  2000. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2001. field);
  2002. return -1;
  2003. }
  2004. #ifdef CONFIG_INTERWORKING
  2005. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2006. struct wpabuf *data)
  2007. {
  2008. char *start = pos;
  2009. size_t i;
  2010. int ret;
  2011. const u8 *d;
  2012. if (data == NULL)
  2013. return start;
  2014. ret = os_snprintf(pos, end - pos, "%s=", title);
  2015. if (ret < 0 || ret >= end - pos)
  2016. return start;
  2017. pos += ret;
  2018. d = wpabuf_head_u8(data);
  2019. for (i = 0; i < wpabuf_len(data); i++) {
  2020. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2021. if (ret < 0 || ret >= end - pos)
  2022. return start;
  2023. pos += ret;
  2024. }
  2025. ret = os_snprintf(pos, end - pos, "\n");
  2026. if (ret < 0 || ret >= end - pos)
  2027. return start;
  2028. pos += ret;
  2029. return pos;
  2030. }
  2031. #endif /* CONFIG_INTERWORKING */
  2032. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2033. const char *cmd, char *buf,
  2034. size_t buflen)
  2035. {
  2036. u8 bssid[ETH_ALEN];
  2037. size_t i;
  2038. struct wpa_bss *bss;
  2039. int ret;
  2040. char *pos, *end;
  2041. const u8 *ie, *ie2;
  2042. struct os_time now;
  2043. if (os_strcmp(cmd, "FIRST") == 0)
  2044. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  2045. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2046. i = atoi(cmd + 3);
  2047. bss = wpa_bss_get_id(wpa_s, i);
  2048. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2049. i = atoi(cmd + 5);
  2050. bss = wpa_bss_get_id(wpa_s, i);
  2051. if (bss) {
  2052. struct dl_list *next = bss->list_id.next;
  2053. if (next == &wpa_s->bss_id)
  2054. bss = NULL;
  2055. else
  2056. bss = dl_list_entry(next, struct wpa_bss,
  2057. list_id);
  2058. }
  2059. #ifdef CONFIG_P2P
  2060. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2061. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2062. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2063. else
  2064. bss = NULL;
  2065. #endif /* CONFIG_P2P */
  2066. } else if (hwaddr_aton(cmd, bssid) == 0)
  2067. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2068. else {
  2069. struct wpa_bss *tmp;
  2070. i = atoi(cmd);
  2071. bss = NULL;
  2072. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2073. {
  2074. if (i-- == 0) {
  2075. bss = tmp;
  2076. break;
  2077. }
  2078. }
  2079. }
  2080. if (bss == NULL)
  2081. return 0;
  2082. os_get_time(&now);
  2083. pos = buf;
  2084. end = buf + buflen;
  2085. ret = os_snprintf(pos, end - pos,
  2086. "id=%u\n"
  2087. "bssid=" MACSTR "\n"
  2088. "freq=%d\n"
  2089. "beacon_int=%d\n"
  2090. "capabilities=0x%04x\n"
  2091. "qual=%d\n"
  2092. "noise=%d\n"
  2093. "level=%d\n"
  2094. "tsf=%016llu\n"
  2095. "age=%d\n"
  2096. "ie=",
  2097. bss->id,
  2098. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  2099. bss->caps, bss->qual, bss->noise, bss->level,
  2100. (unsigned long long) bss->tsf,
  2101. (int) (now.sec - bss->last_update.sec));
  2102. if (ret < 0 || ret >= end - pos)
  2103. return pos - buf;
  2104. pos += ret;
  2105. ie = (const u8 *) (bss + 1);
  2106. for (i = 0; i < bss->ie_len; i++) {
  2107. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2108. if (ret < 0 || ret >= end - pos)
  2109. return pos - buf;
  2110. pos += ret;
  2111. }
  2112. ret = os_snprintf(pos, end - pos, "\n");
  2113. if (ret < 0 || ret >= end - pos)
  2114. return pos - buf;
  2115. pos += ret;
  2116. ret = os_snprintf(pos, end - pos, "flags=");
  2117. if (ret < 0 || ret >= end - pos)
  2118. return pos - buf;
  2119. pos += ret;
  2120. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2121. if (ie)
  2122. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  2123. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2124. if (ie2)
  2125. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  2126. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2127. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2128. ret = os_snprintf(pos, end - pos, "[WEP]");
  2129. if (ret < 0 || ret >= end - pos)
  2130. return pos - buf;
  2131. pos += ret;
  2132. }
  2133. if (bss->caps & IEEE80211_CAP_IBSS) {
  2134. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2135. if (ret < 0 || ret >= end - pos)
  2136. return pos - buf;
  2137. pos += ret;
  2138. }
  2139. if (bss->caps & IEEE80211_CAP_ESS) {
  2140. ret = os_snprintf(pos, end - pos, "[ESS]");
  2141. if (ret < 0 || ret >= end - pos)
  2142. return pos - buf;
  2143. pos += ret;
  2144. }
  2145. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  2146. ret = os_snprintf(pos, end - pos, "[P2P]");
  2147. if (ret < 0 || ret >= end - pos)
  2148. return pos - buf;
  2149. pos += ret;
  2150. }
  2151. ret = os_snprintf(pos, end - pos, "\n");
  2152. if (ret < 0 || ret >= end - pos)
  2153. return pos - buf;
  2154. pos += ret;
  2155. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2156. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2157. if (ret < 0 || ret >= end - pos)
  2158. return pos - buf;
  2159. pos += ret;
  2160. #ifdef CONFIG_WPS
  2161. ie = (const u8 *) (bss + 1);
  2162. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2163. if (ret < 0 || ret >= end - pos)
  2164. return pos - buf;
  2165. pos += ret;
  2166. #endif /* CONFIG_WPS */
  2167. #ifdef CONFIG_P2P
  2168. ie = (const u8 *) (bss + 1);
  2169. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2170. if (ret < 0 || ret >= end - pos)
  2171. return pos - buf;
  2172. pos += ret;
  2173. #endif /* CONFIG_P2P */
  2174. #ifdef CONFIG_INTERWORKING
  2175. pos = anqp_add_hex(pos, end, "anqp_venue_name", bss->anqp_venue_name);
  2176. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2177. bss->anqp_network_auth_type);
  2178. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2179. bss->anqp_roaming_consortium);
  2180. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2181. bss->anqp_ip_addr_type_availability);
  2182. pos = anqp_add_hex(pos, end, "anqp_nai_realm", bss->anqp_nai_realm);
  2183. pos = anqp_add_hex(pos, end, "anqp_3gpp", bss->anqp_3gpp);
  2184. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2185. bss->anqp_domain_name);
  2186. #endif /* CONFIG_INTERWORKING */
  2187. return pos - buf;
  2188. }
  2189. static int wpa_supplicant_ctrl_iface_ap_scan(
  2190. struct wpa_supplicant *wpa_s, char *cmd)
  2191. {
  2192. int ap_scan = atoi(cmd);
  2193. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2194. }
  2195. static int wpa_supplicant_ctrl_iface_scan_interval(
  2196. struct wpa_supplicant *wpa_s, char *cmd)
  2197. {
  2198. int scan_int = atoi(cmd);
  2199. if (scan_int < 0)
  2200. return -1;
  2201. wpa_s->scan_interval = scan_int;
  2202. return 0;
  2203. }
  2204. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  2205. struct wpa_supplicant *wpa_s, char *cmd)
  2206. {
  2207. int expire_age = atoi(cmd);
  2208. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  2209. }
  2210. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  2211. struct wpa_supplicant *wpa_s, char *cmd)
  2212. {
  2213. int expire_count = atoi(cmd);
  2214. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  2215. }
  2216. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  2217. {
  2218. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  2219. /* MLME-DELETEKEYS.request */
  2220. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  2221. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  2222. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  2223. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  2224. #ifdef CONFIG_IEEE80211W
  2225. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  2226. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  2227. #endif /* CONFIG_IEEE80211W */
  2228. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  2229. 0);
  2230. /* MLME-SETPROTECTION.request(None) */
  2231. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  2232. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  2233. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  2234. wpa_sm_drop_sa(wpa_s->wpa);
  2235. }
  2236. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  2237. char *addr)
  2238. {
  2239. #ifdef CONFIG_NO_SCAN_PROCESSING
  2240. return -1;
  2241. #else /* CONFIG_NO_SCAN_PROCESSING */
  2242. u8 bssid[ETH_ALEN];
  2243. struct wpa_bss *bss;
  2244. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2245. if (hwaddr_aton(addr, bssid)) {
  2246. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  2247. "address '%s'", addr);
  2248. return -1;
  2249. }
  2250. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  2251. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2252. if (!bss) {
  2253. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  2254. "from BSS table");
  2255. return -1;
  2256. }
  2257. /*
  2258. * TODO: Find best network configuration block from configuration to
  2259. * allow roaming to other networks
  2260. */
  2261. if (!ssid) {
  2262. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  2263. "configuration known for the target AP");
  2264. return -1;
  2265. }
  2266. wpa_s->reassociate = 1;
  2267. wpa_supplicant_connect(wpa_s, bss, ssid);
  2268. return 0;
  2269. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2270. }
  2271. #ifdef CONFIG_P2P
  2272. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  2273. {
  2274. unsigned int timeout = atoi(cmd);
  2275. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  2276. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  2277. char *pos;
  2278. if (os_strstr(cmd, "type=social"))
  2279. type = P2P_FIND_ONLY_SOCIAL;
  2280. else if (os_strstr(cmd, "type=progressive"))
  2281. type = P2P_FIND_PROGRESSIVE;
  2282. pos = os_strstr(cmd, "dev_id=");
  2283. if (pos) {
  2284. pos += 7;
  2285. if (hwaddr_aton(pos, dev_id))
  2286. return -1;
  2287. _dev_id = dev_id;
  2288. }
  2289. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id);
  2290. }
  2291. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  2292. char *buf, size_t buflen)
  2293. {
  2294. u8 addr[ETH_ALEN];
  2295. char *pos, *pos2;
  2296. char *pin = NULL;
  2297. enum p2p_wps_method wps_method;
  2298. int new_pin;
  2299. int ret;
  2300. int persistent_group;
  2301. int join;
  2302. int auth;
  2303. int go_intent = -1;
  2304. int freq = 0;
  2305. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
  2306. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
  2307. if (hwaddr_aton(cmd, addr))
  2308. return -1;
  2309. pos = cmd + 17;
  2310. if (*pos != ' ')
  2311. return -1;
  2312. pos++;
  2313. persistent_group = os_strstr(pos, " persistent") != NULL;
  2314. join = os_strstr(pos, " join") != NULL;
  2315. auth = os_strstr(pos, " auth") != NULL;
  2316. pos2 = os_strstr(pos, " go_intent=");
  2317. if (pos2) {
  2318. pos2 += 11;
  2319. go_intent = atoi(pos2);
  2320. if (go_intent < 0 || go_intent > 15)
  2321. return -1;
  2322. }
  2323. pos2 = os_strstr(pos, " freq=");
  2324. if (pos2) {
  2325. pos2 += 6;
  2326. freq = atoi(pos2);
  2327. if (freq <= 0)
  2328. return -1;
  2329. }
  2330. if (os_strncmp(pos, "pin", 3) == 0) {
  2331. /* Request random PIN (to be displayed) and enable the PIN */
  2332. wps_method = WPS_PIN_DISPLAY;
  2333. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  2334. wps_method = WPS_PBC;
  2335. } else {
  2336. pin = pos;
  2337. pos = os_strchr(pin, ' ');
  2338. wps_method = WPS_PIN_KEYPAD;
  2339. if (pos) {
  2340. *pos++ = '\0';
  2341. if (os_strncmp(pos, "display", 7) == 0)
  2342. wps_method = WPS_PIN_DISPLAY;
  2343. }
  2344. }
  2345. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  2346. persistent_group, join, auth, go_intent,
  2347. freq);
  2348. if (new_pin == -2) {
  2349. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  2350. return 25;
  2351. }
  2352. if (new_pin == -3) {
  2353. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  2354. return 25;
  2355. }
  2356. if (new_pin < 0)
  2357. return -1;
  2358. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  2359. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  2360. if (ret < 0 || (size_t) ret >= buflen)
  2361. return -1;
  2362. return ret;
  2363. }
  2364. os_memcpy(buf, "OK\n", 3);
  2365. return 3;
  2366. }
  2367. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2368. {
  2369. unsigned int timeout = atoi(cmd);
  2370. return wpas_p2p_listen(wpa_s, timeout);
  2371. }
  2372. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  2373. {
  2374. u8 addr[ETH_ALEN];
  2375. char *pos;
  2376. /* <addr> <config method> [join] */
  2377. if (hwaddr_aton(cmd, addr))
  2378. return -1;
  2379. pos = cmd + 17;
  2380. if (*pos != ' ')
  2381. return -1;
  2382. pos++;
  2383. return wpas_p2p_prov_disc(wpa_s, addr, pos,
  2384. os_strstr(pos, "join") != NULL);
  2385. }
  2386. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  2387. size_t buflen)
  2388. {
  2389. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2390. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2391. ssid->passphrase == NULL)
  2392. return -1;
  2393. os_strlcpy(buf, ssid->passphrase, buflen);
  2394. return os_strlen(buf);
  2395. }
  2396. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  2397. char *buf, size_t buflen)
  2398. {
  2399. u64 ref;
  2400. int res;
  2401. u8 dst_buf[ETH_ALEN], *dst;
  2402. struct wpabuf *tlvs;
  2403. char *pos;
  2404. size_t len;
  2405. if (hwaddr_aton(cmd, dst_buf))
  2406. return -1;
  2407. dst = dst_buf;
  2408. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  2409. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  2410. dst = NULL;
  2411. pos = cmd + 17;
  2412. if (*pos != ' ')
  2413. return -1;
  2414. pos++;
  2415. if (os_strncmp(pos, "upnp ", 5) == 0) {
  2416. u8 version;
  2417. pos += 5;
  2418. if (hexstr2bin(pos, &version, 1) < 0)
  2419. return -1;
  2420. pos += 2;
  2421. if (*pos != ' ')
  2422. return -1;
  2423. pos++;
  2424. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  2425. } else {
  2426. len = os_strlen(pos);
  2427. if (len & 1)
  2428. return -1;
  2429. len /= 2;
  2430. tlvs = wpabuf_alloc(len);
  2431. if (tlvs == NULL)
  2432. return -1;
  2433. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  2434. wpabuf_free(tlvs);
  2435. return -1;
  2436. }
  2437. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  2438. wpabuf_free(tlvs);
  2439. }
  2440. if (ref == 0)
  2441. return -1;
  2442. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  2443. if (res < 0 || (unsigned) res >= buflen)
  2444. return -1;
  2445. return res;
  2446. }
  2447. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  2448. char *cmd)
  2449. {
  2450. long long unsigned val;
  2451. u64 req;
  2452. if (sscanf(cmd, "%llx", &val) != 1)
  2453. return -1;
  2454. req = val;
  2455. return wpas_p2p_sd_cancel_request(wpa_s, req);
  2456. }
  2457. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  2458. {
  2459. int freq;
  2460. u8 dst[ETH_ALEN];
  2461. u8 dialog_token;
  2462. struct wpabuf *resp_tlvs;
  2463. char *pos, *pos2;
  2464. size_t len;
  2465. pos = os_strchr(cmd, ' ');
  2466. if (pos == NULL)
  2467. return -1;
  2468. *pos++ = '\0';
  2469. freq = atoi(cmd);
  2470. if (freq == 0)
  2471. return -1;
  2472. if (hwaddr_aton(pos, dst))
  2473. return -1;
  2474. pos += 17;
  2475. if (*pos != ' ')
  2476. return -1;
  2477. pos++;
  2478. pos2 = os_strchr(pos, ' ');
  2479. if (pos2 == NULL)
  2480. return -1;
  2481. *pos2++ = '\0';
  2482. dialog_token = atoi(pos);
  2483. len = os_strlen(pos2);
  2484. if (len & 1)
  2485. return -1;
  2486. len /= 2;
  2487. resp_tlvs = wpabuf_alloc(len);
  2488. if (resp_tlvs == NULL)
  2489. return -1;
  2490. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  2491. wpabuf_free(resp_tlvs);
  2492. return -1;
  2493. }
  2494. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  2495. wpabuf_free(resp_tlvs);
  2496. return 0;
  2497. }
  2498. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  2499. char *cmd)
  2500. {
  2501. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  2502. return 0;
  2503. }
  2504. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  2505. char *cmd)
  2506. {
  2507. char *pos;
  2508. size_t len;
  2509. struct wpabuf *query, *resp;
  2510. pos = os_strchr(cmd, ' ');
  2511. if (pos == NULL)
  2512. return -1;
  2513. *pos++ = '\0';
  2514. len = os_strlen(cmd);
  2515. if (len & 1)
  2516. return -1;
  2517. len /= 2;
  2518. query = wpabuf_alloc(len);
  2519. if (query == NULL)
  2520. return -1;
  2521. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  2522. wpabuf_free(query);
  2523. return -1;
  2524. }
  2525. len = os_strlen(pos);
  2526. if (len & 1) {
  2527. wpabuf_free(query);
  2528. return -1;
  2529. }
  2530. len /= 2;
  2531. resp = wpabuf_alloc(len);
  2532. if (resp == NULL) {
  2533. wpabuf_free(query);
  2534. return -1;
  2535. }
  2536. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  2537. wpabuf_free(query);
  2538. wpabuf_free(resp);
  2539. return -1;
  2540. }
  2541. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  2542. wpabuf_free(query);
  2543. wpabuf_free(resp);
  2544. return -1;
  2545. }
  2546. return 0;
  2547. }
  2548. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  2549. {
  2550. char *pos;
  2551. u8 version;
  2552. pos = os_strchr(cmd, ' ');
  2553. if (pos == NULL)
  2554. return -1;
  2555. *pos++ = '\0';
  2556. if (hexstr2bin(cmd, &version, 1) < 0)
  2557. return -1;
  2558. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  2559. }
  2560. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  2561. {
  2562. char *pos;
  2563. pos = os_strchr(cmd, ' ');
  2564. if (pos == NULL)
  2565. return -1;
  2566. *pos++ = '\0';
  2567. if (os_strcmp(cmd, "bonjour") == 0)
  2568. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  2569. if (os_strcmp(cmd, "upnp") == 0)
  2570. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  2571. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  2572. return -1;
  2573. }
  2574. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  2575. char *cmd)
  2576. {
  2577. size_t len;
  2578. struct wpabuf *query;
  2579. int ret;
  2580. len = os_strlen(cmd);
  2581. if (len & 1)
  2582. return -1;
  2583. len /= 2;
  2584. query = wpabuf_alloc(len);
  2585. if (query == NULL)
  2586. return -1;
  2587. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  2588. wpabuf_free(query);
  2589. return -1;
  2590. }
  2591. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  2592. wpabuf_free(query);
  2593. return ret;
  2594. }
  2595. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  2596. {
  2597. char *pos;
  2598. u8 version;
  2599. pos = os_strchr(cmd, ' ');
  2600. if (pos == NULL)
  2601. return -1;
  2602. *pos++ = '\0';
  2603. if (hexstr2bin(cmd, &version, 1) < 0)
  2604. return -1;
  2605. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  2606. }
  2607. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  2608. {
  2609. char *pos;
  2610. pos = os_strchr(cmd, ' ');
  2611. if (pos == NULL)
  2612. return -1;
  2613. *pos++ = '\0';
  2614. if (os_strcmp(cmd, "bonjour") == 0)
  2615. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  2616. if (os_strcmp(cmd, "upnp") == 0)
  2617. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  2618. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  2619. return -1;
  2620. }
  2621. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  2622. {
  2623. u8 addr[ETH_ALEN];
  2624. /* <addr> */
  2625. if (hwaddr_aton(cmd, addr))
  2626. return -1;
  2627. return wpas_p2p_reject(wpa_s, addr);
  2628. }
  2629. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  2630. {
  2631. char *pos;
  2632. int id;
  2633. struct wpa_ssid *ssid;
  2634. u8 peer[ETH_ALEN];
  2635. id = atoi(cmd);
  2636. pos = os_strstr(cmd, " peer=");
  2637. if (pos) {
  2638. pos += 6;
  2639. if (hwaddr_aton(pos, peer))
  2640. return -1;
  2641. }
  2642. ssid = wpa_config_get_network(wpa_s->conf, id);
  2643. if (ssid == NULL || ssid->disabled != 2) {
  2644. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2645. "for persistent P2P group",
  2646. id);
  2647. return -1;
  2648. }
  2649. return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
  2650. }
  2651. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  2652. {
  2653. char *pos;
  2654. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  2655. pos = os_strstr(cmd, " peer=");
  2656. if (!pos)
  2657. return -1;
  2658. *pos = '\0';
  2659. pos += 6;
  2660. if (hwaddr_aton(pos, peer)) {
  2661. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  2662. return -1;
  2663. }
  2664. pos = os_strstr(pos, " go_dev_addr=");
  2665. if (pos) {
  2666. pos += 13;
  2667. if (hwaddr_aton(pos, go_dev_addr)) {
  2668. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  2669. pos);
  2670. return -1;
  2671. }
  2672. go_dev = go_dev_addr;
  2673. }
  2674. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  2675. }
  2676. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  2677. {
  2678. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2679. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  2680. if (os_strncmp(cmd, "group=", 6) == 0)
  2681. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  2682. return -1;
  2683. }
  2684. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  2685. char *cmd, int freq)
  2686. {
  2687. int id;
  2688. struct wpa_ssid *ssid;
  2689. id = atoi(cmd);
  2690. ssid = wpa_config_get_network(wpa_s->conf, id);
  2691. if (ssid == NULL || ssid->disabled != 2) {
  2692. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2693. "for persistent P2P group",
  2694. id);
  2695. return -1;
  2696. }
  2697. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
  2698. }
  2699. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  2700. {
  2701. int freq = 0;
  2702. char *pos;
  2703. pos = os_strstr(cmd, "freq=");
  2704. if (pos)
  2705. freq = atoi(pos + 5);
  2706. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2707. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
  2708. if (os_strcmp(cmd, "persistent") == 0 ||
  2709. os_strncmp(cmd, "persistent ", 11) == 0)
  2710. return wpas_p2p_group_add(wpa_s, 1, freq);
  2711. if (os_strncmp(cmd, "freq=", 5) == 0)
  2712. return wpas_p2p_group_add(wpa_s, 0, freq);
  2713. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  2714. cmd);
  2715. return -1;
  2716. }
  2717. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  2718. char *buf, size_t buflen)
  2719. {
  2720. u8 addr[ETH_ALEN], *addr_ptr;
  2721. int next, res;
  2722. const struct p2p_peer_info *info;
  2723. char *pos, *end;
  2724. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2725. struct wpa_ssid *ssid;
  2726. if (!wpa_s->global->p2p)
  2727. return -1;
  2728. if (os_strcmp(cmd, "FIRST") == 0) {
  2729. addr_ptr = NULL;
  2730. next = 0;
  2731. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2732. if (hwaddr_aton(cmd + 5, addr) < 0)
  2733. return -1;
  2734. addr_ptr = addr;
  2735. next = 1;
  2736. } else {
  2737. if (hwaddr_aton(cmd, addr) < 0)
  2738. return -1;
  2739. addr_ptr = addr;
  2740. next = 0;
  2741. }
  2742. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  2743. if (info == NULL)
  2744. return -1;
  2745. pos = buf;
  2746. end = buf + buflen;
  2747. res = os_snprintf(pos, end - pos, MACSTR "\n"
  2748. "pri_dev_type=%s\n"
  2749. "device_name=%s\n"
  2750. "manufacturer=%s\n"
  2751. "model_name=%s\n"
  2752. "model_number=%s\n"
  2753. "serial_number=%s\n"
  2754. "config_methods=0x%x\n"
  2755. "dev_capab=0x%x\n"
  2756. "group_capab=0x%x\n"
  2757. "level=%d\n",
  2758. MAC2STR(info->p2p_device_addr),
  2759. wps_dev_type_bin2str(info->pri_dev_type,
  2760. devtype, sizeof(devtype)),
  2761. info->device_name,
  2762. info->manufacturer,
  2763. info->model_name,
  2764. info->model_number,
  2765. info->serial_number,
  2766. info->config_methods,
  2767. info->dev_capab,
  2768. info->group_capab,
  2769. info->level);
  2770. if (res < 0 || res >= end - pos)
  2771. return pos - buf;
  2772. pos += res;
  2773. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  2774. if (ssid) {
  2775. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  2776. if (res < 0 || res >= end - pos)
  2777. return pos - buf;
  2778. pos += res;
  2779. }
  2780. res = p2p_get_peer_info_txt(info, pos, end - pos);
  2781. if (res < 0)
  2782. return pos - buf;
  2783. pos += res;
  2784. return pos - buf;
  2785. }
  2786. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  2787. {
  2788. char *param;
  2789. if (wpa_s->global->p2p == NULL)
  2790. return -1;
  2791. param = os_strchr(cmd, ' ');
  2792. if (param == NULL)
  2793. return -1;
  2794. *param++ = '\0';
  2795. if (os_strcmp(cmd, "discoverability") == 0) {
  2796. p2p_set_client_discoverability(wpa_s->global->p2p,
  2797. atoi(param));
  2798. return 0;
  2799. }
  2800. if (os_strcmp(cmd, "managed") == 0) {
  2801. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  2802. return 0;
  2803. }
  2804. if (os_strcmp(cmd, "listen_channel") == 0) {
  2805. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  2806. atoi(param));
  2807. }
  2808. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  2809. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  2810. os_strlen(param));
  2811. }
  2812. if (os_strcmp(cmd, "noa") == 0) {
  2813. char *pos;
  2814. int count, start, duration;
  2815. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  2816. count = atoi(param);
  2817. pos = os_strchr(param, ',');
  2818. if (pos == NULL)
  2819. return -1;
  2820. pos++;
  2821. start = atoi(pos);
  2822. pos = os_strchr(pos, ',');
  2823. if (pos == NULL)
  2824. return -1;
  2825. pos++;
  2826. duration = atoi(pos);
  2827. if (count < 0 || count > 255 || start < 0 || duration < 0)
  2828. return -1;
  2829. if (count == 0 && duration > 0)
  2830. return -1;
  2831. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  2832. "start=%d duration=%d", count, start, duration);
  2833. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  2834. }
  2835. if (os_strcmp(cmd, "ps") == 0)
  2836. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  2837. if (os_strcmp(cmd, "oppps") == 0)
  2838. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  2839. if (os_strcmp(cmd, "ctwindow") == 0)
  2840. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  2841. if (os_strcmp(cmd, "disabled") == 0) {
  2842. wpa_s->global->p2p_disabled = atoi(param);
  2843. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  2844. wpa_s->global->p2p_disabled ?
  2845. "disabled" : "enabled");
  2846. if (wpa_s->global->p2p_disabled) {
  2847. wpas_p2p_stop_find(wpa_s);
  2848. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2849. p2p_flush(wpa_s->global->p2p);
  2850. }
  2851. return 0;
  2852. }
  2853. if (os_strcmp(cmd, "force_long_sd") == 0) {
  2854. wpa_s->force_long_sd = atoi(param);
  2855. return 0;
  2856. }
  2857. if (os_strcmp(cmd, "peer_filter") == 0) {
  2858. u8 addr[ETH_ALEN];
  2859. if (hwaddr_aton(param, addr))
  2860. return -1;
  2861. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  2862. return 0;
  2863. }
  2864. if (os_strcmp(cmd, "cross_connect") == 0)
  2865. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  2866. if (os_strcmp(cmd, "go_apsd") == 0) {
  2867. if (os_strcmp(param, "disable") == 0)
  2868. wpa_s->set_ap_uapsd = 0;
  2869. else {
  2870. wpa_s->set_ap_uapsd = 1;
  2871. wpa_s->ap_uapsd = atoi(param);
  2872. }
  2873. return 0;
  2874. }
  2875. if (os_strcmp(cmd, "client_apsd") == 0) {
  2876. if (os_strcmp(param, "disable") == 0)
  2877. wpa_s->set_sta_uapsd = 0;
  2878. else {
  2879. int be, bk, vi, vo;
  2880. char *pos;
  2881. /* format: BE,BK,VI,VO;max SP Length */
  2882. be = atoi(param);
  2883. pos = os_strchr(param, ',');
  2884. if (pos == NULL)
  2885. return -1;
  2886. pos++;
  2887. bk = atoi(pos);
  2888. pos = os_strchr(pos, ',');
  2889. if (pos == NULL)
  2890. return -1;
  2891. pos++;
  2892. vi = atoi(pos);
  2893. pos = os_strchr(pos, ',');
  2894. if (pos == NULL)
  2895. return -1;
  2896. pos++;
  2897. vo = atoi(pos);
  2898. /* ignore max SP Length for now */
  2899. wpa_s->set_sta_uapsd = 1;
  2900. wpa_s->sta_uapsd = 0;
  2901. if (be)
  2902. wpa_s->sta_uapsd |= BIT(0);
  2903. if (bk)
  2904. wpa_s->sta_uapsd |= BIT(1);
  2905. if (vi)
  2906. wpa_s->sta_uapsd |= BIT(2);
  2907. if (vo)
  2908. wpa_s->sta_uapsd |= BIT(3);
  2909. }
  2910. return 0;
  2911. }
  2912. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  2913. cmd);
  2914. return -1;
  2915. }
  2916. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  2917. {
  2918. char *pos, *pos2;
  2919. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  2920. if (cmd[0]) {
  2921. pos = os_strchr(cmd, ' ');
  2922. if (pos == NULL)
  2923. return -1;
  2924. *pos++ = '\0';
  2925. dur1 = atoi(cmd);
  2926. pos2 = os_strchr(pos, ' ');
  2927. if (pos2)
  2928. *pos2++ = '\0';
  2929. int1 = atoi(pos);
  2930. } else
  2931. pos2 = NULL;
  2932. if (pos2) {
  2933. pos = os_strchr(pos2, ' ');
  2934. if (pos == NULL)
  2935. return -1;
  2936. *pos++ = '\0';
  2937. dur2 = atoi(pos2);
  2938. int2 = atoi(pos);
  2939. }
  2940. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  2941. }
  2942. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2943. {
  2944. char *pos;
  2945. unsigned int period = 0, interval = 0;
  2946. if (cmd[0]) {
  2947. pos = os_strchr(cmd, ' ');
  2948. if (pos == NULL)
  2949. return -1;
  2950. *pos++ = '\0';
  2951. period = atoi(cmd);
  2952. interval = atoi(pos);
  2953. }
  2954. return wpas_p2p_ext_listen(wpa_s, period, interval);
  2955. }
  2956. #endif /* CONFIG_P2P */
  2957. #ifdef CONFIG_INTERWORKING
  2958. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  2959. {
  2960. u8 bssid[ETH_ALEN];
  2961. struct wpa_bss *bss;
  2962. if (hwaddr_aton(dst, bssid)) {
  2963. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  2964. return -1;
  2965. }
  2966. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2967. if (bss == NULL) {
  2968. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  2969. MAC2STR(bssid));
  2970. return -1;
  2971. }
  2972. return interworking_connect(wpa_s, bss);
  2973. }
  2974. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  2975. {
  2976. u8 dst_addr[ETH_ALEN];
  2977. int used;
  2978. char *pos;
  2979. #define MAX_ANQP_INFO_ID 100
  2980. u16 id[MAX_ANQP_INFO_ID];
  2981. size_t num_id = 0;
  2982. used = hwaddr_aton2(dst, dst_addr);
  2983. if (used < 0)
  2984. return -1;
  2985. pos = dst + used;
  2986. while (num_id < MAX_ANQP_INFO_ID) {
  2987. id[num_id] = atoi(pos);
  2988. if (id[num_id])
  2989. num_id++;
  2990. pos = os_strchr(pos + 1, ',');
  2991. if (pos == NULL)
  2992. break;
  2993. pos++;
  2994. }
  2995. if (num_id == 0)
  2996. return -1;
  2997. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  2998. }
  2999. #endif /* CONFIG_INTERWORKING */
  3000. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  3001. struct wpa_supplicant *wpa_s, char *cmd)
  3002. {
  3003. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  3004. return 0;
  3005. }
  3006. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  3007. size_t buflen)
  3008. {
  3009. struct wpa_signal_info si;
  3010. int ret;
  3011. ret = wpa_drv_signal_poll(wpa_s, &si);
  3012. if (ret)
  3013. return -1;
  3014. ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
  3015. "NOISE=%d\nFREQUENCY=%u\n",
  3016. si.current_signal, si.current_txrate / 1000,
  3017. si.current_noise, si.frequency);
  3018. if (ret < 0 || (unsigned int) ret > buflen)
  3019. return -1;
  3020. return ret;
  3021. }
  3022. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  3023. char *buf, size_t *resp_len)
  3024. {
  3025. char *reply;
  3026. const int reply_size = 4096;
  3027. int ctrl_rsp = 0;
  3028. int reply_len;
  3029. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  3030. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  3031. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  3032. (const u8 *) buf, os_strlen(buf));
  3033. } else {
  3034. int level = MSG_DEBUG;
  3035. if (os_strcmp(buf, "PING") == 0)
  3036. level = MSG_EXCESSIVE;
  3037. wpa_hexdump_ascii(level, "RX ctrl_iface",
  3038. (const u8 *) buf, os_strlen(buf));
  3039. }
  3040. reply = os_malloc(reply_size);
  3041. if (reply == NULL) {
  3042. *resp_len = 1;
  3043. return NULL;
  3044. }
  3045. os_memcpy(reply, "OK\n", 3);
  3046. reply_len = 3;
  3047. if (os_strcmp(buf, "PING") == 0) {
  3048. os_memcpy(reply, "PONG\n", 5);
  3049. reply_len = 5;
  3050. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  3051. if (wpa_debug_reopen_file() < 0)
  3052. reply_len = -1;
  3053. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  3054. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  3055. } else if (os_strcmp(buf, "MIB") == 0) {
  3056. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  3057. if (reply_len >= 0) {
  3058. int res;
  3059. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  3060. reply_size - reply_len);
  3061. if (res < 0)
  3062. reply_len = -1;
  3063. else
  3064. reply_len += res;
  3065. }
  3066. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  3067. reply_len = wpa_supplicant_ctrl_iface_status(
  3068. wpa_s, buf + 6, reply, reply_size);
  3069. } else if (os_strcmp(buf, "PMKSA") == 0) {
  3070. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  3071. reply_size);
  3072. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  3073. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  3074. reply_len = -1;
  3075. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  3076. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  3077. reply, reply_size);
  3078. } else if (os_strcmp(buf, "LOGON") == 0) {
  3079. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  3080. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  3081. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  3082. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  3083. wpa_s->normal_scans = 0;
  3084. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3085. reply_len = -1;
  3086. else {
  3087. wpa_s->disconnected = 0;
  3088. wpa_s->reassociate = 1;
  3089. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3090. }
  3091. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  3092. wpa_s->normal_scans = 0;
  3093. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3094. reply_len = -1;
  3095. else if (wpa_s->disconnected) {
  3096. wpa_s->disconnected = 0;
  3097. wpa_s->reassociate = 1;
  3098. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3099. }
  3100. #ifdef IEEE8021X_EAPOL
  3101. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  3102. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  3103. reply_len = -1;
  3104. #endif /* IEEE8021X_EAPOL */
  3105. #ifdef CONFIG_PEERKEY
  3106. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  3107. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  3108. reply_len = -1;
  3109. #endif /* CONFIG_PEERKEY */
  3110. #ifdef CONFIG_IEEE80211R
  3111. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  3112. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  3113. reply_len = -1;
  3114. #endif /* CONFIG_IEEE80211R */
  3115. #ifdef CONFIG_WPS
  3116. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  3117. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  3118. if (res == -2) {
  3119. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  3120. reply_len = 17;
  3121. } else if (res)
  3122. reply_len = -1;
  3123. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  3124. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  3125. if (res == -2) {
  3126. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  3127. reply_len = 17;
  3128. } else if (res)
  3129. reply_len = -1;
  3130. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  3131. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  3132. reply,
  3133. reply_size);
  3134. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  3135. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  3136. wpa_s, buf + 14, reply, reply_size);
  3137. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  3138. if (wpas_wps_cancel(wpa_s))
  3139. reply_len = -1;
  3140. #ifdef CONFIG_WPS_OOB
  3141. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  3142. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  3143. reply_len = -1;
  3144. #endif /* CONFIG_WPS_OOB */
  3145. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  3146. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  3147. reply_len = -1;
  3148. #ifdef CONFIG_AP
  3149. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  3150. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  3151. wpa_s, buf + 11, reply, reply_size);
  3152. #endif /* CONFIG_AP */
  3153. #ifdef CONFIG_WPS_ER
  3154. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  3155. if (wpas_wps_er_start(wpa_s, NULL))
  3156. reply_len = -1;
  3157. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  3158. if (wpas_wps_er_start(wpa_s, buf + 13))
  3159. reply_len = -1;
  3160. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  3161. if (wpas_wps_er_stop(wpa_s))
  3162. reply_len = -1;
  3163. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  3164. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  3165. reply_len = -1;
  3166. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  3167. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  3168. if (ret == -2) {
  3169. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  3170. reply_len = 17;
  3171. } else if (ret == -3) {
  3172. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  3173. reply_len = 18;
  3174. } else if (ret == -4) {
  3175. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  3176. reply_len = 20;
  3177. } else if (ret)
  3178. reply_len = -1;
  3179. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  3180. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  3181. reply_len = -1;
  3182. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  3183. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  3184. buf + 18))
  3185. reply_len = -1;
  3186. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  3187. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  3188. reply_len = -1;
  3189. #endif /* CONFIG_WPS_ER */
  3190. #endif /* CONFIG_WPS */
  3191. #ifdef CONFIG_IBSS_RSN
  3192. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  3193. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  3194. reply_len = -1;
  3195. #endif /* CONFIG_IBSS_RSN */
  3196. #ifdef CONFIG_P2P
  3197. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  3198. if (p2p_ctrl_find(wpa_s, buf + 9))
  3199. reply_len = -1;
  3200. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  3201. if (p2p_ctrl_find(wpa_s, ""))
  3202. reply_len = -1;
  3203. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  3204. wpas_p2p_stop_find(wpa_s);
  3205. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  3206. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  3207. reply_size);
  3208. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  3209. if (p2p_ctrl_listen(wpa_s, buf + 11))
  3210. reply_len = -1;
  3211. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  3212. if (p2p_ctrl_listen(wpa_s, ""))
  3213. reply_len = -1;
  3214. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  3215. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  3216. reply_len = -1;
  3217. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  3218. if (wpas_p2p_group_add(wpa_s, 0, 0))
  3219. reply_len = -1;
  3220. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  3221. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  3222. reply_len = -1;
  3223. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  3224. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  3225. reply_len = -1;
  3226. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  3227. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  3228. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  3229. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  3230. reply_size);
  3231. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  3232. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  3233. reply_len = -1;
  3234. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  3235. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  3236. reply_len = -1;
  3237. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  3238. wpas_p2p_sd_service_update(wpa_s);
  3239. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  3240. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  3241. reply_len = -1;
  3242. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  3243. wpas_p2p_service_flush(wpa_s);
  3244. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  3245. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  3246. reply_len = -1;
  3247. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  3248. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  3249. reply_len = -1;
  3250. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  3251. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  3252. reply_len = -1;
  3253. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  3254. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  3255. reply_len = -1;
  3256. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  3257. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  3258. reply_size);
  3259. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  3260. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  3261. reply_len = -1;
  3262. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  3263. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3264. wpa_s->force_long_sd = 0;
  3265. if (wpa_s->global->p2p)
  3266. p2p_flush(wpa_s->global->p2p);
  3267. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  3268. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  3269. reply_len = -1;
  3270. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  3271. if (wpas_p2p_cancel(wpa_s))
  3272. reply_len = -1;
  3273. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  3274. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  3275. reply_len = -1;
  3276. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  3277. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  3278. reply_len = -1;
  3279. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  3280. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  3281. reply_len = -1;
  3282. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  3283. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  3284. reply_len = -1;
  3285. #endif /* CONFIG_P2P */
  3286. #ifdef CONFIG_INTERWORKING
  3287. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  3288. if (interworking_fetch_anqp(wpa_s) < 0)
  3289. reply_len = -1;
  3290. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  3291. interworking_stop_fetch_anqp(wpa_s);
  3292. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  3293. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  3294. NULL) < 0)
  3295. reply_len = -1;
  3296. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  3297. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  3298. reply_len = -1;
  3299. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  3300. if (get_anqp(wpa_s, buf + 9) < 0)
  3301. reply_len = -1;
  3302. #endif /* CONFIG_INTERWORKING */
  3303. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  3304. {
  3305. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  3306. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  3307. reply_len = -1;
  3308. else
  3309. ctrl_rsp = 1;
  3310. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  3311. if (wpa_supplicant_reload_configuration(wpa_s))
  3312. reply_len = -1;
  3313. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  3314. wpa_supplicant_terminate_proc(wpa_s->global);
  3315. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  3316. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  3317. reply_len = -1;
  3318. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  3319. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  3320. wpa_s, buf + 9, reply, reply_size);
  3321. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  3322. reply_len = wpa_supplicant_ctrl_iface_log_level(
  3323. wpa_s, buf + 9, reply, reply_size);
  3324. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  3325. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  3326. wpa_s, reply, reply_size);
  3327. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  3328. wpa_s->reassociate = 0;
  3329. wpa_s->disconnected = 1;
  3330. wpa_supplicant_cancel_sched_scan(wpa_s);
  3331. wpa_supplicant_deauthenticate(wpa_s,
  3332. WLAN_REASON_DEAUTH_LEAVING);
  3333. } else if (os_strcmp(buf, "SCAN") == 0) {
  3334. wpa_s->normal_scans = 0;
  3335. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3336. reply_len = -1;
  3337. else {
  3338. if (!wpa_s->scanning &&
  3339. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  3340. (wpa_s->wpa_state == WPA_COMPLETED))) {
  3341. wpa_s->scan_req = 2;
  3342. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3343. } else {
  3344. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  3345. "reject new request");
  3346. reply_len = os_snprintf(reply, reply_size,
  3347. "FAIL-BUSY\n");
  3348. }
  3349. }
  3350. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  3351. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  3352. wpa_s, reply, reply_size);
  3353. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  3354. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  3355. reply_len = -1;
  3356. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  3357. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  3358. reply_len = -1;
  3359. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  3360. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  3361. reply_len = -1;
  3362. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  3363. reply_len = wpa_supplicant_ctrl_iface_add_network(
  3364. wpa_s, reply, reply_size);
  3365. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  3366. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  3367. reply_len = -1;
  3368. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  3369. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  3370. reply_len = -1;
  3371. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  3372. reply_len = wpa_supplicant_ctrl_iface_get_network(
  3373. wpa_s, buf + 12, reply, reply_size);
  3374. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  3375. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  3376. wpa_s, reply, reply_size);
  3377. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  3378. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  3379. wpa_s, reply, reply_size);
  3380. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  3381. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  3382. reply_len = -1;
  3383. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  3384. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  3385. reply_len = -1;
  3386. #ifndef CONFIG_NO_CONFIG_WRITE
  3387. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  3388. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  3389. reply_len = -1;
  3390. #endif /* CONFIG_NO_CONFIG_WRITE */
  3391. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  3392. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  3393. wpa_s, buf + 15, reply, reply_size);
  3394. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  3395. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  3396. reply_len = -1;
  3397. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  3398. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  3399. reply_len = -1;
  3400. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  3401. reply_len = wpa_supplicant_global_iface_list(
  3402. wpa_s->global, reply, reply_size);
  3403. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  3404. reply_len = wpa_supplicant_global_iface_interfaces(
  3405. wpa_s->global, reply, reply_size);
  3406. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  3407. reply_len = wpa_supplicant_ctrl_iface_bss(
  3408. wpa_s, buf + 4, reply, reply_size);
  3409. #ifdef CONFIG_AP
  3410. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  3411. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  3412. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  3413. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  3414. reply_size);
  3415. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  3416. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  3417. reply_size);
  3418. #endif /* CONFIG_AP */
  3419. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  3420. wpas_notify_suspend(wpa_s->global);
  3421. } else if (os_strcmp(buf, "RESUME") == 0) {
  3422. wpas_notify_resume(wpa_s->global);
  3423. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  3424. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  3425. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  3426. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  3427. reply_len = -1;
  3428. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  3429. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  3430. reply_len = -1;
  3431. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  3432. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  3433. reply_len = -1;
  3434. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  3435. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  3436. buf + 17))
  3437. reply_len = -1;
  3438. #ifdef CONFIG_TDLS
  3439. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  3440. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  3441. reply_len = -1;
  3442. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  3443. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  3444. reply_len = -1;
  3445. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  3446. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  3447. reply_len = -1;
  3448. #endif /* CONFIG_TDLS */
  3449. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  3450. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  3451. reply_size);
  3452. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  3453. eapol_sm_request_reauth(wpa_s->eapol);
  3454. } else {
  3455. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  3456. reply_len = 16;
  3457. }
  3458. if (reply_len < 0) {
  3459. os_memcpy(reply, "FAIL\n", 5);
  3460. reply_len = 5;
  3461. }
  3462. if (ctrl_rsp)
  3463. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  3464. *resp_len = reply_len;
  3465. return reply;
  3466. }
  3467. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  3468. char *cmd)
  3469. {
  3470. struct wpa_interface iface;
  3471. char *pos;
  3472. /*
  3473. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  3474. * TAB<bridge_ifname>
  3475. */
  3476. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  3477. os_memset(&iface, 0, sizeof(iface));
  3478. do {
  3479. iface.ifname = pos = cmd;
  3480. pos = os_strchr(pos, '\t');
  3481. if (pos)
  3482. *pos++ = '\0';
  3483. if (iface.ifname[0] == '\0')
  3484. return -1;
  3485. if (pos == NULL)
  3486. break;
  3487. iface.confname = pos;
  3488. pos = os_strchr(pos, '\t');
  3489. if (pos)
  3490. *pos++ = '\0';
  3491. if (iface.confname[0] == '\0')
  3492. iface.confname = NULL;
  3493. if (pos == NULL)
  3494. break;
  3495. iface.driver = pos;
  3496. pos = os_strchr(pos, '\t');
  3497. if (pos)
  3498. *pos++ = '\0';
  3499. if (iface.driver[0] == '\0')
  3500. iface.driver = NULL;
  3501. if (pos == NULL)
  3502. break;
  3503. iface.ctrl_interface = pos;
  3504. pos = os_strchr(pos, '\t');
  3505. if (pos)
  3506. *pos++ = '\0';
  3507. if (iface.ctrl_interface[0] == '\0')
  3508. iface.ctrl_interface = NULL;
  3509. if (pos == NULL)
  3510. break;
  3511. iface.driver_param = pos;
  3512. pos = os_strchr(pos, '\t');
  3513. if (pos)
  3514. *pos++ = '\0';
  3515. if (iface.driver_param[0] == '\0')
  3516. iface.driver_param = NULL;
  3517. if (pos == NULL)
  3518. break;
  3519. iface.bridge_ifname = pos;
  3520. pos = os_strchr(pos, '\t');
  3521. if (pos)
  3522. *pos++ = '\0';
  3523. if (iface.bridge_ifname[0] == '\0')
  3524. iface.bridge_ifname = NULL;
  3525. if (pos == NULL)
  3526. break;
  3527. } while (0);
  3528. if (wpa_supplicant_get_iface(global, iface.ifname))
  3529. return -1;
  3530. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  3531. }
  3532. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  3533. char *cmd)
  3534. {
  3535. struct wpa_supplicant *wpa_s;
  3536. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  3537. wpa_s = wpa_supplicant_get_iface(global, cmd);
  3538. if (wpa_s == NULL)
  3539. return -1;
  3540. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  3541. }
  3542. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  3543. {
  3544. struct wpa_interface_info *prev;
  3545. while (iface) {
  3546. prev = iface;
  3547. iface = iface->next;
  3548. os_free(prev->ifname);
  3549. os_free(prev->desc);
  3550. os_free(prev);
  3551. }
  3552. }
  3553. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  3554. char *buf, int len)
  3555. {
  3556. int i, res;
  3557. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  3558. char *pos, *end;
  3559. for (i = 0; wpa_drivers[i]; i++) {
  3560. struct wpa_driver_ops *drv = wpa_drivers[i];
  3561. if (drv->get_interfaces == NULL)
  3562. continue;
  3563. tmp = drv->get_interfaces(global->drv_priv[i]);
  3564. if (tmp == NULL)
  3565. continue;
  3566. if (last == NULL)
  3567. iface = last = tmp;
  3568. else
  3569. last->next = tmp;
  3570. while (last->next)
  3571. last = last->next;
  3572. }
  3573. pos = buf;
  3574. end = buf + len;
  3575. for (tmp = iface; tmp; tmp = tmp->next) {
  3576. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  3577. tmp->drv_name, tmp->ifname,
  3578. tmp->desc ? tmp->desc : "");
  3579. if (res < 0 || res >= end - pos) {
  3580. *pos = '\0';
  3581. break;
  3582. }
  3583. pos += res;
  3584. }
  3585. wpa_free_iface_info(iface);
  3586. return pos - buf;
  3587. }
  3588. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  3589. char *buf, int len)
  3590. {
  3591. int res;
  3592. char *pos, *end;
  3593. struct wpa_supplicant *wpa_s;
  3594. wpa_s = global->ifaces;
  3595. pos = buf;
  3596. end = buf + len;
  3597. while (wpa_s) {
  3598. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  3599. if (res < 0 || res >= end - pos) {
  3600. *pos = '\0';
  3601. break;
  3602. }
  3603. pos += res;
  3604. wpa_s = wpa_s->next;
  3605. }
  3606. return pos - buf;
  3607. }
  3608. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  3609. char *buf, size_t *resp_len)
  3610. {
  3611. char *reply;
  3612. const int reply_size = 2048;
  3613. int reply_len;
  3614. int level = MSG_DEBUG;
  3615. if (os_strcmp(buf, "PING") == 0)
  3616. level = MSG_EXCESSIVE;
  3617. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  3618. (const u8 *) buf, os_strlen(buf));
  3619. reply = os_malloc(reply_size);
  3620. if (reply == NULL) {
  3621. *resp_len = 1;
  3622. return NULL;
  3623. }
  3624. os_memcpy(reply, "OK\n", 3);
  3625. reply_len = 3;
  3626. if (os_strcmp(buf, "PING") == 0) {
  3627. os_memcpy(reply, "PONG\n", 5);
  3628. reply_len = 5;
  3629. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  3630. if (wpa_supplicant_global_iface_add(global, buf + 14))
  3631. reply_len = -1;
  3632. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  3633. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  3634. reply_len = -1;
  3635. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  3636. reply_len = wpa_supplicant_global_iface_list(
  3637. global, reply, reply_size);
  3638. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  3639. reply_len = wpa_supplicant_global_iface_interfaces(
  3640. global, reply, reply_size);
  3641. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  3642. wpa_supplicant_terminate_proc(global);
  3643. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  3644. wpas_notify_suspend(global);
  3645. } else if (os_strcmp(buf, "RESUME") == 0) {
  3646. wpas_notify_resume(global);
  3647. } else {
  3648. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  3649. reply_len = 16;
  3650. }
  3651. if (reply_len < 0) {
  3652. os_memcpy(reply, "FAIL\n", 5);
  3653. reply_len = 5;
  3654. }
  3655. *resp_len = reply_len;
  3656. return reply;
  3657. }