addrconf.c 121 KB

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