ctrl_iface.c 130 KB

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