addrconf.c 160 KB

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