addrconf.c 96 KB

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