addrconf.c 87 KB

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