addrconf.c 160 KB

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