ctrl_iface.c 106 KB

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