addrconf.c 111 KB

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