addrconf.c 91 KB

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