ctrl_iface.c 132 KB

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