addrconf.c 108 KB

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