p2p_supplicant.c 251 KB

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