addrconf.c 108 KB

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