addrconf.c 124 KB

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