addrconf.c 111 KB

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