addrconf.c 104 KB

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