ctrl_iface.c 107 KB

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