p2p_supplicant.c 231 KB

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