addrconf.c 92 KB

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