addrconf.c 108 KB

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