addrconf.c 111 KB

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