ctrl_iface.c 105 KB

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