p2p_supplicant.c 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p/p2p.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/p2p_hostapd.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "ap.h"
  24. #include "config_ssid.h"
  25. #include "config.h"
  26. #include "notify.h"
  27. #include "scan.h"
  28. #include "bss.h"
  29. #include "offchannel.h"
  30. #include "wps_supplicant.h"
  31. #include "p2p_supplicant.h"
  32. /*
  33. * How many times to try to scan to find the GO before giving up on join
  34. * request.
  35. */
  36. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  37. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  38. #ifndef P2P_MAX_CLIENT_IDLE
  39. /*
  40. * How many seconds to try to reconnect to the GO when connection in P2P client
  41. * role has been lost.
  42. */
  43. #define P2P_MAX_CLIENT_IDLE 10
  44. #endif /* P2P_MAX_CLIENT_IDLE */
  45. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  46. /*
  47. * How many seconds to wait for initial 4-way handshake to get completed after
  48. * WPS provisioning step.
  49. */
  50. #define P2P_MAX_INITIAL_CONN_WAIT 10
  51. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  52. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  53. /*
  54. * How many seconds to wait for initial 4-way handshake to get completed after
  55. * WPS provisioning step on the GO. This controls the extra time the P2P
  56. * operation is considered to be in progress (e.g., to delay other scans) after
  57. * WPS provisioning has been completed on the GO during group formation.
  58. */
  59. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  60. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  61. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  62. #define P2P_CONCURRENT_SEARCH_DELAY 500
  63. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  64. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  65. enum p2p_group_removal_reason {
  66. P2P_GROUP_REMOVAL_UNKNOWN,
  67. P2P_GROUP_REMOVAL_SILENT,
  68. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  69. P2P_GROUP_REMOVAL_REQUESTED,
  70. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  71. P2P_GROUP_REMOVAL_UNAVAILABLE,
  72. P2P_GROUP_REMOVAL_GO_ENDING_SESSION
  73. };
  74. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  75. static struct wpa_supplicant *
  76. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  77. int go);
  78. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  79. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  80. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  81. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  82. const u8 *dev_addr, enum p2p_wps_method wps_method,
  83. int auto_join);
  84. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  85. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  86. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  87. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  88. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  89. void *timeout_ctx);
  90. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  91. int group_added);
  92. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  93. /*
  94. * Get the number of concurrent channels that the HW can operate, but that are
  95. * currently not in use by any of the wpa_supplicant interfaces.
  96. */
  97. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  98. {
  99. int *freqs;
  100. int num;
  101. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  102. if (!freqs)
  103. return -1;
  104. num = get_shared_radio_freqs(wpa_s, freqs,
  105. wpa_s->num_multichan_concurrent);
  106. os_free(freqs);
  107. return wpa_s->num_multichan_concurrent - num;
  108. }
  109. /*
  110. * Get the frequencies that are currently in use by one or more of the virtual
  111. * interfaces, and that are also valid for P2P operation.
  112. */
  113. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  114. int *p2p_freqs, unsigned int len)
  115. {
  116. int *freqs;
  117. unsigned int num, i, j;
  118. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  119. if (!freqs)
  120. return -1;
  121. num = get_shared_radio_freqs(wpa_s, freqs,
  122. wpa_s->num_multichan_concurrent);
  123. os_memset(p2p_freqs, 0, sizeof(int) * len);
  124. for (i = 0, j = 0; i < num && j < len; i++) {
  125. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  126. p2p_freqs[j++] = freqs[i];
  127. }
  128. os_free(freqs);
  129. return j;
  130. }
  131. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  132. int freq)
  133. {
  134. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  135. return;
  136. if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  137. wpas_p2p_num_unused_channels(wpa_s) > 0 &&
  138. wpa_s->parent->conf->p2p_ignore_shared_freq)
  139. freq = 0;
  140. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  141. }
  142. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  143. struct wpa_scan_results *scan_res)
  144. {
  145. size_t i;
  146. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  147. return;
  148. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  149. (int) scan_res->num);
  150. for (i = 0; i < scan_res->num; i++) {
  151. struct wpa_scan_res *bss = scan_res->res[i];
  152. struct os_time time_tmp_age, entry_ts;
  153. time_tmp_age.sec = bss->age / 1000;
  154. time_tmp_age.usec = (bss->age % 1000) * 1000;
  155. os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  156. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  157. bss->freq, &entry_ts, bss->level,
  158. (const u8 *) (bss + 1),
  159. bss->ie_len) > 0)
  160. break;
  161. }
  162. p2p_scan_res_handled(wpa_s->global->p2p);
  163. }
  164. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  165. unsigned int num_req_dev_types,
  166. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  167. {
  168. struct wpa_supplicant *wpa_s = ctx;
  169. struct wpa_supplicant *ifs;
  170. struct wpa_driver_scan_params params;
  171. int ret;
  172. struct wpabuf *wps_ie, *ies;
  173. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  174. size_t ielen;
  175. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  176. return -1;
  177. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  178. if (ifs->sta_scan_pending &&
  179. (wpas_scan_scheduled(ifs) || ifs->scanning) &&
  180. wpas_p2p_in_progress(wpa_s) == 2) {
  181. wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
  182. "pending station mode scan to be "
  183. "completed on interface %s", ifs->ifname);
  184. wpa_s->global->p2p_cb_on_scan_complete = 1;
  185. wpa_supplicant_req_scan(ifs, 0, 0);
  186. return 1;
  187. }
  188. }
  189. os_memset(&params, 0, sizeof(params));
  190. /* P2P Wildcard SSID */
  191. params.num_ssids = 1;
  192. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  193. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  194. wpa_s->wps->dev.p2p = 1;
  195. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  196. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  197. num_req_dev_types, req_dev_types);
  198. if (wps_ie == NULL)
  199. return -1;
  200. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  201. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  202. if (ies == NULL) {
  203. wpabuf_free(wps_ie);
  204. return -1;
  205. }
  206. wpabuf_put_buf(ies, wps_ie);
  207. wpabuf_free(wps_ie);
  208. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  209. params.p2p_probe = 1;
  210. params.extra_ies = wpabuf_head(ies);
  211. params.extra_ies_len = wpabuf_len(ies);
  212. switch (type) {
  213. case P2P_SCAN_SOCIAL:
  214. params.freqs = social_channels;
  215. break;
  216. case P2P_SCAN_FULL:
  217. break;
  218. case P2P_SCAN_SOCIAL_PLUS_ONE:
  219. social_channels[3] = freq;
  220. params.freqs = social_channels;
  221. break;
  222. }
  223. ret = wpa_drv_scan(wpa_s, &params);
  224. wpabuf_free(ies);
  225. if (ret) {
  226. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  227. if (ifs->scanning ||
  228. ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
  229. wpa_s->global->p2p_cb_on_scan_complete = 1;
  230. ret = 1;
  231. break;
  232. }
  233. }
  234. } else {
  235. os_get_time(&wpa_s->scan_trigger_time);
  236. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  237. }
  238. return ret;
  239. }
  240. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  241. {
  242. switch (p2p_group_interface) {
  243. case P2P_GROUP_INTERFACE_PENDING:
  244. return WPA_IF_P2P_GROUP;
  245. case P2P_GROUP_INTERFACE_GO:
  246. return WPA_IF_P2P_GO;
  247. case P2P_GROUP_INTERFACE_CLIENT:
  248. return WPA_IF_P2P_CLIENT;
  249. }
  250. return WPA_IF_P2P_GROUP;
  251. }
  252. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  253. const u8 *ssid,
  254. size_t ssid_len, int *go)
  255. {
  256. struct wpa_ssid *s;
  257. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  258. for (s = wpa_s->conf->ssid; s; s = s->next) {
  259. if (s->disabled != 0 || !s->p2p_group ||
  260. s->ssid_len != ssid_len ||
  261. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  262. continue;
  263. if (s->mode == WPAS_MODE_P2P_GO &&
  264. s != wpa_s->current_ssid)
  265. continue;
  266. if (go)
  267. *go = s->mode == WPAS_MODE_P2P_GO;
  268. return wpa_s;
  269. }
  270. }
  271. return NULL;
  272. }
  273. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  274. enum p2p_group_removal_reason removal_reason)
  275. {
  276. struct wpa_ssid *ssid;
  277. char *gtype;
  278. const char *reason;
  279. ssid = wpa_s->current_ssid;
  280. if (ssid == NULL) {
  281. /*
  282. * The current SSID was not known, but there may still be a
  283. * pending P2P group interface waiting for provisioning or a
  284. * P2P group that is trying to reconnect.
  285. */
  286. ssid = wpa_s->conf->ssid;
  287. while (ssid) {
  288. if (ssid->p2p_group && ssid->disabled != 2)
  289. break;
  290. ssid = ssid->next;
  291. }
  292. if (ssid == NULL &&
  293. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  294. {
  295. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  296. "not found");
  297. return -1;
  298. }
  299. }
  300. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  301. gtype = "GO";
  302. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  303. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  304. wpa_s->reassociate = 0;
  305. wpa_s->disconnected = 1;
  306. wpa_supplicant_deauthenticate(wpa_s,
  307. WLAN_REASON_DEAUTH_LEAVING);
  308. gtype = "client";
  309. } else
  310. gtype = "GO";
  311. if (wpa_s->cross_connect_in_use) {
  312. wpa_s->cross_connect_in_use = 0;
  313. wpa_msg_global(wpa_s->parent, MSG_INFO,
  314. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  315. wpa_s->ifname, wpa_s->cross_connect_uplink);
  316. }
  317. switch (removal_reason) {
  318. case P2P_GROUP_REMOVAL_REQUESTED:
  319. reason = " reason=REQUESTED";
  320. break;
  321. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  322. reason = " reason=FORMATION_FAILED";
  323. break;
  324. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  325. reason = " reason=IDLE";
  326. break;
  327. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  328. reason = " reason=UNAVAILABLE";
  329. break;
  330. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  331. reason = " reason=GO_ENDING_SESSION";
  332. break;
  333. default:
  334. reason = "";
  335. break;
  336. }
  337. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  338. wpa_msg_global(wpa_s->parent, MSG_INFO,
  339. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  340. wpa_s->ifname, gtype, reason);
  341. }
  342. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  343. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  344. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  345. wpa_s->parent, NULL) > 0) {
  346. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  347. "timeout");
  348. wpa_s->p2p_in_provisioning = 0;
  349. }
  350. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  351. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  352. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  353. struct wpa_global *global;
  354. char *ifname;
  355. enum wpa_driver_if_type type;
  356. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  357. wpa_s->ifname);
  358. global = wpa_s->global;
  359. ifname = os_strdup(wpa_s->ifname);
  360. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  361. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  362. wpa_s = global->ifaces;
  363. if (wpa_s && ifname)
  364. wpa_drv_if_remove(wpa_s, type, ifname);
  365. os_free(ifname);
  366. return 1;
  367. }
  368. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  369. if (ssid && (ssid->p2p_group ||
  370. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  371. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  372. int id = ssid->id;
  373. if (ssid == wpa_s->current_ssid) {
  374. wpa_sm_set_config(wpa_s->wpa, NULL);
  375. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  376. wpa_s->current_ssid = NULL;
  377. }
  378. /*
  379. * Networks objects created during any P2P activities are not
  380. * exposed out as they might/will confuse certain non-P2P aware
  381. * applications since these network objects won't behave like
  382. * regular ones.
  383. *
  384. * Likewise, we don't send out network removed signals for such
  385. * network objects.
  386. */
  387. wpa_config_remove_network(wpa_s->conf, id);
  388. wpa_supplicant_clear_status(wpa_s);
  389. wpa_supplicant_cancel_sched_scan(wpa_s);
  390. wpa_s->sta_scan_pending = 0;
  391. } else {
  392. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  393. "found");
  394. }
  395. if (wpa_s->ap_iface)
  396. wpa_supplicant_ap_deinit(wpa_s);
  397. else
  398. wpa_drv_deinit_p2p_cli(wpa_s);
  399. return 0;
  400. }
  401. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  402. u8 *go_dev_addr,
  403. const u8 *ssid, size_t ssid_len)
  404. {
  405. struct wpa_bss *bss;
  406. const u8 *bssid;
  407. struct wpabuf *p2p;
  408. u8 group_capab;
  409. const u8 *addr;
  410. if (wpa_s->go_params)
  411. bssid = wpa_s->go_params->peer_interface_addr;
  412. else
  413. bssid = wpa_s->bssid;
  414. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  415. if (bss == NULL) {
  416. u8 iface_addr[ETH_ALEN];
  417. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  418. iface_addr) == 0)
  419. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  420. }
  421. if (bss == NULL) {
  422. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  423. "group is persistent - BSS " MACSTR " not found",
  424. MAC2STR(bssid));
  425. return 0;
  426. }
  427. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  428. if (p2p == NULL) {
  429. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  430. "group is persistent - BSS " MACSTR
  431. " did not include P2P IE", MAC2STR(bssid));
  432. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  433. (u8 *) (bss + 1), bss->ie_len);
  434. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  435. ((u8 *) bss + 1) + bss->ie_len,
  436. bss->beacon_ie_len);
  437. return 0;
  438. }
  439. group_capab = p2p_get_group_capab(p2p);
  440. addr = p2p_get_go_dev_addr(p2p);
  441. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  442. "group_capab=0x%x", group_capab);
  443. if (addr) {
  444. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  445. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  446. MAC2STR(addr));
  447. } else
  448. os_memset(go_dev_addr, 0, ETH_ALEN);
  449. wpabuf_free(p2p);
  450. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  451. "go_dev_addr=" MACSTR,
  452. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  453. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  454. }
  455. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  456. struct wpa_ssid *ssid,
  457. const u8 *go_dev_addr)
  458. {
  459. struct wpa_ssid *s;
  460. int changed = 0;
  461. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  462. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  463. for (s = wpa_s->conf->ssid; s; s = s->next) {
  464. if (s->disabled == 2 &&
  465. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  466. s->ssid_len == ssid->ssid_len &&
  467. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  468. break;
  469. }
  470. if (s) {
  471. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  472. "entry");
  473. if (ssid->passphrase && !s->passphrase)
  474. changed = 1;
  475. else if (ssid->passphrase && s->passphrase &&
  476. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  477. changed = 1;
  478. } else {
  479. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  480. "entry");
  481. changed = 1;
  482. s = wpa_config_add_network(wpa_s->conf);
  483. if (s == NULL)
  484. return -1;
  485. /*
  486. * Instead of network_added we emit persistent_group_added
  487. * notification. Also to keep the defense checks in
  488. * persistent_group obj registration method, we set the
  489. * relevant flags in s to designate it as a persistent group.
  490. */
  491. s->p2p_group = 1;
  492. s->p2p_persistent_group = 1;
  493. wpas_notify_persistent_group_added(wpa_s, s);
  494. wpa_config_set_network_defaults(s);
  495. }
  496. s->p2p_group = 1;
  497. s->p2p_persistent_group = 1;
  498. s->disabled = 2;
  499. s->bssid_set = 1;
  500. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  501. s->mode = ssid->mode;
  502. s->auth_alg = WPA_AUTH_ALG_OPEN;
  503. s->key_mgmt = WPA_KEY_MGMT_PSK;
  504. s->proto = WPA_PROTO_RSN;
  505. s->pairwise_cipher = WPA_CIPHER_CCMP;
  506. s->export_keys = 1;
  507. if (ssid->passphrase) {
  508. os_free(s->passphrase);
  509. s->passphrase = os_strdup(ssid->passphrase);
  510. }
  511. if (ssid->psk_set) {
  512. s->psk_set = 1;
  513. os_memcpy(s->psk, ssid->psk, 32);
  514. }
  515. if (s->passphrase && !s->psk_set)
  516. wpa_config_update_psk(s);
  517. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  518. os_free(s->ssid);
  519. s->ssid = os_malloc(ssid->ssid_len);
  520. }
  521. if (s->ssid) {
  522. s->ssid_len = ssid->ssid_len;
  523. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  524. }
  525. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  526. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  527. wpa_s->global->add_psk = NULL;
  528. changed = 1;
  529. }
  530. #ifndef CONFIG_NO_CONFIG_WRITE
  531. if (changed && wpa_s->conf->update_config &&
  532. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  533. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  534. }
  535. #endif /* CONFIG_NO_CONFIG_WRITE */
  536. return s->id;
  537. }
  538. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  539. const u8 *addr)
  540. {
  541. struct wpa_ssid *ssid, *s;
  542. u8 *n;
  543. size_t i;
  544. int found = 0;
  545. ssid = wpa_s->current_ssid;
  546. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  547. !ssid->p2p_persistent_group)
  548. return;
  549. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  550. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  551. continue;
  552. if (s->ssid_len == ssid->ssid_len &&
  553. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  554. break;
  555. }
  556. if (s == NULL)
  557. return;
  558. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  559. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  560. ETH_ALEN) != 0)
  561. continue;
  562. if (i == s->num_p2p_clients - 1)
  563. return; /* already the most recent entry */
  564. /* move the entry to mark it most recent */
  565. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  566. s->p2p_client_list + (i + 1) * ETH_ALEN,
  567. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  568. os_memcpy(s->p2p_client_list +
  569. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  570. found = 1;
  571. break;
  572. }
  573. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  574. n = os_realloc_array(s->p2p_client_list,
  575. s->num_p2p_clients + 1, ETH_ALEN);
  576. if (n == NULL)
  577. return;
  578. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  579. s->p2p_client_list = n;
  580. s->num_p2p_clients++;
  581. } else if (!found) {
  582. /* Not enough room for an additional entry - drop the oldest
  583. * entry */
  584. os_memmove(s->p2p_client_list,
  585. s->p2p_client_list + ETH_ALEN,
  586. (s->num_p2p_clients - 1) * ETH_ALEN);
  587. os_memcpy(s->p2p_client_list +
  588. (s->num_p2p_clients - 1) * ETH_ALEN,
  589. addr, ETH_ALEN);
  590. }
  591. #ifndef CONFIG_NO_CONFIG_WRITE
  592. if (wpa_s->parent->conf->update_config &&
  593. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  594. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  595. #endif /* CONFIG_NO_CONFIG_WRITE */
  596. }
  597. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  598. int success)
  599. {
  600. struct wpa_ssid *ssid;
  601. const char *ssid_txt;
  602. int client;
  603. int persistent;
  604. u8 go_dev_addr[ETH_ALEN];
  605. int network_id = -1;
  606. /*
  607. * This callback is likely called for the main interface. Update wpa_s
  608. * to use the group interface if a new interface was created for the
  609. * group.
  610. */
  611. if (wpa_s->global->p2p_group_formation)
  612. wpa_s = wpa_s->global->p2p_group_formation;
  613. wpa_s->global->p2p_group_formation = NULL;
  614. wpa_s->p2p_in_provisioning = 0;
  615. if (!success) {
  616. wpa_msg_global(wpa_s->parent, MSG_INFO,
  617. P2P_EVENT_GROUP_FORMATION_FAILURE);
  618. wpas_p2p_group_delete(wpa_s,
  619. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  620. return;
  621. }
  622. wpa_msg_global(wpa_s->parent, MSG_INFO,
  623. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  624. ssid = wpa_s->current_ssid;
  625. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  626. ssid->mode = WPAS_MODE_P2P_GO;
  627. p2p_group_notif_formation_done(wpa_s->p2p_group);
  628. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  629. }
  630. persistent = 0;
  631. if (ssid) {
  632. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  633. client = ssid->mode == WPAS_MODE_INFRA;
  634. if (ssid->mode == WPAS_MODE_P2P_GO) {
  635. persistent = ssid->p2p_persistent_group;
  636. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  637. ETH_ALEN);
  638. } else
  639. persistent = wpas_p2p_persistent_group(wpa_s,
  640. go_dev_addr,
  641. ssid->ssid,
  642. ssid->ssid_len);
  643. } else {
  644. ssid_txt = "";
  645. client = wpa_s->p2p_group_interface ==
  646. P2P_GROUP_INTERFACE_CLIENT;
  647. os_memset(go_dev_addr, 0, ETH_ALEN);
  648. }
  649. wpa_s->show_group_started = 0;
  650. if (client) {
  651. /*
  652. * Indicate event only after successfully completed 4-way
  653. * handshake, i.e., when the interface is ready for data
  654. * packets.
  655. */
  656. wpa_s->show_group_started = 1;
  657. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  658. char psk[65];
  659. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  660. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  661. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  662. MACSTR "%s",
  663. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  664. MAC2STR(go_dev_addr),
  665. persistent ? " [PERSISTENT]" : "");
  666. wpas_p2p_cross_connect_setup(wpa_s);
  667. wpas_p2p_set_group_idle_timeout(wpa_s);
  668. } else {
  669. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  670. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  671. "go_dev_addr=" MACSTR "%s",
  672. wpa_s->ifname, ssid_txt,
  673. ssid ? ssid->frequency : 0,
  674. ssid && ssid->passphrase ? ssid->passphrase : "",
  675. MAC2STR(go_dev_addr),
  676. persistent ? " [PERSISTENT]" : "");
  677. wpas_p2p_cross_connect_setup(wpa_s);
  678. wpas_p2p_set_group_idle_timeout(wpa_s);
  679. }
  680. if (persistent)
  681. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  682. ssid, go_dev_addr);
  683. else {
  684. os_free(wpa_s->global->add_psk);
  685. wpa_s->global->add_psk = NULL;
  686. }
  687. if (network_id < 0 && ssid)
  688. network_id = ssid->id;
  689. if (!client) {
  690. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  691. os_get_time(&wpa_s->global->p2p_go_wait_client);
  692. }
  693. }
  694. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  695. unsigned int freq,
  696. const u8 *dst, const u8 *src,
  697. const u8 *bssid,
  698. const u8 *data, size_t data_len,
  699. enum offchannel_send_action_result
  700. result)
  701. {
  702. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  703. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  704. return;
  705. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  706. return;
  707. switch (result) {
  708. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  709. res = P2P_SEND_ACTION_SUCCESS;
  710. break;
  711. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  712. res = P2P_SEND_ACTION_NO_ACK;
  713. break;
  714. case OFFCHANNEL_SEND_ACTION_FAILED:
  715. res = P2P_SEND_ACTION_FAILED;
  716. break;
  717. }
  718. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  719. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  720. wpa_s->pending_pd_before_join &&
  721. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  722. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  723. wpa_s->p2p_fallback_to_go_neg) {
  724. wpa_s->pending_pd_before_join = 0;
  725. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  726. "during p2p_connect-auto");
  727. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  728. return;
  729. }
  730. }
  731. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  732. const u8 *src, const u8 *bssid, const u8 *buf,
  733. size_t len, unsigned int wait_time)
  734. {
  735. struct wpa_supplicant *wpa_s = ctx;
  736. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  737. wait_time,
  738. wpas_p2p_send_action_tx_status, 1);
  739. }
  740. static void wpas_send_action_done(void *ctx)
  741. {
  742. struct wpa_supplicant *wpa_s = ctx;
  743. offchannel_send_action_done(wpa_s);
  744. }
  745. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  746. struct p2p_go_neg_results *params)
  747. {
  748. if (wpa_s->go_params == NULL) {
  749. wpa_s->go_params = os_malloc(sizeof(*params));
  750. if (wpa_s->go_params == NULL)
  751. return -1;
  752. }
  753. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  754. return 0;
  755. }
  756. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  757. struct p2p_go_neg_results *res)
  758. {
  759. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  760. MAC2STR(res->peer_interface_addr));
  761. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  762. res->ssid, res->ssid_len);
  763. wpa_supplicant_ap_deinit(wpa_s);
  764. wpas_copy_go_neg_results(wpa_s, res);
  765. if (res->wps_method == WPS_PBC)
  766. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  767. else {
  768. u16 dev_pw_id = DEV_PW_DEFAULT;
  769. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  770. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  771. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  772. wpa_s->p2p_pin, 1, dev_pw_id);
  773. }
  774. }
  775. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  776. struct wpa_ssid *ssid)
  777. {
  778. struct wpa_ssid *persistent;
  779. struct psk_list_entry *psk;
  780. struct hostapd_data *hapd;
  781. if (!wpa_s->ap_iface)
  782. return;
  783. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  784. ssid->ssid_len);
  785. if (persistent == NULL)
  786. return;
  787. hapd = wpa_s->ap_iface->bss[0];
  788. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  789. list) {
  790. struct hostapd_wpa_psk *hpsk;
  791. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  792. MACSTR " psk=%d",
  793. MAC2STR(psk->addr), psk->p2p);
  794. hpsk = os_zalloc(sizeof(*hpsk));
  795. if (hpsk == NULL)
  796. break;
  797. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  798. if (psk->p2p)
  799. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  800. else
  801. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  802. hpsk->next = hapd->conf->ssid.wpa_psk;
  803. hapd->conf->ssid.wpa_psk = hpsk;
  804. }
  805. }
  806. static void p2p_go_configured(void *ctx, void *data)
  807. {
  808. struct wpa_supplicant *wpa_s = ctx;
  809. struct p2p_go_neg_results *params = data;
  810. struct wpa_ssid *ssid;
  811. int network_id = -1;
  812. ssid = wpa_s->current_ssid;
  813. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  814. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  815. if (wpa_s->global->p2p_group_formation == wpa_s)
  816. wpa_s->global->p2p_group_formation = NULL;
  817. if (os_strlen(params->passphrase) > 0) {
  818. wpa_msg_global(wpa_s->parent, MSG_INFO,
  819. P2P_EVENT_GROUP_STARTED
  820. "%s GO ssid=\"%s\" freq=%d "
  821. "passphrase=\"%s\" go_dev_addr=" MACSTR
  822. "%s", wpa_s->ifname,
  823. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  824. ssid->frequency, params->passphrase,
  825. MAC2STR(wpa_s->global->p2p_dev_addr),
  826. params->persistent_group ?
  827. " [PERSISTENT]" : "");
  828. } else {
  829. char psk[65];
  830. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  831. sizeof(params->psk));
  832. wpa_msg_global(wpa_s->parent, MSG_INFO,
  833. P2P_EVENT_GROUP_STARTED
  834. "%s GO ssid=\"%s\" freq=%d psk=%s "
  835. "go_dev_addr=" MACSTR "%s",
  836. wpa_s->ifname,
  837. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  838. ssid->frequency, psk,
  839. MAC2STR(wpa_s->global->p2p_dev_addr),
  840. params->persistent_group ?
  841. " [PERSISTENT]" : "");
  842. }
  843. if (params->persistent_group) {
  844. network_id = wpas_p2p_store_persistent_group(
  845. wpa_s->parent, ssid,
  846. wpa_s->global->p2p_dev_addr);
  847. wpas_p2p_add_psk_list(wpa_s, ssid);
  848. }
  849. if (network_id < 0)
  850. network_id = ssid->id;
  851. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  852. wpas_p2p_cross_connect_setup(wpa_s);
  853. wpas_p2p_set_group_idle_timeout(wpa_s);
  854. return;
  855. }
  856. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  857. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  858. params->peer_interface_addr)) {
  859. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  860. "filtering");
  861. return;
  862. }
  863. if (params->wps_method == WPS_PBC)
  864. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  865. params->peer_device_addr);
  866. else if (wpa_s->p2p_pin[0])
  867. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  868. wpa_s->p2p_pin, NULL, 0, 0);
  869. os_free(wpa_s->go_params);
  870. wpa_s->go_params = NULL;
  871. }
  872. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  873. struct p2p_go_neg_results *params,
  874. int group_formation)
  875. {
  876. struct wpa_ssid *ssid;
  877. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  878. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  879. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  880. "results");
  881. return;
  882. }
  883. ssid = wpa_config_add_network(wpa_s->conf);
  884. if (ssid == NULL) {
  885. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  886. return;
  887. }
  888. wpa_s->show_group_started = 0;
  889. wpa_config_set_network_defaults(ssid);
  890. ssid->temporary = 1;
  891. ssid->p2p_group = 1;
  892. ssid->p2p_persistent_group = params->persistent_group;
  893. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  894. WPAS_MODE_P2P_GO;
  895. ssid->frequency = params->freq;
  896. ssid->ht40 = params->ht40;
  897. ssid->ssid = os_zalloc(params->ssid_len + 1);
  898. if (ssid->ssid) {
  899. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  900. ssid->ssid_len = params->ssid_len;
  901. }
  902. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  903. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  904. ssid->proto = WPA_PROTO_RSN;
  905. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  906. if (os_strlen(params->passphrase) > 0) {
  907. ssid->passphrase = os_strdup(params->passphrase);
  908. if (ssid->passphrase == NULL) {
  909. wpa_msg_global(wpa_s, MSG_ERROR,
  910. "P2P: Failed to copy passphrase for GO");
  911. wpa_config_remove_network(wpa_s->conf, ssid->id);
  912. return;
  913. }
  914. } else
  915. ssid->passphrase = NULL;
  916. ssid->psk_set = params->psk_set;
  917. if (ssid->psk_set)
  918. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  919. else if (ssid->passphrase)
  920. wpa_config_update_psk(ssid);
  921. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  922. wpa_s->ap_configured_cb = p2p_go_configured;
  923. wpa_s->ap_configured_cb_ctx = wpa_s;
  924. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  925. wpa_s->connect_without_scan = ssid;
  926. wpa_s->reassociate = 1;
  927. wpa_s->disconnected = 0;
  928. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  929. "start GO)");
  930. wpa_supplicant_req_scan(wpa_s, 0, 0);
  931. }
  932. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  933. const struct wpa_supplicant *src)
  934. {
  935. struct wpa_config *d;
  936. const struct wpa_config *s;
  937. d = dst->conf;
  938. s = src->conf;
  939. #define C(n) if (s->n) d->n = os_strdup(s->n)
  940. C(device_name);
  941. C(manufacturer);
  942. C(model_name);
  943. C(model_number);
  944. C(serial_number);
  945. C(config_methods);
  946. #undef C
  947. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  948. os_memcpy(d->sec_device_type, s->sec_device_type,
  949. sizeof(d->sec_device_type));
  950. d->num_sec_device_types = s->num_sec_device_types;
  951. d->p2p_group_idle = s->p2p_group_idle;
  952. d->p2p_intra_bss = s->p2p_intra_bss;
  953. d->persistent_reconnect = s->persistent_reconnect;
  954. d->max_num_sta = s->max_num_sta;
  955. d->pbc_in_m1 = s->pbc_in_m1;
  956. d->ignore_old_scan_res = s->ignore_old_scan_res;
  957. d->beacon_int = s->beacon_int;
  958. d->disassoc_low_ack = s->disassoc_low_ack;
  959. }
  960. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  961. char *ifname, size_t len)
  962. {
  963. char *ifname_ptr = wpa_s->ifname;
  964. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  965. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  966. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  967. }
  968. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  969. if (os_strlen(ifname) >= IFNAMSIZ &&
  970. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  971. /* Try to avoid going over the IFNAMSIZ length limit */
  972. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  973. wpa_s->p2p_group_idx);
  974. }
  975. }
  976. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  977. enum wpa_driver_if_type type)
  978. {
  979. char ifname[120], force_ifname[120];
  980. if (wpa_s->pending_interface_name[0]) {
  981. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  982. "- skip creation of a new one");
  983. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  984. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  985. "unknown?! ifname='%s'",
  986. wpa_s->pending_interface_name);
  987. return -1;
  988. }
  989. return 0;
  990. }
  991. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  992. force_ifname[0] = '\0';
  993. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  994. ifname);
  995. wpa_s->p2p_group_idx++;
  996. wpa_s->pending_interface_type = type;
  997. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  998. wpa_s->pending_interface_addr, NULL) < 0) {
  999. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1000. "interface");
  1001. return -1;
  1002. }
  1003. if (force_ifname[0]) {
  1004. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1005. force_ifname);
  1006. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1007. sizeof(wpa_s->pending_interface_name));
  1008. } else
  1009. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1010. sizeof(wpa_s->pending_interface_name));
  1011. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1012. MACSTR, wpa_s->pending_interface_name,
  1013. MAC2STR(wpa_s->pending_interface_addr));
  1014. return 0;
  1015. }
  1016. static void wpas_p2p_remove_pending_group_interface(
  1017. struct wpa_supplicant *wpa_s)
  1018. {
  1019. if (!wpa_s->pending_interface_name[0] ||
  1020. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1021. return; /* No pending virtual interface */
  1022. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1023. wpa_s->pending_interface_name);
  1024. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1025. wpa_s->pending_interface_name);
  1026. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1027. wpa_s->pending_interface_name[0] = '\0';
  1028. }
  1029. static struct wpa_supplicant *
  1030. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1031. {
  1032. struct wpa_interface iface;
  1033. struct wpa_supplicant *group_wpa_s;
  1034. if (!wpa_s->pending_interface_name[0]) {
  1035. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1036. if (!wpas_p2p_create_iface(wpa_s))
  1037. return NULL;
  1038. /*
  1039. * Something has forced us to remove the pending interface; try
  1040. * to create a new one and hope for the best that we will get
  1041. * the same local address.
  1042. */
  1043. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1044. WPA_IF_P2P_CLIENT) < 0)
  1045. return NULL;
  1046. }
  1047. os_memset(&iface, 0, sizeof(iface));
  1048. iface.ifname = wpa_s->pending_interface_name;
  1049. iface.driver = wpa_s->driver->name;
  1050. if (wpa_s->conf->ctrl_interface == NULL &&
  1051. wpa_s->parent != wpa_s &&
  1052. wpa_s->p2p_mgmt &&
  1053. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1054. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1055. else
  1056. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1057. iface.driver_param = wpa_s->conf->driver_param;
  1058. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1059. if (group_wpa_s == NULL) {
  1060. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1061. "wpa_supplicant interface");
  1062. return NULL;
  1063. }
  1064. wpa_s->pending_interface_name[0] = '\0';
  1065. group_wpa_s->parent = wpa_s;
  1066. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1067. P2P_GROUP_INTERFACE_CLIENT;
  1068. wpa_s->global->p2p_group_formation = group_wpa_s;
  1069. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1070. return group_wpa_s;
  1071. }
  1072. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1073. void *timeout_ctx)
  1074. {
  1075. struct wpa_supplicant *wpa_s = eloop_ctx;
  1076. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1077. wpas_p2p_group_formation_failed(wpa_s);
  1078. }
  1079. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1080. {
  1081. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1082. wpa_s->parent, NULL);
  1083. if (wpa_s->global->p2p)
  1084. p2p_group_formation_failed(wpa_s->global->p2p);
  1085. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1086. wpa_drv_p2p_group_formation_failed(wpa_s);
  1087. wpas_group_formation_completed(wpa_s, 0);
  1088. }
  1089. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1090. {
  1091. struct wpa_supplicant *wpa_s = ctx;
  1092. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1093. wpa_drv_cancel_remain_on_channel(wpa_s);
  1094. wpa_s->off_channel_freq = 0;
  1095. wpa_s->roc_waiting_drv_freq = 0;
  1096. }
  1097. if (res->status) {
  1098. wpa_msg_global(wpa_s, MSG_INFO,
  1099. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1100. res->status);
  1101. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1102. wpas_p2p_remove_pending_group_interface(wpa_s);
  1103. return;
  1104. }
  1105. if (wpa_s->p2p_go_ht40)
  1106. res->ht40 = 1;
  1107. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1108. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1109. " wps_method=%s",
  1110. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1111. MAC2STR(res->peer_device_addr),
  1112. MAC2STR(res->peer_interface_addr),
  1113. p2p_wps_method_text(res->wps_method));
  1114. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1115. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1116. struct wpa_ssid *ssid;
  1117. ssid = wpa_config_get_network(wpa_s->conf,
  1118. wpa_s->p2p_persistent_id);
  1119. if (ssid && ssid->disabled == 2 &&
  1120. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1121. size_t len = os_strlen(ssid->passphrase);
  1122. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1123. "on requested persistent group");
  1124. os_memcpy(res->passphrase, ssid->passphrase, len);
  1125. res->passphrase[len] = '\0';
  1126. }
  1127. }
  1128. if (wpa_s->create_p2p_iface) {
  1129. struct wpa_supplicant *group_wpa_s =
  1130. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1131. if (group_wpa_s == NULL) {
  1132. wpas_p2p_remove_pending_group_interface(wpa_s);
  1133. return;
  1134. }
  1135. if (group_wpa_s != wpa_s) {
  1136. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1137. sizeof(group_wpa_s->p2p_pin));
  1138. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1139. }
  1140. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1141. wpa_s->pending_interface_name[0] = '\0';
  1142. group_wpa_s->p2p_in_provisioning = 1;
  1143. if (res->role_go)
  1144. wpas_start_wps_go(group_wpa_s, res, 1);
  1145. else
  1146. wpas_start_wps_enrollee(group_wpa_s, res);
  1147. } else {
  1148. wpa_s->p2p_in_provisioning = 1;
  1149. wpa_s->global->p2p_group_formation = wpa_s;
  1150. if (res->role_go)
  1151. wpas_start_wps_go(wpa_s, res, 1);
  1152. else
  1153. wpas_start_wps_enrollee(ctx, res);
  1154. }
  1155. wpa_s->p2p_long_listen = 0;
  1156. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1157. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1158. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1159. (res->peer_config_timeout % 100) * 10000,
  1160. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1161. }
  1162. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1163. {
  1164. struct wpa_supplicant *wpa_s = ctx;
  1165. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1166. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1167. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1168. }
  1169. void wpas_dev_found(void *ctx, const u8 *addr,
  1170. const struct p2p_peer_info *info,
  1171. int new_device)
  1172. {
  1173. #ifndef CONFIG_NO_STDOUT_DEBUG
  1174. struct wpa_supplicant *wpa_s = ctx;
  1175. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1176. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1177. " p2p_dev_addr=" MACSTR
  1178. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1179. "dev_capab=0x%x group_capab=0x%x",
  1180. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1181. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1182. sizeof(devtype)),
  1183. info->device_name, info->config_methods,
  1184. info->dev_capab, info->group_capab);
  1185. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1186. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1187. }
  1188. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1189. {
  1190. struct wpa_supplicant *wpa_s = ctx;
  1191. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1192. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1193. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1194. }
  1195. static void wpas_find_stopped(void *ctx)
  1196. {
  1197. struct wpa_supplicant *wpa_s = ctx;
  1198. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1199. }
  1200. static int wpas_start_listen(void *ctx, unsigned int freq,
  1201. unsigned int duration,
  1202. const struct wpabuf *probe_resp_ie)
  1203. {
  1204. struct wpa_supplicant *wpa_s = ctx;
  1205. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1206. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1207. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1208. "report received Probe Request frames");
  1209. return -1;
  1210. }
  1211. wpa_s->pending_listen_freq = freq;
  1212. wpa_s->pending_listen_duration = duration;
  1213. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1214. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1215. "to remain on channel (%u MHz) for Listen "
  1216. "state", freq);
  1217. wpa_s->pending_listen_freq = 0;
  1218. return -1;
  1219. }
  1220. wpa_s->off_channel_freq = 0;
  1221. wpa_s->roc_waiting_drv_freq = freq;
  1222. return 0;
  1223. }
  1224. static void wpas_stop_listen(void *ctx)
  1225. {
  1226. struct wpa_supplicant *wpa_s = ctx;
  1227. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1228. wpa_drv_cancel_remain_on_channel(wpa_s);
  1229. wpa_s->off_channel_freq = 0;
  1230. wpa_s->roc_waiting_drv_freq = 0;
  1231. }
  1232. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1233. wpa_drv_probe_req_report(wpa_s, 0);
  1234. }
  1235. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1236. {
  1237. struct wpa_supplicant *wpa_s = ctx;
  1238. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1239. }
  1240. /*
  1241. * DNS Header section is used only to calculate compression pointers, so the
  1242. * contents of this data does not matter, but the length needs to be reserved
  1243. * in the virtual packet.
  1244. */
  1245. #define DNS_HEADER_LEN 12
  1246. /*
  1247. * 27-octet in-memory packet from P2P specification containing two implied
  1248. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1249. */
  1250. #define P2P_SD_IN_MEMORY_LEN 27
  1251. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1252. u8 **spos, const u8 *end)
  1253. {
  1254. while (*spos < end) {
  1255. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1256. int len;
  1257. if (val == 1 || val == 2) {
  1258. /* These are reserved values in RFC 1035 */
  1259. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1260. "sequence starting with 0x%x", val);
  1261. return -1;
  1262. }
  1263. if (val == 3) {
  1264. u16 offset;
  1265. u8 *spos_tmp;
  1266. /* Offset */
  1267. if (*spos + 2 > end) {
  1268. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1269. "DNS offset field");
  1270. return -1;
  1271. }
  1272. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1273. if (offset >= *spos - start) {
  1274. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1275. "pointer offset %u", offset);
  1276. return -1;
  1277. }
  1278. (*spos) += 2;
  1279. spos_tmp = start + offset;
  1280. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1281. &spos_tmp,
  1282. *spos - 2);
  1283. }
  1284. /* Label */
  1285. len = (*spos)[0] & 0x3f;
  1286. if (len == 0)
  1287. return 0;
  1288. (*spos)++;
  1289. if (*spos + len > end) {
  1290. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1291. "sequence - no room for label with length "
  1292. "%u", len);
  1293. return -1;
  1294. }
  1295. if (*upos + len + 2 > uend)
  1296. return -2;
  1297. os_memcpy(*upos, *spos, len);
  1298. *spos += len;
  1299. *upos += len;
  1300. (*upos)[0] = '.';
  1301. (*upos)++;
  1302. (*upos)[0] = '\0';
  1303. }
  1304. return 0;
  1305. }
  1306. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1307. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1308. * not large enough */
  1309. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1310. size_t msg_len, size_t offset)
  1311. {
  1312. /* 27-octet in-memory packet from P2P specification */
  1313. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1314. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1315. u8 *tmp, *end, *spos;
  1316. char *upos, *uend;
  1317. int ret = 0;
  1318. if (buf_len < 2)
  1319. return -1;
  1320. if (offset > msg_len)
  1321. return -1;
  1322. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1323. if (tmp == NULL)
  1324. return -1;
  1325. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1326. end = spos + msg_len;
  1327. spos += offset;
  1328. os_memset(tmp, 0, DNS_HEADER_LEN);
  1329. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1330. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1331. upos = buf;
  1332. uend = buf + buf_len;
  1333. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1334. if (ret) {
  1335. os_free(tmp);
  1336. return ret;
  1337. }
  1338. if (upos == buf) {
  1339. upos[0] = '.';
  1340. upos[1] = '\0';
  1341. } else if (upos[-1] == '.')
  1342. upos[-1] = '\0';
  1343. os_free(tmp);
  1344. return 0;
  1345. }
  1346. static struct p2p_srv_bonjour *
  1347. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1348. const struct wpabuf *query)
  1349. {
  1350. struct p2p_srv_bonjour *bsrv;
  1351. size_t len;
  1352. len = wpabuf_len(query);
  1353. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1354. struct p2p_srv_bonjour, list) {
  1355. if (len == wpabuf_len(bsrv->query) &&
  1356. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1357. len) == 0)
  1358. return bsrv;
  1359. }
  1360. return NULL;
  1361. }
  1362. static struct p2p_srv_upnp *
  1363. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1364. const char *service)
  1365. {
  1366. struct p2p_srv_upnp *usrv;
  1367. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1368. struct p2p_srv_upnp, list) {
  1369. if (version == usrv->version &&
  1370. os_strcmp(service, usrv->service) == 0)
  1371. return usrv;
  1372. }
  1373. return NULL;
  1374. }
  1375. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1376. u8 srv_trans_id)
  1377. {
  1378. u8 *len_pos;
  1379. if (wpabuf_tailroom(resp) < 5)
  1380. return;
  1381. /* Length (to be filled) */
  1382. len_pos = wpabuf_put(resp, 2);
  1383. wpabuf_put_u8(resp, srv_proto);
  1384. wpabuf_put_u8(resp, srv_trans_id);
  1385. /* Status Code */
  1386. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1387. /* Response Data: empty */
  1388. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1389. }
  1390. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1391. struct wpabuf *resp, u8 srv_trans_id)
  1392. {
  1393. struct p2p_srv_bonjour *bsrv;
  1394. u8 *len_pos;
  1395. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1396. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1397. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1398. return;
  1399. }
  1400. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1401. struct p2p_srv_bonjour, list) {
  1402. if (wpabuf_tailroom(resp) <
  1403. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1404. return;
  1405. /* Length (to be filled) */
  1406. len_pos = wpabuf_put(resp, 2);
  1407. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1408. wpabuf_put_u8(resp, srv_trans_id);
  1409. /* Status Code */
  1410. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1411. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1412. wpabuf_head(bsrv->resp),
  1413. wpabuf_len(bsrv->resp));
  1414. /* Response Data */
  1415. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1416. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1417. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1418. 2);
  1419. }
  1420. }
  1421. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1422. size_t query_len)
  1423. {
  1424. char str_rx[256], str_srv[256];
  1425. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1426. return 0; /* Too short to include DNS Type and Version */
  1427. if (os_memcmp(query + query_len - 3,
  1428. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1429. 3) != 0)
  1430. return 0; /* Mismatch in DNS Type or Version */
  1431. if (query_len == wpabuf_len(bsrv->query) &&
  1432. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1433. return 1; /* Binary match */
  1434. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1435. 0))
  1436. return 0; /* Failed to uncompress query */
  1437. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1438. wpabuf_head(bsrv->query),
  1439. wpabuf_len(bsrv->query) - 3, 0))
  1440. return 0; /* Failed to uncompress service */
  1441. return os_strcmp(str_rx, str_srv) == 0;
  1442. }
  1443. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1444. struct wpabuf *resp, u8 srv_trans_id,
  1445. const u8 *query, size_t query_len)
  1446. {
  1447. struct p2p_srv_bonjour *bsrv;
  1448. u8 *len_pos;
  1449. int matches = 0;
  1450. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1451. query, query_len);
  1452. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1453. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1454. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1455. srv_trans_id);
  1456. return;
  1457. }
  1458. if (query_len == 0) {
  1459. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1460. return;
  1461. }
  1462. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1463. struct p2p_srv_bonjour, list) {
  1464. if (!match_bonjour_query(bsrv, query, query_len))
  1465. continue;
  1466. if (wpabuf_tailroom(resp) <
  1467. 5 + query_len + wpabuf_len(bsrv->resp))
  1468. return;
  1469. matches++;
  1470. /* Length (to be filled) */
  1471. len_pos = wpabuf_put(resp, 2);
  1472. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1473. wpabuf_put_u8(resp, srv_trans_id);
  1474. /* Status Code */
  1475. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1476. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1477. wpabuf_head(bsrv->resp),
  1478. wpabuf_len(bsrv->resp));
  1479. /* Response Data */
  1480. wpabuf_put_data(resp, query, query_len); /* Key */
  1481. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1482. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1483. }
  1484. if (matches == 0) {
  1485. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1486. "available");
  1487. if (wpabuf_tailroom(resp) < 5)
  1488. return;
  1489. /* Length (to be filled) */
  1490. len_pos = wpabuf_put(resp, 2);
  1491. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1492. wpabuf_put_u8(resp, srv_trans_id);
  1493. /* Status Code */
  1494. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1495. /* Response Data: empty */
  1496. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1497. 2);
  1498. }
  1499. }
  1500. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1501. struct wpabuf *resp, u8 srv_trans_id)
  1502. {
  1503. struct p2p_srv_upnp *usrv;
  1504. u8 *len_pos;
  1505. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1506. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1507. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1508. return;
  1509. }
  1510. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1511. struct p2p_srv_upnp, list) {
  1512. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1513. return;
  1514. /* Length (to be filled) */
  1515. len_pos = wpabuf_put(resp, 2);
  1516. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1517. wpabuf_put_u8(resp, srv_trans_id);
  1518. /* Status Code */
  1519. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1520. /* Response Data */
  1521. wpabuf_put_u8(resp, usrv->version);
  1522. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1523. usrv->service);
  1524. wpabuf_put_str(resp, usrv->service);
  1525. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1526. 2);
  1527. }
  1528. }
  1529. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1530. struct wpabuf *resp, u8 srv_trans_id,
  1531. const u8 *query, size_t query_len)
  1532. {
  1533. struct p2p_srv_upnp *usrv;
  1534. u8 *len_pos;
  1535. u8 version;
  1536. char *str;
  1537. int count = 0;
  1538. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1539. query, query_len);
  1540. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1541. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1542. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1543. srv_trans_id);
  1544. return;
  1545. }
  1546. if (query_len == 0) {
  1547. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1548. return;
  1549. }
  1550. if (wpabuf_tailroom(resp) < 5)
  1551. return;
  1552. /* Length (to be filled) */
  1553. len_pos = wpabuf_put(resp, 2);
  1554. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1555. wpabuf_put_u8(resp, srv_trans_id);
  1556. version = query[0];
  1557. str = os_malloc(query_len);
  1558. if (str == NULL)
  1559. return;
  1560. os_memcpy(str, query + 1, query_len - 1);
  1561. str[query_len - 1] = '\0';
  1562. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1563. struct p2p_srv_upnp, list) {
  1564. if (version != usrv->version)
  1565. continue;
  1566. if (os_strcmp(str, "ssdp:all") != 0 &&
  1567. os_strstr(usrv->service, str) == NULL)
  1568. continue;
  1569. if (wpabuf_tailroom(resp) < 2)
  1570. break;
  1571. if (count == 0) {
  1572. /* Status Code */
  1573. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1574. /* Response Data */
  1575. wpabuf_put_u8(resp, version);
  1576. } else
  1577. wpabuf_put_u8(resp, ',');
  1578. count++;
  1579. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1580. usrv->service);
  1581. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1582. break;
  1583. wpabuf_put_str(resp, usrv->service);
  1584. }
  1585. os_free(str);
  1586. if (count == 0) {
  1587. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1588. "available");
  1589. /* Status Code */
  1590. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1591. /* Response Data: empty */
  1592. }
  1593. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1594. }
  1595. #ifdef CONFIG_WIFI_DISPLAY
  1596. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1597. struct wpabuf *resp, u8 srv_trans_id,
  1598. const u8 *query, size_t query_len)
  1599. {
  1600. const u8 *pos;
  1601. u8 role;
  1602. u8 *len_pos;
  1603. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1604. if (!wpa_s->global->wifi_display) {
  1605. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1606. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1607. srv_trans_id);
  1608. return;
  1609. }
  1610. if (query_len < 1) {
  1611. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1612. "Role");
  1613. return;
  1614. }
  1615. if (wpabuf_tailroom(resp) < 5)
  1616. return;
  1617. pos = query;
  1618. role = *pos++;
  1619. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1620. /* TODO: role specific handling */
  1621. /* Length (to be filled) */
  1622. len_pos = wpabuf_put(resp, 2);
  1623. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1624. wpabuf_put_u8(resp, srv_trans_id);
  1625. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1626. while (pos < query + query_len) {
  1627. if (*pos < MAX_WFD_SUBELEMS &&
  1628. wpa_s->global->wfd_subelem[*pos] &&
  1629. wpabuf_tailroom(resp) >=
  1630. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1631. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1632. "subelement %u", *pos);
  1633. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1634. }
  1635. pos++;
  1636. }
  1637. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1638. }
  1639. #endif /* CONFIG_WIFI_DISPLAY */
  1640. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1641. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1642. {
  1643. struct wpa_supplicant *wpa_s = ctx;
  1644. const u8 *pos = tlvs;
  1645. const u8 *end = tlvs + tlvs_len;
  1646. const u8 *tlv_end;
  1647. u16 slen;
  1648. struct wpabuf *resp;
  1649. u8 srv_proto, srv_trans_id;
  1650. size_t buf_len;
  1651. char *buf;
  1652. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1653. tlvs, tlvs_len);
  1654. buf_len = 2 * tlvs_len + 1;
  1655. buf = os_malloc(buf_len);
  1656. if (buf) {
  1657. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1658. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1659. MACSTR " %u %u %s",
  1660. freq, MAC2STR(sa), dialog_token, update_indic,
  1661. buf);
  1662. os_free(buf);
  1663. }
  1664. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1665. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1666. update_indic, tlvs, tlvs_len);
  1667. return; /* to be processed by an external program */
  1668. }
  1669. resp = wpabuf_alloc(10000);
  1670. if (resp == NULL)
  1671. return;
  1672. while (pos + 1 < end) {
  1673. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1674. slen = WPA_GET_LE16(pos);
  1675. pos += 2;
  1676. if (pos + slen > end || slen < 2) {
  1677. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1678. "length");
  1679. wpabuf_free(resp);
  1680. return;
  1681. }
  1682. tlv_end = pos + slen;
  1683. srv_proto = *pos++;
  1684. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1685. srv_proto);
  1686. srv_trans_id = *pos++;
  1687. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1688. srv_trans_id);
  1689. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1690. pos, tlv_end - pos);
  1691. if (wpa_s->force_long_sd) {
  1692. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1693. "response");
  1694. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1695. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1696. goto done;
  1697. }
  1698. switch (srv_proto) {
  1699. case P2P_SERV_ALL_SERVICES:
  1700. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1701. "for all services");
  1702. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1703. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1704. wpa_printf(MSG_DEBUG, "P2P: No service "
  1705. "discovery protocols available");
  1706. wpas_sd_add_proto_not_avail(
  1707. resp, P2P_SERV_ALL_SERVICES,
  1708. srv_trans_id);
  1709. break;
  1710. }
  1711. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1712. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1713. break;
  1714. case P2P_SERV_BONJOUR:
  1715. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1716. pos, tlv_end - pos);
  1717. break;
  1718. case P2P_SERV_UPNP:
  1719. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1720. pos, tlv_end - pos);
  1721. break;
  1722. #ifdef CONFIG_WIFI_DISPLAY
  1723. case P2P_SERV_WIFI_DISPLAY:
  1724. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1725. pos, tlv_end - pos);
  1726. break;
  1727. #endif /* CONFIG_WIFI_DISPLAY */
  1728. default:
  1729. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1730. "protocol %u", srv_proto);
  1731. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1732. srv_trans_id);
  1733. break;
  1734. }
  1735. pos = tlv_end;
  1736. }
  1737. done:
  1738. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1739. update_indic, tlvs, tlvs_len);
  1740. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1741. wpabuf_free(resp);
  1742. }
  1743. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1744. const u8 *tlvs, size_t tlvs_len)
  1745. {
  1746. struct wpa_supplicant *wpa_s = ctx;
  1747. const u8 *pos = tlvs;
  1748. const u8 *end = tlvs + tlvs_len;
  1749. const u8 *tlv_end;
  1750. u16 slen;
  1751. size_t buf_len;
  1752. char *buf;
  1753. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1754. tlvs, tlvs_len);
  1755. if (tlvs_len > 1500) {
  1756. /* TODO: better way for handling this */
  1757. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1758. P2P_EVENT_SERV_DISC_RESP MACSTR
  1759. " %u <long response: %u bytes>",
  1760. MAC2STR(sa), update_indic,
  1761. (unsigned int) tlvs_len);
  1762. } else {
  1763. buf_len = 2 * tlvs_len + 1;
  1764. buf = os_malloc(buf_len);
  1765. if (buf) {
  1766. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1767. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1768. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1769. MAC2STR(sa), update_indic, buf);
  1770. os_free(buf);
  1771. }
  1772. }
  1773. while (pos < end) {
  1774. u8 srv_proto, srv_trans_id, status;
  1775. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1776. slen = WPA_GET_LE16(pos);
  1777. pos += 2;
  1778. if (pos + slen > end || slen < 3) {
  1779. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1780. "length");
  1781. return;
  1782. }
  1783. tlv_end = pos + slen;
  1784. srv_proto = *pos++;
  1785. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1786. srv_proto);
  1787. srv_trans_id = *pos++;
  1788. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1789. srv_trans_id);
  1790. status = *pos++;
  1791. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1792. status);
  1793. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1794. pos, tlv_end - pos);
  1795. pos = tlv_end;
  1796. }
  1797. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1798. }
  1799. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1800. const struct wpabuf *tlvs)
  1801. {
  1802. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1803. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1804. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1805. return 0;
  1806. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1807. }
  1808. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1809. u8 version, const char *query)
  1810. {
  1811. struct wpabuf *tlvs;
  1812. u64 ret;
  1813. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1814. if (tlvs == NULL)
  1815. return 0;
  1816. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1817. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1818. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1819. wpabuf_put_u8(tlvs, version);
  1820. wpabuf_put_str(tlvs, query);
  1821. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1822. wpabuf_free(tlvs);
  1823. return ret;
  1824. }
  1825. #ifdef CONFIG_WIFI_DISPLAY
  1826. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  1827. const struct wpabuf *tlvs)
  1828. {
  1829. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1830. return 0;
  1831. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1832. return 0;
  1833. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  1834. }
  1835. #define MAX_WFD_SD_SUBELEMS 20
  1836. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  1837. const char *subelems)
  1838. {
  1839. u8 *len;
  1840. const char *pos;
  1841. int val;
  1842. int count = 0;
  1843. len = wpabuf_put(tlvs, 2);
  1844. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  1845. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  1846. wpabuf_put_u8(tlvs, role);
  1847. pos = subelems;
  1848. while (*pos) {
  1849. val = atoi(pos);
  1850. if (val >= 0 && val < 256) {
  1851. wpabuf_put_u8(tlvs, val);
  1852. count++;
  1853. if (count == MAX_WFD_SD_SUBELEMS)
  1854. break;
  1855. }
  1856. pos = os_strchr(pos + 1, ',');
  1857. if (pos == NULL)
  1858. break;
  1859. pos++;
  1860. }
  1861. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  1862. }
  1863. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  1864. const u8 *dst, const char *role)
  1865. {
  1866. struct wpabuf *tlvs;
  1867. u64 ret;
  1868. const char *subelems;
  1869. u8 id = 1;
  1870. subelems = os_strchr(role, ' ');
  1871. if (subelems == NULL)
  1872. return 0;
  1873. subelems++;
  1874. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  1875. if (tlvs == NULL)
  1876. return 0;
  1877. if (os_strstr(role, "[source]"))
  1878. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  1879. if (os_strstr(role, "[pri-sink]"))
  1880. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  1881. if (os_strstr(role, "[sec-sink]"))
  1882. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  1883. if (os_strstr(role, "[source+sink]"))
  1884. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  1885. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  1886. wpabuf_free(tlvs);
  1887. return ret;
  1888. }
  1889. #endif /* CONFIG_WIFI_DISPLAY */
  1890. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1891. {
  1892. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1893. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1894. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1895. return -1;
  1896. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1897. (void *) (uintptr_t) req);
  1898. }
  1899. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1900. const u8 *dst, u8 dialog_token,
  1901. const struct wpabuf *resp_tlvs)
  1902. {
  1903. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1904. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1905. resp_tlvs);
  1906. return;
  1907. }
  1908. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1909. return;
  1910. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1911. resp_tlvs);
  1912. }
  1913. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1914. {
  1915. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1916. wpa_drv_p2p_service_update(wpa_s);
  1917. return;
  1918. }
  1919. if (wpa_s->global->p2p)
  1920. p2p_sd_service_update(wpa_s->global->p2p);
  1921. }
  1922. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1923. {
  1924. dl_list_del(&bsrv->list);
  1925. wpabuf_free(bsrv->query);
  1926. wpabuf_free(bsrv->resp);
  1927. os_free(bsrv);
  1928. }
  1929. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1930. {
  1931. dl_list_del(&usrv->list);
  1932. os_free(usrv->service);
  1933. os_free(usrv);
  1934. }
  1935. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1936. {
  1937. struct p2p_srv_bonjour *bsrv, *bn;
  1938. struct p2p_srv_upnp *usrv, *un;
  1939. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1940. struct p2p_srv_bonjour, list)
  1941. wpas_p2p_srv_bonjour_free(bsrv);
  1942. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1943. struct p2p_srv_upnp, list)
  1944. wpas_p2p_srv_upnp_free(usrv);
  1945. wpas_p2p_sd_service_update(wpa_s);
  1946. }
  1947. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1948. struct wpabuf *query, struct wpabuf *resp)
  1949. {
  1950. struct p2p_srv_bonjour *bsrv;
  1951. bsrv = os_zalloc(sizeof(*bsrv));
  1952. if (bsrv == NULL)
  1953. return -1;
  1954. bsrv->query = query;
  1955. bsrv->resp = resp;
  1956. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1957. wpas_p2p_sd_service_update(wpa_s);
  1958. return 0;
  1959. }
  1960. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1961. const struct wpabuf *query)
  1962. {
  1963. struct p2p_srv_bonjour *bsrv;
  1964. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1965. if (bsrv == NULL)
  1966. return -1;
  1967. wpas_p2p_srv_bonjour_free(bsrv);
  1968. wpas_p2p_sd_service_update(wpa_s);
  1969. return 0;
  1970. }
  1971. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1972. const char *service)
  1973. {
  1974. struct p2p_srv_upnp *usrv;
  1975. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1976. return 0; /* Already listed */
  1977. usrv = os_zalloc(sizeof(*usrv));
  1978. if (usrv == NULL)
  1979. return -1;
  1980. usrv->version = version;
  1981. usrv->service = os_strdup(service);
  1982. if (usrv->service == NULL) {
  1983. os_free(usrv);
  1984. return -1;
  1985. }
  1986. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1987. wpas_p2p_sd_service_update(wpa_s);
  1988. return 0;
  1989. }
  1990. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1991. const char *service)
  1992. {
  1993. struct p2p_srv_upnp *usrv;
  1994. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1995. if (usrv == NULL)
  1996. return -1;
  1997. wpas_p2p_srv_upnp_free(usrv);
  1998. wpas_p2p_sd_service_update(wpa_s);
  1999. return 0;
  2000. }
  2001. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2002. const u8 *peer, const char *params,
  2003. unsigned int generated_pin)
  2004. {
  2005. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2006. " %08d%s", MAC2STR(peer), generated_pin, params);
  2007. }
  2008. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2009. const u8 *peer, const char *params)
  2010. {
  2011. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2012. "%s", MAC2STR(peer), params);
  2013. }
  2014. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2015. const u8 *dev_addr, const u8 *pri_dev_type,
  2016. const char *dev_name, u16 supp_config_methods,
  2017. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2018. size_t group_id_len)
  2019. {
  2020. struct wpa_supplicant *wpa_s = ctx;
  2021. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2022. char params[300];
  2023. u8 empty_dev_type[8];
  2024. unsigned int generated_pin = 0;
  2025. struct wpa_supplicant *group = NULL;
  2026. if (group_id) {
  2027. for (group = wpa_s->global->ifaces; group; group = group->next)
  2028. {
  2029. struct wpa_ssid *s = group->current_ssid;
  2030. if (s != NULL &&
  2031. s->mode == WPAS_MODE_P2P_GO &&
  2032. group_id_len - ETH_ALEN == s->ssid_len &&
  2033. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2034. s->ssid_len) == 0)
  2035. break;
  2036. }
  2037. }
  2038. if (pri_dev_type == NULL) {
  2039. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2040. pri_dev_type = empty_dev_type;
  2041. }
  2042. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2043. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2044. "dev_capab=0x%x group_capab=0x%x%s%s",
  2045. MAC2STR(dev_addr),
  2046. wps_dev_type_bin2str(pri_dev_type, devtype,
  2047. sizeof(devtype)),
  2048. dev_name, supp_config_methods, dev_capab, group_capab,
  2049. group ? " group=" : "",
  2050. group ? group->ifname : "");
  2051. params[sizeof(params) - 1] = '\0';
  2052. if (config_methods & WPS_CONFIG_DISPLAY) {
  2053. generated_pin = wps_generate_pin();
  2054. wpas_prov_disc_local_display(wpa_s, peer, params,
  2055. generated_pin);
  2056. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2057. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2058. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2059. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2060. MACSTR "%s", MAC2STR(peer), params);
  2061. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2062. P2P_PROV_DISC_SUCCESS,
  2063. config_methods, generated_pin);
  2064. }
  2065. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2066. {
  2067. struct wpa_supplicant *wpa_s = ctx;
  2068. unsigned int generated_pin = 0;
  2069. char params[20];
  2070. if (wpa_s->pending_pd_before_join &&
  2071. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2072. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2073. wpa_s->pending_pd_before_join = 0;
  2074. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2075. "join-existing-group operation");
  2076. wpas_p2p_join_start(wpa_s);
  2077. return;
  2078. }
  2079. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2080. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2081. os_snprintf(params, sizeof(params), " peer_go=%d",
  2082. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2083. else
  2084. params[0] = '\0';
  2085. if (config_methods & WPS_CONFIG_DISPLAY)
  2086. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2087. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2088. generated_pin = wps_generate_pin();
  2089. wpas_prov_disc_local_display(wpa_s, peer, params,
  2090. generated_pin);
  2091. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2092. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2093. MACSTR "%s", MAC2STR(peer), params);
  2094. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2095. P2P_PROV_DISC_SUCCESS,
  2096. config_methods, generated_pin);
  2097. }
  2098. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2099. enum p2p_prov_disc_status status)
  2100. {
  2101. struct wpa_supplicant *wpa_s = ctx;
  2102. if (wpa_s->p2p_fallback_to_go_neg) {
  2103. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2104. "failed - fall back to GO Negotiation");
  2105. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2106. return;
  2107. }
  2108. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2109. wpa_s->pending_pd_before_join = 0;
  2110. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2111. "join-existing-group operation (no ACK for PD "
  2112. "Req attempts)");
  2113. wpas_p2p_join_start(wpa_s);
  2114. return;
  2115. }
  2116. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2117. " p2p_dev_addr=" MACSTR " status=%d",
  2118. MAC2STR(peer), status);
  2119. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2120. status, 0, 0);
  2121. }
  2122. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2123. {
  2124. if (channels == NULL)
  2125. return 1; /* Assume no restrictions */
  2126. return p2p_channels_includes_freq(channels, freq);
  2127. }
  2128. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2129. const u8 *go_dev_addr, const u8 *ssid,
  2130. size_t ssid_len, int *go, u8 *group_bssid,
  2131. int *force_freq, int persistent_group,
  2132. const struct p2p_channels *channels)
  2133. {
  2134. struct wpa_supplicant *wpa_s = ctx;
  2135. struct wpa_ssid *s;
  2136. int res;
  2137. struct wpa_supplicant *grp;
  2138. if (!persistent_group) {
  2139. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2140. " to join an active group", MAC2STR(sa));
  2141. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2142. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2143. == 0 ||
  2144. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2145. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2146. "authorized invitation");
  2147. goto accept_inv;
  2148. }
  2149. /*
  2150. * Do not accept the invitation automatically; notify user and
  2151. * request approval.
  2152. */
  2153. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2154. }
  2155. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2156. if (grp) {
  2157. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2158. "running persistent group");
  2159. if (*go)
  2160. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2161. goto accept_inv;
  2162. }
  2163. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2164. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2165. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2166. "invitation to re-invoke a persistent group");
  2167. } else if (!wpa_s->conf->persistent_reconnect)
  2168. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2169. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2170. if (s->disabled == 2 &&
  2171. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2172. s->ssid_len == ssid_len &&
  2173. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2174. break;
  2175. }
  2176. if (!s) {
  2177. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2178. " requested reinvocation of an unknown group",
  2179. MAC2STR(sa));
  2180. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2181. }
  2182. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2183. *go = 1;
  2184. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2185. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2186. "interface is already in use - reject "
  2187. "invitation");
  2188. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2189. }
  2190. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2191. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2192. *go = 1;
  2193. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2194. {
  2195. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2196. "interface address for the group");
  2197. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2198. }
  2199. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2200. ETH_ALEN);
  2201. }
  2202. accept_inv:
  2203. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2204. /* Get one of the frequencies currently in use */
  2205. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2206. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
  2207. *force_freq = res;
  2208. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2209. }
  2210. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2211. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2212. if (*go == 0) {
  2213. /* We are the client */
  2214. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2215. "running a GO but we are capable of MCC, "
  2216. "figure out the best channel to use");
  2217. *force_freq = 0;
  2218. } else if (!freq_included(channels, *force_freq)) {
  2219. /* We are the GO, and *force_freq is not in the
  2220. * intersection */
  2221. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2222. "in intersection but we are capable of MCC, "
  2223. "figure out the best channel to use",
  2224. *force_freq);
  2225. *force_freq = 0;
  2226. }
  2227. }
  2228. return P2P_SC_SUCCESS;
  2229. }
  2230. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2231. const u8 *ssid, size_t ssid_len,
  2232. const u8 *go_dev_addr, u8 status,
  2233. int op_freq)
  2234. {
  2235. struct wpa_supplicant *wpa_s = ctx;
  2236. struct wpa_ssid *s;
  2237. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2238. if (s->disabled == 2 &&
  2239. s->ssid_len == ssid_len &&
  2240. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2241. break;
  2242. }
  2243. if (status == P2P_SC_SUCCESS) {
  2244. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2245. " was accepted; op_freq=%d MHz",
  2246. MAC2STR(sa), op_freq);
  2247. if (s) {
  2248. int go = s->mode == WPAS_MODE_P2P_GO;
  2249. wpas_p2p_group_add_persistent(
  2250. wpa_s, s, go, go ? op_freq : 0, 0, NULL);
  2251. } else if (bssid) {
  2252. wpa_s->user_initiated_pd = 0;
  2253. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2254. wpa_s->p2p_wps_method, 0);
  2255. }
  2256. return;
  2257. }
  2258. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2259. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2260. " was rejected (status %u)", MAC2STR(sa), status);
  2261. return;
  2262. }
  2263. if (!s) {
  2264. if (bssid) {
  2265. wpa_msg_global(wpa_s, MSG_INFO,
  2266. P2P_EVENT_INVITATION_RECEIVED
  2267. "sa=" MACSTR " go_dev_addr=" MACSTR
  2268. " bssid=" MACSTR " unknown-network",
  2269. MAC2STR(sa), MAC2STR(go_dev_addr),
  2270. MAC2STR(bssid));
  2271. } else {
  2272. wpa_msg_global(wpa_s, MSG_INFO,
  2273. P2P_EVENT_INVITATION_RECEIVED
  2274. "sa=" MACSTR " go_dev_addr=" MACSTR
  2275. " unknown-network",
  2276. MAC2STR(sa), MAC2STR(go_dev_addr));
  2277. }
  2278. return;
  2279. }
  2280. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2281. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2282. "sa=" MACSTR " persistent=%d freq=%d",
  2283. MAC2STR(sa), s->id, op_freq);
  2284. } else {
  2285. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2286. "sa=" MACSTR " persistent=%d",
  2287. MAC2STR(sa), s->id);
  2288. }
  2289. }
  2290. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2291. struct wpa_ssid *ssid,
  2292. const u8 *peer)
  2293. {
  2294. size_t i;
  2295. if (ssid == NULL)
  2296. return;
  2297. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2298. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2299. ETH_ALEN) == 0)
  2300. break;
  2301. }
  2302. if (i >= ssid->num_p2p_clients) {
  2303. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2304. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2305. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2306. "due to invitation result", ssid->id);
  2307. wpas_notify_network_removed(wpa_s, ssid);
  2308. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2309. return;
  2310. }
  2311. return; /* Peer not found in client list */
  2312. }
  2313. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2314. "group %d client list due to invitation result",
  2315. MAC2STR(peer), ssid->id);
  2316. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2317. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2318. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2319. ssid->num_p2p_clients--;
  2320. #ifndef CONFIG_NO_CONFIG_WRITE
  2321. if (wpa_s->parent->conf->update_config &&
  2322. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2323. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2324. #endif /* CONFIG_NO_CONFIG_WRITE */
  2325. }
  2326. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2327. const u8 *peer)
  2328. {
  2329. struct wpa_ssid *ssid;
  2330. wpa_s = wpa_s->global->p2p_invite_group;
  2331. if (wpa_s == NULL)
  2332. return; /* No known invitation group */
  2333. ssid = wpa_s->current_ssid;
  2334. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2335. !ssid->p2p_persistent_group)
  2336. return; /* Not operating as a GO in persistent group */
  2337. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2338. ssid->ssid, ssid->ssid_len);
  2339. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2340. }
  2341. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2342. const struct p2p_channels *channels,
  2343. const u8 *peer)
  2344. {
  2345. struct wpa_supplicant *wpa_s = ctx;
  2346. struct wpa_ssid *ssid;
  2347. if (bssid) {
  2348. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2349. "status=%d " MACSTR,
  2350. status, MAC2STR(bssid));
  2351. } else {
  2352. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2353. "status=%d ", status);
  2354. }
  2355. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2356. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2357. status, MAC2STR(peer));
  2358. if (wpa_s->pending_invite_ssid_id == -1) {
  2359. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2360. wpas_remove_persistent_client(wpa_s, peer);
  2361. return; /* Invitation to active group */
  2362. }
  2363. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2364. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2365. "invitation exchange to indicate readiness for "
  2366. "re-invocation");
  2367. }
  2368. if (status != P2P_SC_SUCCESS) {
  2369. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2370. ssid = wpa_config_get_network(
  2371. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2372. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2373. }
  2374. wpas_p2p_remove_pending_group_interface(wpa_s);
  2375. return;
  2376. }
  2377. ssid = wpa_config_get_network(wpa_s->conf,
  2378. wpa_s->pending_invite_ssid_id);
  2379. if (ssid == NULL) {
  2380. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2381. "data matching with invitation");
  2382. return;
  2383. }
  2384. /*
  2385. * The peer could have missed our ctrl::ack frame for Invitation
  2386. * Response and continue retransmitting the frame. To reduce the
  2387. * likelihood of the peer not getting successful TX status for the
  2388. * Invitation Response frame, wait a short time here before starting
  2389. * the persistent group so that we will remain on the current channel to
  2390. * acknowledge any possible retransmission from the peer.
  2391. */
  2392. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2393. "starting persistent group");
  2394. os_sleep(0, 50000);
  2395. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2396. ssid->mode == WPAS_MODE_P2P_GO,
  2397. wpa_s->p2p_persistent_go_freq,
  2398. wpa_s->p2p_go_ht40, channels);
  2399. }
  2400. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2401. unsigned int freq)
  2402. {
  2403. unsigned int i;
  2404. if (global->p2p_disallow_freq == NULL)
  2405. return 0;
  2406. for (i = 0; i < global->num_p2p_disallow_freq; i++) {
  2407. if (freq >= global->p2p_disallow_freq[i].min &&
  2408. freq <= global->p2p_disallow_freq[i].max)
  2409. return 1;
  2410. }
  2411. return 0;
  2412. }
  2413. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2414. {
  2415. reg->channel[reg->channels] = chan;
  2416. reg->channels++;
  2417. }
  2418. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2419. struct p2p_channels *chan)
  2420. {
  2421. int i, cla = 0;
  2422. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2423. "band");
  2424. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2425. chan->reg_class[cla].reg_class = 81;
  2426. chan->reg_class[cla].channels = 0;
  2427. for (i = 0; i < 11; i++) {
  2428. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2429. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2430. }
  2431. if (chan->reg_class[cla].channels)
  2432. cla++;
  2433. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2434. "band");
  2435. /* Operating class 115 - 5 GHz, channels 36-48 */
  2436. chan->reg_class[cla].reg_class = 115;
  2437. chan->reg_class[cla].channels = 0;
  2438. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2439. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2440. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2441. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2442. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2443. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2444. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2445. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2446. if (chan->reg_class[cla].channels)
  2447. cla++;
  2448. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2449. "band");
  2450. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2451. chan->reg_class[cla].reg_class = 124;
  2452. chan->reg_class[cla].channels = 0;
  2453. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2454. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2455. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2456. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2457. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2458. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2459. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2460. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2461. if (chan->reg_class[cla].channels)
  2462. cla++;
  2463. chan->reg_classes = cla;
  2464. return 0;
  2465. }
  2466. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2467. u16 num_modes,
  2468. enum hostapd_hw_mode mode)
  2469. {
  2470. u16 i;
  2471. for (i = 0; i < num_modes; i++) {
  2472. if (modes[i].mode == mode)
  2473. return &modes[i];
  2474. }
  2475. return NULL;
  2476. }
  2477. static int has_channel(struct wpa_global *global,
  2478. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2479. {
  2480. int i;
  2481. unsigned int freq;
  2482. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2483. chan * 5;
  2484. if (wpas_p2p_disallowed_freq(global, freq))
  2485. return 0;
  2486. for (i = 0; i < mode->num_channels; i++) {
  2487. if (mode->channels[i].chan == chan) {
  2488. if (flags)
  2489. *flags = mode->channels[i].flag;
  2490. return !(mode->channels[i].flag &
  2491. (HOSTAPD_CHAN_DISABLED |
  2492. HOSTAPD_CHAN_PASSIVE_SCAN |
  2493. HOSTAPD_CHAN_NO_IBSS |
  2494. HOSTAPD_CHAN_RADAR));
  2495. }
  2496. }
  2497. return 0;
  2498. }
  2499. struct p2p_oper_class_map {
  2500. enum hostapd_hw_mode mode;
  2501. u8 op_class;
  2502. u8 min_chan;
  2503. u8 max_chan;
  2504. u8 inc;
  2505. enum { BW20, BW40PLUS, BW40MINUS } bw;
  2506. };
  2507. static struct p2p_oper_class_map op_class[] = {
  2508. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2509. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2510. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2511. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2512. #endif
  2513. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2514. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2515. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2516. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2517. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2518. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2519. { -1, 0, 0, 0, 0, BW20 }
  2520. };
  2521. static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2522. struct hostapd_hw_modes *mode,
  2523. u8 channel, u8 bw)
  2524. {
  2525. int flag;
  2526. if (!has_channel(wpa_s->global, mode, channel, &flag))
  2527. return -1;
  2528. if (bw == BW40MINUS &&
  2529. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  2530. !has_channel(wpa_s->global, mode, channel - 4, NULL)))
  2531. return 0;
  2532. if (bw == BW40PLUS &&
  2533. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  2534. !has_channel(wpa_s->global, mode, channel + 4, NULL)))
  2535. return 0;
  2536. return 1;
  2537. }
  2538. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2539. struct p2p_channels *chan)
  2540. {
  2541. struct hostapd_hw_modes *mode;
  2542. int cla, op;
  2543. if (wpa_s->hw.modes == NULL) {
  2544. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2545. "of all supported channels; assume dualband "
  2546. "support");
  2547. return wpas_p2p_default_channels(wpa_s, chan);
  2548. }
  2549. cla = 0;
  2550. for (op = 0; op_class[op].op_class; op++) {
  2551. struct p2p_oper_class_map *o = &op_class[op];
  2552. u8 ch;
  2553. struct p2p_reg_class *reg = NULL;
  2554. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2555. if (mode == NULL)
  2556. continue;
  2557. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2558. if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
  2559. continue;
  2560. if (reg == NULL) {
  2561. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  2562. "class %u", o->op_class);
  2563. reg = &chan->reg_class[cla];
  2564. cla++;
  2565. reg->reg_class = o->op_class;
  2566. }
  2567. reg->channel[reg->channels] = ch;
  2568. reg->channels++;
  2569. }
  2570. if (reg) {
  2571. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2572. reg->channel, reg->channels);
  2573. }
  2574. }
  2575. chan->reg_classes = cla;
  2576. return 0;
  2577. }
  2578. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2579. struct hostapd_hw_modes *mode, u8 channel)
  2580. {
  2581. int op, ret;
  2582. for (op = 0; op_class[op].op_class; op++) {
  2583. struct p2p_oper_class_map *o = &op_class[op];
  2584. u8 ch;
  2585. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2586. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2587. o->bw == BW20 || ch != channel)
  2588. continue;
  2589. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2590. if (ret < 0)
  2591. continue;
  2592. else if (ret > 0)
  2593. return (o->bw == BW40MINUS) ? -1 : 1;
  2594. else
  2595. return 0;
  2596. }
  2597. }
  2598. return 0;
  2599. }
  2600. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2601. size_t buf_len)
  2602. {
  2603. struct wpa_supplicant *wpa_s = ctx;
  2604. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2605. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2606. break;
  2607. }
  2608. if (wpa_s == NULL)
  2609. return -1;
  2610. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2611. }
  2612. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2613. {
  2614. struct wpa_supplicant *wpa_s = ctx;
  2615. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2616. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2617. if (ssid == NULL)
  2618. continue;
  2619. if (ssid->mode != WPAS_MODE_INFRA)
  2620. continue;
  2621. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2622. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2623. continue;
  2624. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2625. return 1;
  2626. }
  2627. return 0;
  2628. }
  2629. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2630. {
  2631. struct wpa_supplicant *wpa_s = ctx;
  2632. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2633. }
  2634. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2635. {
  2636. struct wpa_interface iface;
  2637. struct wpa_supplicant *p2pdev_wpa_s;
  2638. char ifname[100];
  2639. char force_name[100];
  2640. int ret;
  2641. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2642. wpa_s->ifname);
  2643. force_name[0] = '\0';
  2644. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2645. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2646. force_name, wpa_s->pending_interface_addr, NULL);
  2647. if (ret < 0) {
  2648. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2649. return ret;
  2650. }
  2651. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2652. sizeof(wpa_s->pending_interface_name));
  2653. os_memset(&iface, 0, sizeof(iface));
  2654. iface.p2p_mgmt = 1;
  2655. iface.ifname = wpa_s->pending_interface_name;
  2656. iface.driver = wpa_s->driver->name;
  2657. iface.driver_param = wpa_s->conf->driver_param;
  2658. iface.confname = wpa_s->confname;
  2659. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2660. if (!p2pdev_wpa_s) {
  2661. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2662. return -1;
  2663. }
  2664. p2pdev_wpa_s->parent = wpa_s;
  2665. wpa_s->pending_interface_name[0] = '\0';
  2666. return 0;
  2667. }
  2668. /**
  2669. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2670. * @global: Pointer to global data from wpa_supplicant_init()
  2671. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2672. * Returns: 0 on success, -1 on failure
  2673. */
  2674. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2675. {
  2676. struct p2p_config p2p;
  2677. unsigned int r;
  2678. int i;
  2679. if (wpa_s->conf->p2p_disabled)
  2680. return 0;
  2681. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2682. return 0;
  2683. if (global->p2p)
  2684. return 0;
  2685. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2686. struct p2p_params params;
  2687. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  2688. os_memset(&params, 0, sizeof(params));
  2689. params.dev_name = wpa_s->conf->device_name;
  2690. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  2691. WPS_DEV_TYPE_LEN);
  2692. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2693. os_memcpy(params.sec_dev_type,
  2694. wpa_s->conf->sec_device_type,
  2695. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2696. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  2697. return -1;
  2698. return 0;
  2699. }
  2700. os_memset(&p2p, 0, sizeof(p2p));
  2701. p2p.cb_ctx = wpa_s;
  2702. p2p.debug_print = wpas_p2p_debug_print;
  2703. p2p.p2p_scan = wpas_p2p_scan;
  2704. p2p.send_action = wpas_send_action;
  2705. p2p.send_action_done = wpas_send_action_done;
  2706. p2p.go_neg_completed = wpas_go_neg_completed;
  2707. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2708. p2p.dev_found = wpas_dev_found;
  2709. p2p.dev_lost = wpas_dev_lost;
  2710. p2p.find_stopped = wpas_find_stopped;
  2711. p2p.start_listen = wpas_start_listen;
  2712. p2p.stop_listen = wpas_stop_listen;
  2713. p2p.send_probe_resp = wpas_send_probe_resp;
  2714. p2p.sd_request = wpas_sd_request;
  2715. p2p.sd_response = wpas_sd_response;
  2716. p2p.prov_disc_req = wpas_prov_disc_req;
  2717. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2718. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2719. p2p.invitation_process = wpas_invitation_process;
  2720. p2p.invitation_received = wpas_invitation_received;
  2721. p2p.invitation_result = wpas_invitation_result;
  2722. p2p.get_noa = wpas_get_noa;
  2723. p2p.go_connected = wpas_go_connected;
  2724. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2725. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  2726. p2p.dev_name = wpa_s->conf->device_name;
  2727. p2p.manufacturer = wpa_s->conf->manufacturer;
  2728. p2p.model_name = wpa_s->conf->model_name;
  2729. p2p.model_number = wpa_s->conf->model_number;
  2730. p2p.serial_number = wpa_s->conf->serial_number;
  2731. if (wpa_s->wps) {
  2732. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  2733. p2p.config_methods = wpa_s->wps->config_methods;
  2734. }
  2735. if (wpa_s->conf->p2p_listen_reg_class &&
  2736. wpa_s->conf->p2p_listen_channel) {
  2737. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2738. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2739. } else {
  2740. p2p.reg_class = 81;
  2741. /*
  2742. * Pick one of the social channels randomly as the listen
  2743. * channel.
  2744. */
  2745. os_get_random((u8 *) &r, sizeof(r));
  2746. p2p.channel = 1 + (r % 3) * 5;
  2747. }
  2748. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2749. if (wpa_s->conf->p2p_oper_reg_class &&
  2750. wpa_s->conf->p2p_oper_channel) {
  2751. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2752. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2753. p2p.cfg_op_channel = 1;
  2754. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2755. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2756. } else {
  2757. p2p.op_reg_class = 81;
  2758. /*
  2759. * Use random operation channel from (1, 6, 11) if no other
  2760. * preference is indicated.
  2761. */
  2762. os_get_random((u8 *) &r, sizeof(r));
  2763. p2p.op_channel = 1 + (r % 3) * 5;
  2764. p2p.cfg_op_channel = 0;
  2765. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2766. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2767. }
  2768. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  2769. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  2770. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  2771. }
  2772. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2773. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2774. p2p.country[2] = 0x04;
  2775. } else
  2776. os_memcpy(p2p.country, "XX\x04", 3);
  2777. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2778. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2779. "channel list");
  2780. return -1;
  2781. }
  2782. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  2783. WPS_DEV_TYPE_LEN);
  2784. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2785. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  2786. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2787. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2788. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2789. p2p.max_peers = 100;
  2790. if (wpa_s->conf->p2p_ssid_postfix) {
  2791. p2p.ssid_postfix_len =
  2792. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2793. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2794. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2795. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2796. p2p.ssid_postfix_len);
  2797. }
  2798. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2799. p2p.max_listen = wpa_s->max_remain_on_chan;
  2800. global->p2p = p2p_init(&p2p);
  2801. if (global->p2p == NULL)
  2802. return -1;
  2803. global->p2p_init_wpa_s = wpa_s;
  2804. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  2805. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  2806. continue;
  2807. p2p_add_wps_vendor_extension(
  2808. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  2809. }
  2810. return 0;
  2811. }
  2812. /**
  2813. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2814. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2815. *
  2816. * This function deinitialize per-interface P2P data.
  2817. */
  2818. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2819. {
  2820. if (wpa_s->driver && wpa_s->drv_priv)
  2821. wpa_drv_probe_req_report(wpa_s, 0);
  2822. if (wpa_s->go_params) {
  2823. /* Clear any stored provisioning info */
  2824. p2p_clear_provisioning_info(
  2825. wpa_s->global->p2p,
  2826. wpa_s->go_params->peer_device_addr);
  2827. }
  2828. os_free(wpa_s->go_params);
  2829. wpa_s->go_params = NULL;
  2830. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2831. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2832. wpa_s->p2p_long_listen = 0;
  2833. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2834. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2835. wpas_p2p_remove_pending_group_interface(wpa_s);
  2836. /* TODO: remove group interface from the driver if this wpa_s instance
  2837. * is on top of a P2P group interface */
  2838. }
  2839. /**
  2840. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2841. * @global: Pointer to global data from wpa_supplicant_init()
  2842. *
  2843. * This function deinitializes the global (per device) P2P module.
  2844. */
  2845. void wpas_p2p_deinit_global(struct wpa_global *global)
  2846. {
  2847. struct wpa_supplicant *wpa_s, *tmp;
  2848. wpa_s = global->ifaces;
  2849. if (wpa_s)
  2850. wpas_p2p_service_flush(wpa_s);
  2851. if (global->p2p == NULL)
  2852. return;
  2853. /* Remove remaining P2P group interfaces */
  2854. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2855. wpa_s = wpa_s->next;
  2856. while (wpa_s) {
  2857. tmp = global->ifaces;
  2858. while (tmp &&
  2859. (tmp == wpa_s ||
  2860. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2861. tmp = tmp->next;
  2862. }
  2863. if (tmp == NULL)
  2864. break;
  2865. /* Disconnect from the P2P group and deinit the interface */
  2866. wpas_p2p_disconnect(tmp);
  2867. }
  2868. /*
  2869. * Deinit GO data on any possibly remaining interface (if main
  2870. * interface is used as GO).
  2871. */
  2872. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2873. if (wpa_s->ap_iface)
  2874. wpas_p2p_group_deinit(wpa_s);
  2875. }
  2876. p2p_deinit(global->p2p);
  2877. global->p2p = NULL;
  2878. global->p2p_init_wpa_s = NULL;
  2879. }
  2880. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2881. {
  2882. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2883. wpa_s->conf->p2p_no_group_iface)
  2884. return 0; /* separate interface disabled per configuration */
  2885. if (wpa_s->drv_flags &
  2886. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2887. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2888. return 1; /* P2P group requires a new interface in every case
  2889. */
  2890. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2891. return 0; /* driver does not support concurrent operations */
  2892. if (wpa_s->global->ifaces->next)
  2893. return 1; /* more that one interface already in use */
  2894. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2895. return 1; /* this interface is already in use */
  2896. return 0;
  2897. }
  2898. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2899. const u8 *peer_addr,
  2900. enum p2p_wps_method wps_method,
  2901. int go_intent, const u8 *own_interface_addr,
  2902. unsigned int force_freq, int persistent_group,
  2903. struct wpa_ssid *ssid, unsigned int pref_freq)
  2904. {
  2905. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2906. persistent_group = 2;
  2907. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2908. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2909. go_intent, own_interface_addr,
  2910. force_freq, persistent_group);
  2911. }
  2912. /*
  2913. * Increase GO config timeout if HT40 is used since it takes some time
  2914. * to scan channels for coex purposes before the BSS can be started.
  2915. */
  2916. p2p_set_config_timeout(wpa_s->global->p2p,
  2917. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  2918. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2919. go_intent, own_interface_addr, force_freq,
  2920. persistent_group, ssid ? ssid->ssid : NULL,
  2921. ssid ? ssid->ssid_len : 0,
  2922. wpa_s->p2p_pd_before_go_neg, pref_freq);
  2923. }
  2924. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2925. const u8 *peer_addr,
  2926. enum p2p_wps_method wps_method,
  2927. int go_intent, const u8 *own_interface_addr,
  2928. unsigned int force_freq, int persistent_group,
  2929. struct wpa_ssid *ssid, unsigned int pref_freq)
  2930. {
  2931. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2932. persistent_group = 2;
  2933. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2934. return -1;
  2935. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2936. go_intent, own_interface_addr, force_freq,
  2937. persistent_group, ssid ? ssid->ssid : NULL,
  2938. ssid ? ssid->ssid_len : 0, pref_freq);
  2939. }
  2940. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2941. {
  2942. wpa_s->p2p_join_scan_count++;
  2943. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2944. wpa_s->p2p_join_scan_count);
  2945. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2946. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2947. " for join operationg - stop join attempt",
  2948. MAC2STR(wpa_s->pending_join_iface_addr));
  2949. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2950. if (wpa_s->p2p_auto_pd) {
  2951. wpa_s->p2p_auto_pd = 0;
  2952. wpa_msg_global(wpa_s, MSG_INFO,
  2953. P2P_EVENT_PROV_DISC_FAILURE
  2954. " p2p_dev_addr=" MACSTR " status=N/A",
  2955. MAC2STR(wpa_s->pending_join_dev_addr));
  2956. return;
  2957. }
  2958. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2959. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2960. }
  2961. }
  2962. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  2963. {
  2964. int *freqs, res, num, i;
  2965. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2966. /* Multiple channels are supported and not all are in use */
  2967. return 0;
  2968. }
  2969. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  2970. if (!freqs)
  2971. return 1;
  2972. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  2973. wpa_s->num_multichan_concurrent);
  2974. if (num < 0) {
  2975. res = 1;
  2976. goto exit_free;
  2977. }
  2978. for (i = 0; i < num; i++) {
  2979. if (freqs[i] == freq) {
  2980. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  2981. freq);
  2982. res = 0;
  2983. goto exit_free;
  2984. }
  2985. }
  2986. res = 1;
  2987. exit_free:
  2988. os_free(freqs);
  2989. return res;
  2990. }
  2991. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  2992. const u8 *peer_dev_addr)
  2993. {
  2994. struct wpa_bss *bss;
  2995. int updated;
  2996. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  2997. if (bss == NULL)
  2998. return -1;
  2999. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3000. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3001. "last scan");
  3002. return 0;
  3003. }
  3004. updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
  3005. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3006. "%ld.%06ld (%supdated in last scan)",
  3007. bss->last_update.sec, bss->last_update.usec,
  3008. updated ? "": "not ");
  3009. return updated;
  3010. }
  3011. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3012. struct wpa_scan_results *scan_res)
  3013. {
  3014. struct wpa_bss *bss;
  3015. int freq;
  3016. u8 iface_addr[ETH_ALEN];
  3017. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3018. if (wpa_s->global->p2p_disabled)
  3019. return;
  3020. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3021. scan_res ? (int) scan_res->num : -1,
  3022. wpa_s->p2p_auto_join ? "auto_" : "");
  3023. if (scan_res)
  3024. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3025. if (wpa_s->p2p_auto_pd) {
  3026. int join = wpas_p2p_peer_go(wpa_s,
  3027. wpa_s->pending_join_dev_addr);
  3028. if (join == 0 &&
  3029. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3030. wpa_s->auto_pd_scan_retry++;
  3031. bss = wpa_bss_get_bssid_latest(
  3032. wpa_s, wpa_s->pending_join_dev_addr);
  3033. if (bss) {
  3034. freq = bss->freq;
  3035. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3036. "the peer " MACSTR " at %d MHz",
  3037. wpa_s->auto_pd_scan_retry,
  3038. MAC2STR(wpa_s->
  3039. pending_join_dev_addr),
  3040. freq);
  3041. wpas_p2p_join_scan_req(wpa_s, freq);
  3042. return;
  3043. }
  3044. }
  3045. if (join < 0)
  3046. join = 0;
  3047. wpa_s->p2p_auto_pd = 0;
  3048. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3049. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3050. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3051. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3052. wpa_s->pending_join_dev_addr,
  3053. wpa_s->pending_pd_config_methods, join,
  3054. 0, wpa_s->user_initiated_pd) < 0) {
  3055. wpa_s->p2p_auto_pd = 0;
  3056. wpa_msg_global(wpa_s, MSG_INFO,
  3057. P2P_EVENT_PROV_DISC_FAILURE
  3058. " p2p_dev_addr=" MACSTR " status=N/A",
  3059. MAC2STR(wpa_s->pending_join_dev_addr));
  3060. }
  3061. return;
  3062. }
  3063. if (wpa_s->p2p_auto_join) {
  3064. int join = wpas_p2p_peer_go(wpa_s,
  3065. wpa_s->pending_join_dev_addr);
  3066. if (join < 0) {
  3067. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3068. "running a GO -> use GO Negotiation");
  3069. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3070. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3071. wpa_s->p2p_persistent_group, 0, 0, 0,
  3072. wpa_s->p2p_go_intent,
  3073. wpa_s->p2p_connect_freq,
  3074. wpa_s->p2p_persistent_id,
  3075. wpa_s->p2p_pd_before_go_neg,
  3076. wpa_s->p2p_go_ht40);
  3077. return;
  3078. }
  3079. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3080. "try to join the group", join ? "" :
  3081. " in older scan");
  3082. if (!join)
  3083. wpa_s->p2p_fallback_to_go_neg = 1;
  3084. }
  3085. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3086. wpa_s->pending_join_iface_addr);
  3087. if (freq < 0 &&
  3088. p2p_get_interface_addr(wpa_s->global->p2p,
  3089. wpa_s->pending_join_dev_addr,
  3090. iface_addr) == 0 &&
  3091. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3092. {
  3093. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3094. "address for join from " MACSTR " to " MACSTR
  3095. " based on newly discovered P2P peer entry",
  3096. MAC2STR(wpa_s->pending_join_iface_addr),
  3097. MAC2STR(iface_addr));
  3098. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3099. ETH_ALEN);
  3100. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3101. wpa_s->pending_join_iface_addr);
  3102. }
  3103. if (freq >= 0) {
  3104. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3105. "from P2P peer table: %d MHz", freq);
  3106. }
  3107. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3108. if (bss) {
  3109. freq = bss->freq;
  3110. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3111. "from BSS table: %d MHz", freq);
  3112. }
  3113. if (freq > 0) {
  3114. u16 method;
  3115. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3116. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3117. P2P_EVENT_GROUP_FORMATION_FAILURE
  3118. "reason=FREQ_CONFLICT");
  3119. return;
  3120. }
  3121. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3122. "prior to joining an existing group (GO " MACSTR
  3123. " freq=%u MHz)",
  3124. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3125. wpa_s->pending_pd_before_join = 1;
  3126. switch (wpa_s->pending_join_wps_method) {
  3127. case WPS_PIN_DISPLAY:
  3128. method = WPS_CONFIG_KEYPAD;
  3129. break;
  3130. case WPS_PIN_KEYPAD:
  3131. method = WPS_CONFIG_DISPLAY;
  3132. break;
  3133. case WPS_PBC:
  3134. method = WPS_CONFIG_PUSHBUTTON;
  3135. break;
  3136. default:
  3137. method = 0;
  3138. break;
  3139. }
  3140. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3141. wpa_s->pending_join_dev_addr) ==
  3142. method)) {
  3143. /*
  3144. * We have already performed provision discovery for
  3145. * joining the group. Proceed directly to join
  3146. * operation without duplicated provision discovery. */
  3147. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3148. "with " MACSTR " already done - proceed to "
  3149. "join",
  3150. MAC2STR(wpa_s->pending_join_dev_addr));
  3151. wpa_s->pending_pd_before_join = 0;
  3152. goto start;
  3153. }
  3154. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3155. wpa_s->pending_join_dev_addr, method, 1,
  3156. freq, wpa_s->user_initiated_pd) < 0) {
  3157. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3158. "Discovery Request before joining an "
  3159. "existing group");
  3160. wpa_s->pending_pd_before_join = 0;
  3161. goto start;
  3162. }
  3163. return;
  3164. }
  3165. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3166. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3167. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3168. wpas_p2p_check_join_scan_limit(wpa_s);
  3169. return;
  3170. start:
  3171. /* Start join operation immediately */
  3172. wpas_p2p_join_start(wpa_s);
  3173. }
  3174. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3175. {
  3176. int ret;
  3177. struct wpa_driver_scan_params params;
  3178. struct wpabuf *wps_ie, *ies;
  3179. size_t ielen;
  3180. int freqs[2] = { 0, 0 };
  3181. os_memset(&params, 0, sizeof(params));
  3182. /* P2P Wildcard SSID */
  3183. params.num_ssids = 1;
  3184. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3185. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3186. wpa_s->wps->dev.p2p = 1;
  3187. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3188. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3189. NULL);
  3190. if (wps_ie == NULL) {
  3191. wpas_p2p_scan_res_join(wpa_s, NULL);
  3192. return;
  3193. }
  3194. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3195. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3196. if (ies == NULL) {
  3197. wpabuf_free(wps_ie);
  3198. wpas_p2p_scan_res_join(wpa_s, NULL);
  3199. return;
  3200. }
  3201. wpabuf_put_buf(ies, wps_ie);
  3202. wpabuf_free(wps_ie);
  3203. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3204. params.p2p_probe = 1;
  3205. params.extra_ies = wpabuf_head(ies);
  3206. params.extra_ies_len = wpabuf_len(ies);
  3207. if (freq > 0) {
  3208. freqs[0] = freq;
  3209. params.freqs = freqs;
  3210. }
  3211. /*
  3212. * Run a scan to update BSS table and start Provision Discovery once
  3213. * the new scan results become available.
  3214. */
  3215. ret = wpa_drv_scan(wpa_s, &params);
  3216. if (!ret) {
  3217. os_get_time(&wpa_s->scan_trigger_time);
  3218. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3219. }
  3220. wpabuf_free(ies);
  3221. if (ret) {
  3222. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3223. "try again later");
  3224. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3225. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3226. wpas_p2p_check_join_scan_limit(wpa_s);
  3227. }
  3228. }
  3229. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3230. {
  3231. struct wpa_supplicant *wpa_s = eloop_ctx;
  3232. wpas_p2p_join_scan_req(wpa_s, 0);
  3233. }
  3234. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3235. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3236. int auto_join)
  3237. {
  3238. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3239. MACSTR " dev " MACSTR ")%s",
  3240. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3241. auto_join ? " (auto_join)" : "");
  3242. wpa_s->p2p_auto_pd = 0;
  3243. wpa_s->p2p_auto_join = !!auto_join;
  3244. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3245. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3246. wpa_s->pending_join_wps_method = wps_method;
  3247. /* Make sure we are not running find during connection establishment */
  3248. wpas_p2p_stop_find(wpa_s);
  3249. wpa_s->p2p_join_scan_count = 0;
  3250. wpas_p2p_join_scan(wpa_s, NULL);
  3251. return 0;
  3252. }
  3253. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3254. {
  3255. struct wpa_supplicant *group;
  3256. struct p2p_go_neg_results res;
  3257. struct wpa_bss *bss;
  3258. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3259. if (group == NULL)
  3260. return -1;
  3261. if (group != wpa_s) {
  3262. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3263. sizeof(group->p2p_pin));
  3264. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3265. } else {
  3266. /*
  3267. * Need to mark the current interface for p2p_group_formation
  3268. * when a separate group interface is not used. This is needed
  3269. * to allow p2p_cancel stop a pending p2p_connect-join.
  3270. * wpas_p2p_init_group_interface() addresses this for the case
  3271. * where a separate group interface is used.
  3272. */
  3273. wpa_s->global->p2p_group_formation = wpa_s;
  3274. }
  3275. group->p2p_in_provisioning = 1;
  3276. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3277. os_memset(&res, 0, sizeof(res));
  3278. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3279. ETH_ALEN);
  3280. res.wps_method = wpa_s->pending_join_wps_method;
  3281. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3282. if (bss) {
  3283. res.freq = bss->freq;
  3284. res.ssid_len = bss->ssid_len;
  3285. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3286. }
  3287. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3288. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3289. "starting client");
  3290. wpa_drv_cancel_remain_on_channel(wpa_s);
  3291. wpa_s->off_channel_freq = 0;
  3292. wpa_s->roc_waiting_drv_freq = 0;
  3293. }
  3294. wpas_start_wps_enrollee(group, &res);
  3295. /*
  3296. * Allow a longer timeout for join-a-running-group than normal 15
  3297. * second group formation timeout since the GO may not have authorized
  3298. * our connection yet.
  3299. */
  3300. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3301. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3302. wpa_s, NULL);
  3303. return 0;
  3304. }
  3305. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3306. int *force_freq, int *pref_freq)
  3307. {
  3308. int *freqs, res;
  3309. unsigned int freq_in_use = 0, num, i;
  3310. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3311. if (!freqs)
  3312. return -1;
  3313. num = get_shared_radio_freqs(wpa_s, freqs,
  3314. wpa_s->num_multichan_concurrent);
  3315. wpa_printf(MSG_DEBUG,
  3316. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
  3317. freq, wpa_s->num_multichan_concurrent, num);
  3318. if (freq > 0) {
  3319. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3320. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3321. "(%u MHz) is not supported for P2P uses",
  3322. freq);
  3323. res = -3;
  3324. goto exit_free;
  3325. }
  3326. for (i = 0; i < num; i++) {
  3327. if (freqs[i] == freq)
  3328. freq_in_use = 1;
  3329. }
  3330. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3331. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3332. freq);
  3333. res = -2;
  3334. goto exit_free;
  3335. }
  3336. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3337. "requested channel (%u MHz)", freq);
  3338. *force_freq = freq;
  3339. goto exit_ok;
  3340. }
  3341. for (i = 0; i < num; i++) {
  3342. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3343. continue;
  3344. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3345. *force_freq);
  3346. *force_freq = freqs[i];
  3347. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3348. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
  3349. *pref_freq = *force_freq;
  3350. }
  3351. break;
  3352. }
  3353. if (i == num) {
  3354. if (num < wpa_s->num_multichan_concurrent) {
  3355. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3356. *force_freq = 0;
  3357. } else {
  3358. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3359. res = -2;
  3360. goto exit_free;
  3361. }
  3362. }
  3363. exit_ok:
  3364. res = 0;
  3365. exit_free:
  3366. os_free(freqs);
  3367. return res;
  3368. }
  3369. /**
  3370. * wpas_p2p_connect - Request P2P Group Formation to be started
  3371. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3372. * @peer_addr: Address of the peer P2P Device
  3373. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3374. * @persistent_group: Whether to create a persistent group
  3375. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3376. * @join: Whether to join an existing group (as a client) instead of starting
  3377. * Group Owner negotiation; @peer_addr is BSSID in that case
  3378. * @auth: Whether to only authorize the connection instead of doing that and
  3379. * initiating Group Owner negotiation
  3380. * @go_intent: GO Intent or -1 to use default
  3381. * @freq: Frequency for the group or 0 for auto-selection
  3382. * @persistent_id: Persistent group credentials to use for forcing GO
  3383. * parameters or -1 to generate new values (SSID/passphrase)
  3384. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3385. * interoperability workaround when initiating group formation
  3386. * @ht40: Start GO with 40 MHz channel width
  3387. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3388. * failure, -2 on failure due to channel not currently available,
  3389. * -3 if forced channel is not supported
  3390. */
  3391. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3392. const char *pin, enum p2p_wps_method wps_method,
  3393. int persistent_group, int auto_join, int join, int auth,
  3394. int go_intent, int freq, int persistent_id, int pd,
  3395. int ht40)
  3396. {
  3397. int force_freq = 0, pref_freq = 0;
  3398. int ret = 0, res;
  3399. enum wpa_driver_if_type iftype;
  3400. const u8 *if_addr;
  3401. struct wpa_ssid *ssid = NULL;
  3402. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3403. return -1;
  3404. if (persistent_id >= 0) {
  3405. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3406. if (ssid == NULL || ssid->disabled != 2 ||
  3407. ssid->mode != WPAS_MODE_P2P_GO)
  3408. return -1;
  3409. }
  3410. os_free(wpa_s->global->add_psk);
  3411. wpa_s->global->add_psk = NULL;
  3412. if (go_intent < 0)
  3413. go_intent = wpa_s->conf->p2p_go_intent;
  3414. if (!auth)
  3415. wpa_s->p2p_long_listen = 0;
  3416. wpa_s->p2p_wps_method = wps_method;
  3417. wpa_s->p2p_persistent_group = !!persistent_group;
  3418. wpa_s->p2p_persistent_id = persistent_id;
  3419. wpa_s->p2p_go_intent = go_intent;
  3420. wpa_s->p2p_connect_freq = freq;
  3421. wpa_s->p2p_fallback_to_go_neg = 0;
  3422. wpa_s->p2p_pd_before_go_neg = !!pd;
  3423. wpa_s->p2p_go_ht40 = !!ht40;
  3424. if (pin)
  3425. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3426. else if (wps_method == WPS_PIN_DISPLAY) {
  3427. ret = wps_generate_pin();
  3428. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3429. ret);
  3430. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3431. wpa_s->p2p_pin);
  3432. } else
  3433. wpa_s->p2p_pin[0] = '\0';
  3434. if (join || auto_join) {
  3435. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3436. if (auth) {
  3437. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3438. "connect a running group from " MACSTR,
  3439. MAC2STR(peer_addr));
  3440. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3441. return ret;
  3442. }
  3443. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3444. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3445. iface_addr) < 0) {
  3446. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3447. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3448. dev_addr);
  3449. }
  3450. if (auto_join) {
  3451. os_get_time(&wpa_s->p2p_auto_started);
  3452. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3453. "%ld.%06ld",
  3454. wpa_s->p2p_auto_started.sec,
  3455. wpa_s->p2p_auto_started.usec);
  3456. }
  3457. wpa_s->user_initiated_pd = 1;
  3458. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3459. auto_join) < 0)
  3460. return -1;
  3461. return ret;
  3462. }
  3463. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  3464. if (res)
  3465. return res;
  3466. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  3467. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3468. if (wpa_s->create_p2p_iface) {
  3469. /* Prepare to add a new interface for the group */
  3470. iftype = WPA_IF_P2P_GROUP;
  3471. if (go_intent == 15)
  3472. iftype = WPA_IF_P2P_GO;
  3473. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3474. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3475. "interface for the group");
  3476. return -1;
  3477. }
  3478. if_addr = wpa_s->pending_interface_addr;
  3479. } else
  3480. if_addr = wpa_s->own_addr;
  3481. if (auth) {
  3482. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3483. go_intent, if_addr,
  3484. force_freq, persistent_group, ssid,
  3485. pref_freq) < 0)
  3486. return -1;
  3487. return ret;
  3488. }
  3489. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3490. go_intent, if_addr, force_freq,
  3491. persistent_group, ssid, pref_freq) < 0) {
  3492. if (wpa_s->create_p2p_iface)
  3493. wpas_p2p_remove_pending_group_interface(wpa_s);
  3494. return -1;
  3495. }
  3496. return ret;
  3497. }
  3498. /**
  3499. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3500. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3501. * @freq: Frequency of the channel in MHz
  3502. * @duration: Duration of the stay on the channel in milliseconds
  3503. *
  3504. * This callback is called when the driver indicates that it has started the
  3505. * requested remain-on-channel duration.
  3506. */
  3507. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3508. unsigned int freq, unsigned int duration)
  3509. {
  3510. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3511. return;
  3512. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3513. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3514. wpa_s->pending_listen_duration);
  3515. wpa_s->pending_listen_freq = 0;
  3516. }
  3517. }
  3518. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3519. unsigned int timeout)
  3520. {
  3521. /* Limit maximum Listen state time based on driver limitation. */
  3522. if (timeout > wpa_s->max_remain_on_chan)
  3523. timeout = wpa_s->max_remain_on_chan;
  3524. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3525. return wpa_drv_p2p_listen(wpa_s, timeout);
  3526. return p2p_listen(wpa_s->global->p2p, timeout);
  3527. }
  3528. /**
  3529. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3530. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3531. * @freq: Frequency of the channel in MHz
  3532. *
  3533. * This callback is called when the driver indicates that a remain-on-channel
  3534. * operation has been completed, i.e., the duration on the requested channel
  3535. * has timed out.
  3536. */
  3537. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3538. unsigned int freq)
  3539. {
  3540. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3541. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3542. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3543. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3544. return;
  3545. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3546. return; /* P2P module started a new operation */
  3547. if (offchannel_pending_action_tx(wpa_s))
  3548. return;
  3549. if (wpa_s->p2p_long_listen > 0)
  3550. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3551. if (wpa_s->p2p_long_listen > 0) {
  3552. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3553. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3554. } else {
  3555. /*
  3556. * When listen duration is over, stop listen & update p2p_state
  3557. * to IDLE.
  3558. */
  3559. p2p_stop_listen(wpa_s->global->p2p);
  3560. }
  3561. }
  3562. /**
  3563. * wpas_p2p_group_remove - Remove a P2P group
  3564. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3565. * @ifname: Network interface name of the group interface or "*" to remove all
  3566. * groups
  3567. * Returns: 0 on success, -1 on failure
  3568. *
  3569. * This function is used to remove a P2P group. This can be used to disconnect
  3570. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3571. * case the local end is the Group Owner. If a virtual network interface was
  3572. * created for this group, that interface will be removed. Otherwise, only the
  3573. * configured P2P group network will be removed from the interface.
  3574. */
  3575. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3576. {
  3577. struct wpa_global *global = wpa_s->global;
  3578. if (os_strcmp(ifname, "*") == 0) {
  3579. struct wpa_supplicant *prev;
  3580. wpa_s = global->ifaces;
  3581. while (wpa_s) {
  3582. prev = wpa_s;
  3583. wpa_s = wpa_s->next;
  3584. if (prev->p2p_group_interface !=
  3585. NOT_P2P_GROUP_INTERFACE ||
  3586. (prev->current_ssid &&
  3587. prev->current_ssid->p2p_group))
  3588. wpas_p2p_disconnect(prev);
  3589. }
  3590. return 0;
  3591. }
  3592. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3593. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3594. break;
  3595. }
  3596. return wpas_p2p_disconnect(wpa_s);
  3597. }
  3598. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3599. {
  3600. unsigned int r;
  3601. if (freq == 2) {
  3602. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3603. "band");
  3604. if (wpa_s->best_24_freq > 0 &&
  3605. p2p_supported_freq(wpa_s->global->p2p,
  3606. wpa_s->best_24_freq)) {
  3607. freq = wpa_s->best_24_freq;
  3608. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3609. "channel: %d MHz", freq);
  3610. } else {
  3611. os_get_random((u8 *) &r, sizeof(r));
  3612. freq = 2412 + (r % 3) * 25;
  3613. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3614. "channel: %d MHz", freq);
  3615. }
  3616. }
  3617. if (freq == 5) {
  3618. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3619. "band");
  3620. if (wpa_s->best_5_freq > 0 &&
  3621. p2p_supported_freq(wpa_s->global->p2p,
  3622. wpa_s->best_5_freq)) {
  3623. freq = wpa_s->best_5_freq;
  3624. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3625. "channel: %d MHz", freq);
  3626. } else {
  3627. os_get_random((u8 *) &r, sizeof(r));
  3628. freq = 5180 + (r % 4) * 20;
  3629. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3630. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3631. "5 GHz channel for P2P group");
  3632. return -1;
  3633. }
  3634. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3635. "channel: %d MHz", freq);
  3636. }
  3637. }
  3638. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3639. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3640. "(%u MHz) is not supported for P2P uses",
  3641. freq);
  3642. return -1;
  3643. }
  3644. return freq;
  3645. }
  3646. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3647. struct p2p_go_neg_results *params,
  3648. int freq, int ht40,
  3649. const struct p2p_channels *channels)
  3650. {
  3651. int res, *freqs;
  3652. unsigned int pref_freq;
  3653. unsigned int num, i;
  3654. os_memset(params, 0, sizeof(*params));
  3655. params->role_go = 1;
  3656. params->ht40 = ht40;
  3657. if (freq) {
  3658. if (!freq_included(channels, freq)) {
  3659. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3660. "accepted", freq);
  3661. return -1;
  3662. }
  3663. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3664. "frequency %d MHz", freq);
  3665. params->freq = freq;
  3666. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3667. wpa_s->conf->p2p_oper_channel >= 1 &&
  3668. wpa_s->conf->p2p_oper_channel <= 11 &&
  3669. freq_included(channels,
  3670. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3671. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3672. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3673. "frequency %d MHz", params->freq);
  3674. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3675. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3676. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3677. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3678. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3679. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3680. freq_included(channels,
  3681. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3682. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3683. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3684. "frequency %d MHz", params->freq);
  3685. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3686. wpa_s->best_overall_freq > 0 &&
  3687. p2p_supported_freq(wpa_s->global->p2p,
  3688. wpa_s->best_overall_freq) &&
  3689. freq_included(channels, wpa_s->best_overall_freq)) {
  3690. params->freq = wpa_s->best_overall_freq;
  3691. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3692. "channel %d MHz", params->freq);
  3693. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3694. wpa_s->best_24_freq > 0 &&
  3695. p2p_supported_freq(wpa_s->global->p2p,
  3696. wpa_s->best_24_freq) &&
  3697. freq_included(channels, wpa_s->best_24_freq)) {
  3698. params->freq = wpa_s->best_24_freq;
  3699. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3700. "channel %d MHz", params->freq);
  3701. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3702. wpa_s->best_5_freq > 0 &&
  3703. p2p_supported_freq(wpa_s->global->p2p,
  3704. wpa_s->best_5_freq) &&
  3705. freq_included(channels, wpa_s->best_5_freq)) {
  3706. params->freq = wpa_s->best_5_freq;
  3707. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  3708. "channel %d MHz", params->freq);
  3709. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  3710. channels))) {
  3711. params->freq = pref_freq;
  3712. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  3713. "channels", params->freq);
  3714. } else {
  3715. int chan;
  3716. for (chan = 0; chan < 11; chan++) {
  3717. params->freq = 2412 + chan * 5;
  3718. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  3719. params->freq) &&
  3720. freq_included(channels, params->freq))
  3721. break;
  3722. }
  3723. if (chan == 11) {
  3724. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  3725. "allowed");
  3726. return -1;
  3727. }
  3728. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  3729. "known)", params->freq);
  3730. }
  3731. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3732. if (!freqs)
  3733. return -1;
  3734. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3735. wpa_s->num_multichan_concurrent);
  3736. if (res < 0) {
  3737. os_free(freqs);
  3738. return -1;
  3739. }
  3740. num = res;
  3741. if (!freq) {
  3742. for (i = 0; i < num; i++) {
  3743. if (freq_included(channels, freqs[i])) {
  3744. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  3745. freqs[i]);
  3746. params->freq = freqs[i];
  3747. break;
  3748. }
  3749. }
  3750. if (i == num) {
  3751. if (num == wpa_s->num_multichan_concurrent) {
  3752. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  3753. os_free(freqs);
  3754. return -1;
  3755. } else {
  3756. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3757. }
  3758. }
  3759. } else {
  3760. for (i = 0; i < num; i++) {
  3761. if (freqs[i] == freq)
  3762. break;
  3763. }
  3764. if (i == num) {
  3765. if (num == wpa_s->num_multichan_concurrent) {
  3766. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  3767. os_free(freqs);
  3768. return -1;
  3769. } else {
  3770. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3771. }
  3772. }
  3773. }
  3774. os_free(freqs);
  3775. return 0;
  3776. }
  3777. static struct wpa_supplicant *
  3778. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  3779. int go)
  3780. {
  3781. struct wpa_supplicant *group_wpa_s;
  3782. if (!wpas_p2p_create_iface(wpa_s)) {
  3783. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  3784. "operations");
  3785. return wpa_s;
  3786. }
  3787. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  3788. WPA_IF_P2P_CLIENT) < 0) {
  3789. wpa_msg_global(wpa_s, MSG_ERROR,
  3790. "P2P: Failed to add group interface");
  3791. return NULL;
  3792. }
  3793. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  3794. if (group_wpa_s == NULL) {
  3795. wpa_msg_global(wpa_s, MSG_ERROR,
  3796. "P2P: Failed to initialize group interface");
  3797. wpas_p2p_remove_pending_group_interface(wpa_s);
  3798. return NULL;
  3799. }
  3800. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  3801. group_wpa_s->ifname);
  3802. return group_wpa_s;
  3803. }
  3804. /**
  3805. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  3806. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3807. * @persistent_group: Whether to create a persistent group
  3808. * @freq: Frequency for the group or 0 to indicate no hardcoding
  3809. * Returns: 0 on success, -1 on failure
  3810. *
  3811. * This function creates a new P2P group with the local end as the Group Owner,
  3812. * i.e., without using Group Owner Negotiation.
  3813. */
  3814. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  3815. int freq, int ht40)
  3816. {
  3817. struct p2p_go_neg_results params;
  3818. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3819. return -1;
  3820. os_free(wpa_s->global->add_psk);
  3821. wpa_s->global->add_psk = NULL;
  3822. /* Make sure we are not running find during connection establishment */
  3823. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  3824. wpas_p2p_stop_find_oper(wpa_s);
  3825. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3826. if (freq < 0)
  3827. return -1;
  3828. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
  3829. return -1;
  3830. if (params.freq &&
  3831. !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
  3832. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  3833. "(%u MHz) is not supported for P2P uses",
  3834. params.freq);
  3835. return -1;
  3836. }
  3837. p2p_go_params(wpa_s->global->p2p, &params);
  3838. params.persistent_group = persistent_group;
  3839. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  3840. if (wpa_s == NULL)
  3841. return -1;
  3842. wpas_start_wps_go(wpa_s, &params, 0);
  3843. return 0;
  3844. }
  3845. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  3846. struct wpa_ssid *params, int addr_allocated)
  3847. {
  3848. struct wpa_ssid *ssid;
  3849. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  3850. if (wpa_s == NULL)
  3851. return -1;
  3852. wpa_supplicant_ap_deinit(wpa_s);
  3853. ssid = wpa_config_add_network(wpa_s->conf);
  3854. if (ssid == NULL)
  3855. return -1;
  3856. wpa_config_set_network_defaults(ssid);
  3857. ssid->temporary = 1;
  3858. ssid->proto = WPA_PROTO_RSN;
  3859. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  3860. ssid->group_cipher = WPA_CIPHER_CCMP;
  3861. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  3862. ssid->ssid = os_malloc(params->ssid_len);
  3863. if (ssid->ssid == NULL) {
  3864. wpa_config_remove_network(wpa_s->conf, ssid->id);
  3865. return -1;
  3866. }
  3867. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  3868. ssid->ssid_len = params->ssid_len;
  3869. ssid->p2p_group = 1;
  3870. ssid->export_keys = 1;
  3871. if (params->psk_set) {
  3872. os_memcpy(ssid->psk, params->psk, 32);
  3873. ssid->psk_set = 1;
  3874. }
  3875. if (params->passphrase)
  3876. ssid->passphrase = os_strdup(params->passphrase);
  3877. wpa_supplicant_select_network(wpa_s, ssid);
  3878. wpa_s->show_group_started = 1;
  3879. return 0;
  3880. }
  3881. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  3882. struct wpa_ssid *ssid, int addr_allocated,
  3883. int freq, int ht40,
  3884. const struct p2p_channels *channels)
  3885. {
  3886. struct p2p_go_neg_results params;
  3887. int go = 0;
  3888. if (ssid->disabled != 2 || ssid->ssid == NULL)
  3889. return -1;
  3890. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  3891. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  3892. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  3893. "already running");
  3894. return 0;
  3895. }
  3896. os_free(wpa_s->global->add_psk);
  3897. wpa_s->global->add_psk = NULL;
  3898. /* Make sure we are not running find during connection establishment */
  3899. wpas_p2p_stop_find_oper(wpa_s);
  3900. wpa_s->p2p_fallback_to_go_neg = 0;
  3901. if (ssid->mode == WPAS_MODE_INFRA)
  3902. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  3903. if (ssid->mode != WPAS_MODE_P2P_GO)
  3904. return -1;
  3905. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3906. if (freq < 0)
  3907. return -1;
  3908. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
  3909. return -1;
  3910. params.role_go = 1;
  3911. params.psk_set = ssid->psk_set;
  3912. if (params.psk_set)
  3913. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  3914. if (ssid->passphrase) {
  3915. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  3916. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  3917. "persistent group");
  3918. return -1;
  3919. }
  3920. os_strlcpy(params.passphrase, ssid->passphrase,
  3921. sizeof(params.passphrase));
  3922. }
  3923. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  3924. params.ssid_len = ssid->ssid_len;
  3925. params.persistent_group = 1;
  3926. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  3927. if (wpa_s == NULL)
  3928. return -1;
  3929. wpas_start_wps_go(wpa_s, &params, 0);
  3930. return 0;
  3931. }
  3932. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  3933. struct wpabuf *proberesp_ies)
  3934. {
  3935. struct wpa_supplicant *wpa_s = ctx;
  3936. if (wpa_s->ap_iface) {
  3937. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  3938. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  3939. wpabuf_free(beacon_ies);
  3940. wpabuf_free(proberesp_ies);
  3941. return;
  3942. }
  3943. if (beacon_ies) {
  3944. wpabuf_free(hapd->p2p_beacon_ie);
  3945. hapd->p2p_beacon_ie = beacon_ies;
  3946. }
  3947. wpabuf_free(hapd->p2p_probe_resp_ie);
  3948. hapd->p2p_probe_resp_ie = proberesp_ies;
  3949. } else {
  3950. wpabuf_free(beacon_ies);
  3951. wpabuf_free(proberesp_ies);
  3952. }
  3953. wpa_supplicant_ap_update_beacon(wpa_s);
  3954. }
  3955. static void wpas_p2p_idle_update(void *ctx, int idle)
  3956. {
  3957. struct wpa_supplicant *wpa_s = ctx;
  3958. if (!wpa_s->ap_iface)
  3959. return;
  3960. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  3961. if (idle)
  3962. wpas_p2p_set_group_idle_timeout(wpa_s);
  3963. else
  3964. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3965. }
  3966. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  3967. struct wpa_ssid *ssid)
  3968. {
  3969. struct p2p_group *group;
  3970. struct p2p_group_config *cfg;
  3971. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3972. return NULL;
  3973. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3974. return NULL;
  3975. cfg = os_zalloc(sizeof(*cfg));
  3976. if (cfg == NULL)
  3977. return NULL;
  3978. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  3979. cfg->persistent_group = 2;
  3980. else if (ssid->p2p_persistent_group)
  3981. cfg->persistent_group = 1;
  3982. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  3983. if (wpa_s->max_stations &&
  3984. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  3985. cfg->max_clients = wpa_s->max_stations;
  3986. else
  3987. cfg->max_clients = wpa_s->conf->max_num_sta;
  3988. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  3989. cfg->ssid_len = ssid->ssid_len;
  3990. cfg->cb_ctx = wpa_s;
  3991. cfg->ie_update = wpas_p2p_ie_update;
  3992. cfg->idle_update = wpas_p2p_idle_update;
  3993. group = p2p_group_init(wpa_s->global->p2p, cfg);
  3994. if (group == NULL)
  3995. os_free(cfg);
  3996. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3997. p2p_group_notif_formation_done(group);
  3998. wpa_s->p2p_group = group;
  3999. return group;
  4000. }
  4001. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4002. int registrar)
  4003. {
  4004. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4005. if (!wpa_s->p2p_in_provisioning) {
  4006. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  4007. "provisioning not in progress");
  4008. return;
  4009. }
  4010. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4011. u8 go_dev_addr[ETH_ALEN];
  4012. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  4013. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4014. ssid->ssid_len);
  4015. /* Clear any stored provisioning info */
  4016. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  4017. }
  4018. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  4019. NULL);
  4020. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4021. /*
  4022. * Use a separate timeout for initial data connection to
  4023. * complete to allow the group to be removed automatically if
  4024. * something goes wrong in this step before the P2P group idle
  4025. * timeout mechanism is taken into use.
  4026. */
  4027. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4028. wpas_p2p_group_formation_timeout,
  4029. wpa_s->parent, NULL);
  4030. }
  4031. if (wpa_s->global->p2p)
  4032. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  4033. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4034. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  4035. wpas_group_formation_completed(wpa_s, 1);
  4036. }
  4037. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  4038. struct wps_event_fail *fail)
  4039. {
  4040. if (!wpa_s->p2p_in_provisioning) {
  4041. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  4042. "provisioning not in progress");
  4043. return;
  4044. }
  4045. if (wpa_s->go_params) {
  4046. p2p_clear_provisioning_info(
  4047. wpa_s->global->p2p,
  4048. wpa_s->go_params->peer_device_addr);
  4049. }
  4050. wpas_notify_p2p_wps_failed(wpa_s, fail);
  4051. }
  4052. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4053. const char *config_method,
  4054. enum wpas_p2p_prov_disc_use use)
  4055. {
  4056. u16 config_methods;
  4057. wpa_s->p2p_fallback_to_go_neg = 0;
  4058. wpa_s->pending_pd_use = NORMAL_PD;
  4059. if (os_strncmp(config_method, "display", 7) == 0)
  4060. config_methods = WPS_CONFIG_DISPLAY;
  4061. else if (os_strncmp(config_method, "keypad", 6) == 0)
  4062. config_methods = WPS_CONFIG_KEYPAD;
  4063. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  4064. os_strncmp(config_method, "pushbutton", 10) == 0)
  4065. config_methods = WPS_CONFIG_PUSHBUTTON;
  4066. else {
  4067. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4068. return -1;
  4069. }
  4070. if (use == WPAS_P2P_PD_AUTO) {
  4071. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4072. wpa_s->pending_pd_config_methods = config_methods;
  4073. wpa_s->p2p_auto_pd = 1;
  4074. wpa_s->p2p_auto_join = 0;
  4075. wpa_s->pending_pd_before_join = 0;
  4076. wpa_s->auto_pd_scan_retry = 0;
  4077. wpas_p2p_stop_find(wpa_s);
  4078. wpa_s->p2p_join_scan_count = 0;
  4079. os_get_time(&wpa_s->p2p_auto_started);
  4080. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4081. wpa_s->p2p_auto_started.sec,
  4082. wpa_s->p2p_auto_started.usec);
  4083. wpas_p2p_join_scan(wpa_s, NULL);
  4084. return 0;
  4085. }
  4086. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4087. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  4088. config_methods,
  4089. use == WPAS_P2P_PD_FOR_JOIN);
  4090. }
  4091. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4092. return -1;
  4093. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4094. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4095. 0, 1);
  4096. }
  4097. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4098. char *end)
  4099. {
  4100. return p2p_scan_result_text(ies, ies_len, buf, end);
  4101. }
  4102. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4103. {
  4104. if (!offchannel_pending_action_tx(wpa_s))
  4105. return;
  4106. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4107. "operation request");
  4108. offchannel_clear_pending_action_tx(wpa_s);
  4109. }
  4110. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4111. enum p2p_discovery_type type,
  4112. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4113. const u8 *dev_id, unsigned int search_delay)
  4114. {
  4115. wpas_p2p_clear_pending_action_tx(wpa_s);
  4116. wpa_s->p2p_long_listen = 0;
  4117. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4118. return wpa_drv_p2p_find(wpa_s, timeout, type);
  4119. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4120. wpa_s->p2p_in_provisioning)
  4121. return -1;
  4122. wpa_supplicant_cancel_sched_scan(wpa_s);
  4123. return p2p_find(wpa_s->global->p2p, timeout, type,
  4124. num_req_dev_types, req_dev_types, dev_id,
  4125. search_delay);
  4126. }
  4127. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4128. {
  4129. wpas_p2p_clear_pending_action_tx(wpa_s);
  4130. wpa_s->p2p_long_listen = 0;
  4131. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4132. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4133. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4134. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4135. wpa_drv_p2p_stop_find(wpa_s);
  4136. return 1;
  4137. }
  4138. if (wpa_s->global->p2p)
  4139. p2p_stop_find(wpa_s->global->p2p);
  4140. return 0;
  4141. }
  4142. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4143. {
  4144. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4145. return;
  4146. wpas_p2p_remove_pending_group_interface(wpa_s);
  4147. }
  4148. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4149. {
  4150. struct wpa_supplicant *wpa_s = eloop_ctx;
  4151. wpa_s->p2p_long_listen = 0;
  4152. }
  4153. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4154. {
  4155. int res;
  4156. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4157. return -1;
  4158. wpa_supplicant_cancel_sched_scan(wpa_s);
  4159. wpas_p2p_clear_pending_action_tx(wpa_s);
  4160. if (timeout == 0) {
  4161. /*
  4162. * This is a request for unlimited Listen state. However, at
  4163. * least for now, this is mapped to a Listen state for one
  4164. * hour.
  4165. */
  4166. timeout = 3600;
  4167. }
  4168. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4169. wpa_s->p2p_long_listen = 0;
  4170. /*
  4171. * Stop previous find/listen operation to avoid trying to request a new
  4172. * remain-on-channel operation while the driver is still running the
  4173. * previous one.
  4174. */
  4175. if (wpa_s->global->p2p)
  4176. p2p_stop_find(wpa_s->global->p2p);
  4177. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4178. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4179. wpa_s->p2p_long_listen = timeout * 1000;
  4180. eloop_register_timeout(timeout, 0,
  4181. wpas_p2p_long_listen_timeout,
  4182. wpa_s, NULL);
  4183. }
  4184. return res;
  4185. }
  4186. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4187. u8 *buf, size_t len, int p2p_group)
  4188. {
  4189. struct wpabuf *p2p_ie;
  4190. int ret;
  4191. if (wpa_s->global->p2p_disabled)
  4192. return -1;
  4193. if (wpa_s->global->p2p == NULL)
  4194. return -1;
  4195. if (bss == NULL)
  4196. return -1;
  4197. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4198. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4199. p2p_group, p2p_ie);
  4200. wpabuf_free(p2p_ie);
  4201. return ret;
  4202. }
  4203. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4204. const u8 *dst, const u8 *bssid,
  4205. const u8 *ie, size_t ie_len, int ssi_signal)
  4206. {
  4207. if (wpa_s->global->p2p_disabled)
  4208. return 0;
  4209. if (wpa_s->global->p2p == NULL)
  4210. return 0;
  4211. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4212. ie, ie_len)) {
  4213. case P2P_PREQ_NOT_P2P:
  4214. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4215. ssi_signal);
  4216. /* fall through */
  4217. case P2P_PREQ_MALFORMED:
  4218. case P2P_PREQ_NOT_LISTEN:
  4219. case P2P_PREQ_NOT_PROCESSED:
  4220. default: /* make gcc happy */
  4221. return 0;
  4222. case P2P_PREQ_PROCESSED:
  4223. return 1;
  4224. }
  4225. }
  4226. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4227. const u8 *sa, const u8 *bssid,
  4228. u8 category, const u8 *data, size_t len, int freq)
  4229. {
  4230. if (wpa_s->global->p2p_disabled)
  4231. return;
  4232. if (wpa_s->global->p2p == NULL)
  4233. return;
  4234. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4235. freq);
  4236. }
  4237. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4238. {
  4239. if (wpa_s->global->p2p_disabled)
  4240. return;
  4241. if (wpa_s->global->p2p == NULL)
  4242. return;
  4243. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4244. }
  4245. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4246. {
  4247. p2p_group_deinit(wpa_s->p2p_group);
  4248. wpa_s->p2p_group = NULL;
  4249. wpa_s->ap_configured_cb = NULL;
  4250. wpa_s->ap_configured_cb_ctx = NULL;
  4251. wpa_s->ap_configured_cb_data = NULL;
  4252. wpa_s->connect_without_scan = NULL;
  4253. }
  4254. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4255. {
  4256. wpa_s->p2p_long_listen = 0;
  4257. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4258. return wpa_drv_p2p_reject(wpa_s, addr);
  4259. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4260. return -1;
  4261. return p2p_reject(wpa_s->global->p2p, addr);
  4262. }
  4263. /* Invite to reinvoke a persistent group */
  4264. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4265. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4266. int ht40, int pref_freq)
  4267. {
  4268. enum p2p_invite_role role;
  4269. u8 *bssid = NULL;
  4270. int force_freq = 0;
  4271. int res;
  4272. wpa_s->global->p2p_invite_group = NULL;
  4273. if (peer_addr)
  4274. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4275. else
  4276. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4277. wpa_s->p2p_persistent_go_freq = freq;
  4278. wpa_s->p2p_go_ht40 = !!ht40;
  4279. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4280. role = P2P_INVITE_ROLE_GO;
  4281. if (peer_addr == NULL) {
  4282. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4283. "address in invitation command");
  4284. return -1;
  4285. }
  4286. if (wpas_p2p_create_iface(wpa_s)) {
  4287. if (wpas_p2p_add_group_interface(wpa_s,
  4288. WPA_IF_P2P_GO) < 0) {
  4289. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4290. "allocate a new interface for the "
  4291. "group");
  4292. return -1;
  4293. }
  4294. bssid = wpa_s->pending_interface_addr;
  4295. } else
  4296. bssid = wpa_s->own_addr;
  4297. } else {
  4298. role = P2P_INVITE_ROLE_CLIENT;
  4299. peer_addr = ssid->bssid;
  4300. }
  4301. wpa_s->pending_invite_ssid_id = ssid->id;
  4302. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  4303. if (res)
  4304. return res;
  4305. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4306. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4307. ssid->ssid, ssid->ssid_len,
  4308. go_dev_addr, 1);
  4309. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4310. return -1;
  4311. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4312. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4313. 1, pref_freq);
  4314. }
  4315. /* Invite to join an active group */
  4316. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4317. const u8 *peer_addr, const u8 *go_dev_addr)
  4318. {
  4319. struct wpa_global *global = wpa_s->global;
  4320. enum p2p_invite_role role;
  4321. u8 *bssid = NULL;
  4322. struct wpa_ssid *ssid;
  4323. int persistent;
  4324. int force_freq = 0, pref_freq = 0;
  4325. int res;
  4326. wpa_s->p2p_persistent_go_freq = 0;
  4327. wpa_s->p2p_go_ht40 = 0;
  4328. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4329. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4330. break;
  4331. }
  4332. if (wpa_s == NULL) {
  4333. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4334. return -1;
  4335. }
  4336. ssid = wpa_s->current_ssid;
  4337. if (ssid == NULL) {
  4338. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4339. "invitation");
  4340. return -1;
  4341. }
  4342. wpa_s->global->p2p_invite_group = wpa_s;
  4343. persistent = ssid->p2p_persistent_group &&
  4344. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4345. ssid->ssid, ssid->ssid_len);
  4346. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4347. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4348. bssid = wpa_s->own_addr;
  4349. if (go_dev_addr == NULL)
  4350. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4351. } else {
  4352. role = P2P_INVITE_ROLE_CLIENT;
  4353. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4354. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4355. "invite to current group");
  4356. return -1;
  4357. }
  4358. bssid = wpa_s->bssid;
  4359. if (go_dev_addr == NULL &&
  4360. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4361. go_dev_addr = wpa_s->go_dev_addr;
  4362. }
  4363. wpa_s->parent->pending_invite_ssid_id = -1;
  4364. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4365. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4366. ssid->ssid, ssid->ssid_len,
  4367. go_dev_addr, persistent);
  4368. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4369. return -1;
  4370. res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
  4371. if (res)
  4372. return res;
  4373. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4374. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4375. ssid->ssid, ssid->ssid_len, force_freq,
  4376. go_dev_addr, persistent, pref_freq);
  4377. }
  4378. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4379. {
  4380. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4381. const char *ssid_txt;
  4382. u8 go_dev_addr[ETH_ALEN];
  4383. int network_id = -1;
  4384. int persistent;
  4385. int freq;
  4386. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4387. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4388. wpa_s->parent, NULL);
  4389. }
  4390. if (!wpa_s->show_group_started || !ssid)
  4391. goto done;
  4392. wpa_s->show_group_started = 0;
  4393. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4394. os_memset(go_dev_addr, 0, ETH_ALEN);
  4395. if (ssid->bssid_set)
  4396. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4397. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4398. ssid->ssid_len);
  4399. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4400. if (wpa_s->global->p2p_group_formation == wpa_s)
  4401. wpa_s->global->p2p_group_formation = NULL;
  4402. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4403. (int) wpa_s->assoc_freq;
  4404. if (ssid->passphrase == NULL && ssid->psk_set) {
  4405. char psk[65];
  4406. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4407. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4408. "%s client ssid=\"%s\" freq=%d psk=%s "
  4409. "go_dev_addr=" MACSTR "%s",
  4410. wpa_s->ifname, ssid_txt, freq, psk,
  4411. MAC2STR(go_dev_addr),
  4412. persistent ? " [PERSISTENT]" : "");
  4413. } else {
  4414. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4415. "%s client ssid=\"%s\" freq=%d "
  4416. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4417. wpa_s->ifname, ssid_txt, freq,
  4418. ssid->passphrase ? ssid->passphrase : "",
  4419. MAC2STR(go_dev_addr),
  4420. persistent ? " [PERSISTENT]" : "");
  4421. }
  4422. if (persistent)
  4423. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4424. ssid, go_dev_addr);
  4425. if (network_id < 0)
  4426. network_id = ssid->id;
  4427. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4428. done:
  4429. wpas_p2p_continue_after_scan(wpa_s);
  4430. }
  4431. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4432. u32 interval1, u32 duration2, u32 interval2)
  4433. {
  4434. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4435. return -1;
  4436. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4437. return -1;
  4438. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4439. wpa_s->current_ssid == NULL ||
  4440. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4441. return -1;
  4442. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4443. wpa_s->own_addr, wpa_s->assoc_freq,
  4444. duration1, interval1, duration2, interval2);
  4445. }
  4446. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4447. unsigned int interval)
  4448. {
  4449. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4450. return -1;
  4451. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4452. return -1;
  4453. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4454. }
  4455. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4456. {
  4457. if (wpa_s->current_ssid == NULL) {
  4458. /*
  4459. * current_ssid can be cleared when P2P client interface gets
  4460. * disconnected, so assume this interface was used as P2P
  4461. * client.
  4462. */
  4463. return 1;
  4464. }
  4465. return wpa_s->current_ssid->p2p_group &&
  4466. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4467. }
  4468. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4469. {
  4470. struct wpa_supplicant *wpa_s = eloop_ctx;
  4471. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4472. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4473. "disabled");
  4474. return;
  4475. }
  4476. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4477. "group");
  4478. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4479. }
  4480. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4481. {
  4482. int timeout;
  4483. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4484. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4485. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4486. return;
  4487. timeout = wpa_s->conf->p2p_group_idle;
  4488. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4489. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4490. timeout = P2P_MAX_CLIENT_IDLE;
  4491. if (timeout == 0)
  4492. return;
  4493. if (timeout < 0) {
  4494. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4495. timeout = 0; /* special client mode no-timeout */
  4496. else
  4497. return;
  4498. }
  4499. if (wpa_s->p2p_in_provisioning) {
  4500. /*
  4501. * Use the normal group formation timeout during the
  4502. * provisioning phase to avoid terminating this process too
  4503. * early due to group idle timeout.
  4504. */
  4505. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4506. "during provisioning");
  4507. return;
  4508. }
  4509. if (wpa_s->show_group_started) {
  4510. /*
  4511. * Use the normal group formation timeout between the end of
  4512. * the provisioning phase and completion of 4-way handshake to
  4513. * avoid terminating this process too early due to group idle
  4514. * timeout.
  4515. */
  4516. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4517. "while waiting for initial 4-way handshake to "
  4518. "complete");
  4519. return;
  4520. }
  4521. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4522. timeout);
  4523. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4524. wpa_s, NULL);
  4525. }
  4526. /* Returns 1 if the interface was removed */
  4527. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4528. u16 reason_code, const u8 *ie, size_t ie_len,
  4529. int locally_generated)
  4530. {
  4531. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4532. return 0;
  4533. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4534. return 0;
  4535. if (!locally_generated)
  4536. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4537. ie_len);
  4538. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4539. wpa_s->current_ssid &&
  4540. wpa_s->current_ssid->p2p_group &&
  4541. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4542. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4543. "session is ending");
  4544. if (wpas_p2p_group_delete(wpa_s,
  4545. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4546. > 0)
  4547. return 1;
  4548. }
  4549. return 0;
  4550. }
  4551. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4552. u16 reason_code, const u8 *ie, size_t ie_len,
  4553. int locally_generated)
  4554. {
  4555. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4556. return;
  4557. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4558. return;
  4559. if (!locally_generated)
  4560. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4561. ie_len);
  4562. }
  4563. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4564. {
  4565. struct p2p_data *p2p = wpa_s->global->p2p;
  4566. if (p2p == NULL)
  4567. return;
  4568. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4569. return;
  4570. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4571. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4572. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4573. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4574. if (wpa_s->wps &&
  4575. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4576. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4577. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4578. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4579. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4580. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4581. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4582. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4583. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4584. }
  4585. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4586. p2p_set_sec_dev_types(p2p,
  4587. (void *) wpa_s->conf->sec_device_type,
  4588. wpa_s->conf->num_sec_device_types);
  4589. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4590. int i;
  4591. p2p_remove_wps_vendor_extensions(p2p);
  4592. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4593. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4594. continue;
  4595. p2p_add_wps_vendor_extension(
  4596. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4597. }
  4598. }
  4599. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4600. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4601. char country[3];
  4602. country[0] = wpa_s->conf->country[0];
  4603. country[1] = wpa_s->conf->country[1];
  4604. country[2] = 0x04;
  4605. p2p_set_country(p2p, country);
  4606. }
  4607. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4608. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4609. wpa_s->conf->p2p_ssid_postfix ?
  4610. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4611. 0);
  4612. }
  4613. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4614. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4615. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4616. u8 reg_class, channel;
  4617. int ret;
  4618. unsigned int r;
  4619. if (wpa_s->conf->p2p_listen_reg_class &&
  4620. wpa_s->conf->p2p_listen_channel) {
  4621. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4622. channel = wpa_s->conf->p2p_listen_channel;
  4623. } else {
  4624. reg_class = 81;
  4625. /*
  4626. * Pick one of the social channels randomly as the
  4627. * listen channel.
  4628. */
  4629. os_get_random((u8 *) &r, sizeof(r));
  4630. channel = 1 + (r % 3) * 5;
  4631. }
  4632. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4633. if (ret)
  4634. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4635. "failed: %d", ret);
  4636. }
  4637. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4638. u8 op_reg_class, op_channel, cfg_op_channel;
  4639. int ret = 0;
  4640. unsigned int r;
  4641. if (wpa_s->conf->p2p_oper_reg_class &&
  4642. wpa_s->conf->p2p_oper_channel) {
  4643. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4644. op_channel = wpa_s->conf->p2p_oper_channel;
  4645. cfg_op_channel = 1;
  4646. } else {
  4647. op_reg_class = 81;
  4648. /*
  4649. * Use random operation channel from (1, 6, 11)
  4650. *if no other preference is indicated.
  4651. */
  4652. os_get_random((u8 *) &r, sizeof(r));
  4653. op_channel = 1 + (r % 3) * 5;
  4654. cfg_op_channel = 0;
  4655. }
  4656. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4657. cfg_op_channel);
  4658. if (ret)
  4659. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4660. "failed: %d", ret);
  4661. }
  4662. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4663. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4664. wpa_s->conf->p2p_pref_chan) < 0) {
  4665. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4666. "update failed");
  4667. }
  4668. }
  4669. }
  4670. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4671. int duration)
  4672. {
  4673. if (!wpa_s->ap_iface)
  4674. return -1;
  4675. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4676. duration);
  4677. }
  4678. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4679. {
  4680. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4681. return -1;
  4682. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4683. return -1;
  4684. wpa_s->global->cross_connection = enabled;
  4685. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4686. if (!enabled) {
  4687. struct wpa_supplicant *iface;
  4688. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4689. {
  4690. if (iface->cross_connect_enabled == 0)
  4691. continue;
  4692. iface->cross_connect_enabled = 0;
  4693. iface->cross_connect_in_use = 0;
  4694. wpa_msg_global(iface->parent, MSG_INFO,
  4695. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4696. iface->ifname,
  4697. iface->cross_connect_uplink);
  4698. }
  4699. }
  4700. return 0;
  4701. }
  4702. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4703. {
  4704. struct wpa_supplicant *iface;
  4705. if (!uplink->global->cross_connection)
  4706. return;
  4707. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4708. if (!iface->cross_connect_enabled)
  4709. continue;
  4710. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4711. 0)
  4712. continue;
  4713. if (iface->ap_iface == NULL)
  4714. continue;
  4715. if (iface->cross_connect_in_use)
  4716. continue;
  4717. iface->cross_connect_in_use = 1;
  4718. wpa_msg_global(iface->parent, MSG_INFO,
  4719. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4720. iface->ifname, iface->cross_connect_uplink);
  4721. }
  4722. }
  4723. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  4724. {
  4725. struct wpa_supplicant *iface;
  4726. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4727. if (!iface->cross_connect_enabled)
  4728. continue;
  4729. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4730. 0)
  4731. continue;
  4732. if (!iface->cross_connect_in_use)
  4733. continue;
  4734. wpa_msg_global(iface->parent, MSG_INFO,
  4735. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4736. iface->ifname, iface->cross_connect_uplink);
  4737. iface->cross_connect_in_use = 0;
  4738. }
  4739. }
  4740. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  4741. {
  4742. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  4743. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  4744. wpa_s->cross_connect_disallowed)
  4745. wpas_p2p_disable_cross_connect(wpa_s);
  4746. else
  4747. wpas_p2p_enable_cross_connect(wpa_s);
  4748. if (!wpa_s->ap_iface &&
  4749. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4750. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4751. }
  4752. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  4753. {
  4754. wpas_p2p_disable_cross_connect(wpa_s);
  4755. if (!wpa_s->ap_iface &&
  4756. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  4757. wpa_s, NULL))
  4758. wpas_p2p_set_group_idle_timeout(wpa_s);
  4759. }
  4760. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  4761. {
  4762. struct wpa_supplicant *iface;
  4763. if (!wpa_s->global->cross_connection)
  4764. return;
  4765. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  4766. if (iface == wpa_s)
  4767. continue;
  4768. if (iface->drv_flags &
  4769. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  4770. continue;
  4771. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  4772. continue;
  4773. wpa_s->cross_connect_enabled = 1;
  4774. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  4775. sizeof(wpa_s->cross_connect_uplink));
  4776. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  4777. "%s to %s whenever uplink is available",
  4778. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4779. if (iface->ap_iface || iface->current_ssid == NULL ||
  4780. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  4781. iface->cross_connect_disallowed ||
  4782. iface->wpa_state != WPA_COMPLETED)
  4783. break;
  4784. wpa_s->cross_connect_in_use = 1;
  4785. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4786. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4787. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4788. break;
  4789. }
  4790. }
  4791. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  4792. {
  4793. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  4794. !wpa_s->p2p_in_provisioning)
  4795. return 0; /* not P2P client operation */
  4796. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  4797. "session overlap");
  4798. if (wpa_s != wpa_s->parent)
  4799. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  4800. wpas_p2p_group_formation_failed(wpa_s);
  4801. return 1;
  4802. }
  4803. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  4804. {
  4805. struct p2p_channels chan;
  4806. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  4807. return;
  4808. os_memset(&chan, 0, sizeof(chan));
  4809. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  4810. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  4811. "channel list");
  4812. return;
  4813. }
  4814. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  4815. }
  4816. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  4817. struct wpa_scan_results *scan_res)
  4818. {
  4819. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  4820. }
  4821. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  4822. {
  4823. struct wpa_global *global = wpa_s->global;
  4824. int found = 0;
  4825. const u8 *peer;
  4826. if (global->p2p == NULL)
  4827. return -1;
  4828. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  4829. if (wpa_s->pending_interface_name[0] &&
  4830. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  4831. found = 1;
  4832. peer = p2p_get_go_neg_peer(global->p2p);
  4833. if (peer) {
  4834. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  4835. MACSTR, MAC2STR(peer));
  4836. p2p_unauthorize(global->p2p, peer);
  4837. found = 1;
  4838. }
  4839. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  4840. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  4841. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  4842. found = 1;
  4843. }
  4844. if (wpa_s->pending_pd_before_join) {
  4845. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  4846. wpa_s->pending_pd_before_join = 0;
  4847. found = 1;
  4848. }
  4849. wpas_p2p_stop_find(wpa_s);
  4850. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4851. if (wpa_s == global->p2p_group_formation &&
  4852. (wpa_s->p2p_in_provisioning ||
  4853. wpa_s->parent->pending_interface_type ==
  4854. WPA_IF_P2P_CLIENT)) {
  4855. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  4856. "formation found - cancelling",
  4857. wpa_s->ifname);
  4858. found = 1;
  4859. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4860. wpa_s->parent, NULL);
  4861. if (wpa_s->p2p_in_provisioning) {
  4862. wpas_group_formation_completed(wpa_s, 0);
  4863. break;
  4864. }
  4865. wpas_p2p_group_delete(wpa_s,
  4866. P2P_GROUP_REMOVAL_REQUESTED);
  4867. break;
  4868. }
  4869. }
  4870. if (!found) {
  4871. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  4872. return -1;
  4873. }
  4874. return 0;
  4875. }
  4876. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  4877. {
  4878. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4879. return;
  4880. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  4881. "being available anymore");
  4882. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  4883. }
  4884. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  4885. int freq_24, int freq_5, int freq_overall)
  4886. {
  4887. struct p2p_data *p2p = wpa_s->global->p2p;
  4888. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4889. return;
  4890. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  4891. }
  4892. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  4893. {
  4894. u8 peer[ETH_ALEN];
  4895. struct p2p_data *p2p = wpa_s->global->p2p;
  4896. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4897. return -1;
  4898. if (hwaddr_aton(addr, peer))
  4899. return -1;
  4900. return p2p_unauthorize(p2p, peer);
  4901. }
  4902. /**
  4903. * wpas_p2p_disconnect - Disconnect from a P2P Group
  4904. * @wpa_s: Pointer to wpa_supplicant data
  4905. * Returns: 0 on success, -1 on failure
  4906. *
  4907. * This can be used to disconnect from a group in which the local end is a P2P
  4908. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  4909. * virtual network interface was created for this group, that interface will be
  4910. * removed. Otherwise, only the configured P2P group network will be removed
  4911. * from the interface.
  4912. */
  4913. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  4914. {
  4915. if (wpa_s == NULL)
  4916. return -1;
  4917. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  4918. -1 : 0;
  4919. }
  4920. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  4921. {
  4922. int ret;
  4923. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4924. return 0;
  4925. ret = p2p_in_progress(wpa_s->global->p2p);
  4926. if (ret == 0) {
  4927. /*
  4928. * Check whether there is an ongoing WPS provisioning step (or
  4929. * other parts of group formation) on another interface since
  4930. * p2p_in_progress() does not report this to avoid issues for
  4931. * scans during such provisioning step.
  4932. */
  4933. if (wpa_s->global->p2p_group_formation &&
  4934. wpa_s->global->p2p_group_formation != wpa_s) {
  4935. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  4936. "in group formation",
  4937. wpa_s->global->p2p_group_formation->ifname);
  4938. ret = 1;
  4939. }
  4940. }
  4941. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  4942. struct os_time now;
  4943. os_get_time(&now);
  4944. if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
  4945. P2P_MAX_INITIAL_CONN_WAIT_GO) {
  4946. /* Wait for the first client has expired */
  4947. wpa_s->global->p2p_go_wait_client.sec = 0;
  4948. } else {
  4949. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  4950. ret = 1;
  4951. }
  4952. }
  4953. return ret;
  4954. }
  4955. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  4956. struct wpa_ssid *ssid)
  4957. {
  4958. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  4959. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4960. wpa_s->parent, NULL) > 0) {
  4961. /**
  4962. * Remove the network by scheduling the group formation
  4963. * timeout to happen immediately. The teardown code
  4964. * needs to be scheduled to run asynch later so that we
  4965. * don't delete data from under ourselves unexpectedly.
  4966. * Calling wpas_p2p_group_formation_timeout directly
  4967. * causes a series of crashes in WPS failure scenarios.
  4968. */
  4969. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  4970. "P2P group network getting removed");
  4971. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  4972. wpa_s->parent, NULL);
  4973. }
  4974. }
  4975. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  4976. const u8 *addr, const u8 *ssid,
  4977. size_t ssid_len)
  4978. {
  4979. struct wpa_ssid *s;
  4980. size_t i;
  4981. for (s = wpa_s->conf->ssid; s; s = s->next) {
  4982. if (s->disabled != 2)
  4983. continue;
  4984. if (ssid &&
  4985. (ssid_len != s->ssid_len ||
  4986. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  4987. continue;
  4988. if (addr == NULL) {
  4989. if (s->mode == WPAS_MODE_P2P_GO)
  4990. return s;
  4991. continue;
  4992. }
  4993. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  4994. return s; /* peer is GO in the persistent group */
  4995. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  4996. continue;
  4997. for (i = 0; i < s->num_p2p_clients; i++) {
  4998. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  4999. addr, ETH_ALEN) == 0)
  5000. return s; /* peer is P2P client in persistent
  5001. * group */
  5002. }
  5003. }
  5004. return NULL;
  5005. }
  5006. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  5007. const u8 *addr)
  5008. {
  5009. wpa_s->global->p2p_go_wait_client.sec = 0;
  5010. if (addr == NULL)
  5011. return;
  5012. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  5013. }
  5014. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  5015. int group_added)
  5016. {
  5017. struct wpa_supplicant *group = wpa_s;
  5018. if (wpa_s->global->p2p_group_formation)
  5019. group = wpa_s->global->p2p_group_formation;
  5020. wpa_s = wpa_s->parent;
  5021. offchannel_send_action_done(wpa_s);
  5022. if (group_added)
  5023. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  5024. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  5025. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  5026. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  5027. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  5028. wpa_s->p2p_persistent_id,
  5029. wpa_s->p2p_pd_before_go_neg,
  5030. wpa_s->p2p_go_ht40);
  5031. }
  5032. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  5033. {
  5034. if (!wpa_s->p2p_fallback_to_go_neg ||
  5035. wpa_s->p2p_in_provisioning <= 5)
  5036. return 0;
  5037. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  5038. return 0; /* peer operating as a GO */
  5039. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  5040. "fallback to GO Negotiation");
  5041. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  5042. return 1;
  5043. }
  5044. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  5045. {
  5046. const char *rn, *rn2;
  5047. struct wpa_supplicant *ifs;
  5048. if (wpa_s->wpa_state > WPA_SCANNING) {
  5049. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  5050. "concurrent operation",
  5051. P2P_CONCURRENT_SEARCH_DELAY);
  5052. return P2P_CONCURRENT_SEARCH_DELAY;
  5053. }
  5054. if (!wpa_s->driver->get_radio_name)
  5055. return 0;
  5056. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  5057. if (rn == NULL || rn[0] == '\0')
  5058. return 0;
  5059. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  5060. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  5061. continue;
  5062. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  5063. if (!rn2 || os_strcmp(rn, rn2) != 0)
  5064. continue;
  5065. if (ifs->wpa_state > WPA_SCANNING) {
  5066. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  5067. "delay due to concurrent operation on "
  5068. "interface %s",
  5069. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  5070. return P2P_CONCURRENT_SEARCH_DELAY;
  5071. }
  5072. }
  5073. return 0;
  5074. }
  5075. void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
  5076. {
  5077. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
  5078. "pending anymore (sta_scan_pending=%d "
  5079. "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
  5080. wpa_s->global->p2p_cb_on_scan_complete);
  5081. wpa_s->sta_scan_pending = 0;
  5082. if (!wpa_s->global->p2p_cb_on_scan_complete)
  5083. return;
  5084. wpa_s->global->p2p_cb_on_scan_complete = 0;
  5085. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5086. return;
  5087. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  5088. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  5089. "continued after successful connection");
  5090. p2p_increase_search_delay(wpa_s->global->p2p,
  5091. wpas_p2p_search_delay(wpa_s));
  5092. }
  5093. }
  5094. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  5095. const u8 *p2p_dev_addr,
  5096. const u8 *psk, size_t psk_len)
  5097. {
  5098. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5099. struct wpa_ssid *persistent;
  5100. struct psk_list_entry *p;
  5101. if (psk_len != sizeof(p->psk))
  5102. return;
  5103. if (p2p_dev_addr) {
  5104. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  5105. " p2p_dev_addr=" MACSTR,
  5106. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  5107. if (is_zero_ether_addr(p2p_dev_addr))
  5108. p2p_dev_addr = NULL;
  5109. } else {
  5110. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  5111. MAC2STR(mac_addr));
  5112. }
  5113. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  5114. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  5115. /* To be added to persistent group once created */
  5116. if (wpa_s->global->add_psk == NULL) {
  5117. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  5118. if (wpa_s->global->add_psk == NULL)
  5119. return;
  5120. }
  5121. p = wpa_s->global->add_psk;
  5122. if (p2p_dev_addr) {
  5123. p->p2p = 1;
  5124. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5125. } else {
  5126. p->p2p = 0;
  5127. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5128. }
  5129. os_memcpy(p->psk, psk, psk_len);
  5130. return;
  5131. }
  5132. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  5133. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  5134. return;
  5135. }
  5136. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  5137. ssid->ssid_len);
  5138. if (!persistent) {
  5139. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  5140. return;
  5141. }
  5142. p = os_zalloc(sizeof(*p));
  5143. if (p == NULL)
  5144. return;
  5145. if (p2p_dev_addr) {
  5146. p->p2p = 1;
  5147. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  5148. } else {
  5149. p->p2p = 0;
  5150. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  5151. }
  5152. os_memcpy(p->psk, psk, psk_len);
  5153. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
  5154. struct psk_list_entry *last;
  5155. last = dl_list_last(&persistent->psk_list,
  5156. struct psk_list_entry, list);
  5157. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  5158. MACSTR " (p2p=%u) to make room for a new one",
  5159. MAC2STR(last->addr), last->p2p);
  5160. dl_list_del(&last->list);
  5161. os_free(last);
  5162. }
  5163. dl_list_add(&persistent->psk_list, &p->list);
  5164. #ifndef CONFIG_NO_CONFIG_WRITE
  5165. if (wpa_s->parent->conf->update_config &&
  5166. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  5167. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  5168. #endif /* CONFIG_NO_CONFIG_WRITE */
  5169. }