p2p_supplicant.c 249 KB

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