addrconf.c 104 KB

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