addrconf.c 96 KB

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