addrconf.c 100 KB

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