ctrl_iface.c 120 KB

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