ctrl_iface.c 133 KB

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