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