addrconf.c 93 KB

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