p2p_supplicant.c 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933
  1. /*
  2. * wpa_supplicant - P2P
  3. * Copyright (c) 2009-2010, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "common/ieee802_11_common.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "wps/wps_i.h"
  15. #include "p2p/p2p.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/ap_config.h"
  18. #include "ap/p2p_hostapd.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "wpa_supplicant_i.h"
  22. #include "driver_i.h"
  23. #include "ap.h"
  24. #include "config_ssid.h"
  25. #include "config.h"
  26. #include "notify.h"
  27. #include "scan.h"
  28. #include "bss.h"
  29. #include "offchannel.h"
  30. #include "wps_supplicant.h"
  31. #include "p2p_supplicant.h"
  32. /*
  33. * How many times to try to scan to find the GO before giving up on join
  34. * request.
  35. */
  36. #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
  37. #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
  38. #ifndef P2P_MAX_CLIENT_IDLE
  39. /*
  40. * How many seconds to try to reconnect to the GO when connection in P2P client
  41. * role has been lost.
  42. */
  43. #define P2P_MAX_CLIENT_IDLE 10
  44. #endif /* P2P_MAX_CLIENT_IDLE */
  45. #ifndef P2P_MAX_INITIAL_CONN_WAIT
  46. /*
  47. * How many seconds to wait for initial 4-way handshake to get completed after
  48. * WPS provisioning step.
  49. */
  50. #define P2P_MAX_INITIAL_CONN_WAIT 10
  51. #endif /* P2P_MAX_INITIAL_CONN_WAIT */
  52. #ifndef P2P_CONCURRENT_SEARCH_DELAY
  53. #define P2P_CONCURRENT_SEARCH_DELAY 500
  54. #endif /* P2P_CONCURRENT_SEARCH_DELAY */
  55. #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
  56. enum p2p_group_removal_reason {
  57. P2P_GROUP_REMOVAL_UNKNOWN,
  58. P2P_GROUP_REMOVAL_SILENT,
  59. P2P_GROUP_REMOVAL_FORMATION_FAILED,
  60. P2P_GROUP_REMOVAL_REQUESTED,
  61. P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
  62. P2P_GROUP_REMOVAL_UNAVAILABLE,
  63. P2P_GROUP_REMOVAL_GO_ENDING_SESSION
  64. };
  65. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
  66. static struct wpa_supplicant *
  67. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  68. int go);
  69. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
  70. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
  71. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
  72. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  73. const u8 *dev_addr, enum p2p_wps_method wps_method,
  74. int auto_join);
  75. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
  76. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
  77. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
  78. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
  79. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  80. void *timeout_ctx);
  81. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  82. int group_added);
  83. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
  84. /*
  85. * Get the number of concurrent channels that the HW can operate, but that are
  86. * currently not in use by any of the wpa_supplicant interfaces.
  87. */
  88. static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
  89. {
  90. int *freqs;
  91. int num;
  92. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  93. if (!freqs)
  94. return -1;
  95. num = get_shared_radio_freqs(wpa_s, freqs,
  96. wpa_s->num_multichan_concurrent);
  97. os_free(freqs);
  98. return wpa_s->num_multichan_concurrent - num;
  99. }
  100. /*
  101. * Get the frequencies that are currently in use by one or more of the virtual
  102. * interfaces, and that are also valid for P2P operation.
  103. */
  104. static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
  105. int *p2p_freqs, unsigned int len)
  106. {
  107. int *freqs;
  108. unsigned int num, i, j;
  109. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  110. if (!freqs)
  111. return -1;
  112. num = get_shared_radio_freqs(wpa_s, freqs,
  113. wpa_s->num_multichan_concurrent);
  114. os_memset(p2p_freqs, 0, sizeof(int) * len);
  115. for (i = 0, j = 0; i < num && j < len; i++) {
  116. if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  117. p2p_freqs[j++] = freqs[i];
  118. }
  119. os_free(freqs);
  120. return j;
  121. }
  122. static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
  123. int freq)
  124. {
  125. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  126. return;
  127. if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  128. wpas_p2p_num_unused_channels(wpa_s) > 0 &&
  129. wpa_s->parent->conf->p2p_ignore_shared_freq)
  130. freq = 0;
  131. p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
  132. }
  133. static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
  134. struct wpa_scan_results *scan_res)
  135. {
  136. size_t i;
  137. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  138. return;
  139. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
  140. (int) scan_res->num);
  141. for (i = 0; i < scan_res->num; i++) {
  142. struct wpa_scan_res *bss = scan_res->res[i];
  143. struct os_time time_tmp_age, entry_ts;
  144. time_tmp_age.sec = bss->age / 1000;
  145. time_tmp_age.usec = (bss->age % 1000) * 1000;
  146. os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
  147. if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
  148. bss->freq, &entry_ts, bss->level,
  149. (const u8 *) (bss + 1),
  150. bss->ie_len) > 0)
  151. break;
  152. }
  153. p2p_scan_res_handled(wpa_s->global->p2p);
  154. }
  155. static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
  156. unsigned int num_req_dev_types,
  157. const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
  158. {
  159. struct wpa_supplicant *wpa_s = ctx;
  160. struct wpa_supplicant *ifs;
  161. struct wpa_driver_scan_params params;
  162. int ret;
  163. struct wpabuf *wps_ie, *ies;
  164. int social_channels[] = { 2412, 2437, 2462, 0, 0 };
  165. size_t ielen;
  166. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  167. return -1;
  168. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  169. if (ifs->sta_scan_pending &&
  170. (wpas_scan_scheduled(ifs) || ifs->scanning) &&
  171. wpas_p2p_in_progress(wpa_s) == 2) {
  172. wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
  173. "pending station mode scan to be "
  174. "completed on interface %s", ifs->ifname);
  175. wpa_s->global->p2p_cb_on_scan_complete = 1;
  176. wpa_supplicant_req_scan(ifs, 0, 0);
  177. return 1;
  178. }
  179. }
  180. os_memset(&params, 0, sizeof(params));
  181. /* P2P Wildcard SSID */
  182. params.num_ssids = 1;
  183. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  184. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  185. wpa_s->wps->dev.p2p = 1;
  186. wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
  187. wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
  188. num_req_dev_types, req_dev_types);
  189. if (wps_ie == NULL)
  190. return -1;
  191. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  192. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  193. if (ies == NULL) {
  194. wpabuf_free(wps_ie);
  195. return -1;
  196. }
  197. wpabuf_put_buf(ies, wps_ie);
  198. wpabuf_free(wps_ie);
  199. p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
  200. params.p2p_probe = 1;
  201. params.extra_ies = wpabuf_head(ies);
  202. params.extra_ies_len = wpabuf_len(ies);
  203. switch (type) {
  204. case P2P_SCAN_SOCIAL:
  205. params.freqs = social_channels;
  206. break;
  207. case P2P_SCAN_FULL:
  208. break;
  209. case P2P_SCAN_SOCIAL_PLUS_ONE:
  210. social_channels[3] = freq;
  211. params.freqs = social_channels;
  212. break;
  213. }
  214. ret = wpa_drv_scan(wpa_s, &params);
  215. wpabuf_free(ies);
  216. if (ret) {
  217. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  218. if (ifs->scanning ||
  219. ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
  220. wpa_s->global->p2p_cb_on_scan_complete = 1;
  221. ret = 1;
  222. break;
  223. }
  224. }
  225. } else {
  226. os_get_time(&wpa_s->scan_trigger_time);
  227. wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
  228. }
  229. return ret;
  230. }
  231. static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
  232. {
  233. switch (p2p_group_interface) {
  234. case P2P_GROUP_INTERFACE_PENDING:
  235. return WPA_IF_P2P_GROUP;
  236. case P2P_GROUP_INTERFACE_GO:
  237. return WPA_IF_P2P_GO;
  238. case P2P_GROUP_INTERFACE_CLIENT:
  239. return WPA_IF_P2P_CLIENT;
  240. }
  241. return WPA_IF_P2P_GROUP;
  242. }
  243. static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
  244. const u8 *ssid,
  245. size_t ssid_len, int *go)
  246. {
  247. struct wpa_ssid *s;
  248. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  249. for (s = wpa_s->conf->ssid; s; s = s->next) {
  250. if (s->disabled != 0 || !s->p2p_group ||
  251. s->ssid_len != ssid_len ||
  252. os_memcmp(ssid, s->ssid, ssid_len) != 0)
  253. continue;
  254. if (s->mode == WPAS_MODE_P2P_GO &&
  255. s != wpa_s->current_ssid)
  256. continue;
  257. if (go)
  258. *go = s->mode == WPAS_MODE_P2P_GO;
  259. return wpa_s;
  260. }
  261. }
  262. return NULL;
  263. }
  264. static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
  265. enum p2p_group_removal_reason removal_reason)
  266. {
  267. struct wpa_ssid *ssid;
  268. char *gtype;
  269. const char *reason;
  270. ssid = wpa_s->current_ssid;
  271. if (ssid == NULL) {
  272. /*
  273. * The current SSID was not known, but there may still be a
  274. * pending P2P group interface waiting for provisioning or a
  275. * P2P group that is trying to reconnect.
  276. */
  277. ssid = wpa_s->conf->ssid;
  278. while (ssid) {
  279. if (ssid->p2p_group && ssid->disabled != 2)
  280. break;
  281. ssid = ssid->next;
  282. }
  283. if (ssid == NULL &&
  284. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
  285. {
  286. wpa_printf(MSG_ERROR, "P2P: P2P group interface "
  287. "not found");
  288. return -1;
  289. }
  290. }
  291. if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
  292. gtype = "GO";
  293. else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
  294. (ssid && ssid->mode == WPAS_MODE_INFRA)) {
  295. wpa_s->reassociate = 0;
  296. wpa_s->disconnected = 1;
  297. wpa_supplicant_deauthenticate(wpa_s,
  298. WLAN_REASON_DEAUTH_LEAVING);
  299. gtype = "client";
  300. } else
  301. gtype = "GO";
  302. if (wpa_s->cross_connect_in_use) {
  303. wpa_s->cross_connect_in_use = 0;
  304. wpa_msg_global(wpa_s->parent, MSG_INFO,
  305. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  306. wpa_s->ifname, wpa_s->cross_connect_uplink);
  307. }
  308. switch (removal_reason) {
  309. case P2P_GROUP_REMOVAL_REQUESTED:
  310. reason = " reason=REQUESTED";
  311. break;
  312. case P2P_GROUP_REMOVAL_FORMATION_FAILED:
  313. reason = " reason=FORMATION_FAILED";
  314. break;
  315. case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
  316. reason = " reason=IDLE";
  317. break;
  318. case P2P_GROUP_REMOVAL_UNAVAILABLE:
  319. reason = " reason=UNAVAILABLE";
  320. break;
  321. case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
  322. reason = " reason=GO_ENDING_SESSION";
  323. break;
  324. default:
  325. reason = "";
  326. break;
  327. }
  328. if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
  329. wpa_msg_global(wpa_s->parent, MSG_INFO,
  330. P2P_EVENT_GROUP_REMOVED "%s %s%s",
  331. wpa_s->ifname, gtype, reason);
  332. }
  333. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  334. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  335. if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  336. wpa_s->parent, NULL) > 0) {
  337. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
  338. "timeout");
  339. wpa_s->p2p_in_provisioning = 0;
  340. }
  341. if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
  342. wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
  343. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  344. struct wpa_global *global;
  345. char *ifname;
  346. enum wpa_driver_if_type type;
  347. wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
  348. wpa_s->ifname);
  349. global = wpa_s->global;
  350. ifname = os_strdup(wpa_s->ifname);
  351. type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
  352. wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
  353. wpa_s = global->ifaces;
  354. if (wpa_s && ifname)
  355. wpa_drv_if_remove(wpa_s, type, ifname);
  356. os_free(ifname);
  357. return 1;
  358. }
  359. wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
  360. if (ssid && (ssid->p2p_group ||
  361. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
  362. (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
  363. int id = ssid->id;
  364. if (ssid == wpa_s->current_ssid) {
  365. wpa_sm_set_config(wpa_s->wpa, NULL);
  366. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  367. wpa_s->current_ssid = NULL;
  368. }
  369. /*
  370. * Networks objects created during any P2P activities are not
  371. * exposed out as they might/will confuse certain non-P2P aware
  372. * applications since these network objects won't behave like
  373. * regular ones.
  374. *
  375. * Likewise, we don't send out network removed signals for such
  376. * network objects.
  377. */
  378. wpa_config_remove_network(wpa_s->conf, id);
  379. wpa_supplicant_clear_status(wpa_s);
  380. wpa_supplicant_cancel_sched_scan(wpa_s);
  381. wpa_s->sta_scan_pending = 0;
  382. } else {
  383. wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
  384. "found");
  385. }
  386. if (wpa_s->ap_iface)
  387. wpa_supplicant_ap_deinit(wpa_s);
  388. else
  389. wpa_drv_deinit_p2p_cli(wpa_s);
  390. return 0;
  391. }
  392. static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
  393. u8 *go_dev_addr,
  394. const u8 *ssid, size_t ssid_len)
  395. {
  396. struct wpa_bss *bss;
  397. const u8 *bssid;
  398. struct wpabuf *p2p;
  399. u8 group_capab;
  400. const u8 *addr;
  401. if (wpa_s->go_params)
  402. bssid = wpa_s->go_params->peer_interface_addr;
  403. else
  404. bssid = wpa_s->bssid;
  405. bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
  406. if (bss == NULL) {
  407. u8 iface_addr[ETH_ALEN];
  408. if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
  409. iface_addr) == 0)
  410. bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
  411. }
  412. if (bss == NULL) {
  413. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  414. "group is persistent - BSS " MACSTR " not found",
  415. MAC2STR(bssid));
  416. return 0;
  417. }
  418. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  419. if (p2p == NULL) {
  420. wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
  421. "group is persistent - BSS " MACSTR
  422. " did not include P2P IE", MAC2STR(bssid));
  423. wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
  424. (u8 *) (bss + 1), bss->ie_len);
  425. wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
  426. ((u8 *) bss + 1) + bss->ie_len,
  427. bss->beacon_ie_len);
  428. return 0;
  429. }
  430. group_capab = p2p_get_group_capab(p2p);
  431. addr = p2p_get_go_dev_addr(p2p);
  432. wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
  433. "group_capab=0x%x", group_capab);
  434. if (addr) {
  435. os_memcpy(go_dev_addr, addr, ETH_ALEN);
  436. wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
  437. MAC2STR(addr));
  438. } else
  439. os_memset(go_dev_addr, 0, ETH_ALEN);
  440. wpabuf_free(p2p);
  441. wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
  442. "go_dev_addr=" MACSTR,
  443. MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
  444. return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  445. }
  446. static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
  447. struct wpa_ssid *ssid,
  448. const u8 *go_dev_addr)
  449. {
  450. struct wpa_ssid *s;
  451. int changed = 0;
  452. wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
  453. "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
  454. for (s = wpa_s->conf->ssid; s; s = s->next) {
  455. if (s->disabled == 2 &&
  456. os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
  457. s->ssid_len == ssid->ssid_len &&
  458. os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
  459. break;
  460. }
  461. if (s) {
  462. wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
  463. "entry");
  464. if (ssid->passphrase && !s->passphrase)
  465. changed = 1;
  466. else if (ssid->passphrase && s->passphrase &&
  467. os_strcmp(ssid->passphrase, s->passphrase) != 0)
  468. changed = 1;
  469. } else {
  470. wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
  471. "entry");
  472. changed = 1;
  473. s = wpa_config_add_network(wpa_s->conf);
  474. if (s == NULL)
  475. return -1;
  476. /*
  477. * Instead of network_added we emit persistent_group_added
  478. * notification. Also to keep the defense checks in
  479. * persistent_group obj registration method, we set the
  480. * relevant flags in s to designate it as a persistent group.
  481. */
  482. s->p2p_group = 1;
  483. s->p2p_persistent_group = 1;
  484. wpas_notify_persistent_group_added(wpa_s, s);
  485. wpa_config_set_network_defaults(s);
  486. }
  487. s->p2p_group = 1;
  488. s->p2p_persistent_group = 1;
  489. s->disabled = 2;
  490. s->bssid_set = 1;
  491. os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
  492. s->mode = ssid->mode;
  493. s->auth_alg = WPA_AUTH_ALG_OPEN;
  494. s->key_mgmt = WPA_KEY_MGMT_PSK;
  495. s->proto = WPA_PROTO_RSN;
  496. s->pairwise_cipher = WPA_CIPHER_CCMP;
  497. s->export_keys = 1;
  498. if (ssid->passphrase) {
  499. os_free(s->passphrase);
  500. s->passphrase = os_strdup(ssid->passphrase);
  501. }
  502. if (ssid->psk_set) {
  503. s->psk_set = 1;
  504. os_memcpy(s->psk, ssid->psk, 32);
  505. }
  506. if (s->passphrase && !s->psk_set)
  507. wpa_config_update_psk(s);
  508. if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
  509. os_free(s->ssid);
  510. s->ssid = os_malloc(ssid->ssid_len);
  511. }
  512. if (s->ssid) {
  513. s->ssid_len = ssid->ssid_len;
  514. os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
  515. }
  516. #ifndef CONFIG_NO_CONFIG_WRITE
  517. if (changed && wpa_s->conf->update_config &&
  518. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  519. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  520. }
  521. #endif /* CONFIG_NO_CONFIG_WRITE */
  522. return s->id;
  523. }
  524. static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
  525. const u8 *addr)
  526. {
  527. struct wpa_ssid *ssid, *s;
  528. u8 *n;
  529. size_t i;
  530. int found = 0;
  531. ssid = wpa_s->current_ssid;
  532. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  533. !ssid->p2p_persistent_group)
  534. return;
  535. for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
  536. if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
  537. continue;
  538. if (s->ssid_len == ssid->ssid_len &&
  539. os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
  540. break;
  541. }
  542. if (s == NULL)
  543. return;
  544. for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
  545. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
  546. ETH_ALEN) != 0)
  547. continue;
  548. if (i == s->num_p2p_clients - 1)
  549. return; /* already the most recent entry */
  550. /* move the entry to mark it most recent */
  551. os_memmove(s->p2p_client_list + i * ETH_ALEN,
  552. s->p2p_client_list + (i + 1) * ETH_ALEN,
  553. (s->num_p2p_clients - i - 1) * ETH_ALEN);
  554. os_memcpy(s->p2p_client_list +
  555. (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
  556. found = 1;
  557. break;
  558. }
  559. if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
  560. n = os_realloc_array(s->p2p_client_list,
  561. s->num_p2p_clients + 1, ETH_ALEN);
  562. if (n == NULL)
  563. return;
  564. os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
  565. s->p2p_client_list = n;
  566. s->num_p2p_clients++;
  567. } else if (!found) {
  568. /* Not enough room for an additional entry - drop the oldest
  569. * entry */
  570. os_memmove(s->p2p_client_list,
  571. s->p2p_client_list + ETH_ALEN,
  572. (s->num_p2p_clients - 1) * ETH_ALEN);
  573. os_memcpy(s->p2p_client_list +
  574. (s->num_p2p_clients - 1) * ETH_ALEN,
  575. addr, ETH_ALEN);
  576. }
  577. #ifndef CONFIG_NO_CONFIG_WRITE
  578. if (wpa_s->parent->conf->update_config &&
  579. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  580. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  581. #endif /* CONFIG_NO_CONFIG_WRITE */
  582. }
  583. static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
  584. int success)
  585. {
  586. struct wpa_ssid *ssid;
  587. const char *ssid_txt;
  588. int client;
  589. int persistent;
  590. u8 go_dev_addr[ETH_ALEN];
  591. int network_id = -1;
  592. /*
  593. * This callback is likely called for the main interface. Update wpa_s
  594. * to use the group interface if a new interface was created for the
  595. * group.
  596. */
  597. if (wpa_s->global->p2p_group_formation)
  598. wpa_s = wpa_s->global->p2p_group_formation;
  599. wpa_s->global->p2p_group_formation = NULL;
  600. wpa_s->p2p_in_provisioning = 0;
  601. if (!success) {
  602. wpa_msg_global(wpa_s->parent, MSG_INFO,
  603. P2P_EVENT_GROUP_FORMATION_FAILURE);
  604. wpas_p2p_group_delete(wpa_s,
  605. P2P_GROUP_REMOVAL_FORMATION_FAILED);
  606. return;
  607. }
  608. wpa_msg_global(wpa_s->parent, MSG_INFO,
  609. P2P_EVENT_GROUP_FORMATION_SUCCESS);
  610. ssid = wpa_s->current_ssid;
  611. if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  612. ssid->mode = WPAS_MODE_P2P_GO;
  613. p2p_group_notif_formation_done(wpa_s->p2p_group);
  614. wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
  615. }
  616. persistent = 0;
  617. if (ssid) {
  618. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  619. client = ssid->mode == WPAS_MODE_INFRA;
  620. if (ssid->mode == WPAS_MODE_P2P_GO) {
  621. persistent = ssid->p2p_persistent_group;
  622. os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
  623. ETH_ALEN);
  624. } else
  625. persistent = wpas_p2p_persistent_group(wpa_s,
  626. go_dev_addr,
  627. ssid->ssid,
  628. ssid->ssid_len);
  629. } else {
  630. ssid_txt = "";
  631. client = wpa_s->p2p_group_interface ==
  632. P2P_GROUP_INTERFACE_CLIENT;
  633. os_memset(go_dev_addr, 0, ETH_ALEN);
  634. }
  635. wpa_s->show_group_started = 0;
  636. if (client) {
  637. /*
  638. * Indicate event only after successfully completed 4-way
  639. * handshake, i.e., when the interface is ready for data
  640. * packets.
  641. */
  642. wpa_s->show_group_started = 1;
  643. } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
  644. char psk[65];
  645. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  646. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  647. "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
  648. MACSTR "%s",
  649. wpa_s->ifname, ssid_txt, ssid->frequency, psk,
  650. MAC2STR(go_dev_addr),
  651. persistent ? " [PERSISTENT]" : "");
  652. wpas_p2p_cross_connect_setup(wpa_s);
  653. wpas_p2p_set_group_idle_timeout(wpa_s);
  654. } else {
  655. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  656. "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
  657. "go_dev_addr=" MACSTR "%s",
  658. wpa_s->ifname, ssid_txt,
  659. ssid ? ssid->frequency : 0,
  660. ssid && ssid->passphrase ? ssid->passphrase : "",
  661. MAC2STR(go_dev_addr),
  662. persistent ? " [PERSISTENT]" : "");
  663. wpas_p2p_cross_connect_setup(wpa_s);
  664. wpas_p2p_set_group_idle_timeout(wpa_s);
  665. }
  666. if (persistent)
  667. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  668. ssid, go_dev_addr);
  669. if (network_id < 0 && ssid)
  670. network_id = ssid->id;
  671. if (!client)
  672. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  673. }
  674. static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
  675. unsigned int freq,
  676. const u8 *dst, const u8 *src,
  677. const u8 *bssid,
  678. const u8 *data, size_t data_len,
  679. enum offchannel_send_action_result
  680. result)
  681. {
  682. enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
  683. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  684. return;
  685. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  686. return;
  687. switch (result) {
  688. case OFFCHANNEL_SEND_ACTION_SUCCESS:
  689. res = P2P_SEND_ACTION_SUCCESS;
  690. break;
  691. case OFFCHANNEL_SEND_ACTION_NO_ACK:
  692. res = P2P_SEND_ACTION_NO_ACK;
  693. break;
  694. case OFFCHANNEL_SEND_ACTION_FAILED:
  695. res = P2P_SEND_ACTION_FAILED;
  696. break;
  697. }
  698. p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
  699. if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
  700. wpa_s->pending_pd_before_join &&
  701. (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  702. os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
  703. wpa_s->p2p_fallback_to_go_neg) {
  704. wpa_s->pending_pd_before_join = 0;
  705. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
  706. "during p2p_connect-auto");
  707. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  708. return;
  709. }
  710. }
  711. static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
  712. const u8 *src, const u8 *bssid, const u8 *buf,
  713. size_t len, unsigned int wait_time)
  714. {
  715. struct wpa_supplicant *wpa_s = ctx;
  716. return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
  717. wait_time,
  718. wpas_p2p_send_action_tx_status, 1);
  719. }
  720. static void wpas_send_action_done(void *ctx)
  721. {
  722. struct wpa_supplicant *wpa_s = ctx;
  723. offchannel_send_action_done(wpa_s);
  724. }
  725. static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
  726. struct p2p_go_neg_results *params)
  727. {
  728. if (wpa_s->go_params == NULL) {
  729. wpa_s->go_params = os_malloc(sizeof(*params));
  730. if (wpa_s->go_params == NULL)
  731. return -1;
  732. }
  733. os_memcpy(wpa_s->go_params, params, sizeof(*params));
  734. return 0;
  735. }
  736. static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
  737. struct p2p_go_neg_results *res)
  738. {
  739. wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
  740. MAC2STR(res->peer_interface_addr));
  741. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
  742. res->ssid, res->ssid_len);
  743. wpa_supplicant_ap_deinit(wpa_s);
  744. wpas_copy_go_neg_results(wpa_s, res);
  745. if (res->wps_method == WPS_PBC)
  746. wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
  747. else {
  748. u16 dev_pw_id = DEV_PW_DEFAULT;
  749. if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
  750. dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
  751. wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
  752. wpa_s->p2p_pin, 1, dev_pw_id);
  753. }
  754. }
  755. static void p2p_go_configured(void *ctx, void *data)
  756. {
  757. struct wpa_supplicant *wpa_s = ctx;
  758. struct p2p_go_neg_results *params = data;
  759. struct wpa_ssid *ssid;
  760. int network_id = -1;
  761. ssid = wpa_s->current_ssid;
  762. if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
  763. wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
  764. if (wpa_s->global->p2p_group_formation == wpa_s)
  765. wpa_s->global->p2p_group_formation = NULL;
  766. if (os_strlen(params->passphrase) > 0) {
  767. wpa_msg_global(wpa_s->parent, MSG_INFO,
  768. P2P_EVENT_GROUP_STARTED
  769. "%s GO ssid=\"%s\" freq=%d "
  770. "passphrase=\"%s\" go_dev_addr=" MACSTR
  771. "%s", wpa_s->ifname,
  772. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  773. ssid->frequency, params->passphrase,
  774. MAC2STR(wpa_s->global->p2p_dev_addr),
  775. params->persistent_group ?
  776. " [PERSISTENT]" : "");
  777. } else {
  778. char psk[65];
  779. wpa_snprintf_hex(psk, sizeof(psk), params->psk,
  780. sizeof(params->psk));
  781. wpa_msg_global(wpa_s->parent, MSG_INFO,
  782. P2P_EVENT_GROUP_STARTED
  783. "%s GO ssid=\"%s\" freq=%d psk=%s "
  784. "go_dev_addr=" MACSTR "%s",
  785. wpa_s->ifname,
  786. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  787. ssid->frequency, psk,
  788. MAC2STR(wpa_s->global->p2p_dev_addr),
  789. params->persistent_group ?
  790. " [PERSISTENT]" : "");
  791. }
  792. if (params->persistent_group)
  793. network_id = wpas_p2p_store_persistent_group(
  794. wpa_s->parent, ssid,
  795. wpa_s->global->p2p_dev_addr);
  796. if (network_id < 0)
  797. network_id = ssid->id;
  798. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
  799. wpas_p2p_cross_connect_setup(wpa_s);
  800. wpas_p2p_set_group_idle_timeout(wpa_s);
  801. return;
  802. }
  803. wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
  804. if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
  805. params->peer_interface_addr)) {
  806. wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
  807. "filtering");
  808. return;
  809. }
  810. if (params->wps_method == WPS_PBC)
  811. wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
  812. params->peer_device_addr);
  813. else if (wpa_s->p2p_pin[0])
  814. wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
  815. wpa_s->p2p_pin, NULL, 0, 0);
  816. os_free(wpa_s->go_params);
  817. wpa_s->go_params = NULL;
  818. }
  819. static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
  820. struct p2p_go_neg_results *params,
  821. int group_formation)
  822. {
  823. struct wpa_ssid *ssid;
  824. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
  825. if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
  826. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
  827. "results");
  828. return;
  829. }
  830. ssid = wpa_config_add_network(wpa_s->conf);
  831. if (ssid == NULL) {
  832. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
  833. return;
  834. }
  835. wpa_s->show_group_started = 0;
  836. wpa_config_set_network_defaults(ssid);
  837. ssid->temporary = 1;
  838. ssid->p2p_group = 1;
  839. ssid->p2p_persistent_group = params->persistent_group;
  840. ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
  841. WPAS_MODE_P2P_GO;
  842. ssid->frequency = params->freq;
  843. ssid->ht40 = params->ht40;
  844. ssid->ssid = os_zalloc(params->ssid_len + 1);
  845. if (ssid->ssid) {
  846. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  847. ssid->ssid_len = params->ssid_len;
  848. }
  849. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  850. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  851. ssid->proto = WPA_PROTO_RSN;
  852. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  853. if (os_strlen(params->passphrase) > 0) {
  854. ssid->passphrase = os_strdup(params->passphrase);
  855. if (ssid->passphrase == NULL) {
  856. wpa_msg_global(wpa_s, MSG_ERROR,
  857. "P2P: Failed to copy passphrase for GO");
  858. wpa_config_remove_network(wpa_s->conf, ssid->id);
  859. return;
  860. }
  861. } else
  862. ssid->passphrase = NULL;
  863. ssid->psk_set = params->psk_set;
  864. if (ssid->psk_set)
  865. os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
  866. else if (ssid->passphrase)
  867. wpa_config_update_psk(ssid);
  868. ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
  869. wpa_s->ap_configured_cb = p2p_go_configured;
  870. wpa_s->ap_configured_cb_ctx = wpa_s;
  871. wpa_s->ap_configured_cb_data = wpa_s->go_params;
  872. wpa_s->connect_without_scan = ssid;
  873. wpa_s->reassociate = 1;
  874. wpa_s->disconnected = 0;
  875. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
  876. "start GO)");
  877. wpa_supplicant_req_scan(wpa_s, 0, 0);
  878. }
  879. static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
  880. const struct wpa_supplicant *src)
  881. {
  882. struct wpa_config *d;
  883. const struct wpa_config *s;
  884. d = dst->conf;
  885. s = src->conf;
  886. #define C(n) if (s->n) d->n = os_strdup(s->n)
  887. C(device_name);
  888. C(manufacturer);
  889. C(model_name);
  890. C(model_number);
  891. C(serial_number);
  892. C(config_methods);
  893. #undef C
  894. os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
  895. os_memcpy(d->sec_device_type, s->sec_device_type,
  896. sizeof(d->sec_device_type));
  897. d->num_sec_device_types = s->num_sec_device_types;
  898. d->p2p_group_idle = s->p2p_group_idle;
  899. d->p2p_intra_bss = s->p2p_intra_bss;
  900. d->persistent_reconnect = s->persistent_reconnect;
  901. d->max_num_sta = s->max_num_sta;
  902. d->pbc_in_m1 = s->pbc_in_m1;
  903. d->ignore_old_scan_res = s->ignore_old_scan_res;
  904. d->beacon_int = s->beacon_int;
  905. d->disassoc_low_ack = s->disassoc_low_ack;
  906. }
  907. static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
  908. char *ifname, size_t len)
  909. {
  910. char *ifname_ptr = wpa_s->ifname;
  911. if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
  912. os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
  913. ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
  914. }
  915. os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
  916. if (os_strlen(ifname) >= IFNAMSIZ &&
  917. os_strlen(wpa_s->ifname) < IFNAMSIZ) {
  918. /* Try to avoid going over the IFNAMSIZ length limit */
  919. os_snprintf(ifname, sizeof(ifname), "p2p-%d",
  920. wpa_s->p2p_group_idx);
  921. }
  922. }
  923. static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
  924. enum wpa_driver_if_type type)
  925. {
  926. char ifname[120], force_ifname[120];
  927. if (wpa_s->pending_interface_name[0]) {
  928. wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
  929. "- skip creation of a new one");
  930. if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
  931. wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
  932. "unknown?! ifname='%s'",
  933. wpa_s->pending_interface_name);
  934. return -1;
  935. }
  936. return 0;
  937. }
  938. wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
  939. force_ifname[0] = '\0';
  940. wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
  941. ifname);
  942. wpa_s->p2p_group_idx++;
  943. wpa_s->pending_interface_type = type;
  944. if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
  945. wpa_s->pending_interface_addr, NULL) < 0) {
  946. wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
  947. "interface");
  948. return -1;
  949. }
  950. if (force_ifname[0]) {
  951. wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
  952. force_ifname);
  953. os_strlcpy(wpa_s->pending_interface_name, force_ifname,
  954. sizeof(wpa_s->pending_interface_name));
  955. } else
  956. os_strlcpy(wpa_s->pending_interface_name, ifname,
  957. sizeof(wpa_s->pending_interface_name));
  958. wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
  959. MACSTR, wpa_s->pending_interface_name,
  960. MAC2STR(wpa_s->pending_interface_addr));
  961. return 0;
  962. }
  963. static void wpas_p2p_remove_pending_group_interface(
  964. struct wpa_supplicant *wpa_s)
  965. {
  966. if (!wpa_s->pending_interface_name[0] ||
  967. is_zero_ether_addr(wpa_s->pending_interface_addr))
  968. return; /* No pending virtual interface */
  969. wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
  970. wpa_s->pending_interface_name);
  971. wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
  972. wpa_s->pending_interface_name);
  973. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  974. wpa_s->pending_interface_name[0] = '\0';
  975. }
  976. static struct wpa_supplicant *
  977. wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
  978. {
  979. struct wpa_interface iface;
  980. struct wpa_supplicant *group_wpa_s;
  981. if (!wpa_s->pending_interface_name[0]) {
  982. wpa_printf(MSG_ERROR, "P2P: No pending group interface");
  983. if (!wpas_p2p_create_iface(wpa_s))
  984. return NULL;
  985. /*
  986. * Something has forced us to remove the pending interface; try
  987. * to create a new one and hope for the best that we will get
  988. * the same local address.
  989. */
  990. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  991. WPA_IF_P2P_CLIENT) < 0)
  992. return NULL;
  993. }
  994. os_memset(&iface, 0, sizeof(iface));
  995. iface.ifname = wpa_s->pending_interface_name;
  996. iface.driver = wpa_s->driver->name;
  997. if (wpa_s->conf->ctrl_interface == NULL &&
  998. wpa_s->parent != wpa_s &&
  999. wpa_s->p2p_mgmt &&
  1000. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
  1001. iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
  1002. else
  1003. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  1004. iface.driver_param = wpa_s->conf->driver_param;
  1005. group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  1006. if (group_wpa_s == NULL) {
  1007. wpa_printf(MSG_ERROR, "P2P: Failed to create new "
  1008. "wpa_supplicant interface");
  1009. return NULL;
  1010. }
  1011. wpa_s->pending_interface_name[0] = '\0';
  1012. group_wpa_s->parent = wpa_s;
  1013. group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
  1014. P2P_GROUP_INTERFACE_CLIENT;
  1015. wpa_s->global->p2p_group_formation = group_wpa_s;
  1016. wpas_p2p_clone_config(group_wpa_s, wpa_s);
  1017. return group_wpa_s;
  1018. }
  1019. static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
  1020. void *timeout_ctx)
  1021. {
  1022. struct wpa_supplicant *wpa_s = eloop_ctx;
  1023. wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
  1024. wpas_p2p_group_formation_failed(wpa_s);
  1025. }
  1026. void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
  1027. {
  1028. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  1029. wpa_s->parent, NULL);
  1030. if (wpa_s->global->p2p)
  1031. p2p_group_formation_failed(wpa_s->global->p2p);
  1032. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1033. wpa_drv_p2p_group_formation_failed(wpa_s);
  1034. wpas_group_formation_completed(wpa_s, 0);
  1035. }
  1036. void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
  1037. {
  1038. struct wpa_supplicant *wpa_s = ctx;
  1039. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1040. wpa_drv_cancel_remain_on_channel(wpa_s);
  1041. wpa_s->off_channel_freq = 0;
  1042. wpa_s->roc_waiting_drv_freq = 0;
  1043. }
  1044. if (res->status) {
  1045. wpa_msg_global(wpa_s, MSG_INFO,
  1046. P2P_EVENT_GO_NEG_FAILURE "status=%d",
  1047. res->status);
  1048. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1049. wpas_p2p_remove_pending_group_interface(wpa_s);
  1050. return;
  1051. }
  1052. if (wpa_s->p2p_go_ht40)
  1053. res->ht40 = 1;
  1054. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
  1055. "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
  1056. " wps_method=%s",
  1057. res->role_go ? "GO" : "client", res->freq, res->ht40,
  1058. MAC2STR(res->peer_device_addr),
  1059. MAC2STR(res->peer_interface_addr),
  1060. p2p_wps_method_text(res->wps_method));
  1061. wpas_notify_p2p_go_neg_completed(wpa_s, res);
  1062. if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
  1063. struct wpa_ssid *ssid;
  1064. ssid = wpa_config_get_network(wpa_s->conf,
  1065. wpa_s->p2p_persistent_id);
  1066. if (ssid && ssid->disabled == 2 &&
  1067. ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
  1068. size_t len = os_strlen(ssid->passphrase);
  1069. wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
  1070. "on requested persistent group");
  1071. os_memcpy(res->passphrase, ssid->passphrase, len);
  1072. res->passphrase[len] = '\0';
  1073. }
  1074. }
  1075. if (wpa_s->create_p2p_iface) {
  1076. struct wpa_supplicant *group_wpa_s =
  1077. wpas_p2p_init_group_interface(wpa_s, res->role_go);
  1078. if (group_wpa_s == NULL) {
  1079. wpas_p2p_remove_pending_group_interface(wpa_s);
  1080. return;
  1081. }
  1082. if (group_wpa_s != wpa_s) {
  1083. os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
  1084. sizeof(group_wpa_s->p2p_pin));
  1085. group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
  1086. }
  1087. os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
  1088. wpa_s->pending_interface_name[0] = '\0';
  1089. group_wpa_s->p2p_in_provisioning = 1;
  1090. if (res->role_go)
  1091. wpas_start_wps_go(group_wpa_s, res, 1);
  1092. else
  1093. wpas_start_wps_enrollee(group_wpa_s, res);
  1094. } else {
  1095. wpa_s->p2p_in_provisioning = 1;
  1096. wpa_s->global->p2p_group_formation = wpa_s;
  1097. if (res->role_go)
  1098. wpas_start_wps_go(wpa_s, res, 1);
  1099. else
  1100. wpas_start_wps_enrollee(ctx, res);
  1101. }
  1102. wpa_s->p2p_long_listen = 0;
  1103. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  1104. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1105. eloop_register_timeout(15 + res->peer_config_timeout / 100,
  1106. (res->peer_config_timeout % 100) * 10000,
  1107. wpas_p2p_group_formation_timeout, wpa_s, NULL);
  1108. }
  1109. void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
  1110. {
  1111. struct wpa_supplicant *wpa_s = ctx;
  1112. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
  1113. " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
  1114. wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
  1115. }
  1116. void wpas_dev_found(void *ctx, const u8 *addr,
  1117. const struct p2p_peer_info *info,
  1118. int new_device)
  1119. {
  1120. #ifndef CONFIG_NO_STDOUT_DEBUG
  1121. struct wpa_supplicant *wpa_s = ctx;
  1122. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1123. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
  1124. " p2p_dev_addr=" MACSTR
  1125. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1126. "dev_capab=0x%x group_capab=0x%x",
  1127. MAC2STR(addr), MAC2STR(info->p2p_device_addr),
  1128. wps_dev_type_bin2str(info->pri_dev_type, devtype,
  1129. sizeof(devtype)),
  1130. info->device_name, info->config_methods,
  1131. info->dev_capab, info->group_capab);
  1132. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1133. wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
  1134. }
  1135. static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
  1136. {
  1137. struct wpa_supplicant *wpa_s = ctx;
  1138. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
  1139. "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
  1140. wpas_notify_p2p_device_lost(wpa_s, dev_addr);
  1141. }
  1142. static void wpas_find_stopped(void *ctx)
  1143. {
  1144. struct wpa_supplicant *wpa_s = ctx;
  1145. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
  1146. }
  1147. static int wpas_start_listen(void *ctx, unsigned int freq,
  1148. unsigned int duration,
  1149. const struct wpabuf *probe_resp_ie)
  1150. {
  1151. struct wpa_supplicant *wpa_s = ctx;
  1152. wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
  1153. if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
  1154. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
  1155. "report received Probe Request frames");
  1156. return -1;
  1157. }
  1158. wpa_s->pending_listen_freq = freq;
  1159. wpa_s->pending_listen_duration = duration;
  1160. if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
  1161. wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
  1162. "to remain on channel (%u MHz) for Listen "
  1163. "state", freq);
  1164. wpa_s->pending_listen_freq = 0;
  1165. return -1;
  1166. }
  1167. wpa_s->off_channel_freq = 0;
  1168. wpa_s->roc_waiting_drv_freq = freq;
  1169. return 0;
  1170. }
  1171. static void wpas_stop_listen(void *ctx)
  1172. {
  1173. struct wpa_supplicant *wpa_s = ctx;
  1174. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  1175. wpa_drv_cancel_remain_on_channel(wpa_s);
  1176. wpa_s->off_channel_freq = 0;
  1177. wpa_s->roc_waiting_drv_freq = 0;
  1178. }
  1179. wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
  1180. wpa_drv_probe_req_report(wpa_s, 0);
  1181. }
  1182. static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
  1183. {
  1184. struct wpa_supplicant *wpa_s = ctx;
  1185. return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
  1186. }
  1187. /*
  1188. * DNS Header section is used only to calculate compression pointers, so the
  1189. * contents of this data does not matter, but the length needs to be reserved
  1190. * in the virtual packet.
  1191. */
  1192. #define DNS_HEADER_LEN 12
  1193. /*
  1194. * 27-octet in-memory packet from P2P specification containing two implied
  1195. * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
  1196. */
  1197. #define P2P_SD_IN_MEMORY_LEN 27
  1198. static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
  1199. u8 **spos, const u8 *end)
  1200. {
  1201. while (*spos < end) {
  1202. u8 val = ((*spos)[0] & 0xc0) >> 6;
  1203. int len;
  1204. if (val == 1 || val == 2) {
  1205. /* These are reserved values in RFC 1035 */
  1206. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1207. "sequence starting with 0x%x", val);
  1208. return -1;
  1209. }
  1210. if (val == 3) {
  1211. u16 offset;
  1212. u8 *spos_tmp;
  1213. /* Offset */
  1214. if (*spos + 2 > end) {
  1215. wpa_printf(MSG_DEBUG, "P2P: No room for full "
  1216. "DNS offset field");
  1217. return -1;
  1218. }
  1219. offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
  1220. if (offset >= *spos - start) {
  1221. wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
  1222. "pointer offset %u", offset);
  1223. return -1;
  1224. }
  1225. (*spos) += 2;
  1226. spos_tmp = start + offset;
  1227. return p2p_sd_dns_uncompress_label(upos, uend, start,
  1228. &spos_tmp,
  1229. *spos - 2);
  1230. }
  1231. /* Label */
  1232. len = (*spos)[0] & 0x3f;
  1233. if (len == 0)
  1234. return 0;
  1235. (*spos)++;
  1236. if (*spos + len > end) {
  1237. wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
  1238. "sequence - no room for label with length "
  1239. "%u", len);
  1240. return -1;
  1241. }
  1242. if (*upos + len + 2 > uend)
  1243. return -2;
  1244. os_memcpy(*upos, *spos, len);
  1245. *spos += len;
  1246. *upos += len;
  1247. (*upos)[0] = '.';
  1248. (*upos)++;
  1249. (*upos)[0] = '\0';
  1250. }
  1251. return 0;
  1252. }
  1253. /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
  1254. * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
  1255. * not large enough */
  1256. static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
  1257. size_t msg_len, size_t offset)
  1258. {
  1259. /* 27-octet in-memory packet from P2P specification */
  1260. const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
  1261. "\x04_udp\xC0\x11\x00\x0C\x00\x01";
  1262. u8 *tmp, *end, *spos;
  1263. char *upos, *uend;
  1264. int ret = 0;
  1265. if (buf_len < 2)
  1266. return -1;
  1267. if (offset > msg_len)
  1268. return -1;
  1269. tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
  1270. if (tmp == NULL)
  1271. return -1;
  1272. spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
  1273. end = spos + msg_len;
  1274. spos += offset;
  1275. os_memset(tmp, 0, DNS_HEADER_LEN);
  1276. os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
  1277. os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
  1278. upos = buf;
  1279. uend = buf + buf_len;
  1280. ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
  1281. if (ret) {
  1282. os_free(tmp);
  1283. return ret;
  1284. }
  1285. if (upos == buf) {
  1286. upos[0] = '.';
  1287. upos[1] = '\0';
  1288. } else if (upos[-1] == '.')
  1289. upos[-1] = '\0';
  1290. os_free(tmp);
  1291. return 0;
  1292. }
  1293. static struct p2p_srv_bonjour *
  1294. wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
  1295. const struct wpabuf *query)
  1296. {
  1297. struct p2p_srv_bonjour *bsrv;
  1298. size_t len;
  1299. len = wpabuf_len(query);
  1300. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1301. struct p2p_srv_bonjour, list) {
  1302. if (len == wpabuf_len(bsrv->query) &&
  1303. os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
  1304. len) == 0)
  1305. return bsrv;
  1306. }
  1307. return NULL;
  1308. }
  1309. static struct p2p_srv_upnp *
  1310. wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1311. const char *service)
  1312. {
  1313. struct p2p_srv_upnp *usrv;
  1314. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1315. struct p2p_srv_upnp, list) {
  1316. if (version == usrv->version &&
  1317. os_strcmp(service, usrv->service) == 0)
  1318. return usrv;
  1319. }
  1320. return NULL;
  1321. }
  1322. static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
  1323. u8 srv_trans_id)
  1324. {
  1325. u8 *len_pos;
  1326. if (wpabuf_tailroom(resp) < 5)
  1327. return;
  1328. /* Length (to be filled) */
  1329. len_pos = wpabuf_put(resp, 2);
  1330. wpabuf_put_u8(resp, srv_proto);
  1331. wpabuf_put_u8(resp, srv_trans_id);
  1332. /* Status Code */
  1333. wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
  1334. /* Response Data: empty */
  1335. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1336. }
  1337. static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
  1338. struct wpabuf *resp, u8 srv_trans_id)
  1339. {
  1340. struct p2p_srv_bonjour *bsrv;
  1341. u8 *len_pos;
  1342. wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
  1343. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1344. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1345. return;
  1346. }
  1347. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1348. struct p2p_srv_bonjour, list) {
  1349. if (wpabuf_tailroom(resp) <
  1350. 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
  1351. return;
  1352. /* Length (to be filled) */
  1353. len_pos = wpabuf_put(resp, 2);
  1354. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1355. wpabuf_put_u8(resp, srv_trans_id);
  1356. /* Status Code */
  1357. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1358. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1359. wpabuf_head(bsrv->resp),
  1360. wpabuf_len(bsrv->resp));
  1361. /* Response Data */
  1362. wpabuf_put_buf(resp, bsrv->query); /* Key */
  1363. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1364. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1365. 2);
  1366. }
  1367. }
  1368. static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
  1369. size_t query_len)
  1370. {
  1371. char str_rx[256], str_srv[256];
  1372. if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
  1373. return 0; /* Too short to include DNS Type and Version */
  1374. if (os_memcmp(query + query_len - 3,
  1375. wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
  1376. 3) != 0)
  1377. return 0; /* Mismatch in DNS Type or Version */
  1378. if (query_len == wpabuf_len(bsrv->query) &&
  1379. os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
  1380. return 1; /* Binary match */
  1381. if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
  1382. 0))
  1383. return 0; /* Failed to uncompress query */
  1384. if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
  1385. wpabuf_head(bsrv->query),
  1386. wpabuf_len(bsrv->query) - 3, 0))
  1387. return 0; /* Failed to uncompress service */
  1388. return os_strcmp(str_rx, str_srv) == 0;
  1389. }
  1390. static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
  1391. struct wpabuf *resp, u8 srv_trans_id,
  1392. const u8 *query, size_t query_len)
  1393. {
  1394. struct p2p_srv_bonjour *bsrv;
  1395. u8 *len_pos;
  1396. int matches = 0;
  1397. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
  1398. query, query_len);
  1399. if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1400. wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
  1401. wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
  1402. srv_trans_id);
  1403. return;
  1404. }
  1405. if (query_len == 0) {
  1406. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1407. return;
  1408. }
  1409. dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
  1410. struct p2p_srv_bonjour, list) {
  1411. if (!match_bonjour_query(bsrv, query, query_len))
  1412. continue;
  1413. if (wpabuf_tailroom(resp) <
  1414. 5 + query_len + wpabuf_len(bsrv->resp))
  1415. return;
  1416. matches++;
  1417. /* Length (to be filled) */
  1418. len_pos = wpabuf_put(resp, 2);
  1419. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1420. wpabuf_put_u8(resp, srv_trans_id);
  1421. /* Status Code */
  1422. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1423. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
  1424. wpabuf_head(bsrv->resp),
  1425. wpabuf_len(bsrv->resp));
  1426. /* Response Data */
  1427. wpabuf_put_data(resp, query, query_len); /* Key */
  1428. wpabuf_put_buf(resp, bsrv->resp); /* Value */
  1429. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1430. }
  1431. if (matches == 0) {
  1432. wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
  1433. "available");
  1434. if (wpabuf_tailroom(resp) < 5)
  1435. return;
  1436. /* Length (to be filled) */
  1437. len_pos = wpabuf_put(resp, 2);
  1438. wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
  1439. wpabuf_put_u8(resp, srv_trans_id);
  1440. /* Status Code */
  1441. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1442. /* Response Data: empty */
  1443. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1444. 2);
  1445. }
  1446. }
  1447. static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
  1448. struct wpabuf *resp, u8 srv_trans_id)
  1449. {
  1450. struct p2p_srv_upnp *usrv;
  1451. u8 *len_pos;
  1452. wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
  1453. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1454. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1455. return;
  1456. }
  1457. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1458. struct p2p_srv_upnp, list) {
  1459. if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
  1460. return;
  1461. /* Length (to be filled) */
  1462. len_pos = wpabuf_put(resp, 2);
  1463. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1464. wpabuf_put_u8(resp, srv_trans_id);
  1465. /* Status Code */
  1466. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1467. /* Response Data */
  1468. wpabuf_put_u8(resp, usrv->version);
  1469. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1470. usrv->service);
  1471. wpabuf_put_str(resp, usrv->service);
  1472. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
  1473. 2);
  1474. }
  1475. }
  1476. static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
  1477. struct wpabuf *resp, u8 srv_trans_id,
  1478. const u8 *query, size_t query_len)
  1479. {
  1480. struct p2p_srv_upnp *usrv;
  1481. u8 *len_pos;
  1482. u8 version;
  1483. char *str;
  1484. int count = 0;
  1485. wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
  1486. query, query_len);
  1487. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
  1488. wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
  1489. wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
  1490. srv_trans_id);
  1491. return;
  1492. }
  1493. if (query_len == 0) {
  1494. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1495. return;
  1496. }
  1497. if (wpabuf_tailroom(resp) < 5)
  1498. return;
  1499. /* Length (to be filled) */
  1500. len_pos = wpabuf_put(resp, 2);
  1501. wpabuf_put_u8(resp, P2P_SERV_UPNP);
  1502. wpabuf_put_u8(resp, srv_trans_id);
  1503. version = query[0];
  1504. str = os_malloc(query_len);
  1505. if (str == NULL)
  1506. return;
  1507. os_memcpy(str, query + 1, query_len - 1);
  1508. str[query_len - 1] = '\0';
  1509. dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
  1510. struct p2p_srv_upnp, list) {
  1511. if (version != usrv->version)
  1512. continue;
  1513. if (os_strcmp(str, "ssdp:all") != 0 &&
  1514. os_strstr(usrv->service, str) == NULL)
  1515. continue;
  1516. if (wpabuf_tailroom(resp) < 2)
  1517. break;
  1518. if (count == 0) {
  1519. /* Status Code */
  1520. wpabuf_put_u8(resp, P2P_SD_SUCCESS);
  1521. /* Response Data */
  1522. wpabuf_put_u8(resp, version);
  1523. } else
  1524. wpabuf_put_u8(resp, ',');
  1525. count++;
  1526. wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
  1527. usrv->service);
  1528. if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
  1529. break;
  1530. wpabuf_put_str(resp, usrv->service);
  1531. }
  1532. os_free(str);
  1533. if (count == 0) {
  1534. wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
  1535. "available");
  1536. /* Status Code */
  1537. wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
  1538. /* Response Data: empty */
  1539. }
  1540. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1541. }
  1542. #ifdef CONFIG_WIFI_DISPLAY
  1543. static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
  1544. struct wpabuf *resp, u8 srv_trans_id,
  1545. const u8 *query, size_t query_len)
  1546. {
  1547. const u8 *pos;
  1548. u8 role;
  1549. u8 *len_pos;
  1550. wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
  1551. if (!wpa_s->global->wifi_display) {
  1552. wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
  1553. wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
  1554. srv_trans_id);
  1555. return;
  1556. }
  1557. if (query_len < 1) {
  1558. wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
  1559. "Role");
  1560. return;
  1561. }
  1562. if (wpabuf_tailroom(resp) < 5)
  1563. return;
  1564. pos = query;
  1565. role = *pos++;
  1566. wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
  1567. /* TODO: role specific handling */
  1568. /* Length (to be filled) */
  1569. len_pos = wpabuf_put(resp, 2);
  1570. wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
  1571. wpabuf_put_u8(resp, srv_trans_id);
  1572. wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
  1573. while (pos < query + query_len) {
  1574. if (*pos < MAX_WFD_SUBELEMS &&
  1575. wpa_s->global->wfd_subelem[*pos] &&
  1576. wpabuf_tailroom(resp) >=
  1577. wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
  1578. wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
  1579. "subelement %u", *pos);
  1580. wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
  1581. }
  1582. pos++;
  1583. }
  1584. WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
  1585. }
  1586. #endif /* CONFIG_WIFI_DISPLAY */
  1587. void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
  1588. u16 update_indic, const u8 *tlvs, size_t tlvs_len)
  1589. {
  1590. struct wpa_supplicant *wpa_s = ctx;
  1591. const u8 *pos = tlvs;
  1592. const u8 *end = tlvs + tlvs_len;
  1593. const u8 *tlv_end;
  1594. u16 slen;
  1595. struct wpabuf *resp;
  1596. u8 srv_proto, srv_trans_id;
  1597. size_t buf_len;
  1598. char *buf;
  1599. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
  1600. tlvs, tlvs_len);
  1601. buf_len = 2 * tlvs_len + 1;
  1602. buf = os_malloc(buf_len);
  1603. if (buf) {
  1604. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1605. wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
  1606. MACSTR " %u %u %s",
  1607. freq, MAC2STR(sa), dialog_token, update_indic,
  1608. buf);
  1609. os_free(buf);
  1610. }
  1611. if (wpa_s->p2p_sd_over_ctrl_iface) {
  1612. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1613. update_indic, tlvs, tlvs_len);
  1614. return; /* to be processed by an external program */
  1615. }
  1616. resp = wpabuf_alloc(10000);
  1617. if (resp == NULL)
  1618. return;
  1619. while (pos + 1 < end) {
  1620. wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
  1621. slen = WPA_GET_LE16(pos);
  1622. pos += 2;
  1623. if (pos + slen > end || slen < 2) {
  1624. wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
  1625. "length");
  1626. wpabuf_free(resp);
  1627. return;
  1628. }
  1629. tlv_end = pos + slen;
  1630. srv_proto = *pos++;
  1631. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1632. srv_proto);
  1633. srv_trans_id = *pos++;
  1634. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1635. srv_trans_id);
  1636. wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
  1637. pos, tlv_end - pos);
  1638. if (wpa_s->force_long_sd) {
  1639. wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
  1640. "response");
  1641. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1642. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1643. goto done;
  1644. }
  1645. switch (srv_proto) {
  1646. case P2P_SERV_ALL_SERVICES:
  1647. wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
  1648. "for all services");
  1649. if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
  1650. dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
  1651. wpa_printf(MSG_DEBUG, "P2P: No service "
  1652. "discovery protocols available");
  1653. wpas_sd_add_proto_not_avail(
  1654. resp, P2P_SERV_ALL_SERVICES,
  1655. srv_trans_id);
  1656. break;
  1657. }
  1658. wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
  1659. wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
  1660. break;
  1661. case P2P_SERV_BONJOUR:
  1662. wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
  1663. pos, tlv_end - pos);
  1664. break;
  1665. case P2P_SERV_UPNP:
  1666. wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
  1667. pos, tlv_end - pos);
  1668. break;
  1669. #ifdef CONFIG_WIFI_DISPLAY
  1670. case P2P_SERV_WIFI_DISPLAY:
  1671. wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
  1672. pos, tlv_end - pos);
  1673. break;
  1674. #endif /* CONFIG_WIFI_DISPLAY */
  1675. default:
  1676. wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
  1677. "protocol %u", srv_proto);
  1678. wpas_sd_add_proto_not_avail(resp, srv_proto,
  1679. srv_trans_id);
  1680. break;
  1681. }
  1682. pos = tlv_end;
  1683. }
  1684. done:
  1685. wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
  1686. update_indic, tlvs, tlvs_len);
  1687. wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
  1688. wpabuf_free(resp);
  1689. }
  1690. void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
  1691. const u8 *tlvs, size_t tlvs_len)
  1692. {
  1693. struct wpa_supplicant *wpa_s = ctx;
  1694. const u8 *pos = tlvs;
  1695. const u8 *end = tlvs + tlvs_len;
  1696. const u8 *tlv_end;
  1697. u16 slen;
  1698. size_t buf_len;
  1699. char *buf;
  1700. wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
  1701. tlvs, tlvs_len);
  1702. if (tlvs_len > 1500) {
  1703. /* TODO: better way for handling this */
  1704. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1705. P2P_EVENT_SERV_DISC_RESP MACSTR
  1706. " %u <long response: %u bytes>",
  1707. MAC2STR(sa), update_indic,
  1708. (unsigned int) tlvs_len);
  1709. } else {
  1710. buf_len = 2 * tlvs_len + 1;
  1711. buf = os_malloc(buf_len);
  1712. if (buf) {
  1713. wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
  1714. wpa_msg_ctrl(wpa_s, MSG_INFO,
  1715. P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
  1716. MAC2STR(sa), update_indic, buf);
  1717. os_free(buf);
  1718. }
  1719. }
  1720. while (pos < end) {
  1721. u8 srv_proto, srv_trans_id, status;
  1722. wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
  1723. slen = WPA_GET_LE16(pos);
  1724. pos += 2;
  1725. if (pos + slen > end || slen < 3) {
  1726. wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
  1727. "length");
  1728. return;
  1729. }
  1730. tlv_end = pos + slen;
  1731. srv_proto = *pos++;
  1732. wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
  1733. srv_proto);
  1734. srv_trans_id = *pos++;
  1735. wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
  1736. srv_trans_id);
  1737. status = *pos++;
  1738. wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
  1739. status);
  1740. wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
  1741. pos, tlv_end - pos);
  1742. pos = tlv_end;
  1743. }
  1744. wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
  1745. }
  1746. u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
  1747. const struct wpabuf *tlvs)
  1748. {
  1749. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1750. return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
  1751. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1752. return 0;
  1753. return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
  1754. }
  1755. u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
  1756. u8 version, const char *query)
  1757. {
  1758. struct wpabuf *tlvs;
  1759. u64 ret;
  1760. tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
  1761. if (tlvs == NULL)
  1762. return 0;
  1763. wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
  1764. wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
  1765. wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
  1766. wpabuf_put_u8(tlvs, version);
  1767. wpabuf_put_str(tlvs, query);
  1768. ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1769. wpabuf_free(tlvs);
  1770. return ret;
  1771. }
  1772. #ifdef CONFIG_WIFI_DISPLAY
  1773. static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
  1774. const struct wpabuf *tlvs)
  1775. {
  1776. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1777. return 0;
  1778. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1779. return 0;
  1780. return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
  1781. }
  1782. #define MAX_WFD_SD_SUBELEMS 20
  1783. static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
  1784. const char *subelems)
  1785. {
  1786. u8 *len;
  1787. const char *pos;
  1788. int val;
  1789. int count = 0;
  1790. len = wpabuf_put(tlvs, 2);
  1791. wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
  1792. wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
  1793. wpabuf_put_u8(tlvs, role);
  1794. pos = subelems;
  1795. while (*pos) {
  1796. val = atoi(pos);
  1797. if (val >= 0 && val < 256) {
  1798. wpabuf_put_u8(tlvs, val);
  1799. count++;
  1800. if (count == MAX_WFD_SD_SUBELEMS)
  1801. break;
  1802. }
  1803. pos = os_strchr(pos + 1, ',');
  1804. if (pos == NULL)
  1805. break;
  1806. pos++;
  1807. }
  1808. WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
  1809. }
  1810. u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
  1811. const u8 *dst, const char *role)
  1812. {
  1813. struct wpabuf *tlvs;
  1814. u64 ret;
  1815. const char *subelems;
  1816. u8 id = 1;
  1817. subelems = os_strchr(role, ' ');
  1818. if (subelems == NULL)
  1819. return 0;
  1820. subelems++;
  1821. tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
  1822. if (tlvs == NULL)
  1823. return 0;
  1824. if (os_strstr(role, "[source]"))
  1825. wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
  1826. if (os_strstr(role, "[pri-sink]"))
  1827. wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
  1828. if (os_strstr(role, "[sec-sink]"))
  1829. wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
  1830. if (os_strstr(role, "[source+sink]"))
  1831. wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
  1832. ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
  1833. wpabuf_free(tlvs);
  1834. return ret;
  1835. }
  1836. #endif /* CONFIG_WIFI_DISPLAY */
  1837. int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
  1838. {
  1839. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  1840. return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
  1841. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1842. return -1;
  1843. return p2p_sd_cancel_request(wpa_s->global->p2p,
  1844. (void *) (uintptr_t) req);
  1845. }
  1846. void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
  1847. const u8 *dst, u8 dialog_token,
  1848. const struct wpabuf *resp_tlvs)
  1849. {
  1850. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1851. wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
  1852. resp_tlvs);
  1853. return;
  1854. }
  1855. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  1856. return;
  1857. p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
  1858. resp_tlvs);
  1859. }
  1860. void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
  1861. {
  1862. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  1863. wpa_drv_p2p_service_update(wpa_s);
  1864. return;
  1865. }
  1866. if (wpa_s->global->p2p)
  1867. p2p_sd_service_update(wpa_s->global->p2p);
  1868. }
  1869. static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
  1870. {
  1871. dl_list_del(&bsrv->list);
  1872. wpabuf_free(bsrv->query);
  1873. wpabuf_free(bsrv->resp);
  1874. os_free(bsrv);
  1875. }
  1876. static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
  1877. {
  1878. dl_list_del(&usrv->list);
  1879. os_free(usrv->service);
  1880. os_free(usrv);
  1881. }
  1882. void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
  1883. {
  1884. struct p2p_srv_bonjour *bsrv, *bn;
  1885. struct p2p_srv_upnp *usrv, *un;
  1886. dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
  1887. struct p2p_srv_bonjour, list)
  1888. wpas_p2p_srv_bonjour_free(bsrv);
  1889. dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
  1890. struct p2p_srv_upnp, list)
  1891. wpas_p2p_srv_upnp_free(usrv);
  1892. wpas_p2p_sd_service_update(wpa_s);
  1893. }
  1894. int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1895. struct wpabuf *query, struct wpabuf *resp)
  1896. {
  1897. struct p2p_srv_bonjour *bsrv;
  1898. bsrv = os_zalloc(sizeof(*bsrv));
  1899. if (bsrv == NULL)
  1900. return -1;
  1901. bsrv->query = query;
  1902. bsrv->resp = resp;
  1903. dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
  1904. wpas_p2p_sd_service_update(wpa_s);
  1905. return 0;
  1906. }
  1907. int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1908. const struct wpabuf *query)
  1909. {
  1910. struct p2p_srv_bonjour *bsrv;
  1911. bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
  1912. if (bsrv == NULL)
  1913. return -1;
  1914. wpas_p2p_srv_bonjour_free(bsrv);
  1915. wpas_p2p_sd_service_update(wpa_s);
  1916. return 0;
  1917. }
  1918. int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1919. const char *service)
  1920. {
  1921. struct p2p_srv_upnp *usrv;
  1922. if (wpas_p2p_service_get_upnp(wpa_s, version, service))
  1923. return 0; /* Already listed */
  1924. usrv = os_zalloc(sizeof(*usrv));
  1925. if (usrv == NULL)
  1926. return -1;
  1927. usrv->version = version;
  1928. usrv->service = os_strdup(service);
  1929. if (usrv->service == NULL) {
  1930. os_free(usrv);
  1931. return -1;
  1932. }
  1933. dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
  1934. wpas_p2p_sd_service_update(wpa_s);
  1935. return 0;
  1936. }
  1937. int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
  1938. const char *service)
  1939. {
  1940. struct p2p_srv_upnp *usrv;
  1941. usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
  1942. if (usrv == NULL)
  1943. return -1;
  1944. wpas_p2p_srv_upnp_free(usrv);
  1945. wpas_p2p_sd_service_update(wpa_s);
  1946. return 0;
  1947. }
  1948. static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
  1949. const u8 *peer, const char *params,
  1950. unsigned int generated_pin)
  1951. {
  1952. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
  1953. " %08d%s", MAC2STR(peer), generated_pin, params);
  1954. }
  1955. static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
  1956. const u8 *peer, const char *params)
  1957. {
  1958. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
  1959. "%s", MAC2STR(peer), params);
  1960. }
  1961. void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
  1962. const u8 *dev_addr, const u8 *pri_dev_type,
  1963. const char *dev_name, u16 supp_config_methods,
  1964. u8 dev_capab, u8 group_capab, const u8 *group_id,
  1965. size_t group_id_len)
  1966. {
  1967. struct wpa_supplicant *wpa_s = ctx;
  1968. char devtype[WPS_DEV_TYPE_BUFSIZE];
  1969. char params[300];
  1970. u8 empty_dev_type[8];
  1971. unsigned int generated_pin = 0;
  1972. struct wpa_supplicant *group = NULL;
  1973. if (group_id) {
  1974. for (group = wpa_s->global->ifaces; group; group = group->next)
  1975. {
  1976. struct wpa_ssid *s = group->current_ssid;
  1977. if (s != NULL &&
  1978. s->mode == WPAS_MODE_P2P_GO &&
  1979. group_id_len - ETH_ALEN == s->ssid_len &&
  1980. os_memcmp(group_id + ETH_ALEN, s->ssid,
  1981. s->ssid_len) == 0)
  1982. break;
  1983. }
  1984. }
  1985. if (pri_dev_type == NULL) {
  1986. os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
  1987. pri_dev_type = empty_dev_type;
  1988. }
  1989. os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
  1990. " pri_dev_type=%s name='%s' config_methods=0x%x "
  1991. "dev_capab=0x%x group_capab=0x%x%s%s",
  1992. MAC2STR(dev_addr),
  1993. wps_dev_type_bin2str(pri_dev_type, devtype,
  1994. sizeof(devtype)),
  1995. dev_name, supp_config_methods, dev_capab, group_capab,
  1996. group ? " group=" : "",
  1997. group ? group->ifname : "");
  1998. params[sizeof(params) - 1] = '\0';
  1999. if (config_methods & WPS_CONFIG_DISPLAY) {
  2000. generated_pin = wps_generate_pin();
  2001. wpas_prov_disc_local_display(wpa_s, peer, params,
  2002. generated_pin);
  2003. } else if (config_methods & WPS_CONFIG_KEYPAD)
  2004. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2005. else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2006. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
  2007. MACSTR "%s", MAC2STR(peer), params);
  2008. wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
  2009. P2P_PROV_DISC_SUCCESS,
  2010. config_methods, generated_pin);
  2011. }
  2012. void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
  2013. {
  2014. struct wpa_supplicant *wpa_s = ctx;
  2015. unsigned int generated_pin = 0;
  2016. char params[20];
  2017. if (wpa_s->pending_pd_before_join &&
  2018. (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
  2019. os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
  2020. wpa_s->pending_pd_before_join = 0;
  2021. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2022. "join-existing-group operation");
  2023. wpas_p2p_join_start(wpa_s);
  2024. return;
  2025. }
  2026. if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
  2027. wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
  2028. os_snprintf(params, sizeof(params), " peer_go=%d",
  2029. wpa_s->pending_pd_use == AUTO_PD_JOIN);
  2030. else
  2031. params[0] = '\0';
  2032. if (config_methods & WPS_CONFIG_DISPLAY)
  2033. wpas_prov_disc_local_keypad(wpa_s, peer, params);
  2034. else if (config_methods & WPS_CONFIG_KEYPAD) {
  2035. generated_pin = wps_generate_pin();
  2036. wpas_prov_disc_local_display(wpa_s, peer, params,
  2037. generated_pin);
  2038. } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
  2039. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
  2040. MACSTR "%s", MAC2STR(peer), params);
  2041. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2042. P2P_PROV_DISC_SUCCESS,
  2043. config_methods, generated_pin);
  2044. }
  2045. static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
  2046. enum p2p_prov_disc_status status)
  2047. {
  2048. struct wpa_supplicant *wpa_s = ctx;
  2049. if (wpa_s->p2p_fallback_to_go_neg) {
  2050. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
  2051. "failed - fall back to GO Negotiation");
  2052. wpas_p2p_fallback_to_go_neg(wpa_s, 0);
  2053. return;
  2054. }
  2055. if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
  2056. wpa_s->pending_pd_before_join = 0;
  2057. wpa_printf(MSG_DEBUG, "P2P: Starting pending "
  2058. "join-existing-group operation (no ACK for PD "
  2059. "Req attempts)");
  2060. wpas_p2p_join_start(wpa_s);
  2061. return;
  2062. }
  2063. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
  2064. " p2p_dev_addr=" MACSTR " status=%d",
  2065. MAC2STR(peer), status);
  2066. wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
  2067. status, 0, 0);
  2068. }
  2069. static int freq_included(const struct p2p_channels *channels, unsigned int freq)
  2070. {
  2071. if (channels == NULL)
  2072. return 1; /* Assume no restrictions */
  2073. return p2p_channels_includes_freq(channels, freq);
  2074. }
  2075. static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
  2076. const u8 *go_dev_addr, const u8 *ssid,
  2077. size_t ssid_len, int *go, u8 *group_bssid,
  2078. int *force_freq, int persistent_group,
  2079. const struct p2p_channels *channels)
  2080. {
  2081. struct wpa_supplicant *wpa_s = ctx;
  2082. struct wpa_ssid *s;
  2083. int res;
  2084. struct wpa_supplicant *grp;
  2085. if (!persistent_group) {
  2086. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2087. " to join an active group", MAC2STR(sa));
  2088. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2089. (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
  2090. == 0 ||
  2091. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
  2092. wpa_printf(MSG_DEBUG, "P2P: Accept previously "
  2093. "authorized invitation");
  2094. goto accept_inv;
  2095. }
  2096. /*
  2097. * Do not accept the invitation automatically; notify user and
  2098. * request approval.
  2099. */
  2100. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2101. }
  2102. grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
  2103. if (grp) {
  2104. wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
  2105. "running persistent group");
  2106. if (*go)
  2107. os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
  2108. goto accept_inv;
  2109. }
  2110. if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
  2111. os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
  2112. wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
  2113. "invitation to re-invoke a persistent group");
  2114. } else if (!wpa_s->conf->persistent_reconnect)
  2115. return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  2116. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2117. if (s->disabled == 2 &&
  2118. os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
  2119. s->ssid_len == ssid_len &&
  2120. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2121. break;
  2122. }
  2123. if (!s) {
  2124. wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
  2125. " requested reinvocation of an unknown group",
  2126. MAC2STR(sa));
  2127. return P2P_SC_FAIL_UNKNOWN_GROUP;
  2128. }
  2129. if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
  2130. *go = 1;
  2131. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2132. wpa_printf(MSG_DEBUG, "P2P: The only available "
  2133. "interface is already in use - reject "
  2134. "invitation");
  2135. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2136. }
  2137. os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
  2138. } else if (s->mode == WPAS_MODE_P2P_GO) {
  2139. *go = 1;
  2140. if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
  2141. {
  2142. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  2143. "interface address for the group");
  2144. return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  2145. }
  2146. os_memcpy(group_bssid, wpa_s->pending_interface_addr,
  2147. ETH_ALEN);
  2148. }
  2149. accept_inv:
  2150. wpas_p2p_set_own_freq_preference(wpa_s, 0);
  2151. /* Get one of the frequencies currently in use */
  2152. if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
  2153. wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
  2154. *force_freq = res;
  2155. wpas_p2p_set_own_freq_preference(wpa_s, res);
  2156. }
  2157. if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
  2158. wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2159. if (*go == 0) {
  2160. /* We are the client */
  2161. wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
  2162. "running a GO but we are capable of MCC, "
  2163. "figure out the best channel to use");
  2164. *force_freq = 0;
  2165. } else if (!freq_included(channels, *force_freq)) {
  2166. /* We are the GO, and *force_freq is not in the
  2167. * intersection */
  2168. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  2169. "in intersection but we are capable of MCC, "
  2170. "figure out the best channel to use",
  2171. *force_freq);
  2172. *force_freq = 0;
  2173. }
  2174. }
  2175. return P2P_SC_SUCCESS;
  2176. }
  2177. static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
  2178. const u8 *ssid, size_t ssid_len,
  2179. const u8 *go_dev_addr, u8 status,
  2180. int op_freq)
  2181. {
  2182. struct wpa_supplicant *wpa_s = ctx;
  2183. struct wpa_ssid *s;
  2184. for (s = wpa_s->conf->ssid; s; s = s->next) {
  2185. if (s->disabled == 2 &&
  2186. s->ssid_len == ssid_len &&
  2187. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  2188. break;
  2189. }
  2190. if (status == P2P_SC_SUCCESS) {
  2191. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2192. " was accepted; op_freq=%d MHz",
  2193. MAC2STR(sa), op_freq);
  2194. if (s) {
  2195. int go = s->mode == WPAS_MODE_P2P_GO;
  2196. wpas_p2p_group_add_persistent(
  2197. wpa_s, s, go, go ? op_freq : 0, 0, NULL);
  2198. } else if (bssid) {
  2199. wpa_s->user_initiated_pd = 0;
  2200. wpas_p2p_join(wpa_s, bssid, go_dev_addr,
  2201. wpa_s->p2p_wps_method, 0);
  2202. }
  2203. return;
  2204. }
  2205. if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2206. wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
  2207. " was rejected (status %u)", MAC2STR(sa), status);
  2208. return;
  2209. }
  2210. if (!s) {
  2211. if (bssid) {
  2212. wpa_msg_global(wpa_s, MSG_INFO,
  2213. P2P_EVENT_INVITATION_RECEIVED
  2214. "sa=" MACSTR " go_dev_addr=" MACSTR
  2215. " bssid=" MACSTR " unknown-network",
  2216. MAC2STR(sa), MAC2STR(go_dev_addr),
  2217. MAC2STR(bssid));
  2218. } else {
  2219. wpa_msg_global(wpa_s, MSG_INFO,
  2220. P2P_EVENT_INVITATION_RECEIVED
  2221. "sa=" MACSTR " go_dev_addr=" MACSTR
  2222. " unknown-network",
  2223. MAC2STR(sa), MAC2STR(go_dev_addr));
  2224. }
  2225. return;
  2226. }
  2227. if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
  2228. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2229. "sa=" MACSTR " persistent=%d freq=%d",
  2230. MAC2STR(sa), s->id, op_freq);
  2231. } else {
  2232. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
  2233. "sa=" MACSTR " persistent=%d",
  2234. MAC2STR(sa), s->id);
  2235. }
  2236. }
  2237. static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
  2238. struct wpa_ssid *ssid,
  2239. const u8 *peer)
  2240. {
  2241. size_t i;
  2242. if (ssid == NULL)
  2243. return;
  2244. for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
  2245. if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
  2246. ETH_ALEN) == 0)
  2247. break;
  2248. }
  2249. if (i >= ssid->num_p2p_clients) {
  2250. if (ssid->mode != WPAS_MODE_P2P_GO &&
  2251. os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
  2252. wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
  2253. "due to invitation result", ssid->id);
  2254. wpas_notify_network_removed(wpa_s, ssid);
  2255. wpa_config_remove_network(wpa_s->conf, ssid->id);
  2256. return;
  2257. }
  2258. return; /* Peer not found in client list */
  2259. }
  2260. wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
  2261. "group %d client list due to invitation result",
  2262. MAC2STR(peer), ssid->id);
  2263. os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
  2264. ssid->p2p_client_list + (i + 1) * ETH_ALEN,
  2265. (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
  2266. ssid->num_p2p_clients--;
  2267. #ifndef CONFIG_NO_CONFIG_WRITE
  2268. if (wpa_s->parent->conf->update_config &&
  2269. wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
  2270. wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
  2271. #endif /* CONFIG_NO_CONFIG_WRITE */
  2272. }
  2273. static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
  2274. const u8 *peer)
  2275. {
  2276. struct wpa_ssid *ssid;
  2277. wpa_s = wpa_s->global->p2p_invite_group;
  2278. if (wpa_s == NULL)
  2279. return; /* No known invitation group */
  2280. ssid = wpa_s->current_ssid;
  2281. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2282. !ssid->p2p_persistent_group)
  2283. return; /* Not operating as a GO in persistent group */
  2284. ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
  2285. ssid->ssid, ssid->ssid_len);
  2286. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2287. }
  2288. static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
  2289. const struct p2p_channels *channels,
  2290. const u8 *peer)
  2291. {
  2292. struct wpa_supplicant *wpa_s = ctx;
  2293. struct wpa_ssid *ssid;
  2294. if (bssid) {
  2295. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2296. "status=%d " MACSTR,
  2297. status, MAC2STR(bssid));
  2298. } else {
  2299. wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
  2300. "status=%d ", status);
  2301. }
  2302. wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
  2303. wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
  2304. status, MAC2STR(peer));
  2305. if (wpa_s->pending_invite_ssid_id == -1) {
  2306. if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
  2307. wpas_remove_persistent_client(wpa_s, peer);
  2308. return; /* Invitation to active group */
  2309. }
  2310. if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  2311. wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
  2312. "invitation exchange to indicate readiness for "
  2313. "re-invocation");
  2314. }
  2315. if (status != P2P_SC_SUCCESS) {
  2316. if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
  2317. ssid = wpa_config_get_network(
  2318. wpa_s->conf, wpa_s->pending_invite_ssid_id);
  2319. wpas_remove_persistent_peer(wpa_s, ssid, peer);
  2320. }
  2321. wpas_p2p_remove_pending_group_interface(wpa_s);
  2322. return;
  2323. }
  2324. ssid = wpa_config_get_network(wpa_s->conf,
  2325. wpa_s->pending_invite_ssid_id);
  2326. if (ssid == NULL) {
  2327. wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
  2328. "data matching with invitation");
  2329. return;
  2330. }
  2331. /*
  2332. * The peer could have missed our ctrl::ack frame for Invitation
  2333. * Response and continue retransmitting the frame. To reduce the
  2334. * likelihood of the peer not getting successful TX status for the
  2335. * Invitation Response frame, wait a short time here before starting
  2336. * the persistent group so that we will remain on the current channel to
  2337. * acknowledge any possible retransmission from the peer.
  2338. */
  2339. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
  2340. "starting persistent group");
  2341. os_sleep(0, 50000);
  2342. wpas_p2p_group_add_persistent(wpa_s, ssid,
  2343. ssid->mode == WPAS_MODE_P2P_GO,
  2344. wpa_s->p2p_persistent_go_freq,
  2345. wpa_s->p2p_go_ht40, channels);
  2346. }
  2347. static int wpas_p2p_disallowed_freq(struct wpa_global *global,
  2348. unsigned int freq)
  2349. {
  2350. unsigned int i;
  2351. if (global->p2p_disallow_freq == NULL)
  2352. return 0;
  2353. for (i = 0; i < global->num_p2p_disallow_freq; i++) {
  2354. if (freq >= global->p2p_disallow_freq[i].min &&
  2355. freq <= global->p2p_disallow_freq[i].max)
  2356. return 1;
  2357. }
  2358. return 0;
  2359. }
  2360. static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
  2361. {
  2362. reg->channel[reg->channels] = chan;
  2363. reg->channels++;
  2364. }
  2365. static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
  2366. struct p2p_channels *chan)
  2367. {
  2368. int i, cla = 0;
  2369. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
  2370. "band");
  2371. /* Operating class 81 - 2.4 GHz band channels 1..13 */
  2372. chan->reg_class[cla].reg_class = 81;
  2373. chan->reg_class[cla].channels = 0;
  2374. for (i = 0; i < 11; i++) {
  2375. if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
  2376. wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
  2377. }
  2378. if (chan->reg_class[cla].channels)
  2379. cla++;
  2380. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
  2381. "band");
  2382. /* Operating class 115 - 5 GHz, channels 36-48 */
  2383. chan->reg_class[cla].reg_class = 115;
  2384. chan->reg_class[cla].channels = 0;
  2385. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
  2386. wpas_p2p_add_chan(&chan->reg_class[cla], 36);
  2387. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
  2388. wpas_p2p_add_chan(&chan->reg_class[cla], 40);
  2389. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
  2390. wpas_p2p_add_chan(&chan->reg_class[cla], 44);
  2391. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
  2392. wpas_p2p_add_chan(&chan->reg_class[cla], 48);
  2393. if (chan->reg_class[cla].channels)
  2394. cla++;
  2395. wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
  2396. "band");
  2397. /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
  2398. chan->reg_class[cla].reg_class = 124;
  2399. chan->reg_class[cla].channels = 0;
  2400. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
  2401. wpas_p2p_add_chan(&chan->reg_class[cla], 149);
  2402. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
  2403. wpas_p2p_add_chan(&chan->reg_class[cla], 153);
  2404. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
  2405. wpas_p2p_add_chan(&chan->reg_class[cla], 157);
  2406. if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
  2407. wpas_p2p_add_chan(&chan->reg_class[cla], 161);
  2408. if (chan->reg_class[cla].channels)
  2409. cla++;
  2410. chan->reg_classes = cla;
  2411. return 0;
  2412. }
  2413. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  2414. u16 num_modes,
  2415. enum hostapd_hw_mode mode)
  2416. {
  2417. u16 i;
  2418. for (i = 0; i < num_modes; i++) {
  2419. if (modes[i].mode == mode)
  2420. return &modes[i];
  2421. }
  2422. return NULL;
  2423. }
  2424. static int has_channel(struct wpa_global *global,
  2425. struct hostapd_hw_modes *mode, u8 chan, int *flags)
  2426. {
  2427. int i;
  2428. unsigned int freq;
  2429. freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
  2430. chan * 5;
  2431. if (wpas_p2p_disallowed_freq(global, freq))
  2432. return 0;
  2433. for (i = 0; i < mode->num_channels; i++) {
  2434. if (mode->channels[i].chan == chan) {
  2435. if (flags)
  2436. *flags = mode->channels[i].flag;
  2437. return !(mode->channels[i].flag &
  2438. (HOSTAPD_CHAN_DISABLED |
  2439. HOSTAPD_CHAN_PASSIVE_SCAN |
  2440. HOSTAPD_CHAN_NO_IBSS |
  2441. HOSTAPD_CHAN_RADAR));
  2442. }
  2443. }
  2444. return 0;
  2445. }
  2446. struct p2p_oper_class_map {
  2447. enum hostapd_hw_mode mode;
  2448. u8 op_class;
  2449. u8 min_chan;
  2450. u8 max_chan;
  2451. u8 inc;
  2452. enum { BW20, BW40PLUS, BW40MINUS } bw;
  2453. };
  2454. static struct p2p_oper_class_map op_class[] = {
  2455. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
  2456. #if 0 /* Do not enable HT40 on 2 GHz for now */
  2457. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
  2458. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
  2459. #endif
  2460. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
  2461. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
  2462. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
  2463. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
  2464. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
  2465. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
  2466. { -1, 0, 0, 0, 0, BW20 }
  2467. };
  2468. static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
  2469. struct hostapd_hw_modes *mode,
  2470. u8 channel, u8 bw)
  2471. {
  2472. int flag;
  2473. if (!has_channel(wpa_s->global, mode, channel, &flag))
  2474. return -1;
  2475. if (bw == BW40MINUS &&
  2476. (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
  2477. !has_channel(wpa_s->global, mode, channel - 4, NULL)))
  2478. return 0;
  2479. if (bw == BW40PLUS &&
  2480. (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
  2481. !has_channel(wpa_s->global, mode, channel + 4, NULL)))
  2482. return 0;
  2483. return 1;
  2484. }
  2485. static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
  2486. struct p2p_channels *chan)
  2487. {
  2488. struct hostapd_hw_modes *mode;
  2489. int cla, op;
  2490. if (wpa_s->hw.modes == NULL) {
  2491. wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
  2492. "of all supported channels; assume dualband "
  2493. "support");
  2494. return wpas_p2p_default_channels(wpa_s, chan);
  2495. }
  2496. cla = 0;
  2497. for (op = 0; op_class[op].op_class; op++) {
  2498. struct p2p_oper_class_map *o = &op_class[op];
  2499. u8 ch;
  2500. struct p2p_reg_class *reg = NULL;
  2501. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
  2502. if (mode == NULL)
  2503. continue;
  2504. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2505. if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
  2506. continue;
  2507. if (reg == NULL) {
  2508. wpa_printf(MSG_DEBUG, "P2P: Add operating "
  2509. "class %u", o->op_class);
  2510. reg = &chan->reg_class[cla];
  2511. cla++;
  2512. reg->reg_class = o->op_class;
  2513. }
  2514. reg->channel[reg->channels] = ch;
  2515. reg->channels++;
  2516. }
  2517. if (reg) {
  2518. wpa_hexdump(MSG_DEBUG, "P2P: Channels",
  2519. reg->channel, reg->channels);
  2520. }
  2521. }
  2522. chan->reg_classes = cla;
  2523. return 0;
  2524. }
  2525. int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
  2526. struct hostapd_hw_modes *mode, u8 channel)
  2527. {
  2528. int op, ret;
  2529. for (op = 0; op_class[op].op_class; op++) {
  2530. struct p2p_oper_class_map *o = &op_class[op];
  2531. u8 ch;
  2532. for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
  2533. if (o->mode != HOSTAPD_MODE_IEEE80211A ||
  2534. o->bw == BW20 || ch != channel)
  2535. continue;
  2536. ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
  2537. if (ret < 0)
  2538. continue;
  2539. else if (ret > 0)
  2540. return (o->bw == BW40MINUS) ? -1 : 1;
  2541. else
  2542. return 0;
  2543. }
  2544. }
  2545. return 0;
  2546. }
  2547. static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
  2548. size_t buf_len)
  2549. {
  2550. struct wpa_supplicant *wpa_s = ctx;
  2551. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2552. if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
  2553. break;
  2554. }
  2555. if (wpa_s == NULL)
  2556. return -1;
  2557. return wpa_drv_get_noa(wpa_s, buf, buf_len);
  2558. }
  2559. static int wpas_go_connected(void *ctx, const u8 *dev_addr)
  2560. {
  2561. struct wpa_supplicant *wpa_s = ctx;
  2562. for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2563. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2564. if (ssid == NULL)
  2565. continue;
  2566. if (ssid->mode != WPAS_MODE_INFRA)
  2567. continue;
  2568. if (wpa_s->wpa_state != WPA_COMPLETED &&
  2569. wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
  2570. continue;
  2571. if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
  2572. return 1;
  2573. }
  2574. return 0;
  2575. }
  2576. static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
  2577. {
  2578. struct wpa_supplicant *wpa_s = ctx;
  2579. wpa_msg_global(wpa_s, level, "P2P: %s", msg);
  2580. }
  2581. int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
  2582. {
  2583. struct wpa_interface iface;
  2584. struct wpa_supplicant *p2pdev_wpa_s;
  2585. char ifname[100];
  2586. char force_name[100];
  2587. int ret;
  2588. os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
  2589. wpa_s->ifname);
  2590. force_name[0] = '\0';
  2591. wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
  2592. ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
  2593. force_name, wpa_s->pending_interface_addr, NULL);
  2594. if (ret < 0) {
  2595. wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
  2596. return ret;
  2597. }
  2598. os_strlcpy(wpa_s->pending_interface_name, ifname,
  2599. sizeof(wpa_s->pending_interface_name));
  2600. os_memset(&iface, 0, sizeof(iface));
  2601. iface.p2p_mgmt = 1;
  2602. iface.ifname = wpa_s->pending_interface_name;
  2603. iface.driver = wpa_s->driver->name;
  2604. iface.driver_param = wpa_s->conf->driver_param;
  2605. iface.confname = wpa_s->confname;
  2606. p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
  2607. if (!p2pdev_wpa_s) {
  2608. wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
  2609. return -1;
  2610. }
  2611. p2pdev_wpa_s->parent = wpa_s;
  2612. wpa_s->pending_interface_name[0] = '\0';
  2613. return 0;
  2614. }
  2615. /**
  2616. * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
  2617. * @global: Pointer to global data from wpa_supplicant_init()
  2618. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2619. * Returns: 0 on success, -1 on failure
  2620. */
  2621. int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
  2622. {
  2623. struct p2p_config p2p;
  2624. unsigned int r;
  2625. int i;
  2626. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  2627. return 0;
  2628. if (global->p2p)
  2629. return 0;
  2630. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2631. struct p2p_params params;
  2632. wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
  2633. os_memset(&params, 0, sizeof(params));
  2634. params.dev_name = wpa_s->conf->device_name;
  2635. os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
  2636. WPS_DEV_TYPE_LEN);
  2637. params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2638. os_memcpy(params.sec_dev_type,
  2639. wpa_s->conf->sec_device_type,
  2640. params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2641. if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
  2642. return -1;
  2643. return 0;
  2644. }
  2645. os_memset(&p2p, 0, sizeof(p2p));
  2646. p2p.cb_ctx = wpa_s;
  2647. p2p.debug_print = wpas_p2p_debug_print;
  2648. p2p.p2p_scan = wpas_p2p_scan;
  2649. p2p.send_action = wpas_send_action;
  2650. p2p.send_action_done = wpas_send_action_done;
  2651. p2p.go_neg_completed = wpas_go_neg_completed;
  2652. p2p.go_neg_req_rx = wpas_go_neg_req_rx;
  2653. p2p.dev_found = wpas_dev_found;
  2654. p2p.dev_lost = wpas_dev_lost;
  2655. p2p.find_stopped = wpas_find_stopped;
  2656. p2p.start_listen = wpas_start_listen;
  2657. p2p.stop_listen = wpas_stop_listen;
  2658. p2p.send_probe_resp = wpas_send_probe_resp;
  2659. p2p.sd_request = wpas_sd_request;
  2660. p2p.sd_response = wpas_sd_response;
  2661. p2p.prov_disc_req = wpas_prov_disc_req;
  2662. p2p.prov_disc_resp = wpas_prov_disc_resp;
  2663. p2p.prov_disc_fail = wpas_prov_disc_fail;
  2664. p2p.invitation_process = wpas_invitation_process;
  2665. p2p.invitation_received = wpas_invitation_received;
  2666. p2p.invitation_result = wpas_invitation_result;
  2667. p2p.get_noa = wpas_get_noa;
  2668. p2p.go_connected = wpas_go_connected;
  2669. os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
  2670. os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
  2671. p2p.dev_name = wpa_s->conf->device_name;
  2672. p2p.manufacturer = wpa_s->conf->manufacturer;
  2673. p2p.model_name = wpa_s->conf->model_name;
  2674. p2p.model_number = wpa_s->conf->model_number;
  2675. p2p.serial_number = wpa_s->conf->serial_number;
  2676. if (wpa_s->wps) {
  2677. os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
  2678. p2p.config_methods = wpa_s->wps->config_methods;
  2679. }
  2680. if (wpa_s->conf->p2p_listen_reg_class &&
  2681. wpa_s->conf->p2p_listen_channel) {
  2682. p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
  2683. p2p.channel = wpa_s->conf->p2p_listen_channel;
  2684. } else {
  2685. p2p.reg_class = 81;
  2686. /*
  2687. * Pick one of the social channels randomly as the listen
  2688. * channel.
  2689. */
  2690. os_get_random((u8 *) &r, sizeof(r));
  2691. p2p.channel = 1 + (r % 3) * 5;
  2692. }
  2693. wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
  2694. if (wpa_s->conf->p2p_oper_reg_class &&
  2695. wpa_s->conf->p2p_oper_channel) {
  2696. p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  2697. p2p.op_channel = wpa_s->conf->p2p_oper_channel;
  2698. p2p.cfg_op_channel = 1;
  2699. wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
  2700. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2701. } else {
  2702. p2p.op_reg_class = 81;
  2703. /*
  2704. * Use random operation channel from (1, 6, 11) if no other
  2705. * preference is indicated.
  2706. */
  2707. os_get_random((u8 *) &r, sizeof(r));
  2708. p2p.op_channel = 1 + (r % 3) * 5;
  2709. p2p.cfg_op_channel = 0;
  2710. wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
  2711. "%d:%d", p2p.op_reg_class, p2p.op_channel);
  2712. }
  2713. if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
  2714. p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
  2715. p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
  2716. }
  2717. if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2718. os_memcpy(p2p.country, wpa_s->conf->country, 2);
  2719. p2p.country[2] = 0x04;
  2720. } else
  2721. os_memcpy(p2p.country, "XX\x04", 3);
  2722. if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
  2723. wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
  2724. "channel list");
  2725. return -1;
  2726. }
  2727. os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
  2728. WPS_DEV_TYPE_LEN);
  2729. p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  2730. os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
  2731. p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  2732. p2p.concurrent_operations = !!(wpa_s->drv_flags &
  2733. WPA_DRIVER_FLAGS_P2P_CONCURRENT);
  2734. p2p.max_peers = 100;
  2735. if (wpa_s->conf->p2p_ssid_postfix) {
  2736. p2p.ssid_postfix_len =
  2737. os_strlen(wpa_s->conf->p2p_ssid_postfix);
  2738. if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
  2739. p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
  2740. os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
  2741. p2p.ssid_postfix_len);
  2742. }
  2743. p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
  2744. p2p.max_listen = wpa_s->max_remain_on_chan;
  2745. global->p2p = p2p_init(&p2p);
  2746. if (global->p2p == NULL)
  2747. return -1;
  2748. global->p2p_init_wpa_s = wpa_s;
  2749. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  2750. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  2751. continue;
  2752. p2p_add_wps_vendor_extension(
  2753. global->p2p, wpa_s->conf->wps_vendor_ext[i]);
  2754. }
  2755. return 0;
  2756. }
  2757. /**
  2758. * wpas_p2p_deinit - Deinitialize per-interface P2P data
  2759. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  2760. *
  2761. * This function deinitialize per-interface P2P data.
  2762. */
  2763. void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
  2764. {
  2765. if (wpa_s->driver && wpa_s->drv_priv)
  2766. wpa_drv_probe_req_report(wpa_s, 0);
  2767. if (wpa_s->go_params) {
  2768. /* Clear any stored provisioning info */
  2769. p2p_clear_provisioning_info(
  2770. wpa_s->global->p2p,
  2771. wpa_s->go_params->peer_device_addr);
  2772. }
  2773. os_free(wpa_s->go_params);
  2774. wpa_s->go_params = NULL;
  2775. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  2776. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2777. wpa_s->p2p_long_listen = 0;
  2778. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  2779. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  2780. wpas_p2p_remove_pending_group_interface(wpa_s);
  2781. /* TODO: remove group interface from the driver if this wpa_s instance
  2782. * is on top of a P2P group interface */
  2783. }
  2784. /**
  2785. * wpas_p2p_deinit_global - Deinitialize global P2P module
  2786. * @global: Pointer to global data from wpa_supplicant_init()
  2787. *
  2788. * This function deinitializes the global (per device) P2P module.
  2789. */
  2790. void wpas_p2p_deinit_global(struct wpa_global *global)
  2791. {
  2792. struct wpa_supplicant *wpa_s, *tmp;
  2793. wpa_s = global->ifaces;
  2794. if (wpa_s)
  2795. wpas_p2p_service_flush(wpa_s);
  2796. if (global->p2p == NULL)
  2797. return;
  2798. /* Remove remaining P2P group interfaces */
  2799. while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  2800. wpa_s = wpa_s->next;
  2801. while (wpa_s) {
  2802. tmp = global->ifaces;
  2803. while (tmp &&
  2804. (tmp == wpa_s ||
  2805. tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
  2806. tmp = tmp->next;
  2807. }
  2808. if (tmp == NULL)
  2809. break;
  2810. /* Disconnect from the P2P group and deinit the interface */
  2811. wpas_p2p_disconnect(tmp);
  2812. }
  2813. /*
  2814. * Deinit GO data on any possibly remaining interface (if main
  2815. * interface is used as GO).
  2816. */
  2817. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2818. if (wpa_s->ap_iface)
  2819. wpas_p2p_group_deinit(wpa_s);
  2820. }
  2821. p2p_deinit(global->p2p);
  2822. global->p2p = NULL;
  2823. global->p2p_init_wpa_s = NULL;
  2824. }
  2825. static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
  2826. {
  2827. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  2828. wpa_s->conf->p2p_no_group_iface)
  2829. return 0; /* separate interface disabled per configuration */
  2830. if (wpa_s->drv_flags &
  2831. (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
  2832. WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
  2833. return 1; /* P2P group requires a new interface in every case
  2834. */
  2835. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
  2836. return 0; /* driver does not support concurrent operations */
  2837. if (wpa_s->global->ifaces->next)
  2838. return 1; /* more that one interface already in use */
  2839. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2840. return 1; /* this interface is already in use */
  2841. return 0;
  2842. }
  2843. static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
  2844. const u8 *peer_addr,
  2845. enum p2p_wps_method wps_method,
  2846. int go_intent, const u8 *own_interface_addr,
  2847. unsigned int force_freq, int persistent_group,
  2848. struct wpa_ssid *ssid, unsigned int pref_freq)
  2849. {
  2850. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2851. persistent_group = 2;
  2852. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  2853. return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
  2854. go_intent, own_interface_addr,
  2855. force_freq, persistent_group);
  2856. }
  2857. /*
  2858. * Increase GO config timeout if HT40 is used since it takes some time
  2859. * to scan channels for coex purposes before the BSS can be started.
  2860. */
  2861. p2p_set_config_timeout(wpa_s->global->p2p,
  2862. wpa_s->p2p_go_ht40 ? 255 : 100, 20);
  2863. return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
  2864. go_intent, own_interface_addr, force_freq,
  2865. persistent_group, ssid ? ssid->ssid : NULL,
  2866. ssid ? ssid->ssid_len : 0,
  2867. wpa_s->p2p_pd_before_go_neg, pref_freq);
  2868. }
  2869. static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
  2870. const u8 *peer_addr,
  2871. enum p2p_wps_method wps_method,
  2872. int go_intent, const u8 *own_interface_addr,
  2873. unsigned int force_freq, int persistent_group,
  2874. struct wpa_ssid *ssid, unsigned int pref_freq)
  2875. {
  2876. if (persistent_group && wpa_s->conf->persistent_reconnect)
  2877. persistent_group = 2;
  2878. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  2879. return -1;
  2880. return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
  2881. go_intent, own_interface_addr, force_freq,
  2882. persistent_group, ssid ? ssid->ssid : NULL,
  2883. ssid ? ssid->ssid_len : 0, pref_freq);
  2884. }
  2885. static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
  2886. {
  2887. wpa_s->p2p_join_scan_count++;
  2888. wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
  2889. wpa_s->p2p_join_scan_count);
  2890. if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
  2891. wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
  2892. " for join operationg - stop join attempt",
  2893. MAC2STR(wpa_s->pending_join_iface_addr));
  2894. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2895. if (wpa_s->p2p_auto_pd) {
  2896. wpa_s->p2p_auto_pd = 0;
  2897. wpa_msg_global(wpa_s, MSG_INFO,
  2898. P2P_EVENT_PROV_DISC_FAILURE
  2899. " p2p_dev_addr=" MACSTR " status=N/A",
  2900. MAC2STR(wpa_s->pending_join_dev_addr));
  2901. return;
  2902. }
  2903. wpa_msg_global(wpa_s->parent, MSG_INFO,
  2904. P2P_EVENT_GROUP_FORMATION_FAILURE);
  2905. }
  2906. }
  2907. static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
  2908. {
  2909. int *freqs, res, num, i;
  2910. if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
  2911. /* Multiple channels are supported and not all are in use */
  2912. return 0;
  2913. }
  2914. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  2915. if (!freqs)
  2916. return 1;
  2917. num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  2918. wpa_s->num_multichan_concurrent);
  2919. if (num < 0) {
  2920. res = 1;
  2921. goto exit_free;
  2922. }
  2923. for (i = 0; i < num; i++) {
  2924. if (freqs[i] == freq) {
  2925. wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
  2926. freq);
  2927. res = 0;
  2928. goto exit_free;
  2929. }
  2930. }
  2931. res = 1;
  2932. exit_free:
  2933. os_free(freqs);
  2934. return res;
  2935. }
  2936. static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
  2937. const u8 *peer_dev_addr)
  2938. {
  2939. struct wpa_bss *bss;
  2940. int updated;
  2941. bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
  2942. if (bss == NULL)
  2943. return -1;
  2944. if (bss->last_update_idx < wpa_s->bss_update_idx) {
  2945. wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
  2946. "last scan");
  2947. return 0;
  2948. }
  2949. updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
  2950. wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
  2951. "%ld.%06ld (%supdated in last scan)",
  2952. bss->last_update.sec, bss->last_update.usec,
  2953. updated ? "": "not ");
  2954. return updated;
  2955. }
  2956. static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
  2957. struct wpa_scan_results *scan_res)
  2958. {
  2959. struct wpa_bss *bss;
  2960. int freq;
  2961. u8 iface_addr[ETH_ALEN];
  2962. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  2963. if (wpa_s->global->p2p_disabled)
  2964. return;
  2965. wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
  2966. scan_res ? (int) scan_res->num : -1,
  2967. wpa_s->p2p_auto_join ? "auto_" : "");
  2968. if (scan_res)
  2969. wpas_p2p_scan_res_handler(wpa_s, scan_res);
  2970. if (wpa_s->p2p_auto_pd) {
  2971. int join = wpas_p2p_peer_go(wpa_s,
  2972. wpa_s->pending_join_dev_addr);
  2973. if (join == 0 &&
  2974. wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
  2975. wpa_s->auto_pd_scan_retry++;
  2976. bss = wpa_bss_get_bssid_latest(
  2977. wpa_s, wpa_s->pending_join_dev_addr);
  2978. if (bss) {
  2979. freq = bss->freq;
  2980. wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
  2981. "the peer " MACSTR " at %d MHz",
  2982. wpa_s->auto_pd_scan_retry,
  2983. MAC2STR(wpa_s->
  2984. pending_join_dev_addr),
  2985. freq);
  2986. wpas_p2p_join_scan_req(wpa_s, freq);
  2987. return;
  2988. }
  2989. }
  2990. if (join < 0)
  2991. join = 0;
  2992. wpa_s->p2p_auto_pd = 0;
  2993. wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
  2994. wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
  2995. MAC2STR(wpa_s->pending_join_dev_addr), join);
  2996. if (p2p_prov_disc_req(wpa_s->global->p2p,
  2997. wpa_s->pending_join_dev_addr,
  2998. wpa_s->pending_pd_config_methods, join,
  2999. 0, wpa_s->user_initiated_pd) < 0) {
  3000. wpa_s->p2p_auto_pd = 0;
  3001. wpa_msg_global(wpa_s, MSG_INFO,
  3002. P2P_EVENT_PROV_DISC_FAILURE
  3003. " p2p_dev_addr=" MACSTR " status=N/A",
  3004. MAC2STR(wpa_s->pending_join_dev_addr));
  3005. }
  3006. return;
  3007. }
  3008. if (wpa_s->p2p_auto_join) {
  3009. int join = wpas_p2p_peer_go(wpa_s,
  3010. wpa_s->pending_join_dev_addr);
  3011. if (join < 0) {
  3012. wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
  3013. "running a GO -> use GO Negotiation");
  3014. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
  3015. wpa_s->p2p_pin, wpa_s->p2p_wps_method,
  3016. wpa_s->p2p_persistent_group, 0, 0, 0,
  3017. wpa_s->p2p_go_intent,
  3018. wpa_s->p2p_connect_freq,
  3019. wpa_s->p2p_persistent_id,
  3020. wpa_s->p2p_pd_before_go_neg,
  3021. wpa_s->p2p_go_ht40);
  3022. return;
  3023. }
  3024. wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
  3025. "try to join the group", join ? "" :
  3026. " in older scan");
  3027. if (!join)
  3028. wpa_s->p2p_fallback_to_go_neg = 1;
  3029. }
  3030. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3031. wpa_s->pending_join_iface_addr);
  3032. if (freq < 0 &&
  3033. p2p_get_interface_addr(wpa_s->global->p2p,
  3034. wpa_s->pending_join_dev_addr,
  3035. iface_addr) == 0 &&
  3036. os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
  3037. {
  3038. wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
  3039. "address for join from " MACSTR " to " MACSTR
  3040. " based on newly discovered P2P peer entry",
  3041. MAC2STR(wpa_s->pending_join_iface_addr),
  3042. MAC2STR(iface_addr));
  3043. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
  3044. ETH_ALEN);
  3045. freq = p2p_get_oper_freq(wpa_s->global->p2p,
  3046. wpa_s->pending_join_iface_addr);
  3047. }
  3048. if (freq >= 0) {
  3049. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3050. "from P2P peer table: %d MHz", freq);
  3051. }
  3052. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3053. if (bss) {
  3054. freq = bss->freq;
  3055. wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
  3056. "from BSS table: %d MHz", freq);
  3057. }
  3058. if (freq > 0) {
  3059. u16 method;
  3060. if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
  3061. wpa_msg_global(wpa_s->parent, MSG_INFO,
  3062. P2P_EVENT_GROUP_FORMATION_FAILURE
  3063. "reason=FREQ_CONFLICT");
  3064. return;
  3065. }
  3066. wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
  3067. "prior to joining an existing group (GO " MACSTR
  3068. " freq=%u MHz)",
  3069. MAC2STR(wpa_s->pending_join_dev_addr), freq);
  3070. wpa_s->pending_pd_before_join = 1;
  3071. switch (wpa_s->pending_join_wps_method) {
  3072. case WPS_PIN_DISPLAY:
  3073. method = WPS_CONFIG_KEYPAD;
  3074. break;
  3075. case WPS_PIN_KEYPAD:
  3076. method = WPS_CONFIG_DISPLAY;
  3077. break;
  3078. case WPS_PBC:
  3079. method = WPS_CONFIG_PUSHBUTTON;
  3080. break;
  3081. default:
  3082. method = 0;
  3083. break;
  3084. }
  3085. if ((p2p_get_provisioning_info(wpa_s->global->p2p,
  3086. wpa_s->pending_join_dev_addr) ==
  3087. method)) {
  3088. /*
  3089. * We have already performed provision discovery for
  3090. * joining the group. Proceed directly to join
  3091. * operation without duplicated provision discovery. */
  3092. wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
  3093. "with " MACSTR " already done - proceed to "
  3094. "join",
  3095. MAC2STR(wpa_s->pending_join_dev_addr));
  3096. wpa_s->pending_pd_before_join = 0;
  3097. goto start;
  3098. }
  3099. if (p2p_prov_disc_req(wpa_s->global->p2p,
  3100. wpa_s->pending_join_dev_addr, method, 1,
  3101. freq, wpa_s->user_initiated_pd) < 0) {
  3102. wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
  3103. "Discovery Request before joining an "
  3104. "existing group");
  3105. wpa_s->pending_pd_before_join = 0;
  3106. goto start;
  3107. }
  3108. return;
  3109. }
  3110. wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
  3111. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3112. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3113. wpas_p2p_check_join_scan_limit(wpa_s);
  3114. return;
  3115. start:
  3116. /* Start join operation immediately */
  3117. wpas_p2p_join_start(wpa_s);
  3118. }
  3119. static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
  3120. {
  3121. int ret;
  3122. struct wpa_driver_scan_params params;
  3123. struct wpabuf *wps_ie, *ies;
  3124. size_t ielen;
  3125. int freqs[2] = { 0, 0 };
  3126. os_memset(&params, 0, sizeof(params));
  3127. /* P2P Wildcard SSID */
  3128. params.num_ssids = 1;
  3129. params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
  3130. params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
  3131. wpa_s->wps->dev.p2p = 1;
  3132. wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
  3133. wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
  3134. NULL);
  3135. if (wps_ie == NULL) {
  3136. wpas_p2p_scan_res_join(wpa_s, NULL);
  3137. return;
  3138. }
  3139. ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  3140. ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
  3141. if (ies == NULL) {
  3142. wpabuf_free(wps_ie);
  3143. wpas_p2p_scan_res_join(wpa_s, NULL);
  3144. return;
  3145. }
  3146. wpabuf_put_buf(ies, wps_ie);
  3147. wpabuf_free(wps_ie);
  3148. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  3149. params.p2p_probe = 1;
  3150. params.extra_ies = wpabuf_head(ies);
  3151. params.extra_ies_len = wpabuf_len(ies);
  3152. if (freq > 0) {
  3153. freqs[0] = freq;
  3154. params.freqs = freqs;
  3155. }
  3156. /*
  3157. * Run a scan to update BSS table and start Provision Discovery once
  3158. * the new scan results become available.
  3159. */
  3160. ret = wpa_drv_scan(wpa_s, &params);
  3161. if (!ret) {
  3162. os_get_time(&wpa_s->scan_trigger_time);
  3163. wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
  3164. }
  3165. wpabuf_free(ies);
  3166. if (ret) {
  3167. wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
  3168. "try again later");
  3169. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  3170. eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
  3171. wpas_p2p_check_join_scan_limit(wpa_s);
  3172. }
  3173. }
  3174. static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
  3175. {
  3176. struct wpa_supplicant *wpa_s = eloop_ctx;
  3177. wpas_p2p_join_scan_req(wpa_s, 0);
  3178. }
  3179. static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
  3180. const u8 *dev_addr, enum p2p_wps_method wps_method,
  3181. int auto_join)
  3182. {
  3183. wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
  3184. MACSTR " dev " MACSTR ")%s",
  3185. MAC2STR(iface_addr), MAC2STR(dev_addr),
  3186. auto_join ? " (auto_join)" : "");
  3187. wpa_s->p2p_auto_pd = 0;
  3188. wpa_s->p2p_auto_join = !!auto_join;
  3189. os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
  3190. os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
  3191. wpa_s->pending_join_wps_method = wps_method;
  3192. /* Make sure we are not running find during connection establishment */
  3193. wpas_p2p_stop_find(wpa_s);
  3194. wpa_s->p2p_join_scan_count = 0;
  3195. wpas_p2p_join_scan(wpa_s, NULL);
  3196. return 0;
  3197. }
  3198. static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
  3199. {
  3200. struct wpa_supplicant *group;
  3201. struct p2p_go_neg_results res;
  3202. struct wpa_bss *bss;
  3203. group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
  3204. if (group == NULL)
  3205. return -1;
  3206. if (group != wpa_s) {
  3207. os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
  3208. sizeof(group->p2p_pin));
  3209. group->p2p_wps_method = wpa_s->p2p_wps_method;
  3210. } else {
  3211. /*
  3212. * Need to mark the current interface for p2p_group_formation
  3213. * when a separate group interface is not used. This is needed
  3214. * to allow p2p_cancel stop a pending p2p_connect-join.
  3215. * wpas_p2p_init_group_interface() addresses this for the case
  3216. * where a separate group interface is used.
  3217. */
  3218. wpa_s->global->p2p_group_formation = wpa_s;
  3219. }
  3220. group->p2p_in_provisioning = 1;
  3221. group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
  3222. os_memset(&res, 0, sizeof(res));
  3223. os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
  3224. ETH_ALEN);
  3225. res.wps_method = wpa_s->pending_join_wps_method;
  3226. bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
  3227. if (bss) {
  3228. res.freq = bss->freq;
  3229. res.ssid_len = bss->ssid_len;
  3230. os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
  3231. }
  3232. if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
  3233. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
  3234. "starting client");
  3235. wpa_drv_cancel_remain_on_channel(wpa_s);
  3236. wpa_s->off_channel_freq = 0;
  3237. wpa_s->roc_waiting_drv_freq = 0;
  3238. }
  3239. wpas_start_wps_enrollee(group, &res);
  3240. /*
  3241. * Allow a longer timeout for join-a-running-group than normal 15
  3242. * second group formation timeout since the GO may not have authorized
  3243. * our connection yet.
  3244. */
  3245. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
  3246. eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
  3247. wpa_s, NULL);
  3248. return 0;
  3249. }
  3250. static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
  3251. int *force_freq, int *pref_freq)
  3252. {
  3253. int *freqs, res;
  3254. unsigned int freq_in_use = 0, num, i;
  3255. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3256. if (!freqs)
  3257. return -1;
  3258. num = get_shared_radio_freqs(wpa_s, freqs,
  3259. wpa_s->num_multichan_concurrent);
  3260. wpa_printf(MSG_DEBUG,
  3261. "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
  3262. freq, wpa_s->num_multichan_concurrent, num);
  3263. if (freq > 0) {
  3264. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3265. wpa_printf(MSG_DEBUG, "P2P: The forced channel "
  3266. "(%u MHz) is not supported for P2P uses",
  3267. freq);
  3268. res = -3;
  3269. goto exit_free;
  3270. }
  3271. for (i = 0; i < num; i++) {
  3272. if (freqs[i] == freq)
  3273. freq_in_use = 1;
  3274. }
  3275. if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
  3276. wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
  3277. freq);
  3278. res = -2;
  3279. goto exit_free;
  3280. }
  3281. wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
  3282. "requested channel (%u MHz)", freq);
  3283. *force_freq = freq;
  3284. goto exit_ok;
  3285. }
  3286. for (i = 0; i < num; i++) {
  3287. if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
  3288. continue;
  3289. wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
  3290. *force_freq);
  3291. *force_freq = freqs[i];
  3292. if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
  3293. wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
  3294. *pref_freq = *force_freq;
  3295. }
  3296. break;
  3297. }
  3298. if (i == num) {
  3299. if (num < wpa_s->num_multichan_concurrent) {
  3300. wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
  3301. *force_freq = 0;
  3302. } else {
  3303. wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
  3304. res = -2;
  3305. goto exit_free;
  3306. }
  3307. }
  3308. exit_ok:
  3309. res = 0;
  3310. exit_free:
  3311. os_free(freqs);
  3312. return res;
  3313. }
  3314. /**
  3315. * wpas_p2p_connect - Request P2P Group Formation to be started
  3316. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3317. * @peer_addr: Address of the peer P2P Device
  3318. * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
  3319. * @persistent_group: Whether to create a persistent group
  3320. * @auto_join: Whether to select join vs. GO Negotiation automatically
  3321. * @join: Whether to join an existing group (as a client) instead of starting
  3322. * Group Owner negotiation; @peer_addr is BSSID in that case
  3323. * @auth: Whether to only authorize the connection instead of doing that and
  3324. * initiating Group Owner negotiation
  3325. * @go_intent: GO Intent or -1 to use default
  3326. * @freq: Frequency for the group or 0 for auto-selection
  3327. * @persistent_id: Persistent group credentials to use for forcing GO
  3328. * parameters or -1 to generate new values (SSID/passphrase)
  3329. * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
  3330. * interoperability workaround when initiating group formation
  3331. * @ht40: Start GO with 40 MHz channel width
  3332. * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
  3333. * failure, -2 on failure due to channel not currently available,
  3334. * -3 if forced channel is not supported
  3335. */
  3336. int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3337. const char *pin, enum p2p_wps_method wps_method,
  3338. int persistent_group, int auto_join, int join, int auth,
  3339. int go_intent, int freq, int persistent_id, int pd,
  3340. int ht40)
  3341. {
  3342. int force_freq = 0, pref_freq = 0;
  3343. int ret = 0, res;
  3344. enum wpa_driver_if_type iftype;
  3345. const u8 *if_addr;
  3346. struct wpa_ssid *ssid = NULL;
  3347. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3348. return -1;
  3349. if (persistent_id >= 0) {
  3350. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3351. if (ssid == NULL || ssid->disabled != 2 ||
  3352. ssid->mode != WPAS_MODE_P2P_GO)
  3353. return -1;
  3354. }
  3355. if (go_intent < 0)
  3356. go_intent = wpa_s->conf->p2p_go_intent;
  3357. if (!auth)
  3358. wpa_s->p2p_long_listen = 0;
  3359. wpa_s->p2p_wps_method = wps_method;
  3360. wpa_s->p2p_persistent_group = !!persistent_group;
  3361. wpa_s->p2p_persistent_id = persistent_id;
  3362. wpa_s->p2p_go_intent = go_intent;
  3363. wpa_s->p2p_connect_freq = freq;
  3364. wpa_s->p2p_fallback_to_go_neg = 0;
  3365. wpa_s->p2p_pd_before_go_neg = !!pd;
  3366. wpa_s->p2p_go_ht40 = !!ht40;
  3367. if (pin)
  3368. os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
  3369. else if (wps_method == WPS_PIN_DISPLAY) {
  3370. ret = wps_generate_pin();
  3371. os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
  3372. ret);
  3373. wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
  3374. wpa_s->p2p_pin);
  3375. } else
  3376. wpa_s->p2p_pin[0] = '\0';
  3377. if (join || auto_join) {
  3378. u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
  3379. if (auth) {
  3380. wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
  3381. "connect a running group from " MACSTR,
  3382. MAC2STR(peer_addr));
  3383. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  3384. return ret;
  3385. }
  3386. os_memcpy(dev_addr, peer_addr, ETH_ALEN);
  3387. if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
  3388. iface_addr) < 0) {
  3389. os_memcpy(iface_addr, peer_addr, ETH_ALEN);
  3390. p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
  3391. dev_addr);
  3392. }
  3393. if (auto_join) {
  3394. os_get_time(&wpa_s->p2p_auto_started);
  3395. wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
  3396. "%ld.%06ld",
  3397. wpa_s->p2p_auto_started.sec,
  3398. wpa_s->p2p_auto_started.usec);
  3399. }
  3400. wpa_s->user_initiated_pd = 1;
  3401. if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
  3402. auto_join) < 0)
  3403. return -1;
  3404. return ret;
  3405. }
  3406. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  3407. if (res)
  3408. return res;
  3409. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  3410. wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
  3411. if (wpa_s->create_p2p_iface) {
  3412. /* Prepare to add a new interface for the group */
  3413. iftype = WPA_IF_P2P_GROUP;
  3414. if (go_intent == 15)
  3415. iftype = WPA_IF_P2P_GO;
  3416. if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
  3417. wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
  3418. "interface for the group");
  3419. return -1;
  3420. }
  3421. if_addr = wpa_s->pending_interface_addr;
  3422. } else
  3423. if_addr = wpa_s->own_addr;
  3424. if (auth) {
  3425. if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
  3426. go_intent, if_addr,
  3427. force_freq, persistent_group, ssid,
  3428. pref_freq) < 0)
  3429. return -1;
  3430. return ret;
  3431. }
  3432. if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
  3433. go_intent, if_addr, force_freq,
  3434. persistent_group, ssid, pref_freq) < 0) {
  3435. if (wpa_s->create_p2p_iface)
  3436. wpas_p2p_remove_pending_group_interface(wpa_s);
  3437. return -1;
  3438. }
  3439. return ret;
  3440. }
  3441. /**
  3442. * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
  3443. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3444. * @freq: Frequency of the channel in MHz
  3445. * @duration: Duration of the stay on the channel in milliseconds
  3446. *
  3447. * This callback is called when the driver indicates that it has started the
  3448. * requested remain-on-channel duration.
  3449. */
  3450. void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3451. unsigned int freq, unsigned int duration)
  3452. {
  3453. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3454. return;
  3455. if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
  3456. p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
  3457. wpa_s->pending_listen_duration);
  3458. wpa_s->pending_listen_freq = 0;
  3459. }
  3460. }
  3461. static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
  3462. unsigned int timeout)
  3463. {
  3464. /* Limit maximum Listen state time based on driver limitation. */
  3465. if (timeout > wpa_s->max_remain_on_chan)
  3466. timeout = wpa_s->max_remain_on_chan;
  3467. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3468. return wpa_drv_p2p_listen(wpa_s, timeout);
  3469. return p2p_listen(wpa_s->global->p2p, timeout);
  3470. }
  3471. /**
  3472. * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
  3473. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3474. * @freq: Frequency of the channel in MHz
  3475. *
  3476. * This callback is called when the driver indicates that a remain-on-channel
  3477. * operation has been completed, i.e., the duration on the requested channel
  3478. * has timed out.
  3479. */
  3480. void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  3481. unsigned int freq)
  3482. {
  3483. wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
  3484. "(p2p_long_listen=%d ms pending_action_tx=%p)",
  3485. wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
  3486. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3487. return;
  3488. if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
  3489. return; /* P2P module started a new operation */
  3490. if (offchannel_pending_action_tx(wpa_s))
  3491. return;
  3492. if (wpa_s->p2p_long_listen > 0)
  3493. wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
  3494. if (wpa_s->p2p_long_listen > 0) {
  3495. wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
  3496. wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
  3497. }
  3498. }
  3499. /**
  3500. * wpas_p2p_group_remove - Remove a P2P group
  3501. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3502. * @ifname: Network interface name of the group interface or "*" to remove all
  3503. * groups
  3504. * Returns: 0 on success, -1 on failure
  3505. *
  3506. * This function is used to remove a P2P group. This can be used to disconnect
  3507. * from a group in which the local end is a P2P Client or to end a P2P Group in
  3508. * case the local end is the Group Owner. If a virtual network interface was
  3509. * created for this group, that interface will be removed. Otherwise, only the
  3510. * configured P2P group network will be removed from the interface.
  3511. */
  3512. int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
  3513. {
  3514. struct wpa_global *global = wpa_s->global;
  3515. if (os_strcmp(ifname, "*") == 0) {
  3516. struct wpa_supplicant *prev;
  3517. wpa_s = global->ifaces;
  3518. while (wpa_s) {
  3519. prev = wpa_s;
  3520. wpa_s = wpa_s->next;
  3521. if (prev->p2p_group_interface !=
  3522. NOT_P2P_GROUP_INTERFACE ||
  3523. (prev->current_ssid &&
  3524. prev->current_ssid->p2p_group))
  3525. wpas_p2p_disconnect(prev);
  3526. }
  3527. return 0;
  3528. }
  3529. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  3530. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  3531. break;
  3532. }
  3533. return wpas_p2p_disconnect(wpa_s);
  3534. }
  3535. static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
  3536. {
  3537. unsigned int r;
  3538. if (freq == 2) {
  3539. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
  3540. "band");
  3541. if (wpa_s->best_24_freq > 0 &&
  3542. p2p_supported_freq(wpa_s->global->p2p,
  3543. wpa_s->best_24_freq)) {
  3544. freq = wpa_s->best_24_freq;
  3545. wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
  3546. "channel: %d MHz", freq);
  3547. } else {
  3548. os_get_random((u8 *) &r, sizeof(r));
  3549. freq = 2412 + (r % 3) * 25;
  3550. wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
  3551. "channel: %d MHz", freq);
  3552. }
  3553. }
  3554. if (freq == 5) {
  3555. wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
  3556. "band");
  3557. if (wpa_s->best_5_freq > 0 &&
  3558. p2p_supported_freq(wpa_s->global->p2p,
  3559. wpa_s->best_5_freq)) {
  3560. freq = wpa_s->best_5_freq;
  3561. wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
  3562. "channel: %d MHz", freq);
  3563. } else {
  3564. os_get_random((u8 *) &r, sizeof(r));
  3565. freq = 5180 + (r % 4) * 20;
  3566. if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3567. wpa_printf(MSG_DEBUG, "P2P: Could not select "
  3568. "5 GHz channel for P2P group");
  3569. return -1;
  3570. }
  3571. wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
  3572. "channel: %d MHz", freq);
  3573. }
  3574. }
  3575. if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
  3576. wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
  3577. "(%u MHz) is not supported for P2P uses",
  3578. freq);
  3579. return -1;
  3580. }
  3581. return freq;
  3582. }
  3583. static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
  3584. struct p2p_go_neg_results *params,
  3585. int freq, int ht40,
  3586. const struct p2p_channels *channels)
  3587. {
  3588. int res, *freqs;
  3589. unsigned int pref_freq;
  3590. unsigned int num, i;
  3591. os_memset(params, 0, sizeof(*params));
  3592. params->role_go = 1;
  3593. params->ht40 = ht40;
  3594. if (freq) {
  3595. if (!freq_included(channels, freq)) {
  3596. wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
  3597. "accepted", freq);
  3598. return -1;
  3599. }
  3600. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
  3601. "frequency %d MHz", freq);
  3602. params->freq = freq;
  3603. } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
  3604. wpa_s->conf->p2p_oper_channel >= 1 &&
  3605. wpa_s->conf->p2p_oper_channel <= 11 &&
  3606. freq_included(channels,
  3607. 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3608. params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
  3609. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3610. "frequency %d MHz", params->freq);
  3611. } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
  3612. wpa_s->conf->p2p_oper_reg_class == 116 ||
  3613. wpa_s->conf->p2p_oper_reg_class == 117 ||
  3614. wpa_s->conf->p2p_oper_reg_class == 124 ||
  3615. wpa_s->conf->p2p_oper_reg_class == 126 ||
  3616. wpa_s->conf->p2p_oper_reg_class == 127) &&
  3617. freq_included(channels,
  3618. 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
  3619. params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
  3620. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
  3621. "frequency %d MHz", params->freq);
  3622. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3623. wpa_s->best_overall_freq > 0 &&
  3624. p2p_supported_freq(wpa_s->global->p2p,
  3625. wpa_s->best_overall_freq) &&
  3626. freq_included(channels, wpa_s->best_overall_freq)) {
  3627. params->freq = wpa_s->best_overall_freq;
  3628. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
  3629. "channel %d MHz", params->freq);
  3630. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3631. wpa_s->best_24_freq > 0 &&
  3632. p2p_supported_freq(wpa_s->global->p2p,
  3633. wpa_s->best_24_freq) &&
  3634. freq_included(channels, wpa_s->best_24_freq)) {
  3635. params->freq = wpa_s->best_24_freq;
  3636. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
  3637. "channel %d MHz", params->freq);
  3638. } else if (wpa_s->conf->p2p_oper_channel == 0 &&
  3639. wpa_s->best_5_freq > 0 &&
  3640. p2p_supported_freq(wpa_s->global->p2p,
  3641. wpa_s->best_5_freq) &&
  3642. freq_included(channels, wpa_s->best_5_freq)) {
  3643. params->freq = wpa_s->best_5_freq;
  3644. wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
  3645. "channel %d MHz", params->freq);
  3646. } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
  3647. channels))) {
  3648. params->freq = pref_freq;
  3649. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
  3650. "channels", params->freq);
  3651. } else {
  3652. int chan;
  3653. for (chan = 0; chan < 11; chan++) {
  3654. params->freq = 2412 + chan * 5;
  3655. if (!wpas_p2p_disallowed_freq(wpa_s->global,
  3656. params->freq) &&
  3657. freq_included(channels, params->freq))
  3658. break;
  3659. }
  3660. if (chan == 11) {
  3661. wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
  3662. "allowed");
  3663. return -1;
  3664. }
  3665. wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
  3666. "known)", params->freq);
  3667. }
  3668. freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
  3669. if (!freqs)
  3670. return -1;
  3671. res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
  3672. wpa_s->num_multichan_concurrent);
  3673. if (res < 0) {
  3674. os_free(freqs);
  3675. return -1;
  3676. }
  3677. num = res;
  3678. if (!freq) {
  3679. for (i = 0; i < num; i++) {
  3680. if (freq_included(channels, freqs[i])) {
  3681. wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
  3682. freqs[i]);
  3683. params->freq = freqs[i];
  3684. break;
  3685. }
  3686. }
  3687. if (i == num) {
  3688. if (num == wpa_s->num_multichan_concurrent) {
  3689. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
  3690. os_free(freqs);
  3691. return -1;
  3692. } else {
  3693. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3694. }
  3695. }
  3696. } else {
  3697. for (i = 0; i < num; i++) {
  3698. if (freqs[i] == freq)
  3699. break;
  3700. }
  3701. if (i == num) {
  3702. if (num == wpa_s->num_multichan_concurrent) {
  3703. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
  3704. os_free(freqs);
  3705. return -1;
  3706. } else {
  3707. wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
  3708. }
  3709. }
  3710. }
  3711. os_free(freqs);
  3712. return 0;
  3713. }
  3714. static struct wpa_supplicant *
  3715. wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
  3716. int go)
  3717. {
  3718. struct wpa_supplicant *group_wpa_s;
  3719. if (!wpas_p2p_create_iface(wpa_s)) {
  3720. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
  3721. "operations");
  3722. return wpa_s;
  3723. }
  3724. if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
  3725. WPA_IF_P2P_CLIENT) < 0) {
  3726. wpa_msg_global(wpa_s, MSG_ERROR,
  3727. "P2P: Failed to add group interface");
  3728. return NULL;
  3729. }
  3730. group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
  3731. if (group_wpa_s == NULL) {
  3732. wpa_msg_global(wpa_s, MSG_ERROR,
  3733. "P2P: Failed to initialize group interface");
  3734. wpas_p2p_remove_pending_group_interface(wpa_s);
  3735. return NULL;
  3736. }
  3737. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
  3738. group_wpa_s->ifname);
  3739. return group_wpa_s;
  3740. }
  3741. /**
  3742. * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
  3743. * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
  3744. * @persistent_group: Whether to create a persistent group
  3745. * @freq: Frequency for the group or 0 to indicate no hardcoding
  3746. * Returns: 0 on success, -1 on failure
  3747. *
  3748. * This function creates a new P2P group with the local end as the Group Owner,
  3749. * i.e., without using Group Owner Negotiation.
  3750. */
  3751. int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
  3752. int freq, int ht40)
  3753. {
  3754. struct p2p_go_neg_results params;
  3755. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3756. return -1;
  3757. /* Make sure we are not running find during connection establishment */
  3758. wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
  3759. wpas_p2p_stop_find_oper(wpa_s);
  3760. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3761. if (freq < 0)
  3762. return -1;
  3763. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, NULL))
  3764. return -1;
  3765. if (params.freq &&
  3766. !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
  3767. wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
  3768. "(%u MHz) is not supported for P2P uses",
  3769. params.freq);
  3770. return -1;
  3771. }
  3772. p2p_go_params(wpa_s->global->p2p, &params);
  3773. params.persistent_group = persistent_group;
  3774. wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
  3775. if (wpa_s == NULL)
  3776. return -1;
  3777. wpas_start_wps_go(wpa_s, &params, 0);
  3778. return 0;
  3779. }
  3780. static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
  3781. struct wpa_ssid *params, int addr_allocated)
  3782. {
  3783. struct wpa_ssid *ssid;
  3784. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
  3785. if (wpa_s == NULL)
  3786. return -1;
  3787. wpa_supplicant_ap_deinit(wpa_s);
  3788. ssid = wpa_config_add_network(wpa_s->conf);
  3789. if (ssid == NULL)
  3790. return -1;
  3791. wpa_config_set_network_defaults(ssid);
  3792. ssid->temporary = 1;
  3793. ssid->proto = WPA_PROTO_RSN;
  3794. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  3795. ssid->group_cipher = WPA_CIPHER_CCMP;
  3796. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  3797. ssid->ssid = os_malloc(params->ssid_len);
  3798. if (ssid->ssid == NULL) {
  3799. wpa_config_remove_network(wpa_s->conf, ssid->id);
  3800. return -1;
  3801. }
  3802. os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
  3803. ssid->ssid_len = params->ssid_len;
  3804. ssid->p2p_group = 1;
  3805. ssid->export_keys = 1;
  3806. if (params->psk_set) {
  3807. os_memcpy(ssid->psk, params->psk, 32);
  3808. ssid->psk_set = 1;
  3809. }
  3810. if (params->passphrase)
  3811. ssid->passphrase = os_strdup(params->passphrase);
  3812. wpa_supplicant_select_network(wpa_s, ssid);
  3813. wpa_s->show_group_started = 1;
  3814. return 0;
  3815. }
  3816. int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
  3817. struct wpa_ssid *ssid, int addr_allocated,
  3818. int freq, int ht40,
  3819. const struct p2p_channels *channels)
  3820. {
  3821. struct p2p_go_neg_results params;
  3822. int go = 0;
  3823. if (ssid->disabled != 2 || ssid->ssid == NULL)
  3824. return -1;
  3825. if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
  3826. go == (ssid->mode == WPAS_MODE_P2P_GO)) {
  3827. wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
  3828. "already running");
  3829. return 0;
  3830. }
  3831. /* Make sure we are not running find during connection establishment */
  3832. wpas_p2p_stop_find_oper(wpa_s);
  3833. wpa_s->p2p_fallback_to_go_neg = 0;
  3834. if (ssid->mode == WPAS_MODE_INFRA)
  3835. return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
  3836. if (ssid->mode != WPAS_MODE_P2P_GO)
  3837. return -1;
  3838. freq = wpas_p2p_select_go_freq(wpa_s, freq);
  3839. if (freq < 0)
  3840. return -1;
  3841. if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, channels))
  3842. return -1;
  3843. params.role_go = 1;
  3844. params.psk_set = ssid->psk_set;
  3845. if (params.psk_set)
  3846. os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
  3847. if (ssid->passphrase) {
  3848. if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
  3849. wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
  3850. "persistent group");
  3851. return -1;
  3852. }
  3853. os_strlcpy(params.passphrase, ssid->passphrase,
  3854. sizeof(params.passphrase));
  3855. }
  3856. os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
  3857. params.ssid_len = ssid->ssid_len;
  3858. params.persistent_group = 1;
  3859. wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
  3860. if (wpa_s == NULL)
  3861. return -1;
  3862. wpas_start_wps_go(wpa_s, &params, 0);
  3863. return 0;
  3864. }
  3865. static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
  3866. struct wpabuf *proberesp_ies)
  3867. {
  3868. struct wpa_supplicant *wpa_s = ctx;
  3869. if (wpa_s->ap_iface) {
  3870. struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
  3871. if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
  3872. wpabuf_free(beacon_ies);
  3873. wpabuf_free(proberesp_ies);
  3874. return;
  3875. }
  3876. if (beacon_ies) {
  3877. wpabuf_free(hapd->p2p_beacon_ie);
  3878. hapd->p2p_beacon_ie = beacon_ies;
  3879. }
  3880. wpabuf_free(hapd->p2p_probe_resp_ie);
  3881. hapd->p2p_probe_resp_ie = proberesp_ies;
  3882. } else {
  3883. wpabuf_free(beacon_ies);
  3884. wpabuf_free(proberesp_ies);
  3885. }
  3886. wpa_supplicant_ap_update_beacon(wpa_s);
  3887. }
  3888. static void wpas_p2p_idle_update(void *ctx, int idle)
  3889. {
  3890. struct wpa_supplicant *wpa_s = ctx;
  3891. if (!wpa_s->ap_iface)
  3892. return;
  3893. wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
  3894. if (idle)
  3895. wpas_p2p_set_group_idle_timeout(wpa_s);
  3896. else
  3897. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
  3898. }
  3899. struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
  3900. struct wpa_ssid *ssid)
  3901. {
  3902. struct p2p_group *group;
  3903. struct p2p_group_config *cfg;
  3904. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3905. return NULL;
  3906. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  3907. return NULL;
  3908. cfg = os_zalloc(sizeof(*cfg));
  3909. if (cfg == NULL)
  3910. return NULL;
  3911. if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
  3912. cfg->persistent_group = 2;
  3913. else if (ssid->p2p_persistent_group)
  3914. cfg->persistent_group = 1;
  3915. os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
  3916. if (wpa_s->max_stations &&
  3917. wpa_s->max_stations < wpa_s->conf->max_num_sta)
  3918. cfg->max_clients = wpa_s->max_stations;
  3919. else
  3920. cfg->max_clients = wpa_s->conf->max_num_sta;
  3921. os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
  3922. cfg->ssid_len = ssid->ssid_len;
  3923. cfg->cb_ctx = wpa_s;
  3924. cfg->ie_update = wpas_p2p_ie_update;
  3925. cfg->idle_update = wpas_p2p_idle_update;
  3926. group = p2p_group_init(wpa_s->global->p2p, cfg);
  3927. if (group == NULL)
  3928. os_free(cfg);
  3929. if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
  3930. p2p_group_notif_formation_done(group);
  3931. wpa_s->p2p_group = group;
  3932. return group;
  3933. }
  3934. void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3935. int registrar)
  3936. {
  3937. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3938. if (!wpa_s->p2p_in_provisioning) {
  3939. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
  3940. "provisioning not in progress");
  3941. return;
  3942. }
  3943. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3944. u8 go_dev_addr[ETH_ALEN];
  3945. os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
  3946. wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  3947. ssid->ssid_len);
  3948. /* Clear any stored provisioning info */
  3949. p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
  3950. }
  3951. eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
  3952. NULL);
  3953. if (ssid && ssid->mode == WPAS_MODE_INFRA) {
  3954. /*
  3955. * Use a separate timeout for initial data connection to
  3956. * complete to allow the group to be removed automatically if
  3957. * something goes wrong in this step before the P2P group idle
  3958. * timeout mechanism is taken into use.
  3959. */
  3960. eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
  3961. wpas_p2p_group_formation_timeout,
  3962. wpa_s->parent, NULL);
  3963. }
  3964. if (wpa_s->global->p2p)
  3965. p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
  3966. else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  3967. wpa_drv_wps_success_cb(wpa_s, peer_addr);
  3968. wpas_group_formation_completed(wpa_s, 1);
  3969. }
  3970. void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
  3971. struct wps_event_fail *fail)
  3972. {
  3973. if (!wpa_s->p2p_in_provisioning) {
  3974. wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
  3975. "provisioning not in progress");
  3976. return;
  3977. }
  3978. if (wpa_s->go_params) {
  3979. p2p_clear_provisioning_info(
  3980. wpa_s->global->p2p,
  3981. wpa_s->go_params->peer_device_addr);
  3982. }
  3983. wpas_notify_p2p_wps_failed(wpa_s, fail);
  3984. }
  3985. int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  3986. const char *config_method,
  3987. enum wpas_p2p_prov_disc_use use)
  3988. {
  3989. u16 config_methods;
  3990. wpa_s->p2p_fallback_to_go_neg = 0;
  3991. wpa_s->pending_pd_use = NORMAL_PD;
  3992. if (os_strncmp(config_method, "display", 7) == 0)
  3993. config_methods = WPS_CONFIG_DISPLAY;
  3994. else if (os_strncmp(config_method, "keypad", 6) == 0)
  3995. config_methods = WPS_CONFIG_KEYPAD;
  3996. else if (os_strncmp(config_method, "pbc", 3) == 0 ||
  3997. os_strncmp(config_method, "pushbutton", 10) == 0)
  3998. config_methods = WPS_CONFIG_PUSHBUTTON;
  3999. else {
  4000. wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
  4001. return -1;
  4002. }
  4003. if (use == WPAS_P2P_PD_AUTO) {
  4004. os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
  4005. wpa_s->pending_pd_config_methods = config_methods;
  4006. wpa_s->p2p_auto_pd = 1;
  4007. wpa_s->p2p_auto_join = 0;
  4008. wpa_s->pending_pd_before_join = 0;
  4009. wpa_s->auto_pd_scan_retry = 0;
  4010. wpas_p2p_stop_find(wpa_s);
  4011. wpa_s->p2p_join_scan_count = 0;
  4012. os_get_time(&wpa_s->p2p_auto_started);
  4013. wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
  4014. wpa_s->p2p_auto_started.sec,
  4015. wpa_s->p2p_auto_started.usec);
  4016. wpas_p2p_join_scan(wpa_s, NULL);
  4017. return 0;
  4018. }
  4019. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4020. return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
  4021. config_methods,
  4022. use == WPAS_P2P_PD_FOR_JOIN);
  4023. }
  4024. if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
  4025. return -1;
  4026. return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
  4027. config_methods, use == WPAS_P2P_PD_FOR_JOIN,
  4028. 0, 1);
  4029. }
  4030. int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  4031. char *end)
  4032. {
  4033. return p2p_scan_result_text(ies, ies_len, buf, end);
  4034. }
  4035. static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
  4036. {
  4037. if (!offchannel_pending_action_tx(wpa_s))
  4038. return;
  4039. wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
  4040. "operation request");
  4041. offchannel_clear_pending_action_tx(wpa_s);
  4042. }
  4043. int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
  4044. enum p2p_discovery_type type,
  4045. unsigned int num_req_dev_types, const u8 *req_dev_types,
  4046. const u8 *dev_id, unsigned int search_delay)
  4047. {
  4048. wpas_p2p_clear_pending_action_tx(wpa_s);
  4049. wpa_s->p2p_long_listen = 0;
  4050. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4051. return wpa_drv_p2p_find(wpa_s, timeout, type);
  4052. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
  4053. wpa_s->p2p_in_provisioning)
  4054. return -1;
  4055. wpa_supplicant_cancel_sched_scan(wpa_s);
  4056. return p2p_find(wpa_s->global->p2p, timeout, type,
  4057. num_req_dev_types, req_dev_types, dev_id,
  4058. search_delay);
  4059. }
  4060. static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
  4061. {
  4062. wpas_p2p_clear_pending_action_tx(wpa_s);
  4063. wpa_s->p2p_long_listen = 0;
  4064. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4065. eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
  4066. wpa_s->global->p2p_cb_on_scan_complete = 0;
  4067. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
  4068. wpa_drv_p2p_stop_find(wpa_s);
  4069. return 1;
  4070. }
  4071. if (wpa_s->global->p2p)
  4072. p2p_stop_find(wpa_s->global->p2p);
  4073. return 0;
  4074. }
  4075. void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
  4076. {
  4077. if (wpas_p2p_stop_find_oper(wpa_s) > 0)
  4078. return;
  4079. wpas_p2p_remove_pending_group_interface(wpa_s);
  4080. }
  4081. static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
  4082. {
  4083. struct wpa_supplicant *wpa_s = eloop_ctx;
  4084. wpa_s->p2p_long_listen = 0;
  4085. }
  4086. int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
  4087. {
  4088. int res;
  4089. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4090. return -1;
  4091. wpa_supplicant_cancel_sched_scan(wpa_s);
  4092. wpas_p2p_clear_pending_action_tx(wpa_s);
  4093. if (timeout == 0) {
  4094. /*
  4095. * This is a request for unlimited Listen state. However, at
  4096. * least for now, this is mapped to a Listen state for one
  4097. * hour.
  4098. */
  4099. timeout = 3600;
  4100. }
  4101. eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
  4102. wpa_s->p2p_long_listen = 0;
  4103. /*
  4104. * Stop previous find/listen operation to avoid trying to request a new
  4105. * remain-on-channel operation while the driver is still running the
  4106. * previous one.
  4107. */
  4108. if (wpa_s->global->p2p)
  4109. p2p_stop_find(wpa_s->global->p2p);
  4110. res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
  4111. if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
  4112. wpa_s->p2p_long_listen = timeout * 1000;
  4113. eloop_register_timeout(timeout, 0,
  4114. wpas_p2p_long_listen_timeout,
  4115. wpa_s, NULL);
  4116. }
  4117. return res;
  4118. }
  4119. int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  4120. u8 *buf, size_t len, int p2p_group)
  4121. {
  4122. struct wpabuf *p2p_ie;
  4123. int ret;
  4124. if (wpa_s->global->p2p_disabled)
  4125. return -1;
  4126. if (wpa_s->global->p2p == NULL)
  4127. return -1;
  4128. if (bss == NULL)
  4129. return -1;
  4130. p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  4131. ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
  4132. p2p_group, p2p_ie);
  4133. wpabuf_free(p2p_ie);
  4134. return ret;
  4135. }
  4136. int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
  4137. const u8 *dst, const u8 *bssid,
  4138. const u8 *ie, size_t ie_len, int ssi_signal)
  4139. {
  4140. if (wpa_s->global->p2p_disabled)
  4141. return 0;
  4142. if (wpa_s->global->p2p == NULL)
  4143. return 0;
  4144. switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
  4145. ie, ie_len)) {
  4146. case P2P_PREQ_NOT_P2P:
  4147. wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
  4148. ssi_signal);
  4149. /* fall through */
  4150. case P2P_PREQ_MALFORMED:
  4151. case P2P_PREQ_NOT_LISTEN:
  4152. case P2P_PREQ_NOT_PROCESSED:
  4153. default: /* make gcc happy */
  4154. return 0;
  4155. case P2P_PREQ_PROCESSED:
  4156. return 1;
  4157. }
  4158. }
  4159. void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
  4160. const u8 *sa, const u8 *bssid,
  4161. u8 category, const u8 *data, size_t len, int freq)
  4162. {
  4163. if (wpa_s->global->p2p_disabled)
  4164. return;
  4165. if (wpa_s->global->p2p == NULL)
  4166. return;
  4167. p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
  4168. freq);
  4169. }
  4170. void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
  4171. {
  4172. if (wpa_s->global->p2p_disabled)
  4173. return;
  4174. if (wpa_s->global->p2p == NULL)
  4175. return;
  4176. p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
  4177. }
  4178. void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
  4179. {
  4180. p2p_group_deinit(wpa_s->p2p_group);
  4181. wpa_s->p2p_group = NULL;
  4182. wpa_s->ap_configured_cb = NULL;
  4183. wpa_s->ap_configured_cb_ctx = NULL;
  4184. wpa_s->ap_configured_cb_data = NULL;
  4185. wpa_s->connect_without_scan = NULL;
  4186. }
  4187. int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
  4188. {
  4189. wpa_s->p2p_long_listen = 0;
  4190. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4191. return wpa_drv_p2p_reject(wpa_s, addr);
  4192. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4193. return -1;
  4194. return p2p_reject(wpa_s->global->p2p, addr);
  4195. }
  4196. /* Invite to reinvoke a persistent group */
  4197. int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
  4198. struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
  4199. int ht40, int pref_freq)
  4200. {
  4201. enum p2p_invite_role role;
  4202. u8 *bssid = NULL;
  4203. int force_freq = 0;
  4204. int res;
  4205. wpa_s->global->p2p_invite_group = NULL;
  4206. if (peer_addr)
  4207. os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
  4208. else
  4209. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4210. wpa_s->p2p_persistent_go_freq = freq;
  4211. wpa_s->p2p_go_ht40 = !!ht40;
  4212. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4213. role = P2P_INVITE_ROLE_GO;
  4214. if (peer_addr == NULL) {
  4215. wpa_printf(MSG_DEBUG, "P2P: Missing peer "
  4216. "address in invitation command");
  4217. return -1;
  4218. }
  4219. if (wpas_p2p_create_iface(wpa_s)) {
  4220. if (wpas_p2p_add_group_interface(wpa_s,
  4221. WPA_IF_P2P_GO) < 0) {
  4222. wpa_printf(MSG_ERROR, "P2P: Failed to "
  4223. "allocate a new interface for the "
  4224. "group");
  4225. return -1;
  4226. }
  4227. bssid = wpa_s->pending_interface_addr;
  4228. } else
  4229. bssid = wpa_s->own_addr;
  4230. } else {
  4231. role = P2P_INVITE_ROLE_CLIENT;
  4232. peer_addr = ssid->bssid;
  4233. }
  4234. wpa_s->pending_invite_ssid_id = ssid->id;
  4235. res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
  4236. if (res)
  4237. return res;
  4238. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4239. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4240. ssid->ssid, ssid->ssid_len,
  4241. go_dev_addr, 1);
  4242. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4243. return -1;
  4244. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4245. ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
  4246. 1, pref_freq);
  4247. }
  4248. /* Invite to join an active group */
  4249. int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
  4250. const u8 *peer_addr, const u8 *go_dev_addr)
  4251. {
  4252. struct wpa_global *global = wpa_s->global;
  4253. enum p2p_invite_role role;
  4254. u8 *bssid = NULL;
  4255. struct wpa_ssid *ssid;
  4256. int persistent;
  4257. int force_freq = 0, pref_freq = 0;
  4258. int res;
  4259. wpa_s->p2p_persistent_go_freq = 0;
  4260. wpa_s->p2p_go_ht40 = 0;
  4261. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4262. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4263. break;
  4264. }
  4265. if (wpa_s == NULL) {
  4266. wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
  4267. return -1;
  4268. }
  4269. ssid = wpa_s->current_ssid;
  4270. if (ssid == NULL) {
  4271. wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
  4272. "invitation");
  4273. return -1;
  4274. }
  4275. wpa_s->global->p2p_invite_group = wpa_s;
  4276. persistent = ssid->p2p_persistent_group &&
  4277. wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
  4278. ssid->ssid, ssid->ssid_len);
  4279. if (ssid->mode == WPAS_MODE_P2P_GO) {
  4280. role = P2P_INVITE_ROLE_ACTIVE_GO;
  4281. bssid = wpa_s->own_addr;
  4282. if (go_dev_addr == NULL)
  4283. go_dev_addr = wpa_s->global->p2p_dev_addr;
  4284. } else {
  4285. role = P2P_INVITE_ROLE_CLIENT;
  4286. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  4287. wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
  4288. "invite to current group");
  4289. return -1;
  4290. }
  4291. bssid = wpa_s->bssid;
  4292. if (go_dev_addr == NULL &&
  4293. !is_zero_ether_addr(wpa_s->go_dev_addr))
  4294. go_dev_addr = wpa_s->go_dev_addr;
  4295. }
  4296. wpa_s->parent->pending_invite_ssid_id = -1;
  4297. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4298. return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
  4299. ssid->ssid, ssid->ssid_len,
  4300. go_dev_addr, persistent);
  4301. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4302. return -1;
  4303. res = wpas_p2p_setup_freqs(wpa_s, 0, &force_freq, &pref_freq);
  4304. if (res)
  4305. return res;
  4306. wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
  4307. return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
  4308. ssid->ssid, ssid->ssid_len, force_freq,
  4309. go_dev_addr, persistent, pref_freq);
  4310. }
  4311. void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
  4312. {
  4313. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4314. const char *ssid_txt;
  4315. u8 go_dev_addr[ETH_ALEN];
  4316. int network_id = -1;
  4317. int persistent;
  4318. int freq;
  4319. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
  4320. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4321. wpa_s->parent, NULL);
  4322. }
  4323. if (!wpa_s->show_group_started || !ssid)
  4324. goto done;
  4325. wpa_s->show_group_started = 0;
  4326. ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
  4327. os_memset(go_dev_addr, 0, ETH_ALEN);
  4328. if (ssid->bssid_set)
  4329. os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
  4330. persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
  4331. ssid->ssid_len);
  4332. os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
  4333. if (wpa_s->global->p2p_group_formation == wpa_s)
  4334. wpa_s->global->p2p_group_formation = NULL;
  4335. freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
  4336. (int) wpa_s->assoc_freq;
  4337. if (ssid->passphrase == NULL && ssid->psk_set) {
  4338. char psk[65];
  4339. wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
  4340. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4341. "%s client ssid=\"%s\" freq=%d psk=%s "
  4342. "go_dev_addr=" MACSTR "%s",
  4343. wpa_s->ifname, ssid_txt, freq, psk,
  4344. MAC2STR(go_dev_addr),
  4345. persistent ? " [PERSISTENT]" : "");
  4346. } else {
  4347. wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
  4348. "%s client ssid=\"%s\" freq=%d "
  4349. "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
  4350. wpa_s->ifname, ssid_txt, freq,
  4351. ssid->passphrase ? ssid->passphrase : "",
  4352. MAC2STR(go_dev_addr),
  4353. persistent ? " [PERSISTENT]" : "");
  4354. }
  4355. if (persistent)
  4356. network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
  4357. ssid, go_dev_addr);
  4358. if (network_id < 0)
  4359. network_id = ssid->id;
  4360. wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
  4361. done:
  4362. wpas_p2p_continue_after_scan(wpa_s);
  4363. }
  4364. int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
  4365. u32 interval1, u32 duration2, u32 interval2)
  4366. {
  4367. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4368. return -1;
  4369. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4370. return -1;
  4371. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  4372. wpa_s->current_ssid == NULL ||
  4373. wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
  4374. return -1;
  4375. return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
  4376. wpa_s->own_addr, wpa_s->assoc_freq,
  4377. duration1, interval1, duration2, interval2);
  4378. }
  4379. int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
  4380. unsigned int interval)
  4381. {
  4382. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4383. return -1;
  4384. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4385. return -1;
  4386. return p2p_ext_listen(wpa_s->global->p2p, period, interval);
  4387. }
  4388. static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
  4389. {
  4390. if (wpa_s->current_ssid == NULL) {
  4391. /*
  4392. * current_ssid can be cleared when P2P client interface gets
  4393. * disconnected, so assume this interface was used as P2P
  4394. * client.
  4395. */
  4396. return 1;
  4397. }
  4398. return wpa_s->current_ssid->p2p_group &&
  4399. wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
  4400. }
  4401. static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
  4402. {
  4403. struct wpa_supplicant *wpa_s = eloop_ctx;
  4404. if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
  4405. wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
  4406. "disabled");
  4407. return;
  4408. }
  4409. wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
  4410. "group");
  4411. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
  4412. }
  4413. static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
  4414. {
  4415. int timeout;
  4416. if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4417. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4418. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4419. return;
  4420. timeout = wpa_s->conf->p2p_group_idle;
  4421. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  4422. (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
  4423. timeout = P2P_MAX_CLIENT_IDLE;
  4424. if (timeout == 0)
  4425. return;
  4426. if (timeout < 0) {
  4427. if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
  4428. timeout = 0; /* special client mode no-timeout */
  4429. else
  4430. return;
  4431. }
  4432. if (wpa_s->p2p_in_provisioning) {
  4433. /*
  4434. * Use the normal group formation timeout during the
  4435. * provisioning phase to avoid terminating this process too
  4436. * early due to group idle timeout.
  4437. */
  4438. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4439. "during provisioning");
  4440. return;
  4441. }
  4442. if (wpa_s->show_group_started) {
  4443. /*
  4444. * Use the normal group formation timeout between the end of
  4445. * the provisioning phase and completion of 4-way handshake to
  4446. * avoid terminating this process too early due to group idle
  4447. * timeout.
  4448. */
  4449. wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
  4450. "while waiting for initial 4-way handshake to "
  4451. "complete");
  4452. return;
  4453. }
  4454. wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
  4455. timeout);
  4456. eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
  4457. wpa_s, NULL);
  4458. }
  4459. /* Returns 1 if the interface was removed */
  4460. int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4461. u16 reason_code, const u8 *ie, size_t ie_len,
  4462. int locally_generated)
  4463. {
  4464. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4465. return 0;
  4466. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4467. return 0;
  4468. if (!locally_generated)
  4469. p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4470. ie_len);
  4471. if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
  4472. wpa_s->current_ssid &&
  4473. wpa_s->current_ssid->p2p_group &&
  4474. wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
  4475. wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
  4476. "session is ending");
  4477. if (wpas_p2p_group_delete(wpa_s,
  4478. P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
  4479. > 0)
  4480. return 1;
  4481. }
  4482. return 0;
  4483. }
  4484. void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
  4485. u16 reason_code, const u8 *ie, size_t ie_len,
  4486. int locally_generated)
  4487. {
  4488. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4489. return;
  4490. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4491. return;
  4492. if (!locally_generated)
  4493. p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
  4494. ie_len);
  4495. }
  4496. void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
  4497. {
  4498. struct p2p_data *p2p = wpa_s->global->p2p;
  4499. if (p2p == NULL)
  4500. return;
  4501. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  4502. return;
  4503. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
  4504. p2p_set_dev_name(p2p, wpa_s->conf->device_name);
  4505. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  4506. p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
  4507. if (wpa_s->wps &&
  4508. (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
  4509. p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
  4510. if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
  4511. p2p_set_uuid(p2p, wpa_s->wps->uuid);
  4512. if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
  4513. p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
  4514. p2p_set_model_name(p2p, wpa_s->conf->model_name);
  4515. p2p_set_model_number(p2p, wpa_s->conf->model_number);
  4516. p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
  4517. }
  4518. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
  4519. p2p_set_sec_dev_types(p2p,
  4520. (void *) wpa_s->conf->sec_device_type,
  4521. wpa_s->conf->num_sec_device_types);
  4522. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
  4523. int i;
  4524. p2p_remove_wps_vendor_extensions(p2p);
  4525. for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
  4526. if (wpa_s->conf->wps_vendor_ext[i] == NULL)
  4527. continue;
  4528. p2p_add_wps_vendor_extension(
  4529. p2p, wpa_s->conf->wps_vendor_ext[i]);
  4530. }
  4531. }
  4532. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4533. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4534. char country[3];
  4535. country[0] = wpa_s->conf->country[0];
  4536. country[1] = wpa_s->conf->country[1];
  4537. country[2] = 0x04;
  4538. p2p_set_country(p2p, country);
  4539. }
  4540. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
  4541. p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
  4542. wpa_s->conf->p2p_ssid_postfix ?
  4543. os_strlen(wpa_s->conf->p2p_ssid_postfix) :
  4544. 0);
  4545. }
  4546. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
  4547. p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
  4548. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
  4549. u8 reg_class, channel;
  4550. int ret;
  4551. unsigned int r;
  4552. if (wpa_s->conf->p2p_listen_reg_class &&
  4553. wpa_s->conf->p2p_listen_channel) {
  4554. reg_class = wpa_s->conf->p2p_listen_reg_class;
  4555. channel = wpa_s->conf->p2p_listen_channel;
  4556. } else {
  4557. reg_class = 81;
  4558. /*
  4559. * Pick one of the social channels randomly as the
  4560. * listen channel.
  4561. */
  4562. os_get_random((u8 *) &r, sizeof(r));
  4563. channel = 1 + (r % 3) * 5;
  4564. }
  4565. ret = p2p_set_listen_channel(p2p, reg_class, channel);
  4566. if (ret)
  4567. wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
  4568. "failed: %d", ret);
  4569. }
  4570. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
  4571. u8 op_reg_class, op_channel, cfg_op_channel;
  4572. int ret = 0;
  4573. unsigned int r;
  4574. if (wpa_s->conf->p2p_oper_reg_class &&
  4575. wpa_s->conf->p2p_oper_channel) {
  4576. op_reg_class = wpa_s->conf->p2p_oper_reg_class;
  4577. op_channel = wpa_s->conf->p2p_oper_channel;
  4578. cfg_op_channel = 1;
  4579. } else {
  4580. op_reg_class = 81;
  4581. /*
  4582. * Use random operation channel from (1, 6, 11)
  4583. *if no other preference is indicated.
  4584. */
  4585. os_get_random((u8 *) &r, sizeof(r));
  4586. op_channel = 1 + (r % 3) * 5;
  4587. cfg_op_channel = 0;
  4588. }
  4589. ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
  4590. cfg_op_channel);
  4591. if (ret)
  4592. wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
  4593. "failed: %d", ret);
  4594. }
  4595. if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
  4596. if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
  4597. wpa_s->conf->p2p_pref_chan) < 0) {
  4598. wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
  4599. "update failed");
  4600. }
  4601. }
  4602. }
  4603. int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
  4604. int duration)
  4605. {
  4606. if (!wpa_s->ap_iface)
  4607. return -1;
  4608. return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
  4609. duration);
  4610. }
  4611. int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
  4612. {
  4613. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4614. return -1;
  4615. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
  4616. return -1;
  4617. wpa_s->global->cross_connection = enabled;
  4618. p2p_set_cross_connect(wpa_s->global->p2p, enabled);
  4619. if (!enabled) {
  4620. struct wpa_supplicant *iface;
  4621. for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
  4622. {
  4623. if (iface->cross_connect_enabled == 0)
  4624. continue;
  4625. iface->cross_connect_enabled = 0;
  4626. iface->cross_connect_in_use = 0;
  4627. wpa_msg_global(iface->parent, MSG_INFO,
  4628. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4629. iface->ifname,
  4630. iface->cross_connect_uplink);
  4631. }
  4632. }
  4633. return 0;
  4634. }
  4635. static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
  4636. {
  4637. struct wpa_supplicant *iface;
  4638. if (!uplink->global->cross_connection)
  4639. return;
  4640. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4641. if (!iface->cross_connect_enabled)
  4642. continue;
  4643. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4644. 0)
  4645. continue;
  4646. if (iface->ap_iface == NULL)
  4647. continue;
  4648. if (iface->cross_connect_in_use)
  4649. continue;
  4650. iface->cross_connect_in_use = 1;
  4651. wpa_msg_global(iface->parent, MSG_INFO,
  4652. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4653. iface->ifname, iface->cross_connect_uplink);
  4654. }
  4655. }
  4656. static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
  4657. {
  4658. struct wpa_supplicant *iface;
  4659. for (iface = uplink->global->ifaces; iface; iface = iface->next) {
  4660. if (!iface->cross_connect_enabled)
  4661. continue;
  4662. if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
  4663. 0)
  4664. continue;
  4665. if (!iface->cross_connect_in_use)
  4666. continue;
  4667. wpa_msg_global(iface->parent, MSG_INFO,
  4668. P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
  4669. iface->ifname, iface->cross_connect_uplink);
  4670. iface->cross_connect_in_use = 0;
  4671. }
  4672. }
  4673. void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
  4674. {
  4675. if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
  4676. wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
  4677. wpa_s->cross_connect_disallowed)
  4678. wpas_p2p_disable_cross_connect(wpa_s);
  4679. else
  4680. wpas_p2p_enable_cross_connect(wpa_s);
  4681. if (!wpa_s->ap_iface &&
  4682. eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
  4683. wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
  4684. }
  4685. void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
  4686. {
  4687. wpas_p2p_disable_cross_connect(wpa_s);
  4688. if (!wpa_s->ap_iface &&
  4689. !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
  4690. wpa_s, NULL))
  4691. wpas_p2p_set_group_idle_timeout(wpa_s);
  4692. }
  4693. static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
  4694. {
  4695. struct wpa_supplicant *iface;
  4696. if (!wpa_s->global->cross_connection)
  4697. return;
  4698. for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
  4699. if (iface == wpa_s)
  4700. continue;
  4701. if (iface->drv_flags &
  4702. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
  4703. continue;
  4704. if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
  4705. continue;
  4706. wpa_s->cross_connect_enabled = 1;
  4707. os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
  4708. sizeof(wpa_s->cross_connect_uplink));
  4709. wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
  4710. "%s to %s whenever uplink is available",
  4711. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4712. if (iface->ap_iface || iface->current_ssid == NULL ||
  4713. iface->current_ssid->mode != WPAS_MODE_INFRA ||
  4714. iface->cross_connect_disallowed ||
  4715. iface->wpa_state != WPA_COMPLETED)
  4716. break;
  4717. wpa_s->cross_connect_in_use = 1;
  4718. wpa_msg_global(wpa_s->parent, MSG_INFO,
  4719. P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
  4720. wpa_s->ifname, wpa_s->cross_connect_uplink);
  4721. break;
  4722. }
  4723. }
  4724. int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
  4725. {
  4726. if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
  4727. !wpa_s->p2p_in_provisioning)
  4728. return 0; /* not P2P client operation */
  4729. wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
  4730. "session overlap");
  4731. if (wpa_s != wpa_s->parent)
  4732. wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
  4733. wpas_p2p_group_formation_failed(wpa_s);
  4734. return 1;
  4735. }
  4736. void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
  4737. {
  4738. struct p2p_channels chan;
  4739. if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
  4740. return;
  4741. os_memset(&chan, 0, sizeof(chan));
  4742. if (wpas_p2p_setup_channels(wpa_s, &chan)) {
  4743. wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
  4744. "channel list");
  4745. return;
  4746. }
  4747. p2p_update_channel_list(wpa_s->global->p2p, &chan);
  4748. }
  4749. static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
  4750. struct wpa_scan_results *scan_res)
  4751. {
  4752. wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
  4753. }
  4754. int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
  4755. {
  4756. struct wpa_global *global = wpa_s->global;
  4757. int found = 0;
  4758. const u8 *peer;
  4759. if (global->p2p == NULL)
  4760. return -1;
  4761. wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
  4762. if (wpa_s->pending_interface_name[0] &&
  4763. !is_zero_ether_addr(wpa_s->pending_interface_addr))
  4764. found = 1;
  4765. peer = p2p_get_go_neg_peer(global->p2p);
  4766. if (peer) {
  4767. wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
  4768. MACSTR, MAC2STR(peer));
  4769. p2p_unauthorize(global->p2p, peer);
  4770. found = 1;
  4771. }
  4772. if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
  4773. wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
  4774. wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
  4775. found = 1;
  4776. }
  4777. if (wpa_s->pending_pd_before_join) {
  4778. wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
  4779. wpa_s->pending_pd_before_join = 0;
  4780. found = 1;
  4781. }
  4782. wpas_p2p_stop_find(wpa_s);
  4783. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4784. if (wpa_s == global->p2p_group_formation &&
  4785. (wpa_s->p2p_in_provisioning ||
  4786. wpa_s->parent->pending_interface_type ==
  4787. WPA_IF_P2P_CLIENT)) {
  4788. wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
  4789. "formation found - cancelling",
  4790. wpa_s->ifname);
  4791. found = 1;
  4792. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4793. wpa_s->parent, NULL);
  4794. if (wpa_s->p2p_in_provisioning) {
  4795. wpas_group_formation_completed(wpa_s, 0);
  4796. break;
  4797. }
  4798. wpas_p2p_group_delete(wpa_s,
  4799. P2P_GROUP_REMOVAL_REQUESTED);
  4800. break;
  4801. }
  4802. }
  4803. if (!found) {
  4804. wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
  4805. return -1;
  4806. }
  4807. return 0;
  4808. }
  4809. void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
  4810. {
  4811. if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
  4812. return;
  4813. wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
  4814. "being available anymore");
  4815. wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
  4816. }
  4817. void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
  4818. int freq_24, int freq_5, int freq_overall)
  4819. {
  4820. struct p2p_data *p2p = wpa_s->global->p2p;
  4821. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4822. return;
  4823. p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
  4824. }
  4825. int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
  4826. {
  4827. u8 peer[ETH_ALEN];
  4828. struct p2p_data *p2p = wpa_s->global->p2p;
  4829. if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
  4830. return -1;
  4831. if (hwaddr_aton(addr, peer))
  4832. return -1;
  4833. return p2p_unauthorize(p2p, peer);
  4834. }
  4835. /**
  4836. * wpas_p2p_disconnect - Disconnect from a P2P Group
  4837. * @wpa_s: Pointer to wpa_supplicant data
  4838. * Returns: 0 on success, -1 on failure
  4839. *
  4840. * This can be used to disconnect from a group in which the local end is a P2P
  4841. * Client or to end a P2P Group in case the local end is the Group Owner. If a
  4842. * virtual network interface was created for this group, that interface will be
  4843. * removed. Otherwise, only the configured P2P group network will be removed
  4844. * from the interface.
  4845. */
  4846. int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
  4847. {
  4848. if (wpa_s == NULL)
  4849. return -1;
  4850. return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
  4851. -1 : 0;
  4852. }
  4853. int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
  4854. {
  4855. int ret;
  4856. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  4857. return 0;
  4858. ret = p2p_in_progress(wpa_s->global->p2p);
  4859. if (ret == 0) {
  4860. /*
  4861. * Check whether there is an ongoing WPS provisioning step (or
  4862. * other parts of group formation) on another interface since
  4863. * p2p_in_progress() does not report this to avoid issues for
  4864. * scans during such provisioning step.
  4865. */
  4866. if (wpa_s->global->p2p_group_formation &&
  4867. wpa_s->global->p2p_group_formation != wpa_s) {
  4868. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
  4869. "in group formation",
  4870. wpa_s->global->p2p_group_formation->ifname);
  4871. ret = 1;
  4872. }
  4873. }
  4874. return ret;
  4875. }
  4876. void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
  4877. struct wpa_ssid *ssid)
  4878. {
  4879. if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
  4880. eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
  4881. wpa_s->parent, NULL) > 0) {
  4882. /**
  4883. * Remove the network by scheduling the group formation
  4884. * timeout to happen immediately. The teardown code
  4885. * needs to be scheduled to run asynch later so that we
  4886. * don't delete data from under ourselves unexpectedly.
  4887. * Calling wpas_p2p_group_formation_timeout directly
  4888. * causes a series of crashes in WPS failure scenarios.
  4889. */
  4890. wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
  4891. "P2P group network getting removed");
  4892. eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
  4893. wpa_s->parent, NULL);
  4894. }
  4895. }
  4896. struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
  4897. const u8 *addr, const u8 *ssid,
  4898. size_t ssid_len)
  4899. {
  4900. struct wpa_ssid *s;
  4901. size_t i;
  4902. for (s = wpa_s->conf->ssid; s; s = s->next) {
  4903. if (s->disabled != 2)
  4904. continue;
  4905. if (ssid &&
  4906. (ssid_len != s->ssid_len ||
  4907. os_memcmp(ssid, s->ssid, ssid_len) != 0))
  4908. continue;
  4909. if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
  4910. return s; /* peer is GO in the persistent group */
  4911. if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
  4912. continue;
  4913. for (i = 0; i < s->num_p2p_clients; i++) {
  4914. if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
  4915. addr, ETH_ALEN) == 0)
  4916. return s; /* peer is P2P client in persistent
  4917. * group */
  4918. }
  4919. }
  4920. return NULL;
  4921. }
  4922. void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
  4923. const u8 *addr)
  4924. {
  4925. if (addr == NULL)
  4926. return;
  4927. wpas_p2p_add_persistent_group_client(wpa_s, addr);
  4928. }
  4929. static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
  4930. int group_added)
  4931. {
  4932. struct wpa_supplicant *group = wpa_s;
  4933. if (wpa_s->global->p2p_group_formation)
  4934. group = wpa_s->global->p2p_group_formation;
  4935. wpa_s = wpa_s->parent;
  4936. offchannel_send_action_done(wpa_s);
  4937. if (group_added)
  4938. wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
  4939. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
  4940. wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
  4941. wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
  4942. 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
  4943. wpa_s->p2p_persistent_id,
  4944. wpa_s->p2p_pd_before_go_neg,
  4945. wpa_s->p2p_go_ht40);
  4946. }
  4947. int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
  4948. {
  4949. if (!wpa_s->p2p_fallback_to_go_neg ||
  4950. wpa_s->p2p_in_provisioning <= 5)
  4951. return 0;
  4952. if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
  4953. return 0; /* peer operating as a GO */
  4954. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
  4955. "fallback to GO Negotiation");
  4956. wpas_p2p_fallback_to_go_neg(wpa_s, 1);
  4957. return 1;
  4958. }
  4959. unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
  4960. {
  4961. const char *rn, *rn2;
  4962. struct wpa_supplicant *ifs;
  4963. if (wpa_s->wpa_state > WPA_SCANNING) {
  4964. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
  4965. "concurrent operation",
  4966. P2P_CONCURRENT_SEARCH_DELAY);
  4967. return P2P_CONCURRENT_SEARCH_DELAY;
  4968. }
  4969. if (!wpa_s->driver->get_radio_name)
  4970. return 0;
  4971. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  4972. if (rn == NULL || rn[0] == '\0')
  4973. return 0;
  4974. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  4975. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  4976. continue;
  4977. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  4978. if (!rn2 || os_strcmp(rn, rn2) != 0)
  4979. continue;
  4980. if (ifs->wpa_state > WPA_SCANNING) {
  4981. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
  4982. "delay due to concurrent operation on "
  4983. "interface %s",
  4984. P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
  4985. return P2P_CONCURRENT_SEARCH_DELAY;
  4986. }
  4987. }
  4988. return 0;
  4989. }
  4990. void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
  4991. {
  4992. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
  4993. "pending anymore (sta_scan_pending=%d "
  4994. "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
  4995. wpa_s->global->p2p_cb_on_scan_complete);
  4996. wpa_s->sta_scan_pending = 0;
  4997. if (!wpa_s->global->p2p_cb_on_scan_complete)
  4998. return;
  4999. wpa_s->global->p2p_cb_on_scan_complete = 0;
  5000. if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
  5001. return;
  5002. if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
  5003. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
  5004. "continued after successful connection");
  5005. p2p_increase_search_delay(wpa_s->global->p2p,
  5006. wpas_p2p_search_delay(wpa_s));
  5007. }
  5008. }