addrconf.c 104 KB

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