addrconf.c 100 KB

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