addrconf.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPv6 Address [auto]configuration
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. */
  10. /*
  11. * Changes:
  12. *
  13. * Janos Farkas : delete timer on ifdown
  14. * <chexum@bankinf.banki.hu>
  15. * Andi Kleen : kill double kfree on module
  16. * unload.
  17. * Maciej W. Rozycki : FDDI support
  18. * sekiya@USAGI : Don't send too many RS
  19. * packets.
  20. * yoshfuji@USAGI : Fixed interval between DAD
  21. * packets.
  22. * YOSHIFUJI Hideaki @USAGI : improved accuracy of
  23. * address validation timer.
  24. * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
  25. * support.
  26. * Yuji SEKIYA @USAGI : Don't assign a same IPv6
  27. * address on a same interface.
  28. * YOSHIFUJI Hideaki @USAGI : ARCnet support
  29. * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
  30. * seq_file.
  31. * YOSHIFUJI Hideaki @USAGI : improved source address
  32. * selection; consider scope,
  33. * status etc.
  34. */
  35. #define pr_fmt(fmt) "IPv6: " fmt
  36. #include <linux/errno.h>
  37. #include <linux/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched/signal.h>
  40. #include <linux/socket.h>
  41. #include <linux/sockios.h>
  42. #include <linux/net.h>
  43. #include <linux/inet.h>
  44. #include <linux/in6.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/if_addr.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/if_arcnet.h>
  49. #include <linux/if_infiniband.h>
  50. #include <linux/route.h>
  51. #include <linux/inetdevice.h>
  52. #include <linux/init.h>
  53. #include <linux/slab.h>
  54. #ifdef CONFIG_SYSCTL
  55. #include <linux/sysctl.h>
  56. #endif
  57. #include <linux/capability.h>
  58. #include <linux/delay.h>
  59. #include <linux/notifier.h>
  60. #include <linux/string.h>
  61. #include <linux/hash.h>
  62. #include <net/net_namespace.h>
  63. #include <net/sock.h>
  64. #include <net/snmp.h>
  65. #include <net/6lowpan.h>
  66. #include <net/firewire.h>
  67. #include <net/ipv6.h>
  68. #include <net/protocol.h>
  69. #include <net/ndisc.h>
  70. #include <net/ip6_route.h>
  71. #include <net/addrconf.h>
  72. #include <net/tcp.h>
  73. #include <net/ip.h>
  74. #include <net/netlink.h>
  75. #include <net/pkt_sched.h>
  76. #include <net/l3mdev.h>
  77. #include <linux/if_tunnel.h>
  78. #include <linux/rtnetlink.h>
  79. #include <linux/netconf.h>
  80. #include <linux/random.h>
  81. #include <linux/uaccess.h>
  82. #include <asm/unaligned.h>
  83. #include <linux/proc_fs.h>
  84. #include <linux/seq_file.h>
  85. #include <linux/export.h>
  86. #include <trace/hooks/ipv6.h>
  87. #define INFINITY_LIFE_TIME 0xFFFFFFFF
  88. #define IPV6_MAX_STRLEN \
  89. sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
  90. static inline u32 cstamp_delta(unsigned long cstamp)
  91. {
  92. return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
  93. }
  94. static inline s32 rfc3315_s14_backoff_init(s32 irt)
  95. {
  96. /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
  97. u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
  98. do_div(tmp, 1000000);
  99. return (s32)tmp;
  100. }
  101. static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
  102. {
  103. /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
  104. u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
  105. do_div(tmp, 1000000);
  106. if ((s32)tmp > mrt) {
  107. /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
  108. tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
  109. do_div(tmp, 1000000);
  110. }
  111. return (s32)tmp;
  112. }
  113. #ifdef CONFIG_SYSCTL
  114. static int addrconf_sysctl_register(struct inet6_dev *idev);
  115. static void addrconf_sysctl_unregister(struct inet6_dev *idev);
  116. #else
  117. static inline int addrconf_sysctl_register(struct inet6_dev *idev)
  118. {
  119. return 0;
  120. }
  121. static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
  122. {
  123. }
  124. #endif
  125. static void ipv6_gen_rnd_iid(struct in6_addr *addr);
  126. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
  127. static int ipv6_count_addresses(const struct inet6_dev *idev);
  128. static int ipv6_generate_stable_address(struct in6_addr *addr,
  129. u8 dad_count,
  130. const struct inet6_dev *idev);
  131. #define IN6_ADDR_HSIZE_SHIFT 8
  132. #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
  133. /*
  134. * Configured unicast address hash table
  135. */
  136. static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
  137. static DEFINE_SPINLOCK(addrconf_hash_lock);
  138. static void addrconf_verify(void);
  139. static void addrconf_verify_rtnl(void);
  140. static void addrconf_verify_work(struct work_struct *);
  141. static struct workqueue_struct *addrconf_wq;
  142. static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
  143. static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
  144. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
  145. static void addrconf_type_change(struct net_device *dev,
  146. unsigned long event);
  147. static int addrconf_ifdown(struct net_device *dev, bool unregister);
  148. static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
  149. int plen,
  150. const struct net_device *dev,
  151. u32 flags, u32 noflags,
  152. bool no_gw);
  153. static void addrconf_dad_start(struct inet6_ifaddr *ifp);
  154. static void addrconf_dad_work(struct work_struct *w);
  155. static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
  156. bool send_na);
  157. static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
  158. static void addrconf_rs_timer(struct timer_list *t);
  159. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  160. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
  161. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  162. struct prefix_info *pinfo);
  163. static struct ipv6_devconf ipv6_devconf __read_mostly = {
  164. .forwarding = 0,
  165. .hop_limit = IPV6_DEFAULT_HOPLIMIT,
  166. .mtu6 = IPV6_MIN_MTU,
  167. .accept_ra = 1,
  168. .accept_redirects = 1,
  169. .autoconf = 1,
  170. .force_mld_version = 0,
  171. .mldv1_unsolicited_report_interval = 10 * HZ,
  172. .mldv2_unsolicited_report_interval = HZ,
  173. .dad_transmits = 1,
  174. .rtr_solicits = MAX_RTR_SOLICITATIONS,
  175. .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
  176. .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
  177. .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
  178. .use_tempaddr = 0,
  179. .temp_valid_lft = TEMP_VALID_LIFETIME,
  180. .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
  181. .regen_max_retry = REGEN_MAX_RETRY,
  182. .max_desync_factor = MAX_DESYNC_FACTOR,
  183. .max_addresses = IPV6_MAX_ADDRESSES,
  184. .accept_ra_defrtr = 1,
  185. .accept_ra_from_local = 0,
  186. .accept_ra_min_hop_limit= 1,
  187. .accept_ra_pinfo = 1,
  188. #ifdef CONFIG_IPV6_ROUTER_PREF
  189. .accept_ra_rtr_pref = 1,
  190. .rtr_probe_interval = 60 * HZ,
  191. #ifdef CONFIG_IPV6_ROUTE_INFO
  192. .accept_ra_rt_info_min_plen = 0,
  193. .accept_ra_rt_info_max_plen = 0,
  194. #endif
  195. #endif
  196. .accept_ra_rt_table = 0,
  197. .proxy_ndp = 0,
  198. .accept_source_route = 0, /* we do not accept RH0 by default. */
  199. .disable_ipv6 = 0,
  200. .accept_dad = 0,
  201. .suppress_frag_ndisc = 1,
  202. .accept_ra_mtu = 1,
  203. .stable_secret = {
  204. .initialized = false,
  205. },
  206. .use_oif_addrs_only = 0,
  207. .ignore_routes_with_linkdown = 0,
  208. .keep_addr_on_down = 0,
  209. .seg6_enabled = 0,
  210. #ifdef CONFIG_IPV6_SEG6_HMAC
  211. .seg6_require_hmac = 0,
  212. #endif
  213. .enhanced_dad = 1,
  214. .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
  215. .disable_policy = 0,
  216. .rpl_seg_enabled = 0,
  217. };
  218. static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
  219. .forwarding = 0,
  220. .hop_limit = IPV6_DEFAULT_HOPLIMIT,
  221. .mtu6 = IPV6_MIN_MTU,
  222. .accept_ra = 1,
  223. .accept_redirects = 1,
  224. .autoconf = 1,
  225. .force_mld_version = 0,
  226. .mldv1_unsolicited_report_interval = 10 * HZ,
  227. .mldv2_unsolicited_report_interval = HZ,
  228. .dad_transmits = 1,
  229. .rtr_solicits = MAX_RTR_SOLICITATIONS,
  230. .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
  231. .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
  232. .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
  233. .use_tempaddr = 0,
  234. .temp_valid_lft = TEMP_VALID_LIFETIME,
  235. .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
  236. .regen_max_retry = REGEN_MAX_RETRY,
  237. .max_desync_factor = MAX_DESYNC_FACTOR,
  238. .max_addresses = IPV6_MAX_ADDRESSES,
  239. .accept_ra_defrtr = 1,
  240. .accept_ra_from_local = 0,
  241. .accept_ra_min_hop_limit= 1,
  242. .accept_ra_pinfo = 1,
  243. #ifdef CONFIG_IPV6_ROUTER_PREF
  244. .accept_ra_rtr_pref = 1,
  245. .rtr_probe_interval = 60 * HZ,
  246. #ifdef CONFIG_IPV6_ROUTE_INFO
  247. .accept_ra_rt_info_min_plen = 0,
  248. .accept_ra_rt_info_max_plen = 0,
  249. #endif
  250. #endif
  251. .accept_ra_rt_table = 0,
  252. .proxy_ndp = 0,
  253. .accept_source_route = 0, /* we do not accept RH0 by default. */
  254. .disable_ipv6 = 0,
  255. .accept_dad = 1,
  256. .suppress_frag_ndisc = 1,
  257. .accept_ra_mtu = 1,
  258. .stable_secret = {
  259. .initialized = false,
  260. },
  261. .use_oif_addrs_only = 0,
  262. .ignore_routes_with_linkdown = 0,
  263. .keep_addr_on_down = 0,
  264. .seg6_enabled = 0,
  265. #ifdef CONFIG_IPV6_SEG6_HMAC
  266. .seg6_require_hmac = 0,
  267. #endif
  268. .enhanced_dad = 1,
  269. .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
  270. .disable_policy = 0,
  271. .rpl_seg_enabled = 0,
  272. };
  273. /* Check if link is ready: is it up and is a valid qdisc available */
  274. static inline bool addrconf_link_ready(const struct net_device *dev)
  275. {
  276. return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
  277. }
  278. static void addrconf_del_rs_timer(struct inet6_dev *idev)
  279. {
  280. if (del_timer(&idev->rs_timer))
  281. __in6_dev_put(idev);
  282. }
  283. static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
  284. {
  285. if (cancel_delayed_work(&ifp->dad_work))
  286. __in6_ifa_put(ifp);
  287. }
  288. static void addrconf_mod_rs_timer(struct inet6_dev *idev,
  289. unsigned long when)
  290. {
  291. if (!timer_pending(&idev->rs_timer))
  292. in6_dev_hold(idev);
  293. mod_timer(&idev->rs_timer, jiffies + when);
  294. }
  295. static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
  296. unsigned long delay)
  297. {
  298. in6_ifa_hold(ifp);
  299. if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
  300. in6_ifa_put(ifp);
  301. }
  302. static int snmp6_alloc_dev(struct inet6_dev *idev)
  303. {
  304. int i;
  305. idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
  306. if (!idev->stats.ipv6)
  307. goto err_ip;
  308. for_each_possible_cpu(i) {
  309. struct ipstats_mib *addrconf_stats;
  310. addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
  311. u64_stats_init(&addrconf_stats->syncp);
  312. }
  313. idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
  314. GFP_KERNEL);
  315. if (!idev->stats.icmpv6dev)
  316. goto err_icmp;
  317. idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
  318. GFP_KERNEL);
  319. if (!idev->stats.icmpv6msgdev)
  320. goto err_icmpmsg;
  321. return 0;
  322. err_icmpmsg:
  323. kfree(idev->stats.icmpv6dev);
  324. err_icmp:
  325. free_percpu(idev->stats.ipv6);
  326. err_ip:
  327. return -ENOMEM;
  328. }
  329. static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
  330. {
  331. struct inet6_dev *ndev;
  332. int err = -ENOMEM;
  333. ASSERT_RTNL();
  334. if (dev->mtu < IPV6_MIN_MTU)
  335. return ERR_PTR(-EINVAL);
  336. ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
  337. if (!ndev)
  338. return ERR_PTR(err);
  339. rwlock_init(&ndev->lock);
  340. ndev->dev = dev;
  341. INIT_LIST_HEAD(&ndev->addr_list);
  342. timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
  343. memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
  344. if (ndev->cnf.stable_secret.initialized)
  345. ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
  346. ndev->cnf.mtu6 = dev->mtu;
  347. ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
  348. if (!ndev->nd_parms) {
  349. kfree(ndev);
  350. return ERR_PTR(err);
  351. }
  352. if (ndev->cnf.forwarding)
  353. dev_disable_lro(dev);
  354. /* We refer to the device */
  355. dev_hold(dev);
  356. if (snmp6_alloc_dev(ndev) < 0) {
  357. netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
  358. __func__);
  359. neigh_parms_release(&nd_tbl, ndev->nd_parms);
  360. dev_put(dev);
  361. kfree(ndev);
  362. return ERR_PTR(err);
  363. }
  364. if (snmp6_register_dev(ndev) < 0) {
  365. netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
  366. __func__, dev->name);
  367. goto err_release;
  368. }
  369. /* One reference from device. */
  370. refcount_set(&ndev->refcnt, 1);
  371. if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
  372. ndev->cnf.accept_dad = -1;
  373. #if IS_ENABLED(CONFIG_IPV6_SIT)
  374. if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
  375. pr_info("%s: Disabled Multicast RS\n", dev->name);
  376. ndev->cnf.rtr_solicits = 0;
  377. }
  378. #endif
  379. INIT_LIST_HEAD(&ndev->tempaddr_list);
  380. ndev->desync_factor = U32_MAX;
  381. if ((dev->flags&IFF_LOOPBACK) ||
  382. dev->type == ARPHRD_TUNNEL ||
  383. dev->type == ARPHRD_TUNNEL6 ||
  384. dev->type == ARPHRD_SIT ||
  385. dev->type == ARPHRD_NONE) {
  386. ndev->cnf.use_tempaddr = -1;
  387. }
  388. ndev->token = in6addr_any;
  389. if (netif_running(dev) && addrconf_link_ready(dev))
  390. ndev->if_flags |= IF_READY;
  391. ipv6_mc_init_dev(ndev);
  392. ndev->tstamp = jiffies;
  393. err = addrconf_sysctl_register(ndev);
  394. if (err) {
  395. ipv6_mc_destroy_dev(ndev);
  396. snmp6_unregister_dev(ndev);
  397. goto err_release;
  398. }
  399. /* protected by rtnl_lock */
  400. rcu_assign_pointer(dev->ip6_ptr, ndev);
  401. /* Join interface-local all-node multicast group */
  402. ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
  403. /* Join all-node multicast group */
  404. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
  405. /* Join all-router multicast group if forwarding is set */
  406. if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
  407. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
  408. return ndev;
  409. err_release:
  410. neigh_parms_release(&nd_tbl, ndev->nd_parms);
  411. ndev->dead = 1;
  412. in6_dev_finish_destroy(ndev);
  413. return ERR_PTR(err);
  414. }
  415. static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
  416. {
  417. struct inet6_dev *idev;
  418. ASSERT_RTNL();
  419. idev = __in6_dev_get(dev);
  420. if (!idev) {
  421. idev = ipv6_add_dev(dev);
  422. if (IS_ERR(idev))
  423. return idev;
  424. }
  425. if (dev->flags&IFF_UP)
  426. ipv6_mc_up(idev);
  427. return idev;
  428. }
  429. static int inet6_netconf_msgsize_devconf(int type)
  430. {
  431. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  432. + nla_total_size(4); /* NETCONFA_IFINDEX */
  433. bool all = false;
  434. if (type == NETCONFA_ALL)
  435. all = true;
  436. if (all || type == NETCONFA_FORWARDING)
  437. size += nla_total_size(4);
  438. #ifdef CONFIG_IPV6_MROUTE
  439. if (all || type == NETCONFA_MC_FORWARDING)
  440. size += nla_total_size(4);
  441. #endif
  442. if (all || type == NETCONFA_PROXY_NEIGH)
  443. size += nla_total_size(4);
  444. if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
  445. size += nla_total_size(4);
  446. return size;
  447. }
  448. static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
  449. struct ipv6_devconf *devconf, u32 portid,
  450. u32 seq, int event, unsigned int flags,
  451. int type)
  452. {
  453. struct nlmsghdr *nlh;
  454. struct netconfmsg *ncm;
  455. bool all = false;
  456. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  457. flags);
  458. if (!nlh)
  459. return -EMSGSIZE;
  460. if (type == NETCONFA_ALL)
  461. all = true;
  462. ncm = nlmsg_data(nlh);
  463. ncm->ncm_family = AF_INET6;
  464. if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
  465. goto nla_put_failure;
  466. if (!devconf)
  467. goto out;
  468. if ((all || type == NETCONFA_FORWARDING) &&
  469. nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
  470. goto nla_put_failure;
  471. #ifdef CONFIG_IPV6_MROUTE
  472. if ((all || type == NETCONFA_MC_FORWARDING) &&
  473. nla_put_s32(skb, NETCONFA_MC_FORWARDING,
  474. atomic_read(&devconf->mc_forwarding)) < 0)
  475. goto nla_put_failure;
  476. #endif
  477. if ((all || type == NETCONFA_PROXY_NEIGH) &&
  478. nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
  479. goto nla_put_failure;
  480. if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
  481. nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  482. devconf->ignore_routes_with_linkdown) < 0)
  483. goto nla_put_failure;
  484. out:
  485. nlmsg_end(skb, nlh);
  486. return 0;
  487. nla_put_failure:
  488. nlmsg_cancel(skb, nlh);
  489. return -EMSGSIZE;
  490. }
  491. void inet6_netconf_notify_devconf(struct net *net, int event, int type,
  492. int ifindex, struct ipv6_devconf *devconf)
  493. {
  494. struct sk_buff *skb;
  495. int err = -ENOBUFS;
  496. skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
  497. if (!skb)
  498. goto errout;
  499. err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
  500. event, 0, type);
  501. if (err < 0) {
  502. /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
  503. WARN_ON(err == -EMSGSIZE);
  504. kfree_skb(skb);
  505. goto errout;
  506. }
  507. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
  508. return;
  509. errout:
  510. rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
  511. }
  512. static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
  513. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  514. [NETCONFA_FORWARDING] = { .len = sizeof(int) },
  515. [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
  516. [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
  517. };
  518. static int inet6_netconf_valid_get_req(struct sk_buff *skb,
  519. const struct nlmsghdr *nlh,
  520. struct nlattr **tb,
  521. struct netlink_ext_ack *extack)
  522. {
  523. int i, err;
  524. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
  525. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
  526. return -EINVAL;
  527. }
  528. if (!netlink_strict_get_check(skb))
  529. return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
  530. tb, NETCONFA_MAX,
  531. devconf_ipv6_policy, extack);
  532. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
  533. tb, NETCONFA_MAX,
  534. devconf_ipv6_policy, extack);
  535. if (err)
  536. return err;
  537. for (i = 0; i <= NETCONFA_MAX; i++) {
  538. if (!tb[i])
  539. continue;
  540. switch (i) {
  541. case NETCONFA_IFINDEX:
  542. break;
  543. default:
  544. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
  545. return -EINVAL;
  546. }
  547. }
  548. return 0;
  549. }
  550. static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
  551. struct nlmsghdr *nlh,
  552. struct netlink_ext_ack *extack)
  553. {
  554. struct net *net = sock_net(in_skb->sk);
  555. struct nlattr *tb[NETCONFA_MAX+1];
  556. struct inet6_dev *in6_dev = NULL;
  557. struct net_device *dev = NULL;
  558. struct sk_buff *skb;
  559. struct ipv6_devconf *devconf;
  560. int ifindex;
  561. int err;
  562. err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
  563. if (err < 0)
  564. return err;
  565. if (!tb[NETCONFA_IFINDEX])
  566. return -EINVAL;
  567. err = -EINVAL;
  568. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  569. switch (ifindex) {
  570. case NETCONFA_IFINDEX_ALL:
  571. devconf = net->ipv6.devconf_all;
  572. break;
  573. case NETCONFA_IFINDEX_DEFAULT:
  574. devconf = net->ipv6.devconf_dflt;
  575. break;
  576. default:
  577. dev = dev_get_by_index(net, ifindex);
  578. if (!dev)
  579. return -EINVAL;
  580. in6_dev = in6_dev_get(dev);
  581. if (!in6_dev)
  582. goto errout;
  583. devconf = &in6_dev->cnf;
  584. break;
  585. }
  586. err = -ENOBUFS;
  587. skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  588. if (!skb)
  589. goto errout;
  590. err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
  591. NETLINK_CB(in_skb).portid,
  592. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  593. NETCONFA_ALL);
  594. if (err < 0) {
  595. /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
  596. WARN_ON(err == -EMSGSIZE);
  597. kfree_skb(skb);
  598. goto errout;
  599. }
  600. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  601. errout:
  602. if (in6_dev)
  603. in6_dev_put(in6_dev);
  604. if (dev)
  605. dev_put(dev);
  606. return err;
  607. }
  608. static int inet6_netconf_dump_devconf(struct sk_buff *skb,
  609. struct netlink_callback *cb)
  610. {
  611. const struct nlmsghdr *nlh = cb->nlh;
  612. struct net *net = sock_net(skb->sk);
  613. int h, s_h;
  614. int idx, s_idx;
  615. struct net_device *dev;
  616. struct inet6_dev *idev;
  617. struct hlist_head *head;
  618. if (cb->strict_check) {
  619. struct netlink_ext_ack *extack = cb->extack;
  620. struct netconfmsg *ncm;
  621. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  622. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
  623. return -EINVAL;
  624. }
  625. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  626. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
  627. return -EINVAL;
  628. }
  629. }
  630. s_h = cb->args[0];
  631. s_idx = idx = cb->args[1];
  632. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  633. idx = 0;
  634. head = &net->dev_index_head[h];
  635. rcu_read_lock();
  636. cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
  637. net->dev_base_seq;
  638. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  639. if (idx < s_idx)
  640. goto cont;
  641. idev = __in6_dev_get(dev);
  642. if (!idev)
  643. goto cont;
  644. if (inet6_netconf_fill_devconf(skb, dev->ifindex,
  645. &idev->cnf,
  646. NETLINK_CB(cb->skb).portid,
  647. nlh->nlmsg_seq,
  648. RTM_NEWNETCONF,
  649. NLM_F_MULTI,
  650. NETCONFA_ALL) < 0) {
  651. rcu_read_unlock();
  652. goto done;
  653. }
  654. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  655. cont:
  656. idx++;
  657. }
  658. rcu_read_unlock();
  659. }
  660. if (h == NETDEV_HASHENTRIES) {
  661. if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
  662. net->ipv6.devconf_all,
  663. NETLINK_CB(cb->skb).portid,
  664. nlh->nlmsg_seq,
  665. RTM_NEWNETCONF, NLM_F_MULTI,
  666. NETCONFA_ALL) < 0)
  667. goto done;
  668. else
  669. h++;
  670. }
  671. if (h == NETDEV_HASHENTRIES + 1) {
  672. if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
  673. net->ipv6.devconf_dflt,
  674. NETLINK_CB(cb->skb).portid,
  675. nlh->nlmsg_seq,
  676. RTM_NEWNETCONF, NLM_F_MULTI,
  677. NETCONFA_ALL) < 0)
  678. goto done;
  679. else
  680. h++;
  681. }
  682. done:
  683. cb->args[0] = h;
  684. cb->args[1] = idx;
  685. return skb->len;
  686. }
  687. #ifdef CONFIG_SYSCTL
  688. static void dev_forward_change(struct inet6_dev *idev)
  689. {
  690. struct net_device *dev;
  691. struct inet6_ifaddr *ifa;
  692. if (!idev)
  693. return;
  694. dev = idev->dev;
  695. if (idev->cnf.forwarding)
  696. dev_disable_lro(dev);
  697. if (dev->flags & IFF_MULTICAST) {
  698. if (idev->cnf.forwarding) {
  699. ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
  700. ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
  701. ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
  702. } else {
  703. ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
  704. ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
  705. ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
  706. }
  707. }
  708. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  709. if (ifa->flags&IFA_F_TENTATIVE)
  710. continue;
  711. if (idev->cnf.forwarding)
  712. addrconf_join_anycast(ifa);
  713. else
  714. addrconf_leave_anycast(ifa);
  715. }
  716. inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
  717. NETCONFA_FORWARDING,
  718. dev->ifindex, &idev->cnf);
  719. }
  720. static void addrconf_forward_change(struct net *net, __s32 newf)
  721. {
  722. struct net_device *dev;
  723. struct inet6_dev *idev;
  724. for_each_netdev(net, dev) {
  725. idev = __in6_dev_get(dev);
  726. if (idev) {
  727. int changed = (!idev->cnf.forwarding) ^ (!newf);
  728. idev->cnf.forwarding = newf;
  729. if (changed)
  730. dev_forward_change(idev);
  731. }
  732. }
  733. }
  734. static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
  735. {
  736. struct net *net;
  737. int old;
  738. if (!rtnl_trylock())
  739. return restart_syscall();
  740. net = (struct net *)table->extra2;
  741. old = *p;
  742. *p = newf;
  743. if (p == &net->ipv6.devconf_dflt->forwarding) {
  744. if ((!newf) ^ (!old))
  745. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  746. NETCONFA_FORWARDING,
  747. NETCONFA_IFINDEX_DEFAULT,
  748. net->ipv6.devconf_dflt);
  749. rtnl_unlock();
  750. return 0;
  751. }
  752. if (p == &net->ipv6.devconf_all->forwarding) {
  753. int old_dflt = net->ipv6.devconf_dflt->forwarding;
  754. net->ipv6.devconf_dflt->forwarding = newf;
  755. if ((!newf) ^ (!old_dflt))
  756. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  757. NETCONFA_FORWARDING,
  758. NETCONFA_IFINDEX_DEFAULT,
  759. net->ipv6.devconf_dflt);
  760. addrconf_forward_change(net, newf);
  761. if ((!newf) ^ (!old))
  762. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  763. NETCONFA_FORWARDING,
  764. NETCONFA_IFINDEX_ALL,
  765. net->ipv6.devconf_all);
  766. } else if ((!newf) ^ (!old))
  767. dev_forward_change((struct inet6_dev *)table->extra1);
  768. rtnl_unlock();
  769. if (newf)
  770. rt6_purge_dflt_routers(net);
  771. return 1;
  772. }
  773. static void addrconf_linkdown_change(struct net *net, __s32 newf)
  774. {
  775. struct net_device *dev;
  776. struct inet6_dev *idev;
  777. for_each_netdev(net, dev) {
  778. idev = __in6_dev_get(dev);
  779. if (idev) {
  780. int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
  781. idev->cnf.ignore_routes_with_linkdown = newf;
  782. if (changed)
  783. inet6_netconf_notify_devconf(dev_net(dev),
  784. RTM_NEWNETCONF,
  785. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  786. dev->ifindex,
  787. &idev->cnf);
  788. }
  789. }
  790. }
  791. static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
  792. {
  793. struct net *net;
  794. int old;
  795. if (!rtnl_trylock())
  796. return restart_syscall();
  797. net = (struct net *)table->extra2;
  798. old = *p;
  799. *p = newf;
  800. if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
  801. if ((!newf) ^ (!old))
  802. inet6_netconf_notify_devconf(net,
  803. RTM_NEWNETCONF,
  804. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  805. NETCONFA_IFINDEX_DEFAULT,
  806. net->ipv6.devconf_dflt);
  807. rtnl_unlock();
  808. return 0;
  809. }
  810. if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
  811. net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
  812. addrconf_linkdown_change(net, newf);
  813. if ((!newf) ^ (!old))
  814. inet6_netconf_notify_devconf(net,
  815. RTM_NEWNETCONF,
  816. NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
  817. NETCONFA_IFINDEX_ALL,
  818. net->ipv6.devconf_all);
  819. }
  820. rtnl_unlock();
  821. return 1;
  822. }
  823. #endif
  824. /* Nobody refers to this ifaddr, destroy it */
  825. void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
  826. {
  827. WARN_ON(!hlist_unhashed(&ifp->addr_lst));
  828. #ifdef NET_REFCNT_DEBUG
  829. pr_debug("%s\n", __func__);
  830. #endif
  831. in6_dev_put(ifp->idev);
  832. if (cancel_delayed_work(&ifp->dad_work))
  833. pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
  834. ifp);
  835. if (ifp->state != INET6_IFADDR_STATE_DEAD) {
  836. pr_warn("Freeing alive inet6 address %p\n", ifp);
  837. return;
  838. }
  839. kfree_rcu(ifp, rcu);
  840. }
  841. static void
  842. ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
  843. {
  844. struct list_head *p;
  845. int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
  846. /*
  847. * Each device address list is sorted in order of scope -
  848. * global before linklocal.
  849. */
  850. list_for_each(p, &idev->addr_list) {
  851. struct inet6_ifaddr *ifa
  852. = list_entry(p, struct inet6_ifaddr, if_list);
  853. if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
  854. break;
  855. }
  856. list_add_tail_rcu(&ifp->if_list, p);
  857. }
  858. static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
  859. {
  860. u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
  861. return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
  862. }
  863. static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
  864. struct net_device *dev, unsigned int hash)
  865. {
  866. struct inet6_ifaddr *ifp;
  867. hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
  868. if (!net_eq(dev_net(ifp->idev->dev), net))
  869. continue;
  870. if (ipv6_addr_equal(&ifp->addr, addr)) {
  871. if (!dev || ifp->idev->dev == dev)
  872. return true;
  873. }
  874. }
  875. return false;
  876. }
  877. static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
  878. {
  879. unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
  880. int err = 0;
  881. spin_lock(&addrconf_hash_lock);
  882. /* Ignore adding duplicate addresses on an interface */
  883. if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
  884. netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
  885. err = -EEXIST;
  886. } else {
  887. hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
  888. }
  889. spin_unlock(&addrconf_hash_lock);
  890. return err;
  891. }
  892. /* On success it returns ifp with increased reference count */
  893. static struct inet6_ifaddr *
  894. ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
  895. bool can_block, struct netlink_ext_ack *extack)
  896. {
  897. gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
  898. int addr_type = ipv6_addr_type(cfg->pfx);
  899. struct net *net = dev_net(idev->dev);
  900. struct inet6_ifaddr *ifa = NULL;
  901. struct fib6_info *f6i = NULL;
  902. int err = 0;
  903. if (addr_type == IPV6_ADDR_ANY ||
  904. (addr_type & IPV6_ADDR_MULTICAST &&
  905. !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
  906. (!(idev->dev->flags & IFF_LOOPBACK) &&
  907. !netif_is_l3_master(idev->dev) &&
  908. addr_type & IPV6_ADDR_LOOPBACK))
  909. return ERR_PTR(-EADDRNOTAVAIL);
  910. if (idev->dead) {
  911. err = -ENODEV; /*XXX*/
  912. goto out;
  913. }
  914. if (idev->cnf.disable_ipv6) {
  915. err = -EACCES;
  916. goto out;
  917. }
  918. /* validator notifier needs to be blocking;
  919. * do not call in atomic context
  920. */
  921. if (can_block) {
  922. struct in6_validator_info i6vi = {
  923. .i6vi_addr = *cfg->pfx,
  924. .i6vi_dev = idev,
  925. .extack = extack,
  926. };
  927. err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
  928. err = notifier_to_errno(err);
  929. if (err < 0)
  930. goto out;
  931. }
  932. ifa = kzalloc(sizeof(*ifa), gfp_flags);
  933. if (!ifa) {
  934. err = -ENOBUFS;
  935. goto out;
  936. }
  937. f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
  938. if (IS_ERR(f6i)) {
  939. err = PTR_ERR(f6i);
  940. f6i = NULL;
  941. goto out;
  942. }
  943. if (net->ipv6.devconf_all->disable_policy ||
  944. idev->cnf.disable_policy)
  945. f6i->dst_nopolicy = true;
  946. neigh_parms_data_state_setall(idev->nd_parms);
  947. ifa->addr = *cfg->pfx;
  948. if (cfg->peer_pfx)
  949. ifa->peer_addr = *cfg->peer_pfx;
  950. spin_lock_init(&ifa->lock);
  951. INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
  952. INIT_HLIST_NODE(&ifa->addr_lst);
  953. ifa->scope = cfg->scope;
  954. ifa->prefix_len = cfg->plen;
  955. ifa->rt_priority = cfg->rt_priority;
  956. ifa->flags = cfg->ifa_flags;
  957. /* No need to add the TENTATIVE flag for addresses with NODAD */
  958. if (!(cfg->ifa_flags & IFA_F_NODAD))
  959. ifa->flags |= IFA_F_TENTATIVE;
  960. ifa->valid_lft = cfg->valid_lft;
  961. ifa->prefered_lft = cfg->preferred_lft;
  962. ifa->cstamp = ifa->tstamp = jiffies;
  963. ifa->tokenized = false;
  964. ifa->rt = f6i;
  965. ifa->idev = idev;
  966. in6_dev_hold(idev);
  967. /* For caller */
  968. refcount_set(&ifa->refcnt, 1);
  969. rcu_read_lock_bh();
  970. err = ipv6_add_addr_hash(idev->dev, ifa);
  971. if (err < 0) {
  972. rcu_read_unlock_bh();
  973. goto out;
  974. }
  975. write_lock(&idev->lock);
  976. /* Add to inet6_dev unicast addr list. */
  977. ipv6_link_dev_addr(idev, ifa);
  978. if (ifa->flags&IFA_F_TEMPORARY) {
  979. list_add(&ifa->tmp_list, &idev->tempaddr_list);
  980. in6_ifa_hold(ifa);
  981. }
  982. in6_ifa_hold(ifa);
  983. write_unlock(&idev->lock);
  984. rcu_read_unlock_bh();
  985. inet6addr_notifier_call_chain(NETDEV_UP, ifa);
  986. out:
  987. if (unlikely(err < 0)) {
  988. fib6_info_release(f6i);
  989. if (ifa) {
  990. if (ifa->idev)
  991. in6_dev_put(ifa->idev);
  992. kfree(ifa);
  993. }
  994. ifa = ERR_PTR(err);
  995. }
  996. return ifa;
  997. }
  998. enum cleanup_prefix_rt_t {
  999. CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
  1000. CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
  1001. CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
  1002. };
  1003. /*
  1004. * Check, whether the prefix for ifp would still need a prefix route
  1005. * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
  1006. * constants.
  1007. *
  1008. * 1) we don't purge prefix if address was not permanent.
  1009. * prefix is managed by its own lifetime.
  1010. * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
  1011. * 3) if there are no addresses, delete prefix.
  1012. * 4) if there are still other permanent address(es),
  1013. * corresponding prefix is still permanent.
  1014. * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
  1015. * don't purge the prefix, assume user space is managing it.
  1016. * 6) otherwise, update prefix lifetime to the
  1017. * longest valid lifetime among the corresponding
  1018. * addresses on the device.
  1019. * Note: subsequent RA will update lifetime.
  1020. **/
  1021. static enum cleanup_prefix_rt_t
  1022. check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
  1023. {
  1024. struct inet6_ifaddr *ifa;
  1025. struct inet6_dev *idev = ifp->idev;
  1026. unsigned long lifetime;
  1027. enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
  1028. *expires = jiffies;
  1029. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  1030. if (ifa == ifp)
  1031. continue;
  1032. if (ifa->prefix_len != ifp->prefix_len ||
  1033. !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
  1034. ifp->prefix_len))
  1035. continue;
  1036. if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
  1037. return CLEANUP_PREFIX_RT_NOP;
  1038. action = CLEANUP_PREFIX_RT_EXPIRE;
  1039. spin_lock(&ifa->lock);
  1040. lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
  1041. /*
  1042. * Note: Because this address is
  1043. * not permanent, lifetime <
  1044. * LONG_MAX / HZ here.
  1045. */
  1046. if (time_before(*expires, ifa->tstamp + lifetime * HZ))
  1047. *expires = ifa->tstamp + lifetime * HZ;
  1048. spin_unlock(&ifa->lock);
  1049. }
  1050. return action;
  1051. }
  1052. static void
  1053. cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
  1054. bool del_rt, bool del_peer)
  1055. {
  1056. struct fib6_info *f6i;
  1057. f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
  1058. ifp->prefix_len,
  1059. ifp->idev->dev, 0, RTF_DEFAULT, true);
  1060. if (f6i) {
  1061. if (del_rt)
  1062. ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
  1063. else {
  1064. if (!(f6i->fib6_flags & RTF_EXPIRES))
  1065. fib6_set_expires(f6i, expires);
  1066. fib6_info_release(f6i);
  1067. }
  1068. }
  1069. }
  1070. /* This function wants to get referenced ifp and releases it before return */
  1071. static void ipv6_del_addr(struct inet6_ifaddr *ifp)
  1072. {
  1073. int state;
  1074. enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
  1075. unsigned long expires;
  1076. ASSERT_RTNL();
  1077. spin_lock_bh(&ifp->lock);
  1078. state = ifp->state;
  1079. ifp->state = INET6_IFADDR_STATE_DEAD;
  1080. spin_unlock_bh(&ifp->lock);
  1081. if (state == INET6_IFADDR_STATE_DEAD)
  1082. goto out;
  1083. spin_lock_bh(&addrconf_hash_lock);
  1084. hlist_del_init_rcu(&ifp->addr_lst);
  1085. spin_unlock_bh(&addrconf_hash_lock);
  1086. write_lock_bh(&ifp->idev->lock);
  1087. if (ifp->flags&IFA_F_TEMPORARY) {
  1088. list_del(&ifp->tmp_list);
  1089. if (ifp->ifpub) {
  1090. in6_ifa_put(ifp->ifpub);
  1091. ifp->ifpub = NULL;
  1092. }
  1093. __in6_ifa_put(ifp);
  1094. }
  1095. if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
  1096. action = check_cleanup_prefix_route(ifp, &expires);
  1097. list_del_rcu(&ifp->if_list);
  1098. __in6_ifa_put(ifp);
  1099. write_unlock_bh(&ifp->idev->lock);
  1100. addrconf_del_dad_work(ifp);
  1101. ipv6_ifa_notify(RTM_DELADDR, ifp);
  1102. inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
  1103. if (action != CLEANUP_PREFIX_RT_NOP) {
  1104. cleanup_prefix_route(ifp, expires,
  1105. action == CLEANUP_PREFIX_RT_DEL, false);
  1106. }
  1107. /* clean up prefsrc entries */
  1108. rt6_remove_prefsrc(ifp);
  1109. out:
  1110. in6_ifa_put(ifp);
  1111. }
  1112. static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
  1113. {
  1114. struct inet6_dev *idev = ifp->idev;
  1115. unsigned long tmp_tstamp, age;
  1116. unsigned long regen_advance;
  1117. unsigned long now = jiffies;
  1118. s32 cnf_temp_preferred_lft;
  1119. struct inet6_ifaddr *ift;
  1120. struct ifa6_config cfg;
  1121. long max_desync_factor;
  1122. struct in6_addr addr;
  1123. int ret = 0;
  1124. write_lock_bh(&idev->lock);
  1125. retry:
  1126. in6_dev_hold(idev);
  1127. if (idev->cnf.use_tempaddr <= 0) {
  1128. write_unlock_bh(&idev->lock);
  1129. pr_info("%s: use_tempaddr is disabled\n", __func__);
  1130. in6_dev_put(idev);
  1131. ret = -1;
  1132. goto out;
  1133. }
  1134. spin_lock_bh(&ifp->lock);
  1135. if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
  1136. idev->cnf.use_tempaddr = -1; /*XXX*/
  1137. spin_unlock_bh(&ifp->lock);
  1138. write_unlock_bh(&idev->lock);
  1139. pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
  1140. __func__);
  1141. in6_dev_put(idev);
  1142. ret = -1;
  1143. goto out;
  1144. }
  1145. in6_ifa_hold(ifp);
  1146. memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
  1147. ipv6_gen_rnd_iid(&addr);
  1148. age = (now - ifp->tstamp) / HZ;
  1149. regen_advance = idev->cnf.regen_max_retry *
  1150. idev->cnf.dad_transmits *
  1151. max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
  1152. /* recalculate max_desync_factor each time and update
  1153. * idev->desync_factor if it's larger
  1154. */
  1155. cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
  1156. max_desync_factor = min_t(__u32,
  1157. idev->cnf.max_desync_factor,
  1158. cnf_temp_preferred_lft - regen_advance);
  1159. if (unlikely(idev->desync_factor > max_desync_factor)) {
  1160. if (max_desync_factor > 0) {
  1161. get_random_bytes(&idev->desync_factor,
  1162. sizeof(idev->desync_factor));
  1163. idev->desync_factor %= max_desync_factor;
  1164. } else {
  1165. idev->desync_factor = 0;
  1166. }
  1167. }
  1168. memset(&cfg, 0, sizeof(cfg));
  1169. cfg.valid_lft = min_t(__u32, ifp->valid_lft,
  1170. idev->cnf.temp_valid_lft + age);
  1171. cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
  1172. cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
  1173. cfg.plen = ifp->prefix_len;
  1174. tmp_tstamp = ifp->tstamp;
  1175. spin_unlock_bh(&ifp->lock);
  1176. write_unlock_bh(&idev->lock);
  1177. /* A temporary address is created only if this calculated Preferred
  1178. * Lifetime is greater than REGEN_ADVANCE time units. In particular,
  1179. * an implementation must not create a temporary address with a zero
  1180. * Preferred Lifetime.
  1181. * Use age calculation as in addrconf_verify to avoid unnecessary
  1182. * temporary addresses being generated.
  1183. */
  1184. age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  1185. if (cfg.preferred_lft <= regen_advance + age) {
  1186. in6_ifa_put(ifp);
  1187. in6_dev_put(idev);
  1188. ret = -1;
  1189. goto out;
  1190. }
  1191. cfg.ifa_flags = IFA_F_TEMPORARY;
  1192. /* set in addrconf_prefix_rcv() */
  1193. if (ifp->flags & IFA_F_OPTIMISTIC)
  1194. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  1195. cfg.pfx = &addr;
  1196. cfg.scope = ipv6_addr_scope(cfg.pfx);
  1197. ift = ipv6_add_addr(idev, &cfg, block, NULL);
  1198. if (IS_ERR(ift)) {
  1199. in6_ifa_put(ifp);
  1200. in6_dev_put(idev);
  1201. pr_info("%s: retry temporary address regeneration\n", __func__);
  1202. write_lock_bh(&idev->lock);
  1203. goto retry;
  1204. }
  1205. spin_lock_bh(&ift->lock);
  1206. ift->ifpub = ifp;
  1207. ift->cstamp = now;
  1208. ift->tstamp = tmp_tstamp;
  1209. spin_unlock_bh(&ift->lock);
  1210. addrconf_dad_start(ift);
  1211. in6_ifa_put(ift);
  1212. in6_dev_put(idev);
  1213. out:
  1214. return ret;
  1215. }
  1216. /*
  1217. * Choose an appropriate source address (RFC3484)
  1218. */
  1219. enum {
  1220. IPV6_SADDR_RULE_INIT = 0,
  1221. IPV6_SADDR_RULE_LOCAL,
  1222. IPV6_SADDR_RULE_SCOPE,
  1223. IPV6_SADDR_RULE_PREFERRED,
  1224. #ifdef CONFIG_IPV6_MIP6
  1225. IPV6_SADDR_RULE_HOA,
  1226. #endif
  1227. IPV6_SADDR_RULE_OIF,
  1228. IPV6_SADDR_RULE_LABEL,
  1229. IPV6_SADDR_RULE_PRIVACY,
  1230. IPV6_SADDR_RULE_ORCHID,
  1231. IPV6_SADDR_RULE_PREFIX,
  1232. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1233. IPV6_SADDR_RULE_NOT_OPTIMISTIC,
  1234. #endif
  1235. IPV6_SADDR_RULE_MAX
  1236. };
  1237. struct ipv6_saddr_score {
  1238. int rule;
  1239. int addr_type;
  1240. struct inet6_ifaddr *ifa;
  1241. DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
  1242. int scopedist;
  1243. int matchlen;
  1244. };
  1245. struct ipv6_saddr_dst {
  1246. const struct in6_addr *addr;
  1247. int ifindex;
  1248. int scope;
  1249. int label;
  1250. unsigned int prefs;
  1251. };
  1252. static inline int ipv6_saddr_preferred(int type)
  1253. {
  1254. if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
  1255. return 1;
  1256. return 0;
  1257. }
  1258. static bool ipv6_use_optimistic_addr(struct net *net,
  1259. struct inet6_dev *idev)
  1260. {
  1261. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1262. if (!idev)
  1263. return false;
  1264. if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
  1265. return false;
  1266. if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
  1267. return false;
  1268. return true;
  1269. #else
  1270. return false;
  1271. #endif
  1272. }
  1273. static bool ipv6_allow_optimistic_dad(struct net *net,
  1274. struct inet6_dev *idev)
  1275. {
  1276. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1277. if (!idev)
  1278. return false;
  1279. if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
  1280. return false;
  1281. return true;
  1282. #else
  1283. return false;
  1284. #endif
  1285. }
  1286. static int ipv6_get_saddr_eval(struct net *net,
  1287. struct ipv6_saddr_score *score,
  1288. struct ipv6_saddr_dst *dst,
  1289. int i)
  1290. {
  1291. int ret;
  1292. if (i <= score->rule) {
  1293. switch (i) {
  1294. case IPV6_SADDR_RULE_SCOPE:
  1295. ret = score->scopedist;
  1296. break;
  1297. case IPV6_SADDR_RULE_PREFIX:
  1298. ret = score->matchlen;
  1299. break;
  1300. default:
  1301. ret = !!test_bit(i, score->scorebits);
  1302. }
  1303. goto out;
  1304. }
  1305. switch (i) {
  1306. case IPV6_SADDR_RULE_INIT:
  1307. /* Rule 0: remember if hiscore is not ready yet */
  1308. ret = !!score->ifa;
  1309. break;
  1310. case IPV6_SADDR_RULE_LOCAL:
  1311. /* Rule 1: Prefer same address */
  1312. ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
  1313. break;
  1314. case IPV6_SADDR_RULE_SCOPE:
  1315. /* Rule 2: Prefer appropriate scope
  1316. *
  1317. * ret
  1318. * ^
  1319. * -1 | d 15
  1320. * ---+--+-+---> scope
  1321. * |
  1322. * | d is scope of the destination.
  1323. * B-d | \
  1324. * | \ <- smaller scope is better if
  1325. * B-15 | \ if scope is enough for destination.
  1326. * | ret = B - scope (-1 <= scope >= d <= 15).
  1327. * d-C-1 | /
  1328. * |/ <- greater is better
  1329. * -C / if scope is not enough for destination.
  1330. * /| ret = scope - C (-1 <= d < scope <= 15).
  1331. *
  1332. * d - C - 1 < B -15 (for all -1 <= d <= 15).
  1333. * C > d + 14 - B >= 15 + 14 - B = 29 - B.
  1334. * Assume B = 0 and we get C > 29.
  1335. */
  1336. ret = __ipv6_addr_src_scope(score->addr_type);
  1337. if (ret >= dst->scope)
  1338. ret = -ret;
  1339. else
  1340. ret -= 128; /* 30 is enough */
  1341. score->scopedist = ret;
  1342. break;
  1343. case IPV6_SADDR_RULE_PREFERRED:
  1344. {
  1345. /* Rule 3: Avoid deprecated and optimistic addresses */
  1346. u8 avoid = IFA_F_DEPRECATED;
  1347. if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
  1348. avoid |= IFA_F_OPTIMISTIC;
  1349. ret = ipv6_saddr_preferred(score->addr_type) ||
  1350. !(score->ifa->flags & avoid);
  1351. break;
  1352. }
  1353. #ifdef CONFIG_IPV6_MIP6
  1354. case IPV6_SADDR_RULE_HOA:
  1355. {
  1356. /* Rule 4: Prefer home address */
  1357. int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
  1358. ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
  1359. break;
  1360. }
  1361. #endif
  1362. case IPV6_SADDR_RULE_OIF:
  1363. /* Rule 5: Prefer outgoing interface */
  1364. ret = (!dst->ifindex ||
  1365. dst->ifindex == score->ifa->idev->dev->ifindex);
  1366. break;
  1367. case IPV6_SADDR_RULE_LABEL:
  1368. /* Rule 6: Prefer matching label */
  1369. ret = ipv6_addr_label(net,
  1370. &score->ifa->addr, score->addr_type,
  1371. score->ifa->idev->dev->ifindex) == dst->label;
  1372. break;
  1373. case IPV6_SADDR_RULE_PRIVACY:
  1374. {
  1375. /* Rule 7: Prefer public address
  1376. * Note: prefer temporary address if use_tempaddr >= 2
  1377. */
  1378. int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
  1379. !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
  1380. score->ifa->idev->cnf.use_tempaddr >= 2;
  1381. ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
  1382. break;
  1383. }
  1384. case IPV6_SADDR_RULE_ORCHID:
  1385. /* Rule 8-: Prefer ORCHID vs ORCHID or
  1386. * non-ORCHID vs non-ORCHID
  1387. */
  1388. ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
  1389. ipv6_addr_orchid(dst->addr));
  1390. break;
  1391. case IPV6_SADDR_RULE_PREFIX:
  1392. /* Rule 8: Use longest matching prefix */
  1393. ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
  1394. if (ret > score->ifa->prefix_len)
  1395. ret = score->ifa->prefix_len;
  1396. score->matchlen = ret;
  1397. break;
  1398. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  1399. case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
  1400. /* Optimistic addresses still have lower precedence than other
  1401. * preferred addresses.
  1402. */
  1403. ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
  1404. break;
  1405. #endif
  1406. default:
  1407. ret = 0;
  1408. }
  1409. if (ret)
  1410. __set_bit(i, score->scorebits);
  1411. score->rule = i;
  1412. out:
  1413. return ret;
  1414. }
  1415. static int __ipv6_dev_get_saddr(struct net *net,
  1416. struct ipv6_saddr_dst *dst,
  1417. struct inet6_dev *idev,
  1418. struct ipv6_saddr_score *scores,
  1419. int hiscore_idx)
  1420. {
  1421. struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
  1422. list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
  1423. int i;
  1424. /*
  1425. * - Tentative Address (RFC2462 section 5.4)
  1426. * - A tentative address is not considered
  1427. * "assigned to an interface" in the traditional
  1428. * sense, unless it is also flagged as optimistic.
  1429. * - Candidate Source Address (section 4)
  1430. * - In any case, anycast addresses, multicast
  1431. * addresses, and the unspecified address MUST
  1432. * NOT be included in a candidate set.
  1433. */
  1434. if ((score->ifa->flags & IFA_F_TENTATIVE) &&
  1435. (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
  1436. continue;
  1437. score->addr_type = __ipv6_addr_type(&score->ifa->addr);
  1438. if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
  1439. score->addr_type & IPV6_ADDR_MULTICAST)) {
  1440. net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
  1441. idev->dev->name);
  1442. continue;
  1443. }
  1444. score->rule = -1;
  1445. bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
  1446. for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
  1447. int minihiscore, miniscore;
  1448. minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
  1449. miniscore = ipv6_get_saddr_eval(net, score, dst, i);
  1450. if (minihiscore > miniscore) {
  1451. if (i == IPV6_SADDR_RULE_SCOPE &&
  1452. score->scopedist > 0) {
  1453. /*
  1454. * special case:
  1455. * each remaining entry
  1456. * has too small (not enough)
  1457. * scope, because ifa entries
  1458. * are sorted by their scope
  1459. * values.
  1460. */
  1461. goto out;
  1462. }
  1463. break;
  1464. } else if (minihiscore < miniscore) {
  1465. swap(hiscore, score);
  1466. hiscore_idx = 1 - hiscore_idx;
  1467. /* restore our iterator */
  1468. score->ifa = hiscore->ifa;
  1469. break;
  1470. }
  1471. }
  1472. }
  1473. out:
  1474. return hiscore_idx;
  1475. }
  1476. static int ipv6_get_saddr_master(struct net *net,
  1477. const struct net_device *dst_dev,
  1478. const struct net_device *master,
  1479. struct ipv6_saddr_dst *dst,
  1480. struct ipv6_saddr_score *scores,
  1481. int hiscore_idx)
  1482. {
  1483. struct inet6_dev *idev;
  1484. idev = __in6_dev_get(dst_dev);
  1485. if (idev)
  1486. hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
  1487. scores, hiscore_idx);
  1488. idev = __in6_dev_get(master);
  1489. if (idev)
  1490. hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
  1491. scores, hiscore_idx);
  1492. return hiscore_idx;
  1493. }
  1494. int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
  1495. const struct in6_addr *daddr, unsigned int prefs,
  1496. struct in6_addr *saddr)
  1497. {
  1498. struct ipv6_saddr_score scores[2], *hiscore;
  1499. struct ipv6_saddr_dst dst;
  1500. struct inet6_dev *idev;
  1501. struct net_device *dev;
  1502. int dst_type;
  1503. bool use_oif_addr = false;
  1504. int hiscore_idx = 0;
  1505. int ret = 0;
  1506. dst_type = __ipv6_addr_type(daddr);
  1507. dst.addr = daddr;
  1508. dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
  1509. dst.scope = __ipv6_addr_src_scope(dst_type);
  1510. dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
  1511. dst.prefs = prefs;
  1512. scores[hiscore_idx].rule = -1;
  1513. scores[hiscore_idx].ifa = NULL;
  1514. rcu_read_lock();
  1515. /* Candidate Source Address (section 4)
  1516. * - multicast and link-local destination address,
  1517. * the set of candidate source address MUST only
  1518. * include addresses assigned to interfaces
  1519. * belonging to the same link as the outgoing
  1520. * interface.
  1521. * (- For site-local destination addresses, the
  1522. * set of candidate source addresses MUST only
  1523. * include addresses assigned to interfaces
  1524. * belonging to the same site as the outgoing
  1525. * interface.)
  1526. * - "It is RECOMMENDED that the candidate source addresses
  1527. * be the set of unicast addresses assigned to the
  1528. * interface that will be used to send to the destination
  1529. * (the 'outgoing' interface)." (RFC 6724)
  1530. */
  1531. if (dst_dev) {
  1532. idev = __in6_dev_get(dst_dev);
  1533. if ((dst_type & IPV6_ADDR_MULTICAST) ||
  1534. dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
  1535. (idev && idev->cnf.use_oif_addrs_only)) {
  1536. use_oif_addr = true;
  1537. }
  1538. }
  1539. if (use_oif_addr) {
  1540. if (idev)
  1541. hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
  1542. } else {
  1543. const struct net_device *master;
  1544. int master_idx = 0;
  1545. /* if dst_dev exists and is enslaved to an L3 device, then
  1546. * prefer addresses from dst_dev and then the master over
  1547. * any other enslaved devices in the L3 domain.
  1548. */
  1549. master = l3mdev_master_dev_rcu(dst_dev);
  1550. if (master) {
  1551. master_idx = master->ifindex;
  1552. hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
  1553. master, &dst,
  1554. scores, hiscore_idx);
  1555. if (scores[hiscore_idx].ifa)
  1556. goto out;
  1557. }
  1558. for_each_netdev_rcu(net, dev) {
  1559. /* only consider addresses on devices in the
  1560. * same L3 domain
  1561. */
  1562. if (l3mdev_master_ifindex_rcu(dev) != master_idx)
  1563. continue;
  1564. idev = __in6_dev_get(dev);
  1565. if (!idev)
  1566. continue;
  1567. hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
  1568. }
  1569. }
  1570. out:
  1571. hiscore = &scores[hiscore_idx];
  1572. if (!hiscore->ifa)
  1573. ret = -EADDRNOTAVAIL;
  1574. else
  1575. *saddr = hiscore->ifa->addr;
  1576. rcu_read_unlock();
  1577. return ret;
  1578. }
  1579. EXPORT_SYMBOL(ipv6_dev_get_saddr);
  1580. int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
  1581. u32 banned_flags)
  1582. {
  1583. struct inet6_ifaddr *ifp;
  1584. int err = -EADDRNOTAVAIL;
  1585. list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
  1586. if (ifp->scope > IFA_LINK)
  1587. break;
  1588. if (ifp->scope == IFA_LINK &&
  1589. !(ifp->flags & banned_flags)) {
  1590. *addr = ifp->addr;
  1591. err = 0;
  1592. break;
  1593. }
  1594. }
  1595. return err;
  1596. }
  1597. int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
  1598. u32 banned_flags)
  1599. {
  1600. struct inet6_dev *idev;
  1601. int err = -EADDRNOTAVAIL;
  1602. rcu_read_lock();
  1603. idev = __in6_dev_get(dev);
  1604. if (idev) {
  1605. read_lock_bh(&idev->lock);
  1606. err = __ipv6_get_lladdr(idev, addr, banned_flags);
  1607. read_unlock_bh(&idev->lock);
  1608. }
  1609. rcu_read_unlock();
  1610. return err;
  1611. }
  1612. static int ipv6_count_addresses(const struct inet6_dev *idev)
  1613. {
  1614. const struct inet6_ifaddr *ifp;
  1615. int cnt = 0;
  1616. rcu_read_lock();
  1617. list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
  1618. cnt++;
  1619. rcu_read_unlock();
  1620. return cnt;
  1621. }
  1622. int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
  1623. const struct net_device *dev, int strict)
  1624. {
  1625. return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
  1626. strict, IFA_F_TENTATIVE);
  1627. }
  1628. EXPORT_SYMBOL(ipv6_chk_addr);
  1629. /* device argument is used to find the L3 domain of interest. If
  1630. * skip_dev_check is set, then the ifp device is not checked against
  1631. * the passed in dev argument. So the 2 cases for addresses checks are:
  1632. * 1. does the address exist in the L3 domain that dev is part of
  1633. * (skip_dev_check = true), or
  1634. *
  1635. * 2. does the address exist on the specific device
  1636. * (skip_dev_check = false)
  1637. */
  1638. static struct net_device *
  1639. __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
  1640. const struct net_device *dev, bool skip_dev_check,
  1641. int strict, u32 banned_flags)
  1642. {
  1643. unsigned int hash = inet6_addr_hash(net, addr);
  1644. struct net_device *l3mdev, *ndev;
  1645. struct inet6_ifaddr *ifp;
  1646. u32 ifp_flags;
  1647. rcu_read_lock();
  1648. l3mdev = l3mdev_master_dev_rcu(dev);
  1649. if (skip_dev_check)
  1650. dev = NULL;
  1651. hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
  1652. ndev = ifp->idev->dev;
  1653. if (!net_eq(dev_net(ndev), net))
  1654. continue;
  1655. if (l3mdev_master_dev_rcu(ndev) != l3mdev)
  1656. continue;
  1657. /* Decouple optimistic from tentative for evaluation here.
  1658. * Ban optimistic addresses explicitly, when required.
  1659. */
  1660. ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
  1661. ? (ifp->flags&~IFA_F_TENTATIVE)
  1662. : ifp->flags;
  1663. if (ipv6_addr_equal(&ifp->addr, addr) &&
  1664. !(ifp_flags&banned_flags) &&
  1665. (!dev || ndev == dev ||
  1666. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
  1667. rcu_read_unlock();
  1668. return ndev;
  1669. }
  1670. }
  1671. rcu_read_unlock();
  1672. return NULL;
  1673. }
  1674. int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
  1675. const struct net_device *dev, bool skip_dev_check,
  1676. int strict, u32 banned_flags)
  1677. {
  1678. return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
  1679. strict, banned_flags) ? 1 : 0;
  1680. }
  1681. EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
  1682. /* Compares an address/prefix_len with addresses on device @dev.
  1683. * If one is found it returns true.
  1684. */
  1685. bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
  1686. const unsigned int prefix_len, struct net_device *dev)
  1687. {
  1688. const struct inet6_ifaddr *ifa;
  1689. const struct inet6_dev *idev;
  1690. bool ret = false;
  1691. rcu_read_lock();
  1692. idev = __in6_dev_get(dev);
  1693. if (idev) {
  1694. list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
  1695. ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
  1696. if (ret)
  1697. break;
  1698. }
  1699. }
  1700. rcu_read_unlock();
  1701. return ret;
  1702. }
  1703. EXPORT_SYMBOL(ipv6_chk_custom_prefix);
  1704. int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
  1705. {
  1706. const struct inet6_ifaddr *ifa;
  1707. const struct inet6_dev *idev;
  1708. int onlink;
  1709. onlink = 0;
  1710. rcu_read_lock();
  1711. idev = __in6_dev_get(dev);
  1712. if (idev) {
  1713. list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
  1714. onlink = ipv6_prefix_equal(addr, &ifa->addr,
  1715. ifa->prefix_len);
  1716. if (onlink)
  1717. break;
  1718. }
  1719. }
  1720. rcu_read_unlock();
  1721. return onlink;
  1722. }
  1723. EXPORT_SYMBOL(ipv6_chk_prefix);
  1724. /**
  1725. * ipv6_dev_find - find the first device with a given source address.
  1726. * @net: the net namespace
  1727. * @addr: the source address
  1728. *
  1729. * The caller should be protected by RCU, or RTNL.
  1730. */
  1731. struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
  1732. struct net_device *dev)
  1733. {
  1734. return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
  1735. IFA_F_TENTATIVE);
  1736. }
  1737. EXPORT_SYMBOL(ipv6_dev_find);
  1738. struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
  1739. struct net_device *dev, int strict)
  1740. {
  1741. unsigned int hash = inet6_addr_hash(net, addr);
  1742. struct inet6_ifaddr *ifp, *result = NULL;
  1743. rcu_read_lock();
  1744. hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
  1745. if (!net_eq(dev_net(ifp->idev->dev), net))
  1746. continue;
  1747. if (ipv6_addr_equal(&ifp->addr, addr)) {
  1748. if (!dev || ifp->idev->dev == dev ||
  1749. !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
  1750. result = ifp;
  1751. in6_ifa_hold(ifp);
  1752. break;
  1753. }
  1754. }
  1755. }
  1756. rcu_read_unlock();
  1757. return result;
  1758. }
  1759. /* Gets referenced address, destroys ifaddr */
  1760. static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
  1761. {
  1762. if (dad_failed)
  1763. ifp->flags |= IFA_F_DADFAILED;
  1764. if (ifp->flags&IFA_F_TEMPORARY) {
  1765. struct inet6_ifaddr *ifpub;
  1766. spin_lock_bh(&ifp->lock);
  1767. ifpub = ifp->ifpub;
  1768. if (ifpub) {
  1769. in6_ifa_hold(ifpub);
  1770. spin_unlock_bh(&ifp->lock);
  1771. ipv6_create_tempaddr(ifpub, true);
  1772. in6_ifa_put(ifpub);
  1773. } else {
  1774. spin_unlock_bh(&ifp->lock);
  1775. }
  1776. ipv6_del_addr(ifp);
  1777. } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
  1778. spin_lock_bh(&ifp->lock);
  1779. addrconf_del_dad_work(ifp);
  1780. ifp->flags |= IFA_F_TENTATIVE;
  1781. if (dad_failed)
  1782. ifp->flags &= ~IFA_F_OPTIMISTIC;
  1783. spin_unlock_bh(&ifp->lock);
  1784. if (dad_failed)
  1785. ipv6_ifa_notify(0, ifp);
  1786. in6_ifa_put(ifp);
  1787. } else {
  1788. ipv6_del_addr(ifp);
  1789. }
  1790. }
  1791. static int addrconf_dad_end(struct inet6_ifaddr *ifp)
  1792. {
  1793. int err = -ENOENT;
  1794. spin_lock_bh(&ifp->lock);
  1795. if (ifp->state == INET6_IFADDR_STATE_DAD) {
  1796. ifp->state = INET6_IFADDR_STATE_POSTDAD;
  1797. err = 0;
  1798. }
  1799. spin_unlock_bh(&ifp->lock);
  1800. return err;
  1801. }
  1802. void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
  1803. {
  1804. struct inet6_dev *idev = ifp->idev;
  1805. struct net *net = dev_net(ifp->idev->dev);
  1806. if (addrconf_dad_end(ifp)) {
  1807. in6_ifa_put(ifp);
  1808. return;
  1809. }
  1810. net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
  1811. ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
  1812. spin_lock_bh(&ifp->lock);
  1813. if (ifp->flags & IFA_F_STABLE_PRIVACY) {
  1814. struct in6_addr new_addr;
  1815. struct inet6_ifaddr *ifp2;
  1816. int retries = ifp->stable_privacy_retry + 1;
  1817. struct ifa6_config cfg = {
  1818. .pfx = &new_addr,
  1819. .plen = ifp->prefix_len,
  1820. .ifa_flags = ifp->flags,
  1821. .valid_lft = ifp->valid_lft,
  1822. .preferred_lft = ifp->prefered_lft,
  1823. .scope = ifp->scope,
  1824. };
  1825. if (retries > net->ipv6.sysctl.idgen_retries) {
  1826. net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
  1827. ifp->idev->dev->name);
  1828. goto errdad;
  1829. }
  1830. new_addr = ifp->addr;
  1831. if (ipv6_generate_stable_address(&new_addr, retries,
  1832. idev))
  1833. goto errdad;
  1834. spin_unlock_bh(&ifp->lock);
  1835. if (idev->cnf.max_addresses &&
  1836. ipv6_count_addresses(idev) >=
  1837. idev->cnf.max_addresses)
  1838. goto lock_errdad;
  1839. net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
  1840. ifp->idev->dev->name);
  1841. ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
  1842. if (IS_ERR(ifp2))
  1843. goto lock_errdad;
  1844. spin_lock_bh(&ifp2->lock);
  1845. ifp2->stable_privacy_retry = retries;
  1846. ifp2->state = INET6_IFADDR_STATE_PREDAD;
  1847. spin_unlock_bh(&ifp2->lock);
  1848. addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
  1849. in6_ifa_put(ifp2);
  1850. lock_errdad:
  1851. spin_lock_bh(&ifp->lock);
  1852. }
  1853. errdad:
  1854. /* transition from _POSTDAD to _ERRDAD */
  1855. ifp->state = INET6_IFADDR_STATE_ERRDAD;
  1856. spin_unlock_bh(&ifp->lock);
  1857. addrconf_mod_dad_work(ifp, 0);
  1858. in6_ifa_put(ifp);
  1859. }
  1860. /* Join to solicited addr multicast group.
  1861. * caller must hold RTNL */
  1862. void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
  1863. {
  1864. struct in6_addr maddr;
  1865. if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1866. return;
  1867. addrconf_addr_solict_mult(addr, &maddr);
  1868. ipv6_dev_mc_inc(dev, &maddr);
  1869. }
  1870. /* caller must hold RTNL */
  1871. void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
  1872. {
  1873. struct in6_addr maddr;
  1874. if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
  1875. return;
  1876. addrconf_addr_solict_mult(addr, &maddr);
  1877. __ipv6_dev_mc_dec(idev, &maddr);
  1878. }
  1879. /* caller must hold RTNL */
  1880. static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
  1881. {
  1882. struct in6_addr addr;
  1883. if (ifp->prefix_len >= 127) /* RFC 6164 */
  1884. return;
  1885. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1886. if (ipv6_addr_any(&addr))
  1887. return;
  1888. __ipv6_dev_ac_inc(ifp->idev, &addr);
  1889. }
  1890. /* caller must hold RTNL */
  1891. static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
  1892. {
  1893. struct in6_addr addr;
  1894. if (ifp->prefix_len >= 127) /* RFC 6164 */
  1895. return;
  1896. ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
  1897. if (ipv6_addr_any(&addr))
  1898. return;
  1899. __ipv6_dev_ac_dec(ifp->idev, &addr);
  1900. }
  1901. static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
  1902. {
  1903. switch (dev->addr_len) {
  1904. case ETH_ALEN:
  1905. memcpy(eui, dev->dev_addr, 3);
  1906. eui[3] = 0xFF;
  1907. eui[4] = 0xFE;
  1908. memcpy(eui + 5, dev->dev_addr + 3, 3);
  1909. break;
  1910. case EUI64_ADDR_LEN:
  1911. memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
  1912. eui[0] ^= 2;
  1913. break;
  1914. default:
  1915. return -1;
  1916. }
  1917. return 0;
  1918. }
  1919. static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
  1920. {
  1921. union fwnet_hwaddr *ha;
  1922. if (dev->addr_len != FWNET_ALEN)
  1923. return -1;
  1924. ha = (union fwnet_hwaddr *)dev->dev_addr;
  1925. memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
  1926. eui[0] ^= 2;
  1927. return 0;
  1928. }
  1929. static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
  1930. {
  1931. /* XXX: inherit EUI-64 from other interface -- yoshfuji */
  1932. if (dev->addr_len != ARCNET_ALEN)
  1933. return -1;
  1934. memset(eui, 0, 7);
  1935. eui[7] = *(u8 *)dev->dev_addr;
  1936. return 0;
  1937. }
  1938. static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
  1939. {
  1940. if (dev->addr_len != INFINIBAND_ALEN)
  1941. return -1;
  1942. memcpy(eui, dev->dev_addr + 12, 8);
  1943. eui[0] |= 2;
  1944. return 0;
  1945. }
  1946. static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
  1947. {
  1948. if (addr == 0)
  1949. return -1;
  1950. eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
  1951. ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
  1952. ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
  1953. ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
  1954. ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
  1955. ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
  1956. eui[1] = 0;
  1957. eui[2] = 0x5E;
  1958. eui[3] = 0xFE;
  1959. memcpy(eui + 4, &addr, 4);
  1960. return 0;
  1961. }
  1962. static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
  1963. {
  1964. if (dev->priv_flags & IFF_ISATAP)
  1965. return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
  1966. return -1;
  1967. }
  1968. static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
  1969. {
  1970. return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
  1971. }
  1972. static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
  1973. {
  1974. memcpy(eui, dev->perm_addr, 3);
  1975. memcpy(eui + 5, dev->perm_addr + 3, 3);
  1976. eui[3] = 0xFF;
  1977. eui[4] = 0xFE;
  1978. eui[0] ^= 2;
  1979. return 0;
  1980. }
  1981. static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
  1982. {
  1983. switch (dev->type) {
  1984. case ARPHRD_ETHER:
  1985. case ARPHRD_FDDI:
  1986. return addrconf_ifid_eui48(eui, dev);
  1987. case ARPHRD_ARCNET:
  1988. return addrconf_ifid_arcnet(eui, dev);
  1989. case ARPHRD_INFINIBAND:
  1990. return addrconf_ifid_infiniband(eui, dev);
  1991. case ARPHRD_SIT:
  1992. return addrconf_ifid_sit(eui, dev);
  1993. case ARPHRD_IPGRE:
  1994. case ARPHRD_TUNNEL:
  1995. return addrconf_ifid_gre(eui, dev);
  1996. case ARPHRD_6LOWPAN:
  1997. return addrconf_ifid_6lowpan(eui, dev);
  1998. case ARPHRD_IEEE1394:
  1999. return addrconf_ifid_ieee1394(eui, dev);
  2000. case ARPHRD_TUNNEL6:
  2001. case ARPHRD_IP6GRE:
  2002. case ARPHRD_RAWIP:
  2003. return addrconf_ifid_ip6tnl(eui, dev);
  2004. }
  2005. return -1;
  2006. }
  2007. static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
  2008. {
  2009. int err = -1;
  2010. struct inet6_ifaddr *ifp;
  2011. read_lock_bh(&idev->lock);
  2012. list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
  2013. if (ifp->scope > IFA_LINK)
  2014. break;
  2015. if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
  2016. memcpy(eui, ifp->addr.s6_addr+8, 8);
  2017. err = 0;
  2018. break;
  2019. }
  2020. }
  2021. read_unlock_bh(&idev->lock);
  2022. return err;
  2023. }
  2024. /* Generation of a randomized Interface Identifier
  2025. * draft-ietf-6man-rfc4941bis, Section 3.3.1
  2026. */
  2027. static void ipv6_gen_rnd_iid(struct in6_addr *addr)
  2028. {
  2029. regen:
  2030. get_random_bytes(&addr->s6_addr[8], 8);
  2031. /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
  2032. * check if generated address is not inappropriate:
  2033. *
  2034. * - Reserved IPv6 Interface Identifers
  2035. * - XXX: already assigned to an address on the device
  2036. */
  2037. /* Subnet-router anycast: 0000:0000:0000:0000 */
  2038. if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
  2039. goto regen;
  2040. /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
  2041. * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
  2042. * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
  2043. */
  2044. if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
  2045. (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
  2046. goto regen;
  2047. /* Reserved subnet anycast addresses */
  2048. if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
  2049. ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
  2050. goto regen;
  2051. }
  2052. u32 addrconf_rt_table(const struct net_device *dev, u32 default_table)
  2053. {
  2054. struct inet6_dev *idev = in6_dev_get(dev);
  2055. int sysctl;
  2056. u32 table;
  2057. if (!idev)
  2058. return default_table;
  2059. sysctl = idev->cnf.accept_ra_rt_table;
  2060. if (sysctl == 0) {
  2061. table = default_table;
  2062. } else if (sysctl > 0) {
  2063. table = (u32) sysctl;
  2064. } else {
  2065. table = (unsigned) dev->ifindex + (-sysctl);
  2066. }
  2067. in6_dev_put(idev);
  2068. return table;
  2069. }
  2070. /*
  2071. * Add prefix route.
  2072. */
  2073. static void
  2074. addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
  2075. struct net_device *dev, unsigned long expires,
  2076. u32 flags, gfp_t gfp_flags)
  2077. {
  2078. struct fib6_config cfg = {
  2079. .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
  2080. .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
  2081. .fc_ifindex = dev->ifindex,
  2082. .fc_expires = expires,
  2083. .fc_dst_len = plen,
  2084. .fc_flags = RTF_UP | flags,
  2085. .fc_nlinfo.nl_net = dev_net(dev),
  2086. .fc_protocol = RTPROT_KERNEL,
  2087. .fc_type = RTN_UNICAST,
  2088. };
  2089. cfg.fc_dst = *pfx;
  2090. /* Prevent useless cloning on PtP SIT.
  2091. This thing is done here expecting that the whole
  2092. class of non-broadcast devices need not cloning.
  2093. */
  2094. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2095. if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
  2096. cfg.fc_flags |= RTF_NONEXTHOP;
  2097. #endif
  2098. ip6_route_add(&cfg, gfp_flags, NULL);
  2099. }
  2100. static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
  2101. int plen,
  2102. const struct net_device *dev,
  2103. u32 flags, u32 noflags,
  2104. bool no_gw)
  2105. {
  2106. struct fib6_node *fn;
  2107. struct fib6_info *rt = NULL;
  2108. struct fib6_table *table;
  2109. u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
  2110. table = fib6_get_table(dev_net(dev), tb_id);
  2111. if (!table)
  2112. return NULL;
  2113. rcu_read_lock();
  2114. fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
  2115. if (!fn)
  2116. goto out;
  2117. for_each_fib6_node_rt_rcu(fn) {
  2118. /* prefix routes only use builtin fib6_nh */
  2119. if (rt->nh)
  2120. continue;
  2121. if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
  2122. continue;
  2123. if (no_gw && rt->fib6_nh->fib_nh_gw_family)
  2124. continue;
  2125. if ((rt->fib6_flags & flags) != flags)
  2126. continue;
  2127. if ((rt->fib6_flags & noflags) != 0)
  2128. continue;
  2129. if (!fib6_info_hold_safe(rt))
  2130. continue;
  2131. break;
  2132. }
  2133. out:
  2134. rcu_read_unlock();
  2135. return rt;
  2136. }
  2137. /* Create "default" multicast route to the interface */
  2138. static void addrconf_add_mroute(struct net_device *dev)
  2139. {
  2140. struct fib6_config cfg = {
  2141. .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
  2142. .fc_metric = IP6_RT_PRIO_ADDRCONF,
  2143. .fc_ifindex = dev->ifindex,
  2144. .fc_dst_len = 8,
  2145. .fc_flags = RTF_UP,
  2146. .fc_type = RTN_MULTICAST,
  2147. .fc_nlinfo.nl_net = dev_net(dev),
  2148. .fc_protocol = RTPROT_KERNEL,
  2149. };
  2150. ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
  2151. ip6_route_add(&cfg, GFP_KERNEL, NULL);
  2152. }
  2153. static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
  2154. {
  2155. struct inet6_dev *idev;
  2156. ASSERT_RTNL();
  2157. idev = ipv6_find_idev(dev);
  2158. if (IS_ERR(idev))
  2159. return idev;
  2160. if (idev->cnf.disable_ipv6)
  2161. return ERR_PTR(-EACCES);
  2162. /* Add default multicast route */
  2163. if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
  2164. addrconf_add_mroute(dev);
  2165. return idev;
  2166. }
  2167. static void manage_tempaddrs(struct inet6_dev *idev,
  2168. struct inet6_ifaddr *ifp,
  2169. __u32 valid_lft, __u32 prefered_lft,
  2170. bool create, unsigned long now)
  2171. {
  2172. u32 flags;
  2173. struct inet6_ifaddr *ift;
  2174. read_lock_bh(&idev->lock);
  2175. /* update all temporary addresses in the list */
  2176. list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
  2177. int age, max_valid, max_prefered;
  2178. if (ifp != ift->ifpub)
  2179. continue;
  2180. /* RFC 4941 section 3.3:
  2181. * If a received option will extend the lifetime of a public
  2182. * address, the lifetimes of temporary addresses should
  2183. * be extended, subject to the overall constraint that no
  2184. * temporary addresses should ever remain "valid" or "preferred"
  2185. * for a time longer than (TEMP_VALID_LIFETIME) or
  2186. * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
  2187. */
  2188. age = (now - ift->cstamp) / HZ;
  2189. max_valid = idev->cnf.temp_valid_lft - age;
  2190. if (max_valid < 0)
  2191. max_valid = 0;
  2192. max_prefered = idev->cnf.temp_prefered_lft -
  2193. idev->desync_factor - age;
  2194. if (max_prefered < 0)
  2195. max_prefered = 0;
  2196. if (valid_lft > max_valid)
  2197. valid_lft = max_valid;
  2198. if (prefered_lft > max_prefered)
  2199. prefered_lft = max_prefered;
  2200. spin_lock(&ift->lock);
  2201. flags = ift->flags;
  2202. ift->valid_lft = valid_lft;
  2203. ift->prefered_lft = prefered_lft;
  2204. ift->tstamp = now;
  2205. if (prefered_lft > 0)
  2206. ift->flags &= ~IFA_F_DEPRECATED;
  2207. spin_unlock(&ift->lock);
  2208. if (!(flags&IFA_F_TENTATIVE))
  2209. ipv6_ifa_notify(0, ift);
  2210. }
  2211. if ((create || list_empty(&idev->tempaddr_list)) &&
  2212. idev->cnf.use_tempaddr > 0) {
  2213. /* When a new public address is created as described
  2214. * in [ADDRCONF], also create a new temporary address.
  2215. * Also create a temporary address if it's enabled but
  2216. * no temporary address currently exists.
  2217. */
  2218. read_unlock_bh(&idev->lock);
  2219. ipv6_create_tempaddr(ifp, false);
  2220. } else {
  2221. read_unlock_bh(&idev->lock);
  2222. }
  2223. }
  2224. static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
  2225. {
  2226. return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
  2227. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
  2228. }
  2229. int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
  2230. const struct prefix_info *pinfo,
  2231. struct inet6_dev *in6_dev,
  2232. const struct in6_addr *addr, int addr_type,
  2233. u32 addr_flags, bool sllao, bool tokenized,
  2234. __u32 valid_lft, u32 prefered_lft)
  2235. {
  2236. struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
  2237. int create = 0, update_lft = 0;
  2238. if (!ifp && valid_lft) {
  2239. int max_addresses = in6_dev->cnf.max_addresses;
  2240. struct ifa6_config cfg = {
  2241. .pfx = addr,
  2242. .plen = pinfo->prefix_len,
  2243. .ifa_flags = addr_flags,
  2244. .valid_lft = valid_lft,
  2245. .preferred_lft = prefered_lft,
  2246. .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
  2247. };
  2248. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2249. if ((net->ipv6.devconf_all->optimistic_dad ||
  2250. in6_dev->cnf.optimistic_dad) &&
  2251. !net->ipv6.devconf_all->forwarding && sllao)
  2252. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  2253. #endif
  2254. /* Do not allow to create too much of autoconfigured
  2255. * addresses; this would be too easy way to crash kernel.
  2256. */
  2257. if (!max_addresses ||
  2258. ipv6_count_addresses(in6_dev) < max_addresses)
  2259. ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
  2260. if (IS_ERR_OR_NULL(ifp))
  2261. return -1;
  2262. create = 1;
  2263. spin_lock_bh(&ifp->lock);
  2264. ifp->flags |= IFA_F_MANAGETEMPADDR;
  2265. ifp->cstamp = jiffies;
  2266. ifp->tokenized = tokenized;
  2267. spin_unlock_bh(&ifp->lock);
  2268. addrconf_dad_start(ifp);
  2269. }
  2270. if (ifp) {
  2271. u32 flags;
  2272. unsigned long now;
  2273. u32 stored_lft;
  2274. /* update lifetime (RFC2462 5.5.3 e) */
  2275. spin_lock_bh(&ifp->lock);
  2276. now = jiffies;
  2277. if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
  2278. stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
  2279. else
  2280. stored_lft = 0;
  2281. if (!create && stored_lft) {
  2282. const u32 minimum_lft = min_t(u32,
  2283. stored_lft, MIN_VALID_LIFETIME);
  2284. valid_lft = max(valid_lft, minimum_lft);
  2285. /* RFC4862 Section 5.5.3e:
  2286. * "Note that the preferred lifetime of the
  2287. * corresponding address is always reset to
  2288. * the Preferred Lifetime in the received
  2289. * Prefix Information option, regardless of
  2290. * whether the valid lifetime is also reset or
  2291. * ignored."
  2292. *
  2293. * So we should always update prefered_lft here.
  2294. */
  2295. update_lft = 1;
  2296. }
  2297. if (update_lft) {
  2298. ifp->valid_lft = valid_lft;
  2299. ifp->prefered_lft = prefered_lft;
  2300. ifp->tstamp = now;
  2301. flags = ifp->flags;
  2302. ifp->flags &= ~IFA_F_DEPRECATED;
  2303. spin_unlock_bh(&ifp->lock);
  2304. if (!(flags&IFA_F_TENTATIVE))
  2305. ipv6_ifa_notify(0, ifp);
  2306. } else
  2307. spin_unlock_bh(&ifp->lock);
  2308. manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
  2309. create, now);
  2310. in6_ifa_put(ifp);
  2311. addrconf_verify();
  2312. }
  2313. return 0;
  2314. }
  2315. EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
  2316. void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
  2317. {
  2318. struct prefix_info *pinfo;
  2319. __u32 valid_lft;
  2320. __u32 prefered_lft;
  2321. int addr_type, err;
  2322. u32 addr_flags = 0;
  2323. struct inet6_dev *in6_dev;
  2324. struct net *net = dev_net(dev);
  2325. pinfo = (struct prefix_info *) opt;
  2326. if (len < sizeof(struct prefix_info)) {
  2327. netdev_dbg(dev, "addrconf: prefix option too short\n");
  2328. return;
  2329. }
  2330. /*
  2331. * Validation checks ([ADDRCONF], page 19)
  2332. */
  2333. addr_type = ipv6_addr_type(&pinfo->prefix);
  2334. if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
  2335. return;
  2336. valid_lft = ntohl(pinfo->valid);
  2337. prefered_lft = ntohl(pinfo->prefered);
  2338. if (prefered_lft > valid_lft) {
  2339. net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
  2340. return;
  2341. }
  2342. in6_dev = in6_dev_get(dev);
  2343. if (!in6_dev) {
  2344. net_dbg_ratelimited("addrconf: device %s not configured\n",
  2345. dev->name);
  2346. return;
  2347. }
  2348. /*
  2349. * Two things going on here:
  2350. * 1) Add routes for on-link prefixes
  2351. * 2) Configure prefixes with the auto flag set
  2352. */
  2353. if (pinfo->onlink) {
  2354. struct fib6_info *rt;
  2355. unsigned long rt_expires;
  2356. /* Avoid arithmetic overflow. Really, we could
  2357. * save rt_expires in seconds, likely valid_lft,
  2358. * but it would require division in fib gc, that it
  2359. * not good.
  2360. */
  2361. if (HZ > USER_HZ)
  2362. rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
  2363. else
  2364. rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
  2365. if (addrconf_finite_timeout(rt_expires))
  2366. rt_expires *= HZ;
  2367. rt = addrconf_get_prefix_route(&pinfo->prefix,
  2368. pinfo->prefix_len,
  2369. dev,
  2370. RTF_ADDRCONF | RTF_PREFIX_RT,
  2371. RTF_DEFAULT, true);
  2372. if (rt) {
  2373. /* Autoconf prefix route */
  2374. if (valid_lft == 0) {
  2375. ip6_del_rt(net, rt, false);
  2376. rt = NULL;
  2377. } else if (addrconf_finite_timeout(rt_expires)) {
  2378. /* not infinity */
  2379. fib6_set_expires(rt, jiffies + rt_expires);
  2380. } else {
  2381. fib6_clean_expires(rt);
  2382. }
  2383. } else if (valid_lft) {
  2384. clock_t expires = 0;
  2385. int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
  2386. if (addrconf_finite_timeout(rt_expires)) {
  2387. /* not infinity */
  2388. flags |= RTF_EXPIRES;
  2389. expires = jiffies_to_clock_t(rt_expires);
  2390. }
  2391. addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
  2392. 0, dev, expires, flags,
  2393. GFP_ATOMIC);
  2394. }
  2395. fib6_info_release(rt);
  2396. }
  2397. /* Try to figure out our local address for this prefix */
  2398. if (pinfo->autoconf && in6_dev->cnf.autoconf) {
  2399. struct in6_addr addr;
  2400. bool tokenized = false, dev_addr_generated = false;
  2401. if (pinfo->prefix_len == 64) {
  2402. memcpy(&addr, &pinfo->prefix, 8);
  2403. if (!ipv6_addr_any(&in6_dev->token)) {
  2404. read_lock_bh(&in6_dev->lock);
  2405. memcpy(addr.s6_addr + 8,
  2406. in6_dev->token.s6_addr + 8, 8);
  2407. read_unlock_bh(&in6_dev->lock);
  2408. tokenized = true;
  2409. } else if (is_addr_mode_generate_stable(in6_dev) &&
  2410. !ipv6_generate_stable_address(&addr, 0,
  2411. in6_dev)) {
  2412. addr_flags |= IFA_F_STABLE_PRIVACY;
  2413. goto ok;
  2414. } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
  2415. ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
  2416. goto put;
  2417. } else {
  2418. dev_addr_generated = true;
  2419. }
  2420. goto ok;
  2421. }
  2422. net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
  2423. pinfo->prefix_len);
  2424. goto put;
  2425. ok:
  2426. err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
  2427. &addr, addr_type,
  2428. addr_flags, sllao,
  2429. tokenized, valid_lft,
  2430. prefered_lft);
  2431. if (err)
  2432. goto put;
  2433. /* Ignore error case here because previous prefix add addr was
  2434. * successful which will be notified.
  2435. */
  2436. ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
  2437. addr_type, addr_flags, sllao,
  2438. tokenized, valid_lft,
  2439. prefered_lft,
  2440. dev_addr_generated);
  2441. }
  2442. inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
  2443. put:
  2444. in6_dev_put(in6_dev);
  2445. }
  2446. static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
  2447. struct in6_ifreq *ireq)
  2448. {
  2449. struct ip_tunnel_parm p = { };
  2450. int err;
  2451. if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
  2452. return -EADDRNOTAVAIL;
  2453. p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
  2454. p.iph.version = 4;
  2455. p.iph.ihl = 5;
  2456. p.iph.protocol = IPPROTO_IPV6;
  2457. p.iph.ttl = 64;
  2458. if (!dev->netdev_ops->ndo_tunnel_ctl)
  2459. return -EOPNOTSUPP;
  2460. err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
  2461. if (err)
  2462. return err;
  2463. dev = __dev_get_by_name(net, p.name);
  2464. if (!dev)
  2465. return -ENOBUFS;
  2466. return dev_open(dev, NULL);
  2467. }
  2468. /*
  2469. * Set destination address.
  2470. * Special case for SIT interfaces where we create a new "virtual"
  2471. * device.
  2472. */
  2473. int addrconf_set_dstaddr(struct net *net, void __user *arg)
  2474. {
  2475. struct net_device *dev;
  2476. struct in6_ifreq ireq;
  2477. int err = -ENODEV;
  2478. if (!IS_ENABLED(CONFIG_IPV6_SIT))
  2479. return -ENODEV;
  2480. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2481. return -EFAULT;
  2482. rtnl_lock();
  2483. dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
  2484. if (dev && dev->type == ARPHRD_SIT)
  2485. err = addrconf_set_sit_dstaddr(net, dev, &ireq);
  2486. rtnl_unlock();
  2487. return err;
  2488. }
  2489. static int ipv6_mc_config(struct sock *sk, bool join,
  2490. const struct in6_addr *addr, int ifindex)
  2491. {
  2492. int ret;
  2493. ASSERT_RTNL();
  2494. lock_sock(sk);
  2495. if (join)
  2496. ret = ipv6_sock_mc_join(sk, ifindex, addr);
  2497. else
  2498. ret = ipv6_sock_mc_drop(sk, ifindex, addr);
  2499. release_sock(sk);
  2500. return ret;
  2501. }
  2502. /*
  2503. * Manual configuration of address on an interface
  2504. */
  2505. static int inet6_addr_add(struct net *net, int ifindex,
  2506. struct ifa6_config *cfg,
  2507. struct netlink_ext_ack *extack)
  2508. {
  2509. struct inet6_ifaddr *ifp;
  2510. struct inet6_dev *idev;
  2511. struct net_device *dev;
  2512. unsigned long timeout;
  2513. clock_t expires;
  2514. u32 flags;
  2515. ASSERT_RTNL();
  2516. if (cfg->plen > 128)
  2517. return -EINVAL;
  2518. /* check the lifetime */
  2519. if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
  2520. return -EINVAL;
  2521. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
  2522. return -EINVAL;
  2523. dev = __dev_get_by_index(net, ifindex);
  2524. if (!dev)
  2525. return -ENODEV;
  2526. idev = addrconf_add_dev(dev);
  2527. if (IS_ERR(idev))
  2528. return PTR_ERR(idev);
  2529. if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
  2530. int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
  2531. true, cfg->pfx, ifindex);
  2532. if (ret < 0)
  2533. return ret;
  2534. }
  2535. cfg->scope = ipv6_addr_scope(cfg->pfx);
  2536. timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
  2537. if (addrconf_finite_timeout(timeout)) {
  2538. expires = jiffies_to_clock_t(timeout * HZ);
  2539. cfg->valid_lft = timeout;
  2540. flags = RTF_EXPIRES;
  2541. } else {
  2542. expires = 0;
  2543. flags = 0;
  2544. cfg->ifa_flags |= IFA_F_PERMANENT;
  2545. }
  2546. timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
  2547. if (addrconf_finite_timeout(timeout)) {
  2548. if (timeout == 0)
  2549. cfg->ifa_flags |= IFA_F_DEPRECATED;
  2550. cfg->preferred_lft = timeout;
  2551. }
  2552. ifp = ipv6_add_addr(idev, cfg, true, extack);
  2553. if (!IS_ERR(ifp)) {
  2554. if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
  2555. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  2556. ifp->rt_priority, dev, expires,
  2557. flags, GFP_KERNEL);
  2558. }
  2559. /* Send a netlink notification if DAD is enabled and
  2560. * optimistic flag is not set
  2561. */
  2562. if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
  2563. ipv6_ifa_notify(0, ifp);
  2564. /*
  2565. * Note that section 3.1 of RFC 4429 indicates
  2566. * that the Optimistic flag should not be set for
  2567. * manually configured addresses
  2568. */
  2569. addrconf_dad_start(ifp);
  2570. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
  2571. manage_tempaddrs(idev, ifp, cfg->valid_lft,
  2572. cfg->preferred_lft, true, jiffies);
  2573. in6_ifa_put(ifp);
  2574. addrconf_verify_rtnl();
  2575. return 0;
  2576. } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
  2577. ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
  2578. cfg->pfx, ifindex);
  2579. }
  2580. return PTR_ERR(ifp);
  2581. }
  2582. static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
  2583. const struct in6_addr *pfx, unsigned int plen)
  2584. {
  2585. struct inet6_ifaddr *ifp;
  2586. struct inet6_dev *idev;
  2587. struct net_device *dev;
  2588. if (plen > 128)
  2589. return -EINVAL;
  2590. dev = __dev_get_by_index(net, ifindex);
  2591. if (!dev)
  2592. return -ENODEV;
  2593. idev = __in6_dev_get(dev);
  2594. if (!idev)
  2595. return -ENXIO;
  2596. read_lock_bh(&idev->lock);
  2597. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  2598. if (ifp->prefix_len == plen &&
  2599. ipv6_addr_equal(pfx, &ifp->addr)) {
  2600. in6_ifa_hold(ifp);
  2601. read_unlock_bh(&idev->lock);
  2602. if (!(ifp->flags & IFA_F_TEMPORARY) &&
  2603. (ifa_flags & IFA_F_MANAGETEMPADDR))
  2604. manage_tempaddrs(idev, ifp, 0, 0, false,
  2605. jiffies);
  2606. ipv6_del_addr(ifp);
  2607. addrconf_verify_rtnl();
  2608. if (ipv6_addr_is_multicast(pfx)) {
  2609. ipv6_mc_config(net->ipv6.mc_autojoin_sk,
  2610. false, pfx, dev->ifindex);
  2611. }
  2612. return 0;
  2613. }
  2614. }
  2615. read_unlock_bh(&idev->lock);
  2616. return -EADDRNOTAVAIL;
  2617. }
  2618. int addrconf_add_ifaddr(struct net *net, void __user *arg)
  2619. {
  2620. struct ifa6_config cfg = {
  2621. .ifa_flags = IFA_F_PERMANENT,
  2622. .preferred_lft = INFINITY_LIFE_TIME,
  2623. .valid_lft = INFINITY_LIFE_TIME,
  2624. };
  2625. struct in6_ifreq ireq;
  2626. int err;
  2627. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2628. return -EPERM;
  2629. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2630. return -EFAULT;
  2631. cfg.pfx = &ireq.ifr6_addr;
  2632. cfg.plen = ireq.ifr6_prefixlen;
  2633. rtnl_lock();
  2634. err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
  2635. rtnl_unlock();
  2636. return err;
  2637. }
  2638. int addrconf_del_ifaddr(struct net *net, void __user *arg)
  2639. {
  2640. struct in6_ifreq ireq;
  2641. int err;
  2642. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2643. return -EPERM;
  2644. if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
  2645. return -EFAULT;
  2646. rtnl_lock();
  2647. err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
  2648. ireq.ifr6_prefixlen);
  2649. rtnl_unlock();
  2650. return err;
  2651. }
  2652. static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
  2653. int plen, int scope)
  2654. {
  2655. struct inet6_ifaddr *ifp;
  2656. struct ifa6_config cfg = {
  2657. .pfx = addr,
  2658. .plen = plen,
  2659. .ifa_flags = IFA_F_PERMANENT,
  2660. .valid_lft = INFINITY_LIFE_TIME,
  2661. .preferred_lft = INFINITY_LIFE_TIME,
  2662. .scope = scope
  2663. };
  2664. ifp = ipv6_add_addr(idev, &cfg, true, NULL);
  2665. if (!IS_ERR(ifp)) {
  2666. spin_lock_bh(&ifp->lock);
  2667. ifp->flags &= ~IFA_F_TENTATIVE;
  2668. spin_unlock_bh(&ifp->lock);
  2669. rt_genid_bump_ipv6(dev_net(idev->dev));
  2670. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  2671. in6_ifa_put(ifp);
  2672. }
  2673. }
  2674. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2675. static void sit_add_v4_addrs(struct inet6_dev *idev)
  2676. {
  2677. struct in6_addr addr;
  2678. struct net_device *dev;
  2679. struct net *net = dev_net(idev->dev);
  2680. int scope, plen;
  2681. u32 pflags = 0;
  2682. ASSERT_RTNL();
  2683. memset(&addr, 0, sizeof(struct in6_addr));
  2684. memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
  2685. if (idev->dev->flags&IFF_POINTOPOINT) {
  2686. if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
  2687. return;
  2688. addr.s6_addr32[0] = htonl(0xfe800000);
  2689. scope = IFA_LINK;
  2690. plen = 64;
  2691. } else {
  2692. scope = IPV6_ADDR_COMPATv4;
  2693. plen = 96;
  2694. pflags |= RTF_NONEXTHOP;
  2695. }
  2696. if (addr.s6_addr32[3]) {
  2697. add_addr(idev, &addr, plen, scope);
  2698. addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
  2699. GFP_KERNEL);
  2700. return;
  2701. }
  2702. for_each_netdev(net, dev) {
  2703. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  2704. if (in_dev && (dev->flags & IFF_UP)) {
  2705. struct in_ifaddr *ifa;
  2706. int flag = scope;
  2707. in_dev_for_each_ifa_rtnl(ifa, in_dev) {
  2708. addr.s6_addr32[3] = ifa->ifa_local;
  2709. if (ifa->ifa_scope == RT_SCOPE_LINK)
  2710. continue;
  2711. if (ifa->ifa_scope >= RT_SCOPE_HOST) {
  2712. if (idev->dev->flags&IFF_POINTOPOINT)
  2713. continue;
  2714. flag |= IFA_HOST;
  2715. }
  2716. add_addr(idev, &addr, plen, flag);
  2717. addrconf_prefix_route(&addr, plen, 0, idev->dev,
  2718. 0, pflags, GFP_KERNEL);
  2719. }
  2720. }
  2721. }
  2722. }
  2723. #endif
  2724. static void init_loopback(struct net_device *dev)
  2725. {
  2726. struct inet6_dev *idev;
  2727. /* ::1 */
  2728. ASSERT_RTNL();
  2729. idev = ipv6_find_idev(dev);
  2730. if (IS_ERR(idev)) {
  2731. pr_debug("%s: add_dev failed\n", __func__);
  2732. return;
  2733. }
  2734. add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
  2735. }
  2736. void addrconf_add_linklocal(struct inet6_dev *idev,
  2737. const struct in6_addr *addr, u32 flags)
  2738. {
  2739. struct ifa6_config cfg = {
  2740. .pfx = addr,
  2741. .plen = 64,
  2742. .ifa_flags = flags | IFA_F_PERMANENT,
  2743. .valid_lft = INFINITY_LIFE_TIME,
  2744. .preferred_lft = INFINITY_LIFE_TIME,
  2745. .scope = IFA_LINK
  2746. };
  2747. struct inet6_ifaddr *ifp;
  2748. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  2749. if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
  2750. idev->cnf.optimistic_dad) &&
  2751. !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
  2752. cfg.ifa_flags |= IFA_F_OPTIMISTIC;
  2753. #endif
  2754. ifp = ipv6_add_addr(idev, &cfg, true, NULL);
  2755. if (!IS_ERR(ifp)) {
  2756. addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
  2757. 0, 0, GFP_ATOMIC);
  2758. addrconf_dad_start(ifp);
  2759. in6_ifa_put(ifp);
  2760. }
  2761. }
  2762. EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
  2763. static bool ipv6_reserved_interfaceid(struct in6_addr address)
  2764. {
  2765. if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
  2766. return true;
  2767. if (address.s6_addr32[2] == htonl(0x02005eff) &&
  2768. ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
  2769. return true;
  2770. if (address.s6_addr32[2] == htonl(0xfdffffff) &&
  2771. ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
  2772. return true;
  2773. return false;
  2774. }
  2775. static int ipv6_generate_stable_address(struct in6_addr *address,
  2776. u8 dad_count,
  2777. const struct inet6_dev *idev)
  2778. {
  2779. static DEFINE_SPINLOCK(lock);
  2780. static __u32 digest[SHA1_DIGEST_WORDS];
  2781. static __u32 workspace[SHA1_WORKSPACE_WORDS];
  2782. static union {
  2783. char __data[SHA1_BLOCK_SIZE];
  2784. struct {
  2785. struct in6_addr secret;
  2786. __be32 prefix[2];
  2787. unsigned char hwaddr[MAX_ADDR_LEN];
  2788. u8 dad_count;
  2789. } __packed;
  2790. } data;
  2791. struct in6_addr secret;
  2792. struct in6_addr temp;
  2793. struct net *net = dev_net(idev->dev);
  2794. BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
  2795. if (idev->cnf.stable_secret.initialized)
  2796. secret = idev->cnf.stable_secret.secret;
  2797. else if (net->ipv6.devconf_dflt->stable_secret.initialized)
  2798. secret = net->ipv6.devconf_dflt->stable_secret.secret;
  2799. else
  2800. return -1;
  2801. retry:
  2802. spin_lock_bh(&lock);
  2803. sha1_init(digest);
  2804. memset(&data, 0, sizeof(data));
  2805. memset(workspace, 0, sizeof(workspace));
  2806. memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
  2807. data.prefix[0] = address->s6_addr32[0];
  2808. data.prefix[1] = address->s6_addr32[1];
  2809. data.secret = secret;
  2810. data.dad_count = dad_count;
  2811. sha1_transform(digest, data.__data, workspace);
  2812. temp = *address;
  2813. temp.s6_addr32[2] = (__force __be32)digest[0];
  2814. temp.s6_addr32[3] = (__force __be32)digest[1];
  2815. spin_unlock_bh(&lock);
  2816. if (ipv6_reserved_interfaceid(temp)) {
  2817. dad_count++;
  2818. if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
  2819. return -1;
  2820. goto retry;
  2821. }
  2822. *address = temp;
  2823. return 0;
  2824. }
  2825. static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
  2826. {
  2827. struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
  2828. if (s->initialized)
  2829. return;
  2830. s = &idev->cnf.stable_secret;
  2831. get_random_bytes(&s->secret, sizeof(s->secret));
  2832. s->initialized = true;
  2833. }
  2834. static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
  2835. {
  2836. struct in6_addr addr;
  2837. /* no link local addresses on L3 master devices */
  2838. if (netif_is_l3_master(idev->dev))
  2839. return;
  2840. /* no link local addresses on devices flagged as slaves */
  2841. if (idev->dev->flags & IFF_SLAVE)
  2842. return;
  2843. ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
  2844. switch (idev->cnf.addr_gen_mode) {
  2845. case IN6_ADDR_GEN_MODE_RANDOM:
  2846. ipv6_gen_mode_random_init(idev);
  2847. fallthrough;
  2848. case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
  2849. if (!ipv6_generate_stable_address(&addr, 0, idev))
  2850. addrconf_add_linklocal(idev, &addr,
  2851. IFA_F_STABLE_PRIVACY);
  2852. else if (prefix_route)
  2853. addrconf_prefix_route(&addr, 64, 0, idev->dev,
  2854. 0, 0, GFP_KERNEL);
  2855. break;
  2856. case IN6_ADDR_GEN_MODE_EUI64:
  2857. /* addrconf_add_linklocal also adds a prefix_route and we
  2858. * only need to care about prefix routes if ipv6_generate_eui64
  2859. * couldn't generate one.
  2860. */
  2861. if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
  2862. addrconf_add_linklocal(idev, &addr, 0);
  2863. else if (prefix_route)
  2864. addrconf_prefix_route(&addr, 64, 0, idev->dev,
  2865. 0, 0, GFP_KERNEL);
  2866. break;
  2867. case IN6_ADDR_GEN_MODE_NONE:
  2868. default:
  2869. /* will not add any link local address */
  2870. break;
  2871. }
  2872. }
  2873. static void addrconf_dev_config(struct net_device *dev)
  2874. {
  2875. struct inet6_dev *idev;
  2876. bool ret = false;
  2877. ASSERT_RTNL();
  2878. if ((dev->type != ARPHRD_ETHER) &&
  2879. (dev->type != ARPHRD_FDDI) &&
  2880. (dev->type != ARPHRD_ARCNET) &&
  2881. (dev->type != ARPHRD_INFINIBAND) &&
  2882. (dev->type != ARPHRD_IEEE1394) &&
  2883. (dev->type != ARPHRD_TUNNEL6) &&
  2884. (dev->type != ARPHRD_6LOWPAN) &&
  2885. (dev->type != ARPHRD_IP6GRE) &&
  2886. (dev->type != ARPHRD_IPGRE) &&
  2887. (dev->type != ARPHRD_TUNNEL) &&
  2888. (dev->type != ARPHRD_NONE) &&
  2889. (dev->type != ARPHRD_RAWIP)) {
  2890. /* Alas, we support only Ethernet autoconfiguration. */
  2891. idev = __in6_dev_get(dev);
  2892. if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
  2893. dev->flags & IFF_MULTICAST)
  2894. ipv6_mc_up(idev);
  2895. return;
  2896. }
  2897. idev = addrconf_add_dev(dev);
  2898. if (IS_ERR(idev))
  2899. return;
  2900. trace_android_vh_ipv6_gen_linklocal_addr(dev, &ret);
  2901. if (ret)
  2902. return;
  2903. /* this device type has no EUI support */
  2904. if (dev->type == ARPHRD_NONE &&
  2905. idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
  2906. idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
  2907. addrconf_addr_gen(idev, false);
  2908. }
  2909. #if IS_ENABLED(CONFIG_IPV6_SIT)
  2910. static void addrconf_sit_config(struct net_device *dev)
  2911. {
  2912. struct inet6_dev *idev;
  2913. ASSERT_RTNL();
  2914. /*
  2915. * Configure the tunnel with one of our IPv4
  2916. * addresses... we should configure all of
  2917. * our v4 addrs in the tunnel
  2918. */
  2919. idev = ipv6_find_idev(dev);
  2920. if (IS_ERR(idev)) {
  2921. pr_debug("%s: add_dev failed\n", __func__);
  2922. return;
  2923. }
  2924. if (dev->priv_flags & IFF_ISATAP) {
  2925. addrconf_addr_gen(idev, false);
  2926. return;
  2927. }
  2928. sit_add_v4_addrs(idev);
  2929. if (dev->flags&IFF_POINTOPOINT)
  2930. addrconf_add_mroute(dev);
  2931. }
  2932. #endif
  2933. #if IS_ENABLED(CONFIG_NET_IPGRE)
  2934. static void addrconf_gre_config(struct net_device *dev)
  2935. {
  2936. struct inet6_dev *idev;
  2937. ASSERT_RTNL();
  2938. idev = ipv6_find_idev(dev);
  2939. if (IS_ERR(idev)) {
  2940. pr_debug("%s: add_dev failed\n", __func__);
  2941. return;
  2942. }
  2943. addrconf_addr_gen(idev, true);
  2944. if (dev->flags & IFF_POINTOPOINT)
  2945. addrconf_add_mroute(dev);
  2946. }
  2947. #endif
  2948. static int fixup_permanent_addr(struct net *net,
  2949. struct inet6_dev *idev,
  2950. struct inet6_ifaddr *ifp)
  2951. {
  2952. /* !fib6_node means the host route was removed from the
  2953. * FIB, for example, if 'lo' device is taken down. In that
  2954. * case regenerate the host route.
  2955. */
  2956. if (!ifp->rt || !ifp->rt->fib6_node) {
  2957. struct fib6_info *f6i, *prev;
  2958. f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
  2959. GFP_ATOMIC);
  2960. if (IS_ERR(f6i))
  2961. return PTR_ERR(f6i);
  2962. /* ifp->rt can be accessed outside of rtnl */
  2963. spin_lock(&ifp->lock);
  2964. prev = ifp->rt;
  2965. ifp->rt = f6i;
  2966. spin_unlock(&ifp->lock);
  2967. fib6_info_release(prev);
  2968. }
  2969. if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
  2970. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  2971. ifp->rt_priority, idev->dev, 0, 0,
  2972. GFP_ATOMIC);
  2973. }
  2974. if (ifp->state == INET6_IFADDR_STATE_PREDAD)
  2975. addrconf_dad_start(ifp);
  2976. return 0;
  2977. }
  2978. static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
  2979. {
  2980. struct inet6_ifaddr *ifp, *tmp;
  2981. struct inet6_dev *idev;
  2982. idev = __in6_dev_get(dev);
  2983. if (!idev)
  2984. return;
  2985. write_lock_bh(&idev->lock);
  2986. list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
  2987. if ((ifp->flags & IFA_F_PERMANENT) &&
  2988. fixup_permanent_addr(net, idev, ifp) < 0) {
  2989. write_unlock_bh(&idev->lock);
  2990. in6_ifa_hold(ifp);
  2991. ipv6_del_addr(ifp);
  2992. write_lock_bh(&idev->lock);
  2993. net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
  2994. idev->dev->name, &ifp->addr);
  2995. }
  2996. }
  2997. write_unlock_bh(&idev->lock);
  2998. }
  2999. static int addrconf_notify(struct notifier_block *this, unsigned long event,
  3000. void *ptr)
  3001. {
  3002. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3003. struct netdev_notifier_change_info *change_info;
  3004. struct netdev_notifier_changeupper_info *info;
  3005. struct inet6_dev *idev = __in6_dev_get(dev);
  3006. struct net *net = dev_net(dev);
  3007. int run_pending = 0;
  3008. int err;
  3009. switch (event) {
  3010. case NETDEV_REGISTER:
  3011. if (!idev && dev->mtu >= IPV6_MIN_MTU) {
  3012. idev = ipv6_add_dev(dev);
  3013. if (IS_ERR(idev))
  3014. return notifier_from_errno(PTR_ERR(idev));
  3015. }
  3016. break;
  3017. case NETDEV_CHANGEMTU:
  3018. /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
  3019. if (dev->mtu < IPV6_MIN_MTU) {
  3020. addrconf_ifdown(dev, dev != net->loopback_dev);
  3021. break;
  3022. }
  3023. if (idev) {
  3024. rt6_mtu_change(dev, dev->mtu);
  3025. idev->cnf.mtu6 = dev->mtu;
  3026. break;
  3027. }
  3028. /* allocate new idev */
  3029. idev = ipv6_add_dev(dev);
  3030. if (IS_ERR(idev))
  3031. break;
  3032. /* device is still not ready */
  3033. if (!(idev->if_flags & IF_READY))
  3034. break;
  3035. run_pending = 1;
  3036. fallthrough;
  3037. case NETDEV_UP:
  3038. case NETDEV_CHANGE:
  3039. if (dev->flags & IFF_SLAVE)
  3040. break;
  3041. if (idev && idev->cnf.disable_ipv6)
  3042. break;
  3043. if (event == NETDEV_UP) {
  3044. /* restore routes for permanent addresses */
  3045. addrconf_permanent_addr(net, dev);
  3046. if (!addrconf_link_ready(dev)) {
  3047. /* device is not ready yet. */
  3048. pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
  3049. dev->name);
  3050. break;
  3051. }
  3052. if (!idev && dev->mtu >= IPV6_MIN_MTU)
  3053. idev = ipv6_add_dev(dev);
  3054. if (!IS_ERR_OR_NULL(idev)) {
  3055. idev->if_flags |= IF_READY;
  3056. run_pending = 1;
  3057. }
  3058. } else if (event == NETDEV_CHANGE) {
  3059. if (!addrconf_link_ready(dev)) {
  3060. /* device is still not ready. */
  3061. rt6_sync_down_dev(dev, event);
  3062. break;
  3063. }
  3064. if (!IS_ERR_OR_NULL(idev)) {
  3065. if (idev->if_flags & IF_READY) {
  3066. /* device is already configured -
  3067. * but resend MLD reports, we might
  3068. * have roamed and need to update
  3069. * multicast snooping switches
  3070. */
  3071. ipv6_mc_up(idev);
  3072. change_info = ptr;
  3073. if (change_info->flags_changed & IFF_NOARP)
  3074. addrconf_dad_run(idev, true);
  3075. rt6_sync_up(dev, RTNH_F_LINKDOWN);
  3076. break;
  3077. }
  3078. idev->if_flags |= IF_READY;
  3079. }
  3080. pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
  3081. dev->name);
  3082. run_pending = 1;
  3083. }
  3084. switch (dev->type) {
  3085. #if IS_ENABLED(CONFIG_IPV6_SIT)
  3086. case ARPHRD_SIT:
  3087. addrconf_sit_config(dev);
  3088. break;
  3089. #endif
  3090. #if IS_ENABLED(CONFIG_NET_IPGRE)
  3091. case ARPHRD_IPGRE:
  3092. addrconf_gre_config(dev);
  3093. break;
  3094. #endif
  3095. case ARPHRD_LOOPBACK:
  3096. init_loopback(dev);
  3097. break;
  3098. default:
  3099. addrconf_dev_config(dev);
  3100. break;
  3101. }
  3102. if (!IS_ERR_OR_NULL(idev)) {
  3103. if (run_pending)
  3104. addrconf_dad_run(idev, false);
  3105. /* Device has an address by now */
  3106. rt6_sync_up(dev, RTNH_F_DEAD);
  3107. /*
  3108. * If the MTU changed during the interface down,
  3109. * when the interface up, the changed MTU must be
  3110. * reflected in the idev as well as routers.
  3111. */
  3112. if (idev->cnf.mtu6 != dev->mtu &&
  3113. dev->mtu >= IPV6_MIN_MTU) {
  3114. rt6_mtu_change(dev, dev->mtu);
  3115. idev->cnf.mtu6 = dev->mtu;
  3116. }
  3117. idev->tstamp = jiffies;
  3118. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  3119. /*
  3120. * If the changed mtu during down is lower than
  3121. * IPV6_MIN_MTU stop IPv6 on this interface.
  3122. */
  3123. if (dev->mtu < IPV6_MIN_MTU)
  3124. addrconf_ifdown(dev, dev != net->loopback_dev);
  3125. }
  3126. break;
  3127. case NETDEV_DOWN:
  3128. case NETDEV_UNREGISTER:
  3129. /*
  3130. * Remove all addresses from this interface.
  3131. */
  3132. addrconf_ifdown(dev, event != NETDEV_DOWN);
  3133. break;
  3134. case NETDEV_CHANGENAME:
  3135. if (idev) {
  3136. snmp6_unregister_dev(idev);
  3137. addrconf_sysctl_unregister(idev);
  3138. err = addrconf_sysctl_register(idev);
  3139. if (err)
  3140. return notifier_from_errno(err);
  3141. err = snmp6_register_dev(idev);
  3142. if (err) {
  3143. addrconf_sysctl_unregister(idev);
  3144. return notifier_from_errno(err);
  3145. }
  3146. }
  3147. break;
  3148. case NETDEV_PRE_TYPE_CHANGE:
  3149. case NETDEV_POST_TYPE_CHANGE:
  3150. if (idev)
  3151. addrconf_type_change(dev, event);
  3152. break;
  3153. case NETDEV_CHANGEUPPER:
  3154. info = ptr;
  3155. /* flush all routes if dev is linked to or unlinked from
  3156. * an L3 master device (e.g., VRF)
  3157. */
  3158. if (info->upper_dev && netif_is_l3_master(info->upper_dev))
  3159. addrconf_ifdown(dev, false);
  3160. }
  3161. return NOTIFY_OK;
  3162. }
  3163. /*
  3164. * addrconf module should be notified of a device going up
  3165. */
  3166. static struct notifier_block ipv6_dev_notf = {
  3167. .notifier_call = addrconf_notify,
  3168. .priority = ADDRCONF_NOTIFY_PRIORITY,
  3169. };
  3170. static void addrconf_type_change(struct net_device *dev, unsigned long event)
  3171. {
  3172. struct inet6_dev *idev;
  3173. ASSERT_RTNL();
  3174. idev = __in6_dev_get(dev);
  3175. if (event == NETDEV_POST_TYPE_CHANGE)
  3176. ipv6_mc_remap(idev);
  3177. else if (event == NETDEV_PRE_TYPE_CHANGE)
  3178. ipv6_mc_unmap(idev);
  3179. }
  3180. static bool addr_is_local(const struct in6_addr *addr)
  3181. {
  3182. return ipv6_addr_type(addr) &
  3183. (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
  3184. }
  3185. static int addrconf_ifdown(struct net_device *dev, bool unregister)
  3186. {
  3187. unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
  3188. struct net *net = dev_net(dev);
  3189. struct inet6_dev *idev;
  3190. struct inet6_ifaddr *ifa, *tmp;
  3191. bool keep_addr = false;
  3192. bool was_ready;
  3193. int state, i;
  3194. ASSERT_RTNL();
  3195. rt6_disable_ip(dev, event);
  3196. idev = __in6_dev_get(dev);
  3197. if (!idev)
  3198. return -ENODEV;
  3199. /*
  3200. * Step 1: remove reference to ipv6 device from parent device.
  3201. * Do not dev_put!
  3202. */
  3203. if (unregister) {
  3204. idev->dead = 1;
  3205. /* protected by rtnl_lock */
  3206. RCU_INIT_POINTER(dev->ip6_ptr, NULL);
  3207. /* Step 1.5: remove snmp6 entry */
  3208. snmp6_unregister_dev(idev);
  3209. }
  3210. /* combine the user config with event to determine if permanent
  3211. * addresses are to be removed from address hash table
  3212. */
  3213. if (!unregister && !idev->cnf.disable_ipv6) {
  3214. /* aggregate the system setting and interface setting */
  3215. int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
  3216. if (!_keep_addr)
  3217. _keep_addr = idev->cnf.keep_addr_on_down;
  3218. keep_addr = (_keep_addr > 0);
  3219. }
  3220. /* Step 2: clear hash table */
  3221. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  3222. struct hlist_head *h = &inet6_addr_lst[i];
  3223. spin_lock_bh(&addrconf_hash_lock);
  3224. restart:
  3225. hlist_for_each_entry_rcu(ifa, h, addr_lst) {
  3226. if (ifa->idev == idev) {
  3227. addrconf_del_dad_work(ifa);
  3228. /* combined flag + permanent flag decide if
  3229. * address is retained on a down event
  3230. */
  3231. if (!keep_addr ||
  3232. !(ifa->flags & IFA_F_PERMANENT) ||
  3233. addr_is_local(&ifa->addr)) {
  3234. hlist_del_init_rcu(&ifa->addr_lst);
  3235. goto restart;
  3236. }
  3237. }
  3238. }
  3239. spin_unlock_bh(&addrconf_hash_lock);
  3240. }
  3241. write_lock_bh(&idev->lock);
  3242. addrconf_del_rs_timer(idev);
  3243. /* Step 2: clear flags for stateless addrconf, repeated down
  3244. * detection
  3245. */
  3246. was_ready = idev->if_flags & IF_READY;
  3247. if (!unregister)
  3248. idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
  3249. /* Step 3: clear tempaddr list */
  3250. while (!list_empty(&idev->tempaddr_list)) {
  3251. ifa = list_first_entry(&idev->tempaddr_list,
  3252. struct inet6_ifaddr, tmp_list);
  3253. list_del(&ifa->tmp_list);
  3254. write_unlock_bh(&idev->lock);
  3255. spin_lock_bh(&ifa->lock);
  3256. if (ifa->ifpub) {
  3257. in6_ifa_put(ifa->ifpub);
  3258. ifa->ifpub = NULL;
  3259. }
  3260. spin_unlock_bh(&ifa->lock);
  3261. in6_ifa_put(ifa);
  3262. write_lock_bh(&idev->lock);
  3263. }
  3264. list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
  3265. struct fib6_info *rt = NULL;
  3266. bool keep;
  3267. addrconf_del_dad_work(ifa);
  3268. keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
  3269. !addr_is_local(&ifa->addr);
  3270. write_unlock_bh(&idev->lock);
  3271. spin_lock_bh(&ifa->lock);
  3272. if (keep) {
  3273. /* set state to skip the notifier below */
  3274. state = INET6_IFADDR_STATE_DEAD;
  3275. ifa->state = INET6_IFADDR_STATE_PREDAD;
  3276. if (!(ifa->flags & IFA_F_NODAD))
  3277. ifa->flags |= IFA_F_TENTATIVE;
  3278. rt = ifa->rt;
  3279. ifa->rt = NULL;
  3280. } else {
  3281. state = ifa->state;
  3282. ifa->state = INET6_IFADDR_STATE_DEAD;
  3283. }
  3284. spin_unlock_bh(&ifa->lock);
  3285. if (rt)
  3286. ip6_del_rt(net, rt, false);
  3287. if (state != INET6_IFADDR_STATE_DEAD) {
  3288. __ipv6_ifa_notify(RTM_DELADDR, ifa);
  3289. inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
  3290. } else {
  3291. if (idev->cnf.forwarding)
  3292. addrconf_leave_anycast(ifa);
  3293. addrconf_leave_solict(ifa->idev, &ifa->addr);
  3294. }
  3295. write_lock_bh(&idev->lock);
  3296. if (!keep) {
  3297. list_del_rcu(&ifa->if_list);
  3298. in6_ifa_put(ifa);
  3299. }
  3300. }
  3301. write_unlock_bh(&idev->lock);
  3302. /* Step 5: Discard anycast and multicast list */
  3303. if (unregister) {
  3304. ipv6_ac_destroy_dev(idev);
  3305. ipv6_mc_destroy_dev(idev);
  3306. } else if (was_ready) {
  3307. ipv6_mc_down(idev);
  3308. }
  3309. idev->tstamp = jiffies;
  3310. /* Last: Shot the device (if unregistered) */
  3311. if (unregister) {
  3312. addrconf_sysctl_unregister(idev);
  3313. neigh_parms_release(&nd_tbl, idev->nd_parms);
  3314. neigh_ifdown(&nd_tbl, dev);
  3315. in6_dev_put(idev);
  3316. }
  3317. return 0;
  3318. }
  3319. static void addrconf_rs_timer(struct timer_list *t)
  3320. {
  3321. struct inet6_dev *idev = from_timer(idev, t, rs_timer);
  3322. struct net_device *dev = idev->dev;
  3323. struct in6_addr lladdr;
  3324. write_lock(&idev->lock);
  3325. if (idev->dead || !(idev->if_flags & IF_READY))
  3326. goto out;
  3327. if (!ipv6_accept_ra(idev))
  3328. goto out;
  3329. /* Announcement received after solicitation was sent */
  3330. if (idev->if_flags & IF_RA_RCVD)
  3331. goto out;
  3332. if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
  3333. write_unlock(&idev->lock);
  3334. if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  3335. ndisc_send_rs(dev, &lladdr,
  3336. &in6addr_linklocal_allrouters);
  3337. else
  3338. goto put;
  3339. write_lock(&idev->lock);
  3340. idev->rs_interval = rfc3315_s14_backoff_update(
  3341. idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
  3342. /* The wait after the last probe can be shorter */
  3343. addrconf_mod_rs_timer(idev, (idev->rs_probes ==
  3344. idev->cnf.rtr_solicits) ?
  3345. idev->cnf.rtr_solicit_delay :
  3346. idev->rs_interval);
  3347. } else {
  3348. /*
  3349. * Note: we do not support deprecated "all on-link"
  3350. * assumption any longer.
  3351. */
  3352. pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
  3353. }
  3354. out:
  3355. write_unlock(&idev->lock);
  3356. put:
  3357. in6_dev_put(idev);
  3358. }
  3359. /*
  3360. * Duplicate Address Detection
  3361. */
  3362. static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
  3363. {
  3364. unsigned long rand_num;
  3365. struct inet6_dev *idev = ifp->idev;
  3366. u64 nonce;
  3367. if (ifp->flags & IFA_F_OPTIMISTIC)
  3368. rand_num = 0;
  3369. else
  3370. rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
  3371. nonce = 0;
  3372. if (idev->cnf.enhanced_dad ||
  3373. dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
  3374. do
  3375. get_random_bytes(&nonce, 6);
  3376. while (nonce == 0);
  3377. }
  3378. ifp->dad_nonce = nonce;
  3379. ifp->dad_probes = idev->cnf.dad_transmits;
  3380. addrconf_mod_dad_work(ifp, rand_num);
  3381. }
  3382. static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
  3383. {
  3384. struct inet6_dev *idev = ifp->idev;
  3385. struct net_device *dev = idev->dev;
  3386. bool bump_id, notify = false;
  3387. struct net *net;
  3388. addrconf_join_solict(dev, &ifp->addr);
  3389. prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
  3390. read_lock_bh(&idev->lock);
  3391. spin_lock(&ifp->lock);
  3392. if (ifp->state == INET6_IFADDR_STATE_DEAD)
  3393. goto out;
  3394. net = dev_net(dev);
  3395. if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
  3396. (net->ipv6.devconf_all->accept_dad < 1 &&
  3397. idev->cnf.accept_dad < 1) ||
  3398. !(ifp->flags&IFA_F_TENTATIVE) ||
  3399. ifp->flags & IFA_F_NODAD) {
  3400. bool send_na = false;
  3401. if (ifp->flags & IFA_F_TENTATIVE &&
  3402. !(ifp->flags & IFA_F_OPTIMISTIC))
  3403. send_na = true;
  3404. bump_id = ifp->flags & IFA_F_TENTATIVE;
  3405. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  3406. spin_unlock(&ifp->lock);
  3407. read_unlock_bh(&idev->lock);
  3408. addrconf_dad_completed(ifp, bump_id, send_na);
  3409. return;
  3410. }
  3411. if (!(idev->if_flags & IF_READY)) {
  3412. spin_unlock(&ifp->lock);
  3413. read_unlock_bh(&idev->lock);
  3414. /*
  3415. * If the device is not ready:
  3416. * - keep it tentative if it is a permanent address.
  3417. * - otherwise, kill it.
  3418. */
  3419. in6_ifa_hold(ifp);
  3420. addrconf_dad_stop(ifp, 0);
  3421. return;
  3422. }
  3423. /*
  3424. * Optimistic nodes can start receiving
  3425. * Frames right away
  3426. */
  3427. if (ifp->flags & IFA_F_OPTIMISTIC) {
  3428. ip6_ins_rt(net, ifp->rt);
  3429. if (ipv6_use_optimistic_addr(net, idev)) {
  3430. /* Because optimistic nodes can use this address,
  3431. * notify listeners. If DAD fails, RTM_DELADDR is sent.
  3432. */
  3433. notify = true;
  3434. }
  3435. }
  3436. addrconf_dad_kick(ifp);
  3437. out:
  3438. spin_unlock(&ifp->lock);
  3439. read_unlock_bh(&idev->lock);
  3440. if (notify)
  3441. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  3442. }
  3443. static void addrconf_dad_start(struct inet6_ifaddr *ifp)
  3444. {
  3445. bool begin_dad = false;
  3446. spin_lock_bh(&ifp->lock);
  3447. if (ifp->state != INET6_IFADDR_STATE_DEAD) {
  3448. ifp->state = INET6_IFADDR_STATE_PREDAD;
  3449. begin_dad = true;
  3450. }
  3451. spin_unlock_bh(&ifp->lock);
  3452. if (begin_dad)
  3453. addrconf_mod_dad_work(ifp, 0);
  3454. }
  3455. static void addrconf_dad_work(struct work_struct *w)
  3456. {
  3457. struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
  3458. struct inet6_ifaddr,
  3459. dad_work);
  3460. struct inet6_dev *idev = ifp->idev;
  3461. bool bump_id, disable_ipv6 = false;
  3462. struct in6_addr mcaddr;
  3463. enum {
  3464. DAD_PROCESS,
  3465. DAD_BEGIN,
  3466. DAD_ABORT,
  3467. } action = DAD_PROCESS;
  3468. rtnl_lock();
  3469. spin_lock_bh(&ifp->lock);
  3470. if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
  3471. action = DAD_BEGIN;
  3472. ifp->state = INET6_IFADDR_STATE_DAD;
  3473. } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
  3474. action = DAD_ABORT;
  3475. ifp->state = INET6_IFADDR_STATE_POSTDAD;
  3476. if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
  3477. idev->cnf.accept_dad > 1) &&
  3478. !idev->cnf.disable_ipv6 &&
  3479. !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
  3480. struct in6_addr addr;
  3481. addr.s6_addr32[0] = htonl(0xfe800000);
  3482. addr.s6_addr32[1] = 0;
  3483. if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
  3484. ipv6_addr_equal(&ifp->addr, &addr)) {
  3485. /* DAD failed for link-local based on MAC */
  3486. idev->cnf.disable_ipv6 = 1;
  3487. pr_info("%s: IPv6 being disabled!\n",
  3488. ifp->idev->dev->name);
  3489. disable_ipv6 = true;
  3490. }
  3491. }
  3492. }
  3493. spin_unlock_bh(&ifp->lock);
  3494. if (action == DAD_BEGIN) {
  3495. addrconf_dad_begin(ifp);
  3496. goto out;
  3497. } else if (action == DAD_ABORT) {
  3498. in6_ifa_hold(ifp);
  3499. addrconf_dad_stop(ifp, 1);
  3500. if (disable_ipv6)
  3501. addrconf_ifdown(idev->dev, false);
  3502. goto out;
  3503. }
  3504. if (!ifp->dad_probes && addrconf_dad_end(ifp))
  3505. goto out;
  3506. write_lock_bh(&idev->lock);
  3507. if (idev->dead || !(idev->if_flags & IF_READY)) {
  3508. write_unlock_bh(&idev->lock);
  3509. goto out;
  3510. }
  3511. spin_lock(&ifp->lock);
  3512. if (ifp->state == INET6_IFADDR_STATE_DEAD) {
  3513. spin_unlock(&ifp->lock);
  3514. write_unlock_bh(&idev->lock);
  3515. goto out;
  3516. }
  3517. if (ifp->dad_probes == 0) {
  3518. bool send_na = false;
  3519. /*
  3520. * DAD was successful
  3521. */
  3522. if (ifp->flags & IFA_F_TENTATIVE &&
  3523. !(ifp->flags & IFA_F_OPTIMISTIC))
  3524. send_na = true;
  3525. bump_id = ifp->flags & IFA_F_TENTATIVE;
  3526. ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
  3527. spin_unlock(&ifp->lock);
  3528. write_unlock_bh(&idev->lock);
  3529. addrconf_dad_completed(ifp, bump_id, send_na);
  3530. goto out;
  3531. }
  3532. ifp->dad_probes--;
  3533. addrconf_mod_dad_work(ifp,
  3534. max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
  3535. HZ/100));
  3536. spin_unlock(&ifp->lock);
  3537. write_unlock_bh(&idev->lock);
  3538. /* send a neighbour solicitation for our addr */
  3539. addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
  3540. ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
  3541. ifp->dad_nonce);
  3542. out:
  3543. in6_ifa_put(ifp);
  3544. rtnl_unlock();
  3545. }
  3546. /* ifp->idev must be at least read locked */
  3547. static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
  3548. {
  3549. struct inet6_ifaddr *ifpiter;
  3550. struct inet6_dev *idev = ifp->idev;
  3551. list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
  3552. if (ifpiter->scope > IFA_LINK)
  3553. break;
  3554. if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
  3555. (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
  3556. IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
  3557. IFA_F_PERMANENT)
  3558. return false;
  3559. }
  3560. return true;
  3561. }
  3562. static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
  3563. bool send_na)
  3564. {
  3565. struct net_device *dev = ifp->idev->dev;
  3566. struct in6_addr lladdr;
  3567. bool send_rs, send_mld;
  3568. addrconf_del_dad_work(ifp);
  3569. /*
  3570. * Configure the address for reception. Now it is valid.
  3571. */
  3572. ipv6_ifa_notify(RTM_NEWADDR, ifp);
  3573. /* If added prefix is link local and we are prepared to process
  3574. router advertisements, start sending router solicitations.
  3575. */
  3576. read_lock_bh(&ifp->idev->lock);
  3577. send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
  3578. send_rs = send_mld &&
  3579. ipv6_accept_ra(ifp->idev) &&
  3580. ifp->idev->cnf.rtr_solicits != 0 &&
  3581. (dev->flags&IFF_LOOPBACK) == 0;
  3582. read_unlock_bh(&ifp->idev->lock);
  3583. /* While dad is in progress mld report's source address is in6_addrany.
  3584. * Resend with proper ll now.
  3585. */
  3586. if (send_mld)
  3587. ipv6_mc_dad_complete(ifp->idev);
  3588. /* send unsolicited NA if enabled */
  3589. if (send_na &&
  3590. (ifp->idev->cnf.ndisc_notify ||
  3591. dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
  3592. ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
  3593. /*router=*/ !!ifp->idev->cnf.forwarding,
  3594. /*solicited=*/ false, /*override=*/ true,
  3595. /*inc_opt=*/ true);
  3596. }
  3597. if (send_rs) {
  3598. /*
  3599. * If a host as already performed a random delay
  3600. * [...] as part of DAD [...] there is no need
  3601. * to delay again before sending the first RS
  3602. */
  3603. if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
  3604. return;
  3605. ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
  3606. write_lock_bh(&ifp->idev->lock);
  3607. spin_lock(&ifp->lock);
  3608. ifp->idev->rs_interval = rfc3315_s14_backoff_init(
  3609. ifp->idev->cnf.rtr_solicit_interval);
  3610. ifp->idev->rs_probes = 1;
  3611. ifp->idev->if_flags |= IF_RS_SENT;
  3612. addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
  3613. spin_unlock(&ifp->lock);
  3614. write_unlock_bh(&ifp->idev->lock);
  3615. }
  3616. if (bump_id)
  3617. rt_genid_bump_ipv6(dev_net(dev));
  3618. /* Make sure that a new temporary address will be created
  3619. * before this temporary address becomes deprecated.
  3620. */
  3621. if (ifp->flags & IFA_F_TEMPORARY)
  3622. addrconf_verify_rtnl();
  3623. }
  3624. static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
  3625. {
  3626. struct inet6_ifaddr *ifp;
  3627. read_lock_bh(&idev->lock);
  3628. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  3629. spin_lock(&ifp->lock);
  3630. if ((ifp->flags & IFA_F_TENTATIVE &&
  3631. ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
  3632. if (restart)
  3633. ifp->state = INET6_IFADDR_STATE_PREDAD;
  3634. addrconf_dad_kick(ifp);
  3635. }
  3636. spin_unlock(&ifp->lock);
  3637. }
  3638. read_unlock_bh(&idev->lock);
  3639. }
  3640. #ifdef CONFIG_PROC_FS
  3641. struct if6_iter_state {
  3642. struct seq_net_private p;
  3643. int bucket;
  3644. int offset;
  3645. };
  3646. static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
  3647. {
  3648. struct if6_iter_state *state = seq->private;
  3649. struct net *net = seq_file_net(seq);
  3650. struct inet6_ifaddr *ifa = NULL;
  3651. int p = 0;
  3652. /* initial bucket if pos is 0 */
  3653. if (pos == 0) {
  3654. state->bucket = 0;
  3655. state->offset = 0;
  3656. }
  3657. for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
  3658. hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
  3659. addr_lst) {
  3660. if (!net_eq(dev_net(ifa->idev->dev), net))
  3661. continue;
  3662. /* sync with offset */
  3663. if (p < state->offset) {
  3664. p++;
  3665. continue;
  3666. }
  3667. return ifa;
  3668. }
  3669. /* prepare for next bucket */
  3670. state->offset = 0;
  3671. p = 0;
  3672. }
  3673. return NULL;
  3674. }
  3675. static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
  3676. struct inet6_ifaddr *ifa)
  3677. {
  3678. struct if6_iter_state *state = seq->private;
  3679. struct net *net = seq_file_net(seq);
  3680. hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
  3681. if (!net_eq(dev_net(ifa->idev->dev), net))
  3682. continue;
  3683. state->offset++;
  3684. return ifa;
  3685. }
  3686. state->offset = 0;
  3687. while (++state->bucket < IN6_ADDR_HSIZE) {
  3688. hlist_for_each_entry_rcu(ifa,
  3689. &inet6_addr_lst[state->bucket], addr_lst) {
  3690. if (!net_eq(dev_net(ifa->idev->dev), net))
  3691. continue;
  3692. return ifa;
  3693. }
  3694. }
  3695. return NULL;
  3696. }
  3697. static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
  3698. __acquires(rcu)
  3699. {
  3700. rcu_read_lock();
  3701. return if6_get_first(seq, *pos);
  3702. }
  3703. static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3704. {
  3705. struct inet6_ifaddr *ifa;
  3706. ifa = if6_get_next(seq, v);
  3707. ++*pos;
  3708. return ifa;
  3709. }
  3710. static void if6_seq_stop(struct seq_file *seq, void *v)
  3711. __releases(rcu)
  3712. {
  3713. rcu_read_unlock();
  3714. }
  3715. static int if6_seq_show(struct seq_file *seq, void *v)
  3716. {
  3717. struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
  3718. seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
  3719. &ifp->addr,
  3720. ifp->idev->dev->ifindex,
  3721. ifp->prefix_len,
  3722. ifp->scope,
  3723. (u8) ifp->flags,
  3724. ifp->idev->dev->name);
  3725. return 0;
  3726. }
  3727. static const struct seq_operations if6_seq_ops = {
  3728. .start = if6_seq_start,
  3729. .next = if6_seq_next,
  3730. .show = if6_seq_show,
  3731. .stop = if6_seq_stop,
  3732. };
  3733. static int __net_init if6_proc_net_init(struct net *net)
  3734. {
  3735. if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
  3736. sizeof(struct if6_iter_state)))
  3737. return -ENOMEM;
  3738. return 0;
  3739. }
  3740. static void __net_exit if6_proc_net_exit(struct net *net)
  3741. {
  3742. remove_proc_entry("if_inet6", net->proc_net);
  3743. }
  3744. static struct pernet_operations if6_proc_net_ops = {
  3745. .init = if6_proc_net_init,
  3746. .exit = if6_proc_net_exit,
  3747. };
  3748. int __init if6_proc_init(void)
  3749. {
  3750. return register_pernet_subsys(&if6_proc_net_ops);
  3751. }
  3752. void if6_proc_exit(void)
  3753. {
  3754. unregister_pernet_subsys(&if6_proc_net_ops);
  3755. }
  3756. #endif /* CONFIG_PROC_FS */
  3757. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  3758. /* Check if address is a home address configured on any interface. */
  3759. int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
  3760. {
  3761. unsigned int hash = inet6_addr_hash(net, addr);
  3762. struct inet6_ifaddr *ifp = NULL;
  3763. int ret = 0;
  3764. rcu_read_lock();
  3765. hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
  3766. if (!net_eq(dev_net(ifp->idev->dev), net))
  3767. continue;
  3768. if (ipv6_addr_equal(&ifp->addr, addr) &&
  3769. (ifp->flags & IFA_F_HOMEADDRESS)) {
  3770. ret = 1;
  3771. break;
  3772. }
  3773. }
  3774. rcu_read_unlock();
  3775. return ret;
  3776. }
  3777. #endif
  3778. /* RFC6554 has some algorithm to avoid loops in segment routing by
  3779. * checking if the segments contains any of a local interface address.
  3780. *
  3781. * Quote:
  3782. *
  3783. * To detect loops in the SRH, a router MUST determine if the SRH
  3784. * includes multiple addresses assigned to any interface on that router.
  3785. * If such addresses appear more than once and are separated by at least
  3786. * one address not assigned to that router.
  3787. */
  3788. int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
  3789. unsigned char nsegs)
  3790. {
  3791. const struct in6_addr *addr;
  3792. int i, ret = 0, found = 0;
  3793. struct inet6_ifaddr *ifp;
  3794. bool separated = false;
  3795. unsigned int hash;
  3796. bool hash_found;
  3797. rcu_read_lock();
  3798. for (i = 0; i < nsegs; i++) {
  3799. addr = &segs[i];
  3800. hash = inet6_addr_hash(net, addr);
  3801. hash_found = false;
  3802. hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
  3803. if (!net_eq(dev_net(ifp->idev->dev), net))
  3804. continue;
  3805. if (ipv6_addr_equal(&ifp->addr, addr)) {
  3806. hash_found = true;
  3807. break;
  3808. }
  3809. }
  3810. if (hash_found) {
  3811. if (found > 1 && separated) {
  3812. ret = 1;
  3813. break;
  3814. }
  3815. separated = false;
  3816. found++;
  3817. } else {
  3818. separated = true;
  3819. }
  3820. }
  3821. rcu_read_unlock();
  3822. return ret;
  3823. }
  3824. /*
  3825. * Periodic address status verification
  3826. */
  3827. static void addrconf_verify_rtnl(void)
  3828. {
  3829. unsigned long now, next, next_sec, next_sched;
  3830. struct inet6_ifaddr *ifp;
  3831. int i;
  3832. ASSERT_RTNL();
  3833. rcu_read_lock_bh();
  3834. now = jiffies;
  3835. next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
  3836. cancel_delayed_work(&addr_chk_work);
  3837. for (i = 0; i < IN6_ADDR_HSIZE; i++) {
  3838. restart:
  3839. hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
  3840. unsigned long age;
  3841. /* When setting preferred_lft to a value not zero or
  3842. * infinity, while valid_lft is infinity
  3843. * IFA_F_PERMANENT has a non-infinity life time.
  3844. */
  3845. if ((ifp->flags & IFA_F_PERMANENT) &&
  3846. (ifp->prefered_lft == INFINITY_LIFE_TIME))
  3847. continue;
  3848. spin_lock(&ifp->lock);
  3849. /* We try to batch several events at once. */
  3850. age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
  3851. if (ifp->valid_lft != INFINITY_LIFE_TIME &&
  3852. age >= ifp->valid_lft) {
  3853. spin_unlock(&ifp->lock);
  3854. in6_ifa_hold(ifp);
  3855. ipv6_del_addr(ifp);
  3856. goto restart;
  3857. } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
  3858. spin_unlock(&ifp->lock);
  3859. continue;
  3860. } else if (age >= ifp->prefered_lft) {
  3861. /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
  3862. int deprecate = 0;
  3863. if (!(ifp->flags&IFA_F_DEPRECATED)) {
  3864. deprecate = 1;
  3865. ifp->flags |= IFA_F_DEPRECATED;
  3866. }
  3867. if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
  3868. (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
  3869. next = ifp->tstamp + ifp->valid_lft * HZ;
  3870. spin_unlock(&ifp->lock);
  3871. if (deprecate) {
  3872. in6_ifa_hold(ifp);
  3873. ipv6_ifa_notify(0, ifp);
  3874. in6_ifa_put(ifp);
  3875. goto restart;
  3876. }
  3877. } else if ((ifp->flags&IFA_F_TEMPORARY) &&
  3878. !(ifp->flags&IFA_F_TENTATIVE)) {
  3879. unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
  3880. ifp->idev->cnf.dad_transmits *
  3881. max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
  3882. if (age >= ifp->prefered_lft - regen_advance) {
  3883. struct inet6_ifaddr *ifpub = ifp->ifpub;
  3884. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3885. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3886. if (!ifp->regen_count && ifpub) {
  3887. ifp->regen_count++;
  3888. in6_ifa_hold(ifp);
  3889. in6_ifa_hold(ifpub);
  3890. spin_unlock(&ifp->lock);
  3891. spin_lock(&ifpub->lock);
  3892. ifpub->regen_count = 0;
  3893. spin_unlock(&ifpub->lock);
  3894. rcu_read_unlock_bh();
  3895. ipv6_create_tempaddr(ifpub, true);
  3896. in6_ifa_put(ifpub);
  3897. in6_ifa_put(ifp);
  3898. rcu_read_lock_bh();
  3899. goto restart;
  3900. }
  3901. } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
  3902. next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
  3903. spin_unlock(&ifp->lock);
  3904. } else {
  3905. /* ifp->prefered_lft <= ifp->valid_lft */
  3906. if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
  3907. next = ifp->tstamp + ifp->prefered_lft * HZ;
  3908. spin_unlock(&ifp->lock);
  3909. }
  3910. }
  3911. }
  3912. next_sec = round_jiffies_up(next);
  3913. next_sched = next;
  3914. /* If rounded timeout is accurate enough, accept it. */
  3915. if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
  3916. next_sched = next_sec;
  3917. /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
  3918. if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
  3919. next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
  3920. pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
  3921. now, next, next_sec, next_sched);
  3922. mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
  3923. rcu_read_unlock_bh();
  3924. }
  3925. static void addrconf_verify_work(struct work_struct *w)
  3926. {
  3927. rtnl_lock();
  3928. addrconf_verify_rtnl();
  3929. rtnl_unlock();
  3930. }
  3931. static void addrconf_verify(void)
  3932. {
  3933. mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
  3934. }
  3935. static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
  3936. struct in6_addr **peer_pfx)
  3937. {
  3938. struct in6_addr *pfx = NULL;
  3939. *peer_pfx = NULL;
  3940. if (addr)
  3941. pfx = nla_data(addr);
  3942. if (local) {
  3943. if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
  3944. *peer_pfx = pfx;
  3945. pfx = nla_data(local);
  3946. }
  3947. return pfx;
  3948. }
  3949. static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
  3950. [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
  3951. [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
  3952. [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
  3953. [IFA_FLAGS] = { .len = sizeof(u32) },
  3954. [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
  3955. [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
  3956. };
  3957. static int
  3958. inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
  3959. struct netlink_ext_ack *extack)
  3960. {
  3961. struct net *net = sock_net(skb->sk);
  3962. struct ifaddrmsg *ifm;
  3963. struct nlattr *tb[IFA_MAX+1];
  3964. struct in6_addr *pfx, *peer_pfx;
  3965. u32 ifa_flags;
  3966. int err;
  3967. err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  3968. ifa_ipv6_policy, extack);
  3969. if (err < 0)
  3970. return err;
  3971. ifm = nlmsg_data(nlh);
  3972. pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  3973. if (!pfx)
  3974. return -EINVAL;
  3975. ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
  3976. /* We ignore other flags so far. */
  3977. ifa_flags &= IFA_F_MANAGETEMPADDR;
  3978. return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
  3979. ifm->ifa_prefixlen);
  3980. }
  3981. static int modify_prefix_route(struct inet6_ifaddr *ifp,
  3982. unsigned long expires, u32 flags,
  3983. bool modify_peer)
  3984. {
  3985. struct fib6_info *f6i;
  3986. u32 prio;
  3987. f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
  3988. ifp->prefix_len,
  3989. ifp->idev->dev, 0, RTF_DEFAULT, true);
  3990. if (!f6i)
  3991. return -ENOENT;
  3992. prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
  3993. if (f6i->fib6_metric != prio) {
  3994. /* delete old one */
  3995. ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
  3996. /* add new one */
  3997. addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
  3998. ifp->prefix_len,
  3999. ifp->rt_priority, ifp->idev->dev,
  4000. expires, flags, GFP_KERNEL);
  4001. } else {
  4002. if (!expires)
  4003. fib6_clean_expires(f6i);
  4004. else
  4005. fib6_set_expires(f6i, expires);
  4006. fib6_info_release(f6i);
  4007. }
  4008. return 0;
  4009. }
  4010. static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
  4011. {
  4012. u32 flags;
  4013. clock_t expires;
  4014. unsigned long timeout;
  4015. bool was_managetempaddr;
  4016. bool had_prefixroute;
  4017. bool new_peer = false;
  4018. ASSERT_RTNL();
  4019. if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
  4020. return -EINVAL;
  4021. if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
  4022. (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
  4023. return -EINVAL;
  4024. if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
  4025. cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
  4026. timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
  4027. if (addrconf_finite_timeout(timeout)) {
  4028. expires = jiffies_to_clock_t(timeout * HZ);
  4029. cfg->valid_lft = timeout;
  4030. flags = RTF_EXPIRES;
  4031. } else {
  4032. expires = 0;
  4033. flags = 0;
  4034. cfg->ifa_flags |= IFA_F_PERMANENT;
  4035. }
  4036. timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
  4037. if (addrconf_finite_timeout(timeout)) {
  4038. if (timeout == 0)
  4039. cfg->ifa_flags |= IFA_F_DEPRECATED;
  4040. cfg->preferred_lft = timeout;
  4041. }
  4042. if (cfg->peer_pfx &&
  4043. memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
  4044. if (!ipv6_addr_any(&ifp->peer_addr))
  4045. cleanup_prefix_route(ifp, expires, true, true);
  4046. new_peer = true;
  4047. }
  4048. spin_lock_bh(&ifp->lock);
  4049. was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
  4050. had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
  4051. !(ifp->flags & IFA_F_NOPREFIXROUTE);
  4052. ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
  4053. IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
  4054. IFA_F_NOPREFIXROUTE);
  4055. ifp->flags |= cfg->ifa_flags;
  4056. ifp->tstamp = jiffies;
  4057. ifp->valid_lft = cfg->valid_lft;
  4058. ifp->prefered_lft = cfg->preferred_lft;
  4059. if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
  4060. ifp->rt_priority = cfg->rt_priority;
  4061. if (new_peer)
  4062. ifp->peer_addr = *cfg->peer_pfx;
  4063. spin_unlock_bh(&ifp->lock);
  4064. if (!(ifp->flags&IFA_F_TENTATIVE))
  4065. ipv6_ifa_notify(0, ifp);
  4066. if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
  4067. int rc = -ENOENT;
  4068. if (had_prefixroute)
  4069. rc = modify_prefix_route(ifp, expires, flags, false);
  4070. /* prefix route could have been deleted; if so restore it */
  4071. if (rc == -ENOENT) {
  4072. addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
  4073. ifp->rt_priority, ifp->idev->dev,
  4074. expires, flags, GFP_KERNEL);
  4075. }
  4076. if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
  4077. rc = modify_prefix_route(ifp, expires, flags, true);
  4078. if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
  4079. addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
  4080. ifp->rt_priority, ifp->idev->dev,
  4081. expires, flags, GFP_KERNEL);
  4082. }
  4083. } else if (had_prefixroute) {
  4084. enum cleanup_prefix_rt_t action;
  4085. unsigned long rt_expires;
  4086. write_lock_bh(&ifp->idev->lock);
  4087. action = check_cleanup_prefix_route(ifp, &rt_expires);
  4088. write_unlock_bh(&ifp->idev->lock);
  4089. if (action != CLEANUP_PREFIX_RT_NOP) {
  4090. cleanup_prefix_route(ifp, rt_expires,
  4091. action == CLEANUP_PREFIX_RT_DEL, false);
  4092. }
  4093. }
  4094. if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
  4095. if (was_managetempaddr &&
  4096. !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
  4097. cfg->valid_lft = 0;
  4098. cfg->preferred_lft = 0;
  4099. }
  4100. manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
  4101. cfg->preferred_lft, !was_managetempaddr,
  4102. jiffies);
  4103. }
  4104. addrconf_verify_rtnl();
  4105. return 0;
  4106. }
  4107. static int
  4108. inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
  4109. struct netlink_ext_ack *extack)
  4110. {
  4111. struct net *net = sock_net(skb->sk);
  4112. struct ifaddrmsg *ifm;
  4113. struct nlattr *tb[IFA_MAX+1];
  4114. struct in6_addr *peer_pfx;
  4115. struct inet6_ifaddr *ifa;
  4116. struct net_device *dev;
  4117. struct inet6_dev *idev;
  4118. struct ifa6_config cfg;
  4119. int err;
  4120. err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  4121. ifa_ipv6_policy, extack);
  4122. if (err < 0)
  4123. return err;
  4124. memset(&cfg, 0, sizeof(cfg));
  4125. ifm = nlmsg_data(nlh);
  4126. cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
  4127. if (!cfg.pfx)
  4128. return -EINVAL;
  4129. cfg.peer_pfx = peer_pfx;
  4130. cfg.plen = ifm->ifa_prefixlen;
  4131. if (tb[IFA_RT_PRIORITY])
  4132. cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
  4133. cfg.valid_lft = INFINITY_LIFE_TIME;
  4134. cfg.preferred_lft = INFINITY_LIFE_TIME;
  4135. if (tb[IFA_CACHEINFO]) {
  4136. struct ifa_cacheinfo *ci;
  4137. ci = nla_data(tb[IFA_CACHEINFO]);
  4138. cfg.valid_lft = ci->ifa_valid;
  4139. cfg.preferred_lft = ci->ifa_prefered;
  4140. }
  4141. dev = __dev_get_by_index(net, ifm->ifa_index);
  4142. if (!dev)
  4143. return -ENODEV;
  4144. if (tb[IFA_FLAGS])
  4145. cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
  4146. else
  4147. cfg.ifa_flags = ifm->ifa_flags;
  4148. /* We ignore other flags so far. */
  4149. cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
  4150. IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
  4151. IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
  4152. idev = ipv6_find_idev(dev);
  4153. if (IS_ERR(idev))
  4154. return PTR_ERR(idev);
  4155. if (!ipv6_allow_optimistic_dad(net, idev))
  4156. cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
  4157. if (cfg.ifa_flags & IFA_F_NODAD &&
  4158. cfg.ifa_flags & IFA_F_OPTIMISTIC) {
  4159. NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
  4160. return -EINVAL;
  4161. }
  4162. ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
  4163. if (!ifa) {
  4164. /*
  4165. * It would be best to check for !NLM_F_CREATE here but
  4166. * userspace already relies on not having to provide this.
  4167. */
  4168. return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
  4169. }
  4170. if (nlh->nlmsg_flags & NLM_F_EXCL ||
  4171. !(nlh->nlmsg_flags & NLM_F_REPLACE))
  4172. err = -EEXIST;
  4173. else
  4174. err = inet6_addr_modify(ifa, &cfg);
  4175. in6_ifa_put(ifa);
  4176. return err;
  4177. }
  4178. static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
  4179. u8 scope, int ifindex)
  4180. {
  4181. struct ifaddrmsg *ifm;
  4182. ifm = nlmsg_data(nlh);
  4183. ifm->ifa_family = AF_INET6;
  4184. ifm->ifa_prefixlen = prefixlen;
  4185. ifm->ifa_flags = flags;
  4186. ifm->ifa_scope = scope;
  4187. ifm->ifa_index = ifindex;
  4188. }
  4189. static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
  4190. unsigned long tstamp, u32 preferred, u32 valid)
  4191. {
  4192. struct ifa_cacheinfo ci;
  4193. ci.cstamp = cstamp_delta(cstamp);
  4194. ci.tstamp = cstamp_delta(tstamp);
  4195. ci.ifa_prefered = preferred;
  4196. ci.ifa_valid = valid;
  4197. return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
  4198. }
  4199. static inline int rt_scope(int ifa_scope)
  4200. {
  4201. if (ifa_scope & IFA_HOST)
  4202. return RT_SCOPE_HOST;
  4203. else if (ifa_scope & IFA_LINK)
  4204. return RT_SCOPE_LINK;
  4205. else if (ifa_scope & IFA_SITE)
  4206. return RT_SCOPE_SITE;
  4207. else
  4208. return RT_SCOPE_UNIVERSE;
  4209. }
  4210. static inline int inet6_ifaddr_msgsize(void)
  4211. {
  4212. return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
  4213. + nla_total_size(16) /* IFA_LOCAL */
  4214. + nla_total_size(16) /* IFA_ADDRESS */
  4215. + nla_total_size(sizeof(struct ifa_cacheinfo))
  4216. + nla_total_size(4) /* IFA_FLAGS */
  4217. + nla_total_size(4) /* IFA_RT_PRIORITY */;
  4218. }
  4219. enum addr_type_t {
  4220. UNICAST_ADDR,
  4221. MULTICAST_ADDR,
  4222. ANYCAST_ADDR,
  4223. };
  4224. struct inet6_fill_args {
  4225. u32 portid;
  4226. u32 seq;
  4227. int event;
  4228. unsigned int flags;
  4229. int netnsid;
  4230. int ifindex;
  4231. enum addr_type_t type;
  4232. };
  4233. static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
  4234. struct inet6_fill_args *args)
  4235. {
  4236. struct nlmsghdr *nlh;
  4237. u32 preferred, valid;
  4238. nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
  4239. sizeof(struct ifaddrmsg), args->flags);
  4240. if (!nlh)
  4241. return -EMSGSIZE;
  4242. put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
  4243. ifa->idev->dev->ifindex);
  4244. if (args->netnsid >= 0 &&
  4245. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
  4246. goto error;
  4247. spin_lock_bh(&ifa->lock);
  4248. if (!((ifa->flags&IFA_F_PERMANENT) &&
  4249. (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
  4250. preferred = ifa->prefered_lft;
  4251. valid = ifa->valid_lft;
  4252. if (preferred != INFINITY_LIFE_TIME) {
  4253. long tval = (jiffies - ifa->tstamp)/HZ;
  4254. if (preferred > tval)
  4255. preferred -= tval;
  4256. else
  4257. preferred = 0;
  4258. if (valid != INFINITY_LIFE_TIME) {
  4259. if (valid > tval)
  4260. valid -= tval;
  4261. else
  4262. valid = 0;
  4263. }
  4264. }
  4265. } else {
  4266. preferred = INFINITY_LIFE_TIME;
  4267. valid = INFINITY_LIFE_TIME;
  4268. }
  4269. spin_unlock_bh(&ifa->lock);
  4270. if (!ipv6_addr_any(&ifa->peer_addr)) {
  4271. if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
  4272. nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
  4273. goto error;
  4274. } else
  4275. if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
  4276. goto error;
  4277. if (ifa->rt_priority &&
  4278. nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
  4279. goto error;
  4280. if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
  4281. goto error;
  4282. if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
  4283. goto error;
  4284. nlmsg_end(skb, nlh);
  4285. return 0;
  4286. error:
  4287. nlmsg_cancel(skb, nlh);
  4288. return -EMSGSIZE;
  4289. }
  4290. static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
  4291. struct inet6_fill_args *args)
  4292. {
  4293. struct nlmsghdr *nlh;
  4294. u8 scope = RT_SCOPE_UNIVERSE;
  4295. int ifindex = ifmca->idev->dev->ifindex;
  4296. if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
  4297. scope = RT_SCOPE_SITE;
  4298. nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
  4299. sizeof(struct ifaddrmsg), args->flags);
  4300. if (!nlh)
  4301. return -EMSGSIZE;
  4302. if (args->netnsid >= 0 &&
  4303. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
  4304. nlmsg_cancel(skb, nlh);
  4305. return -EMSGSIZE;
  4306. }
  4307. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  4308. if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
  4309. put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
  4310. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  4311. nlmsg_cancel(skb, nlh);
  4312. return -EMSGSIZE;
  4313. }
  4314. nlmsg_end(skb, nlh);
  4315. return 0;
  4316. }
  4317. static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
  4318. struct inet6_fill_args *args)
  4319. {
  4320. struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
  4321. int ifindex = dev ? dev->ifindex : 1;
  4322. struct nlmsghdr *nlh;
  4323. u8 scope = RT_SCOPE_UNIVERSE;
  4324. if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
  4325. scope = RT_SCOPE_SITE;
  4326. nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
  4327. sizeof(struct ifaddrmsg), args->flags);
  4328. if (!nlh)
  4329. return -EMSGSIZE;
  4330. if (args->netnsid >= 0 &&
  4331. nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
  4332. nlmsg_cancel(skb, nlh);
  4333. return -EMSGSIZE;
  4334. }
  4335. put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
  4336. if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
  4337. put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
  4338. INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
  4339. nlmsg_cancel(skb, nlh);
  4340. return -EMSGSIZE;
  4341. }
  4342. nlmsg_end(skb, nlh);
  4343. return 0;
  4344. }
  4345. /* called with rcu_read_lock() */
  4346. static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
  4347. struct netlink_callback *cb, int s_ip_idx,
  4348. struct inet6_fill_args *fillargs)
  4349. {
  4350. struct ifmcaddr6 *ifmca;
  4351. struct ifacaddr6 *ifaca;
  4352. int ip_idx = 0;
  4353. int err = 1;
  4354. read_lock_bh(&idev->lock);
  4355. switch (fillargs->type) {
  4356. case UNICAST_ADDR: {
  4357. struct inet6_ifaddr *ifa;
  4358. fillargs->event = RTM_NEWADDR;
  4359. /* unicast address incl. temp addr */
  4360. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  4361. if (ip_idx < s_ip_idx)
  4362. goto next;
  4363. err = inet6_fill_ifaddr(skb, ifa, fillargs);
  4364. if (err < 0)
  4365. break;
  4366. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  4367. next:
  4368. ip_idx++;
  4369. }
  4370. break;
  4371. }
  4372. case MULTICAST_ADDR:
  4373. fillargs->event = RTM_GETMULTICAST;
  4374. /* multicast address */
  4375. for (ifmca = idev->mc_list; ifmca;
  4376. ifmca = ifmca->next, ip_idx++) {
  4377. if (ip_idx < s_ip_idx)
  4378. continue;
  4379. err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
  4380. if (err < 0)
  4381. break;
  4382. }
  4383. break;
  4384. case ANYCAST_ADDR:
  4385. fillargs->event = RTM_GETANYCAST;
  4386. /* anycast address */
  4387. for (ifaca = idev->ac_list; ifaca;
  4388. ifaca = ifaca->aca_next, ip_idx++) {
  4389. if (ip_idx < s_ip_idx)
  4390. continue;
  4391. err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
  4392. if (err < 0)
  4393. break;
  4394. }
  4395. break;
  4396. default:
  4397. break;
  4398. }
  4399. read_unlock_bh(&idev->lock);
  4400. cb->args[2] = ip_idx;
  4401. return err;
  4402. }
  4403. static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
  4404. struct inet6_fill_args *fillargs,
  4405. struct net **tgt_net, struct sock *sk,
  4406. struct netlink_callback *cb)
  4407. {
  4408. struct netlink_ext_ack *extack = cb->extack;
  4409. struct nlattr *tb[IFA_MAX+1];
  4410. struct ifaddrmsg *ifm;
  4411. int err, i;
  4412. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  4413. NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
  4414. return -EINVAL;
  4415. }
  4416. ifm = nlmsg_data(nlh);
  4417. if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
  4418. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
  4419. return -EINVAL;
  4420. }
  4421. fillargs->ifindex = ifm->ifa_index;
  4422. if (fillargs->ifindex) {
  4423. cb->answer_flags |= NLM_F_DUMP_FILTERED;
  4424. fillargs->flags |= NLM_F_DUMP_FILTERED;
  4425. }
  4426. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
  4427. ifa_ipv6_policy, extack);
  4428. if (err < 0)
  4429. return err;
  4430. for (i = 0; i <= IFA_MAX; ++i) {
  4431. if (!tb[i])
  4432. continue;
  4433. if (i == IFA_TARGET_NETNSID) {
  4434. struct net *net;
  4435. fillargs->netnsid = nla_get_s32(tb[i]);
  4436. net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
  4437. if (IS_ERR(net)) {
  4438. fillargs->netnsid = -1;
  4439. NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
  4440. return PTR_ERR(net);
  4441. }
  4442. *tgt_net = net;
  4443. } else {
  4444. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
  4445. return -EINVAL;
  4446. }
  4447. }
  4448. return 0;
  4449. }
  4450. static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
  4451. enum addr_type_t type)
  4452. {
  4453. const struct nlmsghdr *nlh = cb->nlh;
  4454. struct inet6_fill_args fillargs = {
  4455. .portid = NETLINK_CB(cb->skb).portid,
  4456. .seq = cb->nlh->nlmsg_seq,
  4457. .flags = NLM_F_MULTI,
  4458. .netnsid = -1,
  4459. .type = type,
  4460. };
  4461. struct net *net = sock_net(skb->sk);
  4462. struct net *tgt_net = net;
  4463. int idx, s_idx, s_ip_idx;
  4464. int h, s_h;
  4465. struct net_device *dev;
  4466. struct inet6_dev *idev;
  4467. struct hlist_head *head;
  4468. int err = 0;
  4469. s_h = cb->args[0];
  4470. s_idx = idx = cb->args[1];
  4471. s_ip_idx = cb->args[2];
  4472. if (cb->strict_check) {
  4473. err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
  4474. skb->sk, cb);
  4475. if (err < 0)
  4476. goto put_tgt_net;
  4477. err = 0;
  4478. if (fillargs.ifindex) {
  4479. dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
  4480. if (!dev) {
  4481. err = -ENODEV;
  4482. goto put_tgt_net;
  4483. }
  4484. idev = __in6_dev_get(dev);
  4485. if (idev) {
  4486. err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
  4487. &fillargs);
  4488. if (err > 0)
  4489. err = 0;
  4490. }
  4491. goto put_tgt_net;
  4492. }
  4493. }
  4494. rcu_read_lock();
  4495. cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
  4496. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  4497. idx = 0;
  4498. head = &tgt_net->dev_index_head[h];
  4499. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  4500. if (idx < s_idx)
  4501. goto cont;
  4502. if (h > s_h || idx > s_idx)
  4503. s_ip_idx = 0;
  4504. idev = __in6_dev_get(dev);
  4505. if (!idev)
  4506. goto cont;
  4507. if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
  4508. &fillargs) < 0)
  4509. goto done;
  4510. cont:
  4511. idx++;
  4512. }
  4513. }
  4514. done:
  4515. rcu_read_unlock();
  4516. cb->args[0] = h;
  4517. cb->args[1] = idx;
  4518. put_tgt_net:
  4519. if (fillargs.netnsid >= 0)
  4520. put_net(tgt_net);
  4521. return skb->len ? : err;
  4522. }
  4523. static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4524. {
  4525. enum addr_type_t type = UNICAST_ADDR;
  4526. return inet6_dump_addr(skb, cb, type);
  4527. }
  4528. static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4529. {
  4530. enum addr_type_t type = MULTICAST_ADDR;
  4531. return inet6_dump_addr(skb, cb, type);
  4532. }
  4533. static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
  4534. {
  4535. enum addr_type_t type = ANYCAST_ADDR;
  4536. return inet6_dump_addr(skb, cb, type);
  4537. }
  4538. static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
  4539. const struct nlmsghdr *nlh,
  4540. struct nlattr **tb,
  4541. struct netlink_ext_ack *extack)
  4542. {
  4543. struct ifaddrmsg *ifm;
  4544. int i, err;
  4545. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  4546. NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
  4547. return -EINVAL;
  4548. }
  4549. if (!netlink_strict_get_check(skb))
  4550. return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
  4551. ifa_ipv6_policy, extack);
  4552. ifm = nlmsg_data(nlh);
  4553. if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
  4554. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
  4555. return -EINVAL;
  4556. }
  4557. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
  4558. ifa_ipv6_policy, extack);
  4559. if (err)
  4560. return err;
  4561. for (i = 0; i <= IFA_MAX; i++) {
  4562. if (!tb[i])
  4563. continue;
  4564. switch (i) {
  4565. case IFA_TARGET_NETNSID:
  4566. case IFA_ADDRESS:
  4567. case IFA_LOCAL:
  4568. break;
  4569. default:
  4570. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
  4571. return -EINVAL;
  4572. }
  4573. }
  4574. return 0;
  4575. }
  4576. static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
  4577. struct netlink_ext_ack *extack)
  4578. {
  4579. struct net *net = sock_net(in_skb->sk);
  4580. struct inet6_fill_args fillargs = {
  4581. .portid = NETLINK_CB(in_skb).portid,
  4582. .seq = nlh->nlmsg_seq,
  4583. .event = RTM_NEWADDR,
  4584. .flags = 0,
  4585. .netnsid = -1,
  4586. };
  4587. struct net *tgt_net = net;
  4588. struct ifaddrmsg *ifm;
  4589. struct nlattr *tb[IFA_MAX+1];
  4590. struct in6_addr *addr = NULL, *peer;
  4591. struct net_device *dev = NULL;
  4592. struct inet6_ifaddr *ifa;
  4593. struct sk_buff *skb;
  4594. int err;
  4595. err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
  4596. if (err < 0)
  4597. return err;
  4598. if (tb[IFA_TARGET_NETNSID]) {
  4599. fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
  4600. tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
  4601. fillargs.netnsid);
  4602. if (IS_ERR(tgt_net))
  4603. return PTR_ERR(tgt_net);
  4604. }
  4605. addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
  4606. if (!addr)
  4607. return -EINVAL;
  4608. ifm = nlmsg_data(nlh);
  4609. if (ifm->ifa_index)
  4610. dev = dev_get_by_index(tgt_net, ifm->ifa_index);
  4611. ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
  4612. if (!ifa) {
  4613. err = -EADDRNOTAVAIL;
  4614. goto errout;
  4615. }
  4616. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
  4617. if (!skb) {
  4618. err = -ENOBUFS;
  4619. goto errout_ifa;
  4620. }
  4621. err = inet6_fill_ifaddr(skb, ifa, &fillargs);
  4622. if (err < 0) {
  4623. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  4624. WARN_ON(err == -EMSGSIZE);
  4625. kfree_skb(skb);
  4626. goto errout_ifa;
  4627. }
  4628. err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
  4629. errout_ifa:
  4630. in6_ifa_put(ifa);
  4631. errout:
  4632. if (dev)
  4633. dev_put(dev);
  4634. if (fillargs.netnsid >= 0)
  4635. put_net(tgt_net);
  4636. return err;
  4637. }
  4638. static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
  4639. {
  4640. struct sk_buff *skb;
  4641. struct net *net = dev_net(ifa->idev->dev);
  4642. struct inet6_fill_args fillargs = {
  4643. .portid = 0,
  4644. .seq = 0,
  4645. .event = event,
  4646. .flags = 0,
  4647. .netnsid = -1,
  4648. };
  4649. int err = -ENOBUFS;
  4650. skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
  4651. if (!skb)
  4652. goto errout;
  4653. err = inet6_fill_ifaddr(skb, ifa, &fillargs);
  4654. if (err < 0) {
  4655. /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
  4656. WARN_ON(err == -EMSGSIZE);
  4657. kfree_skb(skb);
  4658. goto errout;
  4659. }
  4660. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
  4661. return;
  4662. errout:
  4663. if (err < 0)
  4664. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
  4665. }
  4666. static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
  4667. __s32 *array, int bytes)
  4668. {
  4669. BUG_ON(bytes < (DEVCONF_MAX * 4));
  4670. memset(array, 0, bytes);
  4671. array[DEVCONF_FORWARDING] = cnf->forwarding;
  4672. array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
  4673. array[DEVCONF_MTU6] = cnf->mtu6;
  4674. array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
  4675. array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
  4676. array[DEVCONF_AUTOCONF] = cnf->autoconf;
  4677. array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
  4678. array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
  4679. array[DEVCONF_RTR_SOLICIT_INTERVAL] =
  4680. jiffies_to_msecs(cnf->rtr_solicit_interval);
  4681. array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
  4682. jiffies_to_msecs(cnf->rtr_solicit_max_interval);
  4683. array[DEVCONF_RTR_SOLICIT_DELAY] =
  4684. jiffies_to_msecs(cnf->rtr_solicit_delay);
  4685. array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
  4686. array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
  4687. jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
  4688. array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
  4689. jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
  4690. array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
  4691. array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
  4692. array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
  4693. array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
  4694. array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
  4695. array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
  4696. array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
  4697. array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
  4698. array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
  4699. #ifdef CONFIG_IPV6_ROUTER_PREF
  4700. array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
  4701. array[DEVCONF_RTR_PROBE_INTERVAL] =
  4702. jiffies_to_msecs(cnf->rtr_probe_interval);
  4703. #ifdef CONFIG_IPV6_ROUTE_INFO
  4704. array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
  4705. array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
  4706. #endif
  4707. #endif
  4708. array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
  4709. array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
  4710. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  4711. array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
  4712. array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
  4713. #endif
  4714. #ifdef CONFIG_IPV6_MROUTE
  4715. array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
  4716. #endif
  4717. array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
  4718. array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
  4719. array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
  4720. array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
  4721. array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
  4722. array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
  4723. array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
  4724. array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
  4725. /* we omit DEVCONF_STABLE_SECRET for now */
  4726. array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
  4727. array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
  4728. array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
  4729. array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
  4730. array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
  4731. #ifdef CONFIG_IPV6_SEG6_HMAC
  4732. array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
  4733. #endif
  4734. array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
  4735. array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
  4736. array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
  4737. array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
  4738. array[DEVCONF_RPL_SEG_ENABLED] = cnf->rpl_seg_enabled;
  4739. }
  4740. static inline size_t inet6_ifla6_size(void)
  4741. {
  4742. return nla_total_size(4) /* IFLA_INET6_FLAGS */
  4743. + nla_total_size(sizeof(struct ifla_cacheinfo))
  4744. + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
  4745. + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
  4746. + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
  4747. + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
  4748. + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
  4749. + 0;
  4750. }
  4751. static inline size_t inet6_if_nlmsg_size(void)
  4752. {
  4753. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  4754. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  4755. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  4756. + nla_total_size(4) /* IFLA_MTU */
  4757. + nla_total_size(4) /* IFLA_LINK */
  4758. + nla_total_size(1) /* IFLA_OPERSTATE */
  4759. + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
  4760. }
  4761. static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
  4762. int bytes)
  4763. {
  4764. int i;
  4765. int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
  4766. BUG_ON(pad < 0);
  4767. /* Use put_unaligned() because stats may not be aligned for u64. */
  4768. put_unaligned(ICMP6_MIB_MAX, &stats[0]);
  4769. for (i = 1; i < ICMP6_MIB_MAX; i++)
  4770. put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
  4771. memset(&stats[ICMP6_MIB_MAX], 0, pad);
  4772. }
  4773. static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
  4774. int bytes, size_t syncpoff)
  4775. {
  4776. int i, c;
  4777. u64 buff[IPSTATS_MIB_MAX];
  4778. int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
  4779. BUG_ON(pad < 0);
  4780. memset(buff, 0, sizeof(buff));
  4781. buff[0] = IPSTATS_MIB_MAX;
  4782. for_each_possible_cpu(c) {
  4783. for (i = 1; i < IPSTATS_MIB_MAX; i++)
  4784. buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
  4785. }
  4786. memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
  4787. memset(&stats[IPSTATS_MIB_MAX], 0, pad);
  4788. }
  4789. static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
  4790. int bytes)
  4791. {
  4792. switch (attrtype) {
  4793. case IFLA_INET6_STATS:
  4794. __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
  4795. offsetof(struct ipstats_mib, syncp));
  4796. break;
  4797. case IFLA_INET6_ICMP6STATS:
  4798. __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
  4799. break;
  4800. }
  4801. }
  4802. static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
  4803. u32 ext_filter_mask)
  4804. {
  4805. struct nlattr *nla;
  4806. struct ifla_cacheinfo ci;
  4807. if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
  4808. goto nla_put_failure;
  4809. ci.max_reasm_len = IPV6_MAXPLEN;
  4810. ci.tstamp = cstamp_delta(idev->tstamp);
  4811. ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
  4812. ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
  4813. if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
  4814. goto nla_put_failure;
  4815. nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
  4816. if (!nla)
  4817. goto nla_put_failure;
  4818. ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
  4819. /* XXX - MC not implemented */
  4820. if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
  4821. return 0;
  4822. nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
  4823. if (!nla)
  4824. goto nla_put_failure;
  4825. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
  4826. nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
  4827. if (!nla)
  4828. goto nla_put_failure;
  4829. snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
  4830. nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
  4831. if (!nla)
  4832. goto nla_put_failure;
  4833. read_lock_bh(&idev->lock);
  4834. memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
  4835. read_unlock_bh(&idev->lock);
  4836. if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
  4837. goto nla_put_failure;
  4838. return 0;
  4839. nla_put_failure:
  4840. return -EMSGSIZE;
  4841. }
  4842. static size_t inet6_get_link_af_size(const struct net_device *dev,
  4843. u32 ext_filter_mask)
  4844. {
  4845. if (!__in6_dev_get(dev))
  4846. return 0;
  4847. return inet6_ifla6_size();
  4848. }
  4849. static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
  4850. u32 ext_filter_mask)
  4851. {
  4852. struct inet6_dev *idev = __in6_dev_get(dev);
  4853. if (!idev)
  4854. return -ENODATA;
  4855. if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
  4856. return -EMSGSIZE;
  4857. return 0;
  4858. }
  4859. static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
  4860. {
  4861. struct inet6_ifaddr *ifp;
  4862. struct net_device *dev = idev->dev;
  4863. bool clear_token, update_rs = false;
  4864. struct in6_addr ll_addr;
  4865. ASSERT_RTNL();
  4866. if (!token)
  4867. return -EINVAL;
  4868. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
  4869. return -EINVAL;
  4870. if (!ipv6_accept_ra(idev))
  4871. return -EINVAL;
  4872. if (idev->cnf.rtr_solicits == 0)
  4873. return -EINVAL;
  4874. write_lock_bh(&idev->lock);
  4875. BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
  4876. memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
  4877. write_unlock_bh(&idev->lock);
  4878. clear_token = ipv6_addr_any(token);
  4879. if (clear_token)
  4880. goto update_lft;
  4881. if (!idev->dead && (idev->if_flags & IF_READY) &&
  4882. !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
  4883. IFA_F_OPTIMISTIC)) {
  4884. /* If we're not ready, then normal ifup will take care
  4885. * of this. Otherwise, we need to request our rs here.
  4886. */
  4887. ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
  4888. update_rs = true;
  4889. }
  4890. update_lft:
  4891. write_lock_bh(&idev->lock);
  4892. if (update_rs) {
  4893. idev->if_flags |= IF_RS_SENT;
  4894. idev->rs_interval = rfc3315_s14_backoff_init(
  4895. idev->cnf.rtr_solicit_interval);
  4896. idev->rs_probes = 1;
  4897. addrconf_mod_rs_timer(idev, idev->rs_interval);
  4898. }
  4899. /* Well, that's kinda nasty ... */
  4900. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  4901. spin_lock(&ifp->lock);
  4902. if (ifp->tokenized) {
  4903. ifp->valid_lft = 0;
  4904. ifp->prefered_lft = 0;
  4905. }
  4906. spin_unlock(&ifp->lock);
  4907. }
  4908. write_unlock_bh(&idev->lock);
  4909. inet6_ifinfo_notify(RTM_NEWLINK, idev);
  4910. addrconf_verify_rtnl();
  4911. return 0;
  4912. }
  4913. static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
  4914. [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
  4915. [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
  4916. };
  4917. static int check_addr_gen_mode(int mode)
  4918. {
  4919. if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
  4920. mode != IN6_ADDR_GEN_MODE_NONE &&
  4921. mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
  4922. mode != IN6_ADDR_GEN_MODE_RANDOM)
  4923. return -EINVAL;
  4924. return 1;
  4925. }
  4926. static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
  4927. int mode)
  4928. {
  4929. if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
  4930. !idev->cnf.stable_secret.initialized &&
  4931. !net->ipv6.devconf_dflt->stable_secret.initialized)
  4932. return -EINVAL;
  4933. return 1;
  4934. }
  4935. static int inet6_validate_link_af(const struct net_device *dev,
  4936. const struct nlattr *nla)
  4937. {
  4938. struct nlattr *tb[IFLA_INET6_MAX + 1];
  4939. struct inet6_dev *idev = NULL;
  4940. int err;
  4941. if (dev) {
  4942. idev = __in6_dev_get(dev);
  4943. if (!idev)
  4944. return -EAFNOSUPPORT;
  4945. }
  4946. err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
  4947. inet6_af_policy, NULL);
  4948. if (err)
  4949. return err;
  4950. if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
  4951. return -EINVAL;
  4952. if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
  4953. u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
  4954. if (check_addr_gen_mode(mode) < 0)
  4955. return -EINVAL;
  4956. if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
  4957. return -EINVAL;
  4958. }
  4959. return 0;
  4960. }
  4961. static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
  4962. {
  4963. struct inet6_dev *idev = __in6_dev_get(dev);
  4964. struct nlattr *tb[IFLA_INET6_MAX + 1];
  4965. int err;
  4966. if (!idev)
  4967. return -EAFNOSUPPORT;
  4968. if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
  4969. return -EINVAL;
  4970. if (tb[IFLA_INET6_TOKEN]) {
  4971. err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
  4972. if (err)
  4973. return err;
  4974. }
  4975. if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
  4976. u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
  4977. idev->cnf.addr_gen_mode = mode;
  4978. }
  4979. return 0;
  4980. }
  4981. static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
  4982. u32 portid, u32 seq, int event, unsigned int flags)
  4983. {
  4984. struct net_device *dev = idev->dev;
  4985. struct ifinfomsg *hdr;
  4986. struct nlmsghdr *nlh;
  4987. void *protoinfo;
  4988. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
  4989. if (!nlh)
  4990. return -EMSGSIZE;
  4991. hdr = nlmsg_data(nlh);
  4992. hdr->ifi_family = AF_INET6;
  4993. hdr->__ifi_pad = 0;
  4994. hdr->ifi_type = dev->type;
  4995. hdr->ifi_index = dev->ifindex;
  4996. hdr->ifi_flags = dev_get_flags(dev);
  4997. hdr->ifi_change = 0;
  4998. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  4999. (dev->addr_len &&
  5000. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  5001. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  5002. (dev->ifindex != dev_get_iflink(dev) &&
  5003. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
  5004. nla_put_u8(skb, IFLA_OPERSTATE,
  5005. netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
  5006. goto nla_put_failure;
  5007. protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
  5008. if (!protoinfo)
  5009. goto nla_put_failure;
  5010. if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
  5011. goto nla_put_failure;
  5012. nla_nest_end(skb, protoinfo);
  5013. nlmsg_end(skb, nlh);
  5014. return 0;
  5015. nla_put_failure:
  5016. nlmsg_cancel(skb, nlh);
  5017. return -EMSGSIZE;
  5018. }
  5019. static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
  5020. struct netlink_ext_ack *extack)
  5021. {
  5022. struct ifinfomsg *ifm;
  5023. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
  5024. NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
  5025. return -EINVAL;
  5026. }
  5027. if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
  5028. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
  5029. return -EINVAL;
  5030. }
  5031. ifm = nlmsg_data(nlh);
  5032. if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
  5033. ifm->ifi_change || ifm->ifi_index) {
  5034. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
  5035. return -EINVAL;
  5036. }
  5037. return 0;
  5038. }
  5039. static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
  5040. {
  5041. struct net *net = sock_net(skb->sk);
  5042. int h, s_h;
  5043. int idx = 0, s_idx;
  5044. struct net_device *dev;
  5045. struct inet6_dev *idev;
  5046. struct hlist_head *head;
  5047. /* only requests using strict checking can pass data to
  5048. * influence the dump
  5049. */
  5050. if (cb->strict_check) {
  5051. int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
  5052. if (err < 0)
  5053. return err;
  5054. }
  5055. s_h = cb->args[0];
  5056. s_idx = cb->args[1];
  5057. rcu_read_lock();
  5058. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  5059. idx = 0;
  5060. head = &net->dev_index_head[h];
  5061. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  5062. if (idx < s_idx)
  5063. goto cont;
  5064. idev = __in6_dev_get(dev);
  5065. if (!idev)
  5066. goto cont;
  5067. if (inet6_fill_ifinfo(skb, idev,
  5068. NETLINK_CB(cb->skb).portid,
  5069. cb->nlh->nlmsg_seq,
  5070. RTM_NEWLINK, NLM_F_MULTI) < 0)
  5071. goto out;
  5072. cont:
  5073. idx++;
  5074. }
  5075. }
  5076. out:
  5077. rcu_read_unlock();
  5078. cb->args[1] = idx;
  5079. cb->args[0] = h;
  5080. return skb->len;
  5081. }
  5082. void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
  5083. {
  5084. struct sk_buff *skb;
  5085. struct net *net = dev_net(idev->dev);
  5086. int err = -ENOBUFS;
  5087. skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
  5088. if (!skb)
  5089. goto errout;
  5090. err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
  5091. if (err < 0) {
  5092. /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
  5093. WARN_ON(err == -EMSGSIZE);
  5094. kfree_skb(skb);
  5095. goto errout;
  5096. }
  5097. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
  5098. return;
  5099. errout:
  5100. if (err < 0)
  5101. rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
  5102. }
  5103. static inline size_t inet6_prefix_nlmsg_size(void)
  5104. {
  5105. return NLMSG_ALIGN(sizeof(struct prefixmsg))
  5106. + nla_total_size(sizeof(struct in6_addr))
  5107. + nla_total_size(sizeof(struct prefix_cacheinfo));
  5108. }
  5109. static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
  5110. struct prefix_info *pinfo, u32 portid, u32 seq,
  5111. int event, unsigned int flags)
  5112. {
  5113. struct prefixmsg *pmsg;
  5114. struct nlmsghdr *nlh;
  5115. struct prefix_cacheinfo ci;
  5116. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
  5117. if (!nlh)
  5118. return -EMSGSIZE;
  5119. pmsg = nlmsg_data(nlh);
  5120. pmsg->prefix_family = AF_INET6;
  5121. pmsg->prefix_pad1 = 0;
  5122. pmsg->prefix_pad2 = 0;
  5123. pmsg->prefix_ifindex = idev->dev->ifindex;
  5124. pmsg->prefix_len = pinfo->prefix_len;
  5125. pmsg->prefix_type = pinfo->type;
  5126. pmsg->prefix_pad3 = 0;
  5127. pmsg->prefix_flags = 0;
  5128. if (pinfo->onlink)
  5129. pmsg->prefix_flags |= IF_PREFIX_ONLINK;
  5130. if (pinfo->autoconf)
  5131. pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
  5132. if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
  5133. goto nla_put_failure;
  5134. ci.preferred_time = ntohl(pinfo->prefered);
  5135. ci.valid_time = ntohl(pinfo->valid);
  5136. if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
  5137. goto nla_put_failure;
  5138. nlmsg_end(skb, nlh);
  5139. return 0;
  5140. nla_put_failure:
  5141. nlmsg_cancel(skb, nlh);
  5142. return -EMSGSIZE;
  5143. }
  5144. static void inet6_prefix_notify(int event, struct inet6_dev *idev,
  5145. struct prefix_info *pinfo)
  5146. {
  5147. struct sk_buff *skb;
  5148. struct net *net = dev_net(idev->dev);
  5149. int err = -ENOBUFS;
  5150. skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
  5151. if (!skb)
  5152. goto errout;
  5153. err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
  5154. if (err < 0) {
  5155. /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
  5156. WARN_ON(err == -EMSGSIZE);
  5157. kfree_skb(skb);
  5158. goto errout;
  5159. }
  5160. rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
  5161. return;
  5162. errout:
  5163. if (err < 0)
  5164. rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
  5165. }
  5166. static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  5167. {
  5168. struct net *net = dev_net(ifp->idev->dev);
  5169. if (event)
  5170. ASSERT_RTNL();
  5171. inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
  5172. switch (event) {
  5173. case RTM_NEWADDR:
  5174. /*
  5175. * If the address was optimistic we inserted the route at the
  5176. * start of our DAD process, so we don't need to do it again.
  5177. * If the device was taken down in the middle of the DAD
  5178. * cycle there is a race where we could get here without a
  5179. * host route, so nothing to insert. That will be fixed when
  5180. * the device is brought up.
  5181. */
  5182. if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
  5183. ip6_ins_rt(net, ifp->rt);
  5184. } else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
  5185. pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
  5186. &ifp->addr, ifp->idev->dev->name);
  5187. }
  5188. if (ifp->idev->cnf.forwarding)
  5189. addrconf_join_anycast(ifp);
  5190. if (!ipv6_addr_any(&ifp->peer_addr))
  5191. addrconf_prefix_route(&ifp->peer_addr, 128,
  5192. ifp->rt_priority, ifp->idev->dev,
  5193. 0, 0, GFP_ATOMIC);
  5194. break;
  5195. case RTM_DELADDR:
  5196. if (ifp->idev->cnf.forwarding)
  5197. addrconf_leave_anycast(ifp);
  5198. addrconf_leave_solict(ifp->idev, &ifp->addr);
  5199. if (!ipv6_addr_any(&ifp->peer_addr)) {
  5200. struct fib6_info *rt;
  5201. rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
  5202. ifp->idev->dev, 0, 0,
  5203. false);
  5204. if (rt)
  5205. ip6_del_rt(net, rt, false);
  5206. }
  5207. if (ifp->rt) {
  5208. ip6_del_rt(net, ifp->rt, false);
  5209. ifp->rt = NULL;
  5210. }
  5211. rt_genid_bump_ipv6(net);
  5212. break;
  5213. }
  5214. atomic_inc(&net->ipv6.dev_addr_genid);
  5215. }
  5216. static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
  5217. {
  5218. rcu_read_lock_bh();
  5219. if (likely(ifp->idev->dead == 0))
  5220. __ipv6_ifa_notify(event, ifp);
  5221. rcu_read_unlock_bh();
  5222. }
  5223. #ifdef CONFIG_SYSCTL
  5224. static int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
  5225. void *buffer, size_t *lenp, loff_t *ppos)
  5226. {
  5227. int *valp = ctl->data;
  5228. int val = *valp;
  5229. loff_t pos = *ppos;
  5230. struct ctl_table lctl;
  5231. int ret;
  5232. /*
  5233. * ctl->data points to idev->cnf.forwarding, we should
  5234. * not modify it until we get the rtnl lock.
  5235. */
  5236. lctl = *ctl;
  5237. lctl.data = &val;
  5238. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5239. if (write)
  5240. ret = addrconf_fixup_forwarding(ctl, valp, val);
  5241. if (ret)
  5242. *ppos = pos;
  5243. return ret;
  5244. }
  5245. static int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
  5246. void *buffer, size_t *lenp, loff_t *ppos)
  5247. {
  5248. struct inet6_dev *idev = ctl->extra1;
  5249. int min_mtu = IPV6_MIN_MTU;
  5250. struct ctl_table lctl;
  5251. lctl = *ctl;
  5252. lctl.extra1 = &min_mtu;
  5253. lctl.extra2 = idev ? &idev->dev->mtu : NULL;
  5254. return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
  5255. }
  5256. static void dev_disable_change(struct inet6_dev *idev)
  5257. {
  5258. struct netdev_notifier_info info;
  5259. if (!idev || !idev->dev)
  5260. return;
  5261. netdev_notifier_info_init(&info, idev->dev);
  5262. if (idev->cnf.disable_ipv6)
  5263. addrconf_notify(NULL, NETDEV_DOWN, &info);
  5264. else
  5265. addrconf_notify(NULL, NETDEV_UP, &info);
  5266. }
  5267. static void addrconf_disable_change(struct net *net, __s32 newf)
  5268. {
  5269. struct net_device *dev;
  5270. struct inet6_dev *idev;
  5271. for_each_netdev(net, dev) {
  5272. idev = __in6_dev_get(dev);
  5273. if (idev) {
  5274. int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
  5275. idev->cnf.disable_ipv6 = newf;
  5276. if (changed)
  5277. dev_disable_change(idev);
  5278. }
  5279. }
  5280. }
  5281. static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
  5282. {
  5283. struct net *net;
  5284. int old;
  5285. if (!rtnl_trylock())
  5286. return restart_syscall();
  5287. net = (struct net *)table->extra2;
  5288. old = *p;
  5289. *p = newf;
  5290. if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
  5291. rtnl_unlock();
  5292. return 0;
  5293. }
  5294. if (p == &net->ipv6.devconf_all->disable_ipv6) {
  5295. net->ipv6.devconf_dflt->disable_ipv6 = newf;
  5296. addrconf_disable_change(net, newf);
  5297. } else if ((!newf) ^ (!old))
  5298. dev_disable_change((struct inet6_dev *)table->extra1);
  5299. rtnl_unlock();
  5300. return 0;
  5301. }
  5302. static int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
  5303. void *buffer, size_t *lenp, loff_t *ppos)
  5304. {
  5305. int *valp = ctl->data;
  5306. int val = *valp;
  5307. loff_t pos = *ppos;
  5308. struct ctl_table lctl;
  5309. int ret;
  5310. /*
  5311. * ctl->data points to idev->cnf.disable_ipv6, we should
  5312. * not modify it until we get the rtnl lock.
  5313. */
  5314. lctl = *ctl;
  5315. lctl.data = &val;
  5316. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5317. if (write)
  5318. ret = addrconf_disable_ipv6(ctl, valp, val);
  5319. if (ret)
  5320. *ppos = pos;
  5321. return ret;
  5322. }
  5323. static int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
  5324. void *buffer, size_t *lenp, loff_t *ppos)
  5325. {
  5326. int *valp = ctl->data;
  5327. int ret;
  5328. int old, new;
  5329. old = *valp;
  5330. ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  5331. new = *valp;
  5332. if (write && old != new) {
  5333. struct net *net = ctl->extra2;
  5334. if (!rtnl_trylock())
  5335. return restart_syscall();
  5336. if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
  5337. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5338. NETCONFA_PROXY_NEIGH,
  5339. NETCONFA_IFINDEX_DEFAULT,
  5340. net->ipv6.devconf_dflt);
  5341. else if (valp == &net->ipv6.devconf_all->proxy_ndp)
  5342. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5343. NETCONFA_PROXY_NEIGH,
  5344. NETCONFA_IFINDEX_ALL,
  5345. net->ipv6.devconf_all);
  5346. else {
  5347. struct inet6_dev *idev = ctl->extra1;
  5348. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
  5349. NETCONFA_PROXY_NEIGH,
  5350. idev->dev->ifindex,
  5351. &idev->cnf);
  5352. }
  5353. rtnl_unlock();
  5354. }
  5355. return ret;
  5356. }
  5357. static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
  5358. void *buffer, size_t *lenp,
  5359. loff_t *ppos)
  5360. {
  5361. int ret = 0;
  5362. u32 new_val;
  5363. struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
  5364. struct net *net = (struct net *)ctl->extra2;
  5365. struct ctl_table tmp = {
  5366. .data = &new_val,
  5367. .maxlen = sizeof(new_val),
  5368. .mode = ctl->mode,
  5369. };
  5370. if (!rtnl_trylock())
  5371. return restart_syscall();
  5372. new_val = *((u32 *)ctl->data);
  5373. ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
  5374. if (ret != 0)
  5375. goto out;
  5376. if (write) {
  5377. if (check_addr_gen_mode(new_val) < 0) {
  5378. ret = -EINVAL;
  5379. goto out;
  5380. }
  5381. if (idev) {
  5382. if (check_stable_privacy(idev, net, new_val) < 0) {
  5383. ret = -EINVAL;
  5384. goto out;
  5385. }
  5386. if (idev->cnf.addr_gen_mode != new_val) {
  5387. idev->cnf.addr_gen_mode = new_val;
  5388. addrconf_dev_config(idev->dev);
  5389. }
  5390. } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
  5391. struct net_device *dev;
  5392. net->ipv6.devconf_dflt->addr_gen_mode = new_val;
  5393. for_each_netdev(net, dev) {
  5394. idev = __in6_dev_get(dev);
  5395. if (idev &&
  5396. idev->cnf.addr_gen_mode != new_val) {
  5397. idev->cnf.addr_gen_mode = new_val;
  5398. addrconf_dev_config(idev->dev);
  5399. }
  5400. }
  5401. }
  5402. *((u32 *)ctl->data) = new_val;
  5403. }
  5404. out:
  5405. rtnl_unlock();
  5406. return ret;
  5407. }
  5408. static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
  5409. void *buffer, size_t *lenp,
  5410. loff_t *ppos)
  5411. {
  5412. int err;
  5413. struct in6_addr addr;
  5414. char str[IPV6_MAX_STRLEN];
  5415. struct ctl_table lctl = *ctl;
  5416. struct net *net = ctl->extra2;
  5417. struct ipv6_stable_secret *secret = ctl->data;
  5418. if (&net->ipv6.devconf_all->stable_secret == ctl->data)
  5419. return -EIO;
  5420. lctl.maxlen = IPV6_MAX_STRLEN;
  5421. lctl.data = str;
  5422. if (!rtnl_trylock())
  5423. return restart_syscall();
  5424. if (!write && !secret->initialized) {
  5425. err = -EIO;
  5426. goto out;
  5427. }
  5428. err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
  5429. if (err >= sizeof(str)) {
  5430. err = -EIO;
  5431. goto out;
  5432. }
  5433. err = proc_dostring(&lctl, write, buffer, lenp, ppos);
  5434. if (err || !write)
  5435. goto out;
  5436. if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
  5437. err = -EIO;
  5438. goto out;
  5439. }
  5440. secret->initialized = true;
  5441. secret->secret = addr;
  5442. if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
  5443. struct net_device *dev;
  5444. for_each_netdev(net, dev) {
  5445. struct inet6_dev *idev = __in6_dev_get(dev);
  5446. if (idev) {
  5447. idev->cnf.addr_gen_mode =
  5448. IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
  5449. }
  5450. }
  5451. } else {
  5452. struct inet6_dev *idev = ctl->extra1;
  5453. idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
  5454. }
  5455. out:
  5456. rtnl_unlock();
  5457. return err;
  5458. }
  5459. static
  5460. int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
  5461. int write, void *buffer,
  5462. size_t *lenp,
  5463. loff_t *ppos)
  5464. {
  5465. int *valp = ctl->data;
  5466. int val = *valp;
  5467. loff_t pos = *ppos;
  5468. struct ctl_table lctl;
  5469. int ret;
  5470. /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
  5471. * we should not modify it until we get the rtnl lock.
  5472. */
  5473. lctl = *ctl;
  5474. lctl.data = &val;
  5475. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5476. if (write)
  5477. ret = addrconf_fixup_linkdown(ctl, valp, val);
  5478. if (ret)
  5479. *ppos = pos;
  5480. return ret;
  5481. }
  5482. static
  5483. void addrconf_set_nopolicy(struct rt6_info *rt, int action)
  5484. {
  5485. if (rt) {
  5486. if (action)
  5487. rt->dst.flags |= DST_NOPOLICY;
  5488. else
  5489. rt->dst.flags &= ~DST_NOPOLICY;
  5490. }
  5491. }
  5492. static
  5493. void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
  5494. {
  5495. struct inet6_ifaddr *ifa;
  5496. read_lock_bh(&idev->lock);
  5497. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  5498. spin_lock(&ifa->lock);
  5499. if (ifa->rt) {
  5500. /* host routes only use builtin fib6_nh */
  5501. struct fib6_nh *nh = ifa->rt->fib6_nh;
  5502. int cpu;
  5503. rcu_read_lock();
  5504. ifa->rt->dst_nopolicy = val ? true : false;
  5505. if (nh->rt6i_pcpu) {
  5506. for_each_possible_cpu(cpu) {
  5507. struct rt6_info **rtp;
  5508. rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
  5509. addrconf_set_nopolicy(*rtp, val);
  5510. }
  5511. }
  5512. rcu_read_unlock();
  5513. }
  5514. spin_unlock(&ifa->lock);
  5515. }
  5516. read_unlock_bh(&idev->lock);
  5517. }
  5518. static
  5519. int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
  5520. {
  5521. struct inet6_dev *idev;
  5522. struct net *net;
  5523. if (!rtnl_trylock())
  5524. return restart_syscall();
  5525. *valp = val;
  5526. net = (struct net *)ctl->extra2;
  5527. if (valp == &net->ipv6.devconf_dflt->disable_policy) {
  5528. rtnl_unlock();
  5529. return 0;
  5530. }
  5531. if (valp == &net->ipv6.devconf_all->disable_policy) {
  5532. struct net_device *dev;
  5533. for_each_netdev(net, dev) {
  5534. idev = __in6_dev_get(dev);
  5535. if (idev)
  5536. addrconf_disable_policy_idev(idev, val);
  5537. }
  5538. } else {
  5539. idev = (struct inet6_dev *)ctl->extra1;
  5540. addrconf_disable_policy_idev(idev, val);
  5541. }
  5542. rtnl_unlock();
  5543. return 0;
  5544. }
  5545. static int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
  5546. void *buffer, size_t *lenp, loff_t *ppos)
  5547. {
  5548. int *valp = ctl->data;
  5549. int val = *valp;
  5550. loff_t pos = *ppos;
  5551. struct ctl_table lctl;
  5552. int ret;
  5553. lctl = *ctl;
  5554. lctl.data = &val;
  5555. ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
  5556. if (write && (*valp != val))
  5557. ret = addrconf_disable_policy(ctl, valp, val);
  5558. if (ret)
  5559. *ppos = pos;
  5560. return ret;
  5561. }
  5562. static int minus_one = -1;
  5563. static const int two_five_five = 255;
  5564. static const struct ctl_table addrconf_sysctl[] = {
  5565. {
  5566. .procname = "forwarding",
  5567. .data = &ipv6_devconf.forwarding,
  5568. .maxlen = sizeof(int),
  5569. .mode = 0644,
  5570. .proc_handler = addrconf_sysctl_forward,
  5571. },
  5572. {
  5573. .procname = "hop_limit",
  5574. .data = &ipv6_devconf.hop_limit,
  5575. .maxlen = sizeof(int),
  5576. .mode = 0644,
  5577. .proc_handler = proc_dointvec_minmax,
  5578. .extra1 = (void *)SYSCTL_ONE,
  5579. .extra2 = (void *)&two_five_five,
  5580. },
  5581. {
  5582. .procname = "mtu",
  5583. .data = &ipv6_devconf.mtu6,
  5584. .maxlen = sizeof(int),
  5585. .mode = 0644,
  5586. .proc_handler = addrconf_sysctl_mtu,
  5587. },
  5588. {
  5589. .procname = "accept_ra",
  5590. .data = &ipv6_devconf.accept_ra,
  5591. .maxlen = sizeof(int),
  5592. .mode = 0644,
  5593. .proc_handler = proc_dointvec,
  5594. },
  5595. {
  5596. .procname = "accept_redirects",
  5597. .data = &ipv6_devconf.accept_redirects,
  5598. .maxlen = sizeof(int),
  5599. .mode = 0644,
  5600. .proc_handler = proc_dointvec,
  5601. },
  5602. {
  5603. .procname = "autoconf",
  5604. .data = &ipv6_devconf.autoconf,
  5605. .maxlen = sizeof(int),
  5606. .mode = 0644,
  5607. .proc_handler = proc_dointvec,
  5608. },
  5609. {
  5610. .procname = "dad_transmits",
  5611. .data = &ipv6_devconf.dad_transmits,
  5612. .maxlen = sizeof(int),
  5613. .mode = 0644,
  5614. .proc_handler = proc_dointvec,
  5615. },
  5616. {
  5617. .procname = "router_solicitations",
  5618. .data = &ipv6_devconf.rtr_solicits,
  5619. .maxlen = sizeof(int),
  5620. .mode = 0644,
  5621. .proc_handler = proc_dointvec_minmax,
  5622. .extra1 = &minus_one,
  5623. },
  5624. {
  5625. .procname = "router_solicitation_interval",
  5626. .data = &ipv6_devconf.rtr_solicit_interval,
  5627. .maxlen = sizeof(int),
  5628. .mode = 0644,
  5629. .proc_handler = proc_dointvec_jiffies,
  5630. },
  5631. {
  5632. .procname = "router_solicitation_max_interval",
  5633. .data = &ipv6_devconf.rtr_solicit_max_interval,
  5634. .maxlen = sizeof(int),
  5635. .mode = 0644,
  5636. .proc_handler = proc_dointvec_jiffies,
  5637. },
  5638. {
  5639. .procname = "router_solicitation_delay",
  5640. .data = &ipv6_devconf.rtr_solicit_delay,
  5641. .maxlen = sizeof(int),
  5642. .mode = 0644,
  5643. .proc_handler = proc_dointvec_jiffies,
  5644. },
  5645. {
  5646. .procname = "force_mld_version",
  5647. .data = &ipv6_devconf.force_mld_version,
  5648. .maxlen = sizeof(int),
  5649. .mode = 0644,
  5650. .proc_handler = proc_dointvec,
  5651. },
  5652. {
  5653. .procname = "mldv1_unsolicited_report_interval",
  5654. .data =
  5655. &ipv6_devconf.mldv1_unsolicited_report_interval,
  5656. .maxlen = sizeof(int),
  5657. .mode = 0644,
  5658. .proc_handler = proc_dointvec_ms_jiffies,
  5659. },
  5660. {
  5661. .procname = "mldv2_unsolicited_report_interval",
  5662. .data =
  5663. &ipv6_devconf.mldv2_unsolicited_report_interval,
  5664. .maxlen = sizeof(int),
  5665. .mode = 0644,
  5666. .proc_handler = proc_dointvec_ms_jiffies,
  5667. },
  5668. {
  5669. .procname = "use_tempaddr",
  5670. .data = &ipv6_devconf.use_tempaddr,
  5671. .maxlen = sizeof(int),
  5672. .mode = 0644,
  5673. .proc_handler = proc_dointvec,
  5674. },
  5675. {
  5676. .procname = "temp_valid_lft",
  5677. .data = &ipv6_devconf.temp_valid_lft,
  5678. .maxlen = sizeof(int),
  5679. .mode = 0644,
  5680. .proc_handler = proc_dointvec,
  5681. },
  5682. {
  5683. .procname = "temp_prefered_lft",
  5684. .data = &ipv6_devconf.temp_prefered_lft,
  5685. .maxlen = sizeof(int),
  5686. .mode = 0644,
  5687. .proc_handler = proc_dointvec,
  5688. },
  5689. {
  5690. .procname = "regen_max_retry",
  5691. .data = &ipv6_devconf.regen_max_retry,
  5692. .maxlen = sizeof(int),
  5693. .mode = 0644,
  5694. .proc_handler = proc_dointvec,
  5695. },
  5696. {
  5697. .procname = "max_desync_factor",
  5698. .data = &ipv6_devconf.max_desync_factor,
  5699. .maxlen = sizeof(int),
  5700. .mode = 0644,
  5701. .proc_handler = proc_dointvec,
  5702. },
  5703. {
  5704. .procname = "max_addresses",
  5705. .data = &ipv6_devconf.max_addresses,
  5706. .maxlen = sizeof(int),
  5707. .mode = 0644,
  5708. .proc_handler = proc_dointvec,
  5709. },
  5710. {
  5711. .procname = "accept_ra_defrtr",
  5712. .data = &ipv6_devconf.accept_ra_defrtr,
  5713. .maxlen = sizeof(int),
  5714. .mode = 0644,
  5715. .proc_handler = proc_dointvec,
  5716. },
  5717. {
  5718. .procname = "accept_ra_min_hop_limit",
  5719. .data = &ipv6_devconf.accept_ra_min_hop_limit,
  5720. .maxlen = sizeof(int),
  5721. .mode = 0644,
  5722. .proc_handler = proc_dointvec,
  5723. },
  5724. {
  5725. .procname = "accept_ra_pinfo",
  5726. .data = &ipv6_devconf.accept_ra_pinfo,
  5727. .maxlen = sizeof(int),
  5728. .mode = 0644,
  5729. .proc_handler = proc_dointvec,
  5730. },
  5731. #ifdef CONFIG_IPV6_ROUTER_PREF
  5732. {
  5733. .procname = "accept_ra_rtr_pref",
  5734. .data = &ipv6_devconf.accept_ra_rtr_pref,
  5735. .maxlen = sizeof(int),
  5736. .mode = 0644,
  5737. .proc_handler = proc_dointvec,
  5738. },
  5739. {
  5740. .procname = "router_probe_interval",
  5741. .data = &ipv6_devconf.rtr_probe_interval,
  5742. .maxlen = sizeof(int),
  5743. .mode = 0644,
  5744. .proc_handler = proc_dointvec_jiffies,
  5745. },
  5746. #ifdef CONFIG_IPV6_ROUTE_INFO
  5747. {
  5748. .procname = "accept_ra_rt_info_min_plen",
  5749. .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
  5750. .maxlen = sizeof(int),
  5751. .mode = 0644,
  5752. .proc_handler = proc_dointvec,
  5753. },
  5754. {
  5755. .procname = "accept_ra_rt_info_max_plen",
  5756. .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
  5757. .maxlen = sizeof(int),
  5758. .mode = 0644,
  5759. .proc_handler = proc_dointvec,
  5760. },
  5761. #endif
  5762. #endif
  5763. {
  5764. .procname = "accept_ra_rt_table",
  5765. .data = &ipv6_devconf.accept_ra_rt_table,
  5766. .maxlen = sizeof(int),
  5767. .mode = 0644,
  5768. .proc_handler = proc_dointvec,
  5769. },
  5770. {
  5771. .procname = "proxy_ndp",
  5772. .data = &ipv6_devconf.proxy_ndp,
  5773. .maxlen = sizeof(int),
  5774. .mode = 0644,
  5775. .proc_handler = addrconf_sysctl_proxy_ndp,
  5776. },
  5777. {
  5778. .procname = "accept_source_route",
  5779. .data = &ipv6_devconf.accept_source_route,
  5780. .maxlen = sizeof(int),
  5781. .mode = 0644,
  5782. .proc_handler = proc_dointvec,
  5783. },
  5784. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  5785. {
  5786. .procname = "optimistic_dad",
  5787. .data = &ipv6_devconf.optimistic_dad,
  5788. .maxlen = sizeof(int),
  5789. .mode = 0644,
  5790. .proc_handler = proc_dointvec,
  5791. },
  5792. {
  5793. .procname = "use_optimistic",
  5794. .data = &ipv6_devconf.use_optimistic,
  5795. .maxlen = sizeof(int),
  5796. .mode = 0644,
  5797. .proc_handler = proc_dointvec,
  5798. },
  5799. #endif
  5800. #ifdef CONFIG_IPV6_MROUTE
  5801. {
  5802. .procname = "mc_forwarding",
  5803. .data = &ipv6_devconf.mc_forwarding,
  5804. .maxlen = sizeof(int),
  5805. .mode = 0444,
  5806. .proc_handler = proc_dointvec,
  5807. },
  5808. #endif
  5809. {
  5810. .procname = "disable_ipv6",
  5811. .data = &ipv6_devconf.disable_ipv6,
  5812. .maxlen = sizeof(int),
  5813. .mode = 0644,
  5814. .proc_handler = addrconf_sysctl_disable,
  5815. },
  5816. {
  5817. .procname = "accept_dad",
  5818. .data = &ipv6_devconf.accept_dad,
  5819. .maxlen = sizeof(int),
  5820. .mode = 0644,
  5821. .proc_handler = proc_dointvec,
  5822. },
  5823. {
  5824. .procname = "force_tllao",
  5825. .data = &ipv6_devconf.force_tllao,
  5826. .maxlen = sizeof(int),
  5827. .mode = 0644,
  5828. .proc_handler = proc_dointvec
  5829. },
  5830. {
  5831. .procname = "ndisc_notify",
  5832. .data = &ipv6_devconf.ndisc_notify,
  5833. .maxlen = sizeof(int),
  5834. .mode = 0644,
  5835. .proc_handler = proc_dointvec
  5836. },
  5837. {
  5838. .procname = "suppress_frag_ndisc",
  5839. .data = &ipv6_devconf.suppress_frag_ndisc,
  5840. .maxlen = sizeof(int),
  5841. .mode = 0644,
  5842. .proc_handler = proc_dointvec
  5843. },
  5844. {
  5845. .procname = "accept_ra_from_local",
  5846. .data = &ipv6_devconf.accept_ra_from_local,
  5847. .maxlen = sizeof(int),
  5848. .mode = 0644,
  5849. .proc_handler = proc_dointvec,
  5850. },
  5851. {
  5852. .procname = "accept_ra_mtu",
  5853. .data = &ipv6_devconf.accept_ra_mtu,
  5854. .maxlen = sizeof(int),
  5855. .mode = 0644,
  5856. .proc_handler = proc_dointvec,
  5857. },
  5858. {
  5859. .procname = "stable_secret",
  5860. .data = &ipv6_devconf.stable_secret,
  5861. .maxlen = IPV6_MAX_STRLEN,
  5862. .mode = 0600,
  5863. .proc_handler = addrconf_sysctl_stable_secret,
  5864. },
  5865. {
  5866. .procname = "use_oif_addrs_only",
  5867. .data = &ipv6_devconf.use_oif_addrs_only,
  5868. .maxlen = sizeof(int),
  5869. .mode = 0644,
  5870. .proc_handler = proc_dointvec,
  5871. },
  5872. {
  5873. .procname = "ignore_routes_with_linkdown",
  5874. .data = &ipv6_devconf.ignore_routes_with_linkdown,
  5875. .maxlen = sizeof(int),
  5876. .mode = 0644,
  5877. .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
  5878. },
  5879. {
  5880. .procname = "drop_unicast_in_l2_multicast",
  5881. .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
  5882. .maxlen = sizeof(int),
  5883. .mode = 0644,
  5884. .proc_handler = proc_dointvec,
  5885. },
  5886. {
  5887. .procname = "drop_unsolicited_na",
  5888. .data = &ipv6_devconf.drop_unsolicited_na,
  5889. .maxlen = sizeof(int),
  5890. .mode = 0644,
  5891. .proc_handler = proc_dointvec,
  5892. },
  5893. {
  5894. .procname = "keep_addr_on_down",
  5895. .data = &ipv6_devconf.keep_addr_on_down,
  5896. .maxlen = sizeof(int),
  5897. .mode = 0644,
  5898. .proc_handler = proc_dointvec,
  5899. },
  5900. {
  5901. .procname = "seg6_enabled",
  5902. .data = &ipv6_devconf.seg6_enabled,
  5903. .maxlen = sizeof(int),
  5904. .mode = 0644,
  5905. .proc_handler = proc_dointvec,
  5906. },
  5907. #ifdef CONFIG_IPV6_SEG6_HMAC
  5908. {
  5909. .procname = "seg6_require_hmac",
  5910. .data = &ipv6_devconf.seg6_require_hmac,
  5911. .maxlen = sizeof(int),
  5912. .mode = 0644,
  5913. .proc_handler = proc_dointvec,
  5914. },
  5915. #endif
  5916. {
  5917. .procname = "enhanced_dad",
  5918. .data = &ipv6_devconf.enhanced_dad,
  5919. .maxlen = sizeof(int),
  5920. .mode = 0644,
  5921. .proc_handler = proc_dointvec,
  5922. },
  5923. {
  5924. .procname = "addr_gen_mode",
  5925. .data = &ipv6_devconf.addr_gen_mode,
  5926. .maxlen = sizeof(int),
  5927. .mode = 0644,
  5928. .proc_handler = addrconf_sysctl_addr_gen_mode,
  5929. },
  5930. {
  5931. .procname = "disable_policy",
  5932. .data = &ipv6_devconf.disable_policy,
  5933. .maxlen = sizeof(int),
  5934. .mode = 0644,
  5935. .proc_handler = addrconf_sysctl_disable_policy,
  5936. },
  5937. {
  5938. .procname = "ndisc_tclass",
  5939. .data = &ipv6_devconf.ndisc_tclass,
  5940. .maxlen = sizeof(int),
  5941. .mode = 0644,
  5942. .proc_handler = proc_dointvec_minmax,
  5943. .extra1 = (void *)SYSCTL_ZERO,
  5944. .extra2 = (void *)&two_five_five,
  5945. },
  5946. {
  5947. .procname = "rpl_seg_enabled",
  5948. .data = &ipv6_devconf.rpl_seg_enabled,
  5949. .maxlen = sizeof(int),
  5950. .mode = 0644,
  5951. .proc_handler = proc_dointvec,
  5952. },
  5953. {
  5954. /* sentinel */
  5955. }
  5956. };
  5957. static int __addrconf_sysctl_register(struct net *net, char *dev_name,
  5958. struct inet6_dev *idev, struct ipv6_devconf *p)
  5959. {
  5960. int i, ifindex;
  5961. struct ctl_table *table;
  5962. char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
  5963. table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
  5964. if (!table)
  5965. goto out;
  5966. for (i = 0; table[i].data; i++) {
  5967. table[i].data += (char *)p - (char *)&ipv6_devconf;
  5968. /* If one of these is already set, then it is not safe to
  5969. * overwrite either of them: this makes proc_dointvec_minmax
  5970. * usable.
  5971. */
  5972. if (!table[i].extra1 && !table[i].extra2) {
  5973. table[i].extra1 = idev; /* embedded; no ref */
  5974. table[i].extra2 = net;
  5975. }
  5976. }
  5977. snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
  5978. p->sysctl_header = register_net_sysctl(net, path, table);
  5979. if (!p->sysctl_header)
  5980. goto free;
  5981. if (!strcmp(dev_name, "all"))
  5982. ifindex = NETCONFA_IFINDEX_ALL;
  5983. else if (!strcmp(dev_name, "default"))
  5984. ifindex = NETCONFA_IFINDEX_DEFAULT;
  5985. else
  5986. ifindex = idev->dev->ifindex;
  5987. inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
  5988. ifindex, p);
  5989. return 0;
  5990. free:
  5991. kfree(table);
  5992. out:
  5993. return -ENOBUFS;
  5994. }
  5995. static void __addrconf_sysctl_unregister(struct net *net,
  5996. struct ipv6_devconf *p, int ifindex)
  5997. {
  5998. struct ctl_table *table;
  5999. if (!p->sysctl_header)
  6000. return;
  6001. table = p->sysctl_header->ctl_table_arg;
  6002. unregister_net_sysctl_table(p->sysctl_header);
  6003. p->sysctl_header = NULL;
  6004. kfree(table);
  6005. inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
  6006. }
  6007. static int addrconf_sysctl_register(struct inet6_dev *idev)
  6008. {
  6009. int err;
  6010. if (!sysctl_dev_name_is_allowed(idev->dev->name))
  6011. return -EINVAL;
  6012. err = neigh_sysctl_register(idev->dev, idev->nd_parms,
  6013. &ndisc_ifinfo_sysctl_change);
  6014. if (err)
  6015. return err;
  6016. err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
  6017. idev, &idev->cnf);
  6018. if (err)
  6019. neigh_sysctl_unregister(idev->nd_parms);
  6020. return err;
  6021. }
  6022. static void addrconf_sysctl_unregister(struct inet6_dev *idev)
  6023. {
  6024. __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
  6025. idev->dev->ifindex);
  6026. neigh_sysctl_unregister(idev->nd_parms);
  6027. }
  6028. #endif
  6029. static int __net_init addrconf_init_net(struct net *net)
  6030. {
  6031. int err = -ENOMEM;
  6032. struct ipv6_devconf *all, *dflt;
  6033. all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
  6034. if (!all)
  6035. goto err_alloc_all;
  6036. dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
  6037. if (!dflt)
  6038. goto err_alloc_dflt;
  6039. if (IS_ENABLED(CONFIG_SYSCTL) &&
  6040. !net_eq(net, &init_net)) {
  6041. switch (sysctl_devconf_inherit_init_net) {
  6042. case 1: /* copy from init_net */
  6043. memcpy(all, init_net.ipv6.devconf_all,
  6044. sizeof(ipv6_devconf));
  6045. memcpy(dflt, init_net.ipv6.devconf_dflt,
  6046. sizeof(ipv6_devconf_dflt));
  6047. break;
  6048. case 3: /* copy from the current netns */
  6049. memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
  6050. sizeof(ipv6_devconf));
  6051. memcpy(dflt,
  6052. current->nsproxy->net_ns->ipv6.devconf_dflt,
  6053. sizeof(ipv6_devconf_dflt));
  6054. break;
  6055. case 0:
  6056. case 2:
  6057. /* use compiled values */
  6058. break;
  6059. }
  6060. }
  6061. /* these will be inherited by all namespaces */
  6062. dflt->autoconf = ipv6_defaults.autoconf;
  6063. dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
  6064. dflt->stable_secret.initialized = false;
  6065. all->stable_secret.initialized = false;
  6066. net->ipv6.devconf_all = all;
  6067. net->ipv6.devconf_dflt = dflt;
  6068. #ifdef CONFIG_SYSCTL
  6069. err = __addrconf_sysctl_register(net, "all", NULL, all);
  6070. if (err < 0)
  6071. goto err_reg_all;
  6072. err = __addrconf_sysctl_register(net, "default", NULL, dflt);
  6073. if (err < 0)
  6074. goto err_reg_dflt;
  6075. #endif
  6076. return 0;
  6077. #ifdef CONFIG_SYSCTL
  6078. err_reg_dflt:
  6079. __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
  6080. err_reg_all:
  6081. kfree(dflt);
  6082. #endif
  6083. err_alloc_dflt:
  6084. kfree(all);
  6085. err_alloc_all:
  6086. return err;
  6087. }
  6088. static void __net_exit addrconf_exit_net(struct net *net)
  6089. {
  6090. #ifdef CONFIG_SYSCTL
  6091. __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
  6092. NETCONFA_IFINDEX_DEFAULT);
  6093. __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
  6094. NETCONFA_IFINDEX_ALL);
  6095. #endif
  6096. kfree(net->ipv6.devconf_dflt);
  6097. kfree(net->ipv6.devconf_all);
  6098. }
  6099. static struct pernet_operations addrconf_ops = {
  6100. .init = addrconf_init_net,
  6101. .exit = addrconf_exit_net,
  6102. };
  6103. static struct rtnl_af_ops inet6_ops __read_mostly = {
  6104. .family = AF_INET6,
  6105. .fill_link_af = inet6_fill_link_af,
  6106. .get_link_af_size = inet6_get_link_af_size,
  6107. .validate_link_af = inet6_validate_link_af,
  6108. .set_link_af = inet6_set_link_af,
  6109. };
  6110. /*
  6111. * Init / cleanup code
  6112. */
  6113. int __init addrconf_init(void)
  6114. {
  6115. struct inet6_dev *idev;
  6116. int i, err;
  6117. err = ipv6_addr_label_init();
  6118. if (err < 0) {
  6119. pr_crit("%s: cannot initialize default policy table: %d\n",
  6120. __func__, err);
  6121. goto out;
  6122. }
  6123. err = register_pernet_subsys(&addrconf_ops);
  6124. if (err < 0)
  6125. goto out_addrlabel;
  6126. addrconf_wq = create_workqueue("ipv6_addrconf");
  6127. if (!addrconf_wq) {
  6128. err = -ENOMEM;
  6129. goto out_nowq;
  6130. }
  6131. /* The addrconf netdev notifier requires that loopback_dev
  6132. * has it's ipv6 private information allocated and setup
  6133. * before it can bring up and give link-local addresses
  6134. * to other devices which are up.
  6135. *
  6136. * Unfortunately, loopback_dev is not necessarily the first
  6137. * entry in the global dev_base list of net devices. In fact,
  6138. * it is likely to be the very last entry on that list.
  6139. * So this causes the notifier registry below to try and
  6140. * give link-local addresses to all devices besides loopback_dev
  6141. * first, then loopback_dev, which cases all the non-loopback_dev
  6142. * devices to fail to get a link-local address.
  6143. *
  6144. * So, as a temporary fix, allocate the ipv6 structure for
  6145. * loopback_dev first by hand.
  6146. * Longer term, all of the dependencies ipv6 has upon the loopback
  6147. * device and it being up should be removed.
  6148. */
  6149. rtnl_lock();
  6150. idev = ipv6_add_dev(init_net.loopback_dev);
  6151. rtnl_unlock();
  6152. if (IS_ERR(idev)) {
  6153. err = PTR_ERR(idev);
  6154. goto errlo;
  6155. }
  6156. ip6_route_init_special_entries();
  6157. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  6158. INIT_HLIST_HEAD(&inet6_addr_lst[i]);
  6159. register_netdevice_notifier(&ipv6_dev_notf);
  6160. addrconf_verify();
  6161. rtnl_af_register(&inet6_ops);
  6162. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
  6163. NULL, inet6_dump_ifinfo, 0);
  6164. if (err < 0)
  6165. goto errout;
  6166. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
  6167. inet6_rtm_newaddr, NULL, 0);
  6168. if (err < 0)
  6169. goto errout;
  6170. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
  6171. inet6_rtm_deladdr, NULL, 0);
  6172. if (err < 0)
  6173. goto errout;
  6174. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
  6175. inet6_rtm_getaddr, inet6_dump_ifaddr,
  6176. RTNL_FLAG_DOIT_UNLOCKED);
  6177. if (err < 0)
  6178. goto errout;
  6179. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
  6180. NULL, inet6_dump_ifmcaddr, 0);
  6181. if (err < 0)
  6182. goto errout;
  6183. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
  6184. NULL, inet6_dump_ifacaddr, 0);
  6185. if (err < 0)
  6186. goto errout;
  6187. err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
  6188. inet6_netconf_get_devconf,
  6189. inet6_netconf_dump_devconf,
  6190. RTNL_FLAG_DOIT_UNLOCKED);
  6191. if (err < 0)
  6192. goto errout;
  6193. err = ipv6_addr_label_rtnl_register();
  6194. if (err < 0)
  6195. goto errout;
  6196. return 0;
  6197. errout:
  6198. rtnl_unregister_all(PF_INET6);
  6199. rtnl_af_unregister(&inet6_ops);
  6200. unregister_netdevice_notifier(&ipv6_dev_notf);
  6201. errlo:
  6202. destroy_workqueue(addrconf_wq);
  6203. out_nowq:
  6204. unregister_pernet_subsys(&addrconf_ops);
  6205. out_addrlabel:
  6206. ipv6_addr_label_cleanup();
  6207. out:
  6208. return err;
  6209. }
  6210. void addrconf_cleanup(void)
  6211. {
  6212. struct net_device *dev;
  6213. int i;
  6214. unregister_netdevice_notifier(&ipv6_dev_notf);
  6215. unregister_pernet_subsys(&addrconf_ops);
  6216. ipv6_addr_label_cleanup();
  6217. rtnl_af_unregister(&inet6_ops);
  6218. rtnl_lock();
  6219. /* clean dev list */
  6220. for_each_netdev(&init_net, dev) {
  6221. if (__in6_dev_get(dev) == NULL)
  6222. continue;
  6223. addrconf_ifdown(dev, true);
  6224. }
  6225. addrconf_ifdown(init_net.loopback_dev, true);
  6226. /*
  6227. * Check hash table.
  6228. */
  6229. spin_lock_bh(&addrconf_hash_lock);
  6230. for (i = 0; i < IN6_ADDR_HSIZE; i++)
  6231. WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
  6232. spin_unlock_bh(&addrconf_hash_lock);
  6233. cancel_delayed_work(&addr_chk_work);
  6234. rtnl_unlock();
  6235. destroy_workqueue(addrconf_wq);
  6236. }