p2p_supplicant.c 264 KB

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