addrconf.c 99 KB

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