addrconf.c 100 KB

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