p2p_supplicant.c 163 KB

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