p2p_supplicant.c 222 KB

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