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