addrconf.c 99 KB

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