p2p_supplicant.c 257 KB

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