addrconf.c 160 KB

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