addrconf.c 140 KB

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