addrconf.c 94 KB

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