addrconf.c 169 KB

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