addrconf.c 117 KB

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