p2p_supplicant.c 242 KB

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