p2p_supplicant.c 241 KB

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