p2p_supplicant.c 228 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "eloop.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "wps/wps_i.h"
  16. #include "p2p/p2p.h"
  17. #include "ap/hostapd.h"
  18. #include "ap/ap_config.h"
  19. #include "ap/sta_info.h"
  20. #include "ap/ap_drv_ops.h"
  21. #include "ap/wps_hostapd.h"
  22. #include "ap/p2p_hostapd.h"
  23. #include "ap/dfs.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "driver_i.h"
  28. #include "ap.h"
  29. #include "config_ssid.h"
  30. #include "config.h"
  31. #include "notify.h"
  32. #include "scan.h"
  33. #include "bss.h"
  34. #include "offchannel.h"
  35. #include "wps_supplicant.h"
  36. #include "p2p_supplicant.h"
  37. #include "wifi_display.h"
  38. /*
  39. * How many times to try to scan to find the GO before giving up on join
  40. * request.
  41. */
  42. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  43. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  44. #ifndef P2P_MAX_CLIENT_IDLE
  45. /*
  46. * How many seconds to try to reconnect to the GO when connection in P2P client
  47. * role has been lost.
  48. */
  49. #define P2P_MAX_CLIENT_IDLE 10
  50. #endif /* P2P_MAX_CLIENT_IDLE */
  51. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  52. /*
  53. * How many seconds to wait for initial 4-way handshake to get completed after
  54. * WPS provisioning step or after the re-invocation of a persistent group on a
  55. * P2P Client.
  56. */
  57. #define P2P_MAX_INITIAL_CONN_WAIT 10
  58. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  59. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
  60. /*
  61. * How many seconds to wait for initial 4-way handshake to get completed after
  62. * WPS provisioning step on the GO. This controls the extra time the P2P
  63. * operation is considered to be in progress (e.g., to delay other scans) after
  64. * WPS provisioning has been completed on the GO during group formation.
  65. */
  66. #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
  67. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
  68. #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
  69. /*
  70. * How many seconds to wait for initial 4-way handshake to get completed after
  71. * re-invocation of a persistent group on the GO when the client is expected
  72. * to connect automatically (no user interaction).
  73. */
  74. #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
  75. #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
  76. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  77. enum p2p_group_removal_reason {
  78. P2P_GROUP_REMOVAL_UNKNOWN,
  79. P2P_GROUP_REMOVAL_SILENT,
  80. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  81. P2P_GROUP_REMOVAL_REQUESTED,
  82. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  83. P2P_GROUP_REMOVAL_UNAVAILABLE,
  84. P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
  85. P2P_GROUP_REMOVAL_PSK_FAILURE,
  86. P2P_GROUP_REMOVAL_FREQ_CONFLICT
  87. };
  88. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  89. static struct wpa_supplicant *
  90. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  91. int go);
  92. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  93. const u8 *ssid, size_t ssid_len);
  94. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  95. const u8 *ssid, size_t ssid_len);
  96. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  97. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  98. const u8 *dev_addr, enum p2p_wps_method wps_method,
  99. int auto_join, int freq,
  100. const u8 *ssid, size_t ssid_len);
  101. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  102. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  103. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  104. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  105. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  106. void *timeout_ctx);
  107. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
  108. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  109. int group_added);
  110. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  111. static void wpas_stop_listen(void *ctx);
  112. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
  113. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
  114. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  115. enum wpa_driver_if_type type);
  116. /*
  117. * Get the number of concurrent channels that the HW can operate, but that are
  118. * currently not in use by any of the wpa_supplicant interfaces.
  119. */
  120. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  121. {
  122. int *freqs;
  123. int num, unused;
  124. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  125. if (!freqs)
  126. return -1;
  127. num = get_shared_radio_freqs(wpa_s, freqs,
  128. wpa_s->num_multichan_concurrent);
  129. os_free(freqs);
  130. unused = wpa_s->num_multichan_concurrent - num;
  131. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
  132. return unused;
  133. }
  134. /*
  135. * Get the frequencies that are currently in use by one or more of the virtual
  136. * interfaces, and that are also valid for P2P operation.
  137. */
  138. static unsigned int
  139. wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  140. struct wpa_used_freq_data *p2p_freqs,
  141. unsigned int len)
  142. {
  143. struct wpa_used_freq_data *freqs;
  144. unsigned int num, i, j;
  145. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  146. sizeof(struct wpa_used_freq_data));
  147. if (!freqs)
  148. return 0;
  149. num = get_shared_radio_freqs_data(wpa_s, freqs,
  150. wpa_s->num_multichan_concurrent);
  151. os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
  152. for (i = 0, j = 0; i < num && j < len; i++) {
  153. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  154. p2p_freqs[j++] = freqs[i];
  155. }
  156. os_free(freqs);
  157. dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
  158. return j;
  159. }
  160. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  161. int freq)
  162. {
  163. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  164. return;
  165. /* Use the wpa_s used to control the P2P Device operation */
  166. wpa_s = wpa_s->global->p2p_init_wpa_s;
  167. if (wpa_s->conf->p2p_ignore_shared_freq &&
  168. freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  169. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  170. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
  171. freq);
  172. freq = 0;
  173. }
  174. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  175. }
  176. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  177. struct wpa_scan_results *scan_res)
  178. {
  179. size_t i;
  180. if (wpa_s->p2p_scan_work) {
  181. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  182. wpa_s->p2p_scan_work = NULL;
  183. radio_work_done(work);
  184. }
  185. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  186. return;
  187. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  188. (int) scan_res->num);
  189. for (i = 0; i < scan_res->num; i++) {
  190. struct wpa_scan_res *bss = scan_res->res[i];
  191. struct os_reltime time_tmp_age, entry_ts;
  192. const u8 *ies;
  193. size_t ies_len;
  194. time_tmp_age.sec = bss->age / 1000;
  195. time_tmp_age.usec = (bss->age % 1000) * 1000;
  196. os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  197. ies = (const u8 *) (bss + 1);
  198. ies_len = bss->ie_len;
  199. if (bss->beacon_ie_len > 0 &&
  200. !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  201. wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  202. wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
  203. MACSTR, MAC2STR(bss->bssid));
  204. ies = ies + ies_len;
  205. ies_len = bss->beacon_ie_len;
  206. }
  207. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  208. bss->freq, &entry_ts, bss->level,
  209. ies, ies_len) > 0)
  210. break;
  211. }
  212. p2p_scan_res_handled(wpa_s->global->p2p);
  213. }
  214. static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  215. {
  216. struct wpa_supplicant *wpa_s = work->wpa_s;
  217. struct wpa_driver_scan_params *params = work->ctx;
  218. int ret;
  219. if (deinit) {
  220. if (!work->started) {
  221. wpa_scan_free_params(params);
  222. return;
  223. }
  224. wpa_s->p2p_scan_work = NULL;
  225. return;
  226. }
  227. ret = wpa_drv_scan(wpa_s, params);
  228. wpa_scan_free_params(params);
  229. work->ctx = NULL;
  230. if (ret) {
  231. radio_work_done(work);
  232. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  233. return;
  234. }
  235. p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
  236. os_get_reltime(&wpa_s->scan_trigger_time);
  237. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  238. wpa_s->own_scan_requested = 1;
  239. wpa_s->p2p_scan_work = work;
  240. }
  241. static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
  242. int freq)
  243. {
  244. if (wpa_s->global->p2p_24ghz_social_channels &&
  245. (freq == 2412 || freq == 2437 || freq == 2462)) {
  246. /*
  247. * Search all social channels regardless of whether these have
  248. * been disabled for P2P operating channel use to avoid missing
  249. * peers.
  250. */
  251. return 1;
  252. }
  253. return p2p_supported_freq(wpa_s->global->p2p, freq);
  254. }
  255. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  256. unsigned int num_req_dev_types,
  257. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  258. {
  259. struct wpa_supplicant *wpa_s = ctx;
  260. struct wpa_driver_scan_params *params = NULL;
  261. struct wpabuf *wps_ie, *ies;
  262. unsigned int num_channels = 0;
  263. int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
  264. size_t ielen;
  265. u8 *n, i;
  266. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  267. return -1;
  268. if (wpa_s->p2p_scan_work) {
  269. wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
  270. return -1;
  271. }
  272. params = os_zalloc(sizeof(*params));
  273. if (params == NULL)
  274. return -1;
  275. /* P2P Wildcard SSID */
  276. params->num_ssids = 1;
  277. n = os_malloc(P2P_WILDCARD_SSID_LEN);
  278. if (n == NULL)
  279. goto fail;
  280. os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
  281. params->ssids[0].ssid = n;
  282. params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  283. wpa_s->wps->dev.p2p = 1;
  284. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  285. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  286. num_req_dev_types, req_dev_types);
  287. if (wps_ie == NULL)
  288. goto fail;
  289. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  290. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  291. if (ies == NULL) {
  292. wpabuf_free(wps_ie);
  293. goto fail;
  294. }
  295. wpabuf_put_buf(ies, wps_ie);
  296. wpabuf_free(wps_ie);
  297. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  298. params->p2p_probe = 1;
  299. n = os_malloc(wpabuf_len(ies));
  300. if (n == NULL) {
  301. wpabuf_free(ies);
  302. goto fail;
  303. }
  304. os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
  305. params->extra_ies = n;
  306. params->extra_ies_len = wpabuf_len(ies);
  307. wpabuf_free(ies);
  308. switch (type) {
  309. case P2P_SCAN_SOCIAL:
  310. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
  311. sizeof(int));
  312. if (params->freqs == NULL)
  313. goto fail;
  314. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  315. if (wpas_p2p_search_social_channel(
  316. wpa_s, social_channels_freq[i]))
  317. params->freqs[num_channels++] =
  318. social_channels_freq[i];
  319. }
  320. params->freqs[num_channels++] = 0;
  321. break;
  322. case P2P_SCAN_FULL:
  323. break;
  324. case P2P_SCAN_SPECIFIC:
  325. params->freqs = os_calloc(2, sizeof(int));
  326. if (params->freqs == NULL)
  327. goto fail;
  328. params->freqs[0] = freq;
  329. params->freqs[1] = 0;
  330. break;
  331. case P2P_SCAN_SOCIAL_PLUS_ONE:
  332. params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
  333. sizeof(int));
  334. if (params->freqs == NULL)
  335. goto fail;
  336. for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
  337. if (wpas_p2p_search_social_channel(
  338. wpa_s, social_channels_freq[i]))
  339. params->freqs[num_channels++] =
  340. social_channels_freq[i];
  341. }
  342. if (p2p_supported_freq(wpa_s->global->p2p, freq))
  343. params->freqs[num_channels++] = freq;
  344. params->freqs[num_channels++] = 0;
  345. break;
  346. }
  347. radio_remove_works(wpa_s, "p2p-scan", 0);
  348. if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
  349. params) < 0)
  350. goto fail;
  351. return 0;
  352. fail:
  353. wpa_scan_free_params(params);
  354. return -1;
  355. }
  356. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  357. {
  358. switch (p2p_group_interface) {
  359. case P2P_GROUP_INTERFACE_PENDING:
  360. return WPA_IF_P2P_GROUP;
  361. case P2P_GROUP_INTERFACE_GO:
  362. return WPA_IF_P2P_GO;
  363. case P2P_GROUP_INTERFACE_CLIENT:
  364. return WPA_IF_P2P_CLIENT;
  365. }
  366. return WPA_IF_P2P_GROUP;
  367. }
  368. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  369. const u8 *ssid,
  370. size_t ssid_len, int *go)
  371. {
  372. struct wpa_ssid *s;
  373. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  374. for (s = wpa_s->conf->ssid; s; s = s->next) {
  375. if (s->disabled != 0 || !s->p2p_group ||
  376. s->ssid_len != ssid_len ||
  377. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  378. continue;
  379. if (s->mode == WPAS_MODE_P2P_GO &&
  380. s != wpa_s->current_ssid)
  381. continue;
  382. if (go)
  383. *go = s->mode == WPAS_MODE_P2P_GO;
  384. return wpa_s;
  385. }
  386. }
  387. return NULL;
  388. }
  389. static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
  390. {
  391. struct wpa_supplicant *wpa_s = eloop_ctx;
  392. wpa_printf(MSG_DEBUG,
  393. "P2P: Complete previously requested removal of %s",
  394. wpa_s->ifname);
  395. wpas_p2p_disconnect(wpa_s);
  396. }
  397. static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
  398. struct wpa_supplicant *calling_wpa_s)
  399. {
  400. if (calling_wpa_s == wpa_s && wpa_s &&
  401. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  402. /*
  403. * The calling wpa_s instance is going to be removed. Do that
  404. * from an eloop callback to keep the instance available until
  405. * the caller has returned. This my be needed, e.g., to provide
  406. * control interface responses on the per-interface socket.
  407. */
  408. if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
  409. wpa_s, NULL) < 0)
  410. return -1;
  411. return 0;
  412. }
  413. return wpas_p2p_disconnect(wpa_s);
  414. }
  415. /* Determine total number of clients in active groups where we are the GO */
  416. static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
  417. {
  418. unsigned int count = 0;
  419. struct wpa_ssid *s;
  420. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  421. for (s = wpa_s->conf->ssid; s; s = s->next) {
  422. wpa_printf(MSG_DEBUG,
  423. "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
  424. wpa_s, s, s->disabled, s->p2p_group,
  425. s->mode);
  426. if (!s->disabled && s->p2p_group &&
  427. s->mode == WPAS_MODE_P2P_GO) {
  428. count += p2p_get_group_num_members(
  429. wpa_s->p2p_group);
  430. }
  431. }
  432. }
  433. return count;
  434. }
  435. /* Find an interface for a P2P group where we are the GO */
  436. static struct wpa_supplicant *
  437. wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
  438. {
  439. struct wpa_supplicant *save = NULL;
  440. struct wpa_ssid *s;
  441. if (!wpa_s)
  442. return NULL;
  443. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  444. for (s = wpa_s->conf->ssid; s; s = s->next) {
  445. if (s->disabled || !s->p2p_group ||
  446. s->mode != WPAS_MODE_P2P_GO)
  447. continue;
  448. /* Prefer a group with connected clients */
  449. if (p2p_get_group_num_members(wpa_s->p2p_group))
  450. return wpa_s;
  451. save = wpa_s;
  452. }
  453. }
  454. /* No group with connected clients, so pick the one without (if any) */
  455. return save;
  456. }
  457. /* Find an active P2P group where we are the GO */
  458. static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
  459. u8 *bssid)
  460. {
  461. struct wpa_ssid *s, *empty = NULL;
  462. if (!wpa_s)
  463. return 0;
  464. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  465. for (s = wpa_s->conf->ssid; s; s = s->next) {
  466. if (s->disabled || !s->p2p_group ||
  467. s->mode != WPAS_MODE_P2P_GO)
  468. continue;
  469. os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
  470. if (p2p_get_group_num_members(wpa_s->p2p_group))
  471. return s;
  472. empty = s;
  473. }
  474. }
  475. return empty;
  476. }
  477. /* Find a persistent group where we are the GO */
  478. static struct wpa_ssid *
  479. wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
  480. {
  481. struct wpa_ssid *s;
  482. for (s = wpa_s->conf->ssid; s; s = s->next) {
  483. if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
  484. return s;
  485. }
  486. return NULL;
  487. }
  488. static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
  489. {
  490. struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
  491. struct wpa_ssid *s;
  492. u8 conncap = P2PS_SETUP_NONE;
  493. unsigned int owned_members = 0;
  494. unsigned int owner = 0;
  495. unsigned int client = 0;
  496. struct wpa_supplicant *go_wpa_s;
  497. struct wpa_ssid *persistent_go;
  498. int p2p_no_group_iface;
  499. wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
  500. /*
  501. * For non-concurrent capable devices:
  502. * If persistent_go, then no new.
  503. * If GO, then no client.
  504. * If client, then no GO.
  505. */
  506. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  507. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  508. p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
  509. wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
  510. go_wpa_s, persistent_go);
  511. for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
  512. tmp_wpa_s = tmp_wpa_s->next) {
  513. for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
  514. wpa_printf(MSG_DEBUG,
  515. "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
  516. tmp_wpa_s, s, s->disabled,
  517. s->p2p_group, s->mode);
  518. if (!s->disabled && s->p2p_group) {
  519. if (s->mode == WPAS_MODE_P2P_GO) {
  520. owned_members +=
  521. p2p_get_group_num_members(
  522. tmp_wpa_s->p2p_group);
  523. owner++;
  524. } else
  525. client++;
  526. }
  527. }
  528. }
  529. /* If not concurrent, restrict our choices */
  530. if (p2p_no_group_iface) {
  531. wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
  532. if (client)
  533. return P2PS_SETUP_NONE;
  534. if (go_wpa_s) {
  535. if (role == P2PS_SETUP_CLIENT ||
  536. incoming == P2PS_SETUP_GROUP_OWNER ||
  537. p2p_client_limit_reached(go_wpa_s->p2p_group))
  538. return P2PS_SETUP_NONE;
  539. return P2PS_SETUP_GROUP_OWNER;
  540. }
  541. if (persistent_go) {
  542. if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
  543. if (!incoming)
  544. return P2PS_SETUP_GROUP_OWNER |
  545. P2PS_SETUP_CLIENT;
  546. if (incoming == P2PS_SETUP_NEW) {
  547. u8 r;
  548. if (os_get_random(&r, sizeof(r)) < 0 ||
  549. (r & 1))
  550. return P2PS_SETUP_CLIENT;
  551. return P2PS_SETUP_GROUP_OWNER;
  552. }
  553. }
  554. }
  555. }
  556. /* If a required role has been specified, handle it here */
  557. if (role && role != P2PS_SETUP_NEW) {
  558. switch (incoming) {
  559. case P2PS_SETUP_NONE:
  560. case P2PS_SETUP_NEW:
  561. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  562. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  563. conncap = role;
  564. goto grp_owner;
  565. case P2PS_SETUP_GROUP_OWNER:
  566. /*
  567. * Must be a complimentary role - cannot be a client to
  568. * more than one peer.
  569. */
  570. if (incoming == role || client)
  571. return P2PS_SETUP_NONE;
  572. return P2PS_SETUP_CLIENT;
  573. case P2PS_SETUP_CLIENT:
  574. /* Must be a complimentary role */
  575. if (incoming != role) {
  576. conncap = P2PS_SETUP_GROUP_OWNER;
  577. goto grp_owner;
  578. }
  579. default:
  580. return P2PS_SETUP_NONE;
  581. }
  582. }
  583. /*
  584. * For now, we only will support ownership of one group, and being a
  585. * client of one group. Therefore, if we have either an existing GO
  586. * group, or an existing client group, we will not do a new GO
  587. * negotiation, but rather try to re-use the existing groups.
  588. */
  589. switch (incoming) {
  590. case P2PS_SETUP_NONE:
  591. case P2PS_SETUP_NEW:
  592. if (client)
  593. conncap = P2PS_SETUP_GROUP_OWNER;
  594. else if (!owned_members)
  595. conncap = P2PS_SETUP_NEW;
  596. else if (incoming == P2PS_SETUP_NONE)
  597. conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
  598. else
  599. conncap = P2PS_SETUP_CLIENT;
  600. break;
  601. case P2PS_SETUP_CLIENT:
  602. conncap = P2PS_SETUP_GROUP_OWNER;
  603. break;
  604. case P2PS_SETUP_GROUP_OWNER:
  605. if (!client)
  606. conncap = P2PS_SETUP_CLIENT;
  607. break;
  608. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
  609. case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
  610. if (client)
  611. conncap = P2PS_SETUP_GROUP_OWNER;
  612. else {
  613. u8 r;
  614. if (os_get_random(&r, sizeof(r)) < 0 ||
  615. (r & 1))
  616. conncap = P2PS_SETUP_CLIENT;
  617. else
  618. conncap = P2PS_SETUP_GROUP_OWNER;
  619. }
  620. break;
  621. default:
  622. return P2PS_SETUP_NONE;
  623. }
  624. grp_owner:
  625. if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
  626. (!incoming && (conncap & P2PS_SETUP_NEW))) {
  627. if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
  628. conncap &= ~P2PS_SETUP_GROUP_OWNER;
  629. wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
  630. owner, owned_members, conncap);
  631. s = wpas_p2p_get_persistent_go(wpa_s);
  632. if (!s && !owner && p2p_no_group_iface) {
  633. p2p_set_intended_addr(wpa_s->global->p2p,
  634. wpa_s->own_addr);
  635. } else if (!s && !owner) {
  636. if (wpas_p2p_add_group_interface(wpa_s,
  637. WPA_IF_P2P_GO) < 0) {
  638. wpa_printf(MSG_ERROR,
  639. "P2P: Failed to allocate a new interface for the group");
  640. return P2PS_SETUP_NONE;
  641. }
  642. wpa_s->global->pending_group_iface_for_p2ps = 1;
  643. p2p_set_intended_addr(wpa_s->global->p2p,
  644. wpa_s->pending_interface_addr);
  645. }
  646. }
  647. return conncap;
  648. }
  649. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  650. enum p2p_group_removal_reason removal_reason)
  651. {
  652. struct wpa_ssid *ssid;
  653. char *gtype;
  654. const char *reason;
  655. ssid = wpa_s->current_ssid;
  656. if (ssid == NULL) {
  657. /*
  658. * The current SSID was not known, but there may still be a
  659. * pending P2P group interface waiting for provisioning or a
  660. * P2P group that is trying to reconnect.
  661. */
  662. ssid = wpa_s->conf->ssid;
  663. while (ssid) {
  664. if (ssid->p2p_group && ssid->disabled != 2)
  665. break;
  666. ssid = ssid->next;
  667. }
  668. if (ssid == NULL &&
  669. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  670. {
  671. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  672. "not found");
  673. return -1;
  674. }
  675. }
  676. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  677. gtype = "GO";
  678. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  679. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  680. wpa_s->reassociate = 0;
  681. wpa_s->disconnected = 1;
  682. gtype = "client";
  683. } else
  684. gtype = "GO";
  685. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  686. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  687. if (os_strcmp(gtype, "client") == 0) {
  688. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  689. if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
  690. wpa_s, NULL)) {
  691. wpa_printf(MSG_DEBUG,
  692. "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
  693. removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
  694. eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
  695. wpa_s, NULL);
  696. }
  697. }
  698. if (wpa_s->cross_connect_in_use) {
  699. wpa_s->cross_connect_in_use = 0;
  700. wpa_msg_global(wpa_s->parent, MSG_INFO,
  701. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  702. wpa_s->ifname, wpa_s->cross_connect_uplink);
  703. }
  704. switch (removal_reason) {
  705. case P2P_GROUP_REMOVAL_REQUESTED:
  706. reason = " reason=REQUESTED";
  707. break;
  708. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  709. reason = " reason=FORMATION_FAILED";
  710. break;
  711. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  712. reason = " reason=IDLE";
  713. break;
  714. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  715. reason = " reason=UNAVAILABLE";
  716. break;
  717. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  718. reason = " reason=GO_ENDING_SESSION";
  719. break;
  720. case P2P_GROUP_REMOVAL_PSK_FAILURE:
  721. reason = " reason=PSK_FAILURE";
  722. break;
  723. case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
  724. reason = " reason=FREQ_CONFLICT";
  725. break;
  726. default:
  727. reason = "";
  728. break;
  729. }
  730. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  731. wpa_msg_global(wpa_s->parent, MSG_INFO,
  732. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  733. wpa_s->ifname, gtype, reason);
  734. }
  735. if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
  736. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
  737. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  738. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  739. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  740. wpa_s->parent, NULL) > 0) {
  741. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  742. "timeout");
  743. wpa_s->p2p_in_provisioning = 0;
  744. }
  745. wpa_s->p2p_in_invitation = 0;
  746. /*
  747. * Make sure wait for the first client does not remain active after the
  748. * group has been removed.
  749. */
  750. wpa_s->global->p2p_go_wait_client.sec = 0;
  751. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  752. struct wpa_global *global;
  753. char *ifname;
  754. enum wpa_driver_if_type type;
  755. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  756. wpa_s->ifname);
  757. global = wpa_s->global;
  758. ifname = os_strdup(wpa_s->ifname);
  759. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  760. eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
  761. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  762. wpa_s = global->ifaces;
  763. if (wpa_s && ifname)
  764. wpa_drv_if_remove(wpa_s, type, ifname);
  765. os_free(ifname);
  766. return 1;
  767. }
  768. if (!wpa_s->p2p_go_group_formation_completed) {
  769. wpa_s->global->p2p_group_formation = NULL;
  770. wpa_s->p2p_in_provisioning = 0;
  771. }
  772. wpa_s->show_group_started = 0;
  773. os_free(wpa_s->go_params);
  774. wpa_s->go_params = NULL;
  775. os_free(wpa_s->p2p_group_common_freqs);
  776. wpa_s->p2p_group_common_freqs = NULL;
  777. wpa_s->p2p_group_common_freqs_num = 0;
  778. wpa_s->waiting_presence_resp = 0;
  779. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  780. if (ssid && (ssid->p2p_group ||
  781. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  782. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  783. int id = ssid->id;
  784. if (ssid == wpa_s->current_ssid) {
  785. wpa_sm_set_config(wpa_s->wpa, NULL);
  786. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  787. wpa_s->current_ssid = NULL;
  788. }
  789. /*
  790. * Networks objects created during any P2P activities are not
  791. * exposed out as they might/will confuse certain non-P2P aware
  792. * applications since these network objects won't behave like
  793. * regular ones.
  794. *
  795. * Likewise, we don't send out network removed signals for such
  796. * network objects.
  797. */
  798. wpa_config_remove_network(wpa_s->conf, id);
  799. wpa_supplicant_clear_status(wpa_s);
  800. wpa_supplicant_cancel_sched_scan(wpa_s);
  801. } else {
  802. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  803. "found");
  804. }
  805. if (wpa_s->ap_iface)
  806. wpa_supplicant_ap_deinit(wpa_s);
  807. else
  808. wpa_drv_deinit_p2p_cli(wpa_s);
  809. return 0;
  810. }
  811. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  812. u8 *go_dev_addr,
  813. const u8 *ssid, size_t ssid_len)
  814. {
  815. struct wpa_bss *bss;
  816. const u8 *bssid;
  817. struct wpabuf *p2p;
  818. u8 group_capab;
  819. const u8 *addr;
  820. if (wpa_s->go_params)
  821. bssid = wpa_s->go_params->peer_interface_addr;
  822. else
  823. bssid = wpa_s->bssid;
  824. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  825. if (bss == NULL && wpa_s->go_params &&
  826. !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
  827. bss = wpa_bss_get_p2p_dev_addr(
  828. wpa_s, wpa_s->go_params->peer_device_addr);
  829. if (bss == NULL) {
  830. u8 iface_addr[ETH_ALEN];
  831. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  832. iface_addr) == 0)
  833. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  834. }
  835. if (bss == NULL) {
  836. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  837. "group is persistent - BSS " MACSTR " not found",
  838. MAC2STR(bssid));
  839. return 0;
  840. }
  841. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  842. if (p2p == NULL)
  843. p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
  844. P2P_IE_VENDOR_TYPE);
  845. if (p2p == NULL) {
  846. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  847. "group is persistent - BSS " MACSTR
  848. " did not include P2P IE", MAC2STR(bssid));
  849. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  850. (u8 *) (bss + 1), bss->ie_len);
  851. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  852. ((u8 *) bss + 1) + bss->ie_len,
  853. bss->beacon_ie_len);
  854. return 0;
  855. }
  856. group_capab = p2p_get_group_capab(p2p);
  857. addr = p2p_get_go_dev_addr(p2p);
  858. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  859. "group_capab=0x%x", group_capab);
  860. if (addr) {
  861. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  862. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  863. MAC2STR(addr));
  864. } else
  865. os_memset(go_dev_addr, 0, ETH_ALEN);
  866. wpabuf_free(p2p);
  867. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  868. "go_dev_addr=" MACSTR,
  869. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  870. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  871. }
  872. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  873. struct wpa_ssid *ssid,
  874. const u8 *go_dev_addr)
  875. {
  876. struct wpa_ssid *s;
  877. int changed = 0;
  878. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  879. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  880. for (s = wpa_s->conf->ssid; s; s = s->next) {
  881. if (s->disabled == 2 &&
  882. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  883. s->ssid_len == ssid->ssid_len &&
  884. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  885. break;
  886. }
  887. if (s) {
  888. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  889. "entry");
  890. if (ssid->passphrase && !s->passphrase)
  891. changed = 1;
  892. else if (ssid->passphrase && s->passphrase &&
  893. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  894. changed = 1;
  895. } else {
  896. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  897. "entry");
  898. changed = 1;
  899. s = wpa_config_add_network(wpa_s->conf);
  900. if (s == NULL)
  901. return -1;
  902. /*
  903. * Instead of network_added we emit persistent_group_added
  904. * notification. Also to keep the defense checks in
  905. * persistent_group obj registration method, we set the
  906. * relevant flags in s to designate it as a persistent group.
  907. */
  908. s->p2p_group = 1;
  909. s->p2p_persistent_group = 1;
  910. wpas_notify_persistent_group_added(wpa_s, s);
  911. wpa_config_set_network_defaults(s);
  912. }
  913. s->p2p_group = 1;
  914. s->p2p_persistent_group = 1;
  915. s->disabled = 2;
  916. s->bssid_set = 1;
  917. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  918. s->mode = ssid->mode;
  919. s->auth_alg = WPA_AUTH_ALG_OPEN;
  920. s->key_mgmt = WPA_KEY_MGMT_PSK;
  921. s->proto = WPA_PROTO_RSN;
  922. s->pairwise_cipher = WPA_CIPHER_CCMP;
  923. s->export_keys = 1;
  924. if (ssid->passphrase) {
  925. os_free(s->passphrase);
  926. s->passphrase = os_strdup(ssid->passphrase);
  927. }
  928. if (ssid->psk_set) {
  929. s->psk_set = 1;
  930. os_memcpy(s->psk, ssid->psk, 32);
  931. }
  932. if (s->passphrase && !s->psk_set)
  933. wpa_config_update_psk(s);
  934. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  935. os_free(s->ssid);
  936. s->ssid = os_malloc(ssid->ssid_len);
  937. }
  938. if (s->ssid) {
  939. s->ssid_len = ssid->ssid_len;
  940. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  941. }
  942. if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
  943. dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
  944. wpa_s->global->add_psk = NULL;
  945. changed = 1;
  946. }
  947. if (changed && wpa_s->conf->update_config &&
  948. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  949. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  950. }
  951. return s->id;
  952. }
  953. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  954. const u8 *addr)
  955. {
  956. struct wpa_ssid *ssid, *s;
  957. u8 *n;
  958. size_t i;
  959. int found = 0;
  960. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  961. ssid = wpa_s->current_ssid;
  962. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  963. !ssid->p2p_persistent_group)
  964. return;
  965. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  966. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  967. continue;
  968. if (s->ssid_len == ssid->ssid_len &&
  969. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  970. break;
  971. }
  972. if (s == NULL)
  973. return;
  974. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  975. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
  976. ETH_ALEN) != 0)
  977. continue;
  978. if (i == s->num_p2p_clients - 1)
  979. return; /* already the most recent entry */
  980. /* move the entry to mark it most recent */
  981. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  982. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  983. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  984. os_memcpy(s->p2p_client_list +
  985. (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
  986. ETH_ALEN);
  987. os_memset(s->p2p_client_list +
  988. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  989. 0xff, ETH_ALEN);
  990. found = 1;
  991. break;
  992. }
  993. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  994. n = os_realloc_array(s->p2p_client_list,
  995. s->num_p2p_clients + 1, 2 * ETH_ALEN);
  996. if (n == NULL)
  997. return;
  998. os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
  999. ETH_ALEN);
  1000. os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
  1001. 0xff, ETH_ALEN);
  1002. s->p2p_client_list = n;
  1003. s->num_p2p_clients++;
  1004. } else if (!found && s->p2p_client_list) {
  1005. /* Not enough room for an additional entry - drop the oldest
  1006. * entry */
  1007. os_memmove(s->p2p_client_list,
  1008. s->p2p_client_list + 2 * ETH_ALEN,
  1009. (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
  1010. os_memcpy(s->p2p_client_list +
  1011. (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
  1012. addr, ETH_ALEN);
  1013. os_memset(s->p2p_client_list +
  1014. (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
  1015. 0xff, ETH_ALEN);
  1016. }
  1017. if (p2p_wpa_s->conf->update_config &&
  1018. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  1019. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1020. }
  1021. static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
  1022. int go, struct wpa_ssid *ssid, int freq,
  1023. const u8 *psk, const char *passphrase,
  1024. const u8 *go_dev_addr, int persistent,
  1025. const char *extra)
  1026. {
  1027. const char *ssid_txt;
  1028. char psk_txt[65];
  1029. if (psk)
  1030. wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
  1031. else
  1032. psk_txt[0] = '\0';
  1033. if (ssid)
  1034. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  1035. else
  1036. ssid_txt = "";
  1037. if (passphrase && passphrase[0] == '\0')
  1038. passphrase = NULL;
  1039. /*
  1040. * Include PSK/passphrase only in the control interface message and
  1041. * leave it out from the debug log entry.
  1042. */
  1043. wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
  1044. P2P_EVENT_GROUP_STARTED
  1045. "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
  1046. MACSTR "%s%s",
  1047. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1048. psk ? " psk=" : "", psk_txt,
  1049. passphrase ? " passphrase=\"" : "",
  1050. passphrase ? passphrase : "",
  1051. passphrase ? "\"" : "",
  1052. MAC2STR(go_dev_addr),
  1053. persistent ? " [PERSISTENT]" : "", extra);
  1054. wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
  1055. "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
  1056. wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
  1057. MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
  1058. extra);
  1059. }
  1060. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  1061. int success)
  1062. {
  1063. struct wpa_ssid *ssid;
  1064. int client;
  1065. int persistent;
  1066. u8 go_dev_addr[ETH_ALEN];
  1067. int network_id = -1;
  1068. /*
  1069. * This callback is likely called for the main interface. Update wpa_s
  1070. * to use the group interface if a new interface was created for the
  1071. * group.
  1072. */
  1073. if (wpa_s->global->p2p_group_formation)
  1074. wpa_s = wpa_s->global->p2p_group_formation;
  1075. if (wpa_s->p2p_go_group_formation_completed) {
  1076. wpa_s->global->p2p_group_formation = NULL;
  1077. wpa_s->p2p_in_provisioning = 0;
  1078. }
  1079. wpa_s->p2p_in_invitation = 0;
  1080. wpa_s->group_formation_reported = 1;
  1081. if (!success) {
  1082. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1083. P2P_EVENT_GROUP_FORMATION_FAILURE);
  1084. wpas_p2p_group_delete(wpa_s,
  1085. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  1086. return;
  1087. }
  1088. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1089. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  1090. ssid = wpa_s->current_ssid;
  1091. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1092. ssid->mode = WPAS_MODE_P2P_GO;
  1093. p2p_group_notif_formation_done(wpa_s->p2p_group);
  1094. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  1095. }
  1096. persistent = 0;
  1097. if (ssid) {
  1098. client = ssid->mode == WPAS_MODE_INFRA;
  1099. if (ssid->mode == WPAS_MODE_P2P_GO) {
  1100. persistent = ssid->p2p_persistent_group;
  1101. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  1102. ETH_ALEN);
  1103. } else
  1104. persistent = wpas_p2p_persistent_group(wpa_s,
  1105. go_dev_addr,
  1106. ssid->ssid,
  1107. ssid->ssid_len);
  1108. } else {
  1109. client = wpa_s->p2p_group_interface ==
  1110. P2P_GROUP_INTERFACE_CLIENT;
  1111. os_memset(go_dev_addr, 0, ETH_ALEN);
  1112. }
  1113. wpa_s->show_group_started = 0;
  1114. if (client) {
  1115. /*
  1116. * Indicate event only after successfully completed 4-way
  1117. * handshake, i.e., when the interface is ready for data
  1118. * packets.
  1119. */
  1120. wpa_s->show_group_started = 1;
  1121. } else {
  1122. wpas_p2p_group_started(wpa_s, 1, ssid,
  1123. ssid ? ssid->frequency : 0,
  1124. ssid && ssid->passphrase == NULL &&
  1125. ssid->psk_set ? ssid->psk : NULL,
  1126. ssid ? ssid->passphrase : NULL,
  1127. go_dev_addr, persistent, "");
  1128. wpas_p2p_cross_connect_setup(wpa_s);
  1129. wpas_p2p_set_group_idle_timeout(wpa_s);
  1130. }
  1131. if (persistent)
  1132. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  1133. ssid, go_dev_addr);
  1134. else {
  1135. os_free(wpa_s->global->add_psk);
  1136. wpa_s->global->add_psk = NULL;
  1137. }
  1138. if (network_id < 0 && ssid)
  1139. network_id = ssid->id;
  1140. if (!client) {
  1141. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1142. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1143. }
  1144. }
  1145. struct send_action_work {
  1146. unsigned int freq;
  1147. u8 dst[ETH_ALEN];
  1148. u8 src[ETH_ALEN];
  1149. u8 bssid[ETH_ALEN];
  1150. size_t len;
  1151. unsigned int wait_time;
  1152. u8 buf[0];
  1153. };
  1154. static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
  1155. void *timeout_ctx)
  1156. {
  1157. struct wpa_supplicant *wpa_s = eloop_ctx;
  1158. if (!wpa_s->p2p_send_action_work)
  1159. return;
  1160. wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
  1161. os_free(wpa_s->p2p_send_action_work->ctx);
  1162. radio_work_done(wpa_s->p2p_send_action_work);
  1163. wpa_s->p2p_send_action_work = NULL;
  1164. }
  1165. static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
  1166. {
  1167. if (wpa_s->p2p_send_action_work) {
  1168. struct send_action_work *awork;
  1169. awork = wpa_s->p2p_send_action_work->ctx;
  1170. if (awork->wait_time == 0) {
  1171. os_free(awork);
  1172. radio_work_done(wpa_s->p2p_send_action_work);
  1173. wpa_s->p2p_send_action_work = NULL;
  1174. } else {
  1175. /*
  1176. * In theory, this should not be needed, but number of
  1177. * places in the P2P code is still using non-zero wait
  1178. * time for the last Action frame in the sequence and
  1179. * some of these do not call send_action_done().
  1180. */
  1181. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1182. wpa_s, NULL);
  1183. eloop_register_timeout(
  1184. 0, awork->wait_time * 1000,
  1185. wpas_p2p_send_action_work_timeout,
  1186. wpa_s, NULL);
  1187. }
  1188. }
  1189. }
  1190. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  1191. unsigned int freq,
  1192. const u8 *dst, const u8 *src,
  1193. const u8 *bssid,
  1194. const u8 *data, size_t data_len,
  1195. enum offchannel_send_action_result
  1196. result)
  1197. {
  1198. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  1199. wpas_p2p_action_tx_clear(wpa_s);
  1200. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  1201. return;
  1202. switch (result) {
  1203. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  1204. res = P2P_SEND_ACTION_SUCCESS;
  1205. break;
  1206. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  1207. res = P2P_SEND_ACTION_NO_ACK;
  1208. break;
  1209. case OFFCHANNEL_SEND_ACTION_FAILED:
  1210. res = P2P_SEND_ACTION_FAILED;
  1211. break;
  1212. }
  1213. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  1214. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  1215. wpa_s->pending_pd_before_join &&
  1216. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  1217. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  1218. wpa_s->p2p_fallback_to_go_neg) {
  1219. wpa_s->pending_pd_before_join = 0;
  1220. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  1221. "during p2p_connect-auto");
  1222. wpa_msg_global(wpa_s->parent, MSG_INFO,
  1223. P2P_EVENT_FALLBACK_TO_GO_NEG
  1224. "reason=no-ACK-to-PD-Req");
  1225. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  1226. return;
  1227. }
  1228. }
  1229. static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
  1230. {
  1231. struct wpa_supplicant *wpa_s = work->wpa_s;
  1232. struct send_action_work *awork = work->ctx;
  1233. if (deinit) {
  1234. if (work->started) {
  1235. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1236. wpa_s, NULL);
  1237. wpa_s->p2p_send_action_work = NULL;
  1238. offchannel_send_action_done(wpa_s);
  1239. }
  1240. os_free(awork);
  1241. return;
  1242. }
  1243. if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
  1244. awork->bssid, awork->buf, awork->len,
  1245. awork->wait_time,
  1246. wpas_p2p_send_action_tx_status, 1) < 0) {
  1247. os_free(awork);
  1248. radio_work_done(work);
  1249. return;
  1250. }
  1251. wpa_s->p2p_send_action_work = work;
  1252. }
  1253. static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
  1254. unsigned int freq, const u8 *dst,
  1255. const u8 *src, const u8 *bssid, const u8 *buf,
  1256. size_t len, unsigned int wait_time)
  1257. {
  1258. struct send_action_work *awork;
  1259. if (wpa_s->p2p_send_action_work) {
  1260. wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
  1261. return -1;
  1262. }
  1263. awork = os_zalloc(sizeof(*awork) + len);
  1264. if (awork == NULL)
  1265. return -1;
  1266. awork->freq = freq;
  1267. os_memcpy(awork->dst, dst, ETH_ALEN);
  1268. os_memcpy(awork->src, src, ETH_ALEN);
  1269. os_memcpy(awork->bssid, bssid, ETH_ALEN);
  1270. awork->len = len;
  1271. awork->wait_time = wait_time;
  1272. os_memcpy(awork->buf, buf, len);
  1273. if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
  1274. wpas_send_action_cb, awork) < 0) {
  1275. os_free(awork);
  1276. return -1;
  1277. }
  1278. return 0;
  1279. }
  1280. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  1281. const u8 *src, const u8 *bssid, const u8 *buf,
  1282. size_t len, unsigned int wait_time)
  1283. {
  1284. struct wpa_supplicant *wpa_s = ctx;
  1285. int listen_freq = -1, send_freq = -1;
  1286. if (wpa_s->p2p_listen_work)
  1287. listen_freq = wpa_s->p2p_listen_work->freq;
  1288. if (wpa_s->p2p_send_action_work)
  1289. send_freq = wpa_s->p2p_send_action_work->freq;
  1290. if (listen_freq != (int) freq && send_freq != (int) freq) {
  1291. wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
  1292. listen_freq, send_freq);
  1293. return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
  1294. len, wait_time);
  1295. }
  1296. wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
  1297. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  1298. wait_time,
  1299. wpas_p2p_send_action_tx_status, 1);
  1300. }
  1301. static void wpas_send_action_done(void *ctx)
  1302. {
  1303. struct wpa_supplicant *wpa_s = ctx;
  1304. if (wpa_s->p2p_send_action_work) {
  1305. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
  1306. wpa_s, NULL);
  1307. os_free(wpa_s->p2p_send_action_work->ctx);
  1308. radio_work_done(wpa_s->p2p_send_action_work);
  1309. wpa_s->p2p_send_action_work = NULL;
  1310. }
  1311. offchannel_send_action_done(wpa_s);
  1312. }
  1313. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  1314. struct p2p_go_neg_results *params)
  1315. {
  1316. if (wpa_s->go_params == NULL) {
  1317. wpa_s->go_params = os_malloc(sizeof(*params));
  1318. if (wpa_s->go_params == NULL)
  1319. return -1;
  1320. }
  1321. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  1322. return 0;
  1323. }
  1324. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  1325. struct p2p_go_neg_results *res)
  1326. {
  1327. wpa_s->group_formation_reported = 0;
  1328. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
  1329. " dev_addr " MACSTR " wps_method %d",
  1330. MAC2STR(res->peer_interface_addr),
  1331. MAC2STR(res->peer_device_addr), res->wps_method);
  1332. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  1333. res->ssid, res->ssid_len);
  1334. wpa_supplicant_ap_deinit(wpa_s);
  1335. wpas_copy_go_neg_results(wpa_s, res);
  1336. if (res->wps_method == WPS_PBC) {
  1337. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  1338. #ifdef CONFIG_WPS_NFC
  1339. } else if (res->wps_method == WPS_NFC) {
  1340. wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
  1341. res->peer_interface_addr,
  1342. wpa_s->parent->p2p_oob_dev_pw,
  1343. wpa_s->parent->p2p_oob_dev_pw_id, 1,
  1344. wpa_s->parent->p2p_oob_dev_pw_id ==
  1345. DEV_PW_NFC_CONNECTION_HANDOVER ?
  1346. wpa_s->parent->p2p_peer_oob_pubkey_hash :
  1347. NULL,
  1348. NULL, 0, 0);
  1349. #endif /* CONFIG_WPS_NFC */
  1350. } else {
  1351. u16 dev_pw_id = DEV_PW_DEFAULT;
  1352. if (wpa_s->p2p_wps_method == WPS_P2PS)
  1353. dev_pw_id = DEV_PW_P2PS_DEFAULT;
  1354. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  1355. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  1356. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  1357. wpa_s->p2p_pin, 1, dev_pw_id);
  1358. }
  1359. }
  1360. static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
  1361. struct wpa_ssid *ssid)
  1362. {
  1363. struct wpa_ssid *persistent;
  1364. struct psk_list_entry *psk;
  1365. struct hostapd_data *hapd;
  1366. if (!wpa_s->ap_iface)
  1367. return;
  1368. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  1369. ssid->ssid_len);
  1370. if (persistent == NULL)
  1371. return;
  1372. hapd = wpa_s->ap_iface->bss[0];
  1373. dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
  1374. list) {
  1375. struct hostapd_wpa_psk *hpsk;
  1376. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
  1377. MACSTR " psk=%d",
  1378. MAC2STR(psk->addr), psk->p2p);
  1379. hpsk = os_zalloc(sizeof(*hpsk));
  1380. if (hpsk == NULL)
  1381. break;
  1382. os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
  1383. if (psk->p2p)
  1384. os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
  1385. else
  1386. os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
  1387. hpsk->next = hapd->conf->ssid.wpa_psk;
  1388. hapd->conf->ssid.wpa_psk = hpsk;
  1389. }
  1390. }
  1391. static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
  1392. {
  1393. unsigned int i;
  1394. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
  1395. wpa_s->p2p_group_common_freqs_num);
  1396. for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
  1397. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
  1398. i, wpa_s->p2p_group_common_freqs[i]);
  1399. }
  1400. static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
  1401. struct p2p_go_neg_results *params)
  1402. {
  1403. unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
  1404. wpa_s->p2p_group_common_freqs_num = 0;
  1405. os_free(wpa_s->p2p_group_common_freqs);
  1406. wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
  1407. if (!wpa_s->p2p_group_common_freqs)
  1408. return;
  1409. for (i = 0; i < len; i++) {
  1410. if (!wpa_s->go_params->freq_list[i])
  1411. break;
  1412. wpa_s->p2p_group_common_freqs[i] =
  1413. wpa_s->go_params->freq_list[i];
  1414. }
  1415. wpa_s->p2p_group_common_freqs_num = i;
  1416. }
  1417. static void p2p_config_write(struct wpa_supplicant *wpa_s)
  1418. {
  1419. #ifndef CONFIG_NO_CONFIG_WRITE
  1420. if (wpa_s->parent->conf->update_config &&
  1421. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  1422. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  1423. #endif /* CONFIG_NO_CONFIG_WRITE */
  1424. }
  1425. static void p2p_go_configured(void *ctx, void *data)
  1426. {
  1427. struct wpa_supplicant *wpa_s = ctx;
  1428. struct p2p_go_neg_results *params = data;
  1429. struct wpa_ssid *ssid;
  1430. int network_id = -1;
  1431. p2p_go_save_group_common_freqs(wpa_s, params);
  1432. p2p_go_dump_common_freqs(wpa_s);
  1433. ssid = wpa_s->current_ssid;
  1434. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  1435. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  1436. if (wpa_s->global->p2p_group_formation == wpa_s)
  1437. wpa_s->global->p2p_group_formation = NULL;
  1438. wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
  1439. params->passphrase[0] == '\0' ?
  1440. params->psk : NULL,
  1441. params->passphrase,
  1442. wpa_s->global->p2p_dev_addr,
  1443. params->persistent_group, "");
  1444. wpa_s->group_formation_reported = 1;
  1445. if (wpa_s->parent->p2ps_join_addr_valid) {
  1446. wpa_dbg(wpa_s, MSG_DEBUG,
  1447. "P2PS: Setting default PIN for " MACSTR,
  1448. MAC2STR(wpa_s->parent->p2ps_join_addr));
  1449. wpa_supplicant_ap_wps_pin(wpa_s,
  1450. wpa_s->parent->p2ps_join_addr,
  1451. "12345670", NULL, 0, 0);
  1452. wpa_s->parent->p2ps_join_addr_valid = 0;
  1453. }
  1454. os_get_reltime(&wpa_s->global->p2p_go_wait_client);
  1455. if (params->persistent_group) {
  1456. network_id = wpas_p2p_store_persistent_group(
  1457. wpa_s->parent, ssid,
  1458. wpa_s->global->p2p_dev_addr);
  1459. wpas_p2p_add_psk_list(wpa_s, ssid);
  1460. }
  1461. if (network_id < 0)
  1462. network_id = ssid->id;
  1463. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  1464. wpas_p2p_cross_connect_setup(wpa_s);
  1465. wpas_p2p_set_group_idle_timeout(wpa_s);
  1466. if (wpa_s->p2p_first_connection_timeout) {
  1467. wpa_dbg(wpa_s, MSG_DEBUG,
  1468. "P2P: Start group formation timeout of %d seconds until first data connection on GO",
  1469. wpa_s->p2p_first_connection_timeout);
  1470. wpa_s->p2p_go_group_formation_completed = 0;
  1471. wpa_s->global->p2p_group_formation = wpa_s;
  1472. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1473. wpa_s->parent, NULL);
  1474. eloop_register_timeout(
  1475. wpa_s->p2p_first_connection_timeout, 0,
  1476. wpas_p2p_group_formation_timeout,
  1477. wpa_s->parent, NULL);
  1478. }
  1479. return;
  1480. }
  1481. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  1482. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  1483. params->peer_interface_addr)) {
  1484. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  1485. "filtering");
  1486. return;
  1487. }
  1488. if (params->wps_method == WPS_PBC) {
  1489. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  1490. params->peer_device_addr);
  1491. #ifdef CONFIG_WPS_NFC
  1492. } else if (params->wps_method == WPS_NFC) {
  1493. if (wpa_s->parent->p2p_oob_dev_pw_id !=
  1494. DEV_PW_NFC_CONNECTION_HANDOVER &&
  1495. !wpa_s->parent->p2p_oob_dev_pw) {
  1496. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  1497. return;
  1498. }
  1499. wpas_ap_wps_add_nfc_pw(
  1500. wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
  1501. wpa_s->parent->p2p_oob_dev_pw,
  1502. wpa_s->parent->p2p_peer_oob_pk_hash_known ?
  1503. wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
  1504. #endif /* CONFIG_WPS_NFC */
  1505. } else if (wpa_s->p2p_pin[0])
  1506. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  1507. wpa_s->p2p_pin, NULL, 0, 0);
  1508. os_free(wpa_s->go_params);
  1509. wpa_s->go_params = NULL;
  1510. }
  1511. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  1512. struct p2p_go_neg_results *params,
  1513. int group_formation)
  1514. {
  1515. struct wpa_ssid *ssid;
  1516. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  1517. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  1518. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  1519. "results");
  1520. return;
  1521. }
  1522. ssid = wpa_config_add_network(wpa_s->conf);
  1523. if (ssid == NULL) {
  1524. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  1525. return;
  1526. }
  1527. wpa_s->show_group_started = 0;
  1528. wpa_s->p2p_go_group_formation_completed = 0;
  1529. wpa_s->group_formation_reported = 0;
  1530. wpa_config_set_network_defaults(ssid);
  1531. ssid->temporary = 1;
  1532. ssid->p2p_group = 1;
  1533. ssid->p2p_persistent_group = params->persistent_group;
  1534. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  1535. WPAS_MODE_P2P_GO;
  1536. ssid->frequency = params->freq;
  1537. ssid->ht40 = params->ht40;
  1538. ssid->vht = params->vht;
  1539. ssid->ssid = os_zalloc(params->ssid_len + 1);
  1540. if (ssid->ssid) {
  1541. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  1542. ssid->ssid_len = params->ssid_len;
  1543. }
  1544. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  1545. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  1546. ssid->proto = WPA_PROTO_RSN;
  1547. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  1548. ssid->group_cipher = WPA_CIPHER_CCMP;
  1549. if (params->freq > 56160) {
  1550. /*
  1551. * Enable GCMP instead of CCMP as pairwise_cipher and
  1552. * group_cipher in 60 GHz.
  1553. */
  1554. ssid->pairwise_cipher = WPA_CIPHER_GCMP;
  1555. ssid->group_cipher = WPA_CIPHER_GCMP;
  1556. }
  1557. if (os_strlen(params->passphrase) > 0) {
  1558. ssid->passphrase = os_strdup(params->passphrase);
  1559. if (ssid->passphrase == NULL) {
  1560. wpa_msg_global(wpa_s, MSG_ERROR,
  1561. "P2P: Failed to copy passphrase for GO");
  1562. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1563. return;
  1564. }
  1565. } else
  1566. ssid->passphrase = NULL;
  1567. ssid->psk_set = params->psk_set;
  1568. if (ssid->psk_set)
  1569. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  1570. else if (ssid->passphrase)
  1571. wpa_config_update_psk(ssid);
  1572. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  1573. wpa_s->ap_configured_cb = p2p_go_configured;
  1574. wpa_s->ap_configured_cb_ctx = wpa_s;
  1575. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  1576. wpa_s->scan_req = NORMAL_SCAN_REQ;
  1577. wpa_s->connect_without_scan = ssid;
  1578. wpa_s->reassociate = 1;
  1579. wpa_s->disconnected = 0;
  1580. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  1581. "start GO)");
  1582. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1583. }
  1584. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  1585. const struct wpa_supplicant *src)
  1586. {
  1587. struct wpa_config *d;
  1588. const struct wpa_config *s;
  1589. d = dst->conf;
  1590. s = src->conf;
  1591. #define C(n) if (s->n) d->n = os_strdup(s->n)
  1592. C(device_name);
  1593. C(manufacturer);
  1594. C(model_name);
  1595. C(model_number);
  1596. C(serial_number);
  1597. C(config_methods);
  1598. #undef C
  1599. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  1600. os_memcpy(d->sec_device_type, s->sec_device_type,
  1601. sizeof(d->sec_device_type));
  1602. d->num_sec_device_types = s->num_sec_device_types;
  1603. d->p2p_group_idle = s->p2p_group_idle;
  1604. d->p2p_intra_bss = s->p2p_intra_bss;
  1605. d->persistent_reconnect = s->persistent_reconnect;
  1606. d->max_num_sta = s->max_num_sta;
  1607. d->pbc_in_m1 = s->pbc_in_m1;
  1608. d->ignore_old_scan_res = s->ignore_old_scan_res;
  1609. d->beacon_int = s->beacon_int;
  1610. d->dtim_period = s->dtim_period;
  1611. d->p2p_go_ctwindow = s->p2p_go_ctwindow;
  1612. d->disassoc_low_ack = s->disassoc_low_ack;
  1613. d->disable_scan_offload = s->disable_scan_offload;
  1614. d->passive_scan = s->passive_scan;
  1615. if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
  1616. d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
  1617. d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
  1618. }
  1619. }
  1620. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  1621. char *ifname, size_t len)
  1622. {
  1623. char *ifname_ptr = wpa_s->ifname;
  1624. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  1625. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  1626. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  1627. }
  1628. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  1629. if (os_strlen(ifname) >= IFNAMSIZ &&
  1630. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  1631. int res;
  1632. /* Try to avoid going over the IFNAMSIZ length limit */
  1633. res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
  1634. if (os_snprintf_error(len, res) && len)
  1635. ifname[len - 1] = '\0';
  1636. }
  1637. }
  1638. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  1639. enum wpa_driver_if_type type)
  1640. {
  1641. char ifname[120], force_ifname[120];
  1642. if (wpa_s->pending_interface_name[0]) {
  1643. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  1644. "- skip creation of a new one");
  1645. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  1646. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  1647. "unknown?! ifname='%s'",
  1648. wpa_s->pending_interface_name);
  1649. return -1;
  1650. }
  1651. return 0;
  1652. }
  1653. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  1654. force_ifname[0] = '\0';
  1655. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  1656. ifname);
  1657. wpa_s->p2p_group_idx++;
  1658. wpa_s->pending_interface_type = type;
  1659. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  1660. wpa_s->pending_interface_addr, NULL) < 0) {
  1661. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  1662. "interface");
  1663. return -1;
  1664. }
  1665. if (force_ifname[0]) {
  1666. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  1667. force_ifname);
  1668. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  1669. sizeof(wpa_s->pending_interface_name));
  1670. } else
  1671. os_strlcpy(wpa_s->pending_interface_name, ifname,
  1672. sizeof(wpa_s->pending_interface_name));
  1673. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  1674. MACSTR, wpa_s->pending_interface_name,
  1675. MAC2STR(wpa_s->pending_interface_addr));
  1676. return 0;
  1677. }
  1678. static void wpas_p2p_remove_pending_group_interface(
  1679. struct wpa_supplicant *wpa_s)
  1680. {
  1681. if (!wpa_s->pending_interface_name[0] ||
  1682. is_zero_ether_addr(wpa_s->pending_interface_addr))
  1683. return; /* No pending virtual interface */
  1684. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  1685. wpa_s->pending_interface_name);
  1686. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  1687. wpa_s->pending_interface_name);
  1688. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1689. wpa_s->pending_interface_name[0] = '\0';
  1690. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1691. }
  1692. static struct wpa_supplicant *
  1693. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  1694. {
  1695. struct wpa_interface iface;
  1696. struct wpa_supplicant *group_wpa_s;
  1697. if (!wpa_s->pending_interface_name[0]) {
  1698. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  1699. if (!wpas_p2p_create_iface(wpa_s))
  1700. return NULL;
  1701. /*
  1702. * Something has forced us to remove the pending interface; try
  1703. * to create a new one and hope for the best that we will get
  1704. * the same local address.
  1705. */
  1706. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  1707. WPA_IF_P2P_CLIENT) < 0)
  1708. return NULL;
  1709. }
  1710. os_memset(&iface, 0, sizeof(iface));
  1711. iface.ifname = wpa_s->pending_interface_name;
  1712. iface.driver = wpa_s->driver->name;
  1713. if (wpa_s->conf->ctrl_interface == NULL &&
  1714. wpa_s->parent != wpa_s &&
  1715. wpa_s->p2p_mgmt &&
  1716. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1717. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1718. else
  1719. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1720. iface.driver_param = wpa_s->conf->driver_param;
  1721. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  1722. if (group_wpa_s == NULL) {
  1723. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1724. "wpa_supplicant interface");
  1725. return NULL;
  1726. }
  1727. wpa_s->pending_interface_name[0] = '\0';
  1728. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1729. P2P_GROUP_INTERFACE_CLIENT;
  1730. wpa_s->global->p2p_group_formation = group_wpa_s;
  1731. wpa_s->global->pending_group_iface_for_p2ps = 0;
  1732. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1733. return group_wpa_s;
  1734. }
  1735. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1736. void *timeout_ctx)
  1737. {
  1738. struct wpa_supplicant *wpa_s = eloop_ctx;
  1739. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1740. wpas_p2p_group_formation_failed(wpa_s);
  1741. }
  1742. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1743. {
  1744. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1745. wpa_s->parent, NULL);
  1746. if (wpa_s->global->p2p)
  1747. p2p_group_formation_failed(wpa_s->global->p2p);
  1748. wpas_group_formation_completed(wpa_s, 0);
  1749. }
  1750. static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
  1751. {
  1752. wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
  1753. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1754. wpa_s->parent, NULL);
  1755. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1756. wpa_s->parent, NULL);
  1757. wpa_s->global->p2p_fail_on_wps_complete = 0;
  1758. }
  1759. void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
  1760. {
  1761. if (wpa_s->global->p2p_group_formation != wpa_s)
  1762. return;
  1763. /* Speed up group formation timeout since this cannot succeed */
  1764. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1765. wpa_s->parent, NULL);
  1766. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  1767. wpa_s->parent, NULL);
  1768. }
  1769. static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1770. {
  1771. struct wpa_supplicant *wpa_s = ctx;
  1772. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1773. wpa_drv_cancel_remain_on_channel(wpa_s);
  1774. wpa_s->off_channel_freq = 0;
  1775. wpa_s->roc_waiting_drv_freq = 0;
  1776. }
  1777. if (res->status) {
  1778. wpa_msg_global(wpa_s, MSG_INFO,
  1779. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1780. res->status);
  1781. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1782. wpas_p2p_remove_pending_group_interface(wpa_s);
  1783. return;
  1784. }
  1785. if (wpa_s->p2p_go_ht40)
  1786. res->ht40 = 1;
  1787. if (wpa_s->p2p_go_vht)
  1788. res->vht = 1;
  1789. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1790. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1791. " wps_method=%s",
  1792. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1793. MAC2STR(res->peer_device_addr),
  1794. MAC2STR(res->peer_interface_addr),
  1795. p2p_wps_method_text(res->wps_method));
  1796. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1797. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1798. struct wpa_ssid *ssid;
  1799. ssid = wpa_config_get_network(wpa_s->conf,
  1800. wpa_s->p2p_persistent_id);
  1801. if (ssid && ssid->disabled == 2 &&
  1802. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1803. size_t len = os_strlen(ssid->passphrase);
  1804. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1805. "on requested persistent group");
  1806. os_memcpy(res->passphrase, ssid->passphrase, len);
  1807. res->passphrase[len] = '\0';
  1808. }
  1809. }
  1810. if (wpa_s->create_p2p_iface) {
  1811. struct wpa_supplicant *group_wpa_s =
  1812. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1813. if (group_wpa_s == NULL) {
  1814. wpas_p2p_remove_pending_group_interface(wpa_s);
  1815. eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
  1816. wpa_s, NULL);
  1817. wpas_p2p_group_formation_failed(wpa_s);
  1818. return;
  1819. }
  1820. if (group_wpa_s != wpa_s) {
  1821. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1822. sizeof(group_wpa_s->p2p_pin));
  1823. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1824. }
  1825. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1826. wpa_s->pending_interface_name[0] = '\0';
  1827. group_wpa_s->p2p_in_provisioning = 1;
  1828. if (res->role_go)
  1829. wpas_start_wps_go(group_wpa_s, res, 1);
  1830. else
  1831. wpas_start_wps_enrollee(group_wpa_s, res);
  1832. } else {
  1833. wpa_s->p2p_in_provisioning = 1;
  1834. wpa_s->global->p2p_group_formation = wpa_s;
  1835. if (res->role_go)
  1836. wpas_start_wps_go(wpa_s, res, 1);
  1837. else
  1838. wpas_start_wps_enrollee(ctx, res);
  1839. }
  1840. wpa_s->p2p_long_listen = 0;
  1841. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1842. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1843. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1844. (res->peer_config_timeout % 100) * 10000,
  1845. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1846. }
  1847. static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1848. {
  1849. struct wpa_supplicant *wpa_s = ctx;
  1850. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1851. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1852. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1853. }
  1854. static void wpas_dev_found(void *ctx, const u8 *addr,
  1855. const struct p2p_peer_info *info,
  1856. int new_device)
  1857. {
  1858. #ifndef CONFIG_NO_STDOUT_DEBUG
  1859. struct wpa_supplicant *wpa_s = ctx;
  1860. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1861. char *wfd_dev_info_hex = NULL;
  1862. #ifdef CONFIG_WIFI_DISPLAY
  1863. wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
  1864. WFD_SUBELEM_DEVICE_INFO);
  1865. #endif /* CONFIG_WIFI_DISPLAY */
  1866. if (info->p2ps_instance) {
  1867. char str[256];
  1868. const u8 *buf = wpabuf_head(info->p2ps_instance);
  1869. size_t len = wpabuf_len(info->p2ps_instance);
  1870. while (len) {
  1871. u32 id;
  1872. u16 methods;
  1873. u8 str_len;
  1874. if (len < 4 + 2 + 1)
  1875. break;
  1876. id = WPA_GET_LE32(buf);
  1877. buf += sizeof(u32);
  1878. methods = WPA_GET_BE16(buf);
  1879. buf += sizeof(u16);
  1880. str_len = *buf++;
  1881. if (str_len > len - 4 - 2 - 1)
  1882. break;
  1883. os_memcpy(str, buf, str_len);
  1884. str[str_len] = '\0';
  1885. buf += str_len;
  1886. len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
  1887. wpa_msg_global(wpa_s, MSG_INFO,
  1888. P2P_EVENT_DEVICE_FOUND MACSTR
  1889. " p2p_dev_addr=" MACSTR
  1890. " pri_dev_type=%s name='%s'"
  1891. " config_methods=0x%x"
  1892. " dev_capab=0x%x"
  1893. " group_capab=0x%x"
  1894. " adv_id=%x asp_svc=%s%s",
  1895. MAC2STR(addr),
  1896. MAC2STR(info->p2p_device_addr),
  1897. wps_dev_type_bin2str(
  1898. info->pri_dev_type,
  1899. devtype, sizeof(devtype)),
  1900. info->device_name, methods,
  1901. info->dev_capab, info->group_capab,
  1902. id, str,
  1903. info->vendor_elems ?
  1904. " vendor_elems=1" : "");
  1905. }
  1906. goto done;
  1907. }
  1908. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1909. " p2p_dev_addr=" MACSTR
  1910. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1911. "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
  1912. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1913. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1914. sizeof(devtype)),
  1915. info->device_name, info->config_methods,
  1916. info->dev_capab, info->group_capab,
  1917. wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
  1918. wfd_dev_info_hex ? wfd_dev_info_hex : "",
  1919. info->vendor_elems ? " vendor_elems=1" : "",
  1920. new_device);
  1921. done:
  1922. os_free(wfd_dev_info_hex);
  1923. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1924. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1925. }
  1926. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1927. {
  1928. struct wpa_supplicant *wpa_s = ctx;
  1929. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1930. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1931. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1932. }
  1933. static void wpas_find_stopped(void *ctx)
  1934. {
  1935. struct wpa_supplicant *wpa_s = ctx;
  1936. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1937. wpas_notify_p2p_find_stopped(wpa_s);
  1938. }
  1939. struct wpas_p2p_listen_work {
  1940. unsigned int freq;
  1941. unsigned int duration;
  1942. struct wpabuf *probe_resp_ie;
  1943. };
  1944. static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
  1945. {
  1946. if (lwork == NULL)
  1947. return;
  1948. wpabuf_free(lwork->probe_resp_ie);
  1949. os_free(lwork);
  1950. }
  1951. static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
  1952. {
  1953. struct wpas_p2p_listen_work *lwork;
  1954. if (!wpa_s->p2p_listen_work)
  1955. return;
  1956. lwork = wpa_s->p2p_listen_work->ctx;
  1957. wpas_p2p_listen_work_free(lwork);
  1958. radio_work_done(wpa_s->p2p_listen_work);
  1959. wpa_s->p2p_listen_work = NULL;
  1960. }
  1961. static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
  1962. {
  1963. struct wpa_supplicant *wpa_s = work->wpa_s;
  1964. struct wpas_p2p_listen_work *lwork = work->ctx;
  1965. unsigned int duration;
  1966. if (deinit) {
  1967. if (work->started) {
  1968. wpa_s->p2p_listen_work = NULL;
  1969. wpas_stop_listen(wpa_s);
  1970. }
  1971. wpas_p2p_listen_work_free(lwork);
  1972. return;
  1973. }
  1974. wpa_s->p2p_listen_work = work;
  1975. wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
  1976. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1977. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1978. "report received Probe Request frames");
  1979. wpas_p2p_listen_work_done(wpa_s);
  1980. return;
  1981. }
  1982. wpa_s->pending_listen_freq = lwork->freq;
  1983. wpa_s->pending_listen_duration = lwork->duration;
  1984. duration = lwork->duration;
  1985. #ifdef CONFIG_TESTING_OPTIONS
  1986. if (wpa_s->extra_roc_dur) {
  1987. wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
  1988. duration, duration + wpa_s->extra_roc_dur);
  1989. duration += wpa_s->extra_roc_dur;
  1990. }
  1991. #endif /* CONFIG_TESTING_OPTIONS */
  1992. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
  1993. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1994. "to remain on channel (%u MHz) for Listen "
  1995. "state", lwork->freq);
  1996. wpas_p2p_listen_work_done(wpa_s);
  1997. wpa_s->pending_listen_freq = 0;
  1998. return;
  1999. }
  2000. wpa_s->off_channel_freq = 0;
  2001. wpa_s->roc_waiting_drv_freq = lwork->freq;
  2002. }
  2003. static int wpas_start_listen(void *ctx, unsigned int freq,
  2004. unsigned int duration,
  2005. const struct wpabuf *probe_resp_ie)
  2006. {
  2007. struct wpa_supplicant *wpa_s = ctx;
  2008. struct wpas_p2p_listen_work *lwork;
  2009. if (wpa_s->p2p_listen_work) {
  2010. wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
  2011. return -1;
  2012. }
  2013. lwork = os_zalloc(sizeof(*lwork));
  2014. if (lwork == NULL)
  2015. return -1;
  2016. lwork->freq = freq;
  2017. lwork->duration = duration;
  2018. if (probe_resp_ie) {
  2019. lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
  2020. if (lwork->probe_resp_ie == NULL) {
  2021. wpas_p2p_listen_work_free(lwork);
  2022. return -1;
  2023. }
  2024. }
  2025. if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
  2026. lwork) < 0) {
  2027. wpas_p2p_listen_work_free(lwork);
  2028. return -1;
  2029. }
  2030. return 0;
  2031. }
  2032. static void wpas_stop_listen(void *ctx)
  2033. {
  2034. struct wpa_supplicant *wpa_s = ctx;
  2035. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  2036. wpa_drv_cancel_remain_on_channel(wpa_s);
  2037. wpa_s->off_channel_freq = 0;
  2038. wpa_s->roc_waiting_drv_freq = 0;
  2039. }
  2040. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  2041. wpa_drv_probe_req_report(wpa_s, 0);
  2042. wpas_p2p_listen_work_done(wpa_s);
  2043. }
  2044. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  2045. {
  2046. struct wpa_supplicant *wpa_s = ctx;
  2047. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  2048. }
  2049. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  2050. const u8 *peer, const char *params,
  2051. unsigned int generated_pin)
  2052. {
  2053. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  2054. " %08d%s", MAC2STR(peer), generated_pin, params);
  2055. }
  2056. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  2057. const u8 *peer, const char *params)
  2058. {
  2059. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  2060. "%s", MAC2STR(peer), params);
  2061. }
  2062. static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  2063. const u8 *dev_addr, const u8 *pri_dev_type,
  2064. const char *dev_name, u16 supp_config_methods,
  2065. u8 dev_capab, u8 group_capab, const u8 *group_id,
  2066. size_t group_id_len)
  2067. {
  2068. struct wpa_supplicant *wpa_s = ctx;
  2069. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2070. char params[300];
  2071. u8 empty_dev_type[8];
  2072. unsigned int generated_pin = 0;
  2073. struct wpa_supplicant *group = NULL;
  2074. int res;
  2075. if (group_id) {
  2076. for (group = wpa_s->global->ifaces; group; group = group->next)
  2077. {
  2078. struct wpa_ssid *s = group->current_ssid;
  2079. if (s != NULL &&
  2080. s->mode == WPAS_MODE_P2P_GO &&
  2081. group_id_len - ETH_ALEN == s->ssid_len &&
  2082. os_memcmp(group_id + ETH_ALEN, s->ssid,
  2083. s->ssid_len) == 0)
  2084. break;
  2085. }
  2086. }
  2087. if (pri_dev_type == NULL) {
  2088. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  2089. pri_dev_type = empty_dev_type;
  2090. }
  2091. res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  2092. " pri_dev_type=%s name='%s' config_methods=0x%x "
  2093. "dev_capab=0x%x group_capab=0x%x%s%s",
  2094. MAC2STR(dev_addr),
  2095. wps_dev_type_bin2str(pri_dev_type, devtype,
  2096. sizeof(devtype)),
  2097. dev_name, supp_config_methods, dev_capab, group_capab,
  2098. group ? " group=" : "",
  2099. group ? group->ifname : "");
  2100. if (os_snprintf_error(sizeof(params), res))
  2101. wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
  2102. params[sizeof(params) - 1] = '\0';
  2103. if (config_methods & WPS_CONFIG_DISPLAY) {
  2104. generated_pin = wps_generate_pin();
  2105. wpas_prov_disc_local_display(wpa_s, peer, params,
  2106. generated_pin);
  2107. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2108. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2109. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2110. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2111. MACSTR "%s", MAC2STR(peer), params);
  2112. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2113. P2P_PROV_DISC_SUCCESS,
  2114. config_methods, generated_pin);
  2115. }
  2116. static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2117. {
  2118. struct wpa_supplicant *wpa_s = ctx;
  2119. unsigned int generated_pin = 0;
  2120. char params[20];
  2121. if (wpa_s->pending_pd_before_join &&
  2122. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2123. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2124. wpa_s->pending_pd_before_join = 0;
  2125. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2126. "join-existing-group operation");
  2127. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2128. return;
  2129. }
  2130. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2131. wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
  2132. int res;
  2133. res = os_snprintf(params, sizeof(params), " peer_go=%d",
  2134. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2135. if (os_snprintf_error(sizeof(params), res))
  2136. params[sizeof(params) - 1] = '\0';
  2137. } else
  2138. params[0] = '\0';
  2139. if (config_methods & WPS_CONFIG_DISPLAY)
  2140. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2141. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2142. generated_pin = wps_generate_pin();
  2143. wpas_prov_disc_local_display(wpa_s, peer, params,
  2144. generated_pin);
  2145. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2146. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2147. MACSTR "%s", MAC2STR(peer), params);
  2148. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2149. P2P_PROV_DISC_SUCCESS,
  2150. config_methods, generated_pin);
  2151. }
  2152. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2153. enum p2p_prov_disc_status status,
  2154. u32 adv_id, const u8 *adv_mac,
  2155. const char *deferred_session_resp)
  2156. {
  2157. struct wpa_supplicant *wpa_s = ctx;
  2158. if (wpa_s->p2p_fallback_to_go_neg) {
  2159. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2160. "failed - fall back to GO Negotiation");
  2161. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2162. P2P_EVENT_FALLBACK_TO_GO_NEG
  2163. "reason=PD-failed");
  2164. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2165. return;
  2166. }
  2167. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2168. wpa_s->pending_pd_before_join = 0;
  2169. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2170. "join-existing-group operation (no ACK for PD "
  2171. "Req attempts)");
  2172. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  2173. return;
  2174. }
  2175. if (adv_id && adv_mac && deferred_session_resp) {
  2176. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2177. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
  2178. " deferred_session_resp='%s'",
  2179. MAC2STR(peer), status, adv_id,
  2180. deferred_session_resp);
  2181. } else if (adv_id && adv_mac) {
  2182. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2183. " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
  2184. MAC2STR(peer), status, adv_id);
  2185. } else {
  2186. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2187. " p2p_dev_addr=" MACSTR " status=%d",
  2188. MAC2STR(peer), status);
  2189. }
  2190. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2191. status, 0, 0);
  2192. }
  2193. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2194. {
  2195. if (channels == NULL)
  2196. return 1; /* Assume no restrictions */
  2197. return p2p_channels_includes_freq(channels, freq);
  2198. }
  2199. /**
  2200. * Pick the best frequency to use from all the currently used frequencies.
  2201. */
  2202. static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
  2203. struct wpa_used_freq_data *freqs,
  2204. unsigned int num)
  2205. {
  2206. unsigned int i, c;
  2207. /* find a candidate freq that is supported by P2P */
  2208. for (c = 0; c < num; c++)
  2209. if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
  2210. break;
  2211. if (c == num)
  2212. return 0;
  2213. /* once we have a candidate, try to find a 'better' one */
  2214. for (i = c + 1; i < num; i++) {
  2215. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
  2216. continue;
  2217. /*
  2218. * 1. Infrastructure station interfaces have higher preference.
  2219. * 2. P2P Clients have higher preference.
  2220. * 3. All others.
  2221. */
  2222. if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
  2223. c = i;
  2224. break;
  2225. }
  2226. if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
  2227. c = i;
  2228. }
  2229. return freqs[c].freq;
  2230. }
  2231. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2232. const u8 *go_dev_addr, const u8 *ssid,
  2233. size_t ssid_len, int *go, u8 *group_bssid,
  2234. int *force_freq, int persistent_group,
  2235. const struct p2p_channels *channels,
  2236. int dev_pw_id)
  2237. {
  2238. struct wpa_supplicant *wpa_s = ctx;
  2239. struct wpa_ssid *s;
  2240. struct wpa_used_freq_data *freqs;
  2241. struct wpa_supplicant *grp;
  2242. int best_freq;
  2243. if (!persistent_group) {
  2244. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2245. " to join an active group (SSID: %s)",
  2246. MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
  2247. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2248. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2249. == 0 ||
  2250. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2251. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2252. "authorized invitation");
  2253. goto accept_inv;
  2254. }
  2255. #ifdef CONFIG_WPS_NFC
  2256. if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
  2257. dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
  2258. wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
  2259. wpa_s->p2p_wps_method = WPS_NFC;
  2260. wpa_s->pending_join_wps_method = WPS_NFC;
  2261. os_memcpy(wpa_s->pending_join_dev_addr,
  2262. go_dev_addr, ETH_ALEN);
  2263. os_memcpy(wpa_s->pending_join_iface_addr,
  2264. bssid, ETH_ALEN);
  2265. goto accept_inv;
  2266. }
  2267. #endif /* CONFIG_WPS_NFC */
  2268. /*
  2269. * Do not accept the invitation automatically; notify user and
  2270. * request approval.
  2271. */
  2272. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2273. }
  2274. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2275. if (grp) {
  2276. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2277. "running persistent group");
  2278. if (*go)
  2279. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2280. goto accept_inv;
  2281. }
  2282. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2283. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2284. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2285. "invitation to re-invoke a persistent group");
  2286. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2287. } else if (!wpa_s->conf->persistent_reconnect)
  2288. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2289. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2290. if (s->disabled == 2 &&
  2291. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2292. s->ssid_len == ssid_len &&
  2293. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2294. break;
  2295. }
  2296. if (!s) {
  2297. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2298. " requested reinvocation of an unknown group",
  2299. MAC2STR(sa));
  2300. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2301. }
  2302. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2303. *go = 1;
  2304. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2305. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2306. "interface is already in use - reject "
  2307. "invitation");
  2308. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2309. }
  2310. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2311. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2312. *go = 1;
  2313. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2314. {
  2315. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2316. "interface address for the group");
  2317. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2318. }
  2319. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2320. ETH_ALEN);
  2321. }
  2322. accept_inv:
  2323. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2324. best_freq = 0;
  2325. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  2326. sizeof(struct wpa_used_freq_data));
  2327. if (freqs) {
  2328. int num_channels = wpa_s->num_multichan_concurrent;
  2329. int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
  2330. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  2331. os_free(freqs);
  2332. }
  2333. /* Get one of the frequencies currently in use */
  2334. if (best_freq > 0) {
  2335. wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
  2336. wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
  2337. if (wpa_s->num_multichan_concurrent < 2 ||
  2338. wpas_p2p_num_unused_channels(wpa_s) < 1) {
  2339. wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
  2340. *force_freq = best_freq;
  2341. }
  2342. }
  2343. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2344. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2345. if (*go == 0) {
  2346. /* We are the client */
  2347. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2348. "running a GO but we are capable of MCC, "
  2349. "figure out the best channel to use");
  2350. *force_freq = 0;
  2351. } else if (!freq_included(channels, *force_freq)) {
  2352. /* We are the GO, and *force_freq is not in the
  2353. * intersection */
  2354. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2355. "in intersection but we are capable of MCC, "
  2356. "figure out the best channel to use",
  2357. *force_freq);
  2358. *force_freq = 0;
  2359. }
  2360. }
  2361. return P2P_SC_SUCCESS;
  2362. }
  2363. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2364. const u8 *ssid, size_t ssid_len,
  2365. const u8 *go_dev_addr, u8 status,
  2366. int op_freq)
  2367. {
  2368. struct wpa_supplicant *wpa_s = ctx;
  2369. struct wpa_ssid *s;
  2370. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2371. if (s->disabled == 2 &&
  2372. s->ssid_len == ssid_len &&
  2373. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2374. break;
  2375. }
  2376. if (status == P2P_SC_SUCCESS) {
  2377. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2378. " was accepted; op_freq=%d MHz, SSID=%s",
  2379. MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
  2380. if (s) {
  2381. int go = s->mode == WPAS_MODE_P2P_GO;
  2382. wpas_p2p_group_add_persistent(
  2383. wpa_s, s, go, 0, op_freq, 0, 0, NULL,
  2384. go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
  2385. } else if (bssid) {
  2386. wpa_s->user_initiated_pd = 0;
  2387. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2388. wpa_s->p2p_wps_method, 0, op_freq,
  2389. ssid, ssid_len);
  2390. }
  2391. return;
  2392. }
  2393. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2394. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2395. " was rejected (status %u)", MAC2STR(sa), status);
  2396. return;
  2397. }
  2398. if (!s) {
  2399. if (bssid) {
  2400. wpa_msg_global(wpa_s, MSG_INFO,
  2401. P2P_EVENT_INVITATION_RECEIVED
  2402. "sa=" MACSTR " go_dev_addr=" MACSTR
  2403. " bssid=" MACSTR " unknown-network",
  2404. MAC2STR(sa), MAC2STR(go_dev_addr),
  2405. MAC2STR(bssid));
  2406. } else {
  2407. wpa_msg_global(wpa_s, MSG_INFO,
  2408. P2P_EVENT_INVITATION_RECEIVED
  2409. "sa=" MACSTR " go_dev_addr=" MACSTR
  2410. " unknown-network",
  2411. MAC2STR(sa), MAC2STR(go_dev_addr));
  2412. }
  2413. return;
  2414. }
  2415. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2416. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2417. "sa=" MACSTR " persistent=%d freq=%d",
  2418. MAC2STR(sa), s->id, op_freq);
  2419. } else {
  2420. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2421. "sa=" MACSTR " persistent=%d",
  2422. MAC2STR(sa), s->id);
  2423. }
  2424. }
  2425. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2426. struct wpa_ssid *ssid,
  2427. const u8 *peer, int inv)
  2428. {
  2429. size_t i;
  2430. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  2431. if (ssid == NULL)
  2432. return;
  2433. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2434. if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
  2435. ETH_ALEN) == 0)
  2436. break;
  2437. }
  2438. if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
  2439. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2440. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2441. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2442. "due to invitation result", ssid->id);
  2443. wpas_notify_network_removed(wpa_s, ssid);
  2444. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2445. return;
  2446. }
  2447. return; /* Peer not found in client list */
  2448. }
  2449. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2450. "group %d client list%s",
  2451. MAC2STR(peer), ssid->id,
  2452. inv ? " due to invitation result" : "");
  2453. os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
  2454. ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  2455. (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  2456. ssid->num_p2p_clients--;
  2457. if (p2p_wpa_s->conf->update_config &&
  2458. wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
  2459. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2460. }
  2461. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2462. const u8 *peer)
  2463. {
  2464. struct wpa_ssid *ssid;
  2465. wpa_s = wpa_s->global->p2p_invite_group;
  2466. if (wpa_s == NULL)
  2467. return; /* No known invitation group */
  2468. ssid = wpa_s->current_ssid;
  2469. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2470. !ssid->p2p_persistent_group)
  2471. return; /* Not operating as a GO in persistent group */
  2472. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2473. ssid->ssid, ssid->ssid_len);
  2474. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2475. }
  2476. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2477. const struct p2p_channels *channels,
  2478. const u8 *peer, int neg_freq,
  2479. int peer_oper_freq)
  2480. {
  2481. struct wpa_supplicant *wpa_s = ctx;
  2482. struct wpa_ssid *ssid;
  2483. int freq;
  2484. if (bssid) {
  2485. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2486. "status=%d " MACSTR,
  2487. status, MAC2STR(bssid));
  2488. } else {
  2489. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2490. "status=%d ", status);
  2491. }
  2492. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2493. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2494. status, MAC2STR(peer));
  2495. if (wpa_s->pending_invite_ssid_id == -1) {
  2496. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2497. wpas_remove_persistent_client(wpa_s, peer);
  2498. return; /* Invitation to active group */
  2499. }
  2500. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2501. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2502. "invitation exchange to indicate readiness for "
  2503. "re-invocation");
  2504. }
  2505. if (status != P2P_SC_SUCCESS) {
  2506. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2507. ssid = wpa_config_get_network(
  2508. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2509. wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
  2510. }
  2511. wpas_p2p_remove_pending_group_interface(wpa_s);
  2512. return;
  2513. }
  2514. ssid = wpa_config_get_network(wpa_s->conf,
  2515. wpa_s->pending_invite_ssid_id);
  2516. if (ssid == NULL) {
  2517. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2518. "data matching with invitation");
  2519. return;
  2520. }
  2521. /*
  2522. * The peer could have missed our ctrl::ack frame for Invitation
  2523. * Response and continue retransmitting the frame. To reduce the
  2524. * likelihood of the peer not getting successful TX status for the
  2525. * Invitation Response frame, wait a short time here before starting
  2526. * the persistent group so that we will remain on the current channel to
  2527. * acknowledge any possible retransmission from the peer.
  2528. */
  2529. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2530. "starting persistent group");
  2531. os_sleep(0, 50000);
  2532. if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
  2533. freq_included(channels, neg_freq))
  2534. freq = neg_freq;
  2535. else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
  2536. freq_included(channels, peer_oper_freq))
  2537. freq = peer_oper_freq;
  2538. else
  2539. freq = 0;
  2540. wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
  2541. freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2542. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2543. ssid->mode == WPAS_MODE_P2P_GO,
  2544. wpa_s->p2p_persistent_go_freq,
  2545. freq,
  2546. wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
  2547. channels,
  2548. ssid->mode == WPAS_MODE_P2P_GO ?
  2549. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  2550. 0);
  2551. }
  2552. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2553. unsigned int freq)
  2554. {
  2555. if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
  2556. return 1;
  2557. return freq_range_list_includes(&global->p2p_disallow_freq, freq);
  2558. }
  2559. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2560. {
  2561. reg->channel[reg->channels] = chan;
  2562. reg->channels++;
  2563. }
  2564. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2565. struct p2p_channels *chan,
  2566. struct p2p_channels *cli_chan)
  2567. {
  2568. int i, cla = 0;
  2569. wpa_s->global->p2p_24ghz_social_channels = 1;
  2570. os_memset(cli_chan, 0, sizeof(*cli_chan));
  2571. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2572. "band");
  2573. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2574. chan->reg_class[cla].reg_class = 81;
  2575. chan->reg_class[cla].channels = 0;
  2576. for (i = 0; i < 11; i++) {
  2577. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2578. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2579. }
  2580. if (chan->reg_class[cla].channels)
  2581. cla++;
  2582. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2583. "band");
  2584. /* Operating class 115 - 5 GHz, channels 36-48 */
  2585. chan->reg_class[cla].reg_class = 115;
  2586. chan->reg_class[cla].channels = 0;
  2587. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2588. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2589. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2590. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2591. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2592. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2593. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2594. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2595. if (chan->reg_class[cla].channels)
  2596. cla++;
  2597. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2598. "band");
  2599. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2600. chan->reg_class[cla].reg_class = 124;
  2601. chan->reg_class[cla].channels = 0;
  2602. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2603. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2604. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2605. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2606. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2607. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2608. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2609. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2610. if (chan->reg_class[cla].channels)
  2611. cla++;
  2612. chan->reg_classes = cla;
  2613. return 0;
  2614. }
  2615. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2616. u16 num_modes,
  2617. enum hostapd_hw_mode mode)
  2618. {
  2619. u16 i;
  2620. for (i = 0; i < num_modes; i++) {
  2621. if (modes[i].mode == mode)
  2622. return &modes[i];
  2623. }
  2624. return NULL;
  2625. }
  2626. enum chan_allowed {
  2627. NOT_ALLOWED, NO_IR, ALLOWED
  2628. };
  2629. static int has_channel(struct wpa_global *global,
  2630. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2631. {
  2632. int i;
  2633. unsigned int freq;
  2634. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2635. chan * 5;
  2636. if (wpas_p2p_disallowed_freq(global, freq))
  2637. return NOT_ALLOWED;
  2638. for (i = 0; i < mode->num_channels; i++) {
  2639. if (mode->channels[i].chan == chan) {
  2640. if (flags)
  2641. *flags = mode->channels[i].flag;
  2642. if (mode->channels[i].flag &
  2643. (HOSTAPD_CHAN_DISABLED |
  2644. HOSTAPD_CHAN_RADAR))
  2645. return NOT_ALLOWED;
  2646. if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
  2647. return NO_IR;
  2648. return ALLOWED;
  2649. }
  2650. }
  2651. return NOT_ALLOWED;
  2652. }
  2653. struct p2p_oper_class_map {
  2654. enum hostapd_hw_mode mode;
  2655. u8 op_class;
  2656. u8 min_chan;
  2657. u8 max_chan;
  2658. u8 inc;
  2659. enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
  2660. };
  2661. static const struct p2p_oper_class_map op_class[] = {
  2662. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2663. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2664. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2665. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2666. #endif
  2667. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2668. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2669. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2670. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2671. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2672. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2673. /*
  2674. * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
  2675. * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
  2676. * 80 MHz, but currently use the following definition for simplicity
  2677. * (these center frequencies are not actual channels, which makes
  2678. * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
  2679. * removing invalid channels.
  2680. */
  2681. { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
  2682. { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
  2683. { -1, 0, 0, 0, 0, BW20 }
  2684. };
  2685. static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
  2686. struct hostapd_hw_modes *mode,
  2687. u8 channel)
  2688. {
  2689. u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
  2690. unsigned int i;
  2691. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  2692. return 0;
  2693. for (i = 0; i < ARRAY_SIZE(center_channels); i++)
  2694. /*
  2695. * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
  2696. * so the center channel is 6 channels away from the start/end.
  2697. */
  2698. if (channel >= center_channels[i] - 6 &&
  2699. channel <= center_channels[i] + 6)
  2700. return center_channels[i];
  2701. return 0;
  2702. }
  2703. static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
  2704. struct hostapd_hw_modes *mode,
  2705. u8 channel, u8 bw)
  2706. {
  2707. u8 center_chan;
  2708. int i, flags;
  2709. enum chan_allowed res, ret = ALLOWED;
  2710. center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2711. if (!center_chan)
  2712. return NOT_ALLOWED;
  2713. if (center_chan >= 58 && center_chan <= 138)
  2714. return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
  2715. /* check all the channels are available */
  2716. for (i = 0; i < 4; i++) {
  2717. int adj_chan = center_chan - 6 + i * 4;
  2718. res = has_channel(wpa_s->global, mode, adj_chan, &flags);
  2719. if (res == NOT_ALLOWED)
  2720. return NOT_ALLOWED;
  2721. if (res == NO_IR)
  2722. ret = NO_IR;
  2723. if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
  2724. return NOT_ALLOWED;
  2725. if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
  2726. return NOT_ALLOWED;
  2727. if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
  2728. return NOT_ALLOWED;
  2729. if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
  2730. return NOT_ALLOWED;
  2731. }
  2732. return ret;
  2733. }
  2734. static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2735. struct hostapd_hw_modes *mode,
  2736. u8 channel, u8 bw)
  2737. {
  2738. int flag = 0;
  2739. enum chan_allowed res, res2;
  2740. res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
  2741. if (bw == BW40MINUS) {
  2742. if (!(flag & HOSTAPD_CHAN_HT40MINUS))
  2743. return NOT_ALLOWED;
  2744. res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
  2745. } else if (bw == BW40PLUS) {
  2746. if (!(flag & HOSTAPD_CHAN_HT40PLUS))
  2747. return NOT_ALLOWED;
  2748. res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
  2749. } else if (bw == BW80) {
  2750. res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
  2751. }
  2752. if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
  2753. return NOT_ALLOWED;
  2754. if (res == NO_IR || res2 == NO_IR)
  2755. return NO_IR;
  2756. return res;
  2757. }
  2758. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2759. struct p2p_channels *chan,
  2760. struct p2p_channels *cli_chan)
  2761. {
  2762. struct hostapd_hw_modes *mode;
  2763. int cla, op, cli_cla;
  2764. if (wpa_s->hw.modes == NULL) {
  2765. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2766. "of all supported channels; assume dualband "
  2767. "support");
  2768. return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
  2769. }
  2770. cla = cli_cla = 0;
  2771. for (op = 0; op_class[op].op_class; op++) {
  2772. const struct p2p_oper_class_map *o = &op_class[op];
  2773. u8 ch;
  2774. struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
  2775. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2776. if (mode == NULL)
  2777. continue;
  2778. if (mode->mode == HOSTAPD_MODE_IEEE80211G)
  2779. wpa_s->global->p2p_24ghz_social_channels = 1;
  2780. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2781. enum chan_allowed res;
  2782. res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2783. if (res == ALLOWED) {
  2784. if (reg == NULL) {
  2785. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
  2786. o->op_class);
  2787. reg = &chan->reg_class[cla];
  2788. cla++;
  2789. reg->reg_class = o->op_class;
  2790. }
  2791. reg->channel[reg->channels] = ch;
  2792. reg->channels++;
  2793. } else if (res == NO_IR &&
  2794. wpa_s->conf->p2p_add_cli_chan) {
  2795. if (cli_reg == NULL) {
  2796. wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
  2797. o->op_class);
  2798. cli_reg = &cli_chan->reg_class[cli_cla];
  2799. cli_cla++;
  2800. cli_reg->reg_class = o->op_class;
  2801. }
  2802. cli_reg->channel[cli_reg->channels] = ch;
  2803. cli_reg->channels++;
  2804. }
  2805. }
  2806. if (reg) {
  2807. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2808. reg->channel, reg->channels);
  2809. }
  2810. if (cli_reg) {
  2811. wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
  2812. cli_reg->channel, cli_reg->channels);
  2813. }
  2814. }
  2815. chan->reg_classes = cla;
  2816. cli_chan->reg_classes = cli_cla;
  2817. return 0;
  2818. }
  2819. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2820. struct hostapd_hw_modes *mode, u8 channel)
  2821. {
  2822. int op;
  2823. enum chan_allowed ret;
  2824. for (op = 0; op_class[op].op_class; op++) {
  2825. const struct p2p_oper_class_map *o = &op_class[op];
  2826. u8 ch;
  2827. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2828. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2829. o->bw == BW20 || ch != channel)
  2830. continue;
  2831. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2832. if (ret == ALLOWED)
  2833. return (o->bw == BW40MINUS) ? -1 : 1;
  2834. }
  2835. }
  2836. return 0;
  2837. }
  2838. int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
  2839. struct hostapd_hw_modes *mode, u8 channel)
  2840. {
  2841. if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
  2842. return 0;
  2843. return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
  2844. }
  2845. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2846. size_t buf_len)
  2847. {
  2848. struct wpa_supplicant *wpa_s = ctx;
  2849. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2850. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2851. break;
  2852. }
  2853. if (wpa_s == NULL)
  2854. return -1;
  2855. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2856. }
  2857. struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
  2858. const u8 *ssid, size_t ssid_len)
  2859. {
  2860. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2861. struct wpa_ssid *s = wpa_s->current_ssid;
  2862. if (s == NULL)
  2863. continue;
  2864. if (s->mode != WPAS_MODE_P2P_GO &&
  2865. s->mode != WPAS_MODE_AP &&
  2866. s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  2867. continue;
  2868. if (s->ssid_len != ssid_len ||
  2869. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  2870. continue;
  2871. return wpa_s;
  2872. }
  2873. return NULL;
  2874. }
  2875. struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
  2876. const u8 *peer_dev_addr)
  2877. {
  2878. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2879. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2880. if (ssid == NULL)
  2881. continue;
  2882. if (ssid->mode != WPAS_MODE_INFRA)
  2883. continue;
  2884. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2885. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2886. continue;
  2887. if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
  2888. return wpa_s;
  2889. }
  2890. return NULL;
  2891. }
  2892. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2893. {
  2894. struct wpa_supplicant *wpa_s = ctx;
  2895. return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
  2896. }
  2897. static int wpas_is_concurrent_session_active(void *ctx)
  2898. {
  2899. struct wpa_supplicant *wpa_s = ctx;
  2900. struct wpa_supplicant *ifs;
  2901. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  2902. if (ifs == wpa_s)
  2903. continue;
  2904. if (ifs->wpa_state > WPA_ASSOCIATED)
  2905. return 1;
  2906. }
  2907. return 0;
  2908. }
  2909. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2910. {
  2911. struct wpa_supplicant *wpa_s = ctx;
  2912. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2913. }
  2914. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
  2915. const char *conf_p2p_dev)
  2916. {
  2917. struct wpa_interface iface;
  2918. struct wpa_supplicant *p2pdev_wpa_s;
  2919. char ifname[100];
  2920. char force_name[100];
  2921. int ret;
  2922. ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2923. wpa_s->ifname);
  2924. if (os_snprintf_error(sizeof(ifname), ret))
  2925. return -1;
  2926. force_name[0] = '\0';
  2927. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2928. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2929. force_name, wpa_s->pending_interface_addr, NULL);
  2930. if (ret < 0) {
  2931. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2932. return ret;
  2933. }
  2934. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2935. sizeof(wpa_s->pending_interface_name));
  2936. os_memset(&iface, 0, sizeof(iface));
  2937. iface.p2p_mgmt = 1;
  2938. iface.ifname = wpa_s->pending_interface_name;
  2939. iface.driver = wpa_s->driver->name;
  2940. iface.driver_param = wpa_s->conf->driver_param;
  2941. /*
  2942. * If a P2P Device configuration file was given, use it as the interface
  2943. * configuration file (instead of using parent's configuration file.
  2944. */
  2945. if (conf_p2p_dev) {
  2946. iface.confname = conf_p2p_dev;
  2947. iface.ctrl_interface = NULL;
  2948. } else {
  2949. iface.confname = wpa_s->confname;
  2950. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  2951. }
  2952. iface.conf_p2p_dev = NULL;
  2953. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  2954. if (!p2pdev_wpa_s) {
  2955. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2956. return -1;
  2957. }
  2958. wpa_s->pending_interface_name[0] = '\0';
  2959. return 0;
  2960. }
  2961. static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
  2962. const u8 *noa, size_t noa_len)
  2963. {
  2964. struct wpa_supplicant *wpa_s, *intf = ctx;
  2965. char hex[100];
  2966. for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2967. if (wpa_s->waiting_presence_resp)
  2968. break;
  2969. }
  2970. if (!wpa_s) {
  2971. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
  2972. return;
  2973. }
  2974. wpa_s->waiting_presence_resp = 0;
  2975. wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
  2976. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
  2977. " status=%u noa=%s", MAC2STR(src), status, hex);
  2978. }
  2979. static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
  2980. size_t ssid_len, u8 *go_dev_addr,
  2981. u8 *ret_ssid, size_t *ret_ssid_len)
  2982. {
  2983. struct wpa_supplicant *wpa_s = ctx;
  2984. struct wpa_ssid *s;
  2985. s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
  2986. if (s) {
  2987. os_memcpy(ret_ssid, s->ssid, s->ssid_len);
  2988. *ret_ssid_len = s->ssid_len;
  2989. os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
  2990. return 1;
  2991. }
  2992. return 0;
  2993. }
  2994. static int wpas_get_go_info(void *ctx, u8 *intended_addr,
  2995. u8 *ssid, size_t *ssid_len, int *group_iface)
  2996. {
  2997. struct wpa_supplicant *wpa_s = ctx;
  2998. struct wpa_ssid *s;
  2999. u8 bssid[ETH_ALEN];
  3000. s = wpas_p2p_group_go_ssid(wpa_s, bssid);
  3001. if (!s) {
  3002. s = wpas_p2p_get_persistent_go(wpa_s);
  3003. if (s)
  3004. os_memcpy(bssid, s->bssid, ETH_ALEN);
  3005. }
  3006. *group_iface = wpas_p2p_create_iface(wpa_s);
  3007. if (!s)
  3008. return 0;
  3009. os_memcpy(intended_addr, bssid, ETH_ALEN);
  3010. os_memcpy(ssid, s->ssid, s->ssid_len);
  3011. *ssid_len = s->ssid_len;
  3012. return 1;
  3013. }
  3014. static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
  3015. const u8 *ssid, size_t ssid_len)
  3016. {
  3017. struct wpa_supplicant *wpa_s = ctx;
  3018. struct wpa_ssid *s;
  3019. int save_config = 0;
  3020. size_t i;
  3021. /* Start with our first choice of Persistent Groups */
  3022. while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
  3023. if (go && ssid && ssid_len &&
  3024. s->ssid_len == ssid_len &&
  3025. os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
  3026. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  3027. break;
  3028. /* Remove stale persistent group */
  3029. if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
  3030. wpa_config_remove_network(wpa_s->conf, s->id);
  3031. save_config = 1;
  3032. continue;
  3033. }
  3034. for (i = 0; i < s->num_p2p_clients; i++) {
  3035. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  3036. peer, ETH_ALEN) != 0)
  3037. continue;
  3038. os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
  3039. s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
  3040. (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
  3041. break;
  3042. }
  3043. s->num_p2p_clients--;
  3044. save_config = 1;
  3045. }
  3046. if (save_config)
  3047. p2p_config_write(wpa_s);
  3048. /* Return TRUE if valid SSID remains */
  3049. return s != NULL;
  3050. }
  3051. static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
  3052. const u8 *adv_mac, const u8 *ses_mac,
  3053. const u8 *grp_mac, u32 adv_id, u32 ses_id,
  3054. u8 conncap, int passwd_id,
  3055. const u8 *persist_ssid,
  3056. size_t persist_ssid_size, int response_done,
  3057. int prov_start, const char *session_info)
  3058. {
  3059. struct wpa_supplicant *wpa_s = ctx;
  3060. u8 mac[ETH_ALEN];
  3061. struct wpa_ssid *persistent_go, *stale, *s;
  3062. int save_config = 0;
  3063. struct wpa_supplicant *go_wpa_s;
  3064. if (!dev)
  3065. return;
  3066. os_memset(mac, 0, ETH_ALEN);
  3067. if (!adv_mac)
  3068. adv_mac = mac;
  3069. if (!ses_mac)
  3070. ses_mac = mac;
  3071. if (!grp_mac)
  3072. grp_mac = mac;
  3073. if (prov_start) {
  3074. if (session_info == NULL) {
  3075. wpa_msg_global(wpa_s, MSG_INFO,
  3076. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3077. " adv_id=%x conncap=%x"
  3078. " adv_mac=" MACSTR
  3079. " session=%x mac=" MACSTR
  3080. " dev_passwd_id=%d",
  3081. MAC2STR(dev), adv_id, conncap,
  3082. MAC2STR(adv_mac),
  3083. ses_id, MAC2STR(ses_mac),
  3084. passwd_id);
  3085. } else {
  3086. wpa_msg_global(wpa_s, MSG_INFO,
  3087. P2P_EVENT_P2PS_PROVISION_START MACSTR
  3088. " adv_id=%x conncap=%x"
  3089. " adv_mac=" MACSTR
  3090. " session=%x mac=" MACSTR
  3091. " dev_passwd_id=%d info='%s'",
  3092. MAC2STR(dev), adv_id, conncap,
  3093. MAC2STR(adv_mac),
  3094. ses_id, MAC2STR(ses_mac),
  3095. passwd_id, session_info);
  3096. }
  3097. return;
  3098. }
  3099. go_wpa_s = wpas_p2p_get_go_group(wpa_s);
  3100. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3101. if (status && status != P2P_SC_SUCCESS_DEFERRED) {
  3102. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3103. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3104. if (persistent_go && !persistent_go->num_p2p_clients) {
  3105. /* remove empty persistent GO */
  3106. wpa_config_remove_network(wpa_s->conf,
  3107. persistent_go->id);
  3108. }
  3109. wpa_msg_global(wpa_s, MSG_INFO,
  3110. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3111. " status=%d"
  3112. " adv_id=%x adv_mac=" MACSTR
  3113. " session=%x mac=" MACSTR,
  3114. MAC2STR(dev), status,
  3115. adv_id, MAC2STR(adv_mac),
  3116. ses_id, MAC2STR(ses_mac));
  3117. return;
  3118. }
  3119. /* Clean up stale persistent groups with this device */
  3120. s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
  3121. persist_ssid_size);
  3122. for (;;) {
  3123. stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
  3124. if (!stale)
  3125. break;
  3126. if (s && s->ssid_len == stale->ssid_len &&
  3127. os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
  3128. os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
  3129. break;
  3130. /* Remove stale persistent group */
  3131. if (stale->mode != WPAS_MODE_P2P_GO ||
  3132. stale->num_p2p_clients <= 1) {
  3133. wpa_config_remove_network(wpa_s->conf, stale->id);
  3134. } else {
  3135. size_t i;
  3136. for (i = 0; i < stale->num_p2p_clients; i++) {
  3137. if (os_memcmp(stale->p2p_client_list +
  3138. i * ETH_ALEN,
  3139. dev, ETH_ALEN) == 0) {
  3140. os_memmove(stale->p2p_client_list +
  3141. i * ETH_ALEN,
  3142. stale->p2p_client_list +
  3143. (i + 1) * ETH_ALEN,
  3144. (stale->num_p2p_clients -
  3145. i - 1) * ETH_ALEN);
  3146. break;
  3147. }
  3148. }
  3149. stale->num_p2p_clients--;
  3150. }
  3151. save_config = 1;
  3152. }
  3153. if (save_config)
  3154. p2p_config_write(wpa_s);
  3155. if (s) {
  3156. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3157. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3158. if (persistent_go && s != persistent_go &&
  3159. !persistent_go->num_p2p_clients) {
  3160. /* remove empty persistent GO */
  3161. wpa_config_remove_network(wpa_s->conf,
  3162. persistent_go->id);
  3163. /* Save config */
  3164. }
  3165. wpa_msg_global(wpa_s, MSG_INFO,
  3166. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3167. " status=%d"
  3168. " adv_id=%x adv_mac=" MACSTR
  3169. " session=%x mac=" MACSTR
  3170. " persist=%d",
  3171. MAC2STR(dev), status,
  3172. adv_id, MAC2STR(adv_mac),
  3173. ses_id, MAC2STR(ses_mac), s->id);
  3174. return;
  3175. }
  3176. if (conncap == P2PS_SETUP_GROUP_OWNER) {
  3177. const char *go_ifname = NULL;
  3178. if (!go_wpa_s) {
  3179. wpa_s->global->pending_p2ps_group = 1;
  3180. if (wpa_s->conf->p2p_no_group_iface)
  3181. go_ifname = wpa_s->ifname;
  3182. else if (wpa_s->pending_interface_name[0])
  3183. go_ifname = wpa_s->pending_interface_name;
  3184. if (!go_ifname) {
  3185. wpas_p2ps_prov_complete(
  3186. wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
  3187. dev, adv_mac, ses_mac,
  3188. NULL, adv_id, ses_id, 0, 0,
  3189. NULL, 0, 0, 0, NULL);
  3190. return;
  3191. }
  3192. /* If PD Resp complete, start up the GO */
  3193. if (response_done && persistent_go) {
  3194. wpas_p2p_group_add_persistent(
  3195. wpa_s, persistent_go,
  3196. 0, 0, 0, 0, 0, NULL,
  3197. persistent_go->mode ==
  3198. WPAS_MODE_P2P_GO ?
  3199. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
  3200. 0);
  3201. } else if (response_done) {
  3202. wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
  3203. }
  3204. if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3205. os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
  3206. wpa_s->p2ps_join_addr_valid = 1;
  3207. wpa_dbg(wpa_s, MSG_DEBUG,
  3208. "P2PS: Saving PIN for " MACSTR,
  3209. MAC2STR(dev));
  3210. }
  3211. } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
  3212. go_ifname = go_wpa_s->ifname;
  3213. wpa_dbg(go_wpa_s, MSG_DEBUG,
  3214. "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
  3215. wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
  3216. NULL, 0, 0);
  3217. os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
  3218. wpa_s->p2ps_join_addr_valid = 1;
  3219. wpa_dbg(wpa_s, MSG_DEBUG,
  3220. "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
  3221. }
  3222. wpa_msg_global(wpa_s, MSG_INFO,
  3223. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3224. " status=%d conncap=%x"
  3225. " adv_id=%x adv_mac=" MACSTR
  3226. " session=%x mac=" MACSTR
  3227. " dev_passwd_id=%d go=%s",
  3228. MAC2STR(dev), status, conncap,
  3229. adv_id, MAC2STR(adv_mac),
  3230. ses_id, MAC2STR(ses_mac),
  3231. passwd_id, go_ifname);
  3232. return;
  3233. }
  3234. if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
  3235. wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
  3236. if (persistent_go && !persistent_go->num_p2p_clients) {
  3237. /* remove empty persistent GO */
  3238. wpa_config_remove_network(wpa_s->conf, persistent_go->id);
  3239. }
  3240. if (conncap == P2PS_SETUP_CLIENT) {
  3241. wpa_msg_global(wpa_s, MSG_INFO,
  3242. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3243. " status=%d conncap=%x"
  3244. " adv_id=%x adv_mac=" MACSTR
  3245. " session=%x mac=" MACSTR
  3246. " dev_passwd_id=%d join=" MACSTR,
  3247. MAC2STR(dev), status, conncap,
  3248. adv_id, MAC2STR(adv_mac),
  3249. ses_id, MAC2STR(ses_mac),
  3250. passwd_id, MAC2STR(grp_mac));
  3251. } else {
  3252. wpa_msg_global(wpa_s, MSG_INFO,
  3253. P2P_EVENT_P2PS_PROVISION_DONE MACSTR
  3254. " status=%d conncap=%x"
  3255. " adv_id=%x adv_mac=" MACSTR
  3256. " session=%x mac=" MACSTR
  3257. " dev_passwd_id=%d",
  3258. MAC2STR(dev), status, conncap,
  3259. adv_id, MAC2STR(adv_mac),
  3260. ses_id, MAC2STR(ses_mac),
  3261. passwd_id);
  3262. }
  3263. }
  3264. static int _wpas_p2p_in_progress(void *ctx)
  3265. {
  3266. struct wpa_supplicant *wpa_s = ctx;
  3267. return wpas_p2p_in_progress(wpa_s);
  3268. }
  3269. static int wpas_prov_disc_resp_cb(void *ctx)
  3270. {
  3271. struct wpa_supplicant *wpa_s = ctx;
  3272. struct wpa_ssid *persistent_go;
  3273. if (!wpa_s->global->pending_p2ps_group)
  3274. return 0;
  3275. wpa_s->global->pending_p2ps_group = 0;
  3276. if (wpas_p2p_get_go_group(wpa_s))
  3277. return 0;
  3278. persistent_go = wpas_p2p_get_persistent_go(wpa_s);
  3279. if (persistent_go) {
  3280. wpas_p2p_group_add_persistent(
  3281. wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
  3282. persistent_go->mode == WPAS_MODE_P2P_GO ?
  3283. P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
  3284. } else {
  3285. wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
  3286. }
  3287. return 1;
  3288. }
  3289. /**
  3290. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  3291. * @global: Pointer to global data from wpa_supplicant_init()
  3292. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3293. * Returns: 0 on success, -1 on failure
  3294. */
  3295. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  3296. {
  3297. struct p2p_config p2p;
  3298. int i;
  3299. if (wpa_s->conf->p2p_disabled)
  3300. return 0;
  3301. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  3302. return 0;
  3303. if (global->p2p)
  3304. return 0;
  3305. os_memset(&p2p, 0, sizeof(p2p));
  3306. p2p.cb_ctx = wpa_s;
  3307. p2p.debug_print = wpas_p2p_debug_print;
  3308. p2p.p2p_scan = wpas_p2p_scan;
  3309. p2p.send_action = wpas_send_action;
  3310. p2p.send_action_done = wpas_send_action_done;
  3311. p2p.go_neg_completed = wpas_go_neg_completed;
  3312. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  3313. p2p.dev_found = wpas_dev_found;
  3314. p2p.dev_lost = wpas_dev_lost;
  3315. p2p.find_stopped = wpas_find_stopped;
  3316. p2p.start_listen = wpas_start_listen;
  3317. p2p.stop_listen = wpas_stop_listen;
  3318. p2p.send_probe_resp = wpas_send_probe_resp;
  3319. p2p.sd_request = wpas_sd_request;
  3320. p2p.sd_response = wpas_sd_response;
  3321. p2p.prov_disc_req = wpas_prov_disc_req;
  3322. p2p.prov_disc_resp = wpas_prov_disc_resp;
  3323. p2p.prov_disc_fail = wpas_prov_disc_fail;
  3324. p2p.invitation_process = wpas_invitation_process;
  3325. p2p.invitation_received = wpas_invitation_received;
  3326. p2p.invitation_result = wpas_invitation_result;
  3327. p2p.get_noa = wpas_get_noa;
  3328. p2p.go_connected = wpas_go_connected;
  3329. p2p.presence_resp = wpas_presence_resp;
  3330. p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
  3331. p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
  3332. p2p.get_persistent_group = wpas_get_persistent_group;
  3333. p2p.get_go_info = wpas_get_go_info;
  3334. p2p.remove_stale_groups = wpas_remove_stale_groups;
  3335. p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
  3336. p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
  3337. p2p.p2ps_group_capability = p2ps_group_capability;
  3338. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  3339. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  3340. p2p.dev_name = wpa_s->conf->device_name;
  3341. p2p.manufacturer = wpa_s->conf->manufacturer;
  3342. p2p.model_name = wpa_s->conf->model_name;
  3343. p2p.model_number = wpa_s->conf->model_number;
  3344. p2p.serial_number = wpa_s->conf->serial_number;
  3345. if (wpa_s->wps) {
  3346. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  3347. p2p.config_methods = wpa_s->wps->config_methods;
  3348. }
  3349. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
  3350. wpa_printf(MSG_ERROR,
  3351. "P2P: Failed to configure supported channel list");
  3352. return -1;
  3353. }
  3354. if (wpa_s->conf->p2p_listen_reg_class &&
  3355. wpa_s->conf->p2p_listen_channel) {
  3356. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  3357. p2p.channel = wpa_s->conf->p2p_listen_channel;
  3358. p2p.channel_forced = 1;
  3359. } else {
  3360. /*
  3361. * Pick one of the social channels randomly as the listen
  3362. * channel.
  3363. */
  3364. if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
  3365. &p2p.channel) != 0) {
  3366. wpa_printf(MSG_INFO,
  3367. "P2P: No social channels supported by the driver - do not enable P2P");
  3368. return 0;
  3369. }
  3370. p2p.channel_forced = 0;
  3371. }
  3372. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
  3373. p2p.reg_class, p2p.channel);
  3374. if (wpa_s->conf->p2p_oper_reg_class &&
  3375. wpa_s->conf->p2p_oper_channel) {
  3376. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  3377. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  3378. p2p.cfg_op_channel = 1;
  3379. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  3380. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3381. } else {
  3382. /*
  3383. * Use random operation channel from 2.4 GHz band social
  3384. * channels (1, 6, 11) or band 60 GHz social channel (2) if no
  3385. * other preference is indicated.
  3386. */
  3387. if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
  3388. &p2p.op_channel) != 0) {
  3389. wpa_printf(MSG_ERROR,
  3390. "P2P: Failed to select random social channel as operation channel");
  3391. return -1;
  3392. }
  3393. p2p.cfg_op_channel = 0;
  3394. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  3395. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  3396. }
  3397. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  3398. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  3399. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  3400. }
  3401. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  3402. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  3403. p2p.country[2] = 0x04;
  3404. } else
  3405. os_memcpy(p2p.country, "XX\x04", 3);
  3406. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  3407. WPS_DEV_TYPE_LEN);
  3408. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  3409. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  3410. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  3411. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  3412. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  3413. p2p.max_peers = 100;
  3414. if (wpa_s->conf->p2p_ssid_postfix) {
  3415. p2p.ssid_postfix_len =
  3416. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  3417. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  3418. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  3419. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  3420. p2p.ssid_postfix_len);
  3421. }
  3422. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  3423. p2p.max_listen = wpa_s->max_remain_on_chan;
  3424. if (wpa_s->conf->p2p_passphrase_len >= 8 &&
  3425. wpa_s->conf->p2p_passphrase_len <= 63)
  3426. p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
  3427. else
  3428. p2p.passphrase_len = 8;
  3429. global->p2p = p2p_init(&p2p);
  3430. if (global->p2p == NULL)
  3431. return -1;
  3432. global->p2p_init_wpa_s = wpa_s;
  3433. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  3434. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  3435. continue;
  3436. p2p_add_wps_vendor_extension(
  3437. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  3438. }
  3439. p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
  3440. return 0;
  3441. }
  3442. /**
  3443. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  3444. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3445. *
  3446. * This function deinitialize per-interface P2P data.
  3447. */
  3448. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  3449. {
  3450. if (wpa_s->driver && wpa_s->drv_priv)
  3451. wpa_drv_probe_req_report(wpa_s, 0);
  3452. if (wpa_s->go_params) {
  3453. /* Clear any stored provisioning info */
  3454. p2p_clear_provisioning_info(
  3455. wpa_s->global->p2p,
  3456. wpa_s->go_params->peer_device_addr);
  3457. }
  3458. os_free(wpa_s->go_params);
  3459. wpa_s->go_params = NULL;
  3460. eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
  3461. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3462. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3463. wpa_s->p2p_long_listen = 0;
  3464. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  3465. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3466. wpas_p2p_remove_pending_group_interface(wpa_s);
  3467. eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
  3468. wpas_p2p_listen_work_done(wpa_s);
  3469. if (wpa_s->p2p_send_action_work) {
  3470. os_free(wpa_s->p2p_send_action_work->ctx);
  3471. radio_work_done(wpa_s->p2p_send_action_work);
  3472. wpa_s->p2p_send_action_work = NULL;
  3473. }
  3474. eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
  3475. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  3476. wpa_s->p2p_oob_dev_pw = NULL;
  3477. os_free(wpa_s->p2p_group_common_freqs);
  3478. wpa_s->p2p_group_common_freqs = NULL;
  3479. wpa_s->p2p_group_common_freqs_num = 0;
  3480. /* TODO: remove group interface from the driver if this wpa_s instance
  3481. * is on top of a P2P group interface */
  3482. }
  3483. /**
  3484. * wpas_p2p_deinit_global - Deinitialize global P2P module
  3485. * @global: Pointer to global data from wpa_supplicant_init()
  3486. *
  3487. * This function deinitializes the global (per device) P2P module.
  3488. */
  3489. static void wpas_p2p_deinit_global(struct wpa_global *global)
  3490. {
  3491. struct wpa_supplicant *wpa_s, *tmp;
  3492. wpa_s = global->ifaces;
  3493. wpas_p2p_service_flush(global->p2p_init_wpa_s);
  3494. /* Remove remaining P2P group interfaces */
  3495. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  3496. wpa_s = wpa_s->next;
  3497. while (wpa_s) {
  3498. tmp = global->ifaces;
  3499. while (tmp &&
  3500. (tmp == wpa_s ||
  3501. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  3502. tmp = tmp->next;
  3503. }
  3504. if (tmp == NULL)
  3505. break;
  3506. /* Disconnect from the P2P group and deinit the interface */
  3507. wpas_p2p_disconnect(tmp);
  3508. }
  3509. /*
  3510. * Deinit GO data on any possibly remaining interface (if main
  3511. * interface is used as GO).
  3512. */
  3513. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3514. if (wpa_s->ap_iface)
  3515. wpas_p2p_group_deinit(wpa_s);
  3516. }
  3517. p2p_deinit(global->p2p);
  3518. global->p2p = NULL;
  3519. global->p2p_init_wpa_s = NULL;
  3520. }
  3521. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  3522. {
  3523. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  3524. wpa_s->conf->p2p_no_group_iface)
  3525. return 0; /* separate interface disabled per configuration */
  3526. if (wpa_s->drv_flags &
  3527. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  3528. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  3529. return 1; /* P2P group requires a new interface in every case
  3530. */
  3531. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  3532. return 0; /* driver does not support concurrent operations */
  3533. if (wpa_s->global->ifaces->next)
  3534. return 1; /* more that one interface already in use */
  3535. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  3536. return 1; /* this interface is already in use */
  3537. return 0;
  3538. }
  3539. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  3540. const u8 *peer_addr,
  3541. enum p2p_wps_method wps_method,
  3542. int go_intent, const u8 *own_interface_addr,
  3543. unsigned int force_freq, int persistent_group,
  3544. struct wpa_ssid *ssid, unsigned int pref_freq)
  3545. {
  3546. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3547. persistent_group = 2;
  3548. /*
  3549. * Increase GO config timeout if HT40 is used since it takes some time
  3550. * to scan channels for coex purposes before the BSS can be started.
  3551. */
  3552. p2p_set_config_timeout(wpa_s->global->p2p,
  3553. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  3554. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  3555. go_intent, own_interface_addr, force_freq,
  3556. persistent_group, ssid ? ssid->ssid : NULL,
  3557. ssid ? ssid->ssid_len : 0,
  3558. wpa_s->p2p_pd_before_go_neg, pref_freq,
  3559. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3560. 0);
  3561. }
  3562. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  3563. const u8 *peer_addr,
  3564. enum p2p_wps_method wps_method,
  3565. int go_intent, const u8 *own_interface_addr,
  3566. unsigned int force_freq, int persistent_group,
  3567. struct wpa_ssid *ssid, unsigned int pref_freq)
  3568. {
  3569. if (persistent_group && wpa_s->conf->persistent_reconnect)
  3570. persistent_group = 2;
  3571. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  3572. go_intent, own_interface_addr, force_freq,
  3573. persistent_group, ssid ? ssid->ssid : NULL,
  3574. ssid ? ssid->ssid_len : 0, pref_freq,
  3575. wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
  3576. 0);
  3577. }
  3578. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  3579. {
  3580. wpa_s->p2p_join_scan_count++;
  3581. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  3582. wpa_s->p2p_join_scan_count);
  3583. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  3584. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  3585. " for join operationg - stop join attempt",
  3586. MAC2STR(wpa_s->pending_join_iface_addr));
  3587. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3588. if (wpa_s->p2p_auto_pd) {
  3589. wpa_s->p2p_auto_pd = 0;
  3590. wpa_msg_global(wpa_s, MSG_INFO,
  3591. P2P_EVENT_PROV_DISC_FAILURE
  3592. " p2p_dev_addr=" MACSTR " status=N/A",
  3593. MAC2STR(wpa_s->pending_join_dev_addr));
  3594. return;
  3595. }
  3596. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3597. P2P_EVENT_GROUP_FORMATION_FAILURE);
  3598. }
  3599. }
  3600. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  3601. {
  3602. int res;
  3603. unsigned int num, i;
  3604. struct wpa_used_freq_data *freqs;
  3605. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  3606. /* Multiple channels are supported and not all are in use */
  3607. return 0;
  3608. }
  3609. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  3610. sizeof(struct wpa_used_freq_data));
  3611. if (!freqs)
  3612. return 1;
  3613. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3614. wpa_s->num_multichan_concurrent);
  3615. for (i = 0; i < num; i++) {
  3616. if (freqs[i].freq == freq) {
  3617. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  3618. freq);
  3619. res = 0;
  3620. goto exit_free;
  3621. }
  3622. }
  3623. wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
  3624. res = 1;
  3625. exit_free:
  3626. os_free(freqs);
  3627. return res;
  3628. }
  3629. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  3630. const u8 *peer_dev_addr)
  3631. {
  3632. struct wpa_bss *bss;
  3633. int updated;
  3634. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  3635. if (bss == NULL)
  3636. return -1;
  3637. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  3638. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  3639. "last scan");
  3640. return 0;
  3641. }
  3642. updated = os_reltime_before(&wpa_s->p2p_auto_started,
  3643. &bss->last_update);
  3644. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  3645. "%ld.%06ld (%supdated in last scan)",
  3646. bss->last_update.sec, bss->last_update.usec,
  3647. updated ? "": "not ");
  3648. return updated;
  3649. }
  3650. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  3651. struct wpa_scan_results *scan_res)
  3652. {
  3653. struct wpa_bss *bss = NULL;
  3654. int freq;
  3655. u8 iface_addr[ETH_ALEN];
  3656. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3657. if (wpa_s->global->p2p_disabled)
  3658. return;
  3659. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  3660. scan_res ? (int) scan_res->num : -1,
  3661. wpa_s->p2p_auto_join ? "auto_" : "");
  3662. if (scan_res)
  3663. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  3664. if (wpa_s->p2p_auto_pd) {
  3665. int join = wpas_p2p_peer_go(wpa_s,
  3666. wpa_s->pending_join_dev_addr);
  3667. if (join == 0 &&
  3668. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  3669. wpa_s->auto_pd_scan_retry++;
  3670. bss = wpa_bss_get_bssid_latest(
  3671. wpa_s, wpa_s->pending_join_dev_addr);
  3672. if (bss) {
  3673. freq = bss->freq;
  3674. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  3675. "the peer " MACSTR " at %d MHz",
  3676. wpa_s->auto_pd_scan_retry,
  3677. MAC2STR(wpa_s->
  3678. pending_join_dev_addr),
  3679. freq);
  3680. wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
  3681. return;
  3682. }
  3683. }
  3684. if (join < 0)
  3685. join = 0;
  3686. wpa_s->p2p_auto_pd = 0;
  3687. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  3688. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  3689. MAC2STR(wpa_s->pending_join_dev_addr), join);
  3690. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3691. wpa_s->pending_join_dev_addr, NULL,
  3692. wpa_s->pending_pd_config_methods, join,
  3693. 0, wpa_s->user_initiated_pd) < 0) {
  3694. wpa_s->p2p_auto_pd = 0;
  3695. wpa_msg_global(wpa_s, MSG_INFO,
  3696. P2P_EVENT_PROV_DISC_FAILURE
  3697. " p2p_dev_addr=" MACSTR " status=N/A",
  3698. MAC2STR(wpa_s->pending_join_dev_addr));
  3699. }
  3700. return;
  3701. }
  3702. if (wpa_s->p2p_auto_join) {
  3703. int join = wpas_p2p_peer_go(wpa_s,
  3704. wpa_s->pending_join_dev_addr);
  3705. if (join < 0) {
  3706. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3707. "running a GO -> use GO Negotiation");
  3708. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3709. P2P_EVENT_FALLBACK_TO_GO_NEG
  3710. "reason=peer-not-running-GO");
  3711. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3712. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3713. wpa_s->p2p_persistent_group, 0, 0, 0,
  3714. wpa_s->p2p_go_intent,
  3715. wpa_s->p2p_connect_freq,
  3716. wpa_s->p2p_persistent_id,
  3717. wpa_s->p2p_pd_before_go_neg,
  3718. wpa_s->p2p_go_ht40,
  3719. wpa_s->p2p_go_vht);
  3720. return;
  3721. }
  3722. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3723. "try to join the group", join ? "" :
  3724. " in older scan");
  3725. if (!join) {
  3726. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3727. P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
  3728. wpa_s->p2p_fallback_to_go_neg = 1;
  3729. }
  3730. }
  3731. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3732. wpa_s->pending_join_iface_addr);
  3733. if (freq < 0 &&
  3734. p2p_get_interface_addr(wpa_s->global->p2p,
  3735. wpa_s->pending_join_dev_addr,
  3736. iface_addr) == 0 &&
  3737. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
  3738. && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
  3739. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3740. "address for join from " MACSTR " to " MACSTR
  3741. " based on newly discovered P2P peer entry",
  3742. MAC2STR(wpa_s->pending_join_iface_addr),
  3743. MAC2STR(iface_addr));
  3744. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3745. ETH_ALEN);
  3746. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3747. wpa_s->pending_join_iface_addr);
  3748. }
  3749. if (freq >= 0) {
  3750. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3751. "from P2P peer table: %d MHz", freq);
  3752. }
  3753. if (wpa_s->p2p_join_ssid_len) {
  3754. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  3755. MACSTR " and SSID %s",
  3756. MAC2STR(wpa_s->pending_join_iface_addr),
  3757. wpa_ssid_txt(wpa_s->p2p_join_ssid,
  3758. wpa_s->p2p_join_ssid_len));
  3759. bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
  3760. wpa_s->p2p_join_ssid,
  3761. wpa_s->p2p_join_ssid_len);
  3762. }
  3763. if (!bss) {
  3764. wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
  3765. MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
  3766. bss = wpa_bss_get_bssid_latest(wpa_s,
  3767. wpa_s->pending_join_iface_addr);
  3768. }
  3769. if (bss) {
  3770. freq = bss->freq;
  3771. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3772. "from BSS table: %d MHz (SSID %s)", freq,
  3773. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3774. }
  3775. if (freq > 0) {
  3776. u16 method;
  3777. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3778. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3779. P2P_EVENT_GROUP_FORMATION_FAILURE
  3780. "reason=FREQ_CONFLICT");
  3781. return;
  3782. }
  3783. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3784. "prior to joining an existing group (GO " MACSTR
  3785. " freq=%u MHz)",
  3786. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3787. wpa_s->pending_pd_before_join = 1;
  3788. switch (wpa_s->pending_join_wps_method) {
  3789. case WPS_PIN_DISPLAY:
  3790. method = WPS_CONFIG_KEYPAD;
  3791. break;
  3792. case WPS_PIN_KEYPAD:
  3793. method = WPS_CONFIG_DISPLAY;
  3794. break;
  3795. case WPS_PBC:
  3796. method = WPS_CONFIG_PUSHBUTTON;
  3797. break;
  3798. default:
  3799. method = 0;
  3800. break;
  3801. }
  3802. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3803. wpa_s->pending_join_dev_addr) ==
  3804. method)) {
  3805. /*
  3806. * We have already performed provision discovery for
  3807. * joining the group. Proceed directly to join
  3808. * operation without duplicated provision discovery. */
  3809. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3810. "with " MACSTR " already done - proceed to "
  3811. "join",
  3812. MAC2STR(wpa_s->pending_join_dev_addr));
  3813. wpa_s->pending_pd_before_join = 0;
  3814. goto start;
  3815. }
  3816. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3817. wpa_s->pending_join_dev_addr,
  3818. NULL, method, 1,
  3819. freq, wpa_s->user_initiated_pd) < 0) {
  3820. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3821. "Discovery Request before joining an "
  3822. "existing group");
  3823. wpa_s->pending_pd_before_join = 0;
  3824. goto start;
  3825. }
  3826. return;
  3827. }
  3828. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3829. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3830. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3831. wpas_p2p_check_join_scan_limit(wpa_s);
  3832. return;
  3833. start:
  3834. /* Start join operation immediately */
  3835. wpas_p2p_join_start(wpa_s, 0, NULL, 0);
  3836. }
  3837. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
  3838. const u8 *ssid, size_t ssid_len)
  3839. {
  3840. int ret;
  3841. struct wpa_driver_scan_params params;
  3842. struct wpabuf *wps_ie, *ies;
  3843. size_t ielen;
  3844. int freqs[2] = { 0, 0 };
  3845. os_memset(&params, 0, sizeof(params));
  3846. /* P2P Wildcard SSID */
  3847. params.num_ssids = 1;
  3848. if (ssid && ssid_len) {
  3849. params.ssids[0].ssid = ssid;
  3850. params.ssids[0].ssid_len = ssid_len;
  3851. os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
  3852. wpa_s->p2p_join_ssid_len = ssid_len;
  3853. } else {
  3854. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3855. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3856. wpa_s->p2p_join_ssid_len = 0;
  3857. }
  3858. wpa_s->wps->dev.p2p = 1;
  3859. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3860. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3861. NULL);
  3862. if (wps_ie == NULL) {
  3863. wpas_p2p_scan_res_join(wpa_s, NULL);
  3864. return;
  3865. }
  3866. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3867. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3868. if (ies == NULL) {
  3869. wpabuf_free(wps_ie);
  3870. wpas_p2p_scan_res_join(wpa_s, NULL);
  3871. return;
  3872. }
  3873. wpabuf_put_buf(ies, wps_ie);
  3874. wpabuf_free(wps_ie);
  3875. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3876. params.p2p_probe = 1;
  3877. params.extra_ies = wpabuf_head(ies);
  3878. params.extra_ies_len = wpabuf_len(ies);
  3879. if (!freq) {
  3880. int oper_freq;
  3881. /*
  3882. * If freq is not provided, check the operating freq of the GO
  3883. * and use a single channel scan on if possible.
  3884. */
  3885. oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3886. wpa_s->pending_join_iface_addr);
  3887. if (oper_freq > 0)
  3888. freq = oper_freq;
  3889. }
  3890. if (freq > 0) {
  3891. freqs[0] = freq;
  3892. params.freqs = freqs;
  3893. }
  3894. /*
  3895. * Run a scan to update BSS table and start Provision Discovery once
  3896. * the new scan results become available.
  3897. */
  3898. ret = wpa_drv_scan(wpa_s, &params);
  3899. if (!ret) {
  3900. os_get_reltime(&wpa_s->scan_trigger_time);
  3901. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3902. wpa_s->own_scan_requested = 1;
  3903. }
  3904. wpabuf_free(ies);
  3905. if (ret) {
  3906. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3907. "try again later");
  3908. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3909. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3910. wpas_p2p_check_join_scan_limit(wpa_s);
  3911. }
  3912. }
  3913. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3914. {
  3915. struct wpa_supplicant *wpa_s = eloop_ctx;
  3916. wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
  3917. }
  3918. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3919. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3920. int auto_join, int op_freq,
  3921. const u8 *ssid, size_t ssid_len)
  3922. {
  3923. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3924. MACSTR " dev " MACSTR " op_freq=%d)%s",
  3925. MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
  3926. auto_join ? " (auto_join)" : "");
  3927. if (ssid && ssid_len) {
  3928. wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
  3929. wpa_ssid_txt(ssid, ssid_len));
  3930. }
  3931. wpa_s->p2p_auto_pd = 0;
  3932. wpa_s->p2p_auto_join = !!auto_join;
  3933. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3934. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3935. wpa_s->pending_join_wps_method = wps_method;
  3936. /* Make sure we are not running find during connection establishment */
  3937. wpas_p2p_stop_find(wpa_s);
  3938. wpa_s->p2p_join_scan_count = 0;
  3939. wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
  3940. return 0;
  3941. }
  3942. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
  3943. const u8 *ssid, size_t ssid_len)
  3944. {
  3945. struct wpa_supplicant *group;
  3946. struct p2p_go_neg_results res;
  3947. struct wpa_bss *bss;
  3948. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3949. if (group == NULL)
  3950. return -1;
  3951. if (group != wpa_s) {
  3952. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3953. sizeof(group->p2p_pin));
  3954. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3955. } else {
  3956. /*
  3957. * Need to mark the current interface for p2p_group_formation
  3958. * when a separate group interface is not used. This is needed
  3959. * to allow p2p_cancel stop a pending p2p_connect-join.
  3960. * wpas_p2p_init_group_interface() addresses this for the case
  3961. * where a separate group interface is used.
  3962. */
  3963. wpa_s->global->p2p_group_formation = wpa_s;
  3964. }
  3965. group->p2p_in_provisioning = 1;
  3966. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3967. os_memset(&res, 0, sizeof(res));
  3968. os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
  3969. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3970. ETH_ALEN);
  3971. res.wps_method = wpa_s->pending_join_wps_method;
  3972. if (freq && ssid && ssid_len) {
  3973. res.freq = freq;
  3974. res.ssid_len = ssid_len;
  3975. os_memcpy(res.ssid, ssid, ssid_len);
  3976. } else {
  3977. bss = wpa_bss_get_bssid_latest(wpa_s,
  3978. wpa_s->pending_join_iface_addr);
  3979. if (bss) {
  3980. res.freq = bss->freq;
  3981. res.ssid_len = bss->ssid_len;
  3982. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3983. wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
  3984. bss->freq,
  3985. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3986. }
  3987. }
  3988. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3989. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3990. "starting client");
  3991. wpa_drv_cancel_remain_on_channel(wpa_s);
  3992. wpa_s->off_channel_freq = 0;
  3993. wpa_s->roc_waiting_drv_freq = 0;
  3994. }
  3995. wpas_start_wps_enrollee(group, &res);
  3996. /*
  3997. * Allow a longer timeout for join-a-running-group than normal 15
  3998. * second group formation timeout since the GO may not have authorized
  3999. * our connection yet.
  4000. */
  4001. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  4002. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  4003. wpa_s, NULL);
  4004. return 0;
  4005. }
  4006. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  4007. int *force_freq, int *pref_freq, int go)
  4008. {
  4009. struct wpa_used_freq_data *freqs;
  4010. int res, best_freq, num_unused;
  4011. unsigned int freq_in_use = 0, num, i;
  4012. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4013. sizeof(struct wpa_used_freq_data));
  4014. if (!freqs)
  4015. return -1;
  4016. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4017. wpa_s->num_multichan_concurrent);
  4018. /*
  4019. * It is possible that the total number of used frequencies is bigger
  4020. * than the number of frequencies used for P2P, so get the system wide
  4021. * number of unused frequencies.
  4022. */
  4023. num_unused = wpas_p2p_num_unused_channels(wpa_s);
  4024. wpa_printf(MSG_DEBUG,
  4025. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
  4026. freq, wpa_s->num_multichan_concurrent, num, num_unused);
  4027. if (freq > 0) {
  4028. int ret;
  4029. if (go)
  4030. ret = p2p_supported_freq(wpa_s->global->p2p, freq);
  4031. else
  4032. ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
  4033. if (!ret) {
  4034. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4035. ieee80211_is_dfs(freq)) {
  4036. /*
  4037. * If freq is a DFS channel and DFS is offloaded
  4038. * to the driver, allow P2P GO to use it.
  4039. */
  4040. wpa_printf(MSG_DEBUG,
  4041. "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
  4042. freq);
  4043. } else {
  4044. wpa_printf(MSG_DEBUG,
  4045. "P2P: The forced channel (%u MHz) is not supported for P2P uses",
  4046. freq);
  4047. res = -3;
  4048. goto exit_free;
  4049. }
  4050. }
  4051. for (i = 0; i < num; i++) {
  4052. if (freqs[i].freq == freq)
  4053. freq_in_use = 1;
  4054. }
  4055. if (num_unused <= 0 && !freq_in_use) {
  4056. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  4057. freq);
  4058. res = -2;
  4059. goto exit_free;
  4060. }
  4061. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  4062. "requested channel (%u MHz)", freq);
  4063. *force_freq = freq;
  4064. goto exit_ok;
  4065. }
  4066. best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4067. /* We have a candidate frequency to use */
  4068. if (best_freq > 0) {
  4069. if (*pref_freq == 0 && num_unused > 0) {
  4070. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
  4071. best_freq);
  4072. *pref_freq = best_freq;
  4073. } else {
  4074. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  4075. best_freq);
  4076. *force_freq = best_freq;
  4077. }
  4078. } else if (num_unused > 0) {
  4079. wpa_printf(MSG_DEBUG,
  4080. "P2P: Current operating channels are not available for P2P. Try to use another channel");
  4081. *force_freq = 0;
  4082. } else {
  4083. wpa_printf(MSG_DEBUG,
  4084. "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  4085. res = -2;
  4086. goto exit_free;
  4087. }
  4088. exit_ok:
  4089. res = 0;
  4090. exit_free:
  4091. os_free(freqs);
  4092. return res;
  4093. }
  4094. /**
  4095. * wpas_p2p_connect - Request P2P Group Formation to be started
  4096. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4097. * @peer_addr: Address of the peer P2P Device
  4098. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  4099. * @persistent_group: Whether to create a persistent group
  4100. * @auto_join: Whether to select join vs. GO Negotiation automatically
  4101. * @join: Whether to join an existing group (as a client) instead of starting
  4102. * Group Owner negotiation; @peer_addr is BSSID in that case
  4103. * @auth: Whether to only authorize the connection instead of doing that and
  4104. * initiating Group Owner negotiation
  4105. * @go_intent: GO Intent or -1 to use default
  4106. * @freq: Frequency for the group or 0 for auto-selection
  4107. * @persistent_id: Persistent group credentials to use for forcing GO
  4108. * parameters or -1 to generate new values (SSID/passphrase)
  4109. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  4110. * interoperability workaround when initiating group formation
  4111. * @ht40: Start GO with 40 MHz channel width
  4112. * @vht: Start GO with VHT support
  4113. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  4114. * failure, -2 on failure due to channel not currently available,
  4115. * -3 if forced channel is not supported
  4116. */
  4117. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4118. const char *pin, enum p2p_wps_method wps_method,
  4119. int persistent_group, int auto_join, int join, int auth,
  4120. int go_intent, int freq, int persistent_id, int pd,
  4121. int ht40, int vht)
  4122. {
  4123. int force_freq = 0, pref_freq = 0;
  4124. int ret = 0, res;
  4125. enum wpa_driver_if_type iftype;
  4126. const u8 *if_addr;
  4127. struct wpa_ssid *ssid = NULL;
  4128. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4129. return -1;
  4130. if (persistent_id >= 0) {
  4131. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4132. if (ssid == NULL || ssid->disabled != 2 ||
  4133. ssid->mode != WPAS_MODE_P2P_GO)
  4134. return -1;
  4135. }
  4136. os_free(wpa_s->global->add_psk);
  4137. wpa_s->global->add_psk = NULL;
  4138. wpa_s->global->p2p_fail_on_wps_complete = 0;
  4139. wpa_s->global->pending_p2ps_group = 0;
  4140. if (go_intent < 0)
  4141. go_intent = wpa_s->conf->p2p_go_intent;
  4142. if (!auth)
  4143. wpa_s->p2p_long_listen = 0;
  4144. wpa_s->p2p_wps_method = wps_method;
  4145. wpa_s->p2p_persistent_group = !!persistent_group;
  4146. wpa_s->p2p_persistent_id = persistent_id;
  4147. wpa_s->p2p_go_intent = go_intent;
  4148. wpa_s->p2p_connect_freq = freq;
  4149. wpa_s->p2p_fallback_to_go_neg = 0;
  4150. wpa_s->p2p_pd_before_go_neg = !!pd;
  4151. wpa_s->p2p_go_ht40 = !!ht40;
  4152. wpa_s->p2p_go_vht = !!vht;
  4153. if (pin)
  4154. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  4155. else if (wps_method == WPS_PIN_DISPLAY) {
  4156. ret = wps_generate_pin();
  4157. res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
  4158. "%08d", ret);
  4159. if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
  4160. wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
  4161. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  4162. wpa_s->p2p_pin);
  4163. } else
  4164. wpa_s->p2p_pin[0] = '\0';
  4165. if (join || auto_join) {
  4166. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  4167. if (auth) {
  4168. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  4169. "connect a running group from " MACSTR,
  4170. MAC2STR(peer_addr));
  4171. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4172. return ret;
  4173. }
  4174. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  4175. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  4176. iface_addr) < 0) {
  4177. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  4178. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  4179. dev_addr);
  4180. }
  4181. if (auto_join) {
  4182. os_get_reltime(&wpa_s->p2p_auto_started);
  4183. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  4184. "%ld.%06ld",
  4185. wpa_s->p2p_auto_started.sec,
  4186. wpa_s->p2p_auto_started.usec);
  4187. }
  4188. wpa_s->user_initiated_pd = 1;
  4189. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  4190. auto_join, freq, NULL, 0) < 0)
  4191. return -1;
  4192. return ret;
  4193. }
  4194. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  4195. go_intent == 15);
  4196. if (res)
  4197. return res;
  4198. wpas_p2p_set_own_freq_preference(wpa_s,
  4199. force_freq ? force_freq : pref_freq);
  4200. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  4201. if (wpa_s->create_p2p_iface) {
  4202. /* Prepare to add a new interface for the group */
  4203. iftype = WPA_IF_P2P_GROUP;
  4204. if (go_intent == 15)
  4205. iftype = WPA_IF_P2P_GO;
  4206. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  4207. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  4208. "interface for the group");
  4209. return -1;
  4210. }
  4211. if_addr = wpa_s->pending_interface_addr;
  4212. } else
  4213. if_addr = wpa_s->own_addr;
  4214. if (auth) {
  4215. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  4216. go_intent, if_addr,
  4217. force_freq, persistent_group, ssid,
  4218. pref_freq) < 0)
  4219. return -1;
  4220. return ret;
  4221. }
  4222. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  4223. go_intent, if_addr, force_freq,
  4224. persistent_group, ssid, pref_freq) < 0) {
  4225. if (wpa_s->create_p2p_iface)
  4226. wpas_p2p_remove_pending_group_interface(wpa_s);
  4227. return -1;
  4228. }
  4229. return ret;
  4230. }
  4231. /**
  4232. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  4233. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4234. * @freq: Frequency of the channel in MHz
  4235. * @duration: Duration of the stay on the channel in milliseconds
  4236. *
  4237. * This callback is called when the driver indicates that it has started the
  4238. * requested remain-on-channel duration.
  4239. */
  4240. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4241. unsigned int freq, unsigned int duration)
  4242. {
  4243. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4244. return;
  4245. wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
  4246. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4247. wpa_s->roc_waiting_drv_freq, freq, duration);
  4248. if (wpa_s->off_channel_freq &&
  4249. wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  4250. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  4251. wpa_s->pending_listen_duration);
  4252. wpa_s->pending_listen_freq = 0;
  4253. } else {
  4254. wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
  4255. wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
  4256. freq, duration);
  4257. }
  4258. }
  4259. int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4260. {
  4261. /* Limit maximum Listen state time based on driver limitation. */
  4262. if (timeout > wpa_s->max_remain_on_chan)
  4263. timeout = wpa_s->max_remain_on_chan;
  4264. return p2p_listen(wpa_s->global->p2p, timeout);
  4265. }
  4266. /**
  4267. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  4268. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4269. * @freq: Frequency of the channel in MHz
  4270. *
  4271. * This callback is called when the driver indicates that a remain-on-channel
  4272. * operation has been completed, i.e., the duration on the requested channel
  4273. * has timed out.
  4274. */
  4275. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  4276. unsigned int freq)
  4277. {
  4278. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  4279. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  4280. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  4281. wpas_p2p_listen_work_done(wpa_s);
  4282. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4283. return;
  4284. if (wpa_s->p2p_long_listen > 0)
  4285. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  4286. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  4287. return; /* P2P module started a new operation */
  4288. if (offchannel_pending_action_tx(wpa_s))
  4289. return;
  4290. if (wpa_s->p2p_long_listen > 0) {
  4291. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  4292. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  4293. } else {
  4294. /*
  4295. * When listen duration is over, stop listen & update p2p_state
  4296. * to IDLE.
  4297. */
  4298. p2p_stop_listen(wpa_s->global->p2p);
  4299. }
  4300. }
  4301. /**
  4302. * wpas_p2p_group_remove - Remove a P2P group
  4303. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4304. * @ifname: Network interface name of the group interface or "*" to remove all
  4305. * groups
  4306. * Returns: 0 on success, -1 on failure
  4307. *
  4308. * This function is used to remove a P2P group. This can be used to disconnect
  4309. * from a group in which the local end is a P2P Client or to end a P2P Group in
  4310. * case the local end is the Group Owner. If a virtual network interface was
  4311. * created for this group, that interface will be removed. Otherwise, only the
  4312. * configured P2P group network will be removed from the interface.
  4313. */
  4314. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  4315. {
  4316. struct wpa_global *global = wpa_s->global;
  4317. struct wpa_supplicant *calling_wpa_s = wpa_s;
  4318. if (os_strcmp(ifname, "*") == 0) {
  4319. struct wpa_supplicant *prev;
  4320. wpa_s = global->ifaces;
  4321. while (wpa_s) {
  4322. prev = wpa_s;
  4323. wpa_s = wpa_s->next;
  4324. if (prev->p2p_group_interface !=
  4325. NOT_P2P_GROUP_INTERFACE ||
  4326. (prev->current_ssid &&
  4327. prev->current_ssid->p2p_group))
  4328. wpas_p2p_disconnect_safely(prev, calling_wpa_s);
  4329. }
  4330. return 0;
  4331. }
  4332. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4333. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4334. break;
  4335. }
  4336. return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
  4337. }
  4338. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  4339. {
  4340. unsigned int r;
  4341. if (freq == 2) {
  4342. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  4343. "band");
  4344. if (wpa_s->best_24_freq > 0 &&
  4345. p2p_supported_freq_go(wpa_s->global->p2p,
  4346. wpa_s->best_24_freq)) {
  4347. freq = wpa_s->best_24_freq;
  4348. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  4349. "channel: %d MHz", freq);
  4350. } else {
  4351. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4352. return -1;
  4353. freq = 2412 + (r % 3) * 25;
  4354. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  4355. "channel: %d MHz", freq);
  4356. }
  4357. }
  4358. if (freq == 5) {
  4359. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  4360. "band");
  4361. if (wpa_s->best_5_freq > 0 &&
  4362. p2p_supported_freq_go(wpa_s->global->p2p,
  4363. wpa_s->best_5_freq)) {
  4364. freq = wpa_s->best_5_freq;
  4365. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  4366. "channel: %d MHz", freq);
  4367. } else {
  4368. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4369. return -1;
  4370. freq = 5180 + (r % 4) * 20;
  4371. if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4372. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  4373. "5 GHz channel for P2P group");
  4374. return -1;
  4375. }
  4376. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  4377. "channel: %d MHz", freq);
  4378. }
  4379. }
  4380. if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
  4381. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4382. ieee80211_is_dfs(freq)) {
  4383. /*
  4384. * If freq is a DFS channel and DFS is offloaded to the
  4385. * driver, allow P2P GO to use it.
  4386. */
  4387. wpa_printf(MSG_DEBUG, "P2P: "
  4388. "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
  4389. __func__, freq);
  4390. return freq;
  4391. }
  4392. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  4393. "(%u MHz) is not supported for P2P uses",
  4394. freq);
  4395. return -1;
  4396. }
  4397. return freq;
  4398. }
  4399. static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
  4400. struct p2p_go_neg_results *params,
  4401. const struct p2p_channels *channels)
  4402. {
  4403. unsigned int i, r;
  4404. /* first try some random selection of the social channels */
  4405. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  4406. return -1;
  4407. for (i = 0; i < 3; i++) {
  4408. params->freq = 2412 + ((r + i) % 3) * 25;
  4409. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4410. freq_included(channels, params->freq) &&
  4411. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4412. goto out;
  4413. }
  4414. /* try all channels in reg. class 81 */
  4415. for (i = 0; i < 11; i++) {
  4416. params->freq = 2412 + i * 5;
  4417. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4418. freq_included(channels, params->freq) &&
  4419. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4420. goto out;
  4421. }
  4422. /* try all channels in operating class 115 */
  4423. for (i = 0; i < 4; i++) {
  4424. params->freq = 5180 + i * 20;
  4425. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4426. freq_included(channels, params->freq) &&
  4427. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4428. goto out;
  4429. }
  4430. /* try all channels in operating class 124 */
  4431. for (i = 0; i < 4; i++) {
  4432. params->freq = 5745 + i * 20;
  4433. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4434. freq_included(channels, params->freq) &&
  4435. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4436. goto out;
  4437. }
  4438. /* try social channel class 180 channel 2 */
  4439. params->freq = 58320 + 1 * 2160;
  4440. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4441. freq_included(channels, params->freq) &&
  4442. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4443. goto out;
  4444. /* try all channels in reg. class 180 */
  4445. for (i = 0; i < 4; i++) {
  4446. params->freq = 58320 + i * 2160;
  4447. if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
  4448. freq_included(channels, params->freq) &&
  4449. p2p_supported_freq(wpa_s->global->p2p, params->freq))
  4450. goto out;
  4451. }
  4452. wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
  4453. return -1;
  4454. out:
  4455. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
  4456. params->freq);
  4457. return 0;
  4458. }
  4459. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  4460. struct p2p_go_neg_results *params,
  4461. int freq, int ht40, int vht,
  4462. const struct p2p_channels *channels)
  4463. {
  4464. struct wpa_used_freq_data *freqs;
  4465. unsigned int pref_freq, cand_freq;
  4466. unsigned int num, i;
  4467. os_memset(params, 0, sizeof(*params));
  4468. params->role_go = 1;
  4469. params->ht40 = ht40;
  4470. params->vht = vht;
  4471. if (freq) {
  4472. if (!freq_included(channels, freq)) {
  4473. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  4474. "accepted", freq);
  4475. return -1;
  4476. }
  4477. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  4478. "frequency %d MHz", freq);
  4479. params->freq = freq;
  4480. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  4481. wpa_s->conf->p2p_oper_channel >= 1 &&
  4482. wpa_s->conf->p2p_oper_channel <= 11 &&
  4483. freq_included(channels,
  4484. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  4485. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  4486. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4487. "frequency %d MHz", params->freq);
  4488. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  4489. wpa_s->conf->p2p_oper_reg_class == 116 ||
  4490. wpa_s->conf->p2p_oper_reg_class == 117 ||
  4491. wpa_s->conf->p2p_oper_reg_class == 124 ||
  4492. wpa_s->conf->p2p_oper_reg_class == 126 ||
  4493. wpa_s->conf->p2p_oper_reg_class == 127) &&
  4494. freq_included(channels,
  4495. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  4496. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  4497. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  4498. "frequency %d MHz", params->freq);
  4499. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  4500. wpa_s->best_overall_freq > 0 &&
  4501. p2p_supported_freq_go(wpa_s->global->p2p,
  4502. wpa_s->best_overall_freq) &&
  4503. freq_included(channels, wpa_s->best_overall_freq)) {
  4504. params->freq = wpa_s->best_overall_freq;
  4505. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  4506. "channel %d MHz", params->freq);
  4507. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  4508. wpa_s->best_24_freq > 0 &&
  4509. p2p_supported_freq_go(wpa_s->global->p2p,
  4510. wpa_s->best_24_freq) &&
  4511. freq_included(channels, wpa_s->best_24_freq)) {
  4512. params->freq = wpa_s->best_24_freq;
  4513. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  4514. "channel %d MHz", params->freq);
  4515. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  4516. wpa_s->best_5_freq > 0 &&
  4517. p2p_supported_freq_go(wpa_s->global->p2p,
  4518. wpa_s->best_5_freq) &&
  4519. freq_included(channels, wpa_s->best_5_freq)) {
  4520. params->freq = wpa_s->best_5_freq;
  4521. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  4522. "channel %d MHz", params->freq);
  4523. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  4524. channels))) {
  4525. params->freq = pref_freq;
  4526. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  4527. "channels", params->freq);
  4528. } else {
  4529. /* no preference, select some channel */
  4530. if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
  4531. return -1;
  4532. }
  4533. freqs = os_calloc(wpa_s->num_multichan_concurrent,
  4534. sizeof(struct wpa_used_freq_data));
  4535. if (!freqs)
  4536. return -1;
  4537. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  4538. wpa_s->num_multichan_concurrent);
  4539. cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
  4540. /* First try the best used frequency if possible */
  4541. if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
  4542. params->freq = cand_freq;
  4543. } else if (!freq) {
  4544. /* Try any of the used frequencies */
  4545. for (i = 0; i < num; i++) {
  4546. if (freq_included(channels, freqs[i].freq)) {
  4547. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  4548. freqs[i].freq);
  4549. params->freq = freqs[i].freq;
  4550. break;
  4551. }
  4552. }
  4553. if (i == num) {
  4554. if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4555. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  4556. os_free(freqs);
  4557. return -1;
  4558. } else {
  4559. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  4560. }
  4561. }
  4562. } else {
  4563. for (i = 0; i < num; i++) {
  4564. if (freqs[i].freq == freq)
  4565. break;
  4566. }
  4567. if (i == num) {
  4568. if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
  4569. if (freq)
  4570. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  4571. os_free(freqs);
  4572. return -1;
  4573. } else {
  4574. wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
  4575. }
  4576. }
  4577. }
  4578. os_free(freqs);
  4579. return 0;
  4580. }
  4581. static struct wpa_supplicant *
  4582. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  4583. int go)
  4584. {
  4585. struct wpa_supplicant *group_wpa_s;
  4586. if (!wpas_p2p_create_iface(wpa_s)) {
  4587. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  4588. "operations");
  4589. wpa_s->p2p_first_connection_timeout = 0;
  4590. return wpa_s;
  4591. }
  4592. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  4593. WPA_IF_P2P_CLIENT) < 0) {
  4594. wpa_msg_global(wpa_s, MSG_ERROR,
  4595. "P2P: Failed to add group interface");
  4596. return NULL;
  4597. }
  4598. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  4599. if (group_wpa_s == NULL) {
  4600. wpa_msg_global(wpa_s, MSG_ERROR,
  4601. "P2P: Failed to initialize group interface");
  4602. wpas_p2p_remove_pending_group_interface(wpa_s);
  4603. return NULL;
  4604. }
  4605. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  4606. group_wpa_s->ifname);
  4607. group_wpa_s->p2p_first_connection_timeout = 0;
  4608. return group_wpa_s;
  4609. }
  4610. /**
  4611. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  4612. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  4613. * @persistent_group: Whether to create a persistent group
  4614. * @freq: Frequency for the group or 0 to indicate no hardcoding
  4615. * @ht40: Start GO with 40 MHz channel width
  4616. * @vht: Start GO with VHT support
  4617. * Returns: 0 on success, -1 on failure
  4618. *
  4619. * This function creates a new P2P group with the local end as the Group Owner,
  4620. * i.e., without using Group Owner Negotiation.
  4621. */
  4622. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  4623. int freq, int ht40, int vht)
  4624. {
  4625. struct p2p_go_neg_results params;
  4626. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4627. return -1;
  4628. os_free(wpa_s->global->add_psk);
  4629. wpa_s->global->add_psk = NULL;
  4630. /* Make sure we are not running find during connection establishment */
  4631. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  4632. wpas_p2p_stop_find_oper(wpa_s);
  4633. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  4634. if (freq < 0)
  4635. return -1;
  4636. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
  4637. return -1;
  4638. if (params.freq &&
  4639. !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
  4640. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
  4641. ieee80211_is_dfs(params.freq)) {
  4642. /*
  4643. * If freq is a DFS channel and DFS is offloaded to the
  4644. * driver, allow P2P GO to use it.
  4645. */
  4646. wpa_printf(MSG_DEBUG,
  4647. "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
  4648. __func__, params.freq);
  4649. } else {
  4650. wpa_printf(MSG_DEBUG,
  4651. "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
  4652. params.freq);
  4653. return -1;
  4654. }
  4655. }
  4656. p2p_go_params(wpa_s->global->p2p, &params);
  4657. params.persistent_group = persistent_group;
  4658. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  4659. if (wpa_s == NULL)
  4660. return -1;
  4661. wpas_start_wps_go(wpa_s, &params, 0);
  4662. return 0;
  4663. }
  4664. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  4665. struct wpa_ssid *params, int addr_allocated,
  4666. int freq)
  4667. {
  4668. struct wpa_ssid *ssid;
  4669. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  4670. if (wpa_s == NULL)
  4671. return -1;
  4672. wpa_s->p2p_last_4way_hs_fail = NULL;
  4673. wpa_supplicant_ap_deinit(wpa_s);
  4674. ssid = wpa_config_add_network(wpa_s->conf);
  4675. if (ssid == NULL)
  4676. return -1;
  4677. wpa_config_set_network_defaults(ssid);
  4678. ssid->temporary = 1;
  4679. ssid->proto = WPA_PROTO_RSN;
  4680. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  4681. ssid->group_cipher = WPA_CIPHER_CCMP;
  4682. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  4683. ssid->ssid = os_malloc(params->ssid_len);
  4684. if (ssid->ssid == NULL) {
  4685. wpa_config_remove_network(wpa_s->conf, ssid->id);
  4686. return -1;
  4687. }
  4688. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  4689. ssid->ssid_len = params->ssid_len;
  4690. ssid->p2p_group = 1;
  4691. ssid->export_keys = 1;
  4692. if (params->psk_set) {
  4693. os_memcpy(ssid->psk, params->psk, 32);
  4694. ssid->psk_set = 1;
  4695. }
  4696. if (params->passphrase)
  4697. ssid->passphrase = os_strdup(params->passphrase);
  4698. wpa_s->show_group_started = 1;
  4699. wpa_s->p2p_in_invitation = 1;
  4700. wpa_s->p2p_invite_go_freq = freq;
  4701. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  4702. NULL);
  4703. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4704. wpas_p2p_group_formation_timeout,
  4705. wpa_s->parent, NULL);
  4706. wpa_supplicant_select_network(wpa_s, ssid);
  4707. return 0;
  4708. }
  4709. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  4710. struct wpa_ssid *ssid, int addr_allocated,
  4711. int force_freq, int neg_freq, int ht40,
  4712. int vht, const struct p2p_channels *channels,
  4713. int connection_timeout)
  4714. {
  4715. struct p2p_go_neg_results params;
  4716. int go = 0, freq;
  4717. if (ssid->disabled != 2 || ssid->ssid == NULL)
  4718. return -1;
  4719. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  4720. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  4721. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  4722. "already running");
  4723. if (go == 0 &&
  4724. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4725. wpa_s->parent, NULL)) {
  4726. /*
  4727. * This can happen if Invitation Response frame was lost
  4728. * and the peer (GO of a persistent group) tries to
  4729. * invite us again. Reschedule the timeout to avoid
  4730. * terminating the wait for the connection too early
  4731. * since we now know that the peer is still trying to
  4732. * invite us instead of having already started the GO.
  4733. */
  4734. wpa_printf(MSG_DEBUG,
  4735. "P2P: Reschedule group formation timeout since peer is still trying to invite us");
  4736. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4737. wpas_p2p_group_formation_timeout,
  4738. wpa_s->parent, NULL);
  4739. }
  4740. return 0;
  4741. }
  4742. os_free(wpa_s->global->add_psk);
  4743. wpa_s->global->add_psk = NULL;
  4744. /* Make sure we are not running find during connection establishment */
  4745. wpas_p2p_stop_find_oper(wpa_s);
  4746. wpa_s->p2p_fallback_to_go_neg = 0;
  4747. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4748. if (force_freq > 0) {
  4749. freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
  4750. if (freq < 0)
  4751. return -1;
  4752. } else {
  4753. freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
  4754. if (freq < 0 ||
  4755. (freq > 0 && !freq_included(channels, freq)))
  4756. freq = 0;
  4757. }
  4758. } else if (ssid->mode == WPAS_MODE_INFRA) {
  4759. freq = neg_freq;
  4760. if (freq <= 0 || !freq_included(channels, freq)) {
  4761. struct os_reltime now;
  4762. struct wpa_bss *bss =
  4763. wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
  4764. os_get_reltime(&now);
  4765. if (bss &&
  4766. !os_reltime_expired(&now, &bss->last_update, 5) &&
  4767. freq_included(channels, bss->freq))
  4768. freq = bss->freq;
  4769. else
  4770. freq = 0;
  4771. }
  4772. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
  4773. } else {
  4774. return -1;
  4775. }
  4776. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
  4777. return -1;
  4778. params.role_go = 1;
  4779. params.psk_set = ssid->psk_set;
  4780. if (params.psk_set)
  4781. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  4782. if (ssid->passphrase) {
  4783. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  4784. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  4785. "persistent group");
  4786. return -1;
  4787. }
  4788. os_strlcpy(params.passphrase, ssid->passphrase,
  4789. sizeof(params.passphrase));
  4790. }
  4791. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  4792. params.ssid_len = ssid->ssid_len;
  4793. params.persistent_group = 1;
  4794. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  4795. if (wpa_s == NULL)
  4796. return -1;
  4797. p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
  4798. wpa_s->p2p_first_connection_timeout = connection_timeout;
  4799. wpas_start_wps_go(wpa_s, &params, 0);
  4800. return 0;
  4801. }
  4802. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  4803. struct wpabuf *proberesp_ies)
  4804. {
  4805. struct wpa_supplicant *wpa_s = ctx;
  4806. if (wpa_s->ap_iface) {
  4807. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  4808. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  4809. wpabuf_free(beacon_ies);
  4810. wpabuf_free(proberesp_ies);
  4811. return;
  4812. }
  4813. if (beacon_ies) {
  4814. wpabuf_free(hapd->p2p_beacon_ie);
  4815. hapd->p2p_beacon_ie = beacon_ies;
  4816. }
  4817. wpabuf_free(hapd->p2p_probe_resp_ie);
  4818. hapd->p2p_probe_resp_ie = proberesp_ies;
  4819. } else {
  4820. wpabuf_free(beacon_ies);
  4821. wpabuf_free(proberesp_ies);
  4822. }
  4823. wpa_supplicant_ap_update_beacon(wpa_s);
  4824. }
  4825. static void wpas_p2p_idle_update(void *ctx, int idle)
  4826. {
  4827. struct wpa_supplicant *wpa_s = ctx;
  4828. if (!wpa_s->ap_iface)
  4829. return;
  4830. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  4831. if (idle) {
  4832. if (wpa_s->global->p2p_fail_on_wps_complete &&
  4833. wpa_s->p2p_in_provisioning) {
  4834. wpas_p2p_grpform_fail_after_wps(wpa_s);
  4835. return;
  4836. }
  4837. wpas_p2p_set_group_idle_timeout(wpa_s);
  4838. } else
  4839. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  4840. }
  4841. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  4842. struct wpa_ssid *ssid)
  4843. {
  4844. struct p2p_group *group;
  4845. struct p2p_group_config *cfg;
  4846. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4847. return NULL;
  4848. cfg = os_zalloc(sizeof(*cfg));
  4849. if (cfg == NULL)
  4850. return NULL;
  4851. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  4852. cfg->persistent_group = 2;
  4853. else if (ssid->p2p_persistent_group)
  4854. cfg->persistent_group = 1;
  4855. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  4856. if (wpa_s->max_stations &&
  4857. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  4858. cfg->max_clients = wpa_s->max_stations;
  4859. else
  4860. cfg->max_clients = wpa_s->conf->max_num_sta;
  4861. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  4862. cfg->ssid_len = ssid->ssid_len;
  4863. cfg->freq = ssid->frequency;
  4864. cfg->cb_ctx = wpa_s;
  4865. cfg->ie_update = wpas_p2p_ie_update;
  4866. cfg->idle_update = wpas_p2p_idle_update;
  4867. group = p2p_group_init(wpa_s->global->p2p, cfg);
  4868. if (group == NULL)
  4869. os_free(cfg);
  4870. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  4871. p2p_group_notif_formation_done(group);
  4872. wpa_s->p2p_group = group;
  4873. return group;
  4874. }
  4875. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4876. int registrar)
  4877. {
  4878. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4879. if (!wpa_s->p2p_in_provisioning) {
  4880. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  4881. "provisioning not in progress");
  4882. return;
  4883. }
  4884. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4885. u8 go_dev_addr[ETH_ALEN];
  4886. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  4887. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4888. ssid->ssid_len);
  4889. /* Clear any stored provisioning info */
  4890. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  4891. }
  4892. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  4893. NULL);
  4894. wpa_s->p2p_go_group_formation_completed = 1;
  4895. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  4896. /*
  4897. * Use a separate timeout for initial data connection to
  4898. * complete to allow the group to be removed automatically if
  4899. * something goes wrong in this step before the P2P group idle
  4900. * timeout mechanism is taken into use.
  4901. */
  4902. wpa_dbg(wpa_s, MSG_DEBUG,
  4903. "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
  4904. P2P_MAX_INITIAL_CONN_WAIT);
  4905. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  4906. wpas_p2p_group_formation_timeout,
  4907. wpa_s->parent, NULL);
  4908. } else if (ssid) {
  4909. /*
  4910. * Use a separate timeout for initial data connection to
  4911. * complete to allow the group to be removed automatically if
  4912. * the client does not complete data connection successfully.
  4913. */
  4914. wpa_dbg(wpa_s, MSG_DEBUG,
  4915. "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
  4916. P2P_MAX_INITIAL_CONN_WAIT_GO);
  4917. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
  4918. wpas_p2p_group_formation_timeout,
  4919. wpa_s->parent, NULL);
  4920. /*
  4921. * Complete group formation on first successful data connection
  4922. */
  4923. wpa_s->p2p_go_group_formation_completed = 0;
  4924. }
  4925. if (wpa_s->global->p2p)
  4926. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  4927. wpas_group_formation_completed(wpa_s, 1);
  4928. }
  4929. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  4930. struct wps_event_fail *fail)
  4931. {
  4932. if (!wpa_s->p2p_in_provisioning) {
  4933. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  4934. "provisioning not in progress");
  4935. return;
  4936. }
  4937. if (wpa_s->go_params) {
  4938. p2p_clear_provisioning_info(
  4939. wpa_s->global->p2p,
  4940. wpa_s->go_params->peer_device_addr);
  4941. }
  4942. wpas_notify_p2p_wps_failed(wpa_s, fail);
  4943. if (wpa_s == wpa_s->global->p2p_group_formation) {
  4944. /*
  4945. * Allow some time for the failed WPS negotiation exchange to
  4946. * complete, but remove the group since group formation cannot
  4947. * succeed after provisioning failure.
  4948. */
  4949. wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
  4950. wpa_s->global->p2p_fail_on_wps_complete = 1;
  4951. eloop_deplete_timeout(0, 50000,
  4952. wpas_p2p_group_formation_timeout,
  4953. wpa_s->parent, NULL);
  4954. }
  4955. }
  4956. int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  4957. {
  4958. if (!wpa_s->global->p2p_fail_on_wps_complete ||
  4959. !wpa_s->p2p_in_provisioning)
  4960. return 0;
  4961. wpas_p2p_grpform_fail_after_wps(wpa_s);
  4962. return 1;
  4963. }
  4964. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4965. const char *config_method,
  4966. enum wpas_p2p_prov_disc_use use,
  4967. struct p2ps_provision *p2ps_prov)
  4968. {
  4969. u16 config_methods;
  4970. wpa_s->global->pending_p2ps_group = 0;
  4971. wpa_s->p2p_fallback_to_go_neg = 0;
  4972. wpa_s->pending_pd_use = NORMAL_PD;
  4973. if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
  4974. p2ps_prov->conncap = p2ps_group_capability(
  4975. wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
  4976. wpa_printf(MSG_DEBUG,
  4977. "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
  4978. __func__, p2ps_prov->conncap,
  4979. p2ps_prov->adv_id, p2ps_prov->conncap,
  4980. p2ps_prov->status, p2ps_prov->info);
  4981. config_methods = 0;
  4982. } else if (os_strncmp(config_method, "display", 7) == 0)
  4983. config_methods = WPS_CONFIG_DISPLAY;
  4984. else if (os_strncmp(config_method, "keypad", 6) == 0)
  4985. config_methods = WPS_CONFIG_KEYPAD;
  4986. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  4987. os_strncmp(config_method, "pushbutton", 10) == 0)
  4988. config_methods = WPS_CONFIG_PUSHBUTTON;
  4989. else {
  4990. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4991. os_free(p2ps_prov);
  4992. return -1;
  4993. }
  4994. if (use == WPAS_P2P_PD_AUTO) {
  4995. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4996. wpa_s->pending_pd_config_methods = config_methods;
  4997. wpa_s->p2p_auto_pd = 1;
  4998. wpa_s->p2p_auto_join = 0;
  4999. wpa_s->pending_pd_before_join = 0;
  5000. wpa_s->auto_pd_scan_retry = 0;
  5001. wpas_p2p_stop_find(wpa_s);
  5002. wpa_s->p2p_join_scan_count = 0;
  5003. os_get_reltime(&wpa_s->p2p_auto_started);
  5004. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  5005. wpa_s->p2p_auto_started.sec,
  5006. wpa_s->p2p_auto_started.usec);
  5007. wpas_p2p_join_scan(wpa_s, NULL);
  5008. return 0;
  5009. }
  5010. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
  5011. os_free(p2ps_prov);
  5012. return -1;
  5013. }
  5014. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
  5015. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  5016. 0, 1);
  5017. }
  5018. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  5019. char *end)
  5020. {
  5021. return p2p_scan_result_text(ies, ies_len, buf, end);
  5022. }
  5023. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  5024. {
  5025. if (!offchannel_pending_action_tx(wpa_s))
  5026. return;
  5027. wpas_p2p_action_tx_clear(wpa_s);
  5028. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  5029. "operation request");
  5030. offchannel_clear_pending_action_tx(wpa_s);
  5031. }
  5032. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  5033. enum p2p_discovery_type type,
  5034. unsigned int num_req_dev_types, const u8 *req_dev_types,
  5035. const u8 *dev_id, unsigned int search_delay,
  5036. u8 seek_cnt, const char **seek_string, int freq)
  5037. {
  5038. wpas_p2p_clear_pending_action_tx(wpa_s);
  5039. wpa_s->p2p_long_listen = 0;
  5040. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  5041. wpa_s->p2p_in_provisioning)
  5042. return -1;
  5043. wpa_supplicant_cancel_sched_scan(wpa_s);
  5044. return p2p_find(wpa_s->global->p2p, timeout, type,
  5045. num_req_dev_types, req_dev_types, dev_id,
  5046. search_delay, seek_cnt, seek_string, freq);
  5047. }
  5048. static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
  5049. struct wpa_scan_results *scan_res)
  5050. {
  5051. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5052. if (wpa_s->p2p_scan_work) {
  5053. struct wpa_radio_work *work = wpa_s->p2p_scan_work;
  5054. wpa_s->p2p_scan_work = NULL;
  5055. radio_work_done(work);
  5056. }
  5057. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5058. return;
  5059. /*
  5060. * Indicate that results have been processed so that the P2P module can
  5061. * continue pending tasks.
  5062. */
  5063. p2p_scan_res_handled(wpa_s->global->p2p);
  5064. }
  5065. static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  5066. {
  5067. wpas_p2p_clear_pending_action_tx(wpa_s);
  5068. wpa_s->p2p_long_listen = 0;
  5069. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5070. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  5071. if (wpa_s->global->p2p)
  5072. p2p_stop_find(wpa_s->global->p2p);
  5073. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
  5074. wpa_printf(MSG_DEBUG,
  5075. "P2P: Do not consider the scan results after stop_find");
  5076. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
  5077. }
  5078. }
  5079. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  5080. {
  5081. wpas_p2p_stop_find_oper(wpa_s);
  5082. if (!wpa_s->global->pending_group_iface_for_p2ps)
  5083. wpas_p2p_remove_pending_group_interface(wpa_s);
  5084. }
  5085. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  5086. {
  5087. struct wpa_supplicant *wpa_s = eloop_ctx;
  5088. wpa_s->p2p_long_listen = 0;
  5089. }
  5090. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  5091. {
  5092. int res;
  5093. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5094. return -1;
  5095. wpa_supplicant_cancel_sched_scan(wpa_s);
  5096. wpas_p2p_clear_pending_action_tx(wpa_s);
  5097. if (timeout == 0) {
  5098. /*
  5099. * This is a request for unlimited Listen state. However, at
  5100. * least for now, this is mapped to a Listen state for one
  5101. * hour.
  5102. */
  5103. timeout = 3600;
  5104. }
  5105. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  5106. wpa_s->p2p_long_listen = 0;
  5107. /*
  5108. * Stop previous find/listen operation to avoid trying to request a new
  5109. * remain-on-channel operation while the driver is still running the
  5110. * previous one.
  5111. */
  5112. if (wpa_s->global->p2p)
  5113. p2p_stop_find(wpa_s->global->p2p);
  5114. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  5115. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  5116. wpa_s->p2p_long_listen = timeout * 1000;
  5117. eloop_register_timeout(timeout, 0,
  5118. wpas_p2p_long_listen_timeout,
  5119. wpa_s, NULL);
  5120. }
  5121. return res;
  5122. }
  5123. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  5124. u8 *buf, size_t len, int p2p_group)
  5125. {
  5126. struct wpabuf *p2p_ie;
  5127. int ret;
  5128. if (wpa_s->global->p2p_disabled)
  5129. return -1;
  5130. /*
  5131. * Advertize mandatory cross connection capability even on
  5132. * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
  5133. */
  5134. if (wpa_s->conf->p2p_disabled && p2p_group)
  5135. return -1;
  5136. if (wpa_s->global->p2p == NULL)
  5137. return -1;
  5138. if (bss == NULL)
  5139. return -1;
  5140. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  5141. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  5142. p2p_group, p2p_ie);
  5143. wpabuf_free(p2p_ie);
  5144. return ret;
  5145. }
  5146. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  5147. const u8 *dst, const u8 *bssid,
  5148. const u8 *ie, size_t ie_len, int ssi_signal)
  5149. {
  5150. if (wpa_s->global->p2p_disabled)
  5151. return 0;
  5152. if (wpa_s->global->p2p == NULL)
  5153. return 0;
  5154. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  5155. ie, ie_len)) {
  5156. case P2P_PREQ_NOT_P2P:
  5157. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  5158. ssi_signal);
  5159. /* fall through */
  5160. case P2P_PREQ_MALFORMED:
  5161. case P2P_PREQ_NOT_LISTEN:
  5162. case P2P_PREQ_NOT_PROCESSED:
  5163. default: /* make gcc happy */
  5164. return 0;
  5165. case P2P_PREQ_PROCESSED:
  5166. return 1;
  5167. }
  5168. }
  5169. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  5170. const u8 *sa, const u8 *bssid,
  5171. u8 category, const u8 *data, size_t len, int freq)
  5172. {
  5173. if (wpa_s->global->p2p_disabled)
  5174. return;
  5175. if (wpa_s->global->p2p == NULL)
  5176. return;
  5177. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  5178. freq);
  5179. }
  5180. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  5181. {
  5182. if (wpa_s->global->p2p_disabled)
  5183. return;
  5184. if (wpa_s->global->p2p == NULL)
  5185. return;
  5186. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  5187. }
  5188. static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  5189. {
  5190. p2p_group_deinit(wpa_s->p2p_group);
  5191. wpa_s->p2p_group = NULL;
  5192. wpa_s->ap_configured_cb = NULL;
  5193. wpa_s->ap_configured_cb_ctx = NULL;
  5194. wpa_s->ap_configured_cb_data = NULL;
  5195. wpa_s->connect_without_scan = NULL;
  5196. }
  5197. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  5198. {
  5199. wpa_s->p2p_long_listen = 0;
  5200. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5201. return -1;
  5202. return p2p_reject(wpa_s->global->p2p, addr);
  5203. }
  5204. /* Invite to reinvoke a persistent group */
  5205. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  5206. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  5207. int ht40, int vht, int pref_freq)
  5208. {
  5209. enum p2p_invite_role role;
  5210. u8 *bssid = NULL;
  5211. int force_freq = 0;
  5212. int res;
  5213. int no_pref_freq_given = pref_freq == 0;
  5214. wpa_s->global->p2p_invite_group = NULL;
  5215. if (peer_addr)
  5216. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  5217. else
  5218. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5219. wpa_s->p2p_persistent_go_freq = freq;
  5220. wpa_s->p2p_go_ht40 = !!ht40;
  5221. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5222. role = P2P_INVITE_ROLE_GO;
  5223. if (peer_addr == NULL) {
  5224. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  5225. "address in invitation command");
  5226. return -1;
  5227. }
  5228. if (wpas_p2p_create_iface(wpa_s)) {
  5229. if (wpas_p2p_add_group_interface(wpa_s,
  5230. WPA_IF_P2P_GO) < 0) {
  5231. wpa_printf(MSG_ERROR, "P2P: Failed to "
  5232. "allocate a new interface for the "
  5233. "group");
  5234. return -1;
  5235. }
  5236. bssid = wpa_s->pending_interface_addr;
  5237. } else
  5238. bssid = wpa_s->own_addr;
  5239. } else {
  5240. role = P2P_INVITE_ROLE_CLIENT;
  5241. peer_addr = ssid->bssid;
  5242. }
  5243. wpa_s->pending_invite_ssid_id = ssid->id;
  5244. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5245. role == P2P_INVITE_ROLE_GO);
  5246. if (res)
  5247. return res;
  5248. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5249. return -1;
  5250. if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
  5251. no_pref_freq_given && pref_freq > 0 &&
  5252. wpa_s->num_multichan_concurrent > 1 &&
  5253. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  5254. wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
  5255. pref_freq);
  5256. pref_freq = 0;
  5257. }
  5258. /*
  5259. * Stop any find/listen operations before invitation and possibly
  5260. * connection establishment.
  5261. */
  5262. wpas_p2p_stop_find_oper(wpa_s);
  5263. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5264. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  5265. 1, pref_freq, -1);
  5266. }
  5267. /* Invite to join an active group */
  5268. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  5269. const u8 *peer_addr, const u8 *go_dev_addr)
  5270. {
  5271. struct wpa_global *global = wpa_s->global;
  5272. enum p2p_invite_role role;
  5273. u8 *bssid = NULL;
  5274. struct wpa_ssid *ssid;
  5275. int persistent;
  5276. int freq = 0, force_freq = 0, pref_freq = 0;
  5277. int res;
  5278. wpa_s->p2p_persistent_go_freq = 0;
  5279. wpa_s->p2p_go_ht40 = 0;
  5280. wpa_s->p2p_go_vht = 0;
  5281. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5282. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  5283. break;
  5284. }
  5285. if (wpa_s == NULL) {
  5286. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  5287. return -1;
  5288. }
  5289. ssid = wpa_s->current_ssid;
  5290. if (ssid == NULL) {
  5291. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  5292. "invitation");
  5293. return -1;
  5294. }
  5295. wpa_s->global->p2p_invite_group = wpa_s;
  5296. persistent = ssid->p2p_persistent_group &&
  5297. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  5298. ssid->ssid, ssid->ssid_len);
  5299. if (ssid->mode == WPAS_MODE_P2P_GO) {
  5300. role = P2P_INVITE_ROLE_ACTIVE_GO;
  5301. bssid = wpa_s->own_addr;
  5302. if (go_dev_addr == NULL)
  5303. go_dev_addr = wpa_s->global->p2p_dev_addr;
  5304. freq = ssid->frequency;
  5305. } else {
  5306. role = P2P_INVITE_ROLE_CLIENT;
  5307. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  5308. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  5309. "invite to current group");
  5310. return -1;
  5311. }
  5312. bssid = wpa_s->bssid;
  5313. if (go_dev_addr == NULL &&
  5314. !is_zero_ether_addr(wpa_s->go_dev_addr))
  5315. go_dev_addr = wpa_s->go_dev_addr;
  5316. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5317. (int) wpa_s->assoc_freq;
  5318. }
  5319. wpa_s->parent->pending_invite_ssid_id = -1;
  5320. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5321. return -1;
  5322. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
  5323. role == P2P_INVITE_ROLE_ACTIVE_GO);
  5324. if (res)
  5325. return res;
  5326. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  5327. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  5328. ssid->ssid, ssid->ssid_len, force_freq,
  5329. go_dev_addr, persistent, pref_freq, -1);
  5330. }
  5331. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  5332. {
  5333. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5334. u8 go_dev_addr[ETH_ALEN];
  5335. int network_id = -1;
  5336. int persistent;
  5337. int freq;
  5338. u8 ip[3 * 4];
  5339. char ip_addr[100];
  5340. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  5341. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5342. wpa_s->parent, NULL);
  5343. }
  5344. if (!wpa_s->show_group_started || !ssid)
  5345. return;
  5346. wpa_s->show_group_started = 0;
  5347. os_memset(go_dev_addr, 0, ETH_ALEN);
  5348. if (ssid->bssid_set)
  5349. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  5350. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  5351. ssid->ssid_len);
  5352. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  5353. if (wpa_s->global->p2p_group_formation == wpa_s)
  5354. wpa_s->global->p2p_group_formation = NULL;
  5355. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  5356. (int) wpa_s->assoc_freq;
  5357. ip_addr[0] = '\0';
  5358. if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
  5359. int res;
  5360. res = os_snprintf(ip_addr, sizeof(ip_addr),
  5361. " ip_addr=%u.%u.%u.%u "
  5362. "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
  5363. ip[0], ip[1], ip[2], ip[3],
  5364. ip[4], ip[5], ip[6], ip[7],
  5365. ip[8], ip[9], ip[10], ip[11]);
  5366. if (os_snprintf_error(sizeof(ip_addr), res))
  5367. ip_addr[0] = '\0';
  5368. }
  5369. wpas_p2p_group_started(wpa_s, 0, ssid, freq,
  5370. ssid->passphrase == NULL && ssid->psk_set ?
  5371. ssid->psk : NULL,
  5372. ssid->passphrase, go_dev_addr, persistent,
  5373. ip_addr);
  5374. if (persistent)
  5375. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  5376. ssid, go_dev_addr);
  5377. if (network_id < 0)
  5378. network_id = ssid->id;
  5379. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  5380. }
  5381. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  5382. u32 interval1, u32 duration2, u32 interval2)
  5383. {
  5384. int ret;
  5385. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5386. return -1;
  5387. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  5388. wpa_s->current_ssid == NULL ||
  5389. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  5390. return -1;
  5391. ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  5392. wpa_s->own_addr, wpa_s->assoc_freq,
  5393. duration1, interval1, duration2, interval2);
  5394. if (ret == 0)
  5395. wpa_s->waiting_presence_resp = 1;
  5396. return ret;
  5397. }
  5398. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  5399. unsigned int interval)
  5400. {
  5401. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5402. return -1;
  5403. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  5404. }
  5405. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  5406. {
  5407. if (wpa_s->current_ssid == NULL) {
  5408. /*
  5409. * current_ssid can be cleared when P2P client interface gets
  5410. * disconnected, so assume this interface was used as P2P
  5411. * client.
  5412. */
  5413. return 1;
  5414. }
  5415. return wpa_s->current_ssid->p2p_group &&
  5416. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  5417. }
  5418. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  5419. {
  5420. struct wpa_supplicant *wpa_s = eloop_ctx;
  5421. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  5422. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  5423. "disabled");
  5424. return;
  5425. }
  5426. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  5427. "group");
  5428. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  5429. }
  5430. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  5431. {
  5432. int timeout;
  5433. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  5434. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  5435. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  5436. return;
  5437. timeout = wpa_s->conf->p2p_group_idle;
  5438. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  5439. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  5440. timeout = P2P_MAX_CLIENT_IDLE;
  5441. if (timeout == 0)
  5442. return;
  5443. if (timeout < 0) {
  5444. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  5445. timeout = 0; /* special client mode no-timeout */
  5446. else
  5447. return;
  5448. }
  5449. if (wpa_s->p2p_in_provisioning) {
  5450. /*
  5451. * Use the normal group formation timeout during the
  5452. * provisioning phase to avoid terminating this process too
  5453. * early due to group idle timeout.
  5454. */
  5455. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5456. "during provisioning");
  5457. return;
  5458. }
  5459. if (wpa_s->show_group_started) {
  5460. /*
  5461. * Use the normal group formation timeout between the end of
  5462. * the provisioning phase and completion of 4-way handshake to
  5463. * avoid terminating this process too early due to group idle
  5464. * timeout.
  5465. */
  5466. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  5467. "while waiting for initial 4-way handshake to "
  5468. "complete");
  5469. return;
  5470. }
  5471. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  5472. timeout);
  5473. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  5474. wpa_s, NULL);
  5475. }
  5476. /* Returns 1 if the interface was removed */
  5477. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5478. u16 reason_code, const u8 *ie, size_t ie_len,
  5479. int locally_generated)
  5480. {
  5481. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5482. return 0;
  5483. if (!locally_generated)
  5484. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5485. ie_len);
  5486. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  5487. wpa_s->current_ssid &&
  5488. wpa_s->current_ssid->p2p_group &&
  5489. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  5490. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  5491. "session is ending");
  5492. if (wpas_p2p_group_delete(wpa_s,
  5493. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  5494. > 0)
  5495. return 1;
  5496. }
  5497. return 0;
  5498. }
  5499. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5500. u16 reason_code, const u8 *ie, size_t ie_len,
  5501. int locally_generated)
  5502. {
  5503. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5504. return;
  5505. if (!locally_generated)
  5506. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  5507. ie_len);
  5508. }
  5509. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  5510. {
  5511. struct p2p_data *p2p = wpa_s->global->p2p;
  5512. if (p2p == NULL)
  5513. return;
  5514. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  5515. return;
  5516. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  5517. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  5518. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  5519. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  5520. if (wpa_s->wps &&
  5521. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  5522. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  5523. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  5524. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  5525. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  5526. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  5527. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  5528. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  5529. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  5530. }
  5531. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  5532. p2p_set_sec_dev_types(p2p,
  5533. (void *) wpa_s->conf->sec_device_type,
  5534. wpa_s->conf->num_sec_device_types);
  5535. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  5536. int i;
  5537. p2p_remove_wps_vendor_extensions(p2p);
  5538. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  5539. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  5540. continue;
  5541. p2p_add_wps_vendor_extension(
  5542. p2p, wpa_s->conf->wps_vendor_ext[i]);
  5543. }
  5544. }
  5545. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  5546. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  5547. char country[3];
  5548. country[0] = wpa_s->conf->country[0];
  5549. country[1] = wpa_s->conf->country[1];
  5550. country[2] = 0x04;
  5551. p2p_set_country(p2p, country);
  5552. }
  5553. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  5554. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  5555. wpa_s->conf->p2p_ssid_postfix ?
  5556. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  5557. 0);
  5558. }
  5559. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  5560. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  5561. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  5562. u8 reg_class, channel;
  5563. int ret;
  5564. unsigned int r;
  5565. u8 channel_forced;
  5566. if (wpa_s->conf->p2p_listen_reg_class &&
  5567. wpa_s->conf->p2p_listen_channel) {
  5568. reg_class = wpa_s->conf->p2p_listen_reg_class;
  5569. channel = wpa_s->conf->p2p_listen_channel;
  5570. channel_forced = 1;
  5571. } else {
  5572. reg_class = 81;
  5573. /*
  5574. * Pick one of the social channels randomly as the
  5575. * listen channel.
  5576. */
  5577. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  5578. channel = 1;
  5579. else
  5580. channel = 1 + (r % 3) * 5;
  5581. channel_forced = 0;
  5582. }
  5583. ret = p2p_set_listen_channel(p2p, reg_class, channel,
  5584. channel_forced);
  5585. if (ret)
  5586. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  5587. "failed: %d", ret);
  5588. }
  5589. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  5590. u8 op_reg_class, op_channel, cfg_op_channel;
  5591. int ret = 0;
  5592. unsigned int r;
  5593. if (wpa_s->conf->p2p_oper_reg_class &&
  5594. wpa_s->conf->p2p_oper_channel) {
  5595. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  5596. op_channel = wpa_s->conf->p2p_oper_channel;
  5597. cfg_op_channel = 1;
  5598. } else {
  5599. op_reg_class = 81;
  5600. /*
  5601. * Use random operation channel from (1, 6, 11)
  5602. *if no other preference is indicated.
  5603. */
  5604. if (os_get_random((u8 *) &r, sizeof(r)) < 0)
  5605. op_channel = 1;
  5606. else
  5607. op_channel = 1 + (r % 3) * 5;
  5608. cfg_op_channel = 0;
  5609. }
  5610. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  5611. cfg_op_channel);
  5612. if (ret)
  5613. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  5614. "failed: %d", ret);
  5615. }
  5616. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  5617. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  5618. wpa_s->conf->p2p_pref_chan) < 0) {
  5619. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  5620. "update failed");
  5621. }
  5622. if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
  5623. wpa_printf(MSG_ERROR, "P2P: No GO channel list "
  5624. "update failed");
  5625. }
  5626. }
  5627. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
  5628. p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
  5629. }
  5630. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  5631. int duration)
  5632. {
  5633. if (!wpa_s->ap_iface)
  5634. return -1;
  5635. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  5636. duration);
  5637. }
  5638. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  5639. {
  5640. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5641. return -1;
  5642. wpa_s->global->cross_connection = enabled;
  5643. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  5644. if (!enabled) {
  5645. struct wpa_supplicant *iface;
  5646. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  5647. {
  5648. if (iface->cross_connect_enabled == 0)
  5649. continue;
  5650. iface->cross_connect_enabled = 0;
  5651. iface->cross_connect_in_use = 0;
  5652. wpa_msg_global(iface->parent, MSG_INFO,
  5653. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  5654. iface->ifname,
  5655. iface->cross_connect_uplink);
  5656. }
  5657. }
  5658. return 0;
  5659. }
  5660. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  5661. {
  5662. struct wpa_supplicant *iface;
  5663. if (!uplink->global->cross_connection)
  5664. return;
  5665. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  5666. if (!iface->cross_connect_enabled)
  5667. continue;
  5668. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  5669. 0)
  5670. continue;
  5671. if (iface->ap_iface == NULL)
  5672. continue;
  5673. if (iface->cross_connect_in_use)
  5674. continue;
  5675. iface->cross_connect_in_use = 1;
  5676. wpa_msg_global(iface->parent, MSG_INFO,
  5677. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  5678. iface->ifname, iface->cross_connect_uplink);
  5679. }
  5680. }
  5681. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  5682. {
  5683. struct wpa_supplicant *iface;
  5684. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  5685. if (!iface->cross_connect_enabled)
  5686. continue;
  5687. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  5688. 0)
  5689. continue;
  5690. if (!iface->cross_connect_in_use)
  5691. continue;
  5692. wpa_msg_global(iface->parent, MSG_INFO,
  5693. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  5694. iface->ifname, iface->cross_connect_uplink);
  5695. iface->cross_connect_in_use = 0;
  5696. }
  5697. }
  5698. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  5699. {
  5700. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  5701. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  5702. wpa_s->cross_connect_disallowed)
  5703. wpas_p2p_disable_cross_connect(wpa_s);
  5704. else
  5705. wpas_p2p_enable_cross_connect(wpa_s);
  5706. if (!wpa_s->ap_iface &&
  5707. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  5708. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  5709. }
  5710. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  5711. {
  5712. wpas_p2p_disable_cross_connect(wpa_s);
  5713. if (!wpa_s->ap_iface &&
  5714. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  5715. wpa_s, NULL))
  5716. wpas_p2p_set_group_idle_timeout(wpa_s);
  5717. }
  5718. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  5719. {
  5720. struct wpa_supplicant *iface;
  5721. if (!wpa_s->global->cross_connection)
  5722. return;
  5723. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  5724. if (iface == wpa_s)
  5725. continue;
  5726. if (iface->drv_flags &
  5727. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  5728. continue;
  5729. if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
  5730. iface != wpa_s->parent)
  5731. continue;
  5732. wpa_s->cross_connect_enabled = 1;
  5733. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  5734. sizeof(wpa_s->cross_connect_uplink));
  5735. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  5736. "%s to %s whenever uplink is available",
  5737. wpa_s->ifname, wpa_s->cross_connect_uplink);
  5738. if (iface->ap_iface || iface->current_ssid == NULL ||
  5739. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  5740. iface->cross_connect_disallowed ||
  5741. iface->wpa_state != WPA_COMPLETED)
  5742. break;
  5743. wpa_s->cross_connect_in_use = 1;
  5744. wpa_msg_global(wpa_s->parent, MSG_INFO,
  5745. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  5746. wpa_s->ifname, wpa_s->cross_connect_uplink);
  5747. break;
  5748. }
  5749. }
  5750. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  5751. {
  5752. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  5753. !wpa_s->p2p_in_provisioning)
  5754. return 0; /* not P2P client operation */
  5755. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  5756. "session overlap");
  5757. if (wpa_s != wpa_s->parent)
  5758. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  5759. wpas_p2p_group_formation_failed(wpa_s);
  5760. return 1;
  5761. }
  5762. void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  5763. {
  5764. struct wpa_supplicant *wpa_s = eloop_ctx;
  5765. wpas_p2p_notif_pbc_overlap(wpa_s);
  5766. }
  5767. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  5768. {
  5769. struct p2p_channels chan, cli_chan;
  5770. struct wpa_supplicant *ifs;
  5771. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  5772. return;
  5773. os_memset(&chan, 0, sizeof(chan));
  5774. os_memset(&cli_chan, 0, sizeof(cli_chan));
  5775. if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
  5776. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  5777. "channel list");
  5778. return;
  5779. }
  5780. p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
  5781. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  5782. int freq;
  5783. if (!ifs->current_ssid ||
  5784. !ifs->current_ssid->p2p_group ||
  5785. (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
  5786. ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
  5787. continue;
  5788. freq = ifs->current_ssid->frequency;
  5789. if (freq_included(&chan, freq)) {
  5790. wpa_dbg(ifs, MSG_DEBUG,
  5791. "P2P GO operating frequency %d MHz in valid range",
  5792. freq);
  5793. continue;
  5794. }
  5795. wpa_dbg(ifs, MSG_DEBUG,
  5796. "P2P GO operating in invalid frequency %d MHz", freq);
  5797. /* TODO: Consider using CSA or removing the group within
  5798. * wpa_supplicant */
  5799. wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
  5800. }
  5801. }
  5802. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  5803. struct wpa_scan_results *scan_res)
  5804. {
  5805. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  5806. }
  5807. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  5808. {
  5809. struct wpa_global *global = wpa_s->global;
  5810. int found = 0;
  5811. const u8 *peer;
  5812. if (global->p2p == NULL)
  5813. return -1;
  5814. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  5815. if (wpa_s->pending_interface_name[0] &&
  5816. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  5817. found = 1;
  5818. peer = p2p_get_go_neg_peer(global->p2p);
  5819. if (peer) {
  5820. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  5821. MACSTR, MAC2STR(peer));
  5822. p2p_unauthorize(global->p2p, peer);
  5823. found = 1;
  5824. }
  5825. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  5826. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  5827. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  5828. found = 1;
  5829. }
  5830. if (wpa_s->pending_pd_before_join) {
  5831. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  5832. wpa_s->pending_pd_before_join = 0;
  5833. found = 1;
  5834. }
  5835. wpas_p2p_stop_find(wpa_s);
  5836. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5837. if (wpa_s == global->p2p_group_formation &&
  5838. (wpa_s->p2p_in_provisioning ||
  5839. wpa_s->parent->pending_interface_type ==
  5840. WPA_IF_P2P_CLIENT)) {
  5841. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  5842. "formation found - cancelling",
  5843. wpa_s->ifname);
  5844. found = 1;
  5845. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5846. wpa_s->parent, NULL);
  5847. if (wpa_s->p2p_in_provisioning) {
  5848. wpas_group_formation_completed(wpa_s, 0);
  5849. break;
  5850. }
  5851. wpas_p2p_group_delete(wpa_s,
  5852. P2P_GROUP_REMOVAL_REQUESTED);
  5853. break;
  5854. } else if (wpa_s->p2p_in_invitation) {
  5855. wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
  5856. wpa_s->ifname);
  5857. found = 1;
  5858. wpas_p2p_group_formation_failed(wpa_s);
  5859. }
  5860. }
  5861. if (!found) {
  5862. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  5863. return -1;
  5864. }
  5865. return 0;
  5866. }
  5867. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  5868. {
  5869. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  5870. return;
  5871. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  5872. "being available anymore");
  5873. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  5874. }
  5875. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  5876. int freq_24, int freq_5, int freq_overall)
  5877. {
  5878. struct p2p_data *p2p = wpa_s->global->p2p;
  5879. if (p2p == NULL)
  5880. return;
  5881. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  5882. }
  5883. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  5884. {
  5885. u8 peer[ETH_ALEN];
  5886. struct p2p_data *p2p = wpa_s->global->p2p;
  5887. if (p2p == NULL)
  5888. return -1;
  5889. if (hwaddr_aton(addr, peer))
  5890. return -1;
  5891. return p2p_unauthorize(p2p, peer);
  5892. }
  5893. /**
  5894. * wpas_p2p_disconnect - Disconnect from a P2P Group
  5895. * @wpa_s: Pointer to wpa_supplicant data
  5896. * Returns: 0 on success, -1 on failure
  5897. *
  5898. * This can be used to disconnect from a group in which the local end is a P2P
  5899. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  5900. * virtual network interface was created for this group, that interface will be
  5901. * removed. Otherwise, only the configured P2P group network will be removed
  5902. * from the interface.
  5903. */
  5904. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  5905. {
  5906. if (wpa_s == NULL)
  5907. return -1;
  5908. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  5909. -1 : 0;
  5910. }
  5911. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  5912. {
  5913. int ret;
  5914. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5915. return 0;
  5916. ret = p2p_in_progress(wpa_s->global->p2p);
  5917. if (ret == 0) {
  5918. /*
  5919. * Check whether there is an ongoing WPS provisioning step (or
  5920. * other parts of group formation) on another interface since
  5921. * p2p_in_progress() does not report this to avoid issues for
  5922. * scans during such provisioning step.
  5923. */
  5924. if (wpa_s->global->p2p_group_formation &&
  5925. wpa_s->global->p2p_group_formation != wpa_s) {
  5926. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  5927. "in group formation",
  5928. wpa_s->global->p2p_group_formation->ifname);
  5929. ret = 1;
  5930. }
  5931. }
  5932. if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
  5933. struct os_reltime now;
  5934. os_get_reltime(&now);
  5935. if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
  5936. P2P_MAX_INITIAL_CONN_WAIT_GO)) {
  5937. /* Wait for the first client has expired */
  5938. wpa_s->global->p2p_go_wait_client.sec = 0;
  5939. } else {
  5940. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
  5941. ret = 1;
  5942. }
  5943. }
  5944. return ret;
  5945. }
  5946. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  5947. struct wpa_ssid *ssid)
  5948. {
  5949. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  5950. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  5951. wpa_s->parent, NULL) > 0) {
  5952. /**
  5953. * Remove the network by scheduling the group formation
  5954. * timeout to happen immediately. The teardown code
  5955. * needs to be scheduled to run asynch later so that we
  5956. * don't delete data from under ourselves unexpectedly.
  5957. * Calling wpas_p2p_group_formation_timeout directly
  5958. * causes a series of crashes in WPS failure scenarios.
  5959. */
  5960. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  5961. "P2P group network getting removed");
  5962. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  5963. wpa_s->parent, NULL);
  5964. }
  5965. }
  5966. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  5967. const u8 *addr, const u8 *ssid,
  5968. size_t ssid_len)
  5969. {
  5970. struct wpa_ssid *s;
  5971. size_t i;
  5972. for (s = wpa_s->conf->ssid; s; s = s->next) {
  5973. if (s->disabled != 2)
  5974. continue;
  5975. if (ssid &&
  5976. (ssid_len != s->ssid_len ||
  5977. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  5978. continue;
  5979. if (addr == NULL) {
  5980. if (s->mode == WPAS_MODE_P2P_GO)
  5981. return s;
  5982. continue;
  5983. }
  5984. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  5985. return s; /* peer is GO in the persistent group */
  5986. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  5987. continue;
  5988. for (i = 0; i < s->num_p2p_clients; i++) {
  5989. if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
  5990. addr, ETH_ALEN) == 0)
  5991. return s; /* peer is P2P client in persistent
  5992. * group */
  5993. }
  5994. }
  5995. return NULL;
  5996. }
  5997. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  5998. const u8 *addr)
  5999. {
  6000. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  6001. wpa_s->parent, NULL) > 0) {
  6002. /*
  6003. * This can happen if WPS provisioning step is not terminated
  6004. * cleanly (e.g., P2P Client does not send WSC_Done). Since the
  6005. * peer was able to connect, there is no need to time out group
  6006. * formation after this, though. In addition, this is used with
  6007. * the initial connection wait on the GO as a separate formation
  6008. * timeout and as such, expected to be hit after the initial WPS
  6009. * provisioning step.
  6010. */
  6011. wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
  6012. if (!wpa_s->p2p_go_group_formation_completed &&
  6013. !wpa_s->group_formation_reported) {
  6014. /*
  6015. * GO has not yet notified group formation success since
  6016. * the WPS step was not completed cleanly. Do that
  6017. * notification now since the P2P Client was able to
  6018. * connect and as such, must have received the
  6019. * credential from the WPS step.
  6020. */
  6021. if (wpa_s->global->p2p)
  6022. p2p_wps_success_cb(wpa_s->global->p2p, addr);
  6023. wpas_group_formation_completed(wpa_s, 1);
  6024. }
  6025. }
  6026. if (!wpa_s->p2p_go_group_formation_completed) {
  6027. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
  6028. wpa_s->p2p_go_group_formation_completed = 1;
  6029. wpa_s->global->p2p_group_formation = NULL;
  6030. wpa_s->p2p_in_provisioning = 0;
  6031. wpa_s->p2p_in_invitation = 0;
  6032. }
  6033. wpa_s->global->p2p_go_wait_client.sec = 0;
  6034. if (addr == NULL)
  6035. return;
  6036. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  6037. }
  6038. static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  6039. int group_added)
  6040. {
  6041. struct wpa_supplicant *group = wpa_s;
  6042. int ret = 0;
  6043. if (wpa_s->global->p2p_group_formation)
  6044. group = wpa_s->global->p2p_group_formation;
  6045. wpa_s = wpa_s->parent;
  6046. offchannel_send_action_done(wpa_s);
  6047. if (group_added)
  6048. ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  6049. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  6050. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  6051. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  6052. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  6053. wpa_s->p2p_persistent_id,
  6054. wpa_s->p2p_pd_before_go_neg,
  6055. wpa_s->p2p_go_ht40,
  6056. wpa_s->p2p_go_vht);
  6057. return ret;
  6058. }
  6059. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  6060. {
  6061. int res;
  6062. if (!wpa_s->p2p_fallback_to_go_neg ||
  6063. wpa_s->p2p_in_provisioning <= 5)
  6064. return 0;
  6065. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  6066. return 0; /* peer operating as a GO */
  6067. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  6068. "fallback to GO Negotiation");
  6069. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
  6070. "reason=GO-not-found");
  6071. res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  6072. return res == 1 ? 2 : 1;
  6073. }
  6074. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  6075. {
  6076. struct wpa_supplicant *ifs;
  6077. if (wpa_s->wpa_state > WPA_SCANNING) {
  6078. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  6079. "concurrent operation",
  6080. wpa_s->conf->p2p_search_delay);
  6081. return wpa_s->conf->p2p_search_delay;
  6082. }
  6083. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  6084. radio_list) {
  6085. if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
  6086. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  6087. "delay due to concurrent operation on "
  6088. "interface %s",
  6089. wpa_s->conf->p2p_search_delay,
  6090. ifs->ifname);
  6091. return wpa_s->conf->p2p_search_delay;
  6092. }
  6093. }
  6094. return 0;
  6095. }
  6096. static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
  6097. struct wpa_ssid *s, const u8 *addr,
  6098. int iface_addr)
  6099. {
  6100. struct psk_list_entry *psk, *tmp;
  6101. int changed = 0;
  6102. dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
  6103. list) {
  6104. if ((iface_addr && !psk->p2p &&
  6105. os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
  6106. (!iface_addr && psk->p2p &&
  6107. os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
  6108. wpa_dbg(wpa_s, MSG_DEBUG,
  6109. "P2P: Remove persistent group PSK list entry for "
  6110. MACSTR " p2p=%u",
  6111. MAC2STR(psk->addr), psk->p2p);
  6112. dl_list_del(&psk->list);
  6113. os_free(psk);
  6114. changed++;
  6115. }
  6116. }
  6117. return changed;
  6118. }
  6119. void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
  6120. const u8 *p2p_dev_addr,
  6121. const u8 *psk, size_t psk_len)
  6122. {
  6123. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6124. struct wpa_ssid *persistent;
  6125. struct psk_list_entry *p, *last;
  6126. if (psk_len != sizeof(p->psk))
  6127. return;
  6128. if (p2p_dev_addr) {
  6129. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
  6130. " p2p_dev_addr=" MACSTR,
  6131. MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
  6132. if (is_zero_ether_addr(p2p_dev_addr))
  6133. p2p_dev_addr = NULL;
  6134. } else {
  6135. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
  6136. MAC2STR(mac_addr));
  6137. }
  6138. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  6139. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
  6140. /* To be added to persistent group once created */
  6141. if (wpa_s->global->add_psk == NULL) {
  6142. wpa_s->global->add_psk = os_zalloc(sizeof(*p));
  6143. if (wpa_s->global->add_psk == NULL)
  6144. return;
  6145. }
  6146. p = wpa_s->global->add_psk;
  6147. if (p2p_dev_addr) {
  6148. p->p2p = 1;
  6149. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6150. } else {
  6151. p->p2p = 0;
  6152. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6153. }
  6154. os_memcpy(p->psk, psk, psk_len);
  6155. return;
  6156. }
  6157. if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
  6158. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
  6159. return;
  6160. }
  6161. persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
  6162. ssid->ssid_len);
  6163. if (!persistent) {
  6164. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
  6165. return;
  6166. }
  6167. p = os_zalloc(sizeof(*p));
  6168. if (p == NULL)
  6169. return;
  6170. if (p2p_dev_addr) {
  6171. p->p2p = 1;
  6172. os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
  6173. } else {
  6174. p->p2p = 0;
  6175. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  6176. }
  6177. os_memcpy(p->psk, psk, psk_len);
  6178. if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
  6179. (last = dl_list_last(&persistent->psk_list,
  6180. struct psk_list_entry, list))) {
  6181. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
  6182. MACSTR " (p2p=%u) to make room for a new one",
  6183. MAC2STR(last->addr), last->p2p);
  6184. dl_list_del(&last->list);
  6185. os_free(last);
  6186. }
  6187. wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
  6188. p2p_dev_addr ? p2p_dev_addr : mac_addr,
  6189. p2p_dev_addr == NULL);
  6190. if (p2p_dev_addr) {
  6191. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
  6192. MACSTR, MAC2STR(p2p_dev_addr));
  6193. } else {
  6194. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
  6195. MAC2STR(mac_addr));
  6196. }
  6197. dl_list_add(&persistent->psk_list, &p->list);
  6198. if (wpa_s->parent->conf->update_config &&
  6199. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  6200. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  6201. }
  6202. static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
  6203. struct wpa_ssid *s, const u8 *addr,
  6204. int iface_addr)
  6205. {
  6206. int res;
  6207. res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
  6208. if (res > 0 && wpa_s->conf->update_config &&
  6209. wpa_config_write(wpa_s->confname, wpa_s->conf))
  6210. wpa_dbg(wpa_s, MSG_DEBUG,
  6211. "P2P: Failed to update configuration");
  6212. }
  6213. static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
  6214. const u8 *peer, int iface_addr)
  6215. {
  6216. struct hostapd_data *hapd;
  6217. struct hostapd_wpa_psk *psk, *prev, *rem;
  6218. struct sta_info *sta;
  6219. if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
  6220. wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
  6221. return;
  6222. /* Remove per-station PSK entry */
  6223. hapd = wpa_s->ap_iface->bss[0];
  6224. prev = NULL;
  6225. psk = hapd->conf->ssid.wpa_psk;
  6226. while (psk) {
  6227. if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
  6228. (!iface_addr &&
  6229. os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
  6230. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
  6231. MACSTR " iface_addr=%d",
  6232. MAC2STR(peer), iface_addr);
  6233. if (prev)
  6234. prev->next = psk->next;
  6235. else
  6236. hapd->conf->ssid.wpa_psk = psk->next;
  6237. rem = psk;
  6238. psk = psk->next;
  6239. os_free(rem);
  6240. } else {
  6241. prev = psk;
  6242. psk = psk->next;
  6243. }
  6244. }
  6245. /* Disconnect from group */
  6246. if (iface_addr)
  6247. sta = ap_get_sta(hapd, peer);
  6248. else
  6249. sta = ap_get_sta_p2p(hapd, peer);
  6250. if (sta) {
  6251. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
  6252. " (iface_addr=%d) from group",
  6253. MAC2STR(peer), iface_addr);
  6254. hostapd_drv_sta_deauth(hapd, sta->addr,
  6255. WLAN_REASON_DEAUTH_LEAVING);
  6256. ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
  6257. }
  6258. }
  6259. void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
  6260. int iface_addr)
  6261. {
  6262. struct wpa_ssid *s;
  6263. struct wpa_supplicant *w;
  6264. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
  6265. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
  6266. /* Remove from any persistent group */
  6267. for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
  6268. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  6269. continue;
  6270. if (!iface_addr)
  6271. wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
  6272. wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
  6273. }
  6274. /* Remove from any operating group */
  6275. for (w = wpa_s->global->ifaces; w; w = w->next)
  6276. wpas_p2p_remove_client_go(w, peer, iface_addr);
  6277. }
  6278. static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
  6279. {
  6280. struct wpa_supplicant *wpa_s = eloop_ctx;
  6281. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
  6282. }
  6283. static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
  6284. {
  6285. struct wpa_supplicant *wpa_s = eloop_ctx;
  6286. wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
  6287. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
  6288. }
  6289. int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
  6290. struct wpa_ssid *ssid)
  6291. {
  6292. struct wpa_supplicant *iface;
  6293. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6294. if (!iface->current_ssid ||
  6295. iface->current_ssid->frequency == freq ||
  6296. (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  6297. !iface->current_ssid->p2p_group))
  6298. continue;
  6299. /* Remove the connection with least priority */
  6300. if (!wpas_is_p2p_prioritized(iface)) {
  6301. /* STA connection has priority over existing
  6302. * P2P connection, so remove the interface. */
  6303. wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
  6304. eloop_register_timeout(0, 0,
  6305. wpas_p2p_group_freq_conflict,
  6306. iface, NULL);
  6307. /* If connection in progress is P2P connection, do not
  6308. * proceed for the connection. */
  6309. if (wpa_s == iface)
  6310. return -1;
  6311. else
  6312. return 0;
  6313. } else {
  6314. /* P2P connection has priority, disable the STA network
  6315. */
  6316. wpa_supplicant_disable_network(wpa_s->global->ifaces,
  6317. ssid);
  6318. wpa_msg(wpa_s->global->ifaces, MSG_INFO,
  6319. WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
  6320. os_memset(wpa_s->global->ifaces->pending_bssid, 0,
  6321. ETH_ALEN);
  6322. /* If P2P connection is in progress, continue
  6323. * connecting...*/
  6324. if (wpa_s == iface)
  6325. return 0;
  6326. else
  6327. return -1;
  6328. }
  6329. }
  6330. return 0;
  6331. }
  6332. int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
  6333. {
  6334. struct wpa_ssid *ssid = wpa_s->current_ssid;
  6335. if (ssid == NULL || !ssid->p2p_group)
  6336. return 0;
  6337. if (wpa_s->p2p_last_4way_hs_fail &&
  6338. wpa_s->p2p_last_4way_hs_fail == ssid) {
  6339. u8 go_dev_addr[ETH_ALEN];
  6340. struct wpa_ssid *persistent;
  6341. if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
  6342. ssid->ssid,
  6343. ssid->ssid_len) <= 0) {
  6344. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
  6345. goto disconnect;
  6346. }
  6347. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
  6348. MACSTR, MAC2STR(go_dev_addr));
  6349. persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
  6350. ssid->ssid,
  6351. ssid->ssid_len);
  6352. if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
  6353. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
  6354. goto disconnect;
  6355. }
  6356. wpa_msg_global(wpa_s->parent, MSG_INFO,
  6357. P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
  6358. persistent->id);
  6359. disconnect:
  6360. wpa_s->p2p_last_4way_hs_fail = NULL;
  6361. /*
  6362. * Remove the group from a timeout to avoid issues with caller
  6363. * continuing to use the interface if this is on a P2P group
  6364. * interface.
  6365. */
  6366. eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
  6367. wpa_s, NULL);
  6368. return 1;
  6369. }
  6370. wpa_s->p2p_last_4way_hs_fail = ssid;
  6371. return 0;
  6372. }
  6373. #ifdef CONFIG_WPS_NFC
  6374. static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
  6375. struct wpabuf *p2p)
  6376. {
  6377. struct wpabuf *ret;
  6378. size_t wsc_len;
  6379. if (p2p == NULL) {
  6380. wpabuf_free(wsc);
  6381. wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
  6382. return NULL;
  6383. }
  6384. wsc_len = wsc ? wpabuf_len(wsc) : 0;
  6385. ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
  6386. if (ret == NULL) {
  6387. wpabuf_free(wsc);
  6388. wpabuf_free(p2p);
  6389. return NULL;
  6390. }
  6391. wpabuf_put_be16(ret, wsc_len);
  6392. if (wsc)
  6393. wpabuf_put_buf(ret, wsc);
  6394. wpabuf_put_be16(ret, wpabuf_len(p2p));
  6395. wpabuf_put_buf(ret, p2p);
  6396. wpabuf_free(wsc);
  6397. wpabuf_free(p2p);
  6398. wpa_hexdump_buf(MSG_DEBUG,
  6399. "P2P: Generated NFC connection handover message", ret);
  6400. if (ndef && ret) {
  6401. struct wpabuf *tmp;
  6402. tmp = ndef_build_p2p(ret);
  6403. wpabuf_free(ret);
  6404. if (tmp == NULL) {
  6405. wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
  6406. return NULL;
  6407. }
  6408. ret = tmp;
  6409. }
  6410. return ret;
  6411. }
  6412. static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
  6413. struct wpa_ssid **ssid, u8 *go_dev_addr)
  6414. {
  6415. struct wpa_supplicant *iface;
  6416. if (go_dev_addr)
  6417. os_memset(go_dev_addr, 0, ETH_ALEN);
  6418. if (ssid)
  6419. *ssid = NULL;
  6420. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6421. if (iface->wpa_state < WPA_ASSOCIATING ||
  6422. iface->current_ssid == NULL || iface->assoc_freq == 0 ||
  6423. !iface->current_ssid->p2p_group ||
  6424. iface->current_ssid->mode != WPAS_MODE_INFRA)
  6425. continue;
  6426. if (ssid)
  6427. *ssid = iface->current_ssid;
  6428. if (go_dev_addr)
  6429. os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
  6430. return iface->assoc_freq;
  6431. }
  6432. return 0;
  6433. }
  6434. struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
  6435. int ndef)
  6436. {
  6437. struct wpabuf *wsc, *p2p;
  6438. struct wpa_ssid *ssid;
  6439. u8 go_dev_addr[ETH_ALEN];
  6440. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  6441. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
  6442. wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
  6443. return NULL;
  6444. }
  6445. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6446. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6447. &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
  6448. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
  6449. return NULL;
  6450. }
  6451. if (cli_freq == 0) {
  6452. wsc = wps_build_nfc_handover_req_p2p(
  6453. wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
  6454. } else
  6455. wsc = NULL;
  6456. p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
  6457. go_dev_addr, ssid ? ssid->ssid : NULL,
  6458. ssid ? ssid->ssid_len : 0);
  6459. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  6460. }
  6461. struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
  6462. int ndef, int tag)
  6463. {
  6464. struct wpabuf *wsc, *p2p;
  6465. struct wpa_ssid *ssid;
  6466. u8 go_dev_addr[ETH_ALEN];
  6467. int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
  6468. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6469. return NULL;
  6470. if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6471. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6472. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  6473. return NULL;
  6474. if (cli_freq == 0) {
  6475. wsc = wps_build_nfc_handover_sel_p2p(
  6476. wpa_s->parent->wps,
  6477. tag ? wpa_s->conf->wps_nfc_dev_pw_id :
  6478. DEV_PW_NFC_CONNECTION_HANDOVER,
  6479. wpa_s->conf->wps_nfc_dh_pubkey,
  6480. tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
  6481. } else
  6482. wsc = NULL;
  6483. p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
  6484. go_dev_addr, ssid ? ssid->ssid : NULL,
  6485. ssid ? ssid->ssid_len : 0);
  6486. return wpas_p2p_nfc_handover(ndef, wsc, p2p);
  6487. }
  6488. static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
  6489. struct p2p_nfc_params *params)
  6490. {
  6491. wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
  6492. "connection handover (freq=%d)",
  6493. params->go_freq);
  6494. if (params->go_freq && params->go_ssid_len) {
  6495. wpa_s->p2p_wps_method = WPS_NFC;
  6496. wpa_s->pending_join_wps_method = WPS_NFC;
  6497. os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
  6498. os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
  6499. ETH_ALEN);
  6500. return wpas_p2p_join_start(wpa_s, params->go_freq,
  6501. params->go_ssid,
  6502. params->go_ssid_len);
  6503. }
  6504. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  6505. WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
  6506. params->go_freq, -1, 0, 1, 1);
  6507. }
  6508. static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
  6509. struct p2p_nfc_params *params, int tag)
  6510. {
  6511. int res, persistent;
  6512. struct wpa_ssid *ssid;
  6513. wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
  6514. "connection handover");
  6515. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6516. ssid = wpa_s->current_ssid;
  6517. if (ssid == NULL)
  6518. continue;
  6519. if (ssid->mode != WPAS_MODE_P2P_GO)
  6520. continue;
  6521. if (wpa_s->ap_iface == NULL)
  6522. continue;
  6523. break;
  6524. }
  6525. if (wpa_s == NULL) {
  6526. wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
  6527. return -1;
  6528. }
  6529. if (wpa_s->parent->p2p_oob_dev_pw_id !=
  6530. DEV_PW_NFC_CONNECTION_HANDOVER &&
  6531. !wpa_s->parent->p2p_oob_dev_pw) {
  6532. wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
  6533. return -1;
  6534. }
  6535. res = wpas_ap_wps_add_nfc_pw(
  6536. wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
  6537. wpa_s->parent->p2p_oob_dev_pw,
  6538. wpa_s->parent->p2p_peer_oob_pk_hash_known ?
  6539. wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
  6540. if (res)
  6541. return res;
  6542. if (!tag) {
  6543. wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
  6544. return 0;
  6545. }
  6546. if (!params->peer ||
  6547. !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
  6548. return 0;
  6549. wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
  6550. " to join", MAC2STR(params->peer->p2p_device_addr));
  6551. wpa_s->global->p2p_invite_group = wpa_s;
  6552. persistent = ssid->p2p_persistent_group &&
  6553. wpas_p2p_get_persistent(wpa_s->parent,
  6554. params->peer->p2p_device_addr,
  6555. ssid->ssid, ssid->ssid_len);
  6556. wpa_s->parent->pending_invite_ssid_id = -1;
  6557. return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
  6558. P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
  6559. ssid->ssid, ssid->ssid_len, ssid->frequency,
  6560. wpa_s->global->p2p_dev_addr, persistent, 0,
  6561. wpa_s->parent->p2p_oob_dev_pw_id);
  6562. }
  6563. static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
  6564. struct p2p_nfc_params *params,
  6565. int forced_freq)
  6566. {
  6567. wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
  6568. "connection handover");
  6569. return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  6570. WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
  6571. forced_freq, -1, 0, 1, 1);
  6572. }
  6573. static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
  6574. struct p2p_nfc_params *params,
  6575. int forced_freq)
  6576. {
  6577. int res;
  6578. wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
  6579. "connection handover");
  6580. res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
  6581. WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
  6582. forced_freq, -1, 0, 1, 1);
  6583. if (res)
  6584. return res;
  6585. res = wpas_p2p_listen(wpa_s, 60);
  6586. if (res) {
  6587. p2p_unauthorize(wpa_s->global->p2p,
  6588. params->peer->p2p_device_addr);
  6589. }
  6590. return res;
  6591. }
  6592. static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
  6593. const struct wpabuf *data,
  6594. int sel, int tag, int forced_freq)
  6595. {
  6596. const u8 *pos, *end;
  6597. u16 len, id;
  6598. struct p2p_nfc_params params;
  6599. int res;
  6600. os_memset(&params, 0, sizeof(params));
  6601. params.sel = sel;
  6602. wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
  6603. pos = wpabuf_head(data);
  6604. end = pos + wpabuf_len(data);
  6605. if (end - pos < 2) {
  6606. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
  6607. "attributes");
  6608. return -1;
  6609. }
  6610. len = WPA_GET_BE16(pos);
  6611. pos += 2;
  6612. if (len > end - pos) {
  6613. wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
  6614. "attributes");
  6615. return -1;
  6616. }
  6617. params.wsc_attr = pos;
  6618. params.wsc_len = len;
  6619. pos += len;
  6620. if (end - pos < 2) {
  6621. wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
  6622. "attributes");
  6623. return -1;
  6624. }
  6625. len = WPA_GET_BE16(pos);
  6626. pos += 2;
  6627. if (len > end - pos) {
  6628. wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
  6629. "attributes");
  6630. return -1;
  6631. }
  6632. params.p2p_attr = pos;
  6633. params.p2p_len = len;
  6634. pos += len;
  6635. wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
  6636. params.wsc_attr, params.wsc_len);
  6637. wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
  6638. params.p2p_attr, params.p2p_len);
  6639. if (pos < end) {
  6640. wpa_hexdump(MSG_DEBUG,
  6641. "P2P: Ignored extra data after P2P attributes",
  6642. pos, end - pos);
  6643. }
  6644. res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
  6645. if (res)
  6646. return res;
  6647. if (params.next_step == NO_ACTION)
  6648. return 0;
  6649. if (params.next_step == BOTH_GO) {
  6650. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
  6651. MAC2STR(params.peer->p2p_device_addr));
  6652. return 0;
  6653. }
  6654. if (params.next_step == PEER_CLIENT) {
  6655. if (!is_zero_ether_addr(params.go_dev_addr)) {
  6656. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  6657. "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
  6658. " ssid=\"%s\"",
  6659. MAC2STR(params.peer->p2p_device_addr),
  6660. params.go_freq,
  6661. MAC2STR(params.go_dev_addr),
  6662. wpa_ssid_txt(params.go_ssid,
  6663. params.go_ssid_len));
  6664. } else {
  6665. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
  6666. "peer=" MACSTR " freq=%d",
  6667. MAC2STR(params.peer->p2p_device_addr),
  6668. params.go_freq);
  6669. }
  6670. return 0;
  6671. }
  6672. if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
  6673. wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
  6674. MACSTR, MAC2STR(params.peer->p2p_device_addr));
  6675. return 0;
  6676. }
  6677. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  6678. wpa_s->p2p_oob_dev_pw = NULL;
  6679. if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
  6680. wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
  6681. "received");
  6682. return -1;
  6683. }
  6684. id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
  6685. wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
  6686. wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
  6687. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  6688. os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
  6689. params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
  6690. wpa_s->p2p_peer_oob_pk_hash_known = 1;
  6691. if (tag) {
  6692. if (id < 0x10) {
  6693. wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
  6694. "peer OOB Device Password Id %u", id);
  6695. return -1;
  6696. }
  6697. wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
  6698. "Device Password Id %u", id);
  6699. wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
  6700. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  6701. params.oob_dev_pw_len -
  6702. WPS_OOB_PUBKEY_HASH_LEN - 2);
  6703. wpa_s->p2p_oob_dev_pw_id = id;
  6704. wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
  6705. params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
  6706. params.oob_dev_pw_len -
  6707. WPS_OOB_PUBKEY_HASH_LEN - 2);
  6708. if (wpa_s->p2p_oob_dev_pw == NULL)
  6709. return -1;
  6710. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
  6711. wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
  6712. &wpa_s->conf->wps_nfc_dh_privkey) < 0)
  6713. return -1;
  6714. } else {
  6715. wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
  6716. "without Device Password");
  6717. wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
  6718. }
  6719. switch (params.next_step) {
  6720. case NO_ACTION:
  6721. case BOTH_GO:
  6722. case PEER_CLIENT:
  6723. /* already covered above */
  6724. return 0;
  6725. case JOIN_GROUP:
  6726. return wpas_p2p_nfc_join_group(wpa_s, &params);
  6727. case AUTH_JOIN:
  6728. return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
  6729. case INIT_GO_NEG:
  6730. return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
  6731. case RESP_GO_NEG:
  6732. /* TODO: use own OOB Dev Pw */
  6733. return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
  6734. }
  6735. return -1;
  6736. }
  6737. int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
  6738. const struct wpabuf *data, int forced_freq)
  6739. {
  6740. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6741. return -1;
  6742. return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
  6743. }
  6744. int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
  6745. const struct wpabuf *req,
  6746. const struct wpabuf *sel, int forced_freq)
  6747. {
  6748. struct wpabuf *tmp;
  6749. int ret;
  6750. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6751. return -1;
  6752. wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
  6753. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
  6754. wpabuf_head(req), wpabuf_len(req));
  6755. wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
  6756. wpabuf_head(sel), wpabuf_len(sel));
  6757. if (forced_freq)
  6758. wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
  6759. tmp = ndef_parse_p2p(init ? sel : req);
  6760. if (tmp == NULL) {
  6761. wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
  6762. return -1;
  6763. }
  6764. ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
  6765. forced_freq);
  6766. wpabuf_free(tmp);
  6767. return ret;
  6768. }
  6769. int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
  6770. {
  6771. const u8 *if_addr;
  6772. int go_intent = wpa_s->conf->p2p_go_intent;
  6773. struct wpa_supplicant *iface;
  6774. if (wpa_s->global->p2p == NULL)
  6775. return -1;
  6776. if (!enabled) {
  6777. wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
  6778. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  6779. {
  6780. if (!iface->ap_iface)
  6781. continue;
  6782. hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
  6783. }
  6784. p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
  6785. 0, NULL);
  6786. if (wpa_s->p2p_nfc_tag_enabled)
  6787. wpas_p2p_remove_pending_group_interface(wpa_s);
  6788. wpa_s->p2p_nfc_tag_enabled = 0;
  6789. return 0;
  6790. }
  6791. if (wpa_s->global->p2p_disabled)
  6792. return -1;
  6793. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  6794. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  6795. wpa_s->conf->wps_nfc_dev_pw == NULL ||
  6796. wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
  6797. wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
  6798. "to allow static handover cases");
  6799. return -1;
  6800. }
  6801. wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
  6802. wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  6803. wpabuf_free(wpa_s->p2p_oob_dev_pw);
  6804. wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  6805. if (wpa_s->p2p_oob_dev_pw == NULL)
  6806. return -1;
  6807. wpa_s->p2p_peer_oob_pk_hash_known = 0;
  6808. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
  6809. wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
  6810. /*
  6811. * P2P Group Interface present and the command came on group
  6812. * interface, so enable the token for the current interface.
  6813. */
  6814. wpa_s->create_p2p_iface = 0;
  6815. } else {
  6816. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  6817. }
  6818. if (wpa_s->create_p2p_iface) {
  6819. enum wpa_driver_if_type iftype;
  6820. /* Prepare to add a new interface for the group */
  6821. iftype = WPA_IF_P2P_GROUP;
  6822. if (go_intent == 15)
  6823. iftype = WPA_IF_P2P_GO;
  6824. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  6825. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  6826. "interface for the group");
  6827. return -1;
  6828. }
  6829. if_addr = wpa_s->pending_interface_addr;
  6830. } else
  6831. if_addr = wpa_s->own_addr;
  6832. wpa_s->p2p_nfc_tag_enabled = enabled;
  6833. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  6834. struct hostapd_data *hapd;
  6835. if (iface->ap_iface == NULL)
  6836. continue;
  6837. hapd = iface->ap_iface->bss[0];
  6838. wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
  6839. hapd->conf->wps_nfc_dh_pubkey =
  6840. wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  6841. wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
  6842. hapd->conf->wps_nfc_dh_privkey =
  6843. wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  6844. wpabuf_free(hapd->conf->wps_nfc_dev_pw);
  6845. hapd->conf->wps_nfc_dev_pw =
  6846. wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
  6847. hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
  6848. if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
  6849. wpa_dbg(iface, MSG_DEBUG,
  6850. "P2P: Failed to enable NFC Tag for GO");
  6851. }
  6852. }
  6853. p2p_set_authorized_oob_dev_pw_id(
  6854. wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
  6855. if_addr);
  6856. return 0;
  6857. }
  6858. #endif /* CONFIG_WPS_NFC */
  6859. static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
  6860. struct wpa_used_freq_data *freqs,
  6861. unsigned int num)
  6862. {
  6863. u8 curr_chan, cand, chan;
  6864. unsigned int i;
  6865. curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
  6866. for (i = 0, cand = 0; i < num; i++) {
  6867. ieee80211_freq_to_chan(freqs[i].freq, &chan);
  6868. if (curr_chan == chan) {
  6869. cand = 0;
  6870. break;
  6871. }
  6872. if (chan == 1 || chan == 6 || chan == 11)
  6873. cand = chan;
  6874. }
  6875. if (cand) {
  6876. wpa_dbg(wpa_s, MSG_DEBUG,
  6877. "P2P: Update Listen channel to %u based on operating channel",
  6878. cand);
  6879. p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
  6880. }
  6881. }
  6882. void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
  6883. {
  6884. struct wpa_used_freq_data *freqs;
  6885. unsigned int num = wpa_s->num_multichan_concurrent;
  6886. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  6887. return;
  6888. /*
  6889. * If possible, optimize the Listen channel to be a channel that is
  6890. * already used by one of the other interfaces.
  6891. */
  6892. if (!wpa_s->conf->p2p_optimize_listen_chan)
  6893. return;
  6894. if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
  6895. return;
  6896. freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
  6897. if (!freqs)
  6898. return;
  6899. num = get_shared_radio_freqs_data(wpa_s, freqs, num);
  6900. wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
  6901. os_free(freqs);
  6902. }
  6903. void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
  6904. {
  6905. if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
  6906. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
  6907. "the management interface is being removed");
  6908. wpas_p2p_deinit_global(wpa_s->global);
  6909. }
  6910. }
  6911. void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
  6912. {
  6913. if (wpa_s->ap_iface->bss)
  6914. wpa_s->ap_iface->bss[0]->p2p_group = NULL;
  6915. wpas_p2p_group_deinit(wpa_s);
  6916. }