addrconf.c 115 KB

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