addrconf.c 101 KB

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