addrconf.c 108 KB

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