addrconf.c 153 KB

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