addrconf.c 87 KB

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