p2p_supplicant.c 220 KB

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