addrconf.c 93 KB

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