addrconf.c 149 KB

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