ctrl_iface.c 127 KB

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