p2p_supplicant.c 258 KB

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