addrconf.c 103 KB

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