addrconf.c 104 KB

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