addrconf.c 100 KB

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