addrconf.c 94 KB

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