addrconf.c 94 KB

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