addrconf.c 99 KB

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