addrconf.c 100 KB

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