addrconf.c 94 KB

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