ctrl_iface.c 127 KB

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