p2p_supplicant.c 234 KB

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